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