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