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