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