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