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