| 1 | /* ELF attributes support (based on ARM EABI attributes). |
| 2 | Copyright (C) 2005-2015 Free Software Foundation, Inc. |
| 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 |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 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 |
| 18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 19 | MA 02110-1301, USA. */ |
| 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 | { |
| 45 | if (ATTR_TYPE_HAS_INT_VAL (attr->type) && attr->i != 0) |
| 46 | return FALSE; |
| 47 | if (ATTR_TYPE_HAS_STR_VAL (attr->type) && attr->s && *attr->s) |
| 48 | return FALSE; |
| 49 | if (ATTR_TYPE_HAS_NO_DEFAULT (attr->type)) |
| 50 | return FALSE; |
| 51 | |
| 52 | return TRUE; |
| 53 | } |
| 54 | |
| 55 | /* Return the size of a single attribute. */ |
| 56 | static bfd_vma |
| 57 | obj_attr_size (int tag, obj_attribute *attr) |
| 58 | { |
| 59 | bfd_vma size; |
| 60 | |
| 61 | if (is_default_attr (attr)) |
| 62 | return 0; |
| 63 | |
| 64 | size = uleb128_size (tag); |
| 65 | if (ATTR_TYPE_HAS_INT_VAL (attr->type)) |
| 66 | size += uleb128_size (attr->i); |
| 67 | if (ATTR_TYPE_HAS_STR_VAL (attr->type)) |
| 68 | size += strlen ((char *)attr->s) + 1; |
| 69 | return size; |
| 70 | } |
| 71 | |
| 72 | /* Return the vendor name for a given object attributes section. */ |
| 73 | static const char * |
| 74 | vendor_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. */ |
| 84 | static bfd_vma |
| 85 | vendor_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; |
| 98 | for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) |
| 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. */ |
| 113 | bfd_vma |
| 114 | bfd_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. */ |
| 127 | static bfd_byte * |
| 128 | write_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. */ |
| 145 | static bfd_byte * |
| 146 | write_obj_attribute (bfd_byte *p, int tag, obj_attribute *attr) |
| 147 | { |
| 148 | /* Suppress default entries. */ |
| 149 | if (is_default_attr (attr)) |
| 150 | return p; |
| 151 | |
| 152 | p = write_uleb128 (p, tag); |
| 153 | if (ATTR_TYPE_HAS_INT_VAL (attr->type)) |
| 154 | p = write_uleb128 (p, attr->i); |
| 155 | if (ATTR_TYPE_HAS_STR_VAL (attr->type)) |
| 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. */ |
| 169 | static void |
| 170 | vendor_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]; |
| 190 | for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) |
| 191 | { |
| 192 | int tag = i; |
| 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 | } |
| 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. */ |
| 205 | void |
| 206 | bfd_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. */ |
| 229 | static obj_attribute * |
| 230 | elf_new_obj_attr (bfd *abfd, int vendor, int tag) |
| 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 | { |
| 240 | /* Known tags are preallocated. */ |
| 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. */ |
| 267 | int |
| 268 | bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, int tag) |
| 269 | { |
| 270 | obj_attribute_list *p; |
| 271 | |
| 272 | if (tag < NUM_KNOWN_OBJ_ATTRIBUTES) |
| 273 | { |
| 274 | /* Known tags are preallocated. */ |
| 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. */ |
| 293 | void |
| 294 | bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, int tag, unsigned int i) |
| 295 | { |
| 296 | obj_attribute *attr; |
| 297 | |
| 298 | attr = elf_new_obj_attr (abfd, vendor, tag); |
| 299 | attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); |
| 300 | attr->i = i; |
| 301 | } |
| 302 | |
| 303 | /* Duplicate an object attribute string value. */ |
| 304 | char * |
| 305 | _bfd_elf_attr_strdup (bfd *abfd, const char * s) |
| 306 | { |
| 307 | char * p; |
| 308 | int len; |
| 309 | |
| 310 | len = strlen (s) + 1; |
| 311 | p = (char *) bfd_alloc (abfd, len); |
| 312 | return (char *) memcpy (p, s, len); |
| 313 | } |
| 314 | |
| 315 | /* Add a string object attribute. */ |
| 316 | void |
| 317 | bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, int tag, const char *s) |
| 318 | { |
| 319 | obj_attribute *attr; |
| 320 | |
| 321 | attr = elf_new_obj_attr (abfd, vendor, tag); |
| 322 | attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); |
| 323 | attr->s = _bfd_elf_attr_strdup (abfd, s); |
| 324 | } |
| 325 | |
| 326 | /* Add a int+string object attribute. */ |
| 327 | void |
| 328 | bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor, int tag, |
| 329 | unsigned int i, const char *s) |
| 330 | { |
| 331 | obj_attribute *attr; |
| 332 | |
| 333 | attr = elf_new_obj_attr (abfd, vendor, tag); |
| 334 | attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); |
| 335 | attr->i = i; |
| 336 | attr->s = _bfd_elf_attr_strdup (abfd, s); |
| 337 | } |
| 338 | |
| 339 | /* Copy the object attributes from IBFD to OBFD. */ |
| 340 | void |
| 341 | _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd) |
| 342 | { |
| 343 | obj_attribute *in_attr; |
| 344 | obj_attribute *out_attr; |
| 345 | obj_attribute_list *list; |
| 346 | int i; |
| 347 | int vendor; |
| 348 | |
| 349 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
| 350 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
| 351 | return; |
| 352 | |
| 353 | for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++) |
| 354 | { |
| 355 | in_attr |
| 356 | = &elf_known_obj_attributes (ibfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE]; |
| 357 | out_attr |
| 358 | = &elf_known_obj_attributes (obfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE]; |
| 359 | for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) |
| 360 | { |
| 361 | out_attr->type = in_attr->type; |
| 362 | out_attr->i = in_attr->i; |
| 363 | if (in_attr->s && *in_attr->s) |
| 364 | out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s); |
| 365 | in_attr++; |
| 366 | out_attr++; |
| 367 | } |
| 368 | |
| 369 | for (list = elf_other_obj_attributes (ibfd)[vendor]; |
| 370 | list; |
| 371 | list = list->next) |
| 372 | { |
| 373 | in_attr = &list->attr; |
| 374 | switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL)) |
| 375 | { |
| 376 | case ATTR_TYPE_FLAG_INT_VAL: |
| 377 | bfd_elf_add_obj_attr_int (obfd, vendor, list->tag, in_attr->i); |
| 378 | break; |
| 379 | case ATTR_TYPE_FLAG_STR_VAL: |
| 380 | bfd_elf_add_obj_attr_string (obfd, vendor, list->tag, |
| 381 | in_attr->s); |
| 382 | break; |
| 383 | case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: |
| 384 | bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag, |
| 385 | in_attr->i, in_attr->s); |
| 386 | break; |
| 387 | default: |
| 388 | abort (); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* Determine whether a GNU object attribute tag takes an integer, a |
| 395 | string or both. */ |
| 396 | static int |
| 397 | gnu_obj_attrs_arg_type (int tag) |
| 398 | { |
| 399 | /* Except for Tag_compatibility, for GNU attributes we follow the |
| 400 | same rule ARM ones > 32 follow: odd-numbered tags take strings |
| 401 | and even-numbered tags take integers. In addition, tag & 2 is |
| 402 | nonzero for architecture-independent tags and zero for |
| 403 | architecture-dependent ones. */ |
| 404 | if (tag == Tag_compatibility) |
| 405 | return 3; |
| 406 | else |
| 407 | return (tag & 1) != 0 ? 2 : 1; |
| 408 | } |
| 409 | |
| 410 | /* Determine what arguments an attribute tag takes. */ |
| 411 | int |
| 412 | _bfd_elf_obj_attrs_arg_type (bfd *abfd, int vendor, int tag) |
| 413 | { |
| 414 | switch (vendor) |
| 415 | { |
| 416 | case OBJ_ATTR_PROC: |
| 417 | return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag); |
| 418 | break; |
| 419 | case OBJ_ATTR_GNU: |
| 420 | return gnu_obj_attrs_arg_type (tag); |
| 421 | break; |
| 422 | default: |
| 423 | abort (); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* Parse an object attributes section. */ |
| 428 | void |
| 429 | _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr) |
| 430 | { |
| 431 | bfd_byte *contents; |
| 432 | bfd_byte *p; |
| 433 | bfd_byte *p_end; |
| 434 | bfd_vma len; |
| 435 | const char *std_sec; |
| 436 | |
| 437 | /* PR 17512: file: 2844a11d. */ |
| 438 | if (hdr->sh_size == 0) |
| 439 | return; |
| 440 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
| 441 | if (!contents) |
| 442 | return; |
| 443 | if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0, |
| 444 | hdr->sh_size)) |
| 445 | { |
| 446 | free (contents); |
| 447 | return; |
| 448 | } |
| 449 | p = contents; |
| 450 | p_end = p + hdr->sh_size; |
| 451 | std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor; |
| 452 | |
| 453 | if (*(p++) == 'A') |
| 454 | { |
| 455 | len = hdr->sh_size - 1; |
| 456 | |
| 457 | while (len > 0 && p < p_end - 4) |
| 458 | { |
| 459 | unsigned namelen; |
| 460 | bfd_vma section_len; |
| 461 | int vendor; |
| 462 | |
| 463 | section_len = bfd_get_32 (abfd, p); |
| 464 | p += 4; |
| 465 | if (section_len == 0) |
| 466 | break; |
| 467 | if (section_len > len) |
| 468 | section_len = len; |
| 469 | len -= section_len; |
| 470 | section_len -= 4; |
| 471 | namelen = strnlen ((char *) p, section_len) + 1; |
| 472 | if (namelen == 0 || namelen >= section_len) |
| 473 | break; |
| 474 | section_len -= namelen; |
| 475 | if (std_sec && strcmp ((char *) p, std_sec) == 0) |
| 476 | vendor = OBJ_ATTR_PROC; |
| 477 | else if (strcmp ((char *) p, "gnu") == 0) |
| 478 | vendor = OBJ_ATTR_GNU; |
| 479 | else |
| 480 | { |
| 481 | /* Other vendor section. Ignore it. */ |
| 482 | p += namelen + section_len; |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | p += namelen; |
| 487 | while (section_len > 0 && p < p_end) |
| 488 | { |
| 489 | int tag; |
| 490 | unsigned int n; |
| 491 | unsigned int val; |
| 492 | bfd_vma subsection_len; |
| 493 | bfd_byte *end; |
| 494 | |
| 495 | tag = safe_read_leb128 (abfd, p, &n, FALSE, p_end); |
| 496 | p += n; |
| 497 | if (p < p_end - 4) |
| 498 | subsection_len = bfd_get_32 (abfd, p); |
| 499 | else |
| 500 | subsection_len = 0; |
| 501 | p += 4; |
| 502 | if (subsection_len == 0) |
| 503 | break; |
| 504 | if (subsection_len > section_len) |
| 505 | subsection_len = section_len; |
| 506 | section_len -= subsection_len; |
| 507 | subsection_len -= n + 4; |
| 508 | end = p + subsection_len; |
| 509 | /* PR 17512: file: 0e8c0c90. */ |
| 510 | if (end > p_end) |
| 511 | end = p_end; |
| 512 | switch (tag) |
| 513 | { |
| 514 | case Tag_File: |
| 515 | while (p < end) |
| 516 | { |
| 517 | int type; |
| 518 | |
| 519 | tag = safe_read_leb128 (abfd, p, &n, FALSE, end); |
| 520 | p += n; |
| 521 | type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); |
| 522 | switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL)) |
| 523 | { |
| 524 | case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: |
| 525 | val = safe_read_leb128 (abfd, p, &n, FALSE, end); |
| 526 | p += n; |
| 527 | bfd_elf_add_obj_attr_int_string (abfd, vendor, tag, |
| 528 | val, (char *) p); |
| 529 | p += strlen ((char *)p) + 1; |
| 530 | break; |
| 531 | case ATTR_TYPE_FLAG_STR_VAL: |
| 532 | bfd_elf_add_obj_attr_string (abfd, vendor, tag, |
| 533 | (char *) p); |
| 534 | p += strlen ((char *)p) + 1; |
| 535 | break; |
| 536 | case ATTR_TYPE_FLAG_INT_VAL: |
| 537 | val = safe_read_leb128 (abfd, p, &n, FALSE, end); |
| 538 | p += n; |
| 539 | bfd_elf_add_obj_attr_int (abfd, vendor, tag, val); |
| 540 | break; |
| 541 | default: |
| 542 | abort (); |
| 543 | } |
| 544 | } |
| 545 | break; |
| 546 | case Tag_Section: |
| 547 | case Tag_Symbol: |
| 548 | /* Don't have anywhere convenient to attach these. |
| 549 | Fall through for now. */ |
| 550 | default: |
| 551 | /* Ignore things we don't kow about. */ |
| 552 | p += subsection_len; |
| 553 | subsection_len = 0; |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | free (contents); |
| 560 | } |
| 561 | |
| 562 | /* Merge common object attributes from IBFD into OBFD. Raise an error |
| 563 | if there are conflicting attributes. Any processor-specific |
| 564 | attributes have already been merged. This must be called from the |
| 565 | bfd_elfNN_bfd_merge_private_bfd_data hook for each individual |
| 566 | target, along with any target-specific merging. Because there are |
| 567 | no common attributes other than Tag_compatibility at present, and |
| 568 | non-"gnu" Tag_compatibility is not expected in "gnu" sections, this |
| 569 | is not presently called for targets without their own |
| 570 | attributes. */ |
| 571 | |
| 572 | bfd_boolean |
| 573 | _bfd_elf_merge_object_attributes (bfd *ibfd, bfd *obfd) |
| 574 | { |
| 575 | obj_attribute *in_attr; |
| 576 | obj_attribute *out_attr; |
| 577 | int vendor; |
| 578 | |
| 579 | /* The only common attribute is currently Tag_compatibility, |
| 580 | accepted in both processor and "gnu" sections. */ |
| 581 | for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++) |
| 582 | { |
| 583 | /* Handle Tag_compatibility. The tags are only compatible if the flags |
| 584 | are identical and, if the flags are '1', the strings are identical. |
| 585 | If the flags are non-zero, then we can only use the string "gnu". */ |
| 586 | in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility]; |
| 587 | out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility]; |
| 588 | |
| 589 | if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0) |
| 590 | { |
| 591 | _bfd_error_handler |
| 592 | (_("error: %B: Object has vendor-specific contents that " |
| 593 | "must be processed by the '%s' toolchain"), |
| 594 | ibfd, in_attr->s); |
| 595 | return FALSE; |
| 596 | } |
| 597 | |
| 598 | if (in_attr->i != out_attr->i |
| 599 | || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0)) |
| 600 | { |
| 601 | _bfd_error_handler (_("error: %B: Object tag '%d, %s' is " |
| 602 | "incompatible with tag '%d, %s'"), |
| 603 | ibfd, |
| 604 | in_attr->i, in_attr->s ? in_attr->s : "", |
| 605 | out_attr->i, out_attr->s ? out_attr->s : ""); |
| 606 | return FALSE; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | return TRUE; |
| 611 | } |
| 612 | |
| 613 | /* Merge an unknown processor-specific attribute TAG, within the range |
| 614 | of known attributes, from IBFD into OBFD; return TRUE if the link |
| 615 | is OK, FALSE if it must fail. */ |
| 616 | |
| 617 | bfd_boolean |
| 618 | _bfd_elf_merge_unknown_attribute_low (bfd *ibfd, bfd *obfd, int tag) |
| 619 | { |
| 620 | obj_attribute *in_attr; |
| 621 | obj_attribute *out_attr; |
| 622 | bfd *err_bfd = NULL; |
| 623 | bfd_boolean result = TRUE; |
| 624 | |
| 625 | in_attr = elf_known_obj_attributes_proc (ibfd); |
| 626 | out_attr = elf_known_obj_attributes_proc (obfd); |
| 627 | |
| 628 | if (out_attr[tag].i != 0 || out_attr[tag].s != NULL) |
| 629 | err_bfd = obfd; |
| 630 | else if (in_attr[tag].i != 0 || in_attr[tag].s != NULL) |
| 631 | err_bfd = ibfd; |
| 632 | |
| 633 | if (err_bfd != NULL) |
| 634 | result |
| 635 | = get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag); |
| 636 | |
| 637 | /* Only pass on attributes that match in both inputs. */ |
| 638 | if (in_attr[tag].i != out_attr[tag].i |
| 639 | || (in_attr[tag].s == NULL) != (out_attr[tag].s == NULL) |
| 640 | || (in_attr[tag].s != NULL && out_attr[tag].s != NULL |
| 641 | && strcmp (in_attr[tag].s, out_attr[tag].s) != 0)) |
| 642 | { |
| 643 | out_attr[tag].i = 0; |
| 644 | out_attr[tag].s = NULL; |
| 645 | } |
| 646 | |
| 647 | return result; |
| 648 | } |
| 649 | |
| 650 | /* Merge the lists of unknown processor-specific attributes, outside |
| 651 | the known range, from IBFD into OBFD; return TRUE if the link is |
| 652 | OK, FALSE if it must fail. */ |
| 653 | |
| 654 | bfd_boolean |
| 655 | _bfd_elf_merge_unknown_attribute_list (bfd *ibfd, bfd *obfd) |
| 656 | { |
| 657 | obj_attribute_list *in_list; |
| 658 | obj_attribute_list *out_list; |
| 659 | obj_attribute_list **out_listp; |
| 660 | bfd_boolean result = TRUE; |
| 661 | |
| 662 | in_list = elf_other_obj_attributes_proc (ibfd); |
| 663 | out_listp = &elf_other_obj_attributes_proc (obfd); |
| 664 | out_list = *out_listp; |
| 665 | |
| 666 | for (; in_list || out_list; ) |
| 667 | { |
| 668 | bfd *err_bfd = NULL; |
| 669 | int err_tag = 0; |
| 670 | |
| 671 | /* The tags for each list are in numerical order. */ |
| 672 | /* If the tags are equal, then merge. */ |
| 673 | if (out_list && (!in_list || in_list->tag > out_list->tag)) |
| 674 | { |
| 675 | /* This attribute only exists in obfd. We can't merge, and we don't |
| 676 | know what the tag means, so delete it. */ |
| 677 | err_bfd = obfd; |
| 678 | err_tag = out_list->tag; |
| 679 | *out_listp = out_list->next; |
| 680 | out_list = *out_listp; |
| 681 | } |
| 682 | else if (in_list && (!out_list || in_list->tag < out_list->tag)) |
| 683 | { |
| 684 | /* This attribute only exists in ibfd. We can't merge, and we don't |
| 685 | know what the tag means, so ignore it. */ |
| 686 | err_bfd = ibfd; |
| 687 | err_tag = in_list->tag; |
| 688 | in_list = in_list->next; |
| 689 | } |
| 690 | else /* The tags are equal. */ |
| 691 | { |
| 692 | /* As present, all attributes in the list are unknown, and |
| 693 | therefore can't be merged meaningfully. */ |
| 694 | err_bfd = obfd; |
| 695 | err_tag = out_list->tag; |
| 696 | |
| 697 | /* Only pass on attributes that match in both inputs. */ |
| 698 | if (in_list->attr.i != out_list->attr.i |
| 699 | || (in_list->attr.s == NULL) != (out_list->attr.s == NULL) |
| 700 | || (in_list->attr.s && out_list->attr.s |
| 701 | && strcmp (in_list->attr.s, out_list->attr.s) != 0)) |
| 702 | { |
| 703 | /* No match. Delete the attribute. */ |
| 704 | *out_listp = out_list->next; |
| 705 | out_list = *out_listp; |
| 706 | } |
| 707 | else |
| 708 | { |
| 709 | /* Matched. Keep the attribute and move to the next. */ |
| 710 | out_list = out_list->next; |
| 711 | in_list = in_list->next; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (err_bfd) |
| 716 | result = result |
| 717 | && get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, |
| 718 | err_tag); |
| 719 | } |
| 720 | |
| 721 | return result; |
| 722 | } |