Coverage Report

Created: 2026-09-14 08:07

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