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