]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-x86-64.c
* elf64-x86-64.c (elf64_x86_64_check_relocs): Handle R_X86_64_64.
[thirdparty/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
8d88c4ca 1/* X86-64 specific support for 64-bit ELF
70256ad8 2 Copyright 2000, 2001 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"
23#include "libbfd.h"
24#include "elf-bfd.h"
25
26#include "elf/x86-64.h"
27
28/* We use only the RELA entries. */
29#define USE_RELA
30
31/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
32#define MINUS_ONE (~ (bfd_vma) 0)
33
34/* The relocation "howto" table. Order of fields:
407443a3
AJ
35 type, size, bitsize, pc_relative, complain_on_overflow,
36 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
70256ad8
AJ
37static reloc_howto_type x86_64_elf_howto_table[] =
38{
407443a3 39 HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
fe4770f4
AJ
40 bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0x00000000, 0x00000000,
41 false),
407443a3 42 HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
43 bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE,
44 false),
407443a3 45 HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
fe4770f4
AJ
46 bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff,
47 true),
407443a3 48 HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
fe4770f4
AJ
49 bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff,
50 false),
407443a3 51 HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
fe4770f4
AJ
52 bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff,
53 true),
407443a3 54 HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
55 bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff,
56 false),
407443a3 57 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
58 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE,
59 MINUS_ONE, false),
407443a3 60 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
61 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE,
62 MINUS_ONE, false),
4a73f21b 63 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield,
fe4770f4
AJ
64 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE,
65 MINUS_ONE, false),
407443a3 66 HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed,
fe4770f4
AJ
67 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff,
68 0xffffffff, true),
407443a3 69 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
fe4770f4
AJ
70 bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
71 false),
407443a3 72 HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
fe4770f4
AJ
73 bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff,
74 false),
407443a3
AJ
75 HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
76 bfd_elf_generic_reloc, "R_X86_64_16", false, 0xffff, 0xffff, false),
77 HOWTO(R_X86_64_PC16,0, 1, 16, true, 0, complain_overflow_bitfield,
78 bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0xffff, 0xffff, true),
79 HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_signed,
80 bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false),
81 HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
fe4770f4
AJ
82 bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true),
83
84/* GNU extension to record C++ vtable hierarchy. */
85 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont,
86 NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
87
88/* GNU extension to record C++ vtable member usage. */
89 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, false, 0, complain_overflow_dont,
90 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
91 false)
8d88c4ca
NC
92};
93
94/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
95struct elf_reloc_map
96{
8d88c4ca
NC
97 bfd_reloc_code_real_type bfd_reloc_val;
98 unsigned char elf_reloc_val;
99};
100
101static CONST struct elf_reloc_map x86_64_reloc_map[] =
102{
70256ad8
AJ
103 { BFD_RELOC_NONE, R_X86_64_NONE, },
104 { BFD_RELOC_64, R_X86_64_64, },
105 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
106 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
107 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
108 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
109 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
110 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
111 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
112 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
113 { BFD_RELOC_32, R_X86_64_32, },
114 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
115 { BFD_RELOC_16, R_X86_64_16, },
116 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
117 { BFD_RELOC_8, R_X86_64_8, },
118 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
fe4770f4
AJ
119 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
120 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
121};
122
8d88c4ca
NC
123static reloc_howto_type *elf64_x86_64_reloc_type_lookup
124 PARAMS ((bfd *, bfd_reloc_code_real_type));
125static void elf64_x86_64_info_to_howto
126 PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
127static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
128 PARAMS ((bfd *));
70256ad8
AJ
129
130static struct bfd_hash_entry *elf64_x86_64_link_hash_newfunc
131 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
132static boolean elf64_x86_64_adjust_dynamic_symbol
133 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
134
135static boolean elf64_x86_64_size_dynamic_sections
136 PARAMS ((bfd *, struct bfd_link_info *));
8d88c4ca
NC
137static boolean elf64_x86_64_relocate_section
138 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
407443a3 139 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
70256ad8
AJ
140static boolean elf64_x86_64_finish_dynamic_symbol
141 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
142 Elf_Internal_Sym *sym));
143static boolean elf64_x86_64_finish_dynamic_sections
144 PARAMS ((bfd *, struct bfd_link_info *));
8d88c4ca
NC
145
146/* Given a BFD reloc type, return a HOWTO structure. */
147static reloc_howto_type *
148elf64_x86_64_reloc_type_lookup (abfd, code)
149 bfd *abfd ATTRIBUTE_UNUSED;
150 bfd_reloc_code_real_type code;
151{
152 unsigned int i;
153 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
154 i++)
155 {
156 if (x86_64_reloc_map[i].bfd_reloc_val == code)
8da6118f
KH
157 return &x86_64_elf_howto_table[(int)
158 x86_64_reloc_map[i].elf_reloc_val];
8d88c4ca
NC
159 }
160 return 0;
161}
162
8d88c4ca 163/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 164
8d88c4ca
NC
165static void
166elf64_x86_64_info_to_howto (abfd, cache_ptr, dst)
167 bfd *abfd ATTRIBUTE_UNUSED;
168 arelent *cache_ptr;
169 Elf64_Internal_Rela *dst;
170{
fe4770f4 171 unsigned r_type, i;
8d88c4ca
NC
172
173 r_type = ELF64_R_TYPE (dst->r_info);
fe4770f4
AJ
174 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
175 {
176 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_PC8);
177 i = r_type;
178 }
179 else
180 {
181 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
182 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_PC8 - 1);
183 }
184 cache_ptr->howto = &x86_64_elf_howto_table[i];
8d88c4ca
NC
185 BFD_ASSERT (r_type == cache_ptr->howto->type);
186}
70256ad8 187\f
407443a3 188/* Functions for the x86-64 ELF linker. */
70256ad8 189
407443a3 190/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
191 section. */
192
407443a3 193#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
70256ad8
AJ
194
195/* The size in bytes of an entry in the global offset table. */
196
197#define GOT_ENTRY_SIZE 8
8d88c4ca 198
70256ad8 199/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 200
70256ad8
AJ
201#define PLT_ENTRY_SIZE 16
202
203/* The first entry in a procedure linkage table looks like this. See the
204 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
205
206static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
207{
653165cc
AJ
208 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
209 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
210 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
70256ad8
AJ
211};
212
213/* Subsequent entries in a procedure linkage table look like this. */
214
215static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
216{
653165cc 217 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 218 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 219 0x68, /* pushq immediate */
70256ad8
AJ
220 0, 0, 0, 0, /* replaced with index into relocation table. */
221 0xe9, /* jmp relative */
222 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
223};
224
225/* The x86-64 linker needs to keep track of the number of relocs that
407443a3 226 it decides to copy in check_relocs for each symbol. This is so
70256ad8
AJ
227 that it can discard PC relative relocs if it doesn't need them when
228 linking with -Bsymbolic. We store the information in a field
407443a3 229 extending the regular ELF linker hash table. */
70256ad8
AJ
230
231/* This structure keeps track of the number of PC relative relocs we
232 have copied for a given symbol. */
233
234struct elf64_x86_64_pcrel_relocs_copied
235{
236 /* Next section. */
237 struct elf64_x86_64_pcrel_relocs_copied *next;
238 /* A section in dynobj. */
239 asection *section;
240 /* Number of relocs copied in this section. */
241 bfd_size_type count;
242};
243
244/* x86-64 ELF linker hash entry. */
245
246struct elf64_x86_64_link_hash_entry
247{
248 struct elf_link_hash_entry root;
249
250 /* Number of PC relative relocs copied for this symbol. */
251 struct elf64_x86_64_pcrel_relocs_copied *pcrel_relocs_copied;
252};
253
254/* x86-64 ELF linker hash table. */
8d88c4ca 255
407443a3
AJ
256struct elf64_x86_64_link_hash_table
257{
8d88c4ca
NC
258 struct elf_link_hash_table root;
259};
260
70256ad8
AJ
261/* Declare this now that the above structures are defined. */
262
263static boolean elf64_x86_64_discard_copies
264 PARAMS ((struct elf64_x86_64_link_hash_entry *, PTR));
265
266/* Traverse an x86-64 ELF linker hash table. */
267
268#define elf64_x86_64_link_hash_traverse(table, func, info) \
269 (elf_link_hash_traverse \
270 (&(table)->root, \
271 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
272 (info)))
273
274/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca
NC
275
276#define elf64_x86_64_hash_table(p) \
277 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
278
407443a3 279/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
280
281static struct bfd_hash_entry *
282elf64_x86_64_link_hash_newfunc (entry, table, string)
283 struct bfd_hash_entry *entry;
284 struct bfd_hash_table *table;
285 const char *string;
286{
287 struct elf64_x86_64_link_hash_entry *ret =
288 (struct elf64_x86_64_link_hash_entry *) entry;
289
290 /* Allocate the structure if it has not already been allocated by a
407443a3 291 subclass. */
70256ad8
AJ
292 if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
293 ret = ((struct elf64_x86_64_link_hash_entry *)
294 bfd_hash_allocate (table,
295 sizeof (struct elf64_x86_64_link_hash_entry)));
296 if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
297 return (struct bfd_hash_entry *) ret;
298
299 /* Call the allocation method of the superclass. */
300 ret = ((struct elf64_x86_64_link_hash_entry *)
301 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
302 table, string));
303 if (ret != (struct elf64_x86_64_link_hash_entry *) NULL)
304 {
305 ret->pcrel_relocs_copied = NULL;
306 }
307
308 return (struct bfd_hash_entry *) ret;
309}
310
8d88c4ca
NC
311/* Create an X86-64 ELF linker hash table. */
312
313static struct bfd_link_hash_table *
314elf64_x86_64_link_hash_table_create (abfd)
315 bfd *abfd;
316{
317 struct elf64_x86_64_link_hash_table *ret;
318
319 ret = ((struct elf64_x86_64_link_hash_table *)
8da6118f 320 bfd_alloc (abfd, sizeof (struct elf64_x86_64_link_hash_table)));
8d88c4ca
NC
321 if (ret == (struct elf64_x86_64_link_hash_table *) NULL)
322 return NULL;
323
324 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
70256ad8 325 elf64_x86_64_link_hash_newfunc))
8d88c4ca
NC
326 {
327 bfd_release (abfd, ret);
328 return NULL;
329 }
330
331 return &ret->root.root;
332}
333
334boolean
335elf64_x86_64_elf_object_p (abfd)
336 bfd *abfd;
337{
338 /* Set the right machine number for an x86-64 elf64 file. */
339 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
340 return true;
341}
342
70256ad8
AJ
343/* Look through the relocs for a section during the first phase, and
344 allocate space in the global offset table or procedure linkage
345 table. */
346
347static boolean
348elf64_x86_64_check_relocs (abfd, info, sec, relocs)
349 bfd *abfd;
350 struct bfd_link_info *info;
351 asection *sec;
352 const Elf_Internal_Rela *relocs;
353{
354 bfd *dynobj;
355 Elf_Internal_Shdr *symtab_hdr;
356 struct elf_link_hash_entry **sym_hashes;
357 bfd_signed_vma *local_got_refcounts;
358 const Elf_Internal_Rela *rel;
359 const Elf_Internal_Rela *rel_end;
360 asection *sgot;
361 asection *srelgot;
362 asection *sreloc;
363
364 if (info->relocateable)
365 return true;
366
367 dynobj = elf_hash_table (info)->dynobj;
368 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
369 sym_hashes = elf_sym_hashes (abfd);
370 local_got_refcounts = elf_local_got_refcounts (abfd);
371
372 sgot = srelgot = sreloc = NULL;
373 rel_end = relocs + sec->reloc_count;
374 for (rel = relocs; rel < rel_end; rel++)
375 {
376 unsigned long r_symndx;
377 struct elf_link_hash_entry *h;
378
379 r_symndx = ELF64_R_SYM (rel->r_info);
380 if (r_symndx < symtab_hdr->sh_info)
381 h = NULL;
382 else
383 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
384
385 /* Some relocs require a global offset table. */
386 if (dynobj == NULL)
387 {
388 switch (ELF64_R_TYPE (rel->r_info))
389 {
390 case R_X86_64_GOT32:
391 case R_X86_64_GOTPCREL:
392 elf_hash_table (info)->dynobj = dynobj = abfd;
393 if (! _bfd_elf_create_got_section (dynobj, info))
394 return false;
395 break;
396 }
397 }
398
399 switch (ELF64_R_TYPE (rel->r_info))
400 {
51e0a107 401 case R_X86_64_GOTPCREL:
70256ad8 402 case R_X86_64_GOT32:
407443a3 403 /* This symbol requires a global offset table entry. */
70256ad8
AJ
404
405 if (sgot == NULL)
406 {
407 sgot = bfd_get_section_by_name (dynobj, ".got");
408 BFD_ASSERT (sgot != NULL);
409 }
410
411 if (srelgot == NULL && (h != NULL || info->shared))
412 {
413 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
414 if (srelgot == NULL)
415 {
416 srelgot = bfd_make_section (dynobj, ".rela.got");
417 if (srelgot == NULL
418 || ! bfd_set_section_flags (dynobj, srelgot,
419 (SEC_ALLOC
420 | SEC_LOAD
421 | SEC_HAS_CONTENTS
422 | SEC_IN_MEMORY
423 | SEC_LINKER_CREATED
424 | SEC_READONLY))
51e0a107 425 || ! bfd_set_section_alignment (dynobj, srelgot, 3))
70256ad8
AJ
426 return false;
427 }
428 }
429
430 if (h != NULL)
431 {
432 if (h->got.refcount == -1)
433 {
434 h->got.refcount = 1;
435
436 /* Make sure this symbol is output as a dynamic symbol. */
437 if (h->dynindx == -1)
438 {
439 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
440 return false;
441 }
442
443 sgot->_raw_size += GOT_ENTRY_SIZE;
444 srelgot->_raw_size += sizeof (Elf64_External_Rela);
445 }
446 else
447 h->got.refcount += 1;
448 }
449 else
450 {
451 /* This is a global offset table entry for a local symbol. */
452 if (local_got_refcounts == NULL)
453 {
454 size_t size;
455
456 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
457 local_got_refcounts = ((bfd_signed_vma *)
458 bfd_alloc (abfd, size));
459 if (local_got_refcounts == NULL)
460 return false;
461 elf_local_got_refcounts (abfd) = local_got_refcounts;
462 memset (local_got_refcounts, -1, size);
463 }
464 if (local_got_refcounts[r_symndx] == -1)
465 {
466 local_got_refcounts[r_symndx] = 1;
467
468 sgot->_raw_size += GOT_ENTRY_SIZE;
469 if (info->shared)
470 {
471 /* If we are generating a shared object, we need to
472 output a R_X86_64_RELATIVE reloc so that the dynamic
473 linker can adjust this GOT entry. */
474 srelgot->_raw_size += sizeof (Elf64_External_Rela);
475 }
476 }
477 else
478 local_got_refcounts[r_symndx] += 1;
479 }
480 break;
481
482 case R_X86_64_PLT32:
483 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
484 actually build the entry in adjust_dynamic_symbol,
485 because this might be a case of linking PIC code which is
486 never referenced by a dynamic object, in which case we
487 don't need to generate a procedure linkage table entry
488 after all. */
70256ad8
AJ
489
490 /* If this is a local symbol, we resolve it directly without
407443a3 491 creating a procedure linkage table entry. */
70256ad8
AJ
492 if (h == NULL)
493 continue;
494
495 if (h->plt.refcount == -1)
496 {
497 h->plt.refcount = 1;
498 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
499 }
500 else
501 h->plt.refcount += 1;
502 break;
503
6b3db546 504 case R_X86_64_64:
70256ad8
AJ
505 case R_X86_64_32:
506 case R_X86_64_32S:
507 case R_X86_64_PC32:
508 if (h != NULL)
509 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
510
511 /* If we are creating a shared library, and this is a reloc
512 against a global symbol, or a non PC relative reloc
513 against a local symbol, then we need to copy the reloc
514 into the shared library. However, if we are linking with
515 -Bsymbolic, we do not need to copy a reloc against a
516 global symbol which is defined in an object we are
407443a3 517 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
518 this point we have not seen all the input files, so it is
519 possible that DEF_REGULAR is not set now but will be set
520 later (it is never cleared). We account for that
521 possibility below by storing information in the
522 pcrel_relocs_copied field of the hash table entry.
523 A similar situation occurs when creating shared libraries
524 and symbol visibility changes render the symbol local. */
525 if (info->shared
526 && (sec->flags & SEC_ALLOC) != 0
527 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32
528 || (h != NULL
529 && (! info->symbolic
530 || (h->elf_link_hash_flags
531 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
532 {
533 /* When creating a shared object, we must copy these
534 reloc types into the output file. We create a reloc
535 section in dynobj and make room for this reloc. */
536 if (sreloc == NULL)
537 {
538 const char *name;
539
540 name = (bfd_elf_string_from_elf_section
541 (abfd,
542 elf_elfheader (abfd)->e_shstrndx,
543 elf_section_data (sec)->rel_hdr.sh_name));
544 if (name == NULL)
545 return false;
546
547 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
548 && strcmp (bfd_get_section_name (abfd, sec),
549 name + 5) == 0);
550
551 sreloc = bfd_get_section_by_name (dynobj, name);
552 if (sreloc == NULL)
553 {
554 flagword flags;
555
556 sreloc = bfd_make_section (dynobj, name);
557 flags = (SEC_HAS_CONTENTS | SEC_READONLY
558 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
559 if ((sec->flags & SEC_ALLOC) != 0)
560 flags |= SEC_ALLOC | SEC_LOAD;
561 if (sreloc == NULL
562 || ! bfd_set_section_flags (dynobj, sreloc, flags)
563 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
564 return false;
565 }
566 }
567
568 sreloc->_raw_size += sizeof (Elf64_External_Rela);
569
570 /* If this is a global symbol, we count the number of PC
571 relative relocations we have entered for this symbol,
572 so that we can discard them later as necessary. Note
573 that this function is only called if we are using an
574 elf64_x86_64 linker hash table, which means that h is
575 really a pointer to an elf64_x86_64_link_hash_entry. */
fe4770f4 576 if (h != NULL && ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
70256ad8
AJ
577 {
578 struct elf64_x86_64_link_hash_entry *eh;
579 struct elf64_x86_64_pcrel_relocs_copied *p;
580
581 eh = (struct elf64_x86_64_link_hash_entry *) h;
582
583 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
584 if (p->section == sreloc)
585 break;
586
587 if (p == NULL)
588 {
589 p = ((struct elf64_x86_64_pcrel_relocs_copied *)
590 bfd_alloc (dynobj, sizeof *p));
591 if (p == NULL)
592 return false;
593 p->next = eh->pcrel_relocs_copied;
594 eh->pcrel_relocs_copied = p;
595 p->section = sreloc;
596 p->count = 0;
597 }
598
599 ++p->count;
600 }
601 }
602 break;
fe4770f4
AJ
603
604 /* This relocation describes the C++ object vtable hierarchy.
605 Reconstruct it for later use during GC. */
606 case R_X86_64_GNU_VTINHERIT:
607 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
608 return false;
609 break;
610
611 /* This relocation describes which C++ vtable entries are actually
612 used. Record for later use during GC. */
613 case R_X86_64_GNU_VTENTRY:
614 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_offset))
615 return false;
616 break;
70256ad8
AJ
617 }
618 }
619
620 return true;
621}
622
623/* Return the section that should be marked against GC for a given
407443a3 624 relocation. */
70256ad8
AJ
625
626static asection *
627elf64_x86_64_gc_mark_hook (abfd, info, rel, h, sym)
628 bfd *abfd;
629 struct bfd_link_info *info ATTRIBUTE_UNUSED;
630 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED;
631 struct elf_link_hash_entry *h;
632 Elf_Internal_Sym *sym;
633{
634 if (h != NULL)
635 {
fe4770f4 636 switch (ELF64_R_TYPE (rel->r_info))
70256ad8 637 {
fe4770f4
AJ
638 case R_X86_64_GNU_VTINHERIT:
639 case R_X86_64_GNU_VTENTRY:
640 break;
70256ad8
AJ
641
642 default:
fe4770f4
AJ
643 switch (h->root.type)
644 {
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 return h->root.u.def.section;
648
649 case bfd_link_hash_common:
650 return h->root.u.c.p->section;
651
652 default:
653 break;
654 }
70256ad8
AJ
655 }
656 }
657 else
658 {
659 if (!(elf_bad_symtab (abfd)
660 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
661 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
662 && sym->st_shndx != SHN_COMMON))
663 {
664 return bfd_section_from_elf_index (abfd, sym->st_shndx);
665 }
666 }
667
668 return NULL;
669}
670
407443a3 671/* Update the got entry reference counts for the section being removed. */
70256ad8
AJ
672
673static boolean
674elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
675 bfd *abfd;
676 struct bfd_link_info *info ATTRIBUTE_UNUSED;
677 asection *sec;
678 const Elf_Internal_Rela *relocs;
679{
680 Elf_Internal_Shdr *symtab_hdr;
681 struct elf_link_hash_entry **sym_hashes;
682 bfd_signed_vma *local_got_refcounts;
683 const Elf_Internal_Rela *rel, *relend;
684 unsigned long r_symndx;
685 struct elf_link_hash_entry *h;
686 bfd *dynobj;
687 asection *sgot;
688 asection *srelgot;
689
690 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
691 sym_hashes = elf_sym_hashes (abfd);
692 local_got_refcounts = elf_local_got_refcounts (abfd);
693
694 dynobj = elf_hash_table (info)->dynobj;
695 if (dynobj == NULL)
696 return true;
697
698 sgot = bfd_get_section_by_name (dynobj, ".got");
699 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
700
701 relend = relocs + sec->reloc_count;
702 for (rel = relocs; rel < relend; rel++)
703 switch (ELF64_R_TYPE (rel->r_info))
704 {
705 case R_X86_64_GOT32:
706 case R_X86_64_GOTPCREL:
707 r_symndx = ELF64_R_SYM (rel->r_info);
708 if (r_symndx >= symtab_hdr->sh_info)
709 {
710 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
711 if (h->got.refcount > 0)
712 {
713 h->got.refcount -= 1;
714 if (h->got.refcount == 0)
715 {
716 sgot->_raw_size -= GOT_ENTRY_SIZE;
717 srelgot->_raw_size -= sizeof (Elf64_External_Rela);
718 }
719 }
720 }
721 else if (local_got_refcounts != NULL)
722 {
723 if (local_got_refcounts[r_symndx] > 0)
724 {
725 local_got_refcounts[r_symndx] -= 1;
726 if (local_got_refcounts[r_symndx] == 0)
727 {
728 sgot->_raw_size -= GOT_ENTRY_SIZE;
729 if (info->shared)
730 srelgot->_raw_size -= sizeof (Elf64_External_Rela);
731 }
732 }
733 }
734 break;
735
736 case R_X86_64_PLT32:
737 r_symndx = ELF64_R_SYM (rel->r_info);
738 if (r_symndx >= symtab_hdr->sh_info)
739 {
740 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
741 if (h->plt.refcount > 0)
742 h->plt.refcount -= 1;
743 }
744 break;
745
746 default:
747 break;
748 }
749
750 return true;
751}
752
753/* Adjust a symbol defined by a dynamic object and referenced by a
754 regular object. The current definition is in some section of the
755 dynamic object, but we're not including those sections. We have to
756 change the definition to something the rest of the link can
407443a3 757 understand. */
70256ad8
AJ
758
759static boolean
760elf64_x86_64_adjust_dynamic_symbol (info, h)
761 struct bfd_link_info *info;
762 struct elf_link_hash_entry *h;
763{
764 bfd *dynobj;
765 asection *s;
766 unsigned int power_of_two;
767
768 dynobj = elf_hash_table (info)->dynobj;
769
770 /* Make sure we know what is going on here. */
771 BFD_ASSERT (dynobj != NULL
772 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
773 || h->weakdef != NULL
774 || ((h->elf_link_hash_flags
775 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
776 && (h->elf_link_hash_flags
777 & ELF_LINK_HASH_REF_REGULAR) != 0
778 && (h->elf_link_hash_flags
779 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
780
781 /* If this is a function, put it in the procedure linkage table. We
782 will fill in the contents of the procedure linkage table later,
783 when we know the address of the .got section. */
784 if (h->type == STT_FUNC
785 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
786 {
407443a3
AJ
787 if ((! info->shared
788 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
789 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
790 || (info->shared && h->plt.refcount <= 0))
70256ad8 791 {
70256ad8
AJ
792 /* This case can occur if we saw a PLT32 reloc in an input
793 file, but the symbol was never referred to by a dynamic
794 object, or if all references were garbage collected. In
795 such a case, we don't actually need to build a procedure
796 linkage table, and we can just do a PC32 reloc instead. */
70256ad8
AJ
797 h->plt.offset = (bfd_vma) -1;
798 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
799 return true;
800 }
801
802 /* Make sure this symbol is output as a dynamic symbol. */
803 if (h->dynindx == -1)
804 {
805 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
806 return false;
807 }
808
809 s = bfd_get_section_by_name (dynobj, ".plt");
810 BFD_ASSERT (s != NULL);
811
812 /* If this is the first .plt entry, make room for the special
813 first entry. */
814 if (s->_raw_size == 0)
815 s->_raw_size = PLT_ENTRY_SIZE;
816
817 /* If this symbol is not defined in a regular file, and we are
818 not generating a shared library, then set the symbol to this
407443a3 819 location in the .plt. This is required to make function
70256ad8
AJ
820 pointers compare as equal between the normal executable and
821 the shared library. */
822 if (! info->shared
823 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
824 {
825 h->root.u.def.section = s;
826 h->root.u.def.value = s->_raw_size;
827 }
828
829 h->plt.offset = s->_raw_size;
830
831 /* Make room for this entry. */
832 s->_raw_size += PLT_ENTRY_SIZE;
833
407443a3
AJ
834 /* We also need to make an entry in the .got.plt section, which
835 will be placed in the .got section by the linker script. */
836 s = bfd_get_section_by_name (dynobj, ".got.plt");
837 BFD_ASSERT (s != NULL);
838 s->_raw_size += GOT_ENTRY_SIZE;
839
70256ad8
AJ
840 /* We also need to make an entry in the .rela.plt section. */
841 s = bfd_get_section_by_name (dynobj, ".rela.plt");
842 BFD_ASSERT (s != NULL);
843 s->_raw_size += sizeof (Elf64_External_Rela);
844
845 return true;
846 }
847
848 /* If this is a weak symbol, and there is a real definition, the
849 processor independent code will have arranged for us to see the
407443a3 850 real definition first, and we can just use the same value. */
70256ad8
AJ
851 if (h->weakdef != NULL)
852 {
853 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
854 || h->weakdef->root.type == bfd_link_hash_defweak);
855 h->root.u.def.section = h->weakdef->root.u.def.section;
856 h->root.u.def.value = h->weakdef->root.u.def.value;
857 return true;
858 }
859
860 /* This is a reference to a symbol defined by a dynamic object which
407443a3 861 is not a function. */
70256ad8
AJ
862
863 /* If we are creating a shared library, we must presume that the
864 only references to the symbol are via the global offset table.
865 For such cases we need not do anything here; the relocations will
407443a3 866 be handled correctly by relocate_section. */
70256ad8
AJ
867 if (info->shared)
868 return true;
869
870 /* If there are no references to this symbol that do not use the
871 GOT, we don't need to generate a copy reloc. */
872 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
873 return true;
874
875 /* We must allocate the symbol in our .dynbss section, which will
407443a3 876 become part of the .bss section of the executable. There will be
70256ad8
AJ
877 an entry for this symbol in the .dynsym section. The dynamic
878 object will contain position independent code, so all references
879 from the dynamic object to this symbol will go through the global
880 offset table. The dynamic linker will use the .dynsym entry to
881 determine the address it must put in the global offset table, so
882 both the dynamic object and the regular object will refer to the
883 same memory location for the variable. */
884
885 s = bfd_get_section_by_name (dynobj, ".dynbss");
886 BFD_ASSERT (s != NULL);
887
888 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
889 to copy the initial value out of the dynamic object and into the
890 runtime process image. We need to remember the offset into the
891 .rela.bss section we are going to use. */
892 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
893 {
894 asection *srel;
895
896 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
897 BFD_ASSERT (srel != NULL);
898 srel->_raw_size += sizeof (Elf64_External_Rela);
899 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
900 }
901
902 /* We need to figure out the alignment required for this symbol. I
407443a3 903 have no idea how ELF linkers handle this. 16-bytes is the size
70256ad8
AJ
904 of the largest type that requires hard alignment -- long double. */
905 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
906 this construct. */
907 power_of_two = bfd_log2 (h->size);
908 if (power_of_two > 4)
909 power_of_two = 4;
910
911 /* Apply the required alignment. */
912 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
913 if (power_of_two > bfd_get_section_alignment (dynobj, s))
914 {
915 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
916 return false;
917 }
918
919 /* Define the symbol as being at this point in the section. */
920 h->root.u.def.section = s;
921 h->root.u.def.value = s->_raw_size;
922
923 /* Increment the section size to make room for the symbol. */
924 s->_raw_size += h->size;
925
926 return true;
927}
928
929/* Set the sizes of the dynamic sections. */
930
931static boolean
932elf64_x86_64_size_dynamic_sections (output_bfd, info)
933 bfd *output_bfd;
934 struct bfd_link_info *info;
935{
936 bfd *dynobj;
937 asection *s;
938 boolean plt;
939 boolean relocs;
940 boolean reltext;
941
942 dynobj = elf_hash_table (info)->dynobj;
943 BFD_ASSERT (dynobj != NULL);
944
945 if (elf_hash_table (info)->dynamic_sections_created)
946 {
947 /* Set the contents of the .interp section to the interpreter. */
948 if (! info->shared)
949 {
950 s = bfd_get_section_by_name (dynobj, ".interp");
951 BFD_ASSERT (s != NULL);
952 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
953 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
954 }
955 }
956 else
957 {
958 /* We may have created entries in the .rela.got section.
407443a3
AJ
959 However, if we are not creating the dynamic sections, we will
960 not actually use these entries. Reset the size of .rela.got,
961 which will cause it to get stripped from the output file
962 below. */
70256ad8
AJ
963 s = bfd_get_section_by_name (dynobj, ".rela.got");
964 if (s != NULL)
965 s->_raw_size = 0;
966 }
967
968 /* If this is a -Bsymbolic shared link, then we need to discard all
969 PC relative relocs against symbols defined in a regular object.
970 We allocated space for them in the check_relocs routine, but we
971 will not fill them in in the relocate_section routine. */
972 if (info->shared)
973 elf64_x86_64_link_hash_traverse (elf64_x86_64_hash_table (info),
974 elf64_x86_64_discard_copies,
975 (PTR) info);
976
977 /* The check_relocs and adjust_dynamic_symbol entry points have
978 determined the sizes of the various dynamic sections. Allocate
979 memory for them. */
980 plt = relocs = reltext = false;
981 for (s = dynobj->sections; s != NULL; s = s->next)
982 {
983 const char *name;
984 boolean strip;
985
986 if ((s->flags & SEC_LINKER_CREATED) == 0)
987 continue;
988
989 /* It's OK to base decisions on the section name, because none
990 of the dynobj section names depend upon the input files. */
991 name = bfd_get_section_name (dynobj, s);
992
993 strip = false;
994 if (strcmp (name, ".plt") == 0)
995 {
996 if (s->_raw_size == 0)
997 {
998 /* Strip this section if we don't need it; see the
407443a3 999 comment below. */
70256ad8
AJ
1000 strip = true;
1001 }
1002 else
1003 {
1004 /* Remember whether there is a PLT. */
1005 plt = true;
1006 }
1007 }
1008 else if (strncmp (name, ".rela", 5) == 0)
1009 {
1010 if (s->_raw_size == 0)
1011 {
1012 /* If we don't need this section, strip it from the
1013 output file. This is mostly to handle .rela.bss and
1014 .rela.plt. We must create both sections in
1015 create_dynamic_sections, because they must be created
1016 before the linker maps input sections to output
1017 sections. The linker does that before
1018 adjust_dynamic_symbol is called, and it is that
1019 function which decides whether anything needs to go
1020 into these sections. */
1021 strip = true;
1022 }
1023 else
1024 {
1025 asection *target;
1026
1027 /* Remember whether there are any reloc sections other
407443a3 1028 than .rela.plt. */
70256ad8
AJ
1029 if (strcmp (name, ".rela.plt") != 0)
1030 {
1031 const char *outname;
1032
1033 relocs = true;
1034
1035 /* If this relocation section applies to a read only
1036 section, then we probably need a DT_TEXTREL
1037 entry. The entries in the .rela.plt section
1038 really apply to the .got section, which we
1039 created ourselves and so know is not readonly. */
1040 outname = bfd_get_section_name (output_bfd,
1041 s->output_section);
1042 target = bfd_get_section_by_name (output_bfd, outname + 5);
1043 if (target != NULL
1044 && (target->flags & SEC_READONLY) != 0
1045 && (target->flags & SEC_ALLOC) != 0)
1046 reltext = true;
1047 }
1048
1049 /* We use the reloc_count field as a counter if we need
1050 to copy relocs into the output file. */
1051 s->reloc_count = 0;
1052 }
1053 }
1054 else if (strncmp (name, ".got", 4) != 0)
1055 {
1056 /* It's not one of our sections, so don't allocate space. */
1057 continue;
1058 }
1059
1060 if (strip)
1061 {
1062 _bfd_strip_section_from_output (info, s);
1063 continue;
1064 }
1065
1066 /* Allocate memory for the section contents. We use bfd_zalloc
1067 here in case unused entries are not reclaimed before the
1068 section's contents are written out. This should not happen,
1069 but this way if it does, we get a R_X86_64_NONE reloc instead
1070 of garbage. */
1071 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1072 if (s->contents == NULL && s->_raw_size != 0)
1073 return false;
1074 }
1075
1076 if (elf_hash_table (info)->dynamic_sections_created)
1077 {
1078 /* Add some entries to the .dynamic section. We fill in the
1079 values later, in elf64_x86_64_finish_dynamic_sections, but we
1080 must add the entries now so that we get the correct size for
407443a3 1081 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8
AJ
1082 dynamic linker and used by the debugger. */
1083 if (! info->shared)
1084 {
1085 if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
1086 return false;
1087 }
1088
1089 if (plt)
1090 {
70256ad8
AJ
1091 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0)
1092 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
407443a3 1093 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
70256ad8
AJ
1094 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
1095 return false;
1096 }
1097
1098 if (relocs)
1099 {
1100 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
1101 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
1102 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
1103 sizeof (Elf64_External_Rela)))
1104 return false;
1105 }
1106
1107 if (reltext)
1108 {
1109 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
1110 return false;
1111 info->flags |= DF_TEXTREL;
1112 }
1113 }
1114
1115 return true;
1116}
1117
1118/* This function is called via elf64_x86_64_link_hash_traverse if we are
1119 creating a shared object. In the -Bsymbolic case, it discards the
1120 space allocated to copy PC relative relocs against symbols which
1121 are defined in regular objects. For the normal non-symbolic case,
1122 we also discard space for relocs that have become local due to
1123 symbol visibility changes. We allocated space for them in the
1124 check_relocs routine, but we won't fill them in in the
1125 relocate_section routine. */
1126
1127static boolean
1128elf64_x86_64_discard_copies (h, inf)
1129 struct elf64_x86_64_link_hash_entry *h;
1130 PTR inf;
1131{
1132 struct elf64_x86_64_pcrel_relocs_copied *s;
1133 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1134
1135 /* If a symbol has been forced local or we have found a regular
1136 definition for the symbolic link case, then we won't be needing
1137 any relocs. */
1138 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1139 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1140 || info->symbolic))
1141 {
1142 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1143 s->section->_raw_size -= s->count * sizeof (Elf64_External_Rela);
1144 }
1145
1146 return true;
1147}
1148
8d88c4ca
NC
1149/* Relocate an x86_64 ELF section. */
1150
1151static boolean
1152elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
fe4770f4 1153 contents, relocs, local_syms, local_sections)
8d88c4ca
NC
1154 bfd *output_bfd;
1155 struct bfd_link_info *info;
1156 bfd *input_bfd;
1157 asection *input_section;
1158 bfd_byte *contents;
1159 Elf_Internal_Rela *relocs;
1160 Elf_Internal_Sym *local_syms;
1161 asection **local_sections;
1162{
1163 bfd *dynobj;
1164 Elf_Internal_Shdr *symtab_hdr;
1165 struct elf_link_hash_entry **sym_hashes;
1166 bfd_vma *local_got_offsets;
70256ad8
AJ
1167 asection *sgot;
1168 asection *splt;
8d88c4ca 1169 asection *sreloc;
70256ad8 1170 Elf_Internal_Rela *rela;
8d88c4ca
NC
1171 Elf_Internal_Rela *relend;
1172
1173 dynobj = elf_hash_table (info)->dynobj;
1174 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1175 sym_hashes = elf_sym_hashes (input_bfd);
1176 local_got_offsets = elf_local_got_offsets (input_bfd);
1177
70256ad8 1178 sreloc = splt = sgot = NULL;
8d88c4ca 1179 if (dynobj != NULL)
70256ad8
AJ
1180 {
1181 splt = bfd_get_section_by_name (dynobj, ".plt");
1182 sgot = bfd_get_section_by_name (dynobj, ".got");
1183 }
8d88c4ca 1184
70256ad8 1185 rela = relocs;
8d88c4ca 1186 relend = relocs + input_section->reloc_count;
70256ad8 1187 for (; rela < relend; rela++)
8d88c4ca
NC
1188 {
1189 int r_type;
1190 reloc_howto_type *howto;
1191 unsigned long r_symndx;
1192 struct elf_link_hash_entry *h;
1193 Elf_Internal_Sym *sym;
1194 asection *sec;
1195 bfd_vma relocation;
1196 bfd_reloc_status_type r;
1197 unsigned int indx;
1198
70256ad8 1199 r_type = ELF64_R_TYPE (rela->r_info);
fe4770f4
AJ
1200 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1201 || r_type == (int) R_X86_64_GNU_VTENTRY)
1202 continue;
8d88c4ca
NC
1203
1204 if ((indx = (unsigned) r_type) >= R_X86_64_max)
8da6118f
KH
1205 {
1206 bfd_set_error (bfd_error_bad_value);
1207 return false;
1208 }
8d88c4ca
NC
1209 howto = x86_64_elf_howto_table + indx;
1210
70256ad8 1211 r_symndx = ELF64_R_SYM (rela->r_info);
8d88c4ca
NC
1212
1213 if (info->relocateable)
8da6118f
KH
1214 {
1215 /* This is a relocateable link. We don't have to change
1216 anything, unless the reloc is against a section symbol,
1217 in which case we have to adjust according to where the
1218 section symbol winds up in the output section. */
1219 if (r_symndx < symtab_hdr->sh_info)
1220 {
1221 sym = local_syms + r_symndx;
1222 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1223 {
1224 sec = local_sections[r_symndx];
70256ad8 1225 rela->r_addend += sec->output_offset + sym->st_value;
8da6118f
KH
1226 }
1227 }
1228
1229 continue;
1230 }
8d88c4ca 1231
407443a3 1232 /* This is a final link. */
8d88c4ca
NC
1233 h = NULL;
1234 sym = NULL;
1235 sec = NULL;
1236 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
1237 {
1238 sym = local_syms + r_symndx;
1239 sec = local_sections[r_symndx];
1240 relocation = (sec->output_section->vma
1241 + sec->output_offset
1242 + sym->st_value);
1243 }
8d88c4ca 1244 else
8da6118f
KH
1245 {
1246 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1247 while (h->root.type == bfd_link_hash_indirect
1248 || h->root.type == bfd_link_hash_warning)
1249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1250 if (h->root.type == bfd_link_hash_defined
1251 || h->root.type == bfd_link_hash_defweak)
1252 {
1253 sec = h->root.u.def.section;
6a2bda3f
AJ
1254 if ((r_type == R_X86_64_PLT32
1255 && splt != NULL
1256 && h->plt.offset != (bfd_vma) -1)
1257 || ((r_type == R_X86_64_GOT32 || r_type == R_X86_64_GOTPCREL)
bcdd92f3
AJ
1258 && elf_hash_table (info)->dynamic_sections_created
1259 && (!info->shared
1260 || (! info->symbolic && h->dynindx != -1)
1261 || (h->elf_link_hash_flags
1262 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1263 || (info->shared
1264 && ((! info->symbolic && h->dynindx != -1)
1265 || (h->elf_link_hash_flags
1266 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1267 && ( r_type == R_X86_64_8 ||
1268 r_type == R_X86_64_16 ||
1269 r_type == R_X86_64_32 ||
1270 r_type == R_X86_64_64 ||
1271 r_type == R_X86_64_PC16 ||
1272 r_type == R_X86_64_PC32)
1273 && ((input_section->flags & SEC_ALLOC) != 0
1274 /* DWARF will emit R_X86_64_32 relocations in its
1275 sections against symbols defined externally
1276 in shared libraries. We can't do anything
1277 with them here. */
1278 || ((input_section->flags & SEC_DEBUGGING) != 0
1279 && (h->elf_link_hash_flags
1280 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
1281 {
1282 /* In these cases, we don't need the relocation
1283 value. We check specially because in some
1284 obscure cases sec->output_section will be NULL. */
1285 relocation = 0;
1286 }
1287 else if (sec->output_section == NULL)
8da6118f
KH
1288 {
1289 (*_bfd_error_handler)
1290 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1291 bfd_get_filename (input_bfd), h->root.root.string,
1292 bfd_get_section_name (input_bfd, input_section));
1293 relocation = 0;
1294 }
1295 else
1296 relocation = (h->root.u.def.value
1297 + sec->output_section->vma
1298 + sec->output_offset);
1299 }
1300 else if (h->root.type == bfd_link_hash_undefweak)
1301 relocation = 0;
70256ad8
AJ
1302 else if (info->shared && !info->symbolic && !info->no_undefined
1303 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1304 relocation = 0;
8da6118f
KH
1305 else
1306 {
1307 if (! ((*info->callbacks->undefined_symbol)
1308 (info, h->root.root.string, input_bfd,
70256ad8 1309 input_section, rela->r_offset,
8da6118f
KH
1310 (!info->shared || info->no_undefined
1311 || ELF_ST_VISIBILITY (h->other)))))
1312 return false;
1313 relocation = 0;
1314 }
1315 }
70256ad8
AJ
1316
1317 /* When generating a shared object, the relocations handled here are
1318 copied into the output file to be resolved at run time. */
1319 switch (r_type)
1320 {
1321 case R_X86_64_GOT32:
1322 /* Relocation is to the entry for this symbol in the global
1323 offset table. */
70256ad8
AJ
1324 case R_X86_64_GOTPCREL:
1325 /* Use global offset table as symbol value. */
51e0a107 1326 BFD_ASSERT (sgot != NULL);
053579d7 1327
51e0a107 1328 if (h != NULL)
70256ad8 1329 {
51e0a107
JH
1330 bfd_vma off = h->got.offset;
1331 BFD_ASSERT (off != (bfd_vma) -1);
1332
1333 if (! elf_hash_table (info)->dynamic_sections_created
1334 || (info->shared
1335 && (info->symbolic || h->dynindx == -1)
1336 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1337 {
1338 /* This is actually a static link, or it is a -Bsymbolic
1339 link and the symbol is defined locally, or the symbol
407443a3 1340 was forced to be local because of a version file. We
51e0a107
JH
1341 must initialize this entry in the global offset table.
1342 Since the offset must always be a multiple of 8, we
1343 use the least significant bit to record whether we
1344 have initialized it already.
1345
1346 When doing a dynamic link, we create a .rela.got
407443a3
AJ
1347 relocation entry to initialize the value. This is
1348 done in the finish_dynamic_symbol routine. */
51e0a107
JH
1349 if ((off & 1) != 0)
1350 off &= ~1;
1351 else
1352 {
1353 bfd_put_64 (output_bfd, relocation,
1354 sgot->contents + off);
1355 h->got.offset |= 1;
1356 }
1357 }
053579d7
AJ
1358 if (r_type == R_X86_64_GOTPCREL)
1359 relocation = sgot->output_section->vma + sgot->output_offset + off;
1360 else
1361 relocation = sgot->output_offset + off;
70256ad8 1362 }
51e0a107
JH
1363 else
1364 {
1365 bfd_vma off;
70256ad8 1366
51e0a107
JH
1367 BFD_ASSERT (local_got_offsets != NULL
1368 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1369
1370 off = local_got_offsets[r_symndx];
1371
1372 /* The offset must always be a multiple of 8. We use
407443a3
AJ
1373 the least significant bit to record whether we have
1374 already generated the necessary reloc. */
51e0a107
JH
1375 if ((off & 1) != 0)
1376 off &= ~1;
1377 else
1378 {
1379 bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1380
1381 if (info->shared)
1382 {
1383 asection *srelgot;
1384 Elf_Internal_Rela outrel;
70256ad8 1385
51e0a107
JH
1386 /* We need to generate a R_X86_64_RELATIVE reloc
1387 for the dynamic linker. */
1388 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1389 BFD_ASSERT (srelgot != NULL);
1390
1391 outrel.r_offset = (sgot->output_section->vma
1392 + sgot->output_offset
1393 + off);
1394 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1395 outrel.r_addend = relocation;
1396 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1397 (((Elf64_External_Rela *)
1398 srelgot->contents)
1399 + srelgot->reloc_count));
1400 ++srelgot->reloc_count;
1401 }
1402
1403 local_got_offsets[r_symndx] |= 1;
1404 }
1405
053579d7
AJ
1406 if (r_type == R_X86_64_GOTPCREL)
1407 relocation = sgot->output_section->vma + sgot->output_offset + off;
1408 else
1409 relocation = sgot->output_offset + off;
51e0a107 1410 }
6a2bda3f 1411
70256ad8
AJ
1412 break;
1413
1414 case R_X86_64_PLT32:
1415 /* Relocation is to the entry for this symbol in the
1416 procedure linkage table. */
1417
1418 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 1419 without using the procedure linkage table. */
70256ad8
AJ
1420 if (h == NULL)
1421 break;
1422
1423 if (h->plt.offset == (bfd_vma) -1 || splt == NULL)
1424 {
1425 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
1426 happens when statically linking PIC code, or when
1427 using -Bsymbolic. */
70256ad8
AJ
1428 break;
1429 }
1430
1431 relocation = (splt->output_section->vma
1432 + splt->output_offset
1433 + h->plt.offset);
1434 break;
1435
1436 case R_X86_64_8:
1437 case R_X86_64_16:
1438 case R_X86_64_32:
6b3db546 1439 case R_X86_64_64:
70256ad8
AJ
1440 case R_X86_64_PC8:
1441 case R_X86_64_PC16:
1442 case R_X86_64_PC32:
1443 /* FIXME: The abi says the linker should make sure the value is
407443a3 1444 the same when it's zeroextended to 64 bit. */
70256ad8
AJ
1445 if (info->shared
1446 && (input_section->flags & SEC_ALLOC) != 0
1447 && ((r_type != R_X86_64_PC8 && r_type != R_X86_64_PC16
1448 && r_type != R_X86_64_PC32)
1449 || (h != NULL
1450 && h->dynindx != -1
1451 && (! info->symbolic
1452 || (h->elf_link_hash_flags
1453 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1454 {
1455 Elf_Internal_Rela outrel;
1456 boolean skip, relocate;
1457
1458 /* When generating a shared object, these relocations
1459 are copied into the output file to be resolved at run
407443a3 1460 time. */
70256ad8
AJ
1461
1462 if (sreloc == NULL)
1463 {
1464 const char *name;
1465
1466 name = (bfd_elf_string_from_elf_section
1467 (input_bfd,
1468 elf_elfheader (input_bfd)->e_shstrndx,
1469 elf_section_data (input_section)->rel_hdr.sh_name));
1470 if (name == NULL)
1471 return false;
1472
1473 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1474 && strcmp (bfd_get_section_name (input_bfd,
1475 input_section),
1476 name + 5) == 0);
1477
1478 sreloc = bfd_get_section_by_name (dynobj, name);
1479 BFD_ASSERT (sreloc != NULL);
1480 }
1481
1482 skip = false;
1483
1484 if (elf_section_data (input_section)->stab_info == NULL)
1485 outrel.r_offset = rela->r_offset;
1486 else
1487 {
1488 bfd_vma off;
1489
1490 off = (_bfd_stab_section_offset
1491 (output_bfd, &elf_hash_table (info)->stab_info,
1492 input_section,
1493 &elf_section_data (input_section)->stab_info,
1494 rela->r_offset));
1495 if (off == (bfd_vma) -1)
1496 skip = true;
1497 outrel.r_offset = off;
1498 }
1499
1500 outrel.r_offset += (input_section->output_section->vma
1501 + input_section->output_offset);
1502
1503 if (skip)
1504 {
1505 memset (&outrel, 0, sizeof outrel);
1506 relocate = false;
1507 }
1508 else if ((r_type == R_X86_64_PC8) || (r_type == R_X86_64_PC16)
1509 || (r_type == R_X86_64_PC32))
1510 {
1511 BFD_ASSERT (h != NULL && h->dynindx != -1);
1512 relocate = false;
1513 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
5608e05b 1514 outrel.r_addend = relocation + rela->r_addend;
70256ad8
AJ
1515 }
1516 else
1517 {
1518 /* h->dynindx may be -1 if this symbol was marked to
407443a3 1519 become local. */
70256ad8
AJ
1520 if (h == NULL
1521 || ((info->symbolic || h->dynindx == -1)
1522 && (h->elf_link_hash_flags
1523 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1524 {
1525 relocate = true;
1526 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
5608e05b 1527 outrel.r_addend = relocation + rela->r_addend;
70256ad8
AJ
1528 }
1529 else
1530 {
1531 BFD_ASSERT (h->dynindx != -1);
1532 relocate = false;
1533 outrel.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_32);
5608e05b 1534 outrel.r_addend = relocation + rela->r_addend;
70256ad8
AJ
1535 }
1536 }
1537
1538 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1539 (((Elf64_External_Rela *)
1540 sreloc->contents)
1541 + sreloc->reloc_count));
1542 ++sreloc->reloc_count;
1543
1544 /* If this reloc is against an external symbol, we do
1545 not want to fiddle with the addend. Otherwise, we
1546 need to include the symbol value so that it becomes
1547 an addend for the dynamic reloc. */
1548 if (! relocate)
1549 continue;
1550 }
1551
1552 break;
1553
1554 default:
1555 break;
1556 }
8d88c4ca
NC
1557
1558 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
70256ad8
AJ
1559 contents, rela->r_offset,
1560 relocation, rela->r_addend);
8d88c4ca
NC
1561
1562 if (r != bfd_reloc_ok)
8da6118f
KH
1563 {
1564 switch (r)
1565 {
1566 default:
1567 case bfd_reloc_outofrange:
1568 abort ();
1569 case bfd_reloc_overflow:
1570 {
1571 const char *name;
1572
1573 if (h != NULL)
1574 name = h->root.root.string;
1575 else
1576 {
1577 name = bfd_elf_string_from_elf_section (input_bfd,
1578 symtab_hdr->sh_link,
1579 sym->st_name);
1580 if (name == NULL)
1581 return false;
1582 if (*name == '\0')
1583 name = bfd_section_name (input_bfd, sec);
1584 }
1585 if (! ((*info->callbacks->reloc_overflow)
1586 (info, name, howto->name, (bfd_vma) 0,
70256ad8 1587 input_bfd, input_section, rela->r_offset)))
8da6118f
KH
1588 return false;
1589 }
1590 break;
1591 }
1592 }
8d88c4ca 1593 }
70256ad8
AJ
1594
1595 return true;
1596}
1597
1598/* Finish up dynamic symbol handling. We set the contents of various
1599 dynamic sections here. */
1600
1601static boolean
1602elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym)
1603 bfd *output_bfd;
1604 struct bfd_link_info *info;
1605 struct elf_link_hash_entry *h;
1606 Elf_Internal_Sym *sym;
1607{
1608 bfd *dynobj;
1609
1610 dynobj = elf_hash_table (info)->dynobj;
1611
1612 if (h->plt.offset != (bfd_vma) -1)
1613 {
1614 asection *splt;
1615 asection *sgot;
1616 asection *srela;
1617 bfd_vma plt_index;
1618 bfd_vma got_offset;
1619 Elf_Internal_Rela rela;
1620
1621 /* This symbol has an entry in the procedure linkage table. Set
407443a3 1622 it up. */
70256ad8
AJ
1623
1624 BFD_ASSERT (h->dynindx != -1);
1625
1626 splt = bfd_get_section_by_name (dynobj, ".plt");
1627 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1628 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1629 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1630
1631 /* Get the index in the procedure linkage table which
1632 corresponds to this symbol. This is the index of this symbol
1633 in all the symbols for which we are making plt entries. The
1634 first entry in the procedure linkage table is reserved. */
1635 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1636
1637 /* Get the offset into the .got table of the entry that
407443a3 1638 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
fe4770f4 1639 bytes. The first three are reserved for the dynamic linker. */
70256ad8
AJ
1640 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
1641
1642 /* Fill in the entry in the procedure linkage table. */
1643 memcpy (splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
1644 PLT_ENTRY_SIZE);
1645
1646 /* Insert the relocation positions of the plt section. The magic
1647 numbers at the end of the statements are the positions of the
1648 relocations in the plt section. */
653165cc
AJ
1649 /* Put offset for jmp *name@GOTPCREL(%rip), since the
1650 instruction uses 6 bytes, subtract this value. */
1651 bfd_put_32 (output_bfd,
1652 (sgot->output_section->vma
1653 + sgot->output_offset
1654 + got_offset
1655 - splt->output_section->vma
1656 - splt->output_offset
1657 - h->plt.offset
1658 - 6),
1659 splt->contents + h->plt.offset + 2);
1660 /* Put relocation index. */
1661 bfd_put_32 (output_bfd, plt_index,
70256ad8 1662 splt->contents + h->plt.offset + 7);
653165cc
AJ
1663 /* Put offset for jmp .PLT0. */
1664 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
70256ad8
AJ
1665 splt->contents + h->plt.offset + 12);
1666
653165cc
AJ
1667 /* Fill in the entry in the global offset table, initially this
1668 points to the pushq instruction in the PLT which is at offset 6. */
70256ad8
AJ
1669 bfd_put_64 (output_bfd, (splt->output_section->vma + splt->output_offset
1670 + h->plt.offset + 6),
1671 sgot->contents + got_offset);
1672
1673 /* Fill in the entry in the .rela.plt section. */
1674 rela.r_offset = (sgot->output_section->vma
1675 + sgot->output_offset
1676 + got_offset);
1677 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
1678 rela.r_addend = 0;
1679 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1680 ((Elf64_External_Rela *) srela->contents
1681 + plt_index));
1682
1683 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1684 {
1685 /* Mark the symbol as undefined, rather than as defined in
407443a3 1686 the .plt section. Leave the value alone. */
70256ad8 1687 sym->st_shndx = SHN_UNDEF;
05aa1441
AJ
1688 /* If the symbol is weak, we do need to clear the value.
1689 Otherwise, the PLT entry would provide a definition for
1690 the symbol even if the symbol wasn't defined anywhere,
1691 and so the symbol would never be NULL. */
1692 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1693 == 0)
1694 sym->st_value = 0;
70256ad8
AJ
1695 }
1696 }
1697
053579d7
AJ
1698 if (h->got.offset != (bfd_vma) -1)
1699 {
1700 asection *sgot;
1701 asection *srela;
1702 Elf_Internal_Rela rela;
1703
1704 /* This symbol has an entry in the global offset table. Set it
1705 up. */
1706
1707 sgot = bfd_get_section_by_name (dynobj, ".got");
1708 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1709 BFD_ASSERT (sgot != NULL && srela != NULL);
1710
1711 rela.r_offset = (sgot->output_section->vma
1712 + sgot->output_offset
1713 + (h->got.offset &~ 1));
1714
1715 /* If this is a static link, or it is a -Bsymbolic link and the
1716 symbol is defined locally or was forced to be local because
1717 of a version file, we just want to emit a RELATIVE reloc.
1718 The entry in the global offset table will already have been
1719 initialized in the relocate_section function. */
1720 if (! elf_hash_table (info)->dynamic_sections_created
1721 || (info->shared
1722 && (info->symbolic || h->dynindx == -1)
1723 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1724 {
1725 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1726 rela.r_addend = (h->root.u.def.value
1727 + h->root.u.def.section->output_section->vma
1728 + h->root.u.def.section->output_offset);
1729 }
1730 else
1731 {
1732 BFD_ASSERT((h->got.offset & 1) == 0);
1733 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1734 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
1735 rela.r_addend = 0;
1736 }
1737
1738 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1739 ((Elf64_External_Rela *) srela->contents
1740 + srela->reloc_count));
1741 ++srela->reloc_count;
1742 }
1743
70256ad8
AJ
1744 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1745 {
1746 asection *s;
1747 Elf_Internal_Rela rela;
1748
1749 /* This symbol needs a copy reloc. Set it up. */
1750
1751 BFD_ASSERT (h->dynindx != -1
1752 && (h->root.type == bfd_link_hash_defined
1753 || h->root.type == bfd_link_hash_defweak));
1754
1755 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1756 ".rela.bss");
1757 BFD_ASSERT (s != NULL);
1758
1759 rela.r_offset = (h->root.u.def.value
1760 + h->root.u.def.section->output_section->vma
1761 + h->root.u.def.section->output_offset);
1762 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
1763 rela.r_addend = 0;
1764 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1765 ((Elf64_External_Rela *) s->contents
1766 + s->reloc_count));
1767 ++s->reloc_count;
1768 }
1769
1770 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1771 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1772 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1773 sym->st_shndx = SHN_ABS;
1774
1775 return true;
1776}
1777
1778/* Finish up the dynamic sections. */
1779
1780static boolean
1781elf64_x86_64_finish_dynamic_sections (output_bfd, info)
1782 bfd *output_bfd;
1783 struct bfd_link_info *info;
1784{
1785 bfd *dynobj;
1786 asection *sdyn;
1787 asection *sgot;
1788
1789 dynobj = elf_hash_table (info)->dynobj;
1790
653165cc
AJ
1791 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1792 BFD_ASSERT (sgot != NULL);
70256ad8
AJ
1793 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1794
1795 if (elf_hash_table (info)->dynamic_sections_created)
1796 {
1797 asection *splt;
1798 Elf64_External_Dyn *dyncon, *dynconend;
1799
407443a3 1800 BFD_ASSERT (sdyn != NULL);
70256ad8
AJ
1801
1802 dyncon = (Elf64_External_Dyn *) sdyn->contents;
1803 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1804 for (; dyncon < dynconend; dyncon++)
1805 {
1806 Elf_Internal_Dyn dyn;
1807 const char *name;
1808 asection *s;
1809
1810 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
1811
1812 switch (dyn.d_tag)
1813 {
1814 default:
053579d7 1815 continue;
70256ad8
AJ
1816
1817 case DT_PLTGOT:
1818 name = ".got";
1819 goto get_vma;
1820
1821 case DT_JMPREL:
1822 name = ".rela.plt";
1823
1824 get_vma:
1825 s = bfd_get_section_by_name (output_bfd, name);
1826 BFD_ASSERT (s != NULL);
1827 dyn.d_un.d_ptr = s->vma;
1828 break;
1829
1830 case DT_RELASZ:
1831 /* FIXME: This comment and code is from elf64-alpha.c: */
1832 /* My interpretation of the TIS v1.1 ELF document indicates
407443a3 1833 that RELASZ should not include JMPREL. This is not what
70256ad8
AJ
1834 the rest of the BFD does. It is, however, what the
1835 glibc ld.so wants. Do this fixup here until we found
1836 out who is right. */
1837 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1838 if (s)
1839 {
407443a3 1840 /* Subtract JMPREL size from RELASZ. */
70256ad8
AJ
1841 dyn.d_un.d_val -=
1842 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
1843 }
1844 break;
1845
1846 case DT_PLTRELSZ:
1847 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1848 BFD_ASSERT (s != NULL);
1849 dyn.d_un.d_val =
1850 (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size);
1851 break;
1852 }
70256ad8
AJ
1853 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
1854 }
1855
1856 /* Initialize the contents of the .plt section. */
407443a3
AJ
1857 splt = bfd_get_section_by_name (dynobj, ".plt");
1858 BFD_ASSERT (splt != NULL);
70256ad8
AJ
1859 if (splt->_raw_size > 0)
1860 {
653165cc 1861 /* Fill in the first entry in the procedure linkage table. */
407443a3 1862 memcpy (splt->contents, elf64_x86_64_plt0_entry, PLT_ENTRY_SIZE);
653165cc
AJ
1863 /* Add offset for pushq GOT+8(%rip), since the instruction
1864 uses 6 bytes subtract this value. */
1865 bfd_put_32 (output_bfd,
1866 (sgot->output_section->vma
1867 + sgot->output_offset
1868 + 8
1869 - splt->output_section->vma
1870 - splt->output_offset
1871 - 6),
1872 splt->contents + 2);
1873 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
1874 the end of the instruction. */
1875 bfd_put_32 (output_bfd,
1876 (sgot->output_section->vma
1877 + sgot->output_offset
1878 + 16
1879 - splt->output_section->vma
1880 - splt->output_offset
1881 - 12),
1882 splt->contents + 8);
1883
70256ad8
AJ
1884 }
1885
1886 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1887 PLT_ENTRY_SIZE;
1888 }
1889
1890 /* Set the first entry in the global offset table to the address of
1891 the dynamic section. */
70256ad8
AJ
1892 if (sgot->_raw_size > 0)
1893 {
1894 if (sdyn == NULL)
1895 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
1896 else
1897 bfd_put_64 (output_bfd,
1898 sdyn->output_section->vma + sdyn->output_offset,
1899 sgot->contents);
653165cc 1900 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
70256ad8
AJ
1901 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE);
1902 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE*2);
1903 }
1904
1905 elf_section_data (sgot->output_section)->this_hdr.sh_entsize =
1906 GOT_ENTRY_SIZE;
1907
8d88c4ca
NC
1908 return true;
1909}
1910
70256ad8
AJ
1911#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
1912#define TARGET_LITTLE_NAME "elf64-x86-64"
1913#define ELF_ARCH bfd_arch_i386
1914#define ELF_MACHINE_CODE EM_X86_64
1915#define ELF_MAXPAGESIZE 0x100000
1916
1917#define elf_backend_can_gc_sections 1
1918#define elf_backend_want_got_plt 1
1919#define elf_backend_plt_readonly 1
1920#define elf_backend_want_plt_sym 0
1921#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
1922#define elf_backend_plt_header_size PLT_ENTRY_SIZE
1923
1924#define elf_info_to_howto elf64_x86_64_info_to_howto
70256ad8
AJ
1925
1926#define bfd_elf64_bfd_final_link _bfd_elf64_gc_common_final_link
1927#define bfd_elf64_bfd_link_hash_table_create \
1928 elf64_x86_64_link_hash_table_create
407443a3 1929#define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
70256ad8
AJ
1930
1931#define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
1932#define elf_backend_check_relocs elf64_x86_64_check_relocs
1933#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
1934#define elf_backend_finish_dynamic_sections \
1935 elf64_x86_64_finish_dynamic_sections
1936#define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
1937#define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
1938#define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
1939#define elf_backend_relocate_section elf64_x86_64_relocate_section
1940#define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
407443a3 1941#define elf_backend_object_p elf64_x86_64_elf_object_p
8d88c4ca
NC
1942
1943#include "elf64-target.h"