1 /* ELF attributes support (based on ARM EABI attributes).
2 Copyright 2005, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 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 & 1) && attr->i != 0)
47 if ((attr->type & 2) && attr->s && *attr->s)
53 /* Return the size of a single attribute. */
55 obj_attr_size (int tag, obj_attribute *attr)
59 if (is_default_attr (attr))
62 size = uleb128_size (tag);
64 size += uleb128_size (attr->i);
66 size += strlen ((char *)attr->s) + 1;
70 /* Return the vendor name for a given object attributes section. */
72 vendor_obj_attr_name (bfd *abfd, int vendor)
74 return (vendor == OBJ_ATTR_PROC
75 ? get_elf_backend_data (abfd)->obj_attrs_vendor
79 /* Return the size of the object attributes section for VENDOR
80 (OBJ_ATTR_PROC or OBJ_ATTR_GNU), or 0 if there are no attributes
81 for that vendor to record and the vendor is OBJ_ATTR_GNU. */
83 vendor_obj_attr_size (bfd *abfd, int vendor)
87 obj_attribute_list *list;
89 const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
94 attr = elf_known_obj_attributes (abfd)[vendor];
96 for (i = 4; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
97 size += obj_attr_size (i, &attr[i]);
99 for (list = elf_other_obj_attributes (abfd)[vendor];
102 size += obj_attr_size (list->tag, &list->attr);
104 /* <size> <vendor_name> NUL 0x1 <size> */
105 return ((size || vendor == OBJ_ATTR_PROC)
106 ? size + 10 + strlen (vendor_name)
110 /* Return the size of the object attributes section. */
112 bfd_elf_obj_attr_size (bfd *abfd)
116 size = vendor_obj_attr_size (abfd, OBJ_ATTR_PROC);
117 size += vendor_obj_attr_size (abfd, OBJ_ATTR_GNU);
119 /* 'A' <sections for each vendor> */
120 return (size ? size + 1 : 0);
123 /* Write VAL in uleb128 format to P, returning a pointer to the
126 write_uleb128 (bfd_byte *p, unsigned int val)
141 /* Write attribute ATTR to butter P, and return a pointer to the following
144 write_obj_attribute (bfd_byte *p, int tag, obj_attribute *attr)
146 /* Suppress default entries. */
147 if (is_default_attr (attr))
150 p = write_uleb128 (p, tag);
152 p = write_uleb128 (p, attr->i);
157 len = strlen (attr->s) + 1;
158 memcpy (p, attr->s, len);
165 /* Write the contents of the object attributes section (length SIZE)
166 for VENDOR to CONTENTS. */
168 vendor_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size,
173 obj_attribute_list *list;
175 const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
176 size_t vendor_length = strlen (vendor_name) + 1;
179 bfd_put_32 (abfd, size, p);
181 memcpy (p, vendor_name, vendor_length);
184 bfd_put_32 (abfd, size - 4 - vendor_length, p);
187 attr = elf_known_obj_attributes (abfd)[vendor];
188 for (i = 4; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
189 p = write_obj_attribute (p, i, &attr[i]);
191 for (list = elf_other_obj_attributes (abfd)[vendor];
194 p = write_obj_attribute (p, list->tag, &list->attr);
197 /* Write the contents of the object attributes section to CONTENTS. */
199 bfd_elf_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size)
208 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
210 bfd_vma vendor_size = vendor_obj_attr_size (abfd, vendor);
212 vendor_set_obj_attr_contents (abfd, p, vendor_size, vendor);
214 my_size += vendor_size;
221 /* Allocate/find an object attribute. */
222 static obj_attribute *
223 elf_new_obj_attr (bfd *abfd, int vendor, int tag)
226 obj_attribute_list *list;
227 obj_attribute_list *p;
228 obj_attribute_list **lastp;
231 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
233 /* Knwon tags are preallocated. */
234 attr = &elf_known_obj_attributes (abfd)[vendor][tag];
238 /* Create a new tag. */
239 list = (obj_attribute_list *)
240 bfd_alloc (abfd, sizeof (obj_attribute_list));
241 memset (list, 0, sizeof (obj_attribute_list));
243 /* Keep the tag list in order. */
244 lastp = &elf_other_obj_attributes (abfd)[vendor];
245 for (p = *lastp; p; p = p->next)
259 /* Return the value of an integer object attribute. */
261 bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, int tag)
263 obj_attribute_list *p;
265 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
267 /* Knwon tags are preallocated. */
268 return elf_known_obj_attributes (abfd)[vendor][tag].i;
272 for (p = elf_other_obj_attributes (abfd)[vendor];
285 /* Add an integer object attribute. */
287 bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, int tag, unsigned int i)
291 attr = elf_new_obj_attr (abfd, vendor, tag);
296 /* Duplicate an object attribute string value. */
298 _bfd_elf_attr_strdup (bfd *abfd, const char * s)
303 len = strlen (s) + 1;
304 p = (char *) bfd_alloc (abfd, len);
305 return memcpy (p, s, len);
308 /* Add a string object attribute. */
310 bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, int tag, const char *s)
314 attr = elf_new_obj_attr (abfd, vendor, tag);
316 attr->s = _bfd_elf_attr_strdup (abfd, s);
319 /* Add a Tag_compatibility object attribute. */
321 bfd_elf_add_obj_attr_compat (bfd *abfd, int vendor, unsigned int i,
324 obj_attribute_list *list;
325 obj_attribute_list *p;
326 obj_attribute_list **lastp;
328 list = (obj_attribute_list *)
329 bfd_alloc (abfd, sizeof (obj_attribute_list));
330 memset (list, 0, sizeof (obj_attribute_list));
331 list->tag = Tag_compatibility;
334 list->attr.s = _bfd_elf_attr_strdup (abfd, s);
336 lastp = &elf_other_obj_attributes (abfd)[vendor];
337 for (p = *lastp; p; p = p->next)
340 if (p->tag != Tag_compatibility)
342 cmp = strcmp(s, p->attr.s);
343 if (cmp < 0 || (cmp == 0 && i < p->attr.i))
351 /* Copy the object attributes from IBFD to OBFD. */
353 _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd)
355 obj_attribute *in_attr;
356 obj_attribute *out_attr;
357 obj_attribute_list *list;
361 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
363 in_attr = &elf_known_obj_attributes (ibfd)[vendor][4];
364 out_attr = &elf_known_obj_attributes (obfd)[vendor][4];
365 for (i = 4; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
367 out_attr->type = in_attr->type;
368 out_attr->i = in_attr->i;
369 if (in_attr->s && *in_attr->s)
370 out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s);
375 for (list = elf_other_obj_attributes (ibfd)[vendor];
379 in_attr = &list->attr;
380 switch (in_attr->type)
383 bfd_elf_add_obj_attr_int (obfd, vendor, list->tag, in_attr->i);
386 bfd_elf_add_obj_attr_string (obfd, vendor, list->tag,
390 bfd_elf_add_obj_attr_compat (obfd, vendor, in_attr->i,
400 /* Determine whether a GNU object attribute tag takes an integer, a
403 gnu_obj_attrs_arg_type (int tag)
405 /* Except for Tag_compatibility, for GNU attributes we follow the
406 same rule ARM ones > 32 follow: odd-numbered tags take strings
407 and even-numbered tags take integers. In addition, tag & 2 is
408 nonzero for architecture-independent tags and zero for
409 architecture-dependent ones. */
410 if (tag == Tag_compatibility)
413 return (tag & 1) != 0 ? 2 : 1;
416 /* Determine what arguments an attribute tag takes. */
418 _bfd_elf_obj_attrs_arg_type (bfd *abfd, int vendor, int tag)
423 return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag);
426 return gnu_obj_attrs_arg_type (tag);
433 /* Parse an object attributes section. */
435 _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
440 const char *std_section;
442 contents = bfd_malloc (hdr->sh_size);
445 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
452 std_section = get_elf_backend_data (abfd)->obj_attrs_vendor;
455 len = hdr->sh_size - 1;
462 section_len = bfd_get_32 (abfd, p);
464 if (section_len > len)
467 namelen = strlen ((char *)p) + 1;
468 section_len -= namelen + 4;
469 if (std_section && strcmp ((char *)p, std_section) == 0)
470 vendor = OBJ_ATTR_PROC;
471 else if (strcmp ((char *)p, "gnu") == 0)
472 vendor = OBJ_ATTR_GNU;
475 /* Other vendor section. Ignore it. */
476 p += namelen + section_len;
481 while (section_len > 0)
486 bfd_vma subsection_len;
489 tag = read_unsigned_leb128 (abfd, p, &n);
491 subsection_len = bfd_get_32 (abfd, p);
493 if (subsection_len > section_len)
494 subsection_len = section_len;
495 section_len -= subsection_len;
496 subsection_len -= n + 4;
497 end = p + subsection_len;
505 tag = read_unsigned_leb128 (abfd, p, &n);
507 type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
511 val = read_unsigned_leb128 (abfd, p, &n);
513 bfd_elf_add_obj_attr_compat (abfd, vendor, val,
515 p += strlen ((char *)p) + 1;
518 bfd_elf_add_obj_attr_string (abfd, vendor, tag,
520 p += strlen ((char *)p) + 1;
523 val = read_unsigned_leb128 (abfd, p, &n);
525 bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
534 /* Don't have anywhere convenient to attach these.
535 Fall through for now. */
537 /* Ignore things we don't kow about. */
548 /* Merge common object attributes from IBFD into OBFD. Raise an error
549 if there are conflicting attributes. Any processor-specific
550 attributes have already been merged. This must be called from the
551 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
552 target, along with any target-specific merging. Because there are
553 no common attributes other than Tag_compatibility at present, and
554 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
555 is not presently called for targets without their own
559 _bfd_elf_merge_object_attributes (bfd *ibfd, bfd *obfd)
561 obj_attribute *in_attr;
562 obj_attribute *out_attr;
563 obj_attribute_list *in_list;
564 obj_attribute_list *out_list;
567 /* The only common attribute is currently Tag_compatibility,
568 accepted in both processor and "gnu" sections. */
569 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
571 in_list = elf_other_obj_attributes (ibfd)[vendor];
572 out_list = elf_other_obj_attributes (ibfd)[vendor];
573 while (in_list && in_list->tag == Tag_compatibility)
575 in_attr = &in_list->attr;
578 if (in_attr->i == 1 && strcmp (in_attr->s, "gnu") != 0)
581 (_("ERROR: %B: Must be processed by '%s' toolchain"),
585 if (!out_list || out_list->tag != Tag_compatibility
586 || strcmp (in_attr->s, out_list->attr.s) != 0)
588 /* Add this compatibility tag to the output. */
589 bfd_elf_add_proc_attr_compat (obfd, in_attr->i, in_attr->s);
592 out_attr = &out_list->attr;
593 /* Check all the input tags with the same identifier. */
596 if (out_list->tag != Tag_compatibility
597 || in_attr->i != out_attr->i
598 || strcmp (in_attr->s, out_attr->s) != 0)
601 (_("ERROR: %B: Incompatible object tag '%s':%d"),
602 ibfd, in_attr->s, in_attr->i);
605 in_list = in_list->next;
606 if (in_list->tag != Tag_compatibility
607 || strcmp (in_attr->s, in_list->attr.s) != 0)
609 in_attr = &in_list->attr;
610 out_list = out_list->next;
612 out_attr = &out_list->attr;
615 /* Check the output doesn't have extra tags with this identifier. */
616 if (out_list && out_list->tag == Tag_compatibility
617 && strcmp (in_attr->s, out_list->attr.s) == 0)
620 (_("ERROR: %B: Incompatible object tag '%s':%d"),
621 ibfd, in_attr->s, out_list->attr.i);