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