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