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