2009-01-19 Andrew Stubbs <ams@codesourcery.com>
[deliverable/binutils-gdb.git] / bfd / elf-attrs.c
1 /* ELF attributes support (based on ARM EABI attributes).
2 Copyright 2005, 2006, 2007
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libiberty.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27
28 /* Return the number of bytes needed by I in uleb128 format. */
29 static int
30 uleb128_size (unsigned int i)
31 {
32 int size;
33 size = 1;
34 while (i >= 0x80)
35 {
36 i >>= 7;
37 size++;
38 }
39 return size;
40 }
41
42 /* Return TRUE if the attribute has the default value (0/""). */
43 static bfd_boolean
44 is_default_attr (obj_attribute *attr)
45 {
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;
52
53 return TRUE;
54 }
55
56 /* Return the size of a single attribute. */
57 static bfd_vma
58 obj_attr_size (int tag, obj_attribute *attr)
59 {
60 bfd_vma size;
61
62 if (is_default_attr (attr))
63 return 0;
64
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;
71 }
72
73 /* Return the vendor name for a given object attributes section. */
74 static const char *
75 vendor_obj_attr_name (bfd *abfd, int vendor)
76 {
77 return (vendor == OBJ_ATTR_PROC
78 ? get_elf_backend_data (abfd)->obj_attrs_vendor
79 : "gnu");
80 }
81
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)
87 {
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);
93
94 if (!vendor_name)
95 return 0;
96
97 attr = elf_known_obj_attributes (abfd)[vendor];
98 size = 0;
99 for (i = 4; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
100 size += obj_attr_size (i, &attr[i]);
101
102 for (list = elf_other_obj_attributes (abfd)[vendor];
103 list;
104 list = list->next)
105 size += obj_attr_size (list->tag, &list->attr);
106
107 /* <size> <vendor_name> NUL 0x1 <size> */
108 return ((size || vendor == OBJ_ATTR_PROC)
109 ? size + 10 + strlen (vendor_name)
110 : 0);
111 }
112
113 /* Return the size of the object attributes section. */
114 bfd_vma
115 bfd_elf_obj_attr_size (bfd *abfd)
116 {
117 bfd_vma size;
118
119 size = vendor_obj_attr_size (abfd, OBJ_ATTR_PROC);
120 size += vendor_obj_attr_size (abfd, OBJ_ATTR_GNU);
121
122 /* 'A' <sections for each vendor> */
123 return (size ? size + 1 : 0);
124 }
125
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)
130 {
131 bfd_byte c;
132 do
133 {
134 c = val & 0x7f;
135 val >>= 7;
136 if (val)
137 c |= 0x80;
138 *(p++) = c;
139 }
140 while (val);
141 return p;
142 }
143
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)
148 {
149 /* Suppress default entries. */
150 if (is_default_attr (attr))
151 return p;
152
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))
157 {
158 int len;
159
160 len = strlen (attr->s) + 1;
161 memcpy (p, attr->s, len);
162 p += len;
163 }
164
165 return p;
166 }
167
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)
173 {
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;
180
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;
189
190 attr = elf_known_obj_attributes (abfd)[vendor];
191 for (i = 4; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
192 p = write_obj_attribute (p, i, &attr[i]);
193
194 for (list = elf_other_obj_attributes (abfd)[vendor];
195 list;
196 list = list->next)
197 p = write_obj_attribute (p, list->tag, &list->attr);
198 }
199
200 /* Write the contents of the object attributes section to CONTENTS. */
201 void
202 bfd_elf_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size)
203 {
204 bfd_byte *p;
205 int vendor;
206 bfd_vma my_size;
207
208 p = contents;
209 *(p++) = 'A';
210 my_size = 1;
211 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
212 {
213 bfd_vma vendor_size = vendor_obj_attr_size (abfd, vendor);
214 if (vendor_size)
215 vendor_set_obj_attr_contents (abfd, p, vendor_size, vendor);
216 p += vendor_size;
217 my_size += vendor_size;
218 }
219
220 if (size != my_size)
221 abort ();
222 }
223
224 /* Allocate/find an object attribute. */
225 static obj_attribute *
226 elf_new_obj_attr (bfd *abfd, int vendor, int tag)
227 {
228 obj_attribute *attr;
229 obj_attribute_list *list;
230 obj_attribute_list *p;
231 obj_attribute_list **lastp;
232
233
234 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
235 {
236 /* Knwon tags are preallocated. */
237 attr = &elf_known_obj_attributes (abfd)[vendor][tag];
238 }
239 else
240 {
241 /* Create a new tag. */
242 list = (obj_attribute_list *)
243 bfd_alloc (abfd, sizeof (obj_attribute_list));
244 memset (list, 0, sizeof (obj_attribute_list));
245 list->tag = tag;
246 /* Keep the tag list in order. */
247 lastp = &elf_other_obj_attributes (abfd)[vendor];
248 for (p = *lastp; p; p = p->next)
249 {
250 if (tag < p->tag)
251 break;
252 lastp = &p->next;
253 }
254 list->next = *lastp;
255 *lastp = list;
256 attr = &list->attr;
257 }
258
259 return attr;
260 }
261
262 /* Return the value of an integer object attribute. */
263 int
264 bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, int tag)
265 {
266 obj_attribute_list *p;
267
268 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
269 {
270 /* Knwon tags are preallocated. */
271 return elf_known_obj_attributes (abfd)[vendor][tag].i;
272 }
273 else
274 {
275 for (p = elf_other_obj_attributes (abfd)[vendor];
276 p;
277 p = p->next)
278 {
279 if (tag == p->tag)
280 return p->attr.i;
281 if (tag < p->tag)
282 break;
283 }
284 return 0;
285 }
286 }
287
288 /* Add an integer object attribute. */
289 void
290 bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, int tag, unsigned int i)
291 {
292 obj_attribute *attr;
293
294 attr = elf_new_obj_attr (abfd, vendor, tag);
295 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
296 attr->i = i;
297 }
298
299 /* Duplicate an object attribute string value. */
300 char *
301 _bfd_elf_attr_strdup (bfd *abfd, const char * s)
302 {
303 char * p;
304 int len;
305
306 len = strlen (s) + 1;
307 p = (char *) bfd_alloc (abfd, len);
308 return memcpy (p, s, len);
309 }
310
311 /* Add a string object attribute. */
312 void
313 bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, int tag, const char *s)
314 {
315 obj_attribute *attr;
316
317 attr = elf_new_obj_attr (abfd, vendor, tag);
318 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
319 attr->s = _bfd_elf_attr_strdup (abfd, s);
320 }
321
322 /* Add a int+string object attribute. */
323 void
324 bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor, int tag,
325 unsigned int i, const char *s)
326 {
327 obj_attribute *attr;
328
329 attr = elf_new_obj_attr (abfd, vendor, tag);
330 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
331 attr->i = i;
332 attr->s = _bfd_elf_attr_strdup (abfd, s);
333 }
334
335 /* Copy the object attributes from IBFD to OBFD. */
336 void
337 _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd)
338 {
339 obj_attribute *in_attr;
340 obj_attribute *out_attr;
341 obj_attribute_list *list;
342 int i;
343 int vendor;
344
345 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
346 {
347 in_attr = &elf_known_obj_attributes (ibfd)[vendor][4];
348 out_attr = &elf_known_obj_attributes (obfd)[vendor][4];
349 for (i = 4; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
350 {
351 out_attr->type = in_attr->type;
352 out_attr->i = in_attr->i;
353 if (in_attr->s && *in_attr->s)
354 out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s);
355 in_attr++;
356 out_attr++;
357 }
358
359 for (list = elf_other_obj_attributes (ibfd)[vendor];
360 list;
361 list = list->next)
362 {
363 in_attr = &list->attr;
364 switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
365 {
366 case ATTR_TYPE_FLAG_INT_VAL:
367 bfd_elf_add_obj_attr_int (obfd, vendor, list->tag, in_attr->i);
368 break;
369 case ATTR_TYPE_FLAG_STR_VAL:
370 bfd_elf_add_obj_attr_string (obfd, vendor, list->tag,
371 in_attr->s);
372 break;
373 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
374 bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag,
375 in_attr->i, in_attr->s);
376 break;
377 default:
378 abort ();
379 }
380 }
381 }
382 }
383
384 /* Determine whether a GNU object attribute tag takes an integer, a
385 string or both. */
386 static int
387 gnu_obj_attrs_arg_type (int tag)
388 {
389 /* Except for Tag_compatibility, for GNU attributes we follow the
390 same rule ARM ones > 32 follow: odd-numbered tags take strings
391 and even-numbered tags take integers. In addition, tag & 2 is
392 nonzero for architecture-independent tags and zero for
393 architecture-dependent ones. */
394 if (tag == Tag_compatibility)
395 return 3;
396 else
397 return (tag & 1) != 0 ? 2 : 1;
398 }
399
400 /* Determine what arguments an attribute tag takes. */
401 int
402 _bfd_elf_obj_attrs_arg_type (bfd *abfd, int vendor, int tag)
403 {
404 switch (vendor)
405 {
406 case OBJ_ATTR_PROC:
407 return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag);
408 break;
409 case OBJ_ATTR_GNU:
410 return gnu_obj_attrs_arg_type (tag);
411 break;
412 default:
413 abort ();
414 }
415 }
416
417 /* Parse an object attributes section. */
418 void
419 _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
420 {
421 bfd_byte *contents;
422 bfd_byte *p;
423 bfd_vma len;
424 const char *std_section;
425
426 contents = bfd_malloc (hdr->sh_size);
427 if (!contents)
428 return;
429 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
430 hdr->sh_size))
431 {
432 free (contents);
433 return;
434 }
435 p = contents;
436 std_section = get_elf_backend_data (abfd)->obj_attrs_vendor;
437 if (*(p++) == 'A')
438 {
439 len = hdr->sh_size - 1;
440 while (len > 0)
441 {
442 int namelen;
443 bfd_vma section_len;
444 int vendor;
445
446 section_len = bfd_get_32 (abfd, p);
447 p += 4;
448 if (section_len > len)
449 section_len = len;
450 len -= section_len;
451 namelen = strlen ((char *)p) + 1;
452 section_len -= namelen + 4;
453 if (std_section && strcmp ((char *)p, std_section) == 0)
454 vendor = OBJ_ATTR_PROC;
455 else if (strcmp ((char *)p, "gnu") == 0)
456 vendor = OBJ_ATTR_GNU;
457 else
458 {
459 /* Other vendor section. Ignore it. */
460 p += namelen + section_len;
461 continue;
462 }
463
464 p += namelen;
465 while (section_len > 0)
466 {
467 int tag;
468 unsigned int n;
469 unsigned int val;
470 bfd_vma subsection_len;
471 bfd_byte *end;
472
473 tag = read_unsigned_leb128 (abfd, p, &n);
474 p += n;
475 subsection_len = bfd_get_32 (abfd, p);
476 p += 4;
477 if (subsection_len > section_len)
478 subsection_len = section_len;
479 section_len -= subsection_len;
480 subsection_len -= n + 4;
481 end = p + subsection_len;
482 switch (tag)
483 {
484 case Tag_File:
485 while (p < end)
486 {
487 int type;
488
489 tag = read_unsigned_leb128 (abfd, p, &n);
490 p += n;
491 type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
492 switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
493 {
494 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
495 val = read_unsigned_leb128 (abfd, p, &n);
496 p += n;
497 bfd_elf_add_obj_attr_int_string (abfd, vendor, tag,
498 val, (char *)p);
499 p += strlen ((char *)p) + 1;
500 break;
501 case ATTR_TYPE_FLAG_STR_VAL:
502 bfd_elf_add_obj_attr_string (abfd, vendor, tag,
503 (char *)p);
504 p += strlen ((char *)p) + 1;
505 break;
506 case ATTR_TYPE_FLAG_INT_VAL:
507 val = read_unsigned_leb128 (abfd, p, &n);
508 p += n;
509 bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
510 break;
511 default:
512 abort ();
513 }
514 }
515 break;
516 case Tag_Section:
517 case Tag_Symbol:
518 /* Don't have anywhere convenient to attach these.
519 Fall through for now. */
520 default:
521 /* Ignore things we don't kow about. */
522 p += subsection_len;
523 subsection_len = 0;
524 break;
525 }
526 }
527 }
528 }
529 free (contents);
530 }
531
532 /* Merge common object attributes from IBFD into OBFD. Raise an error
533 if there are conflicting attributes. Any processor-specific
534 attributes have already been merged. This must be called from the
535 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
536 target, along with any target-specific merging. Because there are
537 no common attributes other than Tag_compatibility at present, and
538 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
539 is not presently called for targets without their own
540 attributes. */
541
542 bfd_boolean
543 _bfd_elf_merge_object_attributes (bfd *ibfd, bfd *obfd)
544 {
545 obj_attribute *in_attr;
546 obj_attribute *out_attr;
547 int vendor;
548
549 /* The only common attribute is currently Tag_compatibility,
550 accepted in both processor and "gnu" sections. */
551 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
552 {
553 /* Handle Tag_compatibility. The tags are only compatible if the flags
554 are identical and, if the flags are '1', the strings are identical.
555 If the flags are non-zero, then we can only use the string "gnu". */
556 in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility];
557 out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility];
558
559 if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0)
560 {
561 _bfd_error_handler
562 (_("ERROR: %B: Must be processed by '%s' toolchain"),
563 ibfd, in_attr->s);
564 return FALSE;
565 }
566
567 if (in_attr->i != out_attr->i
568 || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0))
569 {
570 _bfd_error_handler (_("ERROR: %B: Object tag '%d, %s' is "
571 "incompatible with tag '%d, %s'"),
572 ibfd,
573 in_attr->i, in_attr->s ? in_attr->s : "",
574 out_attr->i, out_attr->s ? out_attr->s : "");
575 return FALSE;
576 }
577 }
578
579 return TRUE;
580 }
This page took 0.042049 seconds and 4 git commands to generate.