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