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