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