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