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