Update my e-mail address.
[binutils-gdb.git] / bfd / elf-attrs.c
blob761a4ceb2ccdaaac923d952dabd54d23acb9af01
1 /* ELF attributes support (based on ARM EABI attributes).
2 Copyright (C) 2005-2017 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "libiberty.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
27 /* Return the number of bytes needed by I in uleb128 format. */
28 static int
29 uleb128_size (unsigned int i)
31 int size;
32 size = 1;
33 while (i >= 0x80)
35 i >>= 7;
36 size++;
38 return size;
41 /* Return TRUE if the attribute has the default value (0/""). */
42 static bfd_boolean
43 is_default_attr (obj_attribute *attr)
45 if (ATTR_TYPE_HAS_INT_VAL (attr->type) && attr->i != 0)
46 return FALSE;
47 if (ATTR_TYPE_HAS_STR_VAL (attr->type) && attr->s && *attr->s)
48 return FALSE;
49 if (ATTR_TYPE_HAS_NO_DEFAULT (attr->type))
50 return FALSE;
52 return TRUE;
55 /* Return the size of a single attribute. */
56 static bfd_vma
57 obj_attr_size (unsigned int tag, obj_attribute *attr)
59 bfd_vma size;
61 if (is_default_attr (attr))
62 return 0;
64 size = uleb128_size (tag);
65 if (ATTR_TYPE_HAS_INT_VAL (attr->type))
66 size += uleb128_size (attr->i);
67 if (ATTR_TYPE_HAS_STR_VAL (attr->type))
68 size += strlen ((char *)attr->s) + 1;
69 return size;
72 /* Return the vendor name for a given object attributes section. */
73 static const char *
74 vendor_obj_attr_name (bfd *abfd, int vendor)
76 return (vendor == OBJ_ATTR_PROC
77 ? get_elf_backend_data (abfd)->obj_attrs_vendor
78 : "gnu");
81 /* Return the size of the object attributes section for VENDOR
82 (OBJ_ATTR_PROC or OBJ_ATTR_GNU), or 0 if there are no attributes
83 for that vendor to record and the vendor is OBJ_ATTR_GNU. */
84 static bfd_vma
85 vendor_obj_attr_size (bfd *abfd, int vendor)
87 bfd_vma size;
88 obj_attribute *attr;
89 obj_attribute_list *list;
90 int i;
91 const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
93 if (!vendor_name)
94 return 0;
96 attr = elf_known_obj_attributes (abfd)[vendor];
97 size = 0;
98 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
99 size += obj_attr_size (i, &attr[i]);
101 for (list = elf_other_obj_attributes (abfd)[vendor];
102 list;
103 list = list->next)
104 size += obj_attr_size (list->tag, &list->attr);
106 /* <size> <vendor_name> NUL 0x1 <size> */
107 return ((size || vendor == OBJ_ATTR_PROC)
108 ? size + 10 + strlen (vendor_name)
109 : 0);
112 /* Return the size of the object attributes section. */
113 bfd_vma
114 bfd_elf_obj_attr_size (bfd *abfd)
116 bfd_vma size;
118 size = vendor_obj_attr_size (abfd, OBJ_ATTR_PROC);
119 size += vendor_obj_attr_size (abfd, OBJ_ATTR_GNU);
121 /* 'A' <sections for each vendor> */
122 return (size ? size + 1 : 0);
125 /* Write VAL in uleb128 format to P, returning a pointer to the
126 following byte. */
127 static bfd_byte *
128 write_uleb128 (bfd_byte *p, unsigned int val)
130 bfd_byte c;
133 c = val & 0x7f;
134 val >>= 7;
135 if (val)
136 c |= 0x80;
137 *(p++) = c;
139 while (val);
140 return p;
143 /* Write attribute ATTR to butter P, and return a pointer to the following
144 byte. */
145 static bfd_byte *
146 write_obj_attribute (bfd_byte *p, unsigned int tag, obj_attribute *attr)
148 /* Suppress default entries. */
149 if (is_default_attr (attr))
150 return p;
152 p = write_uleb128 (p, tag);
153 if (ATTR_TYPE_HAS_INT_VAL (attr->type))
154 p = write_uleb128 (p, attr->i);
155 if (ATTR_TYPE_HAS_STR_VAL (attr->type))
157 int len;
159 len = strlen (attr->s) + 1;
160 memcpy (p, attr->s, len);
161 p += len;
164 return p;
167 /* Write the contents of the object attributes section (length SIZE)
168 for VENDOR to CONTENTS. */
169 static void
170 vendor_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size,
171 int vendor)
173 bfd_byte *p;
174 obj_attribute *attr;
175 obj_attribute_list *list;
176 int i;
177 const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
178 size_t vendor_length = strlen (vendor_name) + 1;
180 p = contents;
181 bfd_put_32 (abfd, size, p);
182 p += 4;
183 memcpy (p, vendor_name, vendor_length);
184 p += vendor_length;
185 *(p++) = Tag_File;
186 bfd_put_32 (abfd, size - 4 - vendor_length, p);
187 p += 4;
189 attr = elf_known_obj_attributes (abfd)[vendor];
190 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
192 unsigned int tag = i;
193 if (get_elf_backend_data (abfd)->obj_attrs_order)
194 tag = get_elf_backend_data (abfd)->obj_attrs_order (i);
195 p = write_obj_attribute (p, tag, &attr[tag]);
198 for (list = elf_other_obj_attributes (abfd)[vendor];
199 list;
200 list = list->next)
201 p = write_obj_attribute (p, list->tag, &list->attr);
204 /* Write the contents of the object attributes section to CONTENTS. */
205 void
206 bfd_elf_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size)
208 bfd_byte *p;
209 int vendor;
210 bfd_vma my_size;
212 p = contents;
213 *(p++) = 'A';
214 my_size = 1;
215 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
217 bfd_vma vendor_size = vendor_obj_attr_size (abfd, vendor);
218 if (vendor_size)
219 vendor_set_obj_attr_contents (abfd, p, vendor_size, vendor);
220 p += vendor_size;
221 my_size += vendor_size;
224 if (size != my_size)
225 abort ();
228 /* Allocate/find an object attribute. */
229 static obj_attribute *
230 elf_new_obj_attr (bfd *abfd, int vendor, unsigned int tag)
232 obj_attribute *attr;
233 obj_attribute_list *list;
234 obj_attribute_list *p;
235 obj_attribute_list **lastp;
238 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
240 /* Known tags are preallocated. */
241 attr = &elf_known_obj_attributes (abfd)[vendor][tag];
243 else
245 /* Create a new tag. */
246 list = (obj_attribute_list *)
247 bfd_alloc (abfd, sizeof (obj_attribute_list));
248 memset (list, 0, sizeof (obj_attribute_list));
249 list->tag = tag;
250 /* Keep the tag list in order. */
251 lastp = &elf_other_obj_attributes (abfd)[vendor];
252 for (p = *lastp; p; p = p->next)
254 if (tag < p->tag)
255 break;
256 lastp = &p->next;
258 list->next = *lastp;
259 *lastp = list;
260 attr = &list->attr;
263 return attr;
266 /* Return the value of an integer object attribute. */
268 bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, unsigned int tag)
270 obj_attribute_list *p;
272 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
274 /* Known tags are preallocated. */
275 return elf_known_obj_attributes (abfd)[vendor][tag].i;
277 else
279 for (p = elf_other_obj_attributes (abfd)[vendor];
281 p = p->next)
283 if (tag == p->tag)
284 return p->attr.i;
285 if (tag < p->tag)
286 break;
288 return 0;
292 /* Add an integer object attribute. */
293 void
294 bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, unsigned int tag, unsigned int i)
296 obj_attribute *attr;
298 attr = elf_new_obj_attr (abfd, vendor, tag);
299 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
300 attr->i = i;
303 /* Duplicate an object attribute string value. */
304 char *
305 _bfd_elf_attr_strdup (bfd *abfd, const char * s)
307 char * p;
308 int len;
310 len = strlen (s) + 1;
311 p = (char *) bfd_alloc (abfd, len);
312 return (char *) memcpy (p, s, len);
315 /* Add a string object attribute. */
316 void
317 bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag, const char *s)
319 obj_attribute *attr;
321 attr = elf_new_obj_attr (abfd, vendor, tag);
322 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
323 attr->s = _bfd_elf_attr_strdup (abfd, s);
326 /* Add a int+string object attribute. */
327 void
328 bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor,
329 unsigned int tag,
330 unsigned int i, const char *s)
332 obj_attribute *attr;
334 attr = elf_new_obj_attr (abfd, vendor, tag);
335 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
336 attr->i = i;
337 attr->s = _bfd_elf_attr_strdup (abfd, s);
340 /* Copy the object attributes from IBFD to OBFD. */
341 void
342 _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd)
344 obj_attribute *in_attr;
345 obj_attribute *out_attr;
346 obj_attribute_list *list;
347 int i;
348 int vendor;
350 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
351 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
352 return;
354 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
356 in_attr
357 = &elf_known_obj_attributes (ibfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE];
358 out_attr
359 = &elf_known_obj_attributes (obfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE];
360 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
362 out_attr->type = in_attr->type;
363 out_attr->i = in_attr->i;
364 if (in_attr->s && *in_attr->s)
365 out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s);
366 in_attr++;
367 out_attr++;
370 for (list = elf_other_obj_attributes (ibfd)[vendor];
371 list;
372 list = list->next)
374 in_attr = &list->attr;
375 switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
377 case ATTR_TYPE_FLAG_INT_VAL:
378 bfd_elf_add_obj_attr_int (obfd, vendor, list->tag, in_attr->i);
379 break;
380 case ATTR_TYPE_FLAG_STR_VAL:
381 bfd_elf_add_obj_attr_string (obfd, vendor, list->tag,
382 in_attr->s);
383 break;
384 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
385 bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag,
386 in_attr->i, in_attr->s);
387 break;
388 default:
389 abort ();
395 /* Determine whether a GNU object attribute tag takes an integer, a
396 string or both. */
397 static int
398 gnu_obj_attrs_arg_type (unsigned int tag)
400 /* Except for Tag_compatibility, for GNU attributes we follow the
401 same rule ARM ones > 32 follow: odd-numbered tags take strings
402 and even-numbered tags take integers. In addition, tag & 2 is
403 nonzero for architecture-independent tags and zero for
404 architecture-dependent ones. */
405 if (tag == Tag_compatibility)
406 return 3;
407 else
408 return (tag & 1) != 0 ? 2 : 1;
411 /* Determine what arguments an attribute tag takes. */
413 _bfd_elf_obj_attrs_arg_type (bfd *abfd, int vendor, unsigned int tag)
415 switch (vendor)
417 case OBJ_ATTR_PROC:
418 return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag);
419 break;
420 case OBJ_ATTR_GNU:
421 return gnu_obj_attrs_arg_type (tag);
422 break;
423 default:
424 abort ();
428 /* Parse an object attributes section. */
429 void
430 _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
432 bfd_byte *contents;
433 bfd_byte *p;
434 bfd_byte *p_end;
435 bfd_vma len;
436 const char *std_sec;
438 /* PR 17512: file: 2844a11d. */
439 if (hdr->sh_size == 0)
440 return;
441 contents = (bfd_byte *) bfd_malloc (hdr->sh_size + 1);
442 if (!contents)
443 return;
444 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
445 hdr->sh_size))
447 free (contents);
448 return;
450 /* Ensure that the buffer is NUL terminated. */
451 contents[hdr->sh_size] = 0;
452 p = contents;
453 p_end = p + hdr->sh_size;
454 std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor;
456 if (*(p++) == 'A')
458 len = hdr->sh_size - 1;
460 while (len > 0 && p < p_end - 4)
462 unsigned namelen;
463 bfd_vma section_len;
464 int vendor;
466 section_len = bfd_get_32 (abfd, p);
467 p += 4;
468 if (section_len == 0)
469 break;
470 if (section_len > len)
471 section_len = len;
472 len -= section_len;
473 if (section_len <= 4)
475 _bfd_error_handler (_("%B: error: attribute section length too small: %ld"),
476 abfd, section_len);
477 break;
479 section_len -= 4;
480 namelen = strnlen ((char *) p, section_len) + 1;
481 if (namelen == 0 || namelen >= section_len)
482 break;
483 section_len -= namelen;
484 if (std_sec && strcmp ((char *) p, std_sec) == 0)
485 vendor = OBJ_ATTR_PROC;
486 else if (strcmp ((char *) p, "gnu") == 0)
487 vendor = OBJ_ATTR_GNU;
488 else
490 /* Other vendor section. Ignore it. */
491 p += namelen + section_len;
492 continue;
495 p += namelen;
496 while (section_len > 0 && p < p_end)
498 unsigned int tag;
499 unsigned int n;
500 unsigned int val;
501 bfd_vma subsection_len;
502 bfd_byte *end;
504 tag = _bfd_safe_read_leb128 (abfd, p, &n, FALSE, p_end);
505 p += n;
506 if (p < p_end - 4)
507 subsection_len = bfd_get_32 (abfd, p);
508 else
509 subsection_len = 0;
510 p += 4;
511 if (subsection_len == 0)
512 break;
513 if (subsection_len > section_len)
514 subsection_len = section_len;
515 section_len -= subsection_len;
516 subsection_len -= n + 4;
517 end = p + subsection_len;
518 /* PR 17512: file: 0e8c0c90. */
519 if (end > p_end)
520 end = p_end;
521 switch (tag)
523 case Tag_File:
524 while (p < end)
526 int type;
528 tag = _bfd_safe_read_leb128 (abfd, p, &n, FALSE, end);
529 p += n;
530 type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
531 switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
533 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
534 val = _bfd_safe_read_leb128 (abfd, p, &n, FALSE, end);
535 p += n;
536 bfd_elf_add_obj_attr_int_string (abfd, vendor, tag,
537 val, (char *) p);
538 p += strlen ((char *)p) + 1;
539 break;
540 case ATTR_TYPE_FLAG_STR_VAL:
541 bfd_elf_add_obj_attr_string (abfd, vendor, tag,
542 (char *) p);
543 p += strlen ((char *)p) + 1;
544 break;
545 case ATTR_TYPE_FLAG_INT_VAL:
546 val = _bfd_safe_read_leb128 (abfd, p, &n, FALSE, end);
547 p += n;
548 bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
549 break;
550 default:
551 abort ();
554 break;
555 case Tag_Section:
556 case Tag_Symbol:
557 /* Don't have anywhere convenient to attach these.
558 Fall through for now. */
559 default:
560 /* Ignore things we don't kow about. */
561 p += subsection_len;
562 subsection_len = 0;
563 break;
568 free (contents);
571 /* Merge common object attributes from IBFD into OBFD. Raise an error
572 if there are conflicting attributes. Any processor-specific
573 attributes have already been merged. This must be called from the
574 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
575 target, along with any target-specific merging. Because there are
576 no common attributes other than Tag_compatibility at present, and
577 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
578 is not presently called for targets without their own
579 attributes. */
581 bfd_boolean
582 _bfd_elf_merge_object_attributes (bfd *ibfd, struct bfd_link_info *info)
584 bfd *obfd = info->output_bfd;
585 obj_attribute *in_attr;
586 obj_attribute *out_attr;
587 int vendor;
589 /* The only common attribute is currently Tag_compatibility,
590 accepted in both processor and "gnu" sections. */
591 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
593 /* Handle Tag_compatibility. The tags are only compatible if the flags
594 are identical and, if the flags are '1', the strings are identical.
595 If the flags are non-zero, then we can only use the string "gnu". */
596 in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility];
597 out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility];
599 if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0)
601 _bfd_error_handler
602 /* xgettext:c-format */
603 (_("error: %B: Object has vendor-specific contents that "
604 "must be processed by the '%s' toolchain"),
605 ibfd, in_attr->s);
606 return FALSE;
609 if (in_attr->i != out_attr->i
610 || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0))
612 /* xgettext:c-format */
613 _bfd_error_handler (_("error: %B: Object tag '%d, %s' is "
614 "incompatible with tag '%d, %s'"),
615 ibfd,
616 in_attr->i, in_attr->s ? in_attr->s : "",
617 out_attr->i, out_attr->s ? out_attr->s : "");
618 return FALSE;
622 return TRUE;
625 /* Merge an unknown processor-specific attribute TAG, within the range
626 of known attributes, from IBFD into OBFD; return TRUE if the link
627 is OK, FALSE if it must fail. */
629 bfd_boolean
630 _bfd_elf_merge_unknown_attribute_low (bfd *ibfd, bfd *obfd, int tag)
632 obj_attribute *in_attr;
633 obj_attribute *out_attr;
634 bfd *err_bfd = NULL;
635 bfd_boolean result = TRUE;
637 in_attr = elf_known_obj_attributes_proc (ibfd);
638 out_attr = elf_known_obj_attributes_proc (obfd);
640 if (out_attr[tag].i != 0 || out_attr[tag].s != NULL)
641 err_bfd = obfd;
642 else if (in_attr[tag].i != 0 || in_attr[tag].s != NULL)
643 err_bfd = ibfd;
645 if (err_bfd != NULL)
646 result
647 = get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag);
649 /* Only pass on attributes that match in both inputs. */
650 if (in_attr[tag].i != out_attr[tag].i
651 || (in_attr[tag].s == NULL) != (out_attr[tag].s == NULL)
652 || (in_attr[tag].s != NULL && out_attr[tag].s != NULL
653 && strcmp (in_attr[tag].s, out_attr[tag].s) != 0))
655 out_attr[tag].i = 0;
656 out_attr[tag].s = NULL;
659 return result;
662 /* Merge the lists of unknown processor-specific attributes, outside
663 the known range, from IBFD into OBFD; return TRUE if the link is
664 OK, FALSE if it must fail. */
666 bfd_boolean
667 _bfd_elf_merge_unknown_attribute_list (bfd *ibfd, bfd *obfd)
669 obj_attribute_list *in_list;
670 obj_attribute_list *out_list;
671 obj_attribute_list **out_listp;
672 bfd_boolean result = TRUE;
674 in_list = elf_other_obj_attributes_proc (ibfd);
675 out_listp = &elf_other_obj_attributes_proc (obfd);
676 out_list = *out_listp;
678 for (; in_list || out_list; )
680 bfd *err_bfd = NULL;
681 unsigned int err_tag = 0;
683 /* The tags for each list are in numerical order. */
684 /* If the tags are equal, then merge. */
685 if (out_list && (!in_list || in_list->tag > out_list->tag))
687 /* This attribute only exists in obfd. We can't merge, and we don't
688 know what the tag means, so delete it. */
689 err_bfd = obfd;
690 err_tag = out_list->tag;
691 *out_listp = out_list->next;
692 out_list = *out_listp;
694 else if (in_list && (!out_list || in_list->tag < out_list->tag))
696 /* This attribute only exists in ibfd. We can't merge, and we don't
697 know what the tag means, so ignore it. */
698 err_bfd = ibfd;
699 err_tag = in_list->tag;
700 in_list = in_list->next;
702 else /* The tags are equal. */
704 /* As present, all attributes in the list are unknown, and
705 therefore can't be merged meaningfully. */
706 err_bfd = obfd;
707 err_tag = out_list->tag;
709 /* Only pass on attributes that match in both inputs. */
710 if (in_list->attr.i != out_list->attr.i
711 || (in_list->attr.s == NULL) != (out_list->attr.s == NULL)
712 || (in_list->attr.s && out_list->attr.s
713 && strcmp (in_list->attr.s, out_list->attr.s) != 0))
715 /* No match. Delete the attribute. */
716 *out_listp = out_list->next;
717 out_list = *out_listp;
719 else
721 /* Matched. Keep the attribute and move to the next. */
722 out_list = out_list->next;
723 in_list = in_list->next;
727 if (err_bfd)
728 result = result
729 && get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd,
730 err_tag);
733 return result;