Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/bfd/elflink.c
Line
Count
Source (jump to first uncovered line)
1
/* ELF linking support for BFD.
2
   Copyright (C) 1995-2024 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "bfd.h"
23
#include "bfdlink.h"
24
#include "libbfd.h"
25
#define ARCH_SIZE 0
26
#include "elf-bfd.h"
27
#include "safe-ctype.h"
28
#include "libiberty.h"
29
#include "objalloc.h"
30
#if BFD_SUPPORTS_PLUGINS
31
#include "plugin-api.h"
32
#include "plugin.h"
33
#endif
34
35
#include <limits.h>
36
#ifndef CHAR_BIT
37
#define CHAR_BIT 8
38
#endif
39
40
/* This struct is used to pass information to routines called via
41
   elf_link_hash_traverse which must return failure.  */
42
43
struct elf_info_failed
44
{
45
  struct bfd_link_info *info;
46
  bool failed;
47
};
48
49
static bool _bfd_elf_fix_symbol_flags
50
  (struct elf_link_hash_entry *, struct elf_info_failed *);
51
52
/* Return false if linker should avoid caching relocation information
53
   and symbol tables of input files in memory.  */
54
55
static bool
56
_bfd_elf_link_keep_memory (struct bfd_link_info *info)
57
0
{
58
0
#ifdef USE_MMAP
59
  /* Don't cache symbol nor relocation tables if they are mapped in.
60
     NB: Since the --no-keep-memory linker option causes:
61
62
     https://sourceware.org/bugzilla/show_bug.cgi?id=31458
63
64
     this is opt-in by each backend.  */
65
0
  const struct elf_backend_data *bed
66
0
    = get_elf_backend_data (info->output_bfd);
67
0
  if (bed->use_mmap)
68
0
    return false;
69
0
#endif
70
0
  bfd *abfd;
71
0
  bfd_size_type size;
72
73
0
  if (!info->keep_memory)
74
0
    return false;
75
76
0
  if (info->max_cache_size == (bfd_size_type) -1)
77
0
    return true;
78
79
0
  abfd = info->input_bfds;
80
0
  size = info->cache_size;
81
0
  do
82
0
    {
83
0
      if (size >= info->max_cache_size)
84
0
  {
85
    /* Over the limit.  Reduce the memory usage.  */
86
0
    info->keep_memory = false;
87
0
    return false;
88
0
  }
89
0
      if (!abfd)
90
0
  break;
91
0
      size += abfd->alloc_size;
92
0
      abfd = abfd->link.next;
93
0
    }
94
0
  while (1);
95
96
0
  return true;
97
0
}
98
99
asection *
100
_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
101
           unsigned long r_symndx,
102
           bool discard)
103
0
{
104
0
  if (r_symndx >= cookie->locsymcount
105
0
      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
106
0
    {
107
0
      struct elf_link_hash_entry *h;
108
109
0
      h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
110
111
0
      while (h->root.type == bfd_link_hash_indirect
112
0
       || h->root.type == bfd_link_hash_warning)
113
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
114
115
0
      if ((h->root.type == bfd_link_hash_defined
116
0
     || h->root.type == bfd_link_hash_defweak)
117
0
     && discarded_section (h->root.u.def.section))
118
0
  return h->root.u.def.section;
119
0
      else
120
0
  return NULL;
121
0
    }
122
0
  else
123
0
    {
124
      /* It's not a relocation against a global symbol,
125
   but it could be a relocation against a local
126
   symbol for a discarded section.  */
127
0
      asection *isec;
128
0
      Elf_Internal_Sym *isym;
129
130
      /* Need to: get the symbol; get the section.  */
131
0
      isym = &cookie->locsyms[r_symndx];
132
0
      isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
133
0
      if (isec != NULL
134
0
    && discard ? discarded_section (isec) : 1)
135
0
  return isec;
136
0
     }
137
0
  return NULL;
138
0
}
139
140
/* Define a symbol in a dynamic linkage section.  */
141
142
struct elf_link_hash_entry *
143
_bfd_elf_define_linkage_sym (bfd *abfd,
144
           struct bfd_link_info *info,
145
           asection *sec,
146
           const char *name)
147
0
{
148
0
  struct elf_link_hash_entry *h;
149
0
  struct bfd_link_hash_entry *bh;
150
0
  const struct elf_backend_data *bed;
151
152
0
  h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
153
0
  if (h != NULL)
154
0
    {
155
      /* Zap symbol defined in an as-needed lib that wasn't linked.
156
   This is a symptom of a larger problem:  Absolute symbols
157
   defined in shared libraries can't be overridden, because we
158
   lose the link to the bfd which is via the symbol section.  */
159
0
      h->root.type = bfd_link_hash_new;
160
0
      bh = &h->root;
161
0
    }
162
0
  else
163
0
    bh = NULL;
164
165
0
  bed = get_elf_backend_data (abfd);
166
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
167
0
           sec, 0, NULL, false, bed->collect,
168
0
           &bh))
169
0
    return NULL;
170
0
  h = (struct elf_link_hash_entry *) bh;
171
0
  BFD_ASSERT (h != NULL);
172
0
  h->def_regular = 1;
173
0
  h->non_elf = 0;
174
0
  h->root.linker_def = 1;
175
0
  h->type = STT_OBJECT;
176
0
  if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
177
0
    h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
178
179
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
180
0
  return h;
181
0
}
182
183
bool
184
_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
185
0
{
186
0
  flagword flags;
187
0
  asection *s;
188
0
  struct elf_link_hash_entry *h;
189
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
190
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
191
192
  /* This function may be called more than once.  */
193
0
  if (htab->sgot != NULL)
194
0
    return true;
195
196
0
  flags = bed->dynamic_sec_flags;
197
198
0
  s = bfd_make_section_anyway_with_flags (abfd,
199
0
            (bed->rela_plts_and_copies_p
200
0
             ? ".rela.got" : ".rel.got"),
201
0
            (bed->dynamic_sec_flags
202
0
             | SEC_READONLY));
203
0
  if (s == NULL
204
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
205
0
    return false;
206
0
  htab->srelgot = s;
207
208
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
209
0
  if (s == NULL
210
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
211
0
    return false;
212
0
  htab->sgot = s;
213
214
0
  if (bed->want_got_plt)
215
0
    {
216
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
217
0
      if (s == NULL
218
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
219
0
  return false;
220
0
      htab->sgotplt = s;
221
0
    }
222
223
  /* The first bit of the global offset table is the header.  */
224
0
  s->size += bed->got_header_size;
225
226
0
  if (bed->want_got_sym)
227
0
    {
228
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
229
   (or .got.plt) section.  We don't do this in the linker script
230
   because we don't want to define the symbol if we are not creating
231
   a global offset table.  */
232
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
233
0
               "_GLOBAL_OFFSET_TABLE_");
234
0
      elf_hash_table (info)->hgot = h;
235
0
      if (h == NULL)
236
0
  return false;
237
0
    }
238
239
0
  return true;
240
0
}
241

242
/* Create a strtab to hold the dynamic symbol names.  */
243
static bool
244
_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
245
0
{
246
0
  struct elf_link_hash_table *hash_table;
247
248
0
  hash_table = elf_hash_table (info);
249
0
  if (hash_table->dynobj == NULL)
250
0
    {
251
      /* We may not set dynobj, an input file holding linker created
252
   dynamic sections to abfd, which may be a dynamic object with
253
   its own dynamic sections.  We need to find a normal input file
254
   to hold linker created sections if possible.  */
255
0
      if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
256
0
  {
257
0
    bfd *ibfd;
258
0
    asection *s;
259
0
    for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
260
0
      if ((ibfd->flags
261
0
     & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
262
0
    && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
263
0
    && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
264
0
    && !((s = ibfd->sections) != NULL
265
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
266
0
        {
267
0
    abfd = ibfd;
268
0
    break;
269
0
        }
270
0
  }
271
0
      hash_table->dynobj = abfd;
272
0
    }
273
274
0
  if (hash_table->dynstr == NULL)
275
0
    {
276
0
      hash_table->dynstr = _bfd_elf_strtab_init ();
277
0
      if (hash_table->dynstr == NULL)
278
0
  return false;
279
0
    }
280
0
  return true;
281
0
}
282
283
/* Create some sections which will be filled in with dynamic linking
284
   information.  ABFD is an input file which requires dynamic sections
285
   to be created.  The dynamic sections take up virtual memory space
286
   when the final executable is run, so we need to create them before
287
   addresses are assigned to the output sections.  We work out the
288
   actual contents and size of these sections later.  */
289
290
bool
291
_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
292
0
{
293
0
  flagword flags;
294
0
  asection *s;
295
0
  const struct elf_backend_data *bed;
296
0
  struct elf_link_hash_entry *h;
297
298
0
  if (! is_elf_hash_table (info->hash))
299
0
    return false;
300
301
0
  if (elf_hash_table (info)->dynamic_sections_created)
302
0
    return true;
303
304
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
305
0
    return false;
306
307
0
  abfd = elf_hash_table (info)->dynobj;
308
0
  bed = get_elf_backend_data (abfd);
309
310
0
  flags = bed->dynamic_sec_flags;
311
312
  /* A dynamically linked executable has a .interp section, but a
313
     shared library does not.  */
314
0
  if (bfd_link_executable (info) && !info->nointerp)
315
0
    {
316
0
      s = bfd_make_section_anyway_with_flags (abfd, ".interp",
317
0
                flags | SEC_READONLY);
318
0
      if (s == NULL)
319
0
  return false;
320
0
    }
321
322
  /* Create sections to hold version informations.  These are removed
323
     if they are not needed.  */
324
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
325
0
            flags | SEC_READONLY);
326
0
  if (s == NULL
327
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
328
0
    return false;
329
330
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
331
0
            flags | SEC_READONLY);
332
0
  if (s == NULL
333
0
      || !bfd_set_section_alignment (s, 1))
334
0
    return false;
335
336
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
337
0
            flags | SEC_READONLY);
338
0
  if (s == NULL
339
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
340
0
    return false;
341
342
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
343
0
            flags | SEC_READONLY);
344
0
  if (s == NULL
345
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
346
0
    return false;
347
0
  elf_hash_table (info)->dynsym = s;
348
349
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
350
0
            flags | SEC_READONLY);
351
0
  if (s == NULL)
352
0
    return false;
353
354
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
355
0
  if (s == NULL
356
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
357
0
    return false;
358
0
  elf_hash_table (info)->dynamic = s;
359
360
  /* The special symbol _DYNAMIC is always set to the start of the
361
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
362
     only want to define it if we are, in fact, creating a .dynamic
363
     section.  We don't want to define it if there is no .dynamic
364
     section, since on some ELF platforms the start up code examines it
365
     to decide how to initialize the process.  */
366
0
  h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
367
0
  elf_hash_table (info)->hdynamic = h;
368
0
  if (h == NULL)
369
0
    return false;
370
371
0
  if (info->emit_hash)
372
0
    {
373
0
      s = bfd_make_section_anyway_with_flags (abfd, ".hash",
374
0
                flags | SEC_READONLY);
375
0
      if (s == NULL
376
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
377
0
  return false;
378
0
      elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
379
0
    }
380
381
0
  if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
382
0
    {
383
0
      s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
384
0
                flags | SEC_READONLY);
385
0
      if (s == NULL
386
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
387
0
  return false;
388
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
389
   4 32-bit words followed by variable count of 64-bit words, then
390
   variable count of 32-bit words.  */
391
0
      if (bed->s->arch_size == 64)
392
0
  elf_section_data (s)->this_hdr.sh_entsize = 0;
393
0
      else
394
0
  elf_section_data (s)->this_hdr.sh_entsize = 4;
395
0
    }
396
397
0
  if (info->enable_dt_relr)
398
0
    {
399
0
      s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
400
0
                (bed->dynamic_sec_flags
401
0
                 | SEC_READONLY));
402
0
      if (s == NULL
403
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
404
0
  return false;
405
0
      elf_hash_table (info)->srelrdyn = s;
406
0
    }
407
408
  /* Let the backend create the rest of the sections.  This lets the
409
     backend set the right flags.  The backend will normally create
410
     the .got and .plt sections.  */
411
0
  if (bed->elf_backend_create_dynamic_sections == NULL
412
0
      || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
413
0
    return false;
414
415
0
  elf_hash_table (info)->dynamic_sections_created = true;
416
417
0
  return true;
418
0
}
419
420
/* Create dynamic sections when linking against a dynamic object.  */
421
422
bool
423
_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
424
0
{
425
0
  flagword flags, pltflags;
426
0
  struct elf_link_hash_entry *h;
427
0
  asection *s;
428
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
429
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
430
431
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
432
     .rel[a].bss sections.  */
433
0
  flags = bed->dynamic_sec_flags;
434
435
0
  pltflags = flags;
436
0
  if (bed->plt_not_loaded)
437
    /* We do not clear SEC_ALLOC here because we still want the OS to
438
       allocate space for the section; it's just that there's nothing
439
       to read in from the object file.  */
440
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
441
0
  else
442
0
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
443
0
  if (bed->plt_readonly)
444
0
    pltflags |= SEC_READONLY;
445
446
0
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
447
0
  if (s == NULL
448
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
449
0
    return false;
450
0
  htab->splt = s;
451
452
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
453
     .plt section.  */
454
0
  if (bed->want_plt_sym)
455
0
    {
456
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
457
0
               "_PROCEDURE_LINKAGE_TABLE_");
458
0
      elf_hash_table (info)->hplt = h;
459
0
      if (h == NULL)
460
0
  return false;
461
0
    }
462
463
0
  s = bfd_make_section_anyway_with_flags (abfd,
464
0
            (bed->rela_plts_and_copies_p
465
0
             ? ".rela.plt" : ".rel.plt"),
466
0
            flags | SEC_READONLY);
467
0
  if (s == NULL
468
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
469
0
    return false;
470
0
  htab->srelplt = s;
471
472
0
  if (! _bfd_elf_create_got_section (abfd, info))
473
0
    return false;
474
475
0
  if (bed->want_dynbss)
476
0
    {
477
      /* The .dynbss section is a place to put symbols which are defined
478
   by dynamic objects, are referenced by regular objects, and are
479
   not functions.  We must allocate space for them in the process
480
   image and use a R_*_COPY reloc to tell the dynamic linker to
481
   initialize them at run time.  The linker script puts the .dynbss
482
   section into the .bss section of the final image.  */
483
0
      s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
484
0
                SEC_ALLOC | SEC_LINKER_CREATED);
485
0
      if (s == NULL)
486
0
  return false;
487
0
      htab->sdynbss = s;
488
489
0
      if (bed->want_dynrelro)
490
0
  {
491
    /* Similarly, but for symbols that were originally in read-only
492
       sections.  This section doesn't really need to have contents,
493
       but make it like other .data.rel.ro sections.  */
494
0
    s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
495
0
              flags);
496
0
    if (s == NULL)
497
0
      return false;
498
0
    htab->sdynrelro = s;
499
0
  }
500
501
      /* The .rel[a].bss section holds copy relocs.  This section is not
502
   normally needed.  We need to create it here, though, so that the
503
   linker will map it to an output section.  We can't just create it
504
   only if we need it, because we will not know whether we need it
505
   until we have seen all the input files, and the first time the
506
   main linker code calls BFD after examining all the input files
507
   (size_dynamic_sections) the input sections have already been
508
   mapped to the output sections.  If the section turns out not to
509
   be needed, we can discard it later.  We will never need this
510
   section when generating a shared object, since they do not use
511
   copy relocs.  */
512
0
      if (bfd_link_executable (info))
513
0
  {
514
0
    s = bfd_make_section_anyway_with_flags (abfd,
515
0
              (bed->rela_plts_and_copies_p
516
0
               ? ".rela.bss" : ".rel.bss"),
517
0
              flags | SEC_READONLY);
518
0
    if (s == NULL
519
0
        || !bfd_set_section_alignment (s, bed->s->log_file_align))
520
0
      return false;
521
0
    htab->srelbss = s;
522
523
0
    if (bed->want_dynrelro)
524
0
      {
525
0
        s = (bfd_make_section_anyway_with_flags
526
0
       (abfd, (bed->rela_plts_and_copies_p
527
0
         ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
528
0
        flags | SEC_READONLY));
529
0
        if (s == NULL
530
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
531
0
    return false;
532
0
        htab->sreldynrelro = s;
533
0
      }
534
0
  }
535
0
    }
536
537
0
  return true;
538
0
}
539

540
/* Record a new dynamic symbol.  We record the dynamic symbols as we
541
   read the input files, since we need to have a list of all of them
542
   before we can determine the final sizes of the output sections.
543
   Note that we may actually call this function even though we are not
544
   going to output any dynamic symbols; in some cases we know that a
545
   symbol should be in the dynamic symbol table, but only if there is
546
   one.  */
547
548
bool
549
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
550
            struct elf_link_hash_entry *h)
551
0
{
552
0
  if (h->dynindx == -1)
553
0
    {
554
0
      struct elf_strtab_hash *dynstr;
555
0
      char *p;
556
0
      const char *name;
557
0
      size_t indx;
558
559
0
      if (h->root.type == bfd_link_hash_defined
560
0
    || h->root.type == bfd_link_hash_defweak)
561
0
  {
562
    /* An IR symbol should not be made dynamic.  */
563
0
    if (h->root.u.def.section != NULL
564
0
        && h->root.u.def.section->owner != NULL
565
0
        && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
566
0
      return true;
567
0
  }
568
569
      /* XXX: The ABI draft says the linker must turn hidden and
570
   internal symbols into STB_LOCAL symbols when producing the
571
   DSO. However, if ld.so honors st_other in the dynamic table,
572
   this would not be necessary.  */
573
0
      switch (ELF_ST_VISIBILITY (h->other))
574
0
  {
575
0
  case STV_INTERNAL:
576
0
  case STV_HIDDEN:
577
0
    if (h->root.type != bfd_link_hash_undefined
578
0
        && h->root.type != bfd_link_hash_undefweak)
579
0
      {
580
0
        h->forced_local = 1;
581
0
        return true;
582
0
      }
583
584
0
  default:
585
0
    break;
586
0
  }
587
588
0
      h->dynindx = elf_hash_table (info)->dynsymcount;
589
0
      ++elf_hash_table (info)->dynsymcount;
590
591
0
      dynstr = elf_hash_table (info)->dynstr;
592
0
      if (dynstr == NULL)
593
0
  {
594
    /* Create a strtab to hold the dynamic symbol names.  */
595
0
    elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
596
0
    if (dynstr == NULL)
597
0
      return false;
598
0
  }
599
600
0
      char *unversioned_name = NULL;
601
602
      /* We don't put any version information in the dynamic string
603
   table.  */
604
0
      name = h->root.root.string;
605
0
      p = strchr (name, ELF_VER_CHR);
606
0
      if (p != NULL)
607
0
  {
608
0
    unversioned_name = bfd_malloc (p - name + 1);
609
0
    memcpy (unversioned_name, name, p - name);
610
0
    unversioned_name[p - name] = 0;
611
0
    name = unversioned_name;
612
0
  }
613
614
0
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
615
616
0
      if (p != NULL)
617
0
  free (unversioned_name);
618
619
0
      if (indx == (size_t) -1)
620
0
  return false;
621
0
      h->dynstr_index = indx;
622
0
    }
623
624
0
  return true;
625
0
}
626

627
/* Mark a symbol dynamic.  */
628
629
static void
630
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
631
          struct elf_link_hash_entry *h,
632
          Elf_Internal_Sym *sym)
633
0
{
634
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
635
636
  /* It may be called more than once on the same H.  */
637
0
  if(h->dynamic || bfd_link_relocatable (info))
638
0
    return;
639
640
0
  if ((info->dynamic_data
641
0
       && (h->type == STT_OBJECT
642
0
     || h->type == STT_COMMON
643
0
     || (sym != NULL
644
0
         && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
645
0
       || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
646
0
      || (d != NULL
647
0
    && h->non_elf
648
0
    && (*d->match) (&d->head, NULL, h->root.root.string)))
649
0
    {
650
0
      h->dynamic = 1;
651
      /* NB: If a symbol is made dynamic by --dynamic-list, it has
652
   non-IR reference.  */
653
0
      h->root.non_ir_ref_dynamic = 1;
654
0
    }
655
0
}
656
657
/* Record an assignment to a symbol made by a linker script.  We need
658
   this in case some dynamic object refers to this symbol.  */
659
660
bool
661
bfd_elf_record_link_assignment (bfd *output_bfd,
662
        struct bfd_link_info *info,
663
        const char *name,
664
        bool provide,
665
        bool hidden)
666
0
{
667
0
  struct elf_link_hash_entry *h, *hv;
668
0
  struct elf_link_hash_table *htab;
669
0
  const struct elf_backend_data *bed;
670
671
0
  if (!is_elf_hash_table (info->hash))
672
0
    return true;
673
674
0
  htab = elf_hash_table (info);
675
0
  h = elf_link_hash_lookup (htab, name, !provide, true, false);
676
0
  if (h == NULL)
677
0
    return provide;
678
679
0
  if (h->root.type == bfd_link_hash_warning)
680
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
681
682
0
  if (h->versioned == unknown)
683
0
    {
684
      /* Set versioned if symbol version is unknown.  */
685
0
      char *version = strrchr (name, ELF_VER_CHR);
686
0
      if (version)
687
0
  {
688
0
    if (version > name && version[-1] != ELF_VER_CHR)
689
0
      h->versioned = versioned_hidden;
690
0
    else
691
0
      h->versioned = versioned;
692
0
  }
693
0
    }
694
695
  /* Symbols defined in a linker script but not referenced anywhere
696
     else will have non_elf set.  */
697
0
  if (h->non_elf)
698
0
    {
699
0
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
700
0
      h->non_elf = 0;
701
0
    }
702
703
0
  switch (h->root.type)
704
0
    {
705
0
    case bfd_link_hash_defined:
706
0
    case bfd_link_hash_defweak:
707
0
    case bfd_link_hash_common:
708
0
      break;
709
0
    case bfd_link_hash_undefweak:
710
0
    case bfd_link_hash_undefined:
711
      /* Since we're defining the symbol, don't let it seem to have not
712
   been defined.  record_dynamic_symbol and size_dynamic_sections
713
   may depend on this.  */
714
0
      h->root.type = bfd_link_hash_new;
715
0
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
716
0
  bfd_link_repair_undef_list (&htab->root);
717
0
      break;
718
0
    case bfd_link_hash_new:
719
0
      break;
720
0
    case bfd_link_hash_indirect:
721
      /* We had a versioned symbol in a dynamic library.  We make the
722
   the versioned symbol point to this one.  */
723
0
      bed = get_elf_backend_data (output_bfd);
724
0
      hv = h;
725
0
      while (hv->root.type == bfd_link_hash_indirect
726
0
       || hv->root.type == bfd_link_hash_warning)
727
0
  hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
728
      /* We don't need to update h->root.u since linker will set them
729
   later.  */
730
0
      h->root.type = bfd_link_hash_undefined;
731
0
      hv->root.type = bfd_link_hash_indirect;
732
0
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
733
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
734
0
      break;
735
0
    default:
736
0
      BFD_FAIL ();
737
0
      return false;
738
0
    }
739
740
  /* If this symbol is being provided by the linker script, and it is
741
     currently defined by a dynamic object, but not by a regular
742
     object, then mark it as undefined so that the generic linker will
743
     force the correct value.  */
744
0
  if (provide
745
0
      && h->def_dynamic
746
0
      && !h->def_regular)
747
0
    h->root.type = bfd_link_hash_undefined;
748
749
  /* If this symbol is currently defined by a dynamic object, but not
750
     by a regular object, then clear out any version information because
751
     the symbol will not be associated with the dynamic object any
752
     more.  */
753
0
  if (h->def_dynamic && !h->def_regular)
754
0
    h->verinfo.verdef = NULL;
755
756
  /* Make sure this symbol is not garbage collected.  */
757
0
  h->mark = 1;
758
759
0
  h->def_regular = 1;
760
761
0
  if (hidden)
762
0
    {
763
0
      bed = get_elf_backend_data (output_bfd);
764
0
      if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
765
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
766
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
767
0
    }
768
769
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
770
     and executables.  */
771
0
  if (!bfd_link_relocatable (info)
772
0
      && h->dynindx != -1
773
0
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
774
0
    || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
775
0
    h->forced_local = 1;
776
777
0
  if ((h->def_dynamic
778
0
       || h->ref_dynamic
779
0
       || bfd_link_dll (info))
780
0
      && !h->forced_local
781
0
      && h->dynindx == -1)
782
0
    {
783
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
784
0
  return false;
785
786
      /* If this is a weak defined symbol, and we know a corresponding
787
   real symbol from the same dynamic object, make sure the real
788
   symbol is also made into a dynamic symbol.  */
789
0
      if (h->is_weakalias)
790
0
  {
791
0
    struct elf_link_hash_entry *def = weakdef (h);
792
793
0
    if (def->dynindx == -1
794
0
        && !bfd_elf_link_record_dynamic_symbol (info, def))
795
0
      return false;
796
0
  }
797
0
    }
798
799
0
  return true;
800
0
}
801
802
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
803
   success, and 2 on a failure caused by attempting to record a symbol
804
   in a discarded section, eg. a discarded link-once section symbol.  */
805
806
int
807
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
808
            bfd *input_bfd,
809
            long input_indx)
810
0
{
811
0
  size_t amt;
812
0
  struct elf_link_local_dynamic_entry *entry;
813
0
  struct elf_link_hash_table *eht;
814
0
  struct elf_strtab_hash *dynstr;
815
0
  size_t dynstr_index;
816
0
  char *name;
817
0
  Elf_External_Sym_Shndx eshndx;
818
0
  char esym[sizeof (Elf64_External_Sym)];
819
820
0
  if (! is_elf_hash_table (info->hash))
821
0
    return 0;
822
823
  /* See if the entry exists already.  */
824
0
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
825
0
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
826
0
      return 1;
827
828
0
  amt = sizeof (*entry);
829
0
  entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
830
0
  if (entry == NULL)
831
0
    return 0;
832
833
  /* Go find the symbol, so that we can find it's name.  */
834
0
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
835
0
           1, input_indx, &entry->isym, esym, &eshndx))
836
0
    {
837
0
      bfd_release (input_bfd, entry);
838
0
      return 0;
839
0
    }
840
841
0
  if (entry->isym.st_shndx != SHN_UNDEF
842
0
      && entry->isym.st_shndx < SHN_LORESERVE)
843
0
    {
844
0
      asection *s;
845
846
0
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
847
0
      if (s == NULL || bfd_is_abs_section (s->output_section))
848
0
  {
849
    /* We can still bfd_release here as nothing has done another
850
       bfd_alloc.  We can't do this later in this function.  */
851
0
    bfd_release (input_bfd, entry);
852
0
    return 2;
853
0
  }
854
0
    }
855
856
0
  name = (bfd_elf_string_from_elf_section
857
0
    (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
858
0
     entry->isym.st_name));
859
860
0
  dynstr = elf_hash_table (info)->dynstr;
861
0
  if (dynstr == NULL)
862
0
    {
863
      /* Create a strtab to hold the dynamic symbol names.  */
864
0
      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
865
0
      if (dynstr == NULL)
866
0
  return 0;
867
0
    }
868
869
0
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
870
0
  if (dynstr_index == (size_t) -1)
871
0
    return 0;
872
0
  entry->isym.st_name = dynstr_index;
873
874
0
  eht = elf_hash_table (info);
875
876
0
  entry->next = eht->dynlocal;
877
0
  eht->dynlocal = entry;
878
0
  entry->input_bfd = input_bfd;
879
0
  entry->input_indx = input_indx;
880
0
  eht->dynsymcount++;
881
882
  /* Whatever binding the symbol had before, it's now local.  */
883
0
  entry->isym.st_info
884
0
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
885
886
  /* The dynindx will be set at the end of size_dynamic_sections.  */
887
888
0
  return 1;
889
0
}
890
891
/* Return the dynindex of a local dynamic symbol.  */
892
893
long
894
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
895
            bfd *input_bfd,
896
            long input_indx)
897
0
{
898
0
  struct elf_link_local_dynamic_entry *e;
899
900
0
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
901
0
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
902
0
      return e->dynindx;
903
0
  return -1;
904
0
}
905
906
/* This function is used to renumber the dynamic symbols, if some of
907
   them are removed because they are marked as local.  This is called
908
   via elf_link_hash_traverse.  */
909
910
static bool
911
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
912
              void *data)
913
0
{
914
0
  size_t *count = (size_t *) data;
915
916
0
  if (h->forced_local)
917
0
    return true;
918
919
0
  if (h->dynindx != -1)
920
0
    h->dynindx = ++(*count);
921
922
0
  return true;
923
0
}
924
925
926
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
927
   STB_LOCAL binding.  */
928
929
static bool
930
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
931
              void *data)
932
0
{
933
0
  size_t *count = (size_t *) data;
934
935
0
  if (!h->forced_local)
936
0
    return true;
937
938
0
  if (h->dynindx != -1)
939
0
    h->dynindx = ++(*count);
940
941
0
  return true;
942
0
}
943
944
/* Return true if the dynamic symbol for a given section should be
945
   omitted when creating a shared library.  */
946
bool
947
_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
948
              struct bfd_link_info *info,
949
              asection *p)
950
0
{
951
0
  struct elf_link_hash_table *htab;
952
0
  asection *ip;
953
954
0
  switch (elf_section_data (p)->this_hdr.sh_type)
955
0
    {
956
0
    case SHT_PROGBITS:
957
0
    case SHT_NOBITS:
958
      /* If sh_type is yet undecided, assume it could be
959
   SHT_PROGBITS/SHT_NOBITS.  */
960
0
    case SHT_NULL:
961
0
      htab = elf_hash_table (info);
962
0
      if (htab->text_index_section != NULL)
963
0
  return p != htab->text_index_section && p != htab->data_index_section;
964
965
0
      return (htab->dynobj != NULL
966
0
        && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
967
0
        && ip->output_section == p);
968
969
      /* There shouldn't be section relative relocations
970
   against any other section.  */
971
0
    default:
972
0
      return true;
973
0
    }
974
0
}
975
976
bool
977
_bfd_elf_omit_section_dynsym_all
978
    (bfd *output_bfd ATTRIBUTE_UNUSED,
979
     struct bfd_link_info *info ATTRIBUTE_UNUSED,
980
     asection *p ATTRIBUTE_UNUSED)
981
0
{
982
0
  return true;
983
0
}
984
985
/* Assign dynsym indices.  In a shared library we generate a section
986
   symbol for each output section, which come first.  Next come symbols
987
   which have been forced to local binding.  Then all of the back-end
988
   allocated local dynamic syms, followed by the rest of the global
989
   symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
990
   (This prevents the early call before elf_backend_init_index_section
991
   and strip_excluded_output_sections setting dynindx for sections
992
   that are stripped.)  */
993
994
static unsigned long
995
_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
996
        struct bfd_link_info *info,
997
        unsigned long *section_sym_count)
998
0
{
999
0
  unsigned long dynsymcount = 0;
1000
0
  bool do_sec = section_sym_count != NULL;
1001
1002
0
  if (bfd_link_pic (info)
1003
0
      || elf_hash_table (info)->is_relocatable_executable)
1004
0
    {
1005
0
      const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1006
0
      asection *p;
1007
0
      for (p = output_bfd->sections; p ; p = p->next)
1008
0
  if ((p->flags & SEC_EXCLUDE) == 0
1009
0
      && (p->flags & SEC_ALLOC) != 0
1010
0
      && elf_hash_table (info)->dynamic_relocs
1011
0
      && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
1012
0
    {
1013
0
      ++dynsymcount;
1014
0
      if (do_sec)
1015
0
        elf_section_data (p)->dynindx = dynsymcount;
1016
0
    }
1017
0
  else if (do_sec)
1018
0
    elf_section_data (p)->dynindx = 0;
1019
0
    }
1020
0
  if (do_sec)
1021
0
    *section_sym_count = dynsymcount;
1022
1023
0
  elf_link_hash_traverse (elf_hash_table (info),
1024
0
        elf_link_renumber_local_hash_table_dynsyms,
1025
0
        &dynsymcount);
1026
1027
0
  if (elf_hash_table (info)->dynlocal)
1028
0
    {
1029
0
      struct elf_link_local_dynamic_entry *p;
1030
0
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1031
0
  p->dynindx = ++dynsymcount;
1032
0
    }
1033
0
  elf_hash_table (info)->local_dynsymcount = dynsymcount;
1034
1035
0
  elf_link_hash_traverse (elf_hash_table (info),
1036
0
        elf_link_renumber_hash_table_dynsyms,
1037
0
        &dynsymcount);
1038
1039
  /* There is an unused NULL entry at the head of the table which we
1040
     must account for in our count even if the table is empty since it
1041
     is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1042
     .dynamic section.  */
1043
0
  dynsymcount++;
1044
1045
0
  elf_hash_table (info)->dynsymcount = dynsymcount;
1046
0
  return dynsymcount;
1047
0
}
1048
1049
/* Merge st_other field.  */
1050
1051
static void
1052
elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1053
        unsigned int st_other, asection *sec,
1054
        bool definition, bool dynamic)
1055
0
{
1056
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1057
1058
  /* If st_other has a processor-specific meaning, specific
1059
     code might be needed here.  */
1060
0
  if (bed->elf_backend_merge_symbol_attribute)
1061
0
    (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1062
0
            dynamic);
1063
1064
0
  if (!dynamic)
1065
0
    {
1066
0
      unsigned symvis = ELF_ST_VISIBILITY (st_other);
1067
0
      unsigned hvis = ELF_ST_VISIBILITY (h->other);
1068
1069
      /* Keep the most constraining visibility.  Leave the remainder
1070
   of the st_other field to elf_backend_merge_symbol_attribute.  */
1071
0
      if (symvis - 1 < hvis - 1)
1072
0
  h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1073
0
    }
1074
0
  else if (definition
1075
0
     && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1076
0
     && (sec->flags & SEC_READONLY) == 0)
1077
0
    h->protected_def = 1;
1078
0
}
1079
1080
/* This function is called when we want to merge a new symbol with an
1081
   existing symbol.  It handles the various cases which arise when we
1082
   find a definition in a dynamic object, or when there is already a
1083
   definition in a dynamic object.  The new symbol is described by
1084
   NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
1085
   entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
1086
   if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
1087
   of an old common symbol.  We set OVERRIDE if the old symbol is
1088
   overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
1089
   the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
1090
   to change.  By OK to change, we mean that we shouldn't warn if the
1091
   type or size does change.  */
1092
1093
static bool
1094
_bfd_elf_merge_symbol (bfd *abfd,
1095
           struct bfd_link_info *info,
1096
           const char *name,
1097
           Elf_Internal_Sym *sym,
1098
           asection **psec,
1099
           bfd_vma *pvalue,
1100
           struct elf_link_hash_entry **sym_hash,
1101
           bfd **poldbfd,
1102
           bool *pold_weak,
1103
           unsigned int *pold_alignment,
1104
           bool *skip,
1105
           bfd **override,
1106
           bool *type_change_ok,
1107
           bool *size_change_ok,
1108
           bool *matched)
1109
0
{
1110
0
  asection *sec, *oldsec;
1111
0
  struct elf_link_hash_entry *h;
1112
0
  struct elf_link_hash_entry *hi;
1113
0
  struct elf_link_hash_entry *flip;
1114
0
  int bind;
1115
0
  bfd *oldbfd;
1116
0
  bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1117
0
  bool newweak, oldweak, newfunc, oldfunc;
1118
0
  const struct elf_backend_data *bed;
1119
0
  char *new_version;
1120
0
  bool default_sym = *matched;
1121
0
  struct elf_link_hash_table *htab;
1122
1123
0
  *skip = false;
1124
0
  *override = NULL;
1125
1126
0
  sec = *psec;
1127
0
  bind = ELF_ST_BIND (sym->st_info);
1128
1129
0
  if (! bfd_is_und_section (sec))
1130
0
    h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1131
0
  else
1132
0
    h = ((struct elf_link_hash_entry *)
1133
0
   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1134
0
  if (h == NULL)
1135
0
    return false;
1136
0
  *sym_hash = h;
1137
1138
0
  bed = get_elf_backend_data (abfd);
1139
1140
  /* NEW_VERSION is the symbol version of the new symbol.  */
1141
0
  if (h->versioned != unversioned)
1142
0
    {
1143
      /* Symbol version is unknown or versioned.  */
1144
0
      new_version = strrchr (name, ELF_VER_CHR);
1145
0
      if (new_version)
1146
0
  {
1147
0
    if (h->versioned == unknown)
1148
0
      {
1149
0
        if (new_version > name && new_version[-1] != ELF_VER_CHR)
1150
0
    h->versioned = versioned_hidden;
1151
0
        else
1152
0
    h->versioned = versioned;
1153
0
      }
1154
0
    new_version += 1;
1155
0
    if (new_version[0] == '\0')
1156
0
      new_version = NULL;
1157
0
  }
1158
0
      else
1159
0
  h->versioned = unversioned;
1160
0
    }
1161
0
  else
1162
0
    new_version = NULL;
1163
1164
  /* For merging, we only care about real symbols.  But we need to make
1165
     sure that indirect symbol dynamic flags are updated.  */
1166
0
  hi = h;
1167
0
  while (h->root.type == bfd_link_hash_indirect
1168
0
   || h->root.type == bfd_link_hash_warning)
1169
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1170
1171
0
  if (!*matched)
1172
0
    {
1173
0
      if (hi == h || h->root.type == bfd_link_hash_new)
1174
0
  *matched = true;
1175
0
      else
1176
0
  {
1177
    /* OLD_HIDDEN is true if the existing symbol is only visible
1178
       to the symbol with the same symbol version.  NEW_HIDDEN is
1179
       true if the new symbol is only visible to the symbol with
1180
       the same symbol version.  */
1181
0
    bool old_hidden = h->versioned == versioned_hidden;
1182
0
    bool new_hidden = hi->versioned == versioned_hidden;
1183
0
    if (!old_hidden && !new_hidden)
1184
      /* The new symbol matches the existing symbol if both
1185
         aren't hidden.  */
1186
0
      *matched = true;
1187
0
    else
1188
0
      {
1189
        /* OLD_VERSION is the symbol version of the existing
1190
     symbol. */
1191
0
        char *old_version;
1192
1193
0
        if (h->versioned >= versioned)
1194
0
    old_version = strrchr (h->root.root.string,
1195
0
               ELF_VER_CHR) + 1;
1196
0
        else
1197
0
     old_version = NULL;
1198
1199
        /* The new symbol matches the existing symbol if they
1200
     have the same symbol version.  */
1201
0
        *matched = (old_version == new_version
1202
0
        || (old_version != NULL
1203
0
            && new_version != NULL
1204
0
            && strcmp (old_version, new_version) == 0));
1205
0
      }
1206
0
  }
1207
0
    }
1208
1209
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1210
     existing symbol.  */
1211
1212
0
  oldbfd = NULL;
1213
0
  oldsec = NULL;
1214
0
  switch (h->root.type)
1215
0
    {
1216
0
    default:
1217
0
      break;
1218
1219
0
    case bfd_link_hash_undefined:
1220
0
    case bfd_link_hash_undefweak:
1221
0
      oldbfd = h->root.u.undef.abfd;
1222
0
      break;
1223
1224
0
    case bfd_link_hash_defined:
1225
0
    case bfd_link_hash_defweak:
1226
0
      oldbfd = h->root.u.def.section->owner;
1227
0
      oldsec = h->root.u.def.section;
1228
0
      break;
1229
1230
0
    case bfd_link_hash_common:
1231
0
      oldbfd = h->root.u.c.p->section->owner;
1232
0
      oldsec = h->root.u.c.p->section;
1233
0
      if (pold_alignment)
1234
0
  *pold_alignment = h->root.u.c.p->alignment_power;
1235
0
      break;
1236
0
    }
1237
0
  if (poldbfd && *poldbfd == NULL)
1238
0
    *poldbfd = oldbfd;
1239
1240
  /* Differentiate strong and weak symbols.  */
1241
0
  newweak = bind == STB_WEAK;
1242
0
  oldweak = (h->root.type == bfd_link_hash_defweak
1243
0
       || h->root.type == bfd_link_hash_undefweak);
1244
0
  if (pold_weak)
1245
0
    *pold_weak = oldweak;
1246
1247
  /* We have to check it for every instance since the first few may be
1248
     references and not all compilers emit symbol type for undefined
1249
     symbols.  */
1250
0
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1251
1252
0
  htab = elf_hash_table (info);
1253
1254
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1255
     respectively, is from a dynamic object.  */
1256
1257
0
  newdyn = (abfd->flags & DYNAMIC) != 0;
1258
1259
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1260
     syms and defined syms in dynamic libraries respectively.
1261
     ref_dynamic on the other hand can be set for a symbol defined in
1262
     a dynamic library, and def_dynamic may not be set;  When the
1263
     definition in a dynamic lib is overridden by a definition in the
1264
     executable use of the symbol in the dynamic lib becomes a
1265
     reference to the executable symbol.  */
1266
0
  if (newdyn)
1267
0
    {
1268
0
      if (bfd_is_und_section (sec))
1269
0
  {
1270
0
    if (bind != STB_WEAK)
1271
0
      {
1272
0
        h->ref_dynamic_nonweak = 1;
1273
0
        hi->ref_dynamic_nonweak = 1;
1274
0
      }
1275
0
  }
1276
0
      else
1277
0
  {
1278
    /* Update the existing symbol only if they match. */
1279
0
    if (*matched)
1280
0
      h->dynamic_def = 1;
1281
0
    hi->dynamic_def = 1;
1282
0
  }
1283
0
    }
1284
1285
  /* If we just created the symbol, mark it as being an ELF symbol.
1286
     Other than that, there is nothing to do--there is no merge issue
1287
     with a newly defined symbol--so we just return.  */
1288
1289
0
  if (h->root.type == bfd_link_hash_new)
1290
0
    {
1291
0
      h->non_elf = 0;
1292
0
      return true;
1293
0
    }
1294
1295
  /* In cases involving weak versioned symbols, we may wind up trying
1296
     to merge a symbol with itself.  Catch that here, to avoid the
1297
     confusion that results if we try to override a symbol with
1298
     itself.  The additional tests catch cases like
1299
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1300
     dynamic object, which we do want to handle here.  */
1301
0
  if (abfd == oldbfd
1302
0
      && (newweak || oldweak)
1303
0
      && ((abfd->flags & DYNAMIC) == 0
1304
0
    || !h->def_regular))
1305
0
    return true;
1306
1307
0
  olddyn = false;
1308
0
  if (oldbfd != NULL)
1309
0
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1310
0
  else if (oldsec != NULL)
1311
0
    {
1312
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1313
   indices used by MIPS ELF.  */
1314
0
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1315
0
    }
1316
1317
  /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
1318
0
  if (!htab->handling_dt_needed
1319
0
      && oldbfd != NULL
1320
0
      && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1321
0
    {
1322
0
      if (newdyn != olddyn)
1323
0
  {
1324
    /* Handle a case where plugin_notice won't be called and thus
1325
       won't set the non_ir_ref flags on the first pass over
1326
       symbols.  */
1327
0
    h->root.non_ir_ref_dynamic = true;
1328
0
    hi->root.non_ir_ref_dynamic = true;
1329
0
  }
1330
0
      else if ((oldbfd->flags & BFD_PLUGIN) != 0
1331
0
         && hi->root.type == bfd_link_hash_indirect)
1332
0
  {
1333
    /* Change indirect symbol from IR to undefined.  */
1334
0
    hi->root.type = bfd_link_hash_undefined;
1335
0
    hi->root.u.undef.abfd = oldbfd;
1336
0
  }
1337
0
    }
1338
1339
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1340
     respectively, appear to be a definition rather than reference.  */
1341
1342
0
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1343
1344
0
  olddef = (h->root.type != bfd_link_hash_undefined
1345
0
      && h->root.type != bfd_link_hash_undefweak
1346
0
      && h->root.type != bfd_link_hash_common);
1347
1348
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1349
     respectively, appear to be a function.  */
1350
1351
0
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1352
0
       && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1353
1354
0
  oldfunc = (h->type != STT_NOTYPE
1355
0
       && bed->is_function_type (h->type));
1356
1357
0
  if (!(newfunc && oldfunc)
1358
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1359
0
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1360
0
      && h->type != STT_NOTYPE
1361
0
      && (newdef || bfd_is_com_section (sec))
1362
0
      && (olddef || h->root.type == bfd_link_hash_common))
1363
0
    {
1364
      /* If creating a default indirect symbol ("foo" or "foo@") from
1365
   a dynamic versioned definition ("foo@@") skip doing so if
1366
   there is an existing regular definition with a different
1367
   type.  We don't want, for example, a "time" variable in the
1368
   executable overriding a "time" function in a shared library.  */
1369
0
      if (newdyn
1370
0
    && !olddyn)
1371
0
  {
1372
0
    *skip = true;
1373
0
    return true;
1374
0
  }
1375
1376
      /* When adding a symbol from a regular object file after we have
1377
   created indirect symbols, undo the indirection and any
1378
   dynamic state.  */
1379
0
      if (hi != h
1380
0
    && !newdyn
1381
0
    && olddyn)
1382
0
  {
1383
0
    h = hi;
1384
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
1385
0
    h->forced_local = 0;
1386
0
    h->ref_dynamic = 0;
1387
0
    h->def_dynamic = 0;
1388
0
    h->dynamic_def = 0;
1389
0
    if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1390
0
      {
1391
0
        h->root.type = bfd_link_hash_undefined;
1392
0
        h->root.u.undef.abfd = abfd;
1393
0
      }
1394
0
    else
1395
0
      {
1396
0
        h->root.type = bfd_link_hash_new;
1397
0
        h->root.u.undef.abfd = NULL;
1398
0
      }
1399
0
    return true;
1400
0
  }
1401
0
    }
1402
1403
  /* Check TLS symbols.  We don't check undefined symbols introduced
1404
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1405
     check symbols from plugins because they also have no type.  */
1406
0
  if (oldbfd != NULL
1407
0
      && (oldbfd->flags & BFD_PLUGIN) == 0
1408
0
      && (abfd->flags & BFD_PLUGIN) == 0
1409
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1410
0
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1411
0
    {
1412
0
      bfd *ntbfd, *tbfd;
1413
0
      bool ntdef, tdef;
1414
0
      asection *ntsec, *tsec;
1415
1416
0
      if (h->type == STT_TLS)
1417
0
  {
1418
0
    ntbfd = abfd;
1419
0
    ntsec = sec;
1420
0
    ntdef = newdef;
1421
0
    tbfd = oldbfd;
1422
0
    tsec = oldsec;
1423
0
    tdef = olddef;
1424
0
  }
1425
0
      else
1426
0
  {
1427
0
    ntbfd = oldbfd;
1428
0
    ntsec = oldsec;
1429
0
    ntdef = olddef;
1430
0
    tbfd = abfd;
1431
0
    tsec = sec;
1432
0
    tdef = newdef;
1433
0
  }
1434
1435
0
      if (tdef && ntdef)
1436
0
  _bfd_error_handler
1437
    /* xgettext:c-format */
1438
0
    (_("%s: TLS definition in %pB section %pA "
1439
0
       "mismatches non-TLS definition in %pB section %pA"),
1440
0
     h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1441
0
      else if (!tdef && !ntdef)
1442
0
  _bfd_error_handler
1443
    /* xgettext:c-format */
1444
0
    (_("%s: TLS reference in %pB "
1445
0
       "mismatches non-TLS reference in %pB"),
1446
0
     h->root.root.string, tbfd, ntbfd);
1447
0
      else if (tdef)
1448
0
  _bfd_error_handler
1449
    /* xgettext:c-format */
1450
0
    (_("%s: TLS definition in %pB section %pA "
1451
0
       "mismatches non-TLS reference in %pB"),
1452
0
     h->root.root.string, tbfd, tsec, ntbfd);
1453
0
      else
1454
0
  _bfd_error_handler
1455
    /* xgettext:c-format */
1456
0
    (_("%s: TLS reference in %pB "
1457
0
       "mismatches non-TLS definition in %pB section %pA"),
1458
0
     h->root.root.string, tbfd, ntbfd, ntsec);
1459
1460
0
      bfd_set_error (bfd_error_bad_value);
1461
0
      return false;
1462
0
    }
1463
1464
  /* If the old symbol has non-default visibility, we ignore the new
1465
     definition from a dynamic object.  */
1466
0
  if (newdyn
1467
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1468
0
      && !bfd_is_und_section (sec))
1469
0
    {
1470
0
      *skip = true;
1471
      /* Make sure this symbol is dynamic.  */
1472
0
      h->ref_dynamic = 1;
1473
0
      hi->ref_dynamic = 1;
1474
      /* A protected symbol has external availability. Make sure it is
1475
   recorded as dynamic.
1476
1477
   FIXME: Should we check type and size for protected symbol?  */
1478
0
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1479
0
  return bfd_elf_link_record_dynamic_symbol (info, h);
1480
0
      else
1481
0
  return true;
1482
0
    }
1483
0
  else if (!newdyn
1484
0
     && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1485
0
     && h->def_dynamic)
1486
0
    {
1487
      /* If the new symbol with non-default visibility comes from a
1488
   relocatable file and the old definition comes from a dynamic
1489
   object, we remove the old definition.  */
1490
0
      if (hi->root.type == bfd_link_hash_indirect)
1491
0
  {
1492
    /* Handle the case where the old dynamic definition is
1493
       default versioned.  We need to copy the symbol info from
1494
       the symbol with default version to the normal one if it
1495
       was referenced before.  */
1496
0
    if (h->ref_regular)
1497
0
      {
1498
0
        hi->root.type = h->root.type;
1499
0
        h->root.type = bfd_link_hash_indirect;
1500
0
        (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1501
1502
0
        h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1503
0
        if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1504
0
    {
1505
      /* If the new symbol is hidden or internal, completely undo
1506
         any dynamic link state.  */
1507
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
1508
0
      h->forced_local = 0;
1509
0
      h->ref_dynamic = 0;
1510
0
    }
1511
0
        else
1512
0
    h->ref_dynamic = 1;
1513
1514
0
        h->def_dynamic = 0;
1515
        /* FIXME: Should we check type and size for protected symbol?  */
1516
0
        h->size = 0;
1517
0
        h->type = 0;
1518
1519
0
        h = hi;
1520
0
      }
1521
0
    else
1522
0
      h = hi;
1523
0
  }
1524
1525
      /* If the old symbol was undefined before, then it will still be
1526
   on the undefs list.  If the new symbol is undefined or
1527
   common, we can't make it bfd_link_hash_new here, because new
1528
   undefined or common symbols will be added to the undefs list
1529
   by _bfd_generic_link_add_one_symbol.  Symbols may not be
1530
   added twice to the undefs list.  Also, if the new symbol is
1531
   undefweak then we don't want to lose the strong undef.  */
1532
0
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1533
0
  {
1534
0
    h->root.type = bfd_link_hash_undefined;
1535
0
    h->root.u.undef.abfd = abfd;
1536
0
  }
1537
0
      else
1538
0
  {
1539
0
    h->root.type = bfd_link_hash_new;
1540
0
    h->root.u.undef.abfd = NULL;
1541
0
  }
1542
1543
0
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1544
0
  {
1545
    /* If the new symbol is hidden or internal, completely undo
1546
       any dynamic link state.  */
1547
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
1548
0
    h->forced_local = 0;
1549
0
    h->ref_dynamic = 0;
1550
0
  }
1551
0
      else
1552
0
  h->ref_dynamic = 1;
1553
0
      h->def_dynamic = 0;
1554
      /* FIXME: Should we check type and size for protected symbol?  */
1555
0
      h->size = 0;
1556
0
      h->type = 0;
1557
0
      return true;
1558
0
    }
1559
1560
  /* If a new weak symbol definition comes from a regular file and the
1561
     old symbol comes from a dynamic library, we treat the new one as
1562
     strong.  Similarly, an old weak symbol definition from a regular
1563
     file is treated as strong when the new symbol comes from a dynamic
1564
     library.  Further, an old weak symbol from a dynamic library is
1565
     treated as strong if the new symbol is from a dynamic library.
1566
     This reflects the way glibc's ld.so works.
1567
1568
     Also allow a weak symbol to override a linker script symbol
1569
     defined by an early pass over the script.  This is done so the
1570
     linker knows the symbol is defined in an object file, for the
1571
     DEFINED script function.
1572
1573
     Do this before setting *type_change_ok or *size_change_ok so that
1574
     we warn properly when dynamic library symbols are overridden.  */
1575
1576
0
  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1577
0
    newweak = false;
1578
0
  if (olddef && newdyn)
1579
0
    oldweak = false;
1580
1581
  /* Allow changes between different types of function symbol.  */
1582
0
  if (newfunc && oldfunc)
1583
0
    *type_change_ok = true;
1584
1585
  /* It's OK to change the type if either the existing symbol or the
1586
     new symbol is weak.  A type change is also OK if the old symbol
1587
     is undefined and the new symbol is defined.  */
1588
1589
0
  if (oldweak
1590
0
      || newweak
1591
0
      || (newdef
1592
0
    && h->root.type == bfd_link_hash_undefined))
1593
0
    *type_change_ok = true;
1594
1595
  /* It's OK to change the size if either the existing symbol or the
1596
     new symbol is weak, or if the old symbol is undefined.  */
1597
1598
0
  if (*type_change_ok
1599
0
      || h->root.type == bfd_link_hash_undefined)
1600
0
    *size_change_ok = true;
1601
1602
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1603
     symbol, respectively, appears to be a common symbol in a dynamic
1604
     object.  If a symbol appears in an uninitialized section, and is
1605
     not weak, and is not a function, then it may be a common symbol
1606
     which was resolved when the dynamic object was created.  We want
1607
     to treat such symbols specially, because they raise special
1608
     considerations when setting the symbol size: if the symbol
1609
     appears as a common symbol in a regular object, and the size in
1610
     the regular object is larger, we must make sure that we use the
1611
     larger size.  This problematic case can always be avoided in C,
1612
     but it must be handled correctly when using Fortran shared
1613
     libraries.
1614
1615
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1616
     likewise for OLDDYNCOMMON and OLDDEF.
1617
1618
     Note that this test is just a heuristic, and that it is quite
1619
     possible to have an uninitialized symbol in a shared object which
1620
     is really a definition, rather than a common symbol.  This could
1621
     lead to some minor confusion when the symbol really is a common
1622
     symbol in some regular object.  However, I think it will be
1623
     harmless.  */
1624
1625
0
  if (newdyn
1626
0
      && newdef
1627
0
      && !newweak
1628
0
      && (sec->flags & SEC_ALLOC) != 0
1629
0
      && (sec->flags & SEC_LOAD) == 0
1630
0
      && sym->st_size > 0
1631
0
      && !newfunc)
1632
0
    newdyncommon = true;
1633
0
  else
1634
0
    newdyncommon = false;
1635
1636
0
  if (olddyn
1637
0
      && olddef
1638
0
      && h->root.type == bfd_link_hash_defined
1639
0
      && h->def_dynamic
1640
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1641
0
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1642
0
      && h->size > 0
1643
0
      && !oldfunc)
1644
0
    olddyncommon = true;
1645
0
  else
1646
0
    olddyncommon = false;
1647
1648
  /* We now know everything about the old and new symbols.  We ask the
1649
     backend to check if we can merge them.  */
1650
0
  if (bed->merge_symbol != NULL)
1651
0
    {
1652
0
      if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1653
0
  return false;
1654
0
      sec = *psec;
1655
0
    }
1656
1657
  /* There are multiple definitions of a normal symbol.  Skip the
1658
     default symbol as well as definition from an IR object.  */
1659
0
  if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1660
0
      && !default_sym && h->def_regular
1661
0
      && !(oldbfd != NULL
1662
0
     && (oldbfd->flags & BFD_PLUGIN) != 0
1663
0
     && (abfd->flags & BFD_PLUGIN) == 0))
1664
0
    {
1665
      /* Handle a multiple definition.  */
1666
0
      (*info->callbacks->multiple_definition) (info, &h->root,
1667
0
                 abfd, sec, *pvalue);
1668
0
      *skip = true;
1669
0
      return true;
1670
0
    }
1671
1672
  /* If both the old and the new symbols look like common symbols in a
1673
     dynamic object, set the size of the symbol to the larger of the
1674
     two.  */
1675
1676
0
  if (olddyncommon
1677
0
      && newdyncommon
1678
0
      && sym->st_size != h->size)
1679
0
    {
1680
      /* Since we think we have two common symbols, issue a multiple
1681
   common warning if desired.  Note that we only warn if the
1682
   size is different.  If the size is the same, we simply let
1683
   the old symbol override the new one as normally happens with
1684
   symbols defined in dynamic objects.  */
1685
1686
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1687
0
             bfd_link_hash_common, sym->st_size);
1688
0
      if (sym->st_size > h->size)
1689
0
  h->size = sym->st_size;
1690
1691
0
      *size_change_ok = true;
1692
0
    }
1693
1694
  /* If we are looking at a dynamic object, and we have found a
1695
     definition, we need to see if the symbol was already defined by
1696
     some other object.  If so, we want to use the existing
1697
     definition, and we do not want to report a multiple symbol
1698
     definition error; we do this by clobbering *PSEC to be
1699
     bfd_und_section_ptr.
1700
1701
     We treat a common symbol as a definition if the symbol in the
1702
     shared library is a function, since common symbols always
1703
     represent variables; this can cause confusion in principle, but
1704
     any such confusion would seem to indicate an erroneous program or
1705
     shared library.  We also permit a common symbol in a regular
1706
     object to override a weak symbol in a shared object.  */
1707
1708
0
  if (newdyn
1709
0
      && newdef
1710
0
      && (olddef
1711
0
    || (h->root.type == bfd_link_hash_common
1712
0
        && (newweak || newfunc))))
1713
0
    {
1714
0
      *override = abfd;
1715
0
      newdef = false;
1716
0
      newdyncommon = false;
1717
1718
0
      *psec = sec = bfd_und_section_ptr;
1719
0
      *size_change_ok = true;
1720
1721
      /* If we get here when the old symbol is a common symbol, then
1722
   we are explicitly letting it override a weak symbol or
1723
   function in a dynamic object, and we don't want to warn about
1724
   a type change.  If the old symbol is a defined symbol, a type
1725
   change warning may still be appropriate.  */
1726
1727
0
      if (h->root.type == bfd_link_hash_common)
1728
0
  *type_change_ok = true;
1729
0
    }
1730
1731
  /* Handle the special case of an old common symbol merging with a
1732
     new symbol which looks like a common symbol in a shared object.
1733
     We change *PSEC and *PVALUE to make the new symbol look like a
1734
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1735
     right thing.  */
1736
1737
0
  if (newdyncommon
1738
0
      && h->root.type == bfd_link_hash_common)
1739
0
    {
1740
0
      *override = oldbfd;
1741
0
      newdef = false;
1742
0
      newdyncommon = false;
1743
0
      *pvalue = sym->st_size;
1744
0
      *psec = sec = bed->common_section (oldsec);
1745
0
      *size_change_ok = true;
1746
0
    }
1747
1748
  /* Skip weak definitions of symbols that are already defined.  */
1749
0
  if (newdef && olddef && newweak)
1750
0
    {
1751
      /* Don't skip new non-IR weak syms.  */
1752
0
      if (!(oldbfd != NULL
1753
0
      && (oldbfd->flags & BFD_PLUGIN) != 0
1754
0
      && (abfd->flags & BFD_PLUGIN) == 0))
1755
0
  {
1756
0
    newdef = false;
1757
0
    *skip = true;
1758
0
  }
1759
1760
      /* Merge st_other.  If the symbol already has a dynamic index,
1761
   but visibility says it should not be visible, turn it into a
1762
   local symbol.  */
1763
0
      elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1764
0
      if (h->dynindx != -1)
1765
0
  switch (ELF_ST_VISIBILITY (h->other))
1766
0
    {
1767
0
    case STV_INTERNAL:
1768
0
    case STV_HIDDEN:
1769
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
1770
0
      break;
1771
0
    }
1772
0
    }
1773
1774
  /* If the old symbol is from a dynamic object, and the new symbol is
1775
     a definition which is not from a dynamic object, then the new
1776
     symbol overrides the old symbol.  Symbols from regular files
1777
     always take precedence over symbols from dynamic objects, even if
1778
     they are defined after the dynamic object in the link.
1779
1780
     As above, we again permit a common symbol in a regular object to
1781
     override a definition in a shared object if the shared object
1782
     symbol is a function or is weak.  */
1783
1784
0
  flip = NULL;
1785
0
  if (!newdyn
1786
0
      && (newdef
1787
0
    || (bfd_is_com_section (sec)
1788
0
        && (oldweak || oldfunc)))
1789
0
      && olddyn
1790
0
      && olddef
1791
0
      && h->def_dynamic)
1792
0
    {
1793
      /* Change the hash table entry to undefined, and let
1794
   _bfd_generic_link_add_one_symbol do the right thing with the
1795
   new definition.  */
1796
1797
0
      h->root.type = bfd_link_hash_undefined;
1798
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1799
0
      *size_change_ok = true;
1800
1801
0
      olddef = false;
1802
0
      olddyncommon = false;
1803
1804
      /* We again permit a type change when a common symbol may be
1805
   overriding a function.  */
1806
1807
0
      if (bfd_is_com_section (sec))
1808
0
  {
1809
0
    if (oldfunc)
1810
0
      {
1811
        /* If a common symbol overrides a function, make sure
1812
     that it isn't defined dynamically nor has type
1813
     function.  */
1814
0
        h->def_dynamic = 0;
1815
0
        h->type = STT_NOTYPE;
1816
0
      }
1817
0
    *type_change_ok = true;
1818
0
  }
1819
1820
0
      if (hi->root.type == bfd_link_hash_indirect)
1821
0
  flip = hi;
1822
0
      else
1823
  /* This union may have been set to be non-NULL when this symbol
1824
     was seen in a dynamic object.  We must force the union to be
1825
     NULL, so that it is correct for a regular symbol.  */
1826
0
  h->verinfo.vertree = NULL;
1827
0
    }
1828
1829
  /* Handle the special case of a new common symbol merging with an
1830
     old symbol that looks like it might be a common symbol defined in
1831
     a shared object.  Note that we have already handled the case in
1832
     which a new common symbol should simply override the definition
1833
     in the shared library.  */
1834
1835
0
  if (! newdyn
1836
0
      && bfd_is_com_section (sec)
1837
0
      && olddyncommon)
1838
0
    {
1839
      /* It would be best if we could set the hash table entry to a
1840
   common symbol, but we don't know what to use for the section
1841
   or the alignment.  */
1842
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1843
0
             bfd_link_hash_common, sym->st_size);
1844
1845
      /* If the presumed common symbol in the dynamic object is
1846
   larger, pretend that the new symbol has its size.  */
1847
1848
0
      if (h->size > *pvalue)
1849
0
  *pvalue = h->size;
1850
1851
      /* We need to remember the alignment required by the symbol
1852
   in the dynamic object.  */
1853
0
      BFD_ASSERT (pold_alignment);
1854
0
      *pold_alignment = h->root.u.def.section->alignment_power;
1855
1856
0
      olddef = false;
1857
0
      olddyncommon = false;
1858
1859
0
      h->root.type = bfd_link_hash_undefined;
1860
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1861
1862
0
      *size_change_ok = true;
1863
0
      *type_change_ok = true;
1864
1865
0
      if (hi->root.type == bfd_link_hash_indirect)
1866
0
  flip = hi;
1867
0
      else
1868
0
  h->verinfo.vertree = NULL;
1869
0
    }
1870
1871
0
  if (flip != NULL)
1872
0
    {
1873
      /* Handle the case where we had a versioned symbol in a dynamic
1874
   library and now find a definition in a normal object.  In this
1875
   case, we make the versioned symbol point to the normal one.  */
1876
0
      flip->root.type = h->root.type;
1877
0
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1878
0
      h->root.type = bfd_link_hash_indirect;
1879
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1880
0
      (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1881
0
      if (h->def_dynamic)
1882
0
  {
1883
0
    h->def_dynamic = 0;
1884
0
    flip->ref_dynamic = 1;
1885
0
  }
1886
0
    }
1887
1888
0
  return true;
1889
0
}
1890
1891
/* This function is called to create an indirect symbol from the
1892
   default for the symbol with the default version if needed. The
1893
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1894
   set DYNSYM if the new indirect symbol is dynamic.  */
1895
1896
static bool
1897
_bfd_elf_add_default_symbol (bfd *abfd,
1898
           struct bfd_link_info *info,
1899
           struct elf_link_hash_entry *h,
1900
           const char *name,
1901
           Elf_Internal_Sym *sym,
1902
           asection *sec,
1903
           bfd_vma value,
1904
           bfd **poldbfd,
1905
           bool *dynsym)
1906
0
{
1907
0
  bool type_change_ok;
1908
0
  bool size_change_ok;
1909
0
  bool skip;
1910
0
  char *shortname;
1911
0
  struct elf_link_hash_entry *hi;
1912
0
  struct bfd_link_hash_entry *bh;
1913
0
  const struct elf_backend_data *bed;
1914
0
  bool collect;
1915
0
  bool dynamic;
1916
0
  bfd *override;
1917
0
  char *p;
1918
0
  size_t len, shortlen;
1919
0
  asection *tmp_sec;
1920
0
  bool matched;
1921
1922
0
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1923
0
    return true;
1924
1925
  /* If this symbol has a version, and it is the default version, we
1926
     create an indirect symbol from the default name to the fully
1927
     decorated name.  This will cause external references which do not
1928
     specify a version to be bound to this version of the symbol.  */
1929
0
  p = strchr (name, ELF_VER_CHR);
1930
0
  if (h->versioned == unknown)
1931
0
    {
1932
0
      if (p == NULL)
1933
0
  {
1934
0
    h->versioned = unversioned;
1935
0
    return true;
1936
0
  }
1937
0
      else
1938
0
  {
1939
0
    if (p[1] != ELF_VER_CHR)
1940
0
      {
1941
0
        h->versioned = versioned_hidden;
1942
0
        return true;
1943
0
      }
1944
0
    else
1945
0
      h->versioned = versioned;
1946
0
  }
1947
0
    }
1948
0
  else
1949
0
    {
1950
      /* PR ld/19073: We may see an unversioned definition after the
1951
   default version.  */
1952
0
      if (p == NULL)
1953
0
  return true;
1954
0
    }
1955
1956
0
  bed = get_elf_backend_data (abfd);
1957
0
  collect = bed->collect;
1958
0
  dynamic = (abfd->flags & DYNAMIC) != 0;
1959
1960
0
  shortlen = p - name;
1961
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1962
0
  if (shortname == NULL)
1963
0
    return false;
1964
0
  memcpy (shortname, name, shortlen);
1965
0
  shortname[shortlen] = '\0';
1966
1967
  /* We are going to create a new symbol.  Merge it with any existing
1968
     symbol with this name.  For the purposes of the merge, act as
1969
     though we were defining the symbol we just defined, although we
1970
     actually going to define an indirect symbol.  */
1971
0
  type_change_ok = false;
1972
0
  size_change_ok = false;
1973
0
  matched = true;
1974
0
  tmp_sec = sec;
1975
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1976
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
1977
0
            &type_change_ok, &size_change_ok, &matched))
1978
0
    return false;
1979
1980
0
  if (skip)
1981
0
    goto nondefault;
1982
1983
0
  if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1984
0
    {
1985
      /* If the undecorated symbol will have a version added by a
1986
   script different to H, then don't indirect to/from the
1987
   undecorated symbol.  This isn't ideal because we may not yet
1988
   have seen symbol versions, if given by a script on the
1989
   command line rather than via --version-script.  */
1990
0
      if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1991
0
  {
1992
0
    bool hide;
1993
1994
0
    hi->verinfo.vertree
1995
0
      = bfd_find_version_for_sym (info->version_info,
1996
0
          hi->root.root.string, &hide);
1997
0
    if (hi->verinfo.vertree != NULL && hide)
1998
0
      {
1999
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
2000
0
        goto nondefault;
2001
0
      }
2002
0
  }
2003
0
      if (hi->verinfo.vertree != NULL
2004
0
    && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2005
0
  goto nondefault;
2006
0
    }
2007
2008
0
  if (! override)
2009
0
    {
2010
      /* Add the default symbol if not performing a relocatable link.  */
2011
0
      if (! bfd_link_relocatable (info))
2012
0
  {
2013
0
    bh = &hi->root;
2014
0
    if (bh->type == bfd_link_hash_defined
2015
0
        && bh->u.def.section->owner != NULL
2016
0
        && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2017
0
      {
2018
        /* Mark the previous definition from IR object as
2019
     undefined so that the generic linker will override
2020
     it.  */
2021
0
        bh->type = bfd_link_hash_undefined;
2022
0
        bh->u.undef.abfd = bh->u.def.section->owner;
2023
0
      }
2024
0
    if (! (_bfd_generic_link_add_one_symbol
2025
0
     (info, abfd, shortname, BSF_INDIRECT,
2026
0
      bfd_ind_section_ptr,
2027
0
      0, name, false, collect, &bh)))
2028
0
      return false;
2029
0
    hi = (struct elf_link_hash_entry *) bh;
2030
0
  }
2031
0
    }
2032
0
  else
2033
0
    {
2034
      /* In this case the symbol named SHORTNAME is overriding the
2035
   indirect symbol we want to add.  We were planning on making
2036
   SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
2037
   is the name without a version.  NAME is the fully versioned
2038
   name, and it is the default version.
2039
2040
   Overriding means that we already saw a definition for the
2041
   symbol SHORTNAME in a regular object, and it is overriding
2042
   the symbol defined in the dynamic object.
2043
2044
   When this happens, we actually want to change NAME, the
2045
   symbol we just added, to refer to SHORTNAME.  This will cause
2046
   references to NAME in the shared object to become references
2047
   to SHORTNAME in the regular object.  This is what we expect
2048
   when we override a function in a shared object: that the
2049
   references in the shared object will be mapped to the
2050
   definition in the regular object.  */
2051
2052
0
      while (hi->root.type == bfd_link_hash_indirect
2053
0
       || hi->root.type == bfd_link_hash_warning)
2054
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2055
2056
0
      h->root.type = bfd_link_hash_indirect;
2057
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2058
0
      if (h->def_dynamic)
2059
0
  {
2060
0
    h->def_dynamic = 0;
2061
0
    hi->ref_dynamic = 1;
2062
0
    if (hi->ref_regular
2063
0
        || hi->def_regular)
2064
0
      {
2065
0
        if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2066
0
    return false;
2067
0
      }
2068
0
  }
2069
2070
      /* Now set HI to H, so that the following code will set the
2071
   other fields correctly.  */
2072
0
      hi = h;
2073
0
    }
2074
2075
  /* Check if HI is a warning symbol.  */
2076
0
  if (hi->root.type == bfd_link_hash_warning)
2077
0
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2078
2079
  /* If there is a duplicate definition somewhere, then HI may not
2080
     point to an indirect symbol.  We will have reported an error to
2081
     the user in that case.  */
2082
2083
0
  if (hi->root.type == bfd_link_hash_indirect)
2084
0
    {
2085
0
      struct elf_link_hash_entry *ht;
2086
2087
0
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2088
0
      (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2089
2090
      /* If we first saw a reference to SHORTNAME with non-default
2091
   visibility, merge that visibility to the @@VER symbol.  */
2092
0
      elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2093
2094
      /* A reference to the SHORTNAME symbol from a dynamic library
2095
   will be satisfied by the versioned symbol at runtime.  In
2096
   effect, we have a reference to the versioned symbol.  */
2097
0
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2098
0
      hi->dynamic_def |= ht->dynamic_def;
2099
2100
      /* See if the new flags lead us to realize that the symbol must
2101
   be dynamic.  */
2102
0
      if (! *dynsym)
2103
0
  {
2104
0
    if (! dynamic)
2105
0
      {
2106
0
        if (! bfd_link_executable (info)
2107
0
      || hi->def_dynamic
2108
0
      || hi->ref_dynamic)
2109
0
    *dynsym = true;
2110
0
      }
2111
0
    else
2112
0
      {
2113
0
        if (hi->ref_regular)
2114
0
    *dynsym = true;
2115
0
      }
2116
0
  }
2117
0
    }
2118
2119
  /* We also need to define an indirection from the nondefault version
2120
     of the symbol.  */
2121
2122
0
 nondefault:
2123
0
  len = strlen (name);
2124
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2125
0
  if (shortname == NULL)
2126
0
    return false;
2127
0
  memcpy (shortname, name, shortlen);
2128
0
  memcpy (shortname + shortlen, p + 1, len - shortlen);
2129
2130
  /* Once again, merge with any existing symbol.  */
2131
0
  type_change_ok = false;
2132
0
  size_change_ok = false;
2133
0
  tmp_sec = sec;
2134
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2135
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2136
0
            &type_change_ok, &size_change_ok, &matched))
2137
0
    return false;
2138
2139
0
  if (skip)
2140
0
    {
2141
0
      if (!dynamic
2142
0
    && h->root.type == bfd_link_hash_defweak
2143
0
    && hi->root.type == bfd_link_hash_defined)
2144
0
  {
2145
    /* We are handling a weak sym@@ver and attempting to define
2146
       a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2147
       new weak sym@ver because there is already a strong sym@ver.
2148
       However, sym@ver and sym@@ver are really the same symbol.
2149
       The existing strong sym@ver ought to override sym@@ver.  */
2150
0
    h->root.type = bfd_link_hash_defined;
2151
0
    h->root.u.def.section = hi->root.u.def.section;
2152
0
    h->root.u.def.value = hi->root.u.def.value;
2153
0
    hi->root.type = bfd_link_hash_indirect;
2154
0
    hi->root.u.i.link = &h->root;
2155
0
  }
2156
0
      else
2157
0
  return true;
2158
0
    }
2159
0
  else if (override)
2160
0
    {
2161
      /* Here SHORTNAME is a versioned name, so we don't expect to see
2162
   the type of override we do in the case above unless it is
2163
   overridden by a versioned definition.  */
2164
0
      if (hi->root.type != bfd_link_hash_defined
2165
0
    && hi->root.type != bfd_link_hash_defweak)
2166
0
  _bfd_error_handler
2167
    /* xgettext:c-format */
2168
0
    (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2169
0
     abfd, shortname);
2170
0
      return true;
2171
0
    }
2172
0
  else
2173
0
    {
2174
0
      bh = &hi->root;
2175
0
      if (! (_bfd_generic_link_add_one_symbol
2176
0
       (info, abfd, shortname, BSF_INDIRECT,
2177
0
        bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2178
0
  return false;
2179
0
      hi = (struct elf_link_hash_entry *) bh;
2180
0
    }
2181
2182
  /* If there is a duplicate definition somewhere, then HI may not
2183
     point to an indirect symbol.  We will have reported an error
2184
     to the user in that case.  */
2185
0
  if (hi->root.type == bfd_link_hash_indirect)
2186
0
    {
2187
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2188
0
      h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2189
0
      hi->dynamic_def |= h->dynamic_def;
2190
2191
      /* If we first saw a reference to @VER symbol with
2192
   non-default visibility, merge that visibility to the
2193
   @@VER symbol.  */
2194
0
      elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2195
2196
      /* See if the new flags lead us to realize that the symbol
2197
   must be dynamic.  */
2198
0
      if (! *dynsym)
2199
0
  {
2200
0
    if (! dynamic)
2201
0
      {
2202
0
        if (! bfd_link_executable (info)
2203
0
      || hi->ref_dynamic)
2204
0
    *dynsym = true;
2205
0
      }
2206
0
    else
2207
0
      {
2208
0
        if (hi->ref_regular)
2209
0
    *dynsym = true;
2210
0
      }
2211
0
  }
2212
0
    }
2213
2214
0
  return true;
2215
0
}
2216

2217
/* This routine is used to export all defined symbols into the dynamic
2218
   symbol table.  It is called via elf_link_hash_traverse.  */
2219
2220
static bool
2221
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2222
0
{
2223
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
2224
2225
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2226
0
  if (h->root.type == bfd_link_hash_indirect)
2227
0
    return true;
2228
2229
  /* Ignore this if we won't export it.  */
2230
0
  if (!eif->info->export_dynamic && !h->dynamic)
2231
0
    return true;
2232
2233
0
  if (h->dynindx == -1
2234
0
      && (h->def_regular || h->ref_regular)
2235
0
      && ! bfd_hide_sym_by_version (eif->info->version_info,
2236
0
            h->root.root.string))
2237
0
    {
2238
0
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2239
0
  {
2240
0
    eif->failed = true;
2241
0
    return false;
2242
0
  }
2243
0
    }
2244
2245
0
  return true;
2246
0
}
2247

2248
/* Return the glibc version reference if VERSION_DEP is added to the
2249
   list of glibc version dependencies successfully.  VERSION_DEP will
2250
   be put into the .gnu.version_r section.  */
2251
2252
static Elf_Internal_Verneed *
2253
elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2254
          Elf_Internal_Verneed *glibc_verref,
2255
          const char *version_dep)
2256
0
{
2257
0
  Elf_Internal_Verneed *t;
2258
0
  Elf_Internal_Vernaux *a;
2259
0
  size_t amt;
2260
2261
0
  if (glibc_verref != NULL)
2262
0
    {
2263
0
      t = glibc_verref;
2264
2265
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2266
0
  {
2267
    /* Return if VERSION_DEP dependency has been added.  */
2268
0
    if (a->vna_nodename == version_dep
2269
0
        || strcmp (a->vna_nodename, version_dep) == 0)
2270
0
      return t;
2271
0
  }
2272
0
    }
2273
0
  else
2274
0
    {
2275
0
      bool is_glibc;
2276
2277
0
      for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2278
0
     t != NULL;
2279
0
     t = t->vn_nextref)
2280
0
  {
2281
0
    const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2282
0
    if (soname != NULL && startswith (soname, "libc.so."))
2283
0
      break;
2284
0
  }
2285
2286
      /* Skip the shared library if it isn't libc.so.  */
2287
0
      if (t == NULL)
2288
0
  return t;
2289
2290
0
      is_glibc = false;
2291
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2292
0
  {
2293
    /* Return if VERSION_DEP dependency has been added.  */
2294
0
    if (a->vna_nodename == version_dep
2295
0
        || strcmp (a->vna_nodename, version_dep) == 0)
2296
0
      return t;
2297
2298
    /* Check if libc.so provides GLIBC_2.XX version.  */
2299
0
    if (!is_glibc && startswith (a->vna_nodename, "GLIBC_2."))
2300
0
      is_glibc = true;
2301
0
  }
2302
2303
      /* Skip if it isn't linked against glibc.  */
2304
0
      if (!is_glibc)
2305
0
  return NULL;
2306
0
    }
2307
2308
0
  amt = sizeof *a;
2309
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2310
0
  if (a == NULL)
2311
0
    {
2312
0
      rinfo->failed = true;
2313
0
      return NULL;
2314
0
    }
2315
2316
0
  a->vna_nodename = version_dep;
2317
0
  a->vna_flags = 0;
2318
0
  a->vna_nextptr = t->vn_auxptr;
2319
0
  a->vna_other = rinfo->vers + 1;
2320
0
  ++rinfo->vers;
2321
2322
0
  t->vn_auxptr = a;
2323
2324
0
  return t;
2325
0
}
2326
2327
/* Add VERSION_DEP to the list of version dependencies when linked
2328
   against glibc.  */
2329
2330
void
2331
_bfd_elf_link_add_glibc_version_dependency
2332
  (struct elf_find_verdep_info *rinfo,
2333
   const char *version_dep[])
2334
0
{
2335
0
  Elf_Internal_Verneed *t = NULL;
2336
2337
0
  do
2338
0
    {
2339
0
      t = elf_link_add_glibc_verneed (rinfo, t, *version_dep);
2340
      /* Return if there is no glibc version reference.  */
2341
0
      if (t == NULL)
2342
0
  return;
2343
0
      version_dep++;
2344
0
    }
2345
0
  while (*version_dep != NULL);
2346
0
}
2347
2348
/* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2349
   linked against glibc.  */
2350
2351
void
2352
_bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2353
0
{
2354
0
  if (rinfo->info->enable_dt_relr)
2355
0
    {
2356
0
      const char *version[] =
2357
0
  {
2358
0
    "GLIBC_ABI_DT_RELR",
2359
0
    NULL
2360
0
  };
2361
0
      _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2362
0
    }
2363
0
}
2364
2365
/* Look through the symbols which are defined in other shared
2366
   libraries and referenced here.  Update the list of version
2367
   dependencies.  This will be put into the .gnu.version_r section.
2368
   This function is called via elf_link_hash_traverse.  */
2369
2370
static bool
2371
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2372
           void *data)
2373
0
{
2374
0
  struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2375
0
  Elf_Internal_Verneed *t;
2376
0
  Elf_Internal_Vernaux *a;
2377
0
  size_t amt;
2378
2379
  /* We only care about symbols defined in shared objects with version
2380
     information.  */
2381
0
  if (!h->def_dynamic
2382
0
      || h->def_regular
2383
0
      || h->dynindx == -1
2384
0
      || h->verinfo.verdef == NULL
2385
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2386
0
    & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2387
0
    return true;
2388
2389
  /* See if we already know about this version.  */
2390
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2391
0
       t != NULL;
2392
0
       t = t->vn_nextref)
2393
0
    {
2394
0
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2395
0
  continue;
2396
2397
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2398
0
  if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2399
0
    return true;
2400
2401
0
      break;
2402
0
    }
2403
2404
  /* This is a new version.  Add it to tree we are building.  */
2405
2406
0
  if (t == NULL)
2407
0
    {
2408
0
      amt = sizeof *t;
2409
0
      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2410
0
      if (t == NULL)
2411
0
  {
2412
0
    rinfo->failed = true;
2413
0
    return false;
2414
0
  }
2415
2416
0
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2417
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2418
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2419
0
    }
2420
2421
0
  amt = sizeof *a;
2422
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2423
0
  if (a == NULL)
2424
0
    {
2425
0
      rinfo->failed = true;
2426
0
      return false;
2427
0
    }
2428
2429
  /* Note that we are copying a string pointer here, and testing it
2430
     above.  If bfd_elf_string_from_elf_section is ever changed to
2431
     discard the string data when low in memory, this will have to be
2432
     fixed.  */
2433
0
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2434
2435
0
  a->vna_flags = h->verinfo.verdef->vd_flags;
2436
0
  a->vna_nextptr = t->vn_auxptr;
2437
2438
0
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2439
0
  ++rinfo->vers;
2440
2441
0
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2442
2443
0
  t->vn_auxptr = a;
2444
2445
0
  return true;
2446
0
}
2447
2448
/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2449
   hidden.  Set *T_P to NULL if there is no match.  */
2450
2451
static bool
2452
_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2453
             struct elf_link_hash_entry *h,
2454
             const char *version_p,
2455
             struct bfd_elf_version_tree **t_p,
2456
             bool *hide)
2457
0
{
2458
0
  struct bfd_elf_version_tree *t;
2459
2460
  /* Look for the version.  If we find it, it is no longer weak.  */
2461
0
  for (t = info->version_info; t != NULL; t = t->next)
2462
0
    {
2463
0
      if (strcmp (t->name, version_p) == 0)
2464
0
  {
2465
0
    size_t len;
2466
0
    char *alc;
2467
0
    struct bfd_elf_version_expr *d;
2468
2469
0
    len = version_p - h->root.root.string;
2470
0
    alc = (char *) bfd_malloc (len);
2471
0
    if (alc == NULL)
2472
0
      return false;
2473
0
    memcpy (alc, h->root.root.string, len - 1);
2474
0
    alc[len - 1] = '\0';
2475
0
    if (alc[len - 2] == ELF_VER_CHR)
2476
0
      alc[len - 2] = '\0';
2477
2478
0
    h->verinfo.vertree = t;
2479
0
    t->used = true;
2480
0
    d = NULL;
2481
2482
0
    if (t->globals.list != NULL)
2483
0
      d = (*t->match) (&t->globals, NULL, alc);
2484
2485
    /* See if there is anything to force this symbol to
2486
       local scope.  */
2487
0
    if (d == NULL && t->locals.list != NULL)
2488
0
      {
2489
0
        d = (*t->match) (&t->locals, NULL, alc);
2490
0
        if (d != NULL
2491
0
      && h->dynindx != -1
2492
0
      && ! info->export_dynamic)
2493
0
    *hide = true;
2494
0
      }
2495
2496
0
    free (alc);
2497
0
    break;
2498
0
  }
2499
0
    }
2500
2501
0
  *t_p = t;
2502
2503
0
  return true;
2504
0
}
2505
2506
/* Return TRUE if the symbol H is hidden by version script.  */
2507
2508
bool
2509
_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2510
           struct elf_link_hash_entry *h)
2511
0
{
2512
0
  const char *p;
2513
0
  bool hide = false;
2514
0
  const struct elf_backend_data *bed
2515
0
    = get_elf_backend_data (info->output_bfd);
2516
2517
  /* Version script only hides symbols defined in regular objects.  */
2518
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2519
0
    return true;
2520
2521
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2522
0
  if (p != NULL && h->verinfo.vertree == NULL)
2523
0
    {
2524
0
      struct bfd_elf_version_tree *t;
2525
2526
0
      ++p;
2527
0
      if (*p == ELF_VER_CHR)
2528
0
  ++p;
2529
2530
0
      if (*p != '\0'
2531
0
    && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2532
0
    && hide)
2533
0
  {
2534
0
    if (hide)
2535
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
2536
0
    return true;
2537
0
  }
2538
0
    }
2539
2540
  /* If we don't have a version for this symbol, see if we can find
2541
     something.  */
2542
0
  if (h->verinfo.vertree == NULL && info->version_info != NULL)
2543
0
    {
2544
0
      h->verinfo.vertree
2545
0
  = bfd_find_version_for_sym (info->version_info,
2546
0
            h->root.root.string, &hide);
2547
0
      if (h->verinfo.vertree != NULL && hide)
2548
0
  {
2549
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
2550
0
    return true;
2551
0
  }
2552
0
    }
2553
2554
0
  return false;
2555
0
}
2556
2557
/* Figure out appropriate versions for all the symbols.  We may not
2558
   have the version number script until we have read all of the input
2559
   files, so until that point we don't know which symbols should be
2560
   local.  This function is called via elf_link_hash_traverse.  */
2561
2562
static bool
2563
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2564
0
{
2565
0
  struct elf_info_failed *sinfo;
2566
0
  struct bfd_link_info *info;
2567
0
  const struct elf_backend_data *bed;
2568
0
  struct elf_info_failed eif;
2569
0
  char *p;
2570
0
  bool hide;
2571
2572
0
  sinfo = (struct elf_info_failed *) data;
2573
0
  info = sinfo->info;
2574
2575
  /* Fix the symbol flags.  */
2576
0
  eif.failed = false;
2577
0
  eif.info = info;
2578
0
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2579
0
    {
2580
0
      if (eif.failed)
2581
0
  sinfo->failed = true;
2582
0
      return false;
2583
0
    }
2584
2585
0
  bed = get_elf_backend_data (info->output_bfd);
2586
2587
  /* We only need version numbers for symbols defined in regular
2588
     objects.  */
2589
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2590
0
    {
2591
      /* Hide symbols defined in discarded input sections.  */
2592
0
      if ((h->root.type == bfd_link_hash_defined
2593
0
     || h->root.type == bfd_link_hash_defweak)
2594
0
    && discarded_section (h->root.u.def.section))
2595
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2596
0
      return true;
2597
0
    }
2598
2599
0
  hide = false;
2600
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2601
0
  if (p != NULL && h->verinfo.vertree == NULL)
2602
0
    {
2603
0
      struct bfd_elf_version_tree *t;
2604
2605
0
      ++p;
2606
0
      if (*p == ELF_VER_CHR)
2607
0
  ++p;
2608
2609
      /* If there is no version string, we can just return out.  */
2610
0
      if (*p == '\0')
2611
0
  return true;
2612
2613
0
      if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2614
0
  {
2615
0
    sinfo->failed = true;
2616
0
    return false;
2617
0
  }
2618
2619
0
      if (hide)
2620
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2621
2622
      /* If we are building an application, we need to create a
2623
   version node for this version.  */
2624
0
      if (t == NULL && bfd_link_executable (info))
2625
0
  {
2626
0
    struct bfd_elf_version_tree **pp;
2627
0
    int version_index;
2628
2629
    /* If we aren't going to export this symbol, we don't need
2630
       to worry about it.  */
2631
0
    if (h->dynindx == -1)
2632
0
      return true;
2633
2634
0
    t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2635
0
                sizeof *t);
2636
0
    if (t == NULL)
2637
0
      {
2638
0
        sinfo->failed = true;
2639
0
        return false;
2640
0
      }
2641
2642
0
    t->name = p;
2643
0
    t->name_indx = (unsigned int) -1;
2644
0
    t->used = true;
2645
2646
0
    version_index = 1;
2647
    /* Don't count anonymous version tag.  */
2648
0
    if (sinfo->info->version_info != NULL
2649
0
        && sinfo->info->version_info->vernum == 0)
2650
0
      version_index = 0;
2651
0
    for (pp = &sinfo->info->version_info;
2652
0
         *pp != NULL;
2653
0
         pp = &(*pp)->next)
2654
0
      ++version_index;
2655
0
    t->vernum = version_index;
2656
2657
0
    *pp = t;
2658
2659
0
    h->verinfo.vertree = t;
2660
0
  }
2661
0
      else if (t == NULL)
2662
0
  {
2663
    /* We could not find the version for a symbol when
2664
       generating a shared archive.  Return an error.  */
2665
0
    _bfd_error_handler
2666
      /* xgettext:c-format */
2667
0
      (_("%pB: version node not found for symbol %s"),
2668
0
       info->output_bfd, h->root.root.string);
2669
0
    bfd_set_error (bfd_error_bad_value);
2670
0
    sinfo->failed = true;
2671
0
    return false;
2672
0
  }
2673
0
    }
2674
2675
  /* If we don't have a version for this symbol, see if we can find
2676
     something.  */
2677
0
  if (!hide
2678
0
      && h->verinfo.vertree == NULL
2679
0
      && sinfo->info->version_info != NULL)
2680
0
    {
2681
0
      h->verinfo.vertree
2682
0
  = bfd_find_version_for_sym (sinfo->info->version_info,
2683
0
            h->root.root.string, &hide);
2684
0
      if (h->verinfo.vertree != NULL && hide)
2685
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2686
0
    }
2687
2688
0
  return true;
2689
0
}
2690

2691
/* Read and swap the relocs from the section indicated by SHDR.  This
2692
   may be either a REL or a RELA section.  The relocations are
2693
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2694
   which should have already been allocated to contain enough space.
2695
   The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2696
   relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
2697
   *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2698
   memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
2699
   unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2700
2701
   Returns FALSE if something goes wrong.  */
2702
2703
static bool
2704
elf_link_read_relocs_from_section (bfd *abfd,
2705
           const asection *sec,
2706
           Elf_Internal_Shdr *shdr,
2707
           void **external_relocs_addr,
2708
           size_t *external_relocs_size,
2709
           Elf_Internal_Rela *internal_relocs)
2710
0
{
2711
0
  const struct elf_backend_data *bed;
2712
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2713
0
  const bfd_byte *erela;
2714
0
  const bfd_byte *erelaend;
2715
0
  Elf_Internal_Rela *irela;
2716
0
  Elf_Internal_Shdr *symtab_hdr;
2717
0
  size_t nsyms;
2718
0
  void *external_relocs = *external_relocs_addr;
2719
2720
  /* Position ourselves at the start of the section.  */
2721
0
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2722
0
    return false;
2723
2724
  /* Read the relocations.  */
2725
0
  *external_relocs_size = shdr->sh_size;
2726
0
  if (!_bfd_mmap_read_temporary (&external_relocs,
2727
0
         external_relocs_size,
2728
0
         external_relocs_addr, abfd, true))
2729
0
    return false;
2730
2731
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2732
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2733
2734
0
  bed = get_elf_backend_data (abfd);
2735
2736
  /* Convert the external relocations to the internal format.  */
2737
0
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2738
0
    swap_in = bed->s->swap_reloc_in;
2739
0
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2740
0
    swap_in = bed->s->swap_reloca_in;
2741
0
  else
2742
0
    {
2743
0
      bfd_set_error (bfd_error_wrong_format);
2744
0
      return false;
2745
0
    }
2746
2747
0
  erela = (const bfd_byte *) external_relocs;
2748
  /* Setting erelaend like this and comparing with <= handles case of
2749
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2750
0
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2751
0
  irela = internal_relocs;
2752
0
  while (erela <= erelaend)
2753
0
    {
2754
0
      bfd_vma r_symndx;
2755
2756
0
      (*swap_in) (abfd, erela, irela);
2757
0
      r_symndx = ELF32_R_SYM (irela->r_info);
2758
0
      if (bed->s->arch_size == 64)
2759
0
  r_symndx >>= 24;
2760
0
      if (nsyms > 0)
2761
0
  {
2762
0
    if ((size_t) r_symndx >= nsyms)
2763
0
      {
2764
0
        _bfd_error_handler
2765
    /* xgettext:c-format */
2766
0
    (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2767
0
       " for offset %#" PRIx64 " in section `%pA'"),
2768
0
     abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2769
0
     (uint64_t) irela->r_offset, sec);
2770
0
        bfd_set_error (bfd_error_bad_value);
2771
0
        return false;
2772
0
      }
2773
0
  }
2774
0
      else if (r_symndx != STN_UNDEF)
2775
0
  {
2776
0
    _bfd_error_handler
2777
      /* xgettext:c-format */
2778
0
      (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2779
0
         " for offset %#" PRIx64 " in section `%pA'"
2780
0
         " when the object file has no symbol table"),
2781
0
       abfd, (uint64_t) r_symndx,
2782
0
       (uint64_t) irela->r_offset, sec);
2783
0
    bfd_set_error (bfd_error_bad_value);
2784
0
    return false;
2785
0
  }
2786
0
      irela += bed->s->int_rels_per_ext_rel;
2787
0
      erela += shdr->sh_entsize;
2788
0
    }
2789
2790
0
  return true;
2791
0
}
2792
2793
/* Read and swap the relocs for a section O.  They may have been
2794
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2795
   not NULL, they are used as buffers to read into.  They are known to
2796
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2797
   the return value is allocated using either malloc or bfd_alloc,
2798
   according to the KEEP_MEMORY argument.  If O has two relocation
2799
   sections (both REL and RELA relocations), then the REL_HDR
2800
   relocations will appear first in INTERNAL_RELOCS, followed by the
2801
   RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
2802
   update cache_size.  */
2803
2804
Elf_Internal_Rela *
2805
_bfd_elf_link_info_read_relocs (bfd *abfd,
2806
        struct bfd_link_info *info,
2807
        const asection *o,
2808
        void *external_relocs,
2809
        Elf_Internal_Rela *internal_relocs,
2810
        bool keep_memory)
2811
0
{
2812
0
  void *alloc1 = NULL;
2813
0
  size_t alloc1_size;
2814
0
  Elf_Internal_Rela *alloc2 = NULL;
2815
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2816
0
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2817
0
  Elf_Internal_Rela *internal_rela_relocs;
2818
2819
0
  if (esdo->relocs != NULL)
2820
0
    return esdo->relocs;
2821
2822
0
  if (o->reloc_count == 0)
2823
0
    return NULL;
2824
2825
0
  if (internal_relocs == NULL)
2826
0
    {
2827
0
      bfd_size_type size;
2828
2829
0
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2830
0
      if (keep_memory)
2831
0
  {
2832
0
    internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2833
0
    if (info)
2834
0
      info->cache_size += size;
2835
0
  }
2836
0
      else
2837
0
  internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2838
0
      if (internal_relocs == NULL)
2839
0
  return NULL;
2840
0
    }
2841
2842
0
  alloc1 = external_relocs;
2843
0
  internal_rela_relocs = internal_relocs;
2844
0
  if (esdo->rel.hdr)
2845
0
    {
2846
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2847
0
                &alloc1, &alloc1_size,
2848
0
                internal_relocs))
2849
0
  goto error_return;
2850
0
      external_relocs = (((bfd_byte *) external_relocs)
2851
0
       + esdo->rel.hdr->sh_size);
2852
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2853
0
             * bed->s->int_rels_per_ext_rel);
2854
0
    }
2855
2856
0
  if (esdo->rela.hdr
2857
0
      && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2858
0
                &alloc1, &alloc1_size,
2859
0
                internal_rela_relocs)))
2860
0
    goto error_return;
2861
2862
  /* Cache the results for next time, if we can.  */
2863
0
  if (keep_memory)
2864
0
    esdo->relocs = internal_relocs;
2865
2866
0
  _bfd_munmap_readonly_temporary (alloc1, alloc1_size);
2867
2868
  /* Don't free alloc2, since if it was allocated we are passing it
2869
     back (under the name of internal_relocs).  */
2870
2871
0
  return internal_relocs;
2872
2873
0
 error_return:
2874
0
  _bfd_munmap_readonly_temporary (alloc1, alloc1_size);
2875
0
  if (alloc2 != NULL)
2876
0
    {
2877
0
      if (keep_memory)
2878
0
  bfd_release (abfd, alloc2);
2879
0
      else
2880
0
  free (alloc2);
2881
0
    }
2882
0
  return NULL;
2883
0
}
2884
2885
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
2886
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2887
   struct bfd_link_info.  */
2888
2889
Elf_Internal_Rela *
2890
_bfd_elf_link_read_relocs (bfd *abfd,
2891
         const asection *o,
2892
         void *external_relocs,
2893
         Elf_Internal_Rela *internal_relocs,
2894
         bool keep_memory)
2895
0
{
2896
0
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2897
0
           internal_relocs, keep_memory);
2898
2899
0
}
2900
2901
/* Compute the size of, and allocate space for, REL_HDR which is the
2902
   section header for a section containing relocations for O.  */
2903
2904
static bool
2905
_bfd_elf_link_size_reloc_section (bfd *abfd,
2906
          struct bfd_elf_section_reloc_data *reldata)
2907
0
{
2908
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2909
2910
  /* That allows us to calculate the size of the section.  */
2911
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2912
2913
  /* The contents field must last into write_object_contents, so we
2914
     allocate it with bfd_alloc rather than malloc.  Also since we
2915
     cannot be sure that the contents will actually be filled in,
2916
     we zero the allocated space.  */
2917
0
  rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2918
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2919
0
    return false;
2920
2921
0
  if (reldata->hashes == NULL && reldata->count)
2922
0
    {
2923
0
      struct elf_link_hash_entry **p;
2924
2925
0
      p = ((struct elf_link_hash_entry **)
2926
0
     bfd_zmalloc (reldata->count * sizeof (*p)));
2927
0
      if (p == NULL)
2928
0
  return false;
2929
2930
0
      reldata->hashes = p;
2931
0
    }
2932
2933
0
  return true;
2934
0
}
2935
2936
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2937
   originated from the section given by INPUT_REL_HDR) to the
2938
   OUTPUT_BFD.  */
2939
2940
bool
2941
_bfd_elf_link_output_relocs (bfd *output_bfd,
2942
           asection *input_section,
2943
           Elf_Internal_Shdr *input_rel_hdr,
2944
           Elf_Internal_Rela *internal_relocs,
2945
           struct elf_link_hash_entry **rel_hash)
2946
0
{
2947
0
  Elf_Internal_Rela *irela;
2948
0
  Elf_Internal_Rela *irelaend;
2949
0
  bfd_byte *erel;
2950
0
  struct bfd_elf_section_reloc_data *output_reldata;
2951
0
  asection *output_section;
2952
0
  const struct elf_backend_data *bed;
2953
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2954
0
  struct bfd_elf_section_data *esdo;
2955
2956
0
  output_section = input_section->output_section;
2957
2958
0
  bed = get_elf_backend_data (output_bfd);
2959
0
  esdo = elf_section_data (output_section);
2960
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2961
0
    {
2962
0
      output_reldata = &esdo->rel;
2963
0
      swap_out = bed->s->swap_reloc_out;
2964
0
    }
2965
0
  else if (esdo->rela.hdr
2966
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2967
0
    {
2968
0
      output_reldata = &esdo->rela;
2969
0
      swap_out = bed->s->swap_reloca_out;
2970
0
    }
2971
0
  else
2972
0
    {
2973
0
      _bfd_error_handler
2974
  /* xgettext:c-format */
2975
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
2976
0
   output_bfd, input_section->owner, input_section);
2977
0
      bfd_set_error (bfd_error_wrong_format);
2978
0
      return false;
2979
0
    }
2980
2981
0
  erel = output_reldata->hdr->contents;
2982
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
2983
0
  irela = internal_relocs;
2984
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2985
0
          * bed->s->int_rels_per_ext_rel);
2986
0
  while (irela < irelaend)
2987
0
    {
2988
0
      if (rel_hash && *rel_hash)
2989
0
  (*rel_hash)->has_reloc = 1;
2990
0
      (*swap_out) (output_bfd, irela, erel);
2991
0
      irela += bed->s->int_rels_per_ext_rel;
2992
0
      erel += input_rel_hdr->sh_entsize;
2993
0
      if (rel_hash)
2994
0
  rel_hash++;
2995
0
    }
2996
2997
  /* Bump the counter, so that we know where to add the next set of
2998
     relocations.  */
2999
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3000
3001
0
  return true;
3002
0
}
3003

3004
/* Make weak undefined symbols in PIE dynamic.  */
3005
3006
bool
3007
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3008
         struct elf_link_hash_entry *h)
3009
0
{
3010
0
  if (bfd_link_pie (info)
3011
0
      && h->dynindx == -1
3012
0
      && h->root.type == bfd_link_hash_undefweak)
3013
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
3014
3015
0
  return true;
3016
0
}
3017
3018
/* Fix up the flags for a symbol.  This handles various cases which
3019
   can only be fixed after all the input files are seen.  This is
3020
   currently called by both adjust_dynamic_symbol and
3021
   assign_sym_version, which is unnecessary but perhaps more robust in
3022
   the face of future changes.  */
3023
3024
static bool
3025
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3026
         struct elf_info_failed *eif)
3027
0
{
3028
0
  const struct elf_backend_data *bed;
3029
3030
  /* If this symbol was mentioned in a non-ELF file, try to set
3031
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
3032
     permit a non-ELF file to correctly refer to a symbol defined in
3033
     an ELF dynamic object.  */
3034
0
  if (h->non_elf)
3035
0
    {
3036
0
      while (h->root.type == bfd_link_hash_indirect)
3037
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3038
3039
0
      if (h->root.type != bfd_link_hash_defined
3040
0
    && h->root.type != bfd_link_hash_defweak)
3041
0
  {
3042
0
    h->ref_regular = 1;
3043
0
    h->ref_regular_nonweak = 1;
3044
0
  }
3045
0
      else
3046
0
  {
3047
0
    if (h->root.u.def.section->owner != NULL
3048
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
3049
0
      == bfd_target_elf_flavour))
3050
0
      {
3051
0
        h->ref_regular = 1;
3052
0
        h->ref_regular_nonweak = 1;
3053
0
      }
3054
0
    else
3055
0
      h->def_regular = 1;
3056
0
  }
3057
3058
0
      if (h->dynindx == -1
3059
0
    && (h->def_dynamic
3060
0
        || h->ref_dynamic))
3061
0
  {
3062
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3063
0
      {
3064
0
        eif->failed = true;
3065
0
        return false;
3066
0
      }
3067
0
  }
3068
0
    }
3069
0
  else
3070
0
    {
3071
      /* Unfortunately, NON_ELF is only correct if the symbol
3072
   was first seen in a non-ELF file.  Fortunately, if the symbol
3073
   was first seen in an ELF file, we're probably OK unless the
3074
   symbol was defined in a non-ELF file.  Catch that case here.
3075
   FIXME: We're still in trouble if the symbol was first seen in
3076
   a dynamic object, and then later in a non-ELF regular object.  */
3077
0
      if ((h->root.type == bfd_link_hash_defined
3078
0
     || h->root.type == bfd_link_hash_defweak)
3079
0
    && !h->def_regular
3080
0
    && (h->root.u.def.section->owner != NULL
3081
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3082
0
     != bfd_target_elf_flavour)
3083
0
        : (bfd_is_abs_section (h->root.u.def.section)
3084
0
     && !h->def_dynamic)))
3085
0
  h->def_regular = 1;
3086
0
    }
3087
3088
  /* Backend specific symbol fixup.  */
3089
0
  bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3090
0
  if (bed->elf_backend_fixup_symbol
3091
0
      && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3092
0
    return false;
3093
3094
  /* If this is a final link, and the symbol was defined as a common
3095
     symbol in a regular object file, and there was no definition in
3096
     any dynamic object, then the linker will have allocated space for
3097
     the symbol in a common section but the DEF_REGULAR
3098
     flag will not have been set.  */
3099
0
  if (h->root.type == bfd_link_hash_defined
3100
0
      && !h->def_regular
3101
0
      && h->ref_regular
3102
0
      && !h->def_dynamic
3103
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3104
0
    h->def_regular = 1;
3105
3106
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3107
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3108
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3109
3110
  /* If a weak undefined symbol has non-default visibility, we also
3111
     hide it from the dynamic linker.  */
3112
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3113
0
     && h->root.type == bfd_link_hash_undefweak)
3114
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3115
3116
  /* A hidden versioned symbol in executable should be forced local if
3117
     it is is locally defined, not referenced by shared library and not
3118
     exported.  */
3119
0
  else if (bfd_link_executable (eif->info)
3120
0
     && h->versioned == versioned_hidden
3121
0
     && !eif->info->export_dynamic
3122
0
     && !h->dynamic
3123
0
     && !h->ref_dynamic
3124
0
     && h->def_regular)
3125
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3126
3127
  /* If -Bsymbolic was used (which means to bind references to global
3128
     symbols to the definition within the shared object), and this
3129
     symbol was defined in a regular object, then it actually doesn't
3130
     need a PLT entry.  Likewise, if the symbol has non-default
3131
     visibility.  If the symbol has hidden or internal visibility, we
3132
     will force it local.  */
3133
0
  else if (h->needs_plt
3134
0
     && bfd_link_pic (eif->info)
3135
0
     && is_elf_hash_table (eif->info->hash)
3136
0
     && (SYMBOLIC_BIND (eif->info, h)
3137
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3138
0
     && h->def_regular)
3139
0
    {
3140
0
      bool force_local;
3141
3142
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3143
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3144
0
      (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3145
0
    }
3146
3147
  /* If this is a weak defined symbol in a dynamic object, and we know
3148
     the real definition in the dynamic object, copy interesting flags
3149
     over to the real definition.  */
3150
0
  if (h->is_weakalias)
3151
0
    {
3152
0
      struct elf_link_hash_entry *def = weakdef (h);
3153
3154
      /* If the real definition is defined by a regular object file,
3155
   don't do anything special.  See the longer description in
3156
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3157
   bfd_link_hash_defined as it was when put on the alias list
3158
   then it must have originally been a versioned symbol (for
3159
   which a non-versioned indirect symbol is created) and later
3160
   a definition for the non-versioned symbol is found.  In that
3161
   case the indirection is flipped with the versioned symbol
3162
   becoming an indirect pointing at the non-versioned symbol.
3163
   Thus, not an alias any more.  */
3164
0
      if (def->def_regular
3165
0
    || def->root.type != bfd_link_hash_defined)
3166
0
  {
3167
0
    h = def;
3168
0
    while ((h = h->u.alias) != def)
3169
0
      h->is_weakalias = 0;
3170
0
  }
3171
0
      else
3172
0
  {
3173
0
    while (h->root.type == bfd_link_hash_indirect)
3174
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3175
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3176
0
          || h->root.type == bfd_link_hash_defweak);
3177
0
    BFD_ASSERT (def->def_dynamic);
3178
0
    (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3179
0
  }
3180
0
    }
3181
3182
0
  return true;
3183
0
}
3184
3185
/* Make the backend pick a good value for a dynamic symbol.  This is
3186
   called via elf_link_hash_traverse, and also calls itself
3187
   recursively.  */
3188
3189
static bool
3190
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3191
0
{
3192
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
3193
0
  struct elf_link_hash_table *htab;
3194
0
  const struct elf_backend_data *bed;
3195
3196
0
  if (! is_elf_hash_table (eif->info->hash))
3197
0
    return false;
3198
3199
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3200
0
  if (h->root.type == bfd_link_hash_indirect)
3201
0
    return true;
3202
3203
  /* Fix the symbol flags.  */
3204
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3205
0
    return false;
3206
3207
0
  htab = elf_hash_table (eif->info);
3208
0
  bed = get_elf_backend_data (htab->dynobj);
3209
3210
0
  if (h->root.type == bfd_link_hash_undefweak)
3211
0
    {
3212
0
      if (eif->info->dynamic_undefined_weak == 0)
3213
0
  (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3214
0
      else if (eif->info->dynamic_undefined_weak > 0
3215
0
         && h->ref_regular
3216
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3217
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3218
0
              h->root.root.string))
3219
0
  {
3220
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3221
0
      {
3222
0
        eif->failed = true;
3223
0
        return false;
3224
0
      }
3225
0
  }
3226
0
    }
3227
3228
  /* If this symbol does not require a PLT entry, and it is not
3229
     defined by a dynamic object, or is not referenced by a regular
3230
     object, ignore it.  We do have to handle a weak defined symbol,
3231
     even if no regular object refers to it, if we decided to add it
3232
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3233
     about symbols which are defined by one dynamic object and
3234
     referenced by another one?  */
3235
0
  if (!h->needs_plt
3236
0
      && h->type != STT_GNU_IFUNC
3237
0
      && (h->def_regular
3238
0
    || !h->def_dynamic
3239
0
    || (!h->ref_regular
3240
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3241
0
    {
3242
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3243
0
      return true;
3244
0
    }
3245
3246
  /* If we've already adjusted this symbol, don't do it again.  This
3247
     can happen via a recursive call.  */
3248
0
  if (h->dynamic_adjusted)
3249
0
    return true;
3250
3251
  /* Don't look at this symbol again.  Note that we must set this
3252
     after checking the above conditions, because we may look at a
3253
     symbol once, decide not to do anything, and then get called
3254
     recursively later after REF_REGULAR is set below.  */
3255
0
  h->dynamic_adjusted = 1;
3256
3257
  /* If this is a weak definition, and we know a real definition, and
3258
     the real symbol is not itself defined by a regular object file,
3259
     then get a good value for the real definition.  We handle the
3260
     real symbol first, for the convenience of the backend routine.
3261
3262
     Note that there is a confusing case here.  If the real definition
3263
     is defined by a regular object file, we don't get the real symbol
3264
     from the dynamic object, but we do get the weak symbol.  If the
3265
     processor backend uses a COPY reloc, then if some routine in the
3266
     dynamic object changes the real symbol, we will not see that
3267
     change in the corresponding weak symbol.  This is the way other
3268
     ELF linkers work as well, and seems to be a result of the shared
3269
     library model.
3270
3271
     I will clarify this issue.  Most SVR4 shared libraries define the
3272
     variable _timezone and define timezone as a weak synonym.  The
3273
     tzset call changes _timezone.  If you write
3274
       extern int timezone;
3275
       int _timezone = 5;
3276
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3277
     you might expect that, since timezone is a synonym for _timezone,
3278
     the same number will print both times.  However, if the processor
3279
     backend uses a COPY reloc, then actually timezone will be copied
3280
     into your process image, and, since you define _timezone
3281
     yourself, _timezone will not.  Thus timezone and _timezone will
3282
     wind up at different memory locations.  The tzset call will set
3283
     _timezone, leaving timezone unchanged.  */
3284
3285
0
  if (h->is_weakalias)
3286
0
    {
3287
0
      struct elf_link_hash_entry *def = weakdef (h);
3288
3289
      /* If we get to this point, there is an implicit reference to
3290
   the alias by a regular object file via the weak symbol H.  */
3291
0
      def->ref_regular = 1;
3292
3293
      /* Ensure that the backend adjust_dynamic_symbol function sees
3294
   the strong alias before H by recursively calling ourselves.  */
3295
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3296
0
  return false;
3297
0
    }
3298
3299
  /* If a symbol has no type and no size and does not require a PLT
3300
     entry, then we are probably about to do the wrong thing here: we
3301
     are probably going to create a COPY reloc for an empty object.
3302
     This case can arise when a shared object is built with assembly
3303
     code, and the assembly code fails to set the symbol type.  */
3304
0
  if (h->size == 0
3305
0
      && h->type == STT_NOTYPE
3306
0
      && !h->needs_plt)
3307
0
    _bfd_error_handler
3308
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3309
0
       h->root.root.string);
3310
3311
0
  if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3312
0
    {
3313
0
      eif->failed = true;
3314
0
      return false;
3315
0
    }
3316
3317
0
  return true;
3318
0
}
3319
3320
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3321
   DYNBSS.  */
3322
3323
bool
3324
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3325
            struct elf_link_hash_entry *h,
3326
            asection *dynbss)
3327
0
{
3328
0
  unsigned int power_of_two;
3329
0
  bfd_vma mask;
3330
0
  asection *sec = h->root.u.def.section;
3331
3332
  /* The section alignment of the definition is the maximum alignment
3333
     requirement of symbols defined in the section.  Since we don't
3334
     know the symbol alignment requirement, we start with the
3335
     maximum alignment and check low bits of the symbol address
3336
     for the minimum alignment.  */
3337
0
  power_of_two = bfd_section_alignment (sec);
3338
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3339
0
  while ((h->root.u.def.value & mask) != 0)
3340
0
    {
3341
0
       mask >>= 1;
3342
0
       --power_of_two;
3343
0
    }
3344
3345
0
  if (power_of_two > bfd_section_alignment (dynbss))
3346
0
    {
3347
      /* Adjust the section alignment if needed.  */
3348
0
      if (!bfd_set_section_alignment (dynbss, power_of_two))
3349
0
  return false;
3350
0
    }
3351
3352
  /* We make sure that the symbol will be aligned properly.  */
3353
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3354
3355
  /* Define the symbol as being at this point in DYNBSS.  */
3356
0
  h->root.u.def.section = dynbss;
3357
0
  h->root.u.def.value = dynbss->size;
3358
3359
  /* Increment the size of DYNBSS to make room for the symbol.  */
3360
0
  dynbss->size += h->size;
3361
3362
  /* No error if extern_protected_data is true.  */
3363
0
  if (h->protected_def
3364
0
      && (!info->extern_protected_data
3365
0
    || (info->extern_protected_data < 0
3366
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3367
0
    info->callbacks->einfo
3368
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3369
0
       h->root.root.string);
3370
3371
0
  return true;
3372
0
}
3373
3374
/* Adjust all external symbols pointing into SEC_MERGE sections
3375
   to reflect the object merging within the sections.  */
3376
3377
static bool
3378
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3379
0
{
3380
0
  asection *sec;
3381
3382
0
  if ((h->root.type == bfd_link_hash_defined
3383
0
       || h->root.type == bfd_link_hash_defweak)
3384
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3385
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3386
0
    {
3387
0
      bfd *output_bfd = (bfd *) data;
3388
3389
0
      h->root.u.def.value =
3390
0
  _bfd_merged_section_offset (output_bfd,
3391
0
            &h->root.u.def.section,
3392
0
            elf_section_data (sec)->sec_info,
3393
0
            h->root.u.def.value);
3394
0
    }
3395
3396
0
  return true;
3397
0
}
3398
3399
/* Returns false if the symbol referred to by H should be considered
3400
   to resolve local to the current module, and true if it should be
3401
   considered to bind dynamically.  */
3402
3403
bool
3404
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3405
         struct bfd_link_info *info,
3406
         bool not_local_protected)
3407
0
{
3408
0
  bool binding_stays_local_p;
3409
0
  const struct elf_backend_data *bed;
3410
0
  struct elf_link_hash_table *hash_table;
3411
3412
0
  if (h == NULL)
3413
0
    return false;
3414
3415
0
  while (h->root.type == bfd_link_hash_indirect
3416
0
   || h->root.type == bfd_link_hash_warning)
3417
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3418
3419
  /* If it was forced local, then clearly it's not dynamic.  */
3420
0
  if (h->dynindx == -1)
3421
0
    return false;
3422
0
  if (h->forced_local)
3423
0
    return false;
3424
3425
  /* Identify the cases where name binding rules say that a
3426
     visible symbol resolves locally.  */
3427
0
  binding_stays_local_p = (bfd_link_executable (info)
3428
0
         || SYMBOLIC_BIND (info, h));
3429
3430
0
  switch (ELF_ST_VISIBILITY (h->other))
3431
0
    {
3432
0
    case STV_INTERNAL:
3433
0
    case STV_HIDDEN:
3434
0
      return false;
3435
3436
0
    case STV_PROTECTED:
3437
0
      hash_table = elf_hash_table (info);
3438
0
      if (!is_elf_hash_table (&hash_table->root))
3439
0
  return false;
3440
3441
0
      bed = get_elf_backend_data (hash_table->dynobj);
3442
3443
      /* Proper resolution for function pointer equality may require
3444
   that these symbols perhaps be resolved dynamically, even though
3445
   we should be resolving them to the current module.  */
3446
0
      if (!not_local_protected || !bed->is_function_type (h->type))
3447
0
  binding_stays_local_p = true;
3448
0
      break;
3449
3450
0
    default:
3451
0
      break;
3452
0
    }
3453
3454
  /* If it isn't defined locally, then clearly it's dynamic.  */
3455
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3456
0
    return true;
3457
3458
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3459
     us that it remains local.  */
3460
0
  return !binding_stays_local_p;
3461
0
}
3462
3463
/* Return true if the symbol referred to by H should be considered
3464
   to resolve local to the current module, and false otherwise.  Differs
3465
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3466
   undefined symbols.  The two functions are virtually identical except
3467
   for the place where dynindx == -1 is tested.  If that test is true,
3468
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3469
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3470
   defined symbols.
3471
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3472
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3473
   treatment of undefined weak symbols.  For those that do not make
3474
   undefined weak symbols dynamic, both functions may return false.  */
3475
3476
bool
3477
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3478
            struct bfd_link_info *info,
3479
            bool local_protected)
3480
0
{
3481
0
  const struct elf_backend_data *bed;
3482
0
  struct elf_link_hash_table *hash_table;
3483
3484
  /* If it's a local sym, of course we resolve locally.  */
3485
0
  if (h == NULL)
3486
0
    return true;
3487
3488
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3489
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3490
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3491
0
    return true;
3492
3493
  /* Forced local symbols resolve locally.  */
3494
0
  if (h->forced_local)
3495
0
    return true;
3496
3497
  /* Common symbols that become definitions don't get the DEF_REGULAR
3498
     flag set, so test it first, and don't bail out.  */
3499
0
  if (ELF_COMMON_DEF_P (h))
3500
0
    /* Do nothing.  */;
3501
  /* If we don't have a definition in a regular file, then we can't
3502
     resolve locally.  The sym is either undefined or dynamic.  */
3503
0
  else if (!h->def_regular)
3504
0
    return false;
3505
3506
  /* Non-dynamic symbols resolve locally.  */
3507
0
  if (h->dynindx == -1)
3508
0
    return true;
3509
3510
  /* At this point, we know the symbol is defined and dynamic.  In an
3511
     executable it must resolve locally, likewise when building symbolic
3512
     shared libraries.  */
3513
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3514
0
    return true;
3515
3516
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3517
     with default visibility might not resolve locally.  */
3518
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3519
0
    return false;
3520
3521
0
  hash_table = elf_hash_table (info);
3522
0
  if (!is_elf_hash_table (&hash_table->root))
3523
0
    return true;
3524
3525
  /* STV_PROTECTED symbols with indirect external access are local. */
3526
0
  if (info->indirect_extern_access > 0)
3527
0
    return true;
3528
3529
0
  bed = get_elf_backend_data (hash_table->dynobj);
3530
3531
  /* If extern_protected_data is false, STV_PROTECTED non-function
3532
     symbols are local.  */
3533
0
  if ((!info->extern_protected_data
3534
0
       || (info->extern_protected_data < 0
3535
0
     && !bed->extern_protected_data))
3536
0
      && !bed->is_function_type (h->type))
3537
0
    return true;
3538
3539
  /* Function pointer equality tests may require that STV_PROTECTED
3540
     symbols be treated as dynamic symbols.  If the address of a
3541
     function not defined in an executable is set to that function's
3542
     plt entry in the executable, then the address of the function in
3543
     a shared library must also be the plt entry in the executable.  */
3544
0
  return local_protected;
3545
0
}
3546
3547
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3548
   aligned.  Returns the first TLS output section.  */
3549
3550
struct bfd_section *
3551
_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3552
0
{
3553
0
  struct bfd_section *sec, *tls;
3554
0
  unsigned int align = 0;
3555
3556
0
  for (sec = obfd->sections; sec != NULL; sec = sec->next)
3557
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3558
0
      break;
3559
0
  tls = sec;
3560
3561
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3562
0
    if (sec->alignment_power > align)
3563
0
      align = sec->alignment_power;
3564
3565
0
  elf_hash_table (info)->tls_sec = tls;
3566
3567
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3568
     alignment, so that the tls segment starts aligned.  */
3569
0
  if (tls != NULL)
3570
0
    tls->alignment_power = align;
3571
3572
0
  return tls;
3573
0
}
3574
3575
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3576
static bool
3577
is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3578
          Elf_Internal_Sym *sym)
3579
0
{
3580
0
  const struct elf_backend_data *bed;
3581
3582
  /* Local symbols do not count, but target specific ones might.  */
3583
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3584
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3585
0
    return false;
3586
3587
0
  bed = get_elf_backend_data (abfd);
3588
  /* Function symbols do not count.  */
3589
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3590
0
    return false;
3591
3592
  /* If the section is undefined, then so is the symbol.  */
3593
0
  if (sym->st_shndx == SHN_UNDEF)
3594
0
    return false;
3595
3596
  /* If the symbol is defined in the common section, then
3597
     it is a common definition and so does not count.  */
3598
0
  if (bed->common_definition (sym))
3599
0
    return false;
3600
3601
  /* If the symbol is in a target specific section then we
3602
     must rely upon the backend to tell us what it is.  */
3603
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3604
    /* FIXME - this function is not coded yet:
3605
3606
       return _bfd_is_global_symbol_definition (abfd, sym);
3607
3608
       Instead for now assume that the definition is not global,
3609
       Even if this is wrong, at least the linker will behave
3610
       in the same way that it used to do.  */
3611
0
    return false;
3612
3613
0
  return true;
3614
0
}
3615
3616
/* Search the symbol table of the archive element of the archive ABFD
3617
   whose archive map contains a mention of SYMDEF, and determine if
3618
   the symbol is defined in this element.  */
3619
static bool
3620
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3621
0
{
3622
0
  Elf_Internal_Shdr * hdr;
3623
0
  size_t symcount;
3624
0
  size_t extsymcount;
3625
0
  size_t extsymoff;
3626
0
  Elf_Internal_Sym *isymbuf;
3627
0
  Elf_Internal_Sym *isym;
3628
0
  Elf_Internal_Sym *isymend;
3629
0
  bool result;
3630
3631
0
  abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3632
0
  if (abfd == NULL)
3633
0
    return false;
3634
3635
0
  if (! bfd_check_format (abfd, bfd_object))
3636
0
    return false;
3637
3638
  /* Select the appropriate symbol table.  If we don't know if the
3639
     object file is an IR object, give linker LTO plugin a chance to
3640
     get the correct symbol table.  */
3641
0
  if (abfd->plugin_format == bfd_plugin_yes
3642
0
#if BFD_SUPPORTS_PLUGINS
3643
0
      || (abfd->plugin_format == bfd_plugin_unknown
3644
0
    && bfd_link_plugin_object_p (abfd))
3645
0
#endif
3646
0
      )
3647
0
    {
3648
      /* Use the IR symbol table if the object has been claimed by
3649
   plugin.  */
3650
0
      abfd = abfd->plugin_dummy_bfd;
3651
0
      hdr = &elf_tdata (abfd)->symtab_hdr;
3652
0
    }
3653
0
  else
3654
0
    {
3655
0
      if (elf_use_dt_symtab_p (abfd))
3656
0
  {
3657
0
    bfd_set_error (bfd_error_wrong_format);
3658
0
    return false;
3659
0
  }
3660
3661
0
      if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3662
0
  hdr = &elf_tdata (abfd)->symtab_hdr;
3663
0
      else
3664
0
  hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3665
0
    }
3666
3667
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3668
3669
  /* The sh_info field of the symtab header tells us where the
3670
     external symbols start.  We don't care about the local symbols.  */
3671
0
  if (elf_bad_symtab (abfd))
3672
0
    {
3673
0
      extsymcount = symcount;
3674
0
      extsymoff = 0;
3675
0
    }
3676
0
  else
3677
0
    {
3678
0
      extsymcount = symcount - hdr->sh_info;
3679
0
      extsymoff = hdr->sh_info;
3680
0
    }
3681
3682
0
  if (extsymcount == 0)
3683
0
    return false;
3684
3685
  /* Read in the symbol table.  */
3686
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3687
0
          NULL, NULL, NULL);
3688
0
  if (isymbuf == NULL)
3689
0
    return false;
3690
3691
  /* Scan the symbol table looking for SYMDEF.  */
3692
0
  result = false;
3693
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3694
0
    {
3695
0
      const char *name;
3696
3697
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3698
0
                isym->st_name);
3699
0
      if (name == NULL)
3700
0
  break;
3701
3702
0
      if (strcmp (name, symdef->name) == 0)
3703
0
  {
3704
0
    result = is_global_data_symbol_definition (abfd, isym);
3705
0
    break;
3706
0
  }
3707
0
    }
3708
3709
0
  free (isymbuf);
3710
3711
0
  return result;
3712
0
}
3713

3714
/* Add an entry to the .dynamic table.  */
3715
3716
bool
3717
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3718
          bfd_vma tag,
3719
          bfd_vma val)
3720
0
{
3721
0
  struct elf_link_hash_table *hash_table;
3722
0
  const struct elf_backend_data *bed;
3723
0
  asection *s;
3724
0
  bfd_size_type newsize;
3725
0
  bfd_byte *newcontents;
3726
0
  Elf_Internal_Dyn dyn;
3727
3728
0
  hash_table = elf_hash_table (info);
3729
0
  if (! is_elf_hash_table (&hash_table->root))
3730
0
    return false;
3731
3732
0
  if (tag == DT_RELA || tag == DT_REL)
3733
0
    hash_table->dynamic_relocs = true;
3734
3735
0
  bed = get_elf_backend_data (hash_table->dynobj);
3736
0
  s = hash_table->dynamic;
3737
0
  BFD_ASSERT (s != NULL);
3738
3739
0
  newsize = s->size + bed->s->sizeof_dyn;
3740
0
  newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3741
0
  if (newcontents == NULL)
3742
0
    return false;
3743
3744
0
  dyn.d_tag = tag;
3745
0
  dyn.d_un.d_val = val;
3746
0
  bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3747
3748
0
  s->size = newsize;
3749
0
  s->contents = newcontents;
3750
3751
0
  return true;
3752
0
}
3753
3754
/* Strip zero-sized dynamic sections.  */
3755
3756
bool
3757
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3758
0
{
3759
0
  struct elf_link_hash_table *hash_table;
3760
0
  const struct elf_backend_data *bed;
3761
0
  asection *s, *sdynamic, **pp;
3762
0
  asection *rela_dyn, *rel_dyn;
3763
0
  Elf_Internal_Dyn dyn;
3764
0
  bfd_byte *extdyn, *next;
3765
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3766
0
  bool strip_zero_sized;
3767
0
  bool strip_zero_sized_plt;
3768
3769
0
  if (bfd_link_relocatable (info))
3770
0
    return true;
3771
3772
0
  hash_table = elf_hash_table (info);
3773
0
  if (!is_elf_hash_table (&hash_table->root))
3774
0
    return false;
3775
3776
0
  if (!hash_table->dynobj)
3777
0
    return true;
3778
3779
0
  sdynamic= hash_table->dynamic;
3780
0
  if (!sdynamic)
3781
0
    return true;
3782
3783
0
  bed = get_elf_backend_data (hash_table->dynobj);
3784
0
  swap_dyn_in = bed->s->swap_dyn_in;
3785
3786
0
  strip_zero_sized = false;
3787
0
  strip_zero_sized_plt = false;
3788
3789
  /* Strip zero-sized dynamic sections.  */
3790
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3791
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3792
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3793
0
    if (s->size == 0
3794
0
  && (s == rela_dyn
3795
0
      || s == rel_dyn
3796
0
      || s == hash_table->srelplt->output_section
3797
0
      || s == hash_table->splt->output_section))
3798
0
      {
3799
0
  *pp = s->next;
3800
0
  info->output_bfd->section_count--;
3801
0
  strip_zero_sized = true;
3802
0
  if (s == rela_dyn)
3803
0
    s = rela_dyn;
3804
0
  if (s == rel_dyn)
3805
0
    s = rel_dyn;
3806
0
  else if (s == hash_table->splt->output_section)
3807
0
    {
3808
0
      s = hash_table->splt;
3809
0
      strip_zero_sized_plt = true;
3810
0
    }
3811
0
  else
3812
0
    s = hash_table->srelplt;
3813
0
  s->flags |= SEC_EXCLUDE;
3814
0
  s->output_section = bfd_abs_section_ptr;
3815
0
      }
3816
0
    else
3817
0
      pp = &s->next;
3818
3819
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3820
0
    for (extdyn = sdynamic->contents;
3821
0
   extdyn < sdynamic->contents + sdynamic->size;
3822
0
   extdyn = next)
3823
0
      {
3824
0
  next = extdyn + bed->s->sizeof_dyn;
3825
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3826
0
  switch (dyn.d_tag)
3827
0
    {
3828
0
    default:
3829
0
      break;
3830
0
    case DT_JMPREL:
3831
0
    case DT_PLTRELSZ:
3832
0
    case DT_PLTREL:
3833
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3834
         the procedure linkage table (the .plt section) has been
3835
         removed.  */
3836
0
      memmove (extdyn, next,
3837
0
         sdynamic->size - (next - sdynamic->contents));
3838
0
      next = extdyn;
3839
0
    }
3840
0
      }
3841
3842
0
  if (strip_zero_sized)
3843
0
    {
3844
      /* Regenerate program headers.  */
3845
0
      elf_seg_map (info->output_bfd) = NULL;
3846
0
      return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3847
0
            NULL);
3848
0
    }
3849
3850
0
  return true;
3851
0
}
3852
3853
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3854
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3855
3856
int
3857
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3858
0
{
3859
0
  struct elf_link_hash_table *hash_table;
3860
0
  size_t strindex;
3861
0
  const char *soname;
3862
3863
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3864
0
    return -1;
3865
3866
0
  hash_table = elf_hash_table (info);
3867
0
  soname = elf_dt_name (abfd);
3868
0
  strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3869
0
  if (strindex == (size_t) -1)
3870
0
    return -1;
3871
3872
0
  if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3873
0
    {
3874
0
      asection *sdyn;
3875
0
      const struct elf_backend_data *bed;
3876
0
      bfd_byte *extdyn;
3877
3878
0
      bed = get_elf_backend_data (hash_table->dynobj);
3879
0
      sdyn = hash_table->dynamic;
3880
0
      if (sdyn != NULL && sdyn->size != 0)
3881
0
  for (extdyn = sdyn->contents;
3882
0
       extdyn < sdyn->contents + sdyn->size;
3883
0
       extdyn += bed->s->sizeof_dyn)
3884
0
    {
3885
0
      Elf_Internal_Dyn dyn;
3886
3887
0
      bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3888
0
      if (dyn.d_tag == DT_NEEDED
3889
0
    && dyn.d_un.d_val == strindex)
3890
0
        {
3891
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3892
0
    return 1;
3893
0
        }
3894
0
    }
3895
0
    }
3896
3897
0
  if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3898
0
    return -1;
3899
3900
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3901
0
    return -1;
3902
3903
0
  return 0;
3904
0
}
3905
3906
/* Return true if SONAME is on the needed list between NEEDED and STOP
3907
   (or the end of list if STOP is NULL), and needed by a library that
3908
   will be loaded.  */
3909
3910
static bool
3911
on_needed_list (const char *soname,
3912
    struct bfd_link_needed_list *needed,
3913
    struct bfd_link_needed_list *stop)
3914
0
{
3915
0
  struct bfd_link_needed_list *look;
3916
0
  for (look = needed; look != stop; look = look->next)
3917
0
    if (strcmp (soname, look->name) == 0
3918
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3919
      /* If needed by a library that itself is not directly
3920
         needed, recursively check whether that library is
3921
         indirectly needed.  Since we add DT_NEEDED entries to
3922
         the end of the list, library dependencies appear after
3923
         the library.  Therefore search prior to the current
3924
         LOOK, preventing possible infinite recursion.  */
3925
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
3926
0
      return true;
3927
3928
0
  return false;
3929
0
}
3930
3931
/* Sort symbol by value, section, size, and type.  */
3932
static int
3933
elf_sort_symbol (const void *arg1, const void *arg2)
3934
0
{
3935
0
  const struct elf_link_hash_entry *h1;
3936
0
  const struct elf_link_hash_entry *h2;
3937
0
  bfd_signed_vma vdiff;
3938
0
  int sdiff;
3939
0
  const char *n1;
3940
0
  const char *n2;
3941
3942
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
3943
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
3944
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
3945
0
  if (vdiff != 0)
3946
0
    return vdiff > 0 ? 1 : -1;
3947
3948
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3949
0
  if (sdiff != 0)
3950
0
    return sdiff;
3951
3952
  /* Sort so that sized symbols are selected over zero size symbols.  */
3953
0
  vdiff = h1->size - h2->size;
3954
0
  if (vdiff != 0)
3955
0
    return vdiff > 0 ? 1 : -1;
3956
3957
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
3958
0
  if (h1->type != h2->type)
3959
0
    return h1->type - h2->type;
3960
3961
  /* If symbols are properly sized and typed, and multiple strong
3962
     aliases are not defined in a shared library by the user we
3963
     shouldn't get here.  Unfortunately linker script symbols like
3964
     __bss_start sometimes match a user symbol defined at the start of
3965
     .bss without proper size and type.  We'd like to preference the
3966
     user symbol over reserved system symbols.  Sort on leading
3967
     underscores.  */
3968
0
  n1 = h1->root.root.string;
3969
0
  n2 = h2->root.root.string;
3970
0
  while (*n1 == *n2)
3971
0
    {
3972
0
      if (*n1 == 0)
3973
0
  break;
3974
0
      ++n1;
3975
0
      ++n2;
3976
0
    }
3977
0
  if (*n1 == '_')
3978
0
    return -1;
3979
0
  if (*n2 == '_')
3980
0
    return 1;
3981
3982
  /* Final sort on name selects user symbols like '_u' over reserved
3983
     system symbols like '_Z' and also will avoid qsort instability.  */
3984
0
  return *n1 - *n2;
3985
0
}
3986
3987
/* This function is used to adjust offsets into .dynstr for
3988
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
3989
3990
static bool
3991
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3992
0
{
3993
0
  struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3994
3995
0
  if (h->dynindx != -1)
3996
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3997
0
  return true;
3998
0
}
3999
4000
/* Assign string offsets in .dynstr, update all structures referencing
4001
   them.  */
4002
4003
static bool
4004
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4005
0
{
4006
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
4007
0
  struct elf_link_local_dynamic_entry *entry;
4008
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
4009
0
  bfd *dynobj = hash_table->dynobj;
4010
0
  asection *sdyn;
4011
0
  bfd_size_type size;
4012
0
  const struct elf_backend_data *bed;
4013
0
  bfd_byte *extdyn;
4014
4015
0
  _bfd_elf_strtab_finalize (dynstr);
4016
0
  size = _bfd_elf_strtab_size (dynstr);
4017
4018
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
4019
4020
0
  if (info->callbacks->examine_strtab)
4021
0
    info->callbacks->examine_strtab (dynstr);
4022
4023
0
  bed = get_elf_backend_data (dynobj);
4024
0
  sdyn = hash_table->dynamic;
4025
0
  BFD_ASSERT (sdyn != NULL);
4026
4027
  /* Update all .dynamic entries referencing .dynstr strings.  */
4028
0
  for (extdyn = sdyn->contents;
4029
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4030
0
       extdyn += bed->s->sizeof_dyn)
4031
0
    {
4032
0
      Elf_Internal_Dyn dyn;
4033
4034
0
      bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4035
0
      switch (dyn.d_tag)
4036
0
  {
4037
0
  case DT_STRSZ:
4038
0
    dyn.d_un.d_val = size;
4039
0
    break;
4040
0
  case DT_NEEDED:
4041
0
  case DT_SONAME:
4042
0
  case DT_RPATH:
4043
0
  case DT_RUNPATH:
4044
0
  case DT_FILTER:
4045
0
  case DT_AUXILIARY:
4046
0
  case DT_AUDIT:
4047
0
  case DT_DEPAUDIT:
4048
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4049
0
    break;
4050
0
  default:
4051
0
    continue;
4052
0
  }
4053
0
      bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4054
0
    }
4055
4056
  /* Now update local dynamic symbols.  */
4057
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4058
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4059
0
              entry->isym.st_name);
4060
4061
  /* And the rest of dynamic symbols.  */
4062
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4063
4064
  /* Adjust version definitions.  */
4065
0
  if (elf_tdata (output_bfd)->cverdefs)
4066
0
    {
4067
0
      asection *s;
4068
0
      bfd_byte *p;
4069
0
      size_t i;
4070
0
      Elf_Internal_Verdef def;
4071
0
      Elf_Internal_Verdaux defaux;
4072
4073
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4074
0
      p = s->contents;
4075
0
      do
4076
0
  {
4077
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4078
0
           &def);
4079
0
    p += sizeof (Elf_External_Verdef);
4080
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4081
0
      continue;
4082
0
    for (i = 0; i < def.vd_cnt; ++i)
4083
0
      {
4084
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4085
0
          (Elf_External_Verdaux *) p, &defaux);
4086
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4087
0
              defaux.vda_name);
4088
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4089
0
           &defaux, (Elf_External_Verdaux *) p);
4090
0
        p += sizeof (Elf_External_Verdaux);
4091
0
      }
4092
0
  }
4093
0
      while (def.vd_next);
4094
0
    }
4095
4096
  /* Adjust version references.  */
4097
0
  if (elf_tdata (output_bfd)->verref)
4098
0
    {
4099
0
      asection *s;
4100
0
      bfd_byte *p;
4101
0
      size_t i;
4102
0
      Elf_Internal_Verneed need;
4103
0
      Elf_Internal_Vernaux needaux;
4104
4105
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4106
0
      p = s->contents;
4107
0
      do
4108
0
  {
4109
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4110
0
            &need);
4111
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4112
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4113
0
             (Elf_External_Verneed *) p);
4114
0
    p += sizeof (Elf_External_Verneed);
4115
0
    for (i = 0; i < need.vn_cnt; ++i)
4116
0
      {
4117
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4118
0
          (Elf_External_Vernaux *) p, &needaux);
4119
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4120
0
               needaux.vna_name);
4121
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4122
0
           &needaux,
4123
0
           (Elf_External_Vernaux *) p);
4124
0
        p += sizeof (Elf_External_Vernaux);
4125
0
      }
4126
0
  }
4127
0
      while (need.vn_next);
4128
0
    }
4129
4130
0
  return true;
4131
0
}
4132

4133
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4134
   The default is to only match when the INPUT and OUTPUT are exactly
4135
   the same target.  */
4136
4137
bool
4138
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4139
            const bfd_target *output)
4140
0
{
4141
0
  return input == output;
4142
0
}
4143
4144
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4145
   This version is used when different targets for the same architecture
4146
   are virtually identical.  */
4147
4148
bool
4149
_bfd_elf_relocs_compatible (const bfd_target *input,
4150
          const bfd_target *output)
4151
0
{
4152
0
  const struct elf_backend_data *obed, *ibed;
4153
4154
0
  if (input == output)
4155
0
    return true;
4156
4157
0
  ibed = xvec_get_elf_backend_data (input);
4158
0
  obed = xvec_get_elf_backend_data (output);
4159
4160
0
  if (ibed->arch != obed->arch)
4161
0
    return false;
4162
4163
  /* If both backends are using this function, deem them compatible.  */
4164
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4165
0
}
4166
4167
/* Make a special call to the linker "notice" function to tell it that
4168
   we are about to handle an as-needed lib, or have finished
4169
   processing the lib.  */
4170
4171
bool
4172
_bfd_elf_notice_as_needed (bfd *ibfd,
4173
         struct bfd_link_info *info,
4174
         enum notice_asneeded_action act)
4175
0
{
4176
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4177
0
}
4178
4179
/* Call ACTION on each relocation in an ELF object file.  */
4180
4181
bool
4182
_bfd_elf_link_iterate_on_relocs
4183
  (bfd *abfd, struct bfd_link_info *info,
4184
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4185
       const Elf_Internal_Rela *))
4186
0
{
4187
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4188
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4189
4190
  /* If this object is the same format as the output object, and it is
4191
     not a shared library, then let the backend look through the
4192
     relocs.
4193
4194
     This is required to build global offset table entries and to
4195
     arrange for dynamic relocs.  It is not required for the
4196
     particular common case of linking non PIC code, even when linking
4197
     against shared libraries, but unfortunately there is no way of
4198
     knowing whether an object file has been compiled PIC or not.
4199
     Looking through the relocs is not particularly time consuming.
4200
     The problem is that we must either (1) keep the relocs in memory,
4201
     which causes the linker to require additional runtime memory or
4202
     (2) read the relocs twice from the input file, which wastes time.
4203
     This would be a good case for using mmap.
4204
4205
     I have no idea how to handle linking PIC code into a file of a
4206
     different format.  It probably can't be done.  */
4207
0
  if ((abfd->flags & DYNAMIC) == 0
4208
0
      && is_elf_hash_table (&htab->root)
4209
0
      && elf_object_id (abfd) == elf_hash_table_id (htab)
4210
0
      && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4211
0
    {
4212
0
      asection *o;
4213
4214
0
      for (o = abfd->sections; o != NULL; o = o->next)
4215
0
  {
4216
0
    Elf_Internal_Rela *internal_relocs;
4217
0
    bool ok;
4218
4219
    /* Don't check relocations in excluded sections.  Don't do
4220
       anything special with non-loaded, non-alloced sections.
4221
       In particular, any relocs in such sections should not
4222
       affect GOT and PLT reference counting (ie.  we don't
4223
       allow them to create GOT or PLT entries), there's no
4224
       possibility or desire to optimize TLS relocs, and
4225
       there's not much point in propagating relocs to shared
4226
       libs that the dynamic linker won't relocate.  */
4227
0
    if ((o->flags & SEC_ALLOC) == 0
4228
0
        || (o->flags & SEC_RELOC) == 0
4229
0
        || (o->flags & SEC_EXCLUDE) != 0
4230
0
        || o->reloc_count == 0
4231
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4232
0
      && (o->flags & SEC_DEBUGGING) != 0)
4233
0
        || bfd_is_abs_section (o->output_section))
4234
0
      continue;
4235
4236
0
    internal_relocs = _bfd_elf_link_info_read_relocs
4237
0
      (abfd, info, o, NULL, NULL,
4238
0
       _bfd_elf_link_keep_memory (info));
4239
0
    if (internal_relocs == NULL)
4240
0
      return false;
4241
4242
0
    ok = action (abfd, info, o, internal_relocs);
4243
4244
0
    if (elf_section_data (o)->relocs != internal_relocs)
4245
0
      free (internal_relocs);
4246
4247
0
    if (! ok)
4248
0
      return false;
4249
0
  }
4250
0
    }
4251
4252
0
  return true;
4253
0
}
4254
4255
/* Check relocations in an ELF object file.  This is called after
4256
   all input files have been opened.  */
4257
4258
bool
4259
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4260
0
{
4261
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4262
0
  if (bed->check_relocs != NULL)
4263
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4264
0
              bed->check_relocs);
4265
0
  return true;
4266
0
}
4267
4268
/* An entry in the first definition hash table.  */
4269
4270
struct elf_link_first_hash_entry
4271
{
4272
  struct bfd_hash_entry root;
4273
  /* The object of the first definition.  */
4274
  bfd *abfd;
4275
};
4276
4277
/* The function to create a new entry in the first definition hash
4278
   table.  */
4279
4280
static struct bfd_hash_entry *
4281
elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4282
           struct bfd_hash_table *table,
4283
           const char *string)
4284
0
{
4285
0
  struct elf_link_first_hash_entry *ret =
4286
0
    (struct elf_link_first_hash_entry *) entry;
4287
4288
  /* Allocate the structure if it has not already been allocated by a
4289
     subclass.  */
4290
0
  if (ret == NULL)
4291
0
    ret = (struct elf_link_first_hash_entry *)
4292
0
  bfd_hash_allocate (table,
4293
0
         sizeof (struct elf_link_first_hash_entry));
4294
0
  if (ret == NULL)
4295
0
    return NULL;
4296
4297
  /* Call the allocation method of the superclass.  */
4298
0
  ret = ((struct elf_link_first_hash_entry *)
4299
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4300
0
         string));
4301
0
  if (ret != NULL)
4302
0
    ret->abfd = NULL;
4303
4304
0
  return (struct bfd_hash_entry *) ret;
4305
0
}
4306
4307
/* Add the symbol NAME from ABFD to first hash.  */
4308
4309
static void
4310
elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4311
          const char *name, bool copy)
4312
0
{
4313
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4314
  /* Skip if there is no first hash.  */
4315
0
  if (htab->first_hash == NULL)
4316
0
    return;
4317
4318
0
  struct elf_link_first_hash_entry *e
4319
0
    = ((struct elf_link_first_hash_entry *)
4320
0
       bfd_hash_lookup (htab->first_hash, name, true, copy));
4321
0
  if (e == NULL)
4322
0
    info->callbacks->einfo
4323
0
      (_("%F%P: %pB: failed to add %s to first hash\n"), abfd, name);
4324
4325
0
  if (e->abfd == NULL)
4326
    /* Store ABFD in abfd.  */
4327
0
    e->abfd = abfd;
4328
0
}
4329
4330
/* Add symbols from an ELF object file to the linker hash table.  */
4331
4332
static bool
4333
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4334
0
{
4335
0
  Elf_Internal_Ehdr *ehdr;
4336
0
  Elf_Internal_Shdr *hdr;
4337
0
  size_t symcount;
4338
0
  size_t extsymcount;
4339
0
  size_t extsymoff;
4340
0
  struct elf_link_hash_entry **sym_hash;
4341
0
  bool dynamic;
4342
0
  Elf_External_Versym *extversym = NULL;
4343
0
  Elf_External_Versym *extversym_end = NULL;
4344
0
  Elf_External_Versym *ever;
4345
0
  struct elf_link_hash_entry *weaks;
4346
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4347
0
  size_t nondeflt_vers_cnt = 0;
4348
0
  Elf_Internal_Sym *isymbuf = NULL;
4349
0
  Elf_Internal_Sym *isym;
4350
0
  Elf_Internal_Sym *isymend;
4351
0
  const struct elf_backend_data *bed;
4352
0
  bool add_needed;
4353
0
  struct elf_link_hash_table *htab;
4354
0
  void *alloc_mark = NULL;
4355
0
  struct bfd_hash_entry **old_table = NULL;
4356
0
  unsigned int old_size = 0;
4357
0
  unsigned int old_count = 0;
4358
0
  void *old_tab = NULL;
4359
0
  void *old_ent;
4360
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4361
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4362
0
  void *old_strtab = NULL;
4363
0
  size_t tabsize = 0;
4364
0
  asection *s;
4365
0
  bool just_syms;
4366
4367
0
  htab = elf_hash_table (info);
4368
0
  bed = get_elf_backend_data (abfd);
4369
4370
0
  if (elf_use_dt_symtab_p (abfd))
4371
0
    {
4372
0
      bfd_set_error (bfd_error_wrong_format);
4373
0
      return false;
4374
0
    }
4375
4376
0
  if ((abfd->flags & DYNAMIC) == 0)
4377
0
    {
4378
0
      dynamic = false;
4379
0
      if ((abfd->flags & BFD_PLUGIN) != 0
4380
0
    && is_elf_hash_table (&htab->root)
4381
0
    && htab->first_hash == NULL)
4382
0
  {
4383
    /* Initialize first_hash for an IR input.  */
4384
0
    htab->first_hash = (struct bfd_hash_table *)
4385
0
      bfd_malloc (sizeof (struct bfd_hash_table));
4386
0
    if (htab->first_hash == NULL
4387
0
        || !bfd_hash_table_init
4388
0
       (htab->first_hash, elf_link_first_hash_newfunc,
4389
0
        sizeof (struct elf_link_first_hash_entry)))
4390
0
      info->callbacks->einfo
4391
0
        (_("%F%P: first_hash failed to create: %E\n"));
4392
0
  }
4393
0
    }
4394
0
  else
4395
0
    {
4396
0
      dynamic = true;
4397
4398
      /* You can't use -r against a dynamic object.  Also, there's no
4399
   hope of using a dynamic object which does not exactly match
4400
   the format of the output file.  */
4401
0
      if (bfd_link_relocatable (info)
4402
0
    || !is_elf_hash_table (&htab->root)
4403
0
    || info->output_bfd->xvec != abfd->xvec)
4404
0
  {
4405
0
    if (bfd_link_relocatable (info))
4406
0
      bfd_set_error (bfd_error_invalid_operation);
4407
0
    else
4408
0
      bfd_set_error (bfd_error_wrong_format);
4409
0
    goto error_return;
4410
0
  }
4411
0
    }
4412
4413
0
  ehdr = elf_elfheader (abfd);
4414
0
  if (info->warn_alternate_em
4415
0
      && bed->elf_machine_code != ehdr->e_machine
4416
0
      && ((bed->elf_machine_alt1 != 0
4417
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4418
0
    || (bed->elf_machine_alt2 != 0
4419
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4420
0
    _bfd_error_handler
4421
      /* xgettext:c-format */
4422
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4423
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4424
4425
  /* As a GNU extension, any input sections which are named
4426
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4427
     symbol.  This differs from .gnu.warning sections, which generate
4428
     warnings when they are included in an output file.  */
4429
  /* PR 12761: Also generate this warning when building shared libraries.  */
4430
0
  for (s = abfd->sections; s != NULL; s = s->next)
4431
0
    {
4432
0
      const char *name;
4433
4434
0
      name = bfd_section_name (s);
4435
0
      if (startswith (name, ".gnu.warning."))
4436
0
  {
4437
0
    char *msg;
4438
0
    bfd_size_type sz;
4439
4440
0
    name += sizeof ".gnu.warning." - 1;
4441
4442
    /* If this is a shared object, then look up the symbol
4443
       in the hash table.  If it is there, and it is already
4444
       been defined, then we will not be using the entry
4445
       from this shared object, so we don't need to warn.
4446
       FIXME: If we see the definition in a regular object
4447
       later on, we will warn, but we shouldn't.  The only
4448
       fix is to keep track of what warnings we are supposed
4449
       to emit, and then handle them all at the end of the
4450
       link.  */
4451
0
    if (dynamic)
4452
0
      {
4453
0
        struct elf_link_hash_entry *h;
4454
4455
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4456
4457
        /* FIXME: What about bfd_link_hash_common?  */
4458
0
        if (h != NULL
4459
0
      && (h->root.type == bfd_link_hash_defined
4460
0
          || h->root.type == bfd_link_hash_defweak))
4461
0
    continue;
4462
0
      }
4463
4464
0
    sz = s->size;
4465
0
    msg = (char *) bfd_alloc (abfd, sz + 1);
4466
0
    if (msg == NULL)
4467
0
      goto error_return;
4468
4469
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4470
0
      goto error_return;
4471
4472
0
    msg[sz] = '\0';
4473
4474
0
    if (! (_bfd_generic_link_add_one_symbol
4475
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4476
0
      false, bed->collect, NULL)))
4477
0
      goto error_return;
4478
4479
0
    if (bfd_link_executable (info))
4480
0
      {
4481
        /* Clobber the section size so that the warning does
4482
     not get copied into the output file.  */
4483
0
        s->size = 0;
4484
4485
        /* Also set SEC_EXCLUDE, so that symbols defined in
4486
     the warning section don't get copied to the output.  */
4487
0
        s->flags |= SEC_EXCLUDE;
4488
0
      }
4489
0
  }
4490
0
    }
4491
4492
0
  just_syms = ((s = abfd->sections) != NULL
4493
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4494
4495
0
  add_needed = true;
4496
0
  if (! dynamic)
4497
0
    {
4498
      /* If we are creating a shared library, create all the dynamic
4499
   sections immediately.  We need to attach them to something,
4500
   so we attach them to this BFD, provided it is the right
4501
   format and is not from ld --just-symbols.  Always create the
4502
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4503
   are no input BFD's of the same format as the output, we can't
4504
   make a shared library.  */
4505
0
      if (!just_syms
4506
0
    && (bfd_link_pic (info)
4507
0
        || (!bfd_link_relocatable (info)
4508
0
      && info->nointerp
4509
0
      && (info->export_dynamic || info->dynamic)))
4510
0
    && is_elf_hash_table (&htab->root)
4511
0
    && info->output_bfd->xvec == abfd->xvec
4512
0
    && !htab->dynamic_sections_created)
4513
0
  {
4514
0
    if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4515
0
      goto error_return;
4516
0
  }
4517
0
    }
4518
0
  else if (!is_elf_hash_table (&htab->root))
4519
0
    goto error_return;
4520
0
  else
4521
0
    {
4522
0
      const char *soname = NULL;
4523
0
      char *audit = NULL;
4524
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4525
0
      const Elf_Internal_Phdr *phdr;
4526
0
      struct elf_link_loaded_list *loaded_lib;
4527
4528
      /* ld --just-symbols and dynamic objects don't mix very well.
4529
   ld shouldn't allow it.  */
4530
0
      if (just_syms)
4531
0
  abort ();
4532
4533
      /* If this dynamic lib was specified on the command line with
4534
   --as-needed in effect, then we don't want to add a DT_NEEDED
4535
   tag unless the lib is actually used.  Similary for libs brought
4536
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4537
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4538
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4539
   all.  */
4540
0
      add_needed = (elf_dyn_lib_class (abfd)
4541
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4542
0
           | DYN_NO_NEEDED)) == 0;
4543
4544
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4545
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4546
0
  {
4547
0
    bfd_byte *dynbuf;
4548
0
    bfd_byte *extdyn;
4549
0
    unsigned int elfsec;
4550
0
    unsigned long shlink;
4551
4552
0
    if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4553
0
      {
4554
0
      error_free_dyn:
4555
0
        _bfd_elf_munmap_section_contents (s, dynbuf);
4556
0
        goto error_return;
4557
0
      }
4558
4559
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4560
0
    if (elfsec == SHN_BAD)
4561
0
      goto error_free_dyn;
4562
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4563
4564
0
    for (extdyn = dynbuf;
4565
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4566
0
         extdyn += bed->s->sizeof_dyn)
4567
0
      {
4568
0
        Elf_Internal_Dyn dyn;
4569
4570
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4571
0
        if (dyn.d_tag == DT_SONAME)
4572
0
    {
4573
0
      unsigned int tagv = dyn.d_un.d_val;
4574
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4575
0
      if (soname == NULL)
4576
0
        goto error_free_dyn;
4577
0
    }
4578
0
        if (dyn.d_tag == DT_NEEDED)
4579
0
    {
4580
0
      struct bfd_link_needed_list *n, **pn;
4581
0
      char *fnm, *anm;
4582
0
      unsigned int tagv = dyn.d_un.d_val;
4583
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4584
4585
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4586
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4587
0
      if (n == NULL || fnm == NULL)
4588
0
        goto error_free_dyn;
4589
0
      amt = strlen (fnm) + 1;
4590
0
      anm = (char *) bfd_alloc (abfd, amt);
4591
0
      if (anm == NULL)
4592
0
        goto error_free_dyn;
4593
0
      memcpy (anm, fnm, amt);
4594
0
      n->name = anm;
4595
0
      n->by = abfd;
4596
0
      n->next = NULL;
4597
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4598
0
        ;
4599
0
      *pn = n;
4600
0
    }
4601
0
        if (dyn.d_tag == DT_RUNPATH)
4602
0
    {
4603
0
      struct bfd_link_needed_list *n, **pn;
4604
0
      char *fnm, *anm;
4605
0
      unsigned int tagv = dyn.d_un.d_val;
4606
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4607
4608
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4609
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4610
0
      if (n == NULL || fnm == NULL)
4611
0
        goto error_free_dyn;
4612
0
      amt = strlen (fnm) + 1;
4613
0
      anm = (char *) bfd_alloc (abfd, amt);
4614
0
      if (anm == NULL)
4615
0
        goto error_free_dyn;
4616
0
      memcpy (anm, fnm, amt);
4617
0
      n->name = anm;
4618
0
      n->by = abfd;
4619
0
      n->next = NULL;
4620
0
      for (pn = & runpath;
4621
0
           *pn != NULL;
4622
0
           pn = &(*pn)->next)
4623
0
        ;
4624
0
      *pn = n;
4625
0
    }
4626
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4627
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4628
0
    {
4629
0
      struct bfd_link_needed_list *n, **pn;
4630
0
      char *fnm, *anm;
4631
0
      unsigned int tagv = dyn.d_un.d_val;
4632
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4633
4634
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4635
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4636
0
      if (n == NULL || fnm == NULL)
4637
0
        goto error_free_dyn;
4638
0
      amt = strlen (fnm) + 1;
4639
0
      anm = (char *) bfd_alloc (abfd, amt);
4640
0
      if (anm == NULL)
4641
0
        goto error_free_dyn;
4642
0
      memcpy (anm, fnm, amt);
4643
0
      n->name = anm;
4644
0
      n->by = abfd;
4645
0
      n->next = NULL;
4646
0
      for (pn = & rpath;
4647
0
           *pn != NULL;
4648
0
           pn = &(*pn)->next)
4649
0
        ;
4650
0
      *pn = n;
4651
0
    }
4652
0
        if (dyn.d_tag == DT_AUDIT)
4653
0
    {
4654
0
      unsigned int tagv = dyn.d_un.d_val;
4655
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4656
0
    }
4657
0
        if (dyn.d_tag == DT_FLAGS_1)
4658
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4659
0
      }
4660
4661
0
    _bfd_elf_munmap_section_contents (s, dynbuf);
4662
0
  }
4663
4664
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4665
   frees all more recently bfd_alloc'd blocks as well.  */
4666
0
      if (runpath)
4667
0
  rpath = runpath;
4668
4669
0
      if (rpath)
4670
0
  {
4671
0
    struct bfd_link_needed_list **pn;
4672
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4673
0
      ;
4674
0
    *pn = rpath;
4675
0
  }
4676
4677
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4678
   all sections contained fully therein.  This makes relro
4679
   shared library sections appear as they will at run-time.  */
4680
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4681
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4682
0
  if (phdr->p_type == PT_GNU_RELRO)
4683
0
    {
4684
0
      for (s = abfd->sections; s != NULL; s = s->next)
4685
0
        {
4686
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4687
4688
0
    if ((s->flags & SEC_ALLOC) != 0
4689
0
        && s->vma * opb >= phdr->p_vaddr
4690
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4691
0
      s->flags |= SEC_READONLY;
4692
0
        }
4693
0
      break;
4694
0
    }
4695
4696
      /* We do not want to include any of the sections in a dynamic
4697
   object in the output file.  We hack by simply clobbering the
4698
   list of sections in the BFD.  This could be handled more
4699
   cleanly by, say, a new section flag; the existing
4700
   SEC_NEVER_LOAD flag is not the one we want, because that one
4701
   still implies that the section takes up space in the output
4702
   file.  */
4703
0
      bfd_section_list_clear (abfd);
4704
4705
      /* Find the name to use in a DT_NEEDED entry that refers to this
4706
   object.  If the object has a DT_SONAME entry, we use it.
4707
   Otherwise, if the generic linker stuck something in
4708
   elf_dt_name, we use that.  Otherwise, we just use the file
4709
   name.  */
4710
0
      if (soname == NULL || *soname == '\0')
4711
0
  {
4712
0
    soname = elf_dt_name (abfd);
4713
0
    if (soname == NULL || *soname == '\0')
4714
0
      soname = bfd_get_filename (abfd);
4715
0
  }
4716
4717
      /* Save the SONAME because sometimes the linker emulation code
4718
   will need to know it.  */
4719
0
      elf_dt_name (abfd) = soname;
4720
4721
      /* If we have already included this dynamic object in the
4722
   link, just ignore it.  There is no reason to include a
4723
   particular dynamic object more than once.  */
4724
0
      for (loaded_lib = htab->dyn_loaded;
4725
0
     loaded_lib != NULL;
4726
0
     loaded_lib = loaded_lib->next)
4727
0
  {
4728
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4729
0
      return true;
4730
0
  }
4731
4732
      /* Create dynamic sections for backends that require that be done
4733
   before setup_gnu_properties.  */
4734
0
      if (add_needed
4735
0
    && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4736
0
  return false;
4737
4738
      /* Save the DT_AUDIT entry for the linker emulation code. */
4739
0
      elf_dt_audit (abfd) = audit;
4740
0
    }
4741
4742
  /* If this is a dynamic object, we always link against the .dynsym
4743
     symbol table, not the .symtab symbol table.  The dynamic linker
4744
     will only see the .dynsym symbol table, so there is no reason to
4745
     look at .symtab for a dynamic object.  */
4746
4747
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4748
0
    hdr = &elf_tdata (abfd)->symtab_hdr;
4749
0
  else
4750
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4751
4752
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4753
4754
  /* The sh_info field of the symtab header tells us where the
4755
     external symbols start.  We don't care about the local symbols at
4756
     this point.  */
4757
0
  if (elf_bad_symtab (abfd))
4758
0
    {
4759
0
      extsymcount = symcount;
4760
0
      extsymoff = 0;
4761
0
    }
4762
0
  else
4763
0
    {
4764
0
      extsymcount = symcount - hdr->sh_info;
4765
0
      extsymoff = hdr->sh_info;
4766
0
    }
4767
4768
0
  sym_hash = elf_sym_hashes (abfd);
4769
0
  if (extsymcount != 0)
4770
0
    {
4771
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4772
0
              NULL, NULL, NULL);
4773
0
      if (isymbuf == NULL)
4774
0
  goto error_return;
4775
4776
0
      if (sym_hash == NULL)
4777
0
  {
4778
    /* We store a pointer to the hash table entry for each
4779
       external symbol.  */
4780
0
    size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4781
0
    sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4782
0
    if (sym_hash == NULL)
4783
0
      goto error_free_sym;
4784
0
    elf_sym_hashes (abfd) = sym_hash;
4785
0
  }
4786
0
    }
4787
4788
0
  if (dynamic)
4789
0
    {
4790
      /* Read in any version definitions.  */
4791
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4792
0
            info->default_imported_symver))
4793
0
  goto error_free_sym;
4794
4795
      /* Read in the symbol versions, but don't bother to convert them
4796
   to internal format.  */
4797
0
      if (elf_dynversym (abfd) != 0)
4798
0
  {
4799
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4800
0
    bfd_size_type amt = versymhdr->sh_size;
4801
4802
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4803
0
      goto error_free_sym;
4804
0
    extversym = (Elf_External_Versym *)
4805
0
      _bfd_malloc_and_read (abfd, amt, amt);
4806
0
    if (extversym == NULL)
4807
0
      goto error_free_sym;
4808
0
    extversym_end = extversym + amt / sizeof (*extversym);
4809
0
  }
4810
0
    }
4811
4812
  /* If we are loading an as-needed shared lib, save the symbol table
4813
     state before we start adding symbols.  If the lib turns out
4814
     to be unneeded, restore the state.  */
4815
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4816
0
    {
4817
0
      unsigned int i;
4818
0
      size_t entsize;
4819
4820
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4821
0
  {
4822
0
    struct bfd_hash_entry *p;
4823
0
    struct elf_link_hash_entry *h;
4824
4825
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4826
0
      {
4827
0
        h = (struct elf_link_hash_entry *) p;
4828
0
        entsize += htab->root.table.entsize;
4829
0
        if (h->root.type == bfd_link_hash_warning)
4830
0
    {
4831
0
      entsize += htab->root.table.entsize;
4832
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4833
0
    }
4834
0
        if (h->root.type == bfd_link_hash_common)
4835
0
    entsize += sizeof (*h->root.u.c.p);
4836
0
      }
4837
0
  }
4838
4839
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4840
0
      old_tab = bfd_malloc (tabsize + entsize);
4841
0
      if (old_tab == NULL)
4842
0
  goto error_free_vers;
4843
4844
      /* Remember the current objalloc pointer, so that all mem for
4845
   symbols added can later be reclaimed.  */
4846
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4847
0
      if (alloc_mark == NULL)
4848
0
  goto error_free_vers;
4849
4850
      /* Make a special call to the linker "notice" function to
4851
   tell it that we are about to handle an as-needed lib.  */
4852
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4853
0
  goto error_free_vers;
4854
4855
      /* Clone the symbol table.  Remember some pointers into the
4856
   symbol table, and dynamic symbol count.  */
4857
0
      old_ent = (char *) old_tab + tabsize;
4858
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4859
0
      old_undefs = htab->root.undefs;
4860
0
      old_undefs_tail = htab->root.undefs_tail;
4861
0
      old_table = htab->root.table.table;
4862
0
      old_size = htab->root.table.size;
4863
0
      old_count = htab->root.table.count;
4864
0
      old_strtab = NULL;
4865
0
      if (htab->dynstr != NULL)
4866
0
  {
4867
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4868
0
    if (old_strtab == NULL)
4869
0
      goto error_free_vers;
4870
0
  }
4871
4872
0
      for (i = 0; i < htab->root.table.size; i++)
4873
0
  {
4874
0
    struct bfd_hash_entry *p;
4875
0
    struct elf_link_hash_entry *h;
4876
4877
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4878
0
      {
4879
0
        h = (struct elf_link_hash_entry *) p;
4880
0
        memcpy (old_ent, h, htab->root.table.entsize);
4881
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
4882
0
        if (h->root.type == bfd_link_hash_warning)
4883
0
    {
4884
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4885
0
      memcpy (old_ent, h, htab->root.table.entsize);
4886
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
4887
0
    }
4888
0
        if (h->root.type == bfd_link_hash_common)
4889
0
    {
4890
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4891
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4892
0
    }
4893
0
      }
4894
0
  }
4895
0
    }
4896
4897
0
  weaks = NULL;
4898
0
  if (extversym == NULL)
4899
0
    ever = NULL;
4900
0
  else if (extversym + extsymoff < extversym_end)
4901
0
    ever = extversym + extsymoff;
4902
0
  else
4903
0
    {
4904
      /* xgettext:c-format */
4905
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4906
0
        abfd, (long) extsymoff,
4907
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
4908
0
      bfd_set_error (bfd_error_bad_value);
4909
0
      goto error_free_vers;
4910
0
    }
4911
4912
0
  if (!bfd_link_relocatable (info)
4913
0
      && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4914
0
    {
4915
0
      _bfd_error_handler
4916
0
  (_("%pB: plugin needed to handle lto object"), abfd);
4917
0
    }
4918
4919
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4920
0
       isym < isymend;
4921
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4922
0
    {
4923
0
      int bind;
4924
0
      bfd_vma value;
4925
0
      asection *sec, *new_sec;
4926
0
      flagword flags;
4927
0
      const char *name;
4928
0
      bool must_copy_name = false;
4929
0
      struct elf_link_hash_entry *h;
4930
0
      struct elf_link_hash_entry *hi;
4931
0
      bool definition;
4932
0
      bool size_change_ok;
4933
0
      bool type_change_ok;
4934
0
      bool new_weak;
4935
0
      bool old_weak;
4936
0
      bfd *override;
4937
0
      bool common;
4938
0
      bool discarded;
4939
0
      unsigned int old_alignment;
4940
0
      unsigned int shindex;
4941
0
      bfd *old_bfd;
4942
0
      bool matched;
4943
4944
0
      override = NULL;
4945
4946
0
      flags = BSF_NO_FLAGS;
4947
0
      sec = NULL;
4948
0
      value = isym->st_value;
4949
0
      common = bed->common_definition (isym);
4950
0
      if (common && info->inhibit_common_definition)
4951
0
  {
4952
    /* Treat common symbol as undefined for --no-define-common.  */
4953
0
    isym->st_shndx = SHN_UNDEF;
4954
0
    common = false;
4955
0
  }
4956
0
      discarded = false;
4957
4958
0
      bind = ELF_ST_BIND (isym->st_info);
4959
0
      switch (bind)
4960
0
  {
4961
0
  case STB_LOCAL:
4962
    /* This should be impossible, since ELF requires that all
4963
       global symbols follow all local symbols, and that sh_info
4964
       point to the first global symbol.  Unfortunately, Irix 5
4965
       screws this up.  */
4966
0
    if (elf_bad_symtab (abfd))
4967
0
      continue;
4968
4969
    /* If we aren't prepared to handle locals within the globals
4970
       then we'll likely segfault on a NULL symbol hash if the
4971
       symbol is ever referenced in relocations.  */
4972
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
4973
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4974
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4975
0
        " (>= sh_info of %lu)"),
4976
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
4977
0
            (long) extsymoff);
4978
4979
    /* Dynamic object relocations are not processed by ld, so
4980
       ld won't run into the problem mentioned above.  */
4981
0
    if (dynamic)
4982
0
      continue;
4983
0
    bfd_set_error (bfd_error_bad_value);
4984
0
    goto error_free_vers;
4985
4986
0
  case STB_GLOBAL:
4987
0
    if (isym->st_shndx != SHN_UNDEF && !common)
4988
0
      flags = BSF_GLOBAL;
4989
0
    break;
4990
4991
0
  case STB_WEAK:
4992
0
    flags = BSF_WEAK;
4993
0
    break;
4994
4995
0
  case STB_GNU_UNIQUE:
4996
0
    flags = BSF_GNU_UNIQUE;
4997
0
    break;
4998
4999
0
  default:
5000
    /* Leave it up to the processor backend.  */
5001
0
    break;
5002
0
  }
5003
5004
0
      if (isym->st_shndx == SHN_UNDEF)
5005
0
  sec = bfd_und_section_ptr;
5006
0
      else if (isym->st_shndx == SHN_ABS)
5007
0
  sec = bfd_abs_section_ptr;
5008
0
      else if (isym->st_shndx == SHN_COMMON)
5009
0
  {
5010
0
    sec = bfd_com_section_ptr;
5011
    /* What ELF calls the size we call the value.  What ELF
5012
       calls the value we call the alignment.  */
5013
0
    value = isym->st_size;
5014
0
  }
5015
0
      else
5016
0
  {
5017
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5018
0
    if (sec == NULL)
5019
0
      sec = bfd_abs_section_ptr;
5020
0
    else if (discarded_section (sec))
5021
0
      {
5022
        /* Symbols from discarded section are undefined.  We keep
5023
     its visibility.  */
5024
0
        sec = bfd_und_section_ptr;
5025
0
        discarded = true;
5026
0
        isym->st_shndx = SHN_UNDEF;
5027
0
      }
5028
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5029
0
      value -= sec->vma;
5030
0
  }
5031
5032
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5033
0
                isym->st_name);
5034
0
      if (name == NULL)
5035
0
  goto error_free_vers;
5036
5037
0
      if (isym->st_shndx == SHN_COMMON
5038
0
    && (abfd->flags & BFD_PLUGIN) != 0)
5039
0
  {
5040
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5041
5042
0
    if (xc == NULL)
5043
0
      {
5044
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5045
0
         | SEC_EXCLUDE);
5046
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5047
0
        if (xc == NULL)
5048
0
    goto error_free_vers;
5049
0
      }
5050
0
    sec = xc;
5051
0
  }
5052
0
      else if (isym->st_shndx == SHN_COMMON
5053
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
5054
0
         && !bfd_link_relocatable (info))
5055
0
  {
5056
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5057
5058
0
    if (tcomm == NULL)
5059
0
      {
5060
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5061
0
         | SEC_LINKER_CREATED);
5062
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5063
0
        if (tcomm == NULL)
5064
0
    goto error_free_vers;
5065
0
      }
5066
0
    sec = tcomm;
5067
0
  }
5068
0
      else if (bed->elf_add_symbol_hook)
5069
0
  {
5070
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5071
0
               &sec, &value))
5072
0
      goto error_free_vers;
5073
5074
    /* The hook function sets the name to NULL if this symbol
5075
       should be skipped for some reason.  */
5076
0
    if (name == NULL)
5077
0
      continue;
5078
0
  }
5079
5080
      /* Sanity check that all possibilities were handled.  */
5081
0
      if (sec == NULL)
5082
0
  abort ();
5083
5084
      /* Silently discard TLS symbols from --just-syms.  There's
5085
   no way to combine a static TLS block with a new TLS block
5086
   for this executable.  */
5087
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5088
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5089
0
  continue;
5090
5091
0
      if (bfd_is_und_section (sec)
5092
0
    || bfd_is_com_section (sec))
5093
0
  definition = false;
5094
0
      else
5095
0
  definition = true;
5096
5097
0
      size_change_ok = false;
5098
0
      type_change_ok = bed->type_change_ok;
5099
0
      old_weak = false;
5100
0
      matched = false;
5101
0
      old_alignment = 0;
5102
0
      old_bfd = NULL;
5103
0
      new_sec = sec;
5104
5105
0
      if (is_elf_hash_table (&htab->root))
5106
0
  {
5107
0
    Elf_Internal_Versym iver;
5108
0
    unsigned int vernum = 0;
5109
0
    bool skip;
5110
5111
0
    if (ever == NULL)
5112
0
      {
5113
0
        if (info->default_imported_symver)
5114
    /* Use the default symbol version created earlier.  */
5115
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
5116
0
        else
5117
0
    iver.vs_vers = 0;
5118
0
      }
5119
0
    else if (ever >= extversym_end)
5120
0
      {
5121
        /* xgettext:c-format */
5122
0
        _bfd_error_handler (_("%pB: not enough version information"),
5123
0
          abfd);
5124
0
        bfd_set_error (bfd_error_bad_value);
5125
0
        goto error_free_vers;
5126
0
      }
5127
0
    else
5128
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
5129
5130
0
    vernum = iver.vs_vers & VERSYM_VERSION;
5131
5132
    /* If this is a hidden symbol, or if it is not version
5133
       1, we append the version name to the symbol name.
5134
       However, we do not modify a non-hidden absolute symbol
5135
       if it is not a function, because it might be the version
5136
       symbol itself.  FIXME: What if it isn't?  */
5137
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5138
0
        || (vernum > 1
5139
0
      && (!bfd_is_abs_section (sec)
5140
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5141
0
      {
5142
0
        const char *verstr;
5143
0
        size_t namelen, verlen, newlen;
5144
0
        char *newname, *p;
5145
5146
0
        if (isym->st_shndx != SHN_UNDEF)
5147
0
    {
5148
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5149
0
        verstr = NULL;
5150
0
      else if (vernum > 1)
5151
0
        verstr =
5152
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5153
0
      else
5154
0
        verstr = "";
5155
5156
0
      if (verstr == NULL)
5157
0
        {
5158
0
          _bfd_error_handler
5159
      /* xgettext:c-format */
5160
0
      (_("%pB: %s: invalid version %u (max %d)"),
5161
0
       abfd, name, vernum,
5162
0
       elf_tdata (abfd)->cverdefs);
5163
0
          bfd_set_error (bfd_error_bad_value);
5164
0
          goto error_free_vers;
5165
0
        }
5166
0
    }
5167
0
        else
5168
0
    {
5169
      /* We cannot simply test for the number of
5170
         entries in the VERNEED section since the
5171
         numbers for the needed versions do not start
5172
         at 0.  */
5173
0
      Elf_Internal_Verneed *t;
5174
5175
0
      verstr = NULL;
5176
0
      for (t = elf_tdata (abfd)->verref;
5177
0
           t != NULL;
5178
0
           t = t->vn_nextref)
5179
0
        {
5180
0
          Elf_Internal_Vernaux *a;
5181
5182
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5183
0
      {
5184
0
        if (a->vna_other == vernum)
5185
0
          {
5186
0
            verstr = a->vna_nodename;
5187
0
            break;
5188
0
          }
5189
0
      }
5190
0
          if (a != NULL)
5191
0
      break;
5192
0
        }
5193
0
      if (verstr == NULL)
5194
0
        {
5195
0
          _bfd_error_handler
5196
      /* xgettext:c-format */
5197
0
      (_("%pB: %s: invalid needed version %d"),
5198
0
       abfd, name, vernum);
5199
0
          bfd_set_error (bfd_error_bad_value);
5200
0
          goto error_free_vers;
5201
0
        }
5202
0
    }
5203
5204
0
        namelen = strlen (name);
5205
0
        verlen = strlen (verstr);
5206
0
        newlen = namelen + verlen + 2;
5207
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5208
0
      && isym->st_shndx != SHN_UNDEF)
5209
0
    ++newlen;
5210
5211
0
        newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5212
0
        if (newname == NULL)
5213
0
    goto error_free_vers;
5214
0
        memcpy (newname, name, namelen);
5215
0
        p = newname + namelen;
5216
0
        *p++ = ELF_VER_CHR;
5217
        /* If this is a defined non-hidden version symbol,
5218
     we add another @ to the name.  This indicates the
5219
     default version of the symbol.  */
5220
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5221
0
      && isym->st_shndx != SHN_UNDEF)
5222
0
    *p++ = ELF_VER_CHR;
5223
0
        memcpy (p, verstr, verlen + 1);
5224
5225
0
        name = newname;
5226
        /* Since bfd_hash_alloc is used for "name", the string
5227
     must be copied if added to first_hash.  The string
5228
     memory can be freed when an --as-needed library is
5229
     not needed.  */
5230
0
        must_copy_name = true;
5231
0
      }
5232
5233
    /* If this symbol has default visibility and the user has
5234
       requested we not re-export it, then mark it as hidden.  */
5235
0
    if (!bfd_is_und_section (sec)
5236
0
        && !dynamic
5237
0
        && abfd->no_export
5238
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5239
0
      isym->st_other = (STV_HIDDEN
5240
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5241
5242
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5243
0
              sym_hash, &old_bfd, &old_weak,
5244
0
              &old_alignment, &skip, &override,
5245
0
              &type_change_ok, &size_change_ok,
5246
0
              &matched))
5247
0
      goto error_free_vers;
5248
5249
0
    if (skip)
5250
0
      continue;
5251
5252
0
    h = *sym_hash;
5253
0
    while (h->root.type == bfd_link_hash_indirect
5254
0
     || h->root.type == bfd_link_hash_warning)
5255
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5256
5257
    /* Override a definition only if the new symbol matches the
5258
       existing one.  */
5259
0
    if (override && matched)
5260
0
      {
5261
0
        definition = false;
5262
0
        if (htab->first_hash != NULL
5263
0
      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5264
0
      && h->root.non_ir_ref_regular)
5265
0
    {
5266
      /* When reloading --as-needed shared objects for new
5267
         symbols added from IR inputs, if this shared object
5268
         has the first definition, use it.  */
5269
0
      struct elf_link_first_hash_entry *e
5270
0
        = ((struct elf_link_first_hash_entry *)
5271
0
           bfd_hash_lookup (htab->first_hash, name, false,
5272
0
          false));
5273
0
      if (e != NULL && e->abfd == abfd)
5274
0
        definition = true;
5275
0
    }
5276
0
      }
5277
5278
0
    if (h->versioned != unversioned
5279
0
        && elf_tdata (abfd)->verdef != NULL
5280
0
        && vernum > 1
5281
0
        && definition)
5282
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5283
0
  }
5284
5285
0
      if (! (_bfd_generic_link_add_one_symbol
5286
0
       (info, override ? override : abfd, name, flags, sec, value,
5287
0
        NULL, false, bed->collect,
5288
0
        (struct bfd_link_hash_entry **) sym_hash)))
5289
0
  goto error_free_vers;
5290
5291
0
      h = *sym_hash;
5292
      /* We need to make sure that indirect symbol dynamic flags are
5293
   updated.  */
5294
0
      hi = h;
5295
0
      while (h->root.type == bfd_link_hash_indirect
5296
0
       || h->root.type == bfd_link_hash_warning)
5297
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5298
5299
0
      *sym_hash = h;
5300
5301
      /* Setting the index to -3 tells elf_link_output_extsym that
5302
   this symbol is defined in a discarded section.  */
5303
0
      if (discarded && is_elf_hash_table (&htab->root))
5304
0
  h->indx = -3;
5305
5306
0
      new_weak = (flags & BSF_WEAK) != 0;
5307
0
      if (dynamic
5308
0
    && definition
5309
0
    && new_weak
5310
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5311
0
    && is_elf_hash_table (&htab->root)
5312
0
    && h->u.alias == NULL)
5313
0
  {
5314
    /* Keep a list of all weak defined non function symbols from
5315
       a dynamic object, using the alias field.  Later in this
5316
       function we will set the alias field to the correct
5317
       value.  We only put non-function symbols from dynamic
5318
       objects on this list, because that happens to be the only
5319
       time we need to know the normal symbol corresponding to a
5320
       weak symbol, and the information is time consuming to
5321
       figure out.  If the alias field is not already NULL,
5322
       then this symbol was already defined by some previous
5323
       dynamic object, and we will be using that previous
5324
       definition anyhow.  */
5325
5326
0
    h->u.alias = weaks;
5327
0
    weaks = h;
5328
0
  }
5329
5330
      /* Set the alignment of a common symbol.  */
5331
0
      if ((common || bfd_is_com_section (sec))
5332
0
    && h->root.type == bfd_link_hash_common)
5333
0
  {
5334
0
    unsigned int align;
5335
5336
0
    if (common)
5337
0
      align = bfd_log2 (isym->st_value);
5338
0
    else
5339
0
      {
5340
        /* The new symbol is a common symbol in a shared object.
5341
     We need to get the alignment from the section.  */
5342
0
        align = new_sec->alignment_power;
5343
0
      }
5344
0
    if (align > old_alignment)
5345
0
      h->root.u.c.p->alignment_power = align;
5346
0
    else
5347
0
      h->root.u.c.p->alignment_power = old_alignment;
5348
0
  }
5349
5350
0
      if (is_elf_hash_table (&htab->root))
5351
0
  {
5352
    /* Set a flag in the hash table entry indicating the type of
5353
       reference or definition we just found.  A dynamic symbol
5354
       is one which is referenced or defined by both a regular
5355
       object and a shared object.  */
5356
0
    bool dynsym = false;
5357
5358
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5359
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5360
0
      {
5361
        /* Except for this flag to track nonweak references.  */
5362
0
        if (!definition
5363
0
      && bind != STB_WEAK)
5364
0
    h->ref_ir_nonweak = 1;
5365
0
      }
5366
0
    else if (!dynamic)
5367
0
      {
5368
0
        if (! definition)
5369
0
    {
5370
0
      h->ref_regular = 1;
5371
0
      if (bind != STB_WEAK)
5372
0
        h->ref_regular_nonweak = 1;
5373
0
    }
5374
0
        else
5375
0
    {
5376
0
      h->def_regular = 1;
5377
0
      if (h->def_dynamic)
5378
0
        {
5379
0
          h->def_dynamic = 0;
5380
0
          h->ref_dynamic = 1;
5381
0
        }
5382
0
    }
5383
0
      }
5384
0
    else
5385
0
      {
5386
0
        if (! definition)
5387
0
    {
5388
0
      h->ref_dynamic = 1;
5389
0
      hi->ref_dynamic = 1;
5390
0
    }
5391
0
        else
5392
0
    {
5393
0
      h->def_dynamic = 1;
5394
0
      hi->def_dynamic = 1;
5395
0
    }
5396
0
      }
5397
5398
    /* If an indirect symbol has been forced local, don't
5399
       make the real symbol dynamic.  */
5400
0
    if (h != hi && hi->forced_local)
5401
0
      ;
5402
0
    else if (!dynamic)
5403
0
      {
5404
0
        if (bfd_link_dll (info)
5405
0
      || h->def_dynamic
5406
0
      || h->ref_dynamic)
5407
0
    dynsym = true;
5408
0
      }
5409
0
    else
5410
0
      {
5411
0
        if (h->def_regular
5412
0
      || h->ref_regular
5413
0
      || (h->is_weakalias
5414
0
          && weakdef (h)->dynindx != -1))
5415
0
    dynsym = true;
5416
0
      }
5417
5418
    /* Check to see if we need to add an indirect symbol for
5419
       the default name.  */
5420
0
    if ((definition
5421
0
         || (!override && h->root.type == bfd_link_hash_common))
5422
0
        && !(hi != h
5423
0
       && hi->versioned == versioned_hidden))
5424
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5425
0
                sec, value, &old_bfd, &dynsym))
5426
0
        goto error_free_vers;
5427
5428
    /* Check the alignment when a common symbol is involved. This
5429
       can change when a common symbol is overridden by a normal
5430
       definition or a common symbol is ignored due to the old
5431
       normal definition. We need to make sure the maximum
5432
       alignment is maintained.  */
5433
0
    if ((old_alignment || common)
5434
0
        && h->root.type != bfd_link_hash_common)
5435
0
      {
5436
0
        unsigned int common_align;
5437
0
        unsigned int normal_align;
5438
0
        unsigned int symbol_align;
5439
0
        bfd *normal_bfd;
5440
0
        bfd *common_bfd;
5441
5442
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5443
0
        || h->root.type == bfd_link_hash_defweak);
5444
5445
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5446
0
        if (h->root.u.def.section->owner != NULL
5447
0
      && (h->root.u.def.section->owner->flags
5448
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5449
0
    {
5450
0
      normal_align = h->root.u.def.section->alignment_power;
5451
0
      if (normal_align > symbol_align)
5452
0
        normal_align = symbol_align;
5453
0
    }
5454
0
        else
5455
0
    normal_align = symbol_align;
5456
5457
0
        if (old_alignment)
5458
0
    {
5459
0
      common_align = old_alignment;
5460
0
      common_bfd = old_bfd;
5461
0
      normal_bfd = abfd;
5462
0
    }
5463
0
        else
5464
0
    {
5465
0
      common_align = bfd_log2 (isym->st_value);
5466
0
      common_bfd = abfd;
5467
0
      normal_bfd = old_bfd;
5468
0
    }
5469
5470
0
        if (normal_align < common_align)
5471
0
    {
5472
      /* PR binutils/2735 */
5473
0
      if (normal_bfd == NULL)
5474
0
        _bfd_error_handler
5475
          /* xgettext:c-format */
5476
0
          (_("warning: alignment %u of common symbol `%s' in %pB is"
5477
0
       " greater than the alignment (%u) of its section %pA"),
5478
0
           1 << common_align, name, common_bfd,
5479
0
           1 << normal_align, h->root.u.def.section);
5480
0
      else
5481
0
        _bfd_error_handler
5482
          /* xgettext:c-format */
5483
0
          (_("warning: alignment %u of normal symbol `%s' in %pB"
5484
0
       " is smaller than %u used by the common definition in %pB"),
5485
0
           1 << normal_align, name, normal_bfd,
5486
0
           1 << common_align, common_bfd);
5487
5488
      /* PR 30499: make sure that users understand that this warning is serious.  */
5489
0
      _bfd_error_handler
5490
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5491
0
    }
5492
0
      }
5493
5494
    /* Remember the symbol size if it isn't undefined.  */
5495
0
    if (isym->st_size != 0
5496
0
        && isym->st_shndx != SHN_UNDEF
5497
0
        && (definition || h->size == 0))
5498
0
      {
5499
0
        if (h->size != 0
5500
0
      && h->size != isym->st_size
5501
0
      && ! size_change_ok)
5502
0
    {
5503
0
      _bfd_error_handler
5504
        /* xgettext:c-format */
5505
0
        (_("warning: size of symbol `%s' changed"
5506
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5507
0
         name, (uint64_t) h->size, old_bfd,
5508
0
         (uint64_t) isym->st_size, abfd);
5509
5510
      /* PR 30499: make sure that users understand that this warning is serious.  */
5511
0
      _bfd_error_handler
5512
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5513
0
    }
5514
5515
0
        h->size = isym->st_size;
5516
0
      }
5517
5518
    /* If this is a common symbol, then we always want H->SIZE
5519
       to be the size of the common symbol.  The code just above
5520
       won't fix the size if a common symbol becomes larger.  We
5521
       don't warn about a size change here, because that is
5522
       covered by --warn-common.  Allow changes between different
5523
       function types.  */
5524
0
    if (h->root.type == bfd_link_hash_common)
5525
0
      h->size = h->root.u.c.size;
5526
5527
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5528
0
        && ((definition && !new_weak)
5529
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5530
0
      || h->type == STT_NOTYPE))
5531
0
      {
5532
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5533
5534
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5535
     symbol.  */
5536
0
        if (type == STT_GNU_IFUNC
5537
0
      && (abfd->flags & DYNAMIC) != 0)
5538
0
    type = STT_FUNC;
5539
5540
0
        if (h->type != type)
5541
0
    {
5542
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5543
        /* xgettext:c-format */
5544
0
        _bfd_error_handler
5545
0
          (_("warning: type of symbol `%s' changed"
5546
0
       " from %d to %d in %pB"),
5547
0
           name, h->type, type, abfd);
5548
5549
0
      h->type = type;
5550
0
    }
5551
0
      }
5552
5553
    /* Merge st_other field.  */
5554
0
    elf_merge_st_other (abfd, h, isym->st_other, sec,
5555
0
            definition, dynamic);
5556
5557
    /* We don't want to make debug symbol dynamic.  */
5558
0
    if (definition
5559
0
        && (sec->flags & SEC_DEBUGGING)
5560
0
        && !bfd_link_relocatable (info))
5561
0
      dynsym = false;
5562
5563
    /* Nor should we make plugin symbols dynamic.  */
5564
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5565
0
      dynsym = false;
5566
5567
0
    if (definition)
5568
0
      {
5569
0
        h->target_internal = isym->st_target_internal;
5570
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5571
0
      }
5572
5573
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5574
       in IR.  Since all data or text symbols in IR have the
5575
       same type, value and section, we can't tell if a symbol
5576
       is an alias of another symbol by their types, values and
5577
       sections.  */
5578
0
    if (definition
5579
0
        && !dynamic
5580
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5581
0
      {
5582
0
        char *p = strchr (name, ELF_VER_CHR);
5583
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5584
0
    {
5585
      /* Queue non-default versions so that .symver x, x@FOO
5586
         aliases can be checked.  */
5587
0
      if (!nondeflt_vers)
5588
0
        {
5589
0
          size_t amt = ((isymend - isym + 1)
5590
0
            * sizeof (struct elf_link_hash_entry *));
5591
0
          nondeflt_vers
5592
0
      = (struct elf_link_hash_entry **) bfd_malloc (amt);
5593
0
          if (!nondeflt_vers)
5594
0
      goto error_free_vers;
5595
0
        }
5596
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5597
0
    }
5598
0
      }
5599
5600
0
    if (dynsym && h->dynindx == -1)
5601
0
      {
5602
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5603
0
    goto error_free_vers;
5604
0
        if (h->is_weakalias
5605
0
      && weakdef (h)->dynindx == -1)
5606
0
    {
5607
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5608
0
        goto error_free_vers;
5609
0
    }
5610
0
      }
5611
0
    else if (h->dynindx != -1)
5612
      /* If the symbol already has a dynamic index, but
5613
         visibility says it should not be visible, turn it into
5614
         a local symbol.  */
5615
0
      switch (ELF_ST_VISIBILITY (h->other))
5616
0
        {
5617
0
        case STV_INTERNAL:
5618
0
        case STV_HIDDEN:
5619
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
5620
0
    dynsym = false;
5621
0
    break;
5622
0
        }
5623
5624
0
    if (!add_needed
5625
0
        && matched
5626
0
        && definition
5627
0
        && h->root.type != bfd_link_hash_indirect)
5628
0
      {
5629
0
        if ((dynsym
5630
0
       && h->ref_regular_nonweak)
5631
0
      || (old_bfd != NULL
5632
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5633
0
          && h->ref_ir_nonweak
5634
0
          && !info->lto_all_symbols_read)
5635
0
      || (h->ref_dynamic_nonweak
5636
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5637
0
          && !on_needed_list (elf_dt_name (abfd),
5638
0
            htab->needed, NULL)))
5639
0
    {
5640
0
      const char *soname = elf_dt_name (abfd);
5641
5642
0
      info->callbacks->minfo ("%!", soname, old_bfd,
5643
0
            h->root.root.string);
5644
5645
      /* A symbol from a library loaded via DT_NEEDED of some
5646
         other library is referenced by a regular object.
5647
         Add a DT_NEEDED entry for it.  Issue an error if
5648
         --no-add-needed is used and the reference was not
5649
         a weak one.  */
5650
0
      if (old_bfd != NULL
5651
0
          && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5652
0
        {
5653
0
          _bfd_error_handler
5654
      /* xgettext:c-format */
5655
0
      (_("%pB: undefined reference to symbol '%s'"),
5656
0
       old_bfd, name);
5657
0
          bfd_set_error (bfd_error_missing_dso);
5658
0
          goto error_free_vers;
5659
0
        }
5660
5661
0
      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5662
0
        (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5663
5664
      /* Create dynamic sections for backends that require
5665
         that be done before setup_gnu_properties.  */
5666
0
      if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5667
0
        return false;
5668
0
      add_needed = true;
5669
0
    }
5670
0
        else if (dynamic
5671
0
           && h->root.u.def.section->owner == abfd)
5672
    /* Add this symbol to first hash if this shared
5673
       object has the first definition.  */
5674
0
    elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5675
0
      }
5676
0
  }
5677
0
    }
5678
5679
0
  if (info->lto_plugin_active
5680
0
      && !bfd_link_relocatable (info)
5681
0
      && (abfd->flags & BFD_PLUGIN) == 0
5682
0
      && !just_syms
5683
0
      && extsymcount)
5684
0
    {
5685
0
      int r_sym_shift;
5686
5687
0
      if (bed->s->arch_size == 32)
5688
0
  r_sym_shift = 8;
5689
0
      else
5690
0
  r_sym_shift = 32;
5691
5692
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5693
   referenced in regular objects so that linker plugin will get
5694
   the correct symbol resolution.  */
5695
5696
0
      sym_hash = elf_sym_hashes (abfd);
5697
0
      for (s = abfd->sections; s != NULL; s = s->next)
5698
0
  {
5699
0
    Elf_Internal_Rela *internal_relocs;
5700
0
    Elf_Internal_Rela *rel, *relend;
5701
5702
    /* Don't check relocations in excluded sections.  */
5703
0
    if ((s->flags & SEC_RELOC) == 0
5704
0
        || s->reloc_count == 0
5705
0
        || (s->flags & SEC_EXCLUDE) != 0
5706
0
        || ((info->strip == strip_all
5707
0
       || info->strip == strip_debugger)
5708
0
      && (s->flags & SEC_DEBUGGING) != 0))
5709
0
      continue;
5710
5711
0
    internal_relocs = _bfd_elf_link_info_read_relocs
5712
0
      (abfd, info, s, NULL, NULL,
5713
0
       _bfd_elf_link_keep_memory (info));
5714
0
    if (internal_relocs == NULL)
5715
0
      goto error_free_vers;
5716
5717
0
    rel = internal_relocs;
5718
0
    relend = rel + s->reloc_count;
5719
0
    for ( ; rel < relend; rel++)
5720
0
      {
5721
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5722
0
        struct elf_link_hash_entry *h;
5723
5724
        /* Skip local symbols.  */
5725
0
        if (r_symndx < extsymoff)
5726
0
    continue;
5727
5728
0
        h = sym_hash[r_symndx - extsymoff];
5729
0
        if (h != NULL)
5730
0
    h->root.non_ir_ref_regular = 1;
5731
0
      }
5732
5733
0
    if (elf_section_data (s)->relocs != internal_relocs)
5734
0
      free (internal_relocs);
5735
0
  }
5736
0
    }
5737
5738
0
  free (extversym);
5739
0
  extversym = NULL;
5740
0
  free (isymbuf);
5741
0
  isymbuf = NULL;
5742
5743
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5744
0
    {
5745
0
      unsigned int i;
5746
5747
      /* Restore the symbol table.  */
5748
0
      old_ent = (char *) old_tab + tabsize;
5749
0
      memset (elf_sym_hashes (abfd), 0,
5750
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5751
0
      htab->root.table.table = old_table;
5752
0
      htab->root.table.size = old_size;
5753
0
      htab->root.table.count = old_count;
5754
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5755
0
      htab->root.undefs = old_undefs;
5756
0
      htab->root.undefs_tail = old_undefs_tail;
5757
0
      if (htab->dynstr != NULL)
5758
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5759
0
      free (old_strtab);
5760
0
      old_strtab = NULL;
5761
0
      for (i = 0; i < htab->root.table.size; i++)
5762
0
  {
5763
0
    struct bfd_hash_entry *p;
5764
0
    struct elf_link_hash_entry *h;
5765
0
    unsigned int non_ir_ref_dynamic;
5766
5767
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5768
0
      {
5769
        /* Preserve non_ir_ref_dynamic so that this symbol
5770
     will be exported when the dynamic lib becomes needed
5771
     in the second pass.  */
5772
0
        h = (struct elf_link_hash_entry *) p;
5773
0
        if (h->root.type == bfd_link_hash_warning)
5774
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5775
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5776
5777
0
        h = (struct elf_link_hash_entry *) p;
5778
0
        memcpy (h, old_ent, htab->root.table.entsize);
5779
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5780
0
        if (h->root.type == bfd_link_hash_warning)
5781
0
    {
5782
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5783
0
      memcpy (h, old_ent, htab->root.table.entsize);
5784
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5785
0
    }
5786
0
        if (h->root.type == bfd_link_hash_common)
5787
0
    {
5788
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5789
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5790
0
    }
5791
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5792
0
      }
5793
0
  }
5794
5795
      /* Make a special call to the linker "notice" function to
5796
   tell it that symbols added for crefs may need to be removed.  */
5797
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5798
0
  goto error_free_vers;
5799
5800
0
      free (old_tab);
5801
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5802
0
         alloc_mark);
5803
0
      free (nondeflt_vers);
5804
0
      return true;
5805
0
    }
5806
5807
0
  if (old_tab != NULL)
5808
0
    {
5809
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5810
0
  goto error_free_vers;
5811
0
      free (old_tab);
5812
0
      old_tab = NULL;
5813
0
    }
5814
5815
  /* Now that all the symbols from this input file are created, if
5816
     not performing a relocatable link, handle .symver foo, foo@BAR
5817
     such that any relocs against foo become foo@BAR.  */
5818
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5819
0
    {
5820
0
      size_t cnt, symidx;
5821
5822
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5823
0
  {
5824
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5825
0
    char *shortname, *p;
5826
0
    size_t amt;
5827
5828
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5829
0
    if (p == NULL
5830
0
        || (h->root.type != bfd_link_hash_defined
5831
0
      && h->root.type != bfd_link_hash_defweak))
5832
0
      continue;
5833
5834
0
    amt = p - h->root.root.string;
5835
0
    shortname = (char *) bfd_malloc (amt + 1);
5836
0
    if (!shortname)
5837
0
      goto error_free_vers;
5838
0
    memcpy (shortname, h->root.root.string, amt);
5839
0
    shortname[amt] = '\0';
5840
5841
0
    hi = (struct elf_link_hash_entry *)
5842
0
         bfd_link_hash_lookup (&htab->root, shortname,
5843
0
             false, false, false);
5844
0
    if (hi != NULL
5845
0
        && hi->root.type == h->root.type
5846
0
        && hi->root.u.def.value == h->root.u.def.value
5847
0
        && hi->root.u.def.section == h->root.u.def.section)
5848
0
      {
5849
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
5850
0
        hi->root.type = bfd_link_hash_indirect;
5851
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5852
0
        (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5853
0
        sym_hash = elf_sym_hashes (abfd);
5854
0
        if (sym_hash)
5855
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
5856
0
      if (sym_hash[symidx] == hi)
5857
0
        {
5858
0
          sym_hash[symidx] = h;
5859
0
          break;
5860
0
        }
5861
0
      }
5862
0
    free (shortname);
5863
0
  }
5864
0
      free (nondeflt_vers);
5865
0
      nondeflt_vers = NULL;
5866
0
    }
5867
5868
  /* Now set the alias field correctly for all the weak defined
5869
     symbols we found.  The only way to do this is to search all the
5870
     symbols.  Since we only need the information for non functions in
5871
     dynamic objects, that's the only time we actually put anything on
5872
     the list WEAKS.  We need this information so that if a regular
5873
     object refers to a symbol defined weakly in a dynamic object, the
5874
     real symbol in the dynamic object is also put in the dynamic
5875
     symbols; we also must arrange for both symbols to point to the
5876
     same memory location.  We could handle the general case of symbol
5877
     aliasing, but a general symbol alias can only be generated in
5878
     assembler code, handling it correctly would be very time
5879
     consuming, and other ELF linkers don't handle general aliasing
5880
     either.  */
5881
0
  if (weaks != NULL)
5882
0
    {
5883
0
      struct elf_link_hash_entry **hpp;
5884
0
      struct elf_link_hash_entry **hppend;
5885
0
      struct elf_link_hash_entry **sorted_sym_hash;
5886
0
      struct elf_link_hash_entry *h;
5887
0
      size_t sym_count, amt;
5888
5889
      /* Since we have to search the whole symbol list for each weak
5890
   defined symbol, search time for N weak defined symbols will be
5891
   O(N^2). Binary search will cut it down to O(NlogN).  */
5892
0
      amt = extsymcount * sizeof (*sorted_sym_hash);
5893
0
      sorted_sym_hash = bfd_malloc (amt);
5894
0
      if (sorted_sym_hash == NULL)
5895
0
  goto error_return;
5896
0
      sym_hash = sorted_sym_hash;
5897
0
      hpp = elf_sym_hashes (abfd);
5898
0
      hppend = hpp + extsymcount;
5899
0
      sym_count = 0;
5900
0
      for (; hpp < hppend; hpp++)
5901
0
  {
5902
0
    h = *hpp;
5903
0
    if (h != NULL
5904
0
        && h->root.type == bfd_link_hash_defined
5905
0
        && !bed->is_function_type (h->type))
5906
0
      {
5907
0
        *sym_hash = h;
5908
0
        sym_hash++;
5909
0
        sym_count++;
5910
0
      }
5911
0
  }
5912
5913
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5914
0
       elf_sort_symbol);
5915
5916
0
      while (weaks != NULL)
5917
0
  {
5918
0
    struct elf_link_hash_entry *hlook;
5919
0
    asection *slook;
5920
0
    bfd_vma vlook;
5921
0
    size_t i, j, idx = 0;
5922
5923
0
    hlook = weaks;
5924
0
    weaks = hlook->u.alias;
5925
0
    hlook->u.alias = NULL;
5926
5927
0
    if (hlook->root.type != bfd_link_hash_defined
5928
0
        && hlook->root.type != bfd_link_hash_defweak)
5929
0
      continue;
5930
5931
0
    slook = hlook->root.u.def.section;
5932
0
    vlook = hlook->root.u.def.value;
5933
5934
0
    i = 0;
5935
0
    j = sym_count;
5936
0
    while (i != j)
5937
0
      {
5938
0
        bfd_signed_vma vdiff;
5939
0
        idx = (i + j) / 2;
5940
0
        h = sorted_sym_hash[idx];
5941
0
        vdiff = vlook - h->root.u.def.value;
5942
0
        if (vdiff < 0)
5943
0
    j = idx;
5944
0
        else if (vdiff > 0)
5945
0
    i = idx + 1;
5946
0
        else
5947
0
    {
5948
0
      int sdiff = slook->id - h->root.u.def.section->id;
5949
0
      if (sdiff < 0)
5950
0
        j = idx;
5951
0
      else if (sdiff > 0)
5952
0
        i = idx + 1;
5953
0
      else
5954
0
        break;
5955
0
    }
5956
0
      }
5957
5958
    /* We didn't find a value/section match.  */
5959
0
    if (i == j)
5960
0
      continue;
5961
5962
    /* With multiple aliases, or when the weak symbol is already
5963
       strongly defined, we have multiple matching symbols and
5964
       the binary search above may land on any of them.  Step
5965
       one past the matching symbol(s).  */
5966
0
    while (++idx != j)
5967
0
      {
5968
0
        h = sorted_sym_hash[idx];
5969
0
        if (h->root.u.def.section != slook
5970
0
      || h->root.u.def.value != vlook)
5971
0
    break;
5972
0
      }
5973
5974
    /* Now look back over the aliases.  Since we sorted by size
5975
       as well as value and section, we'll choose the one with
5976
       the largest size.  */
5977
0
    while (idx-- != i)
5978
0
      {
5979
0
        h = sorted_sym_hash[idx];
5980
5981
        /* Stop if value or section doesn't match.  */
5982
0
        if (h->root.u.def.section != slook
5983
0
      || h->root.u.def.value != vlook)
5984
0
    break;
5985
0
        else if (h != hlook)
5986
0
    {
5987
0
      struct elf_link_hash_entry *t;
5988
5989
0
      hlook->u.alias = h;
5990
0
      hlook->is_weakalias = 1;
5991
0
      t = h;
5992
0
      if (t->u.alias != NULL)
5993
0
        while (t->u.alias != h)
5994
0
          t = t->u.alias;
5995
0
      t->u.alias = hlook;
5996
5997
      /* If the weak definition is in the list of dynamic
5998
         symbols, make sure the real definition is put
5999
         there as well.  */
6000
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
6001
0
        {
6002
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6003
0
      {
6004
0
      err_free_sym_hash:
6005
0
        free (sorted_sym_hash);
6006
0
        goto error_return;
6007
0
      }
6008
0
        }
6009
6010
      /* If the real definition is in the list of dynamic
6011
         symbols, make sure the weak definition is put
6012
         there as well.  If we don't do this, then the
6013
         dynamic loader might not merge the entries for the
6014
         real definition and the weak definition.  */
6015
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
6016
0
        {
6017
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6018
0
      goto err_free_sym_hash;
6019
0
        }
6020
0
      break;
6021
0
    }
6022
0
      }
6023
0
  }
6024
6025
0
      free (sorted_sym_hash);
6026
0
    }
6027
6028
0
  if (bed->check_directives
6029
0
      && !(*bed->check_directives) (abfd, info))
6030
0
    return false;
6031
6032
  /* If this is a non-traditional link, try to optimize the handling
6033
     of the .stab/.stabstr sections.  */
6034
0
  if (! dynamic
6035
0
      && ! info->traditional_format
6036
0
      && is_elf_hash_table (&htab->root)
6037
0
      && (info->strip != strip_all && info->strip != strip_debugger))
6038
0
    {
6039
0
      asection *stabstr;
6040
6041
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6042
0
      if (stabstr != NULL)
6043
0
  {
6044
0
    bfd_size_type string_offset = 0;
6045
0
    asection *stab;
6046
6047
0
    for (stab = abfd->sections; stab; stab = stab->next)
6048
0
      if (startswith (stab->name, ".stab")
6049
0
    && (!stab->name[5] ||
6050
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6051
0
    && (stab->flags & SEC_MERGE) == 0
6052
0
    && !bfd_is_abs_section (stab->output_section))
6053
0
        {
6054
0
    struct bfd_elf_section_data *secdata;
6055
6056
0
    secdata = elf_section_data (stab);
6057
0
    if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6058
0
                 stabstr, &secdata->sec_info,
6059
0
                 &string_offset))
6060
0
      goto error_return;
6061
0
    if (secdata->sec_info)
6062
0
      stab->sec_info_type = SEC_INFO_TYPE_STABS;
6063
0
      }
6064
0
  }
6065
0
    }
6066
6067
0
  if (dynamic && add_needed)
6068
0
    {
6069
      /* Add this bfd to the loaded list.  */
6070
0
      struct elf_link_loaded_list *n;
6071
6072
0
      n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6073
0
      if (n == NULL)
6074
0
  goto error_return;
6075
0
      n->abfd = abfd;
6076
0
      n->next = htab->dyn_loaded;
6077
0
      htab->dyn_loaded = n;
6078
0
    }
6079
0
  if (dynamic && !add_needed
6080
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6081
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6082
6083
0
  return true;
6084
6085
0
 error_free_vers:
6086
0
  free (old_tab);
6087
0
  free (old_strtab);
6088
0
  free (nondeflt_vers);
6089
0
  free (extversym);
6090
0
 error_free_sym:
6091
0
  free (isymbuf);
6092
0
 error_return:
6093
0
  return false;
6094
0
}
6095
6096
/* Return the linker hash table entry of a symbol that might be
6097
   satisfied by an archive symbol.  Return -1 on error.  */
6098
6099
struct bfd_link_hash_entry *
6100
_bfd_elf_archive_symbol_lookup (bfd *abfd,
6101
        struct bfd_link_info *info,
6102
        const char *name)
6103
0
{
6104
0
  struct bfd_link_hash_entry *h;
6105
0
  char *p, *copy;
6106
0
  size_t len, first;
6107
6108
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6109
0
  if (h != NULL)
6110
0
    return h;
6111
6112
  /* If this is a default version (the name contains @@), look up the
6113
     symbol again with only one `@' as well as without the version.
6114
     The effect is that references to the symbol with and without the
6115
     version will be matched by the default symbol in the archive.  */
6116
6117
0
  p = strchr (name, ELF_VER_CHR);
6118
0
  if (p == NULL || p[1] != ELF_VER_CHR)
6119
0
    {
6120
      /* Add this symbol to first hash if this archive has the first
6121
   definition.  */
6122
0
      if (is_elf_hash_table (info->hash))
6123
0
  elf_link_add_to_first_hash (abfd, info, name, false);
6124
0
      return h;
6125
0
    }
6126
6127
  /* First check with only one `@'.  */
6128
0
  len = strlen (name);
6129
0
  copy = (char *) bfd_alloc (abfd, len);
6130
0
  if (copy == NULL)
6131
0
    return (struct bfd_link_hash_entry *) -1;
6132
6133
0
  first = p - name + 1;
6134
0
  memcpy (copy, name, first);
6135
0
  memcpy (copy + first, name + first + 1, len - first);
6136
6137
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6138
0
  if (h == NULL)
6139
0
    {
6140
      /* We also need to check references to the symbol without the
6141
   version.  */
6142
0
      copy[first - 1] = '\0';
6143
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6144
0
    }
6145
6146
0
  bfd_release (abfd, copy);
6147
0
  return h;
6148
0
}
6149
6150
/* Add symbols from an ELF archive file to the linker hash table.  We
6151
   don't use _bfd_generic_link_add_archive_symbols because we need to
6152
   handle versioned symbols.
6153
6154
   Fortunately, ELF archive handling is simpler than that done by
6155
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6156
   oddities.  In ELF, if we find a symbol in the archive map, and the
6157
   symbol is currently undefined, we know that we must pull in that
6158
   object file.
6159
6160
   Unfortunately, we do have to make multiple passes over the symbol
6161
   table until nothing further is resolved.  */
6162
6163
static bool
6164
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6165
0
{
6166
0
  symindex c;
6167
0
  unsigned char *included = NULL;
6168
0
  carsym *symdefs;
6169
0
  bool loop;
6170
0
  size_t amt;
6171
0
  const struct elf_backend_data *bed;
6172
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
6173
0
    (bfd *, struct bfd_link_info *, const char *);
6174
6175
0
  if (! bfd_has_map (abfd))
6176
0
    {
6177
      /* An empty archive is a special case.  */
6178
0
      if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6179
0
  return true;
6180
0
      bfd_set_error (bfd_error_no_armap);
6181
0
      return false;
6182
0
    }
6183
6184
  /* Keep track of all symbols we know to be already defined, and all
6185
     files we know to be already included.  This is to speed up the
6186
     second and subsequent passes.  */
6187
0
  c = bfd_ardata (abfd)->symdef_count;
6188
0
  if (c == 0)
6189
0
    return true;
6190
0
  amt = c * sizeof (*included);
6191
0
  included = (unsigned char *) bfd_zmalloc (amt);
6192
0
  if (included == NULL)
6193
0
    return false;
6194
6195
0
  symdefs = bfd_ardata (abfd)->symdefs;
6196
0
  bed = get_elf_backend_data (abfd);
6197
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6198
6199
0
  do
6200
0
    {
6201
0
      file_ptr last;
6202
0
      symindex i;
6203
0
      carsym *symdef;
6204
0
      carsym *symdefend;
6205
6206
0
      loop = false;
6207
0
      last = -1;
6208
6209
0
      symdef = symdefs;
6210
0
      symdefend = symdef + c;
6211
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6212
0
  {
6213
0
    struct bfd_link_hash_entry *h;
6214
0
    bfd *element;
6215
0
    struct bfd_link_hash_entry *undefs_tail;
6216
0
    symindex mark;
6217
6218
0
    if (included[i])
6219
0
      continue;
6220
0
    if (symdef->file_offset == last)
6221
0
      {
6222
0
        included[i] = true;
6223
0
        continue;
6224
0
      }
6225
6226
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6227
0
    if (h == (struct bfd_link_hash_entry *) -1)
6228
0
      goto error_return;
6229
6230
0
    if (h == NULL)
6231
0
      continue;
6232
6233
0
    if (h->type == bfd_link_hash_undefined)
6234
0
      {
6235
        /* If the archive element has already been loaded then one
6236
     of the symbols defined by that element might have been
6237
     made undefined due to being in a discarded section.  */
6238
0
        if (is_elf_hash_table (info->hash)
6239
0
      && ((struct elf_link_hash_entry *) h)->indx == -3)
6240
0
    continue;
6241
0
      }
6242
0
    else if (h->type == bfd_link_hash_common)
6243
0
      {
6244
        /* We currently have a common symbol.  The archive map contains
6245
     a reference to this symbol, so we may want to include it.  We
6246
     only want to include it however, if this archive element
6247
     contains a definition of the symbol, not just another common
6248
     declaration of it.
6249
6250
     Unfortunately some archivers (including GNU ar) will put
6251
     declarations of common symbols into their archive maps, as
6252
     well as real definitions, so we cannot just go by the archive
6253
     map alone.  Instead we must read in the element's symbol
6254
     table and check that to see what kind of symbol definition
6255
     this is.  */
6256
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6257
0
    continue;
6258
0
      }
6259
0
    else
6260
0
      {
6261
0
        if (h->type != bfd_link_hash_undefweak)
6262
    /* Symbol must be defined.  Don't check it again.  */
6263
0
    included[i] = true;
6264
6265
0
        if (!is_elf_hash_table (info->hash))
6266
0
    continue;
6267
0
        struct elf_link_hash_entry *eh
6268
0
    = (struct elf_link_hash_entry *) h;
6269
        /* Ignore the archive if the symbol isn't referenced by a
6270
     regular object or isn't defined in a shared object.  */
6271
0
        if (!eh->ref_regular || !eh->def_dynamic)
6272
0
    continue;
6273
        /* Ignore the dynamic definition if symbol is first
6274
     defined in this archive.  */
6275
0
        struct elf_link_hash_table *htab = elf_hash_table (info);
6276
0
        if (htab->first_hash == NULL)
6277
0
    continue;
6278
0
        struct elf_link_first_hash_entry *e
6279
0
    = ((struct elf_link_first_hash_entry *)
6280
0
       bfd_hash_lookup (htab->first_hash, symdef->name,
6281
0
            false, false));
6282
0
        if (e == NULL || e->abfd != abfd)
6283
0
    continue;
6284
0
      }
6285
6286
    /* We need to include this archive member.  */
6287
0
    element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6288
0
               info);
6289
0
    if (element == NULL)
6290
0
      goto error_return;
6291
6292
0
    if (! bfd_check_format (element, bfd_object))
6293
0
      goto error_return;
6294
6295
0
    undefs_tail = info->hash->undefs_tail;
6296
6297
0
    if (!(*info->callbacks
6298
0
    ->add_archive_element) (info, element, symdef->name, &element))
6299
0
      continue;
6300
0
    if (!bfd_link_add_symbols (element, info))
6301
0
      goto error_return;
6302
6303
    /* If there are any new undefined symbols, we need to make
6304
       another pass through the archive in order to see whether
6305
       they can be defined.  FIXME: This isn't perfect, because
6306
       common symbols wind up on undefs_tail and because an
6307
       undefined symbol which is defined later on in this pass
6308
       does not require another pass.  This isn't a bug, but it
6309
       does make the code less efficient than it could be.  */
6310
0
    if (undefs_tail != info->hash->undefs_tail)
6311
0
      loop = true;
6312
6313
    /* Look backward to mark all symbols from this object file
6314
       which we have already seen in this pass.  */
6315
0
    mark = i;
6316
0
    do
6317
0
      {
6318
0
        included[mark] = true;
6319
0
        if (mark == 0)
6320
0
    break;
6321
0
        --mark;
6322
0
      }
6323
0
    while (symdefs[mark].file_offset == symdef->file_offset);
6324
6325
    /* We mark subsequent symbols from this object file as we go
6326
       on through the loop.  */
6327
0
    last = symdef->file_offset;
6328
0
  }
6329
0
    }
6330
0
  while (loop);
6331
6332
0
  free (included);
6333
0
  return true;
6334
6335
0
 error_return:
6336
0
  free (included);
6337
0
  return false;
6338
0
}
6339
6340
/* Given an ELF BFD, add symbols to the global hash table as
6341
   appropriate.  */
6342
6343
bool
6344
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6345
0
{
6346
0
  switch (bfd_get_format (abfd))
6347
0
    {
6348
0
    case bfd_object:
6349
0
      return elf_link_add_object_symbols (abfd, info);
6350
0
    case bfd_archive:
6351
0
      return elf_link_add_archive_symbols (abfd, info);
6352
0
    default:
6353
0
      bfd_set_error (bfd_error_wrong_format);
6354
0
      return false;
6355
0
    }
6356
0
}
6357

6358
struct hash_codes_info
6359
{
6360
  unsigned long *hashcodes;
6361
  bool error;
6362
};
6363
6364
/* This function will be called though elf_link_hash_traverse to store
6365
   all hash value of the exported symbols in an array.  */
6366
6367
static bool
6368
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6369
0
{
6370
0
  struct hash_codes_info *inf = (struct hash_codes_info *) data;
6371
0
  const char *name;
6372
0
  unsigned long ha;
6373
0
  char *alc = NULL;
6374
6375
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6376
0
  if (h->dynindx == -1)
6377
0
    return true;
6378
6379
0
  name = h->root.root.string;
6380
0
  if (h->versioned >= versioned)
6381
0
    {
6382
0
      char *p = strchr (name, ELF_VER_CHR);
6383
0
      if (p != NULL)
6384
0
  {
6385
0
    alc = (char *) bfd_malloc (p - name + 1);
6386
0
    if (alc == NULL)
6387
0
      {
6388
0
        inf->error = true;
6389
0
        return false;
6390
0
      }
6391
0
    memcpy (alc, name, p - name);
6392
0
    alc[p - name] = '\0';
6393
0
    name = alc;
6394
0
  }
6395
0
    }
6396
6397
  /* Compute the hash value.  */
6398
0
  ha = bfd_elf_hash (name);
6399
6400
  /* Store the found hash value in the array given as the argument.  */
6401
0
  *(inf->hashcodes)++ = ha;
6402
6403
  /* And store it in the struct so that we can put it in the hash table
6404
     later.  */
6405
0
  h->u.elf_hash_value = ha;
6406
6407
0
  free (alc);
6408
0
  return true;
6409
0
}
6410
6411
struct collect_gnu_hash_codes
6412
{
6413
  bfd *output_bfd;
6414
  const struct elf_backend_data *bed;
6415
  unsigned long int nsyms;
6416
  unsigned long int maskbits;
6417
  unsigned long int *hashcodes;
6418
  unsigned long int *hashval;
6419
  unsigned long int *indx;
6420
  unsigned long int *counts;
6421
  bfd_vma *bitmask;
6422
  bfd_byte *contents;
6423
  bfd_size_type xlat;
6424
  long int min_dynindx;
6425
  unsigned long int bucketcount;
6426
  unsigned long int symindx;
6427
  long int local_indx;
6428
  long int shift1, shift2;
6429
  unsigned long int mask;
6430
  bool error;
6431
};
6432
6433
/* This function will be called though elf_link_hash_traverse to store
6434
   all hash value of the exported symbols in an array.  */
6435
6436
static bool
6437
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6438
0
{
6439
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6440
0
  const char *name;
6441
0
  unsigned long ha;
6442
0
  char *alc = NULL;
6443
6444
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6445
0
  if (h->dynindx == -1)
6446
0
    return true;
6447
6448
  /* Ignore also local symbols and undefined symbols.  */
6449
0
  if (! (*s->bed->elf_hash_symbol) (h))
6450
0
    return true;
6451
6452
0
  name = h->root.root.string;
6453
0
  if (h->versioned >= versioned)
6454
0
    {
6455
0
      char *p = strchr (name, ELF_VER_CHR);
6456
0
      if (p != NULL)
6457
0
  {
6458
0
    alc = (char *) bfd_malloc (p - name + 1);
6459
0
    if (alc == NULL)
6460
0
      {
6461
0
        s->error = true;
6462
0
        return false;
6463
0
      }
6464
0
    memcpy (alc, name, p - name);
6465
0
    alc[p - name] = '\0';
6466
0
    name = alc;
6467
0
  }
6468
0
    }
6469
6470
  /* Compute the hash value.  */
6471
0
  ha = bfd_elf_gnu_hash (name);
6472
6473
  /* Store the found hash value in the array for compute_bucket_count,
6474
     and also for .dynsym reordering purposes.  */
6475
0
  s->hashcodes[s->nsyms] = ha;
6476
0
  s->hashval[h->dynindx] = ha;
6477
0
  ++s->nsyms;
6478
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6479
0
    s->min_dynindx = h->dynindx;
6480
6481
0
  free (alc);
6482
0
  return true;
6483
0
}
6484
6485
/* This function will be called though elf_link_hash_traverse to do
6486
   final dynamic symbol renumbering in case of .gnu.hash.
6487
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6488
   to the translation table.  */
6489
6490
static bool
6491
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6492
0
{
6493
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6494
0
  unsigned long int bucket;
6495
0
  unsigned long int val;
6496
6497
  /* Ignore indirect symbols.  */
6498
0
  if (h->dynindx == -1)
6499
0
    return true;
6500
6501
  /* Ignore also local symbols and undefined symbols.  */
6502
0
  if (! (*s->bed->elf_hash_symbol) (h))
6503
0
    {
6504
0
      if (h->dynindx >= s->min_dynindx)
6505
0
  {
6506
0
    if (s->bed->record_xhash_symbol != NULL)
6507
0
      {
6508
0
        (*s->bed->record_xhash_symbol) (h, 0);
6509
0
        s->local_indx++;
6510
0
      }
6511
0
    else
6512
0
      h->dynindx = s->local_indx++;
6513
0
  }
6514
0
      return true;
6515
0
    }
6516
6517
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6518
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6519
0
  & ((s->maskbits >> s->shift1) - 1);
6520
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6521
0
  s->bitmask[val]
6522
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6523
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6524
0
  if (s->counts[bucket] == 1)
6525
    /* Last element terminates the chain.  */
6526
0
    val |= 1;
6527
0
  bfd_put_32 (s->output_bfd, val,
6528
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6529
0
  --s->counts[bucket];
6530
0
  if (s->bed->record_xhash_symbol != NULL)
6531
0
    {
6532
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6533
6534
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6535
0
    }
6536
0
  else
6537
0
    h->dynindx = s->indx[bucket]++;
6538
0
  return true;
6539
0
}
6540
6541
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6542
6543
bool
6544
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6545
0
{
6546
0
  return !(h->forced_local
6547
0
     || h->root.type == bfd_link_hash_undefined
6548
0
     || h->root.type == bfd_link_hash_undefweak
6549
0
     || ((h->root.type == bfd_link_hash_defined
6550
0
    || h->root.type == bfd_link_hash_defweak)
6551
0
         && h->root.u.def.section->output_section == NULL));
6552
0
}
6553
6554
/* Array used to determine the number of hash table buckets to use
6555
   based on the number of symbols there are.  If there are fewer than
6556
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6557
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6558
   than 32771 buckets.  */
6559
6560
static const size_t elf_buckets[] =
6561
{
6562
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6563
  16411, 32771, 0
6564
};
6565
6566
/* Compute bucket count for hashing table.  We do not use a static set
6567
   of possible tables sizes anymore.  Instead we determine for all
6568
   possible reasonable sizes of the table the outcome (i.e., the
6569
   number of collisions etc) and choose the best solution.  The
6570
   weighting functions are not too simple to allow the table to grow
6571
   without bounds.  Instead one of the weighting factors is the size.
6572
   Therefore the result is always a good payoff between few collisions
6573
   (= short chain lengths) and table size.  */
6574
static size_t
6575
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6576
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6577
          unsigned long int nsyms,
6578
          int gnu_hash)
6579
0
{
6580
0
  size_t best_size = 0;
6581
0
  unsigned long int i;
6582
6583
0
  if (info->optimize)
6584
0
    {
6585
0
      size_t minsize;
6586
0
      size_t maxsize;
6587
0
      uint64_t best_chlen = ~((uint64_t) 0);
6588
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6589
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6590
0
      const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6591
0
      unsigned long int *counts;
6592
0
      bfd_size_type amt;
6593
0
      unsigned int no_improvement_count = 0;
6594
6595
      /* Possible optimization parameters: if we have NSYMS symbols we say
6596
   that the hashing table must at least have NSYMS/4 and at most
6597
   2*NSYMS buckets.  */
6598
0
      minsize = nsyms / 4;
6599
0
      if (minsize == 0)
6600
0
  minsize = 1;
6601
0
      best_size = maxsize = nsyms * 2;
6602
0
      if (gnu_hash)
6603
0
  {
6604
0
    if (minsize < 2)
6605
0
      minsize = 2;
6606
0
    if ((best_size & 31) == 0)
6607
0
      ++best_size;
6608
0
  }
6609
6610
      /* Create array where we count the collisions in.  We must use bfd_malloc
6611
   since the size could be large.  */
6612
0
      amt = maxsize;
6613
0
      amt *= sizeof (unsigned long int);
6614
0
      counts = (unsigned long int *) bfd_malloc (amt);
6615
0
      if (counts == NULL)
6616
0
  return 0;
6617
6618
      /* Compute the "optimal" size for the hash table.  The criteria is a
6619
   minimal chain length.  The minor criteria is (of course) the size
6620
   of the table.  */
6621
0
      for (i = minsize; i < maxsize; ++i)
6622
0
  {
6623
    /* Walk through the array of hashcodes and count the collisions.  */
6624
0
    uint64_t max;
6625
0
    unsigned long int j;
6626
0
    unsigned long int fact;
6627
6628
0
    if (gnu_hash && (i & 31) == 0)
6629
0
      continue;
6630
6631
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6632
6633
    /* Determine how often each hash bucket is used.  */
6634
0
    for (j = 0; j < nsyms; ++j)
6635
0
      ++counts[hashcodes[j] % i];
6636
6637
    /* For the weight function we need some information about the
6638
       pagesize on the target.  This is information need not be 100%
6639
       accurate.  Since this information is not available (so far) we
6640
       define it here to a reasonable default value.  If it is crucial
6641
       to have a better value some day simply define this value.  */
6642
0
# ifndef BFD_TARGET_PAGESIZE
6643
0
#  define BFD_TARGET_PAGESIZE (4096)
6644
0
# endif
6645
6646
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6647
       and the chains.  */
6648
0
    max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6649
6650
0
# if 1
6651
    /* Variant 1: optimize for short chains.  We add the squares
6652
       of all the chain lengths (which favors many small chain
6653
       over a few long chains).  */
6654
0
    for (j = 0; j < i; ++j)
6655
0
      max += counts[j] * counts[j];
6656
6657
    /* This adds penalties for the overall size of the table.  */
6658
0
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6659
0
    max *= fact * fact;
6660
# else
6661
    /* Variant 2: Optimize a lot more for small table.  Here we
6662
       also add squares of the size but we also add penalties for
6663
       empty slots (the +1 term).  */
6664
    for (j = 0; j < i; ++j)
6665
      max += (1 + counts[j]) * (1 + counts[j]);
6666
6667
    /* The overall size of the table is considered, but not as
6668
       strong as in variant 1, where it is squared.  */
6669
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6670
    max *= fact;
6671
# endif
6672
6673
    /* Compare with current best results.  */
6674
0
    if (max < best_chlen)
6675
0
      {
6676
0
        best_chlen = max;
6677
0
        best_size = i;
6678
0
        no_improvement_count = 0;
6679
0
      }
6680
    /* PR 11843: Avoid futile long searches for the best bucket size
6681
       when there are a large number of symbols.  */
6682
0
    else if (++no_improvement_count == 100)
6683
0
      break;
6684
0
  }
6685
6686
0
      free (counts);
6687
0
    }
6688
0
  else
6689
0
    {
6690
0
      for (i = 0; elf_buckets[i] != 0; i++)
6691
0
  {
6692
0
    best_size = elf_buckets[i];
6693
0
    if (nsyms < elf_buckets[i + 1])
6694
0
      break;
6695
0
  }
6696
0
      if (gnu_hash && best_size < 2)
6697
0
  best_size = 2;
6698
0
    }
6699
6700
0
  return best_size;
6701
0
}
6702
6703
/* Size any SHT_GROUP section for ld -r.  */
6704
6705
bool
6706
_bfd_elf_size_group_sections (struct bfd_link_info *info)
6707
0
{
6708
0
  bfd *ibfd;
6709
0
  asection *s;
6710
6711
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6712
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6713
0
  && (s = ibfd->sections) != NULL
6714
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6715
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6716
0
      return false;
6717
0
  return true;
6718
0
}
6719
6720
/* Set a default stack segment size.  The value in INFO wins.  If it
6721
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6722
   undefined it is initialized.  */
6723
6724
bool
6725
bfd_elf_stack_segment_size (bfd *output_bfd,
6726
          struct bfd_link_info *info,
6727
          const char *legacy_symbol,
6728
          bfd_vma default_size)
6729
0
{
6730
0
  struct elf_link_hash_entry *h = NULL;
6731
6732
  /* Look for legacy symbol.  */
6733
0
  if (legacy_symbol)
6734
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6735
0
            false, false, false);
6736
0
  if (h && (h->root.type == bfd_link_hash_defined
6737
0
      || h->root.type == bfd_link_hash_defweak)
6738
0
      && h->def_regular
6739
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6740
0
    {
6741
      /* The symbol has no type if specified on the command line.  */
6742
0
      h->type = STT_OBJECT;
6743
0
      if (info->stacksize)
6744
  /* xgettext:c-format */
6745
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6746
0
          output_bfd, legacy_symbol);
6747
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6748
  /* xgettext:c-format */
6749
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6750
0
          output_bfd, legacy_symbol);
6751
0
      else
6752
0
  info->stacksize = h->root.u.def.value;
6753
0
    }
6754
6755
0
  if (!info->stacksize)
6756
    /* If the user didn't set a size, or explicitly inhibit the
6757
       size, set it now.  */
6758
0
    info->stacksize = default_size;
6759
6760
  /* Provide the legacy symbol, if it is referenced.  */
6761
0
  if (h && (h->root.type == bfd_link_hash_undefined
6762
0
      || h->root.type == bfd_link_hash_undefweak))
6763
0
    {
6764
0
      struct bfd_link_hash_entry *bh = NULL;
6765
6766
0
      if (!(_bfd_generic_link_add_one_symbol
6767
0
      (info, output_bfd, legacy_symbol,
6768
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6769
0
       info->stacksize >= 0 ? info->stacksize : 0,
6770
0
       NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6771
0
  return false;
6772
6773
0
      h = (struct elf_link_hash_entry *) bh;
6774
0
      h->def_regular = 1;
6775
0
      h->type = STT_OBJECT;
6776
0
    }
6777
6778
0
  return true;
6779
0
}
6780
6781
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6782
6783
struct elf_gc_sweep_symbol_info
6784
{
6785
  struct bfd_link_info *info;
6786
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6787
           bool);
6788
};
6789
6790
static bool
6791
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6792
0
{
6793
0
  if (!h->mark
6794
0
      && (((h->root.type == bfd_link_hash_defined
6795
0
      || h->root.type == bfd_link_hash_defweak)
6796
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6797
0
    && h->root.u.def.section->gc_mark))
6798
0
    || h->root.type == bfd_link_hash_undefined
6799
0
    || h->root.type == bfd_link_hash_undefweak))
6800
0
    {
6801
0
      struct elf_gc_sweep_symbol_info *inf;
6802
6803
0
      inf = (struct elf_gc_sweep_symbol_info *) data;
6804
0
      (*inf->hide_symbol) (inf->info, h, true);
6805
0
      h->def_regular = 0;
6806
0
      h->ref_regular = 0;
6807
0
      h->ref_regular_nonweak = 0;
6808
0
    }
6809
6810
0
  return true;
6811
0
}
6812
6813
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6814
   called by the ELF linker emulation before_allocation routine.  We
6815
   must set the sizes of the sections before the linker sets the
6816
   addresses of the various sections.  */
6817
6818
bool
6819
bfd_elf_size_dynamic_sections (bfd *output_bfd,
6820
             const char *soname,
6821
             const char *rpath,
6822
             const char *filter_shlib,
6823
             const char *audit,
6824
             const char *depaudit,
6825
             const char * const *auxiliary_filters,
6826
             struct bfd_link_info *info,
6827
             asection **sinterpptr)
6828
0
{
6829
0
  bfd *dynobj;
6830
0
  const struct elf_backend_data *bed;
6831
6832
0
  *sinterpptr = NULL;
6833
6834
0
  if (!is_elf_hash_table (info->hash))
6835
0
    return true;
6836
6837
  /* Any syms created from now on start with -1 in
6838
     got.refcount/offset and plt.refcount/offset.  */
6839
0
  elf_hash_table (info)->init_got_refcount
6840
0
    = elf_hash_table (info)->init_got_offset;
6841
0
  elf_hash_table (info)->init_plt_refcount
6842
0
    = elf_hash_table (info)->init_plt_offset;
6843
6844
0
  bed = get_elf_backend_data (output_bfd);
6845
6846
  /* The backend may have to create some sections regardless of whether
6847
     we're dynamic or not.  */
6848
0
  if (bed->elf_backend_early_size_sections
6849
0
      && !bed->elf_backend_early_size_sections (output_bfd, info))
6850
0
    return false;
6851
6852
0
  dynobj = elf_hash_table (info)->dynobj;
6853
6854
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6855
0
    {
6856
0
      struct bfd_elf_version_tree *verdefs;
6857
0
      struct elf_info_failed asvinfo;
6858
0
      struct bfd_elf_version_tree *t;
6859
0
      struct bfd_elf_version_expr *d;
6860
0
      asection *s;
6861
0
      size_t soname_indx;
6862
6863
      /* If we are supposed to export all symbols into the dynamic symbol
6864
   table (this is not the normal case), then do so.  */
6865
0
      if (info->export_dynamic
6866
0
    || (bfd_link_executable (info) && info->dynamic))
6867
0
  {
6868
0
    struct elf_info_failed eif;
6869
6870
0
    eif.info = info;
6871
0
    eif.failed = false;
6872
0
    elf_link_hash_traverse (elf_hash_table (info),
6873
0
          _bfd_elf_export_symbol,
6874
0
          &eif);
6875
0
    if (eif.failed)
6876
0
      return false;
6877
0
  }
6878
6879
0
      if (soname != NULL)
6880
0
  {
6881
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6882
0
               soname, true);
6883
0
    if (soname_indx == (size_t) -1
6884
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6885
0
      return false;
6886
0
  }
6887
0
      else
6888
0
  soname_indx = (size_t) -1;
6889
6890
      /* Make all global versions with definition.  */
6891
0
      for (t = info->version_info; t != NULL; t = t->next)
6892
0
  for (d = t->globals.list; d != NULL; d = d->next)
6893
0
    if (!d->symver && d->literal)
6894
0
      {
6895
0
        const char *verstr, *name;
6896
0
        size_t namelen, verlen, newlen;
6897
0
        char *newname, *p, leading_char;
6898
0
        struct elf_link_hash_entry *newh;
6899
6900
0
        leading_char = bfd_get_symbol_leading_char (output_bfd);
6901
0
        name = d->pattern;
6902
0
        namelen = strlen (name) + (leading_char != '\0');
6903
0
        verstr = t->name;
6904
0
        verlen = strlen (verstr);
6905
0
        newlen = namelen + verlen + 3;
6906
6907
0
        newname = (char *) bfd_malloc (newlen);
6908
0
        if (newname == NULL)
6909
0
    return false;
6910
0
        newname[0] = leading_char;
6911
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
6912
6913
        /* Check the hidden versioned definition.  */
6914
0
        p = newname + namelen;
6915
0
        *p++ = ELF_VER_CHR;
6916
0
        memcpy (p, verstr, verlen + 1);
6917
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
6918
0
             newname, false, false,
6919
0
             false);
6920
0
        if (newh == NULL
6921
0
      || (newh->root.type != bfd_link_hash_defined
6922
0
          && newh->root.type != bfd_link_hash_defweak))
6923
0
    {
6924
      /* Check the default versioned definition.  */
6925
0
      *p++ = ELF_VER_CHR;
6926
0
      memcpy (p, verstr, verlen + 1);
6927
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
6928
0
                 newname, false, false,
6929
0
                 false);
6930
0
    }
6931
0
        free (newname);
6932
6933
        /* Mark this version if there is a definition and it is
6934
     not defined in a shared object.  */
6935
0
        if (newh != NULL
6936
0
      && !newh->def_dynamic
6937
0
      && (newh->root.type == bfd_link_hash_defined
6938
0
          || newh->root.type == bfd_link_hash_defweak))
6939
0
    d->symver = 1;
6940
0
      }
6941
6942
      /* Attach all the symbols to their version information.  */
6943
0
      asvinfo.info = info;
6944
0
      asvinfo.failed = false;
6945
6946
0
      elf_link_hash_traverse (elf_hash_table (info),
6947
0
            _bfd_elf_link_assign_sym_version,
6948
0
            &asvinfo);
6949
0
      if (asvinfo.failed)
6950
0
  return false;
6951
6952
0
      if (!info->allow_undefined_version)
6953
0
  {
6954
    /* Check if all global versions have a definition.  */
6955
0
    bool all_defined = true;
6956
0
    for (t = info->version_info; t != NULL; t = t->next)
6957
0
      for (d = t->globals.list; d != NULL; d = d->next)
6958
0
        if (d->literal && !d->symver && !d->script)
6959
0
    {
6960
0
      _bfd_error_handler
6961
0
        (_("%s: undefined version: %s"),
6962
0
         d->pattern, t->name);
6963
0
      all_defined = false;
6964
0
    }
6965
6966
0
    if (!all_defined)
6967
0
      {
6968
0
        bfd_set_error (bfd_error_bad_value);
6969
0
        return false;
6970
0
      }
6971
0
  }
6972
6973
      /* Set up the version definition section.  */
6974
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6975
0
      BFD_ASSERT (s != NULL);
6976
6977
      /* We may have created additional version definitions if we are
6978
   just linking a regular application.  */
6979
0
      verdefs = info->version_info;
6980
6981
      /* Skip anonymous version tag.  */
6982
0
      if (verdefs != NULL && verdefs->vernum == 0)
6983
0
  verdefs = verdefs->next;
6984
6985
0
      if (verdefs == NULL && !info->create_default_symver)
6986
0
  s->flags |= SEC_EXCLUDE;
6987
0
      else
6988
0
  {
6989
0
    unsigned int cdefs;
6990
0
    bfd_size_type size;
6991
0
    bfd_byte *p;
6992
0
    Elf_Internal_Verdef def;
6993
0
    Elf_Internal_Verdaux defaux;
6994
0
    struct bfd_link_hash_entry *bh;
6995
0
    struct elf_link_hash_entry *h;
6996
0
    const char *name;
6997
6998
0
    cdefs = 0;
6999
0
    size = 0;
7000
7001
    /* Make space for the base version.  */
7002
0
    size += sizeof (Elf_External_Verdef);
7003
0
    size += sizeof (Elf_External_Verdaux);
7004
0
    ++cdefs;
7005
7006
    /* Make space for the default version.  */
7007
0
    if (info->create_default_symver)
7008
0
      {
7009
0
        size += sizeof (Elf_External_Verdef);
7010
0
        ++cdefs;
7011
0
      }
7012
7013
0
    for (t = verdefs; t != NULL; t = t->next)
7014
0
      {
7015
0
        struct bfd_elf_version_deps *n;
7016
7017
        /* Don't emit base version twice.  */
7018
0
        if (t->vernum == 0)
7019
0
    continue;
7020
7021
0
        size += sizeof (Elf_External_Verdef);
7022
0
        size += sizeof (Elf_External_Verdaux);
7023
0
        ++cdefs;
7024
7025
0
        for (n = t->deps; n != NULL; n = n->next)
7026
0
    size += sizeof (Elf_External_Verdaux);
7027
0
      }
7028
7029
0
    s->size = size;
7030
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7031
0
    if (s->contents == NULL && s->size != 0)
7032
0
      return false;
7033
7034
    /* Fill in the version definition section.  */
7035
7036
0
    p = s->contents;
7037
7038
0
    def.vd_version = VER_DEF_CURRENT;
7039
0
    def.vd_flags = VER_FLG_BASE;
7040
0
    def.vd_ndx = 1;
7041
0
    def.vd_cnt = 1;
7042
0
    if (info->create_default_symver)
7043
0
      {
7044
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7045
0
        def.vd_next = sizeof (Elf_External_Verdef);
7046
0
      }
7047
0
    else
7048
0
      {
7049
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7050
0
        def.vd_next = (sizeof (Elf_External_Verdef)
7051
0
           + sizeof (Elf_External_Verdaux));
7052
0
      }
7053
7054
0
    if (soname_indx != (size_t) -1)
7055
0
      {
7056
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7057
0
              soname_indx);
7058
0
        def.vd_hash = bfd_elf_hash (soname);
7059
0
        defaux.vda_name = soname_indx;
7060
0
        name = soname;
7061
0
      }
7062
0
    else
7063
0
      {
7064
0
        size_t indx;
7065
7066
0
        name = lbasename (bfd_get_filename (output_bfd));
7067
0
        def.vd_hash = bfd_elf_hash (name);
7068
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7069
0
            name, false);
7070
0
        if (indx == (size_t) -1)
7071
0
    return false;
7072
0
        defaux.vda_name = indx;
7073
0
      }
7074
0
    defaux.vda_next = 0;
7075
7076
0
    _bfd_elf_swap_verdef_out (output_bfd, &def,
7077
0
            (Elf_External_Verdef *) p);
7078
0
    p += sizeof (Elf_External_Verdef);
7079
0
    if (info->create_default_symver)
7080
0
      {
7081
        /* Add a symbol representing this version.  */
7082
0
        bh = NULL;
7083
0
        if (! (_bfd_generic_link_add_one_symbol
7084
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7085
0
          0, NULL, false,
7086
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7087
0
    return false;
7088
0
        h = (struct elf_link_hash_entry *) bh;
7089
0
        h->non_elf = 0;
7090
0
        h->def_regular = 1;
7091
0
        h->type = STT_OBJECT;
7092
0
        h->verinfo.vertree = NULL;
7093
7094
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7095
0
    return false;
7096
7097
        /* Create a duplicate of the base version with the same
7098
     aux block, but different flags.  */
7099
0
        def.vd_flags = 0;
7100
0
        def.vd_ndx = 2;
7101
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7102
0
        if (verdefs)
7103
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7104
0
             + sizeof (Elf_External_Verdaux));
7105
0
        else
7106
0
    def.vd_next = 0;
7107
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
7108
0
          (Elf_External_Verdef *) p);
7109
0
        p += sizeof (Elf_External_Verdef);
7110
0
      }
7111
0
    _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7112
0
             (Elf_External_Verdaux *) p);
7113
0
    p += sizeof (Elf_External_Verdaux);
7114
7115
0
    for (t = verdefs; t != NULL; t = t->next)
7116
0
      {
7117
0
        unsigned int cdeps;
7118
0
        struct bfd_elf_version_deps *n;
7119
7120
        /* Don't emit the base version twice.  */
7121
0
        if (t->vernum == 0)
7122
0
    continue;
7123
7124
0
        cdeps = 0;
7125
0
        for (n = t->deps; n != NULL; n = n->next)
7126
0
    ++cdeps;
7127
7128
        /* Add a symbol representing this version.  */
7129
0
        bh = NULL;
7130
0
        if (! (_bfd_generic_link_add_one_symbol
7131
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7132
0
          0, NULL, false,
7133
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7134
0
    return false;
7135
0
        h = (struct elf_link_hash_entry *) bh;
7136
0
        h->non_elf = 0;
7137
0
        h->def_regular = 1;
7138
0
        h->type = STT_OBJECT;
7139
0
        h->verinfo.vertree = t;
7140
7141
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7142
0
    return false;
7143
7144
0
        def.vd_version = VER_DEF_CURRENT;
7145
0
        def.vd_flags = 0;
7146
0
        if (t->globals.list == NULL
7147
0
      && t->locals.list == NULL
7148
0
      && ! t->used)
7149
0
    def.vd_flags |= VER_FLG_WEAK;
7150
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7151
0
        def.vd_cnt = cdeps + 1;
7152
0
        def.vd_hash = bfd_elf_hash (t->name);
7153
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7154
0
        def.vd_next = 0;
7155
7156
        /* If a basever node is next, it *must* be the last node in
7157
     the chain, otherwise Verdef construction breaks.  */
7158
0
        if (t->next != NULL && t->next->vernum == 0)
7159
0
    BFD_ASSERT (t->next->next == NULL);
7160
7161
0
        if (t->next != NULL && t->next->vernum != 0)
7162
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7163
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7164
7165
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
7166
0
          (Elf_External_Verdef *) p);
7167
0
        p += sizeof (Elf_External_Verdef);
7168
7169
0
        defaux.vda_name = h->dynstr_index;
7170
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7171
0
              h->dynstr_index);
7172
0
        defaux.vda_next = 0;
7173
0
        if (t->deps != NULL)
7174
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
7175
0
        t->name_indx = defaux.vda_name;
7176
7177
0
        _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7178
0
           (Elf_External_Verdaux *) p);
7179
0
        p += sizeof (Elf_External_Verdaux);
7180
7181
0
        for (n = t->deps; n != NULL; n = n->next)
7182
0
    {
7183
0
      if (n->version_needed == NULL)
7184
0
        {
7185
          /* This can happen if there was an error in the
7186
       version script.  */
7187
0
          defaux.vda_name = 0;
7188
0
        }
7189
0
      else
7190
0
        {
7191
0
          defaux.vda_name = n->version_needed->name_indx;
7192
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7193
0
                defaux.vda_name);
7194
0
        }
7195
0
      if (n->next == NULL)
7196
0
        defaux.vda_next = 0;
7197
0
      else
7198
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7199
7200
0
      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7201
0
               (Elf_External_Verdaux *) p);
7202
0
      p += sizeof (Elf_External_Verdaux);
7203
0
    }
7204
0
      }
7205
7206
0
    elf_tdata (output_bfd)->cverdefs = cdefs;
7207
0
  }
7208
0
    }
7209
7210
0
  if (info->gc_sections && bed->can_gc_sections)
7211
0
    {
7212
0
      struct elf_gc_sweep_symbol_info sweep_info;
7213
7214
      /* Remove the symbols that were in the swept sections from the
7215
   dynamic symbol table.  */
7216
0
      sweep_info.info = info;
7217
0
      sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7218
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7219
0
            &sweep_info);
7220
0
    }
7221
7222
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7223
0
    {
7224
0
      asection *s;
7225
0
      struct elf_find_verdep_info sinfo;
7226
7227
      /* Work out the size of the version reference section.  */
7228
7229
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7230
0
      BFD_ASSERT (s != NULL);
7231
7232
0
      sinfo.info = info;
7233
0
      sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7234
0
      if (sinfo.vers == 0)
7235
0
  sinfo.vers = 1;
7236
0
      sinfo.failed = false;
7237
7238
0
      elf_link_hash_traverse (elf_hash_table (info),
7239
0
            _bfd_elf_link_find_version_dependencies,
7240
0
            &sinfo);
7241
0
      if (sinfo.failed)
7242
0
  return false;
7243
7244
0
      bed->elf_backend_add_glibc_version_dependency (&sinfo);
7245
0
      if (sinfo.failed)
7246
0
  return false;
7247
7248
0
      if (elf_tdata (output_bfd)->verref == NULL)
7249
0
  s->flags |= SEC_EXCLUDE;
7250
0
      else
7251
0
  {
7252
0
    Elf_Internal_Verneed *vn;
7253
0
    unsigned int size;
7254
0
    unsigned int crefs;
7255
0
    bfd_byte *p;
7256
7257
    /* Build the version dependency section.  */
7258
0
    size = 0;
7259
0
    crefs = 0;
7260
0
    for (vn = elf_tdata (output_bfd)->verref;
7261
0
         vn != NULL;
7262
0
         vn = vn->vn_nextref)
7263
0
      {
7264
0
        Elf_Internal_Vernaux *a;
7265
7266
0
        size += sizeof (Elf_External_Verneed);
7267
0
        ++crefs;
7268
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7269
0
    size += sizeof (Elf_External_Vernaux);
7270
0
      }
7271
7272
0
    s->size = size;
7273
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7274
0
    if (s->contents == NULL)
7275
0
      return false;
7276
7277
0
    p = s->contents;
7278
0
    for (vn = elf_tdata (output_bfd)->verref;
7279
0
         vn != NULL;
7280
0
         vn = vn->vn_nextref)
7281
0
      {
7282
0
        unsigned int caux;
7283
0
        Elf_Internal_Vernaux *a;
7284
0
        size_t indx;
7285
7286
0
        caux = 0;
7287
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7288
0
    ++caux;
7289
7290
0
        vn->vn_version = VER_NEED_CURRENT;
7291
0
        vn->vn_cnt = caux;
7292
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7293
0
            elf_dt_name (vn->vn_bfd) != NULL
7294
0
            ? elf_dt_name (vn->vn_bfd)
7295
0
            : lbasename (bfd_get_filename
7296
0
                   (vn->vn_bfd)),
7297
0
            false);
7298
0
        if (indx == (size_t) -1)
7299
0
    return false;
7300
0
        vn->vn_file = indx;
7301
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7302
0
        if (vn->vn_nextref == NULL)
7303
0
    vn->vn_next = 0;
7304
0
        else
7305
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7306
0
             + caux * sizeof (Elf_External_Vernaux));
7307
7308
0
        _bfd_elf_swap_verneed_out (output_bfd, vn,
7309
0
           (Elf_External_Verneed *) p);
7310
0
        p += sizeof (Elf_External_Verneed);
7311
7312
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7313
0
    {
7314
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7315
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7316
0
                a->vna_nodename, false);
7317
0
      if (indx == (size_t) -1)
7318
0
        return false;
7319
0
      a->vna_name = indx;
7320
0
      if (a->vna_nextptr == NULL)
7321
0
        a->vna_next = 0;
7322
0
      else
7323
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7324
7325
0
      _bfd_elf_swap_vernaux_out (output_bfd, a,
7326
0
               (Elf_External_Vernaux *) p);
7327
0
      p += sizeof (Elf_External_Vernaux);
7328
0
    }
7329
0
      }
7330
7331
0
    elf_tdata (output_bfd)->cverrefs = crefs;
7332
0
  }
7333
0
    }
7334
7335
0
  if (bfd_link_relocatable (info)
7336
0
      && !_bfd_elf_size_group_sections (info))
7337
0
    return false;
7338
7339
  /* Determine any GNU_STACK segment requirements, after the backend
7340
     has had a chance to set a default segment size.  */
7341
0
  if (info->execstack)
7342
0
    {
7343
      /* If the user has explicitly requested warnings, then generate one even
7344
   though the choice is the result of another command line option.  */
7345
0
      if (info->warn_execstack == 1)
7346
0
  {
7347
0
    if (info->error_execstack)
7348
0
      {
7349
0
        _bfd_error_handler
7350
0
    (_("\
7351
0
error: creating an executable stack because of -z execstack command line option"));
7352
0
        return false;
7353
0
      }
7354
7355
0
    _bfd_error_handler
7356
0
      (_("\
7357
0
warning: enabling an executable stack because of -z execstack command line option"));
7358
0
  }
7359
7360
0
      elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7361
0
    }
7362
0
  else if (info->noexecstack)
7363
0
    elf_stack_flags (output_bfd) = PF_R | PF_W;
7364
0
  else
7365
0
    {
7366
0
      bfd *inputobj;
7367
0
      asection *notesec = NULL;
7368
0
      bfd *noteobj = NULL;
7369
0
      bfd *emptyobj = NULL;
7370
0
      int exec = 0;
7371
7372
0
      for (inputobj = info->input_bfds;
7373
0
     inputobj;
7374
0
     inputobj = inputobj->link.next)
7375
0
  {
7376
0
    asection *s;
7377
7378
0
    if (inputobj->flags
7379
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7380
0
      continue;
7381
0
    s = inputobj->sections;
7382
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7383
0
      continue;
7384
7385
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7386
0
    if (s)
7387
0
      {
7388
0
        notesec = s;
7389
0
        if (s->flags & SEC_CODE)
7390
0
    {
7391
0
      noteobj = inputobj;
7392
0
      exec = PF_X;
7393
      /* There is no point in scanning the remaining bfds.  */
7394
0
      break;
7395
0
    }
7396
0
      }
7397
0
    else if (bed->default_execstack && info->default_execstack)
7398
0
      {
7399
0
        exec = PF_X;
7400
0
        emptyobj = inputobj;
7401
0
      }
7402
0
  }
7403
7404
0
      if (notesec || info->stacksize > 0)
7405
0
  {
7406
0
    if (exec)
7407
0
      {
7408
0
        if (info->warn_execstack != 0)
7409
0
    {
7410
      /* PR 29072: Because an executable stack is a serious
7411
         security risk, make sure that the user knows that it is
7412
         being enabled despite the fact that it was not requested
7413
         on the command line.  */
7414
0
      if (noteobj)
7415
0
        {
7416
0
          if (info->error_execstack)
7417
0
      {
7418
0
        _bfd_error_handler (_("\
7419
0
error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7420
0
                bfd_get_filename (noteobj));
7421
0
        return false;
7422
0
      }
7423
7424
0
          _bfd_error_handler (_("\
7425
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7426
0
           bfd_get_filename (noteobj));
7427
0
        }
7428
0
      else if (emptyobj)
7429
0
        {
7430
0
          if (info->error_execstack)
7431
0
      {
7432
0
        _bfd_error_handler (_("\
7433
0
error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7434
0
                bfd_get_filename (emptyobj));
7435
0
        return false;
7436
0
      }
7437
7438
0
          _bfd_error_handler (_("\
7439
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7440
0
            bfd_get_filename (emptyobj));
7441
0
          _bfd_error_handler (_("\
7442
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7443
0
        }
7444
0
    }
7445
0
      }
7446
0
    elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7447
0
  }
7448
7449
0
      if (notesec && exec && bfd_link_relocatable (info)
7450
0
    && notesec->output_section != bfd_abs_section_ptr)
7451
0
  notesec->output_section->flags |= SEC_CODE;
7452
0
    }
7453
7454
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7455
0
    {
7456
0
      struct elf_info_failed eif;
7457
0
      struct elf_link_hash_entry *h;
7458
0
      asection *dynstr;
7459
0
      asection *s;
7460
7461
0
      *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7462
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7463
7464
0
      if (info->symbolic)
7465
0
  {
7466
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7467
0
      return false;
7468
0
    info->flags |= DF_SYMBOLIC;
7469
0
  }
7470
7471
0
      if (rpath != NULL)
7472
0
  {
7473
0
    size_t indx;
7474
0
    bfd_vma tag;
7475
7476
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7477
0
              true);
7478
0
    if (indx == (size_t) -1)
7479
0
      return false;
7480
7481
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7482
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7483
0
      return false;
7484
0
  }
7485
7486
0
      if (filter_shlib != NULL)
7487
0
  {
7488
0
    size_t indx;
7489
7490
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7491
0
              filter_shlib, true);
7492
0
    if (indx == (size_t) -1
7493
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7494
0
      return false;
7495
0
  }
7496
7497
0
      if (auxiliary_filters != NULL)
7498
0
  {
7499
0
    const char * const *p;
7500
7501
0
    for (p = auxiliary_filters; *p != NULL; p++)
7502
0
      {
7503
0
        size_t indx;
7504
7505
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7506
0
            *p, true);
7507
0
        if (indx == (size_t) -1
7508
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7509
0
    return false;
7510
0
      }
7511
0
  }
7512
7513
0
      if (audit != NULL)
7514
0
  {
7515
0
    size_t indx;
7516
7517
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7518
0
              true);
7519
0
    if (indx == (size_t) -1
7520
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7521
0
      return false;
7522
0
  }
7523
7524
0
      if (depaudit != NULL)
7525
0
  {
7526
0
    size_t indx;
7527
7528
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7529
0
              true);
7530
0
    if (indx == (size_t) -1
7531
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7532
0
      return false;
7533
0
  }
7534
7535
0
      eif.info = info;
7536
0
      eif.failed = false;
7537
7538
      /* Find all symbols which were defined in a dynamic object and make
7539
   the backend pick a reasonable value for them.  */
7540
0
      elf_link_hash_traverse (elf_hash_table (info),
7541
0
            _bfd_elf_adjust_dynamic_symbol,
7542
0
            &eif);
7543
0
      if (eif.failed)
7544
0
  return false;
7545
7546
      /* Add some entries to the .dynamic section.  We fill in some of the
7547
   values later, in bfd_elf_final_link, but we must add the entries
7548
   now so that we know the final size of the .dynamic section.  */
7549
7550
      /* If there are initialization and/or finalization functions to
7551
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7552
0
      h = (info->init_function
7553
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7554
0
           info->init_function, false,
7555
0
           false, false)
7556
0
     : NULL);
7557
0
      if (h != NULL
7558
0
    && (h->ref_regular
7559
0
        || h->def_regular))
7560
0
  {
7561
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7562
0
      return false;
7563
0
  }
7564
0
      h = (info->fini_function
7565
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7566
0
           info->fini_function, false,
7567
0
           false, false)
7568
0
     : NULL);
7569
0
      if (h != NULL
7570
0
    && (h->ref_regular
7571
0
        || h->def_regular))
7572
0
  {
7573
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7574
0
      return false;
7575
0
  }
7576
7577
0
      s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7578
0
      if (s != NULL && s->linker_has_input)
7579
0
  {
7580
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7581
0
    if (! bfd_link_executable (info))
7582
0
      {
7583
0
        bfd *sub;
7584
0
        asection *o;
7585
7586
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7587
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7588
0
        && (o = sub->sections) != NULL
7589
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7590
0
      for (o = sub->sections; o != NULL; o = o->next)
7591
0
        if (elf_section_data (o)->this_hdr.sh_type
7592
0
      == SHT_PREINIT_ARRAY)
7593
0
          {
7594
0
      _bfd_error_handler
7595
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7596
0
         sub);
7597
0
      break;
7598
0
          }
7599
7600
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7601
0
        return false;
7602
0
      }
7603
7604
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7605
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7606
0
      return false;
7607
0
  }
7608
0
      s = bfd_get_section_by_name (output_bfd, ".init_array");
7609
0
      if (s != NULL && s->linker_has_input)
7610
0
  {
7611
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7612
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7613
0
      return false;
7614
0
  }
7615
0
      s = bfd_get_section_by_name (output_bfd, ".fini_array");
7616
0
      if (s != NULL && s->linker_has_input)
7617
0
  {
7618
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7619
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7620
0
      return false;
7621
0
  }
7622
7623
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7624
      /* If .dynstr is excluded from the link, we don't want any of
7625
   these tags.  Strictly, we should be checking each section
7626
   individually;  This quick check covers for the case where
7627
   someone does a /DISCARD/ : { *(*) }.  */
7628
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7629
0
  {
7630
0
    bfd_size_type strsize;
7631
7632
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7633
0
    if ((info->emit_hash
7634
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7635
0
        || (info->emit_gnu_hash
7636
0
      && (bed->record_xhash_symbol == NULL
7637
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7638
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7639
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7640
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7641
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7642
0
                bed->s->sizeof_sym)
7643
0
        || (info->gnu_flags_1
7644
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7645
0
              info->gnu_flags_1)))
7646
0
      return false;
7647
0
  }
7648
0
    }
7649
7650
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7651
0
    return false;
7652
7653
  /* The backend must work out the sizes of all the other dynamic
7654
     sections.  */
7655
0
  if (bed->elf_backend_late_size_sections != NULL
7656
0
      && !bed->elf_backend_late_size_sections (output_bfd, info))
7657
0
    return false;
7658
7659
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7660
0
    {
7661
0
      if (elf_tdata (output_bfd)->cverdefs)
7662
0
  {
7663
0
    unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7664
7665
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7666
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7667
0
      return false;
7668
0
  }
7669
7670
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7671
0
  {
7672
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7673
0
      return false;
7674
0
  }
7675
0
      else if (info->flags & DF_BIND_NOW)
7676
0
  {
7677
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7678
0
      return false;
7679
0
  }
7680
7681
0
      if (info->flags_1)
7682
0
  {
7683
0
    if (bfd_link_executable (info))
7684
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7685
0
        | DF_1_NODELETE
7686
0
        | DF_1_NOOPEN);
7687
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7688
0
      return false;
7689
0
  }
7690
7691
0
      if (elf_tdata (output_bfd)->cverrefs)
7692
0
  {
7693
0
    unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7694
7695
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7696
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7697
0
      return false;
7698
0
  }
7699
7700
0
      if ((elf_tdata (output_bfd)->cverrefs == 0
7701
0
     && elf_tdata (output_bfd)->cverdefs == 0)
7702
0
    || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7703
0
  {
7704
0
    asection *s;
7705
7706
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7707
0
    s->flags |= SEC_EXCLUDE;
7708
0
  }
7709
0
    }
7710
0
  return true;
7711
0
}
7712
7713
/* Find the first non-excluded output section.  We'll use its
7714
   section symbol for some emitted relocs.  */
7715
void
7716
_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7717
0
{
7718
0
  asection *s;
7719
0
  asection *found = NULL;
7720
7721
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7722
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7723
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7724
0
      {
7725
0
  found = s;
7726
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7727
0
    break;
7728
0
      }
7729
0
  elf_hash_table (info)->text_index_section = found;
7730
0
}
7731
7732
/* Find two non-excluded output sections, one for code, one for data.
7733
   We'll use their section symbols for some emitted relocs.  */
7734
void
7735
_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7736
0
{
7737
0
  asection *s;
7738
0
  asection *found = NULL;
7739
7740
  /* Data first, since setting text_index_section changes
7741
     _bfd_elf_omit_section_dynsym_default.  */
7742
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7743
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7744
0
  && !(s->flags & SEC_READONLY)
7745
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7746
0
      {
7747
0
  found = s;
7748
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7749
0
    break;
7750
0
      }
7751
0
  elf_hash_table (info)->data_index_section = found;
7752
7753
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7754
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7755
0
  && (s->flags & SEC_READONLY)
7756
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7757
0
      {
7758
0
  found = s;
7759
0
  break;
7760
0
      }
7761
0
  elf_hash_table (info)->text_index_section = found;
7762
0
}
7763
7764
#define GNU_HASH_SECTION_NAME(bed)          \
7765
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7766
7767
bool
7768
bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7769
0
{
7770
0
  const struct elf_backend_data *bed;
7771
0
  unsigned long section_sym_count;
7772
0
  bfd_size_type dynsymcount = 0;
7773
7774
0
  if (!is_elf_hash_table (info->hash))
7775
0
    return true;
7776
7777
0
  bed = get_elf_backend_data (output_bfd);
7778
0
  (*bed->elf_backend_init_index_section) (output_bfd, info);
7779
7780
  /* Assign dynsym indices.  In a shared library we generate a section
7781
     symbol for each output section, which come first.  Next come all
7782
     of the back-end allocated local dynamic syms, followed by the rest
7783
     of the global symbols.
7784
7785
     This is usually not needed for static binaries, however backends
7786
     can request to always do it, e.g. the MIPS backend uses dynamic
7787
     symbol counts to lay out GOT, which will be produced in the
7788
     presence of GOT relocations even in static binaries (holding fixed
7789
     data in that case, to satisfy those relocations).  */
7790
7791
0
  if (elf_hash_table (info)->dynamic_sections_created
7792
0
      || bed->always_renumber_dynsyms)
7793
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7794
0
              &section_sym_count);
7795
7796
0
  if (elf_hash_table (info)->dynamic_sections_created)
7797
0
    {
7798
0
      bfd *dynobj;
7799
0
      asection *s;
7800
0
      unsigned int dtagcount;
7801
7802
0
      dynobj = elf_hash_table (info)->dynobj;
7803
7804
      /* Work out the size of the symbol version section.  */
7805
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7806
0
      BFD_ASSERT (s != NULL);
7807
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7808
0
  {
7809
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7810
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7811
0
    if (s->contents == NULL)
7812
0
      return false;
7813
7814
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7815
0
      return false;
7816
0
  }
7817
7818
      /* Set the size of the .dynsym and .hash sections.  We counted
7819
   the number of dynamic symbols in elf_link_add_object_symbols.
7820
   We will build the contents of .dynsym and .hash when we build
7821
   the final symbol table, because until then we do not know the
7822
   correct value to give the symbols.  We built the .dynstr
7823
   section as we went along in elf_link_add_object_symbols.  */
7824
0
      s = elf_hash_table (info)->dynsym;
7825
0
      BFD_ASSERT (s != NULL);
7826
0
      s->size = dynsymcount * bed->s->sizeof_sym;
7827
7828
0
      s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7829
0
      if (s->contents == NULL)
7830
0
  return false;
7831
7832
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7833
   section syms, in case we don't output them all.  */
7834
0
      ++section_sym_count;
7835
0
      memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7836
7837
0
      elf_hash_table (info)->bucketcount = 0;
7838
7839
      /* Compute the size of the hashing table.  As a side effect this
7840
   computes the hash values for all the names we export.  */
7841
0
      if (info->emit_hash)
7842
0
  {
7843
0
    unsigned long int *hashcodes;
7844
0
    struct hash_codes_info hashinf;
7845
0
    bfd_size_type amt;
7846
0
    unsigned long int nsyms;
7847
0
    size_t bucketcount;
7848
0
    size_t hash_entry_size;
7849
7850
    /* Compute the hash values for all exported symbols.  At the same
7851
       time store the values in an array so that we could use them for
7852
       optimizations.  */
7853
0
    amt = dynsymcount * sizeof (unsigned long int);
7854
0
    hashcodes = (unsigned long int *) bfd_malloc (amt);
7855
0
    if (hashcodes == NULL)
7856
0
      return false;
7857
0
    hashinf.hashcodes = hashcodes;
7858
0
    hashinf.error = false;
7859
7860
    /* Put all hash values in HASHCODES.  */
7861
0
    elf_link_hash_traverse (elf_hash_table (info),
7862
0
          elf_collect_hash_codes, &hashinf);
7863
0
    if (hashinf.error)
7864
0
      {
7865
0
        free (hashcodes);
7866
0
        return false;
7867
0
      }
7868
7869
0
    nsyms = hashinf.hashcodes - hashcodes;
7870
0
    bucketcount
7871
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
7872
0
    free (hashcodes);
7873
7874
0
    if (bucketcount == 0 && nsyms > 0)
7875
0
      return false;
7876
7877
0
    elf_hash_table (info)->bucketcount = bucketcount;
7878
7879
0
    s = bfd_get_linker_section (dynobj, ".hash");
7880
0
    BFD_ASSERT (s != NULL);
7881
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7882
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7883
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7884
0
    if (s->contents == NULL)
7885
0
      return false;
7886
7887
0
    bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7888
0
    bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7889
0
       s->contents + hash_entry_size);
7890
0
  }
7891
7892
0
      if (info->emit_gnu_hash)
7893
0
  {
7894
0
    size_t i, cnt;
7895
0
    unsigned char *contents;
7896
0
    struct collect_gnu_hash_codes cinfo;
7897
0
    bfd_size_type amt;
7898
0
    size_t bucketcount;
7899
7900
0
    memset (&cinfo, 0, sizeof (cinfo));
7901
7902
    /* Compute the hash values for all exported symbols.  At the same
7903
       time store the values in an array so that we could use them for
7904
       optimizations.  */
7905
0
    amt = dynsymcount * 2 * sizeof (unsigned long int);
7906
0
    cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7907
0
    if (cinfo.hashcodes == NULL)
7908
0
      return false;
7909
7910
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
7911
0
    cinfo.min_dynindx = -1;
7912
0
    cinfo.output_bfd = output_bfd;
7913
0
    cinfo.bed = bed;
7914
7915
    /* Put all hash values in HASHCODES.  */
7916
0
    elf_link_hash_traverse (elf_hash_table (info),
7917
0
          elf_collect_gnu_hash_codes, &cinfo);
7918
0
    if (cinfo.error)
7919
0
      {
7920
0
        free (cinfo.hashcodes);
7921
0
        return false;
7922
0
      }
7923
7924
0
    bucketcount
7925
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7926
7927
0
    if (bucketcount == 0)
7928
0
      {
7929
0
        free (cinfo.hashcodes);
7930
0
        return false;
7931
0
      }
7932
7933
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7934
0
    BFD_ASSERT (s != NULL);
7935
7936
0
    if (cinfo.nsyms == 0)
7937
0
      {
7938
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
7939
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
7940
0
        free (cinfo.hashcodes);
7941
0
        s->size = 5 * 4 + bed->s->arch_size / 8;
7942
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7943
0
        if (contents == NULL)
7944
0
    return false;
7945
0
        s->contents = contents;
7946
        /* 1 empty bucket.  */
7947
0
        bfd_put_32 (output_bfd, 1, contents);
7948
        /* SYMIDX above the special symbol 0.  */
7949
0
        bfd_put_32 (output_bfd, 1, contents + 4);
7950
        /* Just one word for bitmask.  */
7951
0
        bfd_put_32 (output_bfd, 1, contents + 8);
7952
        /* Only hash fn bloom filter.  */
7953
0
        bfd_put_32 (output_bfd, 0, contents + 12);
7954
        /* No hashes are valid - empty bitmask.  */
7955
0
        bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7956
        /* No hashes in the only bucket.  */
7957
0
        bfd_put_32 (output_bfd, 0,
7958
0
        contents + 16 + bed->s->arch_size / 8);
7959
0
      }
7960
0
    else
7961
0
      {
7962
0
        unsigned long int maskwords, maskbitslog2, x;
7963
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
7964
7965
0
        x = cinfo.nsyms;
7966
0
        maskbitslog2 = 1;
7967
0
        while ((x >>= 1) != 0)
7968
0
    ++maskbitslog2;
7969
0
        if (maskbitslog2 < 3)
7970
0
    maskbitslog2 = 5;
7971
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7972
0
    maskbitslog2 = maskbitslog2 + 3;
7973
0
        else
7974
0
    maskbitslog2 = maskbitslog2 + 2;
7975
0
        if (bed->s->arch_size == 64)
7976
0
    {
7977
0
      if (maskbitslog2 == 5)
7978
0
        maskbitslog2 = 6;
7979
0
      cinfo.shift1 = 6;
7980
0
    }
7981
0
        else
7982
0
    cinfo.shift1 = 5;
7983
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
7984
0
        cinfo.shift2 = maskbitslog2;
7985
0
        cinfo.maskbits = 1 << maskbitslog2;
7986
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7987
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
7988
0
        amt += maskwords * sizeof (bfd_vma);
7989
0
        cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7990
0
        if (cinfo.bitmask == NULL)
7991
0
    {
7992
0
      free (cinfo.hashcodes);
7993
0
      return false;
7994
0
    }
7995
7996
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7997
0
        cinfo.indx = cinfo.counts + bucketcount;
7998
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
7999
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8000
8001
        /* Determine how often each hash bucket is used.  */
8002
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8003
0
        for (i = 0; i < cinfo.nsyms; ++i)
8004
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8005
8006
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8007
0
    if (cinfo.counts[i] != 0)
8008
0
      {
8009
0
        cinfo.indx[i] = cnt;
8010
0
        cnt += cinfo.counts[i];
8011
0
      }
8012
0
        BFD_ASSERT (cnt == dynsymcount);
8013
0
        cinfo.bucketcount = bucketcount;
8014
0
        cinfo.local_indx = cinfo.min_dynindx;
8015
8016
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8017
0
        s->size += cinfo.maskbits / 8;
8018
0
        if (bed->record_xhash_symbol != NULL)
8019
0
    s->size += cinfo.nsyms * 4;
8020
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8021
0
        if (contents == NULL)
8022
0
    {
8023
0
      free (cinfo.bitmask);
8024
0
      free (cinfo.hashcodes);
8025
0
      return false;
8026
0
    }
8027
8028
0
        s->contents = contents;
8029
0
        bfd_put_32 (output_bfd, bucketcount, contents);
8030
0
        bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8031
0
        bfd_put_32 (output_bfd, maskwords, contents + 8);
8032
0
        bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8033
0
        contents += 16 + cinfo.maskbits / 8;
8034
8035
0
        for (i = 0; i < bucketcount; ++i)
8036
0
    {
8037
0
      if (cinfo.counts[i] == 0)
8038
0
        bfd_put_32 (output_bfd, 0, contents);
8039
0
      else
8040
0
        bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8041
0
      contents += 4;
8042
0
    }
8043
8044
0
        cinfo.contents = contents;
8045
8046
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8047
        /* Renumber dynamic symbols, if populating .gnu.hash section.
8048
     If using .MIPS.xhash, populate the translation table.  */
8049
0
        elf_link_hash_traverse (elf_hash_table (info),
8050
0
              elf_gnu_hash_process_symidx, &cinfo);
8051
8052
0
        contents = s->contents + 16;
8053
0
        for (i = 0; i < maskwords; ++i)
8054
0
    {
8055
0
      bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8056
0
         contents);
8057
0
      contents += bed->s->arch_size / 8;
8058
0
    }
8059
8060
0
        free (cinfo.bitmask);
8061
0
        free (cinfo.hashcodes);
8062
0
      }
8063
0
  }
8064
8065
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
8066
0
      BFD_ASSERT (s != NULL);
8067
8068
0
      elf_finalize_dynstr (output_bfd, info);
8069
8070
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8071
8072
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8073
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8074
0
    return false;
8075
0
    }
8076
8077
0
  return true;
8078
0
}
8079

8080
/* Make sure sec_info_type is cleared if sec_info is cleared too.  */
8081
8082
static void
8083
merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
8084
          asection *sec)
8085
0
{
8086
0
  BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
8087
0
  sec->sec_info_type = SEC_INFO_TYPE_NONE;
8088
0
}
8089
8090
/* Finish SHF_MERGE section merging.  */
8091
8092
bool
8093
_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
8094
0
{
8095
0
  bfd *ibfd;
8096
0
  asection *sec;
8097
8098
0
  if (!is_elf_hash_table (info->hash))
8099
0
    return false;
8100
8101
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8102
0
    if ((ibfd->flags & DYNAMIC) == 0
8103
0
  && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
8104
0
  && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
8105
0
      == get_elf_backend_data (obfd)->s->elfclass))
8106
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8107
0
  if ((sec->flags & SEC_MERGE) != 0
8108
0
      && !bfd_is_abs_section (sec->output_section))
8109
0
    {
8110
0
      struct bfd_elf_section_data *secdata;
8111
8112
0
      secdata = elf_section_data (sec);
8113
0
      if (! _bfd_add_merge_section (obfd,
8114
0
            &elf_hash_table (info)->merge_info,
8115
0
            sec, &secdata->sec_info))
8116
0
        return false;
8117
0
      else if (secdata->sec_info)
8118
0
        sec->sec_info_type = SEC_INFO_TYPE_MERGE;
8119
0
    }
8120
8121
0
  if (elf_hash_table (info)->merge_info != NULL)
8122
0
    _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
8123
0
       merge_sections_remove_hook);
8124
0
  return true;
8125
0
}
8126
8127
/* Create an entry in an ELF linker hash table.  */
8128
8129
struct bfd_hash_entry *
8130
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8131
          struct bfd_hash_table *table,
8132
          const char *string)
8133
0
{
8134
  /* Allocate the structure if it has not already been allocated by a
8135
     subclass.  */
8136
0
  if (entry == NULL)
8137
0
    {
8138
0
      entry = (struct bfd_hash_entry *)
8139
0
  bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8140
0
      if (entry == NULL)
8141
0
  return entry;
8142
0
    }
8143
8144
  /* Call the allocation method of the superclass.  */
8145
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
8146
0
  if (entry != NULL)
8147
0
    {
8148
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8149
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8150
8151
      /* Set local fields.  */
8152
0
      ret->indx = -1;
8153
0
      ret->dynindx = -1;
8154
0
      ret->got = htab->init_got_refcount;
8155
0
      ret->plt = htab->init_plt_refcount;
8156
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8157
0
            - offsetof (struct elf_link_hash_entry, size)));
8158
      /* Assume that we have been called by a non-ELF symbol reader.
8159
   This flag is then reset by the code which reads an ELF input
8160
   file.  This ensures that a symbol created by a non-ELF symbol
8161
   reader will have the flag set correctly.  */
8162
0
      ret->non_elf = 1;
8163
0
    }
8164
8165
0
  return entry;
8166
0
}
8167
8168
/* Copy data from an indirect symbol to its direct symbol, hiding the
8169
   old indirect symbol.  Also used for copying flags to a weakdef.  */
8170
8171
void
8172
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8173
          struct elf_link_hash_entry *dir,
8174
          struct elf_link_hash_entry *ind)
8175
0
{
8176
0
  struct elf_link_hash_table *htab;
8177
8178
0
  if (ind->dyn_relocs != NULL)
8179
0
    {
8180
0
      if (dir->dyn_relocs != NULL)
8181
0
  {
8182
0
    struct elf_dyn_relocs **pp;
8183
0
    struct elf_dyn_relocs *p;
8184
8185
    /* Add reloc counts against the indirect sym to the direct sym
8186
       list.  Merge any entries against the same section.  */
8187
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8188
0
      {
8189
0
        struct elf_dyn_relocs *q;
8190
8191
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
8192
0
    if (q->sec == p->sec)
8193
0
      {
8194
0
        q->pc_count += p->pc_count;
8195
0
        q->count += p->count;
8196
0
        *pp = p->next;
8197
0
        break;
8198
0
      }
8199
0
        if (q == NULL)
8200
0
    pp = &p->next;
8201
0
      }
8202
0
    *pp = dir->dyn_relocs;
8203
0
  }
8204
8205
0
      dir->dyn_relocs = ind->dyn_relocs;
8206
0
      ind->dyn_relocs = NULL;
8207
0
    }
8208
8209
  /* Copy down any references that we may have already seen to the
8210
     symbol which just became indirect.  */
8211
8212
0
  if (dir->versioned != versioned_hidden)
8213
0
    dir->ref_dynamic |= ind->ref_dynamic;
8214
0
  dir->ref_regular |= ind->ref_regular;
8215
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8216
0
  dir->non_got_ref |= ind->non_got_ref;
8217
0
  dir->needs_plt |= ind->needs_plt;
8218
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
8219
8220
0
  if (ind->root.type != bfd_link_hash_indirect)
8221
0
    return;
8222
8223
  /* Copy over the global and procedure linkage table refcount entries.
8224
     These may have been already set up by a check_relocs routine.  */
8225
0
  htab = elf_hash_table (info);
8226
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8227
0
    {
8228
0
      if (dir->got.refcount < 0)
8229
0
  dir->got.refcount = 0;
8230
0
      dir->got.refcount += ind->got.refcount;
8231
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8232
0
    }
8233
8234
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8235
0
    {
8236
0
      if (dir->plt.refcount < 0)
8237
0
  dir->plt.refcount = 0;
8238
0
      dir->plt.refcount += ind->plt.refcount;
8239
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8240
0
    }
8241
8242
0
  if (ind->dynindx != -1)
8243
0
    {
8244
0
      if (dir->dynindx != -1)
8245
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8246
0
      dir->dynindx = ind->dynindx;
8247
0
      dir->dynstr_index = ind->dynstr_index;
8248
0
      ind->dynindx = -1;
8249
0
      ind->dynstr_index = 0;
8250
0
    }
8251
0
}
8252
8253
void
8254
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8255
        struct elf_link_hash_entry *h,
8256
        bool force_local)
8257
0
{
8258
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8259
0
  if (h->type != STT_GNU_IFUNC)
8260
0
    {
8261
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8262
0
      h->needs_plt = 0;
8263
0
    }
8264
0
  if (force_local)
8265
0
    {
8266
0
      h->forced_local = 1;
8267
0
      if (h->dynindx != -1)
8268
0
  {
8269
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8270
0
          h->dynstr_index);
8271
0
    h->dynindx = -1;
8272
0
    h->dynstr_index = 0;
8273
0
  }
8274
0
    }
8275
0
}
8276
8277
/* Hide a symbol. */
8278
8279
void
8280
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8281
         struct bfd_link_info *info,
8282
         struct bfd_link_hash_entry *h)
8283
0
{
8284
0
  if (is_elf_hash_table (info->hash))
8285
0
    {
8286
0
      const struct elf_backend_data *bed
8287
0
  = get_elf_backend_data (output_bfd);
8288
0
      struct elf_link_hash_entry *eh
8289
0
  = (struct elf_link_hash_entry *) h;
8290
0
      bed->elf_backend_hide_symbol (info, eh, true);
8291
0
      eh->def_dynamic = 0;
8292
0
      eh->ref_dynamic = 0;
8293
0
      eh->dynamic_def = 0;
8294
0
    }
8295
0
}
8296
8297
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8298
   caller.  */
8299
8300
bool
8301
_bfd_elf_link_hash_table_init
8302
  (struct elf_link_hash_table *table,
8303
   bfd *abfd,
8304
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8305
              struct bfd_hash_table *,
8306
              const char *),
8307
   unsigned int entsize,
8308
   enum elf_target_id target_id)
8309
0
{
8310
0
  bool ret;
8311
0
  int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8312
8313
0
  table->init_got_refcount.refcount = can_refcount - 1;
8314
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8315
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8316
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8317
  /* The first dynamic symbol is a dummy.  */
8318
0
  table->dynsymcount = 1;
8319
8320
0
  ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8321
8322
0
  table->root.type = bfd_link_elf_hash_table;
8323
0
  table->hash_table_id = target_id;
8324
0
  table->target_os = get_elf_backend_data (abfd)->target_os;
8325
8326
0
  return ret;
8327
0
}
8328
8329
/* Create an ELF linker hash table.  */
8330
8331
struct bfd_link_hash_table *
8332
_bfd_elf_link_hash_table_create (bfd *abfd)
8333
0
{
8334
0
  struct elf_link_hash_table *ret;
8335
0
  size_t amt = sizeof (struct elf_link_hash_table);
8336
8337
0
  ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8338
0
  if (ret == NULL)
8339
0
    return NULL;
8340
8341
0
  if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8342
0
               sizeof (struct elf_link_hash_entry),
8343
0
               GENERIC_ELF_DATA))
8344
0
    {
8345
0
      free (ret);
8346
0
      return NULL;
8347
0
    }
8348
0
  ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8349
8350
0
  return &ret->root;
8351
0
}
8352
8353
/* Destroy an ELF linker hash table.  */
8354
8355
void
8356
_bfd_elf_link_hash_table_free (bfd *obfd)
8357
0
{
8358
0
  struct elf_link_hash_table *htab;
8359
8360
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8361
0
  if (htab->dynstr != NULL)
8362
0
    _bfd_elf_strtab_free (htab->dynstr);
8363
0
  _bfd_merge_sections_free (htab->merge_info);
8364
  /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
8365
0
  if (htab->dynamic != NULL)
8366
0
    free (htab->dynamic->contents);
8367
0
  if (htab->first_hash != NULL)
8368
0
    {
8369
0
      bfd_hash_table_free (htab->first_hash);
8370
0
      free (htab->first_hash);
8371
0
    }
8372
0
  _bfd_generic_link_hash_table_free (obfd);
8373
0
}
8374
8375
/* This is a hook for the ELF emulation code in the generic linker to
8376
   tell the backend linker what file name to use for the DT_NEEDED
8377
   entry for a dynamic object.  */
8378
8379
void
8380
bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8381
0
{
8382
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8383
0
      && bfd_get_format (abfd) == bfd_object)
8384
0
    elf_dt_name (abfd) = name;
8385
0
}
8386
8387
int
8388
bfd_elf_get_dyn_lib_class (bfd *abfd)
8389
0
{
8390
0
  int lib_class;
8391
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8392
0
      && bfd_get_format (abfd) == bfd_object)
8393
0
    lib_class = elf_dyn_lib_class (abfd);
8394
0
  else
8395
0
    lib_class = 0;
8396
0
  return lib_class;
8397
0
}
8398
8399
void
8400
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8401
0
{
8402
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8403
0
      && bfd_get_format (abfd) == bfd_object)
8404
0
    elf_dyn_lib_class (abfd) = lib_class;
8405
0
}
8406
8407
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8408
   the linker ELF emulation code.  */
8409
8410
struct bfd_link_needed_list *
8411
bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8412
       struct bfd_link_info *info)
8413
0
{
8414
0
  if (! is_elf_hash_table (info->hash))
8415
0
    return NULL;
8416
0
  return elf_hash_table (info)->needed;
8417
0
}
8418
8419
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8420
   hook for the linker ELF emulation code.  */
8421
8422
struct bfd_link_needed_list *
8423
bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8424
        struct bfd_link_info *info)
8425
0
{
8426
0
  if (! is_elf_hash_table (info->hash))
8427
0
    return NULL;
8428
0
  return elf_hash_table (info)->runpath;
8429
0
}
8430
8431
/* Get the name actually used for a dynamic object for a link.  This
8432
   is the SONAME entry if there is one.  Otherwise, it is the string
8433
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8434
8435
const char *
8436
bfd_elf_get_dt_soname (bfd *abfd)
8437
0
{
8438
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8439
0
      && bfd_get_format (abfd) == bfd_object)
8440
0
    return elf_dt_name (abfd);
8441
0
  return NULL;
8442
0
}
8443
8444
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8445
   the ELF linker emulation code.  */
8446
8447
bool
8448
bfd_elf_get_bfd_needed_list (bfd *abfd,
8449
           struct bfd_link_needed_list **pneeded)
8450
0
{
8451
0
  asection *s;
8452
0
  bfd_byte *dynbuf = NULL;
8453
0
  unsigned int elfsec;
8454
0
  unsigned long shlink;
8455
0
  bfd_byte *extdyn, *extdynend;
8456
0
  size_t extdynsize;
8457
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8458
8459
0
  *pneeded = NULL;
8460
8461
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8462
0
      || bfd_get_format (abfd) != bfd_object)
8463
0
    return true;
8464
8465
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8466
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8467
0
    return true;
8468
8469
0
  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8470
0
    goto error_return;
8471
8472
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8473
0
  if (elfsec == SHN_BAD)
8474
0
    goto error_return;
8475
8476
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8477
8478
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8479
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8480
8481
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8482
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8483
0
       extdyn += extdynsize)
8484
0
    {
8485
0
      Elf_Internal_Dyn dyn;
8486
8487
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8488
8489
0
      if (dyn.d_tag == DT_NULL)
8490
0
  break;
8491
8492
0
      if (dyn.d_tag == DT_NEEDED)
8493
0
  {
8494
0
    const char *string;
8495
0
    struct bfd_link_needed_list *l;
8496
0
    unsigned int tagv = dyn.d_un.d_val;
8497
0
    size_t amt;
8498
8499
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8500
0
    if (string == NULL)
8501
0
      goto error_return;
8502
8503
0
    amt = sizeof *l;
8504
0
    l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8505
0
    if (l == NULL)
8506
0
      goto error_return;
8507
8508
0
    l->by = abfd;
8509
0
    l->name = string;
8510
0
    l->next = *pneeded;
8511
0
    *pneeded = l;
8512
0
  }
8513
0
    }
8514
8515
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8516
8517
0
  return true;
8518
8519
0
 error_return:
8520
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8521
0
  return false;
8522
0
}
8523
8524
struct elf_symbuf_symbol
8525
{
8526
  unsigned long st_name;  /* Symbol name, index in string tbl */
8527
  unsigned char st_info;  /* Type and binding attributes */
8528
  unsigned char st_other; /* Visibilty, and target specific */
8529
};
8530
8531
struct elf_symbuf_head
8532
{
8533
  struct elf_symbuf_symbol *ssym;
8534
  size_t count;
8535
  unsigned int st_shndx;
8536
};
8537
8538
struct elf_symbol
8539
{
8540
  union
8541
    {
8542
      Elf_Internal_Sym *isym;
8543
      struct elf_symbuf_symbol *ssym;
8544
      void *p;
8545
    } u;
8546
  const char *name;
8547
};
8548
8549
/* Sort references to symbols by ascending section number.  */
8550
8551
static int
8552
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8553
0
{
8554
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8555
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8556
8557
0
  if (s1->st_shndx != s2->st_shndx)
8558
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8559
  /* Final sort by the address of the sym in the symbuf ensures
8560
     a stable sort.  */
8561
0
  if (s1 != s2)
8562
0
    return s1 > s2 ? 1 : -1;
8563
0
  return 0;
8564
0
}
8565
8566
static int
8567
elf_sym_name_compare (const void *arg1, const void *arg2)
8568
0
{
8569
0
  const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8570
0
  const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8571
0
  int ret = strcmp (s1->name, s2->name);
8572
0
  if (ret != 0)
8573
0
    return ret;
8574
0
  if (s1->u.p != s2->u.p)
8575
0
    return s1->u.p > s2->u.p ? 1 : -1;
8576
0
  return 0;
8577
0
}
8578
8579
static struct elf_symbuf_head *
8580
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8581
0
{
8582
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8583
0
  struct elf_symbuf_symbol *ssym;
8584
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8585
0
  size_t i, shndx_count, total_size, amt;
8586
8587
0
  amt = symcount * sizeof (*indbuf);
8588
0
  indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8589
0
  if (indbuf == NULL)
8590
0
    return NULL;
8591
8592
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8593
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8594
0
      *ind++ = &isymbuf[i];
8595
0
  indbufend = ind;
8596
8597
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8598
0
   elf_sort_elf_symbol);
8599
8600
0
  shndx_count = 0;
8601
0
  if (indbufend > indbuf)
8602
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8603
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8604
0
  shndx_count++;
8605
8606
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8607
0
    + (indbufend - indbuf) * sizeof (*ssym));
8608
0
  ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8609
0
  if (ssymbuf == NULL)
8610
0
    {
8611
0
      free (indbuf);
8612
0
      return NULL;
8613
0
    }
8614
8615
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8616
0
  ssymbuf->ssym = NULL;
8617
0
  ssymbuf->count = shndx_count;
8618
0
  ssymbuf->st_shndx = 0;
8619
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8620
0
    {
8621
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8622
0
  {
8623
0
    ssymhead++;
8624
0
    ssymhead->ssym = ssym;
8625
0
    ssymhead->count = 0;
8626
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8627
0
  }
8628
0
      ssym->st_name = (*ind)->st_name;
8629
0
      ssym->st_info = (*ind)->st_info;
8630
0
      ssym->st_other = (*ind)->st_other;
8631
0
      ssymhead->count++;
8632
0
    }
8633
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8634
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8635
8636
0
  free (indbuf);
8637
0
  return ssymbuf;
8638
0
}
8639
8640
/* Check if 2 sections define the same set of local and global
8641
   symbols.  */
8642
8643
static bool
8644
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8645
           struct bfd_link_info *info)
8646
0
{
8647
0
  bfd *bfd1, *bfd2;
8648
0
  const struct elf_backend_data *bed1, *bed2;
8649
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8650
0
  size_t symcount1, symcount2;
8651
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8652
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8653
0
  Elf_Internal_Sym *isym, *isymend;
8654
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8655
0
  size_t count1, count2, sec_count1, sec_count2, i;
8656
0
  unsigned int shndx1, shndx2;
8657
0
  bool result;
8658
0
  bool ignore_section_symbol_p;
8659
8660
0
  bfd1 = sec1->owner;
8661
0
  bfd2 = sec2->owner;
8662
8663
  /* Both sections have to be in ELF.  */
8664
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8665
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8666
0
    return false;
8667
8668
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8669
0
    return false;
8670
8671
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8672
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8673
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8674
0
    return false;
8675
8676
0
  bed1 = get_elf_backend_data (bfd1);
8677
0
  bed2 = get_elf_backend_data (bfd2);
8678
0
  hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8679
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8680
0
  hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8681
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8682
8683
0
  if (symcount1 == 0 || symcount2 == 0)
8684
0
    return false;
8685
8686
0
  result = false;
8687
0
  isymbuf1 = NULL;
8688
0
  isymbuf2 = NULL;
8689
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8690
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8691
8692
  /* Ignore section symbols only when matching non-debugging sections
8693
     or linkonce section with comdat section.  */
8694
0
  ignore_section_symbol_p
8695
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8696
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8697
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8698
8699
0
  if (ssymbuf1 == NULL)
8700
0
    {
8701
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8702
0
               NULL, NULL, NULL);
8703
0
      if (isymbuf1 == NULL)
8704
0
  goto done;
8705
8706
0
      if (info != NULL && !info->reduce_memory_overheads)
8707
0
  {
8708
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8709
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8710
0
  }
8711
0
    }
8712
8713
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8714
0
    {
8715
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8716
0
               NULL, NULL, NULL);
8717
0
      if (isymbuf2 == NULL)
8718
0
  goto done;
8719
8720
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8721
0
  {
8722
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8723
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8724
0
  }
8725
0
    }
8726
8727
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8728
0
    {
8729
      /* Optimized faster version.  */
8730
0
      size_t lo, hi, mid;
8731
0
      struct elf_symbol *symp;
8732
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8733
8734
0
      lo = 0;
8735
0
      hi = ssymbuf1->count;
8736
0
      ssymbuf1++;
8737
0
      count1 = 0;
8738
0
      sec_count1 = 0;
8739
0
      while (lo < hi)
8740
0
  {
8741
0
    mid = (lo + hi) / 2;
8742
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8743
0
      hi = mid;
8744
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8745
0
      lo = mid + 1;
8746
0
    else
8747
0
      {
8748
0
        count1 = ssymbuf1[mid].count;
8749
0
        ssymbuf1 += mid;
8750
0
        break;
8751
0
      }
8752
0
  }
8753
0
      if (ignore_section_symbol_p)
8754
0
  {
8755
0
    for (i = 0; i < count1; i++)
8756
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8757
0
        sec_count1++;
8758
0
    count1 -= sec_count1;
8759
0
  }
8760
8761
0
      lo = 0;
8762
0
      hi = ssymbuf2->count;
8763
0
      ssymbuf2++;
8764
0
      count2 = 0;
8765
0
      sec_count2 = 0;
8766
0
      while (lo < hi)
8767
0
  {
8768
0
    mid = (lo + hi) / 2;
8769
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8770
0
      hi = mid;
8771
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8772
0
      lo = mid + 1;
8773
0
    else
8774
0
      {
8775
0
        count2 = ssymbuf2[mid].count;
8776
0
        ssymbuf2 += mid;
8777
0
        break;
8778
0
      }
8779
0
  }
8780
0
      if (ignore_section_symbol_p)
8781
0
  {
8782
0
    for (i = 0; i < count2; i++)
8783
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8784
0
        sec_count2++;
8785
0
    count2 -= sec_count2;
8786
0
  }
8787
8788
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8789
0
  goto done;
8790
8791
0
      symtable1
8792
0
  = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8793
0
      symtable2
8794
0
  = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8795
0
      if (symtable1 == NULL || symtable2 == NULL)
8796
0
  goto done;
8797
8798
0
      symp = symtable1;
8799
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8800
0
     ssym < ssymend; ssym++)
8801
0
  if (sec_count1 == 0
8802
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8803
0
    {
8804
0
      symp->u.ssym = ssym;
8805
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8806
0
                hdr1->sh_link,
8807
0
                ssym->st_name);
8808
0
      symp++;
8809
0
    }
8810
8811
0
      symp = symtable2;
8812
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8813
0
     ssym < ssymend; ssym++)
8814
0
  if (sec_count2 == 0
8815
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8816
0
    {
8817
0
      symp->u.ssym = ssym;
8818
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8819
0
                hdr2->sh_link,
8820
0
                ssym->st_name);
8821
0
      symp++;
8822
0
    }
8823
8824
      /* Sort symbol by name.  */
8825
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8826
0
       elf_sym_name_compare);
8827
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8828
0
       elf_sym_name_compare);
8829
8830
0
      for (i = 0; i < count1; i++)
8831
  /* Two symbols must have the same binding, type and name.  */
8832
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8833
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8834
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8835
0
    goto done;
8836
8837
0
      result = true;
8838
0
      goto done;
8839
0
    }
8840
8841
0
  symtable1 = (struct elf_symbol *)
8842
0
      bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8843
0
  symtable2 = (struct elf_symbol *)
8844
0
      bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8845
0
  if (symtable1 == NULL || symtable2 == NULL)
8846
0
    goto done;
8847
8848
  /* Count definitions in the section.  */
8849
0
  count1 = 0;
8850
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8851
0
    if (isym->st_shndx == shndx1
8852
0
  && (!ignore_section_symbol_p
8853
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8854
0
      symtable1[count1++].u.isym = isym;
8855
8856
0
  count2 = 0;
8857
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8858
0
    if (isym->st_shndx == shndx2
8859
0
  && (!ignore_section_symbol_p
8860
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8861
0
      symtable2[count2++].u.isym = isym;
8862
8863
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8864
0
    goto done;
8865
8866
0
  for (i = 0; i < count1; i++)
8867
0
    symtable1[i].name
8868
0
      = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8869
0
           symtable1[i].u.isym->st_name);
8870
8871
0
  for (i = 0; i < count2; i++)
8872
0
    symtable2[i].name
8873
0
      = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8874
0
           symtable2[i].u.isym->st_name);
8875
8876
  /* Sort symbol by name.  */
8877
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
8878
0
   elf_sym_name_compare);
8879
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
8880
0
   elf_sym_name_compare);
8881
8882
0
  for (i = 0; i < count1; i++)
8883
    /* Two symbols must have the same binding, type and name.  */
8884
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8885
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8886
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8887
0
      goto done;
8888
8889
0
  result = true;
8890
8891
0
 done:
8892
0
  free (symtable1);
8893
0
  free (symtable2);
8894
0
  free (isymbuf1);
8895
0
  free (isymbuf2);
8896
8897
0
  return result;
8898
0
}
8899
8900
/* Return TRUE if 2 section types are compatible.  */
8901
8902
bool
8903
_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8904
         bfd *bbfd, const asection *bsec)
8905
0
{
8906
0
  if (asec == NULL
8907
0
      || bsec == NULL
8908
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
8909
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
8910
0
    return true;
8911
8912
0
  return elf_section_type (asec) == elf_section_type (bsec);
8913
0
}
8914

8915
/* Final phase of ELF linker.  */
8916
8917
/* A structure we use to avoid passing large numbers of arguments.  */
8918
8919
struct elf_final_link_info
8920
{
8921
  /* General link information.  */
8922
  struct bfd_link_info *info;
8923
  /* Output BFD.  */
8924
  bfd *output_bfd;
8925
  /* Symbol string table.  */
8926
  struct elf_strtab_hash *symstrtab;
8927
  /* .hash section.  */
8928
  asection *hash_sec;
8929
  /* symbol version section (.gnu.version).  */
8930
  asection *symver_sec;
8931
  /* Buffer large enough to hold contents of any section.  */
8932
  bfd_byte *contents;
8933
  /* Buffer large enough to hold external relocs of any section.  */
8934
  void *external_relocs;
8935
  /* Buffer large enough to hold internal relocs of any section.  */
8936
  Elf_Internal_Rela *internal_relocs;
8937
  /* Buffer large enough to hold external local symbols of any input
8938
     BFD.  */
8939
  bfd_byte *external_syms;
8940
  /* And a buffer for symbol section indices.  */
8941
  Elf_External_Sym_Shndx *locsym_shndx;
8942
  /* Buffer large enough to hold internal local symbols of any input
8943
     BFD.  */
8944
  Elf_Internal_Sym *internal_syms;
8945
  /* Array large enough to hold a symbol index for each local symbol
8946
     of any input BFD.  */
8947
  long *indices;
8948
  /* Array large enough to hold a section pointer for each local
8949
     symbol of any input BFD.  */
8950
  asection **sections;
8951
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
8952
  Elf_External_Sym_Shndx *symshndxbuf;
8953
  /* Number of STT_FILE syms seen.  */
8954
  size_t filesym_count;
8955
  /* Local symbol hash table.  */
8956
  struct bfd_hash_table local_hash_table;
8957
};
8958
8959
struct local_hash_entry
8960
{
8961
  /* Base hash table entry structure.  */
8962
  struct bfd_hash_entry root;
8963
  /* Size of the local symbol name.  */
8964
  size_t size;
8965
  /* Number of the duplicated local symbol names.  */
8966
  long count;
8967
};
8968
8969
/* Create an entry in the local symbol hash table.  */
8970
8971
static struct bfd_hash_entry *
8972
local_hash_newfunc (struct bfd_hash_entry *entry,
8973
        struct bfd_hash_table *table,
8974
        const char *string)
8975
0
{
8976
8977
  /* Allocate the structure if it has not already been allocated by a
8978
     subclass.  */
8979
0
  if (entry == NULL)
8980
0
    {
8981
0
      entry = bfd_hash_allocate (table,
8982
0
         sizeof (struct local_hash_entry));
8983
0
      if (entry == NULL)
8984
0
        return entry;
8985
0
    }
8986
8987
  /* Call the allocation method of the superclass.  */
8988
0
  entry = bfd_hash_newfunc (entry, table, string);
8989
0
  if (entry != NULL)
8990
0
    {
8991
0
      ((struct local_hash_entry *) entry)->count = 0;
8992
0
      ((struct local_hash_entry *) entry)->size = 0;
8993
0
    }
8994
8995
0
  return entry;
8996
0
}
8997
8998
/* This struct is used to pass information to elf_link_output_extsym.  */
8999
9000
struct elf_outext_info
9001
{
9002
  bool failed;
9003
  bool localsyms;
9004
  bool file_sym_done;
9005
  struct elf_final_link_info *flinfo;
9006
};
9007
9008
9009
/* Support for evaluating a complex relocation.
9010
9011
   Complex relocations are generalized, self-describing relocations.  The
9012
   implementation of them consists of two parts: complex symbols, and the
9013
   relocations themselves.
9014
9015
   The relocations use a reserved elf-wide relocation type code (R_RELC
9016
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
9017
   information (start bit, end bit, word width, etc) into the addend.  This
9018
   information is extracted from CGEN-generated operand tables within gas.
9019
9020
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9021
   internal) representing prefix-notation expressions, including but not
9022
   limited to those sorts of expressions normally encoded as addends in the
9023
   addend field.  The symbol mangling format is:
9024
9025
   <node> := <literal>
9026
    |  <unary-operator> ':' <node>
9027
    |  <binary-operator> ':' <node> ':' <node>
9028
    ;
9029
9030
   <literal> := 's' <digits=N> ':' <N character symbol name>
9031
       |  'S' <digits=N> ':' <N character section name>
9032
       |  '#' <hexdigits>
9033
       ;
9034
9035
   <binary-operator> := as in C
9036
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
9037
9038
static void
9039
set_symbol_value (bfd *bfd_with_globals,
9040
      Elf_Internal_Sym *isymbuf,
9041
      size_t locsymcount,
9042
      size_t symidx,
9043
      bfd_vma val)
9044
0
{
9045
0
  struct elf_link_hash_entry **sym_hashes;
9046
0
  struct elf_link_hash_entry *h;
9047
0
  size_t extsymoff = locsymcount;
9048
9049
0
  if (symidx < locsymcount)
9050
0
    {
9051
0
      Elf_Internal_Sym *sym;
9052
9053
0
      sym = isymbuf + symidx;
9054
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9055
0
  {
9056
    /* It is a local symbol: move it to the
9057
       "absolute" section and give it a value.  */
9058
0
    sym->st_shndx = SHN_ABS;
9059
0
    sym->st_value = val;
9060
0
    return;
9061
0
  }
9062
0
      BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
9063
0
      extsymoff = 0;
9064
0
    }
9065
9066
  /* It is a global symbol: set its link type
9067
     to "defined" and give it a value.  */
9068
9069
0
  sym_hashes = elf_sym_hashes (bfd_with_globals);
9070
0
  h = sym_hashes [symidx - extsymoff];
9071
0
  while (h->root.type == bfd_link_hash_indirect
9072
0
   || h->root.type == bfd_link_hash_warning)
9073
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
9074
0
  h->root.type = bfd_link_hash_defined;
9075
0
  h->root.u.def.value = val;
9076
0
  h->root.u.def.section = bfd_abs_section_ptr;
9077
0
}
9078
9079
static bool
9080
resolve_symbol (const char *name,
9081
    bfd *input_bfd,
9082
    struct elf_final_link_info *flinfo,
9083
    bfd_vma *result,
9084
    Elf_Internal_Sym *isymbuf,
9085
    size_t locsymcount)
9086
0
{
9087
0
  Elf_Internal_Sym *sym;
9088
0
  struct bfd_link_hash_entry *global_entry;
9089
0
  const char *candidate = NULL;
9090
0
  Elf_Internal_Shdr *symtab_hdr;
9091
0
  size_t i;
9092
9093
0
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9094
9095
0
  for (i = 0; i < locsymcount; ++ i)
9096
0
    {
9097
0
      sym = isymbuf + i;
9098
9099
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9100
0
  continue;
9101
9102
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
9103
0
               symtab_hdr->sh_link,
9104
0
               sym->st_name);
9105
#ifdef DEBUG
9106
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9107
        name, candidate, (unsigned long) sym->st_value);
9108
#endif
9109
0
      if (candidate && strcmp (candidate, name) == 0)
9110
0
  {
9111
0
    asection *sec = flinfo->sections [i];
9112
9113
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9114
0
    *result += sec->output_offset + sec->output_section->vma;
9115
#ifdef DEBUG
9116
    printf ("Found symbol with value %8.8lx\n",
9117
      (unsigned long) *result);
9118
#endif
9119
0
    return true;
9120
0
  }
9121
0
    }
9122
9123
  /* Hmm, haven't found it yet. perhaps it is a global.  */
9124
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9125
0
               false, false, true);
9126
0
  if (!global_entry)
9127
0
    return false;
9128
9129
0
  if (global_entry->type == bfd_link_hash_defined
9130
0
      || global_entry->type == bfd_link_hash_defweak)
9131
0
    {
9132
0
      *result = (global_entry->u.def.value
9133
0
     + global_entry->u.def.section->output_section->vma
9134
0
     + global_entry->u.def.section->output_offset);
9135
#ifdef DEBUG
9136
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9137
        global_entry->root.string, (unsigned long) *result);
9138
#endif
9139
0
      return true;
9140
0
    }
9141
9142
0
  return false;
9143
0
}
9144
9145
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
9146
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
9147
   names like "foo.end" which is the end address of section "foo".  */
9148
9149
static bool
9150
resolve_section (const char *name,
9151
     asection *sections,
9152
     bfd_vma *result,
9153
     bfd * abfd)
9154
0
{
9155
0
  asection *curr;
9156
0
  unsigned int len;
9157
9158
0
  for (curr = sections; curr; curr = curr->next)
9159
0
    if (strcmp (curr->name, name) == 0)
9160
0
      {
9161
0
  *result = curr->vma;
9162
0
  return true;
9163
0
      }
9164
9165
  /* Hmm. still haven't found it. try pseudo-section names.  */
9166
  /* FIXME: This could be coded more efficiently...  */
9167
0
  for (curr = sections; curr; curr = curr->next)
9168
0
    {
9169
0
      len = strlen (curr->name);
9170
0
      if (len > strlen (name))
9171
0
  continue;
9172
9173
0
      if (strncmp (curr->name, name, len) == 0)
9174
0
  {
9175
0
    if (startswith (name + len, ".end"))
9176
0
      {
9177
0
        *result = (curr->vma
9178
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
9179
0
        return true;
9180
0
      }
9181
9182
    /* Insert more pseudo-section names here, if you like.  */
9183
0
  }
9184
0
    }
9185
9186
0
  return false;
9187
0
}
9188
9189
static void
9190
undefined_reference (const char *reftype, const char *name)
9191
0
{
9192
  /* xgettext:c-format */
9193
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9194
0
          reftype, name);
9195
0
  bfd_set_error (bfd_error_bad_value);
9196
0
}
9197
9198
static bool
9199
eval_symbol (bfd_vma *result,
9200
       const char **symp,
9201
       bfd *input_bfd,
9202
       struct elf_final_link_info *flinfo,
9203
       bfd_vma dot,
9204
       Elf_Internal_Sym *isymbuf,
9205
       size_t locsymcount,
9206
       int signed_p)
9207
0
{
9208
0
  size_t len;
9209
0
  size_t symlen;
9210
0
  bfd_vma a;
9211
0
  bfd_vma b;
9212
0
  char symbuf[4096];
9213
0
  const char *sym = *symp;
9214
0
  const char *symend;
9215
0
  bool symbol_is_section = false;
9216
9217
0
  len = strlen (sym);
9218
0
  symend = sym + len;
9219
9220
0
  if (len < 1 || len > sizeof (symbuf))
9221
0
    {
9222
0
      bfd_set_error (bfd_error_invalid_operation);
9223
0
      return false;
9224
0
    }
9225
9226
0
  switch (* sym)
9227
0
    {
9228
0
    case '.':
9229
0
      *result = dot;
9230
0
      *symp = sym + 1;
9231
0
      return true;
9232
9233
0
    case '#':
9234
0
      ++sym;
9235
0
      *result = strtoul (sym, (char **) symp, 16);
9236
0
      return true;
9237
9238
0
    case 'S':
9239
0
      symbol_is_section = true;
9240
      /* Fall through.  */
9241
0
    case 's':
9242
0
      ++sym;
9243
0
      symlen = strtol (sym, (char **) symp, 10);
9244
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9245
9246
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9247
0
  {
9248
0
    bfd_set_error (bfd_error_invalid_operation);
9249
0
    return false;
9250
0
  }
9251
9252
0
      memcpy (symbuf, sym, symlen);
9253
0
      symbuf[symlen] = '\0';
9254
0
      *symp = sym + symlen;
9255
9256
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9257
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9258
   interpretation here; section means "try section first", not "must be a
9259
   section", and likewise with symbol.  */
9260
9261
0
      if (symbol_is_section)
9262
0
  {
9263
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9264
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9265
0
          isymbuf, locsymcount))
9266
0
      {
9267
0
        undefined_reference ("section", symbuf);
9268
0
        return false;
9269
0
      }
9270
0
  }
9271
0
      else
9272
0
  {
9273
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9274
0
             isymbuf, locsymcount)
9275
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9276
0
           result, input_bfd))
9277
0
      {
9278
0
        undefined_reference ("symbol", symbuf);
9279
0
        return false;
9280
0
      }
9281
0
  }
9282
9283
0
      return true;
9284
9285
      /* All that remains are operators.  */
9286
9287
0
#define UNARY_OP(op)            \
9288
0
  if (startswith (sym, #op))         \
9289
0
    {               \
9290
0
      sym += strlen (#op);          \
9291
0
      if (*sym == ':')           \
9292
0
  ++sym;             \
9293
0
      *symp = sym;            \
9294
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9295
0
      isymbuf, locsymcount, signed_p)) \
9296
0
  return false;           \
9297
0
      if (signed_p)           \
9298
0
  *result = op ((bfd_signed_vma) a);     \
9299
0
      else              \
9300
0
  *result = op a;           \
9301
0
      return true;            \
9302
0
    }
9303
9304
0
#define BINARY_OP_HEAD(op)          \
9305
0
  if (startswith (sym, #op))         \
9306
0
    {               \
9307
0
      sym += strlen (#op);          \
9308
0
      if (*sym == ':')           \
9309
0
  ++sym;             \
9310
0
      *symp = sym;            \
9311
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9312
0
      isymbuf, locsymcount, signed_p)) \
9313
0
  return false;           \
9314
0
      ++*symp;              \
9315
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9316
0
      isymbuf, locsymcount, signed_p)) \
9317
0
  return false;
9318
0
#define BINARY_OP_TAIL(op)          \
9319
0
      if (signed_p)           \
9320
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9321
0
      else              \
9322
0
  *result = a op b;         \
9323
0
      return true;            \
9324
0
    }
9325
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9326
9327
0
    default:
9328
0
      UNARY_OP  (0-);
9329
0
      BINARY_OP_HEAD (<<);
9330
0
      if (b >= sizeof (a) * CHAR_BIT)
9331
0
  {
9332
0
    *result = 0;
9333
0
    return true;
9334
0
  }
9335
0
      signed_p = 0;
9336
0
      BINARY_OP_TAIL (<<);
9337
0
      BINARY_OP_HEAD (>>);
9338
0
      if (b >= sizeof (a) * CHAR_BIT)
9339
0
  {
9340
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9341
0
    return true;
9342
0
  }
9343
0
      BINARY_OP_TAIL (>>);
9344
0
      BINARY_OP (==);
9345
0
      BINARY_OP (!=);
9346
0
      BINARY_OP (<=);
9347
0
      BINARY_OP (>=);
9348
0
      BINARY_OP (&&);
9349
0
      BINARY_OP (||);
9350
0
      UNARY_OP  (~);
9351
0
      UNARY_OP  (!);
9352
0
      BINARY_OP (*);
9353
0
      BINARY_OP_HEAD (/);
9354
0
      if (b == 0)
9355
0
  {
9356
0
    _bfd_error_handler (_("division by zero"));
9357
0
    bfd_set_error (bfd_error_bad_value);
9358
0
    return false;
9359
0
  }
9360
0
      BINARY_OP_TAIL (/);
9361
0
      BINARY_OP_HEAD (%);
9362
0
      if (b == 0)
9363
0
  {
9364
0
    _bfd_error_handler (_("division by zero"));
9365
0
    bfd_set_error (bfd_error_bad_value);
9366
0
    return false;
9367
0
  }
9368
0
      BINARY_OP_TAIL (%);
9369
0
      BINARY_OP (^);
9370
0
      BINARY_OP (|);
9371
0
      BINARY_OP (&);
9372
0
      BINARY_OP (+);
9373
0
      BINARY_OP (-);
9374
0
      BINARY_OP (<);
9375
0
      BINARY_OP (>);
9376
0
#undef UNARY_OP
9377
0
#undef BINARY_OP
9378
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9379
0
      bfd_set_error (bfd_error_invalid_operation);
9380
0
      return false;
9381
0
    }
9382
0
}
9383
9384
static void
9385
put_value (bfd_vma size,
9386
     unsigned long chunksz,
9387
     bfd *input_bfd,
9388
     bfd_vma x,
9389
     bfd_byte *location)
9390
0
{
9391
0
  location += (size - chunksz);
9392
9393
0
  for (; size; size -= chunksz, location -= chunksz)
9394
0
    {
9395
0
      switch (chunksz)
9396
0
  {
9397
0
  case 1:
9398
0
    bfd_put_8 (input_bfd, x, location);
9399
0
    x >>= 8;
9400
0
    break;
9401
0
  case 2:
9402
0
    bfd_put_16 (input_bfd, x, location);
9403
0
    x >>= 16;
9404
0
    break;
9405
0
  case 4:
9406
0
    bfd_put_32 (input_bfd, x, location);
9407
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9408
0
    x >>= 16;
9409
0
    x >>= 16;
9410
0
    break;
9411
0
#ifdef BFD64
9412
0
  case 8:
9413
0
    bfd_put_64 (input_bfd, x, location);
9414
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9415
0
    x >>= 32;
9416
0
    x >>= 32;
9417
0
    break;
9418
0
#endif
9419
0
  default:
9420
0
    abort ();
9421
0
    break;
9422
0
  }
9423
0
    }
9424
0
}
9425
9426
static bfd_vma
9427
get_value (bfd_vma size,
9428
     unsigned long chunksz,
9429
     bfd *input_bfd,
9430
     bfd_byte *location)
9431
0
{
9432
0
  int shift;
9433
0
  bfd_vma x = 0;
9434
9435
  /* Sanity checks.  */
9436
0
  BFD_ASSERT (chunksz <= sizeof (x)
9437
0
        && size >= chunksz
9438
0
        && chunksz != 0
9439
0
        && (size % chunksz) == 0
9440
0
        && input_bfd != NULL
9441
0
        && location != NULL);
9442
9443
0
  if (chunksz == sizeof (x))
9444
0
    {
9445
0
      BFD_ASSERT (size == chunksz);
9446
9447
      /* Make sure that we do not perform an undefined shift operation.
9448
   We know that size == chunksz so there will only be one iteration
9449
   of the loop below.  */
9450
0
      shift = 0;
9451
0
    }
9452
0
  else
9453
0
    shift = 8 * chunksz;
9454
9455
0
  for (; size; size -= chunksz, location += chunksz)
9456
0
    {
9457
0
      switch (chunksz)
9458
0
  {
9459
0
  case 1:
9460
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9461
0
    break;
9462
0
  case 2:
9463
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9464
0
    break;
9465
0
  case 4:
9466
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9467
0
    break;
9468
0
#ifdef BFD64
9469
0
  case 8:
9470
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9471
0
    break;
9472
0
#endif
9473
0
  default:
9474
0
    abort ();
9475
0
  }
9476
0
    }
9477
0
  return x;
9478
0
}
9479
9480
static void
9481
decode_complex_addend (unsigned long *start,   /* in bits */
9482
           unsigned long *oplen,   /* in bits */
9483
           unsigned long *len,     /* in bits */
9484
           unsigned long *wordsz,  /* in bytes */
9485
           unsigned long *chunksz, /* in bytes */
9486
           unsigned long *lsb0_p,
9487
           unsigned long *signed_p,
9488
           unsigned long *trunc_p,
9489
           unsigned long encoded)
9490
0
{
9491
0
  * start     =  encoded  & 0x3F;
9492
0
  * len       = (encoded >>  6) & 0x3F;
9493
0
  * oplen     = (encoded >> 12) & 0x3F;
9494
0
  * wordsz    = (encoded >> 18) & 0xF;
9495
0
  * chunksz   = (encoded >> 22) & 0xF;
9496
0
  * lsb0_p    = (encoded >> 27) & 1;
9497
0
  * signed_p  = (encoded >> 28) & 1;
9498
0
  * trunc_p   = (encoded >> 29) & 1;
9499
0
}
9500
9501
bfd_reloc_status_type
9502
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9503
            asection *input_section,
9504
            bfd_byte *contents,
9505
            Elf_Internal_Rela *rel,
9506
            bfd_vma relocation)
9507
0
{
9508
0
  bfd_vma shift, x, mask;
9509
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9510
0
  bfd_reloc_status_type r;
9511
0
  bfd_size_type octets;
9512
9513
  /*  Perform this reloc, since it is complex.
9514
      (this is not to say that it necessarily refers to a complex
9515
      symbol; merely that it is a self-describing CGEN based reloc.
9516
      i.e. the addend has the complete reloc information (bit start, end,
9517
      word size, etc) encoded within it.).  */
9518
9519
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9520
0
       &chunksz, &lsb0_p, &signed_p,
9521
0
       &trunc_p, rel->r_addend);
9522
9523
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9524
9525
0
  if (lsb0_p)
9526
0
    shift = (start + 1) - len;
9527
0
  else
9528
0
    shift = (8 * wordsz) - (start + len);
9529
9530
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9531
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9532
9533
#ifdef DEBUG
9534
  printf ("Doing complex reloc: "
9535
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9536
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9537
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9538
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9539
    oplen, (unsigned long) x, (unsigned long) mask,
9540
    (unsigned long) relocation);
9541
#endif
9542
9543
0
  r = bfd_reloc_ok;
9544
0
  if (! trunc_p)
9545
    /* Now do an overflow check.  */
9546
0
    r = bfd_check_overflow ((signed_p
9547
0
           ? complain_overflow_signed
9548
0
           : complain_overflow_unsigned),
9549
0
          len, 0, (8 * wordsz),
9550
0
          relocation);
9551
9552
  /* Do the deed.  */
9553
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9554
9555
#ifdef DEBUG
9556
  printf ("           relocation: %8.8lx\n"
9557
    "         shifted mask: %8.8lx\n"
9558
    " shifted/masked reloc: %8.8lx\n"
9559
    "               result: %8.8lx\n",
9560
    (unsigned long) relocation, (unsigned long) (mask << shift),
9561
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9562
#endif
9563
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9564
0
  return r;
9565
0
}
9566
9567
/* Functions to read r_offset from external (target order) reloc
9568
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9569
   know the value is aligned.  */
9570
9571
static bfd_vma
9572
ext32l_r_offset (const void *p)
9573
0
{
9574
0
  union aligned32
9575
0
  {
9576
0
    uint32_t v;
9577
0
    unsigned char c[4];
9578
0
  };
9579
0
  const union aligned32 *a
9580
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9581
9582
0
  uint32_t aval = (  (uint32_t) a->c[0]
9583
0
       | (uint32_t) a->c[1] << 8
9584
0
       | (uint32_t) a->c[2] << 16
9585
0
       | (uint32_t) a->c[3] << 24);
9586
0
  return aval;
9587
0
}
9588
9589
static bfd_vma
9590
ext32b_r_offset (const void *p)
9591
0
{
9592
0
  union aligned32
9593
0
  {
9594
0
    uint32_t v;
9595
0
    unsigned char c[4];
9596
0
  };
9597
0
  const union aligned32 *a
9598
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9599
9600
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9601
0
       | (uint32_t) a->c[1] << 16
9602
0
       | (uint32_t) a->c[2] << 8
9603
0
       | (uint32_t) a->c[3]);
9604
0
  return aval;
9605
0
}
9606
9607
static bfd_vma
9608
ext64l_r_offset (const void *p)
9609
0
{
9610
0
  union aligned64
9611
0
  {
9612
0
    uint64_t v;
9613
0
    unsigned char c[8];
9614
0
  };
9615
0
  const union aligned64 *a
9616
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9617
9618
0
  uint64_t aval = (  (uint64_t) a->c[0]
9619
0
       | (uint64_t) a->c[1] << 8
9620
0
       | (uint64_t) a->c[2] << 16
9621
0
       | (uint64_t) a->c[3] << 24
9622
0
       | (uint64_t) a->c[4] << 32
9623
0
       | (uint64_t) a->c[5] << 40
9624
0
       | (uint64_t) a->c[6] << 48
9625
0
       | (uint64_t) a->c[7] << 56);
9626
0
  return aval;
9627
0
}
9628
9629
static bfd_vma
9630
ext64b_r_offset (const void *p)
9631
0
{
9632
0
  union aligned64
9633
0
  {
9634
0
    uint64_t v;
9635
0
    unsigned char c[8];
9636
0
  };
9637
0
  const union aligned64 *a
9638
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9639
9640
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9641
0
       | (uint64_t) a->c[1] << 48
9642
0
       | (uint64_t) a->c[2] << 40
9643
0
       | (uint64_t) a->c[3] << 32
9644
0
       | (uint64_t) a->c[4] << 24
9645
0
       | (uint64_t) a->c[5] << 16
9646
0
       | (uint64_t) a->c[6] << 8
9647
0
       | (uint64_t) a->c[7]);
9648
0
  return aval;
9649
0
}
9650
9651
/* When performing a relocatable link, the input relocations are
9652
   preserved.  But, if they reference global symbols, the indices
9653
   referenced must be updated.  Update all the relocations found in
9654
   RELDATA.  */
9655
9656
static bool
9657
elf_link_adjust_relocs (bfd *abfd,
9658
      asection *sec,
9659
      struct bfd_elf_section_reloc_data *reldata,
9660
      bool sort,
9661
      struct bfd_link_info *info)
9662
0
{
9663
0
  unsigned int i;
9664
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9665
0
  bfd_byte *erela;
9666
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9667
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9668
0
  bfd_vma r_type_mask;
9669
0
  int r_sym_shift;
9670
0
  unsigned int count = reldata->count;
9671
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9672
9673
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9674
0
    {
9675
0
      swap_in = bed->s->swap_reloc_in;
9676
0
      swap_out = bed->s->swap_reloc_out;
9677
0
    }
9678
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9679
0
    {
9680
0
      swap_in = bed->s->swap_reloca_in;
9681
0
      swap_out = bed->s->swap_reloca_out;
9682
0
    }
9683
0
  else
9684
0
    abort ();
9685
9686
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9687
0
    abort ();
9688
9689
0
  if (bed->s->arch_size == 32)
9690
0
    {
9691
0
      r_type_mask = 0xff;
9692
0
      r_sym_shift = 8;
9693
0
    }
9694
0
  else
9695
0
    {
9696
0
      r_type_mask = 0xffffffff;
9697
0
      r_sym_shift = 32;
9698
0
    }
9699
9700
0
  erela = reldata->hdr->contents;
9701
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9702
0
    {
9703
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9704
0
      unsigned int j;
9705
9706
0
      if (*rel_hash == NULL)
9707
0
  continue;
9708
9709
0
      if ((*rel_hash)->indx == -2
9710
0
    && info->gc_sections
9711
0
    && ! info->gc_keep_exported)
9712
0
  {
9713
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9714
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9715
0
            abfd, sec,
9716
0
            (*rel_hash)->root.root.string);
9717
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9718
0
            abfd, sec);
9719
0
    bfd_set_error (bfd_error_invalid_operation);
9720
0
    return false;
9721
0
  }
9722
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9723
9724
0
      (*swap_in) (abfd, erela, irela);
9725
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9726
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9727
0
         | (irela[j].r_info & r_type_mask));
9728
0
      (*swap_out) (abfd, irela, erela);
9729
0
    }
9730
9731
0
  if (bed->elf_backend_update_relocs)
9732
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9733
9734
0
  if (sort && count != 0)
9735
0
    {
9736
0
      bfd_vma (*ext_r_off) (const void *);
9737
0
      bfd_vma r_off;
9738
0
      size_t elt_size;
9739
0
      bfd_byte *base, *end, *p, *loc;
9740
0
      bfd_byte *buf = NULL;
9741
9742
0
      if (bed->s->arch_size == 32)
9743
0
  {
9744
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9745
0
      ext_r_off = ext32l_r_offset;
9746
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9747
0
      ext_r_off = ext32b_r_offset;
9748
0
    else
9749
0
      abort ();
9750
0
  }
9751
0
      else
9752
0
  {
9753
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9754
0
      ext_r_off = ext64l_r_offset;
9755
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9756
0
      ext_r_off = ext64b_r_offset;
9757
0
    else
9758
0
      abort ();
9759
0
  }
9760
9761
      /*  Must use a stable sort here.  A modified insertion sort,
9762
    since the relocs are mostly sorted already.  */
9763
0
      elt_size = reldata->hdr->sh_entsize;
9764
0
      base = reldata->hdr->contents;
9765
0
      end = base + count * elt_size;
9766
0
      if (elt_size > sizeof (Elf64_External_Rela))
9767
0
  abort ();
9768
9769
      /* Ensure the first element is lowest.  This acts as a sentinel,
9770
   speeding the main loop below.  */
9771
0
      r_off = (*ext_r_off) (base);
9772
0
      for (p = loc = base; (p += elt_size) < end; )
9773
0
  {
9774
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9775
0
    if (r_off > r_off2)
9776
0
      {
9777
0
        r_off = r_off2;
9778
0
        loc = p;
9779
0
      }
9780
0
  }
9781
0
      if (loc != base)
9782
0
  {
9783
    /* Don't just swap *base and *loc as that changes the order
9784
       of the original base[0] and base[1] if they happen to
9785
       have the same r_offset.  */
9786
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9787
0
    memcpy (onebuf, loc, elt_size);
9788
0
    memmove (base + elt_size, base, loc - base);
9789
0
    memcpy (base, onebuf, elt_size);
9790
0
  }
9791
9792
0
      for (p = base + elt_size; (p += elt_size) < end; )
9793
0
  {
9794
    /* base to p is sorted, *p is next to insert.  */
9795
0
    r_off = (*ext_r_off) (p);
9796
    /* Search the sorted region for location to insert.  */
9797
0
    loc = p - elt_size;
9798
0
    while (r_off < (*ext_r_off) (loc))
9799
0
      loc -= elt_size;
9800
0
    loc += elt_size;
9801
0
    if (loc != p)
9802
0
      {
9803
        /* Chances are there is a run of relocs to insert here,
9804
     from one of more input files.  Files are not always
9805
     linked in order due to the way elf_link_input_bfd is
9806
     called.  See pr17666.  */
9807
0
        size_t sortlen = p - loc;
9808
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9809
0
        size_t runlen = elt_size;
9810
0
        bfd_vma r_off_runend = r_off;
9811
0
        bfd_vma r_off_runend_next;
9812
0
        size_t buf_size = 96 * 1024;
9813
0
        while (p + runlen < end
9814
0
         && (sortlen <= buf_size
9815
0
       || runlen + elt_size <= buf_size)
9816
         /* run must not break the ordering of base..loc+1 */
9817
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9818
         /* run must be already sorted */
9819
0
         && r_off_runend_next >= r_off_runend)
9820
0
    {
9821
0
      runlen += elt_size;
9822
0
      r_off_runend = r_off_runend_next;
9823
0
    }
9824
0
        if (buf == NULL)
9825
0
    {
9826
0
      buf = bfd_malloc (buf_size);
9827
0
      if (buf == NULL)
9828
0
        return false;
9829
0
    }
9830
0
        if (runlen < sortlen)
9831
0
    {
9832
0
      memcpy (buf, p, runlen);
9833
0
      memmove (loc + runlen, loc, sortlen);
9834
0
      memcpy (loc, buf, runlen);
9835
0
    }
9836
0
        else
9837
0
    {
9838
0
      memcpy (buf, loc, sortlen);
9839
0
      memmove (loc, p, runlen);
9840
0
      memcpy (loc + runlen, buf, sortlen);
9841
0
    }
9842
0
        p += runlen - elt_size;
9843
0
      }
9844
0
  }
9845
      /* Hashes are no longer valid.  */
9846
0
      free (reldata->hashes);
9847
0
      reldata->hashes = NULL;
9848
0
      free (buf);
9849
0
    }
9850
0
  return true;
9851
0
}
9852
9853
struct elf_link_sort_rela
9854
{
9855
  union {
9856
    bfd_vma offset;
9857
    bfd_vma sym_mask;
9858
  } u;
9859
  enum elf_reloc_type_class type;
9860
  /* We use this as an array of size int_rels_per_ext_rel.  */
9861
  Elf_Internal_Rela rela[1];
9862
};
9863
9864
/* qsort stability here and for cmp2 is only an issue if multiple
9865
   dynamic relocations are emitted at the same address.  But targets
9866
   that apply a series of dynamic relocations each operating on the
9867
   result of the prior relocation can't use -z combreloc as
9868
   implemented anyway.  Such schemes tend to be broken by sorting on
9869
   symbol index.  That leaves dynamic NONE relocs as the only other
9870
   case where ld might emit multiple relocs at the same address, and
9871
   those are only emitted due to target bugs.  */
9872
9873
static int
9874
elf_link_sort_cmp1 (const void *A, const void *B)
9875
0
{
9876
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9877
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9878
0
  int relativea, relativeb;
9879
9880
0
  relativea = a->type == reloc_class_relative;
9881
0
  relativeb = b->type == reloc_class_relative;
9882
9883
0
  if (relativea < relativeb)
9884
0
    return 1;
9885
0
  if (relativea > relativeb)
9886
0
    return -1;
9887
0
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9888
0
    return -1;
9889
0
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9890
0
    return 1;
9891
0
  if (a->rela->r_offset < b->rela->r_offset)
9892
0
    return -1;
9893
0
  if (a->rela->r_offset > b->rela->r_offset)
9894
0
    return 1;
9895
0
  return 0;
9896
0
}
9897
9898
static int
9899
elf_link_sort_cmp2 (const void *A, const void *B)
9900
0
{
9901
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9902
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9903
9904
0
  if (a->type < b->type)
9905
0
    return -1;
9906
0
  if (a->type > b->type)
9907
0
    return 1;
9908
0
  if (a->u.offset < b->u.offset)
9909
0
    return -1;
9910
0
  if (a->u.offset > b->u.offset)
9911
0
    return 1;
9912
0
  if (a->rela->r_offset < b->rela->r_offset)
9913
0
    return -1;
9914
0
  if (a->rela->r_offset > b->rela->r_offset)
9915
0
    return 1;
9916
0
  return 0;
9917
0
}
9918
9919
static size_t
9920
elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9921
0
{
9922
0
  asection *dynamic_relocs;
9923
0
  asection *rela_dyn;
9924
0
  asection *rel_dyn;
9925
0
  bfd_size_type count, size;
9926
0
  size_t i, ret, sort_elt, ext_size;
9927
0
  bfd_byte *sort, *s_non_relative, *p;
9928
0
  struct elf_link_sort_rela *sq;
9929
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9930
0
  int i2e = bed->s->int_rels_per_ext_rel;
9931
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9932
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9933
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9934
0
  struct bfd_link_order *lo;
9935
0
  bfd_vma r_sym_mask;
9936
0
  bool use_rela;
9937
9938
  /* Find a dynamic reloc section.  */
9939
0
  rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9940
0
  rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
9941
0
  if (rela_dyn != NULL && rela_dyn->size > 0
9942
0
      && rel_dyn != NULL && rel_dyn->size > 0)
9943
0
    {
9944
0
      bool use_rela_initialised = false;
9945
9946
      /* This is just here to stop gcc from complaining.
9947
   Its initialization checking code is not perfect.  */
9948
0
      use_rela = true;
9949
9950
      /* Both sections are present.  Examine the sizes
9951
   of the indirect sections to help us choose.  */
9952
0
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9953
0
  if (lo->type == bfd_indirect_link_order)
9954
0
    {
9955
0
      asection *o = lo->u.indirect.section;
9956
9957
0
      if ((o->size % bed->s->sizeof_rela) == 0)
9958
0
        {
9959
0
    if ((o->size % bed->s->sizeof_rel) == 0)
9960
      /* Section size is divisible by both rel and rela sizes.
9961
         It is of no help to us.  */
9962
0
      ;
9963
0
    else
9964
0
      {
9965
        /* Section size is only divisible by rela.  */
9966
0
        if (use_rela_initialised && !use_rela)
9967
0
          {
9968
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
9969
0
                "they are in more than one size"),
9970
0
              abfd);
9971
0
      bfd_set_error (bfd_error_invalid_operation);
9972
0
      return 0;
9973
0
          }
9974
0
        else
9975
0
          {
9976
0
      use_rela = true;
9977
0
      use_rela_initialised = true;
9978
0
          }
9979
0
      }
9980
0
        }
9981
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
9982
0
        {
9983
    /* Section size is only divisible by rel.  */
9984
0
    if (use_rela_initialised && use_rela)
9985
0
      {
9986
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
9987
0
            "they are in more than one size"),
9988
0
          abfd);
9989
0
        bfd_set_error (bfd_error_invalid_operation);
9990
0
        return 0;
9991
0
      }
9992
0
    else
9993
0
      {
9994
0
        use_rela = false;
9995
0
        use_rela_initialised = true;
9996
0
      }
9997
0
        }
9998
0
      else
9999
0
        {
10000
    /* The section size is not divisible by either -
10001
       something is wrong.  */
10002
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10003
0
              "they are of an unknown size"), abfd);
10004
0
    bfd_set_error (bfd_error_invalid_operation);
10005
0
    return 0;
10006
0
        }
10007
0
    }
10008
10009
0
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10010
0
  if (lo->type == bfd_indirect_link_order)
10011
0
    {
10012
0
      asection *o = lo->u.indirect.section;
10013
10014
0
      if ((o->size % bed->s->sizeof_rela) == 0)
10015
0
        {
10016
0
    if ((o->size % bed->s->sizeof_rel) == 0)
10017
      /* Section size is divisible by both rel and rela sizes.
10018
         It is of no help to us.  */
10019
0
      ;
10020
0
    else
10021
0
      {
10022
        /* Section size is only divisible by rela.  */
10023
0
        if (use_rela_initialised && !use_rela)
10024
0
          {
10025
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10026
0
                "they are in more than one size"),
10027
0
              abfd);
10028
0
      bfd_set_error (bfd_error_invalid_operation);
10029
0
      return 0;
10030
0
          }
10031
0
        else
10032
0
          {
10033
0
      use_rela = true;
10034
0
      use_rela_initialised = true;
10035
0
          }
10036
0
      }
10037
0
        }
10038
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
10039
0
        {
10040
    /* Section size is only divisible by rel.  */
10041
0
    if (use_rela_initialised && use_rela)
10042
0
      {
10043
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10044
0
            "they are in more than one size"),
10045
0
          abfd);
10046
0
        bfd_set_error (bfd_error_invalid_operation);
10047
0
        return 0;
10048
0
      }
10049
0
    else
10050
0
      {
10051
0
        use_rela = false;
10052
0
        use_rela_initialised = true;
10053
0
      }
10054
0
        }
10055
0
      else
10056
0
        {
10057
    /* The section size is not divisible by either -
10058
       something is wrong.  */
10059
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10060
0
              "they are of an unknown size"), abfd);
10061
0
    bfd_set_error (bfd_error_invalid_operation);
10062
0
    return 0;
10063
0
        }
10064
0
    }
10065
10066
0
      if (! use_rela_initialised)
10067
  /* Make a guess.  */
10068
0
  use_rela = true;
10069
0
    }
10070
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
10071
0
    use_rela = true;
10072
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
10073
0
    use_rela = false;
10074
0
  else
10075
0
    return 0;
10076
10077
0
  if (use_rela)
10078
0
    {
10079
0
      dynamic_relocs = rela_dyn;
10080
0
      ext_size = bed->s->sizeof_rela;
10081
0
      swap_in = bed->s->swap_reloca_in;
10082
0
      swap_out = bed->s->swap_reloca_out;
10083
0
    }
10084
0
  else
10085
0
    {
10086
0
      dynamic_relocs = rel_dyn;
10087
0
      ext_size = bed->s->sizeof_rel;
10088
0
      swap_in = bed->s->swap_reloc_in;
10089
0
      swap_out = bed->s->swap_reloc_out;
10090
0
    }
10091
10092
0
  size = 0;
10093
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10094
0
    if (lo->type == bfd_indirect_link_order)
10095
0
      size += lo->u.indirect.section->size;
10096
10097
0
  if (size != dynamic_relocs->size)
10098
0
    return 0;
10099
10100
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
10101
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
10102
10103
0
  count = dynamic_relocs->size / ext_size;
10104
0
  if (count == 0)
10105
0
    return 0;
10106
0
  sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10107
10108
0
  if (sort == NULL)
10109
0
    {
10110
0
      (*info->callbacks->warning)
10111
0
  (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10112
0
      return 0;
10113
0
    }
10114
10115
0
  if (bed->s->arch_size == 32)
10116
0
    r_sym_mask = ~(bfd_vma) 0xff;
10117
0
  else
10118
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
10119
10120
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10121
0
    if (lo->type == bfd_indirect_link_order)
10122
0
      {
10123
0
  bfd_byte *erel, *erelend;
10124
0
  asection *o = lo->u.indirect.section;
10125
10126
0
  if (o->contents == NULL && o->size != 0)
10127
0
    {
10128
      /* This is a reloc section that is being handled as a normal
10129
         section.  See bfd_section_from_shdr.  We can't combine
10130
         relocs in this case.  */
10131
0
      free (sort);
10132
0
      return 0;
10133
0
    }
10134
0
  erel = o->contents;
10135
0
  erelend = o->contents + o->size;
10136
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
10137
10138
0
  while (erel < erelend)
10139
0
    {
10140
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10141
10142
0
      (*swap_in) (abfd, erel, s->rela);
10143
0
      s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10144
0
      s->u.sym_mask = r_sym_mask;
10145
0
      p += sort_elt;
10146
0
      erel += ext_size;
10147
0
    }
10148
0
      }
10149
10150
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10151
10152
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
10153
0
    {
10154
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10155
0
      if (s->type != reloc_class_relative)
10156
0
  break;
10157
0
    }
10158
0
  ret = i;
10159
0
  s_non_relative = p;
10160
10161
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
10162
0
  for (; i < count; i++, p += sort_elt)
10163
0
    {
10164
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10165
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10166
0
  sq = sp;
10167
0
      sp->u.offset = sq->rela->r_offset;
10168
0
    }
10169
10170
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10171
10172
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
10173
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10174
0
    {
10175
      /* We have plt relocs in .rela.dyn.  */
10176
0
      sq = (struct elf_link_sort_rela *) sort;
10177
0
      for (i = 0; i < count; i++)
10178
0
  if (sq[count - i - 1].type != reloc_class_plt)
10179
0
    break;
10180
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
10181
0
  {
10182
0
    struct bfd_link_order **plo;
10183
    /* Put srelplt link_order last.  This is so the output_offset
10184
       set in the next loop is correct for DT_JMPREL.  */
10185
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10186
0
      if ((*plo)->type == bfd_indirect_link_order
10187
0
    && (*plo)->u.indirect.section == htab->srelplt)
10188
0
        {
10189
0
    lo = *plo;
10190
0
    *plo = lo->next;
10191
0
        }
10192
0
      else
10193
0
        plo = &(*plo)->next;
10194
0
    *plo = lo;
10195
0
    lo->next = NULL;
10196
0
    dynamic_relocs->map_tail.link_order = lo;
10197
0
  }
10198
0
    }
10199
10200
0
  p = sort;
10201
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10202
0
    if (lo->type == bfd_indirect_link_order)
10203
0
      {
10204
0
  bfd_byte *erel, *erelend;
10205
0
  asection *o = lo->u.indirect.section;
10206
10207
0
  erel = o->contents;
10208
0
  erelend = o->contents + o->size;
10209
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10210
0
  while (erel < erelend)
10211
0
    {
10212
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10213
0
      (*swap_out) (abfd, s->rela, erel);
10214
0
      p += sort_elt;
10215
0
      erel += ext_size;
10216
0
    }
10217
0
      }
10218
10219
0
  free (sort);
10220
0
  *psec = dynamic_relocs;
10221
0
  return ret;
10222
0
}
10223
10224
/* Add a symbol to the output symbol string table.  */
10225
10226
static int
10227
elf_link_output_symstrtab (void *finf,
10228
         const char *name,
10229
         Elf_Internal_Sym *elfsym,
10230
         asection *input_sec,
10231
         struct elf_link_hash_entry *h)
10232
0
{
10233
0
  struct elf_final_link_info *flinfo = finf;
10234
0
  int (*output_symbol_hook)
10235
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10236
0
     struct elf_link_hash_entry *);
10237
0
  struct elf_link_hash_table *hash_table;
10238
0
  const struct elf_backend_data *bed;
10239
0
  bfd_size_type strtabsize;
10240
10241
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10242
10243
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10244
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10245
0
  if (output_symbol_hook != NULL)
10246
0
    {
10247
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10248
0
      if (ret != 1)
10249
0
  return ret;
10250
0
    }
10251
10252
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10253
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10254
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10255
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10256
10257
0
  if (name == NULL || *name == '\0')
10258
0
    elfsym->st_name = (unsigned long) -1;
10259
0
  else
10260
0
    {
10261
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10262
   to get the final offset for st_name.  */
10263
0
      char *versioned_name = (char *) name;
10264
0
      if (h != NULL)
10265
0
  {
10266
0
    if (h->versioned == versioned && h->def_dynamic)
10267
0
      {
10268
        /* Keep only one '@' for versioned symbols defined in
10269
           shared objects.  */
10270
0
        char *version = strrchr (name, ELF_VER_CHR);
10271
0
        char *base_end = strchr (name, ELF_VER_CHR);
10272
0
        if (version != base_end)
10273
0
    {
10274
0
      size_t base_len;
10275
0
      size_t len = strlen (name);
10276
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10277
0
      if (versioned_name == NULL)
10278
0
        return 0;
10279
0
      base_len = base_end - name;
10280
0
      memcpy (versioned_name, name, base_len);
10281
0
      memcpy (versioned_name + base_len, version,
10282
0
        len - base_len);
10283
0
    }
10284
0
      }
10285
0
  }
10286
0
      else if (flinfo->info->unique_symbol
10287
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10288
0
  {
10289
0
    struct local_hash_entry *lh;
10290
0
    size_t count_len;
10291
0
    size_t base_len;
10292
0
    char buf[30];
10293
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10294
0
      {
10295
0
      case STT_FILE:
10296
0
      case STT_SECTION:
10297
0
        break;
10298
0
      default:
10299
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10300
0
         (&flinfo->local_hash_table, name, true, false);
10301
0
        if (lh == NULL)
10302
0
    return 0;
10303
        /* Always append ".COUNT" to local symbols to avoid
10304
     potential conflicts with local symbol "XXX.COUNT".  */
10305
0
        sprintf (buf, "%lx", lh->count);
10306
0
        base_len = lh->size;
10307
0
        if (!base_len)
10308
0
    {
10309
0
      base_len = strlen (name);
10310
0
      lh->size = base_len;
10311
0
    }
10312
0
        count_len = strlen (buf);
10313
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10314
0
            base_len + count_len + 2);
10315
0
        if (versioned_name == NULL)
10316
0
    return 0;
10317
0
        memcpy (versioned_name, name, base_len);
10318
0
        versioned_name[base_len] = '.';
10319
0
        memcpy (versioned_name + base_len + 1, buf,
10320
0
          count_len + 1);
10321
0
        lh->count++;
10322
0
        break;
10323
0
      }
10324
0
  }
10325
0
      elfsym->st_name
10326
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10327
0
                 versioned_name, false);
10328
0
      if (elfsym->st_name == (unsigned long) -1)
10329
0
  return 0;
10330
0
    }
10331
10332
0
  hash_table = elf_hash_table (flinfo->info);
10333
0
  strtabsize = hash_table->strtabsize;
10334
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10335
0
    {
10336
0
      strtabsize += strtabsize;
10337
0
      hash_table->strtabsize = strtabsize;
10338
0
      strtabsize *= sizeof (*hash_table->strtab);
10339
0
      hash_table->strtab
10340
0
  = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10341
0
             strtabsize);
10342
0
      if (hash_table->strtab == NULL)
10343
0
  return 0;
10344
0
    }
10345
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10346
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10347
0
    = flinfo->output_bfd->symcount;
10348
0
  flinfo->output_bfd->symcount += 1;
10349
10350
0
  return 1;
10351
0
}
10352
10353
/* Swap symbols out to the symbol table and flush the output symbols to
10354
   the file.  */
10355
10356
static bool
10357
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10358
0
{
10359
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10360
0
  size_t amt;
10361
0
  size_t i;
10362
0
  const struct elf_backend_data *bed;
10363
0
  bfd_byte *symbuf;
10364
0
  Elf_Internal_Shdr *hdr;
10365
0
  file_ptr pos;
10366
0
  bool ret;
10367
10368
0
  if (flinfo->output_bfd->symcount == 0)
10369
0
    return true;
10370
10371
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10372
10373
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10374
10375
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10376
0
  symbuf = (bfd_byte *) bfd_malloc (amt);
10377
0
  if (symbuf == NULL)
10378
0
    return false;
10379
10380
0
  if (flinfo->symshndxbuf)
10381
0
    {
10382
0
      amt = sizeof (Elf_External_Sym_Shndx);
10383
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10384
0
      flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10385
0
      if (flinfo->symshndxbuf == NULL)
10386
0
  {
10387
0
    free (symbuf);
10388
0
    return false;
10389
0
  }
10390
0
    }
10391
10392
  /* Now swap out the symbols.  */
10393
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10394
0
    {
10395
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10396
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10397
0
  elfsym->sym.st_name = 0;
10398
0
      else
10399
0
  elfsym->sym.st_name
10400
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10401
0
                elfsym->sym.st_name);
10402
10403
      /* Inform the linker of the addition of this symbol.  */
10404
10405
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10406
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10407
0
             &elfsym->sym);
10408
10409
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10410
0
             ((bfd_byte *) symbuf
10411
0
        + (elfsym->dest_index
10412
0
           * bed->s->sizeof_sym)),
10413
0
             NPTR_ADD (flinfo->symshndxbuf,
10414
0
           elfsym->dest_index));
10415
0
    }
10416
10417
0
  hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10418
0
  pos = hdr->sh_offset + hdr->sh_size;
10419
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10420
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10421
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10422
0
    {
10423
0
      hdr->sh_size += amt;
10424
0
      ret = true;
10425
0
    }
10426
0
  else
10427
0
    ret = false;
10428
10429
0
  free (symbuf);
10430
10431
0
  free (hash_table->strtab);
10432
0
  hash_table->strtab = NULL;
10433
10434
0
  return ret;
10435
0
}
10436
10437
/* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
10438
10439
static bool
10440
check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10441
0
{
10442
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10443
0
      && sym->st_shndx < SHN_LORESERVE)
10444
0
    {
10445
      /* The gABI doesn't support dynamic symbols in output sections
10446
   beyond 64k.  */
10447
0
      _bfd_error_handler
10448
  /* xgettext:c-format */
10449
0
  (_("%pB: too many sections: %d (>= %d)"),
10450
0
   abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10451
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10452
0
      return false;
10453
0
    }
10454
0
  return true;
10455
0
}
10456
10457
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10458
   allowing an unsatisfied unversioned symbol in the DSO to match a
10459
   versioned symbol that would normally require an explicit version.
10460
   We also handle the case that a DSO references a hidden symbol
10461
   which may be satisfied by a versioned symbol in another DSO.  */
10462
10463
static bool
10464
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10465
         const struct elf_backend_data *bed,
10466
         struct elf_link_hash_entry *h)
10467
0
{
10468
0
  bfd *abfd;
10469
0
  struct elf_link_loaded_list *loaded;
10470
10471
0
  if (!is_elf_hash_table (info->hash))
10472
0
    return false;
10473
10474
  /* Check indirect symbol.  */
10475
0
  while (h->root.type == bfd_link_hash_indirect)
10476
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10477
10478
0
  switch (h->root.type)
10479
0
    {
10480
0
    default:
10481
0
      abfd = NULL;
10482
0
      break;
10483
10484
0
    case bfd_link_hash_undefined:
10485
0
    case bfd_link_hash_undefweak:
10486
0
      abfd = h->root.u.undef.abfd;
10487
0
      if (abfd == NULL
10488
0
    || (abfd->flags & DYNAMIC) == 0
10489
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10490
0
  return false;
10491
0
      break;
10492
10493
0
    case bfd_link_hash_defined:
10494
0
    case bfd_link_hash_defweak:
10495
0
      abfd = h->root.u.def.section->owner;
10496
0
      break;
10497
10498
0
    case bfd_link_hash_common:
10499
0
      abfd = h->root.u.c.p->section->owner;
10500
0
      break;
10501
0
    }
10502
0
  BFD_ASSERT (abfd != NULL);
10503
10504
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10505
0
       loaded != NULL;
10506
0
       loaded = loaded->next)
10507
0
    {
10508
0
      bfd *input;
10509
0
      Elf_Internal_Shdr *hdr;
10510
0
      size_t symcount;
10511
0
      size_t extsymcount;
10512
0
      size_t extsymoff;
10513
0
      Elf_Internal_Shdr *versymhdr;
10514
0
      Elf_Internal_Sym *isym;
10515
0
      Elf_Internal_Sym *isymend;
10516
0
      Elf_Internal_Sym *isymbuf;
10517
0
      Elf_External_Versym *ever;
10518
0
      Elf_External_Versym *extversym;
10519
10520
0
      input = loaded->abfd;
10521
10522
      /* We check each DSO for a possible hidden versioned definition.  */
10523
0
      if (input == abfd
10524
0
    || elf_dynversym (input) == 0)
10525
0
  continue;
10526
10527
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10528
10529
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10530
0
      if (elf_bad_symtab (input))
10531
0
  {
10532
0
    extsymcount = symcount;
10533
0
    extsymoff = 0;
10534
0
  }
10535
0
      else
10536
0
  {
10537
0
    extsymcount = symcount - hdr->sh_info;
10538
0
    extsymoff = hdr->sh_info;
10539
0
  }
10540
10541
0
      if (extsymcount == 0)
10542
0
  continue;
10543
10544
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10545
0
              NULL, NULL, NULL);
10546
0
      if (isymbuf == NULL)
10547
0
  return false;
10548
10549
      /* Read in any version definitions.  */
10550
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10551
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10552
0
    || (extversym = (Elf_External_Versym *)
10553
0
        _bfd_malloc_and_read (input, versymhdr->sh_size,
10554
0
            versymhdr->sh_size)) == NULL)
10555
0
  {
10556
0
    free (isymbuf);
10557
0
    return false;
10558
0
  }
10559
10560
0
      ever = extversym + extsymoff;
10561
0
      isymend = isymbuf + extsymcount;
10562
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10563
0
  {
10564
0
    const char *name;
10565
0
    Elf_Internal_Versym iver;
10566
0
    unsigned short version_index;
10567
10568
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10569
0
        || isym->st_shndx == SHN_UNDEF)
10570
0
      continue;
10571
10572
0
    name = bfd_elf_string_from_elf_section (input,
10573
0
              hdr->sh_link,
10574
0
              isym->st_name);
10575
0
    if (strcmp (name, h->root.root.string) != 0)
10576
0
      continue;
10577
10578
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10579
10580
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10581
0
        && !(h->def_regular
10582
0
       && h->forced_local))
10583
0
      {
10584
        /* If we have a non-hidden versioned sym, then it should
10585
     have provided a definition for the undefined sym unless
10586
     it is defined in a non-shared object and forced local.
10587
         */
10588
0
        abort ();
10589
0
      }
10590
10591
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10592
0
    if (version_index == 1 || version_index == 2)
10593
0
      {
10594
        /* This is the base or first version.  We can use it.  */
10595
0
        free (extversym);
10596
0
        free (isymbuf);
10597
0
        return true;
10598
0
      }
10599
0
  }
10600
10601
0
      free (extversym);
10602
0
      free (isymbuf);
10603
0
    }
10604
10605
0
  return false;
10606
0
}
10607
10608
/* Convert ELF common symbol TYPE.  */
10609
10610
static int
10611
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10612
0
{
10613
  /* Commom symbol can only appear in relocatable link.  */
10614
0
  if (!bfd_link_relocatable (info))
10615
0
    abort ();
10616
0
  switch (info->elf_stt_common)
10617
0
    {
10618
0
    case unchanged:
10619
0
      break;
10620
0
    case elf_stt_common:
10621
0
      type = STT_COMMON;
10622
0
      break;
10623
0
    case no_elf_stt_common:
10624
0
      type = STT_OBJECT;
10625
0
      break;
10626
0
    }
10627
0
  return type;
10628
0
}
10629
10630
/* Add an external symbol to the symbol table.  This is called from
10631
   the hash table traversal routine.  When generating a shared object,
10632
   we go through the symbol table twice.  The first time we output
10633
   anything that might have been forced to local scope in a version
10634
   script.  The second time we output the symbols that are still
10635
   global symbols.  */
10636
10637
static bool
10638
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10639
0
{
10640
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10641
0
  struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10642
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10643
0
  bool strip;
10644
0
  Elf_Internal_Sym sym;
10645
0
  asection *input_sec;
10646
0
  const struct elf_backend_data *bed;
10647
0
  long indx;
10648
0
  int ret;
10649
0
  unsigned int type;
10650
10651
0
  if (h->root.type == bfd_link_hash_warning)
10652
0
    {
10653
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10654
0
      if (h->root.type == bfd_link_hash_new)
10655
0
  return true;
10656
0
    }
10657
10658
  /* Decide whether to output this symbol in this pass.  */
10659
0
  if (eoinfo->localsyms)
10660
0
    {
10661
0
      if (!h->forced_local)
10662
0
  return true;
10663
0
    }
10664
0
  else
10665
0
    {
10666
0
      if (h->forced_local)
10667
0
  return true;
10668
0
    }
10669
10670
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10671
10672
0
  if (h->root.type == bfd_link_hash_undefined)
10673
0
    {
10674
      /* If we have an undefined symbol reference here then it must have
10675
   come from a shared library that is being linked in.  (Undefined
10676
   references in regular files have already been handled unless
10677
   they are in unreferenced sections which are removed by garbage
10678
   collection).  */
10679
0
      bool ignore_undef = false;
10680
10681
      /* Some symbols may be special in that the fact that they're
10682
   undefined can be safely ignored - let backend determine that.  */
10683
0
      if (bed->elf_backend_ignore_undef_symbol)
10684
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10685
10686
      /* If we are reporting errors for this situation then do so now.  */
10687
0
      if (!ignore_undef
10688
0
    && h->ref_dynamic_nonweak
10689
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10690
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10691
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10692
0
  {
10693
0
    flinfo->info->callbacks->undefined_symbol
10694
0
      (flinfo->info, h->root.root.string,
10695
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10696
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10697
0
       && !flinfo->info->warn_unresolved_syms);
10698
0
  }
10699
10700
      /* Strip a global symbol defined in a discarded section.  */
10701
0
      if (h->indx == -3)
10702
0
  return true;
10703
0
    }
10704
10705
  /* We should also warn if a forced local symbol is referenced from
10706
     shared libraries.  */
10707
0
  if (bfd_link_executable (flinfo->info)
10708
0
      && h->forced_local
10709
0
      && h->ref_dynamic
10710
0
      && h->def_regular
10711
0
      && !h->dynamic_def
10712
0
      && h->ref_dynamic_nonweak
10713
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10714
0
    {
10715
0
      bfd *def_bfd;
10716
0
      const char *msg;
10717
0
      struct elf_link_hash_entry *hi = h;
10718
10719
      /* Check indirect symbol.  */
10720
0
      while (hi->root.type == bfd_link_hash_indirect)
10721
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10722
10723
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10724
  /* xgettext:c-format */
10725
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10726
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10727
  /* xgettext:c-format */
10728
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10729
0
      else
10730
  /* xgettext:c-format */
10731
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10732
0
      def_bfd = flinfo->output_bfd;
10733
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10734
0
  def_bfd = hi->root.u.def.section->owner;
10735
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10736
0
        h->root.root.string, def_bfd);
10737
0
      bfd_set_error (bfd_error_bad_value);
10738
0
      eoinfo->failed = true;
10739
0
      return false;
10740
0
    }
10741
10742
  /* We don't want to output symbols that have never been mentioned by
10743
     a regular file, or that we have been told to strip.  However, if
10744
     h->indx is set to -2, the symbol is used by a reloc and we must
10745
     output it.  */
10746
0
  strip = false;
10747
0
  if (h->indx == -2)
10748
0
    ;
10749
0
  else if ((h->def_dynamic
10750
0
      || h->ref_dynamic
10751
0
      || h->root.type == bfd_link_hash_new)
10752
0
     && !h->def_regular
10753
0
     && !h->ref_regular)
10754
0
    strip = true;
10755
0
  else if (flinfo->info->strip == strip_all)
10756
0
    strip = true;
10757
0
  else if (flinfo->info->strip == strip_some
10758
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10759
0
             h->root.root.string, false, false) == NULL)
10760
0
    strip = true;
10761
0
  else if ((h->root.type == bfd_link_hash_defined
10762
0
      || h->root.type == bfd_link_hash_defweak)
10763
0
     && ((flinfo->info->strip_discarded
10764
0
    && discarded_section (h->root.u.def.section))
10765
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10766
0
       && h->root.u.def.section->owner != NULL
10767
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10768
0
    strip = true;
10769
0
  else if ((h->root.type == bfd_link_hash_undefined
10770
0
      || h->root.type == bfd_link_hash_undefweak)
10771
0
     && h->root.u.undef.abfd != NULL
10772
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10773
0
    strip = true;
10774
10775
  /* Remember if this symbol should be stripped.  */
10776
0
  bool should_strip = strip;
10777
10778
  /* Strip undefined weak symbols link if they don't have relocation.  */
10779
0
  if (!strip)
10780
0
    strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10781
10782
0
  type = h->type;
10783
10784
  /* If we're stripping it, and it's not a dynamic symbol, there's
10785
     nothing else to do.   However, if it is a forced local symbol or
10786
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10787
     function a chance to make it dynamic.  */
10788
0
  if (strip
10789
0
      && h->dynindx == -1
10790
0
      && type != STT_GNU_IFUNC
10791
0
      && !h->forced_local)
10792
0
    return true;
10793
10794
0
  sym.st_value = 0;
10795
0
  sym.st_size = h->size;
10796
0
  sym.st_other = h->other;
10797
0
  switch (h->root.type)
10798
0
    {
10799
0
    default:
10800
0
    case bfd_link_hash_new:
10801
0
    case bfd_link_hash_warning:
10802
0
      abort ();
10803
0
      return false;
10804
10805
0
    case bfd_link_hash_undefined:
10806
0
    case bfd_link_hash_undefweak:
10807
0
      input_sec = bfd_und_section_ptr;
10808
0
      sym.st_shndx = SHN_UNDEF;
10809
0
      break;
10810
10811
0
    case bfd_link_hash_defined:
10812
0
    case bfd_link_hash_defweak:
10813
0
      {
10814
0
  input_sec = h->root.u.def.section;
10815
0
  if (input_sec->output_section != NULL)
10816
0
    {
10817
0
      sym.st_shndx =
10818
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10819
0
             input_sec->output_section);
10820
0
      if (sym.st_shndx == SHN_BAD)
10821
0
        {
10822
0
    _bfd_error_handler
10823
      /* xgettext:c-format */
10824
0
      (_("%pB: could not find output section %pA for input section %pA"),
10825
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10826
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10827
0
    eoinfo->failed = true;
10828
0
    return false;
10829
0
        }
10830
10831
      /* ELF symbols in relocatable files are section relative,
10832
         but in nonrelocatable files they are virtual
10833
         addresses.  */
10834
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10835
0
      if (!bfd_link_relocatable (flinfo->info))
10836
0
        {
10837
0
    sym.st_value += input_sec->output_section->vma;
10838
0
    if (h->type == STT_TLS)
10839
0
      {
10840
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10841
0
        if (tls_sec != NULL)
10842
0
          sym.st_value -= tls_sec->vma;
10843
0
      }
10844
0
        }
10845
0
    }
10846
0
  else
10847
0
    {
10848
0
      BFD_ASSERT (input_sec->owner == NULL
10849
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10850
0
      sym.st_shndx = SHN_UNDEF;
10851
0
      input_sec = bfd_und_section_ptr;
10852
0
    }
10853
0
      }
10854
0
      break;
10855
10856
0
    case bfd_link_hash_common:
10857
0
      input_sec = h->root.u.c.p->section;
10858
0
      sym.st_shndx = bed->common_section_index (input_sec);
10859
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
10860
0
      break;
10861
10862
0
    case bfd_link_hash_indirect:
10863
      /* These symbols are created by symbol versioning.  They point
10864
   to the decorated version of the name.  For example, if the
10865
   symbol foo@@GNU_1.2 is the default, which should be used when
10866
   foo is used with no version, then we add an indirect symbol
10867
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
10868
   since the indirected symbol is already in the hash table.  */
10869
0
      return true;
10870
0
    }
10871
10872
0
  if (type == STT_COMMON || type == STT_OBJECT)
10873
0
    switch (h->root.type)
10874
0
      {
10875
0
      case bfd_link_hash_common:
10876
0
  type = elf_link_convert_common_type (flinfo->info, type);
10877
0
  break;
10878
0
      case bfd_link_hash_defined:
10879
0
      case bfd_link_hash_defweak:
10880
0
  if (bed->common_definition (&sym))
10881
0
    type = elf_link_convert_common_type (flinfo->info, type);
10882
0
  else
10883
0
    type = STT_OBJECT;
10884
0
  break;
10885
0
      case bfd_link_hash_undefined:
10886
0
      case bfd_link_hash_undefweak:
10887
0
  break;
10888
0
      default:
10889
0
  abort ();
10890
0
      }
10891
10892
0
  if (h->forced_local)
10893
0
    {
10894
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10895
      /* Turn off visibility on local symbol.  */
10896
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10897
0
    }
10898
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
10899
0
  else if (h->unique_global && h->def_regular)
10900
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10901
0
  else if (h->root.type == bfd_link_hash_undefweak
10902
0
     || h->root.type == bfd_link_hash_defweak)
10903
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10904
0
  else
10905
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10906
0
  sym.st_target_internal = h->target_internal;
10907
10908
  /* Give the processor backend a chance to tweak the symbol value,
10909
     and also to finish up anything that needs to be done for this
10910
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
10911
     forced local syms when non-shared is due to a historical quirk.
10912
     STT_GNU_IFUNC symbol must go through PLT.  */
10913
0
  if ((h->type == STT_GNU_IFUNC
10914
0
       && h->def_regular
10915
0
       && !bfd_link_relocatable (flinfo->info))
10916
0
      || ((h->dynindx != -1
10917
0
     || h->forced_local)
10918
0
    && ((bfd_link_pic (flinfo->info)
10919
0
         && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10920
0
       || h->root.type != bfd_link_hash_undefweak))
10921
0
        || !h->forced_local)
10922
0
    && elf_hash_table (flinfo->info)->dynamic_sections_created))
10923
0
    {
10924
0
      if (! ((*bed->elf_backend_finish_dynamic_symbol)
10925
0
       (flinfo->output_bfd, flinfo->info, h, &sym)))
10926
0
  {
10927
0
    eoinfo->failed = true;
10928
0
    return false;
10929
0
  }
10930
      /* If a symbol is in the dynamic symbol table and isn't a
10931
   should-strip symbol, also keep it in the symbol table.  */
10932
0
      if (!should_strip)
10933
0
  strip = false;
10934
0
    }
10935
10936
  /* If we are marking the symbol as undefined, and there are no
10937
     non-weak references to this symbol from a regular object, then
10938
     mark the symbol as weak undefined; if there are non-weak
10939
     references, mark the symbol as strong.  We can't do this earlier,
10940
     because it might not be marked as undefined until the
10941
     finish_dynamic_symbol routine gets through with it.  */
10942
0
  if (sym.st_shndx == SHN_UNDEF
10943
0
      && h->ref_regular
10944
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10945
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10946
0
    {
10947
0
      int bindtype;
10948
0
      type = ELF_ST_TYPE (sym.st_info);
10949
10950
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10951
0
      if (type == STT_GNU_IFUNC)
10952
0
  type = STT_FUNC;
10953
10954
0
      if (h->ref_regular_nonweak)
10955
0
  bindtype = STB_GLOBAL;
10956
0
      else
10957
0
  bindtype = STB_WEAK;
10958
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
10959
0
    }
10960
10961
  /* If this is a symbol defined in a dynamic library, don't use the
10962
     symbol size from the dynamic library.  Relinking an executable
10963
     against a new library may introduce gratuitous changes in the
10964
     executable's symbols if we keep the size.  */
10965
0
  if (sym.st_shndx == SHN_UNDEF
10966
0
      && !h->def_regular
10967
0
      && h->def_dynamic)
10968
0
    sym.st_size = 0;
10969
10970
  /* If a non-weak symbol with non-default visibility is not defined
10971
     locally, it is a fatal error.  */
10972
0
  if (!bfd_link_relocatable (flinfo->info)
10973
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10974
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
10975
0
      && h->root.type == bfd_link_hash_undefined
10976
0
      && !h->def_regular)
10977
0
    {
10978
0
      const char *msg;
10979
10980
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10981
  /* xgettext:c-format */
10982
0
  msg = _("%pB: protected symbol `%s' isn't defined");
10983
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10984
  /* xgettext:c-format */
10985
0
  msg = _("%pB: internal symbol `%s' isn't defined");
10986
0
      else
10987
  /* xgettext:c-format */
10988
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
10989
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10990
0
      bfd_set_error (bfd_error_bad_value);
10991
0
      eoinfo->failed = true;
10992
0
      return false;
10993
0
    }
10994
10995
  /* If this symbol should be put in the .dynsym section, then put it
10996
     there now.  We already know the symbol index.  We also fill in
10997
     the entry in the .hash section.  */
10998
0
  if (h->dynindx != -1
10999
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
11000
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
11001
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11002
0
    {
11003
0
      bfd_byte *esym;
11004
11005
      /* Since there is no version information in the dynamic string,
11006
   if there is no version info in symbol version section, we will
11007
   have a run-time problem if not linking executable, referenced
11008
   by shared library, or not bound locally.  */
11009
0
      if (h->verinfo.verdef == NULL
11010
0
    && (!bfd_link_executable (flinfo->info)
11011
0
        || h->ref_dynamic
11012
0
        || !h->def_regular))
11013
0
  {
11014
0
    char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11015
11016
0
    if (p && p [1] != '\0')
11017
0
      {
11018
0
        _bfd_error_handler
11019
    /* xgettext:c-format */
11020
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
11021
0
     flinfo->output_bfd, h->root.root.string);
11022
0
        eoinfo->failed = true;
11023
0
        return false;
11024
0
      }
11025
0
  }
11026
11027
0
      sym.st_name = h->dynstr_index;
11028
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
11029
0
        + h->dynindx * bed->s->sizeof_sym);
11030
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
11031
0
  {
11032
0
    eoinfo->failed = true;
11033
0
    return false;
11034
0
  }
11035
11036
      /* Inform the linker of the addition of this symbol.  */
11037
11038
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
11039
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11040
11041
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11042
11043
0
      if (flinfo->hash_sec != NULL)
11044
0
  {
11045
0
    size_t hash_entry_size;
11046
0
    bfd_byte *bucketpos;
11047
0
    bfd_vma chain;
11048
0
    size_t bucketcount;
11049
0
    size_t bucket;
11050
11051
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11052
0
    bucket = h->u.elf_hash_value % bucketcount;
11053
11054
0
    hash_entry_size
11055
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11056
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11057
0
           + (bucket + 2) * hash_entry_size);
11058
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11059
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11060
0
       bucketpos);
11061
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11062
0
       ((bfd_byte *) flinfo->hash_sec->contents
11063
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11064
0
  }
11065
11066
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11067
0
  {
11068
0
    Elf_Internal_Versym iversym;
11069
0
    Elf_External_Versym *eversym;
11070
11071
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11072
0
      {
11073
0
        if (h->verinfo.verdef == NULL
11074
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11075
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11076
0
    iversym.vs_vers = 1;
11077
0
        else
11078
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11079
0
      }
11080
0
    else
11081
0
      {
11082
0
        if (h->verinfo.vertree == NULL)
11083
0
    iversym.vs_vers = 1;
11084
0
        else
11085
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11086
0
        if (flinfo->info->create_default_symver)
11087
0
    iversym.vs_vers++;
11088
0
      }
11089
11090
    /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11091
       defined locally.  */
11092
0
    if (h->versioned == versioned_hidden && h->def_regular)
11093
0
      iversym.vs_vers |= VERSYM_HIDDEN;
11094
11095
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11096
0
    eversym += h->dynindx;
11097
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11098
0
  }
11099
0
    }
11100
11101
  /* If the symbol is undefined, and we didn't output it to .dynsym,
11102
     strip it from .symtab too.  Obviously we can't do this for
11103
     relocatable output or when needed for --emit-relocs.  */
11104
0
  else if (input_sec == bfd_und_section_ptr
11105
0
     && h->indx != -2
11106
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
11107
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11108
0
     && !bfd_link_relocatable (flinfo->info))
11109
0
    return true;
11110
11111
  /* Also strip others that we couldn't earlier due to dynamic symbol
11112
     processing.  */
11113
0
  if (strip)
11114
0
    return true;
11115
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
11116
0
    return true;
11117
11118
  /* Output a FILE symbol so that following locals are not associated
11119
     with the wrong input file.  We need one for forced local symbols
11120
     if we've seen more than one FILE symbol or when we have exactly
11121
     one FILE symbol but global symbols are present in a file other
11122
     than the one with the FILE symbol.  We also need one if linker
11123
     defined symbols are present.  In practice these conditions are
11124
     always met, so just emit the FILE symbol unconditionally.  */
11125
0
  if (eoinfo->localsyms
11126
0
      && !eoinfo->file_sym_done
11127
0
      && eoinfo->flinfo->filesym_count != 0)
11128
0
    {
11129
0
      Elf_Internal_Sym fsym;
11130
11131
0
      memset (&fsym, 0, sizeof (fsym));
11132
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11133
0
      fsym.st_shndx = SHN_ABS;
11134
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11135
0
              bfd_und_section_ptr, NULL))
11136
0
  return false;
11137
11138
0
      eoinfo->file_sym_done = true;
11139
0
    }
11140
11141
0
  indx = bfd_get_symcount (flinfo->output_bfd);
11142
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11143
0
           input_sec, h);
11144
0
  if (ret == 0)
11145
0
    {
11146
0
      eoinfo->failed = true;
11147
0
      return false;
11148
0
    }
11149
0
  else if (ret == 1)
11150
0
    h->indx = indx;
11151
0
  else if (h->indx == -2)
11152
0
    abort();
11153
11154
0
  return true;
11155
0
}
11156
11157
/* Return TRUE if special handling is done for relocs in SEC against
11158
   symbols defined in discarded sections.  */
11159
11160
static bool
11161
elf_section_ignore_discarded_relocs (asection *sec)
11162
0
{
11163
0
  const struct elf_backend_data *bed;
11164
11165
0
  switch (sec->sec_info_type)
11166
0
    {
11167
0
    case SEC_INFO_TYPE_STABS:
11168
0
    case SEC_INFO_TYPE_EH_FRAME:
11169
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11170
0
    case SEC_INFO_TYPE_SFRAME:
11171
0
      return true;
11172
0
    default:
11173
0
      break;
11174
0
    }
11175
11176
0
  bed = get_elf_backend_data (sec->owner);
11177
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
11178
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11179
0
    return true;
11180
11181
0
  return false;
11182
0
}
11183
11184
/* Return a mask saying how ld should treat relocations in SEC against
11185
   symbols defined in discarded sections.  If this function returns
11186
   COMPLAIN set, ld will issue a warning message.  If this function
11187
   returns PRETEND set, and the discarded section was link-once and the
11188
   same size as the kept link-once section, ld will pretend that the
11189
   symbol was actually defined in the kept section.  Otherwise ld will
11190
   zero the reloc (at least that is the intent, but some cooperation by
11191
   the target dependent code is needed, particularly for REL targets).  */
11192
11193
unsigned int
11194
_bfd_elf_default_action_discarded (asection *sec)
11195
0
{
11196
0
  const struct elf_backend_data *bed;
11197
0
  bed = get_elf_backend_data (sec->owner);
11198
11199
0
  if (sec->flags & SEC_DEBUGGING)
11200
0
    return PRETEND;
11201
11202
0
  if (strcmp (".eh_frame", sec->name) == 0)
11203
0
    return 0;
11204
11205
0
  if (bed->elf_backend_can_make_multiple_eh_frame
11206
0
      && strncmp (sec->name, ".eh_frame.", 10) == 0)
11207
0
    return 0;
11208
11209
0
  if (strcmp (".sframe", sec->name) == 0)
11210
0
    return 0;
11211
11212
0
  if (strcmp (".gcc_except_table", sec->name) == 0)
11213
0
    return 0;
11214
11215
0
  return COMPLAIN | PRETEND;
11216
0
}
11217
11218
/* Find a match between a section and a member of a section group.  */
11219
11220
static asection *
11221
match_group_member (asection *sec, asection *group,
11222
        struct bfd_link_info *info)
11223
0
{
11224
0
  asection *first = elf_next_in_group (group);
11225
0
  asection *s = first;
11226
11227
0
  while (s != NULL)
11228
0
    {
11229
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
11230
0
  return s;
11231
11232
0
      s = elf_next_in_group (s);
11233
0
      if (s == first)
11234
0
  break;
11235
0
    }
11236
11237
0
  return NULL;
11238
0
}
11239
11240
/* Check if the kept section of a discarded section SEC can be used
11241
   to replace it.  Return the replacement if it is OK.  Otherwise return
11242
   NULL.  */
11243
11244
asection *
11245
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11246
0
{
11247
0
  asection *kept;
11248
11249
0
  kept = sec->kept_section;
11250
0
  if (kept != NULL)
11251
0
    {
11252
0
      if ((kept->flags & SEC_GROUP) != 0)
11253
0
  kept = match_group_member (sec, kept, info);
11254
0
      if (kept != NULL)
11255
0
  {
11256
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11257
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11258
0
      kept = NULL;
11259
0
    else
11260
0
      {
11261
        /* Get the real kept section.  */
11262
0
        asection *next;
11263
0
        for (next = kept->kept_section;
11264
0
       next != NULL;
11265
0
       next = next->kept_section)
11266
0
    kept = next;
11267
0
      }
11268
0
  }
11269
0
      sec->kept_section = kept;
11270
0
    }
11271
0
  return kept;
11272
0
}
11273
11274
/* Link an input file into the linker output file.  This function
11275
   handles all the sections and relocations of the input file at once.
11276
   This is so that we only have to read the local symbols once, and
11277
   don't have to keep them in memory.  */
11278
11279
static bool
11280
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11281
0
{
11282
0
  int (*relocate_section)
11283
0
    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11284
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11285
0
  bfd *output_bfd;
11286
0
  Elf_Internal_Shdr *symtab_hdr;
11287
0
  size_t locsymcount;
11288
0
  size_t extsymoff;
11289
0
  Elf_Internal_Sym *isymbuf;
11290
0
  Elf_Internal_Sym *isym;
11291
0
  Elf_Internal_Sym *isymend;
11292
0
  long *pindex;
11293
0
  asection **ppsection;
11294
0
  asection *o;
11295
0
  const struct elf_backend_data *bed;
11296
0
  struct elf_link_hash_entry **sym_hashes;
11297
0
  bfd_size_type address_size;
11298
0
  bfd_vma r_type_mask;
11299
0
  int r_sym_shift;
11300
0
  bool have_file_sym = false;
11301
11302
0
  output_bfd = flinfo->output_bfd;
11303
0
  bed = get_elf_backend_data (output_bfd);
11304
0
  relocate_section = bed->elf_backend_relocate_section;
11305
11306
  /* If this is a dynamic object, we don't want to do anything here:
11307
     we don't want the local symbols, and we don't want the section
11308
     contents.  */
11309
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11310
0
    return true;
11311
11312
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11313
0
  if (elf_bad_symtab (input_bfd))
11314
0
    {
11315
0
      locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11316
0
      extsymoff = 0;
11317
0
    }
11318
0
  else
11319
0
    {
11320
0
      locsymcount = symtab_hdr->sh_info;
11321
0
      extsymoff = symtab_hdr->sh_info;
11322
0
    }
11323
11324
  /* Enable GNU OSABI features in the output BFD that are used in the input
11325
     BFD.  */
11326
0
  if (bed->elf_osabi == ELFOSABI_NONE
11327
0
      || bed->elf_osabi == ELFOSABI_GNU
11328
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11329
0
    elf_tdata (output_bfd)->has_gnu_osabi
11330
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11331
0
    & (bfd_link_relocatable (flinfo->info)
11332
0
       ? -1 : ~elf_gnu_osabi_retain));
11333
11334
  /* Read the local symbols.  */
11335
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11336
0
  if (isymbuf == NULL && locsymcount != 0)
11337
0
    {
11338
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11339
0
              flinfo->internal_syms,
11340
0
              flinfo->external_syms,
11341
0
              flinfo->locsym_shndx);
11342
0
      if (isymbuf == NULL)
11343
0
  return false;
11344
0
    }
11345
11346
  /* Find local symbol sections and adjust values of symbols in
11347
     SEC_MERGE sections.  Write out those local symbols we know are
11348
     going into the output file.  */
11349
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11350
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11351
0
       isym < isymend;
11352
0
       isym++, pindex++, ppsection++)
11353
0
    {
11354
0
      asection *isec;
11355
0
      const char *name;
11356
0
      Elf_Internal_Sym osym;
11357
0
      long indx;
11358
0
      int ret;
11359
11360
0
      *pindex = -1;
11361
11362
0
      if (elf_bad_symtab (input_bfd))
11363
0
  {
11364
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11365
0
      {
11366
0
        *ppsection = NULL;
11367
0
        continue;
11368
0
      }
11369
0
  }
11370
11371
0
      if (isym->st_shndx == SHN_UNDEF)
11372
0
  isec = bfd_und_section_ptr;
11373
0
      else if (isym->st_shndx == SHN_ABS)
11374
0
  isec = bfd_abs_section_ptr;
11375
0
      else if (isym->st_shndx == SHN_COMMON)
11376
0
  isec = bfd_com_section_ptr;
11377
0
      else
11378
0
  {
11379
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11380
0
    if (isec == NULL)
11381
0
      {
11382
        /* Don't attempt to output symbols with st_shnx in the
11383
     reserved range other than SHN_ABS and SHN_COMMON.  */
11384
0
        isec = bfd_und_section_ptr;
11385
0
      }
11386
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11387
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11388
0
      isym->st_value =
11389
0
        _bfd_merged_section_offset (output_bfd, &isec,
11390
0
            elf_section_data (isec)->sec_info,
11391
0
            isym->st_value);
11392
0
  }
11393
11394
0
      *ppsection = isec;
11395
11396
      /* Don't output the first, undefined, symbol.  In fact, don't
11397
   output any undefined local symbol.  */
11398
0
      if (isec == bfd_und_section_ptr)
11399
0
  continue;
11400
11401
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11402
0
  {
11403
    /* We never output section symbols.  Instead, we use the
11404
       section symbol of the corresponding section in the output
11405
       file.  */
11406
0
    continue;
11407
0
  }
11408
11409
      /* If we are stripping all symbols, we don't want to output this
11410
   one.  */
11411
0
      if (flinfo->info->strip == strip_all)
11412
0
  continue;
11413
11414
      /* If we are discarding all local symbols, we don't want to
11415
   output this one.  If we are generating a relocatable output
11416
   file, then some of the local symbols may be required by
11417
   relocs; we output them below as we discover that they are
11418
   needed.  */
11419
0
      if (flinfo->info->discard == discard_all)
11420
0
  continue;
11421
11422
      /* If this symbol is defined in a section which we are
11423
   discarding, we don't need to keep it.  */
11424
0
      if (isym->st_shndx < SHN_LORESERVE
11425
0
    && (isec->output_section == NULL
11426
0
        || bfd_section_removed_from_list (output_bfd,
11427
0
            isec->output_section)))
11428
0
  continue;
11429
11430
      /* Get the name of the symbol.  */
11431
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11432
0
                isym->st_name);
11433
0
      if (name == NULL)
11434
0
  return false;
11435
11436
      /* See if we are discarding symbols with this name.  */
11437
0
      if ((flinfo->info->strip == strip_some
11438
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11439
0
         == NULL))
11440
0
    || (((flinfo->info->discard == discard_sec_merge
11441
0
    && (isec->flags & SEC_MERGE)
11442
0
    && !bfd_link_relocatable (flinfo->info))
11443
0
         || flinfo->info->discard == discard_l)
11444
0
        && bfd_is_local_label_name (input_bfd, name)))
11445
0
  continue;
11446
11447
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11448
0
  {
11449
0
    if (input_bfd->lto_output)
11450
      /* -flto puts a temp file name here.  This means builds
11451
         are not reproducible.  Discard the symbol.  */
11452
0
      continue;
11453
0
    have_file_sym = true;
11454
0
    flinfo->filesym_count += 1;
11455
0
  }
11456
0
      if (!have_file_sym)
11457
0
  {
11458
    /* In the absence of debug info, bfd_find_nearest_line uses
11459
       FILE symbols to determine the source file for local
11460
       function symbols.  Provide a FILE symbol here if input
11461
       files lack such, so that their symbols won't be
11462
       associated with a previous input file.  It's not the
11463
       source file, but the best we can do.  */
11464
0
    const char *filename;
11465
0
    have_file_sym = true;
11466
0
    flinfo->filesym_count += 1;
11467
0
    memset (&osym, 0, sizeof (osym));
11468
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11469
0
    osym.st_shndx = SHN_ABS;
11470
0
    if (input_bfd->lto_output)
11471
0
      filename = NULL;
11472
0
    else
11473
0
      filename = lbasename (bfd_get_filename (input_bfd));
11474
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11475
0
            bfd_abs_section_ptr, NULL))
11476
0
      return false;
11477
0
  }
11478
11479
0
      osym = *isym;
11480
11481
      /* Adjust the section index for the output file.  */
11482
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11483
0
               isec->output_section);
11484
0
      if (osym.st_shndx == SHN_BAD)
11485
0
  return false;
11486
11487
      /* ELF symbols in relocatable files are section relative, but
11488
   in executable files they are virtual addresses.  Note that
11489
   this code assumes that all ELF sections have an associated
11490
   BFD section with a reasonable value for output_offset; below
11491
   we assume that they also have a reasonable value for
11492
   output_section.  Any special sections must be set up to meet
11493
   these requirements.  */
11494
0
      osym.st_value += isec->output_offset;
11495
0
      if (!bfd_link_relocatable (flinfo->info))
11496
0
  {
11497
0
    osym.st_value += isec->output_section->vma;
11498
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11499
0
      {
11500
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11501
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11502
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11503
0
        else
11504
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11505
0
              STT_NOTYPE);
11506
0
      }
11507
0
  }
11508
11509
0
      indx = bfd_get_symcount (output_bfd);
11510
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11511
0
      if (ret == 0)
11512
0
  return false;
11513
0
      else if (ret == 1)
11514
0
  *pindex = indx;
11515
0
    }
11516
11517
0
  if (bed->s->arch_size == 32)
11518
0
    {
11519
0
      r_type_mask = 0xff;
11520
0
      r_sym_shift = 8;
11521
0
      address_size = 4;
11522
0
    }
11523
0
  else
11524
0
    {
11525
0
      r_type_mask = 0xffffffff;
11526
0
      r_sym_shift = 32;
11527
0
      address_size = 8;
11528
0
    }
11529
11530
  /* Relocate the contents of each section.  */
11531
0
  sym_hashes = elf_sym_hashes (input_bfd);
11532
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11533
0
    {
11534
0
      bfd_byte *contents;
11535
11536
0
      if (! o->linker_mark)
11537
0
  {
11538
    /* This section was omitted from the link.  */
11539
0
    continue;
11540
0
  }
11541
11542
0
      if (!flinfo->info->resolve_section_groups
11543
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11544
0
  {
11545
    /* Deal with the group signature symbol.  */
11546
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11547
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11548
0
    asection *osec = o->output_section;
11549
11550
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11551
0
    if (symndx >= locsymcount
11552
0
        || (elf_bad_symtab (input_bfd)
11553
0
      && flinfo->sections[symndx] == NULL))
11554
0
      {
11555
0
        struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11556
0
        while (h->root.type == bfd_link_hash_indirect
11557
0
         || h->root.type == bfd_link_hash_warning)
11558
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
11559
        /* Arrange for symbol to be output.  */
11560
0
        h->indx = -2;
11561
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11562
0
      }
11563
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11564
0
      {
11565
        /* We'll use the output section target_index.  */
11566
0
        asection *sec = flinfo->sections[symndx]->output_section;
11567
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11568
0
      }
11569
0
    else
11570
0
      {
11571
0
        if (flinfo->indices[symndx] == -1)
11572
0
    {
11573
      /* Otherwise output the local symbol now.  */
11574
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11575
0
      asection *sec = flinfo->sections[symndx]->output_section;
11576
0
      const char *name;
11577
0
      long indx;
11578
0
      int ret;
11579
11580
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11581
0
                symtab_hdr->sh_link,
11582
0
                sym.st_name);
11583
0
      if (name == NULL)
11584
0
        return false;
11585
11586
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11587
0
                    sec);
11588
0
      if (sym.st_shndx == SHN_BAD)
11589
0
        return false;
11590
11591
0
      sym.st_value += o->output_offset;
11592
11593
0
      indx = bfd_get_symcount (output_bfd);
11594
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11595
0
               NULL);
11596
0
      if (ret == 0)
11597
0
        return false;
11598
0
      else if (ret == 1)
11599
0
        flinfo->indices[symndx] = indx;
11600
0
      else
11601
0
        abort ();
11602
0
    }
11603
0
        elf_section_data (osec)->this_hdr.sh_info
11604
0
    = flinfo->indices[symndx];
11605
0
      }
11606
0
  }
11607
11608
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11609
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11610
0
  continue;
11611
11612
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11613
0
  {
11614
    /* Section was created by _bfd_elf_link_create_dynamic_sections
11615
       or somesuch.  */
11616
0
    continue;
11617
0
  }
11618
11619
      /* Get the contents of the section.  They have been cached by a
11620
   relaxation routine.  Note that o is a section in an input
11621
   file, so the contents field will not have been set by any of
11622
   the routines which work on output files.  */
11623
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11624
0
  {
11625
0
    contents = elf_section_data (o)->this_hdr.contents;
11626
0
    if (bed->caches_rawsize
11627
0
        && o->rawsize != 0
11628
0
        && o->rawsize < o->size)
11629
0
      {
11630
0
        memcpy (flinfo->contents, contents, o->rawsize);
11631
0
        contents = flinfo->contents;
11632
0
      }
11633
0
  }
11634
0
      else if (!(o->flags & SEC_RELOC)
11635
0
         && !bed->elf_backend_write_section
11636
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11637
  /* A MERGE section that has no relocations doesn't need the
11638
     contents anymore, they have been recorded earlier.  Except
11639
     if the backend has special provisions for writing sections.  */
11640
0
  contents = NULL;
11641
0
      else
11642
0
  {
11643
0
    contents = flinfo->contents;
11644
0
    if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11645
0
                 &contents))
11646
0
      return false;
11647
0
  }
11648
11649
0
      if ((o->flags & SEC_RELOC) != 0)
11650
0
  {
11651
0
    Elf_Internal_Rela *internal_relocs;
11652
0
    Elf_Internal_Rela *rel, *relend;
11653
0
    int action_discarded;
11654
0
    int ret;
11655
11656
    /* Get the swapped relocs.  */
11657
0
    internal_relocs
11658
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11659
0
                flinfo->external_relocs,
11660
0
                flinfo->internal_relocs,
11661
0
                false);
11662
0
    if (internal_relocs == NULL
11663
0
        && o->reloc_count > 0)
11664
0
      return false;
11665
11666
0
    action_discarded = -1;
11667
0
    if (!elf_section_ignore_discarded_relocs (o))
11668
0
      action_discarded = (*bed->action_discarded) (o);
11669
11670
    /* Run through the relocs evaluating complex reloc symbols and
11671
       looking for relocs against symbols from discarded sections
11672
       or section symbols from removed link-once sections.
11673
       Complain about relocs against discarded sections.  Zero
11674
       relocs against removed link-once sections.  */
11675
11676
0
    rel = internal_relocs;
11677
0
    relend = rel + o->reloc_count;
11678
0
    for ( ; rel < relend; rel++)
11679
0
      {
11680
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11681
0
        unsigned int s_type;
11682
0
        asection **ps, *sec;
11683
0
        struct elf_link_hash_entry *h = NULL;
11684
0
        const char *sym_name;
11685
11686
0
        if (r_symndx == STN_UNDEF)
11687
0
    continue;
11688
11689
0
        if (r_symndx >= locsymcount
11690
0
      || (elf_bad_symtab (input_bfd)
11691
0
          && flinfo->sections[r_symndx] == NULL))
11692
0
    {
11693
0
      h = sym_hashes[r_symndx - extsymoff];
11694
11695
      /* Badly formatted input files can contain relocs that
11696
         reference non-existant symbols.  Check here so that
11697
         we do not seg fault.  */
11698
0
      if (h == NULL)
11699
0
        {
11700
0
          _bfd_error_handler
11701
      /* xgettext:c-format */
11702
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11703
0
         "that references a non-existent global symbol"),
11704
0
       input_bfd, (uint64_t) rel->r_info, o);
11705
0
          bfd_set_error (bfd_error_bad_value);
11706
0
          return false;
11707
0
        }
11708
11709
0
      while (h->root.type == bfd_link_hash_indirect
11710
0
       || h->root.type == bfd_link_hash_warning)
11711
0
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
11712
11713
0
      s_type = h->type;
11714
11715
      /* If a plugin symbol is referenced from a non-IR file,
11716
         mark the symbol as undefined.  Note that the
11717
         linker may attach linker created dynamic sections
11718
         to the plugin bfd.  Symbols defined in linker
11719
         created sections are not plugin symbols.  */
11720
0
      if ((h->root.non_ir_ref_regular
11721
0
           || h->root.non_ir_ref_dynamic)
11722
0
          && (h->root.type == bfd_link_hash_defined
11723
0
        || h->root.type == bfd_link_hash_defweak)
11724
0
          && (h->root.u.def.section->flags
11725
0
        & SEC_LINKER_CREATED) == 0
11726
0
          && h->root.u.def.section->owner != NULL
11727
0
          && (h->root.u.def.section->owner->flags
11728
0
        & BFD_PLUGIN) != 0)
11729
0
        {
11730
0
          h->root.type = bfd_link_hash_undefined;
11731
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11732
0
        }
11733
11734
0
      ps = NULL;
11735
0
      if (h->root.type == bfd_link_hash_defined
11736
0
          || h->root.type == bfd_link_hash_defweak)
11737
0
        ps = &h->root.u.def.section;
11738
11739
0
      sym_name = h->root.root.string;
11740
0
    }
11741
0
        else
11742
0
    {
11743
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11744
11745
0
      s_type = ELF_ST_TYPE (sym->st_info);
11746
0
      ps = &flinfo->sections[r_symndx];
11747
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11748
0
                 sym, *ps);
11749
0
    }
11750
11751
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11752
0
      && !bfd_link_relocatable (flinfo->info))
11753
0
    {
11754
0
      bfd_vma val;
11755
0
      bfd_vma dot = (rel->r_offset
11756
0
         + o->output_offset + o->output_section->vma);
11757
#ifdef DEBUG
11758
      printf ("Encountered a complex symbol!");
11759
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11760
        bfd_get_filename (input_bfd), o->name,
11761
        (long) (rel - internal_relocs));
11762
      printf (" symbol: idx  %8.8lx, name %s\n",
11763
        r_symndx, sym_name);
11764
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11765
        (unsigned long) rel->r_info,
11766
        (unsigned long) rel->r_offset);
11767
#endif
11768
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11769
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11770
0
        return false;
11771
11772
      /* Symbol evaluated OK.  Update to absolute value.  */
11773
0
      set_symbol_value (input_bfd, isymbuf, locsymcount,
11774
0
            r_symndx, val);
11775
0
      continue;
11776
0
    }
11777
11778
0
        if (action_discarded != -1 && ps != NULL)
11779
0
    {
11780
      /* Complain if the definition comes from a
11781
         discarded section.  */
11782
0
      if ((sec = *ps) != NULL && discarded_section (sec))
11783
0
        {
11784
0
          BFD_ASSERT (r_symndx != STN_UNDEF);
11785
0
          if (action_discarded & COMPLAIN)
11786
0
      (*flinfo->info->callbacks->einfo)
11787
        /* xgettext:c-format */
11788
0
        (_("%X`%s' referenced in section `%pA' of %pB: "
11789
0
           "defined in discarded section `%pA' of %pB\n"),
11790
0
         sym_name, o, input_bfd, sec, sec->owner);
11791
11792
          /* Try to do the best we can to support buggy old
11793
       versions of gcc.  Pretend that the symbol is
11794
       really defined in the kept linkonce section.
11795
       FIXME: This is quite broken.  Modifying the
11796
       symbol here means we will be changing all later
11797
       uses of the symbol, not just in this section.  */
11798
0
          if (action_discarded & PRETEND)
11799
0
      {
11800
0
        asection *kept;
11801
11802
0
        kept = _bfd_elf_check_kept_section (sec,
11803
0
                    flinfo->info);
11804
0
        if (kept != NULL)
11805
0
          {
11806
0
            *ps = kept;
11807
0
            continue;
11808
0
          }
11809
0
      }
11810
0
        }
11811
0
    }
11812
0
      }
11813
11814
    /* Relocate the section by invoking a back end routine.
11815
11816
       The back end routine is responsible for adjusting the
11817
       section contents as necessary, and (if using Rela relocs
11818
       and generating a relocatable output file) adjusting the
11819
       reloc addend as necessary.
11820
11821
       The back end routine does not have to worry about setting
11822
       the reloc address or the reloc symbol index.
11823
11824
       The back end routine is given a pointer to the swapped in
11825
       internal symbols, and can access the hash table entries
11826
       for the external symbols via elf_sym_hashes (input_bfd).
11827
11828
       When generating relocatable output, the back end routine
11829
       must handle STB_LOCAL/STT_SECTION symbols specially.  The
11830
       output symbol is going to be a section symbol
11831
       corresponding to the output section, which will require
11832
       the addend to be adjusted.  */
11833
11834
0
    ret = (*relocate_section) (output_bfd, flinfo->info,
11835
0
             input_bfd, o, contents,
11836
0
             internal_relocs,
11837
0
             isymbuf,
11838
0
             flinfo->sections);
11839
0
    if (!ret)
11840
0
      return false;
11841
11842
0
    if (ret == 2
11843
0
        || bfd_link_relocatable (flinfo->info)
11844
0
        || flinfo->info->emitrelocations)
11845
0
      {
11846
0
        Elf_Internal_Rela *irela;
11847
0
        Elf_Internal_Rela *irelaend, *irelamid;
11848
0
        bfd_vma last_offset;
11849
0
        struct elf_link_hash_entry **rel_hash;
11850
0
        struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11851
0
        Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11852
0
        unsigned int next_erel;
11853
0
        bool rela_normal;
11854
0
        struct bfd_elf_section_data *esdi, *esdo;
11855
11856
0
        esdi = elf_section_data (o);
11857
0
        esdo = elf_section_data (o->output_section);
11858
0
        rela_normal = false;
11859
11860
        /* Adjust the reloc addresses and symbol indices.  */
11861
11862
0
        irela = internal_relocs;
11863
0
        irelaend = irela + o->reloc_count;
11864
0
        rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11865
        /* We start processing the REL relocs, if any.  When we reach
11866
     IRELAMID in the loop, we switch to the RELA relocs.  */
11867
0
        irelamid = irela;
11868
0
        if (esdi->rel.hdr != NULL)
11869
0
    irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11870
0
           * bed->s->int_rels_per_ext_rel);
11871
0
        rel_hash_list = rel_hash;
11872
0
        rela_hash_list = NULL;
11873
0
        last_offset = o->output_offset;
11874
0
        if (!bfd_link_relocatable (flinfo->info))
11875
0
    last_offset += o->output_section->vma;
11876
0
        for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11877
0
    {
11878
0
      unsigned long r_symndx;
11879
0
      asection *sec;
11880
0
      Elf_Internal_Sym sym;
11881
11882
0
      if (next_erel == bed->s->int_rels_per_ext_rel)
11883
0
        {
11884
0
          rel_hash++;
11885
0
          next_erel = 0;
11886
0
        }
11887
11888
0
      if (irela == irelamid)
11889
0
        {
11890
0
          rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11891
0
          rela_hash_list = rel_hash;
11892
0
          if (bed->is_rela_normal != NULL)
11893
0
      rela_normal = bed->is_rela_normal (irela);
11894
0
          else
11895
0
      rela_normal = bed->rela_normal;
11896
0
        }
11897
11898
0
      irela->r_offset = _bfd_elf_section_offset (output_bfd,
11899
0
                   flinfo->info, o,
11900
0
                   irela->r_offset);
11901
0
      if (irela->r_offset >= (bfd_vma) -2)
11902
0
        {
11903
          /* This is a reloc for a deleted entry or somesuch.
11904
       Turn it into an R_*_NONE reloc, at the same
11905
       offset as the last reloc.  elf_eh_frame.c and
11906
       bfd_elf_discard_info rely on reloc offsets
11907
       being ordered.  */
11908
0
          irela->r_offset = last_offset;
11909
0
          irela->r_info = 0;
11910
0
          irela->r_addend = 0;
11911
0
          continue;
11912
0
        }
11913
11914
0
      irela->r_offset += o->output_offset;
11915
11916
      /* Relocs in an executable have to be virtual addresses.  */
11917
0
      if (!bfd_link_relocatable (flinfo->info))
11918
0
        irela->r_offset += o->output_section->vma;
11919
11920
0
      last_offset = irela->r_offset;
11921
11922
0
      r_symndx = irela->r_info >> r_sym_shift;
11923
0
      if (r_symndx == STN_UNDEF)
11924
0
        continue;
11925
11926
0
      if (r_symndx >= locsymcount
11927
0
          || (elf_bad_symtab (input_bfd)
11928
0
        && flinfo->sections[r_symndx] == NULL))
11929
0
        {
11930
0
          struct elf_link_hash_entry *rh;
11931
0
          unsigned long indx;
11932
11933
          /* This is a reloc against a global symbol.  We
11934
       have not yet output all the local symbols, so
11935
       we do not know the symbol index of any global
11936
       symbol.  We set the rel_hash entry for this
11937
       reloc to point to the global hash table entry
11938
       for this symbol.  The symbol index is then
11939
       set at the end of bfd_elf_final_link.  */
11940
0
          indx = r_symndx - extsymoff;
11941
0
          rh = elf_sym_hashes (input_bfd)[indx];
11942
0
          while (rh->root.type == bfd_link_hash_indirect
11943
0
           || rh->root.type == bfd_link_hash_warning)
11944
0
      rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11945
11946
          /* Setting the index to -2 tells
11947
       elf_link_output_extsym that this symbol is
11948
       used by a reloc.  */
11949
0
          BFD_ASSERT (rh->indx < 0);
11950
0
          rh->indx = -2;
11951
0
          *rel_hash = rh;
11952
11953
0
          continue;
11954
0
        }
11955
11956
      /* This is a reloc against a local symbol.  */
11957
11958
0
      *rel_hash = NULL;
11959
0
      sym = isymbuf[r_symndx];
11960
0
      sec = flinfo->sections[r_symndx];
11961
0
      if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11962
0
        {
11963
          /* I suppose the backend ought to fill in the
11964
       section of any STT_SECTION symbol against a
11965
       processor specific section.  */
11966
0
          r_symndx = STN_UNDEF;
11967
0
          if (bfd_is_abs_section (sec))
11968
0
      ;
11969
0
          else if (sec == NULL || sec->owner == NULL)
11970
0
      {
11971
0
        bfd_set_error (bfd_error_bad_value);
11972
0
        return false;
11973
0
      }
11974
0
          else
11975
0
      {
11976
0
        asection *osec = sec->output_section;
11977
11978
        /* If we have discarded a section, the output
11979
           section will be the absolute section.  In
11980
           case of discarded SEC_MERGE sections, use
11981
           the kept section.  relocate_section should
11982
           have already handled discarded linkonce
11983
           sections.  */
11984
0
        if (bfd_is_abs_section (osec)
11985
0
            && sec->kept_section != NULL
11986
0
            && sec->kept_section->output_section != NULL)
11987
0
          {
11988
0
            osec = sec->kept_section->output_section;
11989
0
            irela->r_addend -= osec->vma;
11990
0
          }
11991
11992
0
        if (!bfd_is_abs_section (osec))
11993
0
          {
11994
0
            r_symndx = osec->target_index;
11995
0
            if (r_symndx == STN_UNDEF)
11996
0
        {
11997
0
          irela->r_addend += osec->vma;
11998
0
          osec = _bfd_nearby_section (output_bfd, osec,
11999
0
                    osec->vma);
12000
0
          irela->r_addend -= osec->vma;
12001
0
          r_symndx = osec->target_index;
12002
0
        }
12003
0
          }
12004
0
      }
12005
12006
          /* Adjust the addend according to where the
12007
       section winds up in the output section.  */
12008
0
          if (rela_normal)
12009
0
      irela->r_addend += sec->output_offset;
12010
0
        }
12011
0
      else
12012
0
        {
12013
0
          if (flinfo->indices[r_symndx] == -1)
12014
0
      {
12015
0
        unsigned long shlink;
12016
0
        const char *name;
12017
0
        asection *osec;
12018
0
        long indx;
12019
12020
0
        if (flinfo->info->strip == strip_all)
12021
0
          {
12022
            /* You can't do ld -r -s.  */
12023
0
            bfd_set_error (bfd_error_invalid_operation);
12024
0
            return false;
12025
0
          }
12026
12027
        /* This symbol was skipped earlier, but
12028
           since it is needed by a reloc, we
12029
           must output it now.  */
12030
0
        shlink = symtab_hdr->sh_link;
12031
0
        name = (bfd_elf_string_from_elf_section
12032
0
          (input_bfd, shlink, sym.st_name));
12033
0
        if (name == NULL)
12034
0
          return false;
12035
12036
0
        osec = sec->output_section;
12037
0
        sym.st_shndx =
12038
0
          _bfd_elf_section_from_bfd_section (output_bfd,
12039
0
                     osec);
12040
0
        if (sym.st_shndx == SHN_BAD)
12041
0
          return false;
12042
12043
0
        sym.st_value += sec->output_offset;
12044
0
        if (!bfd_link_relocatable (flinfo->info))
12045
0
          {
12046
0
            sym.st_value += osec->vma;
12047
0
            if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12048
0
        {
12049
0
          struct elf_link_hash_table *htab
12050
0
            = elf_hash_table (flinfo->info);
12051
12052
          /* STT_TLS symbols are relative to PT_TLS
12053
             segment base.  */
12054
0
          if (htab->tls_sec != NULL)
12055
0
            sym.st_value -= htab->tls_sec->vma;
12056
0
          else
12057
0
            sym.st_info
12058
0
              = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12059
0
                 STT_NOTYPE);
12060
0
        }
12061
0
          }
12062
12063
0
        indx = bfd_get_symcount (output_bfd);
12064
0
        ret = elf_link_output_symstrtab (flinfo, name,
12065
0
                 &sym, sec,
12066
0
                 NULL);
12067
0
        if (ret == 0)
12068
0
          return false;
12069
0
        else if (ret == 1)
12070
0
          flinfo->indices[r_symndx] = indx;
12071
0
        else
12072
0
          abort ();
12073
0
      }
12074
12075
0
          r_symndx = flinfo->indices[r_symndx];
12076
0
        }
12077
12078
0
      irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12079
0
           | (irela->r_info & r_type_mask));
12080
0
    }
12081
12082
        /* Swap out the relocs.  */
12083
0
        input_rel_hdr = esdi->rel.hdr;
12084
0
        if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12085
0
    {
12086
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12087
0
                 input_rel_hdr,
12088
0
                 internal_relocs,
12089
0
                 rel_hash_list))
12090
0
        return false;
12091
0
      internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12092
0
              * bed->s->int_rels_per_ext_rel);
12093
0
      rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12094
0
    }
12095
12096
0
        input_rela_hdr = esdi->rela.hdr;
12097
0
        if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12098
0
    {
12099
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12100
0
                 input_rela_hdr,
12101
0
                 internal_relocs,
12102
0
                 rela_hash_list))
12103
0
        return false;
12104
0
    }
12105
0
      }
12106
0
  }
12107
12108
      /* Write out the modified section contents.  */
12109
0
      if (bed->elf_backend_write_section
12110
0
    && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12111
0
            contents))
12112
0
  {
12113
    /* Section written out.  */
12114
0
  }
12115
0
      else switch (o->sec_info_type)
12116
0
  {
12117
0
  case SEC_INFO_TYPE_STABS:
12118
0
    if (! (_bfd_write_section_stabs
12119
0
     (output_bfd,
12120
0
      &elf_hash_table (flinfo->info)->stab_info,
12121
0
      o, &elf_section_data (o)->sec_info, contents)))
12122
0
      return false;
12123
0
    break;
12124
0
  case SEC_INFO_TYPE_MERGE:
12125
0
    if (! _bfd_write_merged_section (output_bfd, o,
12126
0
             elf_section_data (o)->sec_info))
12127
0
      return false;
12128
0
    break;
12129
0
  case SEC_INFO_TYPE_EH_FRAME:
12130
0
    {
12131
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12132
0
               o, contents))
12133
0
        return false;
12134
0
    }
12135
0
    break;
12136
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12137
0
    {
12138
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12139
0
               flinfo->info,
12140
0
               o, contents))
12141
0
        return false;
12142
0
    }
12143
0
    break;
12144
0
  case SEC_INFO_TYPE_SFRAME:
12145
0
      {
12146
        /* Merge .sframe sections into the ctf frame encoder
12147
     context of the output_bfd's section.  The final .sframe
12148
     output section will be written out later.  */
12149
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12150
0
              o, contents))
12151
0
    return false;
12152
0
      }
12153
0
      break;
12154
0
  default:
12155
0
    {
12156
0
      if (! (o->flags & SEC_EXCLUDE))
12157
0
        {
12158
0
    file_ptr offset = (file_ptr) o->output_offset;
12159
0
    bfd_size_type todo = o->size;
12160
12161
0
    offset *= bfd_octets_per_byte (output_bfd, o);
12162
12163
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
12164
0
        && o->size > address_size)
12165
0
      {
12166
        /* Reverse-copy input section to output.  */
12167
12168
0
        if ((o->size & (address_size - 1)) != 0
12169
0
      || (o->reloc_count != 0
12170
0
          && (o->size * bed->s->int_rels_per_ext_rel
12171
0
        != o->reloc_count * address_size)))
12172
0
          {
12173
0
      _bfd_error_handler
12174
        /* xgettext:c-format */
12175
0
        (_("error: %pB: size of section %pA is not "
12176
0
           "multiple of address size"),
12177
0
         input_bfd, o);
12178
0
      bfd_set_error (bfd_error_bad_value);
12179
0
      return false;
12180
0
          }
12181
12182
0
        do
12183
0
          {
12184
0
      todo -= address_size;
12185
0
      if (! bfd_set_section_contents (output_bfd,
12186
0
              o->output_section,
12187
0
              contents + todo,
12188
0
              offset,
12189
0
              address_size))
12190
0
        return false;
12191
0
      if (todo == 0)
12192
0
        break;
12193
0
      offset += address_size;
12194
0
          }
12195
0
        while (1);
12196
0
      }
12197
0
    else if (! bfd_set_section_contents (output_bfd,
12198
0
                 o->output_section,
12199
0
                 contents,
12200
0
                 offset, todo))
12201
0
      return false;
12202
0
        }
12203
0
    }
12204
0
    break;
12205
0
  }
12206
12207
      /* Munmap the section contents for each input section.  */
12208
0
      _bfd_elf_link_munmap_section_contents (o);
12209
0
    }
12210
12211
0
  return true;
12212
0
}
12213
12214
/* Generate a reloc when linking an ELF file.  This is a reloc
12215
   requested by the linker, and does not come from any input file.  This
12216
   is used to build constructor and destructor tables when linking
12217
   with -Ur.  */
12218
12219
static bool
12220
elf_reloc_link_order (bfd *output_bfd,
12221
          struct bfd_link_info *info,
12222
          asection *output_section,
12223
          struct bfd_link_order *link_order)
12224
0
{
12225
0
  reloc_howto_type *howto;
12226
0
  long indx;
12227
0
  bfd_vma offset;
12228
0
  bfd_vma addend;
12229
0
  struct bfd_elf_section_reloc_data *reldata;
12230
0
  struct elf_link_hash_entry **rel_hash_ptr;
12231
0
  Elf_Internal_Shdr *rel_hdr;
12232
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12233
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12234
0
  bfd_byte *erel;
12235
0
  unsigned int i;
12236
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12237
12238
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12239
0
  if (howto == NULL)
12240
0
    {
12241
0
      bfd_set_error (bfd_error_bad_value);
12242
0
      return false;
12243
0
    }
12244
12245
0
  addend = link_order->u.reloc.p->addend;
12246
12247
0
  if (esdo->rel.hdr)
12248
0
    reldata = &esdo->rel;
12249
0
  else if (esdo->rela.hdr)
12250
0
    reldata = &esdo->rela;
12251
0
  else
12252
0
    {
12253
0
      reldata = NULL;
12254
0
      BFD_ASSERT (0);
12255
0
    }
12256
12257
  /* Figure out the symbol index.  */
12258
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12259
0
  if (link_order->type == bfd_section_reloc_link_order)
12260
0
    {
12261
0
      indx = link_order->u.reloc.p->u.section->target_index;
12262
0
      BFD_ASSERT (indx != 0);
12263
0
      *rel_hash_ptr = NULL;
12264
0
    }
12265
0
  else
12266
0
    {
12267
0
      struct elf_link_hash_entry *h;
12268
12269
      /* Treat a reloc against a defined symbol as though it were
12270
   actually against the section.  */
12271
0
      h = ((struct elf_link_hash_entry *)
12272
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12273
0
           link_order->u.reloc.p->u.name,
12274
0
           false, false, true));
12275
0
      if (h != NULL
12276
0
    && (h->root.type == bfd_link_hash_defined
12277
0
        || h->root.type == bfd_link_hash_defweak))
12278
0
  {
12279
0
    asection *section;
12280
12281
0
    section = h->root.u.def.section;
12282
0
    indx = section->output_section->target_index;
12283
0
    *rel_hash_ptr = NULL;
12284
    /* It seems that we ought to add the symbol value to the
12285
       addend here, but in practice it has already been added
12286
       because it was passed to constructor_callback.  */
12287
0
    addend += section->output_section->vma + section->output_offset;
12288
0
  }
12289
0
      else if (h != NULL)
12290
0
  {
12291
    /* Setting the index to -2 tells elf_link_output_extsym that
12292
       this symbol is used by a reloc.  */
12293
0
    h->indx = -2;
12294
0
    *rel_hash_ptr = h;
12295
0
    indx = 0;
12296
0
  }
12297
0
      else
12298
0
  {
12299
0
    (*info->callbacks->unattached_reloc)
12300
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12301
0
    indx = 0;
12302
0
  }
12303
0
    }
12304
12305
  /* If this is an inplace reloc, we must write the addend into the
12306
     object file.  */
12307
0
  if (howto->partial_inplace && addend != 0)
12308
0
    {
12309
0
      bfd_size_type size;
12310
0
      bfd_reloc_status_type rstat;
12311
0
      bfd_byte *buf;
12312
0
      bool ok;
12313
0
      const char *sym_name;
12314
0
      bfd_size_type octets;
12315
12316
0
      size = (bfd_size_type) bfd_get_reloc_size (howto);
12317
0
      buf = (bfd_byte *) bfd_zmalloc (size);
12318
0
      if (buf == NULL && size != 0)
12319
0
  return false;
12320
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12321
0
      switch (rstat)
12322
0
  {
12323
0
  case bfd_reloc_ok:
12324
0
    break;
12325
12326
0
  default:
12327
0
  case bfd_reloc_outofrange:
12328
0
    abort ();
12329
12330
0
  case bfd_reloc_overflow:
12331
0
    if (link_order->type == bfd_section_reloc_link_order)
12332
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12333
0
    else
12334
0
      sym_name = link_order->u.reloc.p->u.name;
12335
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12336
0
                howto->name, addend, NULL, NULL,
12337
0
                (bfd_vma) 0);
12338
0
    break;
12339
0
  }
12340
12341
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12342
0
               output_section);
12343
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12344
0
             octets, size);
12345
0
      free (buf);
12346
0
      if (! ok)
12347
0
  return false;
12348
0
    }
12349
12350
  /* The address of a reloc is relative to the section in a
12351
     relocatable file, and is a virtual address in an executable
12352
     file.  */
12353
0
  offset = link_order->offset;
12354
0
  if (! bfd_link_relocatable (info))
12355
0
    offset += output_section->vma;
12356
12357
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12358
0
    {
12359
0
      irel[i].r_offset = offset;
12360
0
      irel[i].r_info = 0;
12361
0
      irel[i].r_addend = 0;
12362
0
    }
12363
0
  if (bed->s->arch_size == 32)
12364
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12365
0
  else
12366
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12367
12368
0
  rel_hdr = reldata->hdr;
12369
0
  erel = rel_hdr->contents;
12370
0
  if (rel_hdr->sh_type == SHT_REL)
12371
0
    {
12372
0
      erel += reldata->count * bed->s->sizeof_rel;
12373
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12374
0
    }
12375
0
  else
12376
0
    {
12377
0
      irel[0].r_addend = addend;
12378
0
      erel += reldata->count * bed->s->sizeof_rela;
12379
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12380
0
    }
12381
12382
0
  ++reldata->count;
12383
12384
0
  return true;
12385
0
}
12386
12387
/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12388
   Returns TRUE upon success, FALSE otherwise.  */
12389
12390
static bool
12391
elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12392
0
{
12393
0
  bool ret = false;
12394
0
  bfd *implib_bfd;
12395
0
  const struct elf_backend_data *bed;
12396
0
  flagword flags;
12397
0
  enum bfd_architecture arch;
12398
0
  unsigned int mach;
12399
0
  asymbol **sympp = NULL;
12400
0
  long symsize;
12401
0
  long symcount;
12402
0
  long src_count;
12403
0
  elf_symbol_type *osymbuf;
12404
0
  size_t amt;
12405
12406
0
  implib_bfd = info->out_implib_bfd;
12407
0
  bed = get_elf_backend_data (abfd);
12408
12409
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12410
0
    return false;
12411
12412
  /* Use flag from executable but make it a relocatable object.  */
12413
0
  flags = bfd_get_file_flags (abfd);
12414
0
  flags &= ~HAS_RELOC;
12415
0
  if (!bfd_set_start_address (implib_bfd, 0)
12416
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12417
0
    return false;
12418
12419
  /* Copy architecture of output file to import library file.  */
12420
0
  arch = bfd_get_arch (abfd);
12421
0
  mach = bfd_get_mach (abfd);
12422
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12423
0
      && (abfd->target_defaulted
12424
0
    || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12425
0
    return false;
12426
12427
  /* Get symbol table size.  */
12428
0
  symsize = bfd_get_symtab_upper_bound (abfd);
12429
0
  if (symsize < 0)
12430
0
    return false;
12431
12432
  /* Read in the symbol table.  */
12433
0
  sympp = (asymbol **) bfd_malloc (symsize);
12434
0
  if (sympp == NULL)
12435
0
    return false;
12436
12437
0
  symcount = bfd_canonicalize_symtab (abfd, sympp);
12438
0
  if (symcount < 0)
12439
0
    goto free_sym_buf;
12440
12441
  /* Allow the BFD backend to copy any private header data it
12442
     understands from the output BFD to the import library BFD.  */
12443
0
  if (! bfd_copy_private_header_data (abfd, implib_bfd))
12444
0
    goto free_sym_buf;
12445
12446
  /* Filter symbols to appear in the import library.  */
12447
0
  if (bed->elf_backend_filter_implib_symbols)
12448
0
    symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12449
0
                   symcount);
12450
0
  else
12451
0
    symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12452
0
  if (symcount == 0)
12453
0
    {
12454
0
      bfd_set_error (bfd_error_no_symbols);
12455
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12456
0
        implib_bfd);
12457
0
      goto free_sym_buf;
12458
0
    }
12459
12460
12461
  /* Make symbols absolute.  */
12462
0
  amt = symcount * sizeof (*osymbuf);
12463
0
  osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12464
0
  if (osymbuf == NULL)
12465
0
    goto free_sym_buf;
12466
12467
0
  for (src_count = 0; src_count < symcount; src_count++)
12468
0
    {
12469
0
      memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12470
0
        sizeof (*osymbuf));
12471
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12472
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12473
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12474
0
      osymbuf[src_count].internal_elf_sym.st_value =
12475
0
  osymbuf[src_count].symbol.value;
12476
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12477
0
    }
12478
12479
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12480
12481
  /* Allow the BFD backend to copy any private data it understands
12482
     from the output BFD to the import library BFD.  This is done last
12483
     to permit the routine to look at the filtered symbol table.  */
12484
0
  if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12485
0
    goto free_sym_buf;
12486
12487
0
  if (!bfd_close (implib_bfd))
12488
0
    goto free_sym_buf;
12489
12490
0
  ret = true;
12491
12492
0
 free_sym_buf:
12493
0
  free (sympp);
12494
0
  return ret;
12495
0
}
12496
12497
static void
12498
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12499
0
{
12500
0
  asection *o;
12501
12502
0
  if (flinfo->symstrtab != NULL)
12503
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12504
0
  free (flinfo->contents);
12505
0
  free (flinfo->external_relocs);
12506
0
  free (flinfo->internal_relocs);
12507
0
  free (flinfo->external_syms);
12508
0
  free (flinfo->locsym_shndx);
12509
0
  free (flinfo->internal_syms);
12510
0
  free (flinfo->indices);
12511
0
  free (flinfo->sections);
12512
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12513
0
    free (flinfo->symshndxbuf);
12514
0
  for (o = obfd->sections; o != NULL; o = o->next)
12515
0
    {
12516
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12517
0
      free (esdo->rel.hashes);
12518
0
      free (esdo->rela.hashes);
12519
0
    }
12520
0
}
12521
12522
/* Do the final step of an ELF link.  */
12523
12524
bool
12525
bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12526
0
{
12527
0
  bool dynamic;
12528
0
  bool emit_relocs;
12529
0
  bfd *dynobj;
12530
0
  struct elf_final_link_info flinfo;
12531
0
  asection *o;
12532
0
  struct bfd_link_order *p;
12533
0
  bfd *sub;
12534
0
  bfd_size_type max_contents_size;
12535
0
  bfd_size_type max_external_reloc_size;
12536
0
  bfd_size_type max_internal_reloc_count;
12537
0
  bfd_size_type max_sym_count;
12538
0
  bfd_size_type max_sym_shndx_count;
12539
0
  Elf_Internal_Sym elfsym;
12540
0
  unsigned int i;
12541
0
  Elf_Internal_Shdr *symtab_hdr;
12542
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12543
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12544
0
  struct elf_outext_info eoinfo;
12545
0
  bool merged;
12546
0
  size_t relativecount;
12547
0
  size_t relr_entsize;
12548
0
  asection *reldyn = 0;
12549
0
  bfd_size_type amt;
12550
0
  asection *attr_section = NULL;
12551
0
  bfd_vma attr_size = 0;
12552
0
  const char *std_attrs_section;
12553
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12554
0
  bool sections_removed;
12555
0
  bool ret;
12556
12557
0
  if (!is_elf_hash_table (&htab->root))
12558
0
    return false;
12559
12560
0
  if (bfd_link_pic (info))
12561
0
    abfd->flags |= DYNAMIC;
12562
12563
0
  dynamic = htab->dynamic_sections_created;
12564
0
  dynobj = htab->dynobj;
12565
12566
0
  emit_relocs = (bfd_link_relocatable (info)
12567
0
     || info->emitrelocations);
12568
12569
0
  memset (&flinfo, 0, sizeof (flinfo));
12570
0
  flinfo.info = info;
12571
0
  flinfo.output_bfd = abfd;
12572
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12573
0
  if (flinfo.symstrtab == NULL)
12574
0
    return false;
12575
12576
0
  if (! dynamic)
12577
0
    {
12578
0
      flinfo.hash_sec = NULL;
12579
0
      flinfo.symver_sec = NULL;
12580
0
    }
12581
0
  else
12582
0
    {
12583
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12584
      /* Note that dynsym_sec can be NULL (on VMS).  */
12585
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12586
      /* Note that it is OK if symver_sec is NULL.  */
12587
0
    }
12588
12589
0
  if (info->unique_symbol
12590
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12591
0
             local_hash_newfunc,
12592
0
             sizeof (struct local_hash_entry)))
12593
0
    return false;
12594
12595
  /* The object attributes have been merged.  Remove the input
12596
     sections from the link, and set the contents of the output
12597
     section.  */
12598
0
  sections_removed = false;
12599
0
  std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12600
0
  for (o = abfd->sections; o != NULL; o = o->next)
12601
0
    {
12602
0
      bool remove_section = false;
12603
12604
0
      if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12605
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12606
0
  {
12607
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12608
0
      {
12609
0
        asection *input_section;
12610
12611
0
        if (p->type != bfd_indirect_link_order)
12612
0
    continue;
12613
0
        input_section = p->u.indirect.section;
12614
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12615
     elf_link_input_bfd ignores this section.  */
12616
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12617
0
      }
12618
12619
0
    attr_size = bfd_elf_obj_attr_size (abfd);
12620
0
    bfd_set_section_size (o, attr_size);
12621
    /* Skip this section later on.  */
12622
0
    o->map_head.link_order = NULL;
12623
0
    if (attr_size)
12624
0
      attr_section = o;
12625
0
    else
12626
0
      remove_section = true;
12627
0
  }
12628
0
      else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12629
0
  {
12630
    /* Remove empty group section from linker output.  */
12631
0
    remove_section = true;
12632
0
  }
12633
0
      if (remove_section)
12634
0
  {
12635
0
    o->flags |= SEC_EXCLUDE;
12636
0
    bfd_section_list_remove (abfd, o);
12637
0
    abfd->section_count--;
12638
0
    sections_removed = true;
12639
0
  }
12640
0
    }
12641
0
  if (sections_removed)
12642
0
    _bfd_fix_excluded_sec_syms (abfd, info);
12643
12644
  /* Count up the number of relocations we will output for each output
12645
     section, so that we know the sizes of the reloc sections.  We
12646
     also figure out some maximum sizes.  */
12647
0
#ifdef USE_MMAP
12648
0
  if (bed->use_mmap)
12649
0
    {
12650
      /* Mmap is used only if section size >= the minimum mmap section
12651
   size.  The initial max_contents_size value covers all sections
12652
   smaller than the minimum mmap section size.  It may be increased
12653
   for compressed or linker created sections or sections whose
12654
   rawsize != size.  max_external_reloc_size covers all relocation
12655
   sections smaller than the minimum mmap section size.  */
12656
0
      max_contents_size = _bfd_minimum_mmap_size;
12657
0
      max_external_reloc_size = _bfd_minimum_mmap_size;
12658
0
    }
12659
0
  else
12660
0
#endif
12661
0
    {
12662
0
      max_contents_size = 0;
12663
0
      max_external_reloc_size = 0;
12664
0
    }
12665
0
  max_internal_reloc_count = 0;
12666
0
  max_sym_count = 0;
12667
0
  max_sym_shndx_count = 0;
12668
0
  merged = false;
12669
0
  for (o = abfd->sections; o != NULL; o = o->next)
12670
0
    {
12671
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12672
0
      o->reloc_count = 0;
12673
12674
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12675
0
  {
12676
0
    unsigned int reloc_count = 0;
12677
0
    unsigned int additional_reloc_count = 0;
12678
0
    struct bfd_elf_section_data *esdi = NULL;
12679
12680
0
    if (p->type == bfd_section_reloc_link_order
12681
0
        || p->type == bfd_symbol_reloc_link_order)
12682
0
      reloc_count = 1;
12683
0
    else if (p->type == bfd_indirect_link_order)
12684
0
      {
12685
0
        asection *sec;
12686
12687
0
        sec = p->u.indirect.section;
12688
12689
        /* Mark all sections which are to be included in the
12690
     link.  This will normally be every section.  We need
12691
     to do this so that we can identify any sections which
12692
     the linker has decided to not include.  */
12693
0
        sec->linker_mark = true;
12694
12695
0
        if (sec->flags & SEC_MERGE)
12696
0
    merged = true;
12697
12698
0
#ifdef USE_MMAP
12699
        /* Mmap is used only on non-compressed, non-linker created
12700
     sections whose rawsize == size.  */
12701
0
        if (!bed->use_mmap
12702
0
      || sec->compress_status != COMPRESS_SECTION_NONE
12703
0
      || (sec->flags & SEC_LINKER_CREATED) != 0
12704
0
      || sec->rawsize != sec->size)
12705
0
#endif
12706
0
    {
12707
0
      if (sec->rawsize > max_contents_size)
12708
0
        max_contents_size = sec->rawsize;
12709
0
      if (sec->size > max_contents_size)
12710
0
        max_contents_size = sec->size;
12711
0
    }
12712
12713
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12714
0
      && (sec->owner->flags & DYNAMIC) == 0)
12715
0
    {
12716
0
      size_t sym_count;
12717
12718
      /* We are interested in just local symbols, not all
12719
         symbols.  */
12720
0
      if (elf_bad_symtab (sec->owner))
12721
0
        sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12722
0
         / bed->s->sizeof_sym);
12723
0
      else
12724
0
        sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12725
12726
0
      if (sym_count > max_sym_count)
12727
0
        max_sym_count = sym_count;
12728
12729
0
      if (sym_count > max_sym_shndx_count
12730
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12731
0
        max_sym_shndx_count = sym_count;
12732
12733
0
      esdi = elf_section_data (sec);
12734
12735
0
      if (esdi->this_hdr.sh_type == SHT_REL
12736
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12737
        /* Some backends use reloc_count in relocation sections
12738
           to count particular types of relocs.  Of course,
12739
           reloc sections themselves can't have relocations.  */
12740
0
        ;
12741
0
      else if (emit_relocs)
12742
0
        {
12743
0
          reloc_count = sec->reloc_count;
12744
0
          if (bed->elf_backend_count_additional_relocs)
12745
0
      {
12746
0
        int c;
12747
0
        c = (*bed->elf_backend_count_additional_relocs) (sec);
12748
0
        additional_reloc_count += c;
12749
0
      }
12750
0
        }
12751
0
      else if (bed->elf_backend_count_relocs)
12752
0
        reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12753
12754
0
      if ((sec->flags & SEC_RELOC) != 0)
12755
0
        {
12756
0
#ifdef USE_MMAP
12757
0
          if (!bed->use_mmap)
12758
0
#endif
12759
0
      {
12760
0
        size_t ext_size = 0;
12761
12762
0
        if (esdi->rel.hdr != NULL)
12763
0
          ext_size = esdi->rel.hdr->sh_size;
12764
0
        if (esdi->rela.hdr != NULL)
12765
0
          ext_size += esdi->rela.hdr->sh_size;
12766
12767
0
        if (ext_size > max_external_reloc_size)
12768
0
          max_external_reloc_size = ext_size;
12769
0
      }
12770
0
          if (sec->reloc_count > max_internal_reloc_count)
12771
0
      max_internal_reloc_count = sec->reloc_count;
12772
0
        }
12773
0
    }
12774
0
      }
12775
12776
0
    if (reloc_count == 0)
12777
0
      continue;
12778
12779
0
    reloc_count += additional_reloc_count;
12780
0
    o->reloc_count += reloc_count;
12781
12782
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12783
0
      {
12784
0
        if (esdi->rel.hdr)
12785
0
    {
12786
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12787
0
      esdo->rel.count += additional_reloc_count;
12788
0
    }
12789
0
        if (esdi->rela.hdr)
12790
0
    {
12791
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12792
0
      esdo->rela.count += additional_reloc_count;
12793
0
    }
12794
0
      }
12795
0
    else
12796
0
      {
12797
0
        if (o->use_rela_p)
12798
0
    esdo->rela.count += reloc_count;
12799
0
        else
12800
0
    esdo->rel.count += reloc_count;
12801
0
      }
12802
0
  }
12803
12804
0
      if (o->reloc_count > 0)
12805
0
  o->flags |= SEC_RELOC;
12806
0
      else
12807
0
  {
12808
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12809
       set it (this is probably a bug) and if it is set
12810
       assign_section_numbers will create a reloc section.  */
12811
0
    o->flags &=~ SEC_RELOC;
12812
0
  }
12813
12814
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12815
   zero.  This is done in elf_fake_sections as well, but forcing
12816
   the VMA to 0 here will ensure that relocs against these
12817
   sections are handled correctly.  */
12818
0
      if ((o->flags & SEC_ALLOC) == 0
12819
0
    && ! o->user_set_vma)
12820
0
  o->vma = 0;
12821
0
    }
12822
12823
0
  if (! bfd_link_relocatable (info) && merged)
12824
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12825
12826
  /* Figure out the file positions for everything but the symbol table
12827
     and the relocs.  We set symcount to force assign_section_numbers
12828
     to create a symbol table.  */
12829
0
  abfd->symcount = info->strip != strip_all || emit_relocs;
12830
0
  BFD_ASSERT (! abfd->output_has_begun);
12831
0
  if (! _bfd_elf_compute_section_file_positions (abfd, info))
12832
0
    goto error_return;
12833
12834
  /* Set sizes, and assign file positions for reloc sections.  */
12835
0
  for (o = abfd->sections; o != NULL; o = o->next)
12836
0
    {
12837
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12838
0
      if ((o->flags & SEC_RELOC) != 0)
12839
0
  {
12840
0
    if (esdo->rel.hdr
12841
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12842
0
      goto error_return;
12843
12844
0
    if (esdo->rela.hdr
12845
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12846
0
      goto error_return;
12847
0
  }
12848
12849
      /* _bfd_elf_compute_section_file_positions makes temporary use
12850
   of target_index.  Reset it.  */
12851
0
      o->target_index = 0;
12852
12853
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12854
   to count upwards while actually outputting the relocations.  */
12855
0
      esdo->rel.count = 0;
12856
0
      esdo->rela.count = 0;
12857
12858
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12859
0
    && !bfd_section_is_ctf (o))
12860
0
  {
12861
    /* Cache the section contents so that they can be compressed
12862
       later.  Use bfd_malloc since it will be freed by
12863
       bfd_compress_section_contents.  */
12864
0
    unsigned char *contents = esdo->this_hdr.contents;
12865
0
    if (contents != NULL)
12866
0
      abort ();
12867
0
    contents
12868
0
      = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12869
0
    if (contents == NULL)
12870
0
      goto error_return;
12871
0
    esdo->this_hdr.contents = contents;
12872
0
  }
12873
0
    }
12874
12875
  /* We have now assigned file positions for all the sections except .symtab,
12876
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
12877
     the .symtab section at the current file position, and write directly to it.
12878
     We build the .strtab section in memory.  */
12879
0
  abfd->symcount = 0;
12880
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12881
  /* sh_name is set in prep_headers.  */
12882
0
  symtab_hdr->sh_type = SHT_SYMTAB;
12883
  /* sh_flags, sh_addr and sh_size all start off zero.  */
12884
0
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12885
  /* sh_link is set in assign_section_numbers.  */
12886
  /* sh_info is set below.  */
12887
  /* sh_offset is set just below.  */
12888
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12889
12890
0
  if (max_sym_count < 20)
12891
0
    max_sym_count = 20;
12892
0
  htab->strtabsize = max_sym_count;
12893
0
  amt = max_sym_count * sizeof (struct elf_sym_strtab);
12894
0
  htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12895
0
  if (htab->strtab == NULL)
12896
0
    goto error_return;
12897
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
12898
0
  flinfo.symshndxbuf
12899
0
    = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12900
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
12901
12902
0
  if (info->strip != strip_all || emit_relocs)
12903
0
    {
12904
0
      file_ptr off = elf_next_file_pos (abfd);
12905
12906
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12907
12908
      /* Note that at this point elf_next_file_pos (abfd) is
12909
   incorrect.  We do not yet know the size of the .symtab section.
12910
   We correct next_file_pos below, after we do know the size.  */
12911
12912
      /* Start writing out the symbol table.  The first symbol is always a
12913
   dummy symbol.  */
12914
0
      elfsym.st_value = 0;
12915
0
      elfsym.st_size = 0;
12916
0
      elfsym.st_info = 0;
12917
0
      elfsym.st_other = 0;
12918
0
      elfsym.st_shndx = SHN_UNDEF;
12919
0
      elfsym.st_target_internal = 0;
12920
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12921
0
             bfd_und_section_ptr, NULL) != 1)
12922
0
  goto error_return;
12923
12924
      /* Output a symbol for each section if asked or they are used for
12925
   relocs.  These symbols usually have no names.  We store the
12926
   index of each one in the index field of the section, so that
12927
   we can find it again when outputting relocs.  */
12928
12929
0
      if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12930
0
  {
12931
0
    bool name_local_sections
12932
0
      = (bed->elf_backend_name_local_section_symbols
12933
0
         && bed->elf_backend_name_local_section_symbols (abfd));
12934
0
    const char *name = NULL;
12935
12936
0
    elfsym.st_size = 0;
12937
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12938
0
    elfsym.st_other = 0;
12939
0
    elfsym.st_value = 0;
12940
0
    elfsym.st_target_internal = 0;
12941
0
    for (i = 1; i < elf_numsections (abfd); i++)
12942
0
      {
12943
0
        o = bfd_section_from_elf_index (abfd, i);
12944
0
        if (o != NULL)
12945
0
    {
12946
0
      o->target_index = bfd_get_symcount (abfd);
12947
0
      elfsym.st_shndx = i;
12948
0
      if (!bfd_link_relocatable (info))
12949
0
        elfsym.st_value = o->vma;
12950
0
      if (name_local_sections)
12951
0
        name = o->name;
12952
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12953
0
             NULL) != 1)
12954
0
        goto error_return;
12955
0
    }
12956
0
      }
12957
0
  }
12958
0
    }
12959
12960
  /* On some targets like Irix 5 the symbol split between local and global
12961
     ones recorded in the sh_info field needs to be done between section
12962
     and all other symbols.  */
12963
0
  if (bed->elf_backend_elfsym_local_is_section
12964
0
      && bed->elf_backend_elfsym_local_is_section (abfd))
12965
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
12966
12967
  /* Allocate some memory to hold information read in from the input
12968
     files.  */
12969
0
  if (max_contents_size != 0)
12970
0
    {
12971
0
      flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12972
0
      if (flinfo.contents == NULL)
12973
0
  goto error_return;
12974
0
    }
12975
12976
0
  if (max_external_reloc_size != 0)
12977
0
    {
12978
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12979
0
      if (flinfo.external_relocs == NULL)
12980
0
  goto error_return;
12981
0
    }
12982
12983
0
  if (max_internal_reloc_count != 0)
12984
0
    {
12985
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12986
0
      flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12987
0
      if (flinfo.internal_relocs == NULL)
12988
0
  goto error_return;
12989
0
    }
12990
12991
0
  if (max_sym_count != 0)
12992
0
    {
12993
0
      amt = max_sym_count * bed->s->sizeof_sym;
12994
0
      flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12995
0
      if (flinfo.external_syms == NULL)
12996
0
  goto error_return;
12997
12998
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
12999
0
      flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13000
0
      if (flinfo.internal_syms == NULL)
13001
0
  goto error_return;
13002
13003
0
      amt = max_sym_count * sizeof (long);
13004
0
      flinfo.indices = (long int *) bfd_malloc (amt);
13005
0
      if (flinfo.indices == NULL)
13006
0
  goto error_return;
13007
13008
0
      amt = max_sym_count * sizeof (asection *);
13009
0
      flinfo.sections = (asection **) bfd_malloc (amt);
13010
0
      if (flinfo.sections == NULL)
13011
0
  goto error_return;
13012
0
    }
13013
13014
0
  if (max_sym_shndx_count != 0)
13015
0
    {
13016
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13017
0
      flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13018
0
      if (flinfo.locsym_shndx == NULL)
13019
0
  goto error_return;
13020
0
    }
13021
13022
0
  if (htab->tls_sec)
13023
0
    {
13024
0
      bfd_vma base, end = 0;  /* Both bytes.  */
13025
0
      asection *sec;
13026
13027
0
      for (sec = htab->tls_sec;
13028
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
13029
0
     sec = sec->next)
13030
0
  {
13031
0
    bfd_size_type size = sec->size;
13032
0
    unsigned int opb = bfd_octets_per_byte (abfd, sec);
13033
13034
0
    if (size == 0
13035
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
13036
0
      {
13037
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
13038
13039
0
        if (ord != NULL)
13040
0
    size = ord->offset * opb + ord->size;
13041
0
      }
13042
0
    end = sec->vma + size / opb;
13043
0
  }
13044
0
      base = htab->tls_sec->vma;
13045
      /* Only align end of TLS section if static TLS doesn't have special
13046
   alignment requirements.  */
13047
0
      if (bed->static_tls_alignment == 1)
13048
0
  end = align_power (end, htab->tls_sec->alignment_power);
13049
0
      htab->tls_size = end - base;
13050
0
    }
13051
13052
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
13053
0
    return false;
13054
13055
  /* Finish relative relocations here after regular symbol processing
13056
     is finished if DT_RELR is enabled.  */
13057
0
  if (info->enable_dt_relr
13058
0
      && bed->finish_relative_relocs
13059
0
      && !bed->finish_relative_relocs (info))
13060
0
    info->callbacks->einfo
13061
0
      (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
13062
13063
  /* Since ELF permits relocations to be against local symbols, we
13064
     must have the local symbols available when we do the relocations.
13065
     Since we would rather only read the local symbols once, and we
13066
     would rather not keep them in memory, we handle all the
13067
     relocations for a single input file at the same time.
13068
13069
     Unfortunately, there is no way to know the total number of local
13070
     symbols until we have seen all of them, and the local symbol
13071
     indices precede the global symbol indices.  This means that when
13072
     we are generating relocatable output, and we see a reloc against
13073
     a global symbol, we can not know the symbol index until we have
13074
     finished examining all the local symbols to see which ones we are
13075
     going to output.  To deal with this, we keep the relocations in
13076
     memory, and don't output them until the end of the link.  This is
13077
     an unfortunate waste of memory, but I don't see a good way around
13078
     it.  Fortunately, it only happens when performing a relocatable
13079
     link, which is not the common case.  FIXME: If keep_memory is set
13080
     we could write the relocs out and then read them again; I don't
13081
     know how bad the memory loss will be.  */
13082
13083
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13084
0
    sub->output_has_begun = false;
13085
0
  for (o = abfd->sections; o != NULL; o = o->next)
13086
0
    {
13087
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
13088
0
  {
13089
0
    if (p->type == bfd_indirect_link_order
13090
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13091
0
      == bfd_target_elf_flavour)
13092
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13093
0
      {
13094
0
        if (! sub->output_has_begun)
13095
0
    {
13096
0
      if (! elf_link_input_bfd (&flinfo, sub))
13097
0
        goto error_return;
13098
0
      sub->output_has_begun = true;
13099
0
    }
13100
0
      }
13101
0
    else if (p->type == bfd_section_reloc_link_order
13102
0
       || p->type == bfd_symbol_reloc_link_order)
13103
0
      {
13104
0
        if (! elf_reloc_link_order (abfd, info, o, p))
13105
0
    goto error_return;
13106
0
      }
13107
0
    else
13108
0
      {
13109
0
        if (! _bfd_default_link_order (abfd, info, o, p))
13110
0
    {
13111
0
      if (p->type == bfd_indirect_link_order
13112
0
          && (bfd_get_flavour (sub)
13113
0
        == bfd_target_elf_flavour)
13114
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
13115
0
        != bed->s->elfclass))
13116
0
        {
13117
0
          const char *iclass, *oclass;
13118
13119
0
          switch (bed->s->elfclass)
13120
0
      {
13121
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
13122
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
13123
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13124
0
      default: abort ();
13125
0
      }
13126
13127
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13128
0
      {
13129
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
13130
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
13131
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13132
0
      default: abort ();
13133
0
      }
13134
13135
0
          bfd_set_error (bfd_error_wrong_format);
13136
0
          _bfd_error_handler
13137
      /* xgettext:c-format */
13138
0
      (_("%pB: file class %s incompatible with %s"),
13139
0
       sub, iclass, oclass);
13140
0
        }
13141
13142
0
      goto error_return;
13143
0
    }
13144
0
      }
13145
0
  }
13146
0
    }
13147
13148
  /* Free symbol buffer if needed.  */
13149
0
  if (!info->reduce_memory_overheads)
13150
0
    {
13151
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13152
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13153
0
    {
13154
0
      free (elf_tdata (sub)->symbuf);
13155
0
      elf_tdata (sub)->symbuf = NULL;
13156
0
    }
13157
0
    }
13158
13159
0
  ret = true;
13160
13161
  /* Output any global symbols that got converted to local in a
13162
     version script or due to symbol visibility.  We do this in a
13163
     separate step since ELF requires all local symbols to appear
13164
     prior to any global symbols.  FIXME: We should only do this if
13165
     some global symbols were, in fact, converted to become local.
13166
     FIXME: Will this work correctly with the Irix 5 linker?  */
13167
0
  eoinfo.failed = false;
13168
0
  eoinfo.flinfo = &flinfo;
13169
0
  eoinfo.localsyms = true;
13170
0
  eoinfo.file_sym_done = false;
13171
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13172
0
  if (eoinfo.failed)
13173
0
    {
13174
0
      ret = false;
13175
0
      goto return_local_hash_table;
13176
0
    }
13177
13178
  /* If backend needs to output some local symbols not present in the hash
13179
     table, do it now.  */
13180
0
  if (bed->elf_backend_output_arch_local_syms)
13181
0
    {
13182
0
      if (! ((*bed->elf_backend_output_arch_local_syms)
13183
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13184
0
  {
13185
0
    ret = false;
13186
0
    goto return_local_hash_table;
13187
0
  }
13188
0
    }
13189
13190
  /* That wrote out all the local symbols.  Finish up the symbol table
13191
     with the global symbols. Even if we want to strip everything we
13192
     can, we still need to deal with those global symbols that got
13193
     converted to local in a version script.  */
13194
13195
  /* The sh_info field records the index of the first non local symbol.  */
13196
0
  if (!symtab_hdr->sh_info)
13197
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
13198
13199
0
  if (dynamic
13200
0
      && htab->dynsym != NULL
13201
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
13202
0
    {
13203
0
      Elf_Internal_Sym sym;
13204
0
      bfd_byte *dynsym = htab->dynsym->contents;
13205
13206
0
      o = htab->dynsym->output_section;
13207
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13208
13209
      /* Write out the section symbols for the output sections.  */
13210
0
      if (bfd_link_pic (info)
13211
0
    || htab->is_relocatable_executable)
13212
0
  {
13213
0
    asection *s;
13214
13215
0
    sym.st_size = 0;
13216
0
    sym.st_name = 0;
13217
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13218
0
    sym.st_other = 0;
13219
0
    sym.st_target_internal = 0;
13220
13221
0
    for (s = abfd->sections; s != NULL; s = s->next)
13222
0
      {
13223
0
        int indx;
13224
0
        bfd_byte *dest;
13225
0
        long dynindx;
13226
13227
0
        dynindx = elf_section_data (s)->dynindx;
13228
0
        if (dynindx <= 0)
13229
0
    continue;
13230
0
        indx = elf_section_data (s)->this_idx;
13231
0
        BFD_ASSERT (indx > 0);
13232
0
        sym.st_shndx = indx;
13233
0
        if (! check_dynsym (abfd, &sym))
13234
0
    {
13235
0
      ret = false;
13236
0
      goto return_local_hash_table;
13237
0
    }
13238
0
        sym.st_value = s->vma;
13239
0
        dest = dynsym + dynindx * bed->s->sizeof_sym;
13240
13241
        /* Inform the linker of the addition of this symbol.  */
13242
13243
0
        if (info->callbacks->ctf_new_dynsym)
13244
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
13245
13246
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13247
0
      }
13248
0
  }
13249
13250
      /* Write out the local dynsyms.  */
13251
0
      if (htab->dynlocal)
13252
0
  {
13253
0
    struct elf_link_local_dynamic_entry *e;
13254
0
    for (e = htab->dynlocal; e ; e = e->next)
13255
0
      {
13256
0
        asection *s;
13257
0
        bfd_byte *dest;
13258
13259
        /* Copy the internal symbol and turn off visibility.
13260
     Note that we saved a word of storage and overwrote
13261
     the original st_name with the dynstr_index.  */
13262
0
        sym = e->isym;
13263
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13264
0
        sym.st_shndx = SHN_UNDEF;
13265
13266
0
        s = bfd_section_from_elf_index (e->input_bfd,
13267
0
                e->isym.st_shndx);
13268
0
        if (s != NULL
13269
0
      && s->output_section != NULL
13270
0
      && elf_section_data (s->output_section) != NULL)
13271
0
    {
13272
0
      sym.st_shndx =
13273
0
        elf_section_data (s->output_section)->this_idx;
13274
0
      if (! check_dynsym (abfd, &sym))
13275
0
        {
13276
0
          ret = false;
13277
0
          goto return_local_hash_table;
13278
0
        }
13279
0
      sym.st_value = (s->output_section->vma
13280
0
          + s->output_offset
13281
0
          + e->isym.st_value);
13282
0
    }
13283
13284
        /* Inform the linker of the addition of this symbol.  */
13285
13286
0
        if (info->callbacks->ctf_new_dynsym)
13287
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13288
13289
0
        dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13290
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13291
0
      }
13292
0
  }
13293
0
    }
13294
13295
  /* We get the global symbols from the hash table.  */
13296
0
  eoinfo.failed = false;
13297
0
  eoinfo.localsyms = false;
13298
0
  eoinfo.flinfo = &flinfo;
13299
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13300
0
  if (eoinfo.failed)
13301
0
    {
13302
0
      ret = false;
13303
0
      goto return_local_hash_table;
13304
0
    }
13305
13306
  /* If backend needs to output some symbols not present in the hash
13307
     table, do it now.  */
13308
0
  if (bed->elf_backend_output_arch_syms
13309
0
      && (info->strip != strip_all || emit_relocs))
13310
0
    {
13311
0
      if (! ((*bed->elf_backend_output_arch_syms)
13312
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13313
0
  {
13314
0
    ret = false;
13315
0
    goto return_local_hash_table;
13316
0
  }
13317
0
    }
13318
13319
  /* Finalize the .strtab section.  */
13320
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13321
13322
  /* Swap out the .strtab section. */
13323
0
  if (!elf_link_swap_symbols_out (&flinfo))
13324
0
    {
13325
0
      ret = false;
13326
0
      goto return_local_hash_table;
13327
0
    }
13328
13329
  /* Now we know the size of the symtab section.  */
13330
0
  if (bfd_get_symcount (abfd) > 0)
13331
0
    {
13332
      /* Finish up and write out the symbol string table (.strtab)
13333
   section.  */
13334
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13335
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13336
13337
0
      if (elf_symtab_shndx_list (abfd))
13338
0
  {
13339
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13340
13341
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13342
0
      {
13343
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13344
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13345
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13346
0
        amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13347
0
        symtab_shndx_hdr->sh_size = amt;
13348
13349
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13350
0
                     off, true);
13351
13352
0
        if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13353
0
      || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13354
0
    {
13355
0
      ret = false;
13356
0
      goto return_local_hash_table;
13357
0
    }
13358
0
      }
13359
0
  }
13360
13361
0
      symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13362
      /* sh_name was set in prep_headers.  */
13363
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13364
0
      symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13365
0
      symstrtab_hdr->sh_addr = 0;
13366
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13367
0
      symstrtab_hdr->sh_entsize = 0;
13368
0
      symstrtab_hdr->sh_link = 0;
13369
0
      symstrtab_hdr->sh_info = 0;
13370
      /* sh_offset is set just below.  */
13371
0
      symstrtab_hdr->sh_addralign = 1;
13372
13373
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13374
0
                   off, true);
13375
0
      elf_next_file_pos (abfd) = off;
13376
13377
0
      if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13378
0
    || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13379
0
  {
13380
0
    ret = false;
13381
0
    goto return_local_hash_table;
13382
0
  }
13383
0
    }
13384
13385
0
  if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13386
0
    {
13387
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13388
0
        info->out_implib_bfd);
13389
0
      ret = false;
13390
0
      goto return_local_hash_table;
13391
0
    }
13392
13393
  /* Adjust the relocs to have the correct symbol indices.  */
13394
0
  for (o = abfd->sections; o != NULL; o = o->next)
13395
0
    {
13396
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13397
0
      bool sort;
13398
13399
0
      if ((o->flags & SEC_RELOC) == 0)
13400
0
  continue;
13401
13402
0
      sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13403
0
      if (esdo->rel.hdr != NULL
13404
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13405
0
  {
13406
0
    ret = false;
13407
0
    goto return_local_hash_table;
13408
0
  }
13409
0
      if (esdo->rela.hdr != NULL
13410
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13411
0
  {
13412
0
    ret = false;
13413
0
    goto return_local_hash_table;
13414
0
  }
13415
13416
      /* Set the reloc_count field to 0 to prevent write_relocs from
13417
   trying to swap the relocs out itself.  */
13418
0
      o->reloc_count = 0;
13419
0
    }
13420
13421
0
  relativecount = 0;
13422
0
  if (dynamic && info->combreloc && dynobj != NULL)
13423
0
    relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13424
13425
0
  relr_entsize = 0;
13426
0
  if (htab->srelrdyn != NULL
13427
0
      && htab->srelrdyn->output_section != NULL
13428
0
      && htab->srelrdyn->size != 0)
13429
0
    {
13430
0
      asection *s = htab->srelrdyn->output_section;
13431
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13432
0
      if (relr_entsize == 0)
13433
0
  {
13434
0
    relr_entsize = bed->s->arch_size / 8;
13435
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13436
0
  }
13437
0
    }
13438
13439
  /* If we are linking against a dynamic object, or generating a
13440
     shared library, finish up the dynamic linking information.  */
13441
0
  if (dynamic)
13442
0
    {
13443
0
      bfd_byte *dyncon, *dynconend;
13444
13445
      /* Fix up .dynamic entries.  */
13446
0
      o = htab->dynamic;
13447
0
      BFD_ASSERT (o != NULL);
13448
13449
0
      dyncon = o->contents;
13450
0
      dynconend = PTR_ADD (o->contents, o->size);
13451
0
      for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13452
0
  {
13453
0
    Elf_Internal_Dyn dyn;
13454
0
    const char *name;
13455
0
    unsigned int type;
13456
0
    bfd_size_type sh_size;
13457
0
    bfd_vma sh_addr;
13458
13459
0
    bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13460
13461
0
    switch (dyn.d_tag)
13462
0
      {
13463
0
      default:
13464
0
        continue;
13465
0
      case DT_NULL:
13466
0
        if (relativecount != 0)
13467
0
    {
13468
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13469
0
        {
13470
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13471
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13472
0
        }
13473
0
      if (dyn.d_tag != DT_NULL
13474
0
          && dynconend - dyncon >= bed->s->sizeof_dyn)
13475
0
        {
13476
0
          dyn.d_un.d_val = relativecount;
13477
0
          relativecount = 0;
13478
0
          break;
13479
0
        }
13480
0
      relativecount = 0;
13481
0
    }
13482
0
        if (relr_entsize != 0)
13483
0
    {
13484
0
      if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13485
0
        {
13486
0
          asection *s = htab->srelrdyn;
13487
0
          dyn.d_tag = DT_RELR;
13488
0
          dyn.d_un.d_ptr
13489
0
      = s->output_section->vma + s->output_offset;
13490
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13491
0
          dyncon += bed->s->sizeof_dyn;
13492
13493
0
          dyn.d_tag = DT_RELRSZ;
13494
0
          dyn.d_un.d_val = s->size;
13495
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13496
0
          dyncon += bed->s->sizeof_dyn;
13497
13498
0
          dyn.d_tag = DT_RELRENT;
13499
0
          dyn.d_un.d_val = relr_entsize;
13500
0
          relr_entsize = 0;
13501
0
          break;
13502
0
        }
13503
0
      relr_entsize = 0;
13504
0
    }
13505
0
        continue;
13506
13507
0
      case DT_INIT:
13508
0
        name = info->init_function;
13509
0
        goto get_sym;
13510
0
      case DT_FINI:
13511
0
        name = info->fini_function;
13512
0
      get_sym:
13513
0
        {
13514
0
    struct elf_link_hash_entry *h;
13515
13516
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13517
0
    if (h != NULL
13518
0
        && (h->root.type == bfd_link_hash_defined
13519
0
      || h->root.type == bfd_link_hash_defweak))
13520
0
      {
13521
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13522
0
        o = h->root.u.def.section;
13523
0
        if (o->output_section != NULL)
13524
0
          dyn.d_un.d_ptr += (o->output_section->vma
13525
0
           + o->output_offset);
13526
0
        else
13527
0
          {
13528
      /* The symbol is imported from another shared
13529
         library and does not apply to this one.  */
13530
0
      dyn.d_un.d_ptr = 0;
13531
0
          }
13532
0
        break;
13533
0
      }
13534
0
        }
13535
0
        continue;
13536
13537
0
      case DT_PREINIT_ARRAYSZ:
13538
0
        name = ".preinit_array";
13539
0
        goto get_out_size;
13540
0
      case DT_INIT_ARRAYSZ:
13541
0
        name = ".init_array";
13542
0
        goto get_out_size;
13543
0
      case DT_FINI_ARRAYSZ:
13544
0
        name = ".fini_array";
13545
0
      get_out_size:
13546
0
        o = bfd_get_section_by_name (abfd, name);
13547
0
        if (o == NULL)
13548
0
    {
13549
0
      _bfd_error_handler
13550
0
        (_("could not find section %s"), name);
13551
0
      goto error_return;
13552
0
    }
13553
0
        if (o->size == 0)
13554
0
    _bfd_error_handler
13555
0
      (_("warning: %s section has zero size"), name);
13556
0
        dyn.d_un.d_val = o->size;
13557
0
        break;
13558
13559
0
      case DT_PREINIT_ARRAY:
13560
0
        name = ".preinit_array";
13561
0
        goto get_out_vma;
13562
0
      case DT_INIT_ARRAY:
13563
0
        name = ".init_array";
13564
0
        goto get_out_vma;
13565
0
      case DT_FINI_ARRAY:
13566
0
        name = ".fini_array";
13567
0
      get_out_vma:
13568
0
        o = bfd_get_section_by_name (abfd, name);
13569
0
        goto do_vma;
13570
13571
0
      case DT_HASH:
13572
0
        name = ".hash";
13573
0
        goto get_vma;
13574
0
      case DT_GNU_HASH:
13575
0
        name = ".gnu.hash";
13576
0
        goto get_vma;
13577
0
      case DT_STRTAB:
13578
0
        name = ".dynstr";
13579
0
        goto get_vma;
13580
0
      case DT_SYMTAB:
13581
0
        name = ".dynsym";
13582
0
        goto get_vma;
13583
0
      case DT_VERDEF:
13584
0
        name = ".gnu.version_d";
13585
0
        goto get_vma;
13586
0
      case DT_VERNEED:
13587
0
        name = ".gnu.version_r";
13588
0
        goto get_vma;
13589
0
      case DT_VERSYM:
13590
0
        name = ".gnu.version";
13591
0
      get_vma:
13592
0
        o = bfd_get_linker_section (dynobj, name);
13593
0
      do_vma:
13594
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13595
0
    {
13596
0
      _bfd_error_handler
13597
0
        (_("could not find section %s"), name);
13598
0
      goto error_return;
13599
0
    }
13600
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13601
0
    {
13602
0
      _bfd_error_handler
13603
0
        (_("warning: section '%s' is being made into a note"), name);
13604
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13605
0
      goto error_return;
13606
0
    }
13607
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13608
0
        break;
13609
13610
0
      case DT_REL:
13611
0
      case DT_RELA:
13612
0
      case DT_RELSZ:
13613
0
      case DT_RELASZ:
13614
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13615
0
    type = SHT_REL;
13616
0
        else
13617
0
    type = SHT_RELA;
13618
0
        sh_size = 0;
13619
0
        sh_addr = 0;
13620
0
        for (i = 1; i < elf_numsections (abfd); i++)
13621
0
    {
13622
0
      Elf_Internal_Shdr *hdr;
13623
13624
0
      hdr = elf_elfsections (abfd)[i];
13625
0
      if (hdr->sh_type == type
13626
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13627
0
        {
13628
0
          sh_size += hdr->sh_size;
13629
0
          if (sh_addr == 0
13630
0
        || sh_addr > hdr->sh_addr)
13631
0
      sh_addr = hdr->sh_addr;
13632
0
        }
13633
0
    }
13634
13635
0
        if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13636
0
    {
13637
0
      unsigned int opb = bfd_octets_per_byte (abfd, o);
13638
13639
      /* Don't count procedure linkage table relocs in the
13640
         overall reloc count.  */
13641
0
      sh_size -= htab->srelplt->size;
13642
0
      if (sh_size == 0)
13643
        /* If the size is zero, make the address zero too.
13644
           This is to avoid a glibc bug.  If the backend
13645
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13646
           zero, then we'll put DT_RELA at the end of
13647
           DT_JMPREL.  glibc will interpret the end of
13648
           DT_RELA matching the end of DT_JMPREL as the
13649
           case where DT_RELA includes DT_JMPREL, and for
13650
           LD_BIND_NOW will decide that processing DT_RELA
13651
           will process the PLT relocs too.  Net result:
13652
           No PLT relocs applied.  */
13653
0
        sh_addr = 0;
13654
13655
      /* If .rela.plt is the first .rela section, exclude
13656
         it from DT_RELA.  */
13657
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13658
0
               + htab->srelplt->output_offset) * opb)
13659
0
        sh_addr += htab->srelplt->size;
13660
0
    }
13661
13662
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13663
0
    dyn.d_un.d_val = sh_size;
13664
0
        else
13665
0
    dyn.d_un.d_ptr = sh_addr;
13666
0
        break;
13667
0
      }
13668
0
    bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13669
0
  }
13670
0
    }
13671
13672
  /* If we have created any dynamic sections, then output them.  */
13673
0
  if (dynobj != NULL)
13674
0
    {
13675
0
      if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13676
0
  goto error_return;
13677
13678
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13679
0
      if (bfd_link_textrel_check (info)
13680
0
    && (o = htab->dynamic) != NULL
13681
0
    && o->size != 0)
13682
0
  {
13683
0
    bfd_byte *dyncon, *dynconend;
13684
13685
0
    dyncon = o->contents;
13686
0
    dynconend = o->contents + o->size;
13687
0
    for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13688
0
      {
13689
0
        Elf_Internal_Dyn dyn;
13690
13691
0
        bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13692
13693
0
        if (dyn.d_tag == DT_TEXTREL)
13694
0
    {
13695
0
      if (info->textrel_check == textrel_check_error)
13696
0
        info->callbacks->einfo
13697
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13698
0
      else if (bfd_link_dll (info))
13699
0
        info->callbacks->einfo
13700
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13701
0
      else if (bfd_link_pde (info))
13702
0
        info->callbacks->einfo
13703
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13704
0
      else
13705
0
        info->callbacks->einfo
13706
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13707
0
      break;
13708
0
    }
13709
0
      }
13710
0
  }
13711
13712
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13713
0
  {
13714
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13715
0
        || o->size == 0
13716
0
        || o->output_section == bfd_abs_section_ptr)
13717
0
      continue;
13718
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13719
0
      {
13720
        /* At this point, we are only interested in sections
13721
     created by _bfd_elf_link_create_dynamic_sections.  */
13722
0
        continue;
13723
0
      }
13724
0
    if (htab->stab_info.stabstr == o)
13725
0
      continue;
13726
0
    if (htab->eh_info.hdr_sec == o)
13727
0
      continue;
13728
0
    if (strcmp (o->name, ".dynstr") != 0)
13729
0
      {
13730
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13731
0
              * bfd_octets_per_byte (abfd, o));
13732
0
        if (!bfd_set_section_contents (abfd, o->output_section,
13733
0
               o->contents, octets, o->size))
13734
0
    goto error_return;
13735
0
      }
13736
0
    else
13737
0
      {
13738
        /* The contents of the .dynstr section are actually in a
13739
     stringtab.  */
13740
0
        file_ptr off;
13741
13742
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13743
0
        if (bfd_seek (abfd, off, SEEK_SET) != 0
13744
0
      || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13745
0
    goto error_return;
13746
0
      }
13747
0
  }
13748
0
    }
13749
13750
0
  if (!info->resolve_section_groups)
13751
0
    {
13752
0
      bool failed = false;
13753
13754
0
      BFD_ASSERT (bfd_link_relocatable (info));
13755
0
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13756
0
      if (failed)
13757
0
  goto error_return;
13758
0
    }
13759
13760
  /* If we have optimized stabs strings, output them.  */
13761
0
  if (htab->stab_info.stabstr != NULL)
13762
0
    {
13763
0
      if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13764
0
  goto error_return;
13765
0
    }
13766
13767
0
  if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13768
0
    goto error_return;
13769
13770
0
  if (! _bfd_elf_write_section_sframe (abfd, info))
13771
0
    goto error_return;
13772
13773
0
  if (info->callbacks->emit_ctf)
13774
0
      info->callbacks->emit_ctf ();
13775
13776
0
  elf_final_link_free (abfd, &flinfo);
13777
13778
0
  if (attr_section)
13779
0
    {
13780
0
      bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13781
0
      if (contents == NULL)
13782
0
  {
13783
    /* Bail out and fail.  */
13784
0
    ret = false;
13785
0
    goto return_local_hash_table;
13786
0
  }
13787
0
      bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13788
0
      bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13789
0
      free (contents);
13790
0
    }
13791
13792
0
 return_local_hash_table:
13793
0
  if (info->unique_symbol)
13794
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13795
0
  return ret;
13796
13797
0
 error_return:
13798
0
  elf_final_link_free (abfd, &flinfo);
13799
0
  ret = false;
13800
0
  goto return_local_hash_table;
13801
0
}
13802

13803
/* Initialize COOKIE for input bfd ABFD.  */
13804
13805
static bool
13806
init_reloc_cookie (struct elf_reloc_cookie *cookie,
13807
       struct bfd_link_info *info, bfd *abfd,
13808
       bool keep_memory)
13809
0
{
13810
0
  Elf_Internal_Shdr *symtab_hdr;
13811
0
  const struct elf_backend_data *bed;
13812
13813
0
  bed = get_elf_backend_data (abfd);
13814
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13815
13816
0
  cookie->abfd = abfd;
13817
0
  cookie->sym_hashes = elf_sym_hashes (abfd);
13818
0
  cookie->bad_symtab = elf_bad_symtab (abfd);
13819
0
  if (cookie->bad_symtab)
13820
0
    {
13821
0
      cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13822
0
      cookie->extsymoff = 0;
13823
0
    }
13824
0
  else
13825
0
    {
13826
0
      cookie->locsymcount = symtab_hdr->sh_info;
13827
0
      cookie->extsymoff = symtab_hdr->sh_info;
13828
0
    }
13829
13830
0
  if (bed->s->arch_size == 32)
13831
0
    cookie->r_sym_shift = 8;
13832
0
  else
13833
0
    cookie->r_sym_shift = 32;
13834
13835
0
  cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13836
0
  if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13837
0
    {
13838
0
      cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13839
0
                cookie->locsymcount, 0,
13840
0
                NULL, NULL, NULL);
13841
0
      if (cookie->locsyms == NULL)
13842
0
  {
13843
0
    info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13844
0
    return false;
13845
0
  }
13846
0
      if (keep_memory || _bfd_elf_link_keep_memory (info))
13847
0
  {
13848
0
    symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13849
0
    info->cache_size += (cookie->locsymcount
13850
0
             * sizeof (Elf_External_Sym_Shndx));
13851
0
  }
13852
0
    }
13853
0
  return true;
13854
0
}
13855
13856
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13857
13858
static void
13859
fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13860
0
{
13861
0
  Elf_Internal_Shdr *symtab_hdr;
13862
13863
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13864
0
  if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13865
0
    free (cookie->locsyms);
13866
0
}
13867
13868
/* Initialize the relocation information in COOKIE for input section SEC
13869
   of input bfd ABFD.  */
13870
13871
static bool
13872
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13873
      struct bfd_link_info *info, bfd *abfd,
13874
      asection *sec, bool keep_memory)
13875
0
{
13876
0
  if (sec->reloc_count == 0)
13877
0
    {
13878
0
      cookie->rels = NULL;
13879
0
      cookie->relend = NULL;
13880
0
    }
13881
0
  else
13882
0
    {
13883
0
      cookie->rels = _bfd_elf_link_info_read_relocs
13884
0
  (abfd, info, sec, NULL, NULL,
13885
0
   keep_memory || _bfd_elf_link_keep_memory (info));
13886
0
      if (cookie->rels == NULL)
13887
0
  return false;
13888
0
      cookie->rel = cookie->rels;
13889
0
      cookie->relend = cookie->rels + sec->reloc_count;
13890
0
    }
13891
0
  cookie->rel = cookie->rels;
13892
0
  return true;
13893
0
}
13894
13895
/* Free the memory allocated by init_reloc_cookie_rels,
13896
   if appropriate.  */
13897
13898
static void
13899
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13900
      asection *sec)
13901
0
{
13902
0
  if (elf_section_data (sec)->relocs != cookie->rels)
13903
0
    free (cookie->rels);
13904
0
}
13905
13906
/* Initialize the whole of COOKIE for input section SEC.  */
13907
13908
static bool
13909
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13910
             struct bfd_link_info *info,
13911
             asection *sec, bool keep_memory)
13912
0
{
13913
0
  if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
13914
0
    goto error1;
13915
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13916
0
             keep_memory))
13917
0
    goto error2;
13918
0
  return true;
13919
13920
0
 error2:
13921
0
  fini_reloc_cookie (cookie, sec->owner);
13922
0
 error1:
13923
0
  return false;
13924
0
}
13925
13926
/* Free the memory allocated by init_reloc_cookie_for_section,
13927
   if appropriate.  */
13928
13929
static void
13930
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13931
             asection *sec)
13932
0
{
13933
0
  fini_reloc_cookie_rels (cookie, sec);
13934
0
  fini_reloc_cookie (cookie, sec->owner);
13935
0
}
13936

13937
/* Garbage collect unused sections.  */
13938
13939
/* Default gc_mark_hook.  */
13940
13941
asection *
13942
_bfd_elf_gc_mark_hook (asection *sec,
13943
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
13944
           Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13945
           struct elf_link_hash_entry *h,
13946
           Elf_Internal_Sym *sym)
13947
0
{
13948
0
  if (h != NULL)
13949
0
    {
13950
0
      switch (h->root.type)
13951
0
  {
13952
0
  case bfd_link_hash_defined:
13953
0
  case bfd_link_hash_defweak:
13954
0
    return h->root.u.def.section;
13955
13956
0
  case bfd_link_hash_common:
13957
0
    return h->root.u.c.p->section;
13958
13959
0
  default:
13960
0
    break;
13961
0
  }
13962
0
    }
13963
0
  else
13964
0
    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13965
13966
0
  return NULL;
13967
0
}
13968
13969
/* Return the debug definition section.  */
13970
13971
static asection *
13972
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13973
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
13974
         Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13975
         struct elf_link_hash_entry *h,
13976
         Elf_Internal_Sym *sym)
13977
0
{
13978
0
  if (h != NULL)
13979
0
    {
13980
      /* Return the global debug definition section.  */
13981
0
      if ((h->root.type == bfd_link_hash_defined
13982
0
     || h->root.type == bfd_link_hash_defweak)
13983
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13984
0
  return h->root.u.def.section;
13985
0
    }
13986
0
  else
13987
0
    {
13988
      /* Return the local debug definition section.  */
13989
0
      asection *isec = bfd_section_from_elf_index (sec->owner,
13990
0
               sym->st_shndx);
13991
0
      if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
13992
0
  return isec;
13993
0
    }
13994
13995
0
  return NULL;
13996
0
}
13997
13998
/* COOKIE->rel describes a relocation against section SEC, which is
13999
   a section we've decided to keep.  Return the section that contains
14000
   the relocation symbol, or NULL if no section contains it.  */
14001
14002
asection *
14003
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14004
           elf_gc_mark_hook_fn gc_mark_hook,
14005
           struct elf_reloc_cookie *cookie,
14006
           bool *start_stop)
14007
0
{
14008
0
  unsigned long r_symndx;
14009
0
  struct elf_link_hash_entry *h, *hw;
14010
14011
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14012
0
  if (r_symndx == STN_UNDEF)
14013
0
    return NULL;
14014
14015
0
  if (r_symndx >= cookie->locsymcount
14016
0
      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14017
0
    {
14018
0
      bool was_marked;
14019
14020
0
      h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
14021
0
      if (h == NULL)
14022
0
  {
14023
0
    info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
14024
0
          sec->owner);
14025
0
    return NULL;
14026
0
  }
14027
0
      while (h->root.type == bfd_link_hash_indirect
14028
0
       || h->root.type == bfd_link_hash_warning)
14029
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
14030
14031
0
      was_marked = h->mark;
14032
0
      h->mark = 1;
14033
      /* Keep all aliases of the symbol too.  If an object symbol
14034
   needs to be copied into .dynbss then all of its aliases
14035
   should be present as dynamic symbols, not just the one used
14036
   on the copy relocation.  */
14037
0
      hw = h;
14038
0
      while (hw->is_weakalias)
14039
0
  {
14040
0
    hw = hw->u.alias;
14041
0
    hw->mark = 1;
14042
0
  }
14043
14044
0
      if (!was_marked && h->start_stop && !h->root.ldscript_def)
14045
0
  {
14046
0
    if (info->start_stop_gc)
14047
0
      return NULL;
14048
14049
    /* To work around a glibc bug, mark XXX input sections
14050
       when there is a reference to __start_XXX or __stop_XXX
14051
       symbols.  */
14052
0
    else if (start_stop != NULL)
14053
0
      {
14054
0
        asection *s = h->u2.start_stop_section;
14055
0
        *start_stop = true;
14056
0
        return s;
14057
0
      }
14058
0
  }
14059
14060
0
      return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
14061
0
    }
14062
14063
0
  return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
14064
0
        &cookie->locsyms[r_symndx]);
14065
0
}
14066
14067
/* COOKIE->rel describes a relocation against section SEC, which is
14068
   a section we've decided to keep.  Mark the section that contains
14069
   the relocation symbol.  */
14070
14071
bool
14072
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14073
      asection *sec,
14074
      elf_gc_mark_hook_fn gc_mark_hook,
14075
      struct elf_reloc_cookie *cookie)
14076
0
{
14077
0
  asection *rsec;
14078
0
  bool start_stop = false;
14079
14080
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14081
0
  while (rsec != NULL)
14082
0
    {
14083
0
      if (!rsec->gc_mark)
14084
0
  {
14085
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14086
0
        || (rsec->owner->flags & DYNAMIC) != 0)
14087
0
      rsec->gc_mark = 1;
14088
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14089
0
      return false;
14090
0
  }
14091
0
      if (!start_stop)
14092
0
  break;
14093
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14094
0
    }
14095
0
  return true;
14096
0
}
14097
14098
/* The mark phase of garbage collection.  For a given section, mark
14099
   it and any sections in this section's group, and all the sections
14100
   which define symbols to which it refers.  */
14101
14102
bool
14103
_bfd_elf_gc_mark (struct bfd_link_info *info,
14104
      asection *sec,
14105
      elf_gc_mark_hook_fn gc_mark_hook)
14106
0
{
14107
0
  bool ret;
14108
0
  asection *group_sec, *eh_frame;
14109
14110
0
  sec->gc_mark = 1;
14111
14112
  /* Mark all the sections in the group.  */
14113
0
  group_sec = elf_section_data (sec)->next_in_group;
14114
0
  if (group_sec && !group_sec->gc_mark)
14115
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14116
0
      return false;
14117
14118
  /* Look through the section relocs.  */
14119
0
  ret = true;
14120
0
  eh_frame = elf_eh_frame_section (sec->owner);
14121
0
  if ((sec->flags & SEC_RELOC) != 0
14122
0
      && sec->reloc_count > 0
14123
0
      && sec != eh_frame)
14124
0
    {
14125
0
      struct elf_reloc_cookie cookie;
14126
14127
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14128
0
  ret = false;
14129
0
      else
14130
0
  {
14131
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
14132
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14133
0
        {
14134
0
    ret = false;
14135
0
    break;
14136
0
        }
14137
0
    fini_reloc_cookie_for_section (&cookie, sec);
14138
0
  }
14139
0
    }
14140
14141
0
  if (ret && eh_frame && elf_fde_list (sec))
14142
0
    {
14143
0
      struct elf_reloc_cookie cookie;
14144
14145
      /* NB: When --no-keep-memory is used, the symbol table and
14146
   relocation info for eh_frame are freed after they are retrieved
14147
   for each text section in the input object.  If an input object
14148
   has many text sections, the same data is retrieved and freed
14149
   many times which can take a very long time.  Always keep the
14150
   symbol table and relocation info for eh_frame to avoid it.  */
14151
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14152
0
            true))
14153
0
  ret = false;
14154
0
      else
14155
0
  {
14156
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14157
0
              gc_mark_hook, &cookie))
14158
0
      ret = false;
14159
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
14160
0
  }
14161
0
    }
14162
14163
0
  eh_frame = elf_section_eh_frame_entry (sec);
14164
0
  if (ret && eh_frame && !eh_frame->gc_mark)
14165
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14166
0
      ret = false;
14167
14168
0
  return ret;
14169
0
}
14170
14171
/* Scan and mark sections in a special or debug section group.  */
14172
14173
static void
14174
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14175
0
{
14176
  /* Point to first section of section group.  */
14177
0
  asection *ssec;
14178
  /* Used to iterate the section group.  */
14179
0
  asection *msec;
14180
14181
0
  bool is_special_grp = true;
14182
0
  bool is_debug_grp = true;
14183
14184
  /* First scan to see if group contains any section other than debug
14185
     and special section.  */
14186
0
  ssec = msec = elf_next_in_group (grp);
14187
0
  do
14188
0
    {
14189
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
14190
0
  is_debug_grp = false;
14191
14192
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14193
0
  is_special_grp = false;
14194
14195
0
      msec = elf_next_in_group (msec);
14196
0
    }
14197
0
  while (msec != ssec);
14198
14199
  /* If this is a pure debug section group or pure special section group,
14200
     keep all sections in this group.  */
14201
0
  if (is_debug_grp || is_special_grp)
14202
0
    {
14203
0
      do
14204
0
  {
14205
0
    msec->gc_mark = 1;
14206
0
    msec = elf_next_in_group (msec);
14207
0
  }
14208
0
      while (msec != ssec);
14209
0
    }
14210
0
}
14211
14212
/* Keep debug and special sections.  */
14213
14214
bool
14215
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14216
         elf_gc_mark_hook_fn mark_hook)
14217
0
{
14218
0
  bfd *ibfd;
14219
14220
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14221
0
    {
14222
0
      asection *isec;
14223
0
      bool some_kept;
14224
0
      bool debug_frag_seen;
14225
0
      bool has_kept_debug_info;
14226
14227
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14228
0
  continue;
14229
0
      isec = ibfd->sections;
14230
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14231
0
  continue;
14232
14233
      /* Ensure all linker created sections are kept,
14234
   see if any other section is already marked,
14235
   and note if we have any fragmented debug sections.  */
14236
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
14237
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14238
0
  {
14239
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
14240
0
      isec->gc_mark = 1;
14241
0
    else if (isec->gc_mark
14242
0
       && (isec->flags & SEC_ALLOC) != 0
14243
0
       && elf_section_type (isec) != SHT_NOTE)
14244
0
      some_kept = true;
14245
0
    else
14246
0
      {
14247
        /* Since all sections, except for backend specific ones,
14248
     have been garbage collected, call mark_hook on this
14249
     section if any of its linked-to sections is marked.  */
14250
0
        asection *linked_to_sec;
14251
0
        for (linked_to_sec = elf_linked_to_section (isec);
14252
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
14253
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14254
0
    {
14255
0
      if (linked_to_sec->gc_mark)
14256
0
        {
14257
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14258
0
      return false;
14259
0
          break;
14260
0
        }
14261
0
      linked_to_sec->linker_mark = 1;
14262
0
    }
14263
0
        for (linked_to_sec = elf_linked_to_section (isec);
14264
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
14265
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14266
0
    linked_to_sec->linker_mark = 0;
14267
0
      }
14268
14269
0
    if (!debug_frag_seen
14270
0
        && (isec->flags & SEC_DEBUGGING)
14271
0
        && startswith (isec->name, ".debug_line."))
14272
0
      debug_frag_seen = true;
14273
0
    else if (strcmp (bfd_section_name (isec),
14274
0
         "__patchable_function_entries") == 0
14275
0
       && elf_linked_to_section (isec) == NULL)
14276
0
        info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
14277
0
          "need linked-to section "
14278
0
          "for --gc-sections\n"),
14279
0
              isec->owner, isec);
14280
0
  }
14281
14282
      /* If no non-note alloc section in this file will be kept, then
14283
   we can toss out the debug and special sections.  */
14284
0
      if (!some_kept)
14285
0
  continue;
14286
14287
      /* Keep debug and special sections like .comment when they are
14288
   not part of a group.  Also keep section groups that contain
14289
   just debug sections or special sections.  NB: Sections with
14290
   linked-to section has been handled above.  */
14291
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14292
0
  {
14293
0
    if ((isec->flags & SEC_GROUP) != 0)
14294
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14295
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14296
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14297
0
       && elf_next_in_group (isec) == NULL
14298
0
       && elf_linked_to_section (isec) == NULL)
14299
0
      isec->gc_mark = 1;
14300
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14301
0
      has_kept_debug_info = true;
14302
0
  }
14303
14304
      /* Look for CODE sections which are going to be discarded,
14305
   and find and discard any fragmented debug sections which
14306
   are associated with that code section.  */
14307
0
      if (debug_frag_seen)
14308
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14309
0
    if ((isec->flags & SEC_CODE) != 0
14310
0
        && isec->gc_mark == 0)
14311
0
      {
14312
0
        unsigned int ilen;
14313
0
        asection *dsec;
14314
14315
0
        ilen = strlen (isec->name);
14316
14317
        /* Association is determined by the name of the debug
14318
     section containing the name of the code section as
14319
     a suffix.  For example .debug_line.text.foo is a
14320
     debug section associated with .text.foo.  */
14321
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14322
0
    {
14323
0
      unsigned int dlen;
14324
14325
0
      if (dsec->gc_mark == 0
14326
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14327
0
        continue;
14328
14329
0
      dlen = strlen (dsec->name);
14330
14331
0
      if (dlen > ilen
14332
0
          && strncmp (dsec->name + (dlen - ilen),
14333
0
          isec->name, ilen) == 0)
14334
0
        dsec->gc_mark = 0;
14335
0
    }
14336
0
    }
14337
14338
      /* Mark debug sections referenced by kept debug sections.  */
14339
0
      if (has_kept_debug_info)
14340
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14341
0
    if (isec->gc_mark
14342
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14343
0
      if (!_bfd_elf_gc_mark (info, isec,
14344
0
           elf_gc_mark_debug_section))
14345
0
        return false;
14346
0
    }
14347
0
  return true;
14348
0
}
14349
14350
static bool
14351
elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14352
0
{
14353
0
  bfd *sub;
14354
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14355
14356
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14357
0
    {
14358
0
      asection *o;
14359
14360
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14361
0
    || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14362
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14363
0
  continue;
14364
0
      o = sub->sections;
14365
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14366
0
  continue;
14367
14368
0
      for (o = sub->sections; o != NULL; o = o->next)
14369
0
  {
14370
    /* When any section in a section group is kept, we keep all
14371
       sections in the section group.  If the first member of
14372
       the section group is excluded, we will also exclude the
14373
       group section.  */
14374
0
    if (o->flags & SEC_GROUP)
14375
0
      {
14376
0
        asection *first = elf_next_in_group (o);
14377
0
        o->gc_mark = first->gc_mark;
14378
0
      }
14379
14380
0
    if (o->gc_mark)
14381
0
      continue;
14382
14383
    /* Skip sweeping sections already excluded.  */
14384
0
    if (o->flags & SEC_EXCLUDE)
14385
0
      continue;
14386
14387
    /* Since this is early in the link process, it is simple
14388
       to remove a section from the output.  */
14389
0
    o->flags |= SEC_EXCLUDE;
14390
14391
0
    if (info->print_gc_sections && o->size != 0)
14392
      /* xgettext:c-format */
14393
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14394
0
        o, sub);
14395
0
  }
14396
0
    }
14397
14398
0
  return true;
14399
0
}
14400
14401
/* Propagate collected vtable information.  This is called through
14402
   elf_link_hash_traverse.  */
14403
14404
static bool
14405
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14406
0
{
14407
  /* Those that are not vtables.  */
14408
0
  if (h->start_stop
14409
0
      || h->u2.vtable == NULL
14410
0
      || h->u2.vtable->parent == NULL)
14411
0
    return true;
14412
14413
  /* Those vtables that do not have parents, we cannot merge.  */
14414
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14415
0
    return true;
14416
14417
  /* If we've already been done, exit.  */
14418
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14419
0
    return true;
14420
14421
  /* Make sure the parent's table is up to date.  */
14422
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14423
14424
0
  if (h->u2.vtable->used == NULL)
14425
0
    {
14426
      /* None of this table's entries were referenced.  Re-use the
14427
   parent's table.  */
14428
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14429
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14430
0
    }
14431
0
  else
14432
0
    {
14433
0
      size_t n;
14434
0
      bool *cu, *pu;
14435
14436
      /* Or the parent's entries into ours.  */
14437
0
      cu = h->u2.vtable->used;
14438
0
      cu[-1] = true;
14439
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14440
0
      if (pu != NULL)
14441
0
  {
14442
0
    const struct elf_backend_data *bed;
14443
0
    unsigned int log_file_align;
14444
14445
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14446
0
    log_file_align = bed->s->log_file_align;
14447
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14448
0
    while (n--)
14449
0
      {
14450
0
        if (*pu)
14451
0
    *cu = true;
14452
0
        pu++;
14453
0
        cu++;
14454
0
      }
14455
0
  }
14456
0
    }
14457
14458
0
  return true;
14459
0
}
14460
14461
struct link_info_ok
14462
{
14463
  struct bfd_link_info *info;
14464
  bool ok;
14465
};
14466
14467
static bool
14468
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14469
            void *ptr)
14470
0
{
14471
0
  asection *sec;
14472
0
  bfd_vma hstart, hend;
14473
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14474
0
  const struct elf_backend_data *bed;
14475
0
  unsigned int log_file_align;
14476
0
  struct link_info_ok *info = (struct link_info_ok *) ptr;
14477
14478
  /* Take care of both those symbols that do not describe vtables as
14479
     well as those that are not loaded.  */
14480
0
  if (h->start_stop
14481
0
      || h->u2.vtable == NULL
14482
0
      || h->u2.vtable->parent == NULL)
14483
0
    return true;
14484
14485
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14486
0
        || h->root.type == bfd_link_hash_defweak);
14487
14488
0
  sec = h->root.u.def.section;
14489
0
  hstart = h->root.u.def.value;
14490
0
  hend = hstart + h->size;
14491
14492
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14493
0
               sec, NULL, NULL, true);
14494
0
  if (!relstart)
14495
0
    return info->ok = false;
14496
0
  bed = get_elf_backend_data (sec->owner);
14497
0
  log_file_align = bed->s->log_file_align;
14498
14499
0
  relend = relstart + sec->reloc_count;
14500
14501
0
  for (rel = relstart; rel < relend; ++rel)
14502
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14503
0
      {
14504
  /* If the entry is in use, do nothing.  */
14505
0
  if (h->u2.vtable->used
14506
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14507
0
    {
14508
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14509
0
      if (h->u2.vtable->used[entry])
14510
0
        continue;
14511
0
    }
14512
  /* Otherwise, kill it.  */
14513
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14514
0
      }
14515
14516
0
  return true;
14517
0
}
14518
14519
/* Mark sections containing dynamically referenced symbols.  When
14520
   building shared libraries, we must assume that any visible symbol is
14521
   referenced.  */
14522
14523
bool
14524
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14525
0
{
14526
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
14527
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14528
14529
0
  if ((h->root.type == bfd_link_hash_defined
14530
0
       || h->root.type == bfd_link_hash_defweak)
14531
0
      && (!h->start_stop
14532
0
    || h->root.ldscript_def
14533
0
    || !info->start_stop_gc)
14534
0
      && ((h->ref_dynamic && !h->forced_local)
14535
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14536
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14537
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14538
0
        && (!bfd_link_executable (info)
14539
0
      || info->gc_keep_exported
14540
0
      || info->export_dynamic
14541
0
      || (h->dynamic
14542
0
          && d != NULL
14543
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14544
0
        && (h->versioned >= versioned
14545
0
      || !bfd_hide_sym_by_version (info->version_info,
14546
0
                 h->root.root.string)))))
14547
0
    h->root.u.def.section->flags |= SEC_KEEP;
14548
14549
0
  return true;
14550
0
}
14551
14552
/* Keep all sections containing symbols undefined on the command-line,
14553
   and the section containing the entry symbol.  */
14554
14555
void
14556
_bfd_elf_gc_keep (struct bfd_link_info *info)
14557
0
{
14558
0
  struct bfd_sym_chain *sym;
14559
14560
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14561
0
    {
14562
0
      struct elf_link_hash_entry *h;
14563
14564
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14565
0
        false, false, false);
14566
14567
0
      if (h != NULL
14568
0
    && (h->root.type == bfd_link_hash_defined
14569
0
        || h->root.type == bfd_link_hash_defweak)
14570
0
    && !bfd_is_const_section (h->root.u.def.section))
14571
0
  h->root.u.def.section->flags |= SEC_KEEP;
14572
0
    }
14573
0
}
14574
14575
bool
14576
bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14577
        struct bfd_link_info *info)
14578
0
{
14579
0
  bfd *ibfd = info->input_bfds;
14580
14581
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14582
0
    {
14583
0
      asection *sec;
14584
0
      struct elf_reloc_cookie cookie;
14585
14586
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14587
0
  continue;
14588
0
      sec = ibfd->sections;
14589
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14590
0
  continue;
14591
14592
0
      if (!init_reloc_cookie (&cookie, info, ibfd, false))
14593
0
  return false;
14594
14595
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14596
0
  {
14597
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14598
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14599
0
           false))
14600
0
      {
14601
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14602
0
        fini_reloc_cookie_rels (&cookie, sec);
14603
0
      }
14604
0
  }
14605
0
    }
14606
0
  return true;
14607
0
}
14608
14609
/* Do mark and sweep of unused sections.  */
14610
14611
bool
14612
bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14613
0
{
14614
0
  bool ok = true;
14615
0
  bfd *sub;
14616
0
  elf_gc_mark_hook_fn gc_mark_hook;
14617
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14618
0
  struct elf_link_hash_table *htab;
14619
0
  struct link_info_ok info_ok;
14620
14621
0
  if (!bed->can_gc_sections
14622
0
      || !is_elf_hash_table (info->hash))
14623
0
    {
14624
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14625
0
      return true;
14626
0
    }
14627
14628
0
  bed->gc_keep (info);
14629
0
  htab = elf_hash_table (info);
14630
14631
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14632
     at the .eh_frame section if we can mark the FDEs individually.  */
14633
0
  for (sub = info->input_bfds;
14634
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14635
0
       sub = sub->link.next)
14636
0
    {
14637
0
      asection *sec;
14638
0
      struct elf_reloc_cookie cookie;
14639
14640
0
      sec = sub->sections;
14641
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14642
0
  continue;
14643
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14644
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14645
0
               false))
14646
0
  {
14647
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14648
0
    if (elf_section_data (sec)->sec_info
14649
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14650
0
      elf_eh_frame_section (sub) = sec;
14651
0
    fini_reloc_cookie_for_section (&cookie, sec);
14652
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14653
0
  }
14654
0
    }
14655
14656
  /* Apply transitive closure to the vtable entry usage info.  */
14657
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14658
0
  if (!ok)
14659
0
    return false;
14660
14661
  /* Kill the vtable relocations that were not used.  */
14662
0
  info_ok.info = info;
14663
0
  info_ok.ok = true;
14664
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14665
0
  if (!info_ok.ok)
14666
0
    return false;
14667
14668
  /* Mark dynamically referenced symbols.  */
14669
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14670
0
    elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14671
14672
  /* Grovel through relocs to find out who stays ...  */
14673
0
  gc_mark_hook = bed->gc_mark_hook;
14674
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14675
0
    {
14676
0
      asection *o;
14677
14678
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14679
0
    || elf_object_id (sub) != elf_hash_table_id (htab)
14680
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14681
0
  continue;
14682
14683
0
      o = sub->sections;
14684
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14685
0
  continue;
14686
14687
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14688
   Also treat note sections as a root, if the section is not part
14689
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14690
   well as FINI_ARRAY sections for ld -r.  */
14691
0
      for (o = sub->sections; o != NULL; o = o->next)
14692
0
  if (!o->gc_mark
14693
0
      && (o->flags & SEC_EXCLUDE) == 0
14694
0
      && ((o->flags & SEC_KEEP) != 0
14695
0
    || (bfd_link_relocatable (info)
14696
0
        && ((elf_section_data (o)->this_hdr.sh_type
14697
0
       == SHT_PREINIT_ARRAY)
14698
0
      || (elf_section_data (o)->this_hdr.sh_type
14699
0
          == SHT_INIT_ARRAY)
14700
0
      || (elf_section_data (o)->this_hdr.sh_type
14701
0
          == SHT_FINI_ARRAY)))
14702
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14703
0
        && elf_next_in_group (o) == NULL
14704
0
        && elf_linked_to_section (o) == NULL)
14705
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14706
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14707
0
    {
14708
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14709
0
        return false;
14710
0
    }
14711
0
    }
14712
14713
  /* Allow the backend to mark additional target specific sections.  */
14714
0
  bed->gc_mark_extra_sections (info, gc_mark_hook);
14715
14716
  /* ... and mark SEC_EXCLUDE for those that go.  */
14717
0
  return elf_gc_sweep (abfd, info);
14718
0
}
14719

14720
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14721
14722
bool
14723
bfd_elf_gc_record_vtinherit (bfd *abfd,
14724
           asection *sec,
14725
           struct elf_link_hash_entry *h,
14726
           bfd_vma offset)
14727
0
{
14728
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14729
0
  struct elf_link_hash_entry **search, *child;
14730
0
  size_t extsymcount;
14731
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14732
14733
  /* The sh_info field of the symtab header tells us where the
14734
     external symbols start.  We don't care about the local symbols at
14735
     this point.  */
14736
0
  extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14737
0
  if (!elf_bad_symtab (abfd))
14738
0
    extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14739
14740
0
  sym_hashes = elf_sym_hashes (abfd);
14741
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14742
14743
  /* Hunt down the child symbol, which is in this section at the same
14744
     offset as the relocation.  */
14745
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14746
0
    {
14747
0
      if ((child = *search) != NULL
14748
0
    && (child->root.type == bfd_link_hash_defined
14749
0
        || child->root.type == bfd_link_hash_defweak)
14750
0
    && child->root.u.def.section == sec
14751
0
    && child->root.u.def.value == offset)
14752
0
  goto win;
14753
0
    }
14754
14755
  /* xgettext:c-format */
14756
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14757
0
          abfd, sec, (uint64_t) offset);
14758
0
  bfd_set_error (bfd_error_invalid_operation);
14759
0
  return false;
14760
14761
0
 win:
14762
0
  if (!child->u2.vtable)
14763
0
    {
14764
0
      child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14765
0
        bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14766
0
      if (!child->u2.vtable)
14767
0
  return false;
14768
0
    }
14769
0
  if (!h)
14770
0
    {
14771
      /* This *should* only be the absolute section.  It could potentially
14772
   be that someone has defined a non-global vtable though, which
14773
   would be bad.  It isn't worth paging in the local symbols to be
14774
   sure though; that case should simply be handled by the assembler.  */
14775
14776
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14777
0
    }
14778
0
  else
14779
0
    child->u2.vtable->parent = h;
14780
14781
0
  return true;
14782
0
}
14783
14784
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14785
14786
bool
14787
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14788
         struct elf_link_hash_entry *h,
14789
         bfd_vma addend)
14790
0
{
14791
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14792
0
  unsigned int log_file_align = bed->s->log_file_align;
14793
14794
0
  if (!h)
14795
0
    {
14796
      /* xgettext:c-format */
14797
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14798
0
        abfd, sec);
14799
0
      bfd_set_error (bfd_error_bad_value);
14800
0
      return false;
14801
0
    }
14802
14803
0
  if (!h->u2.vtable)
14804
0
    {
14805
0
      h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14806
0
          bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14807
0
      if (!h->u2.vtable)
14808
0
  return false;
14809
0
    }
14810
14811
0
  if (addend >= h->u2.vtable->size)
14812
0
    {
14813
0
      size_t size, bytes, file_align;
14814
0
      bool *ptr = h->u2.vtable->used;
14815
14816
      /* While the symbol is undefined, we have to be prepared to handle
14817
   a zero size.  */
14818
0
      file_align = 1 << log_file_align;
14819
0
      if (h->root.type == bfd_link_hash_undefined)
14820
0
  size = addend + file_align;
14821
0
      else
14822
0
  {
14823
0
    size = h->size;
14824
0
    if (addend >= size)
14825
0
      {
14826
        /* Oops!  We've got a reference past the defined end of
14827
     the table.  This is probably a bug -- shall we warn?  */
14828
0
        size = addend + file_align;
14829
0
      }
14830
0
  }
14831
0
      size = (size + file_align - 1) & -file_align;
14832
14833
      /* Allocate one extra entry for use as a "done" flag for the
14834
   consolidation pass.  */
14835
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14836
14837
0
      if (ptr)
14838
0
  {
14839
0
    ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14840
14841
0
    if (ptr != NULL)
14842
0
      {
14843
0
        size_t oldbytes;
14844
14845
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14846
0
        * sizeof (bool));
14847
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14848
0
      }
14849
0
  }
14850
0
      else
14851
0
  ptr = (bool *) bfd_zmalloc (bytes);
14852
14853
0
      if (ptr == NULL)
14854
0
  return false;
14855
14856
      /* And arrange for that done flag to be at index -1.  */
14857
0
      h->u2.vtable->used = ptr + 1;
14858
0
      h->u2.vtable->size = size;
14859
0
    }
14860
14861
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14862
14863
0
  return true;
14864
0
}
14865
14866
/* Map an ELF section header flag to its corresponding string.  */
14867
typedef struct
14868
{
14869
  char *flag_name;
14870
  flagword flag_value;
14871
} elf_flags_to_name_table;
14872
14873
static const elf_flags_to_name_table elf_flags_to_names [] =
14874
{
14875
  { "SHF_WRITE", SHF_WRITE },
14876
  { "SHF_ALLOC", SHF_ALLOC },
14877
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14878
  { "SHF_MERGE", SHF_MERGE },
14879
  { "SHF_STRINGS", SHF_STRINGS },
14880
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14881
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14882
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14883
  { "SHF_GROUP", SHF_GROUP },
14884
  { "SHF_TLS", SHF_TLS },
14885
  { "SHF_MASKOS", SHF_MASKOS },
14886
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14887
};
14888
14889
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14890
bool
14891
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14892
            struct flag_info *flaginfo,
14893
            asection *section)
14894
0
{
14895
0
  const bfd_vma sh_flags = elf_section_flags (section);
14896
14897
0
  if (!flaginfo->flags_initialized)
14898
0
    {
14899
0
      bfd *obfd = info->output_bfd;
14900
0
      const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14901
0
      struct flag_info_list *tf = flaginfo->flag_list;
14902
0
      int with_hex = 0;
14903
0
      int without_hex = 0;
14904
14905
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14906
0
  {
14907
0
    unsigned i;
14908
0
    flagword (*lookup) (char *);
14909
14910
0
    lookup = bed->elf_backend_lookup_section_flags_hook;
14911
0
    if (lookup != NULL)
14912
0
      {
14913
0
        flagword hexval = (*lookup) ((char *) tf->name);
14914
14915
0
        if (hexval != 0)
14916
0
    {
14917
0
      if (tf->with == with_flags)
14918
0
        with_hex |= hexval;
14919
0
      else if (tf->with == without_flags)
14920
0
        without_hex |= hexval;
14921
0
      tf->valid = true;
14922
0
      continue;
14923
0
    }
14924
0
      }
14925
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14926
0
      {
14927
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14928
0
    {
14929
0
      if (tf->with == with_flags)
14930
0
        with_hex |= elf_flags_to_names[i].flag_value;
14931
0
      else if (tf->with == without_flags)
14932
0
        without_hex |= elf_flags_to_names[i].flag_value;
14933
0
      tf->valid = true;
14934
0
      break;
14935
0
    }
14936
0
      }
14937
0
    if (!tf->valid)
14938
0
      {
14939
0
        info->callbacks->einfo
14940
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14941
0
        return false;
14942
0
      }
14943
0
  }
14944
0
      flaginfo->flags_initialized = true;
14945
0
      flaginfo->only_with_flags |= with_hex;
14946
0
      flaginfo->not_with_flags |= without_hex;
14947
0
    }
14948
14949
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14950
0
    return false;
14951
14952
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
14953
0
    return false;
14954
14955
0
  return true;
14956
0
}
14957
14958
struct alloc_got_off_arg {
14959
  bfd_vma gotoff;
14960
  struct bfd_link_info *info;
14961
};
14962
14963
/* We need a special top-level link routine to convert got reference counts
14964
   to real got offsets.  */
14965
14966
static bool
14967
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14968
0
{
14969
0
  struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14970
0
  bfd *obfd = gofarg->info->output_bfd;
14971
0
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14972
14973
0
  if (h->got.refcount > 0)
14974
0
    {
14975
0
      h->got.offset = gofarg->gotoff;
14976
0
      gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14977
0
    }
14978
0
  else
14979
0
    h->got.offset = (bfd_vma) -1;
14980
14981
0
  return true;
14982
0
}
14983
14984
/* And an accompanying bit to work out final got entry offsets once
14985
   we're done.  Should be called from final_link.  */
14986
14987
bool
14988
bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14989
          struct bfd_link_info *info)
14990
0
{
14991
0
  bfd *i;
14992
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14993
0
  bfd_vma gotoff;
14994
0
  struct alloc_got_off_arg gofarg;
14995
14996
0
  BFD_ASSERT (abfd == info->output_bfd);
14997
14998
0
  if (! is_elf_hash_table (info->hash))
14999
0
    return false;
15000
15001
  /* The GOT offset is relative to the .got section, but the GOT header is
15002
     put into the .got.plt section, if the backend uses it.  */
15003
0
  if (bed->want_got_plt)
15004
0
    gotoff = 0;
15005
0
  else
15006
0
    gotoff = bed->got_header_size;
15007
15008
  /* Do the local .got entries first.  */
15009
0
  for (i = info->input_bfds; i; i = i->link.next)
15010
0
    {
15011
0
      bfd_signed_vma *local_got;
15012
0
      size_t j, locsymcount;
15013
0
      Elf_Internal_Shdr *symtab_hdr;
15014
15015
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15016
0
  continue;
15017
15018
0
      local_got = elf_local_got_refcounts (i);
15019
0
      if (!local_got)
15020
0
  continue;
15021
15022
0
      symtab_hdr = &elf_tdata (i)->symtab_hdr;
15023
0
      if (elf_bad_symtab (i))
15024
0
  locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15025
0
      else
15026
0
  locsymcount = symtab_hdr->sh_info;
15027
15028
0
      for (j = 0; j < locsymcount; ++j)
15029
0
  {
15030
0
    if (local_got[j] > 0)
15031
0
      {
15032
0
        local_got[j] = gotoff;
15033
0
        gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15034
0
      }
15035
0
    else
15036
0
      local_got[j] = (bfd_vma) -1;
15037
0
  }
15038
0
    }
15039
15040
  /* Then the global .got entries.  .plt refcounts are handled by
15041
     adjust_dynamic_symbol  */
15042
0
  gofarg.gotoff = gotoff;
15043
0
  gofarg.info = info;
15044
0
  elf_link_hash_traverse (elf_hash_table (info),
15045
0
        elf_gc_allocate_got_offsets,
15046
0
        &gofarg);
15047
0
  return true;
15048
0
}
15049
15050
/* Many folk need no more in the way of final link than this, once
15051
   got entry reference counting is enabled.  */
15052
15053
bool
15054
bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15055
0
{
15056
0
  if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15057
0
    return false;
15058
15059
  /* Invoke the regular ELF backend linker to do all the work.  */
15060
0
  return bfd_elf_final_link (abfd, info);
15061
0
}
15062
15063
bool
15064
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15065
0
{
15066
0
  struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15067
15068
0
  if (rcookie->bad_symtab)
15069
0
    rcookie->rel = rcookie->rels;
15070
15071
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15072
0
    {
15073
0
      unsigned long r_symndx;
15074
15075
0
      if (! rcookie->bad_symtab)
15076
0
  if (rcookie->rel->r_offset > offset)
15077
0
    return false;
15078
0
      if (rcookie->rel->r_offset != offset)
15079
0
  continue;
15080
15081
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15082
0
      if (r_symndx == STN_UNDEF)
15083
0
  return true;
15084
15085
0
      if (r_symndx >= rcookie->locsymcount
15086
0
    || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
15087
0
  {
15088
0
    struct elf_link_hash_entry *h;
15089
15090
0
    h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
15091
15092
0
    while (h->root.type == bfd_link_hash_indirect
15093
0
     || h->root.type == bfd_link_hash_warning)
15094
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
15095
15096
0
    if ((h->root.type == bfd_link_hash_defined
15097
0
         || h->root.type == bfd_link_hash_defweak)
15098
0
        && (h->root.u.def.section->owner != rcookie->abfd
15099
0
      || h->root.u.def.section->kept_section != NULL
15100
0
      || discarded_section (h->root.u.def.section)))
15101
0
      return true;
15102
0
  }
15103
0
      else
15104
0
  {
15105
    /* It's not a relocation against a global symbol,
15106
       but it could be a relocation against a local
15107
       symbol for a discarded section.  */
15108
0
    asection *isec;
15109
0
    Elf_Internal_Sym *isym;
15110
15111
    /* Need to: get the symbol; get the section.  */
15112
0
    isym = &rcookie->locsyms[r_symndx];
15113
0
    isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
15114
0
    if (isec != NULL
15115
0
        && (isec->kept_section != NULL
15116
0
      || discarded_section (isec)))
15117
0
      return true;
15118
0
  }
15119
0
      return false;
15120
0
    }
15121
0
  return false;
15122
0
}
15123
15124
/* Discard unneeded references to discarded sections.
15125
   Returns -1 on error, 1 if any section's size was changed, 0 if
15126
   nothing changed.  This function assumes that the relocations are in
15127
   sorted order, which is true for all known assemblers.  */
15128
15129
int
15130
bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15131
0
{
15132
0
  struct elf_reloc_cookie cookie;
15133
0
  asection *o;
15134
0
  bfd *abfd;
15135
0
  int changed = 0;
15136
15137
0
  if (info->traditional_format
15138
0
      || !is_elf_hash_table (info->hash))
15139
0
    return 0;
15140
15141
0
  o = bfd_get_section_by_name (output_bfd, ".stab");
15142
0
  if (o != NULL)
15143
0
    {
15144
0
      asection *i;
15145
15146
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15147
0
  {
15148
0
    if (i->size == 0
15149
0
        || i->reloc_count == 0
15150
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
15151
0
      continue;
15152
15153
0
    abfd = i->owner;
15154
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15155
0
      continue;
15156
15157
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15158
0
      return -1;
15159
15160
0
    if (_bfd_discard_section_stabs (abfd, i,
15161
0
            elf_section_data (i)->sec_info,
15162
0
            bfd_elf_reloc_symbol_deleted_p,
15163
0
            &cookie))
15164
0
      changed = 1;
15165
15166
0
    fini_reloc_cookie_for_section (&cookie, i);
15167
0
  }
15168
0
    }
15169
15170
0
  o = NULL;
15171
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15172
0
    o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15173
0
  if (o != NULL)
15174
0
    {
15175
0
      asection *i;
15176
0
      int eh_changed = 0;
15177
0
      unsigned int eh_alignment;  /* Octets.  */
15178
15179
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15180
0
  {
15181
0
    if (i->size == 0)
15182
0
      continue;
15183
15184
0
    abfd = i->owner;
15185
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15186
0
      continue;
15187
15188
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15189
0
      return -1;
15190
15191
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15192
0
    if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
15193
0
             bfd_elf_reloc_symbol_deleted_p,
15194
0
             &cookie))
15195
0
      {
15196
0
        eh_changed = 1;
15197
0
        if (i->size != i->rawsize)
15198
0
    changed = 1;
15199
0
      }
15200
15201
0
    fini_reloc_cookie_for_section (&cookie, i);
15202
0
  }
15203
15204
0
      eh_alignment = ((1 << o->alignment_power)
15205
0
          * bfd_octets_per_byte (output_bfd, o));
15206
      /* Skip over zero terminator, and prevent empty sections from
15207
   adding alignment padding at the end.  */
15208
0
      for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15209
0
  if (i->size == 0)
15210
0
    i->flags |= SEC_EXCLUDE;
15211
0
  else if (i->size > 4)
15212
0
    break;
15213
      /* The last non-empty eh_frame section doesn't need padding.  */
15214
0
      if (i != NULL)
15215
0
  i = i->map_tail.s;
15216
      /* Any prior sections must pad the last FDE out to the output
15217
   section alignment.  Otherwise we might have zero padding
15218
   between sections, which would be seen as a terminator.  */
15219
0
      for (; i != NULL; i = i->map_tail.s)
15220
0
  if (i->size == 4)
15221
    /* All but the last zero terminator should have been removed.  */
15222
0
    BFD_FAIL ();
15223
0
  else
15224
0
    {
15225
0
      bfd_size_type size
15226
0
        = (i->size + eh_alignment - 1) & -eh_alignment;
15227
0
      if (i->size != size)
15228
0
        {
15229
0
    i->size = size;
15230
0
    changed = 1;
15231
0
    eh_changed = 1;
15232
0
        }
15233
0
    }
15234
0
      if (eh_changed)
15235
0
  elf_link_hash_traverse (elf_hash_table (info),
15236
0
        _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15237
0
    }
15238
15239
0
  o = bfd_get_section_by_name (output_bfd, ".sframe");
15240
0
  if (o != NULL)
15241
0
    {
15242
0
      asection *i;
15243
15244
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15245
0
  {
15246
0
    if (i->size == 0)
15247
0
      continue;
15248
15249
0
    abfd = i->owner;
15250
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15251
0
      continue;
15252
15253
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15254
0
      return -1;
15255
15256
0
    if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15257
0
      {
15258
0
        if (_bfd_elf_discard_section_sframe (i,
15259
0
               bfd_elf_reloc_symbol_deleted_p,
15260
0
               &cookie))
15261
0
    {
15262
0
      if (i->size != i->rawsize)
15263
0
        changed = 1;
15264
0
    }
15265
0
      }
15266
0
    fini_reloc_cookie_for_section (&cookie, i);
15267
0
  }
15268
      /* Update the reference to the output .sframe section.  Used to
15269
   determine later if PT_GNU_SFRAME segment is to be generated.  */
15270
0
      if (!_bfd_elf_set_section_sframe (output_bfd, info))
15271
0
  return -1;
15272
0
    }
15273
15274
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15275
0
    {
15276
0
      const struct elf_backend_data *bed;
15277
0
      asection *s;
15278
15279
0
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15280
0
  continue;
15281
0
      s = abfd->sections;
15282
0
      if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15283
0
  continue;
15284
15285
0
      bed = get_elf_backend_data (abfd);
15286
15287
0
      if (bed->elf_backend_discard_info != NULL)
15288
0
  {
15289
0
    if (!init_reloc_cookie (&cookie, info, abfd, false))
15290
0
      return -1;
15291
15292
0
    if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15293
0
      changed = 1;
15294
15295
0
    fini_reloc_cookie (&cookie, abfd);
15296
0
  }
15297
0
    }
15298
15299
0
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15300
0
    _bfd_elf_end_eh_frame_parsing (info);
15301
15302
0
  if (info->eh_frame_hdr_type
15303
0
      && !bfd_link_relocatable (info)
15304
0
      && _bfd_elf_discard_section_eh_frame_hdr (info))
15305
0
    changed = 1;
15306
15307
0
  return changed;
15308
0
}
15309
15310
bool
15311
_bfd_elf_section_already_linked (bfd *abfd,
15312
         asection *sec,
15313
         struct bfd_link_info *info)
15314
0
{
15315
0
  flagword flags;
15316
0
  const char *name, *key;
15317
0
  struct bfd_section_already_linked *l;
15318
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
15319
15320
0
  if (sec->output_section == bfd_abs_section_ptr)
15321
0
    return false;
15322
15323
0
  flags = sec->flags;
15324
15325
  /* Return if it isn't a linkonce section.  A comdat group section
15326
     also has SEC_LINK_ONCE set.  */
15327
0
  if ((flags & SEC_LINK_ONCE) == 0)
15328
0
    return false;
15329
15330
  /* Don't put group member sections on our list of already linked
15331
     sections.  They are handled as a group via their group section.  */
15332
0
  if (elf_sec_group (sec) != NULL)
15333
0
    return false;
15334
15335
  /* For a SHT_GROUP section, use the group signature as the key.  */
15336
0
  name = sec->name;
15337
0
  if ((flags & SEC_GROUP) != 0
15338
0
      && elf_next_in_group (sec) != NULL
15339
0
      && elf_group_name (elf_next_in_group (sec)) != NULL)
15340
0
    key = elf_group_name (elf_next_in_group (sec));
15341
0
  else
15342
0
    {
15343
      /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
15344
0
      if (startswith (name, ".gnu.linkonce.")
15345
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15346
0
  key++;
15347
0
      else
15348
  /* Must be a user linkonce section that doesn't follow gcc's
15349
     naming convention.  In this case we won't be matching
15350
     single member groups.  */
15351
0
  key = name;
15352
0
    }
15353
15354
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
15355
15356
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15357
0
    {
15358
      /* We may have 2 different types of sections on the list: group
15359
   sections with a signature of <key> (<key> is some string),
15360
   and linkonce sections named .gnu.linkonce.<type>.<key>.
15361
   Match like sections.  LTO plugin sections are an exception.
15362
   They are always named .gnu.linkonce.t.<key> and match either
15363
   type of section.  */
15364
0
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15365
0
     && ((flags & SEC_GROUP) != 0
15366
0
         || strcmp (name, l->sec->name) == 0))
15367
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
15368
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
15369
0
  {
15370
    /* The section has already been linked.  See if we should
15371
       issue a warning.  */
15372
0
    if (!_bfd_handle_already_linked (sec, l, info))
15373
0
      return false;
15374
15375
0
    if (flags & SEC_GROUP)
15376
0
      {
15377
0
        asection *first = elf_next_in_group (sec);
15378
0
        asection *s = first;
15379
15380
0
        while (s != NULL)
15381
0
    {
15382
0
      s->output_section = bfd_abs_section_ptr;
15383
      /* Record which group discards it.  */
15384
0
      s->kept_section = l->sec;
15385
0
      s = elf_next_in_group (s);
15386
      /* These lists are circular.  */
15387
0
      if (s == first)
15388
0
        break;
15389
0
    }
15390
0
      }
15391
15392
0
    return true;
15393
0
  }
15394
0
    }
15395
15396
  /* A single member comdat group section may be discarded by a
15397
     linkonce section and vice versa.  */
15398
0
  if ((flags & SEC_GROUP) != 0)
15399
0
    {
15400
0
      asection *first = elf_next_in_group (sec);
15401
15402
0
      if (first != NULL && elf_next_in_group (first) == first)
15403
  /* Check this single member group against linkonce sections.  */
15404
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15405
0
    if ((l->sec->flags & SEC_GROUP) == 0
15406
0
        && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15407
0
      {
15408
0
        first->output_section = bfd_abs_section_ptr;
15409
0
        first->kept_section = l->sec;
15410
0
        sec->output_section = bfd_abs_section_ptr;
15411
0
        break;
15412
0
      }
15413
0
    }
15414
0
  else
15415
    /* Check this linkonce section against single member groups.  */
15416
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15417
0
      if (l->sec->flags & SEC_GROUP)
15418
0
  {
15419
0
    asection *first = elf_next_in_group (l->sec);
15420
15421
0
    if (first != NULL
15422
0
        && elf_next_in_group (first) == first
15423
0
        && bfd_elf_match_symbols_in_sections (first, sec, info))
15424
0
      {
15425
0
        sec->output_section = bfd_abs_section_ptr;
15426
0
        sec->kept_section = first;
15427
0
        break;
15428
0
      }
15429
0
  }
15430
15431
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15432
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15433
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15434
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
15435
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
15436
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15437
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
15438
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
15439
     The reverse order cannot happen as there is never a bfd with only the
15440
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
15441
     matter as here were are looking only for cross-bfd sections.  */
15442
15443
0
  if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15444
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15445
0
      if ((l->sec->flags & SEC_GROUP) == 0
15446
0
    && startswith (l->sec->name, ".gnu.linkonce.t."))
15447
0
  {
15448
0
    if (abfd != l->sec->owner)
15449
0
      sec->output_section = bfd_abs_section_ptr;
15450
0
    break;
15451
0
  }
15452
15453
  /* This is the first section with this name.  Record it.  */
15454
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15455
0
    info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15456
0
  return sec->output_section == bfd_abs_section_ptr;
15457
0
}
15458
15459
bool
15460
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15461
0
{
15462
0
  return sym->st_shndx == SHN_COMMON;
15463
0
}
15464
15465
unsigned int
15466
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15467
0
{
15468
0
  return SHN_COMMON;
15469
0
}
15470
15471
asection *
15472
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15473
0
{
15474
0
  return bfd_com_section_ptr;
15475
0
}
15476
15477
bfd_vma
15478
_bfd_elf_default_got_elt_size (bfd *abfd,
15479
             struct bfd_link_info *info ATTRIBUTE_UNUSED,
15480
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15481
             bfd *ibfd ATTRIBUTE_UNUSED,
15482
             unsigned long symndx ATTRIBUTE_UNUSED)
15483
0
{
15484
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15485
0
  return bed->s->arch_size / 8;
15486
0
}
15487
15488
/* Routines to support the creation of dynamic relocs.  */
15489
15490
/* Returns the name of the dynamic reloc section associated with SEC.  */
15491
15492
static const char *
15493
get_dynamic_reloc_section_name (bfd *       abfd,
15494
        asection *  sec,
15495
        bool is_rela)
15496
0
{
15497
0
  char *name;
15498
0
  const char *old_name = bfd_section_name (sec);
15499
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15500
15501
0
  if (old_name == NULL)
15502
0
    return NULL;
15503
15504
0
  name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15505
0
  sprintf (name, "%s%s", prefix, old_name);
15506
15507
0
  return name;
15508
0
}
15509
15510
/* Returns the dynamic reloc section associated with SEC.
15511
   If necessary compute the name of the dynamic reloc section based
15512
   on SEC's name (looked up in ABFD's string table) and the setting
15513
   of IS_RELA.  */
15514
15515
asection *
15516
_bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15517
            asection *sec,
15518
            bool is_rela)
15519
0
{
15520
0
  asection *reloc_sec = elf_section_data (sec)->sreloc;
15521
15522
0
  if (reloc_sec == NULL)
15523
0
    {
15524
0
      const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15525
15526
0
      if (name != NULL)
15527
0
  {
15528
0
    reloc_sec = bfd_get_linker_section (abfd, name);
15529
15530
0
    if (reloc_sec != NULL)
15531
0
      elf_section_data (sec)->sreloc = reloc_sec;
15532
0
  }
15533
0
    }
15534
15535
0
  return reloc_sec;
15536
0
}
15537
15538
/* Returns the dynamic reloc section associated with SEC.  If the
15539
   section does not exist it is created and attached to the DYNOBJ
15540
   bfd and stored in the SRELOC field of SEC's elf_section_data
15541
   structure.
15542
15543
   ALIGNMENT is the alignment for the newly created section and
15544
   IS_RELA defines whether the name should be .rela.<SEC's name>
15545
   or .rel.<SEC's name>.  The section name is looked up in the
15546
   string table associated with ABFD.  */
15547
15548
asection *
15549
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15550
             bfd *dynobj,
15551
             unsigned int alignment,
15552
             bfd *abfd,
15553
             bool is_rela)
15554
0
{
15555
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15556
15557
0
  if (reloc_sec == NULL)
15558
0
    {
15559
0
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15560
15561
0
      if (name == NULL)
15562
0
  return NULL;
15563
15564
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15565
15566
0
      if (reloc_sec == NULL)
15567
0
  {
15568
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15569
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15570
0
    if ((sec->flags & SEC_ALLOC) != 0)
15571
0
      flags |= SEC_ALLOC | SEC_LOAD;
15572
15573
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15574
0
    if (reloc_sec != NULL)
15575
0
      {
15576
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15577
     name.  Override as it may be wrong, eg. for a user
15578
     section named "auto" we'll get ".relauto" which is
15579
     seen to be a .rela section.  */
15580
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15581
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15582
0
    reloc_sec = NULL;
15583
0
      }
15584
0
  }
15585
15586
0
      elf_section_data (sec)->sreloc = reloc_sec;
15587
0
    }
15588
15589
0
  return reloc_sec;
15590
0
}
15591
15592
/* Copy the ELF symbol type and other attributes for a linker script
15593
   assignment from HSRC to HDEST.  Generally this should be treated as
15594
   if we found a strong non-dynamic definition for HDEST (except that
15595
   ld ignores multiple definition errors).  */
15596
void
15597
_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15598
             struct bfd_link_hash_entry *hdest,
15599
             struct bfd_link_hash_entry *hsrc)
15600
0
{
15601
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15602
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15603
0
  Elf_Internal_Sym isym;
15604
15605
0
  ehdest->type = ehsrc->type;
15606
0
  ehdest->target_internal = ehsrc->target_internal;
15607
15608
0
  isym.st_other = ehsrc->other;
15609
0
  elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15610
0
}
15611
15612
/* Append a RELA relocation REL to section S in BFD.  */
15613
15614
void
15615
elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15616
0
{
15617
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15618
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15619
0
  BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15620
0
  bed->s->swap_reloca_out (abfd, rel, loc);
15621
0
}
15622
15623
/* Append a REL relocation REL to section S in BFD.  */
15624
15625
void
15626
elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15627
0
{
15628
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15629
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15630
0
  BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15631
0
  bed->s->swap_reloc_out (abfd, rel, loc);
15632
0
}
15633
15634
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15635
15636
struct bfd_link_hash_entry *
15637
bfd_elf_define_start_stop (struct bfd_link_info *info,
15638
         const char *symbol, asection *sec)
15639
0
{
15640
0
  struct elf_link_hash_entry *h;
15641
15642
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15643
0
          false, false, true);
15644
  /* NB: Common symbols will be turned into definition later.  */
15645
0
  if (h != NULL
15646
0
      && !h->root.ldscript_def
15647
0
      && (h->root.type == bfd_link_hash_undefined
15648
0
    || h->root.type == bfd_link_hash_undefweak
15649
0
    || ((h->ref_regular || h->def_dynamic)
15650
0
        && !h->def_regular
15651
0
        && h->root.type != bfd_link_hash_common)))
15652
0
    {
15653
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15654
0
      h->verinfo.verdef = NULL;
15655
0
      h->root.type = bfd_link_hash_defined;
15656
0
      h->root.u.def.section = sec;
15657
0
      h->root.u.def.value = 0;
15658
0
      h->def_regular = 1;
15659
0
      h->def_dynamic = 0;
15660
0
      h->start_stop = 1;
15661
0
      h->u2.start_stop_section = sec;
15662
0
      if (symbol[0] == '.')
15663
0
  {
15664
    /* .startof. and .sizeof. symbols are local.  */
15665
0
    const struct elf_backend_data *bed;
15666
0
    bed = get_elf_backend_data (info->output_bfd);
15667
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
15668
0
  }
15669
0
      else
15670
0
  {
15671
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15672
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15673
0
      | info->start_stop_visibility);
15674
0
    if (was_dynamic)
15675
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15676
0
  }
15677
0
      return &h->root;
15678
0
    }
15679
0
  return NULL;
15680
0
}
15681
15682
/* Find dynamic relocs for H that apply to read-only sections.  */
15683
15684
asection *
15685
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15686
0
{
15687
0
  struct elf_dyn_relocs *p;
15688
15689
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15690
0
    {
15691
0
      asection *s = p->sec->output_section;
15692
15693
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15694
0
  return p->sec;
15695
0
    }
15696
0
  return NULL;
15697
0
}
15698
15699
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15700
   read-only sections.  */
15701
15702
bool
15703
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15704
0
{
15705
0
  asection *sec;
15706
15707
0
  if (h->root.type == bfd_link_hash_indirect)
15708
0
    return true;
15709
15710
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15711
0
  if (sec != NULL)
15712
0
    {
15713
0
      struct bfd_link_info *info = (struct bfd_link_info *) inf;
15714
15715
0
      info->flags |= DF_TEXTREL;
15716
      /* xgettext:c-format */
15717
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15718
0
        "in read-only section `%pA'\n"),
15719
0
            sec->owner, h->root.root.string, sec);
15720
15721
0
      if (bfd_link_textrel_check (info))
15722
  /* xgettext:c-format */
15723
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15724
0
          "in read-only section `%pA'\n"),
15725
0
        sec->owner, h->root.root.string, sec);
15726
15727
      /* Not an error, just cut short the traversal.  */
15728
0
      return false;
15729
0
    }
15730
0
  return true;
15731
0
}
15732
15733
/* Add dynamic tags.  */
15734
15735
bool
15736
_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15737
         bool need_dynamic_reloc)
15738
0
{
15739
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15740
15741
0
  if (htab->dynamic_sections_created)
15742
0
    {
15743
      /* Add some entries to the .dynamic section.  We fill in the
15744
   values later, in finish_dynamic_sections, but we must add
15745
   the entries now so that we get the correct size for the
15746
   .dynamic section.  The DT_DEBUG entry is filled in by the
15747
   dynamic linker and used by the debugger.  */
15748
0
#define add_dynamic_entry(TAG, VAL) \
15749
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15750
15751
0
      const struct elf_backend_data *bed
15752
0
  = get_elf_backend_data (output_bfd);
15753
15754
0
      if (bfd_link_executable (info))
15755
0
  {
15756
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15757
0
      return false;
15758
0
  }
15759
15760
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15761
0
  {
15762
    /* DT_PLTGOT is used by prelink even if there is no PLT
15763
       relocation.  */
15764
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15765
0
      return false;
15766
0
  }
15767
15768
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15769
0
  {
15770
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15771
0
        || !add_dynamic_entry (DT_PLTREL,
15772
0
             (bed->rela_plts_and_copies_p
15773
0
              ? DT_RELA : DT_REL))
15774
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15775
0
      return false;
15776
0
  }
15777
15778
0
      if (htab->tlsdesc_plt
15779
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15780
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15781
0
  return false;
15782
15783
0
      if (need_dynamic_reloc)
15784
0
  {
15785
0
    if (bed->rela_plts_and_copies_p)
15786
0
      {
15787
0
        if (!add_dynamic_entry (DT_RELA, 0)
15788
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15789
0
      || !add_dynamic_entry (DT_RELAENT,
15790
0
           bed->s->sizeof_rela))
15791
0
    return false;
15792
0
      }
15793
0
    else
15794
0
      {
15795
0
        if (!add_dynamic_entry (DT_REL, 0)
15796
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15797
0
      || !add_dynamic_entry (DT_RELENT,
15798
0
           bed->s->sizeof_rel))
15799
0
    return false;
15800
0
      }
15801
15802
    /* If any dynamic relocs apply to a read-only section,
15803
       then we need a DT_TEXTREL entry.  */
15804
0
    if ((info->flags & DF_TEXTREL) == 0)
15805
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15806
0
            info);
15807
15808
0
    if ((info->flags & DF_TEXTREL) != 0)
15809
0
      {
15810
0
        if (htab->ifunc_resolvers)
15811
0
    info->callbacks->einfo
15812
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15813
0
         "may result in a segfault at runtime; recompile with %s\n"),
15814
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15815
15816
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15817
0
    return false;
15818
0
      }
15819
0
  }
15820
0
    }
15821
0
#undef add_dynamic_entry
15822
15823
0
  return true;
15824
0
}