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