1 /* ELF attributes support (based on ARM EABI attributes). 2 Copyright (C) 2005-2026 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 /* Design note regarding the merge of Object Attributes v2 during linkage 22 23 Entry point: _bfd_elf_link_setup_object_attributes 24 25 The linker is an "advanced" consumer of OAv2. After parsing, it deduplicates 26 them, merges them, detects any compatibility issues, and finally translates 27 them to GNU properties. 28 29 ** Overall design 30 31 The OAv2 processing pipeline follows a map-reduce pattern. Obviously, the 32 actual processing in GNU ld is not multi-threaded, and the operations are not 33 necessarily executed directly one after another. 34 35 * Phase 1, map: successive per-file operations applied on the list of 36 compatible input objects. 37 1. Parsing of the OAv2 section's data (also used by objcopy). 38 2. Translation of relevant GNU properties to OAv2. This is required for the 39 backward-compatibility with input objects only marked using GNU 40 properties. 41 3. Sorting of the subsections and object attributes. Further operations 42 rely on the ordering to perform some optimization in the processing of 43 the data. 44 4. Deduplication of subsections and object attributes, and detection of any 45 conflict between duplicated subsections or tags. 46 5. Translation of relevant OAv2 to GNU properties for a forward 47 -compatibility with the GNU properties merge. 48 6. Marking of unknown subsections to skip them during the merge (in 49 phase 2), and to prune them before the output object's serialization 50 (in phase 3). 51 52 * Phase 2, reduce: OAv2 in input objects are merged together. 53 1. Gathering of "frozen" values (=coming from the command-line arguments) 54 into a virtual read-only list of subsections and attributes. 55 2. Merging of OAv2 from an input file and the frozen input. 56 3. Merging of the results of step 2 together. Since the OAv2 merge is 57 commutative and associative, it can be implemented as a reduce. 58 However, GNU ld implements it as an accumulate because it does not 59 support multithreading. 60 Notes: the two merge phases also perform a marking of unsupported / invalid 61 subsections and attributes. This marking can be used for debugging, and 62 also more practically to drop unsupported optional subsections from the 63 output. 64 65 * Phase 3, finalization of the output. 66 1. Pruning of the unknown / unsupported / invalid subsections and 67 attributes. 68 2. Serialization of OAv2 data (also used by objcopy). 69 Notes: 70 - There is no translation of the merged OAv2 to GNU properties at this 71 stage, as the GNU properties merge process has already all the needed 72 information (translated in step 5 of stage 1) to produce the GNU 73 properties. 74 - The GNU properties are currently required as the runtime linker does 75 not understand OAv2 yet. 76 - Phase 3 should also include a compatibility check between the final 77 merge result of the current link unit and input shared objects. I opted 78 for postponing this compatibility check, and GNU properties merge will 79 take care of it as it already does. 80 81 The Object Ottributes merge process must handle both optional and required 82 subsections. It also treats the first merge of the frozen set specially, as 83 the OAv2 list in the input BFD serves as the accumulator for subsequent 84 merges. 85 86 ** Optional subsections 87 88 Optional subsections are processed as if merging two ordered sets by 89 iterating linearly through both, checking whether an element of a given 90 ordinality is present in the opposite set, and adding it to the accumulator. 91 The added diffuculty with subsections and attributes lies in the fact that 92 missing elements have default values, and these must be merged with existing 93 ones to produce the final value to be stored. 94 95 ** Required subsections 96 97 Required subsections are processed slightly differently from the optional 98 subsections, as they cannot be pruned since they are mandatory, hence an 99 error will be raised by the linker if it is not recognized. 100 101 For now, the subsection for PAuth ABI is the only one use case, and no merge 102 is applied on the values. The values simply need to match. 103 This implementation choice might be challenged in the future if required 104 subsections can have the same diversity as optional subsections. If the case 105 arises, the refactoring to handle this new behavior should consist in adding 106 a new merge policy MERGE-EQUAL, or something similar. Some "if required" 107 should be added in the optional subsections merge logic to error on any 108 missing elements, or mismatch, and messages should also be rephrased to point 109 out that the error is for a required subsection. 110 111 ** Important note regarding support for testing 112 113 In order to test this generic logic, AArch64's use cases are not offering 114 enough coverage, so a "GNU testing namespace" which corresponds to the name 115 of the subsection was introduced. It follows the following pattern: 116 gnu_testing_<XXXXXX>_MERGE_<POLICY> 117 with: 118 - <XXXXXX>: an arbitrary name for your testing subsection. 119 - <POLICY>: the name of the merging policy to apply on the values in the 120 subsection. The currently supported merge policy are: 121 * _MERGE_AND: bitwise AND applied on numerical values. 122 * _MERGE_OR: bitwise OR applied on numerical values. 123 * _MERGE_ADD: concatenates strings together with a '+' in-between. 124 Note: "_MERGE_ADD" does not make really sense, and will very likely never 125 be used for a real merge. Its only purpose is to test the correct 126 handling of merges with strings. 127 Any subsection name matching neither names supported by the backend, nor 128 following the pattern corresponding GNU testing namespace will be considered 129 unknown and its status set to obj_attr_subsection_v2_unknown. This will have 130 for consequence the pruning of this subsection. 131 132 Additionally, the first two tags in gnu_testing namespace, GNUTestTag_0 and 133 GNUTestTag_1, are known, and so have a name and can be initialized to the 134 default value ('0' or NULL) depending on the encoding specified on the 135 subsection. Any tags above 1 will be considered unknown, so will be default 136 -initialized in the same way but its status will be set to obj_attr_v2_unknown. 137 This behavior of the testing tags allows to test the pruning of unknown 138 attributes. */ 139 140 #include "sysdep.h" 141 #include "bfd.h" 142 #include "doubly-linked-list.h" 143 #include "libiberty.h" 144 #include "libbfd.h" 145 #include "elf-bfd.h" 146 147 /* Decode the encoded version number corresponding to the Object Attribute 148 version. Return the version on success, UNSUPPORTED on failure. */ 149 obj_attr_version_t 150 _bfd_obj_attrs_version_dec (uint8_t encoded_version) 151 { 152 if (encoded_version == 'A') 153 return OBJ_ATTR_V1; 154 return OBJ_ATTR_VERSION_UNSUPPORTED; 155 } 156 157 /* Encode the Object Attribute version into a byte. */ 158 uint8_t 159 _bfd_obj_attrs_version_enc (obj_attr_version_t version) 160 { 161 if (version == OBJ_ATTR_V1) 162 return 'A'; 163 abort (); 164 } 165 166 /* Return the number of bytes needed by I in uleb128 format. */ 167 static uint32_t 168 uleb128_size (uint32_t i) 169 { 170 uint32_t size = 1; 171 while (i >= 0x80) 172 { 173 i >>= 7; 174 size++; 175 } 176 return size; 177 } 178 179 /* Return TRUE if the attribute has the default value (0/""). */ 180 static bool 181 is_default_attr (obj_attribute *attr) 182 { 183 if (ATTR_TYPE_HAS_ERROR (attr->type)) 184 return true; 185 if (ATTR_TYPE_HAS_INT_VAL (attr->type) && attr->i != 0) 186 return false; 187 if (ATTR_TYPE_HAS_STR_VAL (attr->type) && attr->s && *attr->s) 188 return false; 189 if (ATTR_TYPE_HAS_NO_DEFAULT (attr->type)) 190 return false; 191 192 return true; 193 } 194 195 /* Return the vendor name for a given object attributes section. */ 196 static const char * 197 obj_attr_v1_vendor_name (bfd *abfd, int vendor) 198 { 199 return (vendor == OBJ_ATTR_PROC 200 ? get_elf_backend_data (abfd)->obj_attrs_vendor 201 : "gnu"); 202 } 203 204 /* Return the size of a single attribute. */ 205 static bfd_vma 206 obj_attr_v1_size (unsigned int tag, obj_attribute *attr) 207 { 208 bfd_vma size; 209 210 if (is_default_attr (attr)) 211 return 0; 212 213 size = uleb128_size (tag); 214 if (ATTR_TYPE_HAS_INT_VAL (attr->type)) 215 size += uleb128_size (attr->i); 216 if (ATTR_TYPE_HAS_STR_VAL (attr->type)) 217 size += strlen ((char *)attr->s) + 1; 218 return size; 219 } 220 221 /* Return the size of the object attributes section for VENDOR 222 (OBJ_ATTR_PROC or OBJ_ATTR_GNU), or 0 if there are no attributes 223 for that vendor to record and the vendor is OBJ_ATTR_GNU. */ 224 static bfd_vma 225 vendor_obj_attrs_v1_size (bfd *abfd, int vendor) 226 { 227 bfd_vma size; 228 obj_attribute *attr; 229 obj_attribute_list *list; 230 int i; 231 const char *vendor_name = obj_attr_v1_vendor_name (abfd, vendor); 232 233 if (!vendor_name) 234 return 0; 235 236 attr = elf_known_obj_attributes (abfd)[vendor]; 237 size = 0; 238 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) 239 size += obj_attr_v1_size (i, &attr[i]); 240 241 for (list = elf_other_obj_attributes (abfd)[vendor]; 242 list; 243 list = list->next) 244 size += obj_attr_v1_size (list->tag, &list->attr); 245 246 /* <size> <vendor_name> NUL 0x1 <size> */ 247 return (size 248 ? size + 10 + strlen (vendor_name) 249 : 0); 250 } 251 252 static bfd_vma 253 oav1_section_size (bfd *abfd) 254 { 255 bfd_vma size = 0; 256 size = vendor_obj_attrs_v1_size (abfd, OBJ_ATTR_PROC); 257 size += vendor_obj_attrs_v1_size (abfd, OBJ_ATTR_GNU); 258 if (size > 0) 259 size += sizeof (uint8_t); /* <format-version: uint8> */ 260 return size; 261 } 262 263 /* Return the size of a single attribute. */ 264 static bfd_vma 265 oav2_attr_size (const obj_attr_v2_t *attr, obj_attr_encoding_v2_t type) 266 { 267 bfd_vma size = uleb128_size (attr->tag); 268 switch (type) 269 { 270 case OA_ENC_ULEB128: 271 size += uleb128_size (attr->val.uint); 272 break; 273 case OA_ENC_NTBS: 274 size += strlen (attr->val.string) + 1; /* +1 for '\0'. */ 275 break; 276 default: 277 abort (); 278 } 279 return size; 280 } 281 282 /* Return the size of a subsection. */ 283 static bfd_vma 284 oav2_subsection_size (const obj_attr_subsection_v2_t *subsec) 285 { 286 bfd_vma size = sizeof (uint32_t); /* <subsection-length: uint32> */ 287 size += strlen (subsec->name) + 1; /* <subsection-name: NTBS> so +1 for '\0'. */ 288 size += 2 * sizeof (uint8_t); /* <optional: uint8> <encoding: uint8> */ 289 /* <attribute>* */ 290 for (const obj_attr_v2_t *attr = subsec->first; 291 attr != NULL; 292 attr = attr->next) 293 size += oav2_attr_size (attr, subsec->encoding); 294 return size; 295 } 296 297 /* Return the size of a object attributes section. */ 298 static bfd_vma 299 oav2_section_size (bfd *abfd) 300 { 301 const obj_attr_subsection_v2_t *subsec 302 = elf_obj_attr_subsections (abfd).first; 303 if (subsec == NULL) 304 return 0; 305 306 bfd_vma size = sizeof (uint8_t); /* <format-version: uint8> */ 307 for (; subsec != NULL; subsec = subsec->next) 308 size += oav2_subsection_size (subsec); 309 return size; 310 } 311 312 /* Return the size of the object attributes section. */ 313 bfd_vma 314 bfd_elf_obj_attr_size (bfd *abfd) 315 { 316 obj_attr_version_t version = elf_obj_attr_version (abfd); 317 switch (version) 318 { 319 case OBJ_ATTR_V1: 320 return oav1_section_size (abfd); 321 case OBJ_ATTR_V2: 322 return oav2_section_size (abfd); 323 case OBJ_ATTR_VERSION_NONE: 324 return 0; 325 default: 326 abort (); 327 } 328 } 329 330 /* Write VAL in uleb128 format to P, returning a pointer to the 331 following byte. */ 332 static bfd_byte * 333 write_uleb128 (bfd_byte *p, uint32_t val) 334 { 335 bfd_byte c; 336 do 337 { 338 c = val & 0x7f; 339 val >>= 7; 340 if (val) 341 c |= 0x80; 342 *(p++) = c; 343 } 344 while (val); 345 return p; 346 } 347 348 /* Write attribute ATTR to butter P, and return a pointer to the following 349 byte. */ 350 static bfd_byte * 351 write_obj_attr_v1 (bfd_byte *p, unsigned int tag, obj_attribute *attr) 352 { 353 /* Suppress default entries. */ 354 if (is_default_attr (attr)) 355 return p; 356 357 p = write_uleb128 (p, tag); 358 if (ATTR_TYPE_HAS_INT_VAL (attr->type)) 359 p = write_uleb128 (p, attr->i); 360 if (ATTR_TYPE_HAS_STR_VAL (attr->type)) 361 { 362 int len; 363 364 len = strlen (attr->s) + 1; 365 memcpy (p, attr->s, len); 366 p += len; 367 } 368 369 return p; 370 } 371 372 /* Write the contents of the object attributes section (length SIZE) 373 for VENDOR to CONTENTS. */ 374 static void 375 write_vendor_obj_attrs_v1 (bfd *abfd, bfd_byte *contents, bfd_vma size, 376 int vendor) 377 { 378 bfd_byte *p; 379 obj_attribute *attr; 380 obj_attribute_list *list; 381 int i; 382 const char *vendor_name = obj_attr_v1_vendor_name (abfd, vendor); 383 size_t vendor_length = strlen (vendor_name) + 1; 384 385 p = contents; 386 bfd_put_32 (abfd, size, p); 387 p += 4; 388 memcpy (p, vendor_name, vendor_length); 389 p += vendor_length; 390 *(p++) = Tag_File; 391 bfd_put_32 (abfd, size - 4 - vendor_length, p); 392 p += 4; 393 394 attr = elf_known_obj_attributes (abfd)[vendor]; 395 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) 396 { 397 unsigned int tag = i; 398 if (get_elf_backend_data (abfd)->obj_attrs_order) 399 tag = get_elf_backend_data (abfd)->obj_attrs_order (i); 400 p = write_obj_attr_v1 (p, tag, &attr[tag]); 401 } 402 403 for (list = elf_other_obj_attributes (abfd)[vendor]; 404 list; 405 list = list->next) 406 p = write_obj_attr_v1 (p, list->tag, &list->attr); 407 } 408 409 static void 410 oav1_write_section (bfd *abfd, bfd_byte *buffer, bfd_vma size) 411 { 412 /* This function should only be called for object attributes version 1. */ 413 BFD_ASSERT (elf_obj_attr_version (abfd) == OBJ_ATTR_V1); 414 415 bfd_byte *p = buffer; 416 417 const struct elf_backend_data *be = get_elf_backend_data (abfd); 418 /* <format-version: uint8> */ 419 *(p++) = be->obj_attrs_version_enc (elf_obj_attr_version (abfd)); 420 421 for (int vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; ++vendor) 422 { 423 bfd_vma vendor_size = vendor_obj_attrs_v1_size (abfd, vendor); 424 if (vendor_size > 0) 425 write_vendor_obj_attrs_v1 (abfd, p, vendor_size, vendor); 426 p += vendor_size; 427 } 428 429 /* We didn't overrun the buffer. */ 430 BFD_ASSERT (p <= buffer + size); 431 } 432 433 static bfd_byte * 434 oav2_write_attr (bfd_byte *p, 435 const obj_attr_v2_t *attr, 436 obj_attr_encoding_v2_t type) 437 { 438 p = write_uleb128 (p, attr->tag); 439 switch (type) 440 { 441 case OA_ENC_ULEB128: 442 p = write_uleb128 (p, attr->val.uint); 443 break; 444 case OA_ENC_NTBS: 445 /* +1 for '\0'. */ 446 p = (bfd_byte *) stpcpy ((char *) p, attr->val.string) + 1; 447 break; 448 default: 449 abort (); 450 } 451 return p; 452 } 453 454 static bfd_byte * 455 oav2_write_subsection (bfd *abfd, 456 const obj_attr_subsection_v2_t *subsec, 457 bfd_byte *p) 458 { 459 /* <subsection-length: uint32> */ 460 bfd_vma subsec_size = oav2_subsection_size (subsec); 461 bfd_put_32 (abfd, subsec_size, p); 462 p += sizeof (uint32_t); 463 464 /* <vendor-name: NTBS> */ 465 size_t vendor_name_size = strlen (subsec->name) + 1; /* +1 for '\0'. */ 466 memcpy (p, subsec->name, vendor_name_size); 467 p += vendor_name_size; 468 469 /* -- <vendor-data: bytes> -- */ 470 /* <optional: uint8> */ 471 bfd_put_8 (abfd, subsec->optional, p); 472 ++p; 473 /* <encoding: uint8> */ 474 bfd_put_8 (abfd, obj_attr_encoding_v2_to_u8 (subsec->encoding), p); 475 ++p; 476 /* <attribute>* */ 477 for (const obj_attr_v2_t *attr = subsec->first; 478 attr != NULL; 479 attr = attr->next) 480 p = oav2_write_attr (p, attr, subsec->encoding); 481 return p; 482 } 483 484 static bfd_vma 485 oav2_write_section (bfd *abfd, bfd_byte *buffer, bfd_vma size) 486 { 487 /* This function should only be called for object attributes version 2. */ 488 BFD_ASSERT (elf_obj_attr_version (abfd) == OBJ_ATTR_V2); 489 490 bfd_vma section_size = oav2_section_size (abfd); 491 if (section_size == 0) 492 return 0; 493 494 bfd_byte *p = buffer; 495 496 const struct elf_backend_data *be = get_elf_backend_data (abfd); 497 /* <format-version: uint8> */ 498 *(p++) = be->obj_attrs_version_enc (elf_obj_attr_version (abfd)); 499 500 /* [ <subsection-length: uint32> <vendor-name: NTBS> <vendor-data: bytes> ]* */ 501 for (const obj_attr_subsection_v2_t *subsec 502 = elf_obj_attr_subsections (abfd).first; 503 subsec != NULL; 504 subsec = subsec->next) 505 p = oav2_write_subsection (abfd, subsec, p); 506 507 /* We didn't overrun the buffer. */ 508 BFD_ASSERT (p <= buffer + size); 509 /* We wrote as many data as it was computed by 510 vendor_section_obj_attr_using_subsections_size(). */ 511 BFD_ASSERT (section_size == (bfd_vma) (p - buffer)); 512 513 return section_size; 514 } 515 516 static void 517 oav2_sort_subsections (obj_attr_subsection_list_t *plist) 518 { 519 for (obj_attr_subsection_v2_t *subsec = plist->first; 520 subsec != NULL; 521 subsec = subsec->next) 522 LINKED_LIST_MERGE_SORT (obj_attr_v2_t) (subsec, _bfd_elf_obj_attr_v2_cmp); 523 524 LINKED_LIST_MERGE_SORT (obj_attr_subsection_v2_t) 525 (plist, _bfd_elf_obj_attr_subsection_v2_cmp); 526 } 527 528 /* Write the contents of the object attributes section to CONTENTS. */ 529 void 530 bfd_elf_set_obj_attr_contents (bfd *abfd, bfd_byte *buffer, bfd_vma size) 531 { 532 if (! abfd->is_linker_output 533 && elf_obj_attr_version (abfd) == OBJ_ATTR_V2) 534 /* Before dumping the data, sort subsections in alphabetical order, and 535 attributes according to their tag in numerical order. This is useful 536 for diagnostic tools so that they dump the same output even if the 537 subsections or their attributes were not declared in the same order in 538 different files. 539 This sorting is only performed in the case of the assembler. In the 540 case of the linker, the subsections and attributes are already sorted 541 by the merge process. */ 542 oav2_sort_subsections (&elf_obj_attr_subsections (abfd)); 543 544 obj_attr_version_t version = elf_obj_attr_version (abfd); 545 switch (version) 546 { 547 case OBJ_ATTR_V1: 548 oav1_write_section (abfd, buffer, size); 549 break; 550 case OBJ_ATTR_V2: 551 oav2_write_section (abfd, buffer, size); 552 break; 553 default: 554 abort (); 555 } 556 } 557 558 /* The first two tags in gnu-testing namespace are known (from the perspective 559 of GNU ld), and so have a name and can be initialized to the default value 560 ('0' or NULL) depending on the encoding specified on the subsection. Any 561 tags above 1 will be considered unknown, so will be default initialized in 562 the same way but its status will be set to obj_attr_subsection_v2_unknown. 563 If the tag is unknown, ld can drop it if it is inside an optional subsection, 564 whereas ld will raise an error in a required subsection. 565 Note: the array below has to be sorted by the tag's integer value. */ 566 static const obj_attr_info_t known_attrs_gnu_testing[] = 567 { 568 { .tag = {"GNUTestTag_0", 0} }, 569 { .tag = {"GNUTestTag_1", 1} }, 570 }; 571 572 /* List of known GNU subsections. 573 Note: this array has to be sorted using the same criteria as in 574 _bfd_elf_obj_attr_subsection_v2_cmp(). */ 575 static const known_subsection_v2_t obj_attr_v2_known_gnu_subsections[] = 576 { 577 { 578 /* Note: the currently set values for the subsection name, its optionality, 579 and encoding are irrelevant for a testing subsection. These values are 580 unused. This entry is only a placeholder for list of known GNU testing 581 tags. */ 582 .subsec_name = NULL, 583 .known_attrs = known_attrs_gnu_testing, 584 .optional = true, 585 .encoding = OA_ENC_ULEB128, 586 .len = ARRAY_SIZE (known_attrs_gnu_testing), 587 }, 588 /* Note for the future: GNU subsections can be added here below. */ 589 }; 590 591 /* Return True if the given subsection name is part of the reserved testing 592 namespace, i.e. SUBSEC_NAME begins with "gnu-testing". */ 593 static bool 594 gnu_testing_namespace (const char *subsec_name) 595 { 596 return strncmp ("gnu_testing_", subsec_name, 12) == 0; 597 } 598 599 /* Identify the scope of a subsection from its name. 600 Note: the code below needs to be kept in sync with the code of 601 elf_parse_attrs_subsection_v2() in binutils/readelf.c. */ 602 obj_attr_subsection_scope_v2_t 603 bfd_elf_obj_attr_subsection_v2_scope (const bfd *abfd, const char *subsec_name) 604 { 605 const char *vendor_name = get_elf_backend_data (abfd)->obj_attrs_vendor; 606 obj_attr_subsection_scope_v2_t scope = OA_SUBSEC_PRIVATE; 607 size_t vendor_name_len = strlen (vendor_name); 608 if ((strncmp (subsec_name, vendor_name, vendor_name_len) == 0 609 && subsec_name[vendor_name_len] == '_') 610 || (strncmp (subsec_name, "gnu_", 4) == 0 611 && !gnu_testing_namespace (subsec_name))) 612 scope = OA_SUBSEC_PUBLIC; 613 return scope; 614 } 615 616 /* Search for a subsection matching NAME in the list of subsections known from 617 bfd (generic or backend-specific). Return the subsection information if it 618 is found, or NULL otherwise. */ 619 const known_subsection_v2_t * 620 bfd_obj_attr_v2_identify_subsection (const struct elf_backend_data *bed, 621 const char *name) 622 { 623 /* Check known backend subsections. */ 624 const known_subsection_v2_t *known_subsections 625 = bed->obj_attr_v2_known_subsections; 626 const size_t known_subsections_size = bed->obj_attr_v2_known_subsections_size; 627 628 for (unsigned i = 0; i < known_subsections_size; ++i) 629 { 630 int cmp = strcmp (known_subsections[i].subsec_name, name); 631 if (cmp == 0) 632 return &known_subsections[i]; 633 else if (cmp > 0) 634 break; 635 } 636 637 /* Check known GNU subsections. */ 638 /* Note for the future: search known GNU subsections here. Don't forget to 639 skip the first entry (placeholder for GNU testing subsection). */ 640 641 /* Check whether this subsection is a GNU testing subsection. */ 642 if (gnu_testing_namespace (name)) 643 return &obj_attr_v2_known_gnu_subsections[0]; 644 645 return NULL; 646 } 647 648 /* Search for the attribute information associated to TAG in the list of known 649 tags registered in the known subsection SUBSEC. Return the tag information 650 if it is found, NULL otherwise. */ 651 static const obj_attr_info_t * 652 identify_tag (const known_subsection_v2_t *subsec, obj_attr_tag_t tag) 653 { 654 for (unsigned i = 0; i < subsec->len; ++i) 655 { 656 const obj_attr_info_t *known_attr = &subsec->known_attrs[i]; 657 if (known_attr->tag.value == tag) 658 return known_attr; 659 else if (known_attr->tag.value > tag) 660 break; 661 } 662 return NULL; 663 } 664 665 /* Return the attribute information associated to the pair SUBSEC, TAG if it 666 exists, NULL otherwise. */ 667 const obj_attr_info_t * 668 _bfd_obj_attr_v2_find_known_by_tag (const struct elf_backend_data *bed, 669 const char *subsec_name, 670 obj_attr_tag_t tag) 671 { 672 const known_subsection_v2_t *subsec_info 673 = bfd_obj_attr_v2_identify_subsection (bed, subsec_name); 674 if (subsec_info != NULL) 675 return identify_tag (subsec_info, tag); 676 return NULL; 677 } 678 679 /* To-string function for the pair <SUBSEC, TAG>. 680 Returns the attribute information associated to TAG if it is found, 681 or "Tag_unknown_<N>" otherwise. */ 682 const char * 683 _bfd_obj_attr_v2_tag_to_string (const struct elf_backend_data *bed, 684 const char *subsec_name, 685 obj_attr_tag_t tag) 686 { 687 const obj_attr_info_t *attr_info 688 = _bfd_obj_attr_v2_find_known_by_tag (bed, subsec_name, tag); 689 if (attr_info != NULL) 690 return xstrdup (attr_info->tag.name); 691 return xasprintf ("Tag_unknown_%"PRIu64, tag); 692 } 693 694 /* To-string function for the subsection parameter "comprehension". */ 695 const char * 696 bfd_oav2_comprehension_to_string (bool comprehension) 697 { 698 return comprehension ? "optional" : "required"; 699 } 700 701 /* To-string function for the subsection parameter "encoding". */ 702 const char * 703 bfd_oav2_encoding_to_string (obj_attr_encoding_v2_t encoding) 704 { 705 return (encoding == OA_ENC_ULEB128) ? "ULEB128" : "NTBS"; 706 } 707 708 /* Return True if the given BFD is an ELF object with the target backend 709 machine code, non-dynamic (i.e. not a shared library), non-executable, not a 710 plugin or created by the linker. False otherwise. */ 711 static bool 712 oav2_relevant_elf_object (const struct bfd_link_info *info, 713 const bfd *const abfd) 714 { 715 const struct elf_backend_data *output_bed 716 = get_elf_backend_data (info->output_bfd); 717 unsigned int elfclass = output_bed->s->elfclass; 718 int elf_machine_code = output_bed->elf_machine_code; 719 return ((abfd->flags & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) == 0 720 && bfd_get_flavour (abfd) == bfd_target_elf_flavour 721 && elf_machine_code == get_elf_backend_data (abfd)->elf_machine_code 722 && elfclass == get_elf_backend_data (abfd)->s->elfclass); 723 } 724 725 /* Structure storing the result of a search in the list of input BFDs. */ 726 typedef struct 727 { 728 /* A pointer to a BFD. This BFD can either point to a file having 729 object attributes, or a candidate file which does not have any. */ 730 bfd *pbfd; 731 732 /* A boolean indicating whether the file actually contains object 733 attributes. */ 734 bool has_object_attributes; 735 736 /* A pointer to the section containing the object attributes, if any 737 were found. */ 738 asection *sec; 739 } bfd_search_result_t; 740 741 /* Checks whether a BFD contains object attributes, and if so search for the 742 relevant section storing them. The name and type of the section have to 743 match with what the backend expects, i.e. elf_backend_obj_attrs_section and 744 elf_backend_obj_attrs_section_type, otherwise the object attributes section 745 won't be recognized as such, and will be skipped. 746 Return True if an object attribute section is found, False otherwise. */ 747 static bool 748 input_bfd_has_object_attributes (const struct bfd_link_info *info, 749 bfd *abfd, 750 bfd_search_result_t *res) 751 { 752 /* The file may contain object attributes. Save this candidate. */ 753 res->pbfd = abfd; 754 755 if (elf_obj_attr_subsections (abfd).size == 0) 756 return false; 757 758 res->has_object_attributes = true; 759 760 uint32_t sec_type = get_elf_backend_data (abfd)->obj_attrs_section_type; 761 const char *sec_name = get_elf_backend_data (abfd)->obj_attrs_section; 762 res->sec = bfd_get_section_by_name (abfd, sec_name); 763 if (res->sec != NULL) 764 { 765 if (elf_section_type (res->sec) != sec_type) 766 { 767 info->callbacks->minfo 768 (_("%X%pB: warning: ignoring section '%s' with unexpected type %#x\n"), 769 abfd, sec_name, elf_section_type (res->sec)); 770 res->sec = NULL; 771 } 772 } 773 return (res->sec != NULL); 774 } 775 776 /* Search for the first input object file containing object attributes. 777 If no such object is found, the result structure's PBFD points to the 778 last object file that could have contained object attributes. The 779 result structure's HAS_OBJECT_ATTRIBUTES allows to distinguish the 780 cases when PBFD contains or does not contain object attributes. If no 781 candidate file is found, PBFD will stay NULL. */ 782 static bfd_search_result_t 783 bfd_linear_find_first_with_obj_attrs (const struct bfd_link_info *info) 784 { 785 bfd_search_result_t res = { .has_object_attributes = false }; 786 for (bfd *abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) 787 { 788 if (oav2_relevant_elf_object (info, abfd) 789 && abfd->section_count != 0 790 && input_bfd_has_object_attributes (info, abfd, &res)) 791 break; 792 } 793 return res; 794 } 795 796 /* Create a object attributes section for the given bfd input. */ 797 static asection * 798 create_object_attributes_section (struct bfd_link_info *info, 799 bfd *abfd) 800 { 801 asection *sec; 802 const char *sec_name = get_elf_backend_data (abfd)->obj_attrs_section; 803 sec = bfd_make_section_with_flags (abfd, 804 sec_name, 805 (SEC_READONLY 806 | SEC_HAS_CONTENTS 807 | SEC_DATA)); 808 if (sec == NULL) 809 info->callbacks->fatal (_("%P: failed to create %s section\n"), sec_name); 810 811 if (!bfd_set_section_alignment (sec, 2)) 812 info->callbacks->fatal (_("%pA: failed to align section\n"), sec); 813 814 elf_section_type (sec) = get_elf_backend_data (abfd)->obj_attrs_section_type; 815 816 bfd_set_section_size (sec, bfd_elf_obj_attr_size (abfd)); 817 818 return sec; 819 } 820 821 /* Translate GNU properties that have object attributes v2 equivalents. */ 822 static void 823 oav2_translate_gnu_props_to_obj_attrs (const bfd *abfd) 824 { 825 const struct elf_backend_data *be = get_elf_backend_data (abfd); 826 if (be->translate_gnu_props_to_obj_attrs == NULL) 827 return; 828 829 for (const elf_property_list *p = elf_properties (abfd); 830 p != NULL; 831 p = p->next) 832 be->translate_gnu_props_to_obj_attrs (abfd, p); 833 } 834 835 /* Translate object attributes v2 that have GNU properties equivalents. */ 836 static void 837 oav2_translate_obj_attrs_to_gnu_props (bfd *abfd) 838 { 839 const struct elf_backend_data *be = get_elf_backend_data (abfd); 840 if (be->translate_obj_attrs_to_gnu_props == NULL) 841 return; 842 843 for (const obj_attr_subsection_v2_t *subsec 844 = elf_obj_attr_subsections (abfd).first; 845 subsec != NULL; 846 subsec = subsec->next) 847 be->translate_obj_attrs_to_gnu_props (abfd, subsec); 848 } 849 850 /* Compact duplicated tag declarations in a subsection. 851 Return True on success, False if any issue is found during the compaction, 852 i.e. conflicting values for the same tag. */ 853 static bool 854 oav2_compact_tags (const bfd *abfd, obj_attr_subsection_v2_t *subsec) 855 { 856 bool success = true; 857 858 for (obj_attr_v2_t *a = subsec->first; 859 a != NULL && a->next != NULL;) 860 { 861 if (a->tag != a->next->tag) 862 { 863 a = a->next; 864 continue; 865 } 866 867 /* For NTBS encoding, ensure that the string value of the attributes is 868 not NULL. 869 Note: a string attribute can only be NULL if it was created during the 870 merge process. Since tag compaction occurs before merging begins, this 871 assertion guarantees that the function is never called in a context 872 where the assumption does not hold. */ 873 BFD_ASSERT 874 (subsec->encoding == OA_ENC_ULEB128 || 875 (a->val.string != NULL && a->next->val.string != NULL)); 876 877 /* Values of a tag are the same, we remove the duplicate. */ 878 if ((subsec->encoding == OA_ENC_ULEB128 879 && a->val.uint == a->next->val.uint) 880 || (subsec->encoding == OA_ENC_NTBS 881 && strcmp (a->val.string, a->next->val.string) == 0)) 882 { 883 obj_attr_v2_t *dup_attr = a->next; 884 LINKED_LIST_REMOVE (obj_attr_v2_t) (subsec, dup_attr); 885 _bfd_elf_obj_attr_v2_free (dup_attr, subsec->encoding); 886 continue; 887 } 888 889 /* The values of a tag are different, there are various options: 890 - the tag values can be merged. This requires a knowledge of a tag 891 merge policy, sometimes generic, sometimes only known from the 892 target backend. Since no such complex case exists for now, it is 893 not implemented. 894 - values for a given tag have to be the same in the same subsection, 895 or it raises an error. This will be the default handling for now. */ 896 success = false; 897 898 const char *tag_s = _bfd_obj_attr_v2_tag_to_string 899 (get_elf_backend_data (abfd), subsec->name, a->tag); 900 901 if (subsec->encoding == OA_ENC_ULEB128) 902 _bfd_error_handler 903 (_("%pB: error: found duplicated attributes '%s' with conflicting " 904 "values (%#x vs %#x) in subsection %s"), 905 abfd, tag_s, a->val.uint, a->next->val.uint, subsec->name); 906 else /* (subsec->encoding == OA_ENC_NTBS) */ 907 _bfd_error_handler 908 (_("%pB: error: found duplicated attributes '%s' with conflicting " 909 "values ('%s' vs '%s') in subsection %s"), 910 abfd, tag_s, a->val.string, a->next->val.string, subsec->name); 911 912 free ((char *) tag_s); 913 914 /* We skip the tag, and continue the compaction of tags to find further 915 issues (if any). */ 916 a = a->next; 917 } 918 919 return success; 920 } 921 922 /* Merge two subsections together (object attributes v2 only). 923 The result is stored into subsec1. subsec2 is destroyed. 924 Return true if the merge was successful, false otherwise. 925 Note: subsec1 and subsec2 are expected to be sorted before the call to this 926 function. */ 927 static bool 928 oav2_subsection_destructive_merge (const bfd *abfd, 929 obj_attr_subsection_v2_t *subsec1, 930 obj_attr_subsection_v2_t *subsec2) 931 { 932 BFD_ASSERT (subsec1->encoding == subsec2->encoding 933 && subsec1->optional == subsec2->optional); 934 935 bool success = true; 936 937 success &= oav2_compact_tags (abfd, subsec1); 938 success &= oav2_compact_tags (abfd, subsec2); 939 940 obj_attr_v2_t *a1 = subsec1->first; 941 obj_attr_v2_t *a2 = subsec2->first; 942 while (a1 != NULL && a2 != NULL) 943 { 944 if (a1->tag < a2->tag) 945 {} /* Nothing to do, a1 is already in subsec1. */ 946 else if (a1->tag > a2->tag) 947 { 948 /* a2 is missing in subsec1, add it. */ 949 obj_attr_v2_t *previous 950 = LINKED_LIST_REMOVE (obj_attr_v2_t) (subsec2, a2); 951 LINKED_LIST_INSERT_BEFORE (obj_attr_v2_t) (subsec1, a2, a1); 952 a2 = previous; 953 } 954 else 955 { 956 const char *tag_s = NULL; 957 if (subsec1->encoding == OA_ENC_ULEB128 958 && a1->val.uint != a2->val.uint) 959 { 960 success = false; 961 tag_s = _bfd_obj_attr_v2_tag_to_string 962 (get_elf_backend_data (abfd), subsec1->name, a1->tag); 963 _bfd_error_handler 964 (_("%pB: error: found 2 subsections with the same name '%s' " 965 "and found conflicting values (%#x vs %#x) for object " 966 "attribute '%s'"), 967 abfd, subsec1->name, a1->val.uint, a2->val.uint, tag_s); 968 } 969 else if (subsec1->encoding == OA_ENC_NTBS 970 && strcmp (a1->val.string, a2->val.string) != 0) 971 { 972 success = false; 973 tag_s = _bfd_obj_attr_v2_tag_to_string 974 (get_elf_backend_data (abfd), subsec1->name, a1->tag); 975 _bfd_error_handler 976 (_("%pB: error: found 2 subsections with the same name '%s' " 977 "and found conflicting values ('%s' vs '%s') for object " 978 "attribute '%s"), 979 abfd, subsec1->name, a1->val.string, a2->val.string, tag_s); 980 } 981 free ((char *) tag_s); 982 } 983 a1 = a1->next; 984 a2 = a2->next; 985 } 986 987 for (; a2 != NULL; a2 = a2->next) 988 { 989 /* a2 is missing in subsec1, add it. */ 990 obj_attr_v2_t *previous 991 = LINKED_LIST_REMOVE (obj_attr_v2_t) (subsec2, a2); 992 LINKED_LIST_INSERT_BEFORE (obj_attr_v2_t) (subsec1, a2, a1); 993 a2 = previous; 994 } 995 996 /* If a1 != NULL, we don't care since it is already in subsec1. */ 997 998 /* Destroy subsec2 before exiting. */ 999 _bfd_elf_obj_attr_subsection_v2_free (subsec2); 1000 1001 return success; 1002 } 1003 1004 /* Merge duplicated subsections and object attributes inside a same object 1005 file. After a call to this function, the subsections and object attributes 1006 are sorted. 1007 Note: this function allows to handle in a best effort exotic objects produced 1008 by a non-GNU assembler. Duplicated subsections could come from the same 1009 section, or different ones. Indeed, the deserializer deserializes the 1010 content of a section if its type matches the object attributes type specified 1011 by the backend, regardless of the section name. The behavior for such cases 1012 is not specified by the Object Attributes specification, and are a question 1013 of implementation. Non-GNU linkers might have a different behavior with such 1014 exotic objects. */ 1015 static bool 1016 oav2_file_scope_merge_subsections (const bfd *abfd) 1017 { 1018 obj_attr_subsection_list_t *subsecs = &elf_obj_attr_subsections (abfd); 1019 1020 /* Sort all the subsections and object attributes as they might not have been 1021 inserted in the right order. From now on, the subsections and attributes 1022 will be assumed to be always sorted. Any additive mutation will need to 1023 preserve the order. */ 1024 oav2_sort_subsections (subsecs); 1025 1026 bool success = true; 1027 1028 obj_attr_subsection_v2_t *subsec = subsecs->first; 1029 while (subsec != NULL && subsec->next != NULL) 1030 { 1031 obj_attr_subsection_v2_t *dup_subsec 1032 = bfd_obj_attr_subsection_v2_find_by_name (subsec->next, 1033 subsec->name, 1034 false); 1035 if (dup_subsec != NULL) 1036 { 1037 LINKED_LIST_REMOVE (obj_attr_subsection_v2_t) (subsecs, dup_subsec); 1038 success &= oav2_subsection_destructive_merge (abfd, subsec, dup_subsec); 1039 } 1040 else 1041 subsec = subsec->next; 1042 } 1043 1044 return success; 1045 } 1046 1047 /* If an Object Attribute subsection inside ABFD cannot be identified neither 1048 as a GNU subsection or a backend-specific one, set the status of this 1049 subsection to UNKNOWN. The unknown subsection will be skipped during the 1050 merge process, and will be pruned from the output. */ 1051 static void 1052 oav2_subsections_mark_unknown (const bfd *abfd) 1053 { 1054 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1055 for (obj_attr_subsection_v2_t *subsec = elf_obj_attr_subsections (abfd).first; 1056 subsec != NULL; 1057 subsec = subsec->next) 1058 { 1059 if (bfd_obj_attr_v2_identify_subsection (bed, subsec->name) == NULL) 1060 subsec->status = obj_attr_subsection_v2_unknown; 1061 } 1062 } 1063 1064 /* Initialize the given ATTR with its default value coming from the known tag 1065 registry. */ 1066 static void 1067 oav2_attr_overwrite_with_default (const struct bfd_link_info *info, 1068 const obj_attr_subsection_v2_t *subsec, 1069 obj_attr_v2_t *attr) 1070 { 1071 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd); 1072 1073 const obj_attr_info_t *attr_info 1074 = _bfd_obj_attr_v2_find_known_by_tag (bed, subsec->name, attr->tag); 1075 if (attr_info == NULL) 1076 { 1077 attr->status = obj_attr_v2_unknown; 1078 if (subsec->encoding == OA_ENC_ULEB128) 1079 attr->val.uint = 0; 1080 else 1081 attr->val.string = NULL; 1082 return; 1083 } 1084 1085 if (bed->obj_attr_v2_default_value != NULL 1086 && bed->obj_attr_v2_default_value (info, attr_info, subsec, attr)) 1087 {} 1088 else if (subsec->encoding == OA_ENC_NTBS) 1089 { 1090 if (attr->val.string != NULL) 1091 { 1092 free ((void *) attr->val.string); 1093 attr->val.string = NULL; 1094 } 1095 if (attr_info->default_value.string != NULL) 1096 attr->val.string = xstrdup (attr_info->default_value.string); 1097 } 1098 else 1099 attr->val.uint = attr_info->default_value.uint; 1100 } 1101 1102 /* Create a new attribute with the same key (=tag) as ATTR, and initialized with 1103 its default value from the known tag registry. */ 1104 static obj_attr_v2_t * 1105 oav2_attr_default (const struct bfd_link_info *info, 1106 const obj_attr_subsection_v2_t *subsec, 1107 const obj_attr_v2_t *attr) 1108 { 1109 obj_attr_v2_t *new_attr = _bfd_elf_obj_attr_v2_copy (attr, subsec->encoding); 1110 oav2_attr_overwrite_with_default (info, subsec, new_attr); 1111 return new_attr; 1112 } 1113 1114 /* The currently supported merge policy in the testing GNU namespace. 1115 - bitwise AND: apply bitwise AND. 1116 - bitwise OR: apply bitwise OR. 1117 - String-ADD: concatenates strings together with a '+' in-between. 1118 Note: Such policies should only be used for testing. */ 1119 typedef enum { 1120 SUBSECTION_TESTING_MERGE_UNSUPPORTED = 0, 1121 SUBSECTION_TESTING_MERGE_AND_POLICY = 1, 1122 SUBSECTION_TESTING_MERGE_OR_POLICY = 2, 1123 SUBSECTION_TESTING_MERGE_ADD_POLICY = 3, 1124 } gnu_testing_merge_policy; 1125 1126 /* Determine which merge policy will be applied to SUBSEC. The GNU policy are 1127 detected from the name of the subsection. It should follow the following 1128 pattern: "gnu_testing_XXXXXX_MERGE_<POLICY>". 1129 Return one of the known merge policy if recognised, UNSUPPORTED otherwise. */ 1130 static gnu_testing_merge_policy 1131 gnu_testing_merge_subsection (const char *subsec_name) 1132 { 1133 if (! gnu_testing_namespace (subsec_name)) 1134 return SUBSECTION_TESTING_MERGE_UNSUPPORTED; 1135 1136 size_t subsec_name_len = strlen (subsec_name); 1137 if (strcmp ("_MERGE_AND", subsec_name + subsec_name_len - 10) == 0) 1138 return SUBSECTION_TESTING_MERGE_AND_POLICY; 1139 else if (strcmp ("_MERGE_OR", subsec_name + subsec_name_len - 9) == 0) 1140 return SUBSECTION_TESTING_MERGE_OR_POLICY; 1141 else if (strcmp ("_MERGE_ADD", subsec_name + subsec_name_len - 10) == 0) 1142 return SUBSECTION_TESTING_MERGE_ADD_POLICY; 1143 else 1144 return SUBSECTION_TESTING_MERGE_UNSUPPORTED; 1145 } 1146 1147 /* Merge policy Integer-AND: apply bitwise AND between REF and RHS. */ 1148 obj_attr_v2_merge_result_t 1149 _bfd_obj_attr_v2_merge_AND (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 1150 const bfd *abfd ATTRIBUTE_UNUSED, 1151 const obj_attr_subsection_v2_t *subsec, 1152 const obj_attr_v2_t *ref, const obj_attr_v2_t *rhs, 1153 const obj_attr_v2_t *frozen ATTRIBUTE_UNUSED) 1154 { 1155 BFD_ASSERT (subsec->encoding == OA_ENC_ULEB128); 1156 1157 obj_attr_v2_merge_result_t res; 1158 1159 res.val.uint = (ref->val.uint & rhs->val.uint); 1160 res.merge = (res.val.uint != ref->val.uint); 1161 res.reason = (!res.merge 1162 ? OAv2_MERGE_SAME_VALUE_AS_REF 1163 : OAv2_MERGE_OK); 1164 1165 return res; 1166 } 1167 1168 /* Merge policy Integer-OR: apply bitwise OR between REF and RHS. */ 1169 static obj_attr_v2_merge_result_t 1170 obj_attr_v2_merge_OR (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 1171 const bfd *abfd ATTRIBUTE_UNUSED, 1172 const obj_attr_subsection_v2_t *subsec, 1173 const obj_attr_v2_t *ref, const obj_attr_v2_t *rhs, 1174 const obj_attr_v2_t *frozen ATTRIBUTE_UNUSED) 1175 { 1176 BFD_ASSERT (subsec->encoding == OA_ENC_ULEB128); 1177 1178 obj_attr_v2_merge_result_t res; 1179 1180 res.val.uint = (ref->val.uint | rhs->val.uint); 1181 res.merge = (res.val.uint != ref->val.uint); 1182 res.reason = (!res.merge 1183 ? OAv2_MERGE_SAME_VALUE_AS_REF 1184 : OAv2_MERGE_OK); 1185 1186 return res; 1187 } 1188 1189 /* Merge policy String-ADD: concatenates strings from REF and RHS together 1190 adding a '+' character in-between. */ 1191 static obj_attr_v2_merge_result_t 1192 obj_attr_v2_merge_ADD (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 1193 const bfd *abfd ATTRIBUTE_UNUSED, 1194 const obj_attr_subsection_v2_t *subsec, 1195 const obj_attr_v2_t *ref, const obj_attr_v2_t *rhs, 1196 const obj_attr_v2_t *frozen ATTRIBUTE_UNUSED) 1197 { 1198 BFD_ASSERT (subsec->encoding == OA_ENC_NTBS); 1199 1200 obj_attr_v2_merge_result_t res = { 1201 .merge = false, 1202 .val.string = NULL, 1203 .reason = OAv2_MERGE_OK, 1204 }; 1205 1206 /* Note: FROZEN is unused for this "concatenating" merge because it is 1207 either passed as RHS when coming from oav2_subsections_merge_frozen(), 1208 or passed as FROZEN when coming from oav2_subsections_merge() and was 1209 already merged. */ 1210 1211 /* REF and RHS have both a value. Concatenate RHS to REF. */ 1212 if (ref->val.string && rhs->val.string) 1213 { 1214 res.merge = true; 1215 size_t ref_s_size = strlen (ref->val.string); 1216 size_t rhs_s_size = strlen (rhs->val.string); 1217 char *buffer = xmalloc (ref_s_size + 1 + rhs_s_size + 1); 1218 res.val.string = buffer; 1219 memcpy (buffer, ref->val.string, ref_s_size); 1220 buffer += ref_s_size; 1221 *buffer = '+'; 1222 ++buffer; 1223 memcpy (buffer, rhs->val.string, rhs_s_size + 1); 1224 } 1225 /* REF has a value, but RHS does not. Nothing to do. */ 1226 else if (ref->val.string) 1227 res.reason = OAv2_MERGE_SAME_VALUE_AS_REF; 1228 /* No previous value in REF. RHS is the new value. */ 1229 else if (rhs->val.string) 1230 { 1231 /* This case could be optimized to avoid the dynamic allocation by moving 1232 the value from RHS to RES, but then, we need to distinguish the case 1233 when RHS and FROZEN are the same, and in this case, the value should 1234 not be moved but copied. The little benefit is not worth the added 1235 complexity. */ 1236 res.merge = true; 1237 res.val.string = xstrdup (rhs->val.string); 1238 } 1239 return res; 1240 } 1241 1242 /* Return the merge result between attributes LHS, RHS and FROZEN. 1243 Note: FROZEN_IS_RHS indicates that FROZEN is in RHS's position. This happens 1244 when this function is called from oav2_subsections_merge_frozen(). When this 1245 boolean is true, arguments are swapped to get the correct diagnostic messages 1246 in case of issues. */ 1247 static obj_attr_v2_merge_result_t 1248 oav2_attr_merge (const struct bfd_link_info *info, 1249 const bfd *abfd, 1250 const obj_attr_subsection_v2_t *subsec, 1251 const obj_attr_v2_t *lhs, const obj_attr_v2_t *rhs, 1252 const obj_attr_v2_t *frozen, bool frozen_is_rhs) 1253 { 1254 obj_attr_v2_merge_result_t res = { 1255 .merge = false, 1256 .val.uint = 0, 1257 .reason = OAv2_MERGE_OK, 1258 }; 1259 1260 gnu_testing_merge_policy policy; 1261 1262 if (get_elf_backend_data (abfd)->obj_attr_v2_tag_merge != NULL) 1263 { 1264 /* If FROZEN is RHS (i.e. called from oav2_subsections_merge_frozen()), it 1265 means that the merged value of LHS (the REF) and FROZEN is going to 1266 be merged by the caller into LHS. However, since diagnostic messages 1267 always point to RHS, we need to swap LHS and RHS, so that ABFD is not 1268 associated wrongly to FROZEN. */ 1269 if (frozen_is_rhs) 1270 { 1271 const obj_attr_v2_t *tmp = lhs; 1272 lhs = rhs; 1273 rhs = tmp; 1274 } 1275 res = get_elf_backend_data (abfd)->obj_attr_v2_tag_merge (info, abfd, 1276 subsec, lhs, rhs, frozen); 1277 if (res.merge || res.reason == OAv2_MERGE_SAME_VALUE_AS_REF) 1278 return res; 1279 } 1280 1281 /* Note for the future: the merge of generic object attributes should be 1282 added here, between the architecture-specific merge, and the reserved GNU 1283 testing namespace. */ 1284 1285 /* GNU testing merge policies are looked up last. */ 1286 if ((res.reason == OAv2_MERGE_UNSUPPORTED || res.reason == OAv2_MERGE_OK) 1287 && (policy = gnu_testing_merge_subsection (subsec->name)) 1288 != SUBSECTION_TESTING_MERGE_UNSUPPORTED) 1289 { 1290 /* Only the first two attributes can be merged, others won't and will 1291 be discarded. */ 1292 if (lhs->tag <= 1) 1293 { 1294 if (policy == SUBSECTION_TESTING_MERGE_AND_POLICY) 1295 res = _bfd_obj_attr_v2_merge_AND (info, abfd, subsec, lhs, rhs, frozen); 1296 else if (policy == SUBSECTION_TESTING_MERGE_OR_POLICY) 1297 res = obj_attr_v2_merge_OR (info, abfd, subsec, lhs, rhs, frozen); 1298 else if (policy == SUBSECTION_TESTING_MERGE_ADD_POLICY) 1299 res = obj_attr_v2_merge_ADD (info, abfd, subsec, lhs, rhs, frozen); 1300 1301 return res; 1302 } 1303 } 1304 1305 /* Nothing matched, thus the merge of this tag is unsupported. */ 1306 res.reason = OAv2_MERGE_UNSUPPORTED; 1307 return res; 1308 } 1309 1310 /* Append a new default-initialized attribute with the same key as AREF to the 1311 given subsection. */ 1312 static void 1313 oav2_subsection_append_attr_default (const struct bfd_link_info *info, 1314 obj_attr_subsection_v2_t *s_abfd_missing, 1315 const obj_attr_v2_t *a_ref) 1316 { 1317 obj_attr_v2_t *new_attr = oav2_attr_default (info, s_abfd_missing, a_ref); 1318 LINKED_LIST_APPEND (obj_attr_v2_t) (s_abfd_missing, new_attr); 1319 } 1320 1321 /* Return a new default-initialized subsection with the same parameters as 1322 SUBSEC. */ 1323 static obj_attr_subsection_v2_t * 1324 oav2_subsection_default_new (const struct bfd_link_info *info, 1325 const obj_attr_subsection_v2_t *subsec) 1326 { 1327 obj_attr_subsection_v2_t *new_subsec = bfd_elf_obj_attr_subsection_v2_init 1328 (xstrdup (subsec->name), subsec->scope, subsec->optional, subsec->encoding); 1329 1330 for (const obj_attr_v2_t *attr = subsec->first; 1331 attr != NULL; 1332 attr = attr->next) 1333 oav2_subsection_append_attr_default (info, new_subsec, attr); 1334 1335 return new_subsec; 1336 } 1337 1338 /* Report missing required attribute with key TAG in subsection SREF. */ 1339 static void 1340 report_missing_required_obj_attr (const struct bfd_link_info *info, 1341 const bfd *abfd, 1342 const obj_attr_subsection_v2_t *s_ref, 1343 const obj_attr_tag_t tag) 1344 { 1345 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1346 const char *tag_s = _bfd_obj_attr_v2_tag_to_string (bed, s_ref->name, tag); 1347 info->callbacks->einfo ( 1348 _("%X%pB: error: missing required object attribute '%s' in subsection '%s'\n"), 1349 abfd, tag_s, s_ref->name); 1350 free ((char *) tag_s); 1351 } 1352 1353 /* Report required attribute A_ABFD mismatching with A_REF. */ 1354 static void 1355 report_mismatching_required_obj_attr (const struct bfd_link_info *info, 1356 const bfd *ref_bfd, 1357 const bfd *abfd, 1358 const obj_attr_subsection_v2_t *s_ref, 1359 const obj_attr_v2_t *a_ref, 1360 const obj_attr_v2_t *a_abfd) 1361 { 1362 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1363 const char *tag_s 1364 = _bfd_obj_attr_v2_tag_to_string (bed, s_ref->name, a_ref->tag); 1365 if (s_ref->encoding == OA_ENC_ULEB128) 1366 { 1367 info->callbacks->einfo ( 1368 _("%X%pB: error: mismatching value for required object attribute '%s' " 1369 "in subsection '%s': %#x\n"), 1370 abfd, tag_s, s_ref->name, a_abfd->val.uint); 1371 info->callbacks->info ( 1372 _("%pB: info: conflicting value '%#x' lives here\n"), 1373 ref_bfd, a_ref->val.uint); 1374 } 1375 else 1376 { 1377 info->callbacks->einfo ( 1378 _("%X%pB: error: mismatching value for required object attribute '%s' " 1379 "in subsection '%s': \"%s\"\n"), 1380 abfd, tag_s, s_ref->name, a_abfd->val.string); 1381 info->callbacks->info ( 1382 _("%pB: info: conflicting value \"%s\" lives here\n"), 1383 ref_bfd, a_ref->val.string); 1384 } 1385 free ((char *) tag_s); 1386 } 1387 1388 /* Attempt a perfect match between subsections S_REF and S_ABFD, and reports 1389 errors for any mismatch. Return S_REF if the subsections match, NULL 1390 otherwise.*/ 1391 static obj_attr_subsection_v2_t * 1392 oav2_subsection_perfect_match (const struct bfd_link_info *info, 1393 const bfd *ref_bfd, const bfd *abfd, 1394 obj_attr_subsection_v2_t *s_ref, 1395 const obj_attr_subsection_v2_t *s_abfd) 1396 { 1397 bool success = true; 1398 const obj_attr_v2_t *a_ref = s_ref->first; 1399 const obj_attr_v2_t *a_abfd = s_abfd->first; 1400 while (a_ref != NULL && a_abfd != NULL) 1401 { 1402 if (a_ref->tag < a_abfd->tag) 1403 { 1404 success = false; 1405 report_missing_required_obj_attr (info, abfd, s_ref, a_ref->tag); 1406 a_ref = a_ref->next; 1407 } 1408 else if (a_ref->tag > a_abfd->tag) 1409 { 1410 success = false; 1411 report_missing_required_obj_attr (info, ref_bfd, s_ref, a_abfd->tag); 1412 a_abfd = a_abfd->next; 1413 } 1414 else 1415 { 1416 if (s_ref->encoding == OA_ENC_ULEB128) 1417 { 1418 if (a_ref->val.uint != a_abfd->val.uint) 1419 { 1420 success = false; 1421 report_mismatching_required_obj_attr (info, ref_bfd, abfd, 1422 s_ref, a_ref, a_abfd); 1423 } 1424 } 1425 else if (s_ref->encoding == OA_ENC_NTBS) 1426 { 1427 if (strcmp (a_ref->val.string, a_abfd->val.string) != 0) 1428 { 1429 success = false; 1430 report_mismatching_required_obj_attr (info, ref_bfd, abfd, 1431 s_ref, a_ref, a_abfd); 1432 } 1433 } 1434 a_ref = a_ref->next; 1435 a_abfd = a_abfd->next; 1436 } 1437 } 1438 1439 for (; a_abfd != NULL; a_abfd = a_abfd->next) 1440 { 1441 success = false; 1442 report_missing_required_obj_attr (info, ref_bfd, s_ref, a_abfd->tag); 1443 } 1444 1445 for (; a_ref != NULL; a_ref = a_ref->next) 1446 { 1447 success = false; 1448 report_missing_required_obj_attr (info, abfd, s_ref, a_ref->tag); 1449 } 1450 1451 return success ? s_ref : NULL; 1452 } 1453 1454 /* Search for the attribute with TAG into a list of attributes. The attributes 1455 list is assumed to be sorted. Return the pointer to the attribute with TAG 1456 if found, NULL otherwise. */ 1457 static obj_attr_v2_t * 1458 oav2_search_by_tag (obj_attr_v2_t *attr_first, obj_attr_tag_t tag) 1459 { 1460 for (obj_attr_v2_t *attr = attr_first; attr != NULL; attr = attr->next) 1461 { 1462 if (attr->tag == tag) 1463 return attr; 1464 else if (attr->tag > tag) 1465 break; 1466 } 1467 return NULL; 1468 } 1469 1470 /* Merge optional subsection S_ABFD into S_REF. S_FROZEN is used to report any 1471 issue with the selected configuration, and to force the merge value to a 1472 specific value if needed. */ 1473 static 1474 obj_attr_subsection_v2_t * 1475 handle_optional_subsection_merge (const struct bfd_link_info *info, 1476 const bfd *ref_bfd, const bfd *abfd, 1477 obj_attr_subsection_v2_t *s_ref, 1478 const obj_attr_subsection_v2_t *s_abfd, 1479 const obj_attr_subsection_v2_t *s_frozen) 1480 { 1481 (void) info; 1482 /* REF_BFD and ABFD are the same only when the call originated from 1483 oav2_subsections_merge_frozen(), when FROZEN is merged into ABFD 1484 (the future REF_BFD). See detailed explanation in oav2_attr_merge(). */ 1485 bool frozen_is_abfd = (ref_bfd == abfd); 1486 obj_attr_v2_t *a_ref = s_ref->first; 1487 const obj_attr_v2_t *a_abfd = s_abfd->first; 1488 obj_attr_v2_t *a_frozen_first = (s_frozen != NULL) ? s_frozen->first : NULL; 1489 while (a_ref != NULL && a_abfd != NULL) 1490 { 1491 uint32_t searched_frozen_tag 1492 = (a_ref->tag < a_abfd->tag 1493 ? a_ref->tag 1494 : a_abfd->tag); 1495 obj_attr_v2_t *a_frozen 1496 = oav2_search_by_tag (a_frozen_first, searched_frozen_tag); 1497 if (a_frozen != NULL) 1498 a_frozen_first = a_frozen; 1499 1500 if (a_ref->tag < a_abfd->tag) 1501 { 1502 obj_attr_v2_t *a_default = oav2_attr_default (info, s_abfd, a_ref); 1503 obj_attr_v2_merge_result_t res 1504 = oav2_attr_merge (info, abfd, s_ref, a_ref, a_default, a_frozen, 1505 frozen_is_abfd); 1506 _bfd_elf_obj_attr_v2_free (a_default, s_ref->encoding); 1507 if (res.merge) 1508 a_ref->val = res.val; 1509 else if (res.reason == OAv2_MERGE_UNSUPPORTED) 1510 a_ref->status = obj_attr_v2_unknown; 1511 a_ref = a_ref->next; 1512 } 1513 else if (a_ref->tag > a_abfd->tag) 1514 { 1515 obj_attr_v2_t *a_default = oav2_attr_default (info, s_ref, a_abfd); 1516 obj_attr_v2_merge_result_t res 1517 = oav2_attr_merge (info, abfd, s_ref, a_default, a_abfd, a_frozen, 1518 frozen_is_abfd); 1519 if (res.merge || res.reason == OAv2_MERGE_SAME_VALUE_AS_REF) 1520 { 1521 a_default->val = res.val; 1522 LINKED_LIST_INSERT_BEFORE (obj_attr_v2_t) 1523 (s_ref, a_default, a_ref); 1524 } 1525 else 1526 _bfd_elf_obj_attr_v2_free (a_default, s_ref->encoding); 1527 a_abfd = a_abfd->next; 1528 } 1529 else 1530 { 1531 obj_attr_v2_merge_result_t res 1532 = oav2_attr_merge (info, abfd, s_ref, a_ref, a_abfd, a_frozen, 1533 frozen_is_abfd); 1534 if (res.merge) 1535 a_ref->val = res.val; 1536 else if (res.reason == OAv2_MERGE_UNSUPPORTED) 1537 a_ref->status = obj_attr_v2_unknown; 1538 a_ref = a_ref->next; 1539 a_abfd = a_abfd->next; 1540 } 1541 } 1542 1543 for (; a_abfd != NULL; a_abfd = a_abfd->next) 1544 { 1545 obj_attr_v2_t *a_default = oav2_attr_default (info, s_ref, a_abfd); 1546 obj_attr_v2_merge_result_t res 1547 = oav2_attr_merge (info, abfd, s_ref, a_default, a_abfd, NULL, false); 1548 if (res.merge || res.reason == OAv2_MERGE_SAME_VALUE_AS_REF) 1549 { 1550 a_default->val = res.val; 1551 LINKED_LIST_APPEND (obj_attr_v2_t) (s_ref, a_default); 1552 } 1553 else 1554 _bfd_elf_obj_attr_v2_free (a_default, s_ref->encoding); 1555 } 1556 1557 for (; a_ref != NULL; a_ref = a_ref->next) 1558 { 1559 obj_attr_v2_t *a_frozen = oav2_search_by_tag (a_frozen_first, a_ref->tag); 1560 if (a_frozen != NULL) 1561 a_frozen_first = a_frozen; 1562 1563 obj_attr_v2_t *a_default = oav2_attr_default (info, s_abfd, a_ref); 1564 obj_attr_v2_merge_result_t res 1565 = oav2_attr_merge (info, abfd, s_ref, a_ref, a_default, a_frozen, 1566 frozen_is_abfd); 1567 _bfd_elf_obj_attr_v2_free (a_default, s_ref->encoding); 1568 if (res.merge) 1569 a_ref->val = res.val; 1570 else if (res.reason == OAv2_MERGE_UNSUPPORTED) 1571 a_ref->status = obj_attr_v2_unknown; 1572 } 1573 1574 return s_ref; 1575 } 1576 1577 /* Merge case 1: S_ABFD and S_REF exists, so merge S_ABFD into S_REF. */ 1578 static obj_attr_subsection_v2_t * 1579 handle_subsection_merge (const struct bfd_link_info *info, 1580 const bfd *ref_bfd, const bfd *abfd, 1581 obj_attr_subsection_v2_t *s_ref, 1582 const obj_attr_subsection_v2_t *s_abfd, 1583 const obj_attr_subsection_v2_t *s_frozen) 1584 { 1585 if (! s_ref->optional) 1586 return oav2_subsection_perfect_match (info, ref_bfd, abfd, s_ref, s_abfd); 1587 return handle_optional_subsection_merge 1588 (info, ref_bfd, abfd, s_ref, s_abfd, s_frozen); 1589 } 1590 1591 /* Merge case 2: S_ABFD does not exist, but S_REF does. 1592 1. Create a new default-initialized S_ABFD. 1593 2. Merge S_ABFD into S_REF. */ 1594 static bool 1595 handle_subsection_missing (const struct bfd_link_info *info, 1596 const bfd *ref_bfd, const bfd *abfd, 1597 obj_attr_subsection_v2_t *s_ref, 1598 const obj_attr_subsection_v2_t *s_frozen) 1599 { 1600 if (! s_ref->optional) 1601 { 1602 info->callbacks->einfo 1603 (_("%X%pB: error: missing required object attributes subsection %s\n"), 1604 abfd, s_ref->name); 1605 return false; 1606 } 1607 1608 /* Compute default values of the missing attributes in ABFD, but present in 1609 REF, and merge ABFD's generated subsection with the one of REF. */ 1610 obj_attr_subsection_v2_t *s_abfd = oav2_subsection_default_new (info, s_ref); 1611 const obj_attr_subsection_v2_t *merged 1612 = handle_subsection_merge (info, ref_bfd, abfd, s_ref, s_abfd, s_frozen); 1613 _bfd_elf_obj_attr_subsection_v2_free (s_abfd); 1614 return merged != NULL; 1615 } 1616 1617 /* Merge case 3: S_ABFD does not have a S_REF equivalent. 1618 1. Create a new default-initialized S_REF subsection. 1619 2. Merge S_ABFD into S_REF. 1620 3. Insert S_REF into REF before S_REF_NEXT. */ 1621 static bool 1622 handle_subsection_additional (const struct bfd_link_info *info, 1623 const bfd *ref_bfd, const bfd *abfd, 1624 obj_attr_subsection_v2_t *s_ref_next, 1625 const obj_attr_subsection_v2_t *s_abfd, 1626 const obj_attr_subsection_v2_t *s_frozen) 1627 { 1628 if (! s_abfd->optional) 1629 { 1630 info->callbacks->einfo 1631 (_("%X%pB: error: missing required object attributes subsection %s\n"), 1632 ref_bfd, s_abfd->name); 1633 return false; 1634 } 1635 1636 /* Compute default values of the missing attributes in REF, but present in 1637 ABFD, and merge REF's generated subsection with the one of ABFD. */ 1638 obj_attr_subsection_v2_t *s_ref = oav2_subsection_default_new (info, s_abfd); 1639 obj_attr_subsection_v2_t *s_merged 1640 = handle_subsection_merge (info, ref_bfd, abfd, s_ref, s_abfd, s_frozen); 1641 if (s_merged != NULL) 1642 { 1643 LINKED_LIST_INSERT_BEFORE (obj_attr_subsection_v2_t) 1644 (&elf_obj_attr_subsections (ref_bfd), s_merged, s_ref_next); 1645 } 1646 else 1647 /* An issue occurred during the merge. s_ref won't be saved so it needs to 1648 be freed. */ 1649 _bfd_elf_obj_attr_subsection_v2_free (s_ref); 1650 return (s_merged != NULL); 1651 } 1652 1653 /* Check whether a subsection is known, and if so, whether the current 1654 properties of the subsection match the expected ones. 1655 Return True if the subsection is known, AND all the properties of the 1656 subsection match the expected. False otherwise. */ 1657 static bool 1658 oav2_subsection_match_known (const struct bfd_link_info *info, 1659 const bfd *abfd, 1660 const obj_attr_subsection_v2_t *subsec) 1661 { 1662 const known_subsection_v2_t *subsec_info 1663 = bfd_obj_attr_v2_identify_subsection (get_elf_backend_data (abfd), 1664 subsec->name); 1665 1666 if (subsec_info == NULL) 1667 return false; 1668 1669 bool match = true; 1670 if (subsec_info->encoding != subsec->encoding) 1671 { 1672 info->callbacks->einfo 1673 (_("%X%pB: error: <%s> property of subsection '%s' was " 1674 "incorrectly set. Got '%s', expected '%s'\n"), 1675 abfd, "encoding", subsec->name, 1676 bfd_oav2_encoding_to_string (subsec->encoding), 1677 bfd_oav2_encoding_to_string (subsec_info->encoding)); 1678 match = false; 1679 } 1680 if (subsec_info->optional != subsec->optional) 1681 { 1682 info->callbacks->einfo 1683 (_("%X%pB: error: <%s> property of subsection '%s' was " 1684 "incorrectly set. Got '%s', expected '%s'\n"), 1685 abfd, "comprehension", subsec->name, 1686 bfd_oav2_comprehension_to_string (subsec->optional), 1687 bfd_oav2_comprehension_to_string (subsec_info->optional)); 1688 match = false; 1689 } 1690 return match; 1691 } 1692 1693 /* Check whether the properties of the subsections S1 and S2 match. 1694 Return True if the properties match, False otherwise. */ 1695 static bool 1696 oav2_subsection_generic_params_match (const struct bfd_link_info *info, 1697 const bfd *f1, const bfd *f2, 1698 const obj_attr_subsection_v2_t *s1, 1699 const obj_attr_subsection_v2_t *s2) 1700 { 1701 bool match = (s1->encoding == s2->encoding && s1->optional == s2->optional); 1702 if (! match) 1703 { 1704 if (f1 != NULL) 1705 { 1706 info->callbacks->einfo ( 1707 _("%X%pB: error: mismatching properties of subsection '%s'\n"), 1708 f2, s1->name); 1709 info->callbacks->info ( 1710 _("%pB: info: conflicting properties (%s, %s) live here\n"), f1, 1711 bfd_oav2_comprehension_to_string (s1->optional), 1712 bfd_oav2_encoding_to_string (s1->encoding)); 1713 info->callbacks->info ( 1714 _("info: (%s, %s) VS (%s, %s)\n"), 1715 bfd_oav2_comprehension_to_string (s2->optional), 1716 bfd_oav2_encoding_to_string (s2->encoding), 1717 bfd_oav2_comprehension_to_string (s1->optional), 1718 bfd_oav2_encoding_to_string (s1->encoding)); 1719 } 1720 else 1721 { 1722 info->callbacks->einfo ( 1723 _("%X%pB: error: corrupted properties in subsection '%s'\n"), 1724 f2, s1->name); 1725 info->callbacks->info ( 1726 _("info: (%s, %s) VS (%s, %s)\n"), 1727 bfd_oav2_comprehension_to_string (s2->optional), 1728 bfd_oav2_encoding_to_string (s2->encoding), 1729 bfd_oav2_comprehension_to_string (s1->optional), 1730 bfd_oav2_encoding_to_string (s1->encoding)); 1731 } 1732 } 1733 return match; 1734 } 1735 1736 /* Check for mismatch between the parameters of subsections S1 and S2. 1737 Return True if the parameters mismatch, False otherwise. 1738 Note: F1 can be null when comparing FROZEN and the first object file used to 1739 store the merge result. If an error is reported, it means that one of the 1740 definition of S1 or S2 is corrupted. Most likely S2 because it is a user 1741 input, or S1 if it is a programmation error of FROZEN. In the second case, 1742 please raise a bug to binutils bug tracker. */ 1743 static bool 1744 oav2_subsection_mismatching_params (const struct bfd_link_info *info, 1745 const bfd *f1, const bfd *f2, 1746 const obj_attr_subsection_v2_t *s1, 1747 const obj_attr_subsection_v2_t *s2) 1748 { 1749 if (gnu_testing_namespace (s2->name)) 1750 return ! oav2_subsection_generic_params_match (info, f1, f2, s1, s2); 1751 1752 /* Check whether the subsection is known, and if so, match against the 1753 expected properties. 1754 Note: this piece of code must be guarded against gnu-testing subsections, 1755 as oav2_subsection_match_known() looks up at the known subsections. 1756 Since the "fictive" entry for gnu-testing known subsection has random 1757 values for its encoding and optionality, it won't be able to detect 1758 mismatching parameters correctly. */ 1759 return ! oav2_subsection_match_known (info, f2, s2); 1760 } 1761 1762 /* Merge object attributes from FROZEN into the object file ABFD. 1763 Note: this function is called only once before starting the merge process 1764 between the object files. ABFD corresponds to the future REF_BFD, and is 1765 used to store the result of the merge. ABFD is also an input file, so any 1766 mismatch against FROZEN should be raised before the values of ABFD be 1767 modified. */ 1768 static bool 1769 oav2_subsections_merge_frozen (const struct bfd_link_info *info, 1770 const bfd *abfd, 1771 const obj_attr_subsection_list_t *frozen_cfg) 1772 { 1773 const obj_attr_subsection_v2_t *s_frozen = frozen_cfg->first; 1774 if (s_frozen == NULL) 1775 return true; 1776 1777 bool success = true; 1778 1779 /* Note: all the handle_subsection_* functions call oav2_attr_merge() down the 1780 stack. Passing ABFD as REF_BFD allows to detect that this function is 1781 the caller. Then a special behavior in oav2_attr_merge() is triggered for 1782 this specific use case, so that we can obtain the right diagnostics. */ 1783 1784 obj_attr_subsection_v2_t *s_abfd = elf_obj_attr_subsections (abfd).first; 1785 while (s_frozen != NULL && s_abfd != NULL) 1786 { 1787 int cmp = strcmp (s_abfd->name, s_frozen->name); 1788 if (cmp < 0) /* ABFD has a subsection that FROZEN doesn't have. */ 1789 { 1790 /* No need to try to merge anything here. */ 1791 s_abfd = s_abfd->next; 1792 } 1793 else if (cmp > 0) /* FROZEN has a subsection that ABFD doesn't have. */ 1794 { 1795 success &= handle_subsection_additional (info, abfd, abfd, 1796 s_abfd, s_frozen, s_frozen); 1797 s_frozen = s_frozen->next; 1798 } 1799 else /* Both ABFD and frozen have the subsection. */ 1800 { 1801 bool mismatch = oav2_subsection_mismatching_params (info, NULL, abfd, 1802 s_frozen, s_abfd); 1803 success &= ! mismatch; 1804 if (mismatch) 1805 /* FROZEN cannot be corrupted as it is generated from the command 1806 line arguments. If it is corrupted, it is a bug. */ 1807 s_abfd->status = obj_attr_subsection_v2_corrupted; 1808 else 1809 success &= (handle_subsection_merge (info, abfd, abfd, 1810 s_abfd, s_frozen, s_frozen) != NULL); 1811 1812 s_abfd = s_abfd->next; 1813 s_frozen = s_frozen->next; 1814 } 1815 } 1816 1817 /* No need to go through the remaining sections of ABFD, only mismatches 1818 against FROZEN are interesting. */ 1819 1820 for (; s_frozen != NULL; s_frozen = s_frozen->next) 1821 success &= handle_subsection_additional (info, abfd, abfd, 1822 elf_obj_attr_subsections (abfd).last, s_frozen, s_frozen); 1823 1824 return success; 1825 } 1826 1827 /* Merge object attributes from object file ABFD and FROZEN_CFG into REF_BFD. */ 1828 static bool 1829 oav2_subsections_merge (const struct bfd_link_info *info, 1830 const bfd *ref_bfd, bfd *abfd, 1831 const obj_attr_subsection_list_t *frozen_cfg) 1832 { 1833 bool success = true; 1834 obj_attr_subsection_list_t *abfd_subsecs = &elf_obj_attr_subsections (abfd); 1835 obj_attr_subsection_list_t *ref_subsecs = &elf_obj_attr_subsections (ref_bfd); 1836 1837 obj_attr_subsection_v2_t *s_frozen_first = frozen_cfg->first; 1838 obj_attr_subsection_v2_t *s_abfd = abfd_subsecs->first; 1839 obj_attr_subsection_v2_t *s_ref = ref_subsecs->first; 1840 1841 while (s_abfd != NULL && s_ref != NULL) 1842 { 1843 int cmp = strcmp (s_ref->name, s_abfd->name); 1844 1845 if (cmp < 0) /* REF has a subsection that ABFD doesn't have. */ 1846 { 1847 if (s_ref->status != obj_attr_subsection_v2_ok) 1848 { 1849 s_ref = s_ref->next; 1850 continue; 1851 } 1852 1853 obj_attr_subsection_v2_t *s_frozen 1854 = bfd_obj_attr_subsection_v2_find_by_name (s_frozen_first, 1855 s_ref->name, 1856 true); 1857 1858 /* Mismatching between REF and FROZEN already done in 1859 oav2_subsections_merge_frozen. */ 1860 success &= handle_subsection_missing (info, ref_bfd, abfd, s_ref, 1861 s_frozen); 1862 1863 if (s_frozen != NULL) 1864 s_frozen_first = s_frozen->next; 1865 s_ref = s_ref->next; 1866 } 1867 else if (cmp > 0) /* ABFD has a subsection that REF doesn't have. */ 1868 { 1869 if (s_abfd->status != obj_attr_subsection_v2_ok) 1870 { 1871 s_abfd = s_abfd->next; 1872 continue; 1873 } 1874 1875 obj_attr_subsection_v2_t *s_frozen 1876 = bfd_obj_attr_subsection_v2_find_by_name (s_frozen_first, 1877 s_abfd->name, 1878 true); 1879 if (s_frozen != NULL) 1880 { 1881 /* Check any mismatch against ABFD and FROZEN. */ 1882 bool mismatch = oav2_subsection_mismatching_params (info, NULL, 1883 abfd, s_frozen, s_abfd); 1884 success &= ! mismatch; 1885 if (mismatch) 1886 s_abfd->status = obj_attr_subsection_v2_corrupted; 1887 else 1888 success &= handle_subsection_additional (info, ref_bfd, abfd, 1889 s_ref, s_abfd, s_frozen); 1890 } 1891 else 1892 success &= handle_subsection_additional (info, ref_bfd, abfd, s_ref, 1893 s_abfd, NULL); 1894 1895 if (s_frozen != NULL) 1896 s_frozen_first = s_frozen->next; 1897 s_abfd = s_abfd->next; 1898 } 1899 else /* Both REF and ABFD have the subsection. */ 1900 { 1901 if (s_ref->status != obj_attr_subsection_v2_ok) 1902 { 1903 s_ref = s_ref->next; 1904 s_abfd = s_abfd->next; 1905 continue; 1906 } 1907 1908 obj_attr_subsection_v2_t *s_frozen 1909 = bfd_obj_attr_subsection_v2_find_by_name (s_frozen_first, 1910 s_ref->name, 1911 true); 1912 1913 bool mismatch = oav2_subsection_mismatching_params 1914 (info, ref_bfd, abfd, s_ref, s_abfd); 1915 success &= ! mismatch; 1916 if (mismatch) 1917 s_abfd->status = obj_attr_subsection_v2_corrupted; 1918 else 1919 success &= (handle_subsection_merge (info, ref_bfd, abfd, s_ref, 1920 s_abfd, s_frozen) != NULL); 1921 1922 if (s_frozen != NULL) 1923 s_frozen_first = s_frozen->next; 1924 s_ref = s_ref->next; 1925 s_abfd = s_abfd->next; 1926 } 1927 } 1928 1929 for (; s_abfd != NULL; s_abfd = s_abfd->next) 1930 { 1931 if (s_abfd->status != obj_attr_subsection_v2_ok) 1932 continue; 1933 1934 obj_attr_subsection_v2_t *s_frozen 1935 = bfd_obj_attr_subsection_v2_find_by_name (s_frozen_first, 1936 s_abfd->name, 1937 true); 1938 if (s_frozen != NULL) 1939 { 1940 bool mismatch = oav2_subsection_mismatching_params (info, NULL, abfd, 1941 s_frozen, s_abfd); 1942 success &= ! mismatch; 1943 if (mismatch) 1944 s_abfd->status = obj_attr_subsection_v2_corrupted; 1945 else 1946 success &= handle_subsection_additional (info, ref_bfd, abfd, 1947 ref_subsecs->last, s_abfd, 1948 s_frozen); 1949 } 1950 else 1951 success &= handle_subsection_additional (info, ref_bfd, abfd, 1952 ref_subsecs->last, s_abfd, 1953 NULL); 1954 } 1955 1956 for (; s_ref != NULL; s_ref = s_ref->next) 1957 { 1958 if (s_ref->status != obj_attr_subsection_v2_ok) 1959 continue; 1960 1961 obj_attr_subsection_v2_t *s_frozen 1962 = bfd_obj_attr_subsection_v2_find_by_name (s_frozen_first, 1963 s_ref->name, 1964 true); 1965 /* No need to check for matching parameters here as frozen has already 1966 been checked against ref. */ 1967 success &= handle_subsection_missing (info, ref_bfd, abfd, s_ref, s_frozen); 1968 } 1969 1970 return success; 1971 } 1972 1973 /* Wrapper for the high-level logic of merging a single file. 1974 It handles both of the following cases: 1975 - ABFD (future REF_BFD) merged against FROZEN. 1976 - ABFD (input) and FROZEN_CFG merged into REF_BFD. 1977 If has_obj_attrs_after_translation is non-NULL, the caller is responsible for 1978 the distinction between the cases where attributes are already present, or 1979 where attributes were added as a result of a translation of GNU properties. 1980 The first step translates existing GNU properties to object attributes. Next, 1981 any duplicates entries in the input are merged, and the resulting object 1982 attributes are written back into GNU properties so that the GNU properties 1983 merge process can correctly diagnose potential issues. Before merging, 1984 unknown subsections and attributes are marked so they can be skipped during 1985 processing. 1986 Return True on success, False on failure. */ 1987 static bool 1988 oav2_merge_one (const struct bfd_link_info *info, 1989 bfd *ref_bfd, bfd *abfd, 1990 const obj_attr_subsection_list_t *frozen_cfg, 1991 bool *has_obj_attrs_after_translation) 1992 { 1993 /* ABFD is an input file that may contain GNU properties, object 1994 attributes, or both. Before merging object attributes, we must first 1995 translate any GNU properties into their equivalent object attributes 1996 (if such equivalents exist) since they may not already be present. */ 1997 oav2_translate_gnu_props_to_obj_attrs (abfd); 1998 if (has_obj_attrs_after_translation 1999 && elf_obj_attr_subsections (abfd).size > 0) 2000 *has_obj_attrs_after_translation = true; 2001 2002 /* Merge duplicates subsections and attributes. */ 2003 if (! oav2_file_scope_merge_subsections (abfd)) 2004 return false; 2005 2006 /* ABFD is an input file that may contain GNU properties, object 2007 attributes, or both. Before merging GNU properties, we must first 2008 translate any object attributes into their equivalent GNU properties 2009 (if such equivalents exist) since they may not already be present. */ 2010 oav2_translate_obj_attrs_to_gnu_props (abfd); 2011 2012 /* Note: object attributes are always merged before GNU properties. 2013 Ideally, there would be a single internal representation, with an 2014 abstraction level flexible enough to capture both GNU properties and 2015 object attributes without loss. In such a design, merge order would 2016 be irrelevant, and translation would occur only at the I/O boundaries 2017 during deserialization of GNU properties and object attributes. */ 2018 2019 /* Mark unknown subsections and attributes to skip them during 2020 the merge. */ 2021 oav2_subsections_mark_unknown (abfd); 2022 2023 if (ref_bfd == NULL) 2024 /* When REF_BFD is null, it means that ABFD is the future REF_BFD, and 2025 will be accumulating the merge result. 2026 It means that we will lose information from ABFD beyond this stage, 2027 so we need to emit warnings / errors (if any) when merging ABFD 2028 against FROZEN. */ 2029 return oav2_subsections_merge_frozen (info, abfd, frozen_cfg); 2030 2031 /* Common merge case. */ 2032 return oav2_subsections_merge (info, ref_bfd, abfd, frozen_cfg); 2033 } 2034 2035 /* Merge all the object attributes in INPUT_BFDS (REF_BFD excluded) into 2036 REF_BFD. Return True on success, False otherwise. */ 2037 static bool 2038 oav2_merge_all (const struct bfd_link_info *info, 2039 bfd *ref_bfd, bfd *input_bfds, 2040 obj_attr_subsection_list_t *frozen_cfg) 2041 { 2042 bool success = true; 2043 for (bfd *abfd = input_bfds; abfd != NULL; abfd = abfd->link.next) 2044 { 2045 if (abfd != ref_bfd && oav2_relevant_elf_object (info, abfd)) 2046 success &= oav2_merge_one (info, ref_bfd, abfd, frozen_cfg, NULL); 2047 } 2048 return success; 2049 } 2050 2051 /* Prune the given attribute, and return the next one in the list. */ 2052 static obj_attr_v2_t * 2053 oav2_attr_delete (obj_attr_subsection_v2_t *subsec, 2054 obj_attr_v2_t *attr) 2055 { 2056 obj_attr_v2_t *next = attr->next; 2057 LINKED_LIST_REMOVE (obj_attr_v2_t) (subsec, attr); 2058 _bfd_elf_obj_attr_v2_free (attr, subsec->encoding); 2059 return next; 2060 } 2061 2062 /* Prune the given subsection, and return the next one in the list. */ 2063 static obj_attr_subsection_v2_t * 2064 oav2_subsec_delete (obj_attr_subsection_list_t *plist, 2065 obj_attr_subsection_v2_t *subsec) 2066 { 2067 obj_attr_subsection_v2_t *next = subsec->next; 2068 LINKED_LIST_REMOVE (obj_attr_subsection_v2_t) (plist, subsec); 2069 _bfd_elf_obj_attr_subsection_v2_free (subsec); 2070 return next; 2071 } 2072 2073 /* Prune any attributes with a status different from obj_attr_v2_ok in the 2074 given subsection. */ 2075 static void 2076 oav2_attrs_prune_nok (const struct bfd_link_info *info, 2077 obj_attr_subsection_v2_t *subsec) 2078 { 2079 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd); 2080 for (obj_attr_v2_t *attr = subsec->first; 2081 attr != NULL;) 2082 { 2083 if (attr->status != obj_attr_v2_ok) 2084 { 2085 const char *tag_s 2086 = _bfd_obj_attr_v2_tag_to_string (bed, subsec->name, attr->tag); 2087 info->callbacks->minfo 2088 (_("Removed attribute '%s' from '%s'\n"), tag_s, subsec->name); 2089 free ((char *) tag_s); 2090 attr = oav2_attr_delete (subsec, attr); 2091 } 2092 else 2093 attr = attr->next; 2094 } 2095 } 2096 2097 /* Prune any subsection with a status different from obj_attr_subsection_v2_ok 2098 in the given list of subsections. */ 2099 static void 2100 oav2_subsecs_prune_nok (const struct bfd_link_info *info, 2101 obj_attr_subsection_list_t *plist) 2102 { 2103 for (obj_attr_subsection_v2_t *subsec = plist->first; 2104 subsec != NULL;) 2105 { 2106 if (subsec->status == obj_attr_subsection_v2_ok) 2107 oav2_attrs_prune_nok (info, subsec); 2108 2109 if (subsec->size == 0 || subsec->status != obj_attr_subsection_v2_ok) 2110 { 2111 info->callbacks->minfo (_("Removed subsection '%s'\n"), subsec->name); 2112 subsec = oav2_subsec_delete (plist, subsec); 2113 } 2114 else 2115 subsec = subsec->next; 2116 } 2117 } 2118 2119 /* Prune any subsection or attribute with a status different from OK. */ 2120 static bool 2121 oav2_prune_nok_attrs (const struct bfd_link_info *info, bfd *abfd) 2122 { 2123 obj_attr_subsection_list_t *plist = &elf_obj_attr_subsections (abfd); 2124 oav2_subsecs_prune_nok (info, plist); 2125 return (plist->size != 0); 2126 } 2127 2128 /* Set up object attributes coming from configuration, and merge them with the 2129 ones from the input object files. Return a pointer to the input object file 2130 containing the merge result on success, NULL otherwise. */ 2131 bfd * 2132 _bfd_elf_link_setup_object_attributes (struct bfd_link_info *info) 2133 { 2134 obj_attr_subsection_list_t *frozen_cfg 2135 = &elf_obj_attr_subsections (info->output_bfd); 2136 2137 bfd_search_result_t res 2138 = bfd_linear_find_first_with_obj_attrs (info); 2139 2140 /* If res.pbfd is NULL, it means that it didn't find any ELF object files. */ 2141 if (res.pbfd == NULL) 2142 return NULL; 2143 2144 /* Sort the frozen subsections and attributes in case that they were not 2145 inserted in the correct order. */ 2146 oav2_sort_subsections (frozen_cfg); 2147 2148 /* Merge object attributes sections. */ 2149 info->callbacks->minfo ("\n"); 2150 info->callbacks->minfo (_("Merging object attributes\n")); 2151 info->callbacks->minfo ("\n"); 2152 2153 bool success = oav2_merge_one (info, NULL, res.pbfd, frozen_cfg, 2154 &res.has_object_attributes); 2155 2156 /* No frozen object attributes and no object file, so nothing to do. */ 2157 if (!res.has_object_attributes && frozen_cfg->size == 0) 2158 return NULL; 2159 /* If frozen object attributes were set by some command-line options, we still 2160 need to emit warnings / errors if incompatibilities exist. */ 2161 2162 /* Set the object attribute version for the output object to the recommended 2163 value by the backend. */ 2164 elf_obj_attr_version (info->output_bfd) 2165 = get_elf_backend_data (info->output_bfd)->default_obj_attr_version; 2166 2167 if (res.sec == NULL) 2168 { 2169 /* This input object has no object attribute section matching the name and 2170 type specified by the backend, i.e. elf_backend_obj_attrs_section and 2171 elf_backend_obj_attrs_section_type. 2172 One of the two following cases is possible: 2173 1. No object attribute were found in this file, so the object attribute 2174 version was never set by the deserializer. 2175 2. The deserializer might have found attributes in another section with 2176 the correct type but the wrong name. The object attribute version 2177 should have been set correctly in this case. 2178 Whatever of those two cases, we set the object attribute version to the 2179 backend's recommended value, and create a new section with the expected 2180 name and type. */ 2181 elf_obj_attr_version (res.pbfd) 2182 = get_elf_backend_data (res.pbfd)->default_obj_attr_version; 2183 res.sec = create_object_attributes_section (info, res.pbfd); 2184 } 2185 2186 /* Merge all the input object files againt res.pbfd and frozen_cfg, and 2187 accumulate the merge result in res.pbfd. */ 2188 success &= oav2_merge_all (info, res.pbfd, info->input_bfds, frozen_cfg); 2189 if (! success) 2190 return NULL; 2191 2192 /* Prune all subsections and attributes with a status different from OK. */ 2193 if (! oav2_prune_nok_attrs (info, res.pbfd)) 2194 return NULL; 2195 BFD_ASSERT (elf_obj_attr_subsections (res.pbfd).size > 0); 2196 2197 2198 /* Shallow-copy the object attributes into output_bfd. */ 2199 elf_obj_attr_subsections (info->output_bfd) 2200 = elf_obj_attr_subsections (res.pbfd); 2201 2202 /* Note: the object attributes section in the output object is copied from 2203 the input object which was used for the merge (res.pbfd). No need to 2204 create it here. However, so that the section is copied to the output 2205 object, the size must be different from 0. For now, we will set this 2206 size to 1. The real size will be set later. */ 2207 res.sec->size = 1; 2208 2209 return res.pbfd; 2210 } 2211 2212 /* Allocate/find an object attribute. */ 2213 obj_attribute * 2214 bfd_elf_new_obj_attr (bfd *abfd, obj_attr_vendor_t vendor, obj_attr_tag_t tag) 2215 { 2216 obj_attribute *attr; 2217 obj_attribute_list *list; 2218 obj_attribute_list *p; 2219 obj_attribute_list **lastp; 2220 2221 2222 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES) 2223 { 2224 /* Known tags are preallocated. */ 2225 attr = &elf_known_obj_attributes (abfd)[vendor][tag]; 2226 } 2227 else 2228 { 2229 /* Create a new tag. */ 2230 list = (obj_attribute_list *) 2231 bfd_alloc (abfd, sizeof (obj_attribute_list)); 2232 if (list == NULL) 2233 return NULL; 2234 memset (list, 0, sizeof (obj_attribute_list)); 2235 list->tag = tag; 2236 /* Keep the tag list in order. */ 2237 lastp = &elf_other_obj_attributes (abfd)[vendor]; 2238 for (p = *lastp; p; p = p->next) 2239 { 2240 if (tag < p->tag) 2241 break; 2242 lastp = &p->next; 2243 } 2244 list->next = *lastp; 2245 *lastp = list; 2246 attr = &list->attr; 2247 } 2248 2249 return attr; 2250 } 2251 2252 /* Return the value of an integer object attribute. */ 2253 int 2254 bfd_elf_get_obj_attr_int (bfd *abfd, 2255 obj_attr_vendor_t vendor, 2256 obj_attr_tag_t tag) 2257 { 2258 obj_attribute_list *p; 2259 2260 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES) 2261 { 2262 /* Known tags are preallocated. */ 2263 return elf_known_obj_attributes (abfd)[vendor][tag].i; 2264 } 2265 else 2266 { 2267 for (p = elf_other_obj_attributes (abfd)[vendor]; 2268 p; 2269 p = p->next) 2270 { 2271 if (tag == p->tag) 2272 return p->attr.i; 2273 if (tag < p->tag) 2274 break; 2275 } 2276 return 0; 2277 } 2278 } 2279 2280 /* Add an integer object attribute. */ 2281 obj_attribute * 2282 bfd_elf_add_obj_attr_int (bfd *abfd, 2283 obj_attr_vendor_t vendor, 2284 obj_attr_tag_t tag, 2285 unsigned int value) 2286 { 2287 obj_attribute *attr; 2288 2289 attr = bfd_elf_new_obj_attr (abfd, vendor, tag); 2290 if (attr != NULL) 2291 { 2292 attr->type = bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); 2293 attr->i = value; 2294 } 2295 return attr; 2296 } 2297 2298 /* Duplicate an object attribute string value. */ 2299 static char * 2300 elf_attr_strdup (bfd *abfd, const char *s, const char *end) 2301 { 2302 char *p; 2303 size_t len; 2304 2305 if (end) 2306 len = strnlen (s, end - s); 2307 else 2308 len = strlen (s); 2309 2310 p = (char *) bfd_alloc (abfd, len + 1); 2311 if (p != NULL) 2312 { 2313 memcpy (p, s, len); 2314 p[len] = 0; 2315 } 2316 return p; 2317 } 2318 2319 char * 2320 _bfd_elf_attr_strdup (bfd *abfd, const char *s) 2321 { 2322 return elf_attr_strdup (abfd, s, NULL); 2323 } 2324 2325 /* Add a string object attribute. */ 2326 static obj_attribute * 2327 elf_add_obj_attr_string (bfd *abfd, obj_attr_vendor_t vendor, obj_attr_tag_t tag, 2328 const char *s, const char *end) 2329 { 2330 obj_attribute *attr; 2331 2332 attr = bfd_elf_new_obj_attr (abfd, vendor, tag); 2333 if (attr != NULL) 2334 { 2335 attr->type = bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); 2336 attr->s = elf_attr_strdup (abfd, s, end); 2337 if (attr->s == NULL) 2338 return NULL; 2339 } 2340 return attr; 2341 } 2342 2343 obj_attribute * 2344 bfd_elf_add_obj_attr_string (bfd *abfd, 2345 obj_attr_vendor_t vendor, 2346 obj_attr_tag_t tag, 2347 const char *s) 2348 { 2349 return elf_add_obj_attr_string (abfd, vendor, tag, s, NULL); 2350 } 2351 2352 /* Add a int+string object attribute. */ 2353 static obj_attribute * 2354 elf_add_obj_attr_int_string (bfd *abfd, 2355 obj_attr_vendor_t vendor, 2356 obj_attr_tag_t tag, 2357 unsigned int i, 2358 const char *s, 2359 const char *end) 2360 { 2361 obj_attribute *attr; 2362 2363 attr = bfd_elf_new_obj_attr (abfd, vendor, tag); 2364 if (attr != NULL) 2365 { 2366 attr->type = bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); 2367 attr->i = i; 2368 attr->s = elf_attr_strdup (abfd, s, end); 2369 if (attr->s == NULL) 2370 return NULL; 2371 } 2372 return attr; 2373 } 2374 2375 obj_attribute * 2376 bfd_elf_add_obj_attr_int_string (bfd *abfd, 2377 obj_attr_vendor_t vendor, 2378 obj_attr_tag_t tag, 2379 unsigned int i, 2380 const char *s) 2381 { 2382 return elf_add_obj_attr_int_string (abfd, vendor, tag, i, s, NULL); 2383 } 2384 2385 /* Copy object attributes v1 from IBFD to OBFD. */ 2386 static void 2387 oav1_copy_attributes (bfd *ibfd, bfd *obfd) 2388 { 2389 obj_attribute *in_attr; 2390 obj_attribute *out_attr; 2391 obj_attribute_list *list; 2392 int i; 2393 obj_attr_vendor_t vendor; 2394 2395 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++) 2396 { 2397 in_attr 2398 = &elf_known_obj_attributes (ibfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE]; 2399 out_attr 2400 = &elf_known_obj_attributes (obfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE]; 2401 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) 2402 { 2403 out_attr->type = in_attr->type; 2404 out_attr->i = in_attr->i; 2405 if (in_attr->s && *in_attr->s) 2406 { 2407 out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s); 2408 if (out_attr->s == NULL) 2409 bfd_perror (_("error adding attribute")); 2410 } 2411 in_attr++; 2412 out_attr++; 2413 } 2414 2415 for (list = elf_other_obj_attributes (ibfd)[vendor]; 2416 list; 2417 list = list->next) 2418 { 2419 bool ok = false; 2420 in_attr = &list->attr; 2421 switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL)) 2422 { 2423 case ATTR_TYPE_FLAG_INT_VAL: 2424 ok = bfd_elf_add_obj_attr_int (obfd, vendor, 2425 list->tag, in_attr->i); 2426 break; 2427 case ATTR_TYPE_FLAG_STR_VAL: 2428 ok = bfd_elf_add_obj_attr_string (obfd, vendor, list->tag, 2429 in_attr->s); 2430 break; 2431 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: 2432 ok = bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag, 2433 in_attr->i, in_attr->s); 2434 break; 2435 default: 2436 abort (); 2437 } 2438 if (!ok) 2439 bfd_perror (_("error adding attribute")); 2440 } 2441 } 2442 } 2443 2444 static obj_attr_subsection_v2_t * 2445 oav2_obj_attr_subsection_v2_copy (const obj_attr_subsection_v2_t *); 2446 2447 /* Copy object attributes v2 from IBFD to OBFD. */ 2448 static void 2449 oav2_copy_attributes (bfd *ibfd, bfd *obfd) 2450 { 2451 const obj_attr_subsection_list_t *in_attr_subsecs 2452 = &elf_obj_attr_subsections (ibfd); 2453 obj_attr_subsection_list_t *out_attr_subsecs 2454 = &elf_obj_attr_subsections (obfd); 2455 2456 for (const obj_attr_subsection_v2_t *isubsec = in_attr_subsecs->first; 2457 isubsec != NULL; 2458 isubsec = isubsec->next) 2459 { 2460 obj_attr_subsection_v2_t *osubsec 2461 = oav2_obj_attr_subsection_v2_copy (isubsec); 2462 LINKED_LIST_APPEND (obj_attr_subsection_v2_t) (out_attr_subsecs, osubsec); 2463 } 2464 } 2465 2466 /* Copy the object attributes from IBFD to OBFD. */ 2467 void 2468 _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd) 2469 { 2470 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 2471 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 2472 return; 2473 2474 obj_attr_version_t version = elf_obj_attr_version (ibfd); 2475 elf_obj_attr_version (obfd) = version; 2476 2477 switch (version) 2478 { 2479 case OBJ_ATTR_VERSION_NONE: 2480 break; 2481 case OBJ_ATTR_V1: 2482 oav1_copy_attributes (ibfd, obfd); 2483 break; 2484 case OBJ_ATTR_V2: 2485 oav2_copy_attributes (ibfd, obfd); 2486 break; 2487 default: 2488 abort (); 2489 } 2490 } 2491 2492 /* Determine whether a GNU object attribute tag takes an integer, a 2493 string or both. */ 2494 static int 2495 gnu_obj_attrs_arg_type (obj_attr_tag_t tag) 2496 { 2497 /* Except for Tag_compatibility, for GNU attributes we follow the 2498 same rule ARM ones > 32 follow: odd-numbered tags take strings 2499 and even-numbered tags take integers. In addition, tag & 2 is 2500 nonzero for architecture-independent tags and zero for 2501 architecture-dependent ones. */ 2502 if (tag == Tag_compatibility) 2503 return 3; 2504 else 2505 return (tag & 1) != 0 ? 2 : 1; 2506 } 2507 2508 /* Determine what arguments an attribute tag takes. */ 2509 int 2510 bfd_elf_obj_attrs_arg_type (bfd *abfd, 2511 obj_attr_vendor_t vendor, 2512 obj_attr_tag_t tag) 2513 { 2514 /* This function should only be called for object attributes version 1. */ 2515 BFD_ASSERT (elf_obj_attr_version (abfd) == OBJ_ATTR_V1); 2516 switch (vendor) 2517 { 2518 case OBJ_ATTR_PROC: 2519 return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag); 2520 break; 2521 case OBJ_ATTR_GNU: 2522 return gnu_obj_attrs_arg_type (tag); 2523 break; 2524 default: 2525 abort (); 2526 } 2527 } 2528 2529 static void 2530 oav1_parse_section (bfd *abfd, bfd_byte *p, bfd_byte *p_end) 2531 { 2532 const char *std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor; 2533 2534 while (p_end - p >= 4) 2535 { 2536 size_t len = p_end - p; 2537 size_t namelen; 2538 size_t section_len; 2539 int vendor; 2540 2541 section_len = bfd_get_32 (abfd, p); 2542 p += 4; 2543 if (section_len == 0) 2544 break; 2545 if (section_len > len) 2546 section_len = len; 2547 if (section_len <= 4) 2548 { 2549 _bfd_error_handler 2550 (_("%pB: error: attribute section length too small: %ld"), 2551 abfd, (long) section_len); 2552 break; 2553 } 2554 section_len -= 4; 2555 namelen = strnlen ((char *) p, section_len) + 1; 2556 if (namelen >= section_len) 2557 break; 2558 if (std_sec && strcmp ((char *) p, std_sec) == 0) 2559 vendor = OBJ_ATTR_PROC; 2560 else if (strcmp ((char *) p, "gnu") == 0) 2561 vendor = OBJ_ATTR_GNU; 2562 else 2563 { 2564 /* Other vendor section. Ignore it. */ 2565 p += section_len; 2566 continue; 2567 } 2568 2569 p += namelen; 2570 section_len -= namelen; 2571 while (section_len > 0) 2572 { 2573 unsigned int tag; 2574 unsigned int val; 2575 size_t subsection_len; 2576 bfd_byte *end, *orig_p; 2577 2578 orig_p = p; 2579 tag = _bfd_safe_read_leb128 (abfd, &p, false, p_end); 2580 if (p_end - p >= 4) 2581 { 2582 subsection_len = bfd_get_32 (abfd, p); 2583 p += 4; 2584 } 2585 else 2586 { 2587 p = p_end; 2588 break; 2589 } 2590 if (subsection_len > section_len) 2591 subsection_len = section_len; 2592 section_len -= subsection_len; 2593 end = orig_p + subsection_len; 2594 if (end < p) 2595 break; 2596 switch (tag) 2597 { 2598 case Tag_File: 2599 while (p < end) 2600 { 2601 int type; 2602 bool ok = false; 2603 2604 tag = _bfd_safe_read_leb128 (abfd, &p, false, end); 2605 type = bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); 2606 switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL)) 2607 { 2608 case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: 2609 val = _bfd_safe_read_leb128 (abfd, &p, false, end); 2610 ok = elf_add_obj_attr_int_string (abfd, vendor, tag, 2611 val, (char *) p, 2612 (char *) end); 2613 p += strnlen ((char *) p, end - p); 2614 if (p < end) 2615 p++; 2616 break; 2617 case ATTR_TYPE_FLAG_STR_VAL: 2618 ok = elf_add_obj_attr_string (abfd, vendor, tag, 2619 (char *) p, 2620 (char *) end); 2621 p += strnlen ((char *) p, end - p); 2622 if (p < end) 2623 p++; 2624 break; 2625 case ATTR_TYPE_FLAG_INT_VAL: 2626 val = _bfd_safe_read_leb128 (abfd, &p, false, end); 2627 ok = bfd_elf_add_obj_attr_int (abfd, vendor, tag, val); 2628 break; 2629 default: 2630 abort (); 2631 } 2632 if (!ok) 2633 bfd_perror (_("error adding attribute")); 2634 } 2635 break; 2636 case Tag_Section: 2637 case Tag_Symbol: 2638 /* Don't have anywhere convenient to attach these. 2639 Fall through for now. */ 2640 default: 2641 /* Ignore things we don't know about. */ 2642 p = end; 2643 break; 2644 } 2645 } 2646 } 2647 } 2648 2649 #define READ_ULEB128(abfd, var, cursor, end, total_read) \ 2650 do \ 2651 { \ 2652 bfd_byte *begin = (cursor); \ 2653 (var) = _bfd_safe_read_leb128 (abfd, &(cursor), false, end); \ 2654 (total_read) += (cursor) - begin; \ 2655 } \ 2656 while (0) 2657 2658 static int 2659 read_ntbs (bfd *abfd, 2660 const bfd_byte *cursor, 2661 const bfd_byte *end, 2662 char **s) 2663 { 2664 *s = NULL; 2665 const size_t MAX_STR_LEN = end - cursor; /* Including \0. */ 2666 const size_t s_len = strnlen ((const char *) cursor, MAX_STR_LEN); 2667 if (s_len == MAX_STR_LEN) 2668 { 2669 bfd_set_error (bfd_error_malformed_archive); 2670 _bfd_error_handler (_("%pB: error: NTBS value seems corrupted " 2671 "(missing '\\0')"), 2672 abfd); 2673 return -1; 2674 } 2675 *s = xmemdup (cursor, s_len + 1, s_len + 1); 2676 return s_len + 1; 2677 } 2678 2679 /* Parse an object attribute (v2 only). */ 2680 static ssize_t 2681 oav2_parse_attr (bfd *abfd, 2682 bfd_byte *cursor, 2683 const bfd_byte *end, 2684 obj_attr_encoding_v2_t attr_type, 2685 obj_attr_v2_t **attr) 2686 { 2687 *attr = NULL; 2688 ssize_t total_read = 0; 2689 2690 obj_attr_tag_t attr_tag; 2691 READ_ULEB128 (abfd, attr_tag, cursor, end, total_read); 2692 2693 union obj_attr_value_v2 attr_val; 2694 switch (attr_type) 2695 { 2696 case OA_ENC_NTBS: 2697 { 2698 int read = read_ntbs (abfd, cursor, end, (char **) &attr_val.string); 2699 if (read <= 0) 2700 return -1; 2701 total_read += read; 2702 } 2703 break; 2704 case OA_ENC_ULEB128: 2705 READ_ULEB128 (abfd, attr_val.uint, cursor, end, total_read); 2706 break; 2707 default: 2708 abort (); 2709 } 2710 2711 *attr = bfd_elf_obj_attr_v2_init (attr_tag, attr_val); 2712 return total_read; 2713 } 2714 2715 /* Parse a subsection (object attributes v2 only). */ 2716 static ssize_t 2717 oav2_parse_subsection (bfd *abfd, 2718 bfd_byte *cursor, 2719 const uint64_t max_read, 2720 obj_attr_subsection_v2_t **subsec) 2721 { 2722 *subsec = NULL; 2723 ssize_t total_read = 0; 2724 2725 char *subsection_name = NULL; 2726 const uint32_t F_SUBSECTION_LEN = sizeof(uint32_t); 2727 const uint32_t F_SUBSECTION_COMPREHENSION = sizeof(uint8_t); 2728 const uint32_t F_SUBSECTION_ENCODING = sizeof(uint8_t); 2729 /* The minimum subsection length is 7: 4 bytes for the length itself, and 1 2730 byte for an empty NUL-terminated string, 1 byte for the comprehension, 2731 1 byte for the encoding, and no vendor-data. */ 2732 const uint32_t F_MIN_SUBSECTION_DATA_LEN 2733 = F_SUBSECTION_LEN + 1 /* for '\0' */ 2734 + F_SUBSECTION_COMPREHENSION + F_SUBSECTION_ENCODING; 2735 2736 /* Similar to the issues reported in PR 17531, we need to check all the sizes 2737 and offsets as we parse the section. */ 2738 if (max_read < F_MIN_SUBSECTION_DATA_LEN) 2739 { 2740 _bfd_error_handler (_("%pB: error: attributes subsection ends " 2741 "prematurely"), 2742 abfd); 2743 goto error; 2744 } 2745 2746 const unsigned int subsection_len = bfd_get_32 (abfd, cursor); 2747 const bfd_byte *const end = cursor + subsection_len; 2748 total_read += F_SUBSECTION_LEN; 2749 cursor += F_SUBSECTION_LEN; 2750 if (subsection_len > max_read) 2751 { 2752 _bfd_error_handler 2753 (_("%pB: error: bad subsection length (%u > max=%" PRIu64 ")"), 2754 abfd, subsection_len, max_read); 2755 goto error; 2756 } 2757 else if (subsection_len < F_MIN_SUBSECTION_DATA_LEN) 2758 { 2759 _bfd_error_handler (_("%pB: error: subsection length of %u is too small"), 2760 abfd, subsection_len); 2761 goto error; 2762 } 2763 2764 const size_t max_subsection_name_len 2765 = subsection_len - F_SUBSECTION_LEN 2766 - F_SUBSECTION_COMPREHENSION - F_SUBSECTION_ENCODING; 2767 const bfd_byte *subsection_name_end 2768 = memchr (cursor, '\0', max_subsection_name_len); 2769 if (subsection_name_end == NULL) 2770 { 2771 _bfd_error_handler (_("%pB: error: subsection name seems corrupted " 2772 "(missing '\\0')"), 2773 abfd); 2774 goto error; 2775 } 2776 else 2777 /* Move the end pointer after '\0'. */ 2778 ++subsection_name_end; 2779 2780 /* Note: if the length of the subsection name is 0 (i.e. the string is '\0'), 2781 it is still considered a valid name, even if it is not particularly 2782 useful. */ 2783 2784 /* Note: at this stage, 2785 1. the length of the subsection name is validated, as the presence of '\0' 2786 at the end of the string, so no risk of buffer overrun. 2787 2. the data for comprehension and encoding can also safely be read. */ 2788 { 2789 /* Note: read_ntbs() assigns a dynamically allocated string to 2790 subsection_name. Either the string has to be freed in case of errors, 2791 or its ownership must be transferred. */ 2792 int read = read_ntbs (abfd, cursor, subsection_name_end, &subsection_name); 2793 total_read += read; 2794 cursor += read; 2795 } 2796 2797 uint8_t comprehension_raw = bfd_get_8 (abfd, cursor); 2798 ++cursor; 2799 ++total_read; 2800 2801 /* Comprehension is supposed to be a boolean, so any value greater than 1 is 2802 considered invalid. */ 2803 if (comprehension_raw > 1) 2804 { 2805 _bfd_error_handler (_("%pB: error: '%s' seems corrupted, got %u but only " 2806 "0 ('%s') or 1 ('%s') are valid values"), 2807 abfd, "comprehension", comprehension_raw, 2808 "required", "optional"); 2809 goto error; 2810 } 2811 2812 uint8_t value_encoding_raw = bfd_get_8 (abfd, cursor); 2813 ++cursor; 2814 ++total_read; 2815 2816 /* Encoding cannot be greater than OA_ENC_MAX, otherwise it means that either 2817 there is a new encoding that was introduced in the spec, and this 2818 implementation in binutils is older, and not aware of it so does not 2819 support it; or the stored value for encoding is garbage. */ 2820 enum obj_attr_encoding_v2 value_encoding 2821 = obj_attr_encoding_v2_from_u8 (value_encoding_raw); 2822 if (value_encoding > OA_ENC_MAX) 2823 { 2824 _bfd_error_handler (_("%pB: error: attribute type seems corrupted, got" 2825 " %u but only 0 (ULEB128) or 1 (NTBS) are " 2826 "valid types"), 2827 abfd, value_encoding); 2828 goto error; 2829 } 2830 2831 obj_attr_subsection_scope_v2_t scope 2832 = bfd_elf_obj_attr_subsection_v2_scope (abfd, subsection_name); 2833 2834 /* Note: ownership of 'subsection_name' is transfered to the callee when 2835 initializing the subsection. That is why we skip free() at the end. */ 2836 *subsec = bfd_elf_obj_attr_subsection_v2_init 2837 (subsection_name, scope, comprehension_raw, value_encoding); 2838 2839 /* A subsection can be empty, so 'cursor' can be equal to 'end' here. */ 2840 bool err = false; 2841 while (!err && cursor < end) 2842 { 2843 obj_attr_v2_t *attr; 2844 ssize_t read = oav2_parse_attr (abfd, cursor, end, value_encoding, &attr); 2845 if (attr != NULL) 2846 LINKED_LIST_APPEND (obj_attr_v2_t) (*subsec, attr); 2847 total_read += read; 2848 err |= (read < 0); 2849 cursor += read; 2850 } 2851 2852 if (err) 2853 { 2854 _bfd_elf_obj_attr_subsection_v2_free (*subsec); 2855 *subsec = NULL; 2856 return -1; 2857 } 2858 2859 BFD_ASSERT (cursor == end); 2860 return total_read; 2861 2862 error: 2863 bfd_set_error (bfd_error_malformed_archive); 2864 if (subsection_name) 2865 free (subsection_name); 2866 return -1; 2867 } 2868 2869 /* Parse the list of subsections (object attributes v2 only). */ 2870 static void 2871 oav2_parse_section (bfd *abfd, 2872 const Elf_Internal_Shdr *hdr, 2873 bfd_byte *cursor) 2874 { 2875 obj_attr_subsection_list_t *subsecs = &elf_obj_attr_subsections (abfd); 2876 ssize_t read = 0; 2877 for (uint64_t remaining = hdr->sh_size - 1; /* Already read 'A'. */ 2878 remaining > 0; 2879 remaining -= read, cursor += read) 2880 { 2881 obj_attr_subsection_v2_t *subsec = NULL; 2882 read = oav2_parse_subsection (abfd, cursor, remaining, &subsec); 2883 if (read < 0) 2884 { 2885 _bfd_error_handler 2886 (_("%pB: error: could not parse subsection at offset %" PRIx64), 2887 abfd, hdr->sh_size - remaining); 2888 bfd_set_error (bfd_error_wrong_format); 2889 break; 2890 } 2891 else 2892 LINKED_LIST_APPEND (obj_attr_subsection_v2_t) (subsecs, subsec); 2893 } 2894 } 2895 2896 /* Parse an object attributes section. 2897 Note: The parsing setup is common between object attributes v1 and v2. */ 2898 void 2899 _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr) 2900 { 2901 elf_obj_attr_version (abfd) = OBJ_ATTR_VERSION_NONE; 2902 2903 /* PR 17512: file: 2844a11d. */ 2904 if (hdr->sh_size == 0) 2905 return; 2906 2907 ufile_ptr filesize = bfd_get_file_size (abfd); 2908 if (filesize != 0 && hdr->sh_size > filesize) 2909 { 2910 _bfd_error_handler 2911 (_("%pB: error: attribute section '%pA' too big: %" PRIu64), 2912 abfd, hdr->bfd_section, (uint64_t) hdr->sh_size); 2913 bfd_set_error (bfd_error_invalid_operation); 2914 return; 2915 } 2916 2917 bfd_byte *data = (bfd_byte *) bfd_malloc (hdr->sh_size); 2918 if (!data) 2919 return; 2920 2921 if (!bfd_get_section_contents (abfd, hdr->bfd_section, data, 0, hdr->sh_size)) 2922 goto free_data; 2923 2924 unsigned char *cursor = data; 2925 2926 /* The first character is the version of the attributes. */ 2927 obj_attr_version_t version 2928 = get_elf_backend_data (abfd)->obj_attrs_version_dec (*cursor); 2929 if (version == OBJ_ATTR_VERSION_UNSUPPORTED || version > OBJ_ATTR_VERSION_MAX) 2930 { 2931 _bfd_error_handler (_("%pB: error: unknown attributes version '%c'(%d)\n"), 2932 abfd, *cursor, *cursor); 2933 bfd_set_error (bfd_error_wrong_format); 2934 goto free_data; 2935 } 2936 2937 ++cursor; 2938 2939 elf_obj_attr_version (abfd) = version; 2940 switch (version) 2941 { 2942 case OBJ_ATTR_V1: 2943 oav1_parse_section (abfd, cursor, data + hdr->sh_size); 2944 break; 2945 case OBJ_ATTR_V2: 2946 oav2_parse_section (abfd, hdr, cursor); 2947 break; 2948 default: 2949 abort (); 2950 } 2951 2952 free_data: 2953 free (data); 2954 } 2955 2956 /* Merge common object attributes from IBFD into OBFD. Raise an error 2957 if there are conflicting attributes. Any processor-specific 2958 attributes have already been merged. This must be called from the 2959 bfd_elfNN_bfd_merge_private_bfd_data hook for each individual 2960 target, along with any target-specific merging. Because there are 2961 no common attributes other than Tag_compatibility at present, and 2962 non-"gnu" Tag_compatibility is not expected in "gnu" sections, this 2963 is not presently called for targets without their own 2964 attributes. */ 2965 2966 bool 2967 _bfd_elf_merge_object_attributes (bfd *ibfd, struct bfd_link_info *info) 2968 { 2969 bfd *obfd = info->output_bfd; 2970 obj_attribute *in_attr; 2971 obj_attribute *out_attr; 2972 int vendor; 2973 2974 /* Set the object attribute version for the output object to the recommended 2975 value by the backend. */ 2976 elf_obj_attr_version (obfd) 2977 = get_elf_backend_data (obfd)->default_obj_attr_version; 2978 2979 /* The only common attribute is currently Tag_compatibility, 2980 accepted in both processor and "gnu" sections. */ 2981 for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++) 2982 { 2983 /* Handle Tag_compatibility. The tags are only compatible if the flags 2984 are identical and, if the flags are '1', the strings are identical. 2985 If the flags are non-zero, then we can only use the string "gnu". */ 2986 in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility]; 2987 out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility]; 2988 2989 if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0) 2990 { 2991 _bfd_error_handler 2992 /* xgettext:c-format */ 2993 (_("error: %pB: object has vendor-specific contents that " 2994 "must be processed by the '%s' toolchain"), 2995 ibfd, in_attr->s); 2996 return false; 2997 } 2998 2999 if (in_attr->i != out_attr->i 3000 || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0)) 3001 { 3002 /* xgettext:c-format */ 3003 _bfd_error_handler (_("error: %pB: object tag '%d, %s' is " 3004 "incompatible with tag '%d, %s'"), 3005 ibfd, 3006 in_attr->i, in_attr->s ? in_attr->s : "", 3007 out_attr->i, out_attr->s ? out_attr->s : ""); 3008 return false; 3009 } 3010 } 3011 3012 return true; 3013 } 3014 3015 /* Merge an unknown processor-specific attribute TAG, within the range 3016 of known attributes, from IBFD into OBFD; return TRUE if the link 3017 is OK, FALSE if it must fail. */ 3018 3019 bool 3020 _bfd_elf_merge_unknown_attribute_low (bfd *ibfd, bfd *obfd, int tag) 3021 { 3022 obj_attribute *in_attr; 3023 obj_attribute *out_attr; 3024 bfd *err_bfd = NULL; 3025 bool result = true; 3026 3027 in_attr = elf_known_obj_attributes_proc (ibfd); 3028 out_attr = elf_known_obj_attributes_proc (obfd); 3029 3030 if (out_attr[tag].i != 0 || out_attr[tag].s != NULL) 3031 err_bfd = obfd; 3032 else if (in_attr[tag].i != 0 || in_attr[tag].s != NULL) 3033 err_bfd = ibfd; 3034 3035 if (err_bfd != NULL) 3036 result 3037 = get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag); 3038 3039 /* Only pass on attributes that match in both inputs. */ 3040 if (in_attr[tag].i != out_attr[tag].i 3041 || (in_attr[tag].s == NULL) != (out_attr[tag].s == NULL) 3042 || (in_attr[tag].s != NULL && out_attr[tag].s != NULL 3043 && strcmp (in_attr[tag].s, out_attr[tag].s) != 0)) 3044 { 3045 out_attr[tag].i = 0; 3046 out_attr[tag].s = NULL; 3047 } 3048 3049 return result; 3050 } 3051 3052 /* Merge the lists of unknown processor-specific attributes, outside 3053 the known range, from IBFD into OBFD; return TRUE if the link is 3054 OK, FALSE if it must fail. */ 3055 3056 bool 3057 _bfd_elf_merge_unknown_attribute_list (bfd *ibfd, bfd *obfd) 3058 { 3059 obj_attribute_list *in_list; 3060 obj_attribute_list *out_list; 3061 obj_attribute_list **out_listp; 3062 bool result = true; 3063 3064 in_list = elf_other_obj_attributes_proc (ibfd); 3065 out_listp = &elf_other_obj_attributes_proc (obfd); 3066 out_list = *out_listp; 3067 3068 for (; in_list || out_list; ) 3069 { 3070 bfd *err_bfd = NULL; 3071 unsigned int err_tag = 0; 3072 3073 /* The tags for each list are in numerical order. */ 3074 /* If the tags are equal, then merge. */ 3075 if (out_list && (!in_list || in_list->tag > out_list->tag)) 3076 { 3077 /* This attribute only exists in obfd. We can't merge, and we don't 3078 know what the tag means, so delete it. */ 3079 err_bfd = obfd; 3080 err_tag = out_list->tag; 3081 *out_listp = out_list->next; 3082 out_list = *out_listp; 3083 } 3084 else if (in_list && (!out_list || in_list->tag < out_list->tag)) 3085 { 3086 /* This attribute only exists in ibfd. We can't merge, and we don't 3087 know what the tag means, so ignore it. */ 3088 err_bfd = ibfd; 3089 err_tag = in_list->tag; 3090 in_list = in_list->next; 3091 } 3092 else /* The tags are equal. */ 3093 { 3094 /* As present, all attributes in the list are unknown, and 3095 therefore can't be merged meaningfully. */ 3096 err_bfd = obfd; 3097 err_tag = out_list->tag; 3098 3099 /* Only pass on attributes that match in both inputs. */ 3100 if (in_list->attr.i != out_list->attr.i 3101 || (in_list->attr.s == NULL) != (out_list->attr.s == NULL) 3102 || (in_list->attr.s && out_list->attr.s 3103 && strcmp (in_list->attr.s, out_list->attr.s) != 0)) 3104 { 3105 /* No match. Delete the attribute. */ 3106 *out_listp = out_list->next; 3107 out_list = *out_listp; 3108 } 3109 else 3110 { 3111 /* Matched. Keep the attribute and move to the next. */ 3112 out_list = out_list->next; 3113 in_list = in_list->next; 3114 } 3115 } 3116 3117 if (err_bfd) 3118 result = result 3119 && get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, 3120 err_tag); 3121 } 3122 3123 return result; 3124 } 3125 3126 /* Create a new object attribute with key TAG and value VAL. 3127 Return a pointer to it. */ 3128 3129 obj_attr_v2_t * 3130 bfd_elf_obj_attr_v2_init (obj_attr_tag_t tag, 3131 union obj_attr_value_v2 val) 3132 { 3133 obj_attr_v2_t *attr = XCNEW (obj_attr_v2_t); 3134 attr->tag = tag; 3135 attr->val = val; 3136 attr->status = obj_attr_v2_ok; 3137 return attr; 3138 } 3139 3140 /* Free memory allocated by the object attribute ATTR. */ 3141 3142 void 3143 _bfd_elf_obj_attr_v2_free (obj_attr_v2_t *attr, obj_attr_encoding_v2_t encoding) 3144 { 3145 if (encoding == OA_ENC_NTBS) 3146 /* Note: this field never holds a string literal. */ 3147 free ((char *) attr->val.string); 3148 free (attr); 3149 } 3150 3151 /* Copy an object attribute OTHER, and return a pointer to the copy. */ 3152 3153 obj_attr_v2_t * 3154 _bfd_elf_obj_attr_v2_copy (const obj_attr_v2_t *other, 3155 obj_attr_encoding_v2_t encoding) 3156 { 3157 union obj_attr_value_v2 val; 3158 if (encoding == OA_ENC_NTBS) 3159 val.string 3160 = (other->val.string != NULL 3161 ? xstrdup (other->val.string) 3162 : NULL); 3163 else if (encoding == OA_ENC_ULEB128) 3164 val.uint = other->val.uint; 3165 else 3166 abort (); 3167 3168 obj_attr_v2_t *copy = bfd_elf_obj_attr_v2_init (other->tag, val); 3169 copy->status = other->status; 3170 return copy; 3171 } 3172 3173 /* Compare two object attributes based on their TAG value only (partial 3174 ordering), and return an integer indicating the result of the comparison, 3175 as follows: 3176 - 0, if A1 and A2 are equal. 3177 - a negative value if A1 is less than A2. 3178 - a positive value if A1 is greater than A2. */ 3179 3180 int 3181 _bfd_elf_obj_attr_v2_cmp (const obj_attr_v2_t *a1, const obj_attr_v2_t *a2) 3182 { 3183 if (a1->tag < a2->tag) 3184 return -1; 3185 if (a1->tag > a2->tag) 3186 return 1; 3187 return 0; 3188 } 3189 3190 /* Return an object attribute in SUBSEC matching TAG or NULL if one is not 3191 found. SORTED specifies whether the given list is ordered by tag number. 3192 This allows an early return if we find a higher numbered tag. */ 3193 3194 obj_attr_v2_t * 3195 bfd_obj_attr_v2_find_by_tag (const obj_attr_subsection_v2_t *subsec, 3196 obj_attr_tag_t tag, 3197 bool sorted) 3198 { 3199 for (obj_attr_v2_t *attr = subsec->first; 3200 attr != NULL; 3201 attr = attr->next) 3202 { 3203 if (attr->tag == tag) 3204 return attr; 3205 if (sorted && attr->tag > tag) 3206 break; 3207 } 3208 return NULL; 3209 } 3210 3211 /* Sort the object attributes inside a subsection. 3212 Note: since a subsection is a list of attributes, the sorting algorithm is 3213 implemented with a merge sort. 3214 See more details in libiberty/doubly-linked-list.h */ 3215 3216 LINKED_LIST_MUTATIVE_OPS_DECL (obj_attr_subsection_v2_t, 3217 obj_attr_v2_t, /* extern */) 3218 LINKED_LIST_MERGE_SORT_DECL (obj_attr_subsection_v2_t, 3219 obj_attr_v2_t, /* extern */) 3220 3221 /* Public API wrapper for LINKED_LIST_APPEND (obj_attr_v2_t). */ 3222 3223 void bfd_obj_attr_subsection_v2_append (obj_attr_subsection_v2_t *subsec, 3224 obj_attr_v2_t *attr) 3225 { 3226 LINKED_LIST_APPEND (obj_attr_v2_t) (subsec, attr); 3227 } 3228 3229 /* Create a new object attribute subsection with the following properties: 3230 - NAME: the name of the subsection. Note: this parameter never holds a 3231 string literal, so the value has to be freeable. 3232 - SCOPE: the scope of the subsection (public or private). 3233 - OPTIONAL: whether this subsection is optional (true) or required (false). 3234 - ENCODING: the expected encoding for the attributes values (ULEB128 or NTBS). 3235 Return a pointer to it. */ 3236 3237 obj_attr_subsection_v2_t * 3238 bfd_elf_obj_attr_subsection_v2_init (const char *name, 3239 obj_attr_subsection_scope_v2_t scope, 3240 bool optional, 3241 obj_attr_encoding_v2_t encoding) 3242 { 3243 obj_attr_subsection_v2_t *subsection = XCNEW (obj_attr_subsection_v2_t); 3244 subsection->name = name; 3245 subsection->scope = scope; 3246 subsection->optional = optional; 3247 subsection->encoding = encoding; 3248 subsection->status = obj_attr_subsection_v2_ok; 3249 return subsection; 3250 } 3251 3252 /* Free memory allocated by the object attribute subsection SUBSEC. */ 3253 3254 void 3255 _bfd_elf_obj_attr_subsection_v2_free (obj_attr_subsection_v2_t *subsec) 3256 { 3257 obj_attr_v2_t *attr = subsec->first; 3258 while (attr != NULL) 3259 { 3260 obj_attr_v2_t *a = attr; 3261 attr = attr->next; 3262 _bfd_elf_obj_attr_v2_free (a, subsec->encoding); 3263 } 3264 /* Note: this field never holds a string literal. */ 3265 free ((char *) subsec->name); 3266 free (subsec); 3267 } 3268 3269 /* Deep copy an object attribute subsection OTHER, and return a pointer to the 3270 copy. */ 3271 3272 static obj_attr_subsection_v2_t * 3273 oav2_obj_attr_subsection_v2_copy (const obj_attr_subsection_v2_t *other) 3274 { 3275 obj_attr_subsection_v2_t *new_subsec 3276 = bfd_elf_obj_attr_subsection_v2_init (xstrdup (other->name), other->scope, 3277 other->optional, other->encoding); 3278 new_subsec->status = other->status; 3279 3280 for (obj_attr_v2_t *attr = other->first; 3281 attr != NULL; 3282 attr = attr->next) 3283 { 3284 obj_attr_v2_t *new_attr = _bfd_elf_obj_attr_v2_copy (attr, other->encoding); 3285 LINKED_LIST_APPEND (obj_attr_v2_t) (new_subsec, new_attr); 3286 } 3287 return new_subsec; 3288 } 3289 3290 /* Compare two object attribute subsections based on all their properties. 3291 This operator can be used to obtain a total order in a collection. 3292 Return an integer indicating the result of the comparison, as follows: 3293 - 0, if S1 and S2 are equal. 3294 - a negative value if S1 is less than S2. 3295 - a positive value if S1 is greater than S2. 3296 3297 NB: the scope is computed from the name, so is not used for the 3298 comparison. */ 3299 3300 int 3301 _bfd_elf_obj_attr_subsection_v2_cmp (const obj_attr_subsection_v2_t *s1, 3302 const obj_attr_subsection_v2_t *s2) 3303 { 3304 int res = strcmp (s1->name, s2->name); 3305 if (res != 0) 3306 return res; 3307 3308 /* Note: The comparison of the encoding and optionality of subsections 3309 is entirely arbitrary. The numeric values could be completely flipped 3310 around without any effect. Likewise, assigning higher priority to 3311 optionality than to encoding is artificial. The desired properties for 3312 this comparison operator are reflexivity, transitivity, antisymmetry, 3313 and totality, in order to achieve a total ordering when sorting a 3314 collection of subsections. 3315 If the nature of this ordering were to change in the future, it would 3316 have no functional impact (but e.g. testsuite expectations might still 3317 need adjusting) on the final merged result in the output file. Only the 3318 order of the serialized subsections would differ, which does not affect 3319 the interpretation of the object attributes. 3320 Similarly, the ordering of subsections and attributes in an input file 3321 does not affect the merge process in ld. The merge process never assumes 3322 any particular ordering from the input files, it always sorts the 3323 subsections and attributes before merging. This means that using an 3324 older version of gas with a newer ld is safe, and vice versa as long as 3325 no new features are used that the older ld doesn't know of. 3326 In conclusion, the (arbitrary) criteria used to sort subsections during 3327 the merge process are entirely internal to ld and have no effect on the 3328 merge result. */ 3329 3330 if (!s1->optional && s2->optional) 3331 return -1; 3332 else if (s1->optional && !s2->optional) 3333 return 1; 3334 3335 if (s1->encoding < s2->encoding) 3336 return -1; 3337 else if (s1->encoding > s2->encoding) 3338 return 1; 3339 3340 return 0; 3341 } 3342 3343 /* Return a subsection in the list FIRST matching NAME, or NULL if one is not 3344 found. SORTED specifies whether the given list is ordered by name. 3345 This allows an early return if we find a alphabetically-higher name. */ 3346 3347 obj_attr_subsection_v2_t * 3348 bfd_obj_attr_subsection_v2_find_by_name (obj_attr_subsection_v2_t *first, 3349 const char *name, 3350 bool sorted) 3351 { 3352 for (obj_attr_subsection_v2_t *s = first; 3353 s != NULL; 3354 s = s->next) 3355 { 3356 int cmp = strcmp (s->name, name); 3357 if (cmp == 0) 3358 return s; 3359 else if (sorted && cmp > 0) 3360 break; 3361 } 3362 return NULL; 3363 } 3364 3365 /* Sort the subsections in a vendor section. 3366 Note: since a section is a list of subsections, the sorting algorithm is 3367 implemented with a merge sort. 3368 See more details in libiberty/doubly-linked-list.h */ 3369 3370 LINKED_LIST_MUTATIVE_OPS_DECL (obj_attr_subsection_list_t, 3371 obj_attr_subsection_v2_t, /* extern */) 3372 LINKED_LIST_MERGE_SORT_DECL (obj_attr_subsection_list_t, 3373 obj_attr_subsection_v2_t, /* extern */) 3374 3375 /* Public API wrapper for LINKED_LIST_APPEND (obj_attr_subsection_v2_t). */ 3376 3377 void 3378 bfd_obj_attr_subsection_v2_list_append (obj_attr_subsection_list_t *l, 3379 obj_attr_subsection_v2_t *subsec) 3380 { 3381 LINKED_LIST_APPEND (obj_attr_subsection_v2_t) (l, subsec); 3382 } 3383 3384 /* Public API wrapper for LINKED_LIST_REMOVE (obj_attr_subsection_v2_t). */ 3385 3386 obj_attr_subsection_v2_t * 3387 bfd_obj_attr_subsection_v2_list_remove (obj_attr_subsection_list_t *l, 3388 obj_attr_subsection_v2_t *subsec) 3389 { 3390 return LINKED_LIST_REMOVE (obj_attr_subsection_v2_t) (l, subsec); 3391 } 3392 3393 /* Serialize the object attributes in ABFD into the vendor section of 3394 OUTPUT_BFD. */ 3395 3396 bool _bfd_elf_write_section_object_attributes 3397 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED) 3398 { 3399 asection *sec = elf_obj_object_attributes (abfd); 3400 3401 if (sec == NULL) 3402 return true; 3403 3404 bfd_byte *contents = (bfd_byte *) bfd_malloc (sec->size); 3405 if (contents == NULL) 3406 return false; /* Bail out and fail. */ 3407 3408 bfd_elf_set_obj_attr_contents (abfd, contents, sec->size); 3409 bfd_set_section_contents (abfd, sec, contents, 0, sec->size); 3410 free (contents); 3411 return true; 3412 } 3413