* emulparams/vax.sh (OUTPUT_FORMAT): Use a.out-vax-bsd format.
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
8d88c4ca 1/* X86-64 specific support for 64-bit ELF
c434dee6 2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
8d88c4ca
NC
3 Contributed by Jan Hubicka <jh@suse.cz>.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
c434dee6 23#include "bfdlink.h"
8d88c4ca
NC
24#include "libbfd.h"
25#include "elf-bfd.h"
26
27#include "elf/x86-64.h"
28
29/* We use only the RELA entries. */
c434dee6 30#define USE_RELA 1
8d88c4ca
NC
31
32/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33#define MINUS_ONE (~ (bfd_vma) 0)
34
35/* The relocation "howto" table. Order of fields:
407443a3
AJ
36 type, size, bitsize, pc_relative, complain_on_overflow,
37 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
70256ad8
AJ
38static reloc_howto_type x86_64_elf_howto_table[] =
39{
407443a3 40 HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
fe4770f4
AJ
41 bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0x00000000, 0x00000000,
42 false),
407443a3 43 HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
44 bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE,
45 false),
407443a3 46 HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
fe4770f4
AJ
47 bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff,
48 true),
407443a3 49 HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
fe4770f4
AJ
50 bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff,
51 false),
407443a3 52 HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
fe4770f4
AJ
53 bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff,
54 true),
407443a3 55 HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
56 bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff,
57 false),
407443a3 58 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
59 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE,
60 MINUS_ONE, false),
407443a3 61 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
62 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE,
63 MINUS_ONE, false),
4a73f21b 64 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
65 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE,
66 MINUS_ONE, false),
407443a3 67 HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed,
fe4770f4
AJ
68 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff,
69 0xffffffff, true),
407443a3 70 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
fe4770f4
AJ
71 bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
72 false),
407443a3 73 HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
fe4770f4
AJ
74 bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff,
75 false),
407443a3
AJ
76 HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_16", false, 0xffff, 0xffff, false),
78 HOWTO(R_X86_64_PC16,0, 1, 16, true, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0xffff, 0xffff, true),
80 HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_signed,
81 bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false),
82 HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
fe4770f4
AJ
83 bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true),
84
85/* GNU extension to record C++ vtable hierarchy. */
86 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont,
87 NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
88
89/* GNU extension to record C++ vtable member usage. */
90 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, false, 0, complain_overflow_dont,
91 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
92 false)
8d88c4ca
NC
93};
94
95/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
96struct elf_reloc_map
97{
8d88c4ca
NC
98 bfd_reloc_code_real_type bfd_reloc_val;
99 unsigned char elf_reloc_val;
100};
101
dc810e39 102static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 103{
70256ad8
AJ
104 { BFD_RELOC_NONE, R_X86_64_NONE, },
105 { BFD_RELOC_64, R_X86_64_64, },
106 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
107 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
108 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
109 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
110 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
111 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
112 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
113 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
114 { BFD_RELOC_32, R_X86_64_32, },
115 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
116 { BFD_RELOC_16, R_X86_64_16, },
117 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
118 { BFD_RELOC_8, R_X86_64_8, },
119 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
fe4770f4
AJ
120 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
121 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
122};
123
8d88c4ca
NC
124static reloc_howto_type *elf64_x86_64_reloc_type_lookup
125 PARAMS ((bfd *, bfd_reloc_code_real_type));
126static void elf64_x86_64_info_to_howto
127 PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
cedb70c5 128static boolean elf64_x86_64_grok_prstatus
3bab7989 129 PARAMS ((bfd *, Elf_Internal_Note *));
cedb70c5 130static boolean elf64_x86_64_grok_psinfo
3bab7989 131 PARAMS ((bfd *, Elf_Internal_Note *));
8d88c4ca
NC
132static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
133 PARAMS ((bfd *));
1e1b3bc2 134static boolean elf64_x86_64_elf_object_p PARAMS ((bfd *abfd));
c434dee6
AJ
135static boolean create_got_section
136 PARAMS((bfd *, struct bfd_link_info *));
137static boolean elf64_x86_64_create_dynamic_sections
138 PARAMS((bfd *, struct bfd_link_info *));
139static void elf64_x86_64_copy_indirect_symbol
140 PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
1e1b3bc2
AJ
141static boolean elf64_x86_64_check_relocs
142 PARAMS ((bfd *, struct bfd_link_info *, asection *sec,
143 const Elf_Internal_Rela *));
144static asection *elf64_x86_64_gc_mark_hook
1e2f5b6e 145 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
1e1b3bc2
AJ
146 struct elf_link_hash_entry *, Elf_Internal_Sym *));
147
148static boolean elf64_x86_64_gc_sweep_hook
149 PARAMS ((bfd *, struct bfd_link_info *, asection *,
150 const Elf_Internal_Rela *));
70256ad8 151
c434dee6 152static struct bfd_hash_entry *link_hash_newfunc
70256ad8
AJ
153 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
154static boolean elf64_x86_64_adjust_dynamic_symbol
155 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
156
c434dee6
AJ
157static boolean allocate_dynrelocs
158 PARAMS ((struct elf_link_hash_entry *, PTR));
159static boolean readonly_dynrelocs
160 PARAMS ((struct elf_link_hash_entry *, PTR));
70256ad8
AJ
161static boolean elf64_x86_64_size_dynamic_sections
162 PARAMS ((bfd *, struct bfd_link_info *));
8d88c4ca
NC
163static boolean elf64_x86_64_relocate_section
164 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
407443a3 165 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
70256ad8
AJ
166static boolean elf64_x86_64_finish_dynamic_symbol
167 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
168 Elf_Internal_Sym *sym));
169static boolean elf64_x86_64_finish_dynamic_sections
170 PARAMS ((bfd *, struct bfd_link_info *));
f51e552e
AM
171static enum elf_reloc_type_class elf64_x86_64_reloc_type_class
172 PARAMS ((const Elf_Internal_Rela *));
8d88c4ca
NC
173
174/* Given a BFD reloc type, return a HOWTO structure. */
175static reloc_howto_type *
176elf64_x86_64_reloc_type_lookup (abfd, code)
177 bfd *abfd ATTRIBUTE_UNUSED;
178 bfd_reloc_code_real_type code;
179{
180 unsigned int i;
181 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
182 i++)
183 {
184 if (x86_64_reloc_map[i].bfd_reloc_val == code)
ffaef159 185 return &x86_64_elf_howto_table[i];
8d88c4ca
NC
186 }
187 return 0;
188}
189
8d88c4ca 190/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 191
8d88c4ca
NC
192static void
193elf64_x86_64_info_to_howto (abfd, cache_ptr, dst)
194 bfd *abfd ATTRIBUTE_UNUSED;
195 arelent *cache_ptr;
196 Elf64_Internal_Rela *dst;
197{
fe4770f4 198 unsigned r_type, i;
8d88c4ca
NC
199
200 r_type = ELF64_R_TYPE (dst->r_info);
fe4770f4
AJ
201 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
202 {
203 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_PC8);
204 i = r_type;
205 }
206 else
207 {
208 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
209 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_PC8 - 1);
210 }
211 cache_ptr->howto = &x86_64_elf_howto_table[i];
8d88c4ca
NC
212 BFD_ASSERT (r_type == cache_ptr->howto->type);
213}
70256ad8 214\f
3bab7989
ML
215/* Support for core dump NOTE sections. */
216static boolean
217elf64_x86_64_grok_prstatus (abfd, note)
218 bfd *abfd;
219 Elf_Internal_Note *note;
220{
221 int offset;
222 size_t raw_size;
223
224 switch (note->descsz)
225 {
226 default:
227 return false;
228
229 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
230 /* pr_cursig */
cedb70c5 231 elf_tdata (abfd)->core_signal
3bab7989
ML
232 = bfd_get_16 (abfd, note->descdata + 12);
233
234 /* pr_pid */
cedb70c5 235 elf_tdata (abfd)->core_pid
3bab7989
ML
236 = bfd_get_32 (abfd, note->descdata + 32);
237
238 /* pr_reg */
239 offset = 112;
240 raw_size = 216;
241
242 break;
243 }
244
245 /* Make a ".reg/999" section. */
246 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
247 raw_size, note->descpos + offset);
248}
249
250static boolean
251elf64_x86_64_grok_psinfo (abfd, note)
252 bfd *abfd;
253 Elf_Internal_Note *note;
254{
255 switch (note->descsz)
256 {
257 default:
258 return false;
259
260 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
261 elf_tdata (abfd)->core_program
262 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
263 elf_tdata (abfd)->core_command
264 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
265 }
266
267 /* Note that for some reason, a spurious space is tacked
268 onto the end of the args in some (at least one anyway)
269 implementations, so strip it off if it exists. */
270
271 {
272 char *command = elf_tdata (abfd)->core_command;
273 int n = strlen (command);
274
275 if (0 < n && command[n - 1] == ' ')
276 command[n - 1] = '\0';
277 }
278
279 return true;
280}
281\f
407443a3 282/* Functions for the x86-64 ELF linker. */
70256ad8 283
407443a3 284/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
285 section. */
286
407443a3 287#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
70256ad8
AJ
288
289/* The size in bytes of an entry in the global offset table. */
290
291#define GOT_ENTRY_SIZE 8
8d88c4ca 292
70256ad8 293/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 294
70256ad8
AJ
295#define PLT_ENTRY_SIZE 16
296
297/* The first entry in a procedure linkage table looks like this. See the
298 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
299
300static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
301{
653165cc
AJ
302 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
303 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
304 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
70256ad8
AJ
305};
306
307/* Subsequent entries in a procedure linkage table look like this. */
308
309static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
310{
653165cc 311 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 312 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 313 0x68, /* pushq immediate */
70256ad8
AJ
314 0, 0, 0, 0, /* replaced with index into relocation table. */
315 0xe9, /* jmp relative */
316 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
317};
318
319/* The x86-64 linker needs to keep track of the number of relocs that
985142a4 320 it decides to copy as dynamic relocs in check_relocs for each symbol.
c434dee6
AJ
321 This is so that it can later discard them if they are found to be
322 unnecessary. We store the information in a field extending the
323 regular ELF linker hash table. */
70256ad8 324
c434dee6 325struct elf64_x86_64_dyn_relocs
70256ad8
AJ
326{
327 /* Next section. */
c434dee6
AJ
328 struct elf64_x86_64_dyn_relocs *next;
329
330 /* The input section of the reloc. */
331 asection *sec;
332
333 /* Total number of relocs copied for the input section. */
70256ad8 334 bfd_size_type count;
c434dee6
AJ
335
336 /* Number of pc-relative relocs copied for the input section. */
337 bfd_size_type pc_count;
70256ad8
AJ
338};
339
340/* x86-64 ELF linker hash entry. */
341
342struct elf64_x86_64_link_hash_entry
343{
c434dee6 344 struct elf_link_hash_entry elf;
70256ad8 345
c434dee6
AJ
346 /* Track dynamic relocs copied for this symbol. */
347 struct elf64_x86_64_dyn_relocs *dyn_relocs;
70256ad8
AJ
348};
349
c434dee6 350/* x86-64 ELF linker hash table. */
8d88c4ca 351
407443a3
AJ
352struct elf64_x86_64_link_hash_table
353{
c434dee6 354 struct elf_link_hash_table elf;
70256ad8 355
c434dee6
AJ
356 /* Short-cuts to get to dynamic linker sections. */
357 asection *sgot;
358 asection *sgotplt;
359 asection *srelgot;
360 asection *splt;
361 asection *srelplt;
362 asection *sdynbss;
363 asection *srelbss;
70256ad8 364
c434dee6
AJ
365 /* Small local sym to section mapping cache. */
366 struct sym_sec_cache sym_sec;
367};
70256ad8
AJ
368
369/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca
NC
370
371#define elf64_x86_64_hash_table(p) \
372 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
373
407443a3 374/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
375
376static struct bfd_hash_entry *
c434dee6 377link_hash_newfunc (entry, table, string)
70256ad8
AJ
378 struct bfd_hash_entry *entry;
379 struct bfd_hash_table *table;
380 const char *string;
381{
70256ad8 382 /* Allocate the structure if it has not already been allocated by a
c434dee6
AJ
383 subclass. */
384 if (entry == NULL)
385 {
386 entry = bfd_hash_allocate (table,
387 sizeof (struct elf64_x86_64_link_hash_entry));
388 if (entry == NULL)
389 return entry;
390 }
70256ad8
AJ
391
392 /* Call the allocation method of the superclass. */
c434dee6
AJ
393 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
394 if (entry != NULL)
70256ad8 395 {
c434dee6
AJ
396 struct elf64_x86_64_link_hash_entry *eh;
397
398 eh = (struct elf64_x86_64_link_hash_entry *) entry;
399 eh->dyn_relocs = NULL;
70256ad8
AJ
400 }
401
c434dee6 402 return entry;
70256ad8
AJ
403}
404
8d88c4ca
NC
405/* Create an X86-64 ELF linker hash table. */
406
407static struct bfd_link_hash_table *
408elf64_x86_64_link_hash_table_create (abfd)
409 bfd *abfd;
410{
411 struct elf64_x86_64_link_hash_table *ret;
dc810e39 412 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
8d88c4ca 413
e2d34d7d 414 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
c434dee6 415 if (ret == NULL)
8d88c4ca
NC
416 return NULL;
417
c434dee6 418 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
8d88c4ca 419 {
e2d34d7d 420 free (ret);
8d88c4ca
NC
421 return NULL;
422 }
423
c434dee6
AJ
424 ret->sgot = NULL;
425 ret->sgotplt = NULL;
426 ret->srelgot = NULL;
427 ret->splt = NULL;
428 ret->srelplt = NULL;
429 ret->sdynbss = NULL;
430 ret->srelbss = NULL;
431 ret->sym_sec.abfd = NULL;
432
433 return &ret->elf.root;
434}
435
436/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
437 shortcuts to them in our hash table. */
438
439static boolean
440create_got_section (dynobj, info)
441 bfd *dynobj;
442 struct bfd_link_info *info;
443{
444 struct elf64_x86_64_link_hash_table *htab;
445
446 if (! _bfd_elf_create_got_section (dynobj, info))
447 return false;
448
449 htab = elf64_x86_64_hash_table (info);
450 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
451 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
452 if (!htab->sgot || !htab->sgotplt)
453 abort ();
454
455 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
456 if (htab->srelgot == NULL
457 || ! bfd_set_section_flags (dynobj, htab->srelgot,
458 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
459 | SEC_IN_MEMORY | SEC_LINKER_CREATED
460 | SEC_READONLY))
461 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
462 return false;
463 return true;
464}
465
466/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
467 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
468 hash table. */
469
470static boolean
471elf64_x86_64_create_dynamic_sections (dynobj, info)
472 bfd *dynobj;
473 struct bfd_link_info *info;
474{
475 struct elf64_x86_64_link_hash_table *htab;
476
477 htab = elf64_x86_64_hash_table (info);
478 if (!htab->sgot && !create_got_section (dynobj, info))
479 return false;
480
481 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
482 return false;
483
484 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
485 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
486 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
487 if (!info->shared)
488 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
489
490 if (!htab->splt || !htab->srelplt || !htab->sdynbss
491 || (!info->shared && !htab->srelbss))
492 abort ();
493
494 return true;
495}
496
497/* Copy the extra info we tack onto an elf_link_hash_entry. */
498
499static void
500elf64_x86_64_copy_indirect_symbol (dir, ind)
501 struct elf_link_hash_entry *dir, *ind;
502{
503 struct elf64_x86_64_link_hash_entry *edir, *eind;
504
505 edir = (struct elf64_x86_64_link_hash_entry *) dir;
506 eind = (struct elf64_x86_64_link_hash_entry *) ind;
507
508 if (eind->dyn_relocs != NULL)
509 {
510 if (edir->dyn_relocs != NULL)
511 {
512 struct elf64_x86_64_dyn_relocs **pp;
513 struct elf64_x86_64_dyn_relocs *p;
514
515 if (ind->root.type == bfd_link_hash_indirect)
516 abort ();
517
518 /* Add reloc counts against the weak sym to the strong sym
519 list. Merge any entries against the same section. */
520 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
521 {
522 struct elf64_x86_64_dyn_relocs *q;
523
524 for (q = edir->dyn_relocs; q != NULL; q = q->next)
525 if (q->sec == p->sec)
526 {
527 q->pc_count += p->pc_count;
528 q->count += p->count;
529 *pp = p->next;
530 break;
531 }
532 if (q == NULL)
533 pp = &p->next;
534 }
535 *pp = edir->dyn_relocs;
536 }
537
538 edir->dyn_relocs = eind->dyn_relocs;
539 eind->dyn_relocs = NULL;
540 }
541
542 _bfd_elf_link_hash_copy_indirect (dir, ind);
8d88c4ca
NC
543}
544
1e1b3bc2 545static boolean
8d88c4ca
NC
546elf64_x86_64_elf_object_p (abfd)
547 bfd *abfd;
548{
549 /* Set the right machine number for an x86-64 elf64 file. */
550 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
551 return true;
552}
553
70256ad8 554/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
555 calculate needed space in the global offset table, procedure
556 linkage table, and dynamic reloc sections. */
70256ad8
AJ
557
558static boolean
559elf64_x86_64_check_relocs (abfd, info, sec, relocs)
560 bfd *abfd;
561 struct bfd_link_info *info;
562 asection *sec;
563 const Elf_Internal_Rela *relocs;
564{
c434dee6 565 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
566 Elf_Internal_Shdr *symtab_hdr;
567 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
568 const Elf_Internal_Rela *rel;
569 const Elf_Internal_Rela *rel_end;
70256ad8
AJ
570 asection *sreloc;
571
572 if (info->relocateable)
573 return true;
574
c434dee6 575 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
576 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
577 sym_hashes = elf_sym_hashes (abfd);
70256ad8 578
c434dee6
AJ
579 sreloc = NULL;
580
70256ad8
AJ
581 rel_end = relocs + sec->reloc_count;
582 for (rel = relocs; rel < rel_end; rel++)
583 {
584 unsigned long r_symndx;
585 struct elf_link_hash_entry *h;
586
587 r_symndx = ELF64_R_SYM (rel->r_info);
c434dee6
AJ
588
589 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
590 {
591 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
592 bfd_archive_filename (abfd),
593 r_symndx);
594 return false;
595 }
596
70256ad8
AJ
597 if (r_symndx < symtab_hdr->sh_info)
598 h = NULL;
599 else
600 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
601
70256ad8
AJ
602 switch (ELF64_R_TYPE (rel->r_info))
603 {
604 case R_X86_64_GOT32:
c434dee6 605 case R_X86_64_GOTPCREL:
407443a3 606 /* This symbol requires a global offset table entry. */
70256ad8
AJ
607 if (h != NULL)
608 {
51b64d56 609 h->got.refcount += 1;
70256ad8
AJ
610 }
611 else
612 {
c434dee6
AJ
613 bfd_signed_vma *local_got_refcounts;
614
70256ad8 615 /* This is a global offset table entry for a local symbol. */
c434dee6 616 local_got_refcounts = elf_local_got_refcounts (abfd);
70256ad8
AJ
617 if (local_got_refcounts == NULL)
618 {
dc810e39 619 bfd_size_type size;
70256ad8 620
dc810e39
AM
621 size = symtab_hdr->sh_info;
622 size *= sizeof (bfd_signed_vma);
70256ad8 623 local_got_refcounts = ((bfd_signed_vma *)
51b64d56 624 bfd_zalloc (abfd, size));
70256ad8
AJ
625 if (local_got_refcounts == NULL)
626 return false;
627 elf_local_got_refcounts (abfd) = local_got_refcounts;
70256ad8 628 }
51b64d56 629 local_got_refcounts[r_symndx] += 1;
70256ad8 630 }
c434dee6
AJ
631 /* Fall through */
632
633 //case R_X86_64_GOTPCREL:
634 if (htab->sgot == NULL)
635 {
636 if (htab->elf.dynobj == NULL)
637 htab->elf.dynobj = abfd;
638 if (!create_got_section (htab->elf.dynobj, info))
639 return false;
640 }
70256ad8
AJ
641 break;
642
643 case R_X86_64_PLT32:
644 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
645 actually build the entry in adjust_dynamic_symbol,
646 because this might be a case of linking PIC code which is
647 never referenced by a dynamic object, in which case we
648 don't need to generate a procedure linkage table entry
649 after all. */
70256ad8
AJ
650
651 /* If this is a local symbol, we resolve it directly without
407443a3 652 creating a procedure linkage table entry. */
70256ad8
AJ
653 if (h == NULL)
654 continue;
655
cc78d0af 656 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
51b64d56 657 h->plt.refcount += 1;
70256ad8
AJ
658 break;
659
cc78d0af
AJ
660 case R_X86_64_8:
661 case R_X86_64_16:
70256ad8
AJ
662 case R_X86_64_32:
663 case R_X86_64_32S:
1b71fb54
AJ
664 /* Let's help debug shared library creation. These relocs
665 cannot be used in shared libs. Don't error out for
666 sections we don't care about, such as debug sections or
667 non-constant sections. */
668 if (info->shared
669 && (sec->flags & SEC_ALLOC) != 0
670 && (sec->flags & SEC_READONLY) != 0)
671 {
672 (*_bfd_error_handler)
673 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
674 bfd_archive_filename (abfd),
675 x86_64_elf_howto_table[ELF64_R_TYPE (rel->r_info)].name);
676 bfd_set_error (bfd_error_bad_value);
677 return false;
678 }
679 /* Fall through. */
680
c434dee6
AJ
681 case R_X86_64_PC8:
682 case R_X86_64_PC16:
70256ad8 683 case R_X86_64_PC32:
1b71fb54 684 case R_X86_64_64:
c434dee6
AJ
685 if (h != NULL && !info->shared)
686 {
687 /* If this reloc is in a read-only section, we might
688 need a copy reloc. We can't check reliably at this
689 stage whether the section is read-only, as input
690 sections have not yet been mapped to output sections.
691 Tentatively set the flag for now, and correct in
692 adjust_dynamic_symbol. */
693 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
694
695 /* We may need a .plt entry if the function this reloc
696 refers to is in a shared lib. */
697 h->plt.refcount += 1;
698 }
70256ad8
AJ
699
700 /* If we are creating a shared library, and this is a reloc
701 against a global symbol, or a non PC relative reloc
702 against a local symbol, then we need to copy the reloc
703 into the shared library. However, if we are linking with
704 -Bsymbolic, we do not need to copy a reloc against a
705 global symbol which is defined in an object we are
407443a3 706 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
707 this point we have not seen all the input files, so it is
708 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
709 later (it is never cleared). In case of a weak definition,
710 DEF_REGULAR may be cleared later by a strong definition in
711 a shared library. We account for that possibility below by
712 storing information in the relocs_copied field of the hash
713 table entry. A similar situation occurs when creating
714 shared libraries and symbol visibility changes render the
715 symbol local.
716
717 If on the other hand, we are creating an executable, we
718 may need to keep relocations for symbols satisfied by a
719 dynamic library if we manage to avoid copy relocs for the
720 symbol. */
721 if ((info->shared
722 && (sec->flags & SEC_ALLOC) != 0
723 && (((ELF64_R_TYPE (rel->r_info) != R_X86_64_PC8)
724 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC16)
725 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32))
726 || (h != NULL
727 && (! info->symbolic
728 || h->root.type == bfd_link_hash_defweak
729 || (h->elf_link_hash_flags
730 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
731 || (!info->shared
732 && (sec->flags & SEC_ALLOC) != 0
733 && h != NULL
734 && (h->root.type == bfd_link_hash_defweak
735 || (h->elf_link_hash_flags
736 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
70256ad8 737 {
c434dee6
AJ
738 struct elf64_x86_64_dyn_relocs *p;
739 struct elf64_x86_64_dyn_relocs **head;
740
741 /* We must copy these reloc types into the output file.
742 Create a reloc section in dynobj and make room for
743 this reloc. */
70256ad8
AJ
744 if (sreloc == NULL)
745 {
746 const char *name;
c434dee6 747 bfd *dynobj;
70256ad8
AJ
748
749 name = (bfd_elf_string_from_elf_section
750 (abfd,
751 elf_elfheader (abfd)->e_shstrndx,
752 elf_section_data (sec)->rel_hdr.sh_name));
753 if (name == NULL)
754 return false;
755
c434dee6
AJ
756 if (strncmp (name, ".rela", 5) != 0
757 || strcmp (bfd_get_section_name (abfd, sec),
758 name + 5) != 0)
759 {
760 (*_bfd_error_handler)
761 (_("%s: bad relocation section name `%s\'"),
762 bfd_archive_filename (abfd), name);
763 }
764
765 if (htab->elf.dynobj == NULL)
766 htab->elf.dynobj = abfd;
767
768 dynobj = htab->elf.dynobj;
70256ad8
AJ
769
770 sreloc = bfd_get_section_by_name (dynobj, name);
771 if (sreloc == NULL)
772 {
773 flagword flags;
774
775 sreloc = bfd_make_section (dynobj, name);
776 flags = (SEC_HAS_CONTENTS | SEC_READONLY
777 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
778 if ((sec->flags & SEC_ALLOC) != 0)
779 flags |= SEC_ALLOC | SEC_LOAD;
780 if (sreloc == NULL
781 || ! bfd_set_section_flags (dynobj, sreloc, flags)
cc78d0af 782 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
70256ad8
AJ
783 return false;
784 }
c434dee6 785 elf_section_data (sec)->sreloc = sreloc;
70256ad8
AJ
786 }
787
c434dee6
AJ
788 /* If this is a global symbol, we count the number of
789 relocations we need for this symbol. */
790 if (h != NULL)
70256ad8 791 {
c434dee6
AJ
792 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
793 }
794 else
795 {
796 /* Track dynamic relocs needed for local syms too.
797 We really need local syms available to do this
798 easily. Oh well. */
799
800 asection *s;
801 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
802 sec, r_symndx);
803 if (s == NULL)
804 return false;
70256ad8 805
c434dee6
AJ
806 head = ((struct elf64_x86_64_dyn_relocs **)
807 &elf_section_data (s)->local_dynrel);
808 }
70256ad8 809
c434dee6
AJ
810 p = *head;
811 if (p == NULL || p->sec != sec)
812 {
813 bfd_size_type amt = sizeof *p;
814 p = ((struct elf64_x86_64_dyn_relocs *)
815 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 816 if (p == NULL)
c434dee6
AJ
817 return false;
818 p->next = *head;
819 *head = p;
820 p->sec = sec;
821 p->count = 0;
822 p->pc_count = 0;
70256ad8 823 }
c434dee6
AJ
824
825 p->count += 1;
826 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8
827 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16
828 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
829 p->pc_count += 1;
70256ad8
AJ
830 }
831 break;
fe4770f4
AJ
832
833 /* This relocation describes the C++ object vtable hierarchy.
834 Reconstruct it for later use during GC. */
835 case R_X86_64_GNU_VTINHERIT:
836 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
837 return false;
838 break;
839
840 /* This relocation describes which C++ vtable entries are actually
841 used. Record for later use during GC. */
842 case R_X86_64_GNU_VTENTRY:
cc78d0af 843 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
fe4770f4
AJ
844 return false;
845 break;
c434dee6
AJ
846
847 default:
848 break;
70256ad8
AJ
849 }
850 }
851
852 return true;
853}
854
855/* Return the section that should be marked against GC for a given
407443a3 856 relocation. */
70256ad8
AJ
857
858static asection *
1e2f5b6e
AM
859elf64_x86_64_gc_mark_hook (sec, info, rel, h, sym)
860 asection *sec;
70256ad8 861 struct bfd_link_info *info ATTRIBUTE_UNUSED;
c434dee6 862 Elf_Internal_Rela *rel;
70256ad8
AJ
863 struct elf_link_hash_entry *h;
864 Elf_Internal_Sym *sym;
865{
866 if (h != NULL)
867 {
fe4770f4 868 switch (ELF64_R_TYPE (rel->r_info))
70256ad8 869 {
fe4770f4
AJ
870 case R_X86_64_GNU_VTINHERIT:
871 case R_X86_64_GNU_VTENTRY:
872 break;
70256ad8
AJ
873
874 default:
fe4770f4
AJ
875 switch (h->root.type)
876 {
877 case bfd_link_hash_defined:
878 case bfd_link_hash_defweak:
879 return h->root.u.def.section;
880
881 case bfd_link_hash_common:
882 return h->root.u.c.p->section;
883
884 default:
885 break;
886 }
70256ad8
AJ
887 }
888 }
889 else
1e2f5b6e 890 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
70256ad8
AJ
891
892 return NULL;
893}
894
407443a3 895/* Update the got entry reference counts for the section being removed. */
70256ad8
AJ
896
897static boolean
898elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
899 bfd *abfd;
c434dee6 900 struct bfd_link_info *info;
70256ad8
AJ
901 asection *sec;
902 const Elf_Internal_Rela *relocs;
903{
904 Elf_Internal_Shdr *symtab_hdr;
905 struct elf_link_hash_entry **sym_hashes;
906 bfd_signed_vma *local_got_refcounts;
907 const Elf_Internal_Rela *rel, *relend;
908 unsigned long r_symndx;
909 struct elf_link_hash_entry *h;
c434dee6
AJ
910
911 elf_section_data (sec)->local_dynrel = NULL;
70256ad8
AJ
912
913 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
914 sym_hashes = elf_sym_hashes (abfd);
915 local_got_refcounts = elf_local_got_refcounts (abfd);
916
70256ad8
AJ
917 relend = relocs + sec->reloc_count;
918 for (rel = relocs; rel < relend; rel++)
919 switch (ELF64_R_TYPE (rel->r_info))
920 {
921 case R_X86_64_GOT32:
922 case R_X86_64_GOTPCREL:
923 r_symndx = ELF64_R_SYM (rel->r_info);
924 if (r_symndx >= symtab_hdr->sh_info)
925 {
926 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
927 if (h->got.refcount > 0)
c434dee6 928 h->got.refcount -= 1;
70256ad8
AJ
929 }
930 else if (local_got_refcounts != NULL)
931 {
932 if (local_got_refcounts[r_symndx] > 0)
c434dee6 933 local_got_refcounts[r_symndx] -= 1;
70256ad8
AJ
934 }
935 break;
936
c434dee6
AJ
937 case R_X86_64_8:
938 case R_X86_64_16:
939 case R_X86_64_32:
940 case R_X86_64_64:
941 case R_X86_64_32S:
942 case R_X86_64_PC8:
943 case R_X86_64_PC16:
944 case R_X86_64_PC32:
945 r_symndx = ELF64_R_SYM (rel->r_info);
946 if (r_symndx >= symtab_hdr->sh_info)
947 {
948 struct elf64_x86_64_link_hash_entry *eh;
949 struct elf64_x86_64_dyn_relocs **pp;
950 struct elf64_x86_64_dyn_relocs *p;
951
952 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
953
954 if (!info->shared && h->plt.refcount > 0)
955 h->plt.refcount -= 1;
956
957 eh = (struct elf64_x86_64_link_hash_entry *) h;
958
959 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
960 if (p->sec == sec)
961 {
962 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8
963 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16
964 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
965 p->pc_count -= 1;
966 p->count -= 1;
967 if (p->count == 0)
968 *pp = p->next;
969 break;
970 }
971 }
972 break;
973
974
70256ad8
AJ
975 case R_X86_64_PLT32:
976 r_symndx = ELF64_R_SYM (rel->r_info);
977 if (r_symndx >= symtab_hdr->sh_info)
978 {
979 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
980 if (h->plt.refcount > 0)
981 h->plt.refcount -= 1;
982 }
983 break;
984
985 default:
986 break;
987 }
988
989 return true;
990}
991
992/* Adjust a symbol defined by a dynamic object and referenced by a
993 regular object. The current definition is in some section of the
994 dynamic object, but we're not including those sections. We have to
995 change the definition to something the rest of the link can
407443a3 996 understand. */
70256ad8
AJ
997
998static boolean
999elf64_x86_64_adjust_dynamic_symbol (info, h)
1000 struct bfd_link_info *info;
1001 struct elf_link_hash_entry *h;
1002{
c434dee6
AJ
1003 struct elf64_x86_64_link_hash_table *htab;
1004 struct elf64_x86_64_link_hash_entry * eh;
1005 struct elf64_x86_64_dyn_relocs *p;
70256ad8
AJ
1006 asection *s;
1007 unsigned int power_of_two;
1008
70256ad8
AJ
1009 /* If this is a function, put it in the procedure linkage table. We
1010 will fill in the contents of the procedure linkage table later,
1011 when we know the address of the .got section. */
1012 if (h->type == STT_FUNC
1013 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1014 {
c434dee6
AJ
1015 if (h->plt.refcount <= 0
1016 || (! info->shared
1017 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1018 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1019 && h->root.type != bfd_link_hash_undefweak
1020 && h->root.type != bfd_link_hash_undefined))
70256ad8 1021 {
70256ad8
AJ
1022 /* This case can occur if we saw a PLT32 reloc in an input
1023 file, but the symbol was never referred to by a dynamic
1024 object, or if all references were garbage collected. In
1025 such a case, we don't actually need to build a procedure
1026 linkage table, and we can just do a PC32 reloc instead. */
70256ad8
AJ
1027 h->plt.offset = (bfd_vma) -1;
1028 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
70256ad8
AJ
1029 }
1030
70256ad8
AJ
1031 return true;
1032 }
bbd7ec4a 1033 else
c434dee6
AJ
1034 /* It's possible that we incorrectly decided a .plt reloc was
1035 needed for an R_X86_64_PC32 reloc to a non-function sym in
1036 check_relocs. We can't decide accurately between function and
1037 non-function syms in check-relocs; Objects loaded later in
1038 the link may change h->type. So fix it now. */
bbd7ec4a 1039 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
1040
1041 /* If this is a weak symbol, and there is a real definition, the
1042 processor independent code will have arranged for us to see the
407443a3 1043 real definition first, and we can just use the same value. */
70256ad8
AJ
1044 if (h->weakdef != NULL)
1045 {
1046 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1047 || h->weakdef->root.type == bfd_link_hash_defweak);
1048 h->root.u.def.section = h->weakdef->root.u.def.section;
1049 h->root.u.def.value = h->weakdef->root.u.def.value;
1050 return true;
1051 }
1052
1053 /* This is a reference to a symbol defined by a dynamic object which
407443a3 1054 is not a function. */
70256ad8
AJ
1055
1056 /* If we are creating a shared library, we must presume that the
1057 only references to the symbol are via the global offset table.
1058 For such cases we need not do anything here; the relocations will
407443a3 1059 be handled correctly by relocate_section. */
70256ad8
AJ
1060 if (info->shared)
1061 return true;
1062
1063 /* If there are no references to this symbol that do not use the
1064 GOT, we don't need to generate a copy reloc. */
1065 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1066 return true;
1067
c434dee6
AJ
1068 /* If -z nocopyreloc was given, we won't generate them either. */
1069 if (info->nocopyreloc)
1070 {
1071 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1072 return true;
1073 }
1074
1075 eh = (struct elf64_x86_64_link_hash_entry *) h;
1076 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1077 {
1078 s = p->sec->output_section;
1079 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1080 break;
1081 }
1082
1083 /* If we didn't find any dynamic relocs in read-only sections, then
1084 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1085 if (p == NULL)
1086 {
1087 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1088 return true;
1089 }
1090
70256ad8 1091 /* We must allocate the symbol in our .dynbss section, which will
407443a3 1092 become part of the .bss section of the executable. There will be
70256ad8
AJ
1093 an entry for this symbol in the .dynsym section. The dynamic
1094 object will contain position independent code, so all references
1095 from the dynamic object to this symbol will go through the global
1096 offset table. The dynamic linker will use the .dynsym entry to
1097 determine the address it must put in the global offset table, so
1098 both the dynamic object and the regular object will refer to the
1099 same memory location for the variable. */
1100
c434dee6 1101 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
1102
1103 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1104 to copy the initial value out of the dynamic object and into the
cedb70c5 1105 runtime process image. */
70256ad8
AJ
1106 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1107 {
c434dee6 1108 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
70256ad8
AJ
1109 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1110 }
1111
1112 /* We need to figure out the alignment required for this symbol. I
407443a3 1113 have no idea how ELF linkers handle this. 16-bytes is the size
70256ad8
AJ
1114 of the largest type that requires hard alignment -- long double. */
1115 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1116 this construct. */
1117 power_of_two = bfd_log2 (h->size);
1118 if (power_of_two > 4)
1119 power_of_two = 4;
1120
1121 /* Apply the required alignment. */
c434dee6 1122 s = htab->sdynbss;
70256ad8 1123 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
c434dee6 1124 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
70256ad8 1125 {
c434dee6 1126 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
70256ad8
AJ
1127 return false;
1128 }
1129
1130 /* Define the symbol as being at this point in the section. */
1131 h->root.u.def.section = s;
1132 h->root.u.def.value = s->_raw_size;
1133
1134 /* Increment the section size to make room for the symbol. */
1135 s->_raw_size += h->size;
1136
1137 return true;
1138}
1139
c434dee6
AJ
1140/* This is the condition under which elf64_x86_64_finish_dynamic_symbol
1141 will be called from elflink.h. If elflink.h doesn't call our
1142 finish_dynamic_symbol routine, we'll need to do something about
1143 initializing any .plt and .got entries in elf64_x86_64_relocate_section. */
1144#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1145 ((DYN) \
1146 && ((INFO)->shared \
1147 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1148 && ((H)->dynindx != -1 \
1149 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1150
1151/* Allocate space in .plt, .got and associated reloc sections for
1152 dynamic relocs. */
1153
1154static boolean
1155allocate_dynrelocs (h, inf)
1156 struct elf_link_hash_entry *h;
1157 PTR inf;
1158{
1159 struct bfd_link_info *info;
1160 struct elf64_x86_64_link_hash_table *htab;
1161 struct elf64_x86_64_link_hash_entry *eh;
1162 struct elf64_x86_64_dyn_relocs *p;
1163
e92d460e 1164 if (h->root.type == bfd_link_hash_indirect)
c434dee6
AJ
1165 return true;
1166
e92d460e
AM
1167 if (h->root.type == bfd_link_hash_warning)
1168 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1169
c434dee6
AJ
1170 info = (struct bfd_link_info *) inf;
1171 htab = elf64_x86_64_hash_table (info);
1172
1173 if (htab->elf.dynamic_sections_created
1174 && h->plt.refcount > 0)
1175 {
1176 /* Make sure this symbol is output as a dynamic symbol.
1177 Undefined weak syms won't yet be marked as dynamic. */
1178 if (h->dynindx == -1
1179 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1180 {
1181 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1182 return false;
1183 }
1184
1185 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1186 {
1187 asection *s = htab->splt;
1188
1189 /* If this is the first .plt entry, make room for the special
1190 first entry. */
1191 if (s->_raw_size == 0)
1192 s->_raw_size += PLT_ENTRY_SIZE;
1193
1194 h->plt.offset = s->_raw_size;
1195
1196 /* If this symbol is not defined in a regular file, and we are
1197 not generating a shared library, then set the symbol to this
1198 location in the .plt. This is required to make function
1199 pointers compare as equal between the normal executable and
1200 the shared library. */
1201 if (! info->shared
1202 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1203 {
1204 h->root.u.def.section = s;
1205 h->root.u.def.value = h->plt.offset;
1206 }
1207
1208 /* Make room for this entry. */
1209 s->_raw_size += PLT_ENTRY_SIZE;
1210
1211 /* We also need to make an entry in the .got.plt section, which
1212 will be placed in the .got section by the linker script. */
1213 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1214
1215 /* We also need to make an entry in the .rela.plt section. */
1216 htab->srelplt->_raw_size += sizeof (Elf64_External_Rela);
1217 }
1218 else
1219 {
1220 h->plt.offset = (bfd_vma) -1;
1221 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1222 }
1223 }
1224 else
1225 {
1226 h->plt.offset = (bfd_vma) -1;
1227 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1228 }
1229
1230 if (h->got.refcount > 0)
1231 {
1232 asection *s;
1233 boolean dyn;
1234
1235 /* Make sure this symbol is output as a dynamic symbol.
1236 Undefined weak syms won't yet be marked as dynamic. */
1237 if (h->dynindx == -1
1238 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1239 {
1240 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1241 return false;
1242 }
1243
1244 s = htab->sgot;
1245 h->got.offset = s->_raw_size;
1246 s->_raw_size += GOT_ENTRY_SIZE;
1247 dyn = htab->elf.dynamic_sections_created;
1248 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1249 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1250 }
1251 else
1252 h->got.offset = (bfd_vma) -1;
1253
1254 eh = (struct elf64_x86_64_link_hash_entry *) h;
1255 if (eh->dyn_relocs == NULL)
1256 return true;
1257
1258 /* In the shared -Bsymbolic case, discard space allocated for
1259 dynamic pc-relative relocs against symbols which turn out to be
1260 defined in regular objects. For the normal shared case, discard
1261 space for pc-relative relocs that have become local due to symbol
1262 visibility changes. */
1263
1264 if (info->shared)
1265 {
1266 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1267 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1268 || info->symbolic))
1269 {
1270 struct elf64_x86_64_dyn_relocs **pp;
1271
1272 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1273 {
1274 p->count -= p->pc_count;
1275 p->pc_count = 0;
1276 if (p->count == 0)
1277 *pp = p->next;
1278 else
1279 pp = &p->next;
1280 }
1281 }
1282 }
1283 else
1284 {
1285 /* For the non-shared case, discard space for relocs against
1286 symbols which turn out to need copy relocs or are not
1287 dynamic. */
1288
1289 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1290 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1291 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1292 || (htab->elf.dynamic_sections_created
1293 && (h->root.type == bfd_link_hash_undefweak
1294 || h->root.type == bfd_link_hash_undefined))))
1295 {
1296 /* Make sure this symbol is output as a dynamic symbol.
1297 Undefined weak syms won't yet be marked as dynamic. */
1298 if (h->dynindx == -1
1299 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1300 {
1301 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1302 return false;
1303 }
1304
1305 /* If that succeeded, we know we'll be keeping all the
1306 relocs. */
1307 if (h->dynindx != -1)
1308 goto keep;
1309 }
1310
1311 eh->dyn_relocs = NULL;
1312
1313 keep: ;
1314 }
1315
1316 /* Finally, allocate space. */
1317 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1318 {
1319 asection *sreloc = elf_section_data (p->sec)->sreloc;
1320 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
1321 }
1322
1323 return true;
1324}
1325
1326/* Find any dynamic relocs that apply to read-only sections. */
1327
1328static boolean
1329readonly_dynrelocs (h, inf)
1330 struct elf_link_hash_entry *h;
1331 PTR inf;
1332{
1333 struct elf64_x86_64_link_hash_entry *eh;
1334 struct elf64_x86_64_dyn_relocs *p;
1335
e92d460e
AM
1336 if (h->root.type == bfd_link_hash_warning)
1337 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1338
c434dee6
AJ
1339 eh = (struct elf64_x86_64_link_hash_entry *) h;
1340 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1341 {
1342 asection *s = p->sec->output_section;
1343
1344 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1345 {
1346 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1347
1348 info->flags |= DF_TEXTREL;
1349
1350 /* Not an error, just cut short the traversal. */
1351 return false;
1352 }
1353 }
1354 return true;
1355}
1356
70256ad8
AJ
1357/* Set the sizes of the dynamic sections. */
1358
1359static boolean
1360elf64_x86_64_size_dynamic_sections (output_bfd, info)
8df9fc9d 1361 bfd *output_bfd ATTRIBUTE_UNUSED;
70256ad8
AJ
1362 struct bfd_link_info *info;
1363{
c434dee6 1364 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1365 bfd *dynobj;
1366 asection *s;
70256ad8 1367 boolean relocs;
c434dee6 1368 bfd *ibfd;
70256ad8 1369
c434dee6
AJ
1370 htab = elf64_x86_64_hash_table (info);
1371 dynobj = htab->elf.dynobj;
1372 if (dynobj == NULL)
1373 abort ();
70256ad8 1374
c434dee6 1375 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1376 {
1377 /* Set the contents of the .interp section to the interpreter. */
1378 if (! info->shared)
1379 {
1380 s = bfd_get_section_by_name (dynobj, ".interp");
c434dee6
AJ
1381 if (s == NULL)
1382 abort ();
70256ad8
AJ
1383 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1384 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1385 }
1386 }
70256ad8 1387
c434dee6
AJ
1388 /* Set up .got offsets for local syms, and space for local dynamic
1389 relocs. */
1390 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 1391 {
c434dee6
AJ
1392 bfd_signed_vma *local_got;
1393 bfd_signed_vma *end_local_got;
1394 bfd_size_type locsymcount;
1395 Elf_Internal_Shdr *symtab_hdr;
1396 asection *srel;
70256ad8 1397
c434dee6 1398 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
70256ad8
AJ
1399 continue;
1400
c434dee6 1401 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 1402 {
c434dee6
AJ
1403 struct elf64_x86_64_dyn_relocs *p;
1404
1405 for (p = *((struct elf64_x86_64_dyn_relocs **)
1406 &elf_section_data (s)->local_dynrel);
1407 p != NULL;
1408 p = p->next)
70256ad8 1409 {
c434dee6
AJ
1410 if (!bfd_is_abs_section (p->sec)
1411 && bfd_is_abs_section (p->sec->output_section))
1412 {
1413 /* Input section has been discarded, either because
1414 it is a copy of a linkonce section or due to
1415 linker script /DISCARD/, so we'll be discarding
1416 the relocs too. */
1417 }
1418 else if (p->count != 0)
1419 {
1420 srel = elf_section_data (p->sec)->sreloc;
1421 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
1422 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1423 info->flags |= DF_TEXTREL;
1424
1425 }
70256ad8
AJ
1426 }
1427 }
c434dee6
AJ
1428
1429 local_got = elf_local_got_refcounts (ibfd);
1430 if (!local_got)
1431 continue;
1432
1433 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1434 locsymcount = symtab_hdr->sh_info;
1435 end_local_got = local_got + locsymcount;
1436 s = htab->sgot;
1437 srel = htab->srelgot;
1438 for (; local_got < end_local_got; ++local_got)
70256ad8 1439 {
c434dee6 1440 if (*local_got > 0)
70256ad8 1441 {
c434dee6
AJ
1442 *local_got = s->_raw_size;
1443 s->_raw_size += GOT_ENTRY_SIZE;
1444 if (info->shared)
1445 srel->_raw_size += sizeof (Elf64_External_Rela);
70256ad8
AJ
1446 }
1447 else
c434dee6
AJ
1448 *local_got = (bfd_vma) -1;
1449 }
1450 }
70256ad8 1451
c434dee6
AJ
1452 /* Allocate global sym .plt and .got entries, and space for global
1453 sym dynamic relocs. */
1454 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1455
1456 /* We now have determined the sizes of the various dynamic sections.
1457 Allocate memory for them. */
1458 relocs = false;
1459 for (s = dynobj->sections; s != NULL; s = s->next)
1460 {
1461 if ((s->flags & SEC_LINKER_CREATED) == 0)
1462 continue;
1463
1464 if (s == htab->splt
1465 || s == htab->sgot
1466 || s == htab->sgotplt)
1467 {
1468 /* Strip this section if we don't need it; see the
1469 comment below. */
1470 }
1471 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1472 {
1473 if (s->_raw_size != 0 && s != htab->srelplt)
1474 relocs = true;
1475
1476 /* We use the reloc_count field as a counter if we need
1477 to copy relocs into the output file. */
1478 s->reloc_count = 0;
70256ad8 1479 }
c434dee6 1480 else
70256ad8
AJ
1481 {
1482 /* It's not one of our sections, so don't allocate space. */
1483 continue;
1484 }
1485
c434dee6 1486 if (s->_raw_size == 0)
70256ad8 1487 {
c434dee6
AJ
1488 /* If we don't need this section, strip it from the
1489 output file. This is mostly to handle .rela.bss and
1490 .rela.plt. We must create both sections in
1491 create_dynamic_sections, because they must be created
1492 before the linker maps input sections to output
1493 sections. The linker does that before
1494 adjust_dynamic_symbol is called, and it is that
1495 function which decides whether anything needs to go
1496 into these sections. */
1497
70256ad8
AJ
1498 _bfd_strip_section_from_output (info, s);
1499 continue;
1500 }
1501
1502 /* Allocate memory for the section contents. We use bfd_zalloc
1503 here in case unused entries are not reclaimed before the
1504 section's contents are written out. This should not happen,
1505 but this way if it does, we get a R_X86_64_NONE reloc instead
1506 of garbage. */
1507 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
c434dee6 1508 if (s->contents == NULL)
70256ad8
AJ
1509 return false;
1510 }
1511
c434dee6 1512 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1513 {
1514 /* Add some entries to the .dynamic section. We fill in the
1515 values later, in elf64_x86_64_finish_dynamic_sections, but we
1516 must add the entries now so that we get the correct size for
407443a3 1517 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 1518 dynamic linker and used by the debugger. */
dc810e39
AM
1519#define add_dynamic_entry(TAG, VAL) \
1520 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1521
70256ad8
AJ
1522 if (! info->shared)
1523 {
dc810e39 1524 if (!add_dynamic_entry (DT_DEBUG, 0))
70256ad8
AJ
1525 return false;
1526 }
1527
c434dee6 1528 if (htab->splt->_raw_size != 0)
70256ad8 1529 {
dc810e39
AM
1530 if (!add_dynamic_entry (DT_PLTGOT, 0)
1531 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1532 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1533 || !add_dynamic_entry (DT_JMPREL, 0))
70256ad8
AJ
1534 return false;
1535 }
1536
1537 if (relocs)
1538 {
dc810e39
AM
1539 if (!add_dynamic_entry (DT_RELA, 0)
1540 || !add_dynamic_entry (DT_RELASZ, 0)
1541 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
70256ad8 1542 return false;
70256ad8 1543
c434dee6
AJ
1544 /* If any dynamic relocs apply to a read-only section,
1545 then we need a DT_TEXTREL entry. */
1546 if ((info->flags & DF_TEXTREL) == 0)
1547 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1548 (PTR) info);
1549
1550 if ((info->flags & DF_TEXTREL) != 0)
1551 {
1552 if (!add_dynamic_entry (DT_TEXTREL, 0))
1553 return false;
1554 }
70256ad8
AJ
1555 }
1556 }
dc810e39 1557#undef add_dynamic_entry
70256ad8
AJ
1558
1559 return true;
1560}
1561
8d88c4ca
NC
1562/* Relocate an x86_64 ELF section. */
1563
1564static boolean
1565elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
fe4770f4 1566 contents, relocs, local_syms, local_sections)
8d88c4ca
NC
1567 bfd *output_bfd;
1568 struct bfd_link_info *info;
1569 bfd *input_bfd;
1570 asection *input_section;
1571 bfd_byte *contents;
1572 Elf_Internal_Rela *relocs;
1573 Elf_Internal_Sym *local_syms;
1574 asection **local_sections;
1575{
c434dee6 1576 struct elf64_x86_64_link_hash_table *htab;
8d88c4ca
NC
1577 Elf_Internal_Shdr *symtab_hdr;
1578 struct elf_link_hash_entry **sym_hashes;
1579 bfd_vma *local_got_offsets;
c434dee6 1580 Elf_Internal_Rela *rel;
8d88c4ca
NC
1581 Elf_Internal_Rela *relend;
1582
b491616a
AM
1583 if (info->relocateable)
1584 return true;
1585
c434dee6 1586 htab = elf64_x86_64_hash_table (info);
8d88c4ca
NC
1587 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1588 sym_hashes = elf_sym_hashes (input_bfd);
1589 local_got_offsets = elf_local_got_offsets (input_bfd);
1590
c434dee6 1591 rel = relocs;
8d88c4ca 1592 relend = relocs + input_section->reloc_count;
c434dee6 1593 for (; rel < relend; rel++)
8d88c4ca
NC
1594 {
1595 int r_type;
1596 reloc_howto_type *howto;
1597 unsigned long r_symndx;
1598 struct elf_link_hash_entry *h;
1599 Elf_Internal_Sym *sym;
1600 asection *sec;
c434dee6 1601 bfd_vma off;
8d88c4ca 1602 bfd_vma relocation;
c434dee6 1603 boolean unresolved_reloc;
8d88c4ca 1604 bfd_reloc_status_type r;
8d88c4ca 1605
c434dee6 1606 r_type = ELF64_R_TYPE (rel->r_info);
fe4770f4
AJ
1607 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1608 || r_type == (int) R_X86_64_GNU_VTENTRY)
1609 continue;
8d88c4ca 1610
c434dee6 1611 if (r_type < 0 || r_type >= R_X86_64_max)
8da6118f
KH
1612 {
1613 bfd_set_error (bfd_error_bad_value);
1614 return false;
1615 }
8d88c4ca 1616
b491616a 1617 howto = x86_64_elf_howto_table + r_type;
c434dee6 1618 r_symndx = ELF64_R_SYM (rel->r_info);
8d88c4ca
NC
1619 h = NULL;
1620 sym = NULL;
1621 sec = NULL;
c434dee6 1622 unresolved_reloc = false;
8d88c4ca 1623 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
1624 {
1625 sym = local_syms + r_symndx;
1626 sec = local_sections[r_symndx];
c434dee6
AJ
1627
1628 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
8da6118f 1629 }
8d88c4ca 1630 else
8da6118f
KH
1631 {
1632 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1633 while (h->root.type == bfd_link_hash_indirect
1634 || h->root.type == bfd_link_hash_warning)
1635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c434dee6 1636
8da6118f
KH
1637 if (h->root.type == bfd_link_hash_defined
1638 || h->root.type == bfd_link_hash_defweak)
1639 {
1640 sec = h->root.u.def.section;
c434dee6 1641 if (sec->output_section == NULL)
8da6118f 1642 {
c434dee6
AJ
1643 /* Set a flag that will be cleared later if we find a
1644 relocation value for this symbol. output_section
1645 is typically NULL for symbols satisfied by a shared
1646 library. */
1647 unresolved_reloc = true;
8da6118f
KH
1648 relocation = 0;
1649 }
1650 else
1651 relocation = (h->root.u.def.value
1652 + sec->output_section->vma
1653 + sec->output_offset);
1654 }
1655 else if (h->root.type == bfd_link_hash_undefweak)
1656 relocation = 0;
671bae9c
NC
1657 else if (info->shared
1658 && (!info->symbolic || info->allow_shlib_undefined)
1659 && !info->no_undefined
70256ad8
AJ
1660 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1661 relocation = 0;
8da6118f
KH
1662 else
1663 {
1664 if (! ((*info->callbacks->undefined_symbol)
1665 (info, h->root.root.string, input_bfd,
c434dee6 1666 input_section, rel->r_offset,
8da6118f
KH
1667 (!info->shared || info->no_undefined
1668 || ELF_ST_VISIBILITY (h->other)))))
1669 return false;
1670 relocation = 0;
1671 }
1672 }
70256ad8
AJ
1673 /* When generating a shared object, the relocations handled here are
1674 copied into the output file to be resolved at run time. */
1675 switch (r_type)
1676 {
1677 case R_X86_64_GOT32:
1678 /* Relocation is to the entry for this symbol in the global
1679 offset table. */
70256ad8
AJ
1680 case R_X86_64_GOTPCREL:
1681 /* Use global offset table as symbol value. */
c434dee6
AJ
1682 if (htab->sgot == NULL)
1683 abort ();
053579d7 1684
51e0a107 1685 if (h != NULL)
70256ad8 1686 {
c434dee6
AJ
1687 boolean dyn;
1688
1689 off = h->got.offset;
1690 dyn = htab->elf.dynamic_sections_created;
51e0a107 1691
c434dee6 1692 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
51e0a107 1693 || (info->shared
c434dee6
AJ
1694 && (info->symbolic
1695 || h->dynindx == -1
1696 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
51e0a107
JH
1697 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1698 {
1699 /* This is actually a static link, or it is a -Bsymbolic
1700 link and the symbol is defined locally, or the symbol
407443a3 1701 was forced to be local because of a version file. We
51e0a107
JH
1702 must initialize this entry in the global offset table.
1703 Since the offset must always be a multiple of 8, we
1704 use the least significant bit to record whether we
1705 have initialized it already.
1706
1707 When doing a dynamic link, we create a .rela.got
407443a3
AJ
1708 relocation entry to initialize the value. This is
1709 done in the finish_dynamic_symbol routine. */
51e0a107
JH
1710 if ((off & 1) != 0)
1711 off &= ~1;
1712 else
1713 {
1714 bfd_put_64 (output_bfd, relocation,
c434dee6 1715 htab->sgot->contents + off);
51e0a107
JH
1716 h->got.offset |= 1;
1717 }
1718 }
053579d7 1719 else
c434dee6 1720 unresolved_reloc = false;
70256ad8 1721 }
51e0a107
JH
1722 else
1723 {
c434dee6
AJ
1724 if (local_got_offsets == NULL)
1725 abort ();
51e0a107
JH
1726
1727 off = local_got_offsets[r_symndx];
1728
1729 /* The offset must always be a multiple of 8. We use
407443a3
AJ
1730 the least significant bit to record whether we have
1731 already generated the necessary reloc. */
51e0a107
JH
1732 if ((off & 1) != 0)
1733 off &= ~1;
1734 else
1735 {
c434dee6
AJ
1736 bfd_put_64 (output_bfd, relocation,
1737 htab->sgot->contents + off);
51e0a107
JH
1738
1739 if (info->shared)
1740 {
1741 asection *srelgot;
1742 Elf_Internal_Rela outrel;
c434dee6 1743 Elf64_External_Rela *loc;
70256ad8 1744
51e0a107
JH
1745 /* We need to generate a R_X86_64_RELATIVE reloc
1746 for the dynamic linker. */
c434dee6
AJ
1747 srelgot = htab->srelgot;
1748 if (srelgot == NULL)
1749 abort ();
51e0a107 1750
c434dee6
AJ
1751 outrel.r_offset = (htab->sgot->output_section->vma
1752 + htab->sgot->output_offset
51e0a107
JH
1753 + off);
1754 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1755 outrel.r_addend = relocation;
c434dee6
AJ
1756 loc = (Elf64_External_Rela *) srelgot->contents;
1757 loc += srelgot->reloc_count++;
1758 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
51e0a107
JH
1759 }
1760
1761 local_got_offsets[r_symndx] |= 1;
1762 }
51e0a107 1763 }
6a2bda3f 1764
c434dee6
AJ
1765 if (off >= (bfd_vma) -2)
1766 abort ();
1767
1768 relocation = htab->sgot->output_offset + off;
1769 if (r_type == R_X86_64_GOTPCREL)
1770 relocation += htab->sgot->output_section->vma;
1771
70256ad8
AJ
1772 break;
1773
1774 case R_X86_64_PLT32:
1775 /* Relocation is to the entry for this symbol in the
1776 procedure linkage table. */
1777
1778 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 1779 without using the procedure linkage table. */
70256ad8
AJ
1780 if (h == NULL)
1781 break;
1782
c434dee6
AJ
1783 if (h->plt.offset == (bfd_vma) -1
1784 || htab->splt == NULL)
70256ad8
AJ
1785 {
1786 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
1787 happens when statically linking PIC code, or when
1788 using -Bsymbolic. */
70256ad8
AJ
1789 break;
1790 }
1791
c434dee6
AJ
1792 relocation = (htab->splt->output_section->vma
1793 + htab->splt->output_offset
70256ad8 1794 + h->plt.offset);
c434dee6 1795 unresolved_reloc = false;
70256ad8
AJ
1796 break;
1797
fd8ab9e5
AJ
1798 case R_X86_64_PC8:
1799 case R_X86_64_PC16:
1800 case R_X86_64_PC32:
70256ad8
AJ
1801 case R_X86_64_8:
1802 case R_X86_64_16:
1803 case R_X86_64_32:
6b3db546 1804 case R_X86_64_64:
80643fbc 1805 /* FIXME: The ABI says the linker should make sure the value is
407443a3 1806 the same when it's zeroextended to 64 bit. */
c434dee6
AJ
1807
1808 /* r_symndx will be zero only for relocs against symbols
1809 from removed linkonce sections, or sections discarded by
1810 a linker script. */
1811 if (r_symndx == 0
1812 || (input_section->flags & SEC_ALLOC) == 0)
1813 break;
1814
1815 if ((info->shared
1816 && ((r_type != R_X86_64_PC8
1817 && r_type != R_X86_64_PC16
1818 && r_type != R_X86_64_PC32)
1819 || (h != NULL
1820 && h->dynindx != -1
1821 && (! info->symbolic
1822 || (h->elf_link_hash_flags
1823 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1824 || (!info->shared
1825 && h != NULL
1826 && h->dynindx != -1
1827 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1828 && (((h->elf_link_hash_flags
1829 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1830 && (h->elf_link_hash_flags
1831 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1832 || h->root.type == bfd_link_hash_undefweak
1833 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
1834 {
1835 Elf_Internal_Rela outrel;
1836 boolean skip, relocate;
c434dee6
AJ
1837 asection *sreloc;
1838 Elf64_External_Rela *loc;
70256ad8
AJ
1839
1840 /* When generating a shared object, these relocations
1841 are copied into the output file to be resolved at run
407443a3 1842 time. */
70256ad8 1843
70256ad8 1844 skip = false;
0bb2d96a 1845 relocate = false;
70256ad8 1846
c629eae0
JJ
1847 outrel.r_offset =
1848 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 1849 rel->r_offset);
c629eae0
JJ
1850 if (outrel.r_offset == (bfd_vma) -1)
1851 skip = true;
0fb19cbc 1852 else if (outrel.r_offset == (bfd_vma) -2)
0bb2d96a 1853 skip = true, relocate = true;
70256ad8
AJ
1854
1855 outrel.r_offset += (input_section->output_section->vma
1856 + input_section->output_offset);
1857
1858 if (skip)
0bb2d96a 1859 memset (&outrel, 0, sizeof outrel);
c434dee6 1860
fd8ab9e5
AJ
1861 /* h->dynindx may be -1 if this symbol was marked to
1862 become local. */
1863 else if (h != NULL
c434dee6
AJ
1864 && h->dynindx != -1
1865 && (r_type == R_X86_64_PC8
1866 || r_type == R_X86_64_PC16
1867 || r_type == R_X86_64_PC32
1868 || !info->shared
1869 || !info->symbolic
fd8ab9e5
AJ
1870 || (h->elf_link_hash_flags
1871 & ELF_LINK_HASH_DEF_REGULAR) == 0))
70256ad8 1872 {
70256ad8 1873 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
c434dee6 1874 outrel.r_addend = rel->r_addend;
70256ad8
AJ
1875 }
1876 else
1877 {
c434dee6
AJ
1878 /* This symbol is local, or marked to become local. */
1879 relocate = true;
1880 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1881 outrel.r_addend = relocation + rel->r_addend;
70256ad8
AJ
1882 }
1883
c434dee6
AJ
1884 sreloc = elf_section_data (input_section)->sreloc;
1885 if (sreloc == NULL)
1886 abort ();
1887
1888 loc = (Elf64_External_Rela *) sreloc->contents;
1889 loc += sreloc->reloc_count++;
1890 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
70256ad8
AJ
1891
1892 /* If this reloc is against an external symbol, we do
1893 not want to fiddle with the addend. Otherwise, we
1894 need to include the symbol value so that it becomes
1895 an addend for the dynamic reloc. */
1896 if (! relocate)
1897 continue;
1898 }
1899
1900 break;
1901
1902 default:
1903 break;
1904 }
8d88c4ca 1905
c434dee6
AJ
1906 /* FIXME: Why do we allow debugging sections to escape this error?
1907 More importantly, why do we not emit dynamic relocs for
1908 R_386_32 above in debugging sections (which are ! SEC_ALLOC)?
1909 If we had emitted the dynamic reloc, we could remove the
1910 fudge here. */
1911 if (unresolved_reloc
1912 && !(info->shared
1913 && (input_section->flags & SEC_DEBUGGING) != 0
1914 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1915 (*_bfd_error_handler)
1916 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1917 bfd_archive_filename (input_bfd),
1918 bfd_get_section_name (input_bfd, input_section),
1919 (long) rel->r_offset,
1920 h->root.root.string);
1921
8d88c4ca 1922 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
1923 contents, rel->r_offset,
1924 relocation, rel->r_addend);
8d88c4ca
NC
1925
1926 if (r != bfd_reloc_ok)
8da6118f 1927 {
c434dee6
AJ
1928 const char *name;
1929
1930 if (h != NULL)
1931 name = h->root.root.string;
1932 else
8da6118f 1933 {
c434dee6
AJ
1934 name = bfd_elf_string_from_elf_section (input_bfd,
1935 symtab_hdr->sh_link,
1936 sym->st_name);
1937 if (name == NULL)
1938 return false;
1939 if (*name == '\0')
1940 name = bfd_section_name (input_bfd, sec);
1941 }
1942
1943 if (r == bfd_reloc_overflow)
1944 {
1945
1946 if (! ((*info->callbacks->reloc_overflow)
1947 (info, name, howto->name, (bfd_vma) 0,
1948 input_bfd, input_section, rel->r_offset)))
1949 return false;
1950 }
1951 else
1952 {
1953 (*_bfd_error_handler)
1954 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
1955 bfd_archive_filename (input_bfd),
1956 bfd_get_section_name (input_bfd, input_section),
1957 (long) rel->r_offset, name, (int) r);
1958 return false;
8da6118f
KH
1959 }
1960 }
8d88c4ca 1961 }
70256ad8
AJ
1962
1963 return true;
1964}
1965
1966/* Finish up dynamic symbol handling. We set the contents of various
1967 dynamic sections here. */
1968
1969static boolean
1970elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym)
1971 bfd *output_bfd;
1972 struct bfd_link_info *info;
1973 struct elf_link_hash_entry *h;
1974 Elf_Internal_Sym *sym;
1975{
c434dee6 1976 struct elf64_x86_64_link_hash_table *htab;
70256ad8 1977
c434dee6 1978 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
1979
1980 if (h->plt.offset != (bfd_vma) -1)
1981 {
70256ad8
AJ
1982 bfd_vma plt_index;
1983 bfd_vma got_offset;
1984 Elf_Internal_Rela rela;
c434dee6 1985 Elf64_External_Rela *loc;
70256ad8
AJ
1986
1987 /* This symbol has an entry in the procedure linkage table. Set
407443a3 1988 it up. */
70256ad8 1989
c434dee6
AJ
1990 if (h->dynindx == -1
1991 || htab->splt == NULL
1992 || htab->sgotplt == NULL
1993 || htab->srelplt == NULL)
1994 abort ();
70256ad8
AJ
1995
1996 /* Get the index in the procedure linkage table which
1997 corresponds to this symbol. This is the index of this symbol
1998 in all the symbols for which we are making plt entries. The
1999 first entry in the procedure linkage table is reserved. */
2000 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2001
2002 /* Get the offset into the .got table of the entry that
407443a3 2003 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
fe4770f4 2004 bytes. The first three are reserved for the dynamic linker. */
70256ad8
AJ
2005 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2006
2007 /* Fill in the entry in the procedure linkage table. */
c434dee6 2008 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
70256ad8
AJ
2009 PLT_ENTRY_SIZE);
2010
2011 /* Insert the relocation positions of the plt section. The magic
2012 numbers at the end of the statements are the positions of the
2013 relocations in the plt section. */
653165cc
AJ
2014 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2015 instruction uses 6 bytes, subtract this value. */
2016 bfd_put_32 (output_bfd,
c434dee6
AJ
2017 (htab->sgotplt->output_section->vma
2018 + htab->sgotplt->output_offset
653165cc 2019 + got_offset
c434dee6
AJ
2020 - htab->splt->output_section->vma
2021 - htab->splt->output_offset
653165cc
AJ
2022 - h->plt.offset
2023 - 6),
c434dee6 2024 htab->splt->contents + h->plt.offset + 2);
653165cc
AJ
2025 /* Put relocation index. */
2026 bfd_put_32 (output_bfd, plt_index,
c434dee6 2027 htab->splt->contents + h->plt.offset + 7);
653165cc
AJ
2028 /* Put offset for jmp .PLT0. */
2029 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
c434dee6 2030 htab->splt->contents + h->plt.offset + 12);
70256ad8 2031
653165cc
AJ
2032 /* Fill in the entry in the global offset table, initially this
2033 points to the pushq instruction in the PLT which is at offset 6. */
c434dee6
AJ
2034 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2035 + htab->splt->output_offset
70256ad8 2036 + h->plt.offset + 6),
c434dee6 2037 htab->sgotplt->contents + got_offset);
70256ad8
AJ
2038
2039 /* Fill in the entry in the .rela.plt section. */
c434dee6
AJ
2040 rela.r_offset = (htab->sgotplt->output_section->vma
2041 + htab->sgotplt->output_offset
70256ad8
AJ
2042 + got_offset);
2043 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2044 rela.r_addend = 0;
c434dee6
AJ
2045 loc = (Elf64_External_Rela *) htab->srelplt->contents + plt_index;
2046 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8
AJ
2047
2048 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2049 {
2050 /* Mark the symbol as undefined, rather than as defined in
c434dee6
AJ
2051 the .plt section. Leave the value alone. This is a clue
2052 for the dynamic linker, to make function pointer
2053 comparisons work between an application and shared
2054 library. */
70256ad8
AJ
2055 sym->st_shndx = SHN_UNDEF;
2056 }
2057 }
2058
053579d7
AJ
2059 if (h->got.offset != (bfd_vma) -1)
2060 {
053579d7 2061 Elf_Internal_Rela rela;
c434dee6 2062 Elf64_External_Rela *loc;
053579d7
AJ
2063
2064 /* This symbol has an entry in the global offset table. Set it
2065 up. */
2066
c434dee6
AJ
2067 if (htab->sgot == NULL || htab->srelgot == NULL)
2068 abort ();
053579d7 2069
c434dee6
AJ
2070 rela.r_offset = (htab->sgot->output_section->vma
2071 + htab->sgot->output_offset
dc810e39 2072 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
2073
2074 /* If this is a static link, or it is a -Bsymbolic link and the
2075 symbol is defined locally or was forced to be local because
2076 of a version file, we just want to emit a RELATIVE reloc.
2077 The entry in the global offset table will already have been
2078 initialized in the relocate_section function. */
c434dee6
AJ
2079 if (info->shared
2080 && (info->symbolic
2081 || h->dynindx == -1
2082 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2083 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
053579d7 2084 {
cc78d0af 2085 BFD_ASSERT((h->got.offset & 1) != 0);
053579d7
AJ
2086 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2087 rela.r_addend = (h->root.u.def.value
2088 + h->root.u.def.section->output_section->vma
2089 + h->root.u.def.section->output_offset);
2090 }
2091 else
2092 {
2093 BFD_ASSERT((h->got.offset & 1) == 0);
c434dee6
AJ
2094 bfd_put_64 (output_bfd, (bfd_vma) 0,
2095 htab->sgot->contents + h->got.offset);
053579d7
AJ
2096 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2097 rela.r_addend = 0;
2098 }
2099
c434dee6
AJ
2100 loc = (Elf64_External_Rela *) htab->srelgot->contents;
2101 loc += htab->srelgot->reloc_count++;
2102 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
053579d7
AJ
2103 }
2104
70256ad8
AJ
2105 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2106 {
70256ad8 2107 Elf_Internal_Rela rela;
c434dee6 2108 Elf64_External_Rela *loc;
70256ad8
AJ
2109
2110 /* This symbol needs a copy reloc. Set it up. */
2111
c434dee6
AJ
2112 if (h->dynindx == -1
2113 || (h->root.type != bfd_link_hash_defined
2114 && h->root.type != bfd_link_hash_defweak)
2115 || htab->srelbss == NULL)
2116 abort ();
70256ad8
AJ
2117
2118 rela.r_offset = (h->root.u.def.value
2119 + h->root.u.def.section->output_section->vma
2120 + h->root.u.def.section->output_offset);
2121 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2122 rela.r_addend = 0;
c434dee6
AJ
2123 loc = (Elf64_External_Rela *) htab->srelbss->contents;
2124 loc += htab->srelbss->reloc_count++;
2125 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8
AJ
2126 }
2127
2128 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2129 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2130 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2131 sym->st_shndx = SHN_ABS;
2132
2133 return true;
2134}
2135
c434dee6
AJ
2136/* Used to decide how to sort relocs in an optimal manner for the
2137 dynamic linker, before writing them out. */
2138
2139static enum elf_reloc_type_class
2140elf64_x86_64_reloc_type_class (rela)
2141 const Elf_Internal_Rela *rela;
2142{
2143 switch ((int) ELF64_R_TYPE (rela->r_info))
2144 {
2145 case R_X86_64_RELATIVE:
2146 return reloc_class_relative;
2147 case R_X86_64_JUMP_SLOT:
2148 return reloc_class_plt;
2149 case R_X86_64_COPY:
2150 return reloc_class_copy;
2151 default:
2152 return reloc_class_normal;
2153 }
2154}
2155
70256ad8
AJ
2156/* Finish up the dynamic sections. */
2157
2158static boolean
2159elf64_x86_64_finish_dynamic_sections (output_bfd, info)
2160 bfd *output_bfd;
2161 struct bfd_link_info *info;
2162{
c434dee6 2163 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
2164 bfd *dynobj;
2165 asection *sdyn;
70256ad8 2166
c434dee6
AJ
2167 htab = elf64_x86_64_hash_table (info);
2168 dynobj = htab->elf.dynobj;
70256ad8
AJ
2169 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2170
c434dee6 2171 if (htab->elf.dynamic_sections_created)
70256ad8 2172 {
70256ad8
AJ
2173 Elf64_External_Dyn *dyncon, *dynconend;
2174
c434dee6
AJ
2175 if (sdyn == NULL || htab->sgot == NULL)
2176 abort ();
70256ad8
AJ
2177
2178 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2179 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2180 for (; dyncon < dynconend; dyncon++)
2181 {
2182 Elf_Internal_Dyn dyn;
70256ad8
AJ
2183 asection *s;
2184
2185 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2186
2187 switch (dyn.d_tag)
2188 {
2189 default:
053579d7 2190 continue;
70256ad8
AJ
2191
2192 case DT_PLTGOT:
c434dee6
AJ
2193 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2194 break;
70256ad8
AJ
2195
2196 case DT_JMPREL:
c434dee6
AJ
2197 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2198 break;
70256ad8 2199
c434dee6
AJ
2200 case DT_PLTRELSZ:
2201 s = htab->srelplt->output_section;
2202 if (s->_cooked_size != 0)
2203 dyn.d_un.d_val = s->_cooked_size;
2204 else
2205 dyn.d_un.d_val = s->_raw_size;
70256ad8
AJ
2206 break;
2207
2208 case DT_RELASZ:
c434dee6
AJ
2209 /* The procedure linkage table relocs (DT_JMPREL) should
2210 not be included in the overall relocs (DT_RELA).
2211 Therefore, we override the DT_RELASZ entry here to
2212 make it not include the JMPREL relocs. Since the
2213 linker script arranges for .rela.plt to follow all
2214 other relocation sections, we don't have to worry
2215 about changing the DT_RELA entry. */
2216 if (htab->srelplt != NULL)
70256ad8 2217 {
c434dee6
AJ
2218 s = htab->srelplt->output_section;
2219 if (s->_cooked_size != 0)
2220 dyn.d_un.d_val -= s->_cooked_size;
2221 else
2222 dyn.d_un.d_val -= s->_raw_size;
70256ad8
AJ
2223 }
2224 break;
70256ad8 2225 }
c434dee6 2226
70256ad8
AJ
2227 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2228 }
2229
c434dee6
AJ
2230 /* Fill in the special first entry in the procedure linkage table. */
2231 if (htab->splt && htab->splt->_raw_size > 0)
70256ad8 2232 {
653165cc 2233 /* Fill in the first entry in the procedure linkage table. */
c434dee6
AJ
2234 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2235 PLT_ENTRY_SIZE);
653165cc
AJ
2236 /* Add offset for pushq GOT+8(%rip), since the instruction
2237 uses 6 bytes subtract this value. */
2238 bfd_put_32 (output_bfd,
c434dee6
AJ
2239 (htab->sgotplt->output_section->vma
2240 + htab->sgotplt->output_offset
653165cc 2241 + 8
c434dee6
AJ
2242 - htab->splt->output_section->vma
2243 - htab->splt->output_offset
653165cc 2244 - 6),
c434dee6 2245 htab->splt->contents + 2);
653165cc
AJ
2246 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2247 the end of the instruction. */
2248 bfd_put_32 (output_bfd,
c434dee6
AJ
2249 (htab->sgotplt->output_section->vma
2250 + htab->sgotplt->output_offset
653165cc 2251 + 16
c434dee6
AJ
2252 - htab->splt->output_section->vma
2253 - htab->splt->output_offset
653165cc 2254 - 12),
c434dee6 2255 htab->splt->contents + 8);
653165cc 2256
c434dee6
AJ
2257 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2258 PLT_ENTRY_SIZE;
70256ad8 2259 }
70256ad8
AJ
2260 }
2261
c434dee6 2262 if (htab->sgotplt)
70256ad8 2263 {
c434dee6
AJ
2264 /* Fill in the first three entries in the global offset table. */
2265 if (htab->sgotplt->_raw_size > 0)
2266 {
2267 /* Set the first entry in the global offset table to the address of
2268 the dynamic section. */
2269 if (sdyn == NULL)
2270 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2271 else
2272 bfd_put_64 (output_bfd,
2273 sdyn->output_section->vma + sdyn->output_offset,
2274 htab->sgotplt->contents);
2275 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2276 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2277 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2278 }
70256ad8 2279
c434dee6
AJ
2280 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2281 GOT_ENTRY_SIZE;
2282 }
70256ad8 2283
8d88c4ca
NC
2284 return true;
2285}
2286
8df9fc9d 2287
70256ad8
AJ
2288#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2289#define TARGET_LITTLE_NAME "elf64-x86-64"
2290#define ELF_ARCH bfd_arch_i386
2291#define ELF_MACHINE_CODE EM_X86_64
2292#define ELF_MAXPAGESIZE 0x100000
2293
2294#define elf_backend_can_gc_sections 1
51b64d56 2295#define elf_backend_can_refcount 1
70256ad8
AJ
2296#define elf_backend_want_got_plt 1
2297#define elf_backend_plt_readonly 1
2298#define elf_backend_want_plt_sym 0
2299#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2300#define elf_backend_plt_header_size PLT_ENTRY_SIZE
b491616a 2301#define elf_backend_rela_normal 1
70256ad8
AJ
2302
2303#define elf_info_to_howto elf64_x86_64_info_to_howto
70256ad8 2304
70256ad8
AJ
2305#define bfd_elf64_bfd_link_hash_table_create \
2306 elf64_x86_64_link_hash_table_create
407443a3 2307#define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
70256ad8
AJ
2308
2309#define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2310#define elf_backend_check_relocs elf64_x86_64_check_relocs
c434dee6
AJ
2311#define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2312#define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2313#define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
70256ad8
AJ
2314#define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2315#define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2316#define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3bab7989
ML
2317#define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2318#define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
c434dee6 2319#define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
70256ad8
AJ
2320#define elf_backend_relocate_section elf64_x86_64_relocate_section
2321#define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
407443a3 2322#define elf_backend_object_p elf64_x86_64_elf_object_p
8d88c4ca
NC
2323
2324#include "elf64-target.h"
This page took 0.225698 seconds and 4 git commands to generate.