Support R_PPC64_ADDR64_LOCAL
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
351f65ca 1/* X86-64 specific support for ELF
4dfe6ac6 2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
61315175 3 2010, 2011, 2012, 2013
9f973f28 4 Free Software Foundation, Inc.
8d88c4ca
NC
5 Contributed by Jan Hubicka <jh@suse.cz>.
6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
8d88c4ca 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
8d88c4ca 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
8d88c4ca 18
ae9a127f
NC
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
cd123cb7
NC
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
8d88c4ca 23
8d88c4ca 24#include "sysdep.h"
3db64b00 25#include "bfd.h"
c434dee6 26#include "bfdlink.h"
8d88c4ca
NC
27#include "libbfd.h"
28#include "elf-bfd.h"
5a68afcf 29#include "elf-nacl.h"
142411ca 30#include "bfd_stdint.h"
c25bc9fc
L
31#include "objalloc.h"
32#include "hashtab.h"
e41b3a13 33#include "dwarf2.h"
d7921315 34#include "libiberty.h"
8d88c4ca
NC
35
36#include "elf/x86-64.h"
37
8fd79e71
L
38#ifdef CORE_HEADER
39#include <stdarg.h>
40#include CORE_HEADER
41#endif
42
8d88c4ca
NC
43/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
44#define MINUS_ONE (~ (bfd_vma) 0)
45
351f65ca
L
46/* Since both 32-bit and 64-bit x86-64 encode relocation type in the
47 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
48 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
49 since they are the same. */
50
51#define ABI_64_P(abfd) \
52 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
53
8d88c4ca 54/* The relocation "howto" table. Order of fields:
7b81dfbb
AJ
55 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
56 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
70256ad8
AJ
57static reloc_howto_type x86_64_elf_howto_table[] =
58{
b34976b6
AM
59 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
60 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
61 FALSE),
62 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
64 FALSE),
65 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
66 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
67 TRUE),
68 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
69 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
70 FALSE),
71 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
72 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
73 TRUE),
74 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
76 FALSE),
77 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
78 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
79 MINUS_ONE, FALSE),
80 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
82 MINUS_ONE, FALSE),
83 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
85 MINUS_ONE, FALSE),
86 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
87 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
88 0xffffffff, TRUE),
89 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
90 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
91 FALSE),
92 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
93 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
94 FALSE),
95 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
96 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
b0360d8c 97 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
b34976b6 98 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
ac2aa337 99 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
b34976b6
AM
100 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
101 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
102 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
103 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
104 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
105 MINUS_ONE, FALSE),
106 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
108 MINUS_ONE, FALSE),
109 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
110 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
111 MINUS_ONE, FALSE),
112 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
114 0xffffffff, TRUE),
115 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
116 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
117 0xffffffff, TRUE),
ac2aa337 118 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
b34976b6
AM
119 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
120 0xffffffff, FALSE),
121 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
122 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
123 0xffffffff, TRUE),
124 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
125 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
126 0xffffffff, FALSE),
d6ab8113
JB
127 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
128 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
129 TRUE),
130 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
131 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
132 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
133 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
134 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
135 FALSE, 0xffffffff, 0xffffffff, TRUE),
7b81dfbb
AJ
136 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
137 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
138 FALSE),
139 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
140 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
141 MINUS_ONE, TRUE),
142 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
143 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
144 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
145 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
146 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
147 MINUS_ONE, FALSE),
148 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
149 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
150 MINUS_ONE, FALSE),
1788fc08
L
151 HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
152 bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
153 FALSE),
154 HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
155 bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
156 FALSE),
67a4f2b7
AO
157 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
158 complain_overflow_bitfield, bfd_elf_generic_reloc,
159 "R_X86_64_GOTPC32_TLSDESC",
160 FALSE, 0xffffffff, 0xffffffff, TRUE),
161 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
162 complain_overflow_dont, bfd_elf_generic_reloc,
163 "R_X86_64_TLSDESC_CALL",
164 FALSE, 0, 0, FALSE),
165 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
166 complain_overflow_bitfield, bfd_elf_generic_reloc,
167 "R_X86_64_TLSDESC",
168 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
cbe950e9
L
169 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
170 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
171 MINUS_ONE, FALSE),
64d25c44
L
172 HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
173 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
174 MINUS_ONE, FALSE),
c3320543
L
175 HOWTO(R_X86_64_PC32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
176 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", FALSE, 0xffffffff, 0xffffffff,
177 TRUE),
178 HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
179 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", FALSE, 0xffffffff, 0xffffffff,
180 TRUE),
fe4770f4 181
a33d77bc
JB
182 /* We have a gap in the reloc numbers here.
183 R_X86_64_standard counts the number up to this point, and
184 R_X86_64_vt_offset is the value to subtract from a reloc type of
185 R_X86_64_GNU_VT* to form an index into this table. */
c3320543 186#define R_X86_64_standard (R_X86_64_PLT32_BND + 1)
a33d77bc
JB
187#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
188
fe4770f4 189/* GNU extension to record C++ vtable hierarchy. */
b34976b6
AM
190 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
191 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
fe4770f4
AJ
192
193/* GNU extension to record C++ vtable member usage. */
b34976b6
AM
194 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
195 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
d7921315
L
196 FALSE),
197
198/* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
199 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
200 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
201 FALSE)
8d88c4ca
NC
202};
203
d8045f23
NC
204#define IS_X86_64_PCREL_TYPE(TYPE) \
205 ( ((TYPE) == R_X86_64_PC8) \
206 || ((TYPE) == R_X86_64_PC16) \
207 || ((TYPE) == R_X86_64_PC32) \
c3320543 208 || ((TYPE) == R_X86_64_PC32_BND) \
d8045f23
NC
209 || ((TYPE) == R_X86_64_PC64))
210
8d88c4ca 211/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
212struct elf_reloc_map
213{
8d88c4ca
NC
214 bfd_reloc_code_real_type bfd_reloc_val;
215 unsigned char elf_reloc_val;
216};
217
dc810e39 218static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 219{
70256ad8
AJ
220 { BFD_RELOC_NONE, R_X86_64_NONE, },
221 { BFD_RELOC_64, R_X86_64_64, },
222 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
223 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
224 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
225 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
226 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
227 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
228 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
229 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
230 { BFD_RELOC_32, R_X86_64_32, },
231 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
232 { BFD_RELOC_16, R_X86_64_16, },
233 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
234 { BFD_RELOC_8, R_X86_64_8, },
235 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
236 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
237 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
238 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
239 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
240 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
241 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
242 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
243 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
d6ab8113
JB
244 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
245 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
246 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
7b81dfbb
AJ
247 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
248 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
249 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
250 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
251 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
1788fc08
L
252 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
253 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
67a4f2b7
AO
254 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
255 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
256 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
cbe950e9 257 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
c3320543
L
258 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND,},
259 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND,},
fe4770f4
AJ
260 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
261 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
262};
263
67a4f2b7 264static reloc_howto_type *
351f65ca 265elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
67a4f2b7
AO
266{
267 unsigned i;
268
d7921315
L
269 if (r_type == (unsigned int) R_X86_64_32)
270 {
271 if (ABI_64_P (abfd))
272 i = r_type;
273 else
274 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
275 }
276 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
277 || r_type >= (unsigned int) R_X86_64_max)
67a4f2b7
AO
278 {
279 if (r_type >= (unsigned int) R_X86_64_standard)
280 {
281 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
282 abfd, (int) r_type);
283 r_type = R_X86_64_NONE;
284 }
285 i = r_type;
286 }
287 else
288 i = r_type - (unsigned int) R_X86_64_vt_offset;
289 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
290 return &x86_64_elf_howto_table[i];
291}
8d88c4ca
NC
292
293/* Given a BFD reloc type, return a HOWTO structure. */
294static reloc_howto_type *
351f65ca
L
295elf_x86_64_reloc_type_lookup (bfd *abfd,
296 bfd_reloc_code_real_type code)
8d88c4ca
NC
297{
298 unsigned int i;
27482721 299
8d88c4ca
NC
300 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
301 i++)
302 {
303 if (x86_64_reloc_map[i].bfd_reloc_val == code)
351f65ca
L
304 return elf_x86_64_rtype_to_howto (abfd,
305 x86_64_reloc_map[i].elf_reloc_val);
8d88c4ca
NC
306 }
307 return 0;
308}
309
157090f7 310static reloc_howto_type *
d7921315 311elf_x86_64_reloc_name_lookup (bfd *abfd,
351f65ca 312 const char *r_name)
157090f7
AM
313{
314 unsigned int i;
315
d7921315
L
316 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
317 {
318 /* Get x32 R_X86_64_32. */
319 reloc_howto_type *reloc
320 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
321 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
322 return reloc;
323 }
324
325 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
157090f7
AM
326 if (x86_64_elf_howto_table[i].name != NULL
327 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
328 return &x86_64_elf_howto_table[i];
329
330 return NULL;
331}
332
8d88c4ca 333/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 334
8d88c4ca 335static void
351f65ca
L
336elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
337 Elf_Internal_Rela *dst)
8d88c4ca 338{
67a4f2b7 339 unsigned r_type;
8d88c4ca 340
351f65ca
L
341 r_type = ELF32_R_TYPE (dst->r_info);
342 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
8d88c4ca
NC
343 BFD_ASSERT (r_type == cache_ptr->howto->type);
344}
70256ad8 345\f
3bab7989 346/* Support for core dump NOTE sections. */
b34976b6 347static bfd_boolean
351f65ca 348elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
349{
350 int offset;
eea6121a 351 size_t size;
3bab7989
ML
352
353 switch (note->descsz)
354 {
355 default:
b34976b6 356 return FALSE;
3bab7989 357
bcd823f1
L
358 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
359 /* pr_cursig */
228e534f 360 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
bcd823f1
L
361
362 /* pr_pid */
228e534f 363 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
bcd823f1
L
364
365 /* pr_reg */
366 offset = 72;
367 size = 216;
368
369 break;
370
3bab7989
ML
371 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
372 /* pr_cursig */
228e534f 373 elf_tdata (abfd)->core->signal
3bab7989
ML
374 = bfd_get_16 (abfd, note->descdata + 12);
375
376 /* pr_pid */
228e534f 377 elf_tdata (abfd)->core->lwpid
3bab7989
ML
378 = bfd_get_32 (abfd, note->descdata + 32);
379
380 /* pr_reg */
381 offset = 112;
eea6121a 382 size = 216;
3bab7989
ML
383
384 break;
385 }
386
387 /* Make a ".reg/999" section. */
388 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 389 size, note->descpos + offset);
3bab7989
ML
390}
391
b34976b6 392static bfd_boolean
351f65ca 393elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
394{
395 switch (note->descsz)
396 {
397 default:
b34976b6 398 return FALSE;
3bab7989 399
bcd823f1 400 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
228e534f 401 elf_tdata (abfd)->core->pid
bcd823f1 402 = bfd_get_32 (abfd, note->descdata + 12);
228e534f 403 elf_tdata (abfd)->core->program
bcd823f1 404 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 405 elf_tdata (abfd)->core->command
bcd823f1
L
406 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
407 break;
408
3bab7989 409 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
228e534f 410 elf_tdata (abfd)->core->pid
261b8d08 411 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 412 elf_tdata (abfd)->core->program
3bab7989 413 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 414 elf_tdata (abfd)->core->command
3bab7989
ML
415 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
416 }
417
418 /* Note that for some reason, a spurious space is tacked
419 onto the end of the args in some (at least one anyway)
420 implementations, so strip it off if it exists. */
421
422 {
228e534f 423 char *command = elf_tdata (abfd)->core->command;
3bab7989
ML
424 int n = strlen (command);
425
426 if (0 < n && command[n - 1] == ' ')
427 command[n - 1] = '\0';
428 }
429
b34976b6 430 return TRUE;
3bab7989 431}
8fd79e71
L
432
433#ifdef CORE_HEADER
434static char *
435elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
436 int note_type, ...)
437{
438 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8fd79e71
L
439 va_list ap;
440 const char *fname, *psargs;
441 long pid;
442 int cursig;
443 const void *gregs;
444
445 switch (note_type)
446 {
447 default:
448 return NULL;
449
450 case NT_PRPSINFO:
451 va_start (ap, note_type);
452 fname = va_arg (ap, const char *);
453 psargs = va_arg (ap, const char *);
454 va_end (ap);
455
456 if (bed->s->elfclass == ELFCLASS32)
457 {
458 prpsinfo32_t data;
459 memset (&data, 0, sizeof (data));
460 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
461 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
462 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
463 &data, sizeof (data));
8fd79e71
L
464 }
465 else
466 {
b1bd052d 467 prpsinfo64_t data;
8fd79e71
L
468 memset (&data, 0, sizeof (data));
469 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
470 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
471 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
472 &data, sizeof (data));
8fd79e71 473 }
e85c6a70 474 /* NOTREACHED */
8fd79e71
L
475
476 case NT_PRSTATUS:
477 va_start (ap, note_type);
478 pid = va_arg (ap, long);
479 cursig = va_arg (ap, int);
480 gregs = va_arg (ap, const void *);
481 va_end (ap);
482
483 if (bed->s->elfclass == ELFCLASS32)
484 {
485 if (bed->elf_machine_code == EM_X86_64)
486 {
487 prstatusx32_t prstat;
488 memset (&prstat, 0, sizeof (prstat));
489 prstat.pr_pid = pid;
490 prstat.pr_cursig = cursig;
491 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
492 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
493 &prstat, sizeof (prstat));
8fd79e71
L
494 }
495 else
496 {
497 prstatus32_t prstat;
498 memset (&prstat, 0, sizeof (prstat));
499 prstat.pr_pid = pid;
500 prstat.pr_cursig = cursig;
501 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
502 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
503 &prstat, sizeof (prstat));
8fd79e71
L
504 }
505 }
506 else
507 {
b1bd052d 508 prstatus64_t prstat;
8fd79e71
L
509 memset (&prstat, 0, sizeof (prstat));
510 prstat.pr_pid = pid;
511 prstat.pr_cursig = cursig;
512 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
513 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
514 &prstat, sizeof (prstat));
8fd79e71 515 }
8fd79e71 516 }
e85c6a70 517 /* NOTREACHED */
8fd79e71
L
518}
519#endif
3bab7989 520\f
407443a3 521/* Functions for the x86-64 ELF linker. */
70256ad8 522
407443a3 523/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
524 section. */
525
351f65ca 526#define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
eec9dd95 527#define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
70256ad8 528
d40d037c
AJ
529/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
530 copying dynamic variables from a shared lib into an app's dynbss
531 section, and instead use a dynamic relocation to point into the
532 shared lib. */
533#define ELIMINATE_COPY_RELOCS 1
534
70256ad8
AJ
535/* The size in bytes of an entry in the global offset table. */
536
537#define GOT_ENTRY_SIZE 8
8d88c4ca 538
70256ad8 539/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 540
70256ad8
AJ
541#define PLT_ENTRY_SIZE 16
542
543/* The first entry in a procedure linkage table looks like this. See the
544 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
545
351f65ca 546static const bfd_byte elf_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
70256ad8 547{
653165cc
AJ
548 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
549 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
10efb593 550 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
70256ad8
AJ
551};
552
553/* Subsequent entries in a procedure linkage table look like this. */
554
351f65ca 555static const bfd_byte elf_x86_64_plt_entry[PLT_ENTRY_SIZE] =
70256ad8 556{
653165cc 557 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 558 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 559 0x68, /* pushq immediate */
70256ad8
AJ
560 0, 0, 0, 0, /* replaced with index into relocation table. */
561 0xe9, /* jmp relative */
562 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
563};
564
0ff2b86e
L
565/* The first entry in a procedure linkage table with BND relocations
566 like this. */
567
568static const bfd_byte elf_x86_64_bnd_plt0_entry[PLT_ENTRY_SIZE] =
569{
570 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
571 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
572 0x0f, 0x1f, 0 /* nopl (%rax) */
573};
574
575/* Subsequent entries for legacy branches in a procedure linkage table
576 with BND relocations look like this. */
577
578static const bfd_byte elf_x86_64_legacy_plt_entry[PLT_ENTRY_SIZE] =
579{
580 0x68, 0, 0, 0, 0, /* pushq immediate */
581 0xe9, 0, 0, 0, 0, /* jmpq relative */
582 0x66, 0x0f, 0x1f, 0x44, 0, 0 /* nopw (%rax,%rax,1) */
583};
584
585/* Subsequent entries for branches with BND prefx in a procedure linkage
586 table with BND relocations look like this. */
587
588static const bfd_byte elf_x86_64_bnd_plt_entry[PLT_ENTRY_SIZE] =
589{
590 0x68, 0, 0, 0, 0, /* pushq immediate */
591 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
592 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
593};
594
595/* Entries for legacy branches in the second procedure linkage table
596 look like this. */
597
598static const bfd_byte elf_x86_64_legacy_plt2_entry[8] =
599{
600 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
601 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
602 0x66, 0x90 /* xchg %ax,%ax */
603};
604
605/* Entries for branches with BND prefix in the second procedure linkage
606 table look like this. */
607
608static const bfd_byte elf_x86_64_bnd_plt2_entry[8] =
609{
610 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
611 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
612 0x90 /* nop */
613};
614
e41b3a13
JJ
615/* .eh_frame covering the .plt section. */
616
617static const bfd_byte elf_x86_64_eh_frame_plt[] =
618{
619#define PLT_CIE_LENGTH 20
620#define PLT_FDE_LENGTH 36
621#define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
622#define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
623 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
624 0, 0, 0, 0, /* CIE ID */
625 1, /* CIE version */
626 'z', 'R', 0, /* Augmentation string */
627 1, /* Code alignment factor */
628 0x78, /* Data alignment factor */
629 16, /* Return address column */
630 1, /* Augmentation size */
631 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
632 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
633 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
634 DW_CFA_nop, DW_CFA_nop,
635
636 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
637 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
638 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
639 0, 0, 0, 0, /* .plt size goes here */
640 0, /* Augmentation size */
641 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
642 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
643 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
644 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
645 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
646 11, /* Block length */
647 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
648 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
649 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
650 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
651 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
652};
653
eed180f8
RM
654/* Architecture-specific backend data for x86-64. */
655
656struct elf_x86_64_backend_data
657{
658 /* Templates for the initial PLT entry and for subsequent entries. */
659 const bfd_byte *plt0_entry;
660 const bfd_byte *plt_entry;
661 unsigned int plt_entry_size; /* Size of each PLT entry. */
662
663 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
664 unsigned int plt0_got1_offset;
665 unsigned int plt0_got2_offset;
666
667 /* Offset of the end of the PC-relative instruction containing
668 plt0_got2_offset. */
669 unsigned int plt0_got2_insn_end;
670
671 /* Offsets into plt_entry that are to be replaced with... */
672 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
673 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
674 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
675
676 /* Length of the PC-relative instruction containing plt_got_offset. */
677 unsigned int plt_got_insn_size;
678
679 /* Offset of the end of the PC-relative jump to plt0_entry. */
680 unsigned int plt_plt_insn_end;
681
682 /* Offset into plt_entry where the initial value of the GOT entry points. */
683 unsigned int plt_lazy_offset;
684
685 /* .eh_frame covering the .plt section. */
686 const bfd_byte *eh_frame_plt;
687 unsigned int eh_frame_plt_size;
688};
689
f8222080
L
690#define get_elf_x86_64_arch_data(bed) \
691 ((const struct elf_x86_64_backend_data *) (bed)->arch_data)
692
eed180f8 693#define get_elf_x86_64_backend_data(abfd) \
f8222080 694 get_elf_x86_64_arch_data (get_elf_backend_data (abfd))
eed180f8
RM
695
696#define GET_PLT_ENTRY_SIZE(abfd) \
697 get_elf_x86_64_backend_data (abfd)->plt_entry_size
698
699/* These are the standard parameters. */
700static const struct elf_x86_64_backend_data elf_x86_64_arch_bed =
701 {
702 elf_x86_64_plt0_entry, /* plt0_entry */
703 elf_x86_64_plt_entry, /* plt_entry */
704 sizeof (elf_x86_64_plt_entry), /* plt_entry_size */
705 2, /* plt0_got1_offset */
706 8, /* plt0_got2_offset */
707 12, /* plt0_got2_insn_end */
708 2, /* plt_got_offset */
709 7, /* plt_reloc_offset */
710 12, /* plt_plt_offset */
711 6, /* plt_got_insn_size */
712 PLT_ENTRY_SIZE, /* plt_plt_insn_end */
713 6, /* plt_lazy_offset */
714 elf_x86_64_eh_frame_plt, /* eh_frame_plt */
715 sizeof (elf_x86_64_eh_frame_plt), /* eh_frame_plt_size */
716 };
717
0ff2b86e
L
718static const struct elf_x86_64_backend_data elf_x86_64_bnd_arch_bed =
719 {
720 elf_x86_64_bnd_plt0_entry, /* plt0_entry */
721 elf_x86_64_bnd_plt_entry, /* plt_entry */
722 sizeof (elf_x86_64_bnd_plt_entry), /* plt_entry_size */
723 2, /* plt0_got1_offset */
724 1+8, /* plt0_got2_offset */
725 1+12, /* plt0_got2_insn_end */
726 1+2, /* plt_got_offset */
727 1, /* plt_reloc_offset */
728 7, /* plt_plt_offset */
729 1+6, /* plt_got_insn_size */
730 11, /* plt_plt_insn_end */
731 0, /* plt_lazy_offset */
732 elf_x86_64_eh_frame_plt, /* eh_frame_plt */
733 sizeof (elf_x86_64_eh_frame_plt), /* eh_frame_plt_size */
734 };
735
eed180f8
RM
736#define elf_backend_arch_data &elf_x86_64_arch_bed
737
70256ad8
AJ
738/* x86-64 ELF linker hash entry. */
739
351f65ca 740struct elf_x86_64_link_hash_entry
70256ad8 741{
c434dee6 742 struct elf_link_hash_entry elf;
70256ad8 743
c434dee6 744 /* Track dynamic relocs copied for this symbol. */
e03a8ed8 745 struct elf_dyn_relocs *dyn_relocs;
bffbf940
JJ
746
747#define GOT_UNKNOWN 0
748#define GOT_NORMAL 1
749#define GOT_TLS_GD 2
750#define GOT_TLS_IE 3
67a4f2b7
AO
751#define GOT_TLS_GDESC 4
752#define GOT_TLS_GD_BOTH_P(type) \
753 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
754#define GOT_TLS_GD_P(type) \
755 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
756#define GOT_TLS_GDESC_P(type) \
757 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
758#define GOT_TLS_GD_ANY_P(type) \
759 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
bffbf940 760 unsigned char tls_type;
67a4f2b7 761
0ff2b86e
L
762 /* TRUE if symbol has at least one BND relocation. */
763 bfd_boolean has_bnd_reloc;
764
765 /* Information about the second PLT entry. Filled when has_bnd_reloc is
766 set. */
767 union gotplt_union plt_bnd;
768
67a4f2b7
AO
769 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
770 starting at the end of the jump table. */
771 bfd_vma tlsdesc_got;
bffbf940
JJ
772};
773
351f65ca
L
774#define elf_x86_64_hash_entry(ent) \
775 ((struct elf_x86_64_link_hash_entry *)(ent))
bffbf940 776
351f65ca 777struct elf_x86_64_obj_tdata
bffbf940
JJ
778{
779 struct elf_obj_tdata root;
780
781 /* tls_type for each local got entry. */
782 char *local_got_tls_type;
67a4f2b7
AO
783
784 /* GOTPLT entries for TLS descriptors. */
785 bfd_vma *local_tlsdesc_gotent;
70256ad8
AJ
786};
787
351f65ca
L
788#define elf_x86_64_tdata(abfd) \
789 ((struct elf_x86_64_obj_tdata *) (abfd)->tdata.any)
bffbf940 790
351f65ca
L
791#define elf_x86_64_local_got_tls_type(abfd) \
792 (elf_x86_64_tdata (abfd)->local_got_tls_type)
bffbf940 793
351f65ca
L
794#define elf_x86_64_local_tlsdesc_gotent(abfd) \
795 (elf_x86_64_tdata (abfd)->local_tlsdesc_gotent)
bffbf940 796
0ffa91dd
NC
797#define is_x86_64_elf(bfd) \
798 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
799 && elf_tdata (bfd) != NULL \
4dfe6ac6 800 && elf_object_id (bfd) == X86_64_ELF_DATA)
0ffa91dd
NC
801
802static bfd_boolean
351f65ca 803elf_x86_64_mkobject (bfd *abfd)
0ffa91dd 804{
351f65ca 805 return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_64_obj_tdata),
4dfe6ac6 806 X86_64_ELF_DATA);
0ffa91dd
NC
807}
808
c434dee6 809/* x86-64 ELF linker hash table. */
8d88c4ca 810
351f65ca 811struct elf_x86_64_link_hash_table
407443a3 812{
c434dee6 813 struct elf_link_hash_table elf;
70256ad8 814
c434dee6 815 /* Short-cuts to get to dynamic linker sections. */
c434dee6
AJ
816 asection *sdynbss;
817 asection *srelbss;
e41b3a13 818 asection *plt_eh_frame;
0ff2b86e 819 asection *plt_bnd;
70256ad8 820
4dfe6ac6
NC
821 union
822 {
bffbf940
JJ
823 bfd_signed_vma refcount;
824 bfd_vma offset;
825 } tls_ld_got;
826
67a4f2b7
AO
827 /* The amount of space used by the jump slots in the GOT. */
828 bfd_vma sgotplt_jump_table_size;
829
87d72d41
AM
830 /* Small local sym cache. */
831 struct sym_cache sym_cache;
9f03412a 832
351f65ca
L
833 bfd_vma (*r_info) (bfd_vma, bfd_vma);
834 bfd_vma (*r_sym) (bfd_vma);
248775ba 835 unsigned int pointer_r_type;
351f65ca
L
836 const char *dynamic_interpreter;
837 int dynamic_interpreter_size;
838
9f03412a
AO
839 /* _TLS_MODULE_BASE_ symbol. */
840 struct bfd_link_hash_entry *tls_module_base;
c25bc9fc
L
841
842 /* Used by local STT_GNU_IFUNC symbols. */
843 htab_t loc_hash_table;
4dfe6ac6
NC
844 void * loc_hash_memory;
845
846 /* The offset into splt of the PLT entry for the TLS descriptor
847 resolver. Special values are 0, if not necessary (or not found
848 to be necessary yet), and -1 if needed but not determined
849 yet. */
850 bfd_vma tlsdesc_plt;
851 /* The offset into sgot of the GOT entry used by the PLT entry
852 above. */
853 bfd_vma tlsdesc_got;
e1f98742
L
854
855 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
856 bfd_vma next_jump_slot_index;
857 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
858 bfd_vma next_irelative_index;
c434dee6 859};
70256ad8
AJ
860
861/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca 862
351f65ca 863#define elf_x86_64_hash_table(p) \
4dfe6ac6 864 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
351f65ca 865 == X86_64_ELF_DATA ? ((struct elf_x86_64_link_hash_table *) ((p)->hash)) : NULL)
8d88c4ca 866
351f65ca 867#define elf_x86_64_compute_jump_table_size(htab) \
6de2ae4a 868 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
67a4f2b7 869
407443a3 870/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
871
872static struct bfd_hash_entry *
351f65ca
L
873elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
874 struct bfd_hash_table *table,
875 const char *string)
70256ad8 876{
70256ad8 877 /* Allocate the structure if it has not already been allocated by a
c434dee6
AJ
878 subclass. */
879 if (entry == NULL)
880 {
a50b1753 881 entry = (struct bfd_hash_entry *)
eed180f8
RM
882 bfd_hash_allocate (table,
883 sizeof (struct elf_x86_64_link_hash_entry));
c434dee6
AJ
884 if (entry == NULL)
885 return entry;
886 }
70256ad8
AJ
887
888 /* Call the allocation method of the superclass. */
c434dee6
AJ
889 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
890 if (entry != NULL)
70256ad8 891 {
351f65ca 892 struct elf_x86_64_link_hash_entry *eh;
c434dee6 893
351f65ca 894 eh = (struct elf_x86_64_link_hash_entry *) entry;
c434dee6 895 eh->dyn_relocs = NULL;
bffbf940 896 eh->tls_type = GOT_UNKNOWN;
0ff2b86e
L
897 eh->has_bnd_reloc = FALSE;
898 eh->plt_bnd.offset = (bfd_vma) -1;
67a4f2b7 899 eh->tlsdesc_got = (bfd_vma) -1;
70256ad8
AJ
900 }
901
c434dee6 902 return entry;
70256ad8
AJ
903}
904
c25bc9fc
L
905/* Compute a hash of a local hash entry. We use elf_link_hash_entry
906 for local symbol so that we can handle local STT_GNU_IFUNC symbols
907 as global symbol. We reuse indx and dynstr_index for local symbol
908 hash since they aren't used by global symbols in this backend. */
909
910static hashval_t
351f65ca 911elf_x86_64_local_htab_hash (const void *ptr)
c25bc9fc
L
912{
913 struct elf_link_hash_entry *h
914 = (struct elf_link_hash_entry *) ptr;
d2149d72 915 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
c25bc9fc
L
916}
917
918/* Compare local hash entries. */
919
920static int
351f65ca 921elf_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
c25bc9fc
L
922{
923 struct elf_link_hash_entry *h1
924 = (struct elf_link_hash_entry *) ptr1;
925 struct elf_link_hash_entry *h2
926 = (struct elf_link_hash_entry *) ptr2;
927
928 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
929}
930
931/* Find and/or create a hash entry for local symbol. */
932
933static struct elf_link_hash_entry *
351f65ca
L
934elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table *htab,
935 bfd *abfd, const Elf_Internal_Rela *rel,
936 bfd_boolean create)
c25bc9fc 937{
351f65ca 938 struct elf_x86_64_link_hash_entry e, *ret;
c25bc9fc 939 asection *sec = abfd->sections;
d2149d72 940 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
351f65ca 941 htab->r_sym (rel->r_info));
c25bc9fc
L
942 void **slot;
943
944 e.elf.indx = sec->id;
351f65ca 945 e.elf.dynstr_index = htab->r_sym (rel->r_info);
c25bc9fc
L
946 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
947 create ? INSERT : NO_INSERT);
948
949 if (!slot)
950 return NULL;
951
952 if (*slot)
953 {
351f65ca 954 ret = (struct elf_x86_64_link_hash_entry *) *slot;
c25bc9fc
L
955 return &ret->elf;
956 }
957
351f65ca 958 ret = (struct elf_x86_64_link_hash_entry *)
c25bc9fc 959 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
351f65ca 960 sizeof (struct elf_x86_64_link_hash_entry));
c25bc9fc
L
961 if (ret)
962 {
963 memset (ret, 0, sizeof (*ret));
964 ret->elf.indx = sec->id;
351f65ca 965 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
c25bc9fc 966 ret->elf.dynindx = -1;
c25bc9fc
L
967 *slot = ret;
968 }
969 return &ret->elf;
970}
971
8d88c4ca
NC
972/* Create an X86-64 ELF linker hash table. */
973
974static struct bfd_link_hash_table *
351f65ca 975elf_x86_64_link_hash_table_create (bfd *abfd)
8d88c4ca 976{
351f65ca
L
977 struct elf_x86_64_link_hash_table *ret;
978 bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
8d88c4ca 979
7bf52ea2 980 ret = (struct elf_x86_64_link_hash_table *) bfd_zmalloc (amt);
c434dee6 981 if (ret == NULL)
8d88c4ca
NC
982 return NULL;
983
eb4ff4d6 984 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
351f65ca
L
985 elf_x86_64_link_hash_newfunc,
986 sizeof (struct elf_x86_64_link_hash_entry),
4dfe6ac6 987 X86_64_ELF_DATA))
8d88c4ca 988 {
e2d34d7d 989 free (ret);
8d88c4ca
NC
990 return NULL;
991 }
992
351f65ca
L
993 if (ABI_64_P (abfd))
994 {
995 ret->r_info = elf64_r_info;
996 ret->r_sym = elf64_r_sym;
248775ba 997 ret->pointer_r_type = R_X86_64_64;
351f65ca
L
998 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
999 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
1000 }
1001 else
1002 {
1003 ret->r_info = elf32_r_info;
1004 ret->r_sym = elf32_r_sym;
248775ba 1005 ret->pointer_r_type = R_X86_64_32;
351f65ca
L
1006 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
1007 ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
1008 }
1009
c25bc9fc 1010 ret->loc_hash_table = htab_try_create (1024,
351f65ca
L
1011 elf_x86_64_local_htab_hash,
1012 elf_x86_64_local_htab_eq,
c25bc9fc
L
1013 NULL);
1014 ret->loc_hash_memory = objalloc_create ();
1015 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1016 {
1017 free (ret);
1018 return NULL;
1019 }
1020
c434dee6
AJ
1021 return &ret->elf.root;
1022}
1023
c25bc9fc
L
1024/* Destroy an X86-64 ELF linker hash table. */
1025
1026static void
351f65ca 1027elf_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
c25bc9fc 1028{
351f65ca
L
1029 struct elf_x86_64_link_hash_table *htab
1030 = (struct elf_x86_64_link_hash_table *) hash;
c25bc9fc
L
1031
1032 if (htab->loc_hash_table)
1033 htab_delete (htab->loc_hash_table);
1034 if (htab->loc_hash_memory)
1035 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
9f7c3e5e 1036 _bfd_elf_link_hash_table_free (hash);
c25bc9fc
L
1037}
1038
c434dee6
AJ
1039/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
1040 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
1041 hash table. */
1042
b34976b6 1043static bfd_boolean
351f65ca
L
1044elf_x86_64_create_dynamic_sections (bfd *dynobj,
1045 struct bfd_link_info *info)
c434dee6 1046{
351f65ca 1047 struct elf_x86_64_link_hash_table *htab;
c434dee6 1048
c434dee6 1049 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 1050 return FALSE;
c434dee6 1051
351f65ca 1052 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1053 if (htab == NULL)
1054 return FALSE;
1055
3d4d4302 1056 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
c434dee6 1057 if (!info->shared)
3d4d4302 1058 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
c434dee6 1059
6de2ae4a 1060 if (!htab->sdynbss
c434dee6
AJ
1061 || (!info->shared && !htab->srelbss))
1062 abort ();
1063
e41b3a13 1064 if (!info->no_ld_generated_unwind_info
2fe0fd06 1065 && htab->plt_eh_frame == NULL
e4de50d4 1066 && htab->elf.splt != NULL)
e41b3a13 1067 {
bbf96e4e
L
1068 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1069 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1070 | SEC_LINKER_CREATED);
e41b3a13 1071 htab->plt_eh_frame
bbf96e4e 1072 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
e41b3a13
JJ
1073 if (htab->plt_eh_frame == NULL
1074 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 3))
1075 return FALSE;
e41b3a13 1076 }
b34976b6 1077 return TRUE;
c434dee6
AJ
1078}
1079
1080/* Copy the extra info we tack onto an elf_link_hash_entry. */
1081
1082static void
351f65ca
L
1083elf_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
1084 struct elf_link_hash_entry *dir,
1085 struct elf_link_hash_entry *ind)
c434dee6 1086{
351f65ca 1087 struct elf_x86_64_link_hash_entry *edir, *eind;
c434dee6 1088
351f65ca
L
1089 edir = (struct elf_x86_64_link_hash_entry *) dir;
1090 eind = (struct elf_x86_64_link_hash_entry *) ind;
c434dee6 1091
0ff2b86e
L
1092 if (!edir->has_bnd_reloc)
1093 edir->has_bnd_reloc = eind->has_bnd_reloc;
1094
c434dee6
AJ
1095 if (eind->dyn_relocs != NULL)
1096 {
1097 if (edir->dyn_relocs != NULL)
1098 {
e03a8ed8
L
1099 struct elf_dyn_relocs **pp;
1100 struct elf_dyn_relocs *p;
c434dee6 1101
fcfa13d2 1102 /* Add reloc counts against the indirect sym to the direct sym
c434dee6
AJ
1103 list. Merge any entries against the same section. */
1104 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1105 {
e03a8ed8 1106 struct elf_dyn_relocs *q;
c434dee6
AJ
1107
1108 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1109 if (q->sec == p->sec)
1110 {
1111 q->pc_count += p->pc_count;
1112 q->count += p->count;
1113 *pp = p->next;
1114 break;
1115 }
1116 if (q == NULL)
1117 pp = &p->next;
1118 }
1119 *pp = edir->dyn_relocs;
1120 }
1121
1122 edir->dyn_relocs = eind->dyn_relocs;
1123 eind->dyn_relocs = NULL;
1124 }
1125
bffbf940
JJ
1126 if (ind->root.type == bfd_link_hash_indirect
1127 && dir->got.refcount <= 0)
1128 {
1129 edir->tls_type = eind->tls_type;
1130 eind->tls_type = GOT_UNKNOWN;
1131 }
1132
d40d037c
AJ
1133 if (ELIMINATE_COPY_RELOCS
1134 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
1135 && dir->dynamic_adjusted)
1136 {
1137 /* If called to transfer flags for a weakdef during processing
1138 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1139 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1140 dir->ref_dynamic |= ind->ref_dynamic;
1141 dir->ref_regular |= ind->ref_regular;
1142 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1143 dir->needs_plt |= ind->needs_plt;
1144 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1145 }
d40d037c 1146 else
fcfa13d2 1147 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
8d88c4ca
NC
1148}
1149
b34976b6 1150static bfd_boolean
27482721 1151elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 1152{
8d88c4ca
NC
1153 /* Set the right machine number for an x86-64 elf64 file. */
1154 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
b34976b6 1155 return TRUE;
8d88c4ca
NC
1156}
1157
8059fb19
RM
1158static bfd_boolean
1159elf32_x86_64_elf_object_p (bfd *abfd)
1160{
1161 /* Set the right machine number for an x86-64 elf32 file. */
1162 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1163 return TRUE;
1164}
1165
142411ca
L
1166/* Return TRUE if the TLS access code sequence support transition
1167 from R_TYPE. */
1168
1169static bfd_boolean
351f65ca
L
1170elf_x86_64_check_tls_transition (bfd *abfd,
1171 struct bfd_link_info *info,
1172 asection *sec,
1173 bfd_byte *contents,
1174 Elf_Internal_Shdr *symtab_hdr,
1175 struct elf_link_hash_entry **sym_hashes,
1176 unsigned int r_type,
1177 const Elf_Internal_Rela *rel,
1178 const Elf_Internal_Rela *relend)
bffbf940 1179{
142411ca
L
1180 unsigned int val;
1181 unsigned long r_symndx;
5c98a14e 1182 bfd_boolean largepic = FALSE;
142411ca
L
1183 struct elf_link_hash_entry *h;
1184 bfd_vma offset;
351f65ca 1185 struct elf_x86_64_link_hash_table *htab;
142411ca
L
1186
1187 /* Get the section contents. */
1188 if (contents == NULL)
1189 {
1190 if (elf_section_data (sec)->this_hdr.contents != NULL)
1191 contents = elf_section_data (sec)->this_hdr.contents;
1192 else
1193 {
1194 /* FIXME: How to better handle error condition? */
1195 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1196 return FALSE;
bffbf940 1197
142411ca
L
1198 /* Cache the section contents for elf_link_input_bfd. */
1199 elf_section_data (sec)->this_hdr.contents = contents;
1200 }
1201 }
1202
351f65ca 1203 htab = elf_x86_64_hash_table (info);
142411ca 1204 offset = rel->r_offset;
bffbf940 1205 switch (r_type)
142411ca
L
1206 {
1207 case R_X86_64_TLSGD:
1208 case R_X86_64_TLSLD:
1209 if ((rel + 1) >= relend)
1210 return FALSE;
1211
1212 if (r_type == R_X86_64_TLSGD)
1213 {
52bc799a 1214 /* Check transition from GD access model. For 64bit, only
142411ca
L
1215 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1216 .word 0x6666; rex64; call __tls_get_addr
52bc799a
L
1217 can transit to different access model. For 32bit, only
1218 leaq foo@tlsgd(%rip), %rdi
1219 .word 0x6666; rex64; call __tls_get_addr
5c98a14e
JJ
1220 can transit to different access model. For largepic
1221 we also support:
1222 leaq foo@tlsgd(%rip), %rdi
1223 movabsq $__tls_get_addr@pltoff, %rax
1224 addq $rbx, %rax
1225 call *%rax. */
142411ca 1226
fa289a5f
AM
1227 static const unsigned char call[] = { 0x66, 0x66, 0x48, 0xe8 };
1228 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1229
5c98a14e 1230 if ((offset + 12) > sec->size)
142411ca 1231 return FALSE;
52bc799a 1232
5c98a14e
JJ
1233 if (memcmp (contents + offset + 4, call, 4) != 0)
1234 {
1235 if (!ABI_64_P (abfd)
1236 || (offset + 19) > sec->size
1237 || offset < 3
1238 || memcmp (contents + offset - 3, leaq + 1, 3) != 0
1239 || memcmp (contents + offset + 4, "\x48\xb8", 2) != 0
1240 || memcmp (contents + offset + 14, "\x48\x01\xd8\xff\xd0", 5)
1241 != 0)
1242 return FALSE;
1243 largepic = TRUE;
1244 }
1245 else if (ABI_64_P (abfd))
52bc799a 1246 {
52bc799a 1247 if (offset < 4
fa289a5f 1248 || memcmp (contents + offset - 4, leaq, 4) != 0)
52bc799a
L
1249 return FALSE;
1250 }
1251 else
1252 {
52bc799a 1253 if (offset < 3
fa289a5f 1254 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
52bc799a
L
1255 return FALSE;
1256 }
142411ca
L
1257 }
1258 else
1259 {
1260 /* Check transition from LD access model. Only
1261 leaq foo@tlsld(%rip), %rdi;
1262 call __tls_get_addr
5c98a14e
JJ
1263 can transit to different access model. For largepic
1264 we also support:
1265 leaq foo@tlsld(%rip), %rdi
1266 movabsq $__tls_get_addr@pltoff, %rax
1267 addq $rbx, %rax
1268 call *%rax. */
142411ca 1269
fa289a5f 1270 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
142411ca
L
1271
1272 if (offset < 3 || (offset + 9) > sec->size)
1273 return FALSE;
1274
5c98a14e 1275 if (memcmp (contents + offset - 3, lea, 3) != 0)
142411ca 1276 return FALSE;
5c98a14e
JJ
1277
1278 if (0xe8 != *(contents + offset + 4))
1279 {
1280 if (!ABI_64_P (abfd)
1281 || (offset + 19) > sec->size
1282 || memcmp (contents + offset + 4, "\x48\xb8", 2) != 0
1283 || memcmp (contents + offset + 14, "\x48\x01\xd8\xff\xd0", 5)
1284 != 0)
1285 return FALSE;
1286 largepic = TRUE;
1287 }
142411ca
L
1288 }
1289
351f65ca 1290 r_symndx = htab->r_sym (rel[1].r_info);
142411ca
L
1291 if (r_symndx < symtab_hdr->sh_info)
1292 return FALSE;
1293
1294 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
c4fb387b 1295 /* Use strncmp to check __tls_get_addr since __tls_get_addr
eed180f8 1296 may be versioned. */
142411ca
L
1297 return (h != NULL
1298 && h->root.root.string != NULL
5c98a14e
JJ
1299 && (largepic
1300 ? ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLTOFF64
1301 : (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1302 || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32))
c4fb387b
L
1303 && (strncmp (h->root.root.string,
1304 "__tls_get_addr", 14) == 0));
142411ca
L
1305
1306 case R_X86_64_GOTTPOFF:
1307 /* Check transition from IE access model:
4a4c5f25
L
1308 mov foo@gottpoff(%rip), %reg
1309 add foo@gottpoff(%rip), %reg
142411ca
L
1310 */
1311
4a4c5f25
L
1312 /* Check REX prefix first. */
1313 if (offset >= 3 && (offset + 4) <= sec->size)
1314 {
1315 val = bfd_get_8 (abfd, contents + offset - 3);
1316 if (val != 0x48 && val != 0x4c)
1317 {
1318 /* X32 may have 0x44 REX prefix or no REX prefix. */
1319 if (ABI_64_P (abfd))
1320 return FALSE;
1321 }
1322 }
1323 else
1324 {
1325 /* X32 may not have any REX prefix. */
1326 if (ABI_64_P (abfd))
1327 return FALSE;
1328 if (offset < 2 || (offset + 3) > sec->size)
1329 return FALSE;
1330 }
142411ca
L
1331
1332 val = bfd_get_8 (abfd, contents + offset - 2);
1333 if (val != 0x8b && val != 0x03)
1334 return FALSE;
1335
1336 val = bfd_get_8 (abfd, contents + offset - 1);
1337 return (val & 0xc7) == 5;
1338
1339 case R_X86_64_GOTPC32_TLSDESC:
1340 /* Check transition from GDesc access model:
1341 leaq x@tlsdesc(%rip), %rax
1342
1343 Make sure it's a leaq adding rip to a 32-bit offset
1344 into any register, although it's probably almost always
1345 going to be rax. */
1346
1347 if (offset < 3 || (offset + 4) > sec->size)
1348 return FALSE;
1349
1350 val = bfd_get_8 (abfd, contents + offset - 3);
1351 if ((val & 0xfb) != 0x48)
1352 return FALSE;
1353
1354 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1355 return FALSE;
1356
1357 val = bfd_get_8 (abfd, contents + offset - 1);
1358 return (val & 0xc7) == 0x05;
1359
1360 case R_X86_64_TLSDESC_CALL:
1361 /* Check transition from GDesc access model:
1362 call *x@tlsdesc(%rax)
1363 */
1364 if (offset + 2 <= sec->size)
1365 {
1366 /* Make sure that it's a call *x@tlsdesc(%rax). */
fa289a5f
AM
1367 static const unsigned char call[] = { 0xff, 0x10 };
1368 return memcmp (contents + offset, call, 2) == 0;
142411ca
L
1369 }
1370
1371 return FALSE;
1372
1373 default:
1374 abort ();
1375 }
1376}
1377
1378/* Return TRUE if the TLS access transition is OK or no transition
1379 will be performed. Update R_TYPE if there is a transition. */
1380
1381static bfd_boolean
351f65ca
L
1382elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1383 asection *sec, bfd_byte *contents,
1384 Elf_Internal_Shdr *symtab_hdr,
1385 struct elf_link_hash_entry **sym_hashes,
1386 unsigned int *r_type, int tls_type,
1387 const Elf_Internal_Rela *rel,
1388 const Elf_Internal_Rela *relend,
1389 struct elf_link_hash_entry *h,
1390 unsigned long r_symndx)
142411ca
L
1391{
1392 unsigned int from_type = *r_type;
1393 unsigned int to_type = from_type;
1394 bfd_boolean check = TRUE;
1395
bb1cb422
L
1396 /* Skip TLS transition for functions. */
1397 if (h != NULL
1398 && (h->type == STT_FUNC
1399 || h->type == STT_GNU_IFUNC))
1400 return TRUE;
1401
142411ca 1402 switch (from_type)
bffbf940
JJ
1403 {
1404 case R_X86_64_TLSGD:
67a4f2b7
AO
1405 case R_X86_64_GOTPC32_TLSDESC:
1406 case R_X86_64_TLSDESC_CALL:
bffbf940 1407 case R_X86_64_GOTTPOFF:
1d85728f 1408 if (info->executable)
142411ca
L
1409 {
1410 if (h == NULL)
1411 to_type = R_X86_64_TPOFF32;
1412 else
1413 to_type = R_X86_64_GOTTPOFF;
1414 }
1415
351f65ca 1416 /* When we are called from elf_x86_64_relocate_section,
142411ca
L
1417 CONTENTS isn't NULL and there may be additional transitions
1418 based on TLS_TYPE. */
1419 if (contents != NULL)
1420 {
1421 unsigned int new_to_type = to_type;
1422
1d85728f 1423 if (info->executable
142411ca
L
1424 && h != NULL
1425 && h->dynindx == -1
1426 && tls_type == GOT_TLS_IE)
1427 new_to_type = R_X86_64_TPOFF32;
1428
1429 if (to_type == R_X86_64_TLSGD
1430 || to_type == R_X86_64_GOTPC32_TLSDESC
1431 || to_type == R_X86_64_TLSDESC_CALL)
1432 {
1433 if (tls_type == GOT_TLS_IE)
1434 new_to_type = R_X86_64_GOTTPOFF;
1435 }
1436
1437 /* We checked the transition before when we were called from
351f65ca 1438 elf_x86_64_check_relocs. We only want to check the new
142411ca
L
1439 transition which hasn't been checked before. */
1440 check = new_to_type != to_type && from_type == to_type;
1441 to_type = new_to_type;
1442 }
1443
1444 break;
1445
bffbf940 1446 case R_X86_64_TLSLD:
1d85728f 1447 if (info->executable)
142411ca
L
1448 to_type = R_X86_64_TPOFF32;
1449 break;
1450
1451 default:
1452 return TRUE;
bffbf940
JJ
1453 }
1454
142411ca
L
1455 /* Return TRUE if there is no transition. */
1456 if (from_type == to_type)
1457 return TRUE;
1458
1459 /* Check if the transition can be performed. */
1460 if (check
351f65ca
L
1461 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1462 symtab_hdr, sym_hashes,
1463 from_type, rel, relend))
142411ca 1464 {
2f629d23 1465 reloc_howto_type *from, *to;
4c544807 1466 const char *name;
142411ca 1467
351f65ca
L
1468 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1469 to = elf_x86_64_rtype_to_howto (abfd, to_type);
142411ca 1470
4c544807
L
1471 if (h)
1472 name = h->root.root.string;
1473 else
1474 {
351f65ca 1475 struct elf_x86_64_link_hash_table *htab;
4dfe6ac6 1476
351f65ca 1477 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1478 if (htab == NULL)
1479 name = "*unknown*";
1480 else
1481 {
1482 Elf_Internal_Sym *isym;
1483
1484 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1485 abfd, r_symndx);
1486 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1487 }
4c544807
L
1488 }
1489
142411ca
L
1490 (*_bfd_error_handler)
1491 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1492 "in section `%A' failed"),
4c544807 1493 abfd, sec, from->name, to->name, name,
142411ca
L
1494 (unsigned long) rel->r_offset);
1495 bfd_set_error (bfd_error_bad_value);
1496 return FALSE;
1497 }
1498
1499 *r_type = to_type;
1500 return TRUE;
bffbf940
JJ
1501}
1502
70256ad8 1503/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
1504 calculate needed space in the global offset table, procedure
1505 linkage table, and dynamic reloc sections. */
70256ad8 1506
b34976b6 1507static bfd_boolean
351f65ca
L
1508elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1509 asection *sec,
1510 const Elf_Internal_Rela *relocs)
70256ad8 1511{
351f65ca 1512 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
1513 Elf_Internal_Shdr *symtab_hdr;
1514 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
1515 const Elf_Internal_Rela *rel;
1516 const Elf_Internal_Rela *rel_end;
70256ad8
AJ
1517 asection *sreloc;
1518
1049f94e 1519 if (info->relocatable)
b34976b6 1520 return TRUE;
70256ad8 1521
0ffa91dd
NC
1522 BFD_ASSERT (is_x86_64_elf (abfd));
1523
351f65ca 1524 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1525 if (htab == NULL)
1526 return FALSE;
1527
0ffa91dd 1528 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8 1529 sym_hashes = elf_sym_hashes (abfd);
70256ad8 1530
c434dee6 1531 sreloc = NULL;
cbe950e9 1532
70256ad8
AJ
1533 rel_end = relocs + sec->reloc_count;
1534 for (rel = relocs; rel < rel_end; rel++)
1535 {
bffbf940 1536 unsigned int r_type;
70256ad8
AJ
1537 unsigned long r_symndx;
1538 struct elf_link_hash_entry *h;
4c544807
L
1539 Elf_Internal_Sym *isym;
1540 const char *name;
06a6a421 1541 bfd_boolean size_reloc;
70256ad8 1542
351f65ca
L
1543 r_symndx = htab->r_sym (rel->r_info);
1544 r_type = ELF32_R_TYPE (rel->r_info);
c434dee6
AJ
1545
1546 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1547 {
d003868e
AM
1548 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1549 abfd, r_symndx);
b34976b6 1550 return FALSE;
c434dee6
AJ
1551 }
1552
70256ad8 1553 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
1554 {
1555 /* A local symbol. */
c2e61a4e
L
1556 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1557 abfd, r_symndx);
1558 if (isym == NULL)
1559 return FALSE;
c25bc9fc
L
1560
1561 /* Check relocation against local STT_GNU_IFUNC symbol. */
351f65ca 1562 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c25bc9fc 1563 {
351f65ca
L
1564 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel,
1565 TRUE);
c25bc9fc 1566 if (h == NULL)
c2e61a4e 1567 return FALSE;
6bbec505 1568
c25bc9fc
L
1569 /* Fake a STT_GNU_IFUNC symbol. */
1570 h->type = STT_GNU_IFUNC;
1571 h->def_regular = 1;
1572 h->ref_regular = 1;
1573 h->forced_local = 1;
1574 h->root.type = bfd_link_hash_defined;
1575 }
1576 else
1577 h = NULL;
1578 }
70256ad8 1579 else
71cb9464 1580 {
4c544807 1581 isym = NULL;
71cb9464
L
1582 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1583 while (h->root.type == bfd_link_hash_indirect
1584 || h->root.type == bfd_link_hash_warning)
1585 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 1586 }
cbe950e9 1587
d1534d16
L
1588 /* Check invalid x32 relocations. */
1589 if (!ABI_64_P (abfd))
1590 switch (r_type)
1591 {
1592 default:
1593 break;
1594
d1534d16
L
1595 case R_X86_64_DTPOFF64:
1596 case R_X86_64_TPOFF64:
1597 case R_X86_64_PC64:
1598 case R_X86_64_GOTOFF64:
1599 case R_X86_64_GOT64:
1600 case R_X86_64_GOTPCREL64:
1601 case R_X86_64_GOTPC64:
1602 case R_X86_64_GOTPLT64:
1603 case R_X86_64_PLTOFF64:
1604 {
1605 if (h)
1606 name = h->root.root.string;
1607 else
1608 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1609 NULL);
1610 (*_bfd_error_handler)
1611 (_("%B: relocation %s against symbol `%s' isn't "
1612 "supported in x32 mode"), abfd,
1613 x86_64_elf_howto_table[r_type].name, name);
1614 bfd_set_error (bfd_error_bad_value);
1615 return FALSE;
1616 }
1617 break;
1618 }
1619
c25bc9fc
L
1620 if (h != NULL)
1621 {
cbe950e9
L
1622 /* Create the ifunc sections for static executables. If we
1623 never see an indirect function symbol nor we are building
1624 a static executable, those sections will be empty and
1625 won't appear in output. */
1626 switch (r_type)
1627 {
1628 default:
1629 break;
1630
0ff2b86e
L
1631 case R_X86_64_PC32_BND:
1632 case R_X86_64_PLT32_BND:
1633 /* MPX PLT is supported only if elf_x86_64_arch_bed
1634 is used in 64-bit mode. */
1635 if (ABI_64_P (abfd)
1636 && (get_elf_x86_64_backend_data (abfd)
1637 == &elf_x86_64_arch_bed))
1638 {
1639 elf_x86_64_hash_entry (h)->has_bnd_reloc = TRUE;
1640
1641 /* Create the second PLT for Intel MPX support. */
1642 if (htab->plt_bnd == NULL)
1643 {
1644 unsigned int plt_bnd_align;
1645 const struct elf_backend_data *bed;
1646
1647 bed = get_elf_backend_data (info->output_bfd);
1648 switch (sizeof (elf_x86_64_bnd_plt2_entry))
1649 {
1650 case 8:
1651 plt_bnd_align = 3;
1652 break;
1653 case 16:
1654 plt_bnd_align = 4;
1655 break;
1656 default:
1657 abort ();
1658 }
1659
1660 if (htab->elf.dynobj == NULL)
1661 htab->elf.dynobj = abfd;
1662 htab->plt_bnd
1663 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
1664 ".plt.bnd",
1665 (bed->dynamic_sec_flags
1666 | SEC_ALLOC
1667 | SEC_CODE
1668 | SEC_LOAD
1669 | SEC_READONLY));
1670 if (htab->plt_bnd == NULL
1671 || !bfd_set_section_alignment (htab->elf.dynobj,
1672 htab->plt_bnd,
1673 plt_bnd_align))
1674 return FALSE;
1675 }
1676 }
1677
cbe950e9
L
1678 case R_X86_64_32S:
1679 case R_X86_64_32:
1680 case R_X86_64_64:
1681 case R_X86_64_PC32:
1682 case R_X86_64_PC64:
1683 case R_X86_64_PLT32:
1684 case R_X86_64_GOTPCREL:
1685 case R_X86_64_GOTPCREL64:
9d4057ee
AM
1686 if (htab->elf.dynobj == NULL)
1687 htab->elf.dynobj = abfd;
1688 if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
c2e61a4e 1689 return FALSE;
cbe950e9
L
1690 break;
1691 }
1692
ad1e85de
L
1693 /* It is referenced by a non-shared object. */
1694 h->ref_regular = 1;
61315175 1695 h->root.non_ir_ref = 1;
71cb9464 1696 }
70256ad8 1697
351f65ca
L
1698 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1699 symtab_hdr, sym_hashes,
1700 &r_type, GOT_UNKNOWN,
1701 rel, rel_end, h, r_symndx))
c2e61a4e 1702 return FALSE;
142411ca 1703
bffbf940 1704 switch (r_type)
70256ad8 1705 {
bffbf940
JJ
1706 case R_X86_64_TLSLD:
1707 htab->tls_ld_got.refcount += 1;
1708 goto create_got;
1709
1710 case R_X86_64_TPOFF32:
351f65ca 1711 if (!info->executable && ABI_64_P (abfd))
70256ad8 1712 {
09a24cbf 1713 if (h)
4c544807
L
1714 name = h->root.root.string;
1715 else
1716 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1717 NULL);
bffbf940 1718 (*_bfd_error_handler)
d003868e
AM
1719 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1720 abfd,
4c544807 1721 x86_64_elf_howto_table[r_type].name, name);
bffbf940 1722 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1723 return FALSE;
70256ad8 1724 }
bffbf940 1725 break;
c434dee6 1726
bffbf940 1727 case R_X86_64_GOTTPOFF:
1d85728f 1728 if (!info->executable)
bffbf940
JJ
1729 info->flags |= DF_STATIC_TLS;
1730 /* Fall through */
70256ad8 1731
bffbf940
JJ
1732 case R_X86_64_GOT32:
1733 case R_X86_64_GOTPCREL:
1734 case R_X86_64_TLSGD:
7b81dfbb
AJ
1735 case R_X86_64_GOT64:
1736 case R_X86_64_GOTPCREL64:
1737 case R_X86_64_GOTPLT64:
67a4f2b7
AO
1738 case R_X86_64_GOTPC32_TLSDESC:
1739 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
1740 /* This symbol requires a global offset table entry. */
1741 {
1742 int tls_type, old_tls_type;
1743
1744 switch (r_type)
1745 {
1746 default: tls_type = GOT_NORMAL; break;
1747 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1748 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
67a4f2b7
AO
1749 case R_X86_64_GOTPC32_TLSDESC:
1750 case R_X86_64_TLSDESC_CALL:
1751 tls_type = GOT_TLS_GDESC; break;
bffbf940
JJ
1752 }
1753
1754 if (h != NULL)
1755 {
7b81dfbb
AJ
1756 if (r_type == R_X86_64_GOTPLT64)
1757 {
1758 /* This relocation indicates that we also need
1759 a PLT entry, as this is a function. We don't need
1760 a PLT entry for local symbols. */
1761 h->needs_plt = 1;
1762 h->plt.refcount += 1;
1763 }
bffbf940 1764 h->got.refcount += 1;
351f65ca 1765 old_tls_type = elf_x86_64_hash_entry (h)->tls_type;
bffbf940
JJ
1766 }
1767 else
1768 {
1769 bfd_signed_vma *local_got_refcounts;
1770
1771 /* This is a global offset table entry for a local symbol. */
1772 local_got_refcounts = elf_local_got_refcounts (abfd);
1773 if (local_got_refcounts == NULL)
1774 {
1775 bfd_size_type size;
1776
1777 size = symtab_hdr->sh_info;
67a4f2b7
AO
1778 size *= sizeof (bfd_signed_vma)
1779 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
1780 local_got_refcounts = ((bfd_signed_vma *)
1781 bfd_zalloc (abfd, size));
1782 if (local_got_refcounts == NULL)
c2e61a4e 1783 return FALSE;
bffbf940 1784 elf_local_got_refcounts (abfd) = local_got_refcounts;
351f65ca 1785 elf_x86_64_local_tlsdesc_gotent (abfd)
67a4f2b7 1786 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
351f65ca 1787 elf_x86_64_local_got_tls_type (abfd)
67a4f2b7 1788 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940
JJ
1789 }
1790 local_got_refcounts[r_symndx] += 1;
1791 old_tls_type
351f65ca 1792 = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
bffbf940
JJ
1793 }
1794
1795 /* If a TLS symbol is accessed using IE at least once,
1796 there is no point to use dynamic model for it. */
1797 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
1798 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1799 || tls_type != GOT_TLS_IE))
bffbf940 1800 {
67a4f2b7 1801 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 1802 tls_type = old_tls_type;
67a4f2b7
AO
1803 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1804 && GOT_TLS_GD_ANY_P (tls_type))
1805 tls_type |= old_tls_type;
bffbf940
JJ
1806 else
1807 {
09a24cbf 1808 if (h)
4c544807
L
1809 name = h->root.root.string;
1810 else
1811 name = bfd_elf_sym_name (abfd, symtab_hdr,
1812 isym, NULL);
bffbf940 1813 (*_bfd_error_handler)
1f7a4e42 1814 (_("%B: '%s' accessed both as normal and thread local symbol"),
4c544807 1815 abfd, name);
68c4a57e 1816 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1817 return FALSE;
bffbf940
JJ
1818 }
1819 }
1820
1821 if (old_tls_type != tls_type)
1822 {
1823 if (h != NULL)
351f65ca 1824 elf_x86_64_hash_entry (h)->tls_type = tls_type;
bffbf940 1825 else
351f65ca 1826 elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
bffbf940
JJ
1827 }
1828 }
c434dee6
AJ
1829 /* Fall through */
1830
d6ab8113
JB
1831 case R_X86_64_GOTOFF64:
1832 case R_X86_64_GOTPC32:
7b81dfbb 1833 case R_X86_64_GOTPC64:
bffbf940 1834 create_got:
6de2ae4a 1835 if (htab->elf.sgot == NULL)
c434dee6
AJ
1836 {
1837 if (htab->elf.dynobj == NULL)
1838 htab->elf.dynobj = abfd;
6de2ae4a
L
1839 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1840 info))
c2e61a4e 1841 return FALSE;
c434dee6 1842 }
70256ad8
AJ
1843 break;
1844
1845 case R_X86_64_PLT32:
c3320543 1846 case R_X86_64_PLT32_BND:
70256ad8 1847 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
1848 actually build the entry in adjust_dynamic_symbol,
1849 because this might be a case of linking PIC code which is
1850 never referenced by a dynamic object, in which case we
1851 don't need to generate a procedure linkage table entry
1852 after all. */
70256ad8
AJ
1853
1854 /* If this is a local symbol, we resolve it directly without
407443a3 1855 creating a procedure linkage table entry. */
70256ad8
AJ
1856 if (h == NULL)
1857 continue;
1858
f5385ebf 1859 h->needs_plt = 1;
51b64d56 1860 h->plt.refcount += 1;
70256ad8
AJ
1861 break;
1862
7b81dfbb
AJ
1863 case R_X86_64_PLTOFF64:
1864 /* This tries to form the 'address' of a function relative
1865 to GOT. For global symbols we need a PLT entry. */
1866 if (h != NULL)
1867 {
1868 h->needs_plt = 1;
1869 h->plt.refcount += 1;
1870 }
1871 goto create_got;
1872
6a3e1bae
L
1873 case R_X86_64_SIZE32:
1874 case R_X86_64_SIZE64:
06a6a421 1875 size_reloc = TRUE;
6a3e1bae
L
1876 goto do_size;
1877
248775ba
L
1878 case R_X86_64_32:
1879 if (!ABI_64_P (abfd))
1880 goto pointer;
cc78d0af
AJ
1881 case R_X86_64_8:
1882 case R_X86_64_16:
70256ad8 1883 case R_X86_64_32S:
1b71fb54
AJ
1884 /* Let's help debug shared library creation. These relocs
1885 cannot be used in shared libs. Don't error out for
1886 sections we don't care about, such as debug sections or
1887 non-constant sections. */
1888 if (info->shared
1889 && (sec->flags & SEC_ALLOC) != 0
1890 && (sec->flags & SEC_READONLY) != 0)
1891 {
09a24cbf 1892 if (h)
4c544807
L
1893 name = h->root.root.string;
1894 else
1895 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1b71fb54 1896 (*_bfd_error_handler)
d003868e 1897 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
4c544807 1898 abfd, x86_64_elf_howto_table[r_type].name, name);
1b71fb54 1899 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1900 return FALSE;
1b71fb54
AJ
1901 }
1902 /* Fall through. */
1903
c434dee6
AJ
1904 case R_X86_64_PC8:
1905 case R_X86_64_PC16:
70256ad8 1906 case R_X86_64_PC32:
c3320543 1907 case R_X86_64_PC32_BND:
d6ab8113 1908 case R_X86_64_PC64:
1b71fb54 1909 case R_X86_64_64:
248775ba 1910pointer:
710ab287 1911 if (h != NULL && info->executable)
c434dee6
AJ
1912 {
1913 /* If this reloc is in a read-only section, we might
1914 need a copy reloc. We can't check reliably at this
1915 stage whether the section is read-only, as input
1916 sections have not yet been mapped to output sections.
1917 Tentatively set the flag for now, and correct in
1918 adjust_dynamic_symbol. */
f5385ebf 1919 h->non_got_ref = 1;
c434dee6
AJ
1920
1921 /* We may need a .plt entry if the function this reloc
1922 refers to is in a shared lib. */
1923 h->plt.refcount += 1;
c3320543
L
1924 if (r_type != R_X86_64_PC32
1925 && r_type != R_X86_64_PC32_BND
1926 && r_type != R_X86_64_PC64)
f5385ebf 1927 h->pointer_equality_needed = 1;
c434dee6 1928 }
70256ad8 1929
06a6a421 1930 size_reloc = FALSE;
6a3e1bae 1931do_size:
70256ad8
AJ
1932 /* If we are creating a shared library, and this is a reloc
1933 against a global symbol, or a non PC relative reloc
1934 against a local symbol, then we need to copy the reloc
1935 into the shared library. However, if we are linking with
1936 -Bsymbolic, we do not need to copy a reloc against a
1937 global symbol which is defined in an object we are
407443a3 1938 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
1939 this point we have not seen all the input files, so it is
1940 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
1941 later (it is never cleared). In case of a weak definition,
1942 DEF_REGULAR may be cleared later by a strong definition in
1943 a shared library. We account for that possibility below by
1944 storing information in the relocs_copied field of the hash
1945 table entry. A similar situation occurs when creating
1946 shared libraries and symbol visibility changes render the
1947 symbol local.
1948
1949 If on the other hand, we are creating an executable, we
1950 may need to keep relocations for symbols satisfied by a
1951 dynamic library if we manage to avoid copy relocs for the
0f88be7a 1952 symbol. */
c434dee6
AJ
1953 if ((info->shared
1954 && (sec->flags & SEC_ALLOC) != 0
d8045f23 1955 && (! IS_X86_64_PCREL_TYPE (r_type)
c434dee6 1956 || (h != NULL
55255dae 1957 && (! SYMBOLIC_BIND (info, h)
c434dee6 1958 || h->root.type == bfd_link_hash_defweak
f5385ebf 1959 || !h->def_regular))))
d40d037c
AJ
1960 || (ELIMINATE_COPY_RELOCS
1961 && !info->shared
c434dee6
AJ
1962 && (sec->flags & SEC_ALLOC) != 0
1963 && h != NULL
1964 && (h->root.type == bfd_link_hash_defweak
0f88be7a 1965 || !h->def_regular)))
70256ad8 1966 {
e03a8ed8
L
1967 struct elf_dyn_relocs *p;
1968 struct elf_dyn_relocs **head;
c434dee6
AJ
1969
1970 /* We must copy these reloc types into the output file.
1971 Create a reloc section in dynobj and make room for
1972 this reloc. */
70256ad8
AJ
1973 if (sreloc == NULL)
1974 {
c434dee6
AJ
1975 if (htab->elf.dynobj == NULL)
1976 htab->elf.dynobj = abfd;
1977
83bac4b0 1978 sreloc = _bfd_elf_make_dynamic_reloc_section
82e96e07
L
1979 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
1980 abfd, /*rela?*/ TRUE);
70256ad8 1981
70256ad8 1982 if (sreloc == NULL)
c2e61a4e 1983 return FALSE;
70256ad8
AJ
1984 }
1985
c434dee6
AJ
1986 /* If this is a global symbol, we count the number of
1987 relocations we need for this symbol. */
1988 if (h != NULL)
70256ad8 1989 {
351f65ca 1990 head = &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs;
c434dee6
AJ
1991 }
1992 else
1993 {
1994 /* Track dynamic relocs needed for local syms too.
1995 We really need local syms available to do this
1996 easily. Oh well. */
c434dee6 1997 asection *s;
87d72d41 1998 void **vpp;
87d72d41
AM
1999
2000 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2001 abfd, r_symndx);
2002 if (isym == NULL)
2003 return FALSE;
2004
2005 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
c434dee6 2006 if (s == NULL)
87d72d41 2007 s = sec;
70256ad8 2008
e81d3500
DD
2009 /* Beware of type punned pointers vs strict aliasing
2010 rules. */
2011 vpp = &(elf_section_data (s)->local_dynrel);
e03a8ed8 2012 head = (struct elf_dyn_relocs **)vpp;
c434dee6 2013 }
70256ad8 2014
c434dee6
AJ
2015 p = *head;
2016 if (p == NULL || p->sec != sec)
2017 {
2018 bfd_size_type amt = sizeof *p;
d8045f23 2019
e03a8ed8 2020 p = ((struct elf_dyn_relocs *)
c434dee6 2021 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 2022 if (p == NULL)
c2e61a4e 2023 return FALSE;
c434dee6
AJ
2024 p->next = *head;
2025 *head = p;
2026 p->sec = sec;
2027 p->count = 0;
2028 p->pc_count = 0;
70256ad8 2029 }
c434dee6
AJ
2030
2031 p->count += 1;
06a6a421
L
2032 /* Count size relocation as PC-relative relocation. */
2033 if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
c434dee6 2034 p->pc_count += 1;
70256ad8
AJ
2035 }
2036 break;
fe4770f4
AJ
2037
2038 /* This relocation describes the C++ object vtable hierarchy.
2039 Reconstruct it for later use during GC. */
2040 case R_X86_64_GNU_VTINHERIT:
c152c796 2041 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
c2e61a4e 2042 return FALSE;
fe4770f4
AJ
2043 break;
2044
2045 /* This relocation describes which C++ vtable entries are actually
2046 used. Record for later use during GC. */
2047 case R_X86_64_GNU_VTENTRY:
d17e0c6e
JB
2048 BFD_ASSERT (h != NULL);
2049 if (h != NULL
2050 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
c2e61a4e 2051 return FALSE;
fe4770f4 2052 break;
c434dee6
AJ
2053
2054 default:
2055 break;
70256ad8
AJ
2056 }
2057 }
2058
b34976b6 2059 return TRUE;
70256ad8
AJ
2060}
2061
2062/* Return the section that should be marked against GC for a given
407443a3 2063 relocation. */
70256ad8
AJ
2064
2065static asection *
351f65ca
L
2066elf_x86_64_gc_mark_hook (asection *sec,
2067 struct bfd_link_info *info,
2068 Elf_Internal_Rela *rel,
2069 struct elf_link_hash_entry *h,
2070 Elf_Internal_Sym *sym)
70256ad8
AJ
2071{
2072 if (h != NULL)
351f65ca 2073 switch (ELF32_R_TYPE (rel->r_info))
07adf181
AM
2074 {
2075 case R_X86_64_GNU_VTINHERIT:
2076 case R_X86_64_GNU_VTENTRY:
2077 return NULL;
2078 }
2079
2080 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
70256ad8
AJ
2081}
2082
407443a3 2083/* Update the got entry reference counts for the section being removed. */
70256ad8 2084
b34976b6 2085static bfd_boolean
351f65ca
L
2086elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
2087 asection *sec,
2088 const Elf_Internal_Rela *relocs)
70256ad8 2089{
351f65ca 2090 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
2091 Elf_Internal_Shdr *symtab_hdr;
2092 struct elf_link_hash_entry **sym_hashes;
2093 bfd_signed_vma *local_got_refcounts;
2094 const Elf_Internal_Rela *rel, *relend;
c434dee6 2095
7dda2462
TG
2096 if (info->relocatable)
2097 return TRUE;
2098
351f65ca 2099 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2100 if (htab == NULL)
2101 return FALSE;
2102
c434dee6 2103 elf_section_data (sec)->local_dynrel = NULL;
70256ad8 2104
0ffa91dd 2105 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8
AJ
2106 sym_hashes = elf_sym_hashes (abfd);
2107 local_got_refcounts = elf_local_got_refcounts (abfd);
2108
351f65ca 2109 htab = elf_x86_64_hash_table (info);
70256ad8
AJ
2110 relend = relocs + sec->reloc_count;
2111 for (rel = relocs; rel < relend; rel++)
26e41594
AM
2112 {
2113 unsigned long r_symndx;
2114 unsigned int r_type;
2115 struct elf_link_hash_entry *h = NULL;
70256ad8 2116
351f65ca 2117 r_symndx = htab->r_sym (rel->r_info);
26e41594
AM
2118 if (r_symndx >= symtab_hdr->sh_info)
2119 {
26e41594 2120 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
2121 while (h->root.type == bfd_link_hash_indirect
2122 || h->root.type == bfd_link_hash_warning)
2123 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 2124 }
bb1cb422
L
2125 else
2126 {
2127 /* A local symbol. */
2128 Elf_Internal_Sym *isym;
2129
2130 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2131 abfd, r_symndx);
2132
2133 /* Check relocation against local STT_GNU_IFUNC symbol. */
2134 if (isym != NULL
82e96e07 2135 && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
bb1cb422 2136 {
351f65ca 2137 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel, FALSE);
bb1cb422
L
2138 if (h == NULL)
2139 abort ();
2140 }
2141 }
c434dee6 2142
3db2e7dd
L
2143 if (h)
2144 {
2145 struct elf_x86_64_link_hash_entry *eh;
2146 struct elf_dyn_relocs **pp;
2147 struct elf_dyn_relocs *p;
2148
2149 eh = (struct elf_x86_64_link_hash_entry *) h;
2150
2151 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
2152 if (p->sec == sec)
2153 {
2154 /* Everything must go for SEC. */
2155 *pp = p->next;
2156 break;
2157 }
2158 }
2159
351f65ca
L
2160 r_type = ELF32_R_TYPE (rel->r_info);
2161 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
2162 symtab_hdr, sym_hashes,
2163 &r_type, GOT_UNKNOWN,
2164 rel, relend, h, r_symndx))
142411ca
L
2165 return FALSE;
2166
26e41594
AM
2167 switch (r_type)
2168 {
2169 case R_X86_64_TLSLD:
4dfe6ac6
NC
2170 if (htab->tls_ld_got.refcount > 0)
2171 htab->tls_ld_got.refcount -= 1;
26e41594 2172 break;
c434dee6 2173
26e41594 2174 case R_X86_64_TLSGD:
67a4f2b7
AO
2175 case R_X86_64_GOTPC32_TLSDESC:
2176 case R_X86_64_TLSDESC_CALL:
26e41594
AM
2177 case R_X86_64_GOTTPOFF:
2178 case R_X86_64_GOT32:
2179 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
2180 case R_X86_64_GOT64:
2181 case R_X86_64_GOTPCREL64:
2182 case R_X86_64_GOTPLT64:
26e41594
AM
2183 if (h != NULL)
2184 {
7b81dfbb 2185 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
eed180f8 2186 h->plt.refcount -= 1;
26e41594
AM
2187 if (h->got.refcount > 0)
2188 h->got.refcount -= 1;
bb1cb422
L
2189 if (h->type == STT_GNU_IFUNC)
2190 {
2191 if (h->plt.refcount > 0)
2192 h->plt.refcount -= 1;
2193 }
26e41594
AM
2194 }
2195 else if (local_got_refcounts != NULL)
2196 {
2197 if (local_got_refcounts[r_symndx] > 0)
2198 local_got_refcounts[r_symndx] -= 1;
2199 }
2200 break;
c434dee6 2201
26e41594
AM
2202 case R_X86_64_8:
2203 case R_X86_64_16:
2204 case R_X86_64_32:
2205 case R_X86_64_64:
2206 case R_X86_64_32S:
2207 case R_X86_64_PC8:
2208 case R_X86_64_PC16:
2209 case R_X86_64_PC32:
c3320543 2210 case R_X86_64_PC32_BND:
d6ab8113 2211 case R_X86_64_PC64:
1788fc08
L
2212 case R_X86_64_SIZE32:
2213 case R_X86_64_SIZE64:
3db2e7dd
L
2214 if (info->shared
2215 && (h == NULL || h->type != STT_GNU_IFUNC))
26e41594
AM
2216 break;
2217 /* Fall thru */
c434dee6 2218
26e41594 2219 case R_X86_64_PLT32:
c3320543 2220 case R_X86_64_PLT32_BND:
7b81dfbb 2221 case R_X86_64_PLTOFF64:
26e41594
AM
2222 if (h != NULL)
2223 {
2224 if (h->plt.refcount > 0)
2225 h->plt.refcount -= 1;
2226 }
2227 break;
70256ad8 2228
26e41594
AM
2229 default:
2230 break;
2231 }
2232 }
70256ad8 2233
b34976b6 2234 return TRUE;
70256ad8
AJ
2235}
2236
2237/* Adjust a symbol defined by a dynamic object and referenced by a
2238 regular object. The current definition is in some section of the
2239 dynamic object, but we're not including those sections. We have to
2240 change the definition to something the rest of the link can
407443a3 2241 understand. */
70256ad8 2242
b34976b6 2243static bfd_boolean
351f65ca
L
2244elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2245 struct elf_link_hash_entry *h)
70256ad8 2246{
351f65ca 2247 struct elf_x86_64_link_hash_table *htab;
70256ad8 2248 asection *s;
5ca5bb35
L
2249 struct elf_x86_64_link_hash_entry *eh;
2250 struct elf_dyn_relocs *p;
70256ad8 2251
cbe950e9
L
2252 /* STT_GNU_IFUNC symbol must go through PLT. */
2253 if (h->type == STT_GNU_IFUNC)
2254 {
73bcf233
L
2255 /* All local STT_GNU_IFUNC references must be treate as local
2256 calls via local PLT. */
5ca5bb35
L
2257 if (h->ref_regular
2258 && SYMBOL_CALLS_LOCAL (info, h))
2259 {
73bcf233 2260 bfd_size_type pc_count = 0, count = 0;
5ca5bb35
L
2261 struct elf_dyn_relocs **pp;
2262
2263 eh = (struct elf_x86_64_link_hash_entry *) h;
2264 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2265 {
2266 pc_count += p->pc_count;
2267 p->count -= p->pc_count;
2268 p->pc_count = 0;
73bcf233 2269 count += p->count;
5ca5bb35
L
2270 if (p->count == 0)
2271 *pp = p->next;
2272 else
2273 pp = &p->next;
2274 }
2275
73bcf233 2276 if (pc_count || count)
5ca5bb35
L
2277 {
2278 h->needs_plt = 1;
5ca5bb35 2279 h->non_got_ref = 1;
a5479e5f
L
2280 if (h->plt.refcount <= 0)
2281 h->plt.refcount = 1;
2282 else
2283 h->plt.refcount += 1;
5ca5bb35
L
2284 }
2285 }
2286
cbe950e9
L
2287 if (h->plt.refcount <= 0)
2288 {
2289 h->plt.offset = (bfd_vma) -1;
2290 h->needs_plt = 0;
2291 }
2292 return TRUE;
2293 }
2294
70256ad8
AJ
2295 /* If this is a function, put it in the procedure linkage table. We
2296 will fill in the contents of the procedure linkage table later,
2297 when we know the address of the .got section. */
2298 if (h->type == STT_FUNC
f5385ebf 2299 || h->needs_plt)
70256ad8 2300 {
c434dee6 2301 if (h->plt.refcount <= 0
27482721
AJ
2302 || SYMBOL_CALLS_LOCAL (info, h)
2303 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2304 && h->root.type == bfd_link_hash_undefweak))
70256ad8 2305 {
70256ad8
AJ
2306 /* This case can occur if we saw a PLT32 reloc in an input
2307 file, but the symbol was never referred to by a dynamic
2308 object, or if all references were garbage collected. In
2309 such a case, we don't actually need to build a procedure
2310 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 2311 h->plt.offset = (bfd_vma) -1;
f5385ebf 2312 h->needs_plt = 0;
70256ad8
AJ
2313 }
2314
b34976b6 2315 return TRUE;
70256ad8 2316 }
bbd7ec4a 2317 else
c434dee6
AJ
2318 /* It's possible that we incorrectly decided a .plt reloc was
2319 needed for an R_X86_64_PC32 reloc to a non-function sym in
2320 check_relocs. We can't decide accurately between function and
2321 non-function syms in check-relocs; Objects loaded later in
2322 the link may change h->type. So fix it now. */
bbd7ec4a 2323 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
2324
2325 /* If this is a weak symbol, and there is a real definition, the
2326 processor independent code will have arranged for us to see the
407443a3 2327 real definition first, and we can just use the same value. */
f6e332e6 2328 if (h->u.weakdef != NULL)
70256ad8 2329 {
f6e332e6
AM
2330 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2331 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2332 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2333 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 2334 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 2335 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 2336 return TRUE;
70256ad8
AJ
2337 }
2338
2339 /* This is a reference to a symbol defined by a dynamic object which
407443a3 2340 is not a function. */
70256ad8
AJ
2341
2342 /* If we are creating a shared library, we must presume that the
2343 only references to the symbol are via the global offset table.
2344 For such cases we need not do anything here; the relocations will
407443a3 2345 be handled correctly by relocate_section. */
70256ad8 2346 if (info->shared)
b34976b6 2347 return TRUE;
70256ad8
AJ
2348
2349 /* If there are no references to this symbol that do not use the
2350 GOT, we don't need to generate a copy reloc. */
f5385ebf 2351 if (!h->non_got_ref)
b34976b6 2352 return TRUE;
70256ad8 2353
c434dee6
AJ
2354 /* If -z nocopyreloc was given, we won't generate them either. */
2355 if (info->nocopyreloc)
2356 {
f5385ebf 2357 h->non_got_ref = 0;
b34976b6 2358 return TRUE;
c434dee6
AJ
2359 }
2360
d40d037c 2361 if (ELIMINATE_COPY_RELOCS)
c434dee6 2362 {
351f65ca 2363 eh = (struct elf_x86_64_link_hash_entry *) h;
d40d037c
AJ
2364 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2365 {
2366 s = p->sec->output_section;
2367 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2368 break;
2369 }
2370
2371 /* If we didn't find any dynamic relocs in read-only sections, then
2372 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2373 if (p == NULL)
2374 {
f5385ebf 2375 h->non_got_ref = 0;
d40d037c
AJ
2376 return TRUE;
2377 }
c434dee6
AJ
2378 }
2379
70256ad8 2380 /* We must allocate the symbol in our .dynbss section, which will
407443a3 2381 become part of the .bss section of the executable. There will be
70256ad8
AJ
2382 an entry for this symbol in the .dynsym section. The dynamic
2383 object will contain position independent code, so all references
2384 from the dynamic object to this symbol will go through the global
2385 offset table. The dynamic linker will use the .dynsym entry to
2386 determine the address it must put in the global offset table, so
2387 both the dynamic object and the regular object will refer to the
2388 same memory location for the variable. */
2389
351f65ca 2390 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2391 if (htab == NULL)
2392 return FALSE;
70256ad8
AJ
2393
2394 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2395 to copy the initial value out of the dynamic object and into the
cedb70c5 2396 runtime process image. */
1d7e9d18 2397 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
70256ad8 2398 {
351f65ca
L
2399 const struct elf_backend_data *bed;
2400 bed = get_elf_backend_data (info->output_bfd);
2401 htab->srelbss->size += bed->s->sizeof_rela;
f5385ebf 2402 h->needs_copy = 1;
70256ad8
AJ
2403 }
2404
c434dee6 2405 s = htab->sdynbss;
70256ad8 2406
027297b7 2407 return _bfd_elf_adjust_dynamic_copy (h, s);
70256ad8
AJ
2408}
2409
c434dee6
AJ
2410/* Allocate space in .plt, .got and associated reloc sections for
2411 dynamic relocs. */
2412
b34976b6 2413static bfd_boolean
351f65ca 2414elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
2415{
2416 struct bfd_link_info *info;
351f65ca
L
2417 struct elf_x86_64_link_hash_table *htab;
2418 struct elf_x86_64_link_hash_entry *eh;
e03a8ed8 2419 struct elf_dyn_relocs *p;
351f65ca 2420 const struct elf_backend_data *bed;
eed180f8 2421 unsigned int plt_entry_size;
c434dee6 2422
e92d460e 2423 if (h->root.type == bfd_link_hash_indirect)
b34976b6 2424 return TRUE;
c434dee6 2425
351f65ca 2426 eh = (struct elf_x86_64_link_hash_entry *) h;
e92d460e 2427
c434dee6 2428 info = (struct bfd_link_info *) inf;
351f65ca 2429 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2430 if (htab == NULL)
2431 return FALSE;
351f65ca 2432 bed = get_elf_backend_data (info->output_bfd);
eed180f8 2433 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
c434dee6 2434
cbe950e9
L
2435 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2436 here if it is defined and referenced in a non-shared object. */
2437 if (h->type == STT_GNU_IFUNC
2438 && h->def_regular)
0ff2b86e
L
2439 {
2440 if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2441 &eh->dyn_relocs,
2442 plt_entry_size,
2443 plt_entry_size,
2444 GOT_ENTRY_SIZE))
2445 {
2446 asection *s = htab->plt_bnd;
2447 if (h->plt.offset != (bfd_vma) -1 && s != NULL)
2448 {
2449 /* Use the .plt.bnd section if it is created. */
2450 eh->plt_bnd.offset = s->size;
2451
2452 /* Make room for this entry in the .plt.bnd section. */
2453 s->size += sizeof (elf_x86_64_legacy_plt2_entry);
2454 }
2455
2456 return TRUE;
2457 }
2458 else
2459 return FALSE;
2460 }
cbe950e9
L
2461 else if (htab->elf.dynamic_sections_created
2462 && h->plt.refcount > 0)
c434dee6
AJ
2463 {
2464 /* Make sure this symbol is output as a dynamic symbol.
2465 Undefined weak syms won't yet be marked as dynamic. */
2466 if (h->dynindx == -1
f5385ebf 2467 && !h->forced_local)
c434dee6 2468 {
c152c796 2469 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2470 return FALSE;
c434dee6
AJ
2471 }
2472
27482721
AJ
2473 if (info->shared
2474 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6 2475 {
6de2ae4a 2476 asection *s = htab->elf.splt;
0ff2b86e 2477 asection *bnd_s = htab->plt_bnd;
c434dee6
AJ
2478
2479 /* If this is the first .plt entry, make room for the special
2480 first entry. */
eea6121a 2481 if (s->size == 0)
bb81b736 2482 s->size = plt_entry_size;
c434dee6 2483
eea6121a 2484 h->plt.offset = s->size;
0ff2b86e
L
2485 if (bnd_s)
2486 eh->plt_bnd.offset = bnd_s->size;
c434dee6
AJ
2487
2488 /* If this symbol is not defined in a regular file, and we are
2489 not generating a shared library, then set the symbol to this
2490 location in the .plt. This is required to make function
2491 pointers compare as equal between the normal executable and
2492 the shared library. */
2493 if (! info->shared
f5385ebf 2494 && !h->def_regular)
c434dee6 2495 {
0ff2b86e
L
2496 if (bnd_s)
2497 {
2498 /* We need to make a call to the entry of the second
2499 PLT instead of regular PLT entry. */
2500 h->root.u.def.section = bnd_s;
2501 h->root.u.def.value = eh->plt_bnd.offset;
2502 }
2503 else
2504 {
2505 h->root.u.def.section = s;
2506 h->root.u.def.value = h->plt.offset;
2507 }
c434dee6
AJ
2508 }
2509
2510 /* Make room for this entry. */
eed180f8 2511 s->size += plt_entry_size;
0ff2b86e
L
2512 if (bnd_s)
2513 {
2514 BFD_ASSERT (sizeof (elf_x86_64_bnd_plt2_entry)
2515 == sizeof (elf_x86_64_legacy_plt2_entry));
2516 bnd_s->size += sizeof (elf_x86_64_legacy_plt2_entry);
2517 }
c434dee6
AJ
2518
2519 /* We also need to make an entry in the .got.plt section, which
2520 will be placed in the .got section by the linker script. */
6de2ae4a 2521 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
c434dee6
AJ
2522
2523 /* We also need to make an entry in the .rela.plt section. */
351f65ca 2524 htab->elf.srelplt->size += bed->s->sizeof_rela;
6de2ae4a 2525 htab->elf.srelplt->reloc_count++;
c434dee6
AJ
2526 }
2527 else
2528 {
2529 h->plt.offset = (bfd_vma) -1;
f5385ebf 2530 h->needs_plt = 0;
c434dee6
AJ
2531 }
2532 }
2533 else
2534 {
2535 h->plt.offset = (bfd_vma) -1;
f5385ebf 2536 h->needs_plt = 0;
c434dee6
AJ
2537 }
2538
67a4f2b7
AO
2539 eh->tlsdesc_got = (bfd_vma) -1;
2540
bffbf940
JJ
2541 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2542 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
2543 if (h->got.refcount > 0
1d85728f 2544 && info->executable
bffbf940 2545 && h->dynindx == -1
351f65ca 2546 && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
d8045f23
NC
2547 {
2548 h->got.offset = (bfd_vma) -1;
2549 }
bffbf940 2550 else if (h->got.refcount > 0)
c434dee6
AJ
2551 {
2552 asection *s;
b34976b6 2553 bfd_boolean dyn;
351f65ca 2554 int tls_type = elf_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
2555
2556 /* Make sure this symbol is output as a dynamic symbol.
2557 Undefined weak syms won't yet be marked as dynamic. */
2558 if (h->dynindx == -1
f5385ebf 2559 && !h->forced_local)
c434dee6 2560 {
c152c796 2561 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2562 return FALSE;
c434dee6
AJ
2563 }
2564
67a4f2b7
AO
2565 if (GOT_TLS_GDESC_P (tls_type))
2566 {
6de2ae4a 2567 eh->tlsdesc_got = htab->elf.sgotplt->size
351f65ca 2568 - elf_x86_64_compute_jump_table_size (htab);
6de2ae4a 2569 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
67a4f2b7
AO
2570 h->got.offset = (bfd_vma) -2;
2571 }
2572 if (! GOT_TLS_GDESC_P (tls_type)
2573 || GOT_TLS_GD_P (tls_type))
2574 {
6de2ae4a 2575 s = htab->elf.sgot;
67a4f2b7
AO
2576 h->got.offset = s->size;
2577 s->size += GOT_ENTRY_SIZE;
2578 if (GOT_TLS_GD_P (tls_type))
2579 s->size += GOT_ENTRY_SIZE;
2580 }
c434dee6 2581 dyn = htab->elf.dynamic_sections_created;
bffbf940
JJ
2582 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2583 and two if global.
2584 R_X86_64_GOTTPOFF needs one dynamic relocation. */
67a4f2b7 2585 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
bffbf940 2586 || tls_type == GOT_TLS_IE)
351f65ca 2587 htab->elf.srelgot->size += bed->s->sizeof_rela;
67a4f2b7 2588 else if (GOT_TLS_GD_P (tls_type))
351f65ca 2589 htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
67a4f2b7
AO
2590 else if (! GOT_TLS_GDESC_P (tls_type)
2591 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2592 || h->root.type != bfd_link_hash_undefweak)
27482721
AJ
2593 && (info->shared
2594 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
351f65ca 2595 htab->elf.srelgot->size += bed->s->sizeof_rela;
67a4f2b7
AO
2596 if (GOT_TLS_GDESC_P (tls_type))
2597 {
351f65ca 2598 htab->elf.srelplt->size += bed->s->sizeof_rela;
67a4f2b7
AO
2599 htab->tlsdesc_plt = (bfd_vma) -1;
2600 }
c434dee6
AJ
2601 }
2602 else
2603 h->got.offset = (bfd_vma) -1;
2604
c434dee6 2605 if (eh->dyn_relocs == NULL)
b34976b6 2606 return TRUE;
c434dee6
AJ
2607
2608 /* In the shared -Bsymbolic case, discard space allocated for
2609 dynamic pc-relative relocs against symbols which turn out to be
2610 defined in regular objects. For the normal shared case, discard
2611 space for pc-relative relocs that have become local due to symbol
2612 visibility changes. */
2613
2614 if (info->shared)
2615 {
27482721
AJ
2616 /* Relocs that use pc_count are those that appear on a call
2617 insn, or certain REL relocs that can generated via assembly.
2618 We want calls to protected symbols to resolve directly to the
2619 function rather than going via the plt. If people want
2620 function pointer comparisons to work as expected then they
2621 should avoid writing weird assembly. */
2622 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6 2623 {
e03a8ed8 2624 struct elf_dyn_relocs **pp;
c434dee6
AJ
2625
2626 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2627 {
c3ce498c
L
2628 p->count -= p->pc_count;
2629 p->pc_count = 0;
c434dee6
AJ
2630 if (p->count == 0)
2631 *pp = p->next;
2632 else
2633 pp = &p->next;
2634 }
2635 }
4e795f50
AM
2636
2637 /* Also discard relocs on undefined weak syms with non-default
c353e543 2638 visibility. */
22d606e9 2639 if (eh->dyn_relocs != NULL
c353e543 2640 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
2641 {
2642 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2643 eh->dyn_relocs = NULL;
2644
2645 /* Make sure undefined weak symbols are output as a dynamic
2646 symbol in PIEs. */
2647 else if (h->dynindx == -1
d8045f23
NC
2648 && ! h->forced_local
2649 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2650 return FALSE;
22d606e9 2651 }
cbe950e9 2652
d8045f23 2653 }
d40d037c 2654 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
2655 {
2656 /* For the non-shared case, discard space for relocs against
2657 symbols which turn out to need copy relocs or are not
2658 dynamic. */
2659
f5385ebf
AM
2660 if (!h->non_got_ref
2661 && ((h->def_dynamic
2662 && !h->def_regular)
c434dee6
AJ
2663 || (htab->elf.dynamic_sections_created
2664 && (h->root.type == bfd_link_hash_undefweak
2665 || h->root.type == bfd_link_hash_undefined))))
2666 {
2667 /* Make sure this symbol is output as a dynamic symbol.
2668 Undefined weak syms won't yet be marked as dynamic. */
2669 if (h->dynindx == -1
d8045f23
NC
2670 && ! h->forced_local
2671 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2672 return FALSE;
c434dee6
AJ
2673
2674 /* If that succeeded, we know we'll be keeping all the
2675 relocs. */
2676 if (h->dynindx != -1)
2677 goto keep;
2678 }
2679
2680 eh->dyn_relocs = NULL;
2681
2682 keep: ;
2683 }
2684
2685 /* Finally, allocate space. */
2686 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2687 {
e7c33416
NC
2688 asection * sreloc;
2689
cbe950e9 2690 sreloc = elf_section_data (p->sec)->sreloc;
e7c33416
NC
2691
2692 BFD_ASSERT (sreloc != NULL);
2693
351f65ca 2694 sreloc->size += p->count * bed->s->sizeof_rela;
c434dee6
AJ
2695 }
2696
b34976b6 2697 return TRUE;
c434dee6
AJ
2698}
2699
c25bc9fc
L
2700/* Allocate space in .plt, .got and associated reloc sections for
2701 local dynamic relocs. */
2702
2703static bfd_boolean
351f65ca 2704elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
c25bc9fc
L
2705{
2706 struct elf_link_hash_entry *h
2707 = (struct elf_link_hash_entry *) *slot;
2708
2709 if (h->type != STT_GNU_IFUNC
2710 || !h->def_regular
2711 || !h->ref_regular
2712 || !h->forced_local
2713 || h->root.type != bfd_link_hash_defined)
2714 abort ();
2715
351f65ca 2716 return elf_x86_64_allocate_dynrelocs (h, inf);
c25bc9fc
L
2717}
2718
c434dee6
AJ
2719/* Find any dynamic relocs that apply to read-only sections. */
2720
b34976b6 2721static bfd_boolean
351f65ca
L
2722elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
2723 void * inf)
c434dee6 2724{
351f65ca 2725 struct elf_x86_64_link_hash_entry *eh;
e03a8ed8 2726 struct elf_dyn_relocs *p;
c434dee6 2727
aa715242
L
2728 /* Skip local IFUNC symbols. */
2729 if (h->forced_local && h->type == STT_GNU_IFUNC)
2730 return TRUE;
2731
351f65ca 2732 eh = (struct elf_x86_64_link_hash_entry *) h;
c434dee6
AJ
2733 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2734 {
2735 asection *s = p->sec->output_section;
2736
2737 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2738 {
2739 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2740
2741 info->flags |= DF_TEXTREL;
2742
b70321a2
L
2743 if (info->warn_shared_textrel && info->shared)
2744 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"),
2745 p->sec->owner, h->root.root.string,
2746 p->sec);
2747
c434dee6 2748 /* Not an error, just cut short the traversal. */
b34976b6 2749 return FALSE;
c434dee6
AJ
2750 }
2751 }
b34976b6 2752 return TRUE;
c434dee6
AJ
2753}
2754
daa67607
L
2755/* Convert
2756 mov foo@GOTPCREL(%rip), %reg
2757 to
2758 lea foo(%rip), %reg
2759 with the local symbol, foo. */
2760
2761static bfd_boolean
2762elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
2763 struct bfd_link_info *link_info)
2764{
2765 Elf_Internal_Shdr *symtab_hdr;
2766 Elf_Internal_Rela *internal_relocs;
2767 Elf_Internal_Rela *irel, *irelend;
2768 bfd_byte *contents;
2769 struct elf_x86_64_link_hash_table *htab;
2770 bfd_boolean changed_contents;
2771 bfd_boolean changed_relocs;
2772 bfd_signed_vma *local_got_refcounts;
2773
2774 /* Don't even try to convert non-ELF outputs. */
2775 if (!is_elf_hash_table (link_info->hash))
2776 return FALSE;
2777
fbdc86d9 2778 /* Nothing to do if there are no codes, no relocations or no output. */
daa67607 2779 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
fbdc86d9
L
2780 || sec->reloc_count == 0
2781 || discarded_section (sec))
daa67607
L
2782 return TRUE;
2783
2784 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2785
2786 /* Load the relocations for this section. */
2787 internal_relocs = (_bfd_elf_link_read_relocs
2788 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2789 link_info->keep_memory));
2790 if (internal_relocs == NULL)
2791 return FALSE;
2792
2793 htab = elf_x86_64_hash_table (link_info);
2794 changed_contents = FALSE;
2795 changed_relocs = FALSE;
2796 local_got_refcounts = elf_local_got_refcounts (abfd);
2797
2798 /* Get the section contents. */
2799 if (elf_section_data (sec)->this_hdr.contents != NULL)
2800 contents = elf_section_data (sec)->this_hdr.contents;
2801 else
2802 {
2803 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2804 goto error_return;
2805 }
2806
2807 irelend = internal_relocs + sec->reloc_count;
2808 for (irel = internal_relocs; irel < irelend; irel++)
2809 {
2810 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
2811 unsigned int r_symndx = htab->r_sym (irel->r_info);
2812 unsigned int indx;
2813 struct elf_link_hash_entry *h;
2814
2815 if (r_type != R_X86_64_GOTPCREL)
2816 continue;
2817
2818 /* Get the symbol referred to by the reloc. */
2819 if (r_symndx < symtab_hdr->sh_info)
2820 {
2821 Elf_Internal_Sym *isym;
2822
2823 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2824 abfd, r_symndx);
2825
2826 /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation. */
2827 if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
2828 && bfd_get_8 (input_bfd,
2829 contents + irel->r_offset - 2) == 0x8b)
2830 {
2831 bfd_put_8 (output_bfd, 0x8d,
2832 contents + irel->r_offset - 2);
2833 irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
2834 if (local_got_refcounts != NULL
2835 && local_got_refcounts[r_symndx] > 0)
2836 local_got_refcounts[r_symndx] -= 1;
2837 changed_contents = TRUE;
2838 changed_relocs = TRUE;
2839 }
2840 continue;
2841 }
2842
2843 indx = r_symndx - symtab_hdr->sh_info;
2844 h = elf_sym_hashes (abfd)[indx];
2845 BFD_ASSERT (h != NULL);
2846
2847 while (h->root.type == bfd_link_hash_indirect
2848 || h->root.type == bfd_link_hash_warning)
2849 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2850
3f65f599
L
2851 /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation. We also
2852 avoid optimizing _DYNAMIC since ld.so may use its link-time
2853 address. */
daa67607
L
2854 if (h->def_regular
2855 && h->type != STT_GNU_IFUNC
9637f6ef 2856 && h != htab->elf.hdynamic
daa67607
L
2857 && SYMBOL_REFERENCES_LOCAL (link_info, h)
2858 && bfd_get_8 (input_bfd,
2859 contents + irel->r_offset - 2) == 0x8b)
2860 {
2861 bfd_put_8 (output_bfd, 0x8d,
2862 contents + irel->r_offset - 2);
2863 irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
2864 if (h->got.refcount > 0)
2865 h->got.refcount -= 1;
2866 changed_contents = TRUE;
2867 changed_relocs = TRUE;
2868 }
2869 }
2870
2871 if (contents != NULL
2872 && elf_section_data (sec)->this_hdr.contents != contents)
2873 {
2874 if (!changed_contents && !link_info->keep_memory)
2875 free (contents);
2876 else
2877 {
2878 /* Cache the section contents for elf_link_input_bfd. */
2879 elf_section_data (sec)->this_hdr.contents = contents;
2880 }
2881 }
2882
2883 if (elf_section_data (sec)->relocs != internal_relocs)
2884 {
2885 if (!changed_relocs)
2886 free (internal_relocs);
2887 else
2888 elf_section_data (sec)->relocs = internal_relocs;
2889 }
2890
2891 return TRUE;
2892
2893 error_return:
2894 if (contents != NULL
2895 && elf_section_data (sec)->this_hdr.contents != contents)
2896 free (contents);
2897 if (internal_relocs != NULL
2898 && elf_section_data (sec)->relocs != internal_relocs)
2899 free (internal_relocs);
2900 return FALSE;
2901}
2902
70256ad8
AJ
2903/* Set the sizes of the dynamic sections. */
2904
b34976b6 2905static bfd_boolean
351f65ca
L
2906elf_x86_64_size_dynamic_sections (bfd *output_bfd,
2907 struct bfd_link_info *info)
70256ad8 2908{
351f65ca 2909 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
2910 bfd *dynobj;
2911 asection *s;
b34976b6 2912 bfd_boolean relocs;
c434dee6 2913 bfd *ibfd;
351f65ca 2914 const struct elf_backend_data *bed;
70256ad8 2915
351f65ca 2916 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2917 if (htab == NULL)
2918 return FALSE;
351f65ca 2919 bed = get_elf_backend_data (output_bfd);
4dfe6ac6 2920
c434dee6
AJ
2921 dynobj = htab->elf.dynobj;
2922 if (dynobj == NULL)
2923 abort ();
70256ad8 2924
c434dee6 2925 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
2926 {
2927 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 2928 if (info->executable)
70256ad8 2929 {
3d4d4302 2930 s = bfd_get_linker_section (dynobj, ".interp");
c434dee6
AJ
2931 if (s == NULL)
2932 abort ();
351f65ca
L
2933 s->size = htab->dynamic_interpreter_size;
2934 s->contents = (unsigned char *) htab->dynamic_interpreter;
70256ad8
AJ
2935 }
2936 }
70256ad8 2937
c434dee6
AJ
2938 /* Set up .got offsets for local syms, and space for local dynamic
2939 relocs. */
2940 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 2941 {
c434dee6
AJ
2942 bfd_signed_vma *local_got;
2943 bfd_signed_vma *end_local_got;
bffbf940 2944 char *local_tls_type;
67a4f2b7 2945 bfd_vma *local_tlsdesc_gotent;
c434dee6
AJ
2946 bfd_size_type locsymcount;
2947 Elf_Internal_Shdr *symtab_hdr;
2948 asection *srel;
70256ad8 2949
0ffa91dd 2950 if (! is_x86_64_elf (ibfd))
70256ad8
AJ
2951 continue;
2952
c434dee6 2953 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 2954 {
e03a8ed8 2955 struct elf_dyn_relocs *p;
c434dee6 2956
daa67607
L
2957 if (!elf_x86_64_convert_mov_to_lea (ibfd, s, info))
2958 return FALSE;
2959
e03a8ed8 2960 for (p = (struct elf_dyn_relocs *)
e81d3500 2961 (elf_section_data (s)->local_dynrel);
c434dee6
AJ
2962 p != NULL;
2963 p = p->next)
70256ad8 2964 {
c434dee6
AJ
2965 if (!bfd_is_abs_section (p->sec)
2966 && bfd_is_abs_section (p->sec->output_section))
2967 {
2968 /* Input section has been discarded, either because
2969 it is a copy of a linkonce section or due to
2970 linker script /DISCARD/, so we'll be discarding
2971 the relocs too. */
2972 }
2973 else if (p->count != 0)
2974 {
2975 srel = elf_section_data (p->sec)->sreloc;
351f65ca 2976 srel->size += p->count * bed->s->sizeof_rela;
4b819e1f
L
2977 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2978 && (info->flags & DF_TEXTREL) == 0)
b70321a2
L
2979 {
2980 info->flags |= DF_TEXTREL;
2981 if (info->warn_shared_textrel && info->shared)
2982 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"),
2983 p->sec->owner, p->sec);
b70321a2 2984 }
c434dee6 2985 }
70256ad8
AJ
2986 }
2987 }
c434dee6
AJ
2988
2989 local_got = elf_local_got_refcounts (ibfd);
2990 if (!local_got)
2991 continue;
2992
0ffa91dd 2993 symtab_hdr = &elf_symtab_hdr (ibfd);
c434dee6
AJ
2994 locsymcount = symtab_hdr->sh_info;
2995 end_local_got = local_got + locsymcount;
351f65ca
L
2996 local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
2997 local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
6de2ae4a
L
2998 s = htab->elf.sgot;
2999 srel = htab->elf.srelgot;
67a4f2b7
AO
3000 for (; local_got < end_local_got;
3001 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
70256ad8 3002 {
67a4f2b7 3003 *local_tlsdesc_gotent = (bfd_vma) -1;
c434dee6 3004 if (*local_got > 0)
70256ad8 3005 {
67a4f2b7
AO
3006 if (GOT_TLS_GDESC_P (*local_tls_type))
3007 {
6de2ae4a 3008 *local_tlsdesc_gotent = htab->elf.sgotplt->size
351f65ca 3009 - elf_x86_64_compute_jump_table_size (htab);
6de2ae4a 3010 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
67a4f2b7
AO
3011 *local_got = (bfd_vma) -2;
3012 }
3013 if (! GOT_TLS_GDESC_P (*local_tls_type)
3014 || GOT_TLS_GD_P (*local_tls_type))
3015 {
3016 *local_got = s->size;
3017 s->size += GOT_ENTRY_SIZE;
3018 if (GOT_TLS_GD_P (*local_tls_type))
3019 s->size += GOT_ENTRY_SIZE;
3020 }
bffbf940 3021 if (info->shared
67a4f2b7 3022 || GOT_TLS_GD_ANY_P (*local_tls_type)
bffbf940 3023 || *local_tls_type == GOT_TLS_IE)
67a4f2b7
AO
3024 {
3025 if (GOT_TLS_GDESC_P (*local_tls_type))
3026 {
6de2ae4a 3027 htab->elf.srelplt->size
351f65ca 3028 += bed->s->sizeof_rela;
67a4f2b7
AO
3029 htab->tlsdesc_plt = (bfd_vma) -1;
3030 }
3031 if (! GOT_TLS_GDESC_P (*local_tls_type)
3032 || GOT_TLS_GD_P (*local_tls_type))
351f65ca 3033 srel->size += bed->s->sizeof_rela;
67a4f2b7 3034 }
70256ad8
AJ
3035 }
3036 else
c434dee6
AJ
3037 *local_got = (bfd_vma) -1;
3038 }
3039 }
70256ad8 3040
bffbf940
JJ
3041 if (htab->tls_ld_got.refcount > 0)
3042 {
3043 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
3044 relocs. */
6de2ae4a
L
3045 htab->tls_ld_got.offset = htab->elf.sgot->size;
3046 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
351f65ca 3047 htab->elf.srelgot->size += bed->s->sizeof_rela;
bffbf940
JJ
3048 }
3049 else
3050 htab->tls_ld_got.offset = -1;
3051
c434dee6
AJ
3052 /* Allocate global sym .plt and .got entries, and space for global
3053 sym dynamic relocs. */
351f65ca 3054 elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
eb4ff4d6 3055 info);
c434dee6 3056
c25bc9fc
L
3057 /* Allocate .plt and .got entries, and space for local symbols. */
3058 htab_traverse (htab->loc_hash_table,
351f65ca 3059 elf_x86_64_allocate_local_dynrelocs,
c25bc9fc
L
3060 info);
3061
67a4f2b7
AO
3062 /* For every jump slot reserved in the sgotplt, reloc_count is
3063 incremented. However, when we reserve space for TLS descriptors,
3064 it's not incremented, so in order to compute the space reserved
3065 for them, it suffices to multiply the reloc count by the jump
e1f98742
L
3066 slot size.
3067
3068 PR ld/13302: We start next_irelative_index at the end of .rela.plt
3069 so that R_X86_64_IRELATIVE entries come last. */
6de2ae4a 3070 if (htab->elf.srelplt)
e1f98742
L
3071 {
3072 htab->sgotplt_jump_table_size
3073 = elf_x86_64_compute_jump_table_size (htab);
3074 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3075 }
3076 else if (htab->elf.irelplt)
3077 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
67a4f2b7
AO
3078
3079 if (htab->tlsdesc_plt)
3080 {
3081 /* If we're not using lazy TLS relocations, don't generate the
3082 PLT and GOT entries they require. */
3083 if ((info->flags & DF_BIND_NOW))
3084 htab->tlsdesc_plt = 0;
3085 else
3086 {
6de2ae4a
L
3087 htab->tlsdesc_got = htab->elf.sgot->size;
3088 htab->elf.sgot->size += GOT_ENTRY_SIZE;
67a4f2b7
AO
3089 /* Reserve room for the initial entry.
3090 FIXME: we could probably do away with it in this case. */
6de2ae4a 3091 if (htab->elf.splt->size == 0)
eed180f8 3092 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
6de2ae4a 3093 htab->tlsdesc_plt = htab->elf.splt->size;
eed180f8 3094 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
67a4f2b7
AO
3095 }
3096 }
3097
a7b16ceb
L
3098 if (htab->elf.sgotplt)
3099 {
3100 /* Don't allocate .got.plt section if there are no GOT nor PLT
eed180f8 3101 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
9d4b2dba
L
3102 if ((htab->elf.hgot == NULL
3103 || !htab->elf.hgot->ref_regular_nonweak)
e28df02b
L
3104 && (htab->elf.sgotplt->size
3105 == get_elf_backend_data (output_bfd)->got_header_size)
a7b16ceb
L
3106 && (htab->elf.splt == NULL
3107 || htab->elf.splt->size == 0)
3108 && (htab->elf.sgot == NULL
3109 || htab->elf.sgot->size == 0)
3110 && (htab->elf.iplt == NULL
3111 || htab->elf.iplt->size == 0)
3112 && (htab->elf.igotplt == NULL
3113 || htab->elf.igotplt->size == 0))
3114 htab->elf.sgotplt->size = 0;
3115 }
3116
9a2a56cc
AM
3117 if (htab->plt_eh_frame != NULL
3118 && htab->elf.splt != NULL
3119 && htab->elf.splt->size != 0
3120 && !bfd_is_abs_section (htab->elf.splt->output_section)
3121 && _bfd_elf_eh_frame_present (info))
3122 {
3123 const struct elf_x86_64_backend_data *arch_data
f8222080 3124 = get_elf_x86_64_arch_data (bed);
9a2a56cc
AM
3125 htab->plt_eh_frame->size = arch_data->eh_frame_plt_size;
3126 }
3127
c434dee6
AJ
3128 /* We now have determined the sizes of the various dynamic sections.
3129 Allocate memory for them. */
b34976b6 3130 relocs = FALSE;
c434dee6
AJ
3131 for (s = dynobj->sections; s != NULL; s = s->next)
3132 {
3133 if ((s->flags & SEC_LINKER_CREATED) == 0)
3134 continue;
3135
6de2ae4a
L
3136 if (s == htab->elf.splt
3137 || s == htab->elf.sgot
3138 || s == htab->elf.sgotplt
3139 || s == htab->elf.iplt
3140 || s == htab->elf.igotplt
0ff2b86e 3141 || s == htab->plt_bnd
9a2a56cc 3142 || s == htab->plt_eh_frame
75ff4589 3143 || s == htab->sdynbss)
c434dee6
AJ
3144 {
3145 /* Strip this section if we don't need it; see the
3146 comment below. */
3147 }
0112cd26 3148 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
c434dee6 3149 {
6de2ae4a 3150 if (s->size != 0 && s != htab->elf.srelplt)
b34976b6 3151 relocs = TRUE;
c434dee6
AJ
3152
3153 /* We use the reloc_count field as a counter if we need
3154 to copy relocs into the output file. */
6de2ae4a 3155 if (s != htab->elf.srelplt)
67a4f2b7 3156 s->reloc_count = 0;
70256ad8 3157 }
c434dee6 3158 else
70256ad8
AJ
3159 {
3160 /* It's not one of our sections, so don't allocate space. */
3161 continue;
3162 }
3163
eea6121a 3164 if (s->size == 0)
70256ad8 3165 {
c434dee6
AJ
3166 /* If we don't need this section, strip it from the
3167 output file. This is mostly to handle .rela.bss and
3168 .rela.plt. We must create both sections in
3169 create_dynamic_sections, because they must be created
3170 before the linker maps input sections to output
3171 sections. The linker does that before
3172 adjust_dynamic_symbol is called, and it is that
3173 function which decides whether anything needs to go
3174 into these sections. */
3175
8423293d 3176 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
3177 continue;
3178 }
3179
c456f082
AM
3180 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3181 continue;
3182
70256ad8
AJ
3183 /* Allocate memory for the section contents. We use bfd_zalloc
3184 here in case unused entries are not reclaimed before the
3185 section's contents are written out. This should not happen,
3186 but this way if it does, we get a R_X86_64_NONE reloc instead
3187 of garbage. */
eea6121a 3188 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 3189 if (s->contents == NULL)
b34976b6 3190 return FALSE;
70256ad8
AJ
3191 }
3192
e41b3a13 3193 if (htab->plt_eh_frame != NULL
9a2a56cc
AM
3194 && htab->plt_eh_frame->contents != NULL)
3195 {
3196 const struct elf_x86_64_backend_data *arch_data
f8222080 3197 = get_elf_x86_64_arch_data (bed);
9a2a56cc
AM
3198
3199 memcpy (htab->plt_eh_frame->contents,
3200 arch_data->eh_frame_plt, htab->plt_eh_frame->size);
3201 bfd_put_32 (dynobj, htab->elf.splt->size,
3202 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3203 }
e41b3a13 3204
c434dee6 3205 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
3206 {
3207 /* Add some entries to the .dynamic section. We fill in the
351f65ca 3208 values later, in elf_x86_64_finish_dynamic_sections, but we
70256ad8 3209 must add the entries now so that we get the correct size for
407443a3 3210 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 3211 dynamic linker and used by the debugger. */
dc810e39 3212#define add_dynamic_entry(TAG, VAL) \
5a580b3a 3213 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 3214
36af4a4e 3215 if (info->executable)
70256ad8 3216 {
dc810e39 3217 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 3218 return FALSE;
70256ad8
AJ
3219 }
3220
6de2ae4a 3221 if (htab->elf.splt->size != 0)
70256ad8 3222 {
dc810e39
AM
3223 if (!add_dynamic_entry (DT_PLTGOT, 0)
3224 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3225 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3226 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 3227 return FALSE;
67a4f2b7
AO
3228
3229 if (htab->tlsdesc_plt
3230 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
3231 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
3232 return FALSE;
70256ad8
AJ
3233 }
3234
3235 if (relocs)
3236 {
dc810e39
AM
3237 if (!add_dynamic_entry (DT_RELA, 0)
3238 || !add_dynamic_entry (DT_RELASZ, 0)
351f65ca 3239 || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
b34976b6 3240 return FALSE;
70256ad8 3241
c434dee6
AJ
3242 /* If any dynamic relocs apply to a read-only section,
3243 then we need a DT_TEXTREL entry. */
3244 if ((info->flags & DF_TEXTREL) == 0)
eed180f8 3245 elf_link_hash_traverse (&htab->elf,
351f65ca 3246 elf_x86_64_readonly_dynrelocs,
eb4ff4d6 3247 info);
c434dee6
AJ
3248
3249 if ((info->flags & DF_TEXTREL) != 0)
3250 {
3251 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3252 return FALSE;
c434dee6 3253 }
70256ad8
AJ
3254 }
3255 }
dc810e39 3256#undef add_dynamic_entry
70256ad8 3257
b34976b6 3258 return TRUE;
70256ad8
AJ
3259}
3260
67a4f2b7 3261static bfd_boolean
351f65ca
L
3262elf_x86_64_always_size_sections (bfd *output_bfd,
3263 struct bfd_link_info *info)
67a4f2b7
AO
3264{
3265 asection *tls_sec = elf_hash_table (info)->tls_sec;
3266
3267 if (tls_sec)
3268 {
3269 struct elf_link_hash_entry *tlsbase;
3270
3271 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3272 "_TLS_MODULE_BASE_",
3273 FALSE, FALSE, FALSE);
3274
3275 if (tlsbase && tlsbase->type == STT_TLS)
3276 {
351f65ca 3277 struct elf_x86_64_link_hash_table *htab;
67a4f2b7
AO
3278 struct bfd_link_hash_entry *bh = NULL;
3279 const struct elf_backend_data *bed
3280 = get_elf_backend_data (output_bfd);
3281
351f65ca 3282 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3283 if (htab == NULL)
3284 return FALSE;
3285
67a4f2b7
AO
3286 if (!(_bfd_generic_link_add_one_symbol
3287 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3288 tls_sec, 0, NULL, FALSE,
3289 bed->collect, &bh)))
3290 return FALSE;
9f03412a 3291
4dfe6ac6 3292 htab->tls_module_base = bh;
9f03412a 3293
67a4f2b7
AO
3294 tlsbase = (struct elf_link_hash_entry *)bh;
3295 tlsbase->def_regular = 1;
3296 tlsbase->other = STV_HIDDEN;
3297 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3298 }
3299 }
3300
3301 return TRUE;
3302}
3303
9f03412a
AO
3304/* _TLS_MODULE_BASE_ needs to be treated especially when linking
3305 executables. Rather than setting it to the beginning of the TLS
3306 section, we have to set it to the end. This function may be called
3307 multiple times, it is idempotent. */
3308
3309static void
351f65ca 3310elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
9f03412a 3311{
351f65ca 3312 struct elf_x86_64_link_hash_table *htab;
9f03412a
AO
3313 struct bfd_link_hash_entry *base;
3314
3315 if (!info->executable)
3316 return;
3317
351f65ca 3318 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3319 if (htab == NULL)
3320 return;
9f03412a 3321
4dfe6ac6
NC
3322 base = htab->tls_module_base;
3323 if (base == NULL)
9f03412a
AO
3324 return;
3325
4dfe6ac6 3326 base->u.def.value = htab->elf.tls_size;
9f03412a
AO
3327}
3328
bffbf940
JJ
3329/* Return the base VMA address which should be subtracted from real addresses
3330 when resolving @dtpoff relocation.
3331 This is PT_TLS segment p_vaddr. */
3332
3333static bfd_vma
351f65ca 3334elf_x86_64_dtpoff_base (struct bfd_link_info *info)
bffbf940 3335{
e1918d23
AM
3336 /* If tls_sec is NULL, we should have signalled an error already. */
3337 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 3338 return 0;
e1918d23 3339 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
3340}
3341
3342/* Return the relocation value for @tpoff relocation
3343 if STT_TLS virtual address is ADDRESS. */
3344
3345static bfd_vma
351f65ca 3346elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 3347{
e1918d23 3348 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
3349 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3350 bfd_vma static_tls_size;
bffbf940
JJ
3351
3352 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 3353 if (htab->tls_sec == NULL)
bffbf940 3354 return 0;
7dc98aea
RO
3355
3356 /* Consider special static TLS alignment requirements. */
3357 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3358 return address - static_tls_size - htab->tls_sec->vma;
bffbf940
JJ
3359}
3360
90f487df
L
3361/* Is the instruction before OFFSET in CONTENTS a 32bit relative
3362 branch? */
3363
3364static bfd_boolean
3365is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
3366{
3367 /* Opcode Instruction
3368 0xe8 call
3369 0xe9 jump
3370 0x0f 0x8x conditional jump */
3371 return ((offset > 0
3372 && (contents [offset - 1] == 0xe8
3373 || contents [offset - 1] == 0xe9))
3374 || (offset > 1
3375 && contents [offset - 2] == 0x0f
3376 && (contents [offset - 1] & 0xf0) == 0x80));
3377}
3378
8d88c4ca
NC
3379/* Relocate an x86_64 ELF section. */
3380
b34976b6 3381static bfd_boolean
351f65ca
L
3382elf_x86_64_relocate_section (bfd *output_bfd,
3383 struct bfd_link_info *info,
3384 bfd *input_bfd,
3385 asection *input_section,
3386 bfd_byte *contents,
3387 Elf_Internal_Rela *relocs,
3388 Elf_Internal_Sym *local_syms,
3389 asection **local_sections)
8d88c4ca 3390{
351f65ca 3391 struct elf_x86_64_link_hash_table *htab;
8d88c4ca
NC
3392 Elf_Internal_Shdr *symtab_hdr;
3393 struct elf_link_hash_entry **sym_hashes;
3394 bfd_vma *local_got_offsets;
67a4f2b7 3395 bfd_vma *local_tlsdesc_gotents;
c434dee6 3396 Elf_Internal_Rela *rel;
8d88c4ca 3397 Elf_Internal_Rela *relend;
eed180f8 3398 const unsigned int plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
8d88c4ca 3399
0ffa91dd
NC
3400 BFD_ASSERT (is_x86_64_elf (input_bfd));
3401
351f65ca 3402 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3403 if (htab == NULL)
3404 return FALSE;
0ffa91dd 3405 symtab_hdr = &elf_symtab_hdr (input_bfd);
8d88c4ca
NC
3406 sym_hashes = elf_sym_hashes (input_bfd);
3407 local_got_offsets = elf_local_got_offsets (input_bfd);
351f65ca 3408 local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
8d88c4ca 3409
351f65ca 3410 elf_x86_64_set_tls_module_base (info);
9f03412a 3411
c434dee6 3412 rel = relocs;
8d88c4ca 3413 relend = relocs + input_section->reloc_count;
c434dee6 3414 for (; rel < relend; rel++)
8d88c4ca 3415 {
bffbf940 3416 unsigned int r_type;
8d88c4ca
NC
3417 reloc_howto_type *howto;
3418 unsigned long r_symndx;
3419 struct elf_link_hash_entry *h;
0ff2b86e 3420 struct elf_x86_64_link_hash_entry *eh;
8d88c4ca
NC
3421 Elf_Internal_Sym *sym;
3422 asection *sec;
0ff2b86e 3423 bfd_vma off, offplt, plt_offset;
8d88c4ca 3424 bfd_vma relocation;
b34976b6 3425 bfd_boolean unresolved_reloc;
8d88c4ca 3426 bfd_reloc_status_type r;
bffbf940 3427 int tls_type;
0ff2b86e 3428 asection *base_got, *resolved_plt;
1788fc08 3429 bfd_vma st_size;
8d88c4ca 3430
351f65ca 3431 r_type = ELF32_R_TYPE (rel->r_info);
fe4770f4
AJ
3432 if (r_type == (int) R_X86_64_GNU_VTINHERIT
3433 || r_type == (int) R_X86_64_GNU_VTENTRY)
3434 continue;
8d88c4ca 3435
9911c0fc 3436 if (r_type >= (int) R_X86_64_standard)
8da6118f 3437 {
9911c0fc
L
3438 (*_bfd_error_handler)
3439 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3440 input_bfd, input_section, r_type);
8da6118f 3441 bfd_set_error (bfd_error_bad_value);
b34976b6 3442 return FALSE;
8da6118f 3443 }
8d88c4ca 3444
d7921315 3445 if (r_type != (int) R_X86_64_32
eed180f8 3446 || ABI_64_P (output_bfd))
d7921315
L
3447 howto = x86_64_elf_howto_table + r_type;
3448 else
3449 howto = (x86_64_elf_howto_table
3450 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
351f65ca 3451 r_symndx = htab->r_sym (rel->r_info);
8d88c4ca
NC
3452 h = NULL;
3453 sym = NULL;
3454 sec = NULL;
b34976b6 3455 unresolved_reloc = FALSE;
8d88c4ca 3456 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
3457 {
3458 sym = local_syms + r_symndx;
3459 sec = local_sections[r_symndx];
c434dee6 3460
c25bc9fc
L
3461 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
3462 &sec, rel);
1788fc08 3463 st_size = sym->st_size;
c25bc9fc
L
3464
3465 /* Relocate against local STT_GNU_IFUNC symbol. */
1f85278f 3466 if (!info->relocatable
351f65ca 3467 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc 3468 {
351f65ca
L
3469 h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
3470 rel, FALSE);
c25bc9fc
L
3471 if (h == NULL)
3472 abort ();
3473
eed180f8 3474 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
3475 h->root.u.def.value = sym->st_value;
3476 h->root.u.def.section = sec;
3477 }
8da6118f 3478 }
8d88c4ca 3479 else
8da6118f 3480 {
c9736ba0 3481 bfd_boolean warned ATTRIBUTE_UNUSED;
62d887d4 3482 bfd_boolean ignored ATTRIBUTE_UNUSED;
c434dee6 3483
b2a8e766
AM
3484 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3485 r_symndx, symtab_hdr, sym_hashes,
3486 h, sec, relocation,
62d887d4 3487 unresolved_reloc, warned, ignored);
1788fc08 3488 st_size = h->size;
8da6118f 3489 }
ab96bf03 3490
dbaa2011 3491 if (sec != NULL && discarded_section (sec))
0672748a 3492 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 3493 rel, 1, relend, howto, 0, contents);
ab96bf03
AM
3494
3495 if (info->relocatable)
3496 continue;
3497
1788fc08 3498 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
64d25c44 3499 {
1788fc08
L
3500 if (r_type == R_X86_64_64)
3501 {
3502 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
3503 zero-extend it to 64bit if addend is zero. */
3504 r_type = R_X86_64_32;
3505 memset (contents + rel->r_offset + 4, 0, 4);
3506 }
3507 else if (r_type == R_X86_64_SIZE64)
3508 {
3509 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
3510 zero-extend it to 64bit if addend is zero. */
3511 r_type = R_X86_64_SIZE32;
3512 memset (contents + rel->r_offset + 4, 0, 4);
3513 }
64d25c44
L
3514 }
3515
0ff2b86e
L
3516 eh = (struct elf_x86_64_link_hash_entry *) h;
3517
cbe950e9
L
3518 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3519 it here if it is defined in a non-shared object. */
3520 if (h != NULL
3521 && h->type == STT_GNU_IFUNC
3522 && h->def_regular)
3523 {
cbe950e9 3524 bfd_vma plt_index;
4c544807 3525 const char *name;
cbe950e9
L
3526
3527 if ((input_section->flags & SEC_ALLOC) == 0
3528 || h->plt.offset == (bfd_vma) -1)
3529 abort ();
3530
3531 /* STT_GNU_IFUNC symbol must go through PLT. */
0ff2b86e
L
3532 if (htab->elf.splt != NULL)
3533 {
3534 if (htab->plt_bnd != NULL)
3535 {
3536 resolved_plt = htab->plt_bnd;
3537 plt_offset = eh->plt_bnd.offset;
3538 }
3539 else
3540 {
3541 resolved_plt = htab->elf.splt;
3542 plt_offset = h->plt.offset;
3543 }
3544 }
3545 else
3546 {
3547 resolved_plt = htab->elf.iplt;
3548 plt_offset = h->plt.offset;
3549 }
3550
3551 relocation = (resolved_plt->output_section->vma
3552 + resolved_plt->output_offset + plt_offset);
cbe950e9
L
3553
3554 switch (r_type)
3555 {
3556 default:
4c544807
L
3557 if (h->root.root.string)
3558 name = h->root.root.string;
3559 else
3560 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3561 NULL);
cbe950e9
L
3562 (*_bfd_error_handler)
3563 (_("%B: relocation %s against STT_GNU_IFUNC "
3564 "symbol `%s' isn't handled by %s"), input_bfd,
3565 x86_64_elf_howto_table[r_type].name,
4c544807 3566 name, __FUNCTION__);
cbe950e9
L
3567 bfd_set_error (bfd_error_bad_value);
3568 return FALSE;
3569
3570 case R_X86_64_32S:
710ab287 3571 if (info->shared)
cbe950e9 3572 abort ();
710ab287
L
3573 goto do_relocation;
3574
248775ba
L
3575 case R_X86_64_32:
3576 if (ABI_64_P (output_bfd))
3577 goto do_relocation;
17672001 3578 /* FALLTHROUGH */
eed180f8 3579 case R_X86_64_64:
710ab287
L
3580 if (rel->r_addend != 0)
3581 {
4c544807
L
3582 if (h->root.root.string)
3583 name = h->root.root.string;
3584 else
3585 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3586 sym, NULL);
710ab287
L
3587 (*_bfd_error_handler)
3588 (_("%B: relocation %s against STT_GNU_IFUNC "
3589 "symbol `%s' has non-zero addend: %d"),
3590 input_bfd, x86_64_elf_howto_table[r_type].name,
4c544807 3591 name, rel->r_addend);
710ab287
L
3592 bfd_set_error (bfd_error_bad_value);
3593 return FALSE;
3594 }
3595
3596 /* Generate dynamic relcoation only when there is a
c293fa49 3597 non-GOT reference in a shared object. */
710ab287
L
3598 if (info->shared && h->non_got_ref)
3599 {
3600 Elf_Internal_Rela outrel;
710ab287
L
3601 asection *sreloc;
3602
c25bc9fc
L
3603 /* Need a dynamic relocation to get the real function
3604 address. */
710ab287
L
3605 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3606 info,
3607 input_section,
3608 rel->r_offset);
3609 if (outrel.r_offset == (bfd_vma) -1
3610 || outrel.r_offset == (bfd_vma) -2)
3611 abort ();
3612
3613 outrel.r_offset += (input_section->output_section->vma
3614 + input_section->output_offset);
3615
3616 if (h->dynindx == -1
44c4ea11
L
3617 || h->forced_local
3618 || info->executable)
710ab287
L
3619 {
3620 /* This symbol is resolved locally. */
56b8aada
L
3621 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3622 outrel.r_addend = (h->root.u.def.value
3623 + h->root.u.def.section->output_section->vma
3624 + h->root.u.def.section->output_offset);
710ab287
L
3625 }
3626 else
3627 {
351f65ca 3628 outrel.r_info = htab->r_info (h->dynindx, r_type);
710ab287
L
3629 outrel.r_addend = 0;
3630 }
3631
6de2ae4a 3632 sreloc = htab->elf.irelifunc;
351f65ca 3633 elf_append_rela (output_bfd, sreloc, &outrel);
710ab287
L
3634
3635 /* If this reloc is against an external symbol, we
3636 do not want to fiddle with the addend. Otherwise,
3637 we need to include the symbol value so that it
3638 becomes an addend for the dynamic reloc. For an
3639 internal symbol, we have updated addend. */
56b8aada 3640 continue;
710ab287 3641 }
17672001 3642 /* FALLTHROUGH */
cbe950e9 3643 case R_X86_64_PC32:
c3320543 3644 case R_X86_64_PC32_BND:
cbe950e9
L
3645 case R_X86_64_PC64:
3646 case R_X86_64_PLT32:
c3320543 3647 case R_X86_64_PLT32_BND:
cbe950e9
L
3648 goto do_relocation;
3649
3650 case R_X86_64_GOTPCREL:
3651 case R_X86_64_GOTPCREL64:
6de2ae4a 3652 base_got = htab->elf.sgot;
cbe950e9
L
3653 off = h->got.offset;
3654
7afd84dc 3655 if (base_got == NULL)
cbe950e9
L
3656 abort ();
3657
7afd84dc 3658 if (off == (bfd_vma) -1)
cbe950e9 3659 {
7afd84dc
L
3660 /* We can't use h->got.offset here to save state, or
3661 even just remember the offset, as finish_dynamic_symbol
3662 would use that as offset into .got. */
cbe950e9 3663
6de2ae4a 3664 if (htab->elf.splt != NULL)
7afd84dc 3665 {
eed180f8 3666 plt_index = h->plt.offset / plt_entry_size - 1;
7afd84dc 3667 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 3668 base_got = htab->elf.sgotplt;
7afd84dc 3669 }
cbe950e9
L
3670 else
3671 {
eed180f8 3672 plt_index = h->plt.offset / plt_entry_size;
7afd84dc 3673 off = plt_index * GOT_ENTRY_SIZE;
6de2ae4a 3674 base_got = htab->elf.igotplt;
7afd84dc
L
3675 }
3676
3677 if (h->dynindx == -1
3678 || h->forced_local
3679 || info->symbolic)
3680 {
eed180f8 3681 /* This references the local defitionion. We must
7afd84dc 3682 initialize this entry in the global offset table.
eed180f8 3683 Since the offset must always be a multiple of 8,
7afd84dc
L
3684 we use the least significant bit to record
3685 whether we have initialized it already.
3686
3687 When doing a dynamic link, we create a .rela.got
3688 relocation entry to initialize the value. This
3689 is done in the finish_dynamic_symbol routine. */
3690 if ((off & 1) != 0)
3691 off &= ~1;
3692 else
3693 {
3694 bfd_put_64 (output_bfd, relocation,
3695 base_got->contents + off);
3696 /* Note that this is harmless for the GOTPLT64
3697 case, as -1 | 1 still is -1. */
3698 h->got.offset |= 1;
3699 }
cbe950e9
L
3700 }
3701 }
3702
3703 relocation = (base_got->output_section->vma
3704 + base_got->output_offset + off);
3705
cbe950e9
L
3706 goto do_relocation;
3707 }
3708 }
3709
70256ad8
AJ
3710 /* When generating a shared object, the relocations handled here are
3711 copied into the output file to be resolved at run time. */
3712 switch (r_type)
3713 {
3714 case R_X86_64_GOT32:
7b81dfbb 3715 case R_X86_64_GOT64:
70256ad8
AJ
3716 /* Relocation is to the entry for this symbol in the global
3717 offset table. */
70256ad8 3718 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
3719 case R_X86_64_GOTPCREL64:
3720 /* Use global offset table entry as symbol value. */
3721 case R_X86_64_GOTPLT64:
3722 /* This is the same as GOT64 for relocation purposes, but
3723 indicates the existence of a PLT entry. The difficulty is,
3724 that we must calculate the GOT slot offset from the PLT
3725 offset, if this symbol got a PLT entry (it was global).
3726 Additionally if it's computed from the PLT entry, then that
3727 GOT offset is relative to .got.plt, not to .got. */
6de2ae4a 3728 base_got = htab->elf.sgot;
7b81dfbb 3729
6de2ae4a 3730 if (htab->elf.sgot == NULL)
c434dee6 3731 abort ();
053579d7 3732
51e0a107 3733 if (h != NULL)
70256ad8 3734 {
b34976b6 3735 bfd_boolean dyn;
c434dee6
AJ
3736
3737 off = h->got.offset;
7b81dfbb 3738 if (h->needs_plt
eed180f8 3739 && h->plt.offset != (bfd_vma)-1
7b81dfbb
AJ
3740 && off == (bfd_vma)-1)
3741 {
3742 /* We can't use h->got.offset here to save
3743 state, or even just remember the offset, as
3744 finish_dynamic_symbol would use that as offset into
3745 .got. */
eed180f8 3746 bfd_vma plt_index = h->plt.offset / plt_entry_size - 1;
7b81dfbb 3747 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 3748 base_got = htab->elf.sgotplt;
7b81dfbb
AJ
3749 }
3750
c434dee6 3751 dyn = htab->elf.dynamic_sections_created;
51e0a107 3752
27482721 3753 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
51e0a107 3754 || (info->shared
27482721 3755 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
3756 || (ELF_ST_VISIBILITY (h->other)
3757 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
3758 {
3759 /* This is actually a static link, or it is a -Bsymbolic
3760 link and the symbol is defined locally, or the symbol
407443a3 3761 was forced to be local because of a version file. We
51e0a107
JH
3762 must initialize this entry in the global offset table.
3763 Since the offset must always be a multiple of 8, we
3764 use the least significant bit to record whether we
3765 have initialized it already.
3766
3767 When doing a dynamic link, we create a .rela.got
407443a3
AJ
3768 relocation entry to initialize the value. This is
3769 done in the finish_dynamic_symbol routine. */
51e0a107
JH
3770 if ((off & 1) != 0)
3771 off &= ~1;
3772 else
3773 {
3774 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
3775 base_got->contents + off);
3776 /* Note that this is harmless for the GOTPLT64 case,
eed180f8 3777 as -1 | 1 still is -1. */
51e0a107
JH
3778 h->got.offset |= 1;
3779 }
3780 }
053579d7 3781 else
b34976b6 3782 unresolved_reloc = FALSE;
70256ad8 3783 }
51e0a107
JH
3784 else
3785 {
c434dee6
AJ
3786 if (local_got_offsets == NULL)
3787 abort ();
51e0a107
JH
3788
3789 off = local_got_offsets[r_symndx];
3790
3791 /* The offset must always be a multiple of 8. We use
407443a3
AJ
3792 the least significant bit to record whether we have
3793 already generated the necessary reloc. */
51e0a107
JH
3794 if ((off & 1) != 0)
3795 off &= ~1;
3796 else
3797 {
c434dee6 3798 bfd_put_64 (output_bfd, relocation,
7b81dfbb 3799 base_got->contents + off);
51e0a107
JH
3800
3801 if (info->shared)
3802 {
947216bf 3803 asection *s;
51e0a107 3804 Elf_Internal_Rela outrel;
70256ad8 3805
51e0a107
JH
3806 /* We need to generate a R_X86_64_RELATIVE reloc
3807 for the dynamic linker. */
6de2ae4a 3808 s = htab->elf.srelgot;
947216bf 3809 if (s == NULL)
c434dee6 3810 abort ();
51e0a107 3811
7b81dfbb
AJ
3812 outrel.r_offset = (base_got->output_section->vma
3813 + base_got->output_offset
51e0a107 3814 + off);
351f65ca 3815 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
51e0a107 3816 outrel.r_addend = relocation;
351f65ca 3817 elf_append_rela (output_bfd, s, &outrel);
51e0a107
JH
3818 }
3819
3820 local_got_offsets[r_symndx] |= 1;
3821 }
51e0a107 3822 }
6a2bda3f 3823
c434dee6
AJ
3824 if (off >= (bfd_vma) -2)
3825 abort ();
3826
7b81dfbb
AJ
3827 relocation = base_got->output_section->vma
3828 + base_got->output_offset + off;
3829 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
6de2ae4a
L
3830 relocation -= htab->elf.sgotplt->output_section->vma
3831 - htab->elf.sgotplt->output_offset;
c434dee6 3832
70256ad8
AJ
3833 break;
3834
d6ab8113
JB
3835 case R_X86_64_GOTOFF64:
3836 /* Relocation is relative to the start of the global offset
3837 table. */
3838
3839 /* Check to make sure it isn't a protected function symbol
3840 for shared library since it may not be local when used
3841 as function address. */
bdb892b9 3842 if (!info->executable
d6ab8113 3843 && h
bdb892b9 3844 && !SYMBOLIC_BIND (info, h)
d6ab8113
JB
3845 && h->def_regular
3846 && h->type == STT_FUNC
3847 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3848 {
3849 (*_bfd_error_handler)
3850 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3851 input_bfd, h->root.root.string);
3852 bfd_set_error (bfd_error_bad_value);
3853 return FALSE;
3854 }
3855
3856 /* Note that sgot is not involved in this
3857 calculation. We always want the start of .got.plt. If we
3858 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3859 permitted by the ABI, we might have to change this
3860 calculation. */
6de2ae4a
L
3861 relocation -= htab->elf.sgotplt->output_section->vma
3862 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3863 break;
3864
3865 case R_X86_64_GOTPC32:
7b81dfbb 3866 case R_X86_64_GOTPC64:
d6ab8113 3867 /* Use global offset table as symbol value. */
6de2ae4a
L
3868 relocation = htab->elf.sgotplt->output_section->vma
3869 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3870 unresolved_reloc = FALSE;
3871 break;
7b81dfbb
AJ
3872
3873 case R_X86_64_PLTOFF64:
3874 /* Relocation is PLT entry relative to GOT. For local
3875 symbols it's the symbol itself relative to GOT. */
eed180f8 3876 if (h != NULL
7b81dfbb
AJ
3877 /* See PLT32 handling. */
3878 && h->plt.offset != (bfd_vma) -1
6de2ae4a 3879 && htab->elf.splt != NULL)
7b81dfbb 3880 {
0ff2b86e
L
3881 if (htab->plt_bnd != NULL)
3882 {
3883 resolved_plt = htab->plt_bnd;
3884 plt_offset = eh->plt_bnd.offset;
3885 }
3886 else
3887 {
3888 resolved_plt = htab->elf.splt;
3889 plt_offset = h->plt.offset;
3890 }
3891
3892 relocation = (resolved_plt->output_section->vma
3893 + resolved_plt->output_offset
3894 + plt_offset);
7b81dfbb
AJ
3895 unresolved_reloc = FALSE;
3896 }
3897
6de2ae4a
L
3898 relocation -= htab->elf.sgotplt->output_section->vma
3899 + htab->elf.sgotplt->output_offset;
7b81dfbb 3900 break;
d6ab8113 3901
70256ad8 3902 case R_X86_64_PLT32:
c3320543 3903 case R_X86_64_PLT32_BND:
70256ad8
AJ
3904 /* Relocation is to the entry for this symbol in the
3905 procedure linkage table. */
3906
3907 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 3908 without using the procedure linkage table. */
70256ad8
AJ
3909 if (h == NULL)
3910 break;
3911
c434dee6 3912 if (h->plt.offset == (bfd_vma) -1
6de2ae4a 3913 || htab->elf.splt == NULL)
70256ad8
AJ
3914 {
3915 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
3916 happens when statically linking PIC code, or when
3917 using -Bsymbolic. */
70256ad8
AJ
3918 break;
3919 }
3920
0ff2b86e
L
3921 if (htab->plt_bnd != NULL)
3922 {
3923 resolved_plt = htab->plt_bnd;
3924 plt_offset = eh->plt_bnd.offset;
3925 }
3926 else
3927 {
3928 resolved_plt = htab->elf.splt;
3929 plt_offset = h->plt.offset;
3930 }
3931
3932 relocation = (resolved_plt->output_section->vma
3933 + resolved_plt->output_offset
3934 + plt_offset);
b34976b6 3935 unresolved_reloc = FALSE;
70256ad8
AJ
3936 break;
3937
1788fc08
L
3938 case R_X86_64_SIZE32:
3939 case R_X86_64_SIZE64:
1788fc08
L
3940 /* Set to symbol size. */
3941 relocation = st_size;
3942 goto direct;
3943
fd8ab9e5
AJ
3944 case R_X86_64_PC8:
3945 case R_X86_64_PC16:
3946 case R_X86_64_PC32:
c3320543 3947 case R_X86_64_PC32_BND:
6610a52d 3948 if (info->shared
ba3bee0b 3949 && (input_section->flags & SEC_ALLOC) != 0
90f487df 3950 && (input_section->flags & SEC_READONLY) != 0
41bed6dd 3951 && h != NULL)
6610a52d 3952 {
41bed6dd
L
3953 bfd_boolean fail = FALSE;
3954 bfd_boolean branch
c3320543
L
3955 = ((r_type == R_X86_64_PC32
3956 || r_type == R_X86_64_PC32_BND)
41bed6dd
L
3957 && is_32bit_relative_branch (contents, rel->r_offset));
3958
3959 if (SYMBOL_REFERENCES_LOCAL (info, h))
3960 {
3961 /* Symbol is referenced locally. Make sure it is
3962 defined locally or for a branch. */
3963 fail = !h->def_regular && !branch;
3964 }
90f487df 3965 else
41bed6dd
L
3966 {
3967 /* Symbol isn't referenced locally. We only allow
3968 branch to symbol with non-default visibility. */
3969 fail = (!branch
3970 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3971 }
3972
3973 if (fail)
3974 {
3975 const char *fmt;
3976 const char *v;
3977 const char *pic = "";
3978
3979 switch (ELF_ST_VISIBILITY (h->other))
3980 {
3981 case STV_HIDDEN:
3982 v = _("hidden symbol");
3983 break;
3984 case STV_INTERNAL:
3985 v = _("internal symbol");
3986 break;
3987 case STV_PROTECTED:
3988 v = _("protected symbol");
3989 break;
3990 default:
3991 v = _("symbol");
3992 pic = _("; recompile with -fPIC");
3993 break;
3994 }
3995
3996 if (h->def_regular)
3997 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3998 else
3999 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
4000
4001 (*_bfd_error_handler) (fmt, input_bfd,
4002 x86_64_elf_howto_table[r_type].name,
4003 v, h->root.root.string, pic);
4004 bfd_set_error (bfd_error_bad_value);
4005 return FALSE;
4006 }
6610a52d
L
4007 }
4008 /* Fall through. */
4009
70256ad8
AJ
4010 case R_X86_64_8:
4011 case R_X86_64_16:
4012 case R_X86_64_32:
d6ab8113 4013 case R_X86_64_PC64:
6b3db546 4014 case R_X86_64_64:
80643fbc 4015 /* FIXME: The ABI says the linker should make sure the value is
407443a3 4016 the same when it's zeroextended to 64 bit. */
c434dee6 4017
1788fc08 4018direct:
b1e24c02 4019 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
4020 break;
4021
4022 if ((info->shared
4bc6e03a
AJ
4023 && (h == NULL
4024 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4025 || h->root.type != bfd_link_hash_undefweak)
1788fc08
L
4026 && ((! IS_X86_64_PCREL_TYPE (r_type)
4027 && r_type != R_X86_64_SIZE32
4028 && r_type != R_X86_64_SIZE64)
d8045f23 4029 || ! SYMBOL_CALLS_LOCAL (info, h)))
d40d037c
AJ
4030 || (ELIMINATE_COPY_RELOCS
4031 && !info->shared
c434dee6
AJ
4032 && h != NULL
4033 && h->dynindx != -1
f5385ebf
AM
4034 && !h->non_got_ref
4035 && ((h->def_dynamic
4036 && !h->def_regular)
c434dee6 4037 || h->root.type == bfd_link_hash_undefweak
0f88be7a 4038 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
4039 {
4040 Elf_Internal_Rela outrel;
b34976b6 4041 bfd_boolean skip, relocate;
c434dee6 4042 asection *sreloc;
70256ad8
AJ
4043
4044 /* When generating a shared object, these relocations
4045 are copied into the output file to be resolved at run
407443a3 4046 time. */
b34976b6
AM
4047 skip = FALSE;
4048 relocate = FALSE;
70256ad8 4049
c629eae0
JJ
4050 outrel.r_offset =
4051 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 4052 rel->r_offset);
c629eae0 4053 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 4054 skip = TRUE;
0fb19cbc 4055 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 4056 skip = TRUE, relocate = TRUE;
70256ad8
AJ
4057
4058 outrel.r_offset += (input_section->output_section->vma
4059 + input_section->output_offset);
4060
4061 if (skip)
0bb2d96a 4062 memset (&outrel, 0, sizeof outrel);
c434dee6 4063
fd8ab9e5
AJ
4064 /* h->dynindx may be -1 if this symbol was marked to
4065 become local. */
4066 else if (h != NULL
c434dee6 4067 && h->dynindx != -1
d8045f23
NC
4068 && (IS_X86_64_PCREL_TYPE (r_type)
4069 || ! info->shared
4070 || ! SYMBOLIC_BIND (info, h)
4071 || ! h->def_regular))
70256ad8 4072 {
351f65ca 4073 outrel.r_info = htab->r_info (h->dynindx, r_type);
c434dee6 4074 outrel.r_addend = rel->r_addend;
70256ad8
AJ
4075 }
4076 else
4077 {
c434dee6 4078 /* This symbol is local, or marked to become local. */
248775ba 4079 if (r_type == htab->pointer_r_type)
607c0e09 4080 {
b34976b6 4081 relocate = TRUE;
351f65ca 4082 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
607c0e09
AS
4083 outrel.r_addend = relocation + rel->r_addend;
4084 }
64d25c44
L
4085 else if (r_type == R_X86_64_64
4086 && !ABI_64_P (output_bfd))
4087 {
4088 relocate = TRUE;
4089 outrel.r_info = htab->r_info (0,
4090 R_X86_64_RELATIVE64);
4091 outrel.r_addend = relocation + rel->r_addend;
8cf0d2dd
L
4092 /* Check addend overflow. */
4093 if ((outrel.r_addend & 0x80000000)
4094 != (rel->r_addend & 0x80000000))
4095 {
4096 const char *name;
268a8d3a 4097 int addend = rel->r_addend;
8cf0d2dd
L
4098 if (h && h->root.root.string)
4099 name = h->root.root.string;
4100 else
4101 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4102 sym, NULL);
6f2c9068
L
4103 if (addend < 0)
4104 (*_bfd_error_handler)
268a8d3a 4105 (_("%B: addend -0x%x in relocation %s against "
6f2c9068
L
4106 "symbol `%s' at 0x%lx in section `%A' is "
4107 "out of range"),
4108 input_bfd, input_section, addend,
4109 x86_64_elf_howto_table[r_type].name,
4110 name, (unsigned long) rel->r_offset);
4111 else
4112 (*_bfd_error_handler)
268a8d3a 4113 (_("%B: addend 0x%x in relocation %s against "
6f2c9068
L
4114 "symbol `%s' at 0x%lx in section `%A' is "
4115 "out of range"),
4116 input_bfd, input_section, addend,
4117 x86_64_elf_howto_table[r_type].name,
4118 name, (unsigned long) rel->r_offset);
8cf0d2dd
L
4119 bfd_set_error (bfd_error_bad_value);
4120 return FALSE;
4121 }
64d25c44 4122 }
607c0e09
AS
4123 else
4124 {
4125 long sindx;
4126
8517fae7 4127 if (bfd_is_abs_section (sec))
607c0e09
AS
4128 sindx = 0;
4129 else if (sec == NULL || sec->owner == NULL)
4130 {
4131 bfd_set_error (bfd_error_bad_value);
b34976b6 4132 return FALSE;
607c0e09
AS
4133 }
4134 else
4135 {
4136 asection *osec;
4137
74541ad4
AM
4138 /* We are turning this relocation into one
4139 against a section symbol. It would be
4140 proper to subtract the symbol's value,
4141 osec->vma, from the emitted reloc addend,
4142 but ld.so expects buggy relocs. */
607c0e09
AS
4143 osec = sec->output_section;
4144 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
4145 if (sindx == 0)
4146 {
4147 asection *oi = htab->elf.text_index_section;
4148 sindx = elf_section_data (oi)->dynindx;
4149 }
4150 BFD_ASSERT (sindx != 0);
607c0e09
AS
4151 }
4152
351f65ca 4153 outrel.r_info = htab->r_info (sindx, r_type);
607c0e09
AS
4154 outrel.r_addend = relocation + rel->r_addend;
4155 }
70256ad8
AJ
4156 }
4157
cbe950e9 4158 sreloc = elf_section_data (input_section)->sreloc;
d8045f23 4159
62d78908
L
4160 if (sreloc == NULL || sreloc->contents == NULL)
4161 {
4162 r = bfd_reloc_notsupported;
4163 goto check_relocation_error;
4164 }
c434dee6 4165
351f65ca 4166 elf_append_rela (output_bfd, sreloc, &outrel);
70256ad8
AJ
4167
4168 /* If this reloc is against an external symbol, we do
4169 not want to fiddle with the addend. Otherwise, we
4170 need to include the symbol value so that it becomes
4171 an addend for the dynamic reloc. */
0f88be7a 4172 if (! relocate)
70256ad8
AJ
4173 continue;
4174 }
4175
4176 break;
4177
bffbf940 4178 case R_X86_64_TLSGD:
67a4f2b7
AO
4179 case R_X86_64_GOTPC32_TLSDESC:
4180 case R_X86_64_TLSDESC_CALL:
bffbf940 4181 case R_X86_64_GOTTPOFF:
bffbf940
JJ
4182 tls_type = GOT_UNKNOWN;
4183 if (h == NULL && local_got_offsets)
351f65ca 4184 tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
bffbf940 4185 else if (h != NULL)
351f65ca 4186 tls_type = elf_x86_64_hash_entry (h)->tls_type;
142411ca 4187
351f65ca
L
4188 if (! elf_x86_64_tls_transition (info, input_bfd,
4189 input_section, contents,
4190 symtab_hdr, sym_hashes,
4191 &r_type, tls_type, rel,
4192 relend, h, r_symndx))
534a31f6 4193 return FALSE;
bffbf940
JJ
4194
4195 if (r_type == R_X86_64_TPOFF32)
4196 {
142411ca
L
4197 bfd_vma roff = rel->r_offset;
4198
bffbf940 4199 BFD_ASSERT (! unresolved_reloc);
142411ca 4200
351f65ca 4201 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
bffbf940 4202 {
52bc799a 4203 /* GD->LE transition. For 64bit, change
abcf1d52 4204 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
a3fadc9a 4205 .word 0x6666; rex64; call __tls_get_addr
52bc799a 4206 into:
bffbf940 4207 movq %fs:0, %rax
52bc799a
L
4208 leaq foo@tpoff(%rax), %rax
4209 For 32bit, change
4210 leaq foo@tlsgd(%rip), %rdi
4211 .word 0x6666; rex64; call __tls_get_addr
4212 into:
4213 movl %fs:0, %eax
5c98a14e
JJ
4214 leaq foo@tpoff(%rax), %rax
4215 For largepic, change:
4216 leaq foo@tlsgd(%rip), %rdi
4217 movabsq $__tls_get_addr@pltoff, %rax
4218 addq %rbx, %rax
4219 call *%rax
4220 into:
4221 movq %fs:0, %rax
4222 leaq foo@tpoff(%rax), %rax
4223 nopw 0x0(%rax,%rax,1) */
4224 int largepic = 0;
4225 if (ABI_64_P (output_bfd)
4226 && contents[roff + 5] == (bfd_byte) '\xb8')
4227 {
4228 memcpy (contents + roff - 3,
4229 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
4230 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4231 largepic = 1;
4232 }
4233 else if (ABI_64_P (output_bfd))
52bc799a
L
4234 memcpy (contents + roff - 4,
4235 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4236 16);
4237 else
4238 memcpy (contents + roff - 3,
4239 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4240 15);
eb4ff4d6 4241 bfd_put_32 (output_bfd,
351f65ca 4242 elf_x86_64_tpoff (info, relocation),
5c98a14e
JJ
4243 contents + roff + 8 + largepic);
4244 /* Skip R_X86_64_PC32/R_X86_64_PLT32/R_X86_64_PLTOFF64. */
bffbf940
JJ
4245 rel++;
4246 continue;
4247 }
351f65ca 4248 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
67a4f2b7
AO
4249 {
4250 /* GDesc -> LE transition.
4251 It's originally something like:
4252 leaq x@tlsdesc(%rip), %rax
4253
4254 Change it to:
c9736ba0 4255 movl $x@tpoff, %rax. */
67a4f2b7 4256
c9736ba0 4257 unsigned int val, type;
67a4f2b7 4258
67a4f2b7 4259 type = bfd_get_8 (input_bfd, contents + roff - 3);
67a4f2b7 4260 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
4261 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
4262 contents + roff - 3);
4263 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
4264 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4265 contents + roff - 1);
eb4ff4d6 4266 bfd_put_32 (output_bfd,
351f65ca 4267 elf_x86_64_tpoff (info, relocation),
67a4f2b7
AO
4268 contents + roff);
4269 continue;
4270 }
351f65ca 4271 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
67a4f2b7
AO
4272 {
4273 /* GDesc -> LE transition.
4274 It's originally:
4275 call *(%rax)
4276 Turn it into:
142411ca 4277 xchg %ax,%ax. */
10efb593 4278 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
4279 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4280 continue;
4281 }
351f65ca 4282 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
bffbf940 4283 {
bffbf940
JJ
4284 /* IE->LE transition:
4285 Originally it can be one of:
4286 movq foo@gottpoff(%rip), %reg
4287 addq foo@gottpoff(%rip), %reg
4288 We change it into:
4289 movq $foo, %reg
4290 leaq foo(%reg), %reg
4291 addq $foo, %reg. */
142411ca
L
4292
4293 unsigned int val, type, reg;
4294
4295 val = bfd_get_8 (input_bfd, contents + roff - 3);
4296 type = bfd_get_8 (input_bfd, contents + roff - 2);
4297 reg = bfd_get_8 (input_bfd, contents + roff - 1);
bffbf940 4298 reg >>= 3;
bffbf940
JJ
4299 if (type == 0x8b)
4300 {
4301 /* movq */
4302 if (val == 0x4c)
4303 bfd_put_8 (output_bfd, 0x49,
142411ca 4304 contents + roff - 3);
4a4c5f25
L
4305 else if (!ABI_64_P (output_bfd) && val == 0x44)
4306 bfd_put_8 (output_bfd, 0x41,
4307 contents + roff - 3);
bffbf940 4308 bfd_put_8 (output_bfd, 0xc7,
142411ca 4309 contents + roff - 2);
bffbf940 4310 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 4311 contents + roff - 1);
bffbf940
JJ
4312 }
4313 else if (reg == 4)
4314 {
4315 /* addq -> addq - addressing with %rsp/%r12 is
4316 special */
4317 if (val == 0x4c)
4318 bfd_put_8 (output_bfd, 0x49,
142411ca 4319 contents + roff - 3);
4a4c5f25
L
4320 else if (!ABI_64_P (output_bfd) && val == 0x44)
4321 bfd_put_8 (output_bfd, 0x41,
4322 contents + roff - 3);
bffbf940 4323 bfd_put_8 (output_bfd, 0x81,
142411ca 4324 contents + roff - 2);
bffbf940 4325 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 4326 contents + roff - 1);
bffbf940
JJ
4327 }
4328 else
4329 {
4330 /* addq -> leaq */
4331 if (val == 0x4c)
4332 bfd_put_8 (output_bfd, 0x4d,
142411ca 4333 contents + roff - 3);
4a4c5f25
L
4334 else if (!ABI_64_P (output_bfd) && val == 0x44)
4335 bfd_put_8 (output_bfd, 0x45,
4336 contents + roff - 3);
bffbf940 4337 bfd_put_8 (output_bfd, 0x8d,
142411ca 4338 contents + roff - 2);
bffbf940 4339 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
142411ca 4340 contents + roff - 1);
bffbf940 4341 }
eb4ff4d6 4342 bfd_put_32 (output_bfd,
351f65ca 4343 elf_x86_64_tpoff (info, relocation),
142411ca 4344 contents + roff);
bffbf940
JJ
4345 continue;
4346 }
142411ca
L
4347 else
4348 BFD_ASSERT (FALSE);
bffbf940
JJ
4349 }
4350
6de2ae4a 4351 if (htab->elf.sgot == NULL)
bffbf940
JJ
4352 abort ();
4353
4354 if (h != NULL)
67a4f2b7
AO
4355 {
4356 off = h->got.offset;
351f65ca 4357 offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
67a4f2b7 4358 }
bffbf940
JJ
4359 else
4360 {
4361 if (local_got_offsets == NULL)
4362 abort ();
4363
4364 off = local_got_offsets[r_symndx];
67a4f2b7 4365 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
4366 }
4367
4368 if ((off & 1) != 0)
4369 off &= ~1;
26e41594 4370 else
bffbf940
JJ
4371 {
4372 Elf_Internal_Rela outrel;
bffbf940 4373 int dr_type, indx;
67a4f2b7 4374 asection *sreloc;
bffbf940 4375
6de2ae4a 4376 if (htab->elf.srelgot == NULL)
bffbf940
JJ
4377 abort ();
4378
67a4f2b7
AO
4379 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4380
4381 if (GOT_TLS_GDESC_P (tls_type))
4382 {
351f65ca 4383 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
67a4f2b7 4384 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
6de2ae4a
L
4385 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4386 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4387 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
4388 + offplt
4389 + htab->sgotplt_jump_table_size);
6de2ae4a 4390 sreloc = htab->elf.srelplt;
67a4f2b7 4391 if (indx == 0)
351f65ca 4392 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
67a4f2b7
AO
4393 else
4394 outrel.r_addend = 0;
351f65ca 4395 elf_append_rela (output_bfd, sreloc, &outrel);
67a4f2b7
AO
4396 }
4397
6de2ae4a 4398 sreloc = htab->elf.srelgot;
67a4f2b7 4399
6de2ae4a
L
4400 outrel.r_offset = (htab->elf.sgot->output_section->vma
4401 + htab->elf.sgot->output_offset + off);
bffbf940 4402
67a4f2b7 4403 if (GOT_TLS_GD_P (tls_type))
bffbf940 4404 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
4405 else if (GOT_TLS_GDESC_P (tls_type))
4406 goto dr_done;
bffbf940
JJ
4407 else
4408 dr_type = R_X86_64_TPOFF64;
4409
6de2ae4a 4410 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
bffbf940 4411 outrel.r_addend = 0;
67a4f2b7
AO
4412 if ((dr_type == R_X86_64_TPOFF64
4413 || dr_type == R_X86_64_TLSDESC) && indx == 0)
351f65ca
L
4414 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
4415 outrel.r_info = htab->r_info (indx, dr_type);
bffbf940 4416
351f65ca 4417 elf_append_rela (output_bfd, sreloc, &outrel);
bffbf940 4418
67a4f2b7 4419 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
4420 {
4421 if (indx == 0)
4422 {
d40d037c 4423 BFD_ASSERT (! unresolved_reloc);
bffbf940 4424 bfd_put_64 (output_bfd,
351f65ca 4425 relocation - elf_x86_64_dtpoff_base (info),
6de2ae4a 4426 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
bffbf940
JJ
4427 }
4428 else
4429 {
4430 bfd_put_64 (output_bfd, 0,
6de2ae4a 4431 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 4432 outrel.r_info = htab->r_info (indx,
bffbf940
JJ
4433 R_X86_64_DTPOFF64);
4434 outrel.r_offset += GOT_ENTRY_SIZE;
351f65ca 4435 elf_append_rela (output_bfd, sreloc,
464d3bd4 4436 &outrel);
bffbf940
JJ
4437 }
4438 }
4439
67a4f2b7 4440 dr_done:
bffbf940
JJ
4441 if (h != NULL)
4442 h->got.offset |= 1;
4443 else
4444 local_got_offsets[r_symndx] |= 1;
4445 }
4446
67a4f2b7
AO
4447 if (off >= (bfd_vma) -2
4448 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940 4449 abort ();
351f65ca 4450 if (r_type == ELF32_R_TYPE (rel->r_info))
bffbf940 4451 {
67a4f2b7
AO
4452 if (r_type == R_X86_64_GOTPC32_TLSDESC
4453 || r_type == R_X86_64_TLSDESC_CALL)
6de2ae4a
L
4454 relocation = htab->elf.sgotplt->output_section->vma
4455 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
4456 + offplt + htab->sgotplt_jump_table_size;
4457 else
6de2ae4a
L
4458 relocation = htab->elf.sgot->output_section->vma
4459 + htab->elf.sgot->output_offset + off;
b34976b6 4460 unresolved_reloc = FALSE;
bffbf940 4461 }
142411ca 4462 else
67a4f2b7 4463 {
142411ca 4464 bfd_vma roff = rel->r_offset;
67a4f2b7 4465
351f65ca 4466 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
142411ca 4467 {
52bc799a 4468 /* GD->IE transition. For 64bit, change
142411ca
L
4469 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4470 .word 0x6666; rex64; call __tls_get_addr@plt
52bc799a 4471 into:
142411ca 4472 movq %fs:0, %rax
52bc799a
L
4473 addq foo@gottpoff(%rip), %rax
4474 For 32bit, change
4475 leaq foo@tlsgd(%rip), %rdi
4476 .word 0x6666; rex64; call __tls_get_addr@plt
4477 into:
4478 movl %fs:0, %eax
5c98a14e
JJ
4479 addq foo@gottpoff(%rip), %rax
4480 For largepic, change:
4481 leaq foo@tlsgd(%rip), %rdi
4482 movabsq $__tls_get_addr@pltoff, %rax
4483 addq %rbx, %rax
4484 call *%rax
4485 into:
4486 movq %fs:0, %rax
4487 addq foo@gottpoff(%rax), %rax
4488 nopw 0x0(%rax,%rax,1) */
4489 int largepic = 0;
4490 if (ABI_64_P (output_bfd)
4491 && contents[roff + 5] == (bfd_byte) '\xb8')
4492 {
4493 memcpy (contents + roff - 3,
4494 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
4495 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4496 largepic = 1;
4497 }
4498 else if (ABI_64_P (output_bfd))
52bc799a
L
4499 memcpy (contents + roff - 4,
4500 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4501 16);
4502 else
4503 memcpy (contents + roff - 3,
4504 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4505 15);
142411ca 4506
6de2ae4a
L
4507 relocation = (htab->elf.sgot->output_section->vma
4508 + htab->elf.sgot->output_offset + off
142411ca 4509 - roff
5c98a14e 4510 - largepic
142411ca
L
4511 - input_section->output_section->vma
4512 - input_section->output_offset
4513 - 12);
4514 bfd_put_32 (output_bfd, relocation,
5c98a14e
JJ
4515 contents + roff + 8 + largepic);
4516 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
142411ca
L
4517 rel++;
4518 continue;
4519 }
351f65ca 4520 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
142411ca
L
4521 {
4522 /* GDesc -> IE transition.
4523 It's originally something like:
4524 leaq x@tlsdesc(%rip), %rax
67a4f2b7 4525
142411ca 4526 Change it to:
c9736ba0 4527 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
67a4f2b7 4528
142411ca
L
4529 /* Now modify the instruction as appropriate. To
4530 turn a leaq into a movq in the form we use it, it
4531 suffices to change the second byte from 0x8d to
4532 0x8b. */
4533 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4534
4535 bfd_put_32 (output_bfd,
6de2ae4a
L
4536 htab->elf.sgot->output_section->vma
4537 + htab->elf.sgot->output_offset + off
142411ca
L
4538 - rel->r_offset
4539 - input_section->output_section->vma
4540 - input_section->output_offset
4541 - 4,
4542 contents + roff);
4543 continue;
4544 }
351f65ca 4545 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
142411ca
L
4546 {
4547 /* GDesc -> IE transition.
4548 It's originally:
4549 call *(%rax)
4550
4551 Change it to:
c9736ba0 4552 xchg %ax, %ax. */
142411ca 4553
142411ca
L
4554 bfd_put_8 (output_bfd, 0x66, contents + roff);
4555 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4556 continue;
4557 }
4558 else
4559 BFD_ASSERT (FALSE);
67a4f2b7 4560 }
bffbf940
JJ
4561 break;
4562
4563 case R_X86_64_TLSLD:
351f65ca
L
4564 if (! elf_x86_64_tls_transition (info, input_bfd,
4565 input_section, contents,
4566 symtab_hdr, sym_hashes,
4567 &r_type, GOT_UNKNOWN,
4568 rel, relend, h, r_symndx))
142411ca 4569 return FALSE;
a3fadc9a 4570
142411ca
L
4571 if (r_type != R_X86_64_TLSLD)
4572 {
bffbf940 4573 /* LD->LE transition:
a3fadc9a 4574 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
52bc799a
L
4575 For 64bit, we change it into:
4576 .word 0x6666; .byte 0x66; movq %fs:0, %rax.
4577 For 32bit, we change it into:
5c98a14e
JJ
4578 nopl 0x0(%rax); movl %fs:0, %eax.
4579 For largepic, change:
4580 leaq foo@tlsgd(%rip), %rdi
4581 movabsq $__tls_get_addr@pltoff, %rax
4582 addq %rbx, %rax
4583 call *%rax
4584 into:
4585 data32 data32 data32 nopw %cs:0x0(%rax,%rax,1)
4586 movq %fs:0, %eax */
142411ca
L
4587
4588 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
5c98a14e
JJ
4589 if (ABI_64_P (output_bfd)
4590 && contents[rel->r_offset + 5] == (bfd_byte) '\xb8')
4591 memcpy (contents + rel->r_offset - 3,
4592 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
4593 "\x64\x48\x8b\x04\x25\0\0\0", 22);
4594 else if (ABI_64_P (output_bfd))
52bc799a
L
4595 memcpy (contents + rel->r_offset - 3,
4596 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4597 else
4598 memcpy (contents + rel->r_offset - 3,
4599 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
5c98a14e 4600 /* Skip R_X86_64_PC32/R_X86_64_PLT32/R_X86_64_PLTOFF64. */
bffbf940
JJ
4601 rel++;
4602 continue;
4603 }
4604
6de2ae4a 4605 if (htab->elf.sgot == NULL)
bffbf940
JJ
4606 abort ();
4607
4608 off = htab->tls_ld_got.offset;
4609 if (off & 1)
4610 off &= ~1;
4611 else
4612 {
4613 Elf_Internal_Rela outrel;
bffbf940 4614
6de2ae4a 4615 if (htab->elf.srelgot == NULL)
bffbf940
JJ
4616 abort ();
4617
6de2ae4a
L
4618 outrel.r_offset = (htab->elf.sgot->output_section->vma
4619 + htab->elf.sgot->output_offset + off);
bffbf940
JJ
4620
4621 bfd_put_64 (output_bfd, 0,
6de2ae4a 4622 htab->elf.sgot->contents + off);
bffbf940 4623 bfd_put_64 (output_bfd, 0,
6de2ae4a 4624 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 4625 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
bffbf940 4626 outrel.r_addend = 0;
351f65ca 4627 elf_append_rela (output_bfd, htab->elf.srelgot,
464d3bd4 4628 &outrel);
bffbf940
JJ
4629 htab->tls_ld_got.offset |= 1;
4630 }
6de2ae4a
L
4631 relocation = htab->elf.sgot->output_section->vma
4632 + htab->elf.sgot->output_offset + off;
b34976b6 4633 unresolved_reloc = FALSE;
bffbf940
JJ
4634 break;
4635
4636 case R_X86_64_DTPOFF32:
1d85728f 4637 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
351f65ca 4638 relocation -= elf_x86_64_dtpoff_base (info);
bffbf940 4639 else
351f65ca 4640 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4641 break;
4642
4643 case R_X86_64_TPOFF32:
6769d501 4644 case R_X86_64_TPOFF64:
9b769489 4645 BFD_ASSERT (info->executable);
351f65ca 4646 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4647 break;
4648
a69ed7f7
L
4649 case R_X86_64_DTPOFF64:
4650 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
4651 relocation -= elf_x86_64_dtpoff_base (info);
4652 break;
4653
70256ad8
AJ
4654 default:
4655 break;
4656 }
8d88c4ca 4657
239e1f3a
AM
4658 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4659 because such sections are not SEC_ALLOC and thus ld.so will
4660 not process them. */
c434dee6 4661 if (unresolved_reloc
239e1f3a 4662 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
4663 && h->def_dynamic)
4664 && _bfd_elf_section_offset (output_bfd, info, input_section,
4665 rel->r_offset) != (bfd_vma) -1)
a040981f
L
4666 {
4667 (*_bfd_error_handler)
4668 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4669 input_bfd,
4670 input_section,
4671 (long) rel->r_offset,
4672 howto->name,
4673 h->root.root.string);
4674 return FALSE;
4675 }
c434dee6 4676
cbe950e9 4677do_relocation:
8d88c4ca 4678 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
4679 contents, rel->r_offset,
4680 relocation, rel->r_addend);
8d88c4ca 4681
62d78908 4682check_relocation_error:
8d88c4ca 4683 if (r != bfd_reloc_ok)
8da6118f 4684 {
c434dee6
AJ
4685 const char *name;
4686
4687 if (h != NULL)
4688 name = h->root.root.string;
4689 else
8da6118f 4690 {
c434dee6
AJ
4691 name = bfd_elf_string_from_elf_section (input_bfd,
4692 symtab_hdr->sh_link,
4693 sym->st_name);
4694 if (name == NULL)
b34976b6 4695 return FALSE;
c434dee6
AJ
4696 if (*name == '\0')
4697 name = bfd_section_name (input_bfd, sec);
4698 }
4699
4700 if (r == bfd_reloc_overflow)
4701 {
c434dee6 4702 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
4703 (info, (h ? &h->root : NULL), name, howto->name,
4704 (bfd_vma) 0, input_bfd, input_section,
4705 rel->r_offset)))
b34976b6 4706 return FALSE;
c434dee6
AJ
4707 }
4708 else
4709 {
4710 (*_bfd_error_handler)
bb95161d 4711 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
d003868e 4712 input_bfd, input_section,
c434dee6 4713 (long) rel->r_offset, name, (int) r);
b34976b6 4714 return FALSE;
8da6118f
KH
4715 }
4716 }
8d88c4ca 4717 }
70256ad8 4718
b34976b6 4719 return TRUE;
70256ad8
AJ
4720}
4721
4722/* Finish up dynamic symbol handling. We set the contents of various
4723 dynamic sections here. */
4724
b34976b6 4725static bfd_boolean
351f65ca
L
4726elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4727 struct bfd_link_info *info,
4728 struct elf_link_hash_entry *h,
220cf809 4729 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
70256ad8 4730{
351f65ca 4731 struct elf_x86_64_link_hash_table *htab;
0ff2b86e
L
4732 const struct elf_x86_64_backend_data *abed;
4733 bfd_boolean use_plt_bnd;
70256ad8 4734
351f65ca 4735 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
4736 if (htab == NULL)
4737 return FALSE;
70256ad8 4738
0ff2b86e
L
4739 /* Use MPX backend data in case of BND relocation. Use .plt_bnd
4740 section only if there is .plt section. */
4741 use_plt_bnd = htab->elf.splt != NULL && htab->plt_bnd != NULL;
4742 abed = (use_plt_bnd
4743 ? &elf_x86_64_bnd_arch_bed
4744 : get_elf_x86_64_backend_data (output_bfd));
4745
70256ad8
AJ
4746 if (h->plt.offset != (bfd_vma) -1)
4747 {
70256ad8 4748 bfd_vma plt_index;
0ff2b86e
L
4749 bfd_vma got_offset, plt_offset, plt_plt_offset, plt_got_offset;
4750 bfd_vma plt_plt_insn_end, plt_got_insn_size;
70256ad8 4751 Elf_Internal_Rela rela;
947216bf 4752 bfd_byte *loc;
0ff2b86e 4753 asection *plt, *gotplt, *relplt, *resolved_plt;
351f65ca 4754 const struct elf_backend_data *bed;
cbe950e9
L
4755
4756 /* When building a static executable, use .iplt, .igot.plt and
4757 .rela.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 4758 if (htab->elf.splt != NULL)
cbe950e9 4759 {
6de2ae4a
L
4760 plt = htab->elf.splt;
4761 gotplt = htab->elf.sgotplt;
4762 relplt = htab->elf.srelplt;
cbe950e9
L
4763 }
4764 else
4765 {
6de2ae4a
L
4766 plt = htab->elf.iplt;
4767 gotplt = htab->elf.igotplt;
4768 relplt = htab->elf.irelplt;
cbe950e9 4769 }
70256ad8
AJ
4770
4771 /* This symbol has an entry in the procedure linkage table. Set
407443a3 4772 it up. */
cbe950e9
L
4773 if ((h->dynindx == -1
4774 && !((h->forced_local || info->executable)
4775 && h->def_regular
4776 && h->type == STT_GNU_IFUNC))
4777 || plt == NULL
4778 || gotplt == NULL
4779 || relplt == NULL)
cec7f46a 4780 abort ();
70256ad8
AJ
4781
4782 /* Get the index in the procedure linkage table which
4783 corresponds to this symbol. This is the index of this symbol
4784 in all the symbols for which we are making plt entries. The
cbe950e9 4785 first entry in the procedure linkage table is reserved.
6bbec505 4786
cbe950e9 4787 Get the offset into the .got table of the entry that
407443a3 4788 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
cbe950e9
L
4789 bytes. The first three are reserved for the dynamic linker.
4790
4791 For static executables, we don't reserve anything. */
4792
6de2ae4a 4793 if (plt == htab->elf.splt)
cbe950e9 4794 {
eed180f8 4795 got_offset = h->plt.offset / abed->plt_entry_size - 1;
e1f98742 4796 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
cbe950e9
L
4797 }
4798 else
4799 {
eed180f8 4800 got_offset = h->plt.offset / abed->plt_entry_size;
e1f98742 4801 got_offset = got_offset * GOT_ENTRY_SIZE;
cbe950e9 4802 }
70256ad8 4803
0ff2b86e
L
4804 plt_plt_insn_end = abed->plt_plt_insn_end;
4805 plt_plt_offset = abed->plt_plt_offset;
4806 plt_got_insn_size = abed->plt_got_insn_size;
4807 plt_got_offset = abed->plt_got_offset;
4808 if (use_plt_bnd)
4809 {
4810 /* Use the second PLT with BND relocations. */
4811 const bfd_byte *plt_entry, *plt2_entry;
4812 struct elf_x86_64_link_hash_entry *eh
4813 = (struct elf_x86_64_link_hash_entry *) h;
4814
4815 if (eh->has_bnd_reloc)
4816 {
4817 plt_entry = elf_x86_64_bnd_plt_entry;
4818 plt2_entry = elf_x86_64_bnd_plt2_entry;
4819 }
4820 else
4821 {
4822 plt_entry = elf_x86_64_legacy_plt_entry;
4823 plt2_entry = elf_x86_64_legacy_plt2_entry;
4824
4825 /* Subtract 1 since there is no BND prefix. */
4826 plt_plt_insn_end -= 1;
4827 plt_plt_offset -= 1;
4828 plt_got_insn_size -= 1;
4829 plt_got_offset -= 1;
4830 }
4831
4832 BFD_ASSERT (sizeof (elf_x86_64_bnd_plt_entry)
4833 == sizeof (elf_x86_64_legacy_plt_entry));
4834
4835 /* Fill in the entry in the procedure linkage table. */
4836 memcpy (plt->contents + h->plt.offset,
4837 plt_entry, sizeof (elf_x86_64_legacy_plt_entry));
4838 /* Fill in the entry in the second PLT. */
4839 memcpy (htab->plt_bnd->contents + eh->plt_bnd.offset,
4840 plt2_entry, sizeof (elf_x86_64_legacy_plt2_entry));
4841
4842 resolved_plt = htab->plt_bnd;
4843 plt_offset = eh->plt_bnd.offset;
4844 }
4845 else
4846 {
4847 /* Fill in the entry in the procedure linkage table. */
4848 memcpy (plt->contents + h->plt.offset, abed->plt_entry,
4849 abed->plt_entry_size);
4850
4851 resolved_plt = plt;
4852 plt_offset = h->plt.offset;
4853 }
eed180f8
RM
4854
4855 /* Insert the relocation positions of the plt section. */
4856
4857 /* Put offset the PC-relative instruction referring to the GOT entry,
4858 subtracting the size of that instruction. */
653165cc 4859 bfd_put_32 (output_bfd,
eed180f8
RM
4860 (gotplt->output_section->vma
4861 + gotplt->output_offset
4862 + got_offset
0ff2b86e
L
4863 - resolved_plt->output_section->vma
4864 - resolved_plt->output_offset
4865 - plt_offset
4866 - plt_got_insn_size),
4867 resolved_plt->contents + plt_offset + plt_got_offset);
cbe950e9 4868
653165cc 4869 /* Fill in the entry in the global offset table, initially this
eed180f8 4870 points to the second part of the PLT entry. */
cbe950e9
L
4871 bfd_put_64 (output_bfd, (plt->output_section->vma
4872 + plt->output_offset
eed180f8 4873 + h->plt.offset + abed->plt_lazy_offset),
cbe950e9 4874 gotplt->contents + got_offset);
70256ad8
AJ
4875
4876 /* Fill in the entry in the .rela.plt section. */
cbe950e9
L
4877 rela.r_offset = (gotplt->output_section->vma
4878 + gotplt->output_offset
70256ad8 4879 + got_offset);
cbe950e9
L
4880 if (h->dynindx == -1
4881 || ((info->executable
4882 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4883 && h->def_regular
4884 && h->type == STT_GNU_IFUNC))
4885 {
4886 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4887 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
351f65ca 4888 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
cbe950e9
L
4889 rela.r_addend = (h->root.u.def.value
4890 + h->root.u.def.section->output_section->vma
4891 + h->root.u.def.section->output_offset);
e1f98742
L
4892 /* R_X86_64_IRELATIVE comes last. */
4893 plt_index = htab->next_irelative_index--;
cbe950e9
L
4894 }
4895 else
4896 {
351f65ca 4897 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
cbe950e9 4898 rela.r_addend = 0;
e1f98742
L
4899 plt_index = htab->next_jump_slot_index++;
4900 }
4901
4902 /* Don't fill PLT entry for static executables. */
4903 if (plt == htab->elf.splt)
4904 {
4905 /* Put relocation index. */
4906 bfd_put_32 (output_bfd, plt_index,
eed180f8 4907 plt->contents + h->plt.offset + abed->plt_reloc_offset);
e1f98742 4908 /* Put offset for jmp .PLT0. */
0ff2b86e
L
4909 bfd_put_32 (output_bfd, - (h->plt.offset + plt_plt_insn_end),
4910 plt->contents + h->plt.offset + plt_plt_offset);
cbe950e9 4911 }
351f65ca
L
4912
4913 bed = get_elf_backend_data (output_bfd);
4914 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
82e96e07 4915 bed->s->swap_reloca_out (output_bfd, &rela, loc);
70256ad8 4916
f5385ebf 4917 if (!h->def_regular)
70256ad8
AJ
4918 {
4919 /* Mark the symbol as undefined, rather than as defined in
47a9f7b3
JJ
4920 the .plt section. Leave the value if there were any
4921 relocations where pointer equality matters (this is a clue
c434dee6
AJ
4922 for the dynamic linker, to make function pointer
4923 comparisons work between an application and shared
47a9f7b3
JJ
4924 library), otherwise set it to zero. If a function is only
4925 called from a binary, there is no need to slow down
4926 shared libraries because of that. */
70256ad8 4927 sym->st_shndx = SHN_UNDEF;
f5385ebf 4928 if (!h->pointer_equality_needed)
47a9f7b3 4929 sym->st_value = 0;
70256ad8
AJ
4930 }
4931 }
4932
bffbf940 4933 if (h->got.offset != (bfd_vma) -1
351f65ca
L
4934 && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
4935 && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
053579d7 4936 {
053579d7
AJ
4937 Elf_Internal_Rela rela;
4938
4939 /* This symbol has an entry in the global offset table. Set it
bffbf940 4940 up. */
6de2ae4a 4941 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
c434dee6 4942 abort ();
053579d7 4943
6de2ae4a
L
4944 rela.r_offset = (htab->elf.sgot->output_section->vma
4945 + htab->elf.sgot->output_offset
dc810e39 4946 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
4947
4948 /* If this is a static link, or it is a -Bsymbolic link and the
4949 symbol is defined locally or was forced to be local because
4950 of a version file, we just want to emit a RELATIVE reloc.
4951 The entry in the global offset table will already have been
4952 initialized in the relocate_section function. */
710ab287 4953 if (h->def_regular
0018b0a3
L
4954 && h->type == STT_GNU_IFUNC)
4955 {
710ab287
L
4956 if (info->shared)
4957 {
4958 /* Generate R_X86_64_GLOB_DAT. */
4959 goto do_glob_dat;
4960 }
4961 else
4962 {
90d60710
L
4963 asection *plt;
4964
710ab287
L
4965 if (!h->pointer_equality_needed)
4966 abort ();
4967
4968 /* For non-shared object, we can't use .got.plt, which
4969 contains the real function addres if we need pointer
4970 equality. We load the GOT entry with the PLT entry. */
90d60710 4971 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
710ab287
L
4972 bfd_put_64 (output_bfd, (plt->output_section->vma
4973 + plt->output_offset
4974 + h->plt.offset),
6de2ae4a 4975 htab->elf.sgot->contents + h->got.offset);
710ab287
L
4976 return TRUE;
4977 }
0018b0a3
L
4978 }
4979 else if (info->shared
4980 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 4981 {
41bed6dd
L
4982 if (!h->def_regular)
4983 return FALSE;
cc78d0af 4984 BFD_ASSERT((h->got.offset & 1) != 0);
351f65ca 4985 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
053579d7
AJ
4986 rela.r_addend = (h->root.u.def.value
4987 + h->root.u.def.section->output_section->vma
4988 + h->root.u.def.section->output_offset);
4989 }
4990 else
4991 {
4992 BFD_ASSERT((h->got.offset & 1) == 0);
710ab287 4993do_glob_dat:
c434dee6 4994 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 4995 htab->elf.sgot->contents + h->got.offset);
351f65ca 4996 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
053579d7
AJ
4997 rela.r_addend = 0;
4998 }
4999
351f65ca 5000 elf_append_rela (output_bfd, htab->elf.srelgot, &rela);
053579d7
AJ
5001 }
5002
f5385ebf 5003 if (h->needs_copy)
70256ad8 5004 {
70256ad8
AJ
5005 Elf_Internal_Rela rela;
5006
5007 /* This symbol needs a copy reloc. Set it up. */
5008
c434dee6
AJ
5009 if (h->dynindx == -1
5010 || (h->root.type != bfd_link_hash_defined
5011 && h->root.type != bfd_link_hash_defweak)
5012 || htab->srelbss == NULL)
5013 abort ();
70256ad8
AJ
5014
5015 rela.r_offset = (h->root.u.def.value
5016 + h->root.u.def.section->output_section->vma
5017 + h->root.u.def.section->output_offset);
351f65ca 5018 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
70256ad8 5019 rela.r_addend = 0;
351f65ca 5020 elf_append_rela (output_bfd, htab->srelbss, &rela);
70256ad8
AJ
5021 }
5022
b34976b6 5023 return TRUE;
70256ad8
AJ
5024}
5025
c25bc9fc
L
5026/* Finish up local dynamic symbol handling. We set the contents of
5027 various dynamic sections here. */
5028
5029static bfd_boolean
351f65ca 5030elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
c25bc9fc
L
5031{
5032 struct elf_link_hash_entry *h
5033 = (struct elf_link_hash_entry *) *slot;
5034 struct bfd_link_info *info
eed180f8 5035 = (struct bfd_link_info *) inf;
c25bc9fc 5036
351f65ca 5037 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
c25bc9fc
L
5038 info, h, NULL);
5039}
5040
c434dee6
AJ
5041/* Used to decide how to sort relocs in an optimal manner for the
5042 dynamic linker, before writing them out. */
5043
5044static enum elf_reloc_type_class
7e612e98
AM
5045elf_x86_64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5046 const asection *rel_sec ATTRIBUTE_UNUSED,
5047 const Elf_Internal_Rela *rela)
c434dee6 5048{
351f65ca 5049 switch ((int) ELF32_R_TYPE (rela->r_info))
c434dee6
AJ
5050 {
5051 case R_X86_64_RELATIVE:
1da80baa 5052 case R_X86_64_RELATIVE64:
c434dee6
AJ
5053 return reloc_class_relative;
5054 case R_X86_64_JUMP_SLOT:
5055 return reloc_class_plt;
5056 case R_X86_64_COPY:
5057 return reloc_class_copy;
5058 default:
5059 return reloc_class_normal;
5060 }
5061}
5062
70256ad8
AJ
5063/* Finish up the dynamic sections. */
5064
b34976b6 5065static bfd_boolean
351f65ca
L
5066elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
5067 struct bfd_link_info *info)
70256ad8 5068{
351f65ca 5069 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
5070 bfd *dynobj;
5071 asection *sdyn;
0ff2b86e 5072 const struct elf_x86_64_backend_data *abed;
70256ad8 5073
351f65ca 5074 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
5075 if (htab == NULL)
5076 return FALSE;
5077
0ff2b86e
L
5078 /* Use MPX backend data in case of BND relocation. Use .plt_bnd
5079 section only if there is .plt section. */
5080 abed = (htab->elf.splt != NULL && htab->plt_bnd != NULL
5081 ? &elf_x86_64_bnd_arch_bed
5082 : get_elf_x86_64_backend_data (output_bfd));
5083
c434dee6 5084 dynobj = htab->elf.dynobj;
3d4d4302 5085 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
70256ad8 5086
c434dee6 5087 if (htab->elf.dynamic_sections_created)
70256ad8 5088 {
82e96e07
L
5089 bfd_byte *dyncon, *dynconend;
5090 const struct elf_backend_data *bed;
5091 bfd_size_type sizeof_dyn;
70256ad8 5092
6de2ae4a 5093 if (sdyn == NULL || htab->elf.sgot == NULL)
c434dee6 5094 abort ();
70256ad8 5095
82e96e07
L
5096 bed = get_elf_backend_data (dynobj);
5097 sizeof_dyn = bed->s->sizeof_dyn;
5098 dyncon = sdyn->contents;
5099 dynconend = sdyn->contents + sdyn->size;
5100 for (; dyncon < dynconend; dyncon += sizeof_dyn)
70256ad8
AJ
5101 {
5102 Elf_Internal_Dyn dyn;
70256ad8
AJ
5103 asection *s;
5104
82e96e07 5105 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
70256ad8
AJ
5106
5107 switch (dyn.d_tag)
5108 {
5109 default:
053579d7 5110 continue;
70256ad8
AJ
5111
5112 case DT_PLTGOT:
6de2ae4a 5113 s = htab->elf.sgotplt;
8c37241b 5114 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 5115 break;
70256ad8
AJ
5116
5117 case DT_JMPREL:
6de2ae4a 5118 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
c434dee6 5119 break;
70256ad8 5120
c434dee6 5121 case DT_PLTRELSZ:
6de2ae4a 5122 s = htab->elf.srelplt->output_section;
eea6121a 5123 dyn.d_un.d_val = s->size;
70256ad8
AJ
5124 break;
5125
5126 case DT_RELASZ:
c434dee6
AJ
5127 /* The procedure linkage table relocs (DT_JMPREL) should
5128 not be included in the overall relocs (DT_RELA).
5129 Therefore, we override the DT_RELASZ entry here to
5130 make it not include the JMPREL relocs. Since the
5131 linker script arranges for .rela.plt to follow all
5132 other relocation sections, we don't have to worry
5133 about changing the DT_RELA entry. */
6de2ae4a 5134 if (htab->elf.srelplt != NULL)
70256ad8 5135 {
6de2ae4a 5136 s = htab->elf.srelplt->output_section;
eea6121a 5137 dyn.d_un.d_val -= s->size;
70256ad8
AJ
5138 }
5139 break;
67a4f2b7
AO
5140
5141 case DT_TLSDESC_PLT:
6de2ae4a 5142 s = htab->elf.splt;
67a4f2b7
AO
5143 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
5144 + htab->tlsdesc_plt;
5145 break;
5146
5147 case DT_TLSDESC_GOT:
6de2ae4a 5148 s = htab->elf.sgot;
67a4f2b7
AO
5149 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
5150 + htab->tlsdesc_got;
5151 break;
70256ad8 5152 }
c434dee6 5153
82e96e07 5154 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
70256ad8
AJ
5155 }
5156
c434dee6 5157 /* Fill in the special first entry in the procedure linkage table. */
6de2ae4a 5158 if (htab->elf.splt && htab->elf.splt->size > 0)
70256ad8 5159 {
653165cc 5160 /* Fill in the first entry in the procedure linkage table. */
eed180f8
RM
5161 memcpy (htab->elf.splt->contents,
5162 abed->plt0_entry, abed->plt_entry_size);
653165cc
AJ
5163 /* Add offset for pushq GOT+8(%rip), since the instruction
5164 uses 6 bytes subtract this value. */
5165 bfd_put_32 (output_bfd,
6de2ae4a
L
5166 (htab->elf.sgotplt->output_section->vma
5167 + htab->elf.sgotplt->output_offset
653165cc 5168 + 8
6de2ae4a
L
5169 - htab->elf.splt->output_section->vma
5170 - htab->elf.splt->output_offset
653165cc 5171 - 6),
eed180f8
RM
5172 htab->elf.splt->contents + abed->plt0_got1_offset);
5173 /* Add offset for the PC-relative instruction accessing GOT+16,
5174 subtracting the offset to the end of that instruction. */
653165cc 5175 bfd_put_32 (output_bfd,
6de2ae4a
L
5176 (htab->elf.sgotplt->output_section->vma
5177 + htab->elf.sgotplt->output_offset
653165cc 5178 + 16
6de2ae4a
L
5179 - htab->elf.splt->output_section->vma
5180 - htab->elf.splt->output_offset
eed180f8
RM
5181 - abed->plt0_got2_insn_end),
5182 htab->elf.splt->contents + abed->plt0_got2_offset);
653165cc 5183
eed180f8
RM
5184 elf_section_data (htab->elf.splt->output_section)
5185 ->this_hdr.sh_entsize = abed->plt_entry_size;
67a4f2b7
AO
5186
5187 if (htab->tlsdesc_plt)
5188 {
5189 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 5190 htab->elf.sgot->contents + htab->tlsdesc_got);
67a4f2b7 5191
6de2ae4a 5192 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
eed180f8 5193 abed->plt0_entry, abed->plt_entry_size);
67a4f2b7
AO
5194
5195 /* Add offset for pushq GOT+8(%rip), since the
5196 instruction uses 6 bytes subtract this value. */
5197 bfd_put_32 (output_bfd,
6de2ae4a
L
5198 (htab->elf.sgotplt->output_section->vma
5199 + htab->elf.sgotplt->output_offset
67a4f2b7 5200 + 8
6de2ae4a
L
5201 - htab->elf.splt->output_section->vma
5202 - htab->elf.splt->output_offset
67a4f2b7
AO
5203 - htab->tlsdesc_plt
5204 - 6),
eed180f8
RM
5205 htab->elf.splt->contents
5206 + htab->tlsdesc_plt + abed->plt0_got1_offset);
5207 /* Add offset for the PC-relative instruction accessing GOT+TDG,
5208 where TGD stands for htab->tlsdesc_got, subtracting the offset
5209 to the end of that instruction. */
67a4f2b7 5210 bfd_put_32 (output_bfd,
6de2ae4a
L
5211 (htab->elf.sgot->output_section->vma
5212 + htab->elf.sgot->output_offset
67a4f2b7 5213 + htab->tlsdesc_got
6de2ae4a
L
5214 - htab->elf.splt->output_section->vma
5215 - htab->elf.splt->output_offset
67a4f2b7 5216 - htab->tlsdesc_plt
eed180f8
RM
5217 - abed->plt0_got2_insn_end),
5218 htab->elf.splt->contents
5219 + htab->tlsdesc_plt + abed->plt0_got2_offset);
67a4f2b7 5220 }
70256ad8 5221 }
70256ad8
AJ
5222 }
5223
0ff2b86e
L
5224 if (htab->plt_bnd != NULL)
5225 elf_section_data (htab->plt_bnd->output_section)
5226 ->this_hdr.sh_entsize = sizeof (elf_x86_64_bnd_plt2_entry);
5227
6de2ae4a 5228 if (htab->elf.sgotplt)
70256ad8 5229 {
56d4289c
L
5230 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
5231 {
5232 (*_bfd_error_handler)
5233 (_("discarded output section: `%A'"), htab->elf.sgotplt);
5234 return FALSE;
5235 }
5236
c434dee6 5237 /* Fill in the first three entries in the global offset table. */
6de2ae4a 5238 if (htab->elf.sgotplt->size > 0)
c434dee6
AJ
5239 {
5240 /* Set the first entry in the global offset table to the address of
5241 the dynamic section. */
5242 if (sdyn == NULL)
6de2ae4a 5243 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
c434dee6
AJ
5244 else
5245 bfd_put_64 (output_bfd,
5246 sdyn->output_section->vma + sdyn->output_offset,
6de2ae4a 5247 htab->elf.sgotplt->contents);
c434dee6 5248 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6de2ae4a
L
5249 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
5250 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
c434dee6 5251 }
70256ad8 5252
6de2ae4a 5253 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
c434dee6
AJ
5254 GOT_ENTRY_SIZE;
5255 }
70256ad8 5256
e41b3a13 5257 /* Adjust .eh_frame for .plt section. */
9a2a56cc
AM
5258 if (htab->plt_eh_frame != NULL
5259 && htab->plt_eh_frame->contents != NULL)
e41b3a13
JJ
5260 {
5261 if (htab->elf.splt != NULL
5262 && htab->elf.splt->size != 0
5263 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
5264 && htab->elf.splt->output_section != NULL
5265 && htab->plt_eh_frame->output_section != NULL)
5266 {
5267 bfd_vma plt_start = htab->elf.splt->output_section->vma;
5268 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
5269 + htab->plt_eh_frame->output_offset
5270 + PLT_FDE_START_OFFSET;
5271 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5272 htab->plt_eh_frame->contents
5273 + PLT_FDE_START_OFFSET);
5274 }
dbaa2011 5275 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
e41b3a13
JJ
5276 {
5277 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5278 htab->plt_eh_frame,
5279 htab->plt_eh_frame->contents))
5280 return FALSE;
5281 }
5282 }
5283
6de2ae4a
L
5284 if (htab->elf.sgot && htab->elf.sgot->size > 0)
5285 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
8c37241b
JJ
5286 = GOT_ENTRY_SIZE;
5287
c25bc9fc
L
5288 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
5289 htab_traverse (htab->loc_hash_table,
351f65ca 5290 elf_x86_64_finish_local_dynamic_symbol,
c25bc9fc
L
5291 info);
5292
b34976b6 5293 return TRUE;
8d88c4ca
NC
5294}
5295
4c45e5c9
JJ
5296/* Return address for Ith PLT stub in section PLT, for relocation REL
5297 or (bfd_vma) -1 if it should not be included. */
5298
5299static bfd_vma
351f65ca
L
5300elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
5301 const arelent *rel ATTRIBUTE_UNUSED)
4c45e5c9 5302{
eed180f8 5303 return plt->vma + (i + 1) * GET_PLT_ENTRY_SIZE (plt->owner);
4c45e5c9 5304}
8df9fc9d 5305
0ff2b86e
L
5306/* Similar to _bfd_elf_get_synthetic_symtab, with .plt.bnd section
5307 support. */
5308
5309static long
5310elf_x86_64_get_synthetic_symtab (bfd *abfd,
5311 long symcount,
5312 asymbol **syms,
5313 long dynsymcount,
5314 asymbol **dynsyms,
5315 asymbol **ret)
5316{
5317 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5318 asection *relplt;
5319 asymbol *s;
5320 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5321 arelent *p;
5322 long count, i, n;
5323 size_t size;
5324 Elf_Internal_Shdr *hdr;
5325 char *names;
5326 asection *plt;
5327 bfd_vma addr;
5328
5329 plt = bfd_get_section_by_name (abfd, ".plt.bnd");
5330 /* Use the generic ELF version if there is no .plt.bnd section. */
5331 if (plt == NULL)
5332 return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
5333 dynsymcount, dynsyms, ret);
5334
5335 *ret = NULL;
5336
5337 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
5338 return 0;
5339
5340 if (dynsymcount <= 0)
5341 return 0;
5342
5343 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
5344 if (relplt == NULL)
5345 return 0;
5346
5347 hdr = &elf_section_data (relplt)->this_hdr;
5348 if (hdr->sh_link != elf_dynsymtab (abfd)
5349 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
5350 return 0;
5351
5352 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5353 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
5354 return -1;
5355
5356 count = relplt->size / hdr->sh_entsize;
5357 size = count * sizeof (asymbol);
5358 p = relplt->relocation;
5359 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
5360 {
5361 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
5362 if (p->addend != 0)
5363 size += sizeof ("+0x") - 1 + 8 + 8;
5364 }
5365
5366 s = *ret = (asymbol *) bfd_malloc (size);
5367 if (s == NULL)
5368 return -1;
5369
5370 names = (char *) (s + count);
5371 p = relplt->relocation;
5372 n = 0;
5373 addr = 0;
5374 for (i = 0; i < count; i++, p++)
5375 {
5376 size_t len;
5377
5378 *s = **p->sym_ptr_ptr;
5379 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
5380 we are defining a symbol, ensure one of them is set. */
5381 if ((s->flags & BSF_LOCAL) == 0)
5382 s->flags |= BSF_GLOBAL;
5383 s->flags |= BSF_SYNTHETIC;
5384 s->section = plt;
5385 s->value = addr;
5386 s->name = names;
5387 s->udata.p = NULL;
5388 len = strlen ((*p->sym_ptr_ptr)->name);
5389 memcpy (names, (*p->sym_ptr_ptr)->name, len);
5390 names += len;
5391 if (p->addend != 0)
5392 {
5393 char buf[30], *a;
5394
5395 memcpy (names, "+0x", sizeof ("+0x") - 1);
5396 names += sizeof ("+0x") - 1;
5397 bfd_sprintf_vma (abfd, buf, p->addend);
5398 for (a = buf; *a == '0'; ++a)
5399 ;
5400 len = strlen (a);
5401 memcpy (names, a, len);
5402 names += len;
5403 }
5404 memcpy (names, "@plt", sizeof ("@plt"));
5405 names += sizeof ("@plt");
5406 ++s, ++n;
5407 addr += sizeof (elf_x86_64_legacy_plt2_entry);
5408 }
5409
5410 return n;
5411}
5412
d2b2c203
DJ
5413/* Handle an x86-64 specific section when reading an object file. This
5414 is called when elfcode.h finds a section with an unknown type. */
5415
5416static bfd_boolean
0c723101
L
5417elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
5418 const char *name, int shindex)
d2b2c203
DJ
5419{
5420 if (hdr->sh_type != SHT_X86_64_UNWIND)
5421 return FALSE;
5422
6dc132d9 5423 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
5424 return FALSE;
5425
5426 return TRUE;
5427}
5428
3b22753a
L
5429/* Hook called by the linker routine which adds symbols from an object
5430 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
5431 of .bss. */
5432
5433static bfd_boolean
351f65ca
L
5434elf_x86_64_add_symbol_hook (bfd *abfd,
5435 struct bfd_link_info *info,
5436 Elf_Internal_Sym *sym,
5437 const char **namep ATTRIBUTE_UNUSED,
5438 flagword *flagsp ATTRIBUTE_UNUSED,
5439 asection **secp,
5440 bfd_vma *valp)
3b22753a
L
5441{
5442 asection *lcomm;
5443
5444 switch (sym->st_shndx)
5445 {
5446 case SHN_X86_64_LCOMMON:
5447 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
5448 if (lcomm == NULL)
5449 {
5450 lcomm = bfd_make_section_with_flags (abfd,
5451 "LARGE_COMMON",
5452 (SEC_ALLOC
5453 | SEC_IS_COMMON
5454 | SEC_LINKER_CREATED));
5455 if (lcomm == NULL)
5456 return FALSE;
5457 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
5458 }
5459 *secp = lcomm;
5460 *valp = sym->st_size;
c35bdf6e 5461 return TRUE;
3b22753a 5462 }
d8045f23 5463
c16153ae 5464 if ((abfd->flags & DYNAMIC) == 0
f64b2e8d
NC
5465 && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
5466 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
5467 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
d8045f23 5468
3b22753a
L
5469 return TRUE;
5470}
5471
5472
5473/* Given a BFD section, try to locate the corresponding ELF section
5474 index. */
5475
5476static bfd_boolean
351f65ca
L
5477elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5478 asection *sec, int *index_return)
3b22753a
L
5479{
5480 if (sec == &_bfd_elf_large_com_section)
5481 {
91d6fa6a 5482 *index_return = SHN_X86_64_LCOMMON;
3b22753a
L
5483 return TRUE;
5484 }
5485 return FALSE;
5486}
5487
5488/* Process a symbol. */
5489
5490static void
351f65ca
L
5491elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5492 asymbol *asym)
3b22753a
L
5493{
5494 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5495
5496 switch (elfsym->internal_elf_sym.st_shndx)
5497 {
5498 case SHN_X86_64_LCOMMON:
5499 asym->section = &_bfd_elf_large_com_section;
5500 asym->value = elfsym->internal_elf_sym.st_size;
5501 /* Common symbol doesn't set BSF_GLOBAL. */
5502 asym->flags &= ~BSF_GLOBAL;
5503 break;
5504 }
5505}
5506
5507static bfd_boolean
351f65ca 5508elf_x86_64_common_definition (Elf_Internal_Sym *sym)
3b22753a
L
5509{
5510 return (sym->st_shndx == SHN_COMMON
5511 || sym->st_shndx == SHN_X86_64_LCOMMON);
5512}
5513
5514static unsigned int
351f65ca 5515elf_x86_64_common_section_index (asection *sec)
3b22753a
L
5516{
5517 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5518 return SHN_COMMON;
5519 else
5520 return SHN_X86_64_LCOMMON;
5521}
5522
5523static asection *
351f65ca 5524elf_x86_64_common_section (asection *sec)
3b22753a
L
5525{
5526 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5527 return bfd_com_section_ptr;
5528 else
5529 return &_bfd_elf_large_com_section;
5530}
5531
5532static bfd_boolean
5d13b3b3
AM
5533elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5534 const Elf_Internal_Sym *sym,
351f65ca 5535 asection **psec,
5d13b3b3
AM
5536 bfd_boolean newdef,
5537 bfd_boolean olddef,
351f65ca 5538 bfd *oldbfd,
5d13b3b3 5539 const asection *oldsec)
3b22753a
L
5540{
5541 /* A normal common symbol and a large common symbol result in a
00492999
L
5542 normal common symbol. We turn the large common symbol into a
5543 normal one. */
5d13b3b3 5544 if (!olddef
3b22753a 5545 && h->root.type == bfd_link_hash_common
5d13b3b3
AM
5546 && !newdef
5547 && bfd_is_com_section (*psec)
5548 && oldsec != *psec)
3b22753a 5549 {
00492999 5550 if (sym->st_shndx == SHN_COMMON
5d13b3b3 5551 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
00492999
L
5552 {
5553 h->root.u.c.p->section
5554 = bfd_make_section_old_way (oldbfd, "COMMON");
5555 h->root.u.c.p->section->flags = SEC_ALLOC;
5556 }
5557 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5d13b3b3
AM
5558 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5559 *psec = bfd_com_section_ptr;
3b22753a
L
5560 }
5561
5562 return TRUE;
5563}
5564
5565static int
351f65ca
L
5566elf_x86_64_additional_program_headers (bfd *abfd,
5567 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
5568{
5569 asection *s;
9a2e389a 5570 int count = 0;
3b22753a
L
5571
5572 /* Check to see if we need a large readonly segment. */
5573 s = bfd_get_section_by_name (abfd, ".lrodata");
5574 if (s && (s->flags & SEC_LOAD))
5575 count++;
5576
5577 /* Check to see if we need a large data segment. Since .lbss sections
5578 is placed right after the .bss section, there should be no need for
5579 a large data segment just because of .lbss. */
5580 s = bfd_get_section_by_name (abfd, ".ldata");
5581 if (s && (s->flags & SEC_LOAD))
5582 count++;
5583
5584 return count;
5585}
5586
fdc90cb4
JJ
5587/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5588
5589static bfd_boolean
351f65ca 5590elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
fdc90cb4
JJ
5591{
5592 if (h->plt.offset != (bfd_vma) -1
5593 && !h->def_regular
5594 && !h->pointer_equality_needed)
5595 return FALSE;
5596
5597 return _bfd_elf_hash_symbol (h);
5598}
5599
c543bf9a
L
5600/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5601
5602static bfd_boolean
5603elf_x86_64_relocs_compatible (const bfd_target *input,
5604 const bfd_target *output)
5605{
5606 return ((xvec_get_elf_backend_data (input)->s->elfclass
5607 == xvec_get_elf_backend_data (output)->s->elfclass)
5608 && _bfd_elf_relocs_compatible (input, output));
5609}
5610
9a2e389a 5611static const struct bfd_elf_special_section
351f65ca 5612 elf_x86_64_special_sections[]=
3b22753a 5613{
0112cd26
NC
5614 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5615 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5616 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
5617 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5618 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5619 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5620 { NULL, 0, 0, 0, 0 }
3b22753a
L
5621};
5622
70256ad8
AJ
5623#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
5624#define TARGET_LITTLE_NAME "elf64-x86-64"
5625#define ELF_ARCH bfd_arch_i386
ae95ffa6 5626#define ELF_TARGET_ID X86_64_ELF_DATA
70256ad8 5627#define ELF_MACHINE_CODE EM_X86_64
f7661549 5628#define ELF_MAXPAGESIZE 0x200000
2043964e 5629#define ELF_MINPAGESIZE 0x1000
24718e3b 5630#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
5631
5632#define elf_backend_can_gc_sections 1
51b64d56 5633#define elf_backend_can_refcount 1
70256ad8
AJ
5634#define elf_backend_want_got_plt 1
5635#define elf_backend_plt_readonly 1
5636#define elf_backend_want_plt_sym 0
5637#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 5638#define elf_backend_rela_normal 1
e41b3a13 5639#define elf_backend_plt_alignment 4
70256ad8 5640
351f65ca 5641#define elf_info_to_howto elf_x86_64_info_to_howto
70256ad8 5642
70256ad8 5643#define bfd_elf64_bfd_link_hash_table_create \
351f65ca 5644 elf_x86_64_link_hash_table_create
c25bc9fc 5645#define bfd_elf64_bfd_link_hash_table_free \
351f65ca
L
5646 elf_x86_64_link_hash_table_free
5647#define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
157090f7 5648#define bfd_elf64_bfd_reloc_name_lookup \
351f65ca 5649 elf_x86_64_reloc_name_lookup
70256ad8 5650
351f65ca 5651#define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
c543bf9a 5652#define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
351f65ca
L
5653#define elf_backend_check_relocs elf_x86_64_check_relocs
5654#define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
5655#define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
5656#define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5657#define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
5658#define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
5659#define elf_backend_gc_sweep_hook elf_x86_64_gc_sweep_hook
5660#define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5661#define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
8fd79e71
L
5662#ifdef CORE_HEADER
5663#define elf_backend_write_core_note elf_x86_64_write_core_note
5664#endif
351f65ca
L
5665#define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5666#define elf_backend_relocate_section elf_x86_64_relocate_section
5667#define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
5668#define elf_backend_always_size_sections elf_x86_64_always_size_sections
74541ad4 5669#define elf_backend_init_index_section _bfd_elf_init_1_index_section
351f65ca 5670#define elf_backend_plt_sym_val elf_x86_64_plt_sym_val
407443a3 5671#define elf_backend_object_p elf64_x86_64_elf_object_p
351f65ca 5672#define bfd_elf64_mkobject elf_x86_64_mkobject
0ff2b86e 5673#define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
8d88c4ca 5674
d2b2c203 5675#define elf_backend_section_from_shdr \
351f65ca 5676 elf_x86_64_section_from_shdr
d2b2c203 5677
3b22753a 5678#define elf_backend_section_from_bfd_section \
351f65ca 5679 elf_x86_64_elf_section_from_bfd_section
3b22753a 5680#define elf_backend_add_symbol_hook \
351f65ca 5681 elf_x86_64_add_symbol_hook
3b22753a 5682#define elf_backend_symbol_processing \
351f65ca 5683 elf_x86_64_symbol_processing
3b22753a 5684#define elf_backend_common_section_index \
351f65ca 5685 elf_x86_64_common_section_index
3b22753a 5686#define elf_backend_common_section \
351f65ca 5687 elf_x86_64_common_section
3b22753a 5688#define elf_backend_common_definition \
351f65ca 5689 elf_x86_64_common_definition
3b22753a 5690#define elf_backend_merge_symbol \
351f65ca 5691 elf_x86_64_merge_symbol
3b22753a 5692#define elf_backend_special_sections \
351f65ca 5693 elf_x86_64_special_sections
3b22753a 5694#define elf_backend_additional_program_headers \
351f65ca 5695 elf_x86_64_additional_program_headers
fdc90cb4 5696#define elf_backend_hash_symbol \
351f65ca 5697 elf_x86_64_hash_symbol
3b22753a 5698
8d88c4ca 5699#include "elf64-target.h"
9d7cbccd
NC
5700
5701/* FreeBSD support. */
5702
5703#undef TARGET_LITTLE_SYM
5704#define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
5705#undef TARGET_LITTLE_NAME
5706#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5707
d1036acb
L
5708#undef ELF_OSABI
5709#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd 5710
9d7cbccd
NC
5711#undef elf64_bed
5712#define elf64_bed elf64_x86_64_fbsd_bed
5713
5714#include "elf64-target.h"
8a9036a4 5715
a6cc6b3b
RO
5716/* Solaris 2 support. */
5717
5718#undef TARGET_LITTLE_SYM
5719#define TARGET_LITTLE_SYM bfd_elf64_x86_64_sol2_vec
5720#undef TARGET_LITTLE_NAME
5721#define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5722
5723/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5724 objects won't be recognized. */
5725#undef ELF_OSABI
5726
5727#undef elf64_bed
5728#define elf64_bed elf64_x86_64_sol2_bed
5729
7dc98aea
RO
5730/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5731 boundary. */
5732#undef elf_backend_static_tls_alignment
5733#define elf_backend_static_tls_alignment 16
5734
a6cc6b3b
RO
5735/* The Solaris 2 ABI requires a plt symbol on all platforms.
5736
5737 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5738 File, p.63. */
5739#undef elf_backend_want_plt_sym
5740#define elf_backend_want_plt_sym 1
5741
5742#include "elf64-target.h"
5743
0ff2b86e
L
5744#undef bfd_elf64_get_synthetic_symtab
5745
8059fb19
RM
5746/* Native Client support. */
5747
64b384e1
RM
5748static bfd_boolean
5749elf64_x86_64_nacl_elf_object_p (bfd *abfd)
5750{
5751 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
5752 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
5753 return TRUE;
5754}
5755
8059fb19
RM
5756#undef TARGET_LITTLE_SYM
5757#define TARGET_LITTLE_SYM bfd_elf64_x86_64_nacl_vec
5758#undef TARGET_LITTLE_NAME
5759#define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
5760#undef elf64_bed
5761#define elf64_bed elf64_x86_64_nacl_bed
5762
5763#undef ELF_MAXPAGESIZE
5764#undef ELF_MINPAGESIZE
5765#undef ELF_COMMONPAGESIZE
5766#define ELF_MAXPAGESIZE 0x10000
5767#define ELF_MINPAGESIZE 0x10000
5768#define ELF_COMMONPAGESIZE 0x10000
5769
5770/* Restore defaults. */
5771#undef ELF_OSABI
5772#undef elf_backend_static_tls_alignment
5773#undef elf_backend_want_plt_sym
5774#define elf_backend_want_plt_sym 0
5775
5776/* NaCl uses substantially different PLT entries for the same effects. */
5777
5778#undef elf_backend_plt_alignment
5779#define elf_backend_plt_alignment 5
5780#define NACL_PLT_ENTRY_SIZE 64
5781#define NACLMASK 0xe0 /* 32-byte alignment mask. */
5782
5783static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
5784 {
5785 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
5786 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
5787 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5788 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5789 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5790
ea2d813e 5791 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
70cc877f 5792 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
ea2d813e
RM
5793
5794 /* 32 bytes of nop to pad out to the standard size. */
8059fb19
RM
5795 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5796 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5797 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5798 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
ea2d813e
RM
5799 0x66, /* excess data32 prefix */
5800 0x90 /* nop */
8059fb19
RM
5801 };
5802
5803static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5804 {
5805 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
5806 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5807 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5808 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5809
5810 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
5811 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5812 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5813
5814 /* Lazy GOT entries point here (32-byte aligned). */
5815 0x68, /* pushq immediate */
5816 0, 0, 0, 0, /* replaced with index into relocation table. */
5817 0xe9, /* jmp relative */
5818 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
5819
5820 /* 22 bytes of nop to pad out to the standard size. */
5821 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5822 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5823 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
5824 };
5825
5826/* .eh_frame covering the .plt section. */
5827
5828static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
5829 {
5830#if (PLT_CIE_LENGTH != 20 \
5831 || PLT_FDE_LENGTH != 36 \
5832 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
5833 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
5834# error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
5835#endif
5836 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5837 0, 0, 0, 0, /* CIE ID */
5838 1, /* CIE version */
5839 'z', 'R', 0, /* Augmentation string */
5840 1, /* Code alignment factor */
5841 0x78, /* Data alignment factor */
5842 16, /* Return address column */
5843 1, /* Augmentation size */
5844 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5845 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
5846 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
5847 DW_CFA_nop, DW_CFA_nop,
5848
5849 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5850 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
5851 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
5852 0, 0, 0, 0, /* .plt size goes here */
5853 0, /* Augmentation size */
5854 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
5855 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5856 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
5857 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5858 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5859 13, /* Block length */
5860 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
5861 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
5862 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5863 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
5864 DW_CFA_nop, DW_CFA_nop
5865 };
5866
5867static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
5868 {
5869 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
5870 elf_x86_64_nacl_plt_entry, /* plt_entry */
5871 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5872 2, /* plt0_got1_offset */
5873 9, /* plt0_got2_offset */
5874 13, /* plt0_got2_insn_end */
5875 3, /* plt_got_offset */
5876 33, /* plt_reloc_offset */
5877 38, /* plt_plt_offset */
5878 7, /* plt_got_insn_size */
5879 42, /* plt_plt_insn_end */
5880 32, /* plt_lazy_offset */
5881 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
5882 sizeof (elf_x86_64_nacl_eh_frame_plt), /* eh_frame_plt_size */
5883 };
5884
5885#undef elf_backend_arch_data
5886#define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
5887
64b384e1
RM
5888#undef elf_backend_object_p
5889#define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
5a68afcf
RM
5890#undef elf_backend_modify_segment_map
5891#define elf_backend_modify_segment_map nacl_modify_segment_map
5892#undef elf_backend_modify_program_headers
5893#define elf_backend_modify_program_headers nacl_modify_program_headers
887badb3
RM
5894#undef elf_backend_final_write_processing
5895#define elf_backend_final_write_processing nacl_final_write_processing
5a68afcf 5896
8059fb19
RM
5897#include "elf64-target.h"
5898
5899/* Native Client x32 support. */
5900
64b384e1
RM
5901static bfd_boolean
5902elf32_x86_64_nacl_elf_object_p (bfd *abfd)
5903{
5904 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
5905 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
5906 return TRUE;
5907}
5908
8059fb19
RM
5909#undef TARGET_LITTLE_SYM
5910#define TARGET_LITTLE_SYM bfd_elf32_x86_64_nacl_vec
5911#undef TARGET_LITTLE_NAME
5912#define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
5913#undef elf32_bed
5914#define elf32_bed elf32_x86_64_nacl_bed
5915
5916#define bfd_elf32_bfd_link_hash_table_create \
5917 elf_x86_64_link_hash_table_create
5918#define bfd_elf32_bfd_link_hash_table_free \
5919 elf_x86_64_link_hash_table_free
5920#define bfd_elf32_bfd_reloc_type_lookup \
5921 elf_x86_64_reloc_type_lookup
5922#define bfd_elf32_bfd_reloc_name_lookup \
5923 elf_x86_64_reloc_name_lookup
5924#define bfd_elf32_mkobject \
5925 elf_x86_64_mkobject
5926
5927#undef elf_backend_object_p
5928#define elf_backend_object_p \
64b384e1 5929 elf32_x86_64_nacl_elf_object_p
8059fb19
RM
5930
5931#undef elf_backend_bfd_from_remote_memory
5932#define elf_backend_bfd_from_remote_memory \
5933 _bfd_elf32_bfd_from_remote_memory
5934
5935#undef elf_backend_size_info
5936#define elf_backend_size_info \
5937 _bfd_elf32_size_info
5938
5939#include "elf32-target.h"
5940
5941/* Restore defaults. */
5a68afcf 5942#undef elf_backend_object_p
8059fb19 5943#define elf_backend_object_p elf64_x86_64_elf_object_p
5a68afcf
RM
5944#undef elf_backend_bfd_from_remote_memory
5945#undef elf_backend_size_info
5946#undef elf_backend_modify_segment_map
5947#undef elf_backend_modify_program_headers
887badb3 5948#undef elf_backend_final_write_processing
8059fb19 5949
8a9036a4
L
5950/* Intel L1OM support. */
5951
5952static bfd_boolean
5953elf64_l1om_elf_object_p (bfd *abfd)
5954{
5955 /* Set the right machine number for an L1OM elf64 file. */
5956 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
5957 return TRUE;
5958}
5959
5960#undef TARGET_LITTLE_SYM
5961#define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
5962#undef TARGET_LITTLE_NAME
5963#define TARGET_LITTLE_NAME "elf64-l1om"
5964#undef ELF_ARCH
5965#define ELF_ARCH bfd_arch_l1om
5966
5967#undef ELF_MACHINE_CODE
5968#define ELF_MACHINE_CODE EM_L1OM
5969
5970#undef ELF_OSABI
5971
5972#undef elf64_bed
5973#define elf64_bed elf64_l1om_bed
5974
5975#undef elf_backend_object_p
5976#define elf_backend_object_p elf64_l1om_elf_object_p
5977
8059fb19
RM
5978/* Restore defaults. */
5979#undef ELF_MAXPAGESIZE
5980#undef ELF_MINPAGESIZE
5981#undef ELF_COMMONPAGESIZE
5982#define ELF_MAXPAGESIZE 0x200000
5983#define ELF_MINPAGESIZE 0x1000
5984#define ELF_COMMONPAGESIZE 0x1000
5985#undef elf_backend_plt_alignment
5986#define elf_backend_plt_alignment 4
5987#undef elf_backend_arch_data
5988#define elf_backend_arch_data &elf_x86_64_arch_bed
1a0c107f 5989
8a9036a4
L
5990#include "elf64-target.h"
5991
5992/* FreeBSD L1OM support. */
5993
5994#undef TARGET_LITTLE_SYM
5995#define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
5996#undef TARGET_LITTLE_NAME
5997#define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
5998
5999#undef ELF_OSABI
6000#define ELF_OSABI ELFOSABI_FREEBSD
6001
6002#undef elf64_bed
6003#define elf64_bed elf64_l1om_fbsd_bed
6004
8a9036a4 6005#include "elf64-target.h"
351f65ca 6006
7a9068fe
L
6007/* Intel K1OM support. */
6008
6009static bfd_boolean
6010elf64_k1om_elf_object_p (bfd *abfd)
6011{
6012 /* Set the right machine number for an K1OM elf64 file. */
6013 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
6014 return TRUE;
6015}
6016
6017#undef TARGET_LITTLE_SYM
6018#define TARGET_LITTLE_SYM bfd_elf64_k1om_vec
6019#undef TARGET_LITTLE_NAME
6020#define TARGET_LITTLE_NAME "elf64-k1om"
6021#undef ELF_ARCH
6022#define ELF_ARCH bfd_arch_k1om
6023
6024#undef ELF_MACHINE_CODE
6025#define ELF_MACHINE_CODE EM_K1OM
6026
6027#undef ELF_OSABI
6028
6029#undef elf64_bed
6030#define elf64_bed elf64_k1om_bed
6031
6032#undef elf_backend_object_p
6033#define elf_backend_object_p elf64_k1om_elf_object_p
6034
6035#undef elf_backend_static_tls_alignment
6036
6037#undef elf_backend_want_plt_sym
6038#define elf_backend_want_plt_sym 0
6039
6040#include "elf64-target.h"
6041
6042/* FreeBSD K1OM support. */
6043
6044#undef TARGET_LITTLE_SYM
6045#define TARGET_LITTLE_SYM bfd_elf64_k1om_freebsd_vec
6046#undef TARGET_LITTLE_NAME
6047#define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
6048
6049#undef ELF_OSABI
6050#define ELF_OSABI ELFOSABI_FREEBSD
6051
6052#undef elf64_bed
6053#define elf64_bed elf64_k1om_fbsd_bed
6054
6055#include "elf64-target.h"
6056
351f65ca
L
6057/* 32bit x86-64 support. */
6058
351f65ca
L
6059#undef TARGET_LITTLE_SYM
6060#define TARGET_LITTLE_SYM bfd_elf32_x86_64_vec
6061#undef TARGET_LITTLE_NAME
6062#define TARGET_LITTLE_NAME "elf32-x86-64"
8059fb19 6063#undef elf32_bed
351f65ca
L
6064
6065#undef ELF_ARCH
6066#define ELF_ARCH bfd_arch_i386
6067
6068#undef ELF_MACHINE_CODE
6069#define ELF_MACHINE_CODE EM_X86_64
6070
351f65ca
L
6071#undef ELF_OSABI
6072
351f65ca
L
6073#undef elf_backend_object_p
6074#define elf_backend_object_p \
6075 elf32_x86_64_elf_object_p
6076
6077#undef elf_backend_bfd_from_remote_memory
6078#define elf_backend_bfd_from_remote_memory \
6079 _bfd_elf32_bfd_from_remote_memory
6080
6081#undef elf_backend_size_info
6082#define elf_backend_size_info \
6083 _bfd_elf32_size_info
6084
6085#include "elf32-target.h"
This page took 1.171837 seconds and 4 git commands to generate.