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