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