Commit | Line | Data |
---|---|---|
8d88c4ca | 1 | /* X86-64 specific support for 64-bit ELF |
70256ad8 | 2 | Copyright 2000, 2001 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" | |
23 | #include "libbfd.h" | |
24 | #include "elf-bfd.h" | |
25 | ||
26 | #include "elf/x86-64.h" | |
27 | ||
28 | /* We use only the RELA entries. */ | |
29 | #define USE_RELA | |
30 | ||
31 | /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */ | |
32 | #define MINUS_ONE (~ (bfd_vma) 0) | |
33 | ||
34 | /* The relocation "howto" table. Order of fields: | |
407443a3 AJ |
35 | type, size, bitsize, pc_relative, complain_on_overflow, |
36 | special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */ | |
70256ad8 AJ |
37 | static reloc_howto_type x86_64_elf_howto_table[] = |
38 | { | |
407443a3 AJ |
39 | HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont, |
40 | bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0x00000000, 0x00000000, false), | |
41 | HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield, | |
42 | bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE, false), | |
43 | HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed, | |
44 | bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff, true), | |
45 | HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed, | |
46 | bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff, false), | |
47 | HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed, | |
48 | bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff, true), | |
49 | HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield, | |
50 | bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff, false), | |
51 | HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield, | |
52 | bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE, MINUS_ONE, false), | |
407443a3 AJ |
53 | HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield, |
54 | bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE, MINUS_ONE, false), | |
4a73f21b AJ |
55 | HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield, |
56 | bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE, MINUS_ONE, false), | |
407443a3 AJ |
57 | HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed, |
58 | bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff, 0xffffffff, true), | |
59 | HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned, | |
60 | bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff, false), | |
61 | HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed, | |
62 | bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff, false), | |
63 | HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield, | |
64 | bfd_elf_generic_reloc, "R_X86_64_16", false, 0xffff, 0xffff, false), | |
65 | HOWTO(R_X86_64_PC16,0, 1, 16, true, 0, complain_overflow_bitfield, | |
66 | bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0xffff, 0xffff, true), | |
67 | HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_signed, | |
68 | bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false), | |
69 | HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed, | |
70 | bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true) | |
8d88c4ca NC |
71 | }; |
72 | ||
73 | /* Map BFD relocs to the x86_64 elf relocs. */ | |
70256ad8 AJ |
74 | struct elf_reloc_map |
75 | { | |
8d88c4ca NC |
76 | bfd_reloc_code_real_type bfd_reloc_val; |
77 | unsigned char elf_reloc_val; | |
78 | }; | |
79 | ||
80 | static CONST struct elf_reloc_map x86_64_reloc_map[] = | |
81 | { | |
70256ad8 AJ |
82 | { BFD_RELOC_NONE, R_X86_64_NONE, }, |
83 | { BFD_RELOC_64, R_X86_64_64, }, | |
84 | { BFD_RELOC_32_PCREL, R_X86_64_PC32, }, | |
85 | { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,}, | |
86 | { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,}, | |
87 | { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, }, | |
88 | { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, }, | |
89 | { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, }, | |
90 | { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, }, | |
91 | { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, }, | |
92 | { BFD_RELOC_32, R_X86_64_32, }, | |
93 | { BFD_RELOC_X86_64_32S, R_X86_64_32S, }, | |
94 | { BFD_RELOC_16, R_X86_64_16, }, | |
95 | { BFD_RELOC_16_PCREL, R_X86_64_PC16, }, | |
96 | { BFD_RELOC_8, R_X86_64_8, }, | |
97 | { BFD_RELOC_8_PCREL, R_X86_64_PC8, }, | |
8d88c4ca NC |
98 | }; |
99 | ||
8d88c4ca NC |
100 | static reloc_howto_type *elf64_x86_64_reloc_type_lookup |
101 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
102 | static void elf64_x86_64_info_to_howto | |
103 | PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *)); | |
104 | static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create | |
105 | PARAMS ((bfd *)); | |
70256ad8 AJ |
106 | |
107 | static struct bfd_hash_entry *elf64_x86_64_link_hash_newfunc | |
108 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
109 | static boolean elf64_x86_64_adjust_dynamic_symbol | |
110 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
111 | ||
112 | static boolean elf64_x86_64_size_dynamic_sections | |
113 | PARAMS ((bfd *, struct bfd_link_info *)); | |
8d88c4ca NC |
114 | static boolean elf64_x86_64_relocate_section |
115 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
407443a3 | 116 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); |
70256ad8 AJ |
117 | static boolean elf64_x86_64_finish_dynamic_symbol |
118 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, | |
119 | Elf_Internal_Sym *sym)); | |
120 | static boolean elf64_x86_64_finish_dynamic_sections | |
121 | PARAMS ((bfd *, struct bfd_link_info *)); | |
8d88c4ca NC |
122 | |
123 | /* Given a BFD reloc type, return a HOWTO structure. */ | |
124 | static reloc_howto_type * | |
125 | elf64_x86_64_reloc_type_lookup (abfd, code) | |
126 | bfd *abfd ATTRIBUTE_UNUSED; | |
127 | bfd_reloc_code_real_type code; | |
128 | { | |
129 | unsigned int i; | |
130 | for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map); | |
131 | i++) | |
132 | { | |
133 | if (x86_64_reloc_map[i].bfd_reloc_val == code) | |
8da6118f KH |
134 | return &x86_64_elf_howto_table[(int) |
135 | x86_64_reloc_map[i].elf_reloc_val]; | |
8d88c4ca NC |
136 | } |
137 | return 0; | |
138 | } | |
139 | ||
8d88c4ca | 140 | /* Given an x86_64 ELF reloc type, fill in an arelent structure. */ |
8da6118f | 141 | |
8d88c4ca NC |
142 | static void |
143 | elf64_x86_64_info_to_howto (abfd, cache_ptr, dst) | |
144 | bfd *abfd ATTRIBUTE_UNUSED; | |
145 | arelent *cache_ptr; | |
146 | Elf64_Internal_Rela *dst; | |
147 | { | |
148 | unsigned r_type; | |
149 | ||
150 | r_type = ELF64_R_TYPE (dst->r_info); | |
151 | BFD_ASSERT (r_type < (unsigned int) R_X86_64_max); | |
152 | cache_ptr->howto = &x86_64_elf_howto_table[r_type]; | |
153 | BFD_ASSERT (r_type == cache_ptr->howto->type); | |
154 | } | |
70256ad8 | 155 | \f |
407443a3 | 156 | /* Functions for the x86-64 ELF linker. */ |
70256ad8 | 157 | |
407443a3 | 158 | /* The name of the dynamic interpreter. This is put in the .interp |
70256ad8 AJ |
159 | section. */ |
160 | ||
407443a3 | 161 | #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1" |
70256ad8 AJ |
162 | |
163 | /* The size in bytes of an entry in the global offset table. */ | |
164 | ||
165 | #define GOT_ENTRY_SIZE 8 | |
8d88c4ca | 166 | |
70256ad8 | 167 | /* The size in bytes of an entry in the procedure linkage table. */ |
8d88c4ca | 168 | |
70256ad8 AJ |
169 | #define PLT_ENTRY_SIZE 16 |
170 | ||
171 | /* The first entry in a procedure linkage table looks like this. See the | |
172 | SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */ | |
173 | ||
174 | static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] = | |
175 | { | |
653165cc AJ |
176 | 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */ |
177 | 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */ | |
178 | 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */ | |
70256ad8 AJ |
179 | }; |
180 | ||
181 | /* Subsequent entries in a procedure linkage table look like this. */ | |
182 | ||
183 | static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] = | |
184 | { | |
653165cc | 185 | 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */ |
407443a3 | 186 | 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */ |
653165cc | 187 | 0x68, /* pushq immediate */ |
70256ad8 AJ |
188 | 0, 0, 0, 0, /* replaced with index into relocation table. */ |
189 | 0xe9, /* jmp relative */ | |
190 | 0, 0, 0, 0 /* replaced with offset to start of .plt0. */ | |
191 | }; | |
192 | ||
193 | /* The x86-64 linker needs to keep track of the number of relocs that | |
407443a3 | 194 | it decides to copy in check_relocs for each symbol. This is so |
70256ad8 AJ |
195 | that it can discard PC relative relocs if it doesn't need them when |
196 | linking with -Bsymbolic. We store the information in a field | |
407443a3 | 197 | extending the regular ELF linker hash table. */ |
70256ad8 AJ |
198 | |
199 | /* This structure keeps track of the number of PC relative relocs we | |
200 | have copied for a given symbol. */ | |
201 | ||
202 | struct elf64_x86_64_pcrel_relocs_copied | |
203 | { | |
204 | /* Next section. */ | |
205 | struct elf64_x86_64_pcrel_relocs_copied *next; | |
206 | /* A section in dynobj. */ | |
207 | asection *section; | |
208 | /* Number of relocs copied in this section. */ | |
209 | bfd_size_type count; | |
210 | }; | |
211 | ||
212 | /* x86-64 ELF linker hash entry. */ | |
213 | ||
214 | struct elf64_x86_64_link_hash_entry | |
215 | { | |
216 | struct elf_link_hash_entry root; | |
217 | ||
218 | /* Number of PC relative relocs copied for this symbol. */ | |
219 | struct elf64_x86_64_pcrel_relocs_copied *pcrel_relocs_copied; | |
220 | }; | |
221 | ||
222 | /* x86-64 ELF linker hash table. */ | |
8d88c4ca | 223 | |
407443a3 AJ |
224 | struct elf64_x86_64_link_hash_table |
225 | { | |
8d88c4ca NC |
226 | struct elf_link_hash_table root; |
227 | }; | |
228 | ||
70256ad8 AJ |
229 | /* Declare this now that the above structures are defined. */ |
230 | ||
231 | static boolean elf64_x86_64_discard_copies | |
232 | PARAMS ((struct elf64_x86_64_link_hash_entry *, PTR)); | |
233 | ||
234 | /* Traverse an x86-64 ELF linker hash table. */ | |
235 | ||
236 | #define elf64_x86_64_link_hash_traverse(table, func, info) \ | |
237 | (elf_link_hash_traverse \ | |
238 | (&(table)->root, \ | |
239 | (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ | |
240 | (info))) | |
241 | ||
242 | /* Get the x86-64 ELF linker hash table from a link_info structure. */ | |
8d88c4ca NC |
243 | |
244 | #define elf64_x86_64_hash_table(p) \ | |
245 | ((struct elf64_x86_64_link_hash_table *) ((p)->hash)) | |
246 | ||
407443a3 | 247 | /* Create an entry in an x86-64 ELF linker hash table. */ |
70256ad8 AJ |
248 | |
249 | static struct bfd_hash_entry * | |
250 | elf64_x86_64_link_hash_newfunc (entry, table, string) | |
251 | struct bfd_hash_entry *entry; | |
252 | struct bfd_hash_table *table; | |
253 | const char *string; | |
254 | { | |
255 | struct elf64_x86_64_link_hash_entry *ret = | |
256 | (struct elf64_x86_64_link_hash_entry *) entry; | |
257 | ||
258 | /* Allocate the structure if it has not already been allocated by a | |
407443a3 | 259 | subclass. */ |
70256ad8 AJ |
260 | if (ret == (struct elf64_x86_64_link_hash_entry *) NULL) |
261 | ret = ((struct elf64_x86_64_link_hash_entry *) | |
262 | bfd_hash_allocate (table, | |
263 | sizeof (struct elf64_x86_64_link_hash_entry))); | |
264 | if (ret == (struct elf64_x86_64_link_hash_entry *) NULL) | |
265 | return (struct bfd_hash_entry *) ret; | |
266 | ||
267 | /* Call the allocation method of the superclass. */ | |
268 | ret = ((struct elf64_x86_64_link_hash_entry *) | |
269 | _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
270 | table, string)); | |
271 | if (ret != (struct elf64_x86_64_link_hash_entry *) NULL) | |
272 | { | |
273 | ret->pcrel_relocs_copied = NULL; | |
274 | } | |
275 | ||
276 | return (struct bfd_hash_entry *) ret; | |
277 | } | |
278 | ||
8d88c4ca NC |
279 | /* Create an X86-64 ELF linker hash table. */ |
280 | ||
281 | static struct bfd_link_hash_table * | |
282 | elf64_x86_64_link_hash_table_create (abfd) | |
283 | bfd *abfd; | |
284 | { | |
285 | struct elf64_x86_64_link_hash_table *ret; | |
286 | ||
287 | ret = ((struct elf64_x86_64_link_hash_table *) | |
8da6118f | 288 | bfd_alloc (abfd, sizeof (struct elf64_x86_64_link_hash_table))); |
8d88c4ca NC |
289 | if (ret == (struct elf64_x86_64_link_hash_table *) NULL) |
290 | return NULL; | |
291 | ||
292 | if (! _bfd_elf_link_hash_table_init (&ret->root, abfd, | |
70256ad8 | 293 | elf64_x86_64_link_hash_newfunc)) |
8d88c4ca NC |
294 | { |
295 | bfd_release (abfd, ret); | |
296 | return NULL; | |
297 | } | |
298 | ||
299 | return &ret->root.root; | |
300 | } | |
301 | ||
302 | boolean | |
303 | elf64_x86_64_elf_object_p (abfd) | |
304 | bfd *abfd; | |
305 | { | |
306 | /* Set the right machine number for an x86-64 elf64 file. */ | |
307 | bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64); | |
308 | return true; | |
309 | } | |
310 | ||
70256ad8 AJ |
311 | /* Look through the relocs for a section during the first phase, and |
312 | allocate space in the global offset table or procedure linkage | |
313 | table. */ | |
314 | ||
315 | static boolean | |
316 | elf64_x86_64_check_relocs (abfd, info, sec, relocs) | |
317 | bfd *abfd; | |
318 | struct bfd_link_info *info; | |
319 | asection *sec; | |
320 | const Elf_Internal_Rela *relocs; | |
321 | { | |
322 | bfd *dynobj; | |
323 | Elf_Internal_Shdr *symtab_hdr; | |
324 | struct elf_link_hash_entry **sym_hashes; | |
325 | bfd_signed_vma *local_got_refcounts; | |
326 | const Elf_Internal_Rela *rel; | |
327 | const Elf_Internal_Rela *rel_end; | |
328 | asection *sgot; | |
329 | asection *srelgot; | |
330 | asection *sreloc; | |
331 | ||
332 | if (info->relocateable) | |
333 | return true; | |
334 | ||
335 | dynobj = elf_hash_table (info)->dynobj; | |
336 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
337 | sym_hashes = elf_sym_hashes (abfd); | |
338 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
339 | ||
340 | sgot = srelgot = sreloc = NULL; | |
341 | rel_end = relocs + sec->reloc_count; | |
342 | for (rel = relocs; rel < rel_end; rel++) | |
343 | { | |
344 | unsigned long r_symndx; | |
345 | struct elf_link_hash_entry *h; | |
346 | ||
347 | r_symndx = ELF64_R_SYM (rel->r_info); | |
348 | if (r_symndx < symtab_hdr->sh_info) | |
349 | h = NULL; | |
350 | else | |
351 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
352 | ||
353 | /* Some relocs require a global offset table. */ | |
354 | if (dynobj == NULL) | |
355 | { | |
356 | switch (ELF64_R_TYPE (rel->r_info)) | |
357 | { | |
358 | case R_X86_64_GOT32: | |
359 | case R_X86_64_GOTPCREL: | |
360 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
361 | if (! _bfd_elf_create_got_section (dynobj, info)) | |
362 | return false; | |
363 | break; | |
364 | } | |
365 | } | |
366 | ||
367 | switch (ELF64_R_TYPE (rel->r_info)) | |
368 | { | |
51e0a107 | 369 | case R_X86_64_GOTPCREL: |
70256ad8 | 370 | case R_X86_64_GOT32: |
407443a3 | 371 | /* This symbol requires a global offset table entry. */ |
70256ad8 AJ |
372 | |
373 | if (sgot == NULL) | |
374 | { | |
375 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
376 | BFD_ASSERT (sgot != NULL); | |
377 | } | |
378 | ||
379 | if (srelgot == NULL && (h != NULL || info->shared)) | |
380 | { | |
381 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
382 | if (srelgot == NULL) | |
383 | { | |
384 | srelgot = bfd_make_section (dynobj, ".rela.got"); | |
385 | if (srelgot == NULL | |
386 | || ! bfd_set_section_flags (dynobj, srelgot, | |
387 | (SEC_ALLOC | |
388 | | SEC_LOAD | |
389 | | SEC_HAS_CONTENTS | |
390 | | SEC_IN_MEMORY | |
391 | | SEC_LINKER_CREATED | |
392 | | SEC_READONLY)) | |
51e0a107 | 393 | || ! bfd_set_section_alignment (dynobj, srelgot, 3)) |
70256ad8 AJ |
394 | return false; |
395 | } | |
396 | } | |
397 | ||
398 | if (h != NULL) | |
399 | { | |
400 | if (h->got.refcount == -1) | |
401 | { | |
402 | h->got.refcount = 1; | |
403 | ||
404 | /* Make sure this symbol is output as a dynamic symbol. */ | |
405 | if (h->dynindx == -1) | |
406 | { | |
407 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
408 | return false; | |
409 | } | |
410 | ||
411 | sgot->_raw_size += GOT_ENTRY_SIZE; | |
412 | srelgot->_raw_size += sizeof (Elf64_External_Rela); | |
413 | } | |
414 | else | |
415 | h->got.refcount += 1; | |
416 | } | |
417 | else | |
418 | { | |
419 | /* This is a global offset table entry for a local symbol. */ | |
420 | if (local_got_refcounts == NULL) | |
421 | { | |
422 | size_t size; | |
423 | ||
424 | size = symtab_hdr->sh_info * sizeof (bfd_signed_vma); | |
425 | local_got_refcounts = ((bfd_signed_vma *) | |
426 | bfd_alloc (abfd, size)); | |
427 | if (local_got_refcounts == NULL) | |
428 | return false; | |
429 | elf_local_got_refcounts (abfd) = local_got_refcounts; | |
430 | memset (local_got_refcounts, -1, size); | |
431 | } | |
432 | if (local_got_refcounts[r_symndx] == -1) | |
433 | { | |
434 | local_got_refcounts[r_symndx] = 1; | |
435 | ||
436 | sgot->_raw_size += GOT_ENTRY_SIZE; | |
437 | if (info->shared) | |
438 | { | |
439 | /* If we are generating a shared object, we need to | |
440 | output a R_X86_64_RELATIVE reloc so that the dynamic | |
441 | linker can adjust this GOT entry. */ | |
442 | srelgot->_raw_size += sizeof (Elf64_External_Rela); | |
443 | } | |
444 | } | |
445 | else | |
446 | local_got_refcounts[r_symndx] += 1; | |
447 | } | |
448 | break; | |
449 | ||
450 | case R_X86_64_PLT32: | |
451 | /* This symbol requires a procedure linkage table entry. We | |
407443a3 AJ |
452 | actually build the entry in adjust_dynamic_symbol, |
453 | because this might be a case of linking PIC code which is | |
454 | never referenced by a dynamic object, in which case we | |
455 | don't need to generate a procedure linkage table entry | |
456 | after all. */ | |
70256ad8 AJ |
457 | |
458 | /* If this is a local symbol, we resolve it directly without | |
407443a3 | 459 | creating a procedure linkage table entry. */ |
70256ad8 AJ |
460 | if (h == NULL) |
461 | continue; | |
462 | ||
463 | if (h->plt.refcount == -1) | |
464 | { | |
465 | h->plt.refcount = 1; | |
466 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; | |
467 | } | |
468 | else | |
469 | h->plt.refcount += 1; | |
470 | break; | |
471 | ||
472 | case R_X86_64_32: | |
473 | case R_X86_64_32S: | |
474 | case R_X86_64_PC32: | |
475 | if (h != NULL) | |
476 | h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF; | |
477 | ||
478 | /* If we are creating a shared library, and this is a reloc | |
479 | against a global symbol, or a non PC relative reloc | |
480 | against a local symbol, then we need to copy the reloc | |
481 | into the shared library. However, if we are linking with | |
482 | -Bsymbolic, we do not need to copy a reloc against a | |
483 | global symbol which is defined in an object we are | |
407443a3 | 484 | including in the link (i.e., DEF_REGULAR is set). At |
70256ad8 AJ |
485 | this point we have not seen all the input files, so it is |
486 | possible that DEF_REGULAR is not set now but will be set | |
487 | later (it is never cleared). We account for that | |
488 | possibility below by storing information in the | |
489 | pcrel_relocs_copied field of the hash table entry. | |
490 | A similar situation occurs when creating shared libraries | |
491 | and symbol visibility changes render the symbol local. */ | |
492 | if (info->shared | |
493 | && (sec->flags & SEC_ALLOC) != 0 | |
494 | && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32 | |
495 | || (h != NULL | |
496 | && (! info->symbolic | |
497 | || (h->elf_link_hash_flags | |
498 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
499 | { | |
500 | /* When creating a shared object, we must copy these | |
501 | reloc types into the output file. We create a reloc | |
502 | section in dynobj and make room for this reloc. */ | |
503 | if (sreloc == NULL) | |
504 | { | |
505 | const char *name; | |
506 | ||
507 | name = (bfd_elf_string_from_elf_section | |
508 | (abfd, | |
509 | elf_elfheader (abfd)->e_shstrndx, | |
510 | elf_section_data (sec)->rel_hdr.sh_name)); | |
511 | if (name == NULL) | |
512 | return false; | |
513 | ||
514 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
515 | && strcmp (bfd_get_section_name (abfd, sec), | |
516 | name + 5) == 0); | |
517 | ||
518 | sreloc = bfd_get_section_by_name (dynobj, name); | |
519 | if (sreloc == NULL) | |
520 | { | |
521 | flagword flags; | |
522 | ||
523 | sreloc = bfd_make_section (dynobj, name); | |
524 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | |
525 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
526 | if ((sec->flags & SEC_ALLOC) != 0) | |
527 | flags |= SEC_ALLOC | SEC_LOAD; | |
528 | if (sreloc == NULL | |
529 | || ! bfd_set_section_flags (dynobj, sreloc, flags) | |
530 | || ! bfd_set_section_alignment (dynobj, sreloc, 2)) | |
531 | return false; | |
532 | } | |
533 | } | |
534 | ||
535 | sreloc->_raw_size += sizeof (Elf64_External_Rela); | |
536 | ||
537 | /* If this is a global symbol, we count the number of PC | |
538 | relative relocations we have entered for this symbol, | |
539 | so that we can discard them later as necessary. Note | |
540 | that this function is only called if we are using an | |
541 | elf64_x86_64 linker hash table, which means that h is | |
542 | really a pointer to an elf64_x86_64_link_hash_entry. */ | |
543 | if (h != NULL | |
544 | && ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32) | |
545 | { | |
546 | struct elf64_x86_64_link_hash_entry *eh; | |
547 | struct elf64_x86_64_pcrel_relocs_copied *p; | |
548 | ||
549 | eh = (struct elf64_x86_64_link_hash_entry *) h; | |
550 | ||
551 | for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next) | |
552 | if (p->section == sreloc) | |
553 | break; | |
554 | ||
555 | if (p == NULL) | |
556 | { | |
557 | p = ((struct elf64_x86_64_pcrel_relocs_copied *) | |
558 | bfd_alloc (dynobj, sizeof *p)); | |
559 | if (p == NULL) | |
560 | return false; | |
561 | p->next = eh->pcrel_relocs_copied; | |
562 | eh->pcrel_relocs_copied = p; | |
563 | p->section = sreloc; | |
564 | p->count = 0; | |
565 | } | |
566 | ||
567 | ++p->count; | |
568 | } | |
569 | } | |
570 | break; | |
571 | } | |
572 | } | |
573 | ||
574 | return true; | |
575 | } | |
576 | ||
577 | /* Return the section that should be marked against GC for a given | |
407443a3 | 578 | relocation. */ |
70256ad8 AJ |
579 | |
580 | static asection * | |
581 | elf64_x86_64_gc_mark_hook (abfd, info, rel, h, sym) | |
582 | bfd *abfd; | |
583 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
584 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED; | |
585 | struct elf_link_hash_entry *h; | |
586 | Elf_Internal_Sym *sym; | |
587 | { | |
588 | if (h != NULL) | |
589 | { | |
590 | switch (h->root.type) | |
591 | { | |
592 | case bfd_link_hash_defined: | |
593 | case bfd_link_hash_defweak: | |
594 | return h->root.u.def.section; | |
595 | ||
596 | case bfd_link_hash_common: | |
597 | return h->root.u.c.p->section; | |
598 | ||
599 | default: | |
600 | break; | |
601 | } | |
602 | } | |
603 | else | |
604 | { | |
605 | if (!(elf_bad_symtab (abfd) | |
606 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
607 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) | |
608 | && sym->st_shndx != SHN_COMMON)) | |
609 | { | |
610 | return bfd_section_from_elf_index (abfd, sym->st_shndx); | |
611 | } | |
612 | } | |
613 | ||
614 | return NULL; | |
615 | } | |
616 | ||
407443a3 | 617 | /* Update the got entry reference counts for the section being removed. */ |
70256ad8 AJ |
618 | |
619 | static boolean | |
620 | elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs) | |
621 | bfd *abfd; | |
622 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
623 | asection *sec; | |
624 | const Elf_Internal_Rela *relocs; | |
625 | { | |
626 | Elf_Internal_Shdr *symtab_hdr; | |
627 | struct elf_link_hash_entry **sym_hashes; | |
628 | bfd_signed_vma *local_got_refcounts; | |
629 | const Elf_Internal_Rela *rel, *relend; | |
630 | unsigned long r_symndx; | |
631 | struct elf_link_hash_entry *h; | |
632 | bfd *dynobj; | |
633 | asection *sgot; | |
634 | asection *srelgot; | |
635 | ||
636 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
637 | sym_hashes = elf_sym_hashes (abfd); | |
638 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
639 | ||
640 | dynobj = elf_hash_table (info)->dynobj; | |
641 | if (dynobj == NULL) | |
642 | return true; | |
643 | ||
644 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
645 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
646 | ||
647 | relend = relocs + sec->reloc_count; | |
648 | for (rel = relocs; rel < relend; rel++) | |
649 | switch (ELF64_R_TYPE (rel->r_info)) | |
650 | { | |
651 | case R_X86_64_GOT32: | |
652 | case R_X86_64_GOTPCREL: | |
653 | r_symndx = ELF64_R_SYM (rel->r_info); | |
654 | if (r_symndx >= symtab_hdr->sh_info) | |
655 | { | |
656 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
657 | if (h->got.refcount > 0) | |
658 | { | |
659 | h->got.refcount -= 1; | |
660 | if (h->got.refcount == 0) | |
661 | { | |
662 | sgot->_raw_size -= GOT_ENTRY_SIZE; | |
663 | srelgot->_raw_size -= sizeof (Elf64_External_Rela); | |
664 | } | |
665 | } | |
666 | } | |
667 | else if (local_got_refcounts != NULL) | |
668 | { | |
669 | if (local_got_refcounts[r_symndx] > 0) | |
670 | { | |
671 | local_got_refcounts[r_symndx] -= 1; | |
672 | if (local_got_refcounts[r_symndx] == 0) | |
673 | { | |
674 | sgot->_raw_size -= GOT_ENTRY_SIZE; | |
675 | if (info->shared) | |
676 | srelgot->_raw_size -= sizeof (Elf64_External_Rela); | |
677 | } | |
678 | } | |
679 | } | |
680 | break; | |
681 | ||
682 | case R_X86_64_PLT32: | |
683 | r_symndx = ELF64_R_SYM (rel->r_info); | |
684 | if (r_symndx >= symtab_hdr->sh_info) | |
685 | { | |
686 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
687 | if (h->plt.refcount > 0) | |
688 | h->plt.refcount -= 1; | |
689 | } | |
690 | break; | |
691 | ||
692 | default: | |
693 | break; | |
694 | } | |
695 | ||
696 | return true; | |
697 | } | |
698 | ||
699 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
700 | regular object. The current definition is in some section of the | |
701 | dynamic object, but we're not including those sections. We have to | |
702 | change the definition to something the rest of the link can | |
407443a3 | 703 | understand. */ |
70256ad8 AJ |
704 | |
705 | static boolean | |
706 | elf64_x86_64_adjust_dynamic_symbol (info, h) | |
707 | struct bfd_link_info *info; | |
708 | struct elf_link_hash_entry *h; | |
709 | { | |
710 | bfd *dynobj; | |
711 | asection *s; | |
712 | unsigned int power_of_two; | |
713 | ||
714 | dynobj = elf_hash_table (info)->dynobj; | |
715 | ||
716 | /* Make sure we know what is going on here. */ | |
717 | BFD_ASSERT (dynobj != NULL | |
718 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) | |
719 | || h->weakdef != NULL | |
720 | || ((h->elf_link_hash_flags | |
721 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
722 | && (h->elf_link_hash_flags | |
723 | & ELF_LINK_HASH_REF_REGULAR) != 0 | |
724 | && (h->elf_link_hash_flags | |
725 | & ELF_LINK_HASH_DEF_REGULAR) == 0))); | |
726 | ||
727 | /* If this is a function, put it in the procedure linkage table. We | |
728 | will fill in the contents of the procedure linkage table later, | |
729 | when we know the address of the .got section. */ | |
730 | if (h->type == STT_FUNC | |
731 | || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) | |
732 | { | |
407443a3 AJ |
733 | if ((! info->shared |
734 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
735 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0) | |
736 | || (info->shared && h->plt.refcount <= 0)) | |
70256ad8 | 737 | { |
70256ad8 AJ |
738 | /* This case can occur if we saw a PLT32 reloc in an input |
739 | file, but the symbol was never referred to by a dynamic | |
740 | object, or if all references were garbage collected. In | |
741 | such a case, we don't actually need to build a procedure | |
742 | linkage table, and we can just do a PC32 reloc instead. */ | |
70256ad8 AJ |
743 | h->plt.offset = (bfd_vma) -1; |
744 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
745 | return true; | |
746 | } | |
747 | ||
748 | /* Make sure this symbol is output as a dynamic symbol. */ | |
749 | if (h->dynindx == -1) | |
750 | { | |
751 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
752 | return false; | |
753 | } | |
754 | ||
755 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
756 | BFD_ASSERT (s != NULL); | |
757 | ||
758 | /* If this is the first .plt entry, make room for the special | |
759 | first entry. */ | |
760 | if (s->_raw_size == 0) | |
761 | s->_raw_size = PLT_ENTRY_SIZE; | |
762 | ||
763 | /* If this symbol is not defined in a regular file, and we are | |
764 | not generating a shared library, then set the symbol to this | |
407443a3 | 765 | location in the .plt. This is required to make function |
70256ad8 AJ |
766 | pointers compare as equal between the normal executable and |
767 | the shared library. */ | |
768 | if (! info->shared | |
769 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
770 | { | |
771 | h->root.u.def.section = s; | |
772 | h->root.u.def.value = s->_raw_size; | |
773 | } | |
774 | ||
775 | h->plt.offset = s->_raw_size; | |
776 | ||
777 | /* Make room for this entry. */ | |
778 | s->_raw_size += PLT_ENTRY_SIZE; | |
779 | ||
407443a3 AJ |
780 | /* We also need to make an entry in the .got.plt section, which |
781 | will be placed in the .got section by the linker script. */ | |
782 | s = bfd_get_section_by_name (dynobj, ".got.plt"); | |
783 | BFD_ASSERT (s != NULL); | |
784 | s->_raw_size += GOT_ENTRY_SIZE; | |
785 | ||
70256ad8 AJ |
786 | /* We also need to make an entry in the .rela.plt section. */ |
787 | s = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
788 | BFD_ASSERT (s != NULL); | |
789 | s->_raw_size += sizeof (Elf64_External_Rela); | |
790 | ||
791 | return true; | |
792 | } | |
793 | ||
794 | /* If this is a weak symbol, and there is a real definition, the | |
795 | processor independent code will have arranged for us to see the | |
407443a3 | 796 | real definition first, and we can just use the same value. */ |
70256ad8 AJ |
797 | if (h->weakdef != NULL) |
798 | { | |
799 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined | |
800 | || h->weakdef->root.type == bfd_link_hash_defweak); | |
801 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
802 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
803 | return true; | |
804 | } | |
805 | ||
806 | /* This is a reference to a symbol defined by a dynamic object which | |
407443a3 | 807 | is not a function. */ |
70256ad8 AJ |
808 | |
809 | /* If we are creating a shared library, we must presume that the | |
810 | only references to the symbol are via the global offset table. | |
811 | For such cases we need not do anything here; the relocations will | |
407443a3 | 812 | be handled correctly by relocate_section. */ |
70256ad8 AJ |
813 | if (info->shared) |
814 | return true; | |
815 | ||
816 | /* If there are no references to this symbol that do not use the | |
817 | GOT, we don't need to generate a copy reloc. */ | |
818 | if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0) | |
819 | return true; | |
820 | ||
821 | /* We must allocate the symbol in our .dynbss section, which will | |
407443a3 | 822 | become part of the .bss section of the executable. There will be |
70256ad8 AJ |
823 | an entry for this symbol in the .dynsym section. The dynamic |
824 | object will contain position independent code, so all references | |
825 | from the dynamic object to this symbol will go through the global | |
826 | offset table. The dynamic linker will use the .dynsym entry to | |
827 | determine the address it must put in the global offset table, so | |
828 | both the dynamic object and the regular object will refer to the | |
829 | same memory location for the variable. */ | |
830 | ||
831 | s = bfd_get_section_by_name (dynobj, ".dynbss"); | |
832 | BFD_ASSERT (s != NULL); | |
833 | ||
834 | /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker | |
835 | to copy the initial value out of the dynamic object and into the | |
836 | runtime process image. We need to remember the offset into the | |
837 | .rela.bss section we are going to use. */ | |
838 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) | |
839 | { | |
840 | asection *srel; | |
841 | ||
842 | srel = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
843 | BFD_ASSERT (srel != NULL); | |
844 | srel->_raw_size += sizeof (Elf64_External_Rela); | |
845 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; | |
846 | } | |
847 | ||
848 | /* We need to figure out the alignment required for this symbol. I | |
407443a3 | 849 | have no idea how ELF linkers handle this. 16-bytes is the size |
70256ad8 AJ |
850 | of the largest type that requires hard alignment -- long double. */ |
851 | /* FIXME: This is VERY ugly. Should be fixed for all architectures using | |
852 | this construct. */ | |
853 | power_of_two = bfd_log2 (h->size); | |
854 | if (power_of_two > 4) | |
855 | power_of_two = 4; | |
856 | ||
857 | /* Apply the required alignment. */ | |
858 | s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two)); | |
859 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) | |
860 | { | |
861 | if (! bfd_set_section_alignment (dynobj, s, power_of_two)) | |
862 | return false; | |
863 | } | |
864 | ||
865 | /* Define the symbol as being at this point in the section. */ | |
866 | h->root.u.def.section = s; | |
867 | h->root.u.def.value = s->_raw_size; | |
868 | ||
869 | /* Increment the section size to make room for the symbol. */ | |
870 | s->_raw_size += h->size; | |
871 | ||
872 | return true; | |
873 | } | |
874 | ||
875 | /* Set the sizes of the dynamic sections. */ | |
876 | ||
877 | static boolean | |
878 | elf64_x86_64_size_dynamic_sections (output_bfd, info) | |
879 | bfd *output_bfd; | |
880 | struct bfd_link_info *info; | |
881 | { | |
882 | bfd *dynobj; | |
883 | asection *s; | |
884 | boolean plt; | |
885 | boolean relocs; | |
886 | boolean reltext; | |
887 | ||
888 | dynobj = elf_hash_table (info)->dynobj; | |
889 | BFD_ASSERT (dynobj != NULL); | |
890 | ||
891 | if (elf_hash_table (info)->dynamic_sections_created) | |
892 | { | |
893 | /* Set the contents of the .interp section to the interpreter. */ | |
894 | if (! info->shared) | |
895 | { | |
896 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
897 | BFD_ASSERT (s != NULL); | |
898 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; | |
899 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
900 | } | |
901 | } | |
902 | else | |
903 | { | |
904 | /* We may have created entries in the .rela.got section. | |
407443a3 AJ |
905 | However, if we are not creating the dynamic sections, we will |
906 | not actually use these entries. Reset the size of .rela.got, | |
907 | which will cause it to get stripped from the output file | |
908 | below. */ | |
70256ad8 AJ |
909 | s = bfd_get_section_by_name (dynobj, ".rela.got"); |
910 | if (s != NULL) | |
911 | s->_raw_size = 0; | |
912 | } | |
913 | ||
914 | /* If this is a -Bsymbolic shared link, then we need to discard all | |
915 | PC relative relocs against symbols defined in a regular object. | |
916 | We allocated space for them in the check_relocs routine, but we | |
917 | will not fill them in in the relocate_section routine. */ | |
918 | if (info->shared) | |
919 | elf64_x86_64_link_hash_traverse (elf64_x86_64_hash_table (info), | |
920 | elf64_x86_64_discard_copies, | |
921 | (PTR) info); | |
922 | ||
923 | /* The check_relocs and adjust_dynamic_symbol entry points have | |
924 | determined the sizes of the various dynamic sections. Allocate | |
925 | memory for them. */ | |
926 | plt = relocs = reltext = false; | |
927 | for (s = dynobj->sections; s != NULL; s = s->next) | |
928 | { | |
929 | const char *name; | |
930 | boolean strip; | |
931 | ||
932 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
933 | continue; | |
934 | ||
935 | /* It's OK to base decisions on the section name, because none | |
936 | of the dynobj section names depend upon the input files. */ | |
937 | name = bfd_get_section_name (dynobj, s); | |
938 | ||
939 | strip = false; | |
940 | if (strcmp (name, ".plt") == 0) | |
941 | { | |
942 | if (s->_raw_size == 0) | |
943 | { | |
944 | /* Strip this section if we don't need it; see the | |
407443a3 | 945 | comment below. */ |
70256ad8 AJ |
946 | strip = true; |
947 | } | |
948 | else | |
949 | { | |
950 | /* Remember whether there is a PLT. */ | |
951 | plt = true; | |
952 | } | |
953 | } | |
954 | else if (strncmp (name, ".rela", 5) == 0) | |
955 | { | |
956 | if (s->_raw_size == 0) | |
957 | { | |
958 | /* If we don't need this section, strip it from the | |
959 | output file. This is mostly to handle .rela.bss and | |
960 | .rela.plt. We must create both sections in | |
961 | create_dynamic_sections, because they must be created | |
962 | before the linker maps input sections to output | |
963 | sections. The linker does that before | |
964 | adjust_dynamic_symbol is called, and it is that | |
965 | function which decides whether anything needs to go | |
966 | into these sections. */ | |
967 | strip = true; | |
968 | } | |
969 | else | |
970 | { | |
971 | asection *target; | |
972 | ||
973 | /* Remember whether there are any reloc sections other | |
407443a3 | 974 | than .rela.plt. */ |
70256ad8 AJ |
975 | if (strcmp (name, ".rela.plt") != 0) |
976 | { | |
977 | const char *outname; | |
978 | ||
979 | relocs = true; | |
980 | ||
981 | /* If this relocation section applies to a read only | |
982 | section, then we probably need a DT_TEXTREL | |
983 | entry. The entries in the .rela.plt section | |
984 | really apply to the .got section, which we | |
985 | created ourselves and so know is not readonly. */ | |
986 | outname = bfd_get_section_name (output_bfd, | |
987 | s->output_section); | |
988 | target = bfd_get_section_by_name (output_bfd, outname + 5); | |
989 | if (target != NULL | |
990 | && (target->flags & SEC_READONLY) != 0 | |
991 | && (target->flags & SEC_ALLOC) != 0) | |
992 | reltext = true; | |
993 | } | |
994 | ||
995 | /* We use the reloc_count field as a counter if we need | |
996 | to copy relocs into the output file. */ | |
997 | s->reloc_count = 0; | |
998 | } | |
999 | } | |
1000 | else if (strncmp (name, ".got", 4) != 0) | |
1001 | { | |
1002 | /* It's not one of our sections, so don't allocate space. */ | |
1003 | continue; | |
1004 | } | |
1005 | ||
1006 | if (strip) | |
1007 | { | |
1008 | _bfd_strip_section_from_output (info, s); | |
1009 | continue; | |
1010 | } | |
1011 | ||
1012 | /* Allocate memory for the section contents. We use bfd_zalloc | |
1013 | here in case unused entries are not reclaimed before the | |
1014 | section's contents are written out. This should not happen, | |
1015 | but this way if it does, we get a R_X86_64_NONE reloc instead | |
1016 | of garbage. */ | |
1017 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size); | |
1018 | if (s->contents == NULL && s->_raw_size != 0) | |
1019 | return false; | |
1020 | } | |
1021 | ||
1022 | if (elf_hash_table (info)->dynamic_sections_created) | |
1023 | { | |
1024 | /* Add some entries to the .dynamic section. We fill in the | |
1025 | values later, in elf64_x86_64_finish_dynamic_sections, but we | |
1026 | must add the entries now so that we get the correct size for | |
407443a3 | 1027 | the .dynamic section. The DT_DEBUG entry is filled in by the |
70256ad8 AJ |
1028 | dynamic linker and used by the debugger. */ |
1029 | if (! info->shared) | |
1030 | { | |
1031 | if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0)) | |
1032 | return false; | |
1033 | } | |
1034 | ||
1035 | if (plt) | |
1036 | { | |
70256ad8 AJ |
1037 | if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0) |
1038 | || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0) | |
407443a3 | 1039 | || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA) |
70256ad8 AJ |
1040 | || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0)) |
1041 | return false; | |
1042 | } | |
1043 | ||
1044 | if (relocs) | |
1045 | { | |
1046 | if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0) | |
1047 | || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0) | |
1048 | || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT, | |
1049 | sizeof (Elf64_External_Rela))) | |
1050 | return false; | |
1051 | } | |
1052 | ||
1053 | if (reltext) | |
1054 | { | |
1055 | if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0)) | |
1056 | return false; | |
1057 | info->flags |= DF_TEXTREL; | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | return true; | |
1062 | } | |
1063 | ||
1064 | /* This function is called via elf64_x86_64_link_hash_traverse if we are | |
1065 | creating a shared object. In the -Bsymbolic case, it discards the | |
1066 | space allocated to copy PC relative relocs against symbols which | |
1067 | are defined in regular objects. For the normal non-symbolic case, | |
1068 | we also discard space for relocs that have become local due to | |
1069 | symbol visibility changes. We allocated space for them in the | |
1070 | check_relocs routine, but we won't fill them in in the | |
1071 | relocate_section routine. */ | |
1072 | ||
1073 | static boolean | |
1074 | elf64_x86_64_discard_copies (h, inf) | |
1075 | struct elf64_x86_64_link_hash_entry *h; | |
1076 | PTR inf; | |
1077 | { | |
1078 | struct elf64_x86_64_pcrel_relocs_copied *s; | |
1079 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
1080 | ||
1081 | /* If a symbol has been forced local or we have found a regular | |
1082 | definition for the symbolic link case, then we won't be needing | |
1083 | any relocs. */ | |
1084 | if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
1085 | && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0 | |
1086 | || info->symbolic)) | |
1087 | { | |
1088 | for (s = h->pcrel_relocs_copied; s != NULL; s = s->next) | |
1089 | s->section->_raw_size -= s->count * sizeof (Elf64_External_Rela); | |
1090 | } | |
1091 | ||
1092 | return true; | |
1093 | } | |
1094 | ||
8d88c4ca NC |
1095 | /* Relocate an x86_64 ELF section. */ |
1096 | ||
1097 | static boolean | |
1098 | elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section, | |
407443a3 | 1099 | contents, relocs, local_syms, local_sections) |
8d88c4ca NC |
1100 | bfd *output_bfd; |
1101 | struct bfd_link_info *info; | |
1102 | bfd *input_bfd; | |
1103 | asection *input_section; | |
1104 | bfd_byte *contents; | |
1105 | Elf_Internal_Rela *relocs; | |
1106 | Elf_Internal_Sym *local_syms; | |
1107 | asection **local_sections; | |
1108 | { | |
1109 | bfd *dynobj; | |
1110 | Elf_Internal_Shdr *symtab_hdr; | |
1111 | struct elf_link_hash_entry **sym_hashes; | |
1112 | bfd_vma *local_got_offsets; | |
70256ad8 AJ |
1113 | asection *sgot; |
1114 | asection *splt; | |
8d88c4ca | 1115 | asection *sreloc; |
70256ad8 | 1116 | Elf_Internal_Rela *rela; |
8d88c4ca NC |
1117 | Elf_Internal_Rela *relend; |
1118 | ||
1119 | dynobj = elf_hash_table (info)->dynobj; | |
1120 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1121 | sym_hashes = elf_sym_hashes (input_bfd); | |
1122 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
1123 | ||
70256ad8 | 1124 | sreloc = splt = sgot = NULL; |
8d88c4ca | 1125 | if (dynobj != NULL) |
70256ad8 AJ |
1126 | { |
1127 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1128 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1129 | } | |
8d88c4ca | 1130 | |
70256ad8 | 1131 | rela = relocs; |
8d88c4ca | 1132 | relend = relocs + input_section->reloc_count; |
70256ad8 | 1133 | for (; rela < relend; rela++) |
8d88c4ca NC |
1134 | { |
1135 | int r_type; | |
1136 | reloc_howto_type *howto; | |
1137 | unsigned long r_symndx; | |
1138 | struct elf_link_hash_entry *h; | |
1139 | Elf_Internal_Sym *sym; | |
1140 | asection *sec; | |
1141 | bfd_vma relocation; | |
1142 | bfd_reloc_status_type r; | |
1143 | unsigned int indx; | |
1144 | ||
70256ad8 | 1145 | r_type = ELF64_R_TYPE (rela->r_info); |
8d88c4ca NC |
1146 | |
1147 | if ((indx = (unsigned) r_type) >= R_X86_64_max) | |
8da6118f KH |
1148 | { |
1149 | bfd_set_error (bfd_error_bad_value); | |
1150 | return false; | |
1151 | } | |
8d88c4ca NC |
1152 | howto = x86_64_elf_howto_table + indx; |
1153 | ||
70256ad8 | 1154 | r_symndx = ELF64_R_SYM (rela->r_info); |
8d88c4ca NC |
1155 | |
1156 | if (info->relocateable) | |
8da6118f KH |
1157 | { |
1158 | /* This is a relocateable link. We don't have to change | |
1159 | anything, unless the reloc is against a section symbol, | |
1160 | in which case we have to adjust according to where the | |
1161 | section symbol winds up in the output section. */ | |
1162 | if (r_symndx < symtab_hdr->sh_info) | |
1163 | { | |
1164 | sym = local_syms + r_symndx; | |
1165 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
1166 | { | |
1167 | sec = local_sections[r_symndx]; | |
70256ad8 | 1168 | rela->r_addend += sec->output_offset + sym->st_value; |
8da6118f KH |
1169 | } |
1170 | } | |
1171 | ||
1172 | continue; | |
1173 | } | |
8d88c4ca | 1174 | |
407443a3 | 1175 | /* This is a final link. */ |
8d88c4ca NC |
1176 | h = NULL; |
1177 | sym = NULL; | |
1178 | sec = NULL; | |
1179 | if (r_symndx < symtab_hdr->sh_info) | |
8da6118f KH |
1180 | { |
1181 | sym = local_syms + r_symndx; | |
1182 | sec = local_sections[r_symndx]; | |
1183 | relocation = (sec->output_section->vma | |
1184 | + sec->output_offset | |
1185 | + sym->st_value); | |
1186 | } | |
8d88c4ca | 1187 | else |
8da6118f KH |
1188 | { |
1189 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1190 | while (h->root.type == bfd_link_hash_indirect | |
1191 | || h->root.type == bfd_link_hash_warning) | |
1192 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1193 | if (h->root.type == bfd_link_hash_defined | |
1194 | || h->root.type == bfd_link_hash_defweak) | |
1195 | { | |
1196 | sec = h->root.u.def.section; | |
bcdd92f3 AJ |
1197 | if (r_type == R_X86_64_GOTPCREL |
1198 | || (r_type = R_X86_64_PLT32 | |
1199 | && splt != NULL | |
1200 | && h->plt.offset != (bfd_vma) -1) | |
1201 | || (r_type = R_X86_64_GOT32 | |
1202 | && elf_hash_table (info)->dynamic_sections_created | |
1203 | && (!info->shared | |
1204 | || (! info->symbolic && h->dynindx != -1) | |
1205 | || (h->elf_link_hash_flags | |
1206 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
1207 | || (info->shared | |
1208 | && ((! info->symbolic && h->dynindx != -1) | |
1209 | || (h->elf_link_hash_flags | |
1210 | & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1211 | && ( r_type == R_X86_64_8 || | |
1212 | r_type == R_X86_64_16 || | |
1213 | r_type == R_X86_64_32 || | |
1214 | r_type == R_X86_64_64 || | |
1215 | r_type == R_X86_64_PC16 || | |
1216 | r_type == R_X86_64_PC32) | |
1217 | && ((input_section->flags & SEC_ALLOC) != 0 | |
1218 | /* DWARF will emit R_X86_64_32 relocations in its | |
1219 | sections against symbols defined externally | |
1220 | in shared libraries. We can't do anything | |
1221 | with them here. */ | |
1222 | || ((input_section->flags & SEC_DEBUGGING) != 0 | |
1223 | && (h->elf_link_hash_flags | |
1224 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0)))) | |
1225 | { | |
1226 | /* In these cases, we don't need the relocation | |
1227 | value. We check specially because in some | |
1228 | obscure cases sec->output_section will be NULL. */ | |
1229 | relocation = 0; | |
1230 | } | |
1231 | else if (sec->output_section == NULL) | |
8da6118f KH |
1232 | { |
1233 | (*_bfd_error_handler) | |
1234 | (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"), | |
1235 | bfd_get_filename (input_bfd), h->root.root.string, | |
1236 | bfd_get_section_name (input_bfd, input_section)); | |
1237 | relocation = 0; | |
1238 | } | |
1239 | else | |
1240 | relocation = (h->root.u.def.value | |
1241 | + sec->output_section->vma | |
1242 | + sec->output_offset); | |
1243 | } | |
1244 | else if (h->root.type == bfd_link_hash_undefweak) | |
1245 | relocation = 0; | |
70256ad8 AJ |
1246 | else if (info->shared && !info->symbolic && !info->no_undefined |
1247 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
1248 | relocation = 0; | |
8da6118f KH |
1249 | else |
1250 | { | |
1251 | if (! ((*info->callbacks->undefined_symbol) | |
1252 | (info, h->root.root.string, input_bfd, | |
70256ad8 | 1253 | input_section, rela->r_offset, |
8da6118f KH |
1254 | (!info->shared || info->no_undefined |
1255 | || ELF_ST_VISIBILITY (h->other))))) | |
1256 | return false; | |
1257 | relocation = 0; | |
1258 | } | |
1259 | } | |
70256ad8 AJ |
1260 | |
1261 | /* When generating a shared object, the relocations handled here are | |
1262 | copied into the output file to be resolved at run time. */ | |
1263 | switch (r_type) | |
1264 | { | |
1265 | case R_X86_64_GOT32: | |
1266 | /* Relocation is to the entry for this symbol in the global | |
1267 | offset table. */ | |
1268 | BFD_ASSERT (sgot != NULL); | |
1269 | ||
1270 | if (h != NULL) | |
1271 | { | |
1272 | bfd_vma off = h->got.offset; | |
1273 | BFD_ASSERT (off != (bfd_vma) -1); | |
1274 | ||
1275 | if (! elf_hash_table (info)->dynamic_sections_created | |
1276 | || (info->shared | |
1277 | && (info->symbolic || h->dynindx == -1) | |
1278 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
1279 | { | |
1280 | /* This is actually a static link, or it is a -Bsymbolic | |
1281 | link and the symbol is defined locally, or the symbol | |
407443a3 | 1282 | was forced to be local because of a version file. We |
70256ad8 AJ |
1283 | must initialize this entry in the global offset table. |
1284 | Since the offset must always be a multiple of 8, we | |
1285 | use the least significant bit to record whether we | |
1286 | have initialized it already. | |
1287 | ||
1288 | When doing a dynamic link, we create a .rela.got | |
407443a3 AJ |
1289 | relocation entry to initialize the value. This is |
1290 | done in the finish_dynamic_symbol routine. */ | |
70256ad8 AJ |
1291 | if ((off & 1) != 0) |
1292 | off &= ~1; | |
1293 | else | |
1294 | { | |
1295 | bfd_put_64 (output_bfd, relocation, | |
1296 | sgot->contents + off); | |
1297 | h->got.offset |= 1; | |
1298 | } | |
1299 | } | |
1300 | relocation = sgot->output_offset + off; | |
1301 | } | |
1302 | else | |
1303 | { | |
1304 | bfd_vma off; | |
1305 | ||
1306 | BFD_ASSERT (local_got_offsets != NULL | |
1307 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
1308 | ||
1309 | off = local_got_offsets[r_symndx]; | |
1310 | ||
1311 | /* The offset must always be a multiple of 8. We use | |
407443a3 AJ |
1312 | the least significant bit to record whether we have |
1313 | already generated the necessary reloc. */ | |
70256ad8 AJ |
1314 | if ((off & 1) != 0) |
1315 | off &= ~1; | |
1316 | else | |
1317 | { | |
1318 | bfd_put_64 (output_bfd, relocation, sgot->contents + off); | |
1319 | ||
51e0a107 | 1320 | if (info->shared) |
70256ad8 AJ |
1321 | { |
1322 | asection *srelgot; | |
1323 | Elf_Internal_Rela outrel; | |
1324 | ||
1325 | /* We need to generate a R_X86_64_RELATIVE reloc | |
1326 | for the dynamic linker. */ | |
1327 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1328 | BFD_ASSERT (srelgot != NULL); | |
1329 | ||
1330 | outrel.r_offset = (sgot->output_section->vma | |
1331 | + sgot->output_offset | |
1332 | + off); | |
1333 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); | |
1334 | outrel.r_addend = relocation; | |
1335 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, | |
1336 | (((Elf64_External_Rela *) | |
1337 | srelgot->contents) | |
1338 | + srelgot->reloc_count)); | |
1339 | ++srelgot->reloc_count; | |
1340 | } | |
1341 | ||
1342 | local_got_offsets[r_symndx] |= 1; | |
1343 | } | |
1344 | ||
1345 | relocation = sgot->output_offset + off; | |
1346 | } | |
1347 | ||
1348 | break; | |
1349 | ||
1350 | case R_X86_64_GOTPCREL: | |
1351 | /* Use global offset table as symbol value. */ | |
1352 | ||
51e0a107 JH |
1353 | BFD_ASSERT (sgot != NULL); |
1354 | if (h != NULL) | |
70256ad8 | 1355 | { |
51e0a107 JH |
1356 | bfd_vma off = h->got.offset; |
1357 | BFD_ASSERT (off != (bfd_vma) -1); | |
1358 | ||
1359 | if (! elf_hash_table (info)->dynamic_sections_created | |
1360 | || (info->shared | |
1361 | && (info->symbolic || h->dynindx == -1) | |
1362 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
1363 | { | |
1364 | /* This is actually a static link, or it is a -Bsymbolic | |
1365 | link and the symbol is defined locally, or the symbol | |
407443a3 | 1366 | was forced to be local because of a version file. We |
51e0a107 JH |
1367 | must initialize this entry in the global offset table. |
1368 | Since the offset must always be a multiple of 8, we | |
1369 | use the least significant bit to record whether we | |
1370 | have initialized it already. | |
1371 | ||
1372 | When doing a dynamic link, we create a .rela.got | |
407443a3 AJ |
1373 | relocation entry to initialize the value. This is |
1374 | done in the finish_dynamic_symbol routine. */ | |
51e0a107 JH |
1375 | if ((off & 1) != 0) |
1376 | off &= ~1; | |
1377 | else | |
1378 | { | |
1379 | bfd_put_64 (output_bfd, relocation, | |
1380 | sgot->contents + off); | |
1381 | h->got.offset |= 1; | |
1382 | } | |
1383 | } | |
1384 | relocation = sgot->output_offset + off; | |
70256ad8 | 1385 | } |
51e0a107 JH |
1386 | else |
1387 | { | |
1388 | bfd_vma off; | |
70256ad8 | 1389 | |
51e0a107 JH |
1390 | BFD_ASSERT (local_got_offsets != NULL |
1391 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
1392 | ||
1393 | off = local_got_offsets[r_symndx]; | |
1394 | ||
1395 | /* The offset must always be a multiple of 8. We use | |
407443a3 AJ |
1396 | the least significant bit to record whether we have |
1397 | already generated the necessary reloc. */ | |
51e0a107 JH |
1398 | if ((off & 1) != 0) |
1399 | off &= ~1; | |
1400 | else | |
1401 | { | |
1402 | bfd_put_64 (output_bfd, relocation, sgot->contents + off); | |
1403 | ||
1404 | if (info->shared) | |
1405 | { | |
1406 | asection *srelgot; | |
1407 | Elf_Internal_Rela outrel; | |
70256ad8 | 1408 | |
51e0a107 JH |
1409 | /* We need to generate a R_X86_64_RELATIVE reloc |
1410 | for the dynamic linker. */ | |
1411 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1412 | BFD_ASSERT (srelgot != NULL); | |
1413 | ||
1414 | outrel.r_offset = (sgot->output_section->vma | |
1415 | + sgot->output_offset | |
1416 | + off); | |
1417 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); | |
1418 | outrel.r_addend = relocation; | |
1419 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, | |
1420 | (((Elf64_External_Rela *) | |
1421 | srelgot->contents) | |
1422 | + srelgot->reloc_count)); | |
1423 | ++srelgot->reloc_count; | |
1424 | } | |
1425 | ||
1426 | local_got_offsets[r_symndx] |= 1; | |
1427 | } | |
1428 | ||
1429 | relocation = sgot->output_section->vma + off; | |
1430 | } | |
70256ad8 AJ |
1431 | break; |
1432 | ||
1433 | case R_X86_64_PLT32: | |
1434 | /* Relocation is to the entry for this symbol in the | |
1435 | procedure linkage table. */ | |
1436 | ||
1437 | /* Resolve a PLT32 reloc against a local symbol directly, | |
407443a3 | 1438 | without using the procedure linkage table. */ |
70256ad8 AJ |
1439 | if (h == NULL) |
1440 | break; | |
1441 | ||
1442 | if (h->plt.offset == (bfd_vma) -1 || splt == NULL) | |
1443 | { | |
1444 | /* We didn't make a PLT entry for this symbol. This | |
407443a3 AJ |
1445 | happens when statically linking PIC code, or when |
1446 | using -Bsymbolic. */ | |
70256ad8 AJ |
1447 | break; |
1448 | } | |
1449 | ||
1450 | relocation = (splt->output_section->vma | |
1451 | + splt->output_offset | |
1452 | + h->plt.offset); | |
1453 | break; | |
1454 | ||
1455 | case R_X86_64_8: | |
1456 | case R_X86_64_16: | |
1457 | case R_X86_64_32: | |
1458 | case R_X86_64_PC8: | |
1459 | case R_X86_64_PC16: | |
1460 | case R_X86_64_PC32: | |
1461 | /* FIXME: The abi says the linker should make sure the value is | |
407443a3 | 1462 | the same when it's zeroextended to 64 bit. */ |
70256ad8 AJ |
1463 | if (info->shared |
1464 | && (input_section->flags & SEC_ALLOC) != 0 | |
1465 | && ((r_type != R_X86_64_PC8 && r_type != R_X86_64_PC16 | |
1466 | && r_type != R_X86_64_PC32) | |
1467 | || (h != NULL | |
1468 | && h->dynindx != -1 | |
1469 | && (! info->symbolic | |
1470 | || (h->elf_link_hash_flags | |
1471 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
1472 | { | |
1473 | Elf_Internal_Rela outrel; | |
1474 | boolean skip, relocate; | |
1475 | ||
1476 | /* When generating a shared object, these relocations | |
1477 | are copied into the output file to be resolved at run | |
407443a3 | 1478 | time. */ |
70256ad8 AJ |
1479 | |
1480 | if (sreloc == NULL) | |
1481 | { | |
1482 | const char *name; | |
1483 | ||
1484 | name = (bfd_elf_string_from_elf_section | |
1485 | (input_bfd, | |
1486 | elf_elfheader (input_bfd)->e_shstrndx, | |
1487 | elf_section_data (input_section)->rel_hdr.sh_name)); | |
1488 | if (name == NULL) | |
1489 | return false; | |
1490 | ||
1491 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
1492 | && strcmp (bfd_get_section_name (input_bfd, | |
1493 | input_section), | |
1494 | name + 5) == 0); | |
1495 | ||
1496 | sreloc = bfd_get_section_by_name (dynobj, name); | |
1497 | BFD_ASSERT (sreloc != NULL); | |
1498 | } | |
1499 | ||
1500 | skip = false; | |
1501 | ||
1502 | if (elf_section_data (input_section)->stab_info == NULL) | |
1503 | outrel.r_offset = rela->r_offset; | |
1504 | else | |
1505 | { | |
1506 | bfd_vma off; | |
1507 | ||
1508 | off = (_bfd_stab_section_offset | |
1509 | (output_bfd, &elf_hash_table (info)->stab_info, | |
1510 | input_section, | |
1511 | &elf_section_data (input_section)->stab_info, | |
1512 | rela->r_offset)); | |
1513 | if (off == (bfd_vma) -1) | |
1514 | skip = true; | |
1515 | outrel.r_offset = off; | |
1516 | } | |
1517 | ||
1518 | outrel.r_offset += (input_section->output_section->vma | |
1519 | + input_section->output_offset); | |
1520 | ||
1521 | if (skip) | |
1522 | { | |
1523 | memset (&outrel, 0, sizeof outrel); | |
1524 | relocate = false; | |
1525 | } | |
1526 | else if ((r_type == R_X86_64_PC8) || (r_type == R_X86_64_PC16) | |
1527 | || (r_type == R_X86_64_PC32)) | |
1528 | { | |
1529 | BFD_ASSERT (h != NULL && h->dynindx != -1); | |
1530 | relocate = false; | |
1531 | outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); | |
5608e05b | 1532 | outrel.r_addend = relocation + rela->r_addend; |
70256ad8 AJ |
1533 | } |
1534 | else | |
1535 | { | |
1536 | /* h->dynindx may be -1 if this symbol was marked to | |
407443a3 | 1537 | become local. */ |
70256ad8 AJ |
1538 | if (h == NULL |
1539 | || ((info->symbolic || h->dynindx == -1) | |
1540 | && (h->elf_link_hash_flags | |
1541 | & ELF_LINK_HASH_DEF_REGULAR) != 0)) | |
1542 | { | |
1543 | relocate = true; | |
1544 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); | |
5608e05b | 1545 | outrel.r_addend = relocation + rela->r_addend; |
70256ad8 AJ |
1546 | } |
1547 | else | |
1548 | { | |
1549 | BFD_ASSERT (h->dynindx != -1); | |
1550 | relocate = false; | |
1551 | outrel.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_32); | |
5608e05b | 1552 | outrel.r_addend = relocation + rela->r_addend; |
70256ad8 AJ |
1553 | } |
1554 | } | |
1555 | ||
1556 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, | |
1557 | (((Elf64_External_Rela *) | |
1558 | sreloc->contents) | |
1559 | + sreloc->reloc_count)); | |
1560 | ++sreloc->reloc_count; | |
1561 | ||
1562 | /* If this reloc is against an external symbol, we do | |
1563 | not want to fiddle with the addend. Otherwise, we | |
1564 | need to include the symbol value so that it becomes | |
1565 | an addend for the dynamic reloc. */ | |
1566 | if (! relocate) | |
1567 | continue; | |
1568 | } | |
1569 | ||
1570 | break; | |
1571 | ||
1572 | default: | |
1573 | break; | |
1574 | } | |
8d88c4ca NC |
1575 | |
1576 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
70256ad8 AJ |
1577 | contents, rela->r_offset, |
1578 | relocation, rela->r_addend); | |
8d88c4ca NC |
1579 | |
1580 | if (r != bfd_reloc_ok) | |
8da6118f KH |
1581 | { |
1582 | switch (r) | |
1583 | { | |
1584 | default: | |
1585 | case bfd_reloc_outofrange: | |
1586 | abort (); | |
1587 | case bfd_reloc_overflow: | |
1588 | { | |
1589 | const char *name; | |
1590 | ||
1591 | if (h != NULL) | |
1592 | name = h->root.root.string; | |
1593 | else | |
1594 | { | |
1595 | name = bfd_elf_string_from_elf_section (input_bfd, | |
1596 | symtab_hdr->sh_link, | |
1597 | sym->st_name); | |
1598 | if (name == NULL) | |
1599 | return false; | |
1600 | if (*name == '\0') | |
1601 | name = bfd_section_name (input_bfd, sec); | |
1602 | } | |
1603 | if (! ((*info->callbacks->reloc_overflow) | |
1604 | (info, name, howto->name, (bfd_vma) 0, | |
70256ad8 | 1605 | input_bfd, input_section, rela->r_offset))) |
8da6118f KH |
1606 | return false; |
1607 | } | |
1608 | break; | |
1609 | } | |
1610 | } | |
8d88c4ca | 1611 | } |
70256ad8 AJ |
1612 | |
1613 | return true; | |
1614 | } | |
1615 | ||
1616 | /* Finish up dynamic symbol handling. We set the contents of various | |
1617 | dynamic sections here. */ | |
1618 | ||
1619 | static boolean | |
1620 | elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym) | |
1621 | bfd *output_bfd; | |
1622 | struct bfd_link_info *info; | |
1623 | struct elf_link_hash_entry *h; | |
1624 | Elf_Internal_Sym *sym; | |
1625 | { | |
1626 | bfd *dynobj; | |
1627 | ||
1628 | dynobj = elf_hash_table (info)->dynobj; | |
1629 | ||
1630 | if (h->plt.offset != (bfd_vma) -1) | |
1631 | { | |
1632 | asection *splt; | |
1633 | asection *sgot; | |
1634 | asection *srela; | |
1635 | bfd_vma plt_index; | |
1636 | bfd_vma got_offset; | |
1637 | Elf_Internal_Rela rela; | |
1638 | ||
1639 | /* This symbol has an entry in the procedure linkage table. Set | |
407443a3 | 1640 | it up. */ |
70256ad8 AJ |
1641 | |
1642 | BFD_ASSERT (h->dynindx != -1); | |
1643 | ||
1644 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1645 | sgot = bfd_get_section_by_name (dynobj, ".got.plt"); | |
1646 | srela = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
1647 | BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL); | |
1648 | ||
1649 | /* Get the index in the procedure linkage table which | |
1650 | corresponds to this symbol. This is the index of this symbol | |
1651 | in all the symbols for which we are making plt entries. The | |
1652 | first entry in the procedure linkage table is reserved. */ | |
1653 | plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; | |
1654 | ||
1655 | /* Get the offset into the .got table of the entry that | |
407443a3 | 1656 | corresponds to this function. Each .got entry is GOT_ENTRY_SIZE |
70256ad8 AJ |
1657 | bytes. The first three are reserved. */ |
1658 | got_offset = (plt_index + 3) * GOT_ENTRY_SIZE; | |
1659 | ||
1660 | /* Fill in the entry in the procedure linkage table. */ | |
1661 | memcpy (splt->contents + h->plt.offset, elf64_x86_64_plt_entry, | |
1662 | PLT_ENTRY_SIZE); | |
1663 | ||
1664 | /* Insert the relocation positions of the plt section. The magic | |
1665 | numbers at the end of the statements are the positions of the | |
1666 | relocations in the plt section. */ | |
653165cc AJ |
1667 | /* Put offset for jmp *name@GOTPCREL(%rip), since the |
1668 | instruction uses 6 bytes, subtract this value. */ | |
1669 | bfd_put_32 (output_bfd, | |
1670 | (sgot->output_section->vma | |
1671 | + sgot->output_offset | |
1672 | + got_offset | |
1673 | - splt->output_section->vma | |
1674 | - splt->output_offset | |
1675 | - h->plt.offset | |
1676 | - 6), | |
1677 | splt->contents + h->plt.offset + 2); | |
1678 | /* Put relocation index. */ | |
1679 | bfd_put_32 (output_bfd, plt_index, | |
70256ad8 | 1680 | splt->contents + h->plt.offset + 7); |
653165cc AJ |
1681 | /* Put offset for jmp .PLT0. */ |
1682 | bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE), | |
70256ad8 AJ |
1683 | splt->contents + h->plt.offset + 12); |
1684 | ||
653165cc AJ |
1685 | /* Fill in the entry in the global offset table, initially this |
1686 | points to the pushq instruction in the PLT which is at offset 6. */ | |
70256ad8 AJ |
1687 | bfd_put_64 (output_bfd, (splt->output_section->vma + splt->output_offset |
1688 | + h->plt.offset + 6), | |
1689 | sgot->contents + got_offset); | |
1690 | ||
1691 | /* Fill in the entry in the .rela.plt section. */ | |
1692 | rela.r_offset = (sgot->output_section->vma | |
1693 | + sgot->output_offset | |
1694 | + got_offset); | |
1695 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT); | |
1696 | rela.r_addend = 0; | |
1697 | bfd_elf64_swap_reloca_out (output_bfd, &rela, | |
1698 | ((Elf64_External_Rela *) srela->contents | |
1699 | + plt_index)); | |
1700 | ||
1701 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1702 | { | |
1703 | /* Mark the symbol as undefined, rather than as defined in | |
407443a3 | 1704 | the .plt section. Leave the value alone. */ |
70256ad8 | 1705 | sym->st_shndx = SHN_UNDEF; |
05aa1441 AJ |
1706 | /* If the symbol is weak, we do need to clear the value. |
1707 | Otherwise, the PLT entry would provide a definition for | |
1708 | the symbol even if the symbol wasn't defined anywhere, | |
1709 | and so the symbol would never be NULL. */ | |
1710 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) | |
1711 | == 0) | |
1712 | sym->st_value = 0; | |
70256ad8 AJ |
1713 | } |
1714 | } | |
1715 | ||
1716 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) | |
1717 | { | |
1718 | asection *s; | |
1719 | Elf_Internal_Rela rela; | |
1720 | ||
1721 | /* This symbol needs a copy reloc. Set it up. */ | |
1722 | ||
1723 | BFD_ASSERT (h->dynindx != -1 | |
1724 | && (h->root.type == bfd_link_hash_defined | |
1725 | || h->root.type == bfd_link_hash_defweak)); | |
1726 | ||
1727 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
1728 | ".rela.bss"); | |
1729 | BFD_ASSERT (s != NULL); | |
1730 | ||
1731 | rela.r_offset = (h->root.u.def.value | |
1732 | + h->root.u.def.section->output_section->vma | |
1733 | + h->root.u.def.section->output_offset); | |
1734 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY); | |
1735 | rela.r_addend = 0; | |
1736 | bfd_elf64_swap_reloca_out (output_bfd, &rela, | |
1737 | ((Elf64_External_Rela *) s->contents | |
1738 | + s->reloc_count)); | |
1739 | ++s->reloc_count; | |
1740 | } | |
1741 | ||
1742 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ | |
1743 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
1744 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
1745 | sym->st_shndx = SHN_ABS; | |
1746 | ||
1747 | return true; | |
1748 | } | |
1749 | ||
1750 | /* Finish up the dynamic sections. */ | |
1751 | ||
1752 | static boolean | |
1753 | elf64_x86_64_finish_dynamic_sections (output_bfd, info) | |
1754 | bfd *output_bfd; | |
1755 | struct bfd_link_info *info; | |
1756 | { | |
1757 | bfd *dynobj; | |
1758 | asection *sdyn; | |
1759 | asection *sgot; | |
1760 | ||
1761 | dynobj = elf_hash_table (info)->dynobj; | |
1762 | ||
653165cc AJ |
1763 | sgot = bfd_get_section_by_name (dynobj, ".got.plt"); |
1764 | BFD_ASSERT (sgot != NULL); | |
70256ad8 AJ |
1765 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); |
1766 | ||
1767 | if (elf_hash_table (info)->dynamic_sections_created) | |
1768 | { | |
1769 | asection *splt; | |
1770 | Elf64_External_Dyn *dyncon, *dynconend; | |
1771 | ||
407443a3 | 1772 | BFD_ASSERT (sdyn != NULL); |
70256ad8 AJ |
1773 | |
1774 | dyncon = (Elf64_External_Dyn *) sdyn->contents; | |
1775 | dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
1776 | for (; dyncon < dynconend; dyncon++) | |
1777 | { | |
1778 | Elf_Internal_Dyn dyn; | |
1779 | const char *name; | |
1780 | asection *s; | |
1781 | ||
1782 | bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); | |
1783 | ||
1784 | switch (dyn.d_tag) | |
1785 | { | |
1786 | default: | |
1787 | break; | |
1788 | ||
1789 | case DT_PLTGOT: | |
1790 | name = ".got"; | |
1791 | goto get_vma; | |
1792 | ||
1793 | case DT_JMPREL: | |
1794 | name = ".rela.plt"; | |
1795 | ||
1796 | get_vma: | |
1797 | s = bfd_get_section_by_name (output_bfd, name); | |
1798 | BFD_ASSERT (s != NULL); | |
1799 | dyn.d_un.d_ptr = s->vma; | |
1800 | break; | |
1801 | ||
1802 | case DT_RELASZ: | |
1803 | /* FIXME: This comment and code is from elf64-alpha.c: */ | |
1804 | /* My interpretation of the TIS v1.1 ELF document indicates | |
407443a3 | 1805 | that RELASZ should not include JMPREL. This is not what |
70256ad8 AJ |
1806 | the rest of the BFD does. It is, however, what the |
1807 | glibc ld.so wants. Do this fixup here until we found | |
1808 | out who is right. */ | |
1809 | s = bfd_get_section_by_name (output_bfd, ".rela.plt"); | |
1810 | if (s) | |
1811 | { | |
407443a3 | 1812 | /* Subtract JMPREL size from RELASZ. */ |
70256ad8 AJ |
1813 | dyn.d_un.d_val -= |
1814 | (s->_cooked_size ? s->_cooked_size : s->_raw_size); | |
1815 | } | |
1816 | break; | |
1817 | ||
1818 | case DT_PLTRELSZ: | |
1819 | s = bfd_get_section_by_name (output_bfd, ".rela.plt"); | |
1820 | BFD_ASSERT (s != NULL); | |
1821 | dyn.d_un.d_val = | |
1822 | (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size); | |
1823 | break; | |
1824 | } | |
1825 | ||
1826 | bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); | |
1827 | } | |
1828 | ||
1829 | /* Initialize the contents of the .plt section. */ | |
407443a3 AJ |
1830 | splt = bfd_get_section_by_name (dynobj, ".plt"); |
1831 | BFD_ASSERT (splt != NULL); | |
70256ad8 AJ |
1832 | if (splt->_raw_size > 0) |
1833 | { | |
653165cc | 1834 | /* Fill in the first entry in the procedure linkage table. */ |
407443a3 | 1835 | memcpy (splt->contents, elf64_x86_64_plt0_entry, PLT_ENTRY_SIZE); |
653165cc AJ |
1836 | /* Add offset for pushq GOT+8(%rip), since the instruction |
1837 | uses 6 bytes subtract this value. */ | |
1838 | bfd_put_32 (output_bfd, | |
1839 | (sgot->output_section->vma | |
1840 | + sgot->output_offset | |
1841 | + 8 | |
1842 | - splt->output_section->vma | |
1843 | - splt->output_offset | |
1844 | - 6), | |
1845 | splt->contents + 2); | |
1846 | /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to | |
1847 | the end of the instruction. */ | |
1848 | bfd_put_32 (output_bfd, | |
1849 | (sgot->output_section->vma | |
1850 | + sgot->output_offset | |
1851 | + 16 | |
1852 | - splt->output_section->vma | |
1853 | - splt->output_offset | |
1854 | - 12), | |
1855 | splt->contents + 8); | |
1856 | ||
70256ad8 AJ |
1857 | } |
1858 | ||
1859 | elf_section_data (splt->output_section)->this_hdr.sh_entsize = | |
1860 | PLT_ENTRY_SIZE; | |
1861 | } | |
1862 | ||
1863 | /* Set the first entry in the global offset table to the address of | |
1864 | the dynamic section. */ | |
70256ad8 AJ |
1865 | if (sgot->_raw_size > 0) |
1866 | { | |
1867 | if (sdyn == NULL) | |
1868 | bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents); | |
1869 | else | |
1870 | bfd_put_64 (output_bfd, | |
1871 | sdyn->output_section->vma + sdyn->output_offset, | |
1872 | sgot->contents); | |
653165cc | 1873 | /* Write GOT[1] and GOT[2], needed for the dynamic linker. */ |
70256ad8 AJ |
1874 | bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE); |
1875 | bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE*2); | |
1876 | } | |
1877 | ||
1878 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize = | |
1879 | GOT_ENTRY_SIZE; | |
1880 | ||
8d88c4ca NC |
1881 | return true; |
1882 | } | |
1883 | ||
407443a3 AJ |
1884 | /* |
1885 | * Why was the hash table entry size definition changed from | |
1886 | * ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and | |
1887 | * this is the only reason for the elf64_x86_64_size_info structure. | |
1888 | */ | |
1889 | ||
1890 | const struct elf_size_info elf64_86_64_size_info = | |
1891 | { | |
1892 | sizeof (Elf64_External_Ehdr), | |
1893 | sizeof (Elf64_External_Phdr), | |
1894 | sizeof (Elf64_External_Shdr), | |
1895 | sizeof (Elf64_External_Rel), | |
1896 | sizeof (Elf64_External_Rela), | |
1897 | sizeof (Elf64_External_Sym), | |
1898 | sizeof (Elf64_External_Dyn), | |
1899 | sizeof (Elf_External_Note), | |
1900 | 8, /* hash-table entry size */ | |
1901 | 1, /* internal relocations per external relocations */ | |
1902 | 64, /* arch_size */ | |
1903 | 8, /* file_align */ | |
1904 | ELFCLASS64, EV_CURRENT, | |
1905 | bfd_elf64_write_out_phdrs, | |
1906 | bfd_elf64_write_shdrs_and_ehdr, | |
1907 | bfd_elf64_write_relocs, | |
1908 | bfd_elf64_swap_symbol_out, | |
1909 | bfd_elf64_slurp_reloc_table, | |
1910 | bfd_elf64_slurp_symbol_table, | |
1911 | bfd_elf64_swap_dyn_in, | |
1912 | bfd_elf64_swap_dyn_out, | |
1913 | NULL, | |
1914 | NULL, | |
1915 | NULL, | |
1916 | NULL | |
1917 | }; | |
1918 | ||
70256ad8 AJ |
1919 | #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec |
1920 | #define TARGET_LITTLE_NAME "elf64-x86-64" | |
1921 | #define ELF_ARCH bfd_arch_i386 | |
1922 | #define ELF_MACHINE_CODE EM_X86_64 | |
1923 | #define ELF_MAXPAGESIZE 0x100000 | |
1924 | ||
407443a3 AJ |
1925 | #define elf_backend_size_info elf64_86_64_size_info |
1926 | ||
70256ad8 AJ |
1927 | #define elf_backend_can_gc_sections 1 |
1928 | #define elf_backend_want_got_plt 1 | |
1929 | #define elf_backend_plt_readonly 1 | |
1930 | #define elf_backend_want_plt_sym 0 | |
1931 | #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3) | |
1932 | #define elf_backend_plt_header_size PLT_ENTRY_SIZE | |
1933 | ||
1934 | #define elf_info_to_howto elf64_x86_64_info_to_howto | |
70256ad8 AJ |
1935 | |
1936 | #define bfd_elf64_bfd_final_link _bfd_elf64_gc_common_final_link | |
1937 | #define bfd_elf64_bfd_link_hash_table_create \ | |
1938 | elf64_x86_64_link_hash_table_create | |
407443a3 | 1939 | #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup |
70256ad8 AJ |
1940 | |
1941 | #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol | |
1942 | #define elf_backend_check_relocs elf64_x86_64_check_relocs | |
1943 | #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections | |
1944 | #define elf_backend_finish_dynamic_sections \ | |
1945 | elf64_x86_64_finish_dynamic_sections | |
1946 | #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol | |
1947 | #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook | |
1948 | #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook | |
1949 | #define elf_backend_relocate_section elf64_x86_64_relocate_section | |
1950 | #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections | |
407443a3 | 1951 | #define elf_backend_object_p elf64_x86_64_elf_object_p |
8d88c4ca NC |
1952 | |
1953 | #include "elf64-target.h" |