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