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 | |
e2d34d7d | 343 | ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt); |
c434dee6 | 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 | 348 | { |
e2d34d7d | 349 | free (ret); |
8d88c4ca NC |
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 | ||
e92d460e | 1078 | if (h->root.type == bfd_link_hash_indirect) |
c434dee6 AJ |
1079 | return true; |
1080 | ||
e92d460e AM |
1081 | if (h->root.type == bfd_link_hash_warning) |
1082 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1083 | ||
c434dee6 AJ |
1084 | info = (struct bfd_link_info *) inf; |
1085 | htab = elf64_x86_64_hash_table (info); | |
1086 | ||
1087 | if (htab->elf.dynamic_sections_created | |
1088 | && h->plt.refcount > 0) | |
1089 | { | |
1090 | /* Make sure this symbol is output as a dynamic symbol. | |
1091 | Undefined weak syms won't yet be marked as dynamic. */ | |
1092 | if (h->dynindx == -1 | |
1093 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1094 | { | |
1095 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
1096 | return false; | |
1097 | } | |
1098 | ||
1099 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h)) | |
1100 | { | |
1101 | asection *s = htab->splt; | |
1102 | ||
1103 | /* If this is the first .plt entry, make room for the special | |
1104 | first entry. */ | |
1105 | if (s->_raw_size == 0) | |
1106 | s->_raw_size += PLT_ENTRY_SIZE; | |
1107 | ||
1108 | h->plt.offset = s->_raw_size; | |
1109 | ||
1110 | /* If this symbol is not defined in a regular file, and we are | |
1111 | not generating a shared library, then set the symbol to this | |
1112 | location in the .plt. This is required to make function | |
1113 | pointers compare as equal between the normal executable and | |
1114 | the shared library. */ | |
1115 | if (! info->shared | |
1116 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1117 | { | |
1118 | h->root.u.def.section = s; | |
1119 | h->root.u.def.value = h->plt.offset; | |
1120 | } | |
1121 | ||
1122 | /* Make room for this entry. */ | |
1123 | s->_raw_size += PLT_ENTRY_SIZE; | |
1124 | ||
1125 | /* We also need to make an entry in the .got.plt section, which | |
1126 | will be placed in the .got section by the linker script. */ | |
1127 | htab->sgotplt->_raw_size += GOT_ENTRY_SIZE; | |
1128 | ||
1129 | /* We also need to make an entry in the .rela.plt section. */ | |
1130 | htab->srelplt->_raw_size += sizeof (Elf64_External_Rela); | |
1131 | } | |
1132 | else | |
1133 | { | |
1134 | h->plt.offset = (bfd_vma) -1; | |
1135 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
1136 | } | |
1137 | } | |
1138 | else | |
1139 | { | |
1140 | h->plt.offset = (bfd_vma) -1; | |
1141 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
1142 | } | |
1143 | ||
1144 | if (h->got.refcount > 0) | |
1145 | { | |
1146 | asection *s; | |
1147 | boolean dyn; | |
1148 | ||
1149 | /* Make sure this symbol is output as a dynamic symbol. | |
1150 | Undefined weak syms won't yet be marked as dynamic. */ | |
1151 | if (h->dynindx == -1 | |
1152 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1153 | { | |
1154 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
1155 | return false; | |
1156 | } | |
1157 | ||
1158 | s = htab->sgot; | |
1159 | h->got.offset = s->_raw_size; | |
1160 | s->_raw_size += GOT_ENTRY_SIZE; | |
1161 | dyn = htab->elf.dynamic_sections_created; | |
1162 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)) | |
1163 | htab->srelgot->_raw_size += sizeof (Elf64_External_Rela); | |
1164 | } | |
1165 | else | |
1166 | h->got.offset = (bfd_vma) -1; | |
1167 | ||
1168 | eh = (struct elf64_x86_64_link_hash_entry *) h; | |
1169 | if (eh->dyn_relocs == NULL) | |
1170 | return true; | |
1171 | ||
1172 | /* In the shared -Bsymbolic case, discard space allocated for | |
1173 | dynamic pc-relative relocs against symbols which turn out to be | |
1174 | defined in regular objects. For the normal shared case, discard | |
1175 | space for pc-relative relocs that have become local due to symbol | |
1176 | visibility changes. */ | |
1177 | ||
1178 | if (info->shared) | |
1179 | { | |
1180 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
1181 | && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0 | |
1182 | || info->symbolic)) | |
1183 | { | |
1184 | struct elf64_x86_64_dyn_relocs **pp; | |
1185 | ||
1186 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
1187 | { | |
1188 | p->count -= p->pc_count; | |
1189 | p->pc_count = 0; | |
1190 | if (p->count == 0) | |
1191 | *pp = p->next; | |
1192 | else | |
1193 | pp = &p->next; | |
1194 | } | |
1195 | } | |
1196 | } | |
1197 | else | |
1198 | { | |
1199 | /* For the non-shared case, discard space for relocs against | |
1200 | symbols which turn out to need copy relocs or are not | |
1201 | dynamic. */ | |
1202 | ||
1203 | if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0 | |
1204 | && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1205 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1206 | || (htab->elf.dynamic_sections_created | |
1207 | && (h->root.type == bfd_link_hash_undefweak | |
1208 | || h->root.type == bfd_link_hash_undefined)))) | |
1209 | { | |
1210 | /* Make sure this symbol is output as a dynamic symbol. | |
1211 | Undefined weak syms won't yet be marked as dynamic. */ | |
1212 | if (h->dynindx == -1 | |
1213 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1214 | { | |
1215 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
1216 | return false; | |
1217 | } | |
1218 | ||
1219 | /* If that succeeded, we know we'll be keeping all the | |
1220 | relocs. */ | |
1221 | if (h->dynindx != -1) | |
1222 | goto keep; | |
1223 | } | |
1224 | ||
1225 | eh->dyn_relocs = NULL; | |
1226 | ||
1227 | keep: ; | |
1228 | } | |
1229 | ||
1230 | /* Finally, allocate space. */ | |
1231 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1232 | { | |
1233 | asection *sreloc = elf_section_data (p->sec)->sreloc; | |
1234 | sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela); | |
1235 | } | |
1236 | ||
1237 | return true; | |
1238 | } | |
1239 | ||
1240 | /* Find any dynamic relocs that apply to read-only sections. */ | |
1241 | ||
1242 | static boolean | |
1243 | readonly_dynrelocs (h, inf) | |
1244 | struct elf_link_hash_entry *h; | |
1245 | PTR inf; | |
1246 | { | |
1247 | struct elf64_x86_64_link_hash_entry *eh; | |
1248 | struct elf64_x86_64_dyn_relocs *p; | |
1249 | ||
e92d460e AM |
1250 | if (h->root.type == bfd_link_hash_warning) |
1251 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1252 | ||
c434dee6 AJ |
1253 | eh = (struct elf64_x86_64_link_hash_entry *) h; |
1254 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1255 | { | |
1256 | asection *s = p->sec->output_section; | |
1257 | ||
1258 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
1259 | { | |
1260 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
1261 | ||
1262 | info->flags |= DF_TEXTREL; | |
1263 | ||
1264 | /* Not an error, just cut short the traversal. */ | |
1265 | return false; | |
1266 | } | |
1267 | } | |
1268 | return true; | |
1269 | } | |
1270 | ||
70256ad8 AJ |
1271 | /* Set the sizes of the dynamic sections. */ |
1272 | ||
1273 | static boolean | |
1274 | elf64_x86_64_size_dynamic_sections (output_bfd, info) | |
8df9fc9d | 1275 | bfd *output_bfd ATTRIBUTE_UNUSED; |
70256ad8 AJ |
1276 | struct bfd_link_info *info; |
1277 | { | |
c434dee6 | 1278 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
1279 | bfd *dynobj; |
1280 | asection *s; | |
70256ad8 | 1281 | boolean relocs; |
c434dee6 | 1282 | bfd *ibfd; |
70256ad8 | 1283 | |
c434dee6 AJ |
1284 | htab = elf64_x86_64_hash_table (info); |
1285 | dynobj = htab->elf.dynobj; | |
1286 | if (dynobj == NULL) | |
1287 | abort (); | |
70256ad8 | 1288 | |
c434dee6 | 1289 | if (htab->elf.dynamic_sections_created) |
70256ad8 AJ |
1290 | { |
1291 | /* Set the contents of the .interp section to the interpreter. */ | |
1292 | if (! info->shared) | |
1293 | { | |
1294 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
c434dee6 AJ |
1295 | if (s == NULL) |
1296 | abort (); | |
70256ad8 AJ |
1297 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; |
1298 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
1299 | } | |
1300 | } | |
70256ad8 | 1301 | |
c434dee6 AJ |
1302 | /* Set up .got offsets for local syms, and space for local dynamic |
1303 | relocs. */ | |
1304 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
70256ad8 | 1305 | { |
c434dee6 AJ |
1306 | bfd_signed_vma *local_got; |
1307 | bfd_signed_vma *end_local_got; | |
1308 | bfd_size_type locsymcount; | |
1309 | Elf_Internal_Shdr *symtab_hdr; | |
1310 | asection *srel; | |
70256ad8 | 1311 | |
c434dee6 | 1312 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
70256ad8 AJ |
1313 | continue; |
1314 | ||
c434dee6 | 1315 | for (s = ibfd->sections; s != NULL; s = s->next) |
70256ad8 | 1316 | { |
c434dee6 AJ |
1317 | struct elf64_x86_64_dyn_relocs *p; |
1318 | ||
1319 | for (p = *((struct elf64_x86_64_dyn_relocs **) | |
1320 | &elf_section_data (s)->local_dynrel); | |
1321 | p != NULL; | |
1322 | p = p->next) | |
70256ad8 | 1323 | { |
c434dee6 AJ |
1324 | if (!bfd_is_abs_section (p->sec) |
1325 | && bfd_is_abs_section (p->sec->output_section)) | |
1326 | { | |
1327 | /* Input section has been discarded, either because | |
1328 | it is a copy of a linkonce section or due to | |
1329 | linker script /DISCARD/, so we'll be discarding | |
1330 | the relocs too. */ | |
1331 | } | |
1332 | else if (p->count != 0) | |
1333 | { | |
1334 | srel = elf_section_data (p->sec)->sreloc; | |
1335 | srel->_raw_size += p->count * sizeof (Elf64_External_Rela); | |
1336 | if ((p->sec->output_section->flags & SEC_READONLY) != 0) | |
1337 | info->flags |= DF_TEXTREL; | |
1338 | ||
1339 | } | |
70256ad8 AJ |
1340 | } |
1341 | } | |
c434dee6 AJ |
1342 | |
1343 | local_got = elf_local_got_refcounts (ibfd); | |
1344 | if (!local_got) | |
1345 | continue; | |
1346 | ||
1347 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1348 | locsymcount = symtab_hdr->sh_info; | |
1349 | end_local_got = local_got + locsymcount; | |
1350 | s = htab->sgot; | |
1351 | srel = htab->srelgot; | |
1352 | for (; local_got < end_local_got; ++local_got) | |
70256ad8 | 1353 | { |
c434dee6 | 1354 | if (*local_got > 0) |
70256ad8 | 1355 | { |
c434dee6 AJ |
1356 | *local_got = s->_raw_size; |
1357 | s->_raw_size += GOT_ENTRY_SIZE; | |
1358 | if (info->shared) | |
1359 | srel->_raw_size += sizeof (Elf64_External_Rela); | |
70256ad8 AJ |
1360 | } |
1361 | else | |
c434dee6 AJ |
1362 | *local_got = (bfd_vma) -1; |
1363 | } | |
1364 | } | |
70256ad8 | 1365 | |
c434dee6 AJ |
1366 | /* Allocate global sym .plt and .got entries, and space for global |
1367 | sym dynamic relocs. */ | |
1368 | elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); | |
1369 | ||
1370 | /* We now have determined the sizes of the various dynamic sections. | |
1371 | Allocate memory for them. */ | |
1372 | relocs = false; | |
1373 | for (s = dynobj->sections; s != NULL; s = s->next) | |
1374 | { | |
1375 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
1376 | continue; | |
1377 | ||
1378 | if (s == htab->splt | |
1379 | || s == htab->sgot | |
1380 | || s == htab->sgotplt) | |
1381 | { | |
1382 | /* Strip this section if we don't need it; see the | |
1383 | comment below. */ | |
1384 | } | |
1385 | else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0) | |
1386 | { | |
1387 | if (s->_raw_size != 0 && s != htab->srelplt) | |
1388 | relocs = true; | |
1389 | ||
1390 | /* We use the reloc_count field as a counter if we need | |
1391 | to copy relocs into the output file. */ | |
1392 | s->reloc_count = 0; | |
70256ad8 | 1393 | } |
c434dee6 | 1394 | else |
70256ad8 AJ |
1395 | { |
1396 | /* It's not one of our sections, so don't allocate space. */ | |
1397 | continue; | |
1398 | } | |
1399 | ||
c434dee6 | 1400 | if (s->_raw_size == 0) |
70256ad8 | 1401 | { |
c434dee6 AJ |
1402 | /* If we don't need this section, strip it from the |
1403 | output file. This is mostly to handle .rela.bss and | |
1404 | .rela.plt. We must create both sections in | |
1405 | create_dynamic_sections, because they must be created | |
1406 | before the linker maps input sections to output | |
1407 | sections. The linker does that before | |
1408 | adjust_dynamic_symbol is called, and it is that | |
1409 | function which decides whether anything needs to go | |
1410 | into these sections. */ | |
1411 | ||
70256ad8 AJ |
1412 | _bfd_strip_section_from_output (info, s); |
1413 | continue; | |
1414 | } | |
1415 | ||
1416 | /* Allocate memory for the section contents. We use bfd_zalloc | |
1417 | here in case unused entries are not reclaimed before the | |
1418 | section's contents are written out. This should not happen, | |
1419 | but this way if it does, we get a R_X86_64_NONE reloc instead | |
1420 | of garbage. */ | |
1421 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size); | |
c434dee6 | 1422 | if (s->contents == NULL) |
70256ad8 AJ |
1423 | return false; |
1424 | } | |
1425 | ||
c434dee6 | 1426 | if (htab->elf.dynamic_sections_created) |
70256ad8 AJ |
1427 | { |
1428 | /* Add some entries to the .dynamic section. We fill in the | |
1429 | values later, in elf64_x86_64_finish_dynamic_sections, but we | |
1430 | must add the entries now so that we get the correct size for | |
407443a3 | 1431 | the .dynamic section. The DT_DEBUG entry is filled in by the |
70256ad8 | 1432 | dynamic linker and used by the debugger. */ |
dc810e39 AM |
1433 | #define add_dynamic_entry(TAG, VAL) \ |
1434 | bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL)) | |
1435 | ||
70256ad8 AJ |
1436 | if (! info->shared) |
1437 | { | |
dc810e39 | 1438 | if (!add_dynamic_entry (DT_DEBUG, 0)) |
70256ad8 AJ |
1439 | return false; |
1440 | } | |
1441 | ||
c434dee6 | 1442 | if (htab->splt->_raw_size != 0) |
70256ad8 | 1443 | { |
dc810e39 AM |
1444 | if (!add_dynamic_entry (DT_PLTGOT, 0) |
1445 | || !add_dynamic_entry (DT_PLTRELSZ, 0) | |
1446 | || !add_dynamic_entry (DT_PLTREL, DT_RELA) | |
1447 | || !add_dynamic_entry (DT_JMPREL, 0)) | |
70256ad8 AJ |
1448 | return false; |
1449 | } | |
1450 | ||
1451 | if (relocs) | |
1452 | { | |
dc810e39 AM |
1453 | if (!add_dynamic_entry (DT_RELA, 0) |
1454 | || !add_dynamic_entry (DT_RELASZ, 0) | |
1455 | || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela))) | |
70256ad8 | 1456 | return false; |
70256ad8 | 1457 | |
c434dee6 AJ |
1458 | /* If any dynamic relocs apply to a read-only section, |
1459 | then we need a DT_TEXTREL entry. */ | |
1460 | if ((info->flags & DF_TEXTREL) == 0) | |
1461 | elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, | |
1462 | (PTR) info); | |
1463 | ||
1464 | if ((info->flags & DF_TEXTREL) != 0) | |
1465 | { | |
1466 | if (!add_dynamic_entry (DT_TEXTREL, 0)) | |
1467 | return false; | |
1468 | } | |
70256ad8 AJ |
1469 | } |
1470 | } | |
dc810e39 | 1471 | #undef add_dynamic_entry |
70256ad8 AJ |
1472 | |
1473 | return true; | |
1474 | } | |
1475 | ||
8d88c4ca NC |
1476 | /* Relocate an x86_64 ELF section. */ |
1477 | ||
1478 | static boolean | |
1479 | elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section, | |
fe4770f4 | 1480 | contents, relocs, local_syms, local_sections) |
8d88c4ca NC |
1481 | bfd *output_bfd; |
1482 | struct bfd_link_info *info; | |
1483 | bfd *input_bfd; | |
1484 | asection *input_section; | |
1485 | bfd_byte *contents; | |
1486 | Elf_Internal_Rela *relocs; | |
1487 | Elf_Internal_Sym *local_syms; | |
1488 | asection **local_sections; | |
1489 | { | |
c434dee6 | 1490 | struct elf64_x86_64_link_hash_table *htab; |
8d88c4ca NC |
1491 | Elf_Internal_Shdr *symtab_hdr; |
1492 | struct elf_link_hash_entry **sym_hashes; | |
1493 | bfd_vma *local_got_offsets; | |
c434dee6 | 1494 | Elf_Internal_Rela *rel; |
8d88c4ca NC |
1495 | Elf_Internal_Rela *relend; |
1496 | ||
c434dee6 | 1497 | htab = elf64_x86_64_hash_table (info); |
8d88c4ca NC |
1498 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
1499 | sym_hashes = elf_sym_hashes (input_bfd); | |
1500 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
1501 | ||
c434dee6 | 1502 | rel = relocs; |
8d88c4ca | 1503 | relend = relocs + input_section->reloc_count; |
c434dee6 | 1504 | for (; rel < relend; rel++) |
8d88c4ca NC |
1505 | { |
1506 | int r_type; | |
1507 | reloc_howto_type *howto; | |
1508 | unsigned long r_symndx; | |
1509 | struct elf_link_hash_entry *h; | |
1510 | Elf_Internal_Sym *sym; | |
1511 | asection *sec; | |
c434dee6 | 1512 | bfd_vma off; |
8d88c4ca | 1513 | bfd_vma relocation; |
c434dee6 | 1514 | boolean unresolved_reloc; |
8d88c4ca | 1515 | bfd_reloc_status_type r; |
8d88c4ca | 1516 | |
c434dee6 | 1517 | r_type = ELF64_R_TYPE (rel->r_info); |
fe4770f4 AJ |
1518 | if (r_type == (int) R_X86_64_GNU_VTINHERIT |
1519 | || r_type == (int) R_X86_64_GNU_VTENTRY) | |
1520 | continue; | |
8d88c4ca | 1521 | |
c434dee6 | 1522 | if (r_type < 0 || r_type >= R_X86_64_max) |
8da6118f KH |
1523 | { |
1524 | bfd_set_error (bfd_error_bad_value); | |
1525 | return false; | |
1526 | } | |
c434dee6 | 1527 | howto = x86_64_elf_howto_table + r_type; |
8d88c4ca | 1528 | |
c434dee6 | 1529 | r_symndx = ELF64_R_SYM (rel->r_info); |
8d88c4ca NC |
1530 | |
1531 | if (info->relocateable) | |
8da6118f KH |
1532 | { |
1533 | /* This is a relocateable link. We don't have to change | |
1534 | anything, unless the reloc is against a section symbol, | |
1535 | in which case we have to adjust according to where the | |
1536 | section symbol winds up in the output section. */ | |
1537 | if (r_symndx < symtab_hdr->sh_info) | |
1538 | { | |
1539 | sym = local_syms + r_symndx; | |
1540 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
1541 | { | |
1542 | sec = local_sections[r_symndx]; | |
c434dee6 | 1543 | rel->r_addend += sec->output_offset + sym->st_value; |
8da6118f KH |
1544 | } |
1545 | } | |
1546 | ||
1547 | continue; | |
1548 | } | |
8d88c4ca | 1549 | |
407443a3 | 1550 | /* This is a final link. */ |
8d88c4ca NC |
1551 | h = NULL; |
1552 | sym = NULL; | |
1553 | sec = NULL; | |
c434dee6 | 1554 | unresolved_reloc = false; |
8d88c4ca | 1555 | if (r_symndx < symtab_hdr->sh_info) |
8da6118f KH |
1556 | { |
1557 | sym = local_syms + r_symndx; | |
1558 | sec = local_sections[r_symndx]; | |
c434dee6 AJ |
1559 | |
1560 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel); | |
8da6118f | 1561 | } |
8d88c4ca | 1562 | else |
8da6118f KH |
1563 | { |
1564 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1565 | while (h->root.type == bfd_link_hash_indirect | |
1566 | || h->root.type == bfd_link_hash_warning) | |
1567 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c434dee6 | 1568 | |
8da6118f KH |
1569 | if (h->root.type == bfd_link_hash_defined |
1570 | || h->root.type == bfd_link_hash_defweak) | |
1571 | { | |
1572 | sec = h->root.u.def.section; | |
c434dee6 | 1573 | if (sec->output_section == NULL) |
8da6118f | 1574 | { |
c434dee6 AJ |
1575 | /* Set a flag that will be cleared later if we find a |
1576 | relocation value for this symbol. output_section | |
1577 | is typically NULL for symbols satisfied by a shared | |
1578 | library. */ | |
1579 | unresolved_reloc = true; | |
8da6118f KH |
1580 | relocation = 0; |
1581 | } | |
1582 | else | |
1583 | relocation = (h->root.u.def.value | |
1584 | + sec->output_section->vma | |
1585 | + sec->output_offset); | |
1586 | } | |
1587 | else if (h->root.type == bfd_link_hash_undefweak) | |
1588 | relocation = 0; | |
671bae9c NC |
1589 | else if (info->shared |
1590 | && (!info->symbolic || info->allow_shlib_undefined) | |
1591 | && !info->no_undefined | |
70256ad8 AJ |
1592 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) |
1593 | relocation = 0; | |
8da6118f KH |
1594 | else |
1595 | { | |
1596 | if (! ((*info->callbacks->undefined_symbol) | |
1597 | (info, h->root.root.string, input_bfd, | |
c434dee6 | 1598 | input_section, rel->r_offset, |
8da6118f KH |
1599 | (!info->shared || info->no_undefined |
1600 | || ELF_ST_VISIBILITY (h->other))))) | |
1601 | return false; | |
1602 | relocation = 0; | |
1603 | } | |
1604 | } | |
70256ad8 AJ |
1605 | /* When generating a shared object, the relocations handled here are |
1606 | copied into the output file to be resolved at run time. */ | |
1607 | switch (r_type) | |
1608 | { | |
1609 | case R_X86_64_GOT32: | |
1610 | /* Relocation is to the entry for this symbol in the global | |
1611 | offset table. */ | |
70256ad8 AJ |
1612 | case R_X86_64_GOTPCREL: |
1613 | /* Use global offset table as symbol value. */ | |
c434dee6 AJ |
1614 | if (htab->sgot == NULL) |
1615 | abort (); | |
053579d7 | 1616 | |
51e0a107 | 1617 | if (h != NULL) |
70256ad8 | 1618 | { |
c434dee6 AJ |
1619 | boolean dyn; |
1620 | ||
1621 | off = h->got.offset; | |
1622 | dyn = htab->elf.dynamic_sections_created; | |
51e0a107 | 1623 | |
c434dee6 | 1624 | if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h) |
51e0a107 | 1625 | || (info->shared |
c434dee6 AJ |
1626 | && (info->symbolic |
1627 | || h->dynindx == -1 | |
1628 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)) | |
51e0a107 JH |
1629 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) |
1630 | { | |
1631 | /* This is actually a static link, or it is a -Bsymbolic | |
1632 | link and the symbol is defined locally, or the symbol | |
407443a3 | 1633 | was forced to be local because of a version file. We |
51e0a107 JH |
1634 | must initialize this entry in the global offset table. |
1635 | Since the offset must always be a multiple of 8, we | |
1636 | use the least significant bit to record whether we | |
1637 | have initialized it already. | |
1638 | ||
1639 | When doing a dynamic link, we create a .rela.got | |
407443a3 AJ |
1640 | relocation entry to initialize the value. This is |
1641 | done in the finish_dynamic_symbol routine. */ | |
51e0a107 JH |
1642 | if ((off & 1) != 0) |
1643 | off &= ~1; | |
1644 | else | |
1645 | { | |
1646 | bfd_put_64 (output_bfd, relocation, | |
c434dee6 | 1647 | htab->sgot->contents + off); |
51e0a107 JH |
1648 | h->got.offset |= 1; |
1649 | } | |
1650 | } | |
053579d7 | 1651 | else |
c434dee6 | 1652 | unresolved_reloc = false; |
70256ad8 | 1653 | } |
51e0a107 JH |
1654 | else |
1655 | { | |
c434dee6 AJ |
1656 | if (local_got_offsets == NULL) |
1657 | abort (); | |
51e0a107 JH |
1658 | |
1659 | off = local_got_offsets[r_symndx]; | |
1660 | ||
1661 | /* The offset must always be a multiple of 8. We use | |
407443a3 AJ |
1662 | the least significant bit to record whether we have |
1663 | already generated the necessary reloc. */ | |
51e0a107 JH |
1664 | if ((off & 1) != 0) |
1665 | off &= ~1; | |
1666 | else | |
1667 | { | |
c434dee6 AJ |
1668 | bfd_put_64 (output_bfd, relocation, |
1669 | htab->sgot->contents + off); | |
51e0a107 JH |
1670 | |
1671 | if (info->shared) | |
1672 | { | |
1673 | asection *srelgot; | |
1674 | Elf_Internal_Rela outrel; | |
c434dee6 | 1675 | Elf64_External_Rela *loc; |
70256ad8 | 1676 | |
51e0a107 JH |
1677 | /* We need to generate a R_X86_64_RELATIVE reloc |
1678 | for the dynamic linker. */ | |
c434dee6 AJ |
1679 | srelgot = htab->srelgot; |
1680 | if (srelgot == NULL) | |
1681 | abort (); | |
51e0a107 | 1682 | |
c434dee6 AJ |
1683 | outrel.r_offset = (htab->sgot->output_section->vma |
1684 | + htab->sgot->output_offset | |
51e0a107 JH |
1685 | + off); |
1686 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); | |
1687 | outrel.r_addend = relocation; | |
c434dee6 AJ |
1688 | loc = (Elf64_External_Rela *) srelgot->contents; |
1689 | loc += srelgot->reloc_count++; | |
1690 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); | |
51e0a107 JH |
1691 | } |
1692 | ||
1693 | local_got_offsets[r_symndx] |= 1; | |
1694 | } | |
51e0a107 | 1695 | } |
6a2bda3f | 1696 | |
c434dee6 AJ |
1697 | if (off >= (bfd_vma) -2) |
1698 | abort (); | |
1699 | ||
1700 | relocation = htab->sgot->output_offset + off; | |
1701 | if (r_type == R_X86_64_GOTPCREL) | |
1702 | relocation += htab->sgot->output_section->vma; | |
1703 | ||
70256ad8 AJ |
1704 | break; |
1705 | ||
1706 | case R_X86_64_PLT32: | |
1707 | /* Relocation is to the entry for this symbol in the | |
1708 | procedure linkage table. */ | |
1709 | ||
1710 | /* Resolve a PLT32 reloc against a local symbol directly, | |
407443a3 | 1711 | without using the procedure linkage table. */ |
70256ad8 AJ |
1712 | if (h == NULL) |
1713 | break; | |
1714 | ||
c434dee6 AJ |
1715 | if (h->plt.offset == (bfd_vma) -1 |
1716 | || htab->splt == NULL) | |
70256ad8 AJ |
1717 | { |
1718 | /* We didn't make a PLT entry for this symbol. This | |
407443a3 AJ |
1719 | happens when statically linking PIC code, or when |
1720 | using -Bsymbolic. */ | |
70256ad8 AJ |
1721 | break; |
1722 | } | |
1723 | ||
c434dee6 AJ |
1724 | relocation = (htab->splt->output_section->vma |
1725 | + htab->splt->output_offset | |
70256ad8 | 1726 | + h->plt.offset); |
c434dee6 | 1727 | unresolved_reloc = false; |
70256ad8 AJ |
1728 | break; |
1729 | ||
fd8ab9e5 AJ |
1730 | case R_X86_64_PC8: |
1731 | case R_X86_64_PC16: | |
1732 | case R_X86_64_PC32: | |
70256ad8 AJ |
1733 | case R_X86_64_8: |
1734 | case R_X86_64_16: | |
1735 | case R_X86_64_32: | |
6b3db546 | 1736 | case R_X86_64_64: |
80643fbc | 1737 | /* FIXME: The ABI says the linker should make sure the value is |
407443a3 | 1738 | the same when it's zeroextended to 64 bit. */ |
c434dee6 AJ |
1739 | |
1740 | /* r_symndx will be zero only for relocs against symbols | |
1741 | from removed linkonce sections, or sections discarded by | |
1742 | a linker script. */ | |
1743 | if (r_symndx == 0 | |
1744 | || (input_section->flags & SEC_ALLOC) == 0) | |
1745 | break; | |
1746 | ||
1747 | if ((info->shared | |
1748 | && ((r_type != R_X86_64_PC8 | |
1749 | && r_type != R_X86_64_PC16 | |
1750 | && r_type != R_X86_64_PC32) | |
1751 | || (h != NULL | |
1752 | && h->dynindx != -1 | |
1753 | && (! info->symbolic | |
1754 | || (h->elf_link_hash_flags | |
1755 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
1756 | || (!info->shared | |
1757 | && h != NULL | |
1758 | && h->dynindx != -1 | |
1759 | && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0 | |
1760 | && (((h->elf_link_hash_flags | |
1761 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1762 | && (h->elf_link_hash_flags | |
1763 | & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1764 | || h->root.type == bfd_link_hash_undefweak | |
1765 | || h->root.type == bfd_link_hash_undefined))) | |
70256ad8 AJ |
1766 | { |
1767 | Elf_Internal_Rela outrel; | |
1768 | boolean skip, relocate; | |
c434dee6 AJ |
1769 | asection *sreloc; |
1770 | Elf64_External_Rela *loc; | |
70256ad8 AJ |
1771 | |
1772 | /* When generating a shared object, these relocations | |
1773 | are copied into the output file to be resolved at run | |
407443a3 | 1774 | time. */ |
70256ad8 | 1775 | |
70256ad8 | 1776 | skip = false; |
0bb2d96a | 1777 | relocate = false; |
70256ad8 | 1778 | |
c629eae0 JJ |
1779 | outrel.r_offset = |
1780 | _bfd_elf_section_offset (output_bfd, info, input_section, | |
c434dee6 | 1781 | rel->r_offset); |
c629eae0 JJ |
1782 | if (outrel.r_offset == (bfd_vma) -1) |
1783 | skip = true; | |
0fb19cbc | 1784 | else if (outrel.r_offset == (bfd_vma) -2) |
0bb2d96a | 1785 | skip = true, relocate = true; |
70256ad8 AJ |
1786 | |
1787 | outrel.r_offset += (input_section->output_section->vma | |
1788 | + input_section->output_offset); | |
1789 | ||
1790 | if (skip) | |
0bb2d96a | 1791 | memset (&outrel, 0, sizeof outrel); |
c434dee6 | 1792 | |
fd8ab9e5 AJ |
1793 | /* h->dynindx may be -1 if this symbol was marked to |
1794 | become local. */ | |
1795 | else if (h != NULL | |
c434dee6 AJ |
1796 | && h->dynindx != -1 |
1797 | && (r_type == R_X86_64_PC8 | |
1798 | || r_type == R_X86_64_PC16 | |
1799 | || r_type == R_X86_64_PC32 | |
1800 | || !info->shared | |
1801 | || !info->symbolic | |
fd8ab9e5 AJ |
1802 | || (h->elf_link_hash_flags |
1803 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
70256ad8 | 1804 | { |
70256ad8 | 1805 | outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); |
c434dee6 | 1806 | outrel.r_addend = rel->r_addend; |
70256ad8 AJ |
1807 | } |
1808 | else | |
1809 | { | |
c434dee6 AJ |
1810 | /* This symbol is local, or marked to become local. */ |
1811 | relocate = true; | |
1812 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); | |
1813 | outrel.r_addend = relocation + rel->r_addend; | |
70256ad8 AJ |
1814 | } |
1815 | ||
c434dee6 AJ |
1816 | sreloc = elf_section_data (input_section)->sreloc; |
1817 | if (sreloc == NULL) | |
1818 | abort (); | |
1819 | ||
1820 | loc = (Elf64_External_Rela *) sreloc->contents; | |
1821 | loc += sreloc->reloc_count++; | |
1822 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); | |
70256ad8 AJ |
1823 | |
1824 | /* If this reloc is against an external symbol, we do | |
1825 | not want to fiddle with the addend. Otherwise, we | |
1826 | need to include the symbol value so that it becomes | |
1827 | an addend for the dynamic reloc. */ | |
1828 | if (! relocate) | |
1829 | continue; | |
1830 | } | |
1831 | ||
1832 | break; | |
1833 | ||
1834 | default: | |
1835 | break; | |
1836 | } | |
8d88c4ca | 1837 | |
c434dee6 AJ |
1838 | /* FIXME: Why do we allow debugging sections to escape this error? |
1839 | More importantly, why do we not emit dynamic relocs for | |
1840 | R_386_32 above in debugging sections (which are ! SEC_ALLOC)? | |
1841 | If we had emitted the dynamic reloc, we could remove the | |
1842 | fudge here. */ | |
1843 | if (unresolved_reloc | |
1844 | && !(info->shared | |
1845 | && (input_section->flags & SEC_DEBUGGING) != 0 | |
1846 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)) | |
1847 | (*_bfd_error_handler) | |
1848 | (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"), | |
1849 | bfd_archive_filename (input_bfd), | |
1850 | bfd_get_section_name (input_bfd, input_section), | |
1851 | (long) rel->r_offset, | |
1852 | h->root.root.string); | |
1853 | ||
8d88c4ca | 1854 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
c434dee6 AJ |
1855 | contents, rel->r_offset, |
1856 | relocation, rel->r_addend); | |
8d88c4ca NC |
1857 | |
1858 | if (r != bfd_reloc_ok) | |
8da6118f | 1859 | { |
c434dee6 AJ |
1860 | const char *name; |
1861 | ||
1862 | if (h != NULL) | |
1863 | name = h->root.root.string; | |
1864 | else | |
8da6118f | 1865 | { |
c434dee6 AJ |
1866 | name = bfd_elf_string_from_elf_section (input_bfd, |
1867 | symtab_hdr->sh_link, | |
1868 | sym->st_name); | |
1869 | if (name == NULL) | |
1870 | return false; | |
1871 | if (*name == '\0') | |
1872 | name = bfd_section_name (input_bfd, sec); | |
1873 | } | |
1874 | ||
1875 | if (r == bfd_reloc_overflow) | |
1876 | { | |
1877 | ||
1878 | if (! ((*info->callbacks->reloc_overflow) | |
1879 | (info, name, howto->name, (bfd_vma) 0, | |
1880 | input_bfd, input_section, rel->r_offset))) | |
1881 | return false; | |
1882 | } | |
1883 | else | |
1884 | { | |
1885 | (*_bfd_error_handler) | |
1886 | (_("%s(%s+0x%lx): reloc against `%s': error %d"), | |
1887 | bfd_archive_filename (input_bfd), | |
1888 | bfd_get_section_name (input_bfd, input_section), | |
1889 | (long) rel->r_offset, name, (int) r); | |
1890 | return false; | |
8da6118f KH |
1891 | } |
1892 | } | |
8d88c4ca | 1893 | } |
70256ad8 AJ |
1894 | |
1895 | return true; | |
1896 | } | |
1897 | ||
1898 | /* Finish up dynamic symbol handling. We set the contents of various | |
1899 | dynamic sections here. */ | |
1900 | ||
1901 | static boolean | |
1902 | elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym) | |
1903 | bfd *output_bfd; | |
1904 | struct bfd_link_info *info; | |
1905 | struct elf_link_hash_entry *h; | |
1906 | Elf_Internal_Sym *sym; | |
1907 | { | |
c434dee6 | 1908 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 | 1909 | |
c434dee6 | 1910 | htab = elf64_x86_64_hash_table (info); |
70256ad8 AJ |
1911 | |
1912 | if (h->plt.offset != (bfd_vma) -1) | |
1913 | { | |
70256ad8 AJ |
1914 | bfd_vma plt_index; |
1915 | bfd_vma got_offset; | |
1916 | Elf_Internal_Rela rela; | |
c434dee6 | 1917 | Elf64_External_Rela *loc; |
70256ad8 AJ |
1918 | |
1919 | /* This symbol has an entry in the procedure linkage table. Set | |
407443a3 | 1920 | it up. */ |
70256ad8 | 1921 | |
c434dee6 AJ |
1922 | if (h->dynindx == -1 |
1923 | || htab->splt == NULL | |
1924 | || htab->sgotplt == NULL | |
1925 | || htab->srelplt == NULL) | |
1926 | abort (); | |
70256ad8 AJ |
1927 | |
1928 | /* Get the index in the procedure linkage table which | |
1929 | corresponds to this symbol. This is the index of this symbol | |
1930 | in all the symbols for which we are making plt entries. The | |
1931 | first entry in the procedure linkage table is reserved. */ | |
1932 | plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; | |
1933 | ||
1934 | /* Get the offset into the .got table of the entry that | |
407443a3 | 1935 | corresponds to this function. Each .got entry is GOT_ENTRY_SIZE |
fe4770f4 | 1936 | bytes. The first three are reserved for the dynamic linker. */ |
70256ad8 AJ |
1937 | got_offset = (plt_index + 3) * GOT_ENTRY_SIZE; |
1938 | ||
1939 | /* Fill in the entry in the procedure linkage table. */ | |
c434dee6 | 1940 | memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry, |
70256ad8 AJ |
1941 | PLT_ENTRY_SIZE); |
1942 | ||
1943 | /* Insert the relocation positions of the plt section. The magic | |
1944 | numbers at the end of the statements are the positions of the | |
1945 | relocations in the plt section. */ | |
653165cc AJ |
1946 | /* Put offset for jmp *name@GOTPCREL(%rip), since the |
1947 | instruction uses 6 bytes, subtract this value. */ | |
1948 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
1949 | (htab->sgotplt->output_section->vma |
1950 | + htab->sgotplt->output_offset | |
653165cc | 1951 | + got_offset |
c434dee6 AJ |
1952 | - htab->splt->output_section->vma |
1953 | - htab->splt->output_offset | |
653165cc AJ |
1954 | - h->plt.offset |
1955 | - 6), | |
c434dee6 | 1956 | htab->splt->contents + h->plt.offset + 2); |
653165cc AJ |
1957 | /* Put relocation index. */ |
1958 | bfd_put_32 (output_bfd, plt_index, | |
c434dee6 | 1959 | htab->splt->contents + h->plt.offset + 7); |
653165cc AJ |
1960 | /* Put offset for jmp .PLT0. */ |
1961 | bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE), | |
c434dee6 | 1962 | htab->splt->contents + h->plt.offset + 12); |
70256ad8 | 1963 | |
653165cc AJ |
1964 | /* Fill in the entry in the global offset table, initially this |
1965 | points to the pushq instruction in the PLT which is at offset 6. */ | |
c434dee6 AJ |
1966 | bfd_put_64 (output_bfd, (htab->splt->output_section->vma |
1967 | + htab->splt->output_offset | |
70256ad8 | 1968 | + h->plt.offset + 6), |
c434dee6 | 1969 | htab->sgotplt->contents + got_offset); |
70256ad8 AJ |
1970 | |
1971 | /* Fill in the entry in the .rela.plt section. */ | |
c434dee6 AJ |
1972 | rela.r_offset = (htab->sgotplt->output_section->vma |
1973 | + htab->sgotplt->output_offset | |
70256ad8 AJ |
1974 | + got_offset); |
1975 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT); | |
1976 | rela.r_addend = 0; | |
c434dee6 AJ |
1977 | loc = (Elf64_External_Rela *) htab->srelplt->contents + plt_index; |
1978 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); | |
70256ad8 AJ |
1979 | |
1980 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1981 | { | |
1982 | /* Mark the symbol as undefined, rather than as defined in | |
c434dee6 AJ |
1983 | the .plt section. Leave the value alone. This is a clue |
1984 | for the dynamic linker, to make function pointer | |
1985 | comparisons work between an application and shared | |
1986 | library. */ | |
70256ad8 AJ |
1987 | sym->st_shndx = SHN_UNDEF; |
1988 | } | |
1989 | } | |
1990 | ||
053579d7 AJ |
1991 | if (h->got.offset != (bfd_vma) -1) |
1992 | { | |
053579d7 | 1993 | Elf_Internal_Rela rela; |
c434dee6 | 1994 | Elf64_External_Rela *loc; |
053579d7 AJ |
1995 | |
1996 | /* This symbol has an entry in the global offset table. Set it | |
1997 | up. */ | |
1998 | ||
c434dee6 AJ |
1999 | if (htab->sgot == NULL || htab->srelgot == NULL) |
2000 | abort (); | |
053579d7 | 2001 | |
c434dee6 AJ |
2002 | rela.r_offset = (htab->sgot->output_section->vma |
2003 | + htab->sgot->output_offset | |
dc810e39 | 2004 | + (h->got.offset &~ (bfd_vma) 1)); |
053579d7 AJ |
2005 | |
2006 | /* If this is a static link, or it is a -Bsymbolic link and the | |
2007 | symbol is defined locally or was forced to be local because | |
2008 | of a version file, we just want to emit a RELATIVE reloc. | |
2009 | The entry in the global offset table will already have been | |
2010 | initialized in the relocate_section function. */ | |
c434dee6 AJ |
2011 | if (info->shared |
2012 | && (info->symbolic | |
2013 | || h->dynindx == -1 | |
2014 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)) | |
2015 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)) | |
053579d7 | 2016 | { |
cc78d0af | 2017 | BFD_ASSERT((h->got.offset & 1) != 0); |
053579d7 AJ |
2018 | rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); |
2019 | rela.r_addend = (h->root.u.def.value | |
2020 | + h->root.u.def.section->output_section->vma | |
2021 | + h->root.u.def.section->output_offset); | |
2022 | } | |
2023 | else | |
2024 | { | |
2025 | BFD_ASSERT((h->got.offset & 1) == 0); | |
c434dee6 AJ |
2026 | bfd_put_64 (output_bfd, (bfd_vma) 0, |
2027 | htab->sgot->contents + h->got.offset); | |
053579d7 AJ |
2028 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT); |
2029 | rela.r_addend = 0; | |
2030 | } | |
2031 | ||
c434dee6 AJ |
2032 | loc = (Elf64_External_Rela *) htab->srelgot->contents; |
2033 | loc += htab->srelgot->reloc_count++; | |
2034 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); | |
053579d7 AJ |
2035 | } |
2036 | ||
70256ad8 AJ |
2037 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) |
2038 | { | |
70256ad8 | 2039 | Elf_Internal_Rela rela; |
c434dee6 | 2040 | Elf64_External_Rela *loc; |
70256ad8 AJ |
2041 | |
2042 | /* This symbol needs a copy reloc. Set it up. */ | |
2043 | ||
c434dee6 AJ |
2044 | if (h->dynindx == -1 |
2045 | || (h->root.type != bfd_link_hash_defined | |
2046 | && h->root.type != bfd_link_hash_defweak) | |
2047 | || htab->srelbss == NULL) | |
2048 | abort (); | |
70256ad8 AJ |
2049 | |
2050 | rela.r_offset = (h->root.u.def.value | |
2051 | + h->root.u.def.section->output_section->vma | |
2052 | + h->root.u.def.section->output_offset); | |
2053 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY); | |
2054 | rela.r_addend = 0; | |
c434dee6 AJ |
2055 | loc = (Elf64_External_Rela *) htab->srelbss->contents; |
2056 | loc += htab->srelbss->reloc_count++; | |
2057 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); | |
70256ad8 AJ |
2058 | } |
2059 | ||
2060 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ | |
2061 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
2062 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
2063 | sym->st_shndx = SHN_ABS; | |
2064 | ||
2065 | return true; | |
2066 | } | |
2067 | ||
c434dee6 AJ |
2068 | /* Used to decide how to sort relocs in an optimal manner for the |
2069 | dynamic linker, before writing them out. */ | |
2070 | ||
2071 | static enum elf_reloc_type_class | |
2072 | elf64_x86_64_reloc_type_class (rela) | |
2073 | const Elf_Internal_Rela *rela; | |
2074 | { | |
2075 | switch ((int) ELF64_R_TYPE (rela->r_info)) | |
2076 | { | |
2077 | case R_X86_64_RELATIVE: | |
2078 | return reloc_class_relative; | |
2079 | case R_X86_64_JUMP_SLOT: | |
2080 | return reloc_class_plt; | |
2081 | case R_X86_64_COPY: | |
2082 | return reloc_class_copy; | |
2083 | default: | |
2084 | return reloc_class_normal; | |
2085 | } | |
2086 | } | |
2087 | ||
70256ad8 AJ |
2088 | /* Finish up the dynamic sections. */ |
2089 | ||
2090 | static boolean | |
2091 | elf64_x86_64_finish_dynamic_sections (output_bfd, info) | |
2092 | bfd *output_bfd; | |
2093 | struct bfd_link_info *info; | |
2094 | { | |
c434dee6 | 2095 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
2096 | bfd *dynobj; |
2097 | asection *sdyn; | |
70256ad8 | 2098 | |
c434dee6 AJ |
2099 | htab = elf64_x86_64_hash_table (info); |
2100 | dynobj = htab->elf.dynobj; | |
70256ad8 AJ |
2101 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); |
2102 | ||
c434dee6 | 2103 | if (htab->elf.dynamic_sections_created) |
70256ad8 | 2104 | { |
70256ad8 AJ |
2105 | Elf64_External_Dyn *dyncon, *dynconend; |
2106 | ||
c434dee6 AJ |
2107 | if (sdyn == NULL || htab->sgot == NULL) |
2108 | abort (); | |
70256ad8 AJ |
2109 | |
2110 | dyncon = (Elf64_External_Dyn *) sdyn->contents; | |
2111 | dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
2112 | for (; dyncon < dynconend; dyncon++) | |
2113 | { | |
2114 | Elf_Internal_Dyn dyn; | |
70256ad8 AJ |
2115 | asection *s; |
2116 | ||
2117 | bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); | |
2118 | ||
2119 | switch (dyn.d_tag) | |
2120 | { | |
2121 | default: | |
053579d7 | 2122 | continue; |
70256ad8 AJ |
2123 | |
2124 | case DT_PLTGOT: | |
c434dee6 AJ |
2125 | dyn.d_un.d_ptr = htab->sgot->output_section->vma; |
2126 | break; | |
70256ad8 AJ |
2127 | |
2128 | case DT_JMPREL: | |
c434dee6 AJ |
2129 | dyn.d_un.d_ptr = htab->srelplt->output_section->vma; |
2130 | break; | |
70256ad8 | 2131 | |
c434dee6 AJ |
2132 | case DT_PLTRELSZ: |
2133 | s = htab->srelplt->output_section; | |
2134 | if (s->_cooked_size != 0) | |
2135 | dyn.d_un.d_val = s->_cooked_size; | |
2136 | else | |
2137 | dyn.d_un.d_val = s->_raw_size; | |
70256ad8 AJ |
2138 | break; |
2139 | ||
2140 | case DT_RELASZ: | |
c434dee6 AJ |
2141 | /* The procedure linkage table relocs (DT_JMPREL) should |
2142 | not be included in the overall relocs (DT_RELA). | |
2143 | Therefore, we override the DT_RELASZ entry here to | |
2144 | make it not include the JMPREL relocs. Since the | |
2145 | linker script arranges for .rela.plt to follow all | |
2146 | other relocation sections, we don't have to worry | |
2147 | about changing the DT_RELA entry. */ | |
2148 | if (htab->srelplt != NULL) | |
70256ad8 | 2149 | { |
c434dee6 AJ |
2150 | s = htab->srelplt->output_section; |
2151 | if (s->_cooked_size != 0) | |
2152 | dyn.d_un.d_val -= s->_cooked_size; | |
2153 | else | |
2154 | dyn.d_un.d_val -= s->_raw_size; | |
70256ad8 AJ |
2155 | } |
2156 | break; | |
70256ad8 | 2157 | } |
c434dee6 | 2158 | |
70256ad8 AJ |
2159 | bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); |
2160 | } | |
2161 | ||
c434dee6 AJ |
2162 | /* Fill in the special first entry in the procedure linkage table. */ |
2163 | if (htab->splt && htab->splt->_raw_size > 0) | |
70256ad8 | 2164 | { |
653165cc | 2165 | /* Fill in the first entry in the procedure linkage table. */ |
c434dee6 AJ |
2166 | memcpy (htab->splt->contents, elf64_x86_64_plt0_entry, |
2167 | PLT_ENTRY_SIZE); | |
653165cc AJ |
2168 | /* Add offset for pushq GOT+8(%rip), since the instruction |
2169 | uses 6 bytes subtract this value. */ | |
2170 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
2171 | (htab->sgotplt->output_section->vma |
2172 | + htab->sgotplt->output_offset | |
653165cc | 2173 | + 8 |
c434dee6 AJ |
2174 | - htab->splt->output_section->vma |
2175 | - htab->splt->output_offset | |
653165cc | 2176 | - 6), |
c434dee6 | 2177 | htab->splt->contents + 2); |
653165cc AJ |
2178 | /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to |
2179 | the end of the instruction. */ | |
2180 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
2181 | (htab->sgotplt->output_section->vma |
2182 | + htab->sgotplt->output_offset | |
653165cc | 2183 | + 16 |
c434dee6 AJ |
2184 | - htab->splt->output_section->vma |
2185 | - htab->splt->output_offset | |
653165cc | 2186 | - 12), |
c434dee6 | 2187 | htab->splt->contents + 8); |
653165cc | 2188 | |
c434dee6 AJ |
2189 | elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize = |
2190 | PLT_ENTRY_SIZE; | |
70256ad8 | 2191 | } |
70256ad8 AJ |
2192 | } |
2193 | ||
c434dee6 | 2194 | if (htab->sgotplt) |
70256ad8 | 2195 | { |
c434dee6 AJ |
2196 | /* Fill in the first three entries in the global offset table. */ |
2197 | if (htab->sgotplt->_raw_size > 0) | |
2198 | { | |
2199 | /* Set the first entry in the global offset table to the address of | |
2200 | the dynamic section. */ | |
2201 | if (sdyn == NULL) | |
2202 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents); | |
2203 | else | |
2204 | bfd_put_64 (output_bfd, | |
2205 | sdyn->output_section->vma + sdyn->output_offset, | |
2206 | htab->sgotplt->contents); | |
2207 | /* Write GOT[1] and GOT[2], needed for the dynamic linker. */ | |
2208 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE); | |
2209 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2); | |
2210 | } | |
70256ad8 | 2211 | |
c434dee6 AJ |
2212 | elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = |
2213 | GOT_ENTRY_SIZE; | |
2214 | } | |
70256ad8 | 2215 | |
8d88c4ca NC |
2216 | return true; |
2217 | } | |
2218 | ||
8df9fc9d | 2219 | |
70256ad8 AJ |
2220 | #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec |
2221 | #define TARGET_LITTLE_NAME "elf64-x86-64" | |
2222 | #define ELF_ARCH bfd_arch_i386 | |
2223 | #define ELF_MACHINE_CODE EM_X86_64 | |
2224 | #define ELF_MAXPAGESIZE 0x100000 | |
2225 | ||
2226 | #define elf_backend_can_gc_sections 1 | |
51b64d56 | 2227 | #define elf_backend_can_refcount 1 |
70256ad8 AJ |
2228 | #define elf_backend_want_got_plt 1 |
2229 | #define elf_backend_plt_readonly 1 | |
2230 | #define elf_backend_want_plt_sym 0 | |
2231 | #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3) | |
2232 | #define elf_backend_plt_header_size PLT_ENTRY_SIZE | |
2233 | ||
2234 | #define elf_info_to_howto elf64_x86_64_info_to_howto | |
70256ad8 | 2235 | |
70256ad8 AJ |
2236 | #define bfd_elf64_bfd_link_hash_table_create \ |
2237 | elf64_x86_64_link_hash_table_create | |
407443a3 | 2238 | #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup |
70256ad8 AJ |
2239 | |
2240 | #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol | |
2241 | #define elf_backend_check_relocs elf64_x86_64_check_relocs | |
c434dee6 AJ |
2242 | #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol |
2243 | #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections | |
2244 | #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections | |
70256ad8 AJ |
2245 | #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol |
2246 | #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook | |
2247 | #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook | |
c434dee6 | 2248 | #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class |
70256ad8 AJ |
2249 | #define elf_backend_relocate_section elf64_x86_64_relocate_section |
2250 | #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections | |
407443a3 | 2251 | #define elf_backend_object_p elf64_x86_64_elf_object_p |
8d88c4ca NC |
2252 | |
2253 | #include "elf64-target.h" |