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