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