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