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