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