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