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