Commit | Line | Data |
---|---|---|
0afcef53 L |
1 | /* x86 specific support for ELF |
2 | Copyright (C) 2017 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
19 | MA 02110-1301, USA. */ | |
20 | ||
21 | #include "elfxx-x86.h" | |
a6798bab | 22 | #include "elf-vxworks.h" |
765e526c L |
23 | #include "objalloc.h" |
24 | #include "elf/i386.h" | |
25 | #include "elf/x86-64.h" | |
26 | ||
27 | /* The name of the dynamic interpreter. This is put in the .interp | |
28 | section. */ | |
29 | ||
30 | #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1" | |
31 | #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1" | |
32 | #define ELFX32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1" | |
0afcef53 | 33 | |
39946cc2 L |
34 | bfd_boolean |
35 | _bfd_x86_elf_mkobject (bfd *abfd) | |
36 | { | |
37 | return bfd_elf_allocate_object (abfd, | |
38 | sizeof (struct elf_x86_obj_tdata), | |
39 | get_elf_backend_data (abfd)->target_id); | |
40 | } | |
41 | ||
0afcef53 L |
42 | /* _TLS_MODULE_BASE_ needs to be treated especially when linking |
43 | executables. Rather than setting it to the beginning of the TLS | |
44 | section, we have to set it to the end. This function may be called | |
45 | multiple times, it is idempotent. */ | |
46 | ||
47 | void | |
48 | _bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info) | |
49 | { | |
50 | struct elf_x86_link_hash_table *htab; | |
51 | struct bfd_link_hash_entry *base; | |
52 | const struct elf_backend_data *bed; | |
53 | ||
54 | if (!bfd_link_executable (info)) | |
55 | return; | |
56 | ||
57 | bed = get_elf_backend_data (info->output_bfd); | |
58 | htab = elf_x86_hash_table (info, bed->target_id); | |
59 | if (htab == NULL) | |
60 | return; | |
61 | ||
62 | base = htab->tls_module_base; | |
63 | if (base == NULL) | |
64 | return; | |
65 | ||
66 | base->u.def.value = htab->elf.tls_size; | |
67 | } | |
68 | ||
69 | /* Return the base VMA address which should be subtracted from real addresses | |
70 | when resolving @dtpoff relocation. | |
71 | This is PT_TLS segment p_vaddr. */ | |
72 | ||
73 | bfd_vma | |
74 | _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info) | |
75 | { | |
76 | /* If tls_sec is NULL, we should have signalled an error already. */ | |
77 | if (elf_hash_table (info)->tls_sec == NULL) | |
78 | return 0; | |
79 | return elf_hash_table (info)->tls_sec->vma; | |
80 | } | |
81 | ||
82 | /* Find any dynamic relocs that apply to read-only sections. */ | |
83 | ||
84 | bfd_boolean | |
85 | _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry *h, | |
86 | void *inf) | |
87 | { | |
88 | struct elf_x86_link_hash_entry *eh; | |
89 | struct elf_dyn_relocs *p; | |
90 | ||
91 | /* Skip local IFUNC symbols. */ | |
92 | if (h->forced_local && h->type == STT_GNU_IFUNC) | |
93 | return TRUE; | |
94 | ||
95 | eh = (struct elf_x86_link_hash_entry *) h; | |
96 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
97 | { | |
98 | asection *s = p->sec->output_section; | |
99 | ||
100 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
101 | { | |
102 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
103 | ||
104 | info->flags |= DF_TEXTREL; | |
105 | ||
106 | if ((info->warn_shared_textrel && bfd_link_pic (info)) | |
107 | || info->error_textrel) | |
108 | /* xgettext:c-format */ | |
109 | info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"), | |
110 | p->sec->owner, h->root.root.string, | |
111 | p->sec); | |
112 | ||
113 | /* Not an error, just cut short the traversal. */ | |
114 | return FALSE; | |
115 | } | |
116 | } | |
117 | return TRUE; | |
118 | } | |
119 | ||
120 | /* Find and/or create a hash entry for local symbol. */ | |
121 | ||
122 | struct elf_link_hash_entry * | |
123 | _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab, | |
124 | bfd *abfd, const Elf_Internal_Rela *rel, | |
125 | bfd_boolean create) | |
126 | { | |
127 | struct elf_x86_link_hash_entry e, *ret; | |
128 | asection *sec = abfd->sections; | |
129 | hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id, | |
130 | htab->r_sym (rel->r_info)); | |
131 | void **slot; | |
132 | ||
133 | e.elf.indx = sec->id; | |
134 | e.elf.dynstr_index = htab->r_sym (rel->r_info); | |
135 | slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h, | |
136 | create ? INSERT : NO_INSERT); | |
137 | ||
138 | if (!slot) | |
139 | return NULL; | |
140 | ||
141 | if (*slot) | |
142 | { | |
143 | ret = (struct elf_x86_link_hash_entry *) *slot; | |
144 | return &ret->elf; | |
145 | } | |
146 | ||
147 | ret = (struct elf_x86_link_hash_entry *) | |
148 | objalloc_alloc ((struct objalloc *) htab->loc_hash_memory, | |
149 | sizeof (struct elf_x86_link_hash_entry)); | |
150 | if (ret) | |
151 | { | |
152 | memset (ret, 0, sizeof (*ret)); | |
153 | ret->elf.indx = sec->id; | |
154 | ret->elf.dynstr_index = htab->r_sym (rel->r_info); | |
155 | ret->elf.dynindx = -1; | |
156 | ret->plt_got.offset = (bfd_vma) -1; | |
157 | *slot = ret; | |
158 | } | |
159 | return &ret->elf; | |
160 | } | |
161 | ||
162 | /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN | |
163 | SYNC WITH _bfd_elf_link_hash_newfunc. */ | |
164 | ||
165 | struct bfd_hash_entry * | |
166 | _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
167 | struct bfd_hash_table *table, | |
168 | const char *string) | |
169 | { | |
170 | /* Allocate the structure if it has not already been allocated by a | |
171 | subclass. */ | |
172 | if (entry == NULL) | |
173 | { | |
174 | entry = (struct bfd_hash_entry *) | |
175 | bfd_hash_allocate (table, | |
176 | sizeof (struct elf_x86_link_hash_entry)); | |
177 | if (entry == NULL) | |
178 | return entry; | |
179 | } | |
180 | ||
181 | /* Call the allocation method of the superclass. */ | |
182 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
183 | if (entry != NULL) | |
184 | { | |
185 | struct elf_x86_link_hash_entry *eh | |
186 | = (struct elf_x86_link_hash_entry *) entry; | |
187 | struct elf_link_hash_table *htab | |
188 | = (struct elf_link_hash_table *) table; | |
189 | ||
190 | memset (&eh->elf.size, 0, | |
191 | (sizeof (struct elf_x86_link_hash_entry) | |
192 | - offsetof (struct elf_link_hash_entry, size))); | |
193 | /* Set local fields. */ | |
194 | eh->elf.indx = -1; | |
195 | eh->elf.dynindx = -1; | |
196 | eh->elf.got = htab->init_got_refcount; | |
197 | eh->elf.plt = htab->init_plt_refcount; | |
198 | /* Assume that we have been called by a non-ELF symbol reader. | |
199 | This flag is then reset by the code which reads an ELF input | |
200 | file. This ensures that a symbol created by a non-ELF symbol | |
201 | reader will have the flag set correctly. */ | |
202 | eh->elf.non_elf = 1; | |
203 | eh->plt_second.offset = (bfd_vma) -1; | |
204 | eh->plt_got.offset = (bfd_vma) -1; | |
205 | eh->tlsdesc_got = (bfd_vma) -1; | |
206 | } | |
207 | ||
208 | return entry; | |
209 | } | |
210 | ||
211 | /* Compute a hash of a local hash entry. We use elf_link_hash_entry | |
212 | for local symbol so that we can handle local STT_GNU_IFUNC symbols | |
213 | as global symbol. We reuse indx and dynstr_index for local symbol | |
214 | hash since they aren't used by global symbols in this backend. */ | |
215 | ||
216 | hashval_t | |
217 | _bfd_x86_elf_local_htab_hash (const void *ptr) | |
218 | { | |
219 | struct elf_link_hash_entry *h | |
220 | = (struct elf_link_hash_entry *) ptr; | |
221 | return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index); | |
222 | } | |
223 | ||
224 | /* Compare local hash entries. */ | |
225 | ||
226 | int | |
227 | _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2) | |
228 | { | |
229 | struct elf_link_hash_entry *h1 | |
230 | = (struct elf_link_hash_entry *) ptr1; | |
231 | struct elf_link_hash_entry *h2 | |
232 | = (struct elf_link_hash_entry *) ptr2; | |
233 | ||
234 | return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index; | |
235 | } | |
236 | ||
237 | /* Destroy an x86 ELF linker hash table. */ | |
238 | ||
765e526c L |
239 | static void |
240 | elf_x86_link_hash_table_free (bfd *obfd) | |
0afcef53 L |
241 | { |
242 | struct elf_x86_link_hash_table *htab | |
243 | = (struct elf_x86_link_hash_table *) obfd->link.hash; | |
244 | ||
245 | if (htab->loc_hash_table) | |
246 | htab_delete (htab->loc_hash_table); | |
247 | if (htab->loc_hash_memory) | |
248 | objalloc_free ((struct objalloc *) htab->loc_hash_memory); | |
249 | _bfd_elf_link_hash_table_free (obfd); | |
250 | } | |
251 | ||
765e526c L |
252 | /* Create an x86 ELF linker hash table. */ |
253 | ||
254 | struct bfd_link_hash_table * | |
255 | _bfd_x86_elf_link_hash_table_create (bfd *abfd) | |
256 | { | |
257 | struct elf_x86_link_hash_table *ret; | |
258 | const struct elf_backend_data *bed; | |
259 | bfd_size_type amt = sizeof (struct elf_x86_link_hash_table); | |
260 | ||
261 | ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt); | |
262 | if (ret == NULL) | |
263 | return NULL; | |
264 | ||
265 | bed = get_elf_backend_data (abfd); | |
266 | if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, | |
267 | _bfd_x86_elf_link_hash_newfunc, | |
268 | sizeof (struct elf_x86_link_hash_entry), | |
269 | bed->target_id)) | |
270 | { | |
271 | free (ret); | |
272 | return NULL; | |
273 | } | |
274 | ||
275 | #ifdef BFD64 | |
276 | if (ABI_64_P (abfd)) | |
277 | { | |
278 | ret->r_info = elf64_r_info; | |
279 | ret->r_sym = elf64_r_sym; | |
280 | ret->pointer_r_type = R_X86_64_64; | |
281 | ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER; | |
282 | ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER; | |
283 | ret->tls_get_addr = "__tls_get_addr"; | |
284 | } | |
285 | else | |
286 | #endif | |
287 | { | |
288 | ret->r_info = elf32_r_info; | |
289 | ret->r_sym = elf32_r_sym; | |
5b86dbf4 | 290 | if (bed->target_id == X86_64_ELF_DATA) |
765e526c L |
291 | { |
292 | ret->pointer_r_type = R_X86_64_32; | |
293 | ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER; | |
294 | ret->dynamic_interpreter_size | |
295 | = sizeof ELFX32_DYNAMIC_INTERPRETER; | |
296 | ret->tls_get_addr = "__tls_get_addr"; | |
297 | } | |
298 | else | |
299 | { | |
300 | ret->pointer_r_type = R_386_32; | |
301 | ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER; | |
302 | ret->dynamic_interpreter_size | |
303 | = sizeof ELF32_DYNAMIC_INTERPRETER; | |
304 | ret->tls_get_addr = "___tls_get_addr"; | |
305 | } | |
306 | } | |
307 | ||
308 | ret->loc_hash_table = htab_try_create (1024, | |
309 | _bfd_x86_elf_local_htab_hash, | |
310 | _bfd_x86_elf_local_htab_eq, | |
311 | NULL); | |
312 | ret->loc_hash_memory = objalloc_create (); | |
313 | if (!ret->loc_hash_table || !ret->loc_hash_memory) | |
314 | { | |
315 | elf_x86_link_hash_table_free (abfd); | |
316 | return NULL; | |
317 | } | |
318 | ret->elf.root.hash_table_free = elf_x86_link_hash_table_free; | |
319 | ||
320 | return &ret->elf.root; | |
321 | } | |
322 | ||
0afcef53 L |
323 | /* Sort relocs into address order. */ |
324 | ||
325 | int | |
326 | _bfd_x86_elf_compare_relocs (const void *ap, const void *bp) | |
327 | { | |
328 | const arelent *a = * (const arelent **) ap; | |
329 | const arelent *b = * (const arelent **) bp; | |
330 | ||
331 | if (a->address > b->address) | |
332 | return 1; | |
333 | else if (a->address < b->address) | |
334 | return -1; | |
335 | else | |
336 | return 0; | |
337 | } | |
338 | ||
339 | bfd_boolean | |
340 | _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
341 | { | |
342 | if (!bfd_link_relocatable (info)) | |
343 | { | |
344 | /* Check for __tls_get_addr reference. */ | |
345 | struct elf_x86_link_hash_table *htab; | |
346 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
347 | htab = elf_x86_hash_table (info, bed->target_id); | |
348 | if (htab) | |
349 | { | |
350 | struct elf_link_hash_entry *h | |
351 | = elf_link_hash_lookup (elf_hash_table (info), | |
352 | htab->tls_get_addr, | |
353 | FALSE, FALSE, FALSE); | |
354 | if (h != NULL) | |
355 | ((struct elf_x86_link_hash_entry *) h)->tls_get_addr = 1; | |
356 | } | |
357 | } | |
358 | ||
359 | /* Invoke the regular ELF backend linker to do all the work. */ | |
360 | return _bfd_elf_link_check_relocs (abfd, info); | |
361 | } | |
362 | ||
363 | bfd_boolean | |
364 | _bfd_x86_elf_always_size_sections (bfd *output_bfd, | |
365 | struct bfd_link_info *info) | |
366 | { | |
367 | asection *tls_sec = elf_hash_table (info)->tls_sec; | |
368 | ||
369 | if (tls_sec) | |
370 | { | |
371 | struct elf_link_hash_entry *tlsbase; | |
372 | ||
373 | tlsbase = elf_link_hash_lookup (elf_hash_table (info), | |
374 | "_TLS_MODULE_BASE_", | |
375 | FALSE, FALSE, FALSE); | |
376 | ||
377 | if (tlsbase && tlsbase->type == STT_TLS) | |
378 | { | |
379 | struct elf_x86_link_hash_table *htab; | |
380 | struct bfd_link_hash_entry *bh = NULL; | |
381 | const struct elf_backend_data *bed | |
382 | = get_elf_backend_data (output_bfd); | |
383 | ||
384 | htab = elf_x86_hash_table (info, bed->target_id); | |
385 | if (htab == NULL) | |
386 | return FALSE; | |
387 | ||
388 | if (!(_bfd_generic_link_add_one_symbol | |
389 | (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL, | |
390 | tls_sec, 0, NULL, FALSE, | |
391 | bed->collect, &bh))) | |
392 | return FALSE; | |
393 | ||
394 | htab->tls_module_base = bh; | |
395 | ||
396 | tlsbase = (struct elf_link_hash_entry *)bh; | |
397 | tlsbase->def_regular = 1; | |
398 | tlsbase->other = STV_HIDDEN; | |
399 | tlsbase->root.linker_def = 1; | |
400 | (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE); | |
401 | } | |
402 | } | |
403 | ||
404 | return TRUE; | |
405 | } | |
406 | ||
407 | void | |
408 | _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h, | |
409 | const Elf_Internal_Sym *isym, | |
410 | bfd_boolean definition, | |
411 | bfd_boolean dynamic ATTRIBUTE_UNUSED) | |
412 | { | |
413 | if (definition) | |
414 | { | |
415 | struct elf_x86_link_hash_entry *eh | |
416 | = (struct elf_x86_link_hash_entry *) h; | |
417 | eh->def_protected = (ELF_ST_VISIBILITY (isym->st_other) | |
418 | == STV_PROTECTED); | |
419 | } | |
420 | } | |
421 | ||
422 | /* Copy the extra info we tack onto an elf_link_hash_entry. */ | |
423 | ||
424 | void | |
425 | _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info, | |
426 | struct elf_link_hash_entry *dir, | |
427 | struct elf_link_hash_entry *ind) | |
428 | { | |
429 | struct elf_x86_link_hash_entry *edir, *eind; | |
430 | ||
431 | edir = (struct elf_x86_link_hash_entry *) dir; | |
432 | eind = (struct elf_x86_link_hash_entry *) ind; | |
433 | ||
434 | if (eind->dyn_relocs != NULL) | |
435 | { | |
436 | if (edir->dyn_relocs != NULL) | |
437 | { | |
438 | struct elf_dyn_relocs **pp; | |
439 | struct elf_dyn_relocs *p; | |
440 | ||
441 | /* Add reloc counts against the indirect sym to the direct sym | |
442 | list. Merge any entries against the same section. */ | |
443 | for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) | |
444 | { | |
445 | struct elf_dyn_relocs *q; | |
446 | ||
447 | for (q = edir->dyn_relocs; q != NULL; q = q->next) | |
448 | if (q->sec == p->sec) | |
449 | { | |
450 | q->pc_count += p->pc_count; | |
451 | q->count += p->count; | |
452 | *pp = p->next; | |
453 | break; | |
454 | } | |
455 | if (q == NULL) | |
456 | pp = &p->next; | |
457 | } | |
458 | *pp = edir->dyn_relocs; | |
459 | } | |
460 | ||
461 | edir->dyn_relocs = eind->dyn_relocs; | |
462 | eind->dyn_relocs = NULL; | |
463 | } | |
464 | ||
465 | if (ind->root.type == bfd_link_hash_indirect | |
466 | && dir->got.refcount <= 0) | |
467 | { | |
468 | edir->tls_type = eind->tls_type; | |
469 | eind->tls_type = GOT_UNKNOWN; | |
470 | } | |
471 | ||
472 | /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will | |
473 | generate a R_386_COPY reloc. */ | |
474 | edir->gotoff_ref |= eind->gotoff_ref; | |
475 | ||
476 | edir->has_got_reloc |= eind->has_got_reloc; | |
477 | edir->has_non_got_reloc |= eind->has_non_got_reloc; | |
478 | ||
479 | if (ELIMINATE_COPY_RELOCS | |
480 | && ind->root.type != bfd_link_hash_indirect | |
481 | && dir->dynamic_adjusted) | |
482 | { | |
483 | /* If called to transfer flags for a weakdef during processing | |
484 | of elf_adjust_dynamic_symbol, don't copy non_got_ref. | |
485 | We clear it ourselves for ELIMINATE_COPY_RELOCS. */ | |
486 | if (dir->versioned != versioned_hidden) | |
487 | dir->ref_dynamic |= ind->ref_dynamic; | |
488 | dir->ref_regular |= ind->ref_regular; | |
489 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
490 | dir->needs_plt |= ind->needs_plt; | |
491 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
492 | } | |
493 | else | |
494 | { | |
495 | if (eind->func_pointer_refcount > 0) | |
496 | { | |
497 | edir->func_pointer_refcount += eind->func_pointer_refcount; | |
498 | eind->func_pointer_refcount = 0; | |
499 | } | |
500 | ||
501 | _bfd_elf_link_hash_copy_indirect (info, dir, ind); | |
502 | } | |
503 | } | |
504 | ||
505 | /* Remove undefined weak symbol from the dynamic symbol table if it | |
506 | is resolved to 0. */ | |
507 | ||
508 | bfd_boolean | |
509 | _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info, | |
510 | struct elf_link_hash_entry *h) | |
511 | { | |
512 | if (h->dynindx != -1) | |
513 | { | |
514 | const struct elf_backend_data *bed | |
515 | = get_elf_backend_data (info->output_bfd); | |
516 | if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, | |
517 | bed->target_id, | |
518 | elf_x86_hash_entry (h)->has_got_reloc, | |
519 | elf_x86_hash_entry (h))) | |
520 | { | |
521 | h->dynindx = -1; | |
522 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
523 | h->dynstr_index); | |
524 | } | |
525 | } | |
526 | return TRUE; | |
527 | } | |
528 | ||
529 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
530 | ||
531 | bfd_boolean | |
532 | _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h) | |
533 | { | |
534 | if (h->plt.offset != (bfd_vma) -1 | |
535 | && !h->def_regular | |
536 | && !h->pointer_equality_needed) | |
537 | return FALSE; | |
538 | ||
539 | return _bfd_elf_hash_symbol (h); | |
540 | } | |
541 | ||
eeb2f20a L |
542 | /* Adjust a symbol defined by a dynamic object and referenced by a |
543 | regular object. The current definition is in some section of the | |
544 | dynamic object, but we're not including those sections. We have to | |
545 | change the definition to something the rest of the link can | |
546 | understand. */ | |
547 | ||
548 | bfd_boolean | |
549 | _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info, | |
550 | struct elf_link_hash_entry *h) | |
551 | { | |
552 | struct elf_x86_link_hash_table *htab; | |
553 | asection *s, *srel; | |
554 | struct elf_x86_link_hash_entry *eh; | |
555 | struct elf_dyn_relocs *p; | |
556 | const struct elf_backend_data *bed | |
557 | = get_elf_backend_data (info->output_bfd); | |
558 | ||
559 | /* STT_GNU_IFUNC symbol must go through PLT. */ | |
560 | if (h->type == STT_GNU_IFUNC) | |
561 | { | |
562 | /* All local STT_GNU_IFUNC references must be treate as local | |
563 | calls via local PLT. */ | |
564 | if (h->ref_regular | |
565 | && SYMBOL_CALLS_LOCAL (info, h)) | |
566 | { | |
567 | bfd_size_type pc_count = 0, count = 0; | |
568 | struct elf_dyn_relocs **pp; | |
569 | ||
570 | eh = (struct elf_x86_link_hash_entry *) h; | |
571 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
572 | { | |
573 | pc_count += p->pc_count; | |
574 | p->count -= p->pc_count; | |
575 | p->pc_count = 0; | |
576 | count += p->count; | |
577 | if (p->count == 0) | |
578 | *pp = p->next; | |
579 | else | |
580 | pp = &p->next; | |
581 | } | |
582 | ||
583 | if (pc_count || count) | |
584 | { | |
585 | h->non_got_ref = 1; | |
586 | if (pc_count) | |
587 | { | |
588 | /* Increment PLT reference count only for PC-relative | |
589 | references. */ | |
590 | h->needs_plt = 1; | |
591 | if (h->plt.refcount <= 0) | |
592 | h->plt.refcount = 1; | |
593 | else | |
594 | h->plt.refcount += 1; | |
595 | } | |
596 | } | |
597 | } | |
598 | ||
599 | if (h->plt.refcount <= 0) | |
600 | { | |
601 | h->plt.offset = (bfd_vma) -1; | |
602 | h->needs_plt = 0; | |
603 | } | |
604 | return TRUE; | |
605 | } | |
606 | ||
607 | /* If this is a function, put it in the procedure linkage table. We | |
608 | will fill in the contents of the procedure linkage table later, | |
609 | when we know the address of the .got section. */ | |
610 | if (h->type == STT_FUNC | |
611 | || h->needs_plt) | |
612 | { | |
613 | if (h->plt.refcount <= 0 | |
614 | || SYMBOL_CALLS_LOCAL (info, h) | |
615 | || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
616 | && h->root.type == bfd_link_hash_undefweak)) | |
617 | { | |
618 | /* This case can occur if we saw a PLT32 reloc in an input | |
619 | file, but the symbol was never referred to by a dynamic | |
620 | object, or if all references were garbage collected. In | |
621 | such a case, we don't actually need to build a procedure | |
622 | linkage table, and we can just do a PC32 reloc instead. */ | |
623 | h->plt.offset = (bfd_vma) -1; | |
624 | h->needs_plt = 0; | |
625 | } | |
626 | ||
627 | return TRUE; | |
628 | } | |
629 | else | |
630 | /* It's possible that we incorrectly decided a .plt reloc was needed | |
631 | * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in | |
632 | check_relocs. We can't decide accurately between function and | |
633 | non-function syms in check-relocs; Objects loaded later in | |
634 | the link may change h->type. So fix it now. */ | |
635 | h->plt.offset = (bfd_vma) -1; | |
636 | ||
637 | eh = (struct elf_x86_link_hash_entry *) h; | |
638 | ||
639 | /* If this is a weak symbol, and there is a real definition, the | |
640 | processor independent code will have arranged for us to see the | |
641 | real definition first, and we can just use the same value. */ | |
642 | if (h->u.weakdef != NULL) | |
643 | { | |
644 | BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined | |
645 | || h->u.weakdef->root.type == bfd_link_hash_defweak); | |
646 | h->root.u.def.section = h->u.weakdef->root.u.def.section; | |
647 | h->root.u.def.value = h->u.weakdef->root.u.def.value; | |
648 | if (ELIMINATE_COPY_RELOCS | |
649 | || info->nocopyreloc | |
650 | || SYMBOL_NO_COPYRELOC (info, eh)) | |
651 | { | |
652 | /* NB: needs_copy is always 0 for i386. */ | |
653 | h->non_got_ref = h->u.weakdef->non_got_ref; | |
654 | eh->needs_copy = h->u.weakdef->needs_copy; | |
655 | } | |
656 | return TRUE; | |
657 | } | |
658 | ||
659 | /* This is a reference to a symbol defined by a dynamic object which | |
660 | is not a function. */ | |
661 | ||
662 | /* If we are creating a shared library, we must presume that the | |
663 | only references to the symbol are via the global offset table. | |
664 | For such cases we need not do anything here; the relocations will | |
665 | be handled correctly by relocate_section. */ | |
666 | if (!bfd_link_executable (info)) | |
667 | return TRUE; | |
668 | ||
669 | /* If there are no references to this symbol that do not use the | |
670 | GOT nor R_386_GOTOFF relocation, we don't need to generate a copy | |
671 | reloc. NB: gotoff_ref is always 0 for x86-64. */ | |
672 | if (!h->non_got_ref && !eh->gotoff_ref) | |
673 | return TRUE; | |
674 | ||
675 | /* If -z nocopyreloc was given, we won't generate them either. */ | |
676 | if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh)) | |
677 | { | |
678 | h->non_got_ref = 0; | |
679 | return TRUE; | |
680 | } | |
681 | ||
682 | htab = elf_x86_hash_table (info, bed->target_id); | |
683 | if (htab == NULL) | |
684 | return FALSE; | |
685 | ||
686 | /* If there aren't any dynamic relocs in read-only sections nor | |
687 | R_386_GOTOFF relocation, then we can keep the dynamic relocs and | |
688 | avoid the copy reloc. This doesn't work on VxWorks, where we can | |
689 | not have dynamic relocations (other than copy and jump slot | |
690 | relocations) in an executable. */ | |
691 | if (ELIMINATE_COPY_RELOCS | |
692 | && (bed->target_id == X86_64_ELF_DATA | |
693 | || (!eh->gotoff_ref | |
694 | && !htab->is_vxworks))) | |
695 | { | |
696 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
697 | { | |
698 | s = p->sec->output_section; | |
699 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
700 | break; | |
701 | } | |
702 | ||
703 | /* If we didn't find any dynamic relocs in read-only sections, | |
704 | then we'll be keeping the dynamic relocs and avoiding the copy | |
705 | reloc. */ | |
706 | if (p == NULL) | |
707 | { | |
708 | h->non_got_ref = 0; | |
709 | return TRUE; | |
710 | } | |
711 | } | |
712 | ||
713 | /* We must allocate the symbol in our .dynbss section, which will | |
714 | become part of the .bss section of the executable. There will be | |
715 | an entry for this symbol in the .dynsym section. The dynamic | |
716 | object will contain position independent code, so all references | |
717 | from the dynamic object to this symbol will go through the global | |
718 | offset table. The dynamic linker will use the .dynsym entry to | |
719 | determine the address it must put in the global offset table, so | |
720 | both the dynamic object and the regular object will refer to the | |
721 | same memory location for the variable. */ | |
722 | ||
723 | /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the | |
724 | dynamic linker to copy the initial value out of the dynamic object | |
725 | and into the runtime process image. */ | |
726 | if ((h->root.u.def.section->flags & SEC_READONLY) != 0) | |
727 | { | |
728 | s = htab->elf.sdynrelro; | |
729 | srel = htab->elf.sreldynrelro; | |
730 | } | |
731 | else | |
732 | { | |
733 | s = htab->elf.sdynbss; | |
734 | srel = htab->elf.srelbss; | |
735 | } | |
736 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0) | |
737 | { | |
738 | srel->size += ((bed->target_id == I386_ELF_DATA) | |
739 | ? sizeof (Elf32_External_Rel) | |
740 | : bed->s->sizeof_rela); | |
741 | h->needs_copy = 1; | |
742 | } | |
743 | ||
744 | return _bfd_elf_adjust_dynamic_copy (info, h, s); | |
745 | } | |
746 | ||
4f501a24 L |
747 | /* Return the section that should be marked against GC for a given |
748 | relocation. */ | |
749 | ||
750 | asection * | |
751 | _bfd_x86_elf_gc_mark_hook (asection *sec, | |
752 | struct bfd_link_info *info, | |
753 | Elf_Internal_Rela *rel, | |
754 | struct elf_link_hash_entry *h, | |
755 | Elf_Internal_Sym *sym) | |
756 | { | |
757 | /* Compiler should optimize this out. */ | |
758 | if (((unsigned int) R_X86_64_GNU_VTINHERIT | |
759 | != (unsigned int) R_386_GNU_VTINHERIT) | |
760 | || ((unsigned int) R_X86_64_GNU_VTENTRY | |
761 | != (unsigned int) R_386_GNU_VTENTRY)) | |
762 | abort (); | |
763 | ||
764 | if (h != NULL) | |
765 | switch (ELF32_R_TYPE (rel->r_info)) | |
766 | { | |
767 | case R_X86_64_GNU_VTINHERIT: | |
768 | case R_X86_64_GNU_VTENTRY: | |
769 | return NULL; | |
770 | } | |
771 | ||
772 | return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); | |
773 | } | |
774 | ||
f493882d L |
775 | static bfd_vma |
776 | elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED, | |
777 | bfd_vma off, | |
778 | bfd_vma offset ATTRIBUTE_UNUSED, | |
779 | bfd_vma got_addr) | |
780 | { | |
781 | return got_addr + off; | |
782 | } | |
783 | ||
784 | static bfd_vma | |
785 | elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p, | |
786 | bfd_vma off, | |
787 | bfd_vma offset, | |
788 | bfd_vma got_addr ATTRIBUTE_UNUSED) | |
789 | { | |
790 | return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size; | |
791 | } | |
792 | ||
793 | static bfd_boolean | |
794 | elf_i386_valid_plt_reloc_p (unsigned int type) | |
795 | { | |
796 | return (type == R_386_JUMP_SLOT | |
797 | || type == R_386_GLOB_DAT | |
798 | || type == R_386_IRELATIVE); | |
799 | } | |
800 | ||
801 | static bfd_boolean | |
802 | elf_x86_64_valid_plt_reloc_p (unsigned int type) | |
803 | { | |
804 | return (type == R_X86_64_JUMP_SLOT | |
805 | || type == R_X86_64_GLOB_DAT | |
806 | || type == R_X86_64_IRELATIVE); | |
807 | } | |
808 | ||
809 | long | |
810 | _bfd_x86_elf_get_synthetic_symtab (bfd *abfd, | |
811 | long count, | |
812 | long relsize, | |
813 | bfd_vma got_addr, | |
814 | struct elf_x86_plt plts[], | |
815 | asymbol **dynsyms, | |
816 | asymbol **ret) | |
817 | { | |
818 | long size, i, n, len; | |
819 | int j; | |
820 | unsigned int plt_got_offset, plt_entry_size; | |
821 | asymbol *s; | |
822 | bfd_byte *plt_contents; | |
823 | long dynrelcount; | |
824 | arelent **dynrelbuf, *p; | |
825 | char *names; | |
826 | const struct elf_backend_data *bed; | |
827 | bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma, | |
828 | bfd_vma); | |
829 | bfd_boolean (*valid_plt_reloc_p) (unsigned int); | |
830 | ||
831 | if (count == 0) | |
832 | return -1; | |
833 | ||
834 | dynrelbuf = (arelent **) bfd_malloc (relsize); | |
835 | if (dynrelbuf == NULL) | |
836 | return -1; | |
837 | ||
838 | dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf, | |
839 | dynsyms); | |
840 | ||
841 | /* Sort the relocs by address. */ | |
842 | qsort (dynrelbuf, dynrelcount, sizeof (arelent *), | |
843 | _bfd_x86_elf_compare_relocs); | |
844 | ||
845 | size = count * sizeof (asymbol); | |
846 | ||
847 | /* Allocate space for @plt suffixes. */ | |
848 | n = 0; | |
849 | for (i = 0; i < dynrelcount; i++) | |
850 | { | |
851 | p = dynrelbuf[i]; | |
852 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
853 | if (p->addend != 0) | |
854 | size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd); | |
855 | } | |
856 | ||
857 | s = *ret = (asymbol *) bfd_zmalloc (size); | |
858 | if (s == NULL) | |
859 | goto bad_return; | |
860 | ||
861 | bed = get_elf_backend_data (abfd); | |
862 | ||
863 | if (bed->target_id == X86_64_ELF_DATA) | |
864 | { | |
865 | get_plt_got_vma = elf_x86_64_get_plt_got_vma; | |
866 | valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p; | |
867 | } | |
868 | else | |
869 | { | |
870 | get_plt_got_vma = elf_i386_get_plt_got_vma; | |
871 | valid_plt_reloc_p = elf_i386_valid_plt_reloc_p; | |
872 | if (got_addr) | |
873 | { | |
874 | /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_ | |
875 | address. */ | |
876 | asection *sec = bfd_get_section_by_name (abfd, ".got.plt"); | |
877 | if (sec != NULL) | |
878 | got_addr = sec->vma; | |
879 | else | |
880 | { | |
881 | sec = bfd_get_section_by_name (abfd, ".got"); | |
882 | if (sec != NULL) | |
883 | got_addr = sec->vma; | |
884 | } | |
885 | ||
886 | if (got_addr == (bfd_vma) -1) | |
887 | goto bad_return; | |
888 | } | |
889 | } | |
890 | ||
891 | /* Check for each PLT section. */ | |
892 | names = (char *) (s + count); | |
893 | size = 0; | |
894 | n = 0; | |
895 | for (j = 0; plts[j].name != NULL; j++) | |
896 | if ((plt_contents = plts[j].contents) != NULL) | |
897 | { | |
898 | long k; | |
899 | bfd_vma offset; | |
900 | asection *plt; | |
901 | struct elf_x86_plt *plt_p = &plts[j]; | |
902 | ||
903 | plt_got_offset = plt_p->plt_got_offset; | |
904 | plt_entry_size = plt_p->plt_entry_size; | |
905 | ||
906 | plt = plt_p->sec; | |
907 | ||
908 | if ((plt_p->type & plt_lazy)) | |
909 | { | |
910 | /* Skip PLT0 in lazy PLT. */ | |
911 | k = 1; | |
912 | offset = plt_entry_size; | |
913 | } | |
914 | else | |
915 | { | |
916 | k = 0; | |
917 | offset = 0; | |
918 | } | |
919 | ||
920 | /* Check each PLT entry against dynamic relocations. */ | |
921 | for (; k < plt_p->count; k++) | |
922 | { | |
923 | int off; | |
924 | bfd_vma got_vma; | |
925 | long min, max, mid; | |
926 | ||
927 | /* Get the GOT offset for i386 or the PC-relative offset | |
928 | for x86-64, a signed 32-bit integer. */ | |
929 | off = H_GET_32 (abfd, (plt_contents + offset | |
930 | + plt_got_offset)); | |
931 | got_vma = get_plt_got_vma (plt_p, off, offset, got_addr); | |
932 | ||
933 | /* Binary search. */ | |
934 | p = dynrelbuf[0]; | |
935 | min = 0; | |
936 | max = dynrelcount; | |
937 | while ((min + 1) < max) | |
938 | { | |
939 | arelent *r; | |
940 | ||
941 | mid = (min + max) / 2; | |
942 | r = dynrelbuf[mid]; | |
943 | if (got_vma > r->address) | |
944 | min = mid; | |
945 | else if (got_vma < r->address) | |
946 | max = mid; | |
947 | else | |
948 | { | |
949 | p = r; | |
950 | break; | |
951 | } | |
952 | } | |
953 | ||
954 | /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */ | |
955 | if (got_vma == p->address | |
956 | && p->howto != NULL | |
957 | && valid_plt_reloc_p (p->howto->type)) | |
958 | { | |
959 | *s = **p->sym_ptr_ptr; | |
960 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL | |
961 | set. Since we are defining a symbol, ensure one | |
962 | of them is set. */ | |
963 | if ((s->flags & BSF_LOCAL) == 0) | |
964 | s->flags |= BSF_GLOBAL; | |
965 | s->flags |= BSF_SYNTHETIC; | |
966 | /* This is no longer a section symbol. */ | |
967 | s->flags &= ~BSF_SECTION_SYM; | |
968 | s->section = plt; | |
969 | s->the_bfd = plt->owner; | |
970 | s->value = offset; | |
971 | s->udata.p = NULL; | |
972 | s->name = names; | |
973 | len = strlen ((*p->sym_ptr_ptr)->name); | |
974 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
975 | names += len; | |
976 | if (p->addend != 0) | |
977 | { | |
978 | char buf[30], *a; | |
979 | ||
980 | memcpy (names, "+0x", sizeof ("+0x") - 1); | |
981 | names += sizeof ("+0x") - 1; | |
982 | bfd_sprintf_vma (abfd, buf, p->addend); | |
983 | for (a = buf; *a == '0'; ++a) | |
984 | ; | |
985 | size = strlen (a); | |
986 | memcpy (names, a, size); | |
987 | names += size; | |
988 | } | |
989 | memcpy (names, "@plt", sizeof ("@plt")); | |
990 | names += sizeof ("@plt"); | |
991 | n++; | |
992 | s++; | |
993 | } | |
994 | offset += plt_entry_size; | |
995 | } | |
996 | } | |
997 | ||
998 | /* PLT entries with R_386_TLS_DESC relocations are skipped. */ | |
999 | if (n == 0) | |
1000 | { | |
1001 | bad_return: | |
1002 | count = -1; | |
1003 | } | |
1004 | else | |
1005 | count = n; | |
1006 | ||
1007 | for (j = 0; plts[j].name != NULL; j++) | |
1008 | if (plts[j].contents != NULL) | |
1009 | free (plts[j].contents); | |
1010 | ||
1011 | free (dynrelbuf); | |
1012 | ||
1013 | return count; | |
1014 | } | |
1015 | ||
0afcef53 L |
1016 | /* Parse x86 GNU properties. */ |
1017 | ||
1018 | enum elf_property_kind | |
1019 | _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type, | |
1020 | bfd_byte *ptr, unsigned int datasz) | |
1021 | { | |
1022 | elf_property *prop; | |
1023 | ||
1024 | switch (type) | |
1025 | { | |
1026 | case GNU_PROPERTY_X86_ISA_1_USED: | |
1027 | case GNU_PROPERTY_X86_ISA_1_NEEDED: | |
1028 | case GNU_PROPERTY_X86_FEATURE_1_AND: | |
1029 | if (datasz != 4) | |
1030 | { | |
1031 | _bfd_error_handler | |
1032 | ((type == GNU_PROPERTY_X86_ISA_1_USED | |
1033 | ? _("error: %B: <corrupt x86 ISA used size: 0x%x>") | |
1034 | : (type == GNU_PROPERTY_X86_ISA_1_NEEDED | |
1035 | ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>") | |
1036 | : _("error: %B: <corrupt x86 feature size: 0x%x>"))), | |
1037 | abfd, datasz); | |
1038 | return property_corrupt; | |
1039 | } | |
1040 | prop = _bfd_elf_get_property (abfd, type, datasz); | |
1041 | /* Combine properties of the same type. */ | |
1042 | prop->u.number |= bfd_h_get_32 (abfd, ptr); | |
1043 | prop->pr_kind = property_number; | |
1044 | break; | |
1045 | ||
1046 | default: | |
1047 | return property_ignored; | |
1048 | } | |
1049 | ||
1050 | return property_number; | |
1051 | } | |
1052 | ||
1053 | /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL, | |
1054 | return TRUE if APROP is updated. Otherwise, return TRUE if BPROP | |
1055 | should be merged with ABFD. */ | |
1056 | ||
1057 | bfd_boolean | |
1058 | _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info, | |
1059 | bfd *abfd ATTRIBUTE_UNUSED, | |
1060 | elf_property *aprop, | |
1061 | elf_property *bprop) | |
1062 | { | |
1063 | unsigned int number, features; | |
1064 | bfd_boolean updated = FALSE; | |
1065 | unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type; | |
1066 | ||
1067 | switch (pr_type) | |
1068 | { | |
1069 | case GNU_PROPERTY_X86_ISA_1_USED: | |
1070 | case GNU_PROPERTY_X86_ISA_1_NEEDED: | |
1071 | if (aprop != NULL && bprop != NULL) | |
1072 | { | |
1073 | number = aprop->u.number; | |
1074 | aprop->u.number = number | bprop->u.number; | |
1075 | updated = number != (unsigned int) aprop->u.number; | |
1076 | } | |
1077 | else | |
1078 | { | |
1079 | /* Return TRUE if APROP is NULL to indicate that BPROP should | |
1080 | be added to ABFD. */ | |
1081 | updated = aprop == NULL; | |
1082 | } | |
1083 | break; | |
1084 | ||
1085 | case GNU_PROPERTY_X86_FEATURE_1_AND: | |
1086 | /* Only one of APROP and BPROP can be NULL: | |
1087 | 1. APROP & BPROP when both APROP and BPROP aren't NULL. | |
1088 | 2. If APROP is NULL, remove x86 feature. | |
1089 | 3. Otherwise, do nothing. | |
1090 | */ | |
1091 | if (aprop != NULL && bprop != NULL) | |
1092 | { | |
1093 | features = 0; | |
1094 | if (info->ibt) | |
1095 | features = GNU_PROPERTY_X86_FEATURE_1_IBT; | |
1096 | if (info->shstk) | |
1097 | features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; | |
1098 | number = aprop->u.number; | |
1099 | /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and | |
1100 | GNU_PROPERTY_X86_FEATURE_1_SHSTK. */ | |
1101 | aprop->u.number = (number & bprop->u.number) | features; | |
1102 | updated = number != (unsigned int) aprop->u.number; | |
1103 | /* Remove the property if all feature bits are cleared. */ | |
1104 | if (aprop->u.number == 0) | |
1105 | aprop->pr_kind = property_remove; | |
1106 | } | |
1107 | else | |
1108 | { | |
1109 | features = 0; | |
1110 | if (info->ibt) | |
1111 | features = GNU_PROPERTY_X86_FEATURE_1_IBT; | |
1112 | if (info->shstk) | |
1113 | features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; | |
1114 | if (features) | |
1115 | { | |
1116 | /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and | |
1117 | GNU_PROPERTY_X86_FEATURE_1_SHSTK. */ | |
1118 | if (aprop != NULL) | |
1119 | { | |
1120 | number = aprop->u.number; | |
1121 | aprop->u.number = number | features; | |
1122 | updated = number != (unsigned int) aprop->u.number; | |
1123 | } | |
1124 | else | |
1125 | { | |
1126 | bprop->u.number |= features; | |
1127 | updated = TRUE; | |
1128 | } | |
1129 | } | |
1130 | else if (aprop != NULL) | |
1131 | { | |
1132 | aprop->pr_kind = property_remove; | |
1133 | updated = TRUE; | |
1134 | } | |
1135 | } | |
1136 | break; | |
1137 | ||
1138 | default: | |
1139 | /* Never should happen. */ | |
1140 | abort (); | |
1141 | } | |
1142 | ||
1143 | return updated; | |
1144 | } | |
a6798bab L |
1145 | |
1146 | /* Set up x86 GNU properties. Return the first relocatable ELF input | |
1147 | with GNU properties if found. Otherwise, return NULL. */ | |
1148 | ||
1149 | bfd * | |
1150 | _bfd_x86_elf_link_setup_gnu_properties | |
1151 | (struct bfd_link_info *info, | |
1152 | struct elf_x86_plt_layout_table *plt_layout) | |
1153 | { | |
1154 | bfd_boolean normal_target; | |
1155 | bfd_boolean lazy_plt; | |
1156 | asection *sec, *pltsec; | |
1157 | bfd *dynobj; | |
1158 | bfd_boolean use_ibt_plt; | |
1159 | unsigned int plt_alignment, features; | |
1160 | struct elf_x86_link_hash_table *htab; | |
1161 | bfd *pbfd; | |
1162 | bfd *ebfd = NULL; | |
1163 | elf_property *prop; | |
1164 | const struct elf_backend_data *bed; | |
1165 | unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2; | |
1166 | unsigned int got_align; | |
1167 | ||
1168 | features = 0; | |
1169 | if (info->ibt) | |
1170 | features = GNU_PROPERTY_X86_FEATURE_1_IBT; | |
1171 | if (info->shstk) | |
1172 | features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; | |
1173 | ||
1174 | /* Find a normal input file with GNU property note. */ | |
1175 | for (pbfd = info->input_bfds; | |
1176 | pbfd != NULL; | |
1177 | pbfd = pbfd->link.next) | |
1178 | if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour | |
1179 | && bfd_count_sections (pbfd) != 0) | |
1180 | { | |
1181 | ebfd = pbfd; | |
1182 | ||
1183 | if (elf_properties (pbfd) != NULL) | |
1184 | break; | |
1185 | } | |
1186 | ||
1187 | if (ebfd != NULL && features) | |
1188 | { | |
1189 | /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and | |
1190 | GNU_PROPERTY_X86_FEATURE_1_SHSTK. */ | |
1191 | prop = _bfd_elf_get_property (ebfd, | |
1192 | GNU_PROPERTY_X86_FEATURE_1_AND, | |
1193 | 4); | |
1194 | prop->u.number |= features; | |
1195 | prop->pr_kind = property_number; | |
1196 | ||
1197 | /* Create the GNU property note section if needed. */ | |
1198 | if (pbfd == NULL) | |
1199 | { | |
1200 | sec = bfd_make_section_with_flags (ebfd, | |
1201 | NOTE_GNU_PROPERTY_SECTION_NAME, | |
1202 | (SEC_ALLOC | |
1203 | | SEC_LOAD | |
1204 | | SEC_IN_MEMORY | |
1205 | | SEC_READONLY | |
1206 | | SEC_HAS_CONTENTS | |
1207 | | SEC_DATA)); | |
1208 | if (sec == NULL) | |
1209 | info->callbacks->einfo (_("%F: failed to create GNU property section\n")); | |
1210 | ||
1211 | if (!bfd_set_section_alignment (ebfd, sec, class_align)) | |
1212 | { | |
1213 | error_alignment: | |
1214 | info->callbacks->einfo (_("%F%A: failed to align section\n"), | |
1215 | sec); | |
1216 | } | |
1217 | ||
1218 | elf_section_type (sec) = SHT_NOTE; | |
1219 | } | |
1220 | } | |
1221 | ||
1222 | pbfd = _bfd_elf_link_setup_gnu_properties (info); | |
1223 | ||
1224 | if (bfd_link_relocatable (info)) | |
1225 | return pbfd; | |
1226 | ||
1227 | bed = get_elf_backend_data (info->output_bfd); | |
1228 | ||
1229 | htab = elf_x86_hash_table (info, bed->target_id); | |
1230 | if (htab == NULL) | |
1231 | return pbfd; | |
1232 | ||
eeb2f20a L |
1233 | htab->is_vxworks = plt_layout->is_vxworks; |
1234 | ||
a6798bab L |
1235 | use_ibt_plt = info->ibtplt || info->ibt; |
1236 | if (!use_ibt_plt && pbfd != NULL) | |
1237 | { | |
1238 | /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */ | |
1239 | elf_property_list *p; | |
1240 | ||
1241 | /* The property list is sorted in order of type. */ | |
1242 | for (p = elf_properties (pbfd); p; p = p->next) | |
1243 | { | |
1244 | if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type) | |
1245 | { | |
1246 | use_ibt_plt = !!(p->property.u.number | |
1247 | & GNU_PROPERTY_X86_FEATURE_1_IBT); | |
1248 | break; | |
1249 | } | |
1250 | else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type) | |
1251 | break; | |
1252 | } | |
1253 | } | |
1254 | ||
1255 | dynobj = htab->elf.dynobj; | |
1256 | ||
1257 | /* Set htab->elf.dynobj here so that there is no need to check and | |
1258 | set it in check_relocs. */ | |
1259 | if (dynobj == NULL) | |
1260 | { | |
1261 | if (pbfd != NULL) | |
1262 | { | |
1263 | htab->elf.dynobj = pbfd; | |
1264 | dynobj = pbfd; | |
1265 | } | |
1266 | else | |
1267 | { | |
1268 | bfd *abfd; | |
1269 | ||
1270 | /* Find a normal input file to hold linker created | |
1271 | sections. */ | |
1272 | for (abfd = info->input_bfds; | |
1273 | abfd != NULL; | |
1274 | abfd = abfd->link.next) | |
1275 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
1276 | && (abfd->flags | |
1277 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0) | |
1278 | { | |
1279 | htab->elf.dynobj = abfd; | |
1280 | dynobj = abfd; | |
1281 | break; | |
1282 | } | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | /* Even when lazy binding is disabled by "-z now", the PLT0 entry may | |
1287 | still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for | |
1288 | canonical function address. */ | |
1289 | htab->plt.has_plt0 = 1; | |
1290 | normal_target = plt_layout->normal_target; | |
1291 | ||
1292 | if (normal_target) | |
1293 | { | |
1294 | if (use_ibt_plt) | |
1295 | { | |
1296 | htab->lazy_plt = plt_layout->lazy_ibt_plt; | |
1297 | htab->non_lazy_plt = plt_layout->non_lazy_ibt_plt; | |
1298 | } | |
1299 | else | |
1300 | { | |
1301 | htab->lazy_plt = plt_layout->lazy_plt; | |
1302 | htab->non_lazy_plt = plt_layout->non_lazy_plt; | |
1303 | } | |
1304 | } | |
1305 | else | |
1306 | { | |
1307 | htab->lazy_plt = plt_layout->lazy_plt; | |
1308 | htab->non_lazy_plt = NULL; | |
1309 | } | |
1310 | ||
1311 | pltsec = htab->elf.splt; | |
1312 | ||
1313 | /* If the non-lazy PLT is available, use it for all PLT entries if | |
1314 | there are no PLT0 or no .plt section. */ | |
1315 | if (htab->non_lazy_plt != NULL | |
1316 | && (!htab->plt.has_plt0 || pltsec == NULL)) | |
1317 | { | |
1318 | lazy_plt = FALSE; | |
1319 | if (bfd_link_pic (info)) | |
1320 | htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry; | |
1321 | else | |
1322 | htab->plt.plt_entry = htab->non_lazy_plt->plt_entry; | |
1323 | htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size; | |
1324 | htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset; | |
1325 | htab->plt.plt_got_insn_size | |
1326 | = htab->non_lazy_plt->plt_got_insn_size; | |
1327 | htab->plt.eh_frame_plt_size | |
1328 | = htab->non_lazy_plt->eh_frame_plt_size; | |
1329 | htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt; | |
1330 | } | |
1331 | else | |
1332 | { | |
1333 | lazy_plt = TRUE; | |
1334 | if (bfd_link_pic (info)) | |
1335 | { | |
1336 | htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry; | |
1337 | htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry; | |
1338 | } | |
1339 | else | |
1340 | { | |
1341 | htab->plt.plt0_entry = htab->lazy_plt->plt0_entry; | |
1342 | htab->plt.plt_entry = htab->lazy_plt->plt_entry; | |
1343 | } | |
1344 | htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size; | |
1345 | htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset; | |
1346 | htab->plt.plt_got_insn_size | |
1347 | = htab->lazy_plt->plt_got_insn_size; | |
1348 | htab->plt.eh_frame_plt_size | |
1349 | = htab->lazy_plt->eh_frame_plt_size; | |
1350 | htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt; | |
1351 | } | |
1352 | ||
1353 | /* Return if there are no normal input files. */ | |
1354 | if (dynobj == NULL) | |
1355 | return pbfd; | |
1356 | ||
eeb2f20a | 1357 | if (htab->is_vxworks |
a6798bab L |
1358 | && !elf_vxworks_create_dynamic_sections (dynobj, info, |
1359 | &htab->srelplt2)) | |
1360 | { | |
1361 | info->callbacks->einfo (_("%F: failed to create VxWorks dynamic sections\n")); | |
1362 | return pbfd; | |
1363 | } | |
1364 | ||
1365 | /* Since create_dynamic_sections isn't always called, but GOT | |
1366 | relocations need GOT relocations, create them here so that we | |
1367 | don't need to do it in check_relocs. */ | |
1368 | if (htab->elf.sgot == NULL | |
1369 | && !_bfd_elf_create_got_section (dynobj, info)) | |
1370 | info->callbacks->einfo (_("%F: failed to create GOT sections\n")); | |
1371 | ||
1372 | got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2; | |
1373 | ||
1374 | /* Align .got and .got.plt sections to their entry size. Do it here | |
1375 | instead of in create_dynamic_sections so that they are always | |
1376 | properly aligned even if create_dynamic_sections isn't called. */ | |
1377 | sec = htab->elf.sgot; | |
1378 | if (!bfd_set_section_alignment (dynobj, sec, got_align)) | |
1379 | goto error_alignment; | |
1380 | ||
1381 | sec = htab->elf.sgotplt; | |
1382 | if (!bfd_set_section_alignment (dynobj, sec, got_align)) | |
1383 | goto error_alignment; | |
1384 | ||
1385 | /* Create the ifunc sections here so that check_relocs can be | |
1386 | simplified. */ | |
1387 | if (!_bfd_elf_create_ifunc_sections (dynobj, info)) | |
1388 | info->callbacks->einfo (_("%F: failed to create ifunc sections\n")); | |
1389 | ||
1390 | plt_alignment = bfd_log2 (htab->plt.plt_entry_size); | |
1391 | ||
1392 | if (pltsec != NULL) | |
1393 | { | |
1394 | /* Whe creating executable, set the contents of the .interp | |
1395 | section to the interpreter. */ | |
1396 | if (bfd_link_executable (info) && !info->nointerp) | |
1397 | { | |
1398 | asection *s = bfd_get_linker_section (dynobj, ".interp"); | |
1399 | if (s == NULL) | |
1400 | abort (); | |
1401 | s->size = htab->dynamic_interpreter_size; | |
1402 | s->contents = (unsigned char *) htab->dynamic_interpreter; | |
1403 | htab->interp = s; | |
1404 | } | |
1405 | ||
1406 | /* Don't change PLT section alignment for NaCl since it uses | |
1407 | 64-byte PLT entry and sets PLT section alignment to 32 | |
1408 | bytes. Don't create additional PLT sections for NaCl. */ | |
1409 | if (normal_target) | |
1410 | { | |
1411 | flagword pltflags = (bed->dynamic_sec_flags | |
1412 | | SEC_ALLOC | |
1413 | | SEC_CODE | |
1414 | | SEC_LOAD | |
1415 | | SEC_READONLY); | |
1416 | unsigned int non_lazy_plt_alignment | |
1417 | = bfd_log2 (htab->non_lazy_plt->plt_entry_size); | |
1418 | ||
1419 | sec = pltsec; | |
1420 | if (!bfd_set_section_alignment (sec->owner, sec, | |
1421 | plt_alignment)) | |
1422 | goto error_alignment; | |
1423 | ||
1424 | /* Create the GOT procedure linkage table. */ | |
1425 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
1426 | ".plt.got", | |
1427 | pltflags); | |
1428 | if (sec == NULL) | |
1429 | info->callbacks->einfo (_("%F: failed to create GOT PLT section\n")); | |
1430 | ||
1431 | if (!bfd_set_section_alignment (dynobj, sec, | |
1432 | non_lazy_plt_alignment)) | |
1433 | goto error_alignment; | |
1434 | ||
1435 | htab->plt_got = sec; | |
1436 | ||
1437 | if (lazy_plt) | |
1438 | { | |
1439 | sec = NULL; | |
1440 | ||
1441 | if (use_ibt_plt) | |
1442 | { | |
1443 | /* Create the second PLT for Intel IBT support. IBT | |
1444 | PLT is supported only for non-NaCl target and is | |
1445 | is needed only for lazy binding. */ | |
1446 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
1447 | ".plt.sec", | |
1448 | pltflags); | |
1449 | if (sec == NULL) | |
1450 | info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n")); | |
1451 | ||
1452 | if (!bfd_set_section_alignment (dynobj, sec, | |
1453 | plt_alignment)) | |
1454 | goto error_alignment; | |
1455 | } | |
1456 | else if (info->bndplt && ABI_64_P (dynobj)) | |
1457 | { | |
1458 | /* Create the second PLT for Intel MPX support. MPX | |
1459 | PLT is supported only for non-NaCl target in 64-bit | |
1460 | mode and is needed only for lazy binding. */ | |
1461 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
1462 | ".plt.sec", | |
1463 | pltflags); | |
1464 | if (sec == NULL) | |
1465 | info->callbacks->einfo (_("%F: failed to create BND PLT section\n")); | |
1466 | ||
1467 | if (!bfd_set_section_alignment (dynobj, sec, | |
1468 | non_lazy_plt_alignment)) | |
1469 | goto error_alignment; | |
1470 | } | |
1471 | ||
1472 | htab->plt_second = sec; | |
1473 | } | |
1474 | } | |
1475 | ||
1476 | if (!info->no_ld_generated_unwind_info) | |
1477 | { | |
1478 | flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
1479 | | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
1480 | | SEC_LINKER_CREATED); | |
1481 | ||
1482 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
1483 | ".eh_frame", | |
1484 | flags); | |
1485 | if (sec == NULL) | |
1486 | info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n")); | |
1487 | ||
1488 | if (!bfd_set_section_alignment (dynobj, sec, class_align)) | |
1489 | goto error_alignment; | |
1490 | ||
1491 | htab->plt_eh_frame = sec; | |
1492 | ||
1493 | if (htab->plt_got != NULL) | |
1494 | { | |
1495 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
1496 | ".eh_frame", | |
1497 | flags); | |
1498 | if (sec == NULL) | |
1499 | info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n")); | |
1500 | ||
1501 | if (!bfd_set_section_alignment (dynobj, sec, class_align)) | |
1502 | goto error_alignment; | |
1503 | ||
1504 | htab->plt_got_eh_frame = sec; | |
1505 | } | |
1506 | ||
1507 | if (htab->plt_second != NULL) | |
1508 | { | |
1509 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
1510 | ".eh_frame", | |
1511 | flags); | |
1512 | if (sec == NULL) | |
1513 | info->callbacks->einfo (_("%F: failed to create the second PLT .eh_frame section\n")); | |
1514 | ||
1515 | if (!bfd_set_section_alignment (dynobj, sec, class_align)) | |
1516 | goto error_alignment; | |
1517 | ||
1518 | htab->plt_second_eh_frame = sec; | |
1519 | } | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | if (normal_target) | |
1524 | { | |
1525 | /* The .iplt section is used for IFUNC symbols in static | |
1526 | executables. */ | |
1527 | sec = htab->elf.iplt; | |
1528 | if (sec != NULL | |
1529 | && !bfd_set_section_alignment (sec->owner, sec, | |
1530 | plt_alignment)) | |
1531 | goto error_alignment; | |
1532 | } | |
1533 | ||
1534 | return pbfd; | |
1535 | } |