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