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