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