merge from gcc
[gdb/gnu.git] / bfd / elf-attrs.c
blob9a6ff6f910997ac7655950971d0dcee3f059004a
1 /* ELF attributes support (based on ARM EABI attributes).
2 Copyright 2005, 2006, 2007, 2009, 2010, 2012
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 3 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,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libiberty.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
28 /* Return the number of bytes needed by I in uleb128 format. */
29 static int
30 uleb128_size (unsigned int i)
32 int size;
33 size = 1;
34 while (i >= 0x80)
36 i >>= 7;
37 size++;
39 return size;
42 /* Return TRUE if the attribute has the default value (0/""). */
43 static bfd_boolean
44 is_default_attr (obj_attribute *attr)
46 if (ATTR_TYPE_HAS_INT_VAL (attr->type) && attr->i != 0)
47 return FALSE;
48 if (ATTR_TYPE_HAS_STR_VAL (attr->type) && attr->s && *attr->s)
49 return FALSE;
50 if (ATTR_TYPE_HAS_NO_DEFAULT (attr->type))
51 return FALSE;
53 return TRUE;
56 /* Return the size of a single attribute. */
57 static bfd_vma
58 obj_attr_size (int tag, obj_attribute *attr)
60 bfd_vma size;
62 if (is_default_attr (attr))
63 return 0;
65 size = uleb128_size (tag);
66 if (ATTR_TYPE_HAS_INT_VAL (attr->type))
67 size += uleb128_size (attr->i);
68 if (ATTR_TYPE_HAS_STR_VAL (attr->type))
69 size += strlen ((char *)attr->s) + 1;
70 return size;
73 /* Return the vendor name for a given object attributes section. */
74 static const char *
75 vendor_obj_attr_name (bfd *abfd, int vendor)
77 return (vendor == OBJ_ATTR_PROC
78 ? get_elf_backend_data (abfd)->obj_attrs_vendor
79 : "gnu");
82 /* Return the size of the object attributes section for VENDOR
83 (OBJ_ATTR_PROC or OBJ_ATTR_GNU), or 0 if there are no attributes
84 for that vendor to record and the vendor is OBJ_ATTR_GNU. */
85 static bfd_vma
86 vendor_obj_attr_size (bfd *abfd, int vendor)
88 bfd_vma size;
89 obj_attribute *attr;
90 obj_attribute_list *list;
91 int i;
92 const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
94 if (!vendor_name)
95 return 0;
97 attr = elf_known_obj_attributes (abfd)[vendor];
98 size = 0;
99 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
100 size += obj_attr_size (i, &attr[i]);
102 for (list = elf_other_obj_attributes (abfd)[vendor];
103 list;
104 list = list->next)
105 size += obj_attr_size (list->tag, &list->attr);
107 /* <size> <vendor_name> NUL 0x1 <size> */
108 return ((size || vendor == OBJ_ATTR_PROC)
109 ? size + 10 + strlen (vendor_name)
110 : 0);
113 /* Return the size of the object attributes section. */
114 bfd_vma
115 bfd_elf_obj_attr_size (bfd *abfd)
117 bfd_vma size;
119 size = vendor_obj_attr_size (abfd, OBJ_ATTR_PROC);
120 size += vendor_obj_attr_size (abfd, OBJ_ATTR_GNU);
122 /* 'A' <sections for each vendor> */
123 return (size ? size + 1 : 0);
126 /* Write VAL in uleb128 format to P, returning a pointer to the
127 following byte. */
128 static bfd_byte *
129 write_uleb128 (bfd_byte *p, unsigned int val)
131 bfd_byte c;
134 c = val & 0x7f;
135 val >>= 7;
136 if (val)
137 c |= 0x80;
138 *(p++) = c;
140 while (val);
141 return p;
144 /* Write attribute ATTR to butter P, and return a pointer to the following
145 byte. */
146 static bfd_byte *
147 write_obj_attribute (bfd_byte *p, int tag, obj_attribute *attr)
149 /* Suppress default entries. */
150 if (is_default_attr (attr))
151 return p;
153 p = write_uleb128 (p, tag);
154 if (ATTR_TYPE_HAS_INT_VAL (attr->type))
155 p = write_uleb128 (p, attr->i);
156 if (ATTR_TYPE_HAS_STR_VAL (attr->type))
158 int len;
160 len = strlen (attr->s) + 1;
161 memcpy (p, attr->s, len);
162 p += len;
165 return p;
168 /* Write the contents of the object attributes section (length SIZE)
169 for VENDOR to CONTENTS. */
170 static void
171 vendor_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size,
172 int vendor)
174 bfd_byte *p;
175 obj_attribute *attr;
176 obj_attribute_list *list;
177 int i;
178 const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
179 size_t vendor_length = strlen (vendor_name) + 1;
181 p = contents;
182 bfd_put_32 (abfd, size, p);
183 p += 4;
184 memcpy (p, vendor_name, vendor_length);
185 p += vendor_length;
186 *(p++) = Tag_File;
187 bfd_put_32 (abfd, size - 4 - vendor_length, p);
188 p += 4;
190 attr = elf_known_obj_attributes (abfd)[vendor];
191 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
193 int tag = i;
194 if (get_elf_backend_data (abfd)->obj_attrs_order)
195 tag = get_elf_backend_data (abfd)->obj_attrs_order (i);
196 p = write_obj_attribute (p, tag, &attr[tag]);
199 for (list = elf_other_obj_attributes (abfd)[vendor];
200 list;
201 list = list->next)
202 p = write_obj_attribute (p, list->tag, &list->attr);
205 /* Write the contents of the object attributes section to CONTENTS. */
206 void
207 bfd_elf_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size)
209 bfd_byte *p;
210 int vendor;
211 bfd_vma my_size;
213 p = contents;
214 *(p++) = 'A';
215 my_size = 1;
216 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
218 bfd_vma vendor_size = vendor_obj_attr_size (abfd, vendor);
219 if (vendor_size)
220 vendor_set_obj_attr_contents (abfd, p, vendor_size, vendor);
221 p += vendor_size;
222 my_size += vendor_size;
225 if (size != my_size)
226 abort ();
229 /* Allocate/find an object attribute. */
230 static obj_attribute *
231 elf_new_obj_attr (bfd *abfd, int vendor, int tag)
233 obj_attribute *attr;
234 obj_attribute_list *list;
235 obj_attribute_list *p;
236 obj_attribute_list **lastp;
239 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
241 /* Known tags are preallocated. */
242 attr = &elf_known_obj_attributes (abfd)[vendor][tag];
244 else
246 /* Create a new tag. */
247 list = (obj_attribute_list *)
248 bfd_alloc (abfd, sizeof (obj_attribute_list));
249 memset (list, 0, sizeof (obj_attribute_list));
250 list->tag = tag;
251 /* Keep the tag list in order. */
252 lastp = &elf_other_obj_attributes (abfd)[vendor];
253 for (p = *lastp; p; p = p->next)
255 if (tag < p->tag)
256 break;
257 lastp = &p->next;
259 list->next = *lastp;
260 *lastp = list;
261 attr = &list->attr;
264 return attr;
267 /* Return the value of an integer object attribute. */
269 bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, int tag)
271 obj_attribute_list *p;
273 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
275 /* Known tags are preallocated. */
276 return elf_known_obj_attributes (abfd)[vendor][tag].i;
278 else
280 for (p = elf_other_obj_attributes (abfd)[vendor];
282 p = p->next)
284 if (tag == p->tag)
285 return p->attr.i;
286 if (tag < p->tag)
287 break;
289 return 0;
293 /* Add an integer object attribute. */
294 void
295 bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, int tag, unsigned int i)
297 obj_attribute *attr;
299 attr = elf_new_obj_attr (abfd, vendor, tag);
300 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
301 attr->i = i;
304 /* Duplicate an object attribute string value. */
305 char *
306 _bfd_elf_attr_strdup (bfd *abfd, const char * s)
308 char * p;
309 int len;
311 len = strlen (s) + 1;
312 p = (char *) bfd_alloc (abfd, len);
313 return (char *) memcpy (p, s, len);
316 /* Add a string object attribute. */
317 void
318 bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, int tag, const char *s)
320 obj_attribute *attr;
322 attr = elf_new_obj_attr (abfd, vendor, tag);
323 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
324 attr->s = _bfd_elf_attr_strdup (abfd, s);
327 /* Add a int+string object attribute. */
328 void
329 bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor, 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 (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, 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_vma len;
435 const char *std_sec;
437 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
438 if (!contents)
439 return;
440 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
441 hdr->sh_size))
443 free (contents);
444 return;
446 p = contents;
447 std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor;
448 if (*(p++) == 'A')
450 len = hdr->sh_size - 1;
451 while (len > 0)
453 int namelen;
454 bfd_vma section_len;
455 int vendor;
457 section_len = bfd_get_32 (abfd, p);
458 p += 4;
459 if (section_len > len)
460 section_len = len;
461 len -= section_len;
462 namelen = strlen ((char *) p) + 1;
463 section_len -= namelen + 4;
464 if (std_sec && strcmp ((char *) p, std_sec) == 0)
465 vendor = OBJ_ATTR_PROC;
466 else if (strcmp ((char *) p, "gnu") == 0)
467 vendor = OBJ_ATTR_GNU;
468 else
470 /* Other vendor section. Ignore it. */
471 p += namelen + section_len;
472 continue;
475 p += namelen;
476 while (section_len > 0)
478 int tag;
479 unsigned int n;
480 unsigned int val;
481 bfd_vma subsection_len;
482 bfd_byte *end;
484 tag = read_unsigned_leb128 (abfd, p, &n);
485 p += n;
486 subsection_len = bfd_get_32 (abfd, p);
487 p += 4;
488 if (subsection_len > section_len)
489 subsection_len = section_len;
490 section_len -= subsection_len;
491 subsection_len -= n + 4;
492 end = p + subsection_len;
493 switch (tag)
495 case Tag_File:
496 while (p < end)
498 int type;
500 tag = read_unsigned_leb128 (abfd, p, &n);
501 p += n;
502 type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
503 switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
505 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
506 val = read_unsigned_leb128 (abfd, p, &n);
507 p += n;
508 bfd_elf_add_obj_attr_int_string (abfd, vendor, tag,
509 val, (char *)p);
510 p += strlen ((char *)p) + 1;
511 break;
512 case ATTR_TYPE_FLAG_STR_VAL:
513 bfd_elf_add_obj_attr_string (abfd, vendor, tag,
514 (char *)p);
515 p += strlen ((char *)p) + 1;
516 break;
517 case ATTR_TYPE_FLAG_INT_VAL:
518 val = read_unsigned_leb128 (abfd, p, &n);
519 p += n;
520 bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
521 break;
522 default:
523 abort ();
526 break;
527 case Tag_Section:
528 case Tag_Symbol:
529 /* Don't have anywhere convenient to attach these.
530 Fall through for now. */
531 default:
532 /* Ignore things we don't kow about. */
533 p += subsection_len;
534 subsection_len = 0;
535 break;
540 free (contents);
543 /* Merge common object attributes from IBFD into OBFD. Raise an error
544 if there are conflicting attributes. Any processor-specific
545 attributes have already been merged. This must be called from the
546 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
547 target, along with any target-specific merging. Because there are
548 no common attributes other than Tag_compatibility at present, and
549 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
550 is not presently called for targets without their own
551 attributes. */
553 bfd_boolean
554 _bfd_elf_merge_object_attributes (bfd *ibfd, bfd *obfd)
556 obj_attribute *in_attr;
557 obj_attribute *out_attr;
558 int vendor;
560 /* The only common attribute is currently Tag_compatibility,
561 accepted in both processor and "gnu" sections. */
562 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
564 /* Handle Tag_compatibility. The tags are only compatible if the flags
565 are identical and, if the flags are '1', the strings are identical.
566 If the flags are non-zero, then we can only use the string "gnu". */
567 in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility];
568 out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility];
570 if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0)
572 _bfd_error_handler
573 (_("error: %B: Object has vendor-specific contents that "
574 "must be processed by the '%s' toolchain"),
575 ibfd, in_attr->s);
576 return FALSE;
579 if (in_attr->i != out_attr->i
580 || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0))
582 _bfd_error_handler (_("error: %B: Object tag '%d, %s' is "
583 "incompatible with tag '%d, %s'"),
584 ibfd,
585 in_attr->i, in_attr->s ? in_attr->s : "",
586 out_attr->i, out_attr->s ? out_attr->s : "");
587 return FALSE;
591 return TRUE;
594 /* Merge an unknown processor-specific attribute TAG, within the range
595 of known attributes, from IBFD into OBFD; return TRUE if the link
596 is OK, FALSE if it must fail. */
598 bfd_boolean
599 _bfd_elf_merge_unknown_attribute_low (bfd *ibfd, bfd *obfd, int tag)
601 obj_attribute *in_attr;
602 obj_attribute *out_attr;
603 bfd *err_bfd = NULL;
604 bfd_boolean result = TRUE;
606 in_attr = elf_known_obj_attributes_proc (ibfd);
607 out_attr = elf_known_obj_attributes_proc (obfd);
609 if (out_attr[tag].i != 0 || out_attr[tag].s != NULL)
610 err_bfd = obfd;
611 else if (in_attr[tag].i != 0 || in_attr[tag].s != NULL)
612 err_bfd = ibfd;
614 if (err_bfd != NULL)
615 result
616 = get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag);
618 /* Only pass on attributes that match in both inputs. */
619 if (in_attr[tag].i != out_attr[tag].i
620 || (in_attr[tag].s == NULL) != (out_attr[tag].s == NULL)
621 || (in_attr[tag].s != NULL && out_attr[tag].s != NULL
622 && strcmp (in_attr[tag].s, out_attr[tag].s) != 0))
624 out_attr[tag].i = 0;
625 out_attr[tag].s = NULL;
628 return result;
631 /* Merge the lists of unknown processor-specific attributes, outside
632 the known range, from IBFD into OBFD; return TRUE if the link is
633 OK, FALSE if it must fail. */
635 bfd_boolean
636 _bfd_elf_merge_unknown_attribute_list (bfd *ibfd, bfd *obfd)
638 obj_attribute_list *in_list;
639 obj_attribute_list *out_list;
640 obj_attribute_list **out_listp;
641 bfd_boolean result = TRUE;
643 in_list = elf_other_obj_attributes_proc (ibfd);
644 out_listp = &elf_other_obj_attributes_proc (obfd);
645 out_list = *out_listp;
647 for (; in_list || out_list; )
649 bfd *err_bfd = NULL;
650 int err_tag = 0;
652 /* The tags for each list are in numerical order. */
653 /* If the tags are equal, then merge. */
654 if (out_list && (!in_list || in_list->tag > out_list->tag))
656 /* This attribute only exists in obfd. We can't merge, and we don't
657 know what the tag means, so delete it. */
658 err_bfd = obfd;
659 err_tag = out_list->tag;
660 *out_listp = out_list->next;
661 out_list = *out_listp;
663 else if (in_list && (!out_list || in_list->tag < out_list->tag))
665 /* This attribute only exists in ibfd. We can't merge, and we don't
666 know what the tag means, so ignore it. */
667 err_bfd = ibfd;
668 err_tag = in_list->tag;
669 in_list = in_list->next;
671 else /* The tags are equal. */
673 /* As present, all attributes in the list are unknown, and
674 therefore can't be merged meaningfully. */
675 err_bfd = obfd;
676 err_tag = out_list->tag;
678 /* Only pass on attributes that match in both inputs. */
679 if (in_list->attr.i != out_list->attr.i
680 || (in_list->attr.s == NULL) != (out_list->attr.s == NULL)
681 || (in_list->attr.s && out_list->attr.s
682 && strcmp (in_list->attr.s, out_list->attr.s) != 0))
684 /* No match. Delete the attribute. */
685 *out_listp = out_list->next;
686 out_list = *out_listp;
688 else
690 /* Matched. Keep the attribute and move to the next. */
691 out_list = out_list->next;
692 in_list = in_list->next;
696 if (err_bfd)
697 result = result
698 && get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd,
699 err_tag);
702 return result;