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