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