]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf-attrs.c
x86: Add tls check in gas
[thirdparty/binutils-gdb.git] / bfd / elf-attrs.c
CommitLineData
104d59d1 1/* ELF attributes support (based on ARM EABI attributes).
fd67aa11 2 Copyright (C) 2005-2024 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/""). */
0a1b45a2 42static bool
104d59d1
JM
43is_default_attr (obj_attribute *attr)
44{
8d2c8c3d 45 if (ATTR_TYPE_HAS_ERROR (attr->type))
0a1b45a2 46 return true;
3483fe2e 47 if (ATTR_TYPE_HAS_INT_VAL (attr->type) && attr->i != 0)
0a1b45a2 48 return false;
3483fe2e 49 if (ATTR_TYPE_HAS_STR_VAL (attr->type) && attr->s && *attr->s)
0a1b45a2 50 return false;
3483fe2e 51 if (ATTR_TYPE_HAS_NO_DEFAULT (attr->type))
0a1b45a2 52 return false;
104d59d1 53
0a1b45a2 54 return true;
104d59d1
JM
55}
56
57/* Return the size of a single attribute. */
58static bfd_vma
5ee4a1ca 59obj_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. */
75static const char *
76vendor_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. */
86static bfd_vma
87vendor_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. */
115bfd_vma
116bfd_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. */
129static bfd_byte *
130write_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. */
147static bfd_byte *
5ee4a1ca 148write_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. */
171static void
172vendor_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. */
207void
208bfd_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. */
231static obj_attribute *
5ee4a1ca 232elf_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));
a1d1634d
AM
250 if (list == NULL)
251 return NULL;
104d59d1
JM
252 memset (list, 0, sizeof (obj_attribute_list));
253 list->tag = tag;
254 /* Keep the tag list in order. */
255 lastp = &elf_other_obj_attributes (abfd)[vendor];
256 for (p = *lastp; p; p = p->next)
257 {
258 if (tag < p->tag)
259 break;
260 lastp = &p->next;
261 }
262 list->next = *lastp;
263 *lastp = list;
264 attr = &list->attr;
265 }
266
267 return attr;
268}
269
270/* Return the value of an integer object attribute. */
271int
5ee4a1ca 272bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, unsigned int tag)
104d59d1
JM
273{
274 obj_attribute_list *p;
275
276 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
277 {
d334575b 278 /* Known tags are preallocated. */
104d59d1
JM
279 return elf_known_obj_attributes (abfd)[vendor][tag].i;
280 }
281 else
282 {
283 for (p = elf_other_obj_attributes (abfd)[vendor];
284 p;
285 p = p->next)
286 {
287 if (tag == p->tag)
288 return p->attr.i;
289 if (tag < p->tag)
290 break;
291 }
292 return 0;
293 }
294}
295
296/* Add an integer object attribute. */
a1d1634d 297obj_attribute *
5ee4a1ca 298bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, unsigned int tag, unsigned int i)
104d59d1
JM
299{
300 obj_attribute *attr;
301
302 attr = elf_new_obj_attr (abfd, vendor, tag);
a1d1634d
AM
303 if (attr != NULL)
304 {
305 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
306 attr->i = i;
307 }
308 return attr;
104d59d1
JM
309}
310
311/* Duplicate an object attribute string value. */
4be1e8db
AM
312static char *
313elf_attr_strdup (bfd *abfd, const char *s, const char *end)
104d59d1 314{
4be1e8db
AM
315 char *p;
316 size_t len;
317
318 if (end)
319 len = strnlen (s, end - s);
320 else
321 len = strlen (s);
322
323 p = (char *) bfd_alloc (abfd, len + 1);
324 if (p != NULL)
325 {
326 memcpy (p, s, len);
327 p[len] = 0;
328 }
329 return p;
330}
a50b1753 331
4be1e8db
AM
332char *
333_bfd_elf_attr_strdup (bfd *abfd, const char *s)
334{
335 return elf_attr_strdup (abfd, s, NULL);
104d59d1
JM
336}
337
338/* Add a string object attribute. */
a1d1634d 339static obj_attribute *
4be1e8db
AM
340elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag,
341 const char *s, const char *end)
104d59d1
JM
342{
343 obj_attribute *attr;
344
345 attr = elf_new_obj_attr (abfd, vendor, tag);
a1d1634d
AM
346 if (attr != NULL)
347 {
348 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
349 attr->s = elf_attr_strdup (abfd, s, end);
350 if (attr->s == NULL)
351 return NULL;
352 }
353 return attr;
104d59d1
JM
354}
355
a1d1634d 356obj_attribute *
4be1e8db
AM
357bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag,
358 const char *s)
359{
a1d1634d 360 return elf_add_obj_attr_string (abfd, vendor, tag, s, NULL);
4be1e8db
AM
361}
362
363/* Add a int+string object attribute. */
a1d1634d 364static obj_attribute *
4be1e8db
AM
365elf_add_obj_attr_int_string (bfd *abfd, int vendor, unsigned int tag,
366 unsigned int i, const char *s, const char *end)
104d59d1 367{
7b86a9fa 368 obj_attribute *attr;
104d59d1 369
7b86a9fa 370 attr = elf_new_obj_attr (abfd, vendor, tag);
a1d1634d
AM
371 if (attr != NULL)
372 {
373 attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
374 attr->i = i;
375 attr->s = elf_attr_strdup (abfd, s, end);
376 if (attr->s == NULL)
377 return NULL;
378 }
379 return attr;
4be1e8db
AM
380}
381
a1d1634d 382obj_attribute *
4be1e8db
AM
383bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor, unsigned int tag,
384 unsigned int i, const char *s)
385{
a1d1634d 386 return elf_add_obj_attr_int_string (abfd, vendor, tag, i, s, NULL);
104d59d1
JM
387}
388
389/* Copy the object attributes from IBFD to OBFD. */
390void
391_bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd)
392{
393 obj_attribute *in_attr;
394 obj_attribute *out_attr;
395 obj_attribute_list *list;
396 int i;
397 int vendor;
398
dafbc74d
AM
399 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
400 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
401 return;
402
104d59d1
JM
403 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
404 {
3de4a297
JM
405 in_attr
406 = &elf_known_obj_attributes (ibfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE];
407 out_attr
408 = &elf_known_obj_attributes (obfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE];
409 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
104d59d1
JM
410 {
411 out_attr->type = in_attr->type;
412 out_attr->i = in_attr->i;
413 if (in_attr->s && *in_attr->s)
a1d1634d
AM
414 {
415 out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s);
416 if (out_attr->s == NULL)
417 bfd_perror (_("error adding attribute"));
418 }
104d59d1
JM
419 in_attr++;
420 out_attr++;
421 }
422
423 for (list = elf_other_obj_attributes (ibfd)[vendor];
424 list;
425 list = list->next)
426 {
a1d1634d 427 bool ok = false;
104d59d1 428 in_attr = &list->attr;
3483fe2e 429 switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
104d59d1 430 {
3483fe2e 431 case ATTR_TYPE_FLAG_INT_VAL:
a1d1634d
AM
432 ok = bfd_elf_add_obj_attr_int (obfd, vendor,
433 list->tag, in_attr->i);
104d59d1 434 break;
3483fe2e 435 case ATTR_TYPE_FLAG_STR_VAL:
a1d1634d
AM
436 ok = bfd_elf_add_obj_attr_string (obfd, vendor, list->tag,
437 in_attr->s);
104d59d1 438 break;
3483fe2e 439 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
a1d1634d
AM
440 ok = bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag,
441 in_attr->i, in_attr->s);
104d59d1
JM
442 break;
443 default:
444 abort ();
445 }
a1d1634d
AM
446 if (!ok)
447 bfd_perror (_("error adding attribute"));
104d59d1
JM
448 }
449 }
450}
451
452/* Determine whether a GNU object attribute tag takes an integer, a
453 string or both. */
454static int
5ee4a1ca 455gnu_obj_attrs_arg_type (unsigned int tag)
104d59d1
JM
456{
457 /* Except for Tag_compatibility, for GNU attributes we follow the
458 same rule ARM ones > 32 follow: odd-numbered tags take strings
459 and even-numbered tags take integers. In addition, tag & 2 is
460 nonzero for architecture-independent tags and zero for
461 architecture-dependent ones. */
462 if (tag == Tag_compatibility)
463 return 3;
464 else
465 return (tag & 1) != 0 ? 2 : 1;
466}
467
468/* Determine what arguments an attribute tag takes. */
469int
5ee4a1ca 470_bfd_elf_obj_attrs_arg_type (bfd *abfd, int vendor, unsigned int tag)
104d59d1
JM
471{
472 switch (vendor)
473 {
474 case OBJ_ATTR_PROC:
475 return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag);
476 break;
477 case OBJ_ATTR_GNU:
478 return gnu_obj_attrs_arg_type (tag);
479 break;
480 default:
481 abort ();
482 }
483}
484
485/* Parse an object attributes section. */
486void
487_bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
488{
489 bfd_byte *contents;
490 bfd_byte *p;
06614111 491 bfd_byte *p_end;
45dfa85a 492 const char *std_sec;
7c5fa58e 493 ufile_ptr filesize;
104d59d1 494
06614111
NC
495 /* PR 17512: file: 2844a11d. */
496 if (hdr->sh_size == 0)
497 return;
7c5fa58e
AM
498
499 filesize = bfd_get_file_size (abfd);
500 if (filesize != 0 && hdr->sh_size > filesize)
95a6d235
NC
501 {
502 /* xgettext:c-format */
503 _bfd_error_handler (_("%pB: error: attribute section '%pA' too big: %#llx"),
504 abfd, hdr->bfd_section, (long long) hdr->sh_size);
505 bfd_set_error (bfd_error_invalid_operation);
506 return;
507 }
508
4be1e8db 509 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
104d59d1
JM
510 if (!contents)
511 return;
512 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
513 hdr->sh_size))
514 {
515 free (contents);
516 return;
517 }
518 p = contents;
06614111 519 p_end = p + hdr->sh_size;
45dfa85a 520 std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor;
1b786873 521
4be1e8db 522 if (*p++ == 'A')
104d59d1 523 {
4be1e8db 524 while (p_end - p >= 4)
104d59d1 525 {
4be1e8db
AM
526 size_t len = p_end - p;
527 size_t namelen;
528 size_t section_len;
104d59d1
JM
529 int vendor;
530
531 section_len = bfd_get_32 (abfd, p);
532 p += 4;
1fe9dc45
WN
533 if (section_len == 0)
534 break;
104d59d1
JM
535 if (section_len > len)
536 section_len = len;
24d3e51b
NC
537 if (section_len <= 4)
538 {
f2b740ac 539 _bfd_error_handler
4be1e8db
AM
540 (_("%pB: error: attribute section length too small: %ld"),
541 abfd, (long) section_len);
24d3e51b
NC
542 break;
543 }
e9847026
NC
544 section_len -= 4;
545 namelen = strnlen ((char *) p, section_len) + 1;
4be1e8db 546 if (namelen >= section_len)
e9847026 547 break;
45dfa85a 548 if (std_sec && strcmp ((char *) p, std_sec) == 0)
104d59d1 549 vendor = OBJ_ATTR_PROC;
45dfa85a 550 else if (strcmp ((char *) p, "gnu") == 0)
104d59d1
JM
551 vendor = OBJ_ATTR_GNU;
552 else
553 {
554 /* Other vendor section. Ignore it. */
4be1e8db 555 p += section_len;
104d59d1
JM
556 continue;
557 }
558
559 p += namelen;
4be1e8db
AM
560 section_len -= namelen;
561 while (section_len > 0)
104d59d1 562 {
5ee4a1ca 563 unsigned int tag;
104d59d1 564 unsigned int val;
4be1e8db 565 size_t subsection_len;
574ec108 566 bfd_byte *end, *orig_p;
104d59d1 567
574ec108
AM
568 orig_p = p;
569 tag = _bfd_safe_read_leb128 (abfd, &p, false, p_end);
570 if (p_end - p >= 4)
571 {
572 subsection_len = bfd_get_32 (abfd, p);
573 p += 4;
574 }
06614111 575 else
574ec108 576 {
574ec108 577 p = p_end;
45342c7c 578 break;
574ec108 579 }
104d59d1
JM
580 if (subsection_len > section_len)
581 subsection_len = section_len;
582 section_len -= subsection_len;
574ec108 583 end = orig_p + subsection_len;
45342c7c
AM
584 if (end < p)
585 break;
104d59d1
JM
586 switch (tag)
587 {
588 case Tag_File:
589 while (p < end)
590 {
591 int type;
a1d1634d 592 bool ok = false;
104d59d1 593
574ec108 594 tag = _bfd_safe_read_leb128 (abfd, &p, false, end);
104d59d1 595 type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
3483fe2e 596 switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
104d59d1 597 {
3483fe2e 598 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
574ec108 599 val = _bfd_safe_read_leb128 (abfd, &p, false, end);
a1d1634d
AM
600 ok = elf_add_obj_attr_int_string (abfd, vendor, tag,
601 val, (char *) p,
602 (char *) end);
4be1e8db
AM
603 p += strnlen ((char *) p, end - p);
604 if (p < end)
605 p++;
104d59d1 606 break;
3483fe2e 607 case ATTR_TYPE_FLAG_STR_VAL:
a1d1634d
AM
608 ok = elf_add_obj_attr_string (abfd, vendor, tag,
609 (char *) p,
610 (char *) end);
4be1e8db
AM
611 p += strnlen ((char *) p, end - p);
612 if (p < end)
613 p++;
104d59d1 614 break;
3483fe2e 615 case ATTR_TYPE_FLAG_INT_VAL:
574ec108 616 val = _bfd_safe_read_leb128 (abfd, &p, false, end);
a1d1634d 617 ok = bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
104d59d1
JM
618 break;
619 default:
620 abort ();
621 }
a1d1634d
AM
622 if (!ok)
623 bfd_perror (_("error adding attribute"));
104d59d1
JM
624 }
625 break;
626 case Tag_Section:
627 case Tag_Symbol:
628 /* Don't have anywhere convenient to attach these.
629 Fall through for now. */
630 default:
574ec108
AM
631 /* Ignore things we don't know about. */
632 p = end;
104d59d1
JM
633 break;
634 }
635 }
636 }
637 }
638 free (contents);
639}
640
641/* Merge common object attributes from IBFD into OBFD. Raise an error
642 if there are conflicting attributes. Any processor-specific
643 attributes have already been merged. This must be called from the
644 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
645 target, along with any target-specific merging. Because there are
646 no common attributes other than Tag_compatibility at present, and
647 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
648 is not presently called for targets without their own
649 attributes. */
650
0a1b45a2 651bool
50e03d47 652_bfd_elf_merge_object_attributes (bfd *ibfd, struct bfd_link_info *info)
104d59d1 653{
50e03d47 654 bfd *obfd = info->output_bfd;
104d59d1
JM
655 obj_attribute *in_attr;
656 obj_attribute *out_attr;
104d59d1
JM
657 int vendor;
658
659 /* The only common attribute is currently Tag_compatibility,
660 accepted in both processor and "gnu" sections. */
661 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
662 {
7b86a9fa
AS
663 /* Handle Tag_compatibility. The tags are only compatible if the flags
664 are identical and, if the flags are '1', the strings are identical.
665 If the flags are non-zero, then we can only use the string "gnu". */
666 in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility];
667 out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility];
668
669 if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0)
104d59d1 670 {
7b86a9fa 671 _bfd_error_handler
695344c0 672 /* xgettext:c-format */
9793eb77 673 (_("error: %pB: object has vendor-specific contents that "
d8879994 674 "must be processed by the '%s' toolchain"),
104d59d1 675 ibfd, in_attr->s);
0a1b45a2 676 return false;
7b86a9fa 677 }
104d59d1 678
7b86a9fa
AS
679 if (in_attr->i != out_attr->i
680 || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0))
681 {
695344c0 682 /* xgettext:c-format */
9793eb77 683 _bfd_error_handler (_("error: %pB: object tag '%d, %s' is "
7b86a9fa
AS
684 "incompatible with tag '%d, %s'"),
685 ibfd,
686 in_attr->i, in_attr->s ? in_attr->s : "",
687 out_attr->i, out_attr->s ? out_attr->s : "");
0a1b45a2 688 return false;
104d59d1
JM
689 }
690 }
691
0a1b45a2 692 return true;
104d59d1 693}
e8b36cd1
JM
694
695/* Merge an unknown processor-specific attribute TAG, within the range
696 of known attributes, from IBFD into OBFD; return TRUE if the link
697 is OK, FALSE if it must fail. */
698
0a1b45a2 699bool
e8b36cd1
JM
700_bfd_elf_merge_unknown_attribute_low (bfd *ibfd, bfd *obfd, int tag)
701{
702 obj_attribute *in_attr;
703 obj_attribute *out_attr;
704 bfd *err_bfd = NULL;
0a1b45a2 705 bool result = true;
e8b36cd1
JM
706
707 in_attr = elf_known_obj_attributes_proc (ibfd);
708 out_attr = elf_known_obj_attributes_proc (obfd);
709
710 if (out_attr[tag].i != 0 || out_attr[tag].s != NULL)
711 err_bfd = obfd;
712 else if (in_attr[tag].i != 0 || in_attr[tag].s != NULL)
713 err_bfd = ibfd;
714
715 if (err_bfd != NULL)
716 result
717 = get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag);
718
719 /* Only pass on attributes that match in both inputs. */
720 if (in_attr[tag].i != out_attr[tag].i
e1a6b263 721 || (in_attr[tag].s == NULL) != (out_attr[tag].s == NULL)
e8b36cd1
JM
722 || (in_attr[tag].s != NULL && out_attr[tag].s != NULL
723 && strcmp (in_attr[tag].s, out_attr[tag].s) != 0))
724 {
725 out_attr[tag].i = 0;
726 out_attr[tag].s = NULL;
727 }
728
729 return result;
730}
731
732/* Merge the lists of unknown processor-specific attributes, outside
733 the known range, from IBFD into OBFD; return TRUE if the link is
734 OK, FALSE if it must fail. */
735
0a1b45a2 736bool
e8b36cd1
JM
737_bfd_elf_merge_unknown_attribute_list (bfd *ibfd, bfd *obfd)
738{
739 obj_attribute_list *in_list;
740 obj_attribute_list *out_list;
741 obj_attribute_list **out_listp;
0a1b45a2 742 bool result = true;
e8b36cd1
JM
743
744 in_list = elf_other_obj_attributes_proc (ibfd);
745 out_listp = &elf_other_obj_attributes_proc (obfd);
746 out_list = *out_listp;
747
748 for (; in_list || out_list; )
749 {
750 bfd *err_bfd = NULL;
5ee4a1ca 751 unsigned int err_tag = 0;
e8b36cd1
JM
752
753 /* The tags for each list are in numerical order. */
754 /* If the tags are equal, then merge. */
755 if (out_list && (!in_list || in_list->tag > out_list->tag))
756 {
757 /* This attribute only exists in obfd. We can't merge, and we don't
758 know what the tag means, so delete it. */
759 err_bfd = obfd;
760 err_tag = out_list->tag;
761 *out_listp = out_list->next;
762 out_list = *out_listp;
763 }
764 else if (in_list && (!out_list || in_list->tag < out_list->tag))
765 {
766 /* This attribute only exists in ibfd. We can't merge, and we don't
767 know what the tag means, so ignore it. */
768 err_bfd = ibfd;
769 err_tag = in_list->tag;
770 in_list = in_list->next;
771 }
772 else /* The tags are equal. */
773 {
774 /* As present, all attributes in the list are unknown, and
775 therefore can't be merged meaningfully. */
776 err_bfd = obfd;
777 err_tag = out_list->tag;
778
779 /* Only pass on attributes that match in both inputs. */
780 if (in_list->attr.i != out_list->attr.i
e1a6b263 781 || (in_list->attr.s == NULL) != (out_list->attr.s == NULL)
e8b36cd1
JM
782 || (in_list->attr.s && out_list->attr.s
783 && strcmp (in_list->attr.s, out_list->attr.s) != 0))
784 {
785 /* No match. Delete the attribute. */
786 *out_listp = out_list->next;
787 out_list = *out_listp;
788 }
789 else
790 {
791 /* Matched. Keep the attribute and move to the next. */
792 out_list = out_list->next;
793 in_list = in_list->next;
794 }
795 }
796
797 if (err_bfd)
798 result = result
799 && get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd,
800 err_tag);
801 }
802
803 return result;
804}