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