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