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