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