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