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