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