2009-03-12 H.J. Lu <hongjiu.lu@intel.com>
[external/binutils.git] / bfd / elf64-x86-64.c
1 /* X86-64 specific support for 64-bit ELF
2    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3    Free Software Foundation, Inc.
4    Contributed by Jan Hubicka <jh@suse.cz>.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "bfd_stdint.h"
29
30 #include "elf/x86-64.h"
31
32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
33 #define MINUS_ONE (~ (bfd_vma) 0)
34
35 /* The relocation "howto" table.  Order of fields:
36    type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
37    special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset.  */
38 static reloc_howto_type x86_64_elf_howto_table[] =
39 {
40   HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
41         bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
42         FALSE),
43   HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
44         bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
45         FALSE),
46   HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
47         bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
48         TRUE),
49   HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
50         bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
51         FALSE),
52   HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
53         bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
54         TRUE),
55   HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56         bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
57         FALSE),
58   HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
59         bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
60         MINUS_ONE, FALSE),
61   HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62         bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
63         MINUS_ONE, FALSE),
64   HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
65         bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
66         MINUS_ONE, FALSE),
67   HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
68         bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
69         0xffffffff, TRUE),
70   HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
71         bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
72         FALSE),
73   HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
74         bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
75         FALSE),
76   HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
77         bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
78   HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
79         bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
80   HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
81         bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
82   HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
83         bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
84   HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
85         bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
86         MINUS_ONE, FALSE),
87   HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
88         bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
89         MINUS_ONE, FALSE),
90   HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
91         bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
92         MINUS_ONE, FALSE),
93   HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
94         bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
95         0xffffffff, TRUE),
96   HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
97         bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
98         0xffffffff, TRUE),
99   HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
100         bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
101         0xffffffff, FALSE),
102   HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
103         bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
104         0xffffffff, TRUE),
105   HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
106         bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
107         0xffffffff, FALSE),
108   HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
109         bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
110         TRUE),
111   HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
112         bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
113         FALSE, MINUS_ONE, MINUS_ONE, FALSE),
114   HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
115         bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
116         FALSE, 0xffffffff, 0xffffffff, TRUE),
117   HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
118         bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
119         FALSE),
120   HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
121         bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
122         MINUS_ONE, TRUE),
123   HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
124         bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
125         FALSE, MINUS_ONE, MINUS_ONE, TRUE),
126   HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
127         bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
128         MINUS_ONE, FALSE),
129   HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
130         bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
131         MINUS_ONE, FALSE),
132   EMPTY_HOWTO (32),
133   EMPTY_HOWTO (33),
134   HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
135         complain_overflow_bitfield, bfd_elf_generic_reloc,
136         "R_X86_64_GOTPC32_TLSDESC",
137         FALSE, 0xffffffff, 0xffffffff, TRUE),
138   HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
139         complain_overflow_dont, bfd_elf_generic_reloc,
140         "R_X86_64_TLSDESC_CALL",
141         FALSE, 0, 0, FALSE),
142   HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
143         complain_overflow_bitfield, bfd_elf_generic_reloc,
144         "R_X86_64_TLSDESC",
145         FALSE, MINUS_ONE, MINUS_ONE, FALSE),
146
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       /* Use strncmp to check __tls_get_addr since __tls_get_addr
814          may be versioned.  */ 
815       return (h != NULL
816               && h->root.root.string != NULL
817               && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
818                   || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
819               && (strncmp (h->root.root.string,
820                            "__tls_get_addr", 14) == 0));
821
822     case R_X86_64_GOTTPOFF:
823       /* Check transition from IE access model:
824                 movq foo@gottpoff(%rip), %reg
825                 addq foo@gottpoff(%rip), %reg
826        */
827
828       if (offset < 3 || (offset + 4) > sec->size)
829         return FALSE;
830
831       val = bfd_get_8 (abfd, contents + offset - 3);
832       if (val != 0x48 && val != 0x4c)
833         return FALSE;
834
835       val = bfd_get_8 (abfd, contents + offset - 2);
836       if (val != 0x8b && val != 0x03)
837         return FALSE;
838
839       val = bfd_get_8 (abfd, contents + offset - 1);
840       return (val & 0xc7) == 5;
841
842     case R_X86_64_GOTPC32_TLSDESC:
843       /* Check transition from GDesc access model:
844                 leaq x@tlsdesc(%rip), %rax
845
846          Make sure it's a leaq adding rip to a 32-bit offset
847          into any register, although it's probably almost always
848          going to be rax.  */
849
850       if (offset < 3 || (offset + 4) > sec->size)
851         return FALSE;
852
853       val = bfd_get_8 (abfd, contents + offset - 3);
854       if ((val & 0xfb) != 0x48)
855         return FALSE;
856
857       if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
858         return FALSE;
859
860       val = bfd_get_8 (abfd, contents + offset - 1);
861       return (val & 0xc7) == 0x05;
862
863     case R_X86_64_TLSDESC_CALL:
864       /* Check transition from GDesc access model:
865                 call *x@tlsdesc(%rax)
866        */
867       if (offset + 2 <= sec->size)
868         {
869           /* Make sure that it's a call *x@tlsdesc(%rax).  */
870           static x86_64_opcode16 call = { { 0xff, 0x10 } };
871           return bfd_get_16 (abfd, contents + offset) == call.i;
872         }
873
874       return FALSE;
875
876     default:
877       abort ();
878     }
879 }
880
881 /* Return TRUE if the TLS access transition is OK or no transition
882    will be performed.  Update R_TYPE if there is a transition.  */
883
884 static bfd_boolean
885 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
886                              asection *sec, bfd_byte *contents,
887                              Elf_Internal_Shdr *symtab_hdr,
888                              struct elf_link_hash_entry **sym_hashes,
889                              unsigned int *r_type, int tls_type,
890                              const Elf_Internal_Rela *rel,
891                              const Elf_Internal_Rela *relend,
892                              struct elf_link_hash_entry *h)
893 {
894   unsigned int from_type = *r_type;
895   unsigned int to_type = from_type;
896   bfd_boolean check = TRUE;
897
898   switch (from_type)
899     {
900     case R_X86_64_TLSGD:
901     case R_X86_64_GOTPC32_TLSDESC:
902     case R_X86_64_TLSDESC_CALL:
903     case R_X86_64_GOTTPOFF:
904       if (!info->shared)
905         {
906           if (h == NULL)
907             to_type = R_X86_64_TPOFF32;
908           else
909             to_type = R_X86_64_GOTTPOFF;
910         }
911
912       /* When we are called from elf64_x86_64_relocate_section,
913          CONTENTS isn't NULL and there may be additional transitions
914          based on TLS_TYPE.  */
915       if (contents != NULL)
916         {
917           unsigned int new_to_type = to_type;
918
919           if (!info->shared
920               && h != NULL
921               && h->dynindx == -1
922               && tls_type == GOT_TLS_IE)
923             new_to_type = R_X86_64_TPOFF32;
924
925           if (to_type == R_X86_64_TLSGD
926               || to_type == R_X86_64_GOTPC32_TLSDESC
927               || to_type == R_X86_64_TLSDESC_CALL)
928             {
929               if (tls_type == GOT_TLS_IE)
930                 new_to_type = R_X86_64_GOTTPOFF;
931             }
932
933           /* We checked the transition before when we were called from
934              elf64_x86_64_check_relocs.  We only want to check the new
935              transition which hasn't been checked before.  */
936           check = new_to_type != to_type && from_type == to_type;
937           to_type = new_to_type;
938         }
939
940       break;
941
942     case R_X86_64_TLSLD:
943       if (!info->shared)
944         to_type = R_X86_64_TPOFF32;
945       break;
946
947     default:
948       return TRUE;
949     }
950
951   /* Return TRUE if there is no transition.  */
952   if (from_type == to_type)
953     return TRUE;
954
955   /* Check if the transition can be performed.  */
956   if (check
957       && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
958                                               symtab_hdr, sym_hashes,
959                                               from_type, rel, relend))
960     {
961       reloc_howto_type *from, *to;
962
963       from = elf64_x86_64_rtype_to_howto (abfd, from_type);
964       to = elf64_x86_64_rtype_to_howto (abfd, to_type);
965
966       (*_bfd_error_handler)
967         (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
968            "in section `%A' failed"),
969          abfd, sec, from->name, to->name,
970          h ? h->root.root.string : "a local symbol",
971          (unsigned long) rel->r_offset);
972       bfd_set_error (bfd_error_bad_value);
973       return FALSE;
974     }
975
976   *r_type = to_type;
977   return TRUE;
978 }
979
980 /* Look through the relocs for a section during the first phase, and
981    calculate needed space in the global offset table, procedure
982    linkage table, and dynamic reloc sections.  */
983
984 static bfd_boolean
985 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
986                            asection *sec,
987                            const Elf_Internal_Rela *relocs)
988 {
989   struct elf64_x86_64_link_hash_table *htab;
990   Elf_Internal_Shdr *symtab_hdr;
991   struct elf_link_hash_entry **sym_hashes;
992   const Elf_Internal_Rela *rel;
993   const Elf_Internal_Rela *rel_end;
994   asection *sreloc;
995
996   if (info->relocatable)
997     return TRUE;
998
999   BFD_ASSERT (is_x86_64_elf (abfd));
1000
1001   htab = elf64_x86_64_hash_table (info);
1002   symtab_hdr = &elf_symtab_hdr (abfd);
1003   sym_hashes = elf_sym_hashes (abfd);
1004
1005   sreloc = NULL;
1006
1007   rel_end = relocs + sec->reloc_count;
1008   for (rel = relocs; rel < rel_end; rel++)
1009     {
1010       unsigned int r_type;
1011       unsigned long r_symndx;
1012       struct elf_link_hash_entry *h;
1013
1014       r_symndx = ELF64_R_SYM (rel->r_info);
1015       r_type = ELF64_R_TYPE (rel->r_info);
1016
1017       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1018         {
1019           (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1020                                  abfd, r_symndx);
1021           return FALSE;
1022         }
1023
1024       if (r_symndx < symtab_hdr->sh_info)
1025         h = NULL;
1026       else
1027         {
1028           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1029           while (h->root.type == bfd_link_hash_indirect
1030                  || h->root.type == bfd_link_hash_warning)
1031             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1032         }
1033
1034       if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1035                                          symtab_hdr, sym_hashes,
1036                                          &r_type, GOT_UNKNOWN,
1037                                          rel, rel_end, h))
1038         return FALSE;
1039
1040       switch (r_type)
1041         {
1042         case R_X86_64_TLSLD:
1043           htab->tls_ld_got.refcount += 1;
1044           goto create_got;
1045
1046         case R_X86_64_TPOFF32:
1047           if (info->shared)
1048             {
1049               (*_bfd_error_handler)
1050                 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1051                  abfd,
1052                  x86_64_elf_howto_table[r_type].name,
1053                  (h) ? h->root.root.string : "a local symbol");
1054               bfd_set_error (bfd_error_bad_value);
1055               return FALSE;
1056             }
1057           break;
1058
1059         case R_X86_64_GOTTPOFF:
1060           if (info->shared)
1061             info->flags |= DF_STATIC_TLS;
1062           /* Fall through */
1063
1064         case R_X86_64_GOT32:
1065         case R_X86_64_GOTPCREL:
1066         case R_X86_64_TLSGD:
1067         case R_X86_64_GOT64:
1068         case R_X86_64_GOTPCREL64:
1069         case R_X86_64_GOTPLT64:
1070         case R_X86_64_GOTPC32_TLSDESC:
1071         case R_X86_64_TLSDESC_CALL:
1072           /* This symbol requires a global offset table entry.  */
1073           {
1074             int tls_type, old_tls_type;
1075
1076             switch (r_type)
1077               {
1078               default: tls_type = GOT_NORMAL; break;
1079               case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1080               case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1081               case R_X86_64_GOTPC32_TLSDESC:
1082               case R_X86_64_TLSDESC_CALL:
1083                 tls_type = GOT_TLS_GDESC; break;
1084               }
1085
1086             if (h != NULL)
1087               {
1088                 if (r_type == R_X86_64_GOTPLT64)
1089                   {
1090                     /* This relocation indicates that we also need
1091                        a PLT entry, as this is a function.  We don't need
1092                        a PLT entry for local symbols.  */
1093                     h->needs_plt = 1;
1094                     h->plt.refcount += 1;
1095                   }
1096                 h->got.refcount += 1;
1097                 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1098               }
1099             else
1100               {
1101                 bfd_signed_vma *local_got_refcounts;
1102
1103                 /* This is a global offset table entry for a local symbol.  */
1104                 local_got_refcounts = elf_local_got_refcounts (abfd);
1105                 if (local_got_refcounts == NULL)
1106                   {
1107                     bfd_size_type size;
1108
1109                     size = symtab_hdr->sh_info;
1110                     size *= sizeof (bfd_signed_vma)
1111                       + sizeof (bfd_vma) + sizeof (char);
1112                     local_got_refcounts = ((bfd_signed_vma *)
1113                                            bfd_zalloc (abfd, size));
1114                     if (local_got_refcounts == NULL)
1115                       return FALSE;
1116                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1117                     elf64_x86_64_local_tlsdesc_gotent (abfd)
1118                       = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1119                     elf64_x86_64_local_got_tls_type (abfd)
1120                       = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1121                   }
1122                 local_got_refcounts[r_symndx] += 1;
1123                 old_tls_type
1124                   = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1125               }
1126
1127             /* If a TLS symbol is accessed using IE at least once,
1128                there is no point to use dynamic model for it.  */
1129             if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1130                 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1131                     || tls_type != GOT_TLS_IE))
1132               {
1133                 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1134                   tls_type = old_tls_type;
1135                 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1136                          && GOT_TLS_GD_ANY_P (tls_type))
1137                   tls_type |= old_tls_type;
1138                 else
1139                   {
1140                     (*_bfd_error_handler)
1141                       (_("%B: '%s' accessed both as normal and thread local symbol"),
1142                        abfd, h ? h->root.root.string : "<local>");
1143                     return FALSE;
1144                   }
1145               }
1146
1147             if (old_tls_type != tls_type)
1148               {
1149                 if (h != NULL)
1150                   elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1151                 else
1152                   elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1153               }
1154           }
1155           /* Fall through */
1156
1157         case R_X86_64_GOTOFF64:
1158         case R_X86_64_GOTPC32:
1159         case R_X86_64_GOTPC64:
1160         create_got:
1161           if (htab->sgot == NULL)
1162             {
1163               if (htab->elf.dynobj == NULL)
1164                 htab->elf.dynobj = abfd;
1165               if (!create_got_section (htab->elf.dynobj, info))
1166                 return FALSE;
1167             }
1168           break;
1169
1170         case R_X86_64_PLT32:
1171           /* This symbol requires a procedure linkage table entry.  We
1172              actually build the entry in adjust_dynamic_symbol,
1173              because this might be a case of linking PIC code which is
1174              never referenced by a dynamic object, in which case we
1175              don't need to generate a procedure linkage table entry
1176              after all.  */
1177
1178           /* If this is a local symbol, we resolve it directly without
1179              creating a procedure linkage table entry.  */
1180           if (h == NULL)
1181             continue;
1182
1183           h->needs_plt = 1;
1184           h->plt.refcount += 1;
1185           break;
1186
1187         case R_X86_64_PLTOFF64:
1188           /* This tries to form the 'address' of a function relative
1189              to GOT.  For global symbols we need a PLT entry.  */
1190           if (h != NULL)
1191             {
1192               h->needs_plt = 1;
1193               h->plt.refcount += 1;
1194             }
1195           goto create_got;
1196
1197         case R_X86_64_8:
1198         case R_X86_64_16:
1199         case R_X86_64_32:
1200         case R_X86_64_32S:
1201           /* Let's help debug shared library creation.  These relocs
1202              cannot be used in shared libs.  Don't error out for
1203              sections we don't care about, such as debug sections or
1204              non-constant sections.  */
1205           if (info->shared
1206               && (sec->flags & SEC_ALLOC) != 0
1207               && (sec->flags & SEC_READONLY) != 0)
1208             {
1209               (*_bfd_error_handler)
1210                 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1211                  abfd,
1212                  x86_64_elf_howto_table[r_type].name,
1213                  (h) ? h->root.root.string : "a local symbol");
1214               bfd_set_error (bfd_error_bad_value);
1215               return FALSE;
1216             }
1217           /* Fall through.  */
1218
1219         case R_X86_64_PC8:
1220         case R_X86_64_PC16:
1221         case R_X86_64_PC32:
1222         case R_X86_64_PC64:
1223         case R_X86_64_64:
1224           if (h != NULL && !info->shared)
1225             {
1226               /* If this reloc is in a read-only section, we might
1227                  need a copy reloc.  We can't check reliably at this
1228                  stage whether the section is read-only, as input
1229                  sections have not yet been mapped to output sections.
1230                  Tentatively set the flag for now, and correct in
1231                  adjust_dynamic_symbol.  */
1232               h->non_got_ref = 1;
1233
1234               /* We may need a .plt entry if the function this reloc
1235                  refers to is in a shared lib.  */
1236               h->plt.refcount += 1;
1237               if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1238                 h->pointer_equality_needed = 1;
1239             }
1240
1241           /* If we are creating a shared library, and this is a reloc
1242              against a global symbol, or a non PC relative reloc
1243              against a local symbol, then we need to copy the reloc
1244              into the shared library.  However, if we are linking with
1245              -Bsymbolic, we do not need to copy a reloc against a
1246              global symbol which is defined in an object we are
1247              including in the link (i.e., DEF_REGULAR is set).  At
1248              this point we have not seen all the input files, so it is
1249              possible that DEF_REGULAR is not set now but will be set
1250              later (it is never cleared).  In case of a weak definition,
1251              DEF_REGULAR may be cleared later by a strong definition in
1252              a shared library.  We account for that possibility below by
1253              storing information in the relocs_copied field of the hash
1254              table entry.  A similar situation occurs when creating
1255              shared libraries and symbol visibility changes render the
1256              symbol local.
1257
1258              If on the other hand, we are creating an executable, we
1259              may need to keep relocations for symbols satisfied by a
1260              dynamic library if we manage to avoid copy relocs for the
1261              symbol.  */
1262
1263           if ((info->shared
1264                && (sec->flags & SEC_ALLOC) != 0
1265                && (((r_type != R_X86_64_PC8)
1266                     && (r_type != R_X86_64_PC16)
1267                     && (r_type != R_X86_64_PC32)
1268                     && (r_type != R_X86_64_PC64))
1269                    || (h != NULL
1270                        && (! SYMBOLIC_BIND (info, h)
1271                            || h->root.type == bfd_link_hash_defweak
1272                            || !h->def_regular))))
1273               || (ELIMINATE_COPY_RELOCS
1274                   && !info->shared
1275                   && (sec->flags & SEC_ALLOC) != 0
1276                   && h != NULL
1277                   && (h->root.type == bfd_link_hash_defweak
1278                       || !h->def_regular)))
1279             {
1280               struct elf64_x86_64_dyn_relocs *p;
1281               struct elf64_x86_64_dyn_relocs **head;
1282
1283               /* We must copy these reloc types into the output file.
1284                  Create a reloc section in dynobj and make room for
1285                  this reloc.  */
1286               if (sreloc == NULL)
1287                 {
1288                   if (htab->elf.dynobj == NULL)
1289                     htab->elf.dynobj = abfd;
1290
1291                   sreloc = _bfd_elf_make_dynamic_reloc_section
1292                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1293
1294                   if (sreloc == NULL)
1295                     return FALSE;
1296                 }
1297
1298               /* If this is a global symbol, we count the number of
1299                  relocations we need for this symbol.  */
1300               if (h != NULL)
1301                 {
1302                   head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1303                 }
1304               else
1305                 {
1306                   void **vpp;
1307                   /* Track dynamic relocs needed for local syms too.
1308                      We really need local syms available to do this
1309                      easily.  Oh well.  */
1310
1311                   asection *s;
1312                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1313                                                  sec, r_symndx);
1314                   if (s == NULL)
1315                     return FALSE;
1316
1317                   /* Beware of type punned pointers vs strict aliasing
1318                      rules.  */
1319                   vpp = &(elf_section_data (s)->local_dynrel);
1320                   head = (struct elf64_x86_64_dyn_relocs **)vpp;
1321                 }
1322
1323               p = *head;
1324               if (p == NULL || p->sec != sec)
1325                 {
1326                   bfd_size_type amt = sizeof *p;
1327                   p = ((struct elf64_x86_64_dyn_relocs *)
1328                        bfd_alloc (htab->elf.dynobj, amt));
1329                   if (p == NULL)
1330                     return FALSE;
1331                   p->next = *head;
1332                   *head = p;
1333                   p->sec = sec;
1334                   p->count = 0;
1335                   p->pc_count = 0;
1336                 }
1337
1338               p->count += 1;
1339               if (r_type == R_X86_64_PC8
1340                   || r_type == R_X86_64_PC16
1341                   || r_type == R_X86_64_PC32
1342                   || r_type == R_X86_64_PC64)
1343                 p->pc_count += 1;
1344             }
1345           break;
1346
1347           /* This relocation describes the C++ object vtable hierarchy.
1348              Reconstruct it for later use during GC.  */
1349         case R_X86_64_GNU_VTINHERIT:
1350           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1351             return FALSE;
1352           break;
1353
1354           /* This relocation describes which C++ vtable entries are actually
1355              used.  Record for later use during GC.  */
1356         case R_X86_64_GNU_VTENTRY:
1357           BFD_ASSERT (h != NULL);
1358           if (h != NULL
1359               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1360             return FALSE;
1361           break;
1362
1363         default:
1364           break;
1365         }
1366     }
1367
1368   return TRUE;
1369 }
1370
1371 /* Return the section that should be marked against GC for a given
1372    relocation.  */
1373
1374 static asection *
1375 elf64_x86_64_gc_mark_hook (asection *sec,
1376                            struct bfd_link_info *info,
1377                            Elf_Internal_Rela *rel,
1378                            struct elf_link_hash_entry *h,
1379                            Elf_Internal_Sym *sym)
1380 {
1381   if (h != NULL)
1382     switch (ELF64_R_TYPE (rel->r_info))
1383       {
1384       case R_X86_64_GNU_VTINHERIT:
1385       case R_X86_64_GNU_VTENTRY:
1386         return NULL;
1387       }
1388
1389   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1390 }
1391
1392 /* Update the got entry reference counts for the section being removed.  */
1393
1394 static bfd_boolean
1395 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1396                             asection *sec,
1397                             const Elf_Internal_Rela *relocs)
1398 {
1399   Elf_Internal_Shdr *symtab_hdr;
1400   struct elf_link_hash_entry **sym_hashes;
1401   bfd_signed_vma *local_got_refcounts;
1402   const Elf_Internal_Rela *rel, *relend;
1403
1404   if (info->relocatable)
1405     return TRUE;
1406
1407   elf_section_data (sec)->local_dynrel = NULL;
1408
1409   symtab_hdr = &elf_symtab_hdr (abfd);
1410   sym_hashes = elf_sym_hashes (abfd);
1411   local_got_refcounts = elf_local_got_refcounts (abfd);
1412
1413   relend = relocs + sec->reloc_count;
1414   for (rel = relocs; rel < relend; rel++)
1415     {
1416       unsigned long r_symndx;
1417       unsigned int r_type;
1418       struct elf_link_hash_entry *h = NULL;
1419
1420       r_symndx = ELF64_R_SYM (rel->r_info);
1421       if (r_symndx >= symtab_hdr->sh_info)
1422         {
1423           struct elf64_x86_64_link_hash_entry *eh;
1424           struct elf64_x86_64_dyn_relocs **pp;
1425           struct elf64_x86_64_dyn_relocs *p;
1426
1427           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1428           while (h->root.type == bfd_link_hash_indirect
1429                  || h->root.type == bfd_link_hash_warning)
1430             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1431           eh = (struct elf64_x86_64_link_hash_entry *) h;
1432
1433           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1434             if (p->sec == sec)
1435               {
1436                 /* Everything must go for SEC.  */
1437                 *pp = p->next;
1438                 break;
1439               }
1440         }
1441
1442       r_type = ELF64_R_TYPE (rel->r_info);
1443       if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1444                                          symtab_hdr, sym_hashes,
1445                                          &r_type, GOT_UNKNOWN,
1446                                          rel, relend, h))
1447         return FALSE;
1448
1449       switch (r_type)
1450         {
1451         case R_X86_64_TLSLD:
1452           if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1453             elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1454           break;
1455
1456         case R_X86_64_TLSGD:
1457         case R_X86_64_GOTPC32_TLSDESC:
1458         case R_X86_64_TLSDESC_CALL:
1459         case R_X86_64_GOTTPOFF:
1460         case R_X86_64_GOT32:
1461         case R_X86_64_GOTPCREL:
1462         case R_X86_64_GOT64:
1463         case R_X86_64_GOTPCREL64:
1464         case R_X86_64_GOTPLT64:
1465           if (h != NULL)
1466             {
1467               if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1468                 h->plt.refcount -= 1;
1469               if (h->got.refcount > 0)
1470                 h->got.refcount -= 1;
1471             }
1472           else if (local_got_refcounts != NULL)
1473             {
1474               if (local_got_refcounts[r_symndx] > 0)
1475                 local_got_refcounts[r_symndx] -= 1;
1476             }
1477           break;
1478
1479         case R_X86_64_8:
1480         case R_X86_64_16:
1481         case R_X86_64_32:
1482         case R_X86_64_64:
1483         case R_X86_64_32S:
1484         case R_X86_64_PC8:
1485         case R_X86_64_PC16:
1486         case R_X86_64_PC32:
1487         case R_X86_64_PC64:
1488           if (info->shared)
1489             break;
1490           /* Fall thru */
1491
1492         case R_X86_64_PLT32:
1493         case R_X86_64_PLTOFF64:
1494           if (h != NULL)
1495             {
1496               if (h->plt.refcount > 0)
1497                 h->plt.refcount -= 1;
1498             }
1499           break;
1500
1501         default:
1502           break;
1503         }
1504     }
1505
1506   return TRUE;
1507 }
1508
1509 /* Adjust a symbol defined by a dynamic object and referenced by a
1510    regular object.  The current definition is in some section of the
1511    dynamic object, but we're not including those sections.  We have to
1512    change the definition to something the rest of the link can
1513    understand.  */
1514
1515 static bfd_boolean
1516 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1517                                     struct elf_link_hash_entry *h)
1518 {
1519   struct elf64_x86_64_link_hash_table *htab;
1520   asection *s;
1521
1522   /* If this is a function, put it in the procedure linkage table.  We
1523      will fill in the contents of the procedure linkage table later,
1524      when we know the address of the .got section.  */
1525   if (h->type == STT_FUNC
1526       || h->needs_plt)
1527     {
1528       if (h->plt.refcount <= 0
1529           || SYMBOL_CALLS_LOCAL (info, h)
1530           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1531               && h->root.type == bfd_link_hash_undefweak))
1532         {
1533           /* This case can occur if we saw a PLT32 reloc in an input
1534              file, but the symbol was never referred to by a dynamic
1535              object, or if all references were garbage collected.  In
1536              such a case, we don't actually need to build a procedure
1537              linkage table, and we can just do a PC32 reloc instead.  */
1538           h->plt.offset = (bfd_vma) -1;
1539           h->needs_plt = 0;
1540         }
1541
1542       return TRUE;
1543     }
1544   else
1545     /* It's possible that we incorrectly decided a .plt reloc was
1546        needed for an R_X86_64_PC32 reloc to a non-function sym in
1547        check_relocs.  We can't decide accurately between function and
1548        non-function syms in check-relocs;  Objects loaded later in
1549        the link may change h->type.  So fix it now.  */
1550     h->plt.offset = (bfd_vma) -1;
1551
1552   /* If this is a weak symbol, and there is a real definition, the
1553      processor independent code will have arranged for us to see the
1554      real definition first, and we can just use the same value.  */
1555   if (h->u.weakdef != NULL)
1556     {
1557       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1558                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1559       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1560       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1561       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1562         h->non_got_ref = h->u.weakdef->non_got_ref;
1563       return TRUE;
1564     }
1565
1566   /* This is a reference to a symbol defined by a dynamic object which
1567      is not a function.  */
1568
1569   /* If we are creating a shared library, we must presume that the
1570      only references to the symbol are via the global offset table.
1571      For such cases we need not do anything here; the relocations will
1572      be handled correctly by relocate_section.  */
1573   if (info->shared)
1574     return TRUE;
1575
1576   /* If there are no references to this symbol that do not use the
1577      GOT, we don't need to generate a copy reloc.  */
1578   if (!h->non_got_ref)
1579     return TRUE;
1580
1581   /* If -z nocopyreloc was given, we won't generate them either.  */
1582   if (info->nocopyreloc)
1583     {
1584       h->non_got_ref = 0;
1585       return TRUE;
1586     }
1587
1588   if (ELIMINATE_COPY_RELOCS)
1589     {
1590       struct elf64_x86_64_link_hash_entry * eh;
1591       struct elf64_x86_64_dyn_relocs *p;
1592
1593       eh = (struct elf64_x86_64_link_hash_entry *) h;
1594       for (p = eh->dyn_relocs; p != NULL; p = p->next)
1595         {
1596           s = p->sec->output_section;
1597           if (s != NULL && (s->flags & SEC_READONLY) != 0)
1598             break;
1599         }
1600
1601       /* If we didn't find any dynamic relocs in read-only sections, then
1602          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1603       if (p == NULL)
1604         {
1605           h->non_got_ref = 0;
1606           return TRUE;
1607         }
1608     }
1609
1610   if (h->size == 0)
1611     {
1612       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1613                              h->root.root.string);
1614       return TRUE;
1615     }
1616
1617   /* We must allocate the symbol in our .dynbss section, which will
1618      become part of the .bss section of the executable.  There will be
1619      an entry for this symbol in the .dynsym section.  The dynamic
1620      object will contain position independent code, so all references
1621      from the dynamic object to this symbol will go through the global
1622      offset table.  The dynamic linker will use the .dynsym entry to
1623      determine the address it must put in the global offset table, so
1624      both the dynamic object and the regular object will refer to the
1625      same memory location for the variable.  */
1626
1627   htab = elf64_x86_64_hash_table (info);
1628
1629   /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1630      to copy the initial value out of the dynamic object and into the
1631      runtime process image.  */
1632   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1633     {
1634       htab->srelbss->size += sizeof (Elf64_External_Rela);
1635       h->needs_copy = 1;
1636     }
1637
1638   s = htab->sdynbss;
1639
1640   return _bfd_elf_adjust_dynamic_copy (h, s);
1641 }
1642
1643 /* Allocate space in .plt, .got and associated reloc sections for
1644    dynamic relocs.  */
1645
1646 static bfd_boolean
1647 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1648 {
1649   struct bfd_link_info *info;
1650   struct elf64_x86_64_link_hash_table *htab;
1651   struct elf64_x86_64_link_hash_entry *eh;
1652   struct elf64_x86_64_dyn_relocs *p;
1653
1654   if (h->root.type == bfd_link_hash_indirect)
1655     return TRUE;
1656
1657   if (h->root.type == bfd_link_hash_warning)
1658     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1659
1660   info = (struct bfd_link_info *) inf;
1661   htab = elf64_x86_64_hash_table (info);
1662
1663   if (htab->elf.dynamic_sections_created
1664       && h->plt.refcount > 0)
1665     {
1666       /* Make sure this symbol is output as a dynamic symbol.
1667          Undefined weak syms won't yet be marked as dynamic.  */
1668       if (h->dynindx == -1
1669           && !h->forced_local)
1670         {
1671           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1672             return FALSE;
1673         }
1674
1675       if (info->shared
1676           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1677         {
1678           asection *s = htab->splt;
1679
1680           /* If this is the first .plt entry, make room for the special
1681              first entry.  */
1682           if (s->size == 0)
1683             s->size += PLT_ENTRY_SIZE;
1684
1685           h->plt.offset = s->size;
1686
1687           /* If this symbol is not defined in a regular file, and we are
1688              not generating a shared library, then set the symbol to this
1689              location in the .plt.  This is required to make function
1690              pointers compare as equal between the normal executable and
1691              the shared library.  */
1692           if (! info->shared
1693               && !h->def_regular)
1694             {
1695               h->root.u.def.section = s;
1696               h->root.u.def.value = h->plt.offset;
1697             }
1698
1699           /* Make room for this entry.  */
1700           s->size += PLT_ENTRY_SIZE;
1701
1702           /* We also need to make an entry in the .got.plt section, which
1703              will be placed in the .got section by the linker script.  */
1704           htab->sgotplt->size += GOT_ENTRY_SIZE;
1705
1706           /* We also need to make an entry in the .rela.plt section.  */
1707           htab->srelplt->size += sizeof (Elf64_External_Rela);
1708           htab->srelplt->reloc_count++;
1709         }
1710       else
1711         {
1712           h->plt.offset = (bfd_vma) -1;
1713           h->needs_plt = 0;
1714         }
1715     }
1716   else
1717     {
1718       h->plt.offset = (bfd_vma) -1;
1719       h->needs_plt = 0;
1720     }
1721
1722   eh = (struct elf64_x86_64_link_hash_entry *) h;
1723   eh->tlsdesc_got = (bfd_vma) -1;
1724
1725   /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1726      make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
1727   if (h->got.refcount > 0
1728       && !info->shared
1729       && h->dynindx == -1
1730       && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1731     h->got.offset = (bfd_vma) -1;
1732   else if (h->got.refcount > 0)
1733     {
1734       asection *s;
1735       bfd_boolean dyn;
1736       int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1737
1738       /* Make sure this symbol is output as a dynamic symbol.
1739          Undefined weak syms won't yet be marked as dynamic.  */
1740       if (h->dynindx == -1
1741           && !h->forced_local)
1742         {
1743           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1744             return FALSE;
1745         }
1746
1747       if (GOT_TLS_GDESC_P (tls_type))
1748         {
1749           eh->tlsdesc_got = htab->sgotplt->size
1750             - elf64_x86_64_compute_jump_table_size (htab);
1751           htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1752           h->got.offset = (bfd_vma) -2;
1753         }
1754       if (! GOT_TLS_GDESC_P (tls_type)
1755           || GOT_TLS_GD_P (tls_type))
1756         {
1757           s = htab->sgot;
1758           h->got.offset = s->size;
1759           s->size += GOT_ENTRY_SIZE;
1760           if (GOT_TLS_GD_P (tls_type))
1761             s->size += GOT_ENTRY_SIZE;
1762         }
1763       dyn = htab->elf.dynamic_sections_created;
1764       /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1765          and two if global.
1766          R_X86_64_GOTTPOFF needs one dynamic relocation.  */
1767       if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1768           || tls_type == GOT_TLS_IE)
1769         htab->srelgot->size += sizeof (Elf64_External_Rela);
1770       else if (GOT_TLS_GD_P (tls_type))
1771         htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1772       else if (! GOT_TLS_GDESC_P (tls_type)
1773                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1774                    || h->root.type != bfd_link_hash_undefweak)
1775                && (info->shared
1776                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1777         htab->srelgot->size += sizeof (Elf64_External_Rela);
1778       if (GOT_TLS_GDESC_P (tls_type))
1779         {
1780           htab->srelplt->size += sizeof (Elf64_External_Rela);
1781           htab->tlsdesc_plt = (bfd_vma) -1;
1782         }
1783     }
1784   else
1785     h->got.offset = (bfd_vma) -1;
1786
1787   if (eh->dyn_relocs == NULL)
1788     return TRUE;
1789
1790   /* In the shared -Bsymbolic case, discard space allocated for
1791      dynamic pc-relative relocs against symbols which turn out to be
1792      defined in regular objects.  For the normal shared case, discard
1793      space for pc-relative relocs that have become local due to symbol
1794      visibility changes.  */
1795
1796   if (info->shared)
1797     {
1798       /* Relocs that use pc_count are those that appear on a call
1799          insn, or certain REL relocs that can generated via assembly.
1800          We want calls to protected symbols to resolve directly to the
1801          function rather than going via the plt.  If people want
1802          function pointer comparisons to work as expected then they
1803          should avoid writing weird assembly.  */
1804       if (SYMBOL_CALLS_LOCAL (info, h))
1805         {
1806           struct elf64_x86_64_dyn_relocs **pp;
1807
1808           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1809             {
1810               p->count -= p->pc_count;
1811               p->pc_count = 0;
1812               if (p->count == 0)
1813                 *pp = p->next;
1814               else
1815                 pp = &p->next;
1816             }
1817         }
1818
1819       /* Also discard relocs on undefined weak syms with non-default
1820          visibility.  */
1821       if (eh->dyn_relocs != NULL
1822           && h->root.type == bfd_link_hash_undefweak)
1823         {
1824           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1825             eh->dyn_relocs = NULL;
1826
1827           /* Make sure undefined weak symbols are output as a dynamic
1828              symbol in PIEs.  */
1829           else if (h->dynindx == -1
1830                    && !h->forced_local)
1831             {
1832               if (! bfd_elf_link_record_dynamic_symbol (info, h))
1833                 return FALSE;
1834             }
1835         }
1836     }
1837   else if (ELIMINATE_COPY_RELOCS)
1838     {
1839       /* For the non-shared case, discard space for relocs against
1840          symbols which turn out to need copy relocs or are not
1841          dynamic.  */
1842
1843       if (!h->non_got_ref
1844           && ((h->def_dynamic
1845                && !h->def_regular)
1846               || (htab->elf.dynamic_sections_created
1847                   && (h->root.type == bfd_link_hash_undefweak
1848                       || h->root.type == bfd_link_hash_undefined))))
1849         {
1850           /* Make sure this symbol is output as a dynamic symbol.
1851              Undefined weak syms won't yet be marked as dynamic.  */
1852           if (h->dynindx == -1
1853               && !h->forced_local)
1854             {
1855               if (! bfd_elf_link_record_dynamic_symbol (info, h))
1856                 return FALSE;
1857             }
1858
1859           /* If that succeeded, we know we'll be keeping all the
1860              relocs.  */
1861           if (h->dynindx != -1)
1862             goto keep;
1863         }
1864
1865       eh->dyn_relocs = NULL;
1866
1867     keep: ;
1868     }
1869
1870   /* Finally, allocate space.  */
1871   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1872     {
1873       asection * sreloc;
1874
1875       sreloc = elf_section_data (p->sec)->sreloc;
1876
1877       BFD_ASSERT (sreloc != NULL);
1878
1879       sreloc->size += p->count * sizeof (Elf64_External_Rela);
1880     }
1881
1882   return TRUE;
1883 }
1884
1885 /* Find any dynamic relocs that apply to read-only sections.  */
1886
1887 static bfd_boolean
1888 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1889 {
1890   struct elf64_x86_64_link_hash_entry *eh;
1891   struct elf64_x86_64_dyn_relocs *p;
1892
1893   if (h->root.type == bfd_link_hash_warning)
1894     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1895
1896   eh = (struct elf64_x86_64_link_hash_entry *) h;
1897   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1898     {
1899       asection *s = p->sec->output_section;
1900
1901       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1902         {
1903           struct bfd_link_info *info = (struct bfd_link_info *) inf;
1904
1905           info->flags |= DF_TEXTREL;
1906
1907           /* Not an error, just cut short the traversal.  */
1908           return FALSE;
1909         }
1910     }
1911   return TRUE;
1912 }
1913
1914 /* Set the sizes of the dynamic sections.  */
1915
1916 static bfd_boolean
1917 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1918                                     struct bfd_link_info *info)
1919 {
1920   struct elf64_x86_64_link_hash_table *htab;
1921   bfd *dynobj;
1922   asection *s;
1923   bfd_boolean relocs;
1924   bfd *ibfd;
1925
1926   htab = elf64_x86_64_hash_table (info);
1927   dynobj = htab->elf.dynobj;
1928   if (dynobj == NULL)
1929     abort ();
1930
1931   if (htab->elf.dynamic_sections_created)
1932     {
1933       /* Set the contents of the .interp section to the interpreter.  */
1934       if (info->executable)
1935         {
1936           s = bfd_get_section_by_name (dynobj, ".interp");
1937           if (s == NULL)
1938             abort ();
1939           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1940           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1941         }
1942     }
1943
1944   /* Set up .got offsets for local syms, and space for local dynamic
1945      relocs.  */
1946   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1947     {
1948       bfd_signed_vma *local_got;
1949       bfd_signed_vma *end_local_got;
1950       char *local_tls_type;
1951       bfd_vma *local_tlsdesc_gotent;
1952       bfd_size_type locsymcount;
1953       Elf_Internal_Shdr *symtab_hdr;
1954       asection *srel;
1955
1956       if (! is_x86_64_elf (ibfd))
1957         continue;
1958
1959       for (s = ibfd->sections; s != NULL; s = s->next)
1960         {
1961           struct elf64_x86_64_dyn_relocs *p;
1962
1963           for (p = (struct elf64_x86_64_dyn_relocs *)
1964                     (elf_section_data (s)->local_dynrel);
1965                p != NULL;
1966                p = p->next)
1967             {
1968               if (!bfd_is_abs_section (p->sec)
1969                   && bfd_is_abs_section (p->sec->output_section))
1970                 {
1971                   /* Input section has been discarded, either because
1972                      it is a copy of a linkonce section or due to
1973                      linker script /DISCARD/, so we'll be discarding
1974                      the relocs too.  */
1975                 }
1976               else if (p->count != 0)
1977                 {
1978                   srel = elf_section_data (p->sec)->sreloc;
1979                   srel->size += p->count * sizeof (Elf64_External_Rela);
1980                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1981                     info->flags |= DF_TEXTREL;
1982                 }
1983             }
1984         }
1985
1986       local_got = elf_local_got_refcounts (ibfd);
1987       if (!local_got)
1988         continue;
1989
1990       symtab_hdr = &elf_symtab_hdr (ibfd);
1991       locsymcount = symtab_hdr->sh_info;
1992       end_local_got = local_got + locsymcount;
1993       local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1994       local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
1995       s = htab->sgot;
1996       srel = htab->srelgot;
1997       for (; local_got < end_local_got;
1998            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1999         {
2000           *local_tlsdesc_gotent = (bfd_vma) -1;
2001           if (*local_got > 0)
2002             {
2003               if (GOT_TLS_GDESC_P (*local_tls_type))
2004                 {
2005                   *local_tlsdesc_gotent = htab->sgotplt->size
2006                     - elf64_x86_64_compute_jump_table_size (htab);
2007                   htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
2008                   *local_got = (bfd_vma) -2;
2009                 }
2010               if (! GOT_TLS_GDESC_P (*local_tls_type)
2011                   || GOT_TLS_GD_P (*local_tls_type))
2012                 {
2013                   *local_got = s->size;
2014                   s->size += GOT_ENTRY_SIZE;
2015                   if (GOT_TLS_GD_P (*local_tls_type))
2016                     s->size += GOT_ENTRY_SIZE;
2017                 }
2018               if (info->shared
2019                   || GOT_TLS_GD_ANY_P (*local_tls_type)
2020                   || *local_tls_type == GOT_TLS_IE)
2021                 {
2022                   if (GOT_TLS_GDESC_P (*local_tls_type))
2023                     {
2024                       htab->srelplt->size += sizeof (Elf64_External_Rela);
2025                       htab->tlsdesc_plt = (bfd_vma) -1;
2026                     }
2027                   if (! GOT_TLS_GDESC_P (*local_tls_type)
2028                       || GOT_TLS_GD_P (*local_tls_type))
2029                     srel->size += sizeof (Elf64_External_Rela);
2030                 }
2031             }
2032           else
2033             *local_got = (bfd_vma) -1;
2034         }
2035     }
2036
2037   if (htab->tls_ld_got.refcount > 0)
2038     {
2039       /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2040          relocs.  */
2041       htab->tls_ld_got.offset = htab->sgot->size;
2042       htab->sgot->size += 2 * GOT_ENTRY_SIZE;
2043       htab->srelgot->size += sizeof (Elf64_External_Rela);
2044     }
2045   else
2046     htab->tls_ld_got.offset = -1;
2047
2048   /* Allocate global sym .plt and .got entries, and space for global
2049      sym dynamic relocs.  */
2050   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2051
2052   /* For every jump slot reserved in the sgotplt, reloc_count is
2053      incremented.  However, when we reserve space for TLS descriptors,
2054      it's not incremented, so in order to compute the space reserved
2055      for them, it suffices to multiply the reloc count by the jump
2056      slot size.  */
2057   if (htab->srelplt)
2058     htab->sgotplt_jump_table_size
2059       = elf64_x86_64_compute_jump_table_size (htab);
2060
2061   if (htab->tlsdesc_plt)
2062     {
2063       /* If we're not using lazy TLS relocations, don't generate the
2064          PLT and GOT entries they require.  */
2065       if ((info->flags & DF_BIND_NOW))
2066         htab->tlsdesc_plt = 0;
2067       else
2068         {
2069           htab->tlsdesc_got = htab->sgot->size;
2070           htab->sgot->size += GOT_ENTRY_SIZE;
2071           /* Reserve room for the initial entry.
2072              FIXME: we could probably do away with it in this case.  */
2073           if (htab->splt->size == 0)
2074             htab->splt->size += PLT_ENTRY_SIZE;
2075           htab->tlsdesc_plt = htab->splt->size;
2076           htab->splt->size += PLT_ENTRY_SIZE;
2077         }
2078     }
2079
2080   /* We now have determined the sizes of the various dynamic sections.
2081      Allocate memory for them.  */
2082   relocs = FALSE;
2083   for (s = dynobj->sections; s != NULL; s = s->next)
2084     {
2085       if ((s->flags & SEC_LINKER_CREATED) == 0)
2086         continue;
2087
2088       if (s == htab->splt
2089           || s == htab->sgot
2090           || s == htab->sgotplt
2091           || s == htab->sdynbss)
2092         {
2093           /* Strip this section if we don't need it; see the
2094              comment below.  */
2095         }
2096       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2097         {
2098           if (s->size != 0 && s != htab->srelplt)
2099             relocs = TRUE;
2100
2101           /* We use the reloc_count field as a counter if we need
2102              to copy relocs into the output file.  */
2103           if (s != htab->srelplt)
2104             s->reloc_count = 0;
2105         }
2106       else
2107         {
2108           /* It's not one of our sections, so don't allocate space.  */
2109           continue;
2110         }
2111
2112       if (s->size == 0)
2113         {
2114           /* If we don't need this section, strip it from the
2115              output file.  This is mostly to handle .rela.bss and
2116              .rela.plt.  We must create both sections in
2117              create_dynamic_sections, because they must be created
2118              before the linker maps input sections to output
2119              sections.  The linker does that before
2120              adjust_dynamic_symbol is called, and it is that
2121              function which decides whether anything needs to go
2122              into these sections.  */
2123
2124           s->flags |= SEC_EXCLUDE;
2125           continue;
2126         }
2127
2128       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2129         continue;
2130
2131       /* Allocate memory for the section contents.  We use bfd_zalloc
2132          here in case unused entries are not reclaimed before the
2133          section's contents are written out.  This should not happen,
2134          but this way if it does, we get a R_X86_64_NONE reloc instead
2135          of garbage.  */
2136       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2137       if (s->contents == NULL)
2138         return FALSE;
2139     }
2140
2141   if (htab->elf.dynamic_sections_created)
2142     {
2143       /* Add some entries to the .dynamic section.  We fill in the
2144          values later, in elf64_x86_64_finish_dynamic_sections, but we
2145          must add the entries now so that we get the correct size for
2146          the .dynamic section.  The DT_DEBUG entry is filled in by the
2147          dynamic linker and used by the debugger.  */
2148 #define add_dynamic_entry(TAG, VAL) \
2149   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2150
2151       if (info->executable)
2152         {
2153           if (!add_dynamic_entry (DT_DEBUG, 0))
2154             return FALSE;
2155         }
2156
2157       if (htab->splt->size != 0)
2158         {
2159           if (!add_dynamic_entry (DT_PLTGOT, 0)
2160               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2161               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2162               || !add_dynamic_entry (DT_JMPREL, 0))
2163             return FALSE;
2164
2165           if (htab->tlsdesc_plt
2166               && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2167                   || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2168             return FALSE;
2169         }
2170
2171       if (relocs)
2172         {
2173           if (!add_dynamic_entry (DT_RELA, 0)
2174               || !add_dynamic_entry (DT_RELASZ, 0)
2175               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2176             return FALSE;
2177
2178           /* If any dynamic relocs apply to a read-only section,
2179              then we need a DT_TEXTREL entry.  */
2180           if ((info->flags & DF_TEXTREL) == 0)
2181             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
2182
2183           if ((info->flags & DF_TEXTREL) != 0)
2184             {
2185               if (!add_dynamic_entry (DT_TEXTREL, 0))
2186                 return FALSE;
2187             }
2188         }
2189     }
2190 #undef add_dynamic_entry
2191
2192   return TRUE;
2193 }
2194
2195 static bfd_boolean
2196 elf64_x86_64_always_size_sections (bfd *output_bfd,
2197                                    struct bfd_link_info *info)
2198 {
2199   asection *tls_sec = elf_hash_table (info)->tls_sec;
2200
2201   if (tls_sec)
2202     {
2203       struct elf_link_hash_entry *tlsbase;
2204
2205       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2206                                       "_TLS_MODULE_BASE_",
2207                                       FALSE, FALSE, FALSE);
2208
2209       if (tlsbase && tlsbase->type == STT_TLS)
2210         {
2211           struct bfd_link_hash_entry *bh = NULL;
2212           const struct elf_backend_data *bed
2213             = get_elf_backend_data (output_bfd);
2214
2215           if (!(_bfd_generic_link_add_one_symbol
2216                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2217                  tls_sec, 0, NULL, FALSE,
2218                  bed->collect, &bh)))
2219             return FALSE;
2220
2221           elf64_x86_64_hash_table (info)->tls_module_base = bh;
2222
2223           tlsbase = (struct elf_link_hash_entry *)bh;
2224           tlsbase->def_regular = 1;
2225           tlsbase->other = STV_HIDDEN;
2226           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2227         }
2228     }
2229
2230   return TRUE;
2231 }
2232
2233 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2234    executables.  Rather than setting it to the beginning of the TLS
2235    section, we have to set it to the end.  This function may be called
2236    multiple times, it is idempotent.  */
2237
2238 static void
2239 set_tls_module_base (struct bfd_link_info *info)
2240 {
2241   struct bfd_link_hash_entry *base;
2242
2243   if (!info->executable)
2244     return;
2245
2246   base = elf64_x86_64_hash_table (info)->tls_module_base;
2247
2248   if (!base)
2249     return;
2250
2251   base->u.def.value = elf_hash_table (info)->tls_size;
2252 }
2253
2254 /* Return the base VMA address which should be subtracted from real addresses
2255    when resolving @dtpoff relocation.
2256    This is PT_TLS segment p_vaddr.  */
2257
2258 static bfd_vma
2259 dtpoff_base (struct bfd_link_info *info)
2260 {
2261   /* If tls_sec is NULL, we should have signalled an error already.  */
2262   if (elf_hash_table (info)->tls_sec == NULL)
2263     return 0;
2264   return elf_hash_table (info)->tls_sec->vma;
2265 }
2266
2267 /* Return the relocation value for @tpoff relocation
2268    if STT_TLS virtual address is ADDRESS.  */
2269
2270 static bfd_vma
2271 tpoff (struct bfd_link_info *info, bfd_vma address)
2272 {
2273   struct elf_link_hash_table *htab = elf_hash_table (info);
2274
2275   /* If tls_segment is NULL, we should have signalled an error already.  */
2276   if (htab->tls_sec == NULL)
2277     return 0;
2278   return address - htab->tls_size - htab->tls_sec->vma;
2279 }
2280
2281 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2282    branch?  */
2283
2284 static bfd_boolean
2285 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2286 {
2287   /* Opcode             Instruction
2288      0xe8               call
2289      0xe9               jump
2290      0x0f 0x8x          conditional jump */
2291   return ((offset > 0
2292            && (contents [offset - 1] == 0xe8
2293                || contents [offset - 1] == 0xe9))
2294           || (offset > 1
2295               && contents [offset - 2] == 0x0f
2296               && (contents [offset - 1] & 0xf0) == 0x80));
2297 }
2298
2299 /* Relocate an x86_64 ELF section.  */
2300
2301 static bfd_boolean
2302 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2303                                bfd *input_bfd, asection *input_section,
2304                                bfd_byte *contents, Elf_Internal_Rela *relocs,
2305                                Elf_Internal_Sym *local_syms,
2306                                asection **local_sections)
2307 {
2308   struct elf64_x86_64_link_hash_table *htab;
2309   Elf_Internal_Shdr *symtab_hdr;
2310   struct elf_link_hash_entry **sym_hashes;
2311   bfd_vma *local_got_offsets;
2312   bfd_vma *local_tlsdesc_gotents;
2313   Elf_Internal_Rela *rel;
2314   Elf_Internal_Rela *relend;
2315
2316   BFD_ASSERT (is_x86_64_elf (input_bfd));
2317
2318   htab = elf64_x86_64_hash_table (info);
2319   symtab_hdr = &elf_symtab_hdr (input_bfd);
2320   sym_hashes = elf_sym_hashes (input_bfd);
2321   local_got_offsets = elf_local_got_offsets (input_bfd);
2322   local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2323
2324   set_tls_module_base (info);
2325
2326   rel = relocs;
2327   relend = relocs + input_section->reloc_count;
2328   for (; rel < relend; rel++)
2329     {
2330       unsigned int r_type;
2331       reloc_howto_type *howto;
2332       unsigned long r_symndx;
2333       struct elf_link_hash_entry *h;
2334       Elf_Internal_Sym *sym;
2335       asection *sec;
2336       bfd_vma off, offplt;
2337       bfd_vma relocation;
2338       bfd_boolean unresolved_reloc;
2339       bfd_reloc_status_type r;
2340       int tls_type;
2341
2342       r_type = ELF64_R_TYPE (rel->r_info);
2343       if (r_type == (int) R_X86_64_GNU_VTINHERIT
2344           || r_type == (int) R_X86_64_GNU_VTENTRY)
2345         continue;
2346
2347       if (r_type >= R_X86_64_max)
2348         {
2349           bfd_set_error (bfd_error_bad_value);
2350           return FALSE;
2351         }
2352
2353       howto = x86_64_elf_howto_table + r_type;
2354       r_symndx = ELF64_R_SYM (rel->r_info);
2355       h = NULL;
2356       sym = NULL;
2357       sec = NULL;
2358       unresolved_reloc = FALSE;
2359       if (r_symndx < symtab_hdr->sh_info)
2360         {
2361           sym = local_syms + r_symndx;
2362           sec = local_sections[r_symndx];
2363
2364           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2365         }
2366       else
2367         {
2368           bfd_boolean warned;
2369
2370           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2371                                    r_symndx, symtab_hdr, sym_hashes,
2372                                    h, sec, relocation,
2373                                    unresolved_reloc, warned);
2374         }
2375
2376       if (sec != NULL && elf_discarded_section (sec))
2377         {
2378           /* For relocs against symbols from removed linkonce sections,
2379              or sections discarded by a linker script, we just want the
2380              section contents zeroed.  Avoid any special processing.  */
2381           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2382           rel->r_info = 0;
2383           rel->r_addend = 0;
2384           continue;
2385         }
2386
2387       if (info->relocatable)
2388         continue;
2389
2390       /* When generating a shared object, the relocations handled here are
2391          copied into the output file to be resolved at run time.  */
2392       switch (r_type)
2393         {
2394         asection *base_got;
2395         case R_X86_64_GOT32:
2396         case R_X86_64_GOT64:
2397           /* Relocation is to the entry for this symbol in the global
2398              offset table.  */
2399         case R_X86_64_GOTPCREL:
2400         case R_X86_64_GOTPCREL64:
2401           /* Use global offset table entry as symbol value.  */
2402         case R_X86_64_GOTPLT64:
2403           /* This is the same as GOT64 for relocation purposes, but
2404              indicates the existence of a PLT entry.  The difficulty is,
2405              that we must calculate the GOT slot offset from the PLT
2406              offset, if this symbol got a PLT entry (it was global).
2407              Additionally if it's computed from the PLT entry, then that
2408              GOT offset is relative to .got.plt, not to .got.  */
2409           base_got = htab->sgot;
2410
2411           if (htab->sgot == NULL)
2412             abort ();
2413
2414           if (h != NULL)
2415             {
2416               bfd_boolean dyn;
2417
2418               off = h->got.offset;
2419               if (h->needs_plt
2420                   && h->plt.offset != (bfd_vma)-1
2421                   && off == (bfd_vma)-1)
2422                 {
2423                   /* We can't use h->got.offset here to save
2424                      state, or even just remember the offset, as
2425                      finish_dynamic_symbol would use that as offset into
2426                      .got.  */
2427                   bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2428                   off = (plt_index + 3) * GOT_ENTRY_SIZE;
2429                   base_got = htab->sgotplt;
2430                 }
2431
2432               dyn = htab->elf.dynamic_sections_created;
2433
2434               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2435                   || (info->shared
2436                       && SYMBOL_REFERENCES_LOCAL (info, h))
2437                   || (ELF_ST_VISIBILITY (h->other)
2438                       && h->root.type == bfd_link_hash_undefweak))
2439                 {
2440                   /* This is actually a static link, or it is a -Bsymbolic
2441                      link and the symbol is defined locally, or the symbol
2442                      was forced to be local because of a version file.  We
2443                      must initialize this entry in the global offset table.
2444                      Since the offset must always be a multiple of 8, we
2445                      use the least significant bit to record whether we
2446                      have initialized it already.
2447
2448                      When doing a dynamic link, we create a .rela.got
2449                      relocation entry to initialize the value.  This is
2450                      done in the finish_dynamic_symbol routine.  */
2451                   if ((off & 1) != 0)
2452                     off &= ~1;
2453                   else
2454                     {
2455                       bfd_put_64 (output_bfd, relocation,
2456                                   base_got->contents + off);
2457                       /* Note that this is harmless for the GOTPLT64 case,
2458                          as -1 | 1 still is -1.  */
2459                       h->got.offset |= 1;
2460                     }
2461                 }
2462               else
2463                 unresolved_reloc = FALSE;
2464             }
2465           else
2466             {
2467               if (local_got_offsets == NULL)
2468                 abort ();
2469
2470               off = local_got_offsets[r_symndx];
2471
2472               /* The offset must always be a multiple of 8.  We use
2473                  the least significant bit to record whether we have
2474                  already generated the necessary reloc.  */
2475               if ((off & 1) != 0)
2476                 off &= ~1;
2477               else
2478                 {
2479                   bfd_put_64 (output_bfd, relocation,
2480                               base_got->contents + off);
2481
2482                   if (info->shared)
2483                     {
2484                       asection *s;
2485                       Elf_Internal_Rela outrel;
2486                       bfd_byte *loc;
2487
2488                       /* We need to generate a R_X86_64_RELATIVE reloc
2489                          for the dynamic linker.  */
2490                       s = htab->srelgot;
2491                       if (s == NULL)
2492                         abort ();
2493
2494                       outrel.r_offset = (base_got->output_section->vma
2495                                          + base_got->output_offset
2496                                          + off);
2497                       outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2498                       outrel.r_addend = relocation;
2499                       loc = s->contents;
2500                       loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2501                       bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2502                     }
2503
2504                   local_got_offsets[r_symndx] |= 1;
2505                 }
2506             }
2507
2508           if (off >= (bfd_vma) -2)
2509             abort ();
2510
2511           relocation = base_got->output_section->vma
2512                        + base_got->output_offset + off;
2513           if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2514             relocation -= htab->sgotplt->output_section->vma
2515                           - htab->sgotplt->output_offset;
2516
2517           break;
2518
2519         case R_X86_64_GOTOFF64:
2520           /* Relocation is relative to the start of the global offset
2521              table.  */
2522
2523           /* Check to make sure it isn't a protected function symbol
2524              for shared library since it may not be local when used
2525              as function address.  */
2526           if (info->shared
2527               && h
2528               && h->def_regular
2529               && h->type == STT_FUNC
2530               && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2531             {
2532               (*_bfd_error_handler)
2533                 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2534                  input_bfd, h->root.root.string);
2535               bfd_set_error (bfd_error_bad_value);
2536               return FALSE;
2537             }
2538
2539           /* Note that sgot is not involved in this
2540              calculation.  We always want the start of .got.plt.  If we
2541              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2542              permitted by the ABI, we might have to change this
2543              calculation.  */
2544           relocation -= htab->sgotplt->output_section->vma
2545                         + htab->sgotplt->output_offset;
2546           break;
2547
2548         case R_X86_64_GOTPC32:
2549         case R_X86_64_GOTPC64:
2550           /* Use global offset table as symbol value.  */
2551           relocation = htab->sgotplt->output_section->vma
2552                        + htab->sgotplt->output_offset;
2553           unresolved_reloc = FALSE;
2554           break;
2555
2556         case R_X86_64_PLTOFF64:
2557           /* Relocation is PLT entry relative to GOT.  For local
2558              symbols it's the symbol itself relative to GOT.  */
2559           if (h != NULL
2560               /* See PLT32 handling.  */
2561               && h->plt.offset != (bfd_vma) -1
2562               && htab->splt != NULL)
2563             {
2564               relocation = (htab->splt->output_section->vma
2565                             + htab->splt->output_offset
2566                             + h->plt.offset);
2567               unresolved_reloc = FALSE;
2568             }
2569
2570           relocation -= htab->sgotplt->output_section->vma
2571                         + htab->sgotplt->output_offset;
2572           break;
2573
2574         case R_X86_64_PLT32:
2575           /* Relocation is to the entry for this symbol in the
2576              procedure linkage table.  */
2577
2578           /* Resolve a PLT32 reloc against a local symbol directly,
2579              without using the procedure linkage table.  */
2580           if (h == NULL)
2581             break;
2582
2583           if (h->plt.offset == (bfd_vma) -1
2584               || htab->splt == NULL)
2585             {
2586               /* We didn't make a PLT entry for this symbol.  This
2587                  happens when statically linking PIC code, or when
2588                  using -Bsymbolic.  */
2589               break;
2590             }
2591
2592           relocation = (htab->splt->output_section->vma
2593                         + htab->splt->output_offset
2594                         + h->plt.offset);
2595           unresolved_reloc = FALSE;
2596           break;
2597
2598         case R_X86_64_PC8:
2599         case R_X86_64_PC16:
2600         case R_X86_64_PC32:
2601           if (info->shared
2602               && (input_section->flags & SEC_ALLOC) != 0
2603               && (input_section->flags & SEC_READONLY) != 0
2604               && h != NULL)
2605             {
2606               bfd_boolean fail = FALSE;
2607               bfd_boolean branch
2608                 = (r_type == R_X86_64_PC32
2609                    && is_32bit_relative_branch (contents, rel->r_offset));
2610
2611               if (SYMBOL_REFERENCES_LOCAL (info, h))
2612                 {
2613                   /* Symbol is referenced locally.  Make sure it is
2614                      defined locally or for a branch.  */
2615                   fail = !h->def_regular && !branch;
2616                 }
2617               else
2618                 {
2619                   /* Symbol isn't referenced locally.  We only allow
2620                      branch to symbol with non-default visibility. */
2621                   fail = (!branch
2622                           || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
2623                 }
2624
2625               if (fail)
2626                 {
2627                   const char *fmt;
2628                   const char *v;
2629                   const char *pic = "";
2630
2631                   switch (ELF_ST_VISIBILITY (h->other))
2632                     {
2633                     case STV_HIDDEN:
2634                       v = _("hidden symbol");
2635                       break;
2636                     case STV_INTERNAL:
2637                       v = _("internal symbol");
2638                       break;
2639                     case STV_PROTECTED:
2640                       v = _("protected symbol");
2641                       break;
2642                     default:
2643                       v = _("symbol");
2644                       pic = _("; recompile with -fPIC");
2645                       break;
2646                     }
2647
2648                   if (h->def_regular)
2649                     fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
2650                   else
2651                     fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
2652
2653                   (*_bfd_error_handler) (fmt, input_bfd,
2654                                          x86_64_elf_howto_table[r_type].name,
2655                                          v,  h->root.root.string, pic);
2656                   bfd_set_error (bfd_error_bad_value);
2657                   return FALSE;
2658                 }
2659             }
2660           /* Fall through.  */
2661
2662         case R_X86_64_8:
2663         case R_X86_64_16:
2664         case R_X86_64_32:
2665         case R_X86_64_PC64:
2666         case R_X86_64_64:
2667           /* FIXME: The ABI says the linker should make sure the value is
2668              the same when it's zeroextended to 64 bit.  */
2669
2670           if ((input_section->flags & SEC_ALLOC) == 0)
2671             break;
2672
2673           if ((info->shared
2674                && (h == NULL
2675                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2676                    || h->root.type != bfd_link_hash_undefweak)
2677                && ((r_type != R_X86_64_PC8
2678                     && r_type != R_X86_64_PC16
2679                     && r_type != R_X86_64_PC32
2680                     && r_type != R_X86_64_PC64)
2681                    || !SYMBOL_CALLS_LOCAL (info, h)))
2682               || (ELIMINATE_COPY_RELOCS
2683                   && !info->shared
2684                   && h != NULL
2685                   && h->dynindx != -1
2686                   && !h->non_got_ref
2687                   && ((h->def_dynamic
2688                        && !h->def_regular)
2689                       || h->root.type == bfd_link_hash_undefweak
2690                       || h->root.type == bfd_link_hash_undefined)))
2691             {
2692               Elf_Internal_Rela outrel;
2693               bfd_byte *loc;
2694               bfd_boolean skip, relocate;
2695               asection *sreloc;
2696
2697               /* When generating a shared object, these relocations
2698                  are copied into the output file to be resolved at run
2699                  time.  */
2700               skip = FALSE;
2701               relocate = FALSE;
2702
2703               outrel.r_offset =
2704                 _bfd_elf_section_offset (output_bfd, info, input_section,
2705                                          rel->r_offset);
2706               if (outrel.r_offset == (bfd_vma) -1)
2707                 skip = TRUE;
2708               else if (outrel.r_offset == (bfd_vma) -2)
2709                 skip = TRUE, relocate = TRUE;
2710
2711               outrel.r_offset += (input_section->output_section->vma
2712                                   + input_section->output_offset);
2713
2714               if (skip)
2715                 memset (&outrel, 0, sizeof outrel);
2716
2717               /* h->dynindx may be -1 if this symbol was marked to
2718                  become local.  */
2719               else if (h != NULL
2720                        && h->dynindx != -1
2721                        && (r_type == R_X86_64_PC8
2722                            || r_type == R_X86_64_PC16
2723                            || r_type == R_X86_64_PC32
2724                            || r_type == R_X86_64_PC64
2725                            || !info->shared
2726                            || !SYMBOLIC_BIND (info, h)
2727                            || !h->def_regular))
2728                 {
2729                   outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2730                   outrel.r_addend = rel->r_addend;
2731                 }
2732               else
2733                 {
2734                   /* This symbol is local, or marked to become local.  */
2735                   if (r_type == R_X86_64_64)
2736                     {
2737                       relocate = TRUE;
2738                       outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2739                       outrel.r_addend = relocation + rel->r_addend;
2740                     }
2741                   else
2742                     {
2743                       long sindx;
2744
2745                       if (bfd_is_abs_section (sec))
2746                         sindx = 0;
2747                       else if (sec == NULL || sec->owner == NULL)
2748                         {
2749                           bfd_set_error (bfd_error_bad_value);
2750                           return FALSE;
2751                         }
2752                       else
2753                         {
2754                           asection *osec;
2755
2756                           /* We are turning this relocation into one
2757                              against a section symbol.  It would be
2758                              proper to subtract the symbol's value,
2759                              osec->vma, from the emitted reloc addend,
2760                              but ld.so expects buggy relocs.  */
2761                           osec = sec->output_section;
2762                           sindx = elf_section_data (osec)->dynindx;
2763                           if (sindx == 0)
2764                             {
2765                               asection *oi = htab->elf.text_index_section;
2766                               sindx = elf_section_data (oi)->dynindx;
2767                             }
2768                           BFD_ASSERT (sindx != 0);
2769                         }
2770
2771                       outrel.r_info = ELF64_R_INFO (sindx, r_type);
2772                       outrel.r_addend = relocation + rel->r_addend;
2773                     }
2774                 }
2775
2776               sreloc = elf_section_data (input_section)->sreloc;
2777                 
2778               BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
2779
2780               loc = sreloc->contents;
2781               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2782               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2783
2784               /* If this reloc is against an external symbol, we do
2785                  not want to fiddle with the addend.  Otherwise, we
2786                  need to include the symbol value so that it becomes
2787                  an addend for the dynamic reloc.  */
2788               if (! relocate)
2789                 continue;
2790             }
2791
2792           break;
2793
2794         case R_X86_64_TLSGD:
2795         case R_X86_64_GOTPC32_TLSDESC:
2796         case R_X86_64_TLSDESC_CALL:
2797         case R_X86_64_GOTTPOFF:
2798           tls_type = GOT_UNKNOWN;
2799           if (h == NULL && local_got_offsets)
2800             tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2801           else if (h != NULL)
2802             tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2803
2804           if (! elf64_x86_64_tls_transition (info, input_bfd,
2805                                              input_section, contents,
2806                                              symtab_hdr, sym_hashes,
2807                                              &r_type, tls_type, rel,
2808                                              relend, h))
2809             return FALSE;
2810
2811           if (r_type == R_X86_64_TPOFF32)
2812             {
2813               bfd_vma roff = rel->r_offset;
2814
2815               BFD_ASSERT (! unresolved_reloc);
2816
2817               if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2818                 {
2819                   /* GD->LE transition.
2820                      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2821                      .word 0x6666; rex64; call __tls_get_addr
2822                      Change it into:
2823                      movq %fs:0, %rax
2824                      leaq foo@tpoff(%rax), %rax */
2825                   memcpy (contents + roff - 4,
2826                           "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2827                           16);
2828                   bfd_put_32 (output_bfd, tpoff (info, relocation),
2829                               contents + roff + 8);
2830                   /* Skip R_X86_64_PC32/R_X86_64_PLT32.  */
2831                   rel++;
2832                   continue;
2833                 }
2834               else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2835                 {
2836                   /* GDesc -> LE transition.
2837                      It's originally something like:
2838                      leaq x@tlsdesc(%rip), %rax
2839
2840                      Change it to:
2841                      movl $x@tpoff, %rax
2842                    */
2843
2844                   unsigned int val, type, type2;
2845
2846                   type = bfd_get_8 (input_bfd, contents + roff - 3);
2847                   type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2848                   val = bfd_get_8 (input_bfd, contents + roff - 1);
2849                   bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2850                              contents + roff - 3);
2851                   bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2852                   bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2853                              contents + roff - 1);
2854                   bfd_put_32 (output_bfd, tpoff (info, relocation),
2855                               contents + roff);
2856                   continue;
2857                 }
2858               else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2859                 {
2860                   /* GDesc -> LE transition.
2861                      It's originally:
2862                      call *(%rax)
2863                      Turn it into:
2864                      xchg %ax,%ax.  */
2865                   bfd_put_8 (output_bfd, 0x66, contents + roff);
2866                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2867                   continue;
2868                 }
2869               else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
2870                 {
2871                   /* IE->LE transition:
2872                      Originally it can be one of:
2873                      movq foo@gottpoff(%rip), %reg
2874                      addq foo@gottpoff(%rip), %reg
2875                      We change it into:
2876                      movq $foo, %reg
2877                      leaq foo(%reg), %reg
2878                      addq $foo, %reg.  */
2879
2880                   unsigned int val, type, reg;
2881
2882                   val = bfd_get_8 (input_bfd, contents + roff - 3);
2883                   type = bfd_get_8 (input_bfd, contents + roff - 2);
2884                   reg = bfd_get_8 (input_bfd, contents + roff - 1);
2885                   reg >>= 3;
2886                   if (type == 0x8b)
2887                     {
2888                       /* movq */
2889                       if (val == 0x4c)
2890                         bfd_put_8 (output_bfd, 0x49,
2891                                    contents + roff - 3);
2892                       bfd_put_8 (output_bfd, 0xc7,
2893                                  contents + roff - 2);
2894                       bfd_put_8 (output_bfd, 0xc0 | reg,
2895                                  contents + roff - 1);
2896                     }
2897                   else if (reg == 4)
2898                     {
2899                       /* addq -> addq - addressing with %rsp/%r12 is
2900                          special  */
2901                       if (val == 0x4c)
2902                         bfd_put_8 (output_bfd, 0x49,
2903                                    contents + roff - 3);
2904                       bfd_put_8 (output_bfd, 0x81,
2905                                  contents + roff - 2);
2906                       bfd_put_8 (output_bfd, 0xc0 | reg,
2907                                  contents + roff - 1);
2908                     }
2909                   else
2910                     {
2911                       /* addq -> leaq */
2912                       if (val == 0x4c)
2913                         bfd_put_8 (output_bfd, 0x4d,
2914                                    contents + roff - 3);
2915                       bfd_put_8 (output_bfd, 0x8d,
2916                                  contents + roff - 2);
2917                       bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2918                                  contents + roff - 1);
2919                     }
2920                   bfd_put_32 (output_bfd, tpoff (info, relocation),
2921                               contents + roff);
2922                   continue;
2923                 }
2924               else
2925                 BFD_ASSERT (FALSE);
2926             }
2927
2928           if (htab->sgot == NULL)
2929             abort ();
2930
2931           if (h != NULL)
2932             {
2933               off = h->got.offset;
2934               offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2935             }
2936           else
2937             {
2938               if (local_got_offsets == NULL)
2939                 abort ();
2940
2941               off = local_got_offsets[r_symndx];
2942               offplt = local_tlsdesc_gotents[r_symndx];
2943             }
2944
2945           if ((off & 1) != 0)
2946             off &= ~1;
2947           else
2948             {
2949               Elf_Internal_Rela outrel;
2950               bfd_byte *loc;
2951               int dr_type, indx;
2952               asection *sreloc;
2953
2954               if (htab->srelgot == NULL)
2955                 abort ();
2956
2957               indx = h && h->dynindx != -1 ? h->dynindx : 0;
2958
2959               if (GOT_TLS_GDESC_P (tls_type))
2960                 {
2961                   outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2962                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2963                               + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2964                   outrel.r_offset = (htab->sgotplt->output_section->vma
2965                                      + htab->sgotplt->output_offset
2966                                      + offplt
2967                                      + htab->sgotplt_jump_table_size);
2968                   sreloc = htab->srelplt;
2969                   loc = sreloc->contents;
2970                   loc += sreloc->reloc_count++
2971                     * sizeof (Elf64_External_Rela);
2972                   BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2973                               <= sreloc->contents + sreloc->size);
2974                   if (indx == 0)
2975                     outrel.r_addend = relocation - dtpoff_base (info);
2976                   else
2977                     outrel.r_addend = 0;
2978                   bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2979                 }
2980
2981               sreloc = htab->srelgot;
2982
2983               outrel.r_offset = (htab->sgot->output_section->vma
2984                                  + htab->sgot->output_offset + off);
2985
2986               if (GOT_TLS_GD_P (tls_type))
2987                 dr_type = R_X86_64_DTPMOD64;
2988               else if (GOT_TLS_GDESC_P (tls_type))
2989                 goto dr_done;
2990               else
2991                 dr_type = R_X86_64_TPOFF64;
2992
2993               bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2994               outrel.r_addend = 0;
2995               if ((dr_type == R_X86_64_TPOFF64
2996                    || dr_type == R_X86_64_TLSDESC) && indx == 0)
2997                 outrel.r_addend = relocation - dtpoff_base (info);
2998               outrel.r_info = ELF64_R_INFO (indx, dr_type);
2999
3000               loc = sreloc->contents;
3001               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3002               BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3003                           <= sreloc->contents + sreloc->size);
3004               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3005
3006               if (GOT_TLS_GD_P (tls_type))
3007                 {
3008                   if (indx == 0)
3009                     {
3010                       BFD_ASSERT (! unresolved_reloc);
3011                       bfd_put_64 (output_bfd,
3012                                   relocation - dtpoff_base (info),
3013                                   htab->sgot->contents + off + GOT_ENTRY_SIZE);
3014                     }
3015                   else
3016                     {
3017                       bfd_put_64 (output_bfd, 0,
3018                                   htab->sgot->contents + off + GOT_ENTRY_SIZE);
3019                       outrel.r_info = ELF64_R_INFO (indx,
3020                                                     R_X86_64_DTPOFF64);
3021                       outrel.r_offset += GOT_ENTRY_SIZE;
3022                       sreloc->reloc_count++;
3023                       loc += sizeof (Elf64_External_Rela);
3024                       BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3025                                   <= sreloc->contents + sreloc->size);
3026                       bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3027                     }
3028                 }
3029
3030             dr_done:
3031               if (h != NULL)
3032                 h->got.offset |= 1;
3033               else
3034                 local_got_offsets[r_symndx] |= 1;
3035             }
3036
3037           if (off >= (bfd_vma) -2
3038               && ! GOT_TLS_GDESC_P (tls_type))
3039             abort ();
3040           if (r_type == ELF64_R_TYPE (rel->r_info))
3041             {
3042               if (r_type == R_X86_64_GOTPC32_TLSDESC
3043                   || r_type == R_X86_64_TLSDESC_CALL)
3044                 relocation = htab->sgotplt->output_section->vma
3045                   + htab->sgotplt->output_offset
3046                   + offplt + htab->sgotplt_jump_table_size;
3047               else
3048                 relocation = htab->sgot->output_section->vma
3049                   + htab->sgot->output_offset + off;
3050               unresolved_reloc = FALSE;
3051             }
3052           else
3053             {
3054               bfd_vma roff = rel->r_offset;
3055
3056               if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3057                 {
3058                   /* GD->IE transition.
3059                      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3060                      .word 0x6666; rex64; call __tls_get_addr@plt
3061                      Change it into:
3062                      movq %fs:0, %rax
3063                      addq foo@gottpoff(%rip), %rax */
3064                   memcpy (contents + roff - 4,
3065                           "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3066                           16);
3067
3068                   relocation = (htab->sgot->output_section->vma
3069                                 + htab->sgot->output_offset + off
3070                                 - roff
3071                                 - input_section->output_section->vma
3072                                 - input_section->output_offset
3073                                 - 12);
3074                   bfd_put_32 (output_bfd, relocation,
3075                               contents + roff + 8);
3076                   /* Skip R_X86_64_PLT32.  */
3077                   rel++;
3078                   continue;
3079                 }
3080               else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3081                 {
3082                   /* GDesc -> IE transition.
3083                      It's originally something like:
3084                      leaq x@tlsdesc(%rip), %rax
3085
3086                      Change it to:
3087                      movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3088                    */
3089
3090                   unsigned int val, type, type2;
3091
3092                   type = bfd_get_8 (input_bfd, contents + roff - 3);
3093                   type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3094                   val = bfd_get_8 (input_bfd, contents + roff - 1);
3095
3096                   /* Now modify the instruction as appropriate. To
3097                      turn a leaq into a movq in the form we use it, it
3098                      suffices to change the second byte from 0x8d to
3099                      0x8b.  */
3100                   bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3101
3102                   bfd_put_32 (output_bfd,
3103                               htab->sgot->output_section->vma
3104                               + htab->sgot->output_offset + off
3105                               - rel->r_offset
3106                               - input_section->output_section->vma
3107                               - input_section->output_offset
3108                               - 4,
3109                               contents + roff);
3110                   continue;
3111                 }
3112               else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3113                 {
3114                   /* GDesc -> IE transition.
3115                      It's originally:
3116                      call *(%rax)
3117
3118                      Change it to:
3119                      xchg %ax,%ax.  */
3120
3121                   unsigned int val, type;
3122
3123                   type = bfd_get_8 (input_bfd, contents + roff);
3124                   val = bfd_get_8 (input_bfd, contents + roff + 1);
3125                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3126                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3127                   continue;
3128                 }
3129               else
3130                 BFD_ASSERT (FALSE);
3131             }
3132           break;
3133
3134         case R_X86_64_TLSLD:
3135           if (! elf64_x86_64_tls_transition (info, input_bfd,
3136                                              input_section, contents,
3137                                              symtab_hdr, sym_hashes,
3138                                              &r_type, GOT_UNKNOWN,
3139                                              rel, relend, h))
3140             return FALSE;
3141
3142           if (r_type != R_X86_64_TLSLD)
3143             {
3144               /* LD->LE transition:
3145                  leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3146                  We change it into:
3147                  .word 0x6666; .byte 0x66; movl %fs:0, %rax.  */
3148
3149               BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3150               memcpy (contents + rel->r_offset - 3,
3151                       "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3152               /* Skip R_X86_64_PC32/R_X86_64_PLT32.  */
3153               rel++;
3154               continue;
3155             }
3156
3157           if (htab->sgot == NULL)
3158             abort ();
3159
3160           off = htab->tls_ld_got.offset;
3161           if (off & 1)
3162             off &= ~1;
3163           else
3164             {
3165               Elf_Internal_Rela outrel;
3166               bfd_byte *loc;
3167
3168               if (htab->srelgot == NULL)
3169                 abort ();
3170
3171               outrel.r_offset = (htab->sgot->output_section->vma
3172                                  + htab->sgot->output_offset + off);
3173
3174               bfd_put_64 (output_bfd, 0,
3175                           htab->sgot->contents + off);
3176               bfd_put_64 (output_bfd, 0,
3177                           htab->sgot->contents + off + GOT_ENTRY_SIZE);
3178               outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3179               outrel.r_addend = 0;
3180               loc = htab->srelgot->contents;
3181               loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3182               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3183               htab->tls_ld_got.offset |= 1;
3184             }
3185           relocation = htab->sgot->output_section->vma
3186                        + htab->sgot->output_offset + off;
3187           unresolved_reloc = FALSE;
3188           break;
3189
3190         case R_X86_64_DTPOFF32:
3191           if (info->shared || (input_section->flags & SEC_CODE) == 0)
3192             relocation -= dtpoff_base (info);
3193           else
3194             relocation = tpoff (info, relocation);
3195           break;
3196
3197         case R_X86_64_TPOFF32:
3198           BFD_ASSERT (! info->shared);
3199           relocation = tpoff (info, relocation);
3200           break;
3201
3202         default:
3203           break;
3204         }
3205
3206       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3207          because such sections are not SEC_ALLOC and thus ld.so will
3208          not process them.  */
3209       if (unresolved_reloc
3210           && !((input_section->flags & SEC_DEBUGGING) != 0
3211                && h->def_dynamic))
3212         (*_bfd_error_handler)
3213           (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3214            input_bfd,
3215            input_section,
3216            (long) rel->r_offset,
3217            howto->name,
3218            h->root.root.string);
3219
3220       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3221                                     contents, rel->r_offset,
3222                                     relocation, rel->r_addend);
3223
3224       if (r != bfd_reloc_ok)
3225         {
3226           const char *name;
3227
3228           if (h != NULL)
3229             name = h->root.root.string;
3230           else
3231             {
3232               name = bfd_elf_string_from_elf_section (input_bfd,
3233                                                       symtab_hdr->sh_link,
3234                                                       sym->st_name);
3235               if (name == NULL)
3236                 return FALSE;
3237               if (*name == '\0')
3238                 name = bfd_section_name (input_bfd, sec);
3239             }
3240
3241           if (r == bfd_reloc_overflow)
3242             {
3243               if (! ((*info->callbacks->reloc_overflow)
3244                      (info, (h ? &h->root : NULL), name, howto->name,
3245                       (bfd_vma) 0, input_bfd, input_section,
3246                       rel->r_offset)))
3247                 return FALSE;
3248             }
3249           else
3250             {
3251               (*_bfd_error_handler)
3252                 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3253                  input_bfd, input_section,
3254                  (long) rel->r_offset, name, (int) r);
3255               return FALSE;
3256             }
3257         }
3258     }
3259
3260   return TRUE;
3261 }
3262
3263 /* Finish up dynamic symbol handling.  We set the contents of various
3264    dynamic sections here.  */
3265
3266 static bfd_boolean
3267 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3268                                     struct bfd_link_info *info,
3269                                     struct elf_link_hash_entry *h,
3270                                     Elf_Internal_Sym *sym)
3271 {
3272   struct elf64_x86_64_link_hash_table *htab;
3273
3274   htab = elf64_x86_64_hash_table (info);
3275
3276   if (h->plt.offset != (bfd_vma) -1)
3277     {
3278       bfd_vma plt_index;
3279       bfd_vma got_offset;
3280       Elf_Internal_Rela rela;
3281       bfd_byte *loc;
3282
3283       /* This symbol has an entry in the procedure linkage table.  Set
3284          it up.  */
3285       if (h->dynindx == -1
3286           || htab->splt == NULL
3287           || htab->sgotplt == NULL
3288           || htab->srelplt == NULL)
3289         abort ();
3290
3291       /* Get the index in the procedure linkage table which
3292          corresponds to this symbol.  This is the index of this symbol
3293          in all the symbols for which we are making plt entries.  The
3294          first entry in the procedure linkage table is reserved.  */
3295       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3296
3297       /* Get the offset into the .got table of the entry that
3298          corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
3299          bytes. The first three are reserved for the dynamic linker.  */
3300       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3301
3302       /* Fill in the entry in the procedure linkage table.  */
3303       memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3304               PLT_ENTRY_SIZE);
3305
3306       /* Insert the relocation positions of the plt section.  The magic
3307          numbers at the end of the statements are the positions of the
3308          relocations in the plt section.  */
3309       /* Put offset for jmp *name@GOTPCREL(%rip), since the
3310          instruction uses 6 bytes, subtract this value.  */
3311       bfd_put_32 (output_bfd,
3312                       (htab->sgotplt->output_section->vma
3313                        + htab->sgotplt->output_offset
3314                        + got_offset
3315                        - htab->splt->output_section->vma
3316                        - htab->splt->output_offset
3317                        - h->plt.offset
3318                        - 6),
3319                   htab->splt->contents + h->plt.offset + 2);
3320       /* Put relocation index.  */
3321       bfd_put_32 (output_bfd, plt_index,
3322                   htab->splt->contents + h->plt.offset + 7);
3323       /* Put offset for jmp .PLT0.  */
3324       bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3325                   htab->splt->contents + h->plt.offset + 12);
3326
3327       /* Fill in the entry in the global offset table, initially this
3328          points to the pushq instruction in the PLT which is at offset 6.  */
3329       bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3330                                + htab->splt->output_offset
3331                                + h->plt.offset + 6),
3332                   htab->sgotplt->contents + got_offset);
3333
3334       /* Fill in the entry in the .rela.plt section.  */
3335       rela.r_offset = (htab->sgotplt->output_section->vma
3336                        + htab->sgotplt->output_offset
3337                        + got_offset);
3338       rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3339       rela.r_addend = 0;
3340       loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
3341       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3342
3343       if (!h->def_regular)
3344         {
3345           /* Mark the symbol as undefined, rather than as defined in
3346              the .plt section.  Leave the value if there were any
3347              relocations where pointer equality matters (this is a clue
3348              for the dynamic linker, to make function pointer
3349              comparisons work between an application and shared
3350              library), otherwise set it to zero.  If a function is only
3351              called from a binary, there is no need to slow down
3352              shared libraries because of that.  */
3353           sym->st_shndx = SHN_UNDEF;
3354           if (!h->pointer_equality_needed)
3355             sym->st_value = 0;
3356         }
3357     }
3358
3359   if (h->got.offset != (bfd_vma) -1
3360       && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3361       && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3362     {
3363       Elf_Internal_Rela rela;
3364       bfd_byte *loc;
3365
3366       /* This symbol has an entry in the global offset table.  Set it
3367          up.  */
3368       if (htab->sgot == NULL || htab->srelgot == NULL)
3369         abort ();
3370
3371       rela.r_offset = (htab->sgot->output_section->vma
3372                        + htab->sgot->output_offset
3373                        + (h->got.offset &~ (bfd_vma) 1));
3374
3375       /* If this is a static link, or it is a -Bsymbolic link and the
3376          symbol is defined locally or was forced to be local because
3377          of a version file, we just want to emit a RELATIVE reloc.
3378          The entry in the global offset table will already have been
3379          initialized in the relocate_section function.  */
3380       if (info->shared
3381           && SYMBOL_REFERENCES_LOCAL (info, h))
3382         {
3383           if (!h->def_regular)
3384             return FALSE;
3385           BFD_ASSERT((h->got.offset & 1) != 0);
3386           rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3387           rela.r_addend = (h->root.u.def.value
3388                            + h->root.u.def.section->output_section->vma
3389                            + h->root.u.def.section->output_offset);
3390         }
3391       else
3392         {
3393           BFD_ASSERT((h->got.offset & 1) == 0);
3394           bfd_put_64 (output_bfd, (bfd_vma) 0,
3395                       htab->sgot->contents + h->got.offset);
3396           rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3397           rela.r_addend = 0;
3398         }
3399
3400       loc = htab->srelgot->contents;
3401       loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3402       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3403     }
3404
3405   if (h->needs_copy)
3406     {
3407       Elf_Internal_Rela rela;
3408       bfd_byte *loc;
3409
3410       /* This symbol needs a copy reloc.  Set it up.  */
3411
3412       if (h->dynindx == -1
3413           || (h->root.type != bfd_link_hash_defined
3414               && h->root.type != bfd_link_hash_defweak)
3415           || htab->srelbss == NULL)
3416         abort ();
3417
3418       rela.r_offset = (h->root.u.def.value
3419                        + h->root.u.def.section->output_section->vma
3420                        + h->root.u.def.section->output_offset);
3421       rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3422       rela.r_addend = 0;
3423       loc = htab->srelbss->contents;
3424       loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3425       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3426     }
3427
3428   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
3429   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3430       || h == htab->elf.hgot)
3431     sym->st_shndx = SHN_ABS;
3432
3433   return TRUE;
3434 }
3435
3436 /* Used to decide how to sort relocs in an optimal manner for the
3437    dynamic linker, before writing them out.  */
3438
3439 static enum elf_reloc_type_class
3440 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3441 {
3442   switch ((int) ELF64_R_TYPE (rela->r_info))
3443     {
3444     case R_X86_64_RELATIVE:
3445       return reloc_class_relative;
3446     case R_X86_64_JUMP_SLOT:
3447       return reloc_class_plt;
3448     case R_X86_64_COPY:
3449       return reloc_class_copy;
3450     default:
3451       return reloc_class_normal;
3452     }
3453 }
3454
3455 /* Finish up the dynamic sections.  */
3456
3457 static bfd_boolean
3458 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3459 {
3460   struct elf64_x86_64_link_hash_table *htab;
3461   bfd *dynobj;
3462   asection *sdyn;
3463
3464   htab = elf64_x86_64_hash_table (info);
3465   dynobj = htab->elf.dynobj;
3466   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3467
3468   if (htab->elf.dynamic_sections_created)
3469     {
3470       Elf64_External_Dyn *dyncon, *dynconend;
3471
3472       if (sdyn == NULL || htab->sgot == NULL)
3473         abort ();
3474
3475       dyncon = (Elf64_External_Dyn *) sdyn->contents;
3476       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3477       for (; dyncon < dynconend; dyncon++)
3478         {
3479           Elf_Internal_Dyn dyn;
3480           asection *s;
3481
3482           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3483
3484           switch (dyn.d_tag)
3485             {
3486             default:
3487               continue;
3488
3489             case DT_PLTGOT:
3490               s = htab->sgotplt;
3491               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3492               break;
3493
3494             case DT_JMPREL:
3495               dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3496               break;
3497
3498             case DT_PLTRELSZ:
3499               s = htab->srelplt->output_section;
3500               dyn.d_un.d_val = s->size;
3501               break;
3502
3503             case DT_RELASZ:
3504               /* The procedure linkage table relocs (DT_JMPREL) should
3505                  not be included in the overall relocs (DT_RELA).
3506                  Therefore, we override the DT_RELASZ entry here to
3507                  make it not include the JMPREL relocs.  Since the
3508                  linker script arranges for .rela.plt to follow all
3509                  other relocation sections, we don't have to worry
3510                  about changing the DT_RELA entry.  */
3511               if (htab->srelplt != NULL)
3512                 {
3513                   s = htab->srelplt->output_section;
3514                   dyn.d_un.d_val -= s->size;
3515                 }
3516               break;
3517
3518             case DT_TLSDESC_PLT:
3519               s = htab->splt;
3520               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3521                 + htab->tlsdesc_plt;
3522               break;
3523
3524             case DT_TLSDESC_GOT:
3525               s = htab->sgot;
3526               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3527                 + htab->tlsdesc_got;
3528               break;
3529             }
3530
3531           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3532         }
3533
3534       /* Fill in the special first entry in the procedure linkage table.  */
3535       if (htab->splt && htab->splt->size > 0)
3536         {
3537           /* Fill in the first entry in the procedure linkage table.  */
3538           memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3539                   PLT_ENTRY_SIZE);
3540           /* Add offset for pushq GOT+8(%rip), since the instruction
3541              uses 6 bytes subtract this value.  */
3542           bfd_put_32 (output_bfd,
3543                       (htab->sgotplt->output_section->vma
3544                        + htab->sgotplt->output_offset
3545                        + 8
3546                        - htab->splt->output_section->vma
3547                        - htab->splt->output_offset
3548                        - 6),
3549                       htab->splt->contents + 2);
3550           /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3551              the end of the instruction.  */
3552           bfd_put_32 (output_bfd,
3553                       (htab->sgotplt->output_section->vma
3554                        + htab->sgotplt->output_offset
3555                        + 16
3556                        - htab->splt->output_section->vma
3557                        - htab->splt->output_offset
3558                        - 12),
3559                       htab->splt->contents + 8);
3560
3561           elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3562             PLT_ENTRY_SIZE;
3563
3564           if (htab->tlsdesc_plt)
3565             {
3566               bfd_put_64 (output_bfd, (bfd_vma) 0,
3567                           htab->sgot->contents + htab->tlsdesc_got);
3568
3569               memcpy (htab->splt->contents + htab->tlsdesc_plt,
3570                       elf64_x86_64_plt0_entry,
3571                       PLT_ENTRY_SIZE);
3572
3573               /* Add offset for pushq GOT+8(%rip), since the
3574                  instruction uses 6 bytes subtract this value.  */
3575               bfd_put_32 (output_bfd,
3576                           (htab->sgotplt->output_section->vma
3577                            + htab->sgotplt->output_offset
3578                            + 8
3579                            - htab->splt->output_section->vma
3580                            - htab->splt->output_offset
3581                            - htab->tlsdesc_plt
3582                            - 6),
3583                           htab->splt->contents + htab->tlsdesc_plt + 2);
3584               /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3585                  htab->tlsdesc_got. The 12 is the offset to the end of
3586                  the instruction.  */
3587               bfd_put_32 (output_bfd,
3588                           (htab->sgot->output_section->vma
3589                            + htab->sgot->output_offset
3590                            + htab->tlsdesc_got
3591                            - htab->splt->output_section->vma
3592                            - htab->splt->output_offset
3593                            - htab->tlsdesc_plt
3594                            - 12),
3595                           htab->splt->contents + htab->tlsdesc_plt + 8);
3596             }
3597         }
3598     }
3599
3600   if (htab->sgotplt)
3601     {
3602       /* Fill in the first three entries in the global offset table.  */
3603       if (htab->sgotplt->size > 0)
3604         {
3605           /* Set the first entry in the global offset table to the address of
3606              the dynamic section.  */
3607           if (sdyn == NULL)
3608             bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3609           else
3610             bfd_put_64 (output_bfd,
3611                         sdyn->output_section->vma + sdyn->output_offset,
3612                         htab->sgotplt->contents);
3613           /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
3614           bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3615           bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3616         }
3617
3618       elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3619         GOT_ENTRY_SIZE;
3620     }
3621
3622   if (htab->sgot && htab->sgot->size > 0)
3623     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3624       = GOT_ENTRY_SIZE;
3625
3626   return TRUE;
3627 }
3628
3629 /* Return address for Ith PLT stub in section PLT, for relocation REL
3630    or (bfd_vma) -1 if it should not be included.  */
3631
3632 static bfd_vma
3633 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3634                           const arelent *rel ATTRIBUTE_UNUSED)
3635 {
3636   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3637 }
3638
3639 /* Handle an x86-64 specific section when reading an object file.  This
3640    is called when elfcode.h finds a section with an unknown type.  */
3641
3642 static bfd_boolean
3643 elf64_x86_64_section_from_shdr (bfd *abfd,
3644                                 Elf_Internal_Shdr *hdr,
3645                                 const char *name,
3646                                 int shindex)
3647 {
3648   if (hdr->sh_type != SHT_X86_64_UNWIND)
3649     return FALSE;
3650
3651   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3652     return FALSE;
3653
3654   return TRUE;
3655 }
3656
3657 /* Hook called by the linker routine which adds symbols from an object
3658    file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3659    of .bss.  */
3660
3661 static bfd_boolean
3662 elf64_x86_64_add_symbol_hook (bfd *abfd,
3663                               struct bfd_link_info *info ATTRIBUTE_UNUSED,
3664                               Elf_Internal_Sym *sym,
3665                               const char **namep ATTRIBUTE_UNUSED,
3666                               flagword *flagsp ATTRIBUTE_UNUSED,
3667                               asection **secp, bfd_vma *valp)
3668 {
3669   asection *lcomm;
3670
3671   switch (sym->st_shndx)
3672     {
3673     case SHN_X86_64_LCOMMON:
3674       lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3675       if (lcomm == NULL)
3676         {
3677           lcomm = bfd_make_section_with_flags (abfd,
3678                                                "LARGE_COMMON",
3679                                                (SEC_ALLOC
3680                                                 | SEC_IS_COMMON
3681                                                 | SEC_LINKER_CREATED));
3682           if (lcomm == NULL)
3683             return FALSE;
3684           elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3685         }
3686       *secp = lcomm;
3687       *valp = sym->st_size;
3688       break;
3689     }
3690   return TRUE;
3691 }
3692
3693
3694 /* Given a BFD section, try to locate the corresponding ELF section
3695    index.  */
3696
3697 static bfd_boolean
3698 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3699                                            asection *sec, int *index)
3700 {
3701   if (sec == &_bfd_elf_large_com_section)
3702     {
3703       *index = SHN_X86_64_LCOMMON;
3704       return TRUE;
3705     }
3706   return FALSE;
3707 }
3708
3709 /* Process a symbol.  */
3710
3711 static void
3712 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3713                                 asymbol *asym)
3714 {
3715   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3716
3717   switch (elfsym->internal_elf_sym.st_shndx)
3718     {
3719     case SHN_X86_64_LCOMMON:
3720       asym->section = &_bfd_elf_large_com_section;
3721       asym->value = elfsym->internal_elf_sym.st_size;
3722       /* Common symbol doesn't set BSF_GLOBAL.  */
3723       asym->flags &= ~BSF_GLOBAL;
3724       break;
3725     }
3726 }
3727
3728 static bfd_boolean
3729 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3730 {
3731   return (sym->st_shndx == SHN_COMMON
3732           || sym->st_shndx == SHN_X86_64_LCOMMON);
3733 }
3734
3735 static unsigned int
3736 elf64_x86_64_common_section_index (asection *sec)
3737 {
3738   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3739     return SHN_COMMON;
3740   else
3741     return SHN_X86_64_LCOMMON;
3742 }
3743
3744 static asection *
3745 elf64_x86_64_common_section (asection *sec)
3746 {
3747   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3748     return bfd_com_section_ptr;
3749   else
3750     return &_bfd_elf_large_com_section;
3751 }
3752
3753 static bfd_boolean
3754 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3755                            struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3756                            struct elf_link_hash_entry *h,
3757                            Elf_Internal_Sym *sym,
3758                            asection **psec,
3759                            bfd_vma *pvalue ATTRIBUTE_UNUSED,
3760                            unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3761                            bfd_boolean *skip ATTRIBUTE_UNUSED,
3762                            bfd_boolean *override ATTRIBUTE_UNUSED,
3763                            bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3764                            bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3765                            bfd_boolean *newdef ATTRIBUTE_UNUSED,
3766                            bfd_boolean *newdyn,
3767                            bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3768                            bfd_boolean *newweak ATTRIBUTE_UNUSED,
3769                            bfd *abfd ATTRIBUTE_UNUSED,
3770                            asection **sec,
3771                            bfd_boolean *olddef ATTRIBUTE_UNUSED,
3772                            bfd_boolean *olddyn,
3773                            bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3774                            bfd_boolean *oldweak ATTRIBUTE_UNUSED,
3775                            bfd *oldbfd,
3776                            asection **oldsec)
3777 {
3778   /* A normal common symbol and a large common symbol result in a
3779      normal common symbol.  We turn the large common symbol into a
3780      normal one.  */
3781   if (!*olddyn
3782       && h->root.type == bfd_link_hash_common
3783       && !*newdyn
3784       && bfd_is_com_section (*sec)
3785       && *oldsec != *sec)
3786     {
3787       if (sym->st_shndx == SHN_COMMON
3788           && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3789         {
3790           h->root.u.c.p->section
3791             = bfd_make_section_old_way (oldbfd, "COMMON");
3792           h->root.u.c.p->section->flags = SEC_ALLOC;
3793         }
3794       else if (sym->st_shndx == SHN_X86_64_LCOMMON
3795                && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
3796         *psec = *sec = bfd_com_section_ptr;
3797     }
3798
3799   return TRUE;
3800 }
3801
3802 static int
3803 elf64_x86_64_additional_program_headers (bfd *abfd,
3804                                          struct bfd_link_info *info ATTRIBUTE_UNUSED)
3805 {
3806   asection *s;
3807   int count = 0;
3808
3809   /* Check to see if we need a large readonly segment.  */
3810   s = bfd_get_section_by_name (abfd, ".lrodata");
3811   if (s && (s->flags & SEC_LOAD))
3812     count++;
3813
3814   /* Check to see if we need a large data segment.  Since .lbss sections
3815      is placed right after the .bss section, there should be no need for
3816      a large data segment just because of .lbss.  */
3817   s = bfd_get_section_by_name (abfd, ".ldata");
3818   if (s && (s->flags & SEC_LOAD))
3819     count++;
3820
3821   return count;
3822 }
3823
3824 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
3825
3826 static bfd_boolean
3827 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
3828 {
3829   if (h->plt.offset != (bfd_vma) -1
3830       && !h->def_regular
3831       && !h->pointer_equality_needed)
3832     return FALSE;
3833
3834   return _bfd_elf_hash_symbol (h);
3835 }
3836
3837 static const struct bfd_elf_special_section
3838   elf64_x86_64_special_sections[]=
3839 {
3840   { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3841   { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3842   { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3843   { STRING_COMMA_LEN (".lbss"),            -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3844   { STRING_COMMA_LEN (".ldata"),           -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3845   { STRING_COMMA_LEN (".lrodata"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3846   { NULL,                       0,          0, 0,            0 }
3847 };
3848
3849 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_vec
3850 #define TARGET_LITTLE_NAME                  "elf64-x86-64"
3851 #define ELF_ARCH                            bfd_arch_i386
3852 #define ELF_MACHINE_CODE                    EM_X86_64
3853 #define ELF_MAXPAGESIZE                     0x200000
3854 #define ELF_MINPAGESIZE                     0x1000
3855 #define ELF_COMMONPAGESIZE                  0x1000
3856
3857 #define elf_backend_can_gc_sections         1
3858 #define elf_backend_can_refcount            1
3859 #define elf_backend_want_got_plt            1
3860 #define elf_backend_plt_readonly            1
3861 #define elf_backend_want_plt_sym            0
3862 #define elf_backend_got_header_size         (GOT_ENTRY_SIZE*3)
3863 #define elf_backend_rela_normal             1
3864
3865 #define elf_info_to_howto                   elf64_x86_64_info_to_howto
3866
3867 #define bfd_elf64_bfd_link_hash_table_create \
3868   elf64_x86_64_link_hash_table_create
3869 #define bfd_elf64_bfd_reloc_type_lookup     elf64_x86_64_reloc_type_lookup
3870 #define bfd_elf64_bfd_reloc_name_lookup \
3871   elf64_x86_64_reloc_name_lookup
3872
3873 #define elf_backend_adjust_dynamic_symbol   elf64_x86_64_adjust_dynamic_symbol
3874 #define elf_backend_relocs_compatible       _bfd_elf_relocs_compatible
3875 #define elf_backend_check_relocs            elf64_x86_64_check_relocs
3876 #define elf_backend_copy_indirect_symbol    elf64_x86_64_copy_indirect_symbol
3877 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3878 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3879 #define elf_backend_finish_dynamic_symbol   elf64_x86_64_finish_dynamic_symbol
3880 #define elf_backend_gc_mark_hook            elf64_x86_64_gc_mark_hook
3881 #define elf_backend_gc_sweep_hook           elf64_x86_64_gc_sweep_hook
3882 #define elf_backend_grok_prstatus           elf64_x86_64_grok_prstatus
3883 #define elf_backend_grok_psinfo             elf64_x86_64_grok_psinfo
3884 #define elf_backend_reloc_type_class        elf64_x86_64_reloc_type_class
3885 #define elf_backend_relocate_section        elf64_x86_64_relocate_section
3886 #define elf_backend_size_dynamic_sections   elf64_x86_64_size_dynamic_sections
3887 #define elf_backend_always_size_sections    elf64_x86_64_always_size_sections
3888 #define elf_backend_init_index_section      _bfd_elf_init_1_index_section
3889 #define elf_backend_plt_sym_val             elf64_x86_64_plt_sym_val
3890 #define elf_backend_object_p                elf64_x86_64_elf_object_p
3891 #define bfd_elf64_mkobject                  elf64_x86_64_mkobject
3892
3893 #define elf_backend_section_from_shdr \
3894         elf64_x86_64_section_from_shdr
3895
3896 #define elf_backend_section_from_bfd_section \
3897   elf64_x86_64_elf_section_from_bfd_section
3898 #define elf_backend_add_symbol_hook \
3899   elf64_x86_64_add_symbol_hook
3900 #define elf_backend_symbol_processing \
3901   elf64_x86_64_symbol_processing
3902 #define elf_backend_common_section_index \
3903   elf64_x86_64_common_section_index
3904 #define elf_backend_common_section \
3905   elf64_x86_64_common_section
3906 #define elf_backend_common_definition \
3907   elf64_x86_64_common_definition
3908 #define elf_backend_merge_symbol \
3909   elf64_x86_64_merge_symbol
3910 #define elf_backend_special_sections \
3911   elf64_x86_64_special_sections
3912 #define elf_backend_additional_program_headers \
3913   elf64_x86_64_additional_program_headers
3914 #define elf_backend_hash_symbol \
3915   elf64_x86_64_hash_symbol
3916
3917 #include "elf64-target.h"
3918
3919 /* FreeBSD support.  */
3920
3921 #undef  TARGET_LITTLE_SYM
3922 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_freebsd_vec
3923 #undef  TARGET_LITTLE_NAME
3924 #define TARGET_LITTLE_NAME                  "elf64-x86-64-freebsd"
3925
3926 #undef  ELF_OSABI
3927 #define ELF_OSABI                           ELFOSABI_FREEBSD
3928
3929 #undef  elf_backend_post_process_headers
3930 #define elf_backend_post_process_headers  _bfd_elf_set_osabi
3931
3932 #undef  elf64_bed
3933 #define elf64_bed elf64_x86_64_fbsd_bed
3934
3935 #include "elf64-target.h"