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