Coverage Report

Created: 2026-10-02 09:53

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
0
{
2857
0
  elf_backend_data *bed;
2858
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2859
0
  const bfd_byte *erela;
2860
0
  const bfd_byte *erelaend;
2861
0
  Elf_Internal_Rela *irela;
2862
0
  Elf_Internal_Shdr *symtab_hdr;
2863
0
  size_t nsyms;
2864
0
  void *external_relocs = *external_relocs_addr;
2865
2866
  /* Position ourselves at the start of the section.  */
2867
0
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2868
0
    return false;
2869
2870
  /* Read the relocations.  */
2871
0
  *external_relocs_size = shdr->sh_size;
2872
0
  if (!_bfd_mmap_read_temporary (&external_relocs,
2873
0
         external_relocs_size,
2874
0
         external_relocs_addr, abfd, true))
2875
0
    return false;
2876
2877
0
  symtab_hdr = &elf_symtab_hdr (abfd);
2878
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2879
2880
0
  bed = get_elf_backend_data (abfd);
2881
2882
  /* Convert the external relocations to the internal format.  */
2883
0
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2884
0
    swap_in = bed->s->swap_reloc_in;
2885
0
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2886
0
    swap_in = bed->s->swap_reloca_in;
2887
0
  else
2888
0
    {
2889
0
      bfd_set_error (bfd_error_wrong_format);
2890
0
      return false;
2891
0
    }
2892
2893
0
  erela = (const bfd_byte *) external_relocs;
2894
  /* Setting erelaend like this and comparing with <= handles case of
2895
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2896
0
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2897
0
  irela = internal_relocs;
2898
0
  while (erela <= erelaend)
2899
0
    {
2900
0
      bfd_vma r_symndx;
2901
2902
0
      (*swap_in) (abfd, erela, irela);
2903
0
      r_symndx = ELF32_R_SYM (irela->r_info);
2904
0
      if (bed->s->arch_size == 64)
2905
0
  r_symndx >>= 24;
2906
0
      if (nsyms > 0)
2907
0
  {
2908
0
    if ((size_t) r_symndx >= nsyms)
2909
0
      {
2910
0
        _bfd_error_handler
2911
    /* xgettext:c-format */
2912
0
    (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2913
0
       " for offset %#" PRIx64 " in section `%pA'"),
2914
0
     abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2915
0
     (uint64_t) irela->r_offset, sec);
2916
0
        bfd_set_error (bfd_error_bad_value);
2917
0
        return false;
2918
0
      }
2919
0
  }
2920
0
      else if (r_symndx != STN_UNDEF)
2921
0
  {
2922
0
    _bfd_error_handler
2923
      /* xgettext:c-format */
2924
0
      (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2925
0
         " for offset %#" PRIx64 " in section `%pA'"
2926
0
         " when the object file has no symbol table"),
2927
0
       abfd, (uint64_t) r_symndx,
2928
0
       (uint64_t) irela->r_offset, sec);
2929
0
    bfd_set_error (bfd_error_bad_value);
2930
0
    return false;
2931
0
  }
2932
0
      irela += bed->s->int_rels_per_ext_rel;
2933
0
      erela += shdr->sh_entsize;
2934
0
    }
2935
2936
0
  return true;
2937
0
}
2938
2939
/* Read and swap the relocs for a section O.  They may have been
2940
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2941
   not NULL, they are used as buffers to read into.  They are known to
2942
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2943
   the return value is allocated using malloc.  If O has both a REL
2944
   relocation section and a RELA relocation section, then the REL
2945
   relocations will appear first in INTERNAL_RELOCS, followed by the
2946
   RELA relocations.  If KEEP_MEMORY is true the internal relocs are
2947
   cached, with info->cache_size updated if INFO is non-NULL.  */
2948
2949
Elf_Internal_Rela *
2950
_bfd_elf_link_info_read_relocs (bfd *abfd,
2951
        struct bfd_link_info *info,
2952
        const asection *o,
2953
        void *external_relocs,
2954
        Elf_Internal_Rela *internal_relocs,
2955
        bool keep_memory)
2956
0
{
2957
0
  void *ext_alloc1, *ext_alloc2;
2958
0
  size_t ext_mmap1, ext_mmap2;
2959
0
  Elf_Internal_Rela *int_alloc = NULL;
2960
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
2961
0
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2962
0
  Elf_Internal_Rela *internal_rela_relocs;
2963
2964
0
  if (esdo->relocs != NULL)
2965
0
    return esdo->relocs;
2966
2967
0
  if (o->reloc_count == 0)
2968
0
    return NULL;
2969
2970
0
  if (internal_relocs == NULL)
2971
0
    {
2972
0
      bfd_size_type size;
2973
2974
0
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2975
0
      if (keep_memory && info)
2976
0
  info->cache_size += size;
2977
0
      internal_relocs = int_alloc = bfd_malloc (size);
2978
0
      if (internal_relocs == NULL)
2979
0
  return NULL;
2980
0
    }
2981
2982
0
  ext_alloc1 = external_relocs;
2983
0
  ext_mmap1 = 0;
2984
0
  internal_rela_relocs = internal_relocs;
2985
0
  if (esdo->rel.hdr)
2986
0
    {
2987
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2988
0
                &ext_alloc1, &ext_mmap1,
2989
0
                internal_relocs))
2990
0
  {
2991
0
    internal_relocs = NULL;
2992
0
    goto out1;
2993
0
  }
2994
0
      if (external_relocs != NULL)
2995
0
  external_relocs = (((bfd_byte *) external_relocs)
2996
0
         + esdo->rel.hdr->sh_size);
2997
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2998
0
             * bed->s->int_rels_per_ext_rel);
2999
0
    }
3000
3001
0
  ext_alloc2 = external_relocs;
3002
0
  ext_mmap2 = 0;
3003
0
  if (esdo->rela.hdr
3004
0
      && !elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
3005
0
               &ext_alloc2, &ext_mmap2,
3006
0
               internal_rela_relocs))
3007
0
    internal_relocs = NULL;
3008
3009
  /* If the external relocs were mmap'd then we want to munmap them
3010
     regardless of whether or not an external_relocs buffer was
3011
     provided.  If they were not mmap'd then we only want to free
3012
     buffers allocated here.  */
3013
0
  if (ext_mmap2 != 0 || external_relocs == NULL)
3014
0
    _bfd_munmap_temporary (ext_alloc2, ext_mmap2);
3015
0
 out1:
3016
0
  if (ext_mmap1 != 0 || external_relocs == NULL)
3017
0
    _bfd_munmap_temporary (ext_alloc1, ext_mmap1);
3018
3019
  /* Don't free int_alloc, if we are returning it under the name of
3020
     internal_relocs.  */
3021
0
  if (internal_relocs == NULL)
3022
0
    free (int_alloc);
3023
3024
  /* Cache the results for next time, if we can.  */
3025
0
  else if (keep_memory)
3026
0
    esdo->relocs = internal_relocs;
3027
3028
0
  return internal_relocs;
3029
0
}
3030
3031
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
3032
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
3033
   struct bfd_link_info.  */
3034
3035
Elf_Internal_Rela *
3036
_bfd_elf_link_read_relocs (bfd *abfd,
3037
         const asection *o,
3038
         void *external_relocs,
3039
         Elf_Internal_Rela *internal_relocs,
3040
         bool keep_memory)
3041
0
{
3042
0
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
3043
0
           internal_relocs, keep_memory);
3044
3045
0
}
3046
3047
/* Compute the size of, and allocate space for, REL_HDR which is the
3048
   section header for a section containing relocations for O.  */
3049
3050
static bool
3051
_bfd_elf_link_size_reloc_section (bfd *obfd,
3052
          struct bfd_elf_section_reloc_data *reldata)
3053
0
{
3054
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
3055
3056
  /* That allows us to calculate the size of the section.  */
3057
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
3058
3059
  /* The contents field must last into write_object_contents, so we
3060
     allocate it with bfd_alloc rather than malloc.  Also since we
3061
     cannot be sure that the contents will actually be filled in,
3062
     we zero the allocated space.  */
3063
0
  rel_hdr->contents = bfd_zalloc (obfd, rel_hdr->sh_size);
3064
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
3065
0
    return false;
3066
3067
0
  if (reldata->hashes == NULL && reldata->count)
3068
0
    {
3069
0
      struct elf_link_hash_entry **p;
3070
3071
0
      p = bfd_zmalloc (reldata->count * sizeof (*p));
3072
0
      if (p == NULL)
3073
0
  return false;
3074
3075
0
      reldata->hashes = p;
3076
0
    }
3077
3078
0
  return true;
3079
0
}
3080
3081
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
3082
   originated from the section given by INPUT_REL_HDR) to the
3083
   OUTPUT_BFD.  */
3084
3085
bool
3086
_bfd_elf_link_output_relocs (bfd *output_bfd,
3087
           asection *input_section,
3088
           Elf_Internal_Shdr *input_rel_hdr,
3089
           Elf_Internal_Rela *internal_relocs,
3090
           struct elf_link_hash_entry **rel_hash)
3091
0
{
3092
0
  Elf_Internal_Rela *irela;
3093
0
  Elf_Internal_Rela *irelaend;
3094
0
  bfd_byte *erel;
3095
0
  struct bfd_elf_section_reloc_data *output_reldata;
3096
0
  asection *output_section;
3097
0
  elf_backend_data *obed;
3098
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
3099
0
  struct bfd_elf_section_data *esdo;
3100
3101
0
  output_section = input_section->output_section;
3102
3103
0
  obed = get_elf_backend_data (output_bfd);
3104
0
  esdo = elf_section_data (output_section);
3105
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3106
0
    {
3107
0
      output_reldata = &esdo->rel;
3108
0
      swap_out = obed->s->swap_reloc_out;
3109
0
    }
3110
0
  else if (esdo->rela.hdr
3111
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3112
0
    {
3113
0
      output_reldata = &esdo->rela;
3114
0
      swap_out = obed->s->swap_reloca_out;
3115
0
    }
3116
0
  else
3117
0
    {
3118
0
      _bfd_error_handler
3119
  /* xgettext:c-format */
3120
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
3121
0
   output_bfd, input_section->owner, input_section);
3122
0
      bfd_set_error (bfd_error_wrong_format);
3123
0
      return false;
3124
0
    }
3125
3126
0
  erel = output_reldata->hdr->contents;
3127
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
3128
0
  irela = internal_relocs;
3129
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3130
0
          * obed->s->int_rels_per_ext_rel);
3131
0
  while (irela < irelaend)
3132
0
    {
3133
0
      if (rel_hash && *rel_hash)
3134
0
  (*rel_hash)->has_reloc = 1;
3135
0
      (*swap_out) (output_bfd, irela, erel);
3136
0
      irela += obed->s->int_rels_per_ext_rel;
3137
0
      erel += input_rel_hdr->sh_entsize;
3138
0
      if (rel_hash)
3139
0
  rel_hash++;
3140
0
    }
3141
3142
  /* Bump the counter, so that we know where to add the next set of
3143
     relocations.  */
3144
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3145
3146
0
  return true;
3147
0
}
3148

3149
/* Make weak undefined symbols in PIE dynamic.  */
3150
3151
bool
3152
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3153
         struct elf_link_hash_entry *h)
3154
0
{
3155
0
  if (bfd_link_pie (info)
3156
0
      && h->dynindx == -1
3157
0
      && h->root.type == bfd_link_hash_undefweak)
3158
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
3159
3160
0
  return true;
3161
0
}
3162
3163
/* Fix up the flags for a symbol.  This handles various cases which
3164
   can only be fixed after all the input files are seen.  This is
3165
   currently called by both adjust_dynamic_symbol and
3166
   assign_sym_version, which is unnecessary but perhaps more robust in
3167
   the face of future changes.  */
3168
3169
static bool
3170
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3171
         struct elf_info_failed *eif)
3172
0
{
3173
0
  elf_backend_data *obed;
3174
3175
  /* If this symbol was mentioned in a non-ELF file, try to set
3176
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
3177
     permit a non-ELF file to correctly refer to a symbol defined in
3178
     an ELF dynamic object.  */
3179
0
  if (h->non_elf)
3180
0
    {
3181
0
      while (h->root.type == bfd_link_hash_indirect)
3182
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3183
3184
0
      if (h->root.type != bfd_link_hash_defined
3185
0
    && h->root.type != bfd_link_hash_defweak)
3186
0
  {
3187
0
    h->ref_regular = 1;
3188
0
    h->ref_regular_nonweak = 1;
3189
0
  }
3190
0
      else
3191
0
  {
3192
0
    if (h->root.u.def.section->owner != NULL
3193
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
3194
0
      == bfd_target_elf_flavour))
3195
0
      {
3196
0
        h->ref_regular = 1;
3197
0
        h->ref_regular_nonweak = 1;
3198
0
      }
3199
0
    else
3200
0
      h->def_regular = 1;
3201
0
  }
3202
3203
0
      if (h->dynindx == -1
3204
0
    && (h->def_dynamic
3205
0
        || h->ref_dynamic))
3206
0
  {
3207
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3208
0
      {
3209
0
        eif->failed = true;
3210
0
        return false;
3211
0
      }
3212
0
  }
3213
0
    }
3214
0
  else
3215
0
    {
3216
      /* Unfortunately, NON_ELF is only correct if the symbol
3217
   was first seen in a non-ELF file.  Fortunately, if the symbol
3218
   was first seen in an ELF file, we're probably OK unless the
3219
   symbol was defined in a non-ELF file.  Catch that case here.
3220
   FIXME: We're still in trouble if the symbol was first seen in
3221
   a dynamic object, and then later in a non-ELF regular object.  */
3222
0
      if ((h->root.type == bfd_link_hash_defined
3223
0
     || h->root.type == bfd_link_hash_defweak)
3224
0
    && !h->def_regular
3225
0
    && (h->root.u.def.section->owner != NULL
3226
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3227
0
     != bfd_target_elf_flavour)
3228
0
        : (bfd_is_abs_section (h->root.u.def.section)
3229
0
     && !h->def_dynamic)))
3230
0
  h->def_regular = 1;
3231
0
    }
3232
3233
  /* Backend specific symbol fixup.  */
3234
0
  obed = get_elf_backend_data (eif->info->output_bfd);
3235
0
  if (obed->elf_backend_fixup_symbol
3236
0
      && !obed->elf_backend_fixup_symbol (eif->info, h))
3237
0
    return false;
3238
3239
  /* If this is a final link, and the symbol was defined as a common
3240
     symbol in a regular object file, and there was no definition in
3241
     any dynamic object, then the linker will have allocated space for
3242
     the symbol in a common section but the DEF_REGULAR
3243
     flag will not have been set.  */
3244
0
  if (h->root.type == bfd_link_hash_defined
3245
0
      && !h->def_regular
3246
0
      && h->ref_regular
3247
0
      && !h->def_dynamic
3248
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3249
0
    h->def_regular = 1;
3250
3251
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3252
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3253
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3254
3255
  /* If a weak undefined symbol has non-default visibility, we also
3256
     hide it from the dynamic linker.  */
3257
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3258
0
     && h->root.type == bfd_link_hash_undefweak)
3259
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3260
3261
  /* A hidden versioned symbol in executable should be forced local if
3262
     it is is locally defined, not referenced by shared library and not
3263
     exported.  */
3264
0
  else if (bfd_link_executable (eif->info)
3265
0
     && h->versioned == versioned_hidden
3266
0
     && !eif->info->export_dynamic
3267
0
     && !h->dynamic
3268
0
     && !h->ref_dynamic
3269
0
     && h->def_regular)
3270
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3271
3272
  /* If -Bsymbolic was used (which means to bind references to global
3273
     symbols to the definition within the shared object), and this
3274
     symbol was defined in a regular object, then it actually doesn't
3275
     need a PLT entry.  Likewise, if the symbol has non-default
3276
     visibility.  If the symbol has hidden or internal visibility, we
3277
     will force it local.  */
3278
0
  else if (h->needs_plt
3279
0
     && bfd_link_pic (eif->info)
3280
0
     && is_elf_hash_table (eif->info->hash)
3281
0
     && (SYMBOLIC_BIND (eif->info, h)
3282
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3283
0
     && h->def_regular)
3284
0
    {
3285
0
      bool force_local;
3286
3287
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3288
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3289
0
      obed->elf_backend_hide_symbol (eif->info, h, force_local);
3290
0
    }
3291
3292
  /* If this is a weak defined symbol in a dynamic object, and we know
3293
     the real definition in the dynamic object, copy interesting flags
3294
     over to the real definition.  */
3295
0
  if (h->is_weakalias)
3296
0
    {
3297
0
      struct elf_link_hash_entry *def = weakdef (h);
3298
3299
      /* If the real definition is defined by a regular object file,
3300
   don't do anything special.  See the longer description in
3301
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3302
   bfd_link_hash_defined as it was when put on the alias list
3303
   then it must have originally been a versioned symbol (for
3304
   which a non-versioned indirect symbol is created) and later
3305
   a definition for the non-versioned symbol is found.  In that
3306
   case the indirection is flipped with the versioned symbol
3307
   becoming an indirect pointing at the non-versioned symbol.
3308
   Thus, not an alias any more.  */
3309
0
      if (def->def_regular
3310
0
    || def->root.type != bfd_link_hash_defined)
3311
0
  {
3312
0
    h = def;
3313
0
    while ((h = h->u.alias) != def)
3314
0
      h->is_weakalias = 0;
3315
0
  }
3316
0
      else
3317
0
  {
3318
0
    while (h->root.type == bfd_link_hash_indirect)
3319
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3320
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3321
0
          || h->root.type == bfd_link_hash_defweak);
3322
0
    BFD_ASSERT (def->def_dynamic);
3323
0
    obed->elf_backend_copy_indirect_symbol (eif->info, def, h);
3324
0
  }
3325
0
    }
3326
3327
0
  return true;
3328
0
}
3329
3330
/* Make the backend pick a good value for a dynamic symbol.  This is
3331
   called via elf_link_hash_traverse, and also calls itself
3332
   recursively.  */
3333
3334
static bool
3335
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3336
0
{
3337
0
  struct elf_info_failed *eif = data;
3338
0
  struct elf_link_hash_table *htab;
3339
0
  elf_backend_data *obed;
3340
3341
0
  if (! is_elf_hash_table (eif->info->hash))
3342
0
    return false;
3343
3344
0
  htab = elf_hash_table (eif->info);
3345
0
  if (h->forced_local && h->dynindx != -1)
3346
0
    htab->has_local_dynsyms = true;
3347
3348
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3349
0
  if (h->root.type == bfd_link_hash_indirect)
3350
0
    return true;
3351
3352
  /* Fix the symbol flags.  */
3353
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3354
0
    return false;
3355
3356
0
  obed = get_elf_backend_data (htab->dynobj);
3357
3358
0
  if (h->root.type == bfd_link_hash_undefweak)
3359
0
    {
3360
0
      if (eif->info->dynamic_undefined_weak == 0)
3361
0
  obed->elf_backend_hide_symbol (eif->info, h, true);
3362
0
      else if (eif->info->dynamic_undefined_weak > 0
3363
0
         && h->ref_regular
3364
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3365
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3366
0
              h->root.root.string))
3367
0
  {
3368
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3369
0
      {
3370
0
        eif->failed = true;
3371
0
        return false;
3372
0
      }
3373
0
  }
3374
0
    }
3375
3376
  /* If this symbol does not require a PLT entry, and it is not
3377
     defined by a dynamic object, or is not referenced by a regular
3378
     object, ignore it.  We do have to handle a weak defined symbol,
3379
     even if no regular object refers to it, if we decided to add it
3380
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3381
     about symbols which are defined by one dynamic object and
3382
     referenced by another one?  */
3383
0
  if (!h->needs_plt
3384
0
      && h->type != STT_GNU_IFUNC
3385
0
      && (h->def_regular
3386
0
    || !h->def_dynamic
3387
0
    || (!h->ref_regular
3388
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3389
0
    {
3390
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3391
0
      return true;
3392
0
    }
3393
3394
  /* If we've already adjusted this symbol, don't do it again.  This
3395
     can happen via a recursive call.  */
3396
0
  if (h->dynamic_adjusted)
3397
0
    return true;
3398
3399
  /* Don't look at this symbol again.  Note that we must set this
3400
     after checking the above conditions, because we may look at a
3401
     symbol once, decide not to do anything, and then get called
3402
     recursively later after REF_REGULAR is set below.  */
3403
0
  h->dynamic_adjusted = 1;
3404
3405
  /* If this is a weak definition, and we know a real definition, and
3406
     the real symbol is not itself defined by a regular object file,
3407
     then get a good value for the real definition.  We handle the
3408
     real symbol first, for the convenience of the backend routine.
3409
3410
     Note that there is a confusing case here.  If the real definition
3411
     is defined by a regular object file, we don't get the real symbol
3412
     from the dynamic object, but we do get the weak symbol.  If the
3413
     processor backend uses a COPY reloc, then if some routine in the
3414
     dynamic object changes the real symbol, we will not see that
3415
     change in the corresponding weak symbol.  This is the way other
3416
     ELF linkers work as well, and seems to be a result of the shared
3417
     library model.
3418
3419
     I will clarify this issue.  Most SVR4 shared libraries define the
3420
     variable _timezone and define timezone as a weak synonym.  The
3421
     tzset call changes _timezone.  If you write
3422
       extern int timezone;
3423
       int _timezone = 5;
3424
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3425
     you might expect that, since timezone is a synonym for _timezone,
3426
     the same number will print both times.  However, if the processor
3427
     backend uses a COPY reloc, then actually timezone will be copied
3428
     into your process image, and, since you define _timezone
3429
     yourself, _timezone will not.  Thus timezone and _timezone will
3430
     wind up at different memory locations.  The tzset call will set
3431
     _timezone, leaving timezone unchanged.  */
3432
3433
0
  if (h->is_weakalias)
3434
0
    {
3435
0
      struct elf_link_hash_entry *def = weakdef (h);
3436
3437
      /* If we get to this point, there is an implicit reference to
3438
   the alias by a regular object file via the weak symbol H.  */
3439
0
      def->ref_regular = 1;
3440
3441
      /* Ensure that the backend adjust_dynamic_symbol function sees
3442
   the strong alias before H by recursively calling ourselves.  */
3443
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3444
0
  return false;
3445
0
    }
3446
3447
  /* If a symbol has no type and no size and does not require a PLT
3448
     entry, then we are probably about to do the wrong thing here: we
3449
     are probably going to create a COPY reloc for an empty object.
3450
     This case can arise when a shared object is built with assembly
3451
     code, and the assembly code fails to set the symbol type.  */
3452
0
  if (h->size == 0
3453
0
      && h->type == STT_NOTYPE
3454
0
      && !h->needs_plt)
3455
0
    _bfd_error_handler
3456
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3457
0
       h->root.root.string);
3458
3459
0
  if (!obed->elf_backend_adjust_dynamic_symbol (eif->info, h))
3460
0
    {
3461
0
      eif->failed = true;
3462
0
      return false;
3463
0
    }
3464
3465
0
  return true;
3466
0
}
3467
3468
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3469
   DYNBSS.  */
3470
3471
bool
3472
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3473
            struct elf_link_hash_entry *h,
3474
            asection *dynbss)
3475
0
{
3476
0
  unsigned int power_of_two;
3477
0
  bfd_vma mask;
3478
0
  asection *sec = h->root.u.def.section;
3479
3480
  /* The section alignment of the definition is the maximum alignment
3481
     requirement of symbols defined in the section.  Since we don't
3482
     know the symbol alignment requirement, we start with the
3483
     maximum alignment and check low bits of the symbol address
3484
     for the minimum alignment.  */
3485
0
  power_of_two = bfd_section_alignment (sec);
3486
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3487
0
  while ((h->root.u.def.value & mask) != 0)
3488
0
    {
3489
0
       mask >>= 1;
3490
0
       --power_of_two;
3491
0
    }
3492
3493
  /* Adjust the section alignment if needed.  */
3494
0
  if (!bfd_link_align_section (dynbss, power_of_two))
3495
0
    return false;
3496
3497
  /* We make sure that the symbol will be aligned properly.  */
3498
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3499
3500
  /* Define the symbol as being at this point in DYNBSS.  */
3501
0
  h->root.u.def.section = dynbss;
3502
0
  h->root.u.def.value = dynbss->size;
3503
3504
  /* Increment the size of DYNBSS to make room for the symbol.  */
3505
0
  dynbss->size += h->size;
3506
3507
  /* No error if extern_protected_data is true.  */
3508
0
  if (h->protected_def
3509
0
      && (!info->extern_protected_data
3510
0
    || (info->extern_protected_data < 0
3511
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3512
0
    info->callbacks->einfo
3513
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3514
0
       h->root.root.string);
3515
3516
0
  return true;
3517
0
}
3518
3519
/* Adjust all external symbols pointing into SEC_MERGE sections
3520
   to reflect the object merging within the sections.  */
3521
3522
static bool
3523
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3524
0
{
3525
0
  asection *sec;
3526
3527
0
  if ((h->root.type == bfd_link_hash_defined
3528
0
       || h->root.type == bfd_link_hash_defweak)
3529
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3530
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3531
0
    {
3532
0
      bfd *output_bfd = data;
3533
3534
0
      h->root.u.def.value =
3535
0
  _bfd_merged_section_offset (output_bfd,
3536
0
            &h->root.u.def.section,
3537
0
            h->root.u.def.value);
3538
0
    }
3539
3540
0
  return true;
3541
0
}
3542
3543
/* Returns false if the symbol referred to by H should be considered
3544
   to resolve local to the current module, and true if it should be
3545
   considered to bind dynamically.  */
3546
3547
bool
3548
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3549
         struct bfd_link_info *info,
3550
         bool not_local_protected)
3551
0
{
3552
0
  bool binding_stays_local_p;
3553
0
  elf_backend_data *obed;
3554
0
  struct elf_link_hash_table *hash_table;
3555
3556
0
  if (h == NULL)
3557
0
    return false;
3558
3559
0
  while (h->root.type == bfd_link_hash_indirect
3560
0
   || h->root.type == bfd_link_hash_warning)
3561
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3562
3563
  /* If it was forced local, then clearly it's not dynamic.  */
3564
0
  if (h->dynindx == -1)
3565
0
    return false;
3566
0
  if (h->forced_local)
3567
0
    return false;
3568
3569
  /* Identify the cases where name binding rules say that a
3570
     visible symbol resolves locally.  */
3571
0
  binding_stays_local_p = (bfd_link_executable (info)
3572
0
         || SYMBOLIC_BIND (info, h));
3573
3574
0
  switch (ELF_ST_VISIBILITY (h->other))
3575
0
    {
3576
0
    case STV_INTERNAL:
3577
0
    case STV_HIDDEN:
3578
0
      return false;
3579
3580
0
    case STV_PROTECTED:
3581
0
      hash_table = elf_hash_table (info);
3582
0
      if (!is_elf_hash_table (&hash_table->root))
3583
0
  return false;
3584
3585
0
      obed = get_elf_backend_data (hash_table->dynobj);
3586
3587
      /* Proper resolution for function pointer equality may require
3588
   that these symbols perhaps be resolved dynamically, even though
3589
   we should be resolving them to the current module.  */
3590
0
      if (!not_local_protected || !obed->is_function_type (h->type))
3591
0
  binding_stays_local_p = true;
3592
0
      break;
3593
3594
0
    default:
3595
0
      break;
3596
0
    }
3597
3598
  /* If it isn't defined locally, then clearly it's dynamic.  */
3599
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3600
0
    return true;
3601
3602
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3603
     us that it remains local.  */
3604
0
  return !binding_stays_local_p;
3605
0
}
3606
3607
/* Return true if the symbol referred to by H should be considered
3608
   to resolve local to the current module, and false otherwise.  Differs
3609
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3610
   undefined symbols.  The two functions are virtually identical except
3611
   for the place where dynindx == -1 is tested.  If that test is true,
3612
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3613
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3614
   defined symbols.
3615
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3616
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3617
   treatment of undefined weak symbols.  For those that do not make
3618
   undefined weak symbols dynamic, both functions may return false.  */
3619
3620
bool
3621
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3622
            struct bfd_link_info *info,
3623
            bool local_protected)
3624
0
{
3625
0
  elf_backend_data *obed;
3626
0
  struct elf_link_hash_table *hash_table;
3627
3628
  /* If it's a local sym, of course we resolve locally.  */
3629
0
  if (h == NULL)
3630
0
    return true;
3631
3632
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3633
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3634
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3635
0
    return true;
3636
3637
  /* Forced local symbols resolve locally.  */
3638
0
  if (h->forced_local)
3639
0
    return true;
3640
3641
  /* Common symbols that become definitions don't get the DEF_REGULAR
3642
     flag set, so test it first, and don't bail out.  */
3643
0
  if (ELF_COMMON_DEF_P (h))
3644
0
    /* Do nothing.  */;
3645
  /* If we don't have a definition in a regular file, then we can't
3646
     resolve locally.  The sym is either undefined or dynamic.  */
3647
0
  else if (!h->def_regular)
3648
0
    return false;
3649
3650
  /* Non-dynamic symbols resolve locally.  */
3651
0
  if (h->dynindx == -1)
3652
0
    return true;
3653
3654
  /* At this point, we know the symbol is defined and dynamic.  In an
3655
     executable it must resolve locally, likewise when building symbolic
3656
     shared libraries.  */
3657
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3658
0
    return true;
3659
3660
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3661
     with default visibility might not resolve locally.  */
3662
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3663
0
    return false;
3664
3665
0
  hash_table = elf_hash_table (info);
3666
0
  if (!is_elf_hash_table (&hash_table->root))
3667
0
    return true;
3668
3669
  /* STV_PROTECTED symbols with indirect external access are local. */
3670
0
  if (info->indirect_extern_access > 0)
3671
0
    return true;
3672
3673
0
  obed = get_elf_backend_data (hash_table->dynobj);
3674
3675
  /* If extern_protected_data is false, STV_PROTECTED non-function
3676
     symbols are local.  */
3677
0
  if ((!info->extern_protected_data
3678
0
       || (info->extern_protected_data < 0
3679
0
     && !obed->extern_protected_data))
3680
0
      && !obed->is_function_type (h->type))
3681
0
    return true;
3682
3683
  /* Function pointer equality tests may require that STV_PROTECTED
3684
     symbols be treated as dynamic symbols.  If the address of a
3685
     function not defined in an executable is set to that function's
3686
     plt entry in the executable, then the address of the function in
3687
     a shared library must also be the plt entry in the executable.  */
3688
0
  return local_protected;
3689
0
}
3690
3691
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3692
   aligned.  Returns the first TLS output section.  */
3693
3694
struct bfd_section *
3695
bfd_elf_tls_setup (struct bfd_link_info *info)
3696
0
{
3697
0
  struct bfd_section *sec, *tls;
3698
0
  unsigned int align = 0;
3699
3700
0
  for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
3701
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3702
0
      break;
3703
0
  tls = sec;
3704
3705
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3706
0
    if (sec->alignment_power > align)
3707
0
      align = sec->alignment_power;
3708
3709
0
  elf_hash_table (info)->tls_sec = tls;
3710
3711
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3712
     alignment, so that the tls segment starts aligned.  */
3713
0
  if (tls != NULL)
3714
0
    (void) bfd_link_align_section (tls, align);
3715
3716
0
  return tls;
3717
0
}
3718
3719
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3720
static bool
3721
is_global_data_symbol_definition (bfd *abfd,
3722
          Elf_Internal_Sym *sym)
3723
0
{
3724
0
  elf_backend_data *bed;
3725
3726
  /* Local symbols do not count, but target specific ones might.  */
3727
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3728
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3729
0
    return false;
3730
3731
0
  bed = get_elf_backend_data (abfd);
3732
  /* Function symbols do not count.  */
3733
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3734
0
    return false;
3735
3736
  /* If the section is undefined, then so is the symbol.  */
3737
0
  if (sym->st_shndx == SHN_UNDEF)
3738
0
    return false;
3739
3740
  /* If the symbol is defined in the common section, then
3741
     it is a common definition and so does not count.  */
3742
0
  if (bed->common_definition (sym))
3743
0
    return false;
3744
3745
  /* If the symbol is in a target specific section then we
3746
     must rely upon the backend to tell us what it is.  */
3747
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3748
    /* FIXME - this function is not coded yet:
3749
3750
       return _bfd_is_global_symbol_definition (abfd, sym);
3751
3752
       Instead for now assume that the definition is not global,
3753
       Even if this is wrong, at least the linker will behave
3754
       in the same way that it used to do.  */
3755
0
    return false;
3756
3757
0
  return true;
3758
0
}
3759
3760
/* Search the symbol table of the archive element of the archive ABFD
3761
   whose archive map contains a mention of SYMDEF, and determine if
3762
   the symbol is defined in this element.  */
3763
static bool
3764
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3765
0
{
3766
0
  Elf_Internal_Shdr * hdr;
3767
0
  size_t symcount;
3768
0
  size_t extsymcount;
3769
0
  size_t extsymoff;
3770
0
  Elf_Internal_Sym *isymbuf;
3771
0
  Elf_Internal_Sym *isym;
3772
0
  Elf_Internal_Sym *isymend;
3773
0
  bool result;
3774
3775
0
  abfd = _bfd_get_elt_from_symdef (abfd, symdef, NULL);
3776
0
  if (abfd == NULL)
3777
0
    return false;
3778
3779
0
  if (! bfd_check_format (abfd, bfd_object))
3780
0
    return false;
3781
3782
  /* Select the appropriate symbol table.  If we don't know if the
3783
     object file is an IR object, give linker LTO plugin a chance to
3784
     get the correct symbol table.  */
3785
0
  if (abfd->plugin_format == bfd_plugin_yes
3786
0
      || abfd->plugin_format == bfd_plugin_yes_unused
3787
0
      || (abfd->plugin_format == bfd_plugin_unknown
3788
0
    && bfd_link_plugin_object_p (abfd)))
3789
0
    {
3790
      /* Use the IR symbol table if the object has been claimed by
3791
   plugin.  */
3792
0
      abfd = abfd->plugin_dummy_bfd;
3793
0
      hdr = &elf_symtab_hdr (abfd);
3794
0
    }
3795
0
  else
3796
0
    {
3797
0
      if (elf_use_dt_symtab_p (abfd))
3798
0
  {
3799
0
    bfd_set_error (bfd_error_wrong_format);
3800
0
    return false;
3801
0
  }
3802
3803
0
      if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3804
0
  hdr = &elf_symtab_hdr (abfd);
3805
0
      else
3806
0
  hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3807
0
    }
3808
3809
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3810
3811
  /* The sh_info field of the symtab header tells us where the
3812
     external symbols start.  We don't care about the local symbols.  */
3813
0
  if (elf_bad_symtab (abfd))
3814
0
    {
3815
0
      extsymcount = symcount;
3816
0
      extsymoff = 0;
3817
0
    }
3818
0
  else
3819
0
    {
3820
0
      extsymcount = symcount - hdr->sh_info;
3821
0
      extsymoff = hdr->sh_info;
3822
0
    }
3823
3824
0
  if (extsymcount == 0)
3825
0
    return false;
3826
3827
  /* Read in the symbol table.  */
3828
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3829
0
          NULL, NULL, NULL);
3830
0
  if (isymbuf == NULL)
3831
0
    return false;
3832
3833
  /* Scan the symbol table looking for SYMDEF.  */
3834
0
  result = false;
3835
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3836
0
    {
3837
0
      const char *name;
3838
3839
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3840
0
                isym->st_name);
3841
0
      if (name == NULL)
3842
0
  break;
3843
3844
0
      if (strcmp (name, symdef->name) == 0)
3845
0
  {
3846
0
    result = is_global_data_symbol_definition (abfd, isym);
3847
0
    break;
3848
0
  }
3849
0
    }
3850
3851
0
  free (isymbuf);
3852
3853
0
  return result;
3854
0
}
3855

3856
/* Add an entry to the .dynamic table.  */
3857
3858
bool
3859
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3860
          bfd_vma tag,
3861
          bfd_vma val)
3862
0
{
3863
0
  struct elf_link_hash_table *hash_table;
3864
0
  elf_backend_data *obed;
3865
0
  asection *s;
3866
0
  bfd_size_type newsize;
3867
0
  bfd_byte *newcontents;
3868
0
  Elf_Internal_Dyn dyn;
3869
3870
0
  hash_table = elf_hash_table (info);
3871
0
  if (! is_elf_hash_table (&hash_table->root))
3872
0
    return false;
3873
3874
0
  if (tag == DT_RELA || tag == DT_REL)
3875
0
    hash_table->dynamic_relocs = true;
3876
3877
0
  obed = get_elf_backend_data (hash_table->dynobj);
3878
0
  s = hash_table->dynamic;
3879
0
  BFD_ASSERT (s != NULL);
3880
3881
0
  newsize = s->size + obed->s->sizeof_dyn;
3882
0
  newcontents = bfd_realloc (s->contents, newsize);
3883
0
  if (newcontents == NULL)
3884
0
    return false;
3885
3886
0
  dyn.d_tag = tag;
3887
0
  dyn.d_un.d_val = val;
3888
0
  obed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3889
3890
0
  s->size = newsize;
3891
0
  s->contents = newcontents;
3892
3893
0
  return true;
3894
0
}
3895
3896
/* Strip zero-sized dynamic sections.  */
3897
3898
bool
3899
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3900
0
{
3901
0
  struct elf_link_hash_table *hash_table;
3902
0
  elf_backend_data *obed;
3903
0
  asection *s, *sdynamic, **pp;
3904
0
  asection *rela_dyn, *rel_dyn;
3905
0
  Elf_Internal_Dyn dyn;
3906
0
  bfd_byte *extdyn, *next;
3907
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3908
0
  bool strip_zero_sized;
3909
0
  bool strip_zero_sized_plt;
3910
3911
0
  if (bfd_link_relocatable (info))
3912
0
    return true;
3913
3914
0
  hash_table = elf_hash_table (info);
3915
0
  if (!is_elf_hash_table (&hash_table->root))
3916
0
    return false;
3917
3918
0
  if (!hash_table->dynobj)
3919
0
    return true;
3920
3921
0
  sdynamic= hash_table->dynamic;
3922
0
  if (!sdynamic)
3923
0
    return true;
3924
3925
0
  obed = get_elf_backend_data (hash_table->dynobj);
3926
0
  swap_dyn_in = obed->s->swap_dyn_in;
3927
3928
0
  strip_zero_sized = false;
3929
0
  strip_zero_sized_plt = false;
3930
3931
  /* Strip zero-sized dynamic sections.  */
3932
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3933
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3934
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3935
0
    if (s->size == 0
3936
0
  && (s == rela_dyn
3937
0
      || s == rel_dyn
3938
0
      || s == hash_table->srelplt->output_section
3939
0
      || s == hash_table->splt->output_section))
3940
0
      {
3941
0
  *pp = s->next;
3942
0
  info->output_bfd->section_count--;
3943
0
  strip_zero_sized = true;
3944
0
  if (s == rela_dyn)
3945
0
    s = rela_dyn;
3946
0
  if (s == rel_dyn)
3947
0
    s = rel_dyn;
3948
0
  else if (s == hash_table->splt->output_section)
3949
0
    {
3950
0
      s = hash_table->splt;
3951
0
      strip_zero_sized_plt = true;
3952
0
    }
3953
0
  else
3954
0
    s = hash_table->srelplt;
3955
0
  s->flags |= SEC_EXCLUDE;
3956
0
  s->output_section = bfd_abs_section_ptr;
3957
0
      }
3958
0
    else
3959
0
      pp = &s->next;
3960
3961
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3962
0
    for (extdyn = sdynamic->contents;
3963
0
   extdyn < sdynamic->contents + sdynamic->size;
3964
0
   extdyn = next)
3965
0
      {
3966
0
  next = extdyn + obed->s->sizeof_dyn;
3967
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3968
0
  switch (dyn.d_tag)
3969
0
    {
3970
0
    default:
3971
0
      break;
3972
0
    case DT_JMPREL:
3973
0
    case DT_PLTRELSZ:
3974
0
    case DT_PLTREL:
3975
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3976
         the procedure linkage table (the .plt section) has been
3977
         removed.  */
3978
0
      memmove (extdyn, next,
3979
0
         sdynamic->size - (next - sdynamic->contents));
3980
0
      next = extdyn;
3981
0
    }
3982
0
      }
3983
3984
0
  if (strip_zero_sized)
3985
0
    {
3986
      /* Regenerate program headers.  */
3987
0
      elf_seg_map (info->output_bfd) = NULL;
3988
0
      return bfd_elf_map_sections_to_segments (info->output_bfd, info, NULL);
3989
0
    }
3990
3991
0
  return true;
3992
0
}
3993
3994
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3995
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3996
3997
int
3998
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3999
0
{
4000
0
  struct elf_strtab_hash *dynstr;
4001
0
  size_t strindex;
4002
0
  const char *soname;
4003
4004
0
  dynstr = _bfd_elf_link_dynstr (info);
4005
0
  if (dynstr == NULL)
4006
0
    return -1;
4007
4008
0
  soname = elf_dt_name (abfd);
4009
0
  strindex = _bfd_elf_strtab_add (dynstr, soname, false);
4010
0
  if (strindex == (size_t) -1)
4011
0
    return -1;
4012
4013
0
  if (!bfd_elf_link_create_dynamic_sections (info))
4014
0
    return -1;
4015
4016
0
  if (_bfd_elf_strtab_refcount (dynstr, strindex) != 1)
4017
0
    {
4018
0
      struct elf_link_hash_table *hash_table = elf_hash_table (info);
4019
0
      elf_backend_data *obed = get_elf_backend_data (hash_table->dynobj);
4020
0
      asection *sdyn = hash_table->dynamic;
4021
0
      bfd_byte *extdyn;
4022
4023
0
      if (sdyn != NULL && sdyn->size != 0)
4024
0
  for (extdyn = sdyn->contents;
4025
0
       extdyn < sdyn->contents + sdyn->size;
4026
0
       extdyn += obed->s->sizeof_dyn)
4027
0
    {
4028
0
      Elf_Internal_Dyn dyn;
4029
4030
0
      obed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
4031
0
      if (dyn.d_tag == DT_NEEDED
4032
0
    && dyn.d_un.d_val == strindex)
4033
0
        {
4034
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
4035
0
    return 1;
4036
0
        }
4037
0
    }
4038
0
    }
4039
4040
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
4041
0
    return -1;
4042
4043
0
  return 0;
4044
0
}
4045
4046
/* Return true if SONAME is on the needed list between NEEDED and STOP
4047
   (or the end of list if STOP is NULL), and needed by a library that
4048
   will be loaded.  */
4049
4050
static bool
4051
on_needed_list (const char *soname,
4052
    struct bfd_link_needed_list *needed,
4053
    struct bfd_link_needed_list *stop)
4054
0
{
4055
0
  struct bfd_link_needed_list *look;
4056
0
  for (look = needed; look != stop; look = look->next)
4057
0
    if (strcmp (soname, look->name) == 0
4058
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
4059
      /* If needed by a library that itself is not directly
4060
         needed, recursively check whether that library is
4061
         indirectly needed.  Since we add DT_NEEDED entries to
4062
         the end of the list, library dependencies appear after
4063
         the library.  Therefore search prior to the current
4064
         LOOK, preventing possible infinite recursion.  */
4065
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
4066
0
      return true;
4067
4068
0
  return false;
4069
0
}
4070
4071
/* Sort symbol by value, section, size, and type.  */
4072
static int
4073
elf_sort_symbol (const void *arg1, const void *arg2)
4074
0
{
4075
0
  const struct elf_link_hash_entry *h1;
4076
0
  const struct elf_link_hash_entry *h2;
4077
0
  bfd_signed_vma vdiff;
4078
0
  int sdiff;
4079
0
  const char *n1;
4080
0
  const char *n2;
4081
4082
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
4083
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
4084
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
4085
0
  if (vdiff != 0)
4086
0
    return vdiff > 0 ? 1 : -1;
4087
4088
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
4089
0
  if (sdiff != 0)
4090
0
    return sdiff;
4091
4092
  /* Sort so that sized symbols are selected over zero size symbols.  */
4093
0
  vdiff = h1->size - h2->size;
4094
0
  if (vdiff != 0)
4095
0
    return vdiff > 0 ? 1 : -1;
4096
4097
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
4098
0
  if (h1->type != h2->type)
4099
0
    return h1->type - h2->type;
4100
4101
  /* If symbols are properly sized and typed, and multiple strong
4102
     aliases are not defined in a shared library by the user we
4103
     shouldn't get here.  Unfortunately linker script symbols like
4104
     __bss_start sometimes match a user symbol defined at the start of
4105
     .bss without proper size and type.  We'd like to preference the
4106
     user symbol over reserved system symbols.  Sort on leading
4107
     underscores.  */
4108
0
  n1 = h1->root.root.string;
4109
0
  n2 = h2->root.root.string;
4110
0
  while (*n1 == *n2)
4111
0
    {
4112
0
      if (*n1 == 0)
4113
0
  break;
4114
0
      ++n1;
4115
0
      ++n2;
4116
0
    }
4117
0
  if (*n1 == '_')
4118
0
    return -1;
4119
0
  if (*n2 == '_')
4120
0
    return 1;
4121
4122
  /* Final sort on name selects user symbols like '_u' over reserved
4123
     system symbols like '_Z' and also will avoid qsort instability.  */
4124
0
  return *n1 - *n2;
4125
0
}
4126
4127
/* This function is used to adjust offsets into .dynstr for
4128
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
4129
4130
static bool
4131
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4132
0
{
4133
0
  struct elf_strtab_hash *dynstr = data;
4134
4135
0
  if (h->dynindx != -1)
4136
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4137
0
  return true;
4138
0
}
4139
4140
/* Assign string offsets in .dynstr, update all structures referencing
4141
   them.  */
4142
4143
static bool
4144
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4145
0
{
4146
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
4147
0
  struct elf_link_local_dynamic_entry *entry;
4148
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
4149
0
  bfd *dynobj = hash_table->dynobj;
4150
0
  asection *sdyn;
4151
0
  bfd_size_type size;
4152
0
  elf_backend_data *obed;
4153
0
  bfd_byte *extdyn;
4154
4155
0
  _bfd_elf_strtab_finalize (dynstr);
4156
0
  size = _bfd_elf_strtab_size (dynstr);
4157
4158
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
4159
4160
0
  if (info->callbacks->examine_strtab)
4161
0
    info->callbacks->examine_strtab (dynstr);
4162
4163
0
  obed = get_elf_backend_data (dynobj);
4164
0
  sdyn = hash_table->dynamic;
4165
0
  BFD_ASSERT (sdyn != NULL);
4166
4167
  /* Update all .dynamic entries referencing .dynstr strings.  */
4168
0
  for (extdyn = sdyn->contents;
4169
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4170
0
       extdyn += obed->s->sizeof_dyn)
4171
0
    {
4172
0
      Elf_Internal_Dyn dyn;
4173
4174
0
      obed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4175
0
      switch (dyn.d_tag)
4176
0
  {
4177
0
  case DT_STRSZ:
4178
0
    dyn.d_un.d_val = size;
4179
0
    break;
4180
0
  case DT_NEEDED:
4181
0
  case DT_SONAME:
4182
0
  case DT_RPATH:
4183
0
  case DT_RUNPATH:
4184
0
  case DT_FILTER:
4185
0
  case DT_AUXILIARY:
4186
0
  case DT_AUDIT:
4187
0
  case DT_DEPAUDIT:
4188
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4189
0
    break;
4190
0
  default:
4191
0
    continue;
4192
0
  }
4193
0
      obed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4194
0
    }
4195
4196
  /* Now update local dynamic symbols.  */
4197
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4198
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4199
0
              entry->isym.st_name);
4200
4201
  /* And the rest of dynamic symbols.  */
4202
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4203
4204
  /* Adjust version definitions.  */
4205
0
  if (elf_tdata (output_bfd)->cverdefs)
4206
0
    {
4207
0
      asection *s;
4208
0
      bfd_byte *p;
4209
0
      size_t i;
4210
0
      Elf_Internal_Verdef def;
4211
0
      Elf_Internal_Verdaux defaux;
4212
4213
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4214
0
      p = s->contents;
4215
0
      do
4216
0
  {
4217
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4218
0
           &def);
4219
0
    p += sizeof (Elf_External_Verdef);
4220
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4221
0
      continue;
4222
0
    for (i = 0; i < def.vd_cnt; ++i)
4223
0
      {
4224
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4225
0
          (Elf_External_Verdaux *) p, &defaux);
4226
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4227
0
              defaux.vda_name);
4228
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4229
0
           &defaux, (Elf_External_Verdaux *) p);
4230
0
        p += sizeof (Elf_External_Verdaux);
4231
0
      }
4232
0
  }
4233
0
      while (def.vd_next);
4234
0
    }
4235
4236
  /* Adjust version references.  */
4237
0
  if (elf_tdata (output_bfd)->verref)
4238
0
    {
4239
0
      asection *s;
4240
0
      bfd_byte *p;
4241
0
      size_t i;
4242
0
      Elf_Internal_Verneed need;
4243
0
      Elf_Internal_Vernaux needaux;
4244
4245
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4246
0
      p = s->contents;
4247
0
      do
4248
0
  {
4249
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4250
0
            &need);
4251
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4252
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4253
0
             (Elf_External_Verneed *) p);
4254
0
    p += sizeof (Elf_External_Verneed);
4255
0
    for (i = 0; i < need.vn_cnt; ++i)
4256
0
      {
4257
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4258
0
          (Elf_External_Vernaux *) p, &needaux);
4259
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4260
0
               needaux.vna_name);
4261
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4262
0
           &needaux,
4263
0
           (Elf_External_Vernaux *) p);
4264
0
        p += sizeof (Elf_External_Vernaux);
4265
0
      }
4266
0
  }
4267
0
      while (need.vn_next);
4268
0
    }
4269
4270
0
  return true;
4271
0
}
4272

4273
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4274
   The default is to only match when the INPUT and OUTPUT are exactly
4275
   the same target.  */
4276
4277
bool
4278
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4279
            const bfd_target *output)
4280
0
{
4281
0
  return input == output;
4282
0
}
4283
4284
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4285
   This version is used when different targets for the same architecture
4286
   are virtually identical.  */
4287
4288
bool
4289
_bfd_elf_relocs_compatible (const bfd_target *input,
4290
          const bfd_target *output)
4291
0
{
4292
0
  elf_backend_data *obed, *ibed;
4293
4294
0
  if (input == output)
4295
0
    return true;
4296
4297
0
  ibed = xvec_get_elf_backend_data (input);
4298
0
  obed = xvec_get_elf_backend_data (output);
4299
4300
0
  if (ibed->arch != obed->arch)
4301
0
    return false;
4302
4303
  /* If both backends are using this function, deem them compatible.  */
4304
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4305
0
}
4306
4307
/* Make a special call to the linker "notice" function to tell it that
4308
   we are about to handle an as-needed lib, or have finished
4309
   processing the lib.  */
4310
4311
bool
4312
_bfd_elf_notice_as_needed (bfd *ibfd,
4313
         struct bfd_link_info *info,
4314
         enum notice_asneeded_action act)
4315
0
{
4316
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4317
0
}
4318
4319
/* Call ACTION on each relocation in an ELF object file.  */
4320
4321
bool
4322
_bfd_elf_link_iterate_on_relocs
4323
  (bfd *abfd, struct bfd_link_info *info,
4324
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4325
       const Elf_Internal_Rela *))
4326
0
{
4327
  /* If this object is the same format as the output object, and it is
4328
     not a shared library, then let the backend look through the
4329
     relocs.
4330
4331
     This is required to build global offset table entries and to
4332
     arrange for dynamic relocs.  It is not required for the
4333
     particular common case of linking non PIC code, even when linking
4334
     against shared libraries, but unfortunately there is no way of
4335
     knowing whether an object file has been compiled PIC or not.
4336
     Looking through the relocs is not particularly time consuming.
4337
     The problem is that we must either (1) keep the relocs in memory,
4338
     which causes the linker to require additional runtime memory or
4339
     (2) read the relocs twice from the input file, which wastes time.
4340
     This would be a good case for using mmap.
4341
4342
     I have no idea how to handle linking PIC code into a file of a
4343
     different format.  It probably can't be done.  */
4344
0
  if ((abfd->flags & DYNAMIC) == 0
4345
0
      && compatible_format (info, abfd))
4346
0
    {
4347
0
      for (asection *o = abfd->sections; o != NULL; o = o->next)
4348
0
  {
4349
0
    Elf_Internal_Rela *internal_relocs;
4350
0
    bool ok;
4351
4352
    /* Don't check relocations in excluded sections.  Don't do
4353
       anything special with non-loaded, non-alloced sections.
4354
       In particular, any relocs in such sections should not
4355
       affect GOT and PLT reference counting (ie.  we don't
4356
       allow them to create GOT or PLT entries), there's no
4357
       possibility or desire to optimize TLS relocs, and
4358
       there's not much point in propagating relocs to shared
4359
       libs that the dynamic linker won't relocate.  */
4360
0
    if ((o->flags & SEC_ALLOC) == 0
4361
0
        || (o->flags & SEC_RELOC) == 0
4362
0
        || (o->flags & SEC_EXCLUDE) != 0
4363
0
        || o->reloc_count == 0
4364
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4365
0
      && (o->flags & SEC_DEBUGGING) != 0)
4366
0
        || bfd_is_abs_section (o->output_section))
4367
0
      continue;
4368
4369
0
    internal_relocs = _bfd_elf_link_info_read_relocs
4370
0
      (abfd, info, o, NULL, NULL,
4371
0
       _bfd_elf_link_keep_memory (info));
4372
0
    if (internal_relocs == NULL)
4373
0
      return false;
4374
4375
0
    ok = action (abfd, info, o, internal_relocs);
4376
4377
0
    if (elf_section_data (o)->relocs != internal_relocs)
4378
0
      free (internal_relocs);
4379
4380
0
    if (! ok)
4381
0
      return false;
4382
0
  }
4383
0
    }
4384
4385
0
  return true;
4386
0
}
4387
4388
/* Check relocations in an ELF object file.  This is called after
4389
   all input files have been opened.  */
4390
4391
bool
4392
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4393
0
{
4394
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4395
0
  if (bed->check_relocs != NULL)
4396
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4397
0
              bed->check_relocs);
4398
0
  return true;
4399
0
}
4400
4401
/* An entry in the first definition hash table.  */
4402
4403
struct elf_link_first_hash_entry
4404
{
4405
  struct bfd_hash_entry root;
4406
  /* The object of the first definition.  */
4407
  bfd *abfd;
4408
};
4409
4410
/* The function to create a new entry in the first definition hash
4411
   table.  */
4412
4413
static struct bfd_hash_entry *
4414
elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4415
           struct bfd_hash_table *table,
4416
           const char *string)
4417
0
{
4418
0
  struct elf_link_first_hash_entry *ret =
4419
0
    (struct elf_link_first_hash_entry *) entry;
4420
4421
  /* Allocate the structure if it has not already been allocated by a
4422
     subclass.  */
4423
0
  if (ret == NULL)
4424
0
    ret = bfd_hash_allocate (table, sizeof (struct elf_link_first_hash_entry));
4425
0
  if (ret == NULL)
4426
0
    return NULL;
4427
4428
  /* Call the allocation method of the superclass.  */
4429
0
  ret = ((struct elf_link_first_hash_entry *)
4430
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4431
0
         string));
4432
0
  if (ret != NULL)
4433
0
    ret->abfd = NULL;
4434
4435
0
  return (struct bfd_hash_entry *) ret;
4436
0
}
4437
4438
/* Add the symbol NAME from ABFD to first hash.  */
4439
4440
static void
4441
elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4442
          const char *name, bool copy)
4443
0
{
4444
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4445
  /* Skip if there is no first hash.  */
4446
0
  if (htab->first_hash == NULL)
4447
0
    return;
4448
4449
0
  struct elf_link_first_hash_entry *e
4450
0
    = ((struct elf_link_first_hash_entry *)
4451
0
       bfd_hash_lookup (htab->first_hash, name, true, copy));
4452
0
  if (e == NULL)
4453
0
    info->callbacks->fatal
4454
0
      (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
4455
4456
0
  if (e->abfd == NULL)
4457
    /* Store ABFD in abfd.  */
4458
0
    e->abfd = abfd;
4459
0
}
4460
4461
/* Add symbols from an ELF object file to the linker hash table.  */
4462
4463
static bool
4464
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4465
0
{
4466
0
  Elf_Internal_Ehdr *ehdr;
4467
0
  Elf_Internal_Shdr *hdr;
4468
0
  size_t symcount;
4469
0
  size_t extsymcount;
4470
0
  size_t extsymoff;
4471
0
  struct elf_link_hash_entry **sym_hash;
4472
0
  bool dynamic;
4473
0
  Elf_External_Versym *extversym = NULL;
4474
0
  Elf_External_Versym *extversym_end = NULL;
4475
0
  Elf_External_Versym *ever;
4476
0
  struct elf_link_hash_entry *weaks;
4477
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4478
0
  size_t nondeflt_vers_cnt = 0;
4479
0
  Elf_Internal_Sym *isymbuf = NULL;
4480
0
  Elf_Internal_Sym *isym;
4481
0
  Elf_Internal_Sym *isymend;
4482
0
  elf_backend_data *bed, *obed;
4483
0
  bool add_needed;
4484
0
  struct elf_link_hash_table *htab;
4485
0
  void *alloc_mark = NULL;
4486
0
  struct bfd_hash_entry **old_table = NULL;
4487
0
  unsigned int old_size = 0;
4488
0
  unsigned int old_count = 0;
4489
0
  void *old_tab = NULL;
4490
0
  void *old_ent;
4491
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4492
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4493
0
  void *old_strtab = NULL;
4494
0
  size_t tabsize = 0;
4495
0
  asection *s;
4496
0
  bool just_syms;
4497
4498
0
  htab = elf_hash_table (info);
4499
0
  bed = get_elf_backend_data (abfd);
4500
0
  obed = get_elf_backend_data (info->output_bfd);
4501
4502
0
  if (elf_use_dt_symtab_p (abfd))
4503
0
    {
4504
0
      bfd_set_error (bfd_error_wrong_format);
4505
0
      return false;
4506
0
    }
4507
4508
0
  if ((abfd->flags & DYNAMIC) == 0)
4509
0
    {
4510
0
      dynamic = false;
4511
0
      if ((abfd->flags & BFD_PLUGIN) != 0
4512
0
    && is_elf_hash_table (&htab->root)
4513
0
    && htab->first_hash == NULL)
4514
0
  {
4515
    /* Initialize first_hash for an IR input.  */
4516
0
    htab->first_hash = bfd_malloc (sizeof (struct bfd_hash_table));
4517
0
    if (htab->first_hash == NULL
4518
0
        || !bfd_hash_table_init
4519
0
       (htab->first_hash, elf_link_first_hash_newfunc,
4520
0
        sizeof (struct elf_link_first_hash_entry)))
4521
0
      info->callbacks->fatal
4522
0
        (_("%P: first_hash failed to create: %E\n"));
4523
0
  }
4524
0
    }
4525
0
  else
4526
0
    {
4527
0
      dynamic = true;
4528
4529
      /* You can't use -r against a dynamic object.  Also, there's no
4530
   hope of using a dynamic object which does not exactly match
4531
   the format of the output file.  */
4532
0
      if (bfd_link_relocatable (info)
4533
0
    || !is_elf_hash_table (&htab->root)
4534
0
    || info->output_bfd->xvec != abfd->xvec)
4535
0
  {
4536
0
    if (bfd_link_relocatable (info))
4537
0
      bfd_set_error (bfd_error_invalid_operation);
4538
0
    else
4539
0
      bfd_set_error (bfd_error_wrong_format);
4540
0
    goto error_return;
4541
0
  }
4542
0
    }
4543
4544
0
  ehdr = elf_elfheader (abfd);
4545
0
  if (info->warn_alternate_em
4546
0
      && bed->elf_machine_code != ehdr->e_machine
4547
0
      && ((bed->elf_machine_alt1 != 0
4548
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4549
0
    || (bed->elf_machine_alt2 != 0
4550
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4551
0
    _bfd_error_handler
4552
      /* xgettext:c-format */
4553
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4554
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4555
4556
  /* As a GNU extension, any input sections which are named
4557
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4558
     symbol.  This differs from .gnu.warning sections, which generate
4559
     warnings when they are included in an output file.  */
4560
  /* PR 12761: Also generate this warning when building shared libraries.  */
4561
0
  for (s = abfd->sections; s != NULL; s = s->next)
4562
0
    {
4563
0
      const char *name;
4564
4565
0
      name = bfd_section_name (s);
4566
0
      if (startswith (name, ".gnu.warning."))
4567
0
  {
4568
0
    char *msg;
4569
0
    bfd_size_type sz;
4570
4571
0
    name += sizeof ".gnu.warning." - 1;
4572
4573
    /* If this is a shared object, then look up the symbol
4574
       in the hash table.  If it is there, and it is already
4575
       been defined, then we will not be using the entry
4576
       from this shared object, so we don't need to warn.
4577
       FIXME: If we see the definition in a regular object
4578
       later on, we will warn, but we shouldn't.  The only
4579
       fix is to keep track of what warnings we are supposed
4580
       to emit, and then handle them all at the end of the
4581
       link.  */
4582
0
    if (dynamic)
4583
0
      {
4584
0
        struct elf_link_hash_entry *h;
4585
4586
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4587
4588
        /* FIXME: What about bfd_link_hash_common?  */
4589
0
        if (h != NULL
4590
0
      && (h->root.type == bfd_link_hash_defined
4591
0
          || h->root.type == bfd_link_hash_defweak))
4592
0
    continue;
4593
0
      }
4594
4595
0
    sz = s->size;
4596
0
    msg = bfd_alloc (abfd, sz + 1);
4597
0
    if (msg == NULL)
4598
0
      goto error_return;
4599
4600
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4601
0
      goto error_return;
4602
4603
0
    msg[sz] = '\0';
4604
4605
0
    if (! (_bfd_generic_link_add_one_symbol
4606
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4607
0
      false, bed->collect, NULL)))
4608
0
      goto error_return;
4609
4610
0
    if (bfd_link_executable (info))
4611
0
      {
4612
        /* Clobber the section size so that the warning does
4613
     not get copied into the output file.  */
4614
0
        s->size = 0;
4615
4616
        /* Also set SEC_EXCLUDE, so that symbols defined in
4617
     the warning section don't get copied to the output.  */
4618
0
        s->flags |= SEC_EXCLUDE;
4619
0
      }
4620
0
  }
4621
0
    }
4622
4623
0
  just_syms = ((s = abfd->sections) != NULL
4624
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4625
4626
0
  add_needed = true;
4627
0
  if (! dynamic)
4628
0
    {
4629
      /* If we are creating a shared library, create all the dynamic
4630
   sections immediately.  We need to attach them to something,
4631
   so we attach them to this BFD, provided it is the right
4632
   format and is not from ld --just-symbols.  Always create the
4633
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4634
   are no input BFD's of the same format as the output, we can't
4635
   make a shared library.  */
4636
0
      if (!just_syms
4637
0
    && (bfd_link_pic (info)
4638
0
        || (!bfd_link_relocatable (info)
4639
0
      && info->nointerp
4640
0
      && (info->export_dynamic || info->dynamic)))
4641
0
    && compatible_format (info, abfd))
4642
0
  {
4643
0
    if (!bfd_elf_link_create_dynamic_sections (info))
4644
0
      goto error_return;
4645
0
  }
4646
0
    }
4647
0
  else if (!is_elf_hash_table (&htab->root))
4648
0
    goto error_return;
4649
0
  else
4650
0
    {
4651
0
      const char *soname = NULL;
4652
0
      char *audit = NULL;
4653
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4654
0
      const Elf_Internal_Phdr *phdr;
4655
0
      struct elf_link_loaded_list *loaded_lib;
4656
4657
      /* ld --just-symbols and dynamic objects don't mix very well.
4658
   ld shouldn't allow it.  */
4659
0
      if (just_syms)
4660
0
  abort ();
4661
4662
      /* If this dynamic lib was specified on the command line with
4663
   --as-needed in effect, then we don't want to add a DT_NEEDED
4664
   tag unless the lib is actually used.  Similary for libs brought
4665
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4666
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4667
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4668
   all.  */
4669
0
      add_needed = (elf_dyn_lib_class (abfd)
4670
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4671
0
           | DYN_NO_NEEDED)) == 0;
4672
4673
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4674
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4675
0
  {
4676
0
    bfd_byte *dynbuf;
4677
0
    bfd_byte *extdyn;
4678
0
    unsigned int elfsec;
4679
0
    unsigned long shlink;
4680
4681
0
    if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4682
0
      {
4683
0
      error_free_dyn:
4684
0
        _bfd_elf_munmap_section_contents (s, dynbuf);
4685
0
        goto error_return;
4686
0
      }
4687
4688
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4689
0
    if (elfsec == SHN_BAD)
4690
0
      goto error_free_dyn;
4691
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4692
4693
0
    for (extdyn = dynbuf;
4694
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4695
0
         extdyn += bed->s->sizeof_dyn)
4696
0
      {
4697
0
        Elf_Internal_Dyn dyn;
4698
4699
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4700
0
        if (dyn.d_tag == DT_SONAME)
4701
0
    {
4702
0
      unsigned int tagv = dyn.d_un.d_val;
4703
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4704
0
      if (soname == NULL)
4705
0
        goto error_free_dyn;
4706
0
    }
4707
0
        if (dyn.d_tag == DT_NEEDED)
4708
0
    {
4709
0
      struct bfd_link_needed_list *n, **pn;
4710
0
      char *fnm, *anm;
4711
0
      unsigned int tagv = dyn.d_un.d_val;
4712
0
      size_t amt;
4713
4714
0
      n = bfd_alloc (abfd, sizeof (*n));
4715
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4716
0
      if (n == NULL || fnm == NULL)
4717
0
        goto error_free_dyn;
4718
0
      amt = strlen (fnm) + 1;
4719
0
      anm = bfd_alloc (abfd, amt);
4720
0
      if (anm == NULL)
4721
0
        goto error_free_dyn;
4722
0
      memcpy (anm, fnm, amt);
4723
0
      n->name = anm;
4724
0
      n->by = abfd;
4725
0
      n->next = NULL;
4726
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4727
0
        ;
4728
0
      *pn = n;
4729
0
    }
4730
0
        if (dyn.d_tag == DT_RUNPATH)
4731
0
    {
4732
0
      struct bfd_link_needed_list *n, **pn;
4733
0
      char *fnm, *anm;
4734
0
      unsigned int tagv = dyn.d_un.d_val;
4735
0
      size_t amt;
4736
4737
0
      n = bfd_alloc (abfd, sizeof (*n));
4738
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4739
0
      if (n == NULL || fnm == NULL)
4740
0
        goto error_free_dyn;
4741
0
      amt = strlen (fnm) + 1;
4742
0
      anm = bfd_alloc (abfd, amt);
4743
0
      if (anm == NULL)
4744
0
        goto error_free_dyn;
4745
0
      memcpy (anm, fnm, amt);
4746
0
      n->name = anm;
4747
0
      n->by = abfd;
4748
0
      n->next = NULL;
4749
0
      for (pn = & runpath;
4750
0
           *pn != NULL;
4751
0
           pn = &(*pn)->next)
4752
0
        ;
4753
0
      *pn = n;
4754
0
    }
4755
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4756
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4757
0
    {
4758
0
      struct bfd_link_needed_list *n, **pn;
4759
0
      char *fnm, *anm;
4760
0
      unsigned int tagv = dyn.d_un.d_val;
4761
0
      size_t amt;
4762
4763
0
      n = bfd_alloc (abfd, sizeof (*n));
4764
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4765
0
      if (n == NULL || fnm == NULL)
4766
0
        goto error_free_dyn;
4767
0
      amt = strlen (fnm) + 1;
4768
0
      anm = bfd_alloc (abfd, amt);
4769
0
      if (anm == NULL)
4770
0
        goto error_free_dyn;
4771
0
      memcpy (anm, fnm, amt);
4772
0
      n->name = anm;
4773
0
      n->by = abfd;
4774
0
      n->next = NULL;
4775
0
      for (pn = & rpath;
4776
0
           *pn != NULL;
4777
0
           pn = &(*pn)->next)
4778
0
        ;
4779
0
      *pn = n;
4780
0
    }
4781
0
        if (dyn.d_tag == DT_AUDIT)
4782
0
    {
4783
0
      unsigned int tagv = dyn.d_un.d_val;
4784
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4785
0
    }
4786
0
        if (dyn.d_tag == DT_FLAGS_1)
4787
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4788
0
      }
4789
4790
0
    _bfd_elf_munmap_section_contents (s, dynbuf);
4791
0
  }
4792
4793
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4794
   frees all more recently bfd_alloc'd blocks as well.  */
4795
0
      if (runpath)
4796
0
  rpath = runpath;
4797
4798
0
      if (rpath)
4799
0
  {
4800
0
    struct bfd_link_needed_list **pn;
4801
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4802
0
      ;
4803
0
    *pn = rpath;
4804
0
  }
4805
4806
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4807
   all sections contained fully therein.  This makes relro
4808
   shared library sections appear as they will at run-time.  */
4809
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4810
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4811
0
  if (phdr->p_type == PT_GNU_RELRO)
4812
0
    {
4813
0
      for (s = abfd->sections; s != NULL; s = s->next)
4814
0
        {
4815
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4816
4817
0
    if ((s->flags & SEC_ALLOC) != 0
4818
0
        && s->vma * opb >= phdr->p_vaddr
4819
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4820
0
      s->flags |= SEC_READONLY;
4821
0
        }
4822
0
      break;
4823
0
    }
4824
4825
      /* We do not want to include any of the sections in a dynamic
4826
   object in the output file.  We hack by simply clobbering the
4827
   list of sections in the BFD.  This could be handled more
4828
   cleanly by, say, a new section flag; the existing
4829
   SEC_NEVER_LOAD flag is not the one we want, because that one
4830
   still implies that the section takes up space in the output
4831
   file.  */
4832
0
      bfd_section_list_clear (abfd);
4833
4834
      /* Find the name to use in a DT_NEEDED entry that refers to this
4835
   object.  If the object has a DT_SONAME entry, we use it.
4836
   Otherwise, if the generic linker stuck something in
4837
   elf_dt_name, we use that.  Otherwise, we just use the file
4838
   name.  */
4839
0
      if (soname == NULL || *soname == '\0')
4840
0
  {
4841
0
    soname = elf_dt_name (abfd);
4842
0
    if (soname == NULL || *soname == '\0')
4843
0
      soname = bfd_get_filename (abfd);
4844
0
  }
4845
4846
      /* Save the SONAME because sometimes the linker emulation code
4847
   will need to know it.  */
4848
0
      elf_dt_name (abfd) = soname;
4849
4850
      /* If we have already included this dynamic object in the
4851
   link, just ignore it.  There is no reason to include a
4852
   particular dynamic object more than once.  */
4853
0
      for (loaded_lib = htab->dyn_loaded;
4854
0
     loaded_lib != NULL;
4855
0
     loaded_lib = loaded_lib->next)
4856
0
  {
4857
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4858
0
      return true;
4859
0
  }
4860
4861
      /* Create dynamic sections for backends that require that be done
4862
   before setup_gnu_properties.  */
4863
0
      if (add_needed
4864
0
    && !bfd_elf_link_create_dynamic_sections (info))
4865
0
  return false;
4866
4867
      /* Save the DT_AUDIT entry for the linker emulation code. */
4868
0
      elf_dt_audit (abfd) = audit;
4869
0
    }
4870
4871
  /* If this is a dynamic object, we always link against the .dynsym
4872
     symbol table, not the .symtab symbol table.  The dynamic linker
4873
     will only see the .dynsym symbol table, so there is no reason to
4874
     look at .symtab for a dynamic object.  */
4875
4876
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4877
0
    hdr = &elf_symtab_hdr (abfd);
4878
0
  else
4879
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4880
4881
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4882
4883
  /* The sh_info field of the symtab header tells us where the
4884
     external symbols start.  We don't care about the local symbols at
4885
     this point.  */
4886
0
  if (elf_bad_symtab (abfd))
4887
0
    {
4888
0
      extsymcount = symcount;
4889
0
      extsymoff = 0;
4890
0
    }
4891
0
  else
4892
0
    {
4893
0
      extsymcount = symcount - hdr->sh_info;
4894
0
      extsymoff = hdr->sh_info;
4895
0
    }
4896
4897
0
  sym_hash = elf_sym_hashes (abfd);
4898
0
  if (extsymcount != 0)
4899
0
    {
4900
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4901
0
              NULL, NULL, NULL);
4902
0
      if (isymbuf == NULL)
4903
0
  goto error_return;
4904
4905
0
      if (sym_hash == NULL)
4906
0
  {
4907
    /* We store a pointer to the hash table entry for each
4908
       external symbol.  */
4909
0
    sym_hash = bfd_zalloc (abfd, extsymcount * sizeof (*sym_hash));
4910
0
    if (sym_hash == NULL)
4911
0
      goto error_free_sym;
4912
0
    elf_sym_hashes (abfd) = sym_hash;
4913
0
  }
4914
0
    }
4915
4916
0
  if (dynamic)
4917
0
    {
4918
      /* Read in any version definitions.  */
4919
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4920
0
            info->default_imported_symver))
4921
0
  goto error_free_sym;
4922
4923
      /* Read in the symbol versions, but don't bother to convert them
4924
   to internal format.  */
4925
0
      if (elf_dynversym (abfd) != 0)
4926
0
  {
4927
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4928
0
    bfd_size_type amt = versymhdr->sh_size;
4929
4930
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4931
0
      goto error_free_sym;
4932
0
    extversym = _bfd_malloc_and_read (abfd, amt, amt);
4933
0
    if (extversym == NULL)
4934
0
      goto error_free_sym;
4935
0
    extversym_end = extversym + amt / sizeof (*extversym);
4936
0
  }
4937
0
    }
4938
4939
  /* If we are loading an as-needed shared lib, save the symbol table
4940
     state before we start adding symbols.  If the lib turns out
4941
     to be unneeded, restore the state.  */
4942
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4943
0
    {
4944
0
      unsigned int i;
4945
0
      size_t entsize;
4946
4947
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4948
0
  {
4949
0
    struct bfd_hash_entry *p;
4950
0
    struct elf_link_hash_entry *h;
4951
4952
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4953
0
      {
4954
0
        h = (struct elf_link_hash_entry *) p;
4955
0
        entsize += htab->root.table.entsize;
4956
0
        if (h->root.type == bfd_link_hash_warning)
4957
0
    {
4958
0
      entsize += htab->root.table.entsize;
4959
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4960
0
    }
4961
0
        if (h->root.type == bfd_link_hash_common)
4962
0
    entsize += sizeof (*h->root.u.c.p);
4963
0
      }
4964
0
  }
4965
4966
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4967
0
      old_tab = bfd_malloc (tabsize + entsize);
4968
0
      if (old_tab == NULL)
4969
0
  goto error_free_vers;
4970
4971
      /* Remember the current objalloc pointer, so that all mem for
4972
   symbols added can later be reclaimed.  */
4973
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4974
0
      if (alloc_mark == NULL)
4975
0
  goto error_free_vers;
4976
4977
      /* Make a special call to the linker "notice" function to
4978
   tell it that we are about to handle an as-needed lib.  */
4979
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4980
0
  goto error_free_vers;
4981
4982
      /* Clone the symbol table.  Remember some pointers into the
4983
   symbol table, and dynamic symbol count.  */
4984
0
      old_ent = (char *) old_tab + tabsize;
4985
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4986
0
      old_undefs = htab->root.undefs;
4987
0
      old_undefs_tail = htab->root.undefs_tail;
4988
0
      old_table = htab->root.table.table;
4989
0
      old_size = htab->root.table.size;
4990
0
      old_count = htab->root.table.count;
4991
0
      old_strtab = NULL;
4992
0
      if (htab->dynstr != NULL)
4993
0
  {
4994
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4995
0
    if (old_strtab == NULL)
4996
0
      goto error_free_vers;
4997
0
  }
4998
4999
0
      for (i = 0; i < htab->root.table.size; i++)
5000
0
  {
5001
0
    struct bfd_hash_entry *p;
5002
0
    struct elf_link_hash_entry *h;
5003
5004
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5005
0
      {
5006
0
        h = (struct elf_link_hash_entry *) p;
5007
0
        memcpy (old_ent, h, htab->root.table.entsize);
5008
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5009
0
        if (h->root.type == bfd_link_hash_warning)
5010
0
    {
5011
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5012
0
      memcpy (old_ent, h, htab->root.table.entsize);
5013
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5014
0
    }
5015
0
        if (h->root.type == bfd_link_hash_common)
5016
0
    {
5017
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
5018
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5019
0
    }
5020
0
      }
5021
0
  }
5022
0
    }
5023
5024
0
  weaks = NULL;
5025
0
  if (extversym == NULL)
5026
0
    ever = NULL;
5027
0
  else if (extversym + extsymoff < extversym_end)
5028
0
    ever = extversym + extsymoff;
5029
0
  else
5030
0
    {
5031
      /* xgettext:c-format */
5032
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
5033
0
        abfd, (long) extsymoff,
5034
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
5035
0
      bfd_set_error (bfd_error_bad_value);
5036
0
      goto error_free_vers;
5037
0
    }
5038
5039
0
  if (!bfd_link_relocatable (info)
5040
0
      && bfd_get_lto_type (abfd) == lto_slim_ir_object)
5041
0
    {
5042
0
      _bfd_error_handler
5043
0
  (_("%pB: plugin needed to handle lto object"), abfd);
5044
0
    }
5045
5046
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
5047
0
       isym < isymend;
5048
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
5049
0
    {
5050
0
      int bind;
5051
0
      bfd_vma value;
5052
0
      asection *sec, *new_sec;
5053
0
      flagword flags;
5054
0
      const char *name;
5055
0
      const char *defvername;
5056
0
      bool must_copy_name = false;
5057
0
      struct elf_link_hash_entry *h;
5058
0
      struct elf_link_hash_entry *hi;
5059
0
      bool definition;
5060
0
      bool size_change_ok;
5061
0
      bool type_change_ok;
5062
0
      bool new_weak;
5063
0
      bool old_weak;
5064
0
      bfd *override;
5065
0
      bool common;
5066
0
      bool discarded;
5067
0
      unsigned int old_alignment;
5068
0
      unsigned int shindex;
5069
0
      bfd *old_bfd;
5070
0
      bool matched;
5071
5072
0
      override = NULL;
5073
5074
0
      flags = BSF_NO_FLAGS;
5075
0
      sec = NULL;
5076
0
      value = isym->st_value;
5077
0
      common = bed->common_definition (isym);
5078
0
      if (common && info->inhibit_common_definition)
5079
0
  {
5080
    /* Treat common symbol as undefined for --no-define-common.  */
5081
0
    isym->st_shndx = SHN_UNDEF;
5082
0
    common = false;
5083
0
  }
5084
0
      discarded = false;
5085
5086
0
      bind = ELF_ST_BIND (isym->st_info);
5087
0
      switch (bind)
5088
0
  {
5089
0
  case STB_LOCAL:
5090
    /* This should be impossible, since ELF requires that all
5091
       global symbols follow all local symbols, and that sh_info
5092
       point to the first global symbol.  Unfortunately, Irix 5
5093
       screws this up.  */
5094
0
    if (elf_bad_symtab (abfd))
5095
0
      continue;
5096
5097
    /* If we aren't prepared to handle locals within the globals
5098
       then we'll likely segfault on a NULL symbol hash if the
5099
       symbol is ever referenced in relocations.  */
5100
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
5101
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5102
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5103
0
        " (>= sh_info of %lu)"),
5104
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
5105
0
            (long) extsymoff);
5106
5107
    /* Dynamic object relocations are not processed by ld, so
5108
       ld won't run into the problem mentioned above.  */
5109
0
    if (dynamic)
5110
0
      continue;
5111
0
    bfd_set_error (bfd_error_bad_value);
5112
0
    goto error_free_vers;
5113
5114
0
  case STB_GLOBAL:
5115
0
    if (isym->st_shndx != SHN_UNDEF && !common)
5116
0
      flags = BSF_GLOBAL;
5117
0
    break;
5118
5119
0
  case STB_WEAK:
5120
0
    flags = BSF_WEAK;
5121
0
    break;
5122
5123
0
  case STB_GNU_UNIQUE:
5124
0
    flags = BSF_GNU_UNIQUE;
5125
0
    break;
5126
5127
0
  default:
5128
    /* Leave it up to the processor backend.  */
5129
0
    break;
5130
0
  }
5131
5132
0
      if (isym->st_shndx == SHN_UNDEF)
5133
0
  sec = bfd_und_section_ptr;
5134
0
      else if (isym->st_shndx == SHN_ABS)
5135
0
  sec = bfd_abs_section_ptr;
5136
0
      else if (isym->st_shndx == SHN_COMMON)
5137
0
  {
5138
0
    sec = bfd_com_section_ptr;
5139
    /* What ELF calls the size we call the value.  What ELF
5140
       calls the value we call the alignment.  */
5141
0
    value = isym->st_size;
5142
0
  }
5143
0
      else
5144
0
  {
5145
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5146
0
    if (sec == NULL)
5147
0
      sec = bfd_abs_section_ptr;
5148
0
    else if (discarded_section (sec))
5149
0
      {
5150
        /* Symbols from discarded section are undefined.  We keep
5151
     its visibility.  */
5152
0
        sec = bfd_und_section_ptr;
5153
0
        discarded = true;
5154
0
        isym->st_shndx = SHN_UNDEF;
5155
0
      }
5156
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5157
0
      value -= sec->vma;
5158
0
  }
5159
5160
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5161
0
                isym->st_name);
5162
0
      if (name == NULL)
5163
0
  goto error_free_vers;
5164
5165
0
      if (isym->st_shndx == SHN_COMMON
5166
0
    && (abfd->flags & BFD_PLUGIN) != 0)
5167
0
  {
5168
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5169
5170
0
    if (xc == NULL)
5171
0
      {
5172
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5173
0
         | SEC_EXCLUDE);
5174
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5175
0
        if (xc == NULL)
5176
0
    goto error_free_vers;
5177
0
      }
5178
0
    sec = xc;
5179
0
  }
5180
0
      else if (isym->st_shndx == SHN_COMMON
5181
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
5182
0
         && !bfd_link_relocatable (info))
5183
0
  {
5184
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5185
5186
0
    if (tcomm == NULL)
5187
0
      {
5188
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5189
0
         | SEC_LINKER_CREATED);
5190
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5191
0
        if (tcomm == NULL)
5192
0
    goto error_free_vers;
5193
0
      }
5194
0
    sec = tcomm;
5195
0
  }
5196
0
      else if (bed->elf_add_symbol_hook)
5197
0
  {
5198
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5199
0
               &sec, &value))
5200
0
      goto error_free_vers;
5201
5202
    /* The hook function sets the name to NULL if this symbol
5203
       should be skipped for some reason.  */
5204
0
    if (name == NULL)
5205
0
      continue;
5206
0
  }
5207
5208
0
      if (name[0] == '\0')
5209
0
  {
5210
0
    _bfd_error_handler (_("%pB: corrupt symbol table"), abfd);
5211
0
    bfd_set_error (bfd_error_bad_value);
5212
0
    goto error_free_vers;
5213
0
  }
5214
5215
      /* Sanity check that all possibilities were handled.  */
5216
0
      if (sec == NULL)
5217
0
  abort ();
5218
5219
      /* Silently discard TLS symbols from --just-syms.  There's
5220
   no way to combine a static TLS block with a new TLS block
5221
   for this executable.  */
5222
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5223
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5224
0
  continue;
5225
5226
0
      if (bfd_is_und_section (sec)
5227
0
    || bfd_is_com_section (sec))
5228
0
  definition = false;
5229
0
      else
5230
0
  definition = true;
5231
5232
0
      size_change_ok = false;
5233
0
      type_change_ok = bed->type_change_ok;
5234
0
      old_weak = false;
5235
0
      matched = false;
5236
0
      old_alignment = 0;
5237
0
      old_bfd = NULL;
5238
0
      new_sec = sec;
5239
0
      defvername = NULL;
5240
5241
0
      if (is_elf_hash_table (&htab->root))
5242
0
  {
5243
0
    Elf_Internal_Versym iver;
5244
0
    unsigned int vernum = 0;
5245
0
    bool skip;
5246
5247
0
    if (ever == NULL)
5248
0
      {
5249
0
        if (info->default_imported_symver)
5250
    /* Use the default symbol version created earlier.  */
5251
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
5252
0
        else
5253
0
    iver.vs_vers = 0;
5254
0
      }
5255
0
    else if (ever >= extversym_end)
5256
0
      {
5257
        /* xgettext:c-format */
5258
0
        _bfd_error_handler (_("%pB: not enough version information"),
5259
0
          abfd);
5260
0
        bfd_set_error (bfd_error_bad_value);
5261
0
        goto error_free_vers;
5262
0
      }
5263
0
    else
5264
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
5265
5266
0
    vernum = iver.vs_vers & VERSYM_VERSION;
5267
5268
    /* If this is a hidden symbol, or if it is not version
5269
       1, we append the version name to the symbol name.
5270
       However, we do not modify a non-hidden absolute symbol
5271
       if it is not a function, because it might be the version
5272
       symbol itself.  FIXME: What if it isn't?  */
5273
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5274
0
        || (vernum > 1
5275
0
      && (!bfd_is_abs_section (sec)
5276
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5277
0
      {
5278
0
        const char *verstr;
5279
0
        size_t namelen, verlen, newlen;
5280
0
        char *newname, *p;
5281
5282
0
        if (isym->st_shndx != SHN_UNDEF)
5283
0
    {
5284
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5285
0
        verstr = NULL;
5286
0
      else if (vernum > 1)
5287
0
        verstr =
5288
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5289
0
      else
5290
0
        verstr = "";
5291
5292
0
      if (verstr == NULL)
5293
0
        {
5294
0
          _bfd_error_handler
5295
      /* xgettext:c-format */
5296
0
      (_("%pB: %s: invalid version %u (max %d)"),
5297
0
       abfd, name, vernum,
5298
0
       elf_tdata (abfd)->cverdefs);
5299
0
          bfd_set_error (bfd_error_bad_value);
5300
0
          goto error_free_vers;
5301
0
        }
5302
0
    }
5303
0
        else
5304
0
    {
5305
      /* We cannot simply test for the number of
5306
         entries in the VERNEED section since the
5307
         numbers for the needed versions do not start
5308
         at 0.  */
5309
0
      Elf_Internal_Verneed *t;
5310
5311
0
      verstr = NULL;
5312
0
      for (t = elf_tdata (abfd)->verref;
5313
0
           t != NULL;
5314
0
           t = t->vn_nextref)
5315
0
        {
5316
0
          Elf_Internal_Vernaux *a;
5317
5318
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5319
0
      {
5320
0
        if (a->vna_other == vernum)
5321
0
          {
5322
0
            verstr = a->vna_nodename;
5323
0
            break;
5324
0
          }
5325
0
      }
5326
0
          if (a != NULL)
5327
0
      break;
5328
0
        }
5329
0
      if (verstr == NULL)
5330
0
        {
5331
0
          _bfd_error_handler
5332
      /* xgettext:c-format */
5333
0
      (_("%pB: %s: invalid needed version %d"),
5334
0
       abfd, name, vernum);
5335
0
          bfd_set_error (bfd_error_bad_value);
5336
0
          goto error_free_vers;
5337
0
        }
5338
0
    }
5339
5340
0
        namelen = strlen (name);
5341
0
        verlen = strlen (verstr);
5342
0
        newlen = namelen + verlen + 2;
5343
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5344
0
      && isym->st_shndx != SHN_UNDEF)
5345
0
    ++newlen;
5346
5347
0
        newname = bfd_hash_allocate (&htab->root.table, newlen);
5348
0
        if (newname == NULL)
5349
0
    goto error_free_vers;
5350
0
        memcpy (newname, name, namelen);
5351
0
        p = newname + namelen;
5352
0
        *p++ = ELF_VER_CHR;
5353
        /* If this is a defined non-hidden version symbol,
5354
     we add another @ to the name.  This indicates the
5355
     default version of the symbol.  */
5356
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5357
0
      && isym->st_shndx != SHN_UNDEF)
5358
0
    *p++ = ELF_VER_CHR, defvername = name;
5359
0
        memcpy (p, verstr, verlen + 1);
5360
5361
0
        name = newname;
5362
        /* Since bfd_hash_alloc is used for "name", the string
5363
     must be copied if added to first_hash.  The string
5364
     memory can be freed when an --as-needed library is
5365
     not needed.  */
5366
0
        must_copy_name = true;
5367
0
      }
5368
5369
    /* If this symbol has default visibility and the user has
5370
       requested we not re-export it, then mark it as hidden.  */
5371
0
    if (!bfd_is_und_section (sec)
5372
0
        && !dynamic
5373
0
        && abfd->no_export
5374
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5375
0
      isym->st_other = (STV_HIDDEN
5376
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5377
5378
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5379
0
              sym_hash, &old_bfd, &old_weak,
5380
0
              &old_alignment, &skip, &override,
5381
0
              &type_change_ok, &size_change_ok,
5382
0
              &matched))
5383
0
      goto error_free_vers;
5384
5385
0
    if (skip)
5386
0
      continue;
5387
5388
0
    h = *sym_hash;
5389
0
    while (h->root.type == bfd_link_hash_indirect
5390
0
     || h->root.type == bfd_link_hash_warning)
5391
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5392
5393
    /* Override a definition only if the new symbol matches the
5394
       existing one.  */
5395
0
    if (override && matched)
5396
0
      {
5397
0
        definition = false;
5398
0
        if (htab->first_hash != NULL
5399
0
      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5400
0
      && h->root.non_ir_ref_regular)
5401
0
    {
5402
      /* When reloading --as-needed shared objects for new
5403
         symbols added from IR inputs, if this shared object
5404
         has the first definition, use it.  */
5405
0
      struct elf_link_first_hash_entry *e
5406
0
        = ((struct elf_link_first_hash_entry *)
5407
0
           bfd_hash_lookup (htab->first_hash, name, false,
5408
0
          false));
5409
0
      if (e != NULL && e->abfd == abfd)
5410
0
        definition = true;
5411
0
    }
5412
0
      }
5413
5414
0
    if (h->versioned != unversioned
5415
0
        && elf_tdata (abfd)->verdef != NULL
5416
0
        && vernum > 1
5417
0
        && definition)
5418
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5419
0
  }
5420
5421
0
      if (! (_bfd_generic_link_add_one_symbol
5422
0
       (info, override ? override : abfd, name, flags, sec, value,
5423
0
        NULL, false, bed->collect,
5424
0
        (struct bfd_link_hash_entry **) sym_hash)))
5425
0
  goto error_free_vers;
5426
5427
0
      h = *sym_hash;
5428
      /* We need to make sure that indirect symbol dynamic flags are
5429
   updated.  */
5430
0
      hi = h;
5431
0
      while (h->root.type == bfd_link_hash_indirect
5432
0
       || h->root.type == bfd_link_hash_warning)
5433
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5434
5435
0
      *sym_hash = h;
5436
5437
      /* Setting the index to -3 tells elf_link_output_extsym that
5438
   this symbol is defined in a discarded section.  */
5439
0
      if (discarded && is_elf_hash_table (&htab->root))
5440
0
  h->indx = -3;
5441
5442
0
      new_weak = (flags & BSF_WEAK) != 0;
5443
0
      if (dynamic
5444
0
    && definition
5445
0
    && new_weak
5446
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5447
0
    && is_elf_hash_table (&htab->root)
5448
0
    && h->u.alias == NULL)
5449
0
  {
5450
    /* Keep a list of all weak defined non function symbols from
5451
       a dynamic object, using the alias field.  Later in this
5452
       function we will set the alias field to the correct
5453
       value.  We only put non-function symbols from dynamic
5454
       objects on this list, because that happens to be the only
5455
       time we need to know the normal symbol corresponding to a
5456
       weak symbol, and the information is time consuming to
5457
       figure out.  If the alias field is not already NULL,
5458
       then this symbol was already defined by some previous
5459
       dynamic object, and we will be using that previous
5460
       definition anyhow.  */
5461
5462
0
    h->u.alias = weaks;
5463
0
    weaks = h;
5464
0
  }
5465
5466
      /* Set the alignment of a common symbol.  */
5467
0
      if ((common || bfd_is_com_section (sec))
5468
0
    && h->root.type == bfd_link_hash_common)
5469
0
  {
5470
0
    unsigned int align;
5471
5472
0
    if (common)
5473
0
      align = bfd_log2 (isym->st_value);
5474
0
    else
5475
0
      {
5476
        /* The new symbol is a common symbol in a shared object.
5477
     We need to get the alignment from the section.  */
5478
0
        align = new_sec->alignment_power;
5479
0
      }
5480
0
    if (align > old_alignment)
5481
0
      h->root.u.c.p->alignment_power = align;
5482
0
    else
5483
0
      h->root.u.c.p->alignment_power = old_alignment;
5484
0
  }
5485
5486
0
      if (is_elf_hash_table (&htab->root))
5487
0
  {
5488
    /* Set a flag in the hash table entry indicating the type of
5489
       reference or definition we just found.  A dynamic symbol
5490
       is one which is referenced or defined by both a regular
5491
       object and a shared object.  */
5492
0
    bool dynsym = false;
5493
5494
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5495
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5496
0
      {
5497
        /* Except for this flag to track nonweak references.  */
5498
0
        if (!definition
5499
0
      && bind != STB_WEAK)
5500
0
    h->ref_ir_nonweak = 1;
5501
0
      }
5502
0
    else if (!dynamic)
5503
0
      {
5504
0
        if (! definition)
5505
0
    {
5506
0
      h->ref_regular = 1;
5507
0
      if (bind != STB_WEAK)
5508
0
        h->ref_regular_nonweak = 1;
5509
0
    }
5510
0
        else
5511
0
    {
5512
0
      h->def_regular = 1;
5513
0
      if (h->def_dynamic)
5514
0
        {
5515
0
          h->def_dynamic = 0;
5516
0
          h->ref_dynamic = 1;
5517
0
        }
5518
0
    }
5519
0
      }
5520
0
    else
5521
0
      {
5522
0
        if (! definition)
5523
0
    {
5524
0
      h->ref_dynamic = 1;
5525
0
      hi->ref_dynamic = 1;
5526
0
    }
5527
0
        else
5528
0
    {
5529
0
      h->def_dynamic = 1;
5530
0
      hi->def_dynamic = 1;
5531
0
    }
5532
0
      }
5533
5534
    /* If an indirect symbol has been forced local, don't
5535
       make the real symbol dynamic.  */
5536
0
    if (h != hi && hi->forced_local)
5537
0
      ;
5538
0
    else if (!dynamic)
5539
0
      {
5540
0
        if (bfd_link_dll (info)
5541
0
      || h->def_dynamic
5542
0
      || h->ref_dynamic)
5543
0
    dynsym = true;
5544
0
      }
5545
0
    else
5546
0
      {
5547
0
        if (h->def_regular
5548
0
      || h->ref_regular
5549
0
      || (h->is_weakalias
5550
0
          && weakdef (h)->dynindx != -1))
5551
0
    dynsym = true;
5552
0
      }
5553
5554
    /* Check to see if we need to add an indirect symbol for
5555
       the default name.  */
5556
0
    if ((definition
5557
0
         || (!override && h->root.type == bfd_link_hash_common))
5558
0
        && !(hi != h
5559
0
       && hi->versioned == versioned_hidden))
5560
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5561
0
                sec, value, &old_bfd, &dynsym))
5562
0
        goto error_free_vers;
5563
5564
    /* Check the alignment when a common symbol is involved. This
5565
       can change when a common symbol is overridden by a normal
5566
       definition or a common symbol is ignored due to the old
5567
       normal definition. We need to make sure the maximum
5568
       alignment is maintained.  */
5569
0
    if ((old_alignment || common)
5570
0
        && h->root.type != bfd_link_hash_common)
5571
0
      {
5572
0
        unsigned int common_align;
5573
0
        unsigned int normal_align;
5574
0
        unsigned int symbol_align;
5575
0
        bfd *normal_bfd;
5576
0
        bfd *common_bfd;
5577
5578
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5579
0
        || h->root.type == bfd_link_hash_defweak);
5580
5581
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5582
0
        if (h->root.u.def.section->owner != NULL
5583
0
      && (h->root.u.def.section->owner->flags
5584
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5585
0
    {
5586
0
      normal_align = h->root.u.def.section->alignment_power;
5587
0
      if (normal_align > symbol_align)
5588
0
        normal_align = symbol_align;
5589
0
    }
5590
0
        else
5591
0
    normal_align = symbol_align;
5592
5593
0
        if (old_alignment)
5594
0
    {
5595
0
      common_align = old_alignment;
5596
0
      common_bfd = old_bfd;
5597
0
      normal_bfd = abfd;
5598
0
    }
5599
0
        else
5600
0
    {
5601
0
      common_align = bfd_log2 (isym->st_value);
5602
0
      common_bfd = abfd;
5603
0
      normal_bfd = old_bfd;
5604
0
    }
5605
5606
0
        if (normal_align < common_align)
5607
0
    {
5608
      /* PR binutils/2735 */
5609
0
      uint64_t c_align = UINT64_C (1) << common_align;
5610
0
      uint64_t n_align = UINT64_C (1) << normal_align;
5611
0
      if (normal_bfd == NULL)
5612
0
        _bfd_error_handler
5613
          /* xgettext:c-format */
5614
0
          (_("warning: alignment %" PRIu64 " of common symbol `%s' in %pB is"
5615
0
       " greater than the alignment (%" PRIu64 ") of its section %pA"),
5616
0
           c_align, name, common_bfd,
5617
0
           n_align, h->root.u.def.section);
5618
0
      else
5619
0
        _bfd_error_handler
5620
          /* xgettext:c-format */
5621
0
          (_("warning: alignment %" PRIu64 " of normal symbol `%s' in %pB"
5622
0
       " is smaller than %" PRIu64 " used by the common definition in %pB"),
5623
0
           n_align, name, normal_bfd,
5624
0
           c_align, common_bfd);
5625
5626
      /* PR 30499: make sure that users understand that this warning is serious.  */
5627
0
      _bfd_error_handler
5628
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5629
0
    }
5630
0
      }
5631
5632
    /* Remember the symbol size if it isn't undefined.  */
5633
0
    if (isym->st_size != 0
5634
0
        && isym->st_shndx != SHN_UNDEF
5635
0
        && (definition || h->size == 0))
5636
0
      {
5637
0
        if (h->size != 0
5638
0
      && h->size != isym->st_size
5639
0
      && ! size_change_ok)
5640
0
    {
5641
0
      _bfd_error_handler
5642
        /* xgettext:c-format */
5643
0
        (_("warning: size of symbol `%s' changed"
5644
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5645
0
         name, (uint64_t) h->size, old_bfd,
5646
0
         (uint64_t) isym->st_size, abfd);
5647
5648
      /* PR 30499: make sure that users understand that this warning is serious.  */
5649
0
      _bfd_error_handler
5650
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5651
0
    }
5652
5653
0
        h->size = isym->st_size;
5654
0
      }
5655
5656
    /* If this is a common symbol, then we always want H->SIZE
5657
       to be the size of the common symbol.  The code just above
5658
       won't fix the size if a common symbol becomes larger.  We
5659
       don't warn about a size change here, because that is
5660
       covered by --warn-common.  Allow changes between different
5661
       function types.  */
5662
0
    if (h->root.type == bfd_link_hash_common)
5663
0
      h->size = h->root.u.c.size;
5664
5665
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5666
0
        && ((definition && !new_weak)
5667
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5668
0
      || h->type == STT_NOTYPE))
5669
0
      {
5670
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5671
5672
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5673
     symbol.  */
5674
0
        if (type == STT_GNU_IFUNC
5675
0
      && (abfd->flags & DYNAMIC) != 0)
5676
0
    type = STT_FUNC;
5677
5678
0
        if (h->type != type)
5679
0
    {
5680
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5681
        /* xgettext:c-format */
5682
0
        _bfd_error_handler
5683
0
          (_("warning: type of symbol `%s' changed"
5684
0
       " from %d to %d in %pB"),
5685
0
           name, h->type, type, abfd);
5686
5687
0
      h->type = type;
5688
0
    }
5689
0
      }
5690
5691
    /* Merge st_other field.  */
5692
0
    elf_merge_st_other (info->output_bfd, abfd, h, isym->st_other,
5693
0
            sec, definition, dynamic);
5694
5695
    /* We don't want to make debug symbol dynamic.  */
5696
0
    if (definition
5697
0
        && (sec->flags & SEC_DEBUGGING)
5698
0
        && !bfd_link_relocatable (info))
5699
0
      dynsym = false;
5700
5701
    /* Nor should we make plugin symbols dynamic.  */
5702
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5703
0
      dynsym = false;
5704
5705
0
    if (definition)
5706
0
      {
5707
0
        h->target_internal = isym->st_target_internal;
5708
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5709
0
      }
5710
5711
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5712
       in IR.  Since all data or text symbols in IR have the
5713
       same type, value and section, we can't tell if a symbol
5714
       is an alias of another symbol by their types, values and
5715
       sections.  */
5716
0
    if (definition
5717
0
        && !dynamic
5718
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5719
0
      {
5720
0
        const char *p = strchr (name, ELF_VER_CHR);
5721
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5722
0
    {
5723
      /* Queue non-default versions so that .symver x, x@FOO
5724
         aliases can be checked.  */
5725
0
      if (!nondeflt_vers)
5726
0
        {
5727
0
          nondeflt_vers = bfd_malloc ((isymend - isym + 1)
5728
0
              * sizeof (*nondeflt_vers));
5729
0
          if (!nondeflt_vers)
5730
0
      goto error_free_vers;
5731
0
        }
5732
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5733
0
    }
5734
0
      }
5735
5736
0
    if (dynsym && h->dynindx == -1)
5737
0
      {
5738
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5739
0
    goto error_free_vers;
5740
0
        if (h->is_weakalias
5741
0
      && weakdef (h)->dynindx == -1)
5742
0
    {
5743
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5744
0
        goto error_free_vers;
5745
0
    }
5746
0
      }
5747
0
    else if (h->dynindx != -1)
5748
      /* If the symbol already has a dynamic index, but
5749
         visibility says it should not be visible, turn it into
5750
         a local symbol.  */
5751
0
      switch (ELF_ST_VISIBILITY (h->other))
5752
0
        {
5753
0
        case STV_INTERNAL:
5754
0
        case STV_HIDDEN:
5755
0
    obed->elf_backend_hide_symbol (info, h, true);
5756
0
    dynsym = false;
5757
0
    break;
5758
0
        }
5759
5760
0
    if (!add_needed
5761
0
        && matched
5762
0
        && definition
5763
0
        && h->root.type != bfd_link_hash_indirect)
5764
0
      {
5765
0
        if ((dynsym
5766
0
       && h->ref_regular_nonweak)
5767
0
      || (old_bfd != NULL
5768
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5769
0
          && h->ref_ir_nonweak
5770
0
          && !info->lto_all_symbols_read)
5771
0
      || (h->ref_dynamic_nonweak
5772
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5773
0
          && !on_needed_list (elf_dt_name (abfd),
5774
0
            htab->needed, NULL)))
5775
0
    {
5776
0
      const char *soname = elf_dt_name (abfd);
5777
5778
0
      info->callbacks->minfo ("%!", soname, old_bfd,
5779
0
            h->root.root.string);
5780
5781
      /* A symbol from a library loaded via DT_NEEDED of some
5782
         other library is referenced by a regular object.
5783
         Add a DT_NEEDED entry for it.  Issue an error if
5784
         --no-add-needed is used and the reference was not
5785
         a weak one.  */
5786
0
      if (old_bfd != NULL
5787
0
          && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5788
0
        {
5789
0
          _bfd_error_handler
5790
      /* xgettext:c-format */
5791
0
      (_("%pB: undefined reference to symbol '%s'"),
5792
0
       old_bfd, name);
5793
0
          bfd_set_error (bfd_error_missing_dso);
5794
0
          goto error_free_vers;
5795
0
        }
5796
5797
0
      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5798
0
        (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5799
5800
      /* Create dynamic sections for backends that require
5801
         that be done before setup_gnu_properties.  */
5802
0
      if (!bfd_elf_link_create_dynamic_sections (info))
5803
0
        goto error_free_vers;
5804
0
      add_needed = true;
5805
0
    }
5806
0
        else if (dynamic
5807
0
           && h->root.u.def.section->owner == abfd)
5808
0
    {
5809
      /* Add this symbol to first hash if this shared
5810
         object has the first definition.  */
5811
0
      elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5812
      /* And if it was the default symbol version definition,
5813
         also add the short name.  */
5814
0
      if (defvername)
5815
0
        elf_link_add_to_first_hash (abfd, info, defvername, false);
5816
0
    }
5817
0
      }
5818
0
  }
5819
0
    }
5820
5821
0
  if (info->lto_plugin_active
5822
0
      && !bfd_link_relocatable (info)
5823
0
      && (abfd->flags & BFD_PLUGIN) == 0
5824
0
      && !just_syms
5825
0
      && extsymcount != 0
5826
0
      && is_elf_hash_table (&htab->root))
5827
0
    {
5828
0
      int r_sym_shift;
5829
5830
0
      if (bed->s->arch_size == 32)
5831
0
  r_sym_shift = 8;
5832
0
      else
5833
0
  r_sym_shift = 32;
5834
5835
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5836
   referenced in regular objects so that linker plugin will get
5837
   the correct symbol resolution.  */
5838
5839
0
      sym_hash = elf_sym_hashes (abfd);
5840
0
      for (s = abfd->sections; s != NULL; s = s->next)
5841
0
  {
5842
0
    Elf_Internal_Rela *internal_relocs;
5843
0
    Elf_Internal_Rela *rel, *relend;
5844
5845
    /* Don't check relocations in excluded sections.  */
5846
0
    if ((s->flags & SEC_RELOC) == 0
5847
0
        || s->reloc_count == 0
5848
0
        || (s->flags & SEC_EXCLUDE) != 0
5849
0
        || (s->flags & SEC_DEBUGGING) != 0)
5850
0
      continue;
5851
5852
0
    internal_relocs = _bfd_elf_link_info_read_relocs
5853
0
      (abfd, info, s, NULL, NULL,
5854
0
       _bfd_elf_link_keep_memory (info));
5855
0
    if (internal_relocs == NULL)
5856
0
      goto error_free_vers;
5857
5858
0
    rel = internal_relocs;
5859
0
    relend = rel + s->reloc_count;
5860
0
    for ( ; rel < relend; rel++)
5861
0
      {
5862
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5863
0
        struct elf_link_hash_entry *h;
5864
5865
        /* Skip local symbols.  */
5866
0
        if (r_symndx < extsymoff)
5867
0
    continue;
5868
5869
0
        h = sym_hash[r_symndx - extsymoff];
5870
0
        if (h != NULL)
5871
0
    h->root.non_ir_ref_regular = 1;
5872
0
      }
5873
5874
0
    if (elf_section_data (s)->relocs != internal_relocs)
5875
0
      free (internal_relocs);
5876
0
  }
5877
0
    }
5878
5879
0
  free (extversym);
5880
0
  extversym = NULL;
5881
0
  free (isymbuf);
5882
0
  isymbuf = NULL;
5883
5884
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5885
0
    {
5886
0
      unsigned int i;
5887
5888
      /* Restore the symbol table.  */
5889
0
      old_ent = (char *) old_tab + tabsize;
5890
0
      memset (elf_sym_hashes (abfd), 0,
5891
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5892
0
      htab->root.table.table = old_table;
5893
0
      htab->root.table.size = old_size;
5894
0
      htab->root.table.count = old_count;
5895
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5896
0
      htab->root.undefs = old_undefs;
5897
0
      htab->root.undefs_tail = old_undefs_tail;
5898
0
      if (htab->dynstr != NULL)
5899
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5900
0
      free (old_strtab);
5901
0
      old_strtab = NULL;
5902
0
      for (i = 0; i < htab->root.table.size; i++)
5903
0
  {
5904
0
    struct bfd_hash_entry *p;
5905
0
    struct elf_link_hash_entry *h;
5906
0
    unsigned int non_ir_ref_dynamic;
5907
5908
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5909
0
      {
5910
        /* Preserve non_ir_ref_dynamic so that this symbol
5911
     will be exported when the dynamic lib becomes needed
5912
     in the second pass.  */
5913
0
        h = (struct elf_link_hash_entry *) p;
5914
0
        if (h->root.type == bfd_link_hash_warning)
5915
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5916
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5917
5918
0
        h = (struct elf_link_hash_entry *) p;
5919
0
        memcpy (h, old_ent, htab->root.table.entsize);
5920
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5921
0
        if (h->root.type == bfd_link_hash_warning)
5922
0
    {
5923
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5924
0
      memcpy (h, old_ent, htab->root.table.entsize);
5925
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5926
0
    }
5927
0
        if (h->root.type == bfd_link_hash_common)
5928
0
    {
5929
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5930
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5931
0
    }
5932
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5933
0
      }
5934
0
  }
5935
5936
      /* Make a special call to the linker "notice" function to
5937
   tell it that symbols added for crefs may need to be removed.  */
5938
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5939
0
  goto error_free_vers;
5940
5941
0
      free (old_tab);
5942
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5943
0
         alloc_mark);
5944
0
      free (nondeflt_vers);
5945
0
      return true;
5946
0
    }
5947
5948
0
  free (old_strtab);
5949
0
  old_strtab = NULL;
5950
0
  if (old_tab != NULL)
5951
0
    {
5952
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5953
0
  goto error_free_vers;
5954
0
      free (old_tab);
5955
0
      old_tab = NULL;
5956
0
    }
5957
5958
  /* Now that all the symbols from this input file are created, if
5959
     not performing a relocatable link, handle .symver foo, foo@BAR
5960
     such that any relocs against foo become foo@BAR.  */
5961
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5962
0
    {
5963
0
      size_t cnt, symidx;
5964
5965
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5966
0
  {
5967
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5968
0
    char *shortname;
5969
0
    const char *p;
5970
0
    size_t amt;
5971
5972
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5973
0
    if (p == NULL
5974
0
        || (h->root.type != bfd_link_hash_defined
5975
0
      && h->root.type != bfd_link_hash_defweak))
5976
0
      continue;
5977
5978
0
    amt = p - h->root.root.string;
5979
0
    shortname = bfd_malloc (amt + 1);
5980
0
    if (!shortname)
5981
0
      goto error_free_vers;
5982
0
    memcpy (shortname, h->root.root.string, amt);
5983
0
    shortname[amt] = '\0';
5984
5985
0
    hi = (struct elf_link_hash_entry *)
5986
0
         bfd_link_hash_lookup (&htab->root, shortname,
5987
0
             false, false, false);
5988
0
    if (hi != NULL
5989
0
        && hi->root.type == h->root.type
5990
0
        && hi->root.u.def.value == h->root.u.def.value
5991
0
        && hi->root.u.def.section == h->root.u.def.section)
5992
0
      {
5993
0
        obed->elf_backend_hide_symbol (info, hi, true);
5994
0
        hi->root.type = bfd_link_hash_indirect;
5995
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5996
0
        obed->elf_backend_copy_indirect_symbol (info, h, hi);
5997
0
        sym_hash = elf_sym_hashes (abfd);
5998
0
        if (sym_hash)
5999
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
6000
0
      if (sym_hash[symidx] == hi)
6001
0
        {
6002
0
          sym_hash[symidx] = h;
6003
0
          break;
6004
0
        }
6005
0
      }
6006
0
    free (shortname);
6007
0
  }
6008
0
    }
6009
0
  free (nondeflt_vers);
6010
0
  nondeflt_vers = NULL;
6011
6012
  /* Now set the alias field correctly for all the weak defined
6013
     symbols we found.  The only way to do this is to search all the
6014
     symbols.  Since we only need the information for non functions in
6015
     dynamic objects, that's the only time we actually put anything on
6016
     the list WEAKS.  We need this information so that if a regular
6017
     object refers to a symbol defined weakly in a dynamic object, the
6018
     real symbol in the dynamic object is also put in the dynamic
6019
     symbols; we also must arrange for both symbols to point to the
6020
     same memory location.  We could handle the general case of symbol
6021
     aliasing, but a general symbol alias can only be generated in
6022
     assembler code, handling it correctly would be very time
6023
     consuming, and other ELF linkers don't handle general aliasing
6024
     either.  */
6025
0
  if (weaks != NULL)
6026
0
    {
6027
0
      struct elf_link_hash_entry **hpp;
6028
0
      struct elf_link_hash_entry **hppend;
6029
0
      struct elf_link_hash_entry **sorted_sym_hash;
6030
0
      struct elf_link_hash_entry *h;
6031
0
      size_t sym_count;
6032
6033
      /* Since we have to search the whole symbol list for each weak
6034
   defined symbol, search time for N weak defined symbols will be
6035
   O(N^2). Binary search will cut it down to O(NlogN).  */
6036
0
      sorted_sym_hash = bfd_malloc (extsymcount * sizeof (*sorted_sym_hash));
6037
0
      if (sorted_sym_hash == NULL)
6038
0
  goto error_return;
6039
0
      sym_hash = sorted_sym_hash;
6040
0
      hpp = elf_sym_hashes (abfd);
6041
0
      hppend = hpp + extsymcount;
6042
0
      sym_count = 0;
6043
0
      for (; hpp < hppend; hpp++)
6044
0
  {
6045
0
    h = *hpp;
6046
0
    if (h != NULL
6047
0
        && h->root.type == bfd_link_hash_defined
6048
0
        && !bed->is_function_type (h->type))
6049
0
      {
6050
0
        *sym_hash = h;
6051
0
        sym_hash++;
6052
0
        sym_count++;
6053
0
      }
6054
0
  }
6055
6056
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
6057
0
       elf_sort_symbol);
6058
6059
0
      while (weaks != NULL)
6060
0
  {
6061
0
    struct elf_link_hash_entry *hlook;
6062
0
    asection *slook;
6063
0
    bfd_vma vlook;
6064
0
    size_t i, j, idx = 0;
6065
6066
0
    hlook = weaks;
6067
0
    weaks = hlook->u.alias;
6068
0
    hlook->u.alias = NULL;
6069
6070
0
    if (hlook->root.type != bfd_link_hash_defined
6071
0
        && hlook->root.type != bfd_link_hash_defweak)
6072
0
      continue;
6073
6074
0
    slook = hlook->root.u.def.section;
6075
0
    vlook = hlook->root.u.def.value;
6076
6077
0
    i = 0;
6078
0
    j = sym_count;
6079
0
    while (i != j)
6080
0
      {
6081
0
        bfd_signed_vma vdiff;
6082
0
        idx = (i + j) / 2;
6083
0
        h = sorted_sym_hash[idx];
6084
0
        vdiff = vlook - h->root.u.def.value;
6085
0
        if (vdiff < 0)
6086
0
    j = idx;
6087
0
        else if (vdiff > 0)
6088
0
    i = idx + 1;
6089
0
        else
6090
0
    {
6091
0
      int sdiff = slook->id - h->root.u.def.section->id;
6092
0
      if (sdiff < 0)
6093
0
        j = idx;
6094
0
      else if (sdiff > 0)
6095
0
        i = idx + 1;
6096
0
      else
6097
0
        break;
6098
0
    }
6099
0
      }
6100
6101
    /* We didn't find a value/section match.  */
6102
0
    if (i == j)
6103
0
      continue;
6104
6105
    /* With multiple aliases, or when the weak symbol is already
6106
       strongly defined, we have multiple matching symbols and
6107
       the binary search above may land on any of them.  Step
6108
       one past the matching symbol(s).  */
6109
0
    while (++idx != j)
6110
0
      {
6111
0
        h = sorted_sym_hash[idx];
6112
0
        if (h->root.u.def.section != slook
6113
0
      || h->root.u.def.value != vlook)
6114
0
    break;
6115
0
      }
6116
6117
    /* Now look back over the aliases.  Since we sorted by size
6118
       as well as value and section, we'll choose the one with
6119
       the largest size.  */
6120
0
    while (idx-- != i)
6121
0
      {
6122
0
        h = sorted_sym_hash[idx];
6123
6124
        /* Stop if value or section doesn't match.  */
6125
0
        if (h->root.u.def.section != slook
6126
0
      || h->root.u.def.value != vlook)
6127
0
    break;
6128
0
        else if (h != hlook)
6129
0
    {
6130
0
      struct elf_link_hash_entry *t;
6131
6132
0
      hlook->u.alias = h;
6133
0
      hlook->is_weakalias = 1;
6134
0
      t = h;
6135
0
      if (t->u.alias != NULL)
6136
0
        while (t->u.alias != h)
6137
0
          t = t->u.alias;
6138
0
      t->u.alias = hlook;
6139
6140
      /* If the weak definition is in the list of dynamic
6141
         symbols, make sure the real definition is put
6142
         there as well.  */
6143
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
6144
0
        {
6145
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6146
0
      {
6147
0
      err_free_sym_hash:
6148
0
        free (sorted_sym_hash);
6149
0
        goto error_return;
6150
0
      }
6151
0
        }
6152
6153
      /* If the real definition is in the list of dynamic
6154
         symbols, make sure the weak definition is put
6155
         there as well.  If we don't do this, then the
6156
         dynamic loader might not merge the entries for the
6157
         real definition and the weak definition.  */
6158
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
6159
0
        {
6160
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6161
0
      goto err_free_sym_hash;
6162
0
        }
6163
0
      break;
6164
0
    }
6165
0
      }
6166
0
  }
6167
6168
0
      free (sorted_sym_hash);
6169
0
    }
6170
6171
0
  if (bed->check_directives
6172
0
      && !(*bed->check_directives) (abfd, info))
6173
0
    goto error_return;
6174
6175
  /* If this is a non-traditional link, try to optimize the handling
6176
     of the .stab/.stabstr sections.  */
6177
0
  if (! dynamic
6178
0
      && ! info->traditional_format
6179
0
      && is_elf_hash_table (&htab->root)
6180
0
      && (info->strip != strip_all && info->strip != strip_debugger))
6181
0
    {
6182
0
      asection *stabstr;
6183
6184
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6185
0
      if (stabstr != NULL)
6186
0
  {
6187
0
    bfd_size_type string_offset = 0;
6188
0
    asection *stab;
6189
6190
0
    for (stab = abfd->sections; stab; stab = stab->next)
6191
0
      if (startswith (stab->name, ".stab")
6192
0
    && (!stab->name[5] ||
6193
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6194
0
    && (stab->flags & SEC_MERGE) == 0
6195
0
    && !bfd_is_abs_section (stab->output_section)
6196
0
    && !_bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6197
0
               stabstr, &string_offset))
6198
0
        goto error_return;
6199
0
  }
6200
0
    }
6201
6202
0
  if (dynamic && add_needed)
6203
0
    {
6204
      /* Add this bfd to the loaded list.  */
6205
0
      struct elf_link_loaded_list *n;
6206
6207
0
      n = bfd_alloc (abfd, sizeof (*n));
6208
0
      if (n == NULL)
6209
0
  goto error_return;
6210
0
      n->abfd = abfd;
6211
0
      n->next = htab->dyn_loaded;
6212
0
      htab->dyn_loaded = n;
6213
0
    }
6214
0
  if (dynamic && !add_needed
6215
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6216
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6217
6218
0
  return true;
6219
6220
0
 error_free_vers:
6221
0
  free (old_tab);
6222
0
  free (old_strtab);
6223
0
  free (nondeflt_vers);
6224
0
  free (extversym);
6225
0
 error_free_sym:
6226
0
  free (isymbuf);
6227
0
 error_return:
6228
0
  return false;
6229
0
}
6230
6231
/* Return the linker hash table entry of a symbol that might be
6232
   satisfied by an archive symbol.  Return -1 on error.  */
6233
6234
struct bfd_link_hash_entry *
6235
_bfd_elf_archive_symbol_lookup (bfd *abfd,
6236
        struct bfd_link_info *info,
6237
        const char *name)
6238
0
{
6239
0
  struct bfd_link_hash_entry *h;
6240
0
  const char *p;
6241
0
  char *copy;
6242
0
  size_t len, first;
6243
6244
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6245
0
  if (h != NULL)
6246
0
    return h;
6247
6248
  /* If this is a default version (the name contains @@), look up the
6249
     symbol again with only one `@' as well as without the version.
6250
     The effect is that references to the symbol with and without the
6251
     version will be matched by the default symbol in the archive.  */
6252
6253
0
  p = strchr (name, ELF_VER_CHR);
6254
0
  if (p == NULL || p[1] != ELF_VER_CHR)
6255
0
    {
6256
      /* Add this symbol to first hash if this archive has the first
6257
   definition.  */
6258
0
      if (is_elf_hash_table (info->hash))
6259
0
  elf_link_add_to_first_hash (abfd, info, name, false);
6260
0
      return h;
6261
0
    }
6262
6263
  /* First check with only one `@'.  */
6264
0
  len = strlen (name);
6265
0
  copy = bfd_alloc (abfd, len);
6266
0
  if (copy == NULL)
6267
0
    return (struct bfd_link_hash_entry *) -1;
6268
6269
0
  first = p - name + 1;
6270
0
  memcpy (copy, name, first);
6271
0
  memcpy (copy + first, name + first + 1, len - first);
6272
6273
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6274
0
  if (h == NULL)
6275
0
    {
6276
      /* We also need to check references to the symbol without the
6277
   version.  */
6278
0
      copy[first - 1] = '\0';
6279
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6280
0
    }
6281
6282
0
  bfd_release (abfd, copy);
6283
0
  return h;
6284
0
}
6285
6286
/* Add symbols from an ELF archive file to the linker hash table.  We
6287
   don't use _bfd_generic_link_add_archive_symbols because we need to
6288
   handle versioned symbols.
6289
6290
   Fortunately, ELF archive handling is simpler than that done by
6291
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6292
   oddities.  In ELF, if we find a symbol in the archive map, and the
6293
   symbol is currently undefined, we know that we must pull in that
6294
   object file.
6295
6296
   Unfortunately, we do have to make multiple passes over the symbol
6297
   table until nothing further is resolved.  */
6298
6299
static bool
6300
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6301
0
{
6302
0
  symindex c;
6303
0
  unsigned char *included = NULL;
6304
0
  carsym *symdefs;
6305
0
  bool loop;
6306
0
  elf_backend_data *bed;
6307
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
6308
0
    (bfd *, struct bfd_link_info *, const char *);
6309
6310
0
  if (! bfd_has_map (abfd))
6311
0
    {
6312
0
      bfd *first_one = bfd_openr_next_archived_file (abfd, NULL);
6313
6314
      /* An empty archive is a special case.  */
6315
0
      if (first_one == NULL)
6316
0
  return true;
6317
6318
0
      if (!_bfd_make_armap (abfd, first_one))
6319
0
  return false;
6320
0
    }
6321
6322
  /* Keep track of all symbols we know to be already defined, and all
6323
     files we know to be already included.  This is to speed up the
6324
     second and subsequent passes.  */
6325
0
  c = bfd_ardata (abfd)->symdef_count;
6326
0
  if (c == 0)
6327
0
    return true;
6328
0
  included = bfd_zmalloc (c * sizeof (*included));
6329
0
  if (included == NULL)
6330
0
    return false;
6331
6332
0
  symdefs = bfd_ardata (abfd)->symdefs;
6333
0
  bed = get_elf_backend_data (abfd);
6334
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6335
6336
0
  do
6337
0
    {
6338
0
      ufile_ptr_or_bfd last;
6339
0
      symindex i;
6340
0
      carsym *symdef;
6341
0
      carsym *symdefend;
6342
6343
0
      loop = false;
6344
0
      last = _bfd_elt_nil (abfd);
6345
6346
0
      symdef = symdefs;
6347
0
      symdefend = symdef + c;
6348
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6349
0
  {
6350
0
    struct bfd_link_hash_entry *h;
6351
0
    bfd *element;
6352
0
    struct bfd_link_hash_entry *undefs_tail;
6353
0
    symindex mark;
6354
6355
0
    if (included[i])
6356
0
      continue;
6357
0
    if (_bfd_elt_eq (abfd, symdef->u, last))
6358
0
      {
6359
0
        included[i] = true;
6360
0
        continue;
6361
0
      }
6362
6363
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6364
0
    if (h == (struct bfd_link_hash_entry *) -1)
6365
0
      goto error_return;
6366
6367
0
    if (h == NULL)
6368
0
      continue;
6369
6370
0
    if (h->type == bfd_link_hash_undefined)
6371
0
      {
6372
0
        if (is_elf_hash_table (info->hash))
6373
0
    {
6374
      /* If the archive element has already been loaded then one
6375
         of the symbols defined by that element might have been
6376
         made undefined due to being in a discarded section.  */
6377
0
      if (((struct elf_link_hash_entry *) h)->indx == -3)
6378
0
        continue;
6379
6380
      /* In the pre-LTO-plugin pass we must not mistakenly
6381
         include this archive member if an earlier shared
6382
         library defined this symbol.  */
6383
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
6384
0
      if (htab->first_hash)
6385
0
        {
6386
0
          struct elf_link_first_hash_entry *e
6387
0
        = ((struct elf_link_first_hash_entry *)
6388
0
           bfd_hash_lookup (htab->first_hash, symdef->name,
6389
0
                false, false));
6390
0
          if (e
6391
0
        && (e->abfd->flags & DYNAMIC) != 0
6392
0
        && e->abfd != abfd)
6393
0
      continue;
6394
0
        }
6395
0
    }
6396
0
      }
6397
0
    else if (h->type == bfd_link_hash_common)
6398
0
      {
6399
        /* We currently have a common symbol.  The archive map contains
6400
     a reference to this symbol, so we may want to include it.  We
6401
     only want to include it however, if this archive element
6402
     contains a definition of the symbol, not just another common
6403
     declaration of it.
6404
6405
     Unfortunately some archivers (including GNU ar) will put
6406
     declarations of common symbols into their archive maps, as
6407
     well as real definitions, so we cannot just go by the archive
6408
     map alone.  Instead we must read in the element's symbol
6409
     table and check that to see what kind of symbol definition
6410
     this is.  */
6411
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6412
0
    continue;
6413
0
      }
6414
0
    else
6415
0
      {
6416
0
        if (h->type != bfd_link_hash_undefweak)
6417
    /* Symbol must be defined.  Don't check it again.  */
6418
0
    included[i] = true;
6419
6420
0
        if (!is_elf_hash_table (info->hash))
6421
0
    continue;
6422
0
        struct elf_link_hash_entry *eh
6423
0
    = (struct elf_link_hash_entry *) h;
6424
        /* Ignore the archive if the symbol isn't referenced by a
6425
     regular object or isn't defined in a shared object.  */
6426
0
        if (!eh->ref_regular || !eh->def_dynamic)
6427
0
    continue;
6428
        /* Ignore the dynamic definition if symbol is first
6429
     defined in this archive.  */
6430
0
        struct elf_link_hash_table *htab = elf_hash_table (info);
6431
0
        if (htab->first_hash == NULL)
6432
0
    continue;
6433
0
        struct elf_link_first_hash_entry *e
6434
0
    = ((struct elf_link_first_hash_entry *)
6435
0
       bfd_hash_lookup (htab->first_hash, symdef->name,
6436
0
            false, false));
6437
0
        if (e == NULL || e->abfd != abfd)
6438
0
    continue;
6439
0
      }
6440
6441
    /* We need to include this archive member.  */
6442
0
    element = _bfd_get_elt_from_symdef (abfd, symdef, info);
6443
0
    if (element == NULL)
6444
0
      goto error_return;
6445
6446
0
    if (! bfd_check_format (element, bfd_object))
6447
0
      goto error_return;
6448
6449
0
    undefs_tail = info->hash->undefs_tail;
6450
6451
0
    if (!(*info->callbacks
6452
0
    ->add_archive_element) (info, element, symdef->name, &element))
6453
0
      continue;
6454
0
    if (!bfd_link_add_symbols (element, info))
6455
0
      goto error_return;
6456
6457
    /* If there are any new undefined symbols, we need to make
6458
       another pass through the archive in order to see whether
6459
       they can be defined.  FIXME: This isn't perfect, because
6460
       common symbols wind up on undefs_tail and because an
6461
       undefined symbol which is defined later on in this pass
6462
       does not require another pass.  This isn't a bug, but it
6463
       does make the code less efficient than it could be.  */
6464
0
    if (undefs_tail != info->hash->undefs_tail)
6465
0
      loop = true;
6466
6467
    /* Look backward to mark all symbols from this object file
6468
       which we have already seen in this pass.  */
6469
0
    mark = i;
6470
0
    do
6471
0
      {
6472
0
        included[mark] = true;
6473
0
        if (mark == 0)
6474
0
    break;
6475
0
        --mark;
6476
0
      }
6477
0
    while (_bfd_elt_eq (abfd, symdefs[mark].u, symdef->u));
6478
6479
    /* We mark subsequent symbols from this object file as we go
6480
       on through the loop.  */
6481
0
    last = symdef->u;
6482
0
  }
6483
0
    }
6484
0
  while (loop);
6485
6486
0
  free (included);
6487
0
  return true;
6488
6489
0
 error_return:
6490
0
  free (included);
6491
0
  return false;
6492
0
}
6493
6494
/* Given an ELF BFD, add symbols to the global hash table as
6495
   appropriate.  */
6496
6497
bool
6498
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6499
0
{
6500
0
  switch (bfd_get_format (abfd))
6501
0
    {
6502
0
    case bfd_object:
6503
0
      return elf_link_add_object_symbols (abfd, info);
6504
0
    case bfd_archive:
6505
0
      return elf_link_add_archive_symbols (abfd, info);
6506
0
    default:
6507
0
      bfd_set_error (bfd_error_wrong_format);
6508
0
      return false;
6509
0
    }
6510
0
}
6511

6512
struct hash_codes_info
6513
{
6514
  unsigned long *hashcodes;
6515
  bool error;
6516
};
6517
6518
/* This function will be called though elf_link_hash_traverse to store
6519
   all hash value of the exported symbols in an array.  */
6520
6521
static bool
6522
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6523
0
{
6524
0
  struct hash_codes_info *inf = data;
6525
0
  const char *name;
6526
0
  unsigned long ha;
6527
0
  char *alc = NULL;
6528
6529
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6530
0
  if (h->dynindx == -1)
6531
0
    return true;
6532
6533
0
  name = h->root.root.string;
6534
0
  if (h->versioned >= versioned)
6535
0
    {
6536
0
      const char *p = strchr (name, ELF_VER_CHR);
6537
0
      if (p != NULL)
6538
0
  {
6539
0
    alc = bfd_malloc (p - name + 1);
6540
0
    if (alc == NULL)
6541
0
      {
6542
0
        inf->error = true;
6543
0
        return false;
6544
0
      }
6545
0
    memcpy (alc, name, p - name);
6546
0
    alc[p - name] = '\0';
6547
0
    name = alc;
6548
0
  }
6549
0
    }
6550
6551
  /* Compute the hash value.  */
6552
0
  ha = bfd_elf_hash (name);
6553
6554
  /* Store the found hash value in the array given as the argument.  */
6555
0
  *(inf->hashcodes)++ = ha;
6556
6557
  /* And store it in the struct so that we can put it in the hash table
6558
     later.  */
6559
0
  h->u.elf_hash_value = ha;
6560
6561
0
  free (alc);
6562
0
  return true;
6563
0
}
6564
6565
struct collect_gnu_hash_codes
6566
{
6567
  bfd *output_bfd;
6568
  elf_backend_data *bed;
6569
  unsigned long int nsyms;
6570
  unsigned long int maskbits;
6571
  unsigned long int *hashcodes;
6572
  unsigned long int *hashval;
6573
  unsigned long int *indx;
6574
  unsigned long int *counts;
6575
  bfd_vma *bitmask;
6576
  bfd_byte *contents;
6577
  bfd_size_type xlat;
6578
  long int min_dynindx;
6579
  unsigned long int bucketcount;
6580
  unsigned long int symindx;
6581
  long int local_indx;
6582
  long int shift1, shift2;
6583
  unsigned long int mask;
6584
  bool error;
6585
  bool base_symbol;
6586
};
6587
6588
/* This function will be called though elf_link_hash_traverse to store
6589
   all hash value of the exported symbols in an array.  */
6590
6591
static bool
6592
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6593
0
{
6594
0
  struct collect_gnu_hash_codes *s = data;
6595
0
  const char *name;
6596
0
  unsigned long ha;
6597
0
  char *alc = NULL;
6598
6599
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6600
0
  if (h->dynindx == -1)
6601
0
    return true;
6602
6603
  /* Ignore also local symbols and undefined symbols.  */
6604
0
  if (! (*s->bed->elf_hash_symbol) (h))
6605
0
    return true;
6606
6607
0
  name = h->root.root.string;
6608
0
  if (h->versioned >= versioned)
6609
0
    {
6610
0
      const char *p = strchr (name, ELF_VER_CHR);
6611
0
      if (p != NULL)
6612
0
  {
6613
0
    alc = bfd_malloc (p - name + 1);
6614
0
    if (alc == NULL)
6615
0
      {
6616
0
        s->error = true;
6617
0
        return false;
6618
0
      }
6619
0
    memcpy (alc, name, p - name);
6620
0
    alc[p - name] = '\0';
6621
0
    name = alc;
6622
0
  }
6623
0
    }
6624
6625
  /* Compute the hash value.  */
6626
0
  ha = bfd_elf_gnu_hash (name);
6627
6628
  /* Store the found hash value in the array for compute_bucket_count,
6629
     and also for .dynsym reordering purposes.  */
6630
0
  s->hashcodes[s->nsyms] = ha;
6631
0
  s->hashval[h->dynindx] = ha;
6632
0
  ++s->nsyms;
6633
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6634
0
    s->min_dynindx = h->dynindx;
6635
6636
0
  free (alc);
6637
0
  return true;
6638
0
}
6639
6640
/* This function will be called though elf_link_hash_traverse to do
6641
   final dynamic symbol renumbering in case of .gnu.hash.
6642
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6643
   to the translation table.  */
6644
6645
static bool
6646
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6647
0
{
6648
0
  struct collect_gnu_hash_codes *s = data;
6649
0
  unsigned long int bucket;
6650
0
  unsigned long int val;
6651
6652
  /* Ignore indirect symbols.  */
6653
0
  if (h->dynindx == -1)
6654
0
    return true;
6655
6656
  /* Skip if base symbol doesn't match.  */
6657
0
  if (s->base_symbol != !!h->base_symbol)
6658
0
    return true;
6659
6660
  /* Ignore also local symbols and undefined symbols.  */
6661
0
  if (! (*s->bed->elf_hash_symbol) (h))
6662
0
    {
6663
0
      if (h->dynindx >= s->min_dynindx)
6664
0
  {
6665
0
    if (s->bed->record_xhash_symbol != NULL)
6666
0
      {
6667
0
        (*s->bed->record_xhash_symbol) (h, 0);
6668
0
        s->local_indx++;
6669
0
      }
6670
0
    else
6671
0
      h->dynindx = s->local_indx++;
6672
0
  }
6673
0
      return true;
6674
0
    }
6675
6676
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6677
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6678
0
  & ((s->maskbits >> s->shift1) - 1);
6679
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6680
0
  s->bitmask[val]
6681
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6682
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6683
0
  if (s->counts[bucket] == 1)
6684
    /* Last element terminates the chain.  */
6685
0
    val |= 1;
6686
0
  bfd_put_32 (s->output_bfd, val,
6687
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6688
0
  --s->counts[bucket];
6689
0
  if (s->bed->record_xhash_symbol != NULL)
6690
0
    {
6691
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6692
6693
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6694
0
    }
6695
0
  else
6696
0
    h->dynindx = s->indx[bucket]++;
6697
0
  return true;
6698
0
}
6699
6700
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6701
6702
bool
6703
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6704
0
{
6705
0
  return !(h->forced_local
6706
0
     || h->root.type == bfd_link_hash_undefined
6707
0
     || h->root.type == bfd_link_hash_undefweak
6708
0
     || ((h->root.type == bfd_link_hash_defined
6709
0
    || h->root.type == bfd_link_hash_defweak)
6710
0
         && h->root.u.def.section->output_section == NULL));
6711
0
}
6712
6713
/* Array used to determine the number of hash table buckets to use
6714
   based on the number of symbols there are.  If there are fewer than
6715
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6716
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6717
   than 32771 buckets.  */
6718
6719
static const size_t elf_buckets[] =
6720
{
6721
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6722
  16411, 32771, 0
6723
};
6724
6725
/* Compute bucket count for hashing table.  We do not use a static set
6726
   of possible tables sizes anymore.  Instead we determine for all
6727
   possible reasonable sizes of the table the outcome (i.e., the
6728
   number of collisions etc) and choose the best solution.  The
6729
   weighting functions are not too simple to allow the table to grow
6730
   without bounds.  Instead one of the weighting factors is the size.
6731
   Therefore the result is always a good payoff between few collisions
6732
   (= short chain lengths) and table size.  */
6733
static size_t
6734
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6735
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6736
          unsigned long int nsyms,
6737
          int gnu_hash)
6738
0
{
6739
0
  size_t best_size = 0;
6740
0
  unsigned long int i;
6741
6742
0
  if (info->optimize)
6743
0
    {
6744
0
      size_t minsize;
6745
0
      size_t maxsize;
6746
0
      uint64_t best_chlen = ~((uint64_t) 0);
6747
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6748
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6749
0
      elf_backend_data *obed = get_elf_backend_data (dynobj);
6750
0
      unsigned long int *counts;
6751
0
      unsigned int no_improvement_count = 0;
6752
6753
      /* Possible optimization parameters: if we have NSYMS symbols we say
6754
   that the hashing table must at least have NSYMS/4 and at most
6755
   2*NSYMS buckets.  */
6756
0
      minsize = nsyms / 4;
6757
0
      if (minsize == 0)
6758
0
  minsize = 1;
6759
0
      best_size = maxsize = nsyms * 2;
6760
0
      if (gnu_hash)
6761
0
  {
6762
0
    if (minsize < 2)
6763
0
      minsize = 2;
6764
0
    if ((best_size & 31) == 0)
6765
0
      ++best_size;
6766
0
  }
6767
6768
      /* Create array where we count the collisions in.  We must use bfd_malloc
6769
   since the size could be large.  */
6770
0
      counts = bfd_malloc (maxsize * sizeof (*counts));
6771
0
      if (counts == NULL)
6772
0
  return 0;
6773
6774
      /* Compute the "optimal" size for the hash table.  The criteria is a
6775
   minimal chain length.  The minor criteria is (of course) the size
6776
   of the table.  */
6777
0
      for (i = minsize; i < maxsize; ++i)
6778
0
  {
6779
    /* Walk through the array of hashcodes and count the collisions.  */
6780
0
    uint64_t max;
6781
0
    unsigned long int j;
6782
0
    unsigned long int fact;
6783
6784
0
    if (gnu_hash && (i & 31) == 0)
6785
0
      continue;
6786
6787
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6788
6789
    /* Determine how often each hash bucket is used.  */
6790
0
    for (j = 0; j < nsyms; ++j)
6791
0
      ++counts[hashcodes[j] % i];
6792
6793
    /* For the weight function we need some information about the
6794
       pagesize on the target.  This is information need not be 100%
6795
       accurate.  Since this information is not available (so far) we
6796
       define it here to a reasonable default value.  If it is crucial
6797
       to have a better value some day simply define this value.  */
6798
0
# ifndef BFD_TARGET_PAGESIZE
6799
0
#  define BFD_TARGET_PAGESIZE (4096)
6800
0
# endif
6801
6802
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6803
       and the chains.  */
6804
0
    max = (2 + dynsymcount) * obed->s->sizeof_hash_entry;
6805
6806
0
# if 1
6807
    /* Variant 1: optimize for short chains.  We add the squares
6808
       of all the chain lengths (which favors many small chain
6809
       over a few long chains).  */
6810
0
    for (j = 0; j < i; ++j)
6811
0
      max += counts[j] * counts[j];
6812
6813
    /* This adds penalties for the overall size of the table.  */
6814
0
    fact = i / (BFD_TARGET_PAGESIZE / obed->s->sizeof_hash_entry) + 1;
6815
0
    max *= fact * fact;
6816
# else
6817
    /* Variant 2: Optimize a lot more for small table.  Here we
6818
       also add squares of the size but we also add penalties for
6819
       empty slots (the +1 term).  */
6820
    for (j = 0; j < i; ++j)
6821
      max += (1 + counts[j]) * (1 + counts[j]);
6822
6823
    /* The overall size of the table is considered, but not as
6824
       strong as in variant 1, where it is squared.  */
6825
    fact = i / (BFD_TARGET_PAGESIZE / obed->s->sizeof_hash_entry) + 1;
6826
    max *= fact;
6827
# endif
6828
6829
    /* Compare with current best results.  */
6830
0
    if (max < best_chlen)
6831
0
      {
6832
0
        best_chlen = max;
6833
0
        best_size = i;
6834
0
        no_improvement_count = 0;
6835
0
      }
6836
    /* PR 11843: Avoid futile long searches for the best bucket size
6837
       when there are a large number of symbols.  */
6838
0
    else if (++no_improvement_count == 100)
6839
0
      break;
6840
0
  }
6841
6842
0
      free (counts);
6843
0
    }
6844
0
  else
6845
0
    {
6846
0
      for (i = 0; elf_buckets[i] != 0; i++)
6847
0
  {
6848
0
    best_size = elf_buckets[i];
6849
0
    if (nsyms < elf_buckets[i + 1])
6850
0
      break;
6851
0
  }
6852
0
      if (gnu_hash && best_size < 2)
6853
0
  best_size = 2;
6854
0
    }
6855
6856
0
  return best_size;
6857
0
}
6858
6859
/* Size any SHT_GROUP section for ld -r.  */
6860
6861
bool
6862
bfd_elf_size_group_sections (struct bfd_link_info *info)
6863
0
{
6864
0
  bfd *ibfd;
6865
0
  asection *s;
6866
6867
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6868
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6869
0
  && (s = ibfd->sections) != NULL
6870
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6871
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6872
0
      return false;
6873
0
  return true;
6874
0
}
6875
6876
/* Set a default stack segment size.  The value in INFO wins.  If it
6877
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6878
   undefined it is initialized.  */
6879
6880
bool
6881
bfd_elf_stack_segment_size (struct bfd_link_info *info,
6882
          const char *legacy_symbol,
6883
          bfd_vma default_size)
6884
0
{
6885
0
  struct elf_link_hash_entry *h = NULL;
6886
6887
  /* Look for legacy symbol.  */
6888
0
  if (legacy_symbol)
6889
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6890
0
            false, false, false);
6891
0
  if (h && (h->root.type == bfd_link_hash_defined
6892
0
      || h->root.type == bfd_link_hash_defweak)
6893
0
      && h->def_regular
6894
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6895
0
    {
6896
      /* The symbol has no type if specified on the command line.  */
6897
0
      h->type = STT_OBJECT;
6898
0
      if (info->stacksize)
6899
  /* xgettext:c-format */
6900
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6901
0
          info->output_bfd, legacy_symbol);
6902
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6903
  /* xgettext:c-format */
6904
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6905
0
          info->output_bfd, legacy_symbol);
6906
0
      else
6907
0
  info->stacksize = h->root.u.def.value;
6908
0
    }
6909
6910
0
  if (!info->stacksize)
6911
    /* If the user didn't set a size, or explicitly inhibit the
6912
       size, set it now.  */
6913
0
    info->stacksize = default_size;
6914
6915
  /* Provide the legacy symbol, if it is referenced.  */
6916
0
  if (h && (h->root.type == bfd_link_hash_undefined
6917
0
      || h->root.type == bfd_link_hash_undefweak))
6918
0
    {
6919
0
      struct bfd_link_hash_entry *bh = NULL;
6920
6921
0
      if (!(_bfd_generic_link_add_one_symbol
6922
0
      (info, info->output_bfd, legacy_symbol,
6923
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6924
0
       info->stacksize >= 0 ? info->stacksize : 0,
6925
0
       NULL, false, get_elf_backend_data (info->output_bfd)->collect,
6926
0
       &bh)))
6927
0
  return false;
6928
6929
0
      h = (struct elf_link_hash_entry *) bh;
6930
0
      h->def_regular = 1;
6931
0
      h->type = STT_OBJECT;
6932
0
    }
6933
6934
0
  return true;
6935
0
}
6936
6937
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6938
6939
struct elf_gc_sweep_symbol_info
6940
{
6941
  struct bfd_link_info *info;
6942
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6943
           bool);
6944
};
6945
6946
static bool
6947
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6948
0
{
6949
0
  if (!h->mark
6950
0
      && (((h->root.type == bfd_link_hash_defined
6951
0
      || h->root.type == bfd_link_hash_defweak)
6952
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6953
0
    && h->root.u.def.section->gc_mark))
6954
0
    || h->root.type == bfd_link_hash_undefined
6955
0
    || h->root.type == bfd_link_hash_undefweak))
6956
0
    {
6957
0
      struct elf_gc_sweep_symbol_info *inf = data;
6958
6959
0
      (*inf->hide_symbol) (inf->info, h, true);
6960
0
      h->def_regular = 0;
6961
0
      h->ref_regular = 0;
6962
0
      h->ref_regular_nonweak = 0;
6963
0
    }
6964
6965
0
  return true;
6966
0
}
6967
6968
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6969
   called by the ELF linker emulation before_allocation routine.  We
6970
   must set the sizes of the sections before the linker sets the
6971
   addresses of the various sections.  */
6972
6973
bool
6974
bfd_elf_size_dynamic_sections (struct bfd_link_info *info,
6975
             const char *soname,
6976
             const char *rpath,
6977
             const char *filter_shlib,
6978
             const char *audit,
6979
             const char *depaudit,
6980
             const char * const *auxiliary_filters,
6981
             asection **sinterpptr)
6982
0
{
6983
0
  bfd *dynobj;
6984
0
  elf_backend_data *obed;
6985
6986
0
  *sinterpptr = NULL;
6987
6988
0
  if (!is_elf_hash_table (info->hash))
6989
0
    return true;
6990
6991
  /* Any syms created from now on start with -1 in
6992
     got.refcount/offset and plt.refcount/offset.  */
6993
0
  elf_hash_table (info)->init_got_refcount
6994
0
    = elf_hash_table (info)->init_got_offset;
6995
0
  elf_hash_table (info)->init_plt_refcount
6996
0
    = elf_hash_table (info)->init_plt_offset;
6997
6998
0
  obed = get_elf_backend_data (info->output_bfd);
6999
7000
  /* The backend may have to create some sections regardless of whether
7001
     we're dynamic or not.  */
7002
0
  if (obed->elf_backend_early_size_sections
7003
0
      && !obed->elf_backend_early_size_sections (info))
7004
0
    return false;
7005
7006
0
  dynobj = elf_hash_table (info)->dynobj;
7007
7008
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7009
0
    {
7010
0
      struct bfd_elf_version_tree *verdefs;
7011
0
      struct elf_info_failed asvinfo;
7012
0
      struct bfd_elf_version_tree *t;
7013
0
      struct bfd_elf_version_expr *d;
7014
0
      asection *s;
7015
0
      size_t soname_indx;
7016
7017
      /* If we are supposed to export all symbols into the dynamic symbol
7018
   table (this is not the normal case), then do so.  */
7019
0
      if (info->export_dynamic
7020
0
    || (bfd_link_executable (info) && info->dynamic))
7021
0
  {
7022
0
    struct elf_info_failed eif;
7023
7024
0
    eif.info = info;
7025
0
    eif.failed = false;
7026
0
    elf_link_hash_traverse (elf_hash_table (info),
7027
0
          _bfd_elf_export_symbol,
7028
0
          &eif);
7029
0
    if (eif.failed)
7030
0
      return false;
7031
0
  }
7032
7033
0
      if (soname != NULL)
7034
0
  {
7035
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7036
0
               soname, true);
7037
0
    if (soname_indx == (size_t) -1
7038
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
7039
0
      return false;
7040
0
  }
7041
0
      else
7042
0
  soname_indx = (size_t) -1;
7043
7044
      /* Make all global versions with definition.  */
7045
0
      for (t = info->version_info; t != NULL; t = t->next)
7046
0
  for (d = t->globals.list; d != NULL; d = d->next)
7047
0
    if (!d->symver && d->literal)
7048
0
      {
7049
0
        const char *verstr, *name;
7050
0
        size_t namelen, verlen, newlen;
7051
0
        char *newname, *p, leading_char;
7052
0
        struct elf_link_hash_entry *newh;
7053
7054
0
        leading_char = bfd_get_symbol_leading_char (info->output_bfd);
7055
0
        name = d->pattern;
7056
0
        namelen = strlen (name) + (leading_char != '\0');
7057
0
        verstr = t->name;
7058
0
        verlen = strlen (verstr);
7059
0
        newlen = namelen + verlen + 3;
7060
7061
0
        newname = bfd_malloc (newlen);
7062
0
        if (newname == NULL)
7063
0
    return false;
7064
0
        newname[0] = leading_char;
7065
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
7066
7067
        /* Check the hidden versioned definition.  */
7068
0
        p = newname + namelen;
7069
0
        *p++ = ELF_VER_CHR;
7070
0
        memcpy (p, verstr, verlen + 1);
7071
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
7072
0
             newname, false, false,
7073
0
             false);
7074
0
        if (newh == NULL
7075
0
      || (newh->root.type != bfd_link_hash_defined
7076
0
          && newh->root.type != bfd_link_hash_defweak))
7077
0
    {
7078
      /* Check the default versioned definition.  */
7079
0
      *p++ = ELF_VER_CHR;
7080
0
      memcpy (p, verstr, verlen + 1);
7081
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
7082
0
                 newname, false, false,
7083
0
                 false);
7084
0
    }
7085
0
        free (newname);
7086
7087
        /* Mark this version if there is a definition and it is
7088
     not defined in a shared object.  */
7089
0
        if (newh != NULL
7090
0
      && !newh->def_dynamic
7091
0
      && (newh->root.type == bfd_link_hash_defined
7092
0
          || newh->root.type == bfd_link_hash_defweak))
7093
0
    d->symver = 1;
7094
0
      }
7095
7096
      /* Attach all the symbols to their version information.  */
7097
0
      asvinfo.info = info;
7098
0
      asvinfo.failed = false;
7099
7100
0
      elf_link_hash_traverse (elf_hash_table (info),
7101
0
            _bfd_elf_link_assign_sym_version,
7102
0
            &asvinfo);
7103
0
      if (asvinfo.failed)
7104
0
  return false;
7105
7106
0
      if (!info->allow_undefined_version)
7107
0
  {
7108
    /* Check if all global versions have a definition.  */
7109
0
    bool all_defined = true;
7110
0
    for (t = info->version_info; t != NULL; t = t->next)
7111
0
      for (d = t->globals.list; d != NULL; d = d->next)
7112
0
        if (d->literal && !d->symver && !d->script)
7113
0
    {
7114
0
      _bfd_error_handler
7115
0
        (_("%s: undefined version: %s"),
7116
0
         d->pattern, t->name);
7117
0
      all_defined = false;
7118
0
    }
7119
7120
0
    if (!all_defined)
7121
0
      {
7122
0
        bfd_set_error (bfd_error_bad_value);
7123
0
        return false;
7124
0
      }
7125
0
  }
7126
7127
      /* Set up the version definition section.  */
7128
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7129
0
      BFD_ASSERT (s != NULL);
7130
7131
      /* We may have created additional version definitions if we are
7132
   just linking a regular application.  */
7133
0
      verdefs = info->version_info;
7134
7135
      /* Skip anonymous version tag.  */
7136
0
      if (verdefs != NULL && verdefs->vernum == 0)
7137
0
  verdefs = verdefs->next;
7138
7139
0
      if (verdefs == NULL && !info->create_default_symver)
7140
0
  s->flags |= SEC_EXCLUDE;
7141
0
      else
7142
0
  {
7143
0
    unsigned int cdefs;
7144
0
    bfd_size_type size;
7145
0
    bfd_byte *p;
7146
0
    Elf_Internal_Verdef def;
7147
0
    Elf_Internal_Verdaux defaux;
7148
0
    struct bfd_link_hash_entry *bh;
7149
0
    struct elf_link_hash_entry *h;
7150
0
    const char *name;
7151
7152
0
    cdefs = 0;
7153
0
    size = 0;
7154
7155
    /* Make space for the base version.  */
7156
0
    size += sizeof (Elf_External_Verdef);
7157
0
    size += sizeof (Elf_External_Verdaux);
7158
0
    ++cdefs;
7159
7160
    /* Make space for the default version.  */
7161
0
    if (info->create_default_symver)
7162
0
      {
7163
0
        size += sizeof (Elf_External_Verdef);
7164
0
        ++cdefs;
7165
0
      }
7166
7167
0
    for (t = verdefs; t != NULL; t = t->next)
7168
0
      {
7169
0
        struct bfd_elf_version_deps *n;
7170
7171
        /* Don't emit base version twice.  */
7172
0
        if (t->vernum == 0)
7173
0
    continue;
7174
7175
0
        size += sizeof (Elf_External_Verdef);
7176
0
        size += sizeof (Elf_External_Verdaux);
7177
0
        ++cdefs;
7178
7179
0
        for (n = t->deps; n != NULL; n = n->next)
7180
0
    size += sizeof (Elf_External_Verdaux);
7181
0
      }
7182
7183
0
    s->size = size;
7184
0
    s->contents = bfd_alloc (info->output_bfd, s->size);
7185
0
    if (s->contents == NULL && s->size != 0)
7186
0
      return false;
7187
0
    s->alloced = 1;
7188
7189
    /* Fill in the version definition section.  */
7190
7191
0
    p = s->contents;
7192
7193
0
    def.vd_version = VER_DEF_CURRENT;
7194
0
    def.vd_flags = VER_FLG_BASE;
7195
0
    def.vd_ndx = 1;
7196
0
    def.vd_cnt = 1;
7197
0
    if (info->create_default_symver)
7198
0
      {
7199
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7200
0
        def.vd_next = sizeof (Elf_External_Verdef);
7201
0
      }
7202
0
    else
7203
0
      {
7204
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7205
0
        def.vd_next = (sizeof (Elf_External_Verdef)
7206
0
           + sizeof (Elf_External_Verdaux));
7207
0
      }
7208
7209
0
    if (soname_indx != (size_t) -1)
7210
0
      {
7211
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7212
0
              soname_indx);
7213
0
        def.vd_hash = bfd_elf_hash (soname);
7214
0
        defaux.vda_name = soname_indx;
7215
0
        name = soname;
7216
0
      }
7217
0
    else
7218
0
      {
7219
0
        size_t indx;
7220
7221
0
        name = lbasename (bfd_get_filename (info->output_bfd));
7222
0
        def.vd_hash = bfd_elf_hash (name);
7223
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7224
0
            name, false);
7225
0
        if (indx == (size_t) -1)
7226
0
    return false;
7227
0
        defaux.vda_name = indx;
7228
0
      }
7229
0
    defaux.vda_next = 0;
7230
7231
0
    _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7232
0
            (Elf_External_Verdef *) p);
7233
0
    p += sizeof (Elf_External_Verdef);
7234
0
    if (info->create_default_symver)
7235
0
      {
7236
        /* Add a symbol representing this version.  */
7237
0
        bh = NULL;
7238
0
        if (! (_bfd_generic_link_add_one_symbol
7239
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7240
0
          0, NULL, false,
7241
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7242
0
    return false;
7243
0
        h = (struct elf_link_hash_entry *) bh;
7244
0
        h->non_elf = 0;
7245
0
        h->def_regular = 1;
7246
0
        h->type = STT_OBJECT;
7247
0
        h->verinfo.vertree = NULL;
7248
7249
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7250
0
    return false;
7251
7252
        /* Create a duplicate of the base version with the same
7253
     aux block, but different flags.  */
7254
0
        def.vd_flags = 0;
7255
0
        def.vd_ndx = 2;
7256
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7257
0
        if (verdefs)
7258
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7259
0
             + sizeof (Elf_External_Verdaux));
7260
0
        else
7261
0
    def.vd_next = 0;
7262
0
        _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7263
0
          (Elf_External_Verdef *) p);
7264
0
        p += sizeof (Elf_External_Verdef);
7265
0
      }
7266
0
    _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7267
0
             (Elf_External_Verdaux *) p);
7268
0
    p += sizeof (Elf_External_Verdaux);
7269
7270
0
    for (t = verdefs; t != NULL; t = t->next)
7271
0
      {
7272
0
        unsigned int cdeps;
7273
0
        struct bfd_elf_version_deps *n;
7274
7275
        /* Don't emit the base version twice.  */
7276
0
        if (t->vernum == 0)
7277
0
    continue;
7278
7279
0
        cdeps = 0;
7280
0
        for (n = t->deps; n != NULL; n = n->next)
7281
0
    ++cdeps;
7282
7283
        /* Add a symbol representing this version.  */
7284
0
        bh = NULL;
7285
0
        if (! (_bfd_generic_link_add_one_symbol
7286
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7287
0
          0, NULL, false,
7288
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7289
0
    return false;
7290
0
        h = (struct elf_link_hash_entry *) bh;
7291
0
        h->non_elf = 0;
7292
0
        h->def_regular = 1;
7293
0
        h->type = STT_OBJECT;
7294
0
        h->verinfo.vertree = t;
7295
7296
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7297
0
    return false;
7298
7299
0
        def.vd_version = VER_DEF_CURRENT;
7300
0
        def.vd_flags = 0;
7301
0
        if (t->globals.list == NULL
7302
0
      && t->locals.list == NULL
7303
0
      && ! t->used)
7304
0
    def.vd_flags |= VER_FLG_WEAK;
7305
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7306
0
        def.vd_cnt = cdeps + 1;
7307
0
        def.vd_hash = bfd_elf_hash (t->name);
7308
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7309
0
        def.vd_next = 0;
7310
7311
        /* If a basever node is next, it *must* be the last node in
7312
     the chain, otherwise Verdef construction breaks.  */
7313
0
        if (t->next != NULL && t->next->vernum == 0)
7314
0
    BFD_ASSERT (t->next->next == NULL);
7315
7316
0
        if (t->next != NULL && t->next->vernum != 0)
7317
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7318
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7319
7320
0
        _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7321
0
          (Elf_External_Verdef *) p);
7322
0
        p += sizeof (Elf_External_Verdef);
7323
7324
0
        defaux.vda_name = h->dynstr_index;
7325
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7326
0
              h->dynstr_index);
7327
0
        defaux.vda_next = 0;
7328
0
        if (t->deps != NULL)
7329
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
7330
0
        t->name_indx = defaux.vda_name;
7331
7332
0
        _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7333
0
           (Elf_External_Verdaux *) p);
7334
0
        p += sizeof (Elf_External_Verdaux);
7335
7336
0
        for (n = t->deps; n != NULL; n = n->next)
7337
0
    {
7338
0
      if (n->version_needed == NULL)
7339
0
        {
7340
          /* This can happen if there was an error in the
7341
       version script.  */
7342
0
          defaux.vda_name = 0;
7343
0
        }
7344
0
      else
7345
0
        {
7346
0
          defaux.vda_name = n->version_needed->name_indx;
7347
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7348
0
                defaux.vda_name);
7349
0
        }
7350
0
      if (n->next == NULL)
7351
0
        defaux.vda_next = 0;
7352
0
      else
7353
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7354
7355
0
      _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7356
0
               (Elf_External_Verdaux *) p);
7357
0
      p += sizeof (Elf_External_Verdaux);
7358
0
    }
7359
0
      }
7360
7361
0
    elf_tdata (info->output_bfd)->cverdefs = cdefs;
7362
0
  }
7363
0
    }
7364
7365
0
  if (info->gc_sections && obed->can_gc_sections)
7366
0
    {
7367
0
      struct elf_gc_sweep_symbol_info sweep_info;
7368
7369
      /* Remove the symbols that were in the swept sections from the
7370
   dynamic symbol table.  */
7371
0
      sweep_info.info = info;
7372
0
      sweep_info.hide_symbol = obed->elf_backend_hide_symbol;
7373
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7374
0
            &sweep_info);
7375
0
    }
7376
7377
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7378
0
    {
7379
0
      asection *s;
7380
0
      struct elf_find_verdep_info sinfo;
7381
7382
      /* Work out the size of the version reference section.  */
7383
7384
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7385
0
      BFD_ASSERT (s != NULL);
7386
7387
0
      sinfo.info = info;
7388
0
      sinfo.vers = elf_tdata (info->output_bfd)->cverdefs;
7389
0
      if (sinfo.vers == 0)
7390
0
  sinfo.vers = 1;
7391
0
      sinfo.failed = false;
7392
7393
0
      elf_link_hash_traverse (elf_hash_table (info),
7394
0
            _bfd_elf_link_find_version_dependencies,
7395
0
            &sinfo);
7396
0
      if (sinfo.failed)
7397
0
  return false;
7398
7399
0
      obed->elf_backend_add_glibc_version_dependency (&sinfo);
7400
0
      if (sinfo.failed)
7401
0
  return false;
7402
7403
0
      if (elf_tdata (info->output_bfd)->verref == NULL)
7404
0
  s->flags |= SEC_EXCLUDE;
7405
0
      else
7406
0
  {
7407
0
    Elf_Internal_Verneed *vn;
7408
0
    unsigned int size;
7409
0
    unsigned int crefs;
7410
0
    bfd_byte *p;
7411
7412
    /* Build the version dependency section.  */
7413
0
    size = 0;
7414
0
    crefs = 0;
7415
0
    for (vn = elf_tdata (info->output_bfd)->verref;
7416
0
         vn != NULL;
7417
0
         vn = vn->vn_nextref)
7418
0
      {
7419
0
        Elf_Internal_Vernaux *a;
7420
7421
0
        size += sizeof (Elf_External_Verneed);
7422
0
        ++crefs;
7423
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7424
0
    size += sizeof (Elf_External_Vernaux);
7425
0
      }
7426
7427
0
    s->size = size;
7428
0
    s->contents = bfd_alloc (info->output_bfd, s->size);
7429
0
    if (s->contents == NULL)
7430
0
      return false;
7431
0
    s->alloced = 1;
7432
7433
0
    p = s->contents;
7434
0
    for (vn = elf_tdata (info->output_bfd)->verref;
7435
0
         vn != NULL;
7436
0
         vn = vn->vn_nextref)
7437
0
      {
7438
0
        unsigned int caux;
7439
0
        Elf_Internal_Vernaux *a;
7440
0
        size_t indx;
7441
7442
0
        caux = 0;
7443
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7444
0
    ++caux;
7445
7446
0
        vn->vn_version = VER_NEED_CURRENT;
7447
0
        vn->vn_cnt = caux;
7448
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7449
0
            elf_dt_name (vn->vn_bfd) != NULL
7450
0
            ? elf_dt_name (vn->vn_bfd)
7451
0
            : lbasename (bfd_get_filename
7452
0
                   (vn->vn_bfd)),
7453
0
            false);
7454
0
        if (indx == (size_t) -1)
7455
0
    return false;
7456
0
        vn->vn_file = indx;
7457
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7458
0
        if (vn->vn_nextref == NULL)
7459
0
    vn->vn_next = 0;
7460
0
        else
7461
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7462
0
             + caux * sizeof (Elf_External_Vernaux));
7463
7464
0
        _bfd_elf_swap_verneed_out (info->output_bfd, vn,
7465
0
           (Elf_External_Verneed *) p);
7466
0
        p += sizeof (Elf_External_Verneed);
7467
7468
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7469
0
    {
7470
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7471
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7472
0
                a->vna_nodename, false);
7473
0
      if (indx == (size_t) -1)
7474
0
        return false;
7475
0
      a->vna_name = indx;
7476
0
      if (a->vna_nextptr == NULL)
7477
0
        a->vna_next = 0;
7478
0
      else
7479
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7480
7481
0
      _bfd_elf_swap_vernaux_out (info->output_bfd, a,
7482
0
               (Elf_External_Vernaux *) p);
7483
0
      p += sizeof (Elf_External_Vernaux);
7484
0
    }
7485
0
      }
7486
7487
0
    elf_tdata (info->output_bfd)->cverrefs = crefs;
7488
0
  }
7489
0
    }
7490
7491
0
  if (bfd_link_relocatable (info)
7492
0
      && !bfd_elf_size_group_sections (info))
7493
0
    return false;
7494
7495
  /* Determine any GNU_STACK segment requirements, after the backend
7496
     has had a chance to set a default segment size.  */
7497
0
  if (info->execstack)
7498
0
    {
7499
      /* If the user has explicitly requested warnings, then generate one even
7500
   though the choice is the result of another command line option.  */
7501
0
      if (info->warn_execstack == 1)
7502
0
  {
7503
0
    if (info->error_execstack)
7504
0
      {
7505
0
        _bfd_error_handler
7506
0
    (_("\
7507
0
error: creating an executable stack because of -z execstack command line option"));
7508
0
        return false;
7509
0
      }
7510
7511
0
    _bfd_error_handler
7512
0
      (_("\
7513
0
warning: enabling an executable stack because of -z execstack command line option"));
7514
0
  }
7515
7516
0
      elf_stack_flags (info->output_bfd) = PF_R | PF_W | PF_X;
7517
0
    }
7518
0
  else if (info->noexecstack)
7519
0
    elf_stack_flags (info->output_bfd) = PF_R | PF_W;
7520
0
  else
7521
0
    {
7522
0
      bfd *inputobj;
7523
0
      asection *notesec = NULL;
7524
0
      bfd *noteobj = NULL;
7525
0
      bfd *emptyobj = NULL;
7526
0
      int exec = 0;
7527
7528
0
      for (inputobj = info->input_bfds;
7529
0
     inputobj;
7530
0
     inputobj = inputobj->link.next)
7531
0
  {
7532
0
    asection *s;
7533
7534
0
    if (inputobj->flags
7535
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7536
0
      continue;
7537
0
    s = inputobj->sections;
7538
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7539
0
      continue;
7540
7541
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7542
0
    if (s)
7543
0
      {
7544
0
        notesec = s;
7545
0
        if (s->flags & SEC_CODE)
7546
0
    {
7547
0
      noteobj = inputobj;
7548
0
      exec = PF_X;
7549
      /* There is no point in scanning the remaining bfds.  */
7550
0
      break;
7551
0
    }
7552
0
      }
7553
0
    else if (obed->default_execstack && info->default_execstack)
7554
0
      {
7555
0
        exec = PF_X;
7556
0
        emptyobj = inputobj;
7557
0
      }
7558
0
  }
7559
7560
0
      if (notesec || info->stacksize > 0)
7561
0
  {
7562
0
    if (exec)
7563
0
      {
7564
0
        if (info->warn_execstack != 0)
7565
0
    {
7566
      /* PR 29072: Because an executable stack is a serious
7567
         security risk, make sure that the user knows that it is
7568
         being enabled despite the fact that it was not requested
7569
         on the command line.  */
7570
0
      if (noteobj)
7571
0
        {
7572
0
          if (info->error_execstack)
7573
0
      {
7574
0
        _bfd_error_handler (_("\
7575
0
error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7576
0
                bfd_get_filename (noteobj));
7577
0
        return false;
7578
0
      }
7579
7580
0
          _bfd_error_handler (_("\
7581
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7582
0
           bfd_get_filename (noteobj));
7583
0
        }
7584
0
      else if (emptyobj)
7585
0
        {
7586
0
          if (info->error_execstack)
7587
0
      {
7588
0
        _bfd_error_handler (_("\
7589
0
error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7590
0
                bfd_get_filename (emptyobj));
7591
0
        return false;
7592
0
      }
7593
7594
0
          _bfd_error_handler (_("\
7595
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7596
0
            bfd_get_filename (emptyobj));
7597
0
          _bfd_error_handler (_("\
7598
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7599
0
        }
7600
0
    }
7601
0
      }
7602
0
    elf_stack_flags (info->output_bfd) = PF_R | PF_W | exec;
7603
0
  }
7604
7605
0
      if (notesec && exec && bfd_link_relocatable (info)
7606
0
    && notesec->output_section != bfd_abs_section_ptr)
7607
0
  notesec->output_section->flags |= SEC_CODE;
7608
0
    }
7609
7610
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7611
0
    {
7612
0
      struct elf_info_failed eif;
7613
0
      struct elf_link_hash_entry *h;
7614
0
      asection *dynstr;
7615
0
      asection *s;
7616
7617
0
      *sinterpptr = elf_hash_table (info)->interp;
7618
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7619
7620
0
      if (info->symbolic)
7621
0
  {
7622
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7623
0
      return false;
7624
0
    info->flags |= DF_SYMBOLIC;
7625
0
  }
7626
7627
0
      if (rpath != NULL)
7628
0
  {
7629
0
    size_t indx;
7630
0
    bfd_vma tag;
7631
7632
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7633
0
              true);
7634
0
    if (indx == (size_t) -1)
7635
0
      return false;
7636
7637
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7638
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7639
0
      return false;
7640
0
  }
7641
7642
0
      if (filter_shlib != NULL)
7643
0
  {
7644
0
    size_t indx;
7645
7646
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7647
0
              filter_shlib, true);
7648
0
    if (indx == (size_t) -1
7649
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7650
0
      return false;
7651
0
  }
7652
7653
0
      if (auxiliary_filters != NULL)
7654
0
  {
7655
0
    const char * const *p;
7656
7657
0
    for (p = auxiliary_filters; *p != NULL; p++)
7658
0
      {
7659
0
        size_t indx;
7660
7661
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7662
0
            *p, true);
7663
0
        if (indx == (size_t) -1
7664
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7665
0
    return false;
7666
0
      }
7667
0
  }
7668
7669
0
      if (audit != NULL)
7670
0
  {
7671
0
    size_t indx;
7672
7673
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7674
0
              true);
7675
0
    if (indx == (size_t) -1
7676
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7677
0
      return false;
7678
0
  }
7679
7680
0
      if (depaudit != NULL)
7681
0
  {
7682
0
    size_t indx;
7683
7684
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7685
0
              true);
7686
0
    if (indx == (size_t) -1
7687
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7688
0
      return false;
7689
0
  }
7690
7691
0
      eif.info = info;
7692
0
      eif.failed = false;
7693
7694
      /* Find all symbols which were defined in a dynamic object and make
7695
   the backend pick a reasonable value for them.  */
7696
0
      elf_link_hash_traverse (elf_hash_table (info),
7697
0
            _bfd_elf_adjust_dynamic_symbol,
7698
0
            &eif);
7699
0
      if (eif.failed)
7700
0
  return false;
7701
7702
      /* Add some entries to the .dynamic section.  We fill in some of the
7703
   values later, in bfd_elf_final_link, but we must add the entries
7704
   now so that we know the final size of the .dynamic section.  */
7705
7706
      /* If there are initialization and/or finalization functions to
7707
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7708
0
      h = (info->init_function
7709
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7710
0
           info->init_function, false,
7711
0
           false, false)
7712
0
     : NULL);
7713
0
      if (h != NULL
7714
0
    && (h->ref_regular
7715
0
        || h->def_regular))
7716
0
  {
7717
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7718
0
      return false;
7719
0
  }
7720
0
      h = (info->fini_function
7721
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7722
0
           info->fini_function, false,
7723
0
           false, false)
7724
0
     : NULL);
7725
0
      if (h != NULL
7726
0
    && (h->ref_regular
7727
0
        || h->def_regular))
7728
0
  {
7729
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7730
0
      return false;
7731
0
  }
7732
7733
0
      s = bfd_get_section_by_name (info->output_bfd, ".preinit_array");
7734
0
      if (s != NULL && s->linker_has_input)
7735
0
  {
7736
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7737
0
    if (! bfd_link_executable (info))
7738
0
      {
7739
0
        bfd *sub;
7740
0
        asection *o;
7741
7742
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7743
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7744
0
        && (o = sub->sections) != NULL
7745
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7746
0
      for (o = sub->sections; o != NULL; o = o->next)
7747
0
        if (elf_section_data (o)->this_hdr.sh_type
7748
0
      == SHT_PREINIT_ARRAY)
7749
0
          {
7750
0
      _bfd_error_handler
7751
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7752
0
         sub);
7753
0
      break;
7754
0
          }
7755
7756
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7757
0
        return false;
7758
0
      }
7759
7760
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7761
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7762
0
      return false;
7763
0
  }
7764
0
      s = bfd_get_section_by_name (info->output_bfd, ".init_array");
7765
0
      if (s != NULL && s->linker_has_input)
7766
0
  {
7767
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7768
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7769
0
      return false;
7770
0
  }
7771
0
      s = bfd_get_section_by_name (info->output_bfd, ".fini_array");
7772
0
      if (s != NULL && s->linker_has_input)
7773
0
  {
7774
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7775
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7776
0
      return false;
7777
0
  }
7778
7779
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7780
      /* If .dynstr is excluded from the link, we don't want any of
7781
   these tags.  Strictly, we should be checking each section
7782
   individually;  This quick check covers for the case where
7783
   someone does a /DISCARD/ : { *(*) }.  */
7784
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7785
0
  {
7786
0
    bfd_size_type strsize;
7787
7788
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7789
0
    if ((info->emit_hash
7790
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7791
0
        || (info->emit_gnu_hash
7792
0
      && (obed->record_xhash_symbol == NULL
7793
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7794
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7795
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7796
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7797
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7798
0
                obed->s->sizeof_sym)
7799
0
        || (info->gnu_flags_1
7800
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7801
0
              info->gnu_flags_1)))
7802
0
      return false;
7803
0
  }
7804
0
    }
7805
7806
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7807
0
    return false;
7808
7809
  /* The backend must work out the sizes of all the other dynamic
7810
     sections.  */
7811
0
  if (obed->elf_backend_late_size_sections != NULL
7812
0
      && !obed->elf_backend_late_size_sections (info))
7813
0
    return false;
7814
7815
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7816
0
    {
7817
0
      if (elf_tdata (info->output_bfd)->cverdefs)
7818
0
  {
7819
0
    unsigned int crefs = elf_tdata (info->output_bfd)->cverdefs;
7820
7821
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7822
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7823
0
      return false;
7824
0
  }
7825
7826
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7827
0
  {
7828
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7829
0
      return false;
7830
0
  }
7831
0
      else if (info->flags & DF_BIND_NOW)
7832
0
  {
7833
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7834
0
      return false;
7835
0
  }
7836
7837
0
      if (info->flags_1)
7838
0
  {
7839
0
    if (bfd_link_executable (info))
7840
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7841
0
        | DF_1_NODELETE
7842
0
        | DF_1_NOOPEN);
7843
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7844
0
      return false;
7845
0
  }
7846
7847
0
      if (elf_tdata (info->output_bfd)->cverrefs)
7848
0
  {
7849
0
    unsigned int crefs = elf_tdata (info->output_bfd)->cverrefs;
7850
7851
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7852
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7853
0
      return false;
7854
0
  }
7855
7856
0
      if ((elf_tdata (info->output_bfd)->cverrefs == 0
7857
0
     && elf_tdata (info->output_bfd)->cverdefs == 0)
7858
0
    || _bfd_elf_link_renumber_dynsyms (info, NULL) <= 1)
7859
0
  {
7860
0
    asection *s;
7861
7862
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7863
0
    s->flags |= SEC_EXCLUDE;
7864
0
  }
7865
0
    }
7866
0
  return true;
7867
0
}
7868
7869
/* Find the first non-excluded output section.  We'll use its
7870
   section symbol for some emitted relocs.  */
7871
void
7872
_bfd_elf_init_1_index_section (struct bfd_link_info *info)
7873
0
{
7874
0
  asection *s;
7875
0
  asection *found = NULL;
7876
7877
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7878
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7879
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7880
0
      {
7881
0
  found = s;
7882
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7883
0
    break;
7884
0
      }
7885
0
  elf_hash_table (info)->text_index_section = found;
7886
0
}
7887
7888
/* Find two non-excluded output sections, one for code, one for data.
7889
   We'll use their section symbols for some emitted relocs.  */
7890
void
7891
_bfd_elf_init_2_index_sections (struct bfd_link_info *info)
7892
0
{
7893
0
  asection *s;
7894
0
  asection *found = NULL;
7895
7896
  /* Data first, since setting text_index_section changes
7897
     _bfd_elf_omit_section_dynsym_default.  */
7898
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7899
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7900
0
  && !(s->flags & SEC_READONLY)
7901
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7902
0
      {
7903
0
  found = s;
7904
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7905
0
    break;
7906
0
      }
7907
0
  elf_hash_table (info)->data_index_section = found;
7908
7909
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7910
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7911
0
  && (s->flags & SEC_READONLY)
7912
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7913
0
      {
7914
0
  found = s;
7915
0
  break;
7916
0
      }
7917
0
  elf_hash_table (info)->text_index_section = found;
7918
0
}
7919
7920
void
7921
_bfd_elf_init_0_index_sections (struct bfd_link_info *info ATTRIBUTE_UNUSED)
7922
0
{
7923
0
}
7924
7925
#define GNU_HASH_SECTION_NAME(bed)          \
7926
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7927
7928
bool
7929
bfd_elf_size_dynsym_hash_dynstr (struct bfd_link_info *info)
7930
0
{
7931
0
  elf_backend_data *obed;
7932
0
  unsigned long section_sym_count;
7933
0
  bfd_size_type dynsymcount = 0;
7934
7935
0
  if (!is_elf_hash_table (info->hash))
7936
0
    return true;
7937
7938
0
  obed = get_elf_backend_data (info->output_bfd);
7939
0
  obed->elf_backend_init_index_section (info);
7940
7941
  /* Assign dynsym indices.  In a shared library we generate a section
7942
     symbol for each output section, which come first.  Next come all
7943
     of the back-end allocated local dynamic syms, followed by the rest
7944
     of the global symbols.
7945
7946
     This is usually not needed for static binaries, however backends
7947
     can request to always do it, e.g. the MIPS backend uses dynamic
7948
     symbol counts to lay out GOT, which will be produced in the
7949
     presence of GOT relocations even in static binaries (holding fixed
7950
     data in that case, to satisfy those relocations).  */
7951
7952
0
  if (elf_hash_table (info)->dynamic_sections_created
7953
0
      || obed->always_renumber_dynsyms)
7954
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (info, &section_sym_count);
7955
7956
0
  if (elf_hash_table (info)->dynamic_sections_created)
7957
0
    {
7958
0
      bfd *dynobj;
7959
0
      asection *s;
7960
0
      unsigned int dtagcount;
7961
7962
0
      dynobj = elf_hash_table (info)->dynobj;
7963
7964
      /* Work out the size of the symbol version section.  */
7965
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7966
0
      BFD_ASSERT (s != NULL);
7967
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7968
0
  {
7969
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7970
0
    s->contents = bfd_zalloc (info->output_bfd, s->size);
7971
0
    if (s->contents == NULL)
7972
0
      return false;
7973
0
    s->alloced = 1;
7974
7975
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7976
0
      return false;
7977
0
  }
7978
7979
      /* Set the size of the .dynsym and .hash sections.  We counted
7980
   the number of dynamic symbols in elf_link_add_object_symbols.
7981
   We will build the contents of .dynsym and .hash when we build
7982
   the final symbol table, because until then we do not know the
7983
   correct value to give the symbols.  We built the .dynstr
7984
   section as we went along in elf_link_add_object_symbols.  */
7985
0
      s = elf_hash_table (info)->dynsym;
7986
0
      BFD_ASSERT (s != NULL);
7987
0
      s->size = dynsymcount * obed->s->sizeof_sym;
7988
7989
0
      s->contents = bfd_alloc (info->output_bfd, s->size);
7990
0
      if (s->contents == NULL)
7991
0
  return false;
7992
0
      s->alloced = 1;
7993
7994
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7995
   section syms, in case we don't output them all.  */
7996
0
      ++section_sym_count;
7997
0
      memset (s->contents, 0, section_sym_count * obed->s->sizeof_sym);
7998
7999
0
      elf_hash_table (info)->bucketcount = 0;
8000
8001
      /* Compute the size of the hashing table.  As a side effect this
8002
   computes the hash values for all the names we export.  */
8003
0
      if (info->emit_hash)
8004
0
  {
8005
0
    unsigned long int *hashcodes;
8006
0
    struct hash_codes_info hashinf;
8007
0
    unsigned long int nsyms;
8008
0
    size_t bucketcount;
8009
0
    size_t hash_entry_size;
8010
8011
    /* Compute the hash values for all exported symbols.  At the same
8012
       time store the values in an array so that we could use them for
8013
       optimizations.  */
8014
0
    hashcodes = bfd_malloc (dynsymcount * sizeof (*hashcodes));
8015
0
    if (hashcodes == NULL)
8016
0
      return false;
8017
0
    hashinf.hashcodes = hashcodes;
8018
0
    hashinf.error = false;
8019
8020
    /* Put all hash values in HASHCODES.  */
8021
0
    elf_link_hash_traverse (elf_hash_table (info),
8022
0
          elf_collect_hash_codes, &hashinf);
8023
0
    if (hashinf.error)
8024
0
      {
8025
0
        free (hashcodes);
8026
0
        return false;
8027
0
      }
8028
8029
0
    nsyms = hashinf.hashcodes - hashcodes;
8030
0
    bucketcount
8031
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
8032
0
    free (hashcodes);
8033
8034
0
    if (bucketcount == 0 && nsyms > 0)
8035
0
      return false;
8036
8037
0
    elf_hash_table (info)->bucketcount = bucketcount;
8038
8039
0
    s = bfd_get_linker_section (dynobj, ".hash");
8040
0
    BFD_ASSERT (s != NULL);
8041
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
8042
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
8043
0
    s->contents = bfd_zalloc (info->output_bfd, s->size);
8044
0
    if (s->contents == NULL)
8045
0
      return false;
8046
0
    s->alloced = 1;
8047
8048
0
    bfd_put (8 * hash_entry_size, info->output_bfd, bucketcount,
8049
0
       s->contents);
8050
0
    bfd_put (8 * hash_entry_size, info->output_bfd, dynsymcount,
8051
0
       s->contents + hash_entry_size);
8052
0
  }
8053
8054
0
      if (info->emit_gnu_hash)
8055
0
  {
8056
0
    size_t i, cnt;
8057
0
    unsigned char *contents;
8058
0
    struct collect_gnu_hash_codes cinfo;
8059
0
    bfd_size_type amt;
8060
0
    size_t bucketcount;
8061
8062
0
    memset (&cinfo, 0, sizeof (cinfo));
8063
8064
    /* Compute the hash values for all exported symbols.  At the same
8065
       time store the values in an array so that we could use them for
8066
       optimizations.  */
8067
0
    amt = dynsymcount * 2 * sizeof (*cinfo.hashcodes);
8068
0
    cinfo.hashcodes = bfd_malloc (amt);
8069
0
    if (cinfo.hashcodes == NULL)
8070
0
      return false;
8071
8072
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
8073
0
    cinfo.min_dynindx = -1;
8074
0
    cinfo.output_bfd = info->output_bfd;
8075
0
    cinfo.bed = obed;
8076
8077
    /* Put all hash values in HASHCODES.  */
8078
0
    elf_link_hash_traverse (elf_hash_table (info),
8079
0
          elf_collect_gnu_hash_codes, &cinfo);
8080
0
    if (cinfo.error)
8081
0
      {
8082
0
        free (cinfo.hashcodes);
8083
0
        return false;
8084
0
      }
8085
8086
0
    bucketcount
8087
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
8088
8089
0
    if (bucketcount == 0)
8090
0
      {
8091
0
        free (cinfo.hashcodes);
8092
0
        return false;
8093
0
      }
8094
8095
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (obed));
8096
0
    BFD_ASSERT (s != NULL);
8097
8098
0
    if (cinfo.nsyms == 0)
8099
0
      {
8100
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
8101
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
8102
0
        free (cinfo.hashcodes);
8103
0
        s->size = 5 * 4 + obed->s->arch_size / 8;
8104
0
        contents = bfd_zalloc (info->output_bfd, s->size);
8105
0
        if (contents == NULL)
8106
0
    return false;
8107
0
        s->contents = contents;
8108
0
        s->alloced = 1;
8109
        /* 1 empty bucket.  */
8110
0
        bfd_put_32 (info->output_bfd, 1, contents);
8111
        /* SYMIDX above the special symbol 0.  */
8112
0
        bfd_put_32 (info->output_bfd, 1, contents + 4);
8113
        /* Just one word for bitmask.  */
8114
0
        bfd_put_32 (info->output_bfd, 1, contents + 8);
8115
        /* Only hash fn bloom filter.  */
8116
0
        bfd_put_32 (info->output_bfd, 0, contents + 12);
8117
        /* No hashes are valid - empty bitmask.  */
8118
0
        bfd_put (obed->s->arch_size, info->output_bfd, 0, contents + 16);
8119
        /* No hashes in the only bucket.  */
8120
0
        bfd_put_32 (info->output_bfd, 0,
8121
0
        contents + 16 + obed->s->arch_size / 8);
8122
0
      }
8123
0
    else
8124
0
      {
8125
0
        unsigned long int maskwords, maskbitslog2, x;
8126
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
8127
8128
0
        x = cinfo.nsyms;
8129
0
        maskbitslog2 = 1;
8130
0
        while ((x >>= 1) != 0)
8131
0
    ++maskbitslog2;
8132
0
        if (maskbitslog2 < 3)
8133
0
    maskbitslog2 = 5;
8134
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8135
0
    maskbitslog2 = maskbitslog2 + 3;
8136
0
        else
8137
0
    maskbitslog2 = maskbitslog2 + 2;
8138
0
        if (obed->s->arch_size == 64)
8139
0
    {
8140
0
      if (maskbitslog2 == 5)
8141
0
        maskbitslog2 = 6;
8142
0
      cinfo.shift1 = 6;
8143
0
    }
8144
0
        else
8145
0
    cinfo.shift1 = 5;
8146
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
8147
0
        cinfo.shift2 = maskbitslog2;
8148
0
        cinfo.maskbits = 1 << maskbitslog2;
8149
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8150
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
8151
0
        amt += maskwords * sizeof (bfd_vma);
8152
0
        cinfo.bitmask = bfd_malloc (amt);
8153
0
        if (cinfo.bitmask == NULL)
8154
0
    {
8155
0
      free (cinfo.hashcodes);
8156
0
      return false;
8157
0
    }
8158
8159
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8160
0
        cinfo.indx = cinfo.counts + bucketcount;
8161
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
8162
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8163
8164
        /* Determine how often each hash bucket is used.  */
8165
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8166
0
        for (i = 0; i < cinfo.nsyms; ++i)
8167
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8168
8169
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8170
0
    if (cinfo.counts[i] != 0)
8171
0
      {
8172
0
        cinfo.indx[i] = cnt;
8173
0
        cnt += cinfo.counts[i];
8174
0
      }
8175
0
        BFD_ASSERT (cnt == dynsymcount);
8176
0
        cinfo.bucketcount = bucketcount;
8177
0
        cinfo.local_indx = cinfo.min_dynindx;
8178
8179
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8180
0
        s->size += cinfo.maskbits / 8;
8181
0
        if (obed->record_xhash_symbol != NULL)
8182
0
    s->size += cinfo.nsyms * 4;
8183
0
        contents = bfd_zalloc (info->output_bfd, s->size);
8184
0
        if (contents == NULL)
8185
0
    {
8186
0
      free (cinfo.bitmask);
8187
0
      free (cinfo.hashcodes);
8188
0
      return false;
8189
0
    }
8190
8191
0
        s->contents = contents;
8192
0
        s->alloced = 1;
8193
0
        bfd_put_32 (info->output_bfd, bucketcount, contents);
8194
0
        bfd_put_32 (info->output_bfd, cinfo.symindx, contents + 4);
8195
0
        bfd_put_32 (info->output_bfd, maskwords, contents + 8);
8196
0
        bfd_put_32 (info->output_bfd, cinfo.shift2, contents + 12);
8197
0
        contents += 16 + cinfo.maskbits / 8;
8198
8199
0
        for (i = 0; i < bucketcount; ++i)
8200
0
    {
8201
0
      if (cinfo.counts[i] == 0)
8202
0
        bfd_put_32 (info->output_bfd, 0, contents);
8203
0
      else
8204
0
        bfd_put_32 (info->output_bfd, cinfo.indx[i], contents);
8205
0
      contents += 4;
8206
0
    }
8207
8208
0
        cinfo.contents = contents;
8209
8210
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8211
8212
0
        if (elf_hash_table (info)->has_base_symbols)
8213
0
    {
8214
      /* Output base symbols first in DT_GNU_HASH so that
8215
         they will be picked before non-base symbols at
8216
         run-time.  */
8217
0
      cinfo.base_symbol = true;
8218
8219
      /* Renumber dynamic symbols, if populating .gnu.hash
8220
         section.  If using .MIPS.xhash, populate the
8221
         translation table.  */
8222
0
      elf_link_hash_traverse (elf_hash_table (info),
8223
0
            elf_gnu_hash_process_symidx, &cinfo);
8224
0
    }
8225
8226
        /* Output non-base symbols last.  */
8227
0
        cinfo.base_symbol = false;
8228
0
        elf_link_hash_traverse (elf_hash_table (info),
8229
0
              elf_gnu_hash_process_symidx, &cinfo);
8230
8231
0
        contents = s->contents + 16;
8232
0
        for (i = 0; i < maskwords; ++i)
8233
0
    {
8234
0
      bfd_put (obed->s->arch_size, info->output_bfd,
8235
0
         cinfo.bitmask[i], contents);
8236
0
      contents += obed->s->arch_size / 8;
8237
0
    }
8238
8239
0
        free (cinfo.bitmask);
8240
0
        free (cinfo.hashcodes);
8241
0
      }
8242
0
  }
8243
8244
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
8245
0
      BFD_ASSERT (s != NULL);
8246
8247
0
      elf_finalize_dynstr (info->output_bfd, info);
8248
8249
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8250
8251
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8252
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8253
0
    return false;
8254
0
    }
8255
8256
0
  return true;
8257
0
}
8258

8259
/* Create an entry in an ELF linker hash table.  */
8260
8261
struct bfd_hash_entry *
8262
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8263
          struct bfd_hash_table *table,
8264
          const char *string)
8265
0
{
8266
  /* Allocate the structure if it has not already been allocated by a
8267
     subclass.  */
8268
0
  if (entry == NULL)
8269
0
    {
8270
0
      entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8271
0
      if (entry == NULL)
8272
0
  return entry;
8273
0
    }
8274
8275
  /* Call the allocation method of the superclass.  */
8276
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
8277
0
  if (entry != NULL)
8278
0
    {
8279
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8280
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8281
8282
      /* Set local fields.  */
8283
0
      ret->indx = -1;
8284
0
      ret->dynindx = -1;
8285
0
      ret->got = htab->init_got_refcount;
8286
0
      ret->plt = htab->init_plt_refcount;
8287
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8288
0
            - offsetof (struct elf_link_hash_entry, size)));
8289
      /* Assume that we have been called by a non-ELF symbol reader.
8290
   This flag is then reset by the code which reads an ELF input
8291
   file.  This ensures that a symbol created by a non-ELF symbol
8292
   reader will have the flag set correctly.  */
8293
0
      ret->non_elf = 1;
8294
0
    }
8295
8296
0
  return entry;
8297
0
}
8298
8299
/* Copy data from an indirect symbol to its direct symbol, hiding the
8300
   old indirect symbol.  Also used for copying flags to a weakdef.  */
8301
8302
void
8303
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8304
          struct elf_link_hash_entry *dir,
8305
          struct elf_link_hash_entry *ind)
8306
0
{
8307
0
  struct elf_link_hash_table *htab;
8308
8309
0
  if (ind->dyn_relocs != NULL)
8310
0
    {
8311
0
      if (dir->dyn_relocs != NULL)
8312
0
  {
8313
0
    struct elf_dyn_relocs **pp;
8314
0
    struct elf_dyn_relocs *p;
8315
8316
    /* Add reloc counts against the indirect sym to the direct sym
8317
       list.  Merge any entries against the same section.  */
8318
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8319
0
      {
8320
0
        struct elf_dyn_relocs *q;
8321
8322
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
8323
0
    if (q->sec == p->sec)
8324
0
      {
8325
0
        q->pc_count += p->pc_count;
8326
0
        q->count += p->count;
8327
0
        *pp = p->next;
8328
0
        break;
8329
0
      }
8330
0
        if (q == NULL)
8331
0
    pp = &p->next;
8332
0
      }
8333
0
    *pp = dir->dyn_relocs;
8334
0
  }
8335
8336
0
      dir->dyn_relocs = ind->dyn_relocs;
8337
0
      ind->dyn_relocs = NULL;
8338
0
    }
8339
8340
  /* Copy down any references that we may have already seen to the
8341
     symbol which just became indirect.  */
8342
8343
0
  if (dir->versioned != versioned_hidden)
8344
0
    dir->ref_dynamic |= ind->ref_dynamic;
8345
0
  dir->ref_regular |= ind->ref_regular;
8346
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8347
0
  dir->non_got_ref |= ind->non_got_ref;
8348
0
  dir->needs_plt |= ind->needs_plt;
8349
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
8350
8351
0
  if (ind->root.type != bfd_link_hash_indirect)
8352
0
    return;
8353
8354
  /* Copy over the global and procedure linkage table refcount entries.
8355
     These may have been already set up by a check_relocs routine.  */
8356
0
  htab = elf_hash_table (info);
8357
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8358
0
    {
8359
0
      if (dir->got.refcount < 0)
8360
0
  dir->got.refcount = 0;
8361
0
      dir->got.refcount += ind->got.refcount;
8362
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8363
0
    }
8364
8365
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8366
0
    {
8367
0
      if (dir->plt.refcount < 0)
8368
0
  dir->plt.refcount = 0;
8369
0
      dir->plt.refcount += ind->plt.refcount;
8370
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8371
0
    }
8372
8373
0
  if (ind->dynindx != -1)
8374
0
    {
8375
0
      if (dir->dynindx != -1)
8376
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8377
0
      dir->dynindx = ind->dynindx;
8378
0
      dir->dynstr_index = ind->dynstr_index;
8379
0
      ind->dynindx = -1;
8380
0
      ind->dynstr_index = 0;
8381
0
    }
8382
0
}
8383
8384
void
8385
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8386
        struct elf_link_hash_entry *h,
8387
        bool force_local)
8388
0
{
8389
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8390
0
  if (h->type != STT_GNU_IFUNC)
8391
0
    {
8392
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8393
0
      h->needs_plt = 0;
8394
0
    }
8395
0
  if (force_local)
8396
0
    {
8397
0
      h->forced_local = 1;
8398
0
      if (h->dynindx != -1)
8399
0
  {
8400
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8401
0
          h->dynstr_index);
8402
0
    h->dynindx = -1;
8403
0
    h->dynstr_index = 0;
8404
0
  }
8405
0
    }
8406
0
}
8407
8408
/* Hide a symbol. */
8409
8410
void
8411
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8412
         struct bfd_link_info *info,
8413
         struct bfd_link_hash_entry *h)
8414
0
{
8415
0
  if (is_elf_hash_table (info->hash))
8416
0
    {
8417
0
      elf_backend_data *obed = get_elf_backend_data (output_bfd);
8418
0
      struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
8419
0
      eh->def_dynamic = 0;
8420
0
      eh->ref_dynamic = 0;
8421
0
      eh->dynamic_def = 0;
8422
0
      obed->elf_backend_hide_symbol (info, eh, true);
8423
0
    }
8424
0
}
8425
8426
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8427
   caller.  */
8428
8429
bool
8430
_bfd_elf_link_hash_table_init
8431
  (struct elf_link_hash_table *table,
8432
   bfd *obfd,
8433
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8434
              struct bfd_hash_table *,
8435
              const char *),
8436
   unsigned int entsize)
8437
0
{
8438
0
  bool ret;
8439
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
8440
0
  int can_refcount = obed->can_refcount;
8441
8442
0
  table->init_got_refcount.refcount = can_refcount - 1;
8443
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8444
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8445
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8446
  /* The first dynamic symbol is a dummy.  */
8447
0
  table->dynsymcount = 1;
8448
8449
0
  ret = _bfd_link_hash_table_init (&table->root, obfd, newfunc, entsize);
8450
8451
0
  table->root.type = bfd_link_elf_hash_table;
8452
0
  table->hash_table_id = obed->target_id;
8453
0
  table->target_os = obed->target_os;
8454
0
  table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8455
8456
0
  return ret;
8457
0
}
8458
8459
/* Create an ELF linker hash table.  */
8460
8461
struct bfd_link_hash_table *
8462
_bfd_elf_link_hash_table_create (bfd *obfd)
8463
0
{
8464
0
  struct elf_link_hash_table *ret;
8465
8466
0
  ret = bfd_zmalloc (sizeof (*ret));
8467
0
  if (ret == NULL)
8468
0
    return NULL;
8469
8470
0
  if (! _bfd_elf_link_hash_table_init (ret, obfd, _bfd_elf_link_hash_newfunc,
8471
0
               sizeof (struct elf_link_hash_entry)))
8472
0
    {
8473
0
      free (ret);
8474
0
      return NULL;
8475
0
    }
8476
8477
0
  return &ret->root;
8478
0
}
8479
8480
/* Destroy an ELF linker hash table.  */
8481
8482
void
8483
_bfd_elf_link_hash_table_free (bfd *obfd)
8484
0
{
8485
0
  struct elf_link_hash_table *htab;
8486
8487
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8488
0
  if (htab->dynstr != NULL)
8489
0
    _bfd_elf_strtab_free (htab->dynstr);
8490
  /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
8491
0
  if (htab->dynamic != NULL)
8492
0
    {
8493
0
      free (htab->dynamic->contents);
8494
0
      htab->dynamic->contents = NULL;
8495
0
    }
8496
0
  if (htab->first_hash != NULL)
8497
0
    {
8498
0
      bfd_hash_table_free (htab->first_hash);
8499
0
      free (htab->first_hash);
8500
0
    }
8501
0
  if (htab->eh_info.frame_hdr_is_compact)
8502
0
    free (htab->eh_info.u.compact.entries);
8503
0
  else
8504
0
    free (htab->eh_info.u.dwarf.array);
8505
0
  sframe_encoder_free (&htab->sfe_info.sfe_ctx);
8506
0
  _bfd_generic_link_hash_table_free (obfd);
8507
0
}
8508
8509
/* This is a hook for the ELF emulation code in the generic linker to
8510
   tell the backend linker what file name to use for the DT_NEEDED
8511
   entry for a dynamic object.  */
8512
8513
void
8514
bfd_elf_set_dt_needed_name (bfd *obfd, const char *name)
8515
0
{
8516
0
  if (bfd_get_flavour (obfd) == bfd_target_elf_flavour
8517
0
      && bfd_get_format (obfd) == bfd_object)
8518
0
    elf_dt_name (obfd) = name;
8519
0
}
8520
8521
int
8522
bfd_elf_get_dyn_lib_class (bfd *abfd)
8523
0
{
8524
0
  int lib_class;
8525
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8526
0
      && bfd_get_format (abfd) == bfd_object)
8527
0
    lib_class = elf_dyn_lib_class (abfd);
8528
0
  else
8529
0
    lib_class = 0;
8530
0
  return lib_class;
8531
0
}
8532
8533
void
8534
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8535
0
{
8536
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8537
0
      && bfd_get_format (abfd) == bfd_object)
8538
0
    elf_dyn_lib_class (abfd) = lib_class;
8539
0
}
8540
8541
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8542
   the linker ELF emulation code.  */
8543
8544
struct bfd_link_needed_list *
8545
bfd_elf_get_needed_list (struct bfd_link_info *info)
8546
0
{
8547
0
  if (! is_elf_hash_table (info->hash))
8548
0
    return NULL;
8549
0
  return elf_hash_table (info)->needed;
8550
0
}
8551
8552
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8553
   hook for the linker ELF emulation code.  */
8554
8555
struct bfd_link_needed_list *
8556
bfd_elf_get_runpath_list (struct bfd_link_info *info)
8557
0
{
8558
0
  if (! is_elf_hash_table (info->hash))
8559
0
    return NULL;
8560
0
  return elf_hash_table (info)->runpath;
8561
0
}
8562
8563
/* Get the name actually used for a dynamic object for a link.  This
8564
   is the SONAME entry if there is one.  Otherwise, it is the string
8565
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8566
8567
const char *
8568
bfd_elf_get_dt_soname (bfd *abfd)
8569
0
{
8570
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8571
0
      && bfd_get_format (abfd) == bfd_object)
8572
0
    return elf_dt_name (abfd);
8573
0
  return NULL;
8574
0
}
8575
8576
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8577
   the ELF linker emulation code.  */
8578
8579
bool
8580
bfd_elf_get_bfd_needed_list (bfd *abfd,
8581
           struct bfd_link_needed_list **pneeded)
8582
0
{
8583
0
  asection *s;
8584
0
  bfd_byte *dynbuf = NULL;
8585
0
  unsigned int elfsec;
8586
0
  unsigned long shlink;
8587
0
  bfd_byte *extdyn, *extdynend;
8588
0
  size_t extdynsize;
8589
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8590
8591
0
  *pneeded = NULL;
8592
8593
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8594
0
      || bfd_get_format (abfd) != bfd_object)
8595
0
    return true;
8596
8597
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8598
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8599
0
    return true;
8600
8601
0
  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8602
0
    goto error_return;
8603
8604
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8605
0
  if (elfsec == SHN_BAD)
8606
0
    goto error_return;
8607
8608
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8609
8610
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8611
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8612
8613
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8614
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8615
0
       extdyn += extdynsize)
8616
0
    {
8617
0
      Elf_Internal_Dyn dyn;
8618
8619
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8620
8621
0
      if (dyn.d_tag == DT_NULL)
8622
0
  break;
8623
8624
0
      if (dyn.d_tag == DT_NEEDED)
8625
0
  {
8626
0
    const char *string;
8627
0
    struct bfd_link_needed_list *l;
8628
0
    unsigned int tagv = dyn.d_un.d_val;
8629
8630
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8631
0
    if (string == NULL)
8632
0
      goto error_return;
8633
8634
0
    l = bfd_alloc (abfd, sizeof (*l));
8635
0
    if (l == NULL)
8636
0
      goto error_return;
8637
8638
0
    l->by = abfd;
8639
0
    l->name = string;
8640
0
    l->next = *pneeded;
8641
0
    *pneeded = l;
8642
0
  }
8643
0
    }
8644
8645
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8646
8647
0
  return true;
8648
8649
0
 error_return:
8650
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8651
0
  return false;
8652
0
}
8653
8654
struct elf_symbuf_symbol
8655
{
8656
  unsigned long st_name;  /* Symbol name, index in string tbl */
8657
  unsigned char st_info;  /* Type and binding attributes */
8658
  unsigned char st_other; /* Visibilty, and target specific */
8659
};
8660
8661
struct elf_symbuf_head
8662
{
8663
  struct elf_symbuf_symbol *ssym;
8664
  size_t count;
8665
  unsigned int st_shndx;
8666
};
8667
8668
struct elf_symbol
8669
{
8670
  union
8671
    {
8672
      Elf_Internal_Sym *isym;
8673
      struct elf_symbuf_symbol *ssym;
8674
      void *p;
8675
    } u;
8676
  const char *name;
8677
};
8678
8679
/* Sort references to symbols by ascending section number.  */
8680
8681
static int
8682
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8683
0
{
8684
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8685
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8686
8687
0
  if (s1->st_shndx != s2->st_shndx)
8688
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8689
  /* Final sort by the address of the sym in the symbuf ensures
8690
     a stable sort.  */
8691
0
  if (s1 != s2)
8692
0
    return s1 > s2 ? 1 : -1;
8693
0
  return 0;
8694
0
}
8695
8696
static int
8697
elf_sym_name_compare (const void *arg1, const void *arg2)
8698
0
{
8699
0
  const struct elf_symbol *s1 = arg1;
8700
0
  const struct elf_symbol *s2 = arg2;
8701
0
  int ret = strcmp (s1->name, s2->name);
8702
0
  if (ret != 0)
8703
0
    return ret;
8704
0
  if (s1->u.p != s2->u.p)
8705
0
    return s1->u.p > s2->u.p ? 1 : -1;
8706
0
  return 0;
8707
0
}
8708
8709
static struct elf_symbuf_head *
8710
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8711
0
{
8712
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8713
0
  struct elf_symbuf_symbol *ssym;
8714
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8715
0
  size_t i, shndx_count, total_size;
8716
8717
0
  indbuf = bfd_malloc (symcount * sizeof (*indbuf));
8718
0
  if (indbuf == NULL)
8719
0
    return NULL;
8720
8721
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8722
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8723
0
      *ind++ = &isymbuf[i];
8724
0
  indbufend = ind;
8725
8726
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8727
0
   elf_sort_elf_symbol);
8728
8729
0
  shndx_count = 0;
8730
0
  if (indbufend > indbuf)
8731
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8732
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8733
0
  shndx_count++;
8734
8735
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8736
0
    + (indbufend - indbuf) * sizeof (*ssym));
8737
0
  ssymbuf = bfd_malloc (total_size);
8738
0
  if (ssymbuf == NULL)
8739
0
    {
8740
0
      free (indbuf);
8741
0
      return NULL;
8742
0
    }
8743
8744
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8745
0
  ssymbuf->ssym = NULL;
8746
0
  ssymbuf->count = shndx_count;
8747
0
  ssymbuf->st_shndx = 0;
8748
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8749
0
    {
8750
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8751
0
  {
8752
0
    ssymhead++;
8753
0
    ssymhead->ssym = ssym;
8754
0
    ssymhead->count = 0;
8755
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8756
0
  }
8757
0
      ssym->st_name = (*ind)->st_name;
8758
0
      ssym->st_info = (*ind)->st_info;
8759
0
      ssym->st_other = (*ind)->st_other;
8760
0
      ssymhead->count++;
8761
0
    }
8762
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8763
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8764
8765
0
  free (indbuf);
8766
0
  return ssymbuf;
8767
0
}
8768
8769
/* Check if 2 sections define the same set of local and global
8770
   symbols.  */
8771
8772
static bool
8773
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8774
           struct bfd_link_info *info)
8775
0
{
8776
0
  bfd *bfd1, *bfd2;
8777
0
  elf_backend_data *bed1, *bed2;
8778
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8779
0
  size_t symcount1, symcount2;
8780
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8781
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8782
0
  Elf_Internal_Sym *isym, *isymend;
8783
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8784
0
  size_t count1, count2, sec_count1, sec_count2, i;
8785
0
  unsigned int shndx1, shndx2;
8786
0
  bool result;
8787
0
  bool ignore_section_symbol_p;
8788
8789
0
  bfd1 = sec1->owner;
8790
0
  bfd2 = sec2->owner;
8791
8792
  /* Both sections have to be in ELF.  */
8793
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8794
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8795
0
    return false;
8796
8797
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8798
0
    return false;
8799
8800
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8801
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8802
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8803
0
    return false;
8804
8805
0
  bed1 = get_elf_backend_data (bfd1);
8806
0
  bed2 = get_elf_backend_data (bfd2);
8807
0
  hdr1 = &elf_symtab_hdr (bfd1);
8808
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8809
0
  hdr2 = &elf_symtab_hdr (bfd2);
8810
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8811
8812
0
  if (symcount1 == 0 || symcount2 == 0)
8813
0
    return false;
8814
8815
0
  result = false;
8816
0
  isymbuf1 = NULL;
8817
0
  isymbuf2 = NULL;
8818
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8819
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8820
8821
  /* Ignore section symbols only when matching non-debugging sections
8822
     or linkonce section with comdat section.  */
8823
0
  ignore_section_symbol_p
8824
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8825
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8826
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8827
8828
0
  if (ssymbuf1 == NULL)
8829
0
    {
8830
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8831
0
               NULL, NULL, NULL);
8832
0
      if (isymbuf1 == NULL)
8833
0
  goto done;
8834
8835
0
      if (info != NULL && !info->reduce_memory_overheads)
8836
0
  {
8837
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8838
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8839
0
  }
8840
0
    }
8841
8842
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8843
0
    {
8844
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8845
0
               NULL, NULL, NULL);
8846
0
      if (isymbuf2 == NULL)
8847
0
  goto done;
8848
8849
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8850
0
  {
8851
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8852
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8853
0
  }
8854
0
    }
8855
8856
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8857
0
    {
8858
      /* Optimized faster version.  */
8859
0
      size_t lo, hi, mid;
8860
0
      struct elf_symbol *symp;
8861
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8862
8863
0
      lo = 0;
8864
0
      hi = ssymbuf1->count;
8865
0
      ssymbuf1++;
8866
0
      count1 = 0;
8867
0
      sec_count1 = 0;
8868
0
      while (lo < hi)
8869
0
  {
8870
0
    mid = (lo + hi) / 2;
8871
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8872
0
      hi = mid;
8873
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8874
0
      lo = mid + 1;
8875
0
    else
8876
0
      {
8877
0
        count1 = ssymbuf1[mid].count;
8878
0
        ssymbuf1 += mid;
8879
0
        break;
8880
0
      }
8881
0
  }
8882
0
      if (ignore_section_symbol_p)
8883
0
  {
8884
0
    for (i = 0; i < count1; i++)
8885
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8886
0
        sec_count1++;
8887
0
    count1 -= sec_count1;
8888
0
  }
8889
8890
0
      lo = 0;
8891
0
      hi = ssymbuf2->count;
8892
0
      ssymbuf2++;
8893
0
      count2 = 0;
8894
0
      sec_count2 = 0;
8895
0
      while (lo < hi)
8896
0
  {
8897
0
    mid = (lo + hi) / 2;
8898
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8899
0
      hi = mid;
8900
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8901
0
      lo = mid + 1;
8902
0
    else
8903
0
      {
8904
0
        count2 = ssymbuf2[mid].count;
8905
0
        ssymbuf2 += mid;
8906
0
        break;
8907
0
      }
8908
0
  }
8909
0
      if (ignore_section_symbol_p)
8910
0
  {
8911
0
    for (i = 0; i < count2; i++)
8912
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8913
0
        sec_count2++;
8914
0
    count2 -= sec_count2;
8915
0
  }
8916
8917
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8918
0
  goto done;
8919
8920
0
      symtable1 = bfd_malloc (count1 * sizeof (*symtable1));
8921
0
      symtable2 = bfd_malloc (count2 * sizeof (*symtable2));
8922
0
      if (symtable1 == NULL || symtable2 == NULL)
8923
0
  goto done;
8924
8925
0
      symp = symtable1;
8926
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8927
0
     ssym < ssymend; ssym++)
8928
0
  if (sec_count1 == 0
8929
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8930
0
    {
8931
0
      symp->u.ssym = ssym;
8932
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8933
0
                hdr1->sh_link,
8934
0
                ssym->st_name);
8935
0
      if (symp->name == NULL)
8936
0
        goto done;
8937
0
      symp++;
8938
0
    }
8939
8940
0
      symp = symtable2;
8941
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8942
0
     ssym < ssymend; ssym++)
8943
0
  if (sec_count2 == 0
8944
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8945
0
    {
8946
0
      symp->u.ssym = ssym;
8947
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8948
0
                hdr2->sh_link,
8949
0
                ssym->st_name);
8950
0
      if (symp->name == NULL)
8951
0
        goto done;
8952
0
      symp++;
8953
0
    }
8954
8955
      /* Sort symbol by name.  */
8956
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8957
0
       elf_sym_name_compare);
8958
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8959
0
       elf_sym_name_compare);
8960
8961
0
      for (i = 0; i < count1; i++)
8962
  /* Two symbols must have the same binding, type and name.  */
8963
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8964
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8965
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8966
0
    goto done;
8967
8968
0
      result = true;
8969
0
      goto done;
8970
0
    }
8971
8972
0
  symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8973
0
  symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8974
0
  if (symtable1 == NULL || symtable2 == NULL)
8975
0
    goto done;
8976
8977
  /* Count definitions in the section.  */
8978
0
  count1 = 0;
8979
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8980
0
    if (isym->st_shndx == shndx1
8981
0
  && (!ignore_section_symbol_p
8982
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8983
0
      symtable1[count1++].u.isym = isym;
8984
8985
0
  count2 = 0;
8986
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8987
0
    if (isym->st_shndx == shndx2
8988
0
  && (!ignore_section_symbol_p
8989
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8990
0
      symtable2[count2++].u.isym = isym;
8991
8992
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8993
0
    goto done;
8994
8995
0
  for (i = 0; i < count1; i++)
8996
0
    {
8997
0
      symtable1[i].name
8998
0
  = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8999
0
             symtable1[i].u.isym->st_name);
9000
0
      if (symtable1[i].name == NULL)
9001
0
  goto done;
9002
0
    }
9003
9004
0
  for (i = 0; i < count2; i++)
9005
0
    {
9006
0
      symtable2[i].name
9007
0
  = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
9008
0
             symtable2[i].u.isym->st_name);
9009
0
      if (symtable2[i].name == NULL)
9010
0
  goto done;
9011
0
    }
9012
9013
  /* Sort symbol by name.  */
9014
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
9015
0
   elf_sym_name_compare);
9016
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
9017
0
   elf_sym_name_compare);
9018
9019
0
  for (i = 0; i < count1; i++)
9020
    /* Two symbols must have the same binding, type and name.  */
9021
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
9022
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
9023
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
9024
0
      goto done;
9025
9026
0
  result = true;
9027
9028
0
 done:
9029
0
  free (symtable1);
9030
0
  free (symtable2);
9031
0
  free (isymbuf1);
9032
0
  free (isymbuf2);
9033
9034
0
  return result;
9035
0
}
9036
9037
/* Return TRUE if 2 section types are compatible.  */
9038
9039
bool
9040
bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
9041
        bfd *bbfd, const asection *bsec)
9042
0
{
9043
0
  if (asec == NULL
9044
0
      || bsec == NULL
9045
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
9046
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
9047
0
    return true;
9048
9049
0
  return elf_section_type (asec) == elf_section_type (bsec);
9050
0
}
9051

9052
/* Final phase of ELF linker.  */
9053
9054
/* A structure we use to avoid passing large numbers of arguments.  */
9055
9056
struct elf_final_link_info
9057
{
9058
  /* General link information.  */
9059
  struct bfd_link_info *info;
9060
  /* Output BFD.  */
9061
  bfd *output_bfd;
9062
  /* Symbol string table.  */
9063
  struct elf_strtab_hash *symstrtab;
9064
  /* .hash section.  */
9065
  asection *hash_sec;
9066
  /* symbol version section (.gnu.version).  */
9067
  asection *symver_sec;
9068
  /* Buffer large enough to hold contents of any section.  */
9069
  bfd_byte *contents;
9070
  /* Buffer large enough to hold external relocs of any section.  */
9071
  void *external_relocs;
9072
  /* Buffer large enough to hold internal relocs of any section.  */
9073
  Elf_Internal_Rela *internal_relocs;
9074
  /* Buffer large enough to hold external local symbols of any input
9075
     BFD.  */
9076
  bfd_byte *external_syms;
9077
  /* And a buffer for symbol section indices.  */
9078
  Elf_External_Sym_Shndx *locsym_shndx;
9079
  /* Buffer large enough to hold internal local symbols of any input
9080
     BFD.  */
9081
  Elf_Internal_Sym *internal_syms;
9082
  /* Array large enough to hold a symbol index for each local symbol
9083
     of any input BFD.  */
9084
  long *indices;
9085
  /* Array large enough to hold a section pointer for each local
9086
     symbol of any input BFD.  */
9087
  asection **sections;
9088
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
9089
  Elf_External_Sym_Shndx *symshndxbuf;
9090
  /* Number of STT_FILE syms seen.  */
9091
  size_t filesym_count;
9092
  /* Local symbol hash table.  */
9093
  struct bfd_hash_table local_hash_table;
9094
};
9095
9096
struct local_hash_entry
9097
{
9098
  /* Base hash table entry structure.  */
9099
  struct bfd_hash_entry root;
9100
  /* Size of the local symbol name.  */
9101
  size_t size;
9102
  /* Number of the duplicated local symbol names.  */
9103
  long count;
9104
};
9105
9106
/* Create an entry in the local symbol hash table.  */
9107
9108
static struct bfd_hash_entry *
9109
local_hash_newfunc (struct bfd_hash_entry *entry,
9110
        struct bfd_hash_table *table,
9111
        const char *string)
9112
0
{
9113
9114
  /* Allocate the structure if it has not already been allocated by a
9115
     subclass.  */
9116
0
  if (entry == NULL)
9117
0
    {
9118
0
      entry = bfd_hash_allocate (table, sizeof (struct local_hash_entry));
9119
0
      if (entry == NULL)
9120
0
        return entry;
9121
0
    }
9122
9123
  /* Call the allocation method of the superclass.  */
9124
0
  entry = bfd_hash_newfunc (entry, table, string);
9125
0
  if (entry != NULL)
9126
0
    {
9127
0
      ((struct local_hash_entry *) entry)->count = 0;
9128
0
      ((struct local_hash_entry *) entry)->size = 0;
9129
0
    }
9130
9131
0
  return entry;
9132
0
}
9133
9134
/* This struct is used to pass information to elf_link_output_extsym.  */
9135
9136
struct elf_outext_info
9137
{
9138
  bool failed;
9139
  bool localsyms;
9140
  bool file_sym_done;
9141
  bool base_symbol;
9142
  struct elf_final_link_info *flinfo;
9143
};
9144
9145
9146
/* Support for evaluating a complex relocation.
9147
9148
   Complex relocations are generalized, self-describing relocations.  The
9149
   implementation of them consists of two parts: complex symbols, and the
9150
   relocations themselves.
9151
9152
   The relocations use a reserved elf-wide relocation type code (R_RELC
9153
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
9154
   information (start bit, end bit, word width, etc) into the addend.  This
9155
   information is extracted from CGEN-generated operand tables within gas.
9156
9157
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9158
   internal) representing prefix-notation expressions, including but not
9159
   limited to those sorts of expressions normally encoded as addends in the
9160
   addend field.  The symbol mangling format is:
9161
9162
   <node> := <literal>
9163
    |  <unary-operator> ':' <node>
9164
    |  <binary-operator> ':' <node> ':' <node>
9165
    ;
9166
9167
   <literal> := 's' <digits=N> ':' <N character symbol name>
9168
       |  'S' <digits=N> ':' <N character section name>
9169
       |  '#' <hexdigits>
9170
       ;
9171
9172
   <binary-operator> := as in C
9173
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
9174
9175
static bool
9176
set_symbol_value (bfd *bfd_with_globals,
9177
      Elf_Internal_Sym *isymbuf,
9178
      size_t locsymcount,
9179
      size_t num_sym,
9180
      size_t symidx,
9181
      bfd_vma val)
9182
0
{
9183
0
  struct elf_link_hash_entry *h;
9184
0
  size_t extsymoff = locsymcount;
9185
9186
0
  if (symidx < locsymcount)
9187
0
    {
9188
0
      Elf_Internal_Sym *sym;
9189
9190
0
      sym = isymbuf + symidx;
9191
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9192
0
  {
9193
    /* It is a local symbol: move it to the
9194
       "absolute" section and give it a value.  */
9195
0
    sym->st_shndx = SHN_ABS;
9196
0
    sym->st_value = val;
9197
0
    return true;
9198
0
  }
9199
0
      if (!elf_bad_symtab (bfd_with_globals))
9200
0
  {
9201
0
    _bfd_error_handler (_("%pB: corrupt symbol table"),
9202
0
            bfd_with_globals);
9203
0
    bfd_set_error (bfd_error_bad_value);
9204
0
    return false;
9205
0
  }
9206
0
      extsymoff = 0;
9207
0
    }
9208
9209
  /* It is a global symbol: set its link type
9210
     to "defined" and give it a value.  */
9211
0
  h = _bfd_elf_get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx,
9212
0
            extsymoff, num_sym);
9213
0
  if (h == NULL)
9214
0
    return false;
9215
0
  h->root.type = bfd_link_hash_defined;
9216
0
  h->root.u.def.value = val;
9217
0
  h->root.u.def.section = bfd_abs_section_ptr;
9218
0
  return true;
9219
0
}
9220
9221
static bool
9222
resolve_symbol (const char *name,
9223
    bfd *input_bfd,
9224
    struct elf_final_link_info *flinfo,
9225
    bfd_vma *result,
9226
    Elf_Internal_Sym *isymbuf,
9227
    size_t locsymcount)
9228
0
{
9229
0
  Elf_Internal_Sym *sym;
9230
0
  struct bfd_link_hash_entry *global_entry;
9231
0
  const char *candidate = NULL;
9232
0
  Elf_Internal_Shdr *symtab_hdr;
9233
0
  size_t i;
9234
9235
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
9236
9237
0
  for (i = 0; i < locsymcount; ++ i)
9238
0
    {
9239
0
      sym = isymbuf + i;
9240
9241
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9242
0
  continue;
9243
9244
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
9245
0
               symtab_hdr->sh_link,
9246
0
               sym->st_name);
9247
#ifdef DEBUG
9248
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9249
        name, candidate, (unsigned long) sym->st_value);
9250
#endif
9251
0
      if (candidate && strcmp (candidate, name) == 0)
9252
0
  {
9253
0
    asection *sec = flinfo->sections [i];
9254
9255
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9256
0
    *result += sec->output_offset + sec->output_section->vma;
9257
#ifdef DEBUG
9258
    printf ("Found symbol with value %8.8lx\n",
9259
      (unsigned long) *result);
9260
#endif
9261
0
    return true;
9262
0
  }
9263
0
    }
9264
9265
  /* Hmm, haven't found it yet. perhaps it is a global.  */
9266
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9267
0
               false, false, true);
9268
0
  if (!global_entry)
9269
0
    return false;
9270
9271
0
  if (global_entry->type == bfd_link_hash_defined
9272
0
      || global_entry->type == bfd_link_hash_defweak)
9273
0
    {
9274
0
      *result = (global_entry->u.def.value
9275
0
     + global_entry->u.def.section->output_section->vma
9276
0
     + global_entry->u.def.section->output_offset);
9277
#ifdef DEBUG
9278
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9279
        global_entry->root.string, (unsigned long) *result);
9280
#endif
9281
0
      return true;
9282
0
    }
9283
9284
0
  return false;
9285
0
}
9286
9287
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
9288
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
9289
   names like "foo.end" which is the end address of section "foo".  */
9290
9291
static bool
9292
resolve_section (const char *name,
9293
     asection *sections,
9294
     bfd_vma *result,
9295
     bfd * abfd)
9296
0
{
9297
0
  asection *curr;
9298
0
  unsigned int len;
9299
9300
0
  for (curr = sections; curr; curr = curr->next)
9301
0
    if (strcmp (curr->name, name) == 0)
9302
0
      {
9303
0
  *result = curr->vma;
9304
0
  return true;
9305
0
      }
9306
9307
  /* Hmm. still haven't found it. try pseudo-section names.  */
9308
  /* FIXME: This could be coded more efficiently...  */
9309
0
  for (curr = sections; curr; curr = curr->next)
9310
0
    {
9311
0
      len = strlen (curr->name);
9312
0
      if (len > strlen (name))
9313
0
  continue;
9314
9315
0
      if (strncmp (curr->name, name, len) == 0)
9316
0
  {
9317
0
    if (startswith (name + len, ".end"))
9318
0
      {
9319
0
        *result = (curr->vma
9320
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
9321
0
        return true;
9322
0
      }
9323
9324
    /* Insert more pseudo-section names here, if you like.  */
9325
0
  }
9326
0
    }
9327
9328
0
  return false;
9329
0
}
9330
9331
static void
9332
undefined_reference (const char *reftype, const char *name)
9333
0
{
9334
  /* xgettext:c-format */
9335
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9336
0
          reftype, name);
9337
0
  bfd_set_error (bfd_error_bad_value);
9338
0
}
9339
9340
static bool
9341
eval_symbol (bfd_vma *result,
9342
       const char **symp,
9343
       bfd *input_bfd,
9344
       struct elf_final_link_info *flinfo,
9345
       bfd_vma dot,
9346
       Elf_Internal_Sym *isymbuf,
9347
       size_t locsymcount,
9348
       int signed_p)
9349
0
{
9350
0
  size_t len;
9351
0
  size_t symlen;
9352
0
  bfd_vma a;
9353
0
  bfd_vma b;
9354
0
  char symbuf[4096];
9355
0
  const char *sym = *symp;
9356
0
  const char *symend;
9357
0
  bool symbol_is_section = false;
9358
9359
0
  len = strlen (sym);
9360
0
  symend = sym + len;
9361
9362
0
  if (len < 1 || len > sizeof (symbuf))
9363
0
    {
9364
0
      bfd_set_error (bfd_error_invalid_operation);
9365
0
      return false;
9366
0
    }
9367
9368
0
  switch (* sym)
9369
0
    {
9370
0
    case '.':
9371
0
      *result = dot;
9372
0
      *symp = sym + 1;
9373
0
      return true;
9374
9375
0
    case '#':
9376
0
      ++sym;
9377
0
      *result = strtoul (sym, (char **) symp, 16);
9378
0
      return true;
9379
9380
0
    case 'S':
9381
0
      symbol_is_section = true;
9382
      /* Fall through.  */
9383
0
    case 's':
9384
0
      ++sym;
9385
0
      symlen = strtol (sym, (char **) symp, 10);
9386
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9387
9388
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9389
0
  {
9390
0
    bfd_set_error (bfd_error_invalid_operation);
9391
0
    return false;
9392
0
  }
9393
9394
0
      memcpy (symbuf, sym, symlen);
9395
0
      symbuf[symlen] = '\0';
9396
0
      *symp = sym + symlen;
9397
9398
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9399
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9400
   interpretation here; section means "try section first", not "must be a
9401
   section", and likewise with symbol.  */
9402
9403
0
      if (symbol_is_section)
9404
0
  {
9405
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9406
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9407
0
          isymbuf, locsymcount))
9408
0
      {
9409
0
        undefined_reference ("section", symbuf);
9410
0
        return false;
9411
0
      }
9412
0
  }
9413
0
      else
9414
0
  {
9415
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9416
0
             isymbuf, locsymcount)
9417
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9418
0
           result, input_bfd))
9419
0
      {
9420
0
        undefined_reference ("symbol", symbuf);
9421
0
        return false;
9422
0
      }
9423
0
  }
9424
9425
0
      return true;
9426
9427
      /* All that remains are operators.  */
9428
9429
0
#define UNARY_OP(op)            \
9430
0
  if (startswith (sym, #op))          \
9431
0
    {               \
9432
0
      sym += strlen (#op);          \
9433
0
      if (*sym == ':')           \
9434
0
  ++sym;             \
9435
0
      *symp = sym;            \
9436
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9437
0
      isymbuf, locsymcount, signed_p)) \
9438
0
  return false;           \
9439
0
      if (signed_p)           \
9440
0
  *result = op ((bfd_signed_vma) a);     \
9441
0
      else              \
9442
0
  *result = op a;           \
9443
0
      return true;            \
9444
0
    }
9445
9446
0
#define BINARY_OP_HEAD(op)          \
9447
0
  if (startswith (sym, #op))          \
9448
0
    {               \
9449
0
      sym += strlen (#op);          \
9450
0
      if (*sym == ':')           \
9451
0
  ++sym;             \
9452
0
      *symp = sym;            \
9453
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9454
0
      isymbuf, locsymcount, signed_p)) \
9455
0
  return false;           \
9456
0
      ++*symp;              \
9457
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9458
0
      isymbuf, locsymcount, signed_p)) \
9459
0
  return false;
9460
0
#define BINARY_OP_TAIL(op)          \
9461
0
      if (signed_p)           \
9462
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9463
0
      else              \
9464
0
  *result = a op b;         \
9465
0
      return true;            \
9466
0
    }
9467
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9468
9469
0
    default:
9470
0
      UNARY_OP  (0-);
9471
0
      BINARY_OP_HEAD (<<);
9472
0
      if (b >= sizeof (a) * CHAR_BIT)
9473
0
  {
9474
0
    *result = 0;
9475
0
    return true;
9476
0
  }
9477
0
      signed_p = 0;
9478
0
      BINARY_OP_TAIL (<<);
9479
0
      BINARY_OP_HEAD (>>);
9480
0
      if (b >= sizeof (a) * CHAR_BIT)
9481
0
  {
9482
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9483
0
    return true;
9484
0
  }
9485
0
      BINARY_OP_TAIL (>>);
9486
0
      BINARY_OP (==);
9487
0
      BINARY_OP (!=);
9488
0
      BINARY_OP (<=);
9489
0
      BINARY_OP (>=);
9490
0
      BINARY_OP (&&);
9491
0
      BINARY_OP (||);
9492
0
      UNARY_OP  (~);
9493
0
      UNARY_OP  (!);
9494
0
      BINARY_OP (*);
9495
0
      BINARY_OP_HEAD (/);
9496
0
      if (b == 0)
9497
0
  {
9498
0
    _bfd_error_handler (_("division by zero"));
9499
0
    bfd_set_error (bfd_error_bad_value);
9500
0
    return false;
9501
0
  }
9502
0
      BINARY_OP_TAIL (/);
9503
0
      BINARY_OP_HEAD (%);
9504
0
      if (b == 0)
9505
0
  {
9506
0
    _bfd_error_handler (_("division by zero"));
9507
0
    bfd_set_error (bfd_error_bad_value);
9508
0
    return false;
9509
0
  }
9510
0
      BINARY_OP_TAIL (%);
9511
0
      BINARY_OP (^);
9512
0
      BINARY_OP (|);
9513
0
      BINARY_OP (&);
9514
0
      BINARY_OP (+);
9515
0
      BINARY_OP (-);
9516
0
      BINARY_OP (<);
9517
0
      BINARY_OP (>);
9518
0
#undef UNARY_OP
9519
0
#undef BINARY_OP
9520
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9521
0
      bfd_set_error (bfd_error_invalid_operation);
9522
0
      return false;
9523
0
    }
9524
0
}
9525
9526
static void
9527
put_value (bfd_vma size,
9528
     unsigned long chunksz,
9529
     bfd *input_bfd,
9530
     bfd_vma x,
9531
     bfd_byte *location)
9532
0
{
9533
0
  location += (size - chunksz);
9534
9535
0
  for (; size; size -= chunksz, location -= chunksz)
9536
0
    {
9537
0
      switch (chunksz)
9538
0
  {
9539
0
  case 1:
9540
0
    bfd_put_8 (input_bfd, x, location);
9541
0
    x >>= 8;
9542
0
    break;
9543
0
  case 2:
9544
0
    bfd_put_16 (input_bfd, x, location);
9545
0
    x >>= 16;
9546
0
    break;
9547
0
  case 4:
9548
0
    bfd_put_32 (input_bfd, x, location);
9549
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9550
0
    x >>= 16;
9551
0
    x >>= 16;
9552
0
    break;
9553
0
#ifdef BFD64
9554
0
  case 8:
9555
0
    bfd_put_64 (input_bfd, x, location);
9556
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9557
0
    x >>= 32;
9558
0
    x >>= 32;
9559
0
    break;
9560
0
#endif
9561
0
  default:
9562
0
    abort ();
9563
0
    break;
9564
0
  }
9565
0
    }
9566
0
}
9567
9568
static bfd_vma
9569
get_value (bfd_vma size,
9570
     unsigned long chunksz,
9571
     bfd *input_bfd,
9572
     bfd_byte *location)
9573
0
{
9574
0
  int shift;
9575
0
  bfd_vma x = 0;
9576
9577
  /* Sanity checks.  */
9578
0
  BFD_ASSERT (chunksz <= sizeof (x)
9579
0
        && size >= chunksz
9580
0
        && chunksz != 0
9581
0
        && (size % chunksz) == 0
9582
0
        && input_bfd != NULL
9583
0
        && location != NULL);
9584
9585
0
  if (chunksz == sizeof (x))
9586
0
    {
9587
0
      BFD_ASSERT (size == chunksz);
9588
9589
      /* Make sure that we do not perform an undefined shift operation.
9590
   We know that size == chunksz so there will only be one iteration
9591
   of the loop below.  */
9592
0
      shift = 0;
9593
0
    }
9594
0
  else
9595
0
    shift = 8 * chunksz;
9596
9597
0
  for (; size; size -= chunksz, location += chunksz)
9598
0
    {
9599
0
      switch (chunksz)
9600
0
  {
9601
0
  case 1:
9602
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9603
0
    break;
9604
0
  case 2:
9605
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9606
0
    break;
9607
0
  case 4:
9608
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9609
0
    break;
9610
0
#ifdef BFD64
9611
0
  case 8:
9612
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9613
0
    break;
9614
0
#endif
9615
0
  default:
9616
0
    abort ();
9617
0
  }
9618
0
    }
9619
0
  return x;
9620
0
}
9621
9622
static void
9623
decode_complex_addend (unsigned long *start,   /* in bits */
9624
           unsigned long *oplen,   /* in bits */
9625
           unsigned long *len,     /* in bits */
9626
           unsigned long *wordsz,  /* in bytes */
9627
           unsigned long *chunksz, /* in bytes */
9628
           unsigned long *lsb0_p,
9629
           unsigned long *signed_p,
9630
           unsigned long *trunc_p,
9631
           unsigned long encoded)
9632
0
{
9633
0
  * start     =  encoded  & 0x3F;
9634
0
  * len       = (encoded >>  6) & 0x3F;
9635
0
  * oplen     = (encoded >> 12) & 0x3F;
9636
0
  * wordsz    = (encoded >> 18) & 0xF;
9637
0
  * chunksz   = (encoded >> 22) & 0xF;
9638
0
  * lsb0_p    = (encoded >> 27) & 1;
9639
0
  * signed_p  = (encoded >> 28) & 1;
9640
0
  * trunc_p   = (encoded >> 29) & 1;
9641
0
}
9642
9643
bfd_reloc_status_type
9644
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9645
            asection *input_section,
9646
            bfd_byte *contents,
9647
            Elf_Internal_Rela *rel,
9648
            bfd_vma relocation)
9649
0
{
9650
0
  bfd_vma shift, x, mask;
9651
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9652
0
  bfd_reloc_status_type r;
9653
0
  bfd_size_type octets;
9654
9655
  /*  Perform this reloc, since it is complex.
9656
      (this is not to say that it necessarily refers to a complex
9657
      symbol; merely that it is a self-describing CGEN based reloc.
9658
      i.e. the addend has the complete reloc information (bit start, end,
9659
      word size, etc) encoded within it.).  */
9660
9661
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9662
0
       &chunksz, &lsb0_p, &signed_p,
9663
0
       &trunc_p, rel->r_addend);
9664
9665
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9666
9667
0
  if (lsb0_p)
9668
0
    shift = (start + 1) - len;
9669
0
  else
9670
0
    shift = (8 * wordsz) - (start + len);
9671
9672
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9673
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9674
9675
#ifdef DEBUG
9676
  printf ("Doing complex reloc: "
9677
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9678
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9679
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9680
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9681
    oplen, (unsigned long) x, (unsigned long) mask,
9682
    (unsigned long) relocation);
9683
#endif
9684
9685
0
  r = bfd_reloc_ok;
9686
0
  if (! trunc_p)
9687
    /* Now do an overflow check.  */
9688
0
    r = bfd_check_overflow ((signed_p
9689
0
           ? complain_overflow_signed
9690
0
           : complain_overflow_unsigned),
9691
0
          len, 0, (8 * wordsz),
9692
0
          relocation);
9693
9694
  /* Do the deed.  */
9695
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9696
9697
#ifdef DEBUG
9698
  printf ("           relocation: %8.8lx\n"
9699
    "         shifted mask: %8.8lx\n"
9700
    " shifted/masked reloc: %8.8lx\n"
9701
    "               result: %8.8lx\n",
9702
    (unsigned long) relocation, (unsigned long) (mask << shift),
9703
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9704
#endif
9705
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9706
0
  return r;
9707
0
}
9708
9709
/* Functions to read r_offset from external (target order) reloc
9710
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9711
   know the value is aligned.  */
9712
9713
static bfd_vma
9714
ext32l_r_offset (const void *p)
9715
0
{
9716
0
  union aligned32
9717
0
  {
9718
0
    uint32_t v;
9719
0
    unsigned char c[4];
9720
0
  };
9721
0
  const union aligned32 *a
9722
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9723
9724
0
  uint32_t aval = (  (uint32_t) a->c[0]
9725
0
       | (uint32_t) a->c[1] << 8
9726
0
       | (uint32_t) a->c[2] << 16
9727
0
       | (uint32_t) a->c[3] << 24);
9728
0
  return aval;
9729
0
}
9730
9731
static bfd_vma
9732
ext32b_r_offset (const void *p)
9733
0
{
9734
0
  union aligned32
9735
0
  {
9736
0
    uint32_t v;
9737
0
    unsigned char c[4];
9738
0
  };
9739
0
  const union aligned32 *a
9740
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9741
9742
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9743
0
       | (uint32_t) a->c[1] << 16
9744
0
       | (uint32_t) a->c[2] << 8
9745
0
       | (uint32_t) a->c[3]);
9746
0
  return aval;
9747
0
}
9748
9749
static bfd_vma
9750
ext64l_r_offset (const void *p)
9751
0
{
9752
0
  union aligned64
9753
0
  {
9754
0
    uint64_t v;
9755
0
    unsigned char c[8];
9756
0
  };
9757
0
  const union aligned64 *a
9758
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9759
9760
0
  uint64_t aval = (  (uint64_t) a->c[0]
9761
0
       | (uint64_t) a->c[1] << 8
9762
0
       | (uint64_t) a->c[2] << 16
9763
0
       | (uint64_t) a->c[3] << 24
9764
0
       | (uint64_t) a->c[4] << 32
9765
0
       | (uint64_t) a->c[5] << 40
9766
0
       | (uint64_t) a->c[6] << 48
9767
0
       | (uint64_t) a->c[7] << 56);
9768
0
  return aval;
9769
0
}
9770
9771
static bfd_vma
9772
ext64b_r_offset (const void *p)
9773
0
{
9774
0
  union aligned64
9775
0
  {
9776
0
    uint64_t v;
9777
0
    unsigned char c[8];
9778
0
  };
9779
0
  const union aligned64 *a
9780
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9781
9782
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9783
0
       | (uint64_t) a->c[1] << 48
9784
0
       | (uint64_t) a->c[2] << 40
9785
0
       | (uint64_t) a->c[3] << 32
9786
0
       | (uint64_t) a->c[4] << 24
9787
0
       | (uint64_t) a->c[5] << 16
9788
0
       | (uint64_t) a->c[6] << 8
9789
0
       | (uint64_t) a->c[7]);
9790
0
  return aval;
9791
0
}
9792
9793
/* When performing a relocatable link, the input relocations are
9794
   preserved.  But, if they reference global symbols, the indices
9795
   referenced must be updated.  Update all the relocations found in
9796
   RELDATA.  */
9797
9798
static bool
9799
elf_link_adjust_relocs (bfd *abfd,
9800
      asection *sec,
9801
      struct bfd_elf_section_reloc_data *reldata,
9802
      bool sort,
9803
      struct bfd_link_info *info)
9804
0
{
9805
0
  unsigned int i;
9806
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
9807
0
  bfd_byte *erela;
9808
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9809
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9810
0
  bfd_vma r_type_mask;
9811
0
  int r_sym_shift;
9812
0
  unsigned int count = reldata->count;
9813
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9814
9815
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9816
0
    {
9817
0
      swap_in = bed->s->swap_reloc_in;
9818
0
      swap_out = bed->s->swap_reloc_out;
9819
0
    }
9820
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9821
0
    {
9822
0
      swap_in = bed->s->swap_reloca_in;
9823
0
      swap_out = bed->s->swap_reloca_out;
9824
0
    }
9825
0
  else
9826
0
    abort ();
9827
9828
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9829
0
    abort ();
9830
9831
0
  if (bed->s->arch_size == 32)
9832
0
    {
9833
0
      r_type_mask = 0xff;
9834
0
      r_sym_shift = 8;
9835
0
    }
9836
0
  else
9837
0
    {
9838
0
      r_type_mask = 0xffffffff;
9839
0
      r_sym_shift = 32;
9840
0
    }
9841
9842
0
  erela = reldata->hdr->contents;
9843
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9844
0
    {
9845
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9846
0
      unsigned int j;
9847
9848
0
      if (*rel_hash == NULL)
9849
0
  continue;
9850
9851
0
      if ((*rel_hash)->indx == -2
9852
0
    && info->gc_sections
9853
0
    && ! info->gc_keep_exported)
9854
0
  {
9855
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9856
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9857
0
            abfd, sec,
9858
0
            (*rel_hash)->root.root.string);
9859
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9860
0
            abfd, sec);
9861
0
    bfd_set_error (bfd_error_invalid_operation);
9862
0
    return false;
9863
0
  }
9864
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9865
9866
0
      (*swap_in) (abfd, erela, irela);
9867
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9868
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9869
0
         | (irela[j].r_info & r_type_mask));
9870
0
      (*swap_out) (abfd, irela, erela);
9871
0
    }
9872
9873
0
  if (bed->elf_backend_update_relocs)
9874
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9875
9876
0
  if (sort && count != 0)
9877
0
    {
9878
0
      bfd_vma (*ext_r_off) (const void *);
9879
0
      bfd_vma r_off;
9880
0
      size_t elt_size;
9881
0
      bfd_byte *base, *end, *p, *loc;
9882
0
      bfd_byte *buf = NULL;
9883
9884
0
      if (bed->s->arch_size == 32)
9885
0
  {
9886
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9887
0
      ext_r_off = ext32l_r_offset;
9888
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9889
0
      ext_r_off = ext32b_r_offset;
9890
0
    else
9891
0
      abort ();
9892
0
  }
9893
0
      else
9894
0
  {
9895
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9896
0
      ext_r_off = ext64l_r_offset;
9897
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9898
0
      ext_r_off = ext64b_r_offset;
9899
0
    else
9900
0
      abort ();
9901
0
  }
9902
9903
      /*  Must use a stable sort here.  A modified insertion sort,
9904
    since the relocs are mostly sorted already.  */
9905
0
      elt_size = reldata->hdr->sh_entsize;
9906
0
      base = reldata->hdr->contents;
9907
0
      end = base + count * elt_size;
9908
0
      if (elt_size > sizeof (Elf64_External_Rela))
9909
0
  abort ();
9910
9911
      /* Ensure the first element is lowest.  This acts as a sentinel,
9912
   speeding the main loop below.  */
9913
0
      r_off = (*ext_r_off) (base);
9914
0
      for (p = loc = base; (p += elt_size) < end; )
9915
0
  {
9916
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9917
0
    if (r_off > r_off2)
9918
0
      {
9919
0
        r_off = r_off2;
9920
0
        loc = p;
9921
0
      }
9922
0
  }
9923
0
      if (loc != base)
9924
0
  {
9925
    /* Don't just swap *base and *loc as that changes the order
9926
       of the original base[0] and base[1] if they happen to
9927
       have the same r_offset.  */
9928
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9929
0
    memcpy (onebuf, loc, elt_size);
9930
0
    memmove (base + elt_size, base, loc - base);
9931
0
    memcpy (base, onebuf, elt_size);
9932
0
  }
9933
9934
0
      for (p = base + elt_size; (p += elt_size) < end; )
9935
0
  {
9936
    /* base to p is sorted, *p is next to insert.  */
9937
0
    r_off = (*ext_r_off) (p);
9938
    /* Search the sorted region for location to insert.  */
9939
0
    loc = p - elt_size;
9940
0
    while (r_off < (*ext_r_off) (loc))
9941
0
      loc -= elt_size;
9942
0
    loc += elt_size;
9943
0
    if (loc != p)
9944
0
      {
9945
        /* Chances are there is a run of relocs to insert here,
9946
     from one of more input files.  Files are not always
9947
     linked in order due to the way elf_link_input_bfd is
9948
     called.  See pr17666.  */
9949
0
        size_t sortlen = p - loc;
9950
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9951
0
        size_t runlen = elt_size;
9952
0
        bfd_vma r_off_runend = r_off;
9953
0
        bfd_vma r_off_runend_next;
9954
0
        size_t buf_size = 96 * 1024;
9955
0
        while (p + runlen < end
9956
0
         && (sortlen <= buf_size
9957
0
       || runlen + elt_size <= buf_size)
9958
         /* run must not break the ordering of base..loc+1 */
9959
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9960
         /* run must be already sorted */
9961
0
         && r_off_runend_next >= r_off_runend)
9962
0
    {
9963
0
      runlen += elt_size;
9964
0
      r_off_runend = r_off_runend_next;
9965
0
    }
9966
0
        if (buf == NULL)
9967
0
    {
9968
0
      buf = bfd_malloc (buf_size);
9969
0
      if (buf == NULL)
9970
0
        return false;
9971
0
    }
9972
0
        if (runlen < sortlen)
9973
0
    {
9974
0
      memcpy (buf, p, runlen);
9975
0
      memmove (loc + runlen, loc, sortlen);
9976
0
      memcpy (loc, buf, runlen);
9977
0
    }
9978
0
        else
9979
0
    {
9980
0
      memcpy (buf, loc, sortlen);
9981
0
      memmove (loc, p, runlen);
9982
0
      memcpy (loc + runlen, buf, sortlen);
9983
0
    }
9984
0
        p += runlen - elt_size;
9985
0
      }
9986
0
  }
9987
      /* Hashes are no longer valid.  */
9988
0
      free (reldata->hashes);
9989
0
      reldata->hashes = NULL;
9990
0
      free (buf);
9991
0
    }
9992
0
  return true;
9993
0
}
9994
9995
struct elf_link_sort_rela
9996
{
9997
  union {
9998
    bfd_vma offset;
9999
    bfd_vma sym_mask;
10000
  } u;
10001
  enum elf_reloc_type_class type;
10002
  /* We use this as an array of size int_rels_per_ext_rel.  */
10003
  Elf_Internal_Rela rela[1];
10004
};
10005
10006
/* qsort stability here and for cmp2 is only an issue if multiple
10007
   dynamic relocations are emitted at the same address.  But targets
10008
   that apply a series of dynamic relocations each operating on the
10009
   result of the prior relocation can't use -z combreloc as
10010
   implemented anyway.  Such schemes tend to be broken by sorting on
10011
   symbol index.  That leaves dynamic NONE relocs as the only other
10012
   case where ld might emit multiple relocs at the same address, and
10013
   those are only emitted due to target bugs.  */
10014
10015
static int
10016
elf_link_sort_cmp1 (const void *A, const void *B)
10017
0
{
10018
0
  const struct elf_link_sort_rela *a = A;
10019
0
  const struct elf_link_sort_rela *b = B;
10020
0
  int relativea, relativeb;
10021
10022
0
  relativea = a->type == reloc_class_relative;
10023
0
  relativeb = b->type == reloc_class_relative;
10024
10025
0
  if (relativea < relativeb)
10026
0
    return 1;
10027
0
  if (relativea > relativeb)
10028
0
    return -1;
10029
0
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
10030
0
    return -1;
10031
0
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
10032
0
    return 1;
10033
0
  if (a->rela->r_offset < b->rela->r_offset)
10034
0
    return -1;
10035
0
  if (a->rela->r_offset > b->rela->r_offset)
10036
0
    return 1;
10037
0
  return 0;
10038
0
}
10039
10040
static int
10041
elf_link_sort_cmp2 (const void *A, const void *B)
10042
0
{
10043
0
  const struct elf_link_sort_rela *a = A;
10044
0
  const struct elf_link_sort_rela *b = B;
10045
10046
0
  if (a->type < b->type)
10047
0
    return -1;
10048
0
  if (a->type > b->type)
10049
0
    return 1;
10050
0
  if (a->u.offset < b->u.offset)
10051
0
    return -1;
10052
0
  if (a->u.offset > b->u.offset)
10053
0
    return 1;
10054
0
  if (a->rela->r_offset < b->rela->r_offset)
10055
0
    return -1;
10056
0
  if (a->rela->r_offset > b->rela->r_offset)
10057
0
    return 1;
10058
0
  return 0;
10059
0
}
10060
10061
static size_t
10062
elf_link_sort_relocs (bfd *obfd, struct bfd_link_info *info, asection **psec)
10063
0
{
10064
0
  asection *dynamic_relocs;
10065
0
  asection *rela_dyn;
10066
0
  asection *rel_dyn;
10067
0
  bfd_size_type count, size;
10068
0
  size_t i, ret, sort_elt, ext_size;
10069
0
  bfd_byte *sort, *s_non_relative, *p;
10070
0
  struct elf_link_sort_rela *sq;
10071
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
10072
0
  int i2e = obed->s->int_rels_per_ext_rel;
10073
0
  unsigned int opb = bfd_octets_per_byte (obfd, NULL);
10074
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10075
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10076
0
  struct bfd_link_order *lo;
10077
0
  bfd_vma r_sym_mask;
10078
0
  bool use_rela;
10079
10080
  /* Find a dynamic reloc section.  */
10081
0
  rela_dyn = bfd_get_section_by_name (obfd, ".rela.dyn");
10082
0
  rel_dyn  = bfd_get_section_by_name (obfd, ".rel.dyn");
10083
0
  if (rela_dyn != NULL && rela_dyn->size > 0
10084
0
      && rel_dyn != NULL && rel_dyn->size > 0)
10085
0
    {
10086
0
      bool use_rela_initialised = false;
10087
10088
      /* This is just here to stop gcc from complaining.
10089
   Its initialization checking code is not perfect.  */
10090
0
      use_rela = true;
10091
10092
      /* Both sections are present.  Examine the sizes
10093
   of the indirect sections to help us choose.  */
10094
0
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10095
0
  if (lo->type == bfd_indirect_link_order)
10096
0
    {
10097
0
      asection *o = lo->u.indirect.section;
10098
10099
0
      if ((o->size % obed->s->sizeof_rela) == 0)
10100
0
        {
10101
0
    if ((o->size % obed->s->sizeof_rel) == 0)
10102
      /* Section size is divisible by both rel and rela sizes.
10103
         It is of no help to us.  */
10104
0
      ;
10105
0
    else
10106
0
      {
10107
        /* Section size is only divisible by rela.  */
10108
0
        if (use_rela_initialised && !use_rela)
10109
0
          {
10110
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10111
0
                "they are in more than one size"),
10112
0
              obfd);
10113
0
      bfd_set_error (bfd_error_invalid_operation);
10114
0
      return 0;
10115
0
          }
10116
0
        else
10117
0
          {
10118
0
      use_rela = true;
10119
0
      use_rela_initialised = true;
10120
0
          }
10121
0
      }
10122
0
        }
10123
0
      else if ((o->size % obed->s->sizeof_rel) == 0)
10124
0
        {
10125
    /* Section size is only divisible by rel.  */
10126
0
    if (use_rela_initialised && use_rela)
10127
0
      {
10128
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10129
0
            "they are in more than one size"),
10130
0
          obfd);
10131
0
        bfd_set_error (bfd_error_invalid_operation);
10132
0
        return 0;
10133
0
      }
10134
0
    else
10135
0
      {
10136
0
        use_rela = false;
10137
0
        use_rela_initialised = true;
10138
0
      }
10139
0
        }
10140
0
      else
10141
0
        {
10142
    /* The section size is not divisible by either -
10143
       something is wrong.  */
10144
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10145
0
              "they are of an unknown size"), obfd);
10146
0
    bfd_set_error (bfd_error_invalid_operation);
10147
0
    return 0;
10148
0
        }
10149
0
    }
10150
10151
0
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10152
0
  if (lo->type == bfd_indirect_link_order)
10153
0
    {
10154
0
      asection *o = lo->u.indirect.section;
10155
10156
0
      if ((o->size % obed->s->sizeof_rela) == 0)
10157
0
        {
10158
0
    if ((o->size % obed->s->sizeof_rel) == 0)
10159
      /* Section size is divisible by both rel and rela sizes.
10160
         It is of no help to us.  */
10161
0
      ;
10162
0
    else
10163
0
      {
10164
        /* Section size is only divisible by rela.  */
10165
0
        if (use_rela_initialised && !use_rela)
10166
0
          {
10167
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10168
0
                "they are in more than one size"),
10169
0
              obfd);
10170
0
      bfd_set_error (bfd_error_invalid_operation);
10171
0
      return 0;
10172
0
          }
10173
0
        else
10174
0
          {
10175
0
      use_rela = true;
10176
0
      use_rela_initialised = true;
10177
0
          }
10178
0
      }
10179
0
        }
10180
0
      else if ((o->size % obed->s->sizeof_rel) == 0)
10181
0
        {
10182
    /* Section size is only divisible by rel.  */
10183
0
    if (use_rela_initialised && use_rela)
10184
0
      {
10185
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10186
0
            "they are in more than one size"),
10187
0
          obfd);
10188
0
        bfd_set_error (bfd_error_invalid_operation);
10189
0
        return 0;
10190
0
      }
10191
0
    else
10192
0
      {
10193
0
        use_rela = false;
10194
0
        use_rela_initialised = true;
10195
0
      }
10196
0
        }
10197
0
      else
10198
0
        {
10199
    /* The section size is not divisible by either -
10200
       something is wrong.  */
10201
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10202
0
              "they are of an unknown size"), obfd);
10203
0
    bfd_set_error (bfd_error_invalid_operation);
10204
0
    return 0;
10205
0
        }
10206
0
    }
10207
10208
0
      if (! use_rela_initialised)
10209
  /* Make a guess.  */
10210
0
  use_rela = true;
10211
0
    }
10212
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
10213
0
    use_rela = true;
10214
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
10215
0
    use_rela = false;
10216
0
  else
10217
0
    return 0;
10218
10219
0
  if (use_rela)
10220
0
    {
10221
0
      dynamic_relocs = rela_dyn;
10222
0
      ext_size = obed->s->sizeof_rela;
10223
0
      swap_in = obed->s->swap_reloca_in;
10224
0
      swap_out = obed->s->swap_reloca_out;
10225
0
    }
10226
0
  else
10227
0
    {
10228
0
      dynamic_relocs = rel_dyn;
10229
0
      ext_size = obed->s->sizeof_rel;
10230
0
      swap_in = obed->s->swap_reloc_in;
10231
0
      swap_out = obed->s->swap_reloc_out;
10232
0
    }
10233
10234
0
  size = 0;
10235
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10236
0
    if (lo->type == bfd_indirect_link_order)
10237
0
      size += lo->u.indirect.section->size;
10238
10239
0
  if (size != dynamic_relocs->size)
10240
0
    return 0;
10241
10242
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
10243
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
10244
10245
0
  count = dynamic_relocs->size / ext_size;
10246
0
  if (count == 0)
10247
0
    return 0;
10248
0
  sort = bfd_zmalloc (sort_elt * count);
10249
10250
0
  if (sort == NULL)
10251
0
    {
10252
0
      (*info->callbacks->warning)
10253
0
  (info, _("not enough memory to sort relocations"), 0, obfd, 0, 0);
10254
0
      return 0;
10255
0
    }
10256
10257
0
  if (obed->s->arch_size == 32)
10258
0
    r_sym_mask = ~(bfd_vma) 0xff;
10259
0
  else
10260
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
10261
10262
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10263
0
    if (lo->type == bfd_indirect_link_order)
10264
0
      {
10265
0
  bfd_byte *erel, *erelend;
10266
0
  asection *o = lo->u.indirect.section;
10267
10268
0
  if (o->contents == NULL && o->size != 0)
10269
0
    {
10270
      /* This is a reloc section that is being handled as a normal
10271
         section.  See bfd_section_from_shdr.  We can't combine
10272
         relocs in this case.  */
10273
0
      free (sort);
10274
0
      return 0;
10275
0
    }
10276
0
  erel = o->contents;
10277
0
  erelend = o->contents + o->size;
10278
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
10279
10280
0
  while (erel < erelend)
10281
0
    {
10282
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10283
10284
0
      (*swap_in) (obfd, erel, s->rela);
10285
0
      s->type = obed->elf_backend_reloc_type_class (info, o, s->rela);
10286
0
      s->u.sym_mask = r_sym_mask;
10287
0
      p += sort_elt;
10288
0
      erel += ext_size;
10289
0
    }
10290
0
      }
10291
10292
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10293
10294
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
10295
0
    {
10296
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10297
0
      if (s->type != reloc_class_relative)
10298
0
  break;
10299
0
    }
10300
0
  ret = i;
10301
0
  s_non_relative = p;
10302
10303
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
10304
0
  for (; i < count; i++, p += sort_elt)
10305
0
    {
10306
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10307
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10308
0
  sq = sp;
10309
0
      sp->u.offset = sq->rela->r_offset;
10310
0
    }
10311
10312
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10313
10314
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
10315
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10316
0
    {
10317
      /* We have plt relocs in .rela.dyn.  */
10318
0
      sq = (struct elf_link_sort_rela *) sort;
10319
0
      for (i = 0; i < count; i++)
10320
0
  if (sq[count - i - 1].type != reloc_class_plt)
10321
0
    break;
10322
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
10323
0
  {
10324
0
    struct bfd_link_order **plo;
10325
    /* Put srelplt link_order last.  This is so the output_offset
10326
       set in the next loop is correct for DT_JMPREL.  */
10327
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10328
0
      if ((*plo)->type == bfd_indirect_link_order
10329
0
    && (*plo)->u.indirect.section == htab->srelplt)
10330
0
        {
10331
0
    lo = *plo;
10332
0
    *plo = lo->next;
10333
0
        }
10334
0
      else
10335
0
        plo = &(*plo)->next;
10336
0
    *plo = lo;
10337
0
    lo->next = NULL;
10338
0
    dynamic_relocs->map_tail.link_order = lo;
10339
0
  }
10340
0
    }
10341
10342
0
  p = sort;
10343
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10344
0
    if (lo->type == bfd_indirect_link_order)
10345
0
      {
10346
0
  bfd_byte *erel, *erelend;
10347
0
  asection *o = lo->u.indirect.section;
10348
10349
0
  erel = o->contents;
10350
0
  erelend = o->contents + o->size;
10351
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10352
0
  while (erel < erelend)
10353
0
    {
10354
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10355
0
      (*swap_out) (obfd, s->rela, erel);
10356
0
      p += sort_elt;
10357
0
      erel += ext_size;
10358
0
    }
10359
0
      }
10360
10361
0
  free (sort);
10362
0
  *psec = dynamic_relocs;
10363
0
  return ret;
10364
0
}
10365
10366
/* Add a symbol to the output symbol string table.  */
10367
10368
static int
10369
elf_link_output_symstrtab (void *finf,
10370
         const char *name,
10371
         Elf_Internal_Sym *elfsym,
10372
         asection *input_sec,
10373
         struct elf_link_hash_entry *h)
10374
0
{
10375
0
  struct elf_final_link_info *flinfo = finf;
10376
0
  int (*output_symbol_hook)
10377
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10378
0
     struct elf_link_hash_entry *);
10379
0
  struct elf_link_hash_table *hash_table;
10380
0
  elf_backend_data *bed;
10381
0
  bfd_size_type strtabsize;
10382
10383
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10384
10385
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10386
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10387
0
  if (output_symbol_hook != NULL)
10388
0
    {
10389
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10390
0
      if (ret != 1)
10391
0
  return ret;
10392
0
    }
10393
10394
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10395
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10396
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10397
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10398
10399
0
  if (name == NULL || *name == '\0')
10400
0
    elfsym->st_name = (unsigned long) -1;
10401
0
  else
10402
0
    {
10403
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10404
   to get the final offset for st_name.  */
10405
0
      char *versioned_name = (char *) name;
10406
0
      if (h != NULL)
10407
0
  {
10408
0
    if (h->versioned == versioned && h->def_dynamic)
10409
0
      {
10410
        /* Keep only one '@' for versioned symbols defined in
10411
           shared objects.  */
10412
0
        const char *version = strrchr (name, ELF_VER_CHR);
10413
0
        const char *base_end = strchr (name, ELF_VER_CHR);
10414
0
        if (version != base_end)
10415
0
    {
10416
0
      size_t base_len;
10417
0
      size_t len = strlen (name);
10418
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10419
0
      if (versioned_name == NULL)
10420
0
        return 0;
10421
0
      base_len = base_end - name;
10422
0
      memcpy (versioned_name, name, base_len);
10423
0
      memcpy (versioned_name + base_len, version,
10424
0
        len - base_len);
10425
0
    }
10426
0
      }
10427
0
  }
10428
0
      else if (flinfo->info->unique_symbol
10429
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10430
0
  {
10431
0
    struct local_hash_entry *lh;
10432
0
    size_t count_len;
10433
0
    size_t base_len;
10434
0
    char buf[30];
10435
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10436
0
      {
10437
0
      case STT_FILE:
10438
0
      case STT_SECTION:
10439
0
        break;
10440
0
      default:
10441
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10442
0
         (&flinfo->local_hash_table, name, true, false);
10443
0
        if (lh == NULL)
10444
0
    return 0;
10445
        /* Always append ".COUNT" to local symbols to avoid
10446
     potential conflicts with local symbol "XXX.COUNT".  */
10447
0
        sprintf (buf, "%lx", lh->count);
10448
0
        base_len = lh->size;
10449
0
        if (!base_len)
10450
0
    {
10451
0
      base_len = strlen (name);
10452
0
      lh->size = base_len;
10453
0
    }
10454
0
        count_len = strlen (buf);
10455
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10456
0
            base_len + count_len + 2);
10457
0
        if (versioned_name == NULL)
10458
0
    return 0;
10459
0
        memcpy (versioned_name, name, base_len);
10460
0
        versioned_name[base_len] = '.';
10461
0
        memcpy (versioned_name + base_len + 1, buf,
10462
0
          count_len + 1);
10463
0
        lh->count++;
10464
0
        break;
10465
0
      }
10466
0
  }
10467
0
      elfsym->st_name
10468
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10469
0
                 versioned_name, false);
10470
0
      if (elfsym->st_name == (unsigned long) -1)
10471
0
  return 0;
10472
0
    }
10473
10474
0
  hash_table = elf_hash_table (flinfo->info);
10475
0
  strtabsize = hash_table->strtabsize;
10476
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10477
0
    {
10478
0
      strtabsize += strtabsize;
10479
0
      hash_table->strtabsize = strtabsize;
10480
0
      strtabsize *= sizeof (*hash_table->strtab);
10481
0
      hash_table->strtab = bfd_realloc (hash_table->strtab, strtabsize);
10482
0
      if (hash_table->strtab == NULL)
10483
0
  return 0;
10484
0
    }
10485
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10486
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10487
0
    = flinfo->output_bfd->symcount;
10488
0
  flinfo->output_bfd->symcount += 1;
10489
10490
0
  return 1;
10491
0
}
10492
10493
/* Swap symbols out to the symbol table and flush the output symbols to
10494
   the file.  */
10495
10496
static bool
10497
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10498
0
{
10499
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10500
0
  size_t amt;
10501
0
  size_t i;
10502
0
  elf_backend_data *bed;
10503
0
  bfd_byte *symbuf;
10504
0
  Elf_Internal_Shdr *hdr;
10505
0
  file_ptr pos;
10506
0
  bool ret;
10507
10508
0
  if (flinfo->output_bfd->symcount == 0)
10509
0
    return true;
10510
10511
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10512
10513
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10514
10515
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10516
0
  symbuf = bfd_malloc (amt);
10517
0
  if (symbuf == NULL)
10518
0
    return false;
10519
10520
0
  if (flinfo->symshndxbuf)
10521
0
    {
10522
0
      amt = sizeof (Elf_External_Sym_Shndx);
10523
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10524
0
      flinfo->symshndxbuf = bfd_zmalloc (amt);
10525
0
      if (flinfo->symshndxbuf == NULL)
10526
0
  {
10527
0
    free (symbuf);
10528
0
    return false;
10529
0
  }
10530
0
    }
10531
10532
  /* Now swap out the symbols.  */
10533
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10534
0
    {
10535
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10536
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10537
0
  elfsym->sym.st_name = 0;
10538
0
      else
10539
0
  elfsym->sym.st_name
10540
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10541
0
                elfsym->sym.st_name);
10542
10543
      /* Inform the linker of the addition of this symbol.  */
10544
10545
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10546
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10547
0
             &elfsym->sym);
10548
10549
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10550
0
             ((bfd_byte *) symbuf
10551
0
        + (elfsym->dest_index
10552
0
           * bed->s->sizeof_sym)),
10553
0
             NPTR_ADD (flinfo->symshndxbuf,
10554
0
           elfsym->dest_index));
10555
0
    }
10556
10557
0
  hdr = &elf_symtab_hdr (flinfo->output_bfd);
10558
0
  pos = hdr->sh_offset + hdr->sh_size;
10559
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10560
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10561
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10562
0
    {
10563
0
      hdr->sh_size += amt;
10564
0
      ret = true;
10565
0
    }
10566
0
  else
10567
0
    ret = false;
10568
10569
0
  free (symbuf);
10570
0
  return ret;
10571
0
}
10572
10573
/* Return TRUE if the dynamic symbol SYM in OBFD is supported.  */
10574
10575
static bool
10576
check_dynsym (bfd *obfd, Elf_Internal_Sym *sym)
10577
0
{
10578
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10579
0
      && sym->st_shndx < SHN_LORESERVE)
10580
0
    {
10581
      /* The gABI doesn't support dynamic symbols in output sections
10582
   beyond 64k.  */
10583
0
      _bfd_error_handler
10584
  /* xgettext:c-format */
10585
0
  (_("%pB: too many sections: %d (>= %d)"),
10586
0
   obfd, bfd_count_sections (obfd), SHN_LORESERVE & 0xffff);
10587
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10588
0
      return false;
10589
0
    }
10590
0
  return true;
10591
0
}
10592
10593
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10594
   allowing an unsatisfied unversioned symbol in the DSO to match a
10595
   versioned symbol that would normally require an explicit version.
10596
   We also handle the case that a DSO references a hidden symbol
10597
   which may be satisfied by a versioned symbol in another DSO.  */
10598
10599
static bool
10600
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10601
         elf_backend_data *bed,
10602
         struct elf_link_hash_entry *h)
10603
0
{
10604
0
  bfd *abfd;
10605
0
  struct elf_link_loaded_list *loaded;
10606
10607
0
  if (!is_elf_hash_table (info->hash))
10608
0
    return false;
10609
10610
  /* Check indirect symbol.  */
10611
0
  while (h->root.type == bfd_link_hash_indirect)
10612
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10613
10614
0
  switch (h->root.type)
10615
0
    {
10616
0
    default:
10617
0
      abfd = NULL;
10618
0
      break;
10619
10620
0
    case bfd_link_hash_undefined:
10621
0
    case bfd_link_hash_undefweak:
10622
0
      abfd = h->root.u.undef.abfd;
10623
0
      if (abfd == NULL
10624
0
    || (abfd->flags & DYNAMIC) == 0
10625
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10626
0
  return false;
10627
0
      break;
10628
10629
0
    case bfd_link_hash_defined:
10630
0
    case bfd_link_hash_defweak:
10631
0
      abfd = h->root.u.def.section->owner;
10632
0
      break;
10633
10634
0
    case bfd_link_hash_common:
10635
0
      abfd = h->root.u.c.p->section->owner;
10636
0
      break;
10637
0
    }
10638
0
  BFD_ASSERT (abfd != NULL);
10639
10640
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10641
0
       loaded != NULL;
10642
0
       loaded = loaded->next)
10643
0
    {
10644
0
      bfd *input;
10645
0
      Elf_Internal_Shdr *hdr;
10646
0
      size_t symcount;
10647
0
      size_t extsymcount;
10648
0
      size_t extsymoff;
10649
0
      Elf_Internal_Shdr *versymhdr;
10650
0
      Elf_Internal_Sym *isym;
10651
0
      Elf_Internal_Sym *isymend;
10652
0
      Elf_Internal_Sym *isymbuf;
10653
0
      Elf_External_Versym *ever;
10654
0
      Elf_External_Versym *extversym;
10655
10656
0
      input = loaded->abfd;
10657
10658
      /* We check each DSO for a possible hidden versioned definition.  */
10659
0
      if (input == abfd
10660
0
    || elf_dynversym (input) == 0)
10661
0
  continue;
10662
10663
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10664
10665
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10666
0
      if (elf_bad_symtab (input))
10667
0
  {
10668
0
    extsymcount = symcount;
10669
0
    extsymoff = 0;
10670
0
  }
10671
0
      else
10672
0
  {
10673
0
    extsymcount = symcount - hdr->sh_info;
10674
0
    extsymoff = hdr->sh_info;
10675
0
  }
10676
10677
0
      if (extsymcount == 0)
10678
0
  continue;
10679
10680
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10681
0
              NULL, NULL, NULL);
10682
0
      if (isymbuf == NULL)
10683
0
  return false;
10684
10685
      /* Read in any version definitions.  */
10686
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10687
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10688
0
    || (extversym = _bfd_malloc_and_read (input, versymhdr->sh_size,
10689
0
            versymhdr->sh_size)) == NULL)
10690
0
  {
10691
0
    free (isymbuf);
10692
0
    return false;
10693
0
  }
10694
10695
0
      ever = extversym + extsymoff;
10696
0
      isymend = isymbuf + extsymcount;
10697
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10698
0
  {
10699
0
    const char *name;
10700
0
    Elf_Internal_Versym iver;
10701
0
    unsigned short version_index;
10702
10703
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10704
0
        || isym->st_shndx == SHN_UNDEF)
10705
0
      continue;
10706
10707
0
    name = bfd_elf_string_from_elf_section (input,
10708
0
              hdr->sh_link,
10709
0
              isym->st_name);
10710
0
    if (strcmp (name, h->root.root.string) != 0)
10711
0
      continue;
10712
10713
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10714
10715
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10716
0
        && !(h->def_regular
10717
0
       && h->forced_local))
10718
0
      {
10719
        /* If we have a non-hidden versioned sym, then it should
10720
     have provided a definition for the undefined sym unless
10721
     it is defined in a non-shared object and forced local.
10722
         */
10723
0
        abort ();
10724
0
      }
10725
10726
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10727
0
    if (version_index == 1 || version_index == 2)
10728
0
      {
10729
        /* This is the base or first version.  We can use it.  */
10730
0
        free (extversym);
10731
0
        free (isymbuf);
10732
0
        return true;
10733
0
      }
10734
0
  }
10735
10736
0
      free (extversym);
10737
0
      free (isymbuf);
10738
0
    }
10739
10740
0
  return false;
10741
0
}
10742
10743
/* Convert ELF common symbol TYPE.  */
10744
10745
static int
10746
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10747
0
{
10748
  /* Commom symbol can only appear in relocatable link.  */
10749
0
  if (!bfd_link_relocatable (info))
10750
0
    abort ();
10751
0
  switch (info->elf_stt_common)
10752
0
    {
10753
0
    case unchanged:
10754
0
      break;
10755
0
    case elf_stt_common:
10756
0
      type = STT_COMMON;
10757
0
      break;
10758
0
    case no_elf_stt_common:
10759
0
      type = STT_OBJECT;
10760
0
      break;
10761
0
    }
10762
0
  return type;
10763
0
}
10764
10765
/* Add an external symbol to the symbol table.  This is called from
10766
   the hash table traversal routine.  When generating a shared object,
10767
   we go through the symbol table twice.  The first time we output
10768
   anything that might have been forced to local scope in a version
10769
   script.  The second time we output the symbols that are still
10770
   global symbols.  */
10771
10772
static bool
10773
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10774
0
{
10775
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10776
0
  struct elf_outext_info *eoinfo = data;
10777
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10778
0
  bool strip;
10779
0
  Elf_Internal_Sym sym;
10780
0
  asection *input_sec;
10781
0
  elf_backend_data *bed;
10782
0
  long indx;
10783
0
  int ret;
10784
0
  unsigned int type;
10785
10786
  /* Skip if base symbol doesn't match.  */
10787
0
  if (eoinfo->base_symbol != !!h->base_symbol)
10788
0
    return true;
10789
10790
0
  if (h->root.type == bfd_link_hash_warning)
10791
0
    {
10792
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10793
0
      if (h->root.type == bfd_link_hash_new)
10794
0
  return true;
10795
0
    }
10796
10797
  /* Decide whether to output this symbol in this pass.  */
10798
0
  if (eoinfo->localsyms)
10799
0
    {
10800
0
      if (!h->forced_local)
10801
0
  return true;
10802
0
    }
10803
0
  else
10804
0
    {
10805
0
      if (h->forced_local)
10806
0
  return true;
10807
0
    }
10808
10809
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10810
10811
0
  if (h->root.type == bfd_link_hash_undefined)
10812
0
    {
10813
      /* If we have an undefined symbol reference here then it must have
10814
   come from a shared library that is being linked in.  (Undefined
10815
   references in regular files have already been handled unless
10816
   they are in unreferenced sections which are removed by garbage
10817
   collection).  */
10818
0
      bool ignore_undef = false;
10819
10820
      /* Some symbols may be special in that the fact that they're
10821
   undefined can be safely ignored - let backend determine that.  */
10822
0
      if (bed->elf_backend_ignore_undef_symbol)
10823
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10824
10825
      /* If we are reporting errors for this situation then do so now.  */
10826
0
      if (!ignore_undef
10827
0
    && h->ref_dynamic_nonweak
10828
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10829
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10830
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10831
0
  {
10832
0
    flinfo->info->callbacks->undefined_symbol
10833
0
      (flinfo->info, h->root.root.string,
10834
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10835
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10836
0
       && !flinfo->info->warn_unresolved_syms);
10837
0
  }
10838
10839
      /* Strip a global symbol defined in a discarded section.  */
10840
0
      if (h->indx == -3)
10841
0
  return true;
10842
0
    }
10843
10844
  /* We should also warn if a forced local symbol is referenced from
10845
     shared libraries.  */
10846
0
  if (bfd_link_executable (flinfo->info)
10847
0
      && h->forced_local
10848
0
      && h->ref_dynamic
10849
0
      && h->def_regular
10850
0
      && !h->dynamic_def
10851
0
      && h->ref_dynamic_nonweak
10852
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10853
0
    {
10854
0
      bfd *def_bfd;
10855
0
      const char *msg;
10856
0
      struct elf_link_hash_entry *hi = h;
10857
10858
      /* Check indirect symbol.  */
10859
0
      while (hi->root.type == bfd_link_hash_indirect)
10860
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10861
10862
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10863
  /* xgettext:c-format */
10864
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10865
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10866
  /* xgettext:c-format */
10867
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10868
0
      else
10869
  /* xgettext:c-format */
10870
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10871
0
      def_bfd = flinfo->output_bfd;
10872
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10873
0
  def_bfd = hi->root.u.def.section->owner;
10874
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10875
0
        h->root.root.string, def_bfd);
10876
0
      bfd_set_error (bfd_error_bad_value);
10877
0
      eoinfo->failed = true;
10878
0
      return false;
10879
0
    }
10880
10881
  /* We don't want to output symbols that have never been mentioned by
10882
     a regular file, or that we have been told to strip.  However, if
10883
     h->indx is set to -2, the symbol is used by a reloc and we must
10884
     output it.  */
10885
0
  strip = false;
10886
0
  if (h->indx == -2)
10887
0
    ;
10888
0
  else if ((h->def_dynamic
10889
0
      || h->ref_dynamic
10890
0
      || h->root.type == bfd_link_hash_new)
10891
0
     && !h->def_regular
10892
0
     && !h->ref_regular)
10893
0
    strip = true;
10894
0
  else if (flinfo->info->strip == strip_all)
10895
0
    strip = true;
10896
0
  else if (flinfo->info->strip == strip_some
10897
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10898
0
             h->root.root.string, false, false) == NULL)
10899
0
    strip = true;
10900
0
  else if ((h->root.type == bfd_link_hash_defined
10901
0
      || h->root.type == bfd_link_hash_defweak)
10902
0
     && ((flinfo->info->strip_discarded
10903
0
    && discarded_section (h->root.u.def.section))
10904
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10905
0
       && h->root.u.def.section->owner != NULL
10906
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10907
0
    strip = true;
10908
0
  else if ((h->root.type == bfd_link_hash_undefined
10909
0
      || h->root.type == bfd_link_hash_undefweak)
10910
0
     && h->root.u.undef.abfd != NULL
10911
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10912
0
    strip = true;
10913
10914
  /* Remember if this symbol should be stripped.  */
10915
0
  bool should_strip = strip;
10916
10917
  /* Strip undefined weak symbols link if they don't have relocation.  */
10918
0
  if (!strip)
10919
0
    strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10920
10921
0
  type = h->type;
10922
10923
  /* If we're stripping it, and it's not a dynamic symbol, there's
10924
     nothing else to do.   However, if it is a forced local symbol or
10925
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10926
     function a chance to make it dynamic.  */
10927
0
  if (strip
10928
0
      && h->dynindx == -1
10929
0
      && type != STT_GNU_IFUNC
10930
0
      && !h->forced_local)
10931
0
    return true;
10932
10933
0
  sym.st_value = 0;
10934
0
  sym.st_size = h->size;
10935
0
  sym.st_other = h->other;
10936
0
  switch (h->root.type)
10937
0
    {
10938
0
    default:
10939
0
    case bfd_link_hash_new:
10940
0
    case bfd_link_hash_warning:
10941
0
      abort ();
10942
0
      return false;
10943
10944
0
    case bfd_link_hash_undefined:
10945
0
    case bfd_link_hash_undefweak:
10946
0
      input_sec = bfd_und_section_ptr;
10947
0
      sym.st_shndx = SHN_UNDEF;
10948
0
      break;
10949
10950
0
    case bfd_link_hash_defined:
10951
0
    case bfd_link_hash_defweak:
10952
0
      {
10953
0
  input_sec = h->root.u.def.section;
10954
0
  if (input_sec->output_section != NULL)
10955
0
    {
10956
0
      sym.st_shndx =
10957
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10958
0
             input_sec->output_section);
10959
0
      if (sym.st_shndx == SHN_BAD)
10960
0
        {
10961
0
    _bfd_error_handler
10962
      /* xgettext:c-format */
10963
0
      (_("%pB: could not find output section %pA for input section %pA"),
10964
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10965
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10966
0
    eoinfo->failed = true;
10967
0
    return false;
10968
0
        }
10969
10970
      /* ELF symbols in relocatable files are section relative,
10971
         but in nonrelocatable files they are virtual
10972
         addresses.  */
10973
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10974
0
      if (!bfd_link_relocatable (flinfo->info))
10975
0
        {
10976
0
    sym.st_value += input_sec->output_section->vma;
10977
0
    if (h->type == STT_TLS)
10978
0
      {
10979
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10980
0
        if (tls_sec != NULL)
10981
0
          sym.st_value -= tls_sec->vma;
10982
0
      }
10983
0
        }
10984
0
    }
10985
0
  else
10986
0
    {
10987
0
      BFD_ASSERT (input_sec->owner == NULL
10988
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10989
0
      sym.st_shndx = SHN_UNDEF;
10990
0
      input_sec = bfd_und_section_ptr;
10991
0
    }
10992
0
      }
10993
0
      break;
10994
10995
0
    case bfd_link_hash_common:
10996
0
      input_sec = h->root.u.c.p->section;
10997
0
      sym.st_shndx = bed->common_section_index (input_sec);
10998
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
10999
0
      break;
11000
11001
0
    case bfd_link_hash_indirect:
11002
      /* These symbols are created by symbol versioning.  They point
11003
   to the decorated version of the name.  For example, if the
11004
   symbol foo@@GNU_1.2 is the default, which should be used when
11005
   foo is used with no version, then we add an indirect symbol
11006
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
11007
   since the indirected symbol is already in the hash table.  */
11008
0
      return true;
11009
0
    }
11010
11011
0
  if (type == STT_COMMON || type == STT_OBJECT)
11012
0
    switch (h->root.type)
11013
0
      {
11014
0
      case bfd_link_hash_common:
11015
0
  type = elf_link_convert_common_type (flinfo->info, type);
11016
0
  break;
11017
0
      case bfd_link_hash_defined:
11018
0
      case bfd_link_hash_defweak:
11019
0
  if (bed->common_definition (&sym))
11020
0
    type = elf_link_convert_common_type (flinfo->info, type);
11021
0
  else
11022
0
    type = STT_OBJECT;
11023
0
  break;
11024
0
      case bfd_link_hash_undefined:
11025
0
      case bfd_link_hash_undefweak:
11026
0
  break;
11027
0
      default:
11028
0
  abort ();
11029
0
      }
11030
11031
0
  if (h->forced_local)
11032
0
    {
11033
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
11034
      /* Turn off visibility on local symbol.  */
11035
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11036
0
    }
11037
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
11038
0
  else if (h->unique_global && h->def_regular)
11039
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
11040
0
  else if (h->root.type == bfd_link_hash_undefweak
11041
0
     || h->root.type == bfd_link_hash_defweak)
11042
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
11043
0
  else
11044
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
11045
0
  sym.st_target_internal = h->target_internal;
11046
11047
  /* Give the processor backend a chance to tweak the symbol value,
11048
     and also to finish up anything that needs to be done for this
11049
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
11050
     forced local syms when non-shared is due to a historical quirk.
11051
     STT_GNU_IFUNC symbol must go through PLT.  */
11052
0
  if (((h->type == STT_GNU_IFUNC
11053
0
  && h->def_regular
11054
0
  && !bfd_link_relocatable (flinfo->info))
11055
0
       || ((h->dynindx != -1
11056
0
      || h->forced_local)
11057
0
     && ((bfd_link_pic (flinfo->info)
11058
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11059
0
        || h->root.type != bfd_link_hash_undefweak))
11060
0
         || !h->forced_local)
11061
0
     && elf_hash_table (flinfo->info)->dynamic_sections_created))
11062
0
      && bed->elf_backend_finish_dynamic_symbol != NULL)
11063
0
    {
11064
0
      if (!bed->elf_backend_finish_dynamic_symbol (flinfo->info, h, &sym))
11065
0
  {
11066
0
    eoinfo->failed = true;
11067
0
    return false;
11068
0
  }
11069
      /* If a symbol is in the dynamic symbol table and isn't a
11070
   should-strip symbol, also keep it in the symbol table.  */
11071
0
      if (!should_strip)
11072
0
  strip = false;
11073
0
    }
11074
11075
  /* If we are marking the symbol as undefined, and there are no
11076
     non-weak references to this symbol from a regular object, then
11077
     mark the symbol as weak undefined; if there are non-weak
11078
     references, mark the symbol as strong.  We can't do this earlier,
11079
     because it might not be marked as undefined until the
11080
     finish_dynamic_symbol routine gets through with it.  */
11081
0
  if (sym.st_shndx == SHN_UNDEF
11082
0
      && h->ref_regular
11083
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11084
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11085
0
    {
11086
0
      int bindtype;
11087
0
      type = ELF_ST_TYPE (sym.st_info);
11088
11089
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11090
0
      if (type == STT_GNU_IFUNC)
11091
0
  type = STT_FUNC;
11092
11093
0
      if (h->ref_regular_nonweak)
11094
0
  bindtype = STB_GLOBAL;
11095
0
      else
11096
0
  bindtype = STB_WEAK;
11097
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
11098
0
    }
11099
11100
  /* If this is a symbol defined in a dynamic library, don't use the
11101
     symbol size from the dynamic library.  Relinking an executable
11102
     against a new library may introduce gratuitous changes in the
11103
     executable's symbols if we keep the size.  */
11104
0
  if (sym.st_shndx == SHN_UNDEF
11105
0
      && !h->def_regular
11106
0
      && h->def_dynamic)
11107
0
    sym.st_size = 0;
11108
11109
  /* If a non-weak symbol with non-default visibility is not defined
11110
     locally, it is a fatal error.  */
11111
0
  if (!bfd_link_relocatable (flinfo->info)
11112
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11113
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
11114
0
      && h->root.type == bfd_link_hash_undefined
11115
0
      && !h->def_regular)
11116
0
    {
11117
0
      const char *msg;
11118
11119
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11120
  /* xgettext:c-format */
11121
0
  msg = _("%pB: protected symbol `%s' isn't defined");
11122
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11123
  /* xgettext:c-format */
11124
0
  msg = _("%pB: internal symbol `%s' isn't defined");
11125
0
      else
11126
  /* xgettext:c-format */
11127
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
11128
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11129
0
      bfd_set_error (bfd_error_bad_value);
11130
0
      eoinfo->failed = true;
11131
0
      return false;
11132
0
    }
11133
11134
  /* If this symbol should be put in the .dynsym section, then put it
11135
     there now.  We already know the symbol index.  We also fill in
11136
     the entry in the .hash section.  */
11137
0
  if (h->dynindx != -1
11138
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
11139
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
11140
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11141
0
    {
11142
0
      bfd_byte *esym;
11143
11144
      /* Since there is no version information in the dynamic string,
11145
   if there is no version info in symbol version section, we will
11146
   have a run-time problem if not linking executable, referenced
11147
   by shared library, or not bound locally.  */
11148
0
      if (h->verinfo.verdef == NULL
11149
0
    && (!bfd_link_executable (flinfo->info)
11150
0
        || h->ref_dynamic
11151
0
        || !h->def_regular))
11152
0
  {
11153
0
    const char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11154
11155
0
    if (p && p [1] != '\0')
11156
0
      {
11157
0
        _bfd_error_handler
11158
    /* xgettext:c-format */
11159
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
11160
0
     flinfo->output_bfd, h->root.root.string);
11161
0
        eoinfo->failed = true;
11162
0
        return false;
11163
0
      }
11164
0
  }
11165
11166
0
      sym.st_name = h->dynstr_index;
11167
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
11168
0
        + h->dynindx * bed->s->sizeof_sym);
11169
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
11170
0
  {
11171
0
    eoinfo->failed = true;
11172
0
    return false;
11173
0
  }
11174
11175
      /* Inform the linker of the addition of this symbol.  */
11176
11177
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
11178
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11179
11180
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11181
11182
0
      if (flinfo->hash_sec != NULL)
11183
0
  {
11184
0
    size_t hash_entry_size;
11185
0
    bfd_byte *bucketpos;
11186
0
    bfd_vma chain;
11187
0
    size_t bucketcount;
11188
0
    size_t bucket;
11189
11190
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11191
0
    bucket = h->u.elf_hash_value % bucketcount;
11192
11193
0
    hash_entry_size
11194
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11195
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11196
0
           + (bucket + 2) * hash_entry_size);
11197
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11198
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11199
0
       bucketpos);
11200
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11201
0
       ((bfd_byte *) flinfo->hash_sec->contents
11202
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11203
0
  }
11204
11205
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11206
0
  {
11207
0
    Elf_Internal_Versym iversym;
11208
0
    Elf_External_Versym *eversym;
11209
11210
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11211
0
      {
11212
0
        if (h->verinfo.verdef == NULL
11213
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11214
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11215
0
    {
11216
0
      iversym.vs_vers = 1;
11217
0
      if (strchr (h->root.root.string, ELF_VER_CHR) == NULL)
11218
        /* Referenced symbol without ELF_VER_CHR has no
11219
           version.  */
11220
0
        iversym.vs_vers = 0;
11221
0
    }
11222
0
        else
11223
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11224
0
      }
11225
0
    else
11226
0
      {
11227
0
        if (h->verinfo.vertree == NULL)
11228
0
    iversym.vs_vers = 1;
11229
0
        else
11230
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11231
0
        if (flinfo->info->create_default_symver)
11232
0
    iversym.vs_vers++;
11233
11234
        /* Turn on VERSYM_HIDDEN only if the hidden versioned
11235
     symbol is defined locally.  */
11236
0
        if (h->versioned == versioned_hidden && h->def_regular)
11237
0
    iversym.vs_vers |= VERSYM_HIDDEN;
11238
0
      }
11239
11240
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11241
0
    eversym += h->dynindx;
11242
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11243
0
  }
11244
0
    }
11245
11246
  /* If the symbol is undefined, and we didn't output it to .dynsym,
11247
     strip it from .symtab too.  Obviously we can't do this for
11248
     relocatable output or when needed for --emit-relocs.  */
11249
0
  else if (input_sec == bfd_und_section_ptr
11250
0
     && h->indx != -2
11251
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
11252
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11253
0
     && !bfd_link_relocatable (flinfo->info))
11254
0
    return true;
11255
11256
  /* Also strip others that we couldn't earlier due to dynamic symbol
11257
     processing.  */
11258
0
  if (strip)
11259
0
    return true;
11260
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
11261
0
    return true;
11262
11263
  /* Output a FILE symbol so that following locals are not associated
11264
     with the wrong input file.  We need one for forced local symbols
11265
     if we've seen more than one FILE symbol or when we have exactly
11266
     one FILE symbol but global symbols are present in a file other
11267
     than the one with the FILE symbol.  We also need one if linker
11268
     defined symbols are present.  In practice these conditions are
11269
     always met, so just emit the FILE symbol unconditionally.  */
11270
0
  if (eoinfo->localsyms
11271
0
      && !eoinfo->file_sym_done
11272
0
      && eoinfo->flinfo->filesym_count != 0)
11273
0
    {
11274
0
      Elf_Internal_Sym fsym;
11275
11276
0
      memset (&fsym, 0, sizeof (fsym));
11277
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11278
0
      fsym.st_shndx = SHN_ABS;
11279
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11280
0
              bfd_und_section_ptr, NULL))
11281
0
  return false;
11282
11283
0
      eoinfo->file_sym_done = true;
11284
0
    }
11285
11286
0
  indx = bfd_get_symcount (flinfo->output_bfd);
11287
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11288
0
           input_sec, h);
11289
0
  if (ret == 0)
11290
0
    {
11291
0
      eoinfo->failed = true;
11292
0
      return false;
11293
0
    }
11294
0
  else if (ret == 1)
11295
0
    h->indx = indx;
11296
0
  else if (h->indx == -2)
11297
0
    abort();
11298
11299
0
  return true;
11300
0
}
11301
11302
/* Return TRUE if special handling is done for relocs in SEC against
11303
   symbols defined in discarded sections.  */
11304
11305
static bool
11306
elf_section_ignore_discarded_relocs (asection *sec)
11307
0
{
11308
0
  elf_backend_data *bed;
11309
11310
0
  switch (sec->sec_info_type)
11311
0
    {
11312
0
    case SEC_INFO_TYPE_STABS:
11313
0
    case SEC_INFO_TYPE_EH_FRAME:
11314
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11315
0
    case SEC_INFO_TYPE_SFRAME:
11316
0
      return true;
11317
0
    default:
11318
0
      break;
11319
0
    }
11320
11321
0
  bed = get_elf_backend_data (sec->owner);
11322
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
11323
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11324
0
    return true;
11325
11326
0
  return false;
11327
0
}
11328
11329
/* Return a mask saying how ld should treat relocations in SEC against
11330
   symbols defined in discarded sections.  If this function returns
11331
   COMPLAIN set, ld will issue a warning message.  If this function
11332
   returns PRETEND set, and the discarded section was link-once and the
11333
   same size as the kept link-once section, ld will pretend that the
11334
   symbol was actually defined in the kept section.  Otherwise ld will
11335
   zero the reloc (at least that is the intent, but some cooperation by
11336
   the target dependent code is needed, particularly for REL targets).  */
11337
11338
unsigned int
11339
_bfd_elf_default_action_discarded (asection *sec)
11340
0
{
11341
0
  if (sec->flags & SEC_DEBUGGING)
11342
0
    return PRETEND;
11343
11344
0
  if (elf_section_type (sec) == SHT_GNU_SFRAME)
11345
0
    return 0;
11346
11347
0
  if (strncmp (sec->name, ".eh_frame", 9) == 0
11348
0
      && (sec->name[9] == 0 || sec->name[9] == '.'))
11349
0
    return 0;
11350
11351
0
  if (strcmp (sec->name, ".gcc_except_table") == 0)
11352
0
    return 0;
11353
11354
0
  return COMPLAIN | PRETEND;
11355
0
}
11356
11357
/* Find a match between a section and a member of a section group.  */
11358
11359
static asection *
11360
match_group_member (asection *sec, asection *group,
11361
        struct bfd_link_info *info)
11362
0
{
11363
0
  asection *first = elf_next_in_group (group);
11364
0
  asection *s = first;
11365
11366
0
  while (s != NULL)
11367
0
    {
11368
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
11369
0
  return s;
11370
11371
0
      s = elf_next_in_group (s);
11372
0
      if (s == first)
11373
0
  break;
11374
0
    }
11375
11376
0
  return NULL;
11377
0
}
11378
11379
/* Check if the kept section of a discarded section SEC can be used
11380
   to replace it.  Return the replacement if it is OK.  Otherwise return
11381
   NULL.  */
11382
11383
asection *
11384
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11385
0
{
11386
0
  asection *kept;
11387
11388
0
  kept = sec->kept_section;
11389
0
  if (kept != NULL)
11390
0
    {
11391
0
      if ((kept->flags & SEC_GROUP) != 0)
11392
0
  kept = match_group_member (sec, kept, info);
11393
0
      if (kept != NULL)
11394
0
  {
11395
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11396
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11397
0
      kept = NULL;
11398
0
    else
11399
0
      {
11400
        /* Get the real kept section.  */
11401
0
        asection *next;
11402
0
        for (next = kept->kept_section;
11403
0
       next != NULL;
11404
0
       next = next->kept_section)
11405
0
    kept = next;
11406
0
      }
11407
0
  }
11408
0
      sec->kept_section = kept;
11409
0
    }
11410
0
  return kept;
11411
0
}
11412
11413
/* Link an input file into the linker output file.  This function
11414
   handles all the sections and relocations of the input file at once.
11415
   This is so that we only have to read the local symbols once, and
11416
   don't have to keep them in memory.  */
11417
11418
static bool
11419
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11420
0
{
11421
0
  int (*relocate_section)
11422
0
    (struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11423
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11424
0
  bfd *output_bfd;
11425
0
  Elf_Internal_Shdr *symtab_hdr;
11426
0
  size_t locsymcount;
11427
0
  size_t extsymoff;
11428
0
  size_t num_sym;
11429
0
  Elf_Internal_Sym *isymbuf;
11430
0
  Elf_Internal_Sym *isym;
11431
0
  Elf_Internal_Sym *isymend;
11432
0
  long *pindex;
11433
0
  asection **ppsection;
11434
0
  asection *o;
11435
0
  elf_backend_data *bed;
11436
0
  struct elf_link_hash_entry **sym_hashes;
11437
0
  bfd_size_type address_size;
11438
0
  bfd_vma r_type_mask;
11439
0
  int r_sym_shift;
11440
0
  bool have_file_sym = false;
11441
11442
0
  output_bfd = flinfo->output_bfd;
11443
0
  bed = get_elf_backend_data (output_bfd);
11444
0
  relocate_section = bed->elf_backend_relocate_section;
11445
11446
  /* If this is a dynamic object, we don't want to do anything here:
11447
     we don't want the local symbols, and we don't want the section
11448
     contents.  */
11449
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11450
0
    return true;
11451
11452
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
11453
0
  num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
11454
0
  if (elf_bad_symtab (input_bfd))
11455
0
    {
11456
0
      locsymcount = num_sym;
11457
0
      extsymoff = 0;
11458
0
    }
11459
0
  else
11460
0
    {
11461
0
      locsymcount = symtab_hdr->sh_info;
11462
0
      extsymoff = symtab_hdr->sh_info;
11463
0
    }
11464
11465
  /* Enable GNU OSABI features in the output BFD that are used in the input
11466
     BFD.  */
11467
0
  if (bed->elf_osabi == ELFOSABI_NONE
11468
0
      || bed->elf_osabi == ELFOSABI_GNU
11469
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11470
0
    elf_tdata (output_bfd)->has_gnu_osabi
11471
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11472
0
    & (bfd_link_relocatable (flinfo->info)
11473
0
       ? -1 : ~elf_gnu_osabi_retain));
11474
11475
  /* Read the local symbols.  */
11476
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11477
0
  if (isymbuf == NULL && locsymcount != 0)
11478
0
    {
11479
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11480
0
              flinfo->internal_syms,
11481
0
              flinfo->external_syms,
11482
0
              flinfo->locsym_shndx);
11483
0
      if (isymbuf == NULL)
11484
0
  return false;
11485
0
    }
11486
11487
  /* Find local symbol sections and adjust values of symbols in
11488
     SEC_MERGE sections.  Write out those local symbols we know are
11489
     going into the output file.  */
11490
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11491
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11492
0
       isym < isymend;
11493
0
       isym++, pindex++, ppsection++)
11494
0
    {
11495
0
      asection *isec;
11496
0
      const char *name;
11497
0
      Elf_Internal_Sym osym;
11498
0
      long indx;
11499
0
      int ret;
11500
11501
0
      *pindex = -1;
11502
11503
0
      if (elf_bad_symtab (input_bfd))
11504
0
  {
11505
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11506
0
      {
11507
0
        *ppsection = NULL;
11508
0
        continue;
11509
0
      }
11510
0
  }
11511
11512
0
      if (isym->st_shndx == SHN_UNDEF)
11513
0
  isec = bfd_und_section_ptr;
11514
0
      else if (isym->st_shndx == SHN_ABS)
11515
0
  isec = bfd_abs_section_ptr;
11516
0
      else if (isym->st_shndx == SHN_COMMON)
11517
0
  isec = bfd_com_section_ptr;
11518
0
      else
11519
0
  {
11520
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11521
0
    if (isec == NULL)
11522
0
      {
11523
        /* Don't attempt to output symbols with st_shnx in the
11524
     reserved range other than SHN_ABS and SHN_COMMON.  */
11525
0
        isec = bfd_und_section_ptr;
11526
0
      }
11527
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11528
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11529
0
      isym->st_value =
11530
0
        _bfd_merged_section_offset (output_bfd, &isec, isym->st_value);
11531
0
  }
11532
11533
0
      *ppsection = isec;
11534
11535
      /* Don't output the first, undefined, symbol.  In fact, don't
11536
   output any undefined local symbol.  */
11537
0
      if (isec == bfd_und_section_ptr)
11538
0
  continue;
11539
11540
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11541
0
  {
11542
    /* We never output section symbols.  Instead, we use the
11543
       section symbol of the corresponding section in the output
11544
       file.  */
11545
0
    continue;
11546
0
  }
11547
11548
      /* If we are stripping all symbols, we don't want to output this
11549
   one.  */
11550
0
      if (flinfo->info->strip == strip_all)
11551
0
  continue;
11552
11553
      /* If we are discarding all local symbols, we don't want to
11554
   output this one.  If we are generating a relocatable output
11555
   file, then some of the local symbols may be required by
11556
   relocs; we output them below as we discover that they are
11557
   needed.  */
11558
0
      if (flinfo->info->discard == discard_all)
11559
0
  continue;
11560
11561
      /* If this symbol is defined in a section which we are
11562
   discarding, we don't need to keep it.  */
11563
0
      if (isym->st_shndx < SHN_LORESERVE
11564
0
    && (isec->output_section == NULL
11565
0
        || bfd_section_removed_from_list (output_bfd,
11566
0
            isec->output_section)))
11567
0
  continue;
11568
11569
      /* Get the name of the symbol.  */
11570
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11571
0
                isym->st_name);
11572
0
      if (name == NULL)
11573
0
  return false;
11574
11575
      /* See if we are discarding symbols with this name.  */
11576
0
      if ((flinfo->info->strip == strip_some
11577
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11578
0
         == NULL))
11579
0
    || (((flinfo->info->discard == discard_sec_merge
11580
0
    && (isec->flags & SEC_MERGE)
11581
0
    && !bfd_link_relocatable (flinfo->info))
11582
0
         || flinfo->info->discard == discard_l)
11583
0
        && bfd_is_local_label_name (input_bfd, name)))
11584
0
  continue;
11585
11586
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11587
0
  {
11588
0
    if (input_bfd->lto_output)
11589
      /* -flto puts a temp file name here.  This means builds
11590
         are not reproducible.  Discard the symbol.  */
11591
0
      continue;
11592
0
    have_file_sym = true;
11593
0
    flinfo->filesym_count += 1;
11594
0
  }
11595
0
      if (!have_file_sym)
11596
0
  {
11597
    /* In the absence of debug info, bfd_find_nearest_line uses
11598
       FILE symbols to determine the source file for local
11599
       function symbols.  Provide a FILE symbol here if input
11600
       files lack such, so that their symbols won't be
11601
       associated with a previous input file.  It's not the
11602
       source file, but the best we can do.  */
11603
0
    const char *filename;
11604
0
    have_file_sym = true;
11605
0
    flinfo->filesym_count += 1;
11606
0
    memset (&osym, 0, sizeof (osym));
11607
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11608
0
    osym.st_shndx = SHN_ABS;
11609
0
    if (input_bfd->lto_output)
11610
0
      filename = NULL;
11611
0
    else
11612
0
      filename = lbasename (bfd_get_filename (input_bfd));
11613
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11614
0
            bfd_abs_section_ptr, NULL))
11615
0
      return false;
11616
0
  }
11617
11618
0
      osym = *isym;
11619
11620
      /* Adjust the section index for the output file.  */
11621
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11622
0
               isec->output_section);
11623
0
      if (osym.st_shndx == SHN_BAD)
11624
0
  return false;
11625
11626
      /* ELF symbols in relocatable files are section relative, but
11627
   in executable files they are virtual addresses.  Note that
11628
   this code assumes that all ELF sections have an associated
11629
   BFD section with a reasonable value for output_offset; below
11630
   we assume that they also have a reasonable value for
11631
   output_section.  Any special sections must be set up to meet
11632
   these requirements.  */
11633
0
      osym.st_value += isec->output_offset;
11634
0
      if (!bfd_link_relocatable (flinfo->info))
11635
0
  {
11636
0
    osym.st_value += isec->output_section->vma;
11637
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11638
0
      {
11639
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11640
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11641
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11642
0
        else
11643
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11644
0
              STT_NOTYPE);
11645
0
      }
11646
0
  }
11647
11648
0
      indx = bfd_get_symcount (output_bfd);
11649
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11650
0
      if (ret == 0)
11651
0
  return false;
11652
0
      else if (ret == 1)
11653
0
  *pindex = indx;
11654
0
    }
11655
11656
0
  if (bed->s->arch_size == 32)
11657
0
    {
11658
0
      r_type_mask = 0xff;
11659
0
      r_sym_shift = 8;
11660
0
      address_size = 4;
11661
0
    }
11662
0
  else
11663
0
    {
11664
0
      r_type_mask = 0xffffffff;
11665
0
      r_sym_shift = 32;
11666
0
      address_size = 8;
11667
0
    }
11668
11669
  /* Relocate the contents of each section.  */
11670
0
  sym_hashes = elf_sym_hashes (input_bfd);
11671
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11672
0
    {
11673
0
      bfd_byte *contents;
11674
11675
0
      if (! o->linker_mark)
11676
0
  {
11677
    /* This section was omitted from the link.  */
11678
0
    continue;
11679
0
  }
11680
11681
0
      if (!flinfo->info->resolve_section_groups
11682
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11683
0
  {
11684
    /* Deal with the group signature symbol.  */
11685
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11686
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11687
0
    asection *osec = o->output_section;
11688
11689
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11690
0
    if (symndx >= locsymcount
11691
0
        || (elf_bad_symtab (input_bfd)
11692
0
      && flinfo->sections[symndx] == NULL))
11693
0
      {
11694
0
        struct elf_link_hash_entry *h;
11695
11696
0
        h = _bfd_elf_get_link_hash_entry (sym_hashes, symndx,
11697
0
            extsymoff, num_sym);
11698
0
        if (h == NULL)
11699
0
    {
11700
0
      _bfd_error_handler
11701
        /* xgettext:c-format */
11702
0
        (_("error: %pB: unable to create group section symbol"),
11703
0
         input_bfd);
11704
0
      bfd_set_error (bfd_error_bad_value);
11705
0
      return false;
11706
0
    }
11707
11708
        /* Arrange for symbol to be output.  */
11709
0
        h->indx = -2;
11710
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11711
0
      }
11712
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11713
0
      {
11714
        /* We'll use the output section target_index.  */
11715
0
        asection *sec = flinfo->sections[symndx]->output_section;
11716
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11717
0
      }
11718
0
    else
11719
0
      {
11720
0
        if (flinfo->indices[symndx] == -1)
11721
0
    {
11722
      /* Otherwise output the local symbol now.  */
11723
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11724
0
      asection *sec = flinfo->sections[symndx]->output_section;
11725
0
      const char *name;
11726
0
      long indx;
11727
0
      int ret;
11728
11729
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11730
0
                symtab_hdr->sh_link,
11731
0
                sym.st_name);
11732
0
      if (name == NULL)
11733
0
        return false;
11734
11735
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11736
0
                    sec);
11737
0
      if (sym.st_shndx == SHN_BAD)
11738
0
        return false;
11739
11740
0
      sym.st_value += o->output_offset;
11741
11742
0
      indx = bfd_get_symcount (output_bfd);
11743
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11744
0
               NULL);
11745
0
      if (ret == 0)
11746
0
        return false;
11747
0
      else if (ret == 1)
11748
0
        flinfo->indices[symndx] = indx;
11749
0
      else
11750
0
        abort ();
11751
0
    }
11752
0
        elf_section_data (osec)->this_hdr.sh_info
11753
0
    = flinfo->indices[symndx];
11754
0
      }
11755
0
  }
11756
11757
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11758
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11759
0
  continue;
11760
11761
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11762
0
  {
11763
    /* Section was created by bfd_elf_link_create_dynamic_sections()
11764
       or somesuch.  */
11765
0
    continue;
11766
0
  }
11767
11768
      /* Get the contents of the section.  They have been cached by a
11769
   relaxation routine.  Note that o is a section in an input
11770
   file, so the contents field will not have been set by any of
11771
   the routines which work on output files.  */
11772
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11773
0
  {
11774
0
    contents = elf_section_data (o)->this_hdr.contents;
11775
0
    if (bed->caches_rawsize
11776
0
        && o->rawsize != 0
11777
0
        && o->rawsize < o->size)
11778
0
      {
11779
0
        memcpy (flinfo->contents, contents, o->rawsize);
11780
0
        contents = flinfo->contents;
11781
0
      }
11782
0
  }
11783
0
      else if (!(o->flags & SEC_RELOC)
11784
0
         && !bed->elf_backend_write_section
11785
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11786
  /* A MERGE section that has no relocations doesn't need the
11787
     contents anymore, they have been recorded earlier.  Except
11788
     if the backend has special provisions for writing sections.  */
11789
0
  contents = NULL;
11790
0
      else
11791
0
  {
11792
0
    contents = flinfo->contents;
11793
0
    if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11794
0
                 &contents))
11795
0
      return false;
11796
0
  }
11797
11798
0
      if ((o->flags & SEC_RELOC) != 0)
11799
0
  {
11800
0
    Elf_Internal_Rela *internal_relocs;
11801
0
    Elf_Internal_Rela *rel, *relend;
11802
0
    int action_discarded;
11803
0
    int ret;
11804
11805
    /* Get the swapped relocs.  */
11806
0
    internal_relocs
11807
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11808
0
                flinfo->external_relocs,
11809
0
                flinfo->internal_relocs,
11810
0
                false);
11811
0
    if (internal_relocs == NULL
11812
0
        && o->reloc_count > 0)
11813
0
      return false;
11814
11815
0
    action_discarded = -1;
11816
0
    if (!elf_section_ignore_discarded_relocs (o))
11817
0
      action_discarded = (*bed->action_discarded) (o);
11818
11819
    /* Run through the relocs evaluating complex reloc symbols and
11820
       looking for relocs against symbols from discarded sections
11821
       or section symbols from removed link-once sections.
11822
       Complain about relocs against discarded sections.  Zero
11823
       relocs against removed link-once sections.  */
11824
11825
0
    rel = internal_relocs;
11826
0
    relend = rel + o->reloc_count;
11827
0
    for ( ; rel < relend; rel++)
11828
0
      {
11829
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11830
0
        unsigned int s_type;
11831
0
        asection **ps, *sec;
11832
0
        struct elf_link_hash_entry *h = NULL;
11833
0
        const char *sym_name;
11834
11835
0
        if (r_symndx == STN_UNDEF)
11836
0
    continue;
11837
11838
0
        if (r_symndx >= locsymcount
11839
0
      || (elf_bad_symtab (input_bfd)
11840
0
          && flinfo->sections[r_symndx] == NULL))
11841
0
    {
11842
0
      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
11843
0
                extsymoff, num_sym);
11844
11845
      /* Badly formatted input files can contain relocs that
11846
         reference non-existant symbols.  Check here so that
11847
         we do not seg fault.  */
11848
0
      if (h == NULL)
11849
0
        {
11850
0
          _bfd_error_handler
11851
      /* xgettext:c-format */
11852
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
11853
0
         "that references a non-existent global symbol"),
11854
0
       input_bfd, (uint64_t) rel->r_info, o);
11855
0
          bfd_set_error (bfd_error_bad_value);
11856
0
          return false;
11857
0
        }
11858
11859
0
      s_type = h->type;
11860
11861
      /* If a plugin symbol is referenced from a non-IR file,
11862
         mark the symbol as undefined.  Note that the
11863
         linker may attach linker created dynamic sections
11864
         to the plugin bfd.  Symbols defined in linker
11865
         created sections are not plugin symbols.  */
11866
0
      if ((h->root.non_ir_ref_regular
11867
0
           || h->root.non_ir_ref_dynamic)
11868
0
          && (h->root.type == bfd_link_hash_defined
11869
0
        || h->root.type == bfd_link_hash_defweak)
11870
0
          && (h->root.u.def.section->flags
11871
0
        & SEC_LINKER_CREATED) == 0
11872
0
          && h->root.u.def.section->owner != NULL
11873
0
          && (h->root.u.def.section->owner->flags
11874
0
        & BFD_PLUGIN) != 0)
11875
0
        {
11876
0
          h->root.type = bfd_link_hash_undefined;
11877
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11878
0
        }
11879
11880
0
      ps = NULL;
11881
0
      if (h->root.type == bfd_link_hash_defined
11882
0
          || h->root.type == bfd_link_hash_defweak)
11883
0
        ps = &h->root.u.def.section;
11884
11885
0
      sym_name = h->root.root.string;
11886
0
    }
11887
0
        else
11888
0
    {
11889
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11890
11891
0
      s_type = ELF_ST_TYPE (sym->st_info);
11892
0
      ps = &flinfo->sections[r_symndx];
11893
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11894
0
                 sym, *ps);
11895
0
    }
11896
11897
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11898
0
      && !bfd_link_relocatable (flinfo->info))
11899
0
    {
11900
0
      bfd_vma val;
11901
0
      bfd_vma dot = (rel->r_offset
11902
0
         + o->output_offset + o->output_section->vma);
11903
#ifdef DEBUG
11904
      printf ("Encountered a complex symbol!");
11905
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11906
        bfd_get_filename (input_bfd), o->name,
11907
        (long) (rel - internal_relocs));
11908
      printf (" symbol: idx  %8.8lx, name %s\n",
11909
        r_symndx, sym_name);
11910
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11911
        (unsigned long) rel->r_info,
11912
        (unsigned long) rel->r_offset);
11913
#endif
11914
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11915
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11916
0
        return false;
11917
11918
      /* Symbol evaluated OK.  Update to absolute value.  */
11919
0
      if (!set_symbol_value (input_bfd, isymbuf, locsymcount,
11920
0
           num_sym, r_symndx, val))
11921
0
        return false;
11922
11923
0
      continue;
11924
0
    }
11925
11926
0
        if (action_discarded != -1 && ps != NULL)
11927
0
    {
11928
      /* Complain if the definition comes from a
11929
         discarded section.  */
11930
0
      if ((sec = *ps) != NULL && discarded_section (sec))
11931
0
        {
11932
0
          BFD_ASSERT (r_symndx != STN_UNDEF);
11933
0
          if (action_discarded & COMPLAIN)
11934
0
      (*flinfo->info->callbacks->einfo)
11935
        /* xgettext:c-format */
11936
0
        (_("%X`%s' referenced in section `%pA' of %pB: "
11937
0
           "defined in discarded section `%pA' of %pB\n"),
11938
0
         sym_name, o, input_bfd, sec, sec->owner);
11939
11940
          /* Try to do the best we can to support buggy old
11941
       versions of gcc.  Pretend that the symbol is
11942
       really defined in the kept linkonce section.
11943
       FIXME: This is quite broken.  Modifying the
11944
       symbol here means we will be changing all later
11945
       uses of the symbol, not just in this section.  */
11946
0
          if (action_discarded & PRETEND)
11947
0
      {
11948
0
        asection *kept;
11949
11950
0
        kept = _bfd_elf_check_kept_section (sec,
11951
0
                    flinfo->info);
11952
0
        if (kept != NULL)
11953
0
          {
11954
0
            *ps = kept;
11955
0
            continue;
11956
0
          }
11957
0
      }
11958
0
        }
11959
0
    }
11960
0
      }
11961
11962
    /* Relocate the section by invoking a back end routine.
11963
11964
       The back end routine is responsible for adjusting the
11965
       section contents as necessary, and (if using Rela relocs
11966
       and generating a relocatable output file) adjusting the
11967
       reloc addend as necessary.
11968
11969
       The back end routine does not have to worry about setting
11970
       the reloc address or the reloc symbol index.
11971
11972
       The back end routine is given a pointer to the swapped in
11973
       internal symbols, and can access the hash table entries
11974
       for the external symbols via elf_sym_hashes (input_bfd).
11975
11976
       When generating relocatable output, the back end routine
11977
       must handle STB_LOCAL/STT_SECTION symbols specially.  The
11978
       output symbol is going to be a section symbol
11979
       corresponding to the output section, which will require
11980
       the addend to be adjusted.  */
11981
11982
0
    ret = relocate_section (flinfo->info,
11983
0
          input_bfd, o, contents,
11984
0
          internal_relocs,
11985
0
          isymbuf,
11986
0
          flinfo->sections);
11987
0
    if (!ret)
11988
0
      return false;
11989
11990
0
    if (ret == 2
11991
0
        || bfd_link_relocatable (flinfo->info)
11992
0
        || flinfo->info->emitrelocations)
11993
0
      {
11994
0
        Elf_Internal_Rela *irela;
11995
0
        Elf_Internal_Rela *irelaend, *irelamid;
11996
0
        bfd_vma last_offset;
11997
0
        struct elf_link_hash_entry **rel_hash;
11998
0
        struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11999
0
        Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
12000
0
        unsigned int next_erel;
12001
0
        bool rela_normal;
12002
0
        struct bfd_elf_section_data *esdi, *esdo;
12003
12004
0
        esdi = elf_section_data (o);
12005
0
        esdo = elf_section_data (o->output_section);
12006
0
        rela_normal = false;
12007
12008
        /* Adjust the reloc addresses and symbol indices.  */
12009
12010
0
        irela = internal_relocs;
12011
0
        irelaend = irela + o->reloc_count;
12012
0
        rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
12013
        /* We start processing the REL relocs, if any.  When we reach
12014
     IRELAMID in the loop, we switch to the RELA relocs.  */
12015
0
        irelamid = irela;
12016
0
        if (esdi->rel.hdr != NULL)
12017
0
    irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
12018
0
           * bed->s->int_rels_per_ext_rel);
12019
0
        rel_hash_list = rel_hash;
12020
0
        rela_hash_list = NULL;
12021
0
        last_offset = o->output_offset;
12022
0
        if (!bfd_link_relocatable (flinfo->info))
12023
0
    last_offset += o->output_section->vma;
12024
0
        for (next_erel = 0; irela < irelaend; irela++, next_erel++)
12025
0
    {
12026
0
      unsigned long r_symndx;
12027
0
      asection *sec;
12028
0
      Elf_Internal_Sym sym;
12029
12030
0
      if (next_erel == bed->s->int_rels_per_ext_rel)
12031
0
        {
12032
0
          rel_hash++;
12033
0
          next_erel = 0;
12034
0
        }
12035
12036
0
      if (irela == irelamid)
12037
0
        {
12038
0
          rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
12039
0
          rela_hash_list = rel_hash;
12040
0
          rela_normal = bed->rela_normal;
12041
0
        }
12042
12043
0
      irela->r_offset = _bfd_elf_section_offset (output_bfd,
12044
0
                   flinfo->info, o,
12045
0
                   irela->r_offset);
12046
0
      if (irela->r_offset >= (bfd_vma) -2)
12047
0
        {
12048
          /* This is a reloc for a deleted entry or somesuch.
12049
       Turn it into an R_*_NONE reloc, at the same
12050
       offset as the last reloc.  elf_eh_frame.c and
12051
       bfd_elf_discard_info rely on reloc offsets
12052
       being ordered.  */
12053
0
          irela->r_offset = last_offset;
12054
0
          irela->r_info = 0;
12055
0
          irela->r_addend = 0;
12056
0
          continue;
12057
0
        }
12058
12059
0
      irela->r_offset += o->output_offset;
12060
12061
      /* Relocs in an executable have to be virtual addresses.  */
12062
0
      if (!bfd_link_relocatable (flinfo->info))
12063
0
        irela->r_offset += o->output_section->vma;
12064
12065
0
      last_offset = irela->r_offset;
12066
12067
0
      r_symndx = irela->r_info >> r_sym_shift;
12068
0
      if (r_symndx == STN_UNDEF)
12069
0
        continue;
12070
12071
0
      if (r_symndx >= locsymcount
12072
0
          || (elf_bad_symtab (input_bfd)
12073
0
        && flinfo->sections[r_symndx] == NULL))
12074
0
        {
12075
0
          struct elf_link_hash_entry *rh;
12076
12077
          /* This is a reloc against a global symbol.  We
12078
       have not yet output all the local symbols, so
12079
       we do not know the symbol index of any global
12080
       symbol.  We set the rel_hash entry for this
12081
       reloc to point to the global hash table entry
12082
       for this symbol.  The symbol index is then
12083
       set at the end of bfd_elf_final_link.  */
12084
0
          rh = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
12085
0
               extsymoff, num_sym);
12086
0
          if (rh == NULL)
12087
0
      {
12088
        /* FIXME: Generate an error ?  */
12089
0
        continue;
12090
0
      }
12091
12092
          /* Setting the index to -2 tells elf_link_output_extsym
12093
       that this symbol is used by a reloc.  */
12094
0
          BFD_ASSERT (rh->indx < 0);
12095
0
          rh->indx = -2;
12096
0
          *rel_hash = rh;
12097
12098
0
          continue;
12099
0
        }
12100
12101
      /* This is a reloc against a local symbol.  */
12102
12103
0
      *rel_hash = NULL;
12104
0
      sym = isymbuf[r_symndx];
12105
0
      sec = flinfo->sections[r_symndx];
12106
0
      if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
12107
0
        {
12108
          /* I suppose the backend ought to fill in the
12109
       section of any STT_SECTION symbol against a
12110
       processor specific section.  */
12111
0
          r_symndx = STN_UNDEF;
12112
0
          if (bfd_is_abs_section (sec))
12113
0
      ;
12114
0
          else if (sec == NULL || sec->owner == NULL)
12115
0
      {
12116
0
        bfd_set_error (bfd_error_bad_value);
12117
0
        return false;
12118
0
      }
12119
0
          else
12120
0
      {
12121
0
        asection *osec = sec->output_section;
12122
12123
        /* If we have discarded a section, the output
12124
           section will be the absolute section.  In
12125
           case of discarded SEC_MERGE sections, use
12126
           the kept section.  relocate_section should
12127
           have already handled discarded linkonce
12128
           sections.  */
12129
0
        if (bfd_is_abs_section (osec)
12130
0
            && sec->kept_section != NULL
12131
0
            && sec->kept_section->output_section != NULL)
12132
0
          {
12133
0
            osec = sec->kept_section->output_section;
12134
0
            irela->r_addend -= osec->vma;
12135
0
          }
12136
12137
0
        if (!bfd_is_abs_section (osec))
12138
0
          {
12139
0
            r_symndx = osec->target_index;
12140
0
            if (r_symndx == STN_UNDEF)
12141
0
        {
12142
0
          irela->r_addend += osec->vma;
12143
0
          osec = flinfo->info->callbacks->nearby_section
12144
0
            (output_bfd, osec, osec->vma);
12145
0
          irela->r_addend -= osec->vma;
12146
0
          r_symndx = osec->target_index;
12147
0
        }
12148
0
          }
12149
0
      }
12150
12151
          /* Adjust the addend according to where the
12152
       section winds up in the output section.  */
12153
0
          if (rela_normal)
12154
0
      irela->r_addend += sec->output_offset;
12155
0
        }
12156
0
      else
12157
0
        {
12158
0
          if (flinfo->indices[r_symndx] == -1)
12159
0
      {
12160
0
        unsigned long shlink;
12161
0
        const char *name;
12162
0
        asection *osec;
12163
0
        long indx;
12164
12165
0
        if (flinfo->info->strip == strip_all)
12166
0
          {
12167
            /* You can't do ld -r -s.  */
12168
0
            bfd_set_error (bfd_error_invalid_operation);
12169
0
            return false;
12170
0
          }
12171
12172
        /* This symbol was skipped earlier, but
12173
           since it is needed by a reloc, we
12174
           must output it now.  */
12175
0
        shlink = symtab_hdr->sh_link;
12176
0
        name = (bfd_elf_string_from_elf_section
12177
0
          (input_bfd, shlink, sym.st_name));
12178
0
        if (name == NULL)
12179
0
          return false;
12180
12181
0
        osec = sec->output_section;
12182
0
        sym.st_shndx =
12183
0
          _bfd_elf_section_from_bfd_section (output_bfd,
12184
0
                     osec);
12185
0
        if (sym.st_shndx == SHN_BAD)
12186
0
          return false;
12187
12188
0
        sym.st_value += sec->output_offset;
12189
0
        if (!bfd_link_relocatable (flinfo->info))
12190
0
          {
12191
0
            sym.st_value += osec->vma;
12192
0
            if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12193
0
        {
12194
0
          struct elf_link_hash_table *htab
12195
0
            = elf_hash_table (flinfo->info);
12196
12197
          /* STT_TLS symbols are relative to PT_TLS
12198
             segment base.  */
12199
0
          if (htab->tls_sec != NULL)
12200
0
            sym.st_value -= htab->tls_sec->vma;
12201
0
          else
12202
0
            sym.st_info
12203
0
              = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12204
0
                 STT_NOTYPE);
12205
0
        }
12206
0
          }
12207
12208
0
        indx = bfd_get_symcount (output_bfd);
12209
0
        ret = elf_link_output_symstrtab (flinfo, name,
12210
0
                 &sym, sec,
12211
0
                 NULL);
12212
0
        if (ret == 0)
12213
0
          return false;
12214
0
        else if (ret == 1)
12215
0
          flinfo->indices[r_symndx] = indx;
12216
0
        else
12217
0
          abort ();
12218
0
      }
12219
12220
0
          r_symndx = flinfo->indices[r_symndx];
12221
0
        }
12222
12223
0
      irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12224
0
           | (irela->r_info & r_type_mask));
12225
0
    }
12226
12227
        /* Swap out the relocs.  */
12228
0
        input_rel_hdr = esdi->rel.hdr;
12229
0
        if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12230
0
    {
12231
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12232
0
                 input_rel_hdr,
12233
0
                 internal_relocs,
12234
0
                 rel_hash_list))
12235
0
        return false;
12236
0
      internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12237
0
              * bed->s->int_rels_per_ext_rel);
12238
0
      rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12239
0
    }
12240
12241
0
        input_rela_hdr = esdi->rela.hdr;
12242
0
        if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12243
0
    {
12244
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12245
0
                 input_rela_hdr,
12246
0
                 internal_relocs,
12247
0
                 rela_hash_list))
12248
0
        return false;
12249
0
    }
12250
0
      }
12251
0
  }
12252
12253
      /* Write out the modified section contents.  */
12254
0
      if (bed->elf_backend_write_section
12255
0
    && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12256
0
            contents))
12257
0
  {
12258
    /* Section written out.  */
12259
0
  }
12260
0
      else switch (o->sec_info_type)
12261
0
  {
12262
0
  case SEC_INFO_TYPE_STABS:
12263
0
    if (! (_bfd_write_section_stabs
12264
0
     (output_bfd,
12265
0
      &elf_hash_table (flinfo->info)->stab_info, o, contents)))
12266
0
      return false;
12267
0
    break;
12268
0
  case SEC_INFO_TYPE_MERGE:
12269
0
    if (! _bfd_write_merged_section (output_bfd, o))
12270
0
      return false;
12271
0
    break;
12272
0
  case SEC_INFO_TYPE_EH_FRAME:
12273
0
    {
12274
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12275
0
               o, contents))
12276
0
        return false;
12277
0
    }
12278
0
    break;
12279
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12280
0
    {
12281
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12282
0
               flinfo->info,
12283
0
               o, contents))
12284
0
        return false;
12285
0
    }
12286
0
    break;
12287
0
  case SEC_INFO_TYPE_SFRAME:
12288
0
      {
12289
        /* Merge SFrame section into the SFrame encoder context of the
12290
     output_bfd's section.  The final .sframe output section will
12291
     be written out later.  */
12292
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12293
0
              o, contents))
12294
0
    return false;
12295
0
      }
12296
0
      break;
12297
0
  default:
12298
0
    {
12299
0
      if (! (o->flags & SEC_EXCLUDE))
12300
0
        {
12301
0
    file_ptr offset = (file_ptr) o->output_offset;
12302
0
    bfd_size_type todo = o->size;
12303
12304
0
    offset *= bfd_octets_per_byte (output_bfd, o);
12305
12306
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
12307
0
        && o->size > address_size)
12308
0
      {
12309
        /* Reverse-copy input section to output.  */
12310
12311
0
        if ((o->size & (address_size - 1)) != 0
12312
0
      || (o->reloc_count != 0
12313
0
          && (o->size * bed->s->int_rels_per_ext_rel
12314
0
        != o->reloc_count * address_size)))
12315
0
          {
12316
0
      _bfd_error_handler
12317
        /* xgettext:c-format */
12318
0
        (_("error: %pB: size of section %pA is not "
12319
0
           "multiple of address size"),
12320
0
         input_bfd, o);
12321
0
      bfd_set_error (bfd_error_bad_value);
12322
0
      return false;
12323
0
          }
12324
12325
0
        do
12326
0
          {
12327
0
      todo -= address_size;
12328
0
      if (! bfd_set_section_contents (output_bfd,
12329
0
              o->output_section,
12330
0
              contents + todo,
12331
0
              offset,
12332
0
              address_size))
12333
0
        return false;
12334
0
      if (todo == 0)
12335
0
        break;
12336
0
      offset += address_size;
12337
0
          }
12338
0
        while (1);
12339
0
      }
12340
0
    else if (! bfd_set_section_contents (output_bfd,
12341
0
                 o->output_section,
12342
0
                 contents,
12343
0
                 offset, todo))
12344
0
      return false;
12345
0
        }
12346
0
    }
12347
0
    break;
12348
0
  }
12349
12350
      /* Munmap the section contents for each input section.  */
12351
0
      _bfd_elf_link_munmap_section_contents (o);
12352
0
    }
12353
12354
0
  return true;
12355
0
}
12356
12357
/* Generate a reloc when linking an ELF file.  This is a reloc
12358
   requested by the linker, and does not come from any input file.  This
12359
   is used to build constructor and destructor tables when linking
12360
   with -Ur.  */
12361
12362
static bool
12363
elf_reloc_link_order (bfd *output_bfd,
12364
          struct bfd_link_info *info,
12365
          asection *output_section,
12366
          struct bfd_link_order *link_order)
12367
0
{
12368
0
  reloc_howto_type *howto;
12369
0
  long indx;
12370
0
  bfd_vma offset;
12371
0
  bfd_vma addend;
12372
0
  struct bfd_elf_section_reloc_data *reldata;
12373
0
  struct elf_link_hash_entry **rel_hash_ptr;
12374
0
  Elf_Internal_Shdr *rel_hdr;
12375
0
  elf_backend_data *bed = get_elf_backend_data (output_bfd);
12376
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12377
0
  bfd_byte *erel;
12378
0
  unsigned int i;
12379
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12380
12381
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12382
0
  if (howto == NULL)
12383
0
    {
12384
0
      bfd_set_error (bfd_error_bad_value);
12385
0
      return false;
12386
0
    }
12387
12388
0
  addend = link_order->u.reloc.p->addend;
12389
12390
0
  if (esdo->rel.hdr)
12391
0
    reldata = &esdo->rel;
12392
0
  else if (esdo->rela.hdr)
12393
0
    reldata = &esdo->rela;
12394
0
  else
12395
0
    {
12396
0
      reldata = NULL;
12397
0
      BFD_ASSERT (0);
12398
0
    }
12399
12400
  /* Figure out the symbol index.  */
12401
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12402
0
  if (link_order->type == bfd_section_reloc_link_order)
12403
0
    {
12404
0
      indx = link_order->u.reloc.p->u.section->target_index;
12405
0
      BFD_ASSERT (indx != 0);
12406
0
      *rel_hash_ptr = NULL;
12407
0
    }
12408
0
  else
12409
0
    {
12410
0
      struct elf_link_hash_entry *h;
12411
12412
      /* Treat a reloc against a defined symbol as though it were
12413
   actually against the section.  */
12414
0
      h = ((struct elf_link_hash_entry *)
12415
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12416
0
           link_order->u.reloc.p->u.name,
12417
0
           false, false, true));
12418
0
      if (h != NULL
12419
0
    && (h->root.type == bfd_link_hash_defined
12420
0
        || h->root.type == bfd_link_hash_defweak))
12421
0
  {
12422
0
    asection *section;
12423
12424
0
    section = h->root.u.def.section;
12425
0
    indx = section->output_section->target_index;
12426
0
    *rel_hash_ptr = NULL;
12427
    /* It seems that we ought to add the symbol value to the
12428
       addend here, but in practice it has already been added
12429
       because it was passed to constructor_callback.  */
12430
0
    addend += section->output_section->vma + section->output_offset;
12431
0
  }
12432
0
      else if (h != NULL)
12433
0
  {
12434
    /* Setting the index to -2 tells elf_link_output_extsym that
12435
       this symbol is used by a reloc.  */
12436
0
    h->indx = -2;
12437
0
    *rel_hash_ptr = h;
12438
0
    indx = 0;
12439
0
  }
12440
0
      else
12441
0
  {
12442
0
    (*info->callbacks->unattached_reloc)
12443
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12444
0
    indx = 0;
12445
0
  }
12446
0
    }
12447
12448
  /* If this is an inplace reloc, we must write the addend into the
12449
     object file.  */
12450
0
  if (howto->partial_inplace && addend != 0)
12451
0
    {
12452
0
      bfd_size_type size;
12453
0
      bfd_reloc_status_type rstat;
12454
0
      bfd_byte *buf;
12455
0
      bool ok;
12456
0
      const char *sym_name;
12457
0
      bfd_size_type octets;
12458
12459
0
      size = bfd_get_reloc_size (howto);
12460
0
      buf = bfd_zmalloc (size);
12461
0
      if (buf == NULL && size != 0)
12462
0
  return false;
12463
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12464
0
      switch (rstat)
12465
0
  {
12466
0
  case bfd_reloc_ok:
12467
0
    break;
12468
12469
0
  default:
12470
0
  case bfd_reloc_outofrange:
12471
0
    abort ();
12472
12473
0
  case bfd_reloc_overflow:
12474
0
    if (link_order->type == bfd_section_reloc_link_order)
12475
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12476
0
    else
12477
0
      sym_name = link_order->u.reloc.p->u.name;
12478
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12479
0
                howto->name, addend, NULL, NULL,
12480
0
                (bfd_vma) 0);
12481
0
    break;
12482
0
  }
12483
12484
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12485
0
               output_section);
12486
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12487
0
             octets, size);
12488
0
      free (buf);
12489
0
      if (! ok)
12490
0
  return false;
12491
0
    }
12492
12493
  /* The address of a reloc is relative to the section in a
12494
     relocatable file, and is a virtual address in an executable
12495
     file.  */
12496
0
  offset = link_order->offset;
12497
0
  if (! bfd_link_relocatable (info))
12498
0
    offset += output_section->vma;
12499
12500
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12501
0
    {
12502
0
      irel[i].r_offset = offset;
12503
0
      irel[i].r_info = 0;
12504
0
      irel[i].r_addend = 0;
12505
0
    }
12506
0
  if (bed->s->arch_size == 32)
12507
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12508
0
  else
12509
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12510
12511
0
  rel_hdr = reldata->hdr;
12512
0
  erel = rel_hdr->contents;
12513
0
  if (rel_hdr->sh_type == SHT_REL)
12514
0
    {
12515
0
      erel += reldata->count * bed->s->sizeof_rel;
12516
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12517
0
    }
12518
0
  else
12519
0
    {
12520
0
      irel[0].r_addend = addend;
12521
0
      erel += reldata->count * bed->s->sizeof_rela;
12522
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12523
0
    }
12524
12525
0
  ++reldata->count;
12526
12527
0
  return true;
12528
0
}
12529
12530
/* Generate an import library in INFO->implib_bfd from symbols in OBFD.
12531
   Returns TRUE upon success, FALSE otherwise.  */
12532
12533
static bool
12534
elf_output_implib (bfd *obfd, struct bfd_link_info *info)
12535
0
{
12536
0
  bool ret = false;
12537
0
  bfd *implib_bfd;
12538
0
  elf_backend_data *obed;
12539
0
  flagword flags;
12540
0
  enum bfd_architecture arch;
12541
0
  unsigned int mach;
12542
0
  asymbol **sympp = NULL;
12543
0
  long symsize;
12544
0
  long symcount;
12545
0
  long src_count;
12546
0
  elf_symbol_type *osymbuf;
12547
12548
0
  implib_bfd = info->out_implib_bfd;
12549
0
  obed = get_elf_backend_data (obfd);
12550
12551
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12552
0
    return false;
12553
12554
  /* Use flag from executable but make it a relocatable object.  */
12555
0
  flags = bfd_get_file_flags (obfd);
12556
0
  flags &= ~HAS_RELOC;
12557
0
  if (!bfd_set_start_address (implib_bfd, 0)
12558
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12559
0
    return false;
12560
12561
  /* Copy architecture of output file to import library file.  */
12562
0
  arch = bfd_get_arch (obfd);
12563
0
  mach = bfd_get_mach (obfd);
12564
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12565
0
      && (obfd->target_defaulted
12566
0
    || bfd_get_arch (obfd) != bfd_get_arch (implib_bfd)))
12567
0
    return false;
12568
12569
  /* Get symbol table size.  */
12570
0
  symsize = bfd_get_symtab_upper_bound (obfd);
12571
0
  if (symsize < 0)
12572
0
    return false;
12573
12574
  /* Read in the symbol table.  */
12575
0
  sympp = bfd_malloc (symsize);
12576
0
  if (sympp == NULL)
12577
0
    return false;
12578
12579
0
  symcount = bfd_canonicalize_symtab (obfd, sympp);
12580
0
  if (symcount < 0)
12581
0
    goto free_sym_buf;
12582
12583
  /* Allow the BFD backend to copy any private header data it
12584
     understands from the output BFD to the import library BFD.  */
12585
0
  if (! bfd_copy_private_header_data (obfd, implib_bfd))
12586
0
    goto free_sym_buf;
12587
12588
  /* Filter symbols to appear in the import library.  */
12589
0
  symcount = obed->elf_backend_filter_implib_symbols (info, sympp, symcount);
12590
0
  if (symcount == 0)
12591
0
    {
12592
0
      bfd_set_error (bfd_error_no_symbols);
12593
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12594
0
        implib_bfd);
12595
0
      goto free_sym_buf;
12596
0
    }
12597
12598
  /* Make symbols absolute.  */
12599
0
  osymbuf = bfd_alloc (implib_bfd, symcount * sizeof (*osymbuf));
12600
0
  if (osymbuf == NULL)
12601
0
    goto free_sym_buf;
12602
12603
0
  for (src_count = 0; src_count < symcount; src_count++)
12604
0
    {
12605
0
      memcpy (&osymbuf[src_count], sympp[src_count], sizeof (*osymbuf));
12606
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12607
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12608
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12609
0
      osymbuf[src_count].internal_elf_sym.st_value =
12610
0
  osymbuf[src_count].symbol.value;
12611
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12612
0
    }
12613
12614
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12615
12616
  /* Allow the BFD backend to copy any private data it understands
12617
     from the output BFD to the import library BFD.  This is done last
12618
     to permit the routine to look at the filtered symbol table.  */
12619
0
  if (! bfd_copy_private_bfd_data (obfd, implib_bfd))
12620
0
    goto free_sym_buf;
12621
12622
0
  if (!bfd_close (implib_bfd))
12623
0
    goto free_sym_buf;
12624
12625
0
  ret = true;
12626
12627
0
 free_sym_buf:
12628
0
  free (sympp);
12629
0
  return ret;
12630
0
}
12631
12632
static void
12633
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12634
0
{
12635
0
  asection *o;
12636
12637
0
  if (flinfo->symstrtab != NULL)
12638
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12639
0
  free (flinfo->contents);
12640
0
  free (flinfo->external_relocs);
12641
0
  free (flinfo->internal_relocs);
12642
0
  free (flinfo->external_syms);
12643
0
  free (flinfo->locsym_shndx);
12644
0
  free (flinfo->internal_syms);
12645
0
  free (flinfo->indices);
12646
0
  free (flinfo->sections);
12647
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12648
0
    free (flinfo->symshndxbuf);
12649
0
  for (o = obfd->sections; o != NULL; o = o->next)
12650
0
    {
12651
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12652
0
      free (esdo->rel.hashes);
12653
0
      free (esdo->rela.hashes);
12654
0
    }
12655
0
}
12656
12657
/* Do the final step of an ELF link.  */
12658
12659
bool
12660
_bfd_elf_final_link (bfd *obfd, struct bfd_link_info *info)
12661
0
{
12662
0
  bool dynamic;
12663
0
  bool emit_relocs;
12664
0
  bfd *dynobj;
12665
0
  struct elf_final_link_info flinfo;
12666
0
  asection *o;
12667
0
  struct bfd_link_order *p;
12668
0
  bfd *sub;
12669
0
  bfd_size_type max_contents_size;
12670
0
  bfd_size_type max_external_reloc_size;
12671
0
  bfd_size_type max_internal_reloc_count;
12672
0
  bfd_size_type max_sym_count;
12673
0
  bfd_size_type max_sym_shndx_count;
12674
0
  Elf_Internal_Sym elfsym;
12675
0
  unsigned int i;
12676
0
  Elf_Internal_Shdr *symtab_hdr;
12677
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12678
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
12679
0
  struct elf_outext_info eoinfo;
12680
0
  bool merged;
12681
0
  size_t relativecount;
12682
0
  size_t relr_entsize;
12683
0
  asection *reldyn = 0;
12684
0
  bfd_size_type amt;
12685
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12686
0
  bool sections_removed;
12687
12688
0
  if (!is_elf_hash_table (&htab->root))
12689
0
    return false;
12690
12691
0
  if (bfd_link_pic (info))
12692
0
    obfd->flags |= DYNAMIC;
12693
12694
0
  dynamic = htab->dynamic_sections_created;
12695
0
  dynobj = htab->dynobj;
12696
12697
0
  emit_relocs = (bfd_link_relocatable (info)
12698
0
     || info->emitrelocations);
12699
12700
0
  memset (&flinfo, 0, sizeof (flinfo));
12701
0
  flinfo.info = info;
12702
0
  flinfo.output_bfd = obfd;
12703
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12704
0
  if (flinfo.symstrtab == NULL)
12705
0
    return false;
12706
12707
0
  if (! dynamic)
12708
0
    {
12709
0
      flinfo.hash_sec = NULL;
12710
0
      flinfo.symver_sec = NULL;
12711
0
    }
12712
0
  else
12713
0
    {
12714
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12715
      /* Note that dynsym_sec can be NULL (on VMS).  */
12716
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12717
      /* Note that it is OK if symver_sec is NULL.  */
12718
0
    }
12719
12720
0
  if (info->unique_symbol
12721
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12722
0
             local_hash_newfunc,
12723
0
             sizeof (struct local_hash_entry)))
12724
0
    return false;
12725
12726
  /* The object attributes have been merged.  Remove the input
12727
     sections from the link, and set the contents of the output
12728
     section.  */
12729
0
  sections_removed = false;
12730
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12731
0
  const char *obj_attrs_section = get_elf_backend_data (obfd)->obj_attrs_section;
12732
0
#endif
12733
0
  for (o = obfd->sections; o != NULL; o = o->next)
12734
0
    {
12735
0
      bool remove_section = false;
12736
12737
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12738
0
      if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
12739
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12740
0
  {
12741
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12742
0
      {
12743
0
        asection *input_section;
12744
12745
0
        if (p->type != bfd_indirect_link_order)
12746
0
    continue;
12747
0
        input_section = p->u.indirect.section;
12748
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12749
     elf_link_input_bfd ignores this section.  */
12750
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12751
0
      }
12752
12753
    /* Skip this section later on.  */
12754
0
    o->map_head.link_order = NULL;
12755
12756
0
    bfd_vma attr_size = bfd_elf_obj_attr_size (obfd);
12757
    /* Once ELF headers have been written, the size of a section is
12758
       frozen. We need to set the size of the attribute section before
12759
       _bfd_elf_compute_section_file_positions.  */
12760
0
    bfd_set_section_size (o, attr_size);
12761
0
    if (attr_size > 0)
12762
0
      elf_obj_object_attributes (obfd) = o;
12763
0
    else
12764
0
      remove_section = true;
12765
0
  }
12766
0
      else
12767
0
#endif /* OBJ_MAYBE_ELF_ATTRIBUTES */
12768
0
      if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12769
0
  {
12770
    /* Remove empty group section from linker output.  */
12771
0
    remove_section = true;
12772
0
  }
12773
0
      if (remove_section)
12774
0
  {
12775
0
    o->flags |= SEC_EXCLUDE;
12776
0
    bfd_section_list_remove (obfd, o);
12777
0
    obfd->section_count--;
12778
0
    sections_removed = true;
12779
0
  }
12780
0
    }
12781
0
  if (sections_removed)
12782
0
    bfd_fix_excluded_sec_syms (info);
12783
12784
  /* Count up the number of relocations we will output for each output
12785
     section, so that we know the sizes of the reloc sections.  We
12786
     also figure out some maximum sizes.  */
12787
0
#ifdef USE_MMAP
12788
0
  if (obed->use_mmap)
12789
0
    {
12790
      /* Mmap is used only if section size >= the minimum mmap section
12791
   size.  The initial max_contents_size value covers all sections
12792
   smaller than the minimum mmap section size.  It may be increased
12793
   for compressed or linker created sections or sections whose
12794
   rawsize != size.  max_external_reloc_size covers all relocation
12795
   sections smaller than the minimum mmap section size.  */
12796
0
      max_contents_size = _bfd_minimum_mmap_size;
12797
0
      max_external_reloc_size = _bfd_minimum_mmap_size;
12798
0
    }
12799
0
  else
12800
0
#endif
12801
0
    {
12802
0
      max_contents_size = 0;
12803
0
      max_external_reloc_size = 0;
12804
0
    }
12805
0
  max_internal_reloc_count = 0;
12806
0
  max_sym_count = 0;
12807
0
  max_sym_shndx_count = 0;
12808
0
  merged = false;
12809
0
  for (o = obfd->sections; o != NULL; o = o->next)
12810
0
    {
12811
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12812
0
      bfd_size_type size_align = 1;
12813
12814
0
      if (o->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
12815
0
  {
12816
    /* eh_frame editing can extend last FDE to cover padding
12817
       between one section and the next.  */
12818
0
    size_align = (((bfd_size_type) 1 << o->alignment_power)
12819
0
      * bfd_octets_per_byte (obfd, o));
12820
0
  }
12821
12822
0
      o->reloc_count = 0;
12823
12824
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12825
0
  {
12826
0
    unsigned int reloc_count = 0;
12827
0
    unsigned int additional_reloc_count = 0;
12828
0
    struct bfd_elf_section_data *esdi = NULL;
12829
12830
0
    if (p->type == bfd_section_reloc_link_order
12831
0
        || p->type == bfd_symbol_reloc_link_order)
12832
0
      reloc_count = 1;
12833
0
    else if (p->type == bfd_indirect_link_order)
12834
0
      {
12835
0
        asection *sec;
12836
12837
0
        sec = p->u.indirect.section;
12838
12839
        /* Mark all sections which are to be included in the
12840
     link.  This will normally be every section.  We need
12841
     to do this so that we can identify any sections which
12842
     the linker has decided to not include.  */
12843
0
        sec->linker_mark = true;
12844
12845
0
        if (sec->flags & SEC_MERGE)
12846
0
    merged = true;
12847
12848
0
#ifdef USE_MMAP
12849
        /* Mmap is used only on non-compressed, non-linker created
12850
     sections whose rawsize == size.  */
12851
0
        if (!obed->use_mmap
12852
0
      || sec->compress_status != COMPRESS_SECTION_NONE
12853
0
      || (sec->flags & SEC_LINKER_CREATED) != 0
12854
0
      || sec->rawsize != sec->size)
12855
0
#endif
12856
0
    {
12857
0
      bfd_size_type size = (sec->rawsize > sec->size
12858
0
          ? sec->rawsize : sec->size);
12859
0
      size = (size + size_align - 1) & -size_align;
12860
0
      if (max_contents_size < size)
12861
0
        max_contents_size = size;
12862
0
    }
12863
12864
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12865
0
      && (sec->owner->flags & DYNAMIC) == 0)
12866
0
    {
12867
0
      size_t sym_count;
12868
12869
      /* We are interested in just local symbols, not all
12870
         symbols.  */
12871
0
      if (elf_bad_symtab (sec->owner))
12872
0
        sym_count = (elf_symtab_hdr (sec->owner).sh_size
12873
0
         / obed->s->sizeof_sym);
12874
0
      else
12875
0
        sym_count = elf_symtab_hdr (sec->owner).sh_info;
12876
12877
0
      if (sym_count > max_sym_count)
12878
0
        max_sym_count = sym_count;
12879
12880
0
      if (sym_count > max_sym_shndx_count
12881
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12882
0
        max_sym_shndx_count = sym_count;
12883
12884
0
      esdi = elf_section_data (sec);
12885
12886
0
      if (esdi->this_hdr.sh_type == SHT_REL
12887
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12888
        /* Some backends use reloc_count in relocation sections
12889
           to count particular types of relocs.  Of course,
12890
           reloc sections themselves can't have relocations.  */
12891
0
        ;
12892
0
      else if (emit_relocs)
12893
0
        {
12894
0
          reloc_count = sec->reloc_count;
12895
0
          if (obed->elf_backend_count_additional_relocs)
12896
0
      {
12897
0
        int c;
12898
0
        c = obed->elf_backend_count_additional_relocs (sec);
12899
0
        additional_reloc_count += c;
12900
0
      }
12901
0
        }
12902
0
      else if (obed->elf_backend_count_relocs)
12903
0
        reloc_count = obed->elf_backend_count_relocs (info, sec);
12904
12905
0
      if ((sec->flags & SEC_RELOC) != 0)
12906
0
        {
12907
0
#ifdef USE_MMAP
12908
0
          if (!obed->use_mmap)
12909
0
#endif
12910
0
      {
12911
0
        size_t ext_size = 0;
12912
12913
0
        if (esdi->rel.hdr != NULL)
12914
0
          ext_size = esdi->rel.hdr->sh_size;
12915
0
        if (esdi->rela.hdr != NULL)
12916
0
          ext_size += esdi->rela.hdr->sh_size;
12917
12918
0
        if (ext_size > max_external_reloc_size)
12919
0
          max_external_reloc_size = ext_size;
12920
0
      }
12921
0
          if (sec->reloc_count > max_internal_reloc_count)
12922
0
      max_internal_reloc_count = sec->reloc_count;
12923
0
        }
12924
0
    }
12925
0
      }
12926
12927
0
    if (reloc_count == 0)
12928
0
      continue;
12929
12930
0
    reloc_count += additional_reloc_count;
12931
0
    o->reloc_count += reloc_count;
12932
12933
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12934
0
      {
12935
0
        if (esdi->rel.hdr)
12936
0
    {
12937
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12938
0
      esdo->rel.count += additional_reloc_count;
12939
0
    }
12940
0
        if (esdi->rela.hdr)
12941
0
    {
12942
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12943
0
      esdo->rela.count += additional_reloc_count;
12944
0
    }
12945
0
      }
12946
0
    else
12947
0
      {
12948
0
        if (o->use_rela_p)
12949
0
    esdo->rela.count += reloc_count;
12950
0
        else
12951
0
    esdo->rel.count += reloc_count;
12952
0
      }
12953
0
  }
12954
12955
0
      if (o->reloc_count > 0)
12956
0
  o->flags |= SEC_RELOC;
12957
0
      else
12958
0
  {
12959
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12960
       set it (this is probably a bug) and if it is set
12961
       assign_section_numbers will create a reloc section.  */
12962
0
    o->flags &=~ SEC_RELOC;
12963
0
  }
12964
12965
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12966
   zero.  This is done in elf_fake_sections as well, but forcing
12967
   the VMA to 0 here will ensure that relocs against these
12968
   sections are handled correctly.  */
12969
0
      if ((o->flags & SEC_ALLOC) == 0
12970
0
    && ! o->user_set_vma)
12971
0
  o->vma = 0;
12972
0
    }
12973
12974
0
  if (! bfd_link_relocatable (info) && merged)
12975
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, obfd);
12976
12977
  /* Figure out the file positions for everything but the symbol table
12978
     and the relocs.  We set symcount to force assign_section_numbers
12979
     to create a symbol table.  */
12980
0
  obfd->symcount = info->strip != strip_all || emit_relocs;
12981
0
  BFD_ASSERT (! obfd->output_has_begun);
12982
0
  if (! _bfd_elf_compute_section_file_positions (obfd, info))
12983
0
    goto error_return;
12984
12985
  /* Set sizes, and assign file positions for reloc sections.  */
12986
0
  for (o = obfd->sections; o != NULL; o = o->next)
12987
0
    {
12988
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12989
0
      if ((o->flags & SEC_RELOC) != 0)
12990
0
  {
12991
0
    if (esdo->rel.hdr
12992
0
        && !(_bfd_elf_link_size_reloc_section (obfd, &esdo->rel)))
12993
0
      goto error_return;
12994
12995
0
    if (esdo->rela.hdr
12996
0
        && !(_bfd_elf_link_size_reloc_section (obfd, &esdo->rela)))
12997
0
      goto error_return;
12998
0
  }
12999
13000
      /* _bfd_elf_compute_section_file_positions makes temporary use
13001
   of target_index.  Reset it.  */
13002
0
      o->target_index = 0;
13003
13004
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
13005
   to count upwards while actually outputting the relocations.  */
13006
0
      esdo->rel.count = 0;
13007
0
      esdo->rela.count = 0;
13008
13009
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
13010
0
    && !bfd_section_is_ctf (o))
13011
0
  {
13012
    /* Cache the section contents so that they can be compressed
13013
       later.  Use bfd_malloc since it will be freed by
13014
       bfd_compress_section_contents.  */
13015
0
    unsigned char *contents = esdo->this_hdr.contents;
13016
0
    if (contents != NULL)
13017
0
      abort ();
13018
0
    contents = bfd_malloc (esdo->this_hdr.sh_size);
13019
0
    if (contents == NULL)
13020
0
      goto error_return;
13021
0
    esdo->this_hdr.contents = contents;
13022
0
  }
13023
0
    }
13024
13025
  /* We have now assigned file positions for all the sections except .symtab,
13026
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
13027
     the .symtab section at the current file position, and write directly to it.
13028
     We build the .strtab section in memory.  */
13029
0
  obfd->symcount = 0;
13030
0
  symtab_hdr = &elf_symtab_hdr (obfd);
13031
  /* sh_name is set in prep_headers.  */
13032
0
  symtab_hdr->sh_type = SHT_SYMTAB;
13033
  /* sh_flags, sh_addr and sh_size all start off zero.  */
13034
0
  symtab_hdr->sh_entsize = obed->s->sizeof_sym;
13035
  /* sh_link is set in assign_section_numbers.  */
13036
  /* sh_info is set below.  */
13037
  /* sh_offset is set just below.  */
13038
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << obed->s->log_file_align;
13039
13040
0
  if (max_sym_count < 20)
13041
0
    max_sym_count = 20;
13042
0
  htab->strtabsize = max_sym_count;
13043
0
  htab->strtab = bfd_malloc (max_sym_count * sizeof (*htab->strtab));
13044
0
  if (htab->strtab == NULL)
13045
0
    goto error_return;
13046
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
13047
0
  flinfo.symshndxbuf
13048
0
    = (elf_numsections (obfd) > (SHN_LORESERVE & 0xFFFF)
13049
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
13050
13051
0
  if (info->strip != strip_all || emit_relocs)
13052
0
    {
13053
0
      file_ptr off = elf_next_file_pos (obfd);
13054
13055
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
13056
13057
      /* Note that at this point elf_next_file_pos (obfd) is
13058
   incorrect.  We do not yet know the size of the .symtab section.
13059
   We correct next_file_pos below, after we do know the size.  */
13060
13061
      /* Start writing out the symbol table.  The first symbol is always a
13062
   dummy symbol.  */
13063
0
      elfsym.st_value = 0;
13064
0
      elfsym.st_size = 0;
13065
0
      elfsym.st_info = 0;
13066
0
      elfsym.st_other = 0;
13067
0
      elfsym.st_shndx = SHN_UNDEF;
13068
0
      elfsym.st_target_internal = 0;
13069
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13070
0
             bfd_und_section_ptr, NULL) != 1)
13071
0
  goto error_return;
13072
13073
      /* Output a symbol for each section if asked or they are used for
13074
   relocs.  These symbols usually have no names.  We store the
13075
   index of each one in the index field of the section, so that
13076
   we can find it again when outputting relocs.  */
13077
13078
0
      if (bfd_keep_unused_section_symbols (obfd) || emit_relocs)
13079
0
  {
13080
0
    bool name_local_sections
13081
0
      = (obed->elf_backend_name_local_section_symbols
13082
0
         && obed->elf_backend_name_local_section_symbols (obfd));
13083
0
    const char *name = NULL;
13084
13085
0
    elfsym.st_size = 0;
13086
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13087
0
    elfsym.st_other = 0;
13088
0
    elfsym.st_value = 0;
13089
0
    elfsym.st_target_internal = 0;
13090
0
    for (i = 1; i < elf_numsections (obfd); i++)
13091
0
      {
13092
0
        o = bfd_section_from_elf_index (obfd, i);
13093
0
        if (o != NULL)
13094
0
    {
13095
0
      o->target_index = bfd_get_symcount (obfd);
13096
0
      elfsym.st_shndx = i;
13097
0
      if (!bfd_link_relocatable (info))
13098
0
        elfsym.st_value = o->vma;
13099
0
      if (name_local_sections)
13100
0
        name = o->name;
13101
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13102
0
             NULL) != 1)
13103
0
        goto error_return;
13104
0
    }
13105
0
      }
13106
0
  }
13107
0
    }
13108
13109
  /* On some targets like Irix 5 the symbol split between local and global
13110
     ones recorded in the sh_info field needs to be done between section
13111
     and all other symbols.  */
13112
0
  if (obed->elf_backend_elfsym_local_is_section
13113
0
      && obed->elf_backend_elfsym_local_is_section (obfd))
13114
0
    symtab_hdr->sh_info = bfd_get_symcount (obfd);
13115
13116
  /* Allocate some memory to hold information read in from the input
13117
     files.  */
13118
0
  if (max_contents_size != 0)
13119
0
    {
13120
0
      flinfo.contents = bfd_malloc (max_contents_size);
13121
0
      if (flinfo.contents == NULL)
13122
0
  goto error_return;
13123
0
    }
13124
13125
0
  if (max_external_reloc_size != 0)
13126
0
    {
13127
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13128
0
      if (flinfo.external_relocs == NULL)
13129
0
  goto error_return;
13130
0
    }
13131
13132
0
  if (max_internal_reloc_count != 0)
13133
0
    {
13134
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13135
0
      flinfo.internal_relocs = bfd_malloc (amt);
13136
0
      if (flinfo.internal_relocs == NULL)
13137
0
  goto error_return;
13138
0
    }
13139
13140
0
  if (max_sym_count != 0)
13141
0
    {
13142
0
      amt = max_sym_count * obed->s->sizeof_sym;
13143
0
      flinfo.external_syms = bfd_malloc (amt);
13144
0
      if (flinfo.external_syms == NULL)
13145
0
  goto error_return;
13146
13147
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
13148
0
      flinfo.internal_syms = bfd_malloc (amt);
13149
0
      if (flinfo.internal_syms == NULL)
13150
0
  goto error_return;
13151
13152
0
      amt = max_sym_count * sizeof (long);
13153
0
      flinfo.indices = bfd_malloc (amt);
13154
0
      if (flinfo.indices == NULL)
13155
0
  goto error_return;
13156
13157
0
      amt = max_sym_count * sizeof (asection *);
13158
0
      flinfo.sections = bfd_malloc (amt);
13159
0
      if (flinfo.sections == NULL)
13160
0
  goto error_return;
13161
0
    }
13162
13163
0
  if (max_sym_shndx_count != 0)
13164
0
    {
13165
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13166
0
      flinfo.locsym_shndx = bfd_malloc (amt);
13167
0
      if (flinfo.locsym_shndx == NULL)
13168
0
  goto error_return;
13169
0
    }
13170
13171
0
  if (htab->tls_sec)
13172
0
    {
13173
0
      bfd_vma base, end = 0;  /* Both bytes.  */
13174
0
      asection *sec;
13175
13176
0
      for (sec = htab->tls_sec;
13177
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
13178
0
     sec = sec->next)
13179
0
  {
13180
0
    bfd_size_type size = sec->size;
13181
0
    unsigned int opb = bfd_octets_per_byte (obfd, sec);
13182
13183
0
    if (size == 0
13184
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
13185
0
      {
13186
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
13187
13188
0
        if (ord != NULL)
13189
0
    size = ord->offset * opb + ord->size;
13190
0
      }
13191
0
    end = sec->vma + size / opb;
13192
0
  }
13193
0
      base = htab->tls_sec->vma;
13194
      /* Only align end of TLS section if static TLS doesn't have special
13195
   alignment requirements.  */
13196
0
      if (obed->static_tls_alignment == 1)
13197
0
  end = align_power (end, htab->tls_sec->alignment_power);
13198
0
      htab->tls_size = end - base;
13199
0
    }
13200
13201
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
13202
0
    return false;
13203
13204
  /* Finish relative relocations here after regular symbol processing
13205
     is finished if DT_RELR is enabled.  */
13206
0
  if (info->enable_dt_relr
13207
0
      && obed->finish_relative_relocs
13208
0
      && !obed->finish_relative_relocs (info))
13209
0
    info->callbacks->fatal
13210
0
      (_("%P: %pB: failed to finish relative relocations\n"), obfd);
13211
13212
  /* Since ELF permits relocations to be against local symbols, we
13213
     must have the local symbols available when we do the relocations.
13214
     Since we would rather only read the local symbols once, and we
13215
     would rather not keep them in memory, we handle all the
13216
     relocations for a single input file at the same time.
13217
13218
     Unfortunately, there is no way to know the total number of local
13219
     symbols until we have seen all of them, and the local symbol
13220
     indices precede the global symbol indices.  This means that when
13221
     we are generating relocatable output, and we see a reloc against
13222
     a global symbol, we can not know the symbol index until we have
13223
     finished examining all the local symbols to see which ones we are
13224
     going to output.  To deal with this, we keep the relocations in
13225
     memory, and don't output them until the end of the link.  This is
13226
     an unfortunate waste of memory, but I don't see a good way around
13227
     it.  Fortunately, it only happens when performing a relocatable
13228
     link, which is not the common case.  FIXME: If keep_memory is set
13229
     we could write the relocs out and then read them again; I don't
13230
     know how bad the memory loss will be.  */
13231
13232
0
  for (o = obfd->sections; o != NULL; o = o->next)
13233
0
    {
13234
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
13235
0
  {
13236
0
    if (p->type == bfd_indirect_link_order
13237
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13238
0
      == bfd_target_elf_flavour)
13239
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == obed->s->elfclass)
13240
0
      {
13241
0
        if (! sub->output_has_begun)
13242
0
    {
13243
0
      sub->output_has_begun = true;
13244
0
      if ((sub->flags & BFD_LINKER_CREATED) == 0
13245
0
          && !elf_link_input_bfd (&flinfo, sub))
13246
0
        goto error_return;
13247
0
    }
13248
0
      }
13249
0
    else if (p->type == bfd_section_reloc_link_order
13250
0
       || p->type == bfd_symbol_reloc_link_order)
13251
0
      {
13252
0
        if (! elf_reloc_link_order (obfd, info, o, p))
13253
0
    goto error_return;
13254
0
      }
13255
0
    else
13256
0
      {
13257
0
        if (! _bfd_default_link_order (obfd, info, o, p))
13258
0
    {
13259
0
      if (p->type == bfd_indirect_link_order
13260
0
          && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13261
0
        == bfd_target_elf_flavour)
13262
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
13263
0
        != obed->s->elfclass))
13264
0
        {
13265
0
          const char *iclass, *oclass;
13266
13267
0
          switch (obed->s->elfclass)
13268
0
      {
13269
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
13270
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
13271
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13272
0
      default: abort ();
13273
0
      }
13274
13275
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13276
0
      {
13277
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
13278
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
13279
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13280
0
      default: abort ();
13281
0
      }
13282
13283
0
          bfd_set_error (bfd_error_wrong_format);
13284
0
          _bfd_error_handler
13285
      /* xgettext:c-format */
13286
0
      (_("%pB: file class %s incompatible with %s"),
13287
0
       sub, iclass, oclass);
13288
0
        }
13289
13290
0
      goto error_return;
13291
0
    }
13292
0
      }
13293
0
  }
13294
0
    }
13295
  /* Writing of linker created BFDs is left until last, because the
13296
     aarch64 backend wants to copy insns from a relocated section to
13297
     a stub section.  See erratum_843419 code.  */
13298
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13299
0
    if (sub->output_has_begun && (sub->flags & BFD_LINKER_CREATED) != 0)
13300
0
      {
13301
0
  if (!elf_link_input_bfd (&flinfo, sub))
13302
0
    goto error_return;
13303
0
      }
13304
13305
  /* Free symbol buffer if needed.  */
13306
0
  if (!info->reduce_memory_overheads)
13307
0
    {
13308
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13309
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13310
0
    {
13311
0
      free (elf_tdata (sub)->symbuf);
13312
0
      elf_tdata (sub)->symbuf = NULL;
13313
0
    }
13314
0
    }
13315
13316
  /* Output any global symbols that got converted to local in a
13317
     version script or due to symbol visibility.  We do this in a
13318
     separate step since ELF requires all local symbols to appear
13319
     prior to any global symbols.  FIXME: We should only do this if
13320
     some global symbols were, in fact, converted to become local.
13321
     FIXME: Will this work correctly with the Irix 5 linker?  */
13322
0
  eoinfo.failed = false;
13323
0
  eoinfo.flinfo = &flinfo;
13324
0
  eoinfo.localsyms = true;
13325
0
  eoinfo.file_sym_done = false;
13326
  /* Output non-base symbols first.  */
13327
0
  eoinfo.base_symbol = false;
13328
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13329
0
  if (eoinfo.failed)
13330
0
    goto error_return;
13331
13332
  /* If backend needs to output some local symbols not present in the hash
13333
     table, do it now.  */
13334
0
  if (obed->elf_backend_output_arch_local_syms)
13335
0
    {
13336
0
      if (!obed->elf_backend_output_arch_local_syms (info, &flinfo,
13337
0
                 elf_link_output_symstrtab))
13338
0
  goto error_return;
13339
0
    }
13340
13341
  /* That wrote out all the local symbols.  Finish up the symbol table
13342
     with the global symbols. Even if we want to strip everything we
13343
     can, we still need to deal with those global symbols that got
13344
     converted to local in a version script.  */
13345
13346
  /* The sh_info field records the index of the first non local symbol.  */
13347
0
  if (!symtab_hdr->sh_info)
13348
0
    symtab_hdr->sh_info = bfd_get_symcount (obfd);
13349
13350
0
  if (dynamic
13351
0
      && htab->dynsym != NULL
13352
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
13353
0
    {
13354
0
      Elf_Internal_Sym sym;
13355
0
      bfd_byte *dynsym = htab->dynsym->contents;
13356
13357
0
      o = htab->dynsym->output_section;
13358
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13359
13360
      /* Write out the section symbols for the output sections.  */
13361
0
      if (bfd_link_pic (info)
13362
0
    || htab->is_relocatable_executable)
13363
0
  {
13364
0
    asection *s;
13365
13366
0
    sym.st_size = 0;
13367
0
    sym.st_name = 0;
13368
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13369
0
    sym.st_other = 0;
13370
0
    sym.st_target_internal = 0;
13371
13372
0
    for (s = obfd->sections; s != NULL; s = s->next)
13373
0
      {
13374
0
        int indx;
13375
0
        bfd_byte *dest;
13376
0
        long dynindx;
13377
13378
0
        dynindx = elf_section_data (s)->dynindx;
13379
0
        if (dynindx <= 0)
13380
0
    continue;
13381
0
        indx = elf_section_data (s)->this_idx;
13382
0
        BFD_ASSERT (indx > 0);
13383
0
        sym.st_shndx = indx;
13384
0
        if (! check_dynsym (obfd, &sym))
13385
0
    goto error_return;
13386
0
        sym.st_value = s->vma;
13387
0
        dest = dynsym + dynindx * obed->s->sizeof_sym;
13388
13389
        /* Inform the linker of the addition of this symbol.  */
13390
13391
0
        if (info->callbacks->ctf_new_dynsym)
13392
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
13393
13394
0
        obed->s->swap_symbol_out (obfd, &sym, dest, 0);
13395
0
      }
13396
0
  }
13397
13398
      /* Write out the local dynsyms.  */
13399
0
      if (htab->dynlocal)
13400
0
  {
13401
0
    struct elf_link_local_dynamic_entry *e;
13402
0
    for (e = htab->dynlocal; e ; e = e->next)
13403
0
      {
13404
0
        asection *s;
13405
0
        bfd_byte *dest;
13406
13407
        /* Copy the internal symbol and turn off visibility.
13408
     Note that we saved a word of storage and overwrote
13409
     the original st_name with the dynstr_index.  */
13410
0
        sym = e->isym;
13411
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13412
0
        sym.st_shndx = SHN_UNDEF;
13413
13414
0
        s = bfd_section_from_elf_index (e->input_bfd,
13415
0
                e->isym.st_shndx);
13416
0
        if (s != NULL
13417
0
      && s->output_section != NULL
13418
0
      && elf_section_data (s->output_section) != NULL)
13419
0
    {
13420
0
      sym.st_shndx =
13421
0
        elf_section_data (s->output_section)->this_idx;
13422
0
      if (! check_dynsym (obfd, &sym))
13423
0
        goto error_return;
13424
0
      sym.st_value = (s->output_section->vma
13425
0
          + s->output_offset
13426
0
          + e->isym.st_value);
13427
0
    }
13428
13429
        /* Inform the linker of the addition of this symbol.  */
13430
13431
0
        if (info->callbacks->ctf_new_dynsym)
13432
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13433
13434
0
        dest = dynsym + e->dynindx * obed->s->sizeof_sym;
13435
0
        obed->s->swap_symbol_out (obfd, &sym, dest, 0);
13436
0
      }
13437
0
  }
13438
0
    }
13439
13440
  /* We get the global symbols from the hash table.  */
13441
0
  eoinfo.failed = false;
13442
0
  eoinfo.localsyms = false;
13443
0
  eoinfo.flinfo = &flinfo;
13444
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13445
0
  if (eoinfo.failed)
13446
0
    goto error_return;
13447
13448
0
  if (htab->has_base_symbols)
13449
0
    {
13450
      /* Output base symbols last in DT_HASH so that they will be picked
13451
   before non-base symbols at run-time.  */
13452
0
      eoinfo.base_symbol = true;
13453
0
      bfd_hash_traverse (&info->hash->table, elf_link_output_extsym,
13454
0
       &eoinfo);
13455
0
      if (eoinfo.failed)
13456
0
  goto error_return;
13457
0
    }
13458
13459
  /* If backend needs to output some symbols not present in the hash
13460
     table, do it now.  */
13461
0
  if (obed->elf_backend_output_arch_syms
13462
0
      && (info->strip != strip_all || emit_relocs))
13463
0
    {
13464
0
      if (!obed->elf_backend_output_arch_syms (info, &flinfo,
13465
0
                 elf_link_output_symstrtab))
13466
0
  goto error_return;
13467
0
    }
13468
13469
  /* Finalize the .strtab section.  */
13470
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13471
13472
  /* Swap out the .strtab section. */
13473
0
  if (!elf_link_swap_symbols_out (&flinfo))
13474
0
    goto error_return;
13475
0
  free (htab->strtab);
13476
0
  htab->strtab = NULL;
13477
13478
  /* Now we know the size of the symtab section.  */
13479
0
  if (bfd_get_symcount (obfd) > 0)
13480
0
    {
13481
      /* Finish up and write out the symbol string table (.strtab)
13482
   section.  */
13483
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13484
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13485
13486
0
      if (elf_symtab_shndx_list (obfd))
13487
0
  {
13488
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (obfd)->hdr;
13489
13490
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13491
0
      {
13492
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13493
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13494
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13495
0
        amt = bfd_get_symcount (obfd) * sizeof (Elf_External_Sym_Shndx);
13496
0
        symtab_shndx_hdr->sh_size = amt;
13497
13498
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13499
0
                     off, true, 0);
13500
13501
0
        if (bfd_seek (obfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13502
0
      || (bfd_write (flinfo.symshndxbuf, amt, obfd) != amt))
13503
0
    goto error_return;
13504
0
      }
13505
0
  }
13506
13507
0
      symstrtab_hdr = &elf_tdata (obfd)->strtab_hdr;
13508
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13509
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13510
0
      symstrtab_hdr->sh_addralign = 1;
13511
13512
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13513
0
                   off, true, 0);
13514
0
      elf_next_file_pos (obfd) = off;
13515
13516
0
      if (bfd_seek (obfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13517
0
    || ! _bfd_elf_strtab_emit (obfd, flinfo.symstrtab))
13518
0
  goto error_return;
13519
0
    }
13520
13521
0
  if (info->out_implib_bfd && !elf_output_implib (obfd, info))
13522
0
    {
13523
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13524
0
        info->out_implib_bfd);
13525
0
      goto error_return;
13526
0
    }
13527
13528
  /* Adjust the relocs to have the correct symbol indices.  */
13529
0
  for (o = obfd->sections; o != NULL; o = o->next)
13530
0
    {
13531
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13532
0
      bool sort;
13533
13534
0
      if ((o->flags & SEC_RELOC) == 0)
13535
0
  continue;
13536
13537
0
      sort = obed->sort_relocs_p == NULL || obed->sort_relocs_p (o);
13538
0
      if (esdo->rel.hdr != NULL
13539
0
    && !elf_link_adjust_relocs (obfd, o, &esdo->rel, sort, info))
13540
0
  goto error_return;
13541
0
      if (esdo->rela.hdr != NULL
13542
0
    && !elf_link_adjust_relocs (obfd, o, &esdo->rela, sort, info))
13543
0
  goto error_return;
13544
13545
      /* Set the reloc_count field to 0 to prevent write_relocs from
13546
   trying to swap the relocs out itself.  */
13547
0
      o->reloc_count = 0;
13548
0
    }
13549
13550
0
  relativecount = 0;
13551
0
  if (dynamic && info->combreloc && dynobj != NULL)
13552
0
    relativecount = elf_link_sort_relocs (obfd, info, &reldyn);
13553
13554
0
  relr_entsize = 0;
13555
0
  if (htab->srelrdyn != NULL
13556
0
      && htab->srelrdyn->output_section != NULL
13557
0
      && htab->srelrdyn->size != 0)
13558
0
    {
13559
0
      asection *s = htab->srelrdyn->output_section;
13560
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13561
0
      if (relr_entsize == 0)
13562
0
  {
13563
0
    relr_entsize = obed->s->arch_size / 8;
13564
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13565
0
  }
13566
0
    }
13567
13568
  /* If we are linking against a dynamic object, or generating a
13569
     shared library, finish up the dynamic linking information.  */
13570
0
  if (dynamic)
13571
0
    {
13572
0
      bfd_byte *dyncon, *dynconend;
13573
13574
      /* Fix up .dynamic entries.  */
13575
0
      o = htab->dynamic;
13576
0
      BFD_ASSERT (o != NULL);
13577
13578
0
      dyncon = o->contents;
13579
0
      dynconend = PTR_ADD (o->contents, o->size);
13580
0
      for (; dyncon < dynconend; dyncon += obed->s->sizeof_dyn)
13581
0
  {
13582
0
    Elf_Internal_Dyn dyn;
13583
0
    const char *name;
13584
0
    unsigned int type;
13585
0
    bfd_size_type sh_size;
13586
0
    bfd_vma sh_addr;
13587
13588
0
    obed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13589
13590
0
    switch (dyn.d_tag)
13591
0
      {
13592
0
      default:
13593
0
        continue;
13594
0
      case DT_NULL:
13595
0
        if (relativecount != 0)
13596
0
    {
13597
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13598
0
        {
13599
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13600
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13601
0
        }
13602
0
      if (dyn.d_tag != DT_NULL
13603
0
          && dynconend - dyncon >= obed->s->sizeof_dyn)
13604
0
        {
13605
0
          dyn.d_un.d_val = relativecount;
13606
0
          relativecount = 0;
13607
0
          break;
13608
0
        }
13609
0
      relativecount = 0;
13610
0
    }
13611
0
        if (relr_entsize != 0)
13612
0
    {
13613
0
      if (dynconend - dyncon >= 3 * obed->s->sizeof_dyn)
13614
0
        {
13615
0
          asection *s = htab->srelrdyn;
13616
0
          dyn.d_tag = DT_RELR;
13617
0
          dyn.d_un.d_ptr
13618
0
      = s->output_section->vma + s->output_offset;
13619
0
          obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13620
0
          dyncon += obed->s->sizeof_dyn;
13621
13622
0
          dyn.d_tag = DT_RELRSZ;
13623
0
          dyn.d_un.d_val = s->size;
13624
0
          obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13625
0
          dyncon += obed->s->sizeof_dyn;
13626
13627
0
          dyn.d_tag = DT_RELRENT;
13628
0
          dyn.d_un.d_val = relr_entsize;
13629
0
          relr_entsize = 0;
13630
0
          break;
13631
0
        }
13632
0
      relr_entsize = 0;
13633
0
    }
13634
0
        continue;
13635
13636
0
      case DT_INIT:
13637
0
        name = info->init_function;
13638
0
        goto get_sym;
13639
0
      case DT_FINI:
13640
0
        name = info->fini_function;
13641
0
      get_sym:
13642
0
        {
13643
0
    struct elf_link_hash_entry *h;
13644
13645
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13646
0
    if (h != NULL
13647
0
        && (h->root.type == bfd_link_hash_defined
13648
0
      || h->root.type == bfd_link_hash_defweak))
13649
0
      {
13650
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13651
0
        o = h->root.u.def.section;
13652
0
        if (o->output_section != NULL)
13653
0
          dyn.d_un.d_ptr += (o->output_section->vma
13654
0
           + o->output_offset);
13655
0
        else
13656
0
          {
13657
      /* The symbol is imported from another shared
13658
         library and does not apply to this one.  */
13659
0
      dyn.d_un.d_ptr = 0;
13660
0
          }
13661
0
        break;
13662
0
      }
13663
0
        }
13664
0
        continue;
13665
13666
0
      case DT_PREINIT_ARRAYSZ:
13667
0
        name = ".preinit_array";
13668
0
        goto get_out_size;
13669
0
      case DT_INIT_ARRAYSZ:
13670
0
        name = ".init_array";
13671
0
        goto get_out_size;
13672
0
      case DT_FINI_ARRAYSZ:
13673
0
        name = ".fini_array";
13674
0
      get_out_size:
13675
0
        o = bfd_get_section_by_name (obfd, name);
13676
0
        if (o == NULL)
13677
0
    {
13678
0
      _bfd_error_handler
13679
0
        (_("could not find section %s"), name);
13680
0
      goto error_return;
13681
0
    }
13682
0
        if (o->size == 0)
13683
0
    _bfd_error_handler
13684
0
      (_("warning: %s section has zero size"), name);
13685
0
        dyn.d_un.d_val = o->size;
13686
0
        break;
13687
13688
0
      case DT_PREINIT_ARRAY:
13689
0
        name = ".preinit_array";
13690
0
        goto get_out_vma;
13691
0
      case DT_INIT_ARRAY:
13692
0
        name = ".init_array";
13693
0
        goto get_out_vma;
13694
0
      case DT_FINI_ARRAY:
13695
0
        name = ".fini_array";
13696
0
      get_out_vma:
13697
0
        o = bfd_get_section_by_name (obfd, name);
13698
0
        goto do_vma;
13699
13700
0
      case DT_HASH:
13701
0
        name = ".hash";
13702
0
        goto get_vma;
13703
0
      case DT_GNU_HASH:
13704
0
        name = ".gnu.hash";
13705
0
        goto get_vma;
13706
0
      case DT_STRTAB:
13707
0
        name = ".dynstr";
13708
0
        goto get_vma;
13709
0
      case DT_SYMTAB:
13710
0
        name = ".dynsym";
13711
0
        goto get_vma;
13712
0
      case DT_VERDEF:
13713
0
        name = ".gnu.version_d";
13714
0
        goto get_vma;
13715
0
      case DT_VERNEED:
13716
0
        name = ".gnu.version_r";
13717
0
        goto get_vma;
13718
0
      case DT_VERSYM:
13719
0
        name = ".gnu.version";
13720
0
      get_vma:
13721
0
        o = bfd_get_linker_section (dynobj, name);
13722
0
      do_vma:
13723
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13724
0
    {
13725
0
      _bfd_error_handler
13726
0
        (_("could not find section %s"), name);
13727
0
      goto error_return;
13728
0
    }
13729
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13730
0
    {
13731
0
      _bfd_error_handler
13732
0
        (_("warning: section '%s' is being made into a note"), name);
13733
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13734
0
      goto error_return;
13735
0
    }
13736
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13737
0
        break;
13738
13739
0
      case DT_REL:
13740
0
      case DT_RELA:
13741
0
      case DT_RELSZ:
13742
0
      case DT_RELASZ:
13743
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13744
0
    type = SHT_REL;
13745
0
        else
13746
0
    type = SHT_RELA;
13747
0
        sh_size = 0;
13748
0
        sh_addr = 0;
13749
0
        for (i = 1; i < elf_numsections (obfd); i++)
13750
0
    {
13751
0
      Elf_Internal_Shdr *hdr;
13752
13753
0
      hdr = elf_elfsections (obfd)[i];
13754
0
      if (hdr->sh_type == type
13755
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13756
0
        {
13757
0
          sh_size += hdr->sh_size;
13758
0
          if (sh_addr == 0
13759
0
        || sh_addr > hdr->sh_addr)
13760
0
      sh_addr = hdr->sh_addr;
13761
0
        }
13762
0
    }
13763
13764
0
        if (obed->dtrel_excludes_plt && htab->srelplt != NULL)
13765
0
    {
13766
0
      unsigned int opb = bfd_octets_per_byte (obfd, o);
13767
13768
      /* Don't count procedure linkage table relocs in the
13769
         overall reloc count.  */
13770
0
      sh_size -= htab->srelplt->size;
13771
0
      if (sh_size == 0)
13772
        /* If the size is zero, make the address zero too.
13773
           This is to avoid a glibc bug.  If the backend
13774
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13775
           zero, then we'll put DT_RELA at the end of
13776
           DT_JMPREL.  glibc will interpret the end of
13777
           DT_RELA matching the end of DT_JMPREL as the
13778
           case where DT_RELA includes DT_JMPREL, and for
13779
           LD_BIND_NOW will decide that processing DT_RELA
13780
           will process the PLT relocs too.  Net result:
13781
           No PLT relocs applied.  */
13782
0
        sh_addr = 0;
13783
13784
      /* If .rela.plt is the first .rela section, exclude
13785
         it from DT_RELA.  */
13786
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13787
0
               + htab->srelplt->output_offset) * opb)
13788
0
        sh_addr += htab->srelplt->size;
13789
0
    }
13790
13791
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13792
0
    dyn.d_un.d_val = sh_size;
13793
0
        else
13794
0
    dyn.d_un.d_ptr = sh_addr;
13795
0
        break;
13796
0
      }
13797
0
    obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13798
0
  }
13799
0
    }
13800
13801
  /* If we have created any dynamic sections, then output them.  */
13802
0
  if (dynobj != NULL)
13803
0
    {
13804
0
      if (!obed->elf_backend_finish_dynamic_sections (info, flinfo.contents))
13805
0
  goto error_return;
13806
13807
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13808
0
      if (bfd_link_textrel_check (info)
13809
0
    && (o = htab->dynamic) != NULL
13810
0
    && o->size != 0)
13811
0
  {
13812
0
    bfd_byte *dyncon, *dynconend;
13813
13814
0
    dyncon = o->contents;
13815
0
    dynconend = o->contents + o->size;
13816
0
    for (; dyncon < dynconend; dyncon += obed->s->sizeof_dyn)
13817
0
      {
13818
0
        Elf_Internal_Dyn dyn;
13819
13820
0
        obed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13821
13822
0
        if (dyn.d_tag == DT_TEXTREL)
13823
0
    {
13824
0
      if (info->textrel_check == textrel_check_error)
13825
0
        info->callbacks->einfo
13826
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13827
0
      else if (bfd_link_dll (info))
13828
0
        info->callbacks->einfo
13829
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13830
0
      else if (bfd_link_pde (info))
13831
0
        info->callbacks->einfo
13832
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13833
0
      else
13834
0
        info->callbacks->einfo
13835
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13836
0
      break;
13837
0
    }
13838
0
      }
13839
0
  }
13840
13841
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13842
0
  {
13843
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13844
0
        || o->size == 0
13845
0
        || o->output_section == bfd_abs_section_ptr)
13846
0
      continue;
13847
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13848
0
      {
13849
        /* At this point, we are only interested in sections
13850
     created by bfd_elf_link_create_dynamic_sections().  */
13851
0
        continue;
13852
0
      }
13853
0
    if (htab->stab_info.stabstr == o)
13854
0
      continue;
13855
0
    if (htab->eh_info.hdr_sec == o)
13856
0
      continue;
13857
0
    if (strcmp (o->name, ".dynstr") != 0)
13858
0
      {
13859
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13860
0
              * bfd_octets_per_byte (obfd, o));
13861
0
        if (!bfd_set_section_contents (obfd, o->output_section,
13862
0
               o->contents, octets, o->size))
13863
0
    goto error_return;
13864
0
      }
13865
0
    else
13866
0
      {
13867
        /* The contents of the .dynstr section are actually in a
13868
     stringtab.  */
13869
0
        file_ptr off;
13870
13871
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13872
0
        if (bfd_seek (obfd, off, SEEK_SET) != 0
13873
0
      || !_bfd_elf_strtab_emit (obfd, htab->dynstr))
13874
0
    goto error_return;
13875
0
      }
13876
0
  }
13877
0
    }
13878
13879
0
  if (!info->resolve_section_groups)
13880
0
    {
13881
0
      bool failed = false;
13882
13883
0
      BFD_ASSERT (bfd_link_relocatable (info));
13884
0
      bfd_map_over_sections (obfd, bfd_elf_set_group_contents, &failed);
13885
0
      if (failed)
13886
0
  goto error_return;
13887
0
    }
13888
13889
  /* If we have optimized stabs strings, output them.  */
13890
0
  if (htab->stab_info.stabstr != NULL)
13891
0
    {
13892
0
      if (!_bfd_write_stab_strings (obfd, &htab->stab_info))
13893
0
  goto error_return;
13894
0
    }
13895
13896
0
  if (! _bfd_elf_write_section_eh_frame_hdr (obfd, info))
13897
0
    goto error_return;
13898
13899
0
  if (! _bfd_elf_write_section_sframe (obfd, info))
13900
0
    goto error_return;
13901
13902
0
  if (! _bfd_elf_write_section_object_attributes (obfd, info))
13903
0
    goto error_ret2;
13904
13905
0
  if (info->callbacks->emit_ctf)
13906
0
      info->callbacks->emit_ctf ();
13907
13908
0
  elf_final_link_free (obfd, &flinfo);
13909
13910
0
  if (info->unique_symbol)
13911
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13912
0
  return true;
13913
13914
0
 error_return:
13915
0
  free (htab->strtab);
13916
0
  htab->strtab = NULL;
13917
0
  elf_final_link_free (obfd, &flinfo);
13918
0
 error_ret2:
13919
0
  if (info->unique_symbol)
13920
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13921
0
  return false;
13922
0
}
13923

13924
/* Initialize COOKIE for input bfd ABFD.  */
13925
13926
static bool
13927
init_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13928
0
{
13929
0
  Elf_Internal_Shdr *symtab_hdr;
13930
0
  elf_backend_data *bed;
13931
13932
0
  bed = get_elf_backend_data (abfd);
13933
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13934
13935
0
  cookie->abfd = abfd;
13936
0
  cookie->num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
13937
0
  if (elf_bad_symtab (abfd))
13938
0
    {
13939
0
      cookie->locsymcount = cookie->num_sym;
13940
0
      cookie->extsymoff = 0;
13941
0
    }
13942
0
  else
13943
0
    {
13944
0
      cookie->locsymcount = symtab_hdr->sh_info;
13945
0
      cookie->extsymoff = symtab_hdr->sh_info;
13946
0
    }
13947
13948
0
  if (bed->s->arch_size == 32)
13949
0
    cookie->r_sym_shift = 8;
13950
0
  else
13951
0
    cookie->r_sym_shift = 32;
13952
13953
0
  return true;
13954
0
}
13955
13956
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13957
13958
static void
13959
fini_reloc_cookie (struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
13960
       bfd *abfd ATTRIBUTE_UNUSED)
13961
0
{
13962
0
}
13963
13964
/* Initialize the relocation information in COOKIE for input section SEC
13965
   of input bfd ABFD.  */
13966
13967
static bool
13968
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13969
      struct bfd_link_info *info, bfd *abfd,
13970
      asection *sec, bool keep_memory)
13971
0
{
13972
0
  if (sec->reloc_count == 0)
13973
0
    {
13974
0
      cookie->rels = NULL;
13975
0
      cookie->relend = NULL;
13976
0
    }
13977
0
  else
13978
0
    {
13979
0
      cookie->rels = _bfd_elf_link_info_read_relocs
13980
0
  (abfd, info, sec, NULL, NULL,
13981
0
   keep_memory || _bfd_elf_link_keep_memory (info));
13982
0
      if (cookie->rels == NULL)
13983
0
  return false;
13984
0
      cookie->rel = cookie->rels;
13985
0
      cookie->relend = cookie->rels + sec->reloc_count;
13986
0
    }
13987
0
  cookie->rel = cookie->rels;
13988
0
  return true;
13989
0
}
13990
13991
/* Free the memory allocated by init_reloc_cookie_rels,
13992
   if appropriate.  */
13993
13994
static void
13995
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13996
      asection *sec)
13997
0
{
13998
0
  if (elf_section_data (sec)->relocs != cookie->rels)
13999
0
    free (cookie->rels);
14000
0
}
14001
14002
/* Initialize the whole of COOKIE for input section SEC.  */
14003
14004
static bool
14005
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
14006
             struct bfd_link_info *info,
14007
             asection *sec, bool keep_memory)
14008
0
{
14009
0
  if (!init_reloc_cookie (cookie, sec->owner))
14010
0
    goto error1;
14011
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
14012
0
             keep_memory))
14013
0
    goto error2;
14014
0
  return true;
14015
14016
0
 error2:
14017
0
  fini_reloc_cookie (cookie, sec->owner);
14018
0
 error1:
14019
0
  return false;
14020
0
}
14021
14022
/* Free the memory allocated by init_reloc_cookie_for_section,
14023
   if appropriate.  */
14024
14025
static void
14026
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
14027
             asection *sec)
14028
0
{
14029
0
  fini_reloc_cookie_rels (cookie, sec);
14030
0
  fini_reloc_cookie (cookie, sec->owner);
14031
0
}
14032

14033
/* Garbage collect unused sections.  */
14034
14035
/* Default gc_mark_hook.  */
14036
14037
asection *
14038
_bfd_elf_gc_mark_hook (asection *sec ATTRIBUTE_UNUSED,
14039
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
14040
           struct elf_reloc_cookie *cookie,
14041
           struct elf_link_hash_entry *h,
14042
           unsigned int symndx)
14043
0
{
14044
0
  if (h == NULL)
14045
0
    return _bfd_get_local_sym_section (cookie, symndx);
14046
14047
0
  switch (h->root.type)
14048
0
    {
14049
0
    case bfd_link_hash_defined:
14050
0
    case bfd_link_hash_defweak:
14051
0
      return h->root.u.def.section;
14052
14053
0
    case bfd_link_hash_common:
14054
0
      return h->root.u.c.p->section;
14055
14056
0
    default:
14057
0
      return NULL;
14058
0
    }
14059
0
}
14060
14061
/* Return the debug definition section.  */
14062
14063
static asection *
14064
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14065
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
14066
         struct elf_reloc_cookie *cookie,
14067
         struct elf_link_hash_entry *h,
14068
         unsigned int symndx)
14069
0
{
14070
0
  if (h != NULL)
14071
0
    {
14072
      /* Return the global debug definition section.  */
14073
0
      if ((h->root.type == bfd_link_hash_defined
14074
0
     || h->root.type == bfd_link_hash_defweak)
14075
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14076
0
  return h->root.u.def.section;
14077
0
    }
14078
0
  else
14079
0
    {
14080
      /* Return the local debug definition section.  */
14081
0
      asection *isec = _bfd_get_local_sym_section (cookie, symndx);
14082
0
      if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14083
0
  return isec;
14084
0
    }
14085
14086
0
  return NULL;
14087
0
}
14088
14089
/* COOKIE->rel describes a relocation against section SEC, which is
14090
   a section we've decided to keep.  Return the section that contains
14091
   the relocation symbol, or NULL if no section contains it.  */
14092
14093
asection *
14094
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14095
           elf_gc_mark_hook_fn gc_mark_hook,
14096
           struct elf_reloc_cookie *cookie,
14097
           bool *start_stop)
14098
0
{
14099
0
  unsigned long r_symndx;
14100
0
  struct elf_link_hash_entry *h, *hw;
14101
14102
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14103
0
  if (r_symndx == STN_UNDEF)
14104
0
    return NULL;
14105
14106
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14107
0
  if (h == NULL)
14108
0
    {
14109
      /* A corrupt input file can lead to a situation where the index
14110
   does not reference either a local or an external symbol.  */
14111
0
      if (r_symndx >= cookie->locsymcount)
14112
0
  return NULL;
14113
14114
0
      return (*gc_mark_hook) (sec, info, cookie, NULL, r_symndx);
14115
0
    }
14116
14117
0
  bool was_marked = h->mark;
14118
14119
0
  h->mark = 1;
14120
  /* Keep all aliases of the symbol too.  If an object symbol
14121
     needs to be copied into .dynbss then all of its aliases
14122
     should be present as dynamic symbols, not just the one used
14123
     on the copy relocation.  */
14124
0
  hw = h;
14125
0
  while (hw->is_weakalias)
14126
0
    {
14127
0
      hw = hw->u.alias;
14128
0
      hw->mark = 1;
14129
0
    }
14130
14131
0
  if (!was_marked && h->start_stop && !h->root.ldscript_def)
14132
0
    {
14133
0
      if (info->start_stop_gc)
14134
0
  return NULL;
14135
14136
      /* To work around a glibc bug, mark XXX input sections
14137
   when there is a reference to __start_XXX or __stop_XXX
14138
   symbols.  */
14139
0
      else if (start_stop != NULL)
14140
0
  {
14141
0
    asection *s = h->u2.start_stop_section;
14142
0
    *start_stop = true;
14143
0
    return s;
14144
0
  }
14145
0
    }
14146
14147
0
  return (*gc_mark_hook) (sec, info, cookie, h, 0);
14148
0
}
14149
14150
/* COOKIE->rel describes a relocation against section SEC, which is
14151
   a section we've decided to keep.  Mark the section that contains
14152
   the relocation symbol.  */
14153
14154
bool
14155
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14156
      asection *sec,
14157
      elf_gc_mark_hook_fn gc_mark_hook,
14158
      struct elf_reloc_cookie *cookie)
14159
0
{
14160
0
  asection *rsec;
14161
0
  bool start_stop = false;
14162
14163
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14164
0
  while (rsec != NULL)
14165
0
    {
14166
0
      if (!rsec->gc_mark)
14167
0
  {
14168
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14169
0
        || (rsec->owner->flags & DYNAMIC) != 0)
14170
0
      rsec->gc_mark = 1;
14171
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14172
0
      return false;
14173
0
  }
14174
0
      if (!start_stop)
14175
0
  break;
14176
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14177
0
    }
14178
0
  return true;
14179
0
}
14180
14181
/* The mark phase of garbage collection.  For a given section, mark
14182
   it and any sections in this section's group, and all the sections
14183
   which define symbols to which it refers.  */
14184
14185
bool
14186
_bfd_elf_gc_mark (struct bfd_link_info *info,
14187
      asection *sec,
14188
      elf_gc_mark_hook_fn gc_mark_hook)
14189
0
{
14190
0
  bool ret;
14191
0
  asection *group_sec, *eh_frame, *sframe;
14192
14193
0
  sec->gc_mark = 1;
14194
14195
  /* Mark all the sections in the group.  */
14196
0
  group_sec = elf_section_data (sec)->next_in_group;
14197
0
  if (group_sec && !group_sec->gc_mark)
14198
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14199
0
      return false;
14200
14201
  /* Look through the section relocs.  */
14202
0
  ret = true;
14203
0
  eh_frame = elf_eh_frame_section (sec->owner);
14204
0
  sframe = elf_sframe_section (sec->owner);
14205
14206
0
  if ((sec->flags & SEC_RELOC) != 0
14207
0
      && sec->reloc_count > 0
14208
0
      && sec != eh_frame
14209
0
      && sec != sframe)
14210
0
    {
14211
0
      struct elf_reloc_cookie cookie;
14212
14213
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14214
0
  ret = false;
14215
0
      else
14216
0
  {
14217
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
14218
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14219
0
        {
14220
0
    ret = false;
14221
0
    break;
14222
0
        }
14223
0
    fini_reloc_cookie_for_section (&cookie, sec);
14224
0
  }
14225
0
    }
14226
14227
0
  if (ret && eh_frame && elf_fde_list (sec))
14228
0
    {
14229
0
      struct elf_reloc_cookie cookie;
14230
14231
      /* NB: When --no-keep-memory is used, the symbol table and
14232
   relocation info for eh_frame are freed after they are retrieved
14233
   for each text section in the input object.  If an input object
14234
   has many text sections, the same data is retrieved and freed
14235
   many times which can take a very long time.  Always keep the
14236
   symbol table and relocation info for eh_frame to avoid it.  */
14237
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14238
0
            true))
14239
0
  ret = false;
14240
0
      else
14241
0
  {
14242
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14243
0
              gc_mark_hook, &cookie))
14244
0
      ret = false;
14245
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
14246
0
  }
14247
0
    }
14248
14249
0
  eh_frame = elf_section_eh_frame_entry (sec);
14250
0
  if (ret && eh_frame && !eh_frame->gc_mark)
14251
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14252
0
      ret = false;
14253
14254
0
  return ret;
14255
0
}
14256
14257
/* Scan and mark sections in a special or debug section group.  */
14258
14259
static void
14260
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14261
0
{
14262
  /* Point to first section of section group.  */
14263
0
  asection *ssec;
14264
  /* Used to iterate the section group.  */
14265
0
  asection *msec;
14266
14267
0
  bool is_special_grp = true;
14268
0
  bool is_debug_grp = true;
14269
14270
  /* First scan to see if group contains any section other than debug
14271
     and special section.  */
14272
0
  ssec = msec = elf_next_in_group (grp);
14273
0
  while (msec != NULL)
14274
0
    {
14275
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
14276
0
  is_debug_grp = false;
14277
14278
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14279
0
  is_special_grp = false;
14280
14281
0
      msec = elf_next_in_group (msec);
14282
0
      if (msec == ssec)
14283
0
  break;
14284
0
    }
14285
14286
  /* If this is a pure debug section group or pure special section group,
14287
     keep all sections in this group.  */
14288
0
  if (is_debug_grp || is_special_grp)
14289
0
    {
14290
0
      msec = ssec;
14291
0
      while (msec != NULL)
14292
0
  {
14293
0
    msec->gc_mark = 1;
14294
0
    msec = elf_next_in_group (msec);
14295
0
    if (msec == ssec)
14296
0
      break;
14297
0
  }
14298
0
    }
14299
0
}
14300
14301
/* Keep debug and special sections.  */
14302
14303
bool
14304
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14305
         elf_gc_mark_hook_fn mark_hook)
14306
0
{
14307
0
  bfd *ibfd;
14308
14309
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14310
0
    {
14311
0
      asection *isec;
14312
0
      bool some_kept;
14313
0
      bool debug_frag_seen;
14314
0
      bool has_kept_debug_info;
14315
14316
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14317
0
  continue;
14318
0
      isec = ibfd->sections;
14319
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14320
0
  continue;
14321
14322
      /* Ensure all linker created sections are kept,
14323
   see if any other section is already marked,
14324
   and note if we have any fragmented debug sections.  */
14325
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
14326
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14327
0
  {
14328
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
14329
0
      isec->gc_mark = 1;
14330
0
    else if (isec->gc_mark
14331
0
       && (isec->flags & SEC_ALLOC) != 0
14332
0
       && elf_section_type (isec) != SHT_NOTE)
14333
0
      some_kept = true;
14334
0
    else
14335
0
      {
14336
        /* Since all sections, except for backend specific ones,
14337
     have been garbage collected, call mark_hook on this
14338
     section if any of its linked-to sections is marked.  */
14339
0
        asection *linked_to_sec;
14340
0
        for (linked_to_sec = elf_linked_to_section (isec);
14341
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
14342
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14343
0
    {
14344
0
      if (linked_to_sec->gc_mark)
14345
0
        {
14346
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14347
0
      return false;
14348
0
          break;
14349
0
        }
14350
0
      linked_to_sec->linker_mark = 1;
14351
0
    }
14352
0
        for (linked_to_sec = elf_linked_to_section (isec);
14353
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
14354
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14355
0
    linked_to_sec->linker_mark = 0;
14356
0
      }
14357
14358
0
    if (!debug_frag_seen
14359
0
        && (isec->flags & SEC_DEBUGGING)
14360
0
        && startswith (isec->name, ".debug_line."))
14361
0
      debug_frag_seen = true;
14362
0
    else if (strcmp (bfd_section_name (isec),
14363
0
         "__patchable_function_entries") == 0
14364
0
       && elf_linked_to_section (isec) == NULL)
14365
0
        info->callbacks->fatal (_("%P: %pB(%pA): error: "
14366
0
          "need linked-to section "
14367
0
          "for --gc-sections\n"),
14368
0
              isec->owner, isec);
14369
0
  }
14370
14371
      /* If no non-note alloc section in this file will be kept, then
14372
   we can toss out the debug and special sections.  */
14373
0
      if (!some_kept)
14374
0
  continue;
14375
14376
      /* Keep debug and special sections like .comment when they are
14377
   not part of a group.  Also keep section groups that contain
14378
   just debug sections or special sections.  NB: Sections with
14379
   linked-to section has been handled above.  */
14380
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14381
0
  {
14382
0
    if ((isec->flags & SEC_GROUP) != 0)
14383
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14384
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14385
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14386
0
       && elf_next_in_group (isec) == NULL
14387
0
       && elf_linked_to_section (isec) == NULL)
14388
0
      isec->gc_mark = 1;
14389
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14390
0
      has_kept_debug_info = true;
14391
0
  }
14392
14393
      /* Look for CODE sections which are going to be discarded,
14394
   and find and discard any fragmented debug sections which
14395
   are associated with that code section.  */
14396
0
      if (debug_frag_seen)
14397
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14398
0
    if ((isec->flags & SEC_CODE) != 0
14399
0
        && isec->gc_mark == 0)
14400
0
      {
14401
0
        unsigned int ilen;
14402
0
        asection *dsec;
14403
14404
0
        ilen = strlen (isec->name);
14405
14406
        /* Association is determined by the name of the debug
14407
     section containing the name of the code section as
14408
     a suffix.  For example .debug_line.text.foo is a
14409
     debug section associated with .text.foo.  */
14410
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14411
0
    {
14412
0
      unsigned int dlen;
14413
14414
0
      if (dsec->gc_mark == 0
14415
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14416
0
        continue;
14417
14418
0
      dlen = strlen (dsec->name);
14419
14420
0
      if (dlen > ilen
14421
0
          && strncmp (dsec->name + (dlen - ilen),
14422
0
          isec->name, ilen) == 0)
14423
0
        dsec->gc_mark = 0;
14424
0
    }
14425
0
    }
14426
14427
      /* Mark debug sections referenced by kept debug sections.  */
14428
0
      if (has_kept_debug_info)
14429
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14430
0
    if (isec->gc_mark
14431
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14432
0
      if (!_bfd_elf_gc_mark (info, isec,
14433
0
           elf_gc_mark_debug_section))
14434
0
        return false;
14435
0
    }
14436
0
  return true;
14437
0
}
14438
14439
static bool
14440
elf_gc_sweep (struct bfd_link_info *info)
14441
0
{
14442
0
  for (bfd *sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14443
0
    {
14444
0
      if (!compatible_format (info, sub))
14445
0
  continue;
14446
14447
0
      for (asection *o = sub->sections; o != NULL; o = o->next)
14448
0
  {
14449
    /* When any section in a section group is kept, we keep all
14450
       sections in the section group.  If the first member of
14451
       the section group is excluded, we will also exclude the
14452
       group section.  */
14453
0
    if (o->flags & SEC_GROUP)
14454
0
      {
14455
0
        asection *first = elf_next_in_group (o);
14456
0
        if (first != NULL)
14457
0
    o->gc_mark = first->gc_mark;
14458
0
      }
14459
14460
0
    if (o->gc_mark)
14461
0
      continue;
14462
14463
    /* Skip sweeping sections already excluded.  */
14464
0
    if (o->flags & SEC_EXCLUDE)
14465
0
      continue;
14466
14467
    /* Since this is early in the link process, it is simple
14468
       to remove a section from the output.  */
14469
0
    o->flags |= SEC_EXCLUDE;
14470
14471
0
    if (info->print_gc_sections && o->size != 0)
14472
      /* xgettext:c-format */
14473
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14474
0
        o, sub);
14475
0
  }
14476
0
    }
14477
14478
0
  return true;
14479
0
}
14480
14481
/* Propagate collected vtable information.  This is called through
14482
   elf_link_hash_traverse.  */
14483
14484
static bool
14485
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14486
0
{
14487
  /* Those that are not vtables.  */
14488
0
  if (h->start_stop
14489
0
      || h->u2.vtable == NULL
14490
0
      || h->u2.vtable->parent == NULL)
14491
0
    return true;
14492
14493
  /* Those vtables that do not have parents, we cannot merge.  */
14494
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14495
0
    return true;
14496
14497
  /* If we've already been done, exit.  */
14498
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14499
0
    return true;
14500
14501
  /* Make sure the parent's table is up to date.  */
14502
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14503
14504
0
  if (h->u2.vtable->used == NULL)
14505
0
    {
14506
      /* None of this table's entries were referenced.  Re-use the
14507
   parent's table.  */
14508
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14509
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14510
0
    }
14511
0
  else
14512
0
    {
14513
0
      size_t n;
14514
0
      bool *cu, *pu;
14515
14516
      /* Or the parent's entries into ours.  */
14517
0
      cu = h->u2.vtable->used;
14518
0
      cu[-1] = true;
14519
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14520
0
      if (pu != NULL)
14521
0
  {
14522
0
    elf_backend_data *bed;
14523
0
    unsigned int log_file_align;
14524
14525
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14526
0
    log_file_align = bed->s->log_file_align;
14527
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14528
0
    while (n--)
14529
0
      {
14530
0
        if (*pu)
14531
0
    *cu = true;
14532
0
        pu++;
14533
0
        cu++;
14534
0
      }
14535
0
  }
14536
0
    }
14537
14538
0
  return true;
14539
0
}
14540
14541
struct link_info_ok
14542
{
14543
  struct bfd_link_info *info;
14544
  bool ok;
14545
};
14546
14547
static bool
14548
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14549
            void *ptr)
14550
0
{
14551
0
  asection *sec;
14552
0
  bfd_vma hstart, hend;
14553
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14554
0
  elf_backend_data *bed;
14555
0
  unsigned int log_file_align;
14556
0
  struct link_info_ok *info = ptr;
14557
14558
  /* Take care of both those symbols that do not describe vtables as
14559
     well as those that are not loaded.  */
14560
0
  if (h->start_stop
14561
0
      || h->u2.vtable == NULL
14562
0
      || h->u2.vtable->parent == NULL)
14563
0
    return true;
14564
14565
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14566
0
        || h->root.type == bfd_link_hash_defweak);
14567
14568
0
  sec = h->root.u.def.section;
14569
0
  hstart = h->root.u.def.value;
14570
0
  hend = hstart + h->size;
14571
14572
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14573
0
               sec, NULL, NULL, true);
14574
0
  if (!relstart)
14575
0
    return info->ok = false;
14576
0
  bed = get_elf_backend_data (sec->owner);
14577
0
  log_file_align = bed->s->log_file_align;
14578
14579
0
  relend = relstart + sec->reloc_count;
14580
14581
0
  for (rel = relstart; rel < relend; ++rel)
14582
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14583
0
      {
14584
  /* If the entry is in use, do nothing.  */
14585
0
  if (h->u2.vtable->used
14586
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14587
0
    {
14588
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14589
0
      if (h->u2.vtable->used[entry])
14590
0
        continue;
14591
0
    }
14592
  /* Otherwise, kill it.  */
14593
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14594
0
      }
14595
14596
0
  return true;
14597
0
}
14598
14599
/* Mark sections containing dynamically referenced symbols.  When
14600
   building shared libraries, we must assume that any visible symbol is
14601
   referenced.  */
14602
14603
bool
14604
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14605
0
{
14606
0
  struct bfd_link_info *info = inf;
14607
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14608
14609
0
  if ((h->root.type == bfd_link_hash_defined
14610
0
       || h->root.type == bfd_link_hash_defweak)
14611
0
      && (!h->start_stop
14612
0
    || h->root.ldscript_def
14613
0
    || !info->start_stop_gc)
14614
0
      && ((h->ref_dynamic && !h->forced_local)
14615
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14616
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14617
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14618
0
        && (!bfd_link_executable (info)
14619
0
      || info->gc_keep_exported
14620
0
      || info->export_dynamic
14621
0
      || (h->dynamic
14622
0
          && d != NULL
14623
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14624
0
        && (h->versioned >= versioned
14625
0
      || !bfd_hide_sym_by_version (info->version_info,
14626
0
                 h->root.root.string)))))
14627
0
    h->root.u.def.section->flags |= SEC_KEEP;
14628
14629
0
  return true;
14630
0
}
14631
14632
/* Keep all sections containing symbols undefined on the command-line,
14633
   and the section containing the entry symbol.  */
14634
14635
void
14636
_bfd_elf_gc_keep (struct bfd_link_info *info)
14637
0
{
14638
0
  struct bfd_sym_chain *sym;
14639
14640
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14641
0
    {
14642
0
      struct elf_link_hash_entry *h;
14643
14644
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14645
0
        false, false, false);
14646
14647
0
      if (h != NULL
14648
0
    && (h->root.type == bfd_link_hash_defined
14649
0
        || h->root.type == bfd_link_hash_defweak)
14650
0
    && !bfd_is_const_section (h->root.u.def.section))
14651
0
  h->root.u.def.section->flags |= SEC_KEEP;
14652
0
    }
14653
0
}
14654
14655
bool
14656
bfd_elf_parse_eh_frame_entries (struct bfd_link_info *info)
14657
0
{
14658
0
  bfd *ibfd = info->input_bfds;
14659
14660
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14661
0
    {
14662
0
      asection *sec;
14663
0
      struct elf_reloc_cookie cookie;
14664
14665
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14666
0
  continue;
14667
0
      sec = ibfd->sections;
14668
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14669
0
  continue;
14670
14671
0
      if (!init_reloc_cookie (&cookie, ibfd))
14672
0
  return false;
14673
14674
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14675
0
  {
14676
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14677
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14678
0
           false))
14679
0
      {
14680
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14681
0
        fini_reloc_cookie_rels (&cookie, sec);
14682
0
      }
14683
0
  }
14684
0
    }
14685
0
  return true;
14686
0
}
14687
14688
/* Do mark and sweep of unused sections.  */
14689
14690
bool
14691
bfd_elf_gc_sections (bfd *obfd, struct bfd_link_info *info)
14692
0
{
14693
0
  bool ok = true;
14694
0
  elf_gc_mark_hook_fn gc_mark_hook;
14695
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
14696
0
  struct elf_link_hash_table *htab;
14697
0
  struct link_info_ok info_ok;
14698
14699
0
  if (!obed->can_gc_sections
14700
0
      || !is_elf_hash_table (info->hash))
14701
0
    {
14702
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14703
0
      return true;
14704
0
    }
14705
14706
0
  obed->gc_keep (info);
14707
0
  htab = elf_hash_table (info);
14708
14709
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14710
     at the .eh_frame section if we can mark the FDEs individually.  */
14711
0
  for (bfd *sub = info->input_bfds;
14712
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14713
0
       sub = sub->link.next)
14714
0
    {
14715
0
      asection *sec;
14716
0
      struct elf_reloc_cookie cookie;
14717
14718
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
14719
0
  continue;
14720
0
      sec = sub->sections;
14721
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14722
0
  continue;
14723
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14724
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14725
0
               false))
14726
0
  {
14727
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14728
0
    if (sec->sec_info
14729
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14730
0
      elf_eh_frame_section (sub) = sec;
14731
0
    fini_reloc_cookie_for_section (&cookie, sec);
14732
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14733
0
  }
14734
14735
      /* Handle .sframe section.  */
14736
0
      sec = bfd_get_section_by_name (sub, ".sframe");
14737
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14738
0
               false))
14739
0
  {
14740
0
    _bfd_elf_parse_sframe (sub, info, sec, &cookie);
14741
14742
0
    if (sec->sec_info
14743
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14744
0
      elf_sframe_section (sub) = sec;
14745
14746
0
    fini_reloc_cookie_for_section (&cookie, sec);
14747
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14748
0
  }
14749
0
    }
14750
14751
  /* Apply transitive closure to the vtable entry usage info.  */
14752
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14753
0
  if (!ok)
14754
0
    return false;
14755
14756
  /* Kill the vtable relocations that were not used.  */
14757
0
  info_ok.info = info;
14758
0
  info_ok.ok = true;
14759
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14760
0
  if (!info_ok.ok)
14761
0
    return false;
14762
14763
  /* Mark dynamically referenced symbols.  */
14764
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14765
0
    elf_link_hash_traverse (htab, obed->gc_mark_dynamic_ref, info);
14766
14767
  /* Grovel through relocs to find out who stays ...  */
14768
0
  gc_mark_hook = obed->gc_mark_hook;
14769
0
  for (bfd *sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14770
0
    {
14771
0
      if (!compatible_format (info, sub))
14772
0
  continue;
14773
14774
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14775
   Also treat note sections as a root, if the section is not part
14776
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14777
   well as FINI_ARRAY sections for ld -r.  */
14778
0
      for (asection *o = sub->sections; o != NULL; o = o->next)
14779
0
  if (!o->gc_mark
14780
0
      && (o->flags & SEC_EXCLUDE) == 0
14781
0
      && ((o->flags & SEC_KEEP) != 0
14782
0
    || (bfd_link_relocatable (info)
14783
0
        && ((elf_section_data (o)->this_hdr.sh_type
14784
0
       == SHT_PREINIT_ARRAY)
14785
0
      || (elf_section_data (o)->this_hdr.sh_type
14786
0
          == SHT_INIT_ARRAY)
14787
0
      || (elf_section_data (o)->this_hdr.sh_type
14788
0
          == SHT_FINI_ARRAY)))
14789
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14790
0
        && elf_next_in_group (o) == NULL
14791
0
        && elf_linked_to_section (o) == NULL)
14792
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14793
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14794
0
    {
14795
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14796
0
        return false;
14797
0
    }
14798
0
    }
14799
14800
  /* Allow the backend to mark additional target specific sections.  */
14801
0
  if (!obed->gc_mark_extra_sections (info, gc_mark_hook))
14802
0
    return false;
14803
14804
  /* ... and mark SEC_EXCLUDE for those that go.  */
14805
0
  return elf_gc_sweep (info);
14806
0
}
14807

14808
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14809
14810
bool
14811
bfd_elf_gc_record_vtinherit (bfd *abfd,
14812
           asection *sec,
14813
           struct elf_link_hash_entry *h,
14814
           bfd_vma offset)
14815
0
{
14816
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14817
0
  struct elf_link_hash_entry **search, *child;
14818
0
  size_t extsymcount;
14819
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14820
14821
  /* The sh_info field of the symtab header tells us where the
14822
     external symbols start.  We don't care about the local symbols at
14823
     this point.  */
14824
0
  extsymcount = elf_symtab_hdr (abfd).sh_size / bed->s->sizeof_sym;
14825
0
  if (!elf_bad_symtab (abfd))
14826
0
    extsymcount -= elf_symtab_hdr (abfd).sh_info;
14827
14828
0
  sym_hashes = elf_sym_hashes (abfd);
14829
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14830
14831
  /* Hunt down the child symbol, which is in this section at the same
14832
     offset as the relocation.  */
14833
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14834
0
    {
14835
0
      if ((child = *search) != NULL
14836
0
    && (child->root.type == bfd_link_hash_defined
14837
0
        || child->root.type == bfd_link_hash_defweak)
14838
0
    && child->root.u.def.section == sec
14839
0
    && child->root.u.def.value == offset)
14840
0
  goto win;
14841
0
    }
14842
14843
  /* xgettext:c-format */
14844
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14845
0
          abfd, sec, (uint64_t) offset);
14846
0
  bfd_set_error (bfd_error_invalid_operation);
14847
0
  return false;
14848
14849
0
 win:
14850
0
  if (!child->u2.vtable)
14851
0
    {
14852
0
      child->u2.vtable = bfd_zalloc (abfd, sizeof (*child->u2.vtable));
14853
0
      if (!child->u2.vtable)
14854
0
  return false;
14855
0
    }
14856
0
  if (!h)
14857
0
    {
14858
      /* This *should* only be the absolute section.  It could potentially
14859
   be that someone has defined a non-global vtable though, which
14860
   would be bad.  It isn't worth paging in the local symbols to be
14861
   sure though; that case should simply be handled by the assembler.  */
14862
14863
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14864
0
    }
14865
0
  else
14866
0
    child->u2.vtable->parent = h;
14867
14868
0
  return true;
14869
0
}
14870
14871
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14872
14873
bool
14874
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14875
         struct elf_link_hash_entry *h,
14876
         bfd_vma addend)
14877
0
{
14878
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14879
0
  unsigned int log_file_align = bed->s->log_file_align;
14880
14881
0
  if (!h || addend > 1u << 28)
14882
0
    {
14883
      /* xgettext:c-format */
14884
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14885
0
        abfd, sec);
14886
0
      bfd_set_error (bfd_error_bad_value);
14887
0
      return false;
14888
0
    }
14889
14890
0
  if (!h->u2.vtable)
14891
0
    {
14892
0
      h->u2.vtable = bfd_zalloc (abfd, sizeof (*h->u2.vtable));
14893
0
      if (!h->u2.vtable)
14894
0
  return false;
14895
0
    }
14896
14897
0
  if (addend >= h->u2.vtable->size)
14898
0
    {
14899
0
      size_t size, bytes, file_align;
14900
0
      bool *ptr = h->u2.vtable->used;
14901
14902
      /* While the symbol is undefined, we have to be prepared to handle
14903
   a zero size.  */
14904
0
      file_align = 1 << log_file_align;
14905
0
      if (h->root.type == bfd_link_hash_undefined)
14906
0
  size = addend + file_align;
14907
0
      else
14908
0
  {
14909
0
    size = h->size;
14910
0
    if (addend >= size)
14911
0
      {
14912
        /* Oops!  We've got a reference past the defined end of
14913
     the table.  This is probably a bug -- shall we warn?  */
14914
0
        size = addend + file_align;
14915
0
      }
14916
0
  }
14917
0
      size = (size + file_align - 1) & -file_align;
14918
14919
      /* Allocate one extra entry for use as a "done" flag for the
14920
   consolidation pass.  */
14921
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14922
14923
0
      if (ptr)
14924
0
  {
14925
0
    ptr = bfd_realloc (ptr - 1, bytes);
14926
14927
0
    if (ptr != NULL)
14928
0
      {
14929
0
        size_t oldbytes;
14930
14931
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14932
0
        * sizeof (bool));
14933
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14934
0
      }
14935
0
  }
14936
0
      else
14937
0
  ptr = bfd_zmalloc (bytes);
14938
14939
0
      if (ptr == NULL)
14940
0
  return false;
14941
14942
      /* And arrange for that done flag to be at index -1.  */
14943
0
      h->u2.vtable->used = ptr + 1;
14944
0
      h->u2.vtable->size = size;
14945
0
    }
14946
14947
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14948
14949
0
  return true;
14950
0
}
14951
14952
/* Map an ELF section header flag to its corresponding string.  */
14953
typedef struct
14954
{
14955
  char *flag_name;
14956
  flagword flag_value;
14957
} elf_flags_to_name_table;
14958
14959
static const elf_flags_to_name_table elf_flags_to_names [] =
14960
{
14961
  { "SHF_WRITE", SHF_WRITE },
14962
  { "SHF_ALLOC", SHF_ALLOC },
14963
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14964
  { "SHF_MERGE", SHF_MERGE },
14965
  { "SHF_STRINGS", SHF_STRINGS },
14966
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14967
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14968
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14969
  { "SHF_GROUP", SHF_GROUP },
14970
  { "SHF_TLS", SHF_TLS },
14971
  { "SHF_MASKOS", SHF_MASKOS },
14972
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14973
};
14974
14975
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14976
bool
14977
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14978
            struct flag_info *flaginfo,
14979
            asection *section)
14980
0
{
14981
0
  const bfd_vma sh_flags = elf_section_flags (section);
14982
14983
0
  if (!flaginfo->flags_initialized)
14984
0
    {
14985
0
      bfd *obfd = info->output_bfd;
14986
0
      elf_backend_data *obed = get_elf_backend_data (obfd);
14987
0
      struct flag_info_list *tf = flaginfo->flag_list;
14988
0
      int with_hex = 0;
14989
0
      int without_hex = 0;
14990
14991
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14992
0
  {
14993
0
    unsigned i;
14994
0
    flagword (*lookup) (char *);
14995
14996
0
    lookup = obed->elf_backend_lookup_section_flags_hook;
14997
0
    if (lookup != NULL)
14998
0
      {
14999
0
        flagword hexval = (*lookup) ((char *) tf->name);
15000
15001
0
        if (hexval != 0)
15002
0
    {
15003
0
      if (tf->with == with_flags)
15004
0
        with_hex |= hexval;
15005
0
      else if (tf->with == without_flags)
15006
0
        without_hex |= hexval;
15007
0
      tf->valid = true;
15008
0
      continue;
15009
0
    }
15010
0
      }
15011
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
15012
0
      {
15013
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
15014
0
    {
15015
0
      if (tf->with == with_flags)
15016
0
        with_hex |= elf_flags_to_names[i].flag_value;
15017
0
      else if (tf->with == without_flags)
15018
0
        without_hex |= elf_flags_to_names[i].flag_value;
15019
0
      tf->valid = true;
15020
0
      break;
15021
0
    }
15022
0
      }
15023
0
    if (!tf->valid)
15024
0
      {
15025
0
        info->callbacks->einfo
15026
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
15027
0
        return false;
15028
0
      }
15029
0
  }
15030
0
      flaginfo->flags_initialized = true;
15031
0
      flaginfo->only_with_flags |= with_hex;
15032
0
      flaginfo->not_with_flags |= without_hex;
15033
0
    }
15034
15035
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
15036
0
    return false;
15037
15038
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
15039
0
    return false;
15040
15041
0
  return true;
15042
0
}
15043
15044
struct alloc_got_off_arg {
15045
  bfd_vma gotoff;
15046
  struct bfd_link_info *info;
15047
};
15048
15049
/* We need a special top-level link routine to convert got reference counts
15050
   to real got offsets.  */
15051
15052
static bool
15053
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15054
0
{
15055
0
  struct alloc_got_off_arg *gofarg = arg;
15056
0
  bfd *obfd = gofarg->info->output_bfd;
15057
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15058
15059
0
  if (h->got.refcount > 0)
15060
0
    {
15061
0
      h->got.offset = gofarg->gotoff;
15062
0
      gofarg->gotoff += obed->got_elt_size (gofarg->info, h, NULL, 0);
15063
0
    }
15064
0
  else
15065
0
    h->got.offset = (bfd_vma) -1;
15066
15067
0
  return true;
15068
0
}
15069
15070
/* And an accompanying bit to work out final got entry offsets once
15071
   we're done.  Should be called from final_link.  */
15072
15073
static bool
15074
bfd_elf_gc_common_finalize_got_offsets (bfd *obfd,
15075
          struct bfd_link_info *info)
15076
0
{
15077
0
  bfd *i;
15078
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15079
0
  bfd_vma gotoff;
15080
0
  struct alloc_got_off_arg gofarg;
15081
15082
0
  BFD_ASSERT (obfd == info->output_bfd);
15083
15084
0
  if (! is_elf_hash_table (info->hash))
15085
0
    return false;
15086
15087
  /* The GOT offset is relative to the .got section, but the GOT header is
15088
     put into the .got.plt section, if the backend uses it.  */
15089
0
  if (obed->want_got_plt)
15090
0
    gotoff = 0;
15091
0
  else
15092
0
    gotoff = obed->got_header_size;
15093
15094
  /* Do the local .got entries first.  */
15095
0
  for (i = info->input_bfds; i; i = i->link.next)
15096
0
    {
15097
0
      bfd_signed_vma *local_got;
15098
0
      size_t j, locsymcount;
15099
0
      Elf_Internal_Shdr *symtab_hdr;
15100
15101
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15102
0
  continue;
15103
15104
0
      local_got = elf_local_got_refcounts (i);
15105
0
      if (!local_got)
15106
0
  continue;
15107
15108
0
      symtab_hdr = &elf_symtab_hdr (i);
15109
0
      if (elf_bad_symtab (i))
15110
0
  locsymcount = symtab_hdr->sh_size / obed->s->sizeof_sym;
15111
0
      else
15112
0
  locsymcount = symtab_hdr->sh_info;
15113
15114
0
      for (j = 0; j < locsymcount; ++j)
15115
0
  {
15116
0
    if (local_got[j] > 0)
15117
0
      {
15118
0
        local_got[j] = gotoff;
15119
0
        gotoff += obed->got_elt_size (info, NULL, i, j);
15120
0
      }
15121
0
    else
15122
0
      local_got[j] = (bfd_vma) -1;
15123
0
  }
15124
0
    }
15125
15126
  /* Then the global .got entries.  .plt refcounts are handled by
15127
     adjust_dynamic_symbol  */
15128
0
  gofarg.gotoff = gotoff;
15129
0
  gofarg.info = info;
15130
0
  elf_link_hash_traverse (elf_hash_table (info),
15131
0
        elf_gc_allocate_got_offsets,
15132
0
        &gofarg);
15133
0
  return true;
15134
0
}
15135
15136
/* Many folk need no more in the way of final link than this, once
15137
   got entry reference counting is enabled.  */
15138
15139
bool
15140
_bfd_elf_gc_common_final_link (bfd *obfd, struct bfd_link_info *info)
15141
0
{
15142
0
  if (!bfd_elf_gc_common_finalize_got_offsets (obfd, info))
15143
0
    return false;
15144
15145
  /* Invoke the regular ELF backend linker to do all the work.  */
15146
0
  return _bfd_elf_final_link (obfd, info);
15147
0
}
15148
15149
bool
15150
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15151
0
{
15152
0
  struct elf_reloc_cookie *rcookie = cookie;
15153
15154
0
  if (elf_bad_symtab (rcookie->abfd))
15155
0
    rcookie->rel = rcookie->rels;
15156
15157
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15158
0
    {
15159
0
      unsigned long r_symndx;
15160
15161
0
      if (!elf_bad_symtab (rcookie->abfd)
15162
0
    && rcookie->rel->r_offset > offset)
15163
0
  return false;
15164
0
      if (rcookie->rel->r_offset != offset)
15165
0
  continue;
15166
15167
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15168
0
      if (r_symndx == STN_UNDEF)
15169
0
  return true;
15170
15171
0
      struct elf_link_hash_entry *h;
15172
15173
0
      h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
15174
15175
0
      if (h != NULL)
15176
0
  {
15177
0
    if ((h->root.type == bfd_link_hash_defined
15178
0
         || h->root.type == bfd_link_hash_defweak)
15179
0
        && (h->root.u.def.section->owner != rcookie->abfd
15180
0
      || h->root.u.def.section->kept_section != NULL
15181
0
      || discarded_section (h->root.u.def.section)))
15182
0
      return true;
15183
0
  }
15184
0
      else
15185
0
  {
15186
0
    if (r_symndx >= rcookie->locsymcount)
15187
      /* This can happen with corrupt input.  */
15188
0
      return false;
15189
15190
    /* It's not a relocation against a global symbol,
15191
       but it could be a relocation against a local
15192
       symbol for a discarded section.  */
15193
0
    asection *isec = _bfd_get_local_sym_section (cookie, r_symndx);
15194
0
    if (isec != NULL
15195
0
        && (isec->kept_section != NULL
15196
0
      || discarded_section (isec)))
15197
0
      return true;
15198
0
  }
15199
15200
0
      return false;
15201
0
    }
15202
0
  return false;
15203
0
}
15204
15205
/* Discard unneeded references to discarded sections.
15206
   Returns -1 on error, 1 if any section's size was changed, 0 if
15207
   nothing changed.  This function assumes that the relocations are in
15208
   sorted order, which is true for all known assemblers.  */
15209
15210
int
15211
bfd_elf_discard_info (struct bfd_link_info *info)
15212
0
{
15213
0
  struct elf_reloc_cookie cookie;
15214
0
  asection *o;
15215
0
  bfd *abfd;
15216
0
  int changed = 0;
15217
15218
0
  if (info->traditional_format
15219
0
      || !is_elf_hash_table (info->hash))
15220
0
    return 0;
15221
15222
0
  o = bfd_get_section_by_name (info->output_bfd, ".stab");
15223
0
  if (o != NULL)
15224
0
    {
15225
0
      asection *i;
15226
15227
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15228
0
  {
15229
0
    if (i->size == 0
15230
0
        || i->reloc_count == 0
15231
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
15232
0
      continue;
15233
15234
0
    abfd = i->owner;
15235
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15236
0
      continue;
15237
15238
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15239
0
      return -1;
15240
15241
0
    if (_bfd_discard_section_stabs (abfd, i,
15242
0
            bfd_elf_reloc_symbol_deleted_p,
15243
0
            &cookie))
15244
0
      changed = 1;
15245
15246
0
    fini_reloc_cookie_for_section (&cookie, i);
15247
0
  }
15248
0
    }
15249
15250
0
  o = NULL;
15251
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15252
0
    o = bfd_get_section_by_name (info->output_bfd, ".eh_frame");
15253
0
  if (o != NULL)
15254
0
    {
15255
0
      asection *i;
15256
0
      int eh_changed = 0;
15257
0
      unsigned int eh_alignment;  /* Octets.  */
15258
15259
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15260
0
  {
15261
0
    int r;
15262
15263
0
    if (i->size == 0)
15264
0
      continue;
15265
15266
0
    abfd = i->owner;
15267
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15268
0
      continue;
15269
15270
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15271
0
      return -1;
15272
15273
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15274
0
    r = _bfd_elf_discard_section_eh_frame (abfd, info, i,
15275
0
             bfd_elf_reloc_symbol_deleted_p,
15276
0
             &cookie);
15277
0
    if (r)
15278
0
      {
15279
0
        eh_changed = 1;
15280
0
        if (r >= 2)
15281
0
    changed = 1;
15282
0
      }
15283
15284
0
    fini_reloc_cookie_for_section (&cookie, i);
15285
0
  }
15286
15287
0
      eh_alignment = ((1 << o->alignment_power)
15288
0
          * bfd_octets_per_byte (info->output_bfd, o));
15289
      /* Skip over zero terminator, and prevent empty sections from
15290
   adding alignment padding at the end.  */
15291
0
      for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15292
0
  if (i->size == 0)
15293
0
    i->flags |= SEC_EXCLUDE;
15294
0
  else if (i->size > 4)
15295
0
    break;
15296
      /* The last non-empty eh_frame section doesn't need padding.  */
15297
0
      if (i != NULL)
15298
0
  i = i->map_tail.s;
15299
      /* Any prior sections must pad the last FDE out to the output
15300
   section alignment.  Otherwise we might have zero padding
15301
   between sections, which would be seen as a terminator.  */
15302
0
      for (; i != NULL; i = i->map_tail.s)
15303
0
  if (i->size == 4)
15304
    /* All but the last zero terminator should have been removed.  */
15305
0
    BFD_FAIL ();
15306
0
  else
15307
0
    {
15308
0
      bfd_size_type size
15309
0
        = (i->size + eh_alignment - 1) & -eh_alignment;
15310
0
      if (i->size != size)
15311
0
        {
15312
0
    i->size = size;
15313
0
    changed = 1;
15314
0
    eh_changed = 1;
15315
0
        }
15316
0
    }
15317
0
      if (eh_changed)
15318
0
  elf_link_hash_traverse (elf_hash_table (info),
15319
0
        _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15320
0
    }
15321
15322
0
  o = bfd_get_section_by_name (info->output_bfd, ".sframe");
15323
0
  if (o != NULL)
15324
0
    {
15325
0
      asection *i;
15326
15327
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15328
0
  {
15329
0
    if (i->size == 0)
15330
0
      continue;
15331
15332
0
    abfd = i->owner;
15333
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15334
0
      continue;
15335
15336
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15337
0
      return -1;
15338
15339
0
    if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15340
0
      {
15341
0
        if (_bfd_elf_discard_section_sframe (i,
15342
0
               bfd_elf_reloc_symbol_deleted_p,
15343
0
               &cookie))
15344
0
    {
15345
0
      if (i->size != i->rawsize)
15346
0
        changed = 1;
15347
0
    }
15348
0
      }
15349
0
    fini_reloc_cookie_for_section (&cookie, i);
15350
0
  }
15351
      /* Update the reference to the output .sframe section.  Used to
15352
   determine later if PT_GNU_SFRAME segment is to be generated.  */
15353
0
      if (!_bfd_elf_set_section_sframe (info->output_bfd, info))
15354
0
  return -1;
15355
0
    }
15356
15357
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15358
0
    {
15359
0
      elf_backend_data *bed;
15360
0
      asection *s;
15361
15362
0
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15363
0
  continue;
15364
0
      s = abfd->sections;
15365
0
      if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15366
0
  continue;
15367
15368
0
      bed = get_elf_backend_data (abfd);
15369
15370
0
      if (bed->elf_backend_discard_info != NULL)
15371
0
  {
15372
0
    if (!init_reloc_cookie (&cookie, abfd))
15373
0
      return -1;
15374
15375
0
    if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15376
0
      changed = 1;
15377
15378
0
    fini_reloc_cookie (&cookie, abfd);
15379
0
  }
15380
0
    }
15381
15382
0
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15383
0
    _bfd_elf_end_eh_frame_parsing (info);
15384
15385
0
  if (_bfd_elf_discard_section_eh_frame_hdr (info))
15386
0
    changed = 1;
15387
15388
0
  return changed;
15389
0
}
15390
15391
bool
15392
_bfd_elf_section_already_linked (bfd *abfd,
15393
         asection *sec,
15394
         struct bfd_link_info *info)
15395
0
{
15396
0
  flagword flags;
15397
0
  const char *name, *key;
15398
0
  struct bfd_section_already_linked *l;
15399
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
15400
15401
0
  if (sec->output_section == bfd_abs_section_ptr)
15402
0
    return false;
15403
15404
0
  flags = sec->flags;
15405
15406
  /* Return if it isn't a linkonce section.  A comdat group section
15407
     also has SEC_LINK_ONCE set.  */
15408
0
  if ((flags & SEC_LINK_ONCE) == 0)
15409
0
    return false;
15410
15411
  /* Don't put group member sections on our list of already linked
15412
     sections.  They are handled as a group via their group section.  */
15413
0
  if (elf_sec_group (sec) != NULL)
15414
0
    return false;
15415
15416
  /* For a SHT_GROUP section, use the group signature as the key.  */
15417
0
  name = sec->name;
15418
0
  if ((flags & SEC_GROUP) != 0
15419
0
      && elf_next_in_group (sec) != NULL
15420
0
      && elf_group_name (elf_next_in_group (sec)) != NULL)
15421
0
    key = elf_group_name (elf_next_in_group (sec));
15422
0
  else
15423
0
    {
15424
      /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
15425
0
      if (startswith (name, ".gnu.linkonce.")
15426
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15427
0
  key++;
15428
0
      else
15429
  /* Must be a user linkonce section that doesn't follow gcc's
15430
     naming convention.  In this case we won't be matching
15431
     single member groups.  */
15432
0
  key = name;
15433
0
    }
15434
15435
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
15436
15437
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15438
0
    {
15439
      /* We may have 2 different types of sections on the list: group
15440
   sections with a signature of <key> (<key> is some string),
15441
   and linkonce sections named .gnu.linkonce.<type>.<key>.
15442
   Match like sections.  LTO plugin sections are an exception.
15443
   They are always named .gnu.linkonce.t.<key> and match either
15444
   type of section.  */
15445
0
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15446
0
     && ((flags & SEC_GROUP) != 0
15447
0
         || strcmp (name, l->sec->name) == 0))
15448
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
15449
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
15450
0
  {
15451
    /* The section has already been linked.  See if we should
15452
       issue a warning.  */
15453
0
    if (!_bfd_handle_already_linked (sec, l, info))
15454
0
      return false;
15455
15456
0
    if (flags & SEC_GROUP)
15457
0
      {
15458
0
        asection *first = elf_next_in_group (sec);
15459
0
        asection *s = first;
15460
15461
0
        while (s != NULL)
15462
0
    {
15463
0
      s->output_section = bfd_abs_section_ptr;
15464
      /* Record which group discards it.  */
15465
0
      s->kept_section = l->sec;
15466
0
      s = elf_next_in_group (s);
15467
      /* These lists are circular.  */
15468
0
      if (s == first)
15469
0
        break;
15470
0
    }
15471
0
      }
15472
15473
0
    return true;
15474
0
  }
15475
0
    }
15476
15477
  /* A single member comdat group section may be discarded by a
15478
     linkonce section and vice versa.  */
15479
0
  if ((flags & SEC_GROUP) != 0)
15480
0
    {
15481
0
      asection *first = elf_next_in_group (sec);
15482
15483
0
      if (first != NULL && elf_next_in_group (first) == first)
15484
  /* Check this single member group against linkonce sections.  */
15485
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15486
0
    if ((l->sec->flags & SEC_GROUP) == 0
15487
0
        && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15488
0
      {
15489
0
        first->output_section = bfd_abs_section_ptr;
15490
0
        first->kept_section = l->sec;
15491
0
        sec->output_section = bfd_abs_section_ptr;
15492
0
        break;
15493
0
      }
15494
0
    }
15495
0
  else
15496
    /* Check this linkonce section against single member groups.  */
15497
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15498
0
      if (l->sec->flags & SEC_GROUP)
15499
0
  {
15500
0
    asection *first = elf_next_in_group (l->sec);
15501
15502
0
    if (first != NULL
15503
0
        && elf_next_in_group (first) == first
15504
0
        && bfd_elf_match_symbols_in_sections (first, sec, info))
15505
0
      {
15506
0
        sec->output_section = bfd_abs_section_ptr;
15507
0
        sec->kept_section = first;
15508
0
        break;
15509
0
      }
15510
0
  }
15511
15512
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15513
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15514
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15515
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
15516
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
15517
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15518
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
15519
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
15520
     The reverse order cannot happen as there is never a bfd with only the
15521
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
15522
     matter as here were are looking only for cross-bfd sections.  */
15523
15524
0
  if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15525
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15526
0
      if ((l->sec->flags & SEC_GROUP) == 0
15527
0
    && startswith (l->sec->name, ".gnu.linkonce.t."))
15528
0
  {
15529
0
    if (abfd != l->sec->owner)
15530
0
      sec->output_section = bfd_abs_section_ptr;
15531
0
    break;
15532
0
  }
15533
15534
  /* This is the first section with this name.  Record it.  */
15535
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15536
0
    info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
15537
0
  return sec->output_section == bfd_abs_section_ptr;
15538
0
}
15539
15540
bool
15541
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15542
0
{
15543
0
  return sym->st_shndx == SHN_COMMON;
15544
0
}
15545
15546
unsigned int
15547
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15548
0
{
15549
0
  return SHN_COMMON;
15550
0
}
15551
15552
asection *
15553
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15554
0
{
15555
0
  return bfd_com_section_ptr;
15556
0
}
15557
15558
bfd_vma
15559
_bfd_elf_default_got_elt_size (struct bfd_link_info *info,
15560
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15561
             bfd *ibfd ATTRIBUTE_UNUSED,
15562
             unsigned long symndx ATTRIBUTE_UNUSED)
15563
0
{
15564
0
  elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15565
0
  return obed->s->arch_size / 8;
15566
0
}
15567
15568
/* Routines to support the creation of dynamic relocs.  */
15569
15570
/* Returns the name of the dynamic reloc section associated with SEC.  */
15571
15572
static char *
15573
get_dynamic_reloc_section_name (bfd *abfd,
15574
        asection *sec,
15575
        bool is_rela)
15576
0
{
15577
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15578
0
  size_t plen = is_rela ? 5 : 4;
15579
0
  const char *old_name = bfd_section_name (sec);
15580
0
  size_t nlen = strlen (old_name);
15581
0
  char *name = bfd_alloc (abfd, plen + nlen + 1);
15582
0
  if (name == NULL)
15583
0
    return NULL;
15584
15585
0
  memcpy (name, prefix, plen);
15586
0
  memcpy (name + plen, old_name, nlen + 1);
15587
0
  return name;
15588
0
}
15589
15590
/* Returns the dynamic reloc section associated with SEC.  If the
15591
   section does not exist it is created and attached to the DYNOBJ
15592
   bfd and stored in the SRELOC field of SEC's elf_section_data
15593
   structure.
15594
15595
   ALIGNMENT is the alignment for the newly created section and
15596
   IS_RELA defines whether the name should be .rela.<SEC's name>
15597
   or .rel.<SEC's name>.  The section name is looked up in the
15598
   string table associated with ABFD.  */
15599
15600
asection *
15601
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15602
             bfd *dynobj,
15603
             unsigned int alignment,
15604
             bfd *abfd,
15605
             bool is_rela)
15606
0
{
15607
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15608
15609
0
  if (reloc_sec == NULL)
15610
0
    {
15611
0
      bool known = (dynobj != NULL
15612
0
        && (get_elf_backend_data (dynobj)
15613
0
      ->relocs_compatible (dynobj->xvec, abfd->xvec)));
15614
0
      BFD_ASSERT (known);
15615
0
      if (!known
15616
0
    || dynobj->xvec->byteorder != abfd->xvec->byteorder)
15617
0
  {
15618
0
    _bfd_error_handler (_("%s dynamic relocs incompatible with%s%s output"),
15619
0
            bfd_get_target (abfd),
15620
0
            dynobj ? " " : "",
15621
0
            dynobj ? bfd_get_target (dynobj) : "");
15622
0
    return NULL;
15623
0
  }
15624
15625
0
      char *name = get_dynamic_reloc_section_name (dynobj, sec, is_rela);
15626
0
      if (name == NULL)
15627
0
  return NULL;
15628
15629
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15630
0
      if (reloc_sec != NULL)
15631
0
  bfd_release (dynobj, name);
15632
0
      else
15633
0
  {
15634
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15635
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15636
0
    if ((sec->flags & SEC_ALLOC) != 0)
15637
0
      flags |= SEC_ALLOC | SEC_LOAD;
15638
15639
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15640
0
    if (reloc_sec != NULL)
15641
0
      {
15642
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15643
     name.  Override as it may be wrong, eg. for a user
15644
     section named "auto" we'll get ".relauto" which is
15645
     seen to be a .rela section.  */
15646
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15647
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15648
0
    reloc_sec = NULL;
15649
0
      }
15650
0
  }
15651
15652
0
      elf_section_data (sec)->sreloc = reloc_sec;
15653
0
    }
15654
15655
0
  return reloc_sec;
15656
0
}
15657
15658
/* Copy the ELF symbol type and other attributes for a linker script
15659
   assignment from HSRC to HDEST.  Generally this should be treated as
15660
   if we found a strong non-dynamic definition for HDEST (except that
15661
   ld ignores multiple definition errors).  */
15662
void
15663
_bfd_elf_copy_link_hash_symbol_type (bfd *obfd,
15664
             struct bfd_link_hash_entry *hdest,
15665
             struct bfd_link_hash_entry *hsrc)
15666
0
{
15667
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15668
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15669
15670
0
  ehdest->type = ehsrc->type;
15671
0
  ehdest->target_internal = ehsrc->target_internal;
15672
15673
0
  elf_merge_st_other (obfd, obfd, ehdest, ehsrc->other, NULL,
15674
0
          true, false);
15675
0
}
15676
15677
/* Append a RELA relocation REL to section S.  */
15678
15679
void
15680
_bfd_elf_append_rela (bfd *obfd, asection *s, Elf_Internal_Rela *rel)
15681
0
{
15682
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15683
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * obed->s->sizeof_rela);
15684
0
  BFD_ASSERT (loc + obed->s->sizeof_rela <= s->contents + s->size);
15685
0
  obed->s->swap_reloca_out (obfd, rel, loc);
15686
0
}
15687
15688
/* Append a REL relocation REL to section S.  */
15689
15690
void
15691
_bfd_elf_append_rel (bfd *obfd, asection *s, Elf_Internal_Rela *rel)
15692
0
{
15693
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15694
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * obed->s->sizeof_rel);
15695
0
  BFD_ASSERT (loc + obed->s->sizeof_rel <= s->contents + s->size);
15696
0
  obed->s->swap_reloc_out (obfd, rel, loc);
15697
0
}
15698
15699
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15700
15701
struct bfd_link_hash_entry *
15702
bfd_elf_define_start_stop (struct bfd_link_info *info,
15703
         const char *symbol, asection *sec)
15704
0
{
15705
0
  struct elf_link_hash_entry *h;
15706
15707
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15708
0
          false, false, true);
15709
  /* NB: Common symbols will be turned into definition later.  */
15710
0
  if (h != NULL
15711
0
      && !h->root.ldscript_def
15712
0
      && (h->root.type == bfd_link_hash_undefined
15713
0
    || h->root.type == bfd_link_hash_undefweak
15714
0
    || ((h->ref_regular || h->def_dynamic)
15715
0
        && !h->def_regular
15716
0
        && h->root.type != bfd_link_hash_common)))
15717
0
    {
15718
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15719
0
      h->verinfo.verdef = NULL;
15720
0
      h->root.type = bfd_link_hash_defined;
15721
0
      h->root.u.def.section = sec;
15722
0
      h->root.u.def.value = 0;
15723
0
      h->def_regular = 1;
15724
0
      h->def_dynamic = 0;
15725
0
      h->start_stop = 1;
15726
0
      h->u2.start_stop_section = sec;
15727
0
      if (symbol[0] == '.')
15728
0
  {
15729
    /* .startof. and .sizeof. symbols are local.  */
15730
0
    elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15731
0
    obed->elf_backend_hide_symbol (info, h, true);
15732
0
  }
15733
0
      else
15734
0
  {
15735
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15736
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15737
0
      | info->start_stop_visibility);
15738
0
    if (was_dynamic)
15739
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15740
0
  }
15741
0
      return &h->root;
15742
0
    }
15743
0
  return NULL;
15744
0
}
15745
15746
/* Find dynamic relocs for H that apply to read-only sections.  */
15747
15748
asection *
15749
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15750
0
{
15751
0
  struct elf_dyn_relocs *p;
15752
15753
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15754
0
    {
15755
0
      asection *s = p->sec->output_section;
15756
15757
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15758
0
  return p->sec;
15759
0
    }
15760
0
  return NULL;
15761
0
}
15762
15763
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15764
   read-only sections.  */
15765
15766
bool
15767
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15768
0
{
15769
0
  asection *sec;
15770
15771
0
  if (h->root.type == bfd_link_hash_indirect)
15772
0
    return true;
15773
15774
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15775
0
  if (sec != NULL)
15776
0
    {
15777
0
      struct bfd_link_info *info = inf;
15778
15779
0
      info->flags |= DF_TEXTREL;
15780
      /* xgettext:c-format */
15781
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15782
0
        "in read-only section `%pA'\n"),
15783
0
            sec->owner, h->root.root.string, sec);
15784
15785
0
      if (bfd_link_textrel_check (info))
15786
  /* xgettext:c-format */
15787
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15788
0
          "in read-only section `%pA'\n"),
15789
0
        sec->owner, h->root.root.string, sec);
15790
15791
      /* Not an error, just cut short the traversal.  */
15792
0
      return false;
15793
0
    }
15794
0
  return true;
15795
0
}
15796
15797
/* Add dynamic tags.  */
15798
15799
bool
15800
_bfd_elf_add_dynamic_tags (struct bfd_link_info *info, bool need_dynamic_reloc)
15801
0
{
15802
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15803
15804
0
  if (htab->dynamic_sections_created)
15805
0
    {
15806
      /* Add some entries to the .dynamic section.  We fill in the
15807
   values later, in finish_dynamic_sections, but we must add
15808
   the entries now so that we get the correct size for the
15809
   .dynamic section.  The DT_DEBUG entry is filled in by the
15810
   dynamic linker and used by the debugger.  */
15811
0
#define add_dynamic_entry(TAG, VAL) \
15812
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15813
15814
0
      elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15815
15816
0
      if (bfd_link_executable (info))
15817
0
  {
15818
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15819
0
      return false;
15820
0
  }
15821
15822
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15823
0
  {
15824
    /* DT_PLTGOT is used by prelink even if there is no PLT
15825
       relocation.  */
15826
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15827
0
      return false;
15828
0
  }
15829
15830
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15831
0
  {
15832
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15833
0
        || !add_dynamic_entry (DT_PLTREL,
15834
0
             (obed->rela_plts_and_copies_p
15835
0
              ? DT_RELA : DT_REL))
15836
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15837
0
      return false;
15838
0
  }
15839
15840
0
      if (htab->tlsdesc_plt
15841
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15842
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15843
0
  return false;
15844
15845
0
      if (need_dynamic_reloc)
15846
0
  {
15847
0
    if (obed->rela_plts_and_copies_p)
15848
0
      {
15849
0
        if (!add_dynamic_entry (DT_RELA, 0)
15850
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15851
0
      || !add_dynamic_entry (DT_RELAENT,
15852
0
           obed->s->sizeof_rela))
15853
0
    return false;
15854
0
      }
15855
0
    else
15856
0
      {
15857
0
        if (!add_dynamic_entry (DT_REL, 0)
15858
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15859
0
      || !add_dynamic_entry (DT_RELENT,
15860
0
           obed->s->sizeof_rel))
15861
0
    return false;
15862
0
      }
15863
15864
    /* If any dynamic relocs apply to a read-only section,
15865
       then we need a DT_TEXTREL entry.  */
15866
0
    if ((info->flags & DF_TEXTREL) == 0)
15867
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15868
0
            info);
15869
15870
0
    if ((info->flags & DF_TEXTREL) != 0)
15871
0
      {
15872
0
        if (htab->ifunc_resolvers)
15873
0
    info->callbacks->einfo
15874
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15875
0
         "may result in a segfault at runtime; recompile with %s\n"),
15876
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15877
15878
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15879
0
    return false;
15880
0
      }
15881
0
  }
15882
0
    }
15883
0
#undef add_dynamic_entry
15884
15885
0
  return true;
15886
0
}