Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/bfd/elfxx-x86.c
Line
Count
Source (jump to first uncovered line)
1
/* x86 specific support for ELF
2
   Copyright (C) 2017-2025 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 "elfxx-x86.h"
22
#include "elf-vxworks.h"
23
#include "objalloc.h"
24
25
/* The name of the dynamic interpreter.  This is put in the .interp
26
   section.  */
27
28
0
#define ELF32_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
29
0
#define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
30
0
#define ELFX32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
31
32
bool
33
_bfd_x86_elf_mkobject (bfd *abfd)
34
2.88M
{
35
2.88M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_obj_tdata));
36
2.88M
}
37
38
/* _TLS_MODULE_BASE_ needs to be treated especially when linking
39
   executables.  Rather than setting it to the beginning of the TLS
40
   section, we have to set it to the end.    This function may be called
41
   multiple times, it is idempotent.  */
42
43
void
44
_bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info)
45
0
{
46
0
  struct elf_x86_link_hash_table *htab;
47
0
  struct bfd_link_hash_entry *base;
48
0
  const struct elf_backend_data *bed;
49
50
0
  if (!bfd_link_executable (info))
51
0
    return;
52
53
0
  bed = get_elf_backend_data (info->output_bfd);
54
0
  htab = elf_x86_hash_table (info, bed->target_id);
55
0
  if (htab == NULL)
56
0
    return;
57
58
0
  base = htab->tls_module_base;
59
0
  if (base == NULL)
60
0
    return;
61
62
0
  base->u.def.value = htab->elf.tls_size;
63
0
}
64
65
/* Return the base VMA address which should be subtracted from real addresses
66
   when resolving @dtpoff relocation.
67
   This is PT_TLS segment p_vaddr.  */
68
69
bfd_vma
70
_bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
71
0
{
72
  /* If tls_sec is NULL, we should have signalled an error already.  */
73
0
  if (elf_hash_table (info)->tls_sec == NULL)
74
0
    return 0;
75
0
  return elf_hash_table (info)->tls_sec->vma;
76
0
}
77
78
/* Allocate space in .plt, .got and associated reloc sections for
79
   dynamic relocs.  */
80
81
static bool
82
elf_x86_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
83
0
{
84
0
  struct bfd_link_info *info;
85
0
  struct elf_x86_link_hash_table *htab;
86
0
  struct elf_x86_link_hash_entry *eh;
87
0
  struct elf_dyn_relocs *p;
88
0
  unsigned int plt_entry_size;
89
0
  bool resolved_to_zero;
90
0
  const struct elf_backend_data *bed;
91
92
0
  if (h->root.type == bfd_link_hash_indirect)
93
0
    return true;
94
95
0
  eh = (struct elf_x86_link_hash_entry *) h;
96
97
0
  info = (struct bfd_link_info *) inf;
98
0
  bed = get_elf_backend_data (info->output_bfd);
99
0
  htab = elf_x86_hash_table (info, bed->target_id);
100
0
  if (htab == NULL)
101
0
    return false;
102
103
0
  plt_entry_size = htab->plt.plt_entry_size;
104
105
0
  resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
106
107
  /* We can't use the GOT PLT if pointer equality is needed since
108
     finish_dynamic_symbol won't clear symbol value and the dynamic
109
     linker won't update the GOT slot.  We will get into an infinite
110
     loop at run-time.  */
111
0
  if (htab->plt_got != NULL
112
0
      && h->type != STT_GNU_IFUNC
113
0
      && !h->pointer_equality_needed
114
0
      && h->plt.refcount > 0
115
0
      && h->got.refcount > 0)
116
0
    {
117
      /* Don't use the regular PLT if there are both GOT and GOTPLT
118
   reloctions.  */
119
0
      h->plt.offset = (bfd_vma) -1;
120
121
      /* Use the GOT PLT.  */
122
0
      eh->plt_got.refcount = 1;
123
0
    }
124
125
  /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
126
     here if it is defined and referenced in a non-shared object.  */
127
0
  if (h->type == STT_GNU_IFUNC
128
0
      && h->def_regular)
129
0
    {
130
      /* GOTOFF relocation needs PLT.  */
131
0
      if (eh->gotoff_ref)
132
0
  h->plt.refcount = 1;
133
134
0
      if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &h->dyn_relocs,
135
0
                plt_entry_size,
136
0
                (htab->plt.has_plt0
137
0
                 * plt_entry_size),
138
0
                 htab->got_entry_size,
139
0
                 true))
140
0
  {
141
0
    asection *s = htab->plt_second;
142
0
    if (h->plt.offset != (bfd_vma) -1 && s != NULL)
143
0
      {
144
        /* Use the second PLT section if it is created.  */
145
0
        eh->plt_second.offset = s->size;
146
147
        /* Make room for this entry in the second PLT section.  */
148
0
        s->size += htab->non_lazy_plt->plt_entry_size;
149
0
      }
150
151
0
    return true;
152
0
  }
153
0
      else
154
0
  return false;
155
0
    }
156
  /* Don't create the PLT entry if there are only function pointer
157
     relocations which can be resolved at run-time.  */
158
0
  else if (htab->elf.dynamic_sections_created
159
0
     && (h->plt.refcount > 0
160
0
         || eh->plt_got.refcount > 0))
161
0
    {
162
0
      bool use_plt_got = eh->plt_got.refcount > 0;
163
164
      /* Make sure this symbol is output as a dynamic symbol.
165
   Undefined weak syms won't yet be marked as dynamic.  */
166
0
      if (h->dynindx == -1
167
0
    && !h->forced_local
168
0
    && !resolved_to_zero
169
0
    && h->root.type == bfd_link_hash_undefweak)
170
0
  {
171
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
172
0
      return false;
173
0
  }
174
175
0
      if (bfd_link_pic (info)
176
0
    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
177
0
  {
178
0
    asection *s = htab->elf.splt;
179
0
    asection *second_s = htab->plt_second;
180
0
    asection *got_s = htab->plt_got;
181
0
    bool use_plt;
182
183
    /* If this is the first .plt entry, make room for the special
184
       first entry.  The .plt section is used by prelink to undo
185
       prelinking for dynamic relocations.  */
186
0
    if (s->size == 0)
187
0
      s->size = htab->plt.has_plt0 * plt_entry_size;
188
189
0
    if (use_plt_got)
190
0
      eh->plt_got.offset = got_s->size;
191
0
    else
192
0
      {
193
0
        h->plt.offset = s->size;
194
0
        if (second_s)
195
0
    eh->plt_second.offset = second_s->size;
196
0
      }
197
198
    /* If this symbol is not defined in a regular file, and we are
199
       generating PDE, then set the symbol to this location in the
200
       .plt.  This is required to make function pointers compare
201
       as equal between PDE and the shared library.
202
203
       NB: If PLT is PC-relative, we can use the .plt in PIE for
204
       function address. */
205
0
    if (h->def_regular)
206
0
      use_plt = false;
207
0
    else if (htab->pcrel_plt)
208
0
      use_plt = ! bfd_link_dll (info);
209
0
    else
210
0
      use_plt = bfd_link_pde (info);
211
0
    if (use_plt)
212
0
      {
213
0
        if (use_plt_got)
214
0
    {
215
      /* We need to make a call to the entry of the GOT PLT
216
         instead of regular PLT entry.  */
217
0
      h->root.u.def.section = got_s;
218
0
      h->root.u.def.value = eh->plt_got.offset;
219
0
    }
220
0
        else
221
0
    {
222
0
      if (second_s)
223
0
        {
224
          /* We need to make a call to the entry of the
225
       second PLT instead of regular PLT entry.  */
226
0
          h->root.u.def.section = second_s;
227
0
          h->root.u.def.value = eh->plt_second.offset;
228
0
        }
229
0
      else
230
0
        {
231
0
          h->root.u.def.section = s;
232
0
          h->root.u.def.value = h->plt.offset;
233
0
        }
234
0
    }
235
0
      }
236
237
    /* Make room for this entry.  */
238
0
    if (use_plt_got)
239
0
      got_s->size += htab->non_lazy_plt->plt_entry_size;
240
0
    else
241
0
      {
242
0
        s->size += plt_entry_size;
243
0
        if (second_s)
244
0
    second_s->size += htab->non_lazy_plt->plt_entry_size;
245
246
        /* We also need to make an entry in the .got.plt section,
247
     which will be placed in the .got section by the linker
248
     script.  */
249
0
        htab->elf.sgotplt->size += htab->got_entry_size;
250
251
        /* There should be no PLT relocation against resolved
252
     undefined weak symbol in executable.  */
253
0
        if (!resolved_to_zero)
254
0
    {
255
      /* We also need to make an entry in the .rel.plt
256
         section.  */
257
0
      htab->elf.srelplt->size += htab->sizeof_reloc;
258
0
      htab->elf.srelplt->reloc_count++;
259
0
    }
260
0
      }
261
262
0
    if (htab->elf.target_os == is_vxworks && !bfd_link_pic (info))
263
0
      {
264
        /* VxWorks has a second set of relocations for each PLT entry
265
     in executables.  They go in a separate relocation section,
266
     which is processed by the kernel loader.  */
267
268
        /* There are two relocations for the initial PLT entry: an
269
     R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
270
     R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
271
272
0
        asection *srelplt2 = htab->srelplt2;
273
0
        if (h->plt.offset == plt_entry_size)
274
0
    srelplt2->size += (htab->sizeof_reloc * 2);
275
276
        /* There are two extra relocations for each subsequent PLT entry:
277
     an R_386_32 relocation for the GOT entry, and an R_386_32
278
     relocation for the PLT entry.  */
279
280
0
        srelplt2->size += (htab->sizeof_reloc * 2);
281
0
      }
282
0
  }
283
0
      else
284
0
  {
285
0
    eh->plt_got.offset = (bfd_vma) -1;
286
0
    h->plt.offset = (bfd_vma) -1;
287
0
    h->needs_plt = 0;
288
0
  }
289
0
    }
290
0
  else
291
0
    {
292
0
      eh->plt_got.offset = (bfd_vma) -1;
293
0
      h->plt.offset = (bfd_vma) -1;
294
0
      h->needs_plt = 0;
295
0
    }
296
297
0
  eh->tlsdesc_got = (bfd_vma) -1;
298
299
  /* For i386, if R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the
300
     binary, make it a R_386_TLS_LE_32 requiring no TLS entry.  For
301
     x86-64, if R_X86_64_GOTTPOFF symbol is now local to the binary,
302
     make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
303
0
  if (h->got.refcount > 0
304
0
      && bfd_link_executable (info)
305
0
      && h->dynindx == -1
306
0
      && (elf_x86_hash_entry (h)->tls_type & GOT_TLS_IE))
307
0
    h->got.offset = (bfd_vma) -1;
308
0
  else if (h->got.refcount > 0)
309
0
    {
310
0
      asection *s;
311
0
      bool dyn;
312
0
      int tls_type = elf_x86_hash_entry (h)->tls_type;
313
314
      /* Make sure this symbol is output as a dynamic symbol.
315
   Undefined weak syms won't yet be marked as dynamic.  */
316
0
      if (h->dynindx == -1
317
0
    && !h->forced_local
318
0
    && !resolved_to_zero
319
0
    && h->root.type == bfd_link_hash_undefweak)
320
0
  {
321
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
322
0
      return false;
323
0
  }
324
325
0
      s = htab->elf.sgot;
326
0
      if (GOT_TLS_GDESC_P (tls_type))
327
0
  {
328
0
    eh->tlsdesc_got = htab->elf.sgotplt->size
329
0
      - elf_x86_compute_jump_table_size (htab);
330
0
    htab->elf.sgotplt->size += 2 * htab->got_entry_size;
331
0
    h->got.offset = (bfd_vma) -2;
332
0
  }
333
0
      if (! GOT_TLS_GDESC_P (tls_type)
334
0
    || GOT_TLS_GD_P (tls_type))
335
0
  {
336
0
    h->got.offset = s->size;
337
0
    s->size += htab->got_entry_size;
338
    /* R_386_TLS_GD and R_X86_64_TLSGD need 2 consecutive GOT
339
       slots.  */
340
0
    if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
341
0
      s->size += htab->got_entry_size;
342
0
  }
343
0
      dyn = htab->elf.dynamic_sections_created;
344
      /* R_386_TLS_IE_32 needs one dynamic relocation,
345
   R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
346
   (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
347
   need two), R_386_TLS_GD and R_X86_64_TLSGD need one if local
348
   symbol and two if global.  No dynamic relocation against
349
   resolved undefined weak symbol in executable.  No dynamic
350
   relocation against non-preemptible absolute symbol.  */
351
0
      if (tls_type == GOT_TLS_IE_BOTH)
352
0
  htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
353
0
      else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
354
0
         || (tls_type & GOT_TLS_IE))
355
0
  htab->elf.srelgot->size += htab->sizeof_reloc;
356
0
      else if (GOT_TLS_GD_P (tls_type))
357
0
  htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
358
0
      else if (! GOT_TLS_GDESC_P (tls_type)
359
0
         && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
360
0
        && !resolved_to_zero)
361
0
       || h->root.type != bfd_link_hash_undefweak)
362
0
         && ((bfd_link_pic (info)
363
0
        && !(h->dynindx == -1
364
0
       && ABS_SYMBOL_P (h)))
365
0
       || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
366
0
  htab->elf.srelgot->size += htab->sizeof_reloc;
367
0
      if (GOT_TLS_GDESC_P (tls_type))
368
0
  {
369
0
    htab->elf.srelplt->size += htab->sizeof_reloc;
370
0
    if (bed->target_id == X86_64_ELF_DATA)
371
0
      htab->elf.tlsdesc_plt = (bfd_vma) -1;
372
0
  }
373
0
    }
374
0
  else
375
0
    h->got.offset = (bfd_vma) -1;
376
377
0
  if (h->dyn_relocs == NULL)
378
0
    return true;
379
380
  /* In the shared -Bsymbolic case, discard space allocated for
381
     dynamic pc-relative relocs against symbols which turn out to be
382
     defined in regular objects.  For the normal shared case, discard
383
     space for pc-relative relocs that have become local due to symbol
384
     visibility changes.  */
385
386
0
  if (bfd_link_pic (info))
387
0
    {
388
      /* Relocs that use pc_count are those that appear on a call
389
   insn, or certain REL relocs that can generated via assembly.
390
   We want calls to protected symbols to resolve directly to the
391
   function rather than going via the plt.  If people want
392
   function pointer comparisons to work as expected then they
393
   should avoid writing weird assembly.  */
394
0
      if (SYMBOL_CALLS_LOCAL (info, h))
395
0
  {
396
0
    struct elf_dyn_relocs **pp;
397
398
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
399
0
      {
400
0
        p->count -= p->pc_count;
401
0
        p->pc_count = 0;
402
0
        if (p->count == 0)
403
0
    *pp = p->next;
404
0
        else
405
0
    pp = &p->next;
406
0
      }
407
0
  }
408
409
0
      if (htab->elf.target_os == is_vxworks)
410
0
  {
411
0
    struct elf_dyn_relocs **pp;
412
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
413
0
      {
414
0
        if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
415
0
    *pp = p->next;
416
0
        else
417
0
    pp = &p->next;
418
0
      }
419
0
  }
420
421
      /* Also discard relocs on undefined weak syms with non-default
422
   visibility or in PIE.  */
423
0
      if (h->dyn_relocs != NULL)
424
0
  {
425
0
    if (h->root.type == bfd_link_hash_undefweak)
426
0
      {
427
        /* Undefined weak symbol is never bound locally in shared
428
     library.  */
429
0
        if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
430
0
      || resolved_to_zero)
431
0
    {
432
0
      if (bed->target_id == I386_ELF_DATA
433
0
          && h->non_got_ref)
434
0
        {
435
          /* Keep dynamic non-GOT/non-PLT relocation so
436
       that we can branch to 0 without PLT.  */
437
0
          struct elf_dyn_relocs **pp;
438
439
0
          for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
440
0
      if (p->pc_count == 0)
441
0
        *pp = p->next;
442
0
      else
443
0
        {
444
          /* Remove non-R_386_PC32 relocation.  */
445
0
          p->count = p->pc_count;
446
0
          pp = &p->next;
447
0
        }
448
449
          /* Make sure undefined weak symbols are output
450
       as dynamic symbols in PIEs for dynamic non-GOT
451
       non-PLT reloations.  */
452
0
          if (h->dyn_relocs != NULL
453
0
        && !bfd_elf_link_record_dynamic_symbol (info, h))
454
0
      return false;
455
0
        }
456
0
      else
457
0
        h->dyn_relocs = NULL;
458
0
    }
459
0
        else if (h->dynindx == -1
460
0
           && !h->forced_local
461
0
           && !bfd_elf_link_record_dynamic_symbol (info, h))
462
0
    return false;
463
0
      }
464
0
    else if (bfd_link_executable (info)
465
0
       && (h->needs_copy || eh->needs_copy)
466
0
       && h->def_dynamic
467
0
       && !h->def_regular)
468
0
      {
469
        /* NB: needs_copy is set only for x86-64.  For PIE,
470
     discard space for pc-relative relocs against symbols
471
     which turn out to need copy relocs.  */
472
0
        struct elf_dyn_relocs **pp;
473
474
0
        for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
475
0
    {
476
0
      if (p->pc_count != 0)
477
0
        *pp = p->next;
478
0
      else
479
0
        pp = &p->next;
480
0
    }
481
0
      }
482
0
  }
483
0
    }
484
0
  else if (ELIMINATE_COPY_RELOCS)
485
0
    {
486
      /* For the non-shared case, discard space for relocs against
487
   symbols which turn out to need copy relocs or are not
488
   dynamic.  Keep dynamic relocations for run-time function
489
   pointer initialization.  */
490
491
0
      if ((!h->non_got_ref
492
0
     || (h->root.type == bfd_link_hash_undefweak
493
0
         && !resolved_to_zero))
494
0
    && ((h->def_dynamic
495
0
         && !h->def_regular)
496
0
        || (htab->elf.dynamic_sections_created
497
0
      && (h->root.type == bfd_link_hash_undefweak
498
0
          || h->root.type == bfd_link_hash_undefined))))
499
0
  {
500
    /* Make sure this symbol is output as a dynamic symbol.
501
       Undefined weak syms won't yet be marked as dynamic.  */
502
0
    if (h->dynindx == -1
503
0
        && !h->forced_local
504
0
        && !resolved_to_zero
505
0
        && h->root.type == bfd_link_hash_undefweak
506
0
        && ! bfd_elf_link_record_dynamic_symbol (info, h))
507
0
      return false;
508
509
    /* If that succeeded, we know we'll be keeping all the
510
       relocs.  */
511
0
    if (h->dynindx != -1)
512
0
      goto keep;
513
0
  }
514
515
0
      h->dyn_relocs = NULL;
516
517
0
    keep: ;
518
0
    }
519
520
  /* Finally, allocate space.  */
521
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
522
0
    {
523
0
      asection *sreloc;
524
525
0
      if (eh->def_protected && bfd_link_executable (info))
526
0
  {
527
    /* Disallow copy relocation against non-copyable protected
528
       symbol.  */
529
0
    asection *s = p->sec->output_section;
530
0
    if (s != NULL && (s->flags & SEC_READONLY) != 0)
531
0
      {
532
0
        info->callbacks->fatal
533
    /* xgettext:c-format */
534
0
    (_("%P: %pB: copy relocation against non-copyable "
535
0
       "protected symbol `%s' in %pB\n"),
536
0
     p->sec->owner, h->root.root.string,
537
0
     h->root.u.def.section->owner);
538
0
        return false;
539
0
      }
540
0
  }
541
542
0
      sreloc = elf_section_data (p->sec)->sreloc;
543
544
0
      BFD_ASSERT (sreloc != NULL);
545
0
      sreloc->size += p->count * htab->sizeof_reloc;
546
0
    }
547
548
0
  return true;
549
0
}
550
551
/* Allocate space in .plt, .got and associated reloc sections for
552
   local dynamic relocs.  */
553
554
static int
555
elf_x86_allocate_local_dynreloc (void **slot, void *inf)
556
0
{
557
0
  struct elf_link_hash_entry *h
558
0
    = (struct elf_link_hash_entry *) *slot;
559
560
0
  if (h->type != STT_GNU_IFUNC
561
0
      || !h->def_regular
562
0
      || !h->ref_regular
563
0
      || !h->forced_local
564
0
      || h->root.type != bfd_link_hash_defined)
565
0
    abort ();
566
567
0
  return elf_x86_allocate_dynrelocs (h, inf);
568
0
}
569
570
/* Find and/or create a hash entry for local symbol.  */
571
572
struct elf_link_hash_entry *
573
_bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
574
         bfd *abfd, const Elf_Internal_Rela *rel,
575
         bool create)
576
0
{
577
0
  struct elf_x86_link_hash_entry e, *ret;
578
0
  asection *sec = abfd->sections;
579
0
  hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
580
0
               htab->r_sym (rel->r_info));
581
0
  void **slot;
582
583
0
  e.elf.indx = sec->id;
584
0
  e.elf.dynstr_index = htab->r_sym (rel->r_info);
585
0
  slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
586
0
           create ? INSERT : NO_INSERT);
587
588
0
  if (!slot)
589
0
    return NULL;
590
591
0
  if (*slot)
592
0
    {
593
0
      ret = (struct elf_x86_link_hash_entry *) *slot;
594
0
      return &ret->elf;
595
0
    }
596
597
0
  ret = (struct elf_x86_link_hash_entry *)
598
0
  objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
599
0
      sizeof (struct elf_x86_link_hash_entry));
600
0
  if (ret)
601
0
    {
602
0
      memset (ret, 0, sizeof (*ret));
603
0
      ret->elf.indx = sec->id;
604
0
      ret->elf.dynstr_index = htab->r_sym (rel->r_info);
605
0
      ret->elf.dynindx = -1;
606
0
      ret->plt_got.offset = (bfd_vma) -1;
607
0
      *slot = ret;
608
0
    }
609
0
  return &ret->elf;
610
0
}
611
612
/* Create an entry in an x86 ELF linker hash table.  */
613
614
struct bfd_hash_entry *
615
_bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
616
        struct bfd_hash_table *table,
617
        const char *string)
618
0
{
619
  /* Allocate the structure if it has not already been allocated by a
620
     subclass.  */
621
0
  if (entry == NULL)
622
0
    {
623
0
      entry = (struct bfd_hash_entry *)
624
0
  bfd_hash_allocate (table,
625
0
         sizeof (struct elf_x86_link_hash_entry));
626
0
      if (entry == NULL)
627
0
  return entry;
628
0
    }
629
630
  /* Call the allocation method of the superclass.  */
631
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
632
0
  if (entry != NULL)
633
0
    {
634
0
      struct elf_x86_link_hash_entry *eh
635
0
       = (struct elf_x86_link_hash_entry *) entry;
636
637
0
      memset (&eh->elf + 1, 0, sizeof (*eh) - sizeof (eh->elf));
638
      /* Set local fields.  */
639
0
      eh->plt_second.offset = (bfd_vma) -1;
640
0
      eh->plt_got.offset = (bfd_vma) -1;
641
0
      eh->tlsdesc_got = (bfd_vma) -1;
642
0
      eh->zero_undefweak = 1;
643
0
    }
644
645
0
  return entry;
646
0
}
647
648
/* Compute a hash of a local hash entry.  We use elf_link_hash_entry
649
  for local symbol so that we can handle local STT_GNU_IFUNC symbols
650
  as global symbol.  We reuse indx and dynstr_index for local symbol
651
  hash since they aren't used by global symbols in this backend.  */
652
653
hashval_t
654
_bfd_x86_elf_local_htab_hash (const void *ptr)
655
0
{
656
0
  struct elf_link_hash_entry *h
657
0
    = (struct elf_link_hash_entry *) ptr;
658
0
  return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
659
0
}
660
661
/* Compare local hash entries.  */
662
663
int
664
_bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
665
0
{
666
0
  struct elf_link_hash_entry *h1
667
0
     = (struct elf_link_hash_entry *) ptr1;
668
0
  struct elf_link_hash_entry *h2
669
0
    = (struct elf_link_hash_entry *) ptr2;
670
671
0
  return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
672
0
}
673
674
/* Destroy an x86 ELF linker hash table.  */
675
676
static void
677
elf_x86_link_hash_table_free (bfd *obfd)
678
0
{
679
0
  struct elf_x86_link_hash_table *htab
680
0
    = (struct elf_x86_link_hash_table *) obfd->link.hash;
681
682
0
  free (htab->dt_relr_bitmap.u.elf64);
683
0
  free (htab->unaligned_relative_reloc.data);
684
0
  free (htab->relative_reloc.data);
685
0
  if (htab->loc_hash_table)
686
0
    htab_delete (htab->loc_hash_table);
687
0
  if (htab->loc_hash_memory)
688
0
    objalloc_free ((struct objalloc *) htab->loc_hash_memory);
689
0
  _bfd_elf_link_hash_table_free (obfd);
690
0
}
691
692
static bool
693
elf_i386_is_reloc_section (const char *secname)
694
0
{
695
0
  return startswith (secname, ".rel");
696
0
}
697
698
static bool
699
elf_x86_64_is_reloc_section (const char *secname)
700
0
{
701
0
  return startswith (secname, ".rela");
702
0
}
703
704
/* Create an x86 ELF linker hash table.  */
705
706
struct bfd_link_hash_table *
707
_bfd_x86_elf_link_hash_table_create (bfd *abfd)
708
0
{
709
0
  struct elf_x86_link_hash_table *ret;
710
0
  size_t amt = sizeof (struct elf_x86_link_hash_table);
711
712
0
  ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
713
0
  if (ret == NULL)
714
0
    return NULL;
715
716
0
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
717
0
              _bfd_x86_elf_link_hash_newfunc,
718
0
              sizeof (struct elf_x86_link_hash_entry)))
719
0
    {
720
0
      free (ret);
721
0
      return NULL;
722
0
    }
723
724
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
725
0
  if (bed->target_id == X86_64_ELF_DATA)
726
0
    {
727
0
      ret->is_reloc_section = elf_x86_64_is_reloc_section;
728
0
      ret->got_entry_size = 8;
729
0
      ret->pcrel_plt = true;
730
0
      ret->tls_get_addr = "__tls_get_addr";
731
0
      ret->relative_r_type = R_X86_64_RELATIVE;
732
0
      ret->relative_r_name = "R_X86_64_RELATIVE";
733
0
      ret->ax_register = "RAX";
734
0
      ret->elf_append_reloc = elf_append_rela;
735
0
      ret->elf_write_addend_in_got = _bfd_elf64_write_addend;
736
0
    }
737
0
  if (ABI_64_P (abfd))
738
0
    {
739
0
      ret->sizeof_reloc = sizeof (Elf64_External_Rela);
740
0
      ret->pointer_r_type = R_X86_64_64;
741
0
      ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
742
0
      ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
743
0
      ret->elf_write_addend = _bfd_elf64_write_addend;
744
0
    }
745
0
  else
746
0
    {
747
0
      if (bed->target_id == X86_64_ELF_DATA)
748
0
  {
749
0
    ret->sizeof_reloc = sizeof (Elf32_External_Rela);
750
0
    ret->pointer_r_type = R_X86_64_32;
751
0
    ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
752
0
    ret->dynamic_interpreter_size
753
0
      = sizeof ELFX32_DYNAMIC_INTERPRETER;
754
0
    ret->elf_write_addend = _bfd_elf32_write_addend;
755
0
  }
756
0
      else
757
0
  {
758
0
    ret->is_reloc_section = elf_i386_is_reloc_section;
759
0
    ret->sizeof_reloc = sizeof (Elf32_External_Rel);
760
0
    ret->got_entry_size = 4;
761
0
    ret->pcrel_plt = false;
762
0
    ret->pointer_r_type = R_386_32;
763
0
    ret->relative_r_type = R_386_RELATIVE;
764
0
    ret->relative_r_name = "R_386_RELATIVE";
765
0
    ret->ax_register = "EAX";
766
0
    ret->elf_append_reloc = elf_append_rel;
767
0
    ret->elf_write_addend = _bfd_elf32_write_addend;
768
0
    ret->elf_write_addend_in_got = _bfd_elf32_write_addend;
769
0
    ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
770
0
    ret->dynamic_interpreter_size
771
0
      = sizeof ELF32_DYNAMIC_INTERPRETER;
772
0
    ret->tls_get_addr = "___tls_get_addr";
773
0
  }
774
0
    }
775
776
0
  ret->loc_hash_table = htab_try_create (1024,
777
0
           _bfd_x86_elf_local_htab_hash,
778
0
           _bfd_x86_elf_local_htab_eq,
779
0
           NULL);
780
0
  ret->loc_hash_memory = objalloc_create ();
781
0
  if (!ret->loc_hash_table || !ret->loc_hash_memory)
782
0
    {
783
0
      elf_x86_link_hash_table_free (abfd);
784
0
      return NULL;
785
0
    }
786
0
  ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
787
788
0
  return &ret->elf.root;
789
0
}
790
791
/* Sort relocs into address order.  */
792
793
int
794
_bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
795
143k
{
796
143k
  const arelent *a = * (const arelent **) ap;
797
143k
  const arelent *b = * (const arelent **) bp;
798
799
143k
  if (a->address > b->address)
800
47.8k
    return 1;
801
95.3k
  else if (a->address < b->address)
802
95.3k
    return -1;
803
0
  else
804
0
    return 0;
805
143k
}
806
807
/* Mark symbol, NAME, as locally defined by linker if it is referenced
808
   and not defined in a relocatable object file.  */
809
810
static void
811
elf_x86_linker_defined (struct bfd_link_info *info, const char *name)
812
0
{
813
0
  struct elf_link_hash_entry *h;
814
815
  /* NULL indicates __ehdr_start.  */
816
0
  if (name == NULL)
817
0
    h = elf_hash_table (info)->hehdr_start;
818
0
  else
819
0
    h = elf_link_hash_lookup (elf_hash_table (info), name,
820
0
            false, false, false);
821
0
  if (h == NULL)
822
0
    return;
823
824
0
  while (h->root.type == bfd_link_hash_indirect)
825
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
826
827
0
  if (h->root.type == bfd_link_hash_new
828
0
      || h->root.type == bfd_link_hash_undefined
829
0
      || h->root.type == bfd_link_hash_undefweak
830
0
      || h->root.type == bfd_link_hash_common
831
0
      || (!h->def_regular && h->def_dynamic))
832
0
    {
833
0
      elf_x86_hash_entry (h)->local_ref = 2;
834
0
      elf_x86_hash_entry (h)->linker_def = 1;
835
0
    }
836
0
}
837
838
/* Hide a linker-defined symbol, NAME, with hidden visibility.  */
839
840
static void
841
elf_x86_hide_linker_defined (struct bfd_link_info *info,
842
           const char *name)
843
0
{
844
0
  struct elf_link_hash_entry *h;
845
846
0
  h = elf_link_hash_lookup (elf_hash_table (info), name,
847
0
          false, false, false);
848
0
  if (h == NULL)
849
0
    return;
850
851
0
  while (h->root.type == bfd_link_hash_indirect)
852
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
853
854
0
  if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
855
0
      || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
856
0
    _bfd_elf_link_hash_hide_symbol (info, h, true);
857
0
}
858
859
bool
860
_bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
861
0
{
862
0
  if (!bfd_link_relocatable (info))
863
0
    {
864
      /* Check for __tls_get_addr reference.  */
865
0
      struct elf_x86_link_hash_table *htab;
866
0
      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
867
0
      htab = elf_x86_hash_table (info, bed->target_id);
868
0
      if (htab)
869
0
  {
870
0
    struct elf_link_hash_entry *h;
871
872
0
    h = elf_link_hash_lookup (elf_hash_table (info),
873
0
            htab->tls_get_addr,
874
0
            false, false, false);
875
0
    if (h != NULL)
876
0
      {
877
0
        elf_x86_hash_entry (h)->tls_get_addr = 1;
878
879
        /* Check the versioned __tls_get_addr symbol.  */
880
0
        while (h->root.type == bfd_link_hash_indirect)
881
0
    {
882
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
883
0
      elf_x86_hash_entry (h)->tls_get_addr = 1;
884
0
    }
885
0
      }
886
887
    /* Pass NULL for __ehdr_start which will be defined by
888
       linker as a hidden symbol later if it is referenced and
889
       not defined.  */
890
0
    elf_x86_linker_defined (info, NULL);
891
892
0
    if (bfd_link_executable (info))
893
0
      {
894
        /* References to __bss_start, _end and _edata should be
895
     locally resolved within executables.  */
896
0
        elf_x86_linker_defined (info, "__bss_start");
897
0
        elf_x86_linker_defined (info, "_end");
898
0
        elf_x86_linker_defined (info, "_edata");
899
0
      }
900
0
    else
901
0
      {
902
        /* Hide hidden __bss_start, _end and _edata in shared
903
     libraries.  */
904
0
        elf_x86_hide_linker_defined (info, "__bss_start");
905
0
        elf_x86_hide_linker_defined (info, "_end");
906
0
        elf_x86_hide_linker_defined (info, "_edata");
907
0
      }
908
0
  }
909
0
    }
910
911
  /* Invoke the regular ELF backend linker to do all the work.  */
912
0
  return _bfd_elf_link_check_relocs (abfd, info);
913
0
}
914
915
/* Look through the relocs for a section before allocation to make the
916
   dynamic reloc section.  */
917
918
bool
919
_bfd_x86_elf_check_relocs (bfd *abfd,
920
         struct bfd_link_info *info,
921
         asection *sec,
922
         const Elf_Internal_Rela *relocs)
923
0
{
924
0
  struct elf_x86_link_hash_table *htab;
925
0
  Elf_Internal_Shdr *symtab_hdr;
926
0
  struct elf_link_hash_entry **sym_hashes;
927
0
  const Elf_Internal_Rela *rel;
928
0
  const Elf_Internal_Rela *rel_end;
929
0
  asection *sreloc;
930
0
  const struct elf_backend_data *bed;
931
0
  bool is_x86_64;
932
933
0
  if (bfd_link_relocatable (info))
934
0
    return true;
935
936
0
  bed = get_elf_backend_data (abfd);
937
0
  htab = elf_x86_hash_table (info, bed->target_id);
938
0
  if (htab == NULL)
939
0
    {
940
0
      sec->check_relocs_failed = 1;
941
0
      return false;
942
0
    }
943
944
0
  is_x86_64 = bed->target_id == X86_64_ELF_DATA;
945
946
0
  symtab_hdr = &elf_symtab_hdr (abfd);
947
0
  sym_hashes = elf_sym_hashes (abfd);
948
949
0
  rel_end = relocs + sec->reloc_count;
950
0
  for (rel = relocs; rel < rel_end; rel++)
951
0
    {
952
0
      unsigned int r_type;
953
0
      unsigned int r_symndx;
954
0
      struct elf_link_hash_entry *h;
955
956
0
      r_symndx = htab->r_sym (rel->r_info);
957
0
      r_type = ELF32_R_TYPE (rel->r_info);
958
959
0
      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
960
0
  {
961
    /* xgettext:c-format */
962
0
    _bfd_error_handler (_("%pB: bad symbol index: %d"),
963
0
            abfd, r_symndx);
964
0
    goto error_return;
965
0
  }
966
967
0
      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx, symtab_hdr);
968
969
0
      if (X86_NEED_DYNAMIC_RELOC_TYPE_P (is_x86_64, r_type)
970
0
    && NEED_DYNAMIC_RELOCATION_P (is_x86_64, info, true, h, sec,
971
0
          r_type, htab->pointer_r_type))
972
0
  {
973
    /* We may copy these reloc types into the output file.
974
       Create a reloc section in dynobj and make room for
975
       this reloc.  */
976
0
    sreloc = _bfd_elf_make_dynamic_reloc_section
977
0
      (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
978
0
       abfd, sec->use_rela_p);
979
980
0
    if (sreloc != NULL)
981
0
      return true;
982
983
0
  error_return:
984
0
    sec->check_relocs_failed = 1;
985
0
    return false;
986
0
  }
987
0
    }
988
989
0
  return true;
990
0
}
991
992
/* Add an entry to the relative reloc record.  */
993
994
static bool
995
elf_x86_relative_reloc_record_add
996
  (struct bfd_link_info *info,
997
   struct elf_x86_relative_reloc_data *relative_reloc,
998
   Elf_Internal_Rela *rel, asection *sec,
999
   asection *sym_sec, struct elf_link_hash_entry *h,
1000
   Elf_Internal_Sym *sym, bfd_vma offset, bool *keep_symbuf_p)
1001
0
{
1002
0
  bfd_size_type newidx;
1003
1004
0
  if (relative_reloc->data == NULL)
1005
0
    {
1006
0
      relative_reloc->data = bfd_malloc
1007
0
  (sizeof (struct elf_x86_relative_reloc_record));
1008
0
      relative_reloc->count = 0;
1009
0
      relative_reloc->size = 1;
1010
0
    }
1011
1012
0
  newidx = relative_reloc->count++;
1013
1014
0
  if (relative_reloc->count > relative_reloc->size)
1015
0
    {
1016
0
      relative_reloc->size <<= 1;
1017
0
      relative_reloc->data = bfd_realloc
1018
0
  (relative_reloc->data,
1019
0
   (relative_reloc->size
1020
0
    * sizeof (struct elf_x86_relative_reloc_record)));
1021
0
    }
1022
1023
0
  if (relative_reloc->data == NULL)
1024
0
    {
1025
0
      info->callbacks->fatal
1026
  /* xgettext:c-format */
1027
0
  (_("%P: %pB: failed to allocate relative reloc record\n"),
1028
0
   info->output_bfd);
1029
0
      return false;
1030
0
    }
1031
1032
0
  relative_reloc->data[newidx].rel = *rel;
1033
0
  relative_reloc->data[newidx].sec = sec;
1034
0
  if (h != NULL)
1035
0
    {
1036
      /* Set SYM to NULL to indicate a global symbol.  */
1037
0
      relative_reloc->data[newidx].sym = NULL;
1038
0
      relative_reloc->data[newidx].u.h = h;
1039
0
    }
1040
0
  else
1041
0
    {
1042
0
      relative_reloc->data[newidx].sym = sym;
1043
0
      relative_reloc->data[newidx].u.sym_sec = sym_sec;
1044
      /* We must keep the symbol buffer since SYM will be used later.  */
1045
0
      *keep_symbuf_p = true;
1046
0
    }
1047
0
  relative_reloc->data[newidx].offset = offset;
1048
0
  relative_reloc->data[newidx].address = 0;
1049
0
  return true;
1050
0
}
1051
1052
/* After input sections have been mapped to output sections and
1053
   addresses of output sections are set initiallly, scan input
1054
   relocations with the same logic in relocate_section to determine
1055
   if a relative relocation should be generated.  Save the relative
1056
   relocation candidate information for sizing the DT_RELR section
1057
   later after all symbols addresses can be determined.  */
1058
1059
bool
1060
_bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
1061
         asection *input_section,
1062
         struct bfd_link_info *info,
1063
         bool *again)
1064
0
{
1065
0
  Elf_Internal_Shdr *symtab_hdr;
1066
0
  Elf_Internal_Rela *internal_relocs;
1067
0
  Elf_Internal_Rela *irel, *irelend;
1068
0
  Elf_Internal_Sym *isymbuf = NULL;
1069
0
  struct elf_link_hash_entry **sym_hashes;
1070
0
  const struct elf_backend_data *bed;
1071
0
  struct elf_x86_link_hash_table *htab;
1072
0
  bfd_vma *local_got_offsets;
1073
0
  bool is_x86_64;
1074
0
  bool unaligned_section;
1075
0
  bool return_status = false;
1076
0
  bool keep_symbuf = false;
1077
1078
  /* Assume we're not going to change any sizes, and we'll only need
1079
     one pass.  */
1080
0
  *again = false;
1081
1082
0
  if (bfd_link_relocatable (info))
1083
0
    return true;
1084
1085
0
  if (!info->enable_dt_relr)
1086
0
    return true;
1087
1088
0
  bed = get_elf_backend_data (abfd);
1089
0
  htab = elf_x86_hash_table (info, bed->target_id);
1090
0
  if (htab == NULL)
1091
0
    return true;
1092
1093
  /* Nothing to do if there are no relocations or relative relocations
1094
     have been packed.  */
1095
0
  if (input_section == htab->elf.srelrdyn
1096
0
      || input_section->relative_reloc_packed
1097
0
      || ((input_section->flags & (SEC_RELOC | SEC_ALLOC))
1098
0
    != (SEC_RELOC | SEC_ALLOC))
1099
0
      || (input_section->flags & SEC_DEBUGGING) != 0
1100
0
      || input_section->reloc_count == 0)
1101
0
    return true;
1102
1103
  /* Skip if the section isn't aligned.  */
1104
0
  unaligned_section = input_section->alignment_power == 0;
1105
1106
0
  is_x86_64 = bed->target_id == X86_64_ELF_DATA;
1107
1108
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1109
0
  sym_hashes = elf_sym_hashes (abfd);
1110
0
  local_got_offsets = elf_local_got_offsets (abfd);
1111
1112
  /* Load the relocations for this section.  */
1113
0
  internal_relocs =
1114
0
    _bfd_elf_link_info_read_relocs (abfd, info, input_section, NULL,
1115
0
            (Elf_Internal_Rela *) NULL,
1116
0
            info->keep_memory);
1117
0
  if (internal_relocs == NULL)
1118
0
    return false;
1119
1120
0
  irelend = internal_relocs + input_section->reloc_count;
1121
0
  for (irel = internal_relocs; irel < irelend; irel++)
1122
0
    {
1123
0
      unsigned int r_type;
1124
0
      unsigned int r_symndx;
1125
0
      Elf_Internal_Sym *isym;
1126
0
      struct elf_link_hash_entry *h;
1127
0
      struct elf_x86_link_hash_entry *eh;
1128
0
      bfd_vma offset;
1129
0
      bool resolved_to_zero;
1130
0
      bool need_copy_reloc_in_pie;
1131
0
      bool pc32_reloc;
1132
0
      asection *sec;
1133
      /* Offset must be a multiple of 2.  */
1134
0
      bool unaligned_offset = (irel->r_offset & 1) != 0;
1135
      /* True if there is a relative relocation against a dynamic
1136
   symbol.  */
1137
0
      bool dynamic_relative_reloc_p;
1138
1139
      /* Get the value of the symbol referred to by the reloc.  */
1140
0
      r_symndx = htab->r_sym (irel->r_info);
1141
1142
0
      r_type = ELF32_R_TYPE (irel->r_info);
1143
      /* Clear the R_X86_64_converted_reloc_bit bit.  */
1144
0
      r_type &= ~R_X86_64_converted_reloc_bit;
1145
1146
0
      sec = NULL;
1147
0
      h = NULL;
1148
0
      dynamic_relative_reloc_p = false;
1149
1150
0
      if (r_symndx < symtab_hdr->sh_info)
1151
0
  {
1152
    /* Read this BFD's local symbols.  */
1153
0
    if (isymbuf == NULL)
1154
0
      {
1155
0
        isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1156
0
        if (isymbuf == NULL)
1157
0
    {
1158
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1159
0
              symtab_hdr->sh_info,
1160
0
              0, NULL, NULL, NULL);
1161
0
      if (isymbuf == NULL)
1162
0
        goto error_return;
1163
0
    }
1164
0
      }
1165
1166
0
    isym = isymbuf + r_symndx;
1167
0
    switch (isym->st_shndx)
1168
0
      {
1169
0
      case SHN_ABS:
1170
0
        sec = bfd_abs_section_ptr;
1171
0
        break;
1172
0
      case SHN_COMMON:
1173
0
        sec = bfd_com_section_ptr;
1174
0
        break;
1175
0
      case SHN_X86_64_LCOMMON:
1176
0
        if (!is_x86_64)
1177
0
    abort ();
1178
0
        sec = &_bfd_elf_large_com_section;
1179
0
        break;
1180
0
      default:
1181
0
        sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1182
0
        break;
1183
0
      }
1184
1185
    /* Skip relocation against local STT_GNU_IFUNC symbol.  */
1186
0
    if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1187
0
      continue;
1188
1189
0
    eh = (struct elf_x86_link_hash_entry *) h;
1190
0
    resolved_to_zero = false;
1191
0
  }
1192
0
      else
1193
0
  {
1194
    /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below.  */
1195
0
    h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx, symtab_hdr);
1196
0
    if (h == NULL)
1197
0
      {
1198
        /* FIXMEL: Issue an error message ?  */
1199
0
        continue;
1200
0
      }
1201
1202
0
    if (h->root.type == bfd_link_hash_defined
1203
0
        || h->root.type == bfd_link_hash_defweak)
1204
0
      sec = h->root.u.def.section;
1205
1206
    /* Skip relocation against STT_GNU_IFUNC symbol.  */
1207
0
    if (h->type == STT_GNU_IFUNC)
1208
0
      continue;
1209
1210
0
    eh = (struct elf_x86_link_hash_entry *) h;
1211
0
    resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
1212
1213
    /* NB: See how elf_backend_finish_dynamic_symbol is called
1214
       from elf_link_output_extsym.  */
1215
0
    if ((h->dynindx != -1 || h->forced_local)
1216
0
        && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1217
0
       || h->root.type != bfd_link_hash_undefweak)
1218
0
      || !h->forced_local)
1219
0
        && h->got.offset != (bfd_vma) -1
1220
0
        && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
1221
0
        && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
1222
0
        && !resolved_to_zero
1223
0
        && SYMBOL_REFERENCES_LOCAL_P (info, h)
1224
0
        && SYMBOL_DEFINED_NON_SHARED_P (h))
1225
0
      dynamic_relative_reloc_p = true;
1226
1227
0
    isym = NULL;
1228
0
  }
1229
1230
0
      if (X86_GOT_TYPE_P (is_x86_64, r_type))
1231
0
  {
1232
    /* Pack GOT relative relocations.  There should be only a
1233
       single R_*_RELATIVE relocation in GOT.  */
1234
0
    if (eh != NULL)
1235
0
      {
1236
0
        if (eh->got_relative_reloc_done)
1237
0
    continue;
1238
1239
0
        if (!(dynamic_relative_reloc_p
1240
0
        || (RESOLVED_LOCALLY_P (info, h, htab)
1241
0
      && GENERATE_RELATIVE_RELOC_P (info, h))))
1242
0
    continue;
1243
1244
0
        if (!dynamic_relative_reloc_p)
1245
0
    eh->no_finish_dynamic_symbol = 1;
1246
0
        eh->got_relative_reloc_done = 1;
1247
0
        offset = h->got.offset;
1248
0
      }
1249
0
    else
1250
0
      {
1251
0
        if (elf_x86_relative_reloc_done (abfd)[r_symndx])
1252
0
    continue;
1253
1254
0
        if (!X86_LOCAL_GOT_RELATIVE_RELOC_P (is_x86_64, info,
1255
0
               isym))
1256
0
    continue;
1257
1258
0
        elf_x86_relative_reloc_done (abfd)[r_symndx] = 1;
1259
0
        offset = local_got_offsets[r_symndx];
1260
0
      }
1261
1262
0
    if (!elf_x86_relative_reloc_record_add (info,
1263
0
              &htab->relative_reloc,
1264
0
              irel, htab->elf.sgot,
1265
0
              sec, h, isym, offset,
1266
0
              &keep_symbuf))
1267
0
      goto error_return;
1268
1269
0
    continue;
1270
0
  }
1271
1272
0
      if (is_x86_64
1273
0
    && irel->r_addend == 0
1274
0
    && !ABI_64_P (info->output_bfd))
1275
0
  {
1276
    /* For x32, if addend is zero, treat R_X86_64_64 like
1277
       R_X86_64_32 and R_X86_64_SIZE64 like R_X86_64_SIZE32.  */
1278
0
    if (r_type == R_X86_64_64)
1279
0
      r_type = R_X86_64_32;
1280
0
    else if (r_type == R_X86_64_SIZE64)
1281
0
      r_type = R_X86_64_SIZE32;
1282
0
  }
1283
1284
0
      if (!X86_RELATIVE_RELOC_TYPE_P (is_x86_64, r_type))
1285
0
  continue;
1286
1287
      /* Pack non-GOT relative relocations.  */
1288
0
      if (is_x86_64)
1289
0
  {
1290
0
    need_copy_reloc_in_pie =
1291
0
      (bfd_link_pie (info)
1292
0
       && h != NULL
1293
0
       && (h->needs_copy
1294
0
     || eh->needs_copy
1295
0
     || (h->root.type == bfd_link_hash_undefined))
1296
0
       && (X86_PCREL_TYPE_P (true, r_type)
1297
0
     || X86_SIZE_TYPE_P (true, r_type)));
1298
0
    pc32_reloc = false;
1299
0
  }
1300
0
      else
1301
0
  {
1302
0
    need_copy_reloc_in_pie = false;
1303
0
    pc32_reloc = r_type == R_386_PC32;
1304
0
  }
1305
1306
0
      if (GENERATE_DYNAMIC_RELOCATION_P (is_x86_64, info, eh, r_type,
1307
0
           sec, need_copy_reloc_in_pie,
1308
0
           resolved_to_zero, pc32_reloc))
1309
0
  {
1310
    /* When generating a shared object, these relocations
1311
       are copied into the output file to be resolved at run
1312
       time.  */
1313
0
    offset = _bfd_elf_section_offset (info->output_bfd, info,
1314
0
              input_section,
1315
0
              irel->r_offset);
1316
0
    if (offset == (bfd_vma) -1
1317
0
        || offset == (bfd_vma) -2
1318
0
        || COPY_INPUT_RELOC_P (is_x86_64, info, h, r_type))
1319
0
      continue;
1320
1321
    /* This symbol is local, or marked to become local.  When
1322
       relocation overflow check is disabled, we convert
1323
       R_X86_64_32 to dynamic R_X86_64_RELATIVE.  */
1324
0
    if (is_x86_64
1325
0
        && !(r_type == htab->pointer_r_type
1326
0
       || (r_type == R_X86_64_32
1327
0
           && htab->params->no_reloc_overflow_check)))
1328
0
      continue;
1329
1330
0
    if (!elf_x86_relative_reloc_record_add
1331
0
          (info,
1332
0
     ((unaligned_section || unaligned_offset)
1333
0
      ? &htab->unaligned_relative_reloc
1334
0
      : &htab->relative_reloc),
1335
0
     irel, input_section, sec, h, isym, offset,
1336
0
     &keep_symbuf))
1337
0
      goto error_return;
1338
0
  }
1339
0
    }
1340
1341
0
  input_section->relative_reloc_packed = 1;
1342
1343
0
  return_status = true;
1344
1345
0
error_return:
1346
0
  if ((unsigned char *) isymbuf != symtab_hdr->contents)
1347
0
    {
1348
      /* Cache the symbol buffer if it must be kept.  */
1349
0
      if (keep_symbuf)
1350
0
  symtab_hdr->contents = (unsigned char *) isymbuf;
1351
0
      else
1352
0
  free (isymbuf);
1353
0
    }
1354
0
  if (elf_section_data (input_section)->relocs != internal_relocs)
1355
0
    free (internal_relocs);
1356
0
  return return_status;
1357
0
}
1358
1359
/* Add an entry to the 64-bit DT_RELR bitmap.  */
1360
1361
static void
1362
elf64_dt_relr_bitmap_add
1363
  (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1364
   uint64_t entry)
1365
0
{
1366
0
  bfd_size_type newidx;
1367
1368
0
  if (bitmap->u.elf64 == NULL)
1369
0
    {
1370
0
      bitmap->u.elf64 = bfd_malloc (sizeof (uint64_t));
1371
0
      bitmap->count = 0;
1372
0
      bitmap->size = 1;
1373
0
    }
1374
1375
0
  newidx = bitmap->count++;
1376
1377
0
  if (bitmap->count > bitmap->size)
1378
0
    {
1379
0
      bitmap->size <<= 1;
1380
0
      bitmap->u.elf64 = bfd_realloc (bitmap->u.elf64,
1381
0
             (bitmap->size * sizeof (uint64_t)));
1382
0
    }
1383
1384
0
  if (bitmap->u.elf64 == NULL)
1385
0
    {
1386
0
      info->callbacks->fatal
1387
  /* xgettext:c-format */
1388
0
  (_("%P: %pB: failed to allocate 64-bit DT_RELR bitmap\n"),
1389
0
   info->output_bfd);
1390
0
    }
1391
1392
0
  bitmap->u.elf64[newidx] = entry;
1393
0
}
1394
1395
/* Add an entry to the 32-bit DT_RELR bitmap.  */
1396
1397
static void
1398
elf32_dt_relr_bitmap_add
1399
  (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1400
   uint32_t entry)
1401
0
{
1402
0
  bfd_size_type newidx;
1403
1404
0
  if (bitmap->u.elf32 == NULL)
1405
0
    {
1406
0
      bitmap->u.elf32 = bfd_malloc (sizeof (uint32_t));
1407
0
      bitmap->count = 0;
1408
0
      bitmap->size = 1;
1409
0
    }
1410
1411
0
  newidx = bitmap->count++;
1412
1413
0
  if (bitmap->count > bitmap->size)
1414
0
    {
1415
0
      bitmap->size <<= 1;
1416
0
      bitmap->u.elf32 = bfd_realloc (bitmap->u.elf32,
1417
0
             (bitmap->size * sizeof (uint32_t)));
1418
0
    }
1419
1420
0
  if (bitmap->u.elf32 == NULL)
1421
0
    {
1422
0
      info->callbacks->fatal
1423
  /* xgettext:c-format */
1424
0
  (_("%P: %pB: failed to allocate 32-bit DT_RELR bitmap\n"),
1425
0
   info->output_bfd);
1426
0
    }
1427
1428
0
  bitmap->u.elf32[newidx] = entry;
1429
0
}
1430
1431
void
1432
_bfd_elf32_write_addend (bfd *abfd, uint64_t value, void *addr)
1433
0
{
1434
0
  bfd_put_32 (abfd, value, addr);
1435
0
}
1436
1437
void
1438
_bfd_elf64_write_addend (bfd *abfd, uint64_t value, void *addr)
1439
0
{
1440
0
  bfd_put_64 (abfd, value, addr);
1441
0
}
1442
1443
/* Size or finish relative relocations to determine the run-time
1444
   addresses for DT_RELR bitmap computation later.  OUTREL is set
1445
   to NULL in the sizing phase and non-NULL in the finising phase
1446
   where the regular relative relocations will be written out.  */
1447
1448
static void
1449
elf_x86_size_or_finish_relative_reloc
1450
  (bool is_x86_64, struct bfd_link_info *info,
1451
   struct elf_x86_link_hash_table *htab, bool unaligned,
1452
   Elf_Internal_Rela *outrel)
1453
0
{
1454
0
  unsigned int align_mask;
1455
0
  bfd_size_type i, count;
1456
0
  asection *sec, *srel;
1457
0
  struct elf_link_hash_entry *h;
1458
0
  bfd_vma offset;
1459
0
  Elf_Internal_Sym *sym;
1460
0
  asection *sym_sec;
1461
0
  asection *sgot = htab->elf.sgot;
1462
0
  asection *srelgot = htab->elf.srelgot;
1463
0
  struct elf_x86_relative_reloc_data *relative_reloc;
1464
1465
0
  if (unaligned)
1466
0
    {
1467
0
      align_mask = 0;
1468
0
      relative_reloc = &htab->unaligned_relative_reloc;
1469
0
    }
1470
0
  else
1471
0
    {
1472
0
      align_mask = 1;
1473
0
      relative_reloc = &htab->relative_reloc;
1474
0
    }
1475
1476
0
  count = relative_reloc->count;
1477
0
  for (i = 0; i < count; i++)
1478
0
    {
1479
0
      sec = relative_reloc->data[i].sec;
1480
0
      sym = relative_reloc->data[i].sym;
1481
1482
      /* If SYM is NULL, it must be a global symbol.  */
1483
0
      if (sym == NULL)
1484
0
  h = relative_reloc->data[i].u.h;
1485
0
      else
1486
0
  h = NULL;
1487
1488
0
      if (is_x86_64)
1489
0
  {
1490
0
    bfd_vma relocation;
1491
    /* This function may be called more than once and REL may be
1492
       updated by _bfd_elf_rela_local_sym below.  */
1493
0
    Elf_Internal_Rela rel = relative_reloc->data[i].rel;
1494
1495
0
    if (h != NULL)
1496
0
      {
1497
0
        if (h->root.type == bfd_link_hash_defined
1498
0
      || h->root.type == bfd_link_hash_defweak)
1499
0
    {
1500
0
      sym_sec = h->root.u.def.section;
1501
0
      relocation = (h->root.u.def.value
1502
0
        + sym_sec->output_section->vma
1503
0
        + sym_sec->output_offset);
1504
0
    }
1505
0
        else
1506
0
    {
1507
      /* Allow undefined symbol only at the sizing phase.
1508
         Otherwise skip undefined symbol here.  Undefined
1509
         symbol will be reported by relocate_section.  */
1510
0
      if (outrel == NULL)
1511
0
        relocation = 0;
1512
0
      else
1513
0
        continue;
1514
0
    }
1515
0
      }
1516
0
    else
1517
0
      {
1518
0
        sym_sec = relative_reloc->data[i].u.sym_sec;
1519
0
        relocation = _bfd_elf_rela_local_sym
1520
0
    (info->output_bfd, sym, &sym_sec, &rel);
1521
0
      }
1522
1523
0
    if (outrel != NULL)
1524
0
      {
1525
0
        outrel->r_addend = relocation;
1526
0
        if (sec == sgot)
1527
0
    {
1528
0
      if (h != NULL && h->needs_plt)
1529
0
        abort ();
1530
0
    }
1531
0
        else
1532
0
    outrel->r_addend += rel.r_addend;
1533
1534
        /* Write the implicit addend if ALIGN_MASK isn't 0.  */
1535
0
        if (align_mask)
1536
0
    {
1537
0
      if (sec == sgot)
1538
0
        {
1539
0
          if (relative_reloc->data[i].offset >= sec->size)
1540
0
      abort ();
1541
0
          htab->elf_write_addend_in_got
1542
0
      (info->output_bfd, outrel->r_addend,
1543
0
       sec->contents + relative_reloc->data[i].offset);
1544
0
        }
1545
0
      else
1546
0
        {
1547
0
          bfd_byte *contents;
1548
1549
0
          if (rel.r_offset >= sec->size)
1550
0
      abort ();
1551
1552
0
          if (elf_section_data (sec)->this_hdr.contents
1553
0
        != NULL)
1554
0
      contents
1555
0
        = elf_section_data (sec)->this_hdr.contents;
1556
0
          else
1557
0
      {
1558
0
        if (!_bfd_elf_mmap_section_contents (sec->owner,
1559
0
                     sec,
1560
0
                     &contents))
1561
0
          info->callbacks->fatal
1562
            /* xgettext:c-format */
1563
0
            (_("%P: %pB: failed to allocate memory for section `%pA'\n"),
1564
0
             info->output_bfd, sec);
1565
1566
        /* Cache the section contents for
1567
           elf_link_input_bfd.  */
1568
0
        elf_section_data (sec)->this_hdr.contents
1569
0
          = contents;
1570
0
      }
1571
0
          htab->elf_write_addend
1572
0
      (info->output_bfd, outrel->r_addend,
1573
0
       contents + rel.r_offset);
1574
0
        }
1575
0
    }
1576
0
      }
1577
0
  }
1578
1579
0
      if (sec == sgot)
1580
0
  srel = srelgot;
1581
0
      else
1582
0
  srel = elf_section_data (sec)->sreloc;
1583
0
      offset = (sec->output_section->vma + sec->output_offset
1584
0
    + relative_reloc->data[i].offset);
1585
0
      relative_reloc->data[i].address = offset;
1586
0
      if (outrel != NULL)
1587
0
  {
1588
0
    outrel->r_offset = offset;
1589
1590
0
    if ((outrel->r_offset & align_mask) != 0)
1591
0
      abort ();
1592
1593
0
    if (htab->params->report_relative_reloc)
1594
0
      _bfd_x86_elf_link_report_relative_reloc
1595
0
        (info, sec, h, sym, htab->relative_r_name, outrel);
1596
1597
    /* Generate regular relative relocation if ALIGN_MASK is 0.  */
1598
0
    if (align_mask == 0)
1599
0
      htab->elf_append_reloc (info->output_bfd, srel, outrel);
1600
0
  }
1601
0
    }
1602
0
}
1603
1604
/* Compute the DT_RELR section size.  Set NEED_PLAYOUT to true if
1605
   the DT_RELR section size has been increased.  */
1606
1607
static void
1608
elf_x86_compute_dl_relr_bitmap
1609
  (struct bfd_link_info *info, struct elf_x86_link_hash_table *htab,
1610
   bool *need_layout)
1611
0
{
1612
0
  bfd_vma base;
1613
0
  bfd_size_type i, count, new_count;
1614
0
  struct elf_x86_relative_reloc_data *relative_reloc =
1615
0
    &htab->relative_reloc;
1616
  /* Save the old DT_RELR bitmap count.  Don't shrink the DT_RELR bitmap
1617
     if the new DT_RELR bitmap count is smaller than the old one.  Pad
1618
     with trailing 1s which won't be decoded to more relocations.  */
1619
0
  bfd_size_type dt_relr_bitmap_count = htab->dt_relr_bitmap.count;
1620
1621
  /* Clear the DT_RELR bitmap count.  */
1622
0
  htab->dt_relr_bitmap.count = 0;
1623
1624
0
  count = relative_reloc->count;
1625
1626
0
  if (ABI_64_P (info->output_bfd))
1627
0
    {
1628
      /* Compute the 64-bit DT_RELR bitmap.  */
1629
0
      i = 0;
1630
0
      while (i < count)
1631
0
  {
1632
0
    if ((relative_reloc->data[i].address % 1) != 0)
1633
0
      abort ();
1634
1635
0
    elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1636
0
            relative_reloc->data[i].address);
1637
1638
0
    base = relative_reloc->data[i].address + 8;
1639
0
    i++;
1640
1641
0
    while (i < count)
1642
0
      {
1643
0
        uint64_t bitmap = 0;
1644
0
        for (; i < count; i++)
1645
0
    {
1646
0
      bfd_vma delta = (relative_reloc->data[i].address
1647
0
           - base);
1648
      /* Stop if it is too far from base.  */
1649
0
      if (delta >= 63 * 8)
1650
0
        break;
1651
      /* Stop if it isn't a multiple of 8.  */
1652
0
      if ((delta % 8) != 0)
1653
0
        break;
1654
0
      bitmap |= 1ULL << (delta / 8);
1655
0
    }
1656
1657
0
        if (bitmap == 0)
1658
0
    break;
1659
1660
0
        elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1661
0
          (bitmap << 1) | 1);
1662
1663
0
        base += 63 * 8;
1664
0
      }
1665
0
  }
1666
1667
0
      new_count = htab->dt_relr_bitmap.count;
1668
0
      if (dt_relr_bitmap_count > new_count)
1669
0
  {
1670
    /* Don't shrink the DT_RELR section size to avoid section
1671
       layout oscillation.  Instead, pad the DT_RELR bitmap with
1672
       1s which do not decode to more relocations.  */
1673
1674
0
    htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1675
0
    count = dt_relr_bitmap_count - new_count;
1676
0
    for (i = 0; i < count; i++)
1677
0
      htab->dt_relr_bitmap.u.elf64[new_count + i] = 1;
1678
0
  }
1679
0
    }
1680
0
  else
1681
0
    {
1682
      /* Compute the 32-bit DT_RELR bitmap.  */
1683
0
      i = 0;
1684
0
      while (i < count)
1685
0
  {
1686
0
    if ((relative_reloc->data[i].address % 1) != 0)
1687
0
      abort ();
1688
1689
0
    elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1690
0
            relative_reloc->data[i].address);
1691
1692
0
    base = relative_reloc->data[i].address + 4;
1693
0
    i++;
1694
1695
0
    while (i < count)
1696
0
      {
1697
0
        uint32_t bitmap = 0;
1698
0
        for (; i < count; i++)
1699
0
    {
1700
0
      bfd_vma delta = (relative_reloc->data[i].address
1701
0
           - base);
1702
      /* Stop if it is too far from base.  */
1703
0
      if (delta >= 31 * 4)
1704
0
        break;
1705
      /* Stop if it isn't a multiple of 4.  */
1706
0
      if ((delta % 4) != 0)
1707
0
        break;
1708
0
      bitmap |= 1ULL << (delta / 4);
1709
0
    }
1710
1711
0
        if (bitmap == 0)
1712
0
    break;
1713
1714
0
        elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1715
0
          (bitmap << 1) | 1);
1716
1717
0
        base += 31 * 4;
1718
0
      }
1719
0
  }
1720
1721
0
      new_count = htab->dt_relr_bitmap.count;
1722
0
      if (dt_relr_bitmap_count > new_count)
1723
0
  {
1724
    /* Don't shrink the DT_RELR section size to avoid section
1725
       layout oscillation.  Instead, pad the DT_RELR bitmap with
1726
       1s which do not decode to more relocations.  */
1727
1728
0
    htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1729
0
    count = dt_relr_bitmap_count - new_count;
1730
0
    for (i = 0; i < count; i++)
1731
0
      htab->dt_relr_bitmap.u.elf32[new_count + i] = 1;
1732
0
  }
1733
0
    }
1734
1735
0
  if (htab->dt_relr_bitmap.count != dt_relr_bitmap_count)
1736
0
    {
1737
0
      if (need_layout)
1738
0
  {
1739
    /* The .relr.dyn section size is changed.  Update the section
1740
       size and tell linker to layout sections again.  */
1741
0
    htab->elf.srelrdyn->size =
1742
0
      (htab->dt_relr_bitmap.count
1743
0
       * (ABI_64_P (info->output_bfd) ? 8 : 4));
1744
1745
0
    *need_layout = true;
1746
0
  }
1747
0
      else
1748
0
  info->callbacks->fatal
1749
    /* xgettext:c-format */
1750
0
    (_("%P: %pB: size of compact relative reloc section is "
1751
0
       "changed: new (%lu) != old (%lu)\n"),
1752
0
     info->output_bfd, htab->dt_relr_bitmap.count,
1753
0
     dt_relr_bitmap_count);
1754
0
    }
1755
0
}
1756
1757
/* Write out the DT_RELR section.  */
1758
1759
static void
1760
elf_x86_write_dl_relr_bitmap (struct bfd_link_info *info,
1761
            struct elf_x86_link_hash_table *htab)
1762
0
{
1763
0
  asection *sec = htab->elf.srelrdyn;
1764
0
  bfd_size_type size = sec->size;
1765
0
  bfd_size_type i;
1766
0
  unsigned char *contents;
1767
1768
0
  contents = (unsigned char *) bfd_alloc (sec->owner, size);
1769
0
  if (contents == NULL)
1770
0
    info->callbacks->fatal
1771
      /* xgettext:c-format */
1772
0
      (_("%P: %pB: failed to allocate compact relative reloc section\n"),
1773
0
       info->output_bfd);
1774
1775
  /* Cache the section contents for elf_link_input_bfd.  */
1776
0
  sec->contents = contents;
1777
0
  sec->alloced = 1;
1778
1779
0
  if (ABI_64_P (info->output_bfd))
1780
0
    for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 8)
1781
0
      bfd_put_64 (info->output_bfd, htab->dt_relr_bitmap.u.elf64[i],
1782
0
      contents);
1783
0
  else
1784
0
    for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 4)
1785
0
      bfd_put_32 (info->output_bfd, htab->dt_relr_bitmap.u.elf32[i],
1786
0
      contents);
1787
0
}
1788
1789
/* Sort relative relocations by address.  */
1790
1791
static int
1792
elf_x86_relative_reloc_compare (const void *pa, const void *pb)
1793
0
{
1794
0
  struct elf_x86_relative_reloc_record *a =
1795
0
    (struct elf_x86_relative_reloc_record *) pa;
1796
0
  struct elf_x86_relative_reloc_record *b =
1797
0
    (struct elf_x86_relative_reloc_record *) pb;
1798
0
  if (a->address < b->address)
1799
0
    return -1;
1800
0
  if (a->address > b->address)
1801
0
    return 1;
1802
0
  return 0;
1803
0
}
1804
1805
enum dynobj_sframe_plt_type
1806
{
1807
  SFRAME_PLT = 1,
1808
  SFRAME_PLT_SEC = 2,
1809
  SFRAME_PLT_GOT = 3,
1810
};
1811
1812
/* Create SFrame stack trace info for the plt entries in the .plt section
1813
   of type PLT_SEC_TYPE.  */
1814
1815
static bool
1816
_bfd_x86_elf_create_sframe_plt (bfd *output_bfd,
1817
        struct bfd_link_info *info,
1818
        unsigned int plt_sec_type)
1819
0
{
1820
0
  struct elf_x86_link_hash_table *htab;
1821
0
  const struct elf_backend_data *bed;
1822
1823
0
  unsigned int plt0_entry_size;
1824
0
  unsigned char func_info;
1825
0
  uint32_t fre_type;
1826
  /* The dynamic plt section for which .sframe stack trace information is being
1827
     created.  */
1828
0
  asection *dpltsec;
1829
1830
0
  int err = 0;
1831
1832
0
  sframe_encoder_ctx **ectx = NULL;
1833
0
  unsigned plt_entry_size = 0;
1834
0
  unsigned int num_pltn_fres = 0;
1835
0
  unsigned int num_pltn_entries = 0;
1836
0
  const sframe_frame_row_entry * const *pltn_fres;
1837
1838
0
  bed = get_elf_backend_data (output_bfd);
1839
0
  htab = elf_x86_hash_table (info, bed->target_id);
1840
  /* Whether SFrame stack trace info for plt0 is to be generated.  */
1841
0
  switch (plt_sec_type)
1842
0
    {
1843
0
    case SFRAME_PLT:
1844
0
  {
1845
0
    ectx = &htab->plt_cfe_ctx;
1846
0
    dpltsec = htab->elf.splt;
1847
1848
0
    plt0_entry_size
1849
0
      = htab->plt.has_plt0 ? htab->sframe_plt->plt0_entry_size : 0;
1850
0
    plt_entry_size = htab->sframe_plt->pltn_entry_size;
1851
0
    pltn_fres = htab->sframe_plt->pltn_fres;
1852
0
    num_pltn_fres = htab->sframe_plt->pltn_num_fres;
1853
0
    num_pltn_entries
1854
0
      = (dpltsec->size - plt0_entry_size) / plt_entry_size;
1855
1856
0
    break;
1857
0
  }
1858
0
    case SFRAME_PLT_SEC:
1859
0
  {
1860
0
    ectx = &htab->plt_second_cfe_ctx;
1861
0
    dpltsec = htab->plt_second;
1862
1863
0
    plt0_entry_size = 0;
1864
1865
0
    plt_entry_size = htab->sframe_plt->sec_pltn_entry_size;
1866
0
    pltn_fres = htab->sframe_plt->sec_pltn_fres;
1867
0
    num_pltn_fres = htab->sframe_plt->sec_pltn_num_fres;
1868
0
    num_pltn_entries = dpltsec->size / plt_entry_size;
1869
1870
0
    break;
1871
0
  }
1872
0
      case SFRAME_PLT_GOT:
1873
0
  {
1874
0
    ectx = &htab->plt_got_cfe_ctx;
1875
0
    dpltsec = htab->plt_got;
1876
1877
0
    plt0_entry_size = 0;
1878
1879
0
    plt_entry_size = htab->sframe_plt->plt_got_entry_size;
1880
0
    pltn_fres = htab->sframe_plt->plt_got_fres;
1881
0
    num_pltn_fres = htab->sframe_plt->plt_got_num_fres;
1882
0
    num_pltn_entries = dpltsec->size / plt_entry_size;
1883
1884
0
    break;
1885
0
  }
1886
1887
0
    default:
1888
      /* No other value is possible.  */
1889
0
      return false;
1890
0
      break;
1891
0
    }
1892
1893
0
  *ectx = sframe_encode (SFRAME_VERSION_2,
1894
0
       0,
1895
0
       SFRAME_ABI_AMD64_ENDIAN_LITTLE,
1896
0
       SFRAME_CFA_FIXED_FP_INVALID,
1897
0
       -8, /*  Fixed RA offset.  */
1898
0
       &err);
1899
1900
  /* FRE type is dependent on the size of the function.  */
1901
0
  fre_type = sframe_calc_fre_type (dpltsec->size);
1902
0
  func_info = sframe_fde_create_func_info (fre_type, SFRAME_FDE_TYPE_PCINC);
1903
1904
  /* Add SFrame FDE and the associated FREs for plt0 if plt0 has been
1905
     generated.  */
1906
0
  if (plt0_entry_size)
1907
0
    {
1908
      /* Add SFrame FDE for plt0, the function start address is updated later
1909
   at _bfd_elf_merge_section_sframe time.  */
1910
0
      sframe_encoder_add_funcdesc_v2 (*ectx,
1911
0
              0, /* func start addr.  */
1912
0
              plt0_entry_size,
1913
0
              func_info,
1914
0
              0,
1915
0
              0 /* Num FREs.  */);
1916
0
      sframe_frame_row_entry plt0_fre;
1917
0
      unsigned int num_plt0_fres = htab->sframe_plt->plt0_num_fres;
1918
0
      for (unsigned int j = 0; j < num_plt0_fres; j++)
1919
0
  {
1920
0
    plt0_fre = *(htab->sframe_plt->plt0_fres[j]);
1921
0
    sframe_encoder_add_fre (*ectx, 0, &plt0_fre);
1922
0
  }
1923
0
    }
1924
1925
1926
0
  if (num_pltn_entries)
1927
0
    {
1928
      /* pltn entries use an SFrame FDE of type
1929
   SFRAME_FDE_TYPE_PCMASK to exploit the repetitive
1930
   pattern of the instructions in these entries.  Using this SFrame FDE
1931
   type helps in keeping the SFrame stack trace info for pltn entries
1932
   compact.  */
1933
0
      func_info = sframe_fde_create_func_info (fre_type,
1934
0
                 SFRAME_FDE_TYPE_PCMASK);
1935
      /* Add the SFrame FDE for all PCs starting at the first pltn entry (hence,
1936
   function start address = plt0_entry_size.  As usual, this will be
1937
   updated later at _bfd_elf_merge_section_sframe, by when the
1938
   sections are relocated.  */
1939
0
      sframe_encoder_add_funcdesc_v2 (*ectx,
1940
0
              plt0_entry_size, /* func start addr.  */
1941
0
              dpltsec->size - plt0_entry_size,
1942
0
              func_info,
1943
0
              plt_entry_size,
1944
0
              0 /* Num FREs.  */);
1945
1946
0
      sframe_frame_row_entry pltn_fre;
1947
      /* Now add the FREs for pltn.  Simply adding the FREs suffices due
1948
   to the usage of SFRAME_FDE_TYPE_PCMASK above.  */
1949
0
      for (unsigned int j = 0; j < num_pltn_fres; j++)
1950
0
  {
1951
0
    unsigned int func_idx = plt0_entry_size ? 1 : 0;
1952
0
    pltn_fre = *(pltn_fres[j]);
1953
0
    sframe_encoder_add_fre (*ectx, func_idx, &pltn_fre);
1954
0
  }
1955
0
    }
1956
1957
0
  return true;
1958
0
}
1959
1960
/* Put contents of the .sframe section corresponding to the specified
1961
   PLT_SEC_TYPE.  */
1962
1963
static bool
1964
_bfd_x86_elf_write_sframe_plt (bfd *output_bfd,
1965
             struct bfd_link_info *info,
1966
             unsigned int plt_sec_type)
1967
0
{
1968
0
  struct elf_x86_link_hash_table *htab;
1969
0
  const struct elf_backend_data *bed;
1970
0
  sframe_encoder_ctx *ectx;
1971
0
  size_t sec_size;
1972
0
  asection *sec;
1973
0
  bfd *dynobj;
1974
1975
0
  int err = 0;
1976
1977
0
  bed = get_elf_backend_data (output_bfd);
1978
0
  htab = elf_x86_hash_table (info, bed->target_id);
1979
0
  dynobj = htab->elf.dynobj;
1980
1981
0
  switch (plt_sec_type)
1982
0
    {
1983
0
    case SFRAME_PLT:
1984
0
      ectx = htab->plt_cfe_ctx;
1985
0
      sec = htab->plt_sframe;
1986
0
      break;
1987
0
    case SFRAME_PLT_SEC:
1988
0
      ectx = htab->plt_second_cfe_ctx;
1989
0
      sec = htab->plt_second_sframe;
1990
0
      break;
1991
0
    case SFRAME_PLT_GOT:
1992
0
      ectx = htab->plt_got_cfe_ctx;
1993
0
      sec = htab->plt_got_sframe;
1994
0
      break;
1995
0
    default:
1996
      /* No other value is possible.  */
1997
0
      return false;
1998
0
      break;
1999
0
    }
2000
2001
0
  BFD_ASSERT (ectx);
2002
2003
0
  void *contents = sframe_encoder_write (ectx, &sec_size, &err);
2004
2005
0
  sec->size = (bfd_size_type) sec_size;
2006
0
  sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);
2007
0
  sec->alloced = 1;
2008
0
  memcpy (sec->contents, contents, sec_size);
2009
2010
0
  sframe_encoder_free (&ectx);
2011
2012
0
  return true;
2013
0
}
2014
2015
bool
2016
_bfd_elf_x86_size_relative_relocs (struct bfd_link_info *info,
2017
           bool *need_layout)
2018
0
{
2019
0
  struct elf_x86_link_hash_table *htab;
2020
0
  const struct elf_backend_data *bed;
2021
0
  bool is_x86_64;
2022
0
  bfd_size_type i, count, unaligned_count;
2023
0
  asection *sec, *srel;
2024
2025
  /* Do nothing for ld -r.  */
2026
0
  if (bfd_link_relocatable (info))
2027
0
    return true;
2028
2029
0
  bed = get_elf_backend_data (info->output_bfd);
2030
0
  htab = elf_x86_hash_table (info, bed->target_id);
2031
0
  if (htab == NULL)
2032
0
    return false;
2033
2034
0
  count = htab->relative_reloc.count;
2035
0
  unaligned_count = htab->unaligned_relative_reloc.count;
2036
0
  if (count == 0)
2037
0
    {
2038
0
      if (htab->generate_relative_reloc_pass == 0
2039
0
    && htab->elf.srelrdyn != NULL)
2040
0
  {
2041
    /* Remove the empty .relr.dyn sections now.  */
2042
0
    if (!bfd_is_abs_section (htab->elf.srelrdyn->output_section))
2043
0
      {
2044
0
        bfd_section_list_remove
2045
0
    (info->output_bfd, htab->elf.srelrdyn->output_section);
2046
0
        info->output_bfd->section_count--;
2047
0
      }
2048
0
    bfd_section_list_remove (htab->elf.srelrdyn->owner,
2049
0
           htab->elf.srelrdyn);
2050
0
    htab->elf.srelrdyn->owner->section_count--;
2051
0
  }
2052
0
      if (unaligned_count == 0)
2053
0
  {
2054
0
    htab->generate_relative_reloc_pass++;
2055
0
    return true;
2056
0
  }
2057
0
    }
2058
2059
0
  is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2060
2061
  /* Size relative relocations.  */
2062
0
  if (htab->generate_relative_reloc_pass)
2063
0
    {
2064
      /* Reset the regular relative relocation count.  */
2065
0
      for (i = 0; i < unaligned_count; i++)
2066
0
  {
2067
0
    sec = htab->unaligned_relative_reloc.data[i].sec;
2068
0
    srel = elf_section_data (sec)->sreloc;
2069
0
    srel->reloc_count = 0;
2070
0
  }
2071
0
    }
2072
0
  else
2073
0
    {
2074
      /* Remove the reserved space for compact relative relocations.  */
2075
0
      if (count)
2076
0
  {
2077
0
    asection *sgot = htab->elf.sgot;
2078
0
    asection *srelgot = htab->elf.srelgot;
2079
2080
0
    for (i = 0; i < count; i++)
2081
0
      {
2082
0
        sec = htab->relative_reloc.data[i].sec;
2083
0
        if (sec == sgot)
2084
0
    srel = srelgot;
2085
0
        else
2086
0
    srel = elf_section_data (sec)->sreloc;
2087
0
        srel->size -= htab->sizeof_reloc;
2088
0
      }
2089
0
  }
2090
0
    }
2091
2092
  /* Size unaligned relative relocations.  */
2093
0
  if (unaligned_count)
2094
0
    elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2095
0
             true, NULL);
2096
2097
0
  if (count)
2098
0
    {
2099
0
      elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2100
0
               false, NULL);
2101
2102
      /* Sort relative relocations by addresses.  We only need to
2103
   sort them in the first pass since the relative positions
2104
   won't change.  */
2105
0
      if (htab->generate_relative_reloc_pass == 0)
2106
0
  qsort (htab->relative_reloc.data, count,
2107
0
         sizeof (struct elf_x86_relative_reloc_record),
2108
0
         elf_x86_relative_reloc_compare);
2109
2110
0
      elf_x86_compute_dl_relr_bitmap (info, htab, need_layout);
2111
0
    }
2112
2113
0
  htab->generate_relative_reloc_pass++;
2114
2115
0
  return true;
2116
0
}
2117
2118
bool
2119
_bfd_elf_x86_finish_relative_relocs (struct bfd_link_info *info)
2120
0
{
2121
0
  struct elf_x86_link_hash_table *htab;
2122
0
  const struct elf_backend_data *bed;
2123
0
  Elf_Internal_Rela outrel;
2124
0
  bool is_x86_64;
2125
0
  bfd_size_type count;
2126
2127
  /* Do nothing for ld -r.  */
2128
0
  if (bfd_link_relocatable (info))
2129
0
    return true;
2130
2131
0
  bed = get_elf_backend_data (info->output_bfd);
2132
0
  htab = elf_x86_hash_table (info, bed->target_id);
2133
0
  if (htab == NULL)
2134
0
    return false;
2135
2136
0
  is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2137
2138
0
  outrel.r_info = htab->r_info (0, htab->relative_r_type);
2139
2140
0
  if (htab->unaligned_relative_reloc.count)
2141
0
    elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2142
0
             true, &outrel);
2143
2144
0
  count = htab->relative_reloc.count;
2145
0
  if (count)
2146
0
    {
2147
0
      elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2148
0
               false, &outrel);
2149
2150
0
      elf_x86_compute_dl_relr_bitmap (info, htab, NULL);
2151
2152
0
      elf_x86_write_dl_relr_bitmap (info, htab);
2153
0
    }
2154
2155
0
  return true;
2156
0
}
2157
2158
bool
2159
_bfd_elf_x86_valid_reloc_p (asection *input_section,
2160
          struct bfd_link_info *info,
2161
          struct elf_x86_link_hash_table *htab,
2162
          const Elf_Internal_Rela *rel,
2163
          struct elf_link_hash_entry *h,
2164
          Elf_Internal_Sym *sym,
2165
          Elf_Internal_Shdr *symtab_hdr,
2166
          bool *no_dynreloc_p)
2167
0
{
2168
0
  bool valid_p = true;
2169
2170
0
  *no_dynreloc_p = false;
2171
2172
  /* Check If relocation against non-preemptible absolute symbol is
2173
     valid in PIC.  FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
2174
     it may call _bfd_elf_link_hide_sym_by_version and result in
2175
     ld-elfvers/ vers21 test failure.  */
2176
0
  if (bfd_link_pic (info)
2177
0
      && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
2178
0
    {
2179
0
      const struct elf_backend_data *bed;
2180
0
      unsigned int r_type;
2181
0
      Elf_Internal_Rela irel;
2182
2183
      /* Skip non-absolute symbol.  */
2184
0
      if (h)
2185
0
  {
2186
0
    if (!ABS_SYMBOL_P (h))
2187
0
      return valid_p;
2188
0
  }
2189
0
      else if (sym->st_shndx != SHN_ABS)
2190
0
  return valid_p;
2191
2192
0
      bed = get_elf_backend_data (input_section->owner);
2193
0
      r_type = ELF32_R_TYPE (rel->r_info);
2194
0
      irel = *rel;
2195
2196
      /* Only allow relocations against absolute symbol, which can be
2197
   resolved as absolute value + addend.  GOTPCREL and GOT32
2198
   relocations are allowed since absolute value + addend is
2199
   stored in the GOT slot.  */
2200
0
      if (bed->target_id == X86_64_ELF_DATA)
2201
0
  {
2202
0
    r_type &= ~R_X86_64_converted_reloc_bit;
2203
0
    valid_p = (r_type == R_X86_64_64
2204
0
         || r_type == R_X86_64_32
2205
0
         || r_type == R_X86_64_32S
2206
0
         || r_type == R_X86_64_16
2207
0
         || r_type == R_X86_64_8
2208
0
         || r_type == R_X86_64_GOTPCREL
2209
0
         || r_type == R_X86_64_GOTPCRELX
2210
0
         || r_type == R_X86_64_REX_GOTPCRELX);
2211
0
    if (!valid_p)
2212
0
      {
2213
0
        unsigned int r_symndx = htab->r_sym (rel->r_info);
2214
0
        irel.r_info = htab->r_info (r_symndx, r_type);
2215
0
      }
2216
0
  }
2217
0
      else
2218
0
  valid_p = (r_type == R_386_32
2219
0
       || r_type == R_386_16
2220
0
       || r_type == R_386_8
2221
0
       || r_type == R_386_GOT32
2222
0
       || r_type == R_386_GOT32X);
2223
2224
0
      if (valid_p)
2225
0
  *no_dynreloc_p = true;
2226
0
      else
2227
0
  {
2228
0
    const char *name;
2229
0
    arelent internal_reloc;
2230
2231
0
    if (!bed->elf_info_to_howto (input_section->owner,
2232
0
               &internal_reloc, &irel)
2233
0
        || internal_reloc.howto == NULL)
2234
0
      abort ();
2235
2236
0
    if (h)
2237
0
      name = h->root.root.string;
2238
0
    else
2239
0
      name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
2240
0
             sym, NULL);
2241
0
    info->callbacks->fatal
2242
      /* xgettext:c-format */
2243
0
      (_("%P: %pB: relocation %s against absolute symbol "
2244
0
         "`%s' in section `%pA' is disallowed\n"),
2245
0
       input_section->owner, internal_reloc.howto->name, name,
2246
0
       input_section);
2247
0
    bfd_set_error (bfd_error_bad_value);
2248
0
  }
2249
0
    }
2250
2251
0
  return valid_p;
2252
0
}
2253
2254
/* Set the sizes of the dynamic sections.  */
2255
2256
bool
2257
_bfd_x86_elf_late_size_sections (bfd *output_bfd,
2258
            struct bfd_link_info *info)
2259
0
{
2260
0
  struct elf_x86_link_hash_table *htab;
2261
0
  bfd *dynobj;
2262
0
  asection *s;
2263
0
  bool relocs;
2264
0
  bfd *ibfd;
2265
0
  const struct elf_backend_data *bed
2266
0
    = get_elf_backend_data (output_bfd);
2267
2268
0
  htab = elf_x86_hash_table (info, bed->target_id);
2269
0
  if (htab == NULL)
2270
0
    return false;
2271
0
  dynobj = htab->elf.dynobj;
2272
0
  if (dynobj == NULL)
2273
0
    return true;
2274
2275
  /* Set up .got offsets for local syms, and space for local dynamic
2276
     relocs.  */
2277
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2278
0
    {
2279
0
      bfd_signed_vma *local_got;
2280
0
      bfd_signed_vma *end_local_got;
2281
0
      char *local_tls_type;
2282
0
      bfd_vma *local_tlsdesc_gotent;
2283
0
      bfd_size_type locsymcount;
2284
0
      Elf_Internal_Shdr *symtab_hdr;
2285
0
      asection *srel;
2286
2287
0
      if (! is_x86_elf (ibfd, htab))
2288
0
  continue;
2289
2290
0
      for (s = ibfd->sections; s != NULL; s = s->next)
2291
0
  {
2292
0
    struct elf_dyn_relocs *p;
2293
2294
0
    for (p = ((struct elf_dyn_relocs *)
2295
0
         elf_section_data (s)->local_dynrel);
2296
0
         p != NULL;
2297
0
         p = p->next)
2298
0
      {
2299
0
        if (!bfd_is_abs_section (p->sec)
2300
0
      && bfd_is_abs_section (p->sec->output_section))
2301
0
    {
2302
      /* Input section has been discarded, either because
2303
         it is a copy of a linkonce section or due to
2304
         linker script /DISCARD/, so we'll be discarding
2305
         the relocs too.  */
2306
0
    }
2307
0
        else if (htab->elf.target_os == is_vxworks
2308
0
           && strcmp (p->sec->output_section->name,
2309
0
          ".tls_vars") == 0)
2310
0
    {
2311
      /* Relocations in vxworks .tls_vars sections are
2312
         handled specially by the loader.  */
2313
0
    }
2314
0
        else if (p->count != 0)
2315
0
    {
2316
0
      srel = elf_section_data (p->sec)->sreloc;
2317
0
      srel->size += p->count * htab->sizeof_reloc;
2318
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0
2319
0
          && (info->flags & DF_TEXTREL) == 0)
2320
0
        {
2321
0
          info->flags |= DF_TEXTREL;
2322
0
          if (bfd_link_textrel_check (info))
2323
      /* xgettext:c-format */
2324
0
      info->callbacks->einfo
2325
0
        (_("%P: %pB: warning: relocation "
2326
0
           "in read-only section `%pA'\n"),
2327
0
         p->sec->owner, p->sec);
2328
0
        }
2329
0
    }
2330
0
      }
2331
0
  }
2332
2333
0
      local_got = elf_local_got_refcounts (ibfd);
2334
0
      if (!local_got)
2335
0
  continue;
2336
2337
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
2338
0
      locsymcount = symtab_hdr->sh_info;
2339
0
      end_local_got = local_got + locsymcount;
2340
0
      local_tls_type = elf_x86_local_got_tls_type (ibfd);
2341
0
      local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
2342
0
      s = htab->elf.sgot;
2343
0
      srel = htab->elf.srelgot;
2344
0
      for (; local_got < end_local_got;
2345
0
     ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2346
0
  {
2347
0
    *local_tlsdesc_gotent = (bfd_vma) -1;
2348
0
    if (*local_got > 0)
2349
0
      {
2350
0
        if (GOT_TLS_GDESC_P (*local_tls_type))
2351
0
    {
2352
0
      *local_tlsdesc_gotent = htab->elf.sgotplt->size
2353
0
        - elf_x86_compute_jump_table_size (htab);
2354
0
      htab->elf.sgotplt->size += 2 * htab->got_entry_size;
2355
0
      *local_got = (bfd_vma) -2;
2356
0
    }
2357
0
        if (! GOT_TLS_GDESC_P (*local_tls_type)
2358
0
      || GOT_TLS_GD_P (*local_tls_type))
2359
0
    {
2360
0
      *local_got = s->size;
2361
0
      s->size += htab->got_entry_size;
2362
0
      if (GOT_TLS_GD_P (*local_tls_type)
2363
0
          || *local_tls_type == GOT_TLS_IE_BOTH)
2364
0
        s->size += htab->got_entry_size;
2365
0
    }
2366
0
        if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
2367
0
      || GOT_TLS_GD_ANY_P (*local_tls_type)
2368
0
      || (*local_tls_type & GOT_TLS_IE))
2369
0
    {
2370
0
      if (*local_tls_type == GOT_TLS_IE_BOTH)
2371
0
        srel->size += 2 * htab->sizeof_reloc;
2372
0
      else if (GOT_TLS_GD_P (*local_tls_type)
2373
0
         || ! GOT_TLS_GDESC_P (*local_tls_type))
2374
0
        srel->size += htab->sizeof_reloc;
2375
0
      if (GOT_TLS_GDESC_P (*local_tls_type))
2376
0
        {
2377
0
          htab->elf.srelplt->size += htab->sizeof_reloc;
2378
0
          if (bed->target_id == X86_64_ELF_DATA)
2379
0
      htab->elf.tlsdesc_plt = (bfd_vma) -1;
2380
0
        }
2381
0
    }
2382
0
      }
2383
0
    else
2384
0
      *local_got = (bfd_vma) -1;
2385
0
  }
2386
0
    }
2387
2388
0
  if (htab->tls_ld_or_ldm_got.refcount > 0)
2389
0
    {
2390
      /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2391
   or R_X86_64_TLSLD relocs.  */
2392
0
      htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
2393
0
      htab->elf.sgot->size += 2 * htab->got_entry_size;
2394
0
      htab->elf.srelgot->size += htab->sizeof_reloc;
2395
0
    }
2396
0
  else
2397
0
    htab->tls_ld_or_ldm_got.offset = -1;
2398
2399
  /* Allocate global sym .plt and .got entries, and space for global
2400
     sym dynamic relocs.  */
2401
0
  elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
2402
0
        info);
2403
2404
  /* Allocate .plt and .got entries, and space for local symbols.  */
2405
0
  htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
2406
0
     info);
2407
2408
  /* For every jump slot reserved in the sgotplt, reloc_count is
2409
     incremented.  However, when we reserve space for TLS descriptors,
2410
     it's not incremented, so in order to compute the space reserved
2411
     for them, it suffices to multiply the reloc count by the jump
2412
     slot size.
2413
2414
     PR ld/13302: We start next_irelative_index at the end of .rela.plt
2415
     so that R_{386,X86_64}_IRELATIVE entries come last.  */
2416
0
  if (htab->elf.srelplt)
2417
0
    {
2418
0
      htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2419
0
      htab->sgotplt_jump_table_size
2420
0
  = elf_x86_compute_jump_table_size (htab);
2421
0
      htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2422
0
    }
2423
0
  else if (htab->elf.irelplt)
2424
0
    htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2425
2426
0
  if (htab->elf.tlsdesc_plt)
2427
0
    {
2428
      /* NB: tlsdesc_plt is set only for x86-64.  If we're not using
2429
   lazy TLS relocations, don't generate the PLT and GOT entries
2430
   they require.  */
2431
0
      if ((info->flags & DF_BIND_NOW))
2432
0
  htab->elf.tlsdesc_plt = 0;
2433
0
      else
2434
0
  {
2435
0
    htab->elf.tlsdesc_got = htab->elf.sgot->size;
2436
0
    htab->elf.sgot->size += htab->got_entry_size;
2437
    /* Reserve room for the initial entry.
2438
       FIXME: we could probably do away with it in this case.  */
2439
0
    if (htab->elf.splt->size == 0)
2440
0
      htab->elf.splt->size = htab->plt.plt_entry_size;
2441
0
    htab->elf.tlsdesc_plt = htab->elf.splt->size;
2442
0
    htab->elf.splt->size += htab->plt.plt_entry_size;
2443
0
  }
2444
0
    }
2445
2446
0
  if (htab->elf.sgotplt)
2447
0
    {
2448
      /* Don't allocate .got.plt section if there are no GOT nor PLT
2449
   entries and there is no reference to _GLOBAL_OFFSET_TABLE_.  */
2450
0
      if ((htab->elf.hgot == NULL
2451
0
     || !htab->got_referenced)
2452
0
    && (htab->elf.sgotplt->size == bed->got_header_size)
2453
0
    && (htab->elf.splt == NULL
2454
0
        || htab->elf.splt->size == 0)
2455
0
    && (htab->elf.sgot == NULL
2456
0
        || htab->elf.sgot->size == 0)
2457
0
    && (htab->elf.iplt == NULL
2458
0
        || htab->elf.iplt->size == 0)
2459
0
    && (htab->elf.igotplt == NULL
2460
0
        || htab->elf.igotplt->size == 0))
2461
0
  {
2462
0
    htab->elf.sgotplt->size = 0;
2463
    /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
2464
       isn't used.  */
2465
0
    if (htab->elf.hgot != NULL
2466
0
        && htab->elf.target_os != is_solaris)
2467
0
      {
2468
        /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
2469
     table. */
2470
0
        htab->elf.hgot->root.type = bfd_link_hash_undefined;
2471
0
        htab->elf.hgot->root.u.undef.abfd
2472
0
    = htab->elf.hgot->root.u.def.section->owner;
2473
0
        htab->elf.hgot->root.linker_def = 0;
2474
0
        htab->elf.hgot->ref_regular = 0;
2475
0
        htab->elf.hgot->def_regular = 0;
2476
0
      }
2477
0
  }
2478
0
    }
2479
2480
0
  if (_bfd_elf_eh_frame_present (info))
2481
0
    {
2482
0
      if (htab->plt_eh_frame != NULL
2483
0
    && htab->elf.splt != NULL
2484
0
    && htab->elf.splt->size != 0
2485
0
    && !bfd_is_abs_section (htab->elf.splt->output_section))
2486
0
  htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
2487
2488
0
      if (htab->plt_got_eh_frame != NULL
2489
0
    && htab->plt_got != NULL
2490
0
    && htab->plt_got->size != 0
2491
0
    && !bfd_is_abs_section (htab->plt_got->output_section))
2492
0
  htab->plt_got_eh_frame->size
2493
0
    = htab->non_lazy_plt->eh_frame_plt_size;
2494
2495
      /* Unwind info for the second PLT and .plt.got sections are
2496
   identical.  */
2497
0
      if (htab->plt_second_eh_frame != NULL
2498
0
    && htab->plt_second != NULL
2499
0
    && htab->plt_second->size != 0
2500
0
    && !bfd_is_abs_section (htab->plt_second->output_section))
2501
0
  htab->plt_second_eh_frame->size
2502
0
    = htab->non_lazy_plt->eh_frame_plt_size;
2503
0
    }
2504
2505
  /* No need to size the .sframe section explicitly because the write-out
2506
     mechanism is different.  Simply prep up the FDE/FRE for the
2507
     .plt section.  */
2508
0
  if (_bfd_elf_sframe_present (info))
2509
0
    {
2510
0
      if (htab->plt_sframe != NULL
2511
0
    && htab->elf.splt != NULL
2512
0
    && htab->elf.splt->size != 0
2513
0
    && !bfd_is_abs_section (htab->elf.splt->output_section))
2514
0
  {
2515
0
    _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT);
2516
    /* FIXME - Dirty Hack.  Set the size to something non-zero for now,
2517
       so that the section does not get stripped out below.  The precise
2518
       size of this section is known only when the contents are
2519
       serialized in _bfd_x86_elf_write_sframe_plt.  */
2520
0
    htab->plt_sframe->size = sizeof (sframe_header) + 1;
2521
0
  }
2522
2523
0
      if (htab->plt_got_sframe != NULL
2524
0
    && htab->plt_got != NULL
2525
0
    && htab->plt_got->size != 0
2526
0
    && !bfd_is_abs_section (htab->plt_got->output_section))
2527
0
  {
2528
0
    _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT_GOT);
2529
    /* FIXME - Dirty Hack.  Set the size to something non-zero for now,
2530
       so that the section does not get stripped out below.  The precise
2531
       size of this section is known only when the contents are
2532
       serialized in _bfd_x86_elf_write_sframe_plt.  */
2533
0
    htab->plt_got_sframe->size = sizeof (sframe_header) + 1;
2534
0
  }
2535
2536
0
      if (htab->plt_second_sframe != NULL
2537
0
    && htab->plt_second != NULL
2538
0
    && htab->plt_second->size != 0
2539
0
    && !bfd_is_abs_section (htab->plt_second->output_section))
2540
0
  {
2541
    /* SFrame stack trace info for the second PLT.  */
2542
0
    _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT_SEC);
2543
    /* FIXME - Dirty Hack.  Set the size to something non-zero for now,
2544
       so that the section does not get stripped out below.  The precise
2545
       size of this section is known only when the contents are
2546
       serialized in _bfd_x86_elf_write_sframe_plt.  */
2547
0
    htab->plt_second_sframe->size = sizeof (sframe_header) + 1;
2548
0
  }
2549
0
    }
2550
2551
0
  asection *resolved_plt = NULL;
2552
2553
0
  if (htab->params->mark_plt && htab->elf.dynamic_sections_created)
2554
0
    {
2555
0
      if (htab->plt_second != NULL)
2556
0
  resolved_plt = htab->plt_second;
2557
0
      else
2558
0
  resolved_plt = htab->elf.splt;
2559
2560
0
      if (resolved_plt != NULL && resolved_plt->size == 0)
2561
0
  resolved_plt = NULL;
2562
0
    }
2563
2564
  /* We now have determined the sizes of the various dynamic sections.
2565
     Allocate memory for them.  */
2566
0
  relocs = false;
2567
0
  for (s = dynobj->sections; s != NULL; s = s->next)
2568
0
    {
2569
0
      bool strip_section = true;
2570
2571
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
2572
0
  continue;
2573
2574
      /* The .relr.dyn section for compact relative relocation will
2575
   be filled later.  */
2576
0
      if (s == htab->elf.srelrdyn)
2577
0
  continue;
2578
2579
0
      if (s == htab->elf.splt
2580
0
    || s == htab->elf.sgot)
2581
0
  {
2582
    /* Strip this section if we don't need it; see the
2583
       comment below.  */
2584
    /* We'd like to strip these sections if they aren't needed, but if
2585
       we've exported dynamic symbols from them we must leave them.
2586
       It's too late to tell BFD to get rid of the symbols.  */
2587
2588
0
    if (htab->elf.hplt != NULL)
2589
0
      strip_section = false;
2590
0
  }
2591
0
      else if (s == htab->elf.sgotplt
2592
0
         || s == htab->elf.iplt
2593
0
         || s == htab->elf.igotplt
2594
0
         || s == htab->plt_second
2595
0
         || s == htab->plt_got
2596
0
         || s == htab->plt_eh_frame
2597
0
         || s == htab->plt_got_eh_frame
2598
0
         || s == htab->plt_second_eh_frame
2599
0
         || s == htab->plt_sframe
2600
0
         || s == htab->plt_second_sframe
2601
0
         || s == htab->plt_got_sframe
2602
0
         || s == htab->elf.sdynbss
2603
0
         || s == htab->elf.sdynrelro)
2604
0
  {
2605
    /* Strip these too.  */
2606
0
  }
2607
0
      else if (htab->is_reloc_section (bfd_section_name (s)))
2608
0
  {
2609
0
    if (s->size != 0
2610
0
        && s != htab->elf.srelplt
2611
0
        && s != htab->srelplt2)
2612
0
      relocs = true;
2613
2614
    /* We use the reloc_count field as a counter if we need
2615
       to copy relocs into the output file.  */
2616
0
    if (s != htab->elf.srelplt)
2617
0
      s->reloc_count = 0;
2618
0
  }
2619
0
      else
2620
0
  {
2621
    /* It's not one of our sections, so don't allocate space.  */
2622
0
    continue;
2623
0
  }
2624
2625
0
      if (s->size == 0)
2626
0
  {
2627
    /* If we don't need this section, strip it from the
2628
       output file.  This is mostly to handle .rel.bss and
2629
       .rel.plt.  We must create both sections in
2630
       create_dynamic_sections, because they must be created
2631
       before the linker maps input sections to output
2632
       sections.  The linker does that before
2633
       adjust_dynamic_symbol is called, and it is that
2634
       function which decides whether anything needs to go
2635
       into these sections.  */
2636
0
    if (strip_section)
2637
0
      s->flags |= SEC_EXCLUDE;
2638
0
    continue;
2639
0
  }
2640
2641
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
2642
0
  continue;
2643
2644
      /* Skip allocating contents for .sframe section as it is written
2645
   out differently.  See below.  */
2646
0
      if ((s == htab->plt_sframe) || (s == htab->plt_second_sframe)
2647
0
    || (s == htab->plt_got_sframe))
2648
0
  continue;
2649
2650
      /* NB: Initially, the iplt section has minimal alignment to
2651
   avoid moving dot of the following section backwards when
2652
   it is empty.  Update its section alignment now since it
2653
   is non-empty.  */
2654
0
      if (s == htab->elf.iplt
2655
0
    && !bfd_link_align_section (s, htab->plt.iplt_alignment))
2656
0
  abort ();
2657
2658
      /* Allocate memory for the section contents.  We use bfd_zalloc
2659
   here in case unused entries are not reclaimed before the
2660
   section's contents are written out.  This should not happen,
2661
   but this way if it does, we get a R_386_NONE or R_X86_64_NONE
2662
   reloc instead of garbage.  */
2663
0
      s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
2664
0
      if (s->contents == NULL)
2665
0
  return false;
2666
0
      s->alloced = 1;
2667
0
    }
2668
2669
0
  if (htab->plt_eh_frame != NULL
2670
0
      && htab->plt_eh_frame->contents != NULL)
2671
0
    {
2672
0
      memcpy (htab->plt_eh_frame->contents,
2673
0
        htab->plt.eh_frame_plt,
2674
0
        htab->plt_eh_frame->size);
2675
0
      bfd_put_32 (dynobj, htab->elf.splt->size,
2676
0
      htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2677
0
    }
2678
2679
0
  if (htab->plt_got_eh_frame != NULL
2680
0
      && htab->plt_got_eh_frame->contents != NULL)
2681
0
    {
2682
0
      memcpy (htab->plt_got_eh_frame->contents,
2683
0
        htab->non_lazy_plt->eh_frame_plt,
2684
0
        htab->plt_got_eh_frame->size);
2685
0
      bfd_put_32 (dynobj, htab->plt_got->size,
2686
0
      (htab->plt_got_eh_frame->contents
2687
0
       + PLT_FDE_LEN_OFFSET));
2688
0
    }
2689
2690
0
  if (htab->plt_second_eh_frame != NULL
2691
0
      && htab->plt_second_eh_frame->contents != NULL)
2692
0
    {
2693
0
      memcpy (htab->plt_second_eh_frame->contents,
2694
0
        htab->non_lazy_plt->eh_frame_plt,
2695
0
        htab->plt_second_eh_frame->size);
2696
0
      bfd_put_32 (dynobj, htab->plt_second->size,
2697
0
      (htab->plt_second_eh_frame->contents
2698
0
       + PLT_FDE_LEN_OFFSET));
2699
0
    }
2700
2701
0
  if (_bfd_elf_sframe_present (info))
2702
0
    {
2703
0
      if (htab->plt_sframe != NULL
2704
0
    && htab->elf.splt != NULL
2705
0
    && htab->elf.splt->size != 0
2706
0
    && htab->plt_sframe->contents == NULL)
2707
0
  _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT);
2708
2709
0
      if (htab->plt_second_sframe != NULL
2710
0
    && htab->plt_second != NULL
2711
0
    && htab->plt_second->size != 0
2712
0
    && htab->plt_second_sframe->contents == NULL)
2713
0
  _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT_SEC);
2714
2715
0
      if (htab->plt_got_sframe != NULL
2716
0
    && htab->plt_got != NULL
2717
0
    && htab->plt_got->size != 0
2718
0
    && htab->plt_got_sframe->contents == NULL)
2719
0
  _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT_GOT);
2720
0
    }
2721
2722
0
  if (resolved_plt != NULL
2723
0
      && (!_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLT, 0)
2724
0
    || !_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLTSZ, 0)
2725
0
    || !_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLTENT, 0)))
2726
0
    return false;
2727
2728
0
  return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
2729
0
              relocs);
2730
0
}
2731
2732
/* Finish up the x86 dynamic sections.  */
2733
2734
struct elf_x86_link_hash_table *
2735
_bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
2736
              struct bfd_link_info *info)
2737
0
{
2738
0
  struct elf_x86_link_hash_table *htab;
2739
0
  const struct elf_backend_data *bed;
2740
0
  bfd *dynobj;
2741
0
  asection *sdyn;
2742
0
  bfd_byte *dyncon, *dynconend;
2743
0
  bfd_size_type sizeof_dyn;
2744
2745
0
  bed = get_elf_backend_data (output_bfd);
2746
0
  htab = elf_x86_hash_table (info, bed->target_id);
2747
0
  if (htab == NULL)
2748
0
    return htab;
2749
2750
0
  dynobj = htab->elf.dynobj;
2751
0
  sdyn = htab->elf.dynamic;
2752
2753
  /* GOT is always created in setup_gnu_properties.  But it may not be
2754
     needed.  .got.plt section may be needed for static IFUNC.  */
2755
0
  if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
2756
0
    {
2757
0
      bfd_vma dynamic_addr;
2758
2759
0
      if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
2760
0
  {
2761
0
    _bfd_error_handler
2762
0
      (_("discarded output section: `%pA'"), htab->elf.sgotplt);
2763
0
    return NULL;
2764
0
  }
2765
2766
0
      elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
2767
0
  = htab->got_entry_size;
2768
2769
0
      dynamic_addr = (sdyn == NULL
2770
0
          ? (bfd_vma) 0
2771
0
          : sdyn->output_section->vma + sdyn->output_offset);
2772
2773
      /* Set the first entry in the global offset table to the address
2774
   of the dynamic section.  Write GOT[1] and GOT[2], needed for
2775
   the dynamic linker.  */
2776
0
      if (htab->got_entry_size == 8)
2777
0
  {
2778
0
    bfd_put_64 (output_bfd, dynamic_addr,
2779
0
          htab->elf.sgotplt->contents);
2780
0
    bfd_put_64 (output_bfd, (bfd_vma) 0,
2781
0
          htab->elf.sgotplt->contents + 8);
2782
0
    bfd_put_64 (output_bfd, (bfd_vma) 0,
2783
0
          htab->elf.sgotplt->contents + 8*2);
2784
0
  }
2785
0
      else
2786
0
  {
2787
0
    bfd_put_32 (output_bfd, dynamic_addr,
2788
0
          htab->elf.sgotplt->contents);
2789
0
    bfd_put_32 (output_bfd, 0,
2790
0
          htab->elf.sgotplt->contents + 4);
2791
0
    bfd_put_32 (output_bfd, 0,
2792
0
          htab->elf.sgotplt->contents + 4*2);
2793
0
  }
2794
0
    }
2795
2796
0
  if (!htab->elf.dynamic_sections_created)
2797
0
    return htab;
2798
2799
0
  if (sdyn == NULL || htab->elf.sgot == NULL)
2800
0
    abort ();
2801
2802
0
  asection *resolved_plt;
2803
0
  if (htab->plt_second != NULL)
2804
0
    resolved_plt = htab->plt_second;
2805
0
  else
2806
0
    resolved_plt = htab->elf.splt;
2807
2808
0
  sizeof_dyn = bed->s->sizeof_dyn;
2809
0
  dyncon = sdyn->contents;
2810
0
  dynconend = sdyn->contents + sdyn->size;
2811
0
  for (; dyncon < dynconend; dyncon += sizeof_dyn)
2812
0
    {
2813
0
      Elf_Internal_Dyn dyn;
2814
0
      asection *s;
2815
2816
0
      (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
2817
2818
0
      switch (dyn.d_tag)
2819
0
  {
2820
0
  default:
2821
0
    if (htab->elf.target_os == is_vxworks
2822
0
        && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
2823
0
      break;
2824
0
    continue;
2825
2826
0
  case DT_PLTGOT:
2827
0
    s = htab->elf.sgotplt;
2828
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2829
0
    break;
2830
2831
0
  case DT_JMPREL:
2832
0
    s = htab->elf.srelplt;
2833
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2834
0
    break;
2835
2836
0
  case DT_PLTRELSZ:
2837
0
    s = htab->elf.srelplt;
2838
0
    dyn.d_un.d_val = s->size;
2839
0
    break;
2840
2841
0
  case DT_TLSDESC_PLT:
2842
0
    s = htab->elf.splt;
2843
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2844
0
      + htab->elf.tlsdesc_plt;
2845
0
    break;
2846
2847
0
  case DT_TLSDESC_GOT:
2848
0
    s = htab->elf.sgot;
2849
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2850
0
      + htab->elf.tlsdesc_got;
2851
0
    break;
2852
2853
0
  case DT_X86_64_PLT:
2854
0
    s = resolved_plt->output_section;
2855
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2856
0
    break;
2857
2858
0
  case DT_X86_64_PLTSZ:
2859
0
    dyn.d_un.d_val = resolved_plt->size;
2860
0
    break;
2861
2862
0
  case DT_X86_64_PLTENT:
2863
0
    dyn.d_un.d_ptr = htab->plt.plt_entry_size;
2864
0
    break;
2865
0
  }
2866
2867
0
      (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
2868
0
    }
2869
2870
0
  if (htab->plt_got != NULL && htab->plt_got->size > 0)
2871
0
    elf_section_data (htab->plt_got->output_section)
2872
0
      ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2873
2874
0
  if (htab->plt_second != NULL && htab->plt_second->size > 0)
2875
0
    elf_section_data (htab->plt_second->output_section)
2876
0
      ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2877
2878
  /* Adjust .eh_frame for .plt section.  */
2879
0
  if (htab->plt_eh_frame != NULL
2880
0
      && htab->plt_eh_frame->contents != NULL)
2881
0
    {
2882
0
      if (htab->elf.splt != NULL
2883
0
    && htab->elf.splt->size != 0
2884
0
    && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2885
0
    && htab->elf.splt->output_section != NULL
2886
0
    && htab->plt_eh_frame->output_section != NULL)
2887
0
  {
2888
0
    bfd_vma plt_start = htab->elf.splt->output_section->vma;
2889
0
    bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
2890
0
           + htab->plt_eh_frame->output_offset
2891
0
           + PLT_FDE_START_OFFSET;
2892
0
    bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2893
0
           htab->plt_eh_frame->contents
2894
0
           + PLT_FDE_START_OFFSET);
2895
0
  }
2896
2897
0
      if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2898
0
  {
2899
0
    if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2900
0
             htab->plt_eh_frame,
2901
0
             htab->plt_eh_frame->contents))
2902
0
      return NULL;
2903
0
  }
2904
0
    }
2905
2906
  /* Adjust .eh_frame for .plt.got section.  */
2907
0
  if (htab->plt_got_eh_frame != NULL
2908
0
      && htab->plt_got_eh_frame->contents != NULL)
2909
0
    {
2910
0
      if (htab->plt_got != NULL
2911
0
    && htab->plt_got->size != 0
2912
0
    && (htab->plt_got->flags & SEC_EXCLUDE) == 0
2913
0
    && htab->plt_got->output_section != NULL
2914
0
    && htab->plt_got_eh_frame->output_section != NULL)
2915
0
  {
2916
0
    bfd_vma plt_start = htab->plt_got->output_section->vma;
2917
0
    bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
2918
0
           + htab->plt_got_eh_frame->output_offset
2919
0
           + PLT_FDE_START_OFFSET;
2920
0
    bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2921
0
           htab->plt_got_eh_frame->contents
2922
0
           + PLT_FDE_START_OFFSET);
2923
0
  }
2924
0
      if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2925
0
  {
2926
0
    if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2927
0
             htab->plt_got_eh_frame,
2928
0
             htab->plt_got_eh_frame->contents))
2929
0
      return NULL;
2930
0
  }
2931
0
    }
2932
2933
  /* Adjust .eh_frame for the second PLT section.  */
2934
0
  if (htab->plt_second_eh_frame != NULL
2935
0
      && htab->plt_second_eh_frame->contents != NULL)
2936
0
    {
2937
0
      if (htab->plt_second != NULL
2938
0
    && htab->plt_second->size != 0
2939
0
    && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2940
0
    && htab->plt_second->output_section != NULL
2941
0
    && htab->plt_second_eh_frame->output_section != NULL)
2942
0
  {
2943
0
    bfd_vma plt_start = htab->plt_second->output_section->vma;
2944
0
    bfd_vma eh_frame_start
2945
0
      = (htab->plt_second_eh_frame->output_section->vma
2946
0
         + htab->plt_second_eh_frame->output_offset
2947
0
         + PLT_FDE_START_OFFSET);
2948
0
    bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2949
0
           htab->plt_second_eh_frame->contents
2950
0
           + PLT_FDE_START_OFFSET);
2951
0
  }
2952
0
      if (htab->plt_second_eh_frame->sec_info_type
2953
0
    == SEC_INFO_TYPE_EH_FRAME)
2954
0
  {
2955
0
    if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2956
0
             htab->plt_second_eh_frame,
2957
0
             htab->plt_second_eh_frame->contents))
2958
0
      return NULL;
2959
0
  }
2960
0
    }
2961
2962
  /* Make any adjustment if necessary and merge .sframe section to
2963
     create the final .sframe section for output_bfd.  */
2964
0
  if (htab->plt_sframe != NULL
2965
0
      && htab->plt_sframe->contents != NULL)
2966
0
    {
2967
0
      if (htab->elf.splt != NULL
2968
0
    && htab->elf.splt->size != 0
2969
0
    && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2970
0
    && htab->elf.splt->output_section != NULL
2971
0
    && htab->plt_sframe->output_section != NULL)
2972
0
  {
2973
0
    bfd_vma plt_start = htab->elf.splt->output_section->vma;
2974
0
    bfd_vma sframe_start = htab->plt_sframe->output_section->vma
2975
0
           + htab->plt_sframe->output_offset
2976
0
           + PLT_SFRAME_FDE_START_OFFSET;
2977
#if 0 /* FIXME Testing only. Remove before review.  */
2978
    bfd_vma test_value = (plt_start - sframe_start)
2979
      + htab->plt_sframe->output_section->vma
2980
      + htab->plt_sframe->output_offset
2981
      + PLT_SFRAME_FDE_START_OFFSET;
2982
    bfd_put_signed_32 (dynobj, test_value,
2983
#endif
2984
0
    bfd_put_signed_32 (dynobj, plt_start - sframe_start,
2985
0
           htab->plt_sframe->contents
2986
0
           + PLT_SFRAME_FDE_START_OFFSET);
2987
0
  }
2988
0
      if (htab->plt_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
2989
0
  {
2990
0
    if (! _bfd_elf_merge_section_sframe (output_bfd, info,
2991
0
                 htab->plt_sframe,
2992
0
                 htab->plt_sframe->contents))
2993
0
      return NULL;
2994
0
  }
2995
0
    }
2996
2997
0
  if (htab->plt_second_sframe != NULL
2998
0
      && htab->plt_second_sframe->contents != NULL)
2999
0
    {
3000
0
      if (htab->plt_second != NULL
3001
0
    && htab->plt_second->size != 0
3002
0
    && (htab->plt_second->flags & SEC_EXCLUDE) == 0
3003
0
    && htab->plt_second->output_section != NULL
3004
0
    && htab->plt_second_sframe->output_section != NULL)
3005
0
  {
3006
0
    bfd_vma plt_start = htab->plt_second->output_section->vma;
3007
0
    bfd_vma sframe_start
3008
0
      = (htab->plt_second_sframe->output_section->vma
3009
0
         + htab->plt_second_sframe->output_offset
3010
0
         + PLT_SFRAME_FDE_START_OFFSET);
3011
#if 0 /* FIXME Testing only. Remove before review.  */
3012
    bfd_vma test_value = (plt_start - sframe_start)
3013
      + htab->plt_second_sframe->output_section->vma
3014
      + htab->plt_second_sframe->output_offset
3015
      + PLT_SFRAME_FDE_START_OFFSET;
3016
    bfd_put_signed_32 (dynobj, test_value,
3017
#endif
3018
0
    bfd_put_signed_32 (dynobj, plt_start - sframe_start,
3019
0
           htab->plt_second_sframe->contents
3020
0
           + PLT_SFRAME_FDE_START_OFFSET);
3021
0
  }
3022
0
      if (htab->plt_second_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
3023
0
  {
3024
0
    if (! _bfd_elf_merge_section_sframe (output_bfd, info,
3025
0
                 htab->plt_second_sframe,
3026
0
                 htab->plt_second_sframe->contents))
3027
0
      return NULL;
3028
0
  }
3029
0
    }
3030
3031
0
  if (htab->plt_got_sframe != NULL
3032
0
      && htab->plt_got_sframe->contents != NULL)
3033
0
    {
3034
0
      if (htab->plt_got != NULL
3035
0
    && htab->plt_got->size != 0
3036
0
    && (htab->plt_got->flags & SEC_EXCLUDE) == 0
3037
0
    && htab->plt_got->output_section != NULL
3038
0
    && htab->plt_got_sframe->output_section != NULL)
3039
0
  {
3040
0
    bfd_vma plt_start = htab->plt_got->output_section->vma;
3041
0
    bfd_vma sframe_start
3042
0
      = (htab->plt_got_sframe->output_section->vma
3043
0
         + htab->plt_got_sframe->output_offset
3044
0
         + PLT_SFRAME_FDE_START_OFFSET);
3045
0
    bfd_put_signed_32 (dynobj, plt_start - sframe_start,
3046
0
           htab->plt_got_sframe->contents
3047
0
           + PLT_SFRAME_FDE_START_OFFSET);
3048
0
  }
3049
0
      if (htab->plt_got_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
3050
0
  {
3051
0
    if (! _bfd_elf_merge_section_sframe (output_bfd, info,
3052
0
                 htab->plt_got_sframe,
3053
0
                 htab->plt_got_sframe->contents))
3054
0
      return NULL;
3055
0
  }
3056
0
    }
3057
3058
0
  if (htab->elf.sgot && htab->elf.sgot->size > 0)
3059
0
    elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
3060
0
      = htab->got_entry_size;
3061
3062
0
  return htab;
3063
0
}
3064
3065
3066
bool
3067
_bfd_x86_elf_early_size_sections (bfd *output_bfd,
3068
          struct bfd_link_info *info)
3069
0
{
3070
0
  asection *tls_sec = elf_hash_table (info)->tls_sec;
3071
3072
0
  if (tls_sec && !bfd_link_relocatable (info))
3073
0
    {
3074
0
      struct elf_link_hash_entry *tlsbase;
3075
3076
0
      tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3077
0
              "_TLS_MODULE_BASE_",
3078
0
              false, false, false);
3079
3080
0
      if (tlsbase && tlsbase->type == STT_TLS)
3081
0
  {
3082
0
    struct elf_x86_link_hash_table *htab;
3083
0
    struct bfd_link_hash_entry *bh = NULL;
3084
0
    const struct elf_backend_data *bed
3085
0
      = get_elf_backend_data (output_bfd);
3086
3087
0
    htab = elf_x86_hash_table (info, bed->target_id);
3088
0
    if (htab == NULL)
3089
0
      return false;
3090
3091
0
    if (!(_bfd_generic_link_add_one_symbol
3092
0
    (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3093
0
     tls_sec, 0, NULL, false,
3094
0
     bed->collect, &bh)))
3095
0
      return false;
3096
3097
0
    htab->tls_module_base = bh;
3098
3099
0
    tlsbase = (struct elf_link_hash_entry *)bh;
3100
0
    tlsbase->def_regular = 1;
3101
0
    tlsbase->other = STV_HIDDEN;
3102
0
    tlsbase->root.linker_def = 1;
3103
0
    (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
3104
0
  }
3105
0
    }
3106
3107
0
  return true;
3108
0
}
3109
3110
void
3111
_bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3112
             unsigned int st_other,
3113
             bool definition,
3114
             bool dynamic ATTRIBUTE_UNUSED)
3115
0
{
3116
0
  if (definition)
3117
0
    {
3118
0
      struct elf_x86_link_hash_entry *eh
3119
0
  = (struct elf_x86_link_hash_entry *) h;
3120
0
      eh->def_protected = ELF_ST_VISIBILITY (st_other) == STV_PROTECTED;
3121
0
    }
3122
0
}
3123
3124
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3125
3126
void
3127
_bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
3128
           struct elf_link_hash_entry *dir,
3129
           struct elf_link_hash_entry *ind)
3130
0
{
3131
0
  struct elf_x86_link_hash_entry *edir, *eind;
3132
3133
0
  edir = (struct elf_x86_link_hash_entry *) dir;
3134
0
  eind = (struct elf_x86_link_hash_entry *) ind;
3135
3136
0
  if (ind->root.type == bfd_link_hash_indirect
3137
0
      && dir->got.refcount <= 0)
3138
0
    {
3139
0
      edir->tls_type = eind->tls_type;
3140
0
      eind->tls_type = GOT_UNKNOWN;
3141
0
    }
3142
3143
  /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
3144
     generate a R_386_COPY reloc.  */
3145
0
  edir->gotoff_ref |= eind->gotoff_ref;
3146
3147
0
  edir->zero_undefweak |= eind->zero_undefweak;
3148
3149
0
  if (ELIMINATE_COPY_RELOCS
3150
0
      && ind->root.type != bfd_link_hash_indirect
3151
0
      && dir->dynamic_adjusted)
3152
0
    {
3153
      /* If called to transfer flags for a weakdef during processing
3154
   of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3155
   We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
3156
0
      if (dir->versioned != versioned_hidden)
3157
0
  dir->ref_dynamic |= ind->ref_dynamic;
3158
0
      dir->ref_regular |= ind->ref_regular;
3159
0
      dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
3160
0
      dir->needs_plt |= ind->needs_plt;
3161
0
      dir->pointer_equality_needed |= ind->pointer_equality_needed;
3162
0
    }
3163
0
  else
3164
0
    _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3165
0
}
3166
3167
/* Remove undefined weak symbol from the dynamic symbol table if it
3168
   is resolved to 0.   */
3169
3170
bool
3171
_bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
3172
         struct elf_link_hash_entry *h)
3173
0
{
3174
0
  if (h->dynindx != -1
3175
0
      && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
3176
0
    {
3177
0
      h->dynindx = -1;
3178
0
      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3179
0
            h->dynstr_index);
3180
0
    }
3181
0
  return true;
3182
0
}
3183
3184
/* Change the STT_GNU_IFUNC symbol defined in position-dependent
3185
   executable into the normal function symbol and set its address
3186
   to its PLT entry, which should be resolved by R_*_IRELATIVE at
3187
   run-time.  */
3188
3189
void
3190
_bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
3191
              struct elf_x86_link_hash_table *htab,
3192
              struct elf_link_hash_entry *h,
3193
              Elf_Internal_Sym *sym)
3194
0
{
3195
0
  if (bfd_link_pde (info)
3196
0
      && h->def_regular
3197
0
      && h->dynindx != -1
3198
0
      && h->plt.offset != (bfd_vma) -1
3199
0
      && h->type == STT_GNU_IFUNC)
3200
0
    {
3201
0
      asection *plt_s;
3202
0
      bfd_vma plt_offset;
3203
0
      bfd *output_bfd = info->output_bfd;
3204
3205
0
      if (htab->plt_second)
3206
0
  {
3207
0
    struct elf_x86_link_hash_entry *eh
3208
0
      = (struct elf_x86_link_hash_entry *) h;
3209
3210
0
    plt_s = htab->plt_second;
3211
0
    plt_offset = eh->plt_second.offset;
3212
0
  }
3213
0
      else
3214
0
  {
3215
0
    plt_s = htab->elf.splt;
3216
0
    plt_offset = h->plt.offset;
3217
0
  }
3218
3219
0
      sym->st_size = 0;
3220
0
      sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
3221
0
      sym->st_shndx
3222
0
  = _bfd_elf_section_from_bfd_section (output_bfd,
3223
0
               plt_s->output_section);
3224
0
      sym->st_value = (plt_s->output_section->vma
3225
0
           + plt_s->output_offset + plt_offset);
3226
0
    }
3227
0
}
3228
3229
/* Report relative relocation.  */
3230
3231
void
3232
_bfd_x86_elf_link_report_relative_reloc
3233
  (struct bfd_link_info *info, asection *asect,
3234
   struct elf_link_hash_entry *h, Elf_Internal_Sym *sym,
3235
   const char *reloc_name, const void *reloc)
3236
0
{
3237
0
  const char *name;
3238
0
  bfd *abfd;
3239
0
  const Elf_Internal_Rela *rel = (const Elf_Internal_Rela *) reloc;
3240
3241
  /* Use the output BFD for linker created sections.  */
3242
0
  if ((asect->flags & SEC_LINKER_CREATED) != 0)
3243
0
    abfd = info->output_bfd;
3244
0
  else
3245
0
    abfd = asect->owner;
3246
3247
0
  if (h != NULL && h->root.root.string != NULL)
3248
0
    name = h->root.root.string;
3249
0
  else
3250
0
    name = bfd_elf_sym_name (abfd, &elf_symtab_hdr (abfd), sym, NULL);
3251
3252
0
  if (asect->use_rela_p)
3253
0
    info->callbacks->einfo
3254
0
      (_("%pB: %s (offset: 0x%v, info: 0x%v, addend: 0x%v) against "
3255
0
   "'%s' " "for section '%pA' in %pB\n"),
3256
0
       info->output_bfd, reloc_name, rel->r_offset, rel->r_info,
3257
0
       rel->r_addend, name, asect, abfd);
3258
0
  else
3259
0
    info->callbacks->einfo
3260
0
      (_("%pB: %s (offset: 0x%v, info: 0x%v) against '%s' for section "
3261
0
   "'%pA' in %pB\n"),
3262
0
       info->output_bfd, reloc_name, rel->r_offset, rel->r_info, name,
3263
0
       asect, abfd);
3264
0
}
3265
3266
/* Report TLS transition error.  */
3267
3268
void
3269
_bfd_x86_elf_link_report_tls_transition_error
3270
  (struct bfd_link_info *info, bfd *abfd, asection *asect,
3271
   Elf_Internal_Shdr *symtab_hdr, struct elf_link_hash_entry *h,
3272
   Elf_Internal_Sym *sym, const Elf_Internal_Rela *rel,
3273
   const char *from_reloc_name, const char *to_reloc_name,
3274
   enum elf_x86_tls_error_type tls_error)
3275
0
{
3276
0
  const char *name;
3277
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3278
0
  struct elf_x86_link_hash_table *htab
3279
0
    = elf_x86_hash_table (info, bed->target_id);
3280
3281
0
  if (h)
3282
0
    name = h->root.root.string;
3283
0
  else
3284
0
    {
3285
0
      if (htab == NULL)
3286
0
  name = "*unknown*";
3287
0
      else
3288
0
  name = bfd_elf_sym_name (abfd, symtab_hdr, sym, NULL);
3289
0
    }
3290
3291
0
  switch (tls_error)
3292
0
    {
3293
0
    case elf_x86_tls_error_yes:
3294
0
      info->callbacks->einfo
3295
  /* xgettext:c-format */
3296
0
  (_("%pB: TLS transition from %s to %s against `%s' at 0x%v in "
3297
0
     "section `%pA' failed\n"),
3298
0
   abfd, from_reloc_name, to_reloc_name, name, rel->r_offset,
3299
0
   asect);
3300
0
      break;
3301
3302
0
    case elf_x86_tls_error_add_mov:
3303
0
      info->callbacks->einfo
3304
  /* xgettext:c-format */
3305
0
  (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3306
0
     "in ADD or MOV only\n"),
3307
0
   abfd, asect, rel->r_offset, from_reloc_name, name);
3308
0
      break;
3309
3310
0
    case elf_x86_tls_error_add_movrs:
3311
0
      info->callbacks->einfo
3312
  /* xgettext:c-format */
3313
0
  (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3314
0
     "in ADD or MOVRS only\n"),
3315
0
   abfd, asect, rel->r_offset, from_reloc_name, name);
3316
0
      break;
3317
3318
0
    case elf_x86_tls_error_add_sub_mov:
3319
0
      info->callbacks->einfo
3320
  /* xgettext:c-format */
3321
0
  (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3322
0
     "in ADD, SUB or MOV only\n"),
3323
0
   abfd, asect, rel->r_offset, from_reloc_name, name);
3324
0
      break;
3325
3326
0
    case elf_x86_tls_error_indirect_call:
3327
0
      info->callbacks->einfo
3328
  /* xgettext:c-format */
3329
0
  (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3330
0
     "in indirect CALL with %s register only\n"),
3331
0
   abfd, asect, rel->r_offset, from_reloc_name, name,
3332
0
   htab->ax_register);
3333
0
      break;
3334
3335
0
    case elf_x86_tls_error_lea:
3336
0
      info->callbacks->einfo
3337
  /* xgettext:c-format */
3338
0
  (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3339
0
     "in LEA only\n"),
3340
0
   abfd, asect, rel->r_offset, from_reloc_name, name);
3341
0
      break;
3342
3343
0
    default:
3344
0
      abort ();
3345
0
      break;
3346
0
    }
3347
3348
0
  bfd_set_error (bfd_error_bad_value);
3349
0
}
3350
3351
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
3352
3353
bool
3354
_bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
3355
0
{
3356
0
  if (h->plt.offset != (bfd_vma) -1
3357
0
      && !h->def_regular
3358
0
      && !h->pointer_equality_needed)
3359
0
    return false;
3360
3361
0
  return _bfd_elf_hash_symbol (h);
3362
0
}
3363
3364
/* Adjust a symbol defined by a dynamic object and referenced by a
3365
   regular object.  The current definition is in some section of the
3366
   dynamic object, but we're not including those sections.  We have to
3367
   change the definition to something the rest of the link can
3368
   understand.  */
3369
3370
bool
3371
_bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3372
            struct elf_link_hash_entry *h)
3373
0
{
3374
0
  struct elf_x86_link_hash_table *htab;
3375
0
  asection *s, *srel;
3376
0
  struct elf_x86_link_hash_entry *eh;
3377
0
  struct elf_dyn_relocs *p;
3378
0
  const struct elf_backend_data *bed
3379
0
    = get_elf_backend_data (info->output_bfd);
3380
3381
0
  eh = (struct elf_x86_link_hash_entry *) h;
3382
3383
  /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
3384
     on by an input relocatable file and there is a non-GOT/non-PLT
3385
     reference from another relocatable file without it.
3386
     NB: There can be non-GOT reference in data sections in input with
3387
     GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS.  */
3388
0
  if (eh->non_got_ref_without_indirect_extern_access
3389
0
      && info->indirect_extern_access == 1
3390
0
      && bfd_link_executable (info))
3391
0
    {
3392
0
      unsigned int needed_1;
3393
0
      info->indirect_extern_access = 0;
3394
      /* Turn off nocopyreloc if implied by indirect_extern_access.  */
3395
0
      if (info->nocopyreloc == 2)
3396
0
  info->nocopyreloc = 0;
3397
0
      needed_1 = bfd_h_get_32 (info->output_bfd, info->needed_1_p);
3398
0
      needed_1 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
3399
0
      bfd_h_put_32 (info->output_bfd, needed_1, info->needed_1_p);
3400
0
    }
3401
3402
  /* STT_GNU_IFUNC symbol must go through PLT. */
3403
0
  if (h->type == STT_GNU_IFUNC)
3404
0
    {
3405
      /* All local STT_GNU_IFUNC references must be treate as local
3406
   calls via local PLT.  */
3407
0
      if (h->ref_regular
3408
0
    && SYMBOL_CALLS_LOCAL (info, h))
3409
0
  {
3410
0
    bfd_size_type pc_count = 0, count = 0;
3411
0
    struct elf_dyn_relocs **pp;
3412
3413
0
    eh = (struct elf_x86_link_hash_entry *) h;
3414
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
3415
0
      {
3416
0
        pc_count += p->pc_count;
3417
0
        p->count -= p->pc_count;
3418
0
        p->pc_count = 0;
3419
0
        count += p->count;
3420
0
        if (p->count == 0)
3421
0
    *pp = p->next;
3422
0
        else
3423
0
    pp = &p->next;
3424
0
      }
3425
3426
0
    if (pc_count || count)
3427
0
      {
3428
0
        h->non_got_ref = 1;
3429
0
        if (pc_count)
3430
0
    {
3431
      /* Increment PLT reference count only for PC-relative
3432
         references.  */
3433
0
      h->needs_plt = 1;
3434
0
      if (h->plt.refcount <= 0)
3435
0
        h->plt.refcount = 1;
3436
0
      else
3437
0
        h->plt.refcount += 1;
3438
0
    }
3439
0
      }
3440
3441
    /* GOTOFF relocation needs PLT.  */
3442
0
    if (eh->gotoff_ref)
3443
0
      h->plt.refcount = 1;
3444
0
  }
3445
3446
0
      if (h->plt.refcount <= 0)
3447
0
  {
3448
0
    h->plt.offset = (bfd_vma) -1;
3449
0
    h->needs_plt = 0;
3450
0
  }
3451
0
      return true;
3452
0
    }
3453
3454
  /* If this is a function, put it in the procedure linkage table.  We
3455
     will fill in the contents of the procedure linkage table later,
3456
     when we know the address of the .got section.  */
3457
0
  if (h->type == STT_FUNC
3458
0
      || h->needs_plt)
3459
0
    {
3460
0
      if (h->plt.refcount <= 0
3461
0
    || SYMBOL_CALLS_LOCAL (info, h)
3462
0
    || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3463
0
        && h->root.type == bfd_link_hash_undefweak))
3464
0
  {
3465
    /* This case can occur if we saw a PLT32 reloc in an input
3466
       file, but the symbol was never referred to by a dynamic
3467
       object, or if all references were garbage collected.  In
3468
       such a case, we don't actually need to build a procedure
3469
       linkage table, and we can just do a PC32 reloc instead.  */
3470
0
    h->plt.offset = (bfd_vma) -1;
3471
0
    h->needs_plt = 0;
3472
0
  }
3473
3474
0
      return true;
3475
0
    }
3476
0
  else
3477
    /* It's possible that we incorrectly decided a .plt reloc was needed
3478
     * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
3479
       check_relocs.  We can't decide accurately between function and
3480
       non-function syms in check-relocs;  Objects loaded later in
3481
       the link may change h->type.  So fix it now.  */
3482
0
    h->plt.offset = (bfd_vma) -1;
3483
3484
  /* If this is a weak symbol, and there is a real definition, the
3485
     processor independent code will have arranged for us to see the
3486
     real definition first, and we can just use the same value.  */
3487
0
  if (h->is_weakalias)
3488
0
    {
3489
0
      struct elf_link_hash_entry *def = weakdef (h);
3490
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3491
0
      h->root.u.def.section = def->root.u.def.section;
3492
0
      h->root.u.def.value = def->root.u.def.value;
3493
0
      if (ELIMINATE_COPY_RELOCS
3494
0
    || info->nocopyreloc
3495
0
    || SYMBOL_NO_COPYRELOC (info, eh))
3496
0
  {
3497
    /* NB: needs_copy is always 0 for i386.  */
3498
0
    h->non_got_ref = def->non_got_ref;
3499
0
    eh->needs_copy = def->needs_copy;
3500
0
  }
3501
0
      return true;
3502
0
    }
3503
3504
  /* This is a reference to a symbol defined by a dynamic object which
3505
     is not a function.  */
3506
3507
  /* If we are creating a shared library, we must presume that the
3508
     only references to the symbol are via the global offset table.
3509
     For such cases we need not do anything here; the relocations will
3510
     be handled correctly by relocate_section.  */
3511
0
  if (!bfd_link_executable (info))
3512
0
    return true;
3513
3514
  /* If there are no references to this symbol that do not use the
3515
     GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
3516
     reloc.  NB: gotoff_ref is always 0 for x86-64.  */
3517
0
  if (!h->non_got_ref && !eh->gotoff_ref)
3518
0
    return true;
3519
3520
  /* If -z nocopyreloc was given, we won't generate them either.  */
3521
0
  if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
3522
0
    {
3523
0
      h->non_got_ref = 0;
3524
0
      return true;
3525
0
    }
3526
3527
0
  htab = elf_x86_hash_table (info, bed->target_id);
3528
0
  if (htab == NULL)
3529
0
    return false;
3530
3531
  /* If there aren't any dynamic relocs in read-only sections nor
3532
     R_386_GOTOFF relocation, then we can keep the dynamic relocs and
3533
     avoid the copy reloc.  This doesn't work on VxWorks, where we can
3534
     not have dynamic relocations (other than copy and jump slot
3535
     relocations) in an executable.  */
3536
0
  if (ELIMINATE_COPY_RELOCS
3537
0
      && (bed->target_id == X86_64_ELF_DATA
3538
0
    || (!eh->gotoff_ref
3539
0
        && htab->elf.target_os != is_vxworks)))
3540
0
    {
3541
      /* If we don't find any dynamic relocs in read-only sections,
3542
   then we'll be keeping the dynamic relocs and avoiding the copy
3543
   reloc.  */
3544
0
      if (!_bfd_elf_readonly_dynrelocs (h))
3545
0
  {
3546
0
    h->non_got_ref = 0;
3547
0
    return true;
3548
0
  }
3549
0
    }
3550
3551
  /* We must allocate the symbol in our .dynbss section, which will
3552
     become part of the .bss section of the executable.  There will be
3553
     an entry for this symbol in the .dynsym section.  The dynamic
3554
     object will contain position independent code, so all references
3555
     from the dynamic object to this symbol will go through the global
3556
     offset table.  The dynamic linker will use the .dynsym entry to
3557
     determine the address it must put in the global offset table, so
3558
     both the dynamic object and the regular object will refer to the
3559
     same memory location for the variable.  */
3560
3561
  /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
3562
     dynamic linker to copy the initial value out of the dynamic object
3563
     and into the runtime process image.  */
3564
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
3565
0
    {
3566
0
      s = htab->elf.sdynrelro;
3567
0
      srel = htab->elf.sreldynrelro;
3568
0
    }
3569
0
  else
3570
0
    {
3571
0
      s = htab->elf.sdynbss;
3572
0
      srel = htab->elf.srelbss;
3573
0
    }
3574
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3575
0
    {
3576
0
      if (eh->def_protected && bfd_link_executable (info))
3577
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
3578
0
    {
3579
      /* Disallow copy relocation against non-copyable protected
3580
         symbol.  */
3581
0
      s = p->sec->output_section;
3582
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
3583
0
        {
3584
0
    info->callbacks->fatal
3585
      /* xgettext:c-format */
3586
0
      (_("%P: %pB: copy relocation against non-copyable "
3587
0
         "protected symbol `%s' in %pB\n"),
3588
0
       p->sec->owner, h->root.root.string,
3589
0
       h->root.u.def.section->owner);
3590
0
    return false;
3591
0
        }
3592
0
    }
3593
3594
0
      srel->size += htab->sizeof_reloc;
3595
0
      h->needs_copy = 1;
3596
0
    }
3597
3598
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
3599
0
}
3600
3601
void
3602
_bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
3603
        struct elf_link_hash_entry *h,
3604
        bool force_local)
3605
0
{
3606
0
  if (h->root.type == bfd_link_hash_undefweak
3607
0
      && info->nointerp
3608
0
      && bfd_link_pie (info))
3609
0
    {
3610
      /* When there is no dynamic interpreter in PIE, make the undefined
3611
   weak symbol dynamic so that PC relative branch to the undefined
3612
   weak symbol will land to address 0.  */
3613
0
      struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3614
0
      if (h->plt.refcount > 0
3615
0
    || eh->plt_got.refcount > 0)
3616
0
  return;
3617
0
    }
3618
3619
0
  _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3620
0
}
3621
3622
/* Return TRUE if a symbol is referenced locally.  It is similar to
3623
   SYMBOL_REFERENCES_LOCAL, but it also checks version script.  It
3624
   works in check_relocs.  */
3625
3626
bool
3627
_bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
3628
             struct elf_link_hash_entry *h)
3629
0
{
3630
0
  struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3631
0
  struct elf_x86_link_hash_table *htab
3632
0
    = (struct elf_x86_link_hash_table *) info->hash;
3633
3634
0
  if (eh->local_ref > 1)
3635
0
    return true;
3636
3637
0
  if (eh->local_ref == 1)
3638
0
    return false;
3639
3640
  /* Unversioned symbols defined in regular objects can be forced local
3641
     by linker version script.  A weak undefined symbol is forced local
3642
     if
3643
     1. It has non-default visibility.  Or
3644
     2. When building executable, there is no dynamic linker.  Or
3645
     3. or "-z nodynamic-undefined-weak" is used.
3646
   */
3647
0
  if (_bfd_elf_symbol_refs_local_p (h, info, 1)
3648
0
      || (h->root.type == bfd_link_hash_undefweak
3649
0
    && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3650
0
        || (bfd_link_executable (info)
3651
0
      && htab->interp == NULL)
3652
0
        || info->dynamic_undefined_weak == 0))
3653
0
      || ((h->def_regular || ELF_COMMON_DEF_P (h))
3654
0
    && info->version_info != NULL
3655
0
    && _bfd_elf_link_hide_sym_by_version (info, h)))
3656
0
    {
3657
0
      eh->local_ref = 2;
3658
0
      return true;
3659
0
    }
3660
3661
0
  eh->local_ref = 1;
3662
0
  return false;
3663
0
}
3664
3665
/* Return the section that should be marked against GC for a given
3666
   relocation.  */
3667
3668
asection *
3669
_bfd_x86_elf_gc_mark_hook (asection *sec,
3670
         struct bfd_link_info *info,
3671
         Elf_Internal_Rela *rel,
3672
         struct elf_link_hash_entry *h,
3673
         Elf_Internal_Sym *sym)
3674
0
{
3675
  /* Compiler should optimize this out.  */
3676
0
  if (((unsigned int) R_X86_64_GNU_VTINHERIT
3677
0
       != (unsigned int) R_386_GNU_VTINHERIT)
3678
0
      || ((unsigned int) R_X86_64_GNU_VTENTRY
3679
0
    != (unsigned int) R_386_GNU_VTENTRY))
3680
0
    abort ();
3681
3682
0
  if (h != NULL)
3683
0
    switch (ELF32_R_TYPE (rel->r_info))
3684
0
      {
3685
0
      case R_X86_64_GNU_VTINHERIT:
3686
0
      case R_X86_64_GNU_VTENTRY:
3687
0
  return NULL;
3688
0
      }
3689
3690
0
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
3691
0
}
3692
3693
static bfd_vma
3694
elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
3695
        bfd_vma off,
3696
        bfd_vma offset ATTRIBUTE_UNUSED,
3697
        bfd_vma got_addr)
3698
4.62k
{
3699
4.62k
  return got_addr + off;
3700
4.62k
}
3701
3702
static bfd_vma
3703
elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
3704
          bfd_vma off,
3705
          bfd_vma offset,
3706
          bfd_vma got_addr ATTRIBUTE_UNUSED)
3707
769
{
3708
769
  return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
3709
769
}
3710
3711
static bool
3712
elf_i386_valid_plt_reloc_p (unsigned int type)
3713
4.62k
{
3714
4.62k
  return (type == R_386_JUMP_SLOT
3715
4.62k
    || type == R_386_GLOB_DAT
3716
4.62k
    || type == R_386_IRELATIVE);
3717
4.62k
}
3718
3719
static bool
3720
elf_x86_64_valid_plt_reloc_p (unsigned int type)
3721
665
{
3722
665
  return (type == R_X86_64_JUMP_SLOT
3723
665
    || type == R_X86_64_GLOB_DAT
3724
665
    || type == R_X86_64_IRELATIVE);
3725
665
}
3726
3727
long
3728
_bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
3729
           long count,
3730
           long relsize,
3731
           bfd_vma got_addr,
3732
           struct elf_x86_plt plts[],
3733
           asymbol **dynsyms,
3734
           asymbol **ret)
3735
128
{
3736
128
  long size, i, n, len;
3737
128
  int j;
3738
128
  unsigned int plt_got_offset, plt_entry_size;
3739
128
  asymbol *s;
3740
128
  bfd_byte *plt_contents;
3741
128
  long dynrelcount;
3742
128
  arelent **dynrelbuf, *p;
3743
128
  char *names;
3744
128
  const struct elf_backend_data *bed;
3745
128
  bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
3746
128
            bfd_vma);
3747
128
  bool (*valid_plt_reloc_p) (unsigned int);
3748
128
  unsigned int jump_slot_reloc;
3749
3750
128
  dynrelbuf = NULL;
3751
128
  if (count == 0)
3752
44
    goto bad_return;
3753
3754
84
  dynrelbuf = (arelent **) bfd_malloc (relsize);
3755
84
  if (dynrelbuf == NULL)
3756
0
    goto bad_return;
3757
3758
84
  dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
3759
84
            dynsyms);
3760
84
  if (dynrelcount <= 0)
3761
2
    goto bad_return;
3762
3763
  /* Sort the relocs by address.  */
3764
82
  qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
3765
82
   _bfd_x86_elf_compare_relocs);
3766
3767
82
  size = count * sizeof (asymbol);
3768
3769
  /* Allocate space for @plt suffixes.  */
3770
82
  n = 0;
3771
18.6k
  for (i = 0; i < dynrelcount; i++)
3772
18.5k
    {
3773
18.5k
      p = dynrelbuf[i];
3774
18.5k
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3775
18.5k
      if (p->addend != 0)
3776
1.81k
  size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
3777
18.5k
    }
3778
3779
82
  s = *ret = (asymbol *) bfd_zmalloc (size);
3780
82
  if (s == NULL)
3781
0
    goto bad_return;
3782
3783
82
  bed = get_elf_backend_data (abfd);
3784
3785
82
  if (bed->target_id == X86_64_ELF_DATA)
3786
69
    {
3787
69
      get_plt_got_vma = elf_x86_64_get_plt_got_vma;
3788
69
      valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
3789
69
      jump_slot_reloc = R_X86_64_JUMP_SLOT;
3790
69
    }
3791
13
  else
3792
13
    {
3793
13
      get_plt_got_vma = elf_i386_get_plt_got_vma;
3794
13
      valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
3795
13
      jump_slot_reloc = R_386_JUMP_SLOT;
3796
13
      if (got_addr)
3797
9
  {
3798
    /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
3799
       address.  */
3800
9
    asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
3801
9
    if (sec != NULL)
3802
2
      got_addr = sec->vma;
3803
7
    else
3804
7
      {
3805
7
        sec = bfd_get_section_by_name (abfd, ".got");
3806
7
        if (sec != NULL)
3807
7
    got_addr = sec->vma;
3808
7
      }
3809
3810
9
    if (got_addr == (bfd_vma) -1)
3811
0
      goto bad_return;
3812
9
  }
3813
13
    }
3814
3815
  /* Check for each PLT section.  */
3816
82
  names = (char *) (s + count);
3817
82
  size = 0;
3818
82
  n = 0;
3819
397
  for (j = 0; plts[j].name != NULL; j++)
3820
315
    if ((plt_contents = plts[j].contents) != NULL)
3821
128
      {
3822
128
  long k;
3823
128
  bfd_vma offset;
3824
128
  asection *plt;
3825
128
  struct elf_x86_plt *plt_p = &plts[j];
3826
3827
128
  plt_got_offset = plt_p->plt_got_offset;
3828
128
  plt_entry_size = plt_p->plt_entry_size;
3829
3830
128
  plt = plt_p->sec;
3831
3832
128
  if ((plt_p->type & plt_lazy))
3833
56
    {
3834
      /* Skip PLT0 in lazy PLT.  */
3835
56
      k = 1;
3836
56
      offset = plt_entry_size;
3837
56
    }
3838
72
  else
3839
72
    {
3840
72
      k = 0;
3841
72
      offset = 0;
3842
72
    }
3843
3844
  /* Check each PLT entry against dynamic relocations.  */
3845
5.52k
  for (; k < plt_p->count; k++)
3846
5.39k
    {
3847
5.39k
      int off;
3848
5.39k
      bfd_vma got_vma;
3849
5.39k
      long min, max, mid;
3850
3851
      /* Get the GOT offset for i386 or the PC-relative offset
3852
         for x86-64, a signed 32-bit integer.  */
3853
5.39k
      off = H_GET_32 (abfd, (plt_contents + offset
3854
5.39k
           + plt_got_offset));
3855
5.39k
      got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
3856
3857
      /* Binary search.  */
3858
5.39k
      p = dynrelbuf[0];
3859
5.39k
      min = 0;
3860
5.39k
      max = dynrelcount;
3861
52.5k
      while ((min + 1) < max)
3862
52.4k
        {
3863
52.4k
    arelent *r;
3864
3865
52.4k
    mid = (min + max) / 2;
3866
52.4k
    r = dynrelbuf[mid];
3867
52.4k
    if (got_vma > r->address)
3868
27.3k
      min = mid;
3869
25.1k
    else if (got_vma < r->address)
3870
19.8k
      max = mid;
3871
5.28k
    else
3872
5.28k
      {
3873
5.28k
        p = r;
3874
5.28k
        break;
3875
5.28k
      }
3876
52.4k
        }
3877
3878
      /* Skip unknown relocation.  PR 17512: file: bc9d6cf5.  */
3879
5.39k
      if (got_vma == p->address
3880
5.39k
    && p->howto != NULL
3881
5.39k
    && valid_plt_reloc_p (p->howto->type))
3882
5.29k
        {
3883
5.29k
    *s = **p->sym_ptr_ptr;
3884
    /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
3885
       set.  Since we are defining a symbol, ensure one
3886
       of them is set.  */
3887
5.29k
    if ((s->flags & BSF_LOCAL) == 0)
3888
5.28k
      s->flags |= BSF_GLOBAL;
3889
5.29k
    s->flags |= BSF_SYNTHETIC;
3890
    /* This is no longer a section symbol.  */
3891
5.29k
    s->flags &= ~BSF_SECTION_SYM;
3892
5.29k
    s->section = plt;
3893
5.29k
    s->the_bfd = plt->owner;
3894
5.29k
    s->value = offset;
3895
5.29k
    s->udata.p = NULL;
3896
5.29k
    s->name = names;
3897
5.29k
    len = strlen ((*p->sym_ptr_ptr)->name);
3898
5.29k
    memcpy (names, (*p->sym_ptr_ptr)->name, len);
3899
5.29k
    names += len;
3900
    /* There may be JUMP_SLOT and IRELATIVE relocations.
3901
       JUMP_SLOT r_addend should be ignored.  */
3902
5.29k
    if (p->addend != 0 && p->howto->type != jump_slot_reloc)
3903
2
      {
3904
2
        char buf[30], *a;
3905
3906
2
        memcpy (names, "+0x", sizeof ("+0x") - 1);
3907
2
        names += sizeof ("+0x") - 1;
3908
2
        bfd_sprintf_vma (abfd, buf, p->addend);
3909
14
        for (a = buf; *a == '0'; ++a)
3910
12
          ;
3911
2
        size = strlen (a);
3912
2
        memcpy (names, a, size);
3913
2
        names += size;
3914
2
      }
3915
5.29k
    memcpy (names, "@plt", sizeof ("@plt"));
3916
5.29k
    names += sizeof ("@plt");
3917
5.29k
    n++;
3918
5.29k
    s++;
3919
    /* There should be only one entry in PLT for a given
3920
       symbol.  Set howto to NULL after processing a PLT
3921
       entry to guard against corrupted PLT.  */
3922
5.29k
    p->howto = NULL;
3923
5.29k
        }
3924
5.39k
      offset += plt_entry_size;
3925
5.39k
    }
3926
128
      }
3927
3928
  /* PLT entries with R_386_TLS_DESC relocations are skipped.  */
3929
82
  if (n == 0)
3930
5
    {
3931
51
    bad_return:
3932
51
      count = -1;
3933
51
    }
3934
77
  else
3935
77
    count = n;
3936
3937
623
  for (j = 0; plts[j].name != NULL; j++)
3938
495
    _bfd_elf_munmap_section_contents (plts[j].sec, plts[j].contents);
3939
3940
128
  free (dynrelbuf);
3941
3942
128
  return count;
3943
82
}
3944
3945
/* Parse x86 GNU properties.  */
3946
3947
enum elf_property_kind
3948
_bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
3949
           bfd_byte *ptr, unsigned int datasz)
3950
267
{
3951
267
  elf_property *prop;
3952
3953
267
  if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3954
267
      || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3955
267
      || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3956
244
    && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3957
267
      || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3958
113
    && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3959
267
      || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3960
98
    && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3961
181
    {
3962
181
      if (datasz != 4)
3963
30
  {
3964
30
    _bfd_error_handler
3965
30
      (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
3966
30
       abfd, type, datasz);
3967
30
    return property_corrupt;
3968
30
  }
3969
151
      prop = _bfd_elf_get_property (abfd, type, datasz);
3970
151
      prop->u.number |= bfd_h_get_32 (abfd, ptr);
3971
151
      prop->pr_kind = property_number;
3972
151
      return property_number;
3973
181
    }
3974
3975
86
  return property_ignored;
3976
267
}
3977
3978
/* Merge x86 GNU property BPROP with APROP.  If APROP isn't NULL,
3979
   return TRUE if APROP is updated.  Otherwise, return TRUE if BPROP
3980
   should be merged with ABFD.  */
3981
3982
bool
3983
_bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
3984
           bfd *abfd ATTRIBUTE_UNUSED,
3985
           bfd *bbfd ATTRIBUTE_UNUSED,
3986
           elf_property *aprop,
3987
           elf_property *bprop)
3988
0
{
3989
0
  unsigned int number, features;
3990
0
  bool updated = false;
3991
0
  const struct elf_backend_data *bed;
3992
0
  struct elf_x86_link_hash_table *htab;
3993
0
  unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
3994
3995
0
  if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3996
0
      || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3997
0
    && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3998
0
    {
3999
0
      if (aprop == NULL || bprop == NULL)
4000
0
  {
4001
    /* Only one of APROP and BPROP can be NULL.  */
4002
0
    if (aprop != NULL)
4003
0
      {
4004
        /* Remove this property since the other input file doesn't
4005
     have it.  */
4006
0
        aprop->pr_kind = property_remove;
4007
0
        updated = true;
4008
0
      }
4009
0
  }
4010
0
      else
4011
0
  {
4012
0
    number = aprop->u.number;
4013
0
    aprop->u.number = number | bprop->u.number;
4014
0
    updated = number != (unsigned int) aprop->u.number;
4015
0
  }
4016
0
      return updated;
4017
0
    }
4018
0
  else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4019
0
     || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
4020
0
         && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
4021
0
    {
4022
0
      features = 0;
4023
0
      if (pr_type == GNU_PROPERTY_X86_ISA_1_NEEDED)
4024
0
  {
4025
0
    bed = get_elf_backend_data (info->output_bfd);
4026
0
    htab = elf_x86_hash_table (info, bed->target_id);
4027
0
    switch (htab->params->isa_level)
4028
0
      {
4029
0
      case 0:
4030
0
        break;
4031
0
      case 2:
4032
0
        features = GNU_PROPERTY_X86_ISA_1_V2;
4033
0
        break;
4034
0
      case 3:
4035
0
        features = GNU_PROPERTY_X86_ISA_1_V3;
4036
0
        break;
4037
0
      case 4:
4038
0
        features = GNU_PROPERTY_X86_ISA_1_V4;
4039
0
        break;
4040
0
      default:
4041
0
        abort ();
4042
0
      }
4043
0
  }
4044
0
      if (aprop != NULL && bprop != NULL)
4045
0
  {
4046
0
    number = aprop->u.number;
4047
0
    aprop->u.number = number | bprop->u.number | features;
4048
    /* Remove the property if all bits are empty.  */
4049
0
    if (aprop->u.number == 0)
4050
0
      {
4051
0
        aprop->pr_kind = property_remove;
4052
0
        updated = true;
4053
0
      }
4054
0
    else
4055
0
      updated = number != (unsigned int) aprop->u.number;
4056
0
  }
4057
0
      else
4058
0
  {
4059
    /* Only one of APROP and BPROP can be NULL.  */
4060
0
    if (aprop != NULL)
4061
0
      {
4062
0
        aprop->u.number |= features;
4063
0
        if (aprop->u.number == 0)
4064
0
    {
4065
      /* Remove APROP if all bits are empty.  */
4066
0
      aprop->pr_kind = property_remove;
4067
0
      updated = true;
4068
0
    }
4069
0
      }
4070
0
    else
4071
0
      {
4072
        /* Return TRUE if APROP is NULL and all bits of BPROP
4073
     aren't empty to indicate that BPROP should be added
4074
     to ABFD.  */
4075
0
        bprop->u.number |= features;
4076
0
        updated = bprop->u.number != 0;
4077
0
      }
4078
0
  }
4079
0
      return updated;
4080
0
    }
4081
0
  else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
4082
0
     && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4083
0
    {
4084
      /* Only one of APROP and BPROP can be NULL:
4085
   1. APROP & BPROP when both APROP and BPROP aren't NULL.
4086
   2. If APROP is NULL, remove x86 feature.
4087
   3. Otherwise, do nothing.
4088
       */
4089
0
      bed = get_elf_backend_data (info->output_bfd);
4090
0
      htab = elf_x86_hash_table (info, bed->target_id);
4091
0
      if (!htab)
4092
0
  abort ();
4093
0
      if (aprop != NULL && bprop != NULL)
4094
0
  {
4095
0
    number = aprop->u.number;
4096
0
    aprop->u.number = number & bprop->u.number;
4097
0
    if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
4098
0
      {
4099
0
        features = 0;
4100
0
        if (htab->params->ibt)
4101
0
    features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4102
0
        if (htab->params->shstk)
4103
0
    features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4104
0
        if (htab->params->lam_u48)
4105
0
    features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4106
0
           | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4107
0
        else if (htab->params->lam_u57)
4108
0
    features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4109
        /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
4110
     GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4111
     GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4112
     GNU_PROPERTY_X86_FEATURE_1_LAM_U57.  */
4113
0
        aprop->u.number |= features;
4114
0
      }
4115
0
    updated = number != (unsigned int) aprop->u.number;
4116
    /* Remove the property if all feature bits are cleared.  */
4117
0
    if (aprop->u.number == 0)
4118
0
      aprop->pr_kind = property_remove;
4119
0
  }
4120
0
      else
4121
0
  {
4122
    /* There should be no AND properties since some input doesn't
4123
       have them.  Set IBT and SHSTK properties for -z ibt and -z
4124
       shstk if needed.  */
4125
0
    features = 0;
4126
0
    if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
4127
0
      {
4128
0
        if (htab->params->ibt)
4129
0
    features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4130
0
        if (htab->params->shstk)
4131
0
    features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4132
0
        if (htab->params->lam_u48)
4133
0
    features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4134
0
           | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4135
0
        else if (htab->params->lam_u57)
4136
0
    features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4137
0
      }
4138
0
    if (features)
4139
0
      {
4140
0
        if (aprop != NULL)
4141
0
    {
4142
0
      updated = features != (unsigned int) aprop->u.number;
4143
0
      aprop->u.number = features;
4144
0
    }
4145
0
        else
4146
0
    {
4147
0
      updated = true;
4148
0
      bprop->u.number = features;
4149
0
    }
4150
0
      }
4151
0
    else if (aprop != NULL)
4152
0
      {
4153
0
        aprop->pr_kind = property_remove;
4154
0
        updated = true;
4155
0
      }
4156
0
  }
4157
0
      return updated;
4158
0
    }
4159
0
  else
4160
0
    {
4161
      /* Never should happen.  */
4162
0
      abort ();
4163
0
    }
4164
4165
0
  return updated;
4166
0
}
4167
4168
/* Report x86-64 ISA level.  */
4169
4170
static void
4171
report_isa_level (struct bfd_link_info *info, bfd *abfd,
4172
      unsigned int bitmask, bool needed)
4173
0
{
4174
0
  if (!bitmask)
4175
0
    return;
4176
4177
0
  if (needed)
4178
0
    info->callbacks->einfo (_("%pB: x86 ISA needed: "), abfd);
4179
0
  else
4180
0
    info->callbacks->einfo (_("%pB: x86 ISA used: "), abfd);
4181
4182
0
  while (bitmask)
4183
0
    {
4184
0
      unsigned int bit = bitmask & (- bitmask);
4185
4186
0
      bitmask &= ~ bit;
4187
0
      switch (bit)
4188
0
  {
4189
0
  case GNU_PROPERTY_X86_ISA_1_BASELINE:
4190
0
    info->callbacks->einfo ("x86-64-baseline");
4191
0
    break;
4192
0
  case GNU_PROPERTY_X86_ISA_1_V2:
4193
0
    info->callbacks->einfo ("x86-64-v2");
4194
0
    break;
4195
0
  case GNU_PROPERTY_X86_ISA_1_V3:
4196
0
    info->callbacks->einfo ("x86-64-v3");
4197
0
    break;
4198
0
  case GNU_PROPERTY_X86_ISA_1_V4:
4199
0
    info->callbacks->einfo ("x86-64-v4");
4200
0
    break;
4201
0
  default:
4202
0
    info->callbacks->einfo (_("<unknown: %x>"), bit);
4203
0
    break;
4204
0
  }
4205
0
      if (bitmask)
4206
0
  info->callbacks->einfo (", ");
4207
0
    }
4208
4209
0
  info->callbacks->einfo ("\n");
4210
0
}
4211
4212
/* Set up x86 GNU properties.  Return the first relocatable ELF input
4213
   with GNU properties if found.  Otherwise, return NULL.  */
4214
4215
bfd *
4216
_bfd_x86_elf_link_setup_gnu_properties
4217
  (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
4218
0
{
4219
0
  bool normal_target;
4220
0
  bool lazy_plt;
4221
0
  asection *sec, *pltsec;
4222
0
  bfd *dynobj;
4223
0
  bool use_ibt_plt;
4224
0
  unsigned int plt_alignment, features, isa_level;
4225
0
  struct elf_x86_link_hash_table *htab;
4226
0
  bfd *pbfd;
4227
0
  bfd *ebfd = NULL;
4228
0
  elf_property *prop;
4229
0
  const struct elf_backend_data *bed;
4230
0
  unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
4231
0
  unsigned int got_align;
4232
4233
  /* Find a normal input file with GNU property note.  */
4234
0
  for (pbfd = info->input_bfds;
4235
0
       pbfd != NULL;
4236
0
       pbfd = pbfd->link.next)
4237
0
    if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
4238
0
  && bfd_count_sections (pbfd) != 0)
4239
0
      {
4240
0
  ebfd = pbfd;
4241
4242
0
  if (elf_properties (pbfd) != NULL)
4243
0
    break;
4244
0
      }
4245
4246
0
  bed = get_elf_backend_data (info->output_bfd);
4247
4248
0
  htab = elf_x86_hash_table (info, bed->target_id);
4249
0
  if (htab == NULL)
4250
0
    return pbfd;
4251
4252
0
  features = 0;
4253
0
  if (htab->params->ibt)
4254
0
    {
4255
0
      features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4256
0
      htab->params->cet_report &= ~prop_report_ibt;
4257
0
    }
4258
0
  if (htab->params->shstk)
4259
0
    {
4260
0
      features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4261
0
      htab->params->cet_report &= ~prop_report_shstk;
4262
0
    }
4263
0
  if (!(htab->params->cet_report & (prop_report_ibt | prop_report_shstk)))
4264
0
    htab->params->cet_report = prop_report_none;
4265
0
  if (htab->params->lam_u48)
4266
0
    {
4267
0
      features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4268
0
       | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4269
0
      htab->params->lam_u48_report = prop_report_none;
4270
0
      htab->params->lam_u57_report = prop_report_none;
4271
0
    }
4272
0
  else if (htab->params->lam_u57)
4273
0
    {
4274
0
      features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4275
0
      htab->params->lam_u57_report = prop_report_none;
4276
0
    }
4277
4278
0
  switch (htab->params->isa_level)
4279
0
    {
4280
0
    case 0:
4281
0
      isa_level = 0;
4282
0
      break;
4283
0
    case 1:
4284
0
      isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE;
4285
0
      break;
4286
0
    case 2:
4287
0
      isa_level = GNU_PROPERTY_X86_ISA_1_V2;
4288
0
      break;
4289
0
    case 3:
4290
0
      isa_level = GNU_PROPERTY_X86_ISA_1_V3;
4291
0
      break;
4292
0
    case 4:
4293
0
      isa_level = GNU_PROPERTY_X86_ISA_1_V4;
4294
0
      break;
4295
0
    default:
4296
0
      abort ();
4297
0
    }
4298
4299
0
  if (ebfd != NULL)
4300
0
    {
4301
0
      prop = NULL;
4302
0
      if (features)
4303
0
  {
4304
    /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
4305
       GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4306
       GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4307
       GNU_PROPERTY_X86_FEATURE_1_LAM_U57.  */
4308
0
    prop = _bfd_elf_get_property (ebfd,
4309
0
          GNU_PROPERTY_X86_FEATURE_1_AND,
4310
0
          4);
4311
0
    prop->u.number |= features;
4312
0
    prop->pr_kind = property_number;
4313
0
  }
4314
4315
0
      if (isa_level)
4316
0
  {
4317
    /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED.  */
4318
0
    prop = _bfd_elf_get_property (ebfd,
4319
0
          GNU_PROPERTY_X86_ISA_1_NEEDED,
4320
0
          4);
4321
0
    prop->u.number |= isa_level;
4322
0
    prop->pr_kind = property_number;
4323
0
  }
4324
4325
      /* Create the GNU property note section if needed.  */
4326
0
      if (prop != NULL && pbfd == NULL)
4327
0
  {
4328
0
    sec = bfd_make_section_with_flags (ebfd,
4329
0
               NOTE_GNU_PROPERTY_SECTION_NAME,
4330
0
               (SEC_ALLOC
4331
0
                | SEC_LOAD
4332
0
                | SEC_IN_MEMORY
4333
0
                | SEC_READONLY
4334
0
                | SEC_HAS_CONTENTS
4335
0
                | SEC_DATA));
4336
0
    if (sec == NULL
4337
0
        || !bfd_set_section_alignment (sec, class_align))
4338
0
      info->callbacks->fatal (_("%P: failed to create %sn"),
4339
0
            NOTE_GNU_PROPERTY_SECTION_NAME);
4340
4341
0
    elf_section_type (sec) = SHT_NOTE;
4342
0
  }
4343
0
    }
4344
4345
0
  bool check_feature_1 = (htab->params->cet_report
4346
0
        || htab->params->lam_u48_report
4347
0
        || htab->params->lam_u57_report);
4348
0
  if (check_feature_1 || htab->params->isa_level_report)
4349
0
    {
4350
      /* Report missing IBT, SHSTK, ISA level and LAM properties.  */
4351
0
      bfd *abfd;
4352
0
      const char *warning_msg = _("%P: %pB: warning: missing %s\n");
4353
0
      const char *error_msg = _("%X%P: %pB: error: missing %s\n");
4354
0
      const char *cet_msg = NULL;
4355
0
      const char *lam_u48_msg = NULL;
4356
0
      const char *lam_u57_msg = NULL;
4357
0
      const char *missing;
4358
0
      elf_property_list *p;
4359
0
      bool missing_ibt, missing_shstk;
4360
0
      bool missing_lam_u48, missing_lam_u57;
4361
0
      bool check_ibt
4362
0
  = (htab->params->cet_report
4363
0
     && (htab->params->cet_report & prop_report_ibt));
4364
0
      bool check_shstk
4365
0
  = (htab->params->cet_report
4366
0
     && (htab->params->cet_report & prop_report_shstk));
4367
0
      bool report_needed_level
4368
0
  = (htab->params->isa_level_report & isa_level_report_needed) != 0;
4369
0
      bool report_used_level
4370
0
  = (htab->params->isa_level_report & isa_level_report_used) != 0;
4371
4372
0
      if (htab->params->cet_report)
4373
0
  {
4374
0
    if ((htab->params->cet_report & prop_report_warning))
4375
0
      cet_msg = warning_msg;
4376
0
    else
4377
0
      cet_msg = error_msg;
4378
0
  }
4379
0
      if (htab->params->lam_u48_report)
4380
0
  {
4381
0
    if ((htab->params->lam_u48_report & prop_report_warning))
4382
0
      lam_u48_msg = warning_msg;
4383
0
    else
4384
0
      lam_u48_msg = error_msg;
4385
0
  }
4386
0
      if (htab->params->lam_u57_report)
4387
0
  {
4388
0
    if ((htab->params->lam_u57_report & prop_report_warning))
4389
0
      lam_u57_msg = warning_msg;
4390
0
    else
4391
0
      lam_u57_msg = error_msg;
4392
0
  }
4393
4394
0
      for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4395
0
  if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
4396
0
      && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
4397
0
    {
4398
0
      elf_property_list *p_feature_1 = NULL;
4399
0
      elf_property_list *p_isa_1_needed = NULL;
4400
0
      elf_property_list *p_isa_1_used = NULL;
4401
0
      bool find_feature_1 = check_feature_1;
4402
0
      bool find_needed_level = report_needed_level;
4403
0
      bool find_used_level = report_used_level;
4404
4405
0
      for (p = elf_properties (abfd); p; p = p->next)
4406
0
        {
4407
0
    switch (p->property.pr_type)
4408
0
      {
4409
0
      case GNU_PROPERTY_X86_FEATURE_1_AND:
4410
0
        if (find_feature_1)
4411
0
          {
4412
0
      p_feature_1 = p;
4413
0
      find_feature_1 = false;
4414
0
          }
4415
0
        break;
4416
0
      case GNU_PROPERTY_X86_ISA_1_NEEDED:
4417
0
        if (find_needed_level)
4418
0
          {
4419
0
      p_isa_1_needed = p;
4420
0
      find_needed_level = false;
4421
0
          }
4422
0
        break;
4423
0
      case GNU_PROPERTY_X86_ISA_1_USED:
4424
0
        if (find_used_level)
4425
0
          {
4426
0
      p_isa_1_used = p;
4427
0
      find_used_level = false;
4428
0
          }
4429
0
        break;
4430
0
      default:
4431
0
        break;
4432
0
      }
4433
4434
0
    if (!find_feature_1
4435
0
        && !find_needed_level
4436
0
        && !find_used_level)
4437
0
      break;
4438
0
        }
4439
4440
4441
0
      missing_ibt = check_ibt;
4442
0
      missing_shstk = check_shstk;
4443
0
      missing_lam_u48 = !!lam_u48_msg;
4444
0
      missing_lam_u57 = !!lam_u57_msg;
4445
0
      if (p_feature_1)
4446
0
        {
4447
0
    missing_ibt &= !(p_feature_1->property.u.number
4448
0
         & GNU_PROPERTY_X86_FEATURE_1_IBT);
4449
0
    missing_shstk &= !(p_feature_1->property.u.number
4450
0
           & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
4451
0
    missing_lam_u48 &= !(p_feature_1->property.u.number
4452
0
             & GNU_PROPERTY_X86_FEATURE_1_LAM_U48);
4453
0
    missing_lam_u57 &= !(p_feature_1->property.u.number
4454
0
             & GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4455
0
        }
4456
0
      if (missing_ibt || missing_shstk)
4457
0
        {
4458
0
    if (missing_ibt && missing_shstk)
4459
0
      missing = _("IBT and SHSTK properties");
4460
0
    else if (missing_ibt)
4461
0
      missing = _("IBT property");
4462
0
    else
4463
0
      missing = _("SHSTK property");
4464
0
    info->callbacks->einfo (cet_msg, abfd, missing);
4465
0
        }
4466
0
      if (missing_lam_u48)
4467
0
        {
4468
0
    missing = _("LAM_U48 property");
4469
0
    info->callbacks->einfo (lam_u48_msg, abfd, missing);
4470
0
        }
4471
0
      if (missing_lam_u57)
4472
0
        {
4473
0
    missing = _("LAM_U57 property");
4474
0
    info->callbacks->einfo (lam_u57_msg, abfd, missing);
4475
0
        }
4476
4477
0
      if (p_isa_1_needed)
4478
0
        report_isa_level (info, abfd,
4479
0
        p_isa_1_needed->property.u.number,
4480
0
        true);
4481
0
      if (p_isa_1_used)
4482
0
        report_isa_level (info, abfd,
4483
0
        p_isa_1_used->property.u.number,
4484
0
        false);
4485
0
    }
4486
0
    }
4487
4488
0
  pbfd = _bfd_elf_link_setup_gnu_properties (info);
4489
4490
0
  htab->r_info = init_table->r_info;
4491
0
  htab->r_sym = init_table->r_sym;
4492
4493
0
  if (bfd_link_relocatable (info))
4494
0
    return pbfd;
4495
4496
0
  htab->plt0_pad_byte = init_table->plt0_pad_byte;
4497
4498
0
  use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
4499
0
  if (!use_ibt_plt && pbfd != NULL)
4500
0
    {
4501
      /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on.  */
4502
0
      elf_property_list *p;
4503
4504
      /* The property list is sorted in order of type.  */
4505
0
      for (p = elf_properties (pbfd); p; p = p->next)
4506
0
  {
4507
0
    if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
4508
0
      {
4509
0
        use_ibt_plt = !!(p->property.u.number
4510
0
             & GNU_PROPERTY_X86_FEATURE_1_IBT);
4511
0
        break;
4512
0
      }
4513
0
    else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
4514
0
      break;
4515
0
  }
4516
0
    }
4517
4518
0
  dynobj = htab->elf.dynobj;
4519
4520
  /* Set htab->elf.dynobj here so that there is no need to check and
4521
     set it in check_relocs.  */
4522
0
  if (dynobj == NULL)
4523
0
    {
4524
0
      if (pbfd != NULL)
4525
0
  {
4526
0
    htab->elf.dynobj = pbfd;
4527
0
    dynobj = pbfd;
4528
0
  }
4529
0
      else
4530
0
  {
4531
0
    bfd *abfd;
4532
4533
    /* Find a normal input file to hold linker created
4534
       sections.  */
4535
0
    for (abfd = info->input_bfds;
4536
0
         abfd != NULL;
4537
0
         abfd = abfd->link.next)
4538
0
      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
4539
0
    && (abfd->flags
4540
0
        & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
4541
0
    && bed->relocs_compatible (abfd->xvec,
4542
0
             info->output_bfd->xvec))
4543
0
        {
4544
0
    htab->elf.dynobj = abfd;
4545
0
    dynobj = abfd;
4546
0
    break;
4547
0
        }
4548
0
  }
4549
0
    }
4550
4551
  /* Return if there are no normal input files.  */
4552
0
  if (dynobj == NULL)
4553
0
    return pbfd;
4554
4555
  /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
4556
     still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
4557
     canonical function address.  */
4558
0
  htab->plt.has_plt0 = 1;
4559
0
  htab->plt.plt_indirect_branch_offset = 0;
4560
0
  normal_target = htab->elf.target_os == is_normal;
4561
4562
0
  if (normal_target)
4563
0
    {
4564
0
      if (use_ibt_plt)
4565
0
  {
4566
0
    htab->lazy_plt = init_table->lazy_ibt_plt;
4567
0
    htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
4568
0
    htab->plt.plt_indirect_branch_offset = 4;
4569
0
  }
4570
0
      else
4571
0
  {
4572
0
    htab->lazy_plt = init_table->lazy_plt;
4573
0
    htab->non_lazy_plt = init_table->non_lazy_plt;
4574
0
  }
4575
0
    }
4576
0
  else
4577
0
    {
4578
0
      htab->lazy_plt = init_table->lazy_plt;
4579
0
      htab->non_lazy_plt = NULL;
4580
0
    }
4581
4582
0
  pltsec = htab->elf.splt;
4583
4584
0
  if (htab->non_lazy_plt != NULL
4585
0
      && (!htab->plt.has_plt0 || pltsec == NULL))
4586
0
    lazy_plt = false;
4587
0
  else
4588
0
    lazy_plt = true;
4589
4590
0
  if (normal_target)
4591
0
    {
4592
0
      if (use_ibt_plt)
4593
0
  {
4594
0
    if (lazy_plt)
4595
0
      htab->sframe_plt = init_table->sframe_lazy_ibt_plt;
4596
0
    else
4597
0
      htab->sframe_plt = init_table->sframe_non_lazy_ibt_plt;
4598
0
  }
4599
0
      else
4600
0
  {
4601
0
    if (lazy_plt)
4602
0
      htab->sframe_plt = init_table->sframe_lazy_plt;
4603
0
    else
4604
0
      htab->sframe_plt = init_table->sframe_non_lazy_plt;
4605
0
  }
4606
0
    }
4607
0
  else
4608
0
    htab->sframe_plt = NULL;
4609
4610
  /* If the non-lazy PLT is available, use it for all PLT entries if
4611
     there are no PLT0 or no .plt section.  */
4612
0
  if (!lazy_plt)
4613
0
    {
4614
0
      if (bfd_link_pic (info))
4615
0
  htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
4616
0
      else
4617
0
  htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
4618
0
      htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
4619
0
      htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
4620
0
      htab->plt.plt_got_insn_size
4621
0
  = htab->non_lazy_plt->plt_got_insn_size;
4622
0
      htab->plt.eh_frame_plt_size
4623
0
  = htab->non_lazy_plt->eh_frame_plt_size;
4624
0
      htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
4625
0
    }
4626
0
  else
4627
0
    {
4628
0
      if (bfd_link_pic (info))
4629
0
  {
4630
0
    htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
4631
0
    htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
4632
0
  }
4633
0
      else
4634
0
  {
4635
0
    htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
4636
0
    htab->plt.plt_entry = htab->lazy_plt->plt_entry;
4637
0
  }
4638
0
      htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
4639
0
      htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
4640
0
      htab->plt.plt_got_insn_size
4641
0
  = htab->lazy_plt->plt_got_insn_size;
4642
0
      htab->plt.eh_frame_plt_size
4643
0
  = htab->lazy_plt->eh_frame_plt_size;
4644
0
      htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
4645
0
    }
4646
4647
0
  if (htab->elf.target_os == is_vxworks
4648
0
      && !elf_vxworks_create_dynamic_sections (dynobj, info,
4649
0
                 &htab->srelplt2))
4650
0
    {
4651
0
      info->callbacks->fatal (_("%P: failed to create VxWorks dynamic sections\n"));
4652
0
      return pbfd;
4653
0
    }
4654
4655
  /* Since create_dynamic_sections isn't always called, but GOT
4656
     relocations need GOT relocations, create them here so that we
4657
     don't need to do it in check_relocs.  */
4658
0
  if (htab->elf.sgot == NULL
4659
0
      && !_bfd_elf_create_got_section (dynobj, info))
4660
0
    info->callbacks->fatal (_("%P: failed to create GOT sections\n"));
4661
4662
0
  got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
4663
4664
  /* Align .got and .got.plt sections to their entry size.  Do it here
4665
     instead of in create_dynamic_sections so that they are always
4666
     properly aligned even if create_dynamic_sections isn't called.  */
4667
0
  sec = htab->elf.sgot;
4668
0
  if (!bfd_set_section_alignment (sec, got_align))
4669
0
    abort ();
4670
4671
0
  sec = htab->elf.sgotplt;
4672
0
  if (!bfd_set_section_alignment (sec, got_align))
4673
0
    abort ();
4674
4675
  /* Create the ifunc sections here so that check_relocs can be
4676
     simplified.  */
4677
0
  if (!_bfd_elf_create_ifunc_sections (dynobj, info))
4678
0
    info->callbacks->fatal (_("%P: failed to create ifunc sections\n"));
4679
4680
0
  plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
4681
4682
0
  if (pltsec != NULL)
4683
0
    {
4684
      /* Whe creating executable, set the contents of the .interp
4685
   section to the interpreter.  */
4686
0
      if (bfd_link_executable (info) && !info->nointerp)
4687
0
  {
4688
0
    asection *s = bfd_get_linker_section (dynobj, ".interp");
4689
0
    if (s == NULL)
4690
0
      abort ();
4691
0
    s->size = htab->dynamic_interpreter_size;
4692
0
    s->contents = (unsigned char *) htab->dynamic_interpreter;
4693
0
    s->alloced = 1;
4694
0
    htab->interp = s;
4695
0
  }
4696
4697
0
      if (normal_target)
4698
0
  {
4699
0
    flagword pltflags = (bed->dynamic_sec_flags
4700
0
             | SEC_ALLOC
4701
0
             | SEC_CODE
4702
0
             | SEC_LOAD
4703
0
             | SEC_READONLY);
4704
0
    unsigned int non_lazy_plt_alignment
4705
0
      = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
4706
4707
0
    sec = pltsec;
4708
0
    if (!bfd_set_section_alignment (sec, plt_alignment))
4709
0
      abort ();
4710
4711
    /* Create the GOT procedure linkage table.  */
4712
0
    sec = bfd_make_section_anyway_with_flags (dynobj,
4713
0
                ".plt.got",
4714
0
                pltflags);
4715
0
    if (sec == NULL
4716
0
        || !bfd_set_section_alignment (sec, non_lazy_plt_alignment))
4717
0
      info->callbacks->fatal (_("%P: failed to create GOT PLT section\n"));
4718
4719
0
    htab->plt_got = sec;
4720
4721
0
    if (lazy_plt)
4722
0
      {
4723
0
        sec = NULL;
4724
4725
0
        if (use_ibt_plt)
4726
0
    {
4727
      /* Create the second PLT for Intel IBT support.  IBT
4728
         PLT is needed only for lazy binding.  */
4729
0
      sec = bfd_make_section_anyway_with_flags (dynobj,
4730
0
                  ".plt.sec",
4731
0
                  pltflags);
4732
0
      if (sec == NULL
4733
0
          || !bfd_set_section_alignment (sec, plt_alignment))
4734
0
        info->callbacks->fatal (_("%P: failed to create IBT-enabled PLT section\n"));
4735
0
    }
4736
4737
0
        htab->plt_second = sec;
4738
0
      }
4739
0
  }
4740
4741
0
      if (!info->no_ld_generated_unwind_info)
4742
0
  {
4743
0
    flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4744
0
          | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4745
0
          | SEC_LINKER_CREATED);
4746
4747
0
    sec = bfd_make_section_anyway_with_flags (dynobj,
4748
0
                ".eh_frame",
4749
0
                flags);
4750
0
    if (sec == NULL
4751
0
        || !bfd_set_section_alignment (sec, class_align))
4752
0
      info->callbacks->fatal (_("%P: failed to create PLT .eh_frame section\n"));
4753
4754
0
    htab->plt_eh_frame = sec;
4755
4756
0
    if (htab->plt_got != NULL)
4757
0
      {
4758
0
        sec = bfd_make_section_anyway_with_flags (dynobj,
4759
0
              ".eh_frame",
4760
0
              flags);
4761
0
        if (sec == NULL
4762
0
      || !bfd_set_section_alignment (sec, class_align))
4763
0
    info->callbacks->fatal (_("%P: failed to create GOT PLT .eh_frame section\n"));
4764
4765
0
        htab->plt_got_eh_frame = sec;
4766
0
      }
4767
4768
0
    if (htab->plt_second != NULL)
4769
0
      {
4770
0
        sec = bfd_make_section_anyway_with_flags (dynobj,
4771
0
              ".eh_frame",
4772
0
              flags);
4773
0
        if (sec == NULL
4774
0
      || !bfd_set_section_alignment (sec, class_align))
4775
0
    info->callbacks->fatal (_("%P: failed to create the second PLT .eh_frame section\n"));
4776
4777
0
        htab->plt_second_eh_frame = sec;
4778
0
      }
4779
0
  }
4780
4781
      /* .sframe sections are emitted for AMD64 ABI only.  */
4782
0
      if (ABI_64_P (info->output_bfd) && !info->no_ld_generated_unwind_info)
4783
0
  {
4784
0
    flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4785
0
          | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4786
0
          | SEC_LINKER_CREATED);
4787
4788
0
    sec = bfd_make_section_anyway_with_flags (dynobj,
4789
0
                ".sframe",
4790
0
                flags);
4791
0
    if (sec == NULL)
4792
0
      info->callbacks->fatal (_("%P: failed to create PLT .sframe section\n"));
4793
4794
    // FIXME check this
4795
    // if (!bfd_set_section_alignment (sec, class_align))
4796
    //  goto error_alignment;
4797
4798
0
    htab->plt_sframe = sec;
4799
4800
    /* Second PLT is generated for Intel IBT + lazy plt.  */
4801
0
    if (htab->plt_second != NULL)
4802
0
      {
4803
0
        sec = bfd_make_section_anyway_with_flags (dynobj,
4804
0
              ".sframe",
4805
0
              flags);
4806
0
        if (sec == NULL)
4807
0
    info->callbacks->fatal (_("%P: failed to create second PLT .sframe section\n"));
4808
4809
0
        htab->plt_second_sframe = sec;
4810
0
      }
4811
4812
    /* .plt.got.  */
4813
0
    if (htab->plt_got != NULL)
4814
0
      {
4815
0
        sec = bfd_make_section_anyway_with_flags (dynobj,
4816
0
              ".sframe",
4817
0
              flags);
4818
0
        if (sec == NULL)
4819
0
    info->callbacks->fatal (_("%P: failed to create PLT GOT .sframe section\n"));
4820
4821
0
        htab->plt_got_sframe = sec;
4822
0
      }
4823
0
  }
4824
0
    }
4825
4826
  /* The .iplt section is used for IFUNC symbols in static
4827
     executables.  */
4828
0
  sec = htab->elf.iplt;
4829
0
  if (sec != NULL)
4830
0
    {
4831
      /* NB: Delay setting its alignment until we know it is non-empty.
4832
   Otherwise an empty iplt section may change vma and lma of the
4833
   following sections, which triggers moving dot of the following
4834
   section backwards, resulting in a warning and section lma not
4835
   being set properly.  It later leads to a "File truncated"
4836
   error.  */
4837
0
      if (!bfd_set_section_alignment (sec, 0))
4838
0
  abort ();
4839
4840
0
      htab->plt.iplt_alignment = (normal_target
4841
0
          ? plt_alignment
4842
0
          : bed->plt_alignment);
4843
0
    }
4844
4845
0
  if (bfd_link_executable (info)
4846
0
      && !info->nointerp
4847
0
      && !htab->params->has_dynamic_linker
4848
0
      && htab->params->static_before_all_inputs)
4849
0
    {
4850
      /* Report error for dynamic input objects if -static is passed at
4851
   command-line before all input files without --dynamic-linker
4852
   unless --no-dynamic-linker is used.  */
4853
0
      bfd *abfd;
4854
4855
0
      for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4856
0
  if ((abfd->flags & DYNAMIC))
4857
0
    info->callbacks->einfo
4858
0
      (_("%X%P: attempted static link of dynamic object `%pB'\n"),
4859
0
       abfd);
4860
0
    }
4861
4862
0
  return pbfd;
4863
0
}
4864
4865
/* Fix up x86 GNU properties.  */
4866
4867
void
4868
_bfd_x86_elf_link_fixup_gnu_properties
4869
  (struct bfd_link_info *info, elf_property_list **listp)
4870
0
{
4871
0
  elf_property_list *p;
4872
4873
0
  for (p = *listp; p; p = p->next)
4874
0
    {
4875
0
      unsigned int type = p->property.pr_type;
4876
0
      if (type == GNU_PROPERTY_MEMORY_SEAL
4877
0
    || type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4878
0
    || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4879
0
    || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4880
0
        && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4881
0
    || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4882
0
        && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
4883
0
    || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4884
0
        && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
4885
0
  {
4886
0
    if (p->property.u.number == 0
4887
0
        && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4888
0
      || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4889
0
          && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4890
0
      || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4891
0
          && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
4892
0
      {
4893
        /* Remove empty property.  */
4894
0
        *listp = p->next;
4895
0
        continue;
4896
0
      }
4897
4898
    /* Keep LAM features only for 64-bit output.  */
4899
0
    if (type == GNU_PROPERTY_X86_FEATURE_1_AND
4900
0
        && !ABI_64_P (info->output_bfd))
4901
0
      p->property.u.number &= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4902
0
              | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4903
4904
0
    listp = &p->next;
4905
0
  }
4906
0
      else if (type > GNU_PROPERTY_HIPROC)
4907
0
  {
4908
    /* The property list is sorted in order of type.  */
4909
0
    break;
4910
0
  }
4911
0
    }
4912
0
}
4913
4914
void
4915
_bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
4916
         struct elf_linker_x86_params *params)
4917
0
{
4918
0
  const struct elf_backend_data *bed
4919
0
    = get_elf_backend_data (info->output_bfd);
4920
0
  struct elf_x86_link_hash_table *htab
4921
0
    = elf_x86_hash_table (info, bed->target_id);
4922
0
  if (htab != NULL)
4923
0
    htab->params = params;
4924
0
}