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 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. */
24 #include "libiberty.h"
28 /* Return the number of bytes needed by I in uleb128 format. */
30 uleb128_size (unsigned int i
)
42 /* Return TRUE if the attribute has the default value (0/""). */
44 is_default_attr (obj_attribute
*attr
)
46 if ((attr
->type
& 1) && attr
->i
!= 0)
48 if ((attr
->type
& 2) && attr
->s
&& *attr
->s
)
54 /* Return the size of a single attribute. */
56 obj_attr_size (int tag
, obj_attribute
*attr
)
60 if (is_default_attr (attr
))
63 size
= uleb128_size (tag
);
65 size
+= uleb128_size (attr
->i
);
67 size
+= strlen ((char *)attr
->s
) + 1;
71 /* Return the vendor name for a given object attributes section. */
73 vendor_obj_attr_name (bfd
*abfd
, int vendor
)
75 return (vendor
== OBJ_ATTR_PROC
76 ? get_elf_backend_data (abfd
)->obj_attrs_vendor
80 /* Return the size of the object attributes section for VENDOR
81 (OBJ_ATTR_PROC or OBJ_ATTR_GNU), or 0 if there are no attributes
82 for that vendor to record and the vendor is OBJ_ATTR_GNU. */
84 vendor_obj_attr_size (bfd
*abfd
, int vendor
)
88 obj_attribute_list
*list
;
90 const char *vendor_name
= vendor_obj_attr_name (abfd
, vendor
);
95 attr
= elf_known_obj_attributes (abfd
)[vendor
];
97 for (i
= 4; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
98 size
+= obj_attr_size (i
, &attr
[i
]);
100 for (list
= elf_other_obj_attributes (abfd
)[vendor
];
103 size
+= obj_attr_size (list
->tag
, &list
->attr
);
105 /* <size> <vendor_name> NUL 0x1 <size> */
106 return ((size
|| vendor
== OBJ_ATTR_PROC
)
107 ? size
+ 10 + strlen (vendor_name
)
111 /* Return the size of the object attributes section. */
113 bfd_elf_obj_attr_size (bfd
*abfd
)
117 size
= vendor_obj_attr_size (abfd
, OBJ_ATTR_PROC
);
118 size
+= vendor_obj_attr_size (abfd
, OBJ_ATTR_GNU
);
120 /* 'A' <sections for each vendor> */
121 return (size
? size
+ 1 : 0);
124 /* Write VAL in uleb128 format to P, returning a pointer to the
127 write_uleb128 (bfd_byte
*p
, unsigned int val
)
142 /* Write attribute ATTR to butter P, and return a pointer to the following
145 write_obj_attribute (bfd_byte
*p
, int tag
, obj_attribute
*attr
)
147 /* Suppress default entries. */
148 if (is_default_attr (attr
))
151 p
= write_uleb128 (p
, tag
);
153 p
= write_uleb128 (p
, attr
->i
);
158 len
= strlen (attr
->s
) + 1;
159 memcpy (p
, attr
->s
, len
);
166 /* Write the contents of the object attributes section (length SIZE)
167 for VENDOR to CONTENTS. */
169 vendor_set_obj_attr_contents (bfd
*abfd
, bfd_byte
*contents
, bfd_vma size
,
174 obj_attribute_list
*list
;
176 const char *vendor_name
= vendor_obj_attr_name (abfd
, vendor
);
177 size_t vendor_length
= strlen (vendor_name
) + 1;
180 bfd_put_32 (abfd
, size
, p
);
182 memcpy (p
, vendor_name
, vendor_length
);
185 bfd_put_32 (abfd
, size
- 4 - vendor_length
, p
);
188 attr
= elf_known_obj_attributes (abfd
)[vendor
];
189 for (i
= 4; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
190 p
= write_obj_attribute (p
, i
, &attr
[i
]);
192 for (list
= elf_other_obj_attributes (abfd
)[vendor
];
195 p
= write_obj_attribute (p
, list
->tag
, &list
->attr
);
198 /* Write the contents of the object attributes section to CONTENTS. */
200 bfd_elf_set_obj_attr_contents (bfd
*abfd
, bfd_byte
*contents
, bfd_vma size
)
209 for (vendor
= OBJ_ATTR_FIRST
; vendor
<= OBJ_ATTR_LAST
; vendor
++)
211 bfd_vma vendor_size
= vendor_obj_attr_size (abfd
, vendor
);
213 vendor_set_obj_attr_contents (abfd
, p
, vendor_size
, vendor
);
215 my_size
+= vendor_size
;
222 /* Allocate/find an object attribute. */
223 static obj_attribute
*
224 elf_new_obj_attr (bfd
*abfd
, int vendor
, int tag
)
227 obj_attribute_list
*list
;
228 obj_attribute_list
*p
;
229 obj_attribute_list
**lastp
;
232 if (tag
< NUM_KNOWN_OBJ_ATTRIBUTES
)
234 /* Knwon tags are preallocated. */
235 attr
= &elf_known_obj_attributes (abfd
)[vendor
][tag
];
239 /* Create a new tag. */
240 list
= (obj_attribute_list
*)
241 bfd_alloc (abfd
, sizeof (obj_attribute_list
));
242 memset (list
, 0, sizeof (obj_attribute_list
));
244 /* Keep the tag list in order. */
245 lastp
= &elf_other_obj_attributes (abfd
)[vendor
];
246 for (p
= *lastp
; p
; p
= p
->next
)
260 /* Return the value of an integer object attribute. */
262 bfd_elf_get_obj_attr_int (bfd
*abfd
, int vendor
, int tag
)
264 obj_attribute_list
*p
;
266 if (tag
< NUM_KNOWN_OBJ_ATTRIBUTES
)
268 /* Knwon tags are preallocated. */
269 return elf_known_obj_attributes (abfd
)[vendor
][tag
].i
;
273 for (p
= elf_other_obj_attributes (abfd
)[vendor
];
286 /* Add an integer object attribute. */
288 bfd_elf_add_obj_attr_int (bfd
*abfd
, int vendor
, int tag
, unsigned int i
)
292 attr
= elf_new_obj_attr (abfd
, vendor
, tag
);
297 /* Duplicate an object attribute string value. */
299 _bfd_elf_attr_strdup (bfd
*abfd
, const char * s
)
304 len
= strlen (s
) + 1;
305 p
= (char *) bfd_alloc (abfd
, len
);
306 return memcpy (p
, s
, len
);
309 /* Add a string object attribute. */
311 bfd_elf_add_obj_attr_string (bfd
*abfd
, int vendor
, int tag
, const char *s
)
315 attr
= elf_new_obj_attr (abfd
, vendor
, tag
);
317 attr
->s
= _bfd_elf_attr_strdup (abfd
, s
);
320 /* Add a int+string object attribute. */
322 bfd_elf_add_obj_attr_int_string (bfd
*abfd
, int vendor
, int tag
,
323 unsigned int i
, const char *s
)
327 attr
= elf_new_obj_attr (abfd
, vendor
, tag
);
330 attr
->s
= _bfd_elf_attr_strdup (abfd
, s
);
333 /* Copy the object attributes from IBFD to OBFD. */
335 _bfd_elf_copy_obj_attributes (bfd
*ibfd
, bfd
*obfd
)
337 obj_attribute
*in_attr
;
338 obj_attribute
*out_attr
;
339 obj_attribute_list
*list
;
343 for (vendor
= OBJ_ATTR_FIRST
; vendor
<= OBJ_ATTR_LAST
; vendor
++)
345 in_attr
= &elf_known_obj_attributes (ibfd
)[vendor
][4];
346 out_attr
= &elf_known_obj_attributes (obfd
)[vendor
][4];
347 for (i
= 4; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
349 out_attr
->type
= in_attr
->type
;
350 out_attr
->i
= in_attr
->i
;
351 if (in_attr
->s
&& *in_attr
->s
)
352 out_attr
->s
= _bfd_elf_attr_strdup (obfd
, in_attr
->s
);
357 for (list
= elf_other_obj_attributes (ibfd
)[vendor
];
361 in_attr
= &list
->attr
;
362 switch (in_attr
->type
)
365 bfd_elf_add_obj_attr_int (obfd
, vendor
, list
->tag
, in_attr
->i
);
368 bfd_elf_add_obj_attr_string (obfd
, vendor
, list
->tag
,
372 bfd_elf_add_obj_attr_int_string (obfd
, vendor
, list
->tag
,
373 in_attr
->i
, in_attr
->s
);
382 /* Determine whether a GNU object attribute tag takes an integer, a
385 gnu_obj_attrs_arg_type (int tag
)
387 /* Except for Tag_compatibility, for GNU attributes we follow the
388 same rule ARM ones > 32 follow: odd-numbered tags take strings
389 and even-numbered tags take integers. In addition, tag & 2 is
390 nonzero for architecture-independent tags and zero for
391 architecture-dependent ones. */
392 if (tag
== Tag_compatibility
)
395 return (tag
& 1) != 0 ? 2 : 1;
398 /* Determine what arguments an attribute tag takes. */
400 _bfd_elf_obj_attrs_arg_type (bfd
*abfd
, int vendor
, int tag
)
405 return get_elf_backend_data (abfd
)->obj_attrs_arg_type (tag
);
408 return gnu_obj_attrs_arg_type (tag
);
415 /* Parse an object attributes section. */
417 _bfd_elf_parse_attributes (bfd
*abfd
, Elf_Internal_Shdr
* hdr
)
422 const char *std_section
;
424 contents
= bfd_malloc (hdr
->sh_size
);
427 if (!bfd_get_section_contents (abfd
, hdr
->bfd_section
, contents
, 0,
434 std_section
= get_elf_backend_data (abfd
)->obj_attrs_vendor
;
437 len
= hdr
->sh_size
- 1;
444 section_len
= bfd_get_32 (abfd
, p
);
446 if (section_len
> len
)
449 namelen
= strlen ((char *)p
) + 1;
450 section_len
-= namelen
+ 4;
451 if (std_section
&& strcmp ((char *)p
, std_section
) == 0)
452 vendor
= OBJ_ATTR_PROC
;
453 else if (strcmp ((char *)p
, "gnu") == 0)
454 vendor
= OBJ_ATTR_GNU
;
457 /* Other vendor section. Ignore it. */
458 p
+= namelen
+ section_len
;
463 while (section_len
> 0)
468 bfd_vma subsection_len
;
471 tag
= read_unsigned_leb128 (abfd
, p
, &n
);
473 subsection_len
= bfd_get_32 (abfd
, p
);
475 if (subsection_len
> section_len
)
476 subsection_len
= section_len
;
477 section_len
-= subsection_len
;
478 subsection_len
-= n
+ 4;
479 end
= p
+ subsection_len
;
487 tag
= read_unsigned_leb128 (abfd
, p
, &n
);
489 type
= _bfd_elf_obj_attrs_arg_type (abfd
, vendor
, tag
);
493 val
= read_unsigned_leb128 (abfd
, p
, &n
);
495 bfd_elf_add_obj_attr_int_string (abfd
, vendor
, tag
,
497 p
+= strlen ((char *)p
) + 1;
500 bfd_elf_add_obj_attr_string (abfd
, vendor
, tag
,
502 p
+= strlen ((char *)p
) + 1;
505 val
= read_unsigned_leb128 (abfd
, p
, &n
);
507 bfd_elf_add_obj_attr_int (abfd
, vendor
, tag
, val
);
516 /* Don't have anywhere convenient to attach these.
517 Fall through for now. */
519 /* Ignore things we don't kow about. */
530 /* Merge common object attributes from IBFD into OBFD. Raise an error
531 if there are conflicting attributes. Any processor-specific
532 attributes have already been merged. This must be called from the
533 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
534 target, along with any target-specific merging. Because there are
535 no common attributes other than Tag_compatibility at present, and
536 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
537 is not presently called for targets without their own
541 _bfd_elf_merge_object_attributes (bfd
*ibfd
, bfd
*obfd
)
543 obj_attribute
*in_attr
;
544 obj_attribute
*out_attr
;
547 /* The only common attribute is currently Tag_compatibility,
548 accepted in both processor and "gnu" sections. */
549 for (vendor
= OBJ_ATTR_FIRST
; vendor
<= OBJ_ATTR_LAST
; vendor
++)
551 /* Handle Tag_compatibility. The tags are only compatible if the flags
552 are identical and, if the flags are '1', the strings are identical.
553 If the flags are non-zero, then we can only use the string "gnu". */
554 in_attr
= &elf_known_obj_attributes (ibfd
)[vendor
][Tag_compatibility
];
555 out_attr
= &elf_known_obj_attributes (obfd
)[vendor
][Tag_compatibility
];
557 if (in_attr
->i
> 0 && strcmp (in_attr
->s
, "gnu") != 0)
560 (_("ERROR: %B: Must be processed by '%s' toolchain"),
565 if (in_attr
->i
!= out_attr
->i
566 || (in_attr
->i
!= 0 && strcmp (in_attr
->s
, out_attr
->s
) != 0))
568 _bfd_error_handler (_("ERROR: %B: Object tag '%d, %s' is "
569 "incompatible with tag '%d, %s'"),
571 in_attr
->i
, in_attr
->s
? in_attr
->s
: "",
572 out_attr
->i
, out_attr
->s
? out_attr
->s
: "");
This page took 0.047225 seconds and 4 git commands to generate.