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