1 /* ELF attributes support (based on ARM EABI attributes).
2 Copyright (C) 2005-2022 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_ERROR (attr
->type
))
47 if (ATTR_TYPE_HAS_INT_VAL (attr
->type
) && attr
->i
!= 0)
49 if (ATTR_TYPE_HAS_STR_VAL (attr
->type
) && attr
->s
&& *attr
->s
)
51 if (ATTR_TYPE_HAS_NO_DEFAULT (attr
->type
))
57 /* Return the size of a single attribute. */
59 obj_attr_size (unsigned int tag
, obj_attribute
*attr
)
63 if (is_default_attr (attr
))
66 size
= uleb128_size (tag
);
67 if (ATTR_TYPE_HAS_INT_VAL (attr
->type
))
68 size
+= uleb128_size (attr
->i
);
69 if (ATTR_TYPE_HAS_STR_VAL (attr
->type
))
70 size
+= strlen ((char *)attr
->s
) + 1;
74 /* Return the vendor name for a given object attributes section. */
76 vendor_obj_attr_name (bfd
*abfd
, int vendor
)
78 return (vendor
== OBJ_ATTR_PROC
79 ? get_elf_backend_data (abfd
)->obj_attrs_vendor
83 /* Return the size of the object attributes section for VENDOR
84 (OBJ_ATTR_PROC or OBJ_ATTR_GNU), or 0 if there are no attributes
85 for that vendor to record and the vendor is OBJ_ATTR_GNU. */
87 vendor_obj_attr_size (bfd
*abfd
, int vendor
)
91 obj_attribute_list
*list
;
93 const char *vendor_name
= vendor_obj_attr_name (abfd
, vendor
);
98 attr
= elf_known_obj_attributes (abfd
)[vendor
];
100 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
101 size
+= obj_attr_size (i
, &attr
[i
]);
103 for (list
= elf_other_obj_attributes (abfd
)[vendor
];
106 size
+= obj_attr_size (list
->tag
, &list
->attr
);
108 /* <size> <vendor_name> NUL 0x1 <size> */
110 ? size
+ 10 + strlen (vendor_name
)
114 /* Return the size of the object attributes section. */
116 bfd_elf_obj_attr_size (bfd
*abfd
)
120 size
= vendor_obj_attr_size (abfd
, OBJ_ATTR_PROC
);
121 size
+= vendor_obj_attr_size (abfd
, OBJ_ATTR_GNU
);
123 /* 'A' <sections for each vendor> */
124 return (size
? size
+ 1 : 0);
127 /* Write VAL in uleb128 format to P, returning a pointer to the
130 write_uleb128 (bfd_byte
*p
, unsigned int val
)
145 /* Write attribute ATTR to butter P, and return a pointer to the following
148 write_obj_attribute (bfd_byte
*p
, unsigned int tag
, obj_attribute
*attr
)
150 /* Suppress default entries. */
151 if (is_default_attr (attr
))
154 p
= write_uleb128 (p
, tag
);
155 if (ATTR_TYPE_HAS_INT_VAL (attr
->type
))
156 p
= write_uleb128 (p
, attr
->i
);
157 if (ATTR_TYPE_HAS_STR_VAL (attr
->type
))
161 len
= strlen (attr
->s
) + 1;
162 memcpy (p
, attr
->s
, len
);
169 /* Write the contents of the object attributes section (length SIZE)
170 for VENDOR to CONTENTS. */
172 vendor_set_obj_attr_contents (bfd
*abfd
, bfd_byte
*contents
, bfd_vma size
,
177 obj_attribute_list
*list
;
179 const char *vendor_name
= vendor_obj_attr_name (abfd
, vendor
);
180 size_t vendor_length
= strlen (vendor_name
) + 1;
183 bfd_put_32 (abfd
, size
, p
);
185 memcpy (p
, vendor_name
, vendor_length
);
188 bfd_put_32 (abfd
, size
- 4 - vendor_length
, p
);
191 attr
= elf_known_obj_attributes (abfd
)[vendor
];
192 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
194 unsigned int tag
= i
;
195 if (get_elf_backend_data (abfd
)->obj_attrs_order
)
196 tag
= get_elf_backend_data (abfd
)->obj_attrs_order (i
);
197 p
= write_obj_attribute (p
, tag
, &attr
[tag
]);
200 for (list
= elf_other_obj_attributes (abfd
)[vendor
];
203 p
= write_obj_attribute (p
, list
->tag
, &list
->attr
);
206 /* Write the contents of the object attributes section to CONTENTS. */
208 bfd_elf_set_obj_attr_contents (bfd
*abfd
, bfd_byte
*contents
, bfd_vma size
)
217 for (vendor
= OBJ_ATTR_FIRST
; vendor
<= OBJ_ATTR_LAST
; vendor
++)
219 bfd_vma vendor_size
= vendor_obj_attr_size (abfd
, vendor
);
221 vendor_set_obj_attr_contents (abfd
, p
, vendor_size
, vendor
);
223 my_size
+= vendor_size
;
230 /* Allocate/find an object attribute. */
231 static obj_attribute
*
232 elf_new_obj_attr (bfd
*abfd
, int vendor
, unsigned int tag
)
235 obj_attribute_list
*list
;
236 obj_attribute_list
*p
;
237 obj_attribute_list
**lastp
;
240 if (tag
< NUM_KNOWN_OBJ_ATTRIBUTES
)
242 /* Known tags are preallocated. */
243 attr
= &elf_known_obj_attributes (abfd
)[vendor
][tag
];
247 /* Create a new tag. */
248 list
= (obj_attribute_list
*)
249 bfd_alloc (abfd
, sizeof (obj_attribute_list
));
250 memset (list
, 0, sizeof (obj_attribute_list
));
252 /* Keep the tag list in order. */
253 lastp
= &elf_other_obj_attributes (abfd
)[vendor
];
254 for (p
= *lastp
; p
; p
= p
->next
)
268 /* Return the value of an integer object attribute. */
270 bfd_elf_get_obj_attr_int (bfd
*abfd
, int vendor
, unsigned int tag
)
272 obj_attribute_list
*p
;
274 if (tag
< NUM_KNOWN_OBJ_ATTRIBUTES
)
276 /* Known tags are preallocated. */
277 return elf_known_obj_attributes (abfd
)[vendor
][tag
].i
;
281 for (p
= elf_other_obj_attributes (abfd
)[vendor
];
294 /* Add an integer object attribute. */
296 bfd_elf_add_obj_attr_int (bfd
*abfd
, int vendor
, unsigned int tag
, unsigned int i
)
300 attr
= elf_new_obj_attr (abfd
, vendor
, tag
);
301 attr
->type
= _bfd_elf_obj_attrs_arg_type (abfd
, vendor
, tag
);
305 /* Duplicate an object attribute string value. */
307 elf_attr_strdup (bfd
*abfd
, const char *s
, const char *end
)
313 len
= strnlen (s
, end
- s
);
317 p
= (char *) bfd_alloc (abfd
, len
+ 1);
327 _bfd_elf_attr_strdup (bfd
*abfd
, const char *s
)
329 return elf_attr_strdup (abfd
, s
, NULL
);
332 /* Add a string object attribute. */
334 elf_add_obj_attr_string (bfd
*abfd
, int vendor
, unsigned int tag
,
335 const char *s
, const char *end
)
339 attr
= elf_new_obj_attr (abfd
, vendor
, tag
);
340 attr
->type
= _bfd_elf_obj_attrs_arg_type (abfd
, vendor
, tag
);
341 attr
->s
= elf_attr_strdup (abfd
, s
, end
);
345 bfd_elf_add_obj_attr_string (bfd
*abfd
, int vendor
, unsigned int tag
,
348 elf_add_obj_attr_string (abfd
, vendor
, tag
, s
, NULL
);
351 /* Add a int+string object attribute. */
353 elf_add_obj_attr_int_string (bfd
*abfd
, int vendor
, unsigned int tag
,
354 unsigned int i
, const char *s
, const char *end
)
358 attr
= elf_new_obj_attr (abfd
, vendor
, tag
);
359 attr
->type
= _bfd_elf_obj_attrs_arg_type (abfd
, vendor
, tag
);
361 attr
->s
= elf_attr_strdup (abfd
, s
, end
);
365 bfd_elf_add_obj_attr_int_string (bfd
*abfd
, int vendor
, unsigned int tag
,
366 unsigned int i
, const char *s
)
368 elf_add_obj_attr_int_string (abfd
, vendor
, tag
, i
, s
, NULL
);
371 /* Copy the object attributes from IBFD to OBFD. */
373 _bfd_elf_copy_obj_attributes (bfd
*ibfd
, bfd
*obfd
)
375 obj_attribute
*in_attr
;
376 obj_attribute
*out_attr
;
377 obj_attribute_list
*list
;
381 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
382 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
385 for (vendor
= OBJ_ATTR_FIRST
; vendor
<= OBJ_ATTR_LAST
; vendor
++)
388 = &elf_known_obj_attributes (ibfd
)[vendor
][LEAST_KNOWN_OBJ_ATTRIBUTE
];
390 = &elf_known_obj_attributes (obfd
)[vendor
][LEAST_KNOWN_OBJ_ATTRIBUTE
];
391 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
393 out_attr
->type
= in_attr
->type
;
394 out_attr
->i
= in_attr
->i
;
395 if (in_attr
->s
&& *in_attr
->s
)
396 out_attr
->s
= _bfd_elf_attr_strdup (obfd
, in_attr
->s
);
401 for (list
= elf_other_obj_attributes (ibfd
)[vendor
];
405 in_attr
= &list
->attr
;
406 switch (in_attr
->type
& (ATTR_TYPE_FLAG_INT_VAL
| ATTR_TYPE_FLAG_STR_VAL
))
408 case ATTR_TYPE_FLAG_INT_VAL
:
409 bfd_elf_add_obj_attr_int (obfd
, vendor
, list
->tag
, in_attr
->i
);
411 case ATTR_TYPE_FLAG_STR_VAL
:
412 bfd_elf_add_obj_attr_string (obfd
, vendor
, list
->tag
,
415 case ATTR_TYPE_FLAG_INT_VAL
| ATTR_TYPE_FLAG_STR_VAL
:
416 bfd_elf_add_obj_attr_int_string (obfd
, vendor
, list
->tag
,
417 in_attr
->i
, in_attr
->s
);
426 /* Determine whether a GNU object attribute tag takes an integer, a
429 gnu_obj_attrs_arg_type (unsigned int tag
)
431 /* Except for Tag_compatibility, for GNU attributes we follow the
432 same rule ARM ones > 32 follow: odd-numbered tags take strings
433 and even-numbered tags take integers. In addition, tag & 2 is
434 nonzero for architecture-independent tags and zero for
435 architecture-dependent ones. */
436 if (tag
== Tag_compatibility
)
439 return (tag
& 1) != 0 ? 2 : 1;
442 /* Determine what arguments an attribute tag takes. */
444 _bfd_elf_obj_attrs_arg_type (bfd
*abfd
, int vendor
, unsigned int tag
)
449 return get_elf_backend_data (abfd
)->obj_attrs_arg_type (tag
);
452 return gnu_obj_attrs_arg_type (tag
);
459 /* Parse an object attributes section. */
461 _bfd_elf_parse_attributes (bfd
*abfd
, Elf_Internal_Shdr
* hdr
)
469 /* PR 17512: file: 2844a11d. */
470 if (hdr
->sh_size
== 0)
473 filesize
= bfd_get_file_size (abfd
);
474 if (filesize
!= 0 && hdr
->sh_size
> filesize
)
476 /* xgettext:c-format */
477 _bfd_error_handler (_("%pB: error: attribute section '%pA' too big: %#llx"),
478 abfd
, hdr
->bfd_section
, (long long) hdr
->sh_size
);
479 bfd_set_error (bfd_error_invalid_operation
);
483 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
486 if (!bfd_get_section_contents (abfd
, hdr
->bfd_section
, contents
, 0,
493 p_end
= p
+ hdr
->sh_size
;
494 std_sec
= get_elf_backend_data (abfd
)->obj_attrs_vendor
;
498 while (p_end
- p
>= 4)
500 size_t len
= p_end
- p
;
505 section_len
= bfd_get_32 (abfd
, p
);
507 if (section_len
== 0)
509 if (section_len
> len
)
511 if (section_len
<= 4)
514 (_("%pB: error: attribute section length too small: %ld"),
515 abfd
, (long) section_len
);
519 namelen
= strnlen ((char *) p
, section_len
) + 1;
520 if (namelen
>= section_len
)
522 if (std_sec
&& strcmp ((char *) p
, std_sec
) == 0)
523 vendor
= OBJ_ATTR_PROC
;
524 else if (strcmp ((char *) p
, "gnu") == 0)
525 vendor
= OBJ_ATTR_GNU
;
528 /* Other vendor section. Ignore it. */
534 section_len
-= namelen
;
535 while (section_len
> 0)
539 size_t subsection_len
;
540 bfd_byte
*end
, *orig_p
;
543 tag
= _bfd_safe_read_leb128 (abfd
, &p
, false, p_end
);
546 subsection_len
= bfd_get_32 (abfd
, p
);
554 if (subsection_len
> section_len
)
555 subsection_len
= section_len
;
556 section_len
-= subsection_len
;
557 end
= orig_p
+ subsection_len
;
567 tag
= _bfd_safe_read_leb128 (abfd
, &p
, false, end
);
568 type
= _bfd_elf_obj_attrs_arg_type (abfd
, vendor
, tag
);
569 switch (type
& (ATTR_TYPE_FLAG_INT_VAL
| ATTR_TYPE_FLAG_STR_VAL
))
571 case ATTR_TYPE_FLAG_INT_VAL
| ATTR_TYPE_FLAG_STR_VAL
:
572 val
= _bfd_safe_read_leb128 (abfd
, &p
, false, end
);
573 elf_add_obj_attr_int_string (abfd
, vendor
, tag
, val
,
576 p
+= strnlen ((char *) p
, end
- p
);
580 case ATTR_TYPE_FLAG_STR_VAL
:
581 elf_add_obj_attr_string (abfd
, vendor
, tag
,
584 p
+= strnlen ((char *) p
, end
- p
);
588 case ATTR_TYPE_FLAG_INT_VAL
:
589 val
= _bfd_safe_read_leb128 (abfd
, &p
, false, end
);
590 bfd_elf_add_obj_attr_int (abfd
, vendor
, tag
, val
);
599 /* Don't have anywhere convenient to attach these.
600 Fall through for now. */
602 /* Ignore things we don't know about. */
612 /* Merge common object attributes from IBFD into OBFD. Raise an error
613 if there are conflicting attributes. Any processor-specific
614 attributes have already been merged. This must be called from the
615 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
616 target, along with any target-specific merging. Because there are
617 no common attributes other than Tag_compatibility at present, and
618 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
619 is not presently called for targets without their own
623 _bfd_elf_merge_object_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
625 bfd
*obfd
= info
->output_bfd
;
626 obj_attribute
*in_attr
;
627 obj_attribute
*out_attr
;
630 /* The only common attribute is currently Tag_compatibility,
631 accepted in both processor and "gnu" sections. */
632 for (vendor
= OBJ_ATTR_FIRST
; vendor
<= OBJ_ATTR_LAST
; vendor
++)
634 /* Handle Tag_compatibility. The tags are only compatible if the flags
635 are identical and, if the flags are '1', the strings are identical.
636 If the flags are non-zero, then we can only use the string "gnu". */
637 in_attr
= &elf_known_obj_attributes (ibfd
)[vendor
][Tag_compatibility
];
638 out_attr
= &elf_known_obj_attributes (obfd
)[vendor
][Tag_compatibility
];
640 if (in_attr
->i
> 0 && strcmp (in_attr
->s
, "gnu") != 0)
643 /* xgettext:c-format */
644 (_("error: %pB: object has vendor-specific contents that "
645 "must be processed by the '%s' toolchain"),
650 if (in_attr
->i
!= out_attr
->i
651 || (in_attr
->i
!= 0 && strcmp (in_attr
->s
, out_attr
->s
) != 0))
653 /* xgettext:c-format */
654 _bfd_error_handler (_("error: %pB: object tag '%d, %s' is "
655 "incompatible with tag '%d, %s'"),
657 in_attr
->i
, in_attr
->s
? in_attr
->s
: "",
658 out_attr
->i
, out_attr
->s
? out_attr
->s
: "");
666 /* Merge an unknown processor-specific attribute TAG, within the range
667 of known attributes, from IBFD into OBFD; return TRUE if the link
668 is OK, FALSE if it must fail. */
671 _bfd_elf_merge_unknown_attribute_low (bfd
*ibfd
, bfd
*obfd
, int tag
)
673 obj_attribute
*in_attr
;
674 obj_attribute
*out_attr
;
678 in_attr
= elf_known_obj_attributes_proc (ibfd
);
679 out_attr
= elf_known_obj_attributes_proc (obfd
);
681 if (out_attr
[tag
].i
!= 0 || out_attr
[tag
].s
!= NULL
)
683 else if (in_attr
[tag
].i
!= 0 || in_attr
[tag
].s
!= NULL
)
688 = get_elf_backend_data (err_bfd
)->obj_attrs_handle_unknown (err_bfd
, tag
);
690 /* Only pass on attributes that match in both inputs. */
691 if (in_attr
[tag
].i
!= out_attr
[tag
].i
692 || (in_attr
[tag
].s
== NULL
) != (out_attr
[tag
].s
== NULL
)
693 || (in_attr
[tag
].s
!= NULL
&& out_attr
[tag
].s
!= NULL
694 && strcmp (in_attr
[tag
].s
, out_attr
[tag
].s
) != 0))
697 out_attr
[tag
].s
= NULL
;
703 /* Merge the lists of unknown processor-specific attributes, outside
704 the known range, from IBFD into OBFD; return TRUE if the link is
705 OK, FALSE if it must fail. */
708 _bfd_elf_merge_unknown_attribute_list (bfd
*ibfd
, bfd
*obfd
)
710 obj_attribute_list
*in_list
;
711 obj_attribute_list
*out_list
;
712 obj_attribute_list
**out_listp
;
715 in_list
= elf_other_obj_attributes_proc (ibfd
);
716 out_listp
= &elf_other_obj_attributes_proc (obfd
);
717 out_list
= *out_listp
;
719 for (; in_list
|| out_list
; )
722 unsigned int err_tag
= 0;
724 /* The tags for each list are in numerical order. */
725 /* If the tags are equal, then merge. */
726 if (out_list
&& (!in_list
|| in_list
->tag
> out_list
->tag
))
728 /* This attribute only exists in obfd. We can't merge, and we don't
729 know what the tag means, so delete it. */
731 err_tag
= out_list
->tag
;
732 *out_listp
= out_list
->next
;
733 out_list
= *out_listp
;
735 else if (in_list
&& (!out_list
|| in_list
->tag
< out_list
->tag
))
737 /* This attribute only exists in ibfd. We can't merge, and we don't
738 know what the tag means, so ignore it. */
740 err_tag
= in_list
->tag
;
741 in_list
= in_list
->next
;
743 else /* The tags are equal. */
745 /* As present, all attributes in the list are unknown, and
746 therefore can't be merged meaningfully. */
748 err_tag
= out_list
->tag
;
750 /* Only pass on attributes that match in both inputs. */
751 if (in_list
->attr
.i
!= out_list
->attr
.i
752 || (in_list
->attr
.s
== NULL
) != (out_list
->attr
.s
== NULL
)
753 || (in_list
->attr
.s
&& out_list
->attr
.s
754 && strcmp (in_list
->attr
.s
, out_list
->attr
.s
) != 0))
756 /* No match. Delete the attribute. */
757 *out_listp
= out_list
->next
;
758 out_list
= *out_listp
;
762 /* Matched. Keep the attribute and move to the next. */
763 out_list
= out_list
->next
;
764 in_list
= in_list
->next
;
770 && get_elf_backend_data (err_bfd
)->obj_attrs_handle_unknown (err_bfd
,