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