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