Coverage Report

Created: 2023-08-28 06:26

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