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