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