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