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