Commit | Line | Data |
---|---|---|
104d59d1 | 1 | /* ELF attributes support (based on ARM EABI attributes). |
b3adc24a | 2 | Copyright (C) 2005-2020 Free Software Foundation, Inc. |
104d59d1 JM |
3 | |
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
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 | |
cd123cb7 | 8 | the Free Software Foundation; either version 3 of the License, or |
104d59d1 JM |
9 | (at your option) any later version. |
10 | ||
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. | |
15 | ||
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 | |
cd123cb7 NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
104d59d1 JM |
20 | |
21 | #include "sysdep.h" | |
22 | #include "bfd.h" | |
23 | #include "libiberty.h" | |
24 | #include "libbfd.h" | |
25 | #include "elf-bfd.h" | |
26 | ||
27 | /* Return the number of bytes needed by I in uleb128 format. */ | |
28 | static int | |
29 | uleb128_size (unsigned int i) | |
30 | { | |
31 | int size; | |
32 | size = 1; | |
33 | while (i >= 0x80) | |
34 | { | |
35 | i >>= 7; | |
36 | size++; | |
37 | } | |
38 | return size; | |
39 | } | |
40 | ||
41 | /* Return TRUE if the attribute has the default value (0/""). */ | |
42 | static bfd_boolean | |
43 | is_default_attr (obj_attribute *attr) | |
44 | { | |
8d2c8c3d AM |
45 | if (ATTR_TYPE_HAS_ERROR (attr->type)) |
46 | return TRUE; | |
3483fe2e | 47 | if (ATTR_TYPE_HAS_INT_VAL (attr->type) && attr->i != 0) |
104d59d1 | 48 | return FALSE; |
3483fe2e | 49 | if (ATTR_TYPE_HAS_STR_VAL (attr->type) && attr->s && *attr->s) |
104d59d1 | 50 | return FALSE; |
3483fe2e | 51 | if (ATTR_TYPE_HAS_NO_DEFAULT (attr->type)) |
2d0bb761 | 52 | return FALSE; |
104d59d1 JM |
53 | |
54 | return TRUE; | |
55 | } | |
56 | ||
57 | /* Return the size of a single attribute. */ | |
58 | static bfd_vma | |
5ee4a1ca | 59 | obj_attr_size (unsigned int tag, obj_attribute *attr) |
104d59d1 JM |
60 | { |
61 | bfd_vma size; | |
62 | ||
63 | if (is_default_attr (attr)) | |
64 | return 0; | |
65 | ||
66 | size = uleb128_size (tag); | |
3483fe2e | 67 | if (ATTR_TYPE_HAS_INT_VAL (attr->type)) |
104d59d1 | 68 | size += uleb128_size (attr->i); |
3483fe2e | 69 | if (ATTR_TYPE_HAS_STR_VAL (attr->type)) |
104d59d1 JM |
70 | size += strlen ((char *)attr->s) + 1; |
71 | return size; | |
72 | } | |
73 | ||
74 | /* Return the vendor name for a given object attributes section. */ | |
75 | static const char * | |
76 | vendor_obj_attr_name (bfd *abfd, int vendor) | |
77 | { | |
78 | return (vendor == OBJ_ATTR_PROC | |
79 | ? get_elf_backend_data (abfd)->obj_attrs_vendor | |
80 | : "gnu"); | |
81 | } | |
82 | ||
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. */ | |
86 | static bfd_vma | |
87 | vendor_obj_attr_size (bfd *abfd, int vendor) | |
88 | { | |
89 | bfd_vma size; | |
90 | obj_attribute *attr; | |
91 | obj_attribute_list *list; | |
92 | int i; | |
93 | const char *vendor_name = vendor_obj_attr_name (abfd, vendor); | |
94 | ||
95 | if (!vendor_name) | |
96 | return 0; | |
97 | ||
98 | attr = elf_known_obj_attributes (abfd)[vendor]; | |
99 | size = 0; | |
3de4a297 | 100 | for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) |
104d59d1 JM |
101 | size += obj_attr_size (i, &attr[i]); |
102 | ||
103 | for (list = elf_other_obj_attributes (abfd)[vendor]; | |
104 | list; | |
105 | list = list->next) | |
106 | size += obj_attr_size (list->tag, &list->attr); | |
107 | ||
108 | /* <size> <vendor_name> NUL 0x1 <size> */ | |
a4bf3d07 | 109 | return (size |
104d59d1 JM |
110 | ? size + 10 + strlen (vendor_name) |
111 | : 0); | |
112 | } | |
113 | ||
114 | /* Return the size of the object attributes section. */ | |
115 | bfd_vma | |
116 | bfd_elf_obj_attr_size (bfd *abfd) | |
117 | { | |
118 | bfd_vma size; | |
119 | ||
120 | size = vendor_obj_attr_size (abfd, OBJ_ATTR_PROC); | |
121 | size += vendor_obj_attr_size (abfd, OBJ_ATTR_GNU); | |
122 | ||
123 | /* 'A' <sections for each vendor> */ | |
124 | return (size ? size + 1 : 0); | |
125 | } | |
126 | ||
127 | /* Write VAL in uleb128 format to P, returning a pointer to the | |
128 | following byte. */ | |
129 | static bfd_byte * | |
130 | write_uleb128 (bfd_byte *p, unsigned int val) | |
131 | { | |
132 | bfd_byte c; | |
133 | do | |
134 | { | |
135 | c = val & 0x7f; | |
136 | val >>= 7; | |
137 | if (val) | |
138 | c |= 0x80; | |
139 | *(p++) = c; | |
140 | } | |
141 | while (val); | |
142 | return p; | |
143 | } | |
144 | ||
145 | /* Write attribute ATTR to butter P, and return a pointer to the following | |
146 | byte. */ | |
147 | static bfd_byte * | |
5ee4a1ca | 148 | write_obj_attribute (bfd_byte *p, unsigned int tag, obj_attribute *attr) |
104d59d1 JM |
149 | { |
150 | /* Suppress default entries. */ | |
151 | if (is_default_attr (attr)) | |
152 | return p; | |
153 | ||
154 | p = write_uleb128 (p, tag); | |
3483fe2e | 155 | if (ATTR_TYPE_HAS_INT_VAL (attr->type)) |
104d59d1 | 156 | p = write_uleb128 (p, attr->i); |
3483fe2e | 157 | if (ATTR_TYPE_HAS_STR_VAL (attr->type)) |
104d59d1 JM |
158 | { |
159 | int len; | |
160 | ||
161 | len = strlen (attr->s) + 1; | |
162 | memcpy (p, attr->s, len); | |
163 | p += len; | |
164 | } | |
165 | ||
166 | return p; | |
167 | } | |
168 | ||
169 | /* Write the contents of the object attributes section (length SIZE) | |
170 | for VENDOR to CONTENTS. */ | |
171 | static void | |
172 | vendor_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size, | |
173 | int vendor) | |
174 | { | |
175 | bfd_byte *p; | |
176 | obj_attribute *attr; | |
177 | obj_attribute_list *list; | |
178 | int i; | |
179 | const char *vendor_name = vendor_obj_attr_name (abfd, vendor); | |
180 | size_t vendor_length = strlen (vendor_name) + 1; | |
181 | ||
182 | p = contents; | |
183 | bfd_put_32 (abfd, size, p); | |
184 | p += 4; | |
185 | memcpy (p, vendor_name, vendor_length); | |
186 | p += vendor_length; | |
187 | *(p++) = Tag_File; | |
188 | bfd_put_32 (abfd, size - 4 - vendor_length, p); | |
189 | p += 4; | |
190 | ||
191 | attr = elf_known_obj_attributes (abfd)[vendor]; | |
3de4a297 | 192 | for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) |
5aa6ff7c | 193 | { |
5ee4a1ca | 194 | unsigned int tag = i; |
5aa6ff7c AS |
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]); | |
198 | } | |
104d59d1 JM |
199 | |
200 | for (list = elf_other_obj_attributes (abfd)[vendor]; | |
201 | list; | |
202 | list = list->next) | |
203 | p = write_obj_attribute (p, list->tag, &list->attr); | |
204 | } | |
205 | ||
206 | /* Write the contents of the object attributes section to CONTENTS. */ | |
207 | void | |
208 | bfd_elf_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size) | |
209 | { | |
210 | bfd_byte *p; | |
211 | int vendor; | |
212 | bfd_vma my_size; | |
213 | ||
214 | p = contents; | |
215 | *(p++) = 'A'; | |
216 | my_size = 1; | |
217 | for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++) | |
218 | { | |
219 | bfd_vma vendor_size = vendor_obj_attr_size (abfd, vendor); | |
220 | if (vendor_size) | |
221 | vendor_set_obj_attr_contents (abfd, p, vendor_size, vendor); | |
222 | p += vendor_size; | |
223 | my_size += vendor_size; | |
224 | } | |
225 | ||
226 | if (size != my_size) | |
227 | abort (); | |
228 | } | |
229 | ||
230 | /* Allocate/find an object attribute. */ | |
231 | static obj_attribute * | |
5ee4a1ca | 232 | elf_new_obj_attr (bfd *abfd, int vendor, unsigned int tag) |
104d59d1 JM |
233 | { |
234 | obj_attribute *attr; | |
235 | obj_attribute_list *list; | |
236 | obj_attribute_list *p; | |
237 | obj_attribute_list **lastp; | |
238 | ||
239 | ||
240 | if (tag < NUM_KNOWN_OBJ_ATTRIBUTES) | |
241 | { | |
d334575b | 242 | /* Known tags are preallocated. */ |
104d59d1 JM |
243 | attr = &elf_known_obj_attributes (abfd)[vendor][tag]; |
244 | } | |
245 | else | |
246 | { | |
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)); | |
251 | list->tag = tag; | |
252 | /* Keep the tag list in order. */ | |
253 | lastp = &elf_other_obj_attributes (abfd)[vendor]; | |
254 | for (p = *lastp; p; p = p->next) | |
255 | { | |
256 | if (tag < p->tag) | |
257 | break; | |
258 | lastp = &p->next; | |
259 | } | |
260 | list->next = *lastp; | |
261 | *lastp = list; | |
262 | attr = &list->attr; | |
263 | } | |
264 | ||
265 | return attr; | |
266 | } | |
267 | ||
268 | /* Return the value of an integer object attribute. */ | |
269 | int | |
5ee4a1ca | 270 | bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, unsigned int tag) |
104d59d1 JM |
271 | { |
272 | obj_attribute_list *p; | |
273 | ||
274 | if (tag < NUM_KNOWN_OBJ_ATTRIBUTES) | |
275 | { | |
d334575b | 276 | /* Known tags are preallocated. */ |
104d59d1 JM |
277 | return elf_known_obj_attributes (abfd)[vendor][tag].i; |
278 | } | |
279 | else | |
280 | { | |
281 | for (p = elf_other_obj_attributes (abfd)[vendor]; | |
282 | p; | |
283 | p = p->next) | |
284 | { | |
285 | if (tag == p->tag) | |
286 | return p->attr.i; | |
287 | if (tag < p->tag) | |
288 | break; | |
289 | } | |
290 | return 0; | |
291 | } | |
292 | } | |
293 | ||
294 | /* Add an integer object attribute. */ | |
295 | void | |
5ee4a1ca | 296 | bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, unsigned int tag, unsigned int i) |
104d59d1 JM |
297 | { |
298 | obj_attribute *attr; | |
299 | ||
300 | attr = elf_new_obj_attr (abfd, vendor, tag); | |
2d0bb761 | 301 | attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); |
104d59d1 JM |
302 | attr->i = i; |
303 | } | |
304 | ||
305 | /* Duplicate an object attribute string value. */ | |
306 | char * | |
307 | _bfd_elf_attr_strdup (bfd *abfd, const char * s) | |
308 | { | |
309 | char * p; | |
310 | int len; | |
a50b1753 | 311 | |
104d59d1 JM |
312 | len = strlen (s) + 1; |
313 | p = (char *) bfd_alloc (abfd, len); | |
a50b1753 | 314 | return (char *) memcpy (p, s, len); |
104d59d1 JM |
315 | } |
316 | ||
317 | /* Add a string object attribute. */ | |
318 | void | |
5ee4a1ca | 319 | bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag, const char *s) |
104d59d1 JM |
320 | { |
321 | obj_attribute *attr; | |
322 | ||
323 | attr = elf_new_obj_attr (abfd, vendor, tag); | |
2d0bb761 | 324 | attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); |
104d59d1 JM |
325 | attr->s = _bfd_elf_attr_strdup (abfd, s); |
326 | } | |
327 | ||
7b86a9fa | 328 | /* Add a int+string object attribute. */ |
104d59d1 | 329 | void |
5ee4a1ca NC |
330 | bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor, |
331 | unsigned int tag, | |
7b86a9fa | 332 | unsigned int i, const char *s) |
104d59d1 | 333 | { |
7b86a9fa | 334 | obj_attribute *attr; |
104d59d1 | 335 | |
7b86a9fa | 336 | attr = elf_new_obj_attr (abfd, vendor, tag); |
2d0bb761 | 337 | attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); |
7b86a9fa AS |
338 | attr->i = i; |
339 | attr->s = _bfd_elf_attr_strdup (abfd, s); | |
104d59d1 JM |
340 | } |
341 | ||
342 | /* Copy the object attributes from IBFD to OBFD. */ | |
343 | void | |
344 | _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd) | |
345 | { | |
346 | obj_attribute *in_attr; | |
347 | obj_attribute *out_attr; | |
348 | obj_attribute_list *list; | |
349 | int i; | |
350 | int vendor; | |
351 | ||
dafbc74d AM |
352 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
353 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
354 | return; | |
355 | ||
104d59d1 JM |
356 | for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++) |
357 | { | |
3de4a297 JM |
358 | in_attr |
359 | = &elf_known_obj_attributes (ibfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE]; | |
360 | out_attr | |
361 | = &elf_known_obj_attributes (obfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE]; | |
362 | for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) | |
104d59d1 JM |
363 | { |
364 | out_attr->type = in_attr->type; | |
365 | out_attr->i = in_attr->i; | |
366 | if (in_attr->s && *in_attr->s) | |
367 | out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s); | |
368 | in_attr++; | |
369 | out_attr++; | |
370 | } | |
371 | ||
372 | for (list = elf_other_obj_attributes (ibfd)[vendor]; | |
373 | list; | |
374 | list = list->next) | |
375 | { | |
376 | in_attr = &list->attr; | |
3483fe2e | 377 | switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL)) |
104d59d1 | 378 | { |
3483fe2e | 379 | case ATTR_TYPE_FLAG_INT_VAL: |
104d59d1 JM |
380 | bfd_elf_add_obj_attr_int (obfd, vendor, list->tag, in_attr->i); |
381 | break; | |
3483fe2e | 382 | case ATTR_TYPE_FLAG_STR_VAL: |
104d59d1 JM |
383 | bfd_elf_add_obj_attr_string (obfd, vendor, list->tag, |
384 | in_attr->s); | |
385 | break; | |
3483fe2e | 386 | case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: |
7b86a9fa AS |
387 | bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag, |
388 | in_attr->i, in_attr->s); | |
104d59d1 JM |
389 | break; |
390 | default: | |
391 | abort (); | |
392 | } | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | /* Determine whether a GNU object attribute tag takes an integer, a | |
398 | string or both. */ | |
399 | static int | |
5ee4a1ca | 400 | gnu_obj_attrs_arg_type (unsigned int tag) |
104d59d1 JM |
401 | { |
402 | /* Except for Tag_compatibility, for GNU attributes we follow the | |
403 | same rule ARM ones > 32 follow: odd-numbered tags take strings | |
404 | and even-numbered tags take integers. In addition, tag & 2 is | |
405 | nonzero for architecture-independent tags and zero for | |
406 | architecture-dependent ones. */ | |
407 | if (tag == Tag_compatibility) | |
408 | return 3; | |
409 | else | |
410 | return (tag & 1) != 0 ? 2 : 1; | |
411 | } | |
412 | ||
413 | /* Determine what arguments an attribute tag takes. */ | |
414 | int | |
5ee4a1ca | 415 | _bfd_elf_obj_attrs_arg_type (bfd *abfd, int vendor, unsigned int tag) |
104d59d1 JM |
416 | { |
417 | switch (vendor) | |
418 | { | |
419 | case OBJ_ATTR_PROC: | |
420 | return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag); | |
421 | break; | |
422 | case OBJ_ATTR_GNU: | |
423 | return gnu_obj_attrs_arg_type (tag); | |
424 | break; | |
425 | default: | |
426 | abort (); | |
427 | } | |
428 | } | |
429 | ||
430 | /* Parse an object attributes section. */ | |
431 | void | |
432 | _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr) | |
433 | { | |
434 | bfd_byte *contents; | |
435 | bfd_byte *p; | |
06614111 | 436 | bfd_byte *p_end; |
104d59d1 | 437 | bfd_vma len; |
45dfa85a | 438 | const char *std_sec; |
7c5fa58e | 439 | ufile_ptr filesize; |
104d59d1 | 440 | |
06614111 NC |
441 | /* PR 17512: file: 2844a11d. */ |
442 | if (hdr->sh_size == 0) | |
443 | return; | |
7c5fa58e AM |
444 | |
445 | filesize = bfd_get_file_size (abfd); | |
446 | if (filesize != 0 && hdr->sh_size > filesize) | |
95a6d235 NC |
447 | { |
448 | /* xgettext:c-format */ | |
449 | _bfd_error_handler (_("%pB: error: attribute section '%pA' too big: %#llx"), | |
450 | abfd, hdr->bfd_section, (long long) hdr->sh_size); | |
451 | bfd_set_error (bfd_error_invalid_operation); | |
452 | return; | |
453 | } | |
454 | ||
2a143b99 | 455 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size + 1); |
104d59d1 JM |
456 | if (!contents) |
457 | return; | |
458 | if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0, | |
459 | hdr->sh_size)) | |
460 | { | |
461 | free (contents); | |
462 | return; | |
463 | } | |
2a143b99 NC |
464 | /* Ensure that the buffer is NUL terminated. */ |
465 | contents[hdr->sh_size] = 0; | |
104d59d1 | 466 | p = contents; |
06614111 | 467 | p_end = p + hdr->sh_size; |
45dfa85a | 468 | std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor; |
1b786873 | 469 | |
104d59d1 JM |
470 | if (*(p++) == 'A') |
471 | { | |
472 | len = hdr->sh_size - 1; | |
06614111 NC |
473 | |
474 | while (len > 0 && p < p_end - 4) | |
104d59d1 | 475 | { |
e9847026 | 476 | unsigned namelen; |
104d59d1 JM |
477 | bfd_vma section_len; |
478 | int vendor; | |
479 | ||
480 | section_len = bfd_get_32 (abfd, p); | |
481 | p += 4; | |
1fe9dc45 WN |
482 | if (section_len == 0) |
483 | break; | |
104d59d1 JM |
484 | if (section_len > len) |
485 | section_len = len; | |
486 | len -= section_len; | |
24d3e51b NC |
487 | if (section_len <= 4) |
488 | { | |
f2b740ac AM |
489 | _bfd_error_handler |
490 | (_("%pB: error: attribute section length too small: %" PRId64), | |
491 | abfd, (int64_t) section_len); | |
24d3e51b NC |
492 | break; |
493 | } | |
e9847026 NC |
494 | section_len -= 4; |
495 | namelen = strnlen ((char *) p, section_len) + 1; | |
496 | if (namelen == 0 || namelen >= section_len) | |
497 | break; | |
498 | section_len -= namelen; | |
45dfa85a | 499 | if (std_sec && strcmp ((char *) p, std_sec) == 0) |
104d59d1 | 500 | vendor = OBJ_ATTR_PROC; |
45dfa85a | 501 | else if (strcmp ((char *) p, "gnu") == 0) |
104d59d1 JM |
502 | vendor = OBJ_ATTR_GNU; |
503 | else | |
504 | { | |
505 | /* Other vendor section. Ignore it. */ | |
506 | p += namelen + section_len; | |
507 | continue; | |
508 | } | |
509 | ||
510 | p += namelen; | |
06614111 | 511 | while (section_len > 0 && p < p_end) |
104d59d1 | 512 | { |
5ee4a1ca | 513 | unsigned int tag; |
104d59d1 JM |
514 | unsigned int n; |
515 | unsigned int val; | |
516 | bfd_vma subsection_len; | |
517 | bfd_byte *end; | |
518 | ||
4265548c | 519 | tag = _bfd_safe_read_leb128 (abfd, p, &n, FALSE, p_end); |
104d59d1 | 520 | p += n; |
06614111 NC |
521 | if (p < p_end - 4) |
522 | subsection_len = bfd_get_32 (abfd, p); | |
523 | else | |
524 | subsection_len = 0; | |
104d59d1 | 525 | p += 4; |
1fe9dc45 WN |
526 | if (subsection_len == 0) |
527 | break; | |
104d59d1 JM |
528 | if (subsection_len > section_len) |
529 | subsection_len = section_len; | |
530 | section_len -= subsection_len; | |
531 | subsection_len -= n + 4; | |
532 | end = p + subsection_len; | |
f64e188b NC |
533 | /* PR 17512: file: 0e8c0c90. */ |
534 | if (end > p_end) | |
535 | end = p_end; | |
104d59d1 JM |
536 | switch (tag) |
537 | { | |
538 | case Tag_File: | |
539 | while (p < end) | |
540 | { | |
541 | int type; | |
542 | ||
4265548c | 543 | tag = _bfd_safe_read_leb128 (abfd, p, &n, FALSE, end); |
104d59d1 JM |
544 | p += n; |
545 | type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); | |
3483fe2e | 546 | switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL)) |
104d59d1 | 547 | { |
3483fe2e | 548 | case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: |
4265548c | 549 | val = _bfd_safe_read_leb128 (abfd, p, &n, FALSE, end); |
104d59d1 | 550 | p += n; |
7b86a9fa | 551 | bfd_elf_add_obj_attr_int_string (abfd, vendor, tag, |
f64e188b | 552 | val, (char *) p); |
104d59d1 JM |
553 | p += strlen ((char *)p) + 1; |
554 | break; | |
3483fe2e | 555 | case ATTR_TYPE_FLAG_STR_VAL: |
104d59d1 | 556 | bfd_elf_add_obj_attr_string (abfd, vendor, tag, |
f64e188b | 557 | (char *) p); |
104d59d1 JM |
558 | p += strlen ((char *)p) + 1; |
559 | break; | |
3483fe2e | 560 | case ATTR_TYPE_FLAG_INT_VAL: |
4265548c | 561 | val = _bfd_safe_read_leb128 (abfd, p, &n, FALSE, end); |
104d59d1 JM |
562 | p += n; |
563 | bfd_elf_add_obj_attr_int (abfd, vendor, tag, val); | |
564 | break; | |
565 | default: | |
566 | abort (); | |
567 | } | |
568 | } | |
569 | break; | |
570 | case Tag_Section: | |
571 | case Tag_Symbol: | |
572 | /* Don't have anywhere convenient to attach these. | |
573 | Fall through for now. */ | |
574 | default: | |
575 | /* Ignore things we don't kow about. */ | |
576 | p += subsection_len; | |
577 | subsection_len = 0; | |
578 | break; | |
579 | } | |
580 | } | |
581 | } | |
582 | } | |
583 | free (contents); | |
584 | } | |
585 | ||
586 | /* Merge common object attributes from IBFD into OBFD. Raise an error | |
587 | if there are conflicting attributes. Any processor-specific | |
588 | attributes have already been merged. This must be called from the | |
589 | bfd_elfNN_bfd_merge_private_bfd_data hook for each individual | |
590 | target, along with any target-specific merging. Because there are | |
591 | no common attributes other than Tag_compatibility at present, and | |
592 | non-"gnu" Tag_compatibility is not expected in "gnu" sections, this | |
593 | is not presently called for targets without their own | |
594 | attributes. */ | |
595 | ||
596 | bfd_boolean | |
50e03d47 | 597 | _bfd_elf_merge_object_attributes (bfd *ibfd, struct bfd_link_info *info) |
104d59d1 | 598 | { |
50e03d47 | 599 | bfd *obfd = info->output_bfd; |
104d59d1 JM |
600 | obj_attribute *in_attr; |
601 | obj_attribute *out_attr; | |
104d59d1 JM |
602 | int vendor; |
603 | ||
604 | /* The only common attribute is currently Tag_compatibility, | |
605 | accepted in both processor and "gnu" sections. */ | |
606 | for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++) | |
607 | { | |
7b86a9fa AS |
608 | /* Handle Tag_compatibility. The tags are only compatible if the flags |
609 | are identical and, if the flags are '1', the strings are identical. | |
610 | If the flags are non-zero, then we can only use the string "gnu". */ | |
611 | in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility]; | |
612 | out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility]; | |
613 | ||
614 | if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0) | |
104d59d1 | 615 | { |
7b86a9fa | 616 | _bfd_error_handler |
695344c0 | 617 | /* xgettext:c-format */ |
9793eb77 | 618 | (_("error: %pB: object has vendor-specific contents that " |
d8879994 | 619 | "must be processed by the '%s' toolchain"), |
104d59d1 | 620 | ibfd, in_attr->s); |
7b86a9fa AS |
621 | return FALSE; |
622 | } | |
104d59d1 | 623 | |
7b86a9fa AS |
624 | if (in_attr->i != out_attr->i |
625 | || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0)) | |
626 | { | |
695344c0 | 627 | /* xgettext:c-format */ |
9793eb77 | 628 | _bfd_error_handler (_("error: %pB: object tag '%d, %s' is " |
7b86a9fa AS |
629 | "incompatible with tag '%d, %s'"), |
630 | ibfd, | |
631 | in_attr->i, in_attr->s ? in_attr->s : "", | |
632 | out_attr->i, out_attr->s ? out_attr->s : ""); | |
633 | return FALSE; | |
104d59d1 JM |
634 | } |
635 | } | |
636 | ||
637 | return TRUE; | |
638 | } | |
e8b36cd1 JM |
639 | |
640 | /* Merge an unknown processor-specific attribute TAG, within the range | |
641 | of known attributes, from IBFD into OBFD; return TRUE if the link | |
642 | is OK, FALSE if it must fail. */ | |
643 | ||
644 | bfd_boolean | |
645 | _bfd_elf_merge_unknown_attribute_low (bfd *ibfd, bfd *obfd, int tag) | |
646 | { | |
647 | obj_attribute *in_attr; | |
648 | obj_attribute *out_attr; | |
649 | bfd *err_bfd = NULL; | |
650 | bfd_boolean result = TRUE; | |
651 | ||
652 | in_attr = elf_known_obj_attributes_proc (ibfd); | |
653 | out_attr = elf_known_obj_attributes_proc (obfd); | |
654 | ||
655 | if (out_attr[tag].i != 0 || out_attr[tag].s != NULL) | |
656 | err_bfd = obfd; | |
657 | else if (in_attr[tag].i != 0 || in_attr[tag].s != NULL) | |
658 | err_bfd = ibfd; | |
659 | ||
660 | if (err_bfd != NULL) | |
661 | result | |
662 | = get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag); | |
663 | ||
664 | /* Only pass on attributes that match in both inputs. */ | |
665 | if (in_attr[tag].i != out_attr[tag].i | |
e1a6b263 | 666 | || (in_attr[tag].s == NULL) != (out_attr[tag].s == NULL) |
e8b36cd1 JM |
667 | || (in_attr[tag].s != NULL && out_attr[tag].s != NULL |
668 | && strcmp (in_attr[tag].s, out_attr[tag].s) != 0)) | |
669 | { | |
670 | out_attr[tag].i = 0; | |
671 | out_attr[tag].s = NULL; | |
672 | } | |
673 | ||
674 | return result; | |
675 | } | |
676 | ||
677 | /* Merge the lists of unknown processor-specific attributes, outside | |
678 | the known range, from IBFD into OBFD; return TRUE if the link is | |
679 | OK, FALSE if it must fail. */ | |
680 | ||
681 | bfd_boolean | |
682 | _bfd_elf_merge_unknown_attribute_list (bfd *ibfd, bfd *obfd) | |
683 | { | |
684 | obj_attribute_list *in_list; | |
685 | obj_attribute_list *out_list; | |
686 | obj_attribute_list **out_listp; | |
687 | bfd_boolean result = TRUE; | |
688 | ||
689 | in_list = elf_other_obj_attributes_proc (ibfd); | |
690 | out_listp = &elf_other_obj_attributes_proc (obfd); | |
691 | out_list = *out_listp; | |
692 | ||
693 | for (; in_list || out_list; ) | |
694 | { | |
695 | bfd *err_bfd = NULL; | |
5ee4a1ca | 696 | unsigned int err_tag = 0; |
e8b36cd1 JM |
697 | |
698 | /* The tags for each list are in numerical order. */ | |
699 | /* If the tags are equal, then merge. */ | |
700 | if (out_list && (!in_list || in_list->tag > out_list->tag)) | |
701 | { | |
702 | /* This attribute only exists in obfd. We can't merge, and we don't | |
703 | know what the tag means, so delete it. */ | |
704 | err_bfd = obfd; | |
705 | err_tag = out_list->tag; | |
706 | *out_listp = out_list->next; | |
707 | out_list = *out_listp; | |
708 | } | |
709 | else if (in_list && (!out_list || in_list->tag < out_list->tag)) | |
710 | { | |
711 | /* This attribute only exists in ibfd. We can't merge, and we don't | |
712 | know what the tag means, so ignore it. */ | |
713 | err_bfd = ibfd; | |
714 | err_tag = in_list->tag; | |
715 | in_list = in_list->next; | |
716 | } | |
717 | else /* The tags are equal. */ | |
718 | { | |
719 | /* As present, all attributes in the list are unknown, and | |
720 | therefore can't be merged meaningfully. */ | |
721 | err_bfd = obfd; | |
722 | err_tag = out_list->tag; | |
723 | ||
724 | /* Only pass on attributes that match in both inputs. */ | |
725 | if (in_list->attr.i != out_list->attr.i | |
e1a6b263 | 726 | || (in_list->attr.s == NULL) != (out_list->attr.s == NULL) |
e8b36cd1 JM |
727 | || (in_list->attr.s && out_list->attr.s |
728 | && strcmp (in_list->attr.s, out_list->attr.s) != 0)) | |
729 | { | |
730 | /* No match. Delete the attribute. */ | |
731 | *out_listp = out_list->next; | |
732 | out_list = *out_listp; | |
733 | } | |
734 | else | |
735 | { | |
736 | /* Matched. Keep the attribute and move to the next. */ | |
737 | out_list = out_list->next; | |
738 | in_list = in_list->next; | |
739 | } | |
740 | } | |
741 | ||
742 | if (err_bfd) | |
743 | result = result | |
744 | && get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, | |
745 | err_tag); | |
746 | } | |
747 | ||
748 | return result; | |
749 | } |