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