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" | |
765e526c L |
22 | #include "objalloc.h" |
23 | #include "elf/i386.h" | |
24 | #include "elf/x86-64.h" | |
25 | ||
26 | /* The name of the dynamic interpreter. This is put in the .interp | |
27 | section. */ | |
28 | ||
29 | #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1" | |
30 | #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1" | |
31 | #define ELFX32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1" | |
0afcef53 L |
32 | |
33 | /* _TLS_MODULE_BASE_ needs to be treated especially when linking | |
34 | executables. Rather than setting it to the beginning of the TLS | |
35 | section, we have to set it to the end. This function may be called | |
36 | multiple times, it is idempotent. */ | |
37 | ||
38 | void | |
39 | _bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info) | |
40 | { | |
41 | struct elf_x86_link_hash_table *htab; | |
42 | struct bfd_link_hash_entry *base; | |
43 | const struct elf_backend_data *bed; | |
44 | ||
45 | if (!bfd_link_executable (info)) | |
46 | return; | |
47 | ||
48 | bed = get_elf_backend_data (info->output_bfd); | |
49 | htab = elf_x86_hash_table (info, bed->target_id); | |
50 | if (htab == NULL) | |
51 | return; | |
52 | ||
53 | base = htab->tls_module_base; | |
54 | if (base == NULL) | |
55 | return; | |
56 | ||
57 | base->u.def.value = htab->elf.tls_size; | |
58 | } | |
59 | ||
60 | /* Return the base VMA address which should be subtracted from real addresses | |
61 | when resolving @dtpoff relocation. | |
62 | This is PT_TLS segment p_vaddr. */ | |
63 | ||
64 | bfd_vma | |
65 | _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info) | |
66 | { | |
67 | /* If tls_sec is NULL, we should have signalled an error already. */ | |
68 | if (elf_hash_table (info)->tls_sec == NULL) | |
69 | return 0; | |
70 | return elf_hash_table (info)->tls_sec->vma; | |
71 | } | |
72 | ||
73 | /* Find any dynamic relocs that apply to read-only sections. */ | |
74 | ||
75 | bfd_boolean | |
76 | _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry *h, | |
77 | void *inf) | |
78 | { | |
79 | struct elf_x86_link_hash_entry *eh; | |
80 | struct elf_dyn_relocs *p; | |
81 | ||
82 | /* Skip local IFUNC symbols. */ | |
83 | if (h->forced_local && h->type == STT_GNU_IFUNC) | |
84 | return TRUE; | |
85 | ||
86 | eh = (struct elf_x86_link_hash_entry *) h; | |
87 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
88 | { | |
89 | asection *s = p->sec->output_section; | |
90 | ||
91 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
92 | { | |
93 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
94 | ||
95 | info->flags |= DF_TEXTREL; | |
96 | ||
97 | if ((info->warn_shared_textrel && bfd_link_pic (info)) | |
98 | || info->error_textrel) | |
99 | /* xgettext:c-format */ | |
100 | info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"), | |
101 | p->sec->owner, h->root.root.string, | |
102 | p->sec); | |
103 | ||
104 | /* Not an error, just cut short the traversal. */ | |
105 | return FALSE; | |
106 | } | |
107 | } | |
108 | return TRUE; | |
109 | } | |
110 | ||
111 | /* Find and/or create a hash entry for local symbol. */ | |
112 | ||
113 | struct elf_link_hash_entry * | |
114 | _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab, | |
115 | bfd *abfd, const Elf_Internal_Rela *rel, | |
116 | bfd_boolean create) | |
117 | { | |
118 | struct elf_x86_link_hash_entry e, *ret; | |
119 | asection *sec = abfd->sections; | |
120 | hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id, | |
121 | htab->r_sym (rel->r_info)); | |
122 | void **slot; | |
123 | ||
124 | e.elf.indx = sec->id; | |
125 | e.elf.dynstr_index = htab->r_sym (rel->r_info); | |
126 | slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h, | |
127 | create ? INSERT : NO_INSERT); | |
128 | ||
129 | if (!slot) | |
130 | return NULL; | |
131 | ||
132 | if (*slot) | |
133 | { | |
134 | ret = (struct elf_x86_link_hash_entry *) *slot; | |
135 | return &ret->elf; | |
136 | } | |
137 | ||
138 | ret = (struct elf_x86_link_hash_entry *) | |
139 | objalloc_alloc ((struct objalloc *) htab->loc_hash_memory, | |
140 | sizeof (struct elf_x86_link_hash_entry)); | |
141 | if (ret) | |
142 | { | |
143 | memset (ret, 0, sizeof (*ret)); | |
144 | ret->elf.indx = sec->id; | |
145 | ret->elf.dynstr_index = htab->r_sym (rel->r_info); | |
146 | ret->elf.dynindx = -1; | |
147 | ret->plt_got.offset = (bfd_vma) -1; | |
148 | *slot = ret; | |
149 | } | |
150 | return &ret->elf; | |
151 | } | |
152 | ||
153 | /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN | |
154 | SYNC WITH _bfd_elf_link_hash_newfunc. */ | |
155 | ||
156 | struct bfd_hash_entry * | |
157 | _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
158 | struct bfd_hash_table *table, | |
159 | const char *string) | |
160 | { | |
161 | /* Allocate the structure if it has not already been allocated by a | |
162 | subclass. */ | |
163 | if (entry == NULL) | |
164 | { | |
165 | entry = (struct bfd_hash_entry *) | |
166 | bfd_hash_allocate (table, | |
167 | sizeof (struct elf_x86_link_hash_entry)); | |
168 | if (entry == NULL) | |
169 | return entry; | |
170 | } | |
171 | ||
172 | /* Call the allocation method of the superclass. */ | |
173 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
174 | if (entry != NULL) | |
175 | { | |
176 | struct elf_x86_link_hash_entry *eh | |
177 | = (struct elf_x86_link_hash_entry *) entry; | |
178 | struct elf_link_hash_table *htab | |
179 | = (struct elf_link_hash_table *) table; | |
180 | ||
181 | memset (&eh->elf.size, 0, | |
182 | (sizeof (struct elf_x86_link_hash_entry) | |
183 | - offsetof (struct elf_link_hash_entry, size))); | |
184 | /* Set local fields. */ | |
185 | eh->elf.indx = -1; | |
186 | eh->elf.dynindx = -1; | |
187 | eh->elf.got = htab->init_got_refcount; | |
188 | eh->elf.plt = htab->init_plt_refcount; | |
189 | /* Assume that we have been called by a non-ELF symbol reader. | |
190 | This flag is then reset by the code which reads an ELF input | |
191 | file. This ensures that a symbol created by a non-ELF symbol | |
192 | reader will have the flag set correctly. */ | |
193 | eh->elf.non_elf = 1; | |
194 | eh->plt_second.offset = (bfd_vma) -1; | |
195 | eh->plt_got.offset = (bfd_vma) -1; | |
196 | eh->tlsdesc_got = (bfd_vma) -1; | |
197 | } | |
198 | ||
199 | return entry; | |
200 | } | |
201 | ||
202 | /* Compute a hash of a local hash entry. We use elf_link_hash_entry | |
203 | for local symbol so that we can handle local STT_GNU_IFUNC symbols | |
204 | as global symbol. We reuse indx and dynstr_index for local symbol | |
205 | hash since they aren't used by global symbols in this backend. */ | |
206 | ||
207 | hashval_t | |
208 | _bfd_x86_elf_local_htab_hash (const void *ptr) | |
209 | { | |
210 | struct elf_link_hash_entry *h | |
211 | = (struct elf_link_hash_entry *) ptr; | |
212 | return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index); | |
213 | } | |
214 | ||
215 | /* Compare local hash entries. */ | |
216 | ||
217 | int | |
218 | _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2) | |
219 | { | |
220 | struct elf_link_hash_entry *h1 | |
221 | = (struct elf_link_hash_entry *) ptr1; | |
222 | struct elf_link_hash_entry *h2 | |
223 | = (struct elf_link_hash_entry *) ptr2; | |
224 | ||
225 | return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index; | |
226 | } | |
227 | ||
228 | /* Destroy an x86 ELF linker hash table. */ | |
229 | ||
765e526c L |
230 | static void |
231 | elf_x86_link_hash_table_free (bfd *obfd) | |
0afcef53 L |
232 | { |
233 | struct elf_x86_link_hash_table *htab | |
234 | = (struct elf_x86_link_hash_table *) obfd->link.hash; | |
235 | ||
236 | if (htab->loc_hash_table) | |
237 | htab_delete (htab->loc_hash_table); | |
238 | if (htab->loc_hash_memory) | |
239 | objalloc_free ((struct objalloc *) htab->loc_hash_memory); | |
240 | _bfd_elf_link_hash_table_free (obfd); | |
241 | } | |
242 | ||
765e526c L |
243 | /* Create an x86 ELF linker hash table. */ |
244 | ||
245 | struct bfd_link_hash_table * | |
246 | _bfd_x86_elf_link_hash_table_create (bfd *abfd) | |
247 | { | |
248 | struct elf_x86_link_hash_table *ret; | |
249 | const struct elf_backend_data *bed; | |
250 | bfd_size_type amt = sizeof (struct elf_x86_link_hash_table); | |
251 | ||
252 | ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt); | |
253 | if (ret == NULL) | |
254 | return NULL; | |
255 | ||
256 | bed = get_elf_backend_data (abfd); | |
257 | if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, | |
258 | _bfd_x86_elf_link_hash_newfunc, | |
259 | sizeof (struct elf_x86_link_hash_entry), | |
260 | bed->target_id)) | |
261 | { | |
262 | free (ret); | |
263 | return NULL; | |
264 | } | |
265 | ||
266 | #ifdef BFD64 | |
267 | if (ABI_64_P (abfd)) | |
268 | { | |
269 | ret->r_info = elf64_r_info; | |
270 | ret->r_sym = elf64_r_sym; | |
271 | ret->pointer_r_type = R_X86_64_64; | |
272 | ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER; | |
273 | ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER; | |
274 | ret->tls_get_addr = "__tls_get_addr"; | |
275 | } | |
276 | else | |
277 | #endif | |
278 | { | |
279 | ret->r_info = elf32_r_info; | |
280 | ret->r_sym = elf32_r_sym; | |
5b86dbf4 | 281 | if (bed->target_id == X86_64_ELF_DATA) |
765e526c L |
282 | { |
283 | ret->pointer_r_type = R_X86_64_32; | |
284 | ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER; | |
285 | ret->dynamic_interpreter_size | |
286 | = sizeof ELFX32_DYNAMIC_INTERPRETER; | |
287 | ret->tls_get_addr = "__tls_get_addr"; | |
288 | } | |
289 | else | |
290 | { | |
291 | ret->pointer_r_type = R_386_32; | |
292 | ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER; | |
293 | ret->dynamic_interpreter_size | |
294 | = sizeof ELF32_DYNAMIC_INTERPRETER; | |
295 | ret->tls_get_addr = "___tls_get_addr"; | |
296 | } | |
297 | } | |
298 | ||
299 | ret->loc_hash_table = htab_try_create (1024, | |
300 | _bfd_x86_elf_local_htab_hash, | |
301 | _bfd_x86_elf_local_htab_eq, | |
302 | NULL); | |
303 | ret->loc_hash_memory = objalloc_create (); | |
304 | if (!ret->loc_hash_table || !ret->loc_hash_memory) | |
305 | { | |
306 | elf_x86_link_hash_table_free (abfd); | |
307 | return NULL; | |
308 | } | |
309 | ret->elf.root.hash_table_free = elf_x86_link_hash_table_free; | |
310 | ||
311 | return &ret->elf.root; | |
312 | } | |
313 | ||
0afcef53 L |
314 | /* Sort relocs into address order. */ |
315 | ||
316 | int | |
317 | _bfd_x86_elf_compare_relocs (const void *ap, const void *bp) | |
318 | { | |
319 | const arelent *a = * (const arelent **) ap; | |
320 | const arelent *b = * (const arelent **) bp; | |
321 | ||
322 | if (a->address > b->address) | |
323 | return 1; | |
324 | else if (a->address < b->address) | |
325 | return -1; | |
326 | else | |
327 | return 0; | |
328 | } | |
329 | ||
330 | bfd_boolean | |
331 | _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
332 | { | |
333 | if (!bfd_link_relocatable (info)) | |
334 | { | |
335 | /* Check for __tls_get_addr reference. */ | |
336 | struct elf_x86_link_hash_table *htab; | |
337 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
338 | htab = elf_x86_hash_table (info, bed->target_id); | |
339 | if (htab) | |
340 | { | |
341 | struct elf_link_hash_entry *h | |
342 | = elf_link_hash_lookup (elf_hash_table (info), | |
343 | htab->tls_get_addr, | |
344 | FALSE, FALSE, FALSE); | |
345 | if (h != NULL) | |
346 | ((struct elf_x86_link_hash_entry *) h)->tls_get_addr = 1; | |
347 | } | |
348 | } | |
349 | ||
350 | /* Invoke the regular ELF backend linker to do all the work. */ | |
351 | return _bfd_elf_link_check_relocs (abfd, info); | |
352 | } | |
353 | ||
354 | bfd_boolean | |
355 | _bfd_x86_elf_always_size_sections (bfd *output_bfd, | |
356 | struct bfd_link_info *info) | |
357 | { | |
358 | asection *tls_sec = elf_hash_table (info)->tls_sec; | |
359 | ||
360 | if (tls_sec) | |
361 | { | |
362 | struct elf_link_hash_entry *tlsbase; | |
363 | ||
364 | tlsbase = elf_link_hash_lookup (elf_hash_table (info), | |
365 | "_TLS_MODULE_BASE_", | |
366 | FALSE, FALSE, FALSE); | |
367 | ||
368 | if (tlsbase && tlsbase->type == STT_TLS) | |
369 | { | |
370 | struct elf_x86_link_hash_table *htab; | |
371 | struct bfd_link_hash_entry *bh = NULL; | |
372 | const struct elf_backend_data *bed | |
373 | = get_elf_backend_data (output_bfd); | |
374 | ||
375 | htab = elf_x86_hash_table (info, bed->target_id); | |
376 | if (htab == NULL) | |
377 | return FALSE; | |
378 | ||
379 | if (!(_bfd_generic_link_add_one_symbol | |
380 | (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL, | |
381 | tls_sec, 0, NULL, FALSE, | |
382 | bed->collect, &bh))) | |
383 | return FALSE; | |
384 | ||
385 | htab->tls_module_base = bh; | |
386 | ||
387 | tlsbase = (struct elf_link_hash_entry *)bh; | |
388 | tlsbase->def_regular = 1; | |
389 | tlsbase->other = STV_HIDDEN; | |
390 | tlsbase->root.linker_def = 1; | |
391 | (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE); | |
392 | } | |
393 | } | |
394 | ||
395 | return TRUE; | |
396 | } | |
397 | ||
398 | void | |
399 | _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h, | |
400 | const Elf_Internal_Sym *isym, | |
401 | bfd_boolean definition, | |
402 | bfd_boolean dynamic ATTRIBUTE_UNUSED) | |
403 | { | |
404 | if (definition) | |
405 | { | |
406 | struct elf_x86_link_hash_entry *eh | |
407 | = (struct elf_x86_link_hash_entry *) h; | |
408 | eh->def_protected = (ELF_ST_VISIBILITY (isym->st_other) | |
409 | == STV_PROTECTED); | |
410 | } | |
411 | } | |
412 | ||
413 | /* Copy the extra info we tack onto an elf_link_hash_entry. */ | |
414 | ||
415 | void | |
416 | _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info, | |
417 | struct elf_link_hash_entry *dir, | |
418 | struct elf_link_hash_entry *ind) | |
419 | { | |
420 | struct elf_x86_link_hash_entry *edir, *eind; | |
421 | ||
422 | edir = (struct elf_x86_link_hash_entry *) dir; | |
423 | eind = (struct elf_x86_link_hash_entry *) ind; | |
424 | ||
425 | if (eind->dyn_relocs != NULL) | |
426 | { | |
427 | if (edir->dyn_relocs != NULL) | |
428 | { | |
429 | struct elf_dyn_relocs **pp; | |
430 | struct elf_dyn_relocs *p; | |
431 | ||
432 | /* Add reloc counts against the indirect sym to the direct sym | |
433 | list. Merge any entries against the same section. */ | |
434 | for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) | |
435 | { | |
436 | struct elf_dyn_relocs *q; | |
437 | ||
438 | for (q = edir->dyn_relocs; q != NULL; q = q->next) | |
439 | if (q->sec == p->sec) | |
440 | { | |
441 | q->pc_count += p->pc_count; | |
442 | q->count += p->count; | |
443 | *pp = p->next; | |
444 | break; | |
445 | } | |
446 | if (q == NULL) | |
447 | pp = &p->next; | |
448 | } | |
449 | *pp = edir->dyn_relocs; | |
450 | } | |
451 | ||
452 | edir->dyn_relocs = eind->dyn_relocs; | |
453 | eind->dyn_relocs = NULL; | |
454 | } | |
455 | ||
456 | if (ind->root.type == bfd_link_hash_indirect | |
457 | && dir->got.refcount <= 0) | |
458 | { | |
459 | edir->tls_type = eind->tls_type; | |
460 | eind->tls_type = GOT_UNKNOWN; | |
461 | } | |
462 | ||
463 | /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will | |
464 | generate a R_386_COPY reloc. */ | |
465 | edir->gotoff_ref |= eind->gotoff_ref; | |
466 | ||
467 | edir->has_got_reloc |= eind->has_got_reloc; | |
468 | edir->has_non_got_reloc |= eind->has_non_got_reloc; | |
469 | ||
470 | if (ELIMINATE_COPY_RELOCS | |
471 | && ind->root.type != bfd_link_hash_indirect | |
472 | && dir->dynamic_adjusted) | |
473 | { | |
474 | /* If called to transfer flags for a weakdef during processing | |
475 | of elf_adjust_dynamic_symbol, don't copy non_got_ref. | |
476 | We clear it ourselves for ELIMINATE_COPY_RELOCS. */ | |
477 | if (dir->versioned != versioned_hidden) | |
478 | dir->ref_dynamic |= ind->ref_dynamic; | |
479 | dir->ref_regular |= ind->ref_regular; | |
480 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
481 | dir->needs_plt |= ind->needs_plt; | |
482 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
483 | } | |
484 | else | |
485 | { | |
486 | if (eind->func_pointer_refcount > 0) | |
487 | { | |
488 | edir->func_pointer_refcount += eind->func_pointer_refcount; | |
489 | eind->func_pointer_refcount = 0; | |
490 | } | |
491 | ||
492 | _bfd_elf_link_hash_copy_indirect (info, dir, ind); | |
493 | } | |
494 | } | |
495 | ||
496 | /* Remove undefined weak symbol from the dynamic symbol table if it | |
497 | is resolved to 0. */ | |
498 | ||
499 | bfd_boolean | |
500 | _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info, | |
501 | struct elf_link_hash_entry *h) | |
502 | { | |
503 | if (h->dynindx != -1) | |
504 | { | |
505 | const struct elf_backend_data *bed | |
506 | = get_elf_backend_data (info->output_bfd); | |
507 | if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, | |
508 | bed->target_id, | |
509 | elf_x86_hash_entry (h)->has_got_reloc, | |
510 | elf_x86_hash_entry (h))) | |
511 | { | |
512 | h->dynindx = -1; | |
513 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
514 | h->dynstr_index); | |
515 | } | |
516 | } | |
517 | return TRUE; | |
518 | } | |
519 | ||
520 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
521 | ||
522 | bfd_boolean | |
523 | _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h) | |
524 | { | |
525 | if (h->plt.offset != (bfd_vma) -1 | |
526 | && !h->def_regular | |
527 | && !h->pointer_equality_needed) | |
528 | return FALSE; | |
529 | ||
530 | return _bfd_elf_hash_symbol (h); | |
531 | } | |
532 | ||
f493882d L |
533 | static bfd_vma |
534 | elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED, | |
535 | bfd_vma off, | |
536 | bfd_vma offset ATTRIBUTE_UNUSED, | |
537 | bfd_vma got_addr) | |
538 | { | |
539 | return got_addr + off; | |
540 | } | |
541 | ||
542 | static bfd_vma | |
543 | elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p, | |
544 | bfd_vma off, | |
545 | bfd_vma offset, | |
546 | bfd_vma got_addr ATTRIBUTE_UNUSED) | |
547 | { | |
548 | return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size; | |
549 | } | |
550 | ||
551 | static bfd_boolean | |
552 | elf_i386_valid_plt_reloc_p (unsigned int type) | |
553 | { | |
554 | return (type == R_386_JUMP_SLOT | |
555 | || type == R_386_GLOB_DAT | |
556 | || type == R_386_IRELATIVE); | |
557 | } | |
558 | ||
559 | static bfd_boolean | |
560 | elf_x86_64_valid_plt_reloc_p (unsigned int type) | |
561 | { | |
562 | return (type == R_X86_64_JUMP_SLOT | |
563 | || type == R_X86_64_GLOB_DAT | |
564 | || type == R_X86_64_IRELATIVE); | |
565 | } | |
566 | ||
567 | long | |
568 | _bfd_x86_elf_get_synthetic_symtab (bfd *abfd, | |
569 | long count, | |
570 | long relsize, | |
571 | bfd_vma got_addr, | |
572 | struct elf_x86_plt plts[], | |
573 | asymbol **dynsyms, | |
574 | asymbol **ret) | |
575 | { | |
576 | long size, i, n, len; | |
577 | int j; | |
578 | unsigned int plt_got_offset, plt_entry_size; | |
579 | asymbol *s; | |
580 | bfd_byte *plt_contents; | |
581 | long dynrelcount; | |
582 | arelent **dynrelbuf, *p; | |
583 | char *names; | |
584 | const struct elf_backend_data *bed; | |
585 | bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma, | |
586 | bfd_vma); | |
587 | bfd_boolean (*valid_plt_reloc_p) (unsigned int); | |
588 | ||
589 | if (count == 0) | |
590 | return -1; | |
591 | ||
592 | dynrelbuf = (arelent **) bfd_malloc (relsize); | |
593 | if (dynrelbuf == NULL) | |
594 | return -1; | |
595 | ||
596 | dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf, | |
597 | dynsyms); | |
598 | ||
599 | /* Sort the relocs by address. */ | |
600 | qsort (dynrelbuf, dynrelcount, sizeof (arelent *), | |
601 | _bfd_x86_elf_compare_relocs); | |
602 | ||
603 | size = count * sizeof (asymbol); | |
604 | ||
605 | /* Allocate space for @plt suffixes. */ | |
606 | n = 0; | |
607 | for (i = 0; i < dynrelcount; i++) | |
608 | { | |
609 | p = dynrelbuf[i]; | |
610 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
611 | if (p->addend != 0) | |
612 | size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd); | |
613 | } | |
614 | ||
615 | s = *ret = (asymbol *) bfd_zmalloc (size); | |
616 | if (s == NULL) | |
617 | goto bad_return; | |
618 | ||
619 | bed = get_elf_backend_data (abfd); | |
620 | ||
621 | if (bed->target_id == X86_64_ELF_DATA) | |
622 | { | |
623 | get_plt_got_vma = elf_x86_64_get_plt_got_vma; | |
624 | valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p; | |
625 | } | |
626 | else | |
627 | { | |
628 | get_plt_got_vma = elf_i386_get_plt_got_vma; | |
629 | valid_plt_reloc_p = elf_i386_valid_plt_reloc_p; | |
630 | if (got_addr) | |
631 | { | |
632 | /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_ | |
633 | address. */ | |
634 | asection *sec = bfd_get_section_by_name (abfd, ".got.plt"); | |
635 | if (sec != NULL) | |
636 | got_addr = sec->vma; | |
637 | else | |
638 | { | |
639 | sec = bfd_get_section_by_name (abfd, ".got"); | |
640 | if (sec != NULL) | |
641 | got_addr = sec->vma; | |
642 | } | |
643 | ||
644 | if (got_addr == (bfd_vma) -1) | |
645 | goto bad_return; | |
646 | } | |
647 | } | |
648 | ||
649 | /* Check for each PLT section. */ | |
650 | names = (char *) (s + count); | |
651 | size = 0; | |
652 | n = 0; | |
653 | for (j = 0; plts[j].name != NULL; j++) | |
654 | if ((plt_contents = plts[j].contents) != NULL) | |
655 | { | |
656 | long k; | |
657 | bfd_vma offset; | |
658 | asection *plt; | |
659 | struct elf_x86_plt *plt_p = &plts[j]; | |
660 | ||
661 | plt_got_offset = plt_p->plt_got_offset; | |
662 | plt_entry_size = plt_p->plt_entry_size; | |
663 | ||
664 | plt = plt_p->sec; | |
665 | ||
666 | if ((plt_p->type & plt_lazy)) | |
667 | { | |
668 | /* Skip PLT0 in lazy PLT. */ | |
669 | k = 1; | |
670 | offset = plt_entry_size; | |
671 | } | |
672 | else | |
673 | { | |
674 | k = 0; | |
675 | offset = 0; | |
676 | } | |
677 | ||
678 | /* Check each PLT entry against dynamic relocations. */ | |
679 | for (; k < plt_p->count; k++) | |
680 | { | |
681 | int off; | |
682 | bfd_vma got_vma; | |
683 | long min, max, mid; | |
684 | ||
685 | /* Get the GOT offset for i386 or the PC-relative offset | |
686 | for x86-64, a signed 32-bit integer. */ | |
687 | off = H_GET_32 (abfd, (plt_contents + offset | |
688 | + plt_got_offset)); | |
689 | got_vma = get_plt_got_vma (plt_p, off, offset, got_addr); | |
690 | ||
691 | /* Binary search. */ | |
692 | p = dynrelbuf[0]; | |
693 | min = 0; | |
694 | max = dynrelcount; | |
695 | while ((min + 1) < max) | |
696 | { | |
697 | arelent *r; | |
698 | ||
699 | mid = (min + max) / 2; | |
700 | r = dynrelbuf[mid]; | |
701 | if (got_vma > r->address) | |
702 | min = mid; | |
703 | else if (got_vma < r->address) | |
704 | max = mid; | |
705 | else | |
706 | { | |
707 | p = r; | |
708 | break; | |
709 | } | |
710 | } | |
711 | ||
712 | /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */ | |
713 | if (got_vma == p->address | |
714 | && p->howto != NULL | |
715 | && valid_plt_reloc_p (p->howto->type)) | |
716 | { | |
717 | *s = **p->sym_ptr_ptr; | |
718 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL | |
719 | set. Since we are defining a symbol, ensure one | |
720 | of them is set. */ | |
721 | if ((s->flags & BSF_LOCAL) == 0) | |
722 | s->flags |= BSF_GLOBAL; | |
723 | s->flags |= BSF_SYNTHETIC; | |
724 | /* This is no longer a section symbol. */ | |
725 | s->flags &= ~BSF_SECTION_SYM; | |
726 | s->section = plt; | |
727 | s->the_bfd = plt->owner; | |
728 | s->value = offset; | |
729 | s->udata.p = NULL; | |
730 | s->name = names; | |
731 | len = strlen ((*p->sym_ptr_ptr)->name); | |
732 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
733 | names += len; | |
734 | if (p->addend != 0) | |
735 | { | |
736 | char buf[30], *a; | |
737 | ||
738 | memcpy (names, "+0x", sizeof ("+0x") - 1); | |
739 | names += sizeof ("+0x") - 1; | |
740 | bfd_sprintf_vma (abfd, buf, p->addend); | |
741 | for (a = buf; *a == '0'; ++a) | |
742 | ; | |
743 | size = strlen (a); | |
744 | memcpy (names, a, size); | |
745 | names += size; | |
746 | } | |
747 | memcpy (names, "@plt", sizeof ("@plt")); | |
748 | names += sizeof ("@plt"); | |
749 | n++; | |
750 | s++; | |
751 | } | |
752 | offset += plt_entry_size; | |
753 | } | |
754 | } | |
755 | ||
756 | /* PLT entries with R_386_TLS_DESC relocations are skipped. */ | |
757 | if (n == 0) | |
758 | { | |
759 | bad_return: | |
760 | count = -1; | |
761 | } | |
762 | else | |
763 | count = n; | |
764 | ||
765 | for (j = 0; plts[j].name != NULL; j++) | |
766 | if (plts[j].contents != NULL) | |
767 | free (plts[j].contents); | |
768 | ||
769 | free (dynrelbuf); | |
770 | ||
771 | return count; | |
772 | } | |
773 | ||
0afcef53 L |
774 | /* Parse x86 GNU properties. */ |
775 | ||
776 | enum elf_property_kind | |
777 | _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type, | |
778 | bfd_byte *ptr, unsigned int datasz) | |
779 | { | |
780 | elf_property *prop; | |
781 | ||
782 | switch (type) | |
783 | { | |
784 | case GNU_PROPERTY_X86_ISA_1_USED: | |
785 | case GNU_PROPERTY_X86_ISA_1_NEEDED: | |
786 | case GNU_PROPERTY_X86_FEATURE_1_AND: | |
787 | if (datasz != 4) | |
788 | { | |
789 | _bfd_error_handler | |
790 | ((type == GNU_PROPERTY_X86_ISA_1_USED | |
791 | ? _("error: %B: <corrupt x86 ISA used size: 0x%x>") | |
792 | : (type == GNU_PROPERTY_X86_ISA_1_NEEDED | |
793 | ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>") | |
794 | : _("error: %B: <corrupt x86 feature size: 0x%x>"))), | |
795 | abfd, datasz); | |
796 | return property_corrupt; | |
797 | } | |
798 | prop = _bfd_elf_get_property (abfd, type, datasz); | |
799 | /* Combine properties of the same type. */ | |
800 | prop->u.number |= bfd_h_get_32 (abfd, ptr); | |
801 | prop->pr_kind = property_number; | |
802 | break; | |
803 | ||
804 | default: | |
805 | return property_ignored; | |
806 | } | |
807 | ||
808 | return property_number; | |
809 | } | |
810 | ||
811 | /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL, | |
812 | return TRUE if APROP is updated. Otherwise, return TRUE if BPROP | |
813 | should be merged with ABFD. */ | |
814 | ||
815 | bfd_boolean | |
816 | _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info, | |
817 | bfd *abfd ATTRIBUTE_UNUSED, | |
818 | elf_property *aprop, | |
819 | elf_property *bprop) | |
820 | { | |
821 | unsigned int number, features; | |
822 | bfd_boolean updated = FALSE; | |
823 | unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type; | |
824 | ||
825 | switch (pr_type) | |
826 | { | |
827 | case GNU_PROPERTY_X86_ISA_1_USED: | |
828 | case GNU_PROPERTY_X86_ISA_1_NEEDED: | |
829 | if (aprop != NULL && bprop != NULL) | |
830 | { | |
831 | number = aprop->u.number; | |
832 | aprop->u.number = number | bprop->u.number; | |
833 | updated = number != (unsigned int) aprop->u.number; | |
834 | } | |
835 | else | |
836 | { | |
837 | /* Return TRUE if APROP is NULL to indicate that BPROP should | |
838 | be added to ABFD. */ | |
839 | updated = aprop == NULL; | |
840 | } | |
841 | break; | |
842 | ||
843 | case GNU_PROPERTY_X86_FEATURE_1_AND: | |
844 | /* Only one of APROP and BPROP can be NULL: | |
845 | 1. APROP & BPROP when both APROP and BPROP aren't NULL. | |
846 | 2. If APROP is NULL, remove x86 feature. | |
847 | 3. Otherwise, do nothing. | |
848 | */ | |
849 | if (aprop != NULL && bprop != NULL) | |
850 | { | |
851 | features = 0; | |
852 | if (info->ibt) | |
853 | features = GNU_PROPERTY_X86_FEATURE_1_IBT; | |
854 | if (info->shstk) | |
855 | features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; | |
856 | number = aprop->u.number; | |
857 | /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and | |
858 | GNU_PROPERTY_X86_FEATURE_1_SHSTK. */ | |
859 | aprop->u.number = (number & bprop->u.number) | features; | |
860 | updated = number != (unsigned int) aprop->u.number; | |
861 | /* Remove the property if all feature bits are cleared. */ | |
862 | if (aprop->u.number == 0) | |
863 | aprop->pr_kind = property_remove; | |
864 | } | |
865 | else | |
866 | { | |
867 | features = 0; | |
868 | if (info->ibt) | |
869 | features = GNU_PROPERTY_X86_FEATURE_1_IBT; | |
870 | if (info->shstk) | |
871 | features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; | |
872 | if (features) | |
873 | { | |
874 | /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and | |
875 | GNU_PROPERTY_X86_FEATURE_1_SHSTK. */ | |
876 | if (aprop != NULL) | |
877 | { | |
878 | number = aprop->u.number; | |
879 | aprop->u.number = number | features; | |
880 | updated = number != (unsigned int) aprop->u.number; | |
881 | } | |
882 | else | |
883 | { | |
884 | bprop->u.number |= features; | |
885 | updated = TRUE; | |
886 | } | |
887 | } | |
888 | else if (aprop != NULL) | |
889 | { | |
890 | aprop->pr_kind = property_remove; | |
891 | updated = TRUE; | |
892 | } | |
893 | } | |
894 | break; | |
895 | ||
896 | default: | |
897 | /* Never should happen. */ | |
898 | abort (); | |
899 | } | |
900 | ||
901 | return updated; | |
902 | } |