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