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