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