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