x86: Add _bfd_x86_elf_link_setup_gnu_properties
[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 #define PLT_CIE_LENGTH          20
651 #define PLT_FDE_LENGTH          36
652 #define PLT_FDE_START_OFFSET    4 + PLT_CIE_LENGTH + 8
653 #define PLT_FDE_LEN_OFFSET      4 + PLT_CIE_LENGTH + 12
654   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
655   0, 0, 0, 0,                   /* CIE ID */
656   1,                            /* CIE version */
657   'z', 'R', 0,                  /* Augmentation string */
658   1,                            /* Code alignment factor */
659   0x78,                         /* Data alignment factor */
660   16,                           /* Return address column */
661   1,                            /* Augmentation size */
662   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
663   DW_CFA_def_cfa, 7, 8,         /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
664   DW_CFA_offset + 16, 1,        /* DW_CFA_offset: r16 (rip) at cfa-8 */
665   DW_CFA_nop, DW_CFA_nop,
666
667   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
668   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
669   0, 0, 0, 0,                   /* R_X86_64_PC32 .plt goes here */
670   0, 0, 0, 0,                   /* .plt size goes here */
671   0,                            /* Augmentation size */
672   DW_CFA_def_cfa_offset, 16,    /* DW_CFA_def_cfa_offset: 16 */
673   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
674   DW_CFA_def_cfa_offset, 24,    /* DW_CFA_def_cfa_offset: 24 */
675   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
676   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
677   11,                           /* Block length */
678   DW_OP_breg7, 8,               /* DW_OP_breg7 (rsp): 8 */
679   DW_OP_breg16, 0,              /* DW_OP_breg16 (rip): 0 */
680   DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
681   DW_OP_lit3, DW_OP_shl, DW_OP_plus,
682   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
683 };
684
685 /* .eh_frame covering the lazy BND .plt section.  */
686
687 static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt[] =
688 {
689   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
690   0, 0, 0, 0,                   /* CIE ID */
691   1,                            /* CIE version */
692   'z', 'R', 0,                  /* Augmentation string */
693   1,                            /* Code alignment factor */
694   0x78,                         /* Data alignment factor */
695   16,                           /* Return address column */
696   1,                            /* Augmentation size */
697   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
698   DW_CFA_def_cfa, 7, 8,         /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
699   DW_CFA_offset + 16, 1,        /* DW_CFA_offset: r16 (rip) at cfa-8 */
700   DW_CFA_nop, DW_CFA_nop,
701
702   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
703   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
704   0, 0, 0, 0,                   /* R_X86_64_PC32 .plt goes here */
705   0, 0, 0, 0,                   /* .plt size goes here */
706   0,                            /* Augmentation size */
707   DW_CFA_def_cfa_offset, 16,    /* DW_CFA_def_cfa_offset: 16 */
708   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
709   DW_CFA_def_cfa_offset, 24,    /* DW_CFA_def_cfa_offset: 24 */
710   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
711   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
712   11,                           /* Block length */
713   DW_OP_breg7, 8,               /* DW_OP_breg7 (rsp): 8 */
714   DW_OP_breg16, 0,              /* DW_OP_breg16 (rip): 0 */
715   DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
716   DW_OP_lit3, DW_OP_shl, DW_OP_plus,
717   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
718 };
719
720 /* .eh_frame covering the lazy .plt section with IBT-enabled.  */
721
722 static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt[] =
723 {
724   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
725   0, 0, 0, 0,                   /* CIE ID */
726   1,                            /* CIE version */
727   'z', 'R', 0,                  /* Augmentation string */
728   1,                            /* Code alignment factor */
729   0x78,                         /* Data alignment factor */
730   16,                           /* Return address column */
731   1,                            /* Augmentation size */
732   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
733   DW_CFA_def_cfa, 7, 8,         /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
734   DW_CFA_offset + 16, 1,        /* DW_CFA_offset: r16 (rip) at cfa-8 */
735   DW_CFA_nop, DW_CFA_nop,
736
737   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
738   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
739   0, 0, 0, 0,                   /* R_X86_64_PC32 .plt goes here */
740   0, 0, 0, 0,                   /* .plt size goes here */
741   0,                            /* Augmentation size */
742   DW_CFA_def_cfa_offset, 16,    /* DW_CFA_def_cfa_offset: 16 */
743   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
744   DW_CFA_def_cfa_offset, 24,    /* DW_CFA_def_cfa_offset: 24 */
745   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
746   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
747   11,                           /* Block length */
748   DW_OP_breg7, 8,               /* DW_OP_breg7 (rsp): 8 */
749   DW_OP_breg16, 0,              /* DW_OP_breg16 (rip): 0 */
750   DW_OP_lit15, DW_OP_and, DW_OP_lit10, DW_OP_ge,
751   DW_OP_lit3, DW_OP_shl, DW_OP_plus,
752   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
753 };
754
755 /* .eh_frame covering the x32 lazy .plt section with IBT-enabled.  */
756
757 static const bfd_byte elf_x32_eh_frame_lazy_ibt_plt[] =
758 {
759   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
760   0, 0, 0, 0,                   /* CIE ID */
761   1,                            /* CIE version */
762   'z', 'R', 0,                  /* Augmentation string */
763   1,                            /* Code alignment factor */
764   0x78,                         /* Data alignment factor */
765   16,                           /* Return address column */
766   1,                            /* Augmentation size */
767   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
768   DW_CFA_def_cfa, 7, 8,         /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
769   DW_CFA_offset + 16, 1,        /* DW_CFA_offset: r16 (rip) at cfa-8 */
770   DW_CFA_nop, DW_CFA_nop,
771
772   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
773   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
774   0, 0, 0, 0,                   /* R_X86_64_PC32 .plt goes here */
775   0, 0, 0, 0,                   /* .plt size goes here */
776   0,                            /* Augmentation size */
777   DW_CFA_def_cfa_offset, 16,    /* DW_CFA_def_cfa_offset: 16 */
778   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
779   DW_CFA_def_cfa_offset, 24,    /* DW_CFA_def_cfa_offset: 24 */
780   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
781   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
782   11,                           /* Block length */
783   DW_OP_breg7, 8,               /* DW_OP_breg7 (rsp): 8 */
784   DW_OP_breg16, 0,              /* DW_OP_breg16 (rip): 0 */
785   DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
786   DW_OP_lit3, DW_OP_shl, DW_OP_plus,
787   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
788 };
789
790 /* .eh_frame covering the non-lazy .plt section.  */
791
792 static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt[] =
793 {
794 #define PLT_GOT_FDE_LENGTH              20
795   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
796   0, 0, 0, 0,                   /* CIE ID */
797   1,                            /* CIE version */
798   'z', 'R', 0,                  /* Augmentation string */
799   1,                            /* Code alignment factor */
800   0x78,                         /* Data alignment factor */
801   16,                           /* Return address column */
802   1,                            /* Augmentation size */
803   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
804   DW_CFA_def_cfa, 7, 8,         /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
805   DW_CFA_offset + 16, 1,        /* DW_CFA_offset: r16 (rip) at cfa-8 */
806   DW_CFA_nop, DW_CFA_nop,
807
808   PLT_GOT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
809   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
810   0, 0, 0, 0,                   /* the start of non-lazy .plt goes here */
811   0, 0, 0, 0,                   /* non-lazy .plt size goes here */
812   0,                            /* Augmentation size */
813   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
814   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
815 };
816
817 /* Architecture-specific backend data for x86-64.  */
818
819 struct elf_x86_64_backend_data
820 {
821   /* Target system.  */
822   enum
823     {
824       is_normal,
825       is_nacl
826     } os;
827 };
828
829 #define get_elf_x86_64_arch_data(bed) \
830   ((const struct elf_x86_64_backend_data *) (bed)->arch_data)
831
832 #define get_elf_x86_64_backend_data(abfd) \
833   get_elf_x86_64_arch_data (get_elf_backend_data (abfd))
834
835 /* These are the standard parameters.  */
836 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_plt =
837   {
838     elf_x86_64_lazy_plt0_entry,         /* plt0_entry */
839     LAZY_PLT_ENTRY_SIZE,                /* plt0_entry_size */
840     elf_x86_64_lazy_plt_entry,          /* plt_entry */
841     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
842     2,                                  /* plt0_got1_offset */
843     8,                                  /* plt0_got2_offset */
844     12,                                 /* plt0_got2_insn_end */
845     2,                                  /* plt_got_offset */
846     7,                                  /* plt_reloc_offset */
847     12,                                 /* plt_plt_offset */
848     6,                                  /* plt_got_insn_size */
849     LAZY_PLT_ENTRY_SIZE,                /* plt_plt_insn_end */
850     6,                                  /* plt_lazy_offset */
851     elf_x86_64_lazy_plt0_entry,         /* pic_plt0_entry */
852     elf_x86_64_lazy_plt_entry,          /* pic_plt_entry */
853     elf_x86_64_eh_frame_lazy_plt,       /* eh_frame_plt */
854     sizeof (elf_x86_64_eh_frame_lazy_plt) /* eh_frame_plt_size */
855   };
856
857 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_plt =
858   {
859     elf_x86_64_non_lazy_plt_entry,      /* plt_entry */
860     elf_x86_64_non_lazy_plt_entry,      /* pic_plt_entry */
861     NON_LAZY_PLT_ENTRY_SIZE,            /* plt_entry_size */
862     2,                                  /* plt_got_offset */
863     6,                                  /* plt_got_insn_size */
864     elf_x86_64_eh_frame_non_lazy_plt,   /* eh_frame_plt */
865     sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
866   };
867
868 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_plt =
869   {
870     elf_x86_64_lazy_bnd_plt0_entry,     /* plt0_entry */
871     LAZY_PLT_ENTRY_SIZE,                /* plt0_entry_size */
872     elf_x86_64_lazy_bnd_plt_entry,      /* plt_entry */
873     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
874     2,                                  /* plt0_got1_offset */
875     1+8,                                /* plt0_got2_offset */
876     1+12,                               /* plt0_got2_insn_end */
877     1+2,                                /* plt_got_offset */
878     1,                                  /* plt_reloc_offset */
879     7,                                  /* plt_plt_offset */
880     1+6,                                /* plt_got_insn_size */
881     11,                                 /* plt_plt_insn_end */
882     0,                                  /* plt_lazy_offset */
883     elf_x86_64_lazy_bnd_plt0_entry,     /* pic_plt0_entry */
884     elf_x86_64_lazy_bnd_plt_entry,      /* pic_plt_entry */
885     elf_x86_64_eh_frame_lazy_bnd_plt,   /* eh_frame_plt */
886     sizeof (elf_x86_64_eh_frame_lazy_bnd_plt) /* eh_frame_plt_size */
887   };
888
889 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt =
890   {
891     elf_x86_64_non_lazy_bnd_plt_entry,  /* plt_entry */
892     elf_x86_64_non_lazy_bnd_plt_entry,  /* pic_plt_entry */
893     NON_LAZY_PLT_ENTRY_SIZE,            /* plt_entry_size */
894     1+2,                                /* plt_got_offset */
895     1+6,                                /* plt_got_insn_size */
896     elf_x86_64_eh_frame_non_lazy_plt,   /* eh_frame_plt */
897     sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
898   };
899
900 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_ibt_plt =
901   {
902     elf_x86_64_lazy_bnd_plt0_entry,     /* plt0_entry */
903     LAZY_PLT_ENTRY_SIZE,                /* plt0_entry_size */
904     elf_x86_64_lazy_ibt_plt_entry,      /* plt_entry */
905     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
906     2,                                  /* plt0_got1_offset */
907     1+8,                                /* plt0_got2_offset */
908     1+12,                               /* plt0_got2_insn_end */
909     4+1+2,                              /* plt_got_offset */
910     4+1,                                /* plt_reloc_offset */
911     4+1+6,                              /* plt_plt_offset */
912     4+1+6,                              /* plt_got_insn_size */
913     4+1+5+5,                            /* plt_plt_insn_end */
914     0,                                  /* plt_lazy_offset */
915     elf_x86_64_lazy_bnd_plt0_entry,     /* pic_plt0_entry */
916     elf_x86_64_lazy_ibt_plt_entry,      /* pic_plt_entry */
917     elf_x86_64_eh_frame_lazy_ibt_plt,   /* eh_frame_plt */
918     sizeof (elf_x86_64_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
919   };
920
921 static const struct elf_x86_lazy_plt_layout elf_x32_lazy_ibt_plt =
922   {
923     elf_x86_64_lazy_plt0_entry,         /* plt0_entry */
924     LAZY_PLT_ENTRY_SIZE,                /* plt0_entry_size */
925     elf_x32_lazy_ibt_plt_entry,         /* plt_entry */
926     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
927     2,                                  /* plt0_got1_offset */
928     8,                                  /* plt0_got2_offset */
929     12,                                 /* plt0_got2_insn_end */
930     4+2,                                /* plt_got_offset */
931     4+1,                                /* plt_reloc_offset */
932     4+6,                                /* plt_plt_offset */
933     4+6,                                /* plt_got_insn_size */
934     4+5+5,                              /* plt_plt_insn_end */
935     0,                                  /* plt_lazy_offset */
936     elf_x86_64_lazy_plt0_entry,         /* pic_plt0_entry */
937     elf_x32_lazy_ibt_plt_entry,         /* pic_plt_entry */
938     elf_x32_eh_frame_lazy_ibt_plt,      /* eh_frame_plt */
939     sizeof (elf_x32_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
940   };
941
942 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt =
943   {
944     elf_x86_64_non_lazy_ibt_plt_entry,  /* plt_entry */
945     elf_x86_64_non_lazy_ibt_plt_entry,  /* pic_plt_entry */
946     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
947     4+1+2,                              /* plt_got_offset */
948     4+1+6,                              /* plt_got_insn_size */
949     elf_x86_64_eh_frame_non_lazy_plt,   /* eh_frame_plt */
950     sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
951   };
952
953 static const struct elf_x86_non_lazy_plt_layout elf_x32_non_lazy_ibt_plt =
954   {
955     elf_x32_non_lazy_ibt_plt_entry,     /* plt_entry */
956     elf_x32_non_lazy_ibt_plt_entry,     /* pic_plt_entry */
957     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
958     4+2,                                /* plt_got_offset */
959     4+6,                                /* plt_got_insn_size */
960     elf_x86_64_eh_frame_non_lazy_plt,   /* eh_frame_plt */
961     sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
962   };
963
964 static const struct elf_x86_64_backend_data elf_x86_64_arch_bed =
965   {
966     is_normal                            /* os */
967   };
968
969 #define elf_backend_arch_data   &elf_x86_64_arch_bed
970
971 /* Values in tls_type of x86 ELF linker hash entry.  */
972 #define GOT_TLS_IE      3
973 #define GOT_TLS_GDESC   4
974 #define GOT_TLS_GD_BOTH_P(type) \
975   ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
976 #define GOT_TLS_GD_P(type) \
977   ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
978 #define GOT_TLS_GDESC_P(type) \
979   ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
980 #define GOT_TLS_GD_ANY_P(type) \
981   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
982
983 #define is_x86_64_elf(bfd)                              \
984   (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
985    && elf_tdata (bfd) != NULL                           \
986    && elf_object_id (bfd) == X86_64_ELF_DATA)
987
988 static bfd_boolean
989 elf_x86_64_mkobject (bfd *abfd)
990 {
991   return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_obj_tdata),
992                                   X86_64_ELF_DATA);
993 }
994
995 #define elf_x86_64_compute_jump_table_size(htab) \
996   ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
997
998 static bfd_boolean
999 elf64_x86_64_elf_object_p (bfd *abfd)
1000 {
1001   /* Set the right machine number for an x86-64 elf64 file.  */
1002   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
1003   return TRUE;
1004 }
1005
1006 static bfd_boolean
1007 elf32_x86_64_elf_object_p (bfd *abfd)
1008 {
1009   /* Set the right machine number for an x86-64 elf32 file.  */
1010   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1011   return TRUE;
1012 }
1013
1014 /* Return TRUE if the TLS access code sequence support transition
1015    from R_TYPE.  */
1016
1017 static bfd_boolean
1018 elf_x86_64_check_tls_transition (bfd *abfd,
1019                                  struct bfd_link_info *info,
1020                                  asection *sec,
1021                                  bfd_byte *contents,
1022                                  Elf_Internal_Shdr *symtab_hdr,
1023                                  struct elf_link_hash_entry **sym_hashes,
1024                                  unsigned int r_type,
1025                                  const Elf_Internal_Rela *rel,
1026                                  const Elf_Internal_Rela *relend)
1027 {
1028   unsigned int val;
1029   unsigned long r_symndx;
1030   bfd_boolean largepic = FALSE;
1031   struct elf_link_hash_entry *h;
1032   bfd_vma offset;
1033   struct elf_x86_link_hash_table *htab;
1034   bfd_byte *call;
1035   bfd_boolean indirect_call;
1036
1037   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1038   offset = rel->r_offset;
1039   switch (r_type)
1040     {
1041     case R_X86_64_TLSGD:
1042     case R_X86_64_TLSLD:
1043       if ((rel + 1) >= relend)
1044         return FALSE;
1045
1046       if (r_type == R_X86_64_TLSGD)
1047         {
1048           /* Check transition from GD access model.  For 64bit, only
1049                 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1050                 .word 0x6666; rex64; call __tls_get_addr@PLT
1051              or
1052                 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1053                 .byte 0x66; rex64
1054                 call *__tls_get_addr@GOTPCREL(%rip)
1055                 which may be converted to
1056                 addr32 call __tls_get_addr
1057              can transit to different access model.  For 32bit, only
1058                 leaq foo@tlsgd(%rip), %rdi
1059                 .word 0x6666; rex64; call __tls_get_addr@PLT
1060              or
1061                 leaq foo@tlsgd(%rip), %rdi
1062                 .byte 0x66; rex64
1063                 call *__tls_get_addr@GOTPCREL(%rip)
1064                 which may be converted to
1065                 addr32 call __tls_get_addr
1066              can transit to different access model.  For largepic,
1067              we also support:
1068                 leaq foo@tlsgd(%rip), %rdi
1069                 movabsq $__tls_get_addr@pltoff, %rax
1070                 addq $r15, %rax
1071                 call *%rax
1072              or
1073                 leaq foo@tlsgd(%rip), %rdi
1074                 movabsq $__tls_get_addr@pltoff, %rax
1075                 addq $rbx, %rax
1076                 call *%rax  */
1077
1078           static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1079
1080           if ((offset + 12) > sec->size)
1081             return FALSE;
1082
1083           call = contents + offset + 4;
1084           if (call[0] != 0x66
1085               || !((call[1] == 0x48
1086                     && call[2] == 0xff
1087                     && call[3] == 0x15)
1088                    || (call[1] == 0x48
1089                        && call[2] == 0x67
1090                        && call[3] == 0xe8)
1091                    || (call[1] == 0x66
1092                        && call[2] == 0x48
1093                        && call[3] == 0xe8)))
1094             {
1095               if (!ABI_64_P (abfd)
1096                   || (offset + 19) > sec->size
1097                   || offset < 3
1098                   || memcmp (call - 7, leaq + 1, 3) != 0
1099                   || memcmp (call, "\x48\xb8", 2) != 0
1100                   || call[11] != 0x01
1101                   || call[13] != 0xff
1102                   || call[14] != 0xd0
1103                   || !((call[10] == 0x48 && call[12] == 0xd8)
1104                        || (call[10] == 0x4c && call[12] == 0xf8)))
1105                 return FALSE;
1106               largepic = TRUE;
1107             }
1108           else if (ABI_64_P (abfd))
1109             {
1110               if (offset < 4
1111                   || memcmp (contents + offset - 4, leaq, 4) != 0)
1112                 return FALSE;
1113             }
1114           else
1115             {
1116               if (offset < 3
1117                   || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1118                 return FALSE;
1119             }
1120           indirect_call = call[2] == 0xff;
1121         }
1122       else
1123         {
1124           /* Check transition from LD access model.  Only
1125                 leaq foo@tlsld(%rip), %rdi;
1126                 call __tls_get_addr@PLT
1127              or
1128                 leaq foo@tlsld(%rip), %rdi;
1129                 call *__tls_get_addr@GOTPCREL(%rip)
1130                 which may be converted to
1131                 addr32 call __tls_get_addr
1132              can transit to different access model.  For largepic
1133              we also support:
1134                 leaq foo@tlsld(%rip), %rdi
1135                 movabsq $__tls_get_addr@pltoff, %rax
1136                 addq $r15, %rax
1137                 call *%rax
1138              or
1139                 leaq foo@tlsld(%rip), %rdi
1140                 movabsq $__tls_get_addr@pltoff, %rax
1141                 addq $rbx, %rax
1142                 call *%rax  */
1143
1144           static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1145
1146           if (offset < 3 || (offset + 9) > sec->size)
1147             return FALSE;
1148
1149           if (memcmp (contents + offset - 3, lea, 3) != 0)
1150             return FALSE;
1151
1152           call = contents + offset + 4;
1153           if (!(call[0] == 0xe8
1154                 || (call[0] == 0xff && call[1] == 0x15)
1155                 || (call[0] == 0x67 && call[1] == 0xe8)))
1156             {
1157               if (!ABI_64_P (abfd)
1158                   || (offset + 19) > sec->size
1159                   || memcmp (call, "\x48\xb8", 2) != 0
1160                   || call[11] != 0x01
1161                   || call[13] != 0xff
1162                   || call[14] != 0xd0
1163                   || !((call[10] == 0x48 && call[12] == 0xd8)
1164                        || (call[10] == 0x4c && call[12] == 0xf8)))
1165                 return FALSE;
1166               largepic = TRUE;
1167             }
1168           indirect_call = call[0] == 0xff;
1169         }
1170
1171       r_symndx = htab->r_sym (rel[1].r_info);
1172       if (r_symndx < symtab_hdr->sh_info)
1173         return FALSE;
1174
1175       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1176       if (h == NULL
1177           || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
1178         return FALSE;
1179       else if (largepic)
1180         return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLTOFF64;
1181       else if (indirect_call)
1182         return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_GOTPCRELX;
1183       else
1184         return (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1185                 || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
1186
1187     case R_X86_64_GOTTPOFF:
1188       /* Check transition from IE access model:
1189                 mov foo@gottpoff(%rip), %reg
1190                 add foo@gottpoff(%rip), %reg
1191        */
1192
1193       /* Check REX prefix first.  */
1194       if (offset >= 3 && (offset + 4) <= sec->size)
1195         {
1196           val = bfd_get_8 (abfd, contents + offset - 3);
1197           if (val != 0x48 && val != 0x4c)
1198             {
1199               /* X32 may have 0x44 REX prefix or no REX prefix.  */
1200               if (ABI_64_P (abfd))
1201                 return FALSE;
1202             }
1203         }
1204       else
1205         {
1206           /* X32 may not have any REX prefix.  */
1207           if (ABI_64_P (abfd))
1208             return FALSE;
1209           if (offset < 2 || (offset + 3) > sec->size)
1210             return FALSE;
1211         }
1212
1213       val = bfd_get_8 (abfd, contents + offset - 2);
1214       if (val != 0x8b && val != 0x03)
1215         return FALSE;
1216
1217       val = bfd_get_8 (abfd, contents + offset - 1);
1218       return (val & 0xc7) == 5;
1219
1220     case R_X86_64_GOTPC32_TLSDESC:
1221       /* Check transition from GDesc access model:
1222                 leaq x@tlsdesc(%rip), %rax
1223
1224          Make sure it's a leaq adding rip to a 32-bit offset
1225          into any register, although it's probably almost always
1226          going to be rax.  */
1227
1228       if (offset < 3 || (offset + 4) > sec->size)
1229         return FALSE;
1230
1231       val = bfd_get_8 (abfd, contents + offset - 3);
1232       if ((val & 0xfb) != 0x48)
1233         return FALSE;
1234
1235       if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1236         return FALSE;
1237
1238       val = bfd_get_8 (abfd, contents + offset - 1);
1239       return (val & 0xc7) == 0x05;
1240
1241     case R_X86_64_TLSDESC_CALL:
1242       /* Check transition from GDesc access model:
1243                 call *x@tlsdesc(%rax)
1244        */
1245       if (offset + 2 <= sec->size)
1246         {
1247           /* Make sure that it's a call *x@tlsdesc(%rax).  */
1248           call = contents + offset;
1249           return call[0] == 0xff && call[1] == 0x10;
1250         }
1251
1252       return FALSE;
1253
1254     default:
1255       abort ();
1256     }
1257 }
1258
1259 /* Return TRUE if the TLS access transition is OK or no transition
1260    will be performed.  Update R_TYPE if there is a transition.  */
1261
1262 static bfd_boolean
1263 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1264                            asection *sec, bfd_byte *contents,
1265                            Elf_Internal_Shdr *symtab_hdr,
1266                            struct elf_link_hash_entry **sym_hashes,
1267                            unsigned int *r_type, int tls_type,
1268                            const Elf_Internal_Rela *rel,
1269                            const Elf_Internal_Rela *relend,
1270                            struct elf_link_hash_entry *h,
1271                            unsigned long r_symndx,
1272                            bfd_boolean from_relocate_section)
1273 {
1274   unsigned int from_type = *r_type;
1275   unsigned int to_type = from_type;
1276   bfd_boolean check = TRUE;
1277
1278   /* Skip TLS transition for functions.  */
1279   if (h != NULL
1280       && (h->type == STT_FUNC
1281           || h->type == STT_GNU_IFUNC))
1282     return TRUE;
1283
1284   switch (from_type)
1285     {
1286     case R_X86_64_TLSGD:
1287     case R_X86_64_GOTPC32_TLSDESC:
1288     case R_X86_64_TLSDESC_CALL:
1289     case R_X86_64_GOTTPOFF:
1290       if (bfd_link_executable (info))
1291         {
1292           if (h == NULL)
1293             to_type = R_X86_64_TPOFF32;
1294           else
1295             to_type = R_X86_64_GOTTPOFF;
1296         }
1297
1298       /* When we are called from elf_x86_64_relocate_section, there may
1299          be additional transitions based on TLS_TYPE.  */
1300       if (from_relocate_section)
1301         {
1302           unsigned int new_to_type = to_type;
1303
1304           if (bfd_link_executable (info)
1305               && h != NULL
1306               && h->dynindx == -1
1307               && tls_type == GOT_TLS_IE)
1308             new_to_type = R_X86_64_TPOFF32;
1309
1310           if (to_type == R_X86_64_TLSGD
1311               || to_type == R_X86_64_GOTPC32_TLSDESC
1312               || to_type == R_X86_64_TLSDESC_CALL)
1313             {
1314               if (tls_type == GOT_TLS_IE)
1315                 new_to_type = R_X86_64_GOTTPOFF;
1316             }
1317
1318           /* We checked the transition before when we were called from
1319              elf_x86_64_check_relocs.  We only want to check the new
1320              transition which hasn't been checked before.  */
1321           check = new_to_type != to_type && from_type == to_type;
1322           to_type = new_to_type;
1323         }
1324
1325       break;
1326
1327     case R_X86_64_TLSLD:
1328       if (bfd_link_executable (info))
1329         to_type = R_X86_64_TPOFF32;
1330       break;
1331
1332     default:
1333       return TRUE;
1334     }
1335
1336   /* Return TRUE if there is no transition.  */
1337   if (from_type == to_type)
1338     return TRUE;
1339
1340   /* Check if the transition can be performed.  */
1341   if (check
1342       && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1343                                             symtab_hdr, sym_hashes,
1344                                             from_type, rel, relend))
1345     {
1346       reloc_howto_type *from, *to;
1347       const char *name;
1348
1349       from = elf_x86_64_rtype_to_howto (abfd, from_type);
1350       to = elf_x86_64_rtype_to_howto (abfd, to_type);
1351
1352       if (h)
1353         name = h->root.root.string;
1354       else
1355         {
1356           struct elf_x86_link_hash_table *htab;
1357
1358           htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1359           if (htab == NULL)
1360             name = "*unknown*";
1361           else
1362             {
1363               Elf_Internal_Sym *isym;
1364
1365               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1366                                             abfd, r_symndx);
1367               name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1368             }
1369         }
1370
1371       _bfd_error_handler
1372         /* xgettext:c-format */
1373         (_("%B: TLS transition from %s to %s against `%s' at %#Lx "
1374            "in section `%A' failed"),
1375          abfd, from->name, to->name, name, rel->r_offset, sec);
1376       bfd_set_error (bfd_error_bad_value);
1377       return FALSE;
1378     }
1379
1380   *r_type = to_type;
1381   return TRUE;
1382 }
1383
1384 /* Rename some of the generic section flags to better document how they
1385    are used here.  */
1386 #define need_convert_load       sec_flg0
1387 #define check_relocs_failed     sec_flg1
1388
1389 static bfd_boolean
1390 elf_x86_64_need_pic (struct bfd_link_info *info,
1391                      bfd *input_bfd, asection *sec,
1392                      struct elf_link_hash_entry *h,
1393                      Elf_Internal_Shdr *symtab_hdr,
1394                      Elf_Internal_Sym *isym,
1395                      reloc_howto_type *howto)
1396 {
1397   const char *v = "";
1398   const char *und = "";
1399   const char *pic = "";
1400   const char *object;
1401
1402   const char *name;
1403   if (h)
1404     {
1405       name = h->root.root.string;
1406       switch (ELF_ST_VISIBILITY (h->other))
1407         {
1408         case STV_HIDDEN:
1409           v = _("hidden symbol ");
1410           break;
1411         case STV_INTERNAL:
1412           v = _("internal symbol ");
1413           break;
1414         case STV_PROTECTED:
1415           v = _("protected symbol ");
1416           break;
1417         default:
1418           if (((struct elf_x86_link_hash_entry *) h)->def_protected)
1419             v = _("protected symbol ");
1420           else
1421             v = _("symbol ");
1422           pic = _("; recompile with -fPIC");
1423           break;
1424         }
1425
1426       if (!h->def_regular && !h->def_dynamic)
1427         und = _("undefined ");
1428     }
1429   else
1430     {
1431       name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1432       pic = _("; recompile with -fPIC");
1433     }
1434
1435   if (bfd_link_dll (info))
1436     object = _("a shared object");
1437   else if (bfd_link_pie (info))
1438     object = _("a PIE object");
1439   else
1440     object = _("a PDE object");
1441
1442   /* xgettext:c-format */
1443   _bfd_error_handler (_("%B: relocation %s against %s%s`%s' can "
1444                         "not be used when making %s%s"),
1445                       input_bfd, howto->name, und, v, name,
1446                       object, pic);
1447   bfd_set_error (bfd_error_bad_value);
1448   sec->check_relocs_failed = 1;
1449   return FALSE;
1450 }
1451
1452 /* With the local symbol, foo, we convert
1453    mov foo@GOTPCREL(%rip), %reg
1454    to
1455    lea foo(%rip), %reg
1456    and convert
1457    call/jmp *foo@GOTPCREL(%rip)
1458    to
1459    nop call foo/jmp foo nop
1460    When PIC is false, convert
1461    test %reg, foo@GOTPCREL(%rip)
1462    to
1463    test $foo, %reg
1464    and convert
1465    binop foo@GOTPCREL(%rip), %reg
1466    to
1467    binop $foo, %reg
1468    where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1469    instructions.  */
1470
1471 static bfd_boolean
1472 elf_x86_64_convert_load_reloc (bfd *abfd, asection *sec,
1473                                bfd_byte *contents,
1474                                Elf_Internal_Rela *irel,
1475                                struct elf_link_hash_entry *h,
1476                                bfd_boolean *converted,
1477                                struct bfd_link_info *link_info)
1478 {
1479   struct elf_x86_link_hash_table *htab;
1480   bfd_boolean is_pic;
1481   bfd_boolean require_reloc_pc32;
1482   bfd_boolean relocx;
1483   bfd_boolean to_reloc_pc32;
1484   asection *tsec;
1485   char symtype;
1486   bfd_signed_vma raddend;
1487   unsigned int opcode;
1488   unsigned int modrm;
1489   unsigned int r_type = ELF32_R_TYPE (irel->r_info);
1490   unsigned int r_symndx;
1491   bfd_vma toff;
1492   bfd_vma roff = irel->r_offset;
1493
1494   if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
1495     return TRUE;
1496
1497   raddend = irel->r_addend;
1498   /* Addend for 32-bit PC-relative relocation must be -4.  */
1499   if (raddend != -4)
1500     return TRUE;
1501
1502   htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
1503   is_pic = bfd_link_pic (link_info);
1504
1505   relocx = (r_type == R_X86_64_GOTPCRELX
1506             || r_type == R_X86_64_REX_GOTPCRELX);
1507
1508   /* TRUE if we can convert only to R_X86_64_PC32.  Enable it for
1509      --no-relax.  */
1510   require_reloc_pc32
1511     = link_info->disable_target_specific_optimizations > 1;
1512
1513   r_symndx = htab->r_sym (irel->r_info);
1514
1515   opcode = bfd_get_8 (abfd, contents + roff - 2);
1516
1517   /* Convert mov to lea since it has been done for a while.  */
1518   if (opcode != 0x8b)
1519     {
1520       /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1521          for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1522          test, xor instructions.  */
1523       if (!relocx)
1524         return TRUE;
1525     }
1526
1527   /* We convert only to R_X86_64_PC32:
1528      1. Branch.
1529      2. R_X86_64_GOTPCREL since we can't modify REX byte.
1530      3. require_reloc_pc32 is true.
1531      4. PIC.
1532      */
1533   to_reloc_pc32 = (opcode == 0xff
1534                    || !relocx
1535                    || require_reloc_pc32
1536                    || is_pic);
1537
1538   /* Get the symbol referred to by the reloc.  */
1539   if (h == NULL)
1540     {
1541       Elf_Internal_Sym *isym
1542         = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
1543
1544       /* Skip relocation against undefined symbols.  */
1545       if (isym->st_shndx == SHN_UNDEF)
1546         return TRUE;
1547
1548       symtype = ELF_ST_TYPE (isym->st_info);
1549
1550       if (isym->st_shndx == SHN_ABS)
1551         tsec = bfd_abs_section_ptr;
1552       else if (isym->st_shndx == SHN_COMMON)
1553         tsec = bfd_com_section_ptr;
1554       else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1555         tsec = &_bfd_elf_large_com_section;
1556       else
1557         tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1558
1559       toff = isym->st_value;
1560     }
1561   else
1562     {
1563       /* Undefined weak symbol is only bound locally in executable
1564          and its reference is resolved as 0 without relocation
1565          overflow.  We can only perform this optimization for
1566          GOTPCRELX relocations since we need to modify REX byte.
1567          It is OK convert mov with R_X86_64_GOTPCREL to
1568          R_X86_64_PC32.  */
1569       if ((relocx || opcode == 0x8b)
1570           && UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info,
1571                                               X86_64_ELF_DATA,
1572                                               TRUE,
1573                                               elf_x86_hash_entry (h)))
1574         {
1575           if (opcode == 0xff)
1576             {
1577               /* Skip for branch instructions since R_X86_64_PC32
1578                  may overflow.  */
1579               if (require_reloc_pc32)
1580                 return TRUE;
1581             }
1582           else if (relocx)
1583             {
1584               /* For non-branch instructions, we can convert to
1585                  R_X86_64_32/R_X86_64_32S since we know if there
1586                  is a REX byte.  */
1587               to_reloc_pc32 = FALSE;
1588             }
1589
1590           /* Since we don't know the current PC when PIC is true,
1591              we can't convert to R_X86_64_PC32.  */
1592           if (to_reloc_pc32 && is_pic)
1593             return TRUE;
1594
1595           goto convert;
1596         }
1597       /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1598          ld.so may use its link-time address.  */
1599       else if (h->start_stop
1600                || ((h->def_regular
1601                     || h->root.type == bfd_link_hash_defined
1602                     || h->root.type == bfd_link_hash_defweak)
1603                    && h != htab->elf.hdynamic
1604                    && SYMBOL_REFERENCES_LOCAL (link_info, h)))
1605         {
1606           /* bfd_link_hash_new or bfd_link_hash_undefined is
1607              set by an assignment in a linker script in
1608              bfd_elf_record_link_assignment.  start_stop is set
1609              on __start_SECNAME/__stop_SECNAME which mark section
1610              SECNAME.  */
1611           if (h->start_stop
1612               || (h->def_regular
1613                   && (h->root.type == bfd_link_hash_new
1614                       || h->root.type == bfd_link_hash_undefined
1615                       || ((h->root.type == bfd_link_hash_defined
1616                            || h->root.type == bfd_link_hash_defweak)
1617                           && h->root.u.def.section == bfd_und_section_ptr))))
1618             {
1619               /* Skip since R_X86_64_32/R_X86_64_32S may overflow.  */
1620               if (require_reloc_pc32)
1621                 return TRUE;
1622               goto convert;
1623             }
1624           tsec = h->root.u.def.section;
1625           toff = h->root.u.def.value;
1626           symtype = h->type;
1627         }
1628       else
1629         return TRUE;
1630     }
1631
1632   /* Don't convert GOTPCREL relocation against large section.  */
1633   if (elf_section_data (tsec) !=  NULL
1634       && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1635     return TRUE;
1636
1637   /* We can only estimate relocation overflow for R_X86_64_PC32.  */
1638   if (!to_reloc_pc32)
1639     goto convert;
1640
1641   if (tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
1642     {
1643       /* At this stage in linking, no SEC_MERGE symbol has been
1644          adjusted, so all references to such symbols need to be
1645          passed through _bfd_merged_section_offset.  (Later, in
1646          relocate_section, all SEC_MERGE symbols *except* for
1647          section symbols have been adjusted.)
1648
1649          gas may reduce relocations against symbols in SEC_MERGE
1650          sections to a relocation against the section symbol when
1651          the original addend was zero.  When the reloc is against
1652          a section symbol we should include the addend in the
1653          offset passed to _bfd_merged_section_offset, since the
1654          location of interest is the original symbol.  On the
1655          other hand, an access to "sym+addend" where "sym" is not
1656          a section symbol should not include the addend;  Such an
1657          access is presumed to be an offset from "sym";  The
1658          location of interest is just "sym".  */
1659       if (symtype == STT_SECTION)
1660         toff += raddend;
1661
1662       toff = _bfd_merged_section_offset (abfd, &tsec,
1663                                          elf_section_data (tsec)->sec_info,
1664                                          toff);
1665
1666       if (symtype != STT_SECTION)
1667         toff += raddend;
1668     }
1669   else
1670     toff += raddend;
1671
1672   /* Don't convert if R_X86_64_PC32 relocation overflows.  */
1673   if (tsec->output_section == sec->output_section)
1674     {
1675       if ((toff - roff + 0x80000000) > 0xffffffff)
1676         return TRUE;
1677     }
1678   else
1679     {
1680       bfd_signed_vma distance;
1681
1682       /* At this point, we don't know the load addresses of TSEC
1683          section nor SEC section.  We estimate the distrance between
1684          SEC and TSEC.  We store the estimated distances in the
1685          compressed_size field of the output section, which is only
1686          used to decompress the compressed input section.  */
1687       if (sec->output_section->compressed_size == 0)
1688         {
1689           asection *asect;
1690           bfd_size_type size = 0;
1691           for (asect = link_info->output_bfd->sections;
1692                asect != NULL;
1693                asect = asect->next)
1694             /* Skip debug sections since compressed_size is used to
1695                compress debug sections.  */
1696             if ((asect->flags & SEC_DEBUGGING) == 0)
1697               {
1698                 asection *i;
1699                 for (i = asect->map_head.s;
1700                      i != NULL;
1701                      i = i->map_head.s)
1702                   {
1703                     size = align_power (size, i->alignment_power);
1704                     size += i->size;
1705                   }
1706                 asect->compressed_size = size;
1707               }
1708         }
1709
1710       /* Don't convert GOTPCREL relocations if TSEC isn't placed
1711          after SEC.  */
1712       distance = (tsec->output_section->compressed_size
1713                   - sec->output_section->compressed_size);
1714       if (distance < 0)
1715         return TRUE;
1716
1717       /* Take PT_GNU_RELRO segment into account by adding
1718          maxpagesize.  */
1719       if ((toff + distance + get_elf_backend_data (abfd)->maxpagesize
1720            - roff + 0x80000000) > 0xffffffff)
1721         return TRUE;
1722     }
1723
1724 convert:
1725   if (opcode == 0xff)
1726     {
1727       /* We have "call/jmp *foo@GOTPCREL(%rip)".  */
1728       unsigned int nop;
1729       unsigned int disp;
1730       bfd_vma nop_offset;
1731
1732       /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1733          R_X86_64_PC32.  */
1734       modrm = bfd_get_8 (abfd, contents + roff - 1);
1735       if (modrm == 0x25)
1736         {
1737           /* Convert to "jmp foo nop".  */
1738           modrm = 0xe9;
1739           nop = NOP_OPCODE;
1740           nop_offset = irel->r_offset + 3;
1741           disp = bfd_get_32 (abfd, contents + irel->r_offset);
1742           irel->r_offset -= 1;
1743           bfd_put_32 (abfd, disp, contents + irel->r_offset);
1744         }
1745       else
1746         {
1747           struct elf_x86_link_hash_entry *eh
1748             = (struct elf_x86_link_hash_entry *) h;
1749
1750           /* Convert to "nop call foo".  ADDR_PREFIX_OPCODE
1751              is a nop prefix.  */
1752           modrm = 0xe8;
1753           /* To support TLS optimization, always use addr32 prefix for
1754              "call *__tls_get_addr@GOTPCREL(%rip)".  */
1755           if (eh && eh->tls_get_addr)
1756             {
1757               nop = 0x67;
1758               nop_offset = irel->r_offset - 2;
1759             }
1760           else
1761             {
1762               nop = link_info->call_nop_byte;
1763               if (link_info->call_nop_as_suffix)
1764                 {
1765                   nop_offset = irel->r_offset + 3;
1766                   disp = bfd_get_32 (abfd, contents + irel->r_offset);
1767                   irel->r_offset -= 1;
1768                   bfd_put_32 (abfd, disp, contents + irel->r_offset);
1769                 }
1770               else
1771                 nop_offset = irel->r_offset - 2;
1772             }
1773         }
1774       bfd_put_8 (abfd, nop, contents + nop_offset);
1775       bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1776       r_type = R_X86_64_PC32;
1777     }
1778   else
1779     {
1780       unsigned int rex;
1781       unsigned int rex_mask = REX_R;
1782
1783       if (r_type == R_X86_64_REX_GOTPCRELX)
1784         rex = bfd_get_8 (abfd, contents + roff - 3);
1785       else
1786         rex = 0;
1787
1788       if (opcode == 0x8b)
1789         {
1790           if (to_reloc_pc32)
1791             {
1792               /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1793                  "lea foo(%rip), %reg".  */
1794               opcode = 0x8d;
1795               r_type = R_X86_64_PC32;
1796             }
1797           else
1798             {
1799               /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1800                  "mov $foo, %reg".  */
1801               opcode = 0xc7;
1802               modrm = bfd_get_8 (abfd, contents + roff - 1);
1803               modrm = 0xc0 | (modrm & 0x38) >> 3;
1804               if ((rex & REX_W) != 0
1805                   && ABI_64_P (link_info->output_bfd))
1806                 {
1807                   /* Keep the REX_W bit in REX byte for LP64.  */
1808                   r_type = R_X86_64_32S;
1809                   goto rewrite_modrm_rex;
1810                 }
1811               else
1812                 {
1813                   /* If the REX_W bit in REX byte isn't needed,
1814                      use R_X86_64_32 and clear the W bit to avoid
1815                      sign-extend imm32 to imm64.  */
1816                   r_type = R_X86_64_32;
1817                   /* Clear the W bit in REX byte.  */
1818                   rex_mask |= REX_W;
1819                   goto rewrite_modrm_rex;
1820                 }
1821             }
1822         }
1823       else
1824         {
1825           /* R_X86_64_PC32 isn't supported.  */
1826           if (to_reloc_pc32)
1827             return TRUE;
1828
1829           modrm = bfd_get_8 (abfd, contents + roff - 1);
1830           if (opcode == 0x85)
1831             {
1832               /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1833                  "test $foo, %reg".  */
1834               modrm = 0xc0 | (modrm & 0x38) >> 3;
1835               opcode = 0xf7;
1836             }
1837           else
1838             {
1839               /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1840                  "binop $foo, %reg".  */
1841               modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1842               opcode = 0x81;
1843             }
1844
1845           /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1846              overflow when sign-extending imm32 to imm64.  */
1847           r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
1848
1849 rewrite_modrm_rex:
1850           bfd_put_8 (abfd, modrm, contents + roff - 1);
1851
1852           if (rex)
1853             {
1854               /* Move the R bit to the B bit in REX byte.  */
1855               rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
1856               bfd_put_8 (abfd, rex, contents + roff - 3);
1857             }
1858
1859           /* No addend for R_X86_64_32/R_X86_64_32S relocations.  */
1860           irel->r_addend = 0;
1861         }
1862
1863       bfd_put_8 (abfd, opcode, contents + roff - 2);
1864     }
1865
1866   irel->r_info = htab->r_info (r_symndx, r_type);
1867
1868   *converted = TRUE;
1869
1870   return TRUE;
1871 }
1872
1873 /* Look through the relocs for a section during the first phase, and
1874    calculate needed space in the global offset table, procedure
1875    linkage table, and dynamic reloc sections.  */
1876
1877 static bfd_boolean
1878 elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1879                          asection *sec,
1880                          const Elf_Internal_Rela *relocs)
1881 {
1882   struct elf_x86_link_hash_table *htab;
1883   Elf_Internal_Shdr *symtab_hdr;
1884   struct elf_link_hash_entry **sym_hashes;
1885   const Elf_Internal_Rela *rel;
1886   const Elf_Internal_Rela *rel_end;
1887   asection *sreloc;
1888   bfd_byte *contents;
1889
1890   if (bfd_link_relocatable (info))
1891     return TRUE;
1892
1893   /* Don't do anything special with non-loaded, non-alloced sections.
1894      In particular, any relocs in such sections should not affect GOT
1895      and PLT reference counting (ie. we don't allow them to create GOT
1896      or PLT entries), there's no possibility or desire to optimize TLS
1897      relocs, and there's not much point in propagating relocs to shared
1898      libs that the dynamic linker won't relocate.  */
1899   if ((sec->flags & SEC_ALLOC) == 0)
1900     return TRUE;
1901
1902   BFD_ASSERT (is_x86_64_elf (abfd));
1903
1904   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1905   if (htab == NULL)
1906     {
1907       sec->check_relocs_failed = 1;
1908       return FALSE;
1909     }
1910
1911   /* Get the section contents.  */
1912   if (elf_section_data (sec)->this_hdr.contents != NULL)
1913     contents = elf_section_data (sec)->this_hdr.contents;
1914   else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1915     {
1916       sec->check_relocs_failed = 1;
1917       return FALSE;
1918     }
1919
1920   symtab_hdr = &elf_symtab_hdr (abfd);
1921   sym_hashes = elf_sym_hashes (abfd);
1922
1923   sreloc = NULL;
1924
1925   rel_end = relocs + sec->reloc_count;
1926   for (rel = relocs; rel < rel_end; rel++)
1927     {
1928       unsigned int r_type;
1929       unsigned int r_symndx;
1930       struct elf_link_hash_entry *h;
1931       struct elf_x86_link_hash_entry *eh;
1932       Elf_Internal_Sym *isym;
1933       const char *name;
1934       bfd_boolean size_reloc;
1935
1936       r_symndx = htab->r_sym (rel->r_info);
1937       r_type = ELF32_R_TYPE (rel->r_info);
1938
1939       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1940         {
1941           /* xgettext:c-format */
1942           _bfd_error_handler (_("%B: bad symbol index: %d"),
1943                               abfd, r_symndx);
1944           goto error_return;
1945         }
1946
1947       if (r_symndx < symtab_hdr->sh_info)
1948         {
1949           /* A local symbol.  */
1950           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1951                                         abfd, r_symndx);
1952           if (isym == NULL)
1953             goto error_return;
1954
1955           /* Check relocation against local STT_GNU_IFUNC symbol.  */
1956           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1957             {
1958               h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
1959                                                    TRUE);
1960               if (h == NULL)
1961                 goto error_return;
1962
1963               /* Fake a STT_GNU_IFUNC symbol.  */
1964               h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1965                                                       isym, NULL);
1966               h->type = STT_GNU_IFUNC;
1967               h->def_regular = 1;
1968               h->ref_regular = 1;
1969               h->forced_local = 1;
1970               h->root.type = bfd_link_hash_defined;
1971             }
1972           else
1973             h = NULL;
1974         }
1975       else
1976         {
1977           isym = NULL;
1978           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1979           while (h->root.type == bfd_link_hash_indirect
1980                  || h->root.type == bfd_link_hash_warning)
1981             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1982         }
1983
1984       /* Check invalid x32 relocations.  */
1985       if (!ABI_64_P (abfd))
1986         switch (r_type)
1987           {
1988           default:
1989             break;
1990
1991           case R_X86_64_DTPOFF64:
1992           case R_X86_64_TPOFF64:
1993           case R_X86_64_PC64:
1994           case R_X86_64_GOTOFF64:
1995           case R_X86_64_GOT64:
1996           case R_X86_64_GOTPCREL64:
1997           case R_X86_64_GOTPC64:
1998           case R_X86_64_GOTPLT64:
1999           case R_X86_64_PLTOFF64:
2000               {
2001                 if (h)
2002                   name = h->root.root.string;
2003                 else
2004                   name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2005                                            NULL);
2006                 _bfd_error_handler
2007                   /* xgettext:c-format */
2008                   (_("%B: relocation %s against symbol `%s' isn't "
2009                      "supported in x32 mode"), abfd,
2010                    x86_64_elf_howto_table[r_type].name, name);
2011                 bfd_set_error (bfd_error_bad_value);
2012                 goto error_return;
2013               }
2014             break;
2015           }
2016
2017       if (h != NULL)
2018         {
2019           /* It is referenced by a non-shared object. */
2020           h->ref_regular = 1;
2021           h->root.non_ir_ref_regular = 1;
2022
2023           if (h->type == STT_GNU_IFUNC)
2024             elf_tdata (info->output_bfd)->has_gnu_symbols
2025               |= elf_gnu_symbol_ifunc;
2026         }
2027
2028       if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
2029                                        symtab_hdr, sym_hashes,
2030                                        &r_type, GOT_UNKNOWN,
2031                                        rel, rel_end, h, r_symndx, FALSE))
2032         goto error_return;
2033
2034       eh = (struct elf_x86_link_hash_entry *) h;
2035       switch (r_type)
2036         {
2037         case R_X86_64_TLSLD:
2038           htab->tls_ld_or_ldm_got.refcount += 1;
2039           goto create_got;
2040
2041         case R_X86_64_TPOFF32:
2042           if (!bfd_link_executable (info) && ABI_64_P (abfd))
2043             return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2044                                         &x86_64_elf_howto_table[r_type]);
2045           if (eh != NULL)
2046             eh->has_got_reloc = 1;
2047           break;
2048
2049         case R_X86_64_GOTTPOFF:
2050           if (!bfd_link_executable (info))
2051             info->flags |= DF_STATIC_TLS;
2052           /* Fall through */
2053
2054         case R_X86_64_GOT32:
2055         case R_X86_64_GOTPCREL:
2056         case R_X86_64_GOTPCRELX:
2057         case R_X86_64_REX_GOTPCRELX:
2058         case R_X86_64_TLSGD:
2059         case R_X86_64_GOT64:
2060         case R_X86_64_GOTPCREL64:
2061         case R_X86_64_GOTPLT64:
2062         case R_X86_64_GOTPC32_TLSDESC:
2063         case R_X86_64_TLSDESC_CALL:
2064           /* This symbol requires a global offset table entry.  */
2065           {
2066             int tls_type, old_tls_type;
2067
2068             switch (r_type)
2069               {
2070               default: tls_type = GOT_NORMAL; break;
2071               case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
2072               case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
2073               case R_X86_64_GOTPC32_TLSDESC:
2074               case R_X86_64_TLSDESC_CALL:
2075                 tls_type = GOT_TLS_GDESC; break;
2076               }
2077
2078             if (h != NULL)
2079               {
2080                 h->got.refcount += 1;
2081                 old_tls_type = eh->tls_type;
2082               }
2083             else
2084               {
2085                 bfd_signed_vma *local_got_refcounts;
2086
2087                 /* This is a global offset table entry for a local symbol.  */
2088                 local_got_refcounts = elf_local_got_refcounts (abfd);
2089                 if (local_got_refcounts == NULL)
2090                   {
2091                     bfd_size_type size;
2092
2093                     size = symtab_hdr->sh_info;
2094                     size *= sizeof (bfd_signed_vma)
2095                       + sizeof (bfd_vma) + sizeof (char);
2096                     local_got_refcounts = ((bfd_signed_vma *)
2097                                            bfd_zalloc (abfd, size));
2098                     if (local_got_refcounts == NULL)
2099                       goto error_return;
2100                     elf_local_got_refcounts (abfd) = local_got_refcounts;
2101                     elf_x86_local_tlsdesc_gotent (abfd)
2102                       = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2103                     elf_x86_local_got_tls_type (abfd)
2104                       = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2105                   }
2106                 local_got_refcounts[r_symndx] += 1;
2107                 old_tls_type
2108                   = elf_x86_local_got_tls_type (abfd) [r_symndx];
2109               }
2110
2111             /* If a TLS symbol is accessed using IE at least once,
2112                there is no point to use dynamic model for it.  */
2113             if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2114                 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2115                     || tls_type != GOT_TLS_IE))
2116               {
2117                 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
2118                   tls_type = old_tls_type;
2119                 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2120                          && GOT_TLS_GD_ANY_P (tls_type))
2121                   tls_type |= old_tls_type;
2122                 else
2123                   {
2124                     if (h)
2125                       name = h->root.root.string;
2126                     else
2127                       name = bfd_elf_sym_name (abfd, symtab_hdr,
2128                                                isym, NULL);
2129                     _bfd_error_handler
2130                       /* xgettext:c-format */
2131                       (_("%B: '%s' accessed both as normal and"
2132                          " thread local symbol"),
2133                        abfd, name);
2134                     bfd_set_error (bfd_error_bad_value);
2135                     goto error_return;
2136                   }
2137               }
2138
2139             if (old_tls_type != tls_type)
2140               {
2141                 if (eh != NULL)
2142                   eh->tls_type = tls_type;
2143                 else
2144                   elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
2145               }
2146           }
2147           /* Fall through */
2148
2149         case R_X86_64_GOTOFF64:
2150         case R_X86_64_GOTPC32:
2151         case R_X86_64_GOTPC64:
2152         create_got:
2153           if (eh != NULL)
2154             eh->has_got_reloc = 1;
2155           break;
2156
2157         case R_X86_64_PLT32:
2158         case R_X86_64_PLT32_BND:
2159           /* This symbol requires a procedure linkage table entry.  We
2160              actually build the entry in adjust_dynamic_symbol,
2161              because this might be a case of linking PIC code which is
2162              never referenced by a dynamic object, in which case we
2163              don't need to generate a procedure linkage table entry
2164              after all.  */
2165
2166           /* If this is a local symbol, we resolve it directly without
2167              creating a procedure linkage table entry.  */
2168           if (h == NULL)
2169             continue;
2170
2171           eh->has_got_reloc = 1;
2172           h->needs_plt = 1;
2173           h->plt.refcount += 1;
2174           break;
2175
2176         case R_X86_64_PLTOFF64:
2177           /* This tries to form the 'address' of a function relative
2178              to GOT.  For global symbols we need a PLT entry.  */
2179           if (h != NULL)
2180             {
2181               h->needs_plt = 1;
2182               h->plt.refcount += 1;
2183             }
2184           goto create_got;
2185
2186         case R_X86_64_SIZE32:
2187         case R_X86_64_SIZE64:
2188           size_reloc = TRUE;
2189           goto do_size;
2190
2191         case R_X86_64_32:
2192           if (!ABI_64_P (abfd))
2193             goto pointer;
2194           /* Fall through.  */
2195         case R_X86_64_8:
2196         case R_X86_64_16:
2197         case R_X86_64_32S:
2198           /* Check relocation overflow as these relocs may lead to
2199              run-time relocation overflow.  Don't error out for
2200              sections we don't care about, such as debug sections or
2201              when relocation overflow check is disabled.  */
2202           if (!info->no_reloc_overflow_check
2203               && (bfd_link_pic (info)
2204                   || (bfd_link_executable (info)
2205                       && h != NULL
2206                       && !h->def_regular
2207                       && h->def_dynamic
2208                       && (sec->flags & SEC_READONLY) == 0)))
2209             return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2210                                         &x86_64_elf_howto_table[r_type]);
2211           /* Fall through.  */
2212
2213         case R_X86_64_PC8:
2214         case R_X86_64_PC16:
2215         case R_X86_64_PC32:
2216         case R_X86_64_PC32_BND:
2217         case R_X86_64_PC64:
2218         case R_X86_64_64:
2219 pointer:
2220           if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2221             eh->has_non_got_reloc = 1;
2222           /* We are called after all symbols have been resolved.  Only
2223              relocation against STT_GNU_IFUNC symbol must go through
2224              PLT.  */
2225           if (h != NULL
2226               && (bfd_link_executable (info)
2227                   || h->type == STT_GNU_IFUNC))
2228             {
2229               /* If this reloc is in a read-only section, we might
2230                  need a copy reloc.  We can't check reliably at this
2231                  stage whether the section is read-only, as input
2232                  sections have not yet been mapped to output sections.
2233                  Tentatively set the flag for now, and correct in
2234                  adjust_dynamic_symbol.  */
2235               h->non_got_ref = 1;
2236
2237               /* We may need a .plt entry if the symbol is a function
2238                  defined in a shared lib or is a STT_GNU_IFUNC function
2239                  referenced from the code or read-only section.  */
2240               if (!h->def_regular
2241                   || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2242                 h->plt.refcount += 1;
2243
2244               if (r_type == R_X86_64_PC32)
2245                 {
2246                   /* Since something like ".long foo - ." may be used
2247                      as pointer, make sure that PLT is used if foo is
2248                      a function defined in a shared library.  */
2249                   if ((sec->flags & SEC_CODE) == 0)
2250                     h->pointer_equality_needed = 1;
2251                 }
2252               else if (r_type != R_X86_64_PC32_BND
2253                        && r_type != R_X86_64_PC64)
2254                 {
2255                   h->pointer_equality_needed = 1;
2256                   /* At run-time, R_X86_64_64 can be resolved for both
2257                      x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2258                      can only be resolved for x32.  */
2259                   if ((sec->flags & SEC_READONLY) == 0
2260                       && (r_type == R_X86_64_64
2261                           || (!ABI_64_P (abfd)
2262                               && (r_type == R_X86_64_32
2263                                   || r_type == R_X86_64_32S))))
2264                     eh->func_pointer_refcount += 1;
2265                 }
2266             }
2267
2268           size_reloc = FALSE;
2269 do_size:
2270           /* If we are creating a shared library, and this is a reloc
2271              against a global symbol, or a non PC relative reloc
2272              against a local symbol, then we need to copy the reloc
2273              into the shared library.  However, if we are linking with
2274              -Bsymbolic, we do not need to copy a reloc against a
2275              global symbol which is defined in an object we are
2276              including in the link (i.e., DEF_REGULAR is set).  At
2277              this point we have not seen all the input files, so it is
2278              possible that DEF_REGULAR is not set now but will be set
2279              later (it is never cleared).  In case of a weak definition,
2280              DEF_REGULAR may be cleared later by a strong definition in
2281              a shared library.  We account for that possibility below by
2282              storing information in the relocs_copied field of the hash
2283              table entry.  A similar situation occurs when creating
2284              shared libraries and symbol visibility changes render the
2285              symbol local.
2286
2287              If on the other hand, we are creating an executable, we
2288              may need to keep relocations for symbols satisfied by a
2289              dynamic library if we manage to avoid copy relocs for the
2290              symbol.
2291
2292              Generate dynamic pointer relocation against STT_GNU_IFUNC
2293              symbol in the non-code section.  */
2294           if ((bfd_link_pic (info)
2295                && (! IS_X86_64_PCREL_TYPE (r_type)
2296                    || (h != NULL
2297                        && (! (bfd_link_pie (info)
2298                               || SYMBOLIC_BIND (info, h))
2299                            || h->root.type == bfd_link_hash_defweak
2300                            || !h->def_regular))))
2301               || (h != NULL
2302                   && h->type == STT_GNU_IFUNC
2303                   && r_type == htab->pointer_r_type
2304                   && (sec->flags & SEC_CODE) == 0)
2305               || (ELIMINATE_COPY_RELOCS
2306                   && !bfd_link_pic (info)
2307                   && h != NULL
2308                   && (h->root.type == bfd_link_hash_defweak
2309                       || !h->def_regular)))
2310             {
2311               struct elf_dyn_relocs *p;
2312               struct elf_dyn_relocs **head;
2313
2314               /* We must copy these reloc types into the output file.
2315                  Create a reloc section in dynobj and make room for
2316                  this reloc.  */
2317               if (sreloc == NULL)
2318                 {
2319                   sreloc = _bfd_elf_make_dynamic_reloc_section
2320                     (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
2321                      abfd, /*rela?*/ TRUE);
2322
2323                   if (sreloc == NULL)
2324                     goto error_return;
2325                 }
2326
2327               /* If this is a global symbol, we count the number of
2328                  relocations we need for this symbol.  */
2329               if (h != NULL)
2330                 head = &eh->dyn_relocs;
2331               else
2332                 {
2333                   /* Track dynamic relocs needed for local syms too.
2334                      We really need local syms available to do this
2335                      easily.  Oh well.  */
2336                   asection *s;
2337                   void **vpp;
2338
2339                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2340                                                 abfd, r_symndx);
2341                   if (isym == NULL)
2342                     goto error_return;
2343
2344                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2345                   if (s == NULL)
2346                     s = sec;
2347
2348                   /* Beware of type punned pointers vs strict aliasing
2349                      rules.  */
2350                   vpp = &(elf_section_data (s)->local_dynrel);
2351                   head = (struct elf_dyn_relocs **)vpp;
2352                 }
2353
2354               p = *head;
2355               if (p == NULL || p->sec != sec)
2356                 {
2357                   bfd_size_type amt = sizeof *p;
2358
2359                   p = ((struct elf_dyn_relocs *)
2360                        bfd_alloc (htab->elf.dynobj, amt));
2361                   if (p == NULL)
2362                     goto error_return;
2363                   p->next = *head;
2364                   *head = p;
2365                   p->sec = sec;
2366                   p->count = 0;
2367                   p->pc_count = 0;
2368                 }
2369
2370               p->count += 1;
2371               /* Count size relocation as PC-relative relocation.  */
2372               if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
2373                 p->pc_count += 1;
2374             }
2375           break;
2376
2377           /* This relocation describes the C++ object vtable hierarchy.
2378              Reconstruct it for later use during GC.  */
2379         case R_X86_64_GNU_VTINHERIT:
2380           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2381             goto error_return;
2382           break;
2383
2384           /* This relocation describes which C++ vtable entries are actually
2385              used.  Record for later use during GC.  */
2386         case R_X86_64_GNU_VTENTRY:
2387           BFD_ASSERT (h != NULL);
2388           if (h != NULL
2389               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2390             goto error_return;
2391           break;
2392
2393         default:
2394           break;
2395         }
2396
2397       if ((r_type == R_X86_64_GOTPCREL
2398            || r_type == R_X86_64_GOTPCRELX
2399            || r_type == R_X86_64_REX_GOTPCRELX)
2400           && (h == NULL || h->type != STT_GNU_IFUNC))
2401         sec->need_convert_load = 1;
2402     }
2403
2404   if (elf_section_data (sec)->this_hdr.contents != contents)
2405     {
2406       if (!info->keep_memory)
2407         free (contents);
2408       else
2409         {
2410           /* Cache the section contents for elf_link_input_bfd.  */
2411           elf_section_data (sec)->this_hdr.contents = contents;
2412         }
2413     }
2414
2415   return TRUE;
2416
2417 error_return:
2418   if (elf_section_data (sec)->this_hdr.contents != contents)
2419     free (contents);
2420   sec->check_relocs_failed = 1;
2421   return FALSE;
2422 }
2423
2424 /* Return the section that should be marked against GC for a given
2425    relocation.  */
2426
2427 static asection *
2428 elf_x86_64_gc_mark_hook (asection *sec,
2429                          struct bfd_link_info *info,
2430                          Elf_Internal_Rela *rel,
2431                          struct elf_link_hash_entry *h,
2432                          Elf_Internal_Sym *sym)
2433 {
2434   if (h != NULL)
2435     switch (ELF32_R_TYPE (rel->r_info))
2436       {
2437       case R_X86_64_GNU_VTINHERIT:
2438       case R_X86_64_GNU_VTENTRY:
2439         return NULL;
2440       }
2441
2442   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2443 }
2444
2445 /* Adjust a symbol defined by a dynamic object and referenced by a
2446    regular object.  The current definition is in some section of the
2447    dynamic object, but we're not including those sections.  We have to
2448    change the definition to something the rest of the link can
2449    understand.  */
2450
2451 static bfd_boolean
2452 elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2453                                   struct elf_link_hash_entry *h)
2454 {
2455   struct elf_x86_link_hash_table *htab;
2456   asection *s, *srel;
2457   struct elf_x86_link_hash_entry *eh;
2458   struct elf_dyn_relocs *p;
2459
2460   /* STT_GNU_IFUNC symbol must go through PLT. */
2461   if (h->type == STT_GNU_IFUNC)
2462     {
2463       /* All local STT_GNU_IFUNC references must be treate as local
2464          calls via local PLT.  */
2465       if (h->ref_regular
2466           && SYMBOL_CALLS_LOCAL (info, h))
2467         {
2468           bfd_size_type pc_count = 0, count = 0;
2469           struct elf_dyn_relocs **pp;
2470
2471           eh = (struct elf_x86_link_hash_entry *) h;
2472           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2473             {
2474               pc_count += p->pc_count;
2475               p->count -= p->pc_count;
2476               p->pc_count = 0;
2477               count += p->count;
2478               if (p->count == 0)
2479                 *pp = p->next;
2480               else
2481                 pp = &p->next;
2482             }
2483
2484           if (pc_count || count)
2485             {
2486               h->non_got_ref = 1;
2487               if (pc_count)
2488                 {
2489                   /* Increment PLT reference count only for PC-relative
2490                      references.  */
2491                   h->needs_plt = 1;
2492                   if (h->plt.refcount <= 0)
2493                     h->plt.refcount = 1;
2494                   else
2495                     h->plt.refcount += 1;
2496                 }
2497             }
2498         }
2499
2500       if (h->plt.refcount <= 0)
2501         {
2502           h->plt.offset = (bfd_vma) -1;
2503           h->needs_plt = 0;
2504         }
2505       return TRUE;
2506     }
2507
2508   /* If this is a function, put it in the procedure linkage table.  We
2509      will fill in the contents of the procedure linkage table later,
2510      when we know the address of the .got section.  */
2511   if (h->type == STT_FUNC
2512       || h->needs_plt)
2513     {
2514       if (h->plt.refcount <= 0
2515           || SYMBOL_CALLS_LOCAL (info, h)
2516           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2517               && h->root.type == bfd_link_hash_undefweak))
2518         {
2519           /* This case can occur if we saw a PLT32 reloc in an input
2520              file, but the symbol was never referred to by a dynamic
2521              object, or if all references were garbage collected.  In
2522              such a case, we don't actually need to build a procedure
2523              linkage table, and we can just do a PC32 reloc instead.  */
2524           h->plt.offset = (bfd_vma) -1;
2525           h->needs_plt = 0;
2526         }
2527
2528       return TRUE;
2529     }
2530   else
2531     /* It's possible that we incorrectly decided a .plt reloc was
2532        needed for an R_X86_64_PC32 reloc to a non-function sym in
2533        check_relocs.  We can't decide accurately between function and
2534        non-function syms in check-relocs;  Objects loaded later in
2535        the link may change h->type.  So fix it now.  */
2536     h->plt.offset = (bfd_vma) -1;
2537
2538   eh = (struct elf_x86_link_hash_entry *) h;
2539
2540   /* If this is a weak symbol, and there is a real definition, the
2541      processor independent code will have arranged for us to see the
2542      real definition first, and we can just use the same value.  */
2543   if (h->u.weakdef != NULL)
2544     {
2545       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2546                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
2547       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2548       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2549       if (ELIMINATE_COPY_RELOCS
2550           || info->nocopyreloc
2551           || SYMBOL_NO_COPYRELOC (info, eh))
2552         {
2553           h->non_got_ref = h->u.weakdef->non_got_ref;
2554           eh->needs_copy = h->u.weakdef->needs_copy;
2555         }
2556       return TRUE;
2557     }
2558
2559   /* This is a reference to a symbol defined by a dynamic object which
2560      is not a function.  */
2561
2562   /* If we are creating a shared library, we must presume that the
2563      only references to the symbol are via the global offset table.
2564      For such cases we need not do anything here; the relocations will
2565      be handled correctly by relocate_section.  */
2566   if (!bfd_link_executable (info))
2567     return TRUE;
2568
2569   /* If there are no references to this symbol that do not use the
2570      GOT, we don't need to generate a copy reloc.  */
2571   if (!h->non_got_ref)
2572     return TRUE;
2573
2574   /* If -z nocopyreloc was given, we won't generate them either.  */
2575   if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
2576     {
2577       h->non_got_ref = 0;
2578       return TRUE;
2579     }
2580
2581   if (ELIMINATE_COPY_RELOCS)
2582     {
2583       eh = (struct elf_x86_link_hash_entry *) h;
2584       for (p = eh->dyn_relocs; p != NULL; p = p->next)
2585         {
2586           s = p->sec->output_section;
2587           if (s != NULL && (s->flags & SEC_READONLY) != 0)
2588             break;
2589         }
2590
2591       /* If we didn't find any dynamic relocs in read-only sections, then
2592          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
2593       if (p == NULL)
2594         {
2595           h->non_got_ref = 0;
2596           return TRUE;
2597         }
2598     }
2599
2600   /* We must allocate the symbol in our .dynbss section, which will
2601      become part of the .bss section of the executable.  There will be
2602      an entry for this symbol in the .dynsym section.  The dynamic
2603      object will contain position independent code, so all references
2604      from the dynamic object to this symbol will go through the global
2605      offset table.  The dynamic linker will use the .dynsym entry to
2606      determine the address it must put in the global offset table, so
2607      both the dynamic object and the regular object will refer to the
2608      same memory location for the variable.  */
2609
2610   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2611   if (htab == NULL)
2612     return FALSE;
2613
2614   /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2615      to copy the initial value out of the dynamic object and into the
2616      runtime process image.  */
2617   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2618     {
2619       s = htab->elf.sdynrelro;
2620       srel = htab->elf.sreldynrelro;
2621     }
2622   else
2623     {
2624       s = htab->elf.sdynbss;
2625       srel = htab->elf.srelbss;
2626     }
2627   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2628     {
2629       const struct elf_backend_data *bed;
2630       bed = get_elf_backend_data (info->output_bfd);
2631       srel->size += bed->s->sizeof_rela;
2632       h->needs_copy = 1;
2633     }
2634
2635   return _bfd_elf_adjust_dynamic_copy (info, h, s);
2636 }
2637
2638 /* Allocate space in .plt, .got and associated reloc sections for
2639    dynamic relocs.  */
2640
2641 static bfd_boolean
2642 elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2643 {
2644   struct bfd_link_info *info;
2645   struct elf_x86_link_hash_table *htab;
2646   struct elf_x86_link_hash_entry *eh;
2647   struct elf_dyn_relocs *p;
2648   const struct elf_backend_data *bed;
2649   unsigned int plt_entry_size;
2650   bfd_boolean resolved_to_zero;
2651
2652   if (h->root.type == bfd_link_hash_indirect)
2653     return TRUE;
2654
2655   eh = (struct elf_x86_link_hash_entry *) h;
2656
2657   info = (struct bfd_link_info *) inf;
2658   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2659   if (htab == NULL)
2660     return FALSE;
2661   bed = get_elf_backend_data (info->output_bfd);
2662
2663   plt_entry_size = htab->plt.plt_entry_size;
2664
2665   resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2666                                                       X86_64_ELF_DATA,
2667                                                       eh->has_got_reloc,
2668                                                       eh);
2669
2670   /* We can't use the GOT PLT if pointer equality is needed since
2671      finish_dynamic_symbol won't clear symbol value and the dynamic
2672      linker won't update the GOT slot.  We will get into an infinite
2673      loop at run-time.  */
2674   if (htab->plt_got != NULL
2675       && h->type != STT_GNU_IFUNC
2676       && !h->pointer_equality_needed
2677       && h->plt.refcount > 0
2678       && h->got.refcount > 0)
2679     {
2680       /* Don't use the regular PLT if there are both GOT and GOTPLT
2681          reloctions.  */
2682       h->plt.offset = (bfd_vma) -1;
2683
2684       /* Use the GOT PLT.  */
2685       eh->plt_got.refcount = 1;
2686     }
2687
2688   /* Clear the reference count of function pointer relocations if
2689      symbol isn't a normal function.  */
2690   if (h->type != STT_FUNC)
2691     eh->func_pointer_refcount = 0;
2692
2693   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2694      here if it is defined and referenced in a non-shared object.  */
2695   if (h->type == STT_GNU_IFUNC
2696       && h->def_regular)
2697     {
2698       if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2699                                               &eh->dyn_relocs,
2700                                               &htab->readonly_dynrelocs_against_ifunc,
2701                                               plt_entry_size,
2702                                               (htab->plt.has_plt0
2703                                                * plt_entry_size),
2704                                               GOT_ENTRY_SIZE, TRUE))
2705         {
2706           asection *s = htab->plt_second;
2707           if (h->plt.offset != (bfd_vma) -1 && s != NULL)
2708             {
2709               /* Use the second PLT section if it is created.  */
2710               eh->plt_second.offset = s->size;
2711
2712               /* Make room for this entry in the second PLT section.  */
2713               s->size += htab->non_lazy_plt->plt_entry_size;
2714             }
2715
2716           return TRUE;
2717         }
2718       else
2719         return FALSE;
2720     }
2721   /* Don't create the PLT entry if there are only function pointer
2722      relocations which can be resolved at run-time.  */
2723   else if (htab->elf.dynamic_sections_created
2724            && (h->plt.refcount > eh->func_pointer_refcount
2725                || eh->plt_got.refcount > 0))
2726     {
2727       bfd_boolean use_plt_got = eh->plt_got.refcount > 0;
2728
2729       /* Clear the reference count of function pointer relocations
2730          if PLT is used.  */
2731       eh->func_pointer_refcount = 0;
2732
2733       /* Make sure this symbol is output as a dynamic symbol.
2734          Undefined weak syms won't yet be marked as dynamic.  */
2735       if (h->dynindx == -1
2736           && !h->forced_local
2737           && !resolved_to_zero
2738           && h->root.type == bfd_link_hash_undefweak)
2739         {
2740           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2741             return FALSE;
2742         }
2743
2744       if (bfd_link_pic (info)
2745           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2746         {
2747           asection *s = htab->elf.splt;
2748           asection *second_s = htab->plt_second;
2749           asection *got_s = htab->plt_got;
2750
2751           /* If this is the first .plt entry, make room for the special
2752              first entry.  The .plt section is used by prelink to undo
2753              prelinking for dynamic relocations.  */
2754           if (s->size == 0)
2755             s->size = htab->plt.has_plt0 * plt_entry_size;
2756
2757           if (use_plt_got)
2758             eh->plt_got.offset = got_s->size;
2759           else
2760             {
2761               h->plt.offset = s->size;
2762               if (second_s)
2763                 eh->plt_second.offset = second_s->size;
2764             }
2765
2766           /* If this symbol is not defined in a regular file, and we are
2767              not generating a shared library, then set the symbol to this
2768              location in the .plt.  This is required to make function
2769              pointers compare as equal between the normal executable and
2770              the shared library.  */
2771           if (! bfd_link_pic (info)
2772               && !h->def_regular)
2773             {
2774               if (use_plt_got)
2775                 {
2776                   /* We need to make a call to the entry of the GOT PLT
2777                      instead of regular PLT entry.  */
2778                   h->root.u.def.section = got_s;
2779                   h->root.u.def.value = eh->plt_got.offset;
2780                 }
2781               else
2782                 {
2783                   if (second_s)
2784                     {
2785                       /* We need to make a call to the entry of the
2786                          second PLT instead of regular PLT entry.  */
2787                       h->root.u.def.section = second_s;
2788                       h->root.u.def.value = eh->plt_second.offset;
2789                     }
2790                   else
2791                     {
2792                       h->root.u.def.section = s;
2793                       h->root.u.def.value = h->plt.offset;
2794                     }
2795                 }
2796             }
2797
2798           /* Make room for this entry.  */
2799           if (use_plt_got)
2800             got_s->size += htab->non_lazy_plt->plt_entry_size;
2801           else
2802             {
2803               s->size += plt_entry_size;
2804               if (second_s)
2805                 second_s->size += htab->non_lazy_plt->plt_entry_size;
2806
2807               /* We also need to make an entry in the .got.plt section,
2808                  which will be placed in the .got section by the linker
2809                  script.  */
2810               htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
2811
2812               /* There should be no PLT relocation against resolved
2813                  undefined weak symbol in executable.  */
2814               if (!resolved_to_zero)
2815                 {
2816                   /* We also need to make an entry in the .rela.plt
2817                      section.  */
2818                   htab->elf.srelplt->size += bed->s->sizeof_rela;
2819                   htab->elf.srelplt->reloc_count++;
2820                 }
2821             }
2822         }
2823       else
2824         {
2825           eh->plt_got.offset = (bfd_vma) -1;
2826           h->plt.offset = (bfd_vma) -1;
2827           h->needs_plt = 0;
2828         }
2829     }
2830   else
2831     {
2832       eh->plt_got.offset = (bfd_vma) -1;
2833       h->plt.offset = (bfd_vma) -1;
2834       h->needs_plt = 0;
2835     }
2836
2837   eh->tlsdesc_got = (bfd_vma) -1;
2838
2839   /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2840      make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
2841   if (h->got.refcount > 0
2842       && bfd_link_executable (info)
2843       && h->dynindx == -1
2844       && elf_x86_hash_entry (h)->tls_type == GOT_TLS_IE)
2845     {
2846       h->got.offset = (bfd_vma) -1;
2847     }
2848   else if (h->got.refcount > 0)
2849     {
2850       asection *s;
2851       bfd_boolean dyn;
2852       int tls_type = elf_x86_hash_entry (h)->tls_type;
2853
2854       /* Make sure this symbol is output as a dynamic symbol.
2855          Undefined weak syms won't yet be marked as dynamic.  */
2856       if (h->dynindx == -1
2857           && !h->forced_local
2858           && !resolved_to_zero
2859           && h->root.type == bfd_link_hash_undefweak)
2860         {
2861           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2862             return FALSE;
2863         }
2864
2865       if (GOT_TLS_GDESC_P (tls_type))
2866         {
2867           eh->tlsdesc_got = htab->elf.sgotplt->size
2868             - elf_x86_64_compute_jump_table_size (htab);
2869           htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2870           h->got.offset = (bfd_vma) -2;
2871         }
2872       if (! GOT_TLS_GDESC_P (tls_type)
2873           || GOT_TLS_GD_P (tls_type))
2874         {
2875           s = htab->elf.sgot;
2876           h->got.offset = s->size;
2877           s->size += GOT_ENTRY_SIZE;
2878           if (GOT_TLS_GD_P (tls_type))
2879             s->size += GOT_ENTRY_SIZE;
2880         }
2881       dyn = htab->elf.dynamic_sections_created;
2882       /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2883          and two if global.  R_X86_64_GOTTPOFF needs one dynamic
2884          relocation.  No dynamic relocation against resolved undefined
2885          weak symbol in executable.  */
2886       if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2887           || tls_type == GOT_TLS_IE)
2888         htab->elf.srelgot->size += bed->s->sizeof_rela;
2889       else if (GOT_TLS_GD_P (tls_type))
2890         htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
2891       else if (! GOT_TLS_GDESC_P (tls_type)
2892                && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2893                     && !resolved_to_zero)
2894                    || h->root.type != bfd_link_hash_undefweak)
2895                && (bfd_link_pic (info)
2896                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2897         htab->elf.srelgot->size += bed->s->sizeof_rela;
2898       if (GOT_TLS_GDESC_P (tls_type))
2899         {
2900           htab->elf.srelplt->size += bed->s->sizeof_rela;
2901           htab->tlsdesc_plt = (bfd_vma) -1;
2902         }
2903     }
2904   else
2905     h->got.offset = (bfd_vma) -1;
2906
2907   if (eh->dyn_relocs == NULL)
2908     return TRUE;
2909
2910   /* In the shared -Bsymbolic case, discard space allocated for
2911      dynamic pc-relative relocs against symbols which turn out to be
2912      defined in regular objects.  For the normal shared case, discard
2913      space for pc-relative relocs that have become local due to symbol
2914      visibility changes.  */
2915
2916   if (bfd_link_pic (info))
2917     {
2918       /* Relocs that use pc_count are those that appear on a call
2919          insn, or certain REL relocs that can generated via assembly.
2920          We want calls to protected symbols to resolve directly to the
2921          function rather than going via the plt.  If people want
2922          function pointer comparisons to work as expected then they
2923          should avoid writing weird assembly.  */
2924       if (SYMBOL_CALLS_LOCAL (info, h))
2925         {
2926           struct elf_dyn_relocs **pp;
2927
2928           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2929             {
2930               p->count -= p->pc_count;
2931               p->pc_count = 0;
2932               if (p->count == 0)
2933                 *pp = p->next;
2934               else
2935                 pp = &p->next;
2936             }
2937         }
2938
2939       /* Also discard relocs on undefined weak syms with non-default
2940          visibility or in PIE.  */
2941       if (eh->dyn_relocs != NULL)
2942         {
2943           if (h->root.type == bfd_link_hash_undefweak)
2944             {
2945               /* Undefined weak symbol is never bound locally in shared
2946                  library.  */
2947               if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2948                   || resolved_to_zero)
2949                 eh->dyn_relocs = NULL;
2950               else if (h->dynindx == -1
2951                        && ! h->forced_local
2952                        && ! bfd_elf_link_record_dynamic_symbol (info, h))
2953                 return FALSE;
2954             }
2955           /* For PIE, discard space for pc-relative relocs against
2956              symbols which turn out to need copy relocs.  */
2957           else if (bfd_link_executable (info)
2958                    && (h->needs_copy || eh->needs_copy)
2959                    && h->def_dynamic
2960                    && !h->def_regular)
2961             {
2962               struct elf_dyn_relocs **pp;
2963
2964               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2965                 {
2966                   if (p->pc_count != 0)
2967                     *pp = p->next;
2968                   else
2969                     pp = &p->next;
2970                 }
2971             }
2972         }
2973     }
2974   else if (ELIMINATE_COPY_RELOCS)
2975     {
2976       /* For the non-shared case, discard space for relocs against
2977          symbols which turn out to need copy relocs or are not
2978          dynamic.  Keep dynamic relocations for run-time function
2979          pointer initialization.  */
2980
2981       if ((!h->non_got_ref
2982            || eh->func_pointer_refcount > 0
2983            || (h->root.type == bfd_link_hash_undefweak
2984                && !resolved_to_zero))
2985           && ((h->def_dynamic
2986                && !h->def_regular)
2987               || (htab->elf.dynamic_sections_created
2988                   && (h->root.type == bfd_link_hash_undefweak
2989                       || h->root.type == bfd_link_hash_undefined))))
2990         {
2991           /* Make sure this symbol is output as a dynamic symbol.
2992              Undefined weak syms won't yet be marked as dynamic.  */
2993           if (h->dynindx == -1
2994               && ! h->forced_local
2995               && ! resolved_to_zero
2996               && h->root.type == bfd_link_hash_undefweak
2997               && ! bfd_elf_link_record_dynamic_symbol (info, h))
2998             return FALSE;
2999
3000           /* If that succeeded, we know we'll be keeping all the
3001              relocs.  */
3002           if (h->dynindx != -1)
3003             goto keep;
3004         }
3005
3006       eh->dyn_relocs = NULL;
3007       eh->func_pointer_refcount = 0;
3008
3009     keep: ;
3010     }
3011
3012   /* Finally, allocate space.  */
3013   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3014     {
3015       asection * sreloc;
3016
3017       sreloc = elf_section_data (p->sec)->sreloc;
3018
3019       BFD_ASSERT (sreloc != NULL);
3020
3021       sreloc->size += p->count * bed->s->sizeof_rela;
3022     }
3023
3024   return TRUE;
3025 }
3026
3027 /* Allocate space in .plt, .got and associated reloc sections for
3028    local dynamic relocs.  */
3029
3030 static bfd_boolean
3031 elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
3032 {
3033   struct elf_link_hash_entry *h
3034     = (struct elf_link_hash_entry *) *slot;
3035
3036   if (h->type != STT_GNU_IFUNC
3037       || !h->def_regular
3038       || !h->ref_regular
3039       || !h->forced_local
3040       || h->root.type != bfd_link_hash_defined)
3041     abort ();
3042
3043   return elf_x86_64_allocate_dynrelocs (h, inf);
3044 }
3045
3046 /* Convert load via the GOT slot to load immediate.  */
3047
3048 static bfd_boolean
3049 elf_x86_64_convert_load (bfd *abfd, asection *sec,
3050                          struct bfd_link_info *link_info)
3051 {
3052   Elf_Internal_Shdr *symtab_hdr;
3053   Elf_Internal_Rela *internal_relocs;
3054   Elf_Internal_Rela *irel, *irelend;
3055   bfd_byte *contents;
3056   struct elf_x86_link_hash_table *htab;
3057   bfd_boolean changed;
3058   bfd_signed_vma *local_got_refcounts;
3059
3060   /* Don't even try to convert non-ELF outputs.  */
3061   if (!is_elf_hash_table (link_info->hash))
3062     return FALSE;
3063
3064   /* Nothing to do if there is no need or no output.  */
3065   if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
3066       || sec->need_convert_load == 0
3067       || bfd_is_abs_section (sec->output_section))
3068     return TRUE;
3069
3070   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3071
3072   /* Load the relocations for this section.  */
3073   internal_relocs = (_bfd_elf_link_read_relocs
3074                      (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3075                       link_info->keep_memory));
3076   if (internal_relocs == NULL)
3077     return FALSE;
3078
3079   changed = FALSE;
3080   htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
3081   local_got_refcounts = elf_local_got_refcounts (abfd);
3082
3083   /* Get the section contents.  */
3084   if (elf_section_data (sec)->this_hdr.contents != NULL)
3085     contents = elf_section_data (sec)->this_hdr.contents;
3086   else
3087     {
3088       if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3089         goto error_return;
3090     }
3091
3092   irelend = internal_relocs + sec->reloc_count;
3093   for (irel = internal_relocs; irel < irelend; irel++)
3094     {
3095       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
3096       unsigned int r_symndx;
3097       struct elf_link_hash_entry *h;
3098       bfd_boolean converted;
3099
3100       if (r_type != R_X86_64_GOTPCRELX
3101           && r_type != R_X86_64_REX_GOTPCRELX
3102           && r_type != R_X86_64_GOTPCREL)
3103         continue;
3104
3105       r_symndx = htab->r_sym (irel->r_info);
3106       if (r_symndx < symtab_hdr->sh_info)
3107         h = _bfd_elf_x86_get_local_sym_hash (htab, sec->owner,
3108                                              (const Elf_Internal_Rela *) irel,
3109                                              FALSE);
3110       else
3111         {
3112           h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
3113           while (h->root.type == bfd_link_hash_indirect
3114                  || h->root.type == bfd_link_hash_warning)
3115             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3116         }
3117
3118       /* STT_GNU_IFUNC must keep GOTPCREL relocations.  */
3119       if (h != NULL && h->type == STT_GNU_IFUNC)
3120         continue;
3121
3122       converted = FALSE;
3123       if (!elf_x86_64_convert_load_reloc (abfd, sec, contents, irel, h,
3124                                           &converted, link_info))
3125         goto error_return;
3126
3127       if (converted)
3128         {
3129           changed = converted;
3130           if (h)
3131             {
3132               if (h->got.refcount > 0)
3133                 h->got.refcount -= 1;
3134             }
3135           else
3136             {
3137               if (local_got_refcounts != NULL
3138                   && local_got_refcounts[r_symndx] > 0)
3139                 local_got_refcounts[r_symndx] -= 1;
3140             }
3141         }
3142     }
3143
3144   if (contents != NULL
3145       && elf_section_data (sec)->this_hdr.contents != contents)
3146     {
3147       if (!changed && !link_info->keep_memory)
3148         free (contents);
3149       else
3150         {
3151           /* Cache the section contents for elf_link_input_bfd.  */
3152           elf_section_data (sec)->this_hdr.contents = contents;
3153         }
3154     }
3155
3156   if (elf_section_data (sec)->relocs != internal_relocs)
3157     {
3158       if (!changed)
3159         free (internal_relocs);
3160       else
3161         elf_section_data (sec)->relocs = internal_relocs;
3162     }
3163
3164   return TRUE;
3165
3166  error_return:
3167   if (contents != NULL
3168       && elf_section_data (sec)->this_hdr.contents != contents)
3169     free (contents);
3170   if (internal_relocs != NULL
3171       && elf_section_data (sec)->relocs != internal_relocs)
3172     free (internal_relocs);
3173   return FALSE;
3174 }
3175
3176 /* Set the sizes of the dynamic sections.  */
3177
3178 static bfd_boolean
3179 elf_x86_64_size_dynamic_sections (bfd *output_bfd,
3180                                   struct bfd_link_info *info)
3181 {
3182   struct elf_x86_link_hash_table *htab;
3183   bfd *dynobj;
3184   asection *s;
3185   bfd_boolean relocs;
3186   bfd *ibfd;
3187   const struct elf_backend_data *bed;
3188
3189   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
3190   if (htab == NULL)
3191     return FALSE;
3192   bed = get_elf_backend_data (output_bfd);
3193
3194   dynobj = htab->elf.dynobj;
3195   if (dynobj == NULL)
3196     abort ();
3197
3198   /* Set up .got offsets for local syms, and space for local dynamic
3199      relocs.  */
3200   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3201     {
3202       bfd_signed_vma *local_got;
3203       bfd_signed_vma *end_local_got;
3204       char *local_tls_type;
3205       bfd_vma *local_tlsdesc_gotent;
3206       bfd_size_type locsymcount;
3207       Elf_Internal_Shdr *symtab_hdr;
3208       asection *srel;
3209
3210       if (! is_x86_64_elf (ibfd))
3211         continue;
3212
3213       for (s = ibfd->sections; s != NULL; s = s->next)
3214         {
3215           struct elf_dyn_relocs *p;
3216
3217           if (!elf_x86_64_convert_load (ibfd, s, info))
3218             return FALSE;
3219
3220           for (p = (struct elf_dyn_relocs *)
3221                     (elf_section_data (s)->local_dynrel);
3222                p != NULL;
3223                p = p->next)
3224             {
3225               if (!bfd_is_abs_section (p->sec)
3226                   && bfd_is_abs_section (p->sec->output_section))
3227                 {
3228                   /* Input section has been discarded, either because
3229                      it is a copy of a linkonce section or due to
3230                      linker script /DISCARD/, so we'll be discarding
3231                      the relocs too.  */
3232                 }
3233               else if (p->count != 0)
3234                 {
3235                   srel = elf_section_data (p->sec)->sreloc;
3236                   srel->size += p->count * bed->s->sizeof_rela;
3237                   if ((p->sec->output_section->flags & SEC_READONLY) != 0
3238                       && (info->flags & DF_TEXTREL) == 0)
3239                     {
3240                       info->flags |= DF_TEXTREL;
3241                       if ((info->warn_shared_textrel && bfd_link_pic (info))
3242                           || info->error_textrel)
3243                         /* xgettext:c-format */
3244                         info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3245                                                 p->sec->owner, p->sec);
3246                     }
3247                 }
3248             }
3249         }
3250
3251       local_got = elf_local_got_refcounts (ibfd);
3252       if (!local_got)
3253         continue;
3254
3255       symtab_hdr = &elf_symtab_hdr (ibfd);
3256       locsymcount = symtab_hdr->sh_info;
3257       end_local_got = local_got + locsymcount;
3258       local_tls_type = elf_x86_local_got_tls_type (ibfd);
3259       local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
3260       s = htab->elf.sgot;
3261       srel = htab->elf.srelgot;
3262       for (; local_got < end_local_got;
3263            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
3264         {
3265           *local_tlsdesc_gotent = (bfd_vma) -1;
3266           if (*local_got > 0)
3267             {
3268               if (GOT_TLS_GDESC_P (*local_tls_type))
3269                 {
3270                   *local_tlsdesc_gotent = htab->elf.sgotplt->size
3271                     - elf_x86_64_compute_jump_table_size (htab);
3272                   htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
3273                   *local_got = (bfd_vma) -2;
3274                 }
3275               if (! GOT_TLS_GDESC_P (*local_tls_type)
3276                   || GOT_TLS_GD_P (*local_tls_type))
3277                 {
3278                   *local_got = s->size;
3279                   s->size += GOT_ENTRY_SIZE;
3280                   if (GOT_TLS_GD_P (*local_tls_type))
3281                     s->size += GOT_ENTRY_SIZE;
3282                 }
3283               if (bfd_link_pic (info)
3284                   || GOT_TLS_GD_ANY_P (*local_tls_type)
3285                   || *local_tls_type == GOT_TLS_IE)
3286                 {
3287                   if (GOT_TLS_GDESC_P (*local_tls_type))
3288                     {
3289                       htab->elf.srelplt->size
3290                         += bed->s->sizeof_rela;
3291                       htab->tlsdesc_plt = (bfd_vma) -1;
3292                     }
3293                   if (! GOT_TLS_GDESC_P (*local_tls_type)
3294                       || GOT_TLS_GD_P (*local_tls_type))
3295                     srel->size += bed->s->sizeof_rela;
3296                 }
3297             }
3298           else
3299             *local_got = (bfd_vma) -1;
3300         }
3301     }
3302
3303   if (htab->tls_ld_or_ldm_got.refcount > 0)
3304     {
3305       /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
3306          relocs.  */
3307       htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
3308       htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
3309       htab->elf.srelgot->size += bed->s->sizeof_rela;
3310     }
3311   else
3312     htab->tls_ld_or_ldm_got.offset = -1;
3313
3314   /* Allocate global sym .plt and .got entries, and space for global
3315      sym dynamic relocs.  */
3316   elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
3317                           info);
3318
3319   /* Allocate .plt and .got entries, and space for local symbols.  */
3320   htab_traverse (htab->loc_hash_table,
3321                  elf_x86_64_allocate_local_dynrelocs,
3322                  info);
3323
3324   /* For every jump slot reserved in the sgotplt, reloc_count is
3325      incremented.  However, when we reserve space for TLS descriptors,
3326      it's not incremented, so in order to compute the space reserved
3327      for them, it suffices to multiply the reloc count by the jump
3328      slot size.
3329
3330      PR ld/13302: We start next_irelative_index at the end of .rela.plt
3331      so that R_X86_64_IRELATIVE entries come last.  */
3332   if (htab->elf.srelplt)
3333     {
3334       htab->sgotplt_jump_table_size
3335         = elf_x86_64_compute_jump_table_size (htab);
3336       htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3337     }
3338   else if (htab->elf.irelplt)
3339     htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
3340
3341   if (htab->tlsdesc_plt)
3342     {
3343       /* If we're not using lazy TLS relocations, don't generate the
3344          PLT and GOT entries they require.  */
3345       if ((info->flags & DF_BIND_NOW))
3346         htab->tlsdesc_plt = 0;
3347       else
3348         {
3349           htab->tlsdesc_got = htab->elf.sgot->size;
3350           htab->elf.sgot->size += GOT_ENTRY_SIZE;
3351           /* Reserve room for the initial entry.
3352              FIXME: we could probably do away with it in this case.  */
3353           if (htab->elf.splt->size == 0)
3354             htab->elf.splt->size = htab->plt.plt_entry_size;
3355           htab->tlsdesc_plt = htab->elf.splt->size;
3356           htab->elf.splt->size += htab->plt.plt_entry_size;
3357         }
3358     }
3359
3360   if (htab->elf.sgotplt)
3361     {
3362       /* Don't allocate .got.plt section if there are no GOT nor PLT
3363          entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
3364       if ((htab->elf.hgot == NULL
3365            || !htab->elf.hgot->ref_regular_nonweak)
3366           && (htab->elf.sgotplt->size
3367               == get_elf_backend_data (output_bfd)->got_header_size)
3368           && (htab->elf.splt == NULL
3369               || htab->elf.splt->size == 0)
3370           && (htab->elf.sgot == NULL
3371               || htab->elf.sgot->size == 0)
3372           && (htab->elf.iplt == NULL
3373               || htab->elf.iplt->size == 0)
3374           && (htab->elf.igotplt == NULL
3375               || htab->elf.igotplt->size == 0))
3376         htab->elf.sgotplt->size = 0;
3377     }
3378
3379   if (_bfd_elf_eh_frame_present (info))
3380     {
3381       if (htab->plt_eh_frame != NULL
3382           && htab->elf.splt != NULL
3383           && htab->elf.splt->size != 0
3384           && !bfd_is_abs_section (htab->elf.splt->output_section))
3385         htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
3386
3387       if (htab->plt_got_eh_frame != NULL
3388           && htab->plt_got != NULL
3389           && htab->plt_got->size != 0
3390           && !bfd_is_abs_section (htab->plt_got->output_section))
3391         htab->plt_got_eh_frame->size
3392           = htab->non_lazy_plt->eh_frame_plt_size;
3393
3394       /* Unwind info for the second PLT and .plt.got sections are
3395          identical.  */
3396       if (htab->plt_second_eh_frame != NULL
3397           && htab->plt_second != NULL
3398           && htab->plt_second->size != 0
3399           && !bfd_is_abs_section (htab->plt_second->output_section))
3400         htab->plt_second_eh_frame->size
3401           = htab->non_lazy_plt->eh_frame_plt_size;
3402     }
3403
3404   /* We now have determined the sizes of the various dynamic sections.
3405      Allocate memory for them.  */
3406   relocs = FALSE;
3407   for (s = dynobj->sections; s != NULL; s = s->next)
3408     {
3409       if ((s->flags & SEC_LINKER_CREATED) == 0)
3410         continue;
3411
3412       if (s == htab->elf.splt
3413           || s == htab->elf.sgot
3414           || s == htab->elf.sgotplt
3415           || s == htab->elf.iplt
3416           || s == htab->elf.igotplt
3417           || s == htab->plt_second
3418           || s == htab->plt_got
3419           || s == htab->plt_eh_frame
3420           || s == htab->plt_got_eh_frame
3421           || s == htab->plt_second_eh_frame
3422           || s == htab->elf.sdynbss
3423           || s == htab->elf.sdynrelro)
3424         {
3425           /* Strip this section if we don't need it; see the
3426              comment below.  */
3427         }
3428       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3429         {
3430           if (s->size != 0 && s != htab->elf.srelplt)
3431             relocs = TRUE;
3432
3433           /* We use the reloc_count field as a counter if we need
3434              to copy relocs into the output file.  */
3435           if (s != htab->elf.srelplt)
3436             s->reloc_count = 0;
3437         }
3438       else
3439         {
3440           /* It's not one of our sections, so don't allocate space.  */
3441           continue;
3442         }
3443
3444       if (s->size == 0)
3445         {
3446           /* If we don't need this section, strip it from the
3447              output file.  This is mostly to handle .rela.bss and
3448              .rela.plt.  We must create both sections in
3449              create_dynamic_sections, because they must be created
3450              before the linker maps input sections to output
3451              sections.  The linker does that before
3452              adjust_dynamic_symbol is called, and it is that
3453              function which decides whether anything needs to go
3454              into these sections.  */
3455
3456           s->flags |= SEC_EXCLUDE;
3457           continue;
3458         }
3459
3460       if ((s->flags & SEC_HAS_CONTENTS) == 0)
3461         continue;
3462
3463       /* Allocate memory for the section contents.  We use bfd_zalloc
3464          here in case unused entries are not reclaimed before the
3465          section's contents are written out.  This should not happen,
3466          but this way if it does, we get a R_X86_64_NONE reloc instead
3467          of garbage.  */
3468       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3469       if (s->contents == NULL)
3470         return FALSE;
3471     }
3472
3473   if (htab->plt_eh_frame != NULL
3474       && htab->plt_eh_frame->contents != NULL)
3475     {
3476       memcpy (htab->plt_eh_frame->contents,
3477               htab->plt.eh_frame_plt, htab->plt_eh_frame->size);
3478       bfd_put_32 (dynobj, htab->elf.splt->size,
3479                   htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3480     }
3481
3482   if (htab->plt_got_eh_frame != NULL
3483       && htab->plt_got_eh_frame->contents != NULL)
3484     {
3485       memcpy (htab->plt_got_eh_frame->contents,
3486               htab->non_lazy_plt->eh_frame_plt,
3487               htab->plt_got_eh_frame->size);
3488       bfd_put_32 (dynobj, htab->plt_got->size,
3489                   (htab->plt_got_eh_frame->contents
3490                    + PLT_FDE_LEN_OFFSET));
3491     }
3492
3493   if (htab->plt_second_eh_frame != NULL
3494       && htab->plt_second_eh_frame->contents != NULL)
3495     {
3496       memcpy (htab->plt_second_eh_frame->contents,
3497               htab->non_lazy_plt->eh_frame_plt,
3498               htab->plt_second_eh_frame->size);
3499       bfd_put_32 (dynobj, htab->plt_second->size,
3500                   (htab->plt_second_eh_frame->contents
3501                    + PLT_FDE_LEN_OFFSET));
3502     }
3503
3504   if (htab->elf.dynamic_sections_created)
3505     {
3506       /* Add some entries to the .dynamic section.  We fill in the
3507          values later, in elf_x86_64_finish_dynamic_sections, but we
3508          must add the entries now so that we get the correct size for
3509          the .dynamic section.  The DT_DEBUG entry is filled in by the
3510          dynamic linker and used by the debugger.  */
3511 #define add_dynamic_entry(TAG, VAL) \
3512   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3513
3514       if (bfd_link_executable (info))
3515         {
3516           if (!add_dynamic_entry (DT_DEBUG, 0))
3517             return FALSE;
3518         }
3519
3520       if (htab->elf.splt->size != 0)
3521         {
3522           /* DT_PLTGOT is used by prelink even if there is no PLT
3523              relocation.  */
3524           if (!add_dynamic_entry (DT_PLTGOT, 0))
3525             return FALSE;
3526         }
3527
3528       if (htab->elf.srelplt->size != 0)
3529         {
3530           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3531               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3532               || !add_dynamic_entry (DT_JMPREL, 0))
3533             return FALSE;
3534         }
3535
3536       if (htab->tlsdesc_plt
3537           && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
3538               || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
3539         return FALSE;
3540
3541       if (relocs)
3542         {
3543           if (!add_dynamic_entry (DT_RELA, 0)
3544               || !add_dynamic_entry (DT_RELASZ, 0)
3545               || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
3546             return FALSE;
3547
3548           /* If any dynamic relocs apply to a read-only section,
3549              then we need a DT_TEXTREL entry.  */
3550           if ((info->flags & DF_TEXTREL) == 0)
3551             elf_link_hash_traverse (&htab->elf,
3552                                     _bfd_x86_elf_readonly_dynrelocs,
3553                                     info);
3554
3555           if ((info->flags & DF_TEXTREL) != 0)
3556             {
3557               if (htab->readonly_dynrelocs_against_ifunc)
3558                 {
3559                   info->callbacks->einfo
3560                     (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3561                   bfd_set_error (bfd_error_bad_value);
3562                   return FALSE;
3563                 }
3564
3565               if (!add_dynamic_entry (DT_TEXTREL, 0))
3566                 return FALSE;
3567             }
3568         }
3569     }
3570 #undef add_dynamic_entry
3571
3572   return TRUE;
3573 }
3574
3575 /* Return the relocation value for @tpoff relocation
3576    if STT_TLS virtual address is ADDRESS.  */
3577
3578 static bfd_vma
3579 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
3580 {
3581   struct elf_link_hash_table *htab = elf_hash_table (info);
3582   const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3583   bfd_vma static_tls_size;
3584
3585   /* If tls_segment is NULL, we should have signalled an error already.  */
3586   if (htab->tls_sec == NULL)
3587     return 0;
3588
3589   /* Consider special static TLS alignment requirements.  */
3590   static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3591   return address - static_tls_size - htab->tls_sec->vma;
3592 }
3593
3594 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
3595    branch?  */
3596
3597 static bfd_boolean
3598 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
3599 {
3600   /* Opcode             Instruction
3601      0xe8               call
3602      0xe9               jump
3603      0x0f 0x8x          conditional jump */
3604   return ((offset > 0
3605            && (contents [offset - 1] == 0xe8
3606                || contents [offset - 1] == 0xe9))
3607           || (offset > 1
3608               && contents [offset - 2] == 0x0f
3609               && (contents [offset - 1] & 0xf0) == 0x80));
3610 }
3611
3612 /* Relocate an x86_64 ELF section.  */
3613
3614 static bfd_boolean
3615 elf_x86_64_relocate_section (bfd *output_bfd,
3616                              struct bfd_link_info *info,
3617                              bfd *input_bfd,
3618                              asection *input_section,
3619                              bfd_byte *contents,
3620                              Elf_Internal_Rela *relocs,
3621                              Elf_Internal_Sym *local_syms,
3622                              asection **local_sections)
3623 {
3624   struct elf_x86_link_hash_table *htab;
3625   Elf_Internal_Shdr *symtab_hdr;
3626   struct elf_link_hash_entry **sym_hashes;
3627   bfd_vma *local_got_offsets;
3628   bfd_vma *local_tlsdesc_gotents;
3629   Elf_Internal_Rela *rel;
3630   Elf_Internal_Rela *wrel;
3631   Elf_Internal_Rela *relend;
3632   unsigned int plt_entry_size;
3633
3634   BFD_ASSERT (is_x86_64_elf (input_bfd));
3635
3636   /* Skip if check_relocs failed.  */
3637   if (input_section->check_relocs_failed)
3638     return FALSE;
3639
3640   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
3641   if (htab == NULL)
3642     return FALSE;
3643   plt_entry_size = htab->plt.plt_entry_size;
3644   symtab_hdr = &elf_symtab_hdr (input_bfd);
3645   sym_hashes = elf_sym_hashes (input_bfd);
3646   local_got_offsets = elf_local_got_offsets (input_bfd);
3647   local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
3648
3649   _bfd_x86_elf_set_tls_module_base (info);
3650
3651   rel = wrel = relocs;
3652   relend = relocs + input_section->reloc_count;
3653   for (; rel < relend; wrel++, rel++)
3654     {
3655       unsigned int r_type;
3656       reloc_howto_type *howto;
3657       unsigned long r_symndx;
3658       struct elf_link_hash_entry *h;
3659       struct elf_x86_link_hash_entry *eh;
3660       Elf_Internal_Sym *sym;
3661       asection *sec;
3662       bfd_vma off, offplt, plt_offset;
3663       bfd_vma relocation;
3664       bfd_boolean unresolved_reloc;
3665       bfd_reloc_status_type r;
3666       int tls_type;
3667       asection *base_got, *resolved_plt;
3668       bfd_vma st_size;
3669       bfd_boolean resolved_to_zero;
3670       bfd_boolean relative_reloc;
3671
3672       r_type = ELF32_R_TYPE (rel->r_info);
3673       if (r_type == (int) R_X86_64_GNU_VTINHERIT
3674           || r_type == (int) R_X86_64_GNU_VTENTRY)
3675         {
3676           if (wrel != rel)
3677             *wrel = *rel;
3678           continue;
3679         }
3680
3681       if (r_type >= (int) R_X86_64_standard)
3682         return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
3683
3684       if (r_type != (int) R_X86_64_32
3685           || ABI_64_P (output_bfd))
3686         howto = x86_64_elf_howto_table + r_type;
3687       else
3688         howto = (x86_64_elf_howto_table
3689                  + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
3690       r_symndx = htab->r_sym (rel->r_info);
3691       h = NULL;
3692       sym = NULL;
3693       sec = NULL;
3694       unresolved_reloc = FALSE;
3695       if (r_symndx < symtab_hdr->sh_info)
3696         {
3697           sym = local_syms + r_symndx;
3698           sec = local_sections[r_symndx];
3699
3700           relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
3701                                                 &sec, rel);
3702           st_size = sym->st_size;
3703
3704           /* Relocate against local STT_GNU_IFUNC symbol.  */
3705           if (!bfd_link_relocatable (info)
3706               && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3707             {
3708               h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
3709                                                    rel, FALSE);
3710               if (h == NULL)
3711                 abort ();
3712
3713               /* Set STT_GNU_IFUNC symbol value.  */
3714               h->root.u.def.value = sym->st_value;
3715               h->root.u.def.section = sec;
3716             }
3717         }
3718       else
3719         {
3720           bfd_boolean warned ATTRIBUTE_UNUSED;
3721           bfd_boolean ignored ATTRIBUTE_UNUSED;
3722
3723           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3724                                    r_symndx, symtab_hdr, sym_hashes,
3725                                    h, sec, relocation,
3726                                    unresolved_reloc, warned, ignored);
3727           st_size = h->size;
3728         }
3729
3730       if (sec != NULL && discarded_section (sec))
3731         {
3732           _bfd_clear_contents (howto, input_bfd, input_section,
3733                                contents + rel->r_offset);
3734           wrel->r_offset = rel->r_offset;
3735           wrel->r_info = 0;
3736           wrel->r_addend = 0;
3737
3738           /* For ld -r, remove relocations in debug sections against
3739              sections defined in discarded sections.  Not done for
3740              eh_frame editing code expects to be present.  */
3741            if (bfd_link_relocatable (info)
3742                && (input_section->flags & SEC_DEBUGGING))
3743              wrel--;
3744
3745           continue;
3746         }
3747
3748       if (bfd_link_relocatable (info))
3749         {
3750           if (wrel != rel)
3751             *wrel = *rel;
3752           continue;
3753         }
3754
3755       if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
3756         {
3757           if (r_type == R_X86_64_64)
3758             {
3759               /* For x32, treat R_X86_64_64 like R_X86_64_32 and
3760                  zero-extend it to 64bit if addend is zero.  */
3761               r_type = R_X86_64_32;
3762               memset (contents + rel->r_offset + 4, 0, 4);
3763             }
3764           else if (r_type == R_X86_64_SIZE64)
3765             {
3766               /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
3767                  zero-extend it to 64bit if addend is zero.  */
3768               r_type = R_X86_64_SIZE32;
3769               memset (contents + rel->r_offset + 4, 0, 4);
3770             }
3771         }
3772
3773       eh = (struct elf_x86_link_hash_entry *) h;
3774
3775       /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3776          it here if it is defined in a non-shared object.  */
3777       if (h != NULL
3778           && h->type == STT_GNU_IFUNC
3779           && h->def_regular)
3780         {
3781           bfd_vma plt_index;
3782           const char *name;
3783
3784           if ((input_section->flags & SEC_ALLOC) == 0)
3785             {
3786               /* Dynamic relocs are not propagated for SEC_DEBUGGING
3787                  sections because such sections are not SEC_ALLOC and
3788                  thus ld.so will not process them.  */
3789               if ((input_section->flags & SEC_DEBUGGING) != 0)
3790                 continue;
3791               abort ();
3792             }
3793
3794           switch (r_type)
3795             {
3796             default:
3797               break;
3798
3799             case R_X86_64_GOTPCREL:
3800             case R_X86_64_GOTPCRELX:
3801             case R_X86_64_REX_GOTPCRELX:
3802             case R_X86_64_GOTPCREL64:
3803               base_got = htab->elf.sgot;
3804               off = h->got.offset;
3805
3806               if (base_got == NULL)
3807                 abort ();
3808
3809               if (off == (bfd_vma) -1)
3810                 {
3811                   /* We can't use h->got.offset here to save state, or
3812                      even just remember the offset, as finish_dynamic_symbol
3813                      would use that as offset into .got.  */
3814
3815                   if (h->plt.offset == (bfd_vma) -1)
3816                     abort ();
3817
3818                   if (htab->elf.splt != NULL)
3819                     {
3820                       plt_index = (h->plt.offset / plt_entry_size
3821                                    - htab->plt.has_plt0);
3822                       off = (plt_index + 3) * GOT_ENTRY_SIZE;
3823                       base_got = htab->elf.sgotplt;
3824                     }
3825                   else
3826                     {
3827                       plt_index = h->plt.offset / plt_entry_size;
3828                       off = plt_index * GOT_ENTRY_SIZE;
3829                       base_got = htab->elf.igotplt;
3830                     }
3831
3832                   if (h->dynindx == -1
3833                       || h->forced_local
3834                       || info->symbolic)
3835                     {
3836                       /* This references the local defitionion.  We must
3837                          initialize this entry in the global offset table.
3838                          Since the offset must always be a multiple of 8,
3839                          we use the least significant bit to record
3840                          whether we have initialized it already.
3841
3842                          When doing a dynamic link, we create a .rela.got
3843                          relocation entry to initialize the value.  This
3844                          is done in the finish_dynamic_symbol routine.   */
3845                       if ((off & 1) != 0)
3846                         off &= ~1;
3847                       else
3848                         {
3849                           bfd_put_64 (output_bfd, relocation,
3850                                       base_got->contents + off);
3851                           /* Note that this is harmless for the GOTPLT64
3852                              case, as -1 | 1 still is -1.  */
3853                           h->got.offset |= 1;
3854                         }
3855                     }
3856                 }
3857
3858               relocation = (base_got->output_section->vma
3859                             + base_got->output_offset + off);
3860
3861               goto do_relocation;
3862             }
3863
3864           if (h->plt.offset == (bfd_vma) -1)
3865             {
3866               /* Handle static pointers of STT_GNU_IFUNC symbols.  */
3867               if (r_type == htab->pointer_r_type
3868                   && (input_section->flags & SEC_CODE) == 0)
3869                 goto do_ifunc_pointer;
3870               goto bad_ifunc_reloc;
3871             }
3872
3873           /* STT_GNU_IFUNC symbol must go through PLT.  */
3874           if (htab->elf.splt != NULL)
3875             {
3876               if (htab->plt_second != NULL)
3877                 {
3878                   resolved_plt = htab->plt_second;
3879                   plt_offset = eh->plt_second.offset;
3880                 }
3881               else
3882                 {
3883                   resolved_plt = htab->elf.splt;
3884                   plt_offset =  h->plt.offset;
3885                 }
3886             }
3887           else
3888             {
3889               resolved_plt = htab->elf.iplt;
3890               plt_offset =  h->plt.offset;
3891             }
3892
3893           relocation = (resolved_plt->output_section->vma
3894                         + resolved_plt->output_offset + plt_offset);
3895
3896           switch (r_type)
3897             {
3898             default:
3899 bad_ifunc_reloc:
3900               if (h->root.root.string)
3901                 name = h->root.root.string;
3902               else
3903                 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3904                                          NULL);
3905               _bfd_error_handler
3906                 /* xgettext:c-format */
3907                 (_("%B: relocation %s against STT_GNU_IFUNC "
3908                    "symbol `%s' isn't supported"), input_bfd,
3909                  howto->name, name);
3910               bfd_set_error (bfd_error_bad_value);
3911               return FALSE;
3912
3913             case R_X86_64_32S:
3914               if (bfd_link_pic (info))
3915                 abort ();
3916               goto do_relocation;
3917
3918             case R_X86_64_32:
3919               if (ABI_64_P (output_bfd))
3920                 goto do_relocation;
3921               /* FALLTHROUGH */
3922             case R_X86_64_64:
3923 do_ifunc_pointer:
3924               if (rel->r_addend != 0)
3925                 {
3926                   if (h->root.root.string)
3927                     name = h->root.root.string;
3928                   else
3929                     name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3930                                              sym, NULL);
3931                   _bfd_error_handler
3932                     /* xgettext:c-format */
3933                     (_("%B: relocation %s against STT_GNU_IFUNC "
3934                        "symbol `%s' has non-zero addend: %Ld"),
3935                      input_bfd, howto->name, name, rel->r_addend);
3936                   bfd_set_error (bfd_error_bad_value);
3937                   return FALSE;
3938                 }
3939
3940               /* Generate dynamic relcoation only when there is a
3941                  non-GOT reference in a shared object or there is no
3942                  PLT.  */
3943               if ((bfd_link_pic (info) && h->non_got_ref)
3944                   || h->plt.offset == (bfd_vma) -1)
3945                 {
3946                   Elf_Internal_Rela outrel;
3947                   asection *sreloc;
3948
3949                   /* Need a dynamic relocation to get the real function
3950                      address.  */
3951                   outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3952                                                              info,
3953                                                              input_section,
3954                                                              rel->r_offset);
3955                   if (outrel.r_offset == (bfd_vma) -1
3956                       || outrel.r_offset == (bfd_vma) -2)
3957                     abort ();
3958
3959                   outrel.r_offset += (input_section->output_section->vma
3960                                       + input_section->output_offset);
3961
3962                   if (h->dynindx == -1
3963                       || h->forced_local
3964                       || bfd_link_executable (info))
3965                     {
3966                       info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3967                                               h->root.root.string,
3968                                               h->root.u.def.section->owner);
3969
3970                       /* This symbol is resolved locally.  */
3971                       outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3972                       outrel.r_addend = (h->root.u.def.value
3973                                          + h->root.u.def.section->output_section->vma
3974                                          + h->root.u.def.section->output_offset);
3975                     }
3976                   else
3977                     {
3978                       outrel.r_info = htab->r_info (h->dynindx, r_type);
3979                       outrel.r_addend = 0;
3980                     }
3981
3982                   /* Dynamic relocations are stored in
3983                      1. .rela.ifunc section in PIC object.
3984                      2. .rela.got section in dynamic executable.
3985                      3. .rela.iplt section in static executable.  */
3986                   if (bfd_link_pic (info))
3987                     sreloc = htab->elf.irelifunc;
3988                   else if (htab->elf.splt != NULL)
3989                     sreloc = htab->elf.srelgot;
3990                   else
3991                     sreloc = htab->elf.irelplt;
3992                   elf_append_rela (output_bfd, sreloc, &outrel);
3993
3994                   /* If this reloc is against an external symbol, we
3995                      do not want to fiddle with the addend.  Otherwise,
3996                      we need to include the symbol value so that it
3997                      becomes an addend for the dynamic reloc.  For an
3998                      internal symbol, we have updated addend.  */
3999                   continue;
4000                 }
4001               /* FALLTHROUGH */
4002             case R_X86_64_PC32:
4003             case R_X86_64_PC32_BND:
4004             case R_X86_64_PC64:
4005             case R_X86_64_PLT32:
4006             case R_X86_64_PLT32_BND:
4007               goto do_relocation;
4008             }
4009         }
4010
4011       resolved_to_zero = (eh != NULL
4012                           && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4013                                                               X86_64_ELF_DATA,
4014                                                               eh->has_got_reloc,
4015                                                               eh));
4016
4017       /* When generating a shared object, the relocations handled here are
4018          copied into the output file to be resolved at run time.  */
4019       switch (r_type)
4020         {
4021         case R_X86_64_GOT32:
4022         case R_X86_64_GOT64:
4023           /* Relocation is to the entry for this symbol in the global
4024              offset table.  */
4025         case R_X86_64_GOTPCREL:
4026         case R_X86_64_GOTPCRELX:
4027         case R_X86_64_REX_GOTPCRELX:
4028         case R_X86_64_GOTPCREL64:
4029           /* Use global offset table entry as symbol value.  */
4030         case R_X86_64_GOTPLT64:
4031           /* This is obsolete and treated the same as GOT64.  */
4032           base_got = htab->elf.sgot;
4033
4034           if (htab->elf.sgot == NULL)
4035             abort ();
4036
4037           relative_reloc = FALSE;
4038           if (h != NULL)
4039             {
4040               bfd_boolean dyn;
4041
4042               off = h->got.offset;
4043               if (h->needs_plt
4044                   && h->plt.offset != (bfd_vma)-1
4045                   && off == (bfd_vma)-1)
4046                 {
4047                   /* We can't use h->got.offset here to save
4048                      state, or even just remember the offset, as
4049                      finish_dynamic_symbol would use that as offset into
4050                      .got.  */
4051                   bfd_vma plt_index = (h->plt.offset / plt_entry_size
4052                                        - htab->plt.has_plt0);
4053                   off = (plt_index + 3) * GOT_ENTRY_SIZE;
4054                   base_got = htab->elf.sgotplt;
4055                 }
4056
4057               dyn = htab->elf.dynamic_sections_created;
4058
4059               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
4060                   || (bfd_link_pic (info)
4061                       && SYMBOL_REFERENCES_LOCAL (info, h))
4062                   || (ELF_ST_VISIBILITY (h->other)
4063                       && h->root.type == bfd_link_hash_undefweak))
4064                 {
4065                   /* This is actually a static link, or it is a -Bsymbolic
4066                      link and the symbol is defined locally, or the symbol
4067                      was forced to be local because of a version file.  We
4068                      must initialize this entry in the global offset table.
4069                      Since the offset must always be a multiple of 8, we
4070                      use the least significant bit to record whether we
4071                      have initialized it already.
4072
4073                      When doing a dynamic link, we create a .rela.got
4074                      relocation entry to initialize the value.  This is
4075                      done in the finish_dynamic_symbol routine.  */
4076                   if ((off & 1) != 0)
4077                     off &= ~1;
4078                   else
4079                     {
4080                       bfd_put_64 (output_bfd, relocation,
4081                                   base_got->contents + off);
4082                       /* Note that this is harmless for the GOTPLT64 case,
4083                          as -1 | 1 still is -1.  */
4084                       h->got.offset |= 1;
4085
4086                       if (h->dynindx == -1
4087                           && !h->forced_local
4088                           && h->root.type != bfd_link_hash_undefweak
4089                           && bfd_link_pic (info))
4090                         {
4091                           /* If this symbol isn't dynamic in PIC,
4092                              generate R_X86_64_RELATIVE here.  */
4093                           eh->no_finish_dynamic_symbol = 1;
4094                           relative_reloc = TRUE;
4095                         }
4096                     }
4097                 }
4098               else
4099                 unresolved_reloc = FALSE;
4100             }
4101           else
4102             {
4103               if (local_got_offsets == NULL)
4104                 abort ();
4105
4106               off = local_got_offsets[r_symndx];
4107
4108               /* The offset must always be a multiple of 8.  We use
4109                  the least significant bit to record whether we have
4110                  already generated the necessary reloc.  */
4111               if ((off & 1) != 0)
4112                 off &= ~1;
4113               else
4114                 {
4115                   bfd_put_64 (output_bfd, relocation,
4116                               base_got->contents + off);
4117                   local_got_offsets[r_symndx] |= 1;
4118
4119                   if (bfd_link_pic (info))
4120                     relative_reloc = TRUE;
4121                 }
4122             }
4123
4124           if (relative_reloc)
4125             {
4126               asection *s;
4127               Elf_Internal_Rela outrel;
4128
4129               /* We need to generate a R_X86_64_RELATIVE reloc
4130                  for the dynamic linker.  */
4131               s = htab->elf.srelgot;
4132               if (s == NULL)
4133                 abort ();
4134
4135               outrel.r_offset = (base_got->output_section->vma
4136                                  + base_got->output_offset
4137                                  + off);
4138               outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4139               outrel.r_addend = relocation;
4140               elf_append_rela (output_bfd, s, &outrel);
4141             }
4142
4143           if (off >= (bfd_vma) -2)
4144             abort ();
4145
4146           relocation = base_got->output_section->vma
4147                        + base_got->output_offset + off;
4148           if (r_type != R_X86_64_GOTPCREL
4149               && r_type != R_X86_64_GOTPCRELX
4150               && r_type != R_X86_64_REX_GOTPCRELX
4151               && r_type != R_X86_64_GOTPCREL64)
4152             relocation -= htab->elf.sgotplt->output_section->vma
4153                           - htab->elf.sgotplt->output_offset;
4154
4155           break;
4156
4157         case R_X86_64_GOTOFF64:
4158           /* Relocation is relative to the start of the global offset
4159              table.  */
4160
4161           /* Check to make sure it isn't a protected function or data
4162              symbol for shared library since it may not be local when
4163              used as function address or with copy relocation.  We also
4164              need to make sure that a symbol is referenced locally.  */
4165           if (bfd_link_pic (info) && h)
4166             {
4167               if (!h->def_regular)
4168                 {
4169                   const char *v;
4170
4171                   switch (ELF_ST_VISIBILITY (h->other))
4172                     {
4173                     case STV_HIDDEN:
4174                       v = _("hidden symbol");
4175                       break;
4176                     case STV_INTERNAL:
4177                       v = _("internal symbol");
4178                       break;
4179                     case STV_PROTECTED:
4180                       v = _("protected symbol");
4181                       break;
4182                     default:
4183                       v = _("symbol");
4184                       break;
4185                     }
4186
4187                   _bfd_error_handler
4188                     /* xgettext:c-format */
4189                     (_("%B: relocation R_X86_64_GOTOFF64 against undefined %s"
4190                        " `%s' can not be used when making a shared object"),
4191                      input_bfd, v, h->root.root.string);
4192                   bfd_set_error (bfd_error_bad_value);
4193                   return FALSE;
4194                 }
4195               else if (!bfd_link_executable (info)
4196                        && !SYMBOL_REFERENCES_LOCAL (info, h)
4197                        && (h->type == STT_FUNC
4198                            || h->type == STT_OBJECT)
4199                        && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4200                 {
4201                   _bfd_error_handler
4202               /* xgettext:c-format */
4203                     (_("%B: relocation R_X86_64_GOTOFF64 against protected %s"
4204                        " `%s' can not be used when making a shared object"),
4205                      input_bfd,
4206                      h->type == STT_FUNC ? "function" : "data",
4207                      h->root.root.string);
4208                   bfd_set_error (bfd_error_bad_value);
4209               return FALSE;
4210                 }
4211             }
4212
4213           /* Note that sgot is not involved in this
4214              calculation.  We always want the start of .got.plt.  If we
4215              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4216              permitted by the ABI, we might have to change this
4217              calculation.  */
4218           relocation -= htab->elf.sgotplt->output_section->vma
4219                         + htab->elf.sgotplt->output_offset;
4220           break;
4221
4222         case R_X86_64_GOTPC32:
4223         case R_X86_64_GOTPC64:
4224           /* Use global offset table as symbol value.  */
4225           relocation = htab->elf.sgotplt->output_section->vma
4226                        + htab->elf.sgotplt->output_offset;
4227           unresolved_reloc = FALSE;
4228           break;
4229
4230         case R_X86_64_PLTOFF64:
4231           /* Relocation is PLT entry relative to GOT.  For local
4232              symbols it's the symbol itself relative to GOT.  */
4233           if (h != NULL
4234               /* See PLT32 handling.  */
4235               && (h->plt.offset != (bfd_vma) -1
4236                   || eh->plt_got.offset != (bfd_vma) -1)
4237               && htab->elf.splt != NULL)
4238             {
4239               if (eh->plt_got.offset != (bfd_vma) -1)
4240                 {
4241                   /* Use the GOT PLT.  */
4242                   resolved_plt = htab->plt_got;
4243                   plt_offset = eh->plt_got.offset;
4244                 }
4245               else if (htab->plt_second != NULL)
4246                 {
4247                   resolved_plt = htab->plt_second;
4248                   plt_offset = eh->plt_second.offset;
4249                 }
4250               else
4251                 {
4252                   resolved_plt = htab->elf.splt;
4253                   plt_offset = h->plt.offset;
4254                 }
4255
4256               relocation = (resolved_plt->output_section->vma
4257                             + resolved_plt->output_offset
4258                             + plt_offset);
4259               unresolved_reloc = FALSE;
4260             }
4261
4262           relocation -= htab->elf.sgotplt->output_section->vma
4263                         + htab->elf.sgotplt->output_offset;
4264           break;
4265
4266         case R_X86_64_PLT32:
4267         case R_X86_64_PLT32_BND:
4268           /* Relocation is to the entry for this symbol in the
4269              procedure linkage table.  */
4270
4271           /* Resolve a PLT32 reloc against a local symbol directly,
4272              without using the procedure linkage table.  */
4273           if (h == NULL)
4274             break;
4275
4276           if ((h->plt.offset == (bfd_vma) -1
4277                && eh->plt_got.offset == (bfd_vma) -1)
4278               || htab->elf.splt == NULL)
4279             {
4280               /* We didn't make a PLT entry for this symbol.  This
4281                  happens when statically linking PIC code, or when
4282                  using -Bsymbolic.  */
4283               break;
4284             }
4285
4286           if (h->plt.offset != (bfd_vma) -1)
4287             {
4288               if (htab->plt_second != NULL)
4289                 {
4290                   resolved_plt = htab->plt_second;
4291                   plt_offset = eh->plt_second.offset;
4292                 }
4293               else
4294                 {
4295                   resolved_plt = htab->elf.splt;
4296                   plt_offset = h->plt.offset;
4297                 }
4298             }
4299           else
4300             {
4301               /* Use the GOT PLT.  */
4302               resolved_plt = htab->plt_got;
4303               plt_offset = eh->plt_got.offset;
4304             }
4305
4306           relocation = (resolved_plt->output_section->vma
4307                         + resolved_plt->output_offset
4308                         + plt_offset);
4309           unresolved_reloc = FALSE;
4310           break;
4311
4312         case R_X86_64_SIZE32:
4313         case R_X86_64_SIZE64:
4314           /* Set to symbol size.  */
4315           relocation = st_size;
4316           goto direct;
4317
4318         case R_X86_64_PC8:
4319         case R_X86_64_PC16:
4320         case R_X86_64_PC32:
4321         case R_X86_64_PC32_BND:
4322           /* Don't complain about -fPIC if the symbol is undefined when
4323              building executable unless it is unresolved weak symbol or
4324              -z nocopyreloc is used.  */
4325           if ((input_section->flags & SEC_ALLOC) != 0
4326               && (input_section->flags & SEC_READONLY) != 0
4327               && h != NULL
4328               && ((bfd_link_executable (info)
4329                    && ((h->root.type == bfd_link_hash_undefweak
4330                         && !resolved_to_zero)
4331                        || ((info->nocopyreloc
4332                             || (eh->def_protected
4333                                 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
4334                            && h->def_dynamic
4335                            && !(h->root.u.def.section->flags & SEC_CODE))))
4336                   || bfd_link_dll (info)))
4337             {
4338               bfd_boolean fail = FALSE;
4339               bfd_boolean branch
4340                 = ((r_type == R_X86_64_PC32
4341                     || r_type == R_X86_64_PC32_BND)
4342                    && is_32bit_relative_branch (contents, rel->r_offset));
4343
4344               if (SYMBOL_REFERENCES_LOCAL (info, h))
4345                 {
4346                   /* Symbol is referenced locally.  Make sure it is
4347                      defined locally or for a branch.  */
4348                   fail = (!(h->def_regular || ELF_COMMON_DEF_P (h))
4349                           && !branch);
4350                 }
4351               else if (!(bfd_link_pie (info)
4352                          && (h->needs_copy || eh->needs_copy)))
4353                 {
4354                   /* Symbol doesn't need copy reloc and isn't referenced
4355                      locally.  We only allow branch to symbol with
4356                      non-default visibility. */
4357                   fail = (!branch
4358                           || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
4359                 }
4360
4361               if (fail)
4362                 return elf_x86_64_need_pic (info, input_bfd, input_section,
4363                                             h, NULL, NULL, howto);
4364             }
4365           /* Fall through.  */
4366
4367         case R_X86_64_8:
4368         case R_X86_64_16:
4369         case R_X86_64_32:
4370         case R_X86_64_PC64:
4371         case R_X86_64_64:
4372           /* FIXME: The ABI says the linker should make sure the value is
4373              the same when it's zeroextended to 64 bit.  */
4374
4375 direct:
4376           if ((input_section->flags & SEC_ALLOC) == 0)
4377             break;
4378
4379            /* Don't copy a pc-relative relocation into the output file
4380               if the symbol needs copy reloc or the symbol is undefined
4381               when building executable.  Copy dynamic function pointer
4382               relocations.  Don't generate dynamic relocations against
4383               resolved undefined weak symbols in PIE.  */
4384           if ((bfd_link_pic (info)
4385                && !(bfd_link_pie (info)
4386                     && h != NULL
4387                     && (h->needs_copy
4388                         || eh->needs_copy
4389                         || h->root.type == bfd_link_hash_undefined)
4390                     && (IS_X86_64_PCREL_TYPE (r_type)
4391                         || r_type == R_X86_64_SIZE32
4392                         || r_type == R_X86_64_SIZE64))
4393                && (h == NULL
4394                    || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4395                         && !resolved_to_zero)
4396                        || h->root.type != bfd_link_hash_undefweak))
4397                && ((! IS_X86_64_PCREL_TYPE (r_type)
4398                       && r_type != R_X86_64_SIZE32
4399                       && r_type != R_X86_64_SIZE64)
4400                    || ! SYMBOL_CALLS_LOCAL (info, h)))
4401               || (ELIMINATE_COPY_RELOCS
4402                   && !bfd_link_pic (info)
4403                   && h != NULL
4404                   && h->dynindx != -1
4405                   && (!h->non_got_ref
4406                       || eh->func_pointer_refcount > 0
4407                       || (h->root.type == bfd_link_hash_undefweak
4408                           && !resolved_to_zero))
4409                   && ((h->def_dynamic && !h->def_regular)
4410                       /* Undefined weak symbol is bound locally when
4411                          PIC is false.  */
4412                       || h->root.type == bfd_link_hash_undefined)))
4413             {
4414               Elf_Internal_Rela outrel;
4415               bfd_boolean skip, relocate;
4416               asection *sreloc;
4417
4418               /* When generating a shared object, these relocations
4419                  are copied into the output file to be resolved at run
4420                  time.  */
4421               skip = FALSE;
4422               relocate = FALSE;
4423
4424               outrel.r_offset =
4425                 _bfd_elf_section_offset (output_bfd, info, input_section,
4426                                          rel->r_offset);
4427               if (outrel.r_offset == (bfd_vma) -1)
4428                 skip = TRUE;
4429               else if (outrel.r_offset == (bfd_vma) -2)
4430                 skip = TRUE, relocate = TRUE;
4431
4432               outrel.r_offset += (input_section->output_section->vma
4433                                   + input_section->output_offset);
4434
4435               if (skip)
4436                 memset (&outrel, 0, sizeof outrel);
4437
4438               /* h->dynindx may be -1 if this symbol was marked to
4439                  become local.  */
4440               else if (h != NULL
4441                        && h->dynindx != -1
4442                        && (IS_X86_64_PCREL_TYPE (r_type)
4443                            || !(bfd_link_executable (info)
4444                                 || SYMBOLIC_BIND (info, h))
4445                            || ! h->def_regular))
4446                 {
4447                   outrel.r_info = htab->r_info (h->dynindx, r_type);
4448                   outrel.r_addend = rel->r_addend;
4449                 }
4450               else
4451                 {
4452                   /* This symbol is local, or marked to become local.
4453                      When relocation overflow check is disabled, we
4454                      convert R_X86_64_32 to dynamic R_X86_64_RELATIVE.  */
4455                   if (r_type == htab->pointer_r_type
4456                       || (r_type == R_X86_64_32
4457                           && info->no_reloc_overflow_check))
4458                     {
4459                       relocate = TRUE;
4460                       outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4461                       outrel.r_addend = relocation + rel->r_addend;
4462                     }
4463                   else if (r_type == R_X86_64_64
4464                            && !ABI_64_P (output_bfd))
4465                     {
4466                       relocate = TRUE;
4467                       outrel.r_info = htab->r_info (0,
4468                                                     R_X86_64_RELATIVE64);
4469                       outrel.r_addend = relocation + rel->r_addend;
4470                       /* Check addend overflow.  */
4471                       if ((outrel.r_addend & 0x80000000)
4472                           != (rel->r_addend & 0x80000000))
4473                         {
4474                           const char *name;
4475                           int addend = rel->r_addend;
4476                           if (h && h->root.root.string)
4477                             name = h->root.root.string;
4478                           else
4479                             name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4480                                                      sym, NULL);
4481                           _bfd_error_handler
4482                             /* xgettext:c-format */
4483                             (_("%B: addend %s%#x in relocation %s against "
4484                                "symbol `%s' at %#Lx in section `%A' is "
4485                                "out of range"),
4486                              input_bfd, addend < 0 ? "-" : "", addend,
4487                              howto->name, name, rel->r_offset, input_section);
4488                           bfd_set_error (bfd_error_bad_value);
4489                           return FALSE;
4490                         }
4491                     }
4492                   else
4493                     {
4494                       long sindx;
4495
4496                       if (bfd_is_abs_section (sec))
4497                         sindx = 0;
4498                       else if (sec == NULL || sec->owner == NULL)
4499                         {
4500                           bfd_set_error (bfd_error_bad_value);
4501                           return FALSE;
4502                         }
4503                       else
4504                         {
4505                           asection *osec;
4506
4507                           /* We are turning this relocation into one
4508                              against a section symbol.  It would be
4509                              proper to subtract the symbol's value,
4510                              osec->vma, from the emitted reloc addend,
4511                              but ld.so expects buggy relocs.  */
4512                           osec = sec->output_section;
4513                           sindx = elf_section_data (osec)->dynindx;
4514                           if (sindx == 0)
4515                             {
4516                               asection *oi = htab->elf.text_index_section;
4517                               sindx = elf_section_data (oi)->dynindx;
4518                             }
4519                           BFD_ASSERT (sindx != 0);
4520                         }
4521
4522                       outrel.r_info = htab->r_info (sindx, r_type);
4523                       outrel.r_addend = relocation + rel->r_addend;
4524                     }
4525                 }
4526
4527               sreloc = elf_section_data (input_section)->sreloc;
4528
4529               if (sreloc == NULL || sreloc->contents == NULL)
4530                 {
4531                   r = bfd_reloc_notsupported;
4532                   goto check_relocation_error;
4533                 }
4534
4535               elf_append_rela (output_bfd, sreloc, &outrel);
4536
4537               /* If this reloc is against an external symbol, we do
4538                  not want to fiddle with the addend.  Otherwise, we
4539                  need to include the symbol value so that it becomes
4540                  an addend for the dynamic reloc.  */
4541               if (! relocate)
4542                 continue;
4543             }
4544
4545           break;
4546
4547         case R_X86_64_TLSGD:
4548         case R_X86_64_GOTPC32_TLSDESC:
4549         case R_X86_64_TLSDESC_CALL:
4550         case R_X86_64_GOTTPOFF:
4551           tls_type = GOT_UNKNOWN;
4552           if (h == NULL && local_got_offsets)
4553             tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
4554           else if (h != NULL)
4555             tls_type = elf_x86_hash_entry (h)->tls_type;
4556
4557           if (! elf_x86_64_tls_transition (info, input_bfd,
4558                                            input_section, contents,
4559                                            symtab_hdr, sym_hashes,
4560                                            &r_type, tls_type, rel,
4561                                            relend, h, r_symndx, TRUE))
4562             return FALSE;
4563
4564           if (r_type == R_X86_64_TPOFF32)
4565             {
4566               bfd_vma roff = rel->r_offset;
4567
4568               BFD_ASSERT (! unresolved_reloc);
4569
4570               if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
4571                 {
4572                   /* GD->LE transition.  For 64bit, change
4573                         .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4574                         .word 0x6666; rex64; call __tls_get_addr@PLT
4575                      or
4576                         .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4577                         .byte 0x66; rex64
4578                         call *__tls_get_addr@GOTPCREL(%rip)
4579                         which may be converted to
4580                         addr32 call __tls_get_addr
4581                      into:
4582                         movq %fs:0, %rax
4583                         leaq foo@tpoff(%rax), %rax
4584                      For 32bit, change
4585                         leaq foo@tlsgd(%rip), %rdi
4586                         .word 0x6666; rex64; call __tls_get_addr@PLT
4587                      or
4588                         leaq foo@tlsgd(%rip), %rdi
4589                         .byte 0x66; rex64
4590                         call *__tls_get_addr@GOTPCREL(%rip)
4591                         which may be converted to
4592                         addr32 call __tls_get_addr
4593                      into:
4594                         movl %fs:0, %eax
4595                         leaq foo@tpoff(%rax), %rax
4596                      For largepic, change:
4597                         leaq foo@tlsgd(%rip), %rdi
4598                         movabsq $__tls_get_addr@pltoff, %rax
4599                         addq %r15, %rax
4600                         call *%rax
4601                      into:
4602                         movq %fs:0, %rax
4603                         leaq foo@tpoff(%rax), %rax
4604                         nopw 0x0(%rax,%rax,1)  */
4605                   int largepic = 0;
4606                   if (ABI_64_P (output_bfd))
4607                     {
4608                       if (contents[roff + 5] == 0xb8)
4609                         {
4610                           memcpy (contents + roff - 3,
4611                                   "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
4612                                   "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4613                           largepic = 1;
4614                         }
4615                       else
4616                         memcpy (contents + roff - 4,
4617                                 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4618                                 16);
4619                     }
4620                   else
4621                     memcpy (contents + roff - 3,
4622                             "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4623                             15);
4624                   bfd_put_32 (output_bfd,
4625                               elf_x86_64_tpoff (info, relocation),
4626                               contents + roff + 8 + largepic);
4627                   /* Skip R_X86_64_PC32, R_X86_64_PLT32,
4628                      R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64.  */
4629                   rel++;
4630                   wrel++;
4631                   continue;
4632                 }
4633               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
4634                 {
4635                   /* GDesc -> LE transition.
4636                      It's originally something like:
4637                      leaq x@tlsdesc(%rip), %rax
4638
4639                      Change it to:
4640                      movl $x@tpoff, %rax.  */
4641
4642                   unsigned int val, type;
4643
4644                   type = bfd_get_8 (input_bfd, contents + roff - 3);
4645                   val = bfd_get_8 (input_bfd, contents + roff - 1);
4646                   bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
4647                              contents + roff - 3);
4648                   bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
4649                   bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4650                              contents + roff - 1);
4651                   bfd_put_32 (output_bfd,
4652                               elf_x86_64_tpoff (info, relocation),
4653                               contents + roff);
4654                   continue;
4655                 }
4656               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
4657                 {
4658                   /* GDesc -> LE transition.
4659                      It's originally:
4660                      call *(%rax)
4661                      Turn it into:
4662                      xchg %ax,%ax.  */
4663                   bfd_put_8 (output_bfd, 0x66, contents + roff);
4664                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4665                   continue;
4666                 }
4667               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
4668                 {
4669                   /* IE->LE transition:
4670                      For 64bit, originally it can be one of:
4671                      movq foo@gottpoff(%rip), %reg
4672                      addq foo@gottpoff(%rip), %reg
4673                      We change it into:
4674                      movq $foo, %reg
4675                      leaq foo(%reg), %reg
4676                      addq $foo, %reg.
4677                      For 32bit, originally it can be one of:
4678                      movq foo@gottpoff(%rip), %reg
4679                      addl foo@gottpoff(%rip), %reg
4680                      We change it into:
4681                      movq $foo, %reg
4682                      leal foo(%reg), %reg
4683                      addl $foo, %reg. */
4684
4685                   unsigned int val, type, reg;
4686
4687                   if (roff >= 3)
4688                     val = bfd_get_8 (input_bfd, contents + roff - 3);
4689                   else
4690                     val = 0;
4691                   type = bfd_get_8 (input_bfd, contents + roff - 2);
4692                   reg = bfd_get_8 (input_bfd, contents + roff - 1);
4693                   reg >>= 3;
4694                   if (type == 0x8b)
4695                     {
4696                       /* movq */
4697                       if (val == 0x4c)
4698                         bfd_put_8 (output_bfd, 0x49,
4699                                    contents + roff - 3);
4700                       else if (!ABI_64_P (output_bfd) && val == 0x44)
4701                         bfd_put_8 (output_bfd, 0x41,
4702                                    contents + roff - 3);
4703                       bfd_put_8 (output_bfd, 0xc7,
4704                                  contents + roff - 2);
4705                       bfd_put_8 (output_bfd, 0xc0 | reg,
4706                                  contents + roff - 1);
4707                     }
4708                   else if (reg == 4)
4709                     {
4710                       /* addq/addl -> addq/addl - addressing with %rsp/%r12
4711                          is special  */
4712                       if (val == 0x4c)
4713                         bfd_put_8 (output_bfd, 0x49,
4714                                    contents + roff - 3);
4715                       else if (!ABI_64_P (output_bfd) && val == 0x44)
4716                         bfd_put_8 (output_bfd, 0x41,
4717                                    contents + roff - 3);
4718                       bfd_put_8 (output_bfd, 0x81,
4719                                  contents + roff - 2);
4720                       bfd_put_8 (output_bfd, 0xc0 | reg,
4721                                  contents + roff - 1);
4722                     }
4723                   else
4724                     {
4725                       /* addq/addl -> leaq/leal */
4726                       if (val == 0x4c)
4727                         bfd_put_8 (output_bfd, 0x4d,
4728                                    contents + roff - 3);
4729                       else if (!ABI_64_P (output_bfd) && val == 0x44)
4730                         bfd_put_8 (output_bfd, 0x45,
4731                                    contents + roff - 3);
4732                       bfd_put_8 (output_bfd, 0x8d,
4733                                  contents + roff - 2);
4734                       bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
4735                                  contents + roff - 1);
4736                     }
4737                   bfd_put_32 (output_bfd,
4738                               elf_x86_64_tpoff (info, relocation),
4739                               contents + roff);
4740                   continue;
4741                 }
4742               else
4743                 BFD_ASSERT (FALSE);
4744             }
4745
4746           if (htab->elf.sgot == NULL)
4747             abort ();
4748
4749           if (h != NULL)
4750             {
4751               off = h->got.offset;
4752               offplt = elf_x86_hash_entry (h)->tlsdesc_got;
4753             }
4754           else
4755             {
4756               if (local_got_offsets == NULL)
4757                 abort ();
4758
4759               off = local_got_offsets[r_symndx];
4760               offplt = local_tlsdesc_gotents[r_symndx];
4761             }
4762
4763           if ((off & 1) != 0)
4764             off &= ~1;
4765           else
4766             {
4767               Elf_Internal_Rela outrel;
4768               int dr_type, indx;
4769               asection *sreloc;
4770
4771               if (htab->elf.srelgot == NULL)
4772                 abort ();
4773
4774               indx = h && h->dynindx != -1 ? h->dynindx : 0;
4775
4776               if (GOT_TLS_GDESC_P (tls_type))
4777                 {
4778                   outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
4779                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
4780                               + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4781                   outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4782                                      + htab->elf.sgotplt->output_offset
4783                                      + offplt
4784                                      + htab->sgotplt_jump_table_size);
4785                   sreloc = htab->elf.srelplt;
4786                   if (indx == 0)
4787                     outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4788                   else
4789                     outrel.r_addend = 0;
4790                   elf_append_rela (output_bfd, sreloc, &outrel);
4791                 }
4792
4793               sreloc = htab->elf.srelgot;
4794
4795               outrel.r_offset = (htab->elf.sgot->output_section->vma
4796                                  + htab->elf.sgot->output_offset + off);
4797
4798               if (GOT_TLS_GD_P (tls_type))
4799                 dr_type = R_X86_64_DTPMOD64;
4800               else if (GOT_TLS_GDESC_P (tls_type))
4801                 goto dr_done;
4802               else
4803                 dr_type = R_X86_64_TPOFF64;
4804
4805               bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
4806               outrel.r_addend = 0;
4807               if ((dr_type == R_X86_64_TPOFF64
4808                    || dr_type == R_X86_64_TLSDESC) && indx == 0)
4809                 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4810               outrel.r_info = htab->r_info (indx, dr_type);
4811
4812               elf_append_rela (output_bfd, sreloc, &outrel);
4813
4814               if (GOT_TLS_GD_P (tls_type))
4815                 {
4816                   if (indx == 0)
4817                     {
4818                       BFD_ASSERT (! unresolved_reloc);
4819                       bfd_put_64 (output_bfd,
4820                                   relocation - _bfd_x86_elf_dtpoff_base (info),
4821                                   htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4822                     }
4823                   else
4824                     {
4825                       bfd_put_64 (output_bfd, 0,
4826                                   htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4827                       outrel.r_info = htab->r_info (indx,
4828                                                     R_X86_64_DTPOFF64);
4829                       outrel.r_offset += GOT_ENTRY_SIZE;
4830                       elf_append_rela (output_bfd, sreloc,
4831                                                 &outrel);
4832                     }
4833                 }
4834
4835             dr_done:
4836               if (h != NULL)
4837                 h->got.offset |= 1;
4838               else
4839                 local_got_offsets[r_symndx] |= 1;
4840             }
4841
4842           if (off >= (bfd_vma) -2
4843               && ! GOT_TLS_GDESC_P (tls_type))
4844             abort ();
4845           if (r_type == ELF32_R_TYPE (rel->r_info))
4846             {
4847               if (r_type == R_X86_64_GOTPC32_TLSDESC
4848                   || r_type == R_X86_64_TLSDESC_CALL)
4849                 relocation = htab->elf.sgotplt->output_section->vma
4850                   + htab->elf.sgotplt->output_offset
4851                   + offplt + htab->sgotplt_jump_table_size;
4852               else
4853                 relocation = htab->elf.sgot->output_section->vma
4854                   + htab->elf.sgot->output_offset + off;
4855               unresolved_reloc = FALSE;
4856             }
4857           else
4858             {
4859               bfd_vma roff = rel->r_offset;
4860
4861               if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
4862                 {
4863                   /* GD->IE transition.  For 64bit, change
4864                         .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4865                         .word 0x6666; rex64; call __tls_get_addr@PLT
4866                      or
4867                         .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4868                         .byte 0x66; rex64
4869                         call *__tls_get_addr@GOTPCREL(%rip
4870                         which may be converted to
4871                         addr32 call __tls_get_addr
4872                      into:
4873                         movq %fs:0, %rax
4874                         addq foo@gottpoff(%rip), %rax
4875                      For 32bit, change
4876                         leaq foo@tlsgd(%rip), %rdi
4877                         .word 0x6666; rex64; call __tls_get_addr@PLT
4878                      or
4879                         leaq foo@tlsgd(%rip), %rdi
4880                         .byte 0x66; rex64;
4881                         call *__tls_get_addr@GOTPCREL(%rip)
4882                         which may be converted to
4883                         addr32 call __tls_get_addr
4884                      into:
4885                         movl %fs:0, %eax
4886                         addq foo@gottpoff(%rip), %rax
4887                      For largepic, change:
4888                         leaq foo@tlsgd(%rip), %rdi
4889                         movabsq $__tls_get_addr@pltoff, %rax
4890                         addq %r15, %rax
4891                         call *%rax
4892                      into:
4893                         movq %fs:0, %rax
4894                         addq foo@gottpoff(%rax), %rax
4895                         nopw 0x0(%rax,%rax,1)  */
4896                   int largepic = 0;
4897                   if (ABI_64_P (output_bfd))
4898                     {
4899                       if (contents[roff + 5] == 0xb8)
4900                         {
4901                           memcpy (contents + roff - 3,
4902                                   "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
4903                                   "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4904                           largepic = 1;
4905                         }
4906                       else
4907                         memcpy (contents + roff - 4,
4908                                 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4909                                 16);
4910                     }
4911                   else
4912                     memcpy (contents + roff - 3,
4913                             "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4914                             15);
4915
4916                   relocation = (htab->elf.sgot->output_section->vma
4917                                 + htab->elf.sgot->output_offset + off
4918                                 - roff
4919                                 - largepic
4920                                 - input_section->output_section->vma
4921                                 - input_section->output_offset
4922                                 - 12);
4923                   bfd_put_32 (output_bfd, relocation,
4924                               contents + roff + 8 + largepic);
4925                   /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64.  */
4926                   rel++;
4927                   wrel++;
4928                   continue;
4929                 }
4930               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
4931                 {
4932                   /* GDesc -> IE transition.
4933                      It's originally something like:
4934                      leaq x@tlsdesc(%rip), %rax
4935
4936                      Change it to:
4937                      movq x@gottpoff(%rip), %rax # before xchg %ax,%ax.  */
4938
4939                   /* Now modify the instruction as appropriate. To
4940                      turn a leaq into a movq in the form we use it, it
4941                      suffices to change the second byte from 0x8d to
4942                      0x8b.  */
4943                   bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4944
4945                   bfd_put_32 (output_bfd,
4946                               htab->elf.sgot->output_section->vma
4947                               + htab->elf.sgot->output_offset + off
4948                               - rel->r_offset
4949                               - input_section->output_section->vma
4950                               - input_section->output_offset
4951                               - 4,
4952                               contents + roff);
4953                   continue;
4954                 }
4955               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
4956                 {
4957                   /* GDesc -> IE transition.
4958                      It's originally:
4959                      call *(%rax)
4960
4961                      Change it to:
4962                      xchg %ax, %ax.  */
4963
4964                   bfd_put_8 (output_bfd, 0x66, contents + roff);
4965                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4966                   continue;
4967                 }
4968               else
4969                 BFD_ASSERT (FALSE);
4970             }
4971           break;
4972
4973         case R_X86_64_TLSLD:
4974           if (! elf_x86_64_tls_transition (info, input_bfd,
4975                                            input_section, contents,
4976                                            symtab_hdr, sym_hashes,
4977                                            &r_type, GOT_UNKNOWN, rel,
4978                                            relend, h, r_symndx, TRUE))
4979             return FALSE;
4980
4981           if (r_type != R_X86_64_TLSLD)
4982             {
4983               /* LD->LE transition:
4984                         leaq foo@tlsld(%rip), %rdi
4985                         call __tls_get_addr@PLT
4986                  For 64bit, we change it into:
4987                         .word 0x6666; .byte 0x66; movq %fs:0, %rax
4988                  For 32bit, we change it into:
4989                         nopl 0x0(%rax); movl %fs:0, %eax
4990                  Or
4991                         leaq foo@tlsld(%rip), %rdi;
4992                         call *__tls_get_addr@GOTPCREL(%rip)
4993                         which may be converted to
4994                         addr32 call __tls_get_addr
4995                  For 64bit, we change it into:
4996                         .word 0x6666; .word 0x6666; movq %fs:0, %rax
4997                  For 32bit, we change it into:
4998                         nopw 0x0(%rax); movl %fs:0, %eax
4999                  For largepic, change:
5000                         leaq foo@tlsgd(%rip), %rdi
5001                         movabsq $__tls_get_addr@pltoff, %rax
5002                         addq %rbx, %rax
5003                         call *%rax
5004                  into
5005                         data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
5006                         movq %fs:0, %eax  */
5007
5008               BFD_ASSERT (r_type == R_X86_64_TPOFF32);
5009               if (ABI_64_P (output_bfd))
5010                 {
5011                   if (contents[rel->r_offset + 5] == 0xb8)
5012                     memcpy (contents + rel->r_offset - 3,
5013                             "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
5014                             "\x64\x48\x8b\x04\x25\0\0\0", 22);
5015                   else if (contents[rel->r_offset + 4] == 0xff
5016                            || contents[rel->r_offset + 4] == 0x67)
5017                     memcpy (contents + rel->r_offset - 3,
5018                             "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
5019                             13);
5020                   else
5021                     memcpy (contents + rel->r_offset - 3,
5022                             "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
5023                 }
5024               else
5025                 {
5026                   if (contents[rel->r_offset + 4] == 0xff)
5027                     memcpy (contents + rel->r_offset - 3,
5028                             "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
5029                             13);
5030                   else
5031                     memcpy (contents + rel->r_offset - 3,
5032                             "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
5033                 }
5034               /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
5035                  and R_X86_64_PLTOFF64.  */
5036               rel++;
5037               wrel++;
5038               continue;
5039             }
5040
5041           if (htab->elf.sgot == NULL)
5042             abort ();
5043
5044           off = htab->tls_ld_or_ldm_got.offset;
5045           if (off & 1)
5046             off &= ~1;
5047           else
5048             {
5049               Elf_Internal_Rela outrel;
5050
5051               if (htab->elf.srelgot == NULL)
5052                 abort ();
5053
5054               outrel.r_offset = (htab->elf.sgot->output_section->vma
5055                                  + htab->elf.sgot->output_offset + off);
5056
5057               bfd_put_64 (output_bfd, 0,
5058                           htab->elf.sgot->contents + off);
5059               bfd_put_64 (output_bfd, 0,
5060                           htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5061               outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
5062               outrel.r_addend = 0;
5063               elf_append_rela (output_bfd, htab->elf.srelgot,
5064                                         &outrel);
5065               htab->tls_ld_or_ldm_got.offset |= 1;
5066             }
5067           relocation = htab->elf.sgot->output_section->vma
5068                        + htab->elf.sgot->output_offset + off;
5069           unresolved_reloc = FALSE;
5070           break;
5071
5072         case R_X86_64_DTPOFF32:
5073           if (!bfd_link_executable (info)
5074               || (input_section->flags & SEC_CODE) == 0)
5075             relocation -= _bfd_x86_elf_dtpoff_base (info);
5076           else
5077             relocation = elf_x86_64_tpoff (info, relocation);
5078           break;
5079
5080         case R_X86_64_TPOFF32:
5081         case R_X86_64_TPOFF64:
5082           BFD_ASSERT (bfd_link_executable (info));
5083           relocation = elf_x86_64_tpoff (info, relocation);
5084           break;
5085
5086         case R_X86_64_DTPOFF64:
5087           BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
5088           relocation -= _bfd_x86_elf_dtpoff_base (info);
5089           break;
5090
5091         default:
5092           break;
5093         }
5094
5095       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5096          because such sections are not SEC_ALLOC and thus ld.so will
5097          not process them.  */
5098       if (unresolved_reloc
5099           && !((input_section->flags & SEC_DEBUGGING) != 0
5100                && h->def_dynamic)
5101           && _bfd_elf_section_offset (output_bfd, info, input_section,
5102                                       rel->r_offset) != (bfd_vma) -1)
5103         {
5104           switch (r_type)
5105             {
5106             case R_X86_64_32S:
5107               sec = h->root.u.def.section;
5108               if ((info->nocopyreloc
5109                    || (eh->def_protected
5110                        && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
5111                   && !(h->root.u.def.section->flags & SEC_CODE))
5112                 return elf_x86_64_need_pic (info, input_bfd, input_section,
5113                                             h, NULL, NULL, howto);
5114               /* Fall through.  */
5115
5116             default:
5117               _bfd_error_handler
5118                 /* xgettext:c-format */
5119                 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
5120                  input_bfd,
5121                  input_section,
5122                  rel->r_offset,
5123                  howto->name,
5124                  h->root.root.string);
5125               return FALSE;
5126             }
5127         }
5128
5129 do_relocation:
5130       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5131                                     contents, rel->r_offset,
5132                                     relocation, rel->r_addend);
5133
5134 check_relocation_error:
5135       if (r != bfd_reloc_ok)
5136         {
5137           const char *name;
5138
5139           if (h != NULL)
5140             name = h->root.root.string;
5141           else
5142             {
5143               name = bfd_elf_string_from_elf_section (input_bfd,
5144                                                       symtab_hdr->sh_link,
5145                                                       sym->st_name);
5146               if (name == NULL)
5147                 return FALSE;
5148               if (*name == '\0')
5149                 name = bfd_section_name (input_bfd, sec);
5150             }
5151
5152           if (r == bfd_reloc_overflow)
5153             (*info->callbacks->reloc_overflow)
5154               (info, (h ? &h->root : NULL), name, howto->name,
5155                (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5156           else
5157             {
5158               _bfd_error_handler
5159                 /* xgettext:c-format */
5160                 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
5161                  input_bfd, input_section,
5162                  rel->r_offset, name, (int) r);
5163               return FALSE;
5164             }
5165         }
5166
5167       if (wrel != rel)
5168         *wrel = *rel;
5169     }
5170
5171   if (wrel != rel)
5172     {
5173       Elf_Internal_Shdr *rel_hdr;
5174       size_t deleted = rel - wrel;
5175
5176       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5177       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5178       if (rel_hdr->sh_size == 0)
5179         {
5180           /* It is too late to remove an empty reloc section.  Leave
5181              one NONE reloc.
5182              ??? What is wrong with an empty section???  */
5183           rel_hdr->sh_size = rel_hdr->sh_entsize;
5184           deleted -= 1;
5185         }
5186       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5187       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5188       input_section->reloc_count -= deleted;
5189     }
5190
5191   return TRUE;
5192 }
5193
5194 /* Finish up dynamic symbol handling.  We set the contents of various
5195    dynamic sections here.  */
5196
5197 static bfd_boolean
5198 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
5199                                   struct bfd_link_info *info,
5200                                   struct elf_link_hash_entry *h,
5201                                   Elf_Internal_Sym *sym)
5202 {
5203   struct elf_x86_link_hash_table *htab;
5204   bfd_boolean use_plt_second;
5205   struct elf_x86_link_hash_entry *eh;
5206   bfd_boolean local_undefweak;
5207
5208   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
5209   if (htab == NULL)
5210     return FALSE;
5211
5212   /* Use the second PLT section only if there is .plt section.  */
5213   use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
5214
5215   eh = (struct elf_x86_link_hash_entry *) h;
5216   if (eh->no_finish_dynamic_symbol)
5217     abort ();
5218
5219   /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5220      resolved undefined weak symbols in executable so that their
5221      references have value 0 at run-time.  */
5222   local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5223                                                      X86_64_ELF_DATA,
5224                                                      eh->has_got_reloc,
5225                                                      eh);
5226
5227   if (h->plt.offset != (bfd_vma) -1)
5228     {
5229       bfd_vma plt_index;
5230       bfd_vma got_offset, plt_offset;
5231       Elf_Internal_Rela rela;
5232       bfd_byte *loc;
5233       asection *plt, *gotplt, *relplt, *resolved_plt;
5234       const struct elf_backend_data *bed;
5235       bfd_vma plt_got_pcrel_offset;
5236
5237       /* When building a static executable, use .iplt, .igot.plt and
5238          .rela.iplt sections for STT_GNU_IFUNC symbols.  */
5239       if (htab->elf.splt != NULL)
5240         {
5241           plt = htab->elf.splt;
5242           gotplt = htab->elf.sgotplt;
5243           relplt = htab->elf.srelplt;
5244         }
5245       else
5246         {
5247           plt = htab->elf.iplt;
5248           gotplt = htab->elf.igotplt;
5249           relplt = htab->elf.irelplt;
5250         }
5251
5252       /* This symbol has an entry in the procedure linkage table.  Set
5253          it up.  */
5254       if ((h->dynindx == -1
5255            && !local_undefweak
5256            && !((h->forced_local || bfd_link_executable (info))
5257                 && h->def_regular
5258                 && h->type == STT_GNU_IFUNC))
5259           || plt == NULL
5260           || gotplt == NULL
5261           || relplt == NULL)
5262         abort ();
5263
5264       /* Get the index in the procedure linkage table which
5265          corresponds to this symbol.  This is the index of this symbol
5266          in all the symbols for which we are making plt entries.  The
5267          first entry in the procedure linkage table is reserved.
5268
5269          Get the offset into the .got table of the entry that
5270          corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
5271          bytes. The first three are reserved for the dynamic linker.
5272
5273          For static executables, we don't reserve anything.  */
5274
5275       if (plt == htab->elf.splt)
5276         {
5277           got_offset = (h->plt.offset / htab->plt.plt_entry_size
5278                         - htab->plt.has_plt0);
5279           got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
5280         }
5281       else
5282         {
5283           got_offset = h->plt.offset / htab->plt.plt_entry_size;
5284           got_offset = got_offset * GOT_ENTRY_SIZE;
5285         }
5286
5287       /* Fill in the entry in the procedure linkage table.  */
5288       memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
5289               htab->plt.plt_entry_size);
5290       if (use_plt_second)
5291         {
5292           memcpy (htab->plt_second->contents + eh->plt_second.offset,
5293                   htab->non_lazy_plt->plt_entry,
5294                   htab->non_lazy_plt->plt_entry_size);
5295
5296           resolved_plt = htab->plt_second;
5297           plt_offset = eh->plt_second.offset;
5298         }
5299       else
5300         {
5301           resolved_plt = plt;
5302           plt_offset = h->plt.offset;
5303         }
5304
5305       /* Insert the relocation positions of the plt section.  */
5306
5307       /* Put offset the PC-relative instruction referring to the GOT entry,
5308          subtracting the size of that instruction.  */
5309       plt_got_pcrel_offset = (gotplt->output_section->vma
5310                               + gotplt->output_offset
5311                               + got_offset
5312                               - resolved_plt->output_section->vma
5313                               - resolved_plt->output_offset
5314                               - plt_offset
5315                               - htab->plt.plt_got_insn_size);
5316
5317       /* Check PC-relative offset overflow in PLT entry.  */
5318       if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
5319         /* xgettext:c-format */
5320         info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
5321                                 output_bfd, h->root.root.string);
5322
5323       bfd_put_32 (output_bfd, plt_got_pcrel_offset,
5324                   (resolved_plt->contents + plt_offset
5325                    + htab->plt.plt_got_offset));
5326
5327       /* Fill in the entry in the global offset table, initially this
5328          points to the second part of the PLT entry.  Leave the entry
5329          as zero for undefined weak symbol in PIE.  No PLT relocation
5330          against undefined weak symbol in PIE.  */
5331       if (!local_undefweak)
5332         {
5333           if (htab->plt.has_plt0)
5334             bfd_put_64 (output_bfd, (plt->output_section->vma
5335                                      + plt->output_offset
5336                                      + h->plt.offset
5337                                      + htab->lazy_plt->plt_lazy_offset),
5338                         gotplt->contents + got_offset);
5339
5340           /* Fill in the entry in the .rela.plt section.  */
5341           rela.r_offset = (gotplt->output_section->vma
5342                            + gotplt->output_offset
5343                            + got_offset);
5344           if (h->dynindx == -1
5345               || ((bfd_link_executable (info)
5346                    || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5347                   && h->def_regular
5348                   && h->type == STT_GNU_IFUNC))
5349             {
5350               info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5351                                       h->root.root.string,
5352                                       h->root.u.def.section->owner);
5353
5354               /* If an STT_GNU_IFUNC symbol is locally defined, generate
5355                  R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT.  */
5356               rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
5357               rela.r_addend = (h->root.u.def.value
5358                                + h->root.u.def.section->output_section->vma
5359                                + h->root.u.def.section->output_offset);
5360               /* R_X86_64_IRELATIVE comes last.  */
5361               plt_index = htab->next_irelative_index--;
5362             }
5363           else
5364             {
5365               rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
5366               rela.r_addend = 0;
5367               plt_index = htab->next_jump_slot_index++;
5368             }
5369
5370           /* Don't fill the second and third slots in PLT entry for
5371              static executables nor without PLT0.  */
5372           if (plt == htab->elf.splt && htab->plt.has_plt0)
5373             {
5374               bfd_vma plt0_offset
5375                 = h->plt.offset + htab->lazy_plt->plt_plt_insn_end;
5376
5377               /* Put relocation index.  */
5378               bfd_put_32 (output_bfd, plt_index,
5379                           (plt->contents + h->plt.offset
5380                            + htab->lazy_plt->plt_reloc_offset));
5381
5382               /* Put offset for jmp .PLT0 and check for overflow.  We don't
5383                  check relocation index for overflow since branch displacement
5384                  will overflow first.  */
5385               if (plt0_offset > 0x80000000)
5386                 /* xgettext:c-format */
5387                 info->callbacks->einfo (_("%F%B: branch displacement overflow in PLT entry for `%s'\n"),
5388                                         output_bfd, h->root.root.string);
5389               bfd_put_32 (output_bfd, - plt0_offset,
5390                           (plt->contents + h->plt.offset
5391                            + htab->lazy_plt->plt_plt_offset));
5392             }
5393
5394           bed = get_elf_backend_data (output_bfd);
5395           loc = relplt->contents + plt_index * bed->s->sizeof_rela;
5396           bed->s->swap_reloca_out (output_bfd, &rela, loc);
5397         }
5398     }
5399   else if (eh->plt_got.offset != (bfd_vma) -1)
5400     {
5401       bfd_vma got_offset, plt_offset;
5402       asection *plt, *got;
5403       bfd_boolean got_after_plt;
5404       int32_t got_pcrel_offset;
5405
5406       /* Set the entry in the GOT procedure linkage table.  */
5407       plt = htab->plt_got;
5408       got = htab->elf.sgot;
5409       got_offset = h->got.offset;
5410
5411       if (got_offset == (bfd_vma) -1
5412           || (h->type == STT_GNU_IFUNC && h->def_regular)
5413           || plt == NULL
5414           || got == NULL)
5415         abort ();
5416
5417       /* Use the non-lazy PLT entry template for the GOT PLT since they
5418          are the identical.  */
5419       /* Fill in the entry in the GOT procedure linkage table.  */
5420       plt_offset = eh->plt_got.offset;
5421       memcpy (plt->contents + plt_offset,
5422               htab->non_lazy_plt->plt_entry,
5423               htab->non_lazy_plt->plt_entry_size);
5424
5425       /* Put offset the PC-relative instruction referring to the GOT
5426          entry, subtracting the size of that instruction.  */
5427       got_pcrel_offset = (got->output_section->vma
5428                           + got->output_offset
5429                           + got_offset
5430                           - plt->output_section->vma
5431                           - plt->output_offset
5432                           - plt_offset
5433                           - htab->non_lazy_plt->plt_got_insn_size);
5434
5435       /* Check PC-relative offset overflow in GOT PLT entry.  */
5436       got_after_plt = got->output_section->vma > plt->output_section->vma;
5437       if ((got_after_plt && got_pcrel_offset < 0)
5438           || (!got_after_plt && got_pcrel_offset > 0))
5439         /* xgettext:c-format */
5440         info->callbacks->einfo (_("%F%B: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
5441                                 output_bfd, h->root.root.string);
5442
5443       bfd_put_32 (output_bfd, got_pcrel_offset,
5444                   (plt->contents + plt_offset
5445                    + htab->non_lazy_plt->plt_got_offset));
5446     }
5447
5448   if (!local_undefweak
5449       && !h->def_regular
5450       && (h->plt.offset != (bfd_vma) -1
5451           || eh->plt_got.offset != (bfd_vma) -1))
5452     {
5453       /* Mark the symbol as undefined, rather than as defined in
5454          the .plt section.  Leave the value if there were any
5455          relocations where pointer equality matters (this is a clue
5456          for the dynamic linker, to make function pointer
5457          comparisons work between an application and shared
5458          library), otherwise set it to zero.  If a function is only
5459          called from a binary, there is no need to slow down
5460          shared libraries because of that.  */
5461       sym->st_shndx = SHN_UNDEF;
5462       if (!h->pointer_equality_needed)
5463         sym->st_value = 0;
5464     }
5465
5466   /* Don't generate dynamic GOT relocation against undefined weak
5467      symbol in executable.  */
5468   if (h->got.offset != (bfd_vma) -1
5469       && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
5470       && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
5471       && !local_undefweak)
5472     {
5473       Elf_Internal_Rela rela;
5474       asection *relgot = htab->elf.srelgot;
5475
5476       /* This symbol has an entry in the global offset table.  Set it
5477          up.  */
5478       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5479         abort ();
5480
5481       rela.r_offset = (htab->elf.sgot->output_section->vma
5482                        + htab->elf.sgot->output_offset
5483                        + (h->got.offset &~ (bfd_vma) 1));
5484
5485       /* If this is a static link, or it is a -Bsymbolic link and the
5486          symbol is defined locally or was forced to be local because
5487          of a version file, we just want to emit a RELATIVE reloc.
5488          The entry in the global offset table will already have been
5489          initialized in the relocate_section function.  */
5490       if (h->def_regular
5491           && h->type == STT_GNU_IFUNC)
5492         {
5493           if (h->plt.offset == (bfd_vma) -1)
5494             {
5495               /* STT_GNU_IFUNC is referenced without PLT.  */
5496               if (htab->elf.splt == NULL)
5497                 {
5498                   /* use .rel[a].iplt section to store .got relocations
5499                      in static executable.  */
5500                   relgot = htab->elf.irelplt;
5501                 }
5502               if (SYMBOL_REFERENCES_LOCAL (info, h))
5503                 {
5504                   info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5505                                           output_bfd,
5506                                           h->root.root.string,
5507                                           h->root.u.def.section->owner);
5508
5509                   rela.r_info = htab->r_info (0,
5510                                               R_X86_64_IRELATIVE);
5511                   rela.r_addend = (h->root.u.def.value
5512                                    + h->root.u.def.section->output_section->vma
5513                                    + h->root.u.def.section->output_offset);
5514                 }
5515               else
5516                 goto do_glob_dat;
5517             }
5518           else if (bfd_link_pic (info))
5519             {
5520               /* Generate R_X86_64_GLOB_DAT.  */
5521               goto do_glob_dat;
5522             }
5523           else
5524             {
5525               asection *plt;
5526               bfd_vma plt_offset;
5527
5528               if (!h->pointer_equality_needed)
5529                 abort ();
5530
5531               /* For non-shared object, we can't use .got.plt, which
5532                  contains the real function addres if we need pointer
5533                  equality.  We load the GOT entry with the PLT entry.  */
5534               if (htab->plt_second != NULL)
5535                 {
5536                   plt = htab->plt_second;
5537                   plt_offset = eh->plt_second.offset;
5538                 }
5539               else
5540                 {
5541                   plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5542                   plt_offset =  h->plt.offset;
5543                 }
5544               bfd_put_64 (output_bfd, (plt->output_section->vma
5545                                        + plt->output_offset
5546                                        + plt_offset),
5547                           htab->elf.sgot->contents + h->got.offset);
5548               return TRUE;
5549             }
5550         }
5551       else if (bfd_link_pic (info)
5552                && SYMBOL_REFERENCES_LOCAL (info, h))
5553         {
5554           if (!h->def_regular)
5555             return FALSE;
5556           BFD_ASSERT((h->got.offset & 1) != 0);
5557           rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
5558           rela.r_addend = (h->root.u.def.value
5559                            + h->root.u.def.section->output_section->vma
5560                            + h->root.u.def.section->output_offset);
5561         }
5562       else
5563         {
5564           BFD_ASSERT((h->got.offset & 1) == 0);
5565 do_glob_dat:
5566           bfd_put_64 (output_bfd, (bfd_vma) 0,
5567                       htab->elf.sgot->contents + h->got.offset);
5568           rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
5569           rela.r_addend = 0;
5570         }
5571
5572       elf_append_rela (output_bfd, relgot, &rela);
5573     }
5574
5575   if (h->needs_copy)
5576     {
5577       Elf_Internal_Rela rela;
5578       asection *s;
5579
5580       /* This symbol needs a copy reloc.  Set it up.  */
5581
5582       if (h->dynindx == -1
5583           || (h->root.type != bfd_link_hash_defined
5584               && h->root.type != bfd_link_hash_defweak)
5585           || htab->elf.srelbss == NULL
5586           || htab->elf.sreldynrelro == NULL)
5587         abort ();
5588
5589       rela.r_offset = (h->root.u.def.value
5590                        + h->root.u.def.section->output_section->vma
5591                        + h->root.u.def.section->output_offset);
5592       rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
5593       rela.r_addend = 0;
5594       if (h->root.u.def.section == htab->elf.sdynrelro)
5595         s = htab->elf.sreldynrelro;
5596       else
5597         s = htab->elf.srelbss;
5598       elf_append_rela (output_bfd, s, &rela);
5599     }
5600
5601   return TRUE;
5602 }
5603
5604 /* Finish up local dynamic symbol handling.  We set the contents of
5605    various dynamic sections here.  */
5606
5607 static bfd_boolean
5608 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
5609 {
5610   struct elf_link_hash_entry *h
5611     = (struct elf_link_hash_entry *) *slot;
5612   struct bfd_link_info *info
5613     = (struct bfd_link_info *) inf;
5614
5615   return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5616                                            info, h, NULL);
5617 }
5618
5619 /* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
5620    here since undefined weak symbol may not be dynamic and may not be
5621    called for elf_x86_64_finish_dynamic_symbol.  */
5622
5623 static bfd_boolean
5624 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5625                                         void *inf)
5626 {
5627   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5628   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5629
5630   if (h->root.type != bfd_link_hash_undefweak
5631       || h->dynindx != -1)
5632     return TRUE;
5633
5634   return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5635                                            info, h, NULL);
5636 }
5637
5638 /* Used to decide how to sort relocs in an optimal manner for the
5639    dynamic linker, before writing them out.  */
5640
5641 static enum elf_reloc_type_class
5642 elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
5643                              const asection *rel_sec ATTRIBUTE_UNUSED,
5644                              const Elf_Internal_Rela *rela)
5645 {
5646   bfd *abfd = info->output_bfd;
5647   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5648   struct elf_x86_link_hash_table *htab
5649     = elf_x86_hash_table (info, X86_64_ELF_DATA);
5650
5651   if (htab->elf.dynsym != NULL
5652       && htab->elf.dynsym->contents != NULL)
5653     {
5654       /* Check relocation against STT_GNU_IFUNC symbol if there are
5655          dynamic symbols.  */
5656       unsigned long r_symndx = htab->r_sym (rela->r_info);
5657       if (r_symndx != STN_UNDEF)
5658         {
5659           Elf_Internal_Sym sym;
5660           if (!bed->s->swap_symbol_in (abfd,
5661                                        (htab->elf.dynsym->contents
5662                                         + r_symndx * bed->s->sizeof_sym),
5663                                        0, &sym))
5664             abort ();
5665
5666           if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5667             return reloc_class_ifunc;
5668         }
5669     }
5670
5671   switch ((int) ELF32_R_TYPE (rela->r_info))
5672     {
5673     case R_X86_64_IRELATIVE:
5674       return reloc_class_ifunc;
5675     case R_X86_64_RELATIVE:
5676     case R_X86_64_RELATIVE64:
5677       return reloc_class_relative;
5678     case R_X86_64_JUMP_SLOT:
5679       return reloc_class_plt;
5680     case R_X86_64_COPY:
5681       return reloc_class_copy;
5682     default:
5683       return reloc_class_normal;
5684     }
5685 }
5686
5687 /* Finish up the dynamic sections.  */
5688
5689 static bfd_boolean
5690 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
5691                                     struct bfd_link_info *info)
5692 {
5693   struct elf_x86_link_hash_table *htab;
5694   bfd *dynobj;
5695   asection *sdyn;
5696
5697   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
5698   if (htab == NULL)
5699     return FALSE;
5700
5701   dynobj = htab->elf.dynobj;
5702   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5703
5704   if (htab->elf.dynamic_sections_created)
5705     {
5706       bfd_byte *dyncon, *dynconend;
5707       const struct elf_backend_data *bed;
5708       bfd_size_type sizeof_dyn;
5709
5710       if (sdyn == NULL || htab->elf.sgot == NULL)
5711         abort ();
5712
5713       bed = get_elf_backend_data (dynobj);
5714       sizeof_dyn = bed->s->sizeof_dyn;
5715       dyncon = sdyn->contents;
5716       dynconend = sdyn->contents + sdyn->size;
5717       for (; dyncon < dynconend; dyncon += sizeof_dyn)
5718         {
5719           Elf_Internal_Dyn dyn;
5720           asection *s;
5721
5722           (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
5723
5724           switch (dyn.d_tag)
5725             {
5726             default:
5727               continue;
5728
5729             case DT_PLTGOT:
5730               s = htab->elf.sgotplt;
5731               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5732               break;
5733
5734             case DT_JMPREL:
5735               dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
5736               break;
5737
5738             case DT_PLTRELSZ:
5739               s = htab->elf.srelplt->output_section;
5740               dyn.d_un.d_val = s->size;
5741               break;
5742
5743             case DT_TLSDESC_PLT:
5744               s = htab->elf.splt;
5745               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
5746                 + htab->tlsdesc_plt;
5747               break;
5748
5749             case DT_TLSDESC_GOT:
5750               s = htab->elf.sgot;
5751               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
5752                 + htab->tlsdesc_got;
5753               break;
5754             }
5755
5756           (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
5757         }
5758
5759       if (htab->elf.splt && htab->elf.splt->size > 0)
5760         {
5761           elf_section_data (htab->elf.splt->output_section)
5762             ->this_hdr.sh_entsize = htab->plt.plt_entry_size;
5763
5764           if (htab->plt.has_plt0)
5765             {
5766               /* Fill in the special first entry in the procedure linkage
5767                  table.  */
5768               memcpy (htab->elf.splt->contents,
5769                       htab->lazy_plt->plt0_entry,
5770                       htab->lazy_plt->plt0_entry_size);
5771               /* Add offset for pushq GOT+8(%rip), since the instruction
5772                  uses 6 bytes subtract this value.  */
5773               bfd_put_32 (output_bfd,
5774                           (htab->elf.sgotplt->output_section->vma
5775                            + htab->elf.sgotplt->output_offset
5776                            + 8
5777                            - htab->elf.splt->output_section->vma
5778                            - htab->elf.splt->output_offset
5779                            - 6),
5780                           (htab->elf.splt->contents
5781                            + htab->lazy_plt->plt0_got1_offset));
5782               /* Add offset for the PC-relative instruction accessing
5783                  GOT+16, subtracting the offset to the end of that
5784                  instruction.  */
5785               bfd_put_32 (output_bfd,
5786                           (htab->elf.sgotplt->output_section->vma
5787                            + htab->elf.sgotplt->output_offset
5788                            + 16
5789                            - htab->elf.splt->output_section->vma
5790                            - htab->elf.splt->output_offset
5791                            - htab->lazy_plt->plt0_got2_insn_end),
5792                           (htab->elf.splt->contents
5793                            + htab->lazy_plt->plt0_got2_offset));
5794
5795               if (htab->tlsdesc_plt)
5796                 {
5797                   bfd_put_64 (output_bfd, (bfd_vma) 0,
5798                               htab->elf.sgot->contents + htab->tlsdesc_got);
5799
5800                   memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
5801                           htab->lazy_plt->plt0_entry,
5802                           htab->lazy_plt->plt0_entry_size);
5803
5804                   /* Add offset for pushq GOT+8(%rip), since the
5805                      instruction uses 6 bytes subtract this value.  */
5806                   bfd_put_32 (output_bfd,
5807                               (htab->elf.sgotplt->output_section->vma
5808                                + htab->elf.sgotplt->output_offset
5809                                + 8
5810                                - htab->elf.splt->output_section->vma
5811                                - htab->elf.splt->output_offset
5812                                - htab->tlsdesc_plt
5813                                - 6),
5814                               (htab->elf.splt->contents
5815                                + htab->tlsdesc_plt
5816                                + htab->lazy_plt->plt0_got1_offset));
5817                   /* Add offset for the PC-relative instruction accessing
5818                      GOT+TDG, where TDG stands for htab->tlsdesc_got,
5819                      subtracting the offset to the end of that
5820                      instruction.  */
5821                   bfd_put_32 (output_bfd,
5822                               (htab->elf.sgot->output_section->vma
5823                                + htab->elf.sgot->output_offset
5824                                + htab->tlsdesc_got
5825                                - htab->elf.splt->output_section->vma
5826                                - htab->elf.splt->output_offset
5827                                - htab->tlsdesc_plt
5828                                - htab->lazy_plt->plt0_got2_insn_end),
5829                               (htab->elf.splt->contents
5830                                + htab->tlsdesc_plt
5831                                + htab->lazy_plt->plt0_got2_offset));
5832                 }
5833             }
5834         }
5835
5836       if (htab->plt_got != NULL && htab->plt_got->size > 0)
5837         elf_section_data (htab->plt_got->output_section)
5838           ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
5839
5840       if (htab->plt_second != NULL && htab->plt_second->size > 0)
5841         elf_section_data (htab->plt_second->output_section)
5842           ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
5843     }
5844
5845   /* GOT is always created in setup_gnu_properties.  But it may not be
5846      needed.  */
5847   if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
5848     {
5849       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
5850         {
5851           _bfd_error_handler
5852             (_("discarded output section: `%A'"), htab->elf.sgotplt);
5853           return FALSE;
5854         }
5855
5856       /* Set the first entry in the global offset table to the address of
5857          the dynamic section.  */
5858       if (sdyn == NULL)
5859         bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
5860       else
5861         bfd_put_64 (output_bfd,
5862                     sdyn->output_section->vma + sdyn->output_offset,
5863                     htab->elf.sgotplt->contents);
5864       /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
5865       bfd_put_64 (output_bfd, (bfd_vma) 0,
5866                   htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
5867       bfd_put_64 (output_bfd, (bfd_vma) 0,
5868                   htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
5869
5870       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
5871         = GOT_ENTRY_SIZE;
5872     }
5873
5874   /* Adjust .eh_frame for .plt section.  */
5875   if (htab->plt_eh_frame != NULL
5876       && htab->plt_eh_frame->contents != NULL)
5877     {
5878       if (htab->elf.splt != NULL
5879           && htab->elf.splt->size != 0
5880           && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
5881           && htab->elf.splt->output_section != NULL
5882           && htab->plt_eh_frame->output_section != NULL)
5883         {
5884           bfd_vma plt_start = htab->elf.splt->output_section->vma;
5885           bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
5886                                    + htab->plt_eh_frame->output_offset
5887                                    + PLT_FDE_START_OFFSET;
5888           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5889                              htab->plt_eh_frame->contents
5890                              + PLT_FDE_START_OFFSET);
5891         }
5892       if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
5893         {
5894           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5895                                                  htab->plt_eh_frame,
5896                                                  htab->plt_eh_frame->contents))
5897             return FALSE;
5898         }
5899     }
5900
5901   /* Adjust .eh_frame for .plt.got section.  */
5902   if (htab->plt_got_eh_frame != NULL
5903       && htab->plt_got_eh_frame->contents != NULL)
5904     {
5905       if (htab->plt_got != NULL
5906           && htab->plt_got->size != 0
5907           && (htab->plt_got->flags & SEC_EXCLUDE) == 0
5908           && htab->plt_got->output_section != NULL
5909           && htab->plt_got_eh_frame->output_section != NULL)
5910         {
5911           bfd_vma plt_start = htab->plt_got->output_section->vma;
5912           bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
5913                                    + htab->plt_got_eh_frame->output_offset
5914                                    + PLT_FDE_START_OFFSET;
5915           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5916                              htab->plt_got_eh_frame->contents
5917                              + PLT_FDE_START_OFFSET);
5918         }
5919       if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
5920         {
5921           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5922                                                  htab->plt_got_eh_frame,
5923                                                  htab->plt_got_eh_frame->contents))
5924             return FALSE;
5925         }
5926     }
5927
5928   /* Adjust .eh_frame for the second PLT section.  */
5929   if (htab->plt_second_eh_frame != NULL
5930       && htab->plt_second_eh_frame->contents != NULL)
5931     {
5932       if (htab->plt_second != NULL
5933           && htab->plt_second->size != 0
5934           && (htab->plt_second->flags & SEC_EXCLUDE) == 0
5935           && htab->plt_second->output_section != NULL
5936           && htab->plt_second_eh_frame->output_section != NULL)
5937         {
5938           bfd_vma plt_start = htab->plt_second->output_section->vma;
5939           bfd_vma eh_frame_start
5940             = (htab->plt_second_eh_frame->output_section->vma
5941                + htab->plt_second_eh_frame->output_offset
5942                + PLT_FDE_START_OFFSET);
5943           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5944                              htab->plt_second_eh_frame->contents
5945                              + PLT_FDE_START_OFFSET);
5946         }
5947       if (htab->plt_second_eh_frame->sec_info_type
5948           == SEC_INFO_TYPE_EH_FRAME)
5949         {
5950           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5951                                                  htab->plt_second_eh_frame,
5952                                                  htab->plt_second_eh_frame->contents))
5953             return FALSE;
5954         }
5955     }
5956
5957   if (htab->elf.sgot && htab->elf.sgot->size > 0)
5958     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
5959       = GOT_ENTRY_SIZE;
5960
5961   /* Fill PLT entries for undefined weak symbols in PIE.  */
5962   if (bfd_link_pie (info))
5963     bfd_hash_traverse (&info->hash->table,
5964                        elf_x86_64_pie_finish_undefweak_symbol,
5965                        info);
5966
5967   return TRUE;
5968 }
5969
5970 /* Fill PLT/GOT entries and allocate dynamic relocations for local
5971    STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
5972    It has to be done before elf_link_sort_relocs is called so that
5973    dynamic relocations are properly sorted.  */
5974
5975 static bfd_boolean
5976 elf_x86_64_output_arch_local_syms
5977   (bfd *output_bfd ATTRIBUTE_UNUSED,
5978    struct bfd_link_info *info,
5979    void *flaginfo ATTRIBUTE_UNUSED,
5980    int (*func) (void *, const char *,
5981                 Elf_Internal_Sym *,
5982                 asection *,
5983                 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
5984 {
5985   struct elf_x86_link_hash_table *htab
5986     = elf_x86_hash_table (info, X86_64_ELF_DATA);
5987   if (htab == NULL)
5988     return FALSE;
5989
5990   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
5991   htab_traverse (htab->loc_hash_table,
5992                  elf_x86_64_finish_local_dynamic_symbol,
5993                  info);
5994
5995   return TRUE;
5996 }
5997
5998 /* Forward declaration.  */
5999 static const struct elf_x86_lazy_plt_layout elf_x86_64_nacl_plt;
6000
6001 /* Similar to _bfd_elf_get_synthetic_symtab.  Support PLTs with all
6002    dynamic relocations.   */
6003
6004 static long
6005 elf_x86_64_get_synthetic_symtab (bfd *abfd,
6006                                  long symcount ATTRIBUTE_UNUSED,
6007                                  asymbol **syms ATTRIBUTE_UNUSED,
6008                                  long dynsymcount,
6009                                  asymbol **dynsyms,
6010                                  asymbol **ret)
6011 {
6012   long count, i, n;
6013   int j;
6014   bfd_byte *plt_contents;
6015   long relsize;
6016   const struct elf_x86_lazy_plt_layout *lazy_plt;
6017   const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
6018   const struct elf_x86_lazy_plt_layout *lazy_bnd_plt;
6019   const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_plt;
6020   const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
6021   const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
6022   asection *plt;
6023   enum elf_x86_plt_type plt_type;
6024   struct elf_x86_plt plts[] =
6025     {
6026       { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
6027       { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
6028       { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
6029       { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
6030       { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
6031     };
6032
6033   *ret = NULL;
6034
6035   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
6036     return 0;
6037
6038   if (dynsymcount <= 0)
6039     return 0;
6040
6041   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
6042   if (relsize <= 0)
6043     return -1;
6044
6045   if (get_elf_x86_64_backend_data (abfd)->os == is_normal)
6046     {
6047       lazy_plt = &elf_x86_64_lazy_plt;
6048       non_lazy_plt = &elf_x86_64_non_lazy_plt;
6049       lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
6050       non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
6051       if (ABI_64_P (abfd))
6052         {
6053           lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
6054           non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
6055         }
6056       else
6057         {
6058           lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
6059           non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
6060         }
6061     }
6062   else
6063     {
6064       lazy_plt = &elf_x86_64_nacl_plt;
6065       non_lazy_plt = NULL;
6066       lazy_bnd_plt = NULL;
6067       non_lazy_bnd_plt = NULL;
6068       lazy_ibt_plt = NULL;
6069       non_lazy_ibt_plt = NULL;
6070     }
6071
6072   count = 0;
6073   for (j = 0; plts[j].name != NULL; j++)
6074     {
6075       plt = bfd_get_section_by_name (abfd, plts[j].name);
6076       if (plt == NULL || plt->size == 0)
6077         continue;
6078
6079       /* Get the PLT section contents.  */
6080       plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6081       if (plt_contents == NULL)
6082         break;
6083       if (!bfd_get_section_contents (abfd, (asection *) plt,
6084                                      plt_contents, 0, plt->size))
6085         {
6086           free (plt_contents);
6087           break;
6088         }
6089
6090       /* Check what kind of PLT it is.  */
6091       plt_type = plt_unknown;
6092       if (plts[j].type == plt_unknown
6093           && (plt->size >= (lazy_plt->plt_entry_size
6094                             + lazy_plt->plt_entry_size)))
6095         {
6096           /* Match lazy PLT first.  Need to check the first two
6097              instructions.   */
6098           if ((memcmp (plt_contents, lazy_plt->plt0_entry,
6099                        lazy_plt->plt0_got1_offset) == 0)
6100               && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
6101                           2) == 0))
6102             plt_type = plt_lazy;
6103           else if (lazy_bnd_plt != NULL
6104                    && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
6105                                lazy_bnd_plt->plt0_got1_offset) == 0)
6106                    && (memcmp (plt_contents + 6,
6107                                lazy_bnd_plt->plt0_entry + 6, 3) == 0))
6108             {
6109               plt_type = plt_lazy | plt_second;
6110               /* The fist entry in the lazy IBT PLT is the same as the
6111                  lazy BND PLT.  */
6112               if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
6113                            lazy_ibt_plt->plt_entry,
6114                            lazy_ibt_plt->plt_got_offset) == 0))
6115                 lazy_plt = lazy_ibt_plt;
6116               else
6117                 lazy_plt = lazy_bnd_plt;
6118             }
6119         }
6120
6121       if (non_lazy_plt != NULL
6122           && (plt_type == plt_unknown || plt_type == plt_non_lazy)
6123           && plt->size >= non_lazy_plt->plt_entry_size)
6124         {
6125           /* Match non-lazy PLT.  */
6126           if (memcmp (plt_contents, non_lazy_plt->plt_entry,
6127                       non_lazy_plt->plt_got_offset) == 0)
6128             plt_type = plt_non_lazy;
6129         }
6130
6131       if (plt_type == plt_unknown || plt_type == plt_second)
6132         {
6133           if (non_lazy_bnd_plt != NULL
6134               && plt->size >= non_lazy_bnd_plt->plt_entry_size
6135               && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
6136                           non_lazy_bnd_plt->plt_got_offset) == 0))
6137             {
6138               /* Match BND PLT.  */
6139               plt_type = plt_second;
6140               non_lazy_plt = non_lazy_bnd_plt;
6141             }
6142           else if (non_lazy_ibt_plt != NULL
6143                    && plt->size >= non_lazy_ibt_plt->plt_entry_size
6144                    && (memcmp (plt_contents,
6145                                non_lazy_ibt_plt->plt_entry,
6146                                non_lazy_ibt_plt->plt_got_offset) == 0))
6147             {
6148               /* Match IBT PLT.  */
6149               plt_type = plt_second;
6150               non_lazy_plt = non_lazy_ibt_plt;
6151             }
6152         }
6153
6154       if (plt_type == plt_unknown)
6155         {
6156           free (plt_contents);
6157           continue;
6158         }
6159
6160       plts[j].sec = plt;
6161       plts[j].type = plt_type;
6162
6163       if ((plt_type & plt_lazy))
6164         {
6165           plts[j].plt_got_offset = lazy_plt->plt_got_offset;
6166           plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
6167           plts[j].plt_entry_size = lazy_plt->plt_entry_size;
6168           /* Skip PLT0 in lazy PLT.  */
6169           i = 1;
6170         }
6171       else
6172         {
6173           plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
6174           plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
6175           plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
6176           i = 0;
6177         }
6178
6179       /* Skip lazy PLT when the second PLT is used.  */
6180       if (plt_type == (plt_lazy | plt_second))
6181         plts[j].count = 0;
6182       else
6183         {
6184           n = plt->size / plts[j].plt_entry_size;
6185           plts[j].count = n;
6186           count += n - i;
6187         }
6188
6189       plts[j].contents = plt_contents;
6190     }
6191
6192   return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
6193                                             (bfd_vma) 0, plts, dynsyms,
6194                                             ret);
6195 }
6196
6197 /* Handle an x86-64 specific section when reading an object file.  This
6198    is called when elfcode.h finds a section with an unknown type.  */
6199
6200 static bfd_boolean
6201 elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
6202                               const char *name, int shindex)
6203 {
6204   if (hdr->sh_type != SHT_X86_64_UNWIND)
6205     return FALSE;
6206
6207   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6208     return FALSE;
6209
6210   return TRUE;
6211 }
6212
6213 /* Hook called by the linker routine which adds symbols from an object
6214    file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
6215    of .bss.  */
6216
6217 static bfd_boolean
6218 elf_x86_64_add_symbol_hook (bfd *abfd,
6219                             struct bfd_link_info *info ATTRIBUTE_UNUSED,
6220                             Elf_Internal_Sym *sym,
6221                             const char **namep ATTRIBUTE_UNUSED,
6222                             flagword *flagsp ATTRIBUTE_UNUSED,
6223                             asection **secp,
6224                             bfd_vma *valp)
6225 {
6226   asection *lcomm;
6227
6228   switch (sym->st_shndx)
6229     {
6230     case SHN_X86_64_LCOMMON:
6231       lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
6232       if (lcomm == NULL)
6233         {
6234           lcomm = bfd_make_section_with_flags (abfd,
6235                                                "LARGE_COMMON",
6236                                                (SEC_ALLOC
6237                                                 | SEC_IS_COMMON
6238                                                 | SEC_LINKER_CREATED));
6239           if (lcomm == NULL)
6240             return FALSE;
6241           elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
6242         }
6243       *secp = lcomm;
6244       *valp = sym->st_size;
6245       return TRUE;
6246     }
6247
6248   return TRUE;
6249 }
6250
6251
6252 /* Given a BFD section, try to locate the corresponding ELF section
6253    index.  */
6254
6255 static bfd_boolean
6256 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6257                                          asection *sec, int *index_return)
6258 {
6259   if (sec == &_bfd_elf_large_com_section)
6260     {
6261       *index_return = SHN_X86_64_LCOMMON;
6262       return TRUE;
6263     }
6264   return FALSE;
6265 }
6266
6267 /* Process a symbol.  */
6268
6269 static void
6270 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
6271                               asymbol *asym)
6272 {
6273   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
6274
6275   switch (elfsym->internal_elf_sym.st_shndx)
6276     {
6277     case SHN_X86_64_LCOMMON:
6278       asym->section = &_bfd_elf_large_com_section;
6279       asym->value = elfsym->internal_elf_sym.st_size;
6280       /* Common symbol doesn't set BSF_GLOBAL.  */
6281       asym->flags &= ~BSF_GLOBAL;
6282       break;
6283     }
6284 }
6285
6286 static bfd_boolean
6287 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
6288 {
6289   return (sym->st_shndx == SHN_COMMON
6290           || sym->st_shndx == SHN_X86_64_LCOMMON);
6291 }
6292
6293 static unsigned int
6294 elf_x86_64_common_section_index (asection *sec)
6295 {
6296   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6297     return SHN_COMMON;
6298   else
6299     return SHN_X86_64_LCOMMON;
6300 }
6301
6302 static asection *
6303 elf_x86_64_common_section (asection *sec)
6304 {
6305   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6306     return bfd_com_section_ptr;
6307   else
6308     return &_bfd_elf_large_com_section;
6309 }
6310
6311 static bfd_boolean
6312 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
6313                          const Elf_Internal_Sym *sym,
6314                          asection **psec,
6315                          bfd_boolean newdef,
6316                          bfd_boolean olddef,
6317                          bfd *oldbfd,
6318                          const asection *oldsec)
6319 {
6320   /* A normal common symbol and a large common symbol result in a
6321      normal common symbol.  We turn the large common symbol into a
6322      normal one.  */
6323   if (!olddef
6324       && h->root.type == bfd_link_hash_common
6325       && !newdef
6326       && bfd_is_com_section (*psec)
6327       && oldsec != *psec)
6328     {
6329       if (sym->st_shndx == SHN_COMMON
6330           && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
6331         {
6332           h->root.u.c.p->section
6333             = bfd_make_section_old_way (oldbfd, "COMMON");
6334           h->root.u.c.p->section->flags = SEC_ALLOC;
6335         }
6336       else if (sym->st_shndx == SHN_X86_64_LCOMMON
6337                && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
6338         *psec = bfd_com_section_ptr;
6339     }
6340
6341   return TRUE;
6342 }
6343
6344 static int
6345 elf_x86_64_additional_program_headers (bfd *abfd,
6346                                        struct bfd_link_info *info ATTRIBUTE_UNUSED)
6347 {
6348   asection *s;
6349   int count = 0;
6350
6351   /* Check to see if we need a large readonly segment.  */
6352   s = bfd_get_section_by_name (abfd, ".lrodata");
6353   if (s && (s->flags & SEC_LOAD))
6354     count++;
6355
6356   /* Check to see if we need a large data segment.  Since .lbss sections
6357      is placed right after the .bss section, there should be no need for
6358      a large data segment just because of .lbss.  */
6359   s = bfd_get_section_by_name (abfd, ".ldata");
6360   if (s && (s->flags & SEC_LOAD))
6361     count++;
6362
6363   return count;
6364 }
6365
6366 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
6367
6368 static bfd_boolean
6369 elf_x86_64_relocs_compatible (const bfd_target *input,
6370                               const bfd_target *output)
6371 {
6372   return ((xvec_get_elf_backend_data (input)->s->elfclass
6373            == xvec_get_elf_backend_data (output)->s->elfclass)
6374           && _bfd_elf_relocs_compatible (input, output));
6375 }
6376
6377 /* Set up x86-64 GNU properties.  Return the first relocatable ELF input
6378    with GNU properties if found.  Otherwise, return NULL.  */
6379
6380 static bfd *
6381 elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
6382 {
6383   struct elf_x86_plt_layout_table plt_layout;
6384
6385   plt_layout.is_vxworks = FALSE;
6386   if (get_elf_x86_64_backend_data (info->output_bfd)->os == is_normal)
6387     {
6388       if (info->bndplt)
6389         {
6390           plt_layout.lazy_plt = &elf_x86_64_lazy_bnd_plt;
6391           plt_layout.non_lazy_plt = &elf_x86_64_non_lazy_bnd_plt;
6392         }
6393       else
6394         {
6395           plt_layout.lazy_plt = &elf_x86_64_lazy_plt;
6396           plt_layout.non_lazy_plt = &elf_x86_64_non_lazy_plt;
6397         }
6398
6399       if (ABI_64_P (info->output_bfd))
6400         {
6401           plt_layout.lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
6402           plt_layout.non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
6403         }
6404       else
6405         {
6406           plt_layout.lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
6407           plt_layout.non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
6408         }
6409       plt_layout.normal_target = TRUE;
6410     }
6411   else
6412     {
6413       plt_layout.lazy_plt = &elf_x86_64_nacl_plt;
6414       plt_layout.non_lazy_plt = NULL;
6415       plt_layout.lazy_ibt_plt = NULL;
6416       plt_layout.non_lazy_ibt_plt = NULL;
6417       plt_layout.normal_target = FALSE;
6418     }
6419
6420   return _bfd_x86_elf_link_setup_gnu_properties (info, &plt_layout);
6421 }
6422
6423 static const struct bfd_elf_special_section
6424 elf_x86_64_special_sections[]=
6425 {
6426   { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6427   { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6428   { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
6429   { STRING_COMMA_LEN (".lbss"),            -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6430   { STRING_COMMA_LEN (".ldata"),           -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6431   { STRING_COMMA_LEN (".lrodata"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6432   { NULL,                       0,          0, 0,            0 }
6433 };
6434
6435 #define TARGET_LITTLE_SYM                   x86_64_elf64_vec
6436 #define TARGET_LITTLE_NAME                  "elf64-x86-64"
6437 #define ELF_ARCH                            bfd_arch_i386
6438 #define ELF_TARGET_ID                       X86_64_ELF_DATA
6439 #define ELF_MACHINE_CODE                    EM_X86_64
6440 #define ELF_MAXPAGESIZE                     0x200000
6441 #define ELF_MINPAGESIZE                     0x1000
6442 #define ELF_COMMONPAGESIZE                  0x1000
6443
6444 #define elf_backend_can_gc_sections         1
6445 #define elf_backend_can_refcount            1
6446 #define elf_backend_want_got_plt            1
6447 #define elf_backend_plt_readonly            1
6448 #define elf_backend_want_plt_sym            0
6449 #define elf_backend_got_header_size         (GOT_ENTRY_SIZE*3)
6450 #define elf_backend_rela_normal             1
6451 #define elf_backend_plt_alignment           4
6452 #define elf_backend_extern_protected_data   1
6453 #define elf_backend_caches_rawsize          1
6454 #define elf_backend_dtrel_excludes_plt      1
6455 #define elf_backend_want_dynrelro           1
6456
6457 #define elf_info_to_howto                   elf_x86_64_info_to_howto
6458
6459 #define bfd_elf64_bfd_reloc_type_lookup     elf_x86_64_reloc_type_lookup
6460 #define bfd_elf64_bfd_reloc_name_lookup \
6461   elf_x86_64_reloc_name_lookup
6462
6463 #define elf_backend_adjust_dynamic_symbol   elf_x86_64_adjust_dynamic_symbol
6464 #define elf_backend_relocs_compatible       elf_x86_64_relocs_compatible
6465 #define elf_backend_check_relocs            elf_x86_64_check_relocs
6466 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
6467 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
6468 #define elf_backend_finish_dynamic_symbol   elf_x86_64_finish_dynamic_symbol
6469 #define elf_backend_output_arch_local_syms  elf_x86_64_output_arch_local_syms
6470 #define elf_backend_gc_mark_hook            elf_x86_64_gc_mark_hook
6471 #define elf_backend_grok_prstatus           elf_x86_64_grok_prstatus
6472 #define elf_backend_grok_psinfo             elf_x86_64_grok_psinfo
6473 #ifdef CORE_HEADER
6474 #define elf_backend_write_core_note         elf_x86_64_write_core_note
6475 #endif
6476 #define elf_backend_reloc_type_class        elf_x86_64_reloc_type_class
6477 #define elf_backend_relocate_section        elf_x86_64_relocate_section
6478 #define elf_backend_size_dynamic_sections   elf_x86_64_size_dynamic_sections
6479 #define elf_backend_init_index_section      _bfd_elf_init_1_index_section
6480 #define elf_backend_object_p                elf64_x86_64_elf_object_p
6481 #define bfd_elf64_mkobject                  elf_x86_64_mkobject
6482 #define bfd_elf64_get_synthetic_symtab      elf_x86_64_get_synthetic_symtab
6483
6484 #define elf_backend_section_from_shdr \
6485         elf_x86_64_section_from_shdr
6486
6487 #define elf_backend_section_from_bfd_section \
6488   elf_x86_64_elf_section_from_bfd_section
6489 #define elf_backend_add_symbol_hook \
6490   elf_x86_64_add_symbol_hook
6491 #define elf_backend_symbol_processing \
6492   elf_x86_64_symbol_processing
6493 #define elf_backend_common_section_index \
6494   elf_x86_64_common_section_index
6495 #define elf_backend_common_section \
6496   elf_x86_64_common_section
6497 #define elf_backend_common_definition \
6498   elf_x86_64_common_definition
6499 #define elf_backend_merge_symbol \
6500   elf_x86_64_merge_symbol
6501 #define elf_backend_special_sections \
6502   elf_x86_64_special_sections
6503 #define elf_backend_additional_program_headers \
6504   elf_x86_64_additional_program_headers
6505 #define elf_backend_setup_gnu_properties \
6506  elf_x86_64_link_setup_gnu_properties
6507
6508 #include "elf64-target.h"
6509
6510 /* CloudABI support.  */
6511
6512 #undef  TARGET_LITTLE_SYM
6513 #define TARGET_LITTLE_SYM                   x86_64_elf64_cloudabi_vec
6514 #undef  TARGET_LITTLE_NAME
6515 #define TARGET_LITTLE_NAME                  "elf64-x86-64-cloudabi"
6516
6517 #undef  ELF_OSABI
6518 #define ELF_OSABI                           ELFOSABI_CLOUDABI
6519
6520 #undef  elf64_bed
6521 #define elf64_bed elf64_x86_64_cloudabi_bed
6522
6523 #include "elf64-target.h"
6524
6525 /* FreeBSD support.  */
6526
6527 #undef  TARGET_LITTLE_SYM
6528 #define TARGET_LITTLE_SYM                   x86_64_elf64_fbsd_vec
6529 #undef  TARGET_LITTLE_NAME
6530 #define TARGET_LITTLE_NAME                  "elf64-x86-64-freebsd"
6531
6532 #undef  ELF_OSABI
6533 #define ELF_OSABI                           ELFOSABI_FREEBSD
6534
6535 #undef  elf64_bed
6536 #define elf64_bed elf64_x86_64_fbsd_bed
6537
6538 #include "elf64-target.h"
6539
6540 /* Solaris 2 support.  */
6541
6542 #undef  TARGET_LITTLE_SYM
6543 #define TARGET_LITTLE_SYM                   x86_64_elf64_sol2_vec
6544 #undef  TARGET_LITTLE_NAME
6545 #define TARGET_LITTLE_NAME                  "elf64-x86-64-sol2"
6546
6547 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
6548    objects won't be recognized.  */
6549 #undef ELF_OSABI
6550
6551 #undef  elf64_bed
6552 #define elf64_bed                           elf64_x86_64_sol2_bed
6553
6554 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
6555    boundary.  */
6556 #undef  elf_backend_static_tls_alignment
6557 #define elf_backend_static_tls_alignment    16
6558
6559 /* The Solaris 2 ABI requires a plt symbol on all platforms.
6560
6561    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6562    File, p.63.  */
6563 #undef  elf_backend_want_plt_sym
6564 #define elf_backend_want_plt_sym            1
6565
6566 #undef  elf_backend_strtab_flags
6567 #define elf_backend_strtab_flags        SHF_STRINGS
6568
6569 static bfd_boolean
6570 elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
6571                                                   bfd *obfd ATTRIBUTE_UNUSED,
6572                                                   const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
6573                                                   Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
6574 {
6575   /* PR 19938: FIXME: Need to add code for setting the sh_info
6576      and sh_link fields of Solaris specific section types.  */
6577   return FALSE;
6578 }
6579
6580 #undef  elf_backend_copy_special_section_fields
6581 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
6582
6583 #include "elf64-target.h"
6584
6585 /* Native Client support.  */
6586
6587 static bfd_boolean
6588 elf64_x86_64_nacl_elf_object_p (bfd *abfd)
6589 {
6590   /* Set the right machine number for a NaCl x86-64 ELF64 file.  */
6591   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
6592   return TRUE;
6593 }
6594
6595 #undef  TARGET_LITTLE_SYM
6596 #define TARGET_LITTLE_SYM               x86_64_elf64_nacl_vec
6597 #undef  TARGET_LITTLE_NAME
6598 #define TARGET_LITTLE_NAME              "elf64-x86-64-nacl"
6599 #undef  elf64_bed
6600 #define elf64_bed                       elf64_x86_64_nacl_bed
6601
6602 #undef  ELF_MAXPAGESIZE
6603 #undef  ELF_MINPAGESIZE
6604 #undef  ELF_COMMONPAGESIZE
6605 #define ELF_MAXPAGESIZE                 0x10000
6606 #define ELF_MINPAGESIZE                 0x10000
6607 #define ELF_COMMONPAGESIZE              0x10000
6608
6609 /* Restore defaults.  */
6610 #undef  ELF_OSABI
6611 #undef  elf_backend_static_tls_alignment
6612 #undef  elf_backend_want_plt_sym
6613 #define elf_backend_want_plt_sym        0
6614 #undef  elf_backend_strtab_flags
6615 #undef  elf_backend_copy_special_section_fields
6616
6617 /* NaCl uses substantially different PLT entries for the same effects.  */
6618
6619 #undef  elf_backend_plt_alignment
6620 #define elf_backend_plt_alignment       5
6621 #define NACL_PLT_ENTRY_SIZE             64
6622 #define NACLMASK                        0xe0 /* 32-byte alignment mask.  */
6623
6624 static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
6625   {
6626     0xff, 0x35, 8, 0, 0, 0,             /* pushq GOT+8(%rip)            */
6627     0x4c, 0x8b, 0x1d, 16, 0, 0, 0,      /* mov GOT+16(%rip), %r11       */
6628     0x41, 0x83, 0xe3, NACLMASK,         /* and $-32, %r11d              */
6629     0x4d, 0x01, 0xfb,                   /* add %r15, %r11               */
6630     0x41, 0xff, 0xe3,                   /* jmpq *%r11                   */
6631
6632     /* 9-byte nop sequence to pad out to the next 32-byte boundary.  */
6633     0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1)     */
6634
6635     /* 32 bytes of nop to pad out to the standard size.  */
6636     0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    /* excess data16 prefixes    */
6637     0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6638     0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    /* excess data16 prefixes    */
6639     0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6640     0x66,                                  /* excess data16 prefix      */
6641     0x90                                   /* nop */
6642   };
6643
6644 static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
6645   {
6646     0x4c, 0x8b, 0x1d, 0, 0, 0, 0,       /* mov name@GOTPCREL(%rip),%r11 */
6647     0x41, 0x83, 0xe3, NACLMASK,         /* and $-32, %r11d              */
6648     0x4d, 0x01, 0xfb,                   /* add %r15, %r11               */
6649     0x41, 0xff, 0xe3,                   /* jmpq *%r11                   */
6650
6651     /* 15-byte nop sequence to pad out to the next 32-byte boundary.  */
6652     0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    /* excess data16 prefixes    */
6653     0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6654
6655     /* Lazy GOT entries point here (32-byte aligned).  */
6656     0x68,                 /* pushq immediate */
6657     0, 0, 0, 0,           /* replaced with index into relocation table.  */
6658     0xe9,                 /* jmp relative */
6659     0, 0, 0, 0,           /* replaced with offset to start of .plt0.  */
6660
6661     /* 22 bytes of nop to pad out to the standard size.  */
6662     0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    /* excess data16 prefixes    */
6663     0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6664     0x0f, 0x1f, 0x80, 0, 0, 0, 0,          /* nopl 0x0(%rax)            */
6665   };
6666
6667 /* .eh_frame covering the .plt section.  */
6668
6669 static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
6670   {
6671 #if (PLT_CIE_LENGTH != 20                               \
6672      || PLT_FDE_LENGTH != 36                            \
6673      || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8  \
6674      || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
6675 # error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
6676 #endif
6677     PLT_CIE_LENGTH, 0, 0, 0,    /* CIE length */
6678     0, 0, 0, 0,                 /* CIE ID */
6679     1,                          /* CIE version */
6680     'z', 'R', 0,                /* Augmentation string */
6681     1,                          /* Code alignment factor */
6682     0x78,                       /* Data alignment factor */
6683     16,                         /* Return address column */
6684     1,                          /* Augmentation size */
6685     DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
6686     DW_CFA_def_cfa, 7, 8,       /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
6687     DW_CFA_offset + 16, 1,      /* DW_CFA_offset: r16 (rip) at cfa-8 */
6688     DW_CFA_nop, DW_CFA_nop,
6689
6690     PLT_FDE_LENGTH, 0, 0, 0,    /* FDE length */
6691     PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
6692     0, 0, 0, 0,                 /* R_X86_64_PC32 .plt goes here */
6693     0, 0, 0, 0,                 /* .plt size goes here */
6694     0,                          /* Augmentation size */
6695     DW_CFA_def_cfa_offset, 16,  /* DW_CFA_def_cfa_offset: 16 */
6696     DW_CFA_advance_loc + 6,     /* DW_CFA_advance_loc: 6 to __PLT__+6 */
6697     DW_CFA_def_cfa_offset, 24,  /* DW_CFA_def_cfa_offset: 24 */
6698     DW_CFA_advance_loc + 58,    /* DW_CFA_advance_loc: 58 to __PLT__+64 */
6699     DW_CFA_def_cfa_expression,  /* DW_CFA_def_cfa_expression */
6700     13,                         /* Block length */
6701     DW_OP_breg7, 8,             /* DW_OP_breg7 (rsp): 8 */
6702     DW_OP_breg16, 0,            /* DW_OP_breg16 (rip): 0 */
6703     DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
6704     DW_OP_lit3, DW_OP_shl, DW_OP_plus,
6705     DW_CFA_nop, DW_CFA_nop
6706   };
6707
6708 static const struct elf_x86_lazy_plt_layout elf_x86_64_nacl_plt =
6709   {
6710     elf_x86_64_nacl_plt0_entry,              /* plt0_entry */
6711     NACL_PLT_ENTRY_SIZE,                     /* plt0_entry_size */
6712     elf_x86_64_nacl_plt_entry,               /* plt_entry */
6713     NACL_PLT_ENTRY_SIZE,                     /* plt_entry_size */
6714     2,                                       /* plt0_got1_offset */
6715     9,                                       /* plt0_got2_offset */
6716     13,                                      /* plt0_got2_insn_end */
6717     3,                                       /* plt_got_offset */
6718     33,                                      /* plt_reloc_offset */
6719     38,                                      /* plt_plt_offset */
6720     7,                                       /* plt_got_insn_size */
6721     42,                                      /* plt_plt_insn_end */
6722     32,                                      /* plt_lazy_offset */
6723     elf_x86_64_nacl_plt0_entry,              /* pic_plt0_entry */
6724     elf_x86_64_nacl_plt_entry,               /* pic_plt_entry */
6725     elf_x86_64_nacl_eh_frame_plt,            /* eh_frame_plt */
6726     sizeof (elf_x86_64_nacl_eh_frame_plt)    /* eh_frame_plt_size */
6727   };
6728
6729 static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
6730   {
6731     is_nacl                                  /* os */
6732   };
6733
6734 #undef  elf_backend_arch_data
6735 #define elf_backend_arch_data   &elf_x86_64_nacl_arch_bed
6736
6737 #undef  elf_backend_object_p
6738 #define elf_backend_object_p                    elf64_x86_64_nacl_elf_object_p
6739 #undef  elf_backend_modify_segment_map
6740 #define elf_backend_modify_segment_map          nacl_modify_segment_map
6741 #undef  elf_backend_modify_program_headers
6742 #define elf_backend_modify_program_headers      nacl_modify_program_headers
6743 #undef  elf_backend_final_write_processing
6744 #define elf_backend_final_write_processing      nacl_final_write_processing
6745
6746 #include "elf64-target.h"
6747
6748 /* Native Client x32 support.  */
6749
6750 static bfd_boolean
6751 elf32_x86_64_nacl_elf_object_p (bfd *abfd)
6752 {
6753   /* Set the right machine number for a NaCl x86-64 ELF32 file.  */
6754   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
6755   return TRUE;
6756 }
6757
6758 #undef  TARGET_LITTLE_SYM
6759 #define TARGET_LITTLE_SYM               x86_64_elf32_nacl_vec
6760 #undef  TARGET_LITTLE_NAME
6761 #define TARGET_LITTLE_NAME              "elf32-x86-64-nacl"
6762 #undef  elf32_bed
6763 #define elf32_bed                       elf32_x86_64_nacl_bed
6764
6765 #define bfd_elf32_bfd_reloc_type_lookup \
6766   elf_x86_64_reloc_type_lookup
6767 #define bfd_elf32_bfd_reloc_name_lookup \
6768   elf_x86_64_reloc_name_lookup
6769 #define bfd_elf32_mkobject \
6770   elf_x86_64_mkobject
6771 #define bfd_elf32_get_synthetic_symtab \
6772   elf_x86_64_get_synthetic_symtab
6773
6774 #undef elf_backend_object_p
6775 #define elf_backend_object_p \
6776   elf32_x86_64_nacl_elf_object_p
6777
6778 #undef elf_backend_bfd_from_remote_memory
6779 #define elf_backend_bfd_from_remote_memory \
6780   _bfd_elf32_bfd_from_remote_memory
6781
6782 #undef elf_backend_size_info
6783 #define elf_backend_size_info \
6784   _bfd_elf32_size_info
6785
6786 #include "elf32-target.h"
6787
6788 /* Restore defaults.  */
6789 #undef  elf_backend_object_p
6790 #define elf_backend_object_p                elf64_x86_64_elf_object_p
6791 #undef  elf_backend_bfd_from_remote_memory
6792 #undef  elf_backend_size_info
6793 #undef  elf_backend_modify_segment_map
6794 #undef  elf_backend_modify_program_headers
6795 #undef  elf_backend_final_write_processing
6796
6797 /* Intel L1OM support.  */
6798
6799 static bfd_boolean
6800 elf64_l1om_elf_object_p (bfd *abfd)
6801 {
6802   /* Set the right machine number for an L1OM elf64 file.  */
6803   bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
6804   return TRUE;
6805 }
6806
6807 #undef  TARGET_LITTLE_SYM
6808 #define TARGET_LITTLE_SYM                   l1om_elf64_vec
6809 #undef  TARGET_LITTLE_NAME
6810 #define TARGET_LITTLE_NAME                  "elf64-l1om"
6811 #undef ELF_ARCH
6812 #define ELF_ARCH                            bfd_arch_l1om
6813
6814 #undef  ELF_MACHINE_CODE
6815 #define ELF_MACHINE_CODE                    EM_L1OM
6816
6817 #undef  ELF_OSABI
6818
6819 #undef  elf64_bed
6820 #define elf64_bed elf64_l1om_bed
6821
6822 #undef elf_backend_object_p
6823 #define elf_backend_object_p                elf64_l1om_elf_object_p
6824
6825 /* Restore defaults.  */
6826 #undef  ELF_MAXPAGESIZE
6827 #undef  ELF_MINPAGESIZE
6828 #undef  ELF_COMMONPAGESIZE
6829 #define ELF_MAXPAGESIZE                 0x200000
6830 #define ELF_MINPAGESIZE                 0x1000
6831 #define ELF_COMMONPAGESIZE              0x1000
6832 #undef  elf_backend_plt_alignment
6833 #define elf_backend_plt_alignment       4
6834 #undef  elf_backend_arch_data
6835 #define elf_backend_arch_data   &elf_x86_64_arch_bed
6836
6837 #include "elf64-target.h"
6838
6839 /* FreeBSD L1OM support.  */
6840
6841 #undef  TARGET_LITTLE_SYM
6842 #define TARGET_LITTLE_SYM                   l1om_elf64_fbsd_vec
6843 #undef  TARGET_LITTLE_NAME
6844 #define TARGET_LITTLE_NAME                  "elf64-l1om-freebsd"
6845
6846 #undef  ELF_OSABI
6847 #define ELF_OSABI                           ELFOSABI_FREEBSD
6848
6849 #undef  elf64_bed
6850 #define elf64_bed elf64_l1om_fbsd_bed
6851
6852 #include "elf64-target.h"
6853
6854 /* Intel K1OM support.  */
6855
6856 static bfd_boolean
6857 elf64_k1om_elf_object_p (bfd *abfd)
6858 {
6859   /* Set the right machine number for an K1OM elf64 file.  */
6860   bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
6861   return TRUE;
6862 }
6863
6864 #undef  TARGET_LITTLE_SYM
6865 #define TARGET_LITTLE_SYM                   k1om_elf64_vec
6866 #undef  TARGET_LITTLE_NAME
6867 #define TARGET_LITTLE_NAME                  "elf64-k1om"
6868 #undef ELF_ARCH
6869 #define ELF_ARCH                            bfd_arch_k1om
6870
6871 #undef  ELF_MACHINE_CODE
6872 #define ELF_MACHINE_CODE                    EM_K1OM
6873
6874 #undef  ELF_OSABI
6875
6876 #undef  elf64_bed
6877 #define elf64_bed elf64_k1om_bed
6878
6879 #undef elf_backend_object_p
6880 #define elf_backend_object_p                elf64_k1om_elf_object_p
6881
6882 #undef  elf_backend_static_tls_alignment
6883
6884 #undef elf_backend_want_plt_sym
6885 #define elf_backend_want_plt_sym            0
6886
6887 #include "elf64-target.h"
6888
6889 /* FreeBSD K1OM support.  */
6890
6891 #undef  TARGET_LITTLE_SYM
6892 #define TARGET_LITTLE_SYM                   k1om_elf64_fbsd_vec
6893 #undef  TARGET_LITTLE_NAME
6894 #define TARGET_LITTLE_NAME                  "elf64-k1om-freebsd"
6895
6896 #undef  ELF_OSABI
6897 #define ELF_OSABI                           ELFOSABI_FREEBSD
6898
6899 #undef  elf64_bed
6900 #define elf64_bed elf64_k1om_fbsd_bed
6901
6902 #include "elf64-target.h"
6903
6904 /* 32bit x86-64 support.  */
6905
6906 #undef  TARGET_LITTLE_SYM
6907 #define TARGET_LITTLE_SYM                   x86_64_elf32_vec
6908 #undef  TARGET_LITTLE_NAME
6909 #define TARGET_LITTLE_NAME                  "elf32-x86-64"
6910 #undef  elf32_bed
6911
6912 #undef ELF_ARCH
6913 #define ELF_ARCH                            bfd_arch_i386
6914
6915 #undef  ELF_MACHINE_CODE
6916 #define ELF_MACHINE_CODE                    EM_X86_64
6917
6918 #undef  ELF_OSABI
6919
6920 #undef elf_backend_object_p
6921 #define elf_backend_object_p \
6922   elf32_x86_64_elf_object_p
6923
6924 #undef elf_backend_bfd_from_remote_memory
6925 #define elf_backend_bfd_from_remote_memory \
6926   _bfd_elf32_bfd_from_remote_memory
6927
6928 #undef elf_backend_size_info
6929 #define elf_backend_size_info \
6930   _bfd_elf32_size_info
6931
6932 #include "elf32-target.h"