1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2024 Google LLC 4 */ 5 6 #define _GNU_SOURCE 7 #include <assert.h> 8 #include <inttypes.h> 9 #include <stdarg.h> 10 #include "gendwarfksyms.h" 11 12 /* See get_union_kabi_status */ 13 #define KABI_PREFIX "__kabi_" 14 #define KABI_PREFIX_LEN (sizeof(KABI_PREFIX) - 1) 15 #define KABI_RESERVED_PREFIX "reserved" 16 #define KABI_RESERVED_PREFIX_LEN (sizeof(KABI_RESERVED_PREFIX) - 1) 17 #define KABI_RENAMED_PREFIX "renamed" 18 #define KABI_RENAMED_PREFIX_LEN (sizeof(KABI_RENAMED_PREFIX) - 1) 19 #define KABI_IGNORED_PREFIX "ignored" 20 #define KABI_IGNORED_PREFIX_LEN (sizeof(KABI_IGNORED_PREFIX) - 1) 21 22 static inline bool is_kabi_prefix(const char *name) 23 { 24 return name && !strncmp(name, KABI_PREFIX, KABI_PREFIX_LEN); 25 } 26 27 enum kabi_status { 28 /* >0 to stop DIE processing */ 29 KABI_NORMAL = 1, 30 KABI_RESERVED, 31 KABI_IGNORED, 32 }; 33 34 static bool do_linebreak; 35 static int indentation_level; 36 37 /* Line breaks and indentation for pretty-printing */ 38 static void process_linebreak(struct die *cache, int n) 39 { 40 indentation_level += n; 41 do_linebreak = true; 42 die_map_add_linebreak(cache, n); 43 } 44 45 #define DEFINE_GET_ATTR(attr, type) \ 46 static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \ 47 type *value) \ 48 { \ 49 Dwarf_Attribute da; \ 50 return dwarf_attr(die, id, &da) && \ 51 !dwarf_form##attr(&da, value); \ 52 } 53 54 DEFINE_GET_ATTR(flag, bool) 55 DEFINE_GET_ATTR(udata, Dwarf_Word) 56 57 static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *value) 58 { 59 Dwarf_Attribute da; 60 61 /* dwarf_formref_die returns a pointer instead of an error value. */ 62 return dwarf_attr(die, id, &da) && dwarf_formref_die(&da, value); 63 } 64 65 #define DEFINE_GET_STRING_ATTR(attr) \ 66 static const char *get_##attr##_attr(Dwarf_Die *die) \ 67 { \ 68 Dwarf_Attribute da; \ 69 if (dwarf_attr(die, DW_AT_##attr, &da)) \ 70 return dwarf_formstring(&da); \ 71 return NULL; \ 72 } 73 74 DEFINE_GET_STRING_ATTR(name) 75 DEFINE_GET_STRING_ATTR(linkage_name) 76 77 static const char *get_symbol_name(Dwarf_Die *die) 78 { 79 const char *name; 80 81 /* rustc uses DW_AT_linkage_name for exported symbols */ 82 name = get_linkage_name_attr(die); 83 if (!name) 84 name = get_name_attr(die); 85 86 return name; 87 } 88 89 static bool match_export_symbol(struct state *state, Dwarf_Die *die) 90 { 91 Dwarf_Die *source = die; 92 Dwarf_Die origin; 93 94 /* If the DIE has an abstract origin, use it for type information. */ 95 if (get_ref_die_attr(die, DW_AT_abstract_origin, &origin)) 96 source = &origin; 97 98 state->sym = symbol_get(get_symbol_name(die)); 99 100 /* Look up using the origin name if there are no matches. */ 101 if (!state->sym && source != die) 102 state->sym = symbol_get(get_symbol_name(source)); 103 104 state->die = *source; 105 return !!state->sym; 106 } 107 108 /* DW_AT_decl_file -> struct srcfile */ 109 static struct cache srcfile_cache; 110 111 static bool is_definition_private(Dwarf_Die *die) 112 { 113 Dwarf_Word filenum; 114 Dwarf_Files *files; 115 Dwarf_Die cudie; 116 const char *s; 117 int res; 118 119 /* 120 * Definitions in .c files cannot change the public ABI, 121 * so consider them private. 122 */ 123 if (!get_udata_attr(die, DW_AT_decl_file, &filenum)) 124 return false; 125 126 res = cache_get(&srcfile_cache, filenum); 127 if (res >= 0) 128 return !!res; 129 130 if (!dwarf_cu_die(die->cu, &cudie, NULL, NULL, NULL, NULL, NULL, NULL)) 131 error("dwarf_cu_die failed: '%s'", dwarf_errmsg(-1)); 132 133 if (dwarf_getsrcfiles(&cudie, &files, NULL)) 134 error("dwarf_getsrcfiles failed: '%s'", dwarf_errmsg(-1)); 135 136 s = dwarf_filesrc(files, filenum, NULL, NULL); 137 if (!s) 138 error("dwarf_filesrc failed: '%s'", dwarf_errmsg(-1)); 139 140 s = strrchr(s, '.'); 141 res = s && !strcmp(s, ".c"); 142 cache_set(&srcfile_cache, filenum, res); 143 144 return !!res; 145 } 146 147 static bool is_kabi_definition(struct die *cache, Dwarf_Die *die) 148 { 149 bool value; 150 151 if (get_flag_attr(die, DW_AT_declaration, &value) && value) 152 return false; 153 154 if (kabi_is_declonly(cache->fqn)) 155 return false; 156 157 return !is_definition_private(die); 158 } 159 160 /* 161 * Type string processing 162 */ 163 static void process(struct die *cache, const char *s) 164 { 165 s = s ?: "<null>"; 166 167 if (dump_dies && do_linebreak) { 168 fputs("\n", stderr); 169 for (int i = 0; i < indentation_level; i++) 170 fputs(" ", stderr); 171 do_linebreak = false; 172 } 173 if (dump_dies) 174 fputs(s, stderr); 175 176 if (cache) 177 die_debug_r("cache %p string '%s'", cache, s); 178 die_map_add_string(cache, s); 179 } 180 181 #define MAX_FMT_BUFFER_SIZE 128 182 183 static void process_fmt(struct die *cache, const char *fmt, ...) 184 { 185 char buf[MAX_FMT_BUFFER_SIZE]; 186 va_list args; 187 188 va_start(args, fmt); 189 190 if (checkp(vsnprintf(buf, sizeof(buf), fmt, args)) >= sizeof(buf)) 191 error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE"); 192 193 process(cache, buf); 194 va_end(args); 195 } 196 197 static void update_fqn(struct die *cache, Dwarf_Die *die) 198 { 199 struct die *fqn; 200 201 if (!cache->fqn) { 202 if (!__die_map_get((uintptr_t)die->addr, DIE_FQN, &fqn) && 203 *fqn->fqn) 204 cache->fqn = xstrdup(fqn->fqn); 205 else 206 cache->fqn = ""; 207 } 208 } 209 210 static void process_fqn(struct die *cache, Dwarf_Die *die) 211 { 212 update_fqn(cache, die); 213 if (*cache->fqn) 214 process(cache, " "); 215 process(cache, cache->fqn); 216 } 217 218 #define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) \ 219 static void process_##attribute##_attr(struct die *cache, \ 220 Dwarf_Die *die) \ 221 { \ 222 Dwarf_Word value; \ 223 if (get_udata_attr(die, DW_AT_##attribute, &value)) \ 224 process_fmt(cache, " " #attribute "(%" PRIu64 ")", \ 225 value); \ 226 } 227 228 DEFINE_PROCESS_UDATA_ATTRIBUTE(accessibility) 229 DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) 230 DEFINE_PROCESS_UDATA_ATTRIBUTE(bit_size) 231 DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) 232 DEFINE_PROCESS_UDATA_ATTRIBUTE(data_bit_offset) 233 DEFINE_PROCESS_UDATA_ATTRIBUTE(data_member_location) 234 DEFINE_PROCESS_UDATA_ATTRIBUTE(discr_value) 235 236 static void process_byte_size_attr(struct die *cache, Dwarf_Die *die) 237 { 238 Dwarf_Word value; 239 unsigned long override; 240 241 if (get_udata_attr(die, DW_AT_byte_size, &value)) { 242 if (stable && kabi_get_byte_size(cache->fqn, &override)) 243 value = override; 244 245 process_fmt(cache, " byte_size(%" PRIu64 ")", value); 246 } 247 } 248 249 /* Match functions -- die_match_callback_t */ 250 #define DEFINE_MATCH(type) \ 251 static bool match_##type##_type(Dwarf_Die *die) \ 252 { \ 253 return dwarf_tag(die) == DW_TAG_##type##_type; \ 254 } 255 256 DEFINE_MATCH(enumerator) 257 DEFINE_MATCH(formal_parameter) 258 DEFINE_MATCH(member) 259 DEFINE_MATCH(subrange) 260 261 bool match_all(Dwarf_Die *die) 262 { 263 return true; 264 } 265 266 int process_die_container(struct state *state, struct die *cache, 267 Dwarf_Die *die, die_callback_t func, 268 die_match_callback_t match) 269 { 270 Dwarf_Die current; 271 int res; 272 273 /* Track the first item in lists. */ 274 if (state) 275 state->first_list_item = true; 276 277 res = checkp(dwarf_child(die, ¤t)); 278 while (!res) { 279 if (match(¤t)) { 280 /* <0 = error, 0 = continue, >0 = stop */ 281 res = checkp(func(state, cache, ¤t)); 282 if (res) 283 goto out; 284 } 285 286 res = checkp(dwarf_siblingof(¤t, ¤t)); 287 } 288 289 res = 0; 290 out: 291 if (state) 292 state->first_list_item = false; 293 294 return res; 295 } 296 297 static int process_type(struct state *state, struct die *parent, 298 Dwarf_Die *die); 299 300 static void process_type_attr(struct state *state, struct die *cache, 301 Dwarf_Die *die) 302 { 303 Dwarf_Die type; 304 305 if (get_ref_die_attr(die, DW_AT_type, &type)) { 306 check(process_type(state, cache, &type)); 307 return; 308 } 309 310 /* Compilers can omit DW_AT_type -- print out 'void' to clarify */ 311 process(cache, "base_type void"); 312 } 313 314 static void process_list_comma(struct state *state, struct die *cache) 315 { 316 if (state->first_list_item) { 317 state->first_list_item = false; 318 } else { 319 process(cache, " ,"); 320 process_linebreak(cache, 0); 321 } 322 } 323 324 /* Comma-separated with DW_AT_type */ 325 static void __process_list_type(struct state *state, struct die *cache, 326 Dwarf_Die *die, const char *type) 327 { 328 const char *name = get_name_attr(die); 329 330 if (stable) { 331 if (is_kabi_prefix(name)) 332 name = NULL; 333 state->kabi.orig_name = NULL; 334 } 335 336 process_list_comma(state, cache); 337 process(cache, type); 338 process_type_attr(state, cache, die); 339 340 if (stable && state->kabi.orig_name) 341 name = state->kabi.orig_name; 342 if (name) { 343 process(cache, " "); 344 process(cache, name); 345 } 346 347 process_accessibility_attr(cache, die); 348 process_bit_size_attr(cache, die); 349 process_data_bit_offset_attr(cache, die); 350 process_data_member_location_attr(cache, die); 351 } 352 353 #define DEFINE_PROCESS_LIST_TYPE(type) \ 354 static void process_##type##_type(struct state *state, \ 355 struct die *cache, Dwarf_Die *die) \ 356 { \ 357 __process_list_type(state, cache, die, #type " "); \ 358 } 359 360 DEFINE_PROCESS_LIST_TYPE(formal_parameter) 361 DEFINE_PROCESS_LIST_TYPE(member) 362 363 /* Container types with DW_AT_type */ 364 static void __process_type(struct state *state, struct die *cache, 365 Dwarf_Die *die, const char *type) 366 { 367 process(cache, type); 368 process_fqn(cache, die); 369 process(cache, " {"); 370 process_linebreak(cache, 1); 371 process_type_attr(state, cache, die); 372 process_linebreak(cache, -1); 373 process(cache, "}"); 374 process_byte_size_attr(cache, die); 375 process_alignment_attr(cache, die); 376 } 377 378 #define DEFINE_PROCESS_TYPE(type) \ 379 static void process_##type##_type(struct state *state, \ 380 struct die *cache, Dwarf_Die *die) \ 381 { \ 382 __process_type(state, cache, die, #type "_type"); \ 383 } 384 385 DEFINE_PROCESS_TYPE(atomic) 386 DEFINE_PROCESS_TYPE(const) 387 DEFINE_PROCESS_TYPE(immutable) 388 DEFINE_PROCESS_TYPE(packed) 389 DEFINE_PROCESS_TYPE(pointer) 390 DEFINE_PROCESS_TYPE(reference) 391 DEFINE_PROCESS_TYPE(restrict) 392 DEFINE_PROCESS_TYPE(rvalue_reference) 393 DEFINE_PROCESS_TYPE(shared) 394 DEFINE_PROCESS_TYPE(template_type_parameter) 395 DEFINE_PROCESS_TYPE(volatile) 396 DEFINE_PROCESS_TYPE(typedef) 397 398 static void process_subrange_type(struct state *state, struct die *cache, 399 Dwarf_Die *die) 400 { 401 Dwarf_Word count = 0; 402 403 if (get_udata_attr(die, DW_AT_count, &count)) 404 process_fmt(cache, "[%" PRIu64 "]", count); 405 else if (get_udata_attr(die, DW_AT_upper_bound, &count)) 406 process_fmt(cache, "[%" PRIu64 "]", count + 1); 407 else 408 process(cache, "[]"); 409 } 410 411 static void process_array_type(struct state *state, struct die *cache, 412 Dwarf_Die *die) 413 { 414 process(cache, "array_type"); 415 /* Array size */ 416 check(process_die_container(state, cache, die, process_type, 417 match_subrange_type)); 418 process(cache, " {"); 419 process_linebreak(cache, 1); 420 process_type_attr(state, cache, die); 421 process_linebreak(cache, -1); 422 process(cache, "}"); 423 } 424 425 static void __process_subroutine_type(struct state *state, struct die *cache, 426 Dwarf_Die *die, const char *type) 427 { 428 process(cache, type); 429 process(cache, " ("); 430 process_linebreak(cache, 1); 431 /* Parameters */ 432 check(process_die_container(state, cache, die, process_type, 433 match_formal_parameter_type)); 434 process_linebreak(cache, -1); 435 process(cache, ")"); 436 process_linebreak(cache, 0); 437 /* Return type */ 438 process(cache, "-> "); 439 process_type_attr(state, cache, die); 440 } 441 442 static void process_subroutine_type(struct state *state, struct die *cache, 443 Dwarf_Die *die) 444 { 445 __process_subroutine_type(state, cache, die, "subroutine_type"); 446 } 447 448 static void process_variant_type(struct state *state, struct die *cache, 449 Dwarf_Die *die) 450 { 451 process_list_comma(state, cache); 452 process(cache, "variant {"); 453 process_linebreak(cache, 1); 454 check(process_die_container(state, cache, die, process_type, 455 match_member_type)); 456 process_linebreak(cache, -1); 457 process(cache, "}"); 458 process_discr_value_attr(cache, die); 459 } 460 461 static void process_variant_part_type(struct state *state, struct die *cache, 462 Dwarf_Die *die) 463 { 464 process_list_comma(state, cache); 465 process(cache, "variant_part {"); 466 process_linebreak(cache, 1); 467 check(process_die_container(state, cache, die, process_type, 468 match_all)); 469 process_linebreak(cache, -1); 470 process(cache, "}"); 471 } 472 473 static int get_kabi_status(Dwarf_Die *die, const char **suffix) 474 { 475 const char *name = get_name_attr(die); 476 477 if (suffix) 478 *suffix = NULL; 479 480 if (is_kabi_prefix(name)) { 481 name += KABI_PREFIX_LEN; 482 483 if (!strncmp(name, KABI_RESERVED_PREFIX, 484 KABI_RESERVED_PREFIX_LEN)) 485 return KABI_RESERVED; 486 if (!strncmp(name, KABI_IGNORED_PREFIX, 487 KABI_IGNORED_PREFIX_LEN)) 488 return KABI_IGNORED; 489 490 if (!strncmp(name, KABI_RENAMED_PREFIX, 491 KABI_RENAMED_PREFIX_LEN)) { 492 if (suffix) { 493 name += KABI_RENAMED_PREFIX_LEN; 494 *suffix = name; 495 } 496 return KABI_RESERVED; 497 } 498 } 499 500 return KABI_NORMAL; 501 } 502 503 static int check_struct_member_kabi_status(struct state *state, 504 struct die *__unused, Dwarf_Die *die) 505 { 506 int res; 507 508 assert(dwarf_tag(die) == DW_TAG_member_type); 509 510 /* 511 * If the union member is a struct, expect the __kabi field to 512 * be the first member of the structure, i.e..: 513 * 514 * union { 515 * type new_member; 516 * struct { 517 * type __kabi_field; 518 * } 519 * }; 520 */ 521 res = get_kabi_status(die, &state->kabi.orig_name); 522 523 if (res == KABI_RESERVED && 524 !get_ref_die_attr(die, DW_AT_type, &state->kabi.placeholder)) 525 error("structure member missing a type?"); 526 527 return res; 528 } 529 530 static int check_union_member_kabi_status(struct state *state, 531 struct die *__unused, Dwarf_Die *die) 532 { 533 Dwarf_Die type; 534 int res; 535 536 assert(dwarf_tag(die) == DW_TAG_member_type); 537 538 if (!get_ref_die_attr(die, DW_AT_type, &type)) 539 error("union member missing a type?"); 540 541 /* 542 * We expect a union with two members. Check if either of them 543 * has a __kabi name prefix, i.e.: 544 * 545 * union { 546 * ... 547 * type memberN; // <- type, N = {0,1} 548 * ... 549 * }; 550 * 551 * The member can also be a structure type, in which case we'll 552 * check the first structure member. 553 * 554 * In any case, stop processing after we've seen two members. 555 */ 556 res = get_kabi_status(die, &state->kabi.orig_name); 557 558 if (res == KABI_RESERVED) 559 state->kabi.placeholder = type; 560 if (res != KABI_NORMAL) 561 return res; 562 563 if (dwarf_tag(&type) == DW_TAG_structure_type) 564 res = checkp(process_die_container( 565 state, NULL, &type, check_struct_member_kabi_status, 566 match_member_type)); 567 568 if (res <= KABI_NORMAL && ++state->kabi.members < 2) 569 return 0; /* Continue */ 570 571 return res; 572 } 573 574 static int get_union_kabi_status(Dwarf_Die *die, Dwarf_Die *placeholder, 575 const char **orig_name) 576 { 577 struct state state; 578 int res; 579 580 if (!stable) 581 return KABI_NORMAL; 582 583 /* 584 * To maintain a stable kABI, distributions may choose to reserve 585 * space in structs for later use by adding placeholder members, 586 * for example: 587 * 588 * struct s { 589 * u32 a; 590 * // an 8-byte placeholder for future use 591 * u64 __kabi_reserved_0; 592 * }; 593 * 594 * When the reserved member is taken into use, the type change 595 * would normally cause the symbol version to change as well, but 596 * if the replacement uses the following convention, gendwarfksyms 597 * continues to use the placeholder type for versioning instead, 598 * thus maintaining the same symbol version: 599 * 600 * struct s { 601 * u32 a; 602 * union { 603 * // placeholder replaced with a new member `b` 604 * struct t b; 605 * struct { 606 * // the placeholder type that is still 607 * // used for versioning 608 * u64 __kabi_reserved_0; 609 * }; 610 * }; 611 * }; 612 * 613 * I.e., as long as the replaced member is in a union, and the 614 * placeholder has a __kabi_reserved name prefix, we'll continue 615 * to use the placeholder type (here u64) for version calculation 616 * instead of the union type. 617 * 618 * It's also possible to ignore new members from versioning if 619 * they've been added to alignment holes, for example, by 620 * including them in a union with another member that uses the 621 * __kabi_ignored name prefix: 622 * 623 * struct s { 624 * u32 a; 625 * // an alignment hole is used to add `n` 626 * union { 627 * u32 n; 628 * // hide the entire union member from versioning 629 * u8 __kabi_ignored_0; 630 * }; 631 * u64 b; 632 * }; 633 * 634 * Note that the user of this feature is responsible for ensuring 635 * that the structure actually remains ABI compatible. 636 */ 637 memset(&state.kabi, 0, sizeof(struct kabi_state)); 638 639 res = checkp(process_die_container(&state, NULL, die, 640 check_union_member_kabi_status, 641 match_member_type)); 642 643 if (res == KABI_RESERVED) { 644 if (placeholder) 645 *placeholder = state.kabi.placeholder; 646 if (orig_name) 647 *orig_name = state.kabi.orig_name; 648 } 649 650 return res; 651 } 652 653 static bool is_kabi_ignored(Dwarf_Die *die) 654 { 655 Dwarf_Die type; 656 657 if (!stable) 658 return false; 659 660 if (!get_ref_die_attr(die, DW_AT_type, &type)) 661 error("member missing a type?"); 662 663 return dwarf_tag(&type) == DW_TAG_union_type && 664 checkp(get_union_kabi_status(&type, NULL, NULL)) == KABI_IGNORED; 665 } 666 667 static int ___process_structure_type(struct state *state, struct die *cache, 668 Dwarf_Die *die) 669 { 670 switch (dwarf_tag(die)) { 671 case DW_TAG_member: 672 if (is_kabi_ignored(die)) 673 return 0; 674 return check(process_type(state, cache, die)); 675 case DW_TAG_variant_part: 676 return check(process_type(state, cache, die)); 677 case DW_TAG_class_type: 678 case DW_TAG_enumeration_type: 679 case DW_TAG_structure_type: 680 case DW_TAG_template_type_parameter: 681 case DW_TAG_union_type: 682 case DW_TAG_subprogram: 683 /* Skip non-member types, including member functions */ 684 return 0; 685 default: 686 error("unexpected structure_type child: %x", dwarf_tag(die)); 687 } 688 } 689 690 static void __process_structure_type(struct state *state, struct die *cache, 691 Dwarf_Die *die, const char *type, 692 die_callback_t process_func, 693 die_match_callback_t match_func) 694 { 695 bool expand; 696 697 process(cache, type); 698 process_fqn(cache, die); 699 process(cache, " {"); 700 process_linebreak(cache, 1); 701 702 expand = state->expand.expand && is_kabi_definition(cache, die); 703 704 if (expand) { 705 state->expand.current_fqn = cache->fqn; 706 check(process_die_container(state, cache, die, process_func, 707 match_func)); 708 } 709 710 process_linebreak(cache, -1); 711 process(cache, "}"); 712 713 if (expand) { 714 process_byte_size_attr(cache, die); 715 process_alignment_attr(cache, die); 716 } 717 } 718 719 #define DEFINE_PROCESS_STRUCTURE_TYPE(structure) \ 720 static void process_##structure##_type( \ 721 struct state *state, struct die *cache, Dwarf_Die *die) \ 722 { \ 723 __process_structure_type(state, cache, die, \ 724 #structure "_type", \ 725 ___process_structure_type, \ 726 match_all); \ 727 } 728 729 DEFINE_PROCESS_STRUCTURE_TYPE(class) 730 DEFINE_PROCESS_STRUCTURE_TYPE(structure) 731 732 static void process_union_type(struct state *state, struct die *cache, 733 Dwarf_Die *die) 734 { 735 Dwarf_Die placeholder; 736 737 int res = checkp(get_union_kabi_status(die, &placeholder, 738 &state->kabi.orig_name)); 739 740 if (res == KABI_RESERVED) 741 check(process_type(state, cache, &placeholder)); 742 if (res > KABI_NORMAL) 743 return; 744 745 __process_structure_type(state, cache, die, "union_type", 746 ___process_structure_type, match_all); 747 } 748 749 static void process_enumerator_type(struct state *state, struct die *cache, 750 Dwarf_Die *die) 751 { 752 bool overridden = false; 753 Dwarf_Word value; 754 755 if (stable) { 756 /* Get the fqn before we process anything */ 757 update_fqn(cache, die); 758 759 if (kabi_is_enumerator_ignored(state->expand.current_fqn, 760 cache->fqn)) 761 return; 762 763 overridden = kabi_get_enumerator_value( 764 state->expand.current_fqn, cache->fqn, &value); 765 } 766 767 process_list_comma(state, cache); 768 process(cache, "enumerator"); 769 process_fqn(cache, die); 770 771 if (overridden || get_udata_attr(die, DW_AT_const_value, &value)) { 772 process(cache, " = "); 773 process_fmt(cache, "%" PRIu64, value); 774 } 775 } 776 777 static void process_enumeration_type(struct state *state, struct die *cache, 778 Dwarf_Die *die) 779 { 780 __process_structure_type(state, cache, die, "enumeration_type", 781 process_type, match_enumerator_type); 782 } 783 784 static void process_base_type(struct state *state, struct die *cache, 785 Dwarf_Die *die) 786 { 787 process(cache, "base_type"); 788 process_fqn(cache, die); 789 process_byte_size_attr(cache, die); 790 process_encoding_attr(cache, die); 791 process_alignment_attr(cache, die); 792 } 793 794 static void process_unspecified_type(struct state *state, struct die *cache, 795 Dwarf_Die *die) 796 { 797 /* 798 * These can be emitted for stand-alone assembly code, which means we 799 * might run into them in vmlinux.o. 800 */ 801 process(cache, "unspecified_type"); 802 } 803 804 static void process_cached(struct state *state, struct die *cache, 805 Dwarf_Die *die) 806 { 807 struct die_fragment *df; 808 Dwarf_Die child; 809 810 list_for_each_entry(df, &cache->fragments, list) { 811 switch (df->type) { 812 case FRAGMENT_STRING: 813 die_debug_b("cache %p STRING '%s'", cache, 814 df->data.str); 815 process(NULL, df->data.str); 816 break; 817 case FRAGMENT_LINEBREAK: 818 process_linebreak(NULL, df->data.linebreak); 819 break; 820 case FRAGMENT_DIE: 821 if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu), 822 (void *)df->data.addr, &child)) 823 error("dwarf_die_addr_die failed"); 824 die_debug_b("cache %p DIE addr %" PRIxPTR " tag %x", 825 cache, df->data.addr, dwarf_tag(&child)); 826 check(process_type(state, NULL, &child)); 827 break; 828 default: 829 error("empty die_fragment"); 830 } 831 } 832 } 833 834 static void state_init(struct state *state) 835 { 836 state->expand.expand = true; 837 state->expand.current_fqn = NULL; 838 cache_init(&state->expansion_cache); 839 } 840 841 static void expansion_state_restore(struct expansion_state *state, 842 struct expansion_state *saved) 843 { 844 state->expand = saved->expand; 845 state->current_fqn = saved->current_fqn; 846 } 847 848 static void expansion_state_save(struct expansion_state *state, 849 struct expansion_state *saved) 850 { 851 expansion_state_restore(saved, state); 852 } 853 854 static bool is_expanded_type(int tag) 855 { 856 return tag == DW_TAG_class_type || tag == DW_TAG_structure_type || 857 tag == DW_TAG_union_type || tag == DW_TAG_enumeration_type; 858 } 859 860 #define PROCESS_TYPE(type) \ 861 case DW_TAG_##type##_type: \ 862 process_##type##_type(state, cache, die); \ 863 break; 864 865 static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) 866 { 867 enum die_state want_state = DIE_COMPLETE; 868 struct die *cache; 869 struct expansion_state saved; 870 int tag = dwarf_tag(die); 871 872 expansion_state_save(&state->expand, &saved); 873 874 /* 875 * Structures and enumeration types are expanded only once per 876 * exported symbol. This is sufficient for detecting ABI changes 877 * within the structure. 878 */ 879 if (is_expanded_type(tag)) { 880 if (cache_was_expanded(&state->expansion_cache, die->addr)) 881 state->expand.expand = false; 882 883 if (state->expand.expand) 884 cache_mark_expanded(&state->expansion_cache, die->addr); 885 else 886 want_state = DIE_UNEXPANDED; 887 } 888 889 /* 890 * If we have want_state already cached, use it instead of walking 891 * through DWARF. 892 */ 893 cache = die_map_get(die, want_state); 894 895 if (cache->state == want_state) { 896 die_debug_g("cached addr %p tag %x -- %s", die->addr, tag, 897 die_state_name(cache->state)); 898 899 process_cached(state, cache, die); 900 die_map_add_die(parent, cache); 901 902 expansion_state_restore(&state->expand, &saved); 903 return 0; 904 } 905 906 die_debug_g("addr %p tag %x -- %s -> %s", die->addr, tag, 907 die_state_name(cache->state), die_state_name(want_state)); 908 909 switch (tag) { 910 /* Type modifiers */ 911 PROCESS_TYPE(atomic) 912 PROCESS_TYPE(const) 913 PROCESS_TYPE(immutable) 914 PROCESS_TYPE(packed) 915 PROCESS_TYPE(pointer) 916 PROCESS_TYPE(reference) 917 PROCESS_TYPE(restrict) 918 PROCESS_TYPE(rvalue_reference) 919 PROCESS_TYPE(shared) 920 PROCESS_TYPE(volatile) 921 /* Container types */ 922 PROCESS_TYPE(class) 923 PROCESS_TYPE(structure) 924 PROCESS_TYPE(union) 925 PROCESS_TYPE(enumeration) 926 /* Subtypes */ 927 PROCESS_TYPE(enumerator) 928 PROCESS_TYPE(formal_parameter) 929 PROCESS_TYPE(member) 930 PROCESS_TYPE(subrange) 931 PROCESS_TYPE(template_type_parameter) 932 PROCESS_TYPE(variant) 933 PROCESS_TYPE(variant_part) 934 /* Other types */ 935 PROCESS_TYPE(array) 936 PROCESS_TYPE(base) 937 PROCESS_TYPE(subroutine) 938 PROCESS_TYPE(typedef) 939 PROCESS_TYPE(unspecified) 940 default: 941 error("unexpected type: %x", tag); 942 } 943 944 die_debug_r("parent %p cache %p die addr %p tag %x", parent, cache, 945 die->addr, tag); 946 947 /* Update cache state and append to the parent (if any) */ 948 cache->tag = tag; 949 cache->state = want_state; 950 die_map_add_die(parent, cache); 951 952 expansion_state_restore(&state->expand, &saved); 953 return 0; 954 } 955 956 /* 957 * Exported symbol processing 958 */ 959 static struct die *get_symbol_cache(struct state *state, Dwarf_Die *die) 960 { 961 struct die *cache; 962 963 cache = die_map_get(die, DIE_SYMBOL); 964 965 if (cache->state != DIE_INCOMPLETE) 966 return NULL; /* We already processed a symbol for this DIE */ 967 968 cache->tag = dwarf_tag(die); 969 return cache; 970 } 971 972 static void process_symbol(struct state *state, Dwarf_Die *die, 973 die_callback_t process_func) 974 { 975 struct die *cache; 976 977 symbol_set_die(state->sym, die); 978 979 cache = get_symbol_cache(state, die); 980 if (!cache) 981 return; 982 983 debug("%s", state->sym->name); 984 check(process_func(state, cache, die)); 985 cache->state = DIE_SYMBOL; 986 if (dump_dies) 987 fputs("\n", stderr); 988 } 989 990 static int __process_subprogram(struct state *state, struct die *cache, 991 Dwarf_Die *die) 992 { 993 __process_subroutine_type(state, cache, die, "subprogram"); 994 return 0; 995 } 996 997 static void process_subprogram(struct state *state, Dwarf_Die *die) 998 { 999 process_symbol(state, die, __process_subprogram); 1000 } 1001 1002 static int __process_variable(struct state *state, struct die *cache, 1003 Dwarf_Die *die) 1004 { 1005 process(cache, "variable "); 1006 process_type_attr(state, cache, die); 1007 return 0; 1008 } 1009 1010 static void process_variable(struct state *state, Dwarf_Die *die) 1011 { 1012 process_symbol(state, die, __process_variable); 1013 } 1014 1015 static void save_symbol_ptr(struct state *state) 1016 { 1017 Dwarf_Die ptr_type; 1018 Dwarf_Die type; 1019 1020 if (!get_ref_die_attr(&state->die, DW_AT_type, &ptr_type) || 1021 dwarf_tag(&ptr_type) != DW_TAG_pointer_type) 1022 error("%s must be a pointer type!", 1023 get_symbol_name(&state->die)); 1024 1025 if (!get_ref_die_attr(&ptr_type, DW_AT_type, &type)) 1026 error("%s pointer missing a type attribute?", 1027 get_symbol_name(&state->die)); 1028 1029 /* 1030 * Save the symbol pointer DIE in case the actual symbol is 1031 * missing from the DWARF. Clang, for example, intentionally 1032 * omits external symbols from the debugging information. 1033 */ 1034 if (dwarf_tag(&type) == DW_TAG_subroutine_type) 1035 symbol_set_ptr(state->sym, &type); 1036 else 1037 symbol_set_ptr(state->sym, &ptr_type); 1038 } 1039 1040 static int process_exported_symbols(struct state *unused, struct die *cache, 1041 Dwarf_Die *die) 1042 { 1043 int tag = dwarf_tag(die); 1044 1045 switch (tag) { 1046 /* Possible containers of exported symbols */ 1047 case DW_TAG_namespace: 1048 case DW_TAG_class_type: 1049 case DW_TAG_structure_type: 1050 return check(process_die_container( 1051 NULL, cache, die, process_exported_symbols, match_all)); 1052 1053 /* Possible exported symbols */ 1054 case DW_TAG_subprogram: 1055 case DW_TAG_variable: { 1056 struct state state; 1057 1058 if (!match_export_symbol(&state, die)) 1059 return 0; 1060 1061 state_init(&state); 1062 1063 if (is_symbol_ptr(get_symbol_name(&state.die))) 1064 save_symbol_ptr(&state); 1065 else if (tag == DW_TAG_subprogram) 1066 process_subprogram(&state, &state.die); 1067 else 1068 process_variable(&state, &state.die); 1069 1070 cache_free(&state.expansion_cache); 1071 return 0; 1072 } 1073 default: 1074 return 0; 1075 } 1076 } 1077 1078 static void process_symbol_ptr(struct symbol *sym, void *arg) 1079 { 1080 struct state state; 1081 Dwarf *dwarf = arg; 1082 1083 if (sym->state != SYMBOL_UNPROCESSED || !sym->ptr_die_addr) 1084 return; 1085 1086 debug("%s", sym->name); 1087 state_init(&state); 1088 state.sym = sym; 1089 1090 if (!dwarf_die_addr_die(dwarf, (void *)sym->ptr_die_addr, &state.die)) 1091 error("dwarf_die_addr_die failed for symbol ptr: '%s'", 1092 sym->name); 1093 1094 if (dwarf_tag(&state.die) == DW_TAG_subroutine_type) 1095 process_subprogram(&state, &state.die); 1096 else 1097 process_variable(&state, &state.die); 1098 1099 cache_free(&state.expansion_cache); 1100 } 1101 1102 static int resolve_fqns(struct state *parent, struct die *unused, 1103 Dwarf_Die *die) 1104 { 1105 struct state state; 1106 struct die *cache; 1107 const char *name; 1108 bool use_prefix; 1109 char *prefix = NULL; 1110 char *fqn = ""; 1111 int tag; 1112 1113 if (!__die_map_get((uintptr_t)die->addr, DIE_FQN, &cache)) 1114 return 0; 1115 1116 tag = dwarf_tag(die); 1117 1118 /* 1119 * Only namespaces and structures need to pass a prefix to the next 1120 * scope. 1121 */ 1122 use_prefix = tag == DW_TAG_namespace || tag == DW_TAG_class_type || 1123 tag == DW_TAG_structure_type; 1124 1125 state.expand.current_fqn = NULL; 1126 name = get_name_attr(die); 1127 1128 if (parent && parent->expand.current_fqn && (use_prefix || name)) { 1129 /* 1130 * The fqn for the current DIE, and if needed, a prefix for the 1131 * next scope. 1132 */ 1133 if (asprintf(&prefix, "%s::%s", parent->expand.current_fqn, 1134 name ? name : "<anonymous>") < 0) 1135 error("asprintf failed"); 1136 1137 if (use_prefix) 1138 state.expand.current_fqn = prefix; 1139 1140 /* 1141 * Use fqn only if the DIE has a name. Otherwise fqn will 1142 * remain empty. 1143 */ 1144 if (name) { 1145 fqn = prefix; 1146 /* prefix will be freed by die_map. */ 1147 prefix = NULL; 1148 } 1149 } else if (name) { 1150 /* No prefix from the previous scope. Use only the name. */ 1151 fqn = xstrdup(name); 1152 1153 if (use_prefix) 1154 state.expand.current_fqn = fqn; 1155 } 1156 1157 /* If the DIE has a non-empty name, cache it. */ 1158 if (*fqn) { 1159 cache = die_map_get(die, DIE_FQN); 1160 /* Move ownership of fqn to die_map. */ 1161 cache->fqn = fqn; 1162 cache->state = DIE_FQN; 1163 } 1164 1165 check(process_die_container(&state, NULL, die, resolve_fqns, 1166 match_all)); 1167 1168 free(prefix); 1169 return 0; 1170 } 1171 1172 void process_cu(Dwarf_Die *cudie) 1173 { 1174 check(process_die_container(NULL, NULL, cudie, resolve_fqns, 1175 match_all)); 1176 1177 check(process_die_container(NULL, NULL, cudie, process_exported_symbols, 1178 match_all)); 1179 1180 symbol_for_each(process_symbol_ptr, dwarf_cu_getdwarf(cudie->cu)); 1181 1182 cache_free(&srcfile_cache); 1183 } 1184