2008-01-11 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "bfd_stdint.h"
29
30 #include "elf/x86-64.h"
31
32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33 #define MINUS_ONE (~ (bfd_vma) 0)
34
35 /* The relocation "howto" table. Order of fields:
36 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
37 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
38 static reloc_howto_type x86_64_elf_howto_table[] =
39 {
40 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
41 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
42 FALSE),
43 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
44 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
45 FALSE),
46 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
47 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
48 TRUE),
49 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
50 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
51 FALSE),
52 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
53 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
54 TRUE),
55 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
57 FALSE),
58 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
60 MINUS_ONE, FALSE),
61 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
63 MINUS_ONE, FALSE),
64 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
65 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
66 MINUS_ONE, FALSE),
67 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
68 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
69 0xffffffff, TRUE),
70 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
71 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
72 FALSE),
73 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
74 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
75 FALSE),
76 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
78 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
80 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
82 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
83 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
84 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
86 MINUS_ONE, FALSE),
87 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
89 MINUS_ONE, FALSE),
90 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
91 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
92 MINUS_ONE, FALSE),
93 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
94 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
95 0xffffffff, TRUE),
96 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
97 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
98 0xffffffff, TRUE),
99 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
100 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
101 0xffffffff, FALSE),
102 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
103 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
104 0xffffffff, TRUE),
105 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
106 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
107 0xffffffff, FALSE),
108 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
110 TRUE),
111 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
112 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
113 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
114 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
115 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
116 FALSE, 0xffffffff, 0xffffffff, TRUE),
117 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
118 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
119 FALSE),
120 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
121 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
122 MINUS_ONE, TRUE),
123 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
124 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
125 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
126 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
127 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
128 MINUS_ONE, FALSE),
129 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
130 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
131 MINUS_ONE, FALSE),
132 EMPTY_HOWTO (32),
133 EMPTY_HOWTO (33),
134 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
135 complain_overflow_bitfield, bfd_elf_generic_reloc,
136 "R_X86_64_GOTPC32_TLSDESC",
137 FALSE, 0xffffffff, 0xffffffff, TRUE),
138 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
139 complain_overflow_dont, bfd_elf_generic_reloc,
140 "R_X86_64_TLSDESC_CALL",
141 FALSE, 0, 0, FALSE),
142 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
143 complain_overflow_bitfield, bfd_elf_generic_reloc,
144 "R_X86_64_TLSDESC",
145 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
146
147 /* We have a gap in the reloc numbers here.
148 R_X86_64_standard counts the number up to this point, and
149 R_X86_64_vt_offset is the value to subtract from a reloc type of
150 R_X86_64_GNU_VT* to form an index into this table. */
151 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
152 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
153
154 /* GNU extension to record C++ vtable hierarchy. */
155 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
156 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
157
158 /* GNU extension to record C++ vtable member usage. */
159 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
160 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
161 FALSE)
162 };
163
164 /* Map BFD relocs to the x86_64 elf relocs. */
165 struct elf_reloc_map
166 {
167 bfd_reloc_code_real_type bfd_reloc_val;
168 unsigned char elf_reloc_val;
169 };
170
171 static const struct elf_reloc_map x86_64_reloc_map[] =
172 {
173 { BFD_RELOC_NONE, R_X86_64_NONE, },
174 { BFD_RELOC_64, R_X86_64_64, },
175 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
176 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
177 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
178 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
179 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
180 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
181 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
182 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
183 { BFD_RELOC_32, R_X86_64_32, },
184 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
185 { BFD_RELOC_16, R_X86_64_16, },
186 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
187 { BFD_RELOC_8, R_X86_64_8, },
188 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
189 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
190 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
191 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
192 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
193 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
194 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
195 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
196 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
197 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
198 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
199 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
200 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
201 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
202 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
203 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
204 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
205 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
206 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
207 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
208 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
209 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
210 };
211
212 static reloc_howto_type *
213 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
214 {
215 unsigned i;
216
217 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
218 || r_type >= (unsigned int) R_X86_64_max)
219 {
220 if (r_type >= (unsigned int) R_X86_64_standard)
221 {
222 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
223 abfd, (int) r_type);
224 r_type = R_X86_64_NONE;
225 }
226 i = r_type;
227 }
228 else
229 i = r_type - (unsigned int) R_X86_64_vt_offset;
230 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
231 return &x86_64_elf_howto_table[i];
232 }
233
234 /* Given a BFD reloc type, return a HOWTO structure. */
235 static reloc_howto_type *
236 elf64_x86_64_reloc_type_lookup (bfd *abfd,
237 bfd_reloc_code_real_type code)
238 {
239 unsigned int i;
240
241 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
242 i++)
243 {
244 if (x86_64_reloc_map[i].bfd_reloc_val == code)
245 return elf64_x86_64_rtype_to_howto (abfd,
246 x86_64_reloc_map[i].elf_reloc_val);
247 }
248 return 0;
249 }
250
251 static reloc_howto_type *
252 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
253 const char *r_name)
254 {
255 unsigned int i;
256
257 for (i = 0;
258 i < (sizeof (x86_64_elf_howto_table)
259 / sizeof (x86_64_elf_howto_table[0]));
260 i++)
261 if (x86_64_elf_howto_table[i].name != NULL
262 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
263 return &x86_64_elf_howto_table[i];
264
265 return NULL;
266 }
267
268 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
269
270 static void
271 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
272 Elf_Internal_Rela *dst)
273 {
274 unsigned r_type;
275
276 r_type = ELF64_R_TYPE (dst->r_info);
277 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
278 BFD_ASSERT (r_type == cache_ptr->howto->type);
279 }
280 \f
281 /* Support for core dump NOTE sections. */
282 static bfd_boolean
283 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
284 {
285 int offset;
286 size_t size;
287
288 switch (note->descsz)
289 {
290 default:
291 return FALSE;
292
293 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
294 /* pr_cursig */
295 elf_tdata (abfd)->core_signal
296 = bfd_get_16 (abfd, note->descdata + 12);
297
298 /* pr_pid */
299 elf_tdata (abfd)->core_pid
300 = bfd_get_32 (abfd, note->descdata + 32);
301
302 /* pr_reg */
303 offset = 112;
304 size = 216;
305
306 break;
307 }
308
309 /* Make a ".reg/999" section. */
310 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
311 size, note->descpos + offset);
312 }
313
314 static bfd_boolean
315 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
316 {
317 switch (note->descsz)
318 {
319 default:
320 return FALSE;
321
322 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
323 elf_tdata (abfd)->core_program
324 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
325 elf_tdata (abfd)->core_command
326 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
327 }
328
329 /* Note that for some reason, a spurious space is tacked
330 onto the end of the args in some (at least one anyway)
331 implementations, so strip it off if it exists. */
332
333 {
334 char *command = elf_tdata (abfd)->core_command;
335 int n = strlen (command);
336
337 if (0 < n && command[n - 1] == ' ')
338 command[n - 1] = '\0';
339 }
340
341 return TRUE;
342 }
343 \f
344 /* Functions for the x86-64 ELF linker. */
345
346 /* The name of the dynamic interpreter. This is put in the .interp
347 section. */
348
349 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
350
351 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
352 copying dynamic variables from a shared lib into an app's dynbss
353 section, and instead use a dynamic relocation to point into the
354 shared lib. */
355 #define ELIMINATE_COPY_RELOCS 1
356
357 /* The size in bytes of an entry in the global offset table. */
358
359 #define GOT_ENTRY_SIZE 8
360
361 /* The size in bytes of an entry in the procedure linkage table. */
362
363 #define PLT_ENTRY_SIZE 16
364
365 /* The first entry in a procedure linkage table looks like this. See the
366 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
367
368 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
369 {
370 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
371 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
372 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
373 };
374
375 /* Subsequent entries in a procedure linkage table look like this. */
376
377 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
378 {
379 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
380 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
381 0x68, /* pushq immediate */
382 0, 0, 0, 0, /* replaced with index into relocation table. */
383 0xe9, /* jmp relative */
384 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
385 };
386
387 /* The x86-64 linker needs to keep track of the number of relocs that
388 it decides to copy as dynamic relocs in check_relocs for each symbol.
389 This is so that it can later discard them if they are found to be
390 unnecessary. We store the information in a field extending the
391 regular ELF linker hash table. */
392
393 struct elf64_x86_64_dyn_relocs
394 {
395 /* Next section. */
396 struct elf64_x86_64_dyn_relocs *next;
397
398 /* The input section of the reloc. */
399 asection *sec;
400
401 /* Total number of relocs copied for the input section. */
402 bfd_size_type count;
403
404 /* Number of pc-relative relocs copied for the input section. */
405 bfd_size_type pc_count;
406 };
407
408 /* x86-64 ELF linker hash entry. */
409
410 struct elf64_x86_64_link_hash_entry
411 {
412 struct elf_link_hash_entry elf;
413
414 /* Track dynamic relocs copied for this symbol. */
415 struct elf64_x86_64_dyn_relocs *dyn_relocs;
416
417 #define GOT_UNKNOWN 0
418 #define GOT_NORMAL 1
419 #define GOT_TLS_GD 2
420 #define GOT_TLS_IE 3
421 #define GOT_TLS_GDESC 4
422 #define GOT_TLS_GD_BOTH_P(type) \
423 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
424 #define GOT_TLS_GD_P(type) \
425 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
426 #define GOT_TLS_GDESC_P(type) \
427 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
428 #define GOT_TLS_GD_ANY_P(type) \
429 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
430 unsigned char tls_type;
431
432 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
433 starting at the end of the jump table. */
434 bfd_vma tlsdesc_got;
435 };
436
437 #define elf64_x86_64_hash_entry(ent) \
438 ((struct elf64_x86_64_link_hash_entry *)(ent))
439
440 struct elf64_x86_64_obj_tdata
441 {
442 struct elf_obj_tdata root;
443
444 /* tls_type for each local got entry. */
445 char *local_got_tls_type;
446
447 /* GOTPLT entries for TLS descriptors. */
448 bfd_vma *local_tlsdesc_gotent;
449 };
450
451 #define elf64_x86_64_tdata(abfd) \
452 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
453
454 #define elf64_x86_64_local_got_tls_type(abfd) \
455 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
456
457 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
458 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
459
460 /* x86-64 ELF linker hash table. */
461
462 struct elf64_x86_64_link_hash_table
463 {
464 struct elf_link_hash_table elf;
465
466 /* Short-cuts to get to dynamic linker sections. */
467 asection *sgot;
468 asection *sgotplt;
469 asection *srelgot;
470 asection *splt;
471 asection *srelplt;
472 asection *sdynbss;
473 asection *srelbss;
474
475 /* The offset into splt of the PLT entry for the TLS descriptor
476 resolver. Special values are 0, if not necessary (or not found
477 to be necessary yet), and -1 if needed but not determined
478 yet. */
479 bfd_vma tlsdesc_plt;
480 /* The offset into sgot of the GOT entry used by the PLT entry
481 above. */
482 bfd_vma tlsdesc_got;
483
484 union {
485 bfd_signed_vma refcount;
486 bfd_vma offset;
487 } tls_ld_got;
488
489 /* The amount of space used by the jump slots in the GOT. */
490 bfd_vma sgotplt_jump_table_size;
491
492 /* Small local sym to section mapping cache. */
493 struct sym_sec_cache sym_sec;
494 };
495
496 /* Get the x86-64 ELF linker hash table from a link_info structure. */
497
498 #define elf64_x86_64_hash_table(p) \
499 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
500
501 #define elf64_x86_64_compute_jump_table_size(htab) \
502 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
503
504 /* Create an entry in an x86-64 ELF linker hash table. */
505
506 static struct bfd_hash_entry *
507 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
508 const char *string)
509 {
510 /* Allocate the structure if it has not already been allocated by a
511 subclass. */
512 if (entry == NULL)
513 {
514 entry = bfd_hash_allocate (table,
515 sizeof (struct elf64_x86_64_link_hash_entry));
516 if (entry == NULL)
517 return entry;
518 }
519
520 /* Call the allocation method of the superclass. */
521 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
522 if (entry != NULL)
523 {
524 struct elf64_x86_64_link_hash_entry *eh;
525
526 eh = (struct elf64_x86_64_link_hash_entry *) entry;
527 eh->dyn_relocs = NULL;
528 eh->tls_type = GOT_UNKNOWN;
529 eh->tlsdesc_got = (bfd_vma) -1;
530 }
531
532 return entry;
533 }
534
535 /* Create an X86-64 ELF linker hash table. */
536
537 static struct bfd_link_hash_table *
538 elf64_x86_64_link_hash_table_create (bfd *abfd)
539 {
540 struct elf64_x86_64_link_hash_table *ret;
541 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
542
543 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
544 if (ret == NULL)
545 return NULL;
546
547 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
548 sizeof (struct elf64_x86_64_link_hash_entry)))
549 {
550 free (ret);
551 return NULL;
552 }
553
554 ret->sgot = NULL;
555 ret->sgotplt = NULL;
556 ret->srelgot = NULL;
557 ret->splt = NULL;
558 ret->srelplt = NULL;
559 ret->sdynbss = NULL;
560 ret->srelbss = NULL;
561 ret->sym_sec.abfd = NULL;
562 ret->tlsdesc_plt = 0;
563 ret->tlsdesc_got = 0;
564 ret->tls_ld_got.refcount = 0;
565 ret->sgotplt_jump_table_size = 0;
566
567 return &ret->elf.root;
568 }
569
570 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
571 shortcuts to them in our hash table. */
572
573 static bfd_boolean
574 create_got_section (bfd *dynobj, struct bfd_link_info *info)
575 {
576 struct elf64_x86_64_link_hash_table *htab;
577
578 if (! _bfd_elf_create_got_section (dynobj, info))
579 return FALSE;
580
581 htab = elf64_x86_64_hash_table (info);
582 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
583 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
584 if (!htab->sgot || !htab->sgotplt)
585 abort ();
586
587 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
588 (SEC_ALLOC | SEC_LOAD
589 | SEC_HAS_CONTENTS
590 | SEC_IN_MEMORY
591 | SEC_LINKER_CREATED
592 | SEC_READONLY));
593 if (htab->srelgot == NULL
594 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
595 return FALSE;
596 return TRUE;
597 }
598
599 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
600 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
601 hash table. */
602
603 static bfd_boolean
604 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
605 {
606 struct elf64_x86_64_link_hash_table *htab;
607
608 htab = elf64_x86_64_hash_table (info);
609 if (!htab->sgot && !create_got_section (dynobj, info))
610 return FALSE;
611
612 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
613 return FALSE;
614
615 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
616 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
617 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
618 if (!info->shared)
619 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
620
621 if (!htab->splt || !htab->srelplt || !htab->sdynbss
622 || (!info->shared && !htab->srelbss))
623 abort ();
624
625 return TRUE;
626 }
627
628 /* Copy the extra info we tack onto an elf_link_hash_entry. */
629
630 static void
631 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
632 struct elf_link_hash_entry *dir,
633 struct elf_link_hash_entry *ind)
634 {
635 struct elf64_x86_64_link_hash_entry *edir, *eind;
636
637 edir = (struct elf64_x86_64_link_hash_entry *) dir;
638 eind = (struct elf64_x86_64_link_hash_entry *) ind;
639
640 if (eind->dyn_relocs != NULL)
641 {
642 if (edir->dyn_relocs != NULL)
643 {
644 struct elf64_x86_64_dyn_relocs **pp;
645 struct elf64_x86_64_dyn_relocs *p;
646
647 /* Add reloc counts against the indirect sym to the direct sym
648 list. Merge any entries against the same section. */
649 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
650 {
651 struct elf64_x86_64_dyn_relocs *q;
652
653 for (q = edir->dyn_relocs; q != NULL; q = q->next)
654 if (q->sec == p->sec)
655 {
656 q->pc_count += p->pc_count;
657 q->count += p->count;
658 *pp = p->next;
659 break;
660 }
661 if (q == NULL)
662 pp = &p->next;
663 }
664 *pp = edir->dyn_relocs;
665 }
666
667 edir->dyn_relocs = eind->dyn_relocs;
668 eind->dyn_relocs = NULL;
669 }
670
671 if (ind->root.type == bfd_link_hash_indirect
672 && dir->got.refcount <= 0)
673 {
674 edir->tls_type = eind->tls_type;
675 eind->tls_type = GOT_UNKNOWN;
676 }
677
678 if (ELIMINATE_COPY_RELOCS
679 && ind->root.type != bfd_link_hash_indirect
680 && dir->dynamic_adjusted)
681 {
682 /* If called to transfer flags for a weakdef during processing
683 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
684 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
685 dir->ref_dynamic |= ind->ref_dynamic;
686 dir->ref_regular |= ind->ref_regular;
687 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
688 dir->needs_plt |= ind->needs_plt;
689 dir->pointer_equality_needed |= ind->pointer_equality_needed;
690 }
691 else
692 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
693 }
694
695 static bfd_boolean
696 elf64_x86_64_mkobject (bfd *abfd)
697 {
698 if (abfd->tdata.any == NULL)
699 {
700 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
701 abfd->tdata.any = bfd_zalloc (abfd, amt);
702 if (abfd->tdata.any == NULL)
703 return FALSE;
704 }
705 return bfd_elf_mkobject (abfd);
706 }
707
708 static bfd_boolean
709 elf64_x86_64_elf_object_p (bfd *abfd)
710 {
711 /* Set the right machine number for an x86-64 elf64 file. */
712 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
713 return TRUE;
714 }
715
716 typedef union
717 {
718 unsigned char c[2];
719 uint16_t i;
720 }
721 x86_64_opcode16;
722
723 typedef union
724 {
725 unsigned char c[4];
726 uint32_t i;
727 }
728 x86_64_opcode32;
729
730 /* Return TRUE if the TLS access code sequence support transition
731 from R_TYPE. */
732
733 static bfd_boolean
734 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
735 bfd_byte *contents,
736 Elf_Internal_Shdr *symtab_hdr,
737 struct elf_link_hash_entry **sym_hashes,
738 unsigned int r_type,
739 const Elf_Internal_Rela *rel,
740 const Elf_Internal_Rela *relend)
741 {
742 unsigned int val;
743 unsigned long r_symndx;
744 struct elf_link_hash_entry *h;
745 bfd_vma offset;
746
747 /* Get the section contents. */
748 if (contents == NULL)
749 {
750 if (elf_section_data (sec)->this_hdr.contents != NULL)
751 contents = elf_section_data (sec)->this_hdr.contents;
752 else
753 {
754 /* FIXME: How to better handle error condition? */
755 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
756 return FALSE;
757
758 /* Cache the section contents for elf_link_input_bfd. */
759 elf_section_data (sec)->this_hdr.contents = contents;
760 }
761 }
762
763 offset = rel->r_offset;
764 switch (r_type)
765 {
766 case R_X86_64_TLSGD:
767 case R_X86_64_TLSLD:
768 if ((rel + 1) >= relend)
769 return FALSE;
770
771 if (r_type == R_X86_64_TLSGD)
772 {
773 /* Check transition from GD access model. Only
774 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
775 .word 0x6666; rex64; call __tls_get_addr
776 can transit to different access model. */
777
778 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
779 call = { { 0x66, 0x66, 0x48, 0xe8 } };
780 if (offset < 4
781 || (offset + 12) > sec->size
782 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
783 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
784 return FALSE;
785 }
786 else
787 {
788 /* Check transition from LD access model. Only
789 leaq foo@tlsld(%rip), %rdi;
790 call __tls_get_addr
791 can transit to different access model. */
792
793 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
794 x86_64_opcode32 op;
795
796 if (offset < 3 || (offset + 9) > sec->size)
797 return FALSE;
798
799 op.i = bfd_get_32 (abfd, contents + offset - 3);
800 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
801 if (op.i != ld.i)
802 return FALSE;
803 }
804
805 r_symndx = ELF64_R_SYM (rel[1].r_info);
806 if (r_symndx < symtab_hdr->sh_info)
807 return FALSE;
808
809 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
810 return (h != NULL
811 && h->root.root.string != NULL
812 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
813 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
814 && (strcmp (h->root.root.string, "__tls_get_addr") == 0));
815
816 case R_X86_64_GOTTPOFF:
817 /* Check transition from IE access model:
818 movq foo@gottpoff(%rip), %reg
819 addq foo@gottpoff(%rip), %reg
820 */
821
822 if (offset < 3 || (offset + 4) > sec->size)
823 return FALSE;
824
825 val = bfd_get_8 (abfd, contents + offset - 3);
826 if (val != 0x48 && val != 0x4c)
827 return FALSE;
828
829 val = bfd_get_8 (abfd, contents + offset - 2);
830 if (val != 0x8b && val != 0x03)
831 return FALSE;
832
833 val = bfd_get_8 (abfd, contents + offset - 1);
834 return (val & 0xc7) == 5;
835
836 case R_X86_64_GOTPC32_TLSDESC:
837 /* Check transition from GDesc access model:
838 leaq x@tlsdesc(%rip), %rax
839
840 Make sure it's a leaq adding rip to a 32-bit offset
841 into any register, although it's probably almost always
842 going to be rax. */
843
844 if (offset < 3 || (offset + 4) > sec->size)
845 return FALSE;
846
847 val = bfd_get_8 (abfd, contents + offset - 3);
848 if ((val & 0xfb) != 0x48)
849 return FALSE;
850
851 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
852 return FALSE;
853
854 val = bfd_get_8 (abfd, contents + offset - 1);
855 return (val & 0xc7) == 0x05;
856
857 case R_X86_64_TLSDESC_CALL:
858 /* Check transition from GDesc access model:
859 call *x@tlsdesc(%rax)
860 */
861 if (offset + 2 <= sec->size)
862 {
863 /* Make sure that it's a call *x@tlsdesc(%rax). */
864 static x86_64_opcode16 call = { { 0xff, 0x10 } };
865 return bfd_get_16 (abfd, contents + offset) == call.i;
866 }
867
868 return FALSE;
869
870 default:
871 abort ();
872 }
873 }
874
875 /* Return TRUE if the TLS access transition is OK or no transition
876 will be performed. Update R_TYPE if there is a transition. */
877
878 static bfd_boolean
879 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
880 asection *sec, bfd_byte *contents,
881 Elf_Internal_Shdr *symtab_hdr,
882 struct elf_link_hash_entry **sym_hashes,
883 unsigned int *r_type, int tls_type,
884 const Elf_Internal_Rela *rel,
885 const Elf_Internal_Rela *relend,
886 struct elf_link_hash_entry *h)
887 {
888 unsigned int from_type = *r_type;
889 unsigned int to_type = from_type;
890 bfd_boolean check = TRUE;
891
892 switch (from_type)
893 {
894 case R_X86_64_TLSGD:
895 case R_X86_64_GOTPC32_TLSDESC:
896 case R_X86_64_TLSDESC_CALL:
897 case R_X86_64_GOTTPOFF:
898 if (!info->shared)
899 {
900 if (h == NULL)
901 to_type = R_X86_64_TPOFF32;
902 else
903 to_type = R_X86_64_GOTTPOFF;
904 }
905
906 /* When we are called from elf64_x86_64_relocate_section,
907 CONTENTS isn't NULL and there may be additional transitions
908 based on TLS_TYPE. */
909 if (contents != NULL)
910 {
911 unsigned int new_to_type = to_type;
912
913 if (!info->shared
914 && h != NULL
915 && h->dynindx == -1
916 && tls_type == GOT_TLS_IE)
917 new_to_type = R_X86_64_TPOFF32;
918
919 if (to_type == R_X86_64_TLSGD
920 || to_type == R_X86_64_GOTPC32_TLSDESC
921 || to_type == R_X86_64_TLSDESC_CALL)
922 {
923 if (tls_type == GOT_TLS_IE)
924 new_to_type = R_X86_64_GOTTPOFF;
925 }
926
927 /* We checked the transition before when we were called from
928 elf64_x86_64_check_relocs. We only want to check the new
929 transition which hasn't been checked before. */
930 check = new_to_type != to_type && from_type == to_type;
931 to_type = new_to_type;
932 }
933
934 break;
935
936 case R_X86_64_TLSLD:
937 if (!info->shared)
938 to_type = R_X86_64_TPOFF32;
939 break;
940
941 default:
942 return TRUE;
943 }
944
945 /* Return TRUE if there is no transition. */
946 if (from_type == to_type)
947 return TRUE;
948
949 /* Check if the transition can be performed. */
950 if (check
951 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
952 symtab_hdr, sym_hashes,
953 from_type, rel, relend))
954 {
955 reloc_howto_type *from, *to;
956
957 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
958 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
959
960 (*_bfd_error_handler)
961 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
962 "in section `%A' failed"),
963 abfd, sec, from->name, to->name,
964 h ? h->root.root.string : "a local symbol",
965 (unsigned long) rel->r_offset);
966 bfd_set_error (bfd_error_bad_value);
967 return FALSE;
968 }
969
970 *r_type = to_type;
971 return TRUE;
972 }
973
974 /* Look through the relocs for a section during the first phase, and
975 calculate needed space in the global offset table, procedure
976 linkage table, and dynamic reloc sections. */
977
978 static bfd_boolean
979 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
980 asection *sec,
981 const Elf_Internal_Rela *relocs)
982 {
983 struct elf64_x86_64_link_hash_table *htab;
984 Elf_Internal_Shdr *symtab_hdr;
985 struct elf_link_hash_entry **sym_hashes;
986 const Elf_Internal_Rela *rel;
987 const Elf_Internal_Rela *rel_end;
988 asection *sreloc;
989
990 if (info->relocatable)
991 return TRUE;
992
993 htab = elf64_x86_64_hash_table (info);
994 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
995 sym_hashes = elf_sym_hashes (abfd);
996
997 sreloc = NULL;
998
999 rel_end = relocs + sec->reloc_count;
1000 for (rel = relocs; rel < rel_end; rel++)
1001 {
1002 unsigned int r_type;
1003 unsigned long r_symndx;
1004 struct elf_link_hash_entry *h;
1005
1006 r_symndx = ELF64_R_SYM (rel->r_info);
1007 r_type = ELF64_R_TYPE (rel->r_info);
1008
1009 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1010 {
1011 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1012 abfd, r_symndx);
1013 return FALSE;
1014 }
1015
1016 if (r_symndx < symtab_hdr->sh_info)
1017 h = NULL;
1018 else
1019 {
1020 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1021 while (h->root.type == bfd_link_hash_indirect
1022 || h->root.type == bfd_link_hash_warning)
1023 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1024 }
1025
1026 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1027 symtab_hdr, sym_hashes,
1028 &r_type, GOT_UNKNOWN,
1029 rel, rel_end, h))
1030 return FALSE;
1031
1032 switch (r_type)
1033 {
1034 case R_X86_64_TLSLD:
1035 htab->tls_ld_got.refcount += 1;
1036 goto create_got;
1037
1038 case R_X86_64_TPOFF32:
1039 if (info->shared)
1040 {
1041 (*_bfd_error_handler)
1042 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1043 abfd,
1044 x86_64_elf_howto_table[r_type].name,
1045 (h) ? h->root.root.string : "a local symbol");
1046 bfd_set_error (bfd_error_bad_value);
1047 return FALSE;
1048 }
1049 break;
1050
1051 case R_X86_64_GOTTPOFF:
1052 if (info->shared)
1053 info->flags |= DF_STATIC_TLS;
1054 /* Fall through */
1055
1056 case R_X86_64_GOT32:
1057 case R_X86_64_GOTPCREL:
1058 case R_X86_64_TLSGD:
1059 case R_X86_64_GOT64:
1060 case R_X86_64_GOTPCREL64:
1061 case R_X86_64_GOTPLT64:
1062 case R_X86_64_GOTPC32_TLSDESC:
1063 case R_X86_64_TLSDESC_CALL:
1064 /* This symbol requires a global offset table entry. */
1065 {
1066 int tls_type, old_tls_type;
1067
1068 switch (r_type)
1069 {
1070 default: tls_type = GOT_NORMAL; break;
1071 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1072 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1073 case R_X86_64_GOTPC32_TLSDESC:
1074 case R_X86_64_TLSDESC_CALL:
1075 tls_type = GOT_TLS_GDESC; break;
1076 }
1077
1078 if (h != NULL)
1079 {
1080 if (r_type == R_X86_64_GOTPLT64)
1081 {
1082 /* This relocation indicates that we also need
1083 a PLT entry, as this is a function. We don't need
1084 a PLT entry for local symbols. */
1085 h->needs_plt = 1;
1086 h->plt.refcount += 1;
1087 }
1088 h->got.refcount += 1;
1089 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1090 }
1091 else
1092 {
1093 bfd_signed_vma *local_got_refcounts;
1094
1095 /* This is a global offset table entry for a local symbol. */
1096 local_got_refcounts = elf_local_got_refcounts (abfd);
1097 if (local_got_refcounts == NULL)
1098 {
1099 bfd_size_type size;
1100
1101 size = symtab_hdr->sh_info;
1102 size *= sizeof (bfd_signed_vma)
1103 + sizeof (bfd_vma) + sizeof (char);
1104 local_got_refcounts = ((bfd_signed_vma *)
1105 bfd_zalloc (abfd, size));
1106 if (local_got_refcounts == NULL)
1107 return FALSE;
1108 elf_local_got_refcounts (abfd) = local_got_refcounts;
1109 elf64_x86_64_local_tlsdesc_gotent (abfd)
1110 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1111 elf64_x86_64_local_got_tls_type (abfd)
1112 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1113 }
1114 local_got_refcounts[r_symndx] += 1;
1115 old_tls_type
1116 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1117 }
1118
1119 /* If a TLS symbol is accessed using IE at least once,
1120 there is no point to use dynamic model for it. */
1121 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1122 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1123 || tls_type != GOT_TLS_IE))
1124 {
1125 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1126 tls_type = old_tls_type;
1127 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1128 && GOT_TLS_GD_ANY_P (tls_type))
1129 tls_type |= old_tls_type;
1130 else
1131 {
1132 (*_bfd_error_handler)
1133 (_("%B: '%s' accessed both as normal and thread local symbol"),
1134 abfd, h ? h->root.root.string : "<local>");
1135 return FALSE;
1136 }
1137 }
1138
1139 if (old_tls_type != tls_type)
1140 {
1141 if (h != NULL)
1142 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1143 else
1144 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1145 }
1146 }
1147 /* Fall through */
1148
1149 case R_X86_64_GOTOFF64:
1150 case R_X86_64_GOTPC32:
1151 case R_X86_64_GOTPC64:
1152 create_got:
1153 if (htab->sgot == NULL)
1154 {
1155 if (htab->elf.dynobj == NULL)
1156 htab->elf.dynobj = abfd;
1157 if (!create_got_section (htab->elf.dynobj, info))
1158 return FALSE;
1159 }
1160 break;
1161
1162 case R_X86_64_PLT32:
1163 /* This symbol requires a procedure linkage table entry. We
1164 actually build the entry in adjust_dynamic_symbol,
1165 because this might be a case of linking PIC code which is
1166 never referenced by a dynamic object, in which case we
1167 don't need to generate a procedure linkage table entry
1168 after all. */
1169
1170 /* If this is a local symbol, we resolve it directly without
1171 creating a procedure linkage table entry. */
1172 if (h == NULL)
1173 continue;
1174
1175 h->needs_plt = 1;
1176 h->plt.refcount += 1;
1177 break;
1178
1179 case R_X86_64_PLTOFF64:
1180 /* This tries to form the 'address' of a function relative
1181 to GOT. For global symbols we need a PLT entry. */
1182 if (h != NULL)
1183 {
1184 h->needs_plt = 1;
1185 h->plt.refcount += 1;
1186 }
1187 goto create_got;
1188
1189 case R_X86_64_8:
1190 case R_X86_64_16:
1191 case R_X86_64_32:
1192 case R_X86_64_32S:
1193 /* Let's help debug shared library creation. These relocs
1194 cannot be used in shared libs. Don't error out for
1195 sections we don't care about, such as debug sections or
1196 non-constant sections. */
1197 if (info->shared
1198 && (sec->flags & SEC_ALLOC) != 0
1199 && (sec->flags & SEC_READONLY) != 0)
1200 {
1201 (*_bfd_error_handler)
1202 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1203 abfd,
1204 x86_64_elf_howto_table[r_type].name,
1205 (h) ? h->root.root.string : "a local symbol");
1206 bfd_set_error (bfd_error_bad_value);
1207 return FALSE;
1208 }
1209 /* Fall through. */
1210
1211 case R_X86_64_PC8:
1212 case R_X86_64_PC16:
1213 case R_X86_64_PC32:
1214 case R_X86_64_PC64:
1215 case R_X86_64_64:
1216 if (h != NULL && !info->shared)
1217 {
1218 /* If this reloc is in a read-only section, we might
1219 need a copy reloc. We can't check reliably at this
1220 stage whether the section is read-only, as input
1221 sections have not yet been mapped to output sections.
1222 Tentatively set the flag for now, and correct in
1223 adjust_dynamic_symbol. */
1224 h->non_got_ref = 1;
1225
1226 /* We may need a .plt entry if the function this reloc
1227 refers to is in a shared lib. */
1228 h->plt.refcount += 1;
1229 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1230 h->pointer_equality_needed = 1;
1231 }
1232
1233 /* If we are creating a shared library, and this is a reloc
1234 against a global symbol, or a non PC relative reloc
1235 against a local symbol, then we need to copy the reloc
1236 into the shared library. However, if we are linking with
1237 -Bsymbolic, we do not need to copy a reloc against a
1238 global symbol which is defined in an object we are
1239 including in the link (i.e., DEF_REGULAR is set). At
1240 this point we have not seen all the input files, so it is
1241 possible that DEF_REGULAR is not set now but will be set
1242 later (it is never cleared). In case of a weak definition,
1243 DEF_REGULAR may be cleared later by a strong definition in
1244 a shared library. We account for that possibility below by
1245 storing information in the relocs_copied field of the hash
1246 table entry. A similar situation occurs when creating
1247 shared libraries and symbol visibility changes render the
1248 symbol local.
1249
1250 If on the other hand, we are creating an executable, we
1251 may need to keep relocations for symbols satisfied by a
1252 dynamic library if we manage to avoid copy relocs for the
1253 symbol. */
1254 if ((info->shared
1255 && (sec->flags & SEC_ALLOC) != 0
1256 && (((r_type != R_X86_64_PC8)
1257 && (r_type != R_X86_64_PC16)
1258 && (r_type != R_X86_64_PC32)
1259 && (r_type != R_X86_64_PC64))
1260 || (h != NULL
1261 && (! SYMBOLIC_BIND (info, h)
1262 || h->root.type == bfd_link_hash_defweak
1263 || !h->def_regular))))
1264 || (ELIMINATE_COPY_RELOCS
1265 && !info->shared
1266 && (sec->flags & SEC_ALLOC) != 0
1267 && h != NULL
1268 && (h->root.type == bfd_link_hash_defweak
1269 || !h->def_regular)))
1270 {
1271 struct elf64_x86_64_dyn_relocs *p;
1272 struct elf64_x86_64_dyn_relocs **head;
1273
1274 /* We must copy these reloc types into the output file.
1275 Create a reloc section in dynobj and make room for
1276 this reloc. */
1277 if (sreloc == NULL)
1278 {
1279 const char *name;
1280 bfd *dynobj;
1281
1282 name = (bfd_elf_string_from_elf_section
1283 (abfd,
1284 elf_elfheader (abfd)->e_shstrndx,
1285 elf_section_data (sec)->rel_hdr.sh_name));
1286 if (name == NULL)
1287 return FALSE;
1288
1289 if (! CONST_STRNEQ (name, ".rela")
1290 || strcmp (bfd_get_section_name (abfd, sec),
1291 name + 5) != 0)
1292 {
1293 (*_bfd_error_handler)
1294 (_("%B: bad relocation section name `%s\'"),
1295 abfd, name);
1296 }
1297
1298 if (htab->elf.dynobj == NULL)
1299 htab->elf.dynobj = abfd;
1300
1301 dynobj = htab->elf.dynobj;
1302
1303 sreloc = bfd_get_section_by_name (dynobj, name);
1304 if (sreloc == NULL)
1305 {
1306 flagword flags;
1307
1308 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1309 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1310 if ((sec->flags & SEC_ALLOC) != 0)
1311 flags |= SEC_ALLOC | SEC_LOAD;
1312 sreloc = bfd_make_section_with_flags (dynobj,
1313 name,
1314 flags);
1315 if (sreloc == NULL
1316 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
1317 return FALSE;
1318 }
1319 elf_section_data (sec)->sreloc = sreloc;
1320 }
1321
1322 /* If this is a global symbol, we count the number of
1323 relocations we need for this symbol. */
1324 if (h != NULL)
1325 {
1326 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1327 }
1328 else
1329 {
1330 void **vpp;
1331 /* Track dynamic relocs needed for local syms too.
1332 We really need local syms available to do this
1333 easily. Oh well. */
1334
1335 asection *s;
1336 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1337 sec, r_symndx);
1338 if (s == NULL)
1339 return FALSE;
1340
1341 /* Beware of type punned pointers vs strict aliasing
1342 rules. */
1343 vpp = &(elf_section_data (s)->local_dynrel);
1344 head = (struct elf64_x86_64_dyn_relocs **)vpp;
1345 }
1346
1347 p = *head;
1348 if (p == NULL || p->sec != sec)
1349 {
1350 bfd_size_type amt = sizeof *p;
1351 p = ((struct elf64_x86_64_dyn_relocs *)
1352 bfd_alloc (htab->elf.dynobj, amt));
1353 if (p == NULL)
1354 return FALSE;
1355 p->next = *head;
1356 *head = p;
1357 p->sec = sec;
1358 p->count = 0;
1359 p->pc_count = 0;
1360 }
1361
1362 p->count += 1;
1363 if (r_type == R_X86_64_PC8
1364 || r_type == R_X86_64_PC16
1365 || r_type == R_X86_64_PC32
1366 || r_type == R_X86_64_PC64)
1367 p->pc_count += 1;
1368 }
1369 break;
1370
1371 /* This relocation describes the C++ object vtable hierarchy.
1372 Reconstruct it for later use during GC. */
1373 case R_X86_64_GNU_VTINHERIT:
1374 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1375 return FALSE;
1376 break;
1377
1378 /* This relocation describes which C++ vtable entries are actually
1379 used. Record for later use during GC. */
1380 case R_X86_64_GNU_VTENTRY:
1381 BFD_ASSERT (h != NULL);
1382 if (h != NULL
1383 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1384 return FALSE;
1385 break;
1386
1387 default:
1388 break;
1389 }
1390 }
1391
1392 return TRUE;
1393 }
1394
1395 /* Return the section that should be marked against GC for a given
1396 relocation. */
1397
1398 static asection *
1399 elf64_x86_64_gc_mark_hook (asection *sec,
1400 struct bfd_link_info *info,
1401 Elf_Internal_Rela *rel,
1402 struct elf_link_hash_entry *h,
1403 Elf_Internal_Sym *sym)
1404 {
1405 if (h != NULL)
1406 switch (ELF64_R_TYPE (rel->r_info))
1407 {
1408 case R_X86_64_GNU_VTINHERIT:
1409 case R_X86_64_GNU_VTENTRY:
1410 return NULL;
1411 }
1412
1413 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1414 }
1415
1416 /* Update the got entry reference counts for the section being removed. */
1417
1418 static bfd_boolean
1419 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1420 asection *sec,
1421 const Elf_Internal_Rela *relocs)
1422 {
1423 Elf_Internal_Shdr *symtab_hdr;
1424 struct elf_link_hash_entry **sym_hashes;
1425 bfd_signed_vma *local_got_refcounts;
1426 const Elf_Internal_Rela *rel, *relend;
1427
1428 if (info->relocatable)
1429 return TRUE;
1430
1431 elf_section_data (sec)->local_dynrel = NULL;
1432
1433 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1434 sym_hashes = elf_sym_hashes (abfd);
1435 local_got_refcounts = elf_local_got_refcounts (abfd);
1436
1437 relend = relocs + sec->reloc_count;
1438 for (rel = relocs; rel < relend; rel++)
1439 {
1440 unsigned long r_symndx;
1441 unsigned int r_type;
1442 struct elf_link_hash_entry *h = NULL;
1443
1444 r_symndx = ELF64_R_SYM (rel->r_info);
1445 if (r_symndx >= symtab_hdr->sh_info)
1446 {
1447 struct elf64_x86_64_link_hash_entry *eh;
1448 struct elf64_x86_64_dyn_relocs **pp;
1449 struct elf64_x86_64_dyn_relocs *p;
1450
1451 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1452 while (h->root.type == bfd_link_hash_indirect
1453 || h->root.type == bfd_link_hash_warning)
1454 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1455 eh = (struct elf64_x86_64_link_hash_entry *) h;
1456
1457 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1458 if (p->sec == sec)
1459 {
1460 /* Everything must go for SEC. */
1461 *pp = p->next;
1462 break;
1463 }
1464 }
1465
1466 r_type = ELF64_R_TYPE (rel->r_info);
1467 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1468 symtab_hdr, sym_hashes,
1469 &r_type, GOT_UNKNOWN,
1470 rel, relend, h))
1471 return FALSE;
1472
1473 switch (r_type)
1474 {
1475 case R_X86_64_TLSLD:
1476 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1477 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1478 break;
1479
1480 case R_X86_64_TLSGD:
1481 case R_X86_64_GOTPC32_TLSDESC:
1482 case R_X86_64_TLSDESC_CALL:
1483 case R_X86_64_GOTTPOFF:
1484 case R_X86_64_GOT32:
1485 case R_X86_64_GOTPCREL:
1486 case R_X86_64_GOT64:
1487 case R_X86_64_GOTPCREL64:
1488 case R_X86_64_GOTPLT64:
1489 if (h != NULL)
1490 {
1491 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1492 h->plt.refcount -= 1;
1493 if (h->got.refcount > 0)
1494 h->got.refcount -= 1;
1495 }
1496 else if (local_got_refcounts != NULL)
1497 {
1498 if (local_got_refcounts[r_symndx] > 0)
1499 local_got_refcounts[r_symndx] -= 1;
1500 }
1501 break;
1502
1503 case R_X86_64_8:
1504 case R_X86_64_16:
1505 case R_X86_64_32:
1506 case R_X86_64_64:
1507 case R_X86_64_32S:
1508 case R_X86_64_PC8:
1509 case R_X86_64_PC16:
1510 case R_X86_64_PC32:
1511 case R_X86_64_PC64:
1512 if (info->shared)
1513 break;
1514 /* Fall thru */
1515
1516 case R_X86_64_PLT32:
1517 case R_X86_64_PLTOFF64:
1518 if (h != NULL)
1519 {
1520 if (h->plt.refcount > 0)
1521 h->plt.refcount -= 1;
1522 }
1523 break;
1524
1525 default:
1526 break;
1527 }
1528 }
1529
1530 return TRUE;
1531 }
1532
1533 /* Adjust a symbol defined by a dynamic object and referenced by a
1534 regular object. The current definition is in some section of the
1535 dynamic object, but we're not including those sections. We have to
1536 change the definition to something the rest of the link can
1537 understand. */
1538
1539 static bfd_boolean
1540 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1541 struct elf_link_hash_entry *h)
1542 {
1543 struct elf64_x86_64_link_hash_table *htab;
1544 asection *s;
1545
1546 /* If this is a function, put it in the procedure linkage table. We
1547 will fill in the contents of the procedure linkage table later,
1548 when we know the address of the .got section. */
1549 if (h->type == STT_FUNC
1550 || h->needs_plt)
1551 {
1552 if (h->plt.refcount <= 0
1553 || SYMBOL_CALLS_LOCAL (info, h)
1554 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1555 && h->root.type == bfd_link_hash_undefweak))
1556 {
1557 /* This case can occur if we saw a PLT32 reloc in an input
1558 file, but the symbol was never referred to by a dynamic
1559 object, or if all references were garbage collected. In
1560 such a case, we don't actually need to build a procedure
1561 linkage table, and we can just do a PC32 reloc instead. */
1562 h->plt.offset = (bfd_vma) -1;
1563 h->needs_plt = 0;
1564 }
1565
1566 return TRUE;
1567 }
1568 else
1569 /* It's possible that we incorrectly decided a .plt reloc was
1570 needed for an R_X86_64_PC32 reloc to a non-function sym in
1571 check_relocs. We can't decide accurately between function and
1572 non-function syms in check-relocs; Objects loaded later in
1573 the link may change h->type. So fix it now. */
1574 h->plt.offset = (bfd_vma) -1;
1575
1576 /* If this is a weak symbol, and there is a real definition, the
1577 processor independent code will have arranged for us to see the
1578 real definition first, and we can just use the same value. */
1579 if (h->u.weakdef != NULL)
1580 {
1581 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1582 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1583 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1584 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1585 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1586 h->non_got_ref = h->u.weakdef->non_got_ref;
1587 return TRUE;
1588 }
1589
1590 /* This is a reference to a symbol defined by a dynamic object which
1591 is not a function. */
1592
1593 /* If we are creating a shared library, we must presume that the
1594 only references to the symbol are via the global offset table.
1595 For such cases we need not do anything here; the relocations will
1596 be handled correctly by relocate_section. */
1597 if (info->shared)
1598 return TRUE;
1599
1600 /* If there are no references to this symbol that do not use the
1601 GOT, we don't need to generate a copy reloc. */
1602 if (!h->non_got_ref)
1603 return TRUE;
1604
1605 /* If -z nocopyreloc was given, we won't generate them either. */
1606 if (info->nocopyreloc)
1607 {
1608 h->non_got_ref = 0;
1609 return TRUE;
1610 }
1611
1612 if (ELIMINATE_COPY_RELOCS)
1613 {
1614 struct elf64_x86_64_link_hash_entry * eh;
1615 struct elf64_x86_64_dyn_relocs *p;
1616
1617 eh = (struct elf64_x86_64_link_hash_entry *) h;
1618 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1619 {
1620 s = p->sec->output_section;
1621 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1622 break;
1623 }
1624
1625 /* If we didn't find any dynamic relocs in read-only sections, then
1626 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1627 if (p == NULL)
1628 {
1629 h->non_got_ref = 0;
1630 return TRUE;
1631 }
1632 }
1633
1634 if (h->size == 0)
1635 {
1636 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1637 h->root.root.string);
1638 return TRUE;
1639 }
1640
1641 /* We must allocate the symbol in our .dynbss section, which will
1642 become part of the .bss section of the executable. There will be
1643 an entry for this symbol in the .dynsym section. The dynamic
1644 object will contain position independent code, so all references
1645 from the dynamic object to this symbol will go through the global
1646 offset table. The dynamic linker will use the .dynsym entry to
1647 determine the address it must put in the global offset table, so
1648 both the dynamic object and the regular object will refer to the
1649 same memory location for the variable. */
1650
1651 htab = elf64_x86_64_hash_table (info);
1652
1653 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1654 to copy the initial value out of the dynamic object and into the
1655 runtime process image. */
1656 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1657 {
1658 htab->srelbss->size += sizeof (Elf64_External_Rela);
1659 h->needs_copy = 1;
1660 }
1661
1662 s = htab->sdynbss;
1663
1664 return _bfd_elf_adjust_dynamic_copy (h, s);
1665 }
1666
1667 /* Allocate space in .plt, .got and associated reloc sections for
1668 dynamic relocs. */
1669
1670 static bfd_boolean
1671 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1672 {
1673 struct bfd_link_info *info;
1674 struct elf64_x86_64_link_hash_table *htab;
1675 struct elf64_x86_64_link_hash_entry *eh;
1676 struct elf64_x86_64_dyn_relocs *p;
1677
1678 if (h->root.type == bfd_link_hash_indirect)
1679 return TRUE;
1680
1681 if (h->root.type == bfd_link_hash_warning)
1682 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1683
1684 info = (struct bfd_link_info *) inf;
1685 htab = elf64_x86_64_hash_table (info);
1686
1687 if (htab->elf.dynamic_sections_created
1688 && h->plt.refcount > 0)
1689 {
1690 /* Make sure this symbol is output as a dynamic symbol.
1691 Undefined weak syms won't yet be marked as dynamic. */
1692 if (h->dynindx == -1
1693 && !h->forced_local)
1694 {
1695 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1696 return FALSE;
1697 }
1698
1699 if (info->shared
1700 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1701 {
1702 asection *s = htab->splt;
1703
1704 /* If this is the first .plt entry, make room for the special
1705 first entry. */
1706 if (s->size == 0)
1707 s->size += PLT_ENTRY_SIZE;
1708
1709 h->plt.offset = s->size;
1710
1711 /* If this symbol is not defined in a regular file, and we are
1712 not generating a shared library, then set the symbol to this
1713 location in the .plt. This is required to make function
1714 pointers compare as equal between the normal executable and
1715 the shared library. */
1716 if (! info->shared
1717 && !h->def_regular)
1718 {
1719 h->root.u.def.section = s;
1720 h->root.u.def.value = h->plt.offset;
1721 }
1722
1723 /* Make room for this entry. */
1724 s->size += PLT_ENTRY_SIZE;
1725
1726 /* We also need to make an entry in the .got.plt section, which
1727 will be placed in the .got section by the linker script. */
1728 htab->sgotplt->size += GOT_ENTRY_SIZE;
1729
1730 /* We also need to make an entry in the .rela.plt section. */
1731 htab->srelplt->size += sizeof (Elf64_External_Rela);
1732 htab->srelplt->reloc_count++;
1733 }
1734 else
1735 {
1736 h->plt.offset = (bfd_vma) -1;
1737 h->needs_plt = 0;
1738 }
1739 }
1740 else
1741 {
1742 h->plt.offset = (bfd_vma) -1;
1743 h->needs_plt = 0;
1744 }
1745
1746 eh = (struct elf64_x86_64_link_hash_entry *) h;
1747 eh->tlsdesc_got = (bfd_vma) -1;
1748
1749 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1750 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1751 if (h->got.refcount > 0
1752 && !info->shared
1753 && h->dynindx == -1
1754 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1755 h->got.offset = (bfd_vma) -1;
1756 else if (h->got.refcount > 0)
1757 {
1758 asection *s;
1759 bfd_boolean dyn;
1760 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1761
1762 /* Make sure this symbol is output as a dynamic symbol.
1763 Undefined weak syms won't yet be marked as dynamic. */
1764 if (h->dynindx == -1
1765 && !h->forced_local)
1766 {
1767 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1768 return FALSE;
1769 }
1770
1771 if (GOT_TLS_GDESC_P (tls_type))
1772 {
1773 eh->tlsdesc_got = htab->sgotplt->size
1774 - elf64_x86_64_compute_jump_table_size (htab);
1775 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1776 h->got.offset = (bfd_vma) -2;
1777 }
1778 if (! GOT_TLS_GDESC_P (tls_type)
1779 || GOT_TLS_GD_P (tls_type))
1780 {
1781 s = htab->sgot;
1782 h->got.offset = s->size;
1783 s->size += GOT_ENTRY_SIZE;
1784 if (GOT_TLS_GD_P (tls_type))
1785 s->size += GOT_ENTRY_SIZE;
1786 }
1787 dyn = htab->elf.dynamic_sections_created;
1788 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1789 and two if global.
1790 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1791 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1792 || tls_type == GOT_TLS_IE)
1793 htab->srelgot->size += sizeof (Elf64_External_Rela);
1794 else if (GOT_TLS_GD_P (tls_type))
1795 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1796 else if (! GOT_TLS_GDESC_P (tls_type)
1797 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1798 || h->root.type != bfd_link_hash_undefweak)
1799 && (info->shared
1800 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1801 htab->srelgot->size += sizeof (Elf64_External_Rela);
1802 if (GOT_TLS_GDESC_P (tls_type))
1803 {
1804 htab->srelplt->size += sizeof (Elf64_External_Rela);
1805 htab->tlsdesc_plt = (bfd_vma) -1;
1806 }
1807 }
1808 else
1809 h->got.offset = (bfd_vma) -1;
1810
1811 if (eh->dyn_relocs == NULL)
1812 return TRUE;
1813
1814 /* In the shared -Bsymbolic case, discard space allocated for
1815 dynamic pc-relative relocs against symbols which turn out to be
1816 defined in regular objects. For the normal shared case, discard
1817 space for pc-relative relocs that have become local due to symbol
1818 visibility changes. */
1819
1820 if (info->shared)
1821 {
1822 /* Relocs that use pc_count are those that appear on a call
1823 insn, or certain REL relocs that can generated via assembly.
1824 We want calls to protected symbols to resolve directly to the
1825 function rather than going via the plt. If people want
1826 function pointer comparisons to work as expected then they
1827 should avoid writing weird assembly. */
1828 if (SYMBOL_CALLS_LOCAL (info, h))
1829 {
1830 struct elf64_x86_64_dyn_relocs **pp;
1831
1832 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1833 {
1834 p->count -= p->pc_count;
1835 p->pc_count = 0;
1836 if (p->count == 0)
1837 *pp = p->next;
1838 else
1839 pp = &p->next;
1840 }
1841 }
1842
1843 /* Also discard relocs on undefined weak syms with non-default
1844 visibility. */
1845 if (eh->dyn_relocs != NULL
1846 && h->root.type == bfd_link_hash_undefweak)
1847 {
1848 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1849 eh->dyn_relocs = NULL;
1850
1851 /* Make sure undefined weak symbols are output as a dynamic
1852 symbol in PIEs. */
1853 else if (h->dynindx == -1
1854 && !h->forced_local)
1855 {
1856 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1857 return FALSE;
1858 }
1859 }
1860 }
1861 else if (ELIMINATE_COPY_RELOCS)
1862 {
1863 /* For the non-shared case, discard space for relocs against
1864 symbols which turn out to need copy relocs or are not
1865 dynamic. */
1866
1867 if (!h->non_got_ref
1868 && ((h->def_dynamic
1869 && !h->def_regular)
1870 || (htab->elf.dynamic_sections_created
1871 && (h->root.type == bfd_link_hash_undefweak
1872 || h->root.type == bfd_link_hash_undefined))))
1873 {
1874 /* Make sure this symbol is output as a dynamic symbol.
1875 Undefined weak syms won't yet be marked as dynamic. */
1876 if (h->dynindx == -1
1877 && !h->forced_local)
1878 {
1879 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1880 return FALSE;
1881 }
1882
1883 /* If that succeeded, we know we'll be keeping all the
1884 relocs. */
1885 if (h->dynindx != -1)
1886 goto keep;
1887 }
1888
1889 eh->dyn_relocs = NULL;
1890
1891 keep: ;
1892 }
1893
1894 /* Finally, allocate space. */
1895 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1896 {
1897 asection *sreloc = elf_section_data (p->sec)->sreloc;
1898 sreloc->size += p->count * sizeof (Elf64_External_Rela);
1899 }
1900
1901 return TRUE;
1902 }
1903
1904 /* Find any dynamic relocs that apply to read-only sections. */
1905
1906 static bfd_boolean
1907 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1908 {
1909 struct elf64_x86_64_link_hash_entry *eh;
1910 struct elf64_x86_64_dyn_relocs *p;
1911
1912 if (h->root.type == bfd_link_hash_warning)
1913 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1914
1915 eh = (struct elf64_x86_64_link_hash_entry *) h;
1916 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1917 {
1918 asection *s = p->sec->output_section;
1919
1920 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1921 {
1922 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1923
1924 info->flags |= DF_TEXTREL;
1925
1926 /* Not an error, just cut short the traversal. */
1927 return FALSE;
1928 }
1929 }
1930 return TRUE;
1931 }
1932
1933 /* Set the sizes of the dynamic sections. */
1934
1935 static bfd_boolean
1936 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1937 struct bfd_link_info *info)
1938 {
1939 struct elf64_x86_64_link_hash_table *htab;
1940 bfd *dynobj;
1941 asection *s;
1942 bfd_boolean relocs;
1943 bfd *ibfd;
1944
1945 htab = elf64_x86_64_hash_table (info);
1946 dynobj = htab->elf.dynobj;
1947 if (dynobj == NULL)
1948 abort ();
1949
1950 if (htab->elf.dynamic_sections_created)
1951 {
1952 /* Set the contents of the .interp section to the interpreter. */
1953 if (info->executable)
1954 {
1955 s = bfd_get_section_by_name (dynobj, ".interp");
1956 if (s == NULL)
1957 abort ();
1958 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1959 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1960 }
1961 }
1962
1963 /* Set up .got offsets for local syms, and space for local dynamic
1964 relocs. */
1965 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1966 {
1967 bfd_signed_vma *local_got;
1968 bfd_signed_vma *end_local_got;
1969 char *local_tls_type;
1970 bfd_vma *local_tlsdesc_gotent;
1971 bfd_size_type locsymcount;
1972 Elf_Internal_Shdr *symtab_hdr;
1973 asection *srel;
1974
1975 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1976 continue;
1977
1978 for (s = ibfd->sections; s != NULL; s = s->next)
1979 {
1980 struct elf64_x86_64_dyn_relocs *p;
1981
1982 for (p = (struct elf64_x86_64_dyn_relocs *)
1983 (elf_section_data (s)->local_dynrel);
1984 p != NULL;
1985 p = p->next)
1986 {
1987 if (!bfd_is_abs_section (p->sec)
1988 && bfd_is_abs_section (p->sec->output_section))
1989 {
1990 /* Input section has been discarded, either because
1991 it is a copy of a linkonce section or due to
1992 linker script /DISCARD/, so we'll be discarding
1993 the relocs too. */
1994 }
1995 else if (p->count != 0)
1996 {
1997 srel = elf_section_data (p->sec)->sreloc;
1998 srel->size += p->count * sizeof (Elf64_External_Rela);
1999 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2000 info->flags |= DF_TEXTREL;
2001
2002 }
2003 }
2004 }
2005
2006 local_got = elf_local_got_refcounts (ibfd);
2007 if (!local_got)
2008 continue;
2009
2010 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2011 locsymcount = symtab_hdr->sh_info;
2012 end_local_got = local_got + locsymcount;
2013 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2014 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2015 s = htab->sgot;
2016 srel = htab->srelgot;
2017 for (; local_got < end_local_got;
2018 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2019 {
2020 *local_tlsdesc_gotent = (bfd_vma) -1;
2021 if (*local_got > 0)
2022 {
2023 if (GOT_TLS_GDESC_P (*local_tls_type))
2024 {
2025 *local_tlsdesc_gotent = htab->sgotplt->size
2026 - elf64_x86_64_compute_jump_table_size (htab);
2027 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
2028 *local_got = (bfd_vma) -2;
2029 }
2030 if (! GOT_TLS_GDESC_P (*local_tls_type)
2031 || GOT_TLS_GD_P (*local_tls_type))
2032 {
2033 *local_got = s->size;
2034 s->size += GOT_ENTRY_SIZE;
2035 if (GOT_TLS_GD_P (*local_tls_type))
2036 s->size += GOT_ENTRY_SIZE;
2037 }
2038 if (info->shared
2039 || GOT_TLS_GD_ANY_P (*local_tls_type)
2040 || *local_tls_type == GOT_TLS_IE)
2041 {
2042 if (GOT_TLS_GDESC_P (*local_tls_type))
2043 {
2044 htab->srelplt->size += sizeof (Elf64_External_Rela);
2045 htab->tlsdesc_plt = (bfd_vma) -1;
2046 }
2047 if (! GOT_TLS_GDESC_P (*local_tls_type)
2048 || GOT_TLS_GD_P (*local_tls_type))
2049 srel->size += sizeof (Elf64_External_Rela);
2050 }
2051 }
2052 else
2053 *local_got = (bfd_vma) -1;
2054 }
2055 }
2056
2057 if (htab->tls_ld_got.refcount > 0)
2058 {
2059 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2060 relocs. */
2061 htab->tls_ld_got.offset = htab->sgot->size;
2062 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
2063 htab->srelgot->size += sizeof (Elf64_External_Rela);
2064 }
2065 else
2066 htab->tls_ld_got.offset = -1;
2067
2068 /* Allocate global sym .plt and .got entries, and space for global
2069 sym dynamic relocs. */
2070 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2071
2072 /* For every jump slot reserved in the sgotplt, reloc_count is
2073 incremented. However, when we reserve space for TLS descriptors,
2074 it's not incremented, so in order to compute the space reserved
2075 for them, it suffices to multiply the reloc count by the jump
2076 slot size. */
2077 if (htab->srelplt)
2078 htab->sgotplt_jump_table_size
2079 = elf64_x86_64_compute_jump_table_size (htab);
2080
2081 if (htab->tlsdesc_plt)
2082 {
2083 /* If we're not using lazy TLS relocations, don't generate the
2084 PLT and GOT entries they require. */
2085 if ((info->flags & DF_BIND_NOW))
2086 htab->tlsdesc_plt = 0;
2087 else
2088 {
2089 htab->tlsdesc_got = htab->sgot->size;
2090 htab->sgot->size += GOT_ENTRY_SIZE;
2091 /* Reserve room for the initial entry.
2092 FIXME: we could probably do away with it in this case. */
2093 if (htab->splt->size == 0)
2094 htab->splt->size += PLT_ENTRY_SIZE;
2095 htab->tlsdesc_plt = htab->splt->size;
2096 htab->splt->size += PLT_ENTRY_SIZE;
2097 }
2098 }
2099
2100 /* We now have determined the sizes of the various dynamic sections.
2101 Allocate memory for them. */
2102 relocs = FALSE;
2103 for (s = dynobj->sections; s != NULL; s = s->next)
2104 {
2105 if ((s->flags & SEC_LINKER_CREATED) == 0)
2106 continue;
2107
2108 if (s == htab->splt
2109 || s == htab->sgot
2110 || s == htab->sgotplt
2111 || s == htab->sdynbss)
2112 {
2113 /* Strip this section if we don't need it; see the
2114 comment below. */
2115 }
2116 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2117 {
2118 if (s->size != 0 && s != htab->srelplt)
2119 relocs = TRUE;
2120
2121 /* We use the reloc_count field as a counter if we need
2122 to copy relocs into the output file. */
2123 if (s != htab->srelplt)
2124 s->reloc_count = 0;
2125 }
2126 else
2127 {
2128 /* It's not one of our sections, so don't allocate space. */
2129 continue;
2130 }
2131
2132 if (s->size == 0)
2133 {
2134 /* If we don't need this section, strip it from the
2135 output file. This is mostly to handle .rela.bss and
2136 .rela.plt. We must create both sections in
2137 create_dynamic_sections, because they must be created
2138 before the linker maps input sections to output
2139 sections. The linker does that before
2140 adjust_dynamic_symbol is called, and it is that
2141 function which decides whether anything needs to go
2142 into these sections. */
2143
2144 s->flags |= SEC_EXCLUDE;
2145 continue;
2146 }
2147
2148 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2149 continue;
2150
2151 /* Allocate memory for the section contents. We use bfd_zalloc
2152 here in case unused entries are not reclaimed before the
2153 section's contents are written out. This should not happen,
2154 but this way if it does, we get a R_X86_64_NONE reloc instead
2155 of garbage. */
2156 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2157 if (s->contents == NULL)
2158 return FALSE;
2159 }
2160
2161 if (htab->elf.dynamic_sections_created)
2162 {
2163 /* Add some entries to the .dynamic section. We fill in the
2164 values later, in elf64_x86_64_finish_dynamic_sections, but we
2165 must add the entries now so that we get the correct size for
2166 the .dynamic section. The DT_DEBUG entry is filled in by the
2167 dynamic linker and used by the debugger. */
2168 #define add_dynamic_entry(TAG, VAL) \
2169 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2170
2171 if (info->executable)
2172 {
2173 if (!add_dynamic_entry (DT_DEBUG, 0))
2174 return FALSE;
2175 }
2176
2177 if (htab->splt->size != 0)
2178 {
2179 if (!add_dynamic_entry (DT_PLTGOT, 0)
2180 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2181 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2182 || !add_dynamic_entry (DT_JMPREL, 0))
2183 return FALSE;
2184
2185 if (htab->tlsdesc_plt
2186 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2187 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2188 return FALSE;
2189 }
2190
2191 if (relocs)
2192 {
2193 if (!add_dynamic_entry (DT_RELA, 0)
2194 || !add_dynamic_entry (DT_RELASZ, 0)
2195 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2196 return FALSE;
2197
2198 /* If any dynamic relocs apply to a read-only section,
2199 then we need a DT_TEXTREL entry. */
2200 if ((info->flags & DF_TEXTREL) == 0)
2201 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2202 (PTR) info);
2203
2204 if ((info->flags & DF_TEXTREL) != 0)
2205 {
2206 if (!add_dynamic_entry (DT_TEXTREL, 0))
2207 return FALSE;
2208 }
2209 }
2210 }
2211 #undef add_dynamic_entry
2212
2213 return TRUE;
2214 }
2215
2216 static bfd_boolean
2217 elf64_x86_64_always_size_sections (bfd *output_bfd,
2218 struct bfd_link_info *info)
2219 {
2220 asection *tls_sec = elf_hash_table (info)->tls_sec;
2221
2222 if (tls_sec)
2223 {
2224 struct elf_link_hash_entry *tlsbase;
2225
2226 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2227 "_TLS_MODULE_BASE_",
2228 FALSE, FALSE, FALSE);
2229
2230 if (tlsbase && tlsbase->type == STT_TLS)
2231 {
2232 struct bfd_link_hash_entry *bh = NULL;
2233 const struct elf_backend_data *bed
2234 = get_elf_backend_data (output_bfd);
2235
2236 if (!(_bfd_generic_link_add_one_symbol
2237 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2238 tls_sec, 0, NULL, FALSE,
2239 bed->collect, &bh)))
2240 return FALSE;
2241 tlsbase = (struct elf_link_hash_entry *)bh;
2242 tlsbase->def_regular = 1;
2243 tlsbase->other = STV_HIDDEN;
2244 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2245 }
2246 }
2247
2248 return TRUE;
2249 }
2250
2251 /* Return the base VMA address which should be subtracted from real addresses
2252 when resolving @dtpoff relocation.
2253 This is PT_TLS segment p_vaddr. */
2254
2255 static bfd_vma
2256 dtpoff_base (struct bfd_link_info *info)
2257 {
2258 /* If tls_sec is NULL, we should have signalled an error already. */
2259 if (elf_hash_table (info)->tls_sec == NULL)
2260 return 0;
2261 return elf_hash_table (info)->tls_sec->vma;
2262 }
2263
2264 /* Return the relocation value for @tpoff relocation
2265 if STT_TLS virtual address is ADDRESS. */
2266
2267 static bfd_vma
2268 tpoff (struct bfd_link_info *info, bfd_vma address)
2269 {
2270 struct elf_link_hash_table *htab = elf_hash_table (info);
2271
2272 /* If tls_segment is NULL, we should have signalled an error already. */
2273 if (htab->tls_sec == NULL)
2274 return 0;
2275 return address - htab->tls_size - htab->tls_sec->vma;
2276 }
2277
2278 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2279 branch? */
2280
2281 static bfd_boolean
2282 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2283 {
2284 /* Opcode Instruction
2285 0xe8 call
2286 0xe9 jump
2287 0x0f 0x8x conditional jump */
2288 return ((offset > 0
2289 && (contents [offset - 1] == 0xe8
2290 || contents [offset - 1] == 0xe9))
2291 || (offset > 1
2292 && contents [offset - 2] == 0x0f
2293 && (contents [offset - 1] & 0xf0) == 0x80));
2294 }
2295
2296 /* Relocate an x86_64 ELF section. */
2297
2298 static bfd_boolean
2299 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2300 bfd *input_bfd, asection *input_section,
2301 bfd_byte *contents, Elf_Internal_Rela *relocs,
2302 Elf_Internal_Sym *local_syms,
2303 asection **local_sections)
2304 {
2305 struct elf64_x86_64_link_hash_table *htab;
2306 Elf_Internal_Shdr *symtab_hdr;
2307 struct elf_link_hash_entry **sym_hashes;
2308 bfd_vma *local_got_offsets;
2309 bfd_vma *local_tlsdesc_gotents;
2310 Elf_Internal_Rela *rel;
2311 Elf_Internal_Rela *relend;
2312
2313 htab = elf64_x86_64_hash_table (info);
2314 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2315 sym_hashes = elf_sym_hashes (input_bfd);
2316 local_got_offsets = elf_local_got_offsets (input_bfd);
2317 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2318
2319 rel = relocs;
2320 relend = relocs + input_section->reloc_count;
2321 for (; rel < relend; rel++)
2322 {
2323 unsigned int r_type;
2324 reloc_howto_type *howto;
2325 unsigned long r_symndx;
2326 struct elf_link_hash_entry *h;
2327 Elf_Internal_Sym *sym;
2328 asection *sec;
2329 bfd_vma off, offplt;
2330 bfd_vma relocation;
2331 bfd_boolean unresolved_reloc;
2332 bfd_reloc_status_type r;
2333 int tls_type;
2334
2335 r_type = ELF64_R_TYPE (rel->r_info);
2336 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2337 || r_type == (int) R_X86_64_GNU_VTENTRY)
2338 continue;
2339
2340 if (r_type >= R_X86_64_max)
2341 {
2342 bfd_set_error (bfd_error_bad_value);
2343 return FALSE;
2344 }
2345
2346 howto = x86_64_elf_howto_table + r_type;
2347 r_symndx = ELF64_R_SYM (rel->r_info);
2348 h = NULL;
2349 sym = NULL;
2350 sec = NULL;
2351 unresolved_reloc = FALSE;
2352 if (r_symndx < symtab_hdr->sh_info)
2353 {
2354 sym = local_syms + r_symndx;
2355 sec = local_sections[r_symndx];
2356
2357 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2358 }
2359 else
2360 {
2361 bfd_boolean warned;
2362
2363 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2364 r_symndx, symtab_hdr, sym_hashes,
2365 h, sec, relocation,
2366 unresolved_reloc, warned);
2367 }
2368
2369 if (sec != NULL && elf_discarded_section (sec))
2370 {
2371 /* For relocs against symbols from removed linkonce sections,
2372 or sections discarded by a linker script, we just want the
2373 section contents zeroed. Avoid any special processing. */
2374 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2375 rel->r_info = 0;
2376 rel->r_addend = 0;
2377 continue;
2378 }
2379
2380 if (info->relocatable)
2381 continue;
2382
2383 /* When generating a shared object, the relocations handled here are
2384 copied into the output file to be resolved at run time. */
2385 switch (r_type)
2386 {
2387 asection *base_got;
2388 case R_X86_64_GOT32:
2389 case R_X86_64_GOT64:
2390 /* Relocation is to the entry for this symbol in the global
2391 offset table. */
2392 case R_X86_64_GOTPCREL:
2393 case R_X86_64_GOTPCREL64:
2394 /* Use global offset table entry as symbol value. */
2395 case R_X86_64_GOTPLT64:
2396 /* This is the same as GOT64 for relocation purposes, but
2397 indicates the existence of a PLT entry. The difficulty is,
2398 that we must calculate the GOT slot offset from the PLT
2399 offset, if this symbol got a PLT entry (it was global).
2400 Additionally if it's computed from the PLT entry, then that
2401 GOT offset is relative to .got.plt, not to .got. */
2402 base_got = htab->sgot;
2403
2404 if (htab->sgot == NULL)
2405 abort ();
2406
2407 if (h != NULL)
2408 {
2409 bfd_boolean dyn;
2410
2411 off = h->got.offset;
2412 if (h->needs_plt
2413 && h->plt.offset != (bfd_vma)-1
2414 && off == (bfd_vma)-1)
2415 {
2416 /* We can't use h->got.offset here to save
2417 state, or even just remember the offset, as
2418 finish_dynamic_symbol would use that as offset into
2419 .got. */
2420 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2421 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2422 base_got = htab->sgotplt;
2423 }
2424
2425 dyn = htab->elf.dynamic_sections_created;
2426
2427 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2428 || (info->shared
2429 && SYMBOL_REFERENCES_LOCAL (info, h))
2430 || (ELF_ST_VISIBILITY (h->other)
2431 && h->root.type == bfd_link_hash_undefweak))
2432 {
2433 /* This is actually a static link, or it is a -Bsymbolic
2434 link and the symbol is defined locally, or the symbol
2435 was forced to be local because of a version file. We
2436 must initialize this entry in the global offset table.
2437 Since the offset must always be a multiple of 8, we
2438 use the least significant bit to record whether we
2439 have initialized it already.
2440
2441 When doing a dynamic link, we create a .rela.got
2442 relocation entry to initialize the value. This is
2443 done in the finish_dynamic_symbol routine. */
2444 if ((off & 1) != 0)
2445 off &= ~1;
2446 else
2447 {
2448 bfd_put_64 (output_bfd, relocation,
2449 base_got->contents + off);
2450 /* Note that this is harmless for the GOTPLT64 case,
2451 as -1 | 1 still is -1. */
2452 h->got.offset |= 1;
2453 }
2454 }
2455 else
2456 unresolved_reloc = FALSE;
2457 }
2458 else
2459 {
2460 if (local_got_offsets == NULL)
2461 abort ();
2462
2463 off = local_got_offsets[r_symndx];
2464
2465 /* The offset must always be a multiple of 8. We use
2466 the least significant bit to record whether we have
2467 already generated the necessary reloc. */
2468 if ((off & 1) != 0)
2469 off &= ~1;
2470 else
2471 {
2472 bfd_put_64 (output_bfd, relocation,
2473 base_got->contents + off);
2474
2475 if (info->shared)
2476 {
2477 asection *s;
2478 Elf_Internal_Rela outrel;
2479 bfd_byte *loc;
2480
2481 /* We need to generate a R_X86_64_RELATIVE reloc
2482 for the dynamic linker. */
2483 s = htab->srelgot;
2484 if (s == NULL)
2485 abort ();
2486
2487 outrel.r_offset = (base_got->output_section->vma
2488 + base_got->output_offset
2489 + off);
2490 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2491 outrel.r_addend = relocation;
2492 loc = s->contents;
2493 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2494 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2495 }
2496
2497 local_got_offsets[r_symndx] |= 1;
2498 }
2499 }
2500
2501 if (off >= (bfd_vma) -2)
2502 abort ();
2503
2504 relocation = base_got->output_section->vma
2505 + base_got->output_offset + off;
2506 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2507 relocation -= htab->sgotplt->output_section->vma
2508 - htab->sgotplt->output_offset;
2509
2510 break;
2511
2512 case R_X86_64_GOTOFF64:
2513 /* Relocation is relative to the start of the global offset
2514 table. */
2515
2516 /* Check to make sure it isn't a protected function symbol
2517 for shared library since it may not be local when used
2518 as function address. */
2519 if (info->shared
2520 && h
2521 && h->def_regular
2522 && h->type == STT_FUNC
2523 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2524 {
2525 (*_bfd_error_handler)
2526 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2527 input_bfd, h->root.root.string);
2528 bfd_set_error (bfd_error_bad_value);
2529 return FALSE;
2530 }
2531
2532 /* Note that sgot is not involved in this
2533 calculation. We always want the start of .got.plt. If we
2534 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2535 permitted by the ABI, we might have to change this
2536 calculation. */
2537 relocation -= htab->sgotplt->output_section->vma
2538 + htab->sgotplt->output_offset;
2539 break;
2540
2541 case R_X86_64_GOTPC32:
2542 case R_X86_64_GOTPC64:
2543 /* Use global offset table as symbol value. */
2544 relocation = htab->sgotplt->output_section->vma
2545 + htab->sgotplt->output_offset;
2546 unresolved_reloc = FALSE;
2547 break;
2548
2549 case R_X86_64_PLTOFF64:
2550 /* Relocation is PLT entry relative to GOT. For local
2551 symbols it's the symbol itself relative to GOT. */
2552 if (h != NULL
2553 /* See PLT32 handling. */
2554 && h->plt.offset != (bfd_vma) -1
2555 && htab->splt != NULL)
2556 {
2557 relocation = (htab->splt->output_section->vma
2558 + htab->splt->output_offset
2559 + h->plt.offset);
2560 unresolved_reloc = FALSE;
2561 }
2562
2563 relocation -= htab->sgotplt->output_section->vma
2564 + htab->sgotplt->output_offset;
2565 break;
2566
2567 case R_X86_64_PLT32:
2568 /* Relocation is to the entry for this symbol in the
2569 procedure linkage table. */
2570
2571 /* Resolve a PLT32 reloc against a local symbol directly,
2572 without using the procedure linkage table. */
2573 if (h == NULL)
2574 break;
2575
2576 if (h->plt.offset == (bfd_vma) -1
2577 || htab->splt == NULL)
2578 {
2579 /* We didn't make a PLT entry for this symbol. This
2580 happens when statically linking PIC code, or when
2581 using -Bsymbolic. */
2582 break;
2583 }
2584
2585 relocation = (htab->splt->output_section->vma
2586 + htab->splt->output_offset
2587 + h->plt.offset);
2588 unresolved_reloc = FALSE;
2589 break;
2590
2591 case R_X86_64_PC8:
2592 case R_X86_64_PC16:
2593 case R_X86_64_PC32:
2594 if (info->shared
2595 && !SYMBOL_REFERENCES_LOCAL (info, h)
2596 && (input_section->flags & SEC_ALLOC) != 0
2597 && (input_section->flags & SEC_READONLY) != 0
2598 && (!h->def_regular
2599 || r_type != R_X86_64_PC32
2600 || h->type != STT_FUNC
2601 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
2602 || !is_32bit_relative_branch (contents,
2603 rel->r_offset)))
2604 {
2605 if (h->def_regular
2606 && r_type == R_X86_64_PC32
2607 && h->type == STT_FUNC
2608 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2609 (*_bfd_error_handler)
2610 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2611 input_bfd, h->root.root.string);
2612 else
2613 (*_bfd_error_handler)
2614 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2615 input_bfd, x86_64_elf_howto_table[r_type].name,
2616 h->root.root.string);
2617 bfd_set_error (bfd_error_bad_value);
2618 return FALSE;
2619 }
2620 /* Fall through. */
2621
2622 case R_X86_64_8:
2623 case R_X86_64_16:
2624 case R_X86_64_32:
2625 case R_X86_64_PC64:
2626 case R_X86_64_64:
2627 /* FIXME: The ABI says the linker should make sure the value is
2628 the same when it's zeroextended to 64 bit. */
2629
2630 if ((input_section->flags & SEC_ALLOC) == 0)
2631 break;
2632
2633 if ((info->shared
2634 && (h == NULL
2635 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2636 || h->root.type != bfd_link_hash_undefweak)
2637 && ((r_type != R_X86_64_PC8
2638 && r_type != R_X86_64_PC16
2639 && r_type != R_X86_64_PC32
2640 && r_type != R_X86_64_PC64)
2641 || !SYMBOL_CALLS_LOCAL (info, h)))
2642 || (ELIMINATE_COPY_RELOCS
2643 && !info->shared
2644 && h != NULL
2645 && h->dynindx != -1
2646 && !h->non_got_ref
2647 && ((h->def_dynamic
2648 && !h->def_regular)
2649 || h->root.type == bfd_link_hash_undefweak
2650 || h->root.type == bfd_link_hash_undefined)))
2651 {
2652 Elf_Internal_Rela outrel;
2653 bfd_byte *loc;
2654 bfd_boolean skip, relocate;
2655 asection *sreloc;
2656
2657 /* When generating a shared object, these relocations
2658 are copied into the output file to be resolved at run
2659 time. */
2660 skip = FALSE;
2661 relocate = FALSE;
2662
2663 outrel.r_offset =
2664 _bfd_elf_section_offset (output_bfd, info, input_section,
2665 rel->r_offset);
2666 if (outrel.r_offset == (bfd_vma) -1)
2667 skip = TRUE;
2668 else if (outrel.r_offset == (bfd_vma) -2)
2669 skip = TRUE, relocate = TRUE;
2670
2671 outrel.r_offset += (input_section->output_section->vma
2672 + input_section->output_offset);
2673
2674 if (skip)
2675 memset (&outrel, 0, sizeof outrel);
2676
2677 /* h->dynindx may be -1 if this symbol was marked to
2678 become local. */
2679 else if (h != NULL
2680 && h->dynindx != -1
2681 && (r_type == R_X86_64_PC8
2682 || r_type == R_X86_64_PC16
2683 || r_type == R_X86_64_PC32
2684 || r_type == R_X86_64_PC64
2685 || !info->shared
2686 || !SYMBOLIC_BIND (info, h)
2687 || !h->def_regular))
2688 {
2689 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2690 outrel.r_addend = rel->r_addend;
2691 }
2692 else
2693 {
2694 /* This symbol is local, or marked to become local. */
2695 if (r_type == R_X86_64_64)
2696 {
2697 relocate = TRUE;
2698 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2699 outrel.r_addend = relocation + rel->r_addend;
2700 }
2701 else
2702 {
2703 long sindx;
2704
2705 if (bfd_is_abs_section (sec))
2706 sindx = 0;
2707 else if (sec == NULL || sec->owner == NULL)
2708 {
2709 bfd_set_error (bfd_error_bad_value);
2710 return FALSE;
2711 }
2712 else
2713 {
2714 asection *osec;
2715
2716 /* We are turning this relocation into one
2717 against a section symbol. It would be
2718 proper to subtract the symbol's value,
2719 osec->vma, from the emitted reloc addend,
2720 but ld.so expects buggy relocs. */
2721 osec = sec->output_section;
2722 sindx = elf_section_data (osec)->dynindx;
2723 if (sindx == 0)
2724 {
2725 asection *oi = htab->elf.text_index_section;
2726 sindx = elf_section_data (oi)->dynindx;
2727 }
2728 BFD_ASSERT (sindx != 0);
2729 }
2730
2731 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2732 outrel.r_addend = relocation + rel->r_addend;
2733 }
2734 }
2735
2736 sreloc = elf_section_data (input_section)->sreloc;
2737 if (sreloc == NULL)
2738 abort ();
2739
2740 loc = sreloc->contents;
2741 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2742 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2743
2744 /* If this reloc is against an external symbol, we do
2745 not want to fiddle with the addend. Otherwise, we
2746 need to include the symbol value so that it becomes
2747 an addend for the dynamic reloc. */
2748 if (! relocate)
2749 continue;
2750 }
2751
2752 break;
2753
2754 case R_X86_64_TLSGD:
2755 case R_X86_64_GOTPC32_TLSDESC:
2756 case R_X86_64_TLSDESC_CALL:
2757 case R_X86_64_GOTTPOFF:
2758 tls_type = GOT_UNKNOWN;
2759 if (h == NULL && local_got_offsets)
2760 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2761 else if (h != NULL)
2762 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2763
2764 if (! elf64_x86_64_tls_transition (info, input_bfd,
2765 input_section, contents,
2766 symtab_hdr, sym_hashes,
2767 &r_type, tls_type, rel,
2768 relend, h))
2769 return FALSE;
2770
2771 if (r_type == R_X86_64_TPOFF32)
2772 {
2773 bfd_vma roff = rel->r_offset;
2774
2775 BFD_ASSERT (! unresolved_reloc);
2776
2777 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2778 {
2779 /* GD->LE transition.
2780 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2781 .word 0x6666; rex64; call __tls_get_addr
2782 Change it into:
2783 movq %fs:0, %rax
2784 leaq foo@tpoff(%rax), %rax */
2785 memcpy (contents + roff - 4,
2786 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2787 16);
2788 bfd_put_32 (output_bfd, tpoff (info, relocation),
2789 contents + roff + 8);
2790 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
2791 rel++;
2792 continue;
2793 }
2794 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2795 {
2796 /* GDesc -> LE transition.
2797 It's originally something like:
2798 leaq x@tlsdesc(%rip), %rax
2799
2800 Change it to:
2801 movl $x@tpoff, %rax
2802 */
2803
2804 unsigned int val, type, type2;
2805
2806 type = bfd_get_8 (input_bfd, contents + roff - 3);
2807 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2808 val = bfd_get_8 (input_bfd, contents + roff - 1);
2809 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2810 contents + roff - 3);
2811 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2812 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2813 contents + roff - 1);
2814 bfd_put_32 (output_bfd, tpoff (info, relocation),
2815 contents + roff);
2816 continue;
2817 }
2818 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2819 {
2820 /* GDesc -> LE transition.
2821 It's originally:
2822 call *(%rax)
2823 Turn it into:
2824 xchg %ax,%ax. */
2825 bfd_put_8 (output_bfd, 0x66, contents + roff);
2826 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2827 continue;
2828 }
2829 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
2830 {
2831 /* IE->LE transition:
2832 Originally it can be one of:
2833 movq foo@gottpoff(%rip), %reg
2834 addq foo@gottpoff(%rip), %reg
2835 We change it into:
2836 movq $foo, %reg
2837 leaq foo(%reg), %reg
2838 addq $foo, %reg. */
2839
2840 unsigned int val, type, reg;
2841
2842 val = bfd_get_8 (input_bfd, contents + roff - 3);
2843 type = bfd_get_8 (input_bfd, contents + roff - 2);
2844 reg = bfd_get_8 (input_bfd, contents + roff - 1);
2845 reg >>= 3;
2846 if (type == 0x8b)
2847 {
2848 /* movq */
2849 if (val == 0x4c)
2850 bfd_put_8 (output_bfd, 0x49,
2851 contents + roff - 3);
2852 bfd_put_8 (output_bfd, 0xc7,
2853 contents + roff - 2);
2854 bfd_put_8 (output_bfd, 0xc0 | reg,
2855 contents + roff - 1);
2856 }
2857 else if (reg == 4)
2858 {
2859 /* addq -> addq - addressing with %rsp/%r12 is
2860 special */
2861 if (val == 0x4c)
2862 bfd_put_8 (output_bfd, 0x49,
2863 contents + roff - 3);
2864 bfd_put_8 (output_bfd, 0x81,
2865 contents + roff - 2);
2866 bfd_put_8 (output_bfd, 0xc0 | reg,
2867 contents + roff - 1);
2868 }
2869 else
2870 {
2871 /* addq -> leaq */
2872 if (val == 0x4c)
2873 bfd_put_8 (output_bfd, 0x4d,
2874 contents + roff - 3);
2875 bfd_put_8 (output_bfd, 0x8d,
2876 contents + roff - 2);
2877 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2878 contents + roff - 1);
2879 }
2880 bfd_put_32 (output_bfd, tpoff (info, relocation),
2881 contents + roff);
2882 continue;
2883 }
2884 else
2885 BFD_ASSERT (FALSE);
2886 }
2887
2888 if (htab->sgot == NULL)
2889 abort ();
2890
2891 if (h != NULL)
2892 {
2893 off = h->got.offset;
2894 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2895 }
2896 else
2897 {
2898 if (local_got_offsets == NULL)
2899 abort ();
2900
2901 off = local_got_offsets[r_symndx];
2902 offplt = local_tlsdesc_gotents[r_symndx];
2903 }
2904
2905 if ((off & 1) != 0)
2906 off &= ~1;
2907 else
2908 {
2909 Elf_Internal_Rela outrel;
2910 bfd_byte *loc;
2911 int dr_type, indx;
2912 asection *sreloc;
2913
2914 if (htab->srelgot == NULL)
2915 abort ();
2916
2917 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2918
2919 if (GOT_TLS_GDESC_P (tls_type))
2920 {
2921 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2922 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2923 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2924 outrel.r_offset = (htab->sgotplt->output_section->vma
2925 + htab->sgotplt->output_offset
2926 + offplt
2927 + htab->sgotplt_jump_table_size);
2928 sreloc = htab->srelplt;
2929 loc = sreloc->contents;
2930 loc += sreloc->reloc_count++
2931 * sizeof (Elf64_External_Rela);
2932 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2933 <= sreloc->contents + sreloc->size);
2934 if (indx == 0)
2935 outrel.r_addend = relocation - dtpoff_base (info);
2936 else
2937 outrel.r_addend = 0;
2938 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2939 }
2940
2941 sreloc = htab->srelgot;
2942
2943 outrel.r_offset = (htab->sgot->output_section->vma
2944 + htab->sgot->output_offset + off);
2945
2946 if (GOT_TLS_GD_P (tls_type))
2947 dr_type = R_X86_64_DTPMOD64;
2948 else if (GOT_TLS_GDESC_P (tls_type))
2949 goto dr_done;
2950 else
2951 dr_type = R_X86_64_TPOFF64;
2952
2953 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2954 outrel.r_addend = 0;
2955 if ((dr_type == R_X86_64_TPOFF64
2956 || dr_type == R_X86_64_TLSDESC) && indx == 0)
2957 outrel.r_addend = relocation - dtpoff_base (info);
2958 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2959
2960 loc = sreloc->contents;
2961 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2962 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2963 <= sreloc->contents + sreloc->size);
2964 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2965
2966 if (GOT_TLS_GD_P (tls_type))
2967 {
2968 if (indx == 0)
2969 {
2970 BFD_ASSERT (! unresolved_reloc);
2971 bfd_put_64 (output_bfd,
2972 relocation - dtpoff_base (info),
2973 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2974 }
2975 else
2976 {
2977 bfd_put_64 (output_bfd, 0,
2978 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2979 outrel.r_info = ELF64_R_INFO (indx,
2980 R_X86_64_DTPOFF64);
2981 outrel.r_offset += GOT_ENTRY_SIZE;
2982 sreloc->reloc_count++;
2983 loc += sizeof (Elf64_External_Rela);
2984 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2985 <= sreloc->contents + sreloc->size);
2986 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2987 }
2988 }
2989
2990 dr_done:
2991 if (h != NULL)
2992 h->got.offset |= 1;
2993 else
2994 local_got_offsets[r_symndx] |= 1;
2995 }
2996
2997 if (off >= (bfd_vma) -2
2998 && ! GOT_TLS_GDESC_P (tls_type))
2999 abort ();
3000 if (r_type == ELF64_R_TYPE (rel->r_info))
3001 {
3002 if (r_type == R_X86_64_GOTPC32_TLSDESC
3003 || r_type == R_X86_64_TLSDESC_CALL)
3004 relocation = htab->sgotplt->output_section->vma
3005 + htab->sgotplt->output_offset
3006 + offplt + htab->sgotplt_jump_table_size;
3007 else
3008 relocation = htab->sgot->output_section->vma
3009 + htab->sgot->output_offset + off;
3010 unresolved_reloc = FALSE;
3011 }
3012 else
3013 {
3014 bfd_vma roff = rel->r_offset;
3015
3016 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3017 {
3018 /* GD->IE transition.
3019 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3020 .word 0x6666; rex64; call __tls_get_addr@plt
3021 Change it into:
3022 movq %fs:0, %rax
3023 addq foo@gottpoff(%rip), %rax */
3024 memcpy (contents + roff - 4,
3025 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3026 16);
3027
3028 relocation = (htab->sgot->output_section->vma
3029 + htab->sgot->output_offset + off
3030 - roff
3031 - input_section->output_section->vma
3032 - input_section->output_offset
3033 - 12);
3034 bfd_put_32 (output_bfd, relocation,
3035 contents + roff + 8);
3036 /* Skip R_X86_64_PLT32. */
3037 rel++;
3038 continue;
3039 }
3040 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3041 {
3042 /* GDesc -> IE transition.
3043 It's originally something like:
3044 leaq x@tlsdesc(%rip), %rax
3045
3046 Change it to:
3047 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3048 */
3049
3050 unsigned int val, type, type2;
3051
3052 type = bfd_get_8 (input_bfd, contents + roff - 3);
3053 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3054 val = bfd_get_8 (input_bfd, contents + roff - 1);
3055
3056 /* Now modify the instruction as appropriate. To
3057 turn a leaq into a movq in the form we use it, it
3058 suffices to change the second byte from 0x8d to
3059 0x8b. */
3060 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3061
3062 bfd_put_32 (output_bfd,
3063 htab->sgot->output_section->vma
3064 + htab->sgot->output_offset + off
3065 - rel->r_offset
3066 - input_section->output_section->vma
3067 - input_section->output_offset
3068 - 4,
3069 contents + roff);
3070 continue;
3071 }
3072 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3073 {
3074 /* GDesc -> IE transition.
3075 It's originally:
3076 call *(%rax)
3077
3078 Change it to:
3079 xchg %ax,%ax. */
3080
3081 unsigned int val, type;
3082
3083 type = bfd_get_8 (input_bfd, contents + roff);
3084 val = bfd_get_8 (input_bfd, contents + roff + 1);
3085 bfd_put_8 (output_bfd, 0x66, contents + roff);
3086 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3087 continue;
3088 }
3089 else
3090 BFD_ASSERT (FALSE);
3091 }
3092 break;
3093
3094 case R_X86_64_TLSLD:
3095 if (! elf64_x86_64_tls_transition (info, input_bfd,
3096 input_section, contents,
3097 symtab_hdr, sym_hashes,
3098 &r_type, GOT_UNKNOWN,
3099 rel, relend, h))
3100 return FALSE;
3101
3102 if (r_type != R_X86_64_TLSLD)
3103 {
3104 /* LD->LE transition:
3105 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3106 We change it into:
3107 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3108
3109 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3110 memcpy (contents + rel->r_offset - 3,
3111 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3112 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3113 rel++;
3114 continue;
3115 }
3116
3117 if (htab->sgot == NULL)
3118 abort ();
3119
3120 off = htab->tls_ld_got.offset;
3121 if (off & 1)
3122 off &= ~1;
3123 else
3124 {
3125 Elf_Internal_Rela outrel;
3126 bfd_byte *loc;
3127
3128 if (htab->srelgot == NULL)
3129 abort ();
3130
3131 outrel.r_offset = (htab->sgot->output_section->vma
3132 + htab->sgot->output_offset + off);
3133
3134 bfd_put_64 (output_bfd, 0,
3135 htab->sgot->contents + off);
3136 bfd_put_64 (output_bfd, 0,
3137 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3138 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3139 outrel.r_addend = 0;
3140 loc = htab->srelgot->contents;
3141 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3142 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3143 htab->tls_ld_got.offset |= 1;
3144 }
3145 relocation = htab->sgot->output_section->vma
3146 + htab->sgot->output_offset + off;
3147 unresolved_reloc = FALSE;
3148 break;
3149
3150 case R_X86_64_DTPOFF32:
3151 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3152 relocation -= dtpoff_base (info);
3153 else
3154 relocation = tpoff (info, relocation);
3155 break;
3156
3157 case R_X86_64_TPOFF32:
3158 BFD_ASSERT (! info->shared);
3159 relocation = tpoff (info, relocation);
3160 break;
3161
3162 default:
3163 break;
3164 }
3165
3166 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3167 because such sections are not SEC_ALLOC and thus ld.so will
3168 not process them. */
3169 if (unresolved_reloc
3170 && !((input_section->flags & SEC_DEBUGGING) != 0
3171 && h->def_dynamic))
3172 (*_bfd_error_handler)
3173 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3174 input_bfd,
3175 input_section,
3176 (long) rel->r_offset,
3177 howto->name,
3178 h->root.root.string);
3179
3180 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3181 contents, rel->r_offset,
3182 relocation, rel->r_addend);
3183
3184 if (r != bfd_reloc_ok)
3185 {
3186 const char *name;
3187
3188 if (h != NULL)
3189 name = h->root.root.string;
3190 else
3191 {
3192 name = bfd_elf_string_from_elf_section (input_bfd,
3193 symtab_hdr->sh_link,
3194 sym->st_name);
3195 if (name == NULL)
3196 return FALSE;
3197 if (*name == '\0')
3198 name = bfd_section_name (input_bfd, sec);
3199 }
3200
3201 if (r == bfd_reloc_overflow)
3202 {
3203 if (! ((*info->callbacks->reloc_overflow)
3204 (info, (h ? &h->root : NULL), name, howto->name,
3205 (bfd_vma) 0, input_bfd, input_section,
3206 rel->r_offset)))
3207 return FALSE;
3208 }
3209 else
3210 {
3211 (*_bfd_error_handler)
3212 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3213 input_bfd, input_section,
3214 (long) rel->r_offset, name, (int) r);
3215 return FALSE;
3216 }
3217 }
3218 }
3219
3220 return TRUE;
3221 }
3222
3223 /* Finish up dynamic symbol handling. We set the contents of various
3224 dynamic sections here. */
3225
3226 static bfd_boolean
3227 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3228 struct bfd_link_info *info,
3229 struct elf_link_hash_entry *h,
3230 Elf_Internal_Sym *sym)
3231 {
3232 struct elf64_x86_64_link_hash_table *htab;
3233
3234 htab = elf64_x86_64_hash_table (info);
3235
3236 if (h->plt.offset != (bfd_vma) -1)
3237 {
3238 bfd_vma plt_index;
3239 bfd_vma got_offset;
3240 Elf_Internal_Rela rela;
3241 bfd_byte *loc;
3242
3243 /* This symbol has an entry in the procedure linkage table. Set
3244 it up. */
3245 if (h->dynindx == -1
3246 || htab->splt == NULL
3247 || htab->sgotplt == NULL
3248 || htab->srelplt == NULL)
3249 abort ();
3250
3251 /* Get the index in the procedure linkage table which
3252 corresponds to this symbol. This is the index of this symbol
3253 in all the symbols for which we are making plt entries. The
3254 first entry in the procedure linkage table is reserved. */
3255 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3256
3257 /* Get the offset into the .got table of the entry that
3258 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3259 bytes. The first three are reserved for the dynamic linker. */
3260 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3261
3262 /* Fill in the entry in the procedure linkage table. */
3263 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3264 PLT_ENTRY_SIZE);
3265
3266 /* Insert the relocation positions of the plt section. The magic
3267 numbers at the end of the statements are the positions of the
3268 relocations in the plt section. */
3269 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3270 instruction uses 6 bytes, subtract this value. */
3271 bfd_put_32 (output_bfd,
3272 (htab->sgotplt->output_section->vma
3273 + htab->sgotplt->output_offset
3274 + got_offset
3275 - htab->splt->output_section->vma
3276 - htab->splt->output_offset
3277 - h->plt.offset
3278 - 6),
3279 htab->splt->contents + h->plt.offset + 2);
3280 /* Put relocation index. */
3281 bfd_put_32 (output_bfd, plt_index,
3282 htab->splt->contents + h->plt.offset + 7);
3283 /* Put offset for jmp .PLT0. */
3284 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3285 htab->splt->contents + h->plt.offset + 12);
3286
3287 /* Fill in the entry in the global offset table, initially this
3288 points to the pushq instruction in the PLT which is at offset 6. */
3289 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3290 + htab->splt->output_offset
3291 + h->plt.offset + 6),
3292 htab->sgotplt->contents + got_offset);
3293
3294 /* Fill in the entry in the .rela.plt section. */
3295 rela.r_offset = (htab->sgotplt->output_section->vma
3296 + htab->sgotplt->output_offset
3297 + got_offset);
3298 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3299 rela.r_addend = 0;
3300 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
3301 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3302
3303 if (!h->def_regular)
3304 {
3305 /* Mark the symbol as undefined, rather than as defined in
3306 the .plt section. Leave the value if there were any
3307 relocations where pointer equality matters (this is a clue
3308 for the dynamic linker, to make function pointer
3309 comparisons work between an application and shared
3310 library), otherwise set it to zero. If a function is only
3311 called from a binary, there is no need to slow down
3312 shared libraries because of that. */
3313 sym->st_shndx = SHN_UNDEF;
3314 if (!h->pointer_equality_needed)
3315 sym->st_value = 0;
3316 }
3317 }
3318
3319 if (h->got.offset != (bfd_vma) -1
3320 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3321 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3322 {
3323 Elf_Internal_Rela rela;
3324 bfd_byte *loc;
3325
3326 /* This symbol has an entry in the global offset table. Set it
3327 up. */
3328 if (htab->sgot == NULL || htab->srelgot == NULL)
3329 abort ();
3330
3331 rela.r_offset = (htab->sgot->output_section->vma
3332 + htab->sgot->output_offset
3333 + (h->got.offset &~ (bfd_vma) 1));
3334
3335 /* If this is a static link, or it is a -Bsymbolic link and the
3336 symbol is defined locally or was forced to be local because
3337 of a version file, we just want to emit a RELATIVE reloc.
3338 The entry in the global offset table will already have been
3339 initialized in the relocate_section function. */
3340 if (info->shared
3341 && SYMBOL_REFERENCES_LOCAL (info, h))
3342 {
3343 BFD_ASSERT((h->got.offset & 1) != 0);
3344 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3345 rela.r_addend = (h->root.u.def.value
3346 + h->root.u.def.section->output_section->vma
3347 + h->root.u.def.section->output_offset);
3348 }
3349 else
3350 {
3351 BFD_ASSERT((h->got.offset & 1) == 0);
3352 bfd_put_64 (output_bfd, (bfd_vma) 0,
3353 htab->sgot->contents + h->got.offset);
3354 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3355 rela.r_addend = 0;
3356 }
3357
3358 loc = htab->srelgot->contents;
3359 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3360 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3361 }
3362
3363 if (h->needs_copy)
3364 {
3365 Elf_Internal_Rela rela;
3366 bfd_byte *loc;
3367
3368 /* This symbol needs a copy reloc. Set it up. */
3369
3370 if (h->dynindx == -1
3371 || (h->root.type != bfd_link_hash_defined
3372 && h->root.type != bfd_link_hash_defweak)
3373 || htab->srelbss == NULL)
3374 abort ();
3375
3376 rela.r_offset = (h->root.u.def.value
3377 + h->root.u.def.section->output_section->vma
3378 + h->root.u.def.section->output_offset);
3379 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3380 rela.r_addend = 0;
3381 loc = htab->srelbss->contents;
3382 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3383 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3384 }
3385
3386 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3387 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3388 || h == htab->elf.hgot)
3389 sym->st_shndx = SHN_ABS;
3390
3391 return TRUE;
3392 }
3393
3394 /* Used to decide how to sort relocs in an optimal manner for the
3395 dynamic linker, before writing them out. */
3396
3397 static enum elf_reloc_type_class
3398 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3399 {
3400 switch ((int) ELF64_R_TYPE (rela->r_info))
3401 {
3402 case R_X86_64_RELATIVE:
3403 return reloc_class_relative;
3404 case R_X86_64_JUMP_SLOT:
3405 return reloc_class_plt;
3406 case R_X86_64_COPY:
3407 return reloc_class_copy;
3408 default:
3409 return reloc_class_normal;
3410 }
3411 }
3412
3413 /* Finish up the dynamic sections. */
3414
3415 static bfd_boolean
3416 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3417 {
3418 struct elf64_x86_64_link_hash_table *htab;
3419 bfd *dynobj;
3420 asection *sdyn;
3421
3422 htab = elf64_x86_64_hash_table (info);
3423 dynobj = htab->elf.dynobj;
3424 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3425
3426 if (htab->elf.dynamic_sections_created)
3427 {
3428 Elf64_External_Dyn *dyncon, *dynconend;
3429
3430 if (sdyn == NULL || htab->sgot == NULL)
3431 abort ();
3432
3433 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3434 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3435 for (; dyncon < dynconend; dyncon++)
3436 {
3437 Elf_Internal_Dyn dyn;
3438 asection *s;
3439
3440 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3441
3442 switch (dyn.d_tag)
3443 {
3444 default:
3445 continue;
3446
3447 case DT_PLTGOT:
3448 s = htab->sgotplt;
3449 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3450 break;
3451
3452 case DT_JMPREL:
3453 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3454 break;
3455
3456 case DT_PLTRELSZ:
3457 s = htab->srelplt->output_section;
3458 dyn.d_un.d_val = s->size;
3459 break;
3460
3461 case DT_RELASZ:
3462 /* The procedure linkage table relocs (DT_JMPREL) should
3463 not be included in the overall relocs (DT_RELA).
3464 Therefore, we override the DT_RELASZ entry here to
3465 make it not include the JMPREL relocs. Since the
3466 linker script arranges for .rela.plt to follow all
3467 other relocation sections, we don't have to worry
3468 about changing the DT_RELA entry. */
3469 if (htab->srelplt != NULL)
3470 {
3471 s = htab->srelplt->output_section;
3472 dyn.d_un.d_val -= s->size;
3473 }
3474 break;
3475
3476 case DT_TLSDESC_PLT:
3477 s = htab->splt;
3478 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3479 + htab->tlsdesc_plt;
3480 break;
3481
3482 case DT_TLSDESC_GOT:
3483 s = htab->sgot;
3484 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3485 + htab->tlsdesc_got;
3486 break;
3487 }
3488
3489 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3490 }
3491
3492 /* Fill in the special first entry in the procedure linkage table. */
3493 if (htab->splt && htab->splt->size > 0)
3494 {
3495 /* Fill in the first entry in the procedure linkage table. */
3496 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3497 PLT_ENTRY_SIZE);
3498 /* Add offset for pushq GOT+8(%rip), since the instruction
3499 uses 6 bytes subtract this value. */
3500 bfd_put_32 (output_bfd,
3501 (htab->sgotplt->output_section->vma
3502 + htab->sgotplt->output_offset
3503 + 8
3504 - htab->splt->output_section->vma
3505 - htab->splt->output_offset
3506 - 6),
3507 htab->splt->contents + 2);
3508 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3509 the end of the instruction. */
3510 bfd_put_32 (output_bfd,
3511 (htab->sgotplt->output_section->vma
3512 + htab->sgotplt->output_offset
3513 + 16
3514 - htab->splt->output_section->vma
3515 - htab->splt->output_offset
3516 - 12),
3517 htab->splt->contents + 8);
3518
3519 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3520 PLT_ENTRY_SIZE;
3521
3522 if (htab->tlsdesc_plt)
3523 {
3524 bfd_put_64 (output_bfd, (bfd_vma) 0,
3525 htab->sgot->contents + htab->tlsdesc_got);
3526
3527 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3528 elf64_x86_64_plt0_entry,
3529 PLT_ENTRY_SIZE);
3530
3531 /* Add offset for pushq GOT+8(%rip), since the
3532 instruction uses 6 bytes subtract this value. */
3533 bfd_put_32 (output_bfd,
3534 (htab->sgotplt->output_section->vma
3535 + htab->sgotplt->output_offset
3536 + 8
3537 - htab->splt->output_section->vma
3538 - htab->splt->output_offset
3539 - htab->tlsdesc_plt
3540 - 6),
3541 htab->splt->contents + htab->tlsdesc_plt + 2);
3542 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3543 htab->tlsdesc_got. The 12 is the offset to the end of
3544 the instruction. */
3545 bfd_put_32 (output_bfd,
3546 (htab->sgot->output_section->vma
3547 + htab->sgot->output_offset
3548 + htab->tlsdesc_got
3549 - htab->splt->output_section->vma
3550 - htab->splt->output_offset
3551 - htab->tlsdesc_plt
3552 - 12),
3553 htab->splt->contents + htab->tlsdesc_plt + 8);
3554 }
3555 }
3556 }
3557
3558 if (htab->sgotplt)
3559 {
3560 /* Fill in the first three entries in the global offset table. */
3561 if (htab->sgotplt->size > 0)
3562 {
3563 /* Set the first entry in the global offset table to the address of
3564 the dynamic section. */
3565 if (sdyn == NULL)
3566 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3567 else
3568 bfd_put_64 (output_bfd,
3569 sdyn->output_section->vma + sdyn->output_offset,
3570 htab->sgotplt->contents);
3571 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3572 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3573 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3574 }
3575
3576 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3577 GOT_ENTRY_SIZE;
3578 }
3579
3580 if (htab->sgot && htab->sgot->size > 0)
3581 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3582 = GOT_ENTRY_SIZE;
3583
3584 return TRUE;
3585 }
3586
3587 /* Return address for Ith PLT stub in section PLT, for relocation REL
3588 or (bfd_vma) -1 if it should not be included. */
3589
3590 static bfd_vma
3591 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3592 const arelent *rel ATTRIBUTE_UNUSED)
3593 {
3594 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3595 }
3596
3597 /* Handle an x86-64 specific section when reading an object file. This
3598 is called when elfcode.h finds a section with an unknown type. */
3599
3600 static bfd_boolean
3601 elf64_x86_64_section_from_shdr (bfd *abfd,
3602 Elf_Internal_Shdr *hdr,
3603 const char *name,
3604 int shindex)
3605 {
3606 if (hdr->sh_type != SHT_X86_64_UNWIND)
3607 return FALSE;
3608
3609 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3610 return FALSE;
3611
3612 return TRUE;
3613 }
3614
3615 /* Hook called by the linker routine which adds symbols from an object
3616 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3617 of .bss. */
3618
3619 static bfd_boolean
3620 elf64_x86_64_add_symbol_hook (bfd *abfd,
3621 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3622 Elf_Internal_Sym *sym,
3623 const char **namep ATTRIBUTE_UNUSED,
3624 flagword *flagsp ATTRIBUTE_UNUSED,
3625 asection **secp, bfd_vma *valp)
3626 {
3627 asection *lcomm;
3628
3629 switch (sym->st_shndx)
3630 {
3631 case SHN_X86_64_LCOMMON:
3632 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3633 if (lcomm == NULL)
3634 {
3635 lcomm = bfd_make_section_with_flags (abfd,
3636 "LARGE_COMMON",
3637 (SEC_ALLOC
3638 | SEC_IS_COMMON
3639 | SEC_LINKER_CREATED));
3640 if (lcomm == NULL)
3641 return FALSE;
3642 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3643 }
3644 *secp = lcomm;
3645 *valp = sym->st_size;
3646 break;
3647 }
3648 return TRUE;
3649 }
3650
3651
3652 /* Given a BFD section, try to locate the corresponding ELF section
3653 index. */
3654
3655 static bfd_boolean
3656 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3657 asection *sec, int *index)
3658 {
3659 if (sec == &_bfd_elf_large_com_section)
3660 {
3661 *index = SHN_X86_64_LCOMMON;
3662 return TRUE;
3663 }
3664 return FALSE;
3665 }
3666
3667 /* Process a symbol. */
3668
3669 static void
3670 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3671 asymbol *asym)
3672 {
3673 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3674
3675 switch (elfsym->internal_elf_sym.st_shndx)
3676 {
3677 case SHN_X86_64_LCOMMON:
3678 asym->section = &_bfd_elf_large_com_section;
3679 asym->value = elfsym->internal_elf_sym.st_size;
3680 /* Common symbol doesn't set BSF_GLOBAL. */
3681 asym->flags &= ~BSF_GLOBAL;
3682 break;
3683 }
3684 }
3685
3686 static bfd_boolean
3687 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3688 {
3689 return (sym->st_shndx == SHN_COMMON
3690 || sym->st_shndx == SHN_X86_64_LCOMMON);
3691 }
3692
3693 static unsigned int
3694 elf64_x86_64_common_section_index (asection *sec)
3695 {
3696 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3697 return SHN_COMMON;
3698 else
3699 return SHN_X86_64_LCOMMON;
3700 }
3701
3702 static asection *
3703 elf64_x86_64_common_section (asection *sec)
3704 {
3705 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3706 return bfd_com_section_ptr;
3707 else
3708 return &_bfd_elf_large_com_section;
3709 }
3710
3711 static bfd_boolean
3712 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3713 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3714 struct elf_link_hash_entry *h,
3715 Elf_Internal_Sym *sym,
3716 asection **psec,
3717 bfd_vma *pvalue ATTRIBUTE_UNUSED,
3718 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3719 bfd_boolean *skip ATTRIBUTE_UNUSED,
3720 bfd_boolean *override ATTRIBUTE_UNUSED,
3721 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3722 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3723 bfd_boolean *newdef ATTRIBUTE_UNUSED,
3724 bfd_boolean *newdyn,
3725 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3726 bfd_boolean *newweak ATTRIBUTE_UNUSED,
3727 bfd *abfd ATTRIBUTE_UNUSED,
3728 asection **sec,
3729 bfd_boolean *olddef ATTRIBUTE_UNUSED,
3730 bfd_boolean *olddyn,
3731 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3732 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
3733 bfd *oldbfd,
3734 asection **oldsec)
3735 {
3736 /* A normal common symbol and a large common symbol result in a
3737 normal common symbol. We turn the large common symbol into a
3738 normal one. */
3739 if (!*olddyn
3740 && h->root.type == bfd_link_hash_common
3741 && !*newdyn
3742 && bfd_is_com_section (*sec)
3743 && *oldsec != *sec)
3744 {
3745 if (sym->st_shndx == SHN_COMMON
3746 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3747 {
3748 h->root.u.c.p->section
3749 = bfd_make_section_old_way (oldbfd, "COMMON");
3750 h->root.u.c.p->section->flags = SEC_ALLOC;
3751 }
3752 else if (sym->st_shndx == SHN_X86_64_LCOMMON
3753 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
3754 *psec = *sec = bfd_com_section_ptr;
3755 }
3756
3757 return TRUE;
3758 }
3759
3760 static int
3761 elf64_x86_64_additional_program_headers (bfd *abfd,
3762 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3763 {
3764 asection *s;
3765 int count = 0;
3766
3767 /* Check to see if we need a large readonly segment. */
3768 s = bfd_get_section_by_name (abfd, ".lrodata");
3769 if (s && (s->flags & SEC_LOAD))
3770 count++;
3771
3772 /* Check to see if we need a large data segment. Since .lbss sections
3773 is placed right after the .bss section, there should be no need for
3774 a large data segment just because of .lbss. */
3775 s = bfd_get_section_by_name (abfd, ".ldata");
3776 if (s && (s->flags & SEC_LOAD))
3777 count++;
3778
3779 return count;
3780 }
3781
3782 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3783
3784 static bfd_boolean
3785 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
3786 {
3787 if (h->plt.offset != (bfd_vma) -1
3788 && !h->def_regular
3789 && !h->pointer_equality_needed)
3790 return FALSE;
3791
3792 return _bfd_elf_hash_symbol (h);
3793 }
3794
3795 static const struct bfd_elf_special_section
3796 elf64_x86_64_special_sections[]=
3797 {
3798 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3799 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3800 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3801 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3802 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3803 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3804 { NULL, 0, 0, 0, 0 }
3805 };
3806
3807 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3808 #define TARGET_LITTLE_NAME "elf64-x86-64"
3809 #define ELF_ARCH bfd_arch_i386
3810 #define ELF_MACHINE_CODE EM_X86_64
3811 #define ELF_MAXPAGESIZE 0x200000
3812 #define ELF_MINPAGESIZE 0x1000
3813 #define ELF_COMMONPAGESIZE 0x1000
3814
3815 #define elf_backend_can_gc_sections 1
3816 #define elf_backend_can_refcount 1
3817 #define elf_backend_want_got_plt 1
3818 #define elf_backend_plt_readonly 1
3819 #define elf_backend_want_plt_sym 0
3820 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
3821 #define elf_backend_rela_normal 1
3822
3823 #define elf_info_to_howto elf64_x86_64_info_to_howto
3824
3825 #define bfd_elf64_bfd_link_hash_table_create \
3826 elf64_x86_64_link_hash_table_create
3827 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
3828 #define bfd_elf64_bfd_reloc_name_lookup \
3829 elf64_x86_64_reloc_name_lookup
3830
3831 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3832 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
3833 #define elf_backend_check_relocs elf64_x86_64_check_relocs
3834 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3835 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3836 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3837 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3838 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3839 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3840 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3841 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
3842 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
3843 #define elf_backend_relocate_section elf64_x86_64_relocate_section
3844 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
3845 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
3846 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
3847 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
3848 #define elf_backend_object_p elf64_x86_64_elf_object_p
3849 #define bfd_elf64_mkobject elf64_x86_64_mkobject
3850
3851 #define elf_backend_section_from_shdr \
3852 elf64_x86_64_section_from_shdr
3853
3854 #define elf_backend_section_from_bfd_section \
3855 elf64_x86_64_elf_section_from_bfd_section
3856 #define elf_backend_add_symbol_hook \
3857 elf64_x86_64_add_symbol_hook
3858 #define elf_backend_symbol_processing \
3859 elf64_x86_64_symbol_processing
3860 #define elf_backend_common_section_index \
3861 elf64_x86_64_common_section_index
3862 #define elf_backend_common_section \
3863 elf64_x86_64_common_section
3864 #define elf_backend_common_definition \
3865 elf64_x86_64_common_definition
3866 #define elf_backend_merge_symbol \
3867 elf64_x86_64_merge_symbol
3868 #define elf_backend_special_sections \
3869 elf64_x86_64_special_sections
3870 #define elf_backend_additional_program_headers \
3871 elf64_x86_64_additional_program_headers
3872 #define elf_backend_hash_symbol \
3873 elf64_x86_64_hash_symbol
3874
3875 #include "elf64-target.h"
3876
3877 /* FreeBSD support. */
3878
3879 #undef TARGET_LITTLE_SYM
3880 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3881 #undef TARGET_LITTLE_NAME
3882 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3883
3884 #undef ELF_OSABI
3885 #define ELF_OSABI ELFOSABI_FREEBSD
3886
3887 #undef elf_backend_post_process_headers
3888 #define elf_backend_post_process_headers _bfd_elf_set_osabi
3889
3890 #undef elf64_bed
3891 #define elf64_bed elf64_x86_64_fbsd_bed
3892
3893 #include "elf64-target.h"
This page took 0.120005 seconds and 4 git commands to generate.