OSDN Git Service

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