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