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