1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 #include "bfd_stdint.h"
30 #include "elf/x86-64.h"
32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33 #define MINUS_ONE (~ (bfd_vma) 0)
35 /* The relocation "howto" table. Order of fields:
36 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
37 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
38 static reloc_howto_type x86_64_elf_howto_table
[] =
40 HOWTO(R_X86_64_NONE
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
41 bfd_elf_generic_reloc
, "R_X86_64_NONE", FALSE
, 0x00000000, 0x00000000,
43 HOWTO(R_X86_64_64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
44 bfd_elf_generic_reloc
, "R_X86_64_64", FALSE
, MINUS_ONE
, MINUS_ONE
,
46 HOWTO(R_X86_64_PC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
47 bfd_elf_generic_reloc
, "R_X86_64_PC32", FALSE
, 0xffffffff, 0xffffffff,
49 HOWTO(R_X86_64_GOT32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
50 bfd_elf_generic_reloc
, "R_X86_64_GOT32", FALSE
, 0xffffffff, 0xffffffff,
52 HOWTO(R_X86_64_PLT32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
53 bfd_elf_generic_reloc
, "R_X86_64_PLT32", FALSE
, 0xffffffff, 0xffffffff,
55 HOWTO(R_X86_64_COPY
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
56 bfd_elf_generic_reloc
, "R_X86_64_COPY", FALSE
, 0xffffffff, 0xffffffff,
58 HOWTO(R_X86_64_GLOB_DAT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
59 bfd_elf_generic_reloc
, "R_X86_64_GLOB_DAT", FALSE
, MINUS_ONE
,
61 HOWTO(R_X86_64_JUMP_SLOT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
62 bfd_elf_generic_reloc
, "R_X86_64_JUMP_SLOT", FALSE
, MINUS_ONE
,
64 HOWTO(R_X86_64_RELATIVE
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
65 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE", FALSE
, MINUS_ONE
,
67 HOWTO(R_X86_64_GOTPCREL
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
68 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL", FALSE
, 0xffffffff,
70 HOWTO(R_X86_64_32
, 0, 2, 32, FALSE
, 0, complain_overflow_unsigned
,
71 bfd_elf_generic_reloc
, "R_X86_64_32", FALSE
, 0xffffffff, 0xffffffff,
73 HOWTO(R_X86_64_32S
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
74 bfd_elf_generic_reloc
, "R_X86_64_32S", FALSE
, 0xffffffff, 0xffffffff,
76 HOWTO(R_X86_64_16
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
,
77 bfd_elf_generic_reloc
, "R_X86_64_16", FALSE
, 0xffff, 0xffff, FALSE
),
78 HOWTO(R_X86_64_PC16
,0, 1, 16, TRUE
, 0, complain_overflow_bitfield
,
79 bfd_elf_generic_reloc
, "R_X86_64_PC16", FALSE
, 0xffff, 0xffff, TRUE
),
80 HOWTO(R_X86_64_8
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
,
81 bfd_elf_generic_reloc
, "R_X86_64_8", FALSE
, 0xff, 0xff, FALSE
),
82 HOWTO(R_X86_64_PC8
, 0, 0, 8, TRUE
, 0, complain_overflow_signed
,
83 bfd_elf_generic_reloc
, "R_X86_64_PC8", FALSE
, 0xff, 0xff, TRUE
),
84 HOWTO(R_X86_64_DTPMOD64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
85 bfd_elf_generic_reloc
, "R_X86_64_DTPMOD64", FALSE
, MINUS_ONE
,
87 HOWTO(R_X86_64_DTPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
88 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF64", FALSE
, MINUS_ONE
,
90 HOWTO(R_X86_64_TPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
91 bfd_elf_generic_reloc
, "R_X86_64_TPOFF64", FALSE
, MINUS_ONE
,
93 HOWTO(R_X86_64_TLSGD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
94 bfd_elf_generic_reloc
, "R_X86_64_TLSGD", FALSE
, 0xffffffff,
96 HOWTO(R_X86_64_TLSLD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
97 bfd_elf_generic_reloc
, "R_X86_64_TLSLD", FALSE
, 0xffffffff,
99 HOWTO(R_X86_64_DTPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
100 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF32", FALSE
, 0xffffffff,
102 HOWTO(R_X86_64_GOTTPOFF
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
103 bfd_elf_generic_reloc
, "R_X86_64_GOTTPOFF", FALSE
, 0xffffffff,
105 HOWTO(R_X86_64_TPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
106 bfd_elf_generic_reloc
, "R_X86_64_TPOFF32", FALSE
, 0xffffffff,
108 HOWTO(R_X86_64_PC64
, 0, 4, 64, TRUE
, 0, complain_overflow_bitfield
,
109 bfd_elf_generic_reloc
, "R_X86_64_PC64", FALSE
, MINUS_ONE
, MINUS_ONE
,
111 HOWTO(R_X86_64_GOTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
112 bfd_elf_generic_reloc
, "R_X86_64_GOTOFF64",
113 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
114 HOWTO(R_X86_64_GOTPC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
115 bfd_elf_generic_reloc
, "R_X86_64_GOTPC32",
116 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
117 HOWTO(R_X86_64_GOT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
118 bfd_elf_generic_reloc
, "R_X86_64_GOT64", FALSE
, MINUS_ONE
, MINUS_ONE
,
120 HOWTO(R_X86_64_GOTPCREL64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
121 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL64", FALSE
, MINUS_ONE
,
123 HOWTO(R_X86_64_GOTPC64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
124 bfd_elf_generic_reloc
, "R_X86_64_GOTPC64",
125 FALSE
, MINUS_ONE
, MINUS_ONE
, TRUE
),
126 HOWTO(R_X86_64_GOTPLT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
127 bfd_elf_generic_reloc
, "R_X86_64_GOTPLT64", FALSE
, MINUS_ONE
,
129 HOWTO(R_X86_64_PLTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
130 bfd_elf_generic_reloc
, "R_X86_64_PLTOFF64", FALSE
, MINUS_ONE
,
134 HOWTO(R_X86_64_GOTPC32_TLSDESC
, 0, 2, 32, TRUE
, 0,
135 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
136 "R_X86_64_GOTPC32_TLSDESC",
137 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
138 HOWTO(R_X86_64_TLSDESC_CALL
, 0, 0, 0, FALSE
, 0,
139 complain_overflow_dont
, bfd_elf_generic_reloc
,
140 "R_X86_64_TLSDESC_CALL",
142 HOWTO(R_X86_64_TLSDESC
, 0, 4, 64, FALSE
, 0,
143 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
145 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
147 /* We have a gap in the reloc numbers here.
148 R_X86_64_standard counts the number up to this point, and
149 R_X86_64_vt_offset is the value to subtract from a reloc type of
150 R_X86_64_GNU_VT* to form an index into this table. */
151 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
152 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
154 /* GNU extension to record C++ vtable hierarchy. */
155 HOWTO (R_X86_64_GNU_VTINHERIT
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
156 NULL
, "R_X86_64_GNU_VTINHERIT", FALSE
, 0, 0, FALSE
),
158 /* GNU extension to record C++ vtable member usage. */
159 HOWTO (R_X86_64_GNU_VTENTRY
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
160 _bfd_elf_rel_vtable_reloc_fn
, "R_X86_64_GNU_VTENTRY", FALSE
, 0, 0,
164 /* Map BFD relocs to the x86_64 elf relocs. */
167 bfd_reloc_code_real_type bfd_reloc_val
;
168 unsigned char elf_reloc_val
;
171 static const struct elf_reloc_map x86_64_reloc_map
[] =
173 { BFD_RELOC_NONE
, R_X86_64_NONE
, },
174 { BFD_RELOC_64
, R_X86_64_64
, },
175 { BFD_RELOC_32_PCREL
, R_X86_64_PC32
, },
176 { BFD_RELOC_X86_64_GOT32
, R_X86_64_GOT32
,},
177 { BFD_RELOC_X86_64_PLT32
, R_X86_64_PLT32
,},
178 { BFD_RELOC_X86_64_COPY
, R_X86_64_COPY
, },
179 { BFD_RELOC_X86_64_GLOB_DAT
, R_X86_64_GLOB_DAT
, },
180 { BFD_RELOC_X86_64_JUMP_SLOT
, R_X86_64_JUMP_SLOT
, },
181 { BFD_RELOC_X86_64_RELATIVE
, R_X86_64_RELATIVE
, },
182 { BFD_RELOC_X86_64_GOTPCREL
, R_X86_64_GOTPCREL
, },
183 { BFD_RELOC_32
, R_X86_64_32
, },
184 { BFD_RELOC_X86_64_32S
, R_X86_64_32S
, },
185 { BFD_RELOC_16
, R_X86_64_16
, },
186 { BFD_RELOC_16_PCREL
, R_X86_64_PC16
, },
187 { BFD_RELOC_8
, R_X86_64_8
, },
188 { BFD_RELOC_8_PCREL
, R_X86_64_PC8
, },
189 { BFD_RELOC_X86_64_DTPMOD64
, R_X86_64_DTPMOD64
, },
190 { BFD_RELOC_X86_64_DTPOFF64
, R_X86_64_DTPOFF64
, },
191 { BFD_RELOC_X86_64_TPOFF64
, R_X86_64_TPOFF64
, },
192 { BFD_RELOC_X86_64_TLSGD
, R_X86_64_TLSGD
, },
193 { BFD_RELOC_X86_64_TLSLD
, R_X86_64_TLSLD
, },
194 { BFD_RELOC_X86_64_DTPOFF32
, R_X86_64_DTPOFF32
, },
195 { BFD_RELOC_X86_64_GOTTPOFF
, R_X86_64_GOTTPOFF
, },
196 { BFD_RELOC_X86_64_TPOFF32
, R_X86_64_TPOFF32
, },
197 { BFD_RELOC_64_PCREL
, R_X86_64_PC64
, },
198 { BFD_RELOC_X86_64_GOTOFF64
, R_X86_64_GOTOFF64
, },
199 { BFD_RELOC_X86_64_GOTPC32
, R_X86_64_GOTPC32
, },
200 { BFD_RELOC_X86_64_GOT64
, R_X86_64_GOT64
, },
201 { BFD_RELOC_X86_64_GOTPCREL64
,R_X86_64_GOTPCREL64
, },
202 { BFD_RELOC_X86_64_GOTPC64
, R_X86_64_GOTPC64
, },
203 { BFD_RELOC_X86_64_GOTPLT64
, R_X86_64_GOTPLT64
, },
204 { BFD_RELOC_X86_64_PLTOFF64
, R_X86_64_PLTOFF64
, },
205 { BFD_RELOC_X86_64_GOTPC32_TLSDESC
, R_X86_64_GOTPC32_TLSDESC
, },
206 { BFD_RELOC_X86_64_TLSDESC_CALL
, R_X86_64_TLSDESC_CALL
, },
207 { BFD_RELOC_X86_64_TLSDESC
, R_X86_64_TLSDESC
, },
208 { BFD_RELOC_VTABLE_INHERIT
, R_X86_64_GNU_VTINHERIT
, },
209 { BFD_RELOC_VTABLE_ENTRY
, R_X86_64_GNU_VTENTRY
, },
212 static reloc_howto_type
*
213 elf64_x86_64_rtype_to_howto (bfd
*abfd
, unsigned r_type
)
217 if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
218 || r_type
>= (unsigned int) R_X86_64_max
)
220 if (r_type
>= (unsigned int) R_X86_64_standard
)
222 (*_bfd_error_handler
) (_("%B: invalid relocation type %d"),
224 r_type
= R_X86_64_NONE
;
229 i
= r_type
- (unsigned int) R_X86_64_vt_offset
;
230 BFD_ASSERT (x86_64_elf_howto_table
[i
].type
== r_type
);
231 return &x86_64_elf_howto_table
[i
];
234 /* Given a BFD reloc type, return a HOWTO structure. */
235 static reloc_howto_type
*
236 elf64_x86_64_reloc_type_lookup (bfd
*abfd
,
237 bfd_reloc_code_real_type code
)
241 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
244 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
245 return elf64_x86_64_rtype_to_howto (abfd
,
246 x86_64_reloc_map
[i
].elf_reloc_val
);
251 static reloc_howto_type
*
252 elf64_x86_64_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
258 i
< (sizeof (x86_64_elf_howto_table
)
259 / sizeof (x86_64_elf_howto_table
[0]));
261 if (x86_64_elf_howto_table
[i
].name
!= NULL
262 && strcasecmp (x86_64_elf_howto_table
[i
].name
, r_name
) == 0)
263 return &x86_64_elf_howto_table
[i
];
268 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
271 elf64_x86_64_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
272 Elf_Internal_Rela
*dst
)
276 r_type
= ELF64_R_TYPE (dst
->r_info
);
277 cache_ptr
->howto
= elf64_x86_64_rtype_to_howto (abfd
, r_type
);
278 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
);
281 /* Support for core dump NOTE sections. */
283 elf64_x86_64_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
288 switch (note
->descsz
)
293 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
295 elf_tdata (abfd
)->core_signal
296 = bfd_get_16 (abfd
, note
->descdata
+ 12);
299 elf_tdata (abfd
)->core_pid
300 = bfd_get_32 (abfd
, note
->descdata
+ 32);
309 /* Make a ".reg/999" section. */
310 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
311 size
, note
->descpos
+ offset
);
315 elf64_x86_64_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
317 switch (note
->descsz
)
322 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
323 elf_tdata (abfd
)->core_program
324 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
325 elf_tdata (abfd
)->core_command
326 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
329 /* Note that for some reason, a spurious space is tacked
330 onto the end of the args in some (at least one anyway)
331 implementations, so strip it off if it exists. */
334 char *command
= elf_tdata (abfd
)->core_command
;
335 int n
= strlen (command
);
337 if (0 < n
&& command
[n
- 1] == ' ')
338 command
[n
- 1] = '\0';
344 /* Functions for the x86-64 ELF linker. */
346 /* The name of the dynamic interpreter. This is put in the .interp
349 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
351 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
352 copying dynamic variables from a shared lib into an app's dynbss
353 section, and instead use a dynamic relocation to point into the
355 #define ELIMINATE_COPY_RELOCS 1
357 /* The size in bytes of an entry in the global offset table. */
359 #define GOT_ENTRY_SIZE 8
361 /* The size in bytes of an entry in the procedure linkage table. */
363 #define PLT_ENTRY_SIZE 16
365 /* The first entry in a procedure linkage table looks like this. See the
366 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
368 static const bfd_byte elf64_x86_64_plt0_entry
[PLT_ENTRY_SIZE
] =
370 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
371 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
372 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
375 /* Subsequent entries in a procedure linkage table look like this. */
377 static const bfd_byte elf64_x86_64_plt_entry
[PLT_ENTRY_SIZE
] =
379 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
380 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
381 0x68, /* pushq immediate */
382 0, 0, 0, 0, /* replaced with index into relocation table. */
383 0xe9, /* jmp relative */
384 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
387 /* The x86-64 linker needs to keep track of the number of relocs that
388 it decides to copy as dynamic relocs in check_relocs for each symbol.
389 This is so that it can later discard them if they are found to be
390 unnecessary. We store the information in a field extending the
391 regular ELF linker hash table. */
393 struct elf64_x86_64_dyn_relocs
396 struct elf64_x86_64_dyn_relocs
*next
;
398 /* The input section of the reloc. */
401 /* Total number of relocs copied for the input section. */
404 /* Number of pc-relative relocs copied for the input section. */
405 bfd_size_type pc_count
;
408 /* x86-64 ELF linker hash entry. */
410 struct elf64_x86_64_link_hash_entry
412 struct elf_link_hash_entry elf
;
414 /* Track dynamic relocs copied for this symbol. */
415 struct elf64_x86_64_dyn_relocs
*dyn_relocs
;
417 #define GOT_UNKNOWN 0
421 #define GOT_TLS_GDESC 4
422 #define GOT_TLS_GD_BOTH_P(type) \
423 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
424 #define GOT_TLS_GD_P(type) \
425 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
426 #define GOT_TLS_GDESC_P(type) \
427 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
428 #define GOT_TLS_GD_ANY_P(type) \
429 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
430 unsigned char tls_type
;
432 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
433 starting at the end of the jump table. */
437 #define elf64_x86_64_hash_entry(ent) \
438 ((struct elf64_x86_64_link_hash_entry *)(ent))
440 struct elf64_x86_64_obj_tdata
442 struct elf_obj_tdata root
;
444 /* tls_type for each local got entry. */
445 char *local_got_tls_type
;
447 /* GOTPLT entries for TLS descriptors. */
448 bfd_vma
*local_tlsdesc_gotent
;
451 #define elf64_x86_64_tdata(abfd) \
452 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
454 #define elf64_x86_64_local_got_tls_type(abfd) \
455 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
457 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
458 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
460 #define is_x86_64_elf(bfd) \
461 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
462 && elf_tdata (bfd) != NULL \
463 && elf_object_id (bfd) == X86_64_ELF_TDATA)
466 elf64_x86_64_mkobject (bfd
*abfd
)
468 return bfd_elf_allocate_object (abfd
, sizeof (struct elf64_x86_64_obj_tdata
),
472 /* x86-64 ELF linker hash table. */
474 struct elf64_x86_64_link_hash_table
476 struct elf_link_hash_table elf
;
478 /* Short-cuts to get to dynamic linker sections. */
487 /* The offset into splt of the PLT entry for the TLS descriptor
488 resolver. Special values are 0, if not necessary (or not found
489 to be necessary yet), and -1 if needed but not determined
492 /* The offset into sgot of the GOT entry used by the PLT entry
497 bfd_signed_vma refcount
;
501 /* The amount of space used by the jump slots in the GOT. */
502 bfd_vma sgotplt_jump_table_size
;
504 /* Small local sym to section mapping cache. */
505 struct sym_sec_cache sym_sec
;
508 /* Get the x86-64 ELF linker hash table from a link_info structure. */
510 #define elf64_x86_64_hash_table(p) \
511 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
513 #define elf64_x86_64_compute_jump_table_size(htab) \
514 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
516 /* Create an entry in an x86-64 ELF linker hash table. */
518 static struct bfd_hash_entry
*
519 link_hash_newfunc (struct bfd_hash_entry
*entry
, struct bfd_hash_table
*table
,
522 /* Allocate the structure if it has not already been allocated by a
526 entry
= bfd_hash_allocate (table
,
527 sizeof (struct elf64_x86_64_link_hash_entry
));
532 /* Call the allocation method of the superclass. */
533 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
536 struct elf64_x86_64_link_hash_entry
*eh
;
538 eh
= (struct elf64_x86_64_link_hash_entry
*) entry
;
539 eh
->dyn_relocs
= NULL
;
540 eh
->tls_type
= GOT_UNKNOWN
;
541 eh
->tlsdesc_got
= (bfd_vma
) -1;
547 /* Create an X86-64 ELF linker hash table. */
549 static struct bfd_link_hash_table
*
550 elf64_x86_64_link_hash_table_create (bfd
*abfd
)
552 struct elf64_x86_64_link_hash_table
*ret
;
553 bfd_size_type amt
= sizeof (struct elf64_x86_64_link_hash_table
);
555 ret
= (struct elf64_x86_64_link_hash_table
*) bfd_malloc (amt
);
559 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
560 sizeof (struct elf64_x86_64_link_hash_entry
)))
573 ret
->sym_sec
.abfd
= NULL
;
574 ret
->tlsdesc_plt
= 0;
575 ret
->tlsdesc_got
= 0;
576 ret
->tls_ld_got
.refcount
= 0;
577 ret
->sgotplt_jump_table_size
= 0;
579 return &ret
->elf
.root
;
582 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
583 shortcuts to them in our hash table. */
586 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
588 struct elf64_x86_64_link_hash_table
*htab
;
590 if (! _bfd_elf_create_got_section (dynobj
, info
))
593 htab
= elf64_x86_64_hash_table (info
);
594 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
595 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
596 if (!htab
->sgot
|| !htab
->sgotplt
)
599 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rela.got",
600 (SEC_ALLOC
| SEC_LOAD
605 if (htab
->srelgot
== NULL
606 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 3))
611 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
612 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
616 elf64_x86_64_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
618 struct elf64_x86_64_link_hash_table
*htab
;
620 htab
= elf64_x86_64_hash_table (info
);
621 if (!htab
->sgot
&& !create_got_section (dynobj
, info
))
624 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
627 htab
->splt
= bfd_get_section_by_name (dynobj
, ".plt");
628 htab
->srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
629 htab
->sdynbss
= bfd_get_section_by_name (dynobj
, ".dynbss");
631 htab
->srelbss
= bfd_get_section_by_name (dynobj
, ".rela.bss");
633 if (!htab
->splt
|| !htab
->srelplt
|| !htab
->sdynbss
634 || (!info
->shared
&& !htab
->srelbss
))
640 /* Copy the extra info we tack onto an elf_link_hash_entry. */
643 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info
*info
,
644 struct elf_link_hash_entry
*dir
,
645 struct elf_link_hash_entry
*ind
)
647 struct elf64_x86_64_link_hash_entry
*edir
, *eind
;
649 edir
= (struct elf64_x86_64_link_hash_entry
*) dir
;
650 eind
= (struct elf64_x86_64_link_hash_entry
*) ind
;
652 if (eind
->dyn_relocs
!= NULL
)
654 if (edir
->dyn_relocs
!= NULL
)
656 struct elf64_x86_64_dyn_relocs
**pp
;
657 struct elf64_x86_64_dyn_relocs
*p
;
659 /* Add reloc counts against the indirect sym to the direct sym
660 list. Merge any entries against the same section. */
661 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
663 struct elf64_x86_64_dyn_relocs
*q
;
665 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
666 if (q
->sec
== p
->sec
)
668 q
->pc_count
+= p
->pc_count
;
669 q
->count
+= p
->count
;
676 *pp
= edir
->dyn_relocs
;
679 edir
->dyn_relocs
= eind
->dyn_relocs
;
680 eind
->dyn_relocs
= NULL
;
683 if (ind
->root
.type
== bfd_link_hash_indirect
684 && dir
->got
.refcount
<= 0)
686 edir
->tls_type
= eind
->tls_type
;
687 eind
->tls_type
= GOT_UNKNOWN
;
690 if (ELIMINATE_COPY_RELOCS
691 && ind
->root
.type
!= bfd_link_hash_indirect
692 && dir
->dynamic_adjusted
)
694 /* If called to transfer flags for a weakdef during processing
695 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
696 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
697 dir
->ref_dynamic
|= ind
->ref_dynamic
;
698 dir
->ref_regular
|= ind
->ref_regular
;
699 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
700 dir
->needs_plt
|= ind
->needs_plt
;
701 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
704 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
708 elf64_x86_64_elf_object_p (bfd
*abfd
)
710 /* Set the right machine number for an x86-64 elf64 file. */
711 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
729 /* Return TRUE if the TLS access code sequence support transition
733 elf64_x86_64_check_tls_transition (bfd
*abfd
, asection
*sec
,
735 Elf_Internal_Shdr
*symtab_hdr
,
736 struct elf_link_hash_entry
**sym_hashes
,
738 const Elf_Internal_Rela
*rel
,
739 const Elf_Internal_Rela
*relend
)
742 unsigned long r_symndx
;
743 struct elf_link_hash_entry
*h
;
746 /* Get the section contents. */
747 if (contents
== NULL
)
749 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
750 contents
= elf_section_data (sec
)->this_hdr
.contents
;
753 /* FIXME: How to better handle error condition? */
754 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
757 /* Cache the section contents for elf_link_input_bfd. */
758 elf_section_data (sec
)->this_hdr
.contents
= contents
;
762 offset
= rel
->r_offset
;
767 if ((rel
+ 1) >= relend
)
770 if (r_type
== R_X86_64_TLSGD
)
772 /* Check transition from GD access model. Only
773 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
774 .word 0x6666; rex64; call __tls_get_addr
775 can transit to different access model. */
777 static x86_64_opcode32 leaq
= { { 0x66, 0x48, 0x8d, 0x3d } },
778 call
= { { 0x66, 0x66, 0x48, 0xe8 } };
780 || (offset
+ 12) > sec
->size
781 || bfd_get_32 (abfd
, contents
+ offset
- 4) != leaq
.i
782 || bfd_get_32 (abfd
, contents
+ offset
+ 4) != call
.i
)
787 /* Check transition from LD access model. Only
788 leaq foo@tlsld(%rip), %rdi;
790 can transit to different access model. */
792 static x86_64_opcode32 ld
= { { 0x48, 0x8d, 0x3d, 0xe8 } };
795 if (offset
< 3 || (offset
+ 9) > sec
->size
)
798 op
.i
= bfd_get_32 (abfd
, contents
+ offset
- 3);
799 op
.c
[3] = bfd_get_8 (abfd
, contents
+ offset
+ 4);
804 r_symndx
= ELF64_R_SYM (rel
[1].r_info
);
805 if (r_symndx
< symtab_hdr
->sh_info
)
808 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
810 && h
->root
.root
.string
!= NULL
811 && (ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PC32
812 || ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
)
813 && (strcmp (h
->root
.root
.string
, "__tls_get_addr") == 0));
815 case R_X86_64_GOTTPOFF
:
816 /* Check transition from IE access model:
817 movq foo@gottpoff(%rip), %reg
818 addq foo@gottpoff(%rip), %reg
821 if (offset
< 3 || (offset
+ 4) > sec
->size
)
824 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
825 if (val
!= 0x48 && val
!= 0x4c)
828 val
= bfd_get_8 (abfd
, contents
+ offset
- 2);
829 if (val
!= 0x8b && val
!= 0x03)
832 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
833 return (val
& 0xc7) == 5;
835 case R_X86_64_GOTPC32_TLSDESC
:
836 /* Check transition from GDesc access model:
837 leaq x@tlsdesc(%rip), %rax
839 Make sure it's a leaq adding rip to a 32-bit offset
840 into any register, although it's probably almost always
843 if (offset
< 3 || (offset
+ 4) > sec
->size
)
846 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
847 if ((val
& 0xfb) != 0x48)
850 if (bfd_get_8 (abfd
, contents
+ offset
- 2) != 0x8d)
853 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
854 return (val
& 0xc7) == 0x05;
856 case R_X86_64_TLSDESC_CALL
:
857 /* Check transition from GDesc access model:
858 call *x@tlsdesc(%rax)
860 if (offset
+ 2 <= sec
->size
)
862 /* Make sure that it's a call *x@tlsdesc(%rax). */
863 static x86_64_opcode16 call
= { { 0xff, 0x10 } };
864 return bfd_get_16 (abfd
, contents
+ offset
) == call
.i
;
874 /* Return TRUE if the TLS access transition is OK or no transition
875 will be performed. Update R_TYPE if there is a transition. */
878 elf64_x86_64_tls_transition (struct bfd_link_info
*info
, bfd
*abfd
,
879 asection
*sec
, bfd_byte
*contents
,
880 Elf_Internal_Shdr
*symtab_hdr
,
881 struct elf_link_hash_entry
**sym_hashes
,
882 unsigned int *r_type
, int tls_type
,
883 const Elf_Internal_Rela
*rel
,
884 const Elf_Internal_Rela
*relend
,
885 struct elf_link_hash_entry
*h
)
887 unsigned int from_type
= *r_type
;
888 unsigned int to_type
= from_type
;
889 bfd_boolean check
= TRUE
;
894 case R_X86_64_GOTPC32_TLSDESC
:
895 case R_X86_64_TLSDESC_CALL
:
896 case R_X86_64_GOTTPOFF
:
900 to_type
= R_X86_64_TPOFF32
;
902 to_type
= R_X86_64_GOTTPOFF
;
905 /* When we are called from elf64_x86_64_relocate_section,
906 CONTENTS isn't NULL and there may be additional transitions
907 based on TLS_TYPE. */
908 if (contents
!= NULL
)
910 unsigned int new_to_type
= to_type
;
915 && tls_type
== GOT_TLS_IE
)
916 new_to_type
= R_X86_64_TPOFF32
;
918 if (to_type
== R_X86_64_TLSGD
919 || to_type
== R_X86_64_GOTPC32_TLSDESC
920 || to_type
== R_X86_64_TLSDESC_CALL
)
922 if (tls_type
== GOT_TLS_IE
)
923 new_to_type
= R_X86_64_GOTTPOFF
;
926 /* We checked the transition before when we were called from
927 elf64_x86_64_check_relocs. We only want to check the new
928 transition which hasn't been checked before. */
929 check
= new_to_type
!= to_type
&& from_type
== to_type
;
930 to_type
= new_to_type
;
937 to_type
= R_X86_64_TPOFF32
;
944 /* Return TRUE if there is no transition. */
945 if (from_type
== to_type
)
948 /* Check if the transition can be performed. */
950 && ! elf64_x86_64_check_tls_transition (abfd
, sec
, contents
,
951 symtab_hdr
, sym_hashes
,
952 from_type
, rel
, relend
))
954 reloc_howto_type
*from
, *to
;
956 from
= elf64_x86_64_rtype_to_howto (abfd
, from_type
);
957 to
= elf64_x86_64_rtype_to_howto (abfd
, to_type
);
959 (*_bfd_error_handler
)
960 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
961 "in section `%A' failed"),
962 abfd
, sec
, from
->name
, to
->name
,
963 h
? h
->root
.root
.string
: "a local symbol",
964 (unsigned long) rel
->r_offset
);
965 bfd_set_error (bfd_error_bad_value
);
973 /* Look through the relocs for a section during the first phase, and
974 calculate needed space in the global offset table, procedure
975 linkage table, and dynamic reloc sections. */
978 elf64_x86_64_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
980 const Elf_Internal_Rela
*relocs
)
982 struct elf64_x86_64_link_hash_table
*htab
;
983 Elf_Internal_Shdr
*symtab_hdr
;
984 struct elf_link_hash_entry
**sym_hashes
;
985 const Elf_Internal_Rela
*rel
;
986 const Elf_Internal_Rela
*rel_end
;
989 if (info
->relocatable
)
992 BFD_ASSERT (is_x86_64_elf (abfd
));
994 htab
= elf64_x86_64_hash_table (info
);
995 symtab_hdr
= &elf_symtab_hdr (abfd
);
996 sym_hashes
= elf_sym_hashes (abfd
);
1000 rel_end
= relocs
+ sec
->reloc_count
;
1001 for (rel
= relocs
; rel
< rel_end
; rel
++)
1003 unsigned int r_type
;
1004 unsigned long r_symndx
;
1005 struct elf_link_hash_entry
*h
;
1007 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1008 r_type
= ELF64_R_TYPE (rel
->r_info
);
1010 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
1012 (*_bfd_error_handler
) (_("%B: bad symbol index: %d"),
1017 if (r_symndx
< symtab_hdr
->sh_info
)
1021 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1022 while (h
->root
.type
== bfd_link_hash_indirect
1023 || h
->root
.type
== bfd_link_hash_warning
)
1024 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1027 if (! elf64_x86_64_tls_transition (info
, abfd
, sec
, NULL
,
1028 symtab_hdr
, sym_hashes
,
1029 &r_type
, GOT_UNKNOWN
,
1035 case R_X86_64_TLSLD
:
1036 htab
->tls_ld_got
.refcount
+= 1;
1039 case R_X86_64_TPOFF32
:
1042 (*_bfd_error_handler
)
1043 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1045 x86_64_elf_howto_table
[r_type
].name
,
1046 (h
) ? h
->root
.root
.string
: "a local symbol");
1047 bfd_set_error (bfd_error_bad_value
);
1052 case R_X86_64_GOTTPOFF
:
1054 info
->flags
|= DF_STATIC_TLS
;
1057 case R_X86_64_GOT32
:
1058 case R_X86_64_GOTPCREL
:
1059 case R_X86_64_TLSGD
:
1060 case R_X86_64_GOT64
:
1061 case R_X86_64_GOTPCREL64
:
1062 case R_X86_64_GOTPLT64
:
1063 case R_X86_64_GOTPC32_TLSDESC
:
1064 case R_X86_64_TLSDESC_CALL
:
1065 /* This symbol requires a global offset table entry. */
1067 int tls_type
, old_tls_type
;
1071 default: tls_type
= GOT_NORMAL
; break;
1072 case R_X86_64_TLSGD
: tls_type
= GOT_TLS_GD
; break;
1073 case R_X86_64_GOTTPOFF
: tls_type
= GOT_TLS_IE
; break;
1074 case R_X86_64_GOTPC32_TLSDESC
:
1075 case R_X86_64_TLSDESC_CALL
:
1076 tls_type
= GOT_TLS_GDESC
; break;
1081 if (r_type
== R_X86_64_GOTPLT64
)
1083 /* This relocation indicates that we also need
1084 a PLT entry, as this is a function. We don't need
1085 a PLT entry for local symbols. */
1087 h
->plt
.refcount
+= 1;
1089 h
->got
.refcount
+= 1;
1090 old_tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
1094 bfd_signed_vma
*local_got_refcounts
;
1096 /* This is a global offset table entry for a local symbol. */
1097 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1098 if (local_got_refcounts
== NULL
)
1102 size
= symtab_hdr
->sh_info
;
1103 size
*= sizeof (bfd_signed_vma
)
1104 + sizeof (bfd_vma
) + sizeof (char);
1105 local_got_refcounts
= ((bfd_signed_vma
*)
1106 bfd_zalloc (abfd
, size
));
1107 if (local_got_refcounts
== NULL
)
1109 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
1110 elf64_x86_64_local_tlsdesc_gotent (abfd
)
1111 = (bfd_vma
*) (local_got_refcounts
+ symtab_hdr
->sh_info
);
1112 elf64_x86_64_local_got_tls_type (abfd
)
1113 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
1115 local_got_refcounts
[r_symndx
] += 1;
1117 = elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
];
1120 /* If a TLS symbol is accessed using IE at least once,
1121 there is no point to use dynamic model for it. */
1122 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
1123 && (! GOT_TLS_GD_ANY_P (old_tls_type
)
1124 || tls_type
!= GOT_TLS_IE
))
1126 if (old_tls_type
== GOT_TLS_IE
&& GOT_TLS_GD_ANY_P (tls_type
))
1127 tls_type
= old_tls_type
;
1128 else if (GOT_TLS_GD_ANY_P (old_tls_type
)
1129 && GOT_TLS_GD_ANY_P (tls_type
))
1130 tls_type
|= old_tls_type
;
1133 (*_bfd_error_handler
)
1134 (_("%B: '%s' accessed both as normal and thread local symbol"),
1135 abfd
, h
? h
->root
.root
.string
: "<local>");
1140 if (old_tls_type
!= tls_type
)
1143 elf64_x86_64_hash_entry (h
)->tls_type
= tls_type
;
1145 elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
1150 case R_X86_64_GOTOFF64
:
1151 case R_X86_64_GOTPC32
:
1152 case R_X86_64_GOTPC64
:
1154 if (htab
->sgot
== NULL
)
1156 if (htab
->elf
.dynobj
== NULL
)
1157 htab
->elf
.dynobj
= abfd
;
1158 if (!create_got_section (htab
->elf
.dynobj
, info
))
1163 case R_X86_64_PLT32
:
1164 /* This symbol requires a procedure linkage table entry. We
1165 actually build the entry in adjust_dynamic_symbol,
1166 because this might be a case of linking PIC code which is
1167 never referenced by a dynamic object, in which case we
1168 don't need to generate a procedure linkage table entry
1171 /* If this is a local symbol, we resolve it directly without
1172 creating a procedure linkage table entry. */
1177 h
->plt
.refcount
+= 1;
1180 case R_X86_64_PLTOFF64
:
1181 /* This tries to form the 'address' of a function relative
1182 to GOT. For global symbols we need a PLT entry. */
1186 h
->plt
.refcount
+= 1;
1194 /* Let's help debug shared library creation. These relocs
1195 cannot be used in shared libs. Don't error out for
1196 sections we don't care about, such as debug sections or
1197 non-constant sections. */
1199 && (sec
->flags
& SEC_ALLOC
) != 0
1200 && (sec
->flags
& SEC_READONLY
) != 0)
1202 (*_bfd_error_handler
)
1203 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1205 x86_64_elf_howto_table
[r_type
].name
,
1206 (h
) ? h
->root
.root
.string
: "a local symbol");
1207 bfd_set_error (bfd_error_bad_value
);
1217 if (h
!= NULL
&& !info
->shared
)
1219 /* If this reloc is in a read-only section, we might
1220 need a copy reloc. We can't check reliably at this
1221 stage whether the section is read-only, as input
1222 sections have not yet been mapped to output sections.
1223 Tentatively set the flag for now, and correct in
1224 adjust_dynamic_symbol. */
1227 /* We may need a .plt entry if the function this reloc
1228 refers to is in a shared lib. */
1229 h
->plt
.refcount
+= 1;
1230 if (r_type
!= R_X86_64_PC32
&& r_type
!= R_X86_64_PC64
)
1231 h
->pointer_equality_needed
= 1;
1234 /* If we are creating a shared library, and this is a reloc
1235 against a global symbol, or a non PC relative reloc
1236 against a local symbol, then we need to copy the reloc
1237 into the shared library. However, if we are linking with
1238 -Bsymbolic, we do not need to copy a reloc against a
1239 global symbol which is defined in an object we are
1240 including in the link (i.e., DEF_REGULAR is set). At
1241 this point we have not seen all the input files, so it is
1242 possible that DEF_REGULAR is not set now but will be set
1243 later (it is never cleared). In case of a weak definition,
1244 DEF_REGULAR may be cleared later by a strong definition in
1245 a shared library. We account for that possibility below by
1246 storing information in the relocs_copied field of the hash
1247 table entry. A similar situation occurs when creating
1248 shared libraries and symbol visibility changes render the
1251 If on the other hand, we are creating an executable, we
1252 may need to keep relocations for symbols satisfied by a
1253 dynamic library if we manage to avoid copy relocs for the
1256 && (sec
->flags
& SEC_ALLOC
) != 0
1257 && (((r_type
!= R_X86_64_PC8
)
1258 && (r_type
!= R_X86_64_PC16
)
1259 && (r_type
!= R_X86_64_PC32
)
1260 && (r_type
!= R_X86_64_PC64
))
1262 && (! SYMBOLIC_BIND (info
, h
)
1263 || h
->root
.type
== bfd_link_hash_defweak
1264 || !h
->def_regular
))))
1265 || (ELIMINATE_COPY_RELOCS
1267 && (sec
->flags
& SEC_ALLOC
) != 0
1269 && (h
->root
.type
== bfd_link_hash_defweak
1270 || !h
->def_regular
)))
1272 struct elf64_x86_64_dyn_relocs
*p
;
1273 struct elf64_x86_64_dyn_relocs
**head
;
1275 /* We must copy these reloc types into the output file.
1276 Create a reloc section in dynobj and make room for
1283 name
= (bfd_elf_string_from_elf_section
1285 elf_elfheader (abfd
)->e_shstrndx
,
1286 elf_section_data (sec
)->rel_hdr
.sh_name
));
1290 if (! CONST_STRNEQ (name
, ".rela")
1291 || strcmp (bfd_get_section_name (abfd
, sec
),
1294 (*_bfd_error_handler
)
1295 (_("%B: bad relocation section name `%s\'"),
1299 if (htab
->elf
.dynobj
== NULL
)
1300 htab
->elf
.dynobj
= abfd
;
1302 dynobj
= htab
->elf
.dynobj
;
1304 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1309 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
1310 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1311 if ((sec
->flags
& SEC_ALLOC
) != 0)
1312 flags
|= SEC_ALLOC
| SEC_LOAD
;
1313 sreloc
= bfd_make_section_with_flags (dynobj
,
1317 || ! bfd_set_section_alignment (dynobj
, sreloc
, 3))
1320 elf_section_data (sec
)->sreloc
= sreloc
;
1323 /* If this is a global symbol, we count the number of
1324 relocations we need for this symbol. */
1327 head
= &((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
;
1332 /* Track dynamic relocs needed for local syms too.
1333 We really need local syms available to do this
1337 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
1342 /* Beware of type punned pointers vs strict aliasing
1344 vpp
= &(elf_section_data (s
)->local_dynrel
);
1345 head
= (struct elf64_x86_64_dyn_relocs
**)vpp
;
1349 if (p
== NULL
|| p
->sec
!= sec
)
1351 bfd_size_type amt
= sizeof *p
;
1352 p
= ((struct elf64_x86_64_dyn_relocs
*)
1353 bfd_alloc (htab
->elf
.dynobj
, amt
));
1364 if (r_type
== R_X86_64_PC8
1365 || r_type
== R_X86_64_PC16
1366 || r_type
== R_X86_64_PC32
1367 || r_type
== R_X86_64_PC64
)
1372 /* This relocation describes the C++ object vtable hierarchy.
1373 Reconstruct it for later use during GC. */
1374 case R_X86_64_GNU_VTINHERIT
:
1375 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1379 /* This relocation describes which C++ vtable entries are actually
1380 used. Record for later use during GC. */
1381 case R_X86_64_GNU_VTENTRY
:
1382 BFD_ASSERT (h
!= NULL
);
1384 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1396 /* Return the section that should be marked against GC for a given
1400 elf64_x86_64_gc_mark_hook (asection
*sec
,
1401 struct bfd_link_info
*info
,
1402 Elf_Internal_Rela
*rel
,
1403 struct elf_link_hash_entry
*h
,
1404 Elf_Internal_Sym
*sym
)
1407 switch (ELF64_R_TYPE (rel
->r_info
))
1409 case R_X86_64_GNU_VTINHERIT
:
1410 case R_X86_64_GNU_VTENTRY
:
1414 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
1417 /* Update the got entry reference counts for the section being removed. */
1420 elf64_x86_64_gc_sweep_hook (bfd
*abfd
, struct bfd_link_info
*info
,
1422 const Elf_Internal_Rela
*relocs
)
1424 Elf_Internal_Shdr
*symtab_hdr
;
1425 struct elf_link_hash_entry
**sym_hashes
;
1426 bfd_signed_vma
*local_got_refcounts
;
1427 const Elf_Internal_Rela
*rel
, *relend
;
1429 if (info
->relocatable
)
1432 elf_section_data (sec
)->local_dynrel
= NULL
;
1434 symtab_hdr
= &elf_symtab_hdr (abfd
);
1435 sym_hashes
= elf_sym_hashes (abfd
);
1436 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1438 relend
= relocs
+ sec
->reloc_count
;
1439 for (rel
= relocs
; rel
< relend
; rel
++)
1441 unsigned long r_symndx
;
1442 unsigned int r_type
;
1443 struct elf_link_hash_entry
*h
= NULL
;
1445 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1446 if (r_symndx
>= symtab_hdr
->sh_info
)
1448 struct elf64_x86_64_link_hash_entry
*eh
;
1449 struct elf64_x86_64_dyn_relocs
**pp
;
1450 struct elf64_x86_64_dyn_relocs
*p
;
1452 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1453 while (h
->root
.type
== bfd_link_hash_indirect
1454 || h
->root
.type
== bfd_link_hash_warning
)
1455 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1456 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1458 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
1461 /* Everything must go for SEC. */
1467 r_type
= ELF64_R_TYPE (rel
->r_info
);
1468 if (! elf64_x86_64_tls_transition (info
, abfd
, sec
, NULL
,
1469 symtab_hdr
, sym_hashes
,
1470 &r_type
, GOT_UNKNOWN
,
1476 case R_X86_64_TLSLD
:
1477 if (elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
> 0)
1478 elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
-= 1;
1481 case R_X86_64_TLSGD
:
1482 case R_X86_64_GOTPC32_TLSDESC
:
1483 case R_X86_64_TLSDESC_CALL
:
1484 case R_X86_64_GOTTPOFF
:
1485 case R_X86_64_GOT32
:
1486 case R_X86_64_GOTPCREL
:
1487 case R_X86_64_GOT64
:
1488 case R_X86_64_GOTPCREL64
:
1489 case R_X86_64_GOTPLT64
:
1492 if (r_type
== R_X86_64_GOTPLT64
&& h
->plt
.refcount
> 0)
1493 h
->plt
.refcount
-= 1;
1494 if (h
->got
.refcount
> 0)
1495 h
->got
.refcount
-= 1;
1497 else if (local_got_refcounts
!= NULL
)
1499 if (local_got_refcounts
[r_symndx
] > 0)
1500 local_got_refcounts
[r_symndx
] -= 1;
1517 case R_X86_64_PLT32
:
1518 case R_X86_64_PLTOFF64
:
1521 if (h
->plt
.refcount
> 0)
1522 h
->plt
.refcount
-= 1;
1534 /* Adjust a symbol defined by a dynamic object and referenced by a
1535 regular object. The current definition is in some section of the
1536 dynamic object, but we're not including those sections. We have to
1537 change the definition to something the rest of the link can
1541 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1542 struct elf_link_hash_entry
*h
)
1544 struct elf64_x86_64_link_hash_table
*htab
;
1547 /* If this is a function, put it in the procedure linkage table. We
1548 will fill in the contents of the procedure linkage table later,
1549 when we know the address of the .got section. */
1550 if (h
->type
== STT_FUNC
1553 if (h
->plt
.refcount
<= 0
1554 || SYMBOL_CALLS_LOCAL (info
, h
)
1555 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1556 && h
->root
.type
== bfd_link_hash_undefweak
))
1558 /* This case can occur if we saw a PLT32 reloc in an input
1559 file, but the symbol was never referred to by a dynamic
1560 object, or if all references were garbage collected. In
1561 such a case, we don't actually need to build a procedure
1562 linkage table, and we can just do a PC32 reloc instead. */
1563 h
->plt
.offset
= (bfd_vma
) -1;
1570 /* It's possible that we incorrectly decided a .plt reloc was
1571 needed for an R_X86_64_PC32 reloc to a non-function sym in
1572 check_relocs. We can't decide accurately between function and
1573 non-function syms in check-relocs; Objects loaded later in
1574 the link may change h->type. So fix it now. */
1575 h
->plt
.offset
= (bfd_vma
) -1;
1577 /* If this is a weak symbol, and there is a real definition, the
1578 processor independent code will have arranged for us to see the
1579 real definition first, and we can just use the same value. */
1580 if (h
->u
.weakdef
!= NULL
)
1582 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
1583 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
1584 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
1585 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
1586 if (ELIMINATE_COPY_RELOCS
|| info
->nocopyreloc
)
1587 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
1591 /* This is a reference to a symbol defined by a dynamic object which
1592 is not a function. */
1594 /* If we are creating a shared library, we must presume that the
1595 only references to the symbol are via the global offset table.
1596 For such cases we need not do anything here; the relocations will
1597 be handled correctly by relocate_section. */
1601 /* If there are no references to this symbol that do not use the
1602 GOT, we don't need to generate a copy reloc. */
1603 if (!h
->non_got_ref
)
1606 /* If -z nocopyreloc was given, we won't generate them either. */
1607 if (info
->nocopyreloc
)
1613 if (ELIMINATE_COPY_RELOCS
)
1615 struct elf64_x86_64_link_hash_entry
* eh
;
1616 struct elf64_x86_64_dyn_relocs
*p
;
1618 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1619 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1621 s
= p
->sec
->output_section
;
1622 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1626 /* If we didn't find any dynamic relocs in read-only sections, then
1627 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1637 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
1638 h
->root
.root
.string
);
1642 /* We must allocate the symbol in our .dynbss section, which will
1643 become part of the .bss section of the executable. There will be
1644 an entry for this symbol in the .dynsym section. The dynamic
1645 object will contain position independent code, so all references
1646 from the dynamic object to this symbol will go through the global
1647 offset table. The dynamic linker will use the .dynsym entry to
1648 determine the address it must put in the global offset table, so
1649 both the dynamic object and the regular object will refer to the
1650 same memory location for the variable. */
1652 htab
= elf64_x86_64_hash_table (info
);
1654 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1655 to copy the initial value out of the dynamic object and into the
1656 runtime process image. */
1657 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1659 htab
->srelbss
->size
+= sizeof (Elf64_External_Rela
);
1665 return _bfd_elf_adjust_dynamic_copy (h
, s
);
1668 /* Allocate space in .plt, .got and associated reloc sections for
1672 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1674 struct bfd_link_info
*info
;
1675 struct elf64_x86_64_link_hash_table
*htab
;
1676 struct elf64_x86_64_link_hash_entry
*eh
;
1677 struct elf64_x86_64_dyn_relocs
*p
;
1679 if (h
->root
.type
== bfd_link_hash_indirect
)
1682 if (h
->root
.type
== bfd_link_hash_warning
)
1683 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1685 info
= (struct bfd_link_info
*) inf
;
1686 htab
= elf64_x86_64_hash_table (info
);
1688 if (htab
->elf
.dynamic_sections_created
1689 && h
->plt
.refcount
> 0)
1691 /* Make sure this symbol is output as a dynamic symbol.
1692 Undefined weak syms won't yet be marked as dynamic. */
1693 if (h
->dynindx
== -1
1694 && !h
->forced_local
)
1696 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1701 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
1703 asection
*s
= htab
->splt
;
1705 /* If this is the first .plt entry, make room for the special
1708 s
->size
+= PLT_ENTRY_SIZE
;
1710 h
->plt
.offset
= s
->size
;
1712 /* If this symbol is not defined in a regular file, and we are
1713 not generating a shared library, then set the symbol to this
1714 location in the .plt. This is required to make function
1715 pointers compare as equal between the normal executable and
1716 the shared library. */
1720 h
->root
.u
.def
.section
= s
;
1721 h
->root
.u
.def
.value
= h
->plt
.offset
;
1724 /* Make room for this entry. */
1725 s
->size
+= PLT_ENTRY_SIZE
;
1727 /* We also need to make an entry in the .got.plt section, which
1728 will be placed in the .got section by the linker script. */
1729 htab
->sgotplt
->size
+= GOT_ENTRY_SIZE
;
1731 /* We also need to make an entry in the .rela.plt section. */
1732 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1733 htab
->srelplt
->reloc_count
++;
1737 h
->plt
.offset
= (bfd_vma
) -1;
1743 h
->plt
.offset
= (bfd_vma
) -1;
1747 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1748 eh
->tlsdesc_got
= (bfd_vma
) -1;
1750 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1751 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1752 if (h
->got
.refcount
> 0
1755 && elf64_x86_64_hash_entry (h
)->tls_type
== GOT_TLS_IE
)
1756 h
->got
.offset
= (bfd_vma
) -1;
1757 else if (h
->got
.refcount
> 0)
1761 int tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
1763 /* Make sure this symbol is output as a dynamic symbol.
1764 Undefined weak syms won't yet be marked as dynamic. */
1765 if (h
->dynindx
== -1
1766 && !h
->forced_local
)
1768 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1772 if (GOT_TLS_GDESC_P (tls_type
))
1774 eh
->tlsdesc_got
= htab
->sgotplt
->size
1775 - elf64_x86_64_compute_jump_table_size (htab
);
1776 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
1777 h
->got
.offset
= (bfd_vma
) -2;
1779 if (! GOT_TLS_GDESC_P (tls_type
)
1780 || GOT_TLS_GD_P (tls_type
))
1783 h
->got
.offset
= s
->size
;
1784 s
->size
+= GOT_ENTRY_SIZE
;
1785 if (GOT_TLS_GD_P (tls_type
))
1786 s
->size
+= GOT_ENTRY_SIZE
;
1788 dyn
= htab
->elf
.dynamic_sections_created
;
1789 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1791 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1792 if ((GOT_TLS_GD_P (tls_type
) && h
->dynindx
== -1)
1793 || tls_type
== GOT_TLS_IE
)
1794 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1795 else if (GOT_TLS_GD_P (tls_type
))
1796 htab
->srelgot
->size
+= 2 * sizeof (Elf64_External_Rela
);
1797 else if (! GOT_TLS_GDESC_P (tls_type
)
1798 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
1799 || h
->root
.type
!= bfd_link_hash_undefweak
)
1801 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
1802 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1803 if (GOT_TLS_GDESC_P (tls_type
))
1805 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1806 htab
->tlsdesc_plt
= (bfd_vma
) -1;
1810 h
->got
.offset
= (bfd_vma
) -1;
1812 if (eh
->dyn_relocs
== NULL
)
1815 /* In the shared -Bsymbolic case, discard space allocated for
1816 dynamic pc-relative relocs against symbols which turn out to be
1817 defined in regular objects. For the normal shared case, discard
1818 space for pc-relative relocs that have become local due to symbol
1819 visibility changes. */
1823 /* Relocs that use pc_count are those that appear on a call
1824 insn, or certain REL relocs that can generated via assembly.
1825 We want calls to protected symbols to resolve directly to the
1826 function rather than going via the plt. If people want
1827 function pointer comparisons to work as expected then they
1828 should avoid writing weird assembly. */
1829 if (SYMBOL_CALLS_LOCAL (info
, h
))
1831 struct elf64_x86_64_dyn_relocs
**pp
;
1833 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1835 p
->count
-= p
->pc_count
;
1844 /* Also discard relocs on undefined weak syms with non-default
1846 if (eh
->dyn_relocs
!= NULL
1847 && h
->root
.type
== bfd_link_hash_undefweak
)
1849 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
1850 eh
->dyn_relocs
= NULL
;
1852 /* Make sure undefined weak symbols are output as a dynamic
1854 else if (h
->dynindx
== -1
1855 && !h
->forced_local
)
1857 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1862 else if (ELIMINATE_COPY_RELOCS
)
1864 /* For the non-shared case, discard space for relocs against
1865 symbols which turn out to need copy relocs or are not
1871 || (htab
->elf
.dynamic_sections_created
1872 && (h
->root
.type
== bfd_link_hash_undefweak
1873 || h
->root
.type
== bfd_link_hash_undefined
))))
1875 /* Make sure this symbol is output as a dynamic symbol.
1876 Undefined weak syms won't yet be marked as dynamic. */
1877 if (h
->dynindx
== -1
1878 && !h
->forced_local
)
1880 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1884 /* If that succeeded, we know we'll be keeping all the
1886 if (h
->dynindx
!= -1)
1890 eh
->dyn_relocs
= NULL
;
1895 /* Finally, allocate space. */
1896 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1898 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1899 sreloc
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
1905 /* Find any dynamic relocs that apply to read-only sections. */
1908 readonly_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1910 struct elf64_x86_64_link_hash_entry
*eh
;
1911 struct elf64_x86_64_dyn_relocs
*p
;
1913 if (h
->root
.type
== bfd_link_hash_warning
)
1914 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1916 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1917 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1919 asection
*s
= p
->sec
->output_section
;
1921 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1923 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
1925 info
->flags
|= DF_TEXTREL
;
1927 /* Not an error, just cut short the traversal. */
1934 /* Set the sizes of the dynamic sections. */
1937 elf64_x86_64_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1938 struct bfd_link_info
*info
)
1940 struct elf64_x86_64_link_hash_table
*htab
;
1946 htab
= elf64_x86_64_hash_table (info
);
1947 dynobj
= htab
->elf
.dynobj
;
1951 if (htab
->elf
.dynamic_sections_created
)
1953 /* Set the contents of the .interp section to the interpreter. */
1954 if (info
->executable
)
1956 s
= bfd_get_section_by_name (dynobj
, ".interp");
1959 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1960 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1964 /* Set up .got offsets for local syms, and space for local dynamic
1966 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
1968 bfd_signed_vma
*local_got
;
1969 bfd_signed_vma
*end_local_got
;
1970 char *local_tls_type
;
1971 bfd_vma
*local_tlsdesc_gotent
;
1972 bfd_size_type locsymcount
;
1973 Elf_Internal_Shdr
*symtab_hdr
;
1976 if (! is_x86_64_elf (ibfd
))
1979 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1981 struct elf64_x86_64_dyn_relocs
*p
;
1983 for (p
= (struct elf64_x86_64_dyn_relocs
*)
1984 (elf_section_data (s
)->local_dynrel
);
1988 if (!bfd_is_abs_section (p
->sec
)
1989 && bfd_is_abs_section (p
->sec
->output_section
))
1991 /* Input section has been discarded, either because
1992 it is a copy of a linkonce section or due to
1993 linker script /DISCARD/, so we'll be discarding
1996 else if (p
->count
!= 0)
1998 srel
= elf_section_data (p
->sec
)->sreloc
;
1999 srel
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
2000 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
2001 info
->flags
|= DF_TEXTREL
;
2007 local_got
= elf_local_got_refcounts (ibfd
);
2011 symtab_hdr
= &elf_symtab_hdr (ibfd
);
2012 locsymcount
= symtab_hdr
->sh_info
;
2013 end_local_got
= local_got
+ locsymcount
;
2014 local_tls_type
= elf64_x86_64_local_got_tls_type (ibfd
);
2015 local_tlsdesc_gotent
= elf64_x86_64_local_tlsdesc_gotent (ibfd
);
2017 srel
= htab
->srelgot
;
2018 for (; local_got
< end_local_got
;
2019 ++local_got
, ++local_tls_type
, ++local_tlsdesc_gotent
)
2021 *local_tlsdesc_gotent
= (bfd_vma
) -1;
2024 if (GOT_TLS_GDESC_P (*local_tls_type
))
2026 *local_tlsdesc_gotent
= htab
->sgotplt
->size
2027 - elf64_x86_64_compute_jump_table_size (htab
);
2028 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
2029 *local_got
= (bfd_vma
) -2;
2031 if (! GOT_TLS_GDESC_P (*local_tls_type
)
2032 || GOT_TLS_GD_P (*local_tls_type
))
2034 *local_got
= s
->size
;
2035 s
->size
+= GOT_ENTRY_SIZE
;
2036 if (GOT_TLS_GD_P (*local_tls_type
))
2037 s
->size
+= GOT_ENTRY_SIZE
;
2040 || GOT_TLS_GD_ANY_P (*local_tls_type
)
2041 || *local_tls_type
== GOT_TLS_IE
)
2043 if (GOT_TLS_GDESC_P (*local_tls_type
))
2045 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
2046 htab
->tlsdesc_plt
= (bfd_vma
) -1;
2048 if (! GOT_TLS_GDESC_P (*local_tls_type
)
2049 || GOT_TLS_GD_P (*local_tls_type
))
2050 srel
->size
+= sizeof (Elf64_External_Rela
);
2054 *local_got
= (bfd_vma
) -1;
2058 if (htab
->tls_ld_got
.refcount
> 0)
2060 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2062 htab
->tls_ld_got
.offset
= htab
->sgot
->size
;
2063 htab
->sgot
->size
+= 2 * GOT_ENTRY_SIZE
;
2064 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
2067 htab
->tls_ld_got
.offset
= -1;
2069 /* Allocate global sym .plt and .got entries, and space for global
2070 sym dynamic relocs. */
2071 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, (PTR
) info
);
2073 /* For every jump slot reserved in the sgotplt, reloc_count is
2074 incremented. However, when we reserve space for TLS descriptors,
2075 it's not incremented, so in order to compute the space reserved
2076 for them, it suffices to multiply the reloc count by the jump
2079 htab
->sgotplt_jump_table_size
2080 = elf64_x86_64_compute_jump_table_size (htab
);
2082 if (htab
->tlsdesc_plt
)
2084 /* If we're not using lazy TLS relocations, don't generate the
2085 PLT and GOT entries they require. */
2086 if ((info
->flags
& DF_BIND_NOW
))
2087 htab
->tlsdesc_plt
= 0;
2090 htab
->tlsdesc_got
= htab
->sgot
->size
;
2091 htab
->sgot
->size
+= GOT_ENTRY_SIZE
;
2092 /* Reserve room for the initial entry.
2093 FIXME: we could probably do away with it in this case. */
2094 if (htab
->splt
->size
== 0)
2095 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
2096 htab
->tlsdesc_plt
= htab
->splt
->size
;
2097 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
2101 /* We now have determined the sizes of the various dynamic sections.
2102 Allocate memory for them. */
2104 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2106 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2111 || s
== htab
->sgotplt
2112 || s
== htab
->sdynbss
)
2114 /* Strip this section if we don't need it; see the
2117 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
2119 if (s
->size
!= 0 && s
!= htab
->srelplt
)
2122 /* We use the reloc_count field as a counter if we need
2123 to copy relocs into the output file. */
2124 if (s
!= htab
->srelplt
)
2129 /* It's not one of our sections, so don't allocate space. */
2135 /* If we don't need this section, strip it from the
2136 output file. This is mostly to handle .rela.bss and
2137 .rela.plt. We must create both sections in
2138 create_dynamic_sections, because they must be created
2139 before the linker maps input sections to output
2140 sections. The linker does that before
2141 adjust_dynamic_symbol is called, and it is that
2142 function which decides whether anything needs to go
2143 into these sections. */
2145 s
->flags
|= SEC_EXCLUDE
;
2149 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
2152 /* Allocate memory for the section contents. We use bfd_zalloc
2153 here in case unused entries are not reclaimed before the
2154 section's contents are written out. This should not happen,
2155 but this way if it does, we get a R_X86_64_NONE reloc instead
2157 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
2158 if (s
->contents
== NULL
)
2162 if (htab
->elf
.dynamic_sections_created
)
2164 /* Add some entries to the .dynamic section. We fill in the
2165 values later, in elf64_x86_64_finish_dynamic_sections, but we
2166 must add the entries now so that we get the correct size for
2167 the .dynamic section. The DT_DEBUG entry is filled in by the
2168 dynamic linker and used by the debugger. */
2169 #define add_dynamic_entry(TAG, VAL) \
2170 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2172 if (info
->executable
)
2174 if (!add_dynamic_entry (DT_DEBUG
, 0))
2178 if (htab
->splt
->size
!= 0)
2180 if (!add_dynamic_entry (DT_PLTGOT
, 0)
2181 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
2182 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
2183 || !add_dynamic_entry (DT_JMPREL
, 0))
2186 if (htab
->tlsdesc_plt
2187 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
2188 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
2194 if (!add_dynamic_entry (DT_RELA
, 0)
2195 || !add_dynamic_entry (DT_RELASZ
, 0)
2196 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf64_External_Rela
)))
2199 /* If any dynamic relocs apply to a read-only section,
2200 then we need a DT_TEXTREL entry. */
2201 if ((info
->flags
& DF_TEXTREL
) == 0)
2202 elf_link_hash_traverse (&htab
->elf
, readonly_dynrelocs
,
2205 if ((info
->flags
& DF_TEXTREL
) != 0)
2207 if (!add_dynamic_entry (DT_TEXTREL
, 0))
2212 #undef add_dynamic_entry
2218 elf64_x86_64_always_size_sections (bfd
*output_bfd
,
2219 struct bfd_link_info
*info
)
2221 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
2225 struct elf_link_hash_entry
*tlsbase
;
2227 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
2228 "_TLS_MODULE_BASE_",
2229 FALSE
, FALSE
, FALSE
);
2231 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
2233 struct bfd_link_hash_entry
*bh
= NULL
;
2234 const struct elf_backend_data
*bed
2235 = get_elf_backend_data (output_bfd
);
2237 if (!(_bfd_generic_link_add_one_symbol
2238 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
2239 tls_sec
, 0, NULL
, FALSE
,
2240 bed
->collect
, &bh
)))
2242 tlsbase
= (struct elf_link_hash_entry
*)bh
;
2243 tlsbase
->def_regular
= 1;
2244 tlsbase
->other
= STV_HIDDEN
;
2245 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
2252 /* Return the base VMA address which should be subtracted from real addresses
2253 when resolving @dtpoff relocation.
2254 This is PT_TLS segment p_vaddr. */
2257 dtpoff_base (struct bfd_link_info
*info
)
2259 /* If tls_sec is NULL, we should have signalled an error already. */
2260 if (elf_hash_table (info
)->tls_sec
== NULL
)
2262 return elf_hash_table (info
)->tls_sec
->vma
;
2265 /* Return the relocation value for @tpoff relocation
2266 if STT_TLS virtual address is ADDRESS. */
2269 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2271 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2273 /* If tls_segment is NULL, we should have signalled an error already. */
2274 if (htab
->tls_sec
== NULL
)
2276 return address
- htab
->tls_size
- htab
->tls_sec
->vma
;
2279 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2283 is_32bit_relative_branch (bfd_byte
*contents
, bfd_vma offset
)
2285 /* Opcode Instruction
2288 0x0f 0x8x conditional jump */
2290 && (contents
[offset
- 1] == 0xe8
2291 || contents
[offset
- 1] == 0xe9))
2293 && contents
[offset
- 2] == 0x0f
2294 && (contents
[offset
- 1] & 0xf0) == 0x80));
2297 /* Relocate an x86_64 ELF section. */
2300 elf64_x86_64_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
2301 bfd
*input_bfd
, asection
*input_section
,
2302 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
2303 Elf_Internal_Sym
*local_syms
,
2304 asection
**local_sections
)
2306 struct elf64_x86_64_link_hash_table
*htab
;
2307 Elf_Internal_Shdr
*symtab_hdr
;
2308 struct elf_link_hash_entry
**sym_hashes
;
2309 bfd_vma
*local_got_offsets
;
2310 bfd_vma
*local_tlsdesc_gotents
;
2311 Elf_Internal_Rela
*rel
;
2312 Elf_Internal_Rela
*relend
;
2314 BFD_ASSERT (is_x86_64_elf (input_bfd
));
2316 htab
= elf64_x86_64_hash_table (info
);
2317 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2318 sym_hashes
= elf_sym_hashes (input_bfd
);
2319 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2320 local_tlsdesc_gotents
= elf64_x86_64_local_tlsdesc_gotent (input_bfd
);
2323 relend
= relocs
+ input_section
->reloc_count
;
2324 for (; rel
< relend
; rel
++)
2326 unsigned int r_type
;
2327 reloc_howto_type
*howto
;
2328 unsigned long r_symndx
;
2329 struct elf_link_hash_entry
*h
;
2330 Elf_Internal_Sym
*sym
;
2332 bfd_vma off
, offplt
;
2334 bfd_boolean unresolved_reloc
;
2335 bfd_reloc_status_type r
;
2338 r_type
= ELF64_R_TYPE (rel
->r_info
);
2339 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
2340 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
2343 if (r_type
>= R_X86_64_max
)
2345 bfd_set_error (bfd_error_bad_value
);
2349 howto
= x86_64_elf_howto_table
+ r_type
;
2350 r_symndx
= ELF64_R_SYM (rel
->r_info
);
2354 unresolved_reloc
= FALSE
;
2355 if (r_symndx
< symtab_hdr
->sh_info
)
2357 sym
= local_syms
+ r_symndx
;
2358 sec
= local_sections
[r_symndx
];
2360 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2366 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2367 r_symndx
, symtab_hdr
, sym_hashes
,
2369 unresolved_reloc
, warned
);
2372 if (sec
!= NULL
&& elf_discarded_section (sec
))
2374 /* For relocs against symbols from removed linkonce sections,
2375 or sections discarded by a linker script, we just want the
2376 section contents zeroed. Avoid any special processing. */
2377 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
2383 if (info
->relocatable
)
2386 /* When generating a shared object, the relocations handled here are
2387 copied into the output file to be resolved at run time. */
2391 case R_X86_64_GOT32
:
2392 case R_X86_64_GOT64
:
2393 /* Relocation is to the entry for this symbol in the global
2395 case R_X86_64_GOTPCREL
:
2396 case R_X86_64_GOTPCREL64
:
2397 /* Use global offset table entry as symbol value. */
2398 case R_X86_64_GOTPLT64
:
2399 /* This is the same as GOT64 for relocation purposes, but
2400 indicates the existence of a PLT entry. The difficulty is,
2401 that we must calculate the GOT slot offset from the PLT
2402 offset, if this symbol got a PLT entry (it was global).
2403 Additionally if it's computed from the PLT entry, then that
2404 GOT offset is relative to .got.plt, not to .got. */
2405 base_got
= htab
->sgot
;
2407 if (htab
->sgot
== NULL
)
2414 off
= h
->got
.offset
;
2416 && h
->plt
.offset
!= (bfd_vma
)-1
2417 && off
== (bfd_vma
)-1)
2419 /* We can't use h->got.offset here to save
2420 state, or even just remember the offset, as
2421 finish_dynamic_symbol would use that as offset into
2423 bfd_vma plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
2424 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2425 base_got
= htab
->sgotplt
;
2428 dyn
= htab
->elf
.dynamic_sections_created
;
2430 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
2432 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2433 || (ELF_ST_VISIBILITY (h
->other
)
2434 && h
->root
.type
== bfd_link_hash_undefweak
))
2436 /* This is actually a static link, or it is a -Bsymbolic
2437 link and the symbol is defined locally, or the symbol
2438 was forced to be local because of a version file. We
2439 must initialize this entry in the global offset table.
2440 Since the offset must always be a multiple of 8, we
2441 use the least significant bit to record whether we
2442 have initialized it already.
2444 When doing a dynamic link, we create a .rela.got
2445 relocation entry to initialize the value. This is
2446 done in the finish_dynamic_symbol routine. */
2451 bfd_put_64 (output_bfd
, relocation
,
2452 base_got
->contents
+ off
);
2453 /* Note that this is harmless for the GOTPLT64 case,
2454 as -1 | 1 still is -1. */
2459 unresolved_reloc
= FALSE
;
2463 if (local_got_offsets
== NULL
)
2466 off
= local_got_offsets
[r_symndx
];
2468 /* The offset must always be a multiple of 8. We use
2469 the least significant bit to record whether we have
2470 already generated the necessary reloc. */
2475 bfd_put_64 (output_bfd
, relocation
,
2476 base_got
->contents
+ off
);
2481 Elf_Internal_Rela outrel
;
2484 /* We need to generate a R_X86_64_RELATIVE reloc
2485 for the dynamic linker. */
2490 outrel
.r_offset
= (base_got
->output_section
->vma
2491 + base_got
->output_offset
2493 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2494 outrel
.r_addend
= relocation
;
2496 loc
+= s
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2497 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2500 local_got_offsets
[r_symndx
] |= 1;
2504 if (off
>= (bfd_vma
) -2)
2507 relocation
= base_got
->output_section
->vma
2508 + base_got
->output_offset
+ off
;
2509 if (r_type
!= R_X86_64_GOTPCREL
&& r_type
!= R_X86_64_GOTPCREL64
)
2510 relocation
-= htab
->sgotplt
->output_section
->vma
2511 - htab
->sgotplt
->output_offset
;
2515 case R_X86_64_GOTOFF64
:
2516 /* Relocation is relative to the start of the global offset
2519 /* Check to make sure it isn't a protected function symbol
2520 for shared library since it may not be local when used
2521 as function address. */
2525 && h
->type
== STT_FUNC
2526 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
2528 (*_bfd_error_handler
)
2529 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2530 input_bfd
, h
->root
.root
.string
);
2531 bfd_set_error (bfd_error_bad_value
);
2535 /* Note that sgot is not involved in this
2536 calculation. We always want the start of .got.plt. If we
2537 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2538 permitted by the ABI, we might have to change this
2540 relocation
-= htab
->sgotplt
->output_section
->vma
2541 + htab
->sgotplt
->output_offset
;
2544 case R_X86_64_GOTPC32
:
2545 case R_X86_64_GOTPC64
:
2546 /* Use global offset table as symbol value. */
2547 relocation
= htab
->sgotplt
->output_section
->vma
2548 + htab
->sgotplt
->output_offset
;
2549 unresolved_reloc
= FALSE
;
2552 case R_X86_64_PLTOFF64
:
2553 /* Relocation is PLT entry relative to GOT. For local
2554 symbols it's the symbol itself relative to GOT. */
2556 /* See PLT32 handling. */
2557 && h
->plt
.offset
!= (bfd_vma
) -1
2558 && htab
->splt
!= NULL
)
2560 relocation
= (htab
->splt
->output_section
->vma
2561 + htab
->splt
->output_offset
2563 unresolved_reloc
= FALSE
;
2566 relocation
-= htab
->sgotplt
->output_section
->vma
2567 + htab
->sgotplt
->output_offset
;
2570 case R_X86_64_PLT32
:
2571 /* Relocation is to the entry for this symbol in the
2572 procedure linkage table. */
2574 /* Resolve a PLT32 reloc against a local symbol directly,
2575 without using the procedure linkage table. */
2579 if (h
->plt
.offset
== (bfd_vma
) -1
2580 || htab
->splt
== NULL
)
2582 /* We didn't make a PLT entry for this symbol. This
2583 happens when statically linking PIC code, or when
2584 using -Bsymbolic. */
2588 relocation
= (htab
->splt
->output_section
->vma
2589 + htab
->splt
->output_offset
2591 unresolved_reloc
= FALSE
;
2598 && (input_section
->flags
& SEC_ALLOC
) != 0
2599 && (input_section
->flags
& SEC_READONLY
) != 0
2602 bfd_boolean fail
= FALSE
;
2604 = (r_type
== R_X86_64_PC32
2605 && is_32bit_relative_branch (contents
, rel
->r_offset
));
2607 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
2609 /* Symbol is referenced locally. Make sure it is
2610 defined locally or for a branch. */
2611 fail
= !h
->def_regular
&& !branch
;
2615 /* Symbol isn't referenced locally. We only allow
2616 branch to symbol with non-default visibility. */
2618 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
);
2625 const char *pic
= "";
2627 switch (ELF_ST_VISIBILITY (h
->other
))
2630 v
= _("hidden symbol");
2633 v
= _("internal symbol");
2636 v
= _("protected symbol");
2640 pic
= _("; recompile with -fPIC");
2645 fmt
= _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
2647 fmt
= _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
2649 (*_bfd_error_handler
) (fmt
, input_bfd
,
2650 x86_64_elf_howto_table
[r_type
].name
,
2651 v
, h
->root
.root
.string
, pic
);
2652 bfd_set_error (bfd_error_bad_value
);
2663 /* FIXME: The ABI says the linker should make sure the value is
2664 the same when it's zeroextended to 64 bit. */
2666 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2671 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2672 || h
->root
.type
!= bfd_link_hash_undefweak
)
2673 && ((r_type
!= R_X86_64_PC8
2674 && r_type
!= R_X86_64_PC16
2675 && r_type
!= R_X86_64_PC32
2676 && r_type
!= R_X86_64_PC64
)
2677 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2678 || (ELIMINATE_COPY_RELOCS
2685 || h
->root
.type
== bfd_link_hash_undefweak
2686 || h
->root
.type
== bfd_link_hash_undefined
)))
2688 Elf_Internal_Rela outrel
;
2690 bfd_boolean skip
, relocate
;
2693 /* When generating a shared object, these relocations
2694 are copied into the output file to be resolved at run
2700 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2702 if (outrel
.r_offset
== (bfd_vma
) -1)
2704 else if (outrel
.r_offset
== (bfd_vma
) -2)
2705 skip
= TRUE
, relocate
= TRUE
;
2707 outrel
.r_offset
+= (input_section
->output_section
->vma
2708 + input_section
->output_offset
);
2711 memset (&outrel
, 0, sizeof outrel
);
2713 /* h->dynindx may be -1 if this symbol was marked to
2717 && (r_type
== R_X86_64_PC8
2718 || r_type
== R_X86_64_PC16
2719 || r_type
== R_X86_64_PC32
2720 || r_type
== R_X86_64_PC64
2722 || !SYMBOLIC_BIND (info
, h
)
2723 || !h
->def_regular
))
2725 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
2726 outrel
.r_addend
= rel
->r_addend
;
2730 /* This symbol is local, or marked to become local. */
2731 if (r_type
== R_X86_64_64
)
2734 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2735 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2741 if (bfd_is_abs_section (sec
))
2743 else if (sec
== NULL
|| sec
->owner
== NULL
)
2745 bfd_set_error (bfd_error_bad_value
);
2752 /* We are turning this relocation into one
2753 against a section symbol. It would be
2754 proper to subtract the symbol's value,
2755 osec->vma, from the emitted reloc addend,
2756 but ld.so expects buggy relocs. */
2757 osec
= sec
->output_section
;
2758 sindx
= elf_section_data (osec
)->dynindx
;
2761 asection
*oi
= htab
->elf
.text_index_section
;
2762 sindx
= elf_section_data (oi
)->dynindx
;
2764 BFD_ASSERT (sindx
!= 0);
2767 outrel
.r_info
= ELF64_R_INFO (sindx
, r_type
);
2768 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2772 sreloc
= elf_section_data (input_section
)->sreloc
;
2776 loc
= sreloc
->contents
;
2777 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2778 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2780 /* If this reloc is against an external symbol, we do
2781 not want to fiddle with the addend. Otherwise, we
2782 need to include the symbol value so that it becomes
2783 an addend for the dynamic reloc. */
2790 case R_X86_64_TLSGD
:
2791 case R_X86_64_GOTPC32_TLSDESC
:
2792 case R_X86_64_TLSDESC_CALL
:
2793 case R_X86_64_GOTTPOFF
:
2794 tls_type
= GOT_UNKNOWN
;
2795 if (h
== NULL
&& local_got_offsets
)
2796 tls_type
= elf64_x86_64_local_got_tls_type (input_bfd
) [r_symndx
];
2798 tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
2800 if (! elf64_x86_64_tls_transition (info
, input_bfd
,
2801 input_section
, contents
,
2802 symtab_hdr
, sym_hashes
,
2803 &r_type
, tls_type
, rel
,
2807 if (r_type
== R_X86_64_TPOFF32
)
2809 bfd_vma roff
= rel
->r_offset
;
2811 BFD_ASSERT (! unresolved_reloc
);
2813 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
2815 /* GD->LE transition.
2816 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2817 .word 0x6666; rex64; call __tls_get_addr
2820 leaq foo@tpoff(%rax), %rax */
2821 memcpy (contents
+ roff
- 4,
2822 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2824 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2825 contents
+ roff
+ 8);
2826 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
2830 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
2832 /* GDesc -> LE transition.
2833 It's originally something like:
2834 leaq x@tlsdesc(%rip), %rax
2840 unsigned int val
, type
, type2
;
2842 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
2843 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
2844 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
2845 bfd_put_8 (output_bfd
, 0x48 | ((type
>> 2) & 1),
2846 contents
+ roff
- 3);
2847 bfd_put_8 (output_bfd
, 0xc7, contents
+ roff
- 2);
2848 bfd_put_8 (output_bfd
, 0xc0 | ((val
>> 3) & 7),
2849 contents
+ roff
- 1);
2850 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2854 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
2856 /* GDesc -> LE transition.
2861 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
2862 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
2865 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTTPOFF
)
2867 /* IE->LE transition:
2868 Originally it can be one of:
2869 movq foo@gottpoff(%rip), %reg
2870 addq foo@gottpoff(%rip), %reg
2873 leaq foo(%reg), %reg
2876 unsigned int val
, type
, reg
;
2878 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
2879 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
2880 reg
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
2886 bfd_put_8 (output_bfd
, 0x49,
2887 contents
+ roff
- 3);
2888 bfd_put_8 (output_bfd
, 0xc7,
2889 contents
+ roff
- 2);
2890 bfd_put_8 (output_bfd
, 0xc0 | reg
,
2891 contents
+ roff
- 1);
2895 /* addq -> addq - addressing with %rsp/%r12 is
2898 bfd_put_8 (output_bfd
, 0x49,
2899 contents
+ roff
- 3);
2900 bfd_put_8 (output_bfd
, 0x81,
2901 contents
+ roff
- 2);
2902 bfd_put_8 (output_bfd
, 0xc0 | reg
,
2903 contents
+ roff
- 1);
2909 bfd_put_8 (output_bfd
, 0x4d,
2910 contents
+ roff
- 3);
2911 bfd_put_8 (output_bfd
, 0x8d,
2912 contents
+ roff
- 2);
2913 bfd_put_8 (output_bfd
, 0x80 | reg
| (reg
<< 3),
2914 contents
+ roff
- 1);
2916 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2924 if (htab
->sgot
== NULL
)
2929 off
= h
->got
.offset
;
2930 offplt
= elf64_x86_64_hash_entry (h
)->tlsdesc_got
;
2934 if (local_got_offsets
== NULL
)
2937 off
= local_got_offsets
[r_symndx
];
2938 offplt
= local_tlsdesc_gotents
[r_symndx
];
2945 Elf_Internal_Rela outrel
;
2950 if (htab
->srelgot
== NULL
)
2953 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
2955 if (GOT_TLS_GDESC_P (tls_type
))
2957 outrel
.r_info
= ELF64_R_INFO (indx
, R_X86_64_TLSDESC
);
2958 BFD_ASSERT (htab
->sgotplt_jump_table_size
+ offplt
2959 + 2 * GOT_ENTRY_SIZE
<= htab
->sgotplt
->size
);
2960 outrel
.r_offset
= (htab
->sgotplt
->output_section
->vma
2961 + htab
->sgotplt
->output_offset
2963 + htab
->sgotplt_jump_table_size
);
2964 sreloc
= htab
->srelplt
;
2965 loc
= sreloc
->contents
;
2966 loc
+= sreloc
->reloc_count
++
2967 * sizeof (Elf64_External_Rela
);
2968 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
2969 <= sreloc
->contents
+ sreloc
->size
);
2971 outrel
.r_addend
= relocation
- dtpoff_base (info
);
2973 outrel
.r_addend
= 0;
2974 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2977 sreloc
= htab
->srelgot
;
2979 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
2980 + htab
->sgot
->output_offset
+ off
);
2982 if (GOT_TLS_GD_P (tls_type
))
2983 dr_type
= R_X86_64_DTPMOD64
;
2984 else if (GOT_TLS_GDESC_P (tls_type
))
2987 dr_type
= R_X86_64_TPOFF64
;
2989 bfd_put_64 (output_bfd
, 0, htab
->sgot
->contents
+ off
);
2990 outrel
.r_addend
= 0;
2991 if ((dr_type
== R_X86_64_TPOFF64
2992 || dr_type
== R_X86_64_TLSDESC
) && indx
== 0)
2993 outrel
.r_addend
= relocation
- dtpoff_base (info
);
2994 outrel
.r_info
= ELF64_R_INFO (indx
, dr_type
);
2996 loc
= sreloc
->contents
;
2997 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2998 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
2999 <= sreloc
->contents
+ sreloc
->size
);
3000 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3002 if (GOT_TLS_GD_P (tls_type
))
3006 BFD_ASSERT (! unresolved_reloc
);
3007 bfd_put_64 (output_bfd
,
3008 relocation
- dtpoff_base (info
),
3009 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3013 bfd_put_64 (output_bfd
, 0,
3014 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3015 outrel
.r_info
= ELF64_R_INFO (indx
,
3017 outrel
.r_offset
+= GOT_ENTRY_SIZE
;
3018 sreloc
->reloc_count
++;
3019 loc
+= sizeof (Elf64_External_Rela
);
3020 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
3021 <= sreloc
->contents
+ sreloc
->size
);
3022 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3030 local_got_offsets
[r_symndx
] |= 1;
3033 if (off
>= (bfd_vma
) -2
3034 && ! GOT_TLS_GDESC_P (tls_type
))
3036 if (r_type
== ELF64_R_TYPE (rel
->r_info
))
3038 if (r_type
== R_X86_64_GOTPC32_TLSDESC
3039 || r_type
== R_X86_64_TLSDESC_CALL
)
3040 relocation
= htab
->sgotplt
->output_section
->vma
3041 + htab
->sgotplt
->output_offset
3042 + offplt
+ htab
->sgotplt_jump_table_size
;
3044 relocation
= htab
->sgot
->output_section
->vma
3045 + htab
->sgot
->output_offset
+ off
;
3046 unresolved_reloc
= FALSE
;
3050 bfd_vma roff
= rel
->r_offset
;
3052 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
3054 /* GD->IE transition.
3055 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3056 .word 0x6666; rex64; call __tls_get_addr@plt
3059 addq foo@gottpoff(%rip), %rax */
3060 memcpy (contents
+ roff
- 4,
3061 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3064 relocation
= (htab
->sgot
->output_section
->vma
3065 + htab
->sgot
->output_offset
+ off
3067 - input_section
->output_section
->vma
3068 - input_section
->output_offset
3070 bfd_put_32 (output_bfd
, relocation
,
3071 contents
+ roff
+ 8);
3072 /* Skip R_X86_64_PLT32. */
3076 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
3078 /* GDesc -> IE transition.
3079 It's originally something like:
3080 leaq x@tlsdesc(%rip), %rax
3083 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3086 unsigned int val
, type
, type2
;
3088 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
3089 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
3090 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
3092 /* Now modify the instruction as appropriate. To
3093 turn a leaq into a movq in the form we use it, it
3094 suffices to change the second byte from 0x8d to
3096 bfd_put_8 (output_bfd
, 0x8b, contents
+ roff
- 2);
3098 bfd_put_32 (output_bfd
,
3099 htab
->sgot
->output_section
->vma
3100 + htab
->sgot
->output_offset
+ off
3102 - input_section
->output_section
->vma
3103 - input_section
->output_offset
3108 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
3110 /* GDesc -> IE transition.
3117 unsigned int val
, type
;
3119 type
= bfd_get_8 (input_bfd
, contents
+ roff
);
3120 val
= bfd_get_8 (input_bfd
, contents
+ roff
+ 1);
3121 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
3122 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
3130 case R_X86_64_TLSLD
:
3131 if (! elf64_x86_64_tls_transition (info
, input_bfd
,
3132 input_section
, contents
,
3133 symtab_hdr
, sym_hashes
,
3134 &r_type
, GOT_UNKNOWN
,
3138 if (r_type
!= R_X86_64_TLSLD
)
3140 /* LD->LE transition:
3141 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3143 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3145 BFD_ASSERT (r_type
== R_X86_64_TPOFF32
);
3146 memcpy (contents
+ rel
->r_offset
- 3,
3147 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3148 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3153 if (htab
->sgot
== NULL
)
3156 off
= htab
->tls_ld_got
.offset
;
3161 Elf_Internal_Rela outrel
;
3164 if (htab
->srelgot
== NULL
)
3167 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
3168 + htab
->sgot
->output_offset
+ off
);
3170 bfd_put_64 (output_bfd
, 0,
3171 htab
->sgot
->contents
+ off
);
3172 bfd_put_64 (output_bfd
, 0,
3173 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3174 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_DTPMOD64
);
3175 outrel
.r_addend
= 0;
3176 loc
= htab
->srelgot
->contents
;
3177 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3178 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3179 htab
->tls_ld_got
.offset
|= 1;
3181 relocation
= htab
->sgot
->output_section
->vma
3182 + htab
->sgot
->output_offset
+ off
;
3183 unresolved_reloc
= FALSE
;
3186 case R_X86_64_DTPOFF32
:
3187 if (info
->shared
|| (input_section
->flags
& SEC_CODE
) == 0)
3188 relocation
-= dtpoff_base (info
);
3190 relocation
= tpoff (info
, relocation
);
3193 case R_X86_64_TPOFF32
:
3194 BFD_ASSERT (! info
->shared
);
3195 relocation
= tpoff (info
, relocation
);
3202 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3203 because such sections are not SEC_ALLOC and thus ld.so will
3204 not process them. */
3205 if (unresolved_reloc
3206 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
3208 (*_bfd_error_handler
)
3209 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3212 (long) rel
->r_offset
,
3214 h
->root
.root
.string
);
3216 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3217 contents
, rel
->r_offset
,
3218 relocation
, rel
->r_addend
);
3220 if (r
!= bfd_reloc_ok
)
3225 name
= h
->root
.root
.string
;
3228 name
= bfd_elf_string_from_elf_section (input_bfd
,
3229 symtab_hdr
->sh_link
,
3234 name
= bfd_section_name (input_bfd
, sec
);
3237 if (r
== bfd_reloc_overflow
)
3239 if (! ((*info
->callbacks
->reloc_overflow
)
3240 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3241 (bfd_vma
) 0, input_bfd
, input_section
,
3247 (*_bfd_error_handler
)
3248 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3249 input_bfd
, input_section
,
3250 (long) rel
->r_offset
, name
, (int) r
);
3259 /* Finish up dynamic symbol handling. We set the contents of various
3260 dynamic sections here. */
3263 elf64_x86_64_finish_dynamic_symbol (bfd
*output_bfd
,
3264 struct bfd_link_info
*info
,
3265 struct elf_link_hash_entry
*h
,
3266 Elf_Internal_Sym
*sym
)
3268 struct elf64_x86_64_link_hash_table
*htab
;
3270 htab
= elf64_x86_64_hash_table (info
);
3272 if (h
->plt
.offset
!= (bfd_vma
) -1)
3276 Elf_Internal_Rela rela
;
3279 /* This symbol has an entry in the procedure linkage table. Set
3281 if (h
->dynindx
== -1
3282 || htab
->splt
== NULL
3283 || htab
->sgotplt
== NULL
3284 || htab
->srelplt
== NULL
)
3287 /* Get the index in the procedure linkage table which
3288 corresponds to this symbol. This is the index of this symbol
3289 in all the symbols for which we are making plt entries. The
3290 first entry in the procedure linkage table is reserved. */
3291 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
3293 /* Get the offset into the .got table of the entry that
3294 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3295 bytes. The first three are reserved for the dynamic linker. */
3296 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
3298 /* Fill in the entry in the procedure linkage table. */
3299 memcpy (htab
->splt
->contents
+ h
->plt
.offset
, elf64_x86_64_plt_entry
,
3302 /* Insert the relocation positions of the plt section. The magic
3303 numbers at the end of the statements are the positions of the
3304 relocations in the plt section. */
3305 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3306 instruction uses 6 bytes, subtract this value. */
3307 bfd_put_32 (output_bfd
,
3308 (htab
->sgotplt
->output_section
->vma
3309 + htab
->sgotplt
->output_offset
3311 - htab
->splt
->output_section
->vma
3312 - htab
->splt
->output_offset
3315 htab
->splt
->contents
+ h
->plt
.offset
+ 2);
3316 /* Put relocation index. */
3317 bfd_put_32 (output_bfd
, plt_index
,
3318 htab
->splt
->contents
+ h
->plt
.offset
+ 7);
3319 /* Put offset for jmp .PLT0. */
3320 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
3321 htab
->splt
->contents
+ h
->plt
.offset
+ 12);
3323 /* Fill in the entry in the global offset table, initially this
3324 points to the pushq instruction in the PLT which is at offset 6. */
3325 bfd_put_64 (output_bfd
, (htab
->splt
->output_section
->vma
3326 + htab
->splt
->output_offset
3327 + h
->plt
.offset
+ 6),
3328 htab
->sgotplt
->contents
+ got_offset
);
3330 /* Fill in the entry in the .rela.plt section. */
3331 rela
.r_offset
= (htab
->sgotplt
->output_section
->vma
3332 + htab
->sgotplt
->output_offset
3334 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_JUMP_SLOT
);
3336 loc
= htab
->srelplt
->contents
+ plt_index
* sizeof (Elf64_External_Rela
);
3337 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3339 if (!h
->def_regular
)
3341 /* Mark the symbol as undefined, rather than as defined in
3342 the .plt section. Leave the value if there were any
3343 relocations where pointer equality matters (this is a clue
3344 for the dynamic linker, to make function pointer
3345 comparisons work between an application and shared
3346 library), otherwise set it to zero. If a function is only
3347 called from a binary, there is no need to slow down
3348 shared libraries because of that. */
3349 sym
->st_shndx
= SHN_UNDEF
;
3350 if (!h
->pointer_equality_needed
)
3355 if (h
->got
.offset
!= (bfd_vma
) -1
3356 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h
)->tls_type
)
3357 && elf64_x86_64_hash_entry (h
)->tls_type
!= GOT_TLS_IE
)
3359 Elf_Internal_Rela rela
;
3362 /* This symbol has an entry in the global offset table. Set it
3364 if (htab
->sgot
== NULL
|| htab
->srelgot
== NULL
)
3367 rela
.r_offset
= (htab
->sgot
->output_section
->vma
3368 + htab
->sgot
->output_offset
3369 + (h
->got
.offset
&~ (bfd_vma
) 1));
3371 /* If this is a static link, or it is a -Bsymbolic link and the
3372 symbol is defined locally or was forced to be local because
3373 of a version file, we just want to emit a RELATIVE reloc.
3374 The entry in the global offset table will already have been
3375 initialized in the relocate_section function. */
3377 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3379 if (!h
->def_regular
)
3381 BFD_ASSERT((h
->got
.offset
& 1) != 0);
3382 rela
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
3383 rela
.r_addend
= (h
->root
.u
.def
.value
3384 + h
->root
.u
.def
.section
->output_section
->vma
3385 + h
->root
.u
.def
.section
->output_offset
);
3389 BFD_ASSERT((h
->got
.offset
& 1) == 0);
3390 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
3391 htab
->sgot
->contents
+ h
->got
.offset
);
3392 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_GLOB_DAT
);
3396 loc
= htab
->srelgot
->contents
;
3397 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3398 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3403 Elf_Internal_Rela rela
;
3406 /* This symbol needs a copy reloc. Set it up. */
3408 if (h
->dynindx
== -1
3409 || (h
->root
.type
!= bfd_link_hash_defined
3410 && h
->root
.type
!= bfd_link_hash_defweak
)
3411 || htab
->srelbss
== NULL
)
3414 rela
.r_offset
= (h
->root
.u
.def
.value
3415 + h
->root
.u
.def
.section
->output_section
->vma
3416 + h
->root
.u
.def
.section
->output_offset
);
3417 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_COPY
);
3419 loc
= htab
->srelbss
->contents
;
3420 loc
+= htab
->srelbss
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3421 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3424 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3425 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
3426 || h
== htab
->elf
.hgot
)
3427 sym
->st_shndx
= SHN_ABS
;
3432 /* Used to decide how to sort relocs in an optimal manner for the
3433 dynamic linker, before writing them out. */
3435 static enum elf_reloc_type_class
3436 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela
*rela
)
3438 switch ((int) ELF64_R_TYPE (rela
->r_info
))
3440 case R_X86_64_RELATIVE
:
3441 return reloc_class_relative
;
3442 case R_X86_64_JUMP_SLOT
:
3443 return reloc_class_plt
;
3445 return reloc_class_copy
;
3447 return reloc_class_normal
;
3451 /* Finish up the dynamic sections. */
3454 elf64_x86_64_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
3456 struct elf64_x86_64_link_hash_table
*htab
;
3460 htab
= elf64_x86_64_hash_table (info
);
3461 dynobj
= htab
->elf
.dynobj
;
3462 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3464 if (htab
->elf
.dynamic_sections_created
)
3466 Elf64_External_Dyn
*dyncon
, *dynconend
;
3468 if (sdyn
== NULL
|| htab
->sgot
== NULL
)
3471 dyncon
= (Elf64_External_Dyn
*) sdyn
->contents
;
3472 dynconend
= (Elf64_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
3473 for (; dyncon
< dynconend
; dyncon
++)
3475 Elf_Internal_Dyn dyn
;
3478 bfd_elf64_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3487 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3491 dyn
.d_un
.d_ptr
= htab
->srelplt
->output_section
->vma
;
3495 s
= htab
->srelplt
->output_section
;
3496 dyn
.d_un
.d_val
= s
->size
;
3500 /* The procedure linkage table relocs (DT_JMPREL) should
3501 not be included in the overall relocs (DT_RELA).
3502 Therefore, we override the DT_RELASZ entry here to
3503 make it not include the JMPREL relocs. Since the
3504 linker script arranges for .rela.plt to follow all
3505 other relocation sections, we don't have to worry
3506 about changing the DT_RELA entry. */
3507 if (htab
->srelplt
!= NULL
)
3509 s
= htab
->srelplt
->output_section
;
3510 dyn
.d_un
.d_val
-= s
->size
;
3514 case DT_TLSDESC_PLT
:
3516 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
3517 + htab
->tlsdesc_plt
;
3520 case DT_TLSDESC_GOT
:
3522 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
3523 + htab
->tlsdesc_got
;
3527 bfd_elf64_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3530 /* Fill in the special first entry in the procedure linkage table. */
3531 if (htab
->splt
&& htab
->splt
->size
> 0)
3533 /* Fill in the first entry in the procedure linkage table. */
3534 memcpy (htab
->splt
->contents
, elf64_x86_64_plt0_entry
,
3536 /* Add offset for pushq GOT+8(%rip), since the instruction
3537 uses 6 bytes subtract this value. */
3538 bfd_put_32 (output_bfd
,
3539 (htab
->sgotplt
->output_section
->vma
3540 + htab
->sgotplt
->output_offset
3542 - htab
->splt
->output_section
->vma
3543 - htab
->splt
->output_offset
3545 htab
->splt
->contents
+ 2);
3546 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3547 the end of the instruction. */
3548 bfd_put_32 (output_bfd
,
3549 (htab
->sgotplt
->output_section
->vma
3550 + htab
->sgotplt
->output_offset
3552 - htab
->splt
->output_section
->vma
3553 - htab
->splt
->output_offset
3555 htab
->splt
->contents
+ 8);
3557 elf_section_data (htab
->splt
->output_section
)->this_hdr
.sh_entsize
=
3560 if (htab
->tlsdesc_plt
)
3562 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
3563 htab
->sgot
->contents
+ htab
->tlsdesc_got
);
3565 memcpy (htab
->splt
->contents
+ htab
->tlsdesc_plt
,
3566 elf64_x86_64_plt0_entry
,
3569 /* Add offset for pushq GOT+8(%rip), since the
3570 instruction uses 6 bytes subtract this value. */
3571 bfd_put_32 (output_bfd
,
3572 (htab
->sgotplt
->output_section
->vma
3573 + htab
->sgotplt
->output_offset
3575 - htab
->splt
->output_section
->vma
3576 - htab
->splt
->output_offset
3579 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 2);
3580 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3581 htab->tlsdesc_got. The 12 is the offset to the end of
3583 bfd_put_32 (output_bfd
,
3584 (htab
->sgot
->output_section
->vma
3585 + htab
->sgot
->output_offset
3587 - htab
->splt
->output_section
->vma
3588 - htab
->splt
->output_offset
3591 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 8);
3598 /* Fill in the first three entries in the global offset table. */
3599 if (htab
->sgotplt
->size
> 0)
3601 /* Set the first entry in the global offset table to the address of
3602 the dynamic section. */
3604 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
);
3606 bfd_put_64 (output_bfd
,
3607 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
3608 htab
->sgotplt
->contents
);
3609 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3610 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3611 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
*2);
3614 elf_section_data (htab
->sgotplt
->output_section
)->this_hdr
.sh_entsize
=
3618 if (htab
->sgot
&& htab
->sgot
->size
> 0)
3619 elf_section_data (htab
->sgot
->output_section
)->this_hdr
.sh_entsize
3625 /* Return address for Ith PLT stub in section PLT, for relocation REL
3626 or (bfd_vma) -1 if it should not be included. */
3629 elf64_x86_64_plt_sym_val (bfd_vma i
, const asection
*plt
,
3630 const arelent
*rel ATTRIBUTE_UNUSED
)
3632 return plt
->vma
+ (i
+ 1) * PLT_ENTRY_SIZE
;
3635 /* Handle an x86-64 specific section when reading an object file. This
3636 is called when elfcode.h finds a section with an unknown type. */
3639 elf64_x86_64_section_from_shdr (bfd
*abfd
,
3640 Elf_Internal_Shdr
*hdr
,
3644 if (hdr
->sh_type
!= SHT_X86_64_UNWIND
)
3647 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3653 /* Hook called by the linker routine which adds symbols from an object
3654 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3658 elf64_x86_64_add_symbol_hook (bfd
*abfd
,
3659 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3660 Elf_Internal_Sym
*sym
,
3661 const char **namep ATTRIBUTE_UNUSED
,
3662 flagword
*flagsp ATTRIBUTE_UNUSED
,
3663 asection
**secp
, bfd_vma
*valp
)
3667 switch (sym
->st_shndx
)
3669 case SHN_X86_64_LCOMMON
:
3670 lcomm
= bfd_get_section_by_name (abfd
, "LARGE_COMMON");
3673 lcomm
= bfd_make_section_with_flags (abfd
,
3677 | SEC_LINKER_CREATED
));
3680 elf_section_flags (lcomm
) |= SHF_X86_64_LARGE
;
3683 *valp
= sym
->st_size
;
3690 /* Given a BFD section, try to locate the corresponding ELF section
3694 elf64_x86_64_elf_section_from_bfd_section (bfd
*abfd ATTRIBUTE_UNUSED
,
3695 asection
*sec
, int *index
)
3697 if (sec
== &_bfd_elf_large_com_section
)
3699 *index
= SHN_X86_64_LCOMMON
;
3705 /* Process a symbol. */
3708 elf64_x86_64_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
3711 elf_symbol_type
*elfsym
= (elf_symbol_type
*) asym
;
3713 switch (elfsym
->internal_elf_sym
.st_shndx
)
3715 case SHN_X86_64_LCOMMON
:
3716 asym
->section
= &_bfd_elf_large_com_section
;
3717 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
3718 /* Common symbol doesn't set BSF_GLOBAL. */
3719 asym
->flags
&= ~BSF_GLOBAL
;
3725 elf64_x86_64_common_definition (Elf_Internal_Sym
*sym
)
3727 return (sym
->st_shndx
== SHN_COMMON
3728 || sym
->st_shndx
== SHN_X86_64_LCOMMON
);
3732 elf64_x86_64_common_section_index (asection
*sec
)
3734 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
3737 return SHN_X86_64_LCOMMON
;
3741 elf64_x86_64_common_section (asection
*sec
)
3743 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
3744 return bfd_com_section_ptr
;
3746 return &_bfd_elf_large_com_section
;
3750 elf64_x86_64_merge_symbol (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3751 struct elf_link_hash_entry
**sym_hash ATTRIBUTE_UNUSED
,
3752 struct elf_link_hash_entry
*h
,
3753 Elf_Internal_Sym
*sym
,
3755 bfd_vma
*pvalue ATTRIBUTE_UNUSED
,
3756 unsigned int *pold_alignment ATTRIBUTE_UNUSED
,
3757 bfd_boolean
*skip ATTRIBUTE_UNUSED
,
3758 bfd_boolean
*override ATTRIBUTE_UNUSED
,
3759 bfd_boolean
*type_change_ok ATTRIBUTE_UNUSED
,
3760 bfd_boolean
*size_change_ok ATTRIBUTE_UNUSED
,
3761 bfd_boolean
*newdef ATTRIBUTE_UNUSED
,
3762 bfd_boolean
*newdyn
,
3763 bfd_boolean
*newdyncommon ATTRIBUTE_UNUSED
,
3764 bfd_boolean
*newweak ATTRIBUTE_UNUSED
,
3765 bfd
*abfd ATTRIBUTE_UNUSED
,
3767 bfd_boolean
*olddef ATTRIBUTE_UNUSED
,
3768 bfd_boolean
*olddyn
,
3769 bfd_boolean
*olddyncommon ATTRIBUTE_UNUSED
,
3770 bfd_boolean
*oldweak ATTRIBUTE_UNUSED
,
3774 /* A normal common symbol and a large common symbol result in a
3775 normal common symbol. We turn the large common symbol into a
3778 && h
->root
.type
== bfd_link_hash_common
3780 && bfd_is_com_section (*sec
)
3783 if (sym
->st_shndx
== SHN_COMMON
3784 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) != 0)
3786 h
->root
.u
.c
.p
->section
3787 = bfd_make_section_old_way (oldbfd
, "COMMON");
3788 h
->root
.u
.c
.p
->section
->flags
= SEC_ALLOC
;
3790 else if (sym
->st_shndx
== SHN_X86_64_LCOMMON
3791 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) == 0)
3792 *psec
= *sec
= bfd_com_section_ptr
;
3799 elf64_x86_64_additional_program_headers (bfd
*abfd
,
3800 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
3805 /* Check to see if we need a large readonly segment. */
3806 s
= bfd_get_section_by_name (abfd
, ".lrodata");
3807 if (s
&& (s
->flags
& SEC_LOAD
))
3810 /* Check to see if we need a large data segment. Since .lbss sections
3811 is placed right after the .bss section, there should be no need for
3812 a large data segment just because of .lbss. */
3813 s
= bfd_get_section_by_name (abfd
, ".ldata");
3814 if (s
&& (s
->flags
& SEC_LOAD
))
3820 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3823 elf64_x86_64_hash_symbol (struct elf_link_hash_entry
*h
)
3825 if (h
->plt
.offset
!= (bfd_vma
) -1
3827 && !h
->pointer_equality_needed
)
3830 return _bfd_elf_hash_symbol (h
);
3833 static const struct bfd_elf_special_section
3834 elf64_x86_64_special_sections
[]=
3836 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3837 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
3838 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
+ SHF_X86_64_LARGE
},
3839 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3840 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3841 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
3842 { NULL
, 0, 0, 0, 0 }
3845 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3846 #define TARGET_LITTLE_NAME "elf64-x86-64"
3847 #define ELF_ARCH bfd_arch_i386
3848 #define ELF_MACHINE_CODE EM_X86_64
3849 #define ELF_MAXPAGESIZE 0x200000
3850 #define ELF_MINPAGESIZE 0x1000
3851 #define ELF_COMMONPAGESIZE 0x1000
3853 #define elf_backend_can_gc_sections 1
3854 #define elf_backend_can_refcount 1
3855 #define elf_backend_want_got_plt 1
3856 #define elf_backend_plt_readonly 1
3857 #define elf_backend_want_plt_sym 0
3858 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
3859 #define elf_backend_rela_normal 1
3861 #define elf_info_to_howto elf64_x86_64_info_to_howto
3863 #define bfd_elf64_bfd_link_hash_table_create \
3864 elf64_x86_64_link_hash_table_create
3865 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
3866 #define bfd_elf64_bfd_reloc_name_lookup \
3867 elf64_x86_64_reloc_name_lookup
3869 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3870 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
3871 #define elf_backend_check_relocs elf64_x86_64_check_relocs
3872 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3873 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3874 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3875 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3876 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3877 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3878 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3879 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
3880 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
3881 #define elf_backend_relocate_section elf64_x86_64_relocate_section
3882 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
3883 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
3884 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
3885 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
3886 #define elf_backend_object_p elf64_x86_64_elf_object_p
3887 #define bfd_elf64_mkobject elf64_x86_64_mkobject
3889 #define elf_backend_section_from_shdr \
3890 elf64_x86_64_section_from_shdr
3892 #define elf_backend_section_from_bfd_section \
3893 elf64_x86_64_elf_section_from_bfd_section
3894 #define elf_backend_add_symbol_hook \
3895 elf64_x86_64_add_symbol_hook
3896 #define elf_backend_symbol_processing \
3897 elf64_x86_64_symbol_processing
3898 #define elf_backend_common_section_index \
3899 elf64_x86_64_common_section_index
3900 #define elf_backend_common_section \
3901 elf64_x86_64_common_section
3902 #define elf_backend_common_definition \
3903 elf64_x86_64_common_definition
3904 #define elf_backend_merge_symbol \
3905 elf64_x86_64_merge_symbol
3906 #define elf_backend_special_sections \
3907 elf64_x86_64_special_sections
3908 #define elf_backend_additional_program_headers \
3909 elf64_x86_64_additional_program_headers
3910 #define elf_backend_hash_symbol \
3911 elf64_x86_64_hash_symbol
3913 #include "elf64-target.h"
3915 /* FreeBSD support. */
3917 #undef TARGET_LITTLE_SYM
3918 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3919 #undef TARGET_LITTLE_NAME
3920 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3923 #define ELF_OSABI ELFOSABI_FREEBSD
3925 #undef elf_backend_post_process_headers
3926 #define elf_backend_post_process_headers _bfd_elf_set_osabi
3929 #define elf64_bed elf64_x86_64_fbsd_bed
3931 #include "elf64-target.h"