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