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