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. */
23 #include "libiberty.h"
27 /* Return the number of bytes needed by I in uleb128 format. */
29 uleb128_size (unsigned int i)
41 /* Return TRUE if the attribute has the default value (0/""). */
43 is_default_attr (obj_attribute *attr)
45 if (ATTR_TYPE_HAS_INT_VAL (attr->type) && attr->i != 0)
47 if (ATTR_TYPE_HAS_STR_VAL (attr->type) && attr->s && *attr->s)
49 if (ATTR_TYPE_HAS_NO_DEFAULT (attr->type))
55 /* Return the size of a single attribute. */
57 obj_attr_size (unsigned int tag, obj_attribute *attr)
61 if (is_default_attr (attr))
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;
72 /* Return the vendor name for a given object attributes section. */
74 vendor_obj_attr_name (bfd *abfd, int vendor)
76 return (vendor == OBJ_ATTR_PROC
77 ? get_elf_backend_data (abfd)->obj_attrs_vendor
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. */
85 vendor_obj_attr_size (bfd *abfd, int vendor)
89 obj_attribute_list *list;
91 const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
96 attr = elf_known_obj_attributes (abfd)[vendor];
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];
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)
112 /* Return the size of the object attributes section. */
114 bfd_elf_obj_attr_size (bfd *abfd)
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
128 write_uleb128 (bfd_byte *p, unsigned int val)
143 /* Write attribute ATTR to butter P, and return a pointer to the following
146 write_obj_attribute (bfd_byte *p, unsigned int tag, obj_attribute *attr)
148 /* Suppress default entries. */
149 if (is_default_attr (attr))
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))
159 len = strlen (attr->s) + 1;
160 memcpy (p, attr->s, len);
167 /* Write the contents of the object attributes section (length SIZE)
168 for VENDOR to CONTENTS. */
170 vendor_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size,
175 obj_attribute_list *list;
177 const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
178 size_t vendor_length = strlen (vendor_name) + 1;
181 bfd_put_32 (abfd, size, p);
183 memcpy (p, vendor_name, vendor_length);
186 bfd_put_32 (abfd, size - 4 - vendor_length, p);
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];
201 p = write_obj_attribute (p, list->tag, &list->attr);
204 /* Write the contents of the object attributes section to CONTENTS. */
206 bfd_elf_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size)
215 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
217 bfd_vma vendor_size = vendor_obj_attr_size (abfd, vendor);
219 vendor_set_obj_attr_contents (abfd, p, vendor_size, vendor);
221 my_size += vendor_size;
228 /* Allocate/find an object attribute. */
229 static obj_attribute *
230 elf_new_obj_attr (bfd *abfd, int vendor, unsigned int tag)
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];
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));
250 /* Keep the tag list in order. */
251 lastp = &elf_other_obj_attributes (abfd)[vendor];
252 for (p = *lastp; p; p = p->next)
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;
279 for (p = elf_other_obj_attributes (abfd)[vendor];
292 /* Add an integer object attribute. */
294 bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, unsigned int tag, unsigned int i)
298 attr = elf_new_obj_attr (abfd, vendor, tag);
299 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
303 /* Duplicate an object attribute string value. */
305 _bfd_elf_attr_strdup (bfd *abfd, const char * s)
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. */
317 bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag, const char *s)
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. */
328 bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor,
330 unsigned int i, const char *s)
334 attr = elf_new_obj_attr (abfd, vendor, tag);
335 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
337 attr->s = _bfd_elf_attr_strdup (abfd, s);
340 /* Copy the object attributes from IBFD to OBFD. */
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;
350 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
351 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
354 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
357 = &elf_known_obj_attributes (ibfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE];
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);
370 for (list = elf_other_obj_attributes (ibfd)[vendor];
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);
380 case ATTR_TYPE_FLAG_STR_VAL:
381 bfd_elf_add_obj_attr_string (obfd, vendor, list->tag,
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);
395 /* Determine whether a GNU object attribute tag takes an integer, a
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)
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)
418 return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag);
421 return gnu_obj_attrs_arg_type (tag);
428 /* Parse an object attributes section. */
430 _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
438 /* PR 17512: file: 2844a11d. */
439 if (hdr->sh_size == 0)
441 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
444 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
451 p_end = p + hdr->sh_size;
452 std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor;
456 len = hdr->sh_size - 1;
458 while (len > 0 && p < p_end - 4)
464 section_len = bfd_get_32 (abfd, p);
466 if (section_len == 0)
468 if (section_len > len)
472 namelen = strnlen ((char *) p, section_len) + 1;
473 if (namelen == 0 || namelen >= section_len)
475 section_len -= namelen;
476 if (std_sec && strcmp ((char *) p, std_sec) == 0)
477 vendor = OBJ_ATTR_PROC;
478 else if (strcmp ((char *) p, "gnu") == 0)
479 vendor = OBJ_ATTR_GNU;
482 /* Other vendor section. Ignore it. */
483 p += namelen + section_len;
488 while (section_len > 0 && p < p_end)
493 bfd_vma subsection_len;
496 tag = safe_read_leb128 (abfd, p, &n, FALSE, p_end);
499 subsection_len = bfd_get_32 (abfd, p);
503 if (subsection_len == 0)
505 if (subsection_len > section_len)
506 subsection_len = section_len;
507 section_len -= subsection_len;
508 subsection_len -= n + 4;
509 end = p + subsection_len;
510 /* PR 17512: file: 0e8c0c90. */
520 tag = safe_read_leb128 (abfd, p, &n, FALSE, end);
522 type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
523 switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
525 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
526 val = safe_read_leb128 (abfd, p, &n, FALSE, end);
528 bfd_elf_add_obj_attr_int_string (abfd, vendor, tag,
530 p += strlen ((char *)p) + 1;
532 case ATTR_TYPE_FLAG_STR_VAL:
533 bfd_elf_add_obj_attr_string (abfd, vendor, tag,
535 p += strlen ((char *)p) + 1;
537 case ATTR_TYPE_FLAG_INT_VAL:
538 val = safe_read_leb128 (abfd, p, &n, FALSE, end);
540 bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
549 /* Don't have anywhere convenient to attach these.
550 Fall through for now. */
552 /* Ignore things we don't kow about. */
563 /* Merge common object attributes from IBFD into OBFD. Raise an error
564 if there are conflicting attributes. Any processor-specific
565 attributes have already been merged. This must be called from the
566 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
567 target, along with any target-specific merging. Because there are
568 no common attributes other than Tag_compatibility at present, and
569 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
570 is not presently called for targets without their own
574 _bfd_elf_merge_object_attributes (bfd *ibfd, struct bfd_link_info *info)
576 bfd *obfd = info->output_bfd;
577 obj_attribute *in_attr;
578 obj_attribute *out_attr;
581 /* The only common attribute is currently Tag_compatibility,
582 accepted in both processor and "gnu" sections. */
583 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
585 /* Handle Tag_compatibility. The tags are only compatible if the flags
586 are identical and, if the flags are '1', the strings are identical.
587 If the flags are non-zero, then we can only use the string "gnu". */
588 in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility];
589 out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility];
591 if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0)
594 /* xgettext:c-format */
595 (_("error: %B: Object has vendor-specific contents that "
596 "must be processed by the '%s' toolchain"),
601 if (in_attr->i != out_attr->i
602 || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0))
604 /* xgettext:c-format */
605 _bfd_error_handler (_("error: %B: Object tag '%d, %s' is "
606 "incompatible with tag '%d, %s'"),
608 in_attr->i, in_attr->s ? in_attr->s : "",
609 out_attr->i, out_attr->s ? out_attr->s : "");
617 /* Merge an unknown processor-specific attribute TAG, within the range
618 of known attributes, from IBFD into OBFD; return TRUE if the link
619 is OK, FALSE if it must fail. */
622 _bfd_elf_merge_unknown_attribute_low (bfd *ibfd, bfd *obfd, int tag)
624 obj_attribute *in_attr;
625 obj_attribute *out_attr;
627 bfd_boolean result = TRUE;
629 in_attr = elf_known_obj_attributes_proc (ibfd);
630 out_attr = elf_known_obj_attributes_proc (obfd);
632 if (out_attr[tag].i != 0 || out_attr[tag].s != NULL)
634 else if (in_attr[tag].i != 0 || in_attr[tag].s != NULL)
639 = get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag);
641 /* Only pass on attributes that match in both inputs. */
642 if (in_attr[tag].i != out_attr[tag].i
643 || (in_attr[tag].s == NULL) != (out_attr[tag].s == NULL)
644 || (in_attr[tag].s != NULL && out_attr[tag].s != NULL
645 && strcmp (in_attr[tag].s, out_attr[tag].s) != 0))
648 out_attr[tag].s = NULL;
654 /* Merge the lists of unknown processor-specific attributes, outside
655 the known range, from IBFD into OBFD; return TRUE if the link is
656 OK, FALSE if it must fail. */
659 _bfd_elf_merge_unknown_attribute_list (bfd *ibfd, bfd *obfd)
661 obj_attribute_list *in_list;
662 obj_attribute_list *out_list;
663 obj_attribute_list **out_listp;
664 bfd_boolean result = TRUE;
666 in_list = elf_other_obj_attributes_proc (ibfd);
667 out_listp = &elf_other_obj_attributes_proc (obfd);
668 out_list = *out_listp;
670 for (; in_list || out_list; )
673 unsigned int err_tag = 0;
675 /* The tags for each list are in numerical order. */
676 /* If the tags are equal, then merge. */
677 if (out_list && (!in_list || in_list->tag > out_list->tag))
679 /* This attribute only exists in obfd. We can't merge, and we don't
680 know what the tag means, so delete it. */
682 err_tag = out_list->tag;
683 *out_listp = out_list->next;
684 out_list = *out_listp;
686 else if (in_list && (!out_list || in_list->tag < out_list->tag))
688 /* This attribute only exists in ibfd. We can't merge, and we don't
689 know what the tag means, so ignore it. */
691 err_tag = in_list->tag;
692 in_list = in_list->next;
694 else /* The tags are equal. */
696 /* As present, all attributes in the list are unknown, and
697 therefore can't be merged meaningfully. */
699 err_tag = out_list->tag;
701 /* Only pass on attributes that match in both inputs. */
702 if (in_list->attr.i != out_list->attr.i
703 || (in_list->attr.s == NULL) != (out_list->attr.s == NULL)
704 || (in_list->attr.s && out_list->attr.s
705 && strcmp (in_list->attr.s, out_list->attr.s) != 0))
707 /* No match. Delete the attribute. */
708 *out_listp = out_list->next;
709 out_list = *out_listp;
713 /* Matched. Keep the attribute and move to the next. */
714 out_list = out_list->next;
715 in_list = in_list->next;
721 && get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd,