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