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