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