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