Commit | Line | Data |
---|---|---|
8d88c4ca | 1 | /* X86-64 specific support for 64-bit ELF |
c434dee6 | 2 | Copyright 2000, 2001, 2002 Free Software Foundation, Inc. |
8d88c4ca NC |
3 | Contributed by Jan Hubicka <jh@suse.cz>. |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
c434dee6 | 23 | #include "bfdlink.h" |
8d88c4ca NC |
24 | #include "libbfd.h" |
25 | #include "elf-bfd.h" | |
26 | ||
27 | #include "elf/x86-64.h" | |
28 | ||
29 | /* We use only the RELA entries. */ | |
c434dee6 | 30 | #define USE_RELA 1 |
8d88c4ca NC |
31 | |
32 | /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */ | |
33 | #define MINUS_ONE (~ (bfd_vma) 0) | |
34 | ||
35 | /* The relocation "howto" table. Order of fields: | |
407443a3 AJ |
36 | type, size, bitsize, pc_relative, complain_on_overflow, |
37 | special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */ | |
70256ad8 AJ |
38 | static reloc_howto_type x86_64_elf_howto_table[] = |
39 | { | |
407443a3 | 40 | HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont, |
fe4770f4 AJ |
41 | bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0x00000000, 0x00000000, |
42 | false), | |
407443a3 | 43 | HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield, |
fe4770f4 AJ |
44 | bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE, |
45 | false), | |
407443a3 | 46 | HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed, |
fe4770f4 AJ |
47 | bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff, |
48 | true), | |
407443a3 | 49 | HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed, |
fe4770f4 AJ |
50 | bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff, |
51 | false), | |
407443a3 | 52 | HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed, |
fe4770f4 AJ |
53 | bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff, |
54 | true), | |
407443a3 | 55 | HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield, |
fe4770f4 AJ |
56 | bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff, |
57 | false), | |
407443a3 | 58 | HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield, |
fe4770f4 AJ |
59 | bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE, |
60 | MINUS_ONE, false), | |
407443a3 | 61 | HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield, |
fe4770f4 AJ |
62 | bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE, |
63 | MINUS_ONE, false), | |
4a73f21b | 64 | HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield, |
fe4770f4 AJ |
65 | bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE, |
66 | MINUS_ONE, false), | |
407443a3 | 67 | HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed, |
fe4770f4 AJ |
68 | bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff, |
69 | 0xffffffff, true), | |
407443a3 | 70 | HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned, |
fe4770f4 AJ |
71 | bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff, |
72 | false), | |
407443a3 | 73 | HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed, |
fe4770f4 AJ |
74 | bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff, |
75 | false), | |
407443a3 AJ |
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_signed, | |
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, | |
fe4770f4 AJ |
83 | bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true), |
84 | ||
85 | /* GNU extension to record C++ vtable hierarchy. */ | |
86 | HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont, | |
87 | NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false), | |
88 | ||
89 | /* GNU extension to record C++ vtable member usage. */ | |
90 | HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, false, 0, complain_overflow_dont, | |
91 | _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0, | |
92 | false) | |
8d88c4ca NC |
93 | }; |
94 | ||
95 | /* Map BFD relocs to the x86_64 elf relocs. */ | |
70256ad8 AJ |
96 | struct elf_reloc_map |
97 | { | |
8d88c4ca NC |
98 | bfd_reloc_code_real_type bfd_reloc_val; |
99 | unsigned char elf_reloc_val; | |
100 | }; | |
101 | ||
dc810e39 | 102 | static const struct elf_reloc_map x86_64_reloc_map[] = |
8d88c4ca | 103 | { |
70256ad8 AJ |
104 | { BFD_RELOC_NONE, R_X86_64_NONE, }, |
105 | { BFD_RELOC_64, R_X86_64_64, }, | |
106 | { BFD_RELOC_32_PCREL, R_X86_64_PC32, }, | |
107 | { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,}, | |
108 | { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,}, | |
109 | { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, }, | |
110 | { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, }, | |
111 | { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, }, | |
112 | { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, }, | |
113 | { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, }, | |
114 | { BFD_RELOC_32, R_X86_64_32, }, | |
115 | { BFD_RELOC_X86_64_32S, R_X86_64_32S, }, | |
116 | { BFD_RELOC_16, R_X86_64_16, }, | |
117 | { BFD_RELOC_16_PCREL, R_X86_64_PC16, }, | |
118 | { BFD_RELOC_8, R_X86_64_8, }, | |
119 | { BFD_RELOC_8_PCREL, R_X86_64_PC8, }, | |
fe4770f4 AJ |
120 | { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, }, |
121 | { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, }, | |
8d88c4ca NC |
122 | }; |
123 | ||
8d88c4ca NC |
124 | static reloc_howto_type *elf64_x86_64_reloc_type_lookup |
125 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
126 | static void elf64_x86_64_info_to_howto | |
127 | PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *)); | |
128 | static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create | |
129 | PARAMS ((bfd *)); | |
1e1b3bc2 | 130 | static boolean elf64_x86_64_elf_object_p PARAMS ((bfd *abfd)); |
c434dee6 AJ |
131 | static boolean create_got_section |
132 | PARAMS((bfd *, struct bfd_link_info *)); | |
133 | static boolean elf64_x86_64_create_dynamic_sections | |
134 | PARAMS((bfd *, struct bfd_link_info *)); | |
135 | static void elf64_x86_64_copy_indirect_symbol | |
136 | PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *)); | |
1e1b3bc2 AJ |
137 | static boolean elf64_x86_64_check_relocs |
138 | PARAMS ((bfd *, struct bfd_link_info *, asection *sec, | |
139 | const Elf_Internal_Rela *)); | |
140 | static asection *elf64_x86_64_gc_mark_hook | |
141 | PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *, | |
142 | struct elf_link_hash_entry *, Elf_Internal_Sym *)); | |
143 | ||
144 | static boolean elf64_x86_64_gc_sweep_hook | |
145 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
146 | const Elf_Internal_Rela *)); | |
70256ad8 | 147 | |
c434dee6 | 148 | static struct bfd_hash_entry *link_hash_newfunc |
70256ad8 AJ |
149 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); |
150 | static boolean elf64_x86_64_adjust_dynamic_symbol | |
151 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
152 | ||
c434dee6 AJ |
153 | static boolean allocate_dynrelocs |
154 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
155 | static boolean readonly_dynrelocs | |
156 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
70256ad8 AJ |
157 | static boolean elf64_x86_64_size_dynamic_sections |
158 | PARAMS ((bfd *, struct bfd_link_info *)); | |
8d88c4ca NC |
159 | static boolean elf64_x86_64_relocate_section |
160 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
407443a3 | 161 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); |
70256ad8 AJ |
162 | static boolean elf64_x86_64_finish_dynamic_symbol |
163 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, | |
164 | Elf_Internal_Sym *sym)); | |
165 | static boolean elf64_x86_64_finish_dynamic_sections | |
166 | PARAMS ((bfd *, struct bfd_link_info *)); | |
f51e552e AM |
167 | static enum elf_reloc_type_class elf64_x86_64_reloc_type_class |
168 | PARAMS ((const Elf_Internal_Rela *)); | |
8d88c4ca NC |
169 | |
170 | /* Given a BFD reloc type, return a HOWTO structure. */ | |
171 | static reloc_howto_type * | |
172 | elf64_x86_64_reloc_type_lookup (abfd, code) | |
173 | bfd *abfd ATTRIBUTE_UNUSED; | |
174 | bfd_reloc_code_real_type code; | |
175 | { | |
176 | unsigned int i; | |
177 | for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map); | |
178 | i++) | |
179 | { | |
180 | if (x86_64_reloc_map[i].bfd_reloc_val == code) | |
ffaef159 | 181 | return &x86_64_elf_howto_table[i]; |
8d88c4ca NC |
182 | } |
183 | return 0; | |
184 | } | |
185 | ||
8d88c4ca | 186 | /* Given an x86_64 ELF reloc type, fill in an arelent structure. */ |
8da6118f | 187 | |
8d88c4ca NC |
188 | static void |
189 | elf64_x86_64_info_to_howto (abfd, cache_ptr, dst) | |
190 | bfd *abfd ATTRIBUTE_UNUSED; | |
191 | arelent *cache_ptr; | |
192 | Elf64_Internal_Rela *dst; | |
193 | { | |
fe4770f4 | 194 | unsigned r_type, i; |
8d88c4ca NC |
195 | |
196 | r_type = ELF64_R_TYPE (dst->r_info); | |
fe4770f4 AJ |
197 | if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT) |
198 | { | |
199 | BFD_ASSERT (r_type <= (unsigned int) R_X86_64_PC8); | |
200 | i = r_type; | |
201 | } | |
202 | else | |
203 | { | |
204 | BFD_ASSERT (r_type < (unsigned int) R_X86_64_max); | |
205 | i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_PC8 - 1); | |
206 | } | |
207 | cache_ptr->howto = &x86_64_elf_howto_table[i]; | |
8d88c4ca NC |
208 | BFD_ASSERT (r_type == cache_ptr->howto->type); |
209 | } | |
70256ad8 | 210 | \f |
407443a3 | 211 | /* Functions for the x86-64 ELF linker. */ |
70256ad8 | 212 | |
407443a3 | 213 | /* The name of the dynamic interpreter. This is put in the .interp |
70256ad8 AJ |
214 | section. */ |
215 | ||
407443a3 | 216 | #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1" |
70256ad8 AJ |
217 | |
218 | /* The size in bytes of an entry in the global offset table. */ | |
219 | ||
220 | #define GOT_ENTRY_SIZE 8 | |
8d88c4ca | 221 | |
70256ad8 | 222 | /* The size in bytes of an entry in the procedure linkage table. */ |
8d88c4ca | 223 | |
70256ad8 AJ |
224 | #define PLT_ENTRY_SIZE 16 |
225 | ||
226 | /* The first entry in a procedure linkage table looks like this. See the | |
227 | SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */ | |
228 | ||
229 | static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] = | |
230 | { | |
653165cc AJ |
231 | 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */ |
232 | 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */ | |
233 | 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */ | |
70256ad8 AJ |
234 | }; |
235 | ||
236 | /* Subsequent entries in a procedure linkage table look like this. */ | |
237 | ||
238 | static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] = | |
239 | { | |
653165cc | 240 | 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */ |
407443a3 | 241 | 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */ |
653165cc | 242 | 0x68, /* pushq immediate */ |
70256ad8 AJ |
243 | 0, 0, 0, 0, /* replaced with index into relocation table. */ |
244 | 0xe9, /* jmp relative */ | |
245 | 0, 0, 0, 0 /* replaced with offset to start of .plt0. */ | |
246 | }; | |
247 | ||
248 | /* The x86-64 linker needs to keep track of the number of relocs that | |
c434dee6 AJ |
249 | decides to copy as dynamic relocs in check_relocs for each symbol. |
250 | This is so that it can later discard them if they are found to be | |
251 | unnecessary. We store the information in a field extending the | |
252 | regular ELF linker hash table. */ | |
70256ad8 | 253 | |
c434dee6 | 254 | struct elf64_x86_64_dyn_relocs |
70256ad8 AJ |
255 | { |
256 | /* Next section. */ | |
c434dee6 AJ |
257 | struct elf64_x86_64_dyn_relocs *next; |
258 | ||
259 | /* The input section of the reloc. */ | |
260 | asection *sec; | |
261 | ||
262 | /* Total number of relocs copied for the input section. */ | |
70256ad8 | 263 | bfd_size_type count; |
c434dee6 AJ |
264 | |
265 | /* Number of pc-relative relocs copied for the input section. */ | |
266 | bfd_size_type pc_count; | |
70256ad8 AJ |
267 | }; |
268 | ||
269 | /* x86-64 ELF linker hash entry. */ | |
270 | ||
271 | struct elf64_x86_64_link_hash_entry | |
272 | { | |
c434dee6 | 273 | struct elf_link_hash_entry elf; |
70256ad8 | 274 | |
c434dee6 AJ |
275 | /* Track dynamic relocs copied for this symbol. */ |
276 | struct elf64_x86_64_dyn_relocs *dyn_relocs; | |
70256ad8 AJ |
277 | }; |
278 | ||
c434dee6 | 279 | /* x86-64 ELF linker hash table. */ |
8d88c4ca | 280 | |
407443a3 AJ |
281 | struct elf64_x86_64_link_hash_table |
282 | { | |
c434dee6 | 283 | struct elf_link_hash_table elf; |
70256ad8 | 284 | |
c434dee6 AJ |
285 | /* Short-cuts to get to dynamic linker sections. */ |
286 | asection *sgot; | |
287 | asection *sgotplt; | |
288 | asection *srelgot; | |
289 | asection *splt; | |
290 | asection *srelplt; | |
291 | asection *sdynbss; | |
292 | asection *srelbss; | |
70256ad8 | 293 | |
c434dee6 AJ |
294 | /* Small local sym to section mapping cache. */ |
295 | struct sym_sec_cache sym_sec; | |
296 | }; | |
70256ad8 AJ |
297 | |
298 | /* Get the x86-64 ELF linker hash table from a link_info structure. */ | |
8d88c4ca NC |
299 | |
300 | #define elf64_x86_64_hash_table(p) \ | |
301 | ((struct elf64_x86_64_link_hash_table *) ((p)->hash)) | |
302 | ||
407443a3 | 303 | /* Create an entry in an x86-64 ELF linker hash table. */ |
70256ad8 AJ |
304 | |
305 | static struct bfd_hash_entry * | |
c434dee6 | 306 | link_hash_newfunc (entry, table, string) |
70256ad8 AJ |
307 | struct bfd_hash_entry *entry; |
308 | struct bfd_hash_table *table; | |
309 | const char *string; | |
310 | { | |
70256ad8 | 311 | /* Allocate the structure if it has not already been allocated by a |
c434dee6 AJ |
312 | subclass. */ |
313 | if (entry == NULL) | |
314 | { | |
315 | entry = bfd_hash_allocate (table, | |
316 | sizeof (struct elf64_x86_64_link_hash_entry)); | |
317 | if (entry == NULL) | |
318 | return entry; | |
319 | } | |
70256ad8 AJ |
320 | |
321 | /* Call the allocation method of the superclass. */ | |
c434dee6 AJ |
322 | entry = _bfd_elf_link_hash_newfunc (entry, table, string); |
323 | if (entry != NULL) | |
70256ad8 | 324 | { |
c434dee6 AJ |
325 | struct elf64_x86_64_link_hash_entry *eh; |
326 | ||
327 | eh = (struct elf64_x86_64_link_hash_entry *) entry; | |
328 | eh->dyn_relocs = NULL; | |
70256ad8 AJ |
329 | } |
330 | ||
c434dee6 | 331 | return entry; |
70256ad8 AJ |
332 | } |
333 | ||
8d88c4ca NC |
334 | /* Create an X86-64 ELF linker hash table. */ |
335 | ||
336 | static struct bfd_link_hash_table * | |
337 | elf64_x86_64_link_hash_table_create (abfd) | |
338 | bfd *abfd; | |
339 | { | |
340 | struct elf64_x86_64_link_hash_table *ret; | |
dc810e39 | 341 | bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table); |
8d88c4ca | 342 | |
c434dee6 AJ |
343 | ret = (struct elf64_x86_64_link_hash_table *) bfd_alloc (abfd, amt); |
344 | if (ret == NULL) | |
8d88c4ca NC |
345 | return NULL; |
346 | ||
c434dee6 | 347 | if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc)) |
8d88c4ca NC |
348 | { |
349 | bfd_release (abfd, ret); | |
350 | return NULL; | |
351 | } | |
352 | ||
c434dee6 AJ |
353 | ret->sgot = NULL; |
354 | ret->sgotplt = NULL; | |
355 | ret->srelgot = NULL; | |
356 | ret->splt = NULL; | |
357 | ret->srelplt = NULL; | |
358 | ret->sdynbss = NULL; | |
359 | ret->srelbss = NULL; | |
360 | ret->sym_sec.abfd = NULL; | |
361 | ||
362 | return &ret->elf.root; | |
363 | } | |
364 | ||
365 | /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up | |
366 | shortcuts to them in our hash table. */ | |
367 | ||
368 | static boolean | |
369 | create_got_section (dynobj, info) | |
370 | bfd *dynobj; | |
371 | struct bfd_link_info *info; | |
372 | { | |
373 | struct elf64_x86_64_link_hash_table *htab; | |
374 | ||
375 | if (! _bfd_elf_create_got_section (dynobj, info)) | |
376 | return false; | |
377 | ||
378 | htab = elf64_x86_64_hash_table (info); | |
379 | htab->sgot = bfd_get_section_by_name (dynobj, ".got"); | |
380 | htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); | |
381 | if (!htab->sgot || !htab->sgotplt) | |
382 | abort (); | |
383 | ||
384 | htab->srelgot = bfd_make_section (dynobj, ".rela.got"); | |
385 | if (htab->srelgot == NULL | |
386 | || ! bfd_set_section_flags (dynobj, htab->srelgot, | |
387 | (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
388 | | SEC_IN_MEMORY | SEC_LINKER_CREATED | |
389 | | SEC_READONLY)) | |
390 | || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3)) | |
391 | return false; | |
392 | return true; | |
393 | } | |
394 | ||
395 | /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and | |
396 | .rela.bss sections in DYNOBJ, and set up shortcuts to them in our | |
397 | hash table. */ | |
398 | ||
399 | static boolean | |
400 | elf64_x86_64_create_dynamic_sections (dynobj, info) | |
401 | bfd *dynobj; | |
402 | struct bfd_link_info *info; | |
403 | { | |
404 | struct elf64_x86_64_link_hash_table *htab; | |
405 | ||
406 | htab = elf64_x86_64_hash_table (info); | |
407 | if (!htab->sgot && !create_got_section (dynobj, info)) | |
408 | return false; | |
409 | ||
410 | if (!_bfd_elf_create_dynamic_sections (dynobj, info)) | |
411 | return false; | |
412 | ||
413 | htab->splt = bfd_get_section_by_name (dynobj, ".plt"); | |
414 | htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
415 | htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss"); | |
416 | if (!info->shared) | |
417 | htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
418 | ||
419 | if (!htab->splt || !htab->srelplt || !htab->sdynbss | |
420 | || (!info->shared && !htab->srelbss)) | |
421 | abort (); | |
422 | ||
423 | return true; | |
424 | } | |
425 | ||
426 | /* Copy the extra info we tack onto an elf_link_hash_entry. */ | |
427 | ||
428 | static void | |
429 | elf64_x86_64_copy_indirect_symbol (dir, ind) | |
430 | struct elf_link_hash_entry *dir, *ind; | |
431 | { | |
432 | struct elf64_x86_64_link_hash_entry *edir, *eind; | |
433 | ||
434 | edir = (struct elf64_x86_64_link_hash_entry *) dir; | |
435 | eind = (struct elf64_x86_64_link_hash_entry *) ind; | |
436 | ||
437 | if (eind->dyn_relocs != NULL) | |
438 | { | |
439 | if (edir->dyn_relocs != NULL) | |
440 | { | |
441 | struct elf64_x86_64_dyn_relocs **pp; | |
442 | struct elf64_x86_64_dyn_relocs *p; | |
443 | ||
444 | if (ind->root.type == bfd_link_hash_indirect) | |
445 | abort (); | |
446 | ||
447 | /* Add reloc counts against the weak sym to the strong sym | |
448 | list. Merge any entries against the same section. */ | |
449 | for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) | |
450 | { | |
451 | struct elf64_x86_64_dyn_relocs *q; | |
452 | ||
453 | for (q = edir->dyn_relocs; q != NULL; q = q->next) | |
454 | if (q->sec == p->sec) | |
455 | { | |
456 | q->pc_count += p->pc_count; | |
457 | q->count += p->count; | |
458 | *pp = p->next; | |
459 | break; | |
460 | } | |
461 | if (q == NULL) | |
462 | pp = &p->next; | |
463 | } | |
464 | *pp = edir->dyn_relocs; | |
465 | } | |
466 | ||
467 | edir->dyn_relocs = eind->dyn_relocs; | |
468 | eind->dyn_relocs = NULL; | |
469 | } | |
470 | ||
471 | _bfd_elf_link_hash_copy_indirect (dir, ind); | |
8d88c4ca NC |
472 | } |
473 | ||
1e1b3bc2 | 474 | static boolean |
8d88c4ca NC |
475 | elf64_x86_64_elf_object_p (abfd) |
476 | bfd *abfd; | |
477 | { | |
478 | /* Set the right machine number for an x86-64 elf64 file. */ | |
479 | bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64); | |
480 | return true; | |
481 | } | |
482 | ||
70256ad8 | 483 | /* Look through the relocs for a section during the first phase, and |
c434dee6 AJ |
484 | calculate needed space in the global offset table, procedure |
485 | linkage table, and dynamic reloc sections. */ | |
70256ad8 AJ |
486 | |
487 | static boolean | |
488 | elf64_x86_64_check_relocs (abfd, info, sec, relocs) | |
489 | bfd *abfd; | |
490 | struct bfd_link_info *info; | |
491 | asection *sec; | |
492 | const Elf_Internal_Rela *relocs; | |
493 | { | |
c434dee6 | 494 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
495 | Elf_Internal_Shdr *symtab_hdr; |
496 | struct elf_link_hash_entry **sym_hashes; | |
70256ad8 AJ |
497 | const Elf_Internal_Rela *rel; |
498 | const Elf_Internal_Rela *rel_end; | |
70256ad8 AJ |
499 | asection *sreloc; |
500 | ||
501 | if (info->relocateable) | |
502 | return true; | |
503 | ||
c434dee6 | 504 | htab = elf64_x86_64_hash_table (info); |
70256ad8 AJ |
505 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
506 | sym_hashes = elf_sym_hashes (abfd); | |
70256ad8 | 507 | |
c434dee6 AJ |
508 | sreloc = NULL; |
509 | ||
70256ad8 AJ |
510 | rel_end = relocs + sec->reloc_count; |
511 | for (rel = relocs; rel < rel_end; rel++) | |
512 | { | |
513 | unsigned long r_symndx; | |
514 | struct elf_link_hash_entry *h; | |
515 | ||
516 | r_symndx = ELF64_R_SYM (rel->r_info); | |
c434dee6 AJ |
517 | |
518 | if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) | |
519 | { | |
520 | (*_bfd_error_handler) (_("%s: bad symbol index: %d"), | |
521 | bfd_archive_filename (abfd), | |
522 | r_symndx); | |
523 | return false; | |
524 | } | |
525 | ||
70256ad8 AJ |
526 | if (r_symndx < symtab_hdr->sh_info) |
527 | h = NULL; | |
528 | else | |
529 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
530 | ||
70256ad8 AJ |
531 | switch (ELF64_R_TYPE (rel->r_info)) |
532 | { | |
533 | case R_X86_64_GOT32: | |
c434dee6 | 534 | case R_X86_64_GOTPCREL: |
407443a3 | 535 | /* This symbol requires a global offset table entry. */ |
70256ad8 AJ |
536 | if (h != NULL) |
537 | { | |
51b64d56 | 538 | h->got.refcount += 1; |
70256ad8 AJ |
539 | } |
540 | else | |
541 | { | |
c434dee6 AJ |
542 | bfd_signed_vma *local_got_refcounts; |
543 | ||
70256ad8 | 544 | /* This is a global offset table entry for a local symbol. */ |
c434dee6 | 545 | local_got_refcounts = elf_local_got_refcounts (abfd); |
70256ad8 AJ |
546 | if (local_got_refcounts == NULL) |
547 | { | |
dc810e39 | 548 | bfd_size_type size; |
70256ad8 | 549 | |
dc810e39 AM |
550 | size = symtab_hdr->sh_info; |
551 | size *= sizeof (bfd_signed_vma); | |
70256ad8 | 552 | local_got_refcounts = ((bfd_signed_vma *) |
51b64d56 | 553 | bfd_zalloc (abfd, size)); |
70256ad8 AJ |
554 | if (local_got_refcounts == NULL) |
555 | return false; | |
556 | elf_local_got_refcounts (abfd) = local_got_refcounts; | |
70256ad8 | 557 | } |
51b64d56 | 558 | local_got_refcounts[r_symndx] += 1; |
70256ad8 | 559 | } |
c434dee6 AJ |
560 | /* Fall through */ |
561 | ||
562 | //case R_X86_64_GOTPCREL: | |
563 | if (htab->sgot == NULL) | |
564 | { | |
565 | if (htab->elf.dynobj == NULL) | |
566 | htab->elf.dynobj = abfd; | |
567 | if (!create_got_section (htab->elf.dynobj, info)) | |
568 | return false; | |
569 | } | |
70256ad8 AJ |
570 | break; |
571 | ||
572 | case R_X86_64_PLT32: | |
573 | /* This symbol requires a procedure linkage table entry. We | |
407443a3 AJ |
574 | actually build the entry in adjust_dynamic_symbol, |
575 | because this might be a case of linking PIC code which is | |
576 | never referenced by a dynamic object, in which case we | |
577 | don't need to generate a procedure linkage table entry | |
578 | after all. */ | |
70256ad8 AJ |
579 | |
580 | /* If this is a local symbol, we resolve it directly without | |
407443a3 | 581 | creating a procedure linkage table entry. */ |
70256ad8 AJ |
582 | if (h == NULL) |
583 | continue; | |
584 | ||
cc78d0af | 585 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; |
51b64d56 | 586 | h->plt.refcount += 1; |
70256ad8 AJ |
587 | break; |
588 | ||
cc78d0af AJ |
589 | case R_X86_64_8: |
590 | case R_X86_64_16: | |
70256ad8 | 591 | case R_X86_64_32: |
cc78d0af | 592 | case R_X86_64_64: |
70256ad8 | 593 | case R_X86_64_32S: |
c434dee6 AJ |
594 | case R_X86_64_PC8: |
595 | case R_X86_64_PC16: | |
70256ad8 | 596 | case R_X86_64_PC32: |
c434dee6 AJ |
597 | if (h != NULL && !info->shared) |
598 | { | |
599 | /* If this reloc is in a read-only section, we might | |
600 | need a copy reloc. We can't check reliably at this | |
601 | stage whether the section is read-only, as input | |
602 | sections have not yet been mapped to output sections. | |
603 | Tentatively set the flag for now, and correct in | |
604 | adjust_dynamic_symbol. */ | |
605 | h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF; | |
606 | ||
607 | /* We may need a .plt entry if the function this reloc | |
608 | refers to is in a shared lib. */ | |
609 | h->plt.refcount += 1; | |
610 | } | |
70256ad8 AJ |
611 | |
612 | /* If we are creating a shared library, and this is a reloc | |
613 | against a global symbol, or a non PC relative reloc | |
614 | against a local symbol, then we need to copy the reloc | |
615 | into the shared library. However, if we are linking with | |
616 | -Bsymbolic, we do not need to copy a reloc against a | |
617 | global symbol which is defined in an object we are | |
407443a3 | 618 | including in the link (i.e., DEF_REGULAR is set). At |
70256ad8 AJ |
619 | this point we have not seen all the input files, so it is |
620 | possible that DEF_REGULAR is not set now but will be set | |
c434dee6 AJ |
621 | later (it is never cleared). In case of a weak definition, |
622 | DEF_REGULAR may be cleared later by a strong definition in | |
623 | a shared library. We account for that possibility below by | |
624 | storing information in the relocs_copied field of the hash | |
625 | table entry. A similar situation occurs when creating | |
626 | shared libraries and symbol visibility changes render the | |
627 | symbol local. | |
628 | ||
629 | If on the other hand, we are creating an executable, we | |
630 | may need to keep relocations for symbols satisfied by a | |
631 | dynamic library if we manage to avoid copy relocs for the | |
632 | symbol. */ | |
633 | if ((info->shared | |
634 | && (sec->flags & SEC_ALLOC) != 0 | |
635 | && (((ELF64_R_TYPE (rel->r_info) != R_X86_64_PC8) | |
636 | && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC16) | |
637 | && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32)) | |
638 | || (h != NULL | |
639 | && (! info->symbolic | |
640 | || h->root.type == bfd_link_hash_defweak | |
641 | || (h->elf_link_hash_flags | |
642 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
643 | || (!info->shared | |
644 | && (sec->flags & SEC_ALLOC) != 0 | |
645 | && h != NULL | |
646 | && (h->root.type == bfd_link_hash_defweak | |
647 | || (h->elf_link_hash_flags | |
648 | & ELF_LINK_HASH_DEF_REGULAR) == 0))) | |
70256ad8 | 649 | { |
c434dee6 AJ |
650 | struct elf64_x86_64_dyn_relocs *p; |
651 | struct elf64_x86_64_dyn_relocs **head; | |
652 | ||
653 | /* We must copy these reloc types into the output file. | |
654 | Create a reloc section in dynobj and make room for | |
655 | this reloc. */ | |
70256ad8 AJ |
656 | if (sreloc == NULL) |
657 | { | |
658 | const char *name; | |
c434dee6 | 659 | bfd *dynobj; |
70256ad8 AJ |
660 | |
661 | name = (bfd_elf_string_from_elf_section | |
662 | (abfd, | |
663 | elf_elfheader (abfd)->e_shstrndx, | |
664 | elf_section_data (sec)->rel_hdr.sh_name)); | |
665 | if (name == NULL) | |
666 | return false; | |
667 | ||
c434dee6 AJ |
668 | if (strncmp (name, ".rela", 5) != 0 |
669 | || strcmp (bfd_get_section_name (abfd, sec), | |
670 | name + 5) != 0) | |
671 | { | |
672 | (*_bfd_error_handler) | |
673 | (_("%s: bad relocation section name `%s\'"), | |
674 | bfd_archive_filename (abfd), name); | |
675 | } | |
676 | ||
677 | if (htab->elf.dynobj == NULL) | |
678 | htab->elf.dynobj = abfd; | |
679 | ||
680 | dynobj = htab->elf.dynobj; | |
70256ad8 AJ |
681 | |
682 | sreloc = bfd_get_section_by_name (dynobj, name); | |
683 | if (sreloc == NULL) | |
684 | { | |
685 | flagword flags; | |
686 | ||
687 | sreloc = bfd_make_section (dynobj, name); | |
688 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | |
689 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
690 | if ((sec->flags & SEC_ALLOC) != 0) | |
691 | flags |= SEC_ALLOC | SEC_LOAD; | |
692 | if (sreloc == NULL | |
693 | || ! bfd_set_section_flags (dynobj, sreloc, flags) | |
cc78d0af | 694 | || ! bfd_set_section_alignment (dynobj, sreloc, 3)) |
70256ad8 AJ |
695 | return false; |
696 | } | |
c434dee6 | 697 | elf_section_data (sec)->sreloc = sreloc; |
70256ad8 AJ |
698 | } |
699 | ||
c434dee6 AJ |
700 | /* If this is a global symbol, we count the number of |
701 | relocations we need for this symbol. */ | |
702 | if (h != NULL) | |
70256ad8 | 703 | { |
c434dee6 AJ |
704 | head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs; |
705 | } | |
706 | else | |
707 | { | |
708 | /* Track dynamic relocs needed for local syms too. | |
709 | We really need local syms available to do this | |
710 | easily. Oh well. */ | |
711 | ||
712 | asection *s; | |
713 | s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, | |
714 | sec, r_symndx); | |
715 | if (s == NULL) | |
716 | return false; | |
70256ad8 | 717 | |
c434dee6 AJ |
718 | head = ((struct elf64_x86_64_dyn_relocs **) |
719 | &elf_section_data (s)->local_dynrel); | |
720 | } | |
70256ad8 | 721 | |
c434dee6 AJ |
722 | p = *head; |
723 | if (p == NULL || p->sec != sec) | |
724 | { | |
725 | bfd_size_type amt = sizeof *p; | |
726 | p = ((struct elf64_x86_64_dyn_relocs *) | |
727 | bfd_alloc (htab->elf.dynobj, amt)); | |
70256ad8 | 728 | if (p == NULL) |
c434dee6 AJ |
729 | return false; |
730 | p->next = *head; | |
731 | *head = p; | |
732 | p->sec = sec; | |
733 | p->count = 0; | |
734 | p->pc_count = 0; | |
70256ad8 | 735 | } |
c434dee6 AJ |
736 | |
737 | p->count += 1; | |
738 | if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8 | |
739 | || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16 | |
740 | || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32) | |
741 | p->pc_count += 1; | |
70256ad8 AJ |
742 | } |
743 | break; | |
fe4770f4 AJ |
744 | |
745 | /* This relocation describes the C++ object vtable hierarchy. | |
746 | Reconstruct it for later use during GC. */ | |
747 | case R_X86_64_GNU_VTINHERIT: | |
748 | if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
749 | return false; | |
750 | break; | |
751 | ||
752 | /* This relocation describes which C++ vtable entries are actually | |
753 | used. Record for later use during GC. */ | |
754 | case R_X86_64_GNU_VTENTRY: | |
cc78d0af | 755 | if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend)) |
fe4770f4 AJ |
756 | return false; |
757 | break; | |
c434dee6 AJ |
758 | |
759 | default: | |
760 | break; | |
70256ad8 AJ |
761 | } |
762 | } | |
763 | ||
764 | return true; | |
765 | } | |
766 | ||
767 | /* Return the section that should be marked against GC for a given | |
407443a3 | 768 | relocation. */ |
70256ad8 AJ |
769 | |
770 | static asection * | |
771 | elf64_x86_64_gc_mark_hook (abfd, info, rel, h, sym) | |
772 | bfd *abfd; | |
773 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
c434dee6 | 774 | Elf_Internal_Rela *rel; |
70256ad8 AJ |
775 | struct elf_link_hash_entry *h; |
776 | Elf_Internal_Sym *sym; | |
777 | { | |
778 | if (h != NULL) | |
779 | { | |
fe4770f4 | 780 | switch (ELF64_R_TYPE (rel->r_info)) |
70256ad8 | 781 | { |
fe4770f4 AJ |
782 | case R_X86_64_GNU_VTINHERIT: |
783 | case R_X86_64_GNU_VTENTRY: | |
784 | break; | |
70256ad8 AJ |
785 | |
786 | default: | |
fe4770f4 AJ |
787 | switch (h->root.type) |
788 | { | |
789 | case bfd_link_hash_defined: | |
790 | case bfd_link_hash_defweak: | |
791 | return h->root.u.def.section; | |
792 | ||
793 | case bfd_link_hash_common: | |
794 | return h->root.u.c.p->section; | |
795 | ||
796 | default: | |
797 | break; | |
798 | } | |
70256ad8 AJ |
799 | } |
800 | } | |
801 | else | |
802 | { | |
9ad5cbcf | 803 | return bfd_section_from_elf_index (abfd, sym->st_shndx); |
70256ad8 AJ |
804 | } |
805 | ||
806 | return NULL; | |
807 | } | |
808 | ||
407443a3 | 809 | /* Update the got entry reference counts for the section being removed. */ |
70256ad8 AJ |
810 | |
811 | static boolean | |
812 | elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs) | |
813 | bfd *abfd; | |
c434dee6 | 814 | struct bfd_link_info *info; |
70256ad8 AJ |
815 | asection *sec; |
816 | const Elf_Internal_Rela *relocs; | |
817 | { | |
818 | Elf_Internal_Shdr *symtab_hdr; | |
819 | struct elf_link_hash_entry **sym_hashes; | |
820 | bfd_signed_vma *local_got_refcounts; | |
821 | const Elf_Internal_Rela *rel, *relend; | |
822 | unsigned long r_symndx; | |
823 | struct elf_link_hash_entry *h; | |
c434dee6 AJ |
824 | |
825 | elf_section_data (sec)->local_dynrel = NULL; | |
70256ad8 AJ |
826 | |
827 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
828 | sym_hashes = elf_sym_hashes (abfd); | |
829 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
830 | ||
70256ad8 AJ |
831 | relend = relocs + sec->reloc_count; |
832 | for (rel = relocs; rel < relend; rel++) | |
833 | switch (ELF64_R_TYPE (rel->r_info)) | |
834 | { | |
835 | case R_X86_64_GOT32: | |
836 | case R_X86_64_GOTPCREL: | |
837 | r_symndx = ELF64_R_SYM (rel->r_info); | |
838 | if (r_symndx >= symtab_hdr->sh_info) | |
839 | { | |
840 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
841 | if (h->got.refcount > 0) | |
c434dee6 | 842 | h->got.refcount -= 1; |
70256ad8 AJ |
843 | } |
844 | else if (local_got_refcounts != NULL) | |
845 | { | |
846 | if (local_got_refcounts[r_symndx] > 0) | |
c434dee6 | 847 | local_got_refcounts[r_symndx] -= 1; |
70256ad8 AJ |
848 | } |
849 | break; | |
850 | ||
c434dee6 AJ |
851 | case R_X86_64_8: |
852 | case R_X86_64_16: | |
853 | case R_X86_64_32: | |
854 | case R_X86_64_64: | |
855 | case R_X86_64_32S: | |
856 | case R_X86_64_PC8: | |
857 | case R_X86_64_PC16: | |
858 | case R_X86_64_PC32: | |
859 | r_symndx = ELF64_R_SYM (rel->r_info); | |
860 | if (r_symndx >= symtab_hdr->sh_info) | |
861 | { | |
862 | struct elf64_x86_64_link_hash_entry *eh; | |
863 | struct elf64_x86_64_dyn_relocs **pp; | |
864 | struct elf64_x86_64_dyn_relocs *p; | |
865 | ||
866 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
867 | ||
868 | if (!info->shared && h->plt.refcount > 0) | |
869 | h->plt.refcount -= 1; | |
870 | ||
871 | eh = (struct elf64_x86_64_link_hash_entry *) h; | |
872 | ||
873 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) | |
874 | if (p->sec == sec) | |
875 | { | |
876 | if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8 | |
877 | || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16 | |
878 | || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32) | |
879 | p->pc_count -= 1; | |
880 | p->count -= 1; | |
881 | if (p->count == 0) | |
882 | *pp = p->next; | |
883 | break; | |
884 | } | |
885 | } | |
886 | break; | |
887 | ||
888 | ||
70256ad8 AJ |
889 | case R_X86_64_PLT32: |
890 | r_symndx = ELF64_R_SYM (rel->r_info); | |
891 | if (r_symndx >= symtab_hdr->sh_info) | |
892 | { | |
893 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
894 | if (h->plt.refcount > 0) | |
895 | h->plt.refcount -= 1; | |
896 | } | |
897 | break; | |
898 | ||
899 | default: | |
900 | break; | |
901 | } | |
902 | ||
903 | return true; | |
904 | } | |
905 | ||
906 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
907 | regular object. The current definition is in some section of the | |
908 | dynamic object, but we're not including those sections. We have to | |
909 | change the definition to something the rest of the link can | |
407443a3 | 910 | understand. */ |
70256ad8 AJ |
911 | |
912 | static boolean | |
913 | elf64_x86_64_adjust_dynamic_symbol (info, h) | |
914 | struct bfd_link_info *info; | |
915 | struct elf_link_hash_entry *h; | |
916 | { | |
c434dee6 AJ |
917 | struct elf64_x86_64_link_hash_table *htab; |
918 | struct elf64_x86_64_link_hash_entry * eh; | |
919 | struct elf64_x86_64_dyn_relocs *p; | |
70256ad8 AJ |
920 | asection *s; |
921 | unsigned int power_of_two; | |
922 | ||
70256ad8 AJ |
923 | /* If this is a function, put it in the procedure linkage table. We |
924 | will fill in the contents of the procedure linkage table later, | |
925 | when we know the address of the .got section. */ | |
926 | if (h->type == STT_FUNC | |
927 | || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) | |
928 | { | |
c434dee6 AJ |
929 | if (h->plt.refcount <= 0 |
930 | || (! info->shared | |
931 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
932 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0 | |
933 | && h->root.type != bfd_link_hash_undefweak | |
934 | && h->root.type != bfd_link_hash_undefined)) | |
70256ad8 | 935 | { |
70256ad8 AJ |
936 | /* This case can occur if we saw a PLT32 reloc in an input |
937 | file, but the symbol was never referred to by a dynamic | |
938 | object, or if all references were garbage collected. In | |
939 | such a case, we don't actually need to build a procedure | |
940 | linkage table, and we can just do a PC32 reloc instead. */ | |
70256ad8 AJ |
941 | h->plt.offset = (bfd_vma) -1; |
942 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
70256ad8 AJ |
943 | } |
944 | ||
70256ad8 AJ |
945 | return true; |
946 | } | |
bbd7ec4a | 947 | else |
c434dee6 AJ |
948 | /* It's possible that we incorrectly decided a .plt reloc was |
949 | needed for an R_X86_64_PC32 reloc to a non-function sym in | |
950 | check_relocs. We can't decide accurately between function and | |
951 | non-function syms in check-relocs; Objects loaded later in | |
952 | the link may change h->type. So fix it now. */ | |
bbd7ec4a | 953 | h->plt.offset = (bfd_vma) -1; |
70256ad8 AJ |
954 | |
955 | /* If this is a weak symbol, and there is a real definition, the | |
956 | processor independent code will have arranged for us to see the | |
407443a3 | 957 | real definition first, and we can just use the same value. */ |
70256ad8 AJ |
958 | if (h->weakdef != NULL) |
959 | { | |
960 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined | |
961 | || h->weakdef->root.type == bfd_link_hash_defweak); | |
962 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
963 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
964 | return true; | |
965 | } | |
966 | ||
967 | /* This is a reference to a symbol defined by a dynamic object which | |
407443a3 | 968 | is not a function. */ |
70256ad8 AJ |
969 | |
970 | /* If we are creating a shared library, we must presume that the | |
971 | only references to the symbol are via the global offset table. | |
972 | For such cases we need not do anything here; the relocations will | |
407443a3 | 973 | be handled correctly by relocate_section. */ |
70256ad8 AJ |
974 | if (info->shared) |
975 | return true; | |
976 | ||
977 | /* If there are no references to this symbol that do not use the | |
978 | GOT, we don't need to generate a copy reloc. */ | |
979 | if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0) | |
980 | return true; | |
981 | ||
c434dee6 AJ |
982 | /* If -z nocopyreloc was given, we won't generate them either. */ |
983 | if (info->nocopyreloc) | |
984 | { | |
985 | h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF; | |
986 | return true; | |
987 | } | |
988 | ||
989 | eh = (struct elf64_x86_64_link_hash_entry *) h; | |
990 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
991 | { | |
992 | s = p->sec->output_section; | |
993 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
994 | break; | |
995 | } | |
996 | ||
997 | /* If we didn't find any dynamic relocs in read-only sections, then | |
998 | we'll be keeping the dynamic relocs and avoiding the copy reloc. */ | |
999 | if (p == NULL) | |
1000 | { | |
1001 | h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF; | |
1002 | return true; | |
1003 | } | |
1004 | ||
70256ad8 | 1005 | /* We must allocate the symbol in our .dynbss section, which will |
407443a3 | 1006 | become part of the .bss section of the executable. There will be |
70256ad8 AJ |
1007 | an entry for this symbol in the .dynsym section. The dynamic |
1008 | object will contain position independent code, so all references | |
1009 | from the dynamic object to this symbol will go through the global | |
1010 | offset table. The dynamic linker will use the .dynsym entry to | |
1011 | determine the address it must put in the global offset table, so | |
1012 | both the dynamic object and the regular object will refer to the | |
1013 | same memory location for the variable. */ | |
1014 | ||
c434dee6 | 1015 | htab = elf64_x86_64_hash_table (info); |
70256ad8 AJ |
1016 | |
1017 | /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker | |
1018 | to copy the initial value out of the dynamic object and into the | |
c434dee6 | 1019 | runtime process image. */ |
70256ad8 AJ |
1020 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) |
1021 | { | |
c434dee6 | 1022 | htab->srelbss->_raw_size += sizeof (Elf64_External_Rela); |
70256ad8 AJ |
1023 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; |
1024 | } | |
1025 | ||
1026 | /* We need to figure out the alignment required for this symbol. I | |
407443a3 | 1027 | have no idea how ELF linkers handle this. 16-bytes is the size |
70256ad8 AJ |
1028 | of the largest type that requires hard alignment -- long double. */ |
1029 | /* FIXME: This is VERY ugly. Should be fixed for all architectures using | |
1030 | this construct. */ | |
1031 | power_of_two = bfd_log2 (h->size); | |
1032 | if (power_of_two > 4) | |
1033 | power_of_two = 4; | |
1034 | ||
1035 | /* Apply the required alignment. */ | |
c434dee6 | 1036 | s = htab->sdynbss; |
70256ad8 | 1037 | s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two)); |
c434dee6 | 1038 | if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s)) |
70256ad8 | 1039 | { |
c434dee6 | 1040 | if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two)) |
70256ad8 AJ |
1041 | return false; |
1042 | } | |
1043 | ||
1044 | /* Define the symbol as being at this point in the section. */ | |
1045 | h->root.u.def.section = s; | |
1046 | h->root.u.def.value = s->_raw_size; | |
1047 | ||
1048 | /* Increment the section size to make room for the symbol. */ | |
1049 | s->_raw_size += h->size; | |
1050 | ||
1051 | return true; | |
1052 | } | |
1053 | ||
c434dee6 AJ |
1054 | /* This is the condition under which elf64_x86_64_finish_dynamic_symbol |
1055 | will be called from elflink.h. If elflink.h doesn't call our | |
1056 | finish_dynamic_symbol routine, we'll need to do something about | |
1057 | initializing any .plt and .got entries in elf64_x86_64_relocate_section. */ | |
1058 | #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \ | |
1059 | ((DYN) \ | |
1060 | && ((INFO)->shared \ | |
1061 | || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \ | |
1062 | && ((H)->dynindx != -1 \ | |
1063 | || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)) | |
1064 | ||
1065 | /* Allocate space in .plt, .got and associated reloc sections for | |
1066 | dynamic relocs. */ | |
1067 | ||
1068 | static boolean | |
1069 | allocate_dynrelocs (h, inf) | |
1070 | struct elf_link_hash_entry *h; | |
1071 | PTR inf; | |
1072 | { | |
1073 | struct bfd_link_info *info; | |
1074 | struct elf64_x86_64_link_hash_table *htab; | |
1075 | struct elf64_x86_64_link_hash_entry *eh; | |
1076 | struct elf64_x86_64_dyn_relocs *p; | |
1077 | ||
1078 | if (h->root.type == bfd_link_hash_indirect | |
1079 | || h->root.type == bfd_link_hash_warning) | |
1080 | return true; | |
1081 | ||
1082 | info = (struct bfd_link_info *) inf; | |
1083 | htab = elf64_x86_64_hash_table (info); | |
1084 | ||
1085 | if (htab->elf.dynamic_sections_created | |
1086 | && h->plt.refcount > 0) | |
1087 | { | |
1088 | /* Make sure this symbol is output as a dynamic symbol. | |
1089 | Undefined weak syms won't yet be marked as dynamic. */ | |
1090 | if (h->dynindx == -1 | |
1091 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1092 | { | |
1093 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
1094 | return false; | |
1095 | } | |
1096 | ||
1097 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h)) | |
1098 | { | |
1099 | asection *s = htab->splt; | |
1100 | ||
1101 | /* If this is the first .plt entry, make room for the special | |
1102 | first entry. */ | |
1103 | if (s->_raw_size == 0) | |
1104 | s->_raw_size += PLT_ENTRY_SIZE; | |
1105 | ||
1106 | h->plt.offset = s->_raw_size; | |
1107 | ||
1108 | /* If this symbol is not defined in a regular file, and we are | |
1109 | not generating a shared library, then set the symbol to this | |
1110 | location in the .plt. This is required to make function | |
1111 | pointers compare as equal between the normal executable and | |
1112 | the shared library. */ | |
1113 | if (! info->shared | |
1114 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1115 | { | |
1116 | h->root.u.def.section = s; | |
1117 | h->root.u.def.value = h->plt.offset; | |
1118 | } | |
1119 | ||
1120 | /* Make room for this entry. */ | |
1121 | s->_raw_size += PLT_ENTRY_SIZE; | |
1122 | ||
1123 | /* We also need to make an entry in the .got.plt section, which | |
1124 | will be placed in the .got section by the linker script. */ | |
1125 | htab->sgotplt->_raw_size += GOT_ENTRY_SIZE; | |
1126 | ||
1127 | /* We also need to make an entry in the .rela.plt section. */ | |
1128 | htab->srelplt->_raw_size += sizeof (Elf64_External_Rela); | |
1129 | } | |
1130 | else | |
1131 | { | |
1132 | h->plt.offset = (bfd_vma) -1; | |
1133 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
1134 | } | |
1135 | } | |
1136 | else | |
1137 | { | |
1138 | h->plt.offset = (bfd_vma) -1; | |
1139 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
1140 | } | |
1141 | ||
1142 | if (h->got.refcount > 0) | |
1143 | { | |
1144 | asection *s; | |
1145 | boolean dyn; | |
1146 | ||
1147 | /* Make sure this symbol is output as a dynamic symbol. | |
1148 | Undefined weak syms won't yet be marked as dynamic. */ | |
1149 | if (h->dynindx == -1 | |
1150 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1151 | { | |
1152 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
1153 | return false; | |
1154 | } | |
1155 | ||
1156 | s = htab->sgot; | |
1157 | h->got.offset = s->_raw_size; | |
1158 | s->_raw_size += GOT_ENTRY_SIZE; | |
1159 | dyn = htab->elf.dynamic_sections_created; | |
1160 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)) | |
1161 | htab->srelgot->_raw_size += sizeof (Elf64_External_Rela); | |
1162 | } | |
1163 | else | |
1164 | h->got.offset = (bfd_vma) -1; | |
1165 | ||
1166 | eh = (struct elf64_x86_64_link_hash_entry *) h; | |
1167 | if (eh->dyn_relocs == NULL) | |
1168 | return true; | |
1169 | ||
1170 | /* In the shared -Bsymbolic case, discard space allocated for | |
1171 | dynamic pc-relative relocs against symbols which turn out to be | |
1172 | defined in regular objects. For the normal shared case, discard | |
1173 | space for pc-relative relocs that have become local due to symbol | |
1174 | visibility changes. */ | |
1175 | ||
1176 | if (info->shared) | |
1177 | { | |
1178 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
1179 | && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0 | |
1180 | || info->symbolic)) | |
1181 | { | |
1182 | struct elf64_x86_64_dyn_relocs **pp; | |
1183 | ||
1184 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
1185 | { | |
1186 | p->count -= p->pc_count; | |
1187 | p->pc_count = 0; | |
1188 | if (p->count == 0) | |
1189 | *pp = p->next; | |
1190 | else | |
1191 | pp = &p->next; | |
1192 | } | |
1193 | } | |
1194 | } | |
1195 | else | |
1196 | { | |
1197 | /* For the non-shared case, discard space for relocs against | |
1198 | symbols which turn out to need copy relocs or are not | |
1199 | dynamic. */ | |
1200 | ||
1201 | if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0 | |
1202 | && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1203 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1204 | || (htab->elf.dynamic_sections_created | |
1205 | && (h->root.type == bfd_link_hash_undefweak | |
1206 | || h->root.type == bfd_link_hash_undefined)))) | |
1207 | { | |
1208 | /* Make sure this symbol is output as a dynamic symbol. | |
1209 | Undefined weak syms won't yet be marked as dynamic. */ | |
1210 | if (h->dynindx == -1 | |
1211 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1212 | { | |
1213 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
1214 | return false; | |
1215 | } | |
1216 | ||
1217 | /* If that succeeded, we know we'll be keeping all the | |
1218 | relocs. */ | |
1219 | if (h->dynindx != -1) | |
1220 | goto keep; | |
1221 | } | |
1222 | ||
1223 | eh->dyn_relocs = NULL; | |
1224 | ||
1225 | keep: ; | |
1226 | } | |
1227 | ||
1228 | /* Finally, allocate space. */ | |
1229 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1230 | { | |
1231 | asection *sreloc = elf_section_data (p->sec)->sreloc; | |
1232 | sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela); | |
1233 | } | |
1234 | ||
1235 | return true; | |
1236 | } | |
1237 | ||
1238 | /* Find any dynamic relocs that apply to read-only sections. */ | |
1239 | ||
1240 | static boolean | |
1241 | readonly_dynrelocs (h, inf) | |
1242 | struct elf_link_hash_entry *h; | |
1243 | PTR inf; | |
1244 | { | |
1245 | struct elf64_x86_64_link_hash_entry *eh; | |
1246 | struct elf64_x86_64_dyn_relocs *p; | |
1247 | ||
1248 | eh = (struct elf64_x86_64_link_hash_entry *) h; | |
1249 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1250 | { | |
1251 | asection *s = p->sec->output_section; | |
1252 | ||
1253 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
1254 | { | |
1255 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
1256 | ||
1257 | info->flags |= DF_TEXTREL; | |
1258 | ||
1259 | /* Not an error, just cut short the traversal. */ | |
1260 | return false; | |
1261 | } | |
1262 | } | |
1263 | return true; | |
1264 | } | |
1265 | ||
70256ad8 AJ |
1266 | /* Set the sizes of the dynamic sections. */ |
1267 | ||
1268 | static boolean | |
1269 | elf64_x86_64_size_dynamic_sections (output_bfd, info) | |
8df9fc9d | 1270 | bfd *output_bfd ATTRIBUTE_UNUSED; |
70256ad8 AJ |
1271 | struct bfd_link_info *info; |
1272 | { | |
c434dee6 | 1273 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
1274 | bfd *dynobj; |
1275 | asection *s; | |
70256ad8 | 1276 | boolean relocs; |
c434dee6 | 1277 | bfd *ibfd; |
70256ad8 | 1278 | |
c434dee6 AJ |
1279 | htab = elf64_x86_64_hash_table (info); |
1280 | dynobj = htab->elf.dynobj; | |
1281 | if (dynobj == NULL) | |
1282 | abort (); | |
70256ad8 | 1283 | |
c434dee6 | 1284 | if (htab->elf.dynamic_sections_created) |
70256ad8 AJ |
1285 | { |
1286 | /* Set the contents of the .interp section to the interpreter. */ | |
1287 | if (! info->shared) | |
1288 | { | |
1289 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
c434dee6 AJ |
1290 | if (s == NULL) |
1291 | abort (); | |
70256ad8 AJ |
1292 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; |
1293 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
1294 | } | |
1295 | } | |
70256ad8 | 1296 | |
c434dee6 AJ |
1297 | /* Set up .got offsets for local syms, and space for local dynamic |
1298 | relocs. */ | |
1299 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
70256ad8 | 1300 | { |
c434dee6 AJ |
1301 | bfd_signed_vma *local_got; |
1302 | bfd_signed_vma *end_local_got; | |
1303 | bfd_size_type locsymcount; | |
1304 | Elf_Internal_Shdr *symtab_hdr; | |
1305 | asection *srel; | |
70256ad8 | 1306 | |
c434dee6 | 1307 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
70256ad8 AJ |
1308 | continue; |
1309 | ||
c434dee6 | 1310 | for (s = ibfd->sections; s != NULL; s = s->next) |
70256ad8 | 1311 | { |
c434dee6 AJ |
1312 | struct elf64_x86_64_dyn_relocs *p; |
1313 | ||
1314 | for (p = *((struct elf64_x86_64_dyn_relocs **) | |
1315 | &elf_section_data (s)->local_dynrel); | |
1316 | p != NULL; | |
1317 | p = p->next) | |
70256ad8 | 1318 | { |
c434dee6 AJ |
1319 | if (!bfd_is_abs_section (p->sec) |
1320 | && bfd_is_abs_section (p->sec->output_section)) | |
1321 | { | |
1322 | /* Input section has been discarded, either because | |
1323 | it is a copy of a linkonce section or due to | |
1324 | linker script /DISCARD/, so we'll be discarding | |
1325 | the relocs too. */ | |
1326 | } | |
1327 | else if (p->count != 0) | |
1328 | { | |
1329 | srel = elf_section_data (p->sec)->sreloc; | |
1330 | srel->_raw_size += p->count * sizeof (Elf64_External_Rela); | |
1331 | if ((p->sec->output_section->flags & SEC_READONLY) != 0) | |
1332 | info->flags |= DF_TEXTREL; | |
1333 | ||
1334 | } | |
70256ad8 AJ |
1335 | } |
1336 | } | |
c434dee6 AJ |
1337 | |
1338 | local_got = elf_local_got_refcounts (ibfd); | |
1339 | if (!local_got) | |
1340 | continue; | |
1341 | ||
1342 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1343 | locsymcount = symtab_hdr->sh_info; | |
1344 | end_local_got = local_got + locsymcount; | |
1345 | s = htab->sgot; | |
1346 | srel = htab->srelgot; | |
1347 | for (; local_got < end_local_got; ++local_got) | |
70256ad8 | 1348 | { |
c434dee6 | 1349 | if (*local_got > 0) |
70256ad8 | 1350 | { |
c434dee6 AJ |
1351 | *local_got = s->_raw_size; |
1352 | s->_raw_size += GOT_ENTRY_SIZE; | |
1353 | if (info->shared) | |
1354 | srel->_raw_size += sizeof (Elf64_External_Rela); | |
70256ad8 AJ |
1355 | } |
1356 | else | |
c434dee6 AJ |
1357 | *local_got = (bfd_vma) -1; |
1358 | } | |
1359 | } | |
70256ad8 | 1360 | |
c434dee6 AJ |
1361 | /* Allocate global sym .plt and .got entries, and space for global |
1362 | sym dynamic relocs. */ | |
1363 | elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); | |
1364 | ||
1365 | /* We now have determined the sizes of the various dynamic sections. | |
1366 | Allocate memory for them. */ | |
1367 | relocs = false; | |
1368 | for (s = dynobj->sections; s != NULL; s = s->next) | |
1369 | { | |
1370 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
1371 | continue; | |
1372 | ||
1373 | if (s == htab->splt | |
1374 | || s == htab->sgot | |
1375 | || s == htab->sgotplt) | |
1376 | { | |
1377 | /* Strip this section if we don't need it; see the | |
1378 | comment below. */ | |
1379 | } | |
1380 | else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0) | |
1381 | { | |
1382 | if (s->_raw_size != 0 && s != htab->srelplt) | |
1383 | relocs = true; | |
1384 | ||
1385 | /* We use the reloc_count field as a counter if we need | |
1386 | to copy relocs into the output file. */ | |
1387 | s->reloc_count = 0; | |
70256ad8 | 1388 | } |
c434dee6 | 1389 | else |
70256ad8 AJ |
1390 | { |
1391 | /* It's not one of our sections, so don't allocate space. */ | |
1392 | continue; | |
1393 | } | |
1394 | ||
c434dee6 | 1395 | if (s->_raw_size == 0) |
70256ad8 | 1396 | { |
c434dee6 AJ |
1397 | /* If we don't need this section, strip it from the |
1398 | output file. This is mostly to handle .rela.bss and | |
1399 | .rela.plt. We must create both sections in | |
1400 | create_dynamic_sections, because they must be created | |
1401 | before the linker maps input sections to output | |
1402 | sections. The linker does that before | |
1403 | adjust_dynamic_symbol is called, and it is that | |
1404 | function which decides whether anything needs to go | |
1405 | into these sections. */ | |
1406 | ||
70256ad8 AJ |
1407 | _bfd_strip_section_from_output (info, s); |
1408 | continue; | |
1409 | } | |
1410 | ||
1411 | /* Allocate memory for the section contents. We use bfd_zalloc | |
1412 | here in case unused entries are not reclaimed before the | |
1413 | section's contents are written out. This should not happen, | |
1414 | but this way if it does, we get a R_X86_64_NONE reloc instead | |
1415 | of garbage. */ | |
1416 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size); | |
c434dee6 | 1417 | if (s->contents == NULL) |
70256ad8 AJ |
1418 | return false; |
1419 | } | |
1420 | ||
c434dee6 | 1421 | if (htab->elf.dynamic_sections_created) |
70256ad8 AJ |
1422 | { |
1423 | /* Add some entries to the .dynamic section. We fill in the | |
1424 | values later, in elf64_x86_64_finish_dynamic_sections, but we | |
1425 | must add the entries now so that we get the correct size for | |
407443a3 | 1426 | the .dynamic section. The DT_DEBUG entry is filled in by the |
70256ad8 | 1427 | dynamic linker and used by the debugger. */ |
dc810e39 AM |
1428 | #define add_dynamic_entry(TAG, VAL) \ |
1429 | bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL)) | |
1430 | ||
70256ad8 AJ |
1431 | if (! info->shared) |
1432 | { | |
dc810e39 | 1433 | if (!add_dynamic_entry (DT_DEBUG, 0)) |
70256ad8 AJ |
1434 | return false; |
1435 | } | |
1436 | ||
c434dee6 | 1437 | if (htab->splt->_raw_size != 0) |
70256ad8 | 1438 | { |
dc810e39 AM |
1439 | if (!add_dynamic_entry (DT_PLTGOT, 0) |
1440 | || !add_dynamic_entry (DT_PLTRELSZ, 0) | |
1441 | || !add_dynamic_entry (DT_PLTREL, DT_RELA) | |
1442 | || !add_dynamic_entry (DT_JMPREL, 0)) | |
70256ad8 AJ |
1443 | return false; |
1444 | } | |
1445 | ||
1446 | if (relocs) | |
1447 | { | |
dc810e39 AM |
1448 | if (!add_dynamic_entry (DT_RELA, 0) |
1449 | || !add_dynamic_entry (DT_RELASZ, 0) | |
1450 | || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela))) | |
70256ad8 | 1451 | return false; |
70256ad8 | 1452 | |
c434dee6 AJ |
1453 | /* If any dynamic relocs apply to a read-only section, |
1454 | then we need a DT_TEXTREL entry. */ | |
1455 | if ((info->flags & DF_TEXTREL) == 0) | |
1456 | elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, | |
1457 | (PTR) info); | |
1458 | ||
1459 | if ((info->flags & DF_TEXTREL) != 0) | |
1460 | { | |
1461 | if (!add_dynamic_entry (DT_TEXTREL, 0)) | |
1462 | return false; | |
1463 | } | |
70256ad8 AJ |
1464 | } |
1465 | } | |
dc810e39 | 1466 | #undef add_dynamic_entry |
70256ad8 AJ |
1467 | |
1468 | return true; | |
1469 | } | |
1470 | ||
8d88c4ca NC |
1471 | /* Relocate an x86_64 ELF section. */ |
1472 | ||
1473 | static boolean | |
1474 | elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section, | |
fe4770f4 | 1475 | contents, relocs, local_syms, local_sections) |
8d88c4ca NC |
1476 | bfd *output_bfd; |
1477 | struct bfd_link_info *info; | |
1478 | bfd *input_bfd; | |
1479 | asection *input_section; | |
1480 | bfd_byte *contents; | |
1481 | Elf_Internal_Rela *relocs; | |
1482 | Elf_Internal_Sym *local_syms; | |
1483 | asection **local_sections; | |
1484 | { | |
c434dee6 | 1485 | struct elf64_x86_64_link_hash_table *htab; |
8d88c4ca NC |
1486 | Elf_Internal_Shdr *symtab_hdr; |
1487 | struct elf_link_hash_entry **sym_hashes; | |
1488 | bfd_vma *local_got_offsets; | |
c434dee6 | 1489 | Elf_Internal_Rela *rel; |
8d88c4ca NC |
1490 | Elf_Internal_Rela *relend; |
1491 | ||
c434dee6 | 1492 | htab = elf64_x86_64_hash_table (info); |
8d88c4ca NC |
1493 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
1494 | sym_hashes = elf_sym_hashes (input_bfd); | |
1495 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
1496 | ||
c434dee6 | 1497 | rel = relocs; |
8d88c4ca | 1498 | relend = relocs + input_section->reloc_count; |
c434dee6 | 1499 | for (; rel < relend; rel++) |
8d88c4ca NC |
1500 | { |
1501 | int r_type; | |
1502 | reloc_howto_type *howto; | |
1503 | unsigned long r_symndx; | |
1504 | struct elf_link_hash_entry *h; | |
1505 | Elf_Internal_Sym *sym; | |
1506 | asection *sec; | |
c434dee6 | 1507 | bfd_vma off; |
8d88c4ca | 1508 | bfd_vma relocation; |
c434dee6 | 1509 | boolean unresolved_reloc; |
8d88c4ca | 1510 | bfd_reloc_status_type r; |
8d88c4ca | 1511 | |
c434dee6 | 1512 | r_type = ELF64_R_TYPE (rel->r_info); |
fe4770f4 AJ |
1513 | if (r_type == (int) R_X86_64_GNU_VTINHERIT |
1514 | || r_type == (int) R_X86_64_GNU_VTENTRY) | |
1515 | continue; | |
8d88c4ca | 1516 | |
c434dee6 | 1517 | if (r_type < 0 || r_type >= R_X86_64_max) |
8da6118f KH |
1518 | { |
1519 | bfd_set_error (bfd_error_bad_value); | |
1520 | return false; | |
1521 | } | |
c434dee6 | 1522 | howto = x86_64_elf_howto_table + r_type; |
8d88c4ca | 1523 | |
c434dee6 | 1524 | r_symndx = ELF64_R_SYM (rel->r_info); |
8d88c4ca NC |
1525 | |
1526 | if (info->relocateable) | |
8da6118f KH |
1527 | { |
1528 | /* This is a relocateable link. We don't have to change | |
1529 | anything, unless the reloc is against a section symbol, | |
1530 | in which case we have to adjust according to where the | |
1531 | section symbol winds up in the output section. */ | |
1532 | if (r_symndx < symtab_hdr->sh_info) | |
1533 | { | |
1534 | sym = local_syms + r_symndx; | |
1535 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
1536 | { | |
1537 | sec = local_sections[r_symndx]; | |
c434dee6 | 1538 | rel->r_addend += sec->output_offset + sym->st_value; |
8da6118f KH |
1539 | } |
1540 | } | |
1541 | ||
1542 | continue; | |
1543 | } | |
8d88c4ca | 1544 | |
407443a3 | 1545 | /* This is a final link. */ |
8d88c4ca NC |
1546 | h = NULL; |
1547 | sym = NULL; | |
1548 | sec = NULL; | |
c434dee6 | 1549 | unresolved_reloc = false; |
8d88c4ca | 1550 | if (r_symndx < symtab_hdr->sh_info) |
8da6118f KH |
1551 | { |
1552 | sym = local_syms + r_symndx; | |
1553 | sec = local_sections[r_symndx]; | |
c434dee6 AJ |
1554 | |
1555 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel); | |
8da6118f | 1556 | } |
8d88c4ca | 1557 | else |
8da6118f KH |
1558 | { |
1559 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1560 | while (h->root.type == bfd_link_hash_indirect | |
1561 | || h->root.type == bfd_link_hash_warning) | |
1562 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c434dee6 | 1563 | |
8da6118f KH |
1564 | if (h->root.type == bfd_link_hash_defined |
1565 | || h->root.type == bfd_link_hash_defweak) | |
1566 | { | |
1567 | sec = h->root.u.def.section; | |
c434dee6 | 1568 | if (sec->output_section == NULL) |
8da6118f | 1569 | { |
c434dee6 AJ |
1570 | /* Set a flag that will be cleared later if we find a |
1571 | relocation value for this symbol. output_section | |
1572 | is typically NULL for symbols satisfied by a shared | |
1573 | library. */ | |
1574 | unresolved_reloc = true; | |
8da6118f KH |
1575 | relocation = 0; |
1576 | } | |
1577 | else | |
1578 | relocation = (h->root.u.def.value | |
1579 | + sec->output_section->vma | |
1580 | + sec->output_offset); | |
1581 | } | |
1582 | else if (h->root.type == bfd_link_hash_undefweak) | |
1583 | relocation = 0; | |
671bae9c NC |
1584 | else if (info->shared |
1585 | && (!info->symbolic || info->allow_shlib_undefined) | |
1586 | && !info->no_undefined | |
70256ad8 AJ |
1587 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) |
1588 | relocation = 0; | |
8da6118f KH |
1589 | else |
1590 | { | |
1591 | if (! ((*info->callbacks->undefined_symbol) | |
1592 | (info, h->root.root.string, input_bfd, | |
c434dee6 | 1593 | input_section, rel->r_offset, |
8da6118f KH |
1594 | (!info->shared || info->no_undefined |
1595 | || ELF_ST_VISIBILITY (h->other))))) | |
1596 | return false; | |
1597 | relocation = 0; | |
1598 | } | |
1599 | } | |
70256ad8 AJ |
1600 | /* When generating a shared object, the relocations handled here are |
1601 | copied into the output file to be resolved at run time. */ | |
1602 | switch (r_type) | |
1603 | { | |
1604 | case R_X86_64_GOT32: | |
1605 | /* Relocation is to the entry for this symbol in the global | |
1606 | offset table. */ | |
70256ad8 AJ |
1607 | case R_X86_64_GOTPCREL: |
1608 | /* Use global offset table as symbol value. */ | |
c434dee6 AJ |
1609 | if (htab->sgot == NULL) |
1610 | abort (); | |
053579d7 | 1611 | |
51e0a107 | 1612 | if (h != NULL) |
70256ad8 | 1613 | { |
c434dee6 AJ |
1614 | boolean dyn; |
1615 | ||
1616 | off = h->got.offset; | |
1617 | dyn = htab->elf.dynamic_sections_created; | |
51e0a107 | 1618 | |
c434dee6 | 1619 | if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h) |
51e0a107 | 1620 | || (info->shared |
c434dee6 AJ |
1621 | && (info->symbolic |
1622 | || h->dynindx == -1 | |
1623 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)) | |
51e0a107 JH |
1624 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) |
1625 | { | |
1626 | /* This is actually a static link, or it is a -Bsymbolic | |
1627 | link and the symbol is defined locally, or the symbol | |
407443a3 | 1628 | was forced to be local because of a version file. We |
51e0a107 JH |
1629 | must initialize this entry in the global offset table. |
1630 | Since the offset must always be a multiple of 8, we | |
1631 | use the least significant bit to record whether we | |
1632 | have initialized it already. | |
1633 | ||
1634 | When doing a dynamic link, we create a .rela.got | |
407443a3 AJ |
1635 | relocation entry to initialize the value. This is |
1636 | done in the finish_dynamic_symbol routine. */ | |
51e0a107 JH |
1637 | if ((off & 1) != 0) |
1638 | off &= ~1; | |
1639 | else | |
1640 | { | |
1641 | bfd_put_64 (output_bfd, relocation, | |
c434dee6 | 1642 | htab->sgot->contents + off); |
51e0a107 JH |
1643 | h->got.offset |= 1; |
1644 | } | |
1645 | } | |
053579d7 | 1646 | else |
c434dee6 | 1647 | unresolved_reloc = false; |
70256ad8 | 1648 | } |
51e0a107 JH |
1649 | else |
1650 | { | |
c434dee6 AJ |
1651 | if (local_got_offsets == NULL) |
1652 | abort (); | |
51e0a107 JH |
1653 | |
1654 | off = local_got_offsets[r_symndx]; | |
1655 | ||
1656 | /* The offset must always be a multiple of 8. We use | |
407443a3 AJ |
1657 | the least significant bit to record whether we have |
1658 | already generated the necessary reloc. */ | |
51e0a107 JH |
1659 | if ((off & 1) != 0) |
1660 | off &= ~1; | |
1661 | else | |
1662 | { | |
c434dee6 AJ |
1663 | bfd_put_64 (output_bfd, relocation, |
1664 | htab->sgot->contents + off); | |
51e0a107 JH |
1665 | |
1666 | if (info->shared) | |
1667 | { | |
1668 | asection *srelgot; | |
1669 | Elf_Internal_Rela outrel; | |
c434dee6 | 1670 | Elf64_External_Rela *loc; |
70256ad8 | 1671 | |
51e0a107 JH |
1672 | /* We need to generate a R_X86_64_RELATIVE reloc |
1673 | for the dynamic linker. */ | |
c434dee6 AJ |
1674 | srelgot = htab->srelgot; |
1675 | if (srelgot == NULL) | |
1676 | abort (); | |
51e0a107 | 1677 | |
c434dee6 AJ |
1678 | outrel.r_offset = (htab->sgot->output_section->vma |
1679 | + htab->sgot->output_offset | |
51e0a107 JH |
1680 | + off); |
1681 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); | |
1682 | outrel.r_addend = relocation; | |
c434dee6 AJ |
1683 | loc = (Elf64_External_Rela *) srelgot->contents; |
1684 | loc += srelgot->reloc_count++; | |
1685 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); | |
51e0a107 JH |
1686 | } |
1687 | ||
1688 | local_got_offsets[r_symndx] |= 1; | |
1689 | } | |
51e0a107 | 1690 | } |
6a2bda3f | 1691 | |
c434dee6 AJ |
1692 | if (off >= (bfd_vma) -2) |
1693 | abort (); | |
1694 | ||
1695 | relocation = htab->sgot->output_offset + off; | |
1696 | if (r_type == R_X86_64_GOTPCREL) | |
1697 | relocation += htab->sgot->output_section->vma; | |
1698 | ||
70256ad8 AJ |
1699 | break; |
1700 | ||
1701 | case R_X86_64_PLT32: | |
1702 | /* Relocation is to the entry for this symbol in the | |
1703 | procedure linkage table. */ | |
1704 | ||
1705 | /* Resolve a PLT32 reloc against a local symbol directly, | |
407443a3 | 1706 | without using the procedure linkage table. */ |
70256ad8 AJ |
1707 | if (h == NULL) |
1708 | break; | |
1709 | ||
c434dee6 AJ |
1710 | if (h->plt.offset == (bfd_vma) -1 |
1711 | || htab->splt == NULL) | |
70256ad8 AJ |
1712 | { |
1713 | /* We didn't make a PLT entry for this symbol. This | |
407443a3 AJ |
1714 | happens when statically linking PIC code, or when |
1715 | using -Bsymbolic. */ | |
70256ad8 AJ |
1716 | break; |
1717 | } | |
1718 | ||
c434dee6 AJ |
1719 | relocation = (htab->splt->output_section->vma |
1720 | + htab->splt->output_offset | |
70256ad8 | 1721 | + h->plt.offset); |
c434dee6 | 1722 | unresolved_reloc = false; |
70256ad8 AJ |
1723 | break; |
1724 | ||
fd8ab9e5 AJ |
1725 | case R_X86_64_PC8: |
1726 | case R_X86_64_PC16: | |
1727 | case R_X86_64_PC32: | |
70256ad8 AJ |
1728 | case R_X86_64_8: |
1729 | case R_X86_64_16: | |
1730 | case R_X86_64_32: | |
6b3db546 | 1731 | case R_X86_64_64: |
80643fbc | 1732 | /* FIXME: The ABI says the linker should make sure the value is |
407443a3 | 1733 | the same when it's zeroextended to 64 bit. */ |
c434dee6 AJ |
1734 | |
1735 | /* r_symndx will be zero only for relocs against symbols | |
1736 | from removed linkonce sections, or sections discarded by | |
1737 | a linker script. */ | |
1738 | if (r_symndx == 0 | |
1739 | || (input_section->flags & SEC_ALLOC) == 0) | |
1740 | break; | |
1741 | ||
1742 | if ((info->shared | |
1743 | && ((r_type != R_X86_64_PC8 | |
1744 | && r_type != R_X86_64_PC16 | |
1745 | && r_type != R_X86_64_PC32) | |
1746 | || (h != NULL | |
1747 | && h->dynindx != -1 | |
1748 | && (! info->symbolic | |
1749 | || (h->elf_link_hash_flags | |
1750 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
1751 | || (!info->shared | |
1752 | && h != NULL | |
1753 | && h->dynindx != -1 | |
1754 | && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0 | |
1755 | && (((h->elf_link_hash_flags | |
1756 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1757 | && (h->elf_link_hash_flags | |
1758 | & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1759 | || h->root.type == bfd_link_hash_undefweak | |
1760 | || h->root.type == bfd_link_hash_undefined))) | |
70256ad8 AJ |
1761 | { |
1762 | Elf_Internal_Rela outrel; | |
1763 | boolean skip, relocate; | |
c434dee6 AJ |
1764 | asection *sreloc; |
1765 | Elf64_External_Rela *loc; | |
70256ad8 AJ |
1766 | |
1767 | /* When generating a shared object, these relocations | |
1768 | are copied into the output file to be resolved at run | |
407443a3 | 1769 | time. */ |
70256ad8 | 1770 | |
70256ad8 | 1771 | skip = false; |
0bb2d96a | 1772 | relocate = false; |
70256ad8 | 1773 | |
c629eae0 JJ |
1774 | outrel.r_offset = |
1775 | _bfd_elf_section_offset (output_bfd, info, input_section, | |
c434dee6 | 1776 | rel->r_offset); |
c629eae0 JJ |
1777 | if (outrel.r_offset == (bfd_vma) -1) |
1778 | skip = true; | |
0fb19cbc | 1779 | else if (outrel.r_offset == (bfd_vma) -2) |
0bb2d96a | 1780 | skip = true, relocate = true; |
70256ad8 AJ |
1781 | |
1782 | outrel.r_offset += (input_section->output_section->vma | |
1783 | + input_section->output_offset); | |
1784 | ||
1785 | if (skip) | |
0bb2d96a | 1786 | memset (&outrel, 0, sizeof outrel); |
c434dee6 | 1787 | |
fd8ab9e5 AJ |
1788 | /* h->dynindx may be -1 if this symbol was marked to |
1789 | become local. */ | |
1790 | else if (h != NULL | |
c434dee6 AJ |
1791 | && h->dynindx != -1 |
1792 | && (r_type == R_X86_64_PC8 | |
1793 | || r_type == R_X86_64_PC16 | |
1794 | || r_type == R_X86_64_PC32 | |
1795 | || !info->shared | |
1796 | || !info->symbolic | |
fd8ab9e5 AJ |
1797 | || (h->elf_link_hash_flags |
1798 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
70256ad8 | 1799 | { |
70256ad8 | 1800 | outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); |
c434dee6 | 1801 | outrel.r_addend = rel->r_addend; |
70256ad8 AJ |
1802 | } |
1803 | else | |
1804 | { | |
c434dee6 AJ |
1805 | /* This symbol is local, or marked to become local. */ |
1806 | relocate = true; | |
1807 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); | |
1808 | outrel.r_addend = relocation + rel->r_addend; | |
70256ad8 AJ |
1809 | } |
1810 | ||
c434dee6 AJ |
1811 | sreloc = elf_section_data (input_section)->sreloc; |
1812 | if (sreloc == NULL) | |
1813 | abort (); | |
1814 | ||
1815 | loc = (Elf64_External_Rela *) sreloc->contents; | |
1816 | loc += sreloc->reloc_count++; | |
1817 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); | |
70256ad8 AJ |
1818 | |
1819 | /* If this reloc is against an external symbol, we do | |
1820 | not want to fiddle with the addend. Otherwise, we | |
1821 | need to include the symbol value so that it becomes | |
1822 | an addend for the dynamic reloc. */ | |
1823 | if (! relocate) | |
1824 | continue; | |
1825 | } | |
1826 | ||
1827 | break; | |
1828 | ||
1829 | default: | |
1830 | break; | |
1831 | } | |
8d88c4ca | 1832 | |
c434dee6 AJ |
1833 | /* FIXME: Why do we allow debugging sections to escape this error? |
1834 | More importantly, why do we not emit dynamic relocs for | |
1835 | R_386_32 above in debugging sections (which are ! SEC_ALLOC)? | |
1836 | If we had emitted the dynamic reloc, we could remove the | |
1837 | fudge here. */ | |
1838 | if (unresolved_reloc | |
1839 | && !(info->shared | |
1840 | && (input_section->flags & SEC_DEBUGGING) != 0 | |
1841 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)) | |
1842 | (*_bfd_error_handler) | |
1843 | (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"), | |
1844 | bfd_archive_filename (input_bfd), | |
1845 | bfd_get_section_name (input_bfd, input_section), | |
1846 | (long) rel->r_offset, | |
1847 | h->root.root.string); | |
1848 | ||
8d88c4ca | 1849 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
c434dee6 AJ |
1850 | contents, rel->r_offset, |
1851 | relocation, rel->r_addend); | |
8d88c4ca NC |
1852 | |
1853 | if (r != bfd_reloc_ok) | |
8da6118f | 1854 | { |
c434dee6 AJ |
1855 | const char *name; |
1856 | ||
1857 | if (h != NULL) | |
1858 | name = h->root.root.string; | |
1859 | else | |
8da6118f | 1860 | { |
c434dee6 AJ |
1861 | name = bfd_elf_string_from_elf_section (input_bfd, |
1862 | symtab_hdr->sh_link, | |
1863 | sym->st_name); | |
1864 | if (name == NULL) | |
1865 | return false; | |
1866 | if (*name == '\0') | |
1867 | name = bfd_section_name (input_bfd, sec); | |
1868 | } | |
1869 | ||
1870 | if (r == bfd_reloc_overflow) | |
1871 | { | |
1872 | ||
1873 | if (! ((*info->callbacks->reloc_overflow) | |
1874 | (info, name, howto->name, (bfd_vma) 0, | |
1875 | input_bfd, input_section, rel->r_offset))) | |
1876 | return false; | |
1877 | } | |
1878 | else | |
1879 | { | |
1880 | (*_bfd_error_handler) | |
1881 | (_("%s(%s+0x%lx): reloc against `%s': error %d"), | |
1882 | bfd_archive_filename (input_bfd), | |
1883 | bfd_get_section_name (input_bfd, input_section), | |
1884 | (long) rel->r_offset, name, (int) r); | |
1885 | return false; | |
8da6118f KH |
1886 | } |
1887 | } | |
8d88c4ca | 1888 | } |
70256ad8 AJ |
1889 | |
1890 | return true; | |
1891 | } | |
1892 | ||
1893 | /* Finish up dynamic symbol handling. We set the contents of various | |
1894 | dynamic sections here. */ | |
1895 | ||
1896 | static boolean | |
1897 | elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym) | |
1898 | bfd *output_bfd; | |
1899 | struct bfd_link_info *info; | |
1900 | struct elf_link_hash_entry *h; | |
1901 | Elf_Internal_Sym *sym; | |
1902 | { | |
c434dee6 | 1903 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 | 1904 | |
c434dee6 | 1905 | htab = elf64_x86_64_hash_table (info); |
70256ad8 AJ |
1906 | |
1907 | if (h->plt.offset != (bfd_vma) -1) | |
1908 | { | |
70256ad8 AJ |
1909 | bfd_vma plt_index; |
1910 | bfd_vma got_offset; | |
1911 | Elf_Internal_Rela rela; | |
c434dee6 | 1912 | Elf64_External_Rela *loc; |
70256ad8 AJ |
1913 | |
1914 | /* This symbol has an entry in the procedure linkage table. Set | |
407443a3 | 1915 | it up. */ |
70256ad8 | 1916 | |
c434dee6 AJ |
1917 | if (h->dynindx == -1 |
1918 | || htab->splt == NULL | |
1919 | || htab->sgotplt == NULL | |
1920 | || htab->srelplt == NULL) | |
1921 | abort (); | |
70256ad8 AJ |
1922 | |
1923 | /* Get the index in the procedure linkage table which | |
1924 | corresponds to this symbol. This is the index of this symbol | |
1925 | in all the symbols for which we are making plt entries. The | |
1926 | first entry in the procedure linkage table is reserved. */ | |
1927 | plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; | |
1928 | ||
1929 | /* Get the offset into the .got table of the entry that | |
407443a3 | 1930 | corresponds to this function. Each .got entry is GOT_ENTRY_SIZE |
fe4770f4 | 1931 | bytes. The first three are reserved for the dynamic linker. */ |
70256ad8 AJ |
1932 | got_offset = (plt_index + 3) * GOT_ENTRY_SIZE; |
1933 | ||
1934 | /* Fill in the entry in the procedure linkage table. */ | |
c434dee6 | 1935 | memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry, |
70256ad8 AJ |
1936 | PLT_ENTRY_SIZE); |
1937 | ||
1938 | /* Insert the relocation positions of the plt section. The magic | |
1939 | numbers at the end of the statements are the positions of the | |
1940 | relocations in the plt section. */ | |
653165cc AJ |
1941 | /* Put offset for jmp *name@GOTPCREL(%rip), since the |
1942 | instruction uses 6 bytes, subtract this value. */ | |
1943 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
1944 | (htab->sgotplt->output_section->vma |
1945 | + htab->sgotplt->output_offset | |
653165cc | 1946 | + got_offset |
c434dee6 AJ |
1947 | - htab->splt->output_section->vma |
1948 | - htab->splt->output_offset | |
653165cc AJ |
1949 | - h->plt.offset |
1950 | - 6), | |
c434dee6 | 1951 | htab->splt->contents + h->plt.offset + 2); |
653165cc AJ |
1952 | /* Put relocation index. */ |
1953 | bfd_put_32 (output_bfd, plt_index, | |
c434dee6 | 1954 | htab->splt->contents + h->plt.offset + 7); |
653165cc AJ |
1955 | /* Put offset for jmp .PLT0. */ |
1956 | bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE), | |
c434dee6 | 1957 | htab->splt->contents + h->plt.offset + 12); |
70256ad8 | 1958 | |
653165cc AJ |
1959 | /* Fill in the entry in the global offset table, initially this |
1960 | points to the pushq instruction in the PLT which is at offset 6. */ | |
c434dee6 AJ |
1961 | bfd_put_64 (output_bfd, (htab->splt->output_section->vma |
1962 | + htab->splt->output_offset | |
70256ad8 | 1963 | + h->plt.offset + 6), |
c434dee6 | 1964 | htab->sgotplt->contents + got_offset); |
70256ad8 AJ |
1965 | |
1966 | /* Fill in the entry in the .rela.plt section. */ | |
c434dee6 AJ |
1967 | rela.r_offset = (htab->sgotplt->output_section->vma |
1968 | + htab->sgotplt->output_offset | |
70256ad8 AJ |
1969 | + got_offset); |
1970 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT); | |
1971 | rela.r_addend = 0; | |
c434dee6 AJ |
1972 | loc = (Elf64_External_Rela *) htab->srelplt->contents + plt_index; |
1973 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); | |
70256ad8 AJ |
1974 | |
1975 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1976 | { | |
1977 | /* Mark the symbol as undefined, rather than as defined in | |
c434dee6 AJ |
1978 | the .plt section. Leave the value alone. This is a clue |
1979 | for the dynamic linker, to make function pointer | |
1980 | comparisons work between an application and shared | |
1981 | library. */ | |
70256ad8 AJ |
1982 | sym->st_shndx = SHN_UNDEF; |
1983 | } | |
1984 | } | |
1985 | ||
053579d7 AJ |
1986 | if (h->got.offset != (bfd_vma) -1) |
1987 | { | |
053579d7 | 1988 | Elf_Internal_Rela rela; |
c434dee6 | 1989 | Elf64_External_Rela *loc; |
053579d7 AJ |
1990 | |
1991 | /* This symbol has an entry in the global offset table. Set it | |
1992 | up. */ | |
1993 | ||
c434dee6 AJ |
1994 | if (htab->sgot == NULL || htab->srelgot == NULL) |
1995 | abort (); | |
053579d7 | 1996 | |
c434dee6 AJ |
1997 | rela.r_offset = (htab->sgot->output_section->vma |
1998 | + htab->sgot->output_offset | |
dc810e39 | 1999 | + (h->got.offset &~ (bfd_vma) 1)); |
053579d7 AJ |
2000 | |
2001 | /* If this is a static link, or it is a -Bsymbolic link and the | |
2002 | symbol is defined locally or was forced to be local because | |
2003 | of a version file, we just want to emit a RELATIVE reloc. | |
2004 | The entry in the global offset table will already have been | |
2005 | initialized in the relocate_section function. */ | |
c434dee6 AJ |
2006 | if (info->shared |
2007 | && (info->symbolic | |
2008 | || h->dynindx == -1 | |
2009 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)) | |
2010 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)) | |
053579d7 | 2011 | { |
cc78d0af | 2012 | BFD_ASSERT((h->got.offset & 1) != 0); |
053579d7 AJ |
2013 | rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); |
2014 | rela.r_addend = (h->root.u.def.value | |
2015 | + h->root.u.def.section->output_section->vma | |
2016 | + h->root.u.def.section->output_offset); | |
2017 | } | |
2018 | else | |
2019 | { | |
2020 | BFD_ASSERT((h->got.offset & 1) == 0); | |
c434dee6 AJ |
2021 | bfd_put_64 (output_bfd, (bfd_vma) 0, |
2022 | htab->sgot->contents + h->got.offset); | |
053579d7 AJ |
2023 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT); |
2024 | rela.r_addend = 0; | |
2025 | } | |
2026 | ||
c434dee6 AJ |
2027 | loc = (Elf64_External_Rela *) htab->srelgot->contents; |
2028 | loc += htab->srelgot->reloc_count++; | |
2029 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); | |
053579d7 AJ |
2030 | } |
2031 | ||
70256ad8 AJ |
2032 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) |
2033 | { | |
70256ad8 | 2034 | Elf_Internal_Rela rela; |
c434dee6 | 2035 | Elf64_External_Rela *loc; |
70256ad8 AJ |
2036 | |
2037 | /* This symbol needs a copy reloc. Set it up. */ | |
2038 | ||
c434dee6 AJ |
2039 | if (h->dynindx == -1 |
2040 | || (h->root.type != bfd_link_hash_defined | |
2041 | && h->root.type != bfd_link_hash_defweak) | |
2042 | || htab->srelbss == NULL) | |
2043 | abort (); | |
70256ad8 AJ |
2044 | |
2045 | rela.r_offset = (h->root.u.def.value | |
2046 | + h->root.u.def.section->output_section->vma | |
2047 | + h->root.u.def.section->output_offset); | |
2048 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY); | |
2049 | rela.r_addend = 0; | |
c434dee6 AJ |
2050 | loc = (Elf64_External_Rela *) htab->srelbss->contents; |
2051 | loc += htab->srelbss->reloc_count++; | |
2052 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); | |
70256ad8 AJ |
2053 | } |
2054 | ||
2055 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ | |
2056 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
2057 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
2058 | sym->st_shndx = SHN_ABS; | |
2059 | ||
2060 | return true; | |
2061 | } | |
2062 | ||
c434dee6 AJ |
2063 | /* Used to decide how to sort relocs in an optimal manner for the |
2064 | dynamic linker, before writing them out. */ | |
2065 | ||
2066 | static enum elf_reloc_type_class | |
2067 | elf64_x86_64_reloc_type_class (rela) | |
2068 | const Elf_Internal_Rela *rela; | |
2069 | { | |
2070 | switch ((int) ELF64_R_TYPE (rela->r_info)) | |
2071 | { | |
2072 | case R_X86_64_RELATIVE: | |
2073 | return reloc_class_relative; | |
2074 | case R_X86_64_JUMP_SLOT: | |
2075 | return reloc_class_plt; | |
2076 | case R_X86_64_COPY: | |
2077 | return reloc_class_copy; | |
2078 | default: | |
2079 | return reloc_class_normal; | |
2080 | } | |
2081 | } | |
2082 | ||
70256ad8 AJ |
2083 | /* Finish up the dynamic sections. */ |
2084 | ||
2085 | static boolean | |
2086 | elf64_x86_64_finish_dynamic_sections (output_bfd, info) | |
2087 | bfd *output_bfd; | |
2088 | struct bfd_link_info *info; | |
2089 | { | |
c434dee6 | 2090 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
2091 | bfd *dynobj; |
2092 | asection *sdyn; | |
70256ad8 | 2093 | |
c434dee6 AJ |
2094 | htab = elf64_x86_64_hash_table (info); |
2095 | dynobj = htab->elf.dynobj; | |
70256ad8 AJ |
2096 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); |
2097 | ||
c434dee6 | 2098 | if (htab->elf.dynamic_sections_created) |
70256ad8 | 2099 | { |
70256ad8 AJ |
2100 | Elf64_External_Dyn *dyncon, *dynconend; |
2101 | ||
c434dee6 AJ |
2102 | if (sdyn == NULL || htab->sgot == NULL) |
2103 | abort (); | |
70256ad8 AJ |
2104 | |
2105 | dyncon = (Elf64_External_Dyn *) sdyn->contents; | |
2106 | dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
2107 | for (; dyncon < dynconend; dyncon++) | |
2108 | { | |
2109 | Elf_Internal_Dyn dyn; | |
70256ad8 AJ |
2110 | asection *s; |
2111 | ||
2112 | bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); | |
2113 | ||
2114 | switch (dyn.d_tag) | |
2115 | { | |
2116 | default: | |
053579d7 | 2117 | continue; |
70256ad8 AJ |
2118 | |
2119 | case DT_PLTGOT: | |
c434dee6 AJ |
2120 | dyn.d_un.d_ptr = htab->sgot->output_section->vma; |
2121 | break; | |
70256ad8 AJ |
2122 | |
2123 | case DT_JMPREL: | |
c434dee6 AJ |
2124 | dyn.d_un.d_ptr = htab->srelplt->output_section->vma; |
2125 | break; | |
70256ad8 | 2126 | |
c434dee6 AJ |
2127 | case DT_PLTRELSZ: |
2128 | s = htab->srelplt->output_section; | |
2129 | if (s->_cooked_size != 0) | |
2130 | dyn.d_un.d_val = s->_cooked_size; | |
2131 | else | |
2132 | dyn.d_un.d_val = s->_raw_size; | |
70256ad8 AJ |
2133 | break; |
2134 | ||
2135 | case DT_RELASZ: | |
c434dee6 AJ |
2136 | /* The procedure linkage table relocs (DT_JMPREL) should |
2137 | not be included in the overall relocs (DT_RELA). | |
2138 | Therefore, we override the DT_RELASZ entry here to | |
2139 | make it not include the JMPREL relocs. Since the | |
2140 | linker script arranges for .rela.plt to follow all | |
2141 | other relocation sections, we don't have to worry | |
2142 | about changing the DT_RELA entry. */ | |
2143 | if (htab->srelplt != NULL) | |
70256ad8 | 2144 | { |
c434dee6 AJ |
2145 | s = htab->srelplt->output_section; |
2146 | if (s->_cooked_size != 0) | |
2147 | dyn.d_un.d_val -= s->_cooked_size; | |
2148 | else | |
2149 | dyn.d_un.d_val -= s->_raw_size; | |
70256ad8 AJ |
2150 | } |
2151 | break; | |
70256ad8 | 2152 | } |
c434dee6 | 2153 | |
70256ad8 AJ |
2154 | bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); |
2155 | } | |
2156 | ||
c434dee6 AJ |
2157 | /* Fill in the special first entry in the procedure linkage table. */ |
2158 | if (htab->splt && htab->splt->_raw_size > 0) | |
70256ad8 | 2159 | { |
653165cc | 2160 | /* Fill in the first entry in the procedure linkage table. */ |
c434dee6 AJ |
2161 | memcpy (htab->splt->contents, elf64_x86_64_plt0_entry, |
2162 | PLT_ENTRY_SIZE); | |
653165cc AJ |
2163 | /* Add offset for pushq GOT+8(%rip), since the instruction |
2164 | uses 6 bytes subtract this value. */ | |
2165 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
2166 | (htab->sgotplt->output_section->vma |
2167 | + htab->sgotplt->output_offset | |
653165cc | 2168 | + 8 |
c434dee6 AJ |
2169 | - htab->splt->output_section->vma |
2170 | - htab->splt->output_offset | |
653165cc | 2171 | - 6), |
c434dee6 | 2172 | htab->splt->contents + 2); |
653165cc AJ |
2173 | /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to |
2174 | the end of the instruction. */ | |
2175 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
2176 | (htab->sgotplt->output_section->vma |
2177 | + htab->sgotplt->output_offset | |
653165cc | 2178 | + 16 |
c434dee6 AJ |
2179 | - htab->splt->output_section->vma |
2180 | - htab->splt->output_offset | |
653165cc | 2181 | - 12), |
c434dee6 | 2182 | htab->splt->contents + 8); |
653165cc | 2183 | |
c434dee6 AJ |
2184 | elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize = |
2185 | PLT_ENTRY_SIZE; | |
70256ad8 | 2186 | } |
70256ad8 AJ |
2187 | } |
2188 | ||
c434dee6 | 2189 | if (htab->sgotplt) |
70256ad8 | 2190 | { |
c434dee6 AJ |
2191 | /* Fill in the first three entries in the global offset table. */ |
2192 | if (htab->sgotplt->_raw_size > 0) | |
2193 | { | |
2194 | /* Set the first entry in the global offset table to the address of | |
2195 | the dynamic section. */ | |
2196 | if (sdyn == NULL) | |
2197 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents); | |
2198 | else | |
2199 | bfd_put_64 (output_bfd, | |
2200 | sdyn->output_section->vma + sdyn->output_offset, | |
2201 | htab->sgotplt->contents); | |
2202 | /* Write GOT[1] and GOT[2], needed for the dynamic linker. */ | |
2203 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE); | |
2204 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2); | |
2205 | } | |
70256ad8 | 2206 | |
c434dee6 AJ |
2207 | elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = |
2208 | GOT_ENTRY_SIZE; | |
2209 | } | |
70256ad8 | 2210 | |
8d88c4ca NC |
2211 | return true; |
2212 | } | |
2213 | ||
8df9fc9d | 2214 | |
70256ad8 AJ |
2215 | #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec |
2216 | #define TARGET_LITTLE_NAME "elf64-x86-64" | |
2217 | #define ELF_ARCH bfd_arch_i386 | |
2218 | #define ELF_MACHINE_CODE EM_X86_64 | |
2219 | #define ELF_MAXPAGESIZE 0x100000 | |
2220 | ||
2221 | #define elf_backend_can_gc_sections 1 | |
51b64d56 | 2222 | #define elf_backend_can_refcount 1 |
70256ad8 AJ |
2223 | #define elf_backend_want_got_plt 1 |
2224 | #define elf_backend_plt_readonly 1 | |
2225 | #define elf_backend_want_plt_sym 0 | |
2226 | #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3) | |
2227 | #define elf_backend_plt_header_size PLT_ENTRY_SIZE | |
2228 | ||
2229 | #define elf_info_to_howto elf64_x86_64_info_to_howto | |
70256ad8 | 2230 | |
70256ad8 AJ |
2231 | #define bfd_elf64_bfd_link_hash_table_create \ |
2232 | elf64_x86_64_link_hash_table_create | |
407443a3 | 2233 | #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup |
70256ad8 AJ |
2234 | |
2235 | #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol | |
2236 | #define elf_backend_check_relocs elf64_x86_64_check_relocs | |
c434dee6 AJ |
2237 | #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol |
2238 | #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections | |
2239 | #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections | |
70256ad8 AJ |
2240 | #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol |
2241 | #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook | |
2242 | #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook | |
c434dee6 | 2243 | #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class |
70256ad8 AJ |
2244 | #define elf_backend_relocate_section elf64_x86_64_relocate_section |
2245 | #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections | |
407443a3 | 2246 | #define elf_backend_object_p elf64_x86_64_elf_object_p |
8d88c4ca NC |
2247 | |
2248 | #include "elf64-target.h" |