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