gdb/riscv: Remove CSR feature file
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
351f65ca 1/* X86-64 specific support for ELF
b3adc24a 2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
8d88c4ca
NC
3 Contributed by Jan Hubicka <jh@suse.cz>.
4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
8d88c4ca 6
ae9a127f
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
ae9a127f 10 (at your option) any later version.
8d88c4ca 11
ae9a127f
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
8d88c4ca 16
ae9a127f
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
8d88c4ca 21
0afcef53 22#include "elfxx-x86.h"
5a68afcf 23#include "elf-nacl.h"
e41b3a13 24#include "dwarf2.h"
d7921315 25#include "libiberty.h"
8d88c4ca 26
56ceb5b5 27#include "opcode/i386.h"
8d88c4ca
NC
28#include "elf/x86-64.h"
29
8fd79e71
L
30#ifdef CORE_HEADER
31#include <stdarg.h>
32#include CORE_HEADER
33#endif
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
8d88c4ca 43/* The relocation "howto" table. Order of fields:
7b81dfbb
AJ
44 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
45 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
70256ad8
AJ
46static reloc_howto_type x86_64_elf_howto_table[] =
47{
6346d5ca 48 HOWTO(R_X86_64_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
b34976b6
AM
49 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
50 FALSE),
51 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
52 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
53 FALSE),
54 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
55 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
56 TRUE),
57 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
58 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
59 FALSE),
60 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
61 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
62 TRUE),
63 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
65 FALSE),
66 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
67 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
68 MINUS_ONE, FALSE),
69 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
70 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
71 MINUS_ONE, FALSE),
72 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
73 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
74 MINUS_ONE, FALSE),
75 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
76 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
77 0xffffffff, TRUE),
78 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
79 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
80 FALSE),
81 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
82 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
83 FALSE),
84 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
b0360d8c 86 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
b34976b6 87 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
ac2aa337 88 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
b34976b6
AM
89 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
90 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
91 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
92 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
93 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
94 MINUS_ONE, FALSE),
95 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
96 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
97 MINUS_ONE, FALSE),
98 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
99 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
100 MINUS_ONE, FALSE),
101 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
102 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
103 0xffffffff, TRUE),
104 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
105 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
106 0xffffffff, TRUE),
ac2aa337 107 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
b34976b6
AM
108 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
109 0xffffffff, FALSE),
110 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
111 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
112 0xffffffff, TRUE),
113 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
114 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
115 0xffffffff, FALSE),
d6ab8113
JB
116 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
117 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
118 TRUE),
119 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
120 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
121 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
122 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
123 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
124 FALSE, 0xffffffff, 0xffffffff, TRUE),
7b81dfbb
AJ
125 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
126 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
127 FALSE),
128 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
129 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
130 MINUS_ONE, TRUE),
131 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
132 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
133 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
134 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
135 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
136 MINUS_ONE, FALSE),
137 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
138 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
139 MINUS_ONE, FALSE),
1788fc08
L
140 HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
141 bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
142 FALSE),
143 HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
144 bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
145 FALSE),
67a4f2b7
AO
146 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
147 complain_overflow_bitfield, bfd_elf_generic_reloc,
148 "R_X86_64_GOTPC32_TLSDESC",
149 FALSE, 0xffffffff, 0xffffffff, TRUE),
150 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
151 complain_overflow_dont, bfd_elf_generic_reloc,
152 "R_X86_64_TLSDESC_CALL",
153 FALSE, 0, 0, FALSE),
154 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
155 complain_overflow_bitfield, bfd_elf_generic_reloc,
156 "R_X86_64_TLSDESC",
157 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
cbe950e9
L
158 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
159 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
160 MINUS_ONE, FALSE),
64d25c44
L
161 HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
162 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
163 MINUS_ONE, FALSE),
c3320543
L
164 HOWTO(R_X86_64_PC32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
165 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", FALSE, 0xffffffff, 0xffffffff,
166 TRUE),
167 HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
168 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", FALSE, 0xffffffff, 0xffffffff,
169 TRUE),
56ceb5b5
L
170 HOWTO(R_X86_64_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
171 bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", FALSE, 0xffffffff,
172 0xffffffff, TRUE),
173 HOWTO(R_X86_64_REX_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
174 bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", FALSE, 0xffffffff,
175 0xffffffff, TRUE),
fe4770f4 176
a33d77bc
JB
177 /* We have a gap in the reloc numbers here.
178 R_X86_64_standard counts the number up to this point, and
179 R_X86_64_vt_offset is the value to subtract from a reloc type of
180 R_X86_64_GNU_VT* to form an index into this table. */
56ceb5b5 181#define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
a33d77bc
JB
182#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
183
fe4770f4 184/* GNU extension to record C++ vtable hierarchy. */
b34976b6
AM
185 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
186 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
fe4770f4
AJ
187
188/* GNU extension to record C++ vtable member usage. */
b34976b6
AM
189 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
190 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
d7921315
L
191 FALSE),
192
193/* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
194 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
195 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
196 FALSE)
8d88c4ca
NC
197};
198
daf1c414 199#define X86_PCREL_TYPE_P(TYPE) \
d8045f23
NC
200 ( ((TYPE) == R_X86_64_PC8) \
201 || ((TYPE) == R_X86_64_PC16) \
202 || ((TYPE) == R_X86_64_PC32) \
c3320543 203 || ((TYPE) == R_X86_64_PC32_BND) \
d8045f23
NC
204 || ((TYPE) == R_X86_64_PC64))
205
aebcc8ff
L
206#define X86_SIZE_TYPE_P(TYPE) \
207 ((TYPE) == R_X86_64_SIZE32 || (TYPE) == R_X86_64_SIZE64)
208
8d88c4ca 209/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
210struct elf_reloc_map
211{
8d88c4ca
NC
212 bfd_reloc_code_real_type bfd_reloc_val;
213 unsigned char elf_reloc_val;
214};
215
dc810e39 216static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 217{
70256ad8
AJ
218 { BFD_RELOC_NONE, R_X86_64_NONE, },
219 { BFD_RELOC_64, R_X86_64_64, },
220 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
221 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
222 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
223 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
224 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
225 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
226 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
227 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
228 { BFD_RELOC_32, R_X86_64_32, },
229 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
230 { BFD_RELOC_16, R_X86_64_16, },
231 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
232 { BFD_RELOC_8, R_X86_64_8, },
233 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
234 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
235 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
236 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
237 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
238 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
239 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
240 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
241 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
d6ab8113
JB
242 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
243 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
244 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
7b81dfbb
AJ
245 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
246 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
247 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
248 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
249 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
1788fc08
L
250 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
251 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
67a4f2b7
AO
252 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
253 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
254 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
cbe950e9 255 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
56ceb5b5
L
256 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
257 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
258 { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
259 { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
fe4770f4
AJ
260 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
261 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
262};
263
67a4f2b7 264static reloc_howto_type *
351f65ca 265elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
67a4f2b7
AO
266{
267 unsigned i;
268
d7921315
L
269 if (r_type == (unsigned int) R_X86_64_32)
270 {
271 if (ABI_64_P (abfd))
272 i = r_type;
273 else
274 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
275 }
276 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
277 || r_type >= (unsigned int) R_X86_64_max)
67a4f2b7
AO
278 {
279 if (r_type >= (unsigned int) R_X86_64_standard)
280 {
695344c0 281 /* xgettext:c-format */
0aa13fee
AM
282 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
283 abfd, r_type);
f3185997
NC
284 bfd_set_error (bfd_error_bad_value);
285 return NULL;
67a4f2b7
AO
286 }
287 i = r_type;
288 }
289 else
290 i = r_type - (unsigned int) R_X86_64_vt_offset;
291 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
292 return &x86_64_elf_howto_table[i];
293}
8d88c4ca
NC
294
295/* Given a BFD reloc type, return a HOWTO structure. */
296static reloc_howto_type *
351f65ca
L
297elf_x86_64_reloc_type_lookup (bfd *abfd,
298 bfd_reloc_code_real_type code)
8d88c4ca
NC
299{
300 unsigned int i;
27482721 301
8d88c4ca
NC
302 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
303 i++)
304 {
305 if (x86_64_reloc_map[i].bfd_reloc_val == code)
351f65ca
L
306 return elf_x86_64_rtype_to_howto (abfd,
307 x86_64_reloc_map[i].elf_reloc_val);
8d88c4ca 308 }
5860e3f8 309 return NULL;
8d88c4ca
NC
310}
311
157090f7 312static reloc_howto_type *
d7921315 313elf_x86_64_reloc_name_lookup (bfd *abfd,
351f65ca 314 const char *r_name)
157090f7
AM
315{
316 unsigned int i;
317
d7921315
L
318 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
319 {
320 /* Get x32 R_X86_64_32. */
321 reloc_howto_type *reloc
322 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
323 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
324 return reloc;
325 }
326
327 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
157090f7
AM
328 if (x86_64_elf_howto_table[i].name != NULL
329 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
330 return &x86_64_elf_howto_table[i];
331
332 return NULL;
333}
334
8d88c4ca 335/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 336
f3185997
NC
337static bfd_boolean
338elf_x86_64_info_to_howto (bfd *abfd, arelent *cache_ptr,
351f65ca 339 Elf_Internal_Rela *dst)
8d88c4ca 340{
67a4f2b7 341 unsigned r_type;
8d88c4ca 342
351f65ca
L
343 r_type = ELF32_R_TYPE (dst->r_info);
344 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
f3185997
NC
345 if (cache_ptr->howto == NULL)
346 return FALSE;
262c0a87 347 BFD_ASSERT (r_type == cache_ptr->howto->type || cache_ptr->howto->type == R_X86_64_NONE);
f3185997 348 return TRUE;
8d88c4ca 349}
70256ad8 350\f
3bab7989 351/* Support for core dump NOTE sections. */
b34976b6 352static bfd_boolean
351f65ca 353elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
354{
355 int offset;
eea6121a 356 size_t size;
3bab7989
ML
357
358 switch (note->descsz)
359 {
360 default:
b34976b6 361 return FALSE;
3bab7989 362
bcd823f1
L
363 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
364 /* pr_cursig */
228e534f 365 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
bcd823f1
L
366
367 /* pr_pid */
228e534f 368 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
bcd823f1
L
369
370 /* pr_reg */
371 offset = 72;
372 size = 216;
373
374 break;
375
3bab7989
ML
376 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
377 /* pr_cursig */
228e534f 378 elf_tdata (abfd)->core->signal
3bab7989
ML
379 = bfd_get_16 (abfd, note->descdata + 12);
380
381 /* pr_pid */
228e534f 382 elf_tdata (abfd)->core->lwpid
3bab7989
ML
383 = bfd_get_32 (abfd, note->descdata + 32);
384
385 /* pr_reg */
386 offset = 112;
eea6121a 387 size = 216;
3bab7989
ML
388
389 break;
390 }
391
392 /* Make a ".reg/999" section. */
393 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 394 size, note->descpos + offset);
3bab7989
ML
395}
396
b34976b6 397static bfd_boolean
351f65ca 398elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
399{
400 switch (note->descsz)
401 {
402 default:
b34976b6 403 return FALSE;
3bab7989 404
bcd823f1 405 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
228e534f 406 elf_tdata (abfd)->core->pid
bcd823f1 407 = bfd_get_32 (abfd, note->descdata + 12);
228e534f 408 elf_tdata (abfd)->core->program
bcd823f1 409 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 410 elf_tdata (abfd)->core->command
bcd823f1
L
411 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
412 break;
413
3bab7989 414 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
228e534f 415 elf_tdata (abfd)->core->pid
261b8d08 416 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 417 elf_tdata (abfd)->core->program
3bab7989 418 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 419 elf_tdata (abfd)->core->command
3bab7989
ML
420 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
421 }
422
423 /* Note that for some reason, a spurious space is tacked
424 onto the end of the args in some (at least one anyway)
425 implementations, so strip it off if it exists. */
426
427 {
228e534f 428 char *command = elf_tdata (abfd)->core->command;
3bab7989
ML
429 int n = strlen (command);
430
431 if (0 < n && command[n - 1] == ' ')
432 command[n - 1] = '\0';
433 }
434
b34976b6 435 return TRUE;
3bab7989 436}
8fd79e71
L
437
438#ifdef CORE_HEADER
602f1657
AM
439# if GCC_VERSION >= 8000
440# pragma GCC diagnostic push
441# pragma GCC diagnostic ignored "-Wstringop-truncation"
442# endif
8fd79e71
L
443static char *
444elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
445 int note_type, ...)
446{
447 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8fd79e71
L
448 va_list ap;
449 const char *fname, *psargs;
450 long pid;
451 int cursig;
452 const void *gregs;
453
454 switch (note_type)
455 {
456 default:
457 return NULL;
458
459 case NT_PRPSINFO:
460 va_start (ap, note_type);
461 fname = va_arg (ap, const char *);
462 psargs = va_arg (ap, const char *);
463 va_end (ap);
464
465 if (bed->s->elfclass == ELFCLASS32)
466 {
467 prpsinfo32_t data;
468 memset (&data, 0, sizeof (data));
469 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
470 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
471 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
472 &data, sizeof (data));
8fd79e71
L
473 }
474 else
475 {
b1bd052d 476 prpsinfo64_t data;
8fd79e71
L
477 memset (&data, 0, sizeof (data));
478 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
479 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
480 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
481 &data, sizeof (data));
8fd79e71 482 }
e85c6a70 483 /* NOTREACHED */
8fd79e71
L
484
485 case NT_PRSTATUS:
486 va_start (ap, note_type);
487 pid = va_arg (ap, long);
488 cursig = va_arg (ap, int);
489 gregs = va_arg (ap, const void *);
490 va_end (ap);
491
492 if (bed->s->elfclass == ELFCLASS32)
493 {
494 if (bed->elf_machine_code == EM_X86_64)
495 {
496 prstatusx32_t prstat;
497 memset (&prstat, 0, sizeof (prstat));
498 prstat.pr_pid = pid;
499 prstat.pr_cursig = cursig;
500 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
501 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
502 &prstat, sizeof (prstat));
8fd79e71
L
503 }
504 else
505 {
506 prstatus32_t prstat;
507 memset (&prstat, 0, sizeof (prstat));
508 prstat.pr_pid = pid;
509 prstat.pr_cursig = cursig;
510 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
511 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
512 &prstat, sizeof (prstat));
8fd79e71
L
513 }
514 }
515 else
516 {
b1bd052d 517 prstatus64_t prstat;
8fd79e71
L
518 memset (&prstat, 0, sizeof (prstat));
519 prstat.pr_pid = pid;
520 prstat.pr_cursig = cursig;
521 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
522 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
523 &prstat, sizeof (prstat));
8fd79e71 524 }
8fd79e71 525 }
e85c6a70 526 /* NOTREACHED */
8fd79e71 527}
602f1657
AM
528# if GCC_VERSION >= 8000
529# pragma GCC diagnostic pop
530# endif
8fd79e71 531#endif
3bab7989 532\f
407443a3 533/* Functions for the x86-64 ELF linker. */
70256ad8 534
70256ad8
AJ
535/* The size in bytes of an entry in the global offset table. */
536
537#define GOT_ENTRY_SIZE 8
8d88c4ca 538
38b12349 539/* The size in bytes of an entry in the lazy procedure linkage table. */
8d88c4ca 540
38b12349 541#define LAZY_PLT_ENTRY_SIZE 16
70256ad8 542
38b12349
L
543/* The size in bytes of an entry in the non-lazy procedure linkage
544 table. */
70256ad8 545
38b12349
L
546#define NON_LAZY_PLT_ENTRY_SIZE 8
547
548/* The first entry in a lazy procedure linkage table looks like this.
549 See the SVR4 ABI i386 supplement and the x86-64 ABI to see how this
550 works. */
551
552static const bfd_byte elf_x86_64_lazy_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
70256ad8 553{
653165cc
AJ
554 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
555 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
10efb593 556 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
70256ad8
AJ
557};
558
38b12349 559/* Subsequent entries in a lazy procedure linkage table look like this. */
70256ad8 560
38b12349 561static const bfd_byte elf_x86_64_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
70256ad8 562{
653165cc 563 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 564 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 565 0x68, /* pushq immediate */
70256ad8
AJ
566 0, 0, 0, 0, /* replaced with index into relocation table. */
567 0xe9, /* jmp relative */
568 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
569};
570
38b12349 571/* The first entry in a lazy procedure linkage table with BND prefix
0ff2b86e
L
572 like this. */
573
38b12349 574static const bfd_byte elf_x86_64_lazy_bnd_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
0ff2b86e 575{
07d6d2b8 576 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
0ff2b86e 577 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
07d6d2b8 578 0x0f, 0x1f, 0 /* nopl (%rax) */
0ff2b86e
L
579};
580
38b12349
L
581/* Subsequent entries for branches with BND prefx in a lazy procedure
582 linkage table look like this. */
0ff2b86e 583
38b12349 584static const bfd_byte elf_x86_64_lazy_bnd_plt_entry[LAZY_PLT_ENTRY_SIZE] =
0ff2b86e 585{
07d6d2b8
AM
586 0x68, 0, 0, 0, 0, /* pushq immediate */
587 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
588 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
0ff2b86e
L
589};
590
ee2fdd6f
L
591/* The first entry in the IBT-enabled lazy procedure linkage table is the
592 the same as the lazy PLT with BND prefix so that bound registers are
593 preserved when control is passed to dynamic linker. Subsequent
594 entries for a IBT-enabled lazy procedure linkage table look like
595 this. */
596
597static const bfd_byte elf_x86_64_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
598{
07d6d2b8
AM
599 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
600 0x68, 0, 0, 0, 0, /* pushq immediate */
601 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
602 0x90 /* nop */
ee2fdd6f
L
603};
604
605/* The first entry in the x32 IBT-enabled lazy procedure linkage table
de194d85 606 is the same as the normal lazy PLT. Subsequent entries for an
ee2fdd6f
L
607 x32 IBT-enabled lazy procedure linkage table look like this. */
608
609static const bfd_byte elf_x32_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
610{
07d6d2b8
AM
611 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
612 0x68, 0, 0, 0, 0, /* pushq immediate */
613 0xe9, 0, 0, 0, 0, /* jmpq relative */
614 0x66, 0x90 /* xchg %ax,%ax */
ee2fdd6f
L
615};
616
38b12349 617/* Entries in the non-lazey procedure linkage table look like this. */
0ff2b86e 618
38b12349 619static const bfd_byte elf_x86_64_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
0ff2b86e 620{
07d6d2b8
AM
621 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
622 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
623 0x66, 0x90 /* xchg %ax,%ax */
0ff2b86e
L
624};
625
38b12349
L
626/* Entries for branches with BND prefix in the non-lazey procedure
627 linkage table look like this. */
0ff2b86e 628
38b12349 629static const bfd_byte elf_x86_64_non_lazy_bnd_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
0ff2b86e 630{
07d6d2b8
AM
631 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
632 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
633 0x90 /* nop */
0ff2b86e
L
634};
635
ee2fdd6f
L
636/* Entries for branches with IBT-enabled in the non-lazey procedure
637 linkage table look like this. They have the same size as the lazy
638 PLT entry. */
639
640static const bfd_byte elf_x86_64_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
641{
07d6d2b8
AM
642 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
643 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
ee2fdd6f 644 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
07d6d2b8 645 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopl 0x0(%rax,%rax,1) */
ee2fdd6f
L
646};
647
648/* Entries for branches with IBT-enabled in the x32 non-lazey procedure
649 linkage table look like this. They have the same size as the lazy
650 PLT entry. */
651
652static const bfd_byte elf_x32_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
653{
07d6d2b8
AM
654 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
655 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
ee2fdd6f
L
656 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
657 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
658};
659
92e68c1d
L
660/* The TLSDESC entry in a lazy procedure linkage table. */
661static const bfd_byte elf_x86_64_tlsdesc_plt_entry[LAZY_PLT_ENTRY_SIZE] =
662{
663 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
664 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
665 0xff, 0x25, 16, 0, 0, 0 /* jmpq *GOT+TDG(%rip) */
666};
667
38b12349 668/* .eh_frame covering the lazy .plt section. */
e41b3a13 669
38b12349 670static const bfd_byte elf_x86_64_eh_frame_lazy_plt[] =
e41b3a13 671{
e41b3a13
JJ
672 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
673 0, 0, 0, 0, /* CIE ID */
674 1, /* CIE version */
675 'z', 'R', 0, /* Augmentation string */
676 1, /* Code alignment factor */
677 0x78, /* Data alignment factor */
678 16, /* Return address column */
679 1, /* Augmentation size */
680 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
681 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
682 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
683 DW_CFA_nop, DW_CFA_nop,
684
685 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
686 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
687 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
688 0, 0, 0, 0, /* .plt size goes here */
689 0, /* Augmentation size */
690 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
691 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
692 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
693 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
694 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
695 11, /* Block length */
696 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
697 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
698 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
699 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
700 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
701};
702
38b12349 703/* .eh_frame covering the lazy BND .plt section. */
9e659176 704
38b12349 705static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt[] =
9e659176
L
706{
707 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
708 0, 0, 0, 0, /* CIE ID */
709 1, /* CIE version */
710 'z', 'R', 0, /* Augmentation string */
711 1, /* Code alignment factor */
712 0x78, /* Data alignment factor */
713 16, /* Return address column */
714 1, /* Augmentation size */
715 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
716 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
717 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
718 DW_CFA_nop, DW_CFA_nop,
719
720 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
721 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
722 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
723 0, 0, 0, 0, /* .plt size goes here */
724 0, /* Augmentation size */
725 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
726 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
727 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
728 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
729 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
730 11, /* Block length */
731 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
732 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
733 DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
734 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
735 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
736};
737
ee2fdd6f
L
738/* .eh_frame covering the lazy .plt section with IBT-enabled. */
739
740static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt[] =
741{
742 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
743 0, 0, 0, 0, /* CIE ID */
744 1, /* CIE version */
745 'z', 'R', 0, /* Augmentation string */
746 1, /* Code alignment factor */
747 0x78, /* Data alignment factor */
748 16, /* Return address column */
749 1, /* Augmentation size */
750 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
751 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
752 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
753 DW_CFA_nop, DW_CFA_nop,
754
755 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
756 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
757 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
758 0, 0, 0, 0, /* .plt size goes here */
759 0, /* Augmentation size */
760 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
761 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
762 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
763 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
764 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
765 11, /* Block length */
766 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
767 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
768 DW_OP_lit15, DW_OP_and, DW_OP_lit10, DW_OP_ge,
769 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
770 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
771};
772
773/* .eh_frame covering the x32 lazy .plt section with IBT-enabled. */
774
775static const bfd_byte elf_x32_eh_frame_lazy_ibt_plt[] =
776{
777 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
778 0, 0, 0, 0, /* CIE ID */
779 1, /* CIE version */
780 'z', 'R', 0, /* Augmentation string */
781 1, /* Code alignment factor */
782 0x78, /* Data alignment factor */
783 16, /* Return address column */
784 1, /* Augmentation size */
785 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
786 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
787 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
788 DW_CFA_nop, DW_CFA_nop,
789
790 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
791 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
792 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
793 0, 0, 0, 0, /* .plt size goes here */
794 0, /* Augmentation size */
795 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
796 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
797 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
798 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
799 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
800 11, /* Block length */
801 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
802 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
803 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
804 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
805 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
806};
807
38b12349 808/* .eh_frame covering the non-lazy .plt section. */
fff53dae 809
38b12349 810static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt[] =
fff53dae
L
811{
812#define PLT_GOT_FDE_LENGTH 20
813 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
814 0, 0, 0, 0, /* CIE ID */
815 1, /* CIE version */
816 'z', 'R', 0, /* Augmentation string */
817 1, /* Code alignment factor */
818 0x78, /* Data alignment factor */
819 16, /* Return address column */
820 1, /* Augmentation size */
821 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
822 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
823 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
824 DW_CFA_nop, DW_CFA_nop,
825
826 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
827 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
38b12349
L
828 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
829 0, 0, 0, 0, /* non-lazy .plt size goes here */
fff53dae
L
830 0, /* Augmentation size */
831 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
832 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
833};
834
eed180f8 835/* These are the standard parameters. */
765e526c 836static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_plt =
eed180f8 837 {
07d6d2b8
AM
838 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
839 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
840 elf_x86_64_lazy_plt_entry, /* plt_entry */
841 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
842 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
843 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
844 6, /* plt_tlsdesc_got1_offset */
845 12, /* plt_tlsdesc_got2_offset */
846 10, /* plt_tlsdesc_got1_insn_end */
847 16, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
848 2, /* plt0_got1_offset */
849 8, /* plt0_got2_offset */
850 12, /* plt0_got2_insn_end */
851 2, /* plt_got_offset */
852 7, /* plt_reloc_offset */
853 12, /* plt_plt_offset */
854 6, /* plt_got_insn_size */
855 LAZY_PLT_ENTRY_SIZE, /* plt_plt_insn_end */
856 6, /* plt_lazy_offset */
857 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
858 elf_x86_64_lazy_plt_entry, /* pic_plt_entry */
859 elf_x86_64_eh_frame_lazy_plt, /* eh_frame_plt */
38b12349
L
860 sizeof (elf_x86_64_eh_frame_lazy_plt) /* eh_frame_plt_size */
861 };
862
765e526c 863static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_plt =
38b12349 864 {
07d6d2b8
AM
865 elf_x86_64_non_lazy_plt_entry, /* plt_entry */
866 elf_x86_64_non_lazy_plt_entry, /* pic_plt_entry */
867 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
868 2, /* plt_got_offset */
869 6, /* plt_got_insn_size */
870 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
38b12349 871 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
eed180f8
RM
872 };
873
765e526c 874static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_plt =
0ff2b86e 875 {
07d6d2b8
AM
876 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
877 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
878 elf_x86_64_lazy_bnd_plt_entry, /* plt_entry */
879 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
880 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
881 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
882 6, /* plt_tlsdesc_got1_offset */
883 12, /* plt_tlsdesc_got2_offset */
884 10, /* plt_tlsdesc_got1_insn_end */
885 16, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
886 2, /* plt0_got1_offset */
887 1+8, /* plt0_got2_offset */
888 1+12, /* plt0_got2_insn_end */
889 1+2, /* plt_got_offset */
890 1, /* plt_reloc_offset */
891 7, /* plt_plt_offset */
892 1+6, /* plt_got_insn_size */
893 11, /* plt_plt_insn_end */
894 0, /* plt_lazy_offset */
895 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
896 elf_x86_64_lazy_bnd_plt_entry, /* pic_plt_entry */
897 elf_x86_64_eh_frame_lazy_bnd_plt, /* eh_frame_plt */
38b12349
L
898 sizeof (elf_x86_64_eh_frame_lazy_bnd_plt) /* eh_frame_plt_size */
899 };
900
765e526c 901static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt =
38b12349 902 {
07d6d2b8
AM
903 elf_x86_64_non_lazy_bnd_plt_entry, /* plt_entry */
904 elf_x86_64_non_lazy_bnd_plt_entry, /* pic_plt_entry */
905 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
906 1+2, /* plt_got_offset */
907 1+6, /* plt_got_insn_size */
908 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
38b12349
L
909 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
910 };
911
765e526c 912static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_ibt_plt =
ee2fdd6f 913 {
07d6d2b8
AM
914 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
915 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
916 elf_x86_64_lazy_ibt_plt_entry, /* plt_entry */
917 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
918 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
919 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
920 6, /* plt_tlsdesc_got1_offset */
921 12, /* plt_tlsdesc_got2_offset */
922 10, /* plt_tlsdesc_got1_insn_end */
923 16, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
924 2, /* plt0_got1_offset */
925 1+8, /* plt0_got2_offset */
926 1+12, /* plt0_got2_insn_end */
927 4+1+2, /* plt_got_offset */
928 4+1, /* plt_reloc_offset */
929 4+1+6, /* plt_plt_offset */
930 4+1+6, /* plt_got_insn_size */
931 4+1+5+5, /* plt_plt_insn_end */
932 0, /* plt_lazy_offset */
933 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
934 elf_x86_64_lazy_ibt_plt_entry, /* pic_plt_entry */
935 elf_x86_64_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
ee2fdd6f
L
936 sizeof (elf_x86_64_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
937 };
938
765e526c 939static const struct elf_x86_lazy_plt_layout elf_x32_lazy_ibt_plt =
ee2fdd6f 940 {
07d6d2b8
AM
941 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
942 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
943 elf_x32_lazy_ibt_plt_entry, /* plt_entry */
944 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
945 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
946 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
947 6, /* plt_tlsdesc_got1_offset */
948 12, /* plt_tlsdesc_got2_offset */
949 10, /* plt_tlsdesc_got1_insn_end */
950 16, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
951 2, /* plt0_got1_offset */
952 8, /* plt0_got2_offset */
953 12, /* plt0_got2_insn_end */
954 4+2, /* plt_got_offset */
955 4+1, /* plt_reloc_offset */
956 4+6, /* plt_plt_offset */
957 4+6, /* plt_got_insn_size */
958 4+5+5, /* plt_plt_insn_end */
959 0, /* plt_lazy_offset */
960 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
961 elf_x32_lazy_ibt_plt_entry, /* pic_plt_entry */
962 elf_x32_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
ee2fdd6f
L
963 sizeof (elf_x32_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
964 };
965
765e526c 966static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt =
ee2fdd6f 967 {
07d6d2b8
AM
968 elf_x86_64_non_lazy_ibt_plt_entry, /* plt_entry */
969 elf_x86_64_non_lazy_ibt_plt_entry, /* pic_plt_entry */
970 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
971 4+1+2, /* plt_got_offset */
972 4+1+6, /* plt_got_insn_size */
973 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
ee2fdd6f
L
974 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
975 };
976
765e526c 977static const struct elf_x86_non_lazy_plt_layout elf_x32_non_lazy_ibt_plt =
ee2fdd6f 978 {
07d6d2b8
AM
979 elf_x32_non_lazy_ibt_plt_entry, /* plt_entry */
980 elf_x32_non_lazy_ibt_plt_entry, /* pic_plt_entry */
981 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
982 4+2, /* plt_got_offset */
983 4+6, /* plt_got_insn_size */
984 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
ee2fdd6f
L
985 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
986 };
987
eed180f8 988
b34976b6 989static bfd_boolean
27482721 990elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 991{
8d88c4ca
NC
992 /* Set the right machine number for an x86-64 elf64 file. */
993 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
b34976b6 994 return TRUE;
8d88c4ca
NC
995}
996
8059fb19
RM
997static bfd_boolean
998elf32_x86_64_elf_object_p (bfd *abfd)
999{
1000 /* Set the right machine number for an x86-64 elf32 file. */
1001 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1002 return TRUE;
1003}
1004
142411ca
L
1005/* Return TRUE if the TLS access code sequence support transition
1006 from R_TYPE. */
1007
1008static bfd_boolean
351f65ca
L
1009elf_x86_64_check_tls_transition (bfd *abfd,
1010 struct bfd_link_info *info,
1011 asection *sec,
1012 bfd_byte *contents,
1013 Elf_Internal_Shdr *symtab_hdr,
1014 struct elf_link_hash_entry **sym_hashes,
1015 unsigned int r_type,
1016 const Elf_Internal_Rela *rel,
1017 const Elf_Internal_Rela *relend)
bffbf940 1018{
142411ca
L
1019 unsigned int val;
1020 unsigned long r_symndx;
5c98a14e 1021 bfd_boolean largepic = FALSE;
142411ca
L
1022 struct elf_link_hash_entry *h;
1023 bfd_vma offset;
0afcef53 1024 struct elf_x86_link_hash_table *htab;
e2cbcd91 1025 bfd_byte *call;
1d4af308 1026 bfd_boolean indirect_call;
142411ca 1027
0afcef53 1028 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
142411ca 1029 offset = rel->r_offset;
bffbf940 1030 switch (r_type)
142411ca
L
1031 {
1032 case R_X86_64_TLSGD:
1033 case R_X86_64_TLSLD:
1034 if ((rel + 1) >= relend)
1035 return FALSE;
1036
1037 if (r_type == R_X86_64_TLSGD)
1038 {
52bc799a 1039 /* Check transition from GD access model. For 64bit, only
142411ca 1040 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
e2cbcd91
L
1041 .word 0x6666; rex64; call __tls_get_addr@PLT
1042 or
1043 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1044 .byte 0x66; rex64
1045 call *__tls_get_addr@GOTPCREL(%rip)
1046 which may be converted to
1047 addr32 call __tls_get_addr
52bc799a
L
1048 can transit to different access model. For 32bit, only
1049 leaq foo@tlsgd(%rip), %rdi
e2cbcd91
L
1050 .word 0x6666; rex64; call __tls_get_addr@PLT
1051 or
1052 leaq foo@tlsgd(%rip), %rdi
1053 .byte 0x66; rex64
1054 call *__tls_get_addr@GOTPCREL(%rip)
1055 which may be converted to
1056 addr32 call __tls_get_addr
1057 can transit to different access model. For largepic,
5c98a14e 1058 we also support:
07d6d2b8
AM
1059 leaq foo@tlsgd(%rip), %rdi
1060 movabsq $__tls_get_addr@pltoff, %rax
1061 addq $r15, %rax
1062 call *%rax
e2cbcd91 1063 or
07d6d2b8
AM
1064 leaq foo@tlsgd(%rip), %rdi
1065 movabsq $__tls_get_addr@pltoff, %rax
1066 addq $rbx, %rax
1067 call *%rax */
142411ca 1068
fa289a5f
AM
1069 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1070
5c98a14e 1071 if ((offset + 12) > sec->size)
142411ca 1072 return FALSE;
52bc799a 1073
e2cbcd91
L
1074 call = contents + offset + 4;
1075 if (call[0] != 0x66
1076 || !((call[1] == 0x48
1077 && call[2] == 0xff
1078 && call[3] == 0x15)
1079 || (call[1] == 0x48
1080 && call[2] == 0x67
1081 && call[3] == 0xe8)
1082 || (call[1] == 0x66
1083 && call[2] == 0x48
1084 && call[3] == 0xe8)))
5c98a14e
JJ
1085 {
1086 if (!ABI_64_P (abfd)
1087 || (offset + 19) > sec->size
1088 || offset < 3
e2cbcd91
L
1089 || memcmp (call - 7, leaq + 1, 3) != 0
1090 || memcmp (call, "\x48\xb8", 2) != 0
1091 || call[11] != 0x01
1092 || call[13] != 0xff
1093 || call[14] != 0xd0
1094 || !((call[10] == 0x48 && call[12] == 0xd8)
1095 || (call[10] == 0x4c && call[12] == 0xf8)))
5c98a14e
JJ
1096 return FALSE;
1097 largepic = TRUE;
1098 }
1099 else if (ABI_64_P (abfd))
52bc799a 1100 {
52bc799a 1101 if (offset < 4
fa289a5f 1102 || memcmp (contents + offset - 4, leaq, 4) != 0)
52bc799a
L
1103 return FALSE;
1104 }
1105 else
1106 {
52bc799a 1107 if (offset < 3
fa289a5f 1108 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
52bc799a
L
1109 return FALSE;
1110 }
e2cbcd91 1111 indirect_call = call[2] == 0xff;
142411ca
L
1112 }
1113 else
1114 {
1115 /* Check transition from LD access model. Only
1116 leaq foo@tlsld(%rip), %rdi;
e2cbcd91 1117 call __tls_get_addr@PLT
07d6d2b8 1118 or
e2cbcd91
L
1119 leaq foo@tlsld(%rip), %rdi;
1120 call *__tls_get_addr@GOTPCREL(%rip)
1121 which may be converted to
1122 addr32 call __tls_get_addr
5c98a14e
JJ
1123 can transit to different access model. For largepic
1124 we also support:
07d6d2b8
AM
1125 leaq foo@tlsld(%rip), %rdi
1126 movabsq $__tls_get_addr@pltoff, %rax
1127 addq $r15, %rax
1128 call *%rax
e2cbcd91 1129 or
07d6d2b8
AM
1130 leaq foo@tlsld(%rip), %rdi
1131 movabsq $__tls_get_addr@pltoff, %rax
1132 addq $rbx, %rax
1133 call *%rax */
142411ca 1134
fa289a5f 1135 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
142411ca
L
1136
1137 if (offset < 3 || (offset + 9) > sec->size)
1138 return FALSE;
1139
5c98a14e 1140 if (memcmp (contents + offset - 3, lea, 3) != 0)
142411ca 1141 return FALSE;
5c98a14e 1142
e2cbcd91
L
1143 call = contents + offset + 4;
1144 if (!(call[0] == 0xe8
1145 || (call[0] == 0xff && call[1] == 0x15)
1146 || (call[0] == 0x67 && call[1] == 0xe8)))
5c98a14e
JJ
1147 {
1148 if (!ABI_64_P (abfd)
1149 || (offset + 19) > sec->size
e2cbcd91
L
1150 || memcmp (call, "\x48\xb8", 2) != 0
1151 || call[11] != 0x01
1152 || call[13] != 0xff
1153 || call[14] != 0xd0
1154 || !((call[10] == 0x48 && call[12] == 0xd8)
1155 || (call[10] == 0x4c && call[12] == 0xf8)))
5c98a14e
JJ
1156 return FALSE;
1157 largepic = TRUE;
1158 }
e2cbcd91 1159 indirect_call = call[0] == 0xff;
142411ca
L
1160 }
1161
351f65ca 1162 r_symndx = htab->r_sym (rel[1].r_info);
142411ca
L
1163 if (r_symndx < symtab_hdr->sh_info)
1164 return FALSE;
1165
1166 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1d4af308 1167 if (h == NULL
0afcef53 1168 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
e2cbcd91 1169 return FALSE;
e2cbcd91 1170 else
78984959
L
1171 {
1172 r_type = (ELF32_R_TYPE (rel[1].r_info)
1173 & ~R_X86_64_converted_reloc_bit);
1174 if (largepic)
1175 return r_type == R_X86_64_PLTOFF64;
1176 else if (indirect_call)
1177 return r_type == R_X86_64_GOTPCRELX;
1178 else
1179 return (r_type == R_X86_64_PC32 || r_type == R_X86_64_PLT32);
1180 }
142411ca
L
1181
1182 case R_X86_64_GOTTPOFF:
1183 /* Check transition from IE access model:
4a4c5f25
L
1184 mov foo@gottpoff(%rip), %reg
1185 add foo@gottpoff(%rip), %reg
142411ca
L
1186 */
1187
4a4c5f25
L
1188 /* Check REX prefix first. */
1189 if (offset >= 3 && (offset + 4) <= sec->size)
1190 {
1191 val = bfd_get_8 (abfd, contents + offset - 3);
1192 if (val != 0x48 && val != 0x4c)
1193 {
1194 /* X32 may have 0x44 REX prefix or no REX prefix. */
1195 if (ABI_64_P (abfd))
1196 return FALSE;
1197 }
1198 }
1199 else
1200 {
1201 /* X32 may not have any REX prefix. */
1202 if (ABI_64_P (abfd))
1203 return FALSE;
1204 if (offset < 2 || (offset + 3) > sec->size)
1205 return FALSE;
1206 }
142411ca
L
1207
1208 val = bfd_get_8 (abfd, contents + offset - 2);
1209 if (val != 0x8b && val != 0x03)
1210 return FALSE;
1211
1212 val = bfd_get_8 (abfd, contents + offset - 1);
1213 return (val & 0xc7) == 5;
1214
1215 case R_X86_64_GOTPC32_TLSDESC:
1216 /* Check transition from GDesc access model:
14470f07
L
1217 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
1218 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
142411ca
L
1219
1220 Make sure it's a leaq adding rip to a 32-bit offset
1221 into any register, although it's probably almost always
1222 going to be rax. */
1223
1224 if (offset < 3 || (offset + 4) > sec->size)
1225 return FALSE;
1226
1227 val = bfd_get_8 (abfd, contents + offset - 3);
14470f07
L
1228 val &= 0xfb;
1229 if (val != 0x48 && (ABI_64_P (abfd) || val != 0x40))
142411ca
L
1230 return FALSE;
1231
1232 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1233 return FALSE;
1234
1235 val = bfd_get_8 (abfd, contents + offset - 1);
1236 return (val & 0xc7) == 0x05;
1237
1238 case R_X86_64_TLSDESC_CALL:
1239 /* Check transition from GDesc access model:
14470f07
L
1240 call *x@tlsdesc(%rax) <--- LP64 mode.
1241 call *x@tlsdesc(%eax) <--- X32 mode.
142411ca
L
1242 */
1243 if (offset + 2 <= sec->size)
1244 {
14470f07 1245 unsigned int prefix;
e2cbcd91 1246 call = contents + offset;
14470f07
L
1247 prefix = 0;
1248 if (!ABI_64_P (abfd))
1249 {
1250 /* Check for call *x@tlsdesc(%eax). */
1251 if (call[0] == 0x67)
1252 {
1253 prefix = 1;
1254 if (offset + 3 > sec->size)
1255 return FALSE;
1256 }
1257 }
1258 /* Make sure that it's a call *x@tlsdesc(%rax). */
1259 return call[prefix] == 0xff && call[1 + prefix] == 0x10;
142411ca
L
1260 }
1261
1262 return FALSE;
1263
1264 default:
1265 abort ();
1266 }
1267}
1268
1269/* Return TRUE if the TLS access transition is OK or no transition
1270 will be performed. Update R_TYPE if there is a transition. */
1271
1272static bfd_boolean
351f65ca
L
1273elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1274 asection *sec, bfd_byte *contents,
1275 Elf_Internal_Shdr *symtab_hdr,
1276 struct elf_link_hash_entry **sym_hashes,
1277 unsigned int *r_type, int tls_type,
1278 const Elf_Internal_Rela *rel,
1279 const Elf_Internal_Rela *relend,
1280 struct elf_link_hash_entry *h,
bedfd056
L
1281 unsigned long r_symndx,
1282 bfd_boolean from_relocate_section)
142411ca
L
1283{
1284 unsigned int from_type = *r_type;
1285 unsigned int to_type = from_type;
1286 bfd_boolean check = TRUE;
1287
bb1cb422
L
1288 /* Skip TLS transition for functions. */
1289 if (h != NULL
1290 && (h->type == STT_FUNC
1291 || h->type == STT_GNU_IFUNC))
1292 return TRUE;
1293
142411ca 1294 switch (from_type)
bffbf940
JJ
1295 {
1296 case R_X86_64_TLSGD:
67a4f2b7
AO
1297 case R_X86_64_GOTPC32_TLSDESC:
1298 case R_X86_64_TLSDESC_CALL:
bffbf940 1299 case R_X86_64_GOTTPOFF:
0e1862bb 1300 if (bfd_link_executable (info))
142411ca
L
1301 {
1302 if (h == NULL)
1303 to_type = R_X86_64_TPOFF32;
1304 else
1305 to_type = R_X86_64_GOTTPOFF;
1306 }
1307
bedfd056
L
1308 /* When we are called from elf_x86_64_relocate_section, there may
1309 be additional transitions based on TLS_TYPE. */
1310 if (from_relocate_section)
142411ca
L
1311 {
1312 unsigned int new_to_type = to_type;
1313
51537393 1314 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
142411ca
L
1315 new_to_type = R_X86_64_TPOFF32;
1316
1317 if (to_type == R_X86_64_TLSGD
1318 || to_type == R_X86_64_GOTPC32_TLSDESC
1319 || to_type == R_X86_64_TLSDESC_CALL)
1320 {
1321 if (tls_type == GOT_TLS_IE)
1322 new_to_type = R_X86_64_GOTTPOFF;
1323 }
1324
1325 /* We checked the transition before when we were called from
351f65ca 1326 elf_x86_64_check_relocs. We only want to check the new
142411ca
L
1327 transition which hasn't been checked before. */
1328 check = new_to_type != to_type && from_type == to_type;
1329 to_type = new_to_type;
1330 }
1331
1332 break;
1333
bffbf940 1334 case R_X86_64_TLSLD:
0e1862bb 1335 if (bfd_link_executable (info))
142411ca
L
1336 to_type = R_X86_64_TPOFF32;
1337 break;
1338
1339 default:
1340 return TRUE;
bffbf940
JJ
1341 }
1342
142411ca
L
1343 /* Return TRUE if there is no transition. */
1344 if (from_type == to_type)
1345 return TRUE;
1346
1347 /* Check if the transition can be performed. */
1348 if (check
351f65ca
L
1349 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1350 symtab_hdr, sym_hashes,
1351 from_type, rel, relend))
142411ca 1352 {
2f629d23 1353 reloc_howto_type *from, *to;
4c544807 1354 const char *name;
142411ca 1355
351f65ca
L
1356 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1357 to = elf_x86_64_rtype_to_howto (abfd, to_type);
142411ca 1358
f3185997
NC
1359 if (from == NULL || to == NULL)
1360 return FALSE;
1361
4c544807
L
1362 if (h)
1363 name = h->root.root.string;
1364 else
1365 {
0afcef53 1366 struct elf_x86_link_hash_table *htab;
4dfe6ac6 1367
0afcef53 1368 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6
NC
1369 if (htab == NULL)
1370 name = "*unknown*";
1371 else
1372 {
1373 Elf_Internal_Sym *isym;
1374
1375 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1376 abfd, r_symndx);
1377 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1378 }
4c544807
L
1379 }
1380
4eca0228 1381 _bfd_error_handler
695344c0 1382 /* xgettext:c-format */
2dcf00ce
AM
1383 (_("%pB: TLS transition from %s to %s against `%s' at %#" PRIx64
1384 " in section `%pA' failed"),
1385 abfd, from->name, to->name, name, (uint64_t) rel->r_offset, sec);
142411ca
L
1386 bfd_set_error (bfd_error_bad_value);
1387 return FALSE;
1388 }
1389
1390 *r_type = to_type;
1391 return TRUE;
bffbf940
JJ
1392}
1393
c1d11331
L
1394/* Rename some of the generic section flags to better document how they
1395 are used here. */
6999821f 1396#define check_relocs_failed sec_flg0
338c190a
L
1397
1398static bfd_boolean
aab921ad
L
1399elf_x86_64_need_pic (struct bfd_link_info *info,
1400 bfd *input_bfd, asection *sec,
338c190a
L
1401 struct elf_link_hash_entry *h,
1402 Elf_Internal_Shdr *symtab_hdr,
1403 Elf_Internal_Sym *isym,
1404 reloc_howto_type *howto)
1405{
1406 const char *v = "";
1407 const char *und = "";
1408 const char *pic = "";
aab921ad 1409 const char *object;
338c190a
L
1410
1411 const char *name;
1412 if (h)
1413 {
1414 name = h->root.root.string;
1415 switch (ELF_ST_VISIBILITY (h->other))
1416 {
1417 case STV_HIDDEN:
1418 v = _("hidden symbol ");
1419 break;
1420 case STV_INTERNAL:
1421 v = _("internal symbol ");
1422 break;
1423 case STV_PROTECTED:
1424 v = _("protected symbol ");
1425 break;
1426 default:
0afcef53 1427 if (((struct elf_x86_link_hash_entry *) h)->def_protected)
a5b4ee94
L
1428 v = _("protected symbol ");
1429 else
1430 v = _("symbol ");
9a7f0679 1431 pic = NULL;
338c190a
L
1432 break;
1433 }
1434
83924b38 1435 if (!SYMBOL_DEFINED_NON_SHARED_P (h) && !h->def_dynamic)
338c190a
L
1436 und = _("undefined ");
1437 }
1438 else
1439 {
1440 name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
9a7f0679 1441 pic = NULL;
338c190a
L
1442 }
1443
aab921ad 1444 if (bfd_link_dll (info))
9a7f0679
L
1445 {
1446 object = _("a shared object");
1447 if (!pic)
1448 pic = _("; recompile with -fPIC");
1449 }
aab921ad 1450 else
9a7f0679
L
1451 {
1452 if (bfd_link_pie (info))
1453 object = _("a PIE object");
1454 else
1455 object = _("a PDE object");
1456 if (!pic)
1457 pic = _("; recompile with -fPIE");
1458 }
aab921ad 1459
695344c0 1460 /* xgettext:c-format */
871b3ab2 1461 _bfd_error_handler (_("%pB: relocation %s against %s%s`%s' can "
aab921ad
L
1462 "not be used when making %s%s"),
1463 input_bfd, howto->name, und, v, name,
1464 object, pic);
338c190a
L
1465 bfd_set_error (bfd_error_bad_value);
1466 sec->check_relocs_failed = 1;
1467 return FALSE;
1468}
c1d11331 1469
c175a657
L
1470/* With the local symbol, foo, we convert
1471 mov foo@GOTPCREL(%rip), %reg
1472 to
1473 lea foo(%rip), %reg
1474 and convert
1475 call/jmp *foo@GOTPCREL(%rip)
1476 to
1477 nop call foo/jmp foo nop
1478 When PIC is false, convert
1479 test %reg, foo@GOTPCREL(%rip)
1480 to
1481 test $foo, %reg
1482 and convert
1483 binop foo@GOTPCREL(%rip), %reg
1484 to
1485 binop $foo, %reg
1486 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1487 instructions. */
1488
1489static bfd_boolean
8b43e456 1490elf_x86_64_convert_load_reloc (bfd *abfd,
c175a657 1491 bfd_byte *contents,
6999821f 1492 unsigned int *r_type_p,
c175a657
L
1493 Elf_Internal_Rela *irel,
1494 struct elf_link_hash_entry *h,
1495 bfd_boolean *converted,
1496 struct bfd_link_info *link_info)
1497{
0afcef53 1498 struct elf_x86_link_hash_table *htab;
c175a657 1499 bfd_boolean is_pic;
8b43e456 1500 bfd_boolean no_overflow;
c175a657
L
1501 bfd_boolean relocx;
1502 bfd_boolean to_reloc_pc32;
382aae06
L
1503 bfd_boolean abs_symbol;
1504 bfd_boolean local_ref;
c175a657 1505 asection *tsec;
c175a657
L
1506 bfd_signed_vma raddend;
1507 unsigned int opcode;
1508 unsigned int modrm;
6999821f 1509 unsigned int r_type = *r_type_p;
c175a657 1510 unsigned int r_symndx;
c175a657 1511 bfd_vma roff = irel->r_offset;
382aae06 1512 bfd_vma abs_relocation;
c175a657
L
1513
1514 if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
1515 return TRUE;
1516
1517 raddend = irel->r_addend;
1518 /* Addend for 32-bit PC-relative relocation must be -4. */
1519 if (raddend != -4)
1520 return TRUE;
1521
0afcef53 1522 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
c175a657
L
1523 is_pic = bfd_link_pic (link_info);
1524
1525 relocx = (r_type == R_X86_64_GOTPCRELX
1526 || r_type == R_X86_64_REX_GOTPCRELX);
1527
8b43e456
L
1528 /* TRUE if --no-relax is used. */
1529 no_overflow = link_info->disable_target_specific_optimizations > 1;
c175a657
L
1530
1531 r_symndx = htab->r_sym (irel->r_info);
1532
1533 opcode = bfd_get_8 (abfd, contents + roff - 2);
1534
1535 /* Convert mov to lea since it has been done for a while. */
1536 if (opcode != 0x8b)
1537 {
1538 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1539 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1540 test, xor instructions. */
1541 if (!relocx)
1542 return TRUE;
1543 }
1544
1545 /* We convert only to R_X86_64_PC32:
1546 1. Branch.
1547 2. R_X86_64_GOTPCREL since we can't modify REX byte.
8b43e456 1548 3. no_overflow is true.
c175a657
L
1549 4. PIC.
1550 */
1551 to_reloc_pc32 = (opcode == 0xff
1552 || !relocx
8b43e456 1553 || no_overflow
c175a657
L
1554 || is_pic);
1555
382aae06
L
1556 abs_symbol = FALSE;
1557 abs_relocation = 0;
1558
c175a657
L
1559 /* Get the symbol referred to by the reloc. */
1560 if (h == NULL)
1561 {
1562 Elf_Internal_Sym *isym
1563 = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
1564
1565 /* Skip relocation against undefined symbols. */
1566 if (isym->st_shndx == SHN_UNDEF)
1567 return TRUE;
1568
382aae06 1569 local_ref = TRUE;
c175a657 1570 if (isym->st_shndx == SHN_ABS)
382aae06
L
1571 {
1572 tsec = bfd_abs_section_ptr;
1573 abs_symbol = TRUE;
1574 abs_relocation = isym->st_value;
1575 }
c175a657
L
1576 else if (isym->st_shndx == SHN_COMMON)
1577 tsec = bfd_com_section_ptr;
1578 else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1579 tsec = &_bfd_elf_large_com_section;
1580 else
1581 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
c175a657
L
1582 }
1583 else
1584 {
1585 /* Undefined weak symbol is only bound locally in executable
1586 and its reference is resolved as 0 without relocation
1587 overflow. We can only perform this optimization for
1588 GOTPCRELX relocations since we need to modify REX byte.
1589 It is OK convert mov with R_X86_64_GOTPCREL to
1590 R_X86_64_PC32. */
0a27fed7
L
1591 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
1592
382aae06
L
1593 abs_symbol = ABS_SYMBOL_P (h);
1594 abs_relocation = h->root.u.def.value;
1595
0a27fed7
L
1596 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1597 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
c175a657 1598 if ((relocx || opcode == 0x8b)
0a27fed7
L
1599 && (h->root.type == bfd_link_hash_undefweak
1600 && !eh->linker_def
1601 && local_ref))
c175a657
L
1602 {
1603 if (opcode == 0xff)
1604 {
1605 /* Skip for branch instructions since R_X86_64_PC32
1606 may overflow. */
8b43e456 1607 if (no_overflow)
c175a657
L
1608 return TRUE;
1609 }
1610 else if (relocx)
1611 {
1612 /* For non-branch instructions, we can convert to
1613 R_X86_64_32/R_X86_64_32S since we know if there
1614 is a REX byte. */
1615 to_reloc_pc32 = FALSE;
1616 }
1617
1618 /* Since we don't know the current PC when PIC is true,
1619 we can't convert to R_X86_64_PC32. */
1620 if (to_reloc_pc32 && is_pic)
1621 return TRUE;
1622
1623 goto convert;
1624 }
1625 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1626 ld.so may use its link-time address. */
cbd0eecf 1627 else if (h->start_stop
0a27fed7 1628 || eh->linker_def
cbd0eecf
L
1629 || ((h->def_regular
1630 || h->root.type == bfd_link_hash_defined
1631 || h->root.type == bfd_link_hash_defweak)
1632 && h != htab->elf.hdynamic
0a27fed7 1633 && local_ref))
c175a657
L
1634 {
1635 /* bfd_link_hash_new or bfd_link_hash_undefined is
1636 set by an assignment in a linker script in
cbd0eecf
L
1637 bfd_elf_record_link_assignment. start_stop is set
1638 on __start_SECNAME/__stop_SECNAME which mark section
1639 SECNAME. */
1640 if (h->start_stop
0a27fed7 1641 || eh->linker_def
cbd0eecf
L
1642 || (h->def_regular
1643 && (h->root.type == bfd_link_hash_new
1644 || h->root.type == bfd_link_hash_undefined
1645 || ((h->root.type == bfd_link_hash_defined
1646 || h->root.type == bfd_link_hash_defweak)
1647 && h->root.u.def.section == bfd_und_section_ptr))))
c175a657
L
1648 {
1649 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
8b43e456 1650 if (no_overflow)
c175a657
L
1651 return TRUE;
1652 goto convert;
1653 }
1654 tsec = h->root.u.def.section;
c175a657
L
1655 }
1656 else
1657 return TRUE;
1658 }
1659
2168b268
L
1660 /* Don't convert GOTPCREL relocation against large section. */
1661 if (elf_section_data (tsec) != NULL
1662 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1663 return TRUE;
1664
8b43e456
L
1665 /* Skip since R_X86_64_PC32/R_X86_64_32/R_X86_64_32S may overflow. */
1666 if (no_overflow)
1667 return TRUE;
c175a657 1668
dc1e8a47 1669 convert:
c175a657
L
1670 if (opcode == 0xff)
1671 {
1672 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1673 unsigned int nop;
1674 unsigned int disp;
1675 bfd_vma nop_offset;
1676
1677 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1678 R_X86_64_PC32. */
1679 modrm = bfd_get_8 (abfd, contents + roff - 1);
1680 if (modrm == 0x25)
1681 {
1682 /* Convert to "jmp foo nop". */
1683 modrm = 0xe9;
1684 nop = NOP_OPCODE;
1685 nop_offset = irel->r_offset + 3;
1686 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1687 irel->r_offset -= 1;
1688 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1689 }
1690 else
1691 {
0afcef53
L
1692 struct elf_x86_link_hash_entry *eh
1693 = (struct elf_x86_link_hash_entry *) h;
e2cbcd91 1694
c175a657
L
1695 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1696 is a nop prefix. */
1697 modrm = 0xe8;
e2cbcd91
L
1698 /* To support TLS optimization, always use addr32 prefix for
1699 "call *__tls_get_addr@GOTPCREL(%rip)". */
1d4af308 1700 if (eh && eh->tls_get_addr)
c175a657 1701 {
e2cbcd91
L
1702 nop = 0x67;
1703 nop_offset = irel->r_offset - 2;
c175a657
L
1704 }
1705 else
e2cbcd91 1706 {
5b9c07b2
L
1707 nop = htab->params->call_nop_byte;
1708 if (htab->params->call_nop_as_suffix)
e2cbcd91
L
1709 {
1710 nop_offset = irel->r_offset + 3;
1711 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1712 irel->r_offset -= 1;
1713 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1714 }
1715 else
1716 nop_offset = irel->r_offset - 2;
1717 }
c175a657
L
1718 }
1719 bfd_put_8 (abfd, nop, contents + nop_offset);
1720 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1721 r_type = R_X86_64_PC32;
1722 }
1723 else
1724 {
1725 unsigned int rex;
1726 unsigned int rex_mask = REX_R;
1727
1728 if (r_type == R_X86_64_REX_GOTPCRELX)
1729 rex = bfd_get_8 (abfd, contents + roff - 3);
1730 else
1731 rex = 0;
1732
1733 if (opcode == 0x8b)
1734 {
382aae06
L
1735 if (abs_symbol && local_ref)
1736 to_reloc_pc32 = FALSE;
1737
c175a657
L
1738 if (to_reloc_pc32)
1739 {
1740 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1741 "lea foo(%rip), %reg". */
1742 opcode = 0x8d;
1743 r_type = R_X86_64_PC32;
1744 }
1745 else
1746 {
1747 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1748 "mov $foo, %reg". */
1749 opcode = 0xc7;
1750 modrm = bfd_get_8 (abfd, contents + roff - 1);
1751 modrm = 0xc0 | (modrm & 0x38) >> 3;
1752 if ((rex & REX_W) != 0
1753 && ABI_64_P (link_info->output_bfd))
1754 {
1755 /* Keep the REX_W bit in REX byte for LP64. */
1756 r_type = R_X86_64_32S;
1757 goto rewrite_modrm_rex;
1758 }
1759 else
1760 {
1761 /* If the REX_W bit in REX byte isn't needed,
1762 use R_X86_64_32 and clear the W bit to avoid
1763 sign-extend imm32 to imm64. */
1764 r_type = R_X86_64_32;
1765 /* Clear the W bit in REX byte. */
1766 rex_mask |= REX_W;
1767 goto rewrite_modrm_rex;
1768 }
1769 }
1770 }
1771 else
1772 {
1773 /* R_X86_64_PC32 isn't supported. */
1774 if (to_reloc_pc32)
1775 return TRUE;
1776
1777 modrm = bfd_get_8 (abfd, contents + roff - 1);
1778 if (opcode == 0x85)
1779 {
1780 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1781 "test $foo, %reg". */
1782 modrm = 0xc0 | (modrm & 0x38) >> 3;
1783 opcode = 0xf7;
1784 }
1785 else
1786 {
1787 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1788 "binop $foo, %reg". */
1789 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1790 opcode = 0x81;
1791 }
1792
1793 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1794 overflow when sign-extending imm32 to imm64. */
1795 r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
1796
dc1e8a47 1797 rewrite_modrm_rex:
382aae06
L
1798 if (abs_relocation)
1799 {
1800 /* Check if R_X86_64_32S/R_X86_64_32 fits. */
1801 if (r_type == R_X86_64_32S)
1802 {
1803 if ((abs_relocation + 0x80000000) > 0xffffffff)
1804 return TRUE;
1805 }
1806 else
1807 {
1808 if (abs_relocation > 0xffffffff)
1809 return TRUE;
1810 }
1811 }
1812
c175a657
L
1813 bfd_put_8 (abfd, modrm, contents + roff - 1);
1814
1815 if (rex)
1816 {
1817 /* Move the R bit to the B bit in REX byte. */
1818 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
1819 bfd_put_8 (abfd, rex, contents + roff - 3);
1820 }
1821
1822 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
1823 irel->r_addend = 0;
1824 }
1825
1826 bfd_put_8 (abfd, opcode, contents + roff - 2);
1827 }
1828
6999821f 1829 *r_type_p = r_type;
78984959
L
1830 irel->r_info = htab->r_info (r_symndx,
1831 r_type | R_X86_64_converted_reloc_bit);
c175a657
L
1832
1833 *converted = TRUE;
1834
1835 return TRUE;
1836}
1837
70256ad8 1838/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
1839 calculate needed space in the global offset table, procedure
1840 linkage table, and dynamic reloc sections. */
70256ad8 1841
b34976b6 1842static bfd_boolean
351f65ca
L
1843elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1844 asection *sec,
1845 const Elf_Internal_Rela *relocs)
70256ad8 1846{
0afcef53 1847 struct elf_x86_link_hash_table *htab;
70256ad8
AJ
1848 Elf_Internal_Shdr *symtab_hdr;
1849 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
1850 const Elf_Internal_Rela *rel;
1851 const Elf_Internal_Rela *rel_end;
70256ad8 1852 asection *sreloc;
bedfd056 1853 bfd_byte *contents;
5e5e02ae 1854 bfd_boolean converted;
70256ad8 1855
0e1862bb 1856 if (bfd_link_relocatable (info))
b34976b6 1857 return TRUE;
70256ad8 1858
0afcef53 1859 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6 1860 if (htab == NULL)
afd9acee
L
1861 {
1862 sec->check_relocs_failed = 1;
1863 return FALSE;
1864 }
4dfe6ac6 1865
fe53b4a4
L
1866 BFD_ASSERT (is_x86_elf (abfd, htab));
1867
bedfd056
L
1868 /* Get the section contents. */
1869 if (elf_section_data (sec)->this_hdr.contents != NULL)
1870 contents = elf_section_data (sec)->this_hdr.contents;
1871 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1872 {
1873 sec->check_relocs_failed = 1;
1874 return FALSE;
1875 }
1876
0ffa91dd 1877 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8 1878 sym_hashes = elf_sym_hashes (abfd);
70256ad8 1879
5e5e02ae
L
1880 converted = FALSE;
1881
c434dee6 1882 sreloc = NULL;
cbe950e9 1883
70256ad8
AJ
1884 rel_end = relocs + sec->reloc_count;
1885 for (rel = relocs; rel < rel_end; rel++)
1886 {
bffbf940 1887 unsigned int r_type;
d42c267e 1888 unsigned int r_symndx;
70256ad8 1889 struct elf_link_hash_entry *h;
0afcef53 1890 struct elf_x86_link_hash_entry *eh;
4c544807
L
1891 Elf_Internal_Sym *isym;
1892 const char *name;
06a6a421 1893 bfd_boolean size_reloc;
6999821f 1894 bfd_boolean converted_reloc;
382aae06 1895 bfd_boolean no_dynreloc;
70256ad8 1896
351f65ca
L
1897 r_symndx = htab->r_sym (rel->r_info);
1898 r_type = ELF32_R_TYPE (rel->r_info);
c434dee6
AJ
1899
1900 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1901 {
695344c0 1902 /* xgettext:c-format */
871b3ab2 1903 _bfd_error_handler (_("%pB: bad symbol index: %d"),
4eca0228 1904 abfd, r_symndx);
afd9acee 1905 goto error_return;
c434dee6
AJ
1906 }
1907
70256ad8 1908 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
1909 {
1910 /* A local symbol. */
c2e61a4e
L
1911 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1912 abfd, r_symndx);
1913 if (isym == NULL)
afd9acee 1914 goto error_return;
c25bc9fc
L
1915
1916 /* Check relocation against local STT_GNU_IFUNC symbol. */
351f65ca 1917 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c25bc9fc 1918 {
0afcef53
L
1919 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
1920 TRUE);
c25bc9fc 1921 if (h == NULL)
afd9acee 1922 goto error_return;
6bbec505 1923
c25bc9fc 1924 /* Fake a STT_GNU_IFUNC symbol. */
6322e5c5
L
1925 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1926 isym, NULL);
c25bc9fc
L
1927 h->type = STT_GNU_IFUNC;
1928 h->def_regular = 1;
1929 h->ref_regular = 1;
1930 h->forced_local = 1;
1931 h->root.type = bfd_link_hash_defined;
1932 }
1933 else
1934 h = NULL;
1935 }
70256ad8 1936 else
71cb9464 1937 {
4c544807 1938 isym = NULL;
71cb9464
L
1939 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1940 while (h->root.type == bfd_link_hash_indirect
1941 || h->root.type == bfd_link_hash_warning)
1942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 1943 }
cbe950e9 1944
d1534d16
L
1945 /* Check invalid x32 relocations. */
1946 if (!ABI_64_P (abfd))
1947 switch (r_type)
1948 {
1949 default:
1950 break;
1951
d1534d16
L
1952 case R_X86_64_DTPOFF64:
1953 case R_X86_64_TPOFF64:
1954 case R_X86_64_PC64:
1955 case R_X86_64_GOTOFF64:
1956 case R_X86_64_GOT64:
1957 case R_X86_64_GOTPCREL64:
1958 case R_X86_64_GOTPC64:
1959 case R_X86_64_GOTPLT64:
1960 case R_X86_64_PLTOFF64:
1961 {
1962 if (h)
1963 name = h->root.root.string;
1964 else
1965 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1966 NULL);
4eca0228 1967 _bfd_error_handler
695344c0 1968 /* xgettext:c-format */
871b3ab2 1969 (_("%pB: relocation %s against symbol `%s' isn't "
d1534d16
L
1970 "supported in x32 mode"), abfd,
1971 x86_64_elf_howto_table[r_type].name, name);
1972 bfd_set_error (bfd_error_bad_value);
afd9acee 1973 goto error_return;
d1534d16
L
1974 }
1975 break;
1976 }
1977
c25bc9fc
L
1978 if (h != NULL)
1979 {
ad1e85de
L
1980 /* It is referenced by a non-shared object. */
1981 h->ref_regular = 1;
71cb9464 1982 }
70256ad8 1983
6999821f
L
1984 converted_reloc = FALSE;
1985 if ((r_type == R_X86_64_GOTPCREL
1986 || r_type == R_X86_64_GOTPCRELX
1987 || r_type == R_X86_64_REX_GOTPCRELX)
1988 && (h == NULL || h->type != STT_GNU_IFUNC))
1989 {
1990 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1991 if (!elf_x86_64_convert_load_reloc (abfd, contents, &r_type,
1992 irel, h, &converted_reloc,
1993 info))
1994 goto error_return;
5e5e02ae
L
1995
1996 if (converted_reloc)
1997 converted = TRUE;
6999821f
L
1998 }
1999
382aae06
L
2000 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
2001 symtab_hdr, &no_dynreloc))
2002 return FALSE;
2003
bedfd056 2004 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
351f65ca
L
2005 symtab_hdr, sym_hashes,
2006 &r_type, GOT_UNKNOWN,
bedfd056 2007 rel, rel_end, h, r_symndx, FALSE))
afd9acee 2008 goto error_return;
142411ca 2009
cd048363
L
2010 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
2011 if (h == htab->elf.hgot)
2012 htab->got_referenced = TRUE;
2013
0afcef53 2014 eh = (struct elf_x86_link_hash_entry *) h;
bffbf940 2015 switch (r_type)
70256ad8 2016 {
bffbf940 2017 case R_X86_64_TLSLD:
0f09b4d8 2018 htab->tls_ld_or_ldm_got.refcount = 1;
bffbf940
JJ
2019 goto create_got;
2020
2021 case R_X86_64_TPOFF32:
0e1862bb 2022 if (!bfd_link_executable (info) && ABI_64_P (abfd))
aab921ad 2023 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
338c190a 2024 &x86_64_elf_howto_table[r_type]);
aec6b87e 2025 if (eh != NULL)
98b273dc 2026 eh->zero_undefweak &= 0x2;
bffbf940 2027 break;
c434dee6 2028
bffbf940 2029 case R_X86_64_GOTTPOFF:
0e1862bb 2030 if (!bfd_link_executable (info))
bffbf940
JJ
2031 info->flags |= DF_STATIC_TLS;
2032 /* Fall through */
70256ad8 2033
bffbf940
JJ
2034 case R_X86_64_GOT32:
2035 case R_X86_64_GOTPCREL:
56ceb5b5
L
2036 case R_X86_64_GOTPCRELX:
2037 case R_X86_64_REX_GOTPCRELX:
bffbf940 2038 case R_X86_64_TLSGD:
7b81dfbb
AJ
2039 case R_X86_64_GOT64:
2040 case R_X86_64_GOTPCREL64:
2041 case R_X86_64_GOTPLT64:
67a4f2b7
AO
2042 case R_X86_64_GOTPC32_TLSDESC:
2043 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
2044 /* This symbol requires a global offset table entry. */
2045 {
2046 int tls_type, old_tls_type;
2047
2048 switch (r_type)
2049 {
382aae06
L
2050 default:
2051 tls_type = GOT_NORMAL;
2052 if (h)
2053 {
2054 if (ABS_SYMBOL_P (h))
2055 tls_type = GOT_ABS;
2056 }
2057 else if (isym->st_shndx == SHN_ABS)
2058 tls_type = GOT_ABS;
2059 break;
2060 case R_X86_64_TLSGD:
2061 tls_type = GOT_TLS_GD;
2062 break;
2063 case R_X86_64_GOTTPOFF:
2064 tls_type = GOT_TLS_IE;
2065 break;
67a4f2b7
AO
2066 case R_X86_64_GOTPC32_TLSDESC:
2067 case R_X86_64_TLSDESC_CALL:
382aae06
L
2068 tls_type = GOT_TLS_GDESC;
2069 break;
bffbf940
JJ
2070 }
2071
2072 if (h != NULL)
2073 {
0f09b4d8 2074 h->got.refcount = 1;
aec6b87e 2075 old_tls_type = eh->tls_type;
bffbf940
JJ
2076 }
2077 else
2078 {
2079 bfd_signed_vma *local_got_refcounts;
2080
2081 /* This is a global offset table entry for a local symbol. */
2082 local_got_refcounts = elf_local_got_refcounts (abfd);
2083 if (local_got_refcounts == NULL)
2084 {
2085 bfd_size_type size;
2086
2087 size = symtab_hdr->sh_info;
67a4f2b7
AO
2088 size *= sizeof (bfd_signed_vma)
2089 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
2090 local_got_refcounts = ((bfd_signed_vma *)
2091 bfd_zalloc (abfd, size));
2092 if (local_got_refcounts == NULL)
afd9acee 2093 goto error_return;
bffbf940 2094 elf_local_got_refcounts (abfd) = local_got_refcounts;
0afcef53 2095 elf_x86_local_tlsdesc_gotent (abfd)
67a4f2b7 2096 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
0afcef53 2097 elf_x86_local_got_tls_type (abfd)
67a4f2b7 2098 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940 2099 }
0f09b4d8 2100 local_got_refcounts[r_symndx] = 1;
bffbf940 2101 old_tls_type
0afcef53 2102 = elf_x86_local_got_tls_type (abfd) [r_symndx];
bffbf940
JJ
2103 }
2104
2105 /* If a TLS symbol is accessed using IE at least once,
2106 there is no point to use dynamic model for it. */
2107 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
2108 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2109 || tls_type != GOT_TLS_IE))
bffbf940 2110 {
67a4f2b7 2111 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 2112 tls_type = old_tls_type;
67a4f2b7
AO
2113 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2114 && GOT_TLS_GD_ANY_P (tls_type))
2115 tls_type |= old_tls_type;
bffbf940
JJ
2116 else
2117 {
09a24cbf 2118 if (h)
4c544807
L
2119 name = h->root.root.string;
2120 else
2121 name = bfd_elf_sym_name (abfd, symtab_hdr,
2122 isym, NULL);
4eca0228 2123 _bfd_error_handler
695344c0 2124 /* xgettext:c-format */
871b3ab2 2125 (_("%pB: '%s' accessed both as normal and"
63a5468a 2126 " thread local symbol"),
4c544807 2127 abfd, name);
68c4a57e 2128 bfd_set_error (bfd_error_bad_value);
afd9acee 2129 goto error_return;
bffbf940
JJ
2130 }
2131 }
2132
2133 if (old_tls_type != tls_type)
2134 {
aec6b87e
L
2135 if (eh != NULL)
2136 eh->tls_type = tls_type;
bffbf940 2137 else
0afcef53 2138 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
bffbf940
JJ
2139 }
2140 }
c434dee6
AJ
2141 /* Fall through */
2142
d6ab8113
JB
2143 case R_X86_64_GOTOFF64:
2144 case R_X86_64_GOTPC32:
7b81dfbb 2145 case R_X86_64_GOTPC64:
bffbf940 2146 create_got:
aec6b87e 2147 if (eh != NULL)
98b273dc 2148 eh->zero_undefweak &= 0x2;
70256ad8
AJ
2149 break;
2150
2151 case R_X86_64_PLT32:
c3320543 2152 case R_X86_64_PLT32_BND:
70256ad8 2153 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
2154 actually build the entry in adjust_dynamic_symbol,
2155 because this might be a case of linking PIC code which is
2156 never referenced by a dynamic object, in which case we
2157 don't need to generate a procedure linkage table entry
2158 after all. */
70256ad8
AJ
2159
2160 /* If this is a local symbol, we resolve it directly without
407443a3 2161 creating a procedure linkage table entry. */
70256ad8
AJ
2162 if (h == NULL)
2163 continue;
2164
98b273dc 2165 eh->zero_undefweak &= 0x2;
f5385ebf 2166 h->needs_plt = 1;
79b0c981 2167 h->plt.refcount = 1;
70256ad8
AJ
2168 break;
2169
7b81dfbb
AJ
2170 case R_X86_64_PLTOFF64:
2171 /* This tries to form the 'address' of a function relative
2172 to GOT. For global symbols we need a PLT entry. */
2173 if (h != NULL)
2174 {
2175 h->needs_plt = 1;
79b0c981 2176 h->plt.refcount = 1;
7b81dfbb
AJ
2177 }
2178 goto create_got;
2179
6a3e1bae
L
2180 case R_X86_64_SIZE32:
2181 case R_X86_64_SIZE64:
06a6a421 2182 size_reloc = TRUE;
6a3e1bae
L
2183 goto do_size;
2184
248775ba
L
2185 case R_X86_64_32:
2186 if (!ABI_64_P (abfd))
2187 goto pointer;
1a0670f3 2188 /* Fall through. */
cc78d0af
AJ
2189 case R_X86_64_8:
2190 case R_X86_64_16:
70256ad8 2191 case R_X86_64_32S:
338c190a
L
2192 /* Check relocation overflow as these relocs may lead to
2193 run-time relocation overflow. Don't error out for
1b71fb54 2194 sections we don't care about, such as debug sections or
338c190a 2195 when relocation overflow check is disabled. */
5b9c07b2 2196 if (!htab->params->no_reloc_overflow_check
6999821f 2197 && !converted_reloc
338c190a
L
2198 && (bfd_link_pic (info)
2199 || (bfd_link_executable (info)
2200 && h != NULL
2201 && !h->def_regular
2202 && h->def_dynamic
081b1afe 2203 && (sec->flags & SEC_READONLY) == 0)))
aab921ad 2204 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
338c190a 2205 &x86_64_elf_howto_table[r_type]);
1b71fb54
AJ
2206 /* Fall through. */
2207
81e8046d
L
2208 case R_X86_64_PC8:
2209 case R_X86_64_PC16:
2210 case R_X86_64_PC32:
2211 case R_X86_64_PC32_BND:
d6ab8113 2212 case R_X86_64_PC64:
1b71fb54 2213 case R_X86_64_64:
dc1e8a47 2214 pointer:
aec6b87e 2215 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
98b273dc 2216 eh->zero_undefweak |= 0x2;
bf52d7c7
L
2217 /* We are called after all symbols have been resolved. Only
2218 relocation against STT_GNU_IFUNC symbol must go through
2219 PLT. */
d1ed1c7d
L
2220 if (h != NULL
2221 && (bfd_link_executable (info)
bf52d7c7 2222 || h->type == STT_GNU_IFUNC))
c434dee6 2223 {
79b0c981 2224 bfd_boolean func_pointer_ref = FALSE;
233cc9c1 2225
5db4f0d3
L
2226 if (r_type == R_X86_64_PC32)
2227 {
2228 /* Since something like ".long foo - ." may be used
2229 as pointer, make sure that PLT is used if foo is
2230 a function defined in a shared library. */
2231 if ((sec->flags & SEC_CODE) == 0)
451875b4
L
2232 {
2233 h->pointer_equality_needed = 1;
2234 if (bfd_link_pie (info)
2235 && h->type == STT_FUNC
2236 && !h->def_regular
2237 && h->def_dynamic)
2238 {
2239 h->needs_plt = 1;
2240 h->plt.refcount = 1;
2241 }
2242 }
5db4f0d3
L
2243 }
2244 else if (r_type != R_X86_64_PC32_BND
2245 && r_type != R_X86_64_PC64)
04ebc307
L
2246 {
2247 h->pointer_equality_needed = 1;
2248 /* At run-time, R_X86_64_64 can be resolved for both
2249 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2250 can only be resolved for x32. */
2251 if ((sec->flags & SEC_READONLY) == 0
2252 && (r_type == R_X86_64_64
2253 || (!ABI_64_P (abfd)
2254 && (r_type == R_X86_64_32
2255 || r_type == R_X86_64_32S))))
79b0c981
L
2256 func_pointer_ref = TRUE;
2257 }
2258
2259 if (!func_pointer_ref)
2260 {
2261 /* If this reloc is in a read-only section, we might
2262 need a copy reloc. We can't check reliably at this
2263 stage whether the section is read-only, as input
2264 sections have not yet been mapped to output sections.
2265 Tentatively set the flag for now, and correct in
2266 adjust_dynamic_symbol. */
2267 h->non_got_ref = 1;
2268
2269 /* We may need a .plt entry if the symbol is a function
2270 defined in a shared lib or is a function referenced
2271 from the code or read-only section. */
2272 if (!h->def_regular
2273 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2274 h->plt.refcount = 1;
04ebc307 2275 }
c434dee6 2276 }
70256ad8 2277
06a6a421 2278 size_reloc = FALSE;
dc1e8a47 2279 do_size:
382aae06
L
2280 if (!no_dynreloc
2281 && NEED_DYNAMIC_RELOCATION_P (info, TRUE, h, sec, r_type,
2282 htab->pointer_r_type))
70256ad8 2283 {
e03a8ed8
L
2284 struct elf_dyn_relocs *p;
2285 struct elf_dyn_relocs **head;
c434dee6
AJ
2286
2287 /* We must copy these reloc types into the output file.
2288 Create a reloc section in dynobj and make room for
2289 this reloc. */
70256ad8
AJ
2290 if (sreloc == NULL)
2291 {
83bac4b0 2292 sreloc = _bfd_elf_make_dynamic_reloc_section
82e96e07
L
2293 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
2294 abfd, /*rela?*/ TRUE);
70256ad8 2295
70256ad8 2296 if (sreloc == NULL)
afd9acee 2297 goto error_return;
70256ad8
AJ
2298 }
2299
c434dee6
AJ
2300 /* If this is a global symbol, we count the number of
2301 relocations we need for this symbol. */
2302 if (h != NULL)
190eb1dd 2303 head = &h->dyn_relocs;
c434dee6
AJ
2304 else
2305 {
2306 /* Track dynamic relocs needed for local syms too.
2307 We really need local syms available to do this
2308 easily. Oh well. */
c434dee6 2309 asection *s;
87d72d41 2310 void **vpp;
87d72d41
AM
2311
2312 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2313 abfd, r_symndx);
2314 if (isym == NULL)
afd9acee 2315 goto error_return;
87d72d41
AM
2316
2317 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
c434dee6 2318 if (s == NULL)
87d72d41 2319 s = sec;
70256ad8 2320
e81d3500
DD
2321 /* Beware of type punned pointers vs strict aliasing
2322 rules. */
2323 vpp = &(elf_section_data (s)->local_dynrel);
e03a8ed8 2324 head = (struct elf_dyn_relocs **)vpp;
c434dee6 2325 }
70256ad8 2326
c434dee6
AJ
2327 p = *head;
2328 if (p == NULL || p->sec != sec)
2329 {
986f0783 2330 size_t amt = sizeof *p;
d8045f23 2331
e03a8ed8 2332 p = ((struct elf_dyn_relocs *)
c434dee6 2333 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 2334 if (p == NULL)
afd9acee 2335 goto error_return;
c434dee6
AJ
2336 p->next = *head;
2337 *head = p;
2338 p->sec = sec;
2339 p->count = 0;
2340 p->pc_count = 0;
70256ad8 2341 }
c434dee6
AJ
2342
2343 p->count += 1;
06a6a421 2344 /* Count size relocation as PC-relative relocation. */
daf1c414 2345 if (X86_PCREL_TYPE_P (r_type) || size_reloc)
c434dee6 2346 p->pc_count += 1;
70256ad8
AJ
2347 }
2348 break;
fe4770f4
AJ
2349
2350 /* This relocation describes the C++ object vtable hierarchy.
2351 Reconstruct it for later use during GC. */
2352 case R_X86_64_GNU_VTINHERIT:
c152c796 2353 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
afd9acee 2354 goto error_return;
fe4770f4
AJ
2355 break;
2356
2357 /* This relocation describes which C++ vtable entries are actually
2358 used. Record for later use during GC. */
2359 case R_X86_64_GNU_VTENTRY:
a0ea3a14 2360 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
afd9acee 2361 goto error_return;
fe4770f4 2362 break;
c434dee6
AJ
2363
2364 default:
2365 break;
70256ad8
AJ
2366 }
2367 }
2368
bedfd056
L
2369 if (elf_section_data (sec)->this_hdr.contents != contents)
2370 {
5e5e02ae 2371 if (!converted && !info->keep_memory)
bedfd056
L
2372 free (contents);
2373 else
2374 {
5e5e02ae
L
2375 /* Cache the section contents for elf_link_input_bfd if any
2376 load is converted or --no-keep-memory isn't used. */
bedfd056
L
2377 elf_section_data (sec)->this_hdr.contents = contents;
2378 }
2379 }
2380
5e5e02ae
L
2381 /* Cache relocations if any load is converted. */
2382 if (elf_section_data (sec)->relocs != relocs && converted)
2383 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
2384
b34976b6 2385 return TRUE;
afd9acee 2386
dc1e8a47 2387 error_return:
bedfd056
L
2388 if (elf_section_data (sec)->this_hdr.contents != contents)
2389 free (contents);
afd9acee
L
2390 sec->check_relocs_failed = 1;
2391 return FALSE;
70256ad8
AJ
2392}
2393
bffbf940
JJ
2394/* Return the relocation value for @tpoff relocation
2395 if STT_TLS virtual address is ADDRESS. */
2396
2397static bfd_vma
351f65ca 2398elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 2399{
e1918d23 2400 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
2401 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2402 bfd_vma static_tls_size;
bffbf940
JJ
2403
2404 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 2405 if (htab->tls_sec == NULL)
bffbf940 2406 return 0;
7dc98aea
RO
2407
2408 /* Consider special static TLS alignment requirements. */
2409 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2410 return address - static_tls_size - htab->tls_sec->vma;
bffbf940
JJ
2411}
2412
8d88c4ca
NC
2413/* Relocate an x86_64 ELF section. */
2414
b34976b6 2415static bfd_boolean
351f65ca
L
2416elf_x86_64_relocate_section (bfd *output_bfd,
2417 struct bfd_link_info *info,
2418 bfd *input_bfd,
2419 asection *input_section,
2420 bfd_byte *contents,
2421 Elf_Internal_Rela *relocs,
2422 Elf_Internal_Sym *local_syms,
2423 asection **local_sections)
8d88c4ca 2424{
0afcef53 2425 struct elf_x86_link_hash_table *htab;
8d88c4ca
NC
2426 Elf_Internal_Shdr *symtab_hdr;
2427 struct elf_link_hash_entry **sym_hashes;
2428 bfd_vma *local_got_offsets;
67a4f2b7 2429 bfd_vma *local_tlsdesc_gotents;
c434dee6 2430 Elf_Internal_Rela *rel;
60f2e42e 2431 Elf_Internal_Rela *wrel;
8d88c4ca 2432 Elf_Internal_Rela *relend;
38b12349 2433 unsigned int plt_entry_size;
8d88c4ca 2434
338c190a
L
2435 /* Skip if check_relocs failed. */
2436 if (input_section->check_relocs_failed)
2437 return FALSE;
2438
0afcef53 2439 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6
NC
2440 if (htab == NULL)
2441 return FALSE;
fe53b4a4 2442
7af5d5c4
AM
2443 if (!is_x86_elf (input_bfd, htab))
2444 {
2445 bfd_set_error (bfd_error_wrong_format);
2446 return FALSE;
2447 }
fe53b4a4 2448
765e526c 2449 plt_entry_size = htab->plt.plt_entry_size;
0ffa91dd 2450 symtab_hdr = &elf_symtab_hdr (input_bfd);
8d88c4ca
NC
2451 sym_hashes = elf_sym_hashes (input_bfd);
2452 local_got_offsets = elf_local_got_offsets (input_bfd);
0afcef53 2453 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
8d88c4ca 2454
0afcef53 2455 _bfd_x86_elf_set_tls_module_base (info);
9f03412a 2456
60f2e42e 2457 rel = wrel = relocs;
8d88c4ca 2458 relend = relocs + input_section->reloc_count;
60f2e42e 2459 for (; rel < relend; wrel++, rel++)
8d88c4ca 2460 {
419414ea 2461 unsigned int r_type, r_type_tls;
8d88c4ca
NC
2462 reloc_howto_type *howto;
2463 unsigned long r_symndx;
2464 struct elf_link_hash_entry *h;
0afcef53 2465 struct elf_x86_link_hash_entry *eh;
8d88c4ca
NC
2466 Elf_Internal_Sym *sym;
2467 asection *sec;
0ff2b86e 2468 bfd_vma off, offplt, plt_offset;
8d88c4ca 2469 bfd_vma relocation;
b34976b6 2470 bfd_boolean unresolved_reloc;
8d88c4ca 2471 bfd_reloc_status_type r;
bffbf940 2472 int tls_type;
0ff2b86e 2473 asection *base_got, *resolved_plt;
1788fc08 2474 bfd_vma st_size;
aec6b87e 2475 bfd_boolean resolved_to_zero;
9e9821dd 2476 bfd_boolean relative_reloc;
78984959 2477 bfd_boolean converted_reloc;
aebcc8ff 2478 bfd_boolean need_copy_reloc_in_pie;
81e8046d 2479 bfd_boolean no_copyreloc_p;
8d88c4ca 2480
351f65ca 2481 r_type = ELF32_R_TYPE (rel->r_info);
fe4770f4
AJ
2482 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2483 || r_type == (int) R_X86_64_GNU_VTENTRY)
18954b29
L
2484 {
2485 if (wrel != rel)
2486 *wrel = *rel;
2487 continue;
2488 }
8d88c4ca 2489
b638b5d5 2490 r_symndx = htab->r_sym (rel->r_info);
78984959 2491 converted_reloc = (r_type & R_X86_64_converted_reloc_bit) != 0;
b638b5d5
L
2492 if (converted_reloc)
2493 {
2494 r_type &= ~R_X86_64_converted_reloc_bit;
2495 rel->r_info = htab->r_info (r_symndx, r_type);
2496 }
78984959 2497
76268e02
L
2498 howto = elf_x86_64_rtype_to_howto (input_bfd, r_type);
2499 if (howto == NULL)
47aeb64c 2500 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
8d88c4ca 2501
8d88c4ca
NC
2502 h = NULL;
2503 sym = NULL;
2504 sec = NULL;
b34976b6 2505 unresolved_reloc = FALSE;
8d88c4ca 2506 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
2507 {
2508 sym = local_syms + r_symndx;
2509 sec = local_sections[r_symndx];
c434dee6 2510
c25bc9fc
L
2511 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2512 &sec, rel);
1788fc08 2513 st_size = sym->st_size;
c25bc9fc
L
2514
2515 /* Relocate against local STT_GNU_IFUNC symbol. */
0e1862bb 2516 if (!bfd_link_relocatable (info)
351f65ca 2517 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc 2518 {
0afcef53
L
2519 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
2520 rel, FALSE);
c25bc9fc
L
2521 if (h == NULL)
2522 abort ();
2523
eed180f8 2524 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
2525 h->root.u.def.value = sym->st_value;
2526 h->root.u.def.section = sec;
2527 }
8da6118f 2528 }
8d88c4ca 2529 else
8da6118f 2530 {
c9736ba0 2531 bfd_boolean warned ATTRIBUTE_UNUSED;
62d887d4 2532 bfd_boolean ignored ATTRIBUTE_UNUSED;
c434dee6 2533
b2a8e766
AM
2534 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2535 r_symndx, symtab_hdr, sym_hashes,
2536 h, sec, relocation,
62d887d4 2537 unresolved_reloc, warned, ignored);
1788fc08 2538 st_size = h->size;
8da6118f 2539 }
ab96bf03 2540
dbaa2011 2541 if (sec != NULL && discarded_section (sec))
60f2e42e
L
2542 {
2543 _bfd_clear_contents (howto, input_bfd, input_section,
0930cb30 2544 contents, rel->r_offset);
60f2e42e
L
2545 wrel->r_offset = rel->r_offset;
2546 wrel->r_info = 0;
2547 wrel->r_addend = 0;
2548
2549 /* For ld -r, remove relocations in debug sections against
2550 sections defined in discarded sections. Not done for
2551 eh_frame editing code expects to be present. */
2552 if (bfd_link_relocatable (info)
2553 && (input_section->flags & SEC_DEBUGGING))
2554 wrel--;
2555
2556 continue;
2557 }
ab96bf03 2558
0e1862bb 2559 if (bfd_link_relocatable (info))
2d5da473
L
2560 {
2561 if (wrel != rel)
2562 *wrel = *rel;
2563 continue;
2564 }
ab96bf03 2565
1788fc08 2566 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
64d25c44 2567 {
1788fc08
L
2568 if (r_type == R_X86_64_64)
2569 {
2570 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
2571 zero-extend it to 64bit if addend is zero. */
2572 r_type = R_X86_64_32;
2573 memset (contents + rel->r_offset + 4, 0, 4);
2574 }
2575 else if (r_type == R_X86_64_SIZE64)
2576 {
2577 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
2578 zero-extend it to 64bit if addend is zero. */
2579 r_type = R_X86_64_SIZE32;
2580 memset (contents + rel->r_offset + 4, 0, 4);
2581 }
64d25c44
L
2582 }
2583
0afcef53 2584 eh = (struct elf_x86_link_hash_entry *) h;
0ff2b86e 2585
cbe950e9
L
2586 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2587 it here if it is defined in a non-shared object. */
2588 if (h != NULL
2589 && h->type == STT_GNU_IFUNC
2590 && h->def_regular)
2591 {
cbe950e9 2592 bfd_vma plt_index;
4c544807 2593 const char *name;
cbe950e9 2594
97dc35c8
L
2595 if ((input_section->flags & SEC_ALLOC) == 0)
2596 {
347a8774
L
2597 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2598 STT_GNU_IFUNC symbol as STT_FUNC. */
2599 if (elf_section_type (input_section) == SHT_NOTE)
2600 goto skip_ifunc;
97dc35c8
L
2601 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2602 sections because such sections are not SEC_ALLOC and
2603 thus ld.so will not process them. */
2604 if ((input_section->flags & SEC_DEBUGGING) != 0)
0eace210 2605 continue;
97dc35c8
L
2606 abort ();
2607 }
233cc9c1
L
2608
2609 switch (r_type)
2610 {
2611 default:
2612 break;
2613
2614 case R_X86_64_GOTPCREL:
2615 case R_X86_64_GOTPCRELX:
2616 case R_X86_64_REX_GOTPCRELX:
2617 case R_X86_64_GOTPCREL64:
2618 base_got = htab->elf.sgot;
2619 off = h->got.offset;
2620
2621 if (base_got == NULL)
2622 abort ();
2623
2624 if (off == (bfd_vma) -1)
2625 {
2626 /* We can't use h->got.offset here to save state, or
2627 even just remember the offset, as finish_dynamic_symbol
2628 would use that as offset into .got. */
2629
2630 if (h->plt.offset == (bfd_vma) -1)
2631 abort ();
2632
2633 if (htab->elf.splt != NULL)
2634 {
38b12349 2635 plt_index = (h->plt.offset / plt_entry_size
765e526c 2636 - htab->plt.has_plt0);
233cc9c1
L
2637 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2638 base_got = htab->elf.sgotplt;
2639 }
2640 else
2641 {
2642 plt_index = h->plt.offset / plt_entry_size;
2643 off = plt_index * GOT_ENTRY_SIZE;
2644 base_got = htab->elf.igotplt;
2645 }
2646
2647 if (h->dynindx == -1
2648 || h->forced_local
2649 || info->symbolic)
2650 {
2651 /* This references the local defitionion. We must
2652 initialize this entry in the global offset table.
2653 Since the offset must always be a multiple of 8,
2654 we use the least significant bit to record
2655 whether we have initialized it already.
2656
2657 When doing a dynamic link, we create a .rela.got
2658 relocation entry to initialize the value. This
2659 is done in the finish_dynamic_symbol routine. */
2660 if ((off & 1) != 0)
2661 off &= ~1;
2662 else
2663 {
2664 bfd_put_64 (output_bfd, relocation,
2665 base_got->contents + off);
2666 /* Note that this is harmless for the GOTPLT64
2667 case, as -1 | 1 still is -1. */
2668 h->got.offset |= 1;
2669 }
2670 }
2671 }
2672
2673 relocation = (base_got->output_section->vma
2674 + base_got->output_offset + off);
2675
2676 goto do_relocation;
2677 }
2678
2679 if (h->plt.offset == (bfd_vma) -1)
2680 {
2681 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2682 if (r_type == htab->pointer_r_type
2683 && (input_section->flags & SEC_CODE) == 0)
2684 goto do_ifunc_pointer;
2685 goto bad_ifunc_reloc;
2686 }
cbe950e9
L
2687
2688 /* STT_GNU_IFUNC symbol must go through PLT. */
0ff2b86e
L
2689 if (htab->elf.splt != NULL)
2690 {
f2c29a16 2691 if (htab->plt_second != NULL)
0ff2b86e 2692 {
f2c29a16
L
2693 resolved_plt = htab->plt_second;
2694 plt_offset = eh->plt_second.offset;
0ff2b86e
L
2695 }
2696 else
2697 {
2698 resolved_plt = htab->elf.splt;
2699 plt_offset = h->plt.offset;
2700 }
2701 }
2702 else
2703 {
2704 resolved_plt = htab->elf.iplt;
2705 plt_offset = h->plt.offset;
2706 }
2707
2708 relocation = (resolved_plt->output_section->vma
2709 + resolved_plt->output_offset + plt_offset);
cbe950e9
L
2710
2711 switch (r_type)
2712 {
2713 default:
dc1e8a47 2714 bad_ifunc_reloc:
4c544807
L
2715 if (h->root.root.string)
2716 name = h->root.root.string;
2717 else
2718 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2719 NULL);
4eca0228 2720 _bfd_error_handler
695344c0 2721 /* xgettext:c-format */
871b3ab2 2722 (_("%pB: relocation %s against STT_GNU_IFUNC "
233cc9c1
L
2723 "symbol `%s' isn't supported"), input_bfd,
2724 howto->name, name);
cbe950e9
L
2725 bfd_set_error (bfd_error_bad_value);
2726 return FALSE;
2727
2728 case R_X86_64_32S:
0e1862bb 2729 if (bfd_link_pic (info))
cbe950e9 2730 abort ();
710ab287
L
2731 goto do_relocation;
2732
248775ba
L
2733 case R_X86_64_32:
2734 if (ABI_64_P (output_bfd))
2735 goto do_relocation;
17672001 2736 /* FALLTHROUGH */
eed180f8 2737 case R_X86_64_64:
dc1e8a47 2738 do_ifunc_pointer:
710ab287
L
2739 if (rel->r_addend != 0)
2740 {
4c544807
L
2741 if (h->root.root.string)
2742 name = h->root.root.string;
2743 else
2744 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2745 sym, NULL);
4eca0228 2746 _bfd_error_handler
695344c0 2747 /* xgettext:c-format */
871b3ab2 2748 (_("%pB: relocation %s against STT_GNU_IFUNC "
2dcf00ce
AM
2749 "symbol `%s' has non-zero addend: %" PRId64),
2750 input_bfd, howto->name, name, (int64_t) rel->r_addend);
710ab287
L
2751 bfd_set_error (bfd_error_bad_value);
2752 return FALSE;
2753 }
2754
2755 /* Generate dynamic relcoation only when there is a
233cc9c1
L
2756 non-GOT reference in a shared object or there is no
2757 PLT. */
2758 if ((bfd_link_pic (info) && h->non_got_ref)
2759 || h->plt.offset == (bfd_vma) -1)
710ab287
L
2760 {
2761 Elf_Internal_Rela outrel;
710ab287
L
2762 asection *sreloc;
2763
c25bc9fc
L
2764 /* Need a dynamic relocation to get the real function
2765 address. */
710ab287
L
2766 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2767 info,
2768 input_section,
2769 rel->r_offset);
2770 if (outrel.r_offset == (bfd_vma) -1
2771 || outrel.r_offset == (bfd_vma) -2)
2772 abort ();
2773
2774 outrel.r_offset += (input_section->output_section->vma
2775 + input_section->output_offset);
2776
cf1070f1 2777 if (POINTER_LOCAL_IFUNC_P (info, h))
710ab287 2778 {
871b3ab2 2779 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
2780 h->root.root.string,
2781 h->root.u.def.section->owner);
2782
710ab287 2783 /* This symbol is resolved locally. */
56b8aada
L
2784 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
2785 outrel.r_addend = (h->root.u.def.value
2786 + h->root.u.def.section->output_section->vma
2787 + h->root.u.def.section->output_offset);
710ab287
L
2788 }
2789 else
2790 {
351f65ca 2791 outrel.r_info = htab->r_info (h->dynindx, r_type);
710ab287
L
2792 outrel.r_addend = 0;
2793 }
2794
233cc9c1
L
2795 /* Dynamic relocations are stored in
2796 1. .rela.ifunc section in PIC object.
2797 2. .rela.got section in dynamic executable.
2798 3. .rela.iplt section in static executable. */
2799 if (bfd_link_pic (info))
2800 sreloc = htab->elf.irelifunc;
2801 else if (htab->elf.splt != NULL)
2802 sreloc = htab->elf.srelgot;
2803 else
2804 sreloc = htab->elf.irelplt;
351f65ca 2805 elf_append_rela (output_bfd, sreloc, &outrel);
710ab287
L
2806
2807 /* If this reloc is against an external symbol, we
2808 do not want to fiddle with the addend. Otherwise,
2809 we need to include the symbol value so that it
2810 becomes an addend for the dynamic reloc. For an
2811 internal symbol, we have updated addend. */
56b8aada 2812 continue;
710ab287 2813 }
17672001 2814 /* FALLTHROUGH */
cbe950e9 2815 case R_X86_64_PC32:
c3320543 2816 case R_X86_64_PC32_BND:
cbe950e9
L
2817 case R_X86_64_PC64:
2818 case R_X86_64_PLT32:
c3320543 2819 case R_X86_64_PLT32_BND:
cbe950e9 2820 goto do_relocation;
cbe950e9
L
2821 }
2822 }
2823
dc1e8a47 2824 skip_ifunc:
aec6b87e 2825 resolved_to_zero = (eh != NULL
c5bce5c6 2826 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
aec6b87e 2827
70256ad8
AJ
2828 /* When generating a shared object, the relocations handled here are
2829 copied into the output file to be resolved at run time. */
2830 switch (r_type)
2831 {
2832 case R_X86_64_GOT32:
7b81dfbb 2833 case R_X86_64_GOT64:
70256ad8
AJ
2834 /* Relocation is to the entry for this symbol in the global
2835 offset table. */
70256ad8 2836 case R_X86_64_GOTPCREL:
56ceb5b5
L
2837 case R_X86_64_GOTPCRELX:
2838 case R_X86_64_REX_GOTPCRELX:
7b81dfbb
AJ
2839 case R_X86_64_GOTPCREL64:
2840 /* Use global offset table entry as symbol value. */
2841 case R_X86_64_GOTPLT64:
de194d85 2842 /* This is obsolete and treated the same as GOT64. */
6de2ae4a 2843 base_got = htab->elf.sgot;
7b81dfbb 2844
6de2ae4a 2845 if (htab->elf.sgot == NULL)
c434dee6 2846 abort ();
053579d7 2847
9e9821dd 2848 relative_reloc = FALSE;
51e0a107 2849 if (h != NULL)
70256ad8 2850 {
c434dee6 2851 off = h->got.offset;
7b81dfbb 2852 if (h->needs_plt
eed180f8 2853 && h->plt.offset != (bfd_vma)-1
7b81dfbb
AJ
2854 && off == (bfd_vma)-1)
2855 {
2856 /* We can't use h->got.offset here to save
2857 state, or even just remember the offset, as
2858 finish_dynamic_symbol would use that as offset into
2859 .got. */
38b12349 2860 bfd_vma plt_index = (h->plt.offset / plt_entry_size
765e526c 2861 - htab->plt.has_plt0);
7b81dfbb 2862 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 2863 base_got = htab->elf.sgotplt;
7b81dfbb
AJ
2864 }
2865
2eba97c2 2866 if (RESOLVED_LOCALLY_P (info, h, htab))
51e0a107 2867 {
2eba97c2
L
2868 /* We must initialize this entry in the global offset
2869 table. Since the offset must always be a multiple
2870 of 8, we use the least significant bit to record
2871 whether we have initialized it already.
51e0a107
JH
2872
2873 When doing a dynamic link, we create a .rela.got
407443a3
AJ
2874 relocation entry to initialize the value. This is
2875 done in the finish_dynamic_symbol routine. */
51e0a107
JH
2876 if ((off & 1) != 0)
2877 off &= ~1;
2878 else
2879 {
2880 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
2881 base_got->contents + off);
2882 /* Note that this is harmless for the GOTPLT64 case,
eed180f8 2883 as -1 | 1 still is -1. */
51e0a107 2884 h->got.offset |= 1;
9e9821dd 2885
f70656b2 2886 if (GENERATE_RELATIVE_RELOC_P (info, h))
9e9821dd
L
2887 {
2888 /* If this symbol isn't dynamic in PIC,
2889 generate R_X86_64_RELATIVE here. */
2890 eh->no_finish_dynamic_symbol = 1;
2891 relative_reloc = TRUE;
2892 }
51e0a107
JH
2893 }
2894 }
053579d7 2895 else
b34976b6 2896 unresolved_reloc = FALSE;
70256ad8 2897 }
51e0a107
JH
2898 else
2899 {
c434dee6
AJ
2900 if (local_got_offsets == NULL)
2901 abort ();
51e0a107
JH
2902
2903 off = local_got_offsets[r_symndx];
2904
2905 /* The offset must always be a multiple of 8. We use
407443a3
AJ
2906 the least significant bit to record whether we have
2907 already generated the necessary reloc. */
51e0a107
JH
2908 if ((off & 1) != 0)
2909 off &= ~1;
2910 else
2911 {
c434dee6 2912 bfd_put_64 (output_bfd, relocation,
7b81dfbb 2913 base_got->contents + off);
9e9821dd 2914 local_got_offsets[r_symndx] |= 1;
51e0a107 2915
382aae06
L
2916 /* NB: GOTPCREL relocations against local absolute
2917 symbol store relocation value in the GOT slot
2918 without relative relocation. */
2919 if (bfd_link_pic (info)
2920 && !(sym->st_shndx == SHN_ABS
2921 && (r_type == R_X86_64_GOTPCREL
2922 || r_type == R_X86_64_GOTPCRELX
2923 || r_type == R_X86_64_REX_GOTPCRELX)))
9e9821dd 2924 relative_reloc = TRUE;
51e0a107 2925 }
51e0a107 2926 }
6a2bda3f 2927
9e9821dd
L
2928 if (relative_reloc)
2929 {
2930 asection *s;
2931 Elf_Internal_Rela outrel;
2932
2933 /* We need to generate a R_X86_64_RELATIVE reloc
2934 for the dynamic linker. */
2935 s = htab->elf.srelgot;
2936 if (s == NULL)
2937 abort ();
2938
2939 outrel.r_offset = (base_got->output_section->vma
2940 + base_got->output_offset
2941 + off);
2942 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
2943 outrel.r_addend = relocation;
2944 elf_append_rela (output_bfd, s, &outrel);
2945 }
2946
c434dee6
AJ
2947 if (off >= (bfd_vma) -2)
2948 abort ();
2949
7b81dfbb
AJ
2950 relocation = base_got->output_section->vma
2951 + base_got->output_offset + off;
56ceb5b5
L
2952 if (r_type != R_X86_64_GOTPCREL
2953 && r_type != R_X86_64_GOTPCRELX
2954 && r_type != R_X86_64_REX_GOTPCRELX
2955 && r_type != R_X86_64_GOTPCREL64)
6de2ae4a
L
2956 relocation -= htab->elf.sgotplt->output_section->vma
2957 - htab->elf.sgotplt->output_offset;
c434dee6 2958
70256ad8
AJ
2959 break;
2960
d6ab8113
JB
2961 case R_X86_64_GOTOFF64:
2962 /* Relocation is relative to the start of the global offset
2963 table. */
2964
3d949995
L
2965 /* Check to make sure it isn't a protected function or data
2966 symbol for shared library since it may not be local when
e3c0e327
L
2967 used as function address or with copy relocation. We also
2968 need to make sure that a symbol is referenced locally. */
0e1862bb 2969 if (bfd_link_pic (info) && h)
d6ab8113 2970 {
e3c0e327
L
2971 if (!h->def_regular)
2972 {
2973 const char *v;
2974
2975 switch (ELF_ST_VISIBILITY (h->other))
2976 {
2977 case STV_HIDDEN:
2978 v = _("hidden symbol");
2979 break;
2980 case STV_INTERNAL:
2981 v = _("internal symbol");
2982 break;
2983 case STV_PROTECTED:
2984 v = _("protected symbol");
2985 break;
2986 default:
2987 v = _("symbol");
2988 break;
2989 }
2990
4eca0228 2991 _bfd_error_handler
695344c0 2992 /* xgettext:c-format */
871b3ab2 2993 (_("%pB: relocation R_X86_64_GOTOFF64 against undefined %s"
63a5468a 2994 " `%s' can not be used when making a shared object"),
e3c0e327
L
2995 input_bfd, v, h->root.root.string);
2996 bfd_set_error (bfd_error_bad_value);
2997 return FALSE;
2998 }
0e1862bb 2999 else if (!bfd_link_executable (info)
6999821f 3000 && !SYMBOL_REFERENCES_LOCAL_P (info, h)
e3c0e327
L
3001 && (h->type == STT_FUNC
3002 || h->type == STT_OBJECT)
3003 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3004 {
4eca0228 3005 _bfd_error_handler
da0d12d2 3006 /* xgettext:c-format */
871b3ab2 3007 (_("%pB: relocation R_X86_64_GOTOFF64 against protected %s"
63a5468a 3008 " `%s' can not be used when making a shared object"),
e3c0e327
L
3009 input_bfd,
3010 h->type == STT_FUNC ? "function" : "data",
3011 h->root.root.string);
3012 bfd_set_error (bfd_error_bad_value);
da0d12d2 3013 return FALSE;
e3c0e327 3014 }
d6ab8113
JB
3015 }
3016
3017 /* Note that sgot is not involved in this
3018 calculation. We always want the start of .got.plt. If we
3019 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3020 permitted by the ABI, we might have to change this
3021 calculation. */
6de2ae4a
L
3022 relocation -= htab->elf.sgotplt->output_section->vma
3023 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3024 break;
3025
3026 case R_X86_64_GOTPC32:
7b81dfbb 3027 case R_X86_64_GOTPC64:
d6ab8113 3028 /* Use global offset table as symbol value. */
6de2ae4a
L
3029 relocation = htab->elf.sgotplt->output_section->vma
3030 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3031 unresolved_reloc = FALSE;
3032 break;
7b81dfbb
AJ
3033
3034 case R_X86_64_PLTOFF64:
3035 /* Relocation is PLT entry relative to GOT. For local
3036 symbols it's the symbol itself relative to GOT. */
eed180f8 3037 if (h != NULL
7b81dfbb 3038 /* See PLT32 handling. */
9d734efa
L
3039 && (h->plt.offset != (bfd_vma) -1
3040 || eh->plt_got.offset != (bfd_vma) -1)
6de2ae4a 3041 && htab->elf.splt != NULL)
7b81dfbb 3042 {
9d734efa
L
3043 if (eh->plt_got.offset != (bfd_vma) -1)
3044 {
3045 /* Use the GOT PLT. */
3046 resolved_plt = htab->plt_got;
3047 plt_offset = eh->plt_got.offset;
3048 }
f2c29a16 3049 else if (htab->plt_second != NULL)
0ff2b86e 3050 {
f2c29a16
L
3051 resolved_plt = htab->plt_second;
3052 plt_offset = eh->plt_second.offset;
0ff2b86e
L
3053 }
3054 else
3055 {
3056 resolved_plt = htab->elf.splt;
3057 plt_offset = h->plt.offset;
3058 }
3059
3060 relocation = (resolved_plt->output_section->vma
3061 + resolved_plt->output_offset
3062 + plt_offset);
7b81dfbb
AJ
3063 unresolved_reloc = FALSE;
3064 }
3065
6de2ae4a
L
3066 relocation -= htab->elf.sgotplt->output_section->vma
3067 + htab->elf.sgotplt->output_offset;
7b81dfbb 3068 break;
d6ab8113 3069
70256ad8 3070 case R_X86_64_PLT32:
c3320543 3071 case R_X86_64_PLT32_BND:
70256ad8
AJ
3072 /* Relocation is to the entry for this symbol in the
3073 procedure linkage table. */
3074
3075 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 3076 without using the procedure linkage table. */
70256ad8
AJ
3077 if (h == NULL)
3078 break;
3079
dd7e64d4
L
3080 if ((h->plt.offset == (bfd_vma) -1
3081 && eh->plt_got.offset == (bfd_vma) -1)
6de2ae4a 3082 || htab->elf.splt == NULL)
70256ad8
AJ
3083 {
3084 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
3085 happens when statically linking PIC code, or when
3086 using -Bsymbolic. */
70256ad8
AJ
3087 break;
3088 }
3089
dc1e8a47 3090 use_plt:
dd7e64d4 3091 if (h->plt.offset != (bfd_vma) -1)
0ff2b86e 3092 {
f2c29a16 3093 if (htab->plt_second != NULL)
dd7e64d4 3094 {
f2c29a16
L
3095 resolved_plt = htab->plt_second;
3096 plt_offset = eh->plt_second.offset;
dd7e64d4
L
3097 }
3098 else
3099 {
3100 resolved_plt = htab->elf.splt;
3101 plt_offset = h->plt.offset;
3102 }
0ff2b86e
L
3103 }
3104 else
3105 {
dd7e64d4
L
3106 /* Use the GOT PLT. */
3107 resolved_plt = htab->plt_got;
3108 plt_offset = eh->plt_got.offset;
0ff2b86e
L
3109 }
3110
3111 relocation = (resolved_plt->output_section->vma
3112 + resolved_plt->output_offset
3113 + plt_offset);
b34976b6 3114 unresolved_reloc = FALSE;
70256ad8
AJ
3115 break;
3116
1788fc08
L
3117 case R_X86_64_SIZE32:
3118 case R_X86_64_SIZE64:
1788fc08
L
3119 /* Set to symbol size. */
3120 relocation = st_size;
3121 goto direct;
3122
fd8ab9e5
AJ
3123 case R_X86_64_PC8:
3124 case R_X86_64_PC16:
3125 case R_X86_64_PC32:
c3320543 3126 case R_X86_64_PC32_BND:
81e8046d
L
3127 /* Don't complain about -fPIC if the symbol is undefined when
3128 building executable unless it is unresolved weak symbol,
3129 references a dynamic definition in PIE or -z nocopyreloc
3130 is used. */
3131 no_copyreloc_p
3132 = (info->nocopyreloc
3133 || (h != NULL
3134 && !h->root.linker_def
3135 && !h->root.ldscript_def
3136 && eh->def_protected
3137 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)));
3138
3139 if ((input_section->flags & SEC_ALLOC) != 0
3140 && (input_section->flags & SEC_READONLY) != 0
3141 && h != NULL
3142 && ((bfd_link_executable (info)
3143 && ((h->root.type == bfd_link_hash_undefweak
3144 && (eh == NULL
3145 || !UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
3146 eh)))
3147 || (bfd_link_pie (info)
3148 && !SYMBOL_DEFINED_NON_SHARED_P (h)
3149 && h->def_dynamic)
3150 || (no_copyreloc_p
3151 && h->def_dynamic
3152 && !(h->root.u.def.section->flags & SEC_CODE))))
3153 || bfd_link_dll (info)))
3154 {
3155 bfd_boolean fail = FALSE;
3156 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3157 {
3158 /* Symbol is referenced locally. Make sure it is
3159 defined locally. */
3160 fail = !SYMBOL_DEFINED_NON_SHARED_P (h);
3161 }
3162 else if (bfd_link_pie (info))
3163 {
3164 /* We can only use PC-relative relocations in PIE
3165 from non-code sections. */
3166 if (h->type == STT_FUNC
3167 && (sec->flags & SEC_CODE) != 0)
3168 fail = TRUE;
3169 }
3170 else if (no_copyreloc_p || bfd_link_dll (info))
3171 {
3172 /* Symbol doesn't need copy reloc and isn't
3173 referenced locally. Don't allow PC-relative
3174 relocations against default and protected
3175 symbols since address of protected function
3176 and location of protected data may not be in
3177 the shared object. */
3178 fail = (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3179 || ELF_ST_VISIBILITY (h->other) == STV_PROTECTED);
3180 }
3181
3182 if (fail)
3183 return elf_x86_64_need_pic (info, input_bfd, input_section,
3184 h, NULL, NULL, howto);
3185 }
451875b4
L
3186 /* Since x86-64 has PC-relative PLT, we can use PLT in PIE
3187 as function address. */
81e8046d
L
3188 else if (h != NULL
3189 && (input_section->flags & SEC_CODE) == 0
3190 && bfd_link_pie (info)
3191 && h->type == STT_FUNC
3192 && !h->def_regular
3193 && h->def_dynamic)
451875b4 3194 goto use_plt;
6610a52d
L
3195 /* Fall through. */
3196
70256ad8
AJ
3197 case R_X86_64_8:
3198 case R_X86_64_16:
3199 case R_X86_64_32:
d6ab8113 3200 case R_X86_64_PC64:
6b3db546 3201 case R_X86_64_64:
80643fbc 3202 /* FIXME: The ABI says the linker should make sure the value is
407443a3 3203 the same when it's zeroextended to 64 bit. */
c434dee6 3204
dc1e8a47 3205 direct:
b1e24c02 3206 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
3207 break;
3208
aebcc8ff
L
3209 need_copy_reloc_in_pie = (bfd_link_pie (info)
3210 && h != NULL
3211 && (h->needs_copy
3212 || eh->needs_copy
3213 || (h->root.type
3214 == bfd_link_hash_undefined))
3215 && (X86_PCREL_TYPE_P (r_type)
3216 || X86_SIZE_TYPE_P (r_type)));
3217
382aae06 3218 if (GENERATE_DYNAMIC_RELOCATION_P (info, eh, r_type, sec,
aebcc8ff
L
3219 need_copy_reloc_in_pie,
3220 resolved_to_zero, FALSE))
70256ad8
AJ
3221 {
3222 Elf_Internal_Rela outrel;
b34976b6 3223 bfd_boolean skip, relocate;
c434dee6 3224 asection *sreloc;
70256ad8
AJ
3225
3226 /* When generating a shared object, these relocations
3227 are copied into the output file to be resolved at run
407443a3 3228 time. */
b34976b6
AM
3229 skip = FALSE;
3230 relocate = FALSE;
70256ad8 3231
c629eae0
JJ
3232 outrel.r_offset =
3233 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 3234 rel->r_offset);
c629eae0 3235 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3236 skip = TRUE;
0fb19cbc 3237 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3238 skip = TRUE, relocate = TRUE;
70256ad8
AJ
3239
3240 outrel.r_offset += (input_section->output_section->vma
3241 + input_section->output_offset);
3242
3243 if (skip)
0bb2d96a 3244 memset (&outrel, 0, sizeof outrel);
c434dee6 3245
e74399c4 3246 else if (COPY_INPUT_RELOC_P (info, h, r_type))
70256ad8 3247 {
351f65ca 3248 outrel.r_info = htab->r_info (h->dynindx, r_type);
c434dee6 3249 outrel.r_addend = rel->r_addend;
70256ad8
AJ
3250 }
3251 else
3252 {
4c10bbaa
L
3253 /* This symbol is local, or marked to become local.
3254 When relocation overflow check is disabled, we
3255 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
3256 if (r_type == htab->pointer_r_type
3257 || (r_type == R_X86_64_32
5b9c07b2 3258 && htab->params->no_reloc_overflow_check))
607c0e09 3259 {
b34976b6 3260 relocate = TRUE;
351f65ca 3261 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
607c0e09
AS
3262 outrel.r_addend = relocation + rel->r_addend;
3263 }
64d25c44
L
3264 else if (r_type == R_X86_64_64
3265 && !ABI_64_P (output_bfd))
3266 {
3267 relocate = TRUE;
3268 outrel.r_info = htab->r_info (0,
3269 R_X86_64_RELATIVE64);
3270 outrel.r_addend = relocation + rel->r_addend;
8cf0d2dd
L
3271 /* Check addend overflow. */
3272 if ((outrel.r_addend & 0x80000000)
3273 != (rel->r_addend & 0x80000000))
3274 {
3275 const char *name;
268a8d3a 3276 int addend = rel->r_addend;
8cf0d2dd
L
3277 if (h && h->root.root.string)
3278 name = h->root.root.string;
3279 else
3280 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3281 sym, NULL);
d42c267e
AM
3282 _bfd_error_handler
3283 /* xgettext:c-format */
871b3ab2 3284 (_("%pB: addend %s%#x in relocation %s against "
2dcf00ce
AM
3285 "symbol `%s' at %#" PRIx64
3286 " in section `%pA' is out of range"),
d42c267e 3287 input_bfd, addend < 0 ? "-" : "", addend,
2dcf00ce
AM
3288 howto->name, name, (uint64_t) rel->r_offset,
3289 input_section);
8cf0d2dd
L
3290 bfd_set_error (bfd_error_bad_value);
3291 return FALSE;
3292 }
64d25c44 3293 }
607c0e09
AS
3294 else
3295 {
3296 long sindx;
3297
8517fae7 3298 if (bfd_is_abs_section (sec))
607c0e09
AS
3299 sindx = 0;
3300 else if (sec == NULL || sec->owner == NULL)
3301 {
3302 bfd_set_error (bfd_error_bad_value);
b34976b6 3303 return FALSE;
607c0e09
AS
3304 }
3305 else
3306 {
3307 asection *osec;
3308
74541ad4
AM
3309 /* We are turning this relocation into one
3310 against a section symbol. It would be
3311 proper to subtract the symbol's value,
3312 osec->vma, from the emitted reloc addend,
3313 but ld.so expects buggy relocs. */
607c0e09
AS
3314 osec = sec->output_section;
3315 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
3316 if (sindx == 0)
3317 {
3318 asection *oi = htab->elf.text_index_section;
3319 sindx = elf_section_data (oi)->dynindx;
3320 }
3321 BFD_ASSERT (sindx != 0);
607c0e09
AS
3322 }
3323
351f65ca 3324 outrel.r_info = htab->r_info (sindx, r_type);
607c0e09
AS
3325 outrel.r_addend = relocation + rel->r_addend;
3326 }
70256ad8
AJ
3327 }
3328
cbe950e9 3329 sreloc = elf_section_data (input_section)->sreloc;
d8045f23 3330
62d78908
L
3331 if (sreloc == NULL || sreloc->contents == NULL)
3332 {
3333 r = bfd_reloc_notsupported;
3334 goto check_relocation_error;
3335 }
c434dee6 3336
351f65ca 3337 elf_append_rela (output_bfd, sreloc, &outrel);
70256ad8
AJ
3338
3339 /* If this reloc is against an external symbol, we do
3340 not want to fiddle with the addend. Otherwise, we
3341 need to include the symbol value so that it becomes
3342 an addend for the dynamic reloc. */
0f88be7a 3343 if (! relocate)
70256ad8
AJ
3344 continue;
3345 }
3346
3347 break;
3348
bffbf940 3349 case R_X86_64_TLSGD:
67a4f2b7
AO
3350 case R_X86_64_GOTPC32_TLSDESC:
3351 case R_X86_64_TLSDESC_CALL:
bffbf940 3352 case R_X86_64_GOTTPOFF:
bffbf940
JJ
3353 tls_type = GOT_UNKNOWN;
3354 if (h == NULL && local_got_offsets)
0afcef53 3355 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
bffbf940 3356 else if (h != NULL)
0afcef53 3357 tls_type = elf_x86_hash_entry (h)->tls_type;
142411ca 3358
419414ea 3359 r_type_tls = r_type;
351f65ca
L
3360 if (! elf_x86_64_tls_transition (info, input_bfd,
3361 input_section, contents,
3362 symtab_hdr, sym_hashes,
419414ea 3363 &r_type_tls, tls_type, rel,
bedfd056 3364 relend, h, r_symndx, TRUE))
534a31f6 3365 return FALSE;
bffbf940 3366
419414ea 3367 if (r_type_tls == R_X86_64_TPOFF32)
bffbf940 3368 {
142411ca
L
3369 bfd_vma roff = rel->r_offset;
3370
bffbf940 3371 BFD_ASSERT (! unresolved_reloc);
142411ca 3372
419414ea 3373 if (r_type == R_X86_64_TLSGD)
bffbf940 3374 {
52bc799a 3375 /* GD->LE transition. For 64bit, change
e2cbcd91
L
3376 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3377 .word 0x6666; rex64; call __tls_get_addr@PLT
3378 or
3379 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3380 .byte 0x66; rex64
3381 call *__tls_get_addr@GOTPCREL(%rip)
3382 which may be converted to
3383 addr32 call __tls_get_addr
52bc799a 3384 into:
e2cbcd91
L
3385 movq %fs:0, %rax
3386 leaq foo@tpoff(%rax), %rax
52bc799a 3387 For 32bit, change
e2cbcd91
L
3388 leaq foo@tlsgd(%rip), %rdi
3389 .word 0x6666; rex64; call __tls_get_addr@PLT
3390 or
3391 leaq foo@tlsgd(%rip), %rdi
3392 .byte 0x66; rex64
3393 call *__tls_get_addr@GOTPCREL(%rip)
3394 which may be converted to
3395 addr32 call __tls_get_addr
52bc799a 3396 into:
e2cbcd91
L
3397 movl %fs:0, %eax
3398 leaq foo@tpoff(%rax), %rax
5c98a14e 3399 For largepic, change:
e2cbcd91
L
3400 leaq foo@tlsgd(%rip), %rdi
3401 movabsq $__tls_get_addr@pltoff, %rax
3402 addq %r15, %rax
3403 call *%rax
5c98a14e 3404 into:
e2cbcd91
L
3405 movq %fs:0, %rax
3406 leaq foo@tpoff(%rax), %rax
3407 nopw 0x0(%rax,%rax,1) */
5c98a14e 3408 int largepic = 0;
e2cbcd91 3409 if (ABI_64_P (output_bfd))
5c98a14e 3410 {
e2cbcd91
L
3411 if (contents[roff + 5] == 0xb8)
3412 {
418d4036
L
3413 if (roff < 3
3414 || (roff - 3 + 22) > input_section->size)
3415 {
dc1e8a47 3416 corrupt_input:
418d4036
L
3417 info->callbacks->einfo
3418 (_("%F%P: corrupt input: %pB\n"),
3419 input_bfd);
3420 return FALSE;
3421 }
e2cbcd91
L
3422 memcpy (contents + roff - 3,
3423 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
3424 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3425 largepic = 1;
3426 }
3427 else
418d4036
L
3428 {
3429 if (roff < 4
3430 || (roff - 4 + 16) > input_section->size)
3431 goto corrupt_input;
3432 memcpy (contents + roff - 4,
3433 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3434 16);
3435 }
5c98a14e 3436 }
52bc799a 3437 else
418d4036
L
3438 {
3439 if (roff < 3
3440 || (roff - 3 + 15) > input_section->size)
3441 goto corrupt_input;
3442 memcpy (contents + roff - 3,
3443 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3444 15);
3445 }
eb4ff4d6 3446 bfd_put_32 (output_bfd,
351f65ca 3447 elf_x86_64_tpoff (info, relocation),
5c98a14e 3448 contents + roff + 8 + largepic);
e2cbcd91
L
3449 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
3450 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
bffbf940 3451 rel++;
60f2e42e 3452 wrel++;
bffbf940
JJ
3453 continue;
3454 }
419414ea 3455 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
67a4f2b7
AO
3456 {
3457 /* GDesc -> LE transition.
3458 It's originally something like:
14470f07
L
3459 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3460 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
67a4f2b7
AO
3461
3462 Change it to:
14470f07
L
3463 movq $x@tpoff, %rax <--- LP64 mode.
3464 rex movl $x@tpoff, %eax <--- X32 mode.
3465 */
67a4f2b7 3466
c9736ba0 3467 unsigned int val, type;
67a4f2b7 3468
418d4036
L
3469 if (roff < 3)
3470 goto corrupt_input;
67a4f2b7 3471 type = bfd_get_8 (input_bfd, contents + roff - 3);
67a4f2b7 3472 val = bfd_get_8 (input_bfd, contents + roff - 1);
14470f07
L
3473 bfd_put_8 (output_bfd,
3474 (type & 0x48) | ((type >> 2) & 1),
67a4f2b7
AO
3475 contents + roff - 3);
3476 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3477 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3478 contents + roff - 1);
eb4ff4d6 3479 bfd_put_32 (output_bfd,
351f65ca 3480 elf_x86_64_tpoff (info, relocation),
67a4f2b7
AO
3481 contents + roff);
3482 continue;
3483 }
419414ea 3484 else if (r_type == R_X86_64_TLSDESC_CALL)
67a4f2b7
AO
3485 {
3486 /* GDesc -> LE transition.
3487 It's originally:
14470f07
L
3488 call *(%rax) <--- LP64 mode.
3489 call *(%eax) <--- X32 mode.
67a4f2b7 3490 Turn it into:
14470f07
L
3491 xchg %ax,%ax <-- LP64 mode.
3492 nopl (%rax) <-- X32 mode.
3493 */
3494 unsigned int prefix = 0;
3495 if (!ABI_64_P (input_bfd))
3496 {
3497 /* Check for call *x@tlsdesc(%eax). */
3498 if (contents[roff] == 0x67)
3499 prefix = 1;
3500 }
3501 if (prefix)
3502 {
3503 bfd_put_8 (output_bfd, 0x0f, contents + roff);
3504 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
3505 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
3506 }
3507 else
3508 {
3509 bfd_put_8 (output_bfd, 0x66, contents + roff);
3510 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3511 }
67a4f2b7
AO
3512 continue;
3513 }
419414ea 3514 else if (r_type == R_X86_64_GOTTPOFF)
bffbf940 3515 {
bffbf940 3516 /* IE->LE transition:
cf61b747 3517 For 64bit, originally it can be one of:
bffbf940
JJ
3518 movq foo@gottpoff(%rip), %reg
3519 addq foo@gottpoff(%rip), %reg
3520 We change it into:
3521 movq $foo, %reg
3522 leaq foo(%reg), %reg
cf61b747
L
3523 addq $foo, %reg.
3524 For 32bit, originally it can be one of:
3525 movq foo@gottpoff(%rip), %reg
3526 addl foo@gottpoff(%rip), %reg
3527 We change it into:
3528 movq $foo, %reg
3529 leal foo(%reg), %reg
3530 addl $foo, %reg. */
142411ca
L
3531
3532 unsigned int val, type, reg;
3533
cf61b747
L
3534 if (roff >= 3)
3535 val = bfd_get_8 (input_bfd, contents + roff - 3);
3536 else
418d4036
L
3537 {
3538 if (roff < 2)
3539 goto corrupt_input;
3540 val = 0;
3541 }
142411ca
L
3542 type = bfd_get_8 (input_bfd, contents + roff - 2);
3543 reg = bfd_get_8 (input_bfd, contents + roff - 1);
bffbf940 3544 reg >>= 3;
bffbf940
JJ
3545 if (type == 0x8b)
3546 {
3547 /* movq */
3548 if (val == 0x4c)
418d4036
L
3549 {
3550 if (roff < 3)
3551 goto corrupt_input;
3552 bfd_put_8 (output_bfd, 0x49,
3553 contents + roff - 3);
3554 }
4a4c5f25 3555 else if (!ABI_64_P (output_bfd) && val == 0x44)
418d4036
L
3556 {
3557 if (roff < 3)
3558 goto corrupt_input;
3559 bfd_put_8 (output_bfd, 0x41,
3560 contents + roff - 3);
3561 }
bffbf940 3562 bfd_put_8 (output_bfd, 0xc7,
142411ca 3563 contents + roff - 2);
bffbf940 3564 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 3565 contents + roff - 1);
bffbf940
JJ
3566 }
3567 else if (reg == 4)
3568 {
cf61b747
L
3569 /* addq/addl -> addq/addl - addressing with %rsp/%r12
3570 is special */
bffbf940 3571 if (val == 0x4c)
418d4036
L
3572 {
3573 if (roff < 3)
3574 goto corrupt_input;
3575 bfd_put_8 (output_bfd, 0x49,
3576 contents + roff - 3);
3577 }
4a4c5f25 3578 else if (!ABI_64_P (output_bfd) && val == 0x44)
418d4036
L
3579 {
3580 if (roff < 3)
3581 goto corrupt_input;
3582 bfd_put_8 (output_bfd, 0x41,
3583 contents + roff - 3);
3584 }
bffbf940 3585 bfd_put_8 (output_bfd, 0x81,
142411ca 3586 contents + roff - 2);
bffbf940 3587 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 3588 contents + roff - 1);
bffbf940
JJ
3589 }
3590 else
3591 {
cf61b747 3592 /* addq/addl -> leaq/leal */
bffbf940 3593 if (val == 0x4c)
418d4036
L
3594 {
3595 if (roff < 3)
3596 goto corrupt_input;
3597 bfd_put_8 (output_bfd, 0x4d,
3598 contents + roff - 3);
3599 }
4a4c5f25 3600 else if (!ABI_64_P (output_bfd) && val == 0x44)
418d4036
L
3601 {
3602 if (roff < 3)
3603 goto corrupt_input;
3604 bfd_put_8 (output_bfd, 0x45,
3605 contents + roff - 3);
3606 }
bffbf940 3607 bfd_put_8 (output_bfd, 0x8d,
142411ca 3608 contents + roff - 2);
bffbf940 3609 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
142411ca 3610 contents + roff - 1);
bffbf940 3611 }
eb4ff4d6 3612 bfd_put_32 (output_bfd,
351f65ca 3613 elf_x86_64_tpoff (info, relocation),
142411ca 3614 contents + roff);
bffbf940
JJ
3615 continue;
3616 }
142411ca
L
3617 else
3618 BFD_ASSERT (FALSE);
bffbf940
JJ
3619 }
3620
6de2ae4a 3621 if (htab->elf.sgot == NULL)
bffbf940
JJ
3622 abort ();
3623
3624 if (h != NULL)
67a4f2b7
AO
3625 {
3626 off = h->got.offset;
0afcef53 3627 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
67a4f2b7 3628 }
bffbf940
JJ
3629 else
3630 {
3631 if (local_got_offsets == NULL)
3632 abort ();
3633
3634 off = local_got_offsets[r_symndx];
67a4f2b7 3635 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
3636 }
3637
3638 if ((off & 1) != 0)
3639 off &= ~1;
26e41594 3640 else
bffbf940
JJ
3641 {
3642 Elf_Internal_Rela outrel;
bffbf940 3643 int dr_type, indx;
67a4f2b7 3644 asection *sreloc;
bffbf940 3645
6de2ae4a 3646 if (htab->elf.srelgot == NULL)
bffbf940
JJ
3647 abort ();
3648
67a4f2b7
AO
3649 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3650
3651 if (GOT_TLS_GDESC_P (tls_type))
3652 {
351f65ca 3653 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
67a4f2b7 3654 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
6de2ae4a
L
3655 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3656 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3657 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3658 + offplt
3659 + htab->sgotplt_jump_table_size);
6de2ae4a 3660 sreloc = htab->elf.srelplt;
67a4f2b7 3661 if (indx == 0)
0afcef53 3662 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
67a4f2b7
AO
3663 else
3664 outrel.r_addend = 0;
351f65ca 3665 elf_append_rela (output_bfd, sreloc, &outrel);
67a4f2b7
AO
3666 }
3667
6de2ae4a 3668 sreloc = htab->elf.srelgot;
67a4f2b7 3669
6de2ae4a
L
3670 outrel.r_offset = (htab->elf.sgot->output_section->vma
3671 + htab->elf.sgot->output_offset + off);
bffbf940 3672
67a4f2b7 3673 if (GOT_TLS_GD_P (tls_type))
bffbf940 3674 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
3675 else if (GOT_TLS_GDESC_P (tls_type))
3676 goto dr_done;
bffbf940
JJ
3677 else
3678 dr_type = R_X86_64_TPOFF64;
3679
6de2ae4a 3680 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
bffbf940 3681 outrel.r_addend = 0;
67a4f2b7
AO
3682 if ((dr_type == R_X86_64_TPOFF64
3683 || dr_type == R_X86_64_TLSDESC) && indx == 0)
0afcef53 3684 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
351f65ca 3685 outrel.r_info = htab->r_info (indx, dr_type);
bffbf940 3686
351f65ca 3687 elf_append_rela (output_bfd, sreloc, &outrel);
bffbf940 3688
67a4f2b7 3689 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
3690 {
3691 if (indx == 0)
3692 {
d40d037c 3693 BFD_ASSERT (! unresolved_reloc);
bffbf940 3694 bfd_put_64 (output_bfd,
0afcef53 3695 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3696 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
bffbf940
JJ
3697 }
3698 else
3699 {
3700 bfd_put_64 (output_bfd, 0,
6de2ae4a 3701 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 3702 outrel.r_info = htab->r_info (indx,
bffbf940
JJ
3703 R_X86_64_DTPOFF64);
3704 outrel.r_offset += GOT_ENTRY_SIZE;
351f65ca 3705 elf_append_rela (output_bfd, sreloc,
464d3bd4 3706 &outrel);
bffbf940
JJ
3707 }
3708 }
3709
67a4f2b7 3710 dr_done:
bffbf940
JJ
3711 if (h != NULL)
3712 h->got.offset |= 1;
3713 else
3714 local_got_offsets[r_symndx] |= 1;
3715 }
3716
67a4f2b7
AO
3717 if (off >= (bfd_vma) -2
3718 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940 3719 abort ();
419414ea 3720 if (r_type_tls == r_type)
bffbf940 3721 {
67a4f2b7
AO
3722 if (r_type == R_X86_64_GOTPC32_TLSDESC
3723 || r_type == R_X86_64_TLSDESC_CALL)
6de2ae4a
L
3724 relocation = htab->elf.sgotplt->output_section->vma
3725 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3726 + offplt + htab->sgotplt_jump_table_size;
3727 else
6de2ae4a
L
3728 relocation = htab->elf.sgot->output_section->vma
3729 + htab->elf.sgot->output_offset + off;
b34976b6 3730 unresolved_reloc = FALSE;
bffbf940 3731 }
142411ca 3732 else
67a4f2b7 3733 {
142411ca 3734 bfd_vma roff = rel->r_offset;
67a4f2b7 3735
419414ea 3736 if (r_type == R_X86_64_TLSGD)
142411ca 3737 {
52bc799a 3738 /* GD->IE transition. For 64bit, change
e2cbcd91
L
3739 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3740 .word 0x6666; rex64; call __tls_get_addr@PLT
3741 or
3742 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3743 .byte 0x66; rex64
3744 call *__tls_get_addr@GOTPCREL(%rip
3745 which may be converted to
3746 addr32 call __tls_get_addr
52bc799a 3747 into:
e2cbcd91
L
3748 movq %fs:0, %rax
3749 addq foo@gottpoff(%rip), %rax
52bc799a 3750 For 32bit, change
e2cbcd91
L
3751 leaq foo@tlsgd(%rip), %rdi
3752 .word 0x6666; rex64; call __tls_get_addr@PLT
3753 or
3754 leaq foo@tlsgd(%rip), %rdi
3755 .byte 0x66; rex64;
3756 call *__tls_get_addr@GOTPCREL(%rip)
3757 which may be converted to
3758 addr32 call __tls_get_addr
52bc799a 3759 into:
e2cbcd91
L
3760 movl %fs:0, %eax
3761 addq foo@gottpoff(%rip), %rax
5c98a14e 3762 For largepic, change:
e2cbcd91
L
3763 leaq foo@tlsgd(%rip), %rdi
3764 movabsq $__tls_get_addr@pltoff, %rax
3765 addq %r15, %rax
3766 call *%rax
5c98a14e 3767 into:
e2cbcd91
L
3768 movq %fs:0, %rax
3769 addq foo@gottpoff(%rax), %rax
3770 nopw 0x0(%rax,%rax,1) */
5c98a14e 3771 int largepic = 0;
e2cbcd91 3772 if (ABI_64_P (output_bfd))
5c98a14e 3773 {
e2cbcd91
L
3774 if (contents[roff + 5] == 0xb8)
3775 {
418d4036
L
3776 if (roff < 3
3777 || (roff - 3 + 22) > input_section->size)
3778 goto corrupt_input;
e2cbcd91
L
3779 memcpy (contents + roff - 3,
3780 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
3781 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3782 largepic = 1;
3783 }
3784 else
418d4036
L
3785 {
3786 if (roff < 4
3787 || (roff - 4 + 16) > input_section->size)
3788 goto corrupt_input;
3789 memcpy (contents + roff - 4,
3790 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3791 16);
3792 }
5c98a14e 3793 }
52bc799a 3794 else
418d4036
L
3795 {
3796 if (roff < 3
3797 || (roff - 3 + 15) > input_section->size)
3798 goto corrupt_input;
3799 memcpy (contents + roff - 3,
3800 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3801 15);
3802 }
142411ca 3803
6de2ae4a
L
3804 relocation = (htab->elf.sgot->output_section->vma
3805 + htab->elf.sgot->output_offset + off
142411ca 3806 - roff
5c98a14e 3807 - largepic
142411ca
L
3808 - input_section->output_section->vma
3809 - input_section->output_offset
3810 - 12);
3811 bfd_put_32 (output_bfd, relocation,
5c98a14e
JJ
3812 contents + roff + 8 + largepic);
3813 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
142411ca 3814 rel++;
60f2e42e 3815 wrel++;
142411ca
L
3816 continue;
3817 }
419414ea 3818 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
142411ca
L
3819 {
3820 /* GDesc -> IE transition.
3821 It's originally something like:
14470f07
L
3822 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3823 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
67a4f2b7 3824
142411ca 3825 Change it to:
14470f07
L
3826 # before xchg %ax,%ax in LP64 mode.
3827 movq x@gottpoff(%rip), %rax
3828 # before nopl (%rax) in X32 mode.
3829 rex movl x@gottpoff(%rip), %eax
3830 */
67a4f2b7 3831
142411ca 3832 /* Now modify the instruction as appropriate. To
14470f07 3833 turn a lea into a mov in the form we use it, it
142411ca
L
3834 suffices to change the second byte from 0x8d to
3835 0x8b. */
418d4036
L
3836 if (roff < 2)
3837 goto corrupt_input;
142411ca
L
3838 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3839
3840 bfd_put_32 (output_bfd,
6de2ae4a
L
3841 htab->elf.sgot->output_section->vma
3842 + htab->elf.sgot->output_offset + off
142411ca
L
3843 - rel->r_offset
3844 - input_section->output_section->vma
3845 - input_section->output_offset
3846 - 4,
3847 contents + roff);
3848 continue;
3849 }
419414ea 3850 else if (r_type == R_X86_64_TLSDESC_CALL)
142411ca
L
3851 {
3852 /* GDesc -> IE transition.
3853 It's originally:
14470f07
L
3854 call *(%rax) <--- LP64 mode.
3855 call *(%eax) <--- X32 mode.
142411ca
L
3856
3857 Change it to:
14470f07
L
3858 xchg %ax, %ax <-- LP64 mode.
3859 nopl (%rax) <-- X32 mode.
3860 */
142411ca 3861
14470f07
L
3862 unsigned int prefix = 0;
3863 if (!ABI_64_P (input_bfd))
3864 {
3865 /* Check for call *x@tlsdesc(%eax). */
3866 if (contents[roff] == 0x67)
3867 prefix = 1;
3868 }
3869 if (prefix)
3870 {
3871 bfd_put_8 (output_bfd, 0x0f, contents + roff);
3872 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
3873 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
3874 }
3875 else
3876 {
3877 bfd_put_8 (output_bfd, 0x66, contents + roff);
3878 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3879 }
142411ca
L
3880 continue;
3881 }
3882 else
3883 BFD_ASSERT (FALSE);
67a4f2b7 3884 }
bffbf940
JJ
3885 break;
3886
3887 case R_X86_64_TLSLD:
351f65ca
L
3888 if (! elf_x86_64_tls_transition (info, input_bfd,
3889 input_section, contents,
3890 symtab_hdr, sym_hashes,
bedfd056
L
3891 &r_type, GOT_UNKNOWN, rel,
3892 relend, h, r_symndx, TRUE))
142411ca 3893 return FALSE;
a3fadc9a 3894
142411ca
L
3895 if (r_type != R_X86_64_TLSLD)
3896 {
bffbf940 3897 /* LD->LE transition:
e2cbcd91
L
3898 leaq foo@tlsld(%rip), %rdi
3899 call __tls_get_addr@PLT
3900 For 64bit, we change it into:
3901 .word 0x6666; .byte 0x66; movq %fs:0, %rax
3902 For 32bit, we change it into:
3903 nopl 0x0(%rax); movl %fs:0, %eax
3904 Or
3905 leaq foo@tlsld(%rip), %rdi;
3906 call *__tls_get_addr@GOTPCREL(%rip)
3907 which may be converted to
3908 addr32 call __tls_get_addr
52bc799a 3909 For 64bit, we change it into:
e2cbcd91 3910 .word 0x6666; .word 0x6666; movq %fs:0, %rax
52bc799a 3911 For 32bit, we change it into:
e2cbcd91 3912 nopw 0x0(%rax); movl %fs:0, %eax
5c98a14e 3913 For largepic, change:
e2cbcd91
L
3914 leaq foo@tlsgd(%rip), %rdi
3915 movabsq $__tls_get_addr@pltoff, %rax
3916 addq %rbx, %rax
3917 call *%rax
3918 into
3919 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
3920 movq %fs:0, %eax */
142411ca
L
3921
3922 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
e2cbcd91
L
3923 if (ABI_64_P (output_bfd))
3924 {
418d4036
L
3925 if ((rel->r_offset + 5) >= input_section->size)
3926 goto corrupt_input;
e2cbcd91 3927 if (contents[rel->r_offset + 5] == 0xb8)
418d4036
L
3928 {
3929 if (rel->r_offset < 3
3930 || (rel->r_offset - 3 + 22) > input_section->size)
3931 goto corrupt_input;
3932 memcpy (contents + rel->r_offset - 3,
3933 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
3934 "\x64\x48\x8b\x04\x25\0\0\0", 22);
3935 }
e2cbcd91
L
3936 else if (contents[rel->r_offset + 4] == 0xff
3937 || contents[rel->r_offset + 4] == 0x67)
418d4036
L
3938 {
3939 if (rel->r_offset < 3
3940 || (rel->r_offset - 3 + 13) > input_section->size)
3941 goto corrupt_input;
3942 memcpy (contents + rel->r_offset - 3,
3943 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
3944 13);
3945
3946 }
e2cbcd91 3947 else
418d4036
L
3948 {
3949 if (rel->r_offset < 3
3950 || (rel->r_offset - 3 + 12) > input_section->size)
3951 goto corrupt_input;
3952 memcpy (contents + rel->r_offset - 3,
3953 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3954 }
e2cbcd91 3955 }
52bc799a 3956 else
e2cbcd91 3957 {
418d4036
L
3958 if ((rel->r_offset + 4) >= input_section->size)
3959 goto corrupt_input;
e2cbcd91 3960 if (contents[rel->r_offset + 4] == 0xff)
418d4036
L
3961 {
3962 if (rel->r_offset < 3
3963 || (rel->r_offset - 3 + 13) > input_section->size)
3964 goto corrupt_input;
3965 memcpy (contents + rel->r_offset - 3,
3966 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
3967 13);
3968 }
e2cbcd91 3969 else
418d4036
L
3970 {
3971 if (rel->r_offset < 3
3972 || (rel->r_offset - 3 + 12) > input_section->size)
3973 goto corrupt_input;
3974 memcpy (contents + rel->r_offset - 3,
3975 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
3976 }
e2cbcd91
L
3977 }
3978 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
3979 and R_X86_64_PLTOFF64. */
bffbf940 3980 rel++;
60f2e42e 3981 wrel++;
bffbf940
JJ
3982 continue;
3983 }
3984
6de2ae4a 3985 if (htab->elf.sgot == NULL)
bffbf940
JJ
3986 abort ();
3987
0afcef53 3988 off = htab->tls_ld_or_ldm_got.offset;
bffbf940
JJ
3989 if (off & 1)
3990 off &= ~1;
3991 else
3992 {
3993 Elf_Internal_Rela outrel;
bffbf940 3994
6de2ae4a 3995 if (htab->elf.srelgot == NULL)
bffbf940
JJ
3996 abort ();
3997
6de2ae4a
L
3998 outrel.r_offset = (htab->elf.sgot->output_section->vma
3999 + htab->elf.sgot->output_offset + off);
bffbf940
JJ
4000
4001 bfd_put_64 (output_bfd, 0,
6de2ae4a 4002 htab->elf.sgot->contents + off);
bffbf940 4003 bfd_put_64 (output_bfd, 0,
6de2ae4a 4004 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 4005 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
bffbf940 4006 outrel.r_addend = 0;
351f65ca 4007 elf_append_rela (output_bfd, htab->elf.srelgot,
464d3bd4 4008 &outrel);
0afcef53 4009 htab->tls_ld_or_ldm_got.offset |= 1;
bffbf940 4010 }
6de2ae4a
L
4011 relocation = htab->elf.sgot->output_section->vma
4012 + htab->elf.sgot->output_offset + off;
b34976b6 4013 unresolved_reloc = FALSE;
bffbf940
JJ
4014 break;
4015
4016 case R_X86_64_DTPOFF32:
0e1862bb
L
4017 if (!bfd_link_executable (info)
4018 || (input_section->flags & SEC_CODE) == 0)
0afcef53 4019 relocation -= _bfd_x86_elf_dtpoff_base (info);
bffbf940 4020 else
351f65ca 4021 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4022 break;
4023
4024 case R_X86_64_TPOFF32:
6769d501 4025 case R_X86_64_TPOFF64:
0e1862bb 4026 BFD_ASSERT (bfd_link_executable (info));
351f65ca 4027 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4028 break;
4029
a69ed7f7
L
4030 case R_X86_64_DTPOFF64:
4031 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
0afcef53 4032 relocation -= _bfd_x86_elf_dtpoff_base (info);
a69ed7f7
L
4033 break;
4034
70256ad8
AJ
4035 default:
4036 break;
4037 }
8d88c4ca 4038
239e1f3a
AM
4039 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4040 because such sections are not SEC_ALLOC and thus ld.so will
4041 not process them. */
c434dee6 4042 if (unresolved_reloc
239e1f3a 4043 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
4044 && h->def_dynamic)
4045 && _bfd_elf_section_offset (output_bfd, info, input_section,
4046 rel->r_offset) != (bfd_vma) -1)
a040981f 4047 {
7073b5b9
L
4048 switch (r_type)
4049 {
4050 case R_X86_64_32S:
a5b4ee94
L
4051 sec = h->root.u.def.section;
4052 if ((info->nocopyreloc
4053 || (eh->def_protected
4054 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
7073b5b9
L
4055 && !(h->root.u.def.section->flags & SEC_CODE))
4056 return elf_x86_64_need_pic (info, input_bfd, input_section,
4057 h, NULL, NULL, howto);
4058 /* Fall through. */
4059
4060 default:
4061 _bfd_error_handler
4062 /* xgettext:c-format */
2dcf00ce
AM
4063 (_("%pB(%pA+%#" PRIx64 "): "
4064 "unresolvable %s relocation against symbol `%s'"),
7073b5b9
L
4065 input_bfd,
4066 input_section,
2dcf00ce 4067 (uint64_t) rel->r_offset,
7073b5b9
L
4068 howto->name,
4069 h->root.root.string);
4070 return FALSE;
4071 }
a040981f 4072 }
c434dee6 4073
dc1e8a47 4074 do_relocation:
8d88c4ca 4075 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
4076 contents, rel->r_offset,
4077 relocation, rel->r_addend);
8d88c4ca 4078
dc1e8a47 4079 check_relocation_error:
8d88c4ca 4080 if (r != bfd_reloc_ok)
8da6118f 4081 {
c434dee6
AJ
4082 const char *name;
4083
4084 if (h != NULL)
4085 name = h->root.root.string;
4086 else
8da6118f 4087 {
c434dee6
AJ
4088 name = bfd_elf_string_from_elf_section (input_bfd,
4089 symtab_hdr->sh_link,
4090 sym->st_name);
4091 if (name == NULL)
b34976b6 4092 return FALSE;
c434dee6 4093 if (*name == '\0')
fd361982 4094 name = bfd_section_name (sec);
c434dee6
AJ
4095 }
4096
4097 if (r == bfd_reloc_overflow)
8b43e456 4098 {
78984959 4099 if (converted_reloc)
8b43e456
L
4100 {
4101 info->callbacks->einfo
4102 (_("%F%P: failed to convert GOTPCREL relocation; relink with --no-relax\n"));
4103 return FALSE;
4104 }
4105 (*info->callbacks->reloc_overflow)
4106 (info, (h ? &h->root : NULL), name, howto->name,
4107 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4108 }
c434dee6
AJ
4109 else
4110 {
4eca0228 4111 _bfd_error_handler
695344c0 4112 /* xgettext:c-format */
2dcf00ce 4113 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
d003868e 4114 input_bfd, input_section,
2dcf00ce 4115 (uint64_t) rel->r_offset, name, (int) r);
b34976b6 4116 return FALSE;
8da6118f
KH
4117 }
4118 }
60f2e42e
L
4119
4120 if (wrel != rel)
4121 *wrel = *rel;
4122 }
4123
4124 if (wrel != rel)
4125 {
4126 Elf_Internal_Shdr *rel_hdr;
4127 size_t deleted = rel - wrel;
4128
4129 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
4130 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4131 if (rel_hdr->sh_size == 0)
4132 {
4133 /* It is too late to remove an empty reloc section. Leave
4134 one NONE reloc.
4135 ??? What is wrong with an empty section??? */
4136 rel_hdr->sh_size = rel_hdr->sh_entsize;
4137 deleted -= 1;
4138 }
4139 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
4140 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4141 input_section->reloc_count -= deleted;
8d88c4ca 4142 }
70256ad8 4143
b34976b6 4144 return TRUE;
70256ad8
AJ
4145}
4146
4147/* Finish up dynamic symbol handling. We set the contents of various
4148 dynamic sections here. */
4149
b34976b6 4150static bfd_boolean
351f65ca
L
4151elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4152 struct bfd_link_info *info,
4153 struct elf_link_hash_entry *h,
aec6b87e 4154 Elf_Internal_Sym *sym)
70256ad8 4155{
0afcef53 4156 struct elf_x86_link_hash_table *htab;
f2c29a16 4157 bfd_boolean use_plt_second;
0afcef53 4158 struct elf_x86_link_hash_entry *eh;
aec6b87e 4159 bfd_boolean local_undefweak;
70256ad8 4160
0afcef53 4161 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6
NC
4162 if (htab == NULL)
4163 return FALSE;
70256ad8 4164
f2c29a16
L
4165 /* Use the second PLT section only if there is .plt section. */
4166 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
0ff2b86e 4167
0afcef53 4168 eh = (struct elf_x86_link_hash_entry *) h;
9e9821dd
L
4169 if (eh->no_finish_dynamic_symbol)
4170 abort ();
dd7e64d4 4171
aec6b87e
L
4172 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
4173 resolved undefined weak symbols in executable so that their
4174 references have value 0 at run-time. */
c5bce5c6 4175 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
aec6b87e 4176
70256ad8
AJ
4177 if (h->plt.offset != (bfd_vma) -1)
4178 {
70256ad8 4179 bfd_vma plt_index;
38b12349 4180 bfd_vma got_offset, plt_offset;
70256ad8 4181 Elf_Internal_Rela rela;
947216bf 4182 bfd_byte *loc;
0ff2b86e 4183 asection *plt, *gotplt, *relplt, *resolved_plt;
351f65ca 4184 const struct elf_backend_data *bed;
5974eba6 4185 bfd_vma plt_got_pcrel_offset;
cbe950e9
L
4186
4187 /* When building a static executable, use .iplt, .igot.plt and
4188 .rela.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 4189 if (htab->elf.splt != NULL)
cbe950e9 4190 {
6de2ae4a
L
4191 plt = htab->elf.splt;
4192 gotplt = htab->elf.sgotplt;
4193 relplt = htab->elf.srelplt;
cbe950e9
L
4194 }
4195 else
4196 {
6de2ae4a
L
4197 plt = htab->elf.iplt;
4198 gotplt = htab->elf.igotplt;
4199 relplt = htab->elf.irelplt;
cbe950e9 4200 }
70256ad8 4201
f3180fa9 4202 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
70256ad8
AJ
4203
4204 /* Get the index in the procedure linkage table which
4205 corresponds to this symbol. This is the index of this symbol
4206 in all the symbols for which we are making plt entries. The
cbe950e9 4207 first entry in the procedure linkage table is reserved.
6bbec505 4208
cbe950e9 4209 Get the offset into the .got table of the entry that
407443a3 4210 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
cbe950e9
L
4211 bytes. The first three are reserved for the dynamic linker.
4212
4213 For static executables, we don't reserve anything. */
4214
6de2ae4a 4215 if (plt == htab->elf.splt)
cbe950e9 4216 {
765e526c
L
4217 got_offset = (h->plt.offset / htab->plt.plt_entry_size
4218 - htab->plt.has_plt0);
e1f98742 4219 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
cbe950e9
L
4220 }
4221 else
4222 {
765e526c 4223 got_offset = h->plt.offset / htab->plt.plt_entry_size;
e1f98742 4224 got_offset = got_offset * GOT_ENTRY_SIZE;
cbe950e9 4225 }
70256ad8 4226
38b12349 4227 /* Fill in the entry in the procedure linkage table. */
765e526c
L
4228 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
4229 htab->plt.plt_entry_size);
f2c29a16 4230 if (use_plt_second)
0ff2b86e 4231 {
f2c29a16 4232 memcpy (htab->plt_second->contents + eh->plt_second.offset,
765e526c
L
4233 htab->non_lazy_plt->plt_entry,
4234 htab->non_lazy_plt->plt_entry_size);
0ff2b86e 4235
f2c29a16
L
4236 resolved_plt = htab->plt_second;
4237 plt_offset = eh->plt_second.offset;
0ff2b86e
L
4238 }
4239 else
4240 {
0ff2b86e
L
4241 resolved_plt = plt;
4242 plt_offset = h->plt.offset;
4243 }
eed180f8
RM
4244
4245 /* Insert the relocation positions of the plt section. */
4246
4247 /* Put offset the PC-relative instruction referring to the GOT entry,
4248 subtracting the size of that instruction. */
ab7fede8
L
4249 plt_got_pcrel_offset = (gotplt->output_section->vma
4250 + gotplt->output_offset
4251 + got_offset
4252 - resolved_plt->output_section->vma
4253 - resolved_plt->output_offset
4254 - plt_offset
765e526c 4255 - htab->plt.plt_got_insn_size);
ab7fede8
L
4256
4257 /* Check PC-relative offset overflow in PLT entry. */
5974eba6 4258 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
695344c0 4259 /* xgettext:c-format */
871b3ab2 4260 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in PLT entry for `%s'\n"),
ab7fede8
L
4261 output_bfd, h->root.root.string);
4262
4263 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
38b12349 4264 (resolved_plt->contents + plt_offset
765e526c 4265 + htab->plt.plt_got_offset));
cbe950e9 4266
653165cc 4267 /* Fill in the entry in the global offset table, initially this
aec6b87e
L
4268 points to the second part of the PLT entry. Leave the entry
4269 as zero for undefined weak symbol in PIE. No PLT relocation
4270 against undefined weak symbol in PIE. */
4271 if (!local_undefweak)
cbe950e9 4272 {
765e526c 4273 if (htab->plt.has_plt0)
38b12349
L
4274 bfd_put_64 (output_bfd, (plt->output_section->vma
4275 + plt->output_offset
4276 + h->plt.offset
765e526c 4277 + htab->lazy_plt->plt_lazy_offset),
38b12349 4278 gotplt->contents + got_offset);
aec6b87e
L
4279
4280 /* Fill in the entry in the .rela.plt section. */
4281 rela.r_offset = (gotplt->output_section->vma
4282 + gotplt->output_offset
4283 + got_offset);
cf1070f1 4284 if (PLT_LOCAL_IFUNC_P (info, h))
aec6b87e 4285 {
871b3ab2 4286 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
4287 h->root.root.string,
4288 h->root.u.def.section->owner);
4289
aec6b87e
L
4290 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4291 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4292 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4293 rela.r_addend = (h->root.u.def.value
4294 + h->root.u.def.section->output_section->vma
4295 + h->root.u.def.section->output_offset);
4296 /* R_X86_64_IRELATIVE comes last. */
4297 plt_index = htab->next_irelative_index--;
4298 }
4299 else
4300 {
4301 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4302 rela.r_addend = 0;
4303 plt_index = htab->next_jump_slot_index++;
4304 }
e1f98742 4305
38b12349
L
4306 /* Don't fill the second and third slots in PLT entry for
4307 static executables nor without PLT0. */
765e526c 4308 if (plt == htab->elf.splt && htab->plt.has_plt0)
aec6b87e 4309 {
38b12349 4310 bfd_vma plt0_offset
765e526c 4311 = h->plt.offset + htab->lazy_plt->plt_plt_insn_end;
aec6b87e
L
4312
4313 /* Put relocation index. */
4314 bfd_put_32 (output_bfd, plt_index,
4315 (plt->contents + h->plt.offset
765e526c 4316 + htab->lazy_plt->plt_reloc_offset));
aec6b87e
L
4317
4318 /* Put offset for jmp .PLT0 and check for overflow. We don't
4319 check relocation index for overflow since branch displacement
4320 will overflow first. */
4321 if (plt0_offset > 0x80000000)
695344c0 4322 /* xgettext:c-format */
871b3ab2 4323 info->callbacks->einfo (_("%F%pB: branch displacement overflow in PLT entry for `%s'\n"),
aec6b87e
L
4324 output_bfd, h->root.root.string);
4325 bfd_put_32 (output_bfd, - plt0_offset,
38b12349 4326 (plt->contents + h->plt.offset
765e526c 4327 + htab->lazy_plt->plt_plt_offset));
aec6b87e 4328 }
351f65ca 4329
aec6b87e
L
4330 bed = get_elf_backend_data (output_bfd);
4331 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4332 bed->s->swap_reloca_out (output_bfd, &rela, loc);
4333 }
dd7e64d4
L
4334 }
4335 else if (eh->plt_got.offset != (bfd_vma) -1)
4336 {
38b12349 4337 bfd_vma got_offset, plt_offset;
dd7e64d4
L
4338 asection *plt, *got;
4339 bfd_boolean got_after_plt;
4340 int32_t got_pcrel_offset;
dd7e64d4
L
4341
4342 /* Set the entry in the GOT procedure linkage table. */
4343 plt = htab->plt_got;
4344 got = htab->elf.sgot;
4345 got_offset = h->got.offset;
4346
4347 if (got_offset == (bfd_vma) -1
e492d2f8 4348 || (h->type == STT_GNU_IFUNC && h->def_regular)
dd7e64d4
L
4349 || plt == NULL
4350 || got == NULL)
4351 abort ();
70256ad8 4352
38b12349 4353 /* Use the non-lazy PLT entry template for the GOT PLT since they
dd7e64d4 4354 are the identical. */
dd7e64d4
L
4355 /* Fill in the entry in the GOT procedure linkage table. */
4356 plt_offset = eh->plt_got.offset;
4357 memcpy (plt->contents + plt_offset,
765e526c
L
4358 htab->non_lazy_plt->plt_entry,
4359 htab->non_lazy_plt->plt_entry_size);
dd7e64d4
L
4360
4361 /* Put offset the PC-relative instruction referring to the GOT
4362 entry, subtracting the size of that instruction. */
4363 got_pcrel_offset = (got->output_section->vma
4364 + got->output_offset
4365 + got_offset
4366 - plt->output_section->vma
4367 - plt->output_offset
4368 - plt_offset
765e526c 4369 - htab->non_lazy_plt->plt_got_insn_size);
dd7e64d4
L
4370
4371 /* Check PC-relative offset overflow in GOT PLT entry. */
4372 got_after_plt = got->output_section->vma > plt->output_section->vma;
4373 if ((got_after_plt && got_pcrel_offset < 0)
4374 || (!got_after_plt && got_pcrel_offset > 0))
695344c0 4375 /* xgettext:c-format */
871b3ab2 4376 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
dd7e64d4
L
4377 output_bfd, h->root.root.string);
4378
4379 bfd_put_32 (output_bfd, got_pcrel_offset,
38b12349 4380 (plt->contents + plt_offset
765e526c 4381 + htab->non_lazy_plt->plt_got_offset));
dd7e64d4
L
4382 }
4383
aec6b87e
L
4384 if (!local_undefweak
4385 && !h->def_regular
dd7e64d4
L
4386 && (h->plt.offset != (bfd_vma) -1
4387 || eh->plt_got.offset != (bfd_vma) -1))
4388 {
4389 /* Mark the symbol as undefined, rather than as defined in
4390 the .plt section. Leave the value if there were any
4391 relocations where pointer equality matters (this is a clue
4392 for the dynamic linker, to make function pointer
4393 comparisons work between an application and shared
4394 library), otherwise set it to zero. If a function is only
4395 called from a binary, there is no need to slow down
4396 shared libraries because of that. */
4397 sym->st_shndx = SHN_UNDEF;
4398 if (!h->pointer_equality_needed)
4399 sym->st_value = 0;
70256ad8
AJ
4400 }
4401
4ec09950
L
4402 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
4403
aec6b87e
L
4404 /* Don't generate dynamic GOT relocation against undefined weak
4405 symbol in executable. */
bffbf940 4406 if (h->got.offset != (bfd_vma) -1
0afcef53
L
4407 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
4408 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
aec6b87e 4409 && !local_undefweak)
053579d7 4410 {
053579d7 4411 Elf_Internal_Rela rela;
233cc9c1 4412 asection *relgot = htab->elf.srelgot;
053579d7
AJ
4413
4414 /* This symbol has an entry in the global offset table. Set it
bffbf940 4415 up. */
6de2ae4a 4416 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
c434dee6 4417 abort ();
053579d7 4418
6de2ae4a
L
4419 rela.r_offset = (htab->elf.sgot->output_section->vma
4420 + htab->elf.sgot->output_offset
dc810e39 4421 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
4422
4423 /* If this is a static link, or it is a -Bsymbolic link and the
4424 symbol is defined locally or was forced to be local because
4425 of a version file, we just want to emit a RELATIVE reloc.
4426 The entry in the global offset table will already have been
4427 initialized in the relocate_section function. */
710ab287 4428 if (h->def_regular
0018b0a3
L
4429 && h->type == STT_GNU_IFUNC)
4430 {
233cc9c1
L
4431 if (h->plt.offset == (bfd_vma) -1)
4432 {
4433 /* STT_GNU_IFUNC is referenced without PLT. */
4434 if (htab->elf.splt == NULL)
4435 {
4436 /* use .rel[a].iplt section to store .got relocations
4437 in static executable. */
4438 relgot = htab->elf.irelplt;
4439 }
6999821f 4440 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
233cc9c1 4441 {
871b3ab2 4442 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
4443 h->root.root.string,
4444 h->root.u.def.section->owner);
4445
233cc9c1
L
4446 rela.r_info = htab->r_info (0,
4447 R_X86_64_IRELATIVE);
4448 rela.r_addend = (h->root.u.def.value
4449 + h->root.u.def.section->output_section->vma
4450 + h->root.u.def.section->output_offset);
4451 }
4452 else
4453 goto do_glob_dat;
4454 }
4455 else if (bfd_link_pic (info))
710ab287
L
4456 {
4457 /* Generate R_X86_64_GLOB_DAT. */
4458 goto do_glob_dat;
4459 }
4460 else
4461 {
90d60710 4462 asection *plt;
aab82f4c 4463 bfd_vma plt_offset;
90d60710 4464
710ab287
L
4465 if (!h->pointer_equality_needed)
4466 abort ();
4467
4468 /* For non-shared object, we can't use .got.plt, which
4469 contains the real function addres if we need pointer
4470 equality. We load the GOT entry with the PLT entry. */
f2c29a16 4471 if (htab->plt_second != NULL)
aab82f4c 4472 {
f2c29a16
L
4473 plt = htab->plt_second;
4474 plt_offset = eh->plt_second.offset;
aab82f4c
L
4475 }
4476 else
4477 {
4478 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4479 plt_offset = h->plt.offset;
4480 }
710ab287
L
4481 bfd_put_64 (output_bfd, (plt->output_section->vma
4482 + plt->output_offset
aab82f4c 4483 + plt_offset),
6de2ae4a 4484 htab->elf.sgot->contents + h->got.offset);
710ab287
L
4485 return TRUE;
4486 }
0018b0a3 4487 }
0e1862bb 4488 else if (bfd_link_pic (info)
6999821f 4489 && SYMBOL_REFERENCES_LOCAL_P (info, h))
053579d7 4490 {
83924b38 4491 if (!SYMBOL_DEFINED_NON_SHARED_P (h))
41bed6dd 4492 return FALSE;
cc78d0af 4493 BFD_ASSERT((h->got.offset & 1) != 0);
351f65ca 4494 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
053579d7
AJ
4495 rela.r_addend = (h->root.u.def.value
4496 + h->root.u.def.section->output_section->vma
4497 + h->root.u.def.section->output_offset);
4498 }
4499 else
4500 {
4501 BFD_ASSERT((h->got.offset & 1) == 0);
dc1e8a47 4502 do_glob_dat:
c434dee6 4503 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 4504 htab->elf.sgot->contents + h->got.offset);
351f65ca 4505 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
053579d7
AJ
4506 rela.r_addend = 0;
4507 }
4508
233cc9c1 4509 elf_append_rela (output_bfd, relgot, &rela);
053579d7
AJ
4510 }
4511
f5385ebf 4512 if (h->needs_copy)
70256ad8 4513 {
70256ad8 4514 Elf_Internal_Rela rela;
5474d94f 4515 asection *s;
70256ad8
AJ
4516
4517 /* This symbol needs a copy reloc. Set it up. */
ff38b4cc 4518 VERIFY_COPY_RELOC (h, htab)
70256ad8
AJ
4519
4520 rela.r_offset = (h->root.u.def.value
4521 + h->root.u.def.section->output_section->vma
4522 + h->root.u.def.section->output_offset);
351f65ca 4523 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
70256ad8 4524 rela.r_addend = 0;
afbf7e8e 4525 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
4526 s = htab->elf.sreldynrelro;
4527 else
4528 s = htab->elf.srelbss;
4529 elf_append_rela (output_bfd, s, &rela);
70256ad8
AJ
4530 }
4531
b34976b6 4532 return TRUE;
70256ad8
AJ
4533}
4534
c25bc9fc
L
4535/* Finish up local dynamic symbol handling. We set the contents of
4536 various dynamic sections here. */
4537
4538static bfd_boolean
351f65ca 4539elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
c25bc9fc
L
4540{
4541 struct elf_link_hash_entry *h
4542 = (struct elf_link_hash_entry *) *slot;
4543 struct bfd_link_info *info
eed180f8 4544 = (struct bfd_link_info *) inf;
c25bc9fc 4545
351f65ca 4546 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5f0f0847 4547 info, h, NULL);
c25bc9fc
L
4548}
4549
aec6b87e
L
4550/* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4551 here since undefined weak symbol may not be dynamic and may not be
4552 called for elf_x86_64_finish_dynamic_symbol. */
4553
4554static bfd_boolean
4555elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4556 void *inf)
4557{
4558 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4559 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4560
4561 if (h->root.type != bfd_link_hash_undefweak
4562 || h->dynindx != -1)
4563 return TRUE;
4564
4565 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5f0f0847 4566 info, h, NULL);
aec6b87e
L
4567}
4568
c434dee6
AJ
4569/* Used to decide how to sort relocs in an optimal manner for the
4570 dynamic linker, before writing them out. */
4571
4572static enum elf_reloc_type_class
cae1fbbb 4573elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
7e612e98
AM
4574 const asection *rel_sec ATTRIBUTE_UNUSED,
4575 const Elf_Internal_Rela *rela)
c434dee6 4576{
cae1fbbb
L
4577 bfd *abfd = info->output_bfd;
4578 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0afcef53
L
4579 struct elf_x86_link_hash_table *htab
4580 = elf_x86_hash_table (info, X86_64_ELF_DATA);
cae1fbbb 4581
d9e3b590
L
4582 if (htab->elf.dynsym != NULL
4583 && htab->elf.dynsym->contents != NULL)
4584 {
4585 /* Check relocation against STT_GNU_IFUNC symbol if there are
07d6d2b8 4586 dynamic symbols. */
d9e3b590 4587 unsigned long r_symndx = htab->r_sym (rela->r_info);
897463b1
L
4588 if (r_symndx != STN_UNDEF)
4589 {
4590 Elf_Internal_Sym sym;
4591 if (!bed->s->swap_symbol_in (abfd,
4592 (htab->elf.dynsym->contents
4593 + r_symndx * bed->s->sizeof_sym),
4594 0, &sym))
4595 abort ();
d9e3b590 4596
897463b1
L
4597 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4598 return reloc_class_ifunc;
4599 }
d9e3b590 4600 }
cae1fbbb 4601
351f65ca 4602 switch ((int) ELF32_R_TYPE (rela->r_info))
c434dee6 4603 {
c428ce9d
L
4604 case R_X86_64_IRELATIVE:
4605 return reloc_class_ifunc;
c434dee6 4606 case R_X86_64_RELATIVE:
1da80baa 4607 case R_X86_64_RELATIVE64:
c434dee6
AJ
4608 return reloc_class_relative;
4609 case R_X86_64_JUMP_SLOT:
4610 return reloc_class_plt;
4611 case R_X86_64_COPY:
4612 return reloc_class_copy;
4613 default:
4614 return reloc_class_normal;
4615 }
4616}
4617
70256ad8
AJ
4618/* Finish up the dynamic sections. */
4619
b34976b6 4620static bfd_boolean
351f65ca
L
4621elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4622 struct bfd_link_info *info)
70256ad8 4623{
0afcef53 4624 struct elf_x86_link_hash_table *htab;
70256ad8 4625
9577f60b 4626 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
4dfe6ac6
NC
4627 if (htab == NULL)
4628 return FALSE;
4629
9577f60b
L
4630 if (! htab->elf.dynamic_sections_created)
4631 return TRUE;
70256ad8 4632
9577f60b 4633 if (htab->elf.splt && htab->elf.splt->size > 0)
e41b3a13 4634 {
9577f60b
L
4635 elf_section_data (htab->elf.splt->output_section)
4636 ->this_hdr.sh_entsize = htab->plt.plt_entry_size;
e41b3a13 4637
9577f60b 4638 if (htab->plt.has_plt0)
fff53dae 4639 {
9577f60b
L
4640 /* Fill in the special first entry in the procedure linkage
4641 table. */
4642 memcpy (htab->elf.splt->contents,
4643 htab->lazy_plt->plt0_entry,
4644 htab->lazy_plt->plt0_entry_size);
4645 /* Add offset for pushq GOT+8(%rip), since the instruction
4646 uses 6 bytes subtract this value. */
4647 bfd_put_32 (output_bfd,
4648 (htab->elf.sgotplt->output_section->vma
4649 + htab->elf.sgotplt->output_offset
4650 + 8
4651 - htab->elf.splt->output_section->vma
4652 - htab->elf.splt->output_offset
4653 - 6),
4654 (htab->elf.splt->contents
4655 + htab->lazy_plt->plt0_got1_offset));
4656 /* Add offset for the PC-relative instruction accessing
4657 GOT+16, subtracting the offset to the end of that
4658 instruction. */
4659 bfd_put_32 (output_bfd,
4660 (htab->elf.sgotplt->output_section->vma
4661 + htab->elf.sgotplt->output_offset
4662 + 16
4663 - htab->elf.splt->output_section->vma
4664 - htab->elf.splt->output_offset
4665 - htab->lazy_plt->plt0_got2_insn_end),
4666 (htab->elf.splt->contents
4667 + htab->lazy_plt->plt0_got2_offset));
fff53dae 4668 }
fff53dae 4669
9bcc30e4 4670 if (htab->elf.tlsdesc_plt)
8361ed4d 4671 {
9577f60b 4672 bfd_put_64 (output_bfd, (bfd_vma) 0,
9bcc30e4 4673 htab->elf.sgot->contents + htab->elf.tlsdesc_got);
9577f60b 4674
9bcc30e4 4675 memcpy (htab->elf.splt->contents + htab->elf.tlsdesc_plt,
92e68c1d
L
4676 htab->lazy_plt->plt_tlsdesc_entry,
4677 htab->lazy_plt->plt_tlsdesc_entry_size);
9577f60b 4678
bf54968b
L
4679 /* Add offset for pushq GOT+8(%rip), since ENDBR64 uses 4
4680 bytes and the instruction uses 6 bytes, subtract these
4681 values. */
9577f60b
L
4682 bfd_put_32 (output_bfd,
4683 (htab->elf.sgotplt->output_section->vma
4684 + htab->elf.sgotplt->output_offset
4685 + 8
4686 - htab->elf.splt->output_section->vma
4687 - htab->elf.splt->output_offset
9bcc30e4 4688 - htab->elf.tlsdesc_plt
92e68c1d 4689 - htab->lazy_plt->plt_tlsdesc_got1_insn_end),
9577f60b 4690 (htab->elf.splt->contents
9bcc30e4 4691 + htab->elf.tlsdesc_plt
92e68c1d 4692 + htab->lazy_plt->plt_tlsdesc_got1_offset));
bf54968b
L
4693 /* Add offset for indirect branch via GOT+TDG, where TDG
4694 stands for htab->tlsdesc_got, subtracting the offset
4695 to the end of that instruction. */
9577f60b
L
4696 bfd_put_32 (output_bfd,
4697 (htab->elf.sgot->output_section->vma
4698 + htab->elf.sgot->output_offset
9bcc30e4 4699 + htab->elf.tlsdesc_got
9577f60b
L
4700 - htab->elf.splt->output_section->vma
4701 - htab->elf.splt->output_offset
9bcc30e4 4702 - htab->elf.tlsdesc_plt
92e68c1d 4703 - htab->lazy_plt->plt_tlsdesc_got2_insn_end),
9577f60b 4704 (htab->elf.splt->contents
9bcc30e4 4705 + htab->elf.tlsdesc_plt
92e68c1d 4706 + htab->lazy_plt->plt_tlsdesc_got2_offset));
8361ed4d
L
4707 }
4708 }
4709
aec6b87e
L
4710 /* Fill PLT entries for undefined weak symbols in PIE. */
4711 if (bfd_link_pie (info))
4712 bfd_hash_traverse (&info->hash->table,
4713 elf_x86_64_pie_finish_undefweak_symbol,
4714 info);
4715
b34976b6 4716 return TRUE;
8d88c4ca
NC
4717}
4718
233cc9c1
L
4719/* Fill PLT/GOT entries and allocate dynamic relocations for local
4720 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4721 It has to be done before elf_link_sort_relocs is called so that
4722 dynamic relocations are properly sorted. */
4723
4724static bfd_boolean
4725elf_x86_64_output_arch_local_syms
4726 (bfd *output_bfd ATTRIBUTE_UNUSED,
4727 struct bfd_link_info *info,
4728 void *flaginfo ATTRIBUTE_UNUSED,
4729 int (*func) (void *, const char *,
4730 Elf_Internal_Sym *,
4731 asection *,
4732 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4733{
0afcef53
L
4734 struct elf_x86_link_hash_table *htab
4735 = elf_x86_hash_table (info, X86_64_ELF_DATA);
233cc9c1
L
4736 if (htab == NULL)
4737 return FALSE;
4738
4739 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4740 htab_traverse (htab->loc_hash_table,
4741 elf_x86_64_finish_local_dynamic_symbol,
4742 info);
4743
4744 return TRUE;
4745}
4746
38b12349 4747/* Forward declaration. */
765e526c 4748static const struct elf_x86_lazy_plt_layout elf_x86_64_nacl_plt;
38b12349
L
4749
4750/* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4751 dynamic relocations. */
4752
4753static long
4754elf_x86_64_get_synthetic_symtab (bfd *abfd,
4755 long symcount ATTRIBUTE_UNUSED,
4756 asymbol **syms ATTRIBUTE_UNUSED,
4757 long dynsymcount,
4758 asymbol **dynsyms,
4759 asymbol **ret)
4760{
f493882d 4761 long count, i, n;
38b12349 4762 int j;
38b12349 4763 bfd_byte *plt_contents;
f493882d 4764 long relsize;
765e526c
L
4765 const struct elf_x86_lazy_plt_layout *lazy_plt;
4766 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4767 const struct elf_x86_lazy_plt_layout *lazy_bnd_plt;
4768 const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_plt;
4769 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4770 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
38b12349 4771 asection *plt;
f493882d
L
4772 enum elf_x86_plt_type plt_type;
4773 struct elf_x86_plt plts[] =
144bed8d 4774 {
38b12349
L
4775 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4776 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
f2c29a16
L
4777 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4778 { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
dd9e66ee 4779 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
38b12349 4780 };
144bed8d 4781
38b12349 4782 *ret = NULL;
144bed8d 4783
38b12349
L
4784 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4785 return 0;
144bed8d 4786
38b12349
L
4787 if (dynsymcount <= 0)
4788 return 0;
cca5b8b6 4789
38b12349
L
4790 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4791 if (relsize <= 0)
4792 return -1;
4793
90c14f0c 4794 if (get_elf_backend_data (abfd)->target_os != is_nacl)
144bed8d 4795 {
38b12349
L
4796 lazy_plt = &elf_x86_64_lazy_plt;
4797 non_lazy_plt = &elf_x86_64_non_lazy_plt;
4798 lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
4799 non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
ee2fdd6f
L
4800 if (ABI_64_P (abfd))
4801 {
4802 lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
4803 non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
4804 }
4805 else
4806 {
4807 lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
4808 non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
4809 }
38b12349
L
4810 }
4811 else
4812 {
4813 lazy_plt = &elf_x86_64_nacl_plt;
4814 non_lazy_plt = NULL;
4815 lazy_bnd_plt = NULL;
4816 non_lazy_bnd_plt = NULL;
ee2fdd6f
L
4817 lazy_ibt_plt = NULL;
4818 non_lazy_ibt_plt = NULL;
38b12349 4819 }
144bed8d 4820
38b12349
L
4821 count = 0;
4822 for (j = 0; plts[j].name != NULL; j++)
4823 {
4824 plt = bfd_get_section_by_name (abfd, plts[j].name);
90efb642 4825 if (plt == NULL || plt->size == 0)
6f25f223 4826 continue;
533d0af0 4827
38b12349
L
4828 /* Get the PLT section contents. */
4829 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4830 if (plt_contents == NULL)
4831 break;
4832 if (!bfd_get_section_contents (abfd, (asection *) plt,
4833 plt_contents, 0, plt->size))
4834 {
4835 free (plt_contents);
4836 break;
4837 }
144bed8d 4838
38b12349
L
4839 /* Check what kind of PLT it is. */
4840 plt_type = plt_unknown;
90efb642
L
4841 if (plts[j].type == plt_unknown
4842 && (plt->size >= (lazy_plt->plt_entry_size
4843 + lazy_plt->plt_entry_size)))
144bed8d 4844 {
38b12349
L
4845 /* Match lazy PLT first. Need to check the first two
4846 instructions. */
4847 if ((memcmp (plt_contents, lazy_plt->plt0_entry,
4848 lazy_plt->plt0_got1_offset) == 0)
4849 && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
4850 2) == 0))
4851 plt_type = plt_lazy;
4852 else if (lazy_bnd_plt != NULL
4853 && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
4854 lazy_bnd_plt->plt0_got1_offset) == 0)
4855 && (memcmp (plt_contents + 6,
4856 lazy_bnd_plt->plt0_entry + 6, 3) == 0))
ec1f73bb 4857 {
f2c29a16 4858 plt_type = plt_lazy | plt_second;
ee2fdd6f
L
4859 /* The fist entry in the lazy IBT PLT is the same as the
4860 lazy BND PLT. */
4861 if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
4862 lazy_ibt_plt->plt_entry,
4863 lazy_ibt_plt->plt_got_offset) == 0))
4864 lazy_plt = lazy_ibt_plt;
4865 else
4866 lazy_plt = lazy_bnd_plt;
ec1f73bb 4867 }
144bed8d 4868 }
fca6ae69 4869
38b12349 4870 if (non_lazy_plt != NULL
90efb642
L
4871 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4872 && plt->size >= non_lazy_plt->plt_entry_size)
38b12349
L
4873 {
4874 /* Match non-lazy PLT. */
4875 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4876 non_lazy_plt->plt_got_offset) == 0)
4877 plt_type = plt_non_lazy;
4878 }
4879
ee2fdd6f 4880 if (plt_type == plt_unknown || plt_type == plt_second)
38b12349 4881 {
ee2fdd6f 4882 if (non_lazy_bnd_plt != NULL
90efb642 4883 && plt->size >= non_lazy_bnd_plt->plt_entry_size
ee2fdd6f
L
4884 && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
4885 non_lazy_bnd_plt->plt_got_offset) == 0))
38b12349 4886 {
ee2fdd6f 4887 /* Match BND PLT. */
f2c29a16 4888 plt_type = plt_second;
38b12349
L
4889 non_lazy_plt = non_lazy_bnd_plt;
4890 }
ee2fdd6f 4891 else if (non_lazy_ibt_plt != NULL
90efb642 4892 && plt->size >= non_lazy_ibt_plt->plt_entry_size
ee2fdd6f
L
4893 && (memcmp (plt_contents,
4894 non_lazy_ibt_plt->plt_entry,
4895 non_lazy_ibt_plt->plt_got_offset) == 0))
4896 {
4897 /* Match IBT PLT. */
4898 plt_type = plt_second;
4899 non_lazy_plt = non_lazy_ibt_plt;
4900 }
38b12349
L
4901 }
4902
4903 if (plt_type == plt_unknown)
37c0b6ee
L
4904 {
4905 free (plt_contents);
4906 continue;
4907 }
38b12349
L
4908
4909 plts[j].sec = plt;
4910 plts[j].type = plt_type;
4911
4912 if ((plt_type & plt_lazy))
4913 {
4914 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4915 plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
4916 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4917 /* Skip PLT0 in lazy PLT. */
4918 i = 1;
4919 }
4920 else
4921 {
4922 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4923 plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
4924 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4925 i = 0;
4926 }
4927
f2c29a16
L
4928 /* Skip lazy PLT when the second PLT is used. */
4929 if (plt_type == (plt_lazy | plt_second))
38b12349
L
4930 plts[j].count = 0;
4931 else
4932 {
4933 n = plt->size / plts[j].plt_entry_size;
4934 plts[j].count = n;
4935 count += n - i;
4936 }
4937
4938 plts[j].contents = plt_contents;
144bed8d
L
4939 }
4940
f493882d
L
4941 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4942 (bfd_vma) 0, plts, dynsyms,
4943 ret);
0ff2b86e
L
4944}
4945
d2b2c203
DJ
4946/* Handle an x86-64 specific section when reading an object file. This
4947 is called when elfcode.h finds a section with an unknown type. */
4948
4949static bfd_boolean
0c723101
L
4950elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
4951 const char *name, int shindex)
d2b2c203
DJ
4952{
4953 if (hdr->sh_type != SHT_X86_64_UNWIND)
4954 return FALSE;
4955
6dc132d9 4956 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
4957 return FALSE;
4958
4959 return TRUE;
4960}
4961
3b22753a
L
4962/* Hook called by the linker routine which adds symbols from an object
4963 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4964 of .bss. */
4965
4966static bfd_boolean
351f65ca 4967elf_x86_64_add_symbol_hook (bfd *abfd,
a43942db 4968 struct bfd_link_info *info ATTRIBUTE_UNUSED,
351f65ca
L
4969 Elf_Internal_Sym *sym,
4970 const char **namep ATTRIBUTE_UNUSED,
4971 flagword *flagsp ATTRIBUTE_UNUSED,
4972 asection **secp,
4973 bfd_vma *valp)
3b22753a
L
4974{
4975 asection *lcomm;
4976
4977 switch (sym->st_shndx)
4978 {
4979 case SHN_X86_64_LCOMMON:
4980 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4981 if (lcomm == NULL)
4982 {
4983 lcomm = bfd_make_section_with_flags (abfd,
4984 "LARGE_COMMON",
4985 (SEC_ALLOC
4986 | SEC_IS_COMMON
4987 | SEC_LINKER_CREATED));
4988 if (lcomm == NULL)
4989 return FALSE;
4990 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4991 }
4992 *secp = lcomm;
4993 *valp = sym->st_size;
c35bdf6e 4994 return TRUE;
3b22753a 4995 }
d8045f23 4996
3b22753a
L
4997 return TRUE;
4998}
4999
5000
5001/* Given a BFD section, try to locate the corresponding ELF section
5002 index. */
5003
5004static bfd_boolean
351f65ca
L
5005elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5006 asection *sec, int *index_return)
3b22753a
L
5007{
5008 if (sec == &_bfd_elf_large_com_section)
5009 {
91d6fa6a 5010 *index_return = SHN_X86_64_LCOMMON;
3b22753a
L
5011 return TRUE;
5012 }
5013 return FALSE;
5014}
5015
5016/* Process a symbol. */
5017
5018static void
351f65ca
L
5019elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5020 asymbol *asym)
3b22753a
L
5021{
5022 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5023
5024 switch (elfsym->internal_elf_sym.st_shndx)
5025 {
5026 case SHN_X86_64_LCOMMON:
5027 asym->section = &_bfd_elf_large_com_section;
5028 asym->value = elfsym->internal_elf_sym.st_size;
5029 /* Common symbol doesn't set BSF_GLOBAL. */
5030 asym->flags &= ~BSF_GLOBAL;
5031 break;
5032 }
5033}
5034
5035static bfd_boolean
351f65ca 5036elf_x86_64_common_definition (Elf_Internal_Sym *sym)
3b22753a
L
5037{
5038 return (sym->st_shndx == SHN_COMMON
5039 || sym->st_shndx == SHN_X86_64_LCOMMON);
5040}
5041
5042static unsigned int
351f65ca 5043elf_x86_64_common_section_index (asection *sec)
3b22753a
L
5044{
5045 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5046 return SHN_COMMON;
5047 else
5048 return SHN_X86_64_LCOMMON;
5049}
5050
5051static asection *
351f65ca 5052elf_x86_64_common_section (asection *sec)
3b22753a
L
5053{
5054 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5055 return bfd_com_section_ptr;
5056 else
5057 return &_bfd_elf_large_com_section;
5058}
5059
5060static bfd_boolean
5d13b3b3
AM
5061elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5062 const Elf_Internal_Sym *sym,
351f65ca 5063 asection **psec,
5d13b3b3
AM
5064 bfd_boolean newdef,
5065 bfd_boolean olddef,
351f65ca 5066 bfd *oldbfd,
5d13b3b3 5067 const asection *oldsec)
3b22753a
L
5068{
5069 /* A normal common symbol and a large common symbol result in a
00492999
L
5070 normal common symbol. We turn the large common symbol into a
5071 normal one. */
5d13b3b3 5072 if (!olddef
3b22753a 5073 && h->root.type == bfd_link_hash_common
5d13b3b3
AM
5074 && !newdef
5075 && bfd_is_com_section (*psec)
5076 && oldsec != *psec)
3b22753a 5077 {
00492999 5078 if (sym->st_shndx == SHN_COMMON
5d13b3b3 5079 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
00492999
L
5080 {
5081 h->root.u.c.p->section
5082 = bfd_make_section_old_way (oldbfd, "COMMON");
5083 h->root.u.c.p->section->flags = SEC_ALLOC;
5084 }
5085 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5d13b3b3
AM
5086 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5087 *psec = bfd_com_section_ptr;
3b22753a
L
5088 }
5089
5090 return TRUE;
5091}
5092
5093static int
351f65ca
L
5094elf_x86_64_additional_program_headers (bfd *abfd,
5095 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
5096{
5097 asection *s;
9a2e389a 5098 int count = 0;
3b22753a
L
5099
5100 /* Check to see if we need a large readonly segment. */
5101 s = bfd_get_section_by_name (abfd, ".lrodata");
5102 if (s && (s->flags & SEC_LOAD))
5103 count++;
5104
5105 /* Check to see if we need a large data segment. Since .lbss sections
5106 is placed right after the .bss section, there should be no need for
5107 a large data segment just because of .lbss. */
5108 s = bfd_get_section_by_name (abfd, ".ldata");
5109 if (s && (s->flags & SEC_LOAD))
5110 count++;
5111
5112 return count;
5113}
5114
c543bf9a
L
5115/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5116
5117static bfd_boolean
5118elf_x86_64_relocs_compatible (const bfd_target *input,
5119 const bfd_target *output)
5120{
5121 return ((xvec_get_elf_backend_data (input)->s->elfclass
5122 == xvec_get_elf_backend_data (output)->s->elfclass)
5123 && _bfd_elf_relocs_compatible (input, output));
5124}
5125
38b12349
L
5126/* Set up x86-64 GNU properties. Return the first relocatable ELF input
5127 with GNU properties if found. Otherwise, return NULL. */
5128
5129static bfd *
5130elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
5131{
1de031c8 5132 struct elf_x86_init_table init_table;
ee2fdd6f 5133
78984959
L
5134 if ((int) R_X86_64_standard >= (int) R_X86_64_converted_reloc_bit
5135 || (int) R_X86_64_max <= (int) R_X86_64_converted_reloc_bit
5136 || ((int) (R_X86_64_GNU_VTINHERIT | R_X86_64_converted_reloc_bit)
5137 != (int) R_X86_64_GNU_VTINHERIT)
5138 || ((int) (R_X86_64_GNU_VTENTRY | R_X86_64_converted_reloc_bit)
5139 != (int) R_X86_64_GNU_VTENTRY))
5140 abort ();
5141
851b6fa1
L
5142 /* This is unused for x86-64. */
5143 init_table.plt0_pad_byte = 0x90;
5144
90c14f0c 5145 if (get_elf_backend_data (info->output_bfd)->target_os != is_nacl)
73784fa5 5146 {
5b9c07b2
L
5147 const struct elf_backend_data *bed
5148 = get_elf_backend_data (info->output_bfd);
5149 struct elf_x86_link_hash_table *htab
5150 = elf_x86_hash_table (info, bed->target_id);
5151 if (!htab)
5152 abort ();
5153 if (htab->params->bndplt)
ee2fdd6f 5154 {
1de031c8
L
5155 init_table.lazy_plt = &elf_x86_64_lazy_bnd_plt;
5156 init_table.non_lazy_plt = &elf_x86_64_non_lazy_bnd_plt;
ee2fdd6f
L
5157 }
5158 else
5159 {
1de031c8
L
5160 init_table.lazy_plt = &elf_x86_64_lazy_plt;
5161 init_table.non_lazy_plt = &elf_x86_64_non_lazy_plt;
ee2fdd6f 5162 }
38b12349 5163
a6798bab 5164 if (ABI_64_P (info->output_bfd))
38b12349 5165 {
1de031c8
L
5166 init_table.lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
5167 init_table.non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
38b12349
L
5168 }
5169 else
5170 {
1de031c8
L
5171 init_table.lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
5172 init_table.non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
38b12349 5173 }
38b12349
L
5174 }
5175 else
5176 {
1de031c8
L
5177 init_table.lazy_plt = &elf_x86_64_nacl_plt;
5178 init_table.non_lazy_plt = NULL;
5179 init_table.lazy_ibt_plt = NULL;
5180 init_table.non_lazy_ibt_plt = NULL;
38b12349
L
5181 }
5182
7a382c1c
L
5183 if (ABI_64_P (info->output_bfd))
5184 {
1de031c8
L
5185 init_table.r_info = elf64_r_info;
5186 init_table.r_sym = elf64_r_sym;
7a382c1c
L
5187 }
5188 else
5189 {
1de031c8
L
5190 init_table.r_info = elf32_r_info;
5191 init_table.r_sym = elf32_r_sym;
7a382c1c
L
5192 }
5193
1de031c8 5194 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
38b12349
L
5195}
5196
9a2e389a 5197static const struct bfd_elf_special_section
46bed679 5198elf_x86_64_special_sections[]=
3b22753a 5199{
0112cd26
NC
5200 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5201 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5202 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
07d6d2b8 5203 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
0112cd26
NC
5204 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5205 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
07d6d2b8 5206 { NULL, 0, 0, 0, 0 }
3b22753a
L
5207};
5208
6d00b590 5209#define TARGET_LITTLE_SYM x86_64_elf64_vec
70256ad8
AJ
5210#define TARGET_LITTLE_NAME "elf64-x86-64"
5211#define ELF_ARCH bfd_arch_i386
ae95ffa6 5212#define ELF_TARGET_ID X86_64_ELF_DATA
70256ad8 5213#define ELF_MACHINE_CODE EM_X86_64
f6aec96d
L
5214#if DEFAULT_LD_Z_SEPARATE_CODE
5215# define ELF_MAXPAGESIZE 0x1000
5216#else
5217# define ELF_MAXPAGESIZE 0x200000
5218#endif
2043964e 5219#define ELF_MINPAGESIZE 0x1000
24718e3b 5220#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
5221
5222#define elf_backend_can_gc_sections 1
51b64d56 5223#define elf_backend_can_refcount 1
70256ad8
AJ
5224#define elf_backend_want_got_plt 1
5225#define elf_backend_plt_readonly 1
5226#define elf_backend_want_plt_sym 0
5227#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 5228#define elf_backend_rela_normal 1
07d6d2b8 5229#define elf_backend_plt_alignment 4
f7483970 5230#define elf_backend_extern_protected_data 1
bedfd056 5231#define elf_backend_caches_rawsize 1
64f52338 5232#define elf_backend_dtrel_excludes_plt 1
5474d94f 5233#define elf_backend_want_dynrelro 1
70256ad8 5234
351f65ca 5235#define elf_info_to_howto elf_x86_64_info_to_howto
70256ad8 5236
351f65ca 5237#define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
157090f7 5238#define bfd_elf64_bfd_reloc_name_lookup \
351f65ca 5239 elf_x86_64_reloc_name_lookup
70256ad8 5240
c543bf9a 5241#define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
351f65ca 5242#define elf_backend_check_relocs elf_x86_64_check_relocs
38b12349 5243#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
351f65ca
L
5244#define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5245#define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
233cc9c1 5246#define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
351f65ca
L
5247#define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5248#define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
8fd79e71
L
5249#ifdef CORE_HEADER
5250#define elf_backend_write_core_note elf_x86_64_write_core_note
5251#endif
351f65ca
L
5252#define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5253#define elf_backend_relocate_section elf_x86_64_relocate_section
74541ad4 5254#define elf_backend_init_index_section _bfd_elf_init_1_index_section
407443a3 5255#define elf_backend_object_p elf64_x86_64_elf_object_p
0ff2b86e 5256#define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
8d88c4ca 5257
d2b2c203 5258#define elf_backend_section_from_shdr \
351f65ca 5259 elf_x86_64_section_from_shdr
d2b2c203 5260
3b22753a 5261#define elf_backend_section_from_bfd_section \
351f65ca 5262 elf_x86_64_elf_section_from_bfd_section
3b22753a 5263#define elf_backend_add_symbol_hook \
351f65ca 5264 elf_x86_64_add_symbol_hook
3b22753a 5265#define elf_backend_symbol_processing \
351f65ca 5266 elf_x86_64_symbol_processing
3b22753a 5267#define elf_backend_common_section_index \
351f65ca 5268 elf_x86_64_common_section_index
3b22753a 5269#define elf_backend_common_section \
351f65ca 5270 elf_x86_64_common_section
3b22753a 5271#define elf_backend_common_definition \
351f65ca 5272 elf_x86_64_common_definition
3b22753a 5273#define elf_backend_merge_symbol \
351f65ca 5274 elf_x86_64_merge_symbol
3b22753a 5275#define elf_backend_special_sections \
351f65ca 5276 elf_x86_64_special_sections
3b22753a 5277#define elf_backend_additional_program_headers \
351f65ca 5278 elf_x86_64_additional_program_headers
38b12349 5279#define elf_backend_setup_gnu_properties \
9f857535
L
5280 elf_x86_64_link_setup_gnu_properties
5281#define elf_backend_hide_symbol \
5282 _bfd_x86_elf_hide_symbol
3b22753a 5283
8f56f7a2
L
5284#undef elf64_bed
5285#define elf64_bed elf64_x86_64_bed
5286
8d88c4ca 5287#include "elf64-target.h"
9d7cbccd 5288
6036f486
ES
5289/* CloudABI support. */
5290
07d6d2b8 5291#undef TARGET_LITTLE_SYM
6036f486 5292#define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
07d6d2b8 5293#undef TARGET_LITTLE_NAME
6036f486
ES
5294#define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
5295
5296#undef ELF_OSABI
5297#define ELF_OSABI ELFOSABI_CLOUDABI
5298
07d6d2b8 5299#undef elf64_bed
6036f486
ES
5300#define elf64_bed elf64_x86_64_cloudabi_bed
5301
5302#include "elf64-target.h"
5303
9d7cbccd
NC
5304/* FreeBSD support. */
5305
07d6d2b8 5306#undef TARGET_LITTLE_SYM
6d00b590 5307#define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
07d6d2b8 5308#undef TARGET_LITTLE_NAME
9d7cbccd
NC
5309#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5310
d1036acb
L
5311#undef ELF_OSABI
5312#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd 5313
07d6d2b8 5314#undef elf64_bed
9d7cbccd
NC
5315#define elf64_bed elf64_x86_64_fbsd_bed
5316
5317#include "elf64-target.h"
8a9036a4 5318
a6cc6b3b
RO
5319/* Solaris 2 support. */
5320
5321#undef TARGET_LITTLE_SYM
6d00b590 5322#define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
a6cc6b3b
RO
5323#undef TARGET_LITTLE_NAME
5324#define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5325
90c14f0c
L
5326#undef ELF_TARGET_OS
5327#define ELF_TARGET_OS is_solaris
3b4c3844 5328
a6cc6b3b
RO
5329/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5330 objects won't be recognized. */
5331#undef ELF_OSABI
5332
5333#undef elf64_bed
5334#define elf64_bed elf64_x86_64_sol2_bed
5335
7dc98aea
RO
5336/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5337 boundary. */
84865015 5338#undef elf_backend_static_tls_alignment
7dc98aea
RO
5339#define elf_backend_static_tls_alignment 16
5340
a6cc6b3b
RO
5341/* The Solaris 2 ABI requires a plt symbol on all platforms.
5342
5343 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5344 File, p.63. */
84865015 5345#undef elf_backend_want_plt_sym
a6cc6b3b
RO
5346#define elf_backend_want_plt_sym 1
5347
84865015
NC
5348#undef elf_backend_strtab_flags
5349#define elf_backend_strtab_flags SHF_STRINGS
5350
5351static bfd_boolean
5522f910
NC
5352elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
5353 bfd *obfd ATTRIBUTE_UNUSED,
5354 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
5355 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
84865015
NC
5356{
5357 /* PR 19938: FIXME: Need to add code for setting the sh_info
5358 and sh_link fields of Solaris specific section types. */
5359 return FALSE;
5360}
5361
5522f910
NC
5362#undef elf_backend_copy_special_section_fields
5363#define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
84865015 5364
a6cc6b3b
RO
5365#include "elf64-target.h"
5366
8059fb19
RM
5367/* Native Client support. */
5368
64b384e1
RM
5369static bfd_boolean
5370elf64_x86_64_nacl_elf_object_p (bfd *abfd)
5371{
5372 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
5373 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
5374 return TRUE;
5375}
5376
8059fb19 5377#undef TARGET_LITTLE_SYM
6d00b590 5378#define TARGET_LITTLE_SYM x86_64_elf64_nacl_vec
8059fb19
RM
5379#undef TARGET_LITTLE_NAME
5380#define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
5381#undef elf64_bed
5382#define elf64_bed elf64_x86_64_nacl_bed
5383
5384#undef ELF_MAXPAGESIZE
5385#undef ELF_MINPAGESIZE
5386#undef ELF_COMMONPAGESIZE
5387#define ELF_MAXPAGESIZE 0x10000
5388#define ELF_MINPAGESIZE 0x10000
5389#define ELF_COMMONPAGESIZE 0x10000
5390
5391/* Restore defaults. */
5392#undef ELF_OSABI
5393#undef elf_backend_static_tls_alignment
5394#undef elf_backend_want_plt_sym
5395#define elf_backend_want_plt_sym 0
84865015 5396#undef elf_backend_strtab_flags
5522f910 5397#undef elf_backend_copy_special_section_fields
8059fb19
RM
5398
5399/* NaCl uses substantially different PLT entries for the same effects. */
5400
5401#undef elf_backend_plt_alignment
5402#define elf_backend_plt_alignment 5
5403#define NACL_PLT_ENTRY_SIZE 64
5404#define NACLMASK 0xe0 /* 32-byte alignment mask. */
5405
5406static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
5407 {
07d6d2b8 5408 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
8059fb19 5409 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
07d6d2b8
AM
5410 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5411 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5412 0x41, 0xff, 0xe3, /* jmpq *%r11 */
8059fb19 5413
ea2d813e 5414 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
70cc877f 5415 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
ea2d813e
RM
5416
5417 /* 32 bytes of nop to pad out to the standard size. */
3ddf1bdd 5418 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19 5419 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
3ddf1bdd 5420 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19 5421 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
07d6d2b8
AM
5422 0x66, /* excess data16 prefix */
5423 0x90 /* nop */
8059fb19
RM
5424 };
5425
5426static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5427 {
5428 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
07d6d2b8
AM
5429 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5430 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5431 0x41, 0xff, 0xe3, /* jmpq *%r11 */
8059fb19
RM
5432
5433 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
3ddf1bdd 5434 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19
RM
5435 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5436
5437 /* Lazy GOT entries point here (32-byte aligned). */
07d6d2b8
AM
5438 0x68, /* pushq immediate */
5439 0, 0, 0, 0, /* replaced with index into relocation table. */
5440 0xe9, /* jmp relative */
5441 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
8059fb19 5442
07d6d2b8 5443 /* 22 bytes of nop to pad out to the standard size. */
3ddf1bdd 5444 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19 5445 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
07d6d2b8 5446 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
8059fb19
RM
5447 };
5448
5449/* .eh_frame covering the .plt section. */
5450
5451static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
5452 {
07d6d2b8
AM
5453#if (PLT_CIE_LENGTH != 20 \
5454 || PLT_FDE_LENGTH != 36 \
5455 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
8059fb19 5456 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
90c14f0c 5457# error "Need PLT_CIE_LENGTH parameters for eh_frame_plt offsets!"
8059fb19
RM
5458#endif
5459 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5460 0, 0, 0, 0, /* CIE ID */
5461 1, /* CIE version */
07d6d2b8 5462 'z', 'R', 0, /* Augmentation string */
8059fb19 5463 1, /* Code alignment factor */
07d6d2b8 5464 0x78, /* Data alignment factor */
8059fb19
RM
5465 16, /* Return address column */
5466 1, /* Augmentation size */
5467 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5468 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
5469 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
5470 DW_CFA_nop, DW_CFA_nop,
5471
5472 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5473 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
5474 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
5475 0, 0, 0, 0, /* .plt size goes here */
5476 0, /* Augmentation size */
5477 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
5478 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5479 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
5480 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5481 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5482 13, /* Block length */
5483 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
5484 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
5485 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5486 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
5487 DW_CFA_nop, DW_CFA_nop
5488 };
5489
765e526c 5490static const struct elf_x86_lazy_plt_layout elf_x86_64_nacl_plt =
8059fb19 5491 {
07d6d2b8
AM
5492 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
5493 NACL_PLT_ENTRY_SIZE, /* plt0_entry_size */
5494 elf_x86_64_nacl_plt_entry, /* plt_entry */
5495 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
5496 elf_x86_64_nacl_plt0_entry, /* plt_tlsdesc_entry */
5497 NACL_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
5498 2, /* plt_tlsdesc_got1_offset */
5499 9, /* plt_tlsdesc_got2_offset */
5500 6, /* plt_tlsdesc_got1_insn_end */
5501 13, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
5502 2, /* plt0_got1_offset */
5503 9, /* plt0_got2_offset */
5504 13, /* plt0_got2_insn_end */
5505 3, /* plt_got_offset */
5506 33, /* plt_reloc_offset */
5507 38, /* plt_plt_offset */
5508 7, /* plt_got_insn_size */
5509 42, /* plt_plt_insn_end */
5510 32, /* plt_lazy_offset */
5511 elf_x86_64_nacl_plt0_entry, /* pic_plt0_entry */
5512 elf_x86_64_nacl_plt_entry, /* pic_plt_entry */
5513 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
38b12349
L
5514 sizeof (elf_x86_64_nacl_eh_frame_plt) /* eh_frame_plt_size */
5515 };
5516
90c14f0c
L
5517#undef ELF_TARGET_OS
5518#define ELF_TARGET_OS is_nacl
8059fb19 5519
64b384e1
RM
5520#undef elf_backend_object_p
5521#define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
5a68afcf
RM
5522#undef elf_backend_modify_segment_map
5523#define elf_backend_modify_segment_map nacl_modify_segment_map
6d6c25c8
AM
5524#undef elf_backend_modify_headers
5525#define elf_backend_modify_headers nacl_modify_headers
887badb3
RM
5526#undef elf_backend_final_write_processing
5527#define elf_backend_final_write_processing nacl_final_write_processing
5a68afcf 5528
8059fb19
RM
5529#include "elf64-target.h"
5530
5531/* Native Client x32 support. */
5532
64b384e1
RM
5533static bfd_boolean
5534elf32_x86_64_nacl_elf_object_p (bfd *abfd)
5535{
5536 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
5537 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
5538 return TRUE;
5539}
5540
07d6d2b8 5541#undef TARGET_LITTLE_SYM
6d00b590 5542#define TARGET_LITTLE_SYM x86_64_elf32_nacl_vec
07d6d2b8 5543#undef TARGET_LITTLE_NAME
8059fb19
RM
5544#define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
5545#undef elf32_bed
5546#define elf32_bed elf32_x86_64_nacl_bed
5547
8059fb19
RM
5548#define bfd_elf32_bfd_reloc_type_lookup \
5549 elf_x86_64_reloc_type_lookup
5550#define bfd_elf32_bfd_reloc_name_lookup \
5551 elf_x86_64_reloc_name_lookup
b7365e5d
L
5552#define bfd_elf32_get_synthetic_symtab \
5553 elf_x86_64_get_synthetic_symtab
8059fb19
RM
5554
5555#undef elf_backend_object_p
5556#define elf_backend_object_p \
64b384e1 5557 elf32_x86_64_nacl_elf_object_p
8059fb19
RM
5558
5559#undef elf_backend_bfd_from_remote_memory
5560#define elf_backend_bfd_from_remote_memory \
5561 _bfd_elf32_bfd_from_remote_memory
5562
5563#undef elf_backend_size_info
5564#define elf_backend_size_info \
5565 _bfd_elf32_size_info
5566
8f56f7a2
L
5567#undef elf32_bed
5568#define elf32_bed elf32_x86_64_bed
5569
8059fb19
RM
5570#include "elf32-target.h"
5571
5572/* Restore defaults. */
5a68afcf 5573#undef elf_backend_object_p
8059fb19 5574#define elf_backend_object_p elf64_x86_64_elf_object_p
5a68afcf
RM
5575#undef elf_backend_bfd_from_remote_memory
5576#undef elf_backend_size_info
5577#undef elf_backend_modify_segment_map
6d6c25c8 5578#undef elf_backend_modify_headers
887badb3 5579#undef elf_backend_final_write_processing
8059fb19 5580
8a9036a4
L
5581/* Intel L1OM support. */
5582
5583static bfd_boolean
5584elf64_l1om_elf_object_p (bfd *abfd)
5585{
5586 /* Set the right machine number for an L1OM elf64 file. */
5587 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
5588 return TRUE;
5589}
5590
5591#undef TARGET_LITTLE_SYM
6d00b590 5592#define TARGET_LITTLE_SYM l1om_elf64_vec
8a9036a4
L
5593#undef TARGET_LITTLE_NAME
5594#define TARGET_LITTLE_NAME "elf64-l1om"
5595#undef ELF_ARCH
5596#define ELF_ARCH bfd_arch_l1om
5597
5598#undef ELF_MACHINE_CODE
5599#define ELF_MACHINE_CODE EM_L1OM
5600
5601#undef ELF_OSABI
5602
5603#undef elf64_bed
5604#define elf64_bed elf64_l1om_bed
5605
5606#undef elf_backend_object_p
5607#define elf_backend_object_p elf64_l1om_elf_object_p
5608
8059fb19
RM
5609/* Restore defaults. */
5610#undef ELF_MAXPAGESIZE
5611#undef ELF_MINPAGESIZE
5612#undef ELF_COMMONPAGESIZE
f6aec96d
L
5613#if DEFAULT_LD_Z_SEPARATE_CODE
5614# define ELF_MAXPAGESIZE 0x1000
5615#else
5616# define ELF_MAXPAGESIZE 0x200000
5617#endif
8059fb19
RM
5618#define ELF_MINPAGESIZE 0x1000
5619#define ELF_COMMONPAGESIZE 0x1000
5620#undef elf_backend_plt_alignment
5621#define elf_backend_plt_alignment 4
90c14f0c 5622#undef ELF_TARGET_OS
1a0c107f 5623
8a9036a4
L
5624#include "elf64-target.h"
5625
5626/* FreeBSD L1OM support. */
5627
5628#undef TARGET_LITTLE_SYM
6d00b590 5629#define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
8a9036a4
L
5630#undef TARGET_LITTLE_NAME
5631#define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
5632
5633#undef ELF_OSABI
5634#define ELF_OSABI ELFOSABI_FREEBSD
5635
5636#undef elf64_bed
5637#define elf64_bed elf64_l1om_fbsd_bed
5638
8a9036a4 5639#include "elf64-target.h"
351f65ca 5640
7a9068fe
L
5641/* Intel K1OM support. */
5642
5643static bfd_boolean
5644elf64_k1om_elf_object_p (bfd *abfd)
5645{
5646 /* Set the right machine number for an K1OM elf64 file. */
5647 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
5648 return TRUE;
5649}
5650
5651#undef TARGET_LITTLE_SYM
6d00b590 5652#define TARGET_LITTLE_SYM k1om_elf64_vec
7a9068fe
L
5653#undef TARGET_LITTLE_NAME
5654#define TARGET_LITTLE_NAME "elf64-k1om"
5655#undef ELF_ARCH
5656#define ELF_ARCH bfd_arch_k1om
5657
5658#undef ELF_MACHINE_CODE
5659#define ELF_MACHINE_CODE EM_K1OM
5660
5661#undef ELF_OSABI
5662
5663#undef elf64_bed
5664#define elf64_bed elf64_k1om_bed
5665
5666#undef elf_backend_object_p
5667#define elf_backend_object_p elf64_k1om_elf_object_p
5668
5669#undef elf_backend_static_tls_alignment
5670
5671#undef elf_backend_want_plt_sym
5672#define elf_backend_want_plt_sym 0
5673
5674#include "elf64-target.h"
5675
5676/* FreeBSD K1OM support. */
5677
5678#undef TARGET_LITTLE_SYM
6d00b590 5679#define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
7a9068fe
L
5680#undef TARGET_LITTLE_NAME
5681#define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
5682
5683#undef ELF_OSABI
5684#define ELF_OSABI ELFOSABI_FREEBSD
5685
5686#undef elf64_bed
5687#define elf64_bed elf64_k1om_fbsd_bed
5688
5689#include "elf64-target.h"
5690
351f65ca
L
5691/* 32bit x86-64 support. */
5692
351f65ca 5693#undef TARGET_LITTLE_SYM
6d00b590 5694#define TARGET_LITTLE_SYM x86_64_elf32_vec
351f65ca
L
5695#undef TARGET_LITTLE_NAME
5696#define TARGET_LITTLE_NAME "elf32-x86-64"
8059fb19 5697#undef elf32_bed
351f65ca
L
5698
5699#undef ELF_ARCH
5700#define ELF_ARCH bfd_arch_i386
5701
5702#undef ELF_MACHINE_CODE
5703#define ELF_MACHINE_CODE EM_X86_64
5704
351f65ca
L
5705#undef ELF_OSABI
5706
351f65ca
L
5707#undef elf_backend_object_p
5708#define elf_backend_object_p \
5709 elf32_x86_64_elf_object_p
5710
5711#undef elf_backend_bfd_from_remote_memory
5712#define elf_backend_bfd_from_remote_memory \
5713 _bfd_elf32_bfd_from_remote_memory
5714
5715#undef elf_backend_size_info
5716#define elf_backend_size_info \
5717 _bfd_elf32_size_info
5718
5719#include "elf32-target.h"
This page took 1.839755 seconds and 4 git commands to generate.