OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [bfd/] [elf64-x86-64.c] - Blame information for rev 853

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 227 jeremybenn
/* X86-64 specific support for 64-bit ELF
2
   Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3
   2010  Free Software Foundation, Inc.
4
   Contributed by Jan Hubicka <jh@suse.cz>.
5
 
6
   This file is part of BFD, the Binary File Descriptor library.
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
 
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
 
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "bfdlink.h"
26
#include "libbfd.h"
27
#include "elf-bfd.h"
28
#include "bfd_stdint.h"
29
#include "objalloc.h"
30
#include "hashtab.h"
31
 
32
#include "elf/x86-64.h"
33
 
34
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
35
#define MINUS_ONE (~ (bfd_vma) 0)
36
 
37
/* The relocation "howto" table.  Order of fields:
38
   type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
39
   special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset.  */
40
static reloc_howto_type x86_64_elf_howto_table[] =
41
{
42
  HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
43
        bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
44
        FALSE),
45
  HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
46
        bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
47
        FALSE),
48
  HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
49
        bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
50
        TRUE),
51
  HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
52
        bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
53
        FALSE),
54
  HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
55
        bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
56
        TRUE),
57
  HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
58
        bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
59
        FALSE),
60
  HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
61
        bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
62
        MINUS_ONE, FALSE),
63
  HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
64
        bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
65
        MINUS_ONE, FALSE),
66
  HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
67
        bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
68
        MINUS_ONE, FALSE),
69
  HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
70
        bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
71
        0xffffffff, TRUE),
72
  HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
73
        bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
74
        FALSE),
75
  HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
76
        bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
77
        FALSE),
78
  HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
79
        bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
80
  HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
81
        bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
82
  HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
83
        bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
84
  HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
85
        bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
86
  HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
87
        bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
88
        MINUS_ONE, FALSE),
89
  HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
90
        bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
91
        MINUS_ONE, FALSE),
92
  HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
93
        bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
94
        MINUS_ONE, FALSE),
95
  HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
96
        bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
97
        0xffffffff, TRUE),
98
  HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
99
        bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
100
        0xffffffff, TRUE),
101
  HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
102
        bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
103
        0xffffffff, FALSE),
104
  HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
105
        bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
106
        0xffffffff, TRUE),
107
  HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
108
        bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
109
        0xffffffff, FALSE),
110
  HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
111
        bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
112
        TRUE),
113
  HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
114
        bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
115
        FALSE, MINUS_ONE, MINUS_ONE, FALSE),
116
  HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
117
        bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
118
        FALSE, 0xffffffff, 0xffffffff, TRUE),
119
  HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
120
        bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
121
        FALSE),
122
  HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
123
        bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
124
        MINUS_ONE, TRUE),
125
  HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
126
        bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
127
        FALSE, MINUS_ONE, MINUS_ONE, TRUE),
128
  HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
129
        bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
130
        MINUS_ONE, FALSE),
131
  HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
132
        bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
133
        MINUS_ONE, FALSE),
134
  EMPTY_HOWTO (32),
135
  EMPTY_HOWTO (33),
136
  HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
137
        complain_overflow_bitfield, bfd_elf_generic_reloc,
138
        "R_X86_64_GOTPC32_TLSDESC",
139
        FALSE, 0xffffffff, 0xffffffff, TRUE),
140
  HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
141
        complain_overflow_dont, bfd_elf_generic_reloc,
142
        "R_X86_64_TLSDESC_CALL",
143
        FALSE, 0, 0, FALSE),
144
  HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
145
        complain_overflow_bitfield, bfd_elf_generic_reloc,
146
        "R_X86_64_TLSDESC",
147
        FALSE, MINUS_ONE, MINUS_ONE, FALSE),
148
  HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
149
        bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
150
        MINUS_ONE, FALSE),
151
 
152
  /* We have a gap in the reloc numbers here.
153
     R_X86_64_standard counts the number up to this point, and
154
     R_X86_64_vt_offset is the value to subtract from a reloc type of
155
     R_X86_64_GNU_VT* to form an index into this table.  */
156
#define R_X86_64_standard (R_X86_64_IRELATIVE + 1)
157
#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
158
 
159
/* GNU extension to record C++ vtable hierarchy.  */
160
  HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
161
         NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
162
 
163
/* GNU extension to record C++ vtable member usage.  */
164
  HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
165
         _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
166
         FALSE)
167
};
168
 
169
#define IS_X86_64_PCREL_TYPE(TYPE)      \
170
  (   ((TYPE) == R_X86_64_PC8)          \
171
   || ((TYPE) == R_X86_64_PC16)         \
172
   || ((TYPE) == R_X86_64_PC32)         \
173
   || ((TYPE) == R_X86_64_PC64))
174
 
175
/* Map BFD relocs to the x86_64 elf relocs.  */
176
struct elf_reloc_map
177
{
178
  bfd_reloc_code_real_type bfd_reloc_val;
179
  unsigned char elf_reloc_val;
180
};
181
 
182
static const struct elf_reloc_map x86_64_reloc_map[] =
183
{
184
  { BFD_RELOC_NONE,             R_X86_64_NONE, },
185
  { BFD_RELOC_64,               R_X86_64_64,   },
186
  { BFD_RELOC_32_PCREL,         R_X86_64_PC32, },
187
  { BFD_RELOC_X86_64_GOT32,     R_X86_64_GOT32,},
188
  { BFD_RELOC_X86_64_PLT32,     R_X86_64_PLT32,},
189
  { BFD_RELOC_X86_64_COPY,      R_X86_64_COPY, },
190
  { BFD_RELOC_X86_64_GLOB_DAT,  R_X86_64_GLOB_DAT, },
191
  { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
192
  { BFD_RELOC_X86_64_RELATIVE,  R_X86_64_RELATIVE, },
193
  { BFD_RELOC_X86_64_GOTPCREL,  R_X86_64_GOTPCREL, },
194
  { BFD_RELOC_32,               R_X86_64_32, },
195
  { BFD_RELOC_X86_64_32S,       R_X86_64_32S, },
196
  { BFD_RELOC_16,               R_X86_64_16, },
197
  { BFD_RELOC_16_PCREL,         R_X86_64_PC16, },
198
  { BFD_RELOC_8,                R_X86_64_8, },
199
  { BFD_RELOC_8_PCREL,          R_X86_64_PC8, },
200
  { BFD_RELOC_X86_64_DTPMOD64,  R_X86_64_DTPMOD64, },
201
  { BFD_RELOC_X86_64_DTPOFF64,  R_X86_64_DTPOFF64, },
202
  { BFD_RELOC_X86_64_TPOFF64,   R_X86_64_TPOFF64, },
203
  { BFD_RELOC_X86_64_TLSGD,     R_X86_64_TLSGD, },
204
  { BFD_RELOC_X86_64_TLSLD,     R_X86_64_TLSLD, },
205
  { BFD_RELOC_X86_64_DTPOFF32,  R_X86_64_DTPOFF32, },
206
  { BFD_RELOC_X86_64_GOTTPOFF,  R_X86_64_GOTTPOFF, },
207
  { BFD_RELOC_X86_64_TPOFF32,   R_X86_64_TPOFF32, },
208
  { BFD_RELOC_64_PCREL,         R_X86_64_PC64, },
209
  { BFD_RELOC_X86_64_GOTOFF64,  R_X86_64_GOTOFF64, },
210
  { BFD_RELOC_X86_64_GOTPC32,   R_X86_64_GOTPC32, },
211
  { BFD_RELOC_X86_64_GOT64,     R_X86_64_GOT64, },
212
  { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
213
  { BFD_RELOC_X86_64_GOTPC64,   R_X86_64_GOTPC64, },
214
  { BFD_RELOC_X86_64_GOTPLT64,  R_X86_64_GOTPLT64, },
215
  { BFD_RELOC_X86_64_PLTOFF64,  R_X86_64_PLTOFF64, },
216
  { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
217
  { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
218
  { BFD_RELOC_X86_64_TLSDESC,   R_X86_64_TLSDESC, },
219
  { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
220
  { BFD_RELOC_VTABLE_INHERIT,   R_X86_64_GNU_VTINHERIT, },
221
  { BFD_RELOC_VTABLE_ENTRY,     R_X86_64_GNU_VTENTRY, },
222
};
223
 
224
static reloc_howto_type *
225
elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
226
{
227
  unsigned i;
228
 
229
  if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
230
      || r_type >= (unsigned int) R_X86_64_max)
231
    {
232
      if (r_type >= (unsigned int) R_X86_64_standard)
233
        {
234
          (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
235
                                 abfd, (int) r_type);
236
          r_type = R_X86_64_NONE;
237
        }
238
      i = r_type;
239
    }
240
  else
241
    i = r_type - (unsigned int) R_X86_64_vt_offset;
242
  BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
243
  return &x86_64_elf_howto_table[i];
244
}
245
 
246
/* Given a BFD reloc type, return a HOWTO structure.  */
247
static reloc_howto_type *
248
elf64_x86_64_reloc_type_lookup (bfd *abfd,
249
                                bfd_reloc_code_real_type code)
250
{
251
  unsigned int i;
252
 
253
  for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
254
       i++)
255
    {
256
      if (x86_64_reloc_map[i].bfd_reloc_val == code)
257
        return elf64_x86_64_rtype_to_howto (abfd,
258
                                            x86_64_reloc_map[i].elf_reloc_val);
259
    }
260
  return 0;
261
}
262
 
263
static reloc_howto_type *
264
elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
265
                                const char *r_name)
266
{
267
  unsigned int i;
268
 
269
  for (i = 0;
270
       i < (sizeof (x86_64_elf_howto_table)
271
            / sizeof (x86_64_elf_howto_table[0]));
272
       i++)
273
    if (x86_64_elf_howto_table[i].name != NULL
274
        && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
275
      return &x86_64_elf_howto_table[i];
276
 
277
  return NULL;
278
}
279
 
280
/* Given an x86_64 ELF reloc type, fill in an arelent structure.  */
281
 
282
static void
283
elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
284
                            Elf_Internal_Rela *dst)
285
{
286
  unsigned r_type;
287
 
288
  r_type = ELF64_R_TYPE (dst->r_info);
289
  cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
290
  BFD_ASSERT (r_type == cache_ptr->howto->type);
291
}
292
 
293
/* Support for core dump NOTE sections.  */
294
static bfd_boolean
295
elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
296
{
297
  int offset;
298
  size_t size;
299
 
300
  switch (note->descsz)
301
    {
302
      default:
303
        return FALSE;
304
 
305
      case 336:         /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
306
        /* pr_cursig */
307
        elf_tdata (abfd)->core_signal
308
          = bfd_get_16 (abfd, note->descdata + 12);
309
 
310
        /* pr_pid */
311
        elf_tdata (abfd)->core_pid
312
          = bfd_get_32 (abfd, note->descdata + 32);
313
 
314
        /* pr_reg */
315
        offset = 112;
316
        size = 216;
317
 
318
        break;
319
    }
320
 
321
  /* Make a ".reg/999" section.  */
322
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
323
                                          size, note->descpos + offset);
324
}
325
 
326
static bfd_boolean
327
elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
328
{
329
  switch (note->descsz)
330
    {
331
      default:
332
        return FALSE;
333
 
334
      case 136:         /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
335
        elf_tdata (abfd)->core_program
336
         = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
337
        elf_tdata (abfd)->core_command
338
         = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
339
    }
340
 
341
  /* Note that for some reason, a spurious space is tacked
342
     onto the end of the args in some (at least one anyway)
343
     implementations, so strip it off if it exists.  */
344
 
345
  {
346
    char *command = elf_tdata (abfd)->core_command;
347
    int n = strlen (command);
348
 
349
    if (0 < n && command[n - 1] == ' ')
350
      command[n - 1] = '\0';
351
  }
352
 
353
  return TRUE;
354
}
355
 
356
/* Functions for the x86-64 ELF linker.  */
357
 
358
/* The name of the dynamic interpreter.  This is put in the .interp
359
   section.  */
360
 
361
#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
362
 
363
/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
364
   copying dynamic variables from a shared lib into an app's dynbss
365
   section, and instead use a dynamic relocation to point into the
366
   shared lib.  */
367
#define ELIMINATE_COPY_RELOCS 1
368
 
369
/* The size in bytes of an entry in the global offset table.  */
370
 
371
#define GOT_ENTRY_SIZE 8
372
 
373
/* The size in bytes of an entry in the procedure linkage table.  */
374
 
375
#define PLT_ENTRY_SIZE 16
376
 
377
/* The first entry in a procedure linkage table looks like this.  See the
378
   SVR4 ABI i386 supplement and the x86-64 ABI to see how this works.  */
379
 
380
static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
381
{
382
  0xff, 0x35, 8, 0, 0, 0,  /* pushq GOT+8(%rip)  */
383
  0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
384
  0x0f, 0x1f, 0x40, 0x00        /* nopl 0(%rax)       */
385
};
386
 
387
/* Subsequent entries in a procedure linkage table look like this.  */
388
 
389
static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
390
{
391
  0xff, 0x25,   /* jmpq *name@GOTPC(%rip) */
392
  0, 0, 0, 0,       /* replaced with offset to this symbol in .got.  */
393
  0x68,         /* pushq immediate */
394
  0, 0, 0, 0,       /* replaced with index into relocation table.  */
395
  0xe9,         /* jmp relative */
396
  0, 0, 0, 0        /* replaced with offset to start of .plt0.  */
397
};
398
 
399
/* x86-64 ELF linker hash entry.  */
400
 
401
struct elf64_x86_64_link_hash_entry
402
{
403
  struct elf_link_hash_entry elf;
404
 
405
  /* Track dynamic relocs copied for this symbol.  */
406
  struct elf_dyn_relocs *dyn_relocs;
407
 
408
#define GOT_UNKNOWN     0
409
#define GOT_NORMAL      1
410
#define GOT_TLS_GD      2
411
#define GOT_TLS_IE      3
412
#define GOT_TLS_GDESC   4
413
#define GOT_TLS_GD_BOTH_P(type) \
414
  ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
415
#define GOT_TLS_GD_P(type) \
416
  ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
417
#define GOT_TLS_GDESC_P(type) \
418
  ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
419
#define GOT_TLS_GD_ANY_P(type) \
420
  (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
421
  unsigned char tls_type;
422
 
423
  /* Offset of the GOTPLT entry reserved for the TLS descriptor,
424
     starting at the end of the jump table.  */
425
  bfd_vma tlsdesc_got;
426
};
427
 
428
#define elf64_x86_64_hash_entry(ent) \
429
  ((struct elf64_x86_64_link_hash_entry *)(ent))
430
 
431
struct elf64_x86_64_obj_tdata
432
{
433
  struct elf_obj_tdata root;
434
 
435
  /* tls_type for each local got entry.  */
436
  char *local_got_tls_type;
437
 
438
  /* GOTPLT entries for TLS descriptors.  */
439
  bfd_vma *local_tlsdesc_gotent;
440
};
441
 
442
#define elf64_x86_64_tdata(abfd) \
443
  ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
444
 
445
#define elf64_x86_64_local_got_tls_type(abfd) \
446
  (elf64_x86_64_tdata (abfd)->local_got_tls_type)
447
 
448
#define elf64_x86_64_local_tlsdesc_gotent(abfd) \
449
  (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
450
 
451
#define is_x86_64_elf(bfd)                              \
452
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
453
   && elf_tdata (bfd) != NULL                           \
454
   && elf_object_id (bfd) == X86_64_ELF_DATA)
455
 
456
static bfd_boolean
457
elf64_x86_64_mkobject (bfd *abfd)
458
{
459
  return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata),
460
                                  X86_64_ELF_DATA);
461
}
462
 
463
/* x86-64 ELF linker hash table.  */
464
 
465
struct elf64_x86_64_link_hash_table
466
{
467
  struct elf_link_hash_table elf;
468
 
469
  /* Short-cuts to get to dynamic linker sections.  */
470
  asection *sdynbss;
471
  asection *srelbss;
472
 
473
  union
474
  {
475
    bfd_signed_vma refcount;
476
    bfd_vma offset;
477
  } tls_ld_got;
478
 
479
  /* The amount of space used by the jump slots in the GOT.  */
480
  bfd_vma sgotplt_jump_table_size;
481
 
482
  /* Small local sym cache.  */
483
  struct sym_cache sym_cache;
484
 
485
  /* _TLS_MODULE_BASE_ symbol.  */
486
  struct bfd_link_hash_entry *tls_module_base;
487
 
488
  /* Used by local STT_GNU_IFUNC symbols.  */
489
  htab_t loc_hash_table;
490
  void * loc_hash_memory;
491
 
492
  /* The offset into splt of the PLT entry for the TLS descriptor
493
     resolver.  Special values are 0, if not necessary (or not found
494
     to be necessary yet), and -1 if needed but not determined
495
     yet.  */
496
  bfd_vma tlsdesc_plt;
497
  /* The offset into sgot of the GOT entry used by the PLT entry
498
     above.  */
499
  bfd_vma tlsdesc_got;
500
};
501
 
502
/* Get the x86-64 ELF linker hash table from a link_info structure.  */
503
 
504
#define elf64_x86_64_hash_table(p) \
505
  (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
506
  == X86_64_ELF_DATA ? ((struct elf64_x86_64_link_hash_table *) ((p)->hash)) : NULL)
507
 
508
#define elf64_x86_64_compute_jump_table_size(htab) \
509
  ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
510
 
511
/* Create an entry in an x86-64 ELF linker hash table.  */
512
 
513
static struct bfd_hash_entry *
514
elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
515
                                struct bfd_hash_table *table,
516
                                const char *string)
517
{
518
  /* Allocate the structure if it has not already been allocated by a
519
     subclass.  */
520
  if (entry == NULL)
521
    {
522
      entry = (struct bfd_hash_entry *)
523
          bfd_hash_allocate (table,
524
                             sizeof (struct elf64_x86_64_link_hash_entry));
525
      if (entry == NULL)
526
        return entry;
527
    }
528
 
529
  /* Call the allocation method of the superclass.  */
530
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
531
  if (entry != NULL)
532
    {
533
      struct elf64_x86_64_link_hash_entry *eh;
534
 
535
      eh = (struct elf64_x86_64_link_hash_entry *) entry;
536
      eh->dyn_relocs = NULL;
537
      eh->tls_type = GOT_UNKNOWN;
538
      eh->tlsdesc_got = (bfd_vma) -1;
539
    }
540
 
541
  return entry;
542
}
543
 
544
/* Compute a hash of a local hash entry.  We use elf_link_hash_entry
545
  for local symbol so that we can handle local STT_GNU_IFUNC symbols
546
  as global symbol.  We reuse indx and dynstr_index for local symbol
547
  hash since they aren't used by global symbols in this backend.  */
548
 
549
static hashval_t
550
elf64_x86_64_local_htab_hash (const void *ptr)
551
{
552
  struct elf_link_hash_entry *h
553
    = (struct elf_link_hash_entry *) ptr;
554
  return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
555
}
556
 
557
/* Compare local hash entries.  */
558
 
559
static int
560
elf64_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
561
{
562
  struct elf_link_hash_entry *h1
563
     = (struct elf_link_hash_entry *) ptr1;
564
  struct elf_link_hash_entry *h2
565
    = (struct elf_link_hash_entry *) ptr2;
566
 
567
  return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
568
}
569
 
570
/* Find and/or create a hash entry for local symbol.  */
571
 
572
static struct elf_link_hash_entry *
573
elf64_x86_64_get_local_sym_hash (struct elf64_x86_64_link_hash_table *htab,
574
                                 bfd *abfd, const Elf_Internal_Rela *rel,
575
                                 bfd_boolean create)
576
{
577
  struct elf64_x86_64_link_hash_entry e, *ret;
578
  asection *sec = abfd->sections;
579
  hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
580
                                       ELF64_R_SYM (rel->r_info));
581
  void **slot;
582
 
583
  e.elf.indx = sec->id;
584
  e.elf.dynstr_index = ELF64_R_SYM (rel->r_info);
585
  slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
586
                                   create ? INSERT : NO_INSERT);
587
 
588
  if (!slot)
589
    return NULL;
590
 
591
  if (*slot)
592
    {
593
      ret = (struct elf64_x86_64_link_hash_entry *) *slot;
594
      return &ret->elf;
595
    }
596
 
597
  ret = (struct elf64_x86_64_link_hash_entry *)
598
        objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
599
                        sizeof (struct elf64_x86_64_link_hash_entry));
600
  if (ret)
601
    {
602
      memset (ret, 0, sizeof (*ret));
603
      ret->elf.indx = sec->id;
604
      ret->elf.dynstr_index = ELF64_R_SYM (rel->r_info);
605
      ret->elf.dynindx = -1;
606
      ret->elf.plt.offset = (bfd_vma) -1;
607
      ret->elf.got.offset = (bfd_vma) -1;
608
      *slot = ret;
609
    }
610
  return &ret->elf;
611
}
612
 
613
/* Create an X86-64 ELF linker hash table.  */
614
 
615
static struct bfd_link_hash_table *
616
elf64_x86_64_link_hash_table_create (bfd *abfd)
617
{
618
  struct elf64_x86_64_link_hash_table *ret;
619
  bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
620
 
621
  ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
622
  if (ret == NULL)
623
    return NULL;
624
 
625
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
626
                                      elf64_x86_64_link_hash_newfunc,
627
                                      sizeof (struct elf64_x86_64_link_hash_entry),
628
                                      X86_64_ELF_DATA))
629
    {
630
      free (ret);
631
      return NULL;
632
    }
633
 
634
  ret->sdynbss = NULL;
635
  ret->srelbss = NULL;
636
  ret->sym_cache.abfd = NULL;
637
  ret->tlsdesc_plt = 0;
638
  ret->tlsdesc_got = 0;
639
  ret->tls_ld_got.refcount = 0;
640
  ret->sgotplt_jump_table_size = 0;
641
  ret->tls_module_base = NULL;
642
 
643
  ret->loc_hash_table = htab_try_create (1024,
644
                                         elf64_x86_64_local_htab_hash,
645
                                         elf64_x86_64_local_htab_eq,
646
                                         NULL);
647
  ret->loc_hash_memory = objalloc_create ();
648
  if (!ret->loc_hash_table || !ret->loc_hash_memory)
649
    {
650
      free (ret);
651
      return NULL;
652
    }
653
 
654
  return &ret->elf.root;
655
}
656
 
657
/* Destroy an X86-64 ELF linker hash table.  */
658
 
659
static void
660
elf64_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
661
{
662
  struct elf64_x86_64_link_hash_table *htab
663
    = (struct elf64_x86_64_link_hash_table *) hash;
664
 
665
  if (htab->loc_hash_table)
666
    htab_delete (htab->loc_hash_table);
667
  if (htab->loc_hash_memory)
668
    objalloc_free ((struct objalloc *) htab->loc_hash_memory);
669
  _bfd_generic_link_hash_table_free (hash);
670
}
671
 
672
/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
673
   .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
674
   hash table.  */
675
 
676
static bfd_boolean
677
elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
678
{
679
  struct elf64_x86_64_link_hash_table *htab;
680
 
681
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
682
    return FALSE;
683
 
684
  htab = elf64_x86_64_hash_table (info);
685
  if (htab == NULL)
686
    return FALSE;
687
 
688
  htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
689
  if (!info->shared)
690
    htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
691
 
692
  if (!htab->sdynbss
693
      || (!info->shared && !htab->srelbss))
694
    abort ();
695
 
696
  return TRUE;
697
}
698
 
699
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
700
 
701
static void
702
elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
703
                                   struct elf_link_hash_entry *dir,
704
                                   struct elf_link_hash_entry *ind)
705
{
706
  struct elf64_x86_64_link_hash_entry *edir, *eind;
707
 
708
  edir = (struct elf64_x86_64_link_hash_entry *) dir;
709
  eind = (struct elf64_x86_64_link_hash_entry *) ind;
710
 
711
  if (eind->dyn_relocs != NULL)
712
    {
713
      if (edir->dyn_relocs != NULL)
714
        {
715
          struct elf_dyn_relocs **pp;
716
          struct elf_dyn_relocs *p;
717
 
718
          /* Add reloc counts against the indirect sym to the direct sym
719
             list.  Merge any entries against the same section.  */
720
          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
721
            {
722
              struct elf_dyn_relocs *q;
723
 
724
              for (q = edir->dyn_relocs; q != NULL; q = q->next)
725
                if (q->sec == p->sec)
726
                  {
727
                    q->pc_count += p->pc_count;
728
                    q->count += p->count;
729
                    *pp = p->next;
730
                    break;
731
                  }
732
              if (q == NULL)
733
                pp = &p->next;
734
            }
735
          *pp = edir->dyn_relocs;
736
        }
737
 
738
      edir->dyn_relocs = eind->dyn_relocs;
739
      eind->dyn_relocs = NULL;
740
    }
741
 
742
  if (ind->root.type == bfd_link_hash_indirect
743
      && dir->got.refcount <= 0)
744
    {
745
      edir->tls_type = eind->tls_type;
746
      eind->tls_type = GOT_UNKNOWN;
747
    }
748
 
749
  if (ELIMINATE_COPY_RELOCS
750
      && ind->root.type != bfd_link_hash_indirect
751
      && dir->dynamic_adjusted)
752
    {
753
      /* If called to transfer flags for a weakdef during processing
754
         of elf_adjust_dynamic_symbol, don't copy non_got_ref.
755
         We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
756
      dir->ref_dynamic |= ind->ref_dynamic;
757
      dir->ref_regular |= ind->ref_regular;
758
      dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
759
      dir->needs_plt |= ind->needs_plt;
760
      dir->pointer_equality_needed |= ind->pointer_equality_needed;
761
    }
762
  else
763
    _bfd_elf_link_hash_copy_indirect (info, dir, ind);
764
}
765
 
766
static bfd_boolean
767
elf64_x86_64_elf_object_p (bfd *abfd)
768
{
769
  /* Set the right machine number for an x86-64 elf64 file.  */
770
  bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
771
  return TRUE;
772
}
773
 
774
typedef union
775
  {
776
    unsigned char c[2];
777
    uint16_t i;
778
  }
779
x86_64_opcode16;
780
 
781
typedef union
782
  {
783
    unsigned char c[4];
784
    uint32_t i;
785
  }
786
x86_64_opcode32;
787
 
788
/* Return TRUE if the TLS access code sequence support transition
789
   from R_TYPE.  */
790
 
791
static bfd_boolean
792
elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
793
                                   bfd_byte *contents,
794
                                   Elf_Internal_Shdr *symtab_hdr,
795
                                   struct elf_link_hash_entry **sym_hashes,
796
                                   unsigned int r_type,
797
                                   const Elf_Internal_Rela *rel,
798
                                   const Elf_Internal_Rela *relend)
799
{
800
  unsigned int val;
801
  unsigned long r_symndx;
802
  struct elf_link_hash_entry *h;
803
  bfd_vma offset;
804
 
805
  /* Get the section contents.  */
806
  if (contents == NULL)
807
    {
808
      if (elf_section_data (sec)->this_hdr.contents != NULL)
809
        contents = elf_section_data (sec)->this_hdr.contents;
810
      else
811
        {
812
          /* FIXME: How to better handle error condition?  */
813
          if (!bfd_malloc_and_get_section (abfd, sec, &contents))
814
            return FALSE;
815
 
816
          /* Cache the section contents for elf_link_input_bfd.  */
817
          elf_section_data (sec)->this_hdr.contents = contents;
818
        }
819
    }
820
 
821
  offset = rel->r_offset;
822
  switch (r_type)
823
    {
824
    case R_X86_64_TLSGD:
825
    case R_X86_64_TLSLD:
826
      if ((rel + 1) >= relend)
827
        return FALSE;
828
 
829
      if (r_type == R_X86_64_TLSGD)
830
        {
831
          /* Check transition from GD access model.  Only
832
                .byte 0x66; leaq foo@tlsgd(%rip), %rdi
833
                .word 0x6666; rex64; call __tls_get_addr
834
             can transit to different access model.  */
835
 
836
          static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
837
                                 call = { { 0x66, 0x66, 0x48, 0xe8 } };
838
          if (offset < 4
839
              || (offset + 12) > sec->size
840
              || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
841
              || bfd_get_32 (abfd, contents + offset + 4) != call.i)
842
            return FALSE;
843
        }
844
      else
845
        {
846
          /* Check transition from LD access model.  Only
847
                leaq foo@tlsld(%rip), %rdi;
848
                call __tls_get_addr
849
             can transit to different access model.  */
850
 
851
          static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
852
          x86_64_opcode32 op;
853
 
854
          if (offset < 3 || (offset + 9) > sec->size)
855
            return FALSE;
856
 
857
          op.i = bfd_get_32 (abfd, contents + offset - 3);
858
          op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
859
          if (op.i != ld.i)
860
            return FALSE;
861
        }
862
 
863
      r_symndx = ELF64_R_SYM (rel[1].r_info);
864
      if (r_symndx < symtab_hdr->sh_info)
865
        return FALSE;
866
 
867
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
868
      /* Use strncmp to check __tls_get_addr since __tls_get_addr
869
         may be versioned.  */
870
      return (h != NULL
871
              && h->root.root.string != NULL
872
              && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
873
                  || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
874
              && (strncmp (h->root.root.string,
875
                           "__tls_get_addr", 14) == 0));
876
 
877
    case R_X86_64_GOTTPOFF:
878
      /* Check transition from IE access model:
879
                movq foo@gottpoff(%rip), %reg
880
                addq foo@gottpoff(%rip), %reg
881
       */
882
 
883
      if (offset < 3 || (offset + 4) > sec->size)
884
        return FALSE;
885
 
886
      val = bfd_get_8 (abfd, contents + offset - 3);
887
      if (val != 0x48 && val != 0x4c)
888
        return FALSE;
889
 
890
      val = bfd_get_8 (abfd, contents + offset - 2);
891
      if (val != 0x8b && val != 0x03)
892
        return FALSE;
893
 
894
      val = bfd_get_8 (abfd, contents + offset - 1);
895
      return (val & 0xc7) == 5;
896
 
897
    case R_X86_64_GOTPC32_TLSDESC:
898
      /* Check transition from GDesc access model:
899
                leaq x@tlsdesc(%rip), %rax
900
 
901
         Make sure it's a leaq adding rip to a 32-bit offset
902
         into any register, although it's probably almost always
903
         going to be rax.  */
904
 
905
      if (offset < 3 || (offset + 4) > sec->size)
906
        return FALSE;
907
 
908
      val = bfd_get_8 (abfd, contents + offset - 3);
909
      if ((val & 0xfb) != 0x48)
910
        return FALSE;
911
 
912
      if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
913
        return FALSE;
914
 
915
      val = bfd_get_8 (abfd, contents + offset - 1);
916
      return (val & 0xc7) == 0x05;
917
 
918
    case R_X86_64_TLSDESC_CALL:
919
      /* Check transition from GDesc access model:
920
                call *x@tlsdesc(%rax)
921
       */
922
      if (offset + 2 <= sec->size)
923
        {
924
          /* Make sure that it's a call *x@tlsdesc(%rax).  */
925
          static x86_64_opcode16 call = { { 0xff, 0x10 } };
926
          return bfd_get_16 (abfd, contents + offset) == call.i;
927
        }
928
 
929
      return FALSE;
930
 
931
    default:
932
      abort ();
933
    }
934
}
935
 
936
/* Return TRUE if the TLS access transition is OK or no transition
937
   will be performed.  Update R_TYPE if there is a transition.  */
938
 
939
static bfd_boolean
940
elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
941
                             asection *sec, bfd_byte *contents,
942
                             Elf_Internal_Shdr *symtab_hdr,
943
                             struct elf_link_hash_entry **sym_hashes,
944
                             unsigned int *r_type, int tls_type,
945
                             const Elf_Internal_Rela *rel,
946
                             const Elf_Internal_Rela *relend,
947
                             struct elf_link_hash_entry *h,
948
                             unsigned long r_symndx)
949
{
950
  unsigned int from_type = *r_type;
951
  unsigned int to_type = from_type;
952
  bfd_boolean check = TRUE;
953
 
954
  switch (from_type)
955
    {
956
    case R_X86_64_TLSGD:
957
    case R_X86_64_GOTPC32_TLSDESC:
958
    case R_X86_64_TLSDESC_CALL:
959
    case R_X86_64_GOTTPOFF:
960
      if (info->executable)
961
        {
962
          if (h == NULL)
963
            to_type = R_X86_64_TPOFF32;
964
          else
965
            to_type = R_X86_64_GOTTPOFF;
966
        }
967
 
968
      /* When we are called from elf64_x86_64_relocate_section,
969
         CONTENTS isn't NULL and there may be additional transitions
970
         based on TLS_TYPE.  */
971
      if (contents != NULL)
972
        {
973
          unsigned int new_to_type = to_type;
974
 
975
          if (info->executable
976
              && h != NULL
977
              && h->dynindx == -1
978
              && tls_type == GOT_TLS_IE)
979
            new_to_type = R_X86_64_TPOFF32;
980
 
981
          if (to_type == R_X86_64_TLSGD
982
              || to_type == R_X86_64_GOTPC32_TLSDESC
983
              || to_type == R_X86_64_TLSDESC_CALL)
984
            {
985
              if (tls_type == GOT_TLS_IE)
986
                new_to_type = R_X86_64_GOTTPOFF;
987
            }
988
 
989
          /* We checked the transition before when we were called from
990
             elf64_x86_64_check_relocs.  We only want to check the new
991
             transition which hasn't been checked before.  */
992
          check = new_to_type != to_type && from_type == to_type;
993
          to_type = new_to_type;
994
        }
995
 
996
      break;
997
 
998
    case R_X86_64_TLSLD:
999
      if (info->executable)
1000
        to_type = R_X86_64_TPOFF32;
1001
      break;
1002
 
1003
    default:
1004
      return TRUE;
1005
    }
1006
 
1007
  /* Return TRUE if there is no transition.  */
1008
  if (from_type == to_type)
1009
    return TRUE;
1010
 
1011
  /* Check if the transition can be performed.  */
1012
  if (check
1013
      && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
1014
                                              symtab_hdr, sym_hashes,
1015
                                              from_type, rel, relend))
1016
    {
1017
      reloc_howto_type *from, *to;
1018
      const char *name;
1019
 
1020
      from = elf64_x86_64_rtype_to_howto (abfd, from_type);
1021
      to = elf64_x86_64_rtype_to_howto (abfd, to_type);
1022
 
1023
      if (h)
1024
        name = h->root.root.string;
1025
      else
1026
        {
1027
          struct elf64_x86_64_link_hash_table *htab;
1028
 
1029
          htab = elf64_x86_64_hash_table (info);
1030
          if (htab == NULL)
1031
            name = "*unknown*";
1032
          else
1033
            {
1034
              Elf_Internal_Sym *isym;
1035
 
1036
              isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1037
                                            abfd, r_symndx);
1038
              name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1039
            }
1040
        }
1041
 
1042
      (*_bfd_error_handler)
1043
        (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1044
           "in section `%A' failed"),
1045
         abfd, sec, from->name, to->name, name,
1046
         (unsigned long) rel->r_offset);
1047
      bfd_set_error (bfd_error_bad_value);
1048
      return FALSE;
1049
    }
1050
 
1051
  *r_type = to_type;
1052
  return TRUE;
1053
}
1054
 
1055
/* Look through the relocs for a section during the first phase, and
1056
   calculate needed space in the global offset table, procedure
1057
   linkage table, and dynamic reloc sections.  */
1058
 
1059
static bfd_boolean
1060
elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1061
                           asection *sec,
1062
                           const Elf_Internal_Rela *relocs)
1063
{
1064
  struct elf64_x86_64_link_hash_table *htab;
1065
  Elf_Internal_Shdr *symtab_hdr;
1066
  struct elf_link_hash_entry **sym_hashes;
1067
  const Elf_Internal_Rela *rel;
1068
  const Elf_Internal_Rela *rel_end;
1069
  asection *sreloc;
1070
 
1071
  if (info->relocatable)
1072
    return TRUE;
1073
 
1074
  BFD_ASSERT (is_x86_64_elf (abfd));
1075
 
1076
  htab = elf64_x86_64_hash_table (info);
1077
  if (htab == NULL)
1078
    return FALSE;
1079
 
1080
  symtab_hdr = &elf_symtab_hdr (abfd);
1081
  sym_hashes = elf_sym_hashes (abfd);
1082
 
1083
  sreloc = NULL;
1084
 
1085
  rel_end = relocs + sec->reloc_count;
1086
  for (rel = relocs; rel < rel_end; rel++)
1087
    {
1088
      unsigned int r_type;
1089
      unsigned long r_symndx;
1090
      struct elf_link_hash_entry *h;
1091
      Elf_Internal_Sym *isym;
1092
      const char *name;
1093
 
1094
      r_symndx = ELF64_R_SYM (rel->r_info);
1095
      r_type = ELF64_R_TYPE (rel->r_info);
1096
 
1097
      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1098
        {
1099
          (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1100
                                 abfd, r_symndx);
1101
          return FALSE;
1102
        }
1103
 
1104
      if (r_symndx < symtab_hdr->sh_info)
1105
        {
1106
          /* A local symbol.  */
1107
          isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1108
                                        abfd, r_symndx);
1109
          if (isym == NULL)
1110
            return FALSE;
1111
 
1112
          /* Check relocation against local STT_GNU_IFUNC symbol.  */
1113
          if (ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1114
            {
1115
              h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1116
                                                   TRUE);
1117
              if (h == NULL)
1118
                return FALSE;
1119
 
1120
              /* Fake a STT_GNU_IFUNC symbol.  */
1121
              h->type = STT_GNU_IFUNC;
1122
              h->def_regular = 1;
1123
              h->ref_regular = 1;
1124
              h->forced_local = 1;
1125
              h->root.type = bfd_link_hash_defined;
1126
            }
1127
          else
1128
            h = NULL;
1129
        }
1130
      else
1131
        {
1132
          isym = NULL;
1133
          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1134
          while (h->root.type == bfd_link_hash_indirect
1135
                 || h->root.type == bfd_link_hash_warning)
1136
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
1137
        }
1138
 
1139
      if (h != NULL)
1140
        {
1141
          /* Create the ifunc sections for static executables.  If we
1142
             never see an indirect function symbol nor we are building
1143
             a static executable, those sections will be empty and
1144
             won't appear in output.  */
1145
          switch (r_type)
1146
            {
1147
            default:
1148
              break;
1149
 
1150
            case R_X86_64_32S:
1151
            case R_X86_64_32:
1152
            case R_X86_64_64:
1153
            case R_X86_64_PC32:
1154
            case R_X86_64_PC64:
1155
            case R_X86_64_PLT32:
1156
            case R_X86_64_GOTPCREL:
1157
            case R_X86_64_GOTPCREL64:
1158
              if (!_bfd_elf_create_ifunc_sections (abfd, info))
1159
                return FALSE;
1160
              break;
1161
            }
1162
 
1163
          /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1164
             it here if it is defined in a non-shared object.  */
1165
          if (h->type == STT_GNU_IFUNC
1166
              && h->def_regular)
1167
            {
1168
              /* It is referenced by a non-shared object. */
1169
              h->ref_regular = 1;
1170
              h->needs_plt = 1;
1171
 
1172
              /* STT_GNU_IFUNC symbol must go through PLT.  */
1173
              h->plt.refcount += 1;
1174
 
1175
              /* STT_GNU_IFUNC needs dynamic sections.  */
1176
              if (htab->elf.dynobj == NULL)
1177
                htab->elf.dynobj = abfd;
1178
 
1179
              switch (r_type)
1180
                {
1181
                default:
1182
                  if (h->root.root.string)
1183
                    name = h->root.root.string;
1184
                  else
1185
                    name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1186
                                             NULL);
1187
                  (*_bfd_error_handler)
1188
                    (_("%B: relocation %s against STT_GNU_IFUNC "
1189
                       "symbol `%s' isn't handled by %s"), abfd,
1190
                     x86_64_elf_howto_table[r_type].name,
1191
                     name, __FUNCTION__);
1192
                  bfd_set_error (bfd_error_bad_value);
1193
                  return FALSE;
1194
 
1195
                case R_X86_64_64:
1196
                  h->non_got_ref = 1;
1197
                  h->pointer_equality_needed = 1;
1198
                  if (info->shared)
1199
                    {
1200
                      /* We must copy these reloc types into the output
1201
                         file.  Create a reloc section in dynobj and
1202
                         make room for this reloc.  */
1203
                      sreloc = _bfd_elf_create_ifunc_dyn_reloc
1204
                        (abfd, info, sec, sreloc,
1205
                         &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs);
1206
                      if (sreloc == NULL)
1207
                        return FALSE;
1208
                    }
1209
                  break;
1210
 
1211
                case R_X86_64_32S:
1212
                case R_X86_64_32:
1213
                case R_X86_64_PC32:
1214
                case R_X86_64_PC64:
1215
                  h->non_got_ref = 1;
1216
                  if (r_type != R_X86_64_PC32
1217
                      && r_type != R_X86_64_PC64)
1218
                    h->pointer_equality_needed = 1;
1219
                  break;
1220
 
1221
                case R_X86_64_PLT32:
1222
                  break;
1223
 
1224
                case R_X86_64_GOTPCREL:
1225
                case R_X86_64_GOTPCREL64:
1226
                  h->got.refcount += 1;
1227
                  if (htab->elf.sgot == NULL
1228
                      && !_bfd_elf_create_got_section (htab->elf.dynobj,
1229
                                                       info))
1230
                    return FALSE;
1231
                  break;
1232
                }
1233
 
1234
              continue;
1235
            }
1236
        }
1237
 
1238
      if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1239
                                         symtab_hdr, sym_hashes,
1240
                                         &r_type, GOT_UNKNOWN,
1241
                                         rel, rel_end, h, r_symndx))
1242
        return FALSE;
1243
 
1244
      switch (r_type)
1245
        {
1246
        case R_X86_64_TLSLD:
1247
          htab->tls_ld_got.refcount += 1;
1248
          goto create_got;
1249
 
1250
        case R_X86_64_TPOFF32:
1251
          if (!info->executable)
1252
            {
1253
              if (h)
1254
                name = h->root.root.string;
1255
              else
1256
                name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1257
                                         NULL);
1258
              (*_bfd_error_handler)
1259
                (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1260
                 abfd,
1261
                 x86_64_elf_howto_table[r_type].name, name);
1262
              bfd_set_error (bfd_error_bad_value);
1263
              return FALSE;
1264
            }
1265
          break;
1266
 
1267
        case R_X86_64_GOTTPOFF:
1268
          if (!info->executable)
1269
            info->flags |= DF_STATIC_TLS;
1270
          /* Fall through */
1271
 
1272
        case R_X86_64_GOT32:
1273
        case R_X86_64_GOTPCREL:
1274
        case R_X86_64_TLSGD:
1275
        case R_X86_64_GOT64:
1276
        case R_X86_64_GOTPCREL64:
1277
        case R_X86_64_GOTPLT64:
1278
        case R_X86_64_GOTPC32_TLSDESC:
1279
        case R_X86_64_TLSDESC_CALL:
1280
          /* This symbol requires a global offset table entry.  */
1281
          {
1282
            int tls_type, old_tls_type;
1283
 
1284
            switch (r_type)
1285
              {
1286
              default: tls_type = GOT_NORMAL; break;
1287
              case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1288
              case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1289
              case R_X86_64_GOTPC32_TLSDESC:
1290
              case R_X86_64_TLSDESC_CALL:
1291
                tls_type = GOT_TLS_GDESC; break;
1292
              }
1293
 
1294
            if (h != NULL)
1295
              {
1296
                if (r_type == R_X86_64_GOTPLT64)
1297
                  {
1298
                    /* This relocation indicates that we also need
1299
                       a PLT entry, as this is a function.  We don't need
1300
                       a PLT entry for local symbols.  */
1301
                    h->needs_plt = 1;
1302
                    h->plt.refcount += 1;
1303
                  }
1304
                h->got.refcount += 1;
1305
                old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1306
              }
1307
            else
1308
              {
1309
                bfd_signed_vma *local_got_refcounts;
1310
 
1311
                /* This is a global offset table entry for a local symbol.  */
1312
                local_got_refcounts = elf_local_got_refcounts (abfd);
1313
                if (local_got_refcounts == NULL)
1314
                  {
1315
                    bfd_size_type size;
1316
 
1317
                    size = symtab_hdr->sh_info;
1318
                    size *= sizeof (bfd_signed_vma)
1319
                      + sizeof (bfd_vma) + sizeof (char);
1320
                    local_got_refcounts = ((bfd_signed_vma *)
1321
                                           bfd_zalloc (abfd, size));
1322
                    if (local_got_refcounts == NULL)
1323
                      return FALSE;
1324
                    elf_local_got_refcounts (abfd) = local_got_refcounts;
1325
                    elf64_x86_64_local_tlsdesc_gotent (abfd)
1326
                      = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1327
                    elf64_x86_64_local_got_tls_type (abfd)
1328
                      = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1329
                  }
1330
                local_got_refcounts[r_symndx] += 1;
1331
                old_tls_type
1332
                  = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1333
              }
1334
 
1335
            /* If a TLS symbol is accessed using IE at least once,
1336
               there is no point to use dynamic model for it.  */
1337
            if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1338
                && (! GOT_TLS_GD_ANY_P (old_tls_type)
1339
                    || tls_type != GOT_TLS_IE))
1340
              {
1341
                if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1342
                  tls_type = old_tls_type;
1343
                else if (GOT_TLS_GD_ANY_P (old_tls_type)
1344
                         && GOT_TLS_GD_ANY_P (tls_type))
1345
                  tls_type |= old_tls_type;
1346
                else
1347
                  {
1348
                    if (h)
1349
                      name = h->root.root.string;
1350
                    else
1351
                      name = bfd_elf_sym_name (abfd, symtab_hdr,
1352
                                               isym, NULL);
1353
                    (*_bfd_error_handler)
1354
                      (_("%B: '%s' accessed both as normal and thread local symbol"),
1355
                       abfd, name);
1356
                    return FALSE;
1357
                  }
1358
              }
1359
 
1360
            if (old_tls_type != tls_type)
1361
              {
1362
                if (h != NULL)
1363
                  elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1364
                else
1365
                  elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1366
              }
1367
          }
1368
          /* Fall through */
1369
 
1370
        case R_X86_64_GOTOFF64:
1371
        case R_X86_64_GOTPC32:
1372
        case R_X86_64_GOTPC64:
1373
        create_got:
1374
          if (htab->elf.sgot == NULL)
1375
            {
1376
              if (htab->elf.dynobj == NULL)
1377
                htab->elf.dynobj = abfd;
1378
              if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1379
                                                info))
1380
                return FALSE;
1381
            }
1382
          break;
1383
 
1384
        case R_X86_64_PLT32:
1385
          /* This symbol requires a procedure linkage table entry.  We
1386
             actually build the entry in adjust_dynamic_symbol,
1387
             because this might be a case of linking PIC code which is
1388
             never referenced by a dynamic object, in which case we
1389
             don't need to generate a procedure linkage table entry
1390
             after all.  */
1391
 
1392
          /* If this is a local symbol, we resolve it directly without
1393
             creating a procedure linkage table entry.  */
1394
          if (h == NULL)
1395
            continue;
1396
 
1397
          h->needs_plt = 1;
1398
          h->plt.refcount += 1;
1399
          break;
1400
 
1401
        case R_X86_64_PLTOFF64:
1402
          /* This tries to form the 'address' of a function relative
1403
             to GOT.  For global symbols we need a PLT entry.  */
1404
          if (h != NULL)
1405
            {
1406
              h->needs_plt = 1;
1407
              h->plt.refcount += 1;
1408
            }
1409
          goto create_got;
1410
 
1411
        case R_X86_64_8:
1412
        case R_X86_64_16:
1413
        case R_X86_64_32:
1414
        case R_X86_64_32S:
1415
          /* Let's help debug shared library creation.  These relocs
1416
             cannot be used in shared libs.  Don't error out for
1417
             sections we don't care about, such as debug sections or
1418
             non-constant sections.  */
1419
          if (info->shared
1420
              && (sec->flags & SEC_ALLOC) != 0
1421
              && (sec->flags & SEC_READONLY) != 0)
1422
            {
1423
              if (h)
1424
                name = h->root.root.string;
1425
              else
1426
                name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1427
              (*_bfd_error_handler)
1428
                (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1429
                 abfd, x86_64_elf_howto_table[r_type].name, name);
1430
              bfd_set_error (bfd_error_bad_value);
1431
              return FALSE;
1432
            }
1433
          /* Fall through.  */
1434
 
1435
        case R_X86_64_PC8:
1436
        case R_X86_64_PC16:
1437
        case R_X86_64_PC32:
1438
        case R_X86_64_PC64:
1439
        case R_X86_64_64:
1440
          if (h != NULL && info->executable)
1441
            {
1442
              /* If this reloc is in a read-only section, we might
1443
                 need a copy reloc.  We can't check reliably at this
1444
                 stage whether the section is read-only, as input
1445
                 sections have not yet been mapped to output sections.
1446
                 Tentatively set the flag for now, and correct in
1447
                 adjust_dynamic_symbol.  */
1448
              h->non_got_ref = 1;
1449
 
1450
              /* We may need a .plt entry if the function this reloc
1451
                 refers to is in a shared lib.  */
1452
              h->plt.refcount += 1;
1453
              if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1454
                h->pointer_equality_needed = 1;
1455
            }
1456
 
1457
          /* If we are creating a shared library, and this is a reloc
1458
             against a global symbol, or a non PC relative reloc
1459
             against a local symbol, then we need to copy the reloc
1460
             into the shared library.  However, if we are linking with
1461
             -Bsymbolic, we do not need to copy a reloc against a
1462
             global symbol which is defined in an object we are
1463
             including in the link (i.e., DEF_REGULAR is set).  At
1464
             this point we have not seen all the input files, so it is
1465
             possible that DEF_REGULAR is not set now but will be set
1466
             later (it is never cleared).  In case of a weak definition,
1467
             DEF_REGULAR may be cleared later by a strong definition in
1468
             a shared library.  We account for that possibility below by
1469
             storing information in the relocs_copied field of the hash
1470
             table entry.  A similar situation occurs when creating
1471
             shared libraries and symbol visibility changes render the
1472
             symbol local.
1473
 
1474
             If on the other hand, we are creating an executable, we
1475
             may need to keep relocations for symbols satisfied by a
1476
             dynamic library if we manage to avoid copy relocs for the
1477
             symbol.  */
1478
          if ((info->shared
1479
               && (sec->flags & SEC_ALLOC) != 0
1480
               && (! IS_X86_64_PCREL_TYPE (r_type)
1481
                   || (h != NULL
1482
                       && (! SYMBOLIC_BIND (info, h)
1483
                           || h->root.type == bfd_link_hash_defweak
1484
                           || !h->def_regular))))
1485
              || (ELIMINATE_COPY_RELOCS
1486
                  && !info->shared
1487
                  && (sec->flags & SEC_ALLOC) != 0
1488
                  && h != NULL
1489
                  && (h->root.type == bfd_link_hash_defweak
1490
                      || !h->def_regular)))
1491
            {
1492
              struct elf_dyn_relocs *p;
1493
              struct elf_dyn_relocs **head;
1494
 
1495
              /* We must copy these reloc types into the output file.
1496
                 Create a reloc section in dynobj and make room for
1497
                 this reloc.  */
1498
              if (sreloc == NULL)
1499
                {
1500
                  if (htab->elf.dynobj == NULL)
1501
                    htab->elf.dynobj = abfd;
1502
 
1503
                  sreloc = _bfd_elf_make_dynamic_reloc_section
1504
                    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1505
 
1506
                  if (sreloc == NULL)
1507
                    return FALSE;
1508
                }
1509
 
1510
              /* If this is a global symbol, we count the number of
1511
                 relocations we need for this symbol.  */
1512
              if (h != NULL)
1513
                {
1514
                  head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1515
                }
1516
              else
1517
                {
1518
                  /* Track dynamic relocs needed for local syms too.
1519
                     We really need local syms available to do this
1520
                     easily.  Oh well.  */
1521
                  asection *s;
1522
                  void **vpp;
1523
 
1524
                  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1525
                                                abfd, r_symndx);
1526
                  if (isym == NULL)
1527
                    return FALSE;
1528
 
1529
                  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1530
                  if (s == NULL)
1531
                    s = sec;
1532
 
1533
                  /* Beware of type punned pointers vs strict aliasing
1534
                     rules.  */
1535
                  vpp = &(elf_section_data (s)->local_dynrel);
1536
                  head = (struct elf_dyn_relocs **)vpp;
1537
                }
1538
 
1539
              p = *head;
1540
              if (p == NULL || p->sec != sec)
1541
                {
1542
                  bfd_size_type amt = sizeof *p;
1543
 
1544
                  p = ((struct elf_dyn_relocs *)
1545
                       bfd_alloc (htab->elf.dynobj, amt));
1546
                  if (p == NULL)
1547
                    return FALSE;
1548
                  p->next = *head;
1549
                  *head = p;
1550
                  p->sec = sec;
1551
                  p->count = 0;
1552
                  p->pc_count = 0;
1553
                }
1554
 
1555
              p->count += 1;
1556
              if (IS_X86_64_PCREL_TYPE (r_type))
1557
                p->pc_count += 1;
1558
            }
1559
          break;
1560
 
1561
          /* This relocation describes the C++ object vtable hierarchy.
1562
             Reconstruct it for later use during GC.  */
1563
        case R_X86_64_GNU_VTINHERIT:
1564
          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1565
            return FALSE;
1566
          break;
1567
 
1568
          /* This relocation describes which C++ vtable entries are actually
1569
             used.  Record for later use during GC.  */
1570
        case R_X86_64_GNU_VTENTRY:
1571
          BFD_ASSERT (h != NULL);
1572
          if (h != NULL
1573
              && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1574
            return FALSE;
1575
          break;
1576
 
1577
        default:
1578
          break;
1579
        }
1580
    }
1581
 
1582
  return TRUE;
1583
}
1584
 
1585
/* Return the section that should be marked against GC for a given
1586
   relocation.  */
1587
 
1588
static asection *
1589
elf64_x86_64_gc_mark_hook (asection *sec,
1590
                           struct bfd_link_info *info,
1591
                           Elf_Internal_Rela *rel,
1592
                           struct elf_link_hash_entry *h,
1593
                           Elf_Internal_Sym *sym)
1594
{
1595
  if (h != NULL)
1596
    switch (ELF64_R_TYPE (rel->r_info))
1597
      {
1598
      case R_X86_64_GNU_VTINHERIT:
1599
      case R_X86_64_GNU_VTENTRY:
1600
        return NULL;
1601
      }
1602
 
1603
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1604
}
1605
 
1606
/* Update the got entry reference counts for the section being removed.  */
1607
 
1608
static bfd_boolean
1609
elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1610
                            asection *sec,
1611
                            const Elf_Internal_Rela *relocs)
1612
{
1613
  struct elf64_x86_64_link_hash_table *htab;
1614
  Elf_Internal_Shdr *symtab_hdr;
1615
  struct elf_link_hash_entry **sym_hashes;
1616
  bfd_signed_vma *local_got_refcounts;
1617
  const Elf_Internal_Rela *rel, *relend;
1618
 
1619
  if (info->relocatable)
1620
    return TRUE;
1621
 
1622
  htab = elf64_x86_64_hash_table (info);
1623
  if (htab == NULL)
1624
    return FALSE;
1625
 
1626
  elf_section_data (sec)->local_dynrel = NULL;
1627
 
1628
  symtab_hdr = &elf_symtab_hdr (abfd);
1629
  sym_hashes = elf_sym_hashes (abfd);
1630
  local_got_refcounts = elf_local_got_refcounts (abfd);
1631
 
1632
  relend = relocs + sec->reloc_count;
1633
  for (rel = relocs; rel < relend; rel++)
1634
    {
1635
      unsigned long r_symndx;
1636
      unsigned int r_type;
1637
      struct elf_link_hash_entry *h = NULL;
1638
 
1639
      r_symndx = ELF64_R_SYM (rel->r_info);
1640
      if (r_symndx >= symtab_hdr->sh_info)
1641
        {
1642
          struct elf64_x86_64_link_hash_entry *eh;
1643
          struct elf_dyn_relocs **pp;
1644
          struct elf_dyn_relocs *p;
1645
 
1646
          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1647
          while (h->root.type == bfd_link_hash_indirect
1648
                 || h->root.type == bfd_link_hash_warning)
1649
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
1650
          eh = (struct elf64_x86_64_link_hash_entry *) h;
1651
 
1652
          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1653
            if (p->sec == sec)
1654
              {
1655
                /* Everything must go for SEC.  */
1656
                *pp = p->next;
1657
                break;
1658
              }
1659
        }
1660
 
1661
      r_type = ELF64_R_TYPE (rel->r_info);
1662
      if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1663
                                         symtab_hdr, sym_hashes,
1664
                                         &r_type, GOT_UNKNOWN,
1665
                                         rel, relend, h, r_symndx))
1666
        return FALSE;
1667
 
1668
      switch (r_type)
1669
        {
1670
        case R_X86_64_TLSLD:
1671
          if (htab->tls_ld_got.refcount > 0)
1672
            htab->tls_ld_got.refcount -= 1;
1673
          break;
1674
 
1675
        case R_X86_64_TLSGD:
1676
        case R_X86_64_GOTPC32_TLSDESC:
1677
        case R_X86_64_TLSDESC_CALL:
1678
        case R_X86_64_GOTTPOFF:
1679
        case R_X86_64_GOT32:
1680
        case R_X86_64_GOTPCREL:
1681
        case R_X86_64_GOT64:
1682
        case R_X86_64_GOTPCREL64:
1683
        case R_X86_64_GOTPLT64:
1684
          if (h != NULL)
1685
            {
1686
              if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1687
                h->plt.refcount -= 1;
1688
              if (h->got.refcount > 0)
1689
                h->got.refcount -= 1;
1690
            }
1691
          else if (local_got_refcounts != NULL)
1692
            {
1693
              if (local_got_refcounts[r_symndx] > 0)
1694
                local_got_refcounts[r_symndx] -= 1;
1695
            }
1696
          break;
1697
 
1698
        case R_X86_64_8:
1699
        case R_X86_64_16:
1700
        case R_X86_64_32:
1701
        case R_X86_64_64:
1702
        case R_X86_64_32S:
1703
        case R_X86_64_PC8:
1704
        case R_X86_64_PC16:
1705
        case R_X86_64_PC32:
1706
        case R_X86_64_PC64:
1707
          if (info->shared)
1708
            break;
1709
          /* Fall thru */
1710
 
1711
        case R_X86_64_PLT32:
1712
        case R_X86_64_PLTOFF64:
1713
          if (h != NULL)
1714
            {
1715
              if (h->plt.refcount > 0)
1716
                h->plt.refcount -= 1;
1717
            }
1718
          break;
1719
 
1720
        default:
1721
          break;
1722
        }
1723
    }
1724
 
1725
  return TRUE;
1726
}
1727
 
1728
/* Adjust a symbol defined by a dynamic object and referenced by a
1729
   regular object.  The current definition is in some section of the
1730
   dynamic object, but we're not including those sections.  We have to
1731
   change the definition to something the rest of the link can
1732
   understand.  */
1733
 
1734
static bfd_boolean
1735
elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1736
                                    struct elf_link_hash_entry *h)
1737
{
1738
  struct elf64_x86_64_link_hash_table *htab;
1739
  asection *s;
1740
 
1741
  /* STT_GNU_IFUNC symbol must go through PLT. */
1742
  if (h->type == STT_GNU_IFUNC)
1743
    {
1744
      if (h->plt.refcount <= 0)
1745
        {
1746
          h->plt.offset = (bfd_vma) -1;
1747
          h->needs_plt = 0;
1748
        }
1749
      return TRUE;
1750
    }
1751
 
1752
  /* If this is a function, put it in the procedure linkage table.  We
1753
     will fill in the contents of the procedure linkage table later,
1754
     when we know the address of the .got section.  */
1755
  if (h->type == STT_FUNC
1756
      || h->needs_plt)
1757
    {
1758
      if (h->plt.refcount <= 0
1759
          || SYMBOL_CALLS_LOCAL (info, h)
1760
          || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1761
              && h->root.type == bfd_link_hash_undefweak))
1762
        {
1763
          /* This case can occur if we saw a PLT32 reloc in an input
1764
             file, but the symbol was never referred to by a dynamic
1765
             object, or if all references were garbage collected.  In
1766
             such a case, we don't actually need to build a procedure
1767
             linkage table, and we can just do a PC32 reloc instead.  */
1768
          h->plt.offset = (bfd_vma) -1;
1769
          h->needs_plt = 0;
1770
        }
1771
 
1772
      return TRUE;
1773
    }
1774
  else
1775
    /* It's possible that we incorrectly decided a .plt reloc was
1776
       needed for an R_X86_64_PC32 reloc to a non-function sym in
1777
       check_relocs.  We can't decide accurately between function and
1778
       non-function syms in check-relocs;  Objects loaded later in
1779
       the link may change h->type.  So fix it now.  */
1780
    h->plt.offset = (bfd_vma) -1;
1781
 
1782
  /* If this is a weak symbol, and there is a real definition, the
1783
     processor independent code will have arranged for us to see the
1784
     real definition first, and we can just use the same value.  */
1785
  if (h->u.weakdef != NULL)
1786
    {
1787
      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1788
                  || h->u.weakdef->root.type == bfd_link_hash_defweak);
1789
      h->root.u.def.section = h->u.weakdef->root.u.def.section;
1790
      h->root.u.def.value = h->u.weakdef->root.u.def.value;
1791
      if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1792
        h->non_got_ref = h->u.weakdef->non_got_ref;
1793
      return TRUE;
1794
    }
1795
 
1796
  /* This is a reference to a symbol defined by a dynamic object which
1797
     is not a function.  */
1798
 
1799
  /* If we are creating a shared library, we must presume that the
1800
     only references to the symbol are via the global offset table.
1801
     For such cases we need not do anything here; the relocations will
1802
     be handled correctly by relocate_section.  */
1803
  if (info->shared)
1804
    return TRUE;
1805
 
1806
  /* If there are no references to this symbol that do not use the
1807
     GOT, we don't need to generate a copy reloc.  */
1808
  if (!h->non_got_ref)
1809
    return TRUE;
1810
 
1811
  /* If -z nocopyreloc was given, we won't generate them either.  */
1812
  if (info->nocopyreloc)
1813
    {
1814
      h->non_got_ref = 0;
1815
      return TRUE;
1816
    }
1817
 
1818
  if (ELIMINATE_COPY_RELOCS)
1819
    {
1820
      struct elf64_x86_64_link_hash_entry * eh;
1821
      struct elf_dyn_relocs *p;
1822
 
1823
      eh = (struct elf64_x86_64_link_hash_entry *) h;
1824
      for (p = eh->dyn_relocs; p != NULL; p = p->next)
1825
        {
1826
          s = p->sec->output_section;
1827
          if (s != NULL && (s->flags & SEC_READONLY) != 0)
1828
            break;
1829
        }
1830
 
1831
      /* If we didn't find any dynamic relocs in read-only sections, then
1832
         we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1833
      if (p == NULL)
1834
        {
1835
          h->non_got_ref = 0;
1836
          return TRUE;
1837
        }
1838
    }
1839
 
1840
  if (h->size == 0)
1841
    {
1842
      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1843
                             h->root.root.string);
1844
      return TRUE;
1845
    }
1846
 
1847
  /* We must allocate the symbol in our .dynbss section, which will
1848
     become part of the .bss section of the executable.  There will be
1849
     an entry for this symbol in the .dynsym section.  The dynamic
1850
     object will contain position independent code, so all references
1851
     from the dynamic object to this symbol will go through the global
1852
     offset table.  The dynamic linker will use the .dynsym entry to
1853
     determine the address it must put in the global offset table, so
1854
     both the dynamic object and the regular object will refer to the
1855
     same memory location for the variable.  */
1856
 
1857
  htab = elf64_x86_64_hash_table (info);
1858
  if (htab == NULL)
1859
    return FALSE;
1860
 
1861
  /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1862
     to copy the initial value out of the dynamic object and into the
1863
     runtime process image.  */
1864
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1865
    {
1866
      htab->srelbss->size += sizeof (Elf64_External_Rela);
1867
      h->needs_copy = 1;
1868
    }
1869
 
1870
  s = htab->sdynbss;
1871
 
1872
  return _bfd_elf_adjust_dynamic_copy (h, s);
1873
}
1874
 
1875
/* Allocate space in .plt, .got and associated reloc sections for
1876
   dynamic relocs.  */
1877
 
1878
static bfd_boolean
1879
elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1880
{
1881
  struct bfd_link_info *info;
1882
  struct elf64_x86_64_link_hash_table *htab;
1883
  struct elf64_x86_64_link_hash_entry *eh;
1884
  struct elf_dyn_relocs *p;
1885
 
1886
  if (h->root.type == bfd_link_hash_indirect)
1887
    return TRUE;
1888
 
1889
  if (h->root.type == bfd_link_hash_warning)
1890
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1891
  eh = (struct elf64_x86_64_link_hash_entry *) h;
1892
 
1893
  info = (struct bfd_link_info *) inf;
1894
  htab = elf64_x86_64_hash_table (info);
1895
  if (htab == NULL)
1896
    return FALSE;
1897
 
1898
  /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1899
     here if it is defined and referenced in a non-shared object.  */
1900
  if (h->type == STT_GNU_IFUNC
1901
      && h->def_regular)
1902
    return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1903
                                               &eh->dyn_relocs,
1904
                                               PLT_ENTRY_SIZE,
1905
                                               GOT_ENTRY_SIZE);
1906
  else if (htab->elf.dynamic_sections_created
1907
           && h->plt.refcount > 0)
1908
    {
1909
      /* Make sure this symbol is output as a dynamic symbol.
1910
         Undefined weak syms won't yet be marked as dynamic.  */
1911
      if (h->dynindx == -1
1912
          && !h->forced_local)
1913
        {
1914
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
1915
            return FALSE;
1916
        }
1917
 
1918
      if (info->shared
1919
          || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1920
        {
1921
          asection *s = htab->elf.splt;
1922
 
1923
          /* If this is the first .plt entry, make room for the special
1924
             first entry.  */
1925
          if (s->size == 0)
1926
            s->size += PLT_ENTRY_SIZE;
1927
 
1928
          h->plt.offset = s->size;
1929
 
1930
          /* If this symbol is not defined in a regular file, and we are
1931
             not generating a shared library, then set the symbol to this
1932
             location in the .plt.  This is required to make function
1933
             pointers compare as equal between the normal executable and
1934
             the shared library.  */
1935
          if (! info->shared
1936
              && !h->def_regular)
1937
            {
1938
              h->root.u.def.section = s;
1939
              h->root.u.def.value = h->plt.offset;
1940
            }
1941
 
1942
          /* Make room for this entry.  */
1943
          s->size += PLT_ENTRY_SIZE;
1944
 
1945
          /* We also need to make an entry in the .got.plt section, which
1946
             will be placed in the .got section by the linker script.  */
1947
          htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1948
 
1949
          /* We also need to make an entry in the .rela.plt section.  */
1950
          htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1951
          htab->elf.srelplt->reloc_count++;
1952
        }
1953
      else
1954
        {
1955
          h->plt.offset = (bfd_vma) -1;
1956
          h->needs_plt = 0;
1957
        }
1958
    }
1959
  else
1960
    {
1961
      h->plt.offset = (bfd_vma) -1;
1962
      h->needs_plt = 0;
1963
    }
1964
 
1965
  eh->tlsdesc_got = (bfd_vma) -1;
1966
 
1967
  /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1968
     make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
1969
  if (h->got.refcount > 0
1970
      && info->executable
1971
      && h->dynindx == -1
1972
      && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1973
    {
1974
      h->got.offset = (bfd_vma) -1;
1975
    }
1976
  else if (h->got.refcount > 0)
1977
    {
1978
      asection *s;
1979
      bfd_boolean dyn;
1980
      int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1981
 
1982
      /* Make sure this symbol is output as a dynamic symbol.
1983
         Undefined weak syms won't yet be marked as dynamic.  */
1984
      if (h->dynindx == -1
1985
          && !h->forced_local)
1986
        {
1987
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
1988
            return FALSE;
1989
        }
1990
 
1991
      if (GOT_TLS_GDESC_P (tls_type))
1992
        {
1993
          eh->tlsdesc_got = htab->elf.sgotplt->size
1994
            - elf64_x86_64_compute_jump_table_size (htab);
1995
          htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
1996
          h->got.offset = (bfd_vma) -2;
1997
        }
1998
      if (! GOT_TLS_GDESC_P (tls_type)
1999
          || GOT_TLS_GD_P (tls_type))
2000
        {
2001
          s = htab->elf.sgot;
2002
          h->got.offset = s->size;
2003
          s->size += GOT_ENTRY_SIZE;
2004
          if (GOT_TLS_GD_P (tls_type))
2005
            s->size += GOT_ENTRY_SIZE;
2006
        }
2007
      dyn = htab->elf.dynamic_sections_created;
2008
      /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2009
         and two if global.
2010
         R_X86_64_GOTTPOFF needs one dynamic relocation.  */
2011
      if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2012
          || tls_type == GOT_TLS_IE)
2013
        htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2014
      else if (GOT_TLS_GD_P (tls_type))
2015
        htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
2016
      else if (! GOT_TLS_GDESC_P (tls_type)
2017
               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2018
                   || h->root.type != bfd_link_hash_undefweak)
2019
               && (info->shared
2020
                   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2021
        htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2022
      if (GOT_TLS_GDESC_P (tls_type))
2023
        {
2024
          htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
2025
          htab->tlsdesc_plt = (bfd_vma) -1;
2026
        }
2027
    }
2028
  else
2029
    h->got.offset = (bfd_vma) -1;
2030
 
2031
  if (eh->dyn_relocs == NULL)
2032
    return TRUE;
2033
 
2034
  /* In the shared -Bsymbolic case, discard space allocated for
2035
     dynamic pc-relative relocs against symbols which turn out to be
2036
     defined in regular objects.  For the normal shared case, discard
2037
     space for pc-relative relocs that have become local due to symbol
2038
     visibility changes.  */
2039
 
2040
  if (info->shared)
2041
    {
2042
      /* Relocs that use pc_count are those that appear on a call
2043
         insn, or certain REL relocs that can generated via assembly.
2044
         We want calls to protected symbols to resolve directly to the
2045
         function rather than going via the plt.  If people want
2046
         function pointer comparisons to work as expected then they
2047
         should avoid writing weird assembly.  */
2048
      if (SYMBOL_CALLS_LOCAL (info, h))
2049
        {
2050
          struct elf_dyn_relocs **pp;
2051
 
2052
          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2053
            {
2054
              p->count -= p->pc_count;
2055
              p->pc_count = 0;
2056
              if (p->count == 0)
2057
                *pp = p->next;
2058
              else
2059
                pp = &p->next;
2060
            }
2061
        }
2062
 
2063
      /* Also discard relocs on undefined weak syms with non-default
2064
         visibility.  */
2065
      if (eh->dyn_relocs != NULL
2066
          && h->root.type == bfd_link_hash_undefweak)
2067
        {
2068
          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2069
            eh->dyn_relocs = NULL;
2070
 
2071
          /* Make sure undefined weak symbols are output as a dynamic
2072
             symbol in PIEs.  */
2073
          else if (h->dynindx == -1
2074
                   && ! h->forced_local
2075
                   && ! bfd_elf_link_record_dynamic_symbol (info, h))
2076
            return FALSE;
2077
        }
2078
 
2079
    }
2080
  else if (ELIMINATE_COPY_RELOCS)
2081
    {
2082
      /* For the non-shared case, discard space for relocs against
2083
         symbols which turn out to need copy relocs or are not
2084
         dynamic.  */
2085
 
2086
      if (!h->non_got_ref
2087
          && ((h->def_dynamic
2088
               && !h->def_regular)
2089
              || (htab->elf.dynamic_sections_created
2090
                  && (h->root.type == bfd_link_hash_undefweak
2091
                      || h->root.type == bfd_link_hash_undefined))))
2092
        {
2093
          /* Make sure this symbol is output as a dynamic symbol.
2094
             Undefined weak syms won't yet be marked as dynamic.  */
2095
          if (h->dynindx == -1
2096
              && ! h->forced_local
2097
              && ! bfd_elf_link_record_dynamic_symbol (info, h))
2098
            return FALSE;
2099
 
2100
          /* If that succeeded, we know we'll be keeping all the
2101
             relocs.  */
2102
          if (h->dynindx != -1)
2103
            goto keep;
2104
        }
2105
 
2106
      eh->dyn_relocs = NULL;
2107
 
2108
    keep: ;
2109
    }
2110
 
2111
  /* Finally, allocate space.  */
2112
  for (p = eh->dyn_relocs; p != NULL; p = p->next)
2113
    {
2114
      asection * sreloc;
2115
 
2116
      sreloc = elf_section_data (p->sec)->sreloc;
2117
 
2118
      BFD_ASSERT (sreloc != NULL);
2119
 
2120
      sreloc->size += p->count * sizeof (Elf64_External_Rela);
2121
    }
2122
 
2123
  return TRUE;
2124
}
2125
 
2126
/* Allocate space in .plt, .got and associated reloc sections for
2127
   local dynamic relocs.  */
2128
 
2129
static bfd_boolean
2130
elf64_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2131
{
2132
  struct elf_link_hash_entry *h
2133
    = (struct elf_link_hash_entry *) *slot;
2134
 
2135
  if (h->type != STT_GNU_IFUNC
2136
      || !h->def_regular
2137
      || !h->ref_regular
2138
      || !h->forced_local
2139
      || h->root.type != bfd_link_hash_defined)
2140
    abort ();
2141
 
2142
  return elf64_x86_64_allocate_dynrelocs (h, inf);
2143
}
2144
 
2145
/* Find any dynamic relocs that apply to read-only sections.  */
2146
 
2147
static bfd_boolean
2148
elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2149
{
2150
  struct elf64_x86_64_link_hash_entry *eh;
2151
  struct elf_dyn_relocs *p;
2152
 
2153
  if (h->root.type == bfd_link_hash_warning)
2154
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2155
 
2156
  eh = (struct elf64_x86_64_link_hash_entry *) h;
2157
  for (p = eh->dyn_relocs; p != NULL; p = p->next)
2158
    {
2159
      asection *s = p->sec->output_section;
2160
 
2161
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
2162
        {
2163
          struct bfd_link_info *info = (struct bfd_link_info *) inf;
2164
 
2165
          info->flags |= DF_TEXTREL;
2166
 
2167
          /* Not an error, just cut short the traversal.  */
2168
          return FALSE;
2169
        }
2170
    }
2171
  return TRUE;
2172
}
2173
 
2174
/* Set the sizes of the dynamic sections.  */
2175
 
2176
static bfd_boolean
2177
elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2178
                                    struct bfd_link_info *info)
2179
{
2180
  struct elf64_x86_64_link_hash_table *htab;
2181
  bfd *dynobj;
2182
  asection *s;
2183
  bfd_boolean relocs;
2184
  bfd *ibfd;
2185
 
2186
  htab = elf64_x86_64_hash_table (info);
2187
  if (htab == NULL)
2188
    return FALSE;
2189
 
2190
  dynobj = htab->elf.dynobj;
2191
  if (dynobj == NULL)
2192
    abort ();
2193
 
2194
  if (htab->elf.dynamic_sections_created)
2195
    {
2196
      /* Set the contents of the .interp section to the interpreter.  */
2197
      if (info->executable)
2198
        {
2199
          s = bfd_get_section_by_name (dynobj, ".interp");
2200
          if (s == NULL)
2201
            abort ();
2202
          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2203
          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2204
        }
2205
    }
2206
 
2207
  /* Set up .got offsets for local syms, and space for local dynamic
2208
     relocs.  */
2209
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2210
    {
2211
      bfd_signed_vma *local_got;
2212
      bfd_signed_vma *end_local_got;
2213
      char *local_tls_type;
2214
      bfd_vma *local_tlsdesc_gotent;
2215
      bfd_size_type locsymcount;
2216
      Elf_Internal_Shdr *symtab_hdr;
2217
      asection *srel;
2218
 
2219
      if (! is_x86_64_elf (ibfd))
2220
        continue;
2221
 
2222
      for (s = ibfd->sections; s != NULL; s = s->next)
2223
        {
2224
          struct elf_dyn_relocs *p;
2225
 
2226
          for (p = (struct elf_dyn_relocs *)
2227
                    (elf_section_data (s)->local_dynrel);
2228
               p != NULL;
2229
               p = p->next)
2230
            {
2231
              if (!bfd_is_abs_section (p->sec)
2232
                  && bfd_is_abs_section (p->sec->output_section))
2233
                {
2234
                  /* Input section has been discarded, either because
2235
                     it is a copy of a linkonce section or due to
2236
                     linker script /DISCARD/, so we'll be discarding
2237
                     the relocs too.  */
2238
                }
2239
              else if (p->count != 0)
2240
                {
2241
                  srel = elf_section_data (p->sec)->sreloc;
2242
                  srel->size += p->count * sizeof (Elf64_External_Rela);
2243
                  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2244
                    info->flags |= DF_TEXTREL;
2245
                }
2246
            }
2247
        }
2248
 
2249
      local_got = elf_local_got_refcounts (ibfd);
2250
      if (!local_got)
2251
        continue;
2252
 
2253
      symtab_hdr = &elf_symtab_hdr (ibfd);
2254
      locsymcount = symtab_hdr->sh_info;
2255
      end_local_got = local_got + locsymcount;
2256
      local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2257
      local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2258
      s = htab->elf.sgot;
2259
      srel = htab->elf.srelgot;
2260
      for (; local_got < end_local_got;
2261
           ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2262
        {
2263
          *local_tlsdesc_gotent = (bfd_vma) -1;
2264
          if (*local_got > 0)
2265
            {
2266
              if (GOT_TLS_GDESC_P (*local_tls_type))
2267
                {
2268
                  *local_tlsdesc_gotent = htab->elf.sgotplt->size
2269
                    - elf64_x86_64_compute_jump_table_size (htab);
2270
                  htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2271
                  *local_got = (bfd_vma) -2;
2272
                }
2273
              if (! GOT_TLS_GDESC_P (*local_tls_type)
2274
                  || GOT_TLS_GD_P (*local_tls_type))
2275
                {
2276
                  *local_got = s->size;
2277
                  s->size += GOT_ENTRY_SIZE;
2278
                  if (GOT_TLS_GD_P (*local_tls_type))
2279
                    s->size += GOT_ENTRY_SIZE;
2280
                }
2281
              if (info->shared
2282
                  || GOT_TLS_GD_ANY_P (*local_tls_type)
2283
                  || *local_tls_type == GOT_TLS_IE)
2284
                {
2285
                  if (GOT_TLS_GDESC_P (*local_tls_type))
2286
                    {
2287
                      htab->elf.srelplt->size
2288
                        += sizeof (Elf64_External_Rela);
2289
                      htab->tlsdesc_plt = (bfd_vma) -1;
2290
                    }
2291
                  if (! GOT_TLS_GDESC_P (*local_tls_type)
2292
                      || GOT_TLS_GD_P (*local_tls_type))
2293
                    srel->size += sizeof (Elf64_External_Rela);
2294
                }
2295
            }
2296
          else
2297
            *local_got = (bfd_vma) -1;
2298
        }
2299
    }
2300
 
2301
  if (htab->tls_ld_got.refcount > 0)
2302
    {
2303
      /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2304
         relocs.  */
2305
      htab->tls_ld_got.offset = htab->elf.sgot->size;
2306
      htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2307
      htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2308
    }
2309
  else
2310
    htab->tls_ld_got.offset = -1;
2311
 
2312
  /* Allocate global sym .plt and .got entries, and space for global
2313
     sym dynamic relocs.  */
2314
  elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2315
                          info);
2316
 
2317
  /* Allocate .plt and .got entries, and space for local symbols.  */
2318
  htab_traverse (htab->loc_hash_table,
2319
                 elf64_x86_64_allocate_local_dynrelocs,
2320
                 info);
2321
 
2322
  /* For every jump slot reserved in the sgotplt, reloc_count is
2323
     incremented.  However, when we reserve space for TLS descriptors,
2324
     it's not incremented, so in order to compute the space reserved
2325
     for them, it suffices to multiply the reloc count by the jump
2326
     slot size.  */
2327
  if (htab->elf.srelplt)
2328
    htab->sgotplt_jump_table_size
2329
      = elf64_x86_64_compute_jump_table_size (htab);
2330
 
2331
  if (htab->tlsdesc_plt)
2332
    {
2333
      /* If we're not using lazy TLS relocations, don't generate the
2334
         PLT and GOT entries they require.  */
2335
      if ((info->flags & DF_BIND_NOW))
2336
        htab->tlsdesc_plt = 0;
2337
      else
2338
        {
2339
          htab->tlsdesc_got = htab->elf.sgot->size;
2340
          htab->elf.sgot->size += GOT_ENTRY_SIZE;
2341
          /* Reserve room for the initial entry.
2342
             FIXME: we could probably do away with it in this case.  */
2343
          if (htab->elf.splt->size == 0)
2344
            htab->elf.splt->size += PLT_ENTRY_SIZE;
2345
          htab->tlsdesc_plt = htab->elf.splt->size;
2346
          htab->elf.splt->size += PLT_ENTRY_SIZE;
2347
        }
2348
    }
2349
 
2350
  /* We now have determined the sizes of the various dynamic sections.
2351
     Allocate memory for them.  */
2352
  relocs = FALSE;
2353
  for (s = dynobj->sections; s != NULL; s = s->next)
2354
    {
2355
      if ((s->flags & SEC_LINKER_CREATED) == 0)
2356
        continue;
2357
 
2358
      if (s == htab->elf.splt
2359
          || s == htab->elf.sgot
2360
          || s == htab->elf.sgotplt
2361
          || s == htab->elf.iplt
2362
          || s == htab->elf.igotplt
2363
          || s == htab->sdynbss)
2364
        {
2365
          /* Strip this section if we don't need it; see the
2366
             comment below.  */
2367
        }
2368
      else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2369
        {
2370
          if (s->size != 0 && s != htab->elf.srelplt)
2371
            relocs = TRUE;
2372
 
2373
          /* We use the reloc_count field as a counter if we need
2374
             to copy relocs into the output file.  */
2375
          if (s != htab->elf.srelplt)
2376
            s->reloc_count = 0;
2377
        }
2378
      else
2379
        {
2380
          /* It's not one of our sections, so don't allocate space.  */
2381
          continue;
2382
        }
2383
 
2384
      if (s->size == 0)
2385
        {
2386
          /* If we don't need this section, strip it from the
2387
             output file.  This is mostly to handle .rela.bss and
2388
             .rela.plt.  We must create both sections in
2389
             create_dynamic_sections, because they must be created
2390
             before the linker maps input sections to output
2391
             sections.  The linker does that before
2392
             adjust_dynamic_symbol is called, and it is that
2393
             function which decides whether anything needs to go
2394
             into these sections.  */
2395
 
2396
          s->flags |= SEC_EXCLUDE;
2397
          continue;
2398
        }
2399
 
2400
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
2401
        continue;
2402
 
2403
      /* Allocate memory for the section contents.  We use bfd_zalloc
2404
         here in case unused entries are not reclaimed before the
2405
         section's contents are written out.  This should not happen,
2406
         but this way if it does, we get a R_X86_64_NONE reloc instead
2407
         of garbage.  */
2408
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2409
      if (s->contents == NULL)
2410
        return FALSE;
2411
    }
2412
 
2413
  if (htab->elf.dynamic_sections_created)
2414
    {
2415
      /* Add some entries to the .dynamic section.  We fill in the
2416
         values later, in elf64_x86_64_finish_dynamic_sections, but we
2417
         must add the entries now so that we get the correct size for
2418
         the .dynamic section.  The DT_DEBUG entry is filled in by the
2419
         dynamic linker and used by the debugger.  */
2420
#define add_dynamic_entry(TAG, VAL) \
2421
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2422
 
2423
      if (info->executable)
2424
        {
2425
          if (!add_dynamic_entry (DT_DEBUG, 0))
2426
            return FALSE;
2427
        }
2428
 
2429
      if (htab->elf.splt->size != 0)
2430
        {
2431
          if (!add_dynamic_entry (DT_PLTGOT, 0)
2432
              || !add_dynamic_entry (DT_PLTRELSZ, 0)
2433
              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2434
              || !add_dynamic_entry (DT_JMPREL, 0))
2435
            return FALSE;
2436
 
2437
          if (htab->tlsdesc_plt
2438
              && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2439
                  || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2440
            return FALSE;
2441
        }
2442
 
2443
      if (relocs)
2444
        {
2445
          if (!add_dynamic_entry (DT_RELA, 0)
2446
              || !add_dynamic_entry (DT_RELASZ, 0)
2447
              || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2448
            return FALSE;
2449
 
2450
          /* If any dynamic relocs apply to a read-only section,
2451
             then we need a DT_TEXTREL entry.  */
2452
          if ((info->flags & DF_TEXTREL) == 0)
2453
            elf_link_hash_traverse (&htab->elf,
2454
                                    elf64_x86_64_readonly_dynrelocs,
2455
                                    info);
2456
 
2457
          if ((info->flags & DF_TEXTREL) != 0)
2458
            {
2459
              if (!add_dynamic_entry (DT_TEXTREL, 0))
2460
                return FALSE;
2461
            }
2462
        }
2463
    }
2464
#undef add_dynamic_entry
2465
 
2466
  return TRUE;
2467
}
2468
 
2469
static bfd_boolean
2470
elf64_x86_64_always_size_sections (bfd *output_bfd,
2471
                                   struct bfd_link_info *info)
2472
{
2473
  asection *tls_sec = elf_hash_table (info)->tls_sec;
2474
 
2475
  if (tls_sec)
2476
    {
2477
      struct elf_link_hash_entry *tlsbase;
2478
 
2479
      tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2480
                                      "_TLS_MODULE_BASE_",
2481
                                      FALSE, FALSE, FALSE);
2482
 
2483
      if (tlsbase && tlsbase->type == STT_TLS)
2484
        {
2485
          struct elf64_x86_64_link_hash_table *htab;
2486
          struct bfd_link_hash_entry *bh = NULL;
2487
          const struct elf_backend_data *bed
2488
            = get_elf_backend_data (output_bfd);
2489
 
2490
          htab = elf64_x86_64_hash_table (info);
2491
          if (htab == NULL)
2492
            return FALSE;
2493
 
2494
          if (!(_bfd_generic_link_add_one_symbol
2495
                (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2496
                 tls_sec, 0, NULL, FALSE,
2497
                 bed->collect, &bh)))
2498
            return FALSE;
2499
 
2500
          htab->tls_module_base = bh;
2501
 
2502
          tlsbase = (struct elf_link_hash_entry *)bh;
2503
          tlsbase->def_regular = 1;
2504
          tlsbase->other = STV_HIDDEN;
2505
          (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2506
        }
2507
    }
2508
 
2509
  return TRUE;
2510
}
2511
 
2512
/* _TLS_MODULE_BASE_ needs to be treated especially when linking
2513
   executables.  Rather than setting it to the beginning of the TLS
2514
   section, we have to set it to the end.  This function may be called
2515
   multiple times, it is idempotent.  */
2516
 
2517
static void
2518
elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2519
{
2520
  struct elf64_x86_64_link_hash_table *htab;
2521
  struct bfd_link_hash_entry *base;
2522
 
2523
  if (!info->executable)
2524
    return;
2525
 
2526
  htab = elf64_x86_64_hash_table (info);
2527
  if (htab == NULL)
2528
    return;
2529
 
2530
  base = htab->tls_module_base;
2531
  if (base == NULL)
2532
    return;
2533
 
2534
  base->u.def.value = htab->elf.tls_size;
2535
}
2536
 
2537
/* Return the base VMA address which should be subtracted from real addresses
2538
   when resolving @dtpoff relocation.
2539
   This is PT_TLS segment p_vaddr.  */
2540
 
2541
static bfd_vma
2542
elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2543
{
2544
  /* If tls_sec is NULL, we should have signalled an error already.  */
2545
  if (elf_hash_table (info)->tls_sec == NULL)
2546
    return 0;
2547
  return elf_hash_table (info)->tls_sec->vma;
2548
}
2549
 
2550
/* Return the relocation value for @tpoff relocation
2551
   if STT_TLS virtual address is ADDRESS.  */
2552
 
2553
static bfd_vma
2554
elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2555
{
2556
  struct elf_link_hash_table *htab = elf_hash_table (info);
2557
 
2558
  /* If tls_segment is NULL, we should have signalled an error already.  */
2559
  if (htab->tls_sec == NULL)
2560
    return 0;
2561
  return address - htab->tls_size - htab->tls_sec->vma;
2562
}
2563
 
2564
/* Is the instruction before OFFSET in CONTENTS a 32bit relative
2565
   branch?  */
2566
 
2567
static bfd_boolean
2568
is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2569
{
2570
  /* Opcode             Instruction
2571
     0xe8               call
2572
     0xe9               jump
2573
     0x0f 0x8x          conditional jump */
2574
  return ((offset > 0
2575
           && (contents [offset - 1] == 0xe8
2576
               || contents [offset - 1] == 0xe9))
2577
          || (offset > 1
2578
              && contents [offset - 2] == 0x0f
2579
              && (contents [offset - 1] & 0xf0) == 0x80));
2580
}
2581
 
2582
static void
2583
elf64_x86_64_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
2584
{
2585
  bfd_byte *loc = s->contents;
2586
  loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2587
  BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2588
              <= s->contents + s->size);
2589
  bfd_elf64_swap_reloca_out (abfd, rel, loc);
2590
}
2591
 
2592
/* Relocate an x86_64 ELF section.  */
2593
 
2594
static bfd_boolean
2595
elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2596
                               bfd *input_bfd, asection *input_section,
2597
                               bfd_byte *contents, Elf_Internal_Rela *relocs,
2598
                               Elf_Internal_Sym *local_syms,
2599
                               asection **local_sections)
2600
{
2601
  struct elf64_x86_64_link_hash_table *htab;
2602
  Elf_Internal_Shdr *symtab_hdr;
2603
  struct elf_link_hash_entry **sym_hashes;
2604
  bfd_vma *local_got_offsets;
2605
  bfd_vma *local_tlsdesc_gotents;
2606
  Elf_Internal_Rela *rel;
2607
  Elf_Internal_Rela *relend;
2608
 
2609
  BFD_ASSERT (is_x86_64_elf (input_bfd));
2610
 
2611
  htab = elf64_x86_64_hash_table (info);
2612
  if (htab == NULL)
2613
    return FALSE;
2614
  symtab_hdr = &elf_symtab_hdr (input_bfd);
2615
  sym_hashes = elf_sym_hashes (input_bfd);
2616
  local_got_offsets = elf_local_got_offsets (input_bfd);
2617
  local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2618
 
2619
  elf64_x86_64_set_tls_module_base (info);
2620
 
2621
  rel = relocs;
2622
  relend = relocs + input_section->reloc_count;
2623
  for (; rel < relend; rel++)
2624
    {
2625
      unsigned int r_type;
2626
      reloc_howto_type *howto;
2627
      unsigned long r_symndx;
2628
      struct elf_link_hash_entry *h;
2629
      Elf_Internal_Sym *sym;
2630
      asection *sec;
2631
      bfd_vma off, offplt;
2632
      bfd_vma relocation;
2633
      bfd_boolean unresolved_reloc;
2634
      bfd_reloc_status_type r;
2635
      int tls_type;
2636
      asection *base_got;
2637
 
2638
      r_type = ELF64_R_TYPE (rel->r_info);
2639
      if (r_type == (int) R_X86_64_GNU_VTINHERIT
2640
          || r_type == (int) R_X86_64_GNU_VTENTRY)
2641
        continue;
2642
 
2643
      if (r_type >= R_X86_64_max)
2644
        {
2645
          bfd_set_error (bfd_error_bad_value);
2646
          return FALSE;
2647
        }
2648
 
2649
      howto = x86_64_elf_howto_table + r_type;
2650
      r_symndx = ELF64_R_SYM (rel->r_info);
2651
      h = NULL;
2652
      sym = NULL;
2653
      sec = NULL;
2654
      unresolved_reloc = FALSE;
2655
      if (r_symndx < symtab_hdr->sh_info)
2656
        {
2657
          sym = local_syms + r_symndx;
2658
          sec = local_sections[r_symndx];
2659
 
2660
          relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2661
                                                &sec, rel);
2662
 
2663
          /* Relocate against local STT_GNU_IFUNC symbol.  */
2664
          if (!info->relocatable
2665
              && ELF64_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2666
            {
2667
              h = elf64_x86_64_get_local_sym_hash (htab, input_bfd,
2668
                                                   rel, FALSE);
2669
              if (h == NULL)
2670
                abort ();
2671
 
2672
              /* Set STT_GNU_IFUNC symbol value.  */
2673
              h->root.u.def.value = sym->st_value;
2674
              h->root.u.def.section = sec;
2675
            }
2676
        }
2677
      else
2678
        {
2679
          bfd_boolean warned;
2680
 
2681
          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2682
                                   r_symndx, symtab_hdr, sym_hashes,
2683
                                   h, sec, relocation,
2684
                                   unresolved_reloc, warned);
2685
        }
2686
 
2687
      if (sec != NULL && elf_discarded_section (sec))
2688
        {
2689
          /* For relocs against symbols from removed linkonce sections,
2690
             or sections discarded by a linker script, we just want the
2691
             section contents zeroed.  Avoid any special processing.  */
2692
          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2693
          rel->r_info = 0;
2694
          rel->r_addend = 0;
2695
          continue;
2696
        }
2697
 
2698
      if (info->relocatable)
2699
        continue;
2700
 
2701
      /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2702
         it here if it is defined in a non-shared object.  */
2703
      if (h != NULL
2704
          && h->type == STT_GNU_IFUNC
2705
          && h->def_regular)
2706
        {
2707
          asection *plt;
2708
          bfd_vma plt_index;
2709
          const char *name;
2710
 
2711
          if ((input_section->flags & SEC_ALLOC) == 0
2712
              || h->plt.offset == (bfd_vma) -1)
2713
            abort ();
2714
 
2715
          /* STT_GNU_IFUNC symbol must go through PLT.  */
2716
          plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2717
          relocation = (plt->output_section->vma
2718
                        + plt->output_offset + h->plt.offset);
2719
 
2720
          switch (r_type)
2721
            {
2722
            default:
2723
              if (h->root.root.string)
2724
                name = h->root.root.string;
2725
              else
2726
                name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2727
                                         NULL);
2728
              (*_bfd_error_handler)
2729
                (_("%B: relocation %s against STT_GNU_IFUNC "
2730
                   "symbol `%s' isn't handled by %s"), input_bfd,
2731
                 x86_64_elf_howto_table[r_type].name,
2732
                 name, __FUNCTION__);
2733
              bfd_set_error (bfd_error_bad_value);
2734
              return FALSE;
2735
 
2736
            case R_X86_64_32S:
2737
              if (info->shared)
2738
                abort ();
2739
              goto do_relocation;
2740
 
2741
            case R_X86_64_64:
2742
              if (rel->r_addend != 0)
2743
                {
2744
                  if (h->root.root.string)
2745
                    name = h->root.root.string;
2746
                  else
2747
                    name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2748
                                             sym, NULL);
2749
                  (*_bfd_error_handler)
2750
                    (_("%B: relocation %s against STT_GNU_IFUNC "
2751
                       "symbol `%s' has non-zero addend: %d"),
2752
                     input_bfd, x86_64_elf_howto_table[r_type].name,
2753
                     name, rel->r_addend);
2754
                  bfd_set_error (bfd_error_bad_value);
2755
                  return FALSE;
2756
                }
2757
 
2758
              /* Generate dynamic relcoation only when there is a
2759
                 non-GOF reference in a shared object.  */
2760
              if (info->shared && h->non_got_ref)
2761
                {
2762
                  Elf_Internal_Rela outrel;
2763
                  asection *sreloc;
2764
 
2765
                  /* Need a dynamic relocation to get the real function
2766
                     address.  */
2767
                  outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2768
                                                             info,
2769
                                                             input_section,
2770
                                                             rel->r_offset);
2771
                  if (outrel.r_offset == (bfd_vma) -1
2772
                      || outrel.r_offset == (bfd_vma) -2)
2773
                    abort ();
2774
 
2775
                  outrel.r_offset += (input_section->output_section->vma
2776
                                      + input_section->output_offset);
2777
 
2778
                  if (h->dynindx == -1
2779
                      || h->forced_local
2780
                      || info->executable)
2781
                    {
2782
                      /* This symbol is resolved locally.  */
2783
                      outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
2784
                      outrel.r_addend = (h->root.u.def.value
2785
                                         + h->root.u.def.section->output_section->vma
2786
                                         + h->root.u.def.section->output_offset);
2787
                    }
2788
                  else
2789
                    {
2790
                      outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2791
                      outrel.r_addend = 0;
2792
                    }
2793
 
2794
                  sreloc = htab->elf.irelifunc;
2795
                  elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
2796
 
2797
                  /* If this reloc is against an external symbol, we
2798
                     do not want to fiddle with the addend.  Otherwise,
2799
                     we need to include the symbol value so that it
2800
                     becomes an addend for the dynamic reloc.  For an
2801
                     internal symbol, we have updated addend.  */
2802
                  continue;
2803
                }
2804
 
2805
            case R_X86_64_32:
2806
            case R_X86_64_PC32:
2807
            case R_X86_64_PC64:
2808
            case R_X86_64_PLT32:
2809
              goto do_relocation;
2810
 
2811
            case R_X86_64_GOTPCREL:
2812
            case R_X86_64_GOTPCREL64:
2813
              base_got = htab->elf.sgot;
2814
              off = h->got.offset;
2815
 
2816
              if (base_got == NULL)
2817
                abort ();
2818
 
2819
              if (off == (bfd_vma) -1)
2820
                {
2821
                  /* We can't use h->got.offset here to save state, or
2822
                     even just remember the offset, as finish_dynamic_symbol
2823
                     would use that as offset into .got.  */
2824
 
2825
                  if (htab->elf.splt != NULL)
2826
                    {
2827
                      plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2828
                      off = (plt_index + 3) * GOT_ENTRY_SIZE;
2829
                      base_got = htab->elf.sgotplt;
2830
                    }
2831
                  else
2832
                    {
2833
                      plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2834
                      off = plt_index * GOT_ENTRY_SIZE;
2835
                      base_got = htab->elf.igotplt;
2836
                    }
2837
 
2838
                  if (h->dynindx == -1
2839
                      || h->forced_local
2840
                      || info->symbolic)
2841
                    {
2842
                      /* This references the local defitionion.  We must
2843
                         initialize this entry in the global offset table.
2844
                         Since the offset must always be a multiple of 8,
2845
                         we use the least significant bit to record
2846
                         whether we have initialized it already.
2847
 
2848
                         When doing a dynamic link, we create a .rela.got
2849
                         relocation entry to initialize the value.  This
2850
                         is done in the finish_dynamic_symbol routine.   */
2851
                      if ((off & 1) != 0)
2852
                        off &= ~1;
2853
                      else
2854
                        {
2855
                          bfd_put_64 (output_bfd, relocation,
2856
                                      base_got->contents + off);
2857
                          /* Note that this is harmless for the GOTPLT64
2858
                             case, as -1 | 1 still is -1.  */
2859
                          h->got.offset |= 1;
2860
                        }
2861
                    }
2862
                }
2863
 
2864
              relocation = (base_got->output_section->vma
2865
                            + base_got->output_offset + off);
2866
 
2867
              if (r_type != R_X86_64_GOTPCREL
2868
                  && r_type != R_X86_64_GOTPCREL64)
2869
                {
2870
                  asection *gotplt;
2871
                  if (htab->elf.splt != NULL)
2872
                    gotplt = htab->elf.sgotplt;
2873
                  else
2874
                    gotplt = htab->elf.igotplt;
2875
                  relocation -= (gotplt->output_section->vma
2876
                                 - gotplt->output_offset);
2877
                }
2878
 
2879
              goto do_relocation;
2880
            }
2881
        }
2882
 
2883
      /* When generating a shared object, the relocations handled here are
2884
         copied into the output file to be resolved at run time.  */
2885
      switch (r_type)
2886
        {
2887
        case R_X86_64_GOT32:
2888
        case R_X86_64_GOT64:
2889
          /* Relocation is to the entry for this symbol in the global
2890
             offset table.  */
2891
        case R_X86_64_GOTPCREL:
2892
        case R_X86_64_GOTPCREL64:
2893
          /* Use global offset table entry as symbol value.  */
2894
        case R_X86_64_GOTPLT64:
2895
          /* This is the same as GOT64 for relocation purposes, but
2896
             indicates the existence of a PLT entry.  The difficulty is,
2897
             that we must calculate the GOT slot offset from the PLT
2898
             offset, if this symbol got a PLT entry (it was global).
2899
             Additionally if it's computed from the PLT entry, then that
2900
             GOT offset is relative to .got.plt, not to .got.  */
2901
          base_got = htab->elf.sgot;
2902
 
2903
          if (htab->elf.sgot == NULL)
2904
            abort ();
2905
 
2906
          if (h != NULL)
2907
            {
2908
              bfd_boolean dyn;
2909
 
2910
              off = h->got.offset;
2911
              if (h->needs_plt
2912
                  && h->plt.offset != (bfd_vma)-1
2913
                  && off == (bfd_vma)-1)
2914
                {
2915
                  /* We can't use h->got.offset here to save
2916
                     state, or even just remember the offset, as
2917
                     finish_dynamic_symbol would use that as offset into
2918
                     .got.  */
2919
                  bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2920
                  off = (plt_index + 3) * GOT_ENTRY_SIZE;
2921
                  base_got = htab->elf.sgotplt;
2922
                }
2923
 
2924
              dyn = htab->elf.dynamic_sections_created;
2925
 
2926
              if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2927
                  || (info->shared
2928
                      && SYMBOL_REFERENCES_LOCAL (info, h))
2929
                  || (ELF_ST_VISIBILITY (h->other)
2930
                      && h->root.type == bfd_link_hash_undefweak))
2931
                {
2932
                  /* This is actually a static link, or it is a -Bsymbolic
2933
                     link and the symbol is defined locally, or the symbol
2934
                     was forced to be local because of a version file.  We
2935
                     must initialize this entry in the global offset table.
2936
                     Since the offset must always be a multiple of 8, we
2937
                     use the least significant bit to record whether we
2938
                     have initialized it already.
2939
 
2940
                     When doing a dynamic link, we create a .rela.got
2941
                     relocation entry to initialize the value.  This is
2942
                     done in the finish_dynamic_symbol routine.  */
2943
                  if ((off & 1) != 0)
2944
                    off &= ~1;
2945
                  else
2946
                    {
2947
                      bfd_put_64 (output_bfd, relocation,
2948
                                  base_got->contents + off);
2949
                      /* Note that this is harmless for the GOTPLT64 case,
2950
                         as -1 | 1 still is -1.  */
2951
                      h->got.offset |= 1;
2952
                    }
2953
                }
2954
              else
2955
                unresolved_reloc = FALSE;
2956
            }
2957
          else
2958
            {
2959
              if (local_got_offsets == NULL)
2960
                abort ();
2961
 
2962
              off = local_got_offsets[r_symndx];
2963
 
2964
              /* The offset must always be a multiple of 8.  We use
2965
                 the least significant bit to record whether we have
2966
                 already generated the necessary reloc.  */
2967
              if ((off & 1) != 0)
2968
                off &= ~1;
2969
              else
2970
                {
2971
                  bfd_put_64 (output_bfd, relocation,
2972
                              base_got->contents + off);
2973
 
2974
                  if (info->shared)
2975
                    {
2976
                      asection *s;
2977
                      Elf_Internal_Rela outrel;
2978
 
2979
                      /* We need to generate a R_X86_64_RELATIVE reloc
2980
                         for the dynamic linker.  */
2981
                      s = htab->elf.srelgot;
2982
                      if (s == NULL)
2983
                        abort ();
2984
 
2985
                      outrel.r_offset = (base_got->output_section->vma
2986
                                         + base_got->output_offset
2987
                                         + off);
2988
                      outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2989
                      outrel.r_addend = relocation;
2990
                      elf64_x86_64_append_rela (output_bfd, s, &outrel);
2991
                    }
2992
 
2993
                  local_got_offsets[r_symndx] |= 1;
2994
                }
2995
            }
2996
 
2997
          if (off >= (bfd_vma) -2)
2998
            abort ();
2999
 
3000
          relocation = base_got->output_section->vma
3001
                       + base_got->output_offset + off;
3002
          if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
3003
            relocation -= htab->elf.sgotplt->output_section->vma
3004
                          - htab->elf.sgotplt->output_offset;
3005
 
3006
          break;
3007
 
3008
        case R_X86_64_GOTOFF64:
3009
          /* Relocation is relative to the start of the global offset
3010
             table.  */
3011
 
3012
          /* Check to make sure it isn't a protected function symbol
3013
             for shared library since it may not be local when used
3014
             as function address.  */
3015
          if (info->shared
3016
              && h
3017
              && h->def_regular
3018
              && h->type == STT_FUNC
3019
              && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3020
            {
3021
              (*_bfd_error_handler)
3022
                (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3023
                 input_bfd, h->root.root.string);
3024
              bfd_set_error (bfd_error_bad_value);
3025
              return FALSE;
3026
            }
3027
 
3028
          /* Note that sgot is not involved in this
3029
             calculation.  We always want the start of .got.plt.  If we
3030
             defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3031
             permitted by the ABI, we might have to change this
3032
             calculation.  */
3033
          relocation -= htab->elf.sgotplt->output_section->vma
3034
                        + htab->elf.sgotplt->output_offset;
3035
          break;
3036
 
3037
        case R_X86_64_GOTPC32:
3038
        case R_X86_64_GOTPC64:
3039
          /* Use global offset table as symbol value.  */
3040
          relocation = htab->elf.sgotplt->output_section->vma
3041
                       + htab->elf.sgotplt->output_offset;
3042
          unresolved_reloc = FALSE;
3043
          break;
3044
 
3045
        case R_X86_64_PLTOFF64:
3046
          /* Relocation is PLT entry relative to GOT.  For local
3047
             symbols it's the symbol itself relative to GOT.  */
3048
          if (h != NULL
3049
              /* See PLT32 handling.  */
3050
              && h->plt.offset != (bfd_vma) -1
3051
              && htab->elf.splt != NULL)
3052
            {
3053
              relocation = (htab->elf.splt->output_section->vma
3054
                            + htab->elf.splt->output_offset
3055
                            + h->plt.offset);
3056
              unresolved_reloc = FALSE;
3057
            }
3058
 
3059
          relocation -= htab->elf.sgotplt->output_section->vma
3060
                        + htab->elf.sgotplt->output_offset;
3061
          break;
3062
 
3063
        case R_X86_64_PLT32:
3064
          /* Relocation is to the entry for this symbol in the
3065
             procedure linkage table.  */
3066
 
3067
          /* Resolve a PLT32 reloc against a local symbol directly,
3068
             without using the procedure linkage table.  */
3069
          if (h == NULL)
3070
            break;
3071
 
3072
          if (h->plt.offset == (bfd_vma) -1
3073
              || htab->elf.splt == NULL)
3074
            {
3075
              /* We didn't make a PLT entry for this symbol.  This
3076
                 happens when statically linking PIC code, or when
3077
                 using -Bsymbolic.  */
3078
              break;
3079
            }
3080
 
3081
          relocation = (htab->elf.splt->output_section->vma
3082
                        + htab->elf.splt->output_offset
3083
                        + h->plt.offset);
3084
          unresolved_reloc = FALSE;
3085
          break;
3086
 
3087
        case R_X86_64_PC8:
3088
        case R_X86_64_PC16:
3089
        case R_X86_64_PC32:
3090
          if (info->shared
3091
              && (input_section->flags & SEC_ALLOC) != 0
3092
              && (input_section->flags & SEC_READONLY) != 0
3093
              && h != NULL)
3094
            {
3095
              bfd_boolean fail = FALSE;
3096
              bfd_boolean branch
3097
                = (r_type == R_X86_64_PC32
3098
                   && is_32bit_relative_branch (contents, rel->r_offset));
3099
 
3100
              if (SYMBOL_REFERENCES_LOCAL (info, h))
3101
                {
3102
                  /* Symbol is referenced locally.  Make sure it is
3103
                     defined locally or for a branch.  */
3104
                  fail = !h->def_regular && !branch;
3105
                }
3106
              else
3107
                {
3108
                  /* Symbol isn't referenced locally.  We only allow
3109
                     branch to symbol with non-default visibility. */
3110
                  fail = (!branch
3111
                          || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3112
                }
3113
 
3114
              if (fail)
3115
                {
3116
                  const char *fmt;
3117
                  const char *v;
3118
                  const char *pic = "";
3119
 
3120
                  switch (ELF_ST_VISIBILITY (h->other))
3121
                    {
3122
                    case STV_HIDDEN:
3123
                      v = _("hidden symbol");
3124
                      break;
3125
                    case STV_INTERNAL:
3126
                      v = _("internal symbol");
3127
                      break;
3128
                    case STV_PROTECTED:
3129
                      v = _("protected symbol");
3130
                      break;
3131
                    default:
3132
                      v = _("symbol");
3133
                      pic = _("; recompile with -fPIC");
3134
                      break;
3135
                    }
3136
 
3137
                  if (h->def_regular)
3138
                    fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3139
                  else
3140
                    fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3141
 
3142
                  (*_bfd_error_handler) (fmt, input_bfd,
3143
                                         x86_64_elf_howto_table[r_type].name,
3144
                                         v,  h->root.root.string, pic);
3145
                  bfd_set_error (bfd_error_bad_value);
3146
                  return FALSE;
3147
                }
3148
            }
3149
          /* Fall through.  */
3150
 
3151
        case R_X86_64_8:
3152
        case R_X86_64_16:
3153
        case R_X86_64_32:
3154
        case R_X86_64_PC64:
3155
        case R_X86_64_64:
3156
          /* FIXME: The ABI says the linker should make sure the value is
3157
             the same when it's zeroextended to 64 bit.  */
3158
 
3159
          if ((input_section->flags & SEC_ALLOC) == 0)
3160
            break;
3161
 
3162
          if ((info->shared
3163
               && (h == NULL
3164
                   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3165
                   || h->root.type != bfd_link_hash_undefweak)
3166
               && (! IS_X86_64_PCREL_TYPE (r_type)
3167
                   || ! SYMBOL_CALLS_LOCAL (info, h)))
3168
              || (ELIMINATE_COPY_RELOCS
3169
                  && !info->shared
3170
                  && h != NULL
3171
                  && h->dynindx != -1
3172
                  && !h->non_got_ref
3173
                  && ((h->def_dynamic
3174
                       && !h->def_regular)
3175
                      || h->root.type == bfd_link_hash_undefweak
3176
                      || h->root.type == bfd_link_hash_undefined)))
3177
            {
3178
              Elf_Internal_Rela outrel;
3179
              bfd_boolean skip, relocate;
3180
              asection *sreloc;
3181
 
3182
              /* When generating a shared object, these relocations
3183
                 are copied into the output file to be resolved at run
3184
                 time.  */
3185
              skip = FALSE;
3186
              relocate = FALSE;
3187
 
3188
              outrel.r_offset =
3189
                _bfd_elf_section_offset (output_bfd, info, input_section,
3190
                                         rel->r_offset);
3191
              if (outrel.r_offset == (bfd_vma) -1)
3192
                skip = TRUE;
3193
              else if (outrel.r_offset == (bfd_vma) -2)
3194
                skip = TRUE, relocate = TRUE;
3195
 
3196
              outrel.r_offset += (input_section->output_section->vma
3197
                                  + input_section->output_offset);
3198
 
3199
              if (skip)
3200
                memset (&outrel, 0, sizeof outrel);
3201
 
3202
              /* h->dynindx may be -1 if this symbol was marked to
3203
                 become local.  */
3204
              else if (h != NULL
3205
                       && h->dynindx != -1
3206
                       && (IS_X86_64_PCREL_TYPE (r_type)
3207
                           || ! info->shared
3208
                           || ! SYMBOLIC_BIND (info, h)
3209
                           || ! h->def_regular))
3210
                {
3211
                  outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3212
                  outrel.r_addend = rel->r_addend;
3213
                }
3214
              else
3215
                {
3216
                  /* This symbol is local, or marked to become local.  */
3217
                  if (r_type == R_X86_64_64)
3218
                    {
3219
                      relocate = TRUE;
3220
                      outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3221
                      outrel.r_addend = relocation + rel->r_addend;
3222
                    }
3223
                  else
3224
                    {
3225
                      long sindx;
3226
 
3227
                      if (bfd_is_abs_section (sec))
3228
                        sindx = 0;
3229
                      else if (sec == NULL || sec->owner == NULL)
3230
                        {
3231
                          bfd_set_error (bfd_error_bad_value);
3232
                          return FALSE;
3233
                        }
3234
                      else
3235
                        {
3236
                          asection *osec;
3237
 
3238
                          /* We are turning this relocation into one
3239
                             against a section symbol.  It would be
3240
                             proper to subtract the symbol's value,
3241
                             osec->vma, from the emitted reloc addend,
3242
                             but ld.so expects buggy relocs.  */
3243
                          osec = sec->output_section;
3244
                          sindx = elf_section_data (osec)->dynindx;
3245
                          if (sindx == 0)
3246
                            {
3247
                              asection *oi = htab->elf.text_index_section;
3248
                              sindx = elf_section_data (oi)->dynindx;
3249
                            }
3250
                          BFD_ASSERT (sindx != 0);
3251
                        }
3252
 
3253
                      outrel.r_info = ELF64_R_INFO (sindx, r_type);
3254
                      outrel.r_addend = relocation + rel->r_addend;
3255
                    }
3256
                }
3257
 
3258
              sreloc = elf_section_data (input_section)->sreloc;
3259
 
3260
              BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3261
 
3262
              elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3263
 
3264
              /* If this reloc is against an external symbol, we do
3265
                 not want to fiddle with the addend.  Otherwise, we
3266
                 need to include the symbol value so that it becomes
3267
                 an addend for the dynamic reloc.  */
3268
              if (! relocate)
3269
                continue;
3270
            }
3271
 
3272
          break;
3273
 
3274
        case R_X86_64_TLSGD:
3275
        case R_X86_64_GOTPC32_TLSDESC:
3276
        case R_X86_64_TLSDESC_CALL:
3277
        case R_X86_64_GOTTPOFF:
3278
          tls_type = GOT_UNKNOWN;
3279
          if (h == NULL && local_got_offsets)
3280
            tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3281
          else if (h != NULL)
3282
            tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3283
 
3284
          if (! elf64_x86_64_tls_transition (info, input_bfd,
3285
                                             input_section, contents,
3286
                                             symtab_hdr, sym_hashes,
3287
                                             &r_type, tls_type, rel,
3288
                                             relend, h, r_symndx))
3289
            return FALSE;
3290
 
3291
          if (r_type == R_X86_64_TPOFF32)
3292
            {
3293
              bfd_vma roff = rel->r_offset;
3294
 
3295
              BFD_ASSERT (! unresolved_reloc);
3296
 
3297
              if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3298
                {
3299
                  /* GD->LE transition.
3300
                     .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3301
                     .word 0x6666; rex64; call __tls_get_addr
3302
                     Change it into:
3303
                     movq %fs:0, %rax
3304
                     leaq foo@tpoff(%rax), %rax */
3305
                  memcpy (contents + roff - 4,
3306
                          "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3307
                          16);
3308
                  bfd_put_32 (output_bfd,
3309
                              elf64_x86_64_tpoff (info, relocation),
3310
                              contents + roff + 8);
3311
                  /* Skip R_X86_64_PC32/R_X86_64_PLT32.  */
3312
                  rel++;
3313
                  continue;
3314
                }
3315
              else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3316
                {
3317
                  /* GDesc -> LE transition.
3318
                     It's originally something like:
3319
                     leaq x@tlsdesc(%rip), %rax
3320
 
3321
                     Change it to:
3322
                     movl $x@tpoff, %rax
3323
                   */
3324
 
3325
                  unsigned int val, type, type2;
3326
 
3327
                  type = bfd_get_8 (input_bfd, contents + roff - 3);
3328
                  type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3329
                  val = bfd_get_8 (input_bfd, contents + roff - 1);
3330
                  bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3331
                             contents + roff - 3);
3332
                  bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3333
                  bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3334
                             contents + roff - 1);
3335
                  bfd_put_32 (output_bfd,
3336
                              elf64_x86_64_tpoff (info, relocation),
3337
                              contents + roff);
3338
                  continue;
3339
                }
3340
              else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3341
                {
3342
                  /* GDesc -> LE transition.
3343
                     It's originally:
3344
                     call *(%rax)
3345
                     Turn it into:
3346
                     xchg %ax,%ax.  */
3347
                  bfd_put_8 (output_bfd, 0x66, contents + roff);
3348
                  bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3349
                  continue;
3350
                }
3351
              else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3352
                {
3353
                  /* IE->LE transition:
3354
                     Originally it can be one of:
3355
                     movq foo@gottpoff(%rip), %reg
3356
                     addq foo@gottpoff(%rip), %reg
3357
                     We change it into:
3358
                     movq $foo, %reg
3359
                     leaq foo(%reg), %reg
3360
                     addq $foo, %reg.  */
3361
 
3362
                  unsigned int val, type, reg;
3363
 
3364
                  val = bfd_get_8 (input_bfd, contents + roff - 3);
3365
                  type = bfd_get_8 (input_bfd, contents + roff - 2);
3366
                  reg = bfd_get_8 (input_bfd, contents + roff - 1);
3367
                  reg >>= 3;
3368
                  if (type == 0x8b)
3369
                    {
3370
                      /* movq */
3371
                      if (val == 0x4c)
3372
                        bfd_put_8 (output_bfd, 0x49,
3373
                                   contents + roff - 3);
3374
                      bfd_put_8 (output_bfd, 0xc7,
3375
                                 contents + roff - 2);
3376
                      bfd_put_8 (output_bfd, 0xc0 | reg,
3377
                                 contents + roff - 1);
3378
                    }
3379
                  else if (reg == 4)
3380
                    {
3381
                      /* addq -> addq - addressing with %rsp/%r12 is
3382
                         special  */
3383
                      if (val == 0x4c)
3384
                        bfd_put_8 (output_bfd, 0x49,
3385
                                   contents + roff - 3);
3386
                      bfd_put_8 (output_bfd, 0x81,
3387
                                 contents + roff - 2);
3388
                      bfd_put_8 (output_bfd, 0xc0 | reg,
3389
                                 contents + roff - 1);
3390
                    }
3391
                  else
3392
                    {
3393
                      /* addq -> leaq */
3394
                      if (val == 0x4c)
3395
                        bfd_put_8 (output_bfd, 0x4d,
3396
                                   contents + roff - 3);
3397
                      bfd_put_8 (output_bfd, 0x8d,
3398
                                 contents + roff - 2);
3399
                      bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3400
                                 contents + roff - 1);
3401
                    }
3402
                  bfd_put_32 (output_bfd,
3403
                              elf64_x86_64_tpoff (info, relocation),
3404
                              contents + roff);
3405
                  continue;
3406
                }
3407
              else
3408
                BFD_ASSERT (FALSE);
3409
            }
3410
 
3411
          if (htab->elf.sgot == NULL)
3412
            abort ();
3413
 
3414
          if (h != NULL)
3415
            {
3416
              off = h->got.offset;
3417
              offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3418
            }
3419
          else
3420
            {
3421
              if (local_got_offsets == NULL)
3422
                abort ();
3423
 
3424
              off = local_got_offsets[r_symndx];
3425
              offplt = local_tlsdesc_gotents[r_symndx];
3426
            }
3427
 
3428
          if ((off & 1) != 0)
3429
            off &= ~1;
3430
          else
3431
            {
3432
              Elf_Internal_Rela outrel;
3433
              int dr_type, indx;
3434
              asection *sreloc;
3435
 
3436
              if (htab->elf.srelgot == NULL)
3437
                abort ();
3438
 
3439
              indx = h && h->dynindx != -1 ? h->dynindx : 0;
3440
 
3441
              if (GOT_TLS_GDESC_P (tls_type))
3442
                {
3443
                  outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3444
                  BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3445
                              + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3446
                  outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3447
                                     + htab->elf.sgotplt->output_offset
3448
                                     + offplt
3449
                                     + htab->sgotplt_jump_table_size);
3450
                  sreloc = htab->elf.srelplt;
3451
                  if (indx == 0)
3452
                    outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3453
                  else
3454
                    outrel.r_addend = 0;
3455
                  elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3456
                }
3457
 
3458
              sreloc = htab->elf.srelgot;
3459
 
3460
              outrel.r_offset = (htab->elf.sgot->output_section->vma
3461
                                 + htab->elf.sgot->output_offset + off);
3462
 
3463
              if (GOT_TLS_GD_P (tls_type))
3464
                dr_type = R_X86_64_DTPMOD64;
3465
              else if (GOT_TLS_GDESC_P (tls_type))
3466
                goto dr_done;
3467
              else
3468
                dr_type = R_X86_64_TPOFF64;
3469
 
3470
              bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3471
              outrel.r_addend = 0;
3472
              if ((dr_type == R_X86_64_TPOFF64
3473
                   || dr_type == R_X86_64_TLSDESC) && indx == 0)
3474
                outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3475
              outrel.r_info = ELF64_R_INFO (indx, dr_type);
3476
 
3477
              elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3478
 
3479
              if (GOT_TLS_GD_P (tls_type))
3480
                {
3481
                  if (indx == 0)
3482
                    {
3483
                      BFD_ASSERT (! unresolved_reloc);
3484
                      bfd_put_64 (output_bfd,
3485
                                  relocation - elf64_x86_64_dtpoff_base (info),
3486
                                  htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3487
                    }
3488
                  else
3489
                    {
3490
                      bfd_put_64 (output_bfd, 0,
3491
                                  htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3492
                      outrel.r_info = ELF64_R_INFO (indx,
3493
                                                    R_X86_64_DTPOFF64);
3494
                      outrel.r_offset += GOT_ENTRY_SIZE;
3495
                      elf64_x86_64_append_rela (output_bfd, sreloc,
3496
                                                &outrel);
3497
                    }
3498
                }
3499
 
3500
            dr_done:
3501
              if (h != NULL)
3502
                h->got.offset |= 1;
3503
              else
3504
                local_got_offsets[r_symndx] |= 1;
3505
            }
3506
 
3507
          if (off >= (bfd_vma) -2
3508
              && ! GOT_TLS_GDESC_P (tls_type))
3509
            abort ();
3510
          if (r_type == ELF64_R_TYPE (rel->r_info))
3511
            {
3512
              if (r_type == R_X86_64_GOTPC32_TLSDESC
3513
                  || r_type == R_X86_64_TLSDESC_CALL)
3514
                relocation = htab->elf.sgotplt->output_section->vma
3515
                  + htab->elf.sgotplt->output_offset
3516
                  + offplt + htab->sgotplt_jump_table_size;
3517
              else
3518
                relocation = htab->elf.sgot->output_section->vma
3519
                  + htab->elf.sgot->output_offset + off;
3520
              unresolved_reloc = FALSE;
3521
            }
3522
          else
3523
            {
3524
              bfd_vma roff = rel->r_offset;
3525
 
3526
              if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3527
                {
3528
                  /* GD->IE transition.
3529
                     .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3530
                     .word 0x6666; rex64; call __tls_get_addr@plt
3531
                     Change it into:
3532
                     movq %fs:0, %rax
3533
                     addq foo@gottpoff(%rip), %rax */
3534
                  memcpy (contents + roff - 4,
3535
                          "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3536
                          16);
3537
 
3538
                  relocation = (htab->elf.sgot->output_section->vma
3539
                                + htab->elf.sgot->output_offset + off
3540
                                - roff
3541
                                - input_section->output_section->vma
3542
                                - input_section->output_offset
3543
                                - 12);
3544
                  bfd_put_32 (output_bfd, relocation,
3545
                              contents + roff + 8);
3546
                  /* Skip R_X86_64_PLT32.  */
3547
                  rel++;
3548
                  continue;
3549
                }
3550
              else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3551
                {
3552
                  /* GDesc -> IE transition.
3553
                     It's originally something like:
3554
                     leaq x@tlsdesc(%rip), %rax
3555
 
3556
                     Change it to:
3557
                     movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3558
                   */
3559
 
3560
                  unsigned int val, type, type2;
3561
 
3562
                  type = bfd_get_8 (input_bfd, contents + roff - 3);
3563
                  type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3564
                  val = bfd_get_8 (input_bfd, contents + roff - 1);
3565
 
3566
                  /* Now modify the instruction as appropriate. To
3567
                     turn a leaq into a movq in the form we use it, it
3568
                     suffices to change the second byte from 0x8d to
3569
                     0x8b.  */
3570
                  bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3571
 
3572
                  bfd_put_32 (output_bfd,
3573
                              htab->elf.sgot->output_section->vma
3574
                              + htab->elf.sgot->output_offset + off
3575
                              - rel->r_offset
3576
                              - input_section->output_section->vma
3577
                              - input_section->output_offset
3578
                              - 4,
3579
                              contents + roff);
3580
                  continue;
3581
                }
3582
              else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3583
                {
3584
                  /* GDesc -> IE transition.
3585
                     It's originally:
3586
                     call *(%rax)
3587
 
3588
                     Change it to:
3589
                     xchg %ax,%ax.  */
3590
 
3591
                  unsigned int val, type;
3592
 
3593
                  type = bfd_get_8 (input_bfd, contents + roff);
3594
                  val = bfd_get_8 (input_bfd, contents + roff + 1);
3595
                  bfd_put_8 (output_bfd, 0x66, contents + roff);
3596
                  bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3597
                  continue;
3598
                }
3599
              else
3600
                BFD_ASSERT (FALSE);
3601
            }
3602
          break;
3603
 
3604
        case R_X86_64_TLSLD:
3605
          if (! elf64_x86_64_tls_transition (info, input_bfd,
3606
                                             input_section, contents,
3607
                                             symtab_hdr, sym_hashes,
3608
                                             &r_type, GOT_UNKNOWN,
3609
                                             rel, relend, h, r_symndx))
3610
            return FALSE;
3611
 
3612
          if (r_type != R_X86_64_TLSLD)
3613
            {
3614
              /* LD->LE transition:
3615
                 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3616
                 We change it into:
3617
                 .word 0x6666; .byte 0x66; movl %fs:0, %rax.  */
3618
 
3619
              BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3620
              memcpy (contents + rel->r_offset - 3,
3621
                      "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3622
              /* Skip R_X86_64_PC32/R_X86_64_PLT32.  */
3623
              rel++;
3624
              continue;
3625
            }
3626
 
3627
          if (htab->elf.sgot == NULL)
3628
            abort ();
3629
 
3630
          off = htab->tls_ld_got.offset;
3631
          if (off & 1)
3632
            off &= ~1;
3633
          else
3634
            {
3635
              Elf_Internal_Rela outrel;
3636
 
3637
              if (htab->elf.srelgot == NULL)
3638
                abort ();
3639
 
3640
              outrel.r_offset = (htab->elf.sgot->output_section->vma
3641
                                 + htab->elf.sgot->output_offset + off);
3642
 
3643
              bfd_put_64 (output_bfd, 0,
3644
                          htab->elf.sgot->contents + off);
3645
              bfd_put_64 (output_bfd, 0,
3646
                          htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3647
              outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3648
              outrel.r_addend = 0;
3649
              elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot,
3650
                                        &outrel);
3651
              htab->tls_ld_got.offset |= 1;
3652
            }
3653
          relocation = htab->elf.sgot->output_section->vma
3654
                       + htab->elf.sgot->output_offset + off;
3655
          unresolved_reloc = FALSE;
3656
          break;
3657
 
3658
        case R_X86_64_DTPOFF32:
3659
          if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
3660
            relocation -= elf64_x86_64_dtpoff_base (info);
3661
          else
3662
            relocation = elf64_x86_64_tpoff (info, relocation);
3663
          break;
3664
 
3665
        case R_X86_64_TPOFF32:
3666
          BFD_ASSERT (info->executable);
3667
          relocation = elf64_x86_64_tpoff (info, relocation);
3668
          break;
3669
 
3670
        default:
3671
          break;
3672
        }
3673
 
3674
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3675
         because such sections are not SEC_ALLOC and thus ld.so will
3676
         not process them.  */
3677
      if (unresolved_reloc
3678
          && !((input_section->flags & SEC_DEBUGGING) != 0
3679
               && h->def_dynamic))
3680
        (*_bfd_error_handler)
3681
          (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3682
           input_bfd,
3683
           input_section,
3684
           (long) rel->r_offset,
3685
           howto->name,
3686
           h->root.root.string);
3687
 
3688
do_relocation:
3689
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3690
                                    contents, rel->r_offset,
3691
                                    relocation, rel->r_addend);
3692
 
3693
      if (r != bfd_reloc_ok)
3694
        {
3695
          const char *name;
3696
 
3697
          if (h != NULL)
3698
            name = h->root.root.string;
3699
          else
3700
            {
3701
              name = bfd_elf_string_from_elf_section (input_bfd,
3702
                                                      symtab_hdr->sh_link,
3703
                                                      sym->st_name);
3704
              if (name == NULL)
3705
                return FALSE;
3706
              if (*name == '\0')
3707
                name = bfd_section_name (input_bfd, sec);
3708
            }
3709
 
3710
          if (r == bfd_reloc_overflow)
3711
            {
3712
              if (! ((*info->callbacks->reloc_overflow)
3713
                     (info, (h ? &h->root : NULL), name, howto->name,
3714
                      (bfd_vma) 0, input_bfd, input_section,
3715
                      rel->r_offset)))
3716
                return FALSE;
3717
            }
3718
          else
3719
            {
3720
              (*_bfd_error_handler)
3721
                (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3722
                 input_bfd, input_section,
3723
                 (long) rel->r_offset, name, (int) r);
3724
              return FALSE;
3725
            }
3726
        }
3727
    }
3728
 
3729
  return TRUE;
3730
}
3731
 
3732
/* Finish up dynamic symbol handling.  We set the contents of various
3733
   dynamic sections here.  */
3734
 
3735
static bfd_boolean
3736
elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3737
                                    struct bfd_link_info *info,
3738
                                    struct elf_link_hash_entry *h,
3739
                                    Elf_Internal_Sym *sym)
3740
{
3741
  struct elf64_x86_64_link_hash_table *htab;
3742
 
3743
  htab = elf64_x86_64_hash_table (info);
3744
  if (htab == NULL)
3745
    return FALSE;
3746
 
3747
  if (h->plt.offset != (bfd_vma) -1)
3748
    {
3749
      bfd_vma plt_index;
3750
      bfd_vma got_offset;
3751
      Elf_Internal_Rela rela;
3752
      bfd_byte *loc;
3753
      asection *plt, *gotplt, *relplt;
3754
 
3755
      /* When building a static executable, use .iplt, .igot.plt and
3756
         .rela.iplt sections for STT_GNU_IFUNC symbols.  */
3757
      if (htab->elf.splt != NULL)
3758
        {
3759
          plt = htab->elf.splt;
3760
          gotplt = htab->elf.sgotplt;
3761
          relplt = htab->elf.srelplt;
3762
        }
3763
      else
3764
        {
3765
          plt = htab->elf.iplt;
3766
          gotplt = htab->elf.igotplt;
3767
          relplt = htab->elf.irelplt;
3768
        }
3769
 
3770
      /* This symbol has an entry in the procedure linkage table.  Set
3771
         it up.  */
3772
      if ((h->dynindx == -1
3773
           && !((h->forced_local || info->executable)
3774
                && h->def_regular
3775
                && h->type == STT_GNU_IFUNC))
3776
          || plt == NULL
3777
          || gotplt == NULL
3778
          || relplt == NULL)
3779
        abort ();
3780
 
3781
      /* Get the index in the procedure linkage table which
3782
         corresponds to this symbol.  This is the index of this symbol
3783
         in all the symbols for which we are making plt entries.  The
3784
         first entry in the procedure linkage table is reserved.
3785
 
3786
         Get the offset into the .got table of the entry that
3787
         corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
3788
         bytes. The first three are reserved for the dynamic linker.
3789
 
3790
         For static executables, we don't reserve anything.  */
3791
 
3792
      if (plt == htab->elf.splt)
3793
        {
3794
          plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3795
          got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3796
        }
3797
      else
3798
        {
3799
          plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3800
          got_offset = plt_index * GOT_ENTRY_SIZE;
3801
        }
3802
 
3803
      /* Fill in the entry in the procedure linkage table.  */
3804
      memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3805
              PLT_ENTRY_SIZE);
3806
 
3807
      /* Insert the relocation positions of the plt section.  The magic
3808
         numbers at the end of the statements are the positions of the
3809
         relocations in the plt section.  */
3810
      /* Put offset for jmp *name@GOTPCREL(%rip), since the
3811
         instruction uses 6 bytes, subtract this value.  */
3812
      bfd_put_32 (output_bfd,
3813
                      (gotplt->output_section->vma
3814
                       + gotplt->output_offset
3815
                       + got_offset
3816
                       - plt->output_section->vma
3817
                       - plt->output_offset
3818
                       - h->plt.offset
3819
                       - 6),
3820
                  plt->contents + h->plt.offset + 2);
3821
 
3822
      /* Don't fill PLT entry for static executables.  */
3823
      if (plt == htab->elf.splt)
3824
        {
3825
          /* Put relocation index.  */
3826
          bfd_put_32 (output_bfd, plt_index,
3827
                      plt->contents + h->plt.offset + 7);
3828
          /* Put offset for jmp .PLT0.  */
3829
          bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3830
                      plt->contents + h->plt.offset + 12);
3831
        }
3832
 
3833
      /* Fill in the entry in the global offset table, initially this
3834
         points to the pushq instruction in the PLT which is at offset 6.  */
3835
      bfd_put_64 (output_bfd, (plt->output_section->vma
3836
                               + plt->output_offset
3837
                               + h->plt.offset + 6),
3838
                  gotplt->contents + got_offset);
3839
 
3840
      /* Fill in the entry in the .rela.plt section.  */
3841
      rela.r_offset = (gotplt->output_section->vma
3842
                       + gotplt->output_offset
3843
                       + got_offset);
3844
      if (h->dynindx == -1
3845
          || ((info->executable
3846
               || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3847
              && h->def_regular
3848
              && h->type == STT_GNU_IFUNC))
3849
        {
3850
          /* If an STT_GNU_IFUNC symbol is locally defined, generate
3851
             R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT.  */
3852
          rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3853
          rela.r_addend = (h->root.u.def.value
3854
                           + h->root.u.def.section->output_section->vma
3855
                           + h->root.u.def.section->output_offset);
3856
        }
3857
      else
3858
        {
3859
          rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3860
          rela.r_addend = 0;
3861
        }
3862
      loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3863
      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3864
 
3865
      if (!h->def_regular)
3866
        {
3867
          /* Mark the symbol as undefined, rather than as defined in
3868
             the .plt section.  Leave the value if there were any
3869
             relocations where pointer equality matters (this is a clue
3870
             for the dynamic linker, to make function pointer
3871
             comparisons work between an application and shared
3872
             library), otherwise set it to zero.  If a function is only
3873
             called from a binary, there is no need to slow down
3874
             shared libraries because of that.  */
3875
          sym->st_shndx = SHN_UNDEF;
3876
          if (!h->pointer_equality_needed)
3877
            sym->st_value = 0;
3878
        }
3879
    }
3880
 
3881
  if (h->got.offset != (bfd_vma) -1
3882
      && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3883
      && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3884
    {
3885
      Elf_Internal_Rela rela;
3886
 
3887
      /* This symbol has an entry in the global offset table.  Set it
3888
         up.  */
3889
      if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3890
        abort ();
3891
 
3892
      rela.r_offset = (htab->elf.sgot->output_section->vma
3893
                       + htab->elf.sgot->output_offset
3894
                       + (h->got.offset &~ (bfd_vma) 1));
3895
 
3896
      /* If this is a static link, or it is a -Bsymbolic link and the
3897
         symbol is defined locally or was forced to be local because
3898
         of a version file, we just want to emit a RELATIVE reloc.
3899
         The entry in the global offset table will already have been
3900
         initialized in the relocate_section function.  */
3901
      if (h->def_regular
3902
          && h->type == STT_GNU_IFUNC)
3903
        {
3904
          if (info->shared)
3905
            {
3906
              /* Generate R_X86_64_GLOB_DAT.  */
3907
              goto do_glob_dat;
3908
            }
3909
          else
3910
            {
3911
              asection *plt;
3912
 
3913
              if (!h->pointer_equality_needed)
3914
                abort ();
3915
 
3916
              /* For non-shared object, we can't use .got.plt, which
3917
                 contains the real function addres if we need pointer
3918
                 equality.  We load the GOT entry with the PLT entry.  */
3919
              plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3920
              bfd_put_64 (output_bfd, (plt->output_section->vma
3921
                                       + plt->output_offset
3922
                                       + h->plt.offset),
3923
                          htab->elf.sgot->contents + h->got.offset);
3924
              return TRUE;
3925
            }
3926
        }
3927
      else if (info->shared
3928
               && SYMBOL_REFERENCES_LOCAL (info, h))
3929
        {
3930
          if (!h->def_regular)
3931
            return FALSE;
3932
          BFD_ASSERT((h->got.offset & 1) != 0);
3933
          rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3934
          rela.r_addend = (h->root.u.def.value
3935
                           + h->root.u.def.section->output_section->vma
3936
                           + h->root.u.def.section->output_offset);
3937
        }
3938
      else
3939
        {
3940
          BFD_ASSERT((h->got.offset & 1) == 0);
3941
do_glob_dat:
3942
          bfd_put_64 (output_bfd, (bfd_vma) 0,
3943
                      htab->elf.sgot->contents + h->got.offset);
3944
          rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3945
          rela.r_addend = 0;
3946
        }
3947
 
3948
      elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot, &rela);
3949
    }
3950
 
3951
  if (h->needs_copy)
3952
    {
3953
      Elf_Internal_Rela rela;
3954
 
3955
      /* This symbol needs a copy reloc.  Set it up.  */
3956
 
3957
      if (h->dynindx == -1
3958
          || (h->root.type != bfd_link_hash_defined
3959
              && h->root.type != bfd_link_hash_defweak)
3960
          || htab->srelbss == NULL)
3961
        abort ();
3962
 
3963
      rela.r_offset = (h->root.u.def.value
3964
                       + h->root.u.def.section->output_section->vma
3965
                       + h->root.u.def.section->output_offset);
3966
      rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3967
      rela.r_addend = 0;
3968
      elf64_x86_64_append_rela (output_bfd, htab->srelbss, &rela);
3969
    }
3970
 
3971
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
3972
     be NULL for local symbols.  */
3973
  if (sym != NULL
3974
      && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3975
          || h == htab->elf.hgot))
3976
    sym->st_shndx = SHN_ABS;
3977
 
3978
  return TRUE;
3979
}
3980
 
3981
/* Finish up local dynamic symbol handling.  We set the contents of
3982
   various dynamic sections here.  */
3983
 
3984
static bfd_boolean
3985
elf64_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
3986
{
3987
  struct elf_link_hash_entry *h
3988
    = (struct elf_link_hash_entry *) *slot;
3989
  struct bfd_link_info *info
3990
    = (struct bfd_link_info *) inf;
3991
 
3992
  return elf64_x86_64_finish_dynamic_symbol (info->output_bfd,
3993
                                             info, h, NULL);
3994
}
3995
 
3996
/* Used to decide how to sort relocs in an optimal manner for the
3997
   dynamic linker, before writing them out.  */
3998
 
3999
static enum elf_reloc_type_class
4000
elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
4001
{
4002
  switch ((int) ELF64_R_TYPE (rela->r_info))
4003
    {
4004
    case R_X86_64_RELATIVE:
4005
      return reloc_class_relative;
4006
    case R_X86_64_JUMP_SLOT:
4007
      return reloc_class_plt;
4008
    case R_X86_64_COPY:
4009
      return reloc_class_copy;
4010
    default:
4011
      return reloc_class_normal;
4012
    }
4013
}
4014
 
4015
/* Finish up the dynamic sections.  */
4016
 
4017
static bfd_boolean
4018
elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4019
{
4020
  struct elf64_x86_64_link_hash_table *htab;
4021
  bfd *dynobj;
4022
  asection *sdyn;
4023
 
4024
  htab = elf64_x86_64_hash_table (info);
4025
  if (htab == NULL)
4026
    return FALSE;
4027
 
4028
  dynobj = htab->elf.dynobj;
4029
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4030
 
4031
  if (htab->elf.dynamic_sections_created)
4032
    {
4033
      Elf64_External_Dyn *dyncon, *dynconend;
4034
 
4035
      if (sdyn == NULL || htab->elf.sgot == NULL)
4036
        abort ();
4037
 
4038
      dyncon = (Elf64_External_Dyn *) sdyn->contents;
4039
      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4040
      for (; dyncon < dynconend; dyncon++)
4041
        {
4042
          Elf_Internal_Dyn dyn;
4043
          asection *s;
4044
 
4045
          bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4046
 
4047
          switch (dyn.d_tag)
4048
            {
4049
            default:
4050
              continue;
4051
 
4052
            case DT_PLTGOT:
4053
              s = htab->elf.sgotplt;
4054
              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4055
              break;
4056
 
4057
            case DT_JMPREL:
4058
              dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4059
              break;
4060
 
4061
            case DT_PLTRELSZ:
4062
              s = htab->elf.srelplt->output_section;
4063
              dyn.d_un.d_val = s->size;
4064
              break;
4065
 
4066
            case DT_RELASZ:
4067
              /* The procedure linkage table relocs (DT_JMPREL) should
4068
                 not be included in the overall relocs (DT_RELA).
4069
                 Therefore, we override the DT_RELASZ entry here to
4070
                 make it not include the JMPREL relocs.  Since the
4071
                 linker script arranges for .rela.plt to follow all
4072
                 other relocation sections, we don't have to worry
4073
                 about changing the DT_RELA entry.  */
4074
              if (htab->elf.srelplt != NULL)
4075
                {
4076
                  s = htab->elf.srelplt->output_section;
4077
                  dyn.d_un.d_val -= s->size;
4078
                }
4079
              break;
4080
 
4081
            case DT_TLSDESC_PLT:
4082
              s = htab->elf.splt;
4083
              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4084
                + htab->tlsdesc_plt;
4085
              break;
4086
 
4087
            case DT_TLSDESC_GOT:
4088
              s = htab->elf.sgot;
4089
              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4090
                + htab->tlsdesc_got;
4091
              break;
4092
            }
4093
 
4094
          bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4095
        }
4096
 
4097
      /* Fill in the special first entry in the procedure linkage table.  */
4098
      if (htab->elf.splt && htab->elf.splt->size > 0)
4099
        {
4100
          /* Fill in the first entry in the procedure linkage table.  */
4101
          memcpy (htab->elf.splt->contents, elf64_x86_64_plt0_entry,
4102
                  PLT_ENTRY_SIZE);
4103
          /* Add offset for pushq GOT+8(%rip), since the instruction
4104
             uses 6 bytes subtract this value.  */
4105
          bfd_put_32 (output_bfd,
4106
                      (htab->elf.sgotplt->output_section->vma
4107
                       + htab->elf.sgotplt->output_offset
4108
                       + 8
4109
                       - htab->elf.splt->output_section->vma
4110
                       - htab->elf.splt->output_offset
4111
                       - 6),
4112
                      htab->elf.splt->contents + 2);
4113
          /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4114
             the end of the instruction.  */
4115
          bfd_put_32 (output_bfd,
4116
                      (htab->elf.sgotplt->output_section->vma
4117
                       + htab->elf.sgotplt->output_offset
4118
                       + 16
4119
                       - htab->elf.splt->output_section->vma
4120
                       - htab->elf.splt->output_offset
4121
                       - 12),
4122
                      htab->elf.splt->contents + 8);
4123
 
4124
          elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4125
            PLT_ENTRY_SIZE;
4126
 
4127
          if (htab->tlsdesc_plt)
4128
            {
4129
              bfd_put_64 (output_bfd, (bfd_vma) 0,
4130
                          htab->elf.sgot->contents + htab->tlsdesc_got);
4131
 
4132
              memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4133
                      elf64_x86_64_plt0_entry,
4134
                      PLT_ENTRY_SIZE);
4135
 
4136
              /* Add offset for pushq GOT+8(%rip), since the
4137
                 instruction uses 6 bytes subtract this value.  */
4138
              bfd_put_32 (output_bfd,
4139
                          (htab->elf.sgotplt->output_section->vma
4140
                           + htab->elf.sgotplt->output_offset
4141
                           + 8
4142
                           - htab->elf.splt->output_section->vma
4143
                           - htab->elf.splt->output_offset
4144
                           - htab->tlsdesc_plt
4145
                           - 6),
4146
                          htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4147
              /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4148
                 htab->tlsdesc_got. The 12 is the offset to the end of
4149
                 the instruction.  */
4150
              bfd_put_32 (output_bfd,
4151
                          (htab->elf.sgot->output_section->vma
4152
                           + htab->elf.sgot->output_offset
4153
                           + htab->tlsdesc_got
4154
                           - htab->elf.splt->output_section->vma
4155
                           - htab->elf.splt->output_offset
4156
                           - htab->tlsdesc_plt
4157
                           - 12),
4158
                          htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4159
            }
4160
        }
4161
    }
4162
 
4163
  if (htab->elf.sgotplt)
4164
    {
4165
      /* Fill in the first three entries in the global offset table.  */
4166
      if (htab->elf.sgotplt->size > 0)
4167
        {
4168
          /* Set the first entry in the global offset table to the address of
4169
             the dynamic section.  */
4170
          if (sdyn == NULL)
4171
            bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4172
          else
4173
            bfd_put_64 (output_bfd,
4174
                        sdyn->output_section->vma + sdyn->output_offset,
4175
                        htab->elf.sgotplt->contents);
4176
          /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
4177
          bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4178
          bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4179
        }
4180
 
4181
      elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4182
        GOT_ENTRY_SIZE;
4183
    }
4184
 
4185
  if (htab->elf.sgot && htab->elf.sgot->size > 0)
4186
    elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4187
      = GOT_ENTRY_SIZE;
4188
 
4189
  /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
4190
  htab_traverse (htab->loc_hash_table,
4191
                 elf64_x86_64_finish_local_dynamic_symbol,
4192
                 info);
4193
 
4194
  return TRUE;
4195
}
4196
 
4197
/* Return address for Ith PLT stub in section PLT, for relocation REL
4198
   or (bfd_vma) -1 if it should not be included.  */
4199
 
4200
static bfd_vma
4201
elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4202
                          const arelent *rel ATTRIBUTE_UNUSED)
4203
{
4204
  return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4205
}
4206
 
4207
/* Handle an x86-64 specific section when reading an object file.  This
4208
   is called when elfcode.h finds a section with an unknown type.  */
4209
 
4210
static bfd_boolean
4211
elf64_x86_64_section_from_shdr (bfd *abfd,
4212
                                Elf_Internal_Shdr *hdr,
4213
                                const char *name,
4214
                                int shindex)
4215
{
4216
  if (hdr->sh_type != SHT_X86_64_UNWIND)
4217
    return FALSE;
4218
 
4219
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4220
    return FALSE;
4221
 
4222
  return TRUE;
4223
}
4224
 
4225
/* Hook called by the linker routine which adds symbols from an object
4226
   file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4227
   of .bss.  */
4228
 
4229
static bfd_boolean
4230
elf64_x86_64_add_symbol_hook (bfd *abfd,
4231
                              struct bfd_link_info *info,
4232
                              Elf_Internal_Sym *sym,
4233
                              const char **namep ATTRIBUTE_UNUSED,
4234
                              flagword *flagsp ATTRIBUTE_UNUSED,
4235
                              asection **secp,
4236
                              bfd_vma *valp)
4237
{
4238
  asection *lcomm;
4239
 
4240
  switch (sym->st_shndx)
4241
    {
4242
    case SHN_X86_64_LCOMMON:
4243
      lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4244
      if (lcomm == NULL)
4245
        {
4246
          lcomm = bfd_make_section_with_flags (abfd,
4247
                                               "LARGE_COMMON",
4248
                                               (SEC_ALLOC
4249
                                                | SEC_IS_COMMON
4250
                                                | SEC_LINKER_CREATED));
4251
          if (lcomm == NULL)
4252
            return FALSE;
4253
          elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4254
        }
4255
      *secp = lcomm;
4256
      *valp = sym->st_size;
4257
      break;
4258
    }
4259
 
4260
  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4261
    elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4262
 
4263
  return TRUE;
4264
}
4265
 
4266
 
4267
/* Given a BFD section, try to locate the corresponding ELF section
4268
   index.  */
4269
 
4270
static bfd_boolean
4271
elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4272
                                           asection *sec, int *index_return)
4273
{
4274
  if (sec == &_bfd_elf_large_com_section)
4275
    {
4276
      *index_return = SHN_X86_64_LCOMMON;
4277
      return TRUE;
4278
    }
4279
  return FALSE;
4280
}
4281
 
4282
/* Process a symbol.  */
4283
 
4284
static void
4285
elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4286
                                asymbol *asym)
4287
{
4288
  elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4289
 
4290
  switch (elfsym->internal_elf_sym.st_shndx)
4291
    {
4292
    case SHN_X86_64_LCOMMON:
4293
      asym->section = &_bfd_elf_large_com_section;
4294
      asym->value = elfsym->internal_elf_sym.st_size;
4295
      /* Common symbol doesn't set BSF_GLOBAL.  */
4296
      asym->flags &= ~BSF_GLOBAL;
4297
      break;
4298
    }
4299
}
4300
 
4301
static bfd_boolean
4302
elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4303
{
4304
  return (sym->st_shndx == SHN_COMMON
4305
          || sym->st_shndx == SHN_X86_64_LCOMMON);
4306
}
4307
 
4308
static unsigned int
4309
elf64_x86_64_common_section_index (asection *sec)
4310
{
4311
  if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4312
    return SHN_COMMON;
4313
  else
4314
    return SHN_X86_64_LCOMMON;
4315
}
4316
 
4317
static asection *
4318
elf64_x86_64_common_section (asection *sec)
4319
{
4320
  if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4321
    return bfd_com_section_ptr;
4322
  else
4323
    return &_bfd_elf_large_com_section;
4324
}
4325
 
4326
static bfd_boolean
4327
elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4328
                           struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4329
                           struct elf_link_hash_entry *h,
4330
                           Elf_Internal_Sym *sym,
4331
                           asection **psec,
4332
                           bfd_vma *pvalue ATTRIBUTE_UNUSED,
4333
                           unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4334
                           bfd_boolean *skip ATTRIBUTE_UNUSED,
4335
                           bfd_boolean *override ATTRIBUTE_UNUSED,
4336
                           bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4337
                           bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4338
                           bfd_boolean *newdef ATTRIBUTE_UNUSED,
4339
                           bfd_boolean *newdyn,
4340
                           bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4341
                           bfd_boolean *newweak ATTRIBUTE_UNUSED,
4342
                           bfd *abfd ATTRIBUTE_UNUSED,
4343
                           asection **sec,
4344
                           bfd_boolean *olddef ATTRIBUTE_UNUSED,
4345
                           bfd_boolean *olddyn,
4346
                           bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4347
                           bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4348
                           bfd *oldbfd,
4349
                           asection **oldsec)
4350
{
4351
  /* A normal common symbol and a large common symbol result in a
4352
     normal common symbol.  We turn the large common symbol into a
4353
     normal one.  */
4354
  if (!*olddyn
4355
      && h->root.type == bfd_link_hash_common
4356
      && !*newdyn
4357
      && bfd_is_com_section (*sec)
4358
      && *oldsec != *sec)
4359
    {
4360
      if (sym->st_shndx == SHN_COMMON
4361
          && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4362
        {
4363
          h->root.u.c.p->section
4364
            = bfd_make_section_old_way (oldbfd, "COMMON");
4365
          h->root.u.c.p->section->flags = SEC_ALLOC;
4366
        }
4367
      else if (sym->st_shndx == SHN_X86_64_LCOMMON
4368
               && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4369
        *psec = *sec = bfd_com_section_ptr;
4370
    }
4371
 
4372
  return TRUE;
4373
}
4374
 
4375
static int
4376
elf64_x86_64_additional_program_headers (bfd *abfd,
4377
                                         struct bfd_link_info *info ATTRIBUTE_UNUSED)
4378
{
4379
  asection *s;
4380
  int count = 0;
4381
 
4382
  /* Check to see if we need a large readonly segment.  */
4383
  s = bfd_get_section_by_name (abfd, ".lrodata");
4384
  if (s && (s->flags & SEC_LOAD))
4385
    count++;
4386
 
4387
  /* Check to see if we need a large data segment.  Since .lbss sections
4388
     is placed right after the .bss section, there should be no need for
4389
     a large data segment just because of .lbss.  */
4390
  s = bfd_get_section_by_name (abfd, ".ldata");
4391
  if (s && (s->flags & SEC_LOAD))
4392
    count++;
4393
 
4394
  return count;
4395
}
4396
 
4397
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
4398
 
4399
static bfd_boolean
4400
elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4401
{
4402
  if (h->plt.offset != (bfd_vma) -1
4403
      && !h->def_regular
4404
      && !h->pointer_equality_needed)
4405
    return FALSE;
4406
 
4407
  return _bfd_elf_hash_symbol (h);
4408
}
4409
 
4410
static const struct bfd_elf_special_section
4411
  elf64_x86_64_special_sections[]=
4412
{
4413
  { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4414
  { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4415
  { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4416
  { STRING_COMMA_LEN (".lbss"),            -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4417
  { STRING_COMMA_LEN (".ldata"),           -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4418
  { STRING_COMMA_LEN (".lrodata"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4419
  { NULL,                       0,          0, 0,            0 }
4420
};
4421
 
4422
#define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_vec
4423
#define TARGET_LITTLE_NAME                  "elf64-x86-64"
4424
#define ELF_ARCH                            bfd_arch_i386
4425
#define ELF_MACHINE_CODE                    EM_X86_64
4426
#define ELF_MAXPAGESIZE                     0x200000
4427
#define ELF_MINPAGESIZE                     0x1000
4428
#define ELF_COMMONPAGESIZE                  0x1000
4429
 
4430
#define elf_backend_can_gc_sections         1
4431
#define elf_backend_can_refcount            1
4432
#define elf_backend_want_got_plt            1
4433
#define elf_backend_plt_readonly            1
4434
#define elf_backend_want_plt_sym            0
4435
#define elf_backend_got_header_size         (GOT_ENTRY_SIZE*3)
4436
#define elf_backend_rela_normal             1
4437
 
4438
#define elf_info_to_howto                   elf64_x86_64_info_to_howto
4439
 
4440
#define bfd_elf64_bfd_link_hash_table_create \
4441
  elf64_x86_64_link_hash_table_create
4442
#define bfd_elf64_bfd_link_hash_table_free \
4443
  elf64_x86_64_link_hash_table_free
4444
#define bfd_elf64_bfd_reloc_type_lookup     elf64_x86_64_reloc_type_lookup
4445
#define bfd_elf64_bfd_reloc_name_lookup \
4446
  elf64_x86_64_reloc_name_lookup
4447
 
4448
#define elf_backend_adjust_dynamic_symbol   elf64_x86_64_adjust_dynamic_symbol
4449
#define elf_backend_relocs_compatible       _bfd_elf_relocs_compatible
4450
#define elf_backend_check_relocs            elf64_x86_64_check_relocs
4451
#define elf_backend_copy_indirect_symbol    elf64_x86_64_copy_indirect_symbol
4452
#define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4453
#define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4454
#define elf_backend_finish_dynamic_symbol   elf64_x86_64_finish_dynamic_symbol
4455
#define elf_backend_gc_mark_hook            elf64_x86_64_gc_mark_hook
4456
#define elf_backend_gc_sweep_hook           elf64_x86_64_gc_sweep_hook
4457
#define elf_backend_grok_prstatus           elf64_x86_64_grok_prstatus
4458
#define elf_backend_grok_psinfo             elf64_x86_64_grok_psinfo
4459
#define elf_backend_reloc_type_class        elf64_x86_64_reloc_type_class
4460
#define elf_backend_relocate_section        elf64_x86_64_relocate_section
4461
#define elf_backend_size_dynamic_sections   elf64_x86_64_size_dynamic_sections
4462
#define elf_backend_always_size_sections    elf64_x86_64_always_size_sections
4463
#define elf_backend_init_index_section      _bfd_elf_init_1_index_section
4464
#define elf_backend_plt_sym_val             elf64_x86_64_plt_sym_val
4465
#define elf_backend_object_p                elf64_x86_64_elf_object_p
4466
#define bfd_elf64_mkobject                  elf64_x86_64_mkobject
4467
 
4468
#define elf_backend_section_from_shdr \
4469
        elf64_x86_64_section_from_shdr
4470
 
4471
#define elf_backend_section_from_bfd_section \
4472
  elf64_x86_64_elf_section_from_bfd_section
4473
#define elf_backend_add_symbol_hook \
4474
  elf64_x86_64_add_symbol_hook
4475
#define elf_backend_symbol_processing \
4476
  elf64_x86_64_symbol_processing
4477
#define elf_backend_common_section_index \
4478
  elf64_x86_64_common_section_index
4479
#define elf_backend_common_section \
4480
  elf64_x86_64_common_section
4481
#define elf_backend_common_definition \
4482
  elf64_x86_64_common_definition
4483
#define elf_backend_merge_symbol \
4484
  elf64_x86_64_merge_symbol
4485
#define elf_backend_special_sections \
4486
  elf64_x86_64_special_sections
4487
#define elf_backend_additional_program_headers \
4488
  elf64_x86_64_additional_program_headers
4489
#define elf_backend_hash_symbol \
4490
  elf64_x86_64_hash_symbol
4491
 
4492
#undef  elf_backend_post_process_headers
4493
#define elf_backend_post_process_headers  _bfd_elf_set_osabi
4494
 
4495
#include "elf64-target.h"
4496
 
4497
/* FreeBSD support.  */
4498
 
4499
#undef  TARGET_LITTLE_SYM
4500
#define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_freebsd_vec
4501
#undef  TARGET_LITTLE_NAME
4502
#define TARGET_LITTLE_NAME                  "elf64-x86-64-freebsd"
4503
 
4504
#undef  ELF_OSABI
4505
#define ELF_OSABI                           ELFOSABI_FREEBSD
4506
 
4507
#undef  elf64_bed
4508
#define elf64_bed elf64_x86_64_fbsd_bed
4509
 
4510
#include "elf64-target.h"
4511
 
4512
/* Intel L1OM support.  */
4513
 
4514
static bfd_boolean
4515
elf64_l1om_elf_object_p (bfd *abfd)
4516
{
4517
  /* Set the right machine number for an L1OM elf64 file.  */
4518
  bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
4519
  return TRUE;
4520
}
4521
 
4522
#undef  TARGET_LITTLE_SYM
4523
#define TARGET_LITTLE_SYM                   bfd_elf64_l1om_vec
4524
#undef  TARGET_LITTLE_NAME
4525
#define TARGET_LITTLE_NAME                  "elf64-l1om"
4526
#undef ELF_ARCH
4527
#define ELF_ARCH                            bfd_arch_l1om
4528
 
4529
#undef  ELF_MACHINE_CODE
4530
#define ELF_MACHINE_CODE                    EM_L1OM
4531
 
4532
#undef  ELF_OSABI
4533
 
4534
#undef  elf64_bed
4535
#define elf64_bed elf64_l1om_bed
4536
 
4537
#undef elf_backend_object_p
4538
#define elf_backend_object_p                elf64_l1om_elf_object_p
4539
 
4540
#undef  elf_backend_post_process_headers
4541
 
4542
#include "elf64-target.h"
4543
 
4544
/* FreeBSD L1OM support.  */
4545
 
4546
#undef  TARGET_LITTLE_SYM
4547
#define TARGET_LITTLE_SYM                   bfd_elf64_l1om_freebsd_vec
4548
#undef  TARGET_LITTLE_NAME
4549
#define TARGET_LITTLE_NAME                  "elf64-l1om-freebsd"
4550
 
4551
#undef  ELF_OSABI
4552
#define ELF_OSABI                           ELFOSABI_FREEBSD
4553
 
4554
#undef  elf64_bed
4555
#define elf64_bed elf64_l1om_fbsd_bed
4556
 
4557
#undef  elf_backend_post_process_headers
4558
#define elf_backend_post_process_headers  _bfd_elf_set_osabi
4559
 
4560
#include "elf64-target.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.