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