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