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