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