* elf64-ppc.c (ppc64_elf_gc_sweep_hook): Follow indirect and warning
[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
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27
28 #include "elf/x86-64.h"
29
30 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
31 #define MINUS_ONE (~ (bfd_vma) 0)
32
33 /* The relocation "howto" table. Order of fields:
34 type, size, bitsize, pc_relative, complain_on_overflow,
35 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
36 static reloc_howto_type x86_64_elf_howto_table[] =
37 {
38 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
39 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
40 FALSE),
41 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
42 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
43 FALSE),
44 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
45 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
46 TRUE),
47 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
48 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
49 FALSE),
50 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
51 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
52 TRUE),
53 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
54 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
55 FALSE),
56 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
57 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
58 MINUS_ONE, FALSE),
59 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
60 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
61 MINUS_ONE, FALSE),
62 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
64 MINUS_ONE, FALSE),
65 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
66 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
67 0xffffffff, TRUE),
68 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
69 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
70 FALSE),
71 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
72 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
73 FALSE),
74 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
76 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
78 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
79 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
80 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
81 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
82 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
84 MINUS_ONE, FALSE),
85 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
87 MINUS_ONE, FALSE),
88 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
89 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
90 MINUS_ONE, FALSE),
91 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
92 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
93 0xffffffff, TRUE),
94 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
95 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
96 0xffffffff, TRUE),
97 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
98 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
99 0xffffffff, FALSE),
100 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
102 0xffffffff, TRUE),
103 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
104 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
105 0xffffffff, FALSE),
106
107 /* GNU extension to record C++ vtable hierarchy. */
108 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
109 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
110
111 /* GNU extension to record C++ vtable member usage. */
112 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
113 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
114 FALSE)
115 };
116
117 /* Map BFD relocs to the x86_64 elf relocs. */
118 struct elf_reloc_map
119 {
120 bfd_reloc_code_real_type bfd_reloc_val;
121 unsigned char elf_reloc_val;
122 };
123
124 static const struct elf_reloc_map x86_64_reloc_map[] =
125 {
126 { BFD_RELOC_NONE, R_X86_64_NONE, },
127 { BFD_RELOC_64, R_X86_64_64, },
128 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
129 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
130 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
131 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
132 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
133 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
134 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
135 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
136 { BFD_RELOC_32, R_X86_64_32, },
137 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
138 { BFD_RELOC_16, R_X86_64_16, },
139 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
140 { BFD_RELOC_8, R_X86_64_8, },
141 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
142 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
143 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
144 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
145 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
146 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
147 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
148 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
149 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
150 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
151 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
152 };
153
154
155 /* Given a BFD reloc type, return a HOWTO structure. */
156 static reloc_howto_type *
157 elf64_x86_64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
158 bfd_reloc_code_real_type code)
159 {
160 unsigned int i;
161
162 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
163 i++)
164 {
165 if (x86_64_reloc_map[i].bfd_reloc_val == code)
166 return &x86_64_elf_howto_table[i];
167 }
168 return 0;
169 }
170
171 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
172
173 static void
174 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
175 Elf_Internal_Rela *dst)
176 {
177 unsigned r_type, i;
178
179 r_type = ELF64_R_TYPE (dst->r_info);
180 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
181 {
182 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32);
183 i = r_type;
184 }
185 else
186 {
187 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
188 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1);
189 }
190 cache_ptr->howto = &x86_64_elf_howto_table[i];
191 BFD_ASSERT (r_type == cache_ptr->howto->type);
192 }
193 \f
194 /* Support for core dump NOTE sections. */
195 static bfd_boolean
196 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
197 {
198 int offset;
199 size_t size;
200
201 switch (note->descsz)
202 {
203 default:
204 return FALSE;
205
206 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
207 /* pr_cursig */
208 elf_tdata (abfd)->core_signal
209 = bfd_get_16 (abfd, note->descdata + 12);
210
211 /* pr_pid */
212 elf_tdata (abfd)->core_pid
213 = bfd_get_32 (abfd, note->descdata + 32);
214
215 /* pr_reg */
216 offset = 112;
217 size = 216;
218
219 break;
220 }
221
222 /* Make a ".reg/999" section. */
223 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
224 size, note->descpos + offset);
225 }
226
227 static bfd_boolean
228 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
229 {
230 switch (note->descsz)
231 {
232 default:
233 return FALSE;
234
235 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
236 elf_tdata (abfd)->core_program
237 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
238 elf_tdata (abfd)->core_command
239 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
240 }
241
242 /* Note that for some reason, a spurious space is tacked
243 onto the end of the args in some (at least one anyway)
244 implementations, so strip it off if it exists. */
245
246 {
247 char *command = elf_tdata (abfd)->core_command;
248 int n = strlen (command);
249
250 if (0 < n && command[n - 1] == ' ')
251 command[n - 1] = '\0';
252 }
253
254 return TRUE;
255 }
256 \f
257 /* Functions for the x86-64 ELF linker. */
258
259 /* The name of the dynamic interpreter. This is put in the .interp
260 section. */
261
262 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
263
264 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
265 copying dynamic variables from a shared lib into an app's dynbss
266 section, and instead use a dynamic relocation to point into the
267 shared lib. */
268 #define ELIMINATE_COPY_RELOCS 1
269
270 /* The size in bytes of an entry in the global offset table. */
271
272 #define GOT_ENTRY_SIZE 8
273
274 /* The size in bytes of an entry in the procedure linkage table. */
275
276 #define PLT_ENTRY_SIZE 16
277
278 /* The first entry in a procedure linkage table looks like this. See the
279 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
280
281 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
282 {
283 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
284 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
285 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
286 };
287
288 /* Subsequent entries in a procedure linkage table look like this. */
289
290 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
291 {
292 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
293 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
294 0x68, /* pushq immediate */
295 0, 0, 0, 0, /* replaced with index into relocation table. */
296 0xe9, /* jmp relative */
297 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
298 };
299
300 /* The x86-64 linker needs to keep track of the number of relocs that
301 it decides to copy as dynamic relocs in check_relocs for each symbol.
302 This is so that it can later discard them if they are found to be
303 unnecessary. We store the information in a field extending the
304 regular ELF linker hash table. */
305
306 struct elf64_x86_64_dyn_relocs
307 {
308 /* Next section. */
309 struct elf64_x86_64_dyn_relocs *next;
310
311 /* The input section of the reloc. */
312 asection *sec;
313
314 /* Total number of relocs copied for the input section. */
315 bfd_size_type count;
316
317 /* Number of pc-relative relocs copied for the input section. */
318 bfd_size_type pc_count;
319 };
320
321 /* x86-64 ELF linker hash entry. */
322
323 struct elf64_x86_64_link_hash_entry
324 {
325 struct elf_link_hash_entry elf;
326
327 /* Track dynamic relocs copied for this symbol. */
328 struct elf64_x86_64_dyn_relocs *dyn_relocs;
329
330 #define GOT_UNKNOWN 0
331 #define GOT_NORMAL 1
332 #define GOT_TLS_GD 2
333 #define GOT_TLS_IE 3
334 unsigned char tls_type;
335 };
336
337 #define elf64_x86_64_hash_entry(ent) \
338 ((struct elf64_x86_64_link_hash_entry *)(ent))
339
340 struct elf64_x86_64_obj_tdata
341 {
342 struct elf_obj_tdata root;
343
344 /* tls_type for each local got entry. */
345 char *local_got_tls_type;
346 };
347
348 #define elf64_x86_64_tdata(abfd) \
349 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
350
351 #define elf64_x86_64_local_got_tls_type(abfd) \
352 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
353
354
355 /* x86-64 ELF linker hash table. */
356
357 struct elf64_x86_64_link_hash_table
358 {
359 struct elf_link_hash_table elf;
360
361 /* Short-cuts to get to dynamic linker sections. */
362 asection *sgot;
363 asection *sgotplt;
364 asection *srelgot;
365 asection *splt;
366 asection *srelplt;
367 asection *sdynbss;
368 asection *srelbss;
369
370 union {
371 bfd_signed_vma refcount;
372 bfd_vma offset;
373 } tls_ld_got;
374
375 /* Small local sym to section mapping cache. */
376 struct sym_sec_cache sym_sec;
377 };
378
379 /* Get the x86-64 ELF linker hash table from a link_info structure. */
380
381 #define elf64_x86_64_hash_table(p) \
382 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
383
384 /* Create an entry in an x86-64 ELF linker hash table. */
385
386 static struct bfd_hash_entry *
387 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
388 const char *string)
389 {
390 /* Allocate the structure if it has not already been allocated by a
391 subclass. */
392 if (entry == NULL)
393 {
394 entry = bfd_hash_allocate (table,
395 sizeof (struct elf64_x86_64_link_hash_entry));
396 if (entry == NULL)
397 return entry;
398 }
399
400 /* Call the allocation method of the superclass. */
401 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
402 if (entry != NULL)
403 {
404 struct elf64_x86_64_link_hash_entry *eh;
405
406 eh = (struct elf64_x86_64_link_hash_entry *) entry;
407 eh->dyn_relocs = NULL;
408 eh->tls_type = GOT_UNKNOWN;
409 }
410
411 return entry;
412 }
413
414 /* Create an X86-64 ELF linker hash table. */
415
416 static struct bfd_link_hash_table *
417 elf64_x86_64_link_hash_table_create (bfd *abfd)
418 {
419 struct elf64_x86_64_link_hash_table *ret;
420 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
421
422 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
423 if (ret == NULL)
424 return NULL;
425
426 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
427 {
428 free (ret);
429 return NULL;
430 }
431
432 ret->sgot = NULL;
433 ret->sgotplt = NULL;
434 ret->srelgot = NULL;
435 ret->splt = NULL;
436 ret->srelplt = NULL;
437 ret->sdynbss = NULL;
438 ret->srelbss = NULL;
439 ret->sym_sec.abfd = NULL;
440 ret->tls_ld_got.refcount = 0;
441
442 return &ret->elf.root;
443 }
444
445 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
446 shortcuts to them in our hash table. */
447
448 static bfd_boolean
449 create_got_section (bfd *dynobj, struct bfd_link_info *info)
450 {
451 struct elf64_x86_64_link_hash_table *htab;
452
453 if (! _bfd_elf_create_got_section (dynobj, info))
454 return FALSE;
455
456 htab = elf64_x86_64_hash_table (info);
457 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
458 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
459 if (!htab->sgot || !htab->sgotplt)
460 abort ();
461
462 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
463 if (htab->srelgot == NULL
464 || ! bfd_set_section_flags (dynobj, htab->srelgot,
465 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
466 | SEC_IN_MEMORY | SEC_LINKER_CREATED
467 | SEC_READONLY))
468 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
469 return FALSE;
470 return TRUE;
471 }
472
473 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
474 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
475 hash table. */
476
477 static bfd_boolean
478 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
479 {
480 struct elf64_x86_64_link_hash_table *htab;
481
482 htab = elf64_x86_64_hash_table (info);
483 if (!htab->sgot && !create_got_section (dynobj, info))
484 return FALSE;
485
486 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
487 return FALSE;
488
489 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
490 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
491 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
492 if (!info->shared)
493 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
494
495 if (!htab->splt || !htab->srelplt || !htab->sdynbss
496 || (!info->shared && !htab->srelbss))
497 abort ();
498
499 return TRUE;
500 }
501
502 /* Copy the extra info we tack onto an elf_link_hash_entry. */
503
504 static void
505 elf64_x86_64_copy_indirect_symbol (const struct elf_backend_data *bed,
506 struct elf_link_hash_entry *dir,
507 struct elf_link_hash_entry *ind)
508 {
509 struct elf64_x86_64_link_hash_entry *edir, *eind;
510
511 edir = (struct elf64_x86_64_link_hash_entry *) dir;
512 eind = (struct elf64_x86_64_link_hash_entry *) ind;
513
514 if (eind->dyn_relocs != NULL)
515 {
516 if (edir->dyn_relocs != NULL)
517 {
518 struct elf64_x86_64_dyn_relocs **pp;
519 struct elf64_x86_64_dyn_relocs *p;
520
521 if (ind->root.type == bfd_link_hash_indirect)
522 abort ();
523
524 /* Add reloc counts against the weak sym to the strong sym
525 list. Merge any entries against the same section. */
526 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
527 {
528 struct elf64_x86_64_dyn_relocs *q;
529
530 for (q = edir->dyn_relocs; q != NULL; q = q->next)
531 if (q->sec == p->sec)
532 {
533 q->pc_count += p->pc_count;
534 q->count += p->count;
535 *pp = p->next;
536 break;
537 }
538 if (q == NULL)
539 pp = &p->next;
540 }
541 *pp = edir->dyn_relocs;
542 }
543
544 edir->dyn_relocs = eind->dyn_relocs;
545 eind->dyn_relocs = NULL;
546 }
547
548 if (ind->root.type == bfd_link_hash_indirect
549 && dir->got.refcount <= 0)
550 {
551 edir->tls_type = eind->tls_type;
552 eind->tls_type = GOT_UNKNOWN;
553 }
554
555 if (ELIMINATE_COPY_RELOCS
556 && ind->root.type != bfd_link_hash_indirect
557 && dir->dynamic_adjusted)
558 {
559 /* If called to transfer flags for a weakdef during processing
560 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
561 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
562 dir->ref_dynamic |= ind->ref_dynamic;
563 dir->ref_regular |= ind->ref_regular;
564 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
565 dir->needs_plt |= ind->needs_plt;
566 dir->pointer_equality_needed |= ind->pointer_equality_needed;
567 }
568 else
569 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
570 }
571
572 static bfd_boolean
573 elf64_x86_64_mkobject (bfd *abfd)
574 {
575 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
576 abfd->tdata.any = bfd_zalloc (abfd, amt);
577 if (abfd->tdata.any == NULL)
578 return FALSE;
579 return TRUE;
580 }
581
582 static bfd_boolean
583 elf64_x86_64_elf_object_p (bfd *abfd)
584 {
585 /* Set the right machine number for an x86-64 elf64 file. */
586 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
587 return TRUE;
588 }
589
590 static int
591 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
592 {
593 if (info->shared)
594 return r_type;
595
596 switch (r_type)
597 {
598 case R_X86_64_TLSGD:
599 case R_X86_64_GOTTPOFF:
600 if (is_local)
601 return R_X86_64_TPOFF32;
602 return R_X86_64_GOTTPOFF;
603 case R_X86_64_TLSLD:
604 return R_X86_64_TPOFF32;
605 }
606
607 return r_type;
608 }
609
610 /* Look through the relocs for a section during the first phase, and
611 calculate needed space in the global offset table, procedure
612 linkage table, and dynamic reloc sections. */
613
614 static bfd_boolean
615 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
616 const Elf_Internal_Rela *relocs)
617 {
618 struct elf64_x86_64_link_hash_table *htab;
619 Elf_Internal_Shdr *symtab_hdr;
620 struct elf_link_hash_entry **sym_hashes;
621 const Elf_Internal_Rela *rel;
622 const Elf_Internal_Rela *rel_end;
623 asection *sreloc;
624
625 if (info->relocatable)
626 return TRUE;
627
628 htab = elf64_x86_64_hash_table (info);
629 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
630 sym_hashes = elf_sym_hashes (abfd);
631
632 sreloc = NULL;
633
634 rel_end = relocs + sec->reloc_count;
635 for (rel = relocs; rel < rel_end; rel++)
636 {
637 unsigned int r_type;
638 unsigned long r_symndx;
639 struct elf_link_hash_entry *h;
640
641 r_symndx = ELF64_R_SYM (rel->r_info);
642 r_type = ELF64_R_TYPE (rel->r_info);
643
644 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
645 {
646 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
647 abfd, r_symndx);
648 return FALSE;
649 }
650
651 if (r_symndx < symtab_hdr->sh_info)
652 h = NULL;
653 else
654 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
655
656 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
657 switch (r_type)
658 {
659 case R_X86_64_TLSLD:
660 htab->tls_ld_got.refcount += 1;
661 goto create_got;
662
663 case R_X86_64_TPOFF32:
664 if (info->shared)
665 {
666 (*_bfd_error_handler)
667 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
668 abfd,
669 x86_64_elf_howto_table[r_type].name,
670 (h) ? h->root.root.string : "a local symbol");
671 bfd_set_error (bfd_error_bad_value);
672 return FALSE;
673 }
674 break;
675
676 case R_X86_64_GOTTPOFF:
677 if (info->shared)
678 info->flags |= DF_STATIC_TLS;
679 /* Fall through */
680
681 case R_X86_64_GOT32:
682 case R_X86_64_GOTPCREL:
683 case R_X86_64_TLSGD:
684 /* This symbol requires a global offset table entry. */
685 {
686 int tls_type, old_tls_type;
687
688 switch (r_type)
689 {
690 default: tls_type = GOT_NORMAL; break;
691 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
692 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
693 }
694
695 if (h != NULL)
696 {
697 h->got.refcount += 1;
698 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
699 }
700 else
701 {
702 bfd_signed_vma *local_got_refcounts;
703
704 /* This is a global offset table entry for a local symbol. */
705 local_got_refcounts = elf_local_got_refcounts (abfd);
706 if (local_got_refcounts == NULL)
707 {
708 bfd_size_type size;
709
710 size = symtab_hdr->sh_info;
711 size *= sizeof (bfd_signed_vma) + sizeof (char);
712 local_got_refcounts = ((bfd_signed_vma *)
713 bfd_zalloc (abfd, size));
714 if (local_got_refcounts == NULL)
715 return FALSE;
716 elf_local_got_refcounts (abfd) = local_got_refcounts;
717 elf64_x86_64_local_got_tls_type (abfd)
718 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
719 }
720 local_got_refcounts[r_symndx] += 1;
721 old_tls_type
722 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
723 }
724
725 /* If a TLS symbol is accessed using IE at least once,
726 there is no point to use dynamic model for it. */
727 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
728 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
729 {
730 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
731 tls_type = old_tls_type;
732 else
733 {
734 (*_bfd_error_handler)
735 (_("%B: %s' accessed both as normal and thread local symbol"),
736 abfd, h ? h->root.root.string : "<local>");
737 return FALSE;
738 }
739 }
740
741 if (old_tls_type != tls_type)
742 {
743 if (h != NULL)
744 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
745 else
746 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
747 }
748 }
749 /* Fall through */
750
751 //case R_X86_64_GOTPCREL:
752 create_got:
753 if (htab->sgot == NULL)
754 {
755 if (htab->elf.dynobj == NULL)
756 htab->elf.dynobj = abfd;
757 if (!create_got_section (htab->elf.dynobj, info))
758 return FALSE;
759 }
760 break;
761
762 case R_X86_64_PLT32:
763 /* This symbol requires a procedure linkage table entry. We
764 actually build the entry in adjust_dynamic_symbol,
765 because this might be a case of linking PIC code which is
766 never referenced by a dynamic object, in which case we
767 don't need to generate a procedure linkage table entry
768 after all. */
769
770 /* If this is a local symbol, we resolve it directly without
771 creating a procedure linkage table entry. */
772 if (h == NULL)
773 continue;
774
775 h->needs_plt = 1;
776 h->plt.refcount += 1;
777 break;
778
779 case R_X86_64_8:
780 case R_X86_64_16:
781 case R_X86_64_32:
782 case R_X86_64_32S:
783 /* Let's help debug shared library creation. These relocs
784 cannot be used in shared libs. Don't error out for
785 sections we don't care about, such as debug sections or
786 non-constant sections. */
787 if (info->shared
788 && (sec->flags & SEC_ALLOC) != 0
789 && (sec->flags & SEC_READONLY) != 0)
790 {
791 (*_bfd_error_handler)
792 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
793 abfd,
794 x86_64_elf_howto_table[r_type].name,
795 (h) ? h->root.root.string : "a local symbol");
796 bfd_set_error (bfd_error_bad_value);
797 return FALSE;
798 }
799 /* Fall through. */
800
801 case R_X86_64_PC8:
802 case R_X86_64_PC16:
803 case R_X86_64_PC32:
804 case R_X86_64_64:
805 if (h != NULL && !info->shared)
806 {
807 /* If this reloc is in a read-only section, we might
808 need a copy reloc. We can't check reliably at this
809 stage whether the section is read-only, as input
810 sections have not yet been mapped to output sections.
811 Tentatively set the flag for now, and correct in
812 adjust_dynamic_symbol. */
813 h->non_got_ref = 1;
814
815 /* We may need a .plt entry if the function this reloc
816 refers to is in a shared lib. */
817 h->plt.refcount += 1;
818 if (r_type != R_X86_64_PC32)
819 h->pointer_equality_needed = 1;
820 }
821
822 /* If we are creating a shared library, and this is a reloc
823 against a global symbol, or a non PC relative reloc
824 against a local symbol, then we need to copy the reloc
825 into the shared library. However, if we are linking with
826 -Bsymbolic, we do not need to copy a reloc against a
827 global symbol which is defined in an object we are
828 including in the link (i.e., DEF_REGULAR is set). At
829 this point we have not seen all the input files, so it is
830 possible that DEF_REGULAR is not set now but will be set
831 later (it is never cleared). In case of a weak definition,
832 DEF_REGULAR may be cleared later by a strong definition in
833 a shared library. We account for that possibility below by
834 storing information in the relocs_copied field of the hash
835 table entry. A similar situation occurs when creating
836 shared libraries and symbol visibility changes render the
837 symbol local.
838
839 If on the other hand, we are creating an executable, we
840 may need to keep relocations for symbols satisfied by a
841 dynamic library if we manage to avoid copy relocs for the
842 symbol. */
843 if ((info->shared
844 && (sec->flags & SEC_ALLOC) != 0
845 && (((r_type != R_X86_64_PC8)
846 && (r_type != R_X86_64_PC16)
847 && (r_type != R_X86_64_PC32))
848 || (h != NULL
849 && (! info->symbolic
850 || h->root.type == bfd_link_hash_defweak
851 || !h->def_regular))))
852 || (ELIMINATE_COPY_RELOCS
853 && !info->shared
854 && (sec->flags & SEC_ALLOC) != 0
855 && h != NULL
856 && (h->root.type == bfd_link_hash_defweak
857 || !h->def_regular)))
858 {
859 struct elf64_x86_64_dyn_relocs *p;
860 struct elf64_x86_64_dyn_relocs **head;
861
862 /* We must copy these reloc types into the output file.
863 Create a reloc section in dynobj and make room for
864 this reloc. */
865 if (sreloc == NULL)
866 {
867 const char *name;
868 bfd *dynobj;
869
870 name = (bfd_elf_string_from_elf_section
871 (abfd,
872 elf_elfheader (abfd)->e_shstrndx,
873 elf_section_data (sec)->rel_hdr.sh_name));
874 if (name == NULL)
875 return FALSE;
876
877 if (strncmp (name, ".rela", 5) != 0
878 || strcmp (bfd_get_section_name (abfd, sec),
879 name + 5) != 0)
880 {
881 (*_bfd_error_handler)
882 (_("%B: bad relocation section name `%s\'"),
883 abfd, name);
884 }
885
886 if (htab->elf.dynobj == NULL)
887 htab->elf.dynobj = abfd;
888
889 dynobj = htab->elf.dynobj;
890
891 sreloc = bfd_get_section_by_name (dynobj, name);
892 if (sreloc == NULL)
893 {
894 flagword flags;
895
896 sreloc = bfd_make_section (dynobj, name);
897 flags = (SEC_HAS_CONTENTS | SEC_READONLY
898 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
899 if ((sec->flags & SEC_ALLOC) != 0)
900 flags |= SEC_ALLOC | SEC_LOAD;
901 if (sreloc == NULL
902 || ! bfd_set_section_flags (dynobj, sreloc, flags)
903 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
904 return FALSE;
905 }
906 elf_section_data (sec)->sreloc = sreloc;
907 }
908
909 /* If this is a global symbol, we count the number of
910 relocations we need for this symbol. */
911 if (h != NULL)
912 {
913 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
914 }
915 else
916 {
917 /* Track dynamic relocs needed for local syms too.
918 We really need local syms available to do this
919 easily. Oh well. */
920
921 asection *s;
922 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
923 sec, r_symndx);
924 if (s == NULL)
925 return FALSE;
926
927 head = ((struct elf64_x86_64_dyn_relocs **)
928 &elf_section_data (s)->local_dynrel);
929 }
930
931 p = *head;
932 if (p == NULL || p->sec != sec)
933 {
934 bfd_size_type amt = sizeof *p;
935 p = ((struct elf64_x86_64_dyn_relocs *)
936 bfd_alloc (htab->elf.dynobj, amt));
937 if (p == NULL)
938 return FALSE;
939 p->next = *head;
940 *head = p;
941 p->sec = sec;
942 p->count = 0;
943 p->pc_count = 0;
944 }
945
946 p->count += 1;
947 if (r_type == R_X86_64_PC8
948 || r_type == R_X86_64_PC16
949 || r_type == R_X86_64_PC32)
950 p->pc_count += 1;
951 }
952 break;
953
954 /* This relocation describes the C++ object vtable hierarchy.
955 Reconstruct it for later use during GC. */
956 case R_X86_64_GNU_VTINHERIT:
957 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
958 return FALSE;
959 break;
960
961 /* This relocation describes which C++ vtable entries are actually
962 used. Record for later use during GC. */
963 case R_X86_64_GNU_VTENTRY:
964 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
965 return FALSE;
966 break;
967
968 default:
969 break;
970 }
971 }
972
973 return TRUE;
974 }
975
976 /* Return the section that should be marked against GC for a given
977 relocation. */
978
979 static asection *
980 elf64_x86_64_gc_mark_hook (asection *sec,
981 struct bfd_link_info *info ATTRIBUTE_UNUSED,
982 Elf_Internal_Rela *rel,
983 struct elf_link_hash_entry *h,
984 Elf_Internal_Sym *sym)
985 {
986 if (h != NULL)
987 {
988 switch (ELF64_R_TYPE (rel->r_info))
989 {
990 case R_X86_64_GNU_VTINHERIT:
991 case R_X86_64_GNU_VTENTRY:
992 break;
993
994 default:
995 switch (h->root.type)
996 {
997 case bfd_link_hash_defined:
998 case bfd_link_hash_defweak:
999 return h->root.u.def.section;
1000
1001 case bfd_link_hash_common:
1002 return h->root.u.c.p->section;
1003
1004 default:
1005 break;
1006 }
1007 }
1008 }
1009 else
1010 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1011
1012 return NULL;
1013 }
1014
1015 /* Update the got entry reference counts for the section being removed. */
1016
1017 static bfd_boolean
1018 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1019 asection *sec, const Elf_Internal_Rela *relocs)
1020 {
1021 Elf_Internal_Shdr *symtab_hdr;
1022 struct elf_link_hash_entry **sym_hashes;
1023 bfd_signed_vma *local_got_refcounts;
1024 const Elf_Internal_Rela *rel, *relend;
1025
1026 elf_section_data (sec)->local_dynrel = NULL;
1027
1028 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1029 sym_hashes = elf_sym_hashes (abfd);
1030 local_got_refcounts = elf_local_got_refcounts (abfd);
1031
1032 relend = relocs + sec->reloc_count;
1033 for (rel = relocs; rel < relend; rel++)
1034 {
1035 unsigned long r_symndx;
1036 unsigned int r_type;
1037 struct elf_link_hash_entry *h = NULL;
1038
1039 r_symndx = ELF64_R_SYM (rel->r_info);
1040 if (r_symndx >= symtab_hdr->sh_info)
1041 {
1042 struct elf64_x86_64_link_hash_entry *eh;
1043 struct elf64_x86_64_dyn_relocs **pp;
1044 struct elf64_x86_64_dyn_relocs *p;
1045
1046 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1047 while (h->root.type == bfd_link_hash_indirect
1048 || h->root.type == bfd_link_hash_warning)
1049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1050 eh = (struct elf64_x86_64_link_hash_entry *) h;
1051
1052 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1053 if (p->sec == sec)
1054 {
1055 /* Everything must go for SEC. */
1056 *pp = p->next;
1057 break;
1058 }
1059 }
1060
1061 r_type = ELF64_R_TYPE (rel->r_info);
1062 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1063 switch (r_type)
1064 {
1065 case R_X86_64_TLSLD:
1066 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1067 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1068 break;
1069
1070 case R_X86_64_TLSGD:
1071 case R_X86_64_GOTTPOFF:
1072 case R_X86_64_GOT32:
1073 case R_X86_64_GOTPCREL:
1074 if (h != NULL)
1075 {
1076 if (h->got.refcount > 0)
1077 h->got.refcount -= 1;
1078 }
1079 else if (local_got_refcounts != NULL)
1080 {
1081 if (local_got_refcounts[r_symndx] > 0)
1082 local_got_refcounts[r_symndx] -= 1;
1083 }
1084 break;
1085
1086 case R_X86_64_8:
1087 case R_X86_64_16:
1088 case R_X86_64_32:
1089 case R_X86_64_64:
1090 case R_X86_64_32S:
1091 case R_X86_64_PC8:
1092 case R_X86_64_PC16:
1093 case R_X86_64_PC32:
1094 if (info->shared)
1095 break;
1096 /* Fall thru */
1097
1098 case R_X86_64_PLT32:
1099 if (h != NULL)
1100 {
1101 if (h->plt.refcount > 0)
1102 h->plt.refcount -= 1;
1103 }
1104 break;
1105
1106 default:
1107 break;
1108 }
1109 }
1110
1111 return TRUE;
1112 }
1113
1114 /* Adjust a symbol defined by a dynamic object and referenced by a
1115 regular object. The current definition is in some section of the
1116 dynamic object, but we're not including those sections. We have to
1117 change the definition to something the rest of the link can
1118 understand. */
1119
1120 static bfd_boolean
1121 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1122 struct elf_link_hash_entry *h)
1123 {
1124 struct elf64_x86_64_link_hash_table *htab;
1125 asection *s;
1126 unsigned int power_of_two;
1127
1128 /* If this is a function, put it in the procedure linkage table. We
1129 will fill in the contents of the procedure linkage table later,
1130 when we know the address of the .got section. */
1131 if (h->type == STT_FUNC
1132 || h->needs_plt)
1133 {
1134 if (h->plt.refcount <= 0
1135 || SYMBOL_CALLS_LOCAL (info, h)
1136 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1137 && h->root.type == bfd_link_hash_undefweak))
1138 {
1139 /* This case can occur if we saw a PLT32 reloc in an input
1140 file, but the symbol was never referred to by a dynamic
1141 object, or if all references were garbage collected. In
1142 such a case, we don't actually need to build a procedure
1143 linkage table, and we can just do a PC32 reloc instead. */
1144 h->plt.offset = (bfd_vma) -1;
1145 h->needs_plt = 0;
1146 }
1147
1148 return TRUE;
1149 }
1150 else
1151 /* It's possible that we incorrectly decided a .plt reloc was
1152 needed for an R_X86_64_PC32 reloc to a non-function sym in
1153 check_relocs. We can't decide accurately between function and
1154 non-function syms in check-relocs; Objects loaded later in
1155 the link may change h->type. So fix it now. */
1156 h->plt.offset = (bfd_vma) -1;
1157
1158 /* If this is a weak symbol, and there is a real definition, the
1159 processor independent code will have arranged for us to see the
1160 real definition first, and we can just use the same value. */
1161 if (h->u.weakdef != NULL)
1162 {
1163 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1164 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1165 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1166 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1167 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1168 h->non_got_ref = h->u.weakdef->non_got_ref;
1169 return TRUE;
1170 }
1171
1172 /* This is a reference to a symbol defined by a dynamic object which
1173 is not a function. */
1174
1175 /* If we are creating a shared library, we must presume that the
1176 only references to the symbol are via the global offset table.
1177 For such cases we need not do anything here; the relocations will
1178 be handled correctly by relocate_section. */
1179 if (info->shared)
1180 return TRUE;
1181
1182 /* If there are no references to this symbol that do not use the
1183 GOT, we don't need to generate a copy reloc. */
1184 if (!h->non_got_ref)
1185 return TRUE;
1186
1187 /* If -z nocopyreloc was given, we won't generate them either. */
1188 if (info->nocopyreloc)
1189 {
1190 h->non_got_ref = 0;
1191 return TRUE;
1192 }
1193
1194 if (ELIMINATE_COPY_RELOCS)
1195 {
1196 struct elf64_x86_64_link_hash_entry * eh;
1197 struct elf64_x86_64_dyn_relocs *p;
1198
1199 eh = (struct elf64_x86_64_link_hash_entry *) h;
1200 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1201 {
1202 s = p->sec->output_section;
1203 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1204 break;
1205 }
1206
1207 /* If we didn't find any dynamic relocs in read-only sections, then
1208 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1209 if (p == NULL)
1210 {
1211 h->non_got_ref = 0;
1212 return TRUE;
1213 }
1214 }
1215
1216 /* We must allocate the symbol in our .dynbss section, which will
1217 become part of the .bss section of the executable. There will be
1218 an entry for this symbol in the .dynsym section. The dynamic
1219 object will contain position independent code, so all references
1220 from the dynamic object to this symbol will go through the global
1221 offset table. The dynamic linker will use the .dynsym entry to
1222 determine the address it must put in the global offset table, so
1223 both the dynamic object and the regular object will refer to the
1224 same memory location for the variable. */
1225
1226 htab = elf64_x86_64_hash_table (info);
1227
1228 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1229 to copy the initial value out of the dynamic object and into the
1230 runtime process image. */
1231 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1232 {
1233 htab->srelbss->size += sizeof (Elf64_External_Rela);
1234 h->needs_copy = 1;
1235 }
1236
1237 /* We need to figure out the alignment required for this symbol. I
1238 have no idea how ELF linkers handle this. 16-bytes is the size
1239 of the largest type that requires hard alignment -- long double. */
1240 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1241 this construct. */
1242 power_of_two = bfd_log2 (h->size);
1243 if (power_of_two > 4)
1244 power_of_two = 4;
1245
1246 /* Apply the required alignment. */
1247 s = htab->sdynbss;
1248 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1249 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1250 {
1251 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1252 return FALSE;
1253 }
1254
1255 /* Define the symbol as being at this point in the section. */
1256 h->root.u.def.section = s;
1257 h->root.u.def.value = s->size;
1258
1259 /* Increment the section size to make room for the symbol. */
1260 s->size += h->size;
1261
1262 return TRUE;
1263 }
1264
1265 /* Allocate space in .plt, .got and associated reloc sections for
1266 dynamic relocs. */
1267
1268 static bfd_boolean
1269 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1270 {
1271 struct bfd_link_info *info;
1272 struct elf64_x86_64_link_hash_table *htab;
1273 struct elf64_x86_64_link_hash_entry *eh;
1274 struct elf64_x86_64_dyn_relocs *p;
1275
1276 if (h->root.type == bfd_link_hash_indirect)
1277 return TRUE;
1278
1279 if (h->root.type == bfd_link_hash_warning)
1280 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1281
1282 info = (struct bfd_link_info *) inf;
1283 htab = elf64_x86_64_hash_table (info);
1284
1285 if (htab->elf.dynamic_sections_created
1286 && h->plt.refcount > 0)
1287 {
1288 /* Make sure this symbol is output as a dynamic symbol.
1289 Undefined weak syms won't yet be marked as dynamic. */
1290 if (h->dynindx == -1
1291 && !h->forced_local)
1292 {
1293 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1294 return FALSE;
1295 }
1296
1297 if (info->shared
1298 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1299 {
1300 asection *s = htab->splt;
1301
1302 /* If this is the first .plt entry, make room for the special
1303 first entry. */
1304 if (s->size == 0)
1305 s->size += PLT_ENTRY_SIZE;
1306
1307 h->plt.offset = s->size;
1308
1309 /* If this symbol is not defined in a regular file, and we are
1310 not generating a shared library, then set the symbol to this
1311 location in the .plt. This is required to make function
1312 pointers compare as equal between the normal executable and
1313 the shared library. */
1314 if (! info->shared
1315 && !h->def_regular)
1316 {
1317 h->root.u.def.section = s;
1318 h->root.u.def.value = h->plt.offset;
1319 }
1320
1321 /* Make room for this entry. */
1322 s->size += PLT_ENTRY_SIZE;
1323
1324 /* We also need to make an entry in the .got.plt section, which
1325 will be placed in the .got section by the linker script. */
1326 htab->sgotplt->size += GOT_ENTRY_SIZE;
1327
1328 /* We also need to make an entry in the .rela.plt section. */
1329 htab->srelplt->size += sizeof (Elf64_External_Rela);
1330 }
1331 else
1332 {
1333 h->plt.offset = (bfd_vma) -1;
1334 h->needs_plt = 0;
1335 }
1336 }
1337 else
1338 {
1339 h->plt.offset = (bfd_vma) -1;
1340 h->needs_plt = 0;
1341 }
1342
1343 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1344 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1345 if (h->got.refcount > 0
1346 && !info->shared
1347 && h->dynindx == -1
1348 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1349 h->got.offset = (bfd_vma) -1;
1350 else if (h->got.refcount > 0)
1351 {
1352 asection *s;
1353 bfd_boolean dyn;
1354 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1355
1356 /* Make sure this symbol is output as a dynamic symbol.
1357 Undefined weak syms won't yet be marked as dynamic. */
1358 if (h->dynindx == -1
1359 && !h->forced_local)
1360 {
1361 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1362 return FALSE;
1363 }
1364
1365 s = htab->sgot;
1366 h->got.offset = s->size;
1367 s->size += GOT_ENTRY_SIZE;
1368 /* R_X86_64_TLSGD needs 2 consecutive GOT slots. */
1369 if (tls_type == GOT_TLS_GD)
1370 s->size += GOT_ENTRY_SIZE;
1371 dyn = htab->elf.dynamic_sections_created;
1372 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1373 and two if global.
1374 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1375 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1376 || tls_type == GOT_TLS_IE)
1377 htab->srelgot->size += sizeof (Elf64_External_Rela);
1378 else if (tls_type == GOT_TLS_GD)
1379 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1380 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1381 || h->root.type != bfd_link_hash_undefweak)
1382 && (info->shared
1383 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1384 htab->srelgot->size += sizeof (Elf64_External_Rela);
1385 }
1386 else
1387 h->got.offset = (bfd_vma) -1;
1388
1389 eh = (struct elf64_x86_64_link_hash_entry *) h;
1390 if (eh->dyn_relocs == NULL)
1391 return TRUE;
1392
1393 /* In the shared -Bsymbolic case, discard space allocated for
1394 dynamic pc-relative relocs against symbols which turn out to be
1395 defined in regular objects. For the normal shared case, discard
1396 space for pc-relative relocs that have become local due to symbol
1397 visibility changes. */
1398
1399 if (info->shared)
1400 {
1401 /* Relocs that use pc_count are those that appear on a call
1402 insn, or certain REL relocs that can generated via assembly.
1403 We want calls to protected symbols to resolve directly to the
1404 function rather than going via the plt. If people want
1405 function pointer comparisons to work as expected then they
1406 should avoid writing weird assembly. */
1407 if (SYMBOL_CALLS_LOCAL (info, h))
1408 {
1409 struct elf64_x86_64_dyn_relocs **pp;
1410
1411 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1412 {
1413 p->count -= p->pc_count;
1414 p->pc_count = 0;
1415 if (p->count == 0)
1416 *pp = p->next;
1417 else
1418 pp = &p->next;
1419 }
1420 }
1421
1422 /* Also discard relocs on undefined weak syms with non-default
1423 visibility. */
1424 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1425 && h->root.type == bfd_link_hash_undefweak)
1426 eh->dyn_relocs = NULL;
1427 }
1428 else if (ELIMINATE_COPY_RELOCS)
1429 {
1430 /* For the non-shared case, discard space for relocs against
1431 symbols which turn out to need copy relocs or are not
1432 dynamic. */
1433
1434 if (!h->non_got_ref
1435 && ((h->def_dynamic
1436 && !h->def_regular)
1437 || (htab->elf.dynamic_sections_created
1438 && (h->root.type == bfd_link_hash_undefweak
1439 || h->root.type == bfd_link_hash_undefined))))
1440 {
1441 /* Make sure this symbol is output as a dynamic symbol.
1442 Undefined weak syms won't yet be marked as dynamic. */
1443 if (h->dynindx == -1
1444 && !h->forced_local)
1445 {
1446 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1447 return FALSE;
1448 }
1449
1450 /* If that succeeded, we know we'll be keeping all the
1451 relocs. */
1452 if (h->dynindx != -1)
1453 goto keep;
1454 }
1455
1456 eh->dyn_relocs = NULL;
1457
1458 keep: ;
1459 }
1460
1461 /* Finally, allocate space. */
1462 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1463 {
1464 asection *sreloc = elf_section_data (p->sec)->sreloc;
1465 sreloc->size += p->count * sizeof (Elf64_External_Rela);
1466 }
1467
1468 return TRUE;
1469 }
1470
1471 /* Find any dynamic relocs that apply to read-only sections. */
1472
1473 static bfd_boolean
1474 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1475 {
1476 struct elf64_x86_64_link_hash_entry *eh;
1477 struct elf64_x86_64_dyn_relocs *p;
1478
1479 if (h->root.type == bfd_link_hash_warning)
1480 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1481
1482 eh = (struct elf64_x86_64_link_hash_entry *) h;
1483 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1484 {
1485 asection *s = p->sec->output_section;
1486
1487 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1488 {
1489 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1490
1491 info->flags |= DF_TEXTREL;
1492
1493 /* Not an error, just cut short the traversal. */
1494 return FALSE;
1495 }
1496 }
1497 return TRUE;
1498 }
1499
1500 /* Set the sizes of the dynamic sections. */
1501
1502 static bfd_boolean
1503 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1504 struct bfd_link_info *info)
1505 {
1506 struct elf64_x86_64_link_hash_table *htab;
1507 bfd *dynobj;
1508 asection *s;
1509 bfd_boolean relocs;
1510 bfd *ibfd;
1511
1512 htab = elf64_x86_64_hash_table (info);
1513 dynobj = htab->elf.dynobj;
1514 if (dynobj == NULL)
1515 abort ();
1516
1517 if (htab->elf.dynamic_sections_created)
1518 {
1519 /* Set the contents of the .interp section to the interpreter. */
1520 if (info->executable)
1521 {
1522 s = bfd_get_section_by_name (dynobj, ".interp");
1523 if (s == NULL)
1524 abort ();
1525 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1526 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1527 }
1528 }
1529
1530 /* Set up .got offsets for local syms, and space for local dynamic
1531 relocs. */
1532 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1533 {
1534 bfd_signed_vma *local_got;
1535 bfd_signed_vma *end_local_got;
1536 char *local_tls_type;
1537 bfd_size_type locsymcount;
1538 Elf_Internal_Shdr *symtab_hdr;
1539 asection *srel;
1540
1541 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1542 continue;
1543
1544 for (s = ibfd->sections; s != NULL; s = s->next)
1545 {
1546 struct elf64_x86_64_dyn_relocs *p;
1547
1548 for (p = *((struct elf64_x86_64_dyn_relocs **)
1549 &elf_section_data (s)->local_dynrel);
1550 p != NULL;
1551 p = p->next)
1552 {
1553 if (!bfd_is_abs_section (p->sec)
1554 && bfd_is_abs_section (p->sec->output_section))
1555 {
1556 /* Input section has been discarded, either because
1557 it is a copy of a linkonce section or due to
1558 linker script /DISCARD/, so we'll be discarding
1559 the relocs too. */
1560 }
1561 else if (p->count != 0)
1562 {
1563 srel = elf_section_data (p->sec)->sreloc;
1564 srel->size += p->count * sizeof (Elf64_External_Rela);
1565 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1566 info->flags |= DF_TEXTREL;
1567
1568 }
1569 }
1570 }
1571
1572 local_got = elf_local_got_refcounts (ibfd);
1573 if (!local_got)
1574 continue;
1575
1576 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1577 locsymcount = symtab_hdr->sh_info;
1578 end_local_got = local_got + locsymcount;
1579 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1580 s = htab->sgot;
1581 srel = htab->srelgot;
1582 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1583 {
1584 if (*local_got > 0)
1585 {
1586 *local_got = s->size;
1587 s->size += GOT_ENTRY_SIZE;
1588 if (*local_tls_type == GOT_TLS_GD)
1589 s->size += GOT_ENTRY_SIZE;
1590 if (info->shared
1591 || *local_tls_type == GOT_TLS_GD
1592 || *local_tls_type == GOT_TLS_IE)
1593 srel->size += sizeof (Elf64_External_Rela);
1594 }
1595 else
1596 *local_got = (bfd_vma) -1;
1597 }
1598 }
1599
1600 if (htab->tls_ld_got.refcount > 0)
1601 {
1602 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1603 relocs. */
1604 htab->tls_ld_got.offset = htab->sgot->size;
1605 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1606 htab->srelgot->size += sizeof (Elf64_External_Rela);
1607 }
1608 else
1609 htab->tls_ld_got.offset = -1;
1610
1611 /* Allocate global sym .plt and .got entries, and space for global
1612 sym dynamic relocs. */
1613 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1614
1615 /* We now have determined the sizes of the various dynamic sections.
1616 Allocate memory for them. */
1617 relocs = FALSE;
1618 for (s = dynobj->sections; s != NULL; s = s->next)
1619 {
1620 if ((s->flags & SEC_LINKER_CREATED) == 0)
1621 continue;
1622
1623 if (s == htab->splt
1624 || s == htab->sgot
1625 || s == htab->sgotplt)
1626 {
1627 /* Strip this section if we don't need it; see the
1628 comment below. */
1629 }
1630 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1631 {
1632 if (s->size != 0 && s != htab->srelplt)
1633 relocs = TRUE;
1634
1635 /* We use the reloc_count field as a counter if we need
1636 to copy relocs into the output file. */
1637 s->reloc_count = 0;
1638 }
1639 else
1640 {
1641 /* It's not one of our sections, so don't allocate space. */
1642 continue;
1643 }
1644
1645 if (s->size == 0)
1646 {
1647 /* If we don't need this section, strip it from the
1648 output file. This is mostly to handle .rela.bss and
1649 .rela.plt. We must create both sections in
1650 create_dynamic_sections, because they must be created
1651 before the linker maps input sections to output
1652 sections. The linker does that before
1653 adjust_dynamic_symbol is called, and it is that
1654 function which decides whether anything needs to go
1655 into these sections. */
1656
1657 _bfd_strip_section_from_output (info, s);
1658 continue;
1659 }
1660
1661 /* Allocate memory for the section contents. We use bfd_zalloc
1662 here in case unused entries are not reclaimed before the
1663 section's contents are written out. This should not happen,
1664 but this way if it does, we get a R_X86_64_NONE reloc instead
1665 of garbage. */
1666 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1667 if (s->contents == NULL)
1668 return FALSE;
1669 }
1670
1671 if (htab->elf.dynamic_sections_created)
1672 {
1673 /* Add some entries to the .dynamic section. We fill in the
1674 values later, in elf64_x86_64_finish_dynamic_sections, but we
1675 must add the entries now so that we get the correct size for
1676 the .dynamic section. The DT_DEBUG entry is filled in by the
1677 dynamic linker and used by the debugger. */
1678 #define add_dynamic_entry(TAG, VAL) \
1679 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1680
1681 if (info->executable)
1682 {
1683 if (!add_dynamic_entry (DT_DEBUG, 0))
1684 return FALSE;
1685 }
1686
1687 if (htab->splt->size != 0)
1688 {
1689 if (!add_dynamic_entry (DT_PLTGOT, 0)
1690 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1691 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1692 || !add_dynamic_entry (DT_JMPREL, 0))
1693 return FALSE;
1694 }
1695
1696 if (relocs)
1697 {
1698 if (!add_dynamic_entry (DT_RELA, 0)
1699 || !add_dynamic_entry (DT_RELASZ, 0)
1700 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1701 return FALSE;
1702
1703 /* If any dynamic relocs apply to a read-only section,
1704 then we need a DT_TEXTREL entry. */
1705 if ((info->flags & DF_TEXTREL) == 0)
1706 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1707 (PTR) info);
1708
1709 if ((info->flags & DF_TEXTREL) != 0)
1710 {
1711 if (!add_dynamic_entry (DT_TEXTREL, 0))
1712 return FALSE;
1713 }
1714 }
1715 }
1716 #undef add_dynamic_entry
1717
1718 return TRUE;
1719 }
1720
1721 /* Return the base VMA address which should be subtracted from real addresses
1722 when resolving @dtpoff relocation.
1723 This is PT_TLS segment p_vaddr. */
1724
1725 static bfd_vma
1726 dtpoff_base (struct bfd_link_info *info)
1727 {
1728 /* If tls_sec is NULL, we should have signalled an error already. */
1729 if (elf_hash_table (info)->tls_sec == NULL)
1730 return 0;
1731 return elf_hash_table (info)->tls_sec->vma;
1732 }
1733
1734 /* Return the relocation value for @tpoff relocation
1735 if STT_TLS virtual address is ADDRESS. */
1736
1737 static bfd_vma
1738 tpoff (struct bfd_link_info *info, bfd_vma address)
1739 {
1740 struct elf_link_hash_table *htab = elf_hash_table (info);
1741
1742 /* If tls_segment is NULL, we should have signalled an error already. */
1743 if (htab->tls_sec == NULL)
1744 return 0;
1745 return address - htab->tls_size - htab->tls_sec->vma;
1746 }
1747
1748 /* Relocate an x86_64 ELF section. */
1749
1750 static bfd_boolean
1751 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1752 bfd *input_bfd, asection *input_section,
1753 bfd_byte *contents, Elf_Internal_Rela *relocs,
1754 Elf_Internal_Sym *local_syms,
1755 asection **local_sections)
1756 {
1757 struct elf64_x86_64_link_hash_table *htab;
1758 Elf_Internal_Shdr *symtab_hdr;
1759 struct elf_link_hash_entry **sym_hashes;
1760 bfd_vma *local_got_offsets;
1761 Elf_Internal_Rela *rel;
1762 Elf_Internal_Rela *relend;
1763
1764 if (info->relocatable)
1765 return TRUE;
1766
1767 htab = elf64_x86_64_hash_table (info);
1768 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1769 sym_hashes = elf_sym_hashes (input_bfd);
1770 local_got_offsets = elf_local_got_offsets (input_bfd);
1771
1772 rel = relocs;
1773 relend = relocs + input_section->reloc_count;
1774 for (; rel < relend; rel++)
1775 {
1776 unsigned int r_type;
1777 reloc_howto_type *howto;
1778 unsigned long r_symndx;
1779 struct elf_link_hash_entry *h;
1780 Elf_Internal_Sym *sym;
1781 asection *sec;
1782 bfd_vma off;
1783 bfd_vma relocation;
1784 bfd_boolean unresolved_reloc;
1785 bfd_reloc_status_type r;
1786 int tls_type;
1787
1788 r_type = ELF64_R_TYPE (rel->r_info);
1789 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1790 || r_type == (int) R_X86_64_GNU_VTENTRY)
1791 continue;
1792
1793 if (r_type >= R_X86_64_max)
1794 {
1795 bfd_set_error (bfd_error_bad_value);
1796 return FALSE;
1797 }
1798
1799 howto = x86_64_elf_howto_table + r_type;
1800 r_symndx = ELF64_R_SYM (rel->r_info);
1801 h = NULL;
1802 sym = NULL;
1803 sec = NULL;
1804 unresolved_reloc = FALSE;
1805 if (r_symndx < symtab_hdr->sh_info)
1806 {
1807 sym = local_syms + r_symndx;
1808 sec = local_sections[r_symndx];
1809
1810 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1811 }
1812 else
1813 {
1814 bfd_boolean warned;
1815
1816 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1817 r_symndx, symtab_hdr, sym_hashes,
1818 h, sec, relocation,
1819 unresolved_reloc, warned);
1820 }
1821 /* When generating a shared object, the relocations handled here are
1822 copied into the output file to be resolved at run time. */
1823 switch (r_type)
1824 {
1825 case R_X86_64_GOT32:
1826 /* Relocation is to the entry for this symbol in the global
1827 offset table. */
1828 case R_X86_64_GOTPCREL:
1829 /* Use global offset table as symbol value. */
1830 if (htab->sgot == NULL)
1831 abort ();
1832
1833 if (h != NULL)
1834 {
1835 bfd_boolean dyn;
1836
1837 off = h->got.offset;
1838 dyn = htab->elf.dynamic_sections_created;
1839
1840 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1841 || (info->shared
1842 && SYMBOL_REFERENCES_LOCAL (info, h))
1843 || (ELF_ST_VISIBILITY (h->other)
1844 && h->root.type == bfd_link_hash_undefweak))
1845 {
1846 /* This is actually a static link, or it is a -Bsymbolic
1847 link and the symbol is defined locally, or the symbol
1848 was forced to be local because of a version file. We
1849 must initialize this entry in the global offset table.
1850 Since the offset must always be a multiple of 8, we
1851 use the least significant bit to record whether we
1852 have initialized it already.
1853
1854 When doing a dynamic link, we create a .rela.got
1855 relocation entry to initialize the value. This is
1856 done in the finish_dynamic_symbol routine. */
1857 if ((off & 1) != 0)
1858 off &= ~1;
1859 else
1860 {
1861 bfd_put_64 (output_bfd, relocation,
1862 htab->sgot->contents + off);
1863 h->got.offset |= 1;
1864 }
1865 }
1866 else
1867 unresolved_reloc = FALSE;
1868 }
1869 else
1870 {
1871 if (local_got_offsets == NULL)
1872 abort ();
1873
1874 off = local_got_offsets[r_symndx];
1875
1876 /* The offset must always be a multiple of 8. We use
1877 the least significant bit to record whether we have
1878 already generated the necessary reloc. */
1879 if ((off & 1) != 0)
1880 off &= ~1;
1881 else
1882 {
1883 bfd_put_64 (output_bfd, relocation,
1884 htab->sgot->contents + off);
1885
1886 if (info->shared)
1887 {
1888 asection *s;
1889 Elf_Internal_Rela outrel;
1890 bfd_byte *loc;
1891
1892 /* We need to generate a R_X86_64_RELATIVE reloc
1893 for the dynamic linker. */
1894 s = htab->srelgot;
1895 if (s == NULL)
1896 abort ();
1897
1898 outrel.r_offset = (htab->sgot->output_section->vma
1899 + htab->sgot->output_offset
1900 + off);
1901 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1902 outrel.r_addend = relocation;
1903 loc = s->contents;
1904 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
1905 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1906 }
1907
1908 local_got_offsets[r_symndx] |= 1;
1909 }
1910 }
1911
1912 if (off >= (bfd_vma) -2)
1913 abort ();
1914
1915 relocation = htab->sgot->output_section->vma
1916 + htab->sgot->output_offset + off;
1917 if (r_type != R_X86_64_GOTPCREL)
1918 relocation -= htab->sgotplt->output_section->vma
1919 - htab->sgotplt->output_offset;
1920
1921 break;
1922
1923 case R_X86_64_PLT32:
1924 /* Relocation is to the entry for this symbol in the
1925 procedure linkage table. */
1926
1927 /* Resolve a PLT32 reloc against a local symbol directly,
1928 without using the procedure linkage table. */
1929 if (h == NULL)
1930 break;
1931
1932 if (h->plt.offset == (bfd_vma) -1
1933 || htab->splt == NULL)
1934 {
1935 /* We didn't make a PLT entry for this symbol. This
1936 happens when statically linking PIC code, or when
1937 using -Bsymbolic. */
1938 break;
1939 }
1940
1941 relocation = (htab->splt->output_section->vma
1942 + htab->splt->output_offset
1943 + h->plt.offset);
1944 unresolved_reloc = FALSE;
1945 break;
1946
1947 case R_X86_64_PC8:
1948 case R_X86_64_PC16:
1949 case R_X86_64_PC32:
1950 if (info->shared
1951 && !SYMBOL_REFERENCES_LOCAL (info, h)
1952 && (input_section->flags & SEC_ALLOC) != 0
1953 && (input_section->flags & SEC_READONLY) != 0)
1954 {
1955 (*_bfd_error_handler)
1956 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1957 input_bfd,
1958 x86_64_elf_howto_table[r_type].name,
1959 (h) ? h->root.root.string : "a local symbol");
1960 bfd_set_error (bfd_error_bad_value);
1961 return FALSE;
1962 }
1963 /* Fall through. */
1964
1965 case R_X86_64_8:
1966 case R_X86_64_16:
1967 case R_X86_64_32:
1968 case R_X86_64_64:
1969 /* FIXME: The ABI says the linker should make sure the value is
1970 the same when it's zeroextended to 64 bit. */
1971
1972 /* r_symndx will be zero only for relocs against symbols
1973 from removed linkonce sections, or sections discarded by
1974 a linker script. */
1975 if (r_symndx == 0
1976 || (input_section->flags & SEC_ALLOC) == 0)
1977 break;
1978
1979 if ((info->shared
1980 && (h == NULL
1981 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1982 || h->root.type != bfd_link_hash_undefweak)
1983 && ((r_type != R_X86_64_PC8
1984 && r_type != R_X86_64_PC16
1985 && r_type != R_X86_64_PC32)
1986 || !SYMBOL_CALLS_LOCAL (info, h)))
1987 || (ELIMINATE_COPY_RELOCS
1988 && !info->shared
1989 && h != NULL
1990 && h->dynindx != -1
1991 && !h->non_got_ref
1992 && ((h->def_dynamic
1993 && !h->def_regular)
1994 || h->root.type == bfd_link_hash_undefweak
1995 || h->root.type == bfd_link_hash_undefined)))
1996 {
1997 Elf_Internal_Rela outrel;
1998 bfd_byte *loc;
1999 bfd_boolean skip, relocate;
2000 asection *sreloc;
2001
2002 /* When generating a shared object, these relocations
2003 are copied into the output file to be resolved at run
2004 time. */
2005 skip = FALSE;
2006 relocate = FALSE;
2007
2008 outrel.r_offset =
2009 _bfd_elf_section_offset (output_bfd, info, input_section,
2010 rel->r_offset);
2011 if (outrel.r_offset == (bfd_vma) -1)
2012 skip = TRUE;
2013 else if (outrel.r_offset == (bfd_vma) -2)
2014 skip = TRUE, relocate = TRUE;
2015
2016 outrel.r_offset += (input_section->output_section->vma
2017 + input_section->output_offset);
2018
2019 if (skip)
2020 memset (&outrel, 0, sizeof outrel);
2021
2022 /* h->dynindx may be -1 if this symbol was marked to
2023 become local. */
2024 else if (h != NULL
2025 && h->dynindx != -1
2026 && (r_type == R_X86_64_PC8
2027 || r_type == R_X86_64_PC16
2028 || r_type == R_X86_64_PC32
2029 || !info->shared
2030 || !info->symbolic
2031 || !h->def_regular))
2032 {
2033 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2034 outrel.r_addend = rel->r_addend;
2035 }
2036 else
2037 {
2038 /* This symbol is local, or marked to become local. */
2039 if (r_type == R_X86_64_64)
2040 {
2041 relocate = TRUE;
2042 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2043 outrel.r_addend = relocation + rel->r_addend;
2044 }
2045 else
2046 {
2047 long sindx;
2048
2049 if (bfd_is_abs_section (sec))
2050 sindx = 0;
2051 else if (sec == NULL || sec->owner == NULL)
2052 {
2053 bfd_set_error (bfd_error_bad_value);
2054 return FALSE;
2055 }
2056 else
2057 {
2058 asection *osec;
2059
2060 osec = sec->output_section;
2061 sindx = elf_section_data (osec)->dynindx;
2062 BFD_ASSERT (sindx > 0);
2063 }
2064
2065 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2066 outrel.r_addend = relocation + rel->r_addend;
2067 }
2068 }
2069
2070 sreloc = elf_section_data (input_section)->sreloc;
2071 if (sreloc == NULL)
2072 abort ();
2073
2074 loc = sreloc->contents;
2075 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2076 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2077
2078 /* If this reloc is against an external symbol, we do
2079 not want to fiddle with the addend. Otherwise, we
2080 need to include the symbol value so that it becomes
2081 an addend for the dynamic reloc. */
2082 if (! relocate)
2083 continue;
2084 }
2085
2086 break;
2087
2088 case R_X86_64_TLSGD:
2089 case R_X86_64_GOTTPOFF:
2090 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2091 tls_type = GOT_UNKNOWN;
2092 if (h == NULL && local_got_offsets)
2093 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2094 else if (h != NULL)
2095 {
2096 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2097 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2098 r_type = R_X86_64_TPOFF32;
2099 }
2100 if (r_type == R_X86_64_TLSGD)
2101 {
2102 if (tls_type == GOT_TLS_IE)
2103 r_type = R_X86_64_GOTTPOFF;
2104 }
2105
2106 if (r_type == R_X86_64_TPOFF32)
2107 {
2108 BFD_ASSERT (! unresolved_reloc);
2109 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2110 {
2111 unsigned int i;
2112 static unsigned char tlsgd[8]
2113 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2114
2115 /* GD->LE transition.
2116 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2117 .word 0x6666; rex64; call __tls_get_addr@plt
2118 Change it into:
2119 movq %fs:0, %rax
2120 leaq foo@tpoff(%rax), %rax */
2121 BFD_ASSERT (rel->r_offset >= 4);
2122 for (i = 0; i < 4; i++)
2123 BFD_ASSERT (bfd_get_8 (input_bfd,
2124 contents + rel->r_offset - 4 + i)
2125 == tlsgd[i]);
2126 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2127 for (i = 0; i < 4; i++)
2128 BFD_ASSERT (bfd_get_8 (input_bfd,
2129 contents + rel->r_offset + 4 + i)
2130 == tlsgd[i+4]);
2131 BFD_ASSERT (rel + 1 < relend);
2132 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2133 memcpy (contents + rel->r_offset - 4,
2134 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2135 16);
2136 bfd_put_32 (output_bfd, tpoff (info, relocation),
2137 contents + rel->r_offset + 8);
2138 /* Skip R_X86_64_PLT32. */
2139 rel++;
2140 continue;
2141 }
2142 else
2143 {
2144 unsigned int val, type, reg;
2145
2146 /* IE->LE transition:
2147 Originally it can be one of:
2148 movq foo@gottpoff(%rip), %reg
2149 addq foo@gottpoff(%rip), %reg
2150 We change it into:
2151 movq $foo, %reg
2152 leaq foo(%reg), %reg
2153 addq $foo, %reg. */
2154 BFD_ASSERT (rel->r_offset >= 3);
2155 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2156 BFD_ASSERT (val == 0x48 || val == 0x4c);
2157 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2158 BFD_ASSERT (type == 0x8b || type == 0x03);
2159 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2160 BFD_ASSERT ((reg & 0xc7) == 5);
2161 reg >>= 3;
2162 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2163 if (type == 0x8b)
2164 {
2165 /* movq */
2166 if (val == 0x4c)
2167 bfd_put_8 (output_bfd, 0x49,
2168 contents + rel->r_offset - 3);
2169 bfd_put_8 (output_bfd, 0xc7,
2170 contents + rel->r_offset - 2);
2171 bfd_put_8 (output_bfd, 0xc0 | reg,
2172 contents + rel->r_offset - 1);
2173 }
2174 else if (reg == 4)
2175 {
2176 /* addq -> addq - addressing with %rsp/%r12 is
2177 special */
2178 if (val == 0x4c)
2179 bfd_put_8 (output_bfd, 0x49,
2180 contents + rel->r_offset - 3);
2181 bfd_put_8 (output_bfd, 0x81,
2182 contents + rel->r_offset - 2);
2183 bfd_put_8 (output_bfd, 0xc0 | reg,
2184 contents + rel->r_offset - 1);
2185 }
2186 else
2187 {
2188 /* addq -> leaq */
2189 if (val == 0x4c)
2190 bfd_put_8 (output_bfd, 0x4d,
2191 contents + rel->r_offset - 3);
2192 bfd_put_8 (output_bfd, 0x8d,
2193 contents + rel->r_offset - 2);
2194 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2195 contents + rel->r_offset - 1);
2196 }
2197 bfd_put_32 (output_bfd, tpoff (info, relocation),
2198 contents + rel->r_offset);
2199 continue;
2200 }
2201 }
2202
2203 if (htab->sgot == NULL)
2204 abort ();
2205
2206 if (h != NULL)
2207 off = h->got.offset;
2208 else
2209 {
2210 if (local_got_offsets == NULL)
2211 abort ();
2212
2213 off = local_got_offsets[r_symndx];
2214 }
2215
2216 if ((off & 1) != 0)
2217 off &= ~1;
2218 else
2219 {
2220 Elf_Internal_Rela outrel;
2221 bfd_byte *loc;
2222 int dr_type, indx;
2223
2224 if (htab->srelgot == NULL)
2225 abort ();
2226
2227 outrel.r_offset = (htab->sgot->output_section->vma
2228 + htab->sgot->output_offset + off);
2229
2230 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2231 if (r_type == R_X86_64_TLSGD)
2232 dr_type = R_X86_64_DTPMOD64;
2233 else
2234 dr_type = R_X86_64_TPOFF64;
2235
2236 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2237 outrel.r_addend = 0;
2238 if (dr_type == R_X86_64_TPOFF64 && indx == 0)
2239 outrel.r_addend = relocation - dtpoff_base (info);
2240 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2241
2242 loc = htab->srelgot->contents;
2243 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2244 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2245
2246 if (r_type == R_X86_64_TLSGD)
2247 {
2248 if (indx == 0)
2249 {
2250 BFD_ASSERT (! unresolved_reloc);
2251 bfd_put_64 (output_bfd,
2252 relocation - dtpoff_base (info),
2253 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2254 }
2255 else
2256 {
2257 bfd_put_64 (output_bfd, 0,
2258 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2259 outrel.r_info = ELF64_R_INFO (indx,
2260 R_X86_64_DTPOFF64);
2261 outrel.r_offset += GOT_ENTRY_SIZE;
2262 htab->srelgot->reloc_count++;
2263 loc += sizeof (Elf64_External_Rela);
2264 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2265 }
2266 }
2267
2268 if (h != NULL)
2269 h->got.offset |= 1;
2270 else
2271 local_got_offsets[r_symndx] |= 1;
2272 }
2273
2274 if (off >= (bfd_vma) -2)
2275 abort ();
2276 if (r_type == ELF64_R_TYPE (rel->r_info))
2277 {
2278 relocation = htab->sgot->output_section->vma
2279 + htab->sgot->output_offset + off;
2280 unresolved_reloc = FALSE;
2281 }
2282 else
2283 {
2284 unsigned int i;
2285 static unsigned char tlsgd[8]
2286 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2287
2288 /* GD->IE transition.
2289 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2290 .word 0x6666; rex64; call __tls_get_addr@plt
2291 Change it into:
2292 movq %fs:0, %rax
2293 addq foo@gottpoff(%rip), %rax */
2294 BFD_ASSERT (rel->r_offset >= 4);
2295 for (i = 0; i < 4; i++)
2296 BFD_ASSERT (bfd_get_8 (input_bfd,
2297 contents + rel->r_offset - 4 + i)
2298 == tlsgd[i]);
2299 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2300 for (i = 0; i < 4; i++)
2301 BFD_ASSERT (bfd_get_8 (input_bfd,
2302 contents + rel->r_offset + 4 + i)
2303 == tlsgd[i+4]);
2304 BFD_ASSERT (rel + 1 < relend);
2305 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2306 memcpy (contents + rel->r_offset - 4,
2307 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2308 16);
2309
2310 relocation = (htab->sgot->output_section->vma
2311 + htab->sgot->output_offset + off
2312 - rel->r_offset
2313 - input_section->output_section->vma
2314 - input_section->output_offset
2315 - 12);
2316 bfd_put_32 (output_bfd, relocation,
2317 contents + rel->r_offset + 8);
2318 /* Skip R_X86_64_PLT32. */
2319 rel++;
2320 continue;
2321 }
2322 break;
2323
2324 case R_X86_64_TLSLD:
2325 if (! info->shared)
2326 {
2327 /* LD->LE transition:
2328 Ensure it is:
2329 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2330 We change it into:
2331 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2332 BFD_ASSERT (rel->r_offset >= 3);
2333 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2334 == 0x48);
2335 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2336 == 0x8d);
2337 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2338 == 0x3d);
2339 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2340 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2341 == 0xe8);
2342 BFD_ASSERT (rel + 1 < relend);
2343 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2344 memcpy (contents + rel->r_offset - 3,
2345 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2346 /* Skip R_X86_64_PLT32. */
2347 rel++;
2348 continue;
2349 }
2350
2351 if (htab->sgot == NULL)
2352 abort ();
2353
2354 off = htab->tls_ld_got.offset;
2355 if (off & 1)
2356 off &= ~1;
2357 else
2358 {
2359 Elf_Internal_Rela outrel;
2360 bfd_byte *loc;
2361
2362 if (htab->srelgot == NULL)
2363 abort ();
2364
2365 outrel.r_offset = (htab->sgot->output_section->vma
2366 + htab->sgot->output_offset + off);
2367
2368 bfd_put_64 (output_bfd, 0,
2369 htab->sgot->contents + off);
2370 bfd_put_64 (output_bfd, 0,
2371 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2372 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2373 outrel.r_addend = 0;
2374 loc = htab->srelgot->contents;
2375 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2376 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2377 htab->tls_ld_got.offset |= 1;
2378 }
2379 relocation = htab->sgot->output_section->vma
2380 + htab->sgot->output_offset + off;
2381 unresolved_reloc = FALSE;
2382 break;
2383
2384 case R_X86_64_DTPOFF32:
2385 if (info->shared || (input_section->flags & SEC_CODE) == 0)
2386 relocation -= dtpoff_base (info);
2387 else
2388 relocation = tpoff (info, relocation);
2389 break;
2390
2391 case R_X86_64_TPOFF32:
2392 BFD_ASSERT (! info->shared);
2393 relocation = tpoff (info, relocation);
2394 break;
2395
2396 default:
2397 break;
2398 }
2399
2400 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2401 because such sections are not SEC_ALLOC and thus ld.so will
2402 not process them. */
2403 if (unresolved_reloc
2404 && !((input_section->flags & SEC_DEBUGGING) != 0
2405 && h->def_dynamic))
2406 (*_bfd_error_handler)
2407 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2408 input_bfd,
2409 input_section,
2410 (long) rel->r_offset,
2411 h->root.root.string);
2412
2413 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2414 contents, rel->r_offset,
2415 relocation, rel->r_addend);
2416
2417 if (r != bfd_reloc_ok)
2418 {
2419 const char *name;
2420
2421 if (h != NULL)
2422 name = h->root.root.string;
2423 else
2424 {
2425 name = bfd_elf_string_from_elf_section (input_bfd,
2426 symtab_hdr->sh_link,
2427 sym->st_name);
2428 if (name == NULL)
2429 return FALSE;
2430 if (*name == '\0')
2431 name = bfd_section_name (input_bfd, sec);
2432 }
2433
2434 if (r == bfd_reloc_overflow)
2435 {
2436 if (h != NULL
2437 && h->root.type == bfd_link_hash_undefweak
2438 && howto->pc_relative)
2439 /* Ignore reloc overflow on branches to undefweak syms. */
2440 continue;
2441
2442 if (! ((*info->callbacks->reloc_overflow)
2443 (info, (h ? &h->root : NULL), name, howto->name,
2444 (bfd_vma) 0, input_bfd, input_section,
2445 rel->r_offset)))
2446 return FALSE;
2447 }
2448 else
2449 {
2450 (*_bfd_error_handler)
2451 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2452 input_bfd, input_section,
2453 (long) rel->r_offset, name, (int) r);
2454 return FALSE;
2455 }
2456 }
2457 }
2458
2459 return TRUE;
2460 }
2461
2462 /* Finish up dynamic symbol handling. We set the contents of various
2463 dynamic sections here. */
2464
2465 static bfd_boolean
2466 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
2467 struct bfd_link_info *info,
2468 struct elf_link_hash_entry *h,
2469 Elf_Internal_Sym *sym)
2470 {
2471 struct elf64_x86_64_link_hash_table *htab;
2472
2473 htab = elf64_x86_64_hash_table (info);
2474
2475 if (h->plt.offset != (bfd_vma) -1)
2476 {
2477 bfd_vma plt_index;
2478 bfd_vma got_offset;
2479 Elf_Internal_Rela rela;
2480 bfd_byte *loc;
2481
2482 /* This symbol has an entry in the procedure linkage table. Set
2483 it up. */
2484 if (h->dynindx == -1
2485 || htab->splt == NULL
2486 || htab->sgotplt == NULL
2487 || htab->srelplt == NULL)
2488 abort ();
2489
2490 /* Get the index in the procedure linkage table which
2491 corresponds to this symbol. This is the index of this symbol
2492 in all the symbols for which we are making plt entries. The
2493 first entry in the procedure linkage table is reserved. */
2494 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2495
2496 /* Get the offset into the .got table of the entry that
2497 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
2498 bytes. The first three are reserved for the dynamic linker. */
2499 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2500
2501 /* Fill in the entry in the procedure linkage table. */
2502 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
2503 PLT_ENTRY_SIZE);
2504
2505 /* Insert the relocation positions of the plt section. The magic
2506 numbers at the end of the statements are the positions of the
2507 relocations in the plt section. */
2508 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2509 instruction uses 6 bytes, subtract this value. */
2510 bfd_put_32 (output_bfd,
2511 (htab->sgotplt->output_section->vma
2512 + htab->sgotplt->output_offset
2513 + got_offset
2514 - htab->splt->output_section->vma
2515 - htab->splt->output_offset
2516 - h->plt.offset
2517 - 6),
2518 htab->splt->contents + h->plt.offset + 2);
2519 /* Put relocation index. */
2520 bfd_put_32 (output_bfd, plt_index,
2521 htab->splt->contents + h->plt.offset + 7);
2522 /* Put offset for jmp .PLT0. */
2523 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
2524 htab->splt->contents + h->plt.offset + 12);
2525
2526 /* Fill in the entry in the global offset table, initially this
2527 points to the pushq instruction in the PLT which is at offset 6. */
2528 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2529 + htab->splt->output_offset
2530 + h->plt.offset + 6),
2531 htab->sgotplt->contents + got_offset);
2532
2533 /* Fill in the entry in the .rela.plt section. */
2534 rela.r_offset = (htab->sgotplt->output_section->vma
2535 + htab->sgotplt->output_offset
2536 + got_offset);
2537 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2538 rela.r_addend = 0;
2539 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
2540 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2541
2542 if (!h->def_regular)
2543 {
2544 /* Mark the symbol as undefined, rather than as defined in
2545 the .plt section. Leave the value if there were any
2546 relocations where pointer equality matters (this is a clue
2547 for the dynamic linker, to make function pointer
2548 comparisons work between an application and shared
2549 library), otherwise set it to zero. If a function is only
2550 called from a binary, there is no need to slow down
2551 shared libraries because of that. */
2552 sym->st_shndx = SHN_UNDEF;
2553 if (!h->pointer_equality_needed)
2554 sym->st_value = 0;
2555 }
2556 }
2557
2558 if (h->got.offset != (bfd_vma) -1
2559 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD
2560 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
2561 {
2562 Elf_Internal_Rela rela;
2563 bfd_byte *loc;
2564
2565 /* This symbol has an entry in the global offset table. Set it
2566 up. */
2567 if (htab->sgot == NULL || htab->srelgot == NULL)
2568 abort ();
2569
2570 rela.r_offset = (htab->sgot->output_section->vma
2571 + htab->sgot->output_offset
2572 + (h->got.offset &~ (bfd_vma) 1));
2573
2574 /* If this is a static link, or it is a -Bsymbolic link and the
2575 symbol is defined locally or was forced to be local because
2576 of a version file, we just want to emit a RELATIVE reloc.
2577 The entry in the global offset table will already have been
2578 initialized in the relocate_section function. */
2579 if (info->shared
2580 && SYMBOL_REFERENCES_LOCAL (info, h))
2581 {
2582 BFD_ASSERT((h->got.offset & 1) != 0);
2583 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2584 rela.r_addend = (h->root.u.def.value
2585 + h->root.u.def.section->output_section->vma
2586 + h->root.u.def.section->output_offset);
2587 }
2588 else
2589 {
2590 BFD_ASSERT((h->got.offset & 1) == 0);
2591 bfd_put_64 (output_bfd, (bfd_vma) 0,
2592 htab->sgot->contents + h->got.offset);
2593 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2594 rela.r_addend = 0;
2595 }
2596
2597 loc = htab->srelgot->contents;
2598 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2599 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2600 }
2601
2602 if (h->needs_copy)
2603 {
2604 Elf_Internal_Rela rela;
2605 bfd_byte *loc;
2606
2607 /* This symbol needs a copy reloc. Set it up. */
2608
2609 if (h->dynindx == -1
2610 || (h->root.type != bfd_link_hash_defined
2611 && h->root.type != bfd_link_hash_defweak)
2612 || htab->srelbss == NULL)
2613 abort ();
2614
2615 rela.r_offset = (h->root.u.def.value
2616 + h->root.u.def.section->output_section->vma
2617 + h->root.u.def.section->output_offset);
2618 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2619 rela.r_addend = 0;
2620 loc = htab->srelbss->contents;
2621 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
2622 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2623 }
2624
2625 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2626 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2627 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2628 sym->st_shndx = SHN_ABS;
2629
2630 return TRUE;
2631 }
2632
2633 /* Used to decide how to sort relocs in an optimal manner for the
2634 dynamic linker, before writing them out. */
2635
2636 static enum elf_reloc_type_class
2637 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
2638 {
2639 switch ((int) ELF64_R_TYPE (rela->r_info))
2640 {
2641 case R_X86_64_RELATIVE:
2642 return reloc_class_relative;
2643 case R_X86_64_JUMP_SLOT:
2644 return reloc_class_plt;
2645 case R_X86_64_COPY:
2646 return reloc_class_copy;
2647 default:
2648 return reloc_class_normal;
2649 }
2650 }
2651
2652 /* Finish up the dynamic sections. */
2653
2654 static bfd_boolean
2655 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2656 {
2657 struct elf64_x86_64_link_hash_table *htab;
2658 bfd *dynobj;
2659 asection *sdyn;
2660
2661 htab = elf64_x86_64_hash_table (info);
2662 dynobj = htab->elf.dynobj;
2663 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2664
2665 if (htab->elf.dynamic_sections_created)
2666 {
2667 Elf64_External_Dyn *dyncon, *dynconend;
2668
2669 if (sdyn == NULL || htab->sgot == NULL)
2670 abort ();
2671
2672 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2673 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2674 for (; dyncon < dynconend; dyncon++)
2675 {
2676 Elf_Internal_Dyn dyn;
2677 asection *s;
2678
2679 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2680
2681 switch (dyn.d_tag)
2682 {
2683 default:
2684 continue;
2685
2686 case DT_PLTGOT:
2687 s = htab->sgotplt;
2688 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2689 break;
2690
2691 case DT_JMPREL:
2692 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2693 break;
2694
2695 case DT_PLTRELSZ:
2696 s = htab->srelplt->output_section;
2697 dyn.d_un.d_val = s->size;
2698 break;
2699
2700 case DT_RELASZ:
2701 /* The procedure linkage table relocs (DT_JMPREL) should
2702 not be included in the overall relocs (DT_RELA).
2703 Therefore, we override the DT_RELASZ entry here to
2704 make it not include the JMPREL relocs. Since the
2705 linker script arranges for .rela.plt to follow all
2706 other relocation sections, we don't have to worry
2707 about changing the DT_RELA entry. */
2708 if (htab->srelplt != NULL)
2709 {
2710 s = htab->srelplt->output_section;
2711 dyn.d_un.d_val -= s->size;
2712 }
2713 break;
2714 }
2715
2716 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2717 }
2718
2719 /* Fill in the special first entry in the procedure linkage table. */
2720 if (htab->splt && htab->splt->size > 0)
2721 {
2722 /* Fill in the first entry in the procedure linkage table. */
2723 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2724 PLT_ENTRY_SIZE);
2725 /* Add offset for pushq GOT+8(%rip), since the instruction
2726 uses 6 bytes subtract this value. */
2727 bfd_put_32 (output_bfd,
2728 (htab->sgotplt->output_section->vma
2729 + htab->sgotplt->output_offset
2730 + 8
2731 - htab->splt->output_section->vma
2732 - htab->splt->output_offset
2733 - 6),
2734 htab->splt->contents + 2);
2735 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2736 the end of the instruction. */
2737 bfd_put_32 (output_bfd,
2738 (htab->sgotplt->output_section->vma
2739 + htab->sgotplt->output_offset
2740 + 16
2741 - htab->splt->output_section->vma
2742 - htab->splt->output_offset
2743 - 12),
2744 htab->splt->contents + 8);
2745
2746 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2747 PLT_ENTRY_SIZE;
2748 }
2749 }
2750
2751 if (htab->sgotplt)
2752 {
2753 /* Fill in the first three entries in the global offset table. */
2754 if (htab->sgotplt->size > 0)
2755 {
2756 /* Set the first entry in the global offset table to the address of
2757 the dynamic section. */
2758 if (sdyn == NULL)
2759 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2760 else
2761 bfd_put_64 (output_bfd,
2762 sdyn->output_section->vma + sdyn->output_offset,
2763 htab->sgotplt->contents);
2764 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2765 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2766 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2767 }
2768
2769 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2770 GOT_ENTRY_SIZE;
2771 }
2772
2773 if (htab->sgot && htab->sgot->size > 0)
2774 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
2775 = GOT_ENTRY_SIZE;
2776
2777 return TRUE;
2778 }
2779
2780 /* Return address for Ith PLT stub in section PLT, for relocation REL
2781 or (bfd_vma) -1 if it should not be included. */
2782
2783 static bfd_vma
2784 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
2785 const arelent *rel ATTRIBUTE_UNUSED)
2786 {
2787 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
2788 }
2789
2790 /* Handle an x86-64 specific section when reading an object file. This
2791 is called when elfcode.h finds a section with an unknown type. */
2792
2793 static bfd_boolean
2794 elf64_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
2795 {
2796 if (hdr->sh_type != SHT_X86_64_UNWIND)
2797 return FALSE;
2798
2799 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2800 return FALSE;
2801
2802 return TRUE;
2803 }
2804
2805 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2806 #define TARGET_LITTLE_NAME "elf64-x86-64"
2807 #define ELF_ARCH bfd_arch_i386
2808 #define ELF_MACHINE_CODE EM_X86_64
2809 #define ELF_MAXPAGESIZE 0x100000
2810
2811 #define elf_backend_can_gc_sections 1
2812 #define elf_backend_can_refcount 1
2813 #define elf_backend_want_got_plt 1
2814 #define elf_backend_plt_readonly 1
2815 #define elf_backend_want_plt_sym 0
2816 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2817 #define elf_backend_rela_normal 1
2818
2819 #define elf_info_to_howto elf64_x86_64_info_to_howto
2820
2821 #define bfd_elf64_bfd_link_hash_table_create \
2822 elf64_x86_64_link_hash_table_create
2823 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
2824
2825 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2826 #define elf_backend_check_relocs elf64_x86_64_check_relocs
2827 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2828 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2829 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
2830 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2831 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2832 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
2833 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2834 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
2835 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
2836 #define elf_backend_relocate_section elf64_x86_64_relocate_section
2837 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
2838 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
2839 #define elf_backend_object_p elf64_x86_64_elf_object_p
2840 #define bfd_elf64_mkobject elf64_x86_64_mkobject
2841
2842 #define elf_backend_section_from_shdr \
2843 elf64_x86_64_section_from_shdr
2844
2845 #include "elf64-target.h"
This page took 0.124596 seconds and 5 git commands to generate.