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