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