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