* nto-procfs.c (get_regset): Pass correct pointer.
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
8d88c4ca 1/* X86-64 specific support for 64-bit ELF
0ffa91dd 2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3eb128b2 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
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
ae9a127f 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
cd123cb7
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
8d88c4ca 22
8d88c4ca 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
c434dee6 25#include "bfdlink.h"
8d88c4ca
NC
26#include "libbfd.h"
27#include "elf-bfd.h"
142411ca 28#include "bfd_stdint.h"
8d88c4ca
NC
29
30#include "elf/x86-64.h"
31
8d88c4ca
NC
32/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33#define MINUS_ONE (~ (bfd_vma) 0)
34
35/* The relocation "howto" table. Order of fields:
7b81dfbb
AJ
36 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
37 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
70256ad8
AJ
38static reloc_howto_type x86_64_elf_howto_table[] =
39{
b34976b6
AM
40 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
41 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
42 FALSE),
43 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
44 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
45 FALSE),
46 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
47 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
48 TRUE),
49 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
50 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
51 FALSE),
52 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
53 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
54 TRUE),
55 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
57 FALSE),
58 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
60 MINUS_ONE, FALSE),
61 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
63 MINUS_ONE, FALSE),
64 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
65 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
66 MINUS_ONE, FALSE),
67 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
68 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
69 0xffffffff, TRUE),
70 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
71 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
72 FALSE),
73 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
74 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
75 FALSE),
76 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
b0360d8c 78 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
b34976b6 79 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
ac2aa337 80 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
b34976b6
AM
81 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
82 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
83 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
84 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
86 MINUS_ONE, FALSE),
87 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
89 MINUS_ONE, FALSE),
90 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
91 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
92 MINUS_ONE, FALSE),
93 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
94 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
95 0xffffffff, TRUE),
96 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
97 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
98 0xffffffff, TRUE),
ac2aa337 99 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
b34976b6
AM
100 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
101 0xffffffff, FALSE),
102 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
103 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
104 0xffffffff, TRUE),
105 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
106 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
107 0xffffffff, FALSE),
d6ab8113
JB
108 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
110 TRUE),
111 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
112 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
113 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
114 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
115 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
116 FALSE, 0xffffffff, 0xffffffff, TRUE),
7b81dfbb
AJ
117 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
118 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
119 FALSE),
120 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
121 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
122 MINUS_ONE, TRUE),
123 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
124 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
125 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
126 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
127 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
128 MINUS_ONE, FALSE),
129 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
130 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
131 MINUS_ONE, FALSE),
67a4f2b7
AO
132 EMPTY_HOWTO (32),
133 EMPTY_HOWTO (33),
134 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
135 complain_overflow_bitfield, bfd_elf_generic_reloc,
136 "R_X86_64_GOTPC32_TLSDESC",
137 FALSE, 0xffffffff, 0xffffffff, TRUE),
138 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
139 complain_overflow_dont, bfd_elf_generic_reloc,
140 "R_X86_64_TLSDESC_CALL",
141 FALSE, 0, 0, FALSE),
142 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
143 complain_overflow_bitfield, bfd_elf_generic_reloc,
144 "R_X86_64_TLSDESC",
145 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
fe4770f4 146
a33d77bc
JB
147 /* We have a gap in the reloc numbers here.
148 R_X86_64_standard counts the number up to this point, and
149 R_X86_64_vt_offset is the value to subtract from a reloc type of
150 R_X86_64_GNU_VT* to form an index into this table. */
67a4f2b7 151#define R_X86_64_standard (R_X86_64_TLSDESC + 1)
a33d77bc
JB
152#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
153
fe4770f4 154/* GNU extension to record C++ vtable hierarchy. */
b34976b6
AM
155 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
156 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
fe4770f4
AJ
157
158/* GNU extension to record C++ vtable member usage. */
b34976b6
AM
159 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
160 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
161 FALSE)
8d88c4ca
NC
162};
163
d8045f23
NC
164#define IS_X86_64_PCREL_TYPE(TYPE) \
165 ( ((TYPE) == R_X86_64_PC8) \
166 || ((TYPE) == R_X86_64_PC16) \
167 || ((TYPE) == R_X86_64_PC32) \
168 || ((TYPE) == R_X86_64_PC64))
169
8d88c4ca 170/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
171struct elf_reloc_map
172{
8d88c4ca
NC
173 bfd_reloc_code_real_type bfd_reloc_val;
174 unsigned char elf_reloc_val;
175};
176
dc810e39 177static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 178{
70256ad8
AJ
179 { BFD_RELOC_NONE, R_X86_64_NONE, },
180 { BFD_RELOC_64, R_X86_64_64, },
181 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
182 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
183 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
184 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
185 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
186 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
187 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
188 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
189 { BFD_RELOC_32, R_X86_64_32, },
190 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
191 { BFD_RELOC_16, R_X86_64_16, },
192 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
193 { BFD_RELOC_8, R_X86_64_8, },
194 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
195 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
196 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
197 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
198 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
199 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
200 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
201 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
202 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
d6ab8113
JB
203 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
204 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
205 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
7b81dfbb
AJ
206 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
207 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
208 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
209 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
210 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
67a4f2b7
AO
211 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
212 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
213 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
fe4770f4
AJ
214 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
215 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
216};
217
67a4f2b7
AO
218static reloc_howto_type *
219elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
220{
221 unsigned i;
222
223 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
224 || r_type >= (unsigned int) R_X86_64_max)
225 {
226 if (r_type >= (unsigned int) R_X86_64_standard)
227 {
228 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
229 abfd, (int) r_type);
230 r_type = R_X86_64_NONE;
231 }
232 i = r_type;
233 }
234 else
235 i = r_type - (unsigned int) R_X86_64_vt_offset;
236 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
237 return &x86_64_elf_howto_table[i];
238}
8d88c4ca
NC
239
240/* Given a BFD reloc type, return a HOWTO structure. */
241static reloc_howto_type *
67a4f2b7 242elf64_x86_64_reloc_type_lookup (bfd *abfd,
27482721 243 bfd_reloc_code_real_type code)
8d88c4ca
NC
244{
245 unsigned int i;
27482721 246
8d88c4ca
NC
247 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
248 i++)
249 {
250 if (x86_64_reloc_map[i].bfd_reloc_val == code)
67a4f2b7
AO
251 return elf64_x86_64_rtype_to_howto (abfd,
252 x86_64_reloc_map[i].elf_reloc_val);
8d88c4ca
NC
253 }
254 return 0;
255}
256
157090f7
AM
257static reloc_howto_type *
258elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
259 const char *r_name)
260{
261 unsigned int i;
262
263 for (i = 0;
264 i < (sizeof (x86_64_elf_howto_table)
265 / sizeof (x86_64_elf_howto_table[0]));
266 i++)
267 if (x86_64_elf_howto_table[i].name != NULL
268 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
269 return &x86_64_elf_howto_table[i];
270
271 return NULL;
272}
273
8d88c4ca 274/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 275
8d88c4ca 276static void
27482721
AJ
277elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
278 Elf_Internal_Rela *dst)
8d88c4ca 279{
67a4f2b7 280 unsigned r_type;
8d88c4ca
NC
281
282 r_type = ELF64_R_TYPE (dst->r_info);
67a4f2b7 283 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
8d88c4ca
NC
284 BFD_ASSERT (r_type == cache_ptr->howto->type);
285}
70256ad8 286\f
3bab7989 287/* Support for core dump NOTE sections. */
b34976b6 288static bfd_boolean
27482721 289elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
290{
291 int offset;
eea6121a 292 size_t size;
3bab7989
ML
293
294 switch (note->descsz)
295 {
296 default:
b34976b6 297 return FALSE;
3bab7989
ML
298
299 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
300 /* pr_cursig */
cedb70c5 301 elf_tdata (abfd)->core_signal
3bab7989
ML
302 = bfd_get_16 (abfd, note->descdata + 12);
303
304 /* pr_pid */
cedb70c5 305 elf_tdata (abfd)->core_pid
3bab7989
ML
306 = bfd_get_32 (abfd, note->descdata + 32);
307
308 /* pr_reg */
309 offset = 112;
eea6121a 310 size = 216;
3bab7989
ML
311
312 break;
313 }
314
315 /* Make a ".reg/999" section. */
316 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 317 size, note->descpos + offset);
3bab7989
ML
318}
319
b34976b6 320static bfd_boolean
27482721 321elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
322{
323 switch (note->descsz)
324 {
325 default:
b34976b6 326 return FALSE;
3bab7989
ML
327
328 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
329 elf_tdata (abfd)->core_program
330 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
331 elf_tdata (abfd)->core_command
332 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
333 }
334
335 /* Note that for some reason, a spurious space is tacked
336 onto the end of the args in some (at least one anyway)
337 implementations, so strip it off if it exists. */
338
339 {
340 char *command = elf_tdata (abfd)->core_command;
341 int n = strlen (command);
342
343 if (0 < n && command[n - 1] == ' ')
344 command[n - 1] = '\0';
345 }
346
b34976b6 347 return TRUE;
3bab7989
ML
348}
349\f
407443a3 350/* Functions for the x86-64 ELF linker. */
70256ad8 351
407443a3 352/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
353 section. */
354
407443a3 355#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
70256ad8 356
d40d037c
AJ
357/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
358 copying dynamic variables from a shared lib into an app's dynbss
359 section, and instead use a dynamic relocation to point into the
360 shared lib. */
361#define ELIMINATE_COPY_RELOCS 1
362
70256ad8
AJ
363/* The size in bytes of an entry in the global offset table. */
364
365#define GOT_ENTRY_SIZE 8
8d88c4ca 366
70256ad8 367/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 368
70256ad8
AJ
369#define PLT_ENTRY_SIZE 16
370
371/* The first entry in a procedure linkage table looks like this. See the
372 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
373
374static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
375{
653165cc
AJ
376 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
377 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
10efb593 378 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
70256ad8
AJ
379};
380
381/* Subsequent entries in a procedure linkage table look like this. */
382
383static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
384{
653165cc 385 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 386 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 387 0x68, /* pushq immediate */
70256ad8
AJ
388 0, 0, 0, 0, /* replaced with index into relocation table. */
389 0xe9, /* jmp relative */
390 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
391};
392
393/* The x86-64 linker needs to keep track of the number of relocs that
985142a4 394 it decides to copy as dynamic relocs in check_relocs for each symbol.
c434dee6
AJ
395 This is so that it can later discard them if they are found to be
396 unnecessary. We store the information in a field extending the
397 regular ELF linker hash table. */
70256ad8 398
c434dee6 399struct elf64_x86_64_dyn_relocs
70256ad8
AJ
400{
401 /* Next section. */
c434dee6
AJ
402 struct elf64_x86_64_dyn_relocs *next;
403
404 /* The input section of the reloc. */
405 asection *sec;
406
407 /* Total number of relocs copied for the input section. */
70256ad8 408 bfd_size_type count;
c434dee6
AJ
409
410 /* Number of pc-relative relocs copied for the input section. */
411 bfd_size_type pc_count;
70256ad8
AJ
412};
413
414/* x86-64 ELF linker hash entry. */
415
416struct elf64_x86_64_link_hash_entry
417{
c434dee6 418 struct elf_link_hash_entry elf;
70256ad8 419
c434dee6
AJ
420 /* Track dynamic relocs copied for this symbol. */
421 struct elf64_x86_64_dyn_relocs *dyn_relocs;
bffbf940
JJ
422
423#define GOT_UNKNOWN 0
424#define GOT_NORMAL 1
425#define GOT_TLS_GD 2
426#define GOT_TLS_IE 3
67a4f2b7
AO
427#define GOT_TLS_GDESC 4
428#define GOT_TLS_GD_BOTH_P(type) \
429 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
430#define GOT_TLS_GD_P(type) \
431 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
432#define GOT_TLS_GDESC_P(type) \
433 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
434#define GOT_TLS_GD_ANY_P(type) \
435 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
bffbf940 436 unsigned char tls_type;
67a4f2b7
AO
437
438 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
439 starting at the end of the jump table. */
440 bfd_vma tlsdesc_got;
bffbf940
JJ
441};
442
443#define elf64_x86_64_hash_entry(ent) \
444 ((struct elf64_x86_64_link_hash_entry *)(ent))
445
446struct elf64_x86_64_obj_tdata
447{
448 struct elf_obj_tdata root;
449
450 /* tls_type for each local got entry. */
451 char *local_got_tls_type;
67a4f2b7
AO
452
453 /* GOTPLT entries for TLS descriptors. */
454 bfd_vma *local_tlsdesc_gotent;
70256ad8
AJ
455};
456
bffbf940
JJ
457#define elf64_x86_64_tdata(abfd) \
458 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
459
460#define elf64_x86_64_local_got_tls_type(abfd) \
461 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
462
67a4f2b7
AO
463#define elf64_x86_64_local_tlsdesc_gotent(abfd) \
464 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
bffbf940 465
0ffa91dd
NC
466#define is_x86_64_elf(bfd) \
467 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
468 && elf_tdata (bfd) != NULL \
469 && elf_object_id (bfd) == X86_64_ELF_TDATA)
470
471static bfd_boolean
472elf64_x86_64_mkobject (bfd *abfd)
473{
474 return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata),
475 X86_64_ELF_TDATA);
476}
477
c434dee6 478/* x86-64 ELF linker hash table. */
8d88c4ca 479
407443a3
AJ
480struct elf64_x86_64_link_hash_table
481{
c434dee6 482 struct elf_link_hash_table elf;
70256ad8 483
c434dee6
AJ
484 /* Short-cuts to get to dynamic linker sections. */
485 asection *sgot;
486 asection *sgotplt;
487 asection *srelgot;
488 asection *splt;
489 asection *srelplt;
490 asection *sdynbss;
491 asection *srelbss;
70256ad8 492
67a4f2b7
AO
493 /* The offset into splt of the PLT entry for the TLS descriptor
494 resolver. Special values are 0, if not necessary (or not found
495 to be necessary yet), and -1 if needed but not determined
496 yet. */
497 bfd_vma tlsdesc_plt;
498 /* The offset into sgot of the GOT entry used by the PLT entry
499 above. */
500 bfd_vma tlsdesc_got;
501
bffbf940
JJ
502 union {
503 bfd_signed_vma refcount;
504 bfd_vma offset;
505 } tls_ld_got;
506
67a4f2b7
AO
507 /* The amount of space used by the jump slots in the GOT. */
508 bfd_vma sgotplt_jump_table_size;
509
c434dee6
AJ
510 /* Small local sym to section mapping cache. */
511 struct sym_sec_cache sym_sec;
9f03412a
AO
512
513 /* _TLS_MODULE_BASE_ symbol. */
514 struct bfd_link_hash_entry *tls_module_base;
c434dee6 515};
70256ad8
AJ
516
517/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca
NC
518
519#define elf64_x86_64_hash_table(p) \
520 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
521
67a4f2b7
AO
522#define elf64_x86_64_compute_jump_table_size(htab) \
523 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
524
407443a3 525/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
526
527static struct bfd_hash_entry *
27482721
AJ
528link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
529 const char *string)
70256ad8 530{
70256ad8 531 /* Allocate the structure if it has not already been allocated by a
c434dee6
AJ
532 subclass. */
533 if (entry == NULL)
534 {
535 entry = bfd_hash_allocate (table,
536 sizeof (struct elf64_x86_64_link_hash_entry));
537 if (entry == NULL)
538 return entry;
539 }
70256ad8
AJ
540
541 /* Call the allocation method of the superclass. */
c434dee6
AJ
542 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
543 if (entry != NULL)
70256ad8 544 {
c434dee6
AJ
545 struct elf64_x86_64_link_hash_entry *eh;
546
547 eh = (struct elf64_x86_64_link_hash_entry *) entry;
548 eh->dyn_relocs = NULL;
bffbf940 549 eh->tls_type = GOT_UNKNOWN;
67a4f2b7 550 eh->tlsdesc_got = (bfd_vma) -1;
70256ad8
AJ
551 }
552
c434dee6 553 return entry;
70256ad8
AJ
554}
555
8d88c4ca
NC
556/* Create an X86-64 ELF linker hash table. */
557
558static struct bfd_link_hash_table *
27482721 559elf64_x86_64_link_hash_table_create (bfd *abfd)
8d88c4ca
NC
560{
561 struct elf64_x86_64_link_hash_table *ret;
dc810e39 562 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
8d88c4ca 563
e2d34d7d 564 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
c434dee6 565 if (ret == NULL)
8d88c4ca
NC
566 return NULL;
567
66eb6687
AM
568 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
569 sizeof (struct elf64_x86_64_link_hash_entry)))
8d88c4ca 570 {
e2d34d7d 571 free (ret);
8d88c4ca
NC
572 return NULL;
573 }
574
c434dee6
AJ
575 ret->sgot = NULL;
576 ret->sgotplt = NULL;
577 ret->srelgot = NULL;
578 ret->splt = NULL;
579 ret->srelplt = NULL;
580 ret->sdynbss = NULL;
581 ret->srelbss = NULL;
582 ret->sym_sec.abfd = NULL;
67a4f2b7
AO
583 ret->tlsdesc_plt = 0;
584 ret->tlsdesc_got = 0;
bffbf940 585 ret->tls_ld_got.refcount = 0;
67a4f2b7 586 ret->sgotplt_jump_table_size = 0;
9f03412a 587 ret->tls_module_base = NULL;
c434dee6
AJ
588
589 return &ret->elf.root;
590}
591
592/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
593 shortcuts to them in our hash table. */
594
b34976b6 595static bfd_boolean
27482721 596create_got_section (bfd *dynobj, struct bfd_link_info *info)
c434dee6
AJ
597{
598 struct elf64_x86_64_link_hash_table *htab;
599
600 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 601 return FALSE;
c434dee6
AJ
602
603 htab = elf64_x86_64_hash_table (info);
604 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
605 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
606 if (!htab->sgot || !htab->sgotplt)
607 abort ();
608
3496cb2a
L
609 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
610 (SEC_ALLOC | SEC_LOAD
611 | SEC_HAS_CONTENTS
612 | SEC_IN_MEMORY
613 | SEC_LINKER_CREATED
614 | SEC_READONLY));
c434dee6 615 if (htab->srelgot == NULL
c434dee6 616 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
b34976b6
AM
617 return FALSE;
618 return TRUE;
c434dee6
AJ
619}
620
621/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
622 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
623 hash table. */
624
b34976b6 625static bfd_boolean
27482721 626elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
c434dee6
AJ
627{
628 struct elf64_x86_64_link_hash_table *htab;
629
630 htab = elf64_x86_64_hash_table (info);
631 if (!htab->sgot && !create_got_section (dynobj, info))
b34976b6 632 return FALSE;
c434dee6
AJ
633
634 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 635 return FALSE;
c434dee6
AJ
636
637 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
638 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
639 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
640 if (!info->shared)
641 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
642
643 if (!htab->splt || !htab->srelplt || !htab->sdynbss
644 || (!info->shared && !htab->srelbss))
645 abort ();
646
b34976b6 647 return TRUE;
c434dee6
AJ
648}
649
650/* Copy the extra info we tack onto an elf_link_hash_entry. */
651
652static void
fcfa13d2 653elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
27482721
AJ
654 struct elf_link_hash_entry *dir,
655 struct elf_link_hash_entry *ind)
c434dee6
AJ
656{
657 struct elf64_x86_64_link_hash_entry *edir, *eind;
658
659 edir = (struct elf64_x86_64_link_hash_entry *) dir;
660 eind = (struct elf64_x86_64_link_hash_entry *) ind;
661
662 if (eind->dyn_relocs != NULL)
663 {
664 if (edir->dyn_relocs != NULL)
665 {
666 struct elf64_x86_64_dyn_relocs **pp;
667 struct elf64_x86_64_dyn_relocs *p;
668
fcfa13d2 669 /* Add reloc counts against the indirect sym to the direct sym
c434dee6
AJ
670 list. Merge any entries against the same section. */
671 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
672 {
673 struct elf64_x86_64_dyn_relocs *q;
674
675 for (q = edir->dyn_relocs; q != NULL; q = q->next)
676 if (q->sec == p->sec)
677 {
678 q->pc_count += p->pc_count;
679 q->count += p->count;
680 *pp = p->next;
681 break;
682 }
683 if (q == NULL)
684 pp = &p->next;
685 }
686 *pp = edir->dyn_relocs;
687 }
688
689 edir->dyn_relocs = eind->dyn_relocs;
690 eind->dyn_relocs = NULL;
691 }
692
bffbf940
JJ
693 if (ind->root.type == bfd_link_hash_indirect
694 && dir->got.refcount <= 0)
695 {
696 edir->tls_type = eind->tls_type;
697 eind->tls_type = GOT_UNKNOWN;
698 }
699
d40d037c
AJ
700 if (ELIMINATE_COPY_RELOCS
701 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
702 && dir->dynamic_adjusted)
703 {
704 /* If called to transfer flags for a weakdef during processing
705 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
706 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
707 dir->ref_dynamic |= ind->ref_dynamic;
708 dir->ref_regular |= ind->ref_regular;
709 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
710 dir->needs_plt |= ind->needs_plt;
711 dir->pointer_equality_needed |= ind->pointer_equality_needed;
712 }
d40d037c 713 else
fcfa13d2 714 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
8d88c4ca
NC
715}
716
b34976b6 717static bfd_boolean
27482721 718elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 719{
8d88c4ca
NC
720 /* Set the right machine number for an x86-64 elf64 file. */
721 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
b34976b6 722 return TRUE;
8d88c4ca
NC
723}
724
142411ca
L
725typedef union
726 {
727 unsigned char c[2];
728 uint16_t i;
729 }
730x86_64_opcode16;
731
732typedef union
733 {
734 unsigned char c[4];
735 uint32_t i;
736 }
737x86_64_opcode32;
738
739/* Return TRUE if the TLS access code sequence support transition
740 from R_TYPE. */
741
742static bfd_boolean
743elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
744 bfd_byte *contents,
745 Elf_Internal_Shdr *symtab_hdr,
746 struct elf_link_hash_entry **sym_hashes,
747 unsigned int r_type,
748 const Elf_Internal_Rela *rel,
749 const Elf_Internal_Rela *relend)
bffbf940 750{
142411ca
L
751 unsigned int val;
752 unsigned long r_symndx;
753 struct elf_link_hash_entry *h;
754 bfd_vma offset;
755
756 /* Get the section contents. */
757 if (contents == NULL)
758 {
759 if (elf_section_data (sec)->this_hdr.contents != NULL)
760 contents = elf_section_data (sec)->this_hdr.contents;
761 else
762 {
763 /* FIXME: How to better handle error condition? */
764 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
765 return FALSE;
bffbf940 766
142411ca
L
767 /* Cache the section contents for elf_link_input_bfd. */
768 elf_section_data (sec)->this_hdr.contents = contents;
769 }
770 }
771
772 offset = rel->r_offset;
bffbf940 773 switch (r_type)
142411ca
L
774 {
775 case R_X86_64_TLSGD:
776 case R_X86_64_TLSLD:
777 if ((rel + 1) >= relend)
778 return FALSE;
779
780 if (r_type == R_X86_64_TLSGD)
781 {
782 /* Check transition from GD access model. Only
783 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
784 .word 0x6666; rex64; call __tls_get_addr
785 can transit to different access model. */
786
787 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
788 call = { { 0x66, 0x66, 0x48, 0xe8 } };
789 if (offset < 4
790 || (offset + 12) > sec->size
791 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
792 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
793 return FALSE;
794 }
795 else
796 {
797 /* Check transition from LD access model. Only
798 leaq foo@tlsld(%rip), %rdi;
799 call __tls_get_addr
800 can transit to different access model. */
801
802 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
803 x86_64_opcode32 op;
804
805 if (offset < 3 || (offset + 9) > sec->size)
806 return FALSE;
807
808 op.i = bfd_get_32 (abfd, contents + offset - 3);
809 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
810 if (op.i != ld.i)
811 return FALSE;
812 }
813
814 r_symndx = ELF64_R_SYM (rel[1].r_info);
815 if (r_symndx < symtab_hdr->sh_info)
816 return FALSE;
817
818 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
c4fb387b
L
819 /* Use strncmp to check __tls_get_addr since __tls_get_addr
820 may be versioned. */
142411ca
L
821 return (h != NULL
822 && h->root.root.string != NULL
823 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
824 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
c4fb387b
L
825 && (strncmp (h->root.root.string,
826 "__tls_get_addr", 14) == 0));
142411ca
L
827
828 case R_X86_64_GOTTPOFF:
829 /* Check transition from IE access model:
830 movq foo@gottpoff(%rip), %reg
831 addq foo@gottpoff(%rip), %reg
832 */
833
834 if (offset < 3 || (offset + 4) > sec->size)
835 return FALSE;
836
837 val = bfd_get_8 (abfd, contents + offset - 3);
838 if (val != 0x48 && val != 0x4c)
839 return FALSE;
840
841 val = bfd_get_8 (abfd, contents + offset - 2);
842 if (val != 0x8b && val != 0x03)
843 return FALSE;
844
845 val = bfd_get_8 (abfd, contents + offset - 1);
846 return (val & 0xc7) == 5;
847
848 case R_X86_64_GOTPC32_TLSDESC:
849 /* Check transition from GDesc access model:
850 leaq x@tlsdesc(%rip), %rax
851
852 Make sure it's a leaq adding rip to a 32-bit offset
853 into any register, although it's probably almost always
854 going to be rax. */
855
856 if (offset < 3 || (offset + 4) > sec->size)
857 return FALSE;
858
859 val = bfd_get_8 (abfd, contents + offset - 3);
860 if ((val & 0xfb) != 0x48)
861 return FALSE;
862
863 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
864 return FALSE;
865
866 val = bfd_get_8 (abfd, contents + offset - 1);
867 return (val & 0xc7) == 0x05;
868
869 case R_X86_64_TLSDESC_CALL:
870 /* Check transition from GDesc access model:
871 call *x@tlsdesc(%rax)
872 */
873 if (offset + 2 <= sec->size)
874 {
875 /* Make sure that it's a call *x@tlsdesc(%rax). */
876 static x86_64_opcode16 call = { { 0xff, 0x10 } };
877 return bfd_get_16 (abfd, contents + offset) == call.i;
878 }
879
880 return FALSE;
881
882 default:
883 abort ();
884 }
885}
886
887/* Return TRUE if the TLS access transition is OK or no transition
888 will be performed. Update R_TYPE if there is a transition. */
889
890static bfd_boolean
891elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
892 asection *sec, bfd_byte *contents,
893 Elf_Internal_Shdr *symtab_hdr,
894 struct elf_link_hash_entry **sym_hashes,
895 unsigned int *r_type, int tls_type,
896 const Elf_Internal_Rela *rel,
897 const Elf_Internal_Rela *relend,
898 struct elf_link_hash_entry *h)
899{
900 unsigned int from_type = *r_type;
901 unsigned int to_type = from_type;
902 bfd_boolean check = TRUE;
903
904 switch (from_type)
bffbf940
JJ
905 {
906 case R_X86_64_TLSGD:
67a4f2b7
AO
907 case R_X86_64_GOTPC32_TLSDESC:
908 case R_X86_64_TLSDESC_CALL:
bffbf940 909 case R_X86_64_GOTTPOFF:
142411ca
L
910 if (!info->shared)
911 {
912 if (h == NULL)
913 to_type = R_X86_64_TPOFF32;
914 else
915 to_type = R_X86_64_GOTTPOFF;
916 }
917
918 /* When we are called from elf64_x86_64_relocate_section,
919 CONTENTS isn't NULL and there may be additional transitions
920 based on TLS_TYPE. */
921 if (contents != NULL)
922 {
923 unsigned int new_to_type = to_type;
924
925 if (!info->shared
926 && h != NULL
927 && h->dynindx == -1
928 && tls_type == GOT_TLS_IE)
929 new_to_type = R_X86_64_TPOFF32;
930
931 if (to_type == R_X86_64_TLSGD
932 || to_type == R_X86_64_GOTPC32_TLSDESC
933 || to_type == R_X86_64_TLSDESC_CALL)
934 {
935 if (tls_type == GOT_TLS_IE)
936 new_to_type = R_X86_64_GOTTPOFF;
937 }
938
939 /* We checked the transition before when we were called from
940 elf64_x86_64_check_relocs. We only want to check the new
941 transition which hasn't been checked before. */
942 check = new_to_type != to_type && from_type == to_type;
943 to_type = new_to_type;
944 }
945
946 break;
947
bffbf940 948 case R_X86_64_TLSLD:
142411ca
L
949 if (!info->shared)
950 to_type = R_X86_64_TPOFF32;
951 break;
952
953 default:
954 return TRUE;
bffbf940
JJ
955 }
956
142411ca
L
957 /* Return TRUE if there is no transition. */
958 if (from_type == to_type)
959 return TRUE;
960
961 /* Check if the transition can be performed. */
962 if (check
963 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
964 symtab_hdr, sym_hashes,
965 from_type, rel, relend))
966 {
2f629d23 967 reloc_howto_type *from, *to;
142411ca
L
968
969 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
970 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
971
972 (*_bfd_error_handler)
973 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
974 "in section `%A' failed"),
975 abfd, sec, from->name, to->name,
976 h ? h->root.root.string : "a local symbol",
977 (unsigned long) rel->r_offset);
978 bfd_set_error (bfd_error_bad_value);
979 return FALSE;
980 }
981
982 *r_type = to_type;
983 return TRUE;
bffbf940
JJ
984}
985
70256ad8 986/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
987 calculate needed space in the global offset table, procedure
988 linkage table, and dynamic reloc sections. */
70256ad8 989
b34976b6 990static bfd_boolean
142411ca
L
991elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
992 asection *sec,
27482721 993 const Elf_Internal_Rela *relocs)
70256ad8 994{
c434dee6 995 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
996 Elf_Internal_Shdr *symtab_hdr;
997 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
998 const Elf_Internal_Rela *rel;
999 const Elf_Internal_Rela *rel_end;
70256ad8
AJ
1000 asection *sreloc;
1001
1049f94e 1002 if (info->relocatable)
b34976b6 1003 return TRUE;
70256ad8 1004
0ffa91dd
NC
1005 BFD_ASSERT (is_x86_64_elf (abfd));
1006
c434dee6 1007 htab = elf64_x86_64_hash_table (info);
0ffa91dd 1008 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8 1009 sym_hashes = elf_sym_hashes (abfd);
70256ad8 1010
c434dee6 1011 sreloc = NULL;
d8045f23 1012
70256ad8
AJ
1013 rel_end = relocs + sec->reloc_count;
1014 for (rel = relocs; rel < rel_end; rel++)
1015 {
bffbf940 1016 unsigned int r_type;
70256ad8
AJ
1017 unsigned long r_symndx;
1018 struct elf_link_hash_entry *h;
1019
1020 r_symndx = ELF64_R_SYM (rel->r_info);
bffbf940 1021 r_type = ELF64_R_TYPE (rel->r_info);
c434dee6
AJ
1022
1023 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1024 {
d003868e
AM
1025 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1026 abfd, r_symndx);
b34976b6 1027 return FALSE;
c434dee6
AJ
1028 }
1029
70256ad8
AJ
1030 if (r_symndx < symtab_hdr->sh_info)
1031 h = NULL;
1032 else
71cb9464
L
1033 {
1034 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1035 while (h->root.type == bfd_link_hash_indirect
1036 || h->root.type == bfd_link_hash_warning)
1037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1038 }
70256ad8 1039
142411ca
L
1040 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1041 symtab_hdr, sym_hashes,
1042 &r_type, GOT_UNKNOWN,
1043 rel, rel_end, h))
1044 return FALSE;
1045
bffbf940 1046 switch (r_type)
70256ad8 1047 {
bffbf940
JJ
1048 case R_X86_64_TLSLD:
1049 htab->tls_ld_got.refcount += 1;
1050 goto create_got;
1051
1052 case R_X86_64_TPOFF32:
1053 if (info->shared)
70256ad8 1054 {
bffbf940 1055 (*_bfd_error_handler)
d003868e
AM
1056 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1057 abfd,
6610a52d
L
1058 x86_64_elf_howto_table[r_type].name,
1059 (h) ? h->root.root.string : "a local symbol");
bffbf940 1060 bfd_set_error (bfd_error_bad_value);
b34976b6 1061 return FALSE;
70256ad8 1062 }
bffbf940 1063 break;
c434dee6 1064
bffbf940
JJ
1065 case R_X86_64_GOTTPOFF:
1066 if (info->shared)
1067 info->flags |= DF_STATIC_TLS;
1068 /* Fall through */
70256ad8 1069
bffbf940
JJ
1070 case R_X86_64_GOT32:
1071 case R_X86_64_GOTPCREL:
1072 case R_X86_64_TLSGD:
7b81dfbb
AJ
1073 case R_X86_64_GOT64:
1074 case R_X86_64_GOTPCREL64:
1075 case R_X86_64_GOTPLT64:
67a4f2b7
AO
1076 case R_X86_64_GOTPC32_TLSDESC:
1077 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
1078 /* This symbol requires a global offset table entry. */
1079 {
1080 int tls_type, old_tls_type;
1081
1082 switch (r_type)
1083 {
1084 default: tls_type = GOT_NORMAL; break;
1085 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1086 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
67a4f2b7
AO
1087 case R_X86_64_GOTPC32_TLSDESC:
1088 case R_X86_64_TLSDESC_CALL:
1089 tls_type = GOT_TLS_GDESC; break;
bffbf940
JJ
1090 }
1091
1092 if (h != NULL)
1093 {
7b81dfbb
AJ
1094 if (r_type == R_X86_64_GOTPLT64)
1095 {
1096 /* This relocation indicates that we also need
1097 a PLT entry, as this is a function. We don't need
1098 a PLT entry for local symbols. */
1099 h->needs_plt = 1;
1100 h->plt.refcount += 1;
1101 }
bffbf940
JJ
1102 h->got.refcount += 1;
1103 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1104 }
1105 else
1106 {
1107 bfd_signed_vma *local_got_refcounts;
1108
1109 /* This is a global offset table entry for a local symbol. */
1110 local_got_refcounts = elf_local_got_refcounts (abfd);
1111 if (local_got_refcounts == NULL)
1112 {
1113 bfd_size_type size;
1114
1115 size = symtab_hdr->sh_info;
67a4f2b7
AO
1116 size *= sizeof (bfd_signed_vma)
1117 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
1118 local_got_refcounts = ((bfd_signed_vma *)
1119 bfd_zalloc (abfd, size));
1120 if (local_got_refcounts == NULL)
b34976b6 1121 return FALSE;
bffbf940 1122 elf_local_got_refcounts (abfd) = local_got_refcounts;
67a4f2b7
AO
1123 elf64_x86_64_local_tlsdesc_gotent (abfd)
1124 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
bffbf940 1125 elf64_x86_64_local_got_tls_type (abfd)
67a4f2b7 1126 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940
JJ
1127 }
1128 local_got_refcounts[r_symndx] += 1;
1129 old_tls_type
1130 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1131 }
1132
1133 /* If a TLS symbol is accessed using IE at least once,
1134 there is no point to use dynamic model for it. */
1135 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
1136 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1137 || tls_type != GOT_TLS_IE))
bffbf940 1138 {
67a4f2b7 1139 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 1140 tls_type = old_tls_type;
67a4f2b7
AO
1141 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1142 && GOT_TLS_GD_ANY_P (tls_type))
1143 tls_type |= old_tls_type;
bffbf940
JJ
1144 else
1145 {
1146 (*_bfd_error_handler)
1f7a4e42 1147 (_("%B: '%s' accessed both as normal and thread local symbol"),
d003868e 1148 abfd, h ? h->root.root.string : "<local>");
b34976b6 1149 return FALSE;
bffbf940
JJ
1150 }
1151 }
1152
1153 if (old_tls_type != tls_type)
1154 {
1155 if (h != NULL)
1156 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1157 else
1158 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1159 }
1160 }
c434dee6
AJ
1161 /* Fall through */
1162
d6ab8113
JB
1163 case R_X86_64_GOTOFF64:
1164 case R_X86_64_GOTPC32:
7b81dfbb 1165 case R_X86_64_GOTPC64:
bffbf940 1166 create_got:
c434dee6
AJ
1167 if (htab->sgot == NULL)
1168 {
1169 if (htab->elf.dynobj == NULL)
1170 htab->elf.dynobj = abfd;
1171 if (!create_got_section (htab->elf.dynobj, info))
b34976b6 1172 return FALSE;
c434dee6 1173 }
70256ad8
AJ
1174 break;
1175
1176 case R_X86_64_PLT32:
1177 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
1178 actually build the entry in adjust_dynamic_symbol,
1179 because this might be a case of linking PIC code which is
1180 never referenced by a dynamic object, in which case we
1181 don't need to generate a procedure linkage table entry
1182 after all. */
70256ad8
AJ
1183
1184 /* If this is a local symbol, we resolve it directly without
407443a3 1185 creating a procedure linkage table entry. */
70256ad8
AJ
1186 if (h == NULL)
1187 continue;
1188
f5385ebf 1189 h->needs_plt = 1;
51b64d56 1190 h->plt.refcount += 1;
70256ad8
AJ
1191 break;
1192
7b81dfbb
AJ
1193 case R_X86_64_PLTOFF64:
1194 /* This tries to form the 'address' of a function relative
1195 to GOT. For global symbols we need a PLT entry. */
1196 if (h != NULL)
1197 {
1198 h->needs_plt = 1;
1199 h->plt.refcount += 1;
1200 }
1201 goto create_got;
1202
cc78d0af
AJ
1203 case R_X86_64_8:
1204 case R_X86_64_16:
70256ad8
AJ
1205 case R_X86_64_32:
1206 case R_X86_64_32S:
1b71fb54
AJ
1207 /* Let's help debug shared library creation. These relocs
1208 cannot be used in shared libs. Don't error out for
1209 sections we don't care about, such as debug sections or
1210 non-constant sections. */
1211 if (info->shared
1212 && (sec->flags & SEC_ALLOC) != 0
1213 && (sec->flags & SEC_READONLY) != 0)
1214 {
1215 (*_bfd_error_handler)
d003868e
AM
1216 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1217 abfd,
6610a52d
L
1218 x86_64_elf_howto_table[r_type].name,
1219 (h) ? h->root.root.string : "a local symbol");
1b71fb54 1220 bfd_set_error (bfd_error_bad_value);
b34976b6 1221 return FALSE;
1b71fb54
AJ
1222 }
1223 /* Fall through. */
1224
c434dee6
AJ
1225 case R_X86_64_PC8:
1226 case R_X86_64_PC16:
70256ad8 1227 case R_X86_64_PC32:
d6ab8113 1228 case R_X86_64_PC64:
1b71fb54 1229 case R_X86_64_64:
c434dee6
AJ
1230 if (h != NULL && !info->shared)
1231 {
1232 /* If this reloc is in a read-only section, we might
1233 need a copy reloc. We can't check reliably at this
1234 stage whether the section is read-only, as input
1235 sections have not yet been mapped to output sections.
1236 Tentatively set the flag for now, and correct in
1237 adjust_dynamic_symbol. */
f5385ebf 1238 h->non_got_ref = 1;
c434dee6
AJ
1239
1240 /* We may need a .plt entry if the function this reloc
1241 refers to is in a shared lib. */
1242 h->plt.refcount += 1;
d6ab8113 1243 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
f5385ebf 1244 h->pointer_equality_needed = 1;
c434dee6 1245 }
70256ad8
AJ
1246
1247 /* If we are creating a shared library, and this is a reloc
1248 against a global symbol, or a non PC relative reloc
1249 against a local symbol, then we need to copy the reloc
1250 into the shared library. However, if we are linking with
1251 -Bsymbolic, we do not need to copy a reloc against a
1252 global symbol which is defined in an object we are
407443a3 1253 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
1254 this point we have not seen all the input files, so it is
1255 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
1256 later (it is never cleared). In case of a weak definition,
1257 DEF_REGULAR may be cleared later by a strong definition in
1258 a shared library. We account for that possibility below by
1259 storing information in the relocs_copied field of the hash
1260 table entry. A similar situation occurs when creating
1261 shared libraries and symbol visibility changes render the
1262 symbol local.
1263
1264 If on the other hand, we are creating an executable, we
1265 may need to keep relocations for symbols satisfied by a
1266 dynamic library if we manage to avoid copy relocs for the
0f88be7a 1267 symbol. */
c434dee6
AJ
1268 if ((info->shared
1269 && (sec->flags & SEC_ALLOC) != 0
d8045f23 1270 && (! IS_X86_64_PCREL_TYPE (r_type)
c434dee6 1271 || (h != NULL
55255dae 1272 && (! SYMBOLIC_BIND (info, h)
c434dee6 1273 || h->root.type == bfd_link_hash_defweak
f5385ebf 1274 || !h->def_regular))))
d40d037c
AJ
1275 || (ELIMINATE_COPY_RELOCS
1276 && !info->shared
c434dee6
AJ
1277 && (sec->flags & SEC_ALLOC) != 0
1278 && h != NULL
1279 && (h->root.type == bfd_link_hash_defweak
0f88be7a 1280 || !h->def_regular)))
70256ad8 1281 {
c434dee6
AJ
1282 struct elf64_x86_64_dyn_relocs *p;
1283 struct elf64_x86_64_dyn_relocs **head;
1284
1285 /* We must copy these reloc types into the output file.
1286 Create a reloc section in dynobj and make room for
1287 this reloc. */
70256ad8
AJ
1288 if (sreloc == NULL)
1289 {
c434dee6
AJ
1290 if (htab->elf.dynobj == NULL)
1291 htab->elf.dynobj = abfd;
1292
83bac4b0
NC
1293 sreloc = _bfd_elf_make_dynamic_reloc_section
1294 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
70256ad8 1295
70256ad8 1296 if (sreloc == NULL)
83bac4b0 1297 return FALSE;
d8045f23
NC
1298
1299 /* Create the ifunc section, even if we will not encounter an
1300 indirect function symbol. We may not even see one in the input
1301 object file, but we can still encounter them in libraries. */
1302 (void) _bfd_elf_make_ifunc_reloc_section
1303 (abfd, sec, htab->elf.dynobj, 2);
70256ad8
AJ
1304 }
1305
c434dee6
AJ
1306 /* If this is a global symbol, we count the number of
1307 relocations we need for this symbol. */
1308 if (h != NULL)
70256ad8 1309 {
c434dee6
AJ
1310 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1311 }
1312 else
1313 {
e81d3500 1314 void **vpp;
c434dee6
AJ
1315 /* Track dynamic relocs needed for local syms too.
1316 We really need local syms available to do this
1317 easily. Oh well. */
1318
1319 asection *s;
1320 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1321 sec, r_symndx);
1322 if (s == NULL)
b34976b6 1323 return FALSE;
70256ad8 1324
e81d3500
DD
1325 /* Beware of type punned pointers vs strict aliasing
1326 rules. */
1327 vpp = &(elf_section_data (s)->local_dynrel);
1328 head = (struct elf64_x86_64_dyn_relocs **)vpp;
c434dee6 1329 }
70256ad8 1330
c434dee6
AJ
1331 p = *head;
1332 if (p == NULL || p->sec != sec)
1333 {
1334 bfd_size_type amt = sizeof *p;
d8045f23 1335
c434dee6
AJ
1336 p = ((struct elf64_x86_64_dyn_relocs *)
1337 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 1338 if (p == NULL)
b34976b6 1339 return FALSE;
c434dee6
AJ
1340 p->next = *head;
1341 *head = p;
1342 p->sec = sec;
1343 p->count = 0;
1344 p->pc_count = 0;
70256ad8 1345 }
c434dee6
AJ
1346
1347 p->count += 1;
d8045f23 1348 if (IS_X86_64_PCREL_TYPE (r_type))
c434dee6 1349 p->pc_count += 1;
70256ad8
AJ
1350 }
1351 break;
fe4770f4
AJ
1352
1353 /* This relocation describes the C++ object vtable hierarchy.
1354 Reconstruct it for later use during GC. */
1355 case R_X86_64_GNU_VTINHERIT:
c152c796 1356 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 1357 return FALSE;
fe4770f4
AJ
1358 break;
1359
1360 /* This relocation describes which C++ vtable entries are actually
1361 used. Record for later use during GC. */
1362 case R_X86_64_GNU_VTENTRY:
d17e0c6e
JB
1363 BFD_ASSERT (h != NULL);
1364 if (h != NULL
1365 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 1366 return FALSE;
fe4770f4 1367 break;
c434dee6
AJ
1368
1369 default:
1370 break;
70256ad8
AJ
1371 }
1372 }
1373
b34976b6 1374 return TRUE;
70256ad8
AJ
1375}
1376
1377/* Return the section that should be marked against GC for a given
407443a3 1378 relocation. */
70256ad8
AJ
1379
1380static asection *
27482721 1381elf64_x86_64_gc_mark_hook (asection *sec,
07adf181 1382 struct bfd_link_info *info,
27482721
AJ
1383 Elf_Internal_Rela *rel,
1384 struct elf_link_hash_entry *h,
1385 Elf_Internal_Sym *sym)
70256ad8
AJ
1386{
1387 if (h != NULL)
07adf181
AM
1388 switch (ELF64_R_TYPE (rel->r_info))
1389 {
1390 case R_X86_64_GNU_VTINHERIT:
1391 case R_X86_64_GNU_VTENTRY:
1392 return NULL;
1393 }
1394
1395 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
70256ad8
AJ
1396}
1397
407443a3 1398/* Update the got entry reference counts for the section being removed. */
70256ad8 1399
b34976b6 1400static bfd_boolean
27482721 1401elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
142411ca
L
1402 asection *sec,
1403 const Elf_Internal_Rela *relocs)
70256ad8
AJ
1404{
1405 Elf_Internal_Shdr *symtab_hdr;
1406 struct elf_link_hash_entry **sym_hashes;
1407 bfd_signed_vma *local_got_refcounts;
1408 const Elf_Internal_Rela *rel, *relend;
c434dee6 1409
7dda2462
TG
1410 if (info->relocatable)
1411 return TRUE;
1412
c434dee6 1413 elf_section_data (sec)->local_dynrel = NULL;
70256ad8 1414
0ffa91dd 1415 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8
AJ
1416 sym_hashes = elf_sym_hashes (abfd);
1417 local_got_refcounts = elf_local_got_refcounts (abfd);
1418
70256ad8
AJ
1419 relend = relocs + sec->reloc_count;
1420 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1421 {
1422 unsigned long r_symndx;
1423 unsigned int r_type;
1424 struct elf_link_hash_entry *h = NULL;
70256ad8 1425
26e41594
AM
1426 r_symndx = ELF64_R_SYM (rel->r_info);
1427 if (r_symndx >= symtab_hdr->sh_info)
1428 {
1429 struct elf64_x86_64_link_hash_entry *eh;
1430 struct elf64_x86_64_dyn_relocs **pp;
1431 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1432
26e41594 1433 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1434 while (h->root.type == bfd_link_hash_indirect
1435 || h->root.type == bfd_link_hash_warning)
1436 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1437 eh = (struct elf64_x86_64_link_hash_entry *) h;
c434dee6 1438
26e41594
AM
1439 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1440 if (p->sec == sec)
1441 {
1442 /* Everything must go for SEC. */
1443 *pp = p->next;
1444 break;
1445 }
1446 }
c434dee6 1447
26e41594 1448 r_type = ELF64_R_TYPE (rel->r_info);
142411ca
L
1449 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1450 symtab_hdr, sym_hashes,
1451 &r_type, GOT_UNKNOWN,
1452 rel, relend, h))
1453 return FALSE;
1454
26e41594
AM
1455 switch (r_type)
1456 {
1457 case R_X86_64_TLSLD:
1458 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1459 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1460 break;
c434dee6 1461
26e41594 1462 case R_X86_64_TLSGD:
67a4f2b7
AO
1463 case R_X86_64_GOTPC32_TLSDESC:
1464 case R_X86_64_TLSDESC_CALL:
26e41594
AM
1465 case R_X86_64_GOTTPOFF:
1466 case R_X86_64_GOT32:
1467 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
1468 case R_X86_64_GOT64:
1469 case R_X86_64_GOTPCREL64:
1470 case R_X86_64_GOTPLT64:
26e41594
AM
1471 if (h != NULL)
1472 {
7b81dfbb
AJ
1473 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1474 h->plt.refcount -= 1;
26e41594
AM
1475 if (h->got.refcount > 0)
1476 h->got.refcount -= 1;
1477 }
1478 else if (local_got_refcounts != NULL)
1479 {
1480 if (local_got_refcounts[r_symndx] > 0)
1481 local_got_refcounts[r_symndx] -= 1;
1482 }
1483 break;
c434dee6 1484
26e41594
AM
1485 case R_X86_64_8:
1486 case R_X86_64_16:
1487 case R_X86_64_32:
1488 case R_X86_64_64:
1489 case R_X86_64_32S:
1490 case R_X86_64_PC8:
1491 case R_X86_64_PC16:
1492 case R_X86_64_PC32:
d6ab8113 1493 case R_X86_64_PC64:
26e41594
AM
1494 if (info->shared)
1495 break;
1496 /* Fall thru */
c434dee6 1497
26e41594 1498 case R_X86_64_PLT32:
7b81dfbb 1499 case R_X86_64_PLTOFF64:
26e41594
AM
1500 if (h != NULL)
1501 {
1502 if (h->plt.refcount > 0)
1503 h->plt.refcount -= 1;
1504 }
1505 break;
70256ad8 1506
26e41594
AM
1507 default:
1508 break;
1509 }
1510 }
70256ad8 1511
b34976b6 1512 return TRUE;
70256ad8
AJ
1513}
1514
1515/* Adjust a symbol defined by a dynamic object and referenced by a
1516 regular object. The current definition is in some section of the
1517 dynamic object, but we're not including those sections. We have to
1518 change the definition to something the rest of the link can
407443a3 1519 understand. */
70256ad8 1520
b34976b6 1521static bfd_boolean
27482721
AJ
1522elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1523 struct elf_link_hash_entry *h)
70256ad8 1524{
c434dee6 1525 struct elf64_x86_64_link_hash_table *htab;
70256ad8 1526 asection *s;
70256ad8 1527
70256ad8
AJ
1528 /* If this is a function, put it in the procedure linkage table. We
1529 will fill in the contents of the procedure linkage table later,
1530 when we know the address of the .got section. */
1531 if (h->type == STT_FUNC
f5385ebf 1532 || h->needs_plt)
70256ad8 1533 {
c434dee6 1534 if (h->plt.refcount <= 0
27482721
AJ
1535 || SYMBOL_CALLS_LOCAL (info, h)
1536 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1537 && h->root.type == bfd_link_hash_undefweak))
70256ad8 1538 {
70256ad8
AJ
1539 /* This case can occur if we saw a PLT32 reloc in an input
1540 file, but the symbol was never referred to by a dynamic
1541 object, or if all references were garbage collected. In
1542 such a case, we don't actually need to build a procedure
1543 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 1544 h->plt.offset = (bfd_vma) -1;
f5385ebf 1545 h->needs_plt = 0;
70256ad8
AJ
1546 }
1547
b34976b6 1548 return TRUE;
70256ad8 1549 }
bbd7ec4a 1550 else
c434dee6
AJ
1551 /* It's possible that we incorrectly decided a .plt reloc was
1552 needed for an R_X86_64_PC32 reloc to a non-function sym in
1553 check_relocs. We can't decide accurately between function and
1554 non-function syms in check-relocs; Objects loaded later in
1555 the link may change h->type. So fix it now. */
bbd7ec4a 1556 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
1557
1558 /* If this is a weak symbol, and there is a real definition, the
1559 processor independent code will have arranged for us to see the
407443a3 1560 real definition first, and we can just use the same value. */
f6e332e6 1561 if (h->u.weakdef != NULL)
70256ad8 1562 {
f6e332e6
AM
1563 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1564 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1565 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1566 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 1567 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 1568 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 1569 return TRUE;
70256ad8
AJ
1570 }
1571
1572 /* This is a reference to a symbol defined by a dynamic object which
407443a3 1573 is not a function. */
70256ad8
AJ
1574
1575 /* If we are creating a shared library, we must presume that the
1576 only references to the symbol are via the global offset table.
1577 For such cases we need not do anything here; the relocations will
407443a3 1578 be handled correctly by relocate_section. */
70256ad8 1579 if (info->shared)
b34976b6 1580 return TRUE;
70256ad8
AJ
1581
1582 /* If there are no references to this symbol that do not use the
1583 GOT, we don't need to generate a copy reloc. */
f5385ebf 1584 if (!h->non_got_ref)
b34976b6 1585 return TRUE;
70256ad8 1586
c434dee6
AJ
1587 /* If -z nocopyreloc was given, we won't generate them either. */
1588 if (info->nocopyreloc)
1589 {
f5385ebf 1590 h->non_got_ref = 0;
b34976b6 1591 return TRUE;
c434dee6
AJ
1592 }
1593
d40d037c 1594 if (ELIMINATE_COPY_RELOCS)
c434dee6 1595 {
d40d037c
AJ
1596 struct elf64_x86_64_link_hash_entry * eh;
1597 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1598
d40d037c
AJ
1599 eh = (struct elf64_x86_64_link_hash_entry *) h;
1600 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1601 {
1602 s = p->sec->output_section;
1603 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1604 break;
1605 }
1606
1607 /* If we didn't find any dynamic relocs in read-only sections, then
1608 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1609 if (p == NULL)
1610 {
f5385ebf 1611 h->non_got_ref = 0;
d40d037c
AJ
1612 return TRUE;
1613 }
c434dee6
AJ
1614 }
1615
909272ee
AM
1616 if (h->size == 0)
1617 {
1618 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1619 h->root.root.string);
1620 return TRUE;
1621 }
1622
70256ad8 1623 /* We must allocate the symbol in our .dynbss section, which will
407443a3 1624 become part of the .bss section of the executable. There will be
70256ad8
AJ
1625 an entry for this symbol in the .dynsym section. The dynamic
1626 object will contain position independent code, so all references
1627 from the dynamic object to this symbol will go through the global
1628 offset table. The dynamic linker will use the .dynsym entry to
1629 determine the address it must put in the global offset table, so
1630 both the dynamic object and the regular object will refer to the
1631 same memory location for the variable. */
1632
c434dee6 1633 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
1634
1635 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1636 to copy the initial value out of the dynamic object and into the
cedb70c5 1637 runtime process image. */
70256ad8
AJ
1638 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1639 {
eea6121a 1640 htab->srelbss->size += sizeof (Elf64_External_Rela);
f5385ebf 1641 h->needs_copy = 1;
70256ad8
AJ
1642 }
1643
c434dee6 1644 s = htab->sdynbss;
70256ad8 1645
027297b7 1646 return _bfd_elf_adjust_dynamic_copy (h, s);
70256ad8
AJ
1647}
1648
c434dee6
AJ
1649/* Allocate space in .plt, .got and associated reloc sections for
1650 dynamic relocs. */
1651
b34976b6 1652static bfd_boolean
27482721 1653allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1654{
1655 struct bfd_link_info *info;
1656 struct elf64_x86_64_link_hash_table *htab;
1657 struct elf64_x86_64_link_hash_entry *eh;
1658 struct elf64_x86_64_dyn_relocs *p;
d8045f23 1659 bfd_boolean use_indirect_section = FALSE;
c434dee6 1660
e92d460e 1661 if (h->root.type == bfd_link_hash_indirect)
b34976b6 1662 return TRUE;
c434dee6 1663
e92d460e
AM
1664 if (h->root.type == bfd_link_hash_warning)
1665 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1666
c434dee6
AJ
1667 info = (struct bfd_link_info *) inf;
1668 htab = elf64_x86_64_hash_table (info);
1669
1670 if (htab->elf.dynamic_sections_created
27482721 1671 && h->plt.refcount > 0)
c434dee6
AJ
1672 {
1673 /* Make sure this symbol is output as a dynamic symbol.
1674 Undefined weak syms won't yet be marked as dynamic. */
1675 if (h->dynindx == -1
f5385ebf 1676 && !h->forced_local)
c434dee6 1677 {
c152c796 1678 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1679 return FALSE;
c434dee6
AJ
1680 }
1681
27482721
AJ
1682 if (info->shared
1683 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6
AJ
1684 {
1685 asection *s = htab->splt;
1686
1687 /* If this is the first .plt entry, make room for the special
1688 first entry. */
eea6121a
AM
1689 if (s->size == 0)
1690 s->size += PLT_ENTRY_SIZE;
c434dee6 1691
eea6121a 1692 h->plt.offset = s->size;
c434dee6
AJ
1693
1694 /* If this symbol is not defined in a regular file, and we are
1695 not generating a shared library, then set the symbol to this
1696 location in the .plt. This is required to make function
1697 pointers compare as equal between the normal executable and
1698 the shared library. */
1699 if (! info->shared
f5385ebf 1700 && !h->def_regular)
c434dee6
AJ
1701 {
1702 h->root.u.def.section = s;
1703 h->root.u.def.value = h->plt.offset;
1704 }
1705
1706 /* Make room for this entry. */
eea6121a 1707 s->size += PLT_ENTRY_SIZE;
c434dee6
AJ
1708
1709 /* We also need to make an entry in the .got.plt section, which
1710 will be placed in the .got section by the linker script. */
eea6121a 1711 htab->sgotplt->size += GOT_ENTRY_SIZE;
c434dee6
AJ
1712
1713 /* We also need to make an entry in the .rela.plt section. */
eea6121a 1714 htab->srelplt->size += sizeof (Elf64_External_Rela);
67a4f2b7 1715 htab->srelplt->reloc_count++;
c434dee6
AJ
1716 }
1717 else
1718 {
1719 h->plt.offset = (bfd_vma) -1;
f5385ebf 1720 h->needs_plt = 0;
c434dee6
AJ
1721 }
1722 }
1723 else
1724 {
1725 h->plt.offset = (bfd_vma) -1;
f5385ebf 1726 h->needs_plt = 0;
c434dee6
AJ
1727 }
1728
67a4f2b7
AO
1729 eh = (struct elf64_x86_64_link_hash_entry *) h;
1730 eh->tlsdesc_got = (bfd_vma) -1;
1731
bffbf940
JJ
1732 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1733 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1734 if (h->got.refcount > 0
1735 && !info->shared
1736 && h->dynindx == -1
1737 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
d8045f23
NC
1738 {
1739 h->got.offset = (bfd_vma) -1;
1740 }
bffbf940 1741 else if (h->got.refcount > 0)
c434dee6
AJ
1742 {
1743 asection *s;
b34976b6 1744 bfd_boolean dyn;
bffbf940 1745 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
1746
1747 /* Make sure this symbol is output as a dynamic symbol.
1748 Undefined weak syms won't yet be marked as dynamic. */
1749 if (h->dynindx == -1
f5385ebf 1750 && !h->forced_local)
c434dee6 1751 {
c152c796 1752 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1753 return FALSE;
c434dee6
AJ
1754 }
1755
67a4f2b7
AO
1756 if (GOT_TLS_GDESC_P (tls_type))
1757 {
1758 eh->tlsdesc_got = htab->sgotplt->size
1759 - elf64_x86_64_compute_jump_table_size (htab);
1760 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1761 h->got.offset = (bfd_vma) -2;
1762 }
1763 if (! GOT_TLS_GDESC_P (tls_type)
1764 || GOT_TLS_GD_P (tls_type))
1765 {
1766 s = htab->sgot;
1767 h->got.offset = s->size;
1768 s->size += GOT_ENTRY_SIZE;
1769 if (GOT_TLS_GD_P (tls_type))
1770 s->size += GOT_ENTRY_SIZE;
1771 }
c434dee6 1772 dyn = htab->elf.dynamic_sections_created;
bffbf940
JJ
1773 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1774 and two if global.
1775 R_X86_64_GOTTPOFF needs one dynamic relocation. */
67a4f2b7 1776 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
bffbf940 1777 || tls_type == GOT_TLS_IE)
eea6121a 1778 htab->srelgot->size += sizeof (Elf64_External_Rela);
67a4f2b7 1779 else if (GOT_TLS_GD_P (tls_type))
eea6121a 1780 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
67a4f2b7
AO
1781 else if (! GOT_TLS_GDESC_P (tls_type)
1782 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1783 || h->root.type != bfd_link_hash_undefweak)
27482721
AJ
1784 && (info->shared
1785 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 1786 htab->srelgot->size += sizeof (Elf64_External_Rela);
67a4f2b7
AO
1787 if (GOT_TLS_GDESC_P (tls_type))
1788 {
1789 htab->srelplt->size += sizeof (Elf64_External_Rela);
1790 htab->tlsdesc_plt = (bfd_vma) -1;
1791 }
c434dee6
AJ
1792 }
1793 else
1794 h->got.offset = (bfd_vma) -1;
1795
c434dee6 1796 if (eh->dyn_relocs == NULL)
b34976b6 1797 return TRUE;
c434dee6
AJ
1798
1799 /* In the shared -Bsymbolic case, discard space allocated for
1800 dynamic pc-relative relocs against symbols which turn out to be
1801 defined in regular objects. For the normal shared case, discard
1802 space for pc-relative relocs that have become local due to symbol
1803 visibility changes. */
1804
1805 if (info->shared)
1806 {
27482721
AJ
1807 /* Relocs that use pc_count are those that appear on a call
1808 insn, or certain REL relocs that can generated via assembly.
1809 We want calls to protected symbols to resolve directly to the
1810 function rather than going via the plt. If people want
1811 function pointer comparisons to work as expected then they
1812 should avoid writing weird assembly. */
1813 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6
AJ
1814 {
1815 struct elf64_x86_64_dyn_relocs **pp;
1816
1817 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1818 {
1819 p->count -= p->pc_count;
1820 p->pc_count = 0;
1821 if (p->count == 0)
1822 *pp = p->next;
1823 else
1824 pp = &p->next;
1825 }
1826 }
4e795f50
AM
1827
1828 /* Also discard relocs on undefined weak syms with non-default
1829 visibility. */
22d606e9 1830 if (eh->dyn_relocs != NULL
4e795f50 1831 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
1832 {
1833 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1834 eh->dyn_relocs = NULL;
1835
1836 /* Make sure undefined weak symbols are output as a dynamic
1837 symbol in PIEs. */
1838 else if (h->dynindx == -1
d8045f23
NC
1839 && ! h->forced_local
1840 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1841 return FALSE;
22d606e9 1842 }
c434dee6 1843 }
68d7e96a 1844 else if (_bfd_elf_is_ifunc_symbol (info->output_bfd, h)
d8045f23
NC
1845 && h->dynindx == -1
1846 && ! h->forced_local)
1847 {
1848 if (bfd_elf_link_record_dynamic_symbol (info, h)
1849 && h->dynindx != -1)
1850 use_indirect_section = TRUE;
1851 else
1852 return FALSE;
1853 }
d40d037c 1854 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
1855 {
1856 /* For the non-shared case, discard space for relocs against
1857 symbols which turn out to need copy relocs or are not
1858 dynamic. */
1859
f5385ebf
AM
1860 if (!h->non_got_ref
1861 && ((h->def_dynamic
1862 && !h->def_regular)
c434dee6
AJ
1863 || (htab->elf.dynamic_sections_created
1864 && (h->root.type == bfd_link_hash_undefweak
1865 || h->root.type == bfd_link_hash_undefined))))
1866 {
1867 /* Make sure this symbol is output as a dynamic symbol.
1868 Undefined weak syms won't yet be marked as dynamic. */
1869 if (h->dynindx == -1
d8045f23
NC
1870 && ! h->forced_local
1871 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1872 return FALSE;
c434dee6
AJ
1873
1874 /* If that succeeded, we know we'll be keeping all the
1875 relocs. */
1876 if (h->dynindx != -1)
1877 goto keep;
1878 }
1879
1880 eh->dyn_relocs = NULL;
1881
1882 keep: ;
1883 }
1884
1885 /* Finally, allocate space. */
1886 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1887 {
e7c33416
NC
1888 asection * sreloc;
1889
d8045f23
NC
1890 if (use_indirect_section)
1891 sreloc = elf_section_data (p->sec)->indirect_relocs;
1892 else
1893 sreloc = elf_section_data (p->sec)->sreloc;
e7c33416
NC
1894
1895 BFD_ASSERT (sreloc != NULL);
1896
eea6121a 1897 sreloc->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1898 }
1899
b34976b6 1900 return TRUE;
c434dee6
AJ
1901}
1902
1903/* Find any dynamic relocs that apply to read-only sections. */
1904
b34976b6 1905static bfd_boolean
27482721 1906readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1907{
1908 struct elf64_x86_64_link_hash_entry *eh;
1909 struct elf64_x86_64_dyn_relocs *p;
1910
e92d460e
AM
1911 if (h->root.type == bfd_link_hash_warning)
1912 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1913
c434dee6
AJ
1914 eh = (struct elf64_x86_64_link_hash_entry *) h;
1915 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1916 {
1917 asection *s = p->sec->output_section;
1918
1919 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1920 {
1921 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1922
1923 info->flags |= DF_TEXTREL;
1924
1925 /* Not an error, just cut short the traversal. */
b34976b6 1926 return FALSE;
c434dee6
AJ
1927 }
1928 }
b34976b6 1929 return TRUE;
c434dee6
AJ
1930}
1931
70256ad8
AJ
1932/* Set the sizes of the dynamic sections. */
1933
b34976b6 1934static bfd_boolean
27482721
AJ
1935elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1936 struct bfd_link_info *info)
70256ad8 1937{
c434dee6 1938 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1939 bfd *dynobj;
1940 asection *s;
b34976b6 1941 bfd_boolean relocs;
c434dee6 1942 bfd *ibfd;
70256ad8 1943
c434dee6
AJ
1944 htab = elf64_x86_64_hash_table (info);
1945 dynobj = htab->elf.dynobj;
1946 if (dynobj == NULL)
1947 abort ();
70256ad8 1948
c434dee6 1949 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1950 {
1951 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 1952 if (info->executable)
70256ad8
AJ
1953 {
1954 s = bfd_get_section_by_name (dynobj, ".interp");
c434dee6
AJ
1955 if (s == NULL)
1956 abort ();
eea6121a 1957 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
70256ad8
AJ
1958 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1959 }
1960 }
70256ad8 1961
c434dee6
AJ
1962 /* Set up .got offsets for local syms, and space for local dynamic
1963 relocs. */
1964 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 1965 {
c434dee6
AJ
1966 bfd_signed_vma *local_got;
1967 bfd_signed_vma *end_local_got;
bffbf940 1968 char *local_tls_type;
67a4f2b7 1969 bfd_vma *local_tlsdesc_gotent;
c434dee6
AJ
1970 bfd_size_type locsymcount;
1971 Elf_Internal_Shdr *symtab_hdr;
1972 asection *srel;
70256ad8 1973
0ffa91dd 1974 if (! is_x86_64_elf (ibfd))
70256ad8
AJ
1975 continue;
1976
c434dee6 1977 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 1978 {
c434dee6
AJ
1979 struct elf64_x86_64_dyn_relocs *p;
1980
e81d3500
DD
1981 for (p = (struct elf64_x86_64_dyn_relocs *)
1982 (elf_section_data (s)->local_dynrel);
c434dee6
AJ
1983 p != NULL;
1984 p = p->next)
70256ad8 1985 {
c434dee6
AJ
1986 if (!bfd_is_abs_section (p->sec)
1987 && bfd_is_abs_section (p->sec->output_section))
1988 {
1989 /* Input section has been discarded, either because
1990 it is a copy of a linkonce section or due to
1991 linker script /DISCARD/, so we'll be discarding
1992 the relocs too. */
1993 }
1994 else if (p->count != 0)
1995 {
1996 srel = elf_section_data (p->sec)->sreloc;
eea6121a 1997 srel->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1998 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1999 info->flags |= DF_TEXTREL;
c434dee6 2000 }
70256ad8
AJ
2001 }
2002 }
c434dee6
AJ
2003
2004 local_got = elf_local_got_refcounts (ibfd);
2005 if (!local_got)
2006 continue;
2007
0ffa91dd 2008 symtab_hdr = &elf_symtab_hdr (ibfd);
c434dee6
AJ
2009 locsymcount = symtab_hdr->sh_info;
2010 end_local_got = local_got + locsymcount;
bffbf940 2011 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
67a4f2b7 2012 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
c434dee6
AJ
2013 s = htab->sgot;
2014 srel = htab->srelgot;
67a4f2b7
AO
2015 for (; local_got < end_local_got;
2016 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
70256ad8 2017 {
67a4f2b7 2018 *local_tlsdesc_gotent = (bfd_vma) -1;
c434dee6 2019 if (*local_got > 0)
70256ad8 2020 {
67a4f2b7
AO
2021 if (GOT_TLS_GDESC_P (*local_tls_type))
2022 {
2023 *local_tlsdesc_gotent = htab->sgotplt->size
2024 - elf64_x86_64_compute_jump_table_size (htab);
2025 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
2026 *local_got = (bfd_vma) -2;
2027 }
2028 if (! GOT_TLS_GDESC_P (*local_tls_type)
2029 || GOT_TLS_GD_P (*local_tls_type))
2030 {
2031 *local_got = s->size;
2032 s->size += GOT_ENTRY_SIZE;
2033 if (GOT_TLS_GD_P (*local_tls_type))
2034 s->size += GOT_ENTRY_SIZE;
2035 }
bffbf940 2036 if (info->shared
67a4f2b7 2037 || GOT_TLS_GD_ANY_P (*local_tls_type)
bffbf940 2038 || *local_tls_type == GOT_TLS_IE)
67a4f2b7
AO
2039 {
2040 if (GOT_TLS_GDESC_P (*local_tls_type))
2041 {
2042 htab->srelplt->size += sizeof (Elf64_External_Rela);
2043 htab->tlsdesc_plt = (bfd_vma) -1;
2044 }
2045 if (! GOT_TLS_GDESC_P (*local_tls_type)
2046 || GOT_TLS_GD_P (*local_tls_type))
2047 srel->size += sizeof (Elf64_External_Rela);
2048 }
70256ad8
AJ
2049 }
2050 else
c434dee6
AJ
2051 *local_got = (bfd_vma) -1;
2052 }
2053 }
70256ad8 2054
bffbf940
JJ
2055 if (htab->tls_ld_got.refcount > 0)
2056 {
2057 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2058 relocs. */
eea6121a
AM
2059 htab->tls_ld_got.offset = htab->sgot->size;
2060 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
2061 htab->srelgot->size += sizeof (Elf64_External_Rela);
bffbf940
JJ
2062 }
2063 else
2064 htab->tls_ld_got.offset = -1;
2065
c434dee6
AJ
2066 /* Allocate global sym .plt and .got entries, and space for global
2067 sym dynamic relocs. */
e7c33416 2068 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
c434dee6 2069
67a4f2b7
AO
2070 /* For every jump slot reserved in the sgotplt, reloc_count is
2071 incremented. However, when we reserve space for TLS descriptors,
2072 it's not incremented, so in order to compute the space reserved
2073 for them, it suffices to multiply the reloc count by the jump
2074 slot size. */
2075 if (htab->srelplt)
2076 htab->sgotplt_jump_table_size
2077 = elf64_x86_64_compute_jump_table_size (htab);
2078
2079 if (htab->tlsdesc_plt)
2080 {
2081 /* If we're not using lazy TLS relocations, don't generate the
2082 PLT and GOT entries they require. */
2083 if ((info->flags & DF_BIND_NOW))
2084 htab->tlsdesc_plt = 0;
2085 else
2086 {
2087 htab->tlsdesc_got = htab->sgot->size;
2088 htab->sgot->size += GOT_ENTRY_SIZE;
2089 /* Reserve room for the initial entry.
2090 FIXME: we could probably do away with it in this case. */
2091 if (htab->splt->size == 0)
2092 htab->splt->size += PLT_ENTRY_SIZE;
2093 htab->tlsdesc_plt = htab->splt->size;
2094 htab->splt->size += PLT_ENTRY_SIZE;
2095 }
2096 }
2097
c434dee6
AJ
2098 /* We now have determined the sizes of the various dynamic sections.
2099 Allocate memory for them. */
b34976b6 2100 relocs = FALSE;
c434dee6
AJ
2101 for (s = dynobj->sections; s != NULL; s = s->next)
2102 {
2103 if ((s->flags & SEC_LINKER_CREATED) == 0)
2104 continue;
2105
2106 if (s == htab->splt
2107 || s == htab->sgot
75ff4589
L
2108 || s == htab->sgotplt
2109 || s == htab->sdynbss)
c434dee6
AJ
2110 {
2111 /* Strip this section if we don't need it; see the
2112 comment below. */
2113 }
0112cd26 2114 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
c434dee6 2115 {
eea6121a 2116 if (s->size != 0 && s != htab->srelplt)
b34976b6 2117 relocs = TRUE;
c434dee6
AJ
2118
2119 /* We use the reloc_count field as a counter if we need
2120 to copy relocs into the output file. */
67a4f2b7
AO
2121 if (s != htab->srelplt)
2122 s->reloc_count = 0;
70256ad8 2123 }
c434dee6 2124 else
70256ad8
AJ
2125 {
2126 /* It's not one of our sections, so don't allocate space. */
2127 continue;
2128 }
2129
eea6121a 2130 if (s->size == 0)
70256ad8 2131 {
c434dee6
AJ
2132 /* If we don't need this section, strip it from the
2133 output file. This is mostly to handle .rela.bss and
2134 .rela.plt. We must create both sections in
2135 create_dynamic_sections, because they must be created
2136 before the linker maps input sections to output
2137 sections. The linker does that before
2138 adjust_dynamic_symbol is called, and it is that
2139 function which decides whether anything needs to go
2140 into these sections. */
2141
8423293d 2142 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
2143 continue;
2144 }
2145
c456f082
AM
2146 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2147 continue;
2148
70256ad8
AJ
2149 /* Allocate memory for the section contents. We use bfd_zalloc
2150 here in case unused entries are not reclaimed before the
2151 section's contents are written out. This should not happen,
2152 but this way if it does, we get a R_X86_64_NONE reloc instead
2153 of garbage. */
eea6121a 2154 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 2155 if (s->contents == NULL)
b34976b6 2156 return FALSE;
70256ad8
AJ
2157 }
2158
c434dee6 2159 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
2160 {
2161 /* Add some entries to the .dynamic section. We fill in the
2162 values later, in elf64_x86_64_finish_dynamic_sections, but we
2163 must add the entries now so that we get the correct size for
407443a3 2164 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 2165 dynamic linker and used by the debugger. */
dc810e39 2166#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2167 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 2168
36af4a4e 2169 if (info->executable)
70256ad8 2170 {
dc810e39 2171 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 2172 return FALSE;
70256ad8
AJ
2173 }
2174
eea6121a 2175 if (htab->splt->size != 0)
70256ad8 2176 {
dc810e39
AM
2177 if (!add_dynamic_entry (DT_PLTGOT, 0)
2178 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2179 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2180 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 2181 return FALSE;
67a4f2b7
AO
2182
2183 if (htab->tlsdesc_plt
2184 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2185 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2186 return FALSE;
70256ad8
AJ
2187 }
2188
2189 if (relocs)
2190 {
dc810e39
AM
2191 if (!add_dynamic_entry (DT_RELA, 0)
2192 || !add_dynamic_entry (DT_RELASZ, 0)
2193 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 2194 return FALSE;
70256ad8 2195
c434dee6
AJ
2196 /* If any dynamic relocs apply to a read-only section,
2197 then we need a DT_TEXTREL entry. */
2198 if ((info->flags & DF_TEXTREL) == 0)
e7c33416 2199 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
c434dee6
AJ
2200
2201 if ((info->flags & DF_TEXTREL) != 0)
2202 {
2203 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 2204 return FALSE;
c434dee6 2205 }
70256ad8
AJ
2206 }
2207 }
dc810e39 2208#undef add_dynamic_entry
70256ad8 2209
b34976b6 2210 return TRUE;
70256ad8
AJ
2211}
2212
67a4f2b7
AO
2213static bfd_boolean
2214elf64_x86_64_always_size_sections (bfd *output_bfd,
2215 struct bfd_link_info *info)
2216{
2217 asection *tls_sec = elf_hash_table (info)->tls_sec;
2218
2219 if (tls_sec)
2220 {
2221 struct elf_link_hash_entry *tlsbase;
2222
2223 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2224 "_TLS_MODULE_BASE_",
2225 FALSE, FALSE, FALSE);
2226
2227 if (tlsbase && tlsbase->type == STT_TLS)
2228 {
2229 struct bfd_link_hash_entry *bh = NULL;
2230 const struct elf_backend_data *bed
2231 = get_elf_backend_data (output_bfd);
2232
2233 if (!(_bfd_generic_link_add_one_symbol
2234 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2235 tls_sec, 0, NULL, FALSE,
2236 bed->collect, &bh)))
2237 return FALSE;
9f03412a
AO
2238
2239 elf64_x86_64_hash_table (info)->tls_module_base = bh;
2240
67a4f2b7
AO
2241 tlsbase = (struct elf_link_hash_entry *)bh;
2242 tlsbase->def_regular = 1;
2243 tlsbase->other = STV_HIDDEN;
2244 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2245 }
2246 }
2247
2248 return TRUE;
2249}
2250
9f03412a
AO
2251/* _TLS_MODULE_BASE_ needs to be treated especially when linking
2252 executables. Rather than setting it to the beginning of the TLS
2253 section, we have to set it to the end. This function may be called
2254 multiple times, it is idempotent. */
2255
2256static void
2257set_tls_module_base (struct bfd_link_info *info)
2258{
2259 struct bfd_link_hash_entry *base;
2260
2261 if (!info->executable)
2262 return;
2263
2264 base = elf64_x86_64_hash_table (info)->tls_module_base;
2265
2266 if (!base)
2267 return;
2268
2269 base->u.def.value = elf_hash_table (info)->tls_size;
2270}
2271
bffbf940
JJ
2272/* Return the base VMA address which should be subtracted from real addresses
2273 when resolving @dtpoff relocation.
2274 This is PT_TLS segment p_vaddr. */
2275
2276static bfd_vma
27482721 2277dtpoff_base (struct bfd_link_info *info)
bffbf940 2278{
e1918d23
AM
2279 /* If tls_sec is NULL, we should have signalled an error already. */
2280 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 2281 return 0;
e1918d23 2282 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
2283}
2284
2285/* Return the relocation value for @tpoff relocation
2286 if STT_TLS virtual address is ADDRESS. */
2287
2288static bfd_vma
27482721 2289tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 2290{
e1918d23 2291 struct elf_link_hash_table *htab = elf_hash_table (info);
bffbf940
JJ
2292
2293 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 2294 if (htab->tls_sec == NULL)
bffbf940 2295 return 0;
e1918d23 2296 return address - htab->tls_size - htab->tls_sec->vma;
bffbf940
JJ
2297}
2298
90f487df
L
2299/* Is the instruction before OFFSET in CONTENTS a 32bit relative
2300 branch? */
2301
2302static bfd_boolean
2303is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2304{
2305 /* Opcode Instruction
2306 0xe8 call
2307 0xe9 jump
2308 0x0f 0x8x conditional jump */
2309 return ((offset > 0
2310 && (contents [offset - 1] == 0xe8
2311 || contents [offset - 1] == 0xe9))
2312 || (offset > 1
2313 && contents [offset - 2] == 0x0f
2314 && (contents [offset - 1] & 0xf0) == 0x80));
2315}
2316
8d88c4ca
NC
2317/* Relocate an x86_64 ELF section. */
2318
b34976b6 2319static bfd_boolean
27482721
AJ
2320elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2321 bfd *input_bfd, asection *input_section,
2322 bfd_byte *contents, Elf_Internal_Rela *relocs,
2323 Elf_Internal_Sym *local_syms,
2324 asection **local_sections)
8d88c4ca 2325{
c434dee6 2326 struct elf64_x86_64_link_hash_table *htab;
8d88c4ca
NC
2327 Elf_Internal_Shdr *symtab_hdr;
2328 struct elf_link_hash_entry **sym_hashes;
2329 bfd_vma *local_got_offsets;
67a4f2b7 2330 bfd_vma *local_tlsdesc_gotents;
c434dee6 2331 Elf_Internal_Rela *rel;
8d88c4ca
NC
2332 Elf_Internal_Rela *relend;
2333
0ffa91dd
NC
2334 BFD_ASSERT (is_x86_64_elf (input_bfd));
2335
c434dee6 2336 htab = elf64_x86_64_hash_table (info);
0ffa91dd 2337 symtab_hdr = &elf_symtab_hdr (input_bfd);
8d88c4ca
NC
2338 sym_hashes = elf_sym_hashes (input_bfd);
2339 local_got_offsets = elf_local_got_offsets (input_bfd);
67a4f2b7 2340 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
8d88c4ca 2341
9f03412a
AO
2342 set_tls_module_base (info);
2343
c434dee6 2344 rel = relocs;
8d88c4ca 2345 relend = relocs + input_section->reloc_count;
c434dee6 2346 for (; rel < relend; rel++)
8d88c4ca 2347 {
bffbf940 2348 unsigned int r_type;
8d88c4ca
NC
2349 reloc_howto_type *howto;
2350 unsigned long r_symndx;
2351 struct elf_link_hash_entry *h;
2352 Elf_Internal_Sym *sym;
2353 asection *sec;
67a4f2b7 2354 bfd_vma off, offplt;
8d88c4ca 2355 bfd_vma relocation;
b34976b6 2356 bfd_boolean unresolved_reloc;
8d88c4ca 2357 bfd_reloc_status_type r;
bffbf940 2358 int tls_type;
8d88c4ca 2359
c434dee6 2360 r_type = ELF64_R_TYPE (rel->r_info);
fe4770f4
AJ
2361 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2362 || r_type == (int) R_X86_64_GNU_VTENTRY)
2363 continue;
8d88c4ca 2364
bffbf940 2365 if (r_type >= R_X86_64_max)
8da6118f
KH
2366 {
2367 bfd_set_error (bfd_error_bad_value);
b34976b6 2368 return FALSE;
8da6118f 2369 }
8d88c4ca 2370
b491616a 2371 howto = x86_64_elf_howto_table + r_type;
c434dee6 2372 r_symndx = ELF64_R_SYM (rel->r_info);
8d88c4ca
NC
2373 h = NULL;
2374 sym = NULL;
2375 sec = NULL;
b34976b6 2376 unresolved_reloc = FALSE;
8d88c4ca 2377 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
2378 {
2379 sym = local_syms + r_symndx;
2380 sec = local_sections[r_symndx];
c434dee6 2381
8517fae7 2382 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8da6118f 2383 }
8d88c4ca 2384 else
8da6118f 2385 {
560e09e9 2386 bfd_boolean warned;
c434dee6 2387
b2a8e766
AM
2388 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2389 r_symndx, symtab_hdr, sym_hashes,
2390 h, sec, relocation,
2391 unresolved_reloc, warned);
8da6118f 2392 }
ab96bf03
AM
2393
2394 if (sec != NULL && elf_discarded_section (sec))
2395 {
2396 /* For relocs against symbols from removed linkonce sections,
2397 or sections discarded by a linker script, we just want the
2398 section contents zeroed. Avoid any special processing. */
2399 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2400 rel->r_info = 0;
2401 rel->r_addend = 0;
2402 continue;
2403 }
2404
2405 if (info->relocatable)
2406 continue;
2407
70256ad8
AJ
2408 /* When generating a shared object, the relocations handled here are
2409 copied into the output file to be resolved at run time. */
2410 switch (r_type)
2411 {
7b81dfbb 2412 asection *base_got;
70256ad8 2413 case R_X86_64_GOT32:
7b81dfbb 2414 case R_X86_64_GOT64:
70256ad8
AJ
2415 /* Relocation is to the entry for this symbol in the global
2416 offset table. */
70256ad8 2417 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
2418 case R_X86_64_GOTPCREL64:
2419 /* Use global offset table entry as symbol value. */
2420 case R_X86_64_GOTPLT64:
2421 /* This is the same as GOT64 for relocation purposes, but
2422 indicates the existence of a PLT entry. The difficulty is,
2423 that we must calculate the GOT slot offset from the PLT
2424 offset, if this symbol got a PLT entry (it was global).
2425 Additionally if it's computed from the PLT entry, then that
2426 GOT offset is relative to .got.plt, not to .got. */
2427 base_got = htab->sgot;
2428
c434dee6
AJ
2429 if (htab->sgot == NULL)
2430 abort ();
053579d7 2431
51e0a107 2432 if (h != NULL)
70256ad8 2433 {
b34976b6 2434 bfd_boolean dyn;
c434dee6
AJ
2435
2436 off = h->got.offset;
7b81dfbb
AJ
2437 if (h->needs_plt
2438 && h->plt.offset != (bfd_vma)-1
2439 && off == (bfd_vma)-1)
2440 {
2441 /* We can't use h->got.offset here to save
2442 state, or even just remember the offset, as
2443 finish_dynamic_symbol would use that as offset into
2444 .got. */
2445 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2446 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2447 base_got = htab->sgotplt;
2448 }
2449
c434dee6 2450 dyn = htab->elf.dynamic_sections_created;
51e0a107 2451
27482721 2452 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
51e0a107 2453 || (info->shared
27482721 2454 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
2455 || (ELF_ST_VISIBILITY (h->other)
2456 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
2457 {
2458 /* This is actually a static link, or it is a -Bsymbolic
2459 link and the symbol is defined locally, or the symbol
407443a3 2460 was forced to be local because of a version file. We
51e0a107
JH
2461 must initialize this entry in the global offset table.
2462 Since the offset must always be a multiple of 8, we
2463 use the least significant bit to record whether we
2464 have initialized it already.
2465
2466 When doing a dynamic link, we create a .rela.got
407443a3
AJ
2467 relocation entry to initialize the value. This is
2468 done in the finish_dynamic_symbol routine. */
51e0a107
JH
2469 if ((off & 1) != 0)
2470 off &= ~1;
2471 else
2472 {
2473 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
2474 base_got->contents + off);
2475 /* Note that this is harmless for the GOTPLT64 case,
2476 as -1 | 1 still is -1. */
51e0a107
JH
2477 h->got.offset |= 1;
2478 }
2479 }
053579d7 2480 else
b34976b6 2481 unresolved_reloc = FALSE;
70256ad8 2482 }
51e0a107
JH
2483 else
2484 {
c434dee6
AJ
2485 if (local_got_offsets == NULL)
2486 abort ();
51e0a107
JH
2487
2488 off = local_got_offsets[r_symndx];
2489
2490 /* The offset must always be a multiple of 8. We use
407443a3
AJ
2491 the least significant bit to record whether we have
2492 already generated the necessary reloc. */
51e0a107
JH
2493 if ((off & 1) != 0)
2494 off &= ~1;
2495 else
2496 {
c434dee6 2497 bfd_put_64 (output_bfd, relocation,
7b81dfbb 2498 base_got->contents + off);
51e0a107
JH
2499
2500 if (info->shared)
2501 {
947216bf 2502 asection *s;
51e0a107 2503 Elf_Internal_Rela outrel;
947216bf 2504 bfd_byte *loc;
70256ad8 2505
51e0a107
JH
2506 /* We need to generate a R_X86_64_RELATIVE reloc
2507 for the dynamic linker. */
947216bf
AM
2508 s = htab->srelgot;
2509 if (s == NULL)
c434dee6 2510 abort ();
51e0a107 2511
7b81dfbb
AJ
2512 outrel.r_offset = (base_got->output_section->vma
2513 + base_got->output_offset
51e0a107
JH
2514 + off);
2515 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2516 outrel.r_addend = relocation;
947216bf
AM
2517 loc = s->contents;
2518 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2519 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
51e0a107
JH
2520 }
2521
2522 local_got_offsets[r_symndx] |= 1;
2523 }
51e0a107 2524 }
6a2bda3f 2525
c434dee6
AJ
2526 if (off >= (bfd_vma) -2)
2527 abort ();
2528
7b81dfbb
AJ
2529 relocation = base_got->output_section->vma
2530 + base_got->output_offset + off;
2531 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
8c37241b
JJ
2532 relocation -= htab->sgotplt->output_section->vma
2533 - htab->sgotplt->output_offset;
c434dee6 2534
70256ad8
AJ
2535 break;
2536
d6ab8113
JB
2537 case R_X86_64_GOTOFF64:
2538 /* Relocation is relative to the start of the global offset
2539 table. */
2540
2541 /* Check to make sure it isn't a protected function symbol
2542 for shared library since it may not be local when used
2543 as function address. */
2544 if (info->shared
2545 && h
2546 && h->def_regular
2547 && h->type == STT_FUNC
2548 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2549 {
2550 (*_bfd_error_handler)
2551 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2552 input_bfd, h->root.root.string);
2553 bfd_set_error (bfd_error_bad_value);
2554 return FALSE;
2555 }
2556
2557 /* Note that sgot is not involved in this
2558 calculation. We always want the start of .got.plt. If we
2559 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2560 permitted by the ABI, we might have to change this
2561 calculation. */
2562 relocation -= htab->sgotplt->output_section->vma
2563 + htab->sgotplt->output_offset;
2564 break;
2565
2566 case R_X86_64_GOTPC32:
7b81dfbb 2567 case R_X86_64_GOTPC64:
d6ab8113
JB
2568 /* Use global offset table as symbol value. */
2569 relocation = htab->sgotplt->output_section->vma
2570 + htab->sgotplt->output_offset;
2571 unresolved_reloc = FALSE;
2572 break;
7b81dfbb
AJ
2573
2574 case R_X86_64_PLTOFF64:
2575 /* Relocation is PLT entry relative to GOT. For local
2576 symbols it's the symbol itself relative to GOT. */
2577 if (h != NULL
2578 /* See PLT32 handling. */
2579 && h->plt.offset != (bfd_vma) -1
2580 && htab->splt != NULL)
2581 {
2582 relocation = (htab->splt->output_section->vma
2583 + htab->splt->output_offset
2584 + h->plt.offset);
2585 unresolved_reloc = FALSE;
2586 }
2587
2588 relocation -= htab->sgotplt->output_section->vma
2589 + htab->sgotplt->output_offset;
2590 break;
d6ab8113 2591
70256ad8
AJ
2592 case R_X86_64_PLT32:
2593 /* Relocation is to the entry for this symbol in the
2594 procedure linkage table. */
2595
2596 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 2597 without using the procedure linkage table. */
70256ad8
AJ
2598 if (h == NULL)
2599 break;
2600
c434dee6
AJ
2601 if (h->plt.offset == (bfd_vma) -1
2602 || htab->splt == NULL)
70256ad8
AJ
2603 {
2604 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
2605 happens when statically linking PIC code, or when
2606 using -Bsymbolic. */
70256ad8
AJ
2607 break;
2608 }
2609
c434dee6
AJ
2610 relocation = (htab->splt->output_section->vma
2611 + htab->splt->output_offset
70256ad8 2612 + h->plt.offset);
b34976b6 2613 unresolved_reloc = FALSE;
70256ad8
AJ
2614 break;
2615
fd8ab9e5
AJ
2616 case R_X86_64_PC8:
2617 case R_X86_64_PC16:
2618 case R_X86_64_PC32:
6610a52d 2619 if (info->shared
ba3bee0b 2620 && (input_section->flags & SEC_ALLOC) != 0
90f487df 2621 && (input_section->flags & SEC_READONLY) != 0
41bed6dd 2622 && h != NULL)
6610a52d 2623 {
41bed6dd
L
2624 bfd_boolean fail = FALSE;
2625 bfd_boolean branch
2626 = (r_type == R_X86_64_PC32
2627 && is_32bit_relative_branch (contents, rel->r_offset));
2628
2629 if (SYMBOL_REFERENCES_LOCAL (info, h))
2630 {
2631 /* Symbol is referenced locally. Make sure it is
2632 defined locally or for a branch. */
2633 fail = !h->def_regular && !branch;
2634 }
90f487df 2635 else
41bed6dd
L
2636 {
2637 /* Symbol isn't referenced locally. We only allow
2638 branch to symbol with non-default visibility. */
2639 fail = (!branch
2640 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
2641 }
2642
2643 if (fail)
2644 {
2645 const char *fmt;
2646 const char *v;
2647 const char *pic = "";
2648
2649 switch (ELF_ST_VISIBILITY (h->other))
2650 {
2651 case STV_HIDDEN:
2652 v = _("hidden symbol");
2653 break;
2654 case STV_INTERNAL:
2655 v = _("internal symbol");
2656 break;
2657 case STV_PROTECTED:
2658 v = _("protected symbol");
2659 break;
2660 default:
2661 v = _("symbol");
2662 pic = _("; recompile with -fPIC");
2663 break;
2664 }
2665
2666 if (h->def_regular)
2667 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
2668 else
2669 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
2670
2671 (*_bfd_error_handler) (fmt, input_bfd,
2672 x86_64_elf_howto_table[r_type].name,
2673 v, h->root.root.string, pic);
2674 bfd_set_error (bfd_error_bad_value);
2675 return FALSE;
2676 }
6610a52d
L
2677 }
2678 /* Fall through. */
2679
70256ad8
AJ
2680 case R_X86_64_8:
2681 case R_X86_64_16:
2682 case R_X86_64_32:
d6ab8113 2683 case R_X86_64_PC64:
6b3db546 2684 case R_X86_64_64:
80643fbc 2685 /* FIXME: The ABI says the linker should make sure the value is
407443a3 2686 the same when it's zeroextended to 64 bit. */
c434dee6 2687
b1e24c02 2688 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
2689 break;
2690
2691 if ((info->shared
4bc6e03a
AJ
2692 && (h == NULL
2693 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2694 || h->root.type != bfd_link_hash_undefweak)
d8045f23
NC
2695 && (! IS_X86_64_PCREL_TYPE (r_type)
2696 || ! SYMBOL_CALLS_LOCAL (info, h)))
2697 || (! info->shared
2698 && h != NULL
2699 && h->dynindx != -1
2700 && ! h->forced_local
2701 && ((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs != NULL
68d7e96a 2702 && _bfd_elf_is_ifunc_symbol (output_bfd, h))
d40d037c
AJ
2703 || (ELIMINATE_COPY_RELOCS
2704 && !info->shared
c434dee6
AJ
2705 && h != NULL
2706 && h->dynindx != -1
f5385ebf
AM
2707 && !h->non_got_ref
2708 && ((h->def_dynamic
2709 && !h->def_regular)
c434dee6 2710 || h->root.type == bfd_link_hash_undefweak
0f88be7a 2711 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
2712 {
2713 Elf_Internal_Rela outrel;
947216bf 2714 bfd_byte *loc;
b34976b6 2715 bfd_boolean skip, relocate;
c434dee6 2716 asection *sreloc;
70256ad8
AJ
2717
2718 /* When generating a shared object, these relocations
2719 are copied into the output file to be resolved at run
407443a3 2720 time. */
b34976b6
AM
2721 skip = FALSE;
2722 relocate = FALSE;
70256ad8 2723
c629eae0
JJ
2724 outrel.r_offset =
2725 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 2726 rel->r_offset);
c629eae0 2727 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2728 skip = TRUE;
0fb19cbc 2729 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2730 skip = TRUE, relocate = TRUE;
70256ad8
AJ
2731
2732 outrel.r_offset += (input_section->output_section->vma
2733 + input_section->output_offset);
2734
2735 if (skip)
0bb2d96a 2736 memset (&outrel, 0, sizeof outrel);
c434dee6 2737
fd8ab9e5
AJ
2738 /* h->dynindx may be -1 if this symbol was marked to
2739 become local. */
2740 else if (h != NULL
c434dee6 2741 && h->dynindx != -1
d8045f23
NC
2742 && (IS_X86_64_PCREL_TYPE (r_type)
2743 || ! info->shared
2744 || ! SYMBOLIC_BIND (info, h)
2745 || ! h->def_regular))
70256ad8 2746 {
70256ad8 2747 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
c434dee6 2748 outrel.r_addend = rel->r_addend;
70256ad8
AJ
2749 }
2750 else
2751 {
c434dee6 2752 /* This symbol is local, or marked to become local. */
607c0e09
AS
2753 if (r_type == R_X86_64_64)
2754 {
b34976b6 2755 relocate = TRUE;
607c0e09
AS
2756 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2757 outrel.r_addend = relocation + rel->r_addend;
2758 }
2759 else
2760 {
2761 long sindx;
2762
8517fae7 2763 if (bfd_is_abs_section (sec))
607c0e09
AS
2764 sindx = 0;
2765 else if (sec == NULL || sec->owner == NULL)
2766 {
2767 bfd_set_error (bfd_error_bad_value);
b34976b6 2768 return FALSE;
607c0e09
AS
2769 }
2770 else
2771 {
2772 asection *osec;
2773
74541ad4
AM
2774 /* We are turning this relocation into one
2775 against a section symbol. It would be
2776 proper to subtract the symbol's value,
2777 osec->vma, from the emitted reloc addend,
2778 but ld.so expects buggy relocs. */
607c0e09
AS
2779 osec = sec->output_section;
2780 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
2781 if (sindx == 0)
2782 {
2783 asection *oi = htab->elf.text_index_section;
2784 sindx = elf_section_data (oi)->dynindx;
2785 }
2786 BFD_ASSERT (sindx != 0);
607c0e09
AS
2787 }
2788
2789 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2790 outrel.r_addend = relocation + rel->r_addend;
2791 }
70256ad8
AJ
2792 }
2793
d8045f23
NC
2794 if (! info->shared
2795 && h != NULL
2796 && h->dynindx != -1
2797 && ! h->forced_local
68d7e96a 2798 && _bfd_elf_is_ifunc_symbol (output_bfd, h)
d8045f23
NC
2799 && elf_section_data (input_section)->indirect_relocs != NULL
2800 && elf_section_data (input_section)->indirect_relocs->contents != NULL)
2801 sreloc = elf_section_data (input_section)->indirect_relocs;
2802 else
2803 sreloc = elf_section_data (input_section)->sreloc;
2804
e7c33416 2805 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
c434dee6 2806
947216bf
AM
2807 loc = sreloc->contents;
2808 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2809 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
70256ad8
AJ
2810
2811 /* If this reloc is against an external symbol, we do
2812 not want to fiddle with the addend. Otherwise, we
2813 need to include the symbol value so that it becomes
2814 an addend for the dynamic reloc. */
0f88be7a 2815 if (! relocate)
70256ad8
AJ
2816 continue;
2817 }
2818
2819 break;
2820
bffbf940 2821 case R_X86_64_TLSGD:
67a4f2b7
AO
2822 case R_X86_64_GOTPC32_TLSDESC:
2823 case R_X86_64_TLSDESC_CALL:
bffbf940 2824 case R_X86_64_GOTTPOFF:
bffbf940
JJ
2825 tls_type = GOT_UNKNOWN;
2826 if (h == NULL && local_got_offsets)
2827 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2828 else if (h != NULL)
142411ca
L
2829 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2830
2831 if (! elf64_x86_64_tls_transition (info, input_bfd,
2832 input_section, contents,
2833 symtab_hdr, sym_hashes,
2834 &r_type, tls_type, rel,
2835 relend, h))
534a31f6 2836 return FALSE;
bffbf940
JJ
2837
2838 if (r_type == R_X86_64_TPOFF32)
2839 {
142411ca
L
2840 bfd_vma roff = rel->r_offset;
2841
bffbf940 2842 BFD_ASSERT (! unresolved_reloc);
142411ca 2843
bffbf940
JJ
2844 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2845 {
bffbf940 2846 /* GD->LE transition.
abcf1d52 2847 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
a3fadc9a 2848 .word 0x6666; rex64; call __tls_get_addr
bffbf940
JJ
2849 Change it into:
2850 movq %fs:0, %rax
2851 leaq foo@tpoff(%rax), %rax */
142411ca 2852 memcpy (contents + roff - 4,
bffbf940
JJ
2853 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2854 16);
2855 bfd_put_32 (output_bfd, tpoff (info, relocation),
142411ca 2856 contents + roff + 8);
a3fadc9a 2857 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
bffbf940
JJ
2858 rel++;
2859 continue;
2860 }
67a4f2b7
AO
2861 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2862 {
2863 /* GDesc -> LE transition.
2864 It's originally something like:
2865 leaq x@tlsdesc(%rip), %rax
2866
2867 Change it to:
2868 movl $x@tpoff, %rax
142411ca 2869 */
67a4f2b7
AO
2870
2871 unsigned int val, type, type2;
67a4f2b7 2872
67a4f2b7 2873 type = bfd_get_8 (input_bfd, contents + roff - 3);
67a4f2b7 2874 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
67a4f2b7 2875 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
2876 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2877 contents + roff - 3);
2878 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2879 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2880 contents + roff - 1);
2881 bfd_put_32 (output_bfd, tpoff (info, relocation),
2882 contents + roff);
2883 continue;
2884 }
2885 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2886 {
2887 /* GDesc -> LE transition.
2888 It's originally:
2889 call *(%rax)
2890 Turn it into:
142411ca 2891 xchg %ax,%ax. */
10efb593 2892 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
2893 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2894 continue;
2895 }
142411ca 2896 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
bffbf940 2897 {
bffbf940
JJ
2898 /* IE->LE transition:
2899 Originally it can be one of:
2900 movq foo@gottpoff(%rip), %reg
2901 addq foo@gottpoff(%rip), %reg
2902 We change it into:
2903 movq $foo, %reg
2904 leaq foo(%reg), %reg
2905 addq $foo, %reg. */
142411ca
L
2906
2907 unsigned int val, type, reg;
2908
2909 val = bfd_get_8 (input_bfd, contents + roff - 3);
2910 type = bfd_get_8 (input_bfd, contents + roff - 2);
2911 reg = bfd_get_8 (input_bfd, contents + roff - 1);
bffbf940 2912 reg >>= 3;
bffbf940
JJ
2913 if (type == 0x8b)
2914 {
2915 /* movq */
2916 if (val == 0x4c)
2917 bfd_put_8 (output_bfd, 0x49,
142411ca 2918 contents + roff - 3);
bffbf940 2919 bfd_put_8 (output_bfd, 0xc7,
142411ca 2920 contents + roff - 2);
bffbf940 2921 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 2922 contents + roff - 1);
bffbf940
JJ
2923 }
2924 else if (reg == 4)
2925 {
2926 /* addq -> addq - addressing with %rsp/%r12 is
2927 special */
2928 if (val == 0x4c)
2929 bfd_put_8 (output_bfd, 0x49,
142411ca 2930 contents + roff - 3);
bffbf940 2931 bfd_put_8 (output_bfd, 0x81,
142411ca 2932 contents + roff - 2);
bffbf940 2933 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 2934 contents + roff - 1);
bffbf940
JJ
2935 }
2936 else
2937 {
2938 /* addq -> leaq */
2939 if (val == 0x4c)
2940 bfd_put_8 (output_bfd, 0x4d,
142411ca 2941 contents + roff - 3);
bffbf940 2942 bfd_put_8 (output_bfd, 0x8d,
142411ca 2943 contents + roff - 2);
bffbf940 2944 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
142411ca 2945 contents + roff - 1);
bffbf940
JJ
2946 }
2947 bfd_put_32 (output_bfd, tpoff (info, relocation),
142411ca 2948 contents + roff);
bffbf940
JJ
2949 continue;
2950 }
142411ca
L
2951 else
2952 BFD_ASSERT (FALSE);
bffbf940
JJ
2953 }
2954
2955 if (htab->sgot == NULL)
2956 abort ();
2957
2958 if (h != NULL)
67a4f2b7
AO
2959 {
2960 off = h->got.offset;
2961 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2962 }
bffbf940
JJ
2963 else
2964 {
2965 if (local_got_offsets == NULL)
2966 abort ();
2967
2968 off = local_got_offsets[r_symndx];
67a4f2b7 2969 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
2970 }
2971
2972 if ((off & 1) != 0)
2973 off &= ~1;
26e41594 2974 else
bffbf940
JJ
2975 {
2976 Elf_Internal_Rela outrel;
947216bf 2977 bfd_byte *loc;
bffbf940 2978 int dr_type, indx;
67a4f2b7 2979 asection *sreloc;
bffbf940
JJ
2980
2981 if (htab->srelgot == NULL)
2982 abort ();
2983
67a4f2b7
AO
2984 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2985
2986 if (GOT_TLS_GDESC_P (tls_type))
2987 {
2988 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2989 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2990 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2991 outrel.r_offset = (htab->sgotplt->output_section->vma
2992 + htab->sgotplt->output_offset
2993 + offplt
2994 + htab->sgotplt_jump_table_size);
2995 sreloc = htab->srelplt;
2996 loc = sreloc->contents;
2997 loc += sreloc->reloc_count++
2998 * sizeof (Elf64_External_Rela);
2999 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3000 <= sreloc->contents + sreloc->size);
3001 if (indx == 0)
3002 outrel.r_addend = relocation - dtpoff_base (info);
3003 else
3004 outrel.r_addend = 0;
3005 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3006 }
3007
3008 sreloc = htab->srelgot;
3009
bffbf940
JJ
3010 outrel.r_offset = (htab->sgot->output_section->vma
3011 + htab->sgot->output_offset + off);
3012
67a4f2b7 3013 if (GOT_TLS_GD_P (tls_type))
bffbf940 3014 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
3015 else if (GOT_TLS_GDESC_P (tls_type))
3016 goto dr_done;
bffbf940
JJ
3017 else
3018 dr_type = R_X86_64_TPOFF64;
3019
3020 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
3021 outrel.r_addend = 0;
67a4f2b7
AO
3022 if ((dr_type == R_X86_64_TPOFF64
3023 || dr_type == R_X86_64_TLSDESC) && indx == 0)
bffbf940
JJ
3024 outrel.r_addend = relocation - dtpoff_base (info);
3025 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3026
67a4f2b7
AO
3027 loc = sreloc->contents;
3028 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3029 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3030 <= sreloc->contents + sreloc->size);
bffbf940
JJ
3031 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3032
67a4f2b7 3033 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
3034 {
3035 if (indx == 0)
3036 {
d40d037c 3037 BFD_ASSERT (! unresolved_reloc);
bffbf940
JJ
3038 bfd_put_64 (output_bfd,
3039 relocation - dtpoff_base (info),
3040 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3041 }
3042 else
3043 {
3044 bfd_put_64 (output_bfd, 0,
3045 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3046 outrel.r_info = ELF64_R_INFO (indx,
3047 R_X86_64_DTPOFF64);
3048 outrel.r_offset += GOT_ENTRY_SIZE;
67a4f2b7 3049 sreloc->reloc_count++;
947216bf 3050 loc += sizeof (Elf64_External_Rela);
67a4f2b7
AO
3051 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3052 <= sreloc->contents + sreloc->size);
947216bf 3053 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
bffbf940
JJ
3054 }
3055 }
3056
67a4f2b7 3057 dr_done:
bffbf940
JJ
3058 if (h != NULL)
3059 h->got.offset |= 1;
3060 else
3061 local_got_offsets[r_symndx] |= 1;
3062 }
3063
67a4f2b7
AO
3064 if (off >= (bfd_vma) -2
3065 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940
JJ
3066 abort ();
3067 if (r_type == ELF64_R_TYPE (rel->r_info))
3068 {
67a4f2b7
AO
3069 if (r_type == R_X86_64_GOTPC32_TLSDESC
3070 || r_type == R_X86_64_TLSDESC_CALL)
3071 relocation = htab->sgotplt->output_section->vma
3072 + htab->sgotplt->output_offset
3073 + offplt + htab->sgotplt_jump_table_size;
3074 else
3075 relocation = htab->sgot->output_section->vma
3076 + htab->sgot->output_offset + off;
b34976b6 3077 unresolved_reloc = FALSE;
bffbf940 3078 }
142411ca 3079 else
67a4f2b7 3080 {
142411ca 3081 bfd_vma roff = rel->r_offset;
67a4f2b7 3082
142411ca
L
3083 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3084 {
3085 /* GD->IE transition.
3086 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3087 .word 0x6666; rex64; call __tls_get_addr@plt
3088 Change it into:
3089 movq %fs:0, %rax
3090 addq foo@gottpoff(%rip), %rax */
3091 memcpy (contents + roff - 4,
3092 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3093 16);
3094
3095 relocation = (htab->sgot->output_section->vma
3096 + htab->sgot->output_offset + off
3097 - roff
3098 - input_section->output_section->vma
3099 - input_section->output_offset
3100 - 12);
3101 bfd_put_32 (output_bfd, relocation,
3102 contents + roff + 8);
3103 /* Skip R_X86_64_PLT32. */
3104 rel++;
3105 continue;
3106 }
3107 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3108 {
3109 /* GDesc -> IE transition.
3110 It's originally something like:
3111 leaq x@tlsdesc(%rip), %rax
67a4f2b7 3112
142411ca
L
3113 Change it to:
3114 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3115 */
67a4f2b7 3116
142411ca 3117 unsigned int val, type, type2;
67a4f2b7 3118
142411ca
L
3119 type = bfd_get_8 (input_bfd, contents + roff - 3);
3120 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3121 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7 3122
142411ca
L
3123 /* Now modify the instruction as appropriate. To
3124 turn a leaq into a movq in the form we use it, it
3125 suffices to change the second byte from 0x8d to
3126 0x8b. */
3127 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3128
3129 bfd_put_32 (output_bfd,
3130 htab->sgot->output_section->vma
3131 + htab->sgot->output_offset + off
3132 - rel->r_offset
3133 - input_section->output_section->vma
3134 - input_section->output_offset
3135 - 4,
3136 contents + roff);
3137 continue;
3138 }
3139 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3140 {
3141 /* GDesc -> IE transition.
3142 It's originally:
3143 call *(%rax)
3144
3145 Change it to:
3146 xchg %ax,%ax. */
3147
3148 unsigned int val, type;
3149
3150 type = bfd_get_8 (input_bfd, contents + roff);
3151 val = bfd_get_8 (input_bfd, contents + roff + 1);
3152 bfd_put_8 (output_bfd, 0x66, contents + roff);
3153 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3154 continue;
3155 }
3156 else
3157 BFD_ASSERT (FALSE);
67a4f2b7 3158 }
bffbf940
JJ
3159 break;
3160
3161 case R_X86_64_TLSLD:
142411ca
L
3162 if (! elf64_x86_64_tls_transition (info, input_bfd,
3163 input_section, contents,
3164 symtab_hdr, sym_hashes,
3165 &r_type, GOT_UNKNOWN,
3166 rel, relend, h))
3167 return FALSE;
a3fadc9a 3168
142411ca
L
3169 if (r_type != R_X86_64_TLSLD)
3170 {
bffbf940 3171 /* LD->LE transition:
a3fadc9a 3172 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
bffbf940
JJ
3173 We change it into:
3174 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
142411ca
L
3175
3176 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
bffbf940
JJ
3177 memcpy (contents + rel->r_offset - 3,
3178 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
a3fadc9a 3179 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
bffbf940
JJ
3180 rel++;
3181 continue;
3182 }
3183
3184 if (htab->sgot == NULL)
3185 abort ();
3186
3187 off = htab->tls_ld_got.offset;
3188 if (off & 1)
3189 off &= ~1;
3190 else
3191 {
3192 Elf_Internal_Rela outrel;
947216bf 3193 bfd_byte *loc;
bffbf940
JJ
3194
3195 if (htab->srelgot == NULL)
3196 abort ();
3197
3198 outrel.r_offset = (htab->sgot->output_section->vma
3199 + htab->sgot->output_offset + off);
3200
3201 bfd_put_64 (output_bfd, 0,
3202 htab->sgot->contents + off);
3203 bfd_put_64 (output_bfd, 0,
3204 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3205 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3206 outrel.r_addend = 0;
947216bf
AM
3207 loc = htab->srelgot->contents;
3208 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
bffbf940
JJ
3209 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3210 htab->tls_ld_got.offset |= 1;
3211 }
3212 relocation = htab->sgot->output_section->vma
3213 + htab->sgot->output_offset + off;
b34976b6 3214 unresolved_reloc = FALSE;
bffbf940
JJ
3215 break;
3216
3217 case R_X86_64_DTPOFF32:
a45bb67d 3218 if (info->shared || (input_section->flags & SEC_CODE) == 0)
bffbf940
JJ
3219 relocation -= dtpoff_base (info);
3220 else
3221 relocation = tpoff (info, relocation);
3222 break;
3223
3224 case R_X86_64_TPOFF32:
3225 BFD_ASSERT (! info->shared);
3226 relocation = tpoff (info, relocation);
3227 break;
3228
70256ad8
AJ
3229 default:
3230 break;
3231 }
8d88c4ca 3232
239e1f3a
AM
3233 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3234 because such sections are not SEC_ALLOC and thus ld.so will
3235 not process them. */
c434dee6 3236 if (unresolved_reloc
239e1f3a 3237 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3238 && h->def_dynamic))
c434dee6 3239 (*_bfd_error_handler)
843fe662 3240 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
3241 input_bfd,
3242 input_section,
c434dee6 3243 (long) rel->r_offset,
843fe662 3244 howto->name,
c434dee6
AJ
3245 h->root.root.string);
3246
8d88c4ca 3247 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
3248 contents, rel->r_offset,
3249 relocation, rel->r_addend);
8d88c4ca
NC
3250
3251 if (r != bfd_reloc_ok)
8da6118f 3252 {
c434dee6
AJ
3253 const char *name;
3254
3255 if (h != NULL)
3256 name = h->root.root.string;
3257 else
8da6118f 3258 {
c434dee6
AJ
3259 name = bfd_elf_string_from_elf_section (input_bfd,
3260 symtab_hdr->sh_link,
3261 sym->st_name);
3262 if (name == NULL)
b34976b6 3263 return FALSE;
c434dee6
AJ
3264 if (*name == '\0')
3265 name = bfd_section_name (input_bfd, sec);
3266 }
3267
3268 if (r == bfd_reloc_overflow)
3269 {
c434dee6 3270 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
3271 (info, (h ? &h->root : NULL), name, howto->name,
3272 (bfd_vma) 0, input_bfd, input_section,
3273 rel->r_offset)))
b34976b6 3274 return FALSE;
c434dee6
AJ
3275 }
3276 else
3277 {
3278 (*_bfd_error_handler)
d003868e
AM
3279 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3280 input_bfd, input_section,
c434dee6 3281 (long) rel->r_offset, name, (int) r);
b34976b6 3282 return FALSE;
8da6118f
KH
3283 }
3284 }
8d88c4ca 3285 }
70256ad8 3286
b34976b6 3287 return TRUE;
70256ad8
AJ
3288}
3289
3290/* Finish up dynamic symbol handling. We set the contents of various
3291 dynamic sections here. */
3292
b34976b6 3293static bfd_boolean
27482721
AJ
3294elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3295 struct bfd_link_info *info,
3296 struct elf_link_hash_entry *h,
3297 Elf_Internal_Sym *sym)
70256ad8 3298{
c434dee6 3299 struct elf64_x86_64_link_hash_table *htab;
70256ad8 3300
c434dee6 3301 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
3302
3303 if (h->plt.offset != (bfd_vma) -1)
3304 {
70256ad8
AJ
3305 bfd_vma plt_index;
3306 bfd_vma got_offset;
3307 Elf_Internal_Rela rela;
947216bf 3308 bfd_byte *loc;
70256ad8
AJ
3309
3310 /* This symbol has an entry in the procedure linkage table. Set
407443a3 3311 it up. */
c434dee6
AJ
3312 if (h->dynindx == -1
3313 || htab->splt == NULL
3314 || htab->sgotplt == NULL
3315 || htab->srelplt == NULL)
3316 abort ();
70256ad8
AJ
3317
3318 /* Get the index in the procedure linkage table which
3319 corresponds to this symbol. This is the index of this symbol
3320 in all the symbols for which we are making plt entries. The
3321 first entry in the procedure linkage table is reserved. */
3322 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3323
3324 /* Get the offset into the .got table of the entry that
407443a3 3325 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
fe4770f4 3326 bytes. The first three are reserved for the dynamic linker. */
70256ad8
AJ
3327 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3328
3329 /* Fill in the entry in the procedure linkage table. */
c434dee6 3330 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
70256ad8
AJ
3331 PLT_ENTRY_SIZE);
3332
3333 /* Insert the relocation positions of the plt section. The magic
3334 numbers at the end of the statements are the positions of the
3335 relocations in the plt section. */
653165cc
AJ
3336 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3337 instruction uses 6 bytes, subtract this value. */
3338 bfd_put_32 (output_bfd,
c434dee6
AJ
3339 (htab->sgotplt->output_section->vma
3340 + htab->sgotplt->output_offset
653165cc 3341 + got_offset
c434dee6
AJ
3342 - htab->splt->output_section->vma
3343 - htab->splt->output_offset
653165cc
AJ
3344 - h->plt.offset
3345 - 6),
c434dee6 3346 htab->splt->contents + h->plt.offset + 2);
653165cc
AJ
3347 /* Put relocation index. */
3348 bfd_put_32 (output_bfd, plt_index,
c434dee6 3349 htab->splt->contents + h->plt.offset + 7);
653165cc
AJ
3350 /* Put offset for jmp .PLT0. */
3351 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
c434dee6 3352 htab->splt->contents + h->plt.offset + 12);
70256ad8 3353
653165cc
AJ
3354 /* Fill in the entry in the global offset table, initially this
3355 points to the pushq instruction in the PLT which is at offset 6. */
c434dee6
AJ
3356 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3357 + htab->splt->output_offset
70256ad8 3358 + h->plt.offset + 6),
c434dee6 3359 htab->sgotplt->contents + got_offset);
70256ad8
AJ
3360
3361 /* Fill in the entry in the .rela.plt section. */
c434dee6
AJ
3362 rela.r_offset = (htab->sgotplt->output_section->vma
3363 + htab->sgotplt->output_offset
70256ad8
AJ
3364 + got_offset);
3365 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3366 rela.r_addend = 0;
947216bf 3367 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
c434dee6 3368 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8 3369
f5385ebf 3370 if (!h->def_regular)
70256ad8
AJ
3371 {
3372 /* Mark the symbol as undefined, rather than as defined in
47a9f7b3
JJ
3373 the .plt section. Leave the value if there were any
3374 relocations where pointer equality matters (this is a clue
c434dee6
AJ
3375 for the dynamic linker, to make function pointer
3376 comparisons work between an application and shared
47a9f7b3
JJ
3377 library), otherwise set it to zero. If a function is only
3378 called from a binary, there is no need to slow down
3379 shared libraries because of that. */
70256ad8 3380 sym->st_shndx = SHN_UNDEF;
f5385ebf 3381 if (!h->pointer_equality_needed)
47a9f7b3 3382 sym->st_value = 0;
70256ad8
AJ
3383 }
3384 }
3385
bffbf940 3386 if (h->got.offset != (bfd_vma) -1
67a4f2b7 3387 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
bffbf940 3388 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
053579d7 3389 {
053579d7 3390 Elf_Internal_Rela rela;
947216bf 3391 bfd_byte *loc;
053579d7
AJ
3392
3393 /* This symbol has an entry in the global offset table. Set it
bffbf940 3394 up. */
c434dee6
AJ
3395 if (htab->sgot == NULL || htab->srelgot == NULL)
3396 abort ();
053579d7 3397
c434dee6
AJ
3398 rela.r_offset = (htab->sgot->output_section->vma
3399 + htab->sgot->output_offset
dc810e39 3400 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
3401
3402 /* If this is a static link, or it is a -Bsymbolic link and the
3403 symbol is defined locally or was forced to be local because
3404 of a version file, we just want to emit a RELATIVE reloc.
3405 The entry in the global offset table will already have been
3406 initialized in the relocate_section function. */
c434dee6 3407 if (info->shared
27482721 3408 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 3409 {
41bed6dd
L
3410 if (!h->def_regular)
3411 return FALSE;
cc78d0af 3412 BFD_ASSERT((h->got.offset & 1) != 0);
053579d7
AJ
3413 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3414 rela.r_addend = (h->root.u.def.value
3415 + h->root.u.def.section->output_section->vma
3416 + h->root.u.def.section->output_offset);
3417 }
3418 else
3419 {
3420 BFD_ASSERT((h->got.offset & 1) == 0);
c434dee6
AJ
3421 bfd_put_64 (output_bfd, (bfd_vma) 0,
3422 htab->sgot->contents + h->got.offset);
053579d7
AJ
3423 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3424 rela.r_addend = 0;
3425 }
3426
947216bf
AM
3427 loc = htab->srelgot->contents;
3428 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 3429 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
053579d7
AJ
3430 }
3431
f5385ebf 3432 if (h->needs_copy)
70256ad8 3433 {
70256ad8 3434 Elf_Internal_Rela rela;
947216bf 3435 bfd_byte *loc;
70256ad8
AJ
3436
3437 /* This symbol needs a copy reloc. Set it up. */
3438
c434dee6
AJ
3439 if (h->dynindx == -1
3440 || (h->root.type != bfd_link_hash_defined
3441 && h->root.type != bfd_link_hash_defweak)
3442 || htab->srelbss == NULL)
3443 abort ();
70256ad8
AJ
3444
3445 rela.r_offset = (h->root.u.def.value
3446 + h->root.u.def.section->output_section->vma
3447 + h->root.u.def.section->output_offset);
3448 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3449 rela.r_addend = 0;
947216bf
AM
3450 loc = htab->srelbss->contents;
3451 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 3452 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8
AJ
3453 }
3454
3455 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3456 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1 3457 || h == htab->elf.hgot)
70256ad8
AJ
3458 sym->st_shndx = SHN_ABS;
3459
b34976b6 3460 return TRUE;
70256ad8
AJ
3461}
3462
c434dee6
AJ
3463/* Used to decide how to sort relocs in an optimal manner for the
3464 dynamic linker, before writing them out. */
3465
3466static enum elf_reloc_type_class
27482721 3467elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
c434dee6
AJ
3468{
3469 switch ((int) ELF64_R_TYPE (rela->r_info))
3470 {
3471 case R_X86_64_RELATIVE:
3472 return reloc_class_relative;
3473 case R_X86_64_JUMP_SLOT:
3474 return reloc_class_plt;
3475 case R_X86_64_COPY:
3476 return reloc_class_copy;
3477 default:
3478 return reloc_class_normal;
3479 }
3480}
3481
70256ad8
AJ
3482/* Finish up the dynamic sections. */
3483
b34976b6 3484static bfd_boolean
27482721 3485elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
70256ad8 3486{
c434dee6 3487 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
3488 bfd *dynobj;
3489 asection *sdyn;
70256ad8 3490
c434dee6
AJ
3491 htab = elf64_x86_64_hash_table (info);
3492 dynobj = htab->elf.dynobj;
70256ad8
AJ
3493 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3494
c434dee6 3495 if (htab->elf.dynamic_sections_created)
70256ad8 3496 {
70256ad8
AJ
3497 Elf64_External_Dyn *dyncon, *dynconend;
3498
c434dee6
AJ
3499 if (sdyn == NULL || htab->sgot == NULL)
3500 abort ();
70256ad8
AJ
3501
3502 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 3503 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
70256ad8
AJ
3504 for (; dyncon < dynconend; dyncon++)
3505 {
3506 Elf_Internal_Dyn dyn;
70256ad8
AJ
3507 asection *s;
3508
3509 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3510
3511 switch (dyn.d_tag)
3512 {
3513 default:
053579d7 3514 continue;
70256ad8
AJ
3515
3516 case DT_PLTGOT:
8c37241b
JJ
3517 s = htab->sgotplt;
3518 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 3519 break;
70256ad8
AJ
3520
3521 case DT_JMPREL:
c434dee6
AJ
3522 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3523 break;
70256ad8 3524
c434dee6
AJ
3525 case DT_PLTRELSZ:
3526 s = htab->srelplt->output_section;
eea6121a 3527 dyn.d_un.d_val = s->size;
70256ad8
AJ
3528 break;
3529
3530 case DT_RELASZ:
c434dee6
AJ
3531 /* The procedure linkage table relocs (DT_JMPREL) should
3532 not be included in the overall relocs (DT_RELA).
3533 Therefore, we override the DT_RELASZ entry here to
3534 make it not include the JMPREL relocs. Since the
3535 linker script arranges for .rela.plt to follow all
3536 other relocation sections, we don't have to worry
3537 about changing the DT_RELA entry. */
3538 if (htab->srelplt != NULL)
70256ad8 3539 {
c434dee6 3540 s = htab->srelplt->output_section;
eea6121a 3541 dyn.d_un.d_val -= s->size;
70256ad8
AJ
3542 }
3543 break;
67a4f2b7
AO
3544
3545 case DT_TLSDESC_PLT:
3546 s = htab->splt;
3547 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3548 + htab->tlsdesc_plt;
3549 break;
3550
3551 case DT_TLSDESC_GOT:
3552 s = htab->sgot;
3553 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3554 + htab->tlsdesc_got;
3555 break;
70256ad8 3556 }
c434dee6 3557
70256ad8
AJ
3558 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3559 }
3560
c434dee6 3561 /* Fill in the special first entry in the procedure linkage table. */
eea6121a 3562 if (htab->splt && htab->splt->size > 0)
70256ad8 3563 {
653165cc 3564 /* Fill in the first entry in the procedure linkage table. */
c434dee6
AJ
3565 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3566 PLT_ENTRY_SIZE);
653165cc
AJ
3567 /* Add offset for pushq GOT+8(%rip), since the instruction
3568 uses 6 bytes subtract this value. */
3569 bfd_put_32 (output_bfd,
c434dee6
AJ
3570 (htab->sgotplt->output_section->vma
3571 + htab->sgotplt->output_offset
653165cc 3572 + 8
c434dee6
AJ
3573 - htab->splt->output_section->vma
3574 - htab->splt->output_offset
653165cc 3575 - 6),
c434dee6 3576 htab->splt->contents + 2);
653165cc
AJ
3577 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3578 the end of the instruction. */
3579 bfd_put_32 (output_bfd,
c434dee6
AJ
3580 (htab->sgotplt->output_section->vma
3581 + htab->sgotplt->output_offset
653165cc 3582 + 16
c434dee6
AJ
3583 - htab->splt->output_section->vma
3584 - htab->splt->output_offset
653165cc 3585 - 12),
c434dee6 3586 htab->splt->contents + 8);
653165cc 3587
c434dee6
AJ
3588 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3589 PLT_ENTRY_SIZE;
67a4f2b7
AO
3590
3591 if (htab->tlsdesc_plt)
3592 {
3593 bfd_put_64 (output_bfd, (bfd_vma) 0,
3594 htab->sgot->contents + htab->tlsdesc_got);
3595
3596 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3597 elf64_x86_64_plt0_entry,
3598 PLT_ENTRY_SIZE);
3599
3600 /* Add offset for pushq GOT+8(%rip), since the
3601 instruction uses 6 bytes subtract this value. */
3602 bfd_put_32 (output_bfd,
3603 (htab->sgotplt->output_section->vma
3604 + htab->sgotplt->output_offset
3605 + 8
3606 - htab->splt->output_section->vma
3607 - htab->splt->output_offset
3608 - htab->tlsdesc_plt
3609 - 6),
3610 htab->splt->contents + htab->tlsdesc_plt + 2);
3611 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3612 htab->tlsdesc_got. The 12 is the offset to the end of
3613 the instruction. */
3614 bfd_put_32 (output_bfd,
3615 (htab->sgot->output_section->vma
3616 + htab->sgot->output_offset
3617 + htab->tlsdesc_got
3618 - htab->splt->output_section->vma
3619 - htab->splt->output_offset
3620 - htab->tlsdesc_plt
3621 - 12),
3622 htab->splt->contents + htab->tlsdesc_plt + 8);
3623 }
70256ad8 3624 }
70256ad8
AJ
3625 }
3626
c434dee6 3627 if (htab->sgotplt)
70256ad8 3628 {
c434dee6 3629 /* Fill in the first three entries in the global offset table. */
eea6121a 3630 if (htab->sgotplt->size > 0)
c434dee6
AJ
3631 {
3632 /* Set the first entry in the global offset table to the address of
3633 the dynamic section. */
3634 if (sdyn == NULL)
3635 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3636 else
3637 bfd_put_64 (output_bfd,
3638 sdyn->output_section->vma + sdyn->output_offset,
3639 htab->sgotplt->contents);
3640 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3641 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3642 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3643 }
70256ad8 3644
c434dee6
AJ
3645 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3646 GOT_ENTRY_SIZE;
3647 }
70256ad8 3648
eea6121a 3649 if (htab->sgot && htab->sgot->size > 0)
8c37241b
JJ
3650 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3651 = GOT_ENTRY_SIZE;
3652
b34976b6 3653 return TRUE;
8d88c4ca
NC
3654}
3655
4c45e5c9
JJ
3656/* Return address for Ith PLT stub in section PLT, for relocation REL
3657 or (bfd_vma) -1 if it should not be included. */
3658
3659static bfd_vma
3660elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3661 const arelent *rel ATTRIBUTE_UNUSED)
3662{
3663 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3664}
8df9fc9d 3665
d2b2c203
DJ
3666/* Handle an x86-64 specific section when reading an object file. This
3667 is called when elfcode.h finds a section with an unknown type. */
3668
3669static bfd_boolean
6dc132d9
L
3670elf64_x86_64_section_from_shdr (bfd *abfd,
3671 Elf_Internal_Shdr *hdr,
3672 const char *name,
3673 int shindex)
d2b2c203
DJ
3674{
3675 if (hdr->sh_type != SHT_X86_64_UNWIND)
3676 return FALSE;
3677
6dc132d9 3678 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
3679 return FALSE;
3680
3681 return TRUE;
3682}
3683
3b22753a
L
3684/* Hook called by the linker routine which adds symbols from an object
3685 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3686 of .bss. */
3687
3688static bfd_boolean
3689elf64_x86_64_add_symbol_hook (bfd *abfd,
d8045f23 3690 struct bfd_link_info *info,
3b22753a
L
3691 Elf_Internal_Sym *sym,
3692 const char **namep ATTRIBUTE_UNUSED,
3693 flagword *flagsp ATTRIBUTE_UNUSED,
d8045f23
NC
3694 asection **secp,
3695 bfd_vma *valp)
3b22753a
L
3696{
3697 asection *lcomm;
3698
3699 switch (sym->st_shndx)
3700 {
3701 case SHN_X86_64_LCOMMON:
3702 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3703 if (lcomm == NULL)
3704 {
3705 lcomm = bfd_make_section_with_flags (abfd,
3706 "LARGE_COMMON",
3707 (SEC_ALLOC
3708 | SEC_IS_COMMON
3709 | SEC_LINKER_CREATED));
3710 if (lcomm == NULL)
3711 return FALSE;
3712 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3713 }
3714 *secp = lcomm;
3715 *valp = sym->st_size;
3716 break;
3717 }
d8045f23
NC
3718
3719 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3720 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
3721
3b22753a
L
3722 return TRUE;
3723}
3724
3725
3726/* Given a BFD section, try to locate the corresponding ELF section
3727 index. */
3728
3729static bfd_boolean
3730elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3731 asection *sec, int *index)
3732{
3733 if (sec == &_bfd_elf_large_com_section)
3734 {
3735 *index = SHN_X86_64_LCOMMON;
3736 return TRUE;
3737 }
3738 return FALSE;
3739}
3740
3741/* Process a symbol. */
3742
3743static void
3744elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3745 asymbol *asym)
3746{
3747 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3748
3749 switch (elfsym->internal_elf_sym.st_shndx)
3750 {
3751 case SHN_X86_64_LCOMMON:
3752 asym->section = &_bfd_elf_large_com_section;
3753 asym->value = elfsym->internal_elf_sym.st_size;
3754 /* Common symbol doesn't set BSF_GLOBAL. */
3755 asym->flags &= ~BSF_GLOBAL;
3756 break;
3757 }
3758}
3759
3760static bfd_boolean
3761elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3762{
3763 return (sym->st_shndx == SHN_COMMON
3764 || sym->st_shndx == SHN_X86_64_LCOMMON);
3765}
3766
3767static unsigned int
3768elf64_x86_64_common_section_index (asection *sec)
3769{
3770 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3771 return SHN_COMMON;
3772 else
3773 return SHN_X86_64_LCOMMON;
3774}
3775
3776static asection *
3777elf64_x86_64_common_section (asection *sec)
3778{
3779 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3780 return bfd_com_section_ptr;
3781 else
3782 return &_bfd_elf_large_com_section;
3783}
3784
3785static bfd_boolean
3786elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3787 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3788 struct elf_link_hash_entry *h,
3789 Elf_Internal_Sym *sym,
00492999 3790 asection **psec,
3b22753a
L
3791 bfd_vma *pvalue ATTRIBUTE_UNUSED,
3792 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3793 bfd_boolean *skip ATTRIBUTE_UNUSED,
3794 bfd_boolean *override ATTRIBUTE_UNUSED,
3795 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3796 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3797 bfd_boolean *newdef ATTRIBUTE_UNUSED,
3798 bfd_boolean *newdyn,
3799 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3800 bfd_boolean *newweak ATTRIBUTE_UNUSED,
3801 bfd *abfd ATTRIBUTE_UNUSED,
3802 asection **sec,
3803 bfd_boolean *olddef ATTRIBUTE_UNUSED,
3804 bfd_boolean *olddyn,
3805 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3806 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
00492999 3807 bfd *oldbfd,
3b22753a
L
3808 asection **oldsec)
3809{
3810 /* A normal common symbol and a large common symbol result in a
00492999
L
3811 normal common symbol. We turn the large common symbol into a
3812 normal one. */
3b22753a
L
3813 if (!*olddyn
3814 && h->root.type == bfd_link_hash_common
3815 && !*newdyn
3816 && bfd_is_com_section (*sec)
00492999 3817 && *oldsec != *sec)
3b22753a 3818 {
00492999
L
3819 if (sym->st_shndx == SHN_COMMON
3820 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3821 {
3822 h->root.u.c.p->section
3823 = bfd_make_section_old_way (oldbfd, "COMMON");
3824 h->root.u.c.p->section->flags = SEC_ALLOC;
3825 }
3826 else if (sym->st_shndx == SHN_X86_64_LCOMMON
3827 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
9a2e389a 3828 *psec = *sec = bfd_com_section_ptr;
3b22753a
L
3829 }
3830
3831 return TRUE;
3832}
3833
3834static int
a6b96beb
AM
3835elf64_x86_64_additional_program_headers (bfd *abfd,
3836 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
3837{
3838 asection *s;
9a2e389a 3839 int count = 0;
3b22753a
L
3840
3841 /* Check to see if we need a large readonly segment. */
3842 s = bfd_get_section_by_name (abfd, ".lrodata");
3843 if (s && (s->flags & SEC_LOAD))
3844 count++;
3845
3846 /* Check to see if we need a large data segment. Since .lbss sections
3847 is placed right after the .bss section, there should be no need for
3848 a large data segment just because of .lbss. */
3849 s = bfd_get_section_by_name (abfd, ".ldata");
3850 if (s && (s->flags & SEC_LOAD))
3851 count++;
3852
3853 return count;
3854}
3855
fdc90cb4
JJ
3856/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3857
3858static bfd_boolean
3859elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
3860{
3861 if (h->plt.offset != (bfd_vma) -1
3862 && !h->def_regular
3863 && !h->pointer_equality_needed)
3864 return FALSE;
3865
3866 return _bfd_elf_hash_symbol (h);
3867}
3868
9a2e389a 3869static const struct bfd_elf_special_section
3b22753a
L
3870 elf64_x86_64_special_sections[]=
3871{
0112cd26
NC
3872 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3873 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3874 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3875 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3876 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3877 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3878 { NULL, 0, 0, 0, 0 }
3b22753a
L
3879};
3880
70256ad8
AJ
3881#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3882#define TARGET_LITTLE_NAME "elf64-x86-64"
3883#define ELF_ARCH bfd_arch_i386
3884#define ELF_MACHINE_CODE EM_X86_64
f7661549 3885#define ELF_MAXPAGESIZE 0x200000
2043964e 3886#define ELF_MINPAGESIZE 0x1000
24718e3b 3887#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
3888
3889#define elf_backend_can_gc_sections 1
51b64d56 3890#define elf_backend_can_refcount 1
70256ad8
AJ
3891#define elf_backend_want_got_plt 1
3892#define elf_backend_plt_readonly 1
3893#define elf_backend_want_plt_sym 0
3894#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 3895#define elf_backend_rela_normal 1
70256ad8
AJ
3896
3897#define elf_info_to_howto elf64_x86_64_info_to_howto
70256ad8 3898
70256ad8
AJ
3899#define bfd_elf64_bfd_link_hash_table_create \
3900 elf64_x86_64_link_hash_table_create
407443a3 3901#define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
157090f7
AM
3902#define bfd_elf64_bfd_reloc_name_lookup \
3903 elf64_x86_64_reloc_name_lookup
70256ad8
AJ
3904
3905#define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
13285a1b 3906#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
70256ad8 3907#define elf_backend_check_relocs elf64_x86_64_check_relocs
c434dee6
AJ
3908#define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3909#define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3910#define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
70256ad8
AJ
3911#define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3912#define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3913#define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3bab7989
ML
3914#define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3915#define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
c434dee6 3916#define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
70256ad8
AJ
3917#define elf_backend_relocate_section elf64_x86_64_relocate_section
3918#define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
67a4f2b7 3919#define elf_backend_always_size_sections elf64_x86_64_always_size_sections
74541ad4 3920#define elf_backend_init_index_section _bfd_elf_init_1_index_section
4c45e5c9 3921#define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
407443a3 3922#define elf_backend_object_p elf64_x86_64_elf_object_p
bffbf940 3923#define bfd_elf64_mkobject elf64_x86_64_mkobject
8d88c4ca 3924
d2b2c203
DJ
3925#define elf_backend_section_from_shdr \
3926 elf64_x86_64_section_from_shdr
3927
3b22753a
L
3928#define elf_backend_section_from_bfd_section \
3929 elf64_x86_64_elf_section_from_bfd_section
3930#define elf_backend_add_symbol_hook \
3931 elf64_x86_64_add_symbol_hook
3932#define elf_backend_symbol_processing \
3933 elf64_x86_64_symbol_processing
3934#define elf_backend_common_section_index \
3935 elf64_x86_64_common_section_index
3936#define elf_backend_common_section \
3937 elf64_x86_64_common_section
3938#define elf_backend_common_definition \
3939 elf64_x86_64_common_definition
3940#define elf_backend_merge_symbol \
3941 elf64_x86_64_merge_symbol
3942#define elf_backend_special_sections \
3943 elf64_x86_64_special_sections
3944#define elf_backend_additional_program_headers \
3945 elf64_x86_64_additional_program_headers
fdc90cb4
JJ
3946#define elf_backend_hash_symbol \
3947 elf64_x86_64_hash_symbol
3b22753a 3948
d8045f23
NC
3949#undef elf_backend_post_process_headers
3950#define elf_backend_post_process_headers _bfd_elf_set_osabi
3951
8d88c4ca 3952#include "elf64-target.h"
9d7cbccd
NC
3953
3954/* FreeBSD support. */
3955
3956#undef TARGET_LITTLE_SYM
3957#define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3958#undef TARGET_LITTLE_NAME
3959#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3960
d1036acb
L
3961#undef ELF_OSABI
3962#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd 3963
9d7cbccd
NC
3964#undef elf64_bed
3965#define elf64_bed elf64_x86_64_fbsd_bed
3966
3967#include "elf64-target.h"
This page took 0.753248 seconds and 4 git commands to generate.