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