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