Automatic date update in version.in
[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. */
1540 if ((relocx || opcode == 0x8b)
1541 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info,
0afcef53 1542 X86_64_ELF_DATA,
c175a657 1543 TRUE,
0afcef53 1544 elf_x86_hash_entry (h)))
c175a657
L
1545 {
1546 if (opcode == 0xff)
1547 {
1548 /* Skip for branch instructions since R_X86_64_PC32
1549 may overflow. */
8b43e456 1550 if (no_overflow)
c175a657
L
1551 return TRUE;
1552 }
1553 else if (relocx)
1554 {
1555 /* For non-branch instructions, we can convert to
1556 R_X86_64_32/R_X86_64_32S since we know if there
1557 is a REX byte. */
1558 to_reloc_pc32 = FALSE;
1559 }
1560
1561 /* Since we don't know the current PC when PIC is true,
1562 we can't convert to R_X86_64_PC32. */
1563 if (to_reloc_pc32 && is_pic)
1564 return TRUE;
1565
1566 goto convert;
1567 }
1568 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1569 ld.so may use its link-time address. */
cbd0eecf
L
1570 else if (h->start_stop
1571 || ((h->def_regular
1572 || h->root.type == bfd_link_hash_defined
1573 || h->root.type == bfd_link_hash_defweak)
1574 && h != htab->elf.hdynamic
6999821f 1575 && SYMBOL_REFERENCES_LOCAL_P (link_info, h)))
c175a657
L
1576 {
1577 /* bfd_link_hash_new or bfd_link_hash_undefined is
1578 set by an assignment in a linker script in
cbd0eecf
L
1579 bfd_elf_record_link_assignment. start_stop is set
1580 on __start_SECNAME/__stop_SECNAME which mark section
1581 SECNAME. */
1582 if (h->start_stop
1583 || (h->def_regular
1584 && (h->root.type == bfd_link_hash_new
1585 || h->root.type == bfd_link_hash_undefined
1586 || ((h->root.type == bfd_link_hash_defined
1587 || h->root.type == bfd_link_hash_defweak)
1588 && h->root.u.def.section == bfd_und_section_ptr))))
c175a657
L
1589 {
1590 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
8b43e456 1591 if (no_overflow)
c175a657
L
1592 return TRUE;
1593 goto convert;
1594 }
1595 tsec = h->root.u.def.section;
c175a657
L
1596 }
1597 else
1598 return TRUE;
1599 }
1600
2168b268
L
1601 /* Don't convert GOTPCREL relocation against large section. */
1602 if (elf_section_data (tsec) != NULL
1603 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1604 return TRUE;
1605
8b43e456
L
1606 /* Skip since R_X86_64_PC32/R_X86_64_32/R_X86_64_32S may overflow. */
1607 if (no_overflow)
1608 return TRUE;
c175a657 1609
dc089072 1610convert:
c175a657
L
1611 if (opcode == 0xff)
1612 {
1613 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1614 unsigned int nop;
1615 unsigned int disp;
1616 bfd_vma nop_offset;
1617
1618 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1619 R_X86_64_PC32. */
1620 modrm = bfd_get_8 (abfd, contents + roff - 1);
1621 if (modrm == 0x25)
1622 {
1623 /* Convert to "jmp foo nop". */
1624 modrm = 0xe9;
1625 nop = NOP_OPCODE;
1626 nop_offset = irel->r_offset + 3;
1627 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1628 irel->r_offset -= 1;
1629 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1630 }
1631 else
1632 {
0afcef53
L
1633 struct elf_x86_link_hash_entry *eh
1634 = (struct elf_x86_link_hash_entry *) h;
e2cbcd91 1635
c175a657
L
1636 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1637 is a nop prefix. */
1638 modrm = 0xe8;
e2cbcd91
L
1639 /* To support TLS optimization, always use addr32 prefix for
1640 "call *__tls_get_addr@GOTPCREL(%rip)". */
1d4af308 1641 if (eh && eh->tls_get_addr)
c175a657 1642 {
e2cbcd91
L
1643 nop = 0x67;
1644 nop_offset = irel->r_offset - 2;
c175a657
L
1645 }
1646 else
e2cbcd91
L
1647 {
1648 nop = link_info->call_nop_byte;
1649 if (link_info->call_nop_as_suffix)
1650 {
1651 nop_offset = irel->r_offset + 3;
1652 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1653 irel->r_offset -= 1;
1654 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1655 }
1656 else
1657 nop_offset = irel->r_offset - 2;
1658 }
c175a657
L
1659 }
1660 bfd_put_8 (abfd, nop, contents + nop_offset);
1661 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1662 r_type = R_X86_64_PC32;
1663 }
1664 else
1665 {
1666 unsigned int rex;
1667 unsigned int rex_mask = REX_R;
1668
1669 if (r_type == R_X86_64_REX_GOTPCRELX)
1670 rex = bfd_get_8 (abfd, contents + roff - 3);
1671 else
1672 rex = 0;
1673
1674 if (opcode == 0x8b)
1675 {
1676 if (to_reloc_pc32)
1677 {
1678 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1679 "lea foo(%rip), %reg". */
1680 opcode = 0x8d;
1681 r_type = R_X86_64_PC32;
1682 }
1683 else
1684 {
1685 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1686 "mov $foo, %reg". */
1687 opcode = 0xc7;
1688 modrm = bfd_get_8 (abfd, contents + roff - 1);
1689 modrm = 0xc0 | (modrm & 0x38) >> 3;
1690 if ((rex & REX_W) != 0
1691 && ABI_64_P (link_info->output_bfd))
1692 {
1693 /* Keep the REX_W bit in REX byte for LP64. */
1694 r_type = R_X86_64_32S;
1695 goto rewrite_modrm_rex;
1696 }
1697 else
1698 {
1699 /* If the REX_W bit in REX byte isn't needed,
1700 use R_X86_64_32 and clear the W bit to avoid
1701 sign-extend imm32 to imm64. */
1702 r_type = R_X86_64_32;
1703 /* Clear the W bit in REX byte. */
1704 rex_mask |= REX_W;
1705 goto rewrite_modrm_rex;
1706 }
1707 }
1708 }
1709 else
1710 {
1711 /* R_X86_64_PC32 isn't supported. */
1712 if (to_reloc_pc32)
1713 return TRUE;
1714
1715 modrm = bfd_get_8 (abfd, contents + roff - 1);
1716 if (opcode == 0x85)
1717 {
1718 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1719 "test $foo, %reg". */
1720 modrm = 0xc0 | (modrm & 0x38) >> 3;
1721 opcode = 0xf7;
1722 }
1723 else
1724 {
1725 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1726 "binop $foo, %reg". */
1727 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1728 opcode = 0x81;
1729 }
1730
1731 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1732 overflow when sign-extending imm32 to imm64. */
1733 r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
1734
1735rewrite_modrm_rex:
1736 bfd_put_8 (abfd, modrm, contents + roff - 1);
1737
1738 if (rex)
1739 {
1740 /* Move the R bit to the B bit in REX byte. */
1741 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
1742 bfd_put_8 (abfd, rex, contents + roff - 3);
1743 }
1744
1745 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
1746 irel->r_addend = 0;
1747 }
1748
1749 bfd_put_8 (abfd, opcode, contents + roff - 2);
1750 }
1751
6999821f 1752 *r_type_p = r_type;
78984959
L
1753 irel->r_info = htab->r_info (r_symndx,
1754 r_type | R_X86_64_converted_reloc_bit);
c175a657
L
1755
1756 *converted = TRUE;
1757
1758 return TRUE;
1759}
1760
70256ad8 1761/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
1762 calculate needed space in the global offset table, procedure
1763 linkage table, and dynamic reloc sections. */
70256ad8 1764
b34976b6 1765static bfd_boolean
351f65ca
L
1766elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1767 asection *sec,
1768 const Elf_Internal_Rela *relocs)
70256ad8 1769{
0afcef53 1770 struct elf_x86_link_hash_table *htab;
70256ad8
AJ
1771 Elf_Internal_Shdr *symtab_hdr;
1772 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
1773 const Elf_Internal_Rela *rel;
1774 const Elf_Internal_Rela *rel_end;
70256ad8 1775 asection *sreloc;
bedfd056 1776 bfd_byte *contents;
70256ad8 1777
0e1862bb 1778 if (bfd_link_relocatable (info))
b34976b6 1779 return TRUE;
70256ad8 1780
081b1afe
L
1781 /* Don't do anything special with non-loaded, non-alloced sections.
1782 In particular, any relocs in such sections should not affect GOT
1783 and PLT reference counting (ie. we don't allow them to create GOT
1784 or PLT entries), there's no possibility or desire to optimize TLS
1785 relocs, and there's not much point in propagating relocs to shared
1786 libs that the dynamic linker won't relocate. */
1787 if ((sec->flags & SEC_ALLOC) == 0)
1788 return TRUE;
1789
0afcef53 1790 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6 1791 if (htab == NULL)
afd9acee
L
1792 {
1793 sec->check_relocs_failed = 1;
1794 return FALSE;
1795 }
4dfe6ac6 1796
fe53b4a4
L
1797 BFD_ASSERT (is_x86_elf (abfd, htab));
1798
bedfd056
L
1799 /* Get the section contents. */
1800 if (elf_section_data (sec)->this_hdr.contents != NULL)
1801 contents = elf_section_data (sec)->this_hdr.contents;
1802 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1803 {
1804 sec->check_relocs_failed = 1;
1805 return FALSE;
1806 }
1807
0ffa91dd 1808 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8 1809 sym_hashes = elf_sym_hashes (abfd);
70256ad8 1810
c434dee6 1811 sreloc = NULL;
cbe950e9 1812
70256ad8
AJ
1813 rel_end = relocs + sec->reloc_count;
1814 for (rel = relocs; rel < rel_end; rel++)
1815 {
bffbf940 1816 unsigned int r_type;
d42c267e 1817 unsigned int r_symndx;
70256ad8 1818 struct elf_link_hash_entry *h;
0afcef53 1819 struct elf_x86_link_hash_entry *eh;
4c544807
L
1820 Elf_Internal_Sym *isym;
1821 const char *name;
06a6a421 1822 bfd_boolean size_reloc;
6999821f 1823 bfd_boolean converted_reloc;
70256ad8 1824
351f65ca
L
1825 r_symndx = htab->r_sym (rel->r_info);
1826 r_type = ELF32_R_TYPE (rel->r_info);
c434dee6
AJ
1827
1828 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1829 {
695344c0 1830 /* xgettext:c-format */
4eca0228
AM
1831 _bfd_error_handler (_("%B: bad symbol index: %d"),
1832 abfd, r_symndx);
afd9acee 1833 goto error_return;
c434dee6
AJ
1834 }
1835
70256ad8 1836 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
1837 {
1838 /* A local symbol. */
c2e61a4e
L
1839 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1840 abfd, r_symndx);
1841 if (isym == NULL)
afd9acee 1842 goto error_return;
c25bc9fc
L
1843
1844 /* Check relocation against local STT_GNU_IFUNC symbol. */
351f65ca 1845 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c25bc9fc 1846 {
0afcef53
L
1847 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
1848 TRUE);
c25bc9fc 1849 if (h == NULL)
afd9acee 1850 goto error_return;
6bbec505 1851
c25bc9fc 1852 /* Fake a STT_GNU_IFUNC symbol. */
6322e5c5
L
1853 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1854 isym, NULL);
c25bc9fc
L
1855 h->type = STT_GNU_IFUNC;
1856 h->def_regular = 1;
1857 h->ref_regular = 1;
1858 h->forced_local = 1;
1859 h->root.type = bfd_link_hash_defined;
1860 }
1861 else
1862 h = NULL;
1863 }
70256ad8 1864 else
71cb9464 1865 {
4c544807 1866 isym = NULL;
71cb9464
L
1867 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1868 while (h->root.type == bfd_link_hash_indirect
1869 || h->root.type == bfd_link_hash_warning)
1870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 1871 }
cbe950e9 1872
d1534d16
L
1873 /* Check invalid x32 relocations. */
1874 if (!ABI_64_P (abfd))
1875 switch (r_type)
1876 {
1877 default:
1878 break;
1879
d1534d16
L
1880 case R_X86_64_DTPOFF64:
1881 case R_X86_64_TPOFF64:
1882 case R_X86_64_PC64:
1883 case R_X86_64_GOTOFF64:
1884 case R_X86_64_GOT64:
1885 case R_X86_64_GOTPCREL64:
1886 case R_X86_64_GOTPC64:
1887 case R_X86_64_GOTPLT64:
1888 case R_X86_64_PLTOFF64:
1889 {
1890 if (h)
1891 name = h->root.root.string;
1892 else
1893 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1894 NULL);
4eca0228 1895 _bfd_error_handler
695344c0 1896 /* xgettext:c-format */
d1534d16
L
1897 (_("%B: relocation %s against symbol `%s' isn't "
1898 "supported in x32 mode"), abfd,
1899 x86_64_elf_howto_table[r_type].name, name);
1900 bfd_set_error (bfd_error_bad_value);
afd9acee 1901 goto error_return;
d1534d16
L
1902 }
1903 break;
1904 }
1905
c25bc9fc
L
1906 if (h != NULL)
1907 {
ad1e85de
L
1908 /* It is referenced by a non-shared object. */
1909 h->ref_regular = 1;
bc4e12de 1910 h->root.non_ir_ref_regular = 1;
13a2df29
L
1911
1912 if (h->type == STT_GNU_IFUNC)
1913 elf_tdata (info->output_bfd)->has_gnu_symbols
1914 |= elf_gnu_symbol_ifunc;
71cb9464 1915 }
70256ad8 1916
6999821f
L
1917 converted_reloc = FALSE;
1918 if ((r_type == R_X86_64_GOTPCREL
1919 || r_type == R_X86_64_GOTPCRELX
1920 || r_type == R_X86_64_REX_GOTPCRELX)
1921 && (h == NULL || h->type != STT_GNU_IFUNC))
1922 {
1923 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1924 if (!elf_x86_64_convert_load_reloc (abfd, contents, &r_type,
1925 irel, h, &converted_reloc,
1926 info))
1927 goto error_return;
1928 }
1929
bedfd056 1930 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
351f65ca
L
1931 symtab_hdr, sym_hashes,
1932 &r_type, GOT_UNKNOWN,
bedfd056 1933 rel, rel_end, h, r_symndx, FALSE))
afd9acee 1934 goto error_return;
142411ca 1935
0afcef53 1936 eh = (struct elf_x86_link_hash_entry *) h;
bffbf940 1937 switch (r_type)
70256ad8 1938 {
bffbf940 1939 case R_X86_64_TLSLD:
0afcef53 1940 htab->tls_ld_or_ldm_got.refcount += 1;
bffbf940
JJ
1941 goto create_got;
1942
1943 case R_X86_64_TPOFF32:
0e1862bb 1944 if (!bfd_link_executable (info) && ABI_64_P (abfd))
aab921ad 1945 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
338c190a 1946 &x86_64_elf_howto_table[r_type]);
aec6b87e
L
1947 if (eh != NULL)
1948 eh->has_got_reloc = 1;
bffbf940 1949 break;
c434dee6 1950
bffbf940 1951 case R_X86_64_GOTTPOFF:
0e1862bb 1952 if (!bfd_link_executable (info))
bffbf940
JJ
1953 info->flags |= DF_STATIC_TLS;
1954 /* Fall through */
70256ad8 1955
bffbf940
JJ
1956 case R_X86_64_GOT32:
1957 case R_X86_64_GOTPCREL:
56ceb5b5
L
1958 case R_X86_64_GOTPCRELX:
1959 case R_X86_64_REX_GOTPCRELX:
bffbf940 1960 case R_X86_64_TLSGD:
7b81dfbb
AJ
1961 case R_X86_64_GOT64:
1962 case R_X86_64_GOTPCREL64:
1963 case R_X86_64_GOTPLT64:
67a4f2b7
AO
1964 case R_X86_64_GOTPC32_TLSDESC:
1965 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
1966 /* This symbol requires a global offset table entry. */
1967 {
1968 int tls_type, old_tls_type;
1969
1970 switch (r_type)
1971 {
1972 default: tls_type = GOT_NORMAL; break;
1973 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1974 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
67a4f2b7
AO
1975 case R_X86_64_GOTPC32_TLSDESC:
1976 case R_X86_64_TLSDESC_CALL:
1977 tls_type = GOT_TLS_GDESC; break;
bffbf940
JJ
1978 }
1979
1980 if (h != NULL)
1981 {
1982 h->got.refcount += 1;
aec6b87e 1983 old_tls_type = eh->tls_type;
bffbf940
JJ
1984 }
1985 else
1986 {
1987 bfd_signed_vma *local_got_refcounts;
1988
1989 /* This is a global offset table entry for a local symbol. */
1990 local_got_refcounts = elf_local_got_refcounts (abfd);
1991 if (local_got_refcounts == NULL)
1992 {
1993 bfd_size_type size;
1994
1995 size = symtab_hdr->sh_info;
67a4f2b7
AO
1996 size *= sizeof (bfd_signed_vma)
1997 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
1998 local_got_refcounts = ((bfd_signed_vma *)
1999 bfd_zalloc (abfd, size));
2000 if (local_got_refcounts == NULL)
afd9acee 2001 goto error_return;
bffbf940 2002 elf_local_got_refcounts (abfd) = local_got_refcounts;
0afcef53 2003 elf_x86_local_tlsdesc_gotent (abfd)
67a4f2b7 2004 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
0afcef53 2005 elf_x86_local_got_tls_type (abfd)
67a4f2b7 2006 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940
JJ
2007 }
2008 local_got_refcounts[r_symndx] += 1;
2009 old_tls_type
0afcef53 2010 = elf_x86_local_got_tls_type (abfd) [r_symndx];
bffbf940
JJ
2011 }
2012
2013 /* If a TLS symbol is accessed using IE at least once,
2014 there is no point to use dynamic model for it. */
2015 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
2016 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2017 || tls_type != GOT_TLS_IE))
bffbf940 2018 {
67a4f2b7 2019 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 2020 tls_type = old_tls_type;
67a4f2b7
AO
2021 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2022 && GOT_TLS_GD_ANY_P (tls_type))
2023 tls_type |= old_tls_type;
bffbf940
JJ
2024 else
2025 {
09a24cbf 2026 if (h)
4c544807
L
2027 name = h->root.root.string;
2028 else
2029 name = bfd_elf_sym_name (abfd, symtab_hdr,
2030 isym, NULL);
4eca0228 2031 _bfd_error_handler
695344c0 2032 /* xgettext:c-format */
63a5468a
AM
2033 (_("%B: '%s' accessed both as normal and"
2034 " thread local symbol"),
4c544807 2035 abfd, name);
68c4a57e 2036 bfd_set_error (bfd_error_bad_value);
afd9acee 2037 goto error_return;
bffbf940
JJ
2038 }
2039 }
2040
2041 if (old_tls_type != tls_type)
2042 {
aec6b87e
L
2043 if (eh != NULL)
2044 eh->tls_type = tls_type;
bffbf940 2045 else
0afcef53 2046 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
bffbf940
JJ
2047 }
2048 }
c434dee6
AJ
2049 /* Fall through */
2050
d6ab8113
JB
2051 case R_X86_64_GOTOFF64:
2052 case R_X86_64_GOTPC32:
7b81dfbb 2053 case R_X86_64_GOTPC64:
bffbf940 2054 create_got:
aec6b87e
L
2055 if (eh != NULL)
2056 eh->has_got_reloc = 1;
70256ad8
AJ
2057 break;
2058
2059 case R_X86_64_PLT32:
c3320543 2060 case R_X86_64_PLT32_BND:
70256ad8 2061 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
2062 actually build the entry in adjust_dynamic_symbol,
2063 because this might be a case of linking PIC code which is
2064 never referenced by a dynamic object, in which case we
2065 don't need to generate a procedure linkage table entry
2066 after all. */
70256ad8
AJ
2067
2068 /* If this is a local symbol, we resolve it directly without
407443a3 2069 creating a procedure linkage table entry. */
70256ad8
AJ
2070 if (h == NULL)
2071 continue;
2072
aec6b87e 2073 eh->has_got_reloc = 1;
f5385ebf 2074 h->needs_plt = 1;
51b64d56 2075 h->plt.refcount += 1;
70256ad8
AJ
2076 break;
2077
7b81dfbb
AJ
2078 case R_X86_64_PLTOFF64:
2079 /* This tries to form the 'address' of a function relative
2080 to GOT. For global symbols we need a PLT entry. */
2081 if (h != NULL)
2082 {
2083 h->needs_plt = 1;
2084 h->plt.refcount += 1;
2085 }
2086 goto create_got;
2087
6a3e1bae
L
2088 case R_X86_64_SIZE32:
2089 case R_X86_64_SIZE64:
06a6a421 2090 size_reloc = TRUE;
6a3e1bae
L
2091 goto do_size;
2092
248775ba
L
2093 case R_X86_64_32:
2094 if (!ABI_64_P (abfd))
2095 goto pointer;
1a0670f3 2096 /* Fall through. */
cc78d0af
AJ
2097 case R_X86_64_8:
2098 case R_X86_64_16:
70256ad8 2099 case R_X86_64_32S:
338c190a
L
2100 /* Check relocation overflow as these relocs may lead to
2101 run-time relocation overflow. Don't error out for
1b71fb54 2102 sections we don't care about, such as debug sections or
338c190a 2103 when relocation overflow check is disabled. */
4c10bbaa 2104 if (!info->no_reloc_overflow_check
6999821f 2105 && !converted_reloc
338c190a
L
2106 && (bfd_link_pic (info)
2107 || (bfd_link_executable (info)
2108 && h != NULL
2109 && !h->def_regular
2110 && h->def_dynamic
081b1afe 2111 && (sec->flags & SEC_READONLY) == 0)))
aab921ad 2112 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
338c190a 2113 &x86_64_elf_howto_table[r_type]);
1b71fb54
AJ
2114 /* Fall through. */
2115
c434dee6
AJ
2116 case R_X86_64_PC8:
2117 case R_X86_64_PC16:
70256ad8 2118 case R_X86_64_PC32:
c3320543 2119 case R_X86_64_PC32_BND:
d6ab8113 2120 case R_X86_64_PC64:
1b71fb54 2121 case R_X86_64_64:
248775ba 2122pointer:
aec6b87e
L
2123 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2124 eh->has_non_got_reloc = 1;
bf52d7c7
L
2125 /* We are called after all symbols have been resolved. Only
2126 relocation against STT_GNU_IFUNC symbol must go through
2127 PLT. */
d1ed1c7d
L
2128 if (h != NULL
2129 && (bfd_link_executable (info)
bf52d7c7 2130 || h->type == STT_GNU_IFUNC))
c434dee6
AJ
2131 {
2132 /* If this reloc is in a read-only section, we might
2133 need a copy reloc. We can't check reliably at this
2134 stage whether the section is read-only, as input
2135 sections have not yet been mapped to output sections.
2136 Tentatively set the flag for now, and correct in
2137 adjust_dynamic_symbol. */
f5385ebf 2138 h->non_got_ref = 1;
c434dee6 2139
233cc9c1
L
2140 /* We may need a .plt entry if the symbol is a function
2141 defined in a shared lib or is a STT_GNU_IFUNC function
2142 referenced from the code or read-only section. */
34bb2571
L
2143 if (!h->def_regular
2144 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
233cc9c1
L
2145 h->plt.refcount += 1;
2146
5db4f0d3
L
2147 if (r_type == R_X86_64_PC32)
2148 {
2149 /* Since something like ".long foo - ." may be used
2150 as pointer, make sure that PLT is used if foo is
2151 a function defined in a shared library. */
2152 if ((sec->flags & SEC_CODE) == 0)
2153 h->pointer_equality_needed = 1;
2154 }
2155 else if (r_type != R_X86_64_PC32_BND
2156 && r_type != R_X86_64_PC64)
04ebc307
L
2157 {
2158 h->pointer_equality_needed = 1;
2159 /* At run-time, R_X86_64_64 can be resolved for both
2160 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2161 can only be resolved for x32. */
2162 if ((sec->flags & SEC_READONLY) == 0
2163 && (r_type == R_X86_64_64
2164 || (!ABI_64_P (abfd)
2165 && (r_type == R_X86_64_32
2166 || r_type == R_X86_64_32S))))
aec6b87e 2167 eh->func_pointer_refcount += 1;
04ebc307 2168 }
c434dee6 2169 }
70256ad8 2170
06a6a421 2171 size_reloc = FALSE;
6a3e1bae 2172do_size:
70256ad8
AJ
2173 /* If we are creating a shared library, and this is a reloc
2174 against a global symbol, or a non PC relative reloc
2175 against a local symbol, then we need to copy the reloc
2176 into the shared library. However, if we are linking with
2177 -Bsymbolic, we do not need to copy a reloc against a
2178 global symbol which is defined in an object we are
407443a3 2179 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
2180 this point we have not seen all the input files, so it is
2181 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
2182 later (it is never cleared). In case of a weak definition,
2183 DEF_REGULAR may be cleared later by a strong definition in
2184 a shared library. We account for that possibility below by
2185 storing information in the relocs_copied field of the hash
2186 table entry. A similar situation occurs when creating
2187 shared libraries and symbol visibility changes render the
31c0ebfe 2188 symbol local.
c434dee6
AJ
2189
2190 If on the other hand, we are creating an executable, we
2191 may need to keep relocations for symbols satisfied by a
2192 dynamic library if we manage to avoid copy relocs for the
233cc9c1
L
2193 symbol.
2194
2195 Generate dynamic pointer relocation against STT_GNU_IFUNC
2196 symbol in the non-code section. */
081b1afe
L
2197 if ((bfd_link_pic (info)
2198 && (! IS_X86_64_PCREL_TYPE (r_type)
2199 || (h != NULL
2200 && (! (bfd_link_pie (info)
2201 || SYMBOLIC_BIND (info, h))
2202 || h->root.type == bfd_link_hash_defweak
2203 || !h->def_regular))))
233cc9c1
L
2204 || (h != NULL
2205 && h->type == STT_GNU_IFUNC
2206 && r_type == htab->pointer_r_type
2207 && (sec->flags & SEC_CODE) == 0)
081b1afe
L
2208 || (ELIMINATE_COPY_RELOCS
2209 && !bfd_link_pic (info)
2210 && h != NULL
2211 && (h->root.type == bfd_link_hash_defweak
2212 || !h->def_regular)))
70256ad8 2213 {
e03a8ed8
L
2214 struct elf_dyn_relocs *p;
2215 struct elf_dyn_relocs **head;
c434dee6
AJ
2216
2217 /* We must copy these reloc types into the output file.
2218 Create a reloc section in dynobj and make room for
2219 this reloc. */
70256ad8
AJ
2220 if (sreloc == NULL)
2221 {
83bac4b0 2222 sreloc = _bfd_elf_make_dynamic_reloc_section
82e96e07
L
2223 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
2224 abfd, /*rela?*/ TRUE);
70256ad8 2225
70256ad8 2226 if (sreloc == NULL)
afd9acee 2227 goto error_return;
70256ad8
AJ
2228 }
2229
c434dee6
AJ
2230 /* If this is a global symbol, we count the number of
2231 relocations we need for this symbol. */
2232 if (h != NULL)
aec6b87e 2233 head = &eh->dyn_relocs;
c434dee6
AJ
2234 else
2235 {
2236 /* Track dynamic relocs needed for local syms too.
2237 We really need local syms available to do this
2238 easily. Oh well. */
c434dee6 2239 asection *s;
87d72d41 2240 void **vpp;
87d72d41
AM
2241
2242 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2243 abfd, r_symndx);
2244 if (isym == NULL)
afd9acee 2245 goto error_return;
87d72d41
AM
2246
2247 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
c434dee6 2248 if (s == NULL)
87d72d41 2249 s = sec;
70256ad8 2250
e81d3500
DD
2251 /* Beware of type punned pointers vs strict aliasing
2252 rules. */
2253 vpp = &(elf_section_data (s)->local_dynrel);
e03a8ed8 2254 head = (struct elf_dyn_relocs **)vpp;
c434dee6 2255 }
70256ad8 2256
c434dee6
AJ
2257 p = *head;
2258 if (p == NULL || p->sec != sec)
2259 {
2260 bfd_size_type amt = sizeof *p;
d8045f23 2261
e03a8ed8 2262 p = ((struct elf_dyn_relocs *)
c434dee6 2263 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 2264 if (p == NULL)
afd9acee 2265 goto error_return;
c434dee6
AJ
2266 p->next = *head;
2267 *head = p;
2268 p->sec = sec;
2269 p->count = 0;
2270 p->pc_count = 0;
70256ad8 2271 }
c434dee6
AJ
2272
2273 p->count += 1;
06a6a421
L
2274 /* Count size relocation as PC-relative relocation. */
2275 if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
c434dee6 2276 p->pc_count += 1;
70256ad8
AJ
2277 }
2278 break;
fe4770f4
AJ
2279
2280 /* This relocation describes the C++ object vtable hierarchy.
2281 Reconstruct it for later use during GC. */
2282 case R_X86_64_GNU_VTINHERIT:
c152c796 2283 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
afd9acee 2284 goto error_return;
fe4770f4
AJ
2285 break;
2286
2287 /* This relocation describes which C++ vtable entries are actually
2288 used. Record for later use during GC. */
2289 case R_X86_64_GNU_VTENTRY:
d17e0c6e
JB
2290 BFD_ASSERT (h != NULL);
2291 if (h != NULL
2292 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
afd9acee 2293 goto error_return;
fe4770f4 2294 break;
c434dee6
AJ
2295
2296 default:
2297 break;
70256ad8
AJ
2298 }
2299 }
2300
bedfd056
L
2301 if (elf_section_data (sec)->this_hdr.contents != contents)
2302 {
2303 if (!info->keep_memory)
2304 free (contents);
2305 else
2306 {
2307 /* Cache the section contents for elf_link_input_bfd. */
2308 elf_section_data (sec)->this_hdr.contents = contents;
2309 }
2310 }
2311
b34976b6 2312 return TRUE;
afd9acee
L
2313
2314error_return:
bedfd056
L
2315 if (elf_section_data (sec)->this_hdr.contents != contents)
2316 free (contents);
afd9acee
L
2317 sec->check_relocs_failed = 1;
2318 return FALSE;
70256ad8
AJ
2319}
2320
bffbf940
JJ
2321/* Return the relocation value for @tpoff relocation
2322 if STT_TLS virtual address is ADDRESS. */
2323
2324static bfd_vma
351f65ca 2325elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 2326{
e1918d23 2327 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
2328 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2329 bfd_vma static_tls_size;
bffbf940
JJ
2330
2331 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 2332 if (htab->tls_sec == NULL)
bffbf940 2333 return 0;
7dc98aea
RO
2334
2335 /* Consider special static TLS alignment requirements. */
2336 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2337 return address - static_tls_size - htab->tls_sec->vma;
bffbf940
JJ
2338}
2339
90f487df
L
2340/* Is the instruction before OFFSET in CONTENTS a 32bit relative
2341 branch? */
2342
2343static bfd_boolean
2344is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2345{
2346 /* Opcode Instruction
2347 0xe8 call
2348 0xe9 jump
2349 0x0f 0x8x conditional jump */
2350 return ((offset > 0
2351 && (contents [offset - 1] == 0xe8
2352 || contents [offset - 1] == 0xe9))
2353 || (offset > 1
2354 && contents [offset - 2] == 0x0f
2355 && (contents [offset - 1] & 0xf0) == 0x80));
2356}
2357
8d88c4ca
NC
2358/* Relocate an x86_64 ELF section. */
2359
b34976b6 2360static bfd_boolean
351f65ca
L
2361elf_x86_64_relocate_section (bfd *output_bfd,
2362 struct bfd_link_info *info,
2363 bfd *input_bfd,
2364 asection *input_section,
2365 bfd_byte *contents,
2366 Elf_Internal_Rela *relocs,
2367 Elf_Internal_Sym *local_syms,
2368 asection **local_sections)
8d88c4ca 2369{
0afcef53 2370 struct elf_x86_link_hash_table *htab;
8d88c4ca
NC
2371 Elf_Internal_Shdr *symtab_hdr;
2372 struct elf_link_hash_entry **sym_hashes;
2373 bfd_vma *local_got_offsets;
67a4f2b7 2374 bfd_vma *local_tlsdesc_gotents;
c434dee6 2375 Elf_Internal_Rela *rel;
60f2e42e 2376 Elf_Internal_Rela *wrel;
8d88c4ca 2377 Elf_Internal_Rela *relend;
38b12349 2378 unsigned int plt_entry_size;
8d88c4ca 2379
338c190a
L
2380 /* Skip if check_relocs failed. */
2381 if (input_section->check_relocs_failed)
2382 return FALSE;
2383
0afcef53 2384 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6
NC
2385 if (htab == NULL)
2386 return FALSE;
fe53b4a4
L
2387
2388 BFD_ASSERT (is_x86_elf (input_bfd, htab));
2389
765e526c 2390 plt_entry_size = htab->plt.plt_entry_size;
0ffa91dd 2391 symtab_hdr = &elf_symtab_hdr (input_bfd);
8d88c4ca
NC
2392 sym_hashes = elf_sym_hashes (input_bfd);
2393 local_got_offsets = elf_local_got_offsets (input_bfd);
0afcef53 2394 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
8d88c4ca 2395
0afcef53 2396 _bfd_x86_elf_set_tls_module_base (info);
9f03412a 2397
60f2e42e 2398 rel = wrel = relocs;
8d88c4ca 2399 relend = relocs + input_section->reloc_count;
60f2e42e 2400 for (; rel < relend; wrel++, rel++)
8d88c4ca 2401 {
419414ea 2402 unsigned int r_type, r_type_tls;
8d88c4ca
NC
2403 reloc_howto_type *howto;
2404 unsigned long r_symndx;
2405 struct elf_link_hash_entry *h;
0afcef53 2406 struct elf_x86_link_hash_entry *eh;
8d88c4ca
NC
2407 Elf_Internal_Sym *sym;
2408 asection *sec;
0ff2b86e 2409 bfd_vma off, offplt, plt_offset;
8d88c4ca 2410 bfd_vma relocation;
b34976b6 2411 bfd_boolean unresolved_reloc;
8d88c4ca 2412 bfd_reloc_status_type r;
bffbf940 2413 int tls_type;
0ff2b86e 2414 asection *base_got, *resolved_plt;
1788fc08 2415 bfd_vma st_size;
aec6b87e 2416 bfd_boolean resolved_to_zero;
9e9821dd 2417 bfd_boolean relative_reloc;
78984959 2418 bfd_boolean converted_reloc;
8d88c4ca 2419
351f65ca 2420 r_type = ELF32_R_TYPE (rel->r_info);
fe4770f4
AJ
2421 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2422 || r_type == (int) R_X86_64_GNU_VTENTRY)
18954b29
L
2423 {
2424 if (wrel != rel)
2425 *wrel = *rel;
2426 continue;
2427 }
8d88c4ca 2428
78984959
L
2429 converted_reloc = (r_type & R_X86_64_converted_reloc_bit) != 0;
2430 r_type &= ~R_X86_64_converted_reloc_bit;
2431
9911c0fc 2432 if (r_type >= (int) R_X86_64_standard)
47aeb64c 2433 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
8d88c4ca 2434
d7921315 2435 if (r_type != (int) R_X86_64_32
eed180f8 2436 || ABI_64_P (output_bfd))
d7921315
L
2437 howto = x86_64_elf_howto_table + r_type;
2438 else
2439 howto = (x86_64_elf_howto_table
2440 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
351f65ca 2441 r_symndx = htab->r_sym (rel->r_info);
8d88c4ca
NC
2442 h = NULL;
2443 sym = NULL;
2444 sec = NULL;
b34976b6 2445 unresolved_reloc = FALSE;
8d88c4ca 2446 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
2447 {
2448 sym = local_syms + r_symndx;
2449 sec = local_sections[r_symndx];
c434dee6 2450
c25bc9fc
L
2451 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2452 &sec, rel);
1788fc08 2453 st_size = sym->st_size;
c25bc9fc
L
2454
2455 /* Relocate against local STT_GNU_IFUNC symbol. */
0e1862bb 2456 if (!bfd_link_relocatable (info)
351f65ca 2457 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc 2458 {
0afcef53
L
2459 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
2460 rel, FALSE);
c25bc9fc
L
2461 if (h == NULL)
2462 abort ();
2463
eed180f8 2464 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
2465 h->root.u.def.value = sym->st_value;
2466 h->root.u.def.section = sec;
2467 }
8da6118f 2468 }
8d88c4ca 2469 else
8da6118f 2470 {
c9736ba0 2471 bfd_boolean warned ATTRIBUTE_UNUSED;
62d887d4 2472 bfd_boolean ignored ATTRIBUTE_UNUSED;
c434dee6 2473
b2a8e766
AM
2474 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2475 r_symndx, symtab_hdr, sym_hashes,
2476 h, sec, relocation,
62d887d4 2477 unresolved_reloc, warned, ignored);
1788fc08 2478 st_size = h->size;
8da6118f 2479 }
ab96bf03 2480
dbaa2011 2481 if (sec != NULL && discarded_section (sec))
60f2e42e
L
2482 {
2483 _bfd_clear_contents (howto, input_bfd, input_section,
2484 contents + rel->r_offset);
2485 wrel->r_offset = rel->r_offset;
2486 wrel->r_info = 0;
2487 wrel->r_addend = 0;
2488
2489 /* For ld -r, remove relocations in debug sections against
2490 sections defined in discarded sections. Not done for
2491 eh_frame editing code expects to be present. */
2492 if (bfd_link_relocatable (info)
2493 && (input_section->flags & SEC_DEBUGGING))
2494 wrel--;
2495
2496 continue;
2497 }
ab96bf03 2498
0e1862bb 2499 if (bfd_link_relocatable (info))
2d5da473
L
2500 {
2501 if (wrel != rel)
2502 *wrel = *rel;
2503 continue;
2504 }
ab96bf03 2505
1788fc08 2506 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
64d25c44 2507 {
1788fc08
L
2508 if (r_type == R_X86_64_64)
2509 {
2510 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
2511 zero-extend it to 64bit if addend is zero. */
2512 r_type = R_X86_64_32;
2513 memset (contents + rel->r_offset + 4, 0, 4);
2514 }
2515 else if (r_type == R_X86_64_SIZE64)
2516 {
2517 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
2518 zero-extend it to 64bit if addend is zero. */
2519 r_type = R_X86_64_SIZE32;
2520 memset (contents + rel->r_offset + 4, 0, 4);
2521 }
64d25c44
L
2522 }
2523
0afcef53 2524 eh = (struct elf_x86_link_hash_entry *) h;
0ff2b86e 2525
cbe950e9
L
2526 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2527 it here if it is defined in a non-shared object. */
2528 if (h != NULL
2529 && h->type == STT_GNU_IFUNC
2530 && h->def_regular)
2531 {
cbe950e9 2532 bfd_vma plt_index;
4c544807 2533 const char *name;
cbe950e9 2534
97dc35c8
L
2535 if ((input_section->flags & SEC_ALLOC) == 0)
2536 {
2537 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2538 sections because such sections are not SEC_ALLOC and
2539 thus ld.so will not process them. */
2540 if ((input_section->flags & SEC_DEBUGGING) != 0)
0eace210 2541 continue;
97dc35c8
L
2542 abort ();
2543 }
233cc9c1
L
2544
2545 switch (r_type)
2546 {
2547 default:
2548 break;
2549
2550 case R_X86_64_GOTPCREL:
2551 case R_X86_64_GOTPCRELX:
2552 case R_X86_64_REX_GOTPCRELX:
2553 case R_X86_64_GOTPCREL64:
2554 base_got = htab->elf.sgot;
2555 off = h->got.offset;
2556
2557 if (base_got == NULL)
2558 abort ();
2559
2560 if (off == (bfd_vma) -1)
2561 {
2562 /* We can't use h->got.offset here to save state, or
2563 even just remember the offset, as finish_dynamic_symbol
2564 would use that as offset into .got. */
2565
2566 if (h->plt.offset == (bfd_vma) -1)
2567 abort ();
2568
2569 if (htab->elf.splt != NULL)
2570 {
38b12349 2571 plt_index = (h->plt.offset / plt_entry_size
765e526c 2572 - htab->plt.has_plt0);
233cc9c1
L
2573 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2574 base_got = htab->elf.sgotplt;
2575 }
2576 else
2577 {
2578 plt_index = h->plt.offset / plt_entry_size;
2579 off = plt_index * GOT_ENTRY_SIZE;
2580 base_got = htab->elf.igotplt;
2581 }
2582
2583 if (h->dynindx == -1
2584 || h->forced_local
2585 || info->symbolic)
2586 {
2587 /* This references the local defitionion. We must
2588 initialize this entry in the global offset table.
2589 Since the offset must always be a multiple of 8,
2590 we use the least significant bit to record
2591 whether we have initialized it already.
2592
2593 When doing a dynamic link, we create a .rela.got
2594 relocation entry to initialize the value. This
2595 is done in the finish_dynamic_symbol routine. */
2596 if ((off & 1) != 0)
2597 off &= ~1;
2598 else
2599 {
2600 bfd_put_64 (output_bfd, relocation,
2601 base_got->contents + off);
2602 /* Note that this is harmless for the GOTPLT64
2603 case, as -1 | 1 still is -1. */
2604 h->got.offset |= 1;
2605 }
2606 }
2607 }
2608
2609 relocation = (base_got->output_section->vma
2610 + base_got->output_offset + off);
2611
2612 goto do_relocation;
2613 }
2614
2615 if (h->plt.offset == (bfd_vma) -1)
2616 {
2617 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2618 if (r_type == htab->pointer_r_type
2619 && (input_section->flags & SEC_CODE) == 0)
2620 goto do_ifunc_pointer;
2621 goto bad_ifunc_reloc;
2622 }
cbe950e9
L
2623
2624 /* STT_GNU_IFUNC symbol must go through PLT. */
0ff2b86e
L
2625 if (htab->elf.splt != NULL)
2626 {
f2c29a16 2627 if (htab->plt_second != NULL)
0ff2b86e 2628 {
f2c29a16
L
2629 resolved_plt = htab->plt_second;
2630 plt_offset = eh->plt_second.offset;
0ff2b86e
L
2631 }
2632 else
2633 {
2634 resolved_plt = htab->elf.splt;
2635 plt_offset = h->plt.offset;
2636 }
2637 }
2638 else
2639 {
2640 resolved_plt = htab->elf.iplt;
2641 plt_offset = h->plt.offset;
2642 }
2643
2644 relocation = (resolved_plt->output_section->vma
2645 + resolved_plt->output_offset + plt_offset);
cbe950e9
L
2646
2647 switch (r_type)
2648 {
2649 default:
233cc9c1 2650bad_ifunc_reloc:
4c544807
L
2651 if (h->root.root.string)
2652 name = h->root.root.string;
2653 else
2654 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2655 NULL);
4eca0228 2656 _bfd_error_handler
695344c0 2657 /* xgettext:c-format */
cbe950e9 2658 (_("%B: relocation %s against STT_GNU_IFUNC "
233cc9c1
L
2659 "symbol `%s' isn't supported"), input_bfd,
2660 howto->name, name);
cbe950e9
L
2661 bfd_set_error (bfd_error_bad_value);
2662 return FALSE;
2663
2664 case R_X86_64_32S:
0e1862bb 2665 if (bfd_link_pic (info))
cbe950e9 2666 abort ();
710ab287
L
2667 goto do_relocation;
2668
248775ba
L
2669 case R_X86_64_32:
2670 if (ABI_64_P (output_bfd))
2671 goto do_relocation;
17672001 2672 /* FALLTHROUGH */
eed180f8 2673 case R_X86_64_64:
233cc9c1 2674do_ifunc_pointer:
710ab287
L
2675 if (rel->r_addend != 0)
2676 {
4c544807
L
2677 if (h->root.root.string)
2678 name = h->root.root.string;
2679 else
2680 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2681 sym, NULL);
4eca0228 2682 _bfd_error_handler
695344c0 2683 /* xgettext:c-format */
710ab287 2684 (_("%B: relocation %s against STT_GNU_IFUNC "
d42c267e 2685 "symbol `%s' has non-zero addend: %Ld"),
ef77d6af 2686 input_bfd, howto->name, name, rel->r_addend);
710ab287
L
2687 bfd_set_error (bfd_error_bad_value);
2688 return FALSE;
2689 }
2690
2691 /* Generate dynamic relcoation only when there is a
233cc9c1
L
2692 non-GOT reference in a shared object or there is no
2693 PLT. */
2694 if ((bfd_link_pic (info) && h->non_got_ref)
2695 || h->plt.offset == (bfd_vma) -1)
710ab287
L
2696 {
2697 Elf_Internal_Rela outrel;
710ab287
L
2698 asection *sreloc;
2699
c25bc9fc
L
2700 /* Need a dynamic relocation to get the real function
2701 address. */
710ab287
L
2702 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2703 info,
2704 input_section,
2705 rel->r_offset);
2706 if (outrel.r_offset == (bfd_vma) -1
2707 || outrel.r_offset == (bfd_vma) -2)
2708 abort ();
2709
2710 outrel.r_offset += (input_section->output_section->vma
2711 + input_section->output_offset);
2712
2713 if (h->dynindx == -1
44c4ea11 2714 || h->forced_local
0e1862bb 2715 || bfd_link_executable (info))
710ab287 2716 {
6322e5c5
L
2717 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
2718 h->root.root.string,
2719 h->root.u.def.section->owner);
2720
710ab287 2721 /* This symbol is resolved locally. */
56b8aada
L
2722 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
2723 outrel.r_addend = (h->root.u.def.value
2724 + h->root.u.def.section->output_section->vma
2725 + h->root.u.def.section->output_offset);
710ab287
L
2726 }
2727 else
2728 {
351f65ca 2729 outrel.r_info = htab->r_info (h->dynindx, r_type);
710ab287
L
2730 outrel.r_addend = 0;
2731 }
2732
233cc9c1
L
2733 /* Dynamic relocations are stored in
2734 1. .rela.ifunc section in PIC object.
2735 2. .rela.got section in dynamic executable.
2736 3. .rela.iplt section in static executable. */
2737 if (bfd_link_pic (info))
2738 sreloc = htab->elf.irelifunc;
2739 else if (htab->elf.splt != NULL)
2740 sreloc = htab->elf.srelgot;
2741 else
2742 sreloc = htab->elf.irelplt;
351f65ca 2743 elf_append_rela (output_bfd, sreloc, &outrel);
710ab287
L
2744
2745 /* If this reloc is against an external symbol, we
2746 do not want to fiddle with the addend. Otherwise,
2747 we need to include the symbol value so that it
2748 becomes an addend for the dynamic reloc. For an
2749 internal symbol, we have updated addend. */
56b8aada 2750 continue;
710ab287 2751 }
17672001 2752 /* FALLTHROUGH */
cbe950e9 2753 case R_X86_64_PC32:
c3320543 2754 case R_X86_64_PC32_BND:
cbe950e9
L
2755 case R_X86_64_PC64:
2756 case R_X86_64_PLT32:
c3320543 2757 case R_X86_64_PLT32_BND:
cbe950e9 2758 goto do_relocation;
cbe950e9
L
2759 }
2760 }
2761
aec6b87e 2762 resolved_to_zero = (eh != NULL
e62b9723 2763 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
0afcef53 2764 X86_64_ELF_DATA,
e62b9723
L
2765 eh->has_got_reloc,
2766 eh));
aec6b87e 2767
70256ad8
AJ
2768 /* When generating a shared object, the relocations handled here are
2769 copied into the output file to be resolved at run time. */
2770 switch (r_type)
2771 {
2772 case R_X86_64_GOT32:
7b81dfbb 2773 case R_X86_64_GOT64:
70256ad8
AJ
2774 /* Relocation is to the entry for this symbol in the global
2775 offset table. */
70256ad8 2776 case R_X86_64_GOTPCREL:
56ceb5b5
L
2777 case R_X86_64_GOTPCRELX:
2778 case R_X86_64_REX_GOTPCRELX:
7b81dfbb
AJ
2779 case R_X86_64_GOTPCREL64:
2780 /* Use global offset table entry as symbol value. */
2781 case R_X86_64_GOTPLT64:
de194d85 2782 /* This is obsolete and treated the same as GOT64. */
6de2ae4a 2783 base_got = htab->elf.sgot;
7b81dfbb 2784
6de2ae4a 2785 if (htab->elf.sgot == NULL)
c434dee6 2786 abort ();
053579d7 2787
9e9821dd 2788 relative_reloc = FALSE;
51e0a107 2789 if (h != NULL)
70256ad8 2790 {
b34976b6 2791 bfd_boolean dyn;
c434dee6
AJ
2792
2793 off = h->got.offset;
7b81dfbb 2794 if (h->needs_plt
eed180f8 2795 && h->plt.offset != (bfd_vma)-1
7b81dfbb
AJ
2796 && off == (bfd_vma)-1)
2797 {
2798 /* We can't use h->got.offset here to save
2799 state, or even just remember the offset, as
2800 finish_dynamic_symbol would use that as offset into
2801 .got. */
38b12349 2802 bfd_vma plt_index = (h->plt.offset / plt_entry_size
765e526c 2803 - htab->plt.has_plt0);
7b81dfbb 2804 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 2805 base_got = htab->elf.sgotplt;
7b81dfbb
AJ
2806 }
2807
c434dee6 2808 dyn = htab->elf.dynamic_sections_created;
51e0a107 2809
0e1862bb
L
2810 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
2811 || (bfd_link_pic (info)
6999821f 2812 && SYMBOL_REFERENCES_LOCAL_P (info, h))
4bc6e03a
AJ
2813 || (ELF_ST_VISIBILITY (h->other)
2814 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
2815 {
2816 /* This is actually a static link, or it is a -Bsymbolic
2817 link and the symbol is defined locally, or the symbol
407443a3 2818 was forced to be local because of a version file. We
51e0a107
JH
2819 must initialize this entry in the global offset table.
2820 Since the offset must always be a multiple of 8, we
2821 use the least significant bit to record whether we
2822 have initialized it already.
2823
2824 When doing a dynamic link, we create a .rela.got
407443a3
AJ
2825 relocation entry to initialize the value. This is
2826 done in the finish_dynamic_symbol routine. */
51e0a107
JH
2827 if ((off & 1) != 0)
2828 off &= ~1;
2829 else
2830 {
2831 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
2832 base_got->contents + off);
2833 /* Note that this is harmless for the GOTPLT64 case,
eed180f8 2834 as -1 | 1 still is -1. */
51e0a107 2835 h->got.offset |= 1;
9e9821dd
L
2836
2837 if (h->dynindx == -1
2838 && !h->forced_local
2839 && h->root.type != bfd_link_hash_undefweak
2840 && bfd_link_pic (info))
2841 {
2842 /* If this symbol isn't dynamic in PIC,
2843 generate R_X86_64_RELATIVE here. */
2844 eh->no_finish_dynamic_symbol = 1;
2845 relative_reloc = TRUE;
2846 }
51e0a107
JH
2847 }
2848 }
053579d7 2849 else
b34976b6 2850 unresolved_reloc = FALSE;
70256ad8 2851 }
51e0a107
JH
2852 else
2853 {
c434dee6
AJ
2854 if (local_got_offsets == NULL)
2855 abort ();
51e0a107
JH
2856
2857 off = local_got_offsets[r_symndx];
2858
2859 /* The offset must always be a multiple of 8. We use
407443a3
AJ
2860 the least significant bit to record whether we have
2861 already generated the necessary reloc. */
51e0a107
JH
2862 if ((off & 1) != 0)
2863 off &= ~1;
2864 else
2865 {
c434dee6 2866 bfd_put_64 (output_bfd, relocation,
7b81dfbb 2867 base_got->contents + off);
9e9821dd 2868 local_got_offsets[r_symndx] |= 1;
51e0a107 2869
0e1862bb 2870 if (bfd_link_pic (info))
9e9821dd 2871 relative_reloc = TRUE;
51e0a107 2872 }
51e0a107 2873 }
6a2bda3f 2874
9e9821dd
L
2875 if (relative_reloc)
2876 {
2877 asection *s;
2878 Elf_Internal_Rela outrel;
2879
2880 /* We need to generate a R_X86_64_RELATIVE reloc
2881 for the dynamic linker. */
2882 s = htab->elf.srelgot;
2883 if (s == NULL)
2884 abort ();
2885
2886 outrel.r_offset = (base_got->output_section->vma
2887 + base_got->output_offset
2888 + off);
2889 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
2890 outrel.r_addend = relocation;
2891 elf_append_rela (output_bfd, s, &outrel);
2892 }
2893
c434dee6
AJ
2894 if (off >= (bfd_vma) -2)
2895 abort ();
2896
7b81dfbb
AJ
2897 relocation = base_got->output_section->vma
2898 + base_got->output_offset + off;
56ceb5b5
L
2899 if (r_type != R_X86_64_GOTPCREL
2900 && r_type != R_X86_64_GOTPCRELX
2901 && r_type != R_X86_64_REX_GOTPCRELX
2902 && r_type != R_X86_64_GOTPCREL64)
6de2ae4a
L
2903 relocation -= htab->elf.sgotplt->output_section->vma
2904 - htab->elf.sgotplt->output_offset;
c434dee6 2905
70256ad8
AJ
2906 break;
2907
d6ab8113
JB
2908 case R_X86_64_GOTOFF64:
2909 /* Relocation is relative to the start of the global offset
2910 table. */
2911
3d949995
L
2912 /* Check to make sure it isn't a protected function or data
2913 symbol for shared library since it may not be local when
e3c0e327
L
2914 used as function address or with copy relocation. We also
2915 need to make sure that a symbol is referenced locally. */
0e1862bb 2916 if (bfd_link_pic (info) && h)
d6ab8113 2917 {
e3c0e327
L
2918 if (!h->def_regular)
2919 {
2920 const char *v;
2921
2922 switch (ELF_ST_VISIBILITY (h->other))
2923 {
2924 case STV_HIDDEN:
2925 v = _("hidden symbol");
2926 break;
2927 case STV_INTERNAL:
2928 v = _("internal symbol");
2929 break;
2930 case STV_PROTECTED:
2931 v = _("protected symbol");
2932 break;
2933 default:
2934 v = _("symbol");
2935 break;
2936 }
2937
4eca0228 2938 _bfd_error_handler
695344c0 2939 /* xgettext:c-format */
63a5468a
AM
2940 (_("%B: relocation R_X86_64_GOTOFF64 against undefined %s"
2941 " `%s' can not be used when making a shared object"),
e3c0e327
L
2942 input_bfd, v, h->root.root.string);
2943 bfd_set_error (bfd_error_bad_value);
2944 return FALSE;
2945 }
0e1862bb 2946 else if (!bfd_link_executable (info)
6999821f 2947 && !SYMBOL_REFERENCES_LOCAL_P (info, h)
e3c0e327
L
2948 && (h->type == STT_FUNC
2949 || h->type == STT_OBJECT)
2950 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2951 {
4eca0228 2952 _bfd_error_handler
695344c0 2953 /* xgettext:c-format */
63a5468a
AM
2954 (_("%B: relocation R_X86_64_GOTOFF64 against protected %s"
2955 " `%s' can not be used when making a shared object"),
e3c0e327
L
2956 input_bfd,
2957 h->type == STT_FUNC ? "function" : "data",
2958 h->root.root.string);
2959 bfd_set_error (bfd_error_bad_value);
d6ab8113 2960 return FALSE;
e3c0e327 2961 }
d6ab8113
JB
2962 }
2963
2964 /* Note that sgot is not involved in this
2965 calculation. We always want the start of .got.plt. If we
2966 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2967 permitted by the ABI, we might have to change this
2968 calculation. */
6de2ae4a
L
2969 relocation -= htab->elf.sgotplt->output_section->vma
2970 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
2971 break;
2972
2973 case R_X86_64_GOTPC32:
7b81dfbb 2974 case R_X86_64_GOTPC64:
d6ab8113 2975 /* Use global offset table as symbol value. */
6de2ae4a
L
2976 relocation = htab->elf.sgotplt->output_section->vma
2977 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
2978 unresolved_reloc = FALSE;
2979 break;
7b81dfbb
AJ
2980
2981 case R_X86_64_PLTOFF64:
2982 /* Relocation is PLT entry relative to GOT. For local
2983 symbols it's the symbol itself relative to GOT. */
eed180f8 2984 if (h != NULL
7b81dfbb 2985 /* See PLT32 handling. */
9d734efa
L
2986 && (h->plt.offset != (bfd_vma) -1
2987 || eh->plt_got.offset != (bfd_vma) -1)
6de2ae4a 2988 && htab->elf.splt != NULL)
7b81dfbb 2989 {
9d734efa
L
2990 if (eh->plt_got.offset != (bfd_vma) -1)
2991 {
2992 /* Use the GOT PLT. */
2993 resolved_plt = htab->plt_got;
2994 plt_offset = eh->plt_got.offset;
2995 }
f2c29a16 2996 else if (htab->plt_second != NULL)
0ff2b86e 2997 {
f2c29a16
L
2998 resolved_plt = htab->plt_second;
2999 plt_offset = eh->plt_second.offset;
0ff2b86e
L
3000 }
3001 else
3002 {
3003 resolved_plt = htab->elf.splt;
3004 plt_offset = h->plt.offset;
3005 }
3006
3007 relocation = (resolved_plt->output_section->vma
3008 + resolved_plt->output_offset
3009 + plt_offset);
7b81dfbb
AJ
3010 unresolved_reloc = FALSE;
3011 }
3012
6de2ae4a
L
3013 relocation -= htab->elf.sgotplt->output_section->vma
3014 + htab->elf.sgotplt->output_offset;
7b81dfbb 3015 break;
d6ab8113 3016
70256ad8 3017 case R_X86_64_PLT32:
c3320543 3018 case R_X86_64_PLT32_BND:
70256ad8
AJ
3019 /* Relocation is to the entry for this symbol in the
3020 procedure linkage table. */
3021
3022 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 3023 without using the procedure linkage table. */
70256ad8
AJ
3024 if (h == NULL)
3025 break;
3026
dd7e64d4
L
3027 if ((h->plt.offset == (bfd_vma) -1
3028 && eh->plt_got.offset == (bfd_vma) -1)
6de2ae4a 3029 || htab->elf.splt == NULL)
70256ad8
AJ
3030 {
3031 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
3032 happens when statically linking PIC code, or when
3033 using -Bsymbolic. */
70256ad8
AJ
3034 break;
3035 }
3036
dd7e64d4 3037 if (h->plt.offset != (bfd_vma) -1)
0ff2b86e 3038 {
f2c29a16 3039 if (htab->plt_second != NULL)
dd7e64d4 3040 {
f2c29a16
L
3041 resolved_plt = htab->plt_second;
3042 plt_offset = eh->plt_second.offset;
dd7e64d4
L
3043 }
3044 else
3045 {
3046 resolved_plt = htab->elf.splt;
3047 plt_offset = h->plt.offset;
3048 }
0ff2b86e
L
3049 }
3050 else
3051 {
dd7e64d4
L
3052 /* Use the GOT PLT. */
3053 resolved_plt = htab->plt_got;
3054 plt_offset = eh->plt_got.offset;
0ff2b86e
L
3055 }
3056
3057 relocation = (resolved_plt->output_section->vma
3058 + resolved_plt->output_offset
3059 + plt_offset);
b34976b6 3060 unresolved_reloc = FALSE;
70256ad8
AJ
3061 break;
3062
1788fc08
L
3063 case R_X86_64_SIZE32:
3064 case R_X86_64_SIZE64:
1788fc08
L
3065 /* Set to symbol size. */
3066 relocation = st_size;
3067 goto direct;
3068
fd8ab9e5
AJ
3069 case R_X86_64_PC8:
3070 case R_X86_64_PC16:
3071 case R_X86_64_PC32:
c3320543 3072 case R_X86_64_PC32_BND:
6333bc0d 3073 /* Don't complain about -fPIC if the symbol is undefined when
7073b5b9
L
3074 building executable unless it is unresolved weak symbol or
3075 -z nocopyreloc is used. */
aec6b87e 3076 if ((input_section->flags & SEC_ALLOC) != 0
90f487df 3077 && (input_section->flags & SEC_READONLY) != 0
6333bc0d 3078 && h != NULL
aec6b87e 3079 && ((bfd_link_executable (info)
7073b5b9
L
3080 && ((h->root.type == bfd_link_hash_undefweak
3081 && !resolved_to_zero)
a5b4ee94
L
3082 || ((info->nocopyreloc
3083 || (eh->def_protected
3084 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
7073b5b9
L
3085 && h->def_dynamic
3086 && !(h->root.u.def.section->flags & SEC_CODE))))
c5bb8910 3087 || bfd_link_dll (info)))
6610a52d 3088 {
41bed6dd
L
3089 bfd_boolean fail = FALSE;
3090 bfd_boolean branch
c3320543
L
3091 = ((r_type == R_X86_64_PC32
3092 || r_type == R_X86_64_PC32_BND)
41bed6dd
L
3093 && is_32bit_relative_branch (contents, rel->r_offset));
3094
6999821f 3095 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
41bed6dd
L
3096 {
3097 /* Symbol is referenced locally. Make sure it is
3098 defined locally or for a branch. */
8170f769
L
3099 fail = (!(h->def_regular || ELF_COMMON_DEF_P (h))
3100 && !branch);
41bed6dd 3101 }
aec6b87e 3102 else if (!(bfd_link_pie (info)
bc696fd5 3103 && (h->needs_copy || eh->needs_copy)))
41bed6dd 3104 {
9a926d55
L
3105 /* Symbol doesn't need copy reloc and isn't referenced
3106 locally. We only allow branch to symbol with
3107 non-default visibility. */
41bed6dd
L
3108 fail = (!branch
3109 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3110 }
3111
3112 if (fail)
aab921ad 3113 return elf_x86_64_need_pic (info, input_bfd, input_section,
338c190a 3114 h, NULL, NULL, howto);
6610a52d
L
3115 }
3116 /* Fall through. */
3117
70256ad8
AJ
3118 case R_X86_64_8:
3119 case R_X86_64_16:
3120 case R_X86_64_32:
d6ab8113 3121 case R_X86_64_PC64:
6b3db546 3122 case R_X86_64_64:
80643fbc 3123 /* FIXME: The ABI says the linker should make sure the value is
407443a3 3124 the same when it's zeroextended to 64 bit. */
c434dee6 3125
1788fc08 3126direct:
b1e24c02 3127 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
3128 break;
3129
9a926d55 3130 /* Don't copy a pc-relative relocation into the output file
6333bc0d 3131 if the symbol needs copy reloc or the symbol is undefined
04ebc307 3132 when building executable. Copy dynamic function pointer
aec6b87e
L
3133 relocations. Don't generate dynamic relocations against
3134 resolved undefined weak symbols in PIE. */
0e1862bb 3135 if ((bfd_link_pic (info)
aec6b87e 3136 && !(bfd_link_pie (info)
fd9edc90 3137 && h != NULL
6333bc0d
L
3138 && (h->needs_copy
3139 || eh->needs_copy
3140 || h->root.type == bfd_link_hash_undefined)
d8457a04
L
3141 && (IS_X86_64_PCREL_TYPE (r_type)
3142 || r_type == R_X86_64_SIZE32
3143 || r_type == R_X86_64_SIZE64))
4bc6e03a 3144 && (h == NULL
aec6b87e
L
3145 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3146 && !resolved_to_zero)
3147 || h->root.type != bfd_link_hash_undefweak))
1788fc08
L
3148 && ((! IS_X86_64_PCREL_TYPE (r_type)
3149 && r_type != R_X86_64_SIZE32
3150 && r_type != R_X86_64_SIZE64)
d8045f23 3151 || ! SYMBOL_CALLS_LOCAL (info, h)))
d40d037c 3152 || (ELIMINATE_COPY_RELOCS
0e1862bb 3153 && !bfd_link_pic (info)
c434dee6
AJ
3154 && h != NULL
3155 && h->dynindx != -1
aec6b87e
L
3156 && (!h->non_got_ref
3157 || eh->func_pointer_refcount > 0
3158 || (h->root.type == bfd_link_hash_undefweak
3159 && !resolved_to_zero))
bae420ef
L
3160 && ((h->def_dynamic && !h->def_regular)
3161 /* Undefined weak symbol is bound locally when
3162 PIC is false. */
0f88be7a 3163 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
3164 {
3165 Elf_Internal_Rela outrel;
b34976b6 3166 bfd_boolean skip, relocate;
c434dee6 3167 asection *sreloc;
70256ad8
AJ
3168
3169 /* When generating a shared object, these relocations
3170 are copied into the output file to be resolved at run
407443a3 3171 time. */
b34976b6
AM
3172 skip = FALSE;
3173 relocate = FALSE;
70256ad8 3174
c629eae0
JJ
3175 outrel.r_offset =
3176 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 3177 rel->r_offset);
c629eae0 3178 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3179 skip = TRUE;
0fb19cbc 3180 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3181 skip = TRUE, relocate = TRUE;
70256ad8
AJ
3182
3183 outrel.r_offset += (input_section->output_section->vma
3184 + input_section->output_offset);
3185
3186 if (skip)
0bb2d96a 3187 memset (&outrel, 0, sizeof outrel);
c434dee6 3188
fd8ab9e5
AJ
3189 /* h->dynindx may be -1 if this symbol was marked to
3190 become local. */
3191 else if (h != NULL
c434dee6 3192 && h->dynindx != -1
d8045f23 3193 && (IS_X86_64_PCREL_TYPE (r_type)
4e0c91e4
L
3194 || !(bfd_link_executable (info)
3195 || SYMBOLIC_BIND (info, h))
d8045f23 3196 || ! h->def_regular))
70256ad8 3197 {
351f65ca 3198 outrel.r_info = htab->r_info (h->dynindx, r_type);
c434dee6 3199 outrel.r_addend = rel->r_addend;
70256ad8
AJ
3200 }
3201 else
3202 {
4c10bbaa
L
3203 /* This symbol is local, or marked to become local.
3204 When relocation overflow check is disabled, we
3205 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
3206 if (r_type == htab->pointer_r_type
3207 || (r_type == R_X86_64_32
3208 && info->no_reloc_overflow_check))
607c0e09 3209 {
b34976b6 3210 relocate = TRUE;
351f65ca 3211 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
607c0e09
AS
3212 outrel.r_addend = relocation + rel->r_addend;
3213 }
64d25c44
L
3214 else if (r_type == R_X86_64_64
3215 && !ABI_64_P (output_bfd))
3216 {
3217 relocate = TRUE;
3218 outrel.r_info = htab->r_info (0,
3219 R_X86_64_RELATIVE64);
3220 outrel.r_addend = relocation + rel->r_addend;
8cf0d2dd
L
3221 /* Check addend overflow. */
3222 if ((outrel.r_addend & 0x80000000)
3223 != (rel->r_addend & 0x80000000))
3224 {
3225 const char *name;
268a8d3a 3226 int addend = rel->r_addend;
8cf0d2dd
L
3227 if (h && h->root.root.string)
3228 name = h->root.root.string;
3229 else
3230 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3231 sym, NULL);
d42c267e
AM
3232 _bfd_error_handler
3233 /* xgettext:c-format */
3234 (_("%B: addend %s%#x in relocation %s against "
3235 "symbol `%s' at %#Lx in section `%A' is "
3236 "out of range"),
3237 input_bfd, addend < 0 ? "-" : "", addend,
3238 howto->name, name, rel->r_offset, input_section);
8cf0d2dd
L
3239 bfd_set_error (bfd_error_bad_value);
3240 return FALSE;
3241 }
64d25c44 3242 }
607c0e09
AS
3243 else
3244 {
3245 long sindx;
3246
8517fae7 3247 if (bfd_is_abs_section (sec))
607c0e09
AS
3248 sindx = 0;
3249 else if (sec == NULL || sec->owner == NULL)
3250 {
3251 bfd_set_error (bfd_error_bad_value);
b34976b6 3252 return FALSE;
607c0e09
AS
3253 }
3254 else
3255 {
3256 asection *osec;
3257
74541ad4
AM
3258 /* We are turning this relocation into one
3259 against a section symbol. It would be
3260 proper to subtract the symbol's value,
3261 osec->vma, from the emitted reloc addend,
3262 but ld.so expects buggy relocs. */
607c0e09
AS
3263 osec = sec->output_section;
3264 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
3265 if (sindx == 0)
3266 {
3267 asection *oi = htab->elf.text_index_section;
3268 sindx = elf_section_data (oi)->dynindx;
3269 }
3270 BFD_ASSERT (sindx != 0);
607c0e09
AS
3271 }
3272
351f65ca 3273 outrel.r_info = htab->r_info (sindx, r_type);
607c0e09
AS
3274 outrel.r_addend = relocation + rel->r_addend;
3275 }
70256ad8
AJ
3276 }
3277
cbe950e9 3278 sreloc = elf_section_data (input_section)->sreloc;
d8045f23 3279
62d78908
L
3280 if (sreloc == NULL || sreloc->contents == NULL)
3281 {
3282 r = bfd_reloc_notsupported;
3283 goto check_relocation_error;
3284 }
c434dee6 3285
351f65ca 3286 elf_append_rela (output_bfd, sreloc, &outrel);
70256ad8
AJ
3287
3288 /* If this reloc is against an external symbol, we do
3289 not want to fiddle with the addend. Otherwise, we
3290 need to include the symbol value so that it becomes
3291 an addend for the dynamic reloc. */
0f88be7a 3292 if (! relocate)
70256ad8
AJ
3293 continue;
3294 }
3295
3296 break;
3297
bffbf940 3298 case R_X86_64_TLSGD:
67a4f2b7
AO
3299 case R_X86_64_GOTPC32_TLSDESC:
3300 case R_X86_64_TLSDESC_CALL:
bffbf940 3301 case R_X86_64_GOTTPOFF:
bffbf940
JJ
3302 tls_type = GOT_UNKNOWN;
3303 if (h == NULL && local_got_offsets)
0afcef53 3304 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
bffbf940 3305 else if (h != NULL)
0afcef53 3306 tls_type = elf_x86_hash_entry (h)->tls_type;
142411ca 3307
419414ea 3308 r_type_tls = r_type;
351f65ca
L
3309 if (! elf_x86_64_tls_transition (info, input_bfd,
3310 input_section, contents,
3311 symtab_hdr, sym_hashes,
419414ea 3312 &r_type_tls, tls_type, rel,
bedfd056 3313 relend, h, r_symndx, TRUE))
534a31f6 3314 return FALSE;
bffbf940 3315
419414ea 3316 if (r_type_tls == R_X86_64_TPOFF32)
bffbf940 3317 {
142411ca
L
3318 bfd_vma roff = rel->r_offset;
3319
bffbf940 3320 BFD_ASSERT (! unresolved_reloc);
142411ca 3321
419414ea 3322 if (r_type == R_X86_64_TLSGD)
bffbf940 3323 {
52bc799a 3324 /* GD->LE transition. For 64bit, change
e2cbcd91
L
3325 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3326 .word 0x6666; rex64; call __tls_get_addr@PLT
3327 or
3328 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3329 .byte 0x66; rex64
3330 call *__tls_get_addr@GOTPCREL(%rip)
3331 which may be converted to
3332 addr32 call __tls_get_addr
52bc799a 3333 into:
e2cbcd91
L
3334 movq %fs:0, %rax
3335 leaq foo@tpoff(%rax), %rax
52bc799a 3336 For 32bit, change
e2cbcd91
L
3337 leaq foo@tlsgd(%rip), %rdi
3338 .word 0x6666; rex64; call __tls_get_addr@PLT
3339 or
3340 leaq foo@tlsgd(%rip), %rdi
3341 .byte 0x66; rex64
3342 call *__tls_get_addr@GOTPCREL(%rip)
3343 which may be converted to
3344 addr32 call __tls_get_addr
52bc799a 3345 into:
e2cbcd91
L
3346 movl %fs:0, %eax
3347 leaq foo@tpoff(%rax), %rax
5c98a14e 3348 For largepic, change:
e2cbcd91
L
3349 leaq foo@tlsgd(%rip), %rdi
3350 movabsq $__tls_get_addr@pltoff, %rax
3351 addq %r15, %rax
3352 call *%rax
5c98a14e 3353 into:
e2cbcd91
L
3354 movq %fs:0, %rax
3355 leaq foo@tpoff(%rax), %rax
3356 nopw 0x0(%rax,%rax,1) */
5c98a14e 3357 int largepic = 0;
e2cbcd91 3358 if (ABI_64_P (output_bfd))
5c98a14e 3359 {
e2cbcd91
L
3360 if (contents[roff + 5] == 0xb8)
3361 {
3362 memcpy (contents + roff - 3,
3363 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
3364 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3365 largepic = 1;
3366 }
3367 else
3368 memcpy (contents + roff - 4,
3369 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3370 16);
5c98a14e 3371 }
52bc799a
L
3372 else
3373 memcpy (contents + roff - 3,
3374 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3375 15);
eb4ff4d6 3376 bfd_put_32 (output_bfd,
351f65ca 3377 elf_x86_64_tpoff (info, relocation),
5c98a14e 3378 contents + roff + 8 + largepic);
e2cbcd91
L
3379 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
3380 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
bffbf940 3381 rel++;
60f2e42e 3382 wrel++;
bffbf940
JJ
3383 continue;
3384 }
419414ea 3385 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
67a4f2b7
AO
3386 {
3387 /* GDesc -> LE transition.
3388 It's originally something like:
3389 leaq x@tlsdesc(%rip), %rax
3390
3391 Change it to:
c9736ba0 3392 movl $x@tpoff, %rax. */
67a4f2b7 3393
c9736ba0 3394 unsigned int val, type;
67a4f2b7 3395
67a4f2b7 3396 type = bfd_get_8 (input_bfd, contents + roff - 3);
67a4f2b7 3397 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
3398 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3399 contents + roff - 3);
3400 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3401 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3402 contents + roff - 1);
eb4ff4d6 3403 bfd_put_32 (output_bfd,
351f65ca 3404 elf_x86_64_tpoff (info, relocation),
67a4f2b7
AO
3405 contents + roff);
3406 continue;
3407 }
419414ea 3408 else if (r_type == R_X86_64_TLSDESC_CALL)
67a4f2b7
AO
3409 {
3410 /* GDesc -> LE transition.
3411 It's originally:
3412 call *(%rax)
3413 Turn it into:
142411ca 3414 xchg %ax,%ax. */
10efb593 3415 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3416 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3417 continue;
3418 }
419414ea 3419 else if (r_type == R_X86_64_GOTTPOFF)
bffbf940 3420 {
bffbf940 3421 /* IE->LE transition:
cf61b747 3422 For 64bit, originally it can be one of:
bffbf940
JJ
3423 movq foo@gottpoff(%rip), %reg
3424 addq foo@gottpoff(%rip), %reg
3425 We change it into:
3426 movq $foo, %reg
3427 leaq foo(%reg), %reg
cf61b747
L
3428 addq $foo, %reg.
3429 For 32bit, originally it can be one of:
3430 movq foo@gottpoff(%rip), %reg
3431 addl foo@gottpoff(%rip), %reg
3432 We change it into:
3433 movq $foo, %reg
3434 leal foo(%reg), %reg
3435 addl $foo, %reg. */
142411ca
L
3436
3437 unsigned int val, type, reg;
3438
cf61b747
L
3439 if (roff >= 3)
3440 val = bfd_get_8 (input_bfd, contents + roff - 3);
3441 else
3442 val = 0;
142411ca
L
3443 type = bfd_get_8 (input_bfd, contents + roff - 2);
3444 reg = bfd_get_8 (input_bfd, contents + roff - 1);
bffbf940 3445 reg >>= 3;
bffbf940
JJ
3446 if (type == 0x8b)
3447 {
3448 /* movq */
3449 if (val == 0x4c)
3450 bfd_put_8 (output_bfd, 0x49,
142411ca 3451 contents + roff - 3);
4a4c5f25
L
3452 else if (!ABI_64_P (output_bfd) && val == 0x44)
3453 bfd_put_8 (output_bfd, 0x41,
3454 contents + roff - 3);
bffbf940 3455 bfd_put_8 (output_bfd, 0xc7,
142411ca 3456 contents + roff - 2);
bffbf940 3457 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 3458 contents + roff - 1);
bffbf940
JJ
3459 }
3460 else if (reg == 4)
3461 {
cf61b747
L
3462 /* addq/addl -> addq/addl - addressing with %rsp/%r12
3463 is special */
bffbf940
JJ
3464 if (val == 0x4c)
3465 bfd_put_8 (output_bfd, 0x49,
142411ca 3466 contents + roff - 3);
4a4c5f25
L
3467 else if (!ABI_64_P (output_bfd) && val == 0x44)
3468 bfd_put_8 (output_bfd, 0x41,
3469 contents + roff - 3);
bffbf940 3470 bfd_put_8 (output_bfd, 0x81,
142411ca 3471 contents + roff - 2);
bffbf940 3472 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 3473 contents + roff - 1);
bffbf940
JJ
3474 }
3475 else
3476 {
cf61b747 3477 /* addq/addl -> leaq/leal */
bffbf940
JJ
3478 if (val == 0x4c)
3479 bfd_put_8 (output_bfd, 0x4d,
142411ca 3480 contents + roff - 3);
4a4c5f25
L
3481 else if (!ABI_64_P (output_bfd) && val == 0x44)
3482 bfd_put_8 (output_bfd, 0x45,
3483 contents + roff - 3);
bffbf940 3484 bfd_put_8 (output_bfd, 0x8d,
142411ca 3485 contents + roff - 2);
bffbf940 3486 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
142411ca 3487 contents + roff - 1);
bffbf940 3488 }
eb4ff4d6 3489 bfd_put_32 (output_bfd,
351f65ca 3490 elf_x86_64_tpoff (info, relocation),
142411ca 3491 contents + roff);
bffbf940
JJ
3492 continue;
3493 }
142411ca
L
3494 else
3495 BFD_ASSERT (FALSE);
bffbf940
JJ
3496 }
3497
6de2ae4a 3498 if (htab->elf.sgot == NULL)
bffbf940
JJ
3499 abort ();
3500
3501 if (h != NULL)
67a4f2b7
AO
3502 {
3503 off = h->got.offset;
0afcef53 3504 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
67a4f2b7 3505 }
bffbf940
JJ
3506 else
3507 {
3508 if (local_got_offsets == NULL)
3509 abort ();
3510
3511 off = local_got_offsets[r_symndx];
67a4f2b7 3512 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
3513 }
3514
3515 if ((off & 1) != 0)
3516 off &= ~1;
26e41594 3517 else
bffbf940
JJ
3518 {
3519 Elf_Internal_Rela outrel;
bffbf940 3520 int dr_type, indx;
67a4f2b7 3521 asection *sreloc;
bffbf940 3522
6de2ae4a 3523 if (htab->elf.srelgot == NULL)
bffbf940
JJ
3524 abort ();
3525
67a4f2b7
AO
3526 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3527
3528 if (GOT_TLS_GDESC_P (tls_type))
3529 {
351f65ca 3530 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
67a4f2b7 3531 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
6de2ae4a
L
3532 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3533 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3534 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3535 + offplt
3536 + htab->sgotplt_jump_table_size);
6de2ae4a 3537 sreloc = htab->elf.srelplt;
67a4f2b7 3538 if (indx == 0)
0afcef53 3539 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
67a4f2b7
AO
3540 else
3541 outrel.r_addend = 0;
351f65ca 3542 elf_append_rela (output_bfd, sreloc, &outrel);
67a4f2b7
AO
3543 }
3544
6de2ae4a 3545 sreloc = htab->elf.srelgot;
67a4f2b7 3546
6de2ae4a
L
3547 outrel.r_offset = (htab->elf.sgot->output_section->vma
3548 + htab->elf.sgot->output_offset + off);
bffbf940 3549
67a4f2b7 3550 if (GOT_TLS_GD_P (tls_type))
bffbf940 3551 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
3552 else if (GOT_TLS_GDESC_P (tls_type))
3553 goto dr_done;
bffbf940
JJ
3554 else
3555 dr_type = R_X86_64_TPOFF64;
3556
6de2ae4a 3557 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
bffbf940 3558 outrel.r_addend = 0;
67a4f2b7
AO
3559 if ((dr_type == R_X86_64_TPOFF64
3560 || dr_type == R_X86_64_TLSDESC) && indx == 0)
0afcef53 3561 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
351f65ca 3562 outrel.r_info = htab->r_info (indx, dr_type);
bffbf940 3563
351f65ca 3564 elf_append_rela (output_bfd, sreloc, &outrel);
bffbf940 3565
67a4f2b7 3566 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
3567 {
3568 if (indx == 0)
3569 {
d40d037c 3570 BFD_ASSERT (! unresolved_reloc);
bffbf940 3571 bfd_put_64 (output_bfd,
0afcef53 3572 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3573 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
bffbf940
JJ
3574 }
3575 else
3576 {
3577 bfd_put_64 (output_bfd, 0,
6de2ae4a 3578 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 3579 outrel.r_info = htab->r_info (indx,
bffbf940
JJ
3580 R_X86_64_DTPOFF64);
3581 outrel.r_offset += GOT_ENTRY_SIZE;
351f65ca 3582 elf_append_rela (output_bfd, sreloc,
464d3bd4 3583 &outrel);
bffbf940
JJ
3584 }
3585 }
3586
67a4f2b7 3587 dr_done:
bffbf940
JJ
3588 if (h != NULL)
3589 h->got.offset |= 1;
3590 else
3591 local_got_offsets[r_symndx] |= 1;
3592 }
3593
67a4f2b7
AO
3594 if (off >= (bfd_vma) -2
3595 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940 3596 abort ();
419414ea 3597 if (r_type_tls == r_type)
bffbf940 3598 {
67a4f2b7
AO
3599 if (r_type == R_X86_64_GOTPC32_TLSDESC
3600 || r_type == R_X86_64_TLSDESC_CALL)
6de2ae4a
L
3601 relocation = htab->elf.sgotplt->output_section->vma
3602 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3603 + offplt + htab->sgotplt_jump_table_size;
3604 else
6de2ae4a
L
3605 relocation = htab->elf.sgot->output_section->vma
3606 + htab->elf.sgot->output_offset + off;
b34976b6 3607 unresolved_reloc = FALSE;
bffbf940 3608 }
142411ca 3609 else
67a4f2b7 3610 {
142411ca 3611 bfd_vma roff = rel->r_offset;
67a4f2b7 3612
419414ea 3613 if (r_type == R_X86_64_TLSGD)
142411ca 3614 {
52bc799a 3615 /* GD->IE transition. For 64bit, change
e2cbcd91
L
3616 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3617 .word 0x6666; rex64; call __tls_get_addr@PLT
3618 or
3619 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3620 .byte 0x66; rex64
3621 call *__tls_get_addr@GOTPCREL(%rip
3622 which may be converted to
3623 addr32 call __tls_get_addr
52bc799a 3624 into:
e2cbcd91
L
3625 movq %fs:0, %rax
3626 addq foo@gottpoff(%rip), %rax
52bc799a 3627 For 32bit, change
e2cbcd91
L
3628 leaq foo@tlsgd(%rip), %rdi
3629 .word 0x6666; rex64; call __tls_get_addr@PLT
3630 or
3631 leaq foo@tlsgd(%rip), %rdi
3632 .byte 0x66; rex64;
3633 call *__tls_get_addr@GOTPCREL(%rip)
3634 which may be converted to
3635 addr32 call __tls_get_addr
52bc799a 3636 into:
e2cbcd91
L
3637 movl %fs:0, %eax
3638 addq foo@gottpoff(%rip), %rax
5c98a14e 3639 For largepic, change:
e2cbcd91
L
3640 leaq foo@tlsgd(%rip), %rdi
3641 movabsq $__tls_get_addr@pltoff, %rax
3642 addq %r15, %rax
3643 call *%rax
5c98a14e 3644 into:
e2cbcd91
L
3645 movq %fs:0, %rax
3646 addq foo@gottpoff(%rax), %rax
3647 nopw 0x0(%rax,%rax,1) */
5c98a14e 3648 int largepic = 0;
e2cbcd91 3649 if (ABI_64_P (output_bfd))
5c98a14e 3650 {
e2cbcd91
L
3651 if (contents[roff + 5] == 0xb8)
3652 {
3653 memcpy (contents + roff - 3,
3654 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
3655 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3656 largepic = 1;
3657 }
3658 else
3659 memcpy (contents + roff - 4,
3660 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3661 16);
5c98a14e 3662 }
52bc799a
L
3663 else
3664 memcpy (contents + roff - 3,
3665 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3666 15);
142411ca 3667
6de2ae4a
L
3668 relocation = (htab->elf.sgot->output_section->vma
3669 + htab->elf.sgot->output_offset + off
142411ca 3670 - roff
5c98a14e 3671 - largepic
142411ca
L
3672 - input_section->output_section->vma
3673 - input_section->output_offset
3674 - 12);
3675 bfd_put_32 (output_bfd, relocation,
5c98a14e
JJ
3676 contents + roff + 8 + largepic);
3677 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
142411ca 3678 rel++;
60f2e42e 3679 wrel++;
142411ca
L
3680 continue;
3681 }
419414ea 3682 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
142411ca
L
3683 {
3684 /* GDesc -> IE transition.
3685 It's originally something like:
3686 leaq x@tlsdesc(%rip), %rax
67a4f2b7 3687
142411ca 3688 Change it to:
c9736ba0 3689 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
67a4f2b7 3690
142411ca
L
3691 /* Now modify the instruction as appropriate. To
3692 turn a leaq into a movq in the form we use it, it
3693 suffices to change the second byte from 0x8d to
3694 0x8b. */
3695 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3696
3697 bfd_put_32 (output_bfd,
6de2ae4a
L
3698 htab->elf.sgot->output_section->vma
3699 + htab->elf.sgot->output_offset + off
142411ca
L
3700 - rel->r_offset
3701 - input_section->output_section->vma
3702 - input_section->output_offset
3703 - 4,
3704 contents + roff);
3705 continue;
3706 }
419414ea 3707 else if (r_type == R_X86_64_TLSDESC_CALL)
142411ca
L
3708 {
3709 /* GDesc -> IE transition.
3710 It's originally:
3711 call *(%rax)
3712
3713 Change it to:
c9736ba0 3714 xchg %ax, %ax. */
142411ca 3715
142411ca
L
3716 bfd_put_8 (output_bfd, 0x66, contents + roff);
3717 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3718 continue;
3719 }
3720 else
3721 BFD_ASSERT (FALSE);
67a4f2b7 3722 }
bffbf940
JJ
3723 break;
3724
3725 case R_X86_64_TLSLD:
351f65ca
L
3726 if (! elf_x86_64_tls_transition (info, input_bfd,
3727 input_section, contents,
3728 symtab_hdr, sym_hashes,
bedfd056
L
3729 &r_type, GOT_UNKNOWN, rel,
3730 relend, h, r_symndx, TRUE))
142411ca 3731 return FALSE;
a3fadc9a 3732
142411ca
L
3733 if (r_type != R_X86_64_TLSLD)
3734 {
bffbf940 3735 /* LD->LE transition:
e2cbcd91
L
3736 leaq foo@tlsld(%rip), %rdi
3737 call __tls_get_addr@PLT
3738 For 64bit, we change it into:
3739 .word 0x6666; .byte 0x66; movq %fs:0, %rax
3740 For 32bit, we change it into:
3741 nopl 0x0(%rax); movl %fs:0, %eax
3742 Or
3743 leaq foo@tlsld(%rip), %rdi;
3744 call *__tls_get_addr@GOTPCREL(%rip)
3745 which may be converted to
3746 addr32 call __tls_get_addr
52bc799a 3747 For 64bit, we change it into:
e2cbcd91 3748 .word 0x6666; .word 0x6666; movq %fs:0, %rax
52bc799a 3749 For 32bit, we change it into:
e2cbcd91 3750 nopw 0x0(%rax); movl %fs:0, %eax
5c98a14e 3751 For largepic, change:
e2cbcd91
L
3752 leaq foo@tlsgd(%rip), %rdi
3753 movabsq $__tls_get_addr@pltoff, %rax
3754 addq %rbx, %rax
3755 call *%rax
3756 into
3757 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
3758 movq %fs:0, %eax */
142411ca
L
3759
3760 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
e2cbcd91
L
3761 if (ABI_64_P (output_bfd))
3762 {
3763 if (contents[rel->r_offset + 5] == 0xb8)
3764 memcpy (contents + rel->r_offset - 3,
3765 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
3766 "\x64\x48\x8b\x04\x25\0\0\0", 22);
3767 else if (contents[rel->r_offset + 4] == 0xff
3768 || contents[rel->r_offset + 4] == 0x67)
3769 memcpy (contents + rel->r_offset - 3,
3770 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
3771 13);
3772 else
3773 memcpy (contents + rel->r_offset - 3,
3774 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3775 }
52bc799a 3776 else
e2cbcd91
L
3777 {
3778 if (contents[rel->r_offset + 4] == 0xff)
3779 memcpy (contents + rel->r_offset - 3,
3780 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
3781 13);
3782 else
3783 memcpy (contents + rel->r_offset - 3,
3784 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
3785 }
3786 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
3787 and R_X86_64_PLTOFF64. */
bffbf940 3788 rel++;
60f2e42e 3789 wrel++;
bffbf940
JJ
3790 continue;
3791 }
3792
6de2ae4a 3793 if (htab->elf.sgot == NULL)
bffbf940
JJ
3794 abort ();
3795
0afcef53 3796 off = htab->tls_ld_or_ldm_got.offset;
bffbf940
JJ
3797 if (off & 1)
3798 off &= ~1;
3799 else
3800 {
3801 Elf_Internal_Rela outrel;
bffbf940 3802
6de2ae4a 3803 if (htab->elf.srelgot == NULL)
bffbf940
JJ
3804 abort ();
3805
6de2ae4a
L
3806 outrel.r_offset = (htab->elf.sgot->output_section->vma
3807 + htab->elf.sgot->output_offset + off);
bffbf940
JJ
3808
3809 bfd_put_64 (output_bfd, 0,
6de2ae4a 3810 htab->elf.sgot->contents + off);
bffbf940 3811 bfd_put_64 (output_bfd, 0,
6de2ae4a 3812 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 3813 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
bffbf940 3814 outrel.r_addend = 0;
351f65ca 3815 elf_append_rela (output_bfd, htab->elf.srelgot,
464d3bd4 3816 &outrel);
0afcef53 3817 htab->tls_ld_or_ldm_got.offset |= 1;
bffbf940 3818 }
6de2ae4a
L
3819 relocation = htab->elf.sgot->output_section->vma
3820 + htab->elf.sgot->output_offset + off;
b34976b6 3821 unresolved_reloc = FALSE;
bffbf940
JJ
3822 break;
3823
3824 case R_X86_64_DTPOFF32:
0e1862bb
L
3825 if (!bfd_link_executable (info)
3826 || (input_section->flags & SEC_CODE) == 0)
0afcef53 3827 relocation -= _bfd_x86_elf_dtpoff_base (info);
bffbf940 3828 else
351f65ca 3829 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
3830 break;
3831
3832 case R_X86_64_TPOFF32:
6769d501 3833 case R_X86_64_TPOFF64:
0e1862bb 3834 BFD_ASSERT (bfd_link_executable (info));
351f65ca 3835 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
3836 break;
3837
a69ed7f7
L
3838 case R_X86_64_DTPOFF64:
3839 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
0afcef53 3840 relocation -= _bfd_x86_elf_dtpoff_base (info);
a69ed7f7
L
3841 break;
3842
70256ad8
AJ
3843 default:
3844 break;
3845 }
8d88c4ca 3846
239e1f3a
AM
3847 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3848 because such sections are not SEC_ALLOC and thus ld.so will
3849 not process them. */
c434dee6 3850 if (unresolved_reloc
239e1f3a 3851 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
3852 && h->def_dynamic)
3853 && _bfd_elf_section_offset (output_bfd, info, input_section,
3854 rel->r_offset) != (bfd_vma) -1)
a040981f 3855 {
7073b5b9
L
3856 switch (r_type)
3857 {
3858 case R_X86_64_32S:
a5b4ee94
L
3859 sec = h->root.u.def.section;
3860 if ((info->nocopyreloc
3861 || (eh->def_protected
3862 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
7073b5b9
L
3863 && !(h->root.u.def.section->flags & SEC_CODE))
3864 return elf_x86_64_need_pic (info, input_bfd, input_section,
3865 h, NULL, NULL, howto);
3866 /* Fall through. */
3867
3868 default:
3869 _bfd_error_handler
3870 /* xgettext:c-format */
3871 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
3872 input_bfd,
3873 input_section,
3874 rel->r_offset,
3875 howto->name,
3876 h->root.root.string);
3877 return FALSE;
3878 }
a040981f 3879 }
c434dee6 3880
cbe950e9 3881do_relocation:
8d88c4ca 3882 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
3883 contents, rel->r_offset,
3884 relocation, rel->r_addend);
8d88c4ca 3885
62d78908 3886check_relocation_error:
8d88c4ca 3887 if (r != bfd_reloc_ok)
8da6118f 3888 {
c434dee6
AJ
3889 const char *name;
3890
3891 if (h != NULL)
3892 name = h->root.root.string;
3893 else
8da6118f 3894 {
c434dee6
AJ
3895 name = bfd_elf_string_from_elf_section (input_bfd,
3896 symtab_hdr->sh_link,
3897 sym->st_name);
3898 if (name == NULL)
b34976b6 3899 return FALSE;
c434dee6
AJ
3900 if (*name == '\0')
3901 name = bfd_section_name (input_bfd, sec);
3902 }
3903
3904 if (r == bfd_reloc_overflow)
8b43e456 3905 {
78984959 3906 if (converted_reloc)
8b43e456
L
3907 {
3908 info->callbacks->einfo
3909 (_("%F%P: failed to convert GOTPCREL relocation; relink with --no-relax\n"));
3910 return FALSE;
3911 }
3912 (*info->callbacks->reloc_overflow)
3913 (info, (h ? &h->root : NULL), name, howto->name,
3914 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3915 }
c434dee6
AJ
3916 else
3917 {
4eca0228 3918 _bfd_error_handler
695344c0 3919 /* xgettext:c-format */
d42c267e 3920 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
d003868e 3921 input_bfd, input_section,
d42c267e 3922 rel->r_offset, name, (int) r);
b34976b6 3923 return FALSE;
8da6118f
KH
3924 }
3925 }
60f2e42e
L
3926
3927 if (wrel != rel)
3928 *wrel = *rel;
3929 }
3930
3931 if (wrel != rel)
3932 {
3933 Elf_Internal_Shdr *rel_hdr;
3934 size_t deleted = rel - wrel;
3935
3936 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3937 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3938 if (rel_hdr->sh_size == 0)
3939 {
3940 /* It is too late to remove an empty reloc section. Leave
3941 one NONE reloc.
3942 ??? What is wrong with an empty section??? */
3943 rel_hdr->sh_size = rel_hdr->sh_entsize;
3944 deleted -= 1;
3945 }
3946 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3947 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3948 input_section->reloc_count -= deleted;
8d88c4ca 3949 }
70256ad8 3950
b34976b6 3951 return TRUE;
70256ad8
AJ
3952}
3953
3954/* Finish up dynamic symbol handling. We set the contents of various
3955 dynamic sections here. */
3956
b34976b6 3957static bfd_boolean
351f65ca
L
3958elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3959 struct bfd_link_info *info,
3960 struct elf_link_hash_entry *h,
aec6b87e 3961 Elf_Internal_Sym *sym)
70256ad8 3962{
0afcef53 3963 struct elf_x86_link_hash_table *htab;
f2c29a16 3964 bfd_boolean use_plt_second;
0afcef53 3965 struct elf_x86_link_hash_entry *eh;
aec6b87e 3966 bfd_boolean local_undefweak;
70256ad8 3967
0afcef53 3968 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6
NC
3969 if (htab == NULL)
3970 return FALSE;
70256ad8 3971
f2c29a16
L
3972 /* Use the second PLT section only if there is .plt section. */
3973 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
0ff2b86e 3974
0afcef53 3975 eh = (struct elf_x86_link_hash_entry *) h;
9e9821dd
L
3976 if (eh->no_finish_dynamic_symbol)
3977 abort ();
dd7e64d4 3978
aec6b87e
L
3979 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3980 resolved undefined weak symbols in executable so that their
3981 references have value 0 at run-time. */
e62b9723 3982 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
0afcef53 3983 X86_64_ELF_DATA,
e62b9723
L
3984 eh->has_got_reloc,
3985 eh);
aec6b87e 3986
70256ad8
AJ
3987 if (h->plt.offset != (bfd_vma) -1)
3988 {
70256ad8 3989 bfd_vma plt_index;
38b12349 3990 bfd_vma got_offset, plt_offset;
70256ad8 3991 Elf_Internal_Rela rela;
947216bf 3992 bfd_byte *loc;
0ff2b86e 3993 asection *plt, *gotplt, *relplt, *resolved_plt;
351f65ca 3994 const struct elf_backend_data *bed;
5974eba6 3995 bfd_vma plt_got_pcrel_offset;
cbe950e9
L
3996
3997 /* When building a static executable, use .iplt, .igot.plt and
3998 .rela.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 3999 if (htab->elf.splt != NULL)
cbe950e9 4000 {
6de2ae4a
L
4001 plt = htab->elf.splt;
4002 gotplt = htab->elf.sgotplt;
4003 relplt = htab->elf.srelplt;
cbe950e9
L
4004 }
4005 else
4006 {
6de2ae4a
L
4007 plt = htab->elf.iplt;
4008 gotplt = htab->elf.igotplt;
4009 relplt = htab->elf.irelplt;
cbe950e9 4010 }
70256ad8
AJ
4011
4012 /* This symbol has an entry in the procedure linkage table. Set
407443a3 4013 it up. */
cbe950e9 4014 if ((h->dynindx == -1
aec6b87e 4015 && !local_undefweak
0e1862bb 4016 && !((h->forced_local || bfd_link_executable (info))
cbe950e9
L
4017 && h->def_regular
4018 && h->type == STT_GNU_IFUNC))
4019 || plt == NULL
4020 || gotplt == NULL
4021 || relplt == NULL)
cec7f46a 4022 abort ();
70256ad8
AJ
4023
4024 /* Get the index in the procedure linkage table which
4025 corresponds to this symbol. This is the index of this symbol
4026 in all the symbols for which we are making plt entries. The
cbe950e9 4027 first entry in the procedure linkage table is reserved.
6bbec505 4028
cbe950e9 4029 Get the offset into the .got table of the entry that
407443a3 4030 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
cbe950e9
L
4031 bytes. The first three are reserved for the dynamic linker.
4032
4033 For static executables, we don't reserve anything. */
4034
6de2ae4a 4035 if (plt == htab->elf.splt)
cbe950e9 4036 {
765e526c
L
4037 got_offset = (h->plt.offset / htab->plt.plt_entry_size
4038 - htab->plt.has_plt0);
e1f98742 4039 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
cbe950e9
L
4040 }
4041 else
4042 {
765e526c 4043 got_offset = h->plt.offset / htab->plt.plt_entry_size;
e1f98742 4044 got_offset = got_offset * GOT_ENTRY_SIZE;
cbe950e9 4045 }
70256ad8 4046
38b12349 4047 /* Fill in the entry in the procedure linkage table. */
765e526c
L
4048 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
4049 htab->plt.plt_entry_size);
f2c29a16 4050 if (use_plt_second)
0ff2b86e 4051 {
f2c29a16 4052 memcpy (htab->plt_second->contents + eh->plt_second.offset,
765e526c
L
4053 htab->non_lazy_plt->plt_entry,
4054 htab->non_lazy_plt->plt_entry_size);
0ff2b86e 4055
f2c29a16
L
4056 resolved_plt = htab->plt_second;
4057 plt_offset = eh->plt_second.offset;
0ff2b86e
L
4058 }
4059 else
4060 {
0ff2b86e
L
4061 resolved_plt = plt;
4062 plt_offset = h->plt.offset;
4063 }
eed180f8
RM
4064
4065 /* Insert the relocation positions of the plt section. */
4066
4067 /* Put offset the PC-relative instruction referring to the GOT entry,
4068 subtracting the size of that instruction. */
ab7fede8
L
4069 plt_got_pcrel_offset = (gotplt->output_section->vma
4070 + gotplt->output_offset
4071 + got_offset
4072 - resolved_plt->output_section->vma
4073 - resolved_plt->output_offset
4074 - plt_offset
765e526c 4075 - htab->plt.plt_got_insn_size);
ab7fede8
L
4076
4077 /* Check PC-relative offset overflow in PLT entry. */
5974eba6 4078 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
695344c0 4079 /* xgettext:c-format */
ab7fede8
L
4080 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
4081 output_bfd, h->root.root.string);
4082
4083 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
38b12349 4084 (resolved_plt->contents + plt_offset
765e526c 4085 + htab->plt.plt_got_offset));
cbe950e9 4086
653165cc 4087 /* Fill in the entry in the global offset table, initially this
aec6b87e
L
4088 points to the second part of the PLT entry. Leave the entry
4089 as zero for undefined weak symbol in PIE. No PLT relocation
4090 against undefined weak symbol in PIE. */
4091 if (!local_undefweak)
cbe950e9 4092 {
765e526c 4093 if (htab->plt.has_plt0)
38b12349
L
4094 bfd_put_64 (output_bfd, (plt->output_section->vma
4095 + plt->output_offset
4096 + h->plt.offset
765e526c 4097 + htab->lazy_plt->plt_lazy_offset),
38b12349 4098 gotplt->contents + got_offset);
aec6b87e
L
4099
4100 /* Fill in the entry in the .rela.plt section. */
4101 rela.r_offset = (gotplt->output_section->vma
4102 + gotplt->output_offset
4103 + got_offset);
4104 if (h->dynindx == -1
4105 || ((bfd_link_executable (info)
4106 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4107 && h->def_regular
4108 && h->type == STT_GNU_IFUNC))
4109 {
6322e5c5
L
4110 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
4111 h->root.root.string,
4112 h->root.u.def.section->owner);
4113
aec6b87e
L
4114 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4115 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4116 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4117 rela.r_addend = (h->root.u.def.value
4118 + h->root.u.def.section->output_section->vma
4119 + h->root.u.def.section->output_offset);
4120 /* R_X86_64_IRELATIVE comes last. */
4121 plt_index = htab->next_irelative_index--;
4122 }
4123 else
4124 {
4125 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4126 rela.r_addend = 0;
4127 plt_index = htab->next_jump_slot_index++;
4128 }
e1f98742 4129
38b12349
L
4130 /* Don't fill the second and third slots in PLT entry for
4131 static executables nor without PLT0. */
765e526c 4132 if (plt == htab->elf.splt && htab->plt.has_plt0)
aec6b87e 4133 {
38b12349 4134 bfd_vma plt0_offset
765e526c 4135 = h->plt.offset + htab->lazy_plt->plt_plt_insn_end;
aec6b87e
L
4136
4137 /* Put relocation index. */
4138 bfd_put_32 (output_bfd, plt_index,
4139 (plt->contents + h->plt.offset
765e526c 4140 + htab->lazy_plt->plt_reloc_offset));
aec6b87e
L
4141
4142 /* Put offset for jmp .PLT0 and check for overflow. We don't
4143 check relocation index for overflow since branch displacement
4144 will overflow first. */
4145 if (plt0_offset > 0x80000000)
695344c0 4146 /* xgettext:c-format */
aec6b87e
L
4147 info->callbacks->einfo (_("%F%B: branch displacement overflow in PLT entry for `%s'\n"),
4148 output_bfd, h->root.root.string);
4149 bfd_put_32 (output_bfd, - plt0_offset,
38b12349 4150 (plt->contents + h->plt.offset
765e526c 4151 + htab->lazy_plt->plt_plt_offset));
aec6b87e 4152 }
351f65ca 4153
aec6b87e
L
4154 bed = get_elf_backend_data (output_bfd);
4155 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4156 bed->s->swap_reloca_out (output_bfd, &rela, loc);
4157 }
dd7e64d4
L
4158 }
4159 else if (eh->plt_got.offset != (bfd_vma) -1)
4160 {
38b12349 4161 bfd_vma got_offset, plt_offset;
dd7e64d4
L
4162 asection *plt, *got;
4163 bfd_boolean got_after_plt;
4164 int32_t got_pcrel_offset;
dd7e64d4
L
4165
4166 /* Set the entry in the GOT procedure linkage table. */
4167 plt = htab->plt_got;
4168 got = htab->elf.sgot;
4169 got_offset = h->got.offset;
4170
4171 if (got_offset == (bfd_vma) -1
e492d2f8 4172 || (h->type == STT_GNU_IFUNC && h->def_regular)
dd7e64d4
L
4173 || plt == NULL
4174 || got == NULL)
4175 abort ();
70256ad8 4176
38b12349 4177 /* Use the non-lazy PLT entry template for the GOT PLT since they
dd7e64d4 4178 are the identical. */
dd7e64d4
L
4179 /* Fill in the entry in the GOT procedure linkage table. */
4180 plt_offset = eh->plt_got.offset;
4181 memcpy (plt->contents + plt_offset,
765e526c
L
4182 htab->non_lazy_plt->plt_entry,
4183 htab->non_lazy_plt->plt_entry_size);
dd7e64d4
L
4184
4185 /* Put offset the PC-relative instruction referring to the GOT
4186 entry, subtracting the size of that instruction. */
4187 got_pcrel_offset = (got->output_section->vma
4188 + got->output_offset
4189 + got_offset
4190 - plt->output_section->vma
4191 - plt->output_offset
4192 - plt_offset
765e526c 4193 - htab->non_lazy_plt->plt_got_insn_size);
dd7e64d4
L
4194
4195 /* Check PC-relative offset overflow in GOT PLT entry. */
4196 got_after_plt = got->output_section->vma > plt->output_section->vma;
4197 if ((got_after_plt && got_pcrel_offset < 0)
4198 || (!got_after_plt && got_pcrel_offset > 0))
695344c0 4199 /* xgettext:c-format */
dd7e64d4
L
4200 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
4201 output_bfd, h->root.root.string);
4202
4203 bfd_put_32 (output_bfd, got_pcrel_offset,
38b12349 4204 (plt->contents + plt_offset
765e526c 4205 + htab->non_lazy_plt->plt_got_offset));
dd7e64d4
L
4206 }
4207
aec6b87e
L
4208 if (!local_undefweak
4209 && !h->def_regular
dd7e64d4
L
4210 && (h->plt.offset != (bfd_vma) -1
4211 || eh->plt_got.offset != (bfd_vma) -1))
4212 {
4213 /* Mark the symbol as undefined, rather than as defined in
4214 the .plt section. Leave the value if there were any
4215 relocations where pointer equality matters (this is a clue
4216 for the dynamic linker, to make function pointer
4217 comparisons work between an application and shared
4218 library), otherwise set it to zero. If a function is only
4219 called from a binary, there is no need to slow down
4220 shared libraries because of that. */
4221 sym->st_shndx = SHN_UNDEF;
4222 if (!h->pointer_equality_needed)
4223 sym->st_value = 0;
70256ad8
AJ
4224 }
4225
aec6b87e
L
4226 /* Don't generate dynamic GOT relocation against undefined weak
4227 symbol in executable. */
bffbf940 4228 if (h->got.offset != (bfd_vma) -1
0afcef53
L
4229 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
4230 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
aec6b87e 4231 && !local_undefweak)
053579d7 4232 {
053579d7 4233 Elf_Internal_Rela rela;
233cc9c1 4234 asection *relgot = htab->elf.srelgot;
053579d7
AJ
4235
4236 /* This symbol has an entry in the global offset table. Set it
bffbf940 4237 up. */
6de2ae4a 4238 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
c434dee6 4239 abort ();
053579d7 4240
6de2ae4a
L
4241 rela.r_offset = (htab->elf.sgot->output_section->vma
4242 + htab->elf.sgot->output_offset
dc810e39 4243 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
4244
4245 /* If this is a static link, or it is a -Bsymbolic link and the
4246 symbol is defined locally or was forced to be local because
4247 of a version file, we just want to emit a RELATIVE reloc.
4248 The entry in the global offset table will already have been
4249 initialized in the relocate_section function. */
710ab287 4250 if (h->def_regular
0018b0a3
L
4251 && h->type == STT_GNU_IFUNC)
4252 {
233cc9c1
L
4253 if (h->plt.offset == (bfd_vma) -1)
4254 {
4255 /* STT_GNU_IFUNC is referenced without PLT. */
4256 if (htab->elf.splt == NULL)
4257 {
4258 /* use .rel[a].iplt section to store .got relocations
4259 in static executable. */
4260 relgot = htab->elf.irelplt;
4261 }
6999821f 4262 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
233cc9c1 4263 {
6322e5c5
L
4264 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
4265 output_bfd,
4266 h->root.root.string,
4267 h->root.u.def.section->owner);
4268
233cc9c1
L
4269 rela.r_info = htab->r_info (0,
4270 R_X86_64_IRELATIVE);
4271 rela.r_addend = (h->root.u.def.value
4272 + h->root.u.def.section->output_section->vma
4273 + h->root.u.def.section->output_offset);
4274 }
4275 else
4276 goto do_glob_dat;
4277 }
4278 else if (bfd_link_pic (info))
710ab287
L
4279 {
4280 /* Generate R_X86_64_GLOB_DAT. */
4281 goto do_glob_dat;
4282 }
4283 else
4284 {
90d60710 4285 asection *plt;
aab82f4c 4286 bfd_vma plt_offset;
90d60710 4287
710ab287
L
4288 if (!h->pointer_equality_needed)
4289 abort ();
4290
4291 /* For non-shared object, we can't use .got.plt, which
4292 contains the real function addres if we need pointer
4293 equality. We load the GOT entry with the PLT entry. */
f2c29a16 4294 if (htab->plt_second != NULL)
aab82f4c 4295 {
f2c29a16
L
4296 plt = htab->plt_second;
4297 plt_offset = eh->plt_second.offset;
aab82f4c
L
4298 }
4299 else
4300 {
4301 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4302 plt_offset = h->plt.offset;
4303 }
710ab287
L
4304 bfd_put_64 (output_bfd, (plt->output_section->vma
4305 + plt->output_offset
aab82f4c 4306 + plt_offset),
6de2ae4a 4307 htab->elf.sgot->contents + h->got.offset);
710ab287
L
4308 return TRUE;
4309 }
0018b0a3 4310 }
0e1862bb 4311 else if (bfd_link_pic (info)
6999821f 4312 && SYMBOL_REFERENCES_LOCAL_P (info, h))
053579d7 4313 {
ad71ce8d 4314 if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
41bed6dd 4315 return FALSE;
cc78d0af 4316 BFD_ASSERT((h->got.offset & 1) != 0);
351f65ca 4317 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
053579d7
AJ
4318 rela.r_addend = (h->root.u.def.value
4319 + h->root.u.def.section->output_section->vma
4320 + h->root.u.def.section->output_offset);
4321 }
4322 else
4323 {
4324 BFD_ASSERT((h->got.offset & 1) == 0);
710ab287 4325do_glob_dat:
c434dee6 4326 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 4327 htab->elf.sgot->contents + h->got.offset);
351f65ca 4328 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
053579d7
AJ
4329 rela.r_addend = 0;
4330 }
4331
233cc9c1 4332 elf_append_rela (output_bfd, relgot, &rela);
053579d7
AJ
4333 }
4334
f5385ebf 4335 if (h->needs_copy)
70256ad8 4336 {
70256ad8 4337 Elf_Internal_Rela rela;
5474d94f 4338 asection *s;
70256ad8
AJ
4339
4340 /* This symbol needs a copy reloc. Set it up. */
4341
c434dee6
AJ
4342 if (h->dynindx == -1
4343 || (h->root.type != bfd_link_hash_defined
4344 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
4345 || htab->elf.srelbss == NULL
4346 || htab->elf.sreldynrelro == NULL)
c434dee6 4347 abort ();
70256ad8
AJ
4348
4349 rela.r_offset = (h->root.u.def.value
4350 + h->root.u.def.section->output_section->vma
4351 + h->root.u.def.section->output_offset);
351f65ca 4352 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
70256ad8 4353 rela.r_addend = 0;
afbf7e8e 4354 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
4355 s = htab->elf.sreldynrelro;
4356 else
4357 s = htab->elf.srelbss;
4358 elf_append_rela (output_bfd, s, &rela);
70256ad8
AJ
4359 }
4360
b34976b6 4361 return TRUE;
70256ad8
AJ
4362}
4363
c25bc9fc
L
4364/* Finish up local dynamic symbol handling. We set the contents of
4365 various dynamic sections here. */
4366
4367static bfd_boolean
351f65ca 4368elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
c25bc9fc
L
4369{
4370 struct elf_link_hash_entry *h
4371 = (struct elf_link_hash_entry *) *slot;
4372 struct bfd_link_info *info
eed180f8 4373 = (struct bfd_link_info *) inf;
c25bc9fc 4374
351f65ca 4375 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5f0f0847 4376 info, h, NULL);
c25bc9fc
L
4377}
4378
aec6b87e
L
4379/* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4380 here since undefined weak symbol may not be dynamic and may not be
4381 called for elf_x86_64_finish_dynamic_symbol. */
4382
4383static bfd_boolean
4384elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4385 void *inf)
4386{
4387 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4388 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4389
4390 if (h->root.type != bfd_link_hash_undefweak
4391 || h->dynindx != -1)
4392 return TRUE;
4393
4394 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5f0f0847 4395 info, h, NULL);
aec6b87e
L
4396}
4397
c434dee6
AJ
4398/* Used to decide how to sort relocs in an optimal manner for the
4399 dynamic linker, before writing them out. */
4400
4401static enum elf_reloc_type_class
cae1fbbb 4402elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
7e612e98
AM
4403 const asection *rel_sec ATTRIBUTE_UNUSED,
4404 const Elf_Internal_Rela *rela)
c434dee6 4405{
cae1fbbb
L
4406 bfd *abfd = info->output_bfd;
4407 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0afcef53
L
4408 struct elf_x86_link_hash_table *htab
4409 = elf_x86_hash_table (info, X86_64_ELF_DATA);
cae1fbbb 4410
d9e3b590
L
4411 if (htab->elf.dynsym != NULL
4412 && htab->elf.dynsym->contents != NULL)
4413 {
4414 /* Check relocation against STT_GNU_IFUNC symbol if there are
4415 dynamic symbols. */
4416 unsigned long r_symndx = htab->r_sym (rela->r_info);
897463b1
L
4417 if (r_symndx != STN_UNDEF)
4418 {
4419 Elf_Internal_Sym sym;
4420 if (!bed->s->swap_symbol_in (abfd,
4421 (htab->elf.dynsym->contents
4422 + r_symndx * bed->s->sizeof_sym),
4423 0, &sym))
4424 abort ();
d9e3b590 4425
897463b1
L
4426 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4427 return reloc_class_ifunc;
4428 }
d9e3b590 4429 }
cae1fbbb 4430
351f65ca 4431 switch ((int) ELF32_R_TYPE (rela->r_info))
c434dee6 4432 {
c428ce9d
L
4433 case R_X86_64_IRELATIVE:
4434 return reloc_class_ifunc;
c434dee6 4435 case R_X86_64_RELATIVE:
1da80baa 4436 case R_X86_64_RELATIVE64:
c434dee6
AJ
4437 return reloc_class_relative;
4438 case R_X86_64_JUMP_SLOT:
4439 return reloc_class_plt;
4440 case R_X86_64_COPY:
4441 return reloc_class_copy;
4442 default:
4443 return reloc_class_normal;
4444 }
4445}
4446
70256ad8
AJ
4447/* Finish up the dynamic sections. */
4448
b34976b6 4449static bfd_boolean
351f65ca
L
4450elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4451 struct bfd_link_info *info)
70256ad8 4452{
0afcef53 4453 struct elf_x86_link_hash_table *htab;
70256ad8
AJ
4454 bfd *dynobj;
4455 asection *sdyn;
70256ad8 4456
0afcef53 4457 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6
NC
4458 if (htab == NULL)
4459 return FALSE;
4460
c434dee6 4461 dynobj = htab->elf.dynobj;
3d4d4302 4462 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
70256ad8 4463
c434dee6 4464 if (htab->elf.dynamic_sections_created)
70256ad8 4465 {
82e96e07
L
4466 bfd_byte *dyncon, *dynconend;
4467 const struct elf_backend_data *bed;
4468 bfd_size_type sizeof_dyn;
70256ad8 4469
6de2ae4a 4470 if (sdyn == NULL || htab->elf.sgot == NULL)
c434dee6 4471 abort ();
70256ad8 4472
82e96e07
L
4473 bed = get_elf_backend_data (dynobj);
4474 sizeof_dyn = bed->s->sizeof_dyn;
4475 dyncon = sdyn->contents;
4476 dynconend = sdyn->contents + sdyn->size;
4477 for (; dyncon < dynconend; dyncon += sizeof_dyn)
70256ad8
AJ
4478 {
4479 Elf_Internal_Dyn dyn;
70256ad8
AJ
4480 asection *s;
4481
82e96e07 4482 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
70256ad8
AJ
4483
4484 switch (dyn.d_tag)
4485 {
4486 default:
053579d7 4487 continue;
70256ad8
AJ
4488
4489 case DT_PLTGOT:
6de2ae4a 4490 s = htab->elf.sgotplt;
8c37241b 4491 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 4492 break;
70256ad8
AJ
4493
4494 case DT_JMPREL:
6de2ae4a 4495 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
c434dee6 4496 break;
70256ad8 4497
c434dee6 4498 case DT_PLTRELSZ:
6de2ae4a 4499 s = htab->elf.srelplt->output_section;
eea6121a 4500 dyn.d_un.d_val = s->size;
70256ad8
AJ
4501 break;
4502
67a4f2b7 4503 case DT_TLSDESC_PLT:
6de2ae4a 4504 s = htab->elf.splt;
67a4f2b7
AO
4505 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4506 + htab->tlsdesc_plt;
4507 break;
4508
4509 case DT_TLSDESC_GOT:
6de2ae4a 4510 s = htab->elf.sgot;
67a4f2b7
AO
4511 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4512 + htab->tlsdesc_got;
4513 break;
70256ad8 4514 }
c434dee6 4515
82e96e07 4516 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
70256ad8
AJ
4517 }
4518
6de2ae4a 4519 if (htab->elf.splt && htab->elf.splt->size > 0)
70256ad8 4520 {
eed180f8 4521 elf_section_data (htab->elf.splt->output_section)
765e526c 4522 ->this_hdr.sh_entsize = htab->plt.plt_entry_size;
67a4f2b7 4523
765e526c 4524 if (htab->plt.has_plt0)
67a4f2b7 4525 {
38b12349
L
4526 /* Fill in the special first entry in the procedure linkage
4527 table. */
4528 memcpy (htab->elf.splt->contents,
765e526c
L
4529 htab->lazy_plt->plt0_entry,
4530 htab->lazy_plt->plt0_entry_size);
38b12349
L
4531 /* Add offset for pushq GOT+8(%rip), since the instruction
4532 uses 6 bytes subtract this value. */
67a4f2b7 4533 bfd_put_32 (output_bfd,
6de2ae4a
L
4534 (htab->elf.sgotplt->output_section->vma
4535 + htab->elf.sgotplt->output_offset
67a4f2b7 4536 + 8
6de2ae4a
L
4537 - htab->elf.splt->output_section->vma
4538 - htab->elf.splt->output_offset
67a4f2b7 4539 - 6),
38b12349 4540 (htab->elf.splt->contents
765e526c 4541 + htab->lazy_plt->plt0_got1_offset));
38b12349
L
4542 /* Add offset for the PC-relative instruction accessing
4543 GOT+16, subtracting the offset to the end of that
4544 instruction. */
67a4f2b7 4545 bfd_put_32 (output_bfd,
38b12349
L
4546 (htab->elf.sgotplt->output_section->vma
4547 + htab->elf.sgotplt->output_offset
4548 + 16
6de2ae4a
L
4549 - htab->elf.splt->output_section->vma
4550 - htab->elf.splt->output_offset
765e526c 4551 - htab->lazy_plt->plt0_got2_insn_end),
38b12349 4552 (htab->elf.splt->contents
765e526c 4553 + htab->lazy_plt->plt0_got2_offset));
38b12349
L
4554
4555 if (htab->tlsdesc_plt)
4556 {
4557 bfd_put_64 (output_bfd, (bfd_vma) 0,
4558 htab->elf.sgot->contents + htab->tlsdesc_got);
4559
4560 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
765e526c
L
4561 htab->lazy_plt->plt0_entry,
4562 htab->lazy_plt->plt0_entry_size);
38b12349
L
4563
4564 /* Add offset for pushq GOT+8(%rip), since the
4565 instruction uses 6 bytes subtract this value. */
4566 bfd_put_32 (output_bfd,
4567 (htab->elf.sgotplt->output_section->vma
4568 + htab->elf.sgotplt->output_offset
4569 + 8
4570 - htab->elf.splt->output_section->vma
4571 - htab->elf.splt->output_offset
4572 - htab->tlsdesc_plt
4573 - 6),
4574 (htab->elf.splt->contents
4575 + htab->tlsdesc_plt
765e526c 4576 + htab->lazy_plt->plt0_got1_offset));
38b12349
L
4577 /* Add offset for the PC-relative instruction accessing
4578 GOT+TDG, where TDG stands for htab->tlsdesc_got,
4579 subtracting the offset to the end of that
4580 instruction. */
4581 bfd_put_32 (output_bfd,
4582 (htab->elf.sgot->output_section->vma
4583 + htab->elf.sgot->output_offset
4584 + htab->tlsdesc_got
4585 - htab->elf.splt->output_section->vma
4586 - htab->elf.splt->output_offset
4587 - htab->tlsdesc_plt
765e526c 4588 - htab->lazy_plt->plt0_got2_insn_end),
38b12349
L
4589 (htab->elf.splt->contents
4590 + htab->tlsdesc_plt
765e526c 4591 + htab->lazy_plt->plt0_got2_offset));
38b12349 4592 }
67a4f2b7 4593 }
70256ad8 4594 }
70256ad8 4595
0afcef53
L
4596 if (htab->plt_got != NULL && htab->plt_got->size > 0)
4597 elf_section_data (htab->plt_got->output_section)
765e526c 4598 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
38b12349 4599
0afcef53
L
4600 if (htab->plt_second != NULL && htab->plt_second->size > 0)
4601 elf_section_data (htab->plt_second->output_section)
765e526c 4602 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
0afcef53 4603 }
0ff2b86e 4604
38b12349
L
4605 /* GOT is always created in setup_gnu_properties. But it may not be
4606 needed. */
4607 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
70256ad8 4608 {
56d4289c
L
4609 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4610 {
4eca0228 4611 _bfd_error_handler
56d4289c
L
4612 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4613 return FALSE;
4614 }
4615
38b12349
L
4616 /* Set the first entry in the global offset table to the address of
4617 the dynamic section. */
4618 if (sdyn == NULL)
4619 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4620 else
4621 bfd_put_64 (output_bfd,
4622 sdyn->output_section->vma + sdyn->output_offset,
4623 htab->elf.sgotplt->contents);
4624 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4625 bfd_put_64 (output_bfd, (bfd_vma) 0,
4626 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4627 bfd_put_64 (output_bfd, (bfd_vma) 0,
4628 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4629
4630 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
4631 = GOT_ENTRY_SIZE;
c434dee6 4632 }
70256ad8 4633
e41b3a13 4634 /* Adjust .eh_frame for .plt section. */
9a2a56cc
AM
4635 if (htab->plt_eh_frame != NULL
4636 && htab->plt_eh_frame->contents != NULL)
e41b3a13
JJ
4637 {
4638 if (htab->elf.splt != NULL
4639 && htab->elf.splt->size != 0
4640 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4641 && htab->elf.splt->output_section != NULL
4642 && htab->plt_eh_frame->output_section != NULL)
4643 {
4644 bfd_vma plt_start = htab->elf.splt->output_section->vma;
4645 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4646 + htab->plt_eh_frame->output_offset
4647 + PLT_FDE_START_OFFSET;
4648 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4649 htab->plt_eh_frame->contents
4650 + PLT_FDE_START_OFFSET);
4651 }
dbaa2011 4652 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
e41b3a13
JJ
4653 {
4654 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4655 htab->plt_eh_frame,
4656 htab->plt_eh_frame->contents))
4657 return FALSE;
4658 }
4659 }
4660
fff53dae
L
4661 /* Adjust .eh_frame for .plt.got section. */
4662 if (htab->plt_got_eh_frame != NULL
4663 && htab->plt_got_eh_frame->contents != NULL)
4664 {
4665 if (htab->plt_got != NULL
4666 && htab->plt_got->size != 0
4667 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
4668 && htab->plt_got->output_section != NULL
4669 && htab->plt_got_eh_frame->output_section != NULL)
4670 {
4671 bfd_vma plt_start = htab->plt_got->output_section->vma;
4672 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
4673 + htab->plt_got_eh_frame->output_offset
4674 + PLT_FDE_START_OFFSET;
4675 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4676 htab->plt_got_eh_frame->contents
4677 + PLT_FDE_START_OFFSET);
4678 }
4679 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
4680 {
4681 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4682 htab->plt_got_eh_frame,
4683 htab->plt_got_eh_frame->contents))
4684 return FALSE;
4685 }
4686 }
4687
f2c29a16
L
4688 /* Adjust .eh_frame for the second PLT section. */
4689 if (htab->plt_second_eh_frame != NULL
4690 && htab->plt_second_eh_frame->contents != NULL)
8361ed4d 4691 {
f2c29a16
L
4692 if (htab->plt_second != NULL
4693 && htab->plt_second->size != 0
4694 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
4695 && htab->plt_second->output_section != NULL
4696 && htab->plt_second_eh_frame->output_section != NULL)
8361ed4d 4697 {
f2c29a16
L
4698 bfd_vma plt_start = htab->plt_second->output_section->vma;
4699 bfd_vma eh_frame_start
4700 = (htab->plt_second_eh_frame->output_section->vma
4701 + htab->plt_second_eh_frame->output_offset
4702 + PLT_FDE_START_OFFSET);
8361ed4d 4703 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
f2c29a16 4704 htab->plt_second_eh_frame->contents
8361ed4d
L
4705 + PLT_FDE_START_OFFSET);
4706 }
f2c29a16
L
4707 if (htab->plt_second_eh_frame->sec_info_type
4708 == SEC_INFO_TYPE_EH_FRAME)
8361ed4d
L
4709 {
4710 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
f2c29a16
L
4711 htab->plt_second_eh_frame,
4712 htab->plt_second_eh_frame->contents))
8361ed4d
L
4713 return FALSE;
4714 }
4715 }
4716
6de2ae4a
L
4717 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4718 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
8c37241b
JJ
4719 = GOT_ENTRY_SIZE;
4720
aec6b87e
L
4721 /* Fill PLT entries for undefined weak symbols in PIE. */
4722 if (bfd_link_pie (info))
4723 bfd_hash_traverse (&info->hash->table,
4724 elf_x86_64_pie_finish_undefweak_symbol,
4725 info);
4726
b34976b6 4727 return TRUE;
8d88c4ca
NC
4728}
4729
233cc9c1
L
4730/* Fill PLT/GOT entries and allocate dynamic relocations for local
4731 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4732 It has to be done before elf_link_sort_relocs is called so that
4733 dynamic relocations are properly sorted. */
4734
4735static bfd_boolean
4736elf_x86_64_output_arch_local_syms
4737 (bfd *output_bfd ATTRIBUTE_UNUSED,
4738 struct bfd_link_info *info,
4739 void *flaginfo ATTRIBUTE_UNUSED,
4740 int (*func) (void *, const char *,
4741 Elf_Internal_Sym *,
4742 asection *,
4743 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4744{
0afcef53
L
4745 struct elf_x86_link_hash_table *htab
4746 = elf_x86_hash_table (info, X86_64_ELF_DATA);
233cc9c1
L
4747 if (htab == NULL)
4748 return FALSE;
4749
4750 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4751 htab_traverse (htab->loc_hash_table,
4752 elf_x86_64_finish_local_dynamic_symbol,
4753 info);
4754
4755 return TRUE;
4756}
4757
38b12349 4758/* Forward declaration. */
765e526c 4759static const struct elf_x86_lazy_plt_layout elf_x86_64_nacl_plt;
38b12349
L
4760
4761/* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4762 dynamic relocations. */
4763
4764static long
4765elf_x86_64_get_synthetic_symtab (bfd *abfd,
4766 long symcount ATTRIBUTE_UNUSED,
4767 asymbol **syms ATTRIBUTE_UNUSED,
4768 long dynsymcount,
4769 asymbol **dynsyms,
4770 asymbol **ret)
4771{
f493882d 4772 long count, i, n;
38b12349 4773 int j;
38b12349 4774 bfd_byte *plt_contents;
f493882d 4775 long relsize;
765e526c
L
4776 const struct elf_x86_lazy_plt_layout *lazy_plt;
4777 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4778 const struct elf_x86_lazy_plt_layout *lazy_bnd_plt;
4779 const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_plt;
4780 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4781 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
38b12349 4782 asection *plt;
f493882d
L
4783 enum elf_x86_plt_type plt_type;
4784 struct elf_x86_plt plts[] =
144bed8d 4785 {
38b12349
L
4786 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4787 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
f2c29a16
L
4788 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4789 { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
dd9e66ee 4790 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
38b12349 4791 };
144bed8d 4792
38b12349 4793 *ret = NULL;
144bed8d 4794
38b12349
L
4795 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4796 return 0;
144bed8d 4797
38b12349
L
4798 if (dynsymcount <= 0)
4799 return 0;
cca5b8b6 4800
38b12349
L
4801 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4802 if (relsize <= 0)
4803 return -1;
4804
38b12349 4805 if (get_elf_x86_64_backend_data (abfd)->os == is_normal)
144bed8d 4806 {
38b12349
L
4807 lazy_plt = &elf_x86_64_lazy_plt;
4808 non_lazy_plt = &elf_x86_64_non_lazy_plt;
4809 lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
4810 non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
ee2fdd6f
L
4811 if (ABI_64_P (abfd))
4812 {
4813 lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
4814 non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
4815 }
4816 else
4817 {
4818 lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
4819 non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
4820 }
38b12349
L
4821 }
4822 else
4823 {
4824 lazy_plt = &elf_x86_64_nacl_plt;
4825 non_lazy_plt = NULL;
4826 lazy_bnd_plt = NULL;
4827 non_lazy_bnd_plt = NULL;
ee2fdd6f
L
4828 lazy_ibt_plt = NULL;
4829 non_lazy_ibt_plt = NULL;
38b12349 4830 }
144bed8d 4831
38b12349
L
4832 count = 0;
4833 for (j = 0; plts[j].name != NULL; j++)
4834 {
4835 plt = bfd_get_section_by_name (abfd, plts[j].name);
90efb642 4836 if (plt == NULL || plt->size == 0)
6f25f223 4837 continue;
533d0af0 4838
38b12349
L
4839 /* Get the PLT section contents. */
4840 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4841 if (plt_contents == NULL)
4842 break;
4843 if (!bfd_get_section_contents (abfd, (asection *) plt,
4844 plt_contents, 0, plt->size))
4845 {
4846 free (plt_contents);
4847 break;
4848 }
144bed8d 4849
38b12349
L
4850 /* Check what kind of PLT it is. */
4851 plt_type = plt_unknown;
90efb642
L
4852 if (plts[j].type == plt_unknown
4853 && (plt->size >= (lazy_plt->plt_entry_size
4854 + lazy_plt->plt_entry_size)))
144bed8d 4855 {
38b12349
L
4856 /* Match lazy PLT first. Need to check the first two
4857 instructions. */
4858 if ((memcmp (plt_contents, lazy_plt->plt0_entry,
4859 lazy_plt->plt0_got1_offset) == 0)
4860 && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
4861 2) == 0))
4862 plt_type = plt_lazy;
4863 else if (lazy_bnd_plt != NULL
4864 && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
4865 lazy_bnd_plt->plt0_got1_offset) == 0)
4866 && (memcmp (plt_contents + 6,
4867 lazy_bnd_plt->plt0_entry + 6, 3) == 0))
ec1f73bb 4868 {
f2c29a16 4869 plt_type = plt_lazy | plt_second;
ee2fdd6f
L
4870 /* The fist entry in the lazy IBT PLT is the same as the
4871 lazy BND PLT. */
4872 if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
4873 lazy_ibt_plt->plt_entry,
4874 lazy_ibt_plt->plt_got_offset) == 0))
4875 lazy_plt = lazy_ibt_plt;
4876 else
4877 lazy_plt = lazy_bnd_plt;
ec1f73bb 4878 }
144bed8d 4879 }
fca6ae69 4880
38b12349 4881 if (non_lazy_plt != NULL
90efb642
L
4882 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4883 && plt->size >= non_lazy_plt->plt_entry_size)
38b12349
L
4884 {
4885 /* Match non-lazy PLT. */
4886 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4887 non_lazy_plt->plt_got_offset) == 0)
4888 plt_type = plt_non_lazy;
4889 }
4890
ee2fdd6f 4891 if (plt_type == plt_unknown || plt_type == plt_second)
38b12349 4892 {
ee2fdd6f 4893 if (non_lazy_bnd_plt != NULL
90efb642 4894 && plt->size >= non_lazy_bnd_plt->plt_entry_size
ee2fdd6f
L
4895 && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
4896 non_lazy_bnd_plt->plt_got_offset) == 0))
38b12349 4897 {
ee2fdd6f 4898 /* Match BND PLT. */
f2c29a16 4899 plt_type = plt_second;
38b12349
L
4900 non_lazy_plt = non_lazy_bnd_plt;
4901 }
ee2fdd6f 4902 else if (non_lazy_ibt_plt != NULL
90efb642 4903 && plt->size >= non_lazy_ibt_plt->plt_entry_size
ee2fdd6f
L
4904 && (memcmp (plt_contents,
4905 non_lazy_ibt_plt->plt_entry,
4906 non_lazy_ibt_plt->plt_got_offset) == 0))
4907 {
4908 /* Match IBT PLT. */
4909 plt_type = plt_second;
4910 non_lazy_plt = non_lazy_ibt_plt;
4911 }
38b12349
L
4912 }
4913
4914 if (plt_type == plt_unknown)
37c0b6ee
L
4915 {
4916 free (plt_contents);
4917 continue;
4918 }
38b12349
L
4919
4920 plts[j].sec = plt;
4921 plts[j].type = plt_type;
4922
4923 if ((plt_type & plt_lazy))
4924 {
4925 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4926 plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
4927 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4928 /* Skip PLT0 in lazy PLT. */
4929 i = 1;
4930 }
4931 else
4932 {
4933 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4934 plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
4935 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4936 i = 0;
4937 }
4938
f2c29a16
L
4939 /* Skip lazy PLT when the second PLT is used. */
4940 if (plt_type == (plt_lazy | plt_second))
38b12349
L
4941 plts[j].count = 0;
4942 else
4943 {
4944 n = plt->size / plts[j].plt_entry_size;
4945 plts[j].count = n;
4946 count += n - i;
4947 }
4948
4949 plts[j].contents = plt_contents;
144bed8d
L
4950 }
4951
f493882d
L
4952 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4953 (bfd_vma) 0, plts, dynsyms,
4954 ret);
0ff2b86e
L
4955}
4956
d2b2c203
DJ
4957/* Handle an x86-64 specific section when reading an object file. This
4958 is called when elfcode.h finds a section with an unknown type. */
4959
4960static bfd_boolean
0c723101
L
4961elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
4962 const char *name, int shindex)
d2b2c203
DJ
4963{
4964 if (hdr->sh_type != SHT_X86_64_UNWIND)
4965 return FALSE;
4966
6dc132d9 4967 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
4968 return FALSE;
4969
4970 return TRUE;
4971}
4972
3b22753a
L
4973/* Hook called by the linker routine which adds symbols from an object
4974 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4975 of .bss. */
4976
4977static bfd_boolean
351f65ca 4978elf_x86_64_add_symbol_hook (bfd *abfd,
a43942db 4979 struct bfd_link_info *info ATTRIBUTE_UNUSED,
351f65ca
L
4980 Elf_Internal_Sym *sym,
4981 const char **namep ATTRIBUTE_UNUSED,
4982 flagword *flagsp ATTRIBUTE_UNUSED,
4983 asection **secp,
4984 bfd_vma *valp)
3b22753a
L
4985{
4986 asection *lcomm;
4987
4988 switch (sym->st_shndx)
4989 {
4990 case SHN_X86_64_LCOMMON:
4991 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4992 if (lcomm == NULL)
4993 {
4994 lcomm = bfd_make_section_with_flags (abfd,
4995 "LARGE_COMMON",
4996 (SEC_ALLOC
4997 | SEC_IS_COMMON
4998 | SEC_LINKER_CREATED));
4999 if (lcomm == NULL)
5000 return FALSE;
5001 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
5002 }
5003 *secp = lcomm;
5004 *valp = sym->st_size;
c35bdf6e 5005 return TRUE;
3b22753a 5006 }
d8045f23 5007
3b22753a
L
5008 return TRUE;
5009}
5010
5011
5012/* Given a BFD section, try to locate the corresponding ELF section
5013 index. */
5014
5015static bfd_boolean
351f65ca
L
5016elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5017 asection *sec, int *index_return)
3b22753a
L
5018{
5019 if (sec == &_bfd_elf_large_com_section)
5020 {
91d6fa6a 5021 *index_return = SHN_X86_64_LCOMMON;
3b22753a
L
5022 return TRUE;
5023 }
5024 return FALSE;
5025}
5026
5027/* Process a symbol. */
5028
5029static void
351f65ca
L
5030elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5031 asymbol *asym)
3b22753a
L
5032{
5033 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5034
5035 switch (elfsym->internal_elf_sym.st_shndx)
5036 {
5037 case SHN_X86_64_LCOMMON:
5038 asym->section = &_bfd_elf_large_com_section;
5039 asym->value = elfsym->internal_elf_sym.st_size;
5040 /* Common symbol doesn't set BSF_GLOBAL. */
5041 asym->flags &= ~BSF_GLOBAL;
5042 break;
5043 }
5044}
5045
5046static bfd_boolean
351f65ca 5047elf_x86_64_common_definition (Elf_Internal_Sym *sym)
3b22753a
L
5048{
5049 return (sym->st_shndx == SHN_COMMON
5050 || sym->st_shndx == SHN_X86_64_LCOMMON);
5051}
5052
5053static unsigned int
351f65ca 5054elf_x86_64_common_section_index (asection *sec)
3b22753a
L
5055{
5056 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5057 return SHN_COMMON;
5058 else
5059 return SHN_X86_64_LCOMMON;
5060}
5061
5062static asection *
351f65ca 5063elf_x86_64_common_section (asection *sec)
3b22753a
L
5064{
5065 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5066 return bfd_com_section_ptr;
5067 else
5068 return &_bfd_elf_large_com_section;
5069}
5070
5071static bfd_boolean
5d13b3b3
AM
5072elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5073 const Elf_Internal_Sym *sym,
351f65ca 5074 asection **psec,
5d13b3b3
AM
5075 bfd_boolean newdef,
5076 bfd_boolean olddef,
351f65ca 5077 bfd *oldbfd,
5d13b3b3 5078 const asection *oldsec)
3b22753a
L
5079{
5080 /* A normal common symbol and a large common symbol result in a
00492999
L
5081 normal common symbol. We turn the large common symbol into a
5082 normal one. */
5d13b3b3 5083 if (!olddef
3b22753a 5084 && h->root.type == bfd_link_hash_common
5d13b3b3
AM
5085 && !newdef
5086 && bfd_is_com_section (*psec)
5087 && oldsec != *psec)
3b22753a 5088 {
00492999 5089 if (sym->st_shndx == SHN_COMMON
5d13b3b3 5090 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
00492999
L
5091 {
5092 h->root.u.c.p->section
5093 = bfd_make_section_old_way (oldbfd, "COMMON");
5094 h->root.u.c.p->section->flags = SEC_ALLOC;
5095 }
5096 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5d13b3b3
AM
5097 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5098 *psec = bfd_com_section_ptr;
3b22753a
L
5099 }
5100
5101 return TRUE;
5102}
5103
5104static int
351f65ca
L
5105elf_x86_64_additional_program_headers (bfd *abfd,
5106 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
5107{
5108 asection *s;
9a2e389a 5109 int count = 0;
3b22753a
L
5110
5111 /* Check to see if we need a large readonly segment. */
5112 s = bfd_get_section_by_name (abfd, ".lrodata");
5113 if (s && (s->flags & SEC_LOAD))
5114 count++;
5115
5116 /* Check to see if we need a large data segment. Since .lbss sections
5117 is placed right after the .bss section, there should be no need for
5118 a large data segment just because of .lbss. */
5119 s = bfd_get_section_by_name (abfd, ".ldata");
5120 if (s && (s->flags & SEC_LOAD))
5121 count++;
5122
5123 return count;
5124}
5125
c543bf9a
L
5126/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5127
5128static bfd_boolean
5129elf_x86_64_relocs_compatible (const bfd_target *input,
5130 const bfd_target *output)
5131{
5132 return ((xvec_get_elf_backend_data (input)->s->elfclass
5133 == xvec_get_elf_backend_data (output)->s->elfclass)
5134 && _bfd_elf_relocs_compatible (input, output));
5135}
5136
38b12349
L
5137/* Set up x86-64 GNU properties. Return the first relocatable ELF input
5138 with GNU properties if found. Otherwise, return NULL. */
5139
5140static bfd *
5141elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
5142{
1de031c8 5143 struct elf_x86_init_table init_table;
ee2fdd6f 5144
78984959
L
5145 if ((int) R_X86_64_standard >= (int) R_X86_64_converted_reloc_bit
5146 || (int) R_X86_64_max <= (int) R_X86_64_converted_reloc_bit
5147 || ((int) (R_X86_64_GNU_VTINHERIT | R_X86_64_converted_reloc_bit)
5148 != (int) R_X86_64_GNU_VTINHERIT)
5149 || ((int) (R_X86_64_GNU_VTENTRY | R_X86_64_converted_reloc_bit)
5150 != (int) R_X86_64_GNU_VTENTRY))
5151 abort ();
5152
1de031c8 5153 init_table.is_vxworks = FALSE;
a6798bab 5154 if (get_elf_x86_64_backend_data (info->output_bfd)->os == is_normal)
73784fa5 5155 {
a6798bab 5156 if (info->bndplt)
ee2fdd6f 5157 {
1de031c8
L
5158 init_table.lazy_plt = &elf_x86_64_lazy_bnd_plt;
5159 init_table.non_lazy_plt = &elf_x86_64_non_lazy_bnd_plt;
ee2fdd6f
L
5160 }
5161 else
5162 {
1de031c8
L
5163 init_table.lazy_plt = &elf_x86_64_lazy_plt;
5164 init_table.non_lazy_plt = &elf_x86_64_non_lazy_plt;
ee2fdd6f 5165 }
38b12349 5166
a6798bab 5167 if (ABI_64_P (info->output_bfd))
38b12349 5168 {
1de031c8
L
5169 init_table.lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
5170 init_table.non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
38b12349
L
5171 }
5172 else
5173 {
1de031c8
L
5174 init_table.lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
5175 init_table.non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
38b12349 5176 }
1de031c8 5177 init_table.normal_target = TRUE;
38b12349
L
5178 }
5179 else
5180 {
1de031c8
L
5181 init_table.lazy_plt = &elf_x86_64_nacl_plt;
5182 init_table.non_lazy_plt = NULL;
5183 init_table.lazy_ibt_plt = NULL;
5184 init_table.non_lazy_ibt_plt = NULL;
5185 init_table.normal_target = FALSE;
38b12349
L
5186 }
5187
7a382c1c
L
5188 if (ABI_64_P (info->output_bfd))
5189 {
1de031c8
L
5190 init_table.r_info = elf64_r_info;
5191 init_table.r_sym = elf64_r_sym;
7a382c1c
L
5192 }
5193 else
5194 {
1de031c8
L
5195 init_table.r_info = elf32_r_info;
5196 init_table.r_sym = elf32_r_sym;
7a382c1c
L
5197 }
5198
1de031c8 5199 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
38b12349
L
5200}
5201
9a2e389a 5202static const struct bfd_elf_special_section
46bed679 5203elf_x86_64_special_sections[]=
3b22753a 5204{
0112cd26
NC
5205 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5206 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5207 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
5208 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5209 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5210 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5211 { NULL, 0, 0, 0, 0 }
3b22753a
L
5212};
5213
6d00b590 5214#define TARGET_LITTLE_SYM x86_64_elf64_vec
70256ad8
AJ
5215#define TARGET_LITTLE_NAME "elf64-x86-64"
5216#define ELF_ARCH bfd_arch_i386
ae95ffa6 5217#define ELF_TARGET_ID X86_64_ELF_DATA
70256ad8 5218#define ELF_MACHINE_CODE EM_X86_64
f7661549 5219#define ELF_MAXPAGESIZE 0x200000
2043964e 5220#define ELF_MINPAGESIZE 0x1000
24718e3b 5221#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
5222
5223#define elf_backend_can_gc_sections 1
51b64d56 5224#define elf_backend_can_refcount 1
70256ad8
AJ
5225#define elf_backend_want_got_plt 1
5226#define elf_backend_plt_readonly 1
5227#define elf_backend_want_plt_sym 0
5228#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 5229#define elf_backend_rela_normal 1
e41b3a13 5230#define elf_backend_plt_alignment 4
f7483970 5231#define elf_backend_extern_protected_data 1
bedfd056 5232#define elf_backend_caches_rawsize 1
64f52338 5233#define elf_backend_dtrel_excludes_plt 1
5474d94f 5234#define elf_backend_want_dynrelro 1
70256ad8 5235
351f65ca 5236#define elf_info_to_howto elf_x86_64_info_to_howto
70256ad8 5237
351f65ca 5238#define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
157090f7 5239#define bfd_elf64_bfd_reloc_name_lookup \
351f65ca 5240 elf_x86_64_reloc_name_lookup
70256ad8 5241
c543bf9a 5242#define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
351f65ca 5243#define elf_backend_check_relocs elf_x86_64_check_relocs
38b12349 5244#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
351f65ca
L
5245#define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5246#define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
233cc9c1 5247#define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
351f65ca
L
5248#define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5249#define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
8fd79e71
L
5250#ifdef CORE_HEADER
5251#define elf_backend_write_core_note elf_x86_64_write_core_note
5252#endif
351f65ca
L
5253#define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5254#define elf_backend_relocate_section elf_x86_64_relocate_section
74541ad4 5255#define elf_backend_init_index_section _bfd_elf_init_1_index_section
407443a3 5256#define elf_backend_object_p elf64_x86_64_elf_object_p
0ff2b86e 5257#define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
8d88c4ca 5258
d2b2c203 5259#define elf_backend_section_from_shdr \
351f65ca 5260 elf_x86_64_section_from_shdr
d2b2c203 5261
3b22753a 5262#define elf_backend_section_from_bfd_section \
351f65ca 5263 elf_x86_64_elf_section_from_bfd_section
3b22753a 5264#define elf_backend_add_symbol_hook \
351f65ca 5265 elf_x86_64_add_symbol_hook
3b22753a 5266#define elf_backend_symbol_processing \
351f65ca 5267 elf_x86_64_symbol_processing
3b22753a 5268#define elf_backend_common_section_index \
351f65ca 5269 elf_x86_64_common_section_index
3b22753a 5270#define elf_backend_common_section \
351f65ca 5271 elf_x86_64_common_section
3b22753a 5272#define elf_backend_common_definition \
351f65ca 5273 elf_x86_64_common_definition
3b22753a 5274#define elf_backend_merge_symbol \
351f65ca 5275 elf_x86_64_merge_symbol
3b22753a 5276#define elf_backend_special_sections \
351f65ca 5277 elf_x86_64_special_sections
3b22753a 5278#define elf_backend_additional_program_headers \
351f65ca 5279 elf_x86_64_additional_program_headers
38b12349
L
5280#define elf_backend_setup_gnu_properties \
5281 elf_x86_64_link_setup_gnu_properties
3b22753a 5282
8d88c4ca 5283#include "elf64-target.h"
9d7cbccd 5284
6036f486
ES
5285/* CloudABI support. */
5286
5287#undef TARGET_LITTLE_SYM
5288#define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
5289#undef TARGET_LITTLE_NAME
5290#define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
5291
5292#undef ELF_OSABI
5293#define ELF_OSABI ELFOSABI_CLOUDABI
5294
5295#undef elf64_bed
5296#define elf64_bed elf64_x86_64_cloudabi_bed
5297
5298#include "elf64-target.h"
5299
9d7cbccd
NC
5300/* FreeBSD support. */
5301
5302#undef TARGET_LITTLE_SYM
6d00b590 5303#define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
9d7cbccd
NC
5304#undef TARGET_LITTLE_NAME
5305#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5306
d1036acb
L
5307#undef ELF_OSABI
5308#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd 5309
9d7cbccd
NC
5310#undef elf64_bed
5311#define elf64_bed elf64_x86_64_fbsd_bed
5312
5313#include "elf64-target.h"
8a9036a4 5314
a6cc6b3b
RO
5315/* Solaris 2 support. */
5316
5317#undef TARGET_LITTLE_SYM
6d00b590 5318#define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
a6cc6b3b
RO
5319#undef TARGET_LITTLE_NAME
5320#define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5321
5322/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5323 objects won't be recognized. */
5324#undef ELF_OSABI
5325
5326#undef elf64_bed
5327#define elf64_bed elf64_x86_64_sol2_bed
5328
7dc98aea
RO
5329/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5330 boundary. */
84865015 5331#undef elf_backend_static_tls_alignment
7dc98aea
RO
5332#define elf_backend_static_tls_alignment 16
5333
a6cc6b3b
RO
5334/* The Solaris 2 ABI requires a plt symbol on all platforms.
5335
5336 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5337 File, p.63. */
84865015 5338#undef elf_backend_want_plt_sym
a6cc6b3b
RO
5339#define elf_backend_want_plt_sym 1
5340
84865015
NC
5341#undef elf_backend_strtab_flags
5342#define elf_backend_strtab_flags SHF_STRINGS
5343
5344static bfd_boolean
5522f910
NC
5345elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
5346 bfd *obfd ATTRIBUTE_UNUSED,
5347 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
5348 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
84865015
NC
5349{
5350 /* PR 19938: FIXME: Need to add code for setting the sh_info
5351 and sh_link fields of Solaris specific section types. */
5352 return FALSE;
5353}
5354
5522f910
NC
5355#undef elf_backend_copy_special_section_fields
5356#define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
84865015 5357
a6cc6b3b
RO
5358#include "elf64-target.h"
5359
8059fb19
RM
5360/* Native Client support. */
5361
64b384e1
RM
5362static bfd_boolean
5363elf64_x86_64_nacl_elf_object_p (bfd *abfd)
5364{
5365 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
5366 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
5367 return TRUE;
5368}
5369
8059fb19 5370#undef TARGET_LITTLE_SYM
6d00b590 5371#define TARGET_LITTLE_SYM x86_64_elf64_nacl_vec
8059fb19
RM
5372#undef TARGET_LITTLE_NAME
5373#define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
5374#undef elf64_bed
5375#define elf64_bed elf64_x86_64_nacl_bed
5376
5377#undef ELF_MAXPAGESIZE
5378#undef ELF_MINPAGESIZE
5379#undef ELF_COMMONPAGESIZE
5380#define ELF_MAXPAGESIZE 0x10000
5381#define ELF_MINPAGESIZE 0x10000
5382#define ELF_COMMONPAGESIZE 0x10000
5383
5384/* Restore defaults. */
5385#undef ELF_OSABI
5386#undef elf_backend_static_tls_alignment
5387#undef elf_backend_want_plt_sym
5388#define elf_backend_want_plt_sym 0
84865015 5389#undef elf_backend_strtab_flags
5522f910 5390#undef elf_backend_copy_special_section_fields
8059fb19
RM
5391
5392/* NaCl uses substantially different PLT entries for the same effects. */
5393
5394#undef elf_backend_plt_alignment
5395#define elf_backend_plt_alignment 5
5396#define NACL_PLT_ENTRY_SIZE 64
5397#define NACLMASK 0xe0 /* 32-byte alignment mask. */
5398
5399static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
5400 {
5401 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
5402 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
5403 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5404 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5405 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5406
ea2d813e 5407 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
70cc877f 5408 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
ea2d813e
RM
5409
5410 /* 32 bytes of nop to pad out to the standard size. */
3ddf1bdd 5411 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19 5412 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
3ddf1bdd 5413 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19 5414 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
3ddf1bdd 5415 0x66, /* excess data16 prefix */
ea2d813e 5416 0x90 /* nop */
8059fb19
RM
5417 };
5418
5419static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5420 {
5421 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
5422 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5423 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5424 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5425
5426 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
3ddf1bdd 5427 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19
RM
5428 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5429
5430 /* Lazy GOT entries point here (32-byte aligned). */
5431 0x68, /* pushq immediate */
5432 0, 0, 0, 0, /* replaced with index into relocation table. */
5433 0xe9, /* jmp relative */
5434 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
5435
5436 /* 22 bytes of nop to pad out to the standard size. */
3ddf1bdd 5437 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19
RM
5438 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5439 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
5440 };
5441
5442/* .eh_frame covering the .plt section. */
5443
5444static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
5445 {
5446#if (PLT_CIE_LENGTH != 20 \
5447 || PLT_FDE_LENGTH != 36 \
5448 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
5449 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
5450# error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
5451#endif
5452 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5453 0, 0, 0, 0, /* CIE ID */
5454 1, /* CIE version */
5455 'z', 'R', 0, /* Augmentation string */
5456 1, /* Code alignment factor */
5457 0x78, /* Data alignment factor */
5458 16, /* Return address column */
5459 1, /* Augmentation size */
5460 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5461 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
5462 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
5463 DW_CFA_nop, DW_CFA_nop,
5464
5465 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5466 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
5467 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
5468 0, 0, 0, 0, /* .plt size goes here */
5469 0, /* Augmentation size */
5470 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
5471 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5472 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
5473 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5474 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5475 13, /* Block length */
5476 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
5477 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
5478 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5479 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
5480 DW_CFA_nop, DW_CFA_nop
5481 };
5482
765e526c 5483static const struct elf_x86_lazy_plt_layout elf_x86_64_nacl_plt =
8059fb19
RM
5484 {
5485 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
765e526c 5486 NACL_PLT_ENTRY_SIZE, /* plt0_entry_size */
8059fb19
RM
5487 elf_x86_64_nacl_plt_entry, /* plt_entry */
5488 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5489 2, /* plt0_got1_offset */
5490 9, /* plt0_got2_offset */
5491 13, /* plt0_got2_insn_end */
5492 3, /* plt_got_offset */
5493 33, /* plt_reloc_offset */
5494 38, /* plt_plt_offset */
5495 7, /* plt_got_insn_size */
5496 42, /* plt_plt_insn_end */
5497 32, /* plt_lazy_offset */
a6798bab
L
5498 elf_x86_64_nacl_plt0_entry, /* pic_plt0_entry */
5499 elf_x86_64_nacl_plt_entry, /* pic_plt_entry */
8059fb19 5500 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
38b12349
L
5501 sizeof (elf_x86_64_nacl_eh_frame_plt) /* eh_frame_plt_size */
5502 };
5503
5504static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
5505 {
5506 is_nacl /* os */
8059fb19
RM
5507 };
5508
5509#undef elf_backend_arch_data
5510#define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
5511
64b384e1
RM
5512#undef elf_backend_object_p
5513#define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
5a68afcf
RM
5514#undef elf_backend_modify_segment_map
5515#define elf_backend_modify_segment_map nacl_modify_segment_map
5516#undef elf_backend_modify_program_headers
5517#define elf_backend_modify_program_headers nacl_modify_program_headers
887badb3
RM
5518#undef elf_backend_final_write_processing
5519#define elf_backend_final_write_processing nacl_final_write_processing
5a68afcf 5520
8059fb19
RM
5521#include "elf64-target.h"
5522
5523/* Native Client x32 support. */
5524
64b384e1
RM
5525static bfd_boolean
5526elf32_x86_64_nacl_elf_object_p (bfd *abfd)
5527{
5528 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
5529 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
5530 return TRUE;
5531}
5532
8059fb19 5533#undef TARGET_LITTLE_SYM
6d00b590 5534#define TARGET_LITTLE_SYM x86_64_elf32_nacl_vec
8059fb19
RM
5535#undef TARGET_LITTLE_NAME
5536#define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
5537#undef elf32_bed
5538#define elf32_bed elf32_x86_64_nacl_bed
5539
8059fb19
RM
5540#define bfd_elf32_bfd_reloc_type_lookup \
5541 elf_x86_64_reloc_type_lookup
5542#define bfd_elf32_bfd_reloc_name_lookup \
5543 elf_x86_64_reloc_name_lookup
b7365e5d
L
5544#define bfd_elf32_get_synthetic_symtab \
5545 elf_x86_64_get_synthetic_symtab
8059fb19
RM
5546
5547#undef elf_backend_object_p
5548#define elf_backend_object_p \
64b384e1 5549 elf32_x86_64_nacl_elf_object_p
8059fb19
RM
5550
5551#undef elf_backend_bfd_from_remote_memory
5552#define elf_backend_bfd_from_remote_memory \
5553 _bfd_elf32_bfd_from_remote_memory
5554
5555#undef elf_backend_size_info
5556#define elf_backend_size_info \
5557 _bfd_elf32_size_info
5558
5559#include "elf32-target.h"
5560
5561/* Restore defaults. */
5a68afcf 5562#undef elf_backend_object_p
8059fb19 5563#define elf_backend_object_p elf64_x86_64_elf_object_p
5a68afcf
RM
5564#undef elf_backend_bfd_from_remote_memory
5565#undef elf_backend_size_info
5566#undef elf_backend_modify_segment_map
5567#undef elf_backend_modify_program_headers
887badb3 5568#undef elf_backend_final_write_processing
8059fb19 5569
8a9036a4
L
5570/* Intel L1OM support. */
5571
5572static bfd_boolean
5573elf64_l1om_elf_object_p (bfd *abfd)
5574{
5575 /* Set the right machine number for an L1OM elf64 file. */
5576 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
5577 return TRUE;
5578}
5579
5580#undef TARGET_LITTLE_SYM
6d00b590 5581#define TARGET_LITTLE_SYM l1om_elf64_vec
8a9036a4
L
5582#undef TARGET_LITTLE_NAME
5583#define TARGET_LITTLE_NAME "elf64-l1om"
5584#undef ELF_ARCH
5585#define ELF_ARCH bfd_arch_l1om
5586
5587#undef ELF_MACHINE_CODE
5588#define ELF_MACHINE_CODE EM_L1OM
5589
5590#undef ELF_OSABI
5591
5592#undef elf64_bed
5593#define elf64_bed elf64_l1om_bed
5594
5595#undef elf_backend_object_p
5596#define elf_backend_object_p elf64_l1om_elf_object_p
5597
8059fb19
RM
5598/* Restore defaults. */
5599#undef ELF_MAXPAGESIZE
5600#undef ELF_MINPAGESIZE
5601#undef ELF_COMMONPAGESIZE
5602#define ELF_MAXPAGESIZE 0x200000
5603#define ELF_MINPAGESIZE 0x1000
5604#define ELF_COMMONPAGESIZE 0x1000
5605#undef elf_backend_plt_alignment
5606#define elf_backend_plt_alignment 4
5607#undef elf_backend_arch_data
5608#define elf_backend_arch_data &elf_x86_64_arch_bed
1a0c107f 5609
8a9036a4
L
5610#include "elf64-target.h"
5611
5612/* FreeBSD L1OM support. */
5613
5614#undef TARGET_LITTLE_SYM
6d00b590 5615#define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
8a9036a4
L
5616#undef TARGET_LITTLE_NAME
5617#define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
5618
5619#undef ELF_OSABI
5620#define ELF_OSABI ELFOSABI_FREEBSD
5621
5622#undef elf64_bed
5623#define elf64_bed elf64_l1om_fbsd_bed
5624
8a9036a4 5625#include "elf64-target.h"
351f65ca 5626
7a9068fe
L
5627/* Intel K1OM support. */
5628
5629static bfd_boolean
5630elf64_k1om_elf_object_p (bfd *abfd)
5631{
5632 /* Set the right machine number for an K1OM elf64 file. */
5633 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
5634 return TRUE;
5635}
5636
5637#undef TARGET_LITTLE_SYM
6d00b590 5638#define TARGET_LITTLE_SYM k1om_elf64_vec
7a9068fe
L
5639#undef TARGET_LITTLE_NAME
5640#define TARGET_LITTLE_NAME "elf64-k1om"
5641#undef ELF_ARCH
5642#define ELF_ARCH bfd_arch_k1om
5643
5644#undef ELF_MACHINE_CODE
5645#define ELF_MACHINE_CODE EM_K1OM
5646
5647#undef ELF_OSABI
5648
5649#undef elf64_bed
5650#define elf64_bed elf64_k1om_bed
5651
5652#undef elf_backend_object_p
5653#define elf_backend_object_p elf64_k1om_elf_object_p
5654
5655#undef elf_backend_static_tls_alignment
5656
5657#undef elf_backend_want_plt_sym
5658#define elf_backend_want_plt_sym 0
5659
5660#include "elf64-target.h"
5661
5662/* FreeBSD K1OM support. */
5663
5664#undef TARGET_LITTLE_SYM
6d00b590 5665#define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
7a9068fe
L
5666#undef TARGET_LITTLE_NAME
5667#define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
5668
5669#undef ELF_OSABI
5670#define ELF_OSABI ELFOSABI_FREEBSD
5671
5672#undef elf64_bed
5673#define elf64_bed elf64_k1om_fbsd_bed
5674
5675#include "elf64-target.h"
5676
351f65ca
L
5677/* 32bit x86-64 support. */
5678
351f65ca 5679#undef TARGET_LITTLE_SYM
6d00b590 5680#define TARGET_LITTLE_SYM x86_64_elf32_vec
351f65ca
L
5681#undef TARGET_LITTLE_NAME
5682#define TARGET_LITTLE_NAME "elf32-x86-64"
8059fb19 5683#undef elf32_bed
351f65ca
L
5684
5685#undef ELF_ARCH
5686#define ELF_ARCH bfd_arch_i386
5687
5688#undef ELF_MACHINE_CODE
5689#define ELF_MACHINE_CODE EM_X86_64
5690
351f65ca
L
5691#undef ELF_OSABI
5692
351f65ca
L
5693#undef elf_backend_object_p
5694#define elf_backend_object_p \
5695 elf32_x86_64_elf_object_p
5696
5697#undef elf_backend_bfd_from_remote_memory
5698#define elf_backend_bfd_from_remote_memory \
5699 _bfd_elf32_bfd_from_remote_memory
5700
5701#undef elf_backend_size_info
5702#define elf_backend_size_info \
5703 _bfd_elf32_size_info
5704
5705#include "elf32-target.h"
This page took 1.716293 seconds and 4 git commands to generate.