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