2011-03-03 Michael Snyder <msnyder@vmware.com>
[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               goto do_relocation;
3016             }
3017         }
3018
3019       /* When generating a shared object, the relocations handled here are
3020          copied into the output file to be resolved at run time.  */
3021       switch (r_type)
3022         {
3023         case R_X86_64_GOT32:
3024         case R_X86_64_GOT64:
3025           /* Relocation is to the entry for this symbol in the global
3026              offset table.  */
3027         case R_X86_64_GOTPCREL:
3028         case R_X86_64_GOTPCREL64:
3029           /* Use global offset table entry as symbol value.  */
3030         case R_X86_64_GOTPLT64:
3031           /* This is the same as GOT64 for relocation purposes, but
3032              indicates the existence of a PLT entry.  The difficulty is,
3033              that we must calculate the GOT slot offset from the PLT
3034              offset, if this symbol got a PLT entry (it was global).
3035              Additionally if it's computed from the PLT entry, then that
3036              GOT offset is relative to .got.plt, not to .got.  */
3037           base_got = htab->elf.sgot;
3038
3039           if (htab->elf.sgot == NULL)
3040             abort ();
3041
3042           if (h != NULL)
3043             {
3044               bfd_boolean dyn;
3045
3046               off = h->got.offset;
3047               if (h->needs_plt
3048                   && h->plt.offset != (bfd_vma)-1
3049                   && off == (bfd_vma)-1)
3050                 {
3051                   /* We can't use h->got.offset here to save
3052                      state, or even just remember the offset, as
3053                      finish_dynamic_symbol would use that as offset into
3054                      .got.  */
3055                   bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3056                   off = (plt_index + 3) * GOT_ENTRY_SIZE;
3057                   base_got = htab->elf.sgotplt;
3058                 }
3059
3060               dyn = htab->elf.dynamic_sections_created;
3061
3062               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3063                   || (info->shared
3064                       && SYMBOL_REFERENCES_LOCAL (info, h))
3065                   || (ELF_ST_VISIBILITY (h->other)
3066                       && h->root.type == bfd_link_hash_undefweak))
3067                 {
3068                   /* This is actually a static link, or it is a -Bsymbolic
3069                      link and the symbol is defined locally, or the symbol
3070                      was forced to be local because of a version file.  We
3071                      must initialize this entry in the global offset table.
3072                      Since the offset must always be a multiple of 8, we
3073                      use the least significant bit to record whether we
3074                      have initialized it already.
3075
3076                      When doing a dynamic link, we create a .rela.got
3077                      relocation entry to initialize the value.  This is
3078                      done in the finish_dynamic_symbol routine.  */
3079                   if ((off & 1) != 0)
3080                     off &= ~1;
3081                   else
3082                     {
3083                       bfd_put_64 (output_bfd, relocation,
3084                                   base_got->contents + off);
3085                       /* Note that this is harmless for the GOTPLT64 case,
3086                          as -1 | 1 still is -1.  */
3087                       h->got.offset |= 1;
3088                     }
3089                 }
3090               else
3091                 unresolved_reloc = FALSE;
3092             }
3093           else
3094             {
3095               if (local_got_offsets == NULL)
3096                 abort ();
3097
3098               off = local_got_offsets[r_symndx];
3099
3100               /* The offset must always be a multiple of 8.  We use
3101                  the least significant bit to record whether we have
3102                  already generated the necessary reloc.  */
3103               if ((off & 1) != 0)
3104                 off &= ~1;
3105               else
3106                 {
3107                   bfd_put_64 (output_bfd, relocation,
3108                               base_got->contents + off);
3109
3110                   if (info->shared)
3111                     {
3112                       asection *s;
3113                       Elf_Internal_Rela outrel;
3114
3115                       /* We need to generate a R_X86_64_RELATIVE reloc
3116                          for the dynamic linker.  */
3117                       s = htab->elf.srelgot;
3118                       if (s == NULL)
3119                         abort ();
3120
3121                       outrel.r_offset = (base_got->output_section->vma
3122                                          + base_got->output_offset
3123                                          + off);
3124                       outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3125                       outrel.r_addend = relocation;
3126                       elf_append_rela (output_bfd, s, &outrel);
3127                     }
3128
3129                   local_got_offsets[r_symndx] |= 1;
3130                 }
3131             }
3132
3133           if (off >= (bfd_vma) -2)
3134             abort ();
3135
3136           relocation = base_got->output_section->vma
3137                        + base_got->output_offset + off;
3138           if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
3139             relocation -= htab->elf.sgotplt->output_section->vma
3140                           - htab->elf.sgotplt->output_offset;
3141
3142           break;
3143
3144         case R_X86_64_GOTOFF64:
3145           /* Relocation is relative to the start of the global offset
3146              table.  */
3147
3148           /* Check to make sure it isn't a protected function symbol
3149              for shared library since it may not be local when used
3150              as function address.  */
3151           if (info->shared
3152               && h
3153               && h->def_regular
3154               && h->type == STT_FUNC
3155               && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3156             {
3157               (*_bfd_error_handler)
3158                 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3159                  input_bfd, h->root.root.string);
3160               bfd_set_error (bfd_error_bad_value);
3161               return FALSE;
3162             }
3163
3164           /* Note that sgot is not involved in this
3165              calculation.  We always want the start of .got.plt.  If we
3166              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3167              permitted by the ABI, we might have to change this
3168              calculation.  */
3169           relocation -= htab->elf.sgotplt->output_section->vma
3170                         + htab->elf.sgotplt->output_offset;
3171           break;
3172
3173         case R_X86_64_GOTPC32:
3174         case R_X86_64_GOTPC64:
3175           /* Use global offset table as symbol value.  */
3176           relocation = htab->elf.sgotplt->output_section->vma
3177                        + htab->elf.sgotplt->output_offset;
3178           unresolved_reloc = FALSE;
3179           break;
3180
3181         case R_X86_64_PLTOFF64:
3182           /* Relocation is PLT entry relative to GOT.  For local
3183              symbols it's the symbol itself relative to GOT.  */
3184           if (h != NULL
3185               /* See PLT32 handling.  */
3186               && h->plt.offset != (bfd_vma) -1
3187               && htab->elf.splt != NULL)
3188             {
3189               relocation = (htab->elf.splt->output_section->vma
3190                             + htab->elf.splt->output_offset
3191                             + h->plt.offset);
3192               unresolved_reloc = FALSE;
3193             }
3194
3195           relocation -= htab->elf.sgotplt->output_section->vma
3196                         + htab->elf.sgotplt->output_offset;
3197           break;
3198
3199         case R_X86_64_PLT32:
3200           /* Relocation is to the entry for this symbol in the
3201              procedure linkage table.  */
3202
3203           /* Resolve a PLT32 reloc against a local symbol directly,
3204              without using the procedure linkage table.  */
3205           if (h == NULL)
3206             break;
3207
3208           if (h->plt.offset == (bfd_vma) -1
3209               || htab->elf.splt == NULL)
3210             {
3211               /* We didn't make a PLT entry for this symbol.  This
3212                  happens when statically linking PIC code, or when
3213                  using -Bsymbolic.  */
3214               break;
3215             }
3216
3217           relocation = (htab->elf.splt->output_section->vma
3218                         + htab->elf.splt->output_offset
3219                         + h->plt.offset);
3220           unresolved_reloc = FALSE;
3221           break;
3222
3223         case R_X86_64_PC8:
3224         case R_X86_64_PC16:
3225         case R_X86_64_PC32:
3226           if (info->shared
3227               && ABI_64_P (output_bfd)
3228               && (input_section->flags & SEC_ALLOC) != 0
3229               && (input_section->flags & SEC_READONLY) != 0
3230               && h != NULL)
3231             {
3232               bfd_boolean fail = FALSE;
3233               bfd_boolean branch
3234                 = (r_type == R_X86_64_PC32
3235                    && is_32bit_relative_branch (contents, rel->r_offset));
3236
3237               if (SYMBOL_REFERENCES_LOCAL (info, h))
3238                 {
3239                   /* Symbol is referenced locally.  Make sure it is
3240                      defined locally or for a branch.  */
3241                   fail = !h->def_regular && !branch;
3242                 }
3243               else
3244                 {
3245                   /* Symbol isn't referenced locally.  We only allow
3246                      branch to symbol with non-default visibility. */
3247                   fail = (!branch
3248                           || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3249                 }
3250
3251               if (fail)
3252                 {
3253                   const char *fmt;
3254                   const char *v;
3255                   const char *pic = "";
3256
3257                   switch (ELF_ST_VISIBILITY (h->other))
3258                     {
3259                     case STV_HIDDEN:
3260                       v = _("hidden symbol");
3261                       break;
3262                     case STV_INTERNAL:
3263                       v = _("internal symbol");
3264                       break;
3265                     case STV_PROTECTED:
3266                       v = _("protected symbol");
3267                       break;
3268                     default:
3269                       v = _("symbol");
3270                       pic = _("; recompile with -fPIC");
3271                       break;
3272                     }
3273
3274                   if (h->def_regular)
3275                     fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3276                   else
3277                     fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3278
3279                   (*_bfd_error_handler) (fmt, input_bfd,
3280                                          x86_64_elf_howto_table[r_type].name,
3281                                          v,  h->root.root.string, pic);
3282                   bfd_set_error (bfd_error_bad_value);
3283                   return FALSE;
3284                 }
3285             }
3286           /* Fall through.  */
3287
3288         case R_X86_64_8:
3289         case R_X86_64_16:
3290         case R_X86_64_32:
3291         case R_X86_64_PC64:
3292         case R_X86_64_64:
3293           /* FIXME: The ABI says the linker should make sure the value is
3294              the same when it's zeroextended to 64 bit.  */
3295
3296           if ((input_section->flags & SEC_ALLOC) == 0)
3297             break;
3298
3299           if ((info->shared
3300                && (h == NULL
3301                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3302                    || h->root.type != bfd_link_hash_undefweak)
3303                && (! IS_X86_64_PCREL_TYPE (r_type)
3304                    || ! SYMBOL_CALLS_LOCAL (info, h)))
3305               || (ELIMINATE_COPY_RELOCS
3306                   && !info->shared
3307                   && h != NULL
3308                   && h->dynindx != -1
3309                   && !h->non_got_ref
3310                   && ((h->def_dynamic
3311                        && !h->def_regular)
3312                       || h->root.type == bfd_link_hash_undefweak
3313                       || h->root.type == bfd_link_hash_undefined)))
3314             {
3315               Elf_Internal_Rela outrel;
3316               bfd_boolean skip, relocate;
3317               asection *sreloc;
3318
3319               /* When generating a shared object, these relocations
3320                  are copied into the output file to be resolved at run
3321                  time.  */
3322               skip = FALSE;
3323               relocate = FALSE;
3324
3325               outrel.r_offset =
3326                 _bfd_elf_section_offset (output_bfd, info, input_section,
3327                                          rel->r_offset);
3328               if (outrel.r_offset == (bfd_vma) -1)
3329                 skip = TRUE;
3330               else if (outrel.r_offset == (bfd_vma) -2)
3331                 skip = TRUE, relocate = TRUE;
3332
3333               outrel.r_offset += (input_section->output_section->vma
3334                                   + input_section->output_offset);
3335
3336               if (skip)
3337                 memset (&outrel, 0, sizeof outrel);
3338
3339               /* h->dynindx may be -1 if this symbol was marked to
3340                  become local.  */
3341               else if (h != NULL
3342                        && h->dynindx != -1
3343                        && (IS_X86_64_PCREL_TYPE (r_type)
3344                            || ! info->shared
3345                            || ! SYMBOLIC_BIND (info, h)
3346                            || ! h->def_regular))
3347                 {
3348                   outrel.r_info = htab->r_info (h->dynindx, r_type);
3349                   outrel.r_addend = rel->r_addend;
3350                 }
3351               else
3352                 {
3353                   /* This symbol is local, or marked to become local.  */
3354                   if (r_type == htab->pointer_r_type)
3355                     {
3356                       relocate = TRUE;
3357                       outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3358                       outrel.r_addend = relocation + rel->r_addend;
3359                     }
3360                   else
3361                     {
3362                       long sindx;
3363
3364                       if (bfd_is_abs_section (sec))
3365                         sindx = 0;
3366                       else if (sec == NULL || sec->owner == NULL)
3367                         {
3368                           bfd_set_error (bfd_error_bad_value);
3369                           return FALSE;
3370                         }
3371                       else
3372                         {
3373                           asection *osec;
3374
3375                           /* We are turning this relocation into one
3376                              against a section symbol.  It would be
3377                              proper to subtract the symbol's value,
3378                              osec->vma, from the emitted reloc addend,
3379                              but ld.so expects buggy relocs.  */
3380                           osec = sec->output_section;
3381                           sindx = elf_section_data (osec)->dynindx;
3382                           if (sindx == 0)
3383                             {
3384                               asection *oi = htab->elf.text_index_section;
3385                               sindx = elf_section_data (oi)->dynindx;
3386                             }
3387                           BFD_ASSERT (sindx != 0);
3388                         }
3389
3390                       outrel.r_info = htab->r_info (sindx, r_type);
3391                       outrel.r_addend = relocation + rel->r_addend;
3392                     }
3393                 }
3394
3395               sreloc = elf_section_data (input_section)->sreloc;
3396
3397               BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3398
3399               elf_append_rela (output_bfd, sreloc, &outrel);
3400
3401               /* If this reloc is against an external symbol, we do
3402                  not want to fiddle with the addend.  Otherwise, we
3403                  need to include the symbol value so that it becomes
3404                  an addend for the dynamic reloc.  */
3405               if (! relocate)
3406                 continue;
3407             }
3408
3409           break;
3410
3411         case R_X86_64_TLSGD:
3412         case R_X86_64_GOTPC32_TLSDESC:
3413         case R_X86_64_TLSDESC_CALL:
3414         case R_X86_64_GOTTPOFF:
3415           tls_type = GOT_UNKNOWN;
3416           if (h == NULL && local_got_offsets)
3417             tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3418           else if (h != NULL)
3419             tls_type = elf_x86_64_hash_entry (h)->tls_type;
3420
3421           if (! elf_x86_64_tls_transition (info, input_bfd,
3422                                            input_section, contents,
3423                                            symtab_hdr, sym_hashes,
3424                                            &r_type, tls_type, rel,
3425                                            relend, h, r_symndx))
3426             return FALSE;
3427
3428           if (r_type == R_X86_64_TPOFF32)
3429             {
3430               bfd_vma roff = rel->r_offset;
3431
3432               BFD_ASSERT (! unresolved_reloc);
3433
3434               if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3435                 {
3436                   /* GD->LE transition.
3437                      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3438                      .word 0x6666; rex64; call __tls_get_addr
3439                      Change it into:
3440                      movq %fs:0, %rax
3441                      leaq foo@tpoff(%rax), %rax */
3442                   memcpy (contents + roff - 4,
3443                           "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3444                           16);
3445                   bfd_put_32 (output_bfd,
3446                               elf_x86_64_tpoff (info, relocation),
3447                               contents + roff + 8);
3448                   /* Skip R_X86_64_PC32/R_X86_64_PLT32.  */
3449                   rel++;
3450                   continue;
3451                 }
3452               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3453                 {
3454                   /* GDesc -> LE transition.
3455                      It's originally something like:
3456                      leaq x@tlsdesc(%rip), %rax
3457
3458                      Change it to:
3459                      movl $x@tpoff, %rax.  */
3460
3461                   unsigned int val, type;
3462
3463                   type = bfd_get_8 (input_bfd, contents + roff - 3);
3464                   val = bfd_get_8 (input_bfd, contents + roff - 1);
3465                   bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3466                              contents + roff - 3);
3467                   bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3468                   bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3469                              contents + roff - 1);
3470                   bfd_put_32 (output_bfd,
3471                               elf_x86_64_tpoff (info, relocation),
3472                               contents + roff);
3473                   continue;
3474                 }
3475               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3476                 {
3477                   /* GDesc -> LE transition.
3478                      It's originally:
3479                      call *(%rax)
3480                      Turn it into:
3481                      xchg %ax,%ax.  */
3482                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3483                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3484                   continue;
3485                 }
3486               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3487                 {
3488                   /* IE->LE transition:
3489                      Originally it can be one of:
3490                      movq foo@gottpoff(%rip), %reg
3491                      addq foo@gottpoff(%rip), %reg
3492                      We change it into:
3493                      movq $foo, %reg
3494                      leaq foo(%reg), %reg
3495                      addq $foo, %reg.  */
3496
3497                   unsigned int val, type, reg;
3498
3499                   val = bfd_get_8 (input_bfd, contents + roff - 3);
3500                   type = bfd_get_8 (input_bfd, contents + roff - 2);
3501                   reg = bfd_get_8 (input_bfd, contents + roff - 1);
3502                   reg >>= 3;
3503                   if (type == 0x8b)
3504                     {
3505                       /* movq */
3506                       if (val == 0x4c)
3507                         bfd_put_8 (output_bfd, 0x49,
3508                                    contents + roff - 3);
3509                       else if (!ABI_64_P (output_bfd) && val == 0x44)
3510                         bfd_put_8 (output_bfd, 0x41,
3511                                    contents + roff - 3);
3512                       bfd_put_8 (output_bfd, 0xc7,
3513                                  contents + roff - 2);
3514                       bfd_put_8 (output_bfd, 0xc0 | reg,
3515                                  contents + roff - 1);
3516                     }
3517                   else if (reg == 4)
3518                     {
3519                       /* addq -> addq - addressing with %rsp/%r12 is
3520                          special  */
3521                       if (val == 0x4c)
3522                         bfd_put_8 (output_bfd, 0x49,
3523                                    contents + roff - 3);
3524                       else if (!ABI_64_P (output_bfd) && val == 0x44)
3525                         bfd_put_8 (output_bfd, 0x41,
3526                                    contents + roff - 3);
3527                       bfd_put_8 (output_bfd, 0x81,
3528                                  contents + roff - 2);
3529                       bfd_put_8 (output_bfd, 0xc0 | reg,
3530                                  contents + roff - 1);
3531                     }
3532                   else
3533                     {
3534                       /* addq -> leaq */
3535                       if (val == 0x4c)
3536                         bfd_put_8 (output_bfd, 0x4d,
3537                                    contents + roff - 3);
3538                       else if (!ABI_64_P (output_bfd) && val == 0x44)
3539                         bfd_put_8 (output_bfd, 0x45,
3540                                    contents + roff - 3);
3541                       bfd_put_8 (output_bfd, 0x8d,
3542                                  contents + roff - 2);
3543                       bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3544                                  contents + roff - 1);
3545                     }
3546                   bfd_put_32 (output_bfd,
3547                               elf_x86_64_tpoff (info, relocation),
3548                               contents + roff);
3549                   continue;
3550                 }
3551               else
3552                 BFD_ASSERT (FALSE);
3553             }
3554
3555           if (htab->elf.sgot == NULL)
3556             abort ();
3557
3558           if (h != NULL)
3559             {
3560               off = h->got.offset;
3561               offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
3562             }
3563           else
3564             {
3565               if (local_got_offsets == NULL)
3566                 abort ();
3567
3568               off = local_got_offsets[r_symndx];
3569               offplt = local_tlsdesc_gotents[r_symndx];
3570             }
3571
3572           if ((off & 1) != 0)
3573             off &= ~1;
3574           else
3575             {
3576               Elf_Internal_Rela outrel;
3577               int dr_type, indx;
3578               asection *sreloc;
3579
3580               if (htab->elf.srelgot == NULL)
3581                 abort ();
3582
3583               indx = h && h->dynindx != -1 ? h->dynindx : 0;
3584
3585               if (GOT_TLS_GDESC_P (tls_type))
3586                 {
3587                   outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
3588                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3589                               + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3590                   outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3591                                      + htab->elf.sgotplt->output_offset
3592                                      + offplt
3593                                      + htab->sgotplt_jump_table_size);
3594                   sreloc = htab->elf.srelplt;
3595                   if (indx == 0)
3596                     outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
3597                   else
3598                     outrel.r_addend = 0;
3599                   elf_append_rela (output_bfd, sreloc, &outrel);
3600                 }
3601
3602               sreloc = htab->elf.srelgot;
3603
3604               outrel.r_offset = (htab->elf.sgot->output_section->vma
3605                                  + htab->elf.sgot->output_offset + off);
3606
3607               if (GOT_TLS_GD_P (tls_type))
3608                 dr_type = R_X86_64_DTPMOD64;
3609               else if (GOT_TLS_GDESC_P (tls_type))
3610                 goto dr_done;
3611               else
3612                 dr_type = R_X86_64_TPOFF64;
3613
3614               bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3615               outrel.r_addend = 0;
3616               if ((dr_type == R_X86_64_TPOFF64
3617                    || dr_type == R_X86_64_TLSDESC) && indx == 0)
3618                 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
3619               outrel.r_info = htab->r_info (indx, dr_type);
3620
3621               elf_append_rela (output_bfd, sreloc, &outrel);
3622
3623               if (GOT_TLS_GD_P (tls_type))
3624                 {
3625                   if (indx == 0)
3626                     {
3627                       BFD_ASSERT (! unresolved_reloc);
3628                       bfd_put_64 (output_bfd,
3629                                   relocation - elf_x86_64_dtpoff_base (info),
3630                                   htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3631                     }
3632                   else
3633                     {
3634                       bfd_put_64 (output_bfd, 0,
3635                                   htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3636                       outrel.r_info = htab->r_info (indx,
3637                                                     R_X86_64_DTPOFF64);
3638                       outrel.r_offset += GOT_ENTRY_SIZE;
3639                       elf_append_rela (output_bfd, sreloc,
3640                                                 &outrel);
3641                     }
3642                 }
3643
3644             dr_done:
3645               if (h != NULL)
3646                 h->got.offset |= 1;
3647               else
3648                 local_got_offsets[r_symndx] |= 1;
3649             }
3650
3651           if (off >= (bfd_vma) -2
3652               && ! GOT_TLS_GDESC_P (tls_type))
3653             abort ();
3654           if (r_type == ELF32_R_TYPE (rel->r_info))
3655             {
3656               if (r_type == R_X86_64_GOTPC32_TLSDESC
3657                   || r_type == R_X86_64_TLSDESC_CALL)
3658                 relocation = htab->elf.sgotplt->output_section->vma
3659                   + htab->elf.sgotplt->output_offset
3660                   + offplt + htab->sgotplt_jump_table_size;
3661               else
3662                 relocation = htab->elf.sgot->output_section->vma
3663                   + htab->elf.sgot->output_offset + off;
3664               unresolved_reloc = FALSE;
3665             }
3666           else
3667             {
3668               bfd_vma roff = rel->r_offset;
3669
3670               if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3671                 {
3672                   /* GD->IE transition.
3673                      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3674                      .word 0x6666; rex64; call __tls_get_addr@plt
3675                      Change it into:
3676                      movq %fs:0, %rax
3677                      addq foo@gottpoff(%rip), %rax */
3678                   memcpy (contents + roff - 4,
3679                           "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3680                           16);
3681
3682                   relocation = (htab->elf.sgot->output_section->vma
3683                                 + htab->elf.sgot->output_offset + off
3684                                 - roff
3685                                 - input_section->output_section->vma
3686                                 - input_section->output_offset
3687                                 - 12);
3688                   bfd_put_32 (output_bfd, relocation,
3689                               contents + roff + 8);
3690                   /* Skip R_X86_64_PLT32.  */
3691                   rel++;
3692                   continue;
3693                 }
3694               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3695                 {
3696                   /* GDesc -> IE transition.
3697                      It's originally something like:
3698                      leaq x@tlsdesc(%rip), %rax
3699
3700                      Change it to:
3701                      movq x@gottpoff(%rip), %rax # before xchg %ax,%ax.  */
3702
3703                   /* Now modify the instruction as appropriate. To
3704                      turn a leaq into a movq in the form we use it, it
3705                      suffices to change the second byte from 0x8d to
3706                      0x8b.  */
3707                   bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3708
3709                   bfd_put_32 (output_bfd,
3710                               htab->elf.sgot->output_section->vma
3711                               + htab->elf.sgot->output_offset + off
3712                               - rel->r_offset
3713                               - input_section->output_section->vma
3714                               - input_section->output_offset
3715                               - 4,
3716                               contents + roff);
3717                   continue;
3718                 }
3719               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3720                 {
3721                   /* GDesc -> IE transition.
3722                      It's originally:
3723                      call *(%rax)
3724
3725                      Change it to:
3726                      xchg %ax, %ax.  */
3727
3728                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3729                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3730                   continue;
3731                 }
3732               else
3733                 BFD_ASSERT (FALSE);
3734             }
3735           break;
3736
3737         case R_X86_64_TLSLD:
3738           if (! elf_x86_64_tls_transition (info, input_bfd,
3739                                            input_section, contents,
3740                                            symtab_hdr, sym_hashes,
3741                                            &r_type, GOT_UNKNOWN,
3742                                            rel, relend, h, r_symndx))
3743             return FALSE;
3744
3745           if (r_type != R_X86_64_TLSLD)
3746             {
3747               /* LD->LE transition:
3748                  leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3749                  We change it into:
3750                  .word 0x6666; .byte 0x66; movl %fs:0, %rax.  */
3751
3752               BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3753               memcpy (contents + rel->r_offset - 3,
3754                       "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3755               /* Skip R_X86_64_PC32/R_X86_64_PLT32.  */
3756               rel++;
3757               continue;
3758             }
3759
3760           if (htab->elf.sgot == NULL)
3761             abort ();
3762
3763           off = htab->tls_ld_got.offset;
3764           if (off & 1)
3765             off &= ~1;
3766           else
3767             {
3768               Elf_Internal_Rela outrel;
3769
3770               if (htab->elf.srelgot == NULL)
3771                 abort ();
3772
3773               outrel.r_offset = (htab->elf.sgot->output_section->vma
3774                                  + htab->elf.sgot->output_offset + off);
3775
3776               bfd_put_64 (output_bfd, 0,
3777                           htab->elf.sgot->contents + off);
3778               bfd_put_64 (output_bfd, 0,
3779                           htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3780               outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
3781               outrel.r_addend = 0;
3782               elf_append_rela (output_bfd, htab->elf.srelgot,
3783                                         &outrel);
3784               htab->tls_ld_got.offset |= 1;
3785             }
3786           relocation = htab->elf.sgot->output_section->vma
3787                        + htab->elf.sgot->output_offset + off;
3788           unresolved_reloc = FALSE;
3789           break;
3790
3791         case R_X86_64_DTPOFF32:
3792           if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
3793             relocation -= elf_x86_64_dtpoff_base (info);
3794           else
3795             relocation = elf_x86_64_tpoff (info, relocation);
3796           break;
3797
3798         case R_X86_64_TPOFF32:
3799           BFD_ASSERT (info->executable);
3800           relocation = elf_x86_64_tpoff (info, relocation);
3801           break;
3802
3803         default:
3804           break;
3805         }
3806
3807       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3808          because such sections are not SEC_ALLOC and thus ld.so will
3809          not process them.  */
3810       if (unresolved_reloc
3811           && !((input_section->flags & SEC_DEBUGGING) != 0
3812                && h->def_dynamic))
3813         (*_bfd_error_handler)
3814           (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3815            input_bfd,
3816            input_section,
3817            (long) rel->r_offset,
3818            howto->name,
3819            h->root.root.string);
3820
3821 do_relocation:
3822       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3823                                     contents, rel->r_offset,
3824                                     relocation, rel->r_addend);
3825
3826       if (r != bfd_reloc_ok)
3827         {
3828           const char *name;
3829
3830           if (h != NULL)
3831             name = h->root.root.string;
3832           else
3833             {
3834               name = bfd_elf_string_from_elf_section (input_bfd,
3835                                                       symtab_hdr->sh_link,
3836                                                       sym->st_name);
3837               if (name == NULL)
3838                 return FALSE;
3839               if (*name == '\0')
3840                 name = bfd_section_name (input_bfd, sec);
3841             }
3842
3843           if (r == bfd_reloc_overflow)
3844             {
3845               if (! ((*info->callbacks->reloc_overflow)
3846                      (info, (h ? &h->root : NULL), name, howto->name,
3847                       (bfd_vma) 0, input_bfd, input_section,
3848                       rel->r_offset)))
3849                 return FALSE;
3850             }
3851           else
3852             {
3853               (*_bfd_error_handler)
3854                 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3855                  input_bfd, input_section,
3856                  (long) rel->r_offset, name, (int) r);
3857               return FALSE;
3858             }
3859         }
3860     }
3861
3862   return TRUE;
3863 }
3864
3865 /* Finish up dynamic symbol handling.  We set the contents of various
3866    dynamic sections here.  */
3867
3868 static bfd_boolean
3869 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3870                                   struct bfd_link_info *info,
3871                                   struct elf_link_hash_entry *h,
3872                                   Elf_Internal_Sym *sym)
3873 {
3874   struct elf_x86_64_link_hash_table *htab;
3875
3876   htab = elf_x86_64_hash_table (info);
3877   if (htab == NULL)
3878     return FALSE;
3879
3880   if (h->plt.offset != (bfd_vma) -1)
3881     {
3882       bfd_vma plt_index;
3883       bfd_vma got_offset;
3884       Elf_Internal_Rela rela;
3885       bfd_byte *loc;
3886       asection *plt, *gotplt, *relplt;
3887       const struct elf_backend_data *bed;
3888
3889       /* When building a static executable, use .iplt, .igot.plt and
3890          .rela.iplt sections for STT_GNU_IFUNC symbols.  */
3891       if (htab->elf.splt != NULL)
3892         {
3893           plt = htab->elf.splt;
3894           gotplt = htab->elf.sgotplt;
3895           relplt = htab->elf.srelplt;
3896         }
3897       else
3898         {
3899           plt = htab->elf.iplt;
3900           gotplt = htab->elf.igotplt;
3901           relplt = htab->elf.irelplt;
3902         }
3903
3904       /* This symbol has an entry in the procedure linkage table.  Set
3905          it up.  */
3906       if ((h->dynindx == -1
3907            && !((h->forced_local || info->executable)
3908                 && h->def_regular
3909                 && h->type == STT_GNU_IFUNC))
3910           || plt == NULL
3911           || gotplt == NULL
3912           || relplt == NULL)
3913         abort ();
3914
3915       /* Get the index in the procedure linkage table which
3916          corresponds to this symbol.  This is the index of this symbol
3917          in all the symbols for which we are making plt entries.  The
3918          first entry in the procedure linkage table is reserved.
3919
3920          Get the offset into the .got table of the entry that
3921          corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
3922          bytes. The first three are reserved for the dynamic linker.
3923
3924          For static executables, we don't reserve anything.  */
3925
3926       if (plt == htab->elf.splt)
3927         {
3928           plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3929           got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3930         }
3931       else
3932         {
3933           plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3934           got_offset = plt_index * GOT_ENTRY_SIZE;
3935         }
3936
3937       /* Fill in the entry in the procedure linkage table.  */
3938       memcpy (plt->contents + h->plt.offset, elf_x86_64_plt_entry,
3939               PLT_ENTRY_SIZE);
3940
3941       /* Insert the relocation positions of the plt section.  The magic
3942          numbers at the end of the statements are the positions of the
3943          relocations in the plt section.  */
3944       /* Put offset for jmp *name@GOTPCREL(%rip), since the
3945          instruction uses 6 bytes, subtract this value.  */
3946       bfd_put_32 (output_bfd,
3947                       (gotplt->output_section->vma
3948                        + gotplt->output_offset
3949                        + got_offset
3950                        - plt->output_section->vma
3951                        - plt->output_offset
3952                        - h->plt.offset
3953                        - 6),
3954                   plt->contents + h->plt.offset + 2);
3955
3956       /* Don't fill PLT entry for static executables.  */
3957       if (plt == htab->elf.splt)
3958         {
3959           /* Put relocation index.  */
3960           bfd_put_32 (output_bfd, plt_index,
3961                       plt->contents + h->plt.offset + 7);
3962           /* Put offset for jmp .PLT0.  */
3963           bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3964                       plt->contents + h->plt.offset + 12);
3965         }
3966
3967       /* Fill in the entry in the global offset table, initially this
3968          points to the pushq instruction in the PLT which is at offset 6.  */
3969       bfd_put_64 (output_bfd, (plt->output_section->vma
3970                                + plt->output_offset
3971                                + h->plt.offset + 6),
3972                   gotplt->contents + got_offset);
3973
3974       /* Fill in the entry in the .rela.plt section.  */
3975       rela.r_offset = (gotplt->output_section->vma
3976                        + gotplt->output_offset
3977                        + got_offset);
3978       if (h->dynindx == -1
3979           || ((info->executable
3980                || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3981               && h->def_regular
3982               && h->type == STT_GNU_IFUNC))
3983         {
3984           /* If an STT_GNU_IFUNC symbol is locally defined, generate
3985              R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT.  */
3986           rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3987           rela.r_addend = (h->root.u.def.value
3988                            + h->root.u.def.section->output_section->vma
3989                            + h->root.u.def.section->output_offset);
3990         }
3991       else
3992         {
3993           rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
3994           rela.r_addend = 0;
3995         }
3996
3997       bed = get_elf_backend_data (output_bfd);
3998       loc = relplt->contents + plt_index * bed->s->sizeof_rela;
3999       bed->s->swap_reloca_out (output_bfd, &rela, loc);
4000
4001       if (!h->def_regular)
4002         {
4003           /* Mark the symbol as undefined, rather than as defined in
4004              the .plt section.  Leave the value if there were any
4005              relocations where pointer equality matters (this is a clue
4006              for the dynamic linker, to make function pointer
4007              comparisons work between an application and shared
4008              library), otherwise set it to zero.  If a function is only
4009              called from a binary, there is no need to slow down
4010              shared libraries because of that.  */
4011           sym->st_shndx = SHN_UNDEF;
4012           if (!h->pointer_equality_needed)
4013             sym->st_value = 0;
4014         }
4015     }
4016
4017   if (h->got.offset != (bfd_vma) -1
4018       && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
4019       && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
4020     {
4021       Elf_Internal_Rela rela;
4022
4023       /* This symbol has an entry in the global offset table.  Set it
4024          up.  */
4025       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4026         abort ();
4027
4028       rela.r_offset = (htab->elf.sgot->output_section->vma
4029                        + htab->elf.sgot->output_offset
4030                        + (h->got.offset &~ (bfd_vma) 1));
4031
4032       /* If this is a static link, or it is a -Bsymbolic link and the
4033          symbol is defined locally or was forced to be local because
4034          of a version file, we just want to emit a RELATIVE reloc.
4035          The entry in the global offset table will already have been
4036          initialized in the relocate_section function.  */
4037       if (h->def_regular
4038           && h->type == STT_GNU_IFUNC)
4039         {
4040           if (info->shared)
4041             {
4042               /* Generate R_X86_64_GLOB_DAT.  */
4043               goto do_glob_dat;
4044             }
4045           else
4046             {
4047               asection *plt;
4048
4049               if (!h->pointer_equality_needed)
4050                 abort ();
4051
4052               /* For non-shared object, we can't use .got.plt, which
4053                  contains the real function addres if we need pointer
4054                  equality.  We load the GOT entry with the PLT entry.  */
4055               plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4056               bfd_put_64 (output_bfd, (plt->output_section->vma
4057                                        + plt->output_offset
4058                                        + h->plt.offset),
4059                           htab->elf.sgot->contents + h->got.offset);
4060               return TRUE;
4061             }
4062         }
4063       else if (info->shared
4064                && SYMBOL_REFERENCES_LOCAL (info, h))
4065         {
4066           if (!h->def_regular)
4067             return FALSE;
4068           BFD_ASSERT((h->got.offset & 1) != 0);
4069           rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4070           rela.r_addend = (h->root.u.def.value
4071                            + h->root.u.def.section->output_section->vma
4072                            + h->root.u.def.section->output_offset);
4073         }
4074       else
4075         {
4076           BFD_ASSERT((h->got.offset & 1) == 0);
4077 do_glob_dat:
4078           bfd_put_64 (output_bfd, (bfd_vma) 0,
4079                       htab->elf.sgot->contents + h->got.offset);
4080           rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
4081           rela.r_addend = 0;
4082         }
4083
4084       elf_append_rela (output_bfd, htab->elf.srelgot, &rela);
4085     }
4086
4087   if (h->needs_copy)
4088     {
4089       Elf_Internal_Rela rela;
4090
4091       /* This symbol needs a copy reloc.  Set it up.  */
4092
4093       if (h->dynindx == -1
4094           || (h->root.type != bfd_link_hash_defined
4095               && h->root.type != bfd_link_hash_defweak)
4096           || htab->srelbss == NULL)
4097         abort ();
4098
4099       rela.r_offset = (h->root.u.def.value
4100                        + h->root.u.def.section->output_section->vma
4101                        + h->root.u.def.section->output_offset);
4102       rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
4103       rela.r_addend = 0;
4104       elf_append_rela (output_bfd, htab->srelbss, &rela);
4105     }
4106
4107   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
4108      be NULL for local symbols.  */
4109   if (sym != NULL
4110       && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4111           || h == htab->elf.hgot))
4112     sym->st_shndx = SHN_ABS;
4113
4114   return TRUE;
4115 }
4116
4117 /* Finish up local dynamic symbol handling.  We set the contents of
4118    various dynamic sections here.  */
4119
4120 static bfd_boolean
4121 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4122 {
4123   struct elf_link_hash_entry *h
4124     = (struct elf_link_hash_entry *) *slot;
4125   struct bfd_link_info *info
4126     = (struct bfd_link_info *) inf; 
4127
4128   return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4129                                              info, h, NULL);
4130 }
4131
4132 /* Used to decide how to sort relocs in an optimal manner for the
4133    dynamic linker, before writing them out.  */
4134
4135 static enum elf_reloc_type_class
4136 elf_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
4137 {
4138   switch ((int) ELF32_R_TYPE (rela->r_info))
4139     {
4140     case R_X86_64_RELATIVE:
4141       return reloc_class_relative;
4142     case R_X86_64_JUMP_SLOT:
4143       return reloc_class_plt;
4144     case R_X86_64_COPY:
4145       return reloc_class_copy;
4146     default:
4147       return reloc_class_normal;
4148     }
4149 }
4150
4151 /* Finish up the dynamic sections.  */
4152
4153 static bfd_boolean
4154 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4155                                     struct bfd_link_info *info)
4156 {
4157   struct elf_x86_64_link_hash_table *htab;
4158   bfd *dynobj;
4159   asection *sdyn;
4160
4161   htab = elf_x86_64_hash_table (info);
4162   if (htab == NULL)
4163     return FALSE;
4164
4165   dynobj = htab->elf.dynobj;
4166   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4167
4168   if (htab->elf.dynamic_sections_created)
4169     {
4170       bfd_byte *dyncon, *dynconend;
4171       const struct elf_backend_data *bed;
4172       bfd_size_type sizeof_dyn;
4173
4174       if (sdyn == NULL || htab->elf.sgot == NULL)
4175         abort ();
4176
4177       bed = get_elf_backend_data (dynobj);
4178       sizeof_dyn = bed->s->sizeof_dyn;
4179       dyncon = sdyn->contents;
4180       dynconend = sdyn->contents + sdyn->size;
4181       for (; dyncon < dynconend; dyncon += sizeof_dyn)
4182         {
4183           Elf_Internal_Dyn dyn;
4184           asection *s;
4185
4186           (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
4187
4188           switch (dyn.d_tag)
4189             {
4190             default:
4191               continue;
4192
4193             case DT_PLTGOT:
4194               s = htab->elf.sgotplt;
4195               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4196               break;
4197
4198             case DT_JMPREL:
4199               dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4200               break;
4201
4202             case DT_PLTRELSZ:
4203               s = htab->elf.srelplt->output_section;
4204               dyn.d_un.d_val = s->size;
4205               break;
4206
4207             case DT_RELASZ:
4208               /* The procedure linkage table relocs (DT_JMPREL) should
4209                  not be included in the overall relocs (DT_RELA).
4210                  Therefore, we override the DT_RELASZ entry here to
4211                  make it not include the JMPREL relocs.  Since the
4212                  linker script arranges for .rela.plt to follow all
4213                  other relocation sections, we don't have to worry
4214                  about changing the DT_RELA entry.  */
4215               if (htab->elf.srelplt != NULL)
4216                 {
4217                   s = htab->elf.srelplt->output_section;
4218                   dyn.d_un.d_val -= s->size;
4219                 }
4220               break;
4221
4222             case DT_TLSDESC_PLT:
4223               s = htab->elf.splt;
4224               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4225                 + htab->tlsdesc_plt;
4226               break;
4227
4228             case DT_TLSDESC_GOT:
4229               s = htab->elf.sgot;
4230               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4231                 + htab->tlsdesc_got;
4232               break;
4233             }
4234
4235           (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
4236         }
4237
4238       /* Fill in the special first entry in the procedure linkage table.  */
4239       if (htab->elf.splt && htab->elf.splt->size > 0)
4240         {
4241           /* Fill in the first entry in the procedure linkage table.  */
4242           memcpy (htab->elf.splt->contents, elf_x86_64_plt0_entry,
4243                   PLT_ENTRY_SIZE);
4244           /* Add offset for pushq GOT+8(%rip), since the instruction
4245              uses 6 bytes subtract this value.  */
4246           bfd_put_32 (output_bfd,
4247                       (htab->elf.sgotplt->output_section->vma
4248                        + htab->elf.sgotplt->output_offset
4249                        + 8
4250                        - htab->elf.splt->output_section->vma
4251                        - htab->elf.splt->output_offset
4252                        - 6),
4253                       htab->elf.splt->contents + 2);
4254           /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4255              the end of the instruction.  */
4256           bfd_put_32 (output_bfd,
4257                       (htab->elf.sgotplt->output_section->vma
4258                        + htab->elf.sgotplt->output_offset
4259                        + 16
4260                        - htab->elf.splt->output_section->vma
4261                        - htab->elf.splt->output_offset
4262                        - 12),
4263                       htab->elf.splt->contents + 8);
4264
4265           elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4266             PLT_ENTRY_SIZE;
4267
4268           if (htab->tlsdesc_plt)
4269             {
4270               bfd_put_64 (output_bfd, (bfd_vma) 0,
4271                           htab->elf.sgot->contents + htab->tlsdesc_got);
4272
4273               memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4274                       elf_x86_64_plt0_entry,
4275                       PLT_ENTRY_SIZE);
4276
4277               /* Add offset for pushq GOT+8(%rip), since the
4278                  instruction uses 6 bytes subtract this value.  */
4279               bfd_put_32 (output_bfd,
4280                           (htab->elf.sgotplt->output_section->vma
4281                            + htab->elf.sgotplt->output_offset
4282                            + 8
4283                            - htab->elf.splt->output_section->vma
4284                            - htab->elf.splt->output_offset
4285                            - htab->tlsdesc_plt
4286                            - 6),
4287                           htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4288               /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4289                  htab->tlsdesc_got. The 12 is the offset to the end of
4290                  the instruction.  */
4291               bfd_put_32 (output_bfd,
4292                           (htab->elf.sgot->output_section->vma
4293                            + htab->elf.sgot->output_offset
4294                            + htab->tlsdesc_got
4295                            - htab->elf.splt->output_section->vma
4296                            - htab->elf.splt->output_offset
4297                            - htab->tlsdesc_plt
4298                            - 12),
4299                           htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4300             }
4301         }
4302     }
4303
4304   if (htab->elf.sgotplt)
4305     {
4306       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4307         {
4308           (*_bfd_error_handler)
4309             (_("discarded output section: `%A'"), htab->elf.sgotplt);
4310           return FALSE;
4311         }
4312
4313       /* Fill in the first three entries in the global offset table.  */
4314       if (htab->elf.sgotplt->size > 0)
4315         {
4316           /* Set the first entry in the global offset table to the address of
4317              the dynamic section.  */
4318           if (sdyn == NULL)
4319             bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4320           else
4321             bfd_put_64 (output_bfd,
4322                         sdyn->output_section->vma + sdyn->output_offset,
4323                         htab->elf.sgotplt->contents);
4324           /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
4325           bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4326           bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4327         }
4328
4329       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4330         GOT_ENTRY_SIZE;
4331     }
4332
4333   if (htab->elf.sgot && htab->elf.sgot->size > 0)
4334     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4335       = GOT_ENTRY_SIZE;
4336
4337   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
4338   htab_traverse (htab->loc_hash_table,
4339                  elf_x86_64_finish_local_dynamic_symbol,
4340                  info);
4341
4342   return TRUE;
4343 }
4344
4345 /* Return address for Ith PLT stub in section PLT, for relocation REL
4346    or (bfd_vma) -1 if it should not be included.  */
4347
4348 static bfd_vma
4349 elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4350                         const arelent *rel ATTRIBUTE_UNUSED)
4351 {
4352   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4353 }
4354
4355 /* Handle an x86-64 specific section when reading an object file.  This
4356    is called when elfcode.h finds a section with an unknown type.  */
4357
4358 static bfd_boolean
4359 elf_x86_64_section_from_shdr (bfd *abfd,
4360                                 Elf_Internal_Shdr *hdr,
4361                                 const char *name,
4362                                 int shindex)
4363 {
4364   if (hdr->sh_type != SHT_X86_64_UNWIND)
4365     return FALSE;
4366
4367   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4368     return FALSE;
4369
4370   return TRUE;
4371 }
4372
4373 /* Hook called by the linker routine which adds symbols from an object
4374    file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4375    of .bss.  */
4376
4377 static bfd_boolean
4378 elf_x86_64_add_symbol_hook (bfd *abfd,
4379                             struct bfd_link_info *info,
4380                             Elf_Internal_Sym *sym,
4381                             const char **namep ATTRIBUTE_UNUSED,
4382                             flagword *flagsp ATTRIBUTE_UNUSED,
4383                             asection **secp,
4384                             bfd_vma *valp)
4385 {
4386   asection *lcomm;
4387
4388   switch (sym->st_shndx)
4389     {
4390     case SHN_X86_64_LCOMMON:
4391       lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4392       if (lcomm == NULL)
4393         {
4394           lcomm = bfd_make_section_with_flags (abfd,
4395                                                "LARGE_COMMON",
4396                                                (SEC_ALLOC
4397                                                 | SEC_IS_COMMON
4398                                                 | SEC_LINKER_CREATED));
4399           if (lcomm == NULL)
4400             return FALSE;
4401           elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4402         }
4403       *secp = lcomm;
4404       *valp = sym->st_size;
4405       return TRUE;
4406     }
4407
4408   if ((abfd->flags & DYNAMIC) == 0
4409       && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4410     elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4411
4412   return TRUE;
4413 }
4414
4415
4416 /* Given a BFD section, try to locate the corresponding ELF section
4417    index.  */
4418
4419 static bfd_boolean
4420 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4421                                          asection *sec, int *index_return)
4422 {
4423   if (sec == &_bfd_elf_large_com_section)
4424     {
4425       *index_return = SHN_X86_64_LCOMMON;
4426       return TRUE;
4427     }
4428   return FALSE;
4429 }
4430
4431 /* Process a symbol.  */
4432
4433 static void
4434 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4435                               asymbol *asym)
4436 {
4437   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4438
4439   switch (elfsym->internal_elf_sym.st_shndx)
4440     {
4441     case SHN_X86_64_LCOMMON:
4442       asym->section = &_bfd_elf_large_com_section;
4443       asym->value = elfsym->internal_elf_sym.st_size;
4444       /* Common symbol doesn't set BSF_GLOBAL.  */
4445       asym->flags &= ~BSF_GLOBAL;
4446       break;
4447     }
4448 }
4449
4450 static bfd_boolean
4451 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
4452 {
4453   return (sym->st_shndx == SHN_COMMON
4454           || sym->st_shndx == SHN_X86_64_LCOMMON);
4455 }
4456
4457 static unsigned int
4458 elf_x86_64_common_section_index (asection *sec)
4459 {
4460   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4461     return SHN_COMMON;
4462   else
4463     return SHN_X86_64_LCOMMON;
4464 }
4465
4466 static asection *
4467 elf_x86_64_common_section (asection *sec)
4468 {
4469   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4470     return bfd_com_section_ptr;
4471   else
4472     return &_bfd_elf_large_com_section;
4473 }
4474
4475 static bfd_boolean
4476 elf_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4477                          struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4478                          struct elf_link_hash_entry *h,
4479                          Elf_Internal_Sym *sym,
4480                          asection **psec,
4481                          bfd_vma *pvalue ATTRIBUTE_UNUSED,
4482                          unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4483                          bfd_boolean *skip ATTRIBUTE_UNUSED,
4484                          bfd_boolean *override ATTRIBUTE_UNUSED,
4485                          bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4486                          bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4487                          bfd_boolean *newdef ATTRIBUTE_UNUSED,
4488                          bfd_boolean *newdyn,
4489                          bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4490                          bfd_boolean *newweak ATTRIBUTE_UNUSED,
4491                          bfd *abfd ATTRIBUTE_UNUSED,
4492                          asection **sec,
4493                          bfd_boolean *olddef ATTRIBUTE_UNUSED,
4494                          bfd_boolean *olddyn,
4495                          bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4496                          bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4497                          bfd *oldbfd,
4498                          asection **oldsec)
4499 {
4500   /* A normal common symbol and a large common symbol result in a
4501      normal common symbol.  We turn the large common symbol into a
4502      normal one.  */
4503   if (!*olddyn
4504       && h->root.type == bfd_link_hash_common
4505       && !*newdyn
4506       && bfd_is_com_section (*sec)
4507       && *oldsec != *sec)
4508     {
4509       if (sym->st_shndx == SHN_COMMON
4510           && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4511         {
4512           h->root.u.c.p->section
4513             = bfd_make_section_old_way (oldbfd, "COMMON");
4514           h->root.u.c.p->section->flags = SEC_ALLOC;
4515         }
4516       else if (sym->st_shndx == SHN_X86_64_LCOMMON
4517                && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4518         *psec = *sec = bfd_com_section_ptr;
4519     }
4520
4521   return TRUE;
4522 }
4523
4524 static int
4525 elf_x86_64_additional_program_headers (bfd *abfd,
4526                                        struct bfd_link_info *info ATTRIBUTE_UNUSED)
4527 {
4528   asection *s;
4529   int count = 0;
4530
4531   /* Check to see if we need a large readonly segment.  */
4532   s = bfd_get_section_by_name (abfd, ".lrodata");
4533   if (s && (s->flags & SEC_LOAD))
4534     count++;
4535
4536   /* Check to see if we need a large data segment.  Since .lbss sections
4537      is placed right after the .bss section, there should be no need for
4538      a large data segment just because of .lbss.  */
4539   s = bfd_get_section_by_name (abfd, ".ldata");
4540   if (s && (s->flags & SEC_LOAD))
4541     count++;
4542
4543   return count;
4544 }
4545
4546 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
4547
4548 static bfd_boolean
4549 elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4550 {
4551   if (h->plt.offset != (bfd_vma) -1
4552       && !h->def_regular
4553       && !h->pointer_equality_needed)
4554     return FALSE;
4555
4556   return _bfd_elf_hash_symbol (h);
4557 }
4558
4559 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
4560
4561 static bfd_boolean
4562 elf_x86_64_relocs_compatible (const bfd_target *input,
4563                               const bfd_target *output)
4564 {
4565   return ((xvec_get_elf_backend_data (input)->s->elfclass
4566            == xvec_get_elf_backend_data (output)->s->elfclass)
4567           && _bfd_elf_relocs_compatible (input, output));
4568 }
4569
4570 static const struct bfd_elf_special_section
4571   elf_x86_64_special_sections[]=
4572 {
4573   { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4574   { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4575   { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4576   { STRING_COMMA_LEN (".lbss"),            -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4577   { STRING_COMMA_LEN (".ldata"),           -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4578   { STRING_COMMA_LEN (".lrodata"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4579   { NULL,                       0,          0, 0,            0 }
4580 };
4581
4582 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_vec
4583 #define TARGET_LITTLE_NAME                  "elf64-x86-64"
4584 #define ELF_ARCH                            bfd_arch_i386
4585 #define ELF_TARGET_ID                       X86_64_ELF_DATA
4586 #define ELF_MACHINE_CODE                    EM_X86_64
4587 #define ELF_MAXPAGESIZE                     0x200000
4588 #define ELF_MINPAGESIZE                     0x1000
4589 #define ELF_COMMONPAGESIZE                  0x1000
4590
4591 #define elf_backend_can_gc_sections         1
4592 #define elf_backend_can_refcount            1
4593 #define elf_backend_want_got_plt            1
4594 #define elf_backend_plt_readonly            1
4595 #define elf_backend_want_plt_sym            0
4596 #define elf_backend_got_header_size         (GOT_ENTRY_SIZE*3)
4597 #define elf_backend_rela_normal             1
4598
4599 #define elf_info_to_howto                   elf_x86_64_info_to_howto
4600
4601 #define bfd_elf64_bfd_link_hash_table_create \
4602   elf_x86_64_link_hash_table_create
4603 #define bfd_elf64_bfd_link_hash_table_free \
4604   elf_x86_64_link_hash_table_free
4605 #define bfd_elf64_bfd_reloc_type_lookup     elf_x86_64_reloc_type_lookup
4606 #define bfd_elf64_bfd_reloc_name_lookup \
4607   elf_x86_64_reloc_name_lookup
4608
4609 #define elf_backend_adjust_dynamic_symbol   elf_x86_64_adjust_dynamic_symbol
4610 #define elf_backend_relocs_compatible       elf_x86_64_relocs_compatible
4611 #define elf_backend_check_relocs            elf_x86_64_check_relocs
4612 #define elf_backend_copy_indirect_symbol    elf_x86_64_copy_indirect_symbol
4613 #define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
4614 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
4615 #define elf_backend_finish_dynamic_symbol   elf_x86_64_finish_dynamic_symbol
4616 #define elf_backend_gc_mark_hook            elf_x86_64_gc_mark_hook
4617 #define elf_backend_gc_sweep_hook           elf_x86_64_gc_sweep_hook
4618 #define elf_backend_grok_prstatus           elf_x86_64_grok_prstatus
4619 #define elf_backend_grok_psinfo             elf_x86_64_grok_psinfo
4620 #define elf_backend_reloc_type_class        elf_x86_64_reloc_type_class
4621 #define elf_backend_relocate_section        elf_x86_64_relocate_section
4622 #define elf_backend_size_dynamic_sections   elf_x86_64_size_dynamic_sections
4623 #define elf_backend_always_size_sections    elf_x86_64_always_size_sections
4624 #define elf_backend_init_index_section      _bfd_elf_init_1_index_section
4625 #define elf_backend_plt_sym_val             elf_x86_64_plt_sym_val
4626 #define elf_backend_object_p                elf64_x86_64_elf_object_p
4627 #define bfd_elf64_mkobject                  elf_x86_64_mkobject
4628
4629 #define elf_backend_section_from_shdr \
4630         elf_x86_64_section_from_shdr
4631
4632 #define elf_backend_section_from_bfd_section \
4633   elf_x86_64_elf_section_from_bfd_section
4634 #define elf_backend_add_symbol_hook \
4635   elf_x86_64_add_symbol_hook
4636 #define elf_backend_symbol_processing \
4637   elf_x86_64_symbol_processing
4638 #define elf_backend_common_section_index \
4639   elf_x86_64_common_section_index
4640 #define elf_backend_common_section \
4641   elf_x86_64_common_section
4642 #define elf_backend_common_definition \
4643   elf_x86_64_common_definition
4644 #define elf_backend_merge_symbol \
4645   elf_x86_64_merge_symbol
4646 #define elf_backend_special_sections \
4647   elf_x86_64_special_sections
4648 #define elf_backend_additional_program_headers \
4649   elf_x86_64_additional_program_headers
4650 #define elf_backend_hash_symbol \
4651   elf_x86_64_hash_symbol
4652
4653 #undef  elf_backend_post_process_headers
4654 #define elf_backend_post_process_headers  _bfd_elf_set_osabi
4655
4656 #include "elf64-target.h"
4657
4658 /* FreeBSD support.  */
4659
4660 #undef  TARGET_LITTLE_SYM
4661 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_freebsd_vec
4662 #undef  TARGET_LITTLE_NAME
4663 #define TARGET_LITTLE_NAME                  "elf64-x86-64-freebsd"
4664
4665 #undef  ELF_OSABI
4666 #define ELF_OSABI                           ELFOSABI_FREEBSD
4667
4668 #undef  elf64_bed
4669 #define elf64_bed elf64_x86_64_fbsd_bed
4670
4671 #include "elf64-target.h"
4672
4673 /* Solaris 2 support.  */
4674
4675 #undef  TARGET_LITTLE_SYM
4676 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_sol2_vec
4677 #undef  TARGET_LITTLE_NAME
4678 #define TARGET_LITTLE_NAME                  "elf64-x86-64-sol2"
4679
4680 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4681    objects won't be recognized.  */
4682 #undef ELF_OSABI
4683
4684 #undef  elf64_bed
4685 #define elf64_bed                           elf64_x86_64_sol2_bed
4686
4687 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
4688    boundary.  */
4689 #undef elf_backend_static_tls_alignment
4690 #define elf_backend_static_tls_alignment    16
4691
4692 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4693
4694    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4695    File, p.63.  */
4696 #undef elf_backend_want_plt_sym
4697 #define elf_backend_want_plt_sym            1
4698
4699 #include "elf64-target.h"
4700
4701 /* Intel L1OM support.  */
4702
4703 static bfd_boolean
4704 elf64_l1om_elf_object_p (bfd *abfd)
4705 {
4706   /* Set the right machine number for an L1OM elf64 file.  */
4707   bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
4708   return TRUE;
4709 }
4710
4711 #undef  TARGET_LITTLE_SYM
4712 #define TARGET_LITTLE_SYM                   bfd_elf64_l1om_vec
4713 #undef  TARGET_LITTLE_NAME
4714 #define TARGET_LITTLE_NAME                  "elf64-l1om"
4715 #undef ELF_ARCH
4716 #define ELF_ARCH                            bfd_arch_l1om
4717
4718 #undef  ELF_MACHINE_CODE
4719 #define ELF_MACHINE_CODE                    EM_L1OM
4720
4721 #undef  ELF_OSABI
4722
4723 #undef  elf64_bed
4724 #define elf64_bed elf64_l1om_bed
4725
4726 #undef elf_backend_object_p
4727 #define elf_backend_object_p                elf64_l1om_elf_object_p
4728
4729 #undef  elf_backend_post_process_headers
4730 #undef  elf_backend_static_tls_alignment
4731
4732 #undef elf_backend_want_plt_sym
4733 #define elf_backend_want_plt_sym            0
4734
4735 #include "elf64-target.h"
4736
4737 /* FreeBSD L1OM support.  */
4738
4739 #undef  TARGET_LITTLE_SYM
4740 #define TARGET_LITTLE_SYM                   bfd_elf64_l1om_freebsd_vec
4741 #undef  TARGET_LITTLE_NAME
4742 #define TARGET_LITTLE_NAME                  "elf64-l1om-freebsd"
4743
4744 #undef  ELF_OSABI
4745 #define ELF_OSABI                           ELFOSABI_FREEBSD
4746
4747 #undef  elf64_bed
4748 #define elf64_bed elf64_l1om_fbsd_bed
4749
4750 #undef  elf_backend_post_process_headers
4751 #define elf_backend_post_process_headers  _bfd_elf_set_osabi
4752
4753 #include "elf64-target.h"
4754
4755 /* 32bit x86-64 support.  */
4756
4757 static bfd_boolean
4758 elf32_x86_64_elf_object_p (bfd *abfd)
4759 {
4760   /* Set the right machine number for an x86-64 elf32 file.  */
4761   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
4762   return TRUE;
4763 }
4764
4765 #undef  TARGET_LITTLE_SYM
4766 #define TARGET_LITTLE_SYM                   bfd_elf32_x86_64_vec
4767 #undef  TARGET_LITTLE_NAME
4768 #define TARGET_LITTLE_NAME                  "elf32-x86-64"
4769
4770 #undef ELF_ARCH
4771 #define ELF_ARCH                            bfd_arch_i386
4772
4773 #undef  ELF_MACHINE_CODE
4774 #define ELF_MACHINE_CODE                    EM_X86_64
4775
4776 #define bfd_elf32_bfd_link_hash_table_create \
4777   elf_x86_64_link_hash_table_create
4778 #define bfd_elf32_bfd_link_hash_table_free \
4779   elf_x86_64_link_hash_table_free
4780 #define bfd_elf32_bfd_reloc_type_lookup \
4781   elf_x86_64_reloc_type_lookup
4782 #define bfd_elf32_bfd_reloc_name_lookup \
4783   elf_x86_64_reloc_name_lookup
4784 #define bfd_elf32_mkobject \
4785   elf_x86_64_mkobject
4786
4787 #undef  ELF_OSABI
4788
4789 #undef elf_backend_post_process_headers
4790
4791 #undef elf_backend_object_p
4792 #define elf_backend_object_p \
4793   elf32_x86_64_elf_object_p
4794
4795 #undef elf_backend_bfd_from_remote_memory
4796 #define elf_backend_bfd_from_remote_memory \
4797   _bfd_elf32_bfd_from_remote_memory
4798
4799 #undef elf_backend_size_info
4800 #define elf_backend_size_info \
4801   _bfd_elf32_size_info
4802
4803 #include "elf32-target.h"