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