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