1 /* X86-64 specific support for ELF
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 #include "bfd_stdint.h"
32 #include "libiberty.h"
34 #include "opcode/i386.h"
35 #include "elf/x86-64.h"
42 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
43 #define MINUS_ONE (~ (bfd_vma) 0)
45 /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
46 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
47 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
48 since they are the same. */
50 #define ABI_64_P(abfd) \
51 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
53 /* The relocation "howto" table. Order of fields:
54 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
55 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
56 static reloc_howto_type x86_64_elf_howto_table
[] =
58 HOWTO(R_X86_64_NONE
, 0, 3, 0, FALSE
, 0, complain_overflow_dont
,
59 bfd_elf_generic_reloc
, "R_X86_64_NONE", FALSE
, 0x00000000, 0x00000000,
61 HOWTO(R_X86_64_64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
62 bfd_elf_generic_reloc
, "R_X86_64_64", FALSE
, MINUS_ONE
, MINUS_ONE
,
64 HOWTO(R_X86_64_PC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
65 bfd_elf_generic_reloc
, "R_X86_64_PC32", FALSE
, 0xffffffff, 0xffffffff,
67 HOWTO(R_X86_64_GOT32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
68 bfd_elf_generic_reloc
, "R_X86_64_GOT32", FALSE
, 0xffffffff, 0xffffffff,
70 HOWTO(R_X86_64_PLT32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
71 bfd_elf_generic_reloc
, "R_X86_64_PLT32", FALSE
, 0xffffffff, 0xffffffff,
73 HOWTO(R_X86_64_COPY
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
74 bfd_elf_generic_reloc
, "R_X86_64_COPY", FALSE
, 0xffffffff, 0xffffffff,
76 HOWTO(R_X86_64_GLOB_DAT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
77 bfd_elf_generic_reloc
, "R_X86_64_GLOB_DAT", FALSE
, MINUS_ONE
,
79 HOWTO(R_X86_64_JUMP_SLOT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
80 bfd_elf_generic_reloc
, "R_X86_64_JUMP_SLOT", FALSE
, MINUS_ONE
,
82 HOWTO(R_X86_64_RELATIVE
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
83 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE", FALSE
, MINUS_ONE
,
85 HOWTO(R_X86_64_GOTPCREL
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
86 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL", FALSE
, 0xffffffff,
88 HOWTO(R_X86_64_32
, 0, 2, 32, FALSE
, 0, complain_overflow_unsigned
,
89 bfd_elf_generic_reloc
, "R_X86_64_32", FALSE
, 0xffffffff, 0xffffffff,
91 HOWTO(R_X86_64_32S
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
92 bfd_elf_generic_reloc
, "R_X86_64_32S", FALSE
, 0xffffffff, 0xffffffff,
94 HOWTO(R_X86_64_16
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
,
95 bfd_elf_generic_reloc
, "R_X86_64_16", FALSE
, 0xffff, 0xffff, FALSE
),
96 HOWTO(R_X86_64_PC16
,0, 1, 16, TRUE
, 0, complain_overflow_bitfield
,
97 bfd_elf_generic_reloc
, "R_X86_64_PC16", FALSE
, 0xffff, 0xffff, TRUE
),
98 HOWTO(R_X86_64_8
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
,
99 bfd_elf_generic_reloc
, "R_X86_64_8", FALSE
, 0xff, 0xff, FALSE
),
100 HOWTO(R_X86_64_PC8
, 0, 0, 8, TRUE
, 0, complain_overflow_signed
,
101 bfd_elf_generic_reloc
, "R_X86_64_PC8", FALSE
, 0xff, 0xff, TRUE
),
102 HOWTO(R_X86_64_DTPMOD64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
103 bfd_elf_generic_reloc
, "R_X86_64_DTPMOD64", FALSE
, MINUS_ONE
,
105 HOWTO(R_X86_64_DTPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
106 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF64", FALSE
, MINUS_ONE
,
108 HOWTO(R_X86_64_TPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
109 bfd_elf_generic_reloc
, "R_X86_64_TPOFF64", FALSE
, MINUS_ONE
,
111 HOWTO(R_X86_64_TLSGD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
112 bfd_elf_generic_reloc
, "R_X86_64_TLSGD", FALSE
, 0xffffffff,
114 HOWTO(R_X86_64_TLSLD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
115 bfd_elf_generic_reloc
, "R_X86_64_TLSLD", FALSE
, 0xffffffff,
117 HOWTO(R_X86_64_DTPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
118 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF32", FALSE
, 0xffffffff,
120 HOWTO(R_X86_64_GOTTPOFF
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
121 bfd_elf_generic_reloc
, "R_X86_64_GOTTPOFF", FALSE
, 0xffffffff,
123 HOWTO(R_X86_64_TPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
124 bfd_elf_generic_reloc
, "R_X86_64_TPOFF32", FALSE
, 0xffffffff,
126 HOWTO(R_X86_64_PC64
, 0, 4, 64, TRUE
, 0, complain_overflow_bitfield
,
127 bfd_elf_generic_reloc
, "R_X86_64_PC64", FALSE
, MINUS_ONE
, MINUS_ONE
,
129 HOWTO(R_X86_64_GOTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
130 bfd_elf_generic_reloc
, "R_X86_64_GOTOFF64",
131 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
132 HOWTO(R_X86_64_GOTPC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
133 bfd_elf_generic_reloc
, "R_X86_64_GOTPC32",
134 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
135 HOWTO(R_X86_64_GOT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
136 bfd_elf_generic_reloc
, "R_X86_64_GOT64", FALSE
, MINUS_ONE
, MINUS_ONE
,
138 HOWTO(R_X86_64_GOTPCREL64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
139 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL64", FALSE
, MINUS_ONE
,
141 HOWTO(R_X86_64_GOTPC64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
142 bfd_elf_generic_reloc
, "R_X86_64_GOTPC64",
143 FALSE
, MINUS_ONE
, MINUS_ONE
, TRUE
),
144 HOWTO(R_X86_64_GOTPLT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
145 bfd_elf_generic_reloc
, "R_X86_64_GOTPLT64", FALSE
, MINUS_ONE
,
147 HOWTO(R_X86_64_PLTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
148 bfd_elf_generic_reloc
, "R_X86_64_PLTOFF64", FALSE
, MINUS_ONE
,
150 HOWTO(R_X86_64_SIZE32
, 0, 2, 32, FALSE
, 0, complain_overflow_unsigned
,
151 bfd_elf_generic_reloc
, "R_X86_64_SIZE32", FALSE
, 0xffffffff, 0xffffffff,
153 HOWTO(R_X86_64_SIZE64
, 0, 4, 64, FALSE
, 0, complain_overflow_unsigned
,
154 bfd_elf_generic_reloc
, "R_X86_64_SIZE64", FALSE
, MINUS_ONE
, MINUS_ONE
,
156 HOWTO(R_X86_64_GOTPC32_TLSDESC
, 0, 2, 32, TRUE
, 0,
157 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
158 "R_X86_64_GOTPC32_TLSDESC",
159 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
160 HOWTO(R_X86_64_TLSDESC_CALL
, 0, 0, 0, FALSE
, 0,
161 complain_overflow_dont
, bfd_elf_generic_reloc
,
162 "R_X86_64_TLSDESC_CALL",
164 HOWTO(R_X86_64_TLSDESC
, 0, 4, 64, FALSE
, 0,
165 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
167 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
168 HOWTO(R_X86_64_IRELATIVE
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
169 bfd_elf_generic_reloc
, "R_X86_64_IRELATIVE", FALSE
, MINUS_ONE
,
171 HOWTO(R_X86_64_RELATIVE64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
172 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE64", FALSE
, MINUS_ONE
,
174 HOWTO(R_X86_64_PC32_BND
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
175 bfd_elf_generic_reloc
, "R_X86_64_PC32_BND", FALSE
, 0xffffffff, 0xffffffff,
177 HOWTO(R_X86_64_PLT32_BND
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
178 bfd_elf_generic_reloc
, "R_X86_64_PLT32_BND", FALSE
, 0xffffffff, 0xffffffff,
180 HOWTO(R_X86_64_GOTPCRELX
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
181 bfd_elf_generic_reloc
, "R_X86_64_GOTPCRELX", FALSE
, 0xffffffff,
183 HOWTO(R_X86_64_REX_GOTPCRELX
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
184 bfd_elf_generic_reloc
, "R_X86_64_REX_GOTPCRELX", FALSE
, 0xffffffff,
187 /* We have a gap in the reloc numbers here.
188 R_X86_64_standard counts the number up to this point, and
189 R_X86_64_vt_offset is the value to subtract from a reloc type of
190 R_X86_64_GNU_VT* to form an index into this table. */
191 #define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
192 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
194 /* GNU extension to record C++ vtable hierarchy. */
195 HOWTO (R_X86_64_GNU_VTINHERIT
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
196 NULL
, "R_X86_64_GNU_VTINHERIT", FALSE
, 0, 0, FALSE
),
198 /* GNU extension to record C++ vtable member usage. */
199 HOWTO (R_X86_64_GNU_VTENTRY
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
200 _bfd_elf_rel_vtable_reloc_fn
, "R_X86_64_GNU_VTENTRY", FALSE
, 0, 0,
203 /* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
204 HOWTO(R_X86_64_32
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
205 bfd_elf_generic_reloc
, "R_X86_64_32", FALSE
, 0xffffffff, 0xffffffff,
209 #define IS_X86_64_PCREL_TYPE(TYPE) \
210 ( ((TYPE) == R_X86_64_PC8) \
211 || ((TYPE) == R_X86_64_PC16) \
212 || ((TYPE) == R_X86_64_PC32) \
213 || ((TYPE) == R_X86_64_PC32_BND) \
214 || ((TYPE) == R_X86_64_PC64))
216 /* Map BFD relocs to the x86_64 elf relocs. */
219 bfd_reloc_code_real_type bfd_reloc_val
;
220 unsigned char elf_reloc_val
;
223 static const struct elf_reloc_map x86_64_reloc_map
[] =
225 { BFD_RELOC_NONE
, R_X86_64_NONE
, },
226 { BFD_RELOC_64
, R_X86_64_64
, },
227 { BFD_RELOC_32_PCREL
, R_X86_64_PC32
, },
228 { BFD_RELOC_X86_64_GOT32
, R_X86_64_GOT32
,},
229 { BFD_RELOC_X86_64_PLT32
, R_X86_64_PLT32
,},
230 { BFD_RELOC_X86_64_COPY
, R_X86_64_COPY
, },
231 { BFD_RELOC_X86_64_GLOB_DAT
, R_X86_64_GLOB_DAT
, },
232 { BFD_RELOC_X86_64_JUMP_SLOT
, R_X86_64_JUMP_SLOT
, },
233 { BFD_RELOC_X86_64_RELATIVE
, R_X86_64_RELATIVE
, },
234 { BFD_RELOC_X86_64_GOTPCREL
, R_X86_64_GOTPCREL
, },
235 { BFD_RELOC_32
, R_X86_64_32
, },
236 { BFD_RELOC_X86_64_32S
, R_X86_64_32S
, },
237 { BFD_RELOC_16
, R_X86_64_16
, },
238 { BFD_RELOC_16_PCREL
, R_X86_64_PC16
, },
239 { BFD_RELOC_8
, R_X86_64_8
, },
240 { BFD_RELOC_8_PCREL
, R_X86_64_PC8
, },
241 { BFD_RELOC_X86_64_DTPMOD64
, R_X86_64_DTPMOD64
, },
242 { BFD_RELOC_X86_64_DTPOFF64
, R_X86_64_DTPOFF64
, },
243 { BFD_RELOC_X86_64_TPOFF64
, R_X86_64_TPOFF64
, },
244 { BFD_RELOC_X86_64_TLSGD
, R_X86_64_TLSGD
, },
245 { BFD_RELOC_X86_64_TLSLD
, R_X86_64_TLSLD
, },
246 { BFD_RELOC_X86_64_DTPOFF32
, R_X86_64_DTPOFF32
, },
247 { BFD_RELOC_X86_64_GOTTPOFF
, R_X86_64_GOTTPOFF
, },
248 { BFD_RELOC_X86_64_TPOFF32
, R_X86_64_TPOFF32
, },
249 { BFD_RELOC_64_PCREL
, R_X86_64_PC64
, },
250 { BFD_RELOC_X86_64_GOTOFF64
, R_X86_64_GOTOFF64
, },
251 { BFD_RELOC_X86_64_GOTPC32
, R_X86_64_GOTPC32
, },
252 { BFD_RELOC_X86_64_GOT64
, R_X86_64_GOT64
, },
253 { BFD_RELOC_X86_64_GOTPCREL64
,R_X86_64_GOTPCREL64
, },
254 { BFD_RELOC_X86_64_GOTPC64
, R_X86_64_GOTPC64
, },
255 { BFD_RELOC_X86_64_GOTPLT64
, R_X86_64_GOTPLT64
, },
256 { BFD_RELOC_X86_64_PLTOFF64
, R_X86_64_PLTOFF64
, },
257 { BFD_RELOC_SIZE32
, R_X86_64_SIZE32
, },
258 { BFD_RELOC_SIZE64
, R_X86_64_SIZE64
, },
259 { BFD_RELOC_X86_64_GOTPC32_TLSDESC
, R_X86_64_GOTPC32_TLSDESC
, },
260 { BFD_RELOC_X86_64_TLSDESC_CALL
, R_X86_64_TLSDESC_CALL
, },
261 { BFD_RELOC_X86_64_TLSDESC
, R_X86_64_TLSDESC
, },
262 { BFD_RELOC_X86_64_IRELATIVE
, R_X86_64_IRELATIVE
, },
263 { BFD_RELOC_X86_64_PC32_BND
, R_X86_64_PC32_BND
, },
264 { BFD_RELOC_X86_64_PLT32_BND
, R_X86_64_PLT32_BND
, },
265 { BFD_RELOC_X86_64_GOTPCRELX
, R_X86_64_GOTPCRELX
, },
266 { BFD_RELOC_X86_64_REX_GOTPCRELX
, R_X86_64_REX_GOTPCRELX
, },
267 { BFD_RELOC_VTABLE_INHERIT
, R_X86_64_GNU_VTINHERIT
, },
268 { BFD_RELOC_VTABLE_ENTRY
, R_X86_64_GNU_VTENTRY
, },
271 static reloc_howto_type
*
272 elf_x86_64_rtype_to_howto (bfd
*abfd
, unsigned r_type
)
276 if (r_type
== (unsigned int) R_X86_64_32
)
281 i
= ARRAY_SIZE (x86_64_elf_howto_table
) - 1;
283 else if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
284 || r_type
>= (unsigned int) R_X86_64_max
)
286 if (r_type
>= (unsigned int) R_X86_64_standard
)
288 /* xgettext:c-format */
289 _bfd_error_handler (_("%B: invalid relocation type %d"),
291 r_type
= R_X86_64_NONE
;
296 i
= r_type
- (unsigned int) R_X86_64_vt_offset
;
297 BFD_ASSERT (x86_64_elf_howto_table
[i
].type
== r_type
);
298 return &x86_64_elf_howto_table
[i
];
301 /* Given a BFD reloc type, return a HOWTO structure. */
302 static reloc_howto_type
*
303 elf_x86_64_reloc_type_lookup (bfd
*abfd
,
304 bfd_reloc_code_real_type code
)
308 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
311 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
312 return elf_x86_64_rtype_to_howto (abfd
,
313 x86_64_reloc_map
[i
].elf_reloc_val
);
318 static reloc_howto_type
*
319 elf_x86_64_reloc_name_lookup (bfd
*abfd
,
324 if (!ABI_64_P (abfd
) && strcasecmp (r_name
, "R_X86_64_32") == 0)
326 /* Get x32 R_X86_64_32. */
327 reloc_howto_type
*reloc
328 = &x86_64_elf_howto_table
[ARRAY_SIZE (x86_64_elf_howto_table
) - 1];
329 BFD_ASSERT (reloc
->type
== (unsigned int) R_X86_64_32
);
333 for (i
= 0; i
< ARRAY_SIZE (x86_64_elf_howto_table
); i
++)
334 if (x86_64_elf_howto_table
[i
].name
!= NULL
335 && strcasecmp (x86_64_elf_howto_table
[i
].name
, r_name
) == 0)
336 return &x86_64_elf_howto_table
[i
];
341 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
344 elf_x86_64_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
345 Elf_Internal_Rela
*dst
)
349 r_type
= ELF32_R_TYPE (dst
->r_info
);
350 cache_ptr
->howto
= elf_x86_64_rtype_to_howto (abfd
, r_type
);
351 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
);
354 /* Support for core dump NOTE sections. */
356 elf_x86_64_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
361 switch (note
->descsz
)
366 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
368 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
371 elf_tdata (abfd
)->core
->lwpid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
379 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
381 elf_tdata (abfd
)->core
->signal
382 = bfd_get_16 (abfd
, note
->descdata
+ 12);
385 elf_tdata (abfd
)->core
->lwpid
386 = bfd_get_32 (abfd
, note
->descdata
+ 32);
395 /* Make a ".reg/999" section. */
396 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
397 size
, note
->descpos
+ offset
);
401 elf_x86_64_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
403 switch (note
->descsz
)
408 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
409 elf_tdata (abfd
)->core
->pid
410 = bfd_get_32 (abfd
, note
->descdata
+ 12);
411 elf_tdata (abfd
)->core
->program
412 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
413 elf_tdata (abfd
)->core
->command
414 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
417 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
418 elf_tdata (abfd
)->core
->pid
419 = bfd_get_32 (abfd
, note
->descdata
+ 24);
420 elf_tdata (abfd
)->core
->program
421 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
422 elf_tdata (abfd
)->core
->command
423 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
426 /* Note that for some reason, a spurious space is tacked
427 onto the end of the args in some (at least one anyway)
428 implementations, so strip it off if it exists. */
431 char *command
= elf_tdata (abfd
)->core
->command
;
432 int n
= strlen (command
);
434 if (0 < n
&& command
[n
- 1] == ' ')
435 command
[n
- 1] = '\0';
443 elf_x86_64_write_core_note (bfd
*abfd
, char *buf
, int *bufsiz
,
446 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
448 const char *fname
, *psargs
;
459 va_start (ap
, note_type
);
460 fname
= va_arg (ap
, const char *);
461 psargs
= va_arg (ap
, const char *);
464 if (bed
->s
->elfclass
== ELFCLASS32
)
467 memset (&data
, 0, sizeof (data
));
468 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
469 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
470 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
471 &data
, sizeof (data
));
476 memset (&data
, 0, sizeof (data
));
477 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
478 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
479 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
480 &data
, sizeof (data
));
485 va_start (ap
, note_type
);
486 pid
= va_arg (ap
, long);
487 cursig
= va_arg (ap
, int);
488 gregs
= va_arg (ap
, const void *);
491 if (bed
->s
->elfclass
== ELFCLASS32
)
493 if (bed
->elf_machine_code
== EM_X86_64
)
495 prstatusx32_t prstat
;
496 memset (&prstat
, 0, sizeof (prstat
));
498 prstat
.pr_cursig
= cursig
;
499 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
500 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
501 &prstat
, sizeof (prstat
));
506 memset (&prstat
, 0, sizeof (prstat
));
508 prstat
.pr_cursig
= cursig
;
509 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
510 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
511 &prstat
, sizeof (prstat
));
517 memset (&prstat
, 0, sizeof (prstat
));
519 prstat
.pr_cursig
= cursig
;
520 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
521 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
522 &prstat
, sizeof (prstat
));
529 /* Functions for the x86-64 ELF linker. */
531 /* The name of the dynamic interpreter. This is put in the .interp
534 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
535 #define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
537 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
538 copying dynamic variables from a shared lib into an app's dynbss
539 section, and instead use a dynamic relocation to point into the
541 #define ELIMINATE_COPY_RELOCS 1
543 /* The size in bytes of an entry in the global offset table. */
545 #define GOT_ENTRY_SIZE 8
547 /* The size in bytes of an entry in the procedure linkage table. */
549 #define PLT_ENTRY_SIZE 16
551 /* The first entry in a procedure linkage table looks like this. See the
552 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
554 static const bfd_byte elf_x86_64_plt0_entry
[PLT_ENTRY_SIZE
] =
556 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
557 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
558 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
561 /* Subsequent entries in a procedure linkage table look like this. */
563 static const bfd_byte elf_x86_64_plt_entry
[PLT_ENTRY_SIZE
] =
565 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
566 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
567 0x68, /* pushq immediate */
568 0, 0, 0, 0, /* replaced with index into relocation table. */
569 0xe9, /* jmp relative */
570 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
573 /* The first entry in a procedure linkage table with BND relocations
576 static const bfd_byte elf_x86_64_bnd_plt0_entry
[PLT_ENTRY_SIZE
] =
578 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
579 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
580 0x0f, 0x1f, 0 /* nopl (%rax) */
583 /* Subsequent entries for legacy branches in a procedure linkage table
584 with BND relocations look like this. */
586 static const bfd_byte elf_x86_64_legacy_plt_entry
[PLT_ENTRY_SIZE
] =
588 0x68, 0, 0, 0, 0, /* pushq immediate */
589 0xe9, 0, 0, 0, 0, /* jmpq relative */
590 0x66, 0x0f, 0x1f, 0x44, 0, 0 /* nopw (%rax,%rax,1) */
593 /* Subsequent entries for branches with BND prefx in a procedure linkage
594 table with BND relocations look like this. */
596 static const bfd_byte elf_x86_64_bnd_plt_entry
[PLT_ENTRY_SIZE
] =
598 0x68, 0, 0, 0, 0, /* pushq immediate */
599 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
600 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
603 /* Entries for legacy branches in the second procedure linkage table
606 static const bfd_byte elf_x86_64_legacy_plt2_entry
[8] =
608 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
609 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
610 0x66, 0x90 /* xchg %ax,%ax */
613 /* Entries for branches with BND prefix in the second procedure linkage
614 table look like this. */
616 static const bfd_byte elf_x86_64_bnd_plt2_entry
[8] =
618 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
619 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
623 /* .eh_frame covering the .plt section. */
625 static const bfd_byte elf_x86_64_eh_frame_plt
[] =
627 #define PLT_CIE_LENGTH 20
628 #define PLT_FDE_LENGTH 36
629 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
630 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
631 PLT_CIE_LENGTH
, 0, 0, 0, /* CIE length */
632 0, 0, 0, 0, /* CIE ID */
634 'z', 'R', 0, /* Augmentation string */
635 1, /* Code alignment factor */
636 0x78, /* Data alignment factor */
637 16, /* Return address column */
638 1, /* Augmentation size */
639 DW_EH_PE_pcrel
| DW_EH_PE_sdata4
, /* FDE encoding */
640 DW_CFA_def_cfa
, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
641 DW_CFA_offset
+ 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
642 DW_CFA_nop
, DW_CFA_nop
,
644 PLT_FDE_LENGTH
, 0, 0, 0, /* FDE length */
645 PLT_CIE_LENGTH
+ 8, 0, 0, 0, /* CIE pointer */
646 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
647 0, 0, 0, 0, /* .plt size goes here */
648 0, /* Augmentation size */
649 DW_CFA_def_cfa_offset
, 16, /* DW_CFA_def_cfa_offset: 16 */
650 DW_CFA_advance_loc
+ 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
651 DW_CFA_def_cfa_offset
, 24, /* DW_CFA_def_cfa_offset: 24 */
652 DW_CFA_advance_loc
+ 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
653 DW_CFA_def_cfa_expression
, /* DW_CFA_def_cfa_expression */
654 11, /* Block length */
655 DW_OP_breg7
, 8, /* DW_OP_breg7 (rsp): 8 */
656 DW_OP_breg16
, 0, /* DW_OP_breg16 (rip): 0 */
657 DW_OP_lit15
, DW_OP_and
, DW_OP_lit11
, DW_OP_ge
,
658 DW_OP_lit3
, DW_OP_shl
, DW_OP_plus
,
659 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
662 /* .eh_frame covering the .plt.got section. */
664 static const bfd_byte elf_x86_64_eh_frame_plt_got
[] =
666 #define PLT_GOT_FDE_LENGTH 20
667 PLT_CIE_LENGTH
, 0, 0, 0, /* CIE length */
668 0, 0, 0, 0, /* CIE ID */
670 'z', 'R', 0, /* Augmentation string */
671 1, /* Code alignment factor */
672 0x78, /* Data alignment factor */
673 16, /* Return address column */
674 1, /* Augmentation size */
675 DW_EH_PE_pcrel
| DW_EH_PE_sdata4
, /* FDE encoding */
676 DW_CFA_def_cfa
, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
677 DW_CFA_offset
+ 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
678 DW_CFA_nop
, DW_CFA_nop
,
680 PLT_GOT_FDE_LENGTH
, 0, 0, 0, /* FDE length */
681 PLT_CIE_LENGTH
+ 8, 0, 0, 0, /* CIE pointer */
682 0, 0, 0, 0, /* the start of .plt.got goes here */
683 0, 0, 0, 0, /* .plt.got size goes here */
684 0, /* Augmentation size */
685 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
,
686 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
689 /* Architecture-specific backend data for x86-64. */
691 struct elf_x86_64_backend_data
693 /* Templates for the initial PLT entry and for subsequent entries. */
694 const bfd_byte
*plt0_entry
;
695 const bfd_byte
*plt_entry
;
696 unsigned int plt_entry_size
; /* Size of each PLT entry. */
698 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
699 unsigned int plt0_got1_offset
;
700 unsigned int plt0_got2_offset
;
702 /* Offset of the end of the PC-relative instruction containing
704 unsigned int plt0_got2_insn_end
;
706 /* Offsets into plt_entry that are to be replaced with... */
707 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
708 unsigned int plt_reloc_offset
; /* ... offset into relocation table. */
709 unsigned int plt_plt_offset
; /* ... offset to start of .plt. */
711 /* Length of the PC-relative instruction containing plt_got_offset. */
712 unsigned int plt_got_insn_size
;
714 /* Offset of the end of the PC-relative jump to plt0_entry. */
715 unsigned int plt_plt_insn_end
;
717 /* Offset into plt_entry where the initial value of the GOT entry points. */
718 unsigned int plt_lazy_offset
;
720 /* .eh_frame covering the .plt section. */
721 const bfd_byte
*eh_frame_plt
;
722 unsigned int eh_frame_plt_size
;
724 /* .eh_frame covering the .plt.got section. */
725 const bfd_byte
*eh_frame_plt_got
;
726 unsigned int eh_frame_plt_got_size
;
729 #define get_elf_x86_64_arch_data(bed) \
730 ((const struct elf_x86_64_backend_data *) (bed)->arch_data)
732 #define get_elf_x86_64_backend_data(abfd) \
733 get_elf_x86_64_arch_data (get_elf_backend_data (abfd))
735 #define GET_PLT_ENTRY_SIZE(abfd) \
736 get_elf_x86_64_backend_data (abfd)->plt_entry_size
738 /* These are the standard parameters. */
739 static const struct elf_x86_64_backend_data elf_x86_64_arch_bed
=
741 elf_x86_64_plt0_entry
, /* plt0_entry */
742 elf_x86_64_plt_entry
, /* plt_entry */
743 sizeof (elf_x86_64_plt_entry
), /* plt_entry_size */
744 2, /* plt0_got1_offset */
745 8, /* plt0_got2_offset */
746 12, /* plt0_got2_insn_end */
747 2, /* plt_got_offset */
748 7, /* plt_reloc_offset */
749 12, /* plt_plt_offset */
750 6, /* plt_got_insn_size */
751 PLT_ENTRY_SIZE
, /* plt_plt_insn_end */
752 6, /* plt_lazy_offset */
753 elf_x86_64_eh_frame_plt
, /* eh_frame_plt */
754 sizeof (elf_x86_64_eh_frame_plt
), /* eh_frame_plt_size */
755 elf_x86_64_eh_frame_plt_got
, /* eh_frame_plt_got */
756 sizeof (elf_x86_64_eh_frame_plt_got
), /* eh_frame_plt_got_size */
759 static const struct elf_x86_64_backend_data elf_x86_64_bnd_arch_bed
=
761 elf_x86_64_bnd_plt0_entry
, /* plt0_entry */
762 elf_x86_64_bnd_plt_entry
, /* plt_entry */
763 sizeof (elf_x86_64_bnd_plt_entry
), /* plt_entry_size */
764 2, /* plt0_got1_offset */
765 1+8, /* plt0_got2_offset */
766 1+12, /* plt0_got2_insn_end */
767 1+2, /* plt_got_offset */
768 1, /* plt_reloc_offset */
769 7, /* plt_plt_offset */
770 1+6, /* plt_got_insn_size */
771 11, /* plt_plt_insn_end */
772 0, /* plt_lazy_offset */
773 elf_x86_64_eh_frame_plt
, /* eh_frame_plt */
774 sizeof (elf_x86_64_eh_frame_plt
), /* eh_frame_plt_size */
775 /* FIXME: Needs .eh_frame coverage. */
776 NULL
, /* eh_frame_plt_got */
777 0, /* eh_frame_plt_got_size */
780 #define elf_backend_arch_data &elf_x86_64_arch_bed
782 /* Is a undefined weak symbol which is resolved to 0. Reference to an
783 undefined weak symbol is resolved to 0 when building executable if
785 1. Has non-GOT/non-PLT relocations in text section. Or
786 2. Has no GOT/PLT relocation.
788 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, GOT_RELOC, EH) \
789 ((EH)->elf.root.type == bfd_link_hash_undefweak \
790 && bfd_link_executable (INFO) \
791 && (elf_x86_64_hash_table (INFO)->interp == NULL \
793 || (EH)->has_non_got_reloc \
794 || !(INFO)->dynamic_undefined_weak))
796 /* x86-64 ELF linker hash entry. */
798 struct elf_x86_64_link_hash_entry
800 struct elf_link_hash_entry elf
;
802 /* Track dynamic relocs copied for this symbol. */
803 struct elf_dyn_relocs
*dyn_relocs
;
805 #define GOT_UNKNOWN 0
809 #define GOT_TLS_GDESC 4
810 #define GOT_TLS_GD_BOTH_P(type) \
811 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
812 #define GOT_TLS_GD_P(type) \
813 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
814 #define GOT_TLS_GDESC_P(type) \
815 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
816 #define GOT_TLS_GD_ANY_P(type) \
817 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
818 unsigned char tls_type
;
820 /* TRUE if a weak symbol with a real definition needs a copy reloc.
821 When there is a weak symbol with a real definition, the processor
822 independent code will have arranged for us to see the real
823 definition first. We need to copy the needs_copy bit from the
824 real definition and check it when allowing copy reloc in PIE. */
825 unsigned int needs_copy
: 1;
827 /* TRUE if symbol has at least one BND relocation. */
828 unsigned int has_bnd_reloc
: 1;
830 /* TRUE if symbol has GOT or PLT relocations. */
831 unsigned int has_got_reloc
: 1;
833 /* TRUE if symbol has non-GOT/non-PLT relocations in text sections. */
834 unsigned int has_non_got_reloc
: 1;
836 /* 0: symbol isn't __tls_get_addr.
837 1: symbol is __tls_get_addr.
838 2: symbol is unknown. */
839 unsigned int tls_get_addr
: 2;
841 /* Reference count of C/C++ function pointer relocations in read-write
842 section which can be resolved at run-time. */
843 bfd_signed_vma func_pointer_refcount
;
845 /* Information about the GOT PLT entry. Filled when there are both
846 GOT and PLT relocations against the same function. */
847 union gotplt_union plt_got
;
849 /* Information about the second PLT entry. Filled when has_bnd_reloc is
851 union gotplt_union plt_bnd
;
853 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
854 starting at the end of the jump table. */
858 #define elf_x86_64_hash_entry(ent) \
859 ((struct elf_x86_64_link_hash_entry *)(ent))
861 struct elf_x86_64_obj_tdata
863 struct elf_obj_tdata root
;
865 /* tls_type for each local got entry. */
866 char *local_got_tls_type
;
868 /* GOTPLT entries for TLS descriptors. */
869 bfd_vma
*local_tlsdesc_gotent
;
872 #define elf_x86_64_tdata(abfd) \
873 ((struct elf_x86_64_obj_tdata *) (abfd)->tdata.any)
875 #define elf_x86_64_local_got_tls_type(abfd) \
876 (elf_x86_64_tdata (abfd)->local_got_tls_type)
878 #define elf_x86_64_local_tlsdesc_gotent(abfd) \
879 (elf_x86_64_tdata (abfd)->local_tlsdesc_gotent)
881 #define is_x86_64_elf(bfd) \
882 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
883 && elf_tdata (bfd) != NULL \
884 && elf_object_id (bfd) == X86_64_ELF_DATA)
887 elf_x86_64_mkobject (bfd
*abfd
)
889 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_x86_64_obj_tdata
),
893 /* x86-64 ELF linker hash table. */
895 struct elf_x86_64_link_hash_table
897 struct elf_link_hash_table elf
;
899 /* Short-cuts to get to dynamic linker sections. */
901 asection
*plt_eh_frame
;
904 asection
*plt_got_eh_frame
;
908 bfd_signed_vma refcount
;
912 /* The amount of space used by the jump slots in the GOT. */
913 bfd_vma sgotplt_jump_table_size
;
915 /* Small local sym cache. */
916 struct sym_cache sym_cache
;
918 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
919 bfd_vma (*r_sym
) (bfd_vma
);
920 unsigned int pointer_r_type
;
921 const char *dynamic_interpreter
;
922 int dynamic_interpreter_size
;
924 /* _TLS_MODULE_BASE_ symbol. */
925 struct bfd_link_hash_entry
*tls_module_base
;
927 /* Used by local STT_GNU_IFUNC symbols. */
928 htab_t loc_hash_table
;
929 void * loc_hash_memory
;
931 /* The offset into splt of the PLT entry for the TLS descriptor
932 resolver. Special values are 0, if not necessary (or not found
933 to be necessary yet), and -1 if needed but not determined
936 /* The offset into sgot of the GOT entry used by the PLT entry
940 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
941 bfd_vma next_jump_slot_index
;
942 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
943 bfd_vma next_irelative_index
;
945 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
946 to read-only sections. */
947 bfd_boolean readonly_dynrelocs_against_ifunc
;
950 /* Get the x86-64 ELF linker hash table from a link_info structure. */
952 #define elf_x86_64_hash_table(p) \
953 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
954 == X86_64_ELF_DATA ? ((struct elf_x86_64_link_hash_table *) ((p)->hash)) : NULL)
956 #define elf_x86_64_compute_jump_table_size(htab) \
957 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
959 /* Create an entry in an x86-64 ELF linker hash table. */
961 static struct bfd_hash_entry
*
962 elf_x86_64_link_hash_newfunc (struct bfd_hash_entry
*entry
,
963 struct bfd_hash_table
*table
,
966 /* Allocate the structure if it has not already been allocated by a
970 entry
= (struct bfd_hash_entry
*)
971 bfd_hash_allocate (table
,
972 sizeof (struct elf_x86_64_link_hash_entry
));
977 /* Call the allocation method of the superclass. */
978 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
981 struct elf_x86_64_link_hash_entry
*eh
;
983 eh
= (struct elf_x86_64_link_hash_entry
*) entry
;
984 eh
->dyn_relocs
= NULL
;
985 eh
->tls_type
= GOT_UNKNOWN
;
987 eh
->has_bnd_reloc
= 0;
988 eh
->has_got_reloc
= 0;
989 eh
->has_non_got_reloc
= 0;
990 eh
->tls_get_addr
= 2;
991 eh
->func_pointer_refcount
= 0;
992 eh
->plt_bnd
.offset
= (bfd_vma
) -1;
993 eh
->plt_got
.offset
= (bfd_vma
) -1;
994 eh
->tlsdesc_got
= (bfd_vma
) -1;
1000 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
1001 for local symbol so that we can handle local STT_GNU_IFUNC symbols
1002 as global symbol. We reuse indx and dynstr_index for local symbol
1003 hash since they aren't used by global symbols in this backend. */
1006 elf_x86_64_local_htab_hash (const void *ptr
)
1008 struct elf_link_hash_entry
*h
1009 = (struct elf_link_hash_entry
*) ptr
;
1010 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
1013 /* Compare local hash entries. */
1016 elf_x86_64_local_htab_eq (const void *ptr1
, const void *ptr2
)
1018 struct elf_link_hash_entry
*h1
1019 = (struct elf_link_hash_entry
*) ptr1
;
1020 struct elf_link_hash_entry
*h2
1021 = (struct elf_link_hash_entry
*) ptr2
;
1023 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
1026 /* Find and/or create a hash entry for local symbol. */
1028 static struct elf_link_hash_entry
*
1029 elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table
*htab
,
1030 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
1033 struct elf_x86_64_link_hash_entry e
, *ret
;
1034 asection
*sec
= abfd
->sections
;
1035 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
1036 htab
->r_sym (rel
->r_info
));
1039 e
.elf
.indx
= sec
->id
;
1040 e
.elf
.dynstr_index
= htab
->r_sym (rel
->r_info
);
1041 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &e
, h
,
1042 create
? INSERT
: NO_INSERT
);
1049 ret
= (struct elf_x86_64_link_hash_entry
*) *slot
;
1053 ret
= (struct elf_x86_64_link_hash_entry
*)
1054 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
1055 sizeof (struct elf_x86_64_link_hash_entry
));
1058 memset (ret
, 0, sizeof (*ret
));
1059 ret
->elf
.indx
= sec
->id
;
1060 ret
->elf
.dynstr_index
= htab
->r_sym (rel
->r_info
);
1061 ret
->elf
.dynindx
= -1;
1062 ret
->func_pointer_refcount
= 0;
1063 ret
->plt_got
.offset
= (bfd_vma
) -1;
1069 /* Destroy an X86-64 ELF linker hash table. */
1072 elf_x86_64_link_hash_table_free (bfd
*obfd
)
1074 struct elf_x86_64_link_hash_table
*htab
1075 = (struct elf_x86_64_link_hash_table
*) obfd
->link
.hash
;
1077 if (htab
->loc_hash_table
)
1078 htab_delete (htab
->loc_hash_table
);
1079 if (htab
->loc_hash_memory
)
1080 objalloc_free ((struct objalloc
*) htab
->loc_hash_memory
);
1081 _bfd_elf_link_hash_table_free (obfd
);
1084 /* Create an X86-64 ELF linker hash table. */
1086 static struct bfd_link_hash_table
*
1087 elf_x86_64_link_hash_table_create (bfd
*abfd
)
1089 struct elf_x86_64_link_hash_table
*ret
;
1090 bfd_size_type amt
= sizeof (struct elf_x86_64_link_hash_table
);
1092 ret
= (struct elf_x86_64_link_hash_table
*) bfd_zmalloc (amt
);
1096 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
,
1097 elf_x86_64_link_hash_newfunc
,
1098 sizeof (struct elf_x86_64_link_hash_entry
),
1105 if (ABI_64_P (abfd
))
1107 ret
->r_info
= elf64_r_info
;
1108 ret
->r_sym
= elf64_r_sym
;
1109 ret
->pointer_r_type
= R_X86_64_64
;
1110 ret
->dynamic_interpreter
= ELF64_DYNAMIC_INTERPRETER
;
1111 ret
->dynamic_interpreter_size
= sizeof ELF64_DYNAMIC_INTERPRETER
;
1115 ret
->r_info
= elf32_r_info
;
1116 ret
->r_sym
= elf32_r_sym
;
1117 ret
->pointer_r_type
= R_X86_64_32
;
1118 ret
->dynamic_interpreter
= ELF32_DYNAMIC_INTERPRETER
;
1119 ret
->dynamic_interpreter_size
= sizeof ELF32_DYNAMIC_INTERPRETER
;
1122 ret
->loc_hash_table
= htab_try_create (1024,
1123 elf_x86_64_local_htab_hash
,
1124 elf_x86_64_local_htab_eq
,
1126 ret
->loc_hash_memory
= objalloc_create ();
1127 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
1129 elf_x86_64_link_hash_table_free (abfd
);
1132 ret
->elf
.root
.hash_table_free
= elf_x86_64_link_hash_table_free
;
1134 return &ret
->elf
.root
;
1137 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
1138 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
1142 elf_x86_64_create_dynamic_sections (bfd
*dynobj
,
1143 struct bfd_link_info
*info
)
1145 struct elf_x86_64_link_hash_table
*htab
;
1147 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
1150 htab
= elf_x86_64_hash_table (info
);
1154 /* Set the contents of the .interp section to the interpreter. */
1155 if (bfd_link_executable (info
) && !info
->nointerp
)
1157 asection
*s
= bfd_get_linker_section (dynobj
, ".interp");
1160 s
->size
= htab
->dynamic_interpreter_size
;
1161 s
->contents
= (unsigned char *) htab
->dynamic_interpreter
;
1165 if (!info
->no_ld_generated_unwind_info
1166 && htab
->plt_eh_frame
== NULL
1167 && htab
->elf
.splt
!= NULL
)
1169 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1170 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
1171 | SEC_LINKER_CREATED
);
1173 = bfd_make_section_anyway_with_flags (dynobj
, ".eh_frame", flags
);
1174 if (htab
->plt_eh_frame
== NULL
1175 || !bfd_set_section_alignment (dynobj
, htab
->plt_eh_frame
,
1176 ABI_64_P (dynobj
) ? 3 : 2))
1180 /* Align .got section to its entry size. */
1181 if (htab
->elf
.sgot
!= NULL
1182 && !bfd_set_section_alignment (dynobj
, htab
->elf
.sgot
, 3))
1185 /* Align .got.plt section to its entry size. */
1186 if (htab
->elf
.sgotplt
!= NULL
1187 && !bfd_set_section_alignment (dynobj
, htab
->elf
.sgotplt
, 3))
1193 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1196 elf_x86_64_copy_indirect_symbol (struct bfd_link_info
*info
,
1197 struct elf_link_hash_entry
*dir
,
1198 struct elf_link_hash_entry
*ind
)
1200 struct elf_x86_64_link_hash_entry
*edir
, *eind
;
1202 edir
= (struct elf_x86_64_link_hash_entry
*) dir
;
1203 eind
= (struct elf_x86_64_link_hash_entry
*) ind
;
1205 edir
->has_bnd_reloc
|= eind
->has_bnd_reloc
;
1206 edir
->has_got_reloc
|= eind
->has_got_reloc
;
1207 edir
->has_non_got_reloc
|= eind
->has_non_got_reloc
;
1209 if (eind
->dyn_relocs
!= NULL
)
1211 if (edir
->dyn_relocs
!= NULL
)
1213 struct elf_dyn_relocs
**pp
;
1214 struct elf_dyn_relocs
*p
;
1216 /* Add reloc counts against the indirect sym to the direct sym
1217 list. Merge any entries against the same section. */
1218 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
1220 struct elf_dyn_relocs
*q
;
1222 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
1223 if (q
->sec
== p
->sec
)
1225 q
->pc_count
+= p
->pc_count
;
1226 q
->count
+= p
->count
;
1233 *pp
= edir
->dyn_relocs
;
1236 edir
->dyn_relocs
= eind
->dyn_relocs
;
1237 eind
->dyn_relocs
= NULL
;
1240 if (ind
->root
.type
== bfd_link_hash_indirect
1241 && dir
->got
.refcount
<= 0)
1243 edir
->tls_type
= eind
->tls_type
;
1244 eind
->tls_type
= GOT_UNKNOWN
;
1247 if (ELIMINATE_COPY_RELOCS
1248 && ind
->root
.type
!= bfd_link_hash_indirect
1249 && dir
->dynamic_adjusted
)
1251 /* If called to transfer flags for a weakdef during processing
1252 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1253 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1254 if (dir
->versioned
!= versioned_hidden
)
1255 dir
->ref_dynamic
|= ind
->ref_dynamic
;
1256 dir
->ref_regular
|= ind
->ref_regular
;
1257 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
1258 dir
->needs_plt
|= ind
->needs_plt
;
1259 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
1263 if (eind
->func_pointer_refcount
> 0)
1265 edir
->func_pointer_refcount
+= eind
->func_pointer_refcount
;
1266 eind
->func_pointer_refcount
= 0;
1269 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
1274 elf64_x86_64_elf_object_p (bfd
*abfd
)
1276 /* Set the right machine number for an x86-64 elf64 file. */
1277 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
1282 elf32_x86_64_elf_object_p (bfd
*abfd
)
1284 /* Set the right machine number for an x86-64 elf32 file. */
1285 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x64_32
);
1289 /* Return TRUE if the TLS access code sequence support transition
1293 elf_x86_64_check_tls_transition (bfd
*abfd
,
1294 struct bfd_link_info
*info
,
1297 Elf_Internal_Shdr
*symtab_hdr
,
1298 struct elf_link_hash_entry
**sym_hashes
,
1299 unsigned int r_type
,
1300 const Elf_Internal_Rela
*rel
,
1301 const Elf_Internal_Rela
*relend
)
1304 unsigned long r_symndx
;
1305 bfd_boolean largepic
= FALSE
;
1306 struct elf_link_hash_entry
*h
;
1308 struct elf_x86_64_link_hash_table
*htab
;
1310 bfd_boolean indirect_call
, tls_get_addr
;
1312 htab
= elf_x86_64_hash_table (info
);
1313 offset
= rel
->r_offset
;
1316 case R_X86_64_TLSGD
:
1317 case R_X86_64_TLSLD
:
1318 if ((rel
+ 1) >= relend
)
1321 if (r_type
== R_X86_64_TLSGD
)
1323 /* Check transition from GD access model. For 64bit, only
1324 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1325 .word 0x6666; rex64; call __tls_get_addr@PLT
1327 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1329 call *__tls_get_addr@GOTPCREL(%rip)
1330 which may be converted to
1331 addr32 call __tls_get_addr
1332 can transit to different access model. For 32bit, only
1333 leaq foo@tlsgd(%rip), %rdi
1334 .word 0x6666; rex64; call __tls_get_addr@PLT
1336 leaq foo@tlsgd(%rip), %rdi
1338 call *__tls_get_addr@GOTPCREL(%rip)
1339 which may be converted to
1340 addr32 call __tls_get_addr
1341 can transit to different access model. For largepic,
1343 leaq foo@tlsgd(%rip), %rdi
1344 movabsq $__tls_get_addr@pltoff, %rax
1348 leaq foo@tlsgd(%rip), %rdi
1349 movabsq $__tls_get_addr@pltoff, %rax
1353 static const unsigned char leaq
[] = { 0x66, 0x48, 0x8d, 0x3d };
1355 if ((offset
+ 12) > sec
->size
)
1358 call
= contents
+ offset
+ 4;
1360 || !((call
[1] == 0x48
1368 && call
[3] == 0xe8)))
1370 if (!ABI_64_P (abfd
)
1371 || (offset
+ 19) > sec
->size
1373 || memcmp (call
- 7, leaq
+ 1, 3) != 0
1374 || memcmp (call
, "\x48\xb8", 2) != 0
1378 || !((call
[10] == 0x48 && call
[12] == 0xd8)
1379 || (call
[10] == 0x4c && call
[12] == 0xf8)))
1383 else if (ABI_64_P (abfd
))
1386 || memcmp (contents
+ offset
- 4, leaq
, 4) != 0)
1392 || memcmp (contents
+ offset
- 3, leaq
+ 1, 3) != 0)
1395 indirect_call
= call
[2] == 0xff;
1399 /* Check transition from LD access model. Only
1400 leaq foo@tlsld(%rip), %rdi;
1401 call __tls_get_addr@PLT
1403 leaq foo@tlsld(%rip), %rdi;
1404 call *__tls_get_addr@GOTPCREL(%rip)
1405 which may be converted to
1406 addr32 call __tls_get_addr
1407 can transit to different access model. For largepic
1409 leaq foo@tlsld(%rip), %rdi
1410 movabsq $__tls_get_addr@pltoff, %rax
1414 leaq foo@tlsld(%rip), %rdi
1415 movabsq $__tls_get_addr@pltoff, %rax
1419 static const unsigned char lea
[] = { 0x48, 0x8d, 0x3d };
1421 if (offset
< 3 || (offset
+ 9) > sec
->size
)
1424 if (memcmp (contents
+ offset
- 3, lea
, 3) != 0)
1427 call
= contents
+ offset
+ 4;
1428 if (!(call
[0] == 0xe8
1429 || (call
[0] == 0xff && call
[1] == 0x15)
1430 || (call
[0] == 0x67 && call
[1] == 0xe8)))
1432 if (!ABI_64_P (abfd
)
1433 || (offset
+ 19) > sec
->size
1434 || memcmp (call
, "\x48\xb8", 2) != 0
1438 || !((call
[10] == 0x48 && call
[12] == 0xd8)
1439 || (call
[10] == 0x4c && call
[12] == 0xf8)))
1443 indirect_call
= call
[0] == 0xff;
1446 r_symndx
= htab
->r_sym (rel
[1].r_info
);
1447 if (r_symndx
< symtab_hdr
->sh_info
)
1450 tls_get_addr
= FALSE
;
1451 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1452 if (h
!= NULL
&& h
->root
.root
.string
!= NULL
)
1454 struct elf_x86_64_link_hash_entry
*eh
1455 = (struct elf_x86_64_link_hash_entry
*) h
;
1456 tls_get_addr
= eh
->tls_get_addr
== 1;
1457 if (eh
->tls_get_addr
> 1)
1459 /* Use strncmp to check __tls_get_addr since
1460 __tls_get_addr may be versioned. */
1461 if (strncmp (h
->root
.root
.string
, "__tls_get_addr", 14)
1464 eh
->tls_get_addr
= 1;
1465 tls_get_addr
= TRUE
;
1468 eh
->tls_get_addr
= 0;
1475 return ELF32_R_TYPE (rel
[1].r_info
) == R_X86_64_PLTOFF64
;
1476 else if (indirect_call
)
1477 return ELF32_R_TYPE (rel
[1].r_info
) == R_X86_64_GOTPCRELX
;
1479 return (ELF32_R_TYPE (rel
[1].r_info
) == R_X86_64_PC32
1480 || ELF32_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
);
1482 case R_X86_64_GOTTPOFF
:
1483 /* Check transition from IE access model:
1484 mov foo@gottpoff(%rip), %reg
1485 add foo@gottpoff(%rip), %reg
1488 /* Check REX prefix first. */
1489 if (offset
>= 3 && (offset
+ 4) <= sec
->size
)
1491 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
1492 if (val
!= 0x48 && val
!= 0x4c)
1494 /* X32 may have 0x44 REX prefix or no REX prefix. */
1495 if (ABI_64_P (abfd
))
1501 /* X32 may not have any REX prefix. */
1502 if (ABI_64_P (abfd
))
1504 if (offset
< 2 || (offset
+ 3) > sec
->size
)
1508 val
= bfd_get_8 (abfd
, contents
+ offset
- 2);
1509 if (val
!= 0x8b && val
!= 0x03)
1512 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
1513 return (val
& 0xc7) == 5;
1515 case R_X86_64_GOTPC32_TLSDESC
:
1516 /* Check transition from GDesc access model:
1517 leaq x@tlsdesc(%rip), %rax
1519 Make sure it's a leaq adding rip to a 32-bit offset
1520 into any register, although it's probably almost always
1523 if (offset
< 3 || (offset
+ 4) > sec
->size
)
1526 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
1527 if ((val
& 0xfb) != 0x48)
1530 if (bfd_get_8 (abfd
, contents
+ offset
- 2) != 0x8d)
1533 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
1534 return (val
& 0xc7) == 0x05;
1536 case R_X86_64_TLSDESC_CALL
:
1537 /* Check transition from GDesc access model:
1538 call *x@tlsdesc(%rax)
1540 if (offset
+ 2 <= sec
->size
)
1542 /* Make sure that it's a call *x@tlsdesc(%rax). */
1543 call
= contents
+ offset
;
1544 return call
[0] == 0xff && call
[1] == 0x10;
1554 /* Return TRUE if the TLS access transition is OK or no transition
1555 will be performed. Update R_TYPE if there is a transition. */
1558 elf_x86_64_tls_transition (struct bfd_link_info
*info
, bfd
*abfd
,
1559 asection
*sec
, bfd_byte
*contents
,
1560 Elf_Internal_Shdr
*symtab_hdr
,
1561 struct elf_link_hash_entry
**sym_hashes
,
1562 unsigned int *r_type
, int tls_type
,
1563 const Elf_Internal_Rela
*rel
,
1564 const Elf_Internal_Rela
*relend
,
1565 struct elf_link_hash_entry
*h
,
1566 unsigned long r_symndx
,
1567 bfd_boolean from_relocate_section
)
1569 unsigned int from_type
= *r_type
;
1570 unsigned int to_type
= from_type
;
1571 bfd_boolean check
= TRUE
;
1573 /* Skip TLS transition for functions. */
1575 && (h
->type
== STT_FUNC
1576 || h
->type
== STT_GNU_IFUNC
))
1581 case R_X86_64_TLSGD
:
1582 case R_X86_64_GOTPC32_TLSDESC
:
1583 case R_X86_64_TLSDESC_CALL
:
1584 case R_X86_64_GOTTPOFF
:
1585 if (bfd_link_executable (info
))
1588 to_type
= R_X86_64_TPOFF32
;
1590 to_type
= R_X86_64_GOTTPOFF
;
1593 /* When we are called from elf_x86_64_relocate_section, there may
1594 be additional transitions based on TLS_TYPE. */
1595 if (from_relocate_section
)
1597 unsigned int new_to_type
= to_type
;
1599 if (bfd_link_executable (info
)
1602 && tls_type
== GOT_TLS_IE
)
1603 new_to_type
= R_X86_64_TPOFF32
;
1605 if (to_type
== R_X86_64_TLSGD
1606 || to_type
== R_X86_64_GOTPC32_TLSDESC
1607 || to_type
== R_X86_64_TLSDESC_CALL
)
1609 if (tls_type
== GOT_TLS_IE
)
1610 new_to_type
= R_X86_64_GOTTPOFF
;
1613 /* We checked the transition before when we were called from
1614 elf_x86_64_check_relocs. We only want to check the new
1615 transition which hasn't been checked before. */
1616 check
= new_to_type
!= to_type
&& from_type
== to_type
;
1617 to_type
= new_to_type
;
1622 case R_X86_64_TLSLD
:
1623 if (bfd_link_executable (info
))
1624 to_type
= R_X86_64_TPOFF32
;
1631 /* Return TRUE if there is no transition. */
1632 if (from_type
== to_type
)
1635 /* Check if the transition can be performed. */
1637 && ! elf_x86_64_check_tls_transition (abfd
, info
, sec
, contents
,
1638 symtab_hdr
, sym_hashes
,
1639 from_type
, rel
, relend
))
1641 reloc_howto_type
*from
, *to
;
1644 from
= elf_x86_64_rtype_to_howto (abfd
, from_type
);
1645 to
= elf_x86_64_rtype_to_howto (abfd
, to_type
);
1648 name
= h
->root
.root
.string
;
1651 struct elf_x86_64_link_hash_table
*htab
;
1653 htab
= elf_x86_64_hash_table (info
);
1658 Elf_Internal_Sym
*isym
;
1660 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
1662 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, isym
, NULL
);
1667 /* xgettext:c-format */
1668 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1669 "in section `%A' failed"),
1670 abfd
, sec
, from
->name
, to
->name
, name
,
1671 (unsigned long) rel
->r_offset
);
1672 bfd_set_error (bfd_error_bad_value
);
1680 /* Rename some of the generic section flags to better document how they
1682 #define need_convert_load sec_flg0
1683 #define check_relocs_failed sec_flg1
1686 elf_x86_64_need_pic (bfd
*input_bfd
, asection
*sec
,
1687 struct elf_link_hash_entry
*h
,
1688 Elf_Internal_Shdr
*symtab_hdr
,
1689 Elf_Internal_Sym
*isym
,
1690 reloc_howto_type
*howto
)
1693 const char *und
= "";
1694 const char *pic
= "";
1699 name
= h
->root
.root
.string
;
1700 switch (ELF_ST_VISIBILITY (h
->other
))
1703 v
= _("hidden symbol ");
1706 v
= _("internal symbol ");
1709 v
= _("protected symbol ");
1713 pic
= _("; recompile with -fPIC");
1717 if (!h
->def_regular
&& !h
->def_dynamic
)
1718 und
= _("undefined ");
1722 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, isym
, NULL
);
1723 pic
= _("; recompile with -fPIC");
1726 /* xgettext:c-format */
1727 _bfd_error_handler (_("%B: relocation %s against %s%s`%s' can "
1728 "not be used when making a shared object%s"),
1729 input_bfd
, howto
->name
, und
, v
, name
, pic
);
1730 bfd_set_error (bfd_error_bad_value
);
1731 sec
->check_relocs_failed
= 1;
1735 /* With the local symbol, foo, we convert
1736 mov foo@GOTPCREL(%rip), %reg
1740 call/jmp *foo@GOTPCREL(%rip)
1742 nop call foo/jmp foo nop
1743 When PIC is false, convert
1744 test %reg, foo@GOTPCREL(%rip)
1748 binop foo@GOTPCREL(%rip), %reg
1751 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1755 elf_x86_64_convert_load_reloc (bfd
*abfd
, asection
*sec
,
1757 Elf_Internal_Rela
*irel
,
1758 struct elf_link_hash_entry
*h
,
1759 bfd_boolean
*converted
,
1760 struct bfd_link_info
*link_info
)
1762 struct elf_x86_64_link_hash_table
*htab
;
1764 bfd_boolean require_reloc_pc32
;
1766 bfd_boolean to_reloc_pc32
;
1769 bfd_signed_vma raddend
;
1770 unsigned int opcode
;
1772 unsigned int r_type
= ELF32_R_TYPE (irel
->r_info
);
1773 unsigned int r_symndx
;
1775 bfd_vma roff
= irel
->r_offset
;
1777 if (roff
< (r_type
== R_X86_64_REX_GOTPCRELX
? 3 : 2))
1780 raddend
= irel
->r_addend
;
1781 /* Addend for 32-bit PC-relative relocation must be -4. */
1785 htab
= elf_x86_64_hash_table (link_info
);
1786 is_pic
= bfd_link_pic (link_info
);
1788 relocx
= (r_type
== R_X86_64_GOTPCRELX
1789 || r_type
== R_X86_64_REX_GOTPCRELX
);
1791 /* TRUE if we can convert only to R_X86_64_PC32. Enable it for
1794 = link_info
->disable_target_specific_optimizations
> 1;
1796 r_symndx
= htab
->r_sym (irel
->r_info
);
1798 opcode
= bfd_get_8 (abfd
, contents
+ roff
- 2);
1800 /* Convert mov to lea since it has been done for a while. */
1803 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1804 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1805 test, xor instructions. */
1810 /* We convert only to R_X86_64_PC32:
1812 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1813 3. require_reloc_pc32 is true.
1816 to_reloc_pc32
= (opcode
== 0xff
1818 || require_reloc_pc32
1821 /* Get the symbol referred to by the reloc. */
1824 Elf_Internal_Sym
*isym
1825 = bfd_sym_from_r_symndx (&htab
->sym_cache
, abfd
, r_symndx
);
1827 /* Skip relocation against undefined symbols. */
1828 if (isym
->st_shndx
== SHN_UNDEF
)
1831 symtype
= ELF_ST_TYPE (isym
->st_info
);
1833 if (isym
->st_shndx
== SHN_ABS
)
1834 tsec
= bfd_abs_section_ptr
;
1835 else if (isym
->st_shndx
== SHN_COMMON
)
1836 tsec
= bfd_com_section_ptr
;
1837 else if (isym
->st_shndx
== SHN_X86_64_LCOMMON
)
1838 tsec
= &_bfd_elf_large_com_section
;
1840 tsec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1842 toff
= isym
->st_value
;
1846 /* Undefined weak symbol is only bound locally in executable
1847 and its reference is resolved as 0 without relocation
1848 overflow. We can only perform this optimization for
1849 GOTPCRELX relocations since we need to modify REX byte.
1850 It is OK convert mov with R_X86_64_GOTPCREL to
1852 if ((relocx
|| opcode
== 0x8b)
1853 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info
,
1855 elf_x86_64_hash_entry (h
)))
1859 /* Skip for branch instructions since R_X86_64_PC32
1861 if (require_reloc_pc32
)
1866 /* For non-branch instructions, we can convert to
1867 R_X86_64_32/R_X86_64_32S since we know if there
1869 to_reloc_pc32
= FALSE
;
1872 /* Since we don't know the current PC when PIC is true,
1873 we can't convert to R_X86_64_PC32. */
1874 if (to_reloc_pc32
&& is_pic
)
1879 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1880 ld.so may use its link-time address. */
1881 else if ((h
->def_regular
1882 || h
->root
.type
== bfd_link_hash_defined
1883 || h
->root
.type
== bfd_link_hash_defweak
)
1884 && h
!= htab
->elf
.hdynamic
1885 && SYMBOL_REFERENCES_LOCAL (link_info
, h
))
1887 /* bfd_link_hash_new or bfd_link_hash_undefined is
1888 set by an assignment in a linker script in
1889 bfd_elf_record_link_assignment. */
1891 && (h
->root
.type
== bfd_link_hash_new
1892 || h
->root
.type
== bfd_link_hash_undefined
1893 || ((h
->root
.type
== bfd_link_hash_defined
1894 || h
->root
.type
== bfd_link_hash_defweak
)
1895 && h
->root
.u
.def
.section
== bfd_und_section_ptr
)))
1897 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1898 if (require_reloc_pc32
)
1902 tsec
= h
->root
.u
.def
.section
;
1903 toff
= h
->root
.u
.def
.value
;
1910 /* Don't convert GOTPCREL relocation against large section. */
1911 if (elf_section_data (tsec
) != NULL
1912 && (elf_section_flags (tsec
) & SHF_X86_64_LARGE
) != 0)
1915 /* We can only estimate relocation overflow for R_X86_64_PC32. */
1919 if (tsec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
1921 /* At this stage in linking, no SEC_MERGE symbol has been
1922 adjusted, so all references to such symbols need to be
1923 passed through _bfd_merged_section_offset. (Later, in
1924 relocate_section, all SEC_MERGE symbols *except* for
1925 section symbols have been adjusted.)
1927 gas may reduce relocations against symbols in SEC_MERGE
1928 sections to a relocation against the section symbol when
1929 the original addend was zero. When the reloc is against
1930 a section symbol we should include the addend in the
1931 offset passed to _bfd_merged_section_offset, since the
1932 location of interest is the original symbol. On the
1933 other hand, an access to "sym+addend" where "sym" is not
1934 a section symbol should not include the addend; Such an
1935 access is presumed to be an offset from "sym"; The
1936 location of interest is just "sym". */
1937 if (symtype
== STT_SECTION
)
1940 toff
= _bfd_merged_section_offset (abfd
, &tsec
,
1941 elf_section_data (tsec
)->sec_info
,
1944 if (symtype
!= STT_SECTION
)
1950 /* Don't convert if R_X86_64_PC32 relocation overflows. */
1951 if (tsec
->output_section
== sec
->output_section
)
1953 if ((toff
- roff
+ 0x80000000) > 0xffffffff)
1958 bfd_signed_vma distance
;
1960 /* At this point, we don't know the load addresses of TSEC
1961 section nor SEC section. We estimate the distrance between
1962 SEC and TSEC. We store the estimated distances in the
1963 compressed_size field of the output section, which is only
1964 used to decompress the compressed input section. */
1965 if (sec
->output_section
->compressed_size
== 0)
1968 bfd_size_type size
= 0;
1969 for (asect
= link_info
->output_bfd
->sections
;
1971 asect
= asect
->next
)
1972 /* Skip debug sections since compressed_size is used to
1973 compress debug sections. */
1974 if ((asect
->flags
& SEC_DEBUGGING
) == 0)
1977 for (i
= asect
->map_head
.s
;
1981 size
= align_power (size
, i
->alignment_power
);
1984 asect
->compressed_size
= size
;
1988 /* Don't convert GOTPCREL relocations if TSEC isn't placed
1990 distance
= (tsec
->output_section
->compressed_size
1991 - sec
->output_section
->compressed_size
);
1995 /* Take PT_GNU_RELRO segment into account by adding
1997 if ((toff
+ distance
+ get_elf_backend_data (abfd
)->maxpagesize
1998 - roff
+ 0x80000000) > 0xffffffff)
2005 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
2010 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
2012 modrm
= bfd_get_8 (abfd
, contents
+ roff
- 1);
2015 /* Convert to "jmp foo nop". */
2018 nop_offset
= irel
->r_offset
+ 3;
2019 disp
= bfd_get_32 (abfd
, contents
+ irel
->r_offset
);
2020 irel
->r_offset
-= 1;
2021 bfd_put_32 (abfd
, disp
, contents
+ irel
->r_offset
);
2025 struct elf_x86_64_link_hash_entry
*eh
2026 = (struct elf_x86_64_link_hash_entry
*) h
;
2028 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
2031 /* To support TLS optimization, always use addr32 prefix for
2032 "call *__tls_get_addr@GOTPCREL(%rip)". */
2033 if (eh
&& eh
->tls_get_addr
== 1)
2036 nop_offset
= irel
->r_offset
- 2;
2040 nop
= link_info
->call_nop_byte
;
2041 if (link_info
->call_nop_as_suffix
)
2043 nop_offset
= irel
->r_offset
+ 3;
2044 disp
= bfd_get_32 (abfd
, contents
+ irel
->r_offset
);
2045 irel
->r_offset
-= 1;
2046 bfd_put_32 (abfd
, disp
, contents
+ irel
->r_offset
);
2049 nop_offset
= irel
->r_offset
- 2;
2052 bfd_put_8 (abfd
, nop
, contents
+ nop_offset
);
2053 bfd_put_8 (abfd
, modrm
, contents
+ irel
->r_offset
- 1);
2054 r_type
= R_X86_64_PC32
;
2059 unsigned int rex_mask
= REX_R
;
2061 if (r_type
== R_X86_64_REX_GOTPCRELX
)
2062 rex
= bfd_get_8 (abfd
, contents
+ roff
- 3);
2070 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
2071 "lea foo(%rip), %reg". */
2073 r_type
= R_X86_64_PC32
;
2077 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
2078 "mov $foo, %reg". */
2080 modrm
= bfd_get_8 (abfd
, contents
+ roff
- 1);
2081 modrm
= 0xc0 | (modrm
& 0x38) >> 3;
2082 if ((rex
& REX_W
) != 0
2083 && ABI_64_P (link_info
->output_bfd
))
2085 /* Keep the REX_W bit in REX byte for LP64. */
2086 r_type
= R_X86_64_32S
;
2087 goto rewrite_modrm_rex
;
2091 /* If the REX_W bit in REX byte isn't needed,
2092 use R_X86_64_32 and clear the W bit to avoid
2093 sign-extend imm32 to imm64. */
2094 r_type
= R_X86_64_32
;
2095 /* Clear the W bit in REX byte. */
2097 goto rewrite_modrm_rex
;
2103 /* R_X86_64_PC32 isn't supported. */
2107 modrm
= bfd_get_8 (abfd
, contents
+ roff
- 1);
2110 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
2111 "test $foo, %reg". */
2112 modrm
= 0xc0 | (modrm
& 0x38) >> 3;
2117 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
2118 "binop $foo, %reg". */
2119 modrm
= 0xc0 | (modrm
& 0x38) >> 3 | (opcode
& 0x3c);
2123 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
2124 overflow when sign-extending imm32 to imm64. */
2125 r_type
= (rex
& REX_W
) != 0 ? R_X86_64_32S
: R_X86_64_32
;
2128 bfd_put_8 (abfd
, modrm
, contents
+ roff
- 1);
2132 /* Move the R bit to the B bit in REX byte. */
2133 rex
= (rex
& ~rex_mask
) | (rex
& REX_R
) >> 2;
2134 bfd_put_8 (abfd
, rex
, contents
+ roff
- 3);
2137 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
2141 bfd_put_8 (abfd
, opcode
, contents
+ roff
- 2);
2144 irel
->r_info
= htab
->r_info (r_symndx
, r_type
);
2151 /* Look through the relocs for a section during the first phase, and
2152 calculate needed space in the global offset table, procedure
2153 linkage table, and dynamic reloc sections. */
2156 elf_x86_64_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
2158 const Elf_Internal_Rela
*relocs
)
2160 struct elf_x86_64_link_hash_table
*htab
;
2161 Elf_Internal_Shdr
*symtab_hdr
;
2162 struct elf_link_hash_entry
**sym_hashes
;
2163 const Elf_Internal_Rela
*rel
;
2164 const Elf_Internal_Rela
*rel_end
;
2167 bfd_boolean use_plt_got
;
2169 if (bfd_link_relocatable (info
))
2172 /* Don't do anything special with non-loaded, non-alloced sections.
2173 In particular, any relocs in such sections should not affect GOT
2174 and PLT reference counting (ie. we don't allow them to create GOT
2175 or PLT entries), there's no possibility or desire to optimize TLS
2176 relocs, and there's not much point in propagating relocs to shared
2177 libs that the dynamic linker won't relocate. */
2178 if ((sec
->flags
& SEC_ALLOC
) == 0)
2181 BFD_ASSERT (is_x86_64_elf (abfd
));
2183 htab
= elf_x86_64_hash_table (info
);
2186 sec
->check_relocs_failed
= 1;
2190 /* Get the section contents. */
2191 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2192 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2193 else if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2195 sec
->check_relocs_failed
= 1;
2199 use_plt_got
= get_elf_x86_64_backend_data (abfd
) == &elf_x86_64_arch_bed
;
2201 symtab_hdr
= &elf_symtab_hdr (abfd
);
2202 sym_hashes
= elf_sym_hashes (abfd
);
2206 rel_end
= relocs
+ sec
->reloc_count
;
2207 for (rel
= relocs
; rel
< rel_end
; rel
++)
2209 unsigned int r_type
;
2210 unsigned long r_symndx
;
2211 struct elf_link_hash_entry
*h
;
2212 struct elf_x86_64_link_hash_entry
*eh
;
2213 Elf_Internal_Sym
*isym
;
2215 bfd_boolean size_reloc
;
2217 r_symndx
= htab
->r_sym (rel
->r_info
);
2218 r_type
= ELF32_R_TYPE (rel
->r_info
);
2220 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
2222 /* xgettext:c-format */
2223 _bfd_error_handler (_("%B: bad symbol index: %d"),
2228 if (r_symndx
< symtab_hdr
->sh_info
)
2230 /* A local symbol. */
2231 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
2236 /* Check relocation against local STT_GNU_IFUNC symbol. */
2237 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
2239 h
= elf_x86_64_get_local_sym_hash (htab
, abfd
, rel
,
2244 /* Fake a STT_GNU_IFUNC symbol. */
2245 h
->type
= STT_GNU_IFUNC
;
2248 h
->forced_local
= 1;
2249 h
->root
.type
= bfd_link_hash_defined
;
2257 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
2258 while (h
->root
.type
== bfd_link_hash_indirect
2259 || h
->root
.type
== bfd_link_hash_warning
)
2260 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2263 /* Check invalid x32 relocations. */
2264 if (!ABI_64_P (abfd
))
2270 case R_X86_64_DTPOFF64
:
2271 case R_X86_64_TPOFF64
:
2273 case R_X86_64_GOTOFF64
:
2274 case R_X86_64_GOT64
:
2275 case R_X86_64_GOTPCREL64
:
2276 case R_X86_64_GOTPC64
:
2277 case R_X86_64_GOTPLT64
:
2278 case R_X86_64_PLTOFF64
:
2281 name
= h
->root
.root
.string
;
2283 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, isym
,
2286 /* xgettext:c-format */
2287 (_("%B: relocation %s against symbol `%s' isn't "
2288 "supported in x32 mode"), abfd
,
2289 x86_64_elf_howto_table
[r_type
].name
, name
);
2290 bfd_set_error (bfd_error_bad_value
);
2303 case R_X86_64_PC32_BND
:
2304 case R_X86_64_PLT32_BND
:
2306 case R_X86_64_PLT32
:
2309 /* MPX PLT is supported only if elf_x86_64_arch_bed
2310 is used in 64-bit mode. */
2313 && (get_elf_x86_64_backend_data (abfd
)
2314 == &elf_x86_64_arch_bed
))
2316 elf_x86_64_hash_entry (h
)->has_bnd_reloc
= 1;
2318 /* Create the second PLT for Intel MPX support. */
2319 if (htab
->plt_bnd
== NULL
)
2321 unsigned int plt_bnd_align
;
2322 const struct elf_backend_data
*bed
;
2324 bed
= get_elf_backend_data (info
->output_bfd
);
2325 BFD_ASSERT (sizeof (elf_x86_64_bnd_plt2_entry
) == 8
2326 && (sizeof (elf_x86_64_bnd_plt2_entry
)
2327 == sizeof (elf_x86_64_legacy_plt2_entry
)));
2330 if (htab
->elf
.dynobj
== NULL
)
2331 htab
->elf
.dynobj
= abfd
;
2333 = bfd_make_section_anyway_with_flags (htab
->elf
.dynobj
,
2335 (bed
->dynamic_sec_flags
2340 if (htab
->plt_bnd
== NULL
2341 || !bfd_set_section_alignment (htab
->elf
.dynobj
,
2351 case R_X86_64_GOTPCREL
:
2352 case R_X86_64_GOTPCRELX
:
2353 case R_X86_64_REX_GOTPCRELX
:
2354 case R_X86_64_GOTPCREL64
:
2355 if (htab
->elf
.dynobj
== NULL
)
2356 htab
->elf
.dynobj
= abfd
;
2357 /* Create the ifunc sections for static executables. */
2358 if (h
->type
== STT_GNU_IFUNC
2359 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
,
2365 /* It is referenced by a non-shared object. */
2367 h
->root
.non_ir_ref
= 1;
2369 if (h
->type
== STT_GNU_IFUNC
)
2370 elf_tdata (info
->output_bfd
)->has_gnu_symbols
2371 |= elf_gnu_symbol_ifunc
;
2374 if (! elf_x86_64_tls_transition (info
, abfd
, sec
, contents
,
2375 symtab_hdr
, sym_hashes
,
2376 &r_type
, GOT_UNKNOWN
,
2377 rel
, rel_end
, h
, r_symndx
, FALSE
))
2380 eh
= (struct elf_x86_64_link_hash_entry
*) h
;
2383 case R_X86_64_TLSLD
:
2384 htab
->tls_ld_got
.refcount
+= 1;
2387 case R_X86_64_TPOFF32
:
2388 if (!bfd_link_executable (info
) && ABI_64_P (abfd
))
2389 return elf_x86_64_need_pic (abfd
, sec
, h
, symtab_hdr
, isym
,
2390 &x86_64_elf_howto_table
[r_type
]);
2392 eh
->has_got_reloc
= 1;
2395 case R_X86_64_GOTTPOFF
:
2396 if (!bfd_link_executable (info
))
2397 info
->flags
|= DF_STATIC_TLS
;
2400 case R_X86_64_GOT32
:
2401 case R_X86_64_GOTPCREL
:
2402 case R_X86_64_GOTPCRELX
:
2403 case R_X86_64_REX_GOTPCRELX
:
2404 case R_X86_64_TLSGD
:
2405 case R_X86_64_GOT64
:
2406 case R_X86_64_GOTPCREL64
:
2407 case R_X86_64_GOTPLT64
:
2408 case R_X86_64_GOTPC32_TLSDESC
:
2409 case R_X86_64_TLSDESC_CALL
:
2410 /* This symbol requires a global offset table entry. */
2412 int tls_type
, old_tls_type
;
2416 default: tls_type
= GOT_NORMAL
; break;
2417 case R_X86_64_TLSGD
: tls_type
= GOT_TLS_GD
; break;
2418 case R_X86_64_GOTTPOFF
: tls_type
= GOT_TLS_IE
; break;
2419 case R_X86_64_GOTPC32_TLSDESC
:
2420 case R_X86_64_TLSDESC_CALL
:
2421 tls_type
= GOT_TLS_GDESC
; break;
2426 h
->got
.refcount
+= 1;
2427 old_tls_type
= eh
->tls_type
;
2431 bfd_signed_vma
*local_got_refcounts
;
2433 /* This is a global offset table entry for a local symbol. */
2434 local_got_refcounts
= elf_local_got_refcounts (abfd
);
2435 if (local_got_refcounts
== NULL
)
2439 size
= symtab_hdr
->sh_info
;
2440 size
*= sizeof (bfd_signed_vma
)
2441 + sizeof (bfd_vma
) + sizeof (char);
2442 local_got_refcounts
= ((bfd_signed_vma
*)
2443 bfd_zalloc (abfd
, size
));
2444 if (local_got_refcounts
== NULL
)
2446 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
2447 elf_x86_64_local_tlsdesc_gotent (abfd
)
2448 = (bfd_vma
*) (local_got_refcounts
+ symtab_hdr
->sh_info
);
2449 elf_x86_64_local_got_tls_type (abfd
)
2450 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
2452 local_got_refcounts
[r_symndx
] += 1;
2454 = elf_x86_64_local_got_tls_type (abfd
) [r_symndx
];
2457 /* If a TLS symbol is accessed using IE at least once,
2458 there is no point to use dynamic model for it. */
2459 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
2460 && (! GOT_TLS_GD_ANY_P (old_tls_type
)
2461 || tls_type
!= GOT_TLS_IE
))
2463 if (old_tls_type
== GOT_TLS_IE
&& GOT_TLS_GD_ANY_P (tls_type
))
2464 tls_type
= old_tls_type
;
2465 else if (GOT_TLS_GD_ANY_P (old_tls_type
)
2466 && GOT_TLS_GD_ANY_P (tls_type
))
2467 tls_type
|= old_tls_type
;
2471 name
= h
->root
.root
.string
;
2473 name
= bfd_elf_sym_name (abfd
, symtab_hdr
,
2476 /* xgettext:c-format */
2477 (_("%B: '%s' accessed both as normal and thread local symbol"),
2479 bfd_set_error (bfd_error_bad_value
);
2484 if (old_tls_type
!= tls_type
)
2487 eh
->tls_type
= tls_type
;
2489 elf_x86_64_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
2494 case R_X86_64_GOTOFF64
:
2495 case R_X86_64_GOTPC32
:
2496 case R_X86_64_GOTPC64
:
2499 eh
->has_got_reloc
= 1;
2500 if (htab
->elf
.sgot
== NULL
)
2502 if (htab
->elf
.dynobj
== NULL
)
2503 htab
->elf
.dynobj
= abfd
;
2504 if (!_bfd_elf_create_got_section (htab
->elf
.dynobj
,
2510 case R_X86_64_PLT32
:
2511 case R_X86_64_PLT32_BND
:
2512 /* This symbol requires a procedure linkage table entry. We
2513 actually build the entry in adjust_dynamic_symbol,
2514 because this might be a case of linking PIC code which is
2515 never referenced by a dynamic object, in which case we
2516 don't need to generate a procedure linkage table entry
2519 /* If this is a local symbol, we resolve it directly without
2520 creating a procedure linkage table entry. */
2524 eh
->has_got_reloc
= 1;
2526 h
->plt
.refcount
+= 1;
2529 case R_X86_64_PLTOFF64
:
2530 /* This tries to form the 'address' of a function relative
2531 to GOT. For global symbols we need a PLT entry. */
2535 h
->plt
.refcount
+= 1;
2539 case R_X86_64_SIZE32
:
2540 case R_X86_64_SIZE64
:
2545 if (!ABI_64_P (abfd
))
2551 /* Check relocation overflow as these relocs may lead to
2552 run-time relocation overflow. Don't error out for
2553 sections we don't care about, such as debug sections or
2554 when relocation overflow check is disabled. */
2555 if (!info
->no_reloc_overflow_check
2556 && (bfd_link_pic (info
)
2557 || (bfd_link_executable (info
)
2561 && (sec
->flags
& SEC_READONLY
) == 0)))
2562 return elf_x86_64_need_pic (abfd
, sec
, h
, symtab_hdr
, isym
,
2563 &x86_64_elf_howto_table
[r_type
]);
2569 case R_X86_64_PC32_BND
:
2573 if (eh
!= NULL
&& (sec
->flags
& SEC_CODE
) != 0)
2574 eh
->has_non_got_reloc
= 1;
2575 /* We are called after all symbols have been resolved. Only
2576 relocation against STT_GNU_IFUNC symbol must go through
2579 && (bfd_link_executable (info
)
2580 || h
->type
== STT_GNU_IFUNC
))
2582 /* If this reloc is in a read-only section, we might
2583 need a copy reloc. We can't check reliably at this
2584 stage whether the section is read-only, as input
2585 sections have not yet been mapped to output sections.
2586 Tentatively set the flag for now, and correct in
2587 adjust_dynamic_symbol. */
2590 /* We may need a .plt entry if the symbol is a function
2591 defined in a shared lib or is a STT_GNU_IFUNC function
2592 referenced from the code or read-only section. */
2594 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
2595 h
->plt
.refcount
+= 1;
2597 if (r_type
== R_X86_64_PC32
)
2599 /* Since something like ".long foo - ." may be used
2600 as pointer, make sure that PLT is used if foo is
2601 a function defined in a shared library. */
2602 if ((sec
->flags
& SEC_CODE
) == 0)
2603 h
->pointer_equality_needed
= 1;
2605 else if (r_type
!= R_X86_64_PC32_BND
2606 && r_type
!= R_X86_64_PC64
)
2608 h
->pointer_equality_needed
= 1;
2609 /* At run-time, R_X86_64_64 can be resolved for both
2610 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2611 can only be resolved for x32. */
2612 if ((sec
->flags
& SEC_READONLY
) == 0
2613 && (r_type
== R_X86_64_64
2614 || (!ABI_64_P (abfd
)
2615 && (r_type
== R_X86_64_32
2616 || r_type
== R_X86_64_32S
))))
2617 eh
->func_pointer_refcount
+= 1;
2623 /* If we are creating a shared library, and this is a reloc
2624 against a global symbol, or a non PC relative reloc
2625 against a local symbol, then we need to copy the reloc
2626 into the shared library. However, if we are linking with
2627 -Bsymbolic, we do not need to copy a reloc against a
2628 global symbol which is defined in an object we are
2629 including in the link (i.e., DEF_REGULAR is set). At
2630 this point we have not seen all the input files, so it is
2631 possible that DEF_REGULAR is not set now but will be set
2632 later (it is never cleared). In case of a weak definition,
2633 DEF_REGULAR may be cleared later by a strong definition in
2634 a shared library. We account for that possibility below by
2635 storing information in the relocs_copied field of the hash
2636 table entry. A similar situation occurs when creating
2637 shared libraries and symbol visibility changes render the
2640 If on the other hand, we are creating an executable, we
2641 may need to keep relocations for symbols satisfied by a
2642 dynamic library if we manage to avoid copy relocs for the
2645 Generate dynamic pointer relocation against STT_GNU_IFUNC
2646 symbol in the non-code section. */
2647 if ((bfd_link_pic (info
)
2648 && (! IS_X86_64_PCREL_TYPE (r_type
)
2650 && (! (bfd_link_pie (info
)
2651 || SYMBOLIC_BIND (info
, h
))
2652 || h
->root
.type
== bfd_link_hash_defweak
2653 || !h
->def_regular
))))
2655 && h
->type
== STT_GNU_IFUNC
2656 && r_type
== htab
->pointer_r_type
2657 && (sec
->flags
& SEC_CODE
) == 0)
2658 || (ELIMINATE_COPY_RELOCS
2659 && !bfd_link_pic (info
)
2661 && (h
->root
.type
== bfd_link_hash_defweak
2662 || !h
->def_regular
)))
2664 struct elf_dyn_relocs
*p
;
2665 struct elf_dyn_relocs
**head
;
2667 /* We must copy these reloc types into the output file.
2668 Create a reloc section in dynobj and make room for
2672 if (htab
->elf
.dynobj
== NULL
)
2673 htab
->elf
.dynobj
= abfd
;
2675 sreloc
= _bfd_elf_make_dynamic_reloc_section
2676 (sec
, htab
->elf
.dynobj
, ABI_64_P (abfd
) ? 3 : 2,
2677 abfd
, /*rela?*/ TRUE
);
2683 /* If this is a global symbol, we count the number of
2684 relocations we need for this symbol. */
2686 head
= &eh
->dyn_relocs
;
2689 /* Track dynamic relocs needed for local syms too.
2690 We really need local syms available to do this
2695 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
2700 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2704 /* Beware of type punned pointers vs strict aliasing
2706 vpp
= &(elf_section_data (s
)->local_dynrel
);
2707 head
= (struct elf_dyn_relocs
**)vpp
;
2711 if (p
== NULL
|| p
->sec
!= sec
)
2713 bfd_size_type amt
= sizeof *p
;
2715 p
= ((struct elf_dyn_relocs
*)
2716 bfd_alloc (htab
->elf
.dynobj
, amt
));
2727 /* Count size relocation as PC-relative relocation. */
2728 if (IS_X86_64_PCREL_TYPE (r_type
) || size_reloc
)
2733 /* This relocation describes the C++ object vtable hierarchy.
2734 Reconstruct it for later use during GC. */
2735 case R_X86_64_GNU_VTINHERIT
:
2736 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
2740 /* This relocation describes which C++ vtable entries are actually
2741 used. Record for later use during GC. */
2742 case R_X86_64_GNU_VTENTRY
:
2743 BFD_ASSERT (h
!= NULL
);
2745 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
2755 && h
->plt
.refcount
> 0
2756 && (((info
->flags
& DF_BIND_NOW
) && !h
->pointer_equality_needed
)
2757 || h
->got
.refcount
> 0)
2758 && htab
->plt_got
== NULL
)
2760 /* Create the GOT procedure linkage table. */
2761 unsigned int plt_got_align
;
2762 const struct elf_backend_data
*bed
;
2764 bed
= get_elf_backend_data (info
->output_bfd
);
2765 BFD_ASSERT (sizeof (elf_x86_64_legacy_plt2_entry
) == 8
2766 && (sizeof (elf_x86_64_bnd_plt2_entry
)
2767 == sizeof (elf_x86_64_legacy_plt2_entry
)));
2770 if (htab
->elf
.dynobj
== NULL
)
2771 htab
->elf
.dynobj
= abfd
;
2773 = bfd_make_section_anyway_with_flags (htab
->elf
.dynobj
,
2775 (bed
->dynamic_sec_flags
2780 if (htab
->plt_got
== NULL
2781 || !bfd_set_section_alignment (htab
->elf
.dynobj
,
2786 if (!info
->no_ld_generated_unwind_info
2787 && htab
->plt_got_eh_frame
== NULL
2788 && get_elf_x86_64_backend_data (abfd
)->eh_frame_plt_got
!= NULL
)
2790 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
2791 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2792 | SEC_LINKER_CREATED
);
2793 htab
->plt_got_eh_frame
2794 = bfd_make_section_anyway_with_flags (htab
->elf
.dynobj
,
2797 if (htab
->plt_got_eh_frame
== NULL
2798 || !bfd_set_section_alignment (htab
->elf
.dynobj
,
2799 htab
->plt_got_eh_frame
,
2800 ABI_64_P (htab
->elf
.dynobj
) ? 3 : 2))
2805 if ((r_type
== R_X86_64_GOTPCREL
2806 || r_type
== R_X86_64_GOTPCRELX
2807 || r_type
== R_X86_64_REX_GOTPCRELX
)
2808 && (h
== NULL
|| h
->type
!= STT_GNU_IFUNC
))
2809 sec
->need_convert_load
= 1;
2812 if (elf_section_data (sec
)->this_hdr
.contents
!= contents
)
2814 if (!info
->keep_memory
)
2818 /* Cache the section contents for elf_link_input_bfd. */
2819 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2826 if (elf_section_data (sec
)->this_hdr
.contents
!= contents
)
2828 sec
->check_relocs_failed
= 1;
2832 /* Return the section that should be marked against GC for a given
2836 elf_x86_64_gc_mark_hook (asection
*sec
,
2837 struct bfd_link_info
*info
,
2838 Elf_Internal_Rela
*rel
,
2839 struct elf_link_hash_entry
*h
,
2840 Elf_Internal_Sym
*sym
)
2843 switch (ELF32_R_TYPE (rel
->r_info
))
2845 case R_X86_64_GNU_VTINHERIT
:
2846 case R_X86_64_GNU_VTENTRY
:
2850 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
2853 /* Remove undefined weak symbol from the dynamic symbol table if it
2854 is resolved to 0. */
2857 elf_x86_64_fixup_symbol (struct bfd_link_info
*info
,
2858 struct elf_link_hash_entry
*h
)
2860 if (h
->dynindx
!= -1
2861 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
2862 elf_x86_64_hash_entry (h
)->has_got_reloc
,
2863 elf_x86_64_hash_entry (h
)))
2866 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
2872 /* Adjust a symbol defined by a dynamic object and referenced by a
2873 regular object. The current definition is in some section of the
2874 dynamic object, but we're not including those sections. We have to
2875 change the definition to something the rest of the link can
2879 elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2880 struct elf_link_hash_entry
*h
)
2882 struct elf_x86_64_link_hash_table
*htab
;
2884 struct elf_x86_64_link_hash_entry
*eh
;
2885 struct elf_dyn_relocs
*p
;
2887 /* STT_GNU_IFUNC symbol must go through PLT. */
2888 if (h
->type
== STT_GNU_IFUNC
)
2890 /* All local STT_GNU_IFUNC references must be treate as local
2891 calls via local PLT. */
2893 && SYMBOL_CALLS_LOCAL (info
, h
))
2895 bfd_size_type pc_count
= 0, count
= 0;
2896 struct elf_dyn_relocs
**pp
;
2898 eh
= (struct elf_x86_64_link_hash_entry
*) h
;
2899 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2901 pc_count
+= p
->pc_count
;
2902 p
->count
-= p
->pc_count
;
2911 if (pc_count
|| count
)
2916 /* Increment PLT reference count only for PC-relative
2919 if (h
->plt
.refcount
<= 0)
2920 h
->plt
.refcount
= 1;
2922 h
->plt
.refcount
+= 1;
2927 if (h
->plt
.refcount
<= 0)
2929 h
->plt
.offset
= (bfd_vma
) -1;
2935 /* If this is a function, put it in the procedure linkage table. We
2936 will fill in the contents of the procedure linkage table later,
2937 when we know the address of the .got section. */
2938 if (h
->type
== STT_FUNC
2941 if (h
->plt
.refcount
<= 0
2942 || SYMBOL_CALLS_LOCAL (info
, h
)
2943 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2944 && h
->root
.type
== bfd_link_hash_undefweak
))
2946 /* This case can occur if we saw a PLT32 reloc in an input
2947 file, but the symbol was never referred to by a dynamic
2948 object, or if all references were garbage collected. In
2949 such a case, we don't actually need to build a procedure
2950 linkage table, and we can just do a PC32 reloc instead. */
2951 h
->plt
.offset
= (bfd_vma
) -1;
2958 /* It's possible that we incorrectly decided a .plt reloc was
2959 needed for an R_X86_64_PC32 reloc to a non-function sym in
2960 check_relocs. We can't decide accurately between function and
2961 non-function syms in check-relocs; Objects loaded later in
2962 the link may change h->type. So fix it now. */
2963 h
->plt
.offset
= (bfd_vma
) -1;
2965 /* If this is a weak symbol, and there is a real definition, the
2966 processor independent code will have arranged for us to see the
2967 real definition first, and we can just use the same value. */
2968 if (h
->u
.weakdef
!= NULL
)
2970 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
2971 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
2972 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
2973 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
2974 if (ELIMINATE_COPY_RELOCS
|| info
->nocopyreloc
)
2976 eh
= (struct elf_x86_64_link_hash_entry
*) h
;
2977 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
2978 eh
->needs_copy
= h
->u
.weakdef
->needs_copy
;
2983 /* This is a reference to a symbol defined by a dynamic object which
2984 is not a function. */
2986 /* If we are creating a shared library, we must presume that the
2987 only references to the symbol are via the global offset table.
2988 For such cases we need not do anything here; the relocations will
2989 be handled correctly by relocate_section. */
2990 if (!bfd_link_executable (info
))
2993 /* If there are no references to this symbol that do not use the
2994 GOT, we don't need to generate a copy reloc. */
2995 if (!h
->non_got_ref
)
2998 /* If -z nocopyreloc was given, we won't generate them either. */
2999 if (info
->nocopyreloc
)
3005 if (ELIMINATE_COPY_RELOCS
)
3007 eh
= (struct elf_x86_64_link_hash_entry
*) h
;
3008 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
3010 s
= p
->sec
->output_section
;
3011 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
3015 /* If we didn't find any dynamic relocs in read-only sections, then
3016 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3024 /* We must allocate the symbol in our .dynbss section, which will
3025 become part of the .bss section of the executable. There will be
3026 an entry for this symbol in the .dynsym section. The dynamic
3027 object will contain position independent code, so all references
3028 from the dynamic object to this symbol will go through the global
3029 offset table. The dynamic linker will use the .dynsym entry to
3030 determine the address it must put in the global offset table, so
3031 both the dynamic object and the regular object will refer to the
3032 same memory location for the variable. */
3034 htab
= elf_x86_64_hash_table (info
);
3038 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
3039 to copy the initial value out of the dynamic object and into the
3040 runtime process image. */
3041 if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
3043 s
= htab
->elf
.sdynrelro
;
3044 srel
= htab
->elf
.sreldynrelro
;
3048 s
= htab
->elf
.sdynbss
;
3049 srel
= htab
->elf
.srelbss
;
3051 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
3053 const struct elf_backend_data
*bed
;
3054 bed
= get_elf_backend_data (info
->output_bfd
);
3055 srel
->size
+= bed
->s
->sizeof_rela
;
3059 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
3062 /* Allocate space in .plt, .got and associated reloc sections for
3066 elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
3068 struct bfd_link_info
*info
;
3069 struct elf_x86_64_link_hash_table
*htab
;
3070 struct elf_x86_64_link_hash_entry
*eh
;
3071 struct elf_dyn_relocs
*p
;
3072 const struct elf_backend_data
*bed
;
3073 unsigned int plt_entry_size
;
3074 bfd_boolean resolved_to_zero
;
3076 if (h
->root
.type
== bfd_link_hash_indirect
)
3079 eh
= (struct elf_x86_64_link_hash_entry
*) h
;
3081 info
= (struct bfd_link_info
*) inf
;
3082 htab
= elf_x86_64_hash_table (info
);
3085 bed
= get_elf_backend_data (info
->output_bfd
);
3086 plt_entry_size
= GET_PLT_ENTRY_SIZE (info
->output_bfd
);
3088 resolved_to_zero
= UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
3092 /* We can't use the GOT PLT if pointer equality is needed since
3093 finish_dynamic_symbol won't clear symbol value and the dynamic
3094 linker won't update the GOT slot. We will get into an infinite
3095 loop at run-time. */
3096 if (htab
->plt_got
!= NULL
3097 && h
->type
!= STT_GNU_IFUNC
3098 && !h
->pointer_equality_needed
3099 && h
->plt
.refcount
> 0
3100 && h
->got
.refcount
> 0)
3102 /* Don't use the regular PLT if there are both GOT and GOTPLT
3104 h
->plt
.offset
= (bfd_vma
) -1;
3106 /* Use the GOT PLT. */
3107 eh
->plt_got
.refcount
= 1;
3110 /* Clear the reference count of function pointer relocations if
3111 symbol isn't a normal function. */
3112 if (h
->type
!= STT_FUNC
)
3113 eh
->func_pointer_refcount
= 0;
3115 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
3116 here if it is defined and referenced in a non-shared object. */
3117 if (h
->type
== STT_GNU_IFUNC
3120 if (_bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
3122 &htab
->readonly_dynrelocs_against_ifunc
,
3125 GOT_ENTRY_SIZE
, TRUE
))
3127 asection
*s
= htab
->plt_bnd
;
3128 if (h
->plt
.offset
!= (bfd_vma
) -1 && s
!= NULL
)
3130 /* Use the .plt.bnd section if it is created. */
3131 eh
->plt_bnd
.offset
= s
->size
;
3133 /* Make room for this entry in the .plt.bnd section. */
3134 s
->size
+= sizeof (elf_x86_64_legacy_plt2_entry
);
3142 /* Don't create the PLT entry if there are only function pointer
3143 relocations which can be resolved at run-time. */
3144 else if (htab
->elf
.dynamic_sections_created
3145 && (h
->plt
.refcount
> eh
->func_pointer_refcount
3146 || eh
->plt_got
.refcount
> 0))
3148 bfd_boolean use_plt_got
;
3150 /* Clear the reference count of function pointer relocations
3152 eh
->func_pointer_refcount
= 0;
3154 if ((info
->flags
& DF_BIND_NOW
) && !h
->pointer_equality_needed
)
3156 /* Don't use the regular PLT for DF_BIND_NOW. */
3157 h
->plt
.offset
= (bfd_vma
) -1;
3159 /* Use the GOT PLT. */
3160 h
->got
.refcount
= 1;
3161 eh
->plt_got
.refcount
= 1;
3164 use_plt_got
= eh
->plt_got
.refcount
> 0;
3166 /* Make sure this symbol is output as a dynamic symbol.
3167 Undefined weak syms won't yet be marked as dynamic. */
3168 if (h
->dynindx
== -1
3170 && !resolved_to_zero
)
3172 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3176 if (bfd_link_pic (info
)
3177 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
3179 asection
*s
= htab
->elf
.splt
;
3180 asection
*bnd_s
= htab
->plt_bnd
;
3181 asection
*got_s
= htab
->plt_got
;
3183 /* If this is the first .plt entry, make room for the special
3184 first entry. The .plt section is used by prelink to undo
3185 prelinking for dynamic relocations. */
3187 s
->size
= plt_entry_size
;
3190 eh
->plt_got
.offset
= got_s
->size
;
3193 h
->plt
.offset
= s
->size
;
3195 eh
->plt_bnd
.offset
= bnd_s
->size
;
3198 /* If this symbol is not defined in a regular file, and we are
3199 not generating a shared library, then set the symbol to this
3200 location in the .plt. This is required to make function
3201 pointers compare as equal between the normal executable and
3202 the shared library. */
3203 if (! bfd_link_pic (info
)
3208 /* We need to make a call to the entry of the GOT PLT
3209 instead of regular PLT entry. */
3210 h
->root
.u
.def
.section
= got_s
;
3211 h
->root
.u
.def
.value
= eh
->plt_got
.offset
;
3217 /* We need to make a call to the entry of the second
3218 PLT instead of regular PLT entry. */
3219 h
->root
.u
.def
.section
= bnd_s
;
3220 h
->root
.u
.def
.value
= eh
->plt_bnd
.offset
;
3224 h
->root
.u
.def
.section
= s
;
3225 h
->root
.u
.def
.value
= h
->plt
.offset
;
3230 /* Make room for this entry. */
3232 got_s
->size
+= sizeof (elf_x86_64_legacy_plt2_entry
);
3235 s
->size
+= plt_entry_size
;
3237 bnd_s
->size
+= sizeof (elf_x86_64_legacy_plt2_entry
);
3239 /* We also need to make an entry in the .got.plt section,
3240 which will be placed in the .got section by the linker
3242 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
3244 /* There should be no PLT relocation against resolved
3245 undefined weak symbol in executable. */
3246 if (!resolved_to_zero
)
3248 /* We also need to make an entry in the .rela.plt
3250 htab
->elf
.srelplt
->size
+= bed
->s
->sizeof_rela
;
3251 htab
->elf
.srelplt
->reloc_count
++;
3257 eh
->plt_got
.offset
= (bfd_vma
) -1;
3258 h
->plt
.offset
= (bfd_vma
) -1;
3264 eh
->plt_got
.offset
= (bfd_vma
) -1;
3265 h
->plt
.offset
= (bfd_vma
) -1;
3269 eh
->tlsdesc_got
= (bfd_vma
) -1;
3271 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
3272 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
3273 if (h
->got
.refcount
> 0
3274 && bfd_link_executable (info
)
3276 && elf_x86_64_hash_entry (h
)->tls_type
== GOT_TLS_IE
)
3278 h
->got
.offset
= (bfd_vma
) -1;
3280 else if (h
->got
.refcount
> 0)
3284 int tls_type
= elf_x86_64_hash_entry (h
)->tls_type
;
3286 /* Make sure this symbol is output as a dynamic symbol.
3287 Undefined weak syms won't yet be marked as dynamic. */
3288 if (h
->dynindx
== -1
3290 && !resolved_to_zero
)
3292 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3296 if (GOT_TLS_GDESC_P (tls_type
))
3298 eh
->tlsdesc_got
= htab
->elf
.sgotplt
->size
3299 - elf_x86_64_compute_jump_table_size (htab
);
3300 htab
->elf
.sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
3301 h
->got
.offset
= (bfd_vma
) -2;
3303 if (! GOT_TLS_GDESC_P (tls_type
)
3304 || GOT_TLS_GD_P (tls_type
))
3307 h
->got
.offset
= s
->size
;
3308 s
->size
+= GOT_ENTRY_SIZE
;
3309 if (GOT_TLS_GD_P (tls_type
))
3310 s
->size
+= GOT_ENTRY_SIZE
;
3312 dyn
= htab
->elf
.dynamic_sections_created
;
3313 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
3314 and two if global. R_X86_64_GOTTPOFF needs one dynamic
3315 relocation. No dynamic relocation against resolved undefined
3316 weak symbol in executable. */
3317 if ((GOT_TLS_GD_P (tls_type
) && h
->dynindx
== -1)
3318 || tls_type
== GOT_TLS_IE
)
3319 htab
->elf
.srelgot
->size
+= bed
->s
->sizeof_rela
;
3320 else if (GOT_TLS_GD_P (tls_type
))
3321 htab
->elf
.srelgot
->size
+= 2 * bed
->s
->sizeof_rela
;
3322 else if (! GOT_TLS_GDESC_P (tls_type
)
3323 && ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3324 && !resolved_to_zero
)
3325 || h
->root
.type
!= bfd_link_hash_undefweak
)
3326 && (bfd_link_pic (info
)
3327 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
3328 htab
->elf
.srelgot
->size
+= bed
->s
->sizeof_rela
;
3329 if (GOT_TLS_GDESC_P (tls_type
))
3331 htab
->elf
.srelplt
->size
+= bed
->s
->sizeof_rela
;
3332 htab
->tlsdesc_plt
= (bfd_vma
) -1;
3336 h
->got
.offset
= (bfd_vma
) -1;
3338 if (eh
->dyn_relocs
== NULL
)
3341 /* In the shared -Bsymbolic case, discard space allocated for
3342 dynamic pc-relative relocs against symbols which turn out to be
3343 defined in regular objects. For the normal shared case, discard
3344 space for pc-relative relocs that have become local due to symbol
3345 visibility changes. */
3347 if (bfd_link_pic (info
))
3349 /* Relocs that use pc_count are those that appear on a call
3350 insn, or certain REL relocs that can generated via assembly.
3351 We want calls to protected symbols to resolve directly to the
3352 function rather than going via the plt. If people want
3353 function pointer comparisons to work as expected then they
3354 should avoid writing weird assembly. */
3355 if (SYMBOL_CALLS_LOCAL (info
, h
))
3357 struct elf_dyn_relocs
**pp
;
3359 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
3361 p
->count
-= p
->pc_count
;
3370 /* Also discard relocs on undefined weak syms with non-default
3371 visibility or in PIE. */
3372 if (eh
->dyn_relocs
!= NULL
)
3374 if (h
->root
.type
== bfd_link_hash_undefweak
)
3376 /* Undefined weak symbol is never bound locally in shared
3378 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3379 || resolved_to_zero
)
3380 eh
->dyn_relocs
= NULL
;
3381 else if (h
->dynindx
== -1
3382 && ! h
->forced_local
3383 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
3386 /* For PIE, discard space for pc-relative relocs against
3387 symbols which turn out to need copy relocs. */
3388 else if (bfd_link_executable (info
)
3389 && (h
->needs_copy
|| eh
->needs_copy
)
3393 struct elf_dyn_relocs
**pp
;
3395 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
3397 if (p
->pc_count
!= 0)
3405 else if (ELIMINATE_COPY_RELOCS
)
3407 /* For the non-shared case, discard space for relocs against
3408 symbols which turn out to need copy relocs or are not
3409 dynamic. Keep dynamic relocations for run-time function
3410 pointer initialization. */
3412 if ((!h
->non_got_ref
3413 || eh
->func_pointer_refcount
> 0
3414 || (h
->root
.type
== bfd_link_hash_undefweak
3415 && !resolved_to_zero
))
3418 || (htab
->elf
.dynamic_sections_created
3419 && (h
->root
.type
== bfd_link_hash_undefweak
3420 || h
->root
.type
== bfd_link_hash_undefined
))))
3422 /* Make sure this symbol is output as a dynamic symbol.
3423 Undefined weak syms won't yet be marked as dynamic. */
3424 if (h
->dynindx
== -1
3425 && ! h
->forced_local
3426 && ! resolved_to_zero
3427 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
3430 /* If that succeeded, we know we'll be keeping all the
3432 if (h
->dynindx
!= -1)
3436 eh
->dyn_relocs
= NULL
;
3437 eh
->func_pointer_refcount
= 0;
3442 /* Finally, allocate space. */
3443 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
3447 sreloc
= elf_section_data (p
->sec
)->sreloc
;
3449 BFD_ASSERT (sreloc
!= NULL
);
3451 sreloc
->size
+= p
->count
* bed
->s
->sizeof_rela
;
3457 /* Allocate space in .plt, .got and associated reloc sections for
3458 local dynamic relocs. */
3461 elf_x86_64_allocate_local_dynrelocs (void **slot
, void *inf
)
3463 struct elf_link_hash_entry
*h
3464 = (struct elf_link_hash_entry
*) *slot
;
3466 if (h
->type
!= STT_GNU_IFUNC
3470 || h
->root
.type
!= bfd_link_hash_defined
)
3473 return elf_x86_64_allocate_dynrelocs (h
, inf
);
3476 /* Find any dynamic relocs that apply to read-only sections. */
3479 elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry
*h
,
3482 struct elf_x86_64_link_hash_entry
*eh
;
3483 struct elf_dyn_relocs
*p
;
3485 /* Skip local IFUNC symbols. */
3486 if (h
->forced_local
&& h
->type
== STT_GNU_IFUNC
)
3489 eh
= (struct elf_x86_64_link_hash_entry
*) h
;
3490 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
3492 asection
*s
= p
->sec
->output_section
;
3494 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
3496 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3498 info
->flags
|= DF_TEXTREL
;
3500 if ((info
->warn_shared_textrel
&& bfd_link_pic (info
))
3501 || info
->error_textrel
)
3502 /* xgettext:c-format */
3503 info
->callbacks
->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
3504 p
->sec
->owner
, h
->root
.root
.string
,
3507 /* Not an error, just cut short the traversal. */
3514 /* Convert load via the GOT slot to load immediate. */
3517 elf_x86_64_convert_load (bfd
*abfd
, asection
*sec
,
3518 struct bfd_link_info
*link_info
)
3520 Elf_Internal_Shdr
*symtab_hdr
;
3521 Elf_Internal_Rela
*internal_relocs
;
3522 Elf_Internal_Rela
*irel
, *irelend
;
3524 struct elf_x86_64_link_hash_table
*htab
;
3525 bfd_boolean changed
;
3526 bfd_signed_vma
*local_got_refcounts
;
3528 /* Don't even try to convert non-ELF outputs. */
3529 if (!is_elf_hash_table (link_info
->hash
))
3532 /* Nothing to do if there is no need or no output. */
3533 if ((sec
->flags
& (SEC_CODE
| SEC_RELOC
)) != (SEC_CODE
| SEC_RELOC
)
3534 || sec
->need_convert_load
== 0
3535 || bfd_is_abs_section (sec
->output_section
))
3538 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3540 /* Load the relocations for this section. */
3541 internal_relocs
= (_bfd_elf_link_read_relocs
3542 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
3543 link_info
->keep_memory
));
3544 if (internal_relocs
== NULL
)
3548 htab
= elf_x86_64_hash_table (link_info
);
3549 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3551 /* Get the section contents. */
3552 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
3553 contents
= elf_section_data (sec
)->this_hdr
.contents
;
3556 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
3560 irelend
= internal_relocs
+ sec
->reloc_count
;
3561 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
3563 unsigned int r_type
= ELF32_R_TYPE (irel
->r_info
);
3564 unsigned int r_symndx
;
3565 struct elf_link_hash_entry
*h
;
3566 bfd_boolean converted
;
3568 if (r_type
!= R_X86_64_GOTPCRELX
3569 && r_type
!= R_X86_64_REX_GOTPCRELX
3570 && r_type
!= R_X86_64_GOTPCREL
)
3573 r_symndx
= htab
->r_sym (irel
->r_info
);
3574 if (r_symndx
< symtab_hdr
->sh_info
)
3575 h
= elf_x86_64_get_local_sym_hash (htab
, sec
->owner
,
3576 (const Elf_Internal_Rela
*) irel
,
3580 h
= elf_sym_hashes (abfd
)[r_symndx
- symtab_hdr
->sh_info
];
3581 while (h
->root
.type
== bfd_link_hash_indirect
3582 || h
->root
.type
== bfd_link_hash_warning
)
3583 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3586 /* STT_GNU_IFUNC must keep GOTPCREL relocations. */
3587 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
3591 if (!elf_x86_64_convert_load_reloc (abfd
, sec
, contents
, irel
, h
,
3592 &converted
, link_info
))
3597 changed
= converted
;
3600 if (h
->got
.refcount
> 0)
3601 h
->got
.refcount
-= 1;
3605 if (local_got_refcounts
!= NULL
3606 && local_got_refcounts
[r_symndx
] > 0)
3607 local_got_refcounts
[r_symndx
] -= 1;
3612 if (contents
!= NULL
3613 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3615 if (!changed
&& !link_info
->keep_memory
)
3619 /* Cache the section contents for elf_link_input_bfd. */
3620 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3624 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
3627 free (internal_relocs
);
3629 elf_section_data (sec
)->relocs
= internal_relocs
;
3635 if (contents
!= NULL
3636 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3638 if (internal_relocs
!= NULL
3639 && elf_section_data (sec
)->relocs
!= internal_relocs
)
3640 free (internal_relocs
);
3644 /* Set the sizes of the dynamic sections. */
3647 elf_x86_64_size_dynamic_sections (bfd
*output_bfd
,
3648 struct bfd_link_info
*info
)
3650 struct elf_x86_64_link_hash_table
*htab
;
3655 const struct elf_backend_data
*bed
;
3657 htab
= elf_x86_64_hash_table (info
);
3660 bed
= get_elf_backend_data (output_bfd
);
3662 dynobj
= htab
->elf
.dynobj
;
3666 /* Set up .got offsets for local syms, and space for local dynamic
3668 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
3670 bfd_signed_vma
*local_got
;
3671 bfd_signed_vma
*end_local_got
;
3672 char *local_tls_type
;
3673 bfd_vma
*local_tlsdesc_gotent
;
3674 bfd_size_type locsymcount
;
3675 Elf_Internal_Shdr
*symtab_hdr
;
3678 if (! is_x86_64_elf (ibfd
))
3681 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3683 struct elf_dyn_relocs
*p
;
3685 if (!elf_x86_64_convert_load (ibfd
, s
, info
))
3688 for (p
= (struct elf_dyn_relocs
*)
3689 (elf_section_data (s
)->local_dynrel
);
3693 if (!bfd_is_abs_section (p
->sec
)
3694 && bfd_is_abs_section (p
->sec
->output_section
))
3696 /* Input section has been discarded, either because
3697 it is a copy of a linkonce section or due to
3698 linker script /DISCARD/, so we'll be discarding
3701 else if (p
->count
!= 0)
3703 srel
= elf_section_data (p
->sec
)->sreloc
;
3704 srel
->size
+= p
->count
* bed
->s
->sizeof_rela
;
3705 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0
3706 && (info
->flags
& DF_TEXTREL
) == 0)
3708 info
->flags
|= DF_TEXTREL
;
3709 if ((info
->warn_shared_textrel
&& bfd_link_pic (info
))
3710 || info
->error_textrel
)
3711 /* xgettext:c-format */
3712 info
->callbacks
->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3713 p
->sec
->owner
, p
->sec
);
3719 local_got
= elf_local_got_refcounts (ibfd
);
3723 symtab_hdr
= &elf_symtab_hdr (ibfd
);
3724 locsymcount
= symtab_hdr
->sh_info
;
3725 end_local_got
= local_got
+ locsymcount
;
3726 local_tls_type
= elf_x86_64_local_got_tls_type (ibfd
);
3727 local_tlsdesc_gotent
= elf_x86_64_local_tlsdesc_gotent (ibfd
);
3729 srel
= htab
->elf
.srelgot
;
3730 for (; local_got
< end_local_got
;
3731 ++local_got
, ++local_tls_type
, ++local_tlsdesc_gotent
)
3733 *local_tlsdesc_gotent
= (bfd_vma
) -1;
3736 if (GOT_TLS_GDESC_P (*local_tls_type
))
3738 *local_tlsdesc_gotent
= htab
->elf
.sgotplt
->size
3739 - elf_x86_64_compute_jump_table_size (htab
);
3740 htab
->elf
.sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
3741 *local_got
= (bfd_vma
) -2;
3743 if (! GOT_TLS_GDESC_P (*local_tls_type
)
3744 || GOT_TLS_GD_P (*local_tls_type
))
3746 *local_got
= s
->size
;
3747 s
->size
+= GOT_ENTRY_SIZE
;
3748 if (GOT_TLS_GD_P (*local_tls_type
))
3749 s
->size
+= GOT_ENTRY_SIZE
;
3751 if (bfd_link_pic (info
)
3752 || GOT_TLS_GD_ANY_P (*local_tls_type
)
3753 || *local_tls_type
== GOT_TLS_IE
)
3755 if (GOT_TLS_GDESC_P (*local_tls_type
))
3757 htab
->elf
.srelplt
->size
3758 += bed
->s
->sizeof_rela
;
3759 htab
->tlsdesc_plt
= (bfd_vma
) -1;
3761 if (! GOT_TLS_GDESC_P (*local_tls_type
)
3762 || GOT_TLS_GD_P (*local_tls_type
))
3763 srel
->size
+= bed
->s
->sizeof_rela
;
3767 *local_got
= (bfd_vma
) -1;
3771 if (htab
->tls_ld_got
.refcount
> 0)
3773 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
3775 htab
->tls_ld_got
.offset
= htab
->elf
.sgot
->size
;
3776 htab
->elf
.sgot
->size
+= 2 * GOT_ENTRY_SIZE
;
3777 htab
->elf
.srelgot
->size
+= bed
->s
->sizeof_rela
;
3780 htab
->tls_ld_got
.offset
= -1;
3782 /* Allocate global sym .plt and .got entries, and space for global
3783 sym dynamic relocs. */
3784 elf_link_hash_traverse (&htab
->elf
, elf_x86_64_allocate_dynrelocs
,
3787 /* Allocate .plt and .got entries, and space for local symbols. */
3788 htab_traverse (htab
->loc_hash_table
,
3789 elf_x86_64_allocate_local_dynrelocs
,
3792 /* For every jump slot reserved in the sgotplt, reloc_count is
3793 incremented. However, when we reserve space for TLS descriptors,
3794 it's not incremented, so in order to compute the space reserved
3795 for them, it suffices to multiply the reloc count by the jump
3798 PR ld/13302: We start next_irelative_index at the end of .rela.plt
3799 so that R_X86_64_IRELATIVE entries come last. */
3800 if (htab
->elf
.srelplt
)
3802 htab
->sgotplt_jump_table_size
3803 = elf_x86_64_compute_jump_table_size (htab
);
3804 htab
->next_irelative_index
= htab
->elf
.srelplt
->reloc_count
- 1;
3806 else if (htab
->elf
.irelplt
)
3807 htab
->next_irelative_index
= htab
->elf
.irelplt
->reloc_count
- 1;
3809 if (htab
->tlsdesc_plt
)
3811 /* If we're not using lazy TLS relocations, don't generate the
3812 PLT and GOT entries they require. */
3813 if ((info
->flags
& DF_BIND_NOW
))
3814 htab
->tlsdesc_plt
= 0;
3817 htab
->tlsdesc_got
= htab
->elf
.sgot
->size
;
3818 htab
->elf
.sgot
->size
+= GOT_ENTRY_SIZE
;
3819 /* Reserve room for the initial entry.
3820 FIXME: we could probably do away with it in this case. */
3821 if (htab
->elf
.splt
->size
== 0)
3822 htab
->elf
.splt
->size
+= GET_PLT_ENTRY_SIZE (output_bfd
);
3823 htab
->tlsdesc_plt
= htab
->elf
.splt
->size
;
3824 htab
->elf
.splt
->size
+= GET_PLT_ENTRY_SIZE (output_bfd
);
3828 if (htab
->elf
.sgotplt
)
3830 /* Don't allocate .got.plt section if there are no GOT nor PLT
3831 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
3832 if ((htab
->elf
.hgot
== NULL
3833 || !htab
->elf
.hgot
->ref_regular_nonweak
)
3834 && (htab
->elf
.sgotplt
->size
3835 == get_elf_backend_data (output_bfd
)->got_header_size
)
3836 && (htab
->elf
.splt
== NULL
3837 || htab
->elf
.splt
->size
== 0)
3838 && (htab
->elf
.sgot
== NULL
3839 || htab
->elf
.sgot
->size
== 0)
3840 && (htab
->elf
.iplt
== NULL
3841 || htab
->elf
.iplt
->size
== 0)
3842 && (htab
->elf
.igotplt
== NULL
3843 || htab
->elf
.igotplt
->size
== 0))
3844 htab
->elf
.sgotplt
->size
= 0;
3847 if (_bfd_elf_eh_frame_present (info
))
3849 if (htab
->plt_eh_frame
!= NULL
3850 && htab
->elf
.splt
!= NULL
3851 && htab
->elf
.splt
->size
!= 0
3852 && !bfd_is_abs_section (htab
->elf
.splt
->output_section
))
3854 const struct elf_x86_64_backend_data
*arch_data
3855 = get_elf_x86_64_arch_data (bed
);
3856 htab
->plt_eh_frame
->size
= arch_data
->eh_frame_plt_size
;
3859 if (htab
->plt_got_eh_frame
!= NULL
3860 && htab
->plt_got
!= NULL
3861 && htab
->plt_got
->size
!= 0
3862 && !bfd_is_abs_section (htab
->plt_got
->output_section
))
3864 const struct elf_x86_64_backend_data
*arch_data
3865 = get_elf_x86_64_arch_data (bed
);
3866 htab
->plt_got_eh_frame
->size
3867 = arch_data
->eh_frame_plt_got_size
;
3871 /* We now have determined the sizes of the various dynamic sections.
3872 Allocate memory for them. */
3874 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
3876 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
3879 if (s
== htab
->elf
.splt
3880 || s
== htab
->elf
.sgot
3881 || s
== htab
->elf
.sgotplt
3882 || s
== htab
->elf
.iplt
3883 || s
== htab
->elf
.igotplt
3884 || s
== htab
->plt_bnd
3885 || s
== htab
->plt_got
3886 || s
== htab
->plt_eh_frame
3887 || s
== htab
->plt_got_eh_frame
3888 || s
== htab
->elf
.sdynbss
3889 || s
== htab
->elf
.sdynrelro
)
3891 /* Strip this section if we don't need it; see the
3894 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
3896 if (s
->size
!= 0 && s
!= htab
->elf
.srelplt
)
3899 /* We use the reloc_count field as a counter if we need
3900 to copy relocs into the output file. */
3901 if (s
!= htab
->elf
.srelplt
)
3906 /* It's not one of our sections, so don't allocate space. */
3912 /* If we don't need this section, strip it from the
3913 output file. This is mostly to handle .rela.bss and
3914 .rela.plt. We must create both sections in
3915 create_dynamic_sections, because they must be created
3916 before the linker maps input sections to output
3917 sections. The linker does that before
3918 adjust_dynamic_symbol is called, and it is that
3919 function which decides whether anything needs to go
3920 into these sections. */
3922 s
->flags
|= SEC_EXCLUDE
;
3926 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3929 /* Allocate memory for the section contents. We use bfd_zalloc
3930 here in case unused entries are not reclaimed before the
3931 section's contents are written out. This should not happen,
3932 but this way if it does, we get a R_X86_64_NONE reloc instead
3934 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
3935 if (s
->contents
== NULL
)
3939 if (htab
->plt_eh_frame
!= NULL
3940 && htab
->plt_eh_frame
->contents
!= NULL
)
3942 const struct elf_x86_64_backend_data
*arch_data
3943 = get_elf_x86_64_arch_data (bed
);
3945 memcpy (htab
->plt_eh_frame
->contents
,
3946 arch_data
->eh_frame_plt
, htab
->plt_eh_frame
->size
);
3947 bfd_put_32 (dynobj
, htab
->elf
.splt
->size
,
3948 htab
->plt_eh_frame
->contents
+ PLT_FDE_LEN_OFFSET
);
3951 if (htab
->plt_got_eh_frame
!= NULL
3952 && htab
->plt_got_eh_frame
->contents
!= NULL
)
3954 const struct elf_x86_64_backend_data
*arch_data
3955 = get_elf_x86_64_arch_data (bed
);
3957 memcpy (htab
->plt_got_eh_frame
->contents
,
3958 arch_data
->eh_frame_plt_got
,
3959 htab
->plt_got_eh_frame
->size
);
3960 bfd_put_32 (dynobj
, htab
->plt_got
->size
,
3961 (htab
->plt_got_eh_frame
->contents
3962 + PLT_FDE_LEN_OFFSET
));
3965 if (htab
->elf
.dynamic_sections_created
)
3967 /* Add some entries to the .dynamic section. We fill in the
3968 values later, in elf_x86_64_finish_dynamic_sections, but we
3969 must add the entries now so that we get the correct size for
3970 the .dynamic section. The DT_DEBUG entry is filled in by the
3971 dynamic linker and used by the debugger. */
3972 #define add_dynamic_entry(TAG, VAL) \
3973 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3975 if (bfd_link_executable (info
))
3977 if (!add_dynamic_entry (DT_DEBUG
, 0))
3981 if (htab
->elf
.splt
->size
!= 0)
3983 /* DT_PLTGOT is used by prelink even if there is no PLT
3985 if (!add_dynamic_entry (DT_PLTGOT
, 0))
3988 if (htab
->elf
.srelplt
->size
!= 0)
3990 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
3991 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3992 || !add_dynamic_entry (DT_JMPREL
, 0))
3996 if (htab
->tlsdesc_plt
3997 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
3998 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
4004 if (!add_dynamic_entry (DT_RELA
, 0)
4005 || !add_dynamic_entry (DT_RELASZ
, 0)
4006 || !add_dynamic_entry (DT_RELAENT
, bed
->s
->sizeof_rela
))
4009 /* If any dynamic relocs apply to a read-only section,
4010 then we need a DT_TEXTREL entry. */
4011 if ((info
->flags
& DF_TEXTREL
) == 0)
4012 elf_link_hash_traverse (&htab
->elf
,
4013 elf_x86_64_readonly_dynrelocs
,
4016 if ((info
->flags
& DF_TEXTREL
) != 0)
4018 if (htab
->readonly_dynrelocs_against_ifunc
)
4020 info
->callbacks
->einfo
4021 (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
4022 bfd_set_error (bfd_error_bad_value
);
4026 if (!add_dynamic_entry (DT_TEXTREL
, 0))
4031 #undef add_dynamic_entry
4037 elf_x86_64_always_size_sections (bfd
*output_bfd
,
4038 struct bfd_link_info
*info
)
4040 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
4044 struct elf_link_hash_entry
*tlsbase
;
4046 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
4047 "_TLS_MODULE_BASE_",
4048 FALSE
, FALSE
, FALSE
);
4050 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
4052 struct elf_x86_64_link_hash_table
*htab
;
4053 struct bfd_link_hash_entry
*bh
= NULL
;
4054 const struct elf_backend_data
*bed
4055 = get_elf_backend_data (output_bfd
);
4057 htab
= elf_x86_64_hash_table (info
);
4061 if (!(_bfd_generic_link_add_one_symbol
4062 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
4063 tls_sec
, 0, NULL
, FALSE
,
4064 bed
->collect
, &bh
)))
4067 htab
->tls_module_base
= bh
;
4069 tlsbase
= (struct elf_link_hash_entry
*)bh
;
4070 tlsbase
->def_regular
= 1;
4071 tlsbase
->other
= STV_HIDDEN
;
4072 tlsbase
->root
.linker_def
= 1;
4073 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
4080 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
4081 executables. Rather than setting it to the beginning of the TLS
4082 section, we have to set it to the end. This function may be called
4083 multiple times, it is idempotent. */
4086 elf_x86_64_set_tls_module_base (struct bfd_link_info
*info
)
4088 struct elf_x86_64_link_hash_table
*htab
;
4089 struct bfd_link_hash_entry
*base
;
4091 if (!bfd_link_executable (info
))
4094 htab
= elf_x86_64_hash_table (info
);
4098 base
= htab
->tls_module_base
;
4102 base
->u
.def
.value
= htab
->elf
.tls_size
;
4105 /* Return the base VMA address which should be subtracted from real addresses
4106 when resolving @dtpoff relocation.
4107 This is PT_TLS segment p_vaddr. */
4110 elf_x86_64_dtpoff_base (struct bfd_link_info
*info
)
4112 /* If tls_sec is NULL, we should have signalled an error already. */
4113 if (elf_hash_table (info
)->tls_sec
== NULL
)
4115 return elf_hash_table (info
)->tls_sec
->vma
;
4118 /* Return the relocation value for @tpoff relocation
4119 if STT_TLS virtual address is ADDRESS. */
4122 elf_x86_64_tpoff (struct bfd_link_info
*info
, bfd_vma address
)
4124 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4125 const struct elf_backend_data
*bed
= get_elf_backend_data (info
->output_bfd
);
4126 bfd_vma static_tls_size
;
4128 /* If tls_segment is NULL, we should have signalled an error already. */
4129 if (htab
->tls_sec
== NULL
)
4132 /* Consider special static TLS alignment requirements. */
4133 static_tls_size
= BFD_ALIGN (htab
->tls_size
, bed
->static_tls_alignment
);
4134 return address
- static_tls_size
- htab
->tls_sec
->vma
;
4137 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
4141 is_32bit_relative_branch (bfd_byte
*contents
, bfd_vma offset
)
4143 /* Opcode Instruction
4146 0x0f 0x8x conditional jump */
4148 && (contents
[offset
- 1] == 0xe8
4149 || contents
[offset
- 1] == 0xe9))
4151 && contents
[offset
- 2] == 0x0f
4152 && (contents
[offset
- 1] & 0xf0) == 0x80));
4155 /* Relocate an x86_64 ELF section. */
4158 elf_x86_64_relocate_section (bfd
*output_bfd
,
4159 struct bfd_link_info
*info
,
4161 asection
*input_section
,
4163 Elf_Internal_Rela
*relocs
,
4164 Elf_Internal_Sym
*local_syms
,
4165 asection
**local_sections
)
4167 struct elf_x86_64_link_hash_table
*htab
;
4168 Elf_Internal_Shdr
*symtab_hdr
;
4169 struct elf_link_hash_entry
**sym_hashes
;
4170 bfd_vma
*local_got_offsets
;
4171 bfd_vma
*local_tlsdesc_gotents
;
4172 Elf_Internal_Rela
*rel
;
4173 Elf_Internal_Rela
*wrel
;
4174 Elf_Internal_Rela
*relend
;
4175 const unsigned int plt_entry_size
= GET_PLT_ENTRY_SIZE (info
->output_bfd
);
4177 BFD_ASSERT (is_x86_64_elf (input_bfd
));
4179 /* Skip if check_relocs failed. */
4180 if (input_section
->check_relocs_failed
)
4183 htab
= elf_x86_64_hash_table (info
);
4186 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
4187 sym_hashes
= elf_sym_hashes (input_bfd
);
4188 local_got_offsets
= elf_local_got_offsets (input_bfd
);
4189 local_tlsdesc_gotents
= elf_x86_64_local_tlsdesc_gotent (input_bfd
);
4191 elf_x86_64_set_tls_module_base (info
);
4193 rel
= wrel
= relocs
;
4194 relend
= relocs
+ input_section
->reloc_count
;
4195 for (; rel
< relend
; wrel
++, rel
++)
4197 unsigned int r_type
;
4198 reloc_howto_type
*howto
;
4199 unsigned long r_symndx
;
4200 struct elf_link_hash_entry
*h
;
4201 struct elf_x86_64_link_hash_entry
*eh
;
4202 Elf_Internal_Sym
*sym
;
4204 bfd_vma off
, offplt
, plt_offset
;
4206 bfd_boolean unresolved_reloc
;
4207 bfd_reloc_status_type r
;
4209 asection
*base_got
, *resolved_plt
;
4211 bfd_boolean resolved_to_zero
;
4213 r_type
= ELF32_R_TYPE (rel
->r_info
);
4214 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
4215 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
4222 if (r_type
>= (int) R_X86_64_standard
)
4225 /* xgettext:c-format */
4226 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
4227 input_bfd
, input_section
, r_type
);
4228 bfd_set_error (bfd_error_bad_value
);
4232 if (r_type
!= (int) R_X86_64_32
4233 || ABI_64_P (output_bfd
))
4234 howto
= x86_64_elf_howto_table
+ r_type
;
4236 howto
= (x86_64_elf_howto_table
4237 + ARRAY_SIZE (x86_64_elf_howto_table
) - 1);
4238 r_symndx
= htab
->r_sym (rel
->r_info
);
4242 unresolved_reloc
= FALSE
;
4243 if (r_symndx
< symtab_hdr
->sh_info
)
4245 sym
= local_syms
+ r_symndx
;
4246 sec
= local_sections
[r_symndx
];
4248 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
,
4250 st_size
= sym
->st_size
;
4252 /* Relocate against local STT_GNU_IFUNC symbol. */
4253 if (!bfd_link_relocatable (info
)
4254 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
4256 h
= elf_x86_64_get_local_sym_hash (htab
, input_bfd
,
4261 /* Set STT_GNU_IFUNC symbol value. */
4262 h
->root
.u
.def
.value
= sym
->st_value
;
4263 h
->root
.u
.def
.section
= sec
;
4268 bfd_boolean warned ATTRIBUTE_UNUSED
;
4269 bfd_boolean ignored ATTRIBUTE_UNUSED
;
4271 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
4272 r_symndx
, symtab_hdr
, sym_hashes
,
4274 unresolved_reloc
, warned
, ignored
);
4278 if (sec
!= NULL
&& discarded_section (sec
))
4280 _bfd_clear_contents (howto
, input_bfd
, input_section
,
4281 contents
+ rel
->r_offset
);
4282 wrel
->r_offset
= rel
->r_offset
;
4286 /* For ld -r, remove relocations in debug sections against
4287 sections defined in discarded sections. Not done for
4288 eh_frame editing code expects to be present. */
4289 if (bfd_link_relocatable (info
)
4290 && (input_section
->flags
& SEC_DEBUGGING
))
4296 if (bfd_link_relocatable (info
))
4303 if (rel
->r_addend
== 0 && !ABI_64_P (output_bfd
))
4305 if (r_type
== R_X86_64_64
)
4307 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
4308 zero-extend it to 64bit if addend is zero. */
4309 r_type
= R_X86_64_32
;
4310 memset (contents
+ rel
->r_offset
+ 4, 0, 4);
4312 else if (r_type
== R_X86_64_SIZE64
)
4314 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
4315 zero-extend it to 64bit if addend is zero. */
4316 r_type
= R_X86_64_SIZE32
;
4317 memset (contents
+ rel
->r_offset
+ 4, 0, 4);
4321 eh
= (struct elf_x86_64_link_hash_entry
*) h
;
4323 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
4324 it here if it is defined in a non-shared object. */
4326 && h
->type
== STT_GNU_IFUNC
4332 if ((input_section
->flags
& SEC_ALLOC
) == 0)
4334 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4335 sections because such sections are not SEC_ALLOC and
4336 thus ld.so will not process them. */
4337 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
4347 case R_X86_64_GOTPCREL
:
4348 case R_X86_64_GOTPCRELX
:
4349 case R_X86_64_REX_GOTPCRELX
:
4350 case R_X86_64_GOTPCREL64
:
4351 base_got
= htab
->elf
.sgot
;
4352 off
= h
->got
.offset
;
4354 if (base_got
== NULL
)
4357 if (off
== (bfd_vma
) -1)
4359 /* We can't use h->got.offset here to save state, or
4360 even just remember the offset, as finish_dynamic_symbol
4361 would use that as offset into .got. */
4363 if (h
->plt
.offset
== (bfd_vma
) -1)
4366 if (htab
->elf
.splt
!= NULL
)
4368 plt_index
= h
->plt
.offset
/ plt_entry_size
- 1;
4369 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
4370 base_got
= htab
->elf
.sgotplt
;
4374 plt_index
= h
->plt
.offset
/ plt_entry_size
;
4375 off
= plt_index
* GOT_ENTRY_SIZE
;
4376 base_got
= htab
->elf
.igotplt
;
4379 if (h
->dynindx
== -1
4383 /* This references the local defitionion. We must
4384 initialize this entry in the global offset table.
4385 Since the offset must always be a multiple of 8,
4386 we use the least significant bit to record
4387 whether we have initialized it already.
4389 When doing a dynamic link, we create a .rela.got
4390 relocation entry to initialize the value. This
4391 is done in the finish_dynamic_symbol routine. */
4396 bfd_put_64 (output_bfd
, relocation
,
4397 base_got
->contents
+ off
);
4398 /* Note that this is harmless for the GOTPLT64
4399 case, as -1 | 1 still is -1. */
4405 relocation
= (base_got
->output_section
->vma
4406 + base_got
->output_offset
+ off
);
4411 if (h
->plt
.offset
== (bfd_vma
) -1)
4413 /* Handle static pointers of STT_GNU_IFUNC symbols. */
4414 if (r_type
== htab
->pointer_r_type
4415 && (input_section
->flags
& SEC_CODE
) == 0)
4416 goto do_ifunc_pointer
;
4417 goto bad_ifunc_reloc
;
4420 /* STT_GNU_IFUNC symbol must go through PLT. */
4421 if (htab
->elf
.splt
!= NULL
)
4423 if (htab
->plt_bnd
!= NULL
)
4425 resolved_plt
= htab
->plt_bnd
;
4426 plt_offset
= eh
->plt_bnd
.offset
;
4430 resolved_plt
= htab
->elf
.splt
;
4431 plt_offset
= h
->plt
.offset
;
4436 resolved_plt
= htab
->elf
.iplt
;
4437 plt_offset
= h
->plt
.offset
;
4440 relocation
= (resolved_plt
->output_section
->vma
4441 + resolved_plt
->output_offset
+ plt_offset
);
4447 if (h
->root
.root
.string
)
4448 name
= h
->root
.root
.string
;
4450 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
,
4453 /* xgettext:c-format */
4454 (_("%B: relocation %s against STT_GNU_IFUNC "
4455 "symbol `%s' isn't supported"), input_bfd
,
4457 bfd_set_error (bfd_error_bad_value
);
4461 if (bfd_link_pic (info
))
4466 if (ABI_64_P (output_bfd
))
4471 if (rel
->r_addend
!= 0)
4473 if (h
->root
.root
.string
)
4474 name
= h
->root
.root
.string
;
4476 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
4479 /* xgettext:c-format */
4480 (_("%B: relocation %s against STT_GNU_IFUNC "
4481 "symbol `%s' has non-zero addend: %d"),
4482 input_bfd
, howto
->name
, name
, rel
->r_addend
);
4483 bfd_set_error (bfd_error_bad_value
);
4487 /* Generate dynamic relcoation only when there is a
4488 non-GOT reference in a shared object or there is no
4490 if ((bfd_link_pic (info
) && h
->non_got_ref
)
4491 || h
->plt
.offset
== (bfd_vma
) -1)
4493 Elf_Internal_Rela outrel
;
4496 /* Need a dynamic relocation to get the real function
4498 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
4502 if (outrel
.r_offset
== (bfd_vma
) -1
4503 || outrel
.r_offset
== (bfd_vma
) -2)
4506 outrel
.r_offset
+= (input_section
->output_section
->vma
4507 + input_section
->output_offset
);
4509 if (h
->dynindx
== -1
4511 || bfd_link_executable (info
))
4513 /* This symbol is resolved locally. */
4514 outrel
.r_info
= htab
->r_info (0, R_X86_64_IRELATIVE
);
4515 outrel
.r_addend
= (h
->root
.u
.def
.value
4516 + h
->root
.u
.def
.section
->output_section
->vma
4517 + h
->root
.u
.def
.section
->output_offset
);
4521 outrel
.r_info
= htab
->r_info (h
->dynindx
, r_type
);
4522 outrel
.r_addend
= 0;
4525 /* Dynamic relocations are stored in
4526 1. .rela.ifunc section in PIC object.
4527 2. .rela.got section in dynamic executable.
4528 3. .rela.iplt section in static executable. */
4529 if (bfd_link_pic (info
))
4530 sreloc
= htab
->elf
.irelifunc
;
4531 else if (htab
->elf
.splt
!= NULL
)
4532 sreloc
= htab
->elf
.srelgot
;
4534 sreloc
= htab
->elf
.irelplt
;
4535 elf_append_rela (output_bfd
, sreloc
, &outrel
);
4537 /* If this reloc is against an external symbol, we
4538 do not want to fiddle with the addend. Otherwise,
4539 we need to include the symbol value so that it
4540 becomes an addend for the dynamic reloc. For an
4541 internal symbol, we have updated addend. */
4546 case R_X86_64_PC32_BND
:
4548 case R_X86_64_PLT32
:
4549 case R_X86_64_PLT32_BND
:
4554 resolved_to_zero
= (eh
!= NULL
4555 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
4559 /* When generating a shared object, the relocations handled here are
4560 copied into the output file to be resolved at run time. */
4563 case R_X86_64_GOT32
:
4564 case R_X86_64_GOT64
:
4565 /* Relocation is to the entry for this symbol in the global
4567 case R_X86_64_GOTPCREL
:
4568 case R_X86_64_GOTPCRELX
:
4569 case R_X86_64_REX_GOTPCRELX
:
4570 case R_X86_64_GOTPCREL64
:
4571 /* Use global offset table entry as symbol value. */
4572 case R_X86_64_GOTPLT64
:
4573 /* This is obsolete and treated the the same as GOT64. */
4574 base_got
= htab
->elf
.sgot
;
4576 if (htab
->elf
.sgot
== NULL
)
4583 off
= h
->got
.offset
;
4585 && h
->plt
.offset
!= (bfd_vma
)-1
4586 && off
== (bfd_vma
)-1)
4588 /* We can't use h->got.offset here to save
4589 state, or even just remember the offset, as
4590 finish_dynamic_symbol would use that as offset into
4592 bfd_vma plt_index
= h
->plt
.offset
/ plt_entry_size
- 1;
4593 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
4594 base_got
= htab
->elf
.sgotplt
;
4597 dyn
= htab
->elf
.dynamic_sections_created
;
4599 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
4600 || (bfd_link_pic (info
)
4601 && SYMBOL_REFERENCES_LOCAL (info
, h
))
4602 || (ELF_ST_VISIBILITY (h
->other
)
4603 && h
->root
.type
== bfd_link_hash_undefweak
))
4605 /* This is actually a static link, or it is a -Bsymbolic
4606 link and the symbol is defined locally, or the symbol
4607 was forced to be local because of a version file. We
4608 must initialize this entry in the global offset table.
4609 Since the offset must always be a multiple of 8, we
4610 use the least significant bit to record whether we
4611 have initialized it already.
4613 When doing a dynamic link, we create a .rela.got
4614 relocation entry to initialize the value. This is
4615 done in the finish_dynamic_symbol routine. */
4620 bfd_put_64 (output_bfd
, relocation
,
4621 base_got
->contents
+ off
);
4622 /* Note that this is harmless for the GOTPLT64 case,
4623 as -1 | 1 still is -1. */
4628 unresolved_reloc
= FALSE
;
4632 if (local_got_offsets
== NULL
)
4635 off
= local_got_offsets
[r_symndx
];
4637 /* The offset must always be a multiple of 8. We use
4638 the least significant bit to record whether we have
4639 already generated the necessary reloc. */
4644 bfd_put_64 (output_bfd
, relocation
,
4645 base_got
->contents
+ off
);
4647 if (bfd_link_pic (info
))
4650 Elf_Internal_Rela outrel
;
4652 /* We need to generate a R_X86_64_RELATIVE reloc
4653 for the dynamic linker. */
4654 s
= htab
->elf
.srelgot
;
4658 outrel
.r_offset
= (base_got
->output_section
->vma
4659 + base_got
->output_offset
4661 outrel
.r_info
= htab
->r_info (0, R_X86_64_RELATIVE
);
4662 outrel
.r_addend
= relocation
;
4663 elf_append_rela (output_bfd
, s
, &outrel
);
4666 local_got_offsets
[r_symndx
] |= 1;
4670 if (off
>= (bfd_vma
) -2)
4673 relocation
= base_got
->output_section
->vma
4674 + base_got
->output_offset
+ off
;
4675 if (r_type
!= R_X86_64_GOTPCREL
4676 && r_type
!= R_X86_64_GOTPCRELX
4677 && r_type
!= R_X86_64_REX_GOTPCRELX
4678 && r_type
!= R_X86_64_GOTPCREL64
)
4679 relocation
-= htab
->elf
.sgotplt
->output_section
->vma
4680 - htab
->elf
.sgotplt
->output_offset
;
4684 case R_X86_64_GOTOFF64
:
4685 /* Relocation is relative to the start of the global offset
4688 /* Check to make sure it isn't a protected function or data
4689 symbol for shared library since it may not be local when
4690 used as function address or with copy relocation. We also
4691 need to make sure that a symbol is referenced locally. */
4692 if (bfd_link_pic (info
) && h
)
4694 if (!h
->def_regular
)
4698 switch (ELF_ST_VISIBILITY (h
->other
))
4701 v
= _("hidden symbol");
4704 v
= _("internal symbol");
4707 v
= _("protected symbol");
4715 /* xgettext:c-format */
4716 (_("%B: relocation R_X86_64_GOTOFF64 against undefined %s `%s' can not be used when making a shared object"),
4717 input_bfd
, v
, h
->root
.root
.string
);
4718 bfd_set_error (bfd_error_bad_value
);
4721 else if (!bfd_link_executable (info
)
4722 && !SYMBOL_REFERENCES_LOCAL (info
, h
)
4723 && (h
->type
== STT_FUNC
4724 || h
->type
== STT_OBJECT
)
4725 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
4728 /* xgettext:c-format */
4729 (_("%B: relocation R_X86_64_GOTOFF64 against protected %s `%s' can not be used when making a shared object"),
4731 h
->type
== STT_FUNC
? "function" : "data",
4732 h
->root
.root
.string
);
4733 bfd_set_error (bfd_error_bad_value
);
4738 /* Note that sgot is not involved in this
4739 calculation. We always want the start of .got.plt. If we
4740 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4741 permitted by the ABI, we might have to change this
4743 relocation
-= htab
->elf
.sgotplt
->output_section
->vma
4744 + htab
->elf
.sgotplt
->output_offset
;
4747 case R_X86_64_GOTPC32
:
4748 case R_X86_64_GOTPC64
:
4749 /* Use global offset table as symbol value. */
4750 relocation
= htab
->elf
.sgotplt
->output_section
->vma
4751 + htab
->elf
.sgotplt
->output_offset
;
4752 unresolved_reloc
= FALSE
;
4755 case R_X86_64_PLTOFF64
:
4756 /* Relocation is PLT entry relative to GOT. For local
4757 symbols it's the symbol itself relative to GOT. */
4759 /* See PLT32 handling. */
4760 && (h
->plt
.offset
!= (bfd_vma
) -1
4761 || eh
->plt_got
.offset
!= (bfd_vma
) -1)
4762 && htab
->elf
.splt
!= NULL
)
4764 if (eh
->plt_got
.offset
!= (bfd_vma
) -1)
4766 /* Use the GOT PLT. */
4767 resolved_plt
= htab
->plt_got
;
4768 plt_offset
= eh
->plt_got
.offset
;
4770 else if (htab
->plt_bnd
!= NULL
)
4772 resolved_plt
= htab
->plt_bnd
;
4773 plt_offset
= eh
->plt_bnd
.offset
;
4777 resolved_plt
= htab
->elf
.splt
;
4778 plt_offset
= h
->plt
.offset
;
4781 relocation
= (resolved_plt
->output_section
->vma
4782 + resolved_plt
->output_offset
4784 unresolved_reloc
= FALSE
;
4787 relocation
-= htab
->elf
.sgotplt
->output_section
->vma
4788 + htab
->elf
.sgotplt
->output_offset
;
4791 case R_X86_64_PLT32
:
4792 case R_X86_64_PLT32_BND
:
4793 /* Relocation is to the entry for this symbol in the
4794 procedure linkage table. */
4796 /* Resolve a PLT32 reloc against a local symbol directly,
4797 without using the procedure linkage table. */
4801 if ((h
->plt
.offset
== (bfd_vma
) -1
4802 && eh
->plt_got
.offset
== (bfd_vma
) -1)
4803 || htab
->elf
.splt
== NULL
)
4805 /* We didn't make a PLT entry for this symbol. This
4806 happens when statically linking PIC code, or when
4807 using -Bsymbolic. */
4811 if (h
->plt
.offset
!= (bfd_vma
) -1)
4813 if (htab
->plt_bnd
!= NULL
)
4815 resolved_plt
= htab
->plt_bnd
;
4816 plt_offset
= eh
->plt_bnd
.offset
;
4820 resolved_plt
= htab
->elf
.splt
;
4821 plt_offset
= h
->plt
.offset
;
4826 /* Use the GOT PLT. */
4827 resolved_plt
= htab
->plt_got
;
4828 plt_offset
= eh
->plt_got
.offset
;
4831 relocation
= (resolved_plt
->output_section
->vma
4832 + resolved_plt
->output_offset
4834 unresolved_reloc
= FALSE
;
4837 case R_X86_64_SIZE32
:
4838 case R_X86_64_SIZE64
:
4839 /* Set to symbol size. */
4840 relocation
= st_size
;
4846 case R_X86_64_PC32_BND
:
4847 /* Don't complain about -fPIC if the symbol is undefined when
4848 building executable unless it is unresolved weak symbol. */
4849 if ((input_section
->flags
& SEC_ALLOC
) != 0
4850 && (input_section
->flags
& SEC_READONLY
) != 0
4852 && ((bfd_link_executable (info
)
4853 && h
->root
.type
== bfd_link_hash_undefweak
4854 && !resolved_to_zero
)
4855 || (bfd_link_pic (info
)
4856 && !(bfd_link_pie (info
)
4857 && h
->root
.type
== bfd_link_hash_undefined
))))
4859 bfd_boolean fail
= FALSE
;
4861 = ((r_type
== R_X86_64_PC32
4862 || r_type
== R_X86_64_PC32_BND
)
4863 && is_32bit_relative_branch (contents
, rel
->r_offset
));
4865 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
4867 /* Symbol is referenced locally. Make sure it is
4868 defined locally or for a branch. */
4869 fail
= !h
->def_regular
&& !branch
;
4871 else if (!(bfd_link_pie (info
)
4872 && (h
->needs_copy
|| eh
->needs_copy
)))
4874 /* Symbol doesn't need copy reloc and isn't referenced
4875 locally. We only allow branch to symbol with
4876 non-default visibility. */
4878 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
);
4882 return elf_x86_64_need_pic (input_bfd
, input_section
,
4883 h
, NULL
, NULL
, howto
);
4892 /* FIXME: The ABI says the linker should make sure the value is
4893 the same when it's zeroextended to 64 bit. */
4896 if ((input_section
->flags
& SEC_ALLOC
) == 0)
4899 /* Don't copy a pc-relative relocation into the output file
4900 if the symbol needs copy reloc or the symbol is undefined
4901 when building executable. Copy dynamic function pointer
4902 relocations. Don't generate dynamic relocations against
4903 resolved undefined weak symbols in PIE. */
4904 if ((bfd_link_pic (info
)
4905 && !(bfd_link_pie (info
)
4909 || h
->root
.type
== bfd_link_hash_undefined
)
4910 && (IS_X86_64_PCREL_TYPE (r_type
)
4911 || r_type
== R_X86_64_SIZE32
4912 || r_type
== R_X86_64_SIZE64
))
4914 || ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4915 && !resolved_to_zero
)
4916 || h
->root
.type
!= bfd_link_hash_undefweak
))
4917 && ((! IS_X86_64_PCREL_TYPE (r_type
)
4918 && r_type
!= R_X86_64_SIZE32
4919 && r_type
!= R_X86_64_SIZE64
)
4920 || ! SYMBOL_CALLS_LOCAL (info
, h
)))
4921 || (ELIMINATE_COPY_RELOCS
4922 && !bfd_link_pic (info
)
4926 || eh
->func_pointer_refcount
> 0
4927 || (h
->root
.type
== bfd_link_hash_undefweak
4928 && !resolved_to_zero
))
4929 && ((h
->def_dynamic
&& !h
->def_regular
)
4930 /* Undefined weak symbol is bound locally when
4932 || h
->root
.type
== bfd_link_hash_undefined
)))
4934 Elf_Internal_Rela outrel
;
4935 bfd_boolean skip
, relocate
;
4938 /* When generating a shared object, these relocations
4939 are copied into the output file to be resolved at run
4945 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
4947 if (outrel
.r_offset
== (bfd_vma
) -1)
4949 else if (outrel
.r_offset
== (bfd_vma
) -2)
4950 skip
= TRUE
, relocate
= TRUE
;
4952 outrel
.r_offset
+= (input_section
->output_section
->vma
4953 + input_section
->output_offset
);
4956 memset (&outrel
, 0, sizeof outrel
);
4958 /* h->dynindx may be -1 if this symbol was marked to
4962 && (IS_X86_64_PCREL_TYPE (r_type
)
4963 || !(bfd_link_executable (info
)
4964 || SYMBOLIC_BIND (info
, h
))
4965 || ! h
->def_regular
))
4967 outrel
.r_info
= htab
->r_info (h
->dynindx
, r_type
);
4968 outrel
.r_addend
= rel
->r_addend
;
4972 /* This symbol is local, or marked to become local.
4973 When relocation overflow check is disabled, we
4974 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
4975 if (r_type
== htab
->pointer_r_type
4976 || (r_type
== R_X86_64_32
4977 && info
->no_reloc_overflow_check
))
4980 outrel
.r_info
= htab
->r_info (0, R_X86_64_RELATIVE
);
4981 outrel
.r_addend
= relocation
+ rel
->r_addend
;
4983 else if (r_type
== R_X86_64_64
4984 && !ABI_64_P (output_bfd
))
4987 outrel
.r_info
= htab
->r_info (0,
4988 R_X86_64_RELATIVE64
);
4989 outrel
.r_addend
= relocation
+ rel
->r_addend
;
4990 /* Check addend overflow. */
4991 if ((outrel
.r_addend
& 0x80000000)
4992 != (rel
->r_addend
& 0x80000000))
4995 int addend
= rel
->r_addend
;
4996 if (h
&& h
->root
.root
.string
)
4997 name
= h
->root
.root
.string
;
4999 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
5003 /* xgettext:c-format */
5004 (_("%B: addend -0x%x in relocation %s against "
5005 "symbol `%s' at 0x%lx in section `%A' is "
5007 input_bfd
, input_section
, addend
,
5009 (unsigned long) rel
->r_offset
);
5012 /* xgettext:c-format */
5013 (_("%B: addend 0x%x in relocation %s against "
5014 "symbol `%s' at 0x%lx in section `%A' is "
5016 input_bfd
, input_section
, addend
,
5018 (unsigned long) rel
->r_offset
);
5019 bfd_set_error (bfd_error_bad_value
);
5027 if (bfd_is_abs_section (sec
))
5029 else if (sec
== NULL
|| sec
->owner
== NULL
)
5031 bfd_set_error (bfd_error_bad_value
);
5038 /* We are turning this relocation into one
5039 against a section symbol. It would be
5040 proper to subtract the symbol's value,
5041 osec->vma, from the emitted reloc addend,
5042 but ld.so expects buggy relocs. */
5043 osec
= sec
->output_section
;
5044 sindx
= elf_section_data (osec
)->dynindx
;
5047 asection
*oi
= htab
->elf
.text_index_section
;
5048 sindx
= elf_section_data (oi
)->dynindx
;
5050 BFD_ASSERT (sindx
!= 0);
5053 outrel
.r_info
= htab
->r_info (sindx
, r_type
);
5054 outrel
.r_addend
= relocation
+ rel
->r_addend
;
5058 sreloc
= elf_section_data (input_section
)->sreloc
;
5060 if (sreloc
== NULL
|| sreloc
->contents
== NULL
)
5062 r
= bfd_reloc_notsupported
;
5063 goto check_relocation_error
;
5066 elf_append_rela (output_bfd
, sreloc
, &outrel
);
5068 /* If this reloc is against an external symbol, we do
5069 not want to fiddle with the addend. Otherwise, we
5070 need to include the symbol value so that it becomes
5071 an addend for the dynamic reloc. */
5078 case R_X86_64_TLSGD
:
5079 case R_X86_64_GOTPC32_TLSDESC
:
5080 case R_X86_64_TLSDESC_CALL
:
5081 case R_X86_64_GOTTPOFF
:
5082 tls_type
= GOT_UNKNOWN
;
5083 if (h
== NULL
&& local_got_offsets
)
5084 tls_type
= elf_x86_64_local_got_tls_type (input_bfd
) [r_symndx
];
5086 tls_type
= elf_x86_64_hash_entry (h
)->tls_type
;
5088 if (! elf_x86_64_tls_transition (info
, input_bfd
,
5089 input_section
, contents
,
5090 symtab_hdr
, sym_hashes
,
5091 &r_type
, tls_type
, rel
,
5092 relend
, h
, r_symndx
, TRUE
))
5095 if (r_type
== R_X86_64_TPOFF32
)
5097 bfd_vma roff
= rel
->r_offset
;
5099 BFD_ASSERT (! unresolved_reloc
);
5101 if (ELF32_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
5103 /* GD->LE transition. For 64bit, change
5104 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5105 .word 0x6666; rex64; call __tls_get_addr@PLT
5107 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5109 call *__tls_get_addr@GOTPCREL(%rip)
5110 which may be converted to
5111 addr32 call __tls_get_addr
5114 leaq foo@tpoff(%rax), %rax
5116 leaq foo@tlsgd(%rip), %rdi
5117 .word 0x6666; rex64; call __tls_get_addr@PLT
5119 leaq foo@tlsgd(%rip), %rdi
5121 call *__tls_get_addr@GOTPCREL(%rip)
5122 which may be converted to
5123 addr32 call __tls_get_addr
5126 leaq foo@tpoff(%rax), %rax
5127 For largepic, change:
5128 leaq foo@tlsgd(%rip), %rdi
5129 movabsq $__tls_get_addr@pltoff, %rax
5134 leaq foo@tpoff(%rax), %rax
5135 nopw 0x0(%rax,%rax,1) */
5137 if (ABI_64_P (output_bfd
))
5139 if (contents
[roff
+ 5] == 0xb8)
5141 memcpy (contents
+ roff
- 3,
5142 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
5143 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5147 memcpy (contents
+ roff
- 4,
5148 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
5152 memcpy (contents
+ roff
- 3,
5153 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
5155 bfd_put_32 (output_bfd
,
5156 elf_x86_64_tpoff (info
, relocation
),
5157 contents
+ roff
+ 8 + largepic
);
5158 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
5159 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
5164 else if (ELF32_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
5166 /* GDesc -> LE transition.
5167 It's originally something like:
5168 leaq x@tlsdesc(%rip), %rax
5171 movl $x@tpoff, %rax. */
5173 unsigned int val
, type
;
5175 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
5176 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
5177 bfd_put_8 (output_bfd
, 0x48 | ((type
>> 2) & 1),
5178 contents
+ roff
- 3);
5179 bfd_put_8 (output_bfd
, 0xc7, contents
+ roff
- 2);
5180 bfd_put_8 (output_bfd
, 0xc0 | ((val
>> 3) & 7),
5181 contents
+ roff
- 1);
5182 bfd_put_32 (output_bfd
,
5183 elf_x86_64_tpoff (info
, relocation
),
5187 else if (ELF32_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
5189 /* GDesc -> LE transition.
5194 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
5195 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
5198 else if (ELF32_R_TYPE (rel
->r_info
) == R_X86_64_GOTTPOFF
)
5200 /* IE->LE transition:
5201 For 64bit, originally it can be one of:
5202 movq foo@gottpoff(%rip), %reg
5203 addq foo@gottpoff(%rip), %reg
5206 leaq foo(%reg), %reg
5208 For 32bit, originally it can be one of:
5209 movq foo@gottpoff(%rip), %reg
5210 addl foo@gottpoff(%rip), %reg
5213 leal foo(%reg), %reg
5216 unsigned int val
, type
, reg
;
5219 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
5222 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
5223 reg
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
5229 bfd_put_8 (output_bfd
, 0x49,
5230 contents
+ roff
- 3);
5231 else if (!ABI_64_P (output_bfd
) && val
== 0x44)
5232 bfd_put_8 (output_bfd
, 0x41,
5233 contents
+ roff
- 3);
5234 bfd_put_8 (output_bfd
, 0xc7,
5235 contents
+ roff
- 2);
5236 bfd_put_8 (output_bfd
, 0xc0 | reg
,
5237 contents
+ roff
- 1);
5241 /* addq/addl -> addq/addl - addressing with %rsp/%r12
5244 bfd_put_8 (output_bfd
, 0x49,
5245 contents
+ roff
- 3);
5246 else if (!ABI_64_P (output_bfd
) && val
== 0x44)
5247 bfd_put_8 (output_bfd
, 0x41,
5248 contents
+ roff
- 3);
5249 bfd_put_8 (output_bfd
, 0x81,
5250 contents
+ roff
- 2);
5251 bfd_put_8 (output_bfd
, 0xc0 | reg
,
5252 contents
+ roff
- 1);
5256 /* addq/addl -> leaq/leal */
5258 bfd_put_8 (output_bfd
, 0x4d,
5259 contents
+ roff
- 3);
5260 else if (!ABI_64_P (output_bfd
) && val
== 0x44)
5261 bfd_put_8 (output_bfd
, 0x45,
5262 contents
+ roff
- 3);
5263 bfd_put_8 (output_bfd
, 0x8d,
5264 contents
+ roff
- 2);
5265 bfd_put_8 (output_bfd
, 0x80 | reg
| (reg
<< 3),
5266 contents
+ roff
- 1);
5268 bfd_put_32 (output_bfd
,
5269 elf_x86_64_tpoff (info
, relocation
),
5277 if (htab
->elf
.sgot
== NULL
)
5282 off
= h
->got
.offset
;
5283 offplt
= elf_x86_64_hash_entry (h
)->tlsdesc_got
;
5287 if (local_got_offsets
== NULL
)
5290 off
= local_got_offsets
[r_symndx
];
5291 offplt
= local_tlsdesc_gotents
[r_symndx
];
5298 Elf_Internal_Rela outrel
;
5302 if (htab
->elf
.srelgot
== NULL
)
5305 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
5307 if (GOT_TLS_GDESC_P (tls_type
))
5309 outrel
.r_info
= htab
->r_info (indx
, R_X86_64_TLSDESC
);
5310 BFD_ASSERT (htab
->sgotplt_jump_table_size
+ offplt
5311 + 2 * GOT_ENTRY_SIZE
<= htab
->elf
.sgotplt
->size
);
5312 outrel
.r_offset
= (htab
->elf
.sgotplt
->output_section
->vma
5313 + htab
->elf
.sgotplt
->output_offset
5315 + htab
->sgotplt_jump_table_size
);
5316 sreloc
= htab
->elf
.srelplt
;
5318 outrel
.r_addend
= relocation
- elf_x86_64_dtpoff_base (info
);
5320 outrel
.r_addend
= 0;
5321 elf_append_rela (output_bfd
, sreloc
, &outrel
);
5324 sreloc
= htab
->elf
.srelgot
;
5326 outrel
.r_offset
= (htab
->elf
.sgot
->output_section
->vma
5327 + htab
->elf
.sgot
->output_offset
+ off
);
5329 if (GOT_TLS_GD_P (tls_type
))
5330 dr_type
= R_X86_64_DTPMOD64
;
5331 else if (GOT_TLS_GDESC_P (tls_type
))
5334 dr_type
= R_X86_64_TPOFF64
;
5336 bfd_put_64 (output_bfd
, 0, htab
->elf
.sgot
->contents
+ off
);
5337 outrel
.r_addend
= 0;
5338 if ((dr_type
== R_X86_64_TPOFF64
5339 || dr_type
== R_X86_64_TLSDESC
) && indx
== 0)
5340 outrel
.r_addend
= relocation
- elf_x86_64_dtpoff_base (info
);
5341 outrel
.r_info
= htab
->r_info (indx
, dr_type
);
5343 elf_append_rela (output_bfd
, sreloc
, &outrel
);
5345 if (GOT_TLS_GD_P (tls_type
))
5349 BFD_ASSERT (! unresolved_reloc
);
5350 bfd_put_64 (output_bfd
,
5351 relocation
- elf_x86_64_dtpoff_base (info
),
5352 htab
->elf
.sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
5356 bfd_put_64 (output_bfd
, 0,
5357 htab
->elf
.sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
5358 outrel
.r_info
= htab
->r_info (indx
,
5360 outrel
.r_offset
+= GOT_ENTRY_SIZE
;
5361 elf_append_rela (output_bfd
, sreloc
,
5370 local_got_offsets
[r_symndx
] |= 1;
5373 if (off
>= (bfd_vma
) -2
5374 && ! GOT_TLS_GDESC_P (tls_type
))
5376 if (r_type
== ELF32_R_TYPE (rel
->r_info
))
5378 if (r_type
== R_X86_64_GOTPC32_TLSDESC
5379 || r_type
== R_X86_64_TLSDESC_CALL
)
5380 relocation
= htab
->elf
.sgotplt
->output_section
->vma
5381 + htab
->elf
.sgotplt
->output_offset
5382 + offplt
+ htab
->sgotplt_jump_table_size
;
5384 relocation
= htab
->elf
.sgot
->output_section
->vma
5385 + htab
->elf
.sgot
->output_offset
+ off
;
5386 unresolved_reloc
= FALSE
;
5390 bfd_vma roff
= rel
->r_offset
;
5392 if (ELF32_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
5394 /* GD->IE transition. For 64bit, change
5395 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5396 .word 0x6666; rex64; call __tls_get_addr@PLT
5398 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5400 call *__tls_get_addr@GOTPCREL(%rip
5401 which may be converted to
5402 addr32 call __tls_get_addr
5405 addq foo@gottpoff(%rip), %rax
5407 leaq foo@tlsgd(%rip), %rdi
5408 .word 0x6666; rex64; call __tls_get_addr@PLT
5410 leaq foo@tlsgd(%rip), %rdi
5412 call *__tls_get_addr@GOTPCREL(%rip)
5413 which may be converted to
5414 addr32 call __tls_get_addr
5417 addq foo@gottpoff(%rip), %rax
5418 For largepic, change:
5419 leaq foo@tlsgd(%rip), %rdi
5420 movabsq $__tls_get_addr@pltoff, %rax
5425 addq foo@gottpoff(%rax), %rax
5426 nopw 0x0(%rax,%rax,1) */
5428 if (ABI_64_P (output_bfd
))
5430 if (contents
[roff
+ 5] == 0xb8)
5432 memcpy (contents
+ roff
- 3,
5433 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
5434 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5438 memcpy (contents
+ roff
- 4,
5439 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5443 memcpy (contents
+ roff
- 3,
5444 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5447 relocation
= (htab
->elf
.sgot
->output_section
->vma
5448 + htab
->elf
.sgot
->output_offset
+ off
5451 - input_section
->output_section
->vma
5452 - input_section
->output_offset
5454 bfd_put_32 (output_bfd
, relocation
,
5455 contents
+ roff
+ 8 + largepic
);
5456 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
5461 else if (ELF32_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
5463 /* GDesc -> IE transition.
5464 It's originally something like:
5465 leaq x@tlsdesc(%rip), %rax
5468 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
5470 /* Now modify the instruction as appropriate. To
5471 turn a leaq into a movq in the form we use it, it
5472 suffices to change the second byte from 0x8d to
5474 bfd_put_8 (output_bfd
, 0x8b, contents
+ roff
- 2);
5476 bfd_put_32 (output_bfd
,
5477 htab
->elf
.sgot
->output_section
->vma
5478 + htab
->elf
.sgot
->output_offset
+ off
5480 - input_section
->output_section
->vma
5481 - input_section
->output_offset
5486 else if (ELF32_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
5488 /* GDesc -> IE transition.
5495 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
5496 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
5504 case R_X86_64_TLSLD
:
5505 if (! elf_x86_64_tls_transition (info
, input_bfd
,
5506 input_section
, contents
,
5507 symtab_hdr
, sym_hashes
,
5508 &r_type
, GOT_UNKNOWN
, rel
,
5509 relend
, h
, r_symndx
, TRUE
))
5512 if (r_type
!= R_X86_64_TLSLD
)
5514 /* LD->LE transition:
5515 leaq foo@tlsld(%rip), %rdi
5516 call __tls_get_addr@PLT
5517 For 64bit, we change it into:
5518 .word 0x6666; .byte 0x66; movq %fs:0, %rax
5519 For 32bit, we change it into:
5520 nopl 0x0(%rax); movl %fs:0, %eax
5522 leaq foo@tlsld(%rip), %rdi;
5523 call *__tls_get_addr@GOTPCREL(%rip)
5524 which may be converted to
5525 addr32 call __tls_get_addr
5526 For 64bit, we change it into:
5527 .word 0x6666; .word 0x6666; movq %fs:0, %rax
5528 For 32bit, we change it into:
5529 nopw 0x0(%rax); movl %fs:0, %eax
5530 For largepic, change:
5531 leaq foo@tlsgd(%rip), %rdi
5532 movabsq $__tls_get_addr@pltoff, %rax
5536 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
5539 BFD_ASSERT (r_type
== R_X86_64_TPOFF32
);
5540 if (ABI_64_P (output_bfd
))
5542 if (contents
[rel
->r_offset
+ 5] == 0xb8)
5543 memcpy (contents
+ rel
->r_offset
- 3,
5544 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
5545 "\x64\x48\x8b\x04\x25\0\0\0", 22);
5546 else if (contents
[rel
->r_offset
+ 4] == 0xff
5547 || contents
[rel
->r_offset
+ 4] == 0x67)
5548 memcpy (contents
+ rel
->r_offset
- 3,
5549 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
5552 memcpy (contents
+ rel
->r_offset
- 3,
5553 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
5557 if (contents
[rel
->r_offset
+ 4] == 0xff)
5558 memcpy (contents
+ rel
->r_offset
- 3,
5559 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
5562 memcpy (contents
+ rel
->r_offset
- 3,
5563 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
5565 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
5566 and R_X86_64_PLTOFF64. */
5572 if (htab
->elf
.sgot
== NULL
)
5575 off
= htab
->tls_ld_got
.offset
;
5580 Elf_Internal_Rela outrel
;
5582 if (htab
->elf
.srelgot
== NULL
)
5585 outrel
.r_offset
= (htab
->elf
.sgot
->output_section
->vma
5586 + htab
->elf
.sgot
->output_offset
+ off
);
5588 bfd_put_64 (output_bfd
, 0,
5589 htab
->elf
.sgot
->contents
+ off
);
5590 bfd_put_64 (output_bfd
, 0,
5591 htab
->elf
.sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
5592 outrel
.r_info
= htab
->r_info (0, R_X86_64_DTPMOD64
);
5593 outrel
.r_addend
= 0;
5594 elf_append_rela (output_bfd
, htab
->elf
.srelgot
,
5596 htab
->tls_ld_got
.offset
|= 1;
5598 relocation
= htab
->elf
.sgot
->output_section
->vma
5599 + htab
->elf
.sgot
->output_offset
+ off
;
5600 unresolved_reloc
= FALSE
;
5603 case R_X86_64_DTPOFF32
:
5604 if (!bfd_link_executable (info
)
5605 || (input_section
->flags
& SEC_CODE
) == 0)
5606 relocation
-= elf_x86_64_dtpoff_base (info
);
5608 relocation
= elf_x86_64_tpoff (info
, relocation
);
5611 case R_X86_64_TPOFF32
:
5612 case R_X86_64_TPOFF64
:
5613 BFD_ASSERT (bfd_link_executable (info
));
5614 relocation
= elf_x86_64_tpoff (info
, relocation
);
5617 case R_X86_64_DTPOFF64
:
5618 BFD_ASSERT ((input_section
->flags
& SEC_CODE
) == 0);
5619 relocation
-= elf_x86_64_dtpoff_base (info
);
5626 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5627 because such sections are not SEC_ALLOC and thus ld.so will
5628 not process them. */
5629 if (unresolved_reloc
5630 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
5632 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
5633 rel
->r_offset
) != (bfd_vma
) -1)
5636 /* xgettext:c-format */
5637 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5640 (long) rel
->r_offset
,
5642 h
->root
.root
.string
);
5647 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
5648 contents
, rel
->r_offset
,
5649 relocation
, rel
->r_addend
);
5651 check_relocation_error
:
5652 if (r
!= bfd_reloc_ok
)
5657 name
= h
->root
.root
.string
;
5660 name
= bfd_elf_string_from_elf_section (input_bfd
,
5661 symtab_hdr
->sh_link
,
5666 name
= bfd_section_name (input_bfd
, sec
);
5669 if (r
== bfd_reloc_overflow
)
5670 (*info
->callbacks
->reloc_overflow
)
5671 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
5672 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
5676 /* xgettext:c-format */
5677 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
5678 input_bfd
, input_section
,
5679 (long) rel
->r_offset
, name
, (int) r
);
5690 Elf_Internal_Shdr
*rel_hdr
;
5691 size_t deleted
= rel
- wrel
;
5693 rel_hdr
= _bfd_elf_single_rel_hdr (input_section
->output_section
);
5694 rel_hdr
->sh_size
-= rel_hdr
->sh_entsize
* deleted
;
5695 if (rel_hdr
->sh_size
== 0)
5697 /* It is too late to remove an empty reloc section. Leave
5699 ??? What is wrong with an empty section??? */
5700 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
;
5703 rel_hdr
= _bfd_elf_single_rel_hdr (input_section
);
5704 rel_hdr
->sh_size
-= rel_hdr
->sh_entsize
* deleted
;
5705 input_section
->reloc_count
-= deleted
;
5711 /* Finish up dynamic symbol handling. We set the contents of various
5712 dynamic sections here. */
5715 elf_x86_64_finish_dynamic_symbol (bfd
*output_bfd
,
5716 struct bfd_link_info
*info
,
5717 struct elf_link_hash_entry
*h
,
5718 Elf_Internal_Sym
*sym
)
5720 struct elf_x86_64_link_hash_table
*htab
;
5721 const struct elf_x86_64_backend_data
*abed
;
5722 bfd_boolean use_plt_bnd
;
5723 struct elf_x86_64_link_hash_entry
*eh
;
5724 bfd_boolean local_undefweak
;
5726 htab
= elf_x86_64_hash_table (info
);
5730 /* Use MPX backend data in case of BND relocation. Use .plt_bnd
5731 section only if there is .plt section. */
5732 use_plt_bnd
= htab
->elf
.splt
!= NULL
&& htab
->plt_bnd
!= NULL
;
5734 ? &elf_x86_64_bnd_arch_bed
5735 : get_elf_x86_64_backend_data (output_bfd
));
5737 eh
= (struct elf_x86_64_link_hash_entry
*) h
;
5739 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5740 resolved undefined weak symbols in executable so that their
5741 references have value 0 at run-time. */
5742 local_undefweak
= UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
5746 if (h
->plt
.offset
!= (bfd_vma
) -1)
5749 bfd_vma got_offset
, plt_offset
, plt_plt_offset
, plt_got_offset
;
5750 bfd_vma plt_plt_insn_end
, plt_got_insn_size
;
5751 Elf_Internal_Rela rela
;
5753 asection
*plt
, *gotplt
, *relplt
, *resolved_plt
;
5754 const struct elf_backend_data
*bed
;
5755 bfd_vma plt_got_pcrel_offset
;
5757 /* When building a static executable, use .iplt, .igot.plt and
5758 .rela.iplt sections for STT_GNU_IFUNC symbols. */
5759 if (htab
->elf
.splt
!= NULL
)
5761 plt
= htab
->elf
.splt
;
5762 gotplt
= htab
->elf
.sgotplt
;
5763 relplt
= htab
->elf
.srelplt
;
5767 plt
= htab
->elf
.iplt
;
5768 gotplt
= htab
->elf
.igotplt
;
5769 relplt
= htab
->elf
.irelplt
;
5772 /* This symbol has an entry in the procedure linkage table. Set
5774 if ((h
->dynindx
== -1
5776 && !((h
->forced_local
|| bfd_link_executable (info
))
5778 && h
->type
== STT_GNU_IFUNC
))
5784 /* Get the index in the procedure linkage table which
5785 corresponds to this symbol. This is the index of this symbol
5786 in all the symbols for which we are making plt entries. The
5787 first entry in the procedure linkage table is reserved.
5789 Get the offset into the .got table of the entry that
5790 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
5791 bytes. The first three are reserved for the dynamic linker.
5793 For static executables, we don't reserve anything. */
5795 if (plt
== htab
->elf
.splt
)
5797 got_offset
= h
->plt
.offset
/ abed
->plt_entry_size
- 1;
5798 got_offset
= (got_offset
+ 3) * GOT_ENTRY_SIZE
;
5802 got_offset
= h
->plt
.offset
/ abed
->plt_entry_size
;
5803 got_offset
= got_offset
* GOT_ENTRY_SIZE
;
5806 plt_plt_insn_end
= abed
->plt_plt_insn_end
;
5807 plt_plt_offset
= abed
->plt_plt_offset
;
5808 plt_got_insn_size
= abed
->plt_got_insn_size
;
5809 plt_got_offset
= abed
->plt_got_offset
;
5812 /* Use the second PLT with BND relocations. */
5813 const bfd_byte
*plt_entry
, *plt2_entry
;
5815 if (eh
->has_bnd_reloc
)
5817 plt_entry
= elf_x86_64_bnd_plt_entry
;
5818 plt2_entry
= elf_x86_64_bnd_plt2_entry
;
5822 plt_entry
= elf_x86_64_legacy_plt_entry
;
5823 plt2_entry
= elf_x86_64_legacy_plt2_entry
;
5825 /* Subtract 1 since there is no BND prefix. */
5826 plt_plt_insn_end
-= 1;
5827 plt_plt_offset
-= 1;
5828 plt_got_insn_size
-= 1;
5829 plt_got_offset
-= 1;
5832 BFD_ASSERT (sizeof (elf_x86_64_bnd_plt_entry
)
5833 == sizeof (elf_x86_64_legacy_plt_entry
));
5835 /* Fill in the entry in the procedure linkage table. */
5836 memcpy (plt
->contents
+ h
->plt
.offset
,
5837 plt_entry
, sizeof (elf_x86_64_legacy_plt_entry
));
5838 /* Fill in the entry in the second PLT. */
5839 memcpy (htab
->plt_bnd
->contents
+ eh
->plt_bnd
.offset
,
5840 plt2_entry
, sizeof (elf_x86_64_legacy_plt2_entry
));
5842 resolved_plt
= htab
->plt_bnd
;
5843 plt_offset
= eh
->plt_bnd
.offset
;
5847 /* Fill in the entry in the procedure linkage table. */
5848 memcpy (plt
->contents
+ h
->plt
.offset
, abed
->plt_entry
,
5849 abed
->plt_entry_size
);
5852 plt_offset
= h
->plt
.offset
;
5855 /* Insert the relocation positions of the plt section. */
5857 /* Put offset the PC-relative instruction referring to the GOT entry,
5858 subtracting the size of that instruction. */
5859 plt_got_pcrel_offset
= (gotplt
->output_section
->vma
5860 + gotplt
->output_offset
5862 - resolved_plt
->output_section
->vma
5863 - resolved_plt
->output_offset
5865 - plt_got_insn_size
);
5867 /* Check PC-relative offset overflow in PLT entry. */
5868 if ((plt_got_pcrel_offset
+ 0x80000000) > 0xffffffff)
5869 /* xgettext:c-format */
5870 info
->callbacks
->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
5871 output_bfd
, h
->root
.root
.string
);
5873 bfd_put_32 (output_bfd
, plt_got_pcrel_offset
,
5874 resolved_plt
->contents
+ plt_offset
+ plt_got_offset
);
5876 /* Fill in the entry in the global offset table, initially this
5877 points to the second part of the PLT entry. Leave the entry
5878 as zero for undefined weak symbol in PIE. No PLT relocation
5879 against undefined weak symbol in PIE. */
5880 if (!local_undefweak
)
5882 bfd_put_64 (output_bfd
, (plt
->output_section
->vma
5883 + plt
->output_offset
5885 + abed
->plt_lazy_offset
),
5886 gotplt
->contents
+ got_offset
);
5888 /* Fill in the entry in the .rela.plt section. */
5889 rela
.r_offset
= (gotplt
->output_section
->vma
5890 + gotplt
->output_offset
5892 if (h
->dynindx
== -1
5893 || ((bfd_link_executable (info
)
5894 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
5896 && h
->type
== STT_GNU_IFUNC
))
5898 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5899 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
5900 rela
.r_info
= htab
->r_info (0, R_X86_64_IRELATIVE
);
5901 rela
.r_addend
= (h
->root
.u
.def
.value
5902 + h
->root
.u
.def
.section
->output_section
->vma
5903 + h
->root
.u
.def
.section
->output_offset
);
5904 /* R_X86_64_IRELATIVE comes last. */
5905 plt_index
= htab
->next_irelative_index
--;
5909 rela
.r_info
= htab
->r_info (h
->dynindx
, R_X86_64_JUMP_SLOT
);
5911 plt_index
= htab
->next_jump_slot_index
++;
5914 /* Don't fill PLT entry for static executables. */
5915 if (plt
== htab
->elf
.splt
)
5917 bfd_vma plt0_offset
= h
->plt
.offset
+ plt_plt_insn_end
;
5919 /* Put relocation index. */
5920 bfd_put_32 (output_bfd
, plt_index
,
5921 (plt
->contents
+ h
->plt
.offset
5922 + abed
->plt_reloc_offset
));
5924 /* Put offset for jmp .PLT0 and check for overflow. We don't
5925 check relocation index for overflow since branch displacement
5926 will overflow first. */
5927 if (plt0_offset
> 0x80000000)
5928 /* xgettext:c-format */
5929 info
->callbacks
->einfo (_("%F%B: branch displacement overflow in PLT entry for `%s'\n"),
5930 output_bfd
, h
->root
.root
.string
);
5931 bfd_put_32 (output_bfd
, - plt0_offset
,
5932 plt
->contents
+ h
->plt
.offset
+ plt_plt_offset
);
5935 bed
= get_elf_backend_data (output_bfd
);
5936 loc
= relplt
->contents
+ plt_index
* bed
->s
->sizeof_rela
;
5937 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
5940 else if (eh
->plt_got
.offset
!= (bfd_vma
) -1)
5942 bfd_vma got_offset
, plt_offset
, plt_got_offset
, plt_got_insn_size
;
5943 asection
*plt
, *got
;
5944 bfd_boolean got_after_plt
;
5945 int32_t got_pcrel_offset
;
5946 const bfd_byte
*got_plt_entry
;
5948 /* Set the entry in the GOT procedure linkage table. */
5949 plt
= htab
->plt_got
;
5950 got
= htab
->elf
.sgot
;
5951 got_offset
= h
->got
.offset
;
5953 if (got_offset
== (bfd_vma
) -1
5954 || h
->type
== STT_GNU_IFUNC
5959 /* Use the second PLT entry template for the GOT PLT since they
5960 are the identical. */
5961 plt_got_insn_size
= elf_x86_64_bnd_arch_bed
.plt_got_insn_size
;
5962 plt_got_offset
= elf_x86_64_bnd_arch_bed
.plt_got_offset
;
5963 if (eh
->has_bnd_reloc
)
5964 got_plt_entry
= elf_x86_64_bnd_plt2_entry
;
5967 got_plt_entry
= elf_x86_64_legacy_plt2_entry
;
5969 /* Subtract 1 since there is no BND prefix. */
5970 plt_got_insn_size
-= 1;
5971 plt_got_offset
-= 1;
5974 /* Fill in the entry in the GOT procedure linkage table. */
5975 plt_offset
= eh
->plt_got
.offset
;
5976 memcpy (plt
->contents
+ plt_offset
,
5977 got_plt_entry
, sizeof (elf_x86_64_legacy_plt2_entry
));
5979 /* Put offset the PC-relative instruction referring to the GOT
5980 entry, subtracting the size of that instruction. */
5981 got_pcrel_offset
= (got
->output_section
->vma
5982 + got
->output_offset
5984 - plt
->output_section
->vma
5985 - plt
->output_offset
5987 - plt_got_insn_size
);
5989 /* Check PC-relative offset overflow in GOT PLT entry. */
5990 got_after_plt
= got
->output_section
->vma
> plt
->output_section
->vma
;
5991 if ((got_after_plt
&& got_pcrel_offset
< 0)
5992 || (!got_after_plt
&& got_pcrel_offset
> 0))
5993 /* xgettext:c-format */
5994 info
->callbacks
->einfo (_("%F%B: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
5995 output_bfd
, h
->root
.root
.string
);
5997 bfd_put_32 (output_bfd
, got_pcrel_offset
,
5998 plt
->contents
+ plt_offset
+ plt_got_offset
);
6001 if (!local_undefweak
6003 && (h
->plt
.offset
!= (bfd_vma
) -1
6004 || eh
->plt_got
.offset
!= (bfd_vma
) -1))
6006 /* Mark the symbol as undefined, rather than as defined in
6007 the .plt section. Leave the value if there were any
6008 relocations where pointer equality matters (this is a clue
6009 for the dynamic linker, to make function pointer
6010 comparisons work between an application and shared
6011 library), otherwise set it to zero. If a function is only
6012 called from a binary, there is no need to slow down
6013 shared libraries because of that. */
6014 sym
->st_shndx
= SHN_UNDEF
;
6015 if (!h
->pointer_equality_needed
)
6019 /* Don't generate dynamic GOT relocation against undefined weak
6020 symbol in executable. */
6021 if (h
->got
.offset
!= (bfd_vma
) -1
6022 && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h
)->tls_type
)
6023 && elf_x86_64_hash_entry (h
)->tls_type
!= GOT_TLS_IE
6024 && !local_undefweak
)
6026 Elf_Internal_Rela rela
;
6027 asection
*relgot
= htab
->elf
.srelgot
;
6029 /* This symbol has an entry in the global offset table. Set it
6031 if (htab
->elf
.sgot
== NULL
|| htab
->elf
.srelgot
== NULL
)
6034 rela
.r_offset
= (htab
->elf
.sgot
->output_section
->vma
6035 + htab
->elf
.sgot
->output_offset
6036 + (h
->got
.offset
&~ (bfd_vma
) 1));
6038 /* If this is a static link, or it is a -Bsymbolic link and the
6039 symbol is defined locally or was forced to be local because
6040 of a version file, we just want to emit a RELATIVE reloc.
6041 The entry in the global offset table will already have been
6042 initialized in the relocate_section function. */
6044 && h
->type
== STT_GNU_IFUNC
)
6046 if (h
->plt
.offset
== (bfd_vma
) -1)
6048 /* STT_GNU_IFUNC is referenced without PLT. */
6049 if (htab
->elf
.splt
== NULL
)
6051 /* use .rel[a].iplt section to store .got relocations
6052 in static executable. */
6053 relgot
= htab
->elf
.irelplt
;
6055 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
6057 rela
.r_info
= htab
->r_info (0,
6058 R_X86_64_IRELATIVE
);
6059 rela
.r_addend
= (h
->root
.u
.def
.value
6060 + h
->root
.u
.def
.section
->output_section
->vma
6061 + h
->root
.u
.def
.section
->output_offset
);
6066 else if (bfd_link_pic (info
))
6068 /* Generate R_X86_64_GLOB_DAT. */
6075 if (!h
->pointer_equality_needed
)
6078 /* For non-shared object, we can't use .got.plt, which
6079 contains the real function addres if we need pointer
6080 equality. We load the GOT entry with the PLT entry. */
6081 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
6082 bfd_put_64 (output_bfd
, (plt
->output_section
->vma
6083 + plt
->output_offset
6085 htab
->elf
.sgot
->contents
+ h
->got
.offset
);
6089 else if (bfd_link_pic (info
)
6090 && SYMBOL_REFERENCES_LOCAL (info
, h
))
6092 if (!h
->def_regular
)
6094 BFD_ASSERT((h
->got
.offset
& 1) != 0);
6095 rela
.r_info
= htab
->r_info (0, R_X86_64_RELATIVE
);
6096 rela
.r_addend
= (h
->root
.u
.def
.value
6097 + h
->root
.u
.def
.section
->output_section
->vma
6098 + h
->root
.u
.def
.section
->output_offset
);
6102 BFD_ASSERT((h
->got
.offset
& 1) == 0);
6104 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
6105 htab
->elf
.sgot
->contents
+ h
->got
.offset
);
6106 rela
.r_info
= htab
->r_info (h
->dynindx
, R_X86_64_GLOB_DAT
);
6110 elf_append_rela (output_bfd
, relgot
, &rela
);
6115 Elf_Internal_Rela rela
;
6118 /* This symbol needs a copy reloc. Set it up. */
6120 if (h
->dynindx
== -1
6121 || (h
->root
.type
!= bfd_link_hash_defined
6122 && h
->root
.type
!= bfd_link_hash_defweak
)
6123 || htab
->elf
.srelbss
== NULL
6124 || htab
->elf
.sreldynrelro
== NULL
)
6127 rela
.r_offset
= (h
->root
.u
.def
.value
6128 + h
->root
.u
.def
.section
->output_section
->vma
6129 + h
->root
.u
.def
.section
->output_offset
);
6130 rela
.r_info
= htab
->r_info (h
->dynindx
, R_X86_64_COPY
);
6132 if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
6133 s
= htab
->elf
.sreldynrelro
;
6135 s
= htab
->elf
.srelbss
;
6136 elf_append_rela (output_bfd
, s
, &rela
);
6142 /* Finish up local dynamic symbol handling. We set the contents of
6143 various dynamic sections here. */
6146 elf_x86_64_finish_local_dynamic_symbol (void **slot
, void *inf
)
6148 struct elf_link_hash_entry
*h
6149 = (struct elf_link_hash_entry
*) *slot
;
6150 struct bfd_link_info
*info
6151 = (struct bfd_link_info
*) inf
;
6153 return elf_x86_64_finish_dynamic_symbol (info
->output_bfd
,
6157 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
6158 here since undefined weak symbol may not be dynamic and may not be
6159 called for elf_x86_64_finish_dynamic_symbol. */
6162 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry
*bh
,
6165 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
6166 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
6168 if (h
->root
.type
!= bfd_link_hash_undefweak
6169 || h
->dynindx
!= -1)
6172 return elf_x86_64_finish_dynamic_symbol (info
->output_bfd
,
6176 /* Used to decide how to sort relocs in an optimal manner for the
6177 dynamic linker, before writing them out. */
6179 static enum elf_reloc_type_class
6180 elf_x86_64_reloc_type_class (const struct bfd_link_info
*info
,
6181 const asection
*rel_sec ATTRIBUTE_UNUSED
,
6182 const Elf_Internal_Rela
*rela
)
6184 bfd
*abfd
= info
->output_bfd
;
6185 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6186 struct elf_x86_64_link_hash_table
*htab
= elf_x86_64_hash_table (info
);
6188 if (htab
->elf
.dynsym
!= NULL
6189 && htab
->elf
.dynsym
->contents
!= NULL
)
6191 /* Check relocation against STT_GNU_IFUNC symbol if there are
6193 unsigned long r_symndx
= htab
->r_sym (rela
->r_info
);
6194 if (r_symndx
!= STN_UNDEF
)
6196 Elf_Internal_Sym sym
;
6197 if (!bed
->s
->swap_symbol_in (abfd
,
6198 (htab
->elf
.dynsym
->contents
6199 + r_symndx
* bed
->s
->sizeof_sym
),
6203 if (ELF_ST_TYPE (sym
.st_info
) == STT_GNU_IFUNC
)
6204 return reloc_class_ifunc
;
6208 switch ((int) ELF32_R_TYPE (rela
->r_info
))
6210 case R_X86_64_IRELATIVE
:
6211 return reloc_class_ifunc
;
6212 case R_X86_64_RELATIVE
:
6213 case R_X86_64_RELATIVE64
:
6214 return reloc_class_relative
;
6215 case R_X86_64_JUMP_SLOT
:
6216 return reloc_class_plt
;
6218 return reloc_class_copy
;
6220 return reloc_class_normal
;
6224 /* Finish up the dynamic sections. */
6227 elf_x86_64_finish_dynamic_sections (bfd
*output_bfd
,
6228 struct bfd_link_info
*info
)
6230 struct elf_x86_64_link_hash_table
*htab
;
6233 const struct elf_x86_64_backend_data
*abed
;
6235 htab
= elf_x86_64_hash_table (info
);
6239 /* Use MPX backend data in case of BND relocation. Use .plt_bnd
6240 section only if there is .plt section. */
6241 abed
= (htab
->elf
.splt
!= NULL
&& htab
->plt_bnd
!= NULL
6242 ? &elf_x86_64_bnd_arch_bed
6243 : get_elf_x86_64_backend_data (output_bfd
));
6245 dynobj
= htab
->elf
.dynobj
;
6246 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
6248 if (htab
->elf
.dynamic_sections_created
)
6250 bfd_byte
*dyncon
, *dynconend
;
6251 const struct elf_backend_data
*bed
;
6252 bfd_size_type sizeof_dyn
;
6254 if (sdyn
== NULL
|| htab
->elf
.sgot
== NULL
)
6257 bed
= get_elf_backend_data (dynobj
);
6258 sizeof_dyn
= bed
->s
->sizeof_dyn
;
6259 dyncon
= sdyn
->contents
;
6260 dynconend
= sdyn
->contents
+ sdyn
->size
;
6261 for (; dyncon
< dynconend
; dyncon
+= sizeof_dyn
)
6263 Elf_Internal_Dyn dyn
;
6266 (*bed
->s
->swap_dyn_in
) (dynobj
, dyncon
, &dyn
);
6274 s
= htab
->elf
.sgotplt
;
6275 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
6279 dyn
.d_un
.d_ptr
= htab
->elf
.srelplt
->output_section
->vma
;
6283 s
= htab
->elf
.srelplt
->output_section
;
6284 dyn
.d_un
.d_val
= s
->size
;
6287 case DT_TLSDESC_PLT
:
6289 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
6290 + htab
->tlsdesc_plt
;
6293 case DT_TLSDESC_GOT
:
6295 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
6296 + htab
->tlsdesc_got
;
6300 (*bed
->s
->swap_dyn_out
) (output_bfd
, &dyn
, dyncon
);
6303 /* Fill in the special first entry in the procedure linkage table. */
6304 if (htab
->elf
.splt
&& htab
->elf
.splt
->size
> 0)
6306 /* Fill in the first entry in the procedure linkage table. */
6307 memcpy (htab
->elf
.splt
->contents
,
6308 abed
->plt0_entry
, abed
->plt_entry_size
);
6309 /* Add offset for pushq GOT+8(%rip), since the instruction
6310 uses 6 bytes subtract this value. */
6311 bfd_put_32 (output_bfd
,
6312 (htab
->elf
.sgotplt
->output_section
->vma
6313 + htab
->elf
.sgotplt
->output_offset
6315 - htab
->elf
.splt
->output_section
->vma
6316 - htab
->elf
.splt
->output_offset
6318 htab
->elf
.splt
->contents
+ abed
->plt0_got1_offset
);
6319 /* Add offset for the PC-relative instruction accessing GOT+16,
6320 subtracting the offset to the end of that instruction. */
6321 bfd_put_32 (output_bfd
,
6322 (htab
->elf
.sgotplt
->output_section
->vma
6323 + htab
->elf
.sgotplt
->output_offset
6325 - htab
->elf
.splt
->output_section
->vma
6326 - htab
->elf
.splt
->output_offset
6327 - abed
->plt0_got2_insn_end
),
6328 htab
->elf
.splt
->contents
+ abed
->plt0_got2_offset
);
6330 elf_section_data (htab
->elf
.splt
->output_section
)
6331 ->this_hdr
.sh_entsize
= abed
->plt_entry_size
;
6333 if (htab
->tlsdesc_plt
)
6335 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
6336 htab
->elf
.sgot
->contents
+ htab
->tlsdesc_got
);
6338 memcpy (htab
->elf
.splt
->contents
+ htab
->tlsdesc_plt
,
6339 abed
->plt0_entry
, abed
->plt_entry_size
);
6341 /* Add offset for pushq GOT+8(%rip), since the
6342 instruction uses 6 bytes subtract this value. */
6343 bfd_put_32 (output_bfd
,
6344 (htab
->elf
.sgotplt
->output_section
->vma
6345 + htab
->elf
.sgotplt
->output_offset
6347 - htab
->elf
.splt
->output_section
->vma
6348 - htab
->elf
.splt
->output_offset
6351 htab
->elf
.splt
->contents
6352 + htab
->tlsdesc_plt
+ abed
->plt0_got1_offset
);
6353 /* Add offset for the PC-relative instruction accessing GOT+TDG,
6354 where TGD stands for htab->tlsdesc_got, subtracting the offset
6355 to the end of that instruction. */
6356 bfd_put_32 (output_bfd
,
6357 (htab
->elf
.sgot
->output_section
->vma
6358 + htab
->elf
.sgot
->output_offset
6360 - htab
->elf
.splt
->output_section
->vma
6361 - htab
->elf
.splt
->output_offset
6363 - abed
->plt0_got2_insn_end
),
6364 htab
->elf
.splt
->contents
6365 + htab
->tlsdesc_plt
+ abed
->plt0_got2_offset
);
6370 if (htab
->plt_bnd
!= NULL
)
6371 elf_section_data (htab
->plt_bnd
->output_section
)
6372 ->this_hdr
.sh_entsize
= sizeof (elf_x86_64_bnd_plt2_entry
);
6374 if (htab
->elf
.sgotplt
)
6376 if (bfd_is_abs_section (htab
->elf
.sgotplt
->output_section
))
6379 (_("discarded output section: `%A'"), htab
->elf
.sgotplt
);
6383 /* Fill in the first three entries in the global offset table. */
6384 if (htab
->elf
.sgotplt
->size
> 0)
6386 /* Set the first entry in the global offset table to the address of
6387 the dynamic section. */
6389 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->elf
.sgotplt
->contents
);
6391 bfd_put_64 (output_bfd
,
6392 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
6393 htab
->elf
.sgotplt
->contents
);
6394 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6395 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
6396 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
*2);
6399 elf_section_data (htab
->elf
.sgotplt
->output_section
)->this_hdr
.sh_entsize
=
6403 /* Adjust .eh_frame for .plt section. */
6404 if (htab
->plt_eh_frame
!= NULL
6405 && htab
->plt_eh_frame
->contents
!= NULL
)
6407 if (htab
->elf
.splt
!= NULL
6408 && htab
->elf
.splt
->size
!= 0
6409 && (htab
->elf
.splt
->flags
& SEC_EXCLUDE
) == 0
6410 && htab
->elf
.splt
->output_section
!= NULL
6411 && htab
->plt_eh_frame
->output_section
!= NULL
)
6413 bfd_vma plt_start
= htab
->elf
.splt
->output_section
->vma
;
6414 bfd_vma eh_frame_start
= htab
->plt_eh_frame
->output_section
->vma
6415 + htab
->plt_eh_frame
->output_offset
6416 + PLT_FDE_START_OFFSET
;
6417 bfd_put_signed_32 (dynobj
, plt_start
- eh_frame_start
,
6418 htab
->plt_eh_frame
->contents
6419 + PLT_FDE_START_OFFSET
);
6421 if (htab
->plt_eh_frame
->sec_info_type
== SEC_INFO_TYPE_EH_FRAME
)
6423 if (! _bfd_elf_write_section_eh_frame (output_bfd
, info
,
6425 htab
->plt_eh_frame
->contents
))
6430 /* Adjust .eh_frame for .plt.got section. */
6431 if (htab
->plt_got_eh_frame
!= NULL
6432 && htab
->plt_got_eh_frame
->contents
!= NULL
)
6434 if (htab
->plt_got
!= NULL
6435 && htab
->plt_got
->size
!= 0
6436 && (htab
->plt_got
->flags
& SEC_EXCLUDE
) == 0
6437 && htab
->plt_got
->output_section
!= NULL
6438 && htab
->plt_got_eh_frame
->output_section
!= NULL
)
6440 bfd_vma plt_start
= htab
->plt_got
->output_section
->vma
;
6441 bfd_vma eh_frame_start
= htab
->plt_got_eh_frame
->output_section
->vma
6442 + htab
->plt_got_eh_frame
->output_offset
6443 + PLT_FDE_START_OFFSET
;
6444 bfd_put_signed_32 (dynobj
, plt_start
- eh_frame_start
,
6445 htab
->plt_got_eh_frame
->contents
6446 + PLT_FDE_START_OFFSET
);
6448 if (htab
->plt_got_eh_frame
->sec_info_type
== SEC_INFO_TYPE_EH_FRAME
)
6450 if (! _bfd_elf_write_section_eh_frame (output_bfd
, info
,
6451 htab
->plt_got_eh_frame
,
6452 htab
->plt_got_eh_frame
->contents
))
6457 if (htab
->elf
.sgot
&& htab
->elf
.sgot
->size
> 0)
6458 elf_section_data (htab
->elf
.sgot
->output_section
)->this_hdr
.sh_entsize
6461 /* Fill PLT entries for undefined weak symbols in PIE. */
6462 if (bfd_link_pie (info
))
6463 bfd_hash_traverse (&info
->hash
->table
,
6464 elf_x86_64_pie_finish_undefweak_symbol
,
6470 /* Fill PLT/GOT entries and allocate dynamic relocations for local
6471 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6472 It has to be done before elf_link_sort_relocs is called so that
6473 dynamic relocations are properly sorted. */
6476 elf_x86_64_output_arch_local_syms
6477 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
6478 struct bfd_link_info
*info
,
6479 void *flaginfo ATTRIBUTE_UNUSED
,
6480 int (*func
) (void *, const char *,
6483 struct elf_link_hash_entry
*) ATTRIBUTE_UNUSED
)
6485 struct elf_x86_64_link_hash_table
*htab
= elf_x86_64_hash_table (info
);
6489 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6490 htab_traverse (htab
->loc_hash_table
,
6491 elf_x86_64_finish_local_dynamic_symbol
,
6497 /* Return an array of PLT entry symbol values. */
6500 elf_x86_64_get_plt_sym_val (bfd
*abfd
, asymbol
**dynsyms
, asection
*plt
,
6503 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
6506 bfd_vma
*plt_sym_val
;
6508 bfd_byte
*plt_contents
;
6509 const struct elf_x86_64_backend_data
*bed
;
6510 Elf_Internal_Shdr
*hdr
;
6513 /* Get the .plt section contents. PLT passed down may point to the
6514 .plt.bnd section. Make sure that PLT always points to the .plt
6516 plt_bnd
= bfd_get_section_by_name (abfd
, ".plt.bnd");
6521 plt
= bfd_get_section_by_name (abfd
, ".plt");
6524 bed
= &elf_x86_64_bnd_arch_bed
;
6527 bed
= get_elf_x86_64_backend_data (abfd
);
6529 plt_contents
= (bfd_byte
*) bfd_malloc (plt
->size
);
6530 if (plt_contents
== NULL
)
6532 if (!bfd_get_section_contents (abfd
, (asection
*) plt
,
6533 plt_contents
, 0, plt
->size
))
6536 free (plt_contents
);
6540 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
6541 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
6544 hdr
= &elf_section_data (relplt
)->this_hdr
;
6545 count
= relplt
->size
/ hdr
->sh_entsize
;
6547 plt_sym_val
= (bfd_vma
*) bfd_malloc (sizeof (bfd_vma
) * count
);
6548 if (plt_sym_val
== NULL
)
6551 for (i
= 0; i
< count
; i
++)
6552 plt_sym_val
[i
] = -1;
6554 plt_offset
= bed
->plt_entry_size
;
6555 p
= relplt
->relocation
;
6556 for (i
= 0; i
< count
; i
++, p
++)
6560 /* Skip unknown relocation. */
6561 if (p
->howto
== NULL
)
6564 if (p
->howto
->type
!= R_X86_64_JUMP_SLOT
6565 && p
->howto
->type
!= R_X86_64_IRELATIVE
)
6568 reloc_index
= H_GET_32 (abfd
, (plt_contents
+ plt_offset
6569 + bed
->plt_reloc_offset
));
6570 if (reloc_index
< count
)
6574 /* This is the index in .plt section. */
6575 long plt_index
= plt_offset
/ bed
->plt_entry_size
;
6576 /* Store VMA + the offset in .plt.bnd section. */
6577 plt_sym_val
[reloc_index
] =
6579 + (plt_index
- 1) * sizeof (elf_x86_64_legacy_plt2_entry
));
6582 plt_sym_val
[reloc_index
] = plt
->vma
+ plt_offset
;
6584 plt_offset
+= bed
->plt_entry_size
;
6586 /* PR binutils/18437: Skip extra relocations in the .rela.plt
6588 if (plt_offset
>= plt
->size
)
6592 free (plt_contents
);
6597 /* Similar to _bfd_elf_get_synthetic_symtab, with .plt.bnd section
6601 elf_x86_64_get_synthetic_symtab (bfd
*abfd
,
6608 /* Pass the .plt.bnd section to _bfd_elf_ifunc_get_synthetic_symtab
6609 as PLT if it exists. */
6610 asection
*plt
= bfd_get_section_by_name (abfd
, ".plt.bnd");
6612 plt
= bfd_get_section_by_name (abfd
, ".plt");
6613 return _bfd_elf_ifunc_get_synthetic_symtab (abfd
, symcount
, syms
,
6614 dynsymcount
, dynsyms
, ret
,
6616 elf_x86_64_get_plt_sym_val
);
6619 /* Handle an x86-64 specific section when reading an object file. This
6620 is called when elfcode.h finds a section with an unknown type. */
6623 elf_x86_64_section_from_shdr (bfd
*abfd
, Elf_Internal_Shdr
*hdr
,
6624 const char *name
, int shindex
)
6626 if (hdr
->sh_type
!= SHT_X86_64_UNWIND
)
6629 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
6635 /* Hook called by the linker routine which adds symbols from an object
6636 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
6640 elf_x86_64_add_symbol_hook (bfd
*abfd
,
6641 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6642 Elf_Internal_Sym
*sym
,
6643 const char **namep ATTRIBUTE_UNUSED
,
6644 flagword
*flagsp ATTRIBUTE_UNUSED
,
6650 switch (sym
->st_shndx
)
6652 case SHN_X86_64_LCOMMON
:
6653 lcomm
= bfd_get_section_by_name (abfd
, "LARGE_COMMON");
6656 lcomm
= bfd_make_section_with_flags (abfd
,
6660 | SEC_LINKER_CREATED
));
6663 elf_section_flags (lcomm
) |= SHF_X86_64_LARGE
;
6666 *valp
= sym
->st_size
;
6674 /* Given a BFD section, try to locate the corresponding ELF section
6678 elf_x86_64_elf_section_from_bfd_section (bfd
*abfd ATTRIBUTE_UNUSED
,
6679 asection
*sec
, int *index_return
)
6681 if (sec
== &_bfd_elf_large_com_section
)
6683 *index_return
= SHN_X86_64_LCOMMON
;
6689 /* Process a symbol. */
6692 elf_x86_64_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
6695 elf_symbol_type
*elfsym
= (elf_symbol_type
*) asym
;
6697 switch (elfsym
->internal_elf_sym
.st_shndx
)
6699 case SHN_X86_64_LCOMMON
:
6700 asym
->section
= &_bfd_elf_large_com_section
;
6701 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
6702 /* Common symbol doesn't set BSF_GLOBAL. */
6703 asym
->flags
&= ~BSF_GLOBAL
;
6709 elf_x86_64_common_definition (Elf_Internal_Sym
*sym
)
6711 return (sym
->st_shndx
== SHN_COMMON
6712 || sym
->st_shndx
== SHN_X86_64_LCOMMON
);
6716 elf_x86_64_common_section_index (asection
*sec
)
6718 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
6721 return SHN_X86_64_LCOMMON
;
6725 elf_x86_64_common_section (asection
*sec
)
6727 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
6728 return bfd_com_section_ptr
;
6730 return &_bfd_elf_large_com_section
;
6734 elf_x86_64_merge_symbol (struct elf_link_hash_entry
*h
,
6735 const Elf_Internal_Sym
*sym
,
6740 const asection
*oldsec
)
6742 /* A normal common symbol and a large common symbol result in a
6743 normal common symbol. We turn the large common symbol into a
6746 && h
->root
.type
== bfd_link_hash_common
6748 && bfd_is_com_section (*psec
)
6751 if (sym
->st_shndx
== SHN_COMMON
6752 && (elf_section_flags (oldsec
) & SHF_X86_64_LARGE
) != 0)
6754 h
->root
.u
.c
.p
->section
6755 = bfd_make_section_old_way (oldbfd
, "COMMON");
6756 h
->root
.u
.c
.p
->section
->flags
= SEC_ALLOC
;
6758 else if (sym
->st_shndx
== SHN_X86_64_LCOMMON
6759 && (elf_section_flags (oldsec
) & SHF_X86_64_LARGE
) == 0)
6760 *psec
= bfd_com_section_ptr
;
6767 elf_x86_64_additional_program_headers (bfd
*abfd
,
6768 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6773 /* Check to see if we need a large readonly segment. */
6774 s
= bfd_get_section_by_name (abfd
, ".lrodata");
6775 if (s
&& (s
->flags
& SEC_LOAD
))
6778 /* Check to see if we need a large data segment. Since .lbss sections
6779 is placed right after the .bss section, there should be no need for
6780 a large data segment just because of .lbss. */
6781 s
= bfd_get_section_by_name (abfd
, ".ldata");
6782 if (s
&& (s
->flags
& SEC_LOAD
))
6788 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6791 elf_x86_64_hash_symbol (struct elf_link_hash_entry
*h
)
6793 if (h
->plt
.offset
!= (bfd_vma
) -1
6795 && !h
->pointer_equality_needed
)
6798 return _bfd_elf_hash_symbol (h
);
6801 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
6804 elf_x86_64_relocs_compatible (const bfd_target
*input
,
6805 const bfd_target
*output
)
6807 return ((xvec_get_elf_backend_data (input
)->s
->elfclass
6808 == xvec_get_elf_backend_data (output
)->s
->elfclass
)
6809 && _bfd_elf_relocs_compatible (input
, output
));
6812 static const struct bfd_elf_special_section
6813 elf_x86_64_special_sections
[]=
6815 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
6816 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
6817 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
+ SHF_X86_64_LARGE
},
6818 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
6819 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
6820 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
6821 { NULL
, 0, 0, 0, 0 }
6824 #define TARGET_LITTLE_SYM x86_64_elf64_vec
6825 #define TARGET_LITTLE_NAME "elf64-x86-64"
6826 #define ELF_ARCH bfd_arch_i386
6827 #define ELF_TARGET_ID X86_64_ELF_DATA
6828 #define ELF_MACHINE_CODE EM_X86_64
6829 #define ELF_MAXPAGESIZE 0x200000
6830 #define ELF_MINPAGESIZE 0x1000
6831 #define ELF_COMMONPAGESIZE 0x1000
6833 #define elf_backend_can_gc_sections 1
6834 #define elf_backend_can_refcount 1
6835 #define elf_backend_want_got_plt 1
6836 #define elf_backend_plt_readonly 1
6837 #define elf_backend_want_plt_sym 0
6838 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
6839 #define elf_backend_rela_normal 1
6840 #define elf_backend_plt_alignment 4
6841 #define elf_backend_extern_protected_data 1
6842 #define elf_backend_caches_rawsize 1
6843 #define elf_backend_dtrel_excludes_plt 1
6844 #define elf_backend_want_dynrelro 1
6846 #define elf_info_to_howto elf_x86_64_info_to_howto
6848 #define bfd_elf64_bfd_link_hash_table_create \
6849 elf_x86_64_link_hash_table_create
6850 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
6851 #define bfd_elf64_bfd_reloc_name_lookup \
6852 elf_x86_64_reloc_name_lookup
6854 #define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
6855 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
6856 #define elf_backend_check_relocs elf_x86_64_check_relocs
6857 #define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
6858 #define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
6859 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
6860 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
6861 #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
6862 #define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
6863 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
6864 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
6866 #define elf_backend_write_core_note elf_x86_64_write_core_note
6868 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
6869 #define elf_backend_relocate_section elf_x86_64_relocate_section
6870 #define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
6871 #define elf_backend_always_size_sections elf_x86_64_always_size_sections
6872 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
6873 #define elf_backend_object_p elf64_x86_64_elf_object_p
6874 #define bfd_elf64_mkobject elf_x86_64_mkobject
6875 #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
6877 #define elf_backend_section_from_shdr \
6878 elf_x86_64_section_from_shdr
6880 #define elf_backend_section_from_bfd_section \
6881 elf_x86_64_elf_section_from_bfd_section
6882 #define elf_backend_add_symbol_hook \
6883 elf_x86_64_add_symbol_hook
6884 #define elf_backend_symbol_processing \
6885 elf_x86_64_symbol_processing
6886 #define elf_backend_common_section_index \
6887 elf_x86_64_common_section_index
6888 #define elf_backend_common_section \
6889 elf_x86_64_common_section
6890 #define elf_backend_common_definition \
6891 elf_x86_64_common_definition
6892 #define elf_backend_merge_symbol \
6893 elf_x86_64_merge_symbol
6894 #define elf_backend_special_sections \
6895 elf_x86_64_special_sections
6896 #define elf_backend_additional_program_headers \
6897 elf_x86_64_additional_program_headers
6898 #define elf_backend_hash_symbol \
6899 elf_x86_64_hash_symbol
6900 #define elf_backend_omit_section_dynsym \
6901 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
6902 #define elf_backend_fixup_symbol \
6903 elf_x86_64_fixup_symbol
6905 #include "elf64-target.h"
6907 /* CloudABI support. */
6909 #undef TARGET_LITTLE_SYM
6910 #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
6911 #undef TARGET_LITTLE_NAME
6912 #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
6915 #define ELF_OSABI ELFOSABI_CLOUDABI
6918 #define elf64_bed elf64_x86_64_cloudabi_bed
6920 #include "elf64-target.h"
6922 /* FreeBSD support. */
6924 #undef TARGET_LITTLE_SYM
6925 #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
6926 #undef TARGET_LITTLE_NAME
6927 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
6930 #define ELF_OSABI ELFOSABI_FREEBSD
6933 #define elf64_bed elf64_x86_64_fbsd_bed
6935 #include "elf64-target.h"
6937 /* Solaris 2 support. */
6939 #undef TARGET_LITTLE_SYM
6940 #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
6941 #undef TARGET_LITTLE_NAME
6942 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
6944 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
6945 objects won't be recognized. */
6949 #define elf64_bed elf64_x86_64_sol2_bed
6951 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
6953 #undef elf_backend_static_tls_alignment
6954 #define elf_backend_static_tls_alignment 16
6956 /* The Solaris 2 ABI requires a plt symbol on all platforms.
6958 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6960 #undef elf_backend_want_plt_sym
6961 #define elf_backend_want_plt_sym 1
6963 #undef elf_backend_strtab_flags
6964 #define elf_backend_strtab_flags SHF_STRINGS
6967 elf64_x86_64_copy_solaris_special_section_fields (const bfd
*ibfd ATTRIBUTE_UNUSED
,
6968 bfd
*obfd ATTRIBUTE_UNUSED
,
6969 const Elf_Internal_Shdr
*isection ATTRIBUTE_UNUSED
,
6970 Elf_Internal_Shdr
*osection ATTRIBUTE_UNUSED
)
6972 /* PR 19938: FIXME: Need to add code for setting the sh_info
6973 and sh_link fields of Solaris specific section types. */
6977 #undef elf_backend_copy_special_section_fields
6978 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
6980 #include "elf64-target.h"
6982 /* Native Client support. */
6985 elf64_x86_64_nacl_elf_object_p (bfd
*abfd
)
6987 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
6988 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64_nacl
);
6992 #undef TARGET_LITTLE_SYM
6993 #define TARGET_LITTLE_SYM x86_64_elf64_nacl_vec
6994 #undef TARGET_LITTLE_NAME
6995 #define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
6997 #define elf64_bed elf64_x86_64_nacl_bed
6999 #undef ELF_MAXPAGESIZE
7000 #undef ELF_MINPAGESIZE
7001 #undef ELF_COMMONPAGESIZE
7002 #define ELF_MAXPAGESIZE 0x10000
7003 #define ELF_MINPAGESIZE 0x10000
7004 #define ELF_COMMONPAGESIZE 0x10000
7006 /* Restore defaults. */
7008 #undef elf_backend_static_tls_alignment
7009 #undef elf_backend_want_plt_sym
7010 #define elf_backend_want_plt_sym 0
7011 #undef elf_backend_strtab_flags
7012 #undef elf_backend_copy_special_section_fields
7014 /* NaCl uses substantially different PLT entries for the same effects. */
7016 #undef elf_backend_plt_alignment
7017 #define elf_backend_plt_alignment 5
7018 #define NACL_PLT_ENTRY_SIZE 64
7019 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
7021 static const bfd_byte elf_x86_64_nacl_plt0_entry
[NACL_PLT_ENTRY_SIZE
] =
7023 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
7024 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
7025 0x41, 0x83, 0xe3, NACLMASK
, /* and $-32, %r11d */
7026 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
7027 0x41, 0xff, 0xe3, /* jmpq *%r11 */
7029 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
7030 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
7032 /* 32 bytes of nop to pad out to the standard size. */
7033 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7034 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7035 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7036 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7037 0x66, /* excess data16 prefix */
7041 static const bfd_byte elf_x86_64_nacl_plt_entry
[NACL_PLT_ENTRY_SIZE
] =
7043 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
7044 0x41, 0x83, 0xe3, NACLMASK
, /* and $-32, %r11d */
7045 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
7046 0x41, 0xff, 0xe3, /* jmpq *%r11 */
7048 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
7049 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7050 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7052 /* Lazy GOT entries point here (32-byte aligned). */
7053 0x68, /* pushq immediate */
7054 0, 0, 0, 0, /* replaced with index into relocation table. */
7055 0xe9, /* jmp relative */
7056 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
7058 /* 22 bytes of nop to pad out to the standard size. */
7059 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7060 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7061 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
7064 /* .eh_frame covering the .plt section. */
7066 static const bfd_byte elf_x86_64_nacl_eh_frame_plt
[] =
7068 #if (PLT_CIE_LENGTH != 20 \
7069 || PLT_FDE_LENGTH != 36 \
7070 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
7071 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
7072 # error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
7074 PLT_CIE_LENGTH
, 0, 0, 0, /* CIE length */
7075 0, 0, 0, 0, /* CIE ID */
7076 1, /* CIE version */
7077 'z', 'R', 0, /* Augmentation string */
7078 1, /* Code alignment factor */
7079 0x78, /* Data alignment factor */
7080 16, /* Return address column */
7081 1, /* Augmentation size */
7082 DW_EH_PE_pcrel
| DW_EH_PE_sdata4
, /* FDE encoding */
7083 DW_CFA_def_cfa
, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
7084 DW_CFA_offset
+ 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
7085 DW_CFA_nop
, DW_CFA_nop
,
7087 PLT_FDE_LENGTH
, 0, 0, 0, /* FDE length */
7088 PLT_CIE_LENGTH
+ 8, 0, 0, 0,/* CIE pointer */
7089 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
7090 0, 0, 0, 0, /* .plt size goes here */
7091 0, /* Augmentation size */
7092 DW_CFA_def_cfa_offset
, 16, /* DW_CFA_def_cfa_offset: 16 */
7093 DW_CFA_advance_loc
+ 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
7094 DW_CFA_def_cfa_offset
, 24, /* DW_CFA_def_cfa_offset: 24 */
7095 DW_CFA_advance_loc
+ 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
7096 DW_CFA_def_cfa_expression
, /* DW_CFA_def_cfa_expression */
7097 13, /* Block length */
7098 DW_OP_breg7
, 8, /* DW_OP_breg7 (rsp): 8 */
7099 DW_OP_breg16
, 0, /* DW_OP_breg16 (rip): 0 */
7100 DW_OP_const1u
, 63, DW_OP_and
, DW_OP_const1u
, 37, DW_OP_ge
,
7101 DW_OP_lit3
, DW_OP_shl
, DW_OP_plus
,
7102 DW_CFA_nop
, DW_CFA_nop
7105 static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed
=
7107 elf_x86_64_nacl_plt0_entry
, /* plt0_entry */
7108 elf_x86_64_nacl_plt_entry
, /* plt_entry */
7109 NACL_PLT_ENTRY_SIZE
, /* plt_entry_size */
7110 2, /* plt0_got1_offset */
7111 9, /* plt0_got2_offset */
7112 13, /* plt0_got2_insn_end */
7113 3, /* plt_got_offset */
7114 33, /* plt_reloc_offset */
7115 38, /* plt_plt_offset */
7116 7, /* plt_got_insn_size */
7117 42, /* plt_plt_insn_end */
7118 32, /* plt_lazy_offset */
7119 elf_x86_64_nacl_eh_frame_plt
, /* eh_frame_plt */
7120 sizeof (elf_x86_64_nacl_eh_frame_plt
), /* eh_frame_plt_size */
7121 NULL
, /* eh_frame_plt_got */
7122 0, /* eh_frame_plt_got_size */
7125 #undef elf_backend_arch_data
7126 #define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
7128 #undef elf_backend_object_p
7129 #define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
7130 #undef elf_backend_modify_segment_map
7131 #define elf_backend_modify_segment_map nacl_modify_segment_map
7132 #undef elf_backend_modify_program_headers
7133 #define elf_backend_modify_program_headers nacl_modify_program_headers
7134 #undef elf_backend_final_write_processing
7135 #define elf_backend_final_write_processing nacl_final_write_processing
7137 #include "elf64-target.h"
7139 /* Native Client x32 support. */
7142 elf32_x86_64_nacl_elf_object_p (bfd
*abfd
)
7144 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
7145 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x64_32_nacl
);
7149 #undef TARGET_LITTLE_SYM
7150 #define TARGET_LITTLE_SYM x86_64_elf32_nacl_vec
7151 #undef TARGET_LITTLE_NAME
7152 #define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
7154 #define elf32_bed elf32_x86_64_nacl_bed
7156 #define bfd_elf32_bfd_link_hash_table_create \
7157 elf_x86_64_link_hash_table_create
7158 #define bfd_elf32_bfd_reloc_type_lookup \
7159 elf_x86_64_reloc_type_lookup
7160 #define bfd_elf32_bfd_reloc_name_lookup \
7161 elf_x86_64_reloc_name_lookup
7162 #define bfd_elf32_mkobject \
7164 #define bfd_elf32_get_synthetic_symtab \
7165 elf_x86_64_get_synthetic_symtab
7167 #undef elf_backend_object_p
7168 #define elf_backend_object_p \
7169 elf32_x86_64_nacl_elf_object_p
7171 #undef elf_backend_bfd_from_remote_memory
7172 #define elf_backend_bfd_from_remote_memory \
7173 _bfd_elf32_bfd_from_remote_memory
7175 #undef elf_backend_size_info
7176 #define elf_backend_size_info \
7177 _bfd_elf32_size_info
7179 #include "elf32-target.h"
7181 /* Restore defaults. */
7182 #undef elf_backend_object_p
7183 #define elf_backend_object_p elf64_x86_64_elf_object_p
7184 #undef elf_backend_bfd_from_remote_memory
7185 #undef elf_backend_size_info
7186 #undef elf_backend_modify_segment_map
7187 #undef elf_backend_modify_program_headers
7188 #undef elf_backend_final_write_processing
7190 /* Intel L1OM support. */
7193 elf64_l1om_elf_object_p (bfd
*abfd
)
7195 /* Set the right machine number for an L1OM elf64 file. */
7196 bfd_default_set_arch_mach (abfd
, bfd_arch_l1om
, bfd_mach_l1om
);
7200 #undef TARGET_LITTLE_SYM
7201 #define TARGET_LITTLE_SYM l1om_elf64_vec
7202 #undef TARGET_LITTLE_NAME
7203 #define TARGET_LITTLE_NAME "elf64-l1om"
7205 #define ELF_ARCH bfd_arch_l1om
7207 #undef ELF_MACHINE_CODE
7208 #define ELF_MACHINE_CODE EM_L1OM
7213 #define elf64_bed elf64_l1om_bed
7215 #undef elf_backend_object_p
7216 #define elf_backend_object_p elf64_l1om_elf_object_p
7218 /* Restore defaults. */
7219 #undef ELF_MAXPAGESIZE
7220 #undef ELF_MINPAGESIZE
7221 #undef ELF_COMMONPAGESIZE
7222 #define ELF_MAXPAGESIZE 0x200000
7223 #define ELF_MINPAGESIZE 0x1000
7224 #define ELF_COMMONPAGESIZE 0x1000
7225 #undef elf_backend_plt_alignment
7226 #define elf_backend_plt_alignment 4
7227 #undef elf_backend_arch_data
7228 #define elf_backend_arch_data &elf_x86_64_arch_bed
7230 #include "elf64-target.h"
7232 /* FreeBSD L1OM support. */
7234 #undef TARGET_LITTLE_SYM
7235 #define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
7236 #undef TARGET_LITTLE_NAME
7237 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
7240 #define ELF_OSABI ELFOSABI_FREEBSD
7243 #define elf64_bed elf64_l1om_fbsd_bed
7245 #include "elf64-target.h"
7247 /* Intel K1OM support. */
7250 elf64_k1om_elf_object_p (bfd
*abfd
)
7252 /* Set the right machine number for an K1OM elf64 file. */
7253 bfd_default_set_arch_mach (abfd
, bfd_arch_k1om
, bfd_mach_k1om
);
7257 #undef TARGET_LITTLE_SYM
7258 #define TARGET_LITTLE_SYM k1om_elf64_vec
7259 #undef TARGET_LITTLE_NAME
7260 #define TARGET_LITTLE_NAME "elf64-k1om"
7262 #define ELF_ARCH bfd_arch_k1om
7264 #undef ELF_MACHINE_CODE
7265 #define ELF_MACHINE_CODE EM_K1OM
7270 #define elf64_bed elf64_k1om_bed
7272 #undef elf_backend_object_p
7273 #define elf_backend_object_p elf64_k1om_elf_object_p
7275 #undef elf_backend_static_tls_alignment
7277 #undef elf_backend_want_plt_sym
7278 #define elf_backend_want_plt_sym 0
7280 #include "elf64-target.h"
7282 /* FreeBSD K1OM support. */
7284 #undef TARGET_LITTLE_SYM
7285 #define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
7286 #undef TARGET_LITTLE_NAME
7287 #define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
7290 #define ELF_OSABI ELFOSABI_FREEBSD
7293 #define elf64_bed elf64_k1om_fbsd_bed
7295 #include "elf64-target.h"
7297 /* 32bit x86-64 support. */
7299 #undef TARGET_LITTLE_SYM
7300 #define TARGET_LITTLE_SYM x86_64_elf32_vec
7301 #undef TARGET_LITTLE_NAME
7302 #define TARGET_LITTLE_NAME "elf32-x86-64"
7306 #define ELF_ARCH bfd_arch_i386
7308 #undef ELF_MACHINE_CODE
7309 #define ELF_MACHINE_CODE EM_X86_64
7313 #undef elf_backend_object_p
7314 #define elf_backend_object_p \
7315 elf32_x86_64_elf_object_p
7317 #undef elf_backend_bfd_from_remote_memory
7318 #define elf_backend_bfd_from_remote_memory \
7319 _bfd_elf32_bfd_from_remote_memory
7321 #undef elf_backend_size_info
7322 #define elf_backend_size_info \
7323 _bfd_elf32_size_info
7325 #include "elf32-target.h"