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