1/* Print information from ELF file in human-readable form. 2 Copyright (C) 1999-2018 Red Hat, Inc. 3 This file is part of elfutils. 4 5 This file is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 elfutils is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18#ifdef HAVE_CONFIG_H 19# include <config.h> 20#endif 21 22#include <argp.h> 23#include <assert.h> 24#include <ctype.h> 25#include <dwarf.h> 26#include <errno.h> 27#include <fcntl.h> 28#include <gelf.h> 29#include <inttypes.h> 30#include <langinfo.h> 31#include <libdw.h> 32#include <libdwfl.h> 33#include <locale.h> 34#include <stdarg.h> 35#include <stdbool.h> 36#include <stdio.h> 37#include <stdio_ext.h> 38#include <stdlib.h> 39#include <string.h> 40#include <strings.h> 41#include <time.h> 42#include <unistd.h> 43#include <sys/stat.h> 44#include <signal.h> 45 46#include <libeu.h> 47#include <system.h> 48#include <printversion.h> 49#include "../libelf/libelfP.h" 50#include "../libelf/common.h" 51#include "../libebl/libeblP.h" 52#include "../libdwelf/libdwelf.h" 53#include "../libdw/libdwP.h" 54#include "../libdwfl/libdwflP.h" 55#include "../libdw/memory-access.h" 56 57#include "../libdw/known-dwarf.h" 58 59#ifdef __linux__ 60#define CORE_SIGILL SIGILL 61#define CORE_SIGBUS SIGBUS 62#define CORE_SIGFPE SIGFPE 63#define CORE_SIGSEGV SIGSEGV 64#define CORE_SI_USER SI_USER 65#else 66/* We want the linux version of those as that is what shows up in the core files. */ 67#define CORE_SIGILL 4 /* Illegal instruction (ANSI). */ 68#define CORE_SIGBUS 7 /* BUS error (4.2 BSD). */ 69#define CORE_SIGFPE 8 /* Floating-point exception (ANSI). */ 70#define CORE_SIGSEGV 11 /* Segmentation violation (ANSI). */ 71#define CORE_SI_USER 0 /* Sent by kill, sigsend. */ 72#endif 73 74/* Name and version of program. */ 75ARGP_PROGRAM_VERSION_HOOK_DEF = print_version; 76 77/* Bug report address. */ 78ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT; 79 80/* argp key value for --elf-section, non-ascii. */ 81#define ELF_INPUT_SECTION 256 82 83/* argp key value for --dwarf-skeleton, non-ascii. */ 84#define DWARF_SKELETON 257 85 86/* argp key value for --dyn-syms, non-ascii. */ 87#define PRINT_DYNSYM_TABLE 258 88 89/* Terrible hack for hooking unrelated skeleton/split compile units, 90 see __libdw_link_skel_split in print_debug. */ 91static bool do_not_close_dwfl = false; 92 93/* Definitions of arguments for argp functions. */ 94static const struct argp_option options[] = 95{ 96 { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 }, 97 { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL, 98 N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF " 99 "input data"), 0 }, 100 { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0, 101 N_("Used with -w to find the skeleton Compile Units in FILE associated " 102 "with the Split Compile units in a .dwo input file"), 0 }, 103 { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 }, 104 { "all", 'a', NULL, 0, 105 N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 }, 106 { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 }, 107 { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 }, 108 { "histogram", 'I', NULL, 0, 109 N_("Display histogram of bucket list lengths"), 0 }, 110 { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 }, 111 { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 }, 112 { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 }, 113 { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 }, 114 { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 }, 115 { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 }, 116 { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL, 117 N_("Display the symbol table sections"), 0 }, 118 { "syms", 's', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 }, 119 { "dyn-syms", PRINT_DYNSYM_TABLE, NULL, 0, 120 N_("Display (only) the dynamic symbol table"), 0 }, 121 { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 }, 122 { "notes", 'n', "SECTION", OPTION_ARG_OPTIONAL, N_("Display the ELF notes"), 0 }, 123 { "arch-specific", 'A', NULL, 0, 124 N_("Display architecture specific information, if any"), 0 }, 125 { "exception", 'e', NULL, 0, 126 N_("Display sections for exception handling"), 0 }, 127 128 { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 }, 129 { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL, 130 N_("Display DWARF section content. SECTION can be one of abbrev, addr, " 131 "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, " 132 "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 }, 133 { "hex-dump", 'x', "SECTION", 0, 134 N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 }, 135 { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL, 136 N_("Print string contents of sections"), 0 }, 137 { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 }, 138 { "archive-index", 'c', NULL, 0, 139 N_("Display the symbol index of an archive"), 0 }, 140 { "use-dynamic", 'D', NULL, 0, 141 N_("Use the dynamic segment when possible for displaying info"), 0 }, 142 143 { NULL, 0, NULL, 0, N_("Output control:"), 0 }, 144 { "numeric-addresses", 'N', NULL, 0, 145 N_("Do not find symbol names for addresses in DWARF data"), 0 }, 146 { "unresolved-address-offsets", 'U', NULL, 0, 147 N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 }, 148 { "wide", 'W', NULL, 0, 149 N_("Ignored for compatibility (lines always wide)"), 0 }, 150 { "decompress", 'z', NULL, 0, 151 N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 }, 152 { NULL, 0, NULL, 0, NULL, 0 } 153}; 154 155/* Short description of program. */ 156static const char doc[] = N_("\ 157Print information from ELF file in human-readable form."); 158 159/* Strings for arguments in help texts. */ 160static const char args_doc[] = N_("FILE..."); 161 162/* Prototype for option handler. */ 163static error_t parse_opt (int key, char *arg, struct argp_state *state); 164 165/* Data structure to communicate with argp functions. */ 166static struct argp argp = 167{ 168 options, parse_opt, args_doc, doc, NULL, NULL, NULL 169}; 170 171/* If non-null, the section from which we should read to (compressed) ELF. */ 172static const char *elf_input_section = NULL; 173 174/* If non-null, the file that contains the skeleton CUs. */ 175static const char *dwarf_skeleton = NULL; 176 177/* Flags set by the option controlling the output. */ 178 179/* True if dynamic segment should be printed. */ 180static bool print_dynamic_table; 181 182/* True if the file header should be printed. */ 183static bool print_file_header; 184 185/* True if the program headers should be printed. */ 186static bool print_program_header; 187 188/* True if relocations should be printed. */ 189static bool print_relocations; 190 191/* True if the section headers should be printed. */ 192static bool print_section_header; 193 194/* True if the symbol table should be printed. */ 195static bool print_symbol_table; 196 197/* True if (only) the dynsym table should be printed. */ 198static bool print_dynsym_table; 199 200/* True if reconstruct dynamic symbol table from the PT_DYNAMIC segment. */ 201static bool use_dynamic_segment; 202 203/* A specific section name, or NULL to print all symbol tables. */ 204static char *symbol_table_section; 205 206/* A specific section name, or NULL to print all ELF notes. */ 207static char *notes_section; 208 209/* True if the version information should be printed. */ 210static bool print_version_info; 211 212/* True if section groups should be printed. */ 213static bool print_section_groups; 214 215/* True if bucket list length histogram should be printed. */ 216static bool print_histogram; 217 218/* True if the architecture specific data should be printed. */ 219static bool print_arch; 220 221/* True if note section content should be printed. */ 222static bool print_notes; 223 224/* True if SHF_STRINGS section content should be printed. */ 225static bool print_string_sections; 226 227/* True if archive index should be printed. */ 228static bool print_archive_index; 229 230/* True if any of the control options except print_archive_index is set. */ 231static bool any_control_option; 232 233/* True if we should print addresses from DWARF in symbolic form. */ 234static bool print_address_names = true; 235 236/* True if we should print raw values instead of relativized addresses. */ 237static bool print_unresolved_addresses = false; 238 239/* True if we should print the .debug_aranges section using libdw. */ 240static bool decodedaranges = false; 241 242/* True if we should print the .debug_aranges section using libdw. */ 243static bool decodedline = false; 244 245/* True if we want to show more information about compressed sections. */ 246static bool print_decompress = false; 247 248/* True if we want to show split compile units for debug_info skeletons. */ 249static bool show_split_units = false; 250 251/* Select printing of debugging sections. */ 252static enum section_e 253{ 254 section_abbrev = 1, /* .debug_abbrev */ 255 section_aranges = 2, /* .debug_aranges */ 256 section_frame = 4, /* .debug_frame or .eh_frame & al. */ 257 section_info = 8, /* .debug_info, (implies .debug_types) */ 258 section_line = 16, /* .debug_line */ 259 section_loc = 32, /* .debug_loc */ 260 section_pubnames = 64, /* .debug_pubnames */ 261 section_str = 128, /* .debug_str */ 262 section_macinfo = 256, /* .debug_macinfo */ 263 section_ranges = 512, /* .debug_ranges */ 264 section_exception = 1024, /* .eh_frame & al. */ 265 section_gdb_index = 2048, /* .gdb_index */ 266 section_macro = 4096, /* .debug_macro */ 267 section_addr = 8192, /* .debug_addr */ 268 section_types = 16384, /* .debug_types (implied by .debug_info) */ 269 section_all = (section_abbrev | section_aranges | section_frame 270 | section_info | section_line | section_loc 271 | section_pubnames | section_str | section_macinfo 272 | section_ranges | section_exception | section_gdb_index 273 | section_macro | section_addr | section_types) 274} print_debug_sections, implicit_debug_sections; 275 276/* Select hex dumping of sections. */ 277static struct section_argument *dump_data_sections; 278static struct section_argument **dump_data_sections_tail = &dump_data_sections; 279 280/* Select string dumping of sections. */ 281static struct section_argument *string_sections; 282static struct section_argument **string_sections_tail = &string_sections; 283 284struct section_argument 285{ 286 struct section_argument *next; 287 const char *arg; 288 bool implicit; 289}; 290 291/* Numbers of sections and program headers in the file. */ 292static size_t shnum; 293static size_t phnum; 294 295 296/* Declarations of local functions. */ 297static void process_file (int fd, const char *fname, bool only_one); 298static void process_elf_file (Dwfl_Module *dwflmod, int fd); 299static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr); 300static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr); 301static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr); 302static void print_scngrp (Ebl *ebl); 303static void print_dynamic (Ebl *ebl); 304static void print_relocs (Ebl *ebl, GElf_Ehdr *ehdr); 305static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, 306 GElf_Shdr *shdr); 307static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, 308 GElf_Shdr *shdr); 309static void print_symtab (Ebl *ebl, int type); 310static void handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr); 311static void print_verinfo (Ebl *ebl); 312static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr); 313static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr); 314static void handle_versym (Ebl *ebl, Elf_Scn *scn, 315 GElf_Shdr *shdr); 316static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr); 317static void handle_hash (Ebl *ebl); 318static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr); 319static void print_liblist (Ebl *ebl); 320static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr); 321static void dump_data (Ebl *ebl); 322static void dump_strings (Ebl *ebl); 323static void print_strings (Ebl *ebl); 324static void dump_archive_index (Elf *, const char *); 325 326enum dyn_idx 327{ 328 i_strsz, 329 i_verneed, 330 i_verdef, 331 i_versym, 332 i_symtab, 333 i_strtab, 334 i_hash, 335 i_gnu_hash, 336 i_max 337}; 338 339/* Declarations of local functions for use-dynamic. */ 340static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr); 341static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]); 342static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n, 343 GElf_Addr addrs[n], GElf_Off offs[n]); 344 345/* Looked up once with gettext in main. */ 346static char *yes_str; 347static char *no_str; 348 349static void 350cleanup_list (struct section_argument *list) 351{ 352 while (list != NULL) 353 { 354 struct section_argument *a = list; 355 list = a->next; 356 free (a); 357 } 358} 359 360int 361main (int argc, char *argv[]) 362{ 363 /* We use no threads here which can interfere with handling a stream. */ 364 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); 365 366 /* Set locale. */ 367 setlocale (LC_ALL, ""); 368 369 /* Initialize the message catalog. */ 370 textdomain (PACKAGE_TARNAME); 371 372 /* Look up once. */ 373 yes_str = _("yes"); 374 no_str = _("no"); 375 376 /* Parse and process arguments. */ 377 int remaining; 378 argp_parse (&argp, argc, argv, 0, &remaining, NULL); 379 380 /* Before we start tell the ELF library which version we are using. */ 381 elf_version (EV_CURRENT); 382 383 /* Now process all the files given at the command line. */ 384 bool only_one = remaining + 1 == argc; 385 do 386 { 387 /* Open the file. */ 388 int fd = open (argv[remaining], O_RDONLY); 389 if (fd == -1) 390 { 391 error (0, errno, _("cannot open input file '%s'"), argv[remaining]); 392 continue; 393 } 394 395 process_file (fd, argv[remaining], only_one); 396 397 close (fd); 398 } 399 while (++remaining < argc); 400 401 cleanup_list (dump_data_sections); 402 cleanup_list (string_sections); 403 404 return error_message_count != 0; 405} 406 407static void 408add_dump_section (const char *name, 409 int key, 410 bool implicit) 411{ 412 struct section_argument *a = xmalloc (sizeof *a); 413 a->arg = name; 414 a->next = NULL; 415 a->implicit = implicit; 416 struct section_argument ***tailp 417 = key == 'x' ? &dump_data_sections_tail : &string_sections_tail; 418 **tailp = a; 419 *tailp = &a->next; 420} 421 422/* Handle program arguments. */ 423static error_t 424parse_opt (int key, char *arg, 425 struct argp_state *state __attribute__ ((unused))) 426{ 427 switch (key) 428 { 429 case 'a': 430 print_file_header = true; 431 print_program_header = true; 432 print_relocations = true; 433 print_section_header = true; 434 print_symbol_table = true; 435 print_version_info = true; 436 print_dynamic_table = true; 437 print_section_groups = true; 438 print_histogram = true; 439 print_arch = true; 440 print_notes = true; 441 implicit_debug_sections |= section_exception; 442 add_dump_section (".strtab", key, true); 443 add_dump_section (".dynstr", key, true); 444 add_dump_section (".comment", key, true); 445 any_control_option = true; 446 break; 447 case 'A': 448 print_arch = true; 449 any_control_option = true; 450 break; 451 case 'd': 452 print_dynamic_table = true; 453 any_control_option = true; 454 break; 455 case 'D': 456 use_dynamic_segment = true; 457 break; 458 case 'e': 459 print_debug_sections |= section_exception; 460 any_control_option = true; 461 break; 462 case 'g': 463 print_section_groups = true; 464 any_control_option = true; 465 break; 466 case 'h': 467 print_file_header = true; 468 any_control_option = true; 469 break; 470 case 'I': 471 print_histogram = true; 472 any_control_option = true; 473 break; 474 case 'l': 475 print_program_header = true; 476 any_control_option = true; 477 break; 478 case 'n': 479 print_notes = true; 480 any_control_option = true; 481 notes_section = arg; 482 break; 483 case 'r': 484 print_relocations = true; 485 any_control_option = true; 486 break; 487 case 'S': 488 print_section_header = true; 489 any_control_option = true; 490 break; 491 case 's': 492 print_symbol_table = true; 493 any_control_option = true; 494 symbol_table_section = arg; 495 break; 496 case PRINT_DYNSYM_TABLE: 497 print_dynsym_table = true; 498 any_control_option = true; 499 break; 500 case 'V': 501 print_version_info = true; 502 any_control_option = true; 503 break; 504 case 'c': 505 print_archive_index = true; 506 break; 507 case 'w': 508 if (arg == NULL) 509 { 510 print_debug_sections = section_all; 511 implicit_debug_sections = section_info; 512 show_split_units = true; 513 } 514 else if (strcmp (arg, "abbrev") == 0) 515 print_debug_sections |= section_abbrev; 516 else if (strcmp (arg, "addr") == 0) 517 { 518 print_debug_sections |= section_addr; 519 implicit_debug_sections |= section_info; 520 } 521 else if (strcmp (arg, "aranges") == 0) 522 print_debug_sections |= section_aranges; 523 else if (strcmp (arg, "decodedaranges") == 0) 524 { 525 print_debug_sections |= section_aranges; 526 decodedaranges = true; 527 } 528 else if (strcmp (arg, "ranges") == 0) 529 { 530 print_debug_sections |= section_ranges; 531 implicit_debug_sections |= section_info; 532 } 533 else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0) 534 print_debug_sections |= section_frame; 535 else if (strcmp (arg, "info") == 0) 536 { 537 print_debug_sections |= section_info; 538 print_debug_sections |= section_types; 539 } 540 else if (strcmp (arg, "info+") == 0) 541 { 542 print_debug_sections |= section_info; 543 print_debug_sections |= section_types; 544 show_split_units = true; 545 } 546 else if (strcmp (arg, "loc") == 0) 547 { 548 print_debug_sections |= section_loc; 549 implicit_debug_sections |= section_info; 550 } 551 else if (strcmp (arg, "line") == 0) 552 print_debug_sections |= section_line; 553 else if (strcmp (arg, "decodedline") == 0) 554 { 555 print_debug_sections |= section_line; 556 decodedline = true; 557 } 558 else if (strcmp (arg, "pubnames") == 0) 559 print_debug_sections |= section_pubnames; 560 else if (strcmp (arg, "str") == 0) 561 { 562 print_debug_sections |= section_str; 563 /* For mapping string offset tables to CUs. */ 564 implicit_debug_sections |= section_info; 565 } 566 else if (strcmp (arg, "macinfo") == 0) 567 print_debug_sections |= section_macinfo; 568 else if (strcmp (arg, "macro") == 0) 569 print_debug_sections |= section_macro; 570 else if (strcmp (arg, "exception") == 0) 571 print_debug_sections |= section_exception; 572 else if (strcmp (arg, "gdb_index") == 0) 573 print_debug_sections |= section_gdb_index; 574 else 575 { 576 fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"), 577 arg); 578 argp_help (&argp, stderr, ARGP_HELP_SEE, 579 program_invocation_short_name); 580 exit (1); 581 } 582 any_control_option = true; 583 break; 584 case 'p': 585 any_control_option = true; 586 if (arg == NULL) 587 { 588 print_string_sections = true; 589 break; 590 } 591 FALLTHROUGH; 592 case 'x': 593 add_dump_section (arg, key, false); 594 any_control_option = true; 595 break; 596 case 'N': 597 print_address_names = false; 598 break; 599 case 'U': 600 print_unresolved_addresses = true; 601 break; 602 case ARGP_KEY_NO_ARGS: 603 fputs (_("Missing file name.\n"), stderr); 604 goto do_argp_help; 605 case ARGP_KEY_FINI: 606 if (! any_control_option && ! print_archive_index) 607 { 608 fputs (_("No operation specified.\n"), stderr); 609 do_argp_help: 610 argp_help (&argp, stderr, ARGP_HELP_SEE, 611 program_invocation_short_name); 612 exit (EXIT_FAILURE); 613 } 614 break; 615 case 'W': /* Ignored. */ 616 break; 617 case 'z': 618 print_decompress = true; 619 break; 620 case ELF_INPUT_SECTION: 621 if (arg == NULL) 622 elf_input_section = ".gnu_debugdata"; 623 else 624 elf_input_section = arg; 625 break; 626 case DWARF_SKELETON: 627 dwarf_skeleton = arg; 628 break; 629 default: 630 return ARGP_ERR_UNKNOWN; 631 } 632 return 0; 633} 634 635 636/* Create a file descriptor to read the data from the 637 elf_input_section given a file descriptor to an ELF file. */ 638static int 639open_input_section (int fd) 640{ 641 size_t shnums; 642 size_t cnt; 643 size_t shstrndx; 644 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL); 645 if (elf == NULL) 646 { 647 error (0, 0, _("cannot generate Elf descriptor: %s"), 648 elf_errmsg (-1)); 649 return -1; 650 } 651 652 if (elf_getshdrnum (elf, &shnums) < 0) 653 { 654 error (0, 0, _("cannot determine number of sections: %s"), 655 elf_errmsg (-1)); 656 open_error: 657 elf_end (elf); 658 return -1; 659 } 660 661 if (elf_getshdrstrndx (elf, &shstrndx) < 0) 662 { 663 error (0, 0, _("cannot get section header string table index")); 664 goto open_error; 665 } 666 667 for (cnt = 0; cnt < shnums; ++cnt) 668 { 669 Elf_Scn *scn = elf_getscn (elf, cnt); 670 if (scn == NULL) 671 { 672 error (0, 0, _("cannot get section: %s"), 673 elf_errmsg (-1)); 674 goto open_error; 675 } 676 677 GElf_Shdr shdr_mem; 678 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 679 if (unlikely (shdr == NULL)) 680 { 681 error (0, 0, _("cannot get section header: %s"), 682 elf_errmsg (-1)); 683 goto open_error; 684 } 685 686 const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name); 687 if (sname == NULL) 688 { 689 error (0, 0, _("cannot get section name")); 690 goto open_error; 691 } 692 693 if (strcmp (sname, elf_input_section) == 0) 694 { 695 Elf_Data *data = elf_rawdata (scn, NULL); 696 if (data == NULL) 697 { 698 error (0, 0, _("cannot get %s content: %s"), 699 sname, elf_errmsg (-1)); 700 goto open_error; 701 } 702 703 /* Create (and immediately unlink) a temporary file to store 704 section data in to create a file descriptor for it. */ 705 const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir; 706 static const char suffix[] = "/readelfXXXXXX"; 707 int tmplen = strlen (tmpdir) + sizeof (suffix); 708 char *tempname = alloca (tmplen); 709 sprintf (tempname, "%s%s", tmpdir, suffix); 710 711 int sfd = mkstemp (tempname); 712 if (sfd == -1) 713 { 714 error (0, 0, _("cannot create temp file '%s'"), 715 tempname); 716 goto open_error; 717 } 718 unlink (tempname); 719 720 ssize_t size = data->d_size; 721 if (write_retry (sfd, data->d_buf, size) != size) 722 { 723 error (0, 0, _("cannot write section data")); 724 goto open_error; 725 } 726 727 if (elf_end (elf) != 0) 728 { 729 error (0, 0, _("error while closing Elf descriptor: %s"), 730 elf_errmsg (-1)); 731 return -1; 732 } 733 734 if (lseek (sfd, 0, SEEK_SET) == -1) 735 { 736 error (0, 0, _("error while rewinding file descriptor")); 737 return -1; 738 } 739 740 return sfd; 741 } 742 } 743 744 /* Named section not found. */ 745 if (elf_end (elf) != 0) 746 error (0, 0, _("error while closing Elf descriptor: %s"), 747 elf_errmsg (-1)); 748 return -1; 749} 750 751/* Check if the file is an archive, and if so dump its index. */ 752static void 753check_archive_index (int fd, const char *fname, bool only_one) 754{ 755 /* Create an `Elf' descriptor. */ 756 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL); 757 if (elf == NULL) 758 error (0, 0, _("cannot generate Elf descriptor: %s"), 759 elf_errmsg (-1)); 760 else 761 { 762 if (elf_kind (elf) == ELF_K_AR) 763 { 764 if (!only_one) 765 printf ("\n%s:\n\n", fname); 766 dump_archive_index (elf, fname); 767 } 768 else 769 error (0, 0, 770 _("'%s' is not an archive, cannot print archive index"), 771 fname); 772 773 /* Now we can close the descriptor. */ 774 if (elf_end (elf) != 0) 775 error (0, 0, _("error while closing Elf descriptor: %s"), 776 elf_errmsg (-1)); 777 } 778} 779 780/* Trivial callback used for checking if we opened an archive. */ 781static int 782count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)), 783 void **userdata __attribute__ ((unused)), 784 const char *name __attribute__ ((unused)), 785 Dwarf_Addr base __attribute__ ((unused)), 786 void *arg) 787{ 788 if (*(bool *) arg) 789 return DWARF_CB_ABORT; 790 *(bool *) arg = true; 791 return DWARF_CB_OK; 792} 793 794struct process_dwflmod_args 795{ 796 int fd; 797 bool only_one; 798}; 799 800static int 801process_dwflmod (Dwfl_Module *dwflmod, 802 void **userdata __attribute__ ((unused)), 803 const char *name __attribute__ ((unused)), 804 Dwarf_Addr base __attribute__ ((unused)), 805 void *arg) 806{ 807 const struct process_dwflmod_args *a = arg; 808 809 /* Print the file name. */ 810 if (!a->only_one) 811 { 812 const char *fname; 813 dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL); 814 815 printf ("\n%s:\n\n", fname); 816 } 817 818 process_elf_file (dwflmod, a->fd); 819 820 return DWARF_CB_OK; 821} 822 823/* Stub libdwfl callback, only the ELF handle already open is ever used. 824 Only used for finding the alternate debug file if the Dwarf comes from 825 the main file. We are not interested in separate debuginfo. */ 826static int 827find_no_debuginfo (Dwfl_Module *mod, 828 void **userdata, 829 const char *modname, 830 Dwarf_Addr base, 831 const char *file_name, 832 const char *debuglink_file, 833 GElf_Word debuglink_crc, 834 char **debuginfo_file_name) 835{ 836 Dwarf_Addr dwbias; 837 dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL); 838 839 /* We are only interested if the Dwarf has been setup on the main 840 elf file but is only missing the alternate debug link. If dwbias 841 hasn't even been setup, this is searching for separate debuginfo 842 for the main elf. We don't care in that case. */ 843 if (dwbias == (Dwarf_Addr) -1) 844 return -1; 845 846 return dwfl_standard_find_debuginfo (mod, userdata, modname, base, 847 file_name, debuglink_file, 848 debuglink_crc, debuginfo_file_name); 849} 850 851static Dwfl * 852create_dwfl (int fd, const char *fname) 853{ 854 /* Duplicate an fd for dwfl_report_offline to swallow. */ 855 int dwfl_fd = dup (fd); 856 if (unlikely (dwfl_fd < 0)) 857 error_exit (errno, "dup"); 858 859 /* Use libdwfl in a trivial way to open the libdw handle for us. 860 This takes care of applying relocations to DWARF data in ET_REL files. */ 861 static const Dwfl_Callbacks callbacks = 862 { 863 .section_address = dwfl_offline_section_address, 864 .find_debuginfo = find_no_debuginfo 865 }; 866 Dwfl *dwfl = dwfl_begin (&callbacks); 867 if (likely (dwfl != NULL)) 868 /* Let 0 be the logical address of the file (or first in archive). */ 869 dwfl->offline_next_address = 0; 870 if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL) 871 { 872 struct stat st; 873 if (fstat (dwfl_fd, &st) != 0) 874 error (0, errno, _("cannot stat input file")); 875 else if (unlikely (st.st_size == 0)) 876 error (0, 0, _("input file is empty")); 877 else 878 error (0, 0, _("failed reading '%s': %s"), 879 fname, dwfl_errmsg (-1)); 880 close (dwfl_fd); /* Consumed on success, not on failure. */ 881 dwfl = NULL; 882 } 883 else 884 dwfl_report_end (dwfl, NULL, NULL); 885 886 return dwfl; 887} 888 889/* Process one input file. */ 890static void 891process_file (int fd, const char *fname, bool only_one) 892{ 893 if (print_archive_index) 894 check_archive_index (fd, fname, only_one); 895 896 if (!any_control_option) 897 return; 898 899 if (elf_input_section != NULL) 900 { 901 /* Replace fname and fd with section content. */ 902 char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2); 903 sprintf (fnname, "%s:%s", fname, elf_input_section); 904 fd = open_input_section (fd); 905 if (fd == -1) 906 { 907 error (0, 0, _("No such section '%s' in '%s'"), 908 elf_input_section, fname); 909 return; 910 } 911 fname = fnname; 912 } 913 914 Dwfl *dwfl = create_dwfl (fd, fname); 915 if (dwfl != NULL) 916 { 917 if (only_one) 918 { 919 /* Clear ONLY_ONE if we have multiple modules, from an archive. */ 920 bool seen = false; 921 only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0; 922 } 923 924 /* Process the one or more modules gleaned from this file. */ 925 struct process_dwflmod_args a = { .fd = fd, .only_one = only_one }; 926 dwfl_getmodules (dwfl, &process_dwflmod, &a, 0); 927 } 928 /* Terrible hack for hooking unrelated skeleton/split compile units, 929 see __libdw_link_skel_split in print_debug. */ 930 if (! do_not_close_dwfl) 931 dwfl_end (dwfl); 932 933 /* Need to close the replaced fd if we created it. Caller takes 934 care of original. */ 935 if (elf_input_section != NULL) 936 close (fd); 937} 938 939/* Check whether there are any compressed sections in the ELF file. */ 940static bool 941elf_contains_chdrs (Elf *elf) 942{ 943 Elf_Scn *scn = NULL; 944 while ((scn = elf_nextscn (elf, scn)) != NULL) 945 { 946 GElf_Shdr shdr_mem; 947 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 948 if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0) 949 return true; 950 } 951 return false; 952} 953 954/* Process one ELF file. */ 955static void 956process_elf_file (Dwfl_Module *dwflmod, int fd) 957{ 958 GElf_Addr dwflbias; 959 Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias); 960 961 GElf_Ehdr ehdr_mem; 962 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem); 963 964 if (ehdr == NULL) 965 { 966 error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1)); 967 return; 968 } 969 970 Ebl *ebl = ebl_openbackend (elf); 971 if (unlikely (ebl == NULL)) 972 { 973 ebl_error: 974 error (0, errno, _("cannot create EBL handle")); 975 return; 976 } 977 978 /* Determine the number of sections. */ 979 if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0)) 980 error_exit (0, _("cannot determine number of sections: %s"), 981 elf_errmsg (-1)); 982 983 /* Determine the number of phdrs. */ 984 if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0)) 985 error_exit (0, _("cannot determine number of program headers: %s"), 986 elf_errmsg (-1)); 987 988 /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and 989 may have applied relocation to some sections. If there are any 990 compressed sections, any pass (or libdw/libdwfl) might have 991 uncompressed them. So we need to get a fresh Elf handle on the 992 file to display those. */ 993 bool print_unchanged = ((print_section_header 994 || print_relocations 995 || dump_data_sections != NULL 996 || print_notes) 997 && (ehdr->e_type == ET_REL 998 || elf_contains_chdrs (ebl->elf))); 999 1000 Elf *pure_elf = NULL; 1001 Ebl *pure_ebl = ebl; 1002 if (print_unchanged) 1003 { 1004 /* Read the file afresh. */ 1005 off_t aroff = elf_getaroff (elf); 1006 pure_elf = dwelf_elf_begin (fd); 1007 if (aroff > 0) 1008 { 1009 /* Archive member. */ 1010 (void) elf_rand (pure_elf, aroff); 1011 Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf); 1012 elf_end (pure_elf); 1013 pure_elf = armem; 1014 } 1015 if (pure_elf == NULL) 1016 { 1017 error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1)); 1018 return; 1019 } 1020 pure_ebl = ebl_openbackend (pure_elf); 1021 if (pure_ebl == NULL) 1022 goto ebl_error; 1023 } 1024 1025 if (print_file_header) 1026 print_ehdr (ebl, ehdr); 1027 if (print_section_header) 1028 print_shdr (pure_ebl, ehdr); 1029 if (print_program_header) 1030 print_phdr (ebl, ehdr); 1031 if (print_section_groups) 1032 print_scngrp (ebl); 1033 if (print_dynamic_table) 1034 print_dynamic (ebl); 1035 if (print_relocations) 1036 print_relocs (pure_ebl, ehdr); 1037 if (print_histogram) 1038 handle_hash (ebl); 1039 if (print_symbol_table || print_dynsym_table) 1040 print_symtab (ebl, SHT_DYNSYM); 1041 if (print_version_info) 1042 print_verinfo (ebl); 1043 if (print_symbol_table) 1044 print_symtab (ebl, SHT_SYMTAB); 1045 if (print_arch) 1046 print_liblist (ebl); 1047 if (print_arch) 1048 print_attributes (ebl, ehdr); 1049 if (dump_data_sections != NULL) 1050 dump_data (pure_ebl); 1051 if (string_sections != NULL) 1052 dump_strings (ebl); 1053 if ((print_debug_sections | implicit_debug_sections) != 0) 1054 print_debug (dwflmod, ebl, ehdr); 1055 if (print_notes) 1056 handle_notes (pure_ebl, ehdr); 1057 if (print_string_sections) 1058 print_strings (ebl); 1059 1060 ebl_closebackend (ebl); 1061 1062 if (pure_ebl != ebl) 1063 { 1064 ebl_closebackend (pure_ebl); 1065 elf_end (pure_elf); 1066 } 1067} 1068 1069 1070/* Print file type. */ 1071static void 1072print_file_type (unsigned short int e_type) 1073{ 1074 if (likely (e_type <= ET_CORE)) 1075 { 1076 static const char *const knowntypes[] = 1077 { 1078 N_("NONE (None)"), 1079 N_("REL (Relocatable file)"), 1080 N_("EXEC (Executable file)"), 1081 N_("DYN (Shared object file)"), 1082 N_("CORE (Core file)") 1083 }; 1084 puts (_(knowntypes[e_type])); 1085 } 1086 else if (e_type >= ET_LOOS && e_type <= ET_HIOS) 1087 printf (_("OS Specific: (%x)\n"), e_type); 1088 else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */) 1089 printf (_("Processor Specific: (%x)\n"), e_type); 1090 else 1091 puts ("???"); 1092} 1093 1094 1095/* Print ELF header. */ 1096static void 1097print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr) 1098{ 1099 fputs_unlocked (_("ELF Header:\n Magic: "), stdout); 1100 for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt) 1101 printf (" %02hhx", ehdr->e_ident[cnt]); 1102 1103 printf (_("\n Class: %s\n"), 1104 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32" 1105 : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64" 1106 : "\?\?\?"); 1107 1108 printf (_(" Data: %s\n"), 1109 ehdr->e_ident[EI_DATA] == ELFDATA2LSB 1110 ? "2's complement, little endian" 1111 : ehdr->e_ident[EI_DATA] == ELFDATA2MSB 1112 ? "2's complement, big endian" : "\?\?\?"); 1113 1114 printf (_(" Ident Version: %hhd %s\n"), 1115 ehdr->e_ident[EI_VERSION], 1116 ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)") 1117 : "(\?\?\?)"); 1118 1119 char buf[512]; 1120 printf (_(" OS/ABI: %s\n"), 1121 ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf))); 1122 1123 printf (_(" ABI Version: %hhd\n"), 1124 ehdr->e_ident[EI_ABIVERSION]); 1125 1126 fputs_unlocked (_(" Type: "), stdout); 1127 print_file_type (ehdr->e_type); 1128 1129 const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine); 1130 if (machine != NULL) 1131 printf (_(" Machine: %s\n"), machine); 1132 else 1133 printf (_(" Machine: <unknown>: 0x%x\n"), 1134 ehdr->e_machine); 1135 1136 printf (_(" Version: %d %s\n"), 1137 ehdr->e_version, 1138 ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)"); 1139 1140 printf (_(" Entry point address: %#" PRIx64 "\n"), 1141 ehdr->e_entry); 1142 1143 printf (_(" Start of program headers: %" PRId64 " %s\n"), 1144 ehdr->e_phoff, _("(bytes into file)")); 1145 1146 printf (_(" Start of section headers: %" PRId64 " %s\n"), 1147 ehdr->e_shoff, _("(bytes into file)")); 1148 1149 printf (_(" Flags: %s\n"), 1150 ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf))); 1151 1152 printf (_(" Size of this header: %" PRId16 " %s\n"), 1153 ehdr->e_ehsize, _("(bytes)")); 1154 1155 printf (_(" Size of program header entries: %" PRId16 " %s\n"), 1156 ehdr->e_phentsize, _("(bytes)")); 1157 1158 printf (_(" Number of program headers entries: %" PRId16), 1159 ehdr->e_phnum); 1160 if (ehdr->e_phnum == PN_XNUM) 1161 { 1162 GElf_Shdr shdr_mem; 1163 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem); 1164 if (shdr != NULL) 1165 printf (_(" (%" PRIu32 " in [0].sh_info)"), 1166 (uint32_t) shdr->sh_info); 1167 else 1168 fputs_unlocked (_(" ([0] not available)"), stdout); 1169 } 1170 fputc_unlocked ('\n', stdout); 1171 1172 printf (_(" Size of section header entries: %" PRId16 " %s\n"), 1173 ehdr->e_shentsize, _("(bytes)")); 1174 1175 printf (_(" Number of section headers entries: %" PRId16), 1176 ehdr->e_shnum); 1177 if (ehdr->e_shnum == 0) 1178 { 1179 GElf_Shdr shdr_mem; 1180 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem); 1181 if (shdr != NULL) 1182 printf (_(" (%" PRIu32 " in [0].sh_size)"), 1183 (uint32_t) shdr->sh_size); 1184 else 1185 fputs_unlocked (_(" ([0] not available)"), stdout); 1186 } 1187 fputc_unlocked ('\n', stdout); 1188 1189 if (unlikely (ehdr->e_shstrndx == SHN_XINDEX)) 1190 { 1191 GElf_Shdr shdr_mem; 1192 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem); 1193 if (shdr != NULL) 1194 /* We managed to get the zeroth section. */ 1195 snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"), 1196 (uint32_t) shdr->sh_link); 1197 else 1198 { 1199 strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1); 1200 buf[sizeof (buf) - 1] = '\0'; 1201 } 1202 1203 printf (_(" Section header string table index: XINDEX%s\n\n"), 1204 buf); 1205 } 1206 else 1207 printf (_(" Section header string table index: %" PRId16 "\n\n"), 1208 ehdr->e_shstrndx); 1209} 1210 1211 1212static const char * 1213get_visibility_type (int value) 1214{ 1215 switch (value) 1216 { 1217 case STV_DEFAULT: 1218 return "DEFAULT"; 1219 case STV_INTERNAL: 1220 return "INTERNAL"; 1221 case STV_HIDDEN: 1222 return "HIDDEN"; 1223 case STV_PROTECTED: 1224 return "PROTECTED"; 1225 default: 1226 return "???"; 1227 } 1228} 1229 1230static const char * 1231elf_ch_type_name (unsigned int code) 1232{ 1233 if (code == 0) 1234 return "NONE"; 1235 1236 if (code == ELFCOMPRESS_ZLIB) 1237 return "ZLIB"; 1238 1239 return "UNKNOWN"; 1240} 1241 1242/* Print the section headers. */ 1243static void 1244print_shdr (Ebl *ebl, GElf_Ehdr *ehdr) 1245{ 1246 size_t cnt; 1247 size_t shstrndx; 1248 1249 if (! print_file_header) 1250 { 1251 size_t sections; 1252 if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0)) 1253 error_exit (0, _("cannot get number of sections: %s"), 1254 elf_errmsg (-1)); 1255 1256 printf (_("\ 1257There are %zd section headers, starting at offset %#" PRIx64 ":\n\ 1258\n"), 1259 sections, ehdr->e_shoff); 1260 } 1261 1262 /* Get the section header string table index. */ 1263 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 1264 error_exit (0, _("cannot get section header string table index: %s"), 1265 elf_errmsg (-1)); 1266 1267 puts (_("Section Headers:")); 1268 1269 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32) 1270 puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al")); 1271 else 1272 puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al")); 1273 1274 if (print_decompress) 1275 { 1276 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32) 1277 puts (_(" [Compression Size Al]")); 1278 else 1279 puts (_(" [Compression Size Al]")); 1280 } 1281 1282 for (cnt = 0; cnt < shnum; ++cnt) 1283 { 1284 Elf_Scn *scn = elf_getscn (ebl->elf, cnt); 1285 1286 if (unlikely (scn == NULL)) 1287 error_exit (0, _("cannot get section: %s"), 1288 elf_errmsg (-1)); 1289 1290 /* Get the section header. */ 1291 GElf_Shdr shdr_mem; 1292 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 1293 if (unlikely (shdr == NULL)) 1294 error_exit (0, _("cannot get section header: %s"), 1295 elf_errmsg (-1)); 1296 1297 char flagbuf[20]; 1298 char *cp = flagbuf; 1299 if (shdr->sh_flags & SHF_WRITE) 1300 *cp++ = 'W'; 1301 if (shdr->sh_flags & SHF_ALLOC) 1302 *cp++ = 'A'; 1303 if (shdr->sh_flags & SHF_EXECINSTR) 1304 *cp++ = 'X'; 1305 if (shdr->sh_flags & SHF_MERGE) 1306 *cp++ = 'M'; 1307 if (shdr->sh_flags & SHF_STRINGS) 1308 *cp++ = 'S'; 1309 if (shdr->sh_flags & SHF_INFO_LINK) 1310 *cp++ = 'I'; 1311 if (shdr->sh_flags & SHF_LINK_ORDER) 1312 *cp++ = 'L'; 1313 if (shdr->sh_flags & SHF_OS_NONCONFORMING) 1314 *cp++ = 'N'; 1315 if (shdr->sh_flags & SHF_GROUP) 1316 *cp++ = 'G'; 1317 if (shdr->sh_flags & SHF_TLS) 1318 *cp++ = 'T'; 1319 if (shdr->sh_flags & SHF_COMPRESSED) 1320 *cp++ = 'C'; 1321 if (shdr->sh_flags & SHF_ORDERED) 1322 *cp++ = 'O'; 1323 if (shdr->sh_flags & SHF_EXCLUDE) 1324 *cp++ = 'E'; 1325 if (shdr->sh_flags & SHF_GNU_RETAIN) 1326 *cp++ = 'R'; 1327 *cp = '\0'; 1328 1329 const char *sname; 1330 char buf[128]; 1331 sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>"; 1332 printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64 1333 " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32 1334 " %2" PRId64 "\n", 1335 cnt, sname, 1336 ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)), 1337 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr, 1338 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset, 1339 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size, 1340 shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info, 1341 shdr->sh_addralign); 1342 1343 if (print_decompress) 1344 { 1345 if ((shdr->sh_flags & SHF_COMPRESSED) != 0) 1346 { 1347 GElf_Chdr chdr; 1348 if (gelf_getchdr (scn, &chdr) != NULL) 1349 printf (" [ELF %s (%" PRId32 ") %0*" PRIx64 1350 " %2" PRId64 "]\n", 1351 elf_ch_type_name (chdr.ch_type), 1352 chdr.ch_type, 1353 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, 1354 chdr.ch_size, chdr.ch_addralign); 1355 else 1356 error (0, 0, 1357 _("bad compression header for section %zd: %s"), 1358 elf_ndxscn (scn), elf_errmsg (-1)); 1359 } 1360 else if (startswith (sname, ".zdebug")) 1361 { 1362 ssize_t size; 1363 if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0) 1364 printf (" [GNU ZLIB %0*zx ]\n", 1365 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size); 1366 else 1367 error (0, 0, 1368 _("bad gnu compressed size for section %zd: %s"), 1369 elf_ndxscn (scn), elf_errmsg (-1)); 1370 } 1371 } 1372 } 1373 1374 fputc_unlocked ('\n', stdout); 1375} 1376 1377 1378/* Print the program header. */ 1379static void 1380print_phdr (Ebl *ebl, GElf_Ehdr *ehdr) 1381{ 1382 if (phnum == 0) 1383 /* No program header, this is OK in relocatable objects. */ 1384 return; 1385 1386 puts (_("Program Headers:")); 1387 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32) 1388 puts (_("\ 1389 Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align")); 1390 else 1391 puts (_("\ 1392 Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align")); 1393 1394 /* Process all program headers. */ 1395 bool has_relro = false; 1396 GElf_Addr relro_from = 0; 1397 GElf_Addr relro_to = 0; 1398 for (size_t cnt = 0; cnt < phnum; ++cnt) 1399 { 1400 char buf[128]; 1401 GElf_Phdr mem; 1402 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem); 1403 1404 /* If for some reason the header cannot be returned show this. */ 1405 if (unlikely (phdr == NULL)) 1406 { 1407 puts (" ???"); 1408 continue; 1409 } 1410 1411 printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64 1412 " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n", 1413 ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)), 1414 phdr->p_offset, 1415 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr, 1416 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr, 1417 phdr->p_filesz, 1418 phdr->p_memsz, 1419 phdr->p_flags & PF_R ? 'R' : ' ', 1420 phdr->p_flags & PF_W ? 'W' : ' ', 1421 phdr->p_flags & PF_X ? 'E' : ' ', 1422 phdr->p_align); 1423 1424 if (phdr->p_type == PT_INTERP) 1425 { 1426 /* If we are sure the file offset is valid then we can show 1427 the user the name of the interpreter. We check whether 1428 there is a section at the file offset. Normally there 1429 would be a section called ".interp". But in separate 1430 .debug files it is a NOBITS section (and so doesn't match 1431 with gelf_offscn). Which probably means the offset is 1432 not valid another reason could be because the ELF file 1433 just doesn't contain any section headers, in that case 1434 just play it safe and don't display anything. */ 1435 1436 Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset); 1437 GElf_Shdr shdr_mem; 1438 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 1439 1440 size_t maxsize; 1441 char *filedata = elf_rawfile (ebl->elf, &maxsize); 1442 1443 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS 1444 && filedata != NULL && phdr->p_offset < maxsize 1445 && phdr->p_filesz <= maxsize - phdr->p_offset 1446 && memchr (filedata + phdr->p_offset, '\0', 1447 phdr->p_filesz) != NULL) 1448 printf (_("\t[Requesting program interpreter: %s]\n"), 1449 filedata + phdr->p_offset); 1450 } 1451 else if (phdr->p_type == PT_GNU_RELRO) 1452 { 1453 has_relro = true; 1454 relro_from = phdr->p_vaddr; 1455 relro_to = relro_from + phdr->p_memsz; 1456 } 1457 } 1458 1459 size_t sections; 1460 if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0)) 1461 error_exit (0, _("cannot get number of sections: %s"), 1462 elf_errmsg (-1)); 1463 1464 if (sections == 0) 1465 /* No sections in the file. Punt. */ 1466 return; 1467 1468 /* Get the section header string table index. */ 1469 size_t shstrndx; 1470 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 1471 error_exit (0, _("cannot get section header string table index")); 1472 1473 puts (_("\n Section to Segment mapping:\n Segment Sections...")); 1474 1475 for (size_t cnt = 0; cnt < phnum; ++cnt) 1476 { 1477 /* Print the segment number. */ 1478 printf (" %2.2zu ", cnt); 1479 1480 GElf_Phdr phdr_mem; 1481 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem); 1482 /* This must not happen. */ 1483 if (unlikely (phdr == NULL)) 1484 error_exit (0, _("cannot get program header: %s"), 1485 elf_errmsg (-1)); 1486 1487 /* Iterate over the sections. */ 1488 bool in_relro = false; 1489 bool in_ro = false; 1490 for (size_t inner = 1; inner < shnum; ++inner) 1491 { 1492 Elf_Scn *scn = elf_getscn (ebl->elf, inner); 1493 /* This should not happen. */ 1494 if (unlikely (scn == NULL)) 1495 error_exit (0, _("cannot get section: %s"), 1496 elf_errmsg (-1)); 1497 1498 /* Get the section header. */ 1499 GElf_Shdr shdr_mem; 1500 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 1501 if (unlikely (shdr == NULL)) 1502 error_exit (0, _("cannot get section header: %s"), 1503 elf_errmsg (-1)); 1504 1505 if (shdr->sh_size > 0 1506 /* Compare allocated sections by VMA, unallocated 1507 sections by file offset. */ 1508 && (shdr->sh_flags & SHF_ALLOC 1509 ? (shdr->sh_addr >= phdr->p_vaddr 1510 && (shdr->sh_addr + shdr->sh_size 1511 <= phdr->p_vaddr + phdr->p_memsz)) 1512 : (shdr->sh_offset >= phdr->p_offset 1513 && (shdr->sh_offset + shdr->sh_size 1514 <= phdr->p_offset + phdr->p_filesz)))) 1515 { 1516 if (has_relro && !in_relro 1517 && shdr->sh_addr >= relro_from 1518 && shdr->sh_addr + shdr->sh_size <= relro_to) 1519 { 1520 fputs_unlocked (" [RELRO:", stdout); 1521 in_relro = true; 1522 } 1523 else if (has_relro && in_relro && shdr->sh_addr >= relro_to) 1524 { 1525 fputs_unlocked ("]", stdout); 1526 in_relro = false; 1527 } 1528 else if (has_relro && in_relro 1529 && shdr->sh_addr + shdr->sh_size > relro_to) 1530 fputs_unlocked ("] <RELRO:", stdout); 1531 else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0) 1532 { 1533 if (!in_ro) 1534 { 1535 fputs_unlocked (" [RO:", stdout); 1536 in_ro = true; 1537 } 1538 } 1539 else 1540 { 1541 /* Determine the segment this section is part of. */ 1542 size_t cnt2; 1543 GElf_Phdr phdr2_mem; 1544 GElf_Phdr *phdr2 = NULL; 1545 for (cnt2 = 0; cnt2 < phnum; ++cnt2) 1546 { 1547 phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem); 1548 1549 if (phdr2 != NULL && phdr2->p_type == PT_LOAD 1550 && shdr->sh_addr >= phdr2->p_vaddr 1551 && (shdr->sh_addr + shdr->sh_size 1552 <= phdr2->p_vaddr + phdr2->p_memsz)) 1553 break; 1554 } 1555 1556 if (cnt2 < phnum) 1557 { 1558 if ((phdr2->p_flags & PF_W) == 0 && !in_ro) 1559 { 1560 fputs_unlocked (" [RO:", stdout); 1561 in_ro = true; 1562 } 1563 else if ((phdr2->p_flags & PF_W) != 0 && in_ro) 1564 { 1565 fputs_unlocked ("]", stdout); 1566 in_ro = false; 1567 } 1568 } 1569 } 1570 1571 printf (" %s", 1572 elf_strptr (ebl->elf, shstrndx, shdr->sh_name)); 1573 1574 /* Signal that this section is only partially covered. */ 1575 if (has_relro && in_relro 1576 && shdr->sh_addr + shdr->sh_size > relro_to) 1577 { 1578 fputs_unlocked (">", stdout); 1579 in_relro = false; 1580 } 1581 } 1582 } 1583 if (in_relro || in_ro) 1584 fputs_unlocked ("]", stdout); 1585 1586 /* Finish the line. */ 1587 fputc_unlocked ('\n', stdout); 1588 } 1589} 1590 1591 1592static const char * 1593section_name (Ebl *ebl, GElf_Shdr *shdr) 1594{ 1595 size_t shstrndx; 1596 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0) 1597 return "???"; 1598 return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???"; 1599} 1600 1601 1602static void 1603handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr) 1604{ 1605 /* Get the data of the section. */ 1606 Elf_Data *data = elf_getdata (scn, NULL); 1607 1608 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link); 1609 GElf_Shdr symshdr_mem; 1610 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem); 1611 Elf_Data *symdata = elf_getdata (symscn, NULL); 1612 1613 if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL 1614 || symdata == NULL) 1615 return; 1616 1617 /* Get the section header string table index. */ 1618 size_t shstrndx; 1619 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 1620 error_exit (0, _("cannot get section header string table index")); 1621 1622 Elf32_Word *grpref = (Elf32_Word *) data->d_buf; 1623 1624 GElf_Sym sym_mem; 1625 GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem); 1626 1627 printf ((grpref[0] & GRP_COMDAT) 1628 ? ngettext ("\ 1629\nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n", 1630 "\ 1631\nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n", 1632 data->d_size / sizeof (Elf32_Word) - 1) 1633 : ngettext ("\ 1634\nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\ 1635\nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n", 1636 data->d_size / sizeof (Elf32_Word) - 1), 1637 elf_ndxscn (scn), 1638 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 1639 (sym == NULL ? NULL 1640 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name)) 1641 ?: _("<INVALID SYMBOL>"), 1642 data->d_size / sizeof (Elf32_Word) - 1); 1643 1644 for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt) 1645 { 1646 GElf_Shdr grpshdr_mem; 1647 GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]), 1648 &grpshdr_mem); 1649 1650 const char *str; 1651 printf (" [%2u] %s\n", 1652 grpref[cnt], 1653 grpshdr != NULL 1654 && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name)) 1655 ? str : _("<INVALID SECTION>")); 1656 } 1657} 1658 1659 1660static void 1661print_scngrp (Ebl *ebl) 1662{ 1663 /* Find all relocation sections and handle them. */ 1664 Elf_Scn *scn = NULL; 1665 1666 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 1667 { 1668 /* Handle the section if it is a symbol table. */ 1669 GElf_Shdr shdr_mem; 1670 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 1671 1672 if (shdr != NULL && shdr->sh_type == SHT_GROUP) 1673 { 1674 if ((shdr->sh_flags & SHF_COMPRESSED) != 0) 1675 { 1676 if (elf_compress (scn, 0, 0) < 0) 1677 printf ("WARNING: %s [%zd]\n", 1678 _("Couldn't uncompress section"), 1679 elf_ndxscn (scn)); 1680 shdr = gelf_getshdr (scn, &shdr_mem); 1681 if (unlikely (shdr == NULL)) 1682 error_exit (0, _("cannot get section [%zd] header: %s"), 1683 elf_ndxscn (scn), 1684 elf_errmsg (-1)); 1685 } 1686 handle_scngrp (ebl, scn, shdr); 1687 } 1688 } 1689} 1690 1691 1692static const struct flags 1693{ 1694 int mask; 1695 const char *str; 1696} dt_flags[] = 1697 { 1698 { DF_ORIGIN, "ORIGIN" }, 1699 { DF_SYMBOLIC, "SYMBOLIC" }, 1700 { DF_TEXTREL, "TEXTREL" }, 1701 { DF_BIND_NOW, "BIND_NOW" }, 1702 { DF_STATIC_TLS, "STATIC_TLS" } 1703 }; 1704static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]); 1705 1706static const struct flags dt_flags_1[] = 1707 { 1708 { DF_1_NOW, "NOW" }, 1709 { DF_1_GLOBAL, "GLOBAL" }, 1710 { DF_1_GROUP, "GROUP" }, 1711 { DF_1_NODELETE, "NODELETE" }, 1712 { DF_1_LOADFLTR, "LOADFLTR" }, 1713 { DF_1_INITFIRST, "INITFIRST" }, 1714 { DF_1_NOOPEN, "NOOPEN" }, 1715 { DF_1_ORIGIN, "ORIGIN" }, 1716 { DF_1_DIRECT, "DIRECT" }, 1717 { DF_1_TRANS, "TRANS" }, 1718 { DF_1_INTERPOSE, "INTERPOSE" }, 1719 { DF_1_NODEFLIB, "NODEFLIB" }, 1720 { DF_1_NODUMP, "NODUMP" }, 1721 { DF_1_CONFALT, "CONFALT" }, 1722 { DF_1_ENDFILTEE, "ENDFILTEE" }, 1723 { DF_1_DISPRELDNE, "DISPRELDNE" }, 1724 { DF_1_DISPRELPND, "DISPRELPND" }, 1725 }; 1726static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]); 1727 1728static const struct flags dt_feature_1[] = 1729 { 1730 { DTF_1_PARINIT, "PARINIT" }, 1731 { DTF_1_CONFEXP, "CONFEXP" } 1732 }; 1733static const int ndt_feature_1 = (sizeof (dt_feature_1) 1734 / sizeof (dt_feature_1[0])); 1735 1736static const struct flags dt_posflag_1[] = 1737 { 1738 { DF_P1_LAZYLOAD, "LAZYLOAD" }, 1739 { DF_P1_GROUPPERM, "GROUPPERM" } 1740 }; 1741static const int ndt_posflag_1 = (sizeof (dt_posflag_1) 1742 / sizeof (dt_posflag_1[0])); 1743 1744 1745static void 1746print_flags (int class, GElf_Xword d_val, const struct flags *flags, 1747 int nflags) 1748{ 1749 bool first = true; 1750 int cnt; 1751 1752 for (cnt = 0; cnt < nflags; ++cnt) 1753 if (d_val & flags[cnt].mask) 1754 { 1755 if (!first) 1756 putchar_unlocked (' '); 1757 fputs_unlocked (flags[cnt].str, stdout); 1758 d_val &= ~flags[cnt].mask; 1759 first = false; 1760 } 1761 1762 if (d_val != 0) 1763 { 1764 if (!first) 1765 putchar_unlocked (' '); 1766 printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val); 1767 } 1768 1769 putchar_unlocked ('\n'); 1770} 1771 1772 1773static void 1774print_dt_flags (int class, GElf_Xword d_val) 1775{ 1776 print_flags (class, d_val, dt_flags, ndt_flags); 1777} 1778 1779 1780static void 1781print_dt_flags_1 (int class, GElf_Xword d_val) 1782{ 1783 print_flags (class, d_val, dt_flags_1, ndt_flags_1); 1784} 1785 1786 1787static void 1788print_dt_feature_1 (int class, GElf_Xword d_val) 1789{ 1790 print_flags (class, d_val, dt_feature_1, ndt_feature_1); 1791} 1792 1793 1794static void 1795print_dt_posflag_1 (int class, GElf_Xword d_val) 1796{ 1797 print_flags (class, d_val, dt_posflag_1, ndt_posflag_1); 1798} 1799 1800 1801static size_t 1802get_dyn_ents (Elf_Data * dyn_data) 1803{ 1804 GElf_Dyn *dyn; 1805 GElf_Dyn dyn_mem; 1806 size_t dyn_idx = 0; 1807 do 1808 { 1809 dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem); 1810 if (dyn != NULL) 1811 ++dyn_idx; 1812 } 1813 while (dyn != NULL && dyn->d_tag != DT_NULL); 1814 1815 return dyn_idx; 1816} 1817 1818 1819static void 1820handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr) 1821{ 1822 int class = gelf_getclass (ebl->elf); 1823 GElf_Shdr glink_mem; 1824 GElf_Shdr *glink; 1825 Elf_Data *data; 1826 size_t cnt; 1827 size_t shstrndx; 1828 size_t dyn_ents; 1829 1830 /* Get the data of the section. */ 1831 if (use_dynamic_segment) 1832 data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset, 1833 phdr->p_filesz, ELF_T_DYN); 1834 else 1835 data = elf_getdata (scn, NULL); 1836 1837 if (data == NULL) 1838 return; 1839 1840 /* Get the dynamic section entry number */ 1841 dyn_ents = get_dyn_ents (data); 1842 1843 if (!use_dynamic_segment) 1844 { 1845 /* Get the section header string table index. */ 1846 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 1847 error_exit (0, _("cannot get section header string table index")); 1848 1849 glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem); 1850 if (glink == NULL) 1851 error_exit (0, _("invalid sh_link value in section %zu"), 1852 elf_ndxscn (scn)); 1853 1854 printf (ngettext ("\ 1855\nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n", 1856 "\ 1857\nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n", 1858 dyn_ents), 1859 (unsigned long int) dyn_ents, 1860 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr, 1861 shdr->sh_offset, 1862 (int) shdr->sh_link, 1863 elf_strptr (ebl->elf, shstrndx, glink->sh_name)); 1864 } 1865 else 1866 { 1867 printf (ngettext ("\ 1868\nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n", 1869 "\ 1870\nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n", 1871 dyn_ents), 1872 (unsigned long int) dyn_ents, 1873 class == ELFCLASS32 ? 10 : 18, phdr->p_paddr, 1874 phdr->p_offset); 1875 } 1876 1877 fputs_unlocked (_(" Type Value\n"), stdout); 1878 1879 /* if --use-dynamic option is enabled, 1880 use the string table to get the related library info. */ 1881 Elf_Data *strtab_data = NULL; 1882 if (use_dynamic_segment) 1883 { 1884 strtab_data = get_dynscn_strtab(ebl->elf, phdr); 1885 if (strtab_data == NULL) 1886 error_exit (0, _("cannot get string table by using dynamic segment")); 1887 } 1888 1889 for (cnt = 0; cnt < dyn_ents; ++cnt) 1890 { 1891 GElf_Dyn dynmem; 1892 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem); 1893 if (dyn == NULL) 1894 break; 1895 1896 char buf[64]; 1897 printf (" %-17s ", 1898 ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf))); 1899 1900 char *name = NULL; 1901 if (dyn->d_tag == DT_NEEDED 1902 || dyn->d_tag == DT_SONAME 1903 || dyn->d_tag == DT_RPATH 1904 || dyn->d_tag == DT_RUNPATH) 1905 { 1906 if (! use_dynamic_segment) 1907 name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val); 1908 else if (dyn->d_un.d_val < strtab_data->d_size 1909 && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0', 1910 strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL) 1911 name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val; 1912 } 1913 1914 switch (dyn->d_tag) 1915 { 1916 case DT_NULL: 1917 case DT_DEBUG: 1918 case DT_BIND_NOW: 1919 case DT_TEXTREL: 1920 /* No further output. */ 1921 fputc_unlocked ('\n', stdout); 1922 break; 1923 1924 case DT_NEEDED: 1925 printf (_("Shared library: [%s]\n"), name); 1926 break; 1927 1928 case DT_SONAME: 1929 printf (_("Library soname: [%s]\n"), name); 1930 break; 1931 1932 case DT_RPATH: 1933 printf (_("Library rpath: [%s]\n"), name); 1934 break; 1935 1936 case DT_RUNPATH: 1937 printf (_("Library runpath: [%s]\n"), name); 1938 break; 1939 1940 case DT_PLTRELSZ: 1941 case DT_RELASZ: 1942 case DT_STRSZ: 1943 case DT_RELSZ: 1944 case DT_RELAENT: 1945 case DT_SYMENT: 1946 case DT_RELENT: 1947 case DT_PLTPADSZ: 1948 case DT_MOVEENT: 1949 case DT_MOVESZ: 1950 case DT_INIT_ARRAYSZ: 1951 case DT_FINI_ARRAYSZ: 1952 case DT_SYMINSZ: 1953 case DT_SYMINENT: 1954 case DT_GNU_CONFLICTSZ: 1955 case DT_GNU_LIBLISTSZ: 1956 printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val); 1957 break; 1958 1959 case DT_VERDEFNUM: 1960 case DT_VERNEEDNUM: 1961 case DT_RELACOUNT: 1962 case DT_RELCOUNT: 1963 printf ("%" PRId64 "\n", dyn->d_un.d_val); 1964 break; 1965 1966 case DT_PLTREL:; 1967 const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val, 1968 NULL, 0); 1969 puts (tagname ?: "???"); 1970 break; 1971 1972 case DT_FLAGS: 1973 print_dt_flags (class, dyn->d_un.d_val); 1974 break; 1975 1976 case DT_FLAGS_1: 1977 print_dt_flags_1 (class, dyn->d_un.d_val); 1978 break; 1979 1980 case DT_FEATURE_1: 1981 print_dt_feature_1 (class, dyn->d_un.d_val); 1982 break; 1983 1984 case DT_POSFLAG_1: 1985 print_dt_posflag_1 (class, dyn->d_un.d_val); 1986 break; 1987 1988 default: 1989 printf ("%#0*" PRIx64 "\n", 1990 class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val); 1991 break; 1992 } 1993 } 1994} 1995 1996 1997/* Print the dynamic segment. */ 1998static void 1999print_dynamic (Ebl *ebl) 2000{ 2001 for (size_t i = 0; i < phnum; ++i) 2002 { 2003 GElf_Phdr phdr_mem; 2004 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem); 2005 2006 if (phdr != NULL && phdr->p_type == PT_DYNAMIC) 2007 { 2008 Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset); 2009 GElf_Shdr shdr_mem; 2010 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 2011 if ((use_dynamic_segment && phdr != NULL) 2012 || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)) 2013 handle_dynamic (ebl, scn, shdr, phdr); 2014 break; 2015 } 2016 } 2017} 2018 2019 2020/* Print relocations. */ 2021static void 2022print_relocs (Ebl *ebl, GElf_Ehdr *ehdr) 2023{ 2024 /* Find all relocation sections and handle them. */ 2025 Elf_Scn *scn = NULL; 2026 2027 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 2028 { 2029 /* Handle the section if it is a symbol table. */ 2030 GElf_Shdr shdr_mem; 2031 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 2032 2033 if (likely (shdr != NULL)) 2034 { 2035 if (shdr->sh_type == SHT_REL) 2036 handle_relocs_rel (ebl, ehdr, scn, shdr); 2037 else if (shdr->sh_type == SHT_RELA) 2038 handle_relocs_rela (ebl, ehdr, scn, shdr); 2039 } 2040 } 2041} 2042 2043 2044/* Handle a relocation section. */ 2045static void 2046handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr) 2047{ 2048 int class = gelf_getclass (ebl->elf); 2049 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT); 2050 int nentries = shdr->sh_size / sh_entsize; 2051 2052 /* Get the data of the section. */ 2053 Elf_Data *data = elf_getdata (scn, NULL); 2054 if (data == NULL) 2055 return; 2056 2057 /* Get the symbol table information. */ 2058 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link); 2059 GElf_Shdr symshdr_mem; 2060 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem); 2061 Elf_Data *symdata = elf_getdata (symscn, NULL); 2062 2063 /* Get the section header of the section the relocations are for. */ 2064 GElf_Shdr destshdr_mem; 2065 GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info), 2066 &destshdr_mem); 2067 2068 if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL)) 2069 { 2070 printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"), 2071 shdr->sh_offset); 2072 return; 2073 } 2074 2075 /* Search for the optional extended section index table. */ 2076 Elf_Data *xndxdata = NULL; 2077 int xndxscnidx = elf_scnshndx (scn); 2078 if (unlikely (xndxscnidx > 0)) 2079 xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL); 2080 2081 /* Get the section header string table index. */ 2082 size_t shstrndx; 2083 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 2084 error_exit (0, _("cannot get section header string table index")); 2085 2086 if (shdr->sh_info != 0) 2087 printf (ngettext ("\ 2088\nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n", 2089 "\ 2090\nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n", 2091 nentries), 2092 elf_ndxscn (scn), 2093 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 2094 (unsigned int) shdr->sh_info, 2095 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name), 2096 shdr->sh_offset, 2097 nentries); 2098 else 2099 /* The .rel.dyn section does not refer to a specific section but 2100 instead of section index zero. Do not try to print a section 2101 name. */ 2102 printf (ngettext ("\ 2103\nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n", 2104 "\ 2105\nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n", 2106 nentries), 2107 (unsigned int) elf_ndxscn (scn), 2108 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 2109 shdr->sh_offset, 2110 nentries); 2111 fputs_unlocked (class == ELFCLASS32 2112 ? _("\ 2113 Offset Type Value Name\n") 2114 : _("\ 2115 Offset Type Value Name\n"), 2116 stdout); 2117 2118 int is_statically_linked = 0; 2119 for (int cnt = 0; cnt < nentries; ++cnt) 2120 { 2121 GElf_Rel relmem; 2122 GElf_Rel *rel = gelf_getrel (data, cnt, &relmem); 2123 if (likely (rel != NULL)) 2124 { 2125 char buf[128]; 2126 GElf_Sym symmem; 2127 Elf32_Word xndx; 2128 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata, 2129 GELF_R_SYM (rel->r_info), 2130 &symmem, &xndx); 2131 if (unlikely (sym == NULL)) 2132 { 2133 /* As a special case we have to handle relocations in static 2134 executables. This only happens for IRELATIVE relocations 2135 (so far). There is no symbol table. */ 2136 if (is_statically_linked == 0) 2137 { 2138 /* Find the program header and look for a PT_INTERP entry. */ 2139 is_statically_linked = -1; 2140 if (ehdr->e_type == ET_EXEC) 2141 { 2142 is_statically_linked = 1; 2143 2144 for (size_t inner = 0; inner < phnum; ++inner) 2145 { 2146 GElf_Phdr phdr_mem; 2147 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner, 2148 &phdr_mem); 2149 if (phdr != NULL && phdr->p_type == PT_INTERP) 2150 { 2151 is_statically_linked = -1; 2152 break; 2153 } 2154 } 2155 } 2156 } 2157 2158 if (is_statically_linked > 0 && shdr->sh_link == 0) 2159 printf ("\ 2160 %#0*" PRIx64 " %-20s %*s %s\n", 2161 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2162 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info)) 2163 /* Avoid the leading R_ which isn't carrying any 2164 information. */ 2165 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2166 buf, sizeof (buf)) + 2 2167 : _("<INVALID RELOC>"), 2168 class == ELFCLASS32 ? 10 : 18, "", 2169 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name)); 2170 else 2171 printf (" %#0*" PRIx64 " %-20s <%s %ld>\n", 2172 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2173 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info)) 2174 /* Avoid the leading R_ which isn't carrying any 2175 information. */ 2176 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2177 buf, sizeof (buf)) + 2 2178 : _("<INVALID RELOC>"), 2179 _("INVALID SYMBOL"), 2180 (long int) GELF_R_SYM (rel->r_info)); 2181 } 2182 else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION) 2183 printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n", 2184 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2185 likely (ebl_reloc_type_check (ebl, 2186 GELF_R_TYPE (rel->r_info))) 2187 /* Avoid the leading R_ which isn't carrying any 2188 information. */ 2189 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2190 buf, sizeof (buf)) + 2 2191 : _("<INVALID RELOC>"), 2192 class == ELFCLASS32 ? 10 : 18, sym->st_value, 2193 elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name)); 2194 else 2195 { 2196 /* This is a relocation against a STT_SECTION symbol. */ 2197 GElf_Shdr secshdr_mem; 2198 GElf_Shdr *secshdr; 2199 secshdr = gelf_getshdr (elf_getscn (ebl->elf, 2200 sym->st_shndx == SHN_XINDEX 2201 ? xndx : sym->st_shndx), 2202 &secshdr_mem); 2203 2204 if (unlikely (secshdr == NULL)) 2205 printf (" %#0*" PRIx64 " %-20s <%s %ld>\n", 2206 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2207 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info)) 2208 /* Avoid the leading R_ which isn't carrying any 2209 information. */ 2210 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2211 buf, sizeof (buf)) + 2 2212 : _("<INVALID RELOC>"), 2213 _("INVALID SECTION"), 2214 (long int) (sym->st_shndx == SHN_XINDEX 2215 ? xndx : sym->st_shndx)); 2216 else 2217 printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n", 2218 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2219 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info)) 2220 /* Avoid the leading R_ which isn't carrying any 2221 information. */ 2222 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2223 buf, sizeof (buf)) + 2 2224 : _("<INVALID RELOC>"), 2225 class == ELFCLASS32 ? 10 : 18, sym->st_value, 2226 elf_strptr (ebl->elf, shstrndx, secshdr->sh_name)); 2227 } 2228 } 2229 } 2230} 2231 2232 2233/* Handle a relocation section. */ 2234static void 2235handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr) 2236{ 2237 int class = gelf_getclass (ebl->elf); 2238 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT); 2239 int nentries = shdr->sh_size / sh_entsize; 2240 2241 /* Get the data of the section. */ 2242 Elf_Data *data = elf_getdata (scn, NULL); 2243 if (data == NULL) 2244 return; 2245 2246 /* Get the symbol table information. */ 2247 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link); 2248 GElf_Shdr symshdr_mem; 2249 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem); 2250 Elf_Data *symdata = elf_getdata (symscn, NULL); 2251 2252 /* Get the section header of the section the relocations are for. */ 2253 GElf_Shdr destshdr_mem; 2254 GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info), 2255 &destshdr_mem); 2256 2257 if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL)) 2258 { 2259 printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"), 2260 shdr->sh_offset); 2261 return; 2262 } 2263 2264 /* Search for the optional extended section index table. */ 2265 Elf_Data *xndxdata = NULL; 2266 int xndxscnidx = elf_scnshndx (scn); 2267 if (unlikely (xndxscnidx > 0)) 2268 xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL); 2269 2270 /* Get the section header string table index. */ 2271 size_t shstrndx; 2272 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 2273 error_exit (0, _("cannot get section header string table index")); 2274 2275 if (shdr->sh_info != 0) 2276 printf (ngettext ("\ 2277\nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n", 2278 "\ 2279\nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n", 2280 nentries), 2281 elf_ndxscn (scn), 2282 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 2283 (unsigned int) shdr->sh_info, 2284 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name), 2285 shdr->sh_offset, 2286 nentries); 2287 else 2288 /* The .rela.dyn section does not refer to a specific section but 2289 instead of section index zero. Do not try to print a section 2290 name. */ 2291 printf (ngettext ("\ 2292\nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n", 2293 "\ 2294\nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n", 2295 nentries), 2296 (unsigned int) elf_ndxscn (scn), 2297 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 2298 shdr->sh_offset, 2299 nentries); 2300 fputs_unlocked (class == ELFCLASS32 2301 ? _("\ 2302 Offset Type Value Addend Name\n") 2303 : _("\ 2304 Offset Type Value Addend Name\n"), 2305 stdout); 2306 2307 int is_statically_linked = 0; 2308 for (int cnt = 0; cnt < nentries; ++cnt) 2309 { 2310 GElf_Rela relmem; 2311 GElf_Rela *rel = gelf_getrela (data, cnt, &relmem); 2312 if (likely (rel != NULL)) 2313 { 2314 char buf[64]; 2315 GElf_Sym symmem; 2316 Elf32_Word xndx; 2317 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata, 2318 GELF_R_SYM (rel->r_info), 2319 &symmem, &xndx); 2320 2321 if (unlikely (sym == NULL)) 2322 { 2323 /* As a special case we have to handle relocations in static 2324 executables. This only happens for IRELATIVE relocations 2325 (so far). There is no symbol table. */ 2326 if (is_statically_linked == 0) 2327 { 2328 /* Find the program header and look for a PT_INTERP entry. */ 2329 is_statically_linked = -1; 2330 if (ehdr->e_type == ET_EXEC) 2331 { 2332 is_statically_linked = 1; 2333 2334 for (size_t inner = 0; inner < phnum; ++inner) 2335 { 2336 GElf_Phdr phdr_mem; 2337 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner, 2338 &phdr_mem); 2339 if (phdr != NULL && phdr->p_type == PT_INTERP) 2340 { 2341 is_statically_linked = -1; 2342 break; 2343 } 2344 } 2345 } 2346 } 2347 2348 if (is_statically_linked > 0 && shdr->sh_link == 0) 2349 printf ("\ 2350 %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n", 2351 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2352 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info)) 2353 /* Avoid the leading R_ which isn't carrying any 2354 information. */ 2355 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2356 buf, sizeof (buf)) + 2 2357 : _("<INVALID RELOC>"), 2358 class == ELFCLASS32 ? 10 : 18, "", 2359 rel->r_addend, 2360 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name)); 2361 else 2362 printf (" %#0*" PRIx64 " %-15s <%s %ld>\n", 2363 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2364 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info)) 2365 /* Avoid the leading R_ which isn't carrying any 2366 information. */ 2367 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2368 buf, sizeof (buf)) + 2 2369 : _("<INVALID RELOC>"), 2370 _("INVALID SYMBOL"), 2371 (long int) GELF_R_SYM (rel->r_info)); 2372 } 2373 else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION) 2374 printf ("\ 2375 %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n", 2376 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2377 likely (ebl_reloc_type_check (ebl, 2378 GELF_R_TYPE (rel->r_info))) 2379 /* Avoid the leading R_ which isn't carrying any 2380 information. */ 2381 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2382 buf, sizeof (buf)) + 2 2383 : _("<INVALID RELOC>"), 2384 class == ELFCLASS32 ? 10 : 18, sym->st_value, 2385 rel->r_addend, 2386 elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name)); 2387 else 2388 { 2389 /* This is a relocation against a STT_SECTION symbol. */ 2390 GElf_Shdr secshdr_mem; 2391 GElf_Shdr *secshdr; 2392 secshdr = gelf_getshdr (elf_getscn (ebl->elf, 2393 sym->st_shndx == SHN_XINDEX 2394 ? xndx : sym->st_shndx), 2395 &secshdr_mem); 2396 2397 if (unlikely (secshdr == NULL)) 2398 printf (" %#0*" PRIx64 " %-15s <%s %ld>\n", 2399 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2400 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info)) 2401 /* Avoid the leading R_ which isn't carrying any 2402 information. */ 2403 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2404 buf, sizeof (buf)) + 2 2405 : _("<INVALID RELOC>"), 2406 _("INVALID SECTION"), 2407 (long int) (sym->st_shndx == SHN_XINDEX 2408 ? xndx : sym->st_shndx)); 2409 else 2410 printf ("\ 2411 %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n", 2412 class == ELFCLASS32 ? 10 : 18, rel->r_offset, 2413 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info)) 2414 /* Avoid the leading R_ which isn't carrying any 2415 information. */ 2416 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info), 2417 buf, sizeof (buf)) + 2 2418 : _("<INVALID RELOC>"), 2419 class == ELFCLASS32 ? 10 : 18, sym->st_value, 2420 rel->r_addend, 2421 elf_strptr (ebl->elf, shstrndx, secshdr->sh_name)); 2422 } 2423 } 2424 } 2425} 2426 2427 2428/* Print the program header. */ 2429static void 2430print_symtab (Ebl *ebl, int type) 2431{ 2432 /* Find the symbol table(s). For this we have to search through the 2433 section table. */ 2434 Elf_Scn *scn = NULL; 2435 2436 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 2437 { 2438 /* Handle the section if it is a symbol table. */ 2439 GElf_Shdr shdr_mem; 2440 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 2441 2442 if (shdr != NULL && shdr->sh_type == (GElf_Word) type) 2443 { 2444 if (symbol_table_section != NULL) 2445 { 2446 /* Get the section header string table index. */ 2447 size_t shstrndx; 2448 const char *sname; 2449 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 2450 error_exit (0, 2451 _("cannot get section header string table index")); 2452 sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name); 2453 if (sname == NULL || strcmp (sname, symbol_table_section) != 0) 2454 continue; 2455 } 2456 2457 if ((shdr->sh_flags & SHF_COMPRESSED) != 0) 2458 { 2459 if (elf_compress (scn, 0, 0) < 0) 2460 printf ("WARNING: %s [%zd]\n", 2461 _("Couldn't uncompress section"), 2462 elf_ndxscn (scn)); 2463 shdr = gelf_getshdr (scn, &shdr_mem); 2464 if (unlikely (shdr == NULL)) 2465 error_exit (0, 2466 _("cannot get section [%zd] header: %s"), 2467 elf_ndxscn (scn), elf_errmsg (-1)); 2468 } 2469 handle_symtab (ebl, scn, shdr); 2470 } 2471 } 2472} 2473 2474 2475static void 2476handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr) 2477{ 2478 Elf_Data *versym_data = NULL; 2479 Elf_Data *verneed_data = NULL; 2480 Elf_Data *verdef_data = NULL; 2481 Elf_Data *xndx_data = NULL; 2482 int class = gelf_getclass (ebl->elf); 2483 Elf32_Word verneed_stridx = 0; 2484 Elf32_Word verdef_stridx = 0; 2485 2486 /* Get the data of the section. */ 2487 Elf_Data *data = elf_getdata (scn, NULL); 2488 if (data == NULL) 2489 return; 2490 2491 /* Find out whether we have other sections we might need. */ 2492 Elf_Scn *runscn = NULL; 2493 while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL) 2494 { 2495 GElf_Shdr runshdr_mem; 2496 GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem); 2497 2498 if (likely (runshdr != NULL)) 2499 { 2500 if (runshdr->sh_type == SHT_GNU_versym 2501 && runshdr->sh_link == elf_ndxscn (scn)) 2502 /* Bingo, found the version information. Now get the data. */ 2503 versym_data = elf_getdata (runscn, NULL); 2504 else if (runshdr->sh_type == SHT_GNU_verneed) 2505 { 2506 /* This is the information about the needed versions. */ 2507 verneed_data = elf_getdata (runscn, NULL); 2508 verneed_stridx = runshdr->sh_link; 2509 } 2510 else if (runshdr->sh_type == SHT_GNU_verdef) 2511 { 2512 /* This is the information about the defined versions. */ 2513 verdef_data = elf_getdata (runscn, NULL); 2514 verdef_stridx = runshdr->sh_link; 2515 } 2516 else if (runshdr->sh_type == SHT_SYMTAB_SHNDX 2517 && runshdr->sh_link == elf_ndxscn (scn)) 2518 /* Extended section index. */ 2519 xndx_data = elf_getdata (runscn, NULL); 2520 } 2521 } 2522 2523 /* Get the section header string table index. */ 2524 size_t shstrndx; 2525 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 2526 error_exit (0, _("cannot get section header string table index")); 2527 2528 GElf_Shdr glink_mem; 2529 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), 2530 &glink_mem); 2531 if (glink == NULL) 2532 error_exit (0, _("invalid sh_link value in section %zu"), 2533 elf_ndxscn (scn)); 2534 2535 /* Now we can compute the number of entries in the section. */ 2536 unsigned int nsyms = data->d_size / (class == ELFCLASS32 2537 ? sizeof (Elf32_Sym) 2538 : sizeof (Elf64_Sym)); 2539 2540 printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n", 2541 "\nSymbol table [%2u] '%s' contains %u entries:\n", 2542 nsyms), 2543 (unsigned int) elf_ndxscn (scn), 2544 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms); 2545 printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n", 2546 " %lu local symbols String table: [%2u] '%s'\n", 2547 shdr->sh_info), 2548 (unsigned long int) shdr->sh_info, 2549 (unsigned int) shdr->sh_link, 2550 elf_strptr (ebl->elf, shstrndx, glink->sh_name)); 2551 2552 fputs_unlocked (class == ELFCLASS32 2553 ? _("\ 2554 Num: Value Size Type Bind Vis Ndx Name\n") 2555 : _("\ 2556 Num: Value Size Type Bind Vis Ndx Name\n"), 2557 stdout); 2558 2559 for (unsigned int cnt = 0; cnt < nsyms; ++cnt) 2560 { 2561 char typebuf[64]; 2562 char bindbuf[64]; 2563 char scnbuf[64]; 2564 Elf32_Word xndx; 2565 GElf_Sym sym_mem; 2566 GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx); 2567 2568 if (unlikely (sym == NULL)) 2569 continue; 2570 2571 /* Determine the real section index. */ 2572 if (likely (sym->st_shndx != SHN_XINDEX)) 2573 xndx = sym->st_shndx; 2574 2575 printf (_("\ 2576%5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"), 2577 cnt, 2578 class == ELFCLASS32 ? 8 : 16, 2579 sym->st_value, 2580 sym->st_size, 2581 ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), 2582 typebuf, sizeof (typebuf)), 2583 ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info), 2584 bindbuf, sizeof (bindbuf)), 2585 get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)), 2586 ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf, 2587 sizeof (scnbuf), NULL, shnum), 2588 elf_strptr (ebl->elf, shdr->sh_link, sym->st_name)); 2589 2590 if (versym_data != NULL) 2591 { 2592 /* Get the version information. */ 2593 GElf_Versym versym_mem; 2594 GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem); 2595 2596 if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1)) 2597 { 2598 bool is_nobits = false; 2599 bool check_def = xndx != SHN_UNDEF; 2600 2601 if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX) 2602 { 2603 GElf_Shdr symshdr_mem; 2604 GElf_Shdr *symshdr = 2605 gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem); 2606 2607 is_nobits = (symshdr != NULL 2608 && symshdr->sh_type == SHT_NOBITS); 2609 } 2610 2611 if (is_nobits || ! check_def) 2612 { 2613 /* We must test both. */ 2614 GElf_Vernaux vernaux_mem; 2615 GElf_Vernaux *vernaux = NULL; 2616 size_t vn_offset = 0; 2617 2618 GElf_Verneed verneed_mem; 2619 GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0, 2620 &verneed_mem); 2621 while (verneed != NULL) 2622 { 2623 size_t vna_offset = vn_offset; 2624 2625 vernaux = gelf_getvernaux (verneed_data, 2626 vna_offset += verneed->vn_aux, 2627 &vernaux_mem); 2628 while (vernaux != NULL 2629 && vernaux->vna_other != *versym 2630 && vernaux->vna_next != 0 2631 && (verneed_data->d_size - vna_offset 2632 >= vernaux->vna_next)) 2633 { 2634 /* Update the offset. */ 2635 vna_offset += vernaux->vna_next; 2636 2637 vernaux = (vernaux->vna_next == 0 2638 ? NULL 2639 : gelf_getvernaux (verneed_data, 2640 vna_offset, 2641 &vernaux_mem)); 2642 } 2643 2644 /* Check whether we found the version. */ 2645 if (vernaux != NULL && vernaux->vna_other == *versym) 2646 /* Found it. */ 2647 break; 2648 2649 if (verneed_data->d_size - vn_offset < verneed->vn_next) 2650 break; 2651 2652 vn_offset += verneed->vn_next; 2653 verneed = (verneed->vn_next == 0 2654 ? NULL 2655 : gelf_getverneed (verneed_data, vn_offset, 2656 &verneed_mem)); 2657 } 2658 2659 if (vernaux != NULL && vernaux->vna_other == *versym) 2660 { 2661 printf ("@%s (%u)", 2662 elf_strptr (ebl->elf, verneed_stridx, 2663 vernaux->vna_name), 2664 (unsigned int) vernaux->vna_other); 2665 check_def = 0; 2666 } 2667 else if (unlikely (! is_nobits)) 2668 error (0, 0, _("bad dynamic symbol")); 2669 else 2670 check_def = 1; 2671 } 2672 2673 if (check_def && *versym != 0x8001) 2674 { 2675 /* We must test both. */ 2676 size_t vd_offset = 0; 2677 2678 GElf_Verdef verdef_mem; 2679 GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0, 2680 &verdef_mem); 2681 while (verdef != NULL) 2682 { 2683 if (verdef->vd_ndx == (*versym & 0x7fff)) 2684 /* Found the definition. */ 2685 break; 2686 2687 if (verdef_data->d_size - vd_offset < verdef->vd_next) 2688 break; 2689 2690 vd_offset += verdef->vd_next; 2691 verdef = (verdef->vd_next == 0 2692 ? NULL 2693 : gelf_getverdef (verdef_data, vd_offset, 2694 &verdef_mem)); 2695 } 2696 2697 if (verdef != NULL) 2698 { 2699 GElf_Verdaux verdaux_mem; 2700 GElf_Verdaux *verdaux 2701 = gelf_getverdaux (verdef_data, 2702 vd_offset + verdef->vd_aux, 2703 &verdaux_mem); 2704 2705 if (verdaux != NULL) 2706 printf ((*versym & 0x8000) ? "@%s" : "@@%s", 2707 elf_strptr (ebl->elf, verdef_stridx, 2708 verdaux->vda_name)); 2709 } 2710 } 2711 } 2712 } 2713 2714 putchar_unlocked ('\n'); 2715 } 2716} 2717 2718 2719/* Print version information. */ 2720static void 2721print_verinfo (Ebl *ebl) 2722{ 2723 /* Find the version information sections. For this we have to 2724 search through the section table. */ 2725 Elf_Scn *scn = NULL; 2726 2727 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 2728 { 2729 /* Handle the section if it is part of the versioning handling. */ 2730 GElf_Shdr shdr_mem; 2731 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 2732 2733 if (likely (shdr != NULL)) 2734 { 2735 if (shdr->sh_type == SHT_GNU_verneed) 2736 handle_verneed (ebl, scn, shdr); 2737 else if (shdr->sh_type == SHT_GNU_verdef) 2738 handle_verdef (ebl, scn, shdr); 2739 else if (shdr->sh_type == SHT_GNU_versym) 2740 handle_versym (ebl, scn, shdr); 2741 } 2742 } 2743} 2744 2745 2746static const char * 2747get_ver_flags (unsigned int flags) 2748{ 2749 static char buf[32]; 2750 char *endp; 2751 2752 if (flags == 0) 2753 return _("none"); 2754 2755 if (flags & VER_FLG_BASE) 2756 endp = stpcpy (buf, "BASE "); 2757 else 2758 endp = buf; 2759 2760 if (flags & VER_FLG_WEAK) 2761 { 2762 if (endp != buf) 2763 endp = stpcpy (endp, "| "); 2764 2765 endp = stpcpy (endp, "WEAK "); 2766 } 2767 2768 if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK))) 2769 { 2770 strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp); 2771 buf[sizeof (buf) - 1] = '\0'; 2772 } 2773 2774 return buf; 2775} 2776 2777 2778static void 2779handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr) 2780{ 2781 int class = gelf_getclass (ebl->elf); 2782 2783 /* Get the data of the section. */ 2784 Elf_Data *data = elf_getdata (scn, NULL); 2785 if (data == NULL) 2786 return; 2787 2788 /* Get the section header string table index. */ 2789 size_t shstrndx; 2790 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 2791 error_exit (0, _("cannot get section header string table index")); 2792 2793 GElf_Shdr glink_mem; 2794 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), 2795 &glink_mem); 2796 if (glink == NULL) 2797 error_exit (0, _("invalid sh_link value in section %zu"), 2798 elf_ndxscn (scn)); 2799 2800 printf (ngettext ("\ 2801\nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n", 2802 "\ 2803\nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n", 2804 shdr->sh_info), 2805 (unsigned int) elf_ndxscn (scn), 2806 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info, 2807 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr, 2808 shdr->sh_offset, 2809 (unsigned int) shdr->sh_link, 2810 elf_strptr (ebl->elf, shstrndx, glink->sh_name)); 2811 2812 unsigned int offset = 0; 2813 for (int cnt = shdr->sh_info; --cnt >= 0; ) 2814 { 2815 /* Get the data at the next offset. */ 2816 GElf_Verneed needmem; 2817 GElf_Verneed *need = gelf_getverneed (data, offset, &needmem); 2818 if (unlikely (need == NULL)) 2819 break; 2820 2821 printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"), 2822 offset, (unsigned short int) need->vn_version, 2823 elf_strptr (ebl->elf, shdr->sh_link, need->vn_file), 2824 (unsigned short int) need->vn_cnt); 2825 2826 unsigned int auxoffset = offset + need->vn_aux; 2827 for (int cnt2 = need->vn_cnt; --cnt2 >= 0; ) 2828 { 2829 GElf_Vernaux auxmem; 2830 GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem); 2831 if (unlikely (aux == NULL)) 2832 break; 2833 2834 printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"), 2835 auxoffset, 2836 elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name), 2837 get_ver_flags (aux->vna_flags), 2838 (unsigned short int) aux->vna_other); 2839 2840 if (aux->vna_next == 0) 2841 break; 2842 2843 auxoffset += aux->vna_next; 2844 } 2845 2846 /* Find the next offset. */ 2847 if (need->vn_next == 0) 2848 break; 2849 2850 offset += need->vn_next; 2851 } 2852} 2853 2854 2855static void 2856handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr) 2857{ 2858 /* Get the data of the section. */ 2859 Elf_Data *data = elf_getdata (scn, NULL); 2860 if (data == NULL) 2861 return; 2862 2863 /* Get the section header string table index. */ 2864 size_t shstrndx; 2865 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 2866 error_exit (0, _("cannot get section header string table index")); 2867 2868 GElf_Shdr glink_mem; 2869 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), 2870 &glink_mem); 2871 if (glink == NULL) 2872 error_exit (0, _("invalid sh_link value in section %zu"), 2873 elf_ndxscn (scn)); 2874 2875 int class = gelf_getclass (ebl->elf); 2876 printf (ngettext ("\ 2877\nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n", 2878 "\ 2879\nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n", 2880 shdr->sh_info), 2881 (unsigned int) elf_ndxscn (scn), 2882 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 2883 shdr->sh_info, 2884 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr, 2885 shdr->sh_offset, 2886 (unsigned int) shdr->sh_link, 2887 elf_strptr (ebl->elf, shstrndx, glink->sh_name)); 2888 2889 unsigned int offset = 0; 2890 for (int cnt = shdr->sh_info; --cnt >= 0; ) 2891 { 2892 /* Get the data at the next offset. */ 2893 GElf_Verdef defmem; 2894 GElf_Verdef *def = gelf_getverdef (data, offset, &defmem); 2895 if (unlikely (def == NULL)) 2896 break; 2897 2898 unsigned int auxoffset = offset + def->vd_aux; 2899 GElf_Verdaux auxmem; 2900 GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem); 2901 if (unlikely (aux == NULL)) 2902 break; 2903 2904 printf (_("\ 2905 %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"), 2906 offset, def->vd_version, 2907 get_ver_flags (def->vd_flags), 2908 def->vd_ndx, 2909 def->vd_cnt, 2910 elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name)); 2911 2912 auxoffset += aux->vda_next; 2913 for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2) 2914 { 2915 aux = gelf_getverdaux (data, auxoffset, &auxmem); 2916 if (unlikely (aux == NULL)) 2917 break; 2918 2919 printf (_(" %#06x: Parent %d: %s\n"), 2920 auxoffset, cnt2, 2921 elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name)); 2922 2923 if (aux->vda_next == 0) 2924 break; 2925 2926 auxoffset += aux->vda_next; 2927 } 2928 2929 /* Find the next offset. */ 2930 if (def->vd_next == 0) 2931 break; 2932 offset += def->vd_next; 2933 } 2934} 2935 2936 2937static void 2938handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr) 2939{ 2940 int class = gelf_getclass (ebl->elf); 2941 const char **vername; 2942 const char **filename; 2943 2944 /* Get the data of the section. */ 2945 Elf_Data *data = elf_getdata (scn, NULL); 2946 if (data == NULL) 2947 return; 2948 2949 /* Get the section header string table index. */ 2950 size_t shstrndx; 2951 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 2952 error_exit (0, _("cannot get section header string table index")); 2953 2954 /* We have to find the version definition section and extract the 2955 version names. */ 2956 Elf_Scn *defscn = NULL; 2957 Elf_Scn *needscn = NULL; 2958 2959 Elf_Scn *verscn = NULL; 2960 while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL) 2961 { 2962 GElf_Shdr vershdr_mem; 2963 GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem); 2964 2965 if (likely (vershdr != NULL)) 2966 { 2967 if (vershdr->sh_type == SHT_GNU_verdef) 2968 defscn = verscn; 2969 else if (vershdr->sh_type == SHT_GNU_verneed) 2970 needscn = verscn; 2971 } 2972 } 2973 2974 size_t nvername; 2975 if (defscn != NULL || needscn != NULL) 2976 { 2977 /* We have a version information (better should have). Now get 2978 the version names. First find the maximum version number. */ 2979 nvername = 0; 2980 if (defscn != NULL) 2981 { 2982 /* Run through the version definitions and find the highest 2983 index. */ 2984 unsigned int offset = 0; 2985 Elf_Data *defdata; 2986 GElf_Shdr defshdrmem; 2987 GElf_Shdr *defshdr; 2988 2989 defdata = elf_getdata (defscn, NULL); 2990 if (unlikely (defdata == NULL)) 2991 return; 2992 2993 defshdr = gelf_getshdr (defscn, &defshdrmem); 2994 if (unlikely (defshdr == NULL)) 2995 return; 2996 2997 for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt) 2998 { 2999 GElf_Verdef defmem; 3000 GElf_Verdef *def; 3001 3002 /* Get the data at the next offset. */ 3003 def = gelf_getverdef (defdata, offset, &defmem); 3004 if (unlikely (def == NULL)) 3005 break; 3006 3007 nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff)); 3008 3009 if (def->vd_next == 0) 3010 break; 3011 offset += def->vd_next; 3012 } 3013 } 3014 if (needscn != NULL) 3015 { 3016 unsigned int offset = 0; 3017 Elf_Data *needdata; 3018 GElf_Shdr needshdrmem; 3019 GElf_Shdr *needshdr; 3020 3021 needdata = elf_getdata (needscn, NULL); 3022 if (unlikely (needdata == NULL)) 3023 return; 3024 3025 needshdr = gelf_getshdr (needscn, &needshdrmem); 3026 if (unlikely (needshdr == NULL)) 3027 return; 3028 3029 for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt) 3030 { 3031 GElf_Verneed needmem; 3032 GElf_Verneed *need; 3033 unsigned int auxoffset; 3034 int cnt2; 3035 3036 /* Get the data at the next offset. */ 3037 need = gelf_getverneed (needdata, offset, &needmem); 3038 if (unlikely (need == NULL)) 3039 break; 3040 3041 /* Run through the auxiliary entries. */ 3042 auxoffset = offset + need->vn_aux; 3043 for (cnt2 = need->vn_cnt; --cnt2 >= 0; ) 3044 { 3045 GElf_Vernaux auxmem; 3046 GElf_Vernaux *aux; 3047 3048 aux = gelf_getvernaux (needdata, auxoffset, &auxmem); 3049 if (unlikely (aux == NULL)) 3050 break; 3051 3052 nvername = MAX (nvername, 3053 (size_t) (aux->vna_other & 0x7fff)); 3054 3055 if (aux->vna_next == 0) 3056 break; 3057 auxoffset += aux->vna_next; 3058 } 3059 3060 if (need->vn_next == 0) 3061 break; 3062 offset += need->vn_next; 3063 } 3064 } 3065 3066 /* This is the number of versions we know about. */ 3067 ++nvername; 3068 3069 /* Allocate the array. */ 3070 vername = (const char **) alloca (nvername * sizeof (const char *)); 3071 memset(vername, 0, nvername * sizeof (const char *)); 3072 filename = (const char **) alloca (nvername * sizeof (const char *)); 3073 memset(filename, 0, nvername * sizeof (const char *)); 3074 3075 /* Run through the data structures again and collect the strings. */ 3076 if (defscn != NULL) 3077 { 3078 /* Run through the version definitions and find the highest 3079 index. */ 3080 unsigned int offset = 0; 3081 Elf_Data *defdata; 3082 GElf_Shdr defshdrmem; 3083 GElf_Shdr *defshdr; 3084 3085 defdata = elf_getdata (defscn, NULL); 3086 if (unlikely (defdata == NULL)) 3087 return; 3088 3089 defshdr = gelf_getshdr (defscn, &defshdrmem); 3090 if (unlikely (defshdr == NULL)) 3091 return; 3092 3093 for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt) 3094 { 3095 3096 /* Get the data at the next offset. */ 3097 GElf_Verdef defmem; 3098 GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem); 3099 if (unlikely (def == NULL)) 3100 break; 3101 3102 GElf_Verdaux auxmem; 3103 GElf_Verdaux *aux = gelf_getverdaux (defdata, 3104 offset + def->vd_aux, 3105 &auxmem); 3106 if (unlikely (aux == NULL)) 3107 break; 3108 3109 vername[def->vd_ndx & 0x7fff] 3110 = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name); 3111 filename[def->vd_ndx & 0x7fff] = NULL; 3112 3113 if (def->vd_next == 0) 3114 break; 3115 offset += def->vd_next; 3116 } 3117 } 3118 if (needscn != NULL) 3119 { 3120 unsigned int offset = 0; 3121 3122 Elf_Data *needdata = elf_getdata (needscn, NULL); 3123 GElf_Shdr needshdrmem; 3124 GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem); 3125 if (unlikely (needdata == NULL || needshdr == NULL)) 3126 return; 3127 3128 for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt) 3129 { 3130 /* Get the data at the next offset. */ 3131 GElf_Verneed needmem; 3132 GElf_Verneed *need = gelf_getverneed (needdata, offset, 3133 &needmem); 3134 if (unlikely (need == NULL)) 3135 break; 3136 3137 /* Run through the auxiliary entries. */ 3138 unsigned int auxoffset = offset + need->vn_aux; 3139 for (int cnt2 = need->vn_cnt; --cnt2 >= 0; ) 3140 { 3141 GElf_Vernaux auxmem; 3142 GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset, 3143 &auxmem); 3144 if (unlikely (aux == NULL)) 3145 break; 3146 3147 vername[aux->vna_other & 0x7fff] 3148 = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name); 3149 filename[aux->vna_other & 0x7fff] 3150 = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file); 3151 3152 if (aux->vna_next == 0) 3153 break; 3154 auxoffset += aux->vna_next; 3155 } 3156 3157 if (need->vn_next == 0) 3158 break; 3159 offset += need->vn_next; 3160 } 3161 } 3162 } 3163 else 3164 { 3165 vername = NULL; 3166 nvername = 1; 3167 filename = NULL; 3168 } 3169 3170 GElf_Shdr glink_mem; 3171 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), 3172 &glink_mem); 3173 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT); 3174 if (glink == NULL) 3175 error_exit (0, _("invalid sh_link value in section %zu"), 3176 elf_ndxscn (scn)); 3177 3178 /* Print the header. */ 3179 printf (ngettext ("\ 3180\nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'", 3181 "\ 3182\nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'", 3183 shdr->sh_size / sh_entsize), 3184 (unsigned int) elf_ndxscn (scn), 3185 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 3186 (int) (shdr->sh_size / sh_entsize), 3187 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr, 3188 shdr->sh_offset, 3189 (unsigned int) shdr->sh_link, 3190 elf_strptr (ebl->elf, shstrndx, glink->sh_name)); 3191 3192 /* Now we can finally look at the actual contents of this section. */ 3193 for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt) 3194 { 3195 if (cnt % 2 == 0) 3196 printf ("\n %4d:", cnt); 3197 3198 GElf_Versym symmem; 3199 GElf_Versym *sym = gelf_getversym (data, cnt, &symmem); 3200 if (sym == NULL) 3201 break; 3202 3203 switch (*sym) 3204 { 3205 ssize_t n; 3206 case 0: 3207 fputs_unlocked (_(" 0 *local* "), 3208 stdout); 3209 break; 3210 3211 case 1: 3212 fputs_unlocked (_(" 1 *global* "), 3213 stdout); 3214 break; 3215 3216 default: 3217 n = printf ("%4d%c%s", 3218 *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ', 3219 (vername != NULL 3220 && (unsigned int) (*sym & 0x7fff) < nvername) 3221 ? vername[*sym & 0x7fff] : "???"); 3222 if ((unsigned int) (*sym & 0x7fff) < nvername 3223 && filename != NULL && filename[*sym & 0x7fff] != NULL) 3224 n += printf ("(%s)", filename[*sym & 0x7fff]); 3225 printf ("%*s", MAX (0, 33 - (int) n), " "); 3226 break; 3227 } 3228 } 3229 putchar_unlocked ('\n'); 3230} 3231 3232 3233static void 3234print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx, 3235 uint_fast32_t maxlength, Elf32_Word nbucket, 3236 uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr) 3237{ 3238 uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t)); 3239 3240 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt) 3241 ++counts[lengths[cnt]]; 3242 3243 GElf_Shdr glink_mem; 3244 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, 3245 shdr->sh_link), 3246 &glink_mem); 3247 if (glink == NULL) 3248 { 3249 error (0, 0, _("invalid sh_link value in section %zu"), 3250 elf_ndxscn (scn)); 3251 free (counts); 3252 return; 3253 } 3254 3255 printf (ngettext ("\ 3256\nHistogram for bucket list length in section [%2u] '%s' (total of %d bucket):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n", 3257 "\ 3258\nHistogram for bucket list length in section [%2u] '%s' (total of %d buckets):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n", 3259 nbucket), 3260 (unsigned int) elf_ndxscn (scn), 3261 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 3262 (int) nbucket, 3263 gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18, 3264 shdr->sh_addr, 3265 shdr->sh_offset, 3266 (unsigned int) shdr->sh_link, 3267 elf_strptr (ebl->elf, shstrndx, glink->sh_name)); 3268 3269 if (extrastr != NULL) 3270 fputs (extrastr, stdout); 3271 3272 if (likely (nbucket > 0)) 3273 { 3274 uint64_t success = 0; 3275 3276 /* xgettext:no-c-format */ 3277 fputs_unlocked (_("\ 3278 Length Number % of total Coverage\n"), stdout); 3279 printf (_(" 0 %6" PRIu32 " %5.1f%%\n"), 3280 counts[0], (counts[0] * 100.0) / nbucket); 3281 3282 uint64_t nzero_counts = 0; 3283 for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt) 3284 { 3285 nzero_counts += counts[cnt] * cnt; 3286 printf (_("\ 3287%7d %6" PRIu32 " %5.1f%% %5.1f%%\n"), 3288 (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket, 3289 (nzero_counts * 100.0) / nsyms); 3290 } 3291 3292 Elf32_Word acc = 0; 3293 for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt) 3294 { 3295 acc += cnt; 3296 success += counts[cnt] * acc; 3297 } 3298 3299 printf (_("\ 3300 Average number of tests: successful lookup: %f\n\ 3301 unsuccessful lookup: %f\n"), 3302 (double) success / (double) nzero_counts, 3303 (double) nzero_counts / (double) nbucket); 3304 } 3305 3306 free (counts); 3307} 3308 3309 3310/* This function handles the traditional System V-style hash table format. */ 3311static void 3312handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx) 3313{ 3314 Elf_Data *data = elf_getdata (scn, NULL); 3315 if (unlikely (data == NULL)) 3316 { 3317 error (0, 0, _("cannot get data for section %d: %s"), 3318 (int) elf_ndxscn (scn), elf_errmsg (-1)); 3319 return; 3320 } 3321 3322 if (unlikely (data->d_size < 2 * sizeof (Elf32_Word))) 3323 { 3324 invalid_data: 3325 error (0, 0, _("invalid data in sysv.hash section %d"), 3326 (int) elf_ndxscn (scn)); 3327 return; 3328 } 3329 3330 Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0]; 3331 Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1]; 3332 3333 uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word); 3334 if (used_buf > data->d_size) 3335 goto invalid_data; 3336 3337 Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2]; 3338 Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket]; 3339 3340 uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t)); 3341 3342 uint_fast32_t maxlength = 0; 3343 uint_fast32_t nsyms = 0; 3344 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt) 3345 { 3346 Elf32_Word inner = bucket[cnt]; 3347 Elf32_Word chain_len = 0; 3348 while (inner > 0 && inner < nchain) 3349 { 3350 ++nsyms; 3351 ++chain_len; 3352 if (chain_len > nchain) 3353 { 3354 error (0, 0, _("invalid chain in sysv.hash section %d"), 3355 (int) elf_ndxscn (scn)); 3356 free (lengths); 3357 return; 3358 } 3359 if (maxlength < ++lengths[cnt]) 3360 ++maxlength; 3361 3362 inner = chain[inner]; 3363 } 3364 } 3365 3366 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms, 3367 lengths, NULL); 3368 3369 free (lengths); 3370} 3371 3372 3373/* This function handles the incorrect, System V-style hash table 3374 format some 64-bit architectures use. */ 3375static void 3376handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx) 3377{ 3378 Elf_Data *data = elf_getdata (scn, NULL); 3379 if (unlikely (data == NULL)) 3380 { 3381 error (0, 0, _("cannot get data for section %d: %s"), 3382 (int) elf_ndxscn (scn), elf_errmsg (-1)); 3383 return; 3384 } 3385 3386 if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword))) 3387 { 3388 invalid_data: 3389 error (0, 0, _("invalid data in sysv.hash64 section %d"), 3390 (int) elf_ndxscn (scn)); 3391 return; 3392 } 3393 3394 Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0]; 3395 Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1]; 3396 3397 uint64_t maxwords = data->d_size / sizeof (Elf64_Xword); 3398 if (maxwords < 2 3399 || maxwords - 2 < nbucket 3400 || maxwords - 2 - nbucket < nchain) 3401 goto invalid_data; 3402 3403 Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2]; 3404 Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket]; 3405 3406 uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t)); 3407 3408 uint_fast32_t maxlength = 0; 3409 uint_fast32_t nsyms = 0; 3410 for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt) 3411 { 3412 Elf64_Xword inner = bucket[cnt]; 3413 Elf64_Xword chain_len = 0; 3414 while (inner > 0 && inner < nchain) 3415 { 3416 ++nsyms; 3417 ++chain_len; 3418 if (chain_len > nchain) 3419 { 3420 error (0, 0, _("invalid chain in sysv.hash64 section %d"), 3421 (int) elf_ndxscn (scn)); 3422 free (lengths); 3423 return; 3424 } 3425 if (maxlength < ++lengths[cnt]) 3426 ++maxlength; 3427 3428 inner = chain[inner]; 3429 } 3430 } 3431 3432 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms, 3433 lengths, NULL); 3434 3435 free (lengths); 3436} 3437 3438 3439/* This function handles the GNU-style hash table format. */ 3440static void 3441handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx) 3442{ 3443 uint32_t *lengths = NULL; 3444 Elf_Data *data = elf_getdata (scn, NULL); 3445 if (unlikely (data == NULL)) 3446 { 3447 error (0, 0, _("cannot get data for section %d: %s"), 3448 (int) elf_ndxscn (scn), elf_errmsg (-1)); 3449 return; 3450 } 3451 3452 if (unlikely (data->d_size < 4 * sizeof (Elf32_Word))) 3453 { 3454 invalid_data: 3455 free (lengths); 3456 error (0, 0, _("invalid data in gnu.hash section %d"), 3457 (int) elf_ndxscn (scn)); 3458 return; 3459 } 3460 3461 Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0]; 3462 Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1]; 3463 3464 /* Next comes the size of the bitmap. It's measured in words for 3465 the architecture. It's 32 bits for 32 bit archs, and 64 bits for 3466 64 bit archs. There is always a bloom filter present, so zero is 3467 an invalid value. */ 3468 Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2]; 3469 if (gelf_getclass (ebl->elf) == ELFCLASS64) 3470 bitmask_words *= 2; 3471 3472 if (bitmask_words == 0) 3473 goto invalid_data; 3474 3475 Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3]; 3476 3477 /* Is there still room for the sym chain? 3478 Use uint64_t calculation to prevent 32bit overflow. */ 3479 uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word); 3480 uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word); 3481 if (used_buf > data->d_size) 3482 goto invalid_data; 3483 3484 lengths = xcalloc (nbucket, sizeof (uint32_t)); 3485 3486 Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4]; 3487 Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words]; 3488 Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words 3489 + nbucket]; 3490 3491 /* Compute distribution of chain lengths. */ 3492 uint_fast32_t maxlength = 0; 3493 uint_fast32_t nsyms = 0; 3494 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt) 3495 if (bucket[cnt] != 0) 3496 { 3497 Elf32_Word inner = bucket[cnt] - symbias; 3498 do 3499 { 3500 ++nsyms; 3501 if (maxlength < ++lengths[cnt]) 3502 ++maxlength; 3503 if (inner >= max_nsyms) 3504 goto invalid_data; 3505 } 3506 while ((chain[inner++] & 1) == 0); 3507 } 3508 3509 /* Count bits in bitmask. */ 3510 uint_fast32_t nbits = 0; 3511 for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt) 3512 { 3513 uint_fast32_t word = bitmask[cnt]; 3514 3515 word = (word & 0x55555555) + ((word >> 1) & 0x55555555); 3516 word = (word & 0x33333333) + ((word >> 2) & 0x33333333); 3517 word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f); 3518 word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff); 3519 nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff); 3520 } 3521 3522 char *str = xasprintf (_("\ 3523 Symbol Bias: %u\n\ 3524 Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"), 3525 (unsigned int) symbias, 3526 bitmask_words * sizeof (Elf32_Word), 3527 ((nbits * 100 + 50) 3528 / (uint_fast32_t) (bitmask_words 3529 * sizeof (Elf32_Word) * 8)), 3530 (unsigned int) shift); 3531 3532 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms, 3533 lengths, str); 3534 3535 free (str); 3536 free (lengths); 3537} 3538 3539 3540/* Find the symbol table(s). For this we have to search through the 3541 section table. */ 3542static void 3543handle_hash (Ebl *ebl) 3544{ 3545 /* Get the section header string table index. */ 3546 size_t shstrndx; 3547 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 3548 error_exit (0, _("cannot get section header string table index")); 3549 3550 Elf_Scn *scn = NULL; 3551 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 3552 { 3553 /* Handle the section if it is a symbol table. */ 3554 GElf_Shdr shdr_mem; 3555 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 3556 3557 if (likely (shdr != NULL)) 3558 { 3559 if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH) 3560 && (shdr->sh_flags & SHF_COMPRESSED) != 0) 3561 { 3562 if (elf_compress (scn, 0, 0) < 0) 3563 printf ("WARNING: %s [%zd]\n", 3564 _("Couldn't uncompress section"), 3565 elf_ndxscn (scn)); 3566 shdr = gelf_getshdr (scn, &shdr_mem); 3567 if (unlikely (shdr == NULL)) 3568 error_exit (0, _("cannot get section [%zd] header: %s"), 3569 elf_ndxscn (scn), elf_errmsg (-1)); 3570 } 3571 3572 if (shdr->sh_type == SHT_HASH) 3573 { 3574 if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword)) 3575 handle_sysv_hash64 (ebl, scn, shdr, shstrndx); 3576 else 3577 handle_sysv_hash (ebl, scn, shdr, shstrndx); 3578 } 3579 else if (shdr->sh_type == SHT_GNU_HASH) 3580 handle_gnu_hash (ebl, scn, shdr, shstrndx); 3581 } 3582 } 3583} 3584 3585 3586static void 3587print_liblist (Ebl *ebl) 3588{ 3589 /* Find the library list sections. For this we have to search 3590 through the section table. */ 3591 Elf_Scn *scn = NULL; 3592 3593 /* Get the section header string table index. */ 3594 size_t shstrndx; 3595 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 3596 error_exit (0, _("cannot get section header string table index")); 3597 3598 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 3599 { 3600 GElf_Shdr shdr_mem; 3601 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 3602 3603 if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST) 3604 { 3605 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT); 3606 int nentries = shdr->sh_size / sh_entsize; 3607 printf (ngettext ("\ 3608\nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n", 3609 "\ 3610\nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n", 3611 nentries), 3612 elf_ndxscn (scn), 3613 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 3614 shdr->sh_offset, 3615 nentries); 3616 3617 Elf_Data *data = elf_getdata (scn, NULL); 3618 if (data == NULL) 3619 return; 3620 3621 puts (_("\ 3622 Library Time Stamp Checksum Version Flags")); 3623 3624 for (int cnt = 0; cnt < nentries; ++cnt) 3625 { 3626 GElf_Lib lib_mem; 3627 GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem); 3628 if (unlikely (lib == NULL)) 3629 continue; 3630 3631 time_t t = (time_t) lib->l_time_stamp; 3632 struct tm *tm = gmtime (&t); 3633 if (unlikely (tm == NULL)) 3634 continue; 3635 3636 printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n", 3637 cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name), 3638 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 3639 tm->tm_hour, tm->tm_min, tm->tm_sec, 3640 (unsigned int) lib->l_checksum, 3641 (unsigned int) lib->l_version, 3642 (unsigned int) lib->l_flags); 3643 } 3644 } 3645 } 3646} 3647 3648static inline size_t 3649left (Elf_Data *data, 3650 const unsigned char *p) 3651{ 3652 return (const unsigned char *) data->d_buf + data->d_size - p; 3653} 3654 3655static void 3656print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr) 3657{ 3658 /* Find the object attributes sections. For this we have to search 3659 through the section table. */ 3660 Elf_Scn *scn = NULL; 3661 3662 /* Get the section header string table index. */ 3663 size_t shstrndx; 3664 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 3665 error_exit (0, _("cannot get section header string table index")); 3666 3667 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 3668 { 3669 GElf_Shdr shdr_mem; 3670 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 3671 3672 if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES 3673 && (shdr->sh_type != SHT_ARM_ATTRIBUTES 3674 || ehdr->e_machine != EM_ARM) 3675 && (shdr->sh_type != SHT_CSKY_ATTRIBUTES 3676 || ehdr->e_machine != EM_CSKY) 3677 && (shdr->sh_type != SHT_RISCV_ATTRIBUTES 3678 || ehdr->e_machine != EM_RISCV))) 3679 continue; 3680 3681 printf (_("\ 3682\nObject attributes section [%2zu] '%s' of %" PRIu64 3683 " bytes at offset %#0" PRIx64 ":\n"), 3684 elf_ndxscn (scn), 3685 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 3686 shdr->sh_size, shdr->sh_offset); 3687 3688 Elf_Data *data = elf_rawdata (scn, NULL); 3689 if (unlikely (data == NULL || data->d_size == 0)) 3690 return; 3691 3692 const unsigned char *p = data->d_buf; 3693 3694 /* There is only one 'version', A. */ 3695 if (unlikely (*p++ != 'A')) 3696 return; 3697 3698 fputs_unlocked (_(" Owner Size\n"), stdout); 3699 3700 /* Loop over the sections. */ 3701 while (left (data, p) >= 4) 3702 { 3703 /* Section length. */ 3704 uint32_t len; 3705 memcpy (&len, p, sizeof len); 3706 3707 if (MY_ELFDATA != ehdr->e_ident[EI_DATA]) 3708 CONVERT (len); 3709 3710 if (unlikely (len > left (data, p))) 3711 break; 3712 3713 /* Section vendor name. */ 3714 const unsigned char *name = p + sizeof len; 3715 p += len; 3716 3717 unsigned const char *q = memchr (name, '\0', len); 3718 if (unlikely (q == NULL)) 3719 break; 3720 ++q; 3721 3722 printf (_(" %-13s %4" PRIu32 "\n"), name, len); 3723 3724 bool gnu_vendor = (q - name == sizeof "gnu" 3725 && !memcmp (name, "gnu", sizeof "gnu")); 3726 3727 /* Loop over subsections. */ 3728 if (shdr->sh_type != SHT_GNU_ATTRIBUTES 3729 || gnu_vendor) 3730 while (q < p) 3731 { 3732 const unsigned char *const sub = q; 3733 3734 unsigned int subsection_tag; 3735 get_uleb128 (subsection_tag, q, p); 3736 if (unlikely (q >= p)) 3737 break; 3738 3739 uint32_t subsection_len; 3740 if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len)) 3741 break; 3742 3743 memcpy (&subsection_len, q, sizeof subsection_len); 3744 3745 if (MY_ELFDATA != ehdr->e_ident[EI_DATA]) 3746 CONVERT (subsection_len); 3747 3748 /* Don't overflow, ptrdiff_t might be 32bits, but signed. */ 3749 if (unlikely (subsection_len == 0 3750 || subsection_len >= (uint32_t) PTRDIFF_MAX 3751 || p - sub < (ptrdiff_t) subsection_len)) 3752 break; 3753 3754 const unsigned char *r = q + sizeof subsection_len; 3755 q = sub + subsection_len; 3756 3757 switch (subsection_tag) 3758 { 3759 default: 3760 /* Unknown subsection, print and skip. */ 3761 printf (_(" %-4u %12" PRIu32 "\n"), 3762 subsection_tag, subsection_len); 3763 break; 3764 3765 case 1: /* Tag_File */ 3766 printf (_(" File: %11" PRIu32 "\n"), 3767 subsection_len); 3768 3769 while (r < q) 3770 { 3771 unsigned int tag; 3772 get_uleb128 (tag, r, q); 3773 if (unlikely (r >= q)) 3774 break; 3775 3776 /* GNU style tags have either a uleb128 value, 3777 when lowest bit is not set, or a string 3778 when the lowest bit is set. 3779 "compatibility" (32) is special. It has 3780 both a string and a uleb128 value. For 3781 non-gnu we assume 6 till 31 only take ints. 3782 XXX see arm backend, do we need a separate 3783 hook? */ 3784 uint64_t value = 0; 3785 const char *string = NULL; 3786 if (tag == 32 || (tag & 1) == 0 3787 || (! gnu_vendor && (tag > 5 && tag < 32))) 3788 { 3789 get_uleb128 (value, r, q); 3790 if (r > q) 3791 break; 3792 } 3793 if (tag == 32 3794 || ((tag & 1) != 0 3795 && (gnu_vendor 3796 || (! gnu_vendor && tag > 32))) 3797 || (! gnu_vendor && tag > 3 && tag < 6)) 3798 { 3799 string = (const char *) r; 3800 r = memchr (r, '\0', q - r); 3801 if (r == NULL) 3802 break; 3803 ++r; 3804 } 3805 3806 const char *tag_name = NULL; 3807 const char *value_name = NULL; 3808 ebl_check_object_attribute (ebl, (const char *) name, 3809 tag, value, 3810 &tag_name, &value_name); 3811 3812 if (tag_name != NULL) 3813 { 3814 if (tag == 32) 3815 printf (_(" %s: %" PRId64 ", %s\n"), 3816 tag_name, value, string); 3817 else if (string == NULL && value_name == NULL) 3818 printf (_(" %s: %" PRId64 "\n"), 3819 tag_name, value); 3820 else 3821 printf (_(" %s: %s\n"), 3822 tag_name, string ?: value_name); 3823 } 3824 else 3825 { 3826 /* For "gnu" vendor 32 "compatibility" has 3827 already been handled above. */ 3828 assert (tag != 32 3829 || strcmp ((const char *) name, "gnu")); 3830 if (string == NULL) 3831 printf (_(" %u: %" PRId64 "\n"), 3832 tag, value); 3833 else 3834 printf (_(" %u: %s\n"), 3835 tag, string); 3836 } 3837 } 3838 } 3839 } 3840 } 3841 } 3842} 3843 3844 3845void 3846print_dwarf_addr (Dwfl_Module *dwflmod, 3847 int address_size, Dwarf_Addr address, Dwarf_Addr raw) 3848{ 3849 /* See if there is a name we can give for this address. */ 3850 GElf_Sym sym; 3851 GElf_Off off = 0; 3852 const char *name = (print_address_names && ! print_unresolved_addresses) 3853 ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL) 3854 : NULL; 3855 3856 const char *scn; 3857 if (print_unresolved_addresses) 3858 { 3859 address = raw; 3860 scn = NULL; 3861 } 3862 else 3863 { 3864 /* Relativize the address. */ 3865 int n = dwfl_module_relocations (dwflmod); 3866 int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address); 3867 3868 /* In an ET_REL file there is a section name to refer to. */ 3869 scn = (i < 0 ? NULL 3870 : dwfl_module_relocation_info (dwflmod, i, NULL)); 3871 } 3872 3873 if ((name != NULL 3874 ? (off != 0 3875 ? (scn != NULL 3876 ? (address_size == 0 3877 ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">", 3878 scn, address, name, off) 3879 : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">", 3880 scn, 2 + address_size * 2, address, 3881 name, off)) 3882 : (address_size == 0 3883 ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">", 3884 address, name, off) 3885 : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">", 3886 2 + address_size * 2, address, 3887 name, off))) 3888 : (scn != NULL 3889 ? (address_size == 0 3890 ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name) 3891 : printf ("%s+%#0*" PRIx64 " <%s>", 3892 scn, 2 + address_size * 2, address, name)) 3893 : (address_size == 0 3894 ? printf ("%#" PRIx64 " <%s>", address, name) 3895 : printf ("%#0*" PRIx64 " <%s>", 3896 2 + address_size * 2, address, name)))) 3897 : (scn != NULL 3898 ? (address_size == 0 3899 ? printf ("%s+%#" PRIx64, scn, address) 3900 : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address)) 3901 : (address_size == 0 3902 ? printf ("%#" PRIx64, address) 3903 : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0) 3904 error_exit (0, _("sprintf failure")); 3905} 3906 3907 3908static const char * 3909dwarf_tag_string (unsigned int tag) 3910{ 3911 switch (tag) 3912 { 3913#define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME; 3914 DWARF_ALL_KNOWN_DW_TAG 3915#undef DWARF_ONE_KNOWN_DW_TAG 3916 default: 3917 return NULL; 3918 } 3919} 3920 3921 3922static const char * 3923dwarf_attr_string (unsigned int attrnum) 3924{ 3925 switch (attrnum) 3926 { 3927#define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME; 3928 DWARF_ALL_KNOWN_DW_AT 3929#undef DWARF_ONE_KNOWN_DW_AT 3930 default: 3931 return NULL; 3932 } 3933} 3934 3935 3936static const char * 3937dwarf_form_string (unsigned int form) 3938{ 3939 switch (form) 3940 { 3941#define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME; 3942 DWARF_ALL_KNOWN_DW_FORM 3943#undef DWARF_ONE_KNOWN_DW_FORM 3944 default: 3945 return NULL; 3946 } 3947} 3948 3949 3950static const char * 3951dwarf_lang_string (unsigned int lang) 3952{ 3953 switch (lang) 3954 { 3955#define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME; 3956 DWARF_ALL_KNOWN_DW_LANG 3957#undef DWARF_ONE_KNOWN_DW_LANG 3958 default: 3959 return NULL; 3960 } 3961} 3962 3963 3964static const char * 3965dwarf_inline_string (unsigned int code) 3966{ 3967 static const char *const known[] = 3968 { 3969#define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME, 3970 DWARF_ALL_KNOWN_DW_INL 3971#undef DWARF_ONE_KNOWN_DW_INL 3972 }; 3973 3974 if (likely (code < sizeof (known) / sizeof (known[0]))) 3975 return known[code]; 3976 3977 return NULL; 3978} 3979 3980 3981static const char * 3982dwarf_encoding_string (unsigned int code) 3983{ 3984 static const char *const known[] = 3985 { 3986#define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME, 3987 DWARF_ALL_KNOWN_DW_ATE 3988#undef DWARF_ONE_KNOWN_DW_ATE 3989 }; 3990 3991 if (likely (code < sizeof (known) / sizeof (known[0]))) 3992 return known[code]; 3993 3994 return NULL; 3995} 3996 3997 3998static const char * 3999dwarf_access_string (unsigned int code) 4000{ 4001 static const char *const known[] = 4002 { 4003#define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME, 4004 DWARF_ALL_KNOWN_DW_ACCESS 4005#undef DWARF_ONE_KNOWN_DW_ACCESS 4006 }; 4007 4008 if (likely (code < sizeof (known) / sizeof (known[0]))) 4009 return known[code]; 4010 4011 return NULL; 4012} 4013 4014 4015static const char * 4016dwarf_defaulted_string (unsigned int code) 4017{ 4018 static const char *const known[] = 4019 { 4020#define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME, 4021 DWARF_ALL_KNOWN_DW_DEFAULTED 4022#undef DWARF_ONE_KNOWN_DW_DEFAULTED 4023 }; 4024 4025 if (likely (code < sizeof (known) / sizeof (known[0]))) 4026 return known[code]; 4027 4028 return NULL; 4029} 4030 4031 4032static const char * 4033dwarf_visibility_string (unsigned int code) 4034{ 4035 static const char *const known[] = 4036 { 4037#define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME, 4038 DWARF_ALL_KNOWN_DW_VIS 4039#undef DWARF_ONE_KNOWN_DW_VIS 4040 }; 4041 4042 if (likely (code < sizeof (known) / sizeof (known[0]))) 4043 return known[code]; 4044 4045 return NULL; 4046} 4047 4048 4049static const char * 4050dwarf_virtuality_string (unsigned int code) 4051{ 4052 static const char *const known[] = 4053 { 4054#define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME, 4055 DWARF_ALL_KNOWN_DW_VIRTUALITY 4056#undef DWARF_ONE_KNOWN_DW_VIRTUALITY 4057 }; 4058 4059 if (likely (code < sizeof (known) / sizeof (known[0]))) 4060 return known[code]; 4061 4062 return NULL; 4063} 4064 4065 4066static const char * 4067dwarf_identifier_case_string (unsigned int code) 4068{ 4069 static const char *const known[] = 4070 { 4071#define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME, 4072 DWARF_ALL_KNOWN_DW_ID 4073#undef DWARF_ONE_KNOWN_DW_ID 4074 }; 4075 4076 if (likely (code < sizeof (known) / sizeof (known[0]))) 4077 return known[code]; 4078 4079 return NULL; 4080} 4081 4082 4083static const char * 4084dwarf_calling_convention_string (unsigned int code) 4085{ 4086 static const char *const known[] = 4087 { 4088#define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME, 4089 DWARF_ALL_KNOWN_DW_CC 4090#undef DWARF_ONE_KNOWN_DW_CC 4091 }; 4092 4093 if (likely (code < sizeof (known) / sizeof (known[0]))) 4094 return known[code]; 4095 4096 return NULL; 4097} 4098 4099 4100static const char * 4101dwarf_ordering_string (unsigned int code) 4102{ 4103 static const char *const known[] = 4104 { 4105#define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME, 4106 DWARF_ALL_KNOWN_DW_ORD 4107#undef DWARF_ONE_KNOWN_DW_ORD 4108 }; 4109 4110 if (likely (code < sizeof (known) / sizeof (known[0]))) 4111 return known[code]; 4112 4113 return NULL; 4114} 4115 4116 4117static const char * 4118dwarf_discr_list_string (unsigned int code) 4119{ 4120 static const char *const known[] = 4121 { 4122#define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME, 4123 DWARF_ALL_KNOWN_DW_DSC 4124#undef DWARF_ONE_KNOWN_DW_DSC 4125 }; 4126 4127 if (likely (code < sizeof (known) / sizeof (known[0]))) 4128 return known[code]; 4129 4130 return NULL; 4131} 4132 4133 4134static const char * 4135dwarf_locexpr_opcode_string (unsigned int code) 4136{ 4137 static const char *const known[] = 4138 { 4139 /* Normally we can't afford building huge table of 64K entries, 4140 most of them zero, just because there are a couple defined 4141 values at the far end. In case of opcodes, it's OK. */ 4142#define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME, 4143 DWARF_ALL_KNOWN_DW_OP 4144#undef DWARF_ONE_KNOWN_DW_OP 4145 }; 4146 4147 if (likely (code < sizeof (known) / sizeof (known[0]))) 4148 return known[code]; 4149 4150 return NULL; 4151} 4152 4153 4154static const char * 4155dwarf_unit_string (unsigned int type) 4156{ 4157 switch (type) 4158 { 4159#define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME; 4160 DWARF_ALL_KNOWN_DW_UT 4161#undef DWARF_ONE_KNOWN_DW_UT 4162 default: 4163 return NULL; 4164 } 4165} 4166 4167 4168static const char * 4169dwarf_range_list_encoding_string (unsigned int kind) 4170{ 4171 switch (kind) 4172 { 4173#define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME; 4174 DWARF_ALL_KNOWN_DW_RLE 4175#undef DWARF_ONE_KNOWN_DW_RLE 4176 default: 4177 return NULL; 4178 } 4179} 4180 4181 4182static const char * 4183dwarf_loc_list_encoding_string (unsigned int kind) 4184{ 4185 switch (kind) 4186 { 4187#define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME; 4188 DWARF_ALL_KNOWN_DW_LLE 4189#undef DWARF_ONE_KNOWN_DW_LLE 4190 /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */ 4191 case DW_LLE_GNU_view_pair: return "GNU_view_pair"; 4192 default: 4193 return NULL; 4194 } 4195} 4196 4197 4198static const char * 4199dwarf_line_content_description_string (unsigned int kind) 4200{ 4201 switch (kind) 4202 { 4203#define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME; 4204 DWARF_ALL_KNOWN_DW_LNCT 4205#undef DWARF_ONE_KNOWN_DW_LNCT 4206 default: 4207 return NULL; 4208 } 4209} 4210 4211 4212/* Used by all dwarf_foo_name functions. */ 4213static const char * 4214string_or_unknown (const char *known, unsigned int code, 4215 unsigned int lo_user, unsigned int hi_user, 4216 bool print_unknown_num) 4217{ 4218 static char unknown_buf[20]; 4219 4220 if (likely (known != NULL)) 4221 return known; 4222 4223 if (lo_user != 0 && code >= lo_user && code <= hi_user) 4224 { 4225 snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x", 4226 code - lo_user); 4227 return unknown_buf; 4228 } 4229 4230 if (print_unknown_num) 4231 { 4232 snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code); 4233 return unknown_buf; 4234 } 4235 4236 return "???"; 4237} 4238 4239 4240static const char * 4241dwarf_tag_name (unsigned int tag) 4242{ 4243 const char *ret = dwarf_tag_string (tag); 4244 return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true); 4245} 4246 4247static const char * 4248dwarf_attr_name (unsigned int attr) 4249{ 4250 const char *ret = dwarf_attr_string (attr); 4251 return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true); 4252} 4253 4254 4255static const char * 4256dwarf_form_name (unsigned int form) 4257{ 4258 const char *ret = dwarf_form_string (form); 4259 return string_or_unknown (ret, form, 0, 0, true); 4260} 4261 4262 4263static const char * 4264dwarf_lang_name (unsigned int lang) 4265{ 4266 const char *ret = dwarf_lang_string (lang); 4267 return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false); 4268} 4269 4270 4271static const char * 4272dwarf_inline_name (unsigned int code) 4273{ 4274 const char *ret = dwarf_inline_string (code); 4275 return string_or_unknown (ret, code, 0, 0, false); 4276} 4277 4278 4279static const char * 4280dwarf_encoding_name (unsigned int code) 4281{ 4282 const char *ret = dwarf_encoding_string (code); 4283 return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false); 4284} 4285 4286 4287static const char * 4288dwarf_access_name (unsigned int code) 4289{ 4290 const char *ret = dwarf_access_string (code); 4291 return string_or_unknown (ret, code, 0, 0, false); 4292} 4293 4294 4295static const char * 4296dwarf_defaulted_name (unsigned int code) 4297{ 4298 const char *ret = dwarf_defaulted_string (code); 4299 return string_or_unknown (ret, code, 0, 0, false); 4300} 4301 4302 4303static const char * 4304dwarf_visibility_name (unsigned int code) 4305{ 4306 const char *ret = dwarf_visibility_string (code); 4307 return string_or_unknown (ret, code, 0, 0, false); 4308} 4309 4310 4311static const char * 4312dwarf_virtuality_name (unsigned int code) 4313{ 4314 const char *ret = dwarf_virtuality_string (code); 4315 return string_or_unknown (ret, code, 0, 0, false); 4316} 4317 4318 4319static const char * 4320dwarf_identifier_case_name (unsigned int code) 4321{ 4322 const char *ret = dwarf_identifier_case_string (code); 4323 return string_or_unknown (ret, code, 0, 0, false); 4324} 4325 4326 4327static const char * 4328dwarf_calling_convention_name (unsigned int code) 4329{ 4330 const char *ret = dwarf_calling_convention_string (code); 4331 return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false); 4332} 4333 4334 4335static const char * 4336dwarf_ordering_name (unsigned int code) 4337{ 4338 const char *ret = dwarf_ordering_string (code); 4339 return string_or_unknown (ret, code, 0, 0, false); 4340} 4341 4342 4343static const char * 4344dwarf_discr_list_name (unsigned int code) 4345{ 4346 const char *ret = dwarf_discr_list_string (code); 4347 return string_or_unknown (ret, code, 0, 0, false); 4348} 4349 4350 4351static const char * 4352dwarf_unit_name (unsigned int type) 4353{ 4354 const char *ret = dwarf_unit_string (type); 4355 return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true); 4356} 4357 4358 4359static const char * 4360dwarf_range_list_encoding_name (unsigned int kind) 4361{ 4362 const char *ret = dwarf_range_list_encoding_string (kind); 4363 return string_or_unknown (ret, kind, 0, 0, false); 4364} 4365 4366 4367static const char * 4368dwarf_loc_list_encoding_name (unsigned int kind) 4369{ 4370 const char *ret = dwarf_loc_list_encoding_string (kind); 4371 return string_or_unknown (ret, kind, 0, 0, false); 4372} 4373 4374 4375static const char * 4376dwarf_line_content_description_name (unsigned int kind) 4377{ 4378 const char *ret = dwarf_line_content_description_string (kind); 4379 return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user, 4380 false); 4381} 4382 4383 4384static void 4385print_block (size_t n, const void *block) 4386{ 4387 if (n == 0) 4388 puts (_("empty block")); 4389 else 4390 { 4391 printf (_("%zu byte block:"), n); 4392 const unsigned char *data = block; 4393 do 4394 printf (" %02x", *data++); 4395 while (--n > 0); 4396 putchar ('\n'); 4397 } 4398} 4399 4400static void 4401print_bytes (size_t n, const unsigned char *bytes) 4402{ 4403 while (n-- > 0) 4404 { 4405 printf ("%02x", *bytes++); 4406 if (n > 0) 4407 printf (" "); 4408 } 4409} 4410 4411static int 4412get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr) 4413{ 4414 if (cu == NULL) 4415 return -1; 4416 4417 Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr]; 4418 if (debug_addr == NULL) 4419 return -1; 4420 4421 Dwarf_Off base = __libdw_cu_addr_base (cu); 4422 Dwarf_Word off = idx * cu->address_size; 4423 if (base > debug_addr->d_size 4424 || off > debug_addr->d_size - base 4425 || cu->address_size > debug_addr->d_size - base - off) 4426 return -1; 4427 4428 const unsigned char *addrp = debug_addr->d_buf + base + off; 4429 if (cu->address_size == 4) 4430 *addr = read_4ubyte_unaligned (cu->dbg, addrp); 4431 else 4432 *addr = read_8ubyte_unaligned (cu->dbg, addrp); 4433 4434 return 0; 4435} 4436 4437static void 4438print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest, 4439 unsigned int vers, unsigned int addrsize, unsigned int offset_size, 4440 struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data) 4441{ 4442 const unsigned int ref_size = vers < 3 ? addrsize : offset_size; 4443 4444 if (len == 0) 4445 { 4446 printf ("%*s(empty)\n", indent, ""); 4447 return; 4448 } 4449 4450#define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid 4451#define CONSUME(n) NEED (n); else len -= (n) 4452 4453 Dwarf_Word offset = 0; 4454 while (len-- > 0) 4455 { 4456 uint_fast8_t op = *data++; 4457 4458 const char *op_name = dwarf_locexpr_opcode_string (op); 4459 if (unlikely (op_name == NULL)) 4460 { 4461 static char buf[20]; 4462 if (op >= DW_OP_lo_user) 4463 snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user); 4464 else 4465 snprintf (buf, sizeof buf, "??? (%#x)", op); 4466 op_name = buf; 4467 } 4468 4469 switch (op) 4470 { 4471 case DW_OP_addr:; 4472 /* Address operand. */ 4473 Dwarf_Word addr; 4474 NEED (addrsize); 4475 if (addrsize == 4) 4476 addr = read_4ubyte_unaligned (dbg, data); 4477 else if (addrsize == 8) 4478 addr = read_8ubyte_unaligned (dbg, data); 4479 else 4480 goto invalid; 4481 data += addrsize; 4482 CONSUME (addrsize); 4483 4484 printf ("%*s[%2" PRIuMAX "] %s ", 4485 indent, "", (uintmax_t) offset, op_name); 4486 print_dwarf_addr (dwflmod, 0, addr, addr); 4487 printf ("\n"); 4488 4489 offset += 1 + addrsize; 4490 break; 4491 4492 case DW_OP_call_ref: 4493 case DW_OP_GNU_variable_value: 4494 /* Offset operand. */ 4495 if (ref_size != 4 && ref_size != 8) 4496 goto invalid; /* Cannot be used in CFA. */ 4497 NEED (ref_size); 4498 if (ref_size == 4) 4499 addr = read_4ubyte_unaligned (dbg, data); 4500 else 4501 addr = read_8ubyte_unaligned (dbg, data); 4502 data += ref_size; 4503 CONSUME (ref_size); 4504 /* addr is a DIE offset, so format it as one. */ 4505 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n", 4506 indent, "", (uintmax_t) offset, 4507 op_name, (uintmax_t) addr); 4508 offset += 1 + ref_size; 4509 break; 4510 4511 case DW_OP_deref_size: 4512 case DW_OP_xderef_size: 4513 case DW_OP_pick: 4514 case DW_OP_const1u: 4515 // XXX value might be modified by relocation 4516 NEED (1); 4517 printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n", 4518 indent, "", (uintmax_t) offset, 4519 op_name, *((uint8_t *) data)); 4520 ++data; 4521 --len; 4522 offset += 2; 4523 break; 4524 4525 case DW_OP_const2u: 4526 NEED (2); 4527 // XXX value might be modified by relocation 4528 printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n", 4529 indent, "", (uintmax_t) offset, 4530 op_name, read_2ubyte_unaligned (dbg, data)); 4531 CONSUME (2); 4532 data += 2; 4533 offset += 3; 4534 break; 4535 4536 case DW_OP_const4u: 4537 NEED (4); 4538 // XXX value might be modified by relocation 4539 printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n", 4540 indent, "", (uintmax_t) offset, 4541 op_name, read_4ubyte_unaligned (dbg, data)); 4542 CONSUME (4); 4543 data += 4; 4544 offset += 5; 4545 break; 4546 4547 case DW_OP_const8u: 4548 NEED (8); 4549 // XXX value might be modified by relocation 4550 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n", 4551 indent, "", (uintmax_t) offset, 4552 op_name, (uint64_t) read_8ubyte_unaligned (dbg, data)); 4553 CONSUME (8); 4554 data += 8; 4555 offset += 9; 4556 break; 4557 4558 case DW_OP_const1s: 4559 NEED (1); 4560 // XXX value might be modified by relocation 4561 printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n", 4562 indent, "", (uintmax_t) offset, 4563 op_name, *((int8_t *) data)); 4564 ++data; 4565 --len; 4566 offset += 2; 4567 break; 4568 4569 case DW_OP_const2s: 4570 NEED (2); 4571 // XXX value might be modified by relocation 4572 printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n", 4573 indent, "", (uintmax_t) offset, 4574 op_name, read_2sbyte_unaligned (dbg, data)); 4575 CONSUME (2); 4576 data += 2; 4577 offset += 3; 4578 break; 4579 4580 case DW_OP_const4s: 4581 NEED (4); 4582 // XXX value might be modified by relocation 4583 printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n", 4584 indent, "", (uintmax_t) offset, 4585 op_name, read_4sbyte_unaligned (dbg, data)); 4586 CONSUME (4); 4587 data += 4; 4588 offset += 5; 4589 break; 4590 4591 case DW_OP_const8s: 4592 NEED (8); 4593 // XXX value might be modified by relocation 4594 printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n", 4595 indent, "", (uintmax_t) offset, 4596 op_name, read_8sbyte_unaligned (dbg, data)); 4597 CONSUME (8); 4598 data += 8; 4599 offset += 9; 4600 break; 4601 4602 case DW_OP_piece: 4603 case DW_OP_regx: 4604 case DW_OP_plus_uconst: 4605 case DW_OP_constu:; 4606 const unsigned char *start = data; 4607 uint64_t uleb; 4608 NEED (1); 4609 get_uleb128 (uleb, data, data + len); 4610 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n", 4611 indent, "", (uintmax_t) offset, op_name, uleb); 4612 CONSUME (data - start); 4613 offset += 1 + (data - start); 4614 break; 4615 4616 case DW_OP_addrx: 4617 case DW_OP_GNU_addr_index: 4618 case DW_OP_constx: 4619 case DW_OP_GNU_const_index:; 4620 start = data; 4621 NEED (1); 4622 get_uleb128 (uleb, data, data + len); 4623 printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ", 4624 indent, "", (uintmax_t) offset, op_name, uleb); 4625 CONSUME (data - start); 4626 offset += 1 + (data - start); 4627 if (get_indexed_addr (cu, uleb, &addr) != 0) 4628 printf ("???\n"); 4629 else 4630 { 4631 print_dwarf_addr (dwflmod, 0, addr, addr); 4632 printf ("\n"); 4633 } 4634 break; 4635 4636 case DW_OP_bit_piece: 4637 start = data; 4638 uint64_t uleb2; 4639 NEED (1); 4640 get_uleb128 (uleb, data, data + len); 4641 NEED (1); 4642 get_uleb128 (uleb2, data, data + len); 4643 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n", 4644 indent, "", (uintmax_t) offset, op_name, uleb, uleb2); 4645 CONSUME (data - start); 4646 offset += 1 + (data - start); 4647 break; 4648 4649 case DW_OP_fbreg: 4650 case DW_OP_breg0 ... DW_OP_breg31: 4651 case DW_OP_consts: 4652 start = data; 4653 int64_t sleb; 4654 NEED (1); 4655 get_sleb128 (sleb, data, data + len); 4656 printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n", 4657 indent, "", (uintmax_t) offset, op_name, sleb); 4658 CONSUME (data - start); 4659 offset += 1 + (data - start); 4660 break; 4661 4662 case DW_OP_bregx: 4663 start = data; 4664 NEED (1); 4665 get_uleb128 (uleb, data, data + len); 4666 NEED (1); 4667 get_sleb128 (sleb, data, data + len); 4668 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n", 4669 indent, "", (uintmax_t) offset, op_name, uleb, sleb); 4670 CONSUME (data - start); 4671 offset += 1 + (data - start); 4672 break; 4673 4674 case DW_OP_call2: 4675 NEED (2); 4676 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n", 4677 indent, "", (uintmax_t) offset, op_name, 4678 read_2ubyte_unaligned (dbg, data)); 4679 CONSUME (2); 4680 data += 2; 4681 offset += 3; 4682 break; 4683 4684 case DW_OP_call4: 4685 NEED (4); 4686 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n", 4687 indent, "", (uintmax_t) offset, op_name, 4688 read_4ubyte_unaligned (dbg, data)); 4689 CONSUME (4); 4690 data += 4; 4691 offset += 5; 4692 break; 4693 4694 case DW_OP_skip: 4695 case DW_OP_bra: 4696 NEED (2); 4697 printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n", 4698 indent, "", (uintmax_t) offset, op_name, 4699 (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3)); 4700 CONSUME (2); 4701 data += 2; 4702 offset += 3; 4703 break; 4704 4705 case DW_OP_implicit_value: 4706 start = data; 4707 NEED (1); 4708 get_uleb128 (uleb, data, data + len); 4709 printf ("%*s[%2" PRIuMAX "] %s: ", 4710 indent, "", (uintmax_t) offset, op_name); 4711 NEED (uleb); 4712 print_block (uleb, data); 4713 data += uleb; 4714 CONSUME (data - start); 4715 offset += 1 + (data - start); 4716 break; 4717 4718 case DW_OP_implicit_pointer: 4719 case DW_OP_GNU_implicit_pointer: 4720 /* DIE offset operand. */ 4721 start = data; 4722 NEED (ref_size); 4723 if (ref_size != 4 && ref_size != 8) 4724 goto invalid; /* Cannot be used in CFA. */ 4725 if (ref_size == 4) 4726 addr = read_4ubyte_unaligned (dbg, data); 4727 else 4728 addr = read_8ubyte_unaligned (dbg, data); 4729 data += ref_size; 4730 /* Byte offset operand. */ 4731 NEED (1); 4732 get_sleb128 (sleb, data, data + len); 4733 4734 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n", 4735 indent, "", (intmax_t) offset, 4736 op_name, (uintmax_t) addr, sleb); 4737 CONSUME (data - start); 4738 offset += 1 + (data - start); 4739 break; 4740 4741 case DW_OP_entry_value: 4742 case DW_OP_GNU_entry_value: 4743 /* Size plus expression block. */ 4744 start = data; 4745 NEED (1); 4746 get_uleb128 (uleb, data, data + len); 4747 printf ("%*s[%2" PRIuMAX "] %s:\n", 4748 indent, "", (uintmax_t) offset, op_name); 4749 NEED (uleb); 4750 print_ops (dwflmod, dbg, indent + 5, indent + 5, vers, 4751 addrsize, offset_size, cu, uleb, data); 4752 data += uleb; 4753 CONSUME (data - start); 4754 offset += 1 + (data - start); 4755 break; 4756 4757 case DW_OP_const_type: 4758 case DW_OP_GNU_const_type: 4759 /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte 4760 unsigned size plus block. */ 4761 start = data; 4762 NEED (1); 4763 get_uleb128 (uleb, data, data + len); 4764 if (! print_unresolved_addresses && cu != NULL) 4765 uleb += cu->start; 4766 NEED (1); 4767 uint8_t usize = *(uint8_t *) data++; 4768 NEED (usize); 4769 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ", 4770 indent, "", (uintmax_t) offset, op_name, uleb); 4771 print_block (usize, data); 4772 data += usize; 4773 CONSUME (data - start); 4774 offset += 1 + (data - start); 4775 break; 4776 4777 case DW_OP_regval_type: 4778 case DW_OP_GNU_regval_type: 4779 /* uleb128 register number, uleb128 CU relative 4780 DW_TAG_base_type DIE offset. */ 4781 start = data; 4782 NEED (1); 4783 get_uleb128 (uleb, data, data + len); 4784 NEED (1); 4785 get_uleb128 (uleb2, data, data + len); 4786 if (! print_unresolved_addresses && cu != NULL) 4787 uleb2 += cu->start; 4788 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n", 4789 indent, "", (uintmax_t) offset, op_name, uleb, uleb2); 4790 CONSUME (data - start); 4791 offset += 1 + (data - start); 4792 break; 4793 4794 case DW_OP_deref_type: 4795 case DW_OP_GNU_deref_type: 4796 /* 1-byte unsigned size of value, uleb128 CU relative 4797 DW_TAG_base_type DIE offset. */ 4798 start = data; 4799 NEED (1); 4800 usize = *(uint8_t *) data++; 4801 NEED (1); 4802 get_uleb128 (uleb, data, data + len); 4803 if (! print_unresolved_addresses && cu != NULL) 4804 uleb += cu->start; 4805 printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n", 4806 indent, "", (uintmax_t) offset, 4807 op_name, usize, uleb); 4808 CONSUME (data - start); 4809 offset += 1 + (data - start); 4810 break; 4811 4812 case DW_OP_xderef_type: 4813 /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */ 4814 start = data; 4815 NEED (1); 4816 usize = *(uint8_t *) data++; 4817 NEED (1); 4818 get_uleb128 (uleb, data, data + len); 4819 printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n", 4820 indent, "", (uintmax_t) offset, 4821 op_name, usize, uleb); 4822 CONSUME (data - start); 4823 offset += 1 + (data - start); 4824 break; 4825 4826 case DW_OP_convert: 4827 case DW_OP_GNU_convert: 4828 case DW_OP_reinterpret: 4829 case DW_OP_GNU_reinterpret: 4830 /* uleb128 CU relative offset to DW_TAG_base_type, or zero 4831 for conversion to untyped. */ 4832 start = data; 4833 NEED (1); 4834 get_uleb128 (uleb, data, data + len); 4835 if (uleb != 0 && ! print_unresolved_addresses && cu != NULL) 4836 uleb += cu->start; 4837 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n", 4838 indent, "", (uintmax_t) offset, op_name, uleb); 4839 CONSUME (data - start); 4840 offset += 1 + (data - start); 4841 break; 4842 4843 case DW_OP_GNU_parameter_ref: 4844 /* 4 byte CU relative reference to the abstract optimized away 4845 DW_TAG_formal_parameter. */ 4846 NEED (4); 4847 uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data); 4848 if (! print_unresolved_addresses && cu != NULL) 4849 param_off += cu->start; 4850 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n", 4851 indent, "", (uintmax_t) offset, op_name, param_off); 4852 CONSUME (4); 4853 data += 4; 4854 offset += 5; 4855 break; 4856 4857 default: 4858 /* No Operand. */ 4859 printf ("%*s[%2" PRIuMAX "] %s\n", 4860 indent, "", (uintmax_t) offset, op_name); 4861 ++offset; 4862 break; 4863 } 4864 4865 indent = indentrest; 4866 continue; 4867 4868 invalid: 4869 printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"), 4870 indent, "", (uintmax_t) offset, op_name); 4871 break; 4872 } 4873} 4874 4875 4876/* Turn the addresses into file offsets by using the phdrs. */ 4877static void 4878find_offsets(Elf *elf, GElf_Addr main_bias, size_t n, 4879 GElf_Addr addrs[n], GElf_Off offs[n]) 4880{ 4881 size_t unsolved = n; 4882 for (size_t i = 0; i < phnum; ++i) { 4883 GElf_Phdr phdr_mem; 4884 GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem); 4885 if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0) 4886 for (size_t j = 0; j < n; ++j) 4887 if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias && 4888 addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) { 4889 offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset; 4890 if (--unsolved == 0) 4891 break; 4892 } 4893 } 4894} 4895 4896/* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section. 4897 And .dynamic section contains an array of the dynamic structures. 4898 We use the array to get: 4899 DT_STRTAB: the address of the string table 4900 DT_SYMTAB: the address of the symbol table 4901 DT_STRSZ: the size, in bytes, of the string table 4902 ... */ 4903static void 4904get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]) 4905{ 4906 Elf_Data *data = elf_getdata_rawchunk( 4907 elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN); 4908 4909 int dyn_idx = 0; 4910 for (;; ++dyn_idx) { 4911 GElf_Dyn dyn_mem; 4912 GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem); 4913 /* DT_NULL Marks end of dynamic section. */ 4914 if (dyn->d_tag == DT_NULL) 4915 break; 4916 4917 switch (dyn->d_tag) { 4918 case DT_SYMTAB: 4919 addrs[i_symtab] = dyn->d_un.d_ptr; 4920 break; 4921 4922 case DT_HASH: 4923 addrs[i_hash] = dyn->d_un.d_ptr; 4924 break; 4925 4926 case DT_GNU_HASH: 4927 addrs[i_gnu_hash] = dyn->d_un.d_ptr; 4928 break; 4929 4930 case DT_STRTAB: 4931 addrs[i_strtab] = dyn->d_un.d_ptr; 4932 break; 4933 4934 case DT_VERSYM: 4935 addrs[i_versym] = dyn->d_un.d_ptr; 4936 break; 4937 4938 case DT_VERDEF: 4939 addrs[i_verdef] = dyn->d_un.d_ptr; 4940 break; 4941 4942 case DT_VERNEED: 4943 addrs[i_verneed] = dyn->d_un.d_ptr; 4944 break; 4945 4946 case DT_STRSZ: 4947 addrs[i_strsz] = dyn->d_un.d_val; 4948 break; 4949 } 4950 } 4951} 4952 4953 4954/* Use dynamic segment to get data for the string table section. */ 4955static Elf_Data * 4956get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr) 4957{ 4958 Elf_Data *strtab_data; 4959 GElf_Addr addrs[i_max] = {0,}; 4960 GElf_Off offs[i_max] = {0,}; 4961 get_dynscn_addrs(elf, phdr, addrs); 4962 find_offsets(elf, 0, i_max, addrs, offs); 4963 strtab_data = elf_getdata_rawchunk( 4964 elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE); 4965 return strtab_data; 4966} 4967 4968 4969struct listptr 4970{ 4971 Dwarf_Off offset:(64 - 3); 4972 bool addr64:1; 4973 bool dwarf64:1; 4974 bool warned:1; 4975 struct Dwarf_CU *cu; 4976 unsigned int attr; 4977}; 4978 4979#define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4) 4980#define listptr_address_size(p) ((p)->addr64 ? 8 : 4) 4981 4982static Dwarf_Addr 4983cudie_base (Dwarf_Die *cudie) 4984{ 4985 Dwarf_Addr base; 4986 /* Find the base address of the compilation unit. It will normally 4987 be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base 4988 address could be overridden by DW_AT_entry_pc. It's been 4989 removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for 4990 compilation units with discontinuous ranges. */ 4991 if (unlikely (dwarf_lowpc (cudie, &base) != 0)) 4992 { 4993 Dwarf_Attribute attr_mem; 4994 if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem), 4995 &base) != 0) 4996 base = 0; 4997 } 4998 return base; 4999} 5000 5001static Dwarf_Addr 5002listptr_base (struct listptr *p) 5003{ 5004 Dwarf_Die cu = CUDIE (p->cu); 5005 return cudie_base (&cu); 5006} 5007 5008/* To store the name used in compare_listptr */ 5009static const char *sort_listptr_name; 5010 5011static int 5012compare_listptr (const void *a, const void *b) 5013{ 5014 const char *name = sort_listptr_name; 5015 struct listptr *p1 = (void *) a; 5016 struct listptr *p2 = (void *) b; 5017 5018 if (p1->offset < p2->offset) 5019 return -1; 5020 if (p1->offset > p2->offset) 5021 return 1; 5022 5023 if (!p1->warned && !p2->warned) 5024 { 5025 if (p1->addr64 != p2->addr64) 5026 { 5027 p1->warned = p2->warned = true; 5028 error (0, 0, 5029 _("%s %#" PRIx64 " used with different address sizes"), 5030 name, (uint64_t) p1->offset); 5031 } 5032 if (p1->dwarf64 != p2->dwarf64) 5033 { 5034 p1->warned = p2->warned = true; 5035 error (0, 0, 5036 _("%s %#" PRIx64 " used with different offset sizes"), 5037 name, (uint64_t) p1->offset); 5038 } 5039 if (listptr_base (p1) != listptr_base (p2)) 5040 { 5041 p1->warned = p2->warned = true; 5042 error (0, 0, 5043 _("%s %#" PRIx64 " used with different base addresses"), 5044 name, (uint64_t) p1->offset); 5045 } 5046 if (p1->attr != p2 ->attr) 5047 { 5048 p1->warned = p2->warned = true; 5049 error (0, 0, 5050 _("%s %#" PRIx64 5051 " used with different attribute %s and %s"), 5052 name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr), 5053 dwarf_attr_name (p2->attr)); 5054 } 5055 } 5056 5057 return 0; 5058} 5059 5060struct listptr_table 5061{ 5062 size_t n; 5063 size_t alloc; 5064 struct listptr *table; 5065}; 5066 5067static struct listptr_table known_locsptr; 5068static struct listptr_table known_loclistsptr; 5069static struct listptr_table known_rangelistptr; 5070static struct listptr_table known_rnglistptr; 5071static struct listptr_table known_addrbases; 5072static struct listptr_table known_stroffbases; 5073 5074static void 5075reset_listptr (struct listptr_table *table) 5076{ 5077 free (table->table); 5078 table->table = NULL; 5079 table->n = table->alloc = 0; 5080} 5081 5082/* Returns false if offset doesn't fit. See struct listptr. */ 5083static bool 5084notice_listptr (enum section_e section, struct listptr_table *table, 5085 uint_fast8_t address_size, uint_fast8_t offset_size, 5086 struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr) 5087{ 5088 if (print_debug_sections & section) 5089 { 5090 if (table->n == table->alloc) 5091 { 5092 if (table->alloc == 0) 5093 table->alloc = 128; 5094 else 5095 table->alloc *= 2; 5096 table->table = xrealloc (table->table, 5097 table->alloc * sizeof table->table[0]); 5098 } 5099 5100 struct listptr *p = &table->table[table->n++]; 5101 5102 *p = (struct listptr) 5103 { 5104 .addr64 = address_size == 8, 5105 .dwarf64 = offset_size == 8, 5106 .offset = offset, 5107 .cu = cu, 5108 .attr = attr 5109 }; 5110 5111 if (p->offset != offset) 5112 { 5113 table->n--; 5114 return false; 5115 } 5116 } 5117 return true; 5118} 5119 5120static void 5121sort_listptr (struct listptr_table *table, const char *name) 5122{ 5123 if (table->n > 0) 5124 { 5125 sort_listptr_name = name; 5126 qsort (table->table, table->n, sizeof table->table[0], 5127 &compare_listptr); 5128 } 5129} 5130 5131static bool 5132skip_listptr_hole (struct listptr_table *table, size_t *idxp, 5133 uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep, 5134 Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset, 5135 unsigned char **readp, unsigned char *endp, 5136 unsigned int *attr) 5137{ 5138 if (table->n == 0) 5139 return false; 5140 5141 while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset) 5142 ++*idxp; 5143 5144 struct listptr *p = &table->table[*idxp]; 5145 5146 if (*idxp == table->n 5147 || p->offset >= (Dwarf_Off) (endp - *readp + offset)) 5148 { 5149 *readp = endp; 5150 printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"), 5151 offset); 5152 return true; 5153 } 5154 5155 if (p->offset != (Dwarf_Off) offset) 5156 { 5157 *readp += p->offset - offset; 5158 printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"), 5159 offset, (Dwarf_Off) p->offset - offset); 5160 return true; 5161 } 5162 5163 if (address_sizep != NULL) 5164 *address_sizep = listptr_address_size (p); 5165 if (offset_sizep != NULL) 5166 *offset_sizep = listptr_offset_size (p); 5167 if (base != NULL) 5168 *base = listptr_base (p); 5169 if (cu != NULL) 5170 *cu = p->cu; 5171 if (attr != NULL) 5172 *attr = p->attr; 5173 5174 return false; 5175} 5176 5177static Dwarf_Off 5178next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off) 5179{ 5180 /* Note that multiple attributes could in theory point to the same loclist 5181 offset, so make sure we pick one that is bigger than the current one. 5182 The table is sorted on offset. */ 5183 if (*idxp < table->n) 5184 { 5185 while (++*idxp < table->n) 5186 { 5187 Dwarf_Off next = table->table[*idxp].offset; 5188 if (next > off) 5189 return next; 5190 } 5191 } 5192 return 0; 5193} 5194 5195/* Returns the listptr associated with the given index, or NULL. */ 5196static struct listptr * 5197get_listptr (struct listptr_table *table, size_t idx) 5198{ 5199 if (idx >= table->n) 5200 return NULL; 5201 return &table->table[idx]; 5202} 5203 5204/* Returns the next index, base address and CU associated with the 5205 list unit offsets. If there is none false is returned, otherwise 5206 true. Assumes the table has been sorted. */ 5207static bool 5208listptr_cu (struct listptr_table *table, size_t *idxp, 5209 Dwarf_Off start, Dwarf_Off end, 5210 Dwarf_Addr *base, struct Dwarf_CU **cu) 5211{ 5212 while (*idxp < table->n 5213 && table->table[*idxp].offset < start) 5214 ++*idxp; 5215 5216 if (*idxp < table->n 5217 && table->table[*idxp].offset >= start 5218 && table->table[*idxp].offset < end) 5219 { 5220 struct listptr *p = &table->table[*idxp]; 5221 *base = listptr_base (p); 5222 *cu = p->cu; 5223 return true; 5224 } 5225 5226 return false; 5227} 5228 5229/* Returns the next index with the current CU for the given attribute. 5230 If there is none false is returned, otherwise true. Assumes the 5231 table has been sorted. */ 5232static bool 5233listptr_attr (struct listptr_table *table, size_t idxp, 5234 Dwarf_Off offset, unsigned int attr) 5235{ 5236 struct listptr *listptr; 5237 do 5238 { 5239 listptr = get_listptr (table, idxp); 5240 if (listptr == NULL) 5241 return false; 5242 5243 if (listptr->offset == offset && listptr->attr == attr) 5244 return true; 5245 5246 idxp++; 5247 } 5248 while (listptr->offset <= offset); 5249 5250 return false; 5251} 5252 5253static void 5254print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 5255 Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)), 5256 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 5257{ 5258 const size_t sh_size = (dbg->sectiondata[IDX_debug_abbrev] ? 5259 dbg->sectiondata[IDX_debug_abbrev]->d_size : 0); 5260 5261 printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n" 5262 " [ Code]\n"), 5263 elf_ndxscn (scn), section_name (ebl, shdr), 5264 (uint64_t) shdr->sh_offset); 5265 5266 Dwarf_Off offset = 0; 5267 while (offset < sh_size) 5268 { 5269 printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"), 5270 offset); 5271 5272 while (1) 5273 { 5274 size_t length; 5275 Dwarf_Abbrev abbrev; 5276 5277 int res = dwarf_offabbrev (dbg, offset, &length, &abbrev); 5278 if (res != 0) 5279 { 5280 if (unlikely (res < 0)) 5281 { 5282 printf (_("\ 5283 *** error while reading abbreviation: %s\n"), 5284 dwarf_errmsg (-1)); 5285 return; 5286 } 5287 5288 /* This is the NUL byte at the end of the section. */ 5289 ++offset; 5290 break; 5291 } 5292 5293 /* We know these calls can never fail. */ 5294 unsigned int code = dwarf_getabbrevcode (&abbrev); 5295 unsigned int tag = dwarf_getabbrevtag (&abbrev); 5296 int has_children = dwarf_abbrevhaschildren (&abbrev); 5297 5298 printf (_(" [%5u] offset: %" PRId64 5299 ", children: %s, tag: %s\n"), 5300 code, (int64_t) offset, 5301 has_children ? yes_str : no_str, 5302 dwarf_tag_name (tag)); 5303 5304 size_t cnt = 0; 5305 unsigned int name; 5306 unsigned int form; 5307 Dwarf_Sword data; 5308 Dwarf_Off enoffset; 5309 while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form, 5310 &data, &enoffset) == 0) 5311 { 5312 printf (" attr: %s, form: %s", 5313 dwarf_attr_name (name), dwarf_form_name (form)); 5314 if (form == DW_FORM_implicit_const) 5315 printf (" (%" PRId64 ")", data); 5316 printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset); 5317 ++cnt; 5318 } 5319 5320 offset += length; 5321 } 5322 } 5323} 5324 5325 5326static void 5327print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 5328 Ebl *ebl, GElf_Ehdr *ehdr, 5329 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 5330{ 5331 printf (_("\ 5332\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 5333 elf_ndxscn (scn), section_name (ebl, shdr), 5334 (uint64_t) shdr->sh_offset); 5335 5336 if (shdr->sh_size == 0) 5337 return; 5338 5339 /* We like to get the section from libdw to make sure they are relocated. */ 5340 Elf_Data *data = (dbg->sectiondata[IDX_debug_addr] 5341 ?: elf_rawdata (scn, NULL)); 5342 if (unlikely (data == NULL)) 5343 { 5344 error (0, 0, _("cannot get .debug_addr section data: %s"), 5345 elf_errmsg (-1)); 5346 return; 5347 } 5348 5349 size_t idx = 0; 5350 sort_listptr (&known_addrbases, "addr_base"); 5351 5352 const unsigned char *start = (const unsigned char *) data->d_buf; 5353 const unsigned char *readp = start; 5354 const unsigned char *readendp = ((const unsigned char *) data->d_buf 5355 + data->d_size); 5356 5357 while (readp < readendp) 5358 { 5359 /* We cannot really know whether or not there is an header. The 5360 DebugFission extension to DWARF4 doesn't add one. The DWARF5 5361 .debug_addr variant does. Whether or not we have an header, 5362 DW_AT_[GNU_]addr_base points at "index 0". So if the current 5363 offset equals the CU addr_base then we can just start 5364 printing addresses. If there is no CU with an exact match 5365 then we'll try to parse the header first. */ 5366 Dwarf_Off off = (Dwarf_Off) (readp 5367 - (const unsigned char *) data->d_buf); 5368 5369 printf ("Table at offset %" PRIx64 " ", off); 5370 5371 struct listptr *listptr = get_listptr (&known_addrbases, idx++); 5372 const unsigned char *next_unitp; 5373 5374 uint64_t unit_length; 5375 uint16_t version; 5376 uint8_t address_size; 5377 uint8_t segment_size; 5378 if (listptr == NULL) 5379 { 5380 error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64, 5381 off); 5382 5383 /* We will have to assume it is just addresses to the end... */ 5384 address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8; 5385 next_unitp = readendp; 5386 printf ("Unknown CU:\n"); 5387 } 5388 else 5389 { 5390 Dwarf_Die cudie; 5391 if (dwarf_cu_die (listptr->cu, &cudie, 5392 NULL, NULL, NULL, NULL, 5393 NULL, NULL) == NULL) 5394 printf ("Unknown CU (%s):\n", dwarf_errmsg (-1)); 5395 else 5396 printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie)); 5397 5398 if (listptr->offset == off) 5399 { 5400 address_size = listptr_address_size (listptr); 5401 segment_size = 0; 5402 version = 4; 5403 5404 /* The addresses start here, but where do they end? */ 5405 listptr = get_listptr (&known_addrbases, idx); 5406 if (listptr == NULL) 5407 next_unitp = readendp; 5408 else if (listptr->cu->version < 5) 5409 { 5410 next_unitp = start + listptr->offset; 5411 if (listptr->offset < off || listptr->offset > data->d_size) 5412 { 5413 error (0, 0, 5414 "Warning: Bad address base for next unit at %" 5415 PRIx64, off); 5416 next_unitp = readendp; 5417 } 5418 } 5419 else 5420 { 5421 /* Tricky, we don't have a header for this unit, but 5422 there is one for the next. We will have to 5423 "guess" how big it is and subtract it from the 5424 offset (because that points after the header). */ 5425 unsigned int offset_size = listptr_offset_size (listptr); 5426 Dwarf_Off next_off = (listptr->offset 5427 - (offset_size == 4 ? 4 : 12) /* len */ 5428 - 2 /* version */ 5429 - 1 /* address size */ 5430 - 1); /* segment selector size */ 5431 next_unitp = start + next_off; 5432 if (next_off < off || next_off > data->d_size) 5433 { 5434 error (0, 0, 5435 "Warning: Couldn't calculate .debug_addr " 5436 " unit length at %" PRIx64, off); 5437 next_unitp = readendp; 5438 } 5439 } 5440 unit_length = (uint64_t) (next_unitp - readp); 5441 5442 /* Pretend we have a header. */ 5443 printf ("\n"); 5444 printf (_(" Length: %8" PRIu64 "\n"), 5445 unit_length); 5446 printf (_(" DWARF version: %8" PRIu16 "\n"), version); 5447 printf (_(" Address size: %8" PRIu64 "\n"), 5448 (uint64_t) address_size); 5449 printf (_(" Segment size: %8" PRIu64 "\n"), 5450 (uint64_t) segment_size); 5451 printf ("\n"); 5452 } 5453 else 5454 { 5455 /* OK, we have to parse an header first. */ 5456 unit_length = read_4ubyte_unaligned_inc (dbg, readp); 5457 if (unlikely (unit_length == 0xffffffff)) 5458 { 5459 if (unlikely (readp > readendp - 8)) 5460 { 5461 invalid_data: 5462 error (0, 0, "Invalid data"); 5463 return; 5464 } 5465 unit_length = read_8ubyte_unaligned_inc (dbg, readp); 5466 } 5467 printf ("\n"); 5468 printf (_(" Length: %8" PRIu64 "\n"), 5469 unit_length); 5470 5471 /* We need at least 2-bytes (version) + 1-byte 5472 (addr_size) + 1-byte (segment_size) = 4 bytes to 5473 complete the header. And this unit cannot go beyond 5474 the section data. */ 5475 if (readp > readendp - 4 5476 || unit_length < 4 5477 || unit_length > (uint64_t) (readendp - readp)) 5478 goto invalid_data; 5479 5480 next_unitp = readp + unit_length; 5481 5482 version = read_2ubyte_unaligned_inc (dbg, readp); 5483 printf (_(" DWARF version: %8" PRIu16 "\n"), version); 5484 5485 if (version != 5) 5486 { 5487 error (0, 0, _("Unknown version")); 5488 goto next_unit; 5489 } 5490 5491 address_size = *readp++; 5492 printf (_(" Address size: %8" PRIu64 "\n"), 5493 (uint64_t) address_size); 5494 5495 if (address_size != 4 && address_size != 8) 5496 { 5497 error (0, 0, _("unsupported address size")); 5498 goto next_unit; 5499 } 5500 5501 segment_size = *readp++; 5502 printf (_(" Segment size: %8" PRIu64 "\n"), 5503 (uint64_t) segment_size); 5504 printf ("\n"); 5505 5506 if (segment_size != 0) 5507 { 5508 error (0, 0, _("unsupported segment size")); 5509 goto next_unit; 5510 } 5511 5512 if (listptr->offset != (Dwarf_Off) (readp - start)) 5513 { 5514 error (0, 0, "Address index doesn't start after header"); 5515 goto next_unit; 5516 } 5517 } 5518 } 5519 5520 int digits = 1; 5521 size_t addresses = (next_unitp - readp) / address_size; 5522 while (addresses >= 10) 5523 { 5524 ++digits; 5525 addresses /= 10; 5526 } 5527 5528 unsigned int uidx = 0; 5529 size_t index_offset = readp - (const unsigned char *) data->d_buf; 5530 printf (" Addresses start at offset 0x%zx:\n", index_offset); 5531 while (readp <= next_unitp - address_size) 5532 { 5533 Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg, 5534 readp); 5535 printf (" [%*u] ", digits, uidx++); 5536 print_dwarf_addr (dwflmod, address_size, addr, addr); 5537 printf ("\n"); 5538 } 5539 printf ("\n"); 5540 5541 if (readp != next_unitp) 5542 error (0, 0, "extra %zd bytes at end of unit", 5543 (size_t) (next_unitp - readp)); 5544 5545 next_unit: 5546 readp = next_unitp; 5547 } 5548} 5549 5550/* Print content of DWARF .debug_aranges section. We fortunately do 5551 not have to know a bit about the structure of the section, libdwarf 5552 takes care of it. */ 5553static void 5554print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, 5555 GElf_Shdr *shdr, Dwarf *dbg) 5556{ 5557 Dwarf_Aranges *aranges; 5558 size_t cnt; 5559 if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0)) 5560 { 5561 error (0, 0, _("cannot get .debug_aranges content: %s"), 5562 dwarf_errmsg (-1)); 5563 return; 5564 } 5565 5566 GElf_Shdr glink_mem; 5567 GElf_Shdr *glink; 5568 glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem); 5569 if (glink == NULL) 5570 { 5571 error (0, 0, _("invalid sh_link value in section %zu"), 5572 elf_ndxscn (scn)); 5573 return; 5574 } 5575 5576 printf (ngettext ("\ 5577\nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n", 5578 "\ 5579\nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n", 5580 cnt), 5581 elf_ndxscn (scn), section_name (ebl, shdr), 5582 (uint64_t) shdr->sh_offset, cnt); 5583 5584 /* Compute floor(log16(cnt)). */ 5585 size_t tmp = cnt; 5586 int digits = 1; 5587 while (tmp >= 16) 5588 { 5589 ++digits; 5590 tmp >>= 4; 5591 } 5592 5593 for (size_t n = 0; n < cnt; ++n) 5594 { 5595 Dwarf_Arange *runp = dwarf_onearange (aranges, n); 5596 if (unlikely (runp == NULL)) 5597 { 5598 printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1)); 5599 return; 5600 } 5601 5602 Dwarf_Addr start; 5603 Dwarf_Word length; 5604 Dwarf_Off offset; 5605 5606 if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0)) 5607 printf (_(" [%*zu] ???\n"), digits, n); 5608 else 5609 printf (_(" [%*zu] start: %0#*" PRIx64 5610 ", length: %5" PRIu64 ", CU DIE offset: %6" 5611 PRId64 "\n"), 5612 digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18, 5613 (uint64_t) start, (uint64_t) length, (int64_t) offset); 5614 } 5615} 5616 5617 5618/* Print content of DWARF .debug_aranges section. */ 5619static void 5620print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 5621 Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, 5622 GElf_Shdr *shdr, Dwarf *dbg) 5623{ 5624 if (decodedaranges) 5625 { 5626 print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg); 5627 return; 5628 } 5629 5630 Elf_Data *data = (dbg->sectiondata[IDX_debug_aranges] 5631 ?: elf_rawdata (scn, NULL)); 5632 5633 if (unlikely (data == NULL)) 5634 { 5635 error (0, 0, _("cannot get .debug_aranges content: %s"), 5636 elf_errmsg (-1)); 5637 return; 5638 } 5639 5640 printf (_("\ 5641\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 5642 elf_ndxscn (scn), section_name (ebl, shdr), 5643 (uint64_t) shdr->sh_offset); 5644 5645 const unsigned char *readp = data->d_buf; 5646 const unsigned char *readendp = readp + data->d_size; 5647 5648 while (readp < readendp) 5649 { 5650 const unsigned char *hdrstart = readp; 5651 size_t start_offset = hdrstart - (const unsigned char *) data->d_buf; 5652 5653 printf (_("\nTable at offset %zu:\n"), start_offset); 5654 if (readp + 4 > readendp) 5655 { 5656 invalid_data: 5657 error (0, 0, _("invalid data in section [%zu] '%s'"), 5658 elf_ndxscn (scn), section_name (ebl, shdr)); 5659 return; 5660 } 5661 5662 Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp); 5663 unsigned int length_bytes = 4; 5664 if (length == DWARF3_LENGTH_64_BIT) 5665 { 5666 if (readp + 8 > readendp) 5667 goto invalid_data; 5668 length = read_8ubyte_unaligned_inc (dbg, readp); 5669 length_bytes = 8; 5670 } 5671 5672 const unsigned char *nexthdr = readp + length; 5673 printf (_("\n Length: %6" PRIu64 "\n"), 5674 (uint64_t) length); 5675 5676 if (unlikely (length > (size_t) (readendp - readp))) 5677 goto invalid_data; 5678 5679 if (length == 0) 5680 continue; 5681 5682 if (readp + 2 > readendp) 5683 goto invalid_data; 5684 uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp); 5685 printf (_(" DWARF version: %6" PRIuFAST16 "\n"), 5686 version); 5687 if (version != 2) 5688 { 5689 error (0, 0, _("unsupported aranges version")); 5690 goto next_table; 5691 } 5692 5693 Dwarf_Word offset; 5694 if (readp + length_bytes > readendp) 5695 goto invalid_data; 5696 if (length_bytes == 8) 5697 offset = read_8ubyte_unaligned_inc (dbg, readp); 5698 else 5699 offset = read_4ubyte_unaligned_inc (dbg, readp); 5700 printf (_(" CU offset: %6" PRIx64 "\n"), 5701 (uint64_t) offset); 5702 5703 if (readp + 1 > readendp) 5704 goto invalid_data; 5705 unsigned int address_size = *readp++; 5706 printf (_(" Address size: %6" PRIu64 "\n"), 5707 (uint64_t) address_size); 5708 if (address_size != 4 && address_size != 8) 5709 { 5710 error (0, 0, _("unsupported address size")); 5711 goto next_table; 5712 } 5713 5714 if (readp + 1 > readendp) 5715 goto invalid_data; 5716 unsigned int segment_size = *readp++; 5717 printf (_(" Segment size: %6" PRIu64 "\n\n"), 5718 (uint64_t) segment_size); 5719 if (segment_size != 0 && segment_size != 4 && segment_size != 8) 5720 { 5721 error (0, 0, _("unsupported segment size")); 5722 goto next_table; 5723 } 5724 5725 /* Round the address to the next multiple of 2*address_size. */ 5726 readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size))) 5727 % (2 * address_size)); 5728 5729 while (readp < nexthdr) 5730 { 5731 Dwarf_Word range_address; 5732 Dwarf_Word range_length; 5733 Dwarf_Word segment = 0; 5734 if (readp + 2 * address_size + segment_size > readendp) 5735 goto invalid_data; 5736 if (address_size == 4) 5737 { 5738 range_address = read_4ubyte_unaligned_inc (dbg, readp); 5739 range_length = read_4ubyte_unaligned_inc (dbg, readp); 5740 } 5741 else 5742 { 5743 range_address = read_8ubyte_unaligned_inc (dbg, readp); 5744 range_length = read_8ubyte_unaligned_inc (dbg, readp); 5745 } 5746 5747 if (segment_size == 4) 5748 segment = read_4ubyte_unaligned_inc (dbg, readp); 5749 else if (segment_size == 8) 5750 segment = read_8ubyte_unaligned_inc (dbg, readp); 5751 5752 if (range_address == 0 && range_length == 0 && segment == 0) 5753 break; 5754 5755 printf (" "); 5756 print_dwarf_addr (dwflmod, address_size, range_address, 5757 range_address); 5758 printf (".."); 5759 print_dwarf_addr (dwflmod, address_size, 5760 range_address + range_length - 1, 5761 range_length); 5762 if (segment_size != 0) 5763 printf (" (%" PRIx64 ")\n", (uint64_t) segment); 5764 else 5765 printf ("\n"); 5766 } 5767 5768 next_table: 5769 if (readp != nexthdr) 5770 { 5771 size_t padding = nexthdr - readp; 5772 printf (_(" %zu padding bytes\n"), padding); 5773 readp = nexthdr; 5774 } 5775 } 5776} 5777 5778 5779static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu); 5780 5781/* Returns true and sets cu and cu_base if the given Dwarf is a split 5782 DWARF (.dwo) file. */ 5783static bool 5784split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base) 5785{ 5786 uint64_t id; 5787 if (is_split_dwarf (dbg, &id, cu)) 5788 { 5789 Dwarf_Die cudie; 5790 if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0) 5791 { 5792 *cu_base = cudie_base (&cudie); 5793 return true; 5794 } 5795 } 5796 return false; 5797} 5798 5799/* Print content of DWARF .debug_rnglists section. */ 5800static void 5801print_debug_rnglists_section (Dwfl_Module *dwflmod, 5802 Ebl *ebl, 5803 GElf_Ehdr *ehdr __attribute__ ((unused)), 5804 Elf_Scn *scn, GElf_Shdr *shdr, 5805 Dwarf *dbg __attribute__((unused))) 5806{ 5807 printf (_("\ 5808\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 5809 elf_ndxscn (scn), section_name (ebl, shdr), 5810 (uint64_t) shdr->sh_offset); 5811 5812 Elf_Data *data =(dbg->sectiondata[IDX_debug_rnglists] 5813 ?: elf_rawdata (scn, NULL)); 5814 if (unlikely (data == NULL)) 5815 { 5816 error (0, 0, _("cannot get .debug_rnglists content: %s"), 5817 elf_errmsg (-1)); 5818 return; 5819 } 5820 5821 /* For the listptr to get the base address/CU. */ 5822 sort_listptr (&known_rnglistptr, "rnglistptr"); 5823 size_t listptr_idx = 0; 5824 5825 const unsigned char *readp = data->d_buf; 5826 const unsigned char *const dataend = ((unsigned char *) data->d_buf 5827 + data->d_size); 5828 while (readp < dataend) 5829 { 5830 if (unlikely (readp > dataend - 4)) 5831 { 5832 invalid_data: 5833 error (0, 0, _("invalid data in section [%zu] '%s'"), 5834 elf_ndxscn (scn), section_name (ebl, shdr)); 5835 return; 5836 } 5837 5838 ptrdiff_t offset = readp - (unsigned char *) data->d_buf; 5839 printf (_("Table at Offset 0x%" PRIx64 ":\n\n"), 5840 (uint64_t) offset); 5841 5842 uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp); 5843 unsigned int offset_size = 4; 5844 if (unlikely (unit_length == 0xffffffff)) 5845 { 5846 if (unlikely (readp > dataend - 8)) 5847 goto invalid_data; 5848 5849 unit_length = read_8ubyte_unaligned_inc (dbg, readp); 5850 offset_size = 8; 5851 } 5852 printf (_(" Length: %8" PRIu64 "\n"), unit_length); 5853 5854 /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8 5855 bytes to complete the header. And this unit cannot go beyond 5856 the section data. */ 5857 if (readp > dataend - 8 5858 || unit_length < 8 5859 || unit_length > (uint64_t) (dataend - readp)) 5860 goto invalid_data; 5861 5862 const unsigned char *nexthdr = readp + unit_length; 5863 5864 uint16_t version = read_2ubyte_unaligned_inc (dbg, readp); 5865 printf (_(" DWARF version: %8" PRIu16 "\n"), version); 5866 5867 if (version != 5) 5868 { 5869 error (0, 0, _("Unknown version")); 5870 goto next_table; 5871 } 5872 5873 uint8_t address_size = *readp++; 5874 printf (_(" Address size: %8" PRIu64 "\n"), 5875 (uint64_t) address_size); 5876 5877 if (address_size != 4 && address_size != 8) 5878 { 5879 error (0, 0, _("unsupported address size")); 5880 goto next_table; 5881 } 5882 5883 uint8_t segment_size = *readp++; 5884 printf (_(" Segment size: %8" PRIu64 "\n"), 5885 (uint64_t) segment_size); 5886 5887 if (segment_size != 0 && segment_size != 4 && segment_size != 8) 5888 { 5889 error (0, 0, _("unsupported segment size")); 5890 goto next_table; 5891 } 5892 5893 uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp); 5894 printf (_(" Offset entries: %8" PRIu64 "\n"), 5895 (uint64_t) offset_entry_count); 5896 5897 /* We need the CU that uses this unit to get the initial base address. */ 5898 Dwarf_Addr cu_base = 0; 5899 struct Dwarf_CU *cu = NULL; 5900 if (listptr_cu (&known_rnglistptr, &listptr_idx, 5901 (Dwarf_Off) offset, 5902 (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf), 5903 &cu_base, &cu) 5904 || split_dwarf_cu_base (dbg, &cu, &cu_base)) 5905 { 5906 Dwarf_Die cudie; 5907 if (dwarf_cu_die (cu, &cudie, 5908 NULL, NULL, NULL, NULL, 5909 NULL, NULL) == NULL) 5910 printf (_(" Unknown CU base: ")); 5911 else 5912 printf (_(" CU [%6" PRIx64 "] base: "), 5913 dwarf_dieoffset (&cudie)); 5914 print_dwarf_addr (dwflmod, address_size, cu_base, cu_base); 5915 printf ("\n"); 5916 } 5917 else 5918 printf (_(" Not associated with a CU.\n")); 5919 5920 printf ("\n"); 5921 5922 const unsigned char *offset_array_start = readp; 5923 if (offset_entry_count > 0) 5924 { 5925 uint64_t max_entries = (unit_length - 8) / offset_size; 5926 if (offset_entry_count > max_entries) 5927 { 5928 error (0, 0, 5929 _("too many offset entries for unit length")); 5930 offset_entry_count = max_entries; 5931 } 5932 5933 printf (_(" Offsets starting at 0x%" PRIx64 ":\n"), 5934 (uint64_t) (offset_array_start 5935 - (unsigned char *) data->d_buf)); 5936 for (uint32_t idx = 0; idx < offset_entry_count; idx++) 5937 { 5938 printf (" [%6" PRIu32 "] ", idx); 5939 if (offset_size == 4) 5940 { 5941 uint32_t off = read_4ubyte_unaligned_inc (dbg, readp); 5942 printf ("0x%" PRIx32 "\n", off); 5943 } 5944 else 5945 { 5946 uint64_t off = read_8ubyte_unaligned_inc (dbg, readp); 5947 printf ("0x%" PRIx64 "\n", off); 5948 } 5949 } 5950 printf ("\n"); 5951 } 5952 5953 Dwarf_Addr base = cu_base; 5954 bool start_of_list = true; 5955 while (readp < nexthdr) 5956 { 5957 uint8_t kind = *readp++; 5958 uint64_t op1, op2; 5959 5960 /* Skip padding. */ 5961 if (start_of_list && kind == DW_RLE_end_of_list) 5962 continue; 5963 5964 if (start_of_list) 5965 { 5966 base = cu_base; 5967 printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n", 5968 (uint64_t) (readp - (unsigned char *) data->d_buf - 1), 5969 (uint64_t) (readp - offset_array_start - 1)); 5970 start_of_list = false; 5971 } 5972 5973 printf (" %s", dwarf_range_list_encoding_name (kind)); 5974 switch (kind) 5975 { 5976 case DW_RLE_end_of_list: 5977 start_of_list = true; 5978 printf ("\n\n"); 5979 break; 5980 5981 case DW_RLE_base_addressx: 5982 if ((uint64_t) (nexthdr - readp) < 1) 5983 { 5984 invalid_range: 5985 error (0, 0, _("invalid range list data")); 5986 goto next_table; 5987 } 5988 get_uleb128 (op1, readp, nexthdr); 5989 printf (" %" PRIx64 "\n", op1); 5990 if (! print_unresolved_addresses) 5991 { 5992 Dwarf_Addr addr; 5993 if (get_indexed_addr (cu, op1, &addr) != 0) 5994 printf (" ???\n"); 5995 else 5996 { 5997 printf (" "); 5998 print_dwarf_addr (dwflmod, address_size, addr, addr); 5999 printf ("\n"); 6000 } 6001 } 6002 break; 6003 6004 case DW_RLE_startx_endx: 6005 if ((uint64_t) (nexthdr - readp) < 1) 6006 goto invalid_range; 6007 get_uleb128 (op1, readp, nexthdr); 6008 if ((uint64_t) (nexthdr - readp) < 1) 6009 goto invalid_range; 6010 get_uleb128 (op2, readp, nexthdr); 6011 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2); 6012 if (! print_unresolved_addresses) 6013 { 6014 Dwarf_Addr addr1; 6015 Dwarf_Addr addr2; 6016 if (get_indexed_addr (cu, op1, &addr1) != 0 6017 || get_indexed_addr (cu, op2, &addr2) != 0) 6018 { 6019 printf (" ???..\n"); 6020 printf (" ???\n"); 6021 } 6022 else 6023 { 6024 printf (" "); 6025 print_dwarf_addr (dwflmod, address_size, addr1, addr1); 6026 printf ("..\n "); 6027 print_dwarf_addr (dwflmod, address_size, 6028 addr2 - 1, addr2); 6029 printf ("\n"); 6030 } 6031 } 6032 break; 6033 6034 case DW_RLE_startx_length: 6035 if ((uint64_t) (nexthdr - readp) < 1) 6036 goto invalid_range; 6037 get_uleb128 (op1, readp, nexthdr); 6038 if ((uint64_t) (nexthdr - readp) < 1) 6039 goto invalid_range; 6040 get_uleb128 (op2, readp, nexthdr); 6041 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2); 6042 if (! print_unresolved_addresses) 6043 { 6044 Dwarf_Addr addr1; 6045 Dwarf_Addr addr2; 6046 if (get_indexed_addr (cu, op1, &addr1) != 0) 6047 { 6048 printf (" ???..\n"); 6049 printf (" ???\n"); 6050 } 6051 else 6052 { 6053 addr2 = addr1 + op2; 6054 printf (" "); 6055 print_dwarf_addr (dwflmod, address_size, addr1, addr1); 6056 printf ("..\n "); 6057 print_dwarf_addr (dwflmod, address_size, 6058 addr2 - 1, addr2); 6059 printf ("\n"); 6060 } 6061 } 6062 break; 6063 6064 case DW_RLE_offset_pair: 6065 if ((uint64_t) (nexthdr - readp) < 1) 6066 goto invalid_range; 6067 get_uleb128 (op1, readp, nexthdr); 6068 if ((uint64_t) (nexthdr - readp) < 1) 6069 goto invalid_range; 6070 get_uleb128 (op2, readp, nexthdr); 6071 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2); 6072 if (! print_unresolved_addresses) 6073 { 6074 op1 += base; 6075 op2 += base; 6076 printf (" "); 6077 print_dwarf_addr (dwflmod, address_size, op1, op1); 6078 printf ("..\n "); 6079 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2); 6080 printf ("\n"); 6081 } 6082 break; 6083 6084 case DW_RLE_base_address: 6085 if (address_size == 4) 6086 { 6087 if ((uint64_t) (nexthdr - readp) < 4) 6088 goto invalid_range; 6089 op1 = read_4ubyte_unaligned_inc (dbg, readp); 6090 } 6091 else 6092 { 6093 if ((uint64_t) (nexthdr - readp) < 8) 6094 goto invalid_range; 6095 op1 = read_8ubyte_unaligned_inc (dbg, readp); 6096 } 6097 base = op1; 6098 printf (" 0x%" PRIx64 "\n", base); 6099 if (! print_unresolved_addresses) 6100 { 6101 printf (" "); 6102 print_dwarf_addr (dwflmod, address_size, base, base); 6103 printf ("\n"); 6104 } 6105 break; 6106 6107 case DW_RLE_start_end: 6108 if (address_size == 4) 6109 { 6110 if ((uint64_t) (nexthdr - readp) < 8) 6111 goto invalid_range; 6112 op1 = read_4ubyte_unaligned_inc (dbg, readp); 6113 op2 = read_4ubyte_unaligned_inc (dbg, readp); 6114 } 6115 else 6116 { 6117 if ((uint64_t) (nexthdr - readp) < 16) 6118 goto invalid_range; 6119 op1 = read_8ubyte_unaligned_inc (dbg, readp); 6120 op2 = read_8ubyte_unaligned_inc (dbg, readp); 6121 } 6122 printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2); 6123 if (! print_unresolved_addresses) 6124 { 6125 printf (" "); 6126 print_dwarf_addr (dwflmod, address_size, op1, op1); 6127 printf ("..\n "); 6128 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2); 6129 printf ("\n"); 6130 } 6131 break; 6132 6133 case DW_RLE_start_length: 6134 if (address_size == 4) 6135 { 6136 if ((uint64_t) (nexthdr - readp) < 4) 6137 goto invalid_range; 6138 op1 = read_4ubyte_unaligned_inc (dbg, readp); 6139 } 6140 else 6141 { 6142 if ((uint64_t) (nexthdr - readp) < 8) 6143 goto invalid_range; 6144 op1 = read_8ubyte_unaligned_inc (dbg, readp); 6145 } 6146 if ((uint64_t) (nexthdr - readp) < 1) 6147 goto invalid_range; 6148 get_uleb128 (op2, readp, nexthdr); 6149 printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2); 6150 if (! print_unresolved_addresses) 6151 { 6152 op2 = op1 + op2; 6153 printf (" "); 6154 print_dwarf_addr (dwflmod, address_size, op1, op1); 6155 printf ("..\n "); 6156 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2); 6157 printf ("\n"); 6158 } 6159 break; 6160 6161 default: 6162 goto invalid_range; 6163 } 6164 } 6165 6166 next_table: 6167 if (readp != nexthdr) 6168 { 6169 size_t padding = nexthdr - readp; 6170 printf (_(" %zu padding bytes\n\n"), padding); 6171 readp = nexthdr; 6172 } 6173 } 6174} 6175 6176/* Print content of DWARF .debug_ranges section. */ 6177static void 6178print_debug_ranges_section (Dwfl_Module *dwflmod, 6179 Ebl *ebl, GElf_Ehdr *ehdr, 6180 Elf_Scn *scn, GElf_Shdr *shdr, 6181 Dwarf *dbg) 6182{ 6183 Elf_Data *data = (dbg->sectiondata[IDX_debug_ranges] 6184 ?: elf_rawdata (scn, NULL)); 6185 if (unlikely (data == NULL)) 6186 { 6187 error (0, 0, _("cannot get .debug_ranges content: %s"), 6188 elf_errmsg (-1)); 6189 return; 6190 } 6191 6192 printf (_("\ 6193\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 6194 elf_ndxscn (scn), section_name (ebl, shdr), 6195 (uint64_t) shdr->sh_offset); 6196 6197 sort_listptr (&known_rangelistptr, "rangelistptr"); 6198 size_t listptr_idx = 0; 6199 6200 uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8; 6201 6202 bool first = true; 6203 Dwarf_Addr base = 0; 6204 unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size; 6205 unsigned char *readp = data->d_buf; 6206 Dwarf_CU *last_cu = NULL; 6207 while (readp < endp) 6208 { 6209 ptrdiff_t offset = readp - (unsigned char *) data->d_buf; 6210 Dwarf_CU *cu = last_cu; 6211 6212 if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx, 6213 &address_size, NULL, &base, &cu, 6214 offset, &readp, endp, NULL)) 6215 continue; 6216 6217 if (last_cu != cu) 6218 { 6219 Dwarf_Die cudie; 6220 if (dwarf_cu_die (cu, &cudie, 6221 NULL, NULL, NULL, NULL, 6222 NULL, NULL) == NULL) 6223 printf (_("\n Unknown CU base: ")); 6224 else 6225 printf (_("\n CU [%6" PRIx64 "] base: "), 6226 dwarf_dieoffset (&cudie)); 6227 print_dwarf_addr (dwflmod, address_size, base, base); 6228 printf ("\n"); 6229 } 6230 last_cu = cu; 6231 6232 if (unlikely (data->d_size - offset < (size_t) address_size * 2)) 6233 { 6234 printf (_(" [%6tx] <INVALID DATA>\n"), offset); 6235 break; 6236 } 6237 6238 Dwarf_Addr begin; 6239 Dwarf_Addr end; 6240 if (address_size == 8) 6241 { 6242 begin = read_8ubyte_unaligned_inc (dbg, readp); 6243 end = read_8ubyte_unaligned_inc (dbg, readp); 6244 } 6245 else 6246 { 6247 begin = read_4ubyte_unaligned_inc (dbg, readp); 6248 end = read_4ubyte_unaligned_inc (dbg, readp); 6249 if (begin == (Dwarf_Addr) (uint32_t) -1) 6250 begin = (Dwarf_Addr) -1l; 6251 } 6252 6253 if (begin == (Dwarf_Addr) -1l) /* Base address entry. */ 6254 { 6255 if (first) 6256 printf (" [%6tx] ", offset); 6257 else 6258 printf (" "); 6259 puts (_("base address")); 6260 printf (" "); 6261 print_dwarf_addr (dwflmod, address_size, end, end); 6262 printf ("\n"); 6263 base = end; 6264 first = false; 6265 } 6266 else if (begin == 0 && end == 0) /* End of list entry. */ 6267 { 6268 if (first) 6269 printf (_(" [%6tx] empty list\n"), offset); 6270 first = true; 6271 } 6272 else 6273 { 6274 /* We have an address range entry. */ 6275 if (first) /* First address range entry in a list. */ 6276 printf (" [%6tx] ", offset); 6277 else 6278 printf (" "); 6279 6280 printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end); 6281 if (! print_unresolved_addresses) 6282 { 6283 printf (" "); 6284 print_dwarf_addr (dwflmod, address_size, base + begin, 6285 base + begin); 6286 printf ("..\n "); 6287 print_dwarf_addr (dwflmod, address_size, 6288 base + end - 1, base + end); 6289 printf ("\n"); 6290 } 6291 6292 first = false; 6293 } 6294 } 6295} 6296 6297#define REGNAMESZ 16 6298static const char * 6299register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc, 6300 char name[REGNAMESZ], int *bits, int *type) 6301{ 6302 const char *set; 6303 const char *pfx; 6304 int ignore; 6305 ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set, 6306 bits ?: &ignore, type ?: &ignore); 6307 if (n <= 0) 6308 { 6309 if (loc != NULL) 6310 snprintf (name, REGNAMESZ, "reg%u", loc->regno); 6311 else 6312 snprintf (name, REGNAMESZ, "??? 0x%x", regno); 6313 if (bits != NULL) 6314 *bits = loc != NULL ? loc->bits : 0; 6315 if (type != NULL) 6316 *type = DW_ATE_unsigned; 6317 set = "??? unrecognized"; 6318 } 6319 else 6320 { 6321 if (bits != NULL && *bits <= 0) 6322 *bits = loc != NULL ? loc->bits : 0; 6323 if (type != NULL && *type == DW_ATE_void) 6324 *type = DW_ATE_unsigned; 6325 6326 } 6327 return set; 6328} 6329 6330static const unsigned char * 6331read_encoded (unsigned int encoding, const unsigned char *readp, 6332 const unsigned char *const endp, uint64_t *res, Dwarf *dbg) 6333{ 6334 if ((encoding & 0xf) == DW_EH_PE_absptr) 6335 encoding = gelf_getclass (dbg->elf) == ELFCLASS32 6336 ? DW_EH_PE_udata4 : DW_EH_PE_udata8; 6337 6338 switch (encoding & 0xf) 6339 { 6340 case DW_EH_PE_uleb128: 6341 get_uleb128 (*res, readp, endp); 6342 break; 6343 case DW_EH_PE_sleb128: 6344 get_sleb128 (*res, readp, endp); 6345 break; 6346 case DW_EH_PE_udata2: 6347 if (readp + 2 > endp) 6348 goto invalid; 6349 *res = read_2ubyte_unaligned_inc (dbg, readp); 6350 break; 6351 case DW_EH_PE_udata4: 6352 if (readp + 4 > endp) 6353 goto invalid; 6354 *res = read_4ubyte_unaligned_inc (dbg, readp); 6355 break; 6356 case DW_EH_PE_udata8: 6357 if (readp + 8 > endp) 6358 goto invalid; 6359 *res = read_8ubyte_unaligned_inc (dbg, readp); 6360 break; 6361 case DW_EH_PE_sdata2: 6362 if (readp + 2 > endp) 6363 goto invalid; 6364 *res = read_2sbyte_unaligned_inc (dbg, readp); 6365 break; 6366 case DW_EH_PE_sdata4: 6367 if (readp + 4 > endp) 6368 goto invalid; 6369 *res = read_4sbyte_unaligned_inc (dbg, readp); 6370 break; 6371 case DW_EH_PE_sdata8: 6372 if (readp + 8 > endp) 6373 goto invalid; 6374 *res = read_8sbyte_unaligned_inc (dbg, readp); 6375 break; 6376 default: 6377 invalid: 6378 error (1, 0, 6379 _("invalid encoding")); 6380 } 6381 6382 return readp; 6383} 6384 6385static const char * 6386regname (Ebl *ebl, unsigned int regno, char *regnamebuf) 6387{ 6388 register_info (ebl, regno, NULL, regnamebuf, NULL, NULL); 6389 6390 return regnamebuf; 6391} 6392 6393static void 6394print_cfa_program (const unsigned char *readp, const unsigned char *const endp, 6395 Dwarf_Word vma_base, unsigned int code_align, 6396 int data_align, 6397 unsigned int version, unsigned int ptr_size, 6398 unsigned int encoding, 6399 Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg) 6400{ 6401 char regnamebuf[REGNAMESZ]; 6402 6403 puts ("\n Program:"); 6404 Dwarf_Word pc = vma_base; 6405 while (readp < endp) 6406 { 6407 unsigned int opcode = *readp++; 6408 6409 if (opcode < DW_CFA_advance_loc) 6410 /* Extended opcode. */ 6411 switch (opcode) 6412 { 6413 uint64_t op1; 6414 int64_t sop1; 6415 uint64_t op2; 6416 int64_t sop2; 6417 6418 case DW_CFA_nop: 6419 puts (" nop"); 6420 break; 6421 case DW_CFA_set_loc: 6422 if ((uint64_t) (endp - readp) < 1) 6423 goto invalid; 6424 readp = read_encoded (encoding, readp, endp, &op1, dbg); 6425 printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n", 6426 op1, pc = vma_base + op1); 6427 break; 6428 case DW_CFA_advance_loc1: 6429 if ((uint64_t) (endp - readp) < 1) 6430 goto invalid; 6431 printf (" advance_loc1 %u to %#" PRIx64 "\n", 6432 *readp, pc += *readp * code_align); 6433 ++readp; 6434 break; 6435 case DW_CFA_advance_loc2: 6436 if ((uint64_t) (endp - readp) < 2) 6437 goto invalid; 6438 op1 = read_2ubyte_unaligned_inc (dbg, readp); 6439 printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n", 6440 op1, pc += op1 * code_align); 6441 break; 6442 case DW_CFA_advance_loc4: 6443 if ((uint64_t) (endp - readp) < 4) 6444 goto invalid; 6445 op1 = read_4ubyte_unaligned_inc (dbg, readp); 6446 printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n", 6447 op1, pc += op1 * code_align); 6448 break; 6449 case DW_CFA_offset_extended: 6450 if ((uint64_t) (endp - readp) < 1) 6451 goto invalid; 6452 get_uleb128 (op1, readp, endp); 6453 if ((uint64_t) (endp - readp) < 1) 6454 goto invalid; 6455 get_uleb128 (op2, readp, endp); 6456 printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64 6457 "\n", 6458 op1, regname (ebl, op1, regnamebuf), op2 * data_align); 6459 break; 6460 case DW_CFA_restore_extended: 6461 if ((uint64_t) (endp - readp) < 1) 6462 goto invalid; 6463 get_uleb128 (op1, readp, endp); 6464 printf (" restore_extended r%" PRIu64 " (%s)\n", 6465 op1, regname (ebl, op1, regnamebuf)); 6466 break; 6467 case DW_CFA_undefined: 6468 if ((uint64_t) (endp - readp) < 1) 6469 goto invalid; 6470 get_uleb128 (op1, readp, endp); 6471 printf (" undefined r%" PRIu64 " (%s)\n", op1, 6472 regname (ebl, op1, regnamebuf)); 6473 break; 6474 case DW_CFA_same_value: 6475 if ((uint64_t) (endp - readp) < 1) 6476 goto invalid; 6477 get_uleb128 (op1, readp, endp); 6478 printf (" same_value r%" PRIu64 " (%s)\n", op1, 6479 regname (ebl, op1, regnamebuf)); 6480 break; 6481 case DW_CFA_register: 6482 if ((uint64_t) (endp - readp) < 1) 6483 goto invalid; 6484 get_uleb128 (op1, readp, endp); 6485 if ((uint64_t) (endp - readp) < 1) 6486 goto invalid; 6487 get_uleb128 (op2, readp, endp); 6488 printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n", 6489 op1, regname (ebl, op1, regnamebuf), op2, 6490 regname (ebl, op2, regnamebuf)); 6491 break; 6492 case DW_CFA_remember_state: 6493 puts (" remember_state"); 6494 break; 6495 case DW_CFA_restore_state: 6496 puts (" restore_state"); 6497 break; 6498 case DW_CFA_def_cfa: 6499 if ((uint64_t) (endp - readp) < 1) 6500 goto invalid; 6501 get_uleb128 (op1, readp, endp); 6502 if ((uint64_t) (endp - readp) < 1) 6503 goto invalid; 6504 get_uleb128 (op2, readp, endp); 6505 printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n", 6506 op1, regname (ebl, op1, regnamebuf), op2); 6507 break; 6508 case DW_CFA_def_cfa_register: 6509 if ((uint64_t) (endp - readp) < 1) 6510 goto invalid; 6511 get_uleb128 (op1, readp, endp); 6512 printf (" def_cfa_register r%" PRIu64 " (%s)\n", 6513 op1, regname (ebl, op1, regnamebuf)); 6514 break; 6515 case DW_CFA_def_cfa_offset: 6516 if ((uint64_t) (endp - readp) < 1) 6517 goto invalid; 6518 get_uleb128 (op1, readp, endp); 6519 printf (" def_cfa_offset %" PRIu64 "\n", op1); 6520 break; 6521 case DW_CFA_def_cfa_expression: 6522 if ((uint64_t) (endp - readp) < 1) 6523 goto invalid; 6524 get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */ 6525 printf (" def_cfa_expression %" PRIu64 "\n", op1); 6526 if ((uint64_t) (endp - readp) < op1) 6527 { 6528 invalid: 6529 fputs (_(" <INVALID DATA>\n"), stdout); 6530 return; 6531 } 6532 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL, 6533 op1, readp); 6534 readp += op1; 6535 break; 6536 case DW_CFA_expression: 6537 if ((uint64_t) (endp - readp) < 1) 6538 goto invalid; 6539 get_uleb128 (op1, readp, endp); 6540 if ((uint64_t) (endp - readp) < 1) 6541 goto invalid; 6542 get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */ 6543 printf (" expression r%" PRIu64 " (%s) \n", 6544 op1, regname (ebl, op1, regnamebuf)); 6545 if ((uint64_t) (endp - readp) < op2) 6546 goto invalid; 6547 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL, 6548 op2, readp); 6549 readp += op2; 6550 break; 6551 case DW_CFA_offset_extended_sf: 6552 if ((uint64_t) (endp - readp) < 1) 6553 goto invalid; 6554 get_uleb128 (op1, readp, endp); 6555 if ((uint64_t) (endp - readp) < 1) 6556 goto invalid; 6557 get_sleb128 (sop2, readp, endp); 6558 printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+" 6559 PRId64 "\n", 6560 op1, regname (ebl, op1, regnamebuf), sop2 * data_align); 6561 break; 6562 case DW_CFA_def_cfa_sf: 6563 if ((uint64_t) (endp - readp) < 1) 6564 goto invalid; 6565 get_uleb128 (op1, readp, endp); 6566 if ((uint64_t) (endp - readp) < 1) 6567 goto invalid; 6568 get_sleb128 (sop2, readp, endp); 6569 printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n", 6570 op1, regname (ebl, op1, regnamebuf), sop2 * data_align); 6571 break; 6572 case DW_CFA_def_cfa_offset_sf: 6573 if ((uint64_t) (endp - readp) < 1) 6574 goto invalid; 6575 get_sleb128 (sop1, readp, endp); 6576 printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align); 6577 break; 6578 case DW_CFA_val_offset: 6579 if ((uint64_t) (endp - readp) < 1) 6580 goto invalid; 6581 get_uleb128 (op1, readp, endp); 6582 if ((uint64_t) (endp - readp) < 1) 6583 goto invalid; 6584 get_uleb128 (op2, readp, endp); 6585 printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n", 6586 op1, op2 * data_align); 6587 break; 6588 case DW_CFA_val_offset_sf: 6589 if ((uint64_t) (endp - readp) < 1) 6590 goto invalid; 6591 get_uleb128 (op1, readp, endp); 6592 if ((uint64_t) (endp - readp) < 1) 6593 goto invalid; 6594 get_sleb128 (sop2, readp, endp); 6595 printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n", 6596 op1, sop2 * data_align); 6597 break; 6598 case DW_CFA_val_expression: 6599 if ((uint64_t) (endp - readp) < 1) 6600 goto invalid; 6601 get_uleb128 (op1, readp, endp); 6602 if ((uint64_t) (endp - readp) < 1) 6603 goto invalid; 6604 get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */ 6605 printf (" val_expression r%" PRIu64 " (%s)\n", 6606 op1, regname (ebl, op1, regnamebuf)); 6607 if ((uint64_t) (endp - readp) < op2) 6608 goto invalid; 6609 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, 6610 NULL, op2, readp); 6611 readp += op2; 6612 break; 6613 case DW_CFA_MIPS_advance_loc8: 6614 if ((uint64_t) (endp - readp) < 8) 6615 goto invalid; 6616 op1 = read_8ubyte_unaligned_inc (dbg, readp); 6617 printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n", 6618 op1, pc += op1 * code_align); 6619 break; 6620 case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */ 6621 if (ehdr->e_machine == EM_AARCH64) 6622 puts (" AARCH64_negate_ra_state"); 6623 else 6624 puts (" GNU_window_save"); 6625 break; 6626 case DW_CFA_GNU_args_size: 6627 if ((uint64_t) (endp - readp) < 1) 6628 goto invalid; 6629 get_uleb128 (op1, readp, endp); 6630 printf (" args_size %" PRIu64 "\n", op1); 6631 break; 6632 default: 6633 printf (" ??? (%u)\n", opcode); 6634 break; 6635 } 6636 else if (opcode < DW_CFA_offset) 6637 printf (" advance_loc %u to %#" PRIx64 "\n", 6638 opcode & 0x3f, pc += (opcode & 0x3f) * code_align); 6639 else if (opcode < DW_CFA_restore) 6640 { 6641 uint64_t offset; 6642 if ((uint64_t) (endp - readp) < 1) 6643 goto invalid; 6644 get_uleb128 (offset, readp, endp); 6645 printf (" offset r%u (%s) at cfa%+" PRId64 "\n", 6646 opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf), 6647 offset * data_align); 6648 } 6649 else 6650 printf (" restore r%u (%s)\n", 6651 opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf)); 6652 } 6653} 6654 6655 6656static unsigned int 6657encoded_ptr_size (int encoding, unsigned int ptr_size) 6658{ 6659 switch (encoding & 7) 6660 { 6661 case DW_EH_PE_udata4: 6662 return 4; 6663 case DW_EH_PE_udata8: 6664 return 8; 6665 case 0: 6666 return ptr_size; 6667 } 6668 6669 fprintf (stderr, "Unsupported pointer encoding: %#x, " 6670 "assuming pointer size of %d.\n", encoding, ptr_size); 6671 return ptr_size; 6672} 6673 6674 6675static unsigned int 6676print_encoding (unsigned int val) 6677{ 6678 switch (val & 0xf) 6679 { 6680 case DW_EH_PE_absptr: 6681 fputs ("absptr", stdout); 6682 break; 6683 case DW_EH_PE_uleb128: 6684 fputs ("uleb128", stdout); 6685 break; 6686 case DW_EH_PE_udata2: 6687 fputs ("udata2", stdout); 6688 break; 6689 case DW_EH_PE_udata4: 6690 fputs ("udata4", stdout); 6691 break; 6692 case DW_EH_PE_udata8: 6693 fputs ("udata8", stdout); 6694 break; 6695 case DW_EH_PE_sleb128: 6696 fputs ("sleb128", stdout); 6697 break; 6698 case DW_EH_PE_sdata2: 6699 fputs ("sdata2", stdout); 6700 break; 6701 case DW_EH_PE_sdata4: 6702 fputs ("sdata4", stdout); 6703 break; 6704 case DW_EH_PE_sdata8: 6705 fputs ("sdata8", stdout); 6706 break; 6707 default: 6708 /* We did not use any of the bits after all. */ 6709 return val; 6710 } 6711 6712 return val & ~0xf; 6713} 6714 6715 6716static unsigned int 6717print_relinfo (unsigned int val) 6718{ 6719 switch (val & 0x70) 6720 { 6721 case DW_EH_PE_pcrel: 6722 fputs ("pcrel", stdout); 6723 break; 6724 case DW_EH_PE_textrel: 6725 fputs ("textrel", stdout); 6726 break; 6727 case DW_EH_PE_datarel: 6728 fputs ("datarel", stdout); 6729 break; 6730 case DW_EH_PE_funcrel: 6731 fputs ("funcrel", stdout); 6732 break; 6733 case DW_EH_PE_aligned: 6734 fputs ("aligned", stdout); 6735 break; 6736 default: 6737 return val; 6738 } 6739 6740 return val & ~0x70; 6741} 6742 6743 6744static void 6745print_encoding_base (const char *pfx, unsigned int fde_encoding) 6746{ 6747 printf ("(%s", pfx); 6748 6749 if (fde_encoding == DW_EH_PE_omit) 6750 puts ("omit)"); 6751 else 6752 { 6753 unsigned int w = fde_encoding; 6754 6755 w = print_encoding (w); 6756 6757 if (w & 0x70) 6758 { 6759 if (w != fde_encoding) 6760 fputc_unlocked (' ', stdout); 6761 6762 w = print_relinfo (w); 6763 } 6764 6765 if (w != 0) 6766 printf ("%s%x", w != fde_encoding ? " " : "", w); 6767 6768 puts (")"); 6769 } 6770} 6771 6772 6773static void 6774print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, 6775 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 6776{ 6777 size_t shstrndx; 6778 /* We know this call will succeed since it did in the caller. */ 6779 (void) elf_getshdrstrndx (ebl->elf, &shstrndx); 6780 const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name); 6781 6782 /* Needed if we find PC-relative addresses. */ 6783 GElf_Addr bias; 6784 if (dwfl_module_getelf (dwflmod, &bias) == NULL) 6785 { 6786 error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1)); 6787 return; 6788 } 6789 6790 bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0; 6791 Elf_Data *data = (is_eh_frame 6792 ? elf_rawdata (scn, NULL) 6793 : (dbg->sectiondata[IDX_debug_frame] 6794 ?: elf_rawdata (scn, NULL))); 6795 6796 if (unlikely (data == NULL)) 6797 { 6798 error (0, 0, _("cannot get %s content: %s"), 6799 scnname, elf_errmsg (-1)); 6800 return; 6801 } 6802 6803 if (is_eh_frame) 6804 printf (_("\ 6805\nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 6806 elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset); 6807 else 6808 printf (_("\ 6809\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 6810 elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset); 6811 6812 struct cieinfo 6813 { 6814 ptrdiff_t cie_offset; 6815 const char *augmentation; 6816 unsigned int code_alignment_factor; 6817 unsigned int data_alignment_factor; 6818 uint8_t address_size; 6819 uint8_t fde_encoding; 6820 uint8_t lsda_encoding; 6821 struct cieinfo *next; 6822 } *cies = NULL; 6823 6824 const unsigned char *readp = data->d_buf; 6825 const unsigned char *const dataend = ((unsigned char *) data->d_buf 6826 + data->d_size); 6827 while (readp < dataend) 6828 { 6829 if (unlikely (readp + 4 > dataend)) 6830 { 6831 invalid_data: 6832 error (0, 0, _("invalid data in section [%zu] '%s'"), 6833 elf_ndxscn (scn), scnname); 6834 return; 6835 } 6836 6837 /* At the beginning there must be a CIE. There can be multiple, 6838 hence we test tis in a loop. */ 6839 ptrdiff_t offset = readp - (unsigned char *) data->d_buf; 6840 6841 Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp); 6842 unsigned int length = 4; 6843 if (unlikely (unit_length == 0xffffffff)) 6844 { 6845 if (unlikely (readp + 8 > dataend)) 6846 goto invalid_data; 6847 6848 unit_length = read_8ubyte_unaligned_inc (dbg, readp); 6849 length = 8; 6850 } 6851 6852 if (unlikely (unit_length == 0)) 6853 { 6854 printf (_("\n [%6tx] Zero terminator\n"), offset); 6855 continue; 6856 } 6857 6858 Dwarf_Word maxsize = dataend - readp; 6859 if (unlikely (unit_length > maxsize)) 6860 goto invalid_data; 6861 6862 unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8; 6863 6864 ptrdiff_t start = readp - (unsigned char *) data->d_buf; 6865 const unsigned char *const cieend = readp + unit_length; 6866 if (unlikely (cieend > dataend)) 6867 goto invalid_data; 6868 6869 Dwarf_Off cie_id; 6870 if (length == 4) 6871 { 6872 if (unlikely (cieend - readp < 4)) 6873 goto invalid_data; 6874 cie_id = read_4ubyte_unaligned_inc (dbg, readp); 6875 if (!is_eh_frame && cie_id == DW_CIE_ID_32) 6876 cie_id = DW_CIE_ID_64; 6877 } 6878 else 6879 { 6880 if (unlikely (cieend - readp < 8)) 6881 goto invalid_data; 6882 cie_id = read_8ubyte_unaligned_inc (dbg, readp); 6883 } 6884 6885 uint_fast8_t version = 2; 6886 unsigned int code_alignment_factor; 6887 int data_alignment_factor; 6888 unsigned int fde_encoding = 0; 6889 unsigned int lsda_encoding = 0; 6890 Dwarf_Word initial_location = 0; 6891 Dwarf_Word vma_base = 0; 6892 6893 if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64)) 6894 { 6895 if (unlikely (cieend - readp < 2)) 6896 goto invalid_data; 6897 version = *readp++; 6898 const char *const augmentation = (const char *) readp; 6899 readp = memchr (readp, '\0', cieend - readp); 6900 if (unlikely (readp == NULL)) 6901 goto invalid_data; 6902 ++readp; 6903 6904 uint_fast8_t segment_size = 0; 6905 if (version >= 4) 6906 { 6907 if (cieend - readp < 5) 6908 goto invalid_data; 6909 ptr_size = *readp++; 6910 segment_size = *readp++; 6911 } 6912 6913 if (cieend - readp < 1) 6914 goto invalid_data; 6915 get_uleb128 (code_alignment_factor, readp, cieend); 6916 if (cieend - readp < 1) 6917 goto invalid_data; 6918 get_sleb128 (data_alignment_factor, readp, cieend); 6919 6920 /* In some variant for unwind data there is another field. */ 6921 if (strcmp (augmentation, "eh") == 0) 6922 readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8; 6923 6924 unsigned int return_address_register; 6925 if (cieend - readp < 1) 6926 goto invalid_data; 6927 if (unlikely (version == 1)) 6928 return_address_register = *readp++; 6929 else 6930 get_uleb128 (return_address_register, readp, cieend); 6931 6932 printf ("\n [%6tx] CIE length=%" PRIu64 "\n" 6933 " CIE_id: %" PRIu64 "\n" 6934 " version: %u\n" 6935 " augmentation: \"%s\"\n", 6936 offset, (uint64_t) unit_length, (uint64_t) cie_id, 6937 version, augmentation); 6938 if (version >= 4) 6939 printf (" address_size: %u\n" 6940 " segment_size: %u\n", 6941 ptr_size, segment_size); 6942 printf (" code_alignment_factor: %u\n" 6943 " data_alignment_factor: %d\n" 6944 " return_address_register: %u\n", 6945 code_alignment_factor, 6946 data_alignment_factor, return_address_register); 6947 6948 if (augmentation[0] == 'z') 6949 { 6950 unsigned int augmentationlen; 6951 get_uleb128 (augmentationlen, readp, cieend); 6952 6953 if (augmentationlen > (size_t) (cieend - readp)) 6954 { 6955 error (0, 0, _("invalid augmentation length")); 6956 readp = cieend; 6957 continue; 6958 } 6959 6960 const char *hdr = "Augmentation data:"; 6961 const char *cp = augmentation + 1; 6962 while (*cp != '\0' && cp < augmentation + augmentationlen + 1) 6963 { 6964 printf (" %-26s%#x ", hdr, *readp); 6965 hdr = ""; 6966 6967 if (*cp == 'R') 6968 { 6969 fde_encoding = *readp++; 6970 print_encoding_base (_("FDE address encoding: "), 6971 fde_encoding); 6972 } 6973 else if (*cp == 'L') 6974 { 6975 lsda_encoding = *readp++; 6976 print_encoding_base (_("LSDA pointer encoding: "), 6977 lsda_encoding); 6978 } 6979 else if (*cp == 'P') 6980 { 6981 /* Personality. This field usually has a relocation 6982 attached pointing to __gcc_personality_v0. */ 6983 const unsigned char *startp = readp; 6984 unsigned int encoding = *readp++; 6985 uint64_t val = 0; 6986 readp = read_encoded (encoding, readp, 6987 readp - 1 + augmentationlen, 6988 &val, dbg); 6989 6990 while (++startp < readp) 6991 printf ("%#x ", *startp); 6992 6993 putchar ('('); 6994 print_encoding (encoding); 6995 putchar (' '); 6996 switch (encoding & 0xf) 6997 { 6998 case DW_EH_PE_sleb128: 6999 case DW_EH_PE_sdata2: 7000 case DW_EH_PE_sdata4: 7001 printf ("%" PRId64 ")\n", val); 7002 break; 7003 default: 7004 printf ("%#" PRIx64 ")\n", val); 7005 break; 7006 } 7007 } 7008 else 7009 printf ("(%x)\n", *readp++); 7010 7011 ++cp; 7012 } 7013 } 7014 7015 if (likely (ptr_size == 4 || ptr_size == 8)) 7016 { 7017 struct cieinfo *newp = alloca (sizeof (*newp)); 7018 newp->cie_offset = offset; 7019 newp->augmentation = augmentation; 7020 newp->fde_encoding = fde_encoding; 7021 newp->lsda_encoding = lsda_encoding; 7022 newp->address_size = ptr_size; 7023 newp->code_alignment_factor = code_alignment_factor; 7024 newp->data_alignment_factor = data_alignment_factor; 7025 newp->next = cies; 7026 cies = newp; 7027 } 7028 } 7029 else 7030 { 7031 struct cieinfo *cie = cies; 7032 while (cie != NULL) 7033 if (is_eh_frame 7034 ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset 7035 : cie_id == (Dwarf_Off) cie->cie_offset) 7036 break; 7037 else 7038 cie = cie->next; 7039 if (unlikely (cie == NULL)) 7040 { 7041 puts ("invalid CIE reference in FDE"); 7042 return; 7043 } 7044 7045 /* Initialize from CIE data. */ 7046 fde_encoding = cie->fde_encoding; 7047 lsda_encoding = cie->lsda_encoding; 7048 ptr_size = encoded_ptr_size (fde_encoding, cie->address_size); 7049 code_alignment_factor = cie->code_alignment_factor; 7050 data_alignment_factor = cie->data_alignment_factor; 7051 7052 const unsigned char *base = readp; 7053 // XXX There are sometimes relocations for this value 7054 initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp); 7055 Dwarf_Word address_range 7056 = read_addr_unaligned_inc (ptr_size, dbg, readp); 7057 7058 /* pcrel for an FDE address is relative to the runtime 7059 address of the start_address field itself. Sign extend 7060 if necessary to make sure the calculation is done on the 7061 full 64 bit address even when initial_location only holds 7062 the lower 32 bits. */ 7063 Dwarf_Addr pc_start = initial_location; 7064 if (ptr_size == 4) 7065 pc_start = (uint64_t) (int32_t) pc_start; 7066 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel) 7067 pc_start += ((uint64_t) shdr->sh_addr 7068 + (base - (const unsigned char *) data->d_buf) 7069 - bias); 7070 7071 printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n" 7072 " CIE_pointer: %" PRIu64 "\n" 7073 " initial_location: ", 7074 offset, (uint64_t) unit_length, 7075 cie->cie_offset, (uint64_t) cie_id); 7076 print_dwarf_addr (dwflmod, cie->address_size, 7077 pc_start, initial_location); 7078 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel) 7079 { 7080 vma_base = (((uint64_t) shdr->sh_offset 7081 + (base - (const unsigned char *) data->d_buf) 7082 + (uint64_t) initial_location) 7083 & (ptr_size == 4 7084 ? UINT64_C (0xffffffff) 7085 : UINT64_C (0xffffffffffffffff))); 7086 printf (_(" (offset: %#" PRIx64 ")"), 7087 (uint64_t) vma_base); 7088 } 7089 7090 printf ("\n address_range: %#" PRIx64, 7091 (uint64_t) address_range); 7092 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel) 7093 printf (_(" (end offset: %#" PRIx64 ")"), 7094 ((uint64_t) vma_base + (uint64_t) address_range) 7095 & (ptr_size == 4 7096 ? UINT64_C (0xffffffff) 7097 : UINT64_C (0xffffffffffffffff))); 7098 putchar ('\n'); 7099 7100 if (cie->augmentation[0] == 'z') 7101 { 7102 unsigned int augmentationlen; 7103 if (cieend - readp < 1) 7104 goto invalid_data; 7105 get_uleb128 (augmentationlen, readp, cieend); 7106 7107 if (augmentationlen > (size_t) (cieend - readp)) 7108 { 7109 error (0, 0, _("invalid augmentation length")); 7110 readp = cieend; 7111 continue; 7112 } 7113 7114 if (augmentationlen > 0) 7115 { 7116 const char *hdr = "Augmentation data:"; 7117 const char *cp = cie->augmentation + 1; 7118 unsigned int u = 0; 7119 while (*cp != '\0' 7120 && cp < cie->augmentation + augmentationlen + 1) 7121 { 7122 if (*cp == 'L') 7123 { 7124 uint64_t lsda_pointer; 7125 const unsigned char *p 7126 = read_encoded (lsda_encoding, &readp[u], 7127 &readp[augmentationlen], 7128 &lsda_pointer, dbg); 7129 u = p - readp; 7130 printf (_("\ 7131 %-26sLSDA pointer: %#" PRIx64 "\n"), 7132 hdr, lsda_pointer); 7133 hdr = ""; 7134 } 7135 ++cp; 7136 } 7137 7138 while (u < augmentationlen) 7139 { 7140 printf (" %-26s%#x\n", hdr, readp[u++]); 7141 hdr = ""; 7142 } 7143 } 7144 7145 readp += augmentationlen; 7146 } 7147 } 7148 7149 /* Handle the initialization instructions. */ 7150 if (ptr_size != 4 && ptr_size !=8) 7151 printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size); 7152 else 7153 print_cfa_program (readp, cieend, vma_base, code_alignment_factor, 7154 data_alignment_factor, version, ptr_size, 7155 fde_encoding, dwflmod, ebl, ehdr, dbg); 7156 readp = cieend; 7157 } 7158} 7159 7160 7161/* Returns the signedness (or false if it cannot be determined) and 7162 the byte size (or zero if it cannot be gotten) of the given DIE 7163 DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */ 7164static void 7165die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes) 7166{ 7167 Dwarf_Attribute attr; 7168 Dwarf_Die type; 7169 7170 *bytes = 0; 7171 *is_signed = false; 7172 7173 if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die, 7174 DW_AT_type, 7175 &attr), &type), 7176 &type) == 0) 7177 { 7178 Dwarf_Word val; 7179 *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding, 7180 &attr), &val) == 0 7181 && (val == DW_ATE_signed || val == DW_ATE_signed_char)); 7182 7183 if (dwarf_aggregate_size (&type, &val) == 0) 7184 *bytes = val; 7185 } 7186} 7187 7188struct attrcb_args 7189{ 7190 Dwfl_Module *dwflmod; 7191 Dwarf *dbg; 7192 Dwarf_Die *dies; 7193 int level; 7194 bool silent; 7195 bool is_split; 7196 unsigned int version; 7197 unsigned int addrsize; 7198 unsigned int offset_size; 7199 struct Dwarf_CU *cu; 7200}; 7201 7202 7203static int 7204attr_callback (Dwarf_Attribute *attrp, void *arg) 7205{ 7206 struct attrcb_args *cbargs = (struct attrcb_args *) arg; 7207 const int level = cbargs->level; 7208 Dwarf_Die *die = &cbargs->dies[level]; 7209 bool is_split = cbargs->is_split; 7210 7211 unsigned int attr = dwarf_whatattr (attrp); 7212 if (unlikely (attr == 0)) 7213 { 7214 if (!cbargs->silent) 7215 error (0, 0, _("DIE [%" PRIx64 "] " 7216 "cannot get attribute code: %s"), 7217 dwarf_dieoffset (die), dwarf_errmsg (-1)); 7218 return DWARF_CB_ABORT; 7219 } 7220 7221 unsigned int form = dwarf_whatform (attrp); 7222 if (unlikely (form == 0)) 7223 { 7224 if (!cbargs->silent) 7225 error (0, 0, _("DIE [%" PRIx64 "] " 7226 "cannot get attribute form: %s"), 7227 dwarf_dieoffset (die), dwarf_errmsg (-1)); 7228 return DWARF_CB_ABORT; 7229 } 7230 7231 switch (form) 7232 { 7233 case DW_FORM_addr: 7234 case DW_FORM_addrx: 7235 case DW_FORM_addrx1: 7236 case DW_FORM_addrx2: 7237 case DW_FORM_addrx3: 7238 case DW_FORM_addrx4: 7239 case DW_FORM_GNU_addr_index: 7240 if (!cbargs->silent) 7241 { 7242 Dwarf_Addr addr; 7243 if (unlikely (dwarf_formaddr (attrp, &addr) != 0)) 7244 { 7245 attrval_out: 7246 if (!cbargs->silent) 7247 error (0, 0, _("DIE [%" PRIx64 "] " 7248 "cannot get attribute '%s' (%s) value: " 7249 "%s"), 7250 dwarf_dieoffset (die), 7251 dwarf_attr_name (attr), 7252 dwarf_form_name (form), 7253 dwarf_errmsg (-1)); 7254 /* Don't ABORT, it might be other attributes can be resolved. */ 7255 return DWARF_CB_OK; 7256 } 7257 if (form != DW_FORM_addr ) 7258 { 7259 Dwarf_Word word; 7260 if (dwarf_formudata (attrp, &word) != 0) 7261 goto attrval_out; 7262 printf (" %*s%-20s (%s) [%" PRIx64 "] ", 7263 (int) (level * 2), "", dwarf_attr_name (attr), 7264 dwarf_form_name (form), word); 7265 } 7266 else 7267 printf (" %*s%-20s (%s) ", 7268 (int) (level * 2), "", dwarf_attr_name (attr), 7269 dwarf_form_name (form)); 7270 print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr); 7271 printf ("\n"); 7272 } 7273 break; 7274 7275 case DW_FORM_indirect: 7276 case DW_FORM_strp: 7277 case DW_FORM_line_strp: 7278 case DW_FORM_strx: 7279 case DW_FORM_strx1: 7280 case DW_FORM_strx2: 7281 case DW_FORM_strx3: 7282 case DW_FORM_strx4: 7283 case DW_FORM_string: 7284 case DW_FORM_GNU_strp_alt: 7285 case DW_FORM_GNU_str_index: 7286 if (cbargs->silent) 7287 break; 7288 const char *str = dwarf_formstring (attrp); 7289 if (unlikely (str == NULL)) 7290 goto attrval_out; 7291 printf (" %*s%-20s (%s) \"%s\"\n", 7292 (int) (level * 2), "", dwarf_attr_name (attr), 7293 dwarf_form_name (form), str); 7294 break; 7295 7296 case DW_FORM_ref_addr: 7297 case DW_FORM_ref_udata: 7298 case DW_FORM_ref8: 7299 case DW_FORM_ref4: 7300 case DW_FORM_ref2: 7301 case DW_FORM_ref1: 7302 case DW_FORM_GNU_ref_alt: 7303 case DW_FORM_ref_sup4: 7304 case DW_FORM_ref_sup8: 7305 if (cbargs->silent) 7306 break; 7307 Dwarf_Die ref; 7308 if (unlikely (dwarf_formref_die (attrp, &ref) == NULL)) 7309 goto attrval_out; 7310 7311 printf (" %*s%-20s (%s) ", 7312 (int) (level * 2), "", dwarf_attr_name (attr), 7313 dwarf_form_name (form)); 7314 if (is_split) 7315 printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref)); 7316 else 7317 printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref)); 7318 break; 7319 7320 case DW_FORM_ref_sig8: 7321 if (cbargs->silent) 7322 break; 7323 printf (" %*s%-20s (%s) {%6" PRIx64 "}\n", 7324 (int) (level * 2), "", dwarf_attr_name (attr), 7325 dwarf_form_name (form), 7326 (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp)); 7327 break; 7328 7329 case DW_FORM_sec_offset: 7330 case DW_FORM_rnglistx: 7331 case DW_FORM_loclistx: 7332 case DW_FORM_implicit_const: 7333 case DW_FORM_udata: 7334 case DW_FORM_sdata: 7335 case DW_FORM_data8: /* Note no data16 here, we see that as block. */ 7336 case DW_FORM_data4: 7337 case DW_FORM_data2: 7338 case DW_FORM_data1:; 7339 Dwarf_Word num; 7340 if (unlikely (dwarf_formudata (attrp, &num) != 0)) 7341 goto attrval_out; 7342 7343 const char *valuestr = NULL; 7344 bool as_hex_id = false; 7345 switch (attr) 7346 { 7347 /* This case can take either a constant or a loclistptr. */ 7348 case DW_AT_data_member_location: 7349 if (form != DW_FORM_sec_offset 7350 && (cbargs->version >= 4 7351 || (form != DW_FORM_data4 && form != DW_FORM_data8))) 7352 { 7353 if (!cbargs->silent) 7354 printf (" %*s%-20s (%s) %" PRIuMAX "\n", 7355 (int) (level * 2), "", dwarf_attr_name (attr), 7356 dwarf_form_name (form), (uintmax_t) num); 7357 return DWARF_CB_OK; 7358 } 7359 FALLTHROUGH; 7360 7361 /* These cases always take a loclist[ptr] and no constant. */ 7362 case DW_AT_location: 7363 case DW_AT_data_location: 7364 case DW_AT_vtable_elem_location: 7365 case DW_AT_string_length: 7366 case DW_AT_use_location: 7367 case DW_AT_frame_base: 7368 case DW_AT_return_addr: 7369 case DW_AT_static_link: 7370 case DW_AT_segment: 7371 case DW_AT_GNU_call_site_value: 7372 case DW_AT_GNU_call_site_data_value: 7373 case DW_AT_GNU_call_site_target: 7374 case DW_AT_GNU_call_site_target_clobbered: 7375 case DW_AT_GNU_locviews: 7376 { 7377 bool nlpt; 7378 if (cbargs->cu->version < 5) 7379 { 7380 if (! cbargs->is_split) 7381 { 7382 nlpt = notice_listptr (section_loc, &known_locsptr, 7383 cbargs->addrsize, 7384 cbargs->offset_size, 7385 cbargs->cu, num, attr); 7386 } 7387 else 7388 nlpt = true; 7389 } 7390 else 7391 { 7392 /* Only register for a real section offset. Otherwise 7393 it is a DW_FORM_loclistx which is just an index 7394 number and we should already have registered the 7395 section offset for the index when we saw the 7396 DW_AT_loclists_base CU attribute. */ 7397 if (form == DW_FORM_sec_offset) 7398 nlpt = notice_listptr (section_loc, &known_loclistsptr, 7399 cbargs->addrsize, cbargs->offset_size, 7400 cbargs->cu, num, attr); 7401 else 7402 nlpt = true; 7403 7404 } 7405 7406 if (!cbargs->silent) 7407 { 7408 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset) 7409 printf (" %*s%-20s (%s) location list [%6" 7410 PRIxMAX "]%s\n", 7411 (int) (level * 2), "", dwarf_attr_name (attr), 7412 dwarf_form_name (form), (uintmax_t) num, 7413 nlpt ? "" : " <WARNING offset too big>"); 7414 else 7415 printf (" %*s%-20s (%s) location index [%6" 7416 PRIxMAX "]\n", 7417 (int) (level * 2), "", dwarf_attr_name (attr), 7418 dwarf_form_name (form), (uintmax_t) num); 7419 } 7420 } 7421 return DWARF_CB_OK; 7422 7423 case DW_AT_loclists_base: 7424 { 7425 bool nlpt = notice_listptr (section_loc, &known_loclistsptr, 7426 cbargs->addrsize, cbargs->offset_size, 7427 cbargs->cu, num, attr); 7428 7429 if (!cbargs->silent) 7430 printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n", 7431 (int) (level * 2), "", dwarf_attr_name (attr), 7432 dwarf_form_name (form), (uintmax_t) num, 7433 nlpt ? "" : " <WARNING offset too big>"); 7434 } 7435 return DWARF_CB_OK; 7436 7437 case DW_AT_ranges: 7438 case DW_AT_start_scope: 7439 { 7440 bool nlpt; 7441 if (cbargs->cu->version < 5) 7442 nlpt = notice_listptr (section_ranges, &known_rangelistptr, 7443 cbargs->addrsize, cbargs->offset_size, 7444 cbargs->cu, num, attr); 7445 else 7446 { 7447 /* Only register for a real section offset. Otherwise 7448 it is a DW_FORM_rangelistx which is just an index 7449 number and we should already have registered the 7450 section offset for the index when we saw the 7451 DW_AT_rnglists_base CU attribute. */ 7452 if (form == DW_FORM_sec_offset) 7453 nlpt = notice_listptr (section_ranges, &known_rnglistptr, 7454 cbargs->addrsize, cbargs->offset_size, 7455 cbargs->cu, num, attr); 7456 else 7457 nlpt = true; 7458 } 7459 7460 if (!cbargs->silent) 7461 { 7462 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset) 7463 printf (" %*s%-20s (%s) range list [%6" 7464 PRIxMAX "]%s\n", 7465 (int) (level * 2), "", dwarf_attr_name (attr), 7466 dwarf_form_name (form), (uintmax_t) num, 7467 nlpt ? "" : " <WARNING offset too big>"); 7468 else 7469 printf (" %*s%-20s (%s) range index [%6" 7470 PRIxMAX "]\n", 7471 (int) (level * 2), "", dwarf_attr_name (attr), 7472 dwarf_form_name (form), (uintmax_t) num); 7473 } 7474 } 7475 return DWARF_CB_OK; 7476 7477 case DW_AT_rnglists_base: 7478 { 7479 bool nlpt = notice_listptr (section_ranges, &known_rnglistptr, 7480 cbargs->addrsize, cbargs->offset_size, 7481 cbargs->cu, num, attr); 7482 if (!cbargs->silent) 7483 printf (" %*s%-20s (%s) range list [%6" 7484 PRIxMAX "]%s\n", 7485 (int) (level * 2), "", dwarf_attr_name (attr), 7486 dwarf_form_name (form), (uintmax_t) num, 7487 nlpt ? "" : " <WARNING offset too big>"); 7488 } 7489 return DWARF_CB_OK; 7490 7491 case DW_AT_addr_base: 7492 case DW_AT_GNU_addr_base: 7493 { 7494 bool addrbase = notice_listptr (section_addr, &known_addrbases, 7495 cbargs->addrsize, 7496 cbargs->offset_size, 7497 cbargs->cu, num, attr); 7498 if (!cbargs->silent) 7499 printf (" %*s%-20s (%s) address base [%6" 7500 PRIxMAX "]%s\n", 7501 (int) (level * 2), "", dwarf_attr_name (attr), 7502 dwarf_form_name (form), (uintmax_t) num, 7503 addrbase ? "" : " <WARNING offset too big>"); 7504 } 7505 return DWARF_CB_OK; 7506 7507 case DW_AT_str_offsets_base: 7508 { 7509 bool stroffbase = notice_listptr (section_str, &known_stroffbases, 7510 cbargs->addrsize, 7511 cbargs->offset_size, 7512 cbargs->cu, num, attr); 7513 if (!cbargs->silent) 7514 printf (" %*s%-20s (%s) str offsets base [%6" 7515 PRIxMAX "]%s\n", 7516 (int) (level * 2), "", dwarf_attr_name (attr), 7517 dwarf_form_name (form), (uintmax_t) num, 7518 stroffbase ? "" : " <WARNING offset too big>"); 7519 } 7520 return DWARF_CB_OK; 7521 7522 case DW_AT_language: 7523 valuestr = dwarf_lang_name (num); 7524 break; 7525 case DW_AT_encoding: 7526 valuestr = dwarf_encoding_name (num); 7527 break; 7528 case DW_AT_accessibility: 7529 valuestr = dwarf_access_name (num); 7530 break; 7531 case DW_AT_defaulted: 7532 valuestr = dwarf_defaulted_name (num); 7533 break; 7534 case DW_AT_visibility: 7535 valuestr = dwarf_visibility_name (num); 7536 break; 7537 case DW_AT_virtuality: 7538 valuestr = dwarf_virtuality_name (num); 7539 break; 7540 case DW_AT_identifier_case: 7541 valuestr = dwarf_identifier_case_name (num); 7542 break; 7543 case DW_AT_calling_convention: 7544 valuestr = dwarf_calling_convention_name (num); 7545 break; 7546 case DW_AT_inline: 7547 valuestr = dwarf_inline_name (num); 7548 break; 7549 case DW_AT_ordering: 7550 valuestr = dwarf_ordering_name (num); 7551 break; 7552 case DW_AT_decl_file: 7553 case DW_AT_call_file: 7554 { 7555 if (cbargs->silent) 7556 break; 7557 7558 /* Try to get the actual file, the current interface only 7559 gives us full paths, but we only want to show the file 7560 name for now. */ 7561 Dwarf_Die cudie; 7562 if (dwarf_cu_die (cbargs->cu, &cudie, 7563 NULL, NULL, NULL, NULL, NULL, NULL) != NULL) 7564 { 7565 Dwarf_Files *files; 7566 size_t nfiles; 7567 if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0) 7568 { 7569 valuestr = dwarf_filesrc (files, num, NULL, NULL); 7570 if (valuestr != NULL) 7571 { 7572 char *filename = strrchr (valuestr, '/'); 7573 if (filename != NULL) 7574 valuestr = filename + 1; 7575 } 7576 else 7577 error (0, 0, _("invalid file (%" PRId64 "): %s"), 7578 num, dwarf_errmsg (-1)); 7579 } 7580 else 7581 error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"), 7582 dwarf_dieoffset (&cudie)); 7583 } 7584 else 7585 error (0, 0, _("couldn't get DWARF CU: %s"), 7586 dwarf_errmsg (-1)); 7587 if (valuestr == NULL) 7588 valuestr = "???"; 7589 } 7590 break; 7591 case DW_AT_GNU_dwo_id: 7592 as_hex_id = true; 7593 break; 7594 7595 default: 7596 /* Nothing. */ 7597 break; 7598 } 7599 7600 if (cbargs->silent) 7601 break; 7602 7603 /* When highpc is in constant form it is relative to lowpc. 7604 In that case also show the address. */ 7605 Dwarf_Addr highpc; 7606 if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0) 7607 { 7608 printf (" %*s%-20s (%s) %" PRIuMAX " (", 7609 (int) (level * 2), "", dwarf_attr_name (attr), 7610 dwarf_form_name (form), (uintmax_t) num); 7611 print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc); 7612 printf (")\n"); 7613 } 7614 else 7615 { 7616 if (as_hex_id) 7617 { 7618 printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n", 7619 (int) (level * 2), "", dwarf_attr_name (attr), 7620 dwarf_form_name (form), num); 7621 } 7622 else 7623 { 7624 Dwarf_Sword snum = 0; 7625 bool is_signed; 7626 int bytes = 0; 7627 if (attr == DW_AT_const_value) 7628 die_type_sign_bytes (die, &is_signed, &bytes); 7629 else 7630 is_signed = (form == DW_FORM_sdata 7631 || form == DW_FORM_implicit_const); 7632 7633 if (is_signed) 7634 if (unlikely (dwarf_formsdata (attrp, &snum) != 0)) 7635 goto attrval_out; 7636 7637 if (valuestr == NULL) 7638 { 7639 printf (" %*s%-20s (%s) ", 7640 (int) (level * 2), "", dwarf_attr_name (attr), 7641 dwarf_form_name (form)); 7642 } 7643 else 7644 { 7645 printf (" %*s%-20s (%s) %s (", 7646 (int) (level * 2), "", dwarf_attr_name (attr), 7647 dwarf_form_name (form), valuestr); 7648 } 7649 7650 switch (bytes) 7651 { 7652 case 1: 7653 if (is_signed) 7654 printf ("%" PRId8, (int8_t) snum); 7655 else 7656 printf ("%" PRIu8, (uint8_t) num); 7657 break; 7658 7659 case 2: 7660 if (is_signed) 7661 printf ("%" PRId16, (int16_t) snum); 7662 else 7663 printf ("%" PRIu16, (uint16_t) num); 7664 break; 7665 7666 case 4: 7667 if (is_signed) 7668 printf ("%" PRId32, (int32_t) snum); 7669 else 7670 printf ("%" PRIu32, (uint32_t) num); 7671 break; 7672 7673 case 8: 7674 if (is_signed) 7675 printf ("%" PRId64, (int64_t) snum); 7676 else 7677 printf ("%" PRIu64, (uint64_t) num); 7678 break; 7679 7680 default: 7681 if (is_signed) 7682 printf ("%" PRIdMAX, (intmax_t) snum); 7683 else 7684 printf ("%" PRIuMAX, (uintmax_t) num); 7685 break; 7686 } 7687 7688 /* Make clear if we switched from a signed encoding to 7689 an unsigned value. */ 7690 if (attr == DW_AT_const_value 7691 && (form == DW_FORM_sdata || form == DW_FORM_implicit_const) 7692 && !is_signed) 7693 printf (" (%" PRIdMAX ")", (intmax_t) num); 7694 7695 if (valuestr == NULL) 7696 printf ("\n"); 7697 else 7698 printf (")\n"); 7699 } 7700 } 7701 break; 7702 7703 case DW_FORM_flag: 7704 if (cbargs->silent) 7705 break; 7706 bool flag; 7707 if (unlikely (dwarf_formflag (attrp, &flag) != 0)) 7708 goto attrval_out; 7709 7710 printf (" %*s%-20s (%s) %s\n", 7711 (int) (level * 2), "", dwarf_attr_name (attr), 7712 dwarf_form_name (form), flag ? yes_str : no_str); 7713 break; 7714 7715 case DW_FORM_flag_present: 7716 if (cbargs->silent) 7717 break; 7718 printf (" %*s%-20s (%s) %s\n", 7719 (int) (level * 2), "", dwarf_attr_name (attr), 7720 dwarf_form_name (form), yes_str); 7721 break; 7722 7723 case DW_FORM_exprloc: 7724 case DW_FORM_block4: 7725 case DW_FORM_block2: 7726 case DW_FORM_block1: 7727 case DW_FORM_block: 7728 case DW_FORM_data16: /* DWARF5 calls this a constant class. */ 7729 if (cbargs->silent) 7730 break; 7731 Dwarf_Block block; 7732 if (unlikely (dwarf_formblock (attrp, &block) != 0)) 7733 goto attrval_out; 7734 7735 printf (" %*s%-20s (%s) ", 7736 (int) (level * 2), "", dwarf_attr_name (attr), 7737 dwarf_form_name (form)); 7738 7739 switch (attr) 7740 { 7741 default: 7742 if (form != DW_FORM_exprloc) 7743 { 7744 print_block (block.length, block.data); 7745 break; 7746 } 7747 FALLTHROUGH; 7748 7749 case DW_AT_location: 7750 case DW_AT_data_location: 7751 case DW_AT_data_member_location: 7752 case DW_AT_vtable_elem_location: 7753 case DW_AT_string_length: 7754 case DW_AT_use_location: 7755 case DW_AT_frame_base: 7756 case DW_AT_return_addr: 7757 case DW_AT_static_link: 7758 case DW_AT_allocated: 7759 case DW_AT_associated: 7760 case DW_AT_bit_size: 7761 case DW_AT_bit_offset: 7762 case DW_AT_bit_stride: 7763 case DW_AT_byte_size: 7764 case DW_AT_byte_stride: 7765 case DW_AT_count: 7766 case DW_AT_lower_bound: 7767 case DW_AT_upper_bound: 7768 case DW_AT_GNU_call_site_value: 7769 case DW_AT_GNU_call_site_data_value: 7770 case DW_AT_GNU_call_site_target: 7771 case DW_AT_GNU_call_site_target_clobbered: 7772 if (form == DW_FORM_exprloc 7773 || (form != DW_FORM_data16 7774 && attrp->cu->version < 4)) /* blocks were expressions. */ 7775 { 7776 putchar ('\n'); 7777 print_ops (cbargs->dwflmod, cbargs->dbg, 7778 12 + level * 2, 12 + level * 2, 7779 cbargs->version, cbargs->addrsize, cbargs->offset_size, 7780 attrp->cu, block.length, block.data); 7781 } 7782 else 7783 print_block (block.length, block.data); 7784 break; 7785 7786 case DW_AT_discr_list: 7787 if (block.length == 0) 7788 puts ("<default>"); 7789 else if (form != DW_FORM_data16) 7790 { 7791 const unsigned char *readp = block.data; 7792 const unsigned char *readendp = readp + block.length; 7793 7794 /* See if we are dealing with a signed or unsigned 7795 values. If the parent of this variant DIE is a 7796 variant_part then it will either have a discriminant 7797 which points to the member which type is the 7798 discriminant type. Or the variant_part itself has a 7799 type representing the discriminant. */ 7800 bool is_signed = false; 7801 if (level > 0) 7802 { 7803 Dwarf_Die *parent = &cbargs->dies[level - 1]; 7804 if (dwarf_tag (die) == DW_TAG_variant 7805 && dwarf_tag (parent) == DW_TAG_variant_part) 7806 { 7807 Dwarf_Die member; 7808 Dwarf_Attribute discr_attr; 7809 int bytes; 7810 if (dwarf_formref_die (dwarf_attr (parent, 7811 DW_AT_discr, 7812 &discr_attr), 7813 &member) != NULL) 7814 die_type_sign_bytes (&member, &is_signed, &bytes); 7815 else 7816 die_type_sign_bytes (parent, &is_signed, &bytes); 7817 } 7818 } 7819 while (readp < readendp) 7820 { 7821 int d = (int) *readp++; 7822 printf ("%s ", dwarf_discr_list_name (d)); 7823 if (readp >= readendp) 7824 goto attrval_out; 7825 7826 Dwarf_Word val; 7827 Dwarf_Sword sval; 7828 if (d == DW_DSC_label) 7829 { 7830 if (is_signed) 7831 { 7832 get_sleb128 (sval, readp, readendp); 7833 printf ("%" PRId64 "", sval); 7834 } 7835 else 7836 { 7837 get_uleb128 (val, readp, readendp); 7838 printf ("%" PRIu64 "", val); 7839 } 7840 } 7841 else if (d == DW_DSC_range) 7842 { 7843 if (is_signed) 7844 { 7845 get_sleb128 (sval, readp, readendp); 7846 printf ("%" PRId64 "..", sval); 7847 if (readp >= readendp) 7848 goto attrval_out; 7849 get_sleb128 (sval, readp, readendp); 7850 printf ("%" PRId64 "", sval); 7851 } 7852 else 7853 { 7854 get_uleb128 (val, readp, readendp); 7855 printf ("%" PRIu64 "..", val); 7856 if (readp >= readendp) 7857 goto attrval_out; 7858 get_uleb128 (val, readp, readendp); 7859 printf ("%" PRIu64 "", val); 7860 } 7861 } 7862 else 7863 { 7864 print_block (readendp - readp, readp); 7865 break; 7866 } 7867 if (readp < readendp) 7868 printf (", "); 7869 } 7870 putchar ('\n'); 7871 } 7872 else 7873 print_block (block.length, block.data); 7874 break; 7875 } 7876 break; 7877 7878 default: 7879 if (cbargs->silent) 7880 break; 7881 printf (" %*s%-20s (%s) ???\n", 7882 (int) (level * 2), "", dwarf_attr_name (attr), 7883 dwarf_form_name (form)); 7884 break; 7885 } 7886 7887 return DWARF_CB_OK; 7888} 7889 7890static void 7891print_debug_units (Dwfl_Module *dwflmod, 7892 Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)), 7893 Elf_Scn *scn, GElf_Shdr *shdr, 7894 Dwarf *dbg, bool debug_types) 7895{ 7896 const bool silent = !(print_debug_sections & section_info) && !debug_types; 7897 const char *secname = section_name (ebl, shdr); 7898 7899 if (!silent) 7900 printf (_("\ 7901\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"), 7902 elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset); 7903 7904 /* If the section is empty we don't have to do anything. */ 7905 if (!silent && shdr->sh_size == 0) 7906 return; 7907 7908 int maxdies = 20; 7909 Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die)); 7910 7911 /* New compilation unit. */ 7912 Dwarf_Half version; 7913 7914 Dwarf_Die result; 7915 Dwarf_Off abbroffset; 7916 uint8_t addrsize; 7917 uint8_t offsize; 7918 uint64_t unit_id; 7919 Dwarf_Off subdie_off; 7920 7921 int unit_res; 7922 Dwarf_CU *cu; 7923 Dwarf_CU cu_mem; 7924 uint8_t unit_type; 7925 Dwarf_Die cudie; 7926 7927 /* We cheat a little because we want to see only the CUs from .debug_info 7928 or .debug_types. We know the Dwarf_CU struct layout. Set it up at 7929 the end of .debug_info if we want .debug_types only. Check the returned 7930 Dwarf_CU is still in the expected section. */ 7931 if (debug_types) 7932 { 7933 cu_mem.dbg = dbg; 7934 cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size; 7935 cu_mem.sec_idx = IDX_debug_info; 7936 cu = &cu_mem; 7937 } 7938 else 7939 cu = NULL; 7940 7941 next_cu: 7942 unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type, 7943 &cudie, NULL); 7944 if (unit_res == 1) 7945 goto do_return; 7946 7947 if (unit_res == -1) 7948 { 7949 if (!silent) 7950 error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1)); 7951 goto do_return; 7952 } 7953 7954 if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info)) 7955 goto do_return; 7956 7957 dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize, 7958 &unit_id, &subdie_off); 7959 7960 if (!silent) 7961 { 7962 Dwarf_Off offset = cu->start; 7963 if (debug_types && version < 5) 7964 { 7965 Dwarf_Die typedie; 7966 Dwarf_Off dieoffset; 7967 dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start 7968 + subdie_off, 7969 &typedie)); 7970 printf (_(" Type unit at offset %" PRIu64 ":\n" 7971 " Version: %" PRIu16 7972 ", Abbreviation section offset: %" PRIu64 7973 ", Address size: %" PRIu8 7974 ", Offset size: %" PRIu8 7975 "\n Type signature: %#" PRIx64 7976 ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"), 7977 (uint64_t) offset, version, abbroffset, addrsize, offsize, 7978 unit_id, (uint64_t) subdie_off, dieoffset); 7979 } 7980 else 7981 { 7982 printf (_(" Compilation unit at offset %" PRIu64 ":\n" 7983 " Version: %" PRIu16 7984 ", Abbreviation section offset: %" PRIu64 7985 ", Address size: %" PRIu8 7986 ", Offset size: %" PRIu8 "\n"), 7987 (uint64_t) offset, version, abbroffset, addrsize, offsize); 7988 7989 if (version >= 5 || (unit_type != DW_UT_compile 7990 && unit_type != DW_UT_partial)) 7991 { 7992 printf (_(" Unit type: %s (%" PRIu8 ")"), 7993 dwarf_unit_name (unit_type), unit_type); 7994 if (unit_type == DW_UT_type 7995 || unit_type == DW_UT_skeleton 7996 || unit_type == DW_UT_split_compile 7997 || unit_type == DW_UT_split_type) 7998 printf (", Unit id: 0x%.16" PRIx64 "", unit_id); 7999 if (unit_type == DW_UT_type 8000 || unit_type == DW_UT_split_type) 8001 { 8002 Dwarf_Die typedie; 8003 Dwarf_Off dieoffset; 8004 dwarf_cu_info (cu, NULL, NULL, NULL, &typedie, 8005 NULL, NULL, NULL); 8006 dieoffset = dwarf_dieoffset (&typedie); 8007 printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]", 8008 subdie_off, dieoffset); 8009 } 8010 printf ("\n"); 8011 } 8012 } 8013 } 8014 8015 if (version < 2 || version > 5 8016 || unit_type < DW_UT_compile || unit_type > DW_UT_split_type) 8017 { 8018 if (!silent) 8019 error (0, 0, _("unknown version (%d) or unit type (%d)"), 8020 version, unit_type); 8021 goto next_cu; 8022 } 8023 8024 struct attrcb_args args = 8025 { 8026 .dwflmod = dwflmod, 8027 .silent = silent, 8028 .version = version, 8029 .addrsize = addrsize, 8030 .offset_size = offsize 8031 }; 8032 8033 bool is_split = false; 8034 int level = 0; 8035 dies[0] = cudie; 8036 args.cu = dies[0].cu; 8037 args.dbg = dbg; 8038 args.is_split = is_split; 8039 8040 /* We might return here again for the split CU subdie. */ 8041 do_cu: 8042 do 8043 { 8044 Dwarf_Off offset = dwarf_dieoffset (&dies[level]); 8045 if (unlikely (offset == (Dwarf_Off) -1)) 8046 { 8047 if (!silent) 8048 error (0, 0, _("cannot get DIE offset: %s"), 8049 dwarf_errmsg (-1)); 8050 goto do_return; 8051 } 8052 8053 int tag = dwarf_tag (&dies[level]); 8054 if (unlikely (tag == DW_TAG_invalid)) 8055 { 8056 if (!silent) 8057 error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64 8058 "] in section '%s': %s"), 8059 (uint64_t) offset, secname, dwarf_errmsg (-1)); 8060 goto do_return; 8061 } 8062 8063 if (!silent) 8064 { 8065 unsigned int code = dwarf_getabbrevcode (dies[level].abbrev); 8066 if (is_split) 8067 printf (" {%6" PRIx64 "} ", (uint64_t) offset); 8068 else 8069 printf (" [%6" PRIx64 "] ", (uint64_t) offset); 8070 printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "", 8071 dwarf_tag_name (tag), code); 8072 } 8073 8074 /* Print the attribute values. */ 8075 args.level = level; 8076 args.dies = dies; 8077 (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0); 8078 8079 /* Make room for the next level's DIE. */ 8080 if (level + 1 == maxdies) 8081 dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die)); 8082 8083 int res = dwarf_child (&dies[level], &dies[level + 1]); 8084 if (res > 0) 8085 { 8086 while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1) 8087 if (level-- == 0) 8088 break; 8089 8090 if (unlikely (res == -1)) 8091 { 8092 if (!silent) 8093 error (0, 0, _("cannot get next DIE: %s\n"), 8094 dwarf_errmsg (-1)); 8095 goto do_return; 8096 } 8097 } 8098 else if (unlikely (res < 0)) 8099 { 8100 if (!silent) 8101 error (0, 0, _("cannot get next DIE: %s"), 8102 dwarf_errmsg (-1)); 8103 goto do_return; 8104 } 8105 else 8106 ++level; 8107 } 8108 while (level >= 0); 8109 8110 /* We might want to show the split compile unit if this was a skeleton. 8111 We need to scan it if we are requesting printing .debug_ranges for 8112 DWARF4 since GNU DebugFission uses "offsets" into the main ranges 8113 section. */ 8114 if (unit_type == DW_UT_skeleton 8115 && ((!silent && show_split_units) 8116 || (version < 5 && (print_debug_sections & section_ranges) != 0))) 8117 { 8118 Dwarf_Die subdie; 8119 if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0 8120 || dwarf_tag (&subdie) == DW_TAG_invalid) 8121 { 8122 if (!silent) 8123 { 8124 Dwarf_Attribute dwo_at; 8125 const char *dwo_name = 8126 (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name, 8127 &dwo_at)) 8128 ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name, 8129 &dwo_at)) 8130 ?: "<unknown>")); 8131 fprintf (stderr, 8132 "Could not find split unit '%s', id: %" PRIx64 "\n", 8133 dwo_name, unit_id); 8134 } 8135 } 8136 else 8137 { 8138 Dwarf_CU *split_cu = subdie.cu; 8139 dwarf_cu_die (split_cu, &result, NULL, &abbroffset, 8140 &addrsize, &offsize, &unit_id, &subdie_off); 8141 Dwarf_Off offset = cu->start; 8142 8143 if (!silent) 8144 { 8145 printf (_(" Split compilation unit at offset %" 8146 PRIu64 ":\n" 8147 " Version: %" PRIu16 8148 ", Abbreviation section offset: %" PRIu64 8149 ", Address size: %" PRIu8 8150 ", Offset size: %" PRIu8 "\n"), 8151 (uint64_t) offset, version, abbroffset, 8152 addrsize, offsize); 8153 printf (_(" Unit type: %s (%" PRIu8 ")"), 8154 dwarf_unit_name (unit_type), unit_type); 8155 printf (", Unit id: 0x%.16" PRIx64 "", unit_id); 8156 printf ("\n"); 8157 } 8158 8159 unit_type = DW_UT_split_compile; 8160 is_split = true; 8161 level = 0; 8162 dies[0] = subdie; 8163 args.cu = dies[0].cu; 8164 args.dbg = split_cu->dbg; 8165 args.is_split = is_split; 8166 goto do_cu; 8167 } 8168 } 8169 8170 /* And again... */ 8171 goto next_cu; 8172 8173 do_return: 8174 free (dies); 8175} 8176 8177static void 8178print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, 8179 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 8180{ 8181 print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false); 8182} 8183 8184static void 8185print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, 8186 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 8187{ 8188 print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true); 8189} 8190 8191 8192static void 8193print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl, 8194 GElf_Ehdr *ehdr __attribute__ ((unused)), 8195 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 8196{ 8197 printf (_("\ 8198\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"), 8199 elf_ndxscn (scn), section_name (ebl, shdr), 8200 (uint64_t) shdr->sh_offset); 8201 8202 size_t address_size 8203 = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8; 8204 8205 Dwarf_Lines *lines; 8206 size_t nlines; 8207 Dwarf_Off off, next_off = 0; 8208 Dwarf_CU *cu = NULL; 8209 while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL, 8210 &lines, &nlines) == 0) 8211 { 8212 Dwarf_Die cudie; 8213 if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie, 8214 NULL, NULL, NULL, NULL) == 0) 8215 printf (" CU [%" PRIx64 "] %s\n", 8216 dwarf_dieoffset (&cudie), dwarf_diename (&cudie)); 8217 else 8218 { 8219 /* DWARF5 lines can be independent of any CU, but they probably 8220 are used by some CU. Determine the CU this block is for. */ 8221 Dwarf_Off cuoffset; 8222 Dwarf_Off ncuoffset = 0; 8223 size_t hsize; 8224 while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize, 8225 NULL, NULL, NULL) == 0) 8226 { 8227 if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL) 8228 continue; 8229 Dwarf_Attribute stmt_list; 8230 if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL) 8231 continue; 8232 Dwarf_Word lineoff; 8233 if (dwarf_formudata (&stmt_list, &lineoff) != 0) 8234 continue; 8235 if (lineoff == off) 8236 { 8237 /* Found the CU. */ 8238 cu = cudie.cu; 8239 break; 8240 } 8241 } 8242 8243 if (cu != NULL) 8244 printf (" CU [%" PRIx64 "] %s\n", 8245 dwarf_dieoffset (&cudie), dwarf_diename (&cudie)); 8246 else 8247 printf (" No CU\n"); 8248 } 8249 8250 printf (" line:col SBPE* disc isa op address" 8251 " (Statement Block Prologue Epilogue *End)\n"); 8252 const char *last_file = ""; 8253 for (size_t n = 0; n < nlines; n++) 8254 { 8255 Dwarf_Line *line = dwarf_onesrcline (lines, n); 8256 if (line == NULL) 8257 { 8258 printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1)); 8259 continue; 8260 } 8261 Dwarf_Word mtime, length; 8262 const char *file = dwarf_linesrc (line, &mtime, &length); 8263 if (file == NULL) 8264 { 8265 printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1)); 8266 last_file = ""; 8267 } 8268 else if (strcmp (last_file, file) != 0) 8269 { 8270 printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n", 8271 file, mtime, length); 8272 last_file = file; 8273 } 8274 8275 int lineno, colno; 8276 bool statement, endseq, block, prologue_end, epilogue_begin; 8277 unsigned int lineop, isa, disc; 8278 Dwarf_Addr address; 8279 dwarf_lineaddr (line, &address); 8280 dwarf_lineno (line, &lineno); 8281 dwarf_linecol (line, &colno); 8282 dwarf_lineop_index (line, &lineop); 8283 dwarf_linebeginstatement (line, &statement); 8284 dwarf_lineendsequence (line, &endseq); 8285 dwarf_lineblock (line, &block); 8286 dwarf_lineprologueend (line, &prologue_end); 8287 dwarf_lineepiloguebegin (line, &epilogue_begin); 8288 dwarf_lineisa (line, &isa); 8289 dwarf_linediscriminator (line, &disc); 8290 8291 /* End sequence is special, it is one byte past. */ 8292 printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ", 8293 lineno, colno, 8294 (statement ? 'S' : ' '), 8295 (block ? 'B' : ' '), 8296 (prologue_end ? 'P' : ' '), 8297 (epilogue_begin ? 'E' : ' '), 8298 (endseq ? '*' : ' '), 8299 disc, isa, lineop); 8300 print_dwarf_addr (dwflmod, address_size, 8301 address - (endseq ? 1 : 0), address); 8302 printf ("\n"); 8303 8304 if (endseq) 8305 printf("\n"); 8306 } 8307 } 8308} 8309 8310 8311/* Print the value of a form. 8312 Returns new value of readp, or readendp on failure. */ 8313static const unsigned char * 8314print_form_data (Dwarf *dbg, int form, const unsigned char *readp, 8315 const unsigned char *readendp, unsigned int offset_len, 8316 Dwarf_Off str_offsets_base) 8317{ 8318 Dwarf_Word val; 8319 unsigned char *endp; 8320 Elf_Data *data; 8321 char *str; 8322 switch (form) 8323 { 8324 case DW_FORM_data1: 8325 if (readendp - readp < 1) 8326 { 8327 invalid_data: 8328 error (0, 0, "invalid data"); 8329 return readendp; 8330 } 8331 val = *readp++; 8332 printf (" %" PRIx8, (unsigned int) val); 8333 break; 8334 8335 case DW_FORM_data2: 8336 if (readendp - readp < 2) 8337 goto invalid_data; 8338 val = read_2ubyte_unaligned_inc (dbg, readp); 8339 printf(" %" PRIx16, (unsigned int) val); 8340 break; 8341 8342 case DW_FORM_data4: 8343 if (readendp - readp < 4) 8344 goto invalid_data; 8345 val = read_4ubyte_unaligned_inc (dbg, readp); 8346 printf (" %" PRIx32, (unsigned int) val); 8347 break; 8348 8349 case DW_FORM_data8: 8350 if (readendp - readp < 8) 8351 goto invalid_data; 8352 val = read_8ubyte_unaligned_inc (dbg, readp); 8353 printf (" %" PRIx64, val); 8354 break; 8355 8356 case DW_FORM_sdata: 8357 if (readendp - readp < 1) 8358 goto invalid_data; 8359 get_sleb128 (val, readp, readendp); 8360 printf (" %" PRIx64, val); 8361 break; 8362 8363 case DW_FORM_udata: 8364 if (readendp - readp < 1) 8365 goto invalid_data; 8366 get_uleb128 (val, readp, readendp); 8367 printf (" %" PRIx64, val); 8368 break; 8369 8370 case DW_FORM_block: 8371 if (readendp - readp < 1) 8372 goto invalid_data; 8373 get_uleb128 (val, readp, readendp); 8374 if ((size_t) (readendp - readp) < val) 8375 goto invalid_data; 8376 print_bytes (val, readp); 8377 readp += val; 8378 break; 8379 8380 case DW_FORM_block1: 8381 if (readendp - readp < 1) 8382 goto invalid_data; 8383 val = *readp++; 8384 if ((size_t) (readendp - readp) < val) 8385 goto invalid_data; 8386 print_bytes (val, readp); 8387 readp += val; 8388 break; 8389 8390 case DW_FORM_block2: 8391 if (readendp - readp < 2) 8392 goto invalid_data; 8393 val = read_2ubyte_unaligned_inc (dbg, readp); 8394 if ((size_t) (readendp - readp) < val) 8395 goto invalid_data; 8396 print_bytes (val, readp); 8397 readp += val; 8398 break; 8399 8400 case DW_FORM_block4: 8401 if (readendp - readp < 4) 8402 goto invalid_data; 8403 val = read_4ubyte_unaligned_inc (dbg, readp); 8404 if ((size_t) (readendp - readp) < val) 8405 goto invalid_data; 8406 print_bytes (val, readp); 8407 readp += val; 8408 break; 8409 8410 case DW_FORM_data16: 8411 if (readendp - readp < 16) 8412 goto invalid_data; 8413 print_bytes (16, readp); 8414 readp += 16; 8415 break; 8416 8417 case DW_FORM_flag: 8418 if (readendp - readp < 1) 8419 goto invalid_data; 8420 val = *readp++; 8421 printf ("%s", val != 0 ? yes_str : no_str); 8422 break; 8423 8424 case DW_FORM_string: 8425 endp = memchr (readp, '\0', readendp - readp); 8426 if (endp == NULL) 8427 goto invalid_data; 8428 printf ("%s", readp); 8429 readp = endp + 1; 8430 break; 8431 8432 case DW_FORM_strp: 8433 case DW_FORM_line_strp: 8434 case DW_FORM_strp_sup: 8435 if ((size_t) (readendp - readp) < offset_len) 8436 goto invalid_data; 8437 if (offset_len == 8) 8438 val = read_8ubyte_unaligned_inc (dbg, readp); 8439 else 8440 val = read_4ubyte_unaligned_inc (dbg, readp); 8441 if (form == DW_FORM_strp) 8442 data = dbg->sectiondata[IDX_debug_str]; 8443 else if (form == DW_FORM_line_strp) 8444 data = dbg->sectiondata[IDX_debug_line_str]; 8445 else /* form == DW_FORM_strp_sup */ 8446 { 8447 Dwarf *alt = dwarf_getalt (dbg); 8448 data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL; 8449 } 8450 if (data == NULL || val >= data->d_size 8451 || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL) 8452 str = "???"; 8453 else 8454 str = (char *) data->d_buf + val; 8455 printf ("%s (%" PRIu64 ")", str, val); 8456 break; 8457 8458 case DW_FORM_sec_offset: 8459 if ((size_t) (readendp - readp) < offset_len) 8460 goto invalid_data; 8461 if (offset_len == 8) 8462 val = read_8ubyte_unaligned_inc (dbg, readp); 8463 else 8464 val = read_4ubyte_unaligned_inc (dbg, readp); 8465 printf ("[%" PRIx64 "]", val); 8466 break; 8467 8468 case DW_FORM_strx: 8469 case DW_FORM_GNU_str_index: 8470 if (readendp - readp < 1) 8471 goto invalid_data; 8472 get_uleb128 (val, readp, readendp); 8473 strx_val: 8474 data = dbg->sectiondata[IDX_debug_str_offsets]; 8475 if (data == NULL 8476 || data->d_size - str_offsets_base < val) 8477 str = "???"; 8478 else 8479 { 8480 const unsigned char *strreadp = data->d_buf + str_offsets_base + val; 8481 const unsigned char *strreadendp = data->d_buf + data->d_size; 8482 if ((size_t) (strreadendp - strreadp) < offset_len) 8483 str = "???"; 8484 else 8485 { 8486 Dwarf_Off idx; 8487 if (offset_len == 8) 8488 idx = read_8ubyte_unaligned (dbg, strreadp); 8489 else 8490 idx = read_4ubyte_unaligned (dbg, strreadp); 8491 8492 data = dbg->sectiondata[IDX_debug_str]; 8493 if (data == NULL || idx >= data->d_size 8494 || memchr (data->d_buf + idx, '\0', 8495 data->d_size - idx) == NULL) 8496 str = "???"; 8497 else 8498 str = (char *) data->d_buf + idx; 8499 } 8500 } 8501 printf ("%s (%" PRIu64 ")", str, val); 8502 break; 8503 8504 case DW_FORM_strx1: 8505 if (readendp - readp < 1) 8506 goto invalid_data; 8507 val = *readp++; 8508 goto strx_val; 8509 8510 case DW_FORM_strx2: 8511 if (readendp - readp < 2) 8512 goto invalid_data; 8513 val = read_2ubyte_unaligned_inc (dbg, readp); 8514 goto strx_val; 8515 8516 case DW_FORM_strx3: 8517 if (readendp - readp < 3) 8518 goto invalid_data; 8519 val = read_3ubyte_unaligned_inc (dbg, readp); 8520 goto strx_val; 8521 8522 case DW_FORM_strx4: 8523 if (readendp - readp < 4) 8524 goto invalid_data; 8525 val = read_4ubyte_unaligned_inc (dbg, readp); 8526 goto strx_val; 8527 8528 default: 8529 error (0, 0, _("unknown form: %s"), dwarf_form_name (form)); 8530 return readendp; 8531 } 8532 8533 return readp; 8534} 8535 8536/* Only used via run_advance_pc() macro */ 8537static inline void 8538run_advance_pc (unsigned int op_advance, 8539 unsigned int minimum_instr_len, 8540 unsigned int max_ops_per_instr, 8541 unsigned int *op_addr_advance, 8542 Dwarf_Word *address, 8543 unsigned int *op_index) 8544{ 8545 const unsigned int advanced_op_index = (*op_index) + op_advance; 8546 8547 *op_addr_advance = minimum_instr_len * (advanced_op_index 8548 / max_ops_per_instr); 8549 *address = *address + *op_addr_advance; 8550 *op_index = advanced_op_index % max_ops_per_instr; 8551} 8552 8553static void 8554print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, 8555 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 8556{ 8557 if (decodedline) 8558 { 8559 print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg); 8560 return; 8561 } 8562 8563 printf (_("\ 8564\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 8565 elf_ndxscn (scn), section_name (ebl, shdr), 8566 (uint64_t) shdr->sh_offset); 8567 8568 if (shdr->sh_size == 0) 8569 return; 8570 8571 /* There is no functionality in libdw to read the information in the 8572 way it is represented here. Hardcode the decoder. */ 8573 Elf_Data *data = (dbg->sectiondata[IDX_debug_line] 8574 ?: elf_rawdata (scn, NULL)); 8575 if (unlikely (data == NULL)) 8576 { 8577 error (0, 0, _("cannot get line data section data: %s"), 8578 elf_errmsg (-1)); 8579 return; 8580 } 8581 8582 const unsigned char *linep = (const unsigned char *) data->d_buf; 8583 const unsigned char *lineendp; 8584 8585 while (linep 8586 < (lineendp = (const unsigned char *) data->d_buf + data->d_size)) 8587 { 8588 size_t start_offset = linep - (const unsigned char *) data->d_buf; 8589 8590 printf (_("\nTable at offset %zu:\n"), start_offset); 8591 8592 if (unlikely (linep + 4 > lineendp)) 8593 goto invalid_data; 8594 Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep); 8595 unsigned int length = 4; 8596 if (unlikely (unit_length == 0xffffffff)) 8597 { 8598 if (unlikely (linep + 8 > lineendp)) 8599 { 8600 invalid_data: 8601 error (0, 0, _("invalid data in section [%zu] '%s'"), 8602 elf_ndxscn (scn), section_name (ebl, shdr)); 8603 return; 8604 } 8605 unit_length = read_8ubyte_unaligned_inc (dbg, linep); 8606 length = 8; 8607 } 8608 8609 /* Check whether we have enough room in the section. */ 8610 if (unlikely (unit_length > (size_t) (lineendp - linep))) 8611 goto invalid_data; 8612 lineendp = linep + unit_length; 8613 8614 /* The next element of the header is the version identifier. */ 8615 if ((size_t) (lineendp - linep) < 2) 8616 goto invalid_data; 8617 uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep); 8618 8619 size_t address_size 8620 = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8; 8621 unsigned char segment_selector_size = 0; 8622 if (version > 4) 8623 { 8624 if ((size_t) (lineendp - linep) < 2) 8625 goto invalid_data; 8626 address_size = *linep++; 8627 segment_selector_size = *linep++; 8628 } 8629 8630 /* Next comes the header length. */ 8631 Dwarf_Word header_length; 8632 if (length == 4) 8633 { 8634 if ((size_t) (lineendp - linep) < 4) 8635 goto invalid_data; 8636 header_length = read_4ubyte_unaligned_inc (dbg, linep); 8637 } 8638 else 8639 { 8640 if ((size_t) (lineendp - linep) < 8) 8641 goto invalid_data; 8642 header_length = read_8ubyte_unaligned_inc (dbg, linep); 8643 } 8644 8645 const unsigned char *header_start = linep; 8646 8647 /* Next the minimum instruction length. */ 8648 if ((size_t) (lineendp - linep) < 1) 8649 goto invalid_data; 8650 uint_fast8_t minimum_instr_len = *linep++; 8651 8652 /* Next the maximum operations per instruction, in version 4 format. */ 8653 uint_fast8_t max_ops_per_instr; 8654 if (version < 4) 8655 max_ops_per_instr = 1; 8656 else 8657 { 8658 if ((size_t) (lineendp - linep) < 1) 8659 goto invalid_data; 8660 max_ops_per_instr = *linep++; 8661 } 8662 8663 /* We need at least 4 more bytes. */ 8664 if ((size_t) (lineendp - linep) < 4) 8665 goto invalid_data; 8666 8667 /* Then the flag determining the default value of the is_stmt 8668 register. */ 8669 uint_fast8_t default_is_stmt = *linep++; 8670 8671 /* Now the line base. */ 8672 int_fast8_t line_base = *linep++; 8673 8674 /* And the line range. */ 8675 uint_fast8_t line_range = *linep++; 8676 8677 /* The opcode base. */ 8678 uint_fast8_t opcode_base = *linep++; 8679 8680 /* Print what we got so far. */ 8681 printf (_("\n" 8682 " Length: %" PRIu64 "\n" 8683 " DWARF version: %" PRIuFAST16 "\n" 8684 " Prologue length: %" PRIu64 "\n" 8685 " Address size: %zd\n" 8686 " Segment selector size: %zd\n" 8687 " Min instruction length: %" PRIuFAST8 "\n" 8688 " Max operations per instruction: %" PRIuFAST8 "\n" 8689 " Initial value if 'is_stmt': %" PRIuFAST8 "\n" 8690 " Line base: %" PRIdFAST8 "\n" 8691 " Line range: %" PRIuFAST8 "\n" 8692 " Opcode base: %" PRIuFAST8 "\n" 8693 "\n" 8694 "Opcodes:\n"), 8695 (uint64_t) unit_length, version, (uint64_t) header_length, 8696 address_size, (size_t) segment_selector_size, 8697 minimum_instr_len, max_ops_per_instr, 8698 default_is_stmt, line_base, 8699 line_range, opcode_base); 8700 8701 if (version < 2 || version > 5) 8702 { 8703 error (0, 0, _("cannot handle .debug_line version: %u\n"), 8704 (unsigned int) version); 8705 linep = lineendp; 8706 continue; 8707 } 8708 8709 if (address_size != 4 && address_size != 8) 8710 { 8711 error (0, 0, _("cannot handle address size: %u\n"), 8712 (unsigned int) address_size); 8713 linep = lineendp; 8714 continue; 8715 } 8716 8717 if (segment_selector_size != 0) 8718 { 8719 error (0, 0, _("cannot handle segment selector size: %u\n"), 8720 (unsigned int) segment_selector_size); 8721 linep = lineendp; 8722 continue; 8723 } 8724 8725 if (unlikely (linep + opcode_base - 1 >= lineendp)) 8726 { 8727 invalid_unit: 8728 error (0, 0, 8729 _("invalid data at offset %tu in section [%zu] '%s'"), 8730 linep - (const unsigned char *) data->d_buf, 8731 elf_ndxscn (scn), section_name (ebl, shdr)); 8732 linep = lineendp; 8733 continue; 8734 } 8735 int opcode_base_l10 = 1; 8736 unsigned int tmp = opcode_base; 8737 while (tmp > 10) 8738 { 8739 tmp /= 10; 8740 ++opcode_base_l10; 8741 } 8742 const uint8_t *standard_opcode_lengths = linep - 1; 8743 for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt) 8744 printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n", 8745 " [%*" PRIuFAST8 "] %hhu arguments\n", 8746 (int) linep[cnt - 1]), 8747 opcode_base_l10, cnt, linep[cnt - 1]); 8748 linep += opcode_base - 1; 8749 8750 if (unlikely (linep >= lineendp)) 8751 goto invalid_unit; 8752 8753 Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL); 8754 8755 puts (_("\nDirectory table:")); 8756 if (version > 4) 8757 { 8758 struct encpair { uint16_t desc; uint16_t form; }; 8759 struct encpair enc[256]; 8760 8761 printf (_(" [")); 8762 if ((size_t) (lineendp - linep) < 1) 8763 goto invalid_data; 8764 unsigned char directory_entry_format_count = *linep++; 8765 for (int i = 0; i < directory_entry_format_count; i++) 8766 { 8767 uint16_t desc, form; 8768 if ((size_t) (lineendp - linep) < 1) 8769 goto invalid_data; 8770 get_uleb128 (desc, linep, lineendp); 8771 if ((size_t) (lineendp - linep) < 1) 8772 goto invalid_data; 8773 get_uleb128 (form, linep, lineendp); 8774 8775 enc[i].desc = desc; 8776 enc[i].form = form; 8777 8778 printf ("%s(%s)", 8779 dwarf_line_content_description_name (desc), 8780 dwarf_form_name (form)); 8781 if (i + 1 < directory_entry_format_count) 8782 printf (", "); 8783 } 8784 printf ("]\n"); 8785 8786 uint64_t directories_count; 8787 if ((size_t) (lineendp - linep) < 1) 8788 goto invalid_data; 8789 get_uleb128 (directories_count, linep, lineendp); 8790 8791 if (directory_entry_format_count == 0 8792 && directories_count != 0) 8793 goto invalid_data; 8794 8795 for (uint64_t i = 0; i < directories_count; i++) 8796 { 8797 printf (" %-5" PRIu64 " ", i); 8798 for (int j = 0; j < directory_entry_format_count; j++) 8799 { 8800 linep = print_form_data (dbg, enc[j].form, 8801 linep, lineendp, length, 8802 str_offsets_base); 8803 if (j + 1 < directory_entry_format_count) 8804 printf (", "); 8805 } 8806 printf ("\n"); 8807 if (linep >= lineendp) 8808 goto invalid_unit; 8809 } 8810 } 8811 else 8812 { 8813 while (linep < lineendp && *linep != 0) 8814 { 8815 unsigned char *endp = memchr (linep, '\0', lineendp - linep); 8816 if (unlikely (endp == NULL)) 8817 goto invalid_unit; 8818 8819 printf (" %s\n", (char *) linep); 8820 8821 linep = endp + 1; 8822 } 8823 if (linep >= lineendp || *linep != 0) 8824 goto invalid_unit; 8825 /* Skip the final NUL byte. */ 8826 ++linep; 8827 } 8828 8829 if (unlikely (linep >= lineendp)) 8830 goto invalid_unit; 8831 8832 puts (_("\nFile name table:")); 8833 if (version > 4) 8834 { 8835 struct encpair { uint16_t desc; uint16_t form; }; 8836 struct encpair enc[256]; 8837 8838 printf (_(" [")); 8839 if ((size_t) (lineendp - linep) < 1) 8840 goto invalid_data; 8841 unsigned char file_name_format_count = *linep++; 8842 for (int i = 0; i < file_name_format_count; i++) 8843 { 8844 uint64_t desc, form; 8845 if ((size_t) (lineendp - linep) < 1) 8846 goto invalid_data; 8847 get_uleb128 (desc, linep, lineendp); 8848 if ((size_t) (lineendp - linep) < 1) 8849 goto invalid_data; 8850 get_uleb128 (form, linep, lineendp); 8851 8852 if (! libdw_valid_user_form (form)) 8853 goto invalid_data; 8854 8855 enc[i].desc = desc; 8856 enc[i].form = form; 8857 8858 printf ("%s(%s)", 8859 dwarf_line_content_description_name (desc), 8860 dwarf_form_name (form)); 8861 if (i + 1 < file_name_format_count) 8862 printf (", "); 8863 } 8864 printf ("]\n"); 8865 8866 uint64_t file_name_count; 8867 if ((size_t) (lineendp - linep) < 1) 8868 goto invalid_data; 8869 get_uleb128 (file_name_count, linep, lineendp); 8870 8871 if (file_name_format_count == 0 8872 && file_name_count != 0) 8873 goto invalid_data; 8874 8875 for (uint64_t i = 0; i < file_name_count; i++) 8876 { 8877 printf (" %-5" PRIu64 " ", i); 8878 for (int j = 0; j < file_name_format_count; j++) 8879 { 8880 linep = print_form_data (dbg, enc[j].form, 8881 linep, lineendp, length, 8882 str_offsets_base); 8883 if (j + 1 < file_name_format_count) 8884 printf (", "); 8885 } 8886 printf ("\n"); 8887 if (linep > lineendp) 8888 goto invalid_unit; 8889 } 8890 } 8891 else 8892 { 8893 puts (_(" Entry Dir Time Size Name")); 8894 for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt) 8895 { 8896 /* First comes the file name. */ 8897 char *fname = (char *) linep; 8898 unsigned char *endp = memchr (fname, '\0', lineendp - linep); 8899 if (unlikely (endp == NULL)) 8900 goto invalid_unit; 8901 linep = endp + 1; 8902 8903 /* Then the index. */ 8904 unsigned int diridx; 8905 if (lineendp - linep < 1) 8906 goto invalid_unit; 8907 get_uleb128 (diridx, linep, lineendp); 8908 8909 /* Next comes the modification time. */ 8910 unsigned int mtime; 8911 if (lineendp - linep < 1) 8912 goto invalid_unit; 8913 get_uleb128 (mtime, linep, lineendp); 8914 8915 /* Finally the length of the file. */ 8916 unsigned int fsize; 8917 if (lineendp - linep < 1) 8918 goto invalid_unit; 8919 get_uleb128 (fsize, linep, lineendp); 8920 8921 printf (" %-5u %-5u %-9u %-9u %s\n", 8922 cnt, diridx, mtime, fsize, fname); 8923 } 8924 if (linep >= lineendp || *linep != '\0') 8925 goto invalid_unit; 8926 /* Skip the final NUL byte. */ 8927 ++linep; 8928 } 8929 8930 unsigned int debug_str_offset = 0; 8931 if (unlikely (linep == header_start + header_length - 4)) 8932 { 8933 /* CUBINs contain an unsigned 4-byte offset */ 8934 debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep); 8935 } 8936 8937 if (linep == lineendp) 8938 { 8939 puts (_("\nNo line number statements.")); 8940 continue; 8941 } 8942 8943 puts (_("\nLine number statements:")); 8944 Dwarf_Word address = 0; 8945 unsigned int op_index = 0; 8946 size_t line = 1; 8947 uint_fast8_t is_stmt = default_is_stmt; 8948 8949 /* Apply the "operation advance" from a special opcode 8950 or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */ 8951 unsigned int op_addr_advance; 8952#define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \ 8953 max_ops_per_instr, &op_addr_advance, &address, &op_index) 8954 8955 if (max_ops_per_instr == 0) 8956 { 8957 error (0, 0, 8958 _("invalid maximum operations per instruction is zero")); 8959 linep = lineendp; 8960 continue; 8961 } 8962 8963 while (linep < lineendp) 8964 { 8965 size_t offset = linep - (const unsigned char *) data->d_buf; 8966 unsigned int u128; 8967 int s128; 8968 8969 /* Read the opcode. */ 8970 unsigned int opcode = *linep++; 8971 8972 printf (" [%6" PRIx64 "]", (uint64_t)offset); 8973 /* Is this a special opcode? */ 8974 if (likely (opcode >= opcode_base)) 8975 { 8976 if (unlikely (line_range == 0)) 8977 goto invalid_unit; 8978 8979 /* Yes. Handling this is quite easy since the opcode value 8980 is computed with 8981 8982 opcode = (desired line increment - line_base) 8983 + (line_range * address advance) + opcode_base 8984 */ 8985 int line_increment = (line_base 8986 + (opcode - opcode_base) % line_range); 8987 8988 /* Perform the increments. */ 8989 line += line_increment; 8990 advance_pc ((opcode - opcode_base) / line_range); 8991 8992 printf (_(" special opcode %u: address+%u = "), 8993 opcode, op_addr_advance); 8994 print_dwarf_addr (dwflmod, 0, address, address); 8995 if (op_index > 0) 8996 printf (_(", op_index = %u, line%+d = %zu\n"), 8997 op_index, line_increment, line); 8998 else 8999 printf (_(", line%+d = %zu\n"), 9000 line_increment, line); 9001 } 9002 else if (opcode == 0) 9003 { 9004 /* This an extended opcode. */ 9005 if (unlikely (linep + 2 > lineendp)) 9006 goto invalid_unit; 9007 9008 /* The length. */ 9009 unsigned int len = *linep++; 9010 9011 if (unlikely (linep + len > lineendp)) 9012 goto invalid_unit; 9013 9014 /* The sub-opcode. */ 9015 opcode = *linep++; 9016 9017 printf (_(" extended opcode %u: "), opcode); 9018 9019 switch (opcode) 9020 { 9021 case DW_LNE_end_sequence: 9022 puts (_(" end of sequence")); 9023 9024 /* Reset the registers we care about. */ 9025 address = 0; 9026 op_index = 0; 9027 line = 1; 9028 is_stmt = default_is_stmt; 9029 break; 9030 9031 case DW_LNE_set_address: 9032 op_index = 0; 9033 if (unlikely ((size_t) (lineendp - linep) < address_size)) 9034 goto invalid_unit; 9035 if (address_size == 4) 9036 address = read_4ubyte_unaligned_inc (dbg, linep); 9037 else 9038 address = read_8ubyte_unaligned_inc (dbg, linep); 9039 { 9040 printf (_(" set address to ")); 9041 print_dwarf_addr (dwflmod, 0, address, address); 9042 printf ("\n"); 9043 } 9044 break; 9045 9046 case DW_LNE_define_file: 9047 { 9048 char *fname = (char *) linep; 9049 unsigned char *endp = memchr (linep, '\0', 9050 lineendp - linep); 9051 if (unlikely (endp == NULL)) 9052 goto invalid_unit; 9053 linep = endp + 1; 9054 9055 unsigned int diridx; 9056 if (lineendp - linep < 1) 9057 goto invalid_unit; 9058 get_uleb128 (diridx, linep, lineendp); 9059 Dwarf_Word mtime; 9060 if (lineendp - linep < 1) 9061 goto invalid_unit; 9062 get_uleb128 (mtime, linep, lineendp); 9063 Dwarf_Word filelength; 9064 if (lineendp - linep < 1) 9065 goto invalid_unit; 9066 get_uleb128 (filelength, linep, lineendp); 9067 9068 printf (_("\ 9069 define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"), 9070 diridx, (uint64_t) mtime, (uint64_t) filelength, 9071 fname); 9072 } 9073 break; 9074 9075 case DW_LNE_set_discriminator: 9076 /* Takes one ULEB128 parameter, the discriminator. */ 9077 if (unlikely (standard_opcode_lengths[opcode] != 1 9078 || lineendp - linep < 1)) 9079 goto invalid_unit; 9080 9081 get_uleb128 (u128, linep, lineendp); 9082 printf (_(" set discriminator to %u\n"), u128); 9083 break; 9084 9085 case DW_LNE_NVIDIA_inlined_call: 9086 { 9087 if (unlikely (linep >= lineendp)) 9088 goto invalid_data; 9089 9090 unsigned int context; 9091 get_uleb128 (context, linep, lineendp); 9092 9093 if (unlikely (linep >= lineendp)) 9094 goto invalid_data; 9095 9096 unsigned int function_name; 9097 get_uleb128 (function_name, linep, lineendp); 9098 function_name += debug_str_offset; 9099 9100 Elf_Data *str_data = dbg->sectiondata[IDX_debug_str]; 9101 char *function_str; 9102 if (str_data == NULL || function_name >= str_data->d_size 9103 || memchr (str_data->d_buf + function_name, '\0', 9104 str_data->d_size - function_name) == NULL) 9105 function_str = "???"; 9106 else 9107 function_str = (char *) str_data->d_buf + function_name; 9108 9109 printf (_(" set inlined context %u," 9110 " function name %s (0x%x)\n"), 9111 context, function_str, function_name); 9112 break; 9113 } 9114 9115 case DW_LNE_NVIDIA_set_function_name: 9116 { 9117 if (unlikely (linep >= lineendp)) 9118 goto invalid_data; 9119 9120 unsigned int function_name; 9121 get_uleb128 (function_name, linep, lineendp); 9122 function_name += debug_str_offset; 9123 9124 Elf_Data *str_data = dbg->sectiondata[IDX_debug_str]; 9125 char *function_str; 9126 if (str_data == NULL || function_name >= str_data->d_size 9127 || memchr (str_data->d_buf + function_name, '\0', 9128 str_data->d_size - function_name) == NULL) 9129 function_str = "???"; 9130 else 9131 function_str = (char *) str_data->d_buf + function_name; 9132 9133 printf (_(" set function name %s (0x%x)\n"), 9134 function_str, function_name); 9135 } 9136 break; 9137 9138 default: 9139 /* Unknown, ignore it. */ 9140 puts (_(" unknown opcode")); 9141 linep += len - 1; 9142 break; 9143 } 9144 } 9145 else if (opcode <= DW_LNS_set_isa) 9146 { 9147 /* This is a known standard opcode. */ 9148 switch (opcode) 9149 { 9150 case DW_LNS_copy: 9151 /* Takes no argument. */ 9152 puts (_(" copy")); 9153 break; 9154 9155 case DW_LNS_advance_pc: 9156 /* Takes one uleb128 parameter which is added to the 9157 address. */ 9158 if (lineendp - linep < 1) 9159 goto invalid_unit; 9160 get_uleb128 (u128, linep, lineendp); 9161 advance_pc (u128); 9162 { 9163 printf (_(" advance address by %u to "), 9164 op_addr_advance); 9165 print_dwarf_addr (dwflmod, 0, address, address); 9166 if (op_index > 0) 9167 printf (_(", op_index to %u"), op_index); 9168 printf ("\n"); 9169 } 9170 break; 9171 9172 case DW_LNS_advance_line: 9173 /* Takes one sleb128 parameter which is added to the 9174 line. */ 9175 if (lineendp - linep < 1) 9176 goto invalid_unit; 9177 get_sleb128 (s128, linep, lineendp); 9178 line += s128; 9179 printf (_("\ 9180 advance line by constant %d to %" PRId64 "\n"), 9181 s128, (int64_t) line); 9182 break; 9183 9184 case DW_LNS_set_file: 9185 /* Takes one uleb128 parameter which is stored in file. */ 9186 if (lineendp - linep < 1) 9187 goto invalid_unit; 9188 get_uleb128 (u128, linep, lineendp); 9189 printf (_(" set file to %" PRIu64 "\n"), 9190 (uint64_t) u128); 9191 break; 9192 9193 case DW_LNS_set_column: 9194 /* Takes one uleb128 parameter which is stored in column. */ 9195 if (unlikely (standard_opcode_lengths[opcode] != 1 9196 || lineendp - linep < 1)) 9197 goto invalid_unit; 9198 9199 get_uleb128 (u128, linep, lineendp); 9200 printf (_(" set column to %" PRIu64 "\n"), 9201 (uint64_t) u128); 9202 break; 9203 9204 case DW_LNS_negate_stmt: 9205 /* Takes no argument. */ 9206 is_stmt = 1 - is_stmt; 9207 printf (_(" set '%s' to %" PRIuFAST8 "\n"), 9208 "is_stmt", is_stmt); 9209 break; 9210 9211 case DW_LNS_set_basic_block: 9212 /* Takes no argument. */ 9213 puts (_(" set basic block flag")); 9214 break; 9215 9216 case DW_LNS_const_add_pc: 9217 /* Takes no argument. */ 9218 9219 if (unlikely (line_range == 0)) 9220 goto invalid_unit; 9221 9222 advance_pc ((255 - opcode_base) / line_range); 9223 { 9224 printf (_(" advance address by constant %u to "), 9225 op_addr_advance); 9226 print_dwarf_addr (dwflmod, 0, address, address); 9227 if (op_index > 0) 9228 printf (_(", op_index to %u"), op_index); 9229 printf ("\n"); 9230 } 9231 break; 9232 9233 case DW_LNS_fixed_advance_pc: 9234 /* Takes one 16 bit parameter which is added to the 9235 address. */ 9236 if (unlikely (standard_opcode_lengths[opcode] != 1 9237 || lineendp - linep < 2)) 9238 goto invalid_unit; 9239 9240 u128 = read_2ubyte_unaligned_inc (dbg, linep); 9241 address += u128; 9242 op_index = 0; 9243 { 9244 printf (_("\ 9245 advance address by fixed value %u to \n"), 9246 u128); 9247 print_dwarf_addr (dwflmod, 0, address, address); 9248 printf ("\n"); 9249 } 9250 break; 9251 9252 case DW_LNS_set_prologue_end: 9253 /* Takes no argument. */ 9254 puts (_(" set prologue end flag")); 9255 break; 9256 9257 case DW_LNS_set_epilogue_begin: 9258 /* Takes no argument. */ 9259 puts (_(" set epilogue begin flag")); 9260 break; 9261 9262 case DW_LNS_set_isa: 9263 /* Takes one uleb128 parameter which is stored in isa. */ 9264 if (unlikely (standard_opcode_lengths[opcode] != 1 9265 || lineendp - linep < 1)) 9266 goto invalid_unit; 9267 9268 get_uleb128 (u128, linep, lineendp); 9269 printf (_(" set isa to %u\n"), u128); 9270 break; 9271 } 9272 } 9273 else 9274 { 9275 /* This is a new opcode the generator but not we know about. 9276 Read the parameters associated with it but then discard 9277 everything. Read all the parameters for this opcode. */ 9278 printf (ngettext (" unknown opcode with %" PRIu8 " parameter:", 9279 " unknown opcode with %" PRIu8 " parameters:", 9280 standard_opcode_lengths[opcode]), 9281 standard_opcode_lengths[opcode]); 9282 for (int n = standard_opcode_lengths[opcode]; 9283 n > 0 && linep < lineendp; --n) 9284 { 9285 get_uleb128 (u128, linep, lineendp); 9286 if (n != standard_opcode_lengths[opcode]) 9287 putc_unlocked (',', stdout); 9288 printf (" %u", u128); 9289 } 9290 9291 /* Next round, ignore this opcode. */ 9292 continue; 9293 } 9294 } 9295 } 9296 9297 /* There must only be one data block. */ 9298 assert (elf_getdata (scn, data) == NULL); 9299} 9300 9301 9302static void 9303print_debug_loclists_section (Dwfl_Module *dwflmod, 9304 Ebl *ebl, 9305 GElf_Ehdr *ehdr __attribute__ ((unused)), 9306 Elf_Scn *scn, GElf_Shdr *shdr, 9307 Dwarf *dbg) 9308{ 9309 printf (_("\ 9310\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 9311 elf_ndxscn (scn), section_name (ebl, shdr), 9312 (uint64_t) shdr->sh_offset); 9313 9314 Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists] 9315 ?: elf_rawdata (scn, NULL)); 9316 if (unlikely (data == NULL)) 9317 { 9318 error (0, 0, _("cannot get .debug_loclists content: %s"), 9319 elf_errmsg (-1)); 9320 return; 9321 } 9322 9323 /* For the listptr to get the base address/CU. */ 9324 sort_listptr (&known_loclistsptr, "loclistsptr"); 9325 size_t listptr_idx = 0; 9326 9327 const unsigned char *readp = data->d_buf; 9328 const unsigned char *const dataend = ((unsigned char *) data->d_buf 9329 + data->d_size); 9330 while (readp < dataend) 9331 { 9332 if (unlikely (readp > dataend - 4)) 9333 { 9334 invalid_data: 9335 error (0, 0, _("invalid data in section [%zu] '%s'"), 9336 elf_ndxscn (scn), section_name (ebl, shdr)); 9337 return; 9338 } 9339 9340 ptrdiff_t offset = readp - (unsigned char *) data->d_buf; 9341 printf (_("Table at Offset 0x%" PRIx64 ":\n\n"), 9342 (uint64_t) offset); 9343 9344 uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp); 9345 unsigned int offset_size = 4; 9346 if (unlikely (unit_length == 0xffffffff)) 9347 { 9348 if (unlikely (readp > dataend - 8)) 9349 goto invalid_data; 9350 9351 unit_length = read_8ubyte_unaligned_inc (dbg, readp); 9352 offset_size = 8; 9353 } 9354 printf (_(" Length: %8" PRIu64 "\n"), unit_length); 9355 9356 /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8 9357 bytes to complete the header. And this unit cannot go beyond 9358 the section data. */ 9359 if (readp > dataend - 8 9360 || unit_length < 8 9361 || unit_length > (uint64_t) (dataend - readp)) 9362 goto invalid_data; 9363 9364 const unsigned char *nexthdr = readp + unit_length; 9365 9366 uint16_t version = read_2ubyte_unaligned_inc (dbg, readp); 9367 printf (_(" DWARF version: %8" PRIu16 "\n"), version); 9368 9369 if (version != 5) 9370 { 9371 error (0, 0, _("Unknown version")); 9372 goto next_table; 9373 } 9374 9375 uint8_t address_size = *readp++; 9376 printf (_(" Address size: %8" PRIu64 "\n"), 9377 (uint64_t) address_size); 9378 9379 if (address_size != 4 && address_size != 8) 9380 { 9381 error (0, 0, _("unsupported address size")); 9382 goto next_table; 9383 } 9384 9385 uint8_t segment_size = *readp++; 9386 printf (_(" Segment size: %8" PRIu64 "\n"), 9387 (uint64_t) segment_size); 9388 9389 if (segment_size != 0) 9390 { 9391 error (0, 0, _("unsupported segment size")); 9392 goto next_table; 9393 } 9394 9395 uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp); 9396 printf (_(" Offset entries: %8" PRIu64 "\n"), 9397 (uint64_t) offset_entry_count); 9398 9399 /* We need the CU that uses this unit to get the initial base address. */ 9400 Dwarf_Addr cu_base = 0; 9401 struct Dwarf_CU *cu = NULL; 9402 if (listptr_cu (&known_loclistsptr, &listptr_idx, 9403 (Dwarf_Off) offset, 9404 (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf), 9405 &cu_base, &cu) 9406 || split_dwarf_cu_base (dbg, &cu, &cu_base)) 9407 { 9408 Dwarf_Die cudie; 9409 if (dwarf_cu_die (cu, &cudie, 9410 NULL, NULL, NULL, NULL, 9411 NULL, NULL) == NULL) 9412 printf (_(" Unknown CU base: ")); 9413 else 9414 printf (_(" CU [%6" PRIx64 "] base: "), 9415 dwarf_dieoffset (&cudie)); 9416 print_dwarf_addr (dwflmod, address_size, cu_base, cu_base); 9417 printf ("\n"); 9418 } 9419 else 9420 printf (_(" Not associated with a CU.\n")); 9421 9422 printf ("\n"); 9423 9424 const unsigned char *offset_array_start = readp; 9425 if (offset_entry_count > 0) 9426 { 9427 uint64_t max_entries = (unit_length - 8) / offset_size; 9428 if (offset_entry_count > max_entries) 9429 { 9430 error (0, 0, 9431 _("too many offset entries for unit length")); 9432 offset_entry_count = max_entries; 9433 } 9434 9435 printf (_(" Offsets starting at 0x%" PRIx64 ":\n"), 9436 (uint64_t) (offset_array_start 9437 - (unsigned char *) data->d_buf)); 9438 for (uint32_t idx = 0; idx < offset_entry_count; idx++) 9439 { 9440 printf (" [%6" PRIu32 "] ", idx); 9441 if (offset_size == 4) 9442 { 9443 uint32_t off = read_4ubyte_unaligned_inc (dbg, readp); 9444 printf ("0x%" PRIx32 "\n", off); 9445 } 9446 else 9447 { 9448 uint64_t off = read_8ubyte_unaligned_inc (dbg, readp); 9449 printf ("0x%" PRIx64 "\n", off); 9450 } 9451 } 9452 printf ("\n"); 9453 } 9454 9455 Dwarf_Addr base = cu_base; 9456 bool start_of_list = true; 9457 while (readp < nexthdr) 9458 { 9459 Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf); 9460 if (listptr_attr (&known_loclistsptr, listptr_idx, off, 9461 DW_AT_GNU_locviews)) 9462 { 9463 Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr, 9464 &listptr_idx, off); 9465 const unsigned char *locp = readp; 9466 const unsigned char *locendp; 9467 if (next_off == 0 9468 || next_off > (size_t) (nexthdr - ((const unsigned char *) 9469 data->d_buf))) 9470 locendp = nexthdr; 9471 else 9472 locendp = (const unsigned char *) data->d_buf + next_off; 9473 9474 printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n", 9475 (uint64_t) (readp - (unsigned char *) data->d_buf), 9476 (uint64_t) (readp - offset_array_start)); 9477 9478 while (locp < locendp) 9479 { 9480 uint64_t v1, v2; 9481 get_uleb128 (v1, locp, locendp); 9482 if (locp >= locendp) 9483 { 9484 printf (_(" <INVALID DATA>\n")); 9485 break; 9486 } 9487 get_uleb128 (v2, locp, locendp); 9488 printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2); 9489 } 9490 9491 printf ("\n"); 9492 readp = (unsigned char *) locendp; 9493 continue; 9494 } 9495 9496 uint8_t kind = *readp++; 9497 uint64_t op1, op2, len; 9498 9499 /* Skip padding. */ 9500 if (start_of_list && kind == DW_LLE_end_of_list) 9501 continue; 9502 9503 if (start_of_list) 9504 { 9505 base = cu_base; 9506 printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n", 9507 (uint64_t) (readp - (unsigned char *) data->d_buf - 1), 9508 (uint64_t) (readp - offset_array_start - 1)); 9509 start_of_list = false; 9510 } 9511 9512 printf (" %s", dwarf_loc_list_encoding_name (kind)); 9513 switch (kind) 9514 { 9515 case DW_LLE_end_of_list: 9516 start_of_list = true; 9517 printf ("\n\n"); 9518 break; 9519 9520 case DW_LLE_base_addressx: 9521 if ((uint64_t) (nexthdr - readp) < 1) 9522 { 9523 invalid_entry: 9524 error (0, 0, _("invalid loclists data")); 9525 goto next_table; 9526 } 9527 get_uleb128 (op1, readp, nexthdr); 9528 printf (" %" PRIx64 "\n", op1); 9529 if (! print_unresolved_addresses) 9530 { 9531 Dwarf_Addr addr; 9532 if (get_indexed_addr (cu, op1, &addr) != 0) 9533 printf (" ???\n"); 9534 else 9535 { 9536 printf (" "); 9537 print_dwarf_addr (dwflmod, address_size, addr, addr); 9538 printf ("\n"); 9539 } 9540 } 9541 break; 9542 9543 case DW_LLE_startx_endx: 9544 if ((uint64_t) (nexthdr - readp) < 1) 9545 goto invalid_entry; 9546 get_uleb128 (op1, readp, nexthdr); 9547 if ((uint64_t) (nexthdr - readp) < 1) 9548 goto invalid_entry; 9549 get_uleb128 (op2, readp, nexthdr); 9550 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2); 9551 if (! print_unresolved_addresses) 9552 { 9553 Dwarf_Addr addr1; 9554 Dwarf_Addr addr2; 9555 if (get_indexed_addr (cu, op1, &addr1) != 0 9556 || get_indexed_addr (cu, op2, &addr2) != 0) 9557 { 9558 printf (" ???..\n"); 9559 printf (" ???\n"); 9560 } 9561 else 9562 { 9563 printf (" "); 9564 print_dwarf_addr (dwflmod, address_size, addr1, addr1); 9565 printf ("..\n "); 9566 print_dwarf_addr (dwflmod, address_size, 9567 addr2 - 1, addr2); 9568 printf ("\n"); 9569 } 9570 } 9571 if ((uint64_t) (nexthdr - readp) < 1) 9572 goto invalid_entry; 9573 get_uleb128 (len, readp, nexthdr); 9574 if ((uint64_t) (nexthdr - readp) < len) 9575 goto invalid_entry; 9576 print_ops (dwflmod, dbg, 8, 8, version, 9577 address_size, offset_size, cu, len, readp); 9578 readp += len; 9579 break; 9580 9581 case DW_LLE_startx_length: 9582 if ((uint64_t) (nexthdr - readp) < 1) 9583 goto invalid_entry; 9584 get_uleb128 (op1, readp, nexthdr); 9585 if ((uint64_t) (nexthdr - readp) < 1) 9586 goto invalid_entry; 9587 get_uleb128 (op2, readp, nexthdr); 9588 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2); 9589 if (! print_unresolved_addresses) 9590 { 9591 Dwarf_Addr addr1; 9592 Dwarf_Addr addr2; 9593 if (get_indexed_addr (cu, op1, &addr1) != 0) 9594 { 9595 printf (" ???..\n"); 9596 printf (" ???\n"); 9597 } 9598 else 9599 { 9600 addr2 = addr1 + op2; 9601 printf (" "); 9602 print_dwarf_addr (dwflmod, address_size, addr1, addr1); 9603 printf ("..\n "); 9604 print_dwarf_addr (dwflmod, address_size, 9605 addr2 - 1, addr2); 9606 printf ("\n"); 9607 } 9608 } 9609 if ((uint64_t) (nexthdr - readp) < 1) 9610 goto invalid_entry; 9611 get_uleb128 (len, readp, nexthdr); 9612 if ((uint64_t) (nexthdr - readp) < len) 9613 goto invalid_entry; 9614 print_ops (dwflmod, dbg, 8, 8, version, 9615 address_size, offset_size, cu, len, readp); 9616 readp += len; 9617 break; 9618 9619 case DW_LLE_offset_pair: 9620 if ((uint64_t) (nexthdr - readp) < 1) 9621 goto invalid_entry; 9622 get_uleb128 (op1, readp, nexthdr); 9623 if ((uint64_t) (nexthdr - readp) < 1) 9624 goto invalid_entry; 9625 get_uleb128 (op2, readp, nexthdr); 9626 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2); 9627 if (! print_unresolved_addresses) 9628 { 9629 op1 += base; 9630 op2 += base; 9631 printf (" "); 9632 print_dwarf_addr (dwflmod, address_size, op1, op1); 9633 printf ("..\n "); 9634 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2); 9635 printf ("\n"); 9636 } 9637 if ((uint64_t) (nexthdr - readp) < 1) 9638 goto invalid_entry; 9639 get_uleb128 (len, readp, nexthdr); 9640 if ((uint64_t) (nexthdr - readp) < len) 9641 goto invalid_entry; 9642 print_ops (dwflmod, dbg, 8, 8, version, 9643 address_size, offset_size, cu, len, readp); 9644 readp += len; 9645 break; 9646 9647 case DW_LLE_default_location: 9648 if ((uint64_t) (nexthdr - readp) < 1) 9649 goto invalid_entry; 9650 get_uleb128 (len, readp, nexthdr); 9651 if ((uint64_t) (nexthdr - readp) < len) 9652 goto invalid_entry; 9653 print_ops (dwflmod, dbg, 8, 8, version, 9654 address_size, offset_size, cu, len, readp); 9655 readp += len; 9656 break; 9657 9658 case DW_LLE_base_address: 9659 if (address_size == 4) 9660 { 9661 if ((uint64_t) (nexthdr - readp) < 4) 9662 goto invalid_entry; 9663 op1 = read_4ubyte_unaligned_inc (dbg, readp); 9664 } 9665 else 9666 { 9667 if ((uint64_t) (nexthdr - readp) < 8) 9668 goto invalid_entry; 9669 op1 = read_8ubyte_unaligned_inc (dbg, readp); 9670 } 9671 base = op1; 9672 printf (" 0x%" PRIx64 "\n", base); 9673 if (! print_unresolved_addresses) 9674 { 9675 printf (" "); 9676 print_dwarf_addr (dwflmod, address_size, base, base); 9677 printf ("\n"); 9678 } 9679 break; 9680 9681 case DW_LLE_start_end: 9682 if (address_size == 4) 9683 { 9684 if ((uint64_t) (nexthdr - readp) < 8) 9685 goto invalid_entry; 9686 op1 = read_4ubyte_unaligned_inc (dbg, readp); 9687 op2 = read_4ubyte_unaligned_inc (dbg, readp); 9688 } 9689 else 9690 { 9691 if ((uint64_t) (nexthdr - readp) < 16) 9692 goto invalid_entry; 9693 op1 = read_8ubyte_unaligned_inc (dbg, readp); 9694 op2 = read_8ubyte_unaligned_inc (dbg, readp); 9695 } 9696 printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2); 9697 if (! print_unresolved_addresses) 9698 { 9699 printf (" "); 9700 print_dwarf_addr (dwflmod, address_size, op1, op1); 9701 printf ("..\n "); 9702 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2); 9703 printf ("\n"); 9704 } 9705 if ((uint64_t) (nexthdr - readp) < 1) 9706 goto invalid_entry; 9707 get_uleb128 (len, readp, nexthdr); 9708 if ((uint64_t) (nexthdr - readp) < len) 9709 goto invalid_entry; 9710 print_ops (dwflmod, dbg, 8, 8, version, 9711 address_size, offset_size, cu, len, readp); 9712 readp += len; 9713 break; 9714 9715 case DW_LLE_start_length: 9716 if (address_size == 4) 9717 { 9718 if ((uint64_t) (nexthdr - readp) < 4) 9719 goto invalid_entry; 9720 op1 = read_4ubyte_unaligned_inc (dbg, readp); 9721 } 9722 else 9723 { 9724 if ((uint64_t) (nexthdr - readp) < 8) 9725 goto invalid_entry; 9726 op1 = read_8ubyte_unaligned_inc (dbg, readp); 9727 } 9728 if ((uint64_t) (nexthdr - readp) < 1) 9729 goto invalid_entry; 9730 get_uleb128 (op2, readp, nexthdr); 9731 printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2); 9732 if (! print_unresolved_addresses) 9733 { 9734 op2 = op1 + op2; 9735 printf (" "); 9736 print_dwarf_addr (dwflmod, address_size, op1, op1); 9737 printf ("..\n "); 9738 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2); 9739 printf ("\n"); 9740 } 9741 if ((uint64_t) (nexthdr - readp) < 1) 9742 goto invalid_entry; 9743 get_uleb128 (len, readp, nexthdr); 9744 if ((uint64_t) (nexthdr - readp) < len) 9745 goto invalid_entry; 9746 print_ops (dwflmod, dbg, 8, 8, version, 9747 address_size, offset_size, cu, len, readp); 9748 readp += len; 9749 break; 9750 9751 case DW_LLE_GNU_view_pair: 9752 if ((uint64_t) (nexthdr - readp) < 1) 9753 goto invalid_entry; 9754 get_uleb128 (op1, readp, nexthdr); 9755 if ((uint64_t) (nexthdr - readp) < 1) 9756 goto invalid_entry; 9757 get_uleb128 (op2, readp, nexthdr); 9758 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2); 9759 break; 9760 9761 default: 9762 goto invalid_entry; 9763 } 9764 } 9765 9766 next_table: 9767 if (readp != nexthdr) 9768 { 9769 size_t padding = nexthdr - readp; 9770 printf (_(" %zu padding bytes\n\n"), padding); 9771 readp = nexthdr; 9772 } 9773 } 9774} 9775 9776 9777static void 9778print_debug_loc_section (Dwfl_Module *dwflmod, 9779 Ebl *ebl, GElf_Ehdr *ehdr, 9780 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 9781{ 9782 Elf_Data *data = (dbg->sectiondata[IDX_debug_loc] 9783 ?: elf_rawdata (scn, NULL)); 9784 9785 if (unlikely (data == NULL)) 9786 { 9787 error (0, 0, _("cannot get .debug_loc content: %s"), 9788 elf_errmsg (-1)); 9789 return; 9790 } 9791 9792 printf (_("\ 9793\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 9794 elf_ndxscn (scn), section_name (ebl, shdr), 9795 (uint64_t) shdr->sh_offset); 9796 9797 sort_listptr (&known_locsptr, "loclistptr"); 9798 size_t listptr_idx = 0; 9799 9800 uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8; 9801 uint_fast8_t offset_size = 4; 9802 9803 bool first = true; 9804 Dwarf_Addr base = 0; 9805 unsigned char *readp = data->d_buf; 9806 unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size; 9807 Dwarf_CU *last_cu = NULL; 9808 while (readp < endp) 9809 { 9810 ptrdiff_t offset = readp - (unsigned char *) data->d_buf; 9811 Dwarf_CU *cu = last_cu; 9812 unsigned int attr = 0; 9813 9814 if (first && skip_listptr_hole (&known_locsptr, &listptr_idx, 9815 &address_size, &offset_size, &base, 9816 &cu, offset, &readp, endp, &attr)) 9817 continue; 9818 9819 if (last_cu != cu) 9820 { 9821 Dwarf_Die cudie; 9822 if (dwarf_cu_die (cu, &cudie, 9823 NULL, NULL, NULL, NULL, 9824 NULL, NULL) == NULL) 9825 printf (_("\n Unknown CU base: ")); 9826 else 9827 printf (_("\n CU [%6" PRIx64 "] base: "), 9828 dwarf_dieoffset (&cudie)); 9829 print_dwarf_addr (dwflmod, address_size, base, base); 9830 printf ("\n"); 9831 } 9832 last_cu = cu; 9833 9834 if (attr == DW_AT_GNU_locviews) 9835 { 9836 Dwarf_Off next_off = next_listptr_offset (&known_locsptr, 9837 &listptr_idx, offset); 9838 const unsigned char *locp = readp; 9839 const unsigned char *locendp; 9840 if (next_off == 0 9841 || next_off > (size_t) (endp 9842 - (const unsigned char *) data->d_buf)) 9843 locendp = endp; 9844 else 9845 locendp = (const unsigned char *) data->d_buf + next_off; 9846 9847 while (locp < locendp) 9848 { 9849 uint64_t v1, v2; 9850 get_uleb128 (v1, locp, locendp); 9851 if (locp >= locendp) 9852 { 9853 printf (_(" [%6tx] <INVALID DATA>\n"), offset); 9854 break; 9855 } 9856 get_uleb128 (v2, locp, locendp); 9857 if (first) /* First view pair in a list. */ 9858 printf (" [%6tx] ", offset); 9859 else 9860 printf (" "); 9861 printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2); 9862 first = false; 9863 } 9864 9865 first = true; 9866 readp = (unsigned char *) locendp; 9867 continue; 9868 } 9869 9870 /* GNU DebugFission encoded addresses as addrx. */ 9871 bool is_debugfission = ((cu != NULL 9872 || split_dwarf_cu_base (dbg, &cu, &base)) 9873 && (cu->version < 5 9874 && cu->unit_type == DW_UT_split_compile)); 9875 if (!is_debugfission 9876 && unlikely (data->d_size - offset < (size_t) address_size * 2)) 9877 { 9878 invalid_data: 9879 printf (_(" [%6tx] <INVALID DATA>\n"), offset); 9880 break; 9881 } 9882 9883 Dwarf_Addr begin; 9884 Dwarf_Addr end; 9885 bool use_base = true; 9886 if (is_debugfission) 9887 { 9888 const unsigned char *locp = readp; 9889 const unsigned char *locendp = readp + data->d_size; 9890 if (locp >= locendp) 9891 goto invalid_data; 9892 9893 Dwarf_Word idx; 9894 unsigned char code = *locp++; 9895 switch (code) 9896 { 9897 case DW_LLE_GNU_end_of_list_entry: 9898 begin = 0; 9899 end = 0; 9900 break; 9901 9902 case DW_LLE_GNU_base_address_selection_entry: 9903 if (locp >= locendp) 9904 goto invalid_data; 9905 begin = (Dwarf_Addr) -1; 9906 get_uleb128 (idx, locp, locendp); 9907 if (get_indexed_addr (cu, idx, &end) != 0) 9908 end = idx; /* ... */ 9909 break; 9910 9911 case DW_LLE_GNU_start_end_entry: 9912 if (locp >= locendp) 9913 goto invalid_data; 9914 get_uleb128 (idx, locp, locendp); 9915 if (get_indexed_addr (cu, idx, &begin) != 0) 9916 begin = idx; /* ... */ 9917 if (locp >= locendp) 9918 goto invalid_data; 9919 get_uleb128 (idx, locp, locendp); 9920 if (get_indexed_addr (cu, idx, &end) != 0) 9921 end = idx; /* ... */ 9922 use_base = false; 9923 break; 9924 9925 case DW_LLE_GNU_start_length_entry: 9926 if (locp >= locendp) 9927 goto invalid_data; 9928 get_uleb128 (idx, locp, locendp); 9929 if (get_indexed_addr (cu, idx, &begin) != 0) 9930 begin = idx; /* ... */ 9931 if (locendp - locp < 4) 9932 goto invalid_data; 9933 end = read_4ubyte_unaligned_inc (dbg, locp); 9934 end += begin; 9935 use_base = false; 9936 break; 9937 9938 default: 9939 goto invalid_data; 9940 } 9941 9942 readp = (unsigned char *) locp; 9943 } 9944 else if (address_size == 8) 9945 { 9946 begin = read_8ubyte_unaligned_inc (dbg, readp); 9947 end = read_8ubyte_unaligned_inc (dbg, readp); 9948 } 9949 else 9950 { 9951 begin = read_4ubyte_unaligned_inc (dbg, readp); 9952 end = read_4ubyte_unaligned_inc (dbg, readp); 9953 if (begin == (Dwarf_Addr) (uint32_t) -1) 9954 begin = (Dwarf_Addr) -1l; 9955 } 9956 9957 if (begin == (Dwarf_Addr) -1l) /* Base address entry. */ 9958 { 9959 if (first) 9960 printf (" [%6tx] ", offset); 9961 else 9962 printf (" "); 9963 puts (_("base address")); 9964 printf (" "); 9965 print_dwarf_addr (dwflmod, address_size, end, end); 9966 printf ("\n"); 9967 base = end; 9968 first = false; 9969 } 9970 else if (begin == 0 && end == 0) /* End of list entry. */ 9971 { 9972 if (first) 9973 printf (_(" [%6tx] empty list\n"), offset); 9974 first = true; 9975 } 9976 else 9977 { 9978 /* We have a location expression entry. */ 9979 uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp); 9980 9981 if (first) /* First entry in a list. */ 9982 printf (" [%6tx] ", offset); 9983 else 9984 printf (" "); 9985 9986 printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end); 9987 if (! print_unresolved_addresses) 9988 { 9989 Dwarf_Addr dab = use_base ? base + begin : begin; 9990 Dwarf_Addr dae = use_base ? base + end : end; 9991 printf (" "); 9992 print_dwarf_addr (dwflmod, address_size, dab, dab); 9993 printf ("..\n "); 9994 print_dwarf_addr (dwflmod, address_size, dae - 1, dae); 9995 printf ("\n"); 9996 } 9997 9998 if (endp - readp <= (ptrdiff_t) len) 9999 { 10000 fputs (_(" <INVALID DATA>\n"), stdout); 10001 break; 10002 } 10003 10004 print_ops (dwflmod, dbg, 11, 11, 10005 cu != NULL ? cu->version : 3, 10006 address_size, offset_size, cu, len, readp); 10007 10008 first = false; 10009 readp += len; 10010 } 10011 } 10012} 10013 10014struct mac_culist 10015{ 10016 Dwarf_Die die; 10017 Dwarf_Off offset; 10018 Dwarf_Files *files; 10019 struct mac_culist *next; 10020}; 10021 10022 10023static int 10024mac_compare (const void *p1, const void *p2) 10025{ 10026 struct mac_culist *m1 = (struct mac_culist *) p1; 10027 struct mac_culist *m2 = (struct mac_culist *) p2; 10028 10029 if (m1->offset < m2->offset) 10030 return -1; 10031 if (m1->offset > m2->offset) 10032 return 1; 10033 return 0; 10034} 10035 10036 10037static void 10038print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 10039 Ebl *ebl, 10040 GElf_Ehdr *ehdr __attribute__ ((unused)), 10041 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 10042{ 10043 printf (_("\ 10044\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 10045 elf_ndxscn (scn), section_name (ebl, shdr), 10046 (uint64_t) shdr->sh_offset); 10047 putc_unlocked ('\n', stdout); 10048 10049 /* There is no function in libdw to iterate over the raw content of 10050 the section but it is easy enough to do. */ 10051 Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo] 10052 ?: elf_rawdata (scn, NULL)); 10053 if (unlikely (data == NULL)) 10054 { 10055 error (0, 0, _("cannot get macro information section data: %s"), 10056 elf_errmsg (-1)); 10057 return; 10058 } 10059 10060 /* Get the source file information for all CUs. */ 10061 Dwarf_Off offset; 10062 Dwarf_Off ncu = 0; 10063 size_t hsize; 10064 struct mac_culist *culist = NULL; 10065 size_t nculist = 0; 10066 while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0) 10067 { 10068 Dwarf_Die cudie; 10069 if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL) 10070 continue; 10071 10072 Dwarf_Attribute attr; 10073 if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL) 10074 continue; 10075 10076 Dwarf_Word macoff; 10077 if (dwarf_formudata (&attr, &macoff) != 0) 10078 continue; 10079 10080 struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp)); 10081 newp->die = cudie; 10082 newp->offset = macoff; 10083 newp->files = NULL; 10084 newp->next = culist; 10085 culist = newp; 10086 ++nculist; 10087 } 10088 10089 /* Convert the list into an array for easier consumption. */ 10090 struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1) 10091 * sizeof (*cus)); 10092 /* Add sentinel. */ 10093 cus[nculist].offset = data->d_size; 10094 cus[nculist].files = (Dwarf_Files *) -1l; 10095 if (nculist > 0) 10096 { 10097 for (size_t cnt = nculist - 1; culist != NULL; --cnt) 10098 { 10099 assert (cnt < nculist); 10100 cus[cnt] = *culist; 10101 culist = culist->next; 10102 } 10103 10104 /* Sort the array according to the offset in the .debug_macinfo 10105 section. Note we keep the sentinel at the end. */ 10106 qsort (cus, nculist, sizeof (*cus), mac_compare); 10107 } 10108 10109 const unsigned char *readp = (const unsigned char *) data->d_buf; 10110 const unsigned char *readendp = readp + data->d_size; 10111 int level = 1; 10112 10113 while (readp < readendp) 10114 { 10115 unsigned int opcode = *readp++; 10116 unsigned int u128; 10117 unsigned int u128_2; 10118 const unsigned char *endp; 10119 10120 switch (opcode) 10121 { 10122 case DW_MACINFO_define: 10123 case DW_MACINFO_undef: 10124 case DW_MACINFO_vendor_ext: 10125 /* For the first two opcodes the parameters are 10126 line, string 10127 For the latter 10128 number, string. 10129 We can treat these cases together. */ 10130 get_uleb128 (u128, readp, readendp); 10131 10132 endp = memchr (readp, '\0', readendp - readp); 10133 if (unlikely (endp == NULL)) 10134 { 10135 printf (_("\ 10136%*s*** non-terminated string at end of section"), 10137 level, ""); 10138 return; 10139 } 10140 10141 if (opcode == DW_MACINFO_define) 10142 printf ("%*s#define %s, line %u\n", 10143 level, "", (char *) readp, u128); 10144 else if (opcode == DW_MACINFO_undef) 10145 printf ("%*s#undef %s, line %u\n", 10146 level, "", (char *) readp, u128); 10147 else 10148 printf (" #vendor-ext %s, number %u\n", (char *) readp, u128); 10149 10150 readp = endp + 1; 10151 break; 10152 10153 case DW_MACINFO_start_file: 10154 /* The two parameters are line and file index, in this order. */ 10155 get_uleb128 (u128, readp, readendp); 10156 if (readendp - readp < 1) 10157 { 10158 printf (_("\ 10159%*s*** missing DW_MACINFO_start_file argument at end of section"), 10160 level, ""); 10161 return; 10162 } 10163 get_uleb128 (u128_2, readp, readendp); 10164 10165 /* Find the CU DIE for this file. */ 10166 size_t macoff = readp - (const unsigned char *) data->d_buf; 10167 const char *fname = "???"; 10168 if (macoff >= cus[0].offset && cus[0].offset != data->d_size) 10169 { 10170 while (macoff >= cus[1].offset && cus[1].offset != data->d_size) 10171 ++cus; 10172 10173 if (cus[0].files == NULL 10174 && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0) 10175 cus[0].files = (Dwarf_Files *) -1l; 10176 10177 if (cus[0].files != (Dwarf_Files *) -1l) 10178 fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL) 10179 ?: "???"); 10180 } 10181 10182 printf ("%*sstart_file %u, [%u] %s\n", 10183 level, "", u128, u128_2, fname); 10184 ++level; 10185 break; 10186 10187 case DW_MACINFO_end_file: 10188 --level; 10189 printf ("%*send_file\n", level, ""); 10190 /* Nothing more to do. */ 10191 break; 10192 10193 default: 10194 // XXX gcc seems to generate files with a trailing zero. 10195 if (unlikely (opcode != 0 || readp != readendp)) 10196 printf ("%*s*** invalid opcode %u\n", level, "", opcode); 10197 break; 10198 } 10199 } 10200} 10201 10202 10203static void 10204print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 10205 Ebl *ebl, 10206 GElf_Ehdr *ehdr __attribute__ ((unused)), 10207 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 10208{ 10209 printf (_("\ 10210\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 10211 elf_ndxscn (scn), section_name (ebl, shdr), 10212 (uint64_t) shdr->sh_offset); 10213 putc_unlocked ('\n', stdout); 10214 10215 Elf_Data *data = elf_getdata (scn, NULL); 10216 if (unlikely (data == NULL)) 10217 { 10218 error (0, 0, _("cannot get macro information section data: %s"), 10219 elf_errmsg (-1)); 10220 return; 10221 } 10222 10223 /* Get the source file information for all CUs. Uses same 10224 datastructure as macinfo. But uses offset field to directly 10225 match .debug_line offset. And just stored in a list. */ 10226 Dwarf_Off offset; 10227 Dwarf_Off ncu = 0; 10228 size_t hsize; 10229 struct mac_culist *culist = NULL; 10230 size_t nculist = 0; 10231 while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0) 10232 { 10233 Dwarf_Die cudie; 10234 if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL) 10235 continue; 10236 10237 Dwarf_Attribute attr; 10238 if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL) 10239 continue; 10240 10241 Dwarf_Word lineoff; 10242 if (dwarf_formudata (&attr, &lineoff) != 0) 10243 continue; 10244 10245 struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp)); 10246 newp->die = cudie; 10247 newp->offset = lineoff; 10248 newp->files = NULL; 10249 newp->next = culist; 10250 culist = newp; 10251 ++nculist; 10252 } 10253 10254 const unsigned char *readp = (const unsigned char *) data->d_buf; 10255 const unsigned char *readendp = readp + data->d_size; 10256 10257 while (readp < readendp) 10258 { 10259 printf (_(" Offset: 0x%" PRIx64 "\n"), 10260 (uint64_t) (readp - (const unsigned char *) data->d_buf)); 10261 10262 // Header, 2 byte version, 1 byte flag, optional .debug_line offset, 10263 // optional vendor extension macro entry table. 10264 if (readp + 2 > readendp) 10265 { 10266 invalid_data: 10267 error (0, 0, _("invalid data")); 10268 return; 10269 } 10270 const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp); 10271 printf (_(" Version: %" PRIu16 "\n"), vers); 10272 10273 // Version 4 is the GNU extension for DWARF4. DWARF5 will use version 10274 // 5 when it gets standardized. 10275 if (vers != 4 && vers != 5) 10276 { 10277 printf (_(" unknown version, cannot parse section\n")); 10278 return; 10279 } 10280 10281 if (readp + 1 > readendp) 10282 goto invalid_data; 10283 const unsigned char flag = *readp++; 10284 printf (_(" Flag: 0x%" PRIx8), flag); 10285 if (flag != 0) 10286 { 10287 printf (" ("); 10288 if ((flag & 0x01) != 0) 10289 { 10290 printf ("offset_size"); 10291 if ((flag & 0xFE) != 0) 10292 printf (", "); 10293 } 10294 if ((flag & 0x02) != 0) 10295 { 10296 printf ("debug_line_offset"); 10297 if ((flag & 0xFC) != 0) 10298 printf (", "); 10299 } 10300 if ((flag & 0x04) != 0) 10301 { 10302 printf ("operands_table"); 10303 if ((flag & 0xF8) != 0) 10304 printf (", "); 10305 } 10306 if ((flag & 0xF8) != 0) 10307 printf ("unknown"); 10308 printf (")"); 10309 } 10310 printf ("\n"); 10311 10312 unsigned int offset_len = (flag & 0x01) ? 8 : 4; 10313 printf (_(" Offset length: %" PRIu8 "\n"), offset_len); 10314 Dwarf_Off line_offset = -1; 10315 if (flag & 0x02) 10316 { 10317 if (offset_len == 8) 10318 line_offset = read_8ubyte_unaligned_inc (dbg, readp); 10319 else 10320 line_offset = read_4ubyte_unaligned_inc (dbg, readp); 10321 printf (_(" .debug_line offset: 0x%" PRIx64 "\n"), 10322 line_offset); 10323 } 10324 10325 struct mac_culist *cu = NULL; 10326 if (line_offset != (Dwarf_Off) -1) 10327 { 10328 cu = culist; 10329 while (cu != NULL && line_offset != cu->offset) 10330 cu = cu->next; 10331 } 10332 10333 Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL 10334 ? cu->die.cu 10335 : NULL)); 10336 10337 const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1]; 10338 memset (vendor, 0, sizeof vendor); 10339 if (flag & 0x04) 10340 { 10341 // 1 byte length, for each item, 1 byte opcode, uleb128 number 10342 // of arguments, for each argument 1 byte form code. 10343 if (readp + 1 > readendp) 10344 goto invalid_data; 10345 unsigned int tlen = *readp++; 10346 printf (_(" extension opcode table, %" PRIu8 " items:\n"), 10347 tlen); 10348 for (unsigned int i = 0; i < tlen; i++) 10349 { 10350 if (readp + 1 > readendp) 10351 goto invalid_data; 10352 unsigned int opcode = *readp++; 10353 printf (_(" [%" PRIx8 "]"), opcode); 10354 if (opcode < DW_MACRO_lo_user 10355 || opcode > DW_MACRO_hi_user) 10356 goto invalid_data; 10357 // Record the start of description for this vendor opcode. 10358 // uleb128 nr args, 1 byte per arg form. 10359 vendor[opcode - DW_MACRO_lo_user] = readp; 10360 if (readp + 1 > readendp) 10361 goto invalid_data; 10362 unsigned int args = *readp++; 10363 if (args > 0) 10364 { 10365 printf (_(" %" PRIu8 " arguments:"), args); 10366 while (args > 0) 10367 { 10368 if (readp + 1 > readendp) 10369 goto invalid_data; 10370 unsigned int form = *readp++; 10371 printf (" %s", dwarf_form_name (form)); 10372 if (! libdw_valid_user_form (form)) 10373 goto invalid_data; 10374 args--; 10375 if (args > 0) 10376 putchar_unlocked (','); 10377 } 10378 } 10379 else 10380 printf (_(" no arguments.")); 10381 putchar_unlocked ('\n'); 10382 } 10383 } 10384 putchar_unlocked ('\n'); 10385 10386 int level = 1; 10387 if (readp + 1 > readendp) 10388 goto invalid_data; 10389 unsigned int opcode = *readp++; 10390 while (opcode != 0) 10391 { 10392 unsigned int u128; 10393 unsigned int u128_2; 10394 const unsigned char *endp; 10395 uint64_t off; 10396 10397 switch (opcode) 10398 { 10399 case DW_MACRO_start_file: 10400 get_uleb128 (u128, readp, readendp); 10401 if (readp >= readendp) 10402 goto invalid_data; 10403 get_uleb128 (u128_2, readp, readendp); 10404 10405 /* Find the CU DIE that matches this line offset. */ 10406 const char *fname = "???"; 10407 if (cu != NULL) 10408 { 10409 if (cu->files == NULL 10410 && dwarf_getsrcfiles (&cu->die, &cu->files, 10411 NULL) != 0) 10412 cu->files = (Dwarf_Files *) -1l; 10413 10414 if (cu->files != (Dwarf_Files *) -1l) 10415 fname = (dwarf_filesrc (cu->files, u128_2, 10416 NULL, NULL) ?: "???"); 10417 } 10418 printf ("%*sstart_file %u, [%u] %s\n", 10419 level, "", u128, u128_2, fname); 10420 ++level; 10421 break; 10422 10423 case DW_MACRO_end_file: 10424 --level; 10425 printf ("%*send_file\n", level, ""); 10426 break; 10427 10428 case DW_MACRO_define: 10429 get_uleb128 (u128, readp, readendp); 10430 endp = memchr (readp, '\0', readendp - readp); 10431 if (endp == NULL) 10432 goto invalid_data; 10433 printf ("%*s#define %s, line %u\n", 10434 level, "", readp, u128); 10435 readp = endp + 1; 10436 break; 10437 10438 case DW_MACRO_undef: 10439 get_uleb128 (u128, readp, readendp); 10440 endp = memchr (readp, '\0', readendp - readp); 10441 if (endp == NULL) 10442 goto invalid_data; 10443 printf ("%*s#undef %s, line %u\n", 10444 level, "", readp, u128); 10445 readp = endp + 1; 10446 break; 10447 10448 case DW_MACRO_define_strp: 10449 get_uleb128 (u128, readp, readendp); 10450 if (readp + offset_len > readendp) 10451 goto invalid_data; 10452 if (offset_len == 8) 10453 off = read_8ubyte_unaligned_inc (dbg, readp); 10454 else 10455 off = read_4ubyte_unaligned_inc (dbg, readp); 10456 printf ("%*s#define %s, line %u (indirect)\n", 10457 level, "", dwarf_getstring (dbg, off, NULL), u128); 10458 break; 10459 10460 case DW_MACRO_undef_strp: 10461 get_uleb128 (u128, readp, readendp); 10462 if (readp + offset_len > readendp) 10463 goto invalid_data; 10464 if (offset_len == 8) 10465 off = read_8ubyte_unaligned_inc (dbg, readp); 10466 else 10467 off = read_4ubyte_unaligned_inc (dbg, readp); 10468 printf ("%*s#undef %s, line %u (indirect)\n", 10469 level, "", dwarf_getstring (dbg, off, NULL), u128); 10470 break; 10471 10472 case DW_MACRO_import: 10473 if (readp + offset_len > readendp) 10474 goto invalid_data; 10475 if (offset_len == 8) 10476 off = read_8ubyte_unaligned_inc (dbg, readp); 10477 else 10478 off = read_4ubyte_unaligned_inc (dbg, readp); 10479 printf ("%*s#include offset 0x%" PRIx64 "\n", 10480 level, "", off); 10481 break; 10482 10483 case DW_MACRO_define_sup: 10484 get_uleb128 (u128, readp, readendp); 10485 if (readp + offset_len > readendp) 10486 goto invalid_data; 10487 printf ("%*s#define ", level, ""); 10488 readp = print_form_data (dbg, DW_FORM_strp_sup, 10489 readp, readendp, offset_len, 10490 str_offsets_base); 10491 printf (", line %u (sup)\n", u128); 10492 break; 10493 10494 case DW_MACRO_undef_sup: 10495 get_uleb128 (u128, readp, readendp); 10496 if (readp + offset_len > readendp) 10497 goto invalid_data; 10498 printf ("%*s#undef ", level, ""); 10499 readp = print_form_data (dbg, DW_FORM_strp_sup, 10500 readp, readendp, offset_len, 10501 str_offsets_base); 10502 printf (", line %u (sup)\n", u128); 10503 break; 10504 10505 case DW_MACRO_import_sup: 10506 if (readp + offset_len > readendp) 10507 goto invalid_data; 10508 if (offset_len == 8) 10509 off = read_8ubyte_unaligned_inc (dbg, readp); 10510 else 10511 off = read_4ubyte_unaligned_inc (dbg, readp); 10512 // XXX Needs support for reading from supplementary object file. 10513 printf ("%*s#include offset 0x%" PRIx64 " (sup)\n", 10514 level, "", off); 10515 break; 10516 10517 case DW_MACRO_define_strx: 10518 get_uleb128 (u128, readp, readendp); 10519 if (readp + offset_len > readendp) 10520 goto invalid_data; 10521 printf ("%*s#define ", level, ""); 10522 readp = print_form_data (dbg, DW_FORM_strx, 10523 readp, readendp, offset_len, 10524 str_offsets_base); 10525 printf (", line %u (strx)\n", u128); 10526 break; 10527 10528 case DW_MACRO_undef_strx: 10529 get_uleb128 (u128, readp, readendp); 10530 if (readp + offset_len > readendp) 10531 goto invalid_data; 10532 printf ("%*s#undef ", level, ""); 10533 readp = print_form_data (dbg, DW_FORM_strx, 10534 readp, readendp, offset_len, 10535 str_offsets_base); 10536 printf (", line %u (strx)\n", u128); 10537 break; 10538 10539 default: 10540 printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode); 10541 if (opcode < DW_MACRO_lo_user 10542 || opcode > DW_MACRO_lo_user 10543 || vendor[opcode - DW_MACRO_lo_user] == NULL) 10544 goto invalid_data; 10545 10546 const unsigned char *op_desc; 10547 op_desc = vendor[opcode - DW_MACRO_lo_user]; 10548 10549 // Just skip the arguments, we cannot really interpret them, 10550 // but print as much as we can. 10551 unsigned int args = *op_desc++; 10552 while (args > 0 && readp < readendp) 10553 { 10554 unsigned int form = *op_desc++; 10555 readp = print_form_data (dbg, form, readp, readendp, 10556 offset_len, str_offsets_base); 10557 args--; 10558 if (args > 0) 10559 printf (", "); 10560 } 10561 putchar_unlocked ('\n'); 10562 } 10563 10564 if (readp + 1 > readendp) 10565 goto invalid_data; 10566 opcode = *readp++; 10567 if (opcode == 0) 10568 putchar_unlocked ('\n'); 10569 } 10570 } 10571} 10572 10573 10574/* Callback for printing global names. */ 10575static int 10576print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global, 10577 void *arg) 10578{ 10579 int *np = (int *) arg; 10580 10581 printf (_(" [%5d] DIE offset: %6" PRId64 10582 ", CU DIE offset: %6" PRId64 ", name: %s\n"), 10583 (*np)++, global->die_offset, global->cu_offset, global->name); 10584 10585 return 0; 10586} 10587 10588 10589/* Print the known exported symbols in the DWARF section '.debug_pubnames'. */ 10590static void 10591print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 10592 Ebl *ebl, 10593 GElf_Ehdr *ehdr __attribute__ ((unused)), 10594 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 10595{ 10596 printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 10597 elf_ndxscn (scn), section_name (ebl, shdr), 10598 (uint64_t) shdr->sh_offset); 10599 10600 int n = 0; 10601 (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0); 10602} 10603 10604/* Print the content of the DWARF string section '.debug_str'. */ 10605static void 10606print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 10607 Ebl *ebl, 10608 GElf_Ehdr *ehdr __attribute__ ((unused)), 10609 Elf_Scn *scn, GElf_Shdr *shdr, 10610 Dwarf *dbg __attribute__ ((unused))) 10611{ 10612 Elf_Data *data = elf_rawdata (scn, NULL); 10613 const size_t sh_size = data ? data->d_size : 0; 10614 10615 /* Compute floor(log16(shdr->sh_size)). */ 10616 GElf_Addr tmp = sh_size; 10617 int digits = 1; 10618 while (tmp >= 16) 10619 { 10620 ++digits; 10621 tmp >>= 4; 10622 } 10623 digits = MAX (4, digits); 10624 10625 printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n" 10626 " %*s String\n"), 10627 elf_ndxscn (scn), 10628 section_name (ebl, shdr), (uint64_t) shdr->sh_offset, 10629 /* TRANS: the debugstr| prefix makes the string unique. */ 10630 digits + 2, sgettext ("debugstr|Offset")); 10631 10632 Dwarf_Off offset = 0; 10633 while (offset < sh_size) 10634 { 10635 size_t len; 10636 const char *str = (const char *) data->d_buf + offset; 10637 const char *endp = memchr (str, '\0', sh_size - offset); 10638 if (unlikely (endp == NULL)) 10639 { 10640 printf (_(" *** error, missing string terminator\n")); 10641 break; 10642 } 10643 10644 printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str); 10645 len = endp - str; 10646 offset += len + 1; 10647 } 10648} 10649 10650static void 10651print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 10652 Ebl *ebl, 10653 GElf_Ehdr *ehdr __attribute__ ((unused)), 10654 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 10655{ 10656 printf (_("\ 10657\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"), 10658 elf_ndxscn (scn), section_name (ebl, shdr), 10659 (uint64_t) shdr->sh_offset); 10660 10661 if (shdr->sh_size == 0) 10662 return; 10663 10664 /* We like to get the section from libdw to make sure they are relocated. */ 10665 Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets] 10666 ?: elf_rawdata (scn, NULL)); 10667 if (unlikely (data == NULL)) 10668 { 10669 error (0, 0, _("cannot get .debug_str_offsets section data: %s"), 10670 elf_errmsg (-1)); 10671 return; 10672 } 10673 10674 size_t idx = 0; 10675 sort_listptr (&known_stroffbases, "str_offsets"); 10676 10677 const unsigned char *start = (const unsigned char *) data->d_buf; 10678 const unsigned char *readp = start; 10679 const unsigned char *readendp = ((const unsigned char *) data->d_buf 10680 + data->d_size); 10681 10682 while (readp < readendp) 10683 { 10684 /* Most string offset tables will have a header. For split 10685 dwarf unit GNU DebugFission didn't add one. But they were 10686 also only defined for split units (main or skeleton units 10687 didn't have indirect strings). So if we don't have a 10688 DW_AT_str_offsets_base at all and this is offset zero, then 10689 just start printing offsets immediately, if this is a .dwo 10690 section. */ 10691 Dwarf_Off off = (Dwarf_Off) (readp 10692 - (const unsigned char *) data->d_buf); 10693 10694 printf ("Table at offset %" PRIx64 " ", off); 10695 10696 struct listptr *listptr = get_listptr (&known_stroffbases, idx++); 10697 const unsigned char *next_unitp = readendp; 10698 uint8_t offset_size; 10699 bool has_header; 10700 if (listptr == NULL) 10701 { 10702 /* This can happen for .dwo files. There is only an header 10703 in the case this is a version 5 split DWARF file. */ 10704 Dwarf_CU *cu; 10705 uint8_t unit_type; 10706 if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type, 10707 NULL, NULL) != 0) 10708 { 10709 error (0, 0, "Warning: Cannot find any DWARF unit."); 10710 /* Just guess some values. */ 10711 has_header = false; 10712 offset_size = 4; 10713 } 10714 else if (off == 0 10715 && (unit_type == DW_UT_split_type 10716 || unit_type == DW_UT_split_compile)) 10717 { 10718 has_header = cu->version > 4; 10719 offset_size = cu->offset_size; 10720 } 10721 else 10722 { 10723 error (0, 0, 10724 "Warning: No CU references .debug_str_offsets after %" 10725 PRIx64, off); 10726 has_header = cu->version > 4; 10727 offset_size = cu->offset_size; 10728 } 10729 printf ("\n"); 10730 } 10731 else 10732 { 10733 /* This must be DWARF5, since GNU DebugFission didn't define 10734 DW_AT_str_offsets_base. */ 10735 has_header = true; 10736 10737 Dwarf_Die cudie; 10738 if (dwarf_cu_die (listptr->cu, &cudie, 10739 NULL, NULL, NULL, NULL, 10740 NULL, NULL) == NULL) 10741 printf ("Unknown CU (%s):\n", dwarf_errmsg (-1)); 10742 else 10743 printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie)); 10744 } 10745 10746 if (has_header) 10747 { 10748 uint64_t unit_length; 10749 uint16_t version; 10750 uint16_t padding; 10751 10752 unit_length = read_4ubyte_unaligned_inc (dbg, readp); 10753 if (unlikely (unit_length == 0xffffffff)) 10754 { 10755 if (unlikely (readp > readendp - 8)) 10756 { 10757 invalid_data: 10758 error (0, 0, "Invalid data"); 10759 return; 10760 } 10761 unit_length = read_8ubyte_unaligned_inc (dbg, readp); 10762 offset_size = 8; 10763 } 10764 else 10765 offset_size = 4; 10766 10767 printf ("\n"); 10768 printf (_(" Length: %8" PRIu64 "\n"), 10769 unit_length); 10770 printf (_(" Offset size: %8" PRIu8 "\n"), 10771 offset_size); 10772 10773 /* We need at least 2-bytes (version) + 2-bytes (padding) = 10774 4 bytes to complete the header. And this unit cannot go 10775 beyond the section data. */ 10776 if (readp > readendp - 4 10777 || unit_length < 4 10778 || unit_length > (uint64_t) (readendp - readp)) 10779 goto invalid_data; 10780 10781 next_unitp = readp + unit_length; 10782 10783 version = read_2ubyte_unaligned_inc (dbg, readp); 10784 printf (_(" DWARF version: %8" PRIu16 "\n"), version); 10785 10786 if (version != 5) 10787 { 10788 error (0, 0, _("Unknown version")); 10789 goto next_unit; 10790 } 10791 10792 padding = read_2ubyte_unaligned_inc (dbg, readp); 10793 printf (_(" Padding: %8" PRIx16 "\n"), padding); 10794 10795 if (listptr != NULL 10796 && listptr->offset != (Dwarf_Off) (readp - start)) 10797 { 10798 error (0, 0, "String offsets index doesn't start after header"); 10799 goto next_unit; 10800 } 10801 10802 printf ("\n"); 10803 } 10804 10805 int digits = 1; 10806 size_t offsets = (next_unitp - readp) / offset_size; 10807 while (offsets >= 10) 10808 { 10809 ++digits; 10810 offsets /= 10; 10811 } 10812 10813 unsigned int uidx = 0; 10814 size_t index_offset = readp - (const unsigned char *) data->d_buf; 10815 printf (" Offsets start at 0x%zx:\n", index_offset); 10816 while (readp <= next_unitp - offset_size) 10817 { 10818 Dwarf_Word offset; 10819 if (offset_size == 4) 10820 offset = read_4ubyte_unaligned_inc (dbg, readp); 10821 else 10822 offset = read_8ubyte_unaligned_inc (dbg, readp); 10823 const char *str = dwarf_getstring (dbg, offset, NULL); 10824 printf (" [%*u] [%*" PRIx64 "] \"%s\"\n", 10825 digits, uidx++, (int) offset_size * 2, offset, str ?: "???"); 10826 } 10827 printf ("\n"); 10828 10829 if (readp != next_unitp) 10830 error (0, 0, "extra %zd bytes at end of unit", 10831 (size_t) (next_unitp - readp)); 10832 10833 next_unit: 10834 readp = next_unitp; 10835 } 10836} 10837 10838 10839/* Print the content of the call frame search table section 10840 '.eh_frame_hdr'. */ 10841static void 10842print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)), 10843 Ebl *ebl __attribute__ ((unused)), 10844 GElf_Ehdr *ehdr __attribute__ ((unused)), 10845 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 10846{ 10847 printf (_("\ 10848\nCall frame search table section [%2zu] '.eh_frame_hdr':\n"), 10849 elf_ndxscn (scn)); 10850 10851 Elf_Data *data = elf_rawdata (scn, NULL); 10852 10853 if (unlikely (data == NULL)) 10854 { 10855 error (0, 0, _("cannot get %s content: %s"), 10856 ".eh_frame_hdr", elf_errmsg (-1)); 10857 return; 10858 } 10859 10860 const unsigned char *readp = data->d_buf; 10861 const unsigned char *const dataend = ((unsigned char *) data->d_buf 10862 + data->d_size); 10863 10864 if (unlikely (readp + 4 > dataend)) 10865 { 10866 invalid_data: 10867 error (0, 0, _("invalid data")); 10868 return; 10869 } 10870 10871 unsigned int version = *readp++; 10872 unsigned int eh_frame_ptr_enc = *readp++; 10873 unsigned int fde_count_enc = *readp++; 10874 unsigned int table_enc = *readp++; 10875 10876 printf (" version: %u\n" 10877 " eh_frame_ptr_enc: %#x ", 10878 version, eh_frame_ptr_enc); 10879 print_encoding_base ("", eh_frame_ptr_enc); 10880 printf (" fde_count_enc: %#x ", fde_count_enc); 10881 print_encoding_base ("", fde_count_enc); 10882 printf (" table_enc: %#x ", table_enc); 10883 print_encoding_base ("", table_enc); 10884 10885 uint64_t eh_frame_ptr = 0; 10886 if (eh_frame_ptr_enc != DW_EH_PE_omit) 10887 { 10888 readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr, 10889 dbg); 10890 if (unlikely (readp == NULL)) 10891 goto invalid_data; 10892 10893 printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr); 10894 if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel) 10895 printf (" (offset: %#" PRIx64 ")", 10896 /* +4 because of the 4 byte header of the section. */ 10897 (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr); 10898 10899 putchar_unlocked ('\n'); 10900 } 10901 10902 uint64_t fde_count = 0; 10903 if (fde_count_enc != DW_EH_PE_omit) 10904 { 10905 readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg); 10906 if (unlikely (readp == NULL)) 10907 goto invalid_data; 10908 10909 printf (" fde_count: %" PRIu64 "\n", fde_count); 10910 } 10911 10912 if (fde_count == 0 || table_enc == DW_EH_PE_omit) 10913 return; 10914 10915 puts (" Table:"); 10916 10917 /* Optimize for the most common case. */ 10918 if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4)) 10919 while (fde_count > 0 && readp + 8 <= dataend) 10920 { 10921 int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp); 10922 uint64_t initial_offset = ((uint64_t) shdr->sh_offset 10923 + (int64_t) initial_location); 10924 int32_t address = read_4sbyte_unaligned_inc (dbg, readp); 10925 // XXX Possibly print symbol name or section offset for initial_offset 10926 printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32 10927 " fde=[%6" PRIx64 "]\n", 10928 initial_location, initial_offset, 10929 address, address - (eh_frame_ptr + 4)); 10930 } 10931 else 10932 while (0 && readp < dataend) 10933 { 10934 10935 } 10936} 10937 10938 10939/* Print the content of the exception handling table section 10940 '.eh_frame_hdr'. */ 10941static void 10942print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)), 10943 Ebl *ebl __attribute__ ((unused)), 10944 GElf_Ehdr *ehdr __attribute__ ((unused)), 10945 Elf_Scn *scn, 10946 GElf_Shdr *shdr __attribute__ ((unused)), 10947 Dwarf *dbg __attribute__ ((unused))) 10948{ 10949 printf (_("\ 10950\nException handling table section [%2zu] '.gcc_except_table':\n"), 10951 elf_ndxscn (scn)); 10952 10953 Elf_Data *data = elf_rawdata (scn, NULL); 10954 10955 if (unlikely (data == NULL)) 10956 { 10957 error (0, 0, _("cannot get %s content: %s"), 10958 ".gcc_except_table", elf_errmsg (-1)); 10959 return; 10960 } 10961 10962 const unsigned char *readp = data->d_buf; 10963 const unsigned char *const dataend = readp + data->d_size; 10964 10965 if (unlikely (readp + 1 > dataend)) 10966 { 10967 invalid_data: 10968 error (0, 0, _("invalid data")); 10969 return; 10970 } 10971 unsigned int lpstart_encoding = *readp++; 10972 printf (_(" LPStart encoding: %#x "), lpstart_encoding); 10973 print_encoding_base ("", lpstart_encoding); 10974 if (lpstart_encoding != DW_EH_PE_omit) 10975 { 10976 uint64_t lpstart; 10977 readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg); 10978 printf (" LPStart: %#" PRIx64 "\n", lpstart); 10979 } 10980 10981 if (unlikely (readp + 1 > dataend)) 10982 goto invalid_data; 10983 unsigned int ttype_encoding = *readp++; 10984 printf (_(" TType encoding: %#x "), ttype_encoding); 10985 print_encoding_base ("", ttype_encoding); 10986 const unsigned char *ttype_base = NULL; 10987 if (ttype_encoding != DW_EH_PE_omit) 10988 { 10989 unsigned int ttype_base_offset; 10990 get_uleb128 (ttype_base_offset, readp, dataend); 10991 printf (" TType base offset: %#x\n", ttype_base_offset); 10992 if ((size_t) (dataend - readp) > ttype_base_offset) 10993 ttype_base = readp + ttype_base_offset; 10994 } 10995 10996 if (unlikely (readp + 1 > dataend)) 10997 goto invalid_data; 10998 unsigned int call_site_encoding = *readp++; 10999 printf (_(" Call site encoding: %#x "), call_site_encoding); 11000 print_encoding_base ("", call_site_encoding); 11001 unsigned int call_site_table_len; 11002 get_uleb128 (call_site_table_len, readp, dataend); 11003 11004 const unsigned char *const action_table = readp + call_site_table_len; 11005 if (unlikely (action_table > dataend)) 11006 goto invalid_data; 11007 unsigned int u = 0; 11008 unsigned int max_action = 0; 11009 while (readp < action_table) 11010 { 11011 if (u == 0) 11012 puts (_("\n Call site table:")); 11013 11014 uint64_t call_site_start; 11015 readp = read_encoded (call_site_encoding, readp, dataend, 11016 &call_site_start, dbg); 11017 uint64_t call_site_length; 11018 readp = read_encoded (call_site_encoding, readp, dataend, 11019 &call_site_length, dbg); 11020 uint64_t landing_pad; 11021 readp = read_encoded (call_site_encoding, readp, dataend, 11022 &landing_pad, dbg); 11023 unsigned int action; 11024 get_uleb128 (action, readp, dataend); 11025 max_action = MAX (action, max_action); 11026 printf (_(" [%4u] Call site start: %#" PRIx64 "\n" 11027 " Call site length: %" PRIu64 "\n" 11028 " Landing pad: %#" PRIx64 "\n" 11029 " Action: %u\n"), 11030 u++, call_site_start, call_site_length, landing_pad, action); 11031 } 11032 if (readp != action_table) 11033 goto invalid_data; 11034 11035 unsigned int max_ar_filter = 0; 11036 if (max_action > 0) 11037 { 11038 puts ("\n Action table:"); 11039 11040 size_t maxdata = (size_t) (dataend - action_table); 11041 if (max_action > maxdata || maxdata - max_action < 1) 11042 { 11043 invalid_action_table: 11044 fputs (_(" <INVALID DATA>\n"), stdout); 11045 return; 11046 } 11047 11048 const unsigned char *const action_table_end 11049 = action_table + max_action + 1; 11050 11051 u = 0; 11052 do 11053 { 11054 int ar_filter; 11055 get_sleb128 (ar_filter, readp, action_table_end); 11056 if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter) 11057 max_ar_filter = ar_filter; 11058 int ar_disp; 11059 if (readp >= action_table_end) 11060 goto invalid_action_table; 11061 get_sleb128 (ar_disp, readp, action_table_end); 11062 11063 printf (" [%4u] ar_filter: % d\n" 11064 " ar_disp: % -5d", 11065 u, ar_filter, ar_disp); 11066 if (abs (ar_disp) & 1) 11067 printf (" -> [%4u]\n", u + (ar_disp + 1) / 2); 11068 else if (ar_disp != 0) 11069 puts (" -> ???"); 11070 else 11071 putchar_unlocked ('\n'); 11072 ++u; 11073 } 11074 while (readp < action_table_end); 11075 } 11076 11077 if (max_ar_filter > 0 && ttype_base != NULL) 11078 { 11079 unsigned char dsize; 11080 puts ("\n TType table:"); 11081 11082 // XXX Not *4, size of encoding; 11083 switch (ttype_encoding & 7) 11084 { 11085 case DW_EH_PE_udata2: 11086 case DW_EH_PE_sdata2: 11087 dsize = 2; 11088 break; 11089 case DW_EH_PE_udata4: 11090 case DW_EH_PE_sdata4: 11091 dsize = 4; 11092 break; 11093 case DW_EH_PE_udata8: 11094 case DW_EH_PE_sdata8: 11095 dsize = 8; 11096 break; 11097 default: 11098 dsize = 0; 11099 error (1, 0, _("invalid TType encoding")); 11100 } 11101 11102 if (max_ar_filter 11103 > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize) 11104 goto invalid_data; 11105 11106 readp = ttype_base - max_ar_filter * dsize; 11107 do 11108 { 11109 uint64_t ttype; 11110 readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype, 11111 dbg); 11112 printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype); 11113 } 11114 while (readp < ttype_base); 11115 } 11116} 11117 11118/* Print the content of the '.gdb_index' section. 11119 http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html 11120*/ 11121static void 11122print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl, 11123 GElf_Ehdr *ehdr __attribute__ ((unused)), 11124 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg) 11125{ 11126 printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64 11127 " contains %" PRId64 " bytes :\n"), 11128 elf_ndxscn (scn), section_name (ebl, shdr), 11129 (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size); 11130 11131 Elf_Data *data = elf_rawdata (scn, NULL); 11132 11133 if (unlikely (data == NULL)) 11134 { 11135 error (0, 0, _("cannot get %s content: %s"), 11136 ".gdb_index", elf_errmsg (-1)); 11137 return; 11138 } 11139 11140 // .gdb_index is always in little endian. 11141 Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB }; 11142 dbg = &dummy_dbg; 11143 11144 const unsigned char *readp = data->d_buf; 11145 const unsigned char *const dataend = readp + data->d_size; 11146 11147 if (unlikely (readp + 4 > dataend)) 11148 { 11149 invalid_data: 11150 error (0, 0, _("invalid data")); 11151 return; 11152 } 11153 11154 int32_t vers = read_4ubyte_unaligned (dbg, readp); 11155 printf (_(" Version: %" PRId32 "\n"), vers); 11156 11157 // The only difference between version 4 and version 5 is the 11158 // hash used for generating the table. Version 6 contains symbols 11159 // for inlined functions, older versions didn't. Version 7 adds 11160 // symbol kinds. Version 8 just indicates that it correctly includes 11161 // TUs for symbols. 11162 if (vers < 4 || vers > 8) 11163 { 11164 printf (_(" unknown version, cannot parse section\n")); 11165 return; 11166 } 11167 11168 readp += 4; 11169 if (unlikely (readp + 4 > dataend)) 11170 goto invalid_data; 11171 11172 uint32_t cu_off = read_4ubyte_unaligned (dbg, readp); 11173 printf (_(" CU offset: %#" PRIx32 "\n"), cu_off); 11174 11175 readp += 4; 11176 if (unlikely (readp + 4 > dataend)) 11177 goto invalid_data; 11178 11179 uint32_t tu_off = read_4ubyte_unaligned (dbg, readp); 11180 printf (_(" TU offset: %#" PRIx32 "\n"), tu_off); 11181 11182 readp += 4; 11183 if (unlikely (readp + 4 > dataend)) 11184 goto invalid_data; 11185 11186 uint32_t addr_off = read_4ubyte_unaligned (dbg, readp); 11187 printf (_(" address offset: %#" PRIx32 "\n"), addr_off); 11188 11189 readp += 4; 11190 if (unlikely (readp + 4 > dataend)) 11191 goto invalid_data; 11192 11193 uint32_t sym_off = read_4ubyte_unaligned (dbg, readp); 11194 printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off); 11195 11196 readp += 4; 11197 if (unlikely (readp + 4 > dataend)) 11198 goto invalid_data; 11199 11200 uint32_t const_off = read_4ubyte_unaligned (dbg, readp); 11201 printf (_(" constant offset: %#" PRIx32 "\n"), const_off); 11202 11203 if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf) 11204 < const_off)) 11205 goto invalid_data; 11206 11207 readp = data->d_buf + cu_off; 11208 11209 const unsigned char *nextp = data->d_buf + tu_off; 11210 if (tu_off >= data->d_size) 11211 goto invalid_data; 11212 11213 size_t cu_nr = (nextp - readp) / 16; 11214 11215 printf (_("\n CU list at offset %#" PRIx32 11216 " contains %zu entries:\n"), 11217 cu_off, cu_nr); 11218 11219 size_t n = 0; 11220 while (dataend - readp >= 16 && n < cu_nr) 11221 { 11222 uint64_t off = read_8ubyte_unaligned (dbg, readp); 11223 readp += 8; 11224 11225 uint64_t len = read_8ubyte_unaligned (dbg, readp); 11226 readp += 8; 11227 11228 printf (" [%4zu] start: %0#8" PRIx64 11229 ", length: %5" PRIu64 "\n", n, off, len); 11230 n++; 11231 } 11232 11233 readp = data->d_buf + tu_off; 11234 nextp = data->d_buf + addr_off; 11235 if (addr_off >= data->d_size) 11236 goto invalid_data; 11237 11238 size_t tu_nr = (nextp - readp) / 24; 11239 11240 printf (_("\n TU list at offset %#" PRIx32 11241 " contains %zu entries:\n"), 11242 tu_off, tu_nr); 11243 11244 n = 0; 11245 while (dataend - readp >= 24 && n < tu_nr) 11246 { 11247 uint64_t off = read_8ubyte_unaligned (dbg, readp); 11248 readp += 8; 11249 11250 uint64_t type = read_8ubyte_unaligned (dbg, readp); 11251 readp += 8; 11252 11253 uint64_t sig = read_8ubyte_unaligned (dbg, readp); 11254 readp += 8; 11255 11256 printf (" [%4zu] CU offset: %5" PRId64 11257 ", type offset: %5" PRId64 11258 ", signature: %0#8" PRIx64 "\n", n, off, type, sig); 11259 n++; 11260 } 11261 11262 readp = data->d_buf + addr_off; 11263 nextp = data->d_buf + sym_off; 11264 if (sym_off >= data->d_size) 11265 goto invalid_data; 11266 11267 size_t addr_nr = (nextp - readp) / 20; 11268 11269 printf (_("\n Address list at offset %#" PRIx32 11270 " contains %zu entries:\n"), 11271 addr_off, addr_nr); 11272 11273 n = 0; 11274 while (dataend - readp >= 20 && n < addr_nr) 11275 { 11276 uint64_t low = read_8ubyte_unaligned (dbg, readp); 11277 readp += 8; 11278 11279 uint64_t high = read_8ubyte_unaligned (dbg, readp); 11280 readp += 8; 11281 11282 uint32_t idx = read_4ubyte_unaligned (dbg, readp); 11283 readp += 4; 11284 11285 printf (" [%4zu] ", n); 11286 print_dwarf_addr (dwflmod, 8, low, low); 11287 printf (".."); 11288 print_dwarf_addr (dwflmod, 8, high - 1, high); 11289 printf (", CU index: %5" PRId32 "\n", idx); 11290 n++; 11291 } 11292 11293 const unsigned char *const_start = data->d_buf + const_off; 11294 if (const_off >= data->d_size) 11295 goto invalid_data; 11296 11297 readp = data->d_buf + sym_off; 11298 nextp = const_start; 11299 size_t sym_nr = (nextp - readp) / 8; 11300 11301 printf (_("\n Symbol table at offset %#" PRIx32 11302 " contains %zu slots:\n"), 11303 addr_off, sym_nr); 11304 11305 n = 0; 11306 while (dataend - readp >= 8 && n < sym_nr) 11307 { 11308 uint32_t name = read_4ubyte_unaligned (dbg, readp); 11309 readp += 4; 11310 11311 uint32_t vector = read_4ubyte_unaligned (dbg, readp); 11312 readp += 4; 11313 11314 if (name != 0 || vector != 0) 11315 { 11316 const unsigned char *sym = const_start + name; 11317 if (unlikely ((size_t) (dataend - const_start) < name 11318 || memchr (sym, '\0', dataend - sym) == NULL)) 11319 goto invalid_data; 11320 11321 printf (" [%4zu] symbol: %s, CUs: ", n, sym); 11322 11323 const unsigned char *readcus = const_start + vector; 11324 if (unlikely ((size_t) (dataend - const_start) < vector)) 11325 goto invalid_data; 11326 uint32_t cus = read_4ubyte_unaligned (dbg, readcus); 11327 while (cus--) 11328 { 11329 uint32_t cu_kind, cu, kind; 11330 bool is_static; 11331 readcus += 4; 11332 if (unlikely (readcus + 4 > dataend)) 11333 goto invalid_data; 11334 cu_kind = read_4ubyte_unaligned (dbg, readcus); 11335 cu = cu_kind & ((1 << 24) - 1); 11336 kind = (cu_kind >> 28) & 7; 11337 is_static = cu_kind & (1U << 31); 11338 if (cu > cu_nr - 1) 11339 printf ("%" PRId32 "T", cu - (uint32_t) cu_nr); 11340 else 11341 printf ("%" PRId32, cu); 11342 if (kind != 0) 11343 { 11344 printf (" ("); 11345 switch (kind) 11346 { 11347 case 1: 11348 printf ("type"); 11349 break; 11350 case 2: 11351 printf ("var"); 11352 break; 11353 case 3: 11354 printf ("func"); 11355 break; 11356 case 4: 11357 printf ("other"); 11358 break; 11359 default: 11360 printf ("unknown-0x%" PRIx32, kind); 11361 break; 11362 } 11363 printf (":%c)", (is_static ? 'S' : 'G')); 11364 } 11365 if (cus > 0) 11366 printf (", "); 11367 } 11368 printf ("\n"); 11369 } 11370 n++; 11371 } 11372} 11373 11374/* Returns true and sets split DWARF CU id if there is a split compile 11375 unit in the given Dwarf, and no non-split units are found (before it). */ 11376static bool 11377is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu) 11378{ 11379 Dwarf_CU *cu = NULL; 11380 while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0) 11381 { 11382 uint8_t unit_type; 11383 if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL, 11384 id, NULL, NULL) != 0) 11385 return false; 11386 11387 if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type) 11388 return false; 11389 11390 /* We really only care about the split compile unit, the types 11391 should be fine and self sufficient. Also they don't have an 11392 id that we can match with a skeleton unit. */ 11393 if (unit_type == DW_UT_split_compile) 11394 { 11395 *split_cu = cu; 11396 return true; 11397 } 11398 } 11399 11400 return false; 11401} 11402 11403/* Check that there is one and only one Dwfl_Module, return in arg. */ 11404static int 11405getone_dwflmod (Dwfl_Module *dwflmod, 11406 void **userdata __attribute__ ((unused)), 11407 const char *name __attribute__ ((unused)), 11408 Dwarf_Addr base __attribute__ ((unused)), 11409 void *arg) 11410{ 11411 Dwfl_Module **m = (Dwfl_Module **) arg; 11412 if (*m != NULL) 11413 return DWARF_CB_ABORT; 11414 *m = dwflmod; 11415 return DWARF_CB_OK; 11416} 11417 11418static void 11419print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr) 11420{ 11421 /* Used for skeleton file, if necessary for split DWARF. */ 11422 Dwfl *skel_dwfl = NULL; 11423 Dwfl_Module *skel_mod = NULL; 11424 char *skel_name = NULL; 11425 Dwarf *split_dbg = NULL; 11426 Dwarf_CU *split_cu = NULL; 11427 11428 /* Before we start the real work get a debug context descriptor. */ 11429 Dwarf_Addr dwbias; 11430 Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias); 11431 Dwarf dummy_dbg = 11432 { 11433 .elf = ebl->elf, 11434 .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA] 11435 }; 11436 if (dbg == NULL) 11437 { 11438 if ((print_debug_sections & ~(section_exception|section_frame)) != 0) 11439 error (0, 0, _("cannot get debug context descriptor: %s"), 11440 dwfl_errmsg (-1)); 11441 dbg = &dummy_dbg; 11442 } 11443 else 11444 { 11445 /* If we are asked about a split dwarf (.dwo) file, use the user 11446 provided, or find the corresponding skeleton file. If we got 11447 a skeleton file, replace the given dwflmod and dbg, with one 11448 derived from the skeleton file to provide enough context. */ 11449 uint64_t split_id; 11450 if (is_split_dwarf (dbg, &split_id, &split_cu)) 11451 { 11452 if (dwarf_skeleton != NULL) 11453 skel_name = strdup (dwarf_skeleton); 11454 else 11455 { 11456 /* Replace file.dwo with file.o and see if that matches. */ 11457 const char *fname; 11458 dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, 11459 &fname, NULL); 11460 if (fname != NULL) 11461 { 11462 size_t flen = strlen (fname); 11463 if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0) 11464 { 11465 skel_name = strdup (fname); 11466 if (skel_name != NULL) 11467 { 11468 skel_name[flen - 3] = 'o'; 11469 skel_name[flen - 2] = '\0'; 11470 } 11471 } 11472 } 11473 } 11474 11475 if (skel_name != NULL) 11476 { 11477 int skel_fd = open (skel_name, O_RDONLY); 11478 if (skel_fd == -1) 11479 fprintf (stderr, "Warning: Couldn't open DWARF skeleton file" 11480 " '%s'\n", skel_name); 11481 else 11482 skel_dwfl = create_dwfl (skel_fd, skel_name); 11483 11484 if (skel_dwfl != NULL) 11485 { 11486 if (dwfl_getmodules (skel_dwfl, &getone_dwflmod, 11487 &skel_mod, 0) != 0) 11488 { 11489 fprintf (stderr, "Warning: Bad DWARF skeleton," 11490 " multiple modules '%s'\n", skel_name); 11491 dwfl_end (skel_dwfl); 11492 skel_mod = NULL; 11493 } 11494 } 11495 else if (skel_fd != -1) 11496 fprintf (stderr, "Warning: Couldn't create skeleton dwfl for" 11497 " '%s': %s\n", skel_name, dwfl_errmsg (-1)); 11498 11499 if (skel_mod != NULL) 11500 { 11501 Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias); 11502 if (skel_dbg != NULL) 11503 { 11504 /* First check the skeleton CU DIE, only fetch 11505 the split DIE if we know the id matches to 11506 not unnecessary search for any split DIEs we 11507 don't need. */ 11508 Dwarf_CU *cu = NULL; 11509 while (dwarf_get_units (skel_dbg, cu, &cu, 11510 NULL, NULL, NULL, NULL) == 0) 11511 { 11512 uint8_t unit_type; 11513 uint64_t skel_id; 11514 if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL, 11515 &skel_id, NULL, NULL) == 0 11516 && unit_type == DW_UT_skeleton 11517 && split_id == skel_id) 11518 { 11519 Dwarf_Die subdie; 11520 if (dwarf_cu_info (cu, NULL, NULL, NULL, 11521 &subdie, 11522 NULL, NULL, NULL) == 0 11523 && dwarf_tag (&subdie) != DW_TAG_invalid) 11524 { 11525 split_dbg = dwarf_cu_getdwarf (subdie.cu); 11526 if (split_dbg == NULL) 11527 fprintf (stderr, 11528 "Warning: Couldn't get split_dbg:" 11529 " %s\n", dwarf_errmsg (-1)); 11530 break; 11531 } 11532 else 11533 { 11534 /* Everything matches up, but not 11535 according to libdw. Which means 11536 the user knew better. So... 11537 Terrible hack... We can never 11538 destroy the underlying dwfl 11539 because it would free the wrong 11540 Dwarfs... So we leak memory...*/ 11541 if (cu->split == NULL 11542 && dwarf_skeleton != NULL) 11543 { 11544 do_not_close_dwfl = true; 11545 __libdw_link_skel_split (cu, split_cu); 11546 split_dbg = dwarf_cu_getdwarf (split_cu); 11547 break; 11548 } 11549 else 11550 fprintf (stderr, "Warning: Couldn't get" 11551 " skeleton subdie: %s\n", 11552 dwarf_errmsg (-1)); 11553 } 11554 } 11555 } 11556 if (split_dbg == NULL) 11557 fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id); 11558 } 11559 else 11560 fprintf (stderr, "Warning: Couldn't get skeleton DWARF:" 11561 " %s\n", dwfl_errmsg (-1)); 11562 } 11563 } 11564 11565 if (split_dbg != NULL) 11566 { 11567 dbg = split_dbg; 11568 dwflmod = skel_mod; 11569 } 11570 else if (skel_name == NULL) 11571 fprintf (stderr, 11572 "Warning: split DWARF file, but no skeleton found.\n"); 11573 } 11574 else if (dwarf_skeleton != NULL) 11575 fprintf (stderr, "Warning: DWARF skeleton given," 11576 " but not a split DWARF file\n"); 11577 } 11578 11579 /* Get the section header string table index. */ 11580 size_t shstrndx; 11581 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 11582 error_exit (0, _("cannot get section header string table index")); 11583 11584 /* If the .debug_info section is listed as implicitly required then 11585 we must make sure to handle it before handling any other debug 11586 section. Various other sections depend on the CU DIEs being 11587 scanned (silently) first. */ 11588 bool implicit_info = (implicit_debug_sections & section_info) != 0; 11589 bool explicit_info = (print_debug_sections & section_info) != 0; 11590 if (implicit_info) 11591 { 11592 Elf_Scn *scn = NULL; 11593 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 11594 { 11595 GElf_Shdr shdr_mem; 11596 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 11597 11598 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS) 11599 { 11600 const char *name = elf_strptr (ebl->elf, shstrndx, 11601 shdr->sh_name); 11602 if (name == NULL) 11603 continue; 11604 11605 if (strcmp (name, ".debug_info") == 0 11606 || strcmp (name, ".debug_info.dwo") == 0 11607 || strcmp (name, ".zdebug_info") == 0 11608 || strcmp (name, ".zdebug_info.dwo") == 0 11609 || strcmp (name, ".gnu.debuglto_.debug_info") == 0) 11610 { 11611 print_debug_info_section (dwflmod, ebl, ehdr, 11612 scn, shdr, dbg); 11613 break; 11614 } 11615 } 11616 } 11617 print_debug_sections &= ~section_info; 11618 implicit_debug_sections &= ~section_info; 11619 } 11620 11621 /* Look through all the sections for the debugging sections to print. */ 11622 Elf_Scn *scn = NULL; 11623 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 11624 { 11625 GElf_Shdr shdr_mem; 11626 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 11627 11628 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS) 11629 { 11630 static const struct 11631 { 11632 const char *name; 11633 enum section_e bitmask; 11634 void (*fp) (Dwfl_Module *, Ebl *, 11635 GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *); 11636 } debug_sections[] = 11637 { 11638#define NEW_SECTION(name) \ 11639 { ".debug_" #name, section_##name, print_debug_##name##_section } 11640 NEW_SECTION (abbrev), 11641 NEW_SECTION (addr), 11642 NEW_SECTION (aranges), 11643 NEW_SECTION (frame), 11644 NEW_SECTION (info), 11645 NEW_SECTION (types), 11646 NEW_SECTION (line), 11647 NEW_SECTION (loc), 11648 /* loclists is loc for DWARF5. */ 11649 { ".debug_loclists", section_loc, 11650 print_debug_loclists_section }, 11651 NEW_SECTION (pubnames), 11652 NEW_SECTION (str), 11653 /* A DWARF5 specialised debug string section. */ 11654 { ".debug_line_str", section_str, 11655 print_debug_str_section }, 11656 /* DWARF5 string offsets table. */ 11657 { ".debug_str_offsets", section_str, 11658 print_debug_str_offsets_section }, 11659 NEW_SECTION (macinfo), 11660 NEW_SECTION (macro), 11661 NEW_SECTION (ranges), 11662 /* rnglists is ranges for DWARF5. */ 11663 { ".debug_rnglists", section_ranges, 11664 print_debug_rnglists_section }, 11665 { ".eh_frame", section_frame | section_exception, 11666 print_debug_frame_section }, 11667 { ".eh_frame_hdr", section_frame | section_exception, 11668 print_debug_frame_hdr_section }, 11669 { ".gcc_except_table", section_frame | section_exception, 11670 print_debug_exception_table }, 11671 { ".gdb_index", section_gdb_index, print_gdb_index_section } 11672 }; 11673 const int ndebug_sections = (sizeof (debug_sections) 11674 / sizeof (debug_sections[0])); 11675 const char *name = elf_strptr (ebl->elf, shstrndx, 11676 shdr->sh_name); 11677 if (name == NULL) 11678 continue; 11679 11680 int n; 11681 for (n = 0; n < ndebug_sections; ++n) 11682 { 11683 size_t dbglen = strlen (debug_sections[n].name); 11684 size_t scnlen = strlen (name); 11685 if ((strncmp (name, debug_sections[n].name, dbglen) == 0 11686 && (dbglen == scnlen 11687 || (scnlen == dbglen + 4 11688 && strstr (name, ".dwo") == name + dbglen))) 11689 || (name[0] == '.' && name[1] == 'z' 11690 && debug_sections[n].name[1] == 'd' 11691 && strncmp (&name[2], &debug_sections[n].name[1], 11692 dbglen - 1) == 0 11693 && (scnlen == dbglen + 1 11694 || (scnlen == dbglen + 5 11695 && strstr (name, ".dwo") == name + dbglen + 1))) 11696 || (scnlen > 14 /* .gnu.debuglto_ prefix. */ 11697 && startswith (name, ".gnu.debuglto_") 11698 && strcmp (&name[14], debug_sections[n].name) == 0) 11699) 11700 { 11701 if ((print_debug_sections | implicit_debug_sections) 11702 & debug_sections[n].bitmask) 11703 debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg); 11704 break; 11705 } 11706 } 11707 } 11708 } 11709 11710 dwfl_end (skel_dwfl); 11711 free (skel_name); 11712 11713 /* Turn implicit and/or explicit back on in case we go over another file. */ 11714 if (implicit_info) 11715 implicit_debug_sections |= section_info; 11716 if (explicit_info) 11717 print_debug_sections |= section_info; 11718 11719 reset_listptr (&known_locsptr); 11720 reset_listptr (&known_loclistsptr); 11721 reset_listptr (&known_rangelistptr); 11722 reset_listptr (&known_rnglistptr); 11723 reset_listptr (&known_addrbases); 11724 reset_listptr (&known_stroffbases); 11725} 11726 11727 11728#define ITEM_INDENT 4 11729#define WRAP_COLUMN 75 11730 11731/* Print "NAME: FORMAT", wrapping when output text would make the line 11732 exceed WRAP_COLUMN. Unpadded numbers look better for the core items 11733 but this function is also used for registers which should be printed 11734 aligned. Fortunately registers output uses fixed fields width (such 11735 as %11d) for the alignment. 11736 11737 Line breaks should not depend on the particular values although that 11738 may happen in some cases of the core items. */ 11739 11740static unsigned int 11741__attribute__ ((format (printf, 6, 7))) 11742print_core_item (unsigned int colno, char sep, unsigned int wrap, 11743 size_t name_width, const char *name, const char *format, ...) 11744{ 11745 size_t len = strlen (name); 11746 if (name_width < len) 11747 name_width = len; 11748 11749 char *out; 11750 va_list ap; 11751 va_start (ap, format); 11752 int out_len = vasprintf (&out, format, ap); 11753 va_end (ap); 11754 if (out_len == -1) 11755 error_exit (0, _("memory exhausted")); 11756 11757 size_t n = name_width + sizeof ": " - 1 + out_len; 11758 11759 if (colno == 0) 11760 { 11761 printf ("%*s", ITEM_INDENT, ""); 11762 colno = ITEM_INDENT + n; 11763 } 11764 else if (colno + 2 + n < wrap) 11765 { 11766 printf ("%c ", sep); 11767 colno += 2 + n; 11768 } 11769 else 11770 { 11771 printf ("\n%*s", ITEM_INDENT, ""); 11772 colno = ITEM_INDENT + n; 11773 } 11774 11775 printf ("%s: %*s%s", name, (int) (name_width - len), "", out); 11776 11777 free (out); 11778 11779 return colno; 11780} 11781 11782static const void * 11783convert (Elf *core, Elf_Type type, uint_fast16_t count, 11784 void *value, const void *data, size_t size) 11785{ 11786 Elf_Data valuedata = 11787 { 11788 .d_type = type, 11789 .d_buf = value, 11790 .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT), 11791 .d_version = EV_CURRENT, 11792 }; 11793 Elf_Data indata = 11794 { 11795 .d_type = type, 11796 .d_buf = (void *) data, 11797 .d_size = valuedata.d_size, 11798 .d_version = EV_CURRENT, 11799 }; 11800 11801 Elf_Data *d = (gelf_getclass (core) == ELFCLASS32 11802 ? elf32_xlatetom : elf64_xlatetom) 11803 (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]); 11804 if (d == NULL) 11805 error_exit (0, _("cannot convert core note data: %s"), 11806 elf_errmsg (-1)); 11807 11808 return data + indata.d_size; 11809} 11810 11811typedef uint8_t GElf_Byte; 11812 11813static unsigned int 11814handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc, 11815 unsigned int colno, size_t *repeated_size) 11816{ 11817 uint_fast16_t count = item->count ?: 1; 11818 /* Ebl_Core_Item count is always a small number. 11819 Make sure the backend didn't put in some large bogus value. */ 11820 assert (count < 128); 11821 11822#define TYPES \ 11823 DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \ 11824 DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \ 11825 DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \ 11826 DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \ 11827 DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \ 11828 DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64) 11829 11830#define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name 11831 typedef union { TYPES; } value_t; 11832 void *data = alloca (count * sizeof (value_t)); 11833#undef DO_TYPE 11834 11835#define DO_TYPE(NAME, Name, hex, dec) \ 11836 GElf_##Name *value_##Name __attribute__((unused)) = data 11837 TYPES; 11838#undef DO_TYPE 11839 11840 size_t size = gelf_fsize (core, item->type, count, EV_CURRENT); 11841 size_t convsize = size; 11842 if (repeated_size != NULL) 11843 { 11844 if (*repeated_size > size && (item->format == 'b' || item->format == 'B')) 11845 { 11846 data = alloca (*repeated_size); 11847 count *= *repeated_size / size; 11848 convsize = count * size; 11849 *repeated_size -= convsize; 11850 } 11851 else if (item->count != 0 || item->format != '\n') 11852 *repeated_size -= size; 11853 } 11854 11855 convert (core, item->type, count, data, desc + item->offset, convsize); 11856 11857 Elf_Type type = item->type; 11858 if (type == ELF_T_ADDR) 11859 type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD; 11860 11861 switch (item->format) 11862 { 11863 case 'd': 11864 assert (count == 1); 11865 switch (type) 11866 { 11867#define DO_TYPE(NAME, Name, hex, dec) \ 11868 case ELF_T_##NAME: \ 11869 colno = print_core_item (colno, ',', WRAP_COLUMN, \ 11870 0, item->name, dec, value_##Name[0]); \ 11871 break 11872 TYPES; 11873#undef DO_TYPE 11874 default: 11875 abort (); 11876 } 11877 break; 11878 11879 case 'x': 11880 assert (count == 1); 11881 switch (type) 11882 { 11883#define DO_TYPE(NAME, Name, hex, dec) \ 11884 case ELF_T_##NAME: \ 11885 colno = print_core_item (colno, ',', WRAP_COLUMN, \ 11886 0, item->name, hex, value_##Name[0]); \ 11887 break 11888 TYPES; 11889#undef DO_TYPE 11890 default: 11891 abort (); 11892 } 11893 break; 11894 11895 case 'b': 11896 case 'B': 11897 assert (size % sizeof (unsigned int) == 0); 11898 unsigned int nbits = count * size * 8; 11899 unsigned int pop = 0; 11900 for (const unsigned int *i = data; (void *) i < data + count * size; ++i) 11901 pop += __builtin_popcount (*i); 11902 bool negate = pop > nbits / 2; 11903 const unsigned int bias = item->format == 'b'; 11904 11905 { 11906 char printed[(negate ? nbits - pop : pop) * 16 + 1]; 11907 char *p = printed; 11908 *p = '\0'; 11909 11910 if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int)) 11911 { 11912 assert (size == sizeof (unsigned int) * 2); 11913 for (unsigned int *i = data; 11914 (void *) i < data + count * size; i += 2) 11915 { 11916 unsigned int w = i[1]; 11917 i[1] = i[0]; 11918 i[0] = w; 11919 } 11920 } 11921 11922 unsigned int lastbit = 0; 11923 unsigned int run = 0; 11924 for (const unsigned int *i = data; 11925 (void *) i < data + count * size; ++i) 11926 { 11927 unsigned int bit = ((void *) i - data) * 8; 11928 unsigned int w = negate ? ~*i : *i; 11929 while (w != 0) 11930 { 11931 /* Note that a right shift equal to (or greater than) 11932 the number of bits of w is undefined behaviour. In 11933 particular when the least significant bit is bit 32 11934 (w = 0x8000000) then w >>= n is undefined. So 11935 explicitly handle that case separately. */ 11936 unsigned int n = ffs (w); 11937 if (n < sizeof (w) * 8) 11938 w >>= n; 11939 else 11940 w = 0; 11941 bit += n; 11942 11943 if (lastbit != 0 && lastbit + 1 == bit) 11944 ++run; 11945 else 11946 { 11947 if (lastbit == 0) 11948 p += sprintf (p, "%u", bit - bias); 11949 else if (run == 0) 11950 p += sprintf (p, ",%u", bit - bias); 11951 else 11952 p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias); 11953 run = 0; 11954 } 11955 11956 lastbit = bit; 11957 } 11958 } 11959 if (lastbit > 0 && run > 0 && lastbit + 1 != nbits) 11960 p += sprintf (p, "-%u", lastbit - bias); 11961 11962 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name, 11963 negate ? "~<%s>" : "<%s>", printed); 11964 } 11965 break; 11966 11967 case 'T': 11968 case (char) ('T'|0x80): 11969 assert (count == 2); 11970 Dwarf_Word sec; 11971 Dwarf_Word usec; 11972 switch (type) 11973 { 11974#define DO_TYPE(NAME, Name, hex, dec) \ 11975 case ELF_T_##NAME: \ 11976 sec = value_##Name[0]; \ 11977 usec = value_##Name[1]; \ 11978 break 11979 TYPES; 11980#undef DO_TYPE 11981 default: 11982 abort (); 11983 } 11984 if (unlikely (item->format == (char) ('T'|0x80))) 11985 { 11986 /* This is a hack for an ill-considered 64-bit ABI where 11987 tv_usec is actually a 32-bit field with 32 bits of padding 11988 rounding out struct timeval. We've already converted it as 11989 a 64-bit field. For little-endian, this just means the 11990 high half is the padding; it's presumably zero, but should 11991 be ignored anyway. For big-endian, it means the 32-bit 11992 field went into the high half of USEC. */ 11993 GElf_Ehdr ehdr_mem; 11994 GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem); 11995 if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)) 11996 usec >>= 32; 11997 else 11998 usec &= UINT32_MAX; 11999 } 12000 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name, 12001 "%" PRIu64 ".%.6" PRIu64, sec, usec); 12002 break; 12003 12004 case 'c': 12005 assert (count == 1); 12006 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name, 12007 "%c", value_Byte[0]); 12008 break; 12009 12010 case 's': 12011 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name, 12012 "%.*s", (int) count, value_Byte); 12013 break; 12014 12015 case '\n': 12016 /* This is a list of strings separated by '\n'. */ 12017 assert (item->count == 0); 12018 assert (repeated_size != NULL); 12019 assert (item->name == NULL); 12020 if (unlikely (item->offset >= *repeated_size)) 12021 break; 12022 12023 const char *s = desc + item->offset; 12024 size = *repeated_size - item->offset; 12025 *repeated_size = 0; 12026 while (size > 0) 12027 { 12028 const char *eol = memchr (s, '\n', size); 12029 int len = size; 12030 if (eol != NULL) 12031 len = eol - s; 12032 printf ("%*s%.*s\n", ITEM_INDENT, "", len, s); 12033 if (eol == NULL) 12034 break; 12035 size -= eol + 1 - s; 12036 s = eol + 1; 12037 } 12038 12039 colno = WRAP_COLUMN; 12040 break; 12041 12042 case 'h': 12043 break; 12044 12045 default: 12046 error (0, 0, "XXX not handling format '%c' for %s", 12047 item->format, item->name); 12048 break; 12049 } 12050 12051#undef TYPES 12052 12053 return colno; 12054} 12055 12056 12057/* Sort items by group, and by layout offset within each group. */ 12058static int 12059compare_core_items (const void *a, const void *b) 12060{ 12061 const Ebl_Core_Item *const *p1 = a; 12062 const Ebl_Core_Item *const *p2 = b; 12063 const Ebl_Core_Item *item1 = *p1; 12064 const Ebl_Core_Item *item2 = *p2; 12065 12066 return ((item1->group == item2->group ? 0 12067 : strcmp (item1->group, item2->group)) 12068 ?: (int) item1->offset - (int) item2->offset); 12069} 12070 12071/* Sort item groups by layout offset of the first item in the group. */ 12072static int 12073compare_core_item_groups (const void *a, const void *b) 12074{ 12075 const Ebl_Core_Item *const *const *p1 = a; 12076 const Ebl_Core_Item *const *const *p2 = b; 12077 const Ebl_Core_Item *const *group1 = *p1; 12078 const Ebl_Core_Item *const *group2 = *p2; 12079 const Ebl_Core_Item *item1 = *group1; 12080 const Ebl_Core_Item *item2 = *group2; 12081 12082 return (int) item1->offset - (int) item2->offset; 12083} 12084 12085static unsigned int 12086handle_core_items (Elf *core, const void *desc, size_t descsz, 12087 const Ebl_Core_Item *items, size_t nitems) 12088{ 12089 if (nitems == 0) 12090 return 0; 12091 unsigned int colno = 0; 12092 12093 /* FORMAT '\n' makes sense to be present only as a single item as it 12094 processes all the data of a note. FORMATs 'b' and 'B' have a special case 12095 if present as a single item but they can be also processed with other 12096 items below. */ 12097 if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b' 12098 || items[0].format == 'B')) 12099 { 12100 assert (items[0].offset == 0); 12101 size_t size = descsz; 12102 colno = handle_core_item (core, items, desc, colno, &size); 12103 /* If SIZE is not zero here there is some remaining data. But we do not 12104 know how to process it anyway. */ 12105 return colno; 12106 } 12107 for (size_t i = 0; i < nitems; ++i) 12108 assert (items[i].format != '\n'); 12109 12110 /* Sort to collect the groups together. */ 12111 const Ebl_Core_Item *sorted_items[nitems]; 12112 for (size_t i = 0; i < nitems; ++i) 12113 sorted_items[i] = &items[i]; 12114 qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items); 12115 12116 /* Collect the unique groups and sort them. */ 12117 const Ebl_Core_Item **groups[nitems]; 12118 groups[0] = &sorted_items[0]; 12119 size_t ngroups = 1; 12120 for (size_t i = 1; i < nitems; ++i) 12121 if (sorted_items[i]->group != sorted_items[i - 1]->group 12122 && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group)) 12123 groups[ngroups++] = &sorted_items[i]; 12124 qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups); 12125 12126 /* Write out all the groups. */ 12127 const void *last = desc; 12128 do 12129 { 12130 for (size_t i = 0; i < ngroups; ++i) 12131 { 12132 for (const Ebl_Core_Item **item = groups[i]; 12133 (item < &sorted_items[nitems] 12134 && ((*item)->group == groups[i][0]->group 12135 || !strcmp ((*item)->group, groups[i][0]->group))); 12136 ++item) 12137 colno = handle_core_item (core, *item, desc, colno, NULL); 12138 12139 /* Force a line break at the end of the group. */ 12140 colno = WRAP_COLUMN; 12141 } 12142 12143 if (descsz == 0) 12144 break; 12145 12146 /* This set of items consumed a certain amount of the note's data. 12147 If there is more data there, we have another unit of the same size. 12148 Loop to print that out too. */ 12149 const Ebl_Core_Item *item = &items[nitems - 1]; 12150 size_t eltsz = item->offset + gelf_fsize (core, item->type, 12151 item->count ?: 1, EV_CURRENT); 12152 12153 int reps = -1; 12154 do 12155 { 12156 ++reps; 12157 desc += eltsz; 12158 descsz -= eltsz; 12159 } 12160 while (descsz >= eltsz && !memcmp (desc, last, eltsz)); 12161 12162 if (reps == 1) 12163 { 12164 /* For just one repeat, print it unabridged twice. */ 12165 desc -= eltsz; 12166 descsz += eltsz; 12167 } 12168 else if (reps > 1) 12169 printf (_("\n%*s... <repeats %u more times> ..."), 12170 ITEM_INDENT, "", reps); 12171 12172 last = desc; 12173 } 12174 while (descsz > 0); 12175 12176 return colno; 12177} 12178 12179static unsigned int 12180handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc, 12181 unsigned int colno) 12182{ 12183 desc += regloc->offset; 12184 12185 abort (); /* XXX */ 12186 return colno; 12187} 12188 12189 12190static unsigned int 12191handle_core_register (Ebl *ebl, Elf *core, int maxregname, 12192 const Ebl_Register_Location *regloc, const void *desc, 12193 unsigned int colno) 12194{ 12195 if (regloc->bits % 8 != 0) 12196 return handle_bit_registers (regloc, desc, colno); 12197 12198 desc += regloc->offset; 12199 12200 for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg) 12201 { 12202 char name[REGNAMESZ]; 12203 int bits; 12204 int type; 12205 register_info (ebl, reg, regloc, name, &bits, &type); 12206 12207#define TYPES \ 12208 BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \ 12209 BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \ 12210 BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \ 12211 BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64) 12212 12213#define BITS(bits, xtype, sfmt, ufmt) \ 12214 uint##bits##_t b##bits; int##bits##_t b##bits##s 12215 union { TYPES; uint64_t b128[2]; } value; 12216#undef BITS 12217 12218 switch (type) 12219 { 12220 case DW_ATE_unsigned: 12221 case DW_ATE_signed: 12222 case DW_ATE_address: 12223 switch (bits) 12224 { 12225#define BITS(bits, xtype, sfmt, ufmt) \ 12226 case bits: \ 12227 desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \ 12228 if (type == DW_ATE_signed) \ 12229 colno = print_core_item (colno, ' ', WRAP_COLUMN, \ 12230 maxregname, name, \ 12231 sfmt, value.b##bits##s); \ 12232 else \ 12233 colno = print_core_item (colno, ' ', WRAP_COLUMN, \ 12234 maxregname, name, \ 12235 ufmt, value.b##bits); \ 12236 break 12237 12238 TYPES; 12239 12240 case 128: 12241 assert (type == DW_ATE_unsigned); 12242 desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0); 12243 int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB; 12244 colno = print_core_item (colno, ' ', WRAP_COLUMN, 12245 maxregname, name, 12246 "0x%.16" PRIx64 "%.16" PRIx64, 12247 value.b128[!be], value.b128[be]); 12248 break; 12249 12250 default: 12251 abort (); 12252#undef BITS 12253 } 12254 break; 12255 12256 default: 12257 /* Print each byte in hex, the whole thing in native byte order. */ 12258 assert (bits % 8 == 0); 12259 const uint8_t *bytes = desc; 12260 desc += bits / 8; 12261 char hex[bits / 4 + 1]; 12262 hex[bits / 4] = '\0'; 12263 int incr = 1; 12264 if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB) 12265 { 12266 bytes += bits / 8 - 1; 12267 incr = -1; 12268 } 12269 size_t idx = 0; 12270 for (char *h = hex; bits > 0; bits -= 8, idx += incr) 12271 { 12272 *h++ = "0123456789abcdef"[bytes[idx] >> 4]; 12273 *h++ = "0123456789abcdef"[bytes[idx] & 0xf]; 12274 } 12275 colno = print_core_item (colno, ' ', WRAP_COLUMN, 12276 maxregname, name, "0x%s", hex); 12277 break; 12278 } 12279 desc += regloc->pad; 12280 12281#undef TYPES 12282 } 12283 12284 return colno; 12285} 12286 12287 12288struct register_info 12289{ 12290 const Ebl_Register_Location *regloc; 12291 const char *set; 12292 char name[REGNAMESZ]; 12293 int regno; 12294 int bits; 12295 int type; 12296}; 12297 12298static int 12299register_bitpos (const struct register_info *r) 12300{ 12301 return (r->regloc->offset * 8 12302 + ((r->regno - r->regloc->regno) 12303 * (r->regloc->bits + r->regloc->pad * 8))); 12304} 12305 12306static int 12307compare_sets_by_info (const struct register_info *r1, 12308 const struct register_info *r2) 12309{ 12310 return ((int) r2->bits - (int) r1->bits 12311 ?: register_bitpos (r1) - register_bitpos (r2)); 12312} 12313 12314/* Sort registers by set, and by size and layout offset within each set. */ 12315static int 12316compare_registers (const void *a, const void *b) 12317{ 12318 const struct register_info *r1 = a; 12319 const struct register_info *r2 = b; 12320 12321 /* Unused elements sort last. */ 12322 if (r1->regloc == NULL) 12323 return r2->regloc == NULL ? 0 : 1; 12324 if (r2->regloc == NULL) 12325 return -1; 12326 12327 return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set)) 12328 ?: compare_sets_by_info (r1, r2)); 12329} 12330 12331/* Sort register sets by layout offset of the first register in the set. */ 12332static int 12333compare_register_sets (const void *a, const void *b) 12334{ 12335 const struct register_info *const *p1 = a; 12336 const struct register_info *const *p2 = b; 12337 return compare_sets_by_info (*p1, *p2); 12338} 12339 12340static inline bool 12341same_set (const struct register_info *a, 12342 const struct register_info *b, 12343 const struct register_info *regs, 12344 size_t maxnreg) 12345{ 12346 return (a < ®s[maxnreg] && a->regloc != NULL 12347 && b < ®s[maxnreg] && b->regloc != NULL 12348 && a->bits == b->bits 12349 && (a->set == b->set || !strcmp (a->set, b->set))); 12350} 12351 12352static unsigned int 12353handle_core_registers (Ebl *ebl, Elf *core, const void *desc, 12354 const Ebl_Register_Location *reglocs, size_t nregloc) 12355{ 12356 if (nregloc == 0) 12357 return 0; 12358 12359 ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL); 12360 if (maxnreg <= 0) 12361 { 12362 for (size_t i = 0; i < nregloc; ++i) 12363 if (maxnreg < reglocs[i].regno + reglocs[i].count) 12364 maxnreg = reglocs[i].regno + reglocs[i].count; 12365 assert (maxnreg > 0); 12366 } 12367 12368 struct register_info regs[maxnreg]; 12369 memset (regs, 0, sizeof regs); 12370 12371 /* Sort to collect the sets together. */ 12372 int maxreg = 0; 12373 for (size_t i = 0; i < nregloc; ++i) 12374 for (int reg = reglocs[i].regno; 12375 reg < reglocs[i].regno + reglocs[i].count; 12376 ++reg) 12377 { 12378 assert (reg < maxnreg); 12379 if (reg > maxreg) 12380 maxreg = reg; 12381 struct register_info *info = ®s[reg]; 12382 info->regloc = ®locs[i]; 12383 info->regno = reg; 12384 info->set = register_info (ebl, reg, ®locs[i], 12385 info->name, &info->bits, &info->type); 12386 } 12387 qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers); 12388 12389 /* Collect the unique sets and sort them. */ 12390 struct register_info *sets[maxreg + 1]; 12391 sets[0] = ®s[0]; 12392 size_t nsets = 1; 12393 for (int i = 1; i <= maxreg; ++i) 12394 if (regs[i].regloc != NULL 12395 && !same_set (®s[i], ®s[i - 1], regs, maxnreg)) 12396 sets[nsets++] = ®s[i]; 12397 qsort (sets, nsets, sizeof sets[0], &compare_register_sets); 12398 12399 /* Write out all the sets. */ 12400 unsigned int colno = 0; 12401 for (size_t i = 0; i < nsets; ++i) 12402 { 12403 /* Find the longest name of a register in this set. */ 12404 size_t maxname = 0; 12405 const struct register_info *end; 12406 for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end) 12407 { 12408 size_t len = strlen (end->name); 12409 if (len > maxname) 12410 maxname = len; 12411 } 12412 12413 for (const struct register_info *reg = sets[i]; 12414 reg < end; 12415 reg += reg->regloc->count ?: 1) 12416 colno = handle_core_register (ebl, core, maxname, 12417 reg->regloc, desc, colno); 12418 12419 /* Force a line break at the end of the group. */ 12420 colno = WRAP_COLUMN; 12421 } 12422 12423 return colno; 12424} 12425 12426static void 12427handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos) 12428{ 12429 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV); 12430 if (data == NULL) 12431 elf_error: 12432 error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1)); 12433 12434 const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT); 12435 for (size_t i = 0; i < nauxv; ++i) 12436 { 12437 GElf_auxv_t av_mem; 12438 GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem); 12439 if (av == NULL) 12440 goto elf_error; 12441 12442 const char *name; 12443 const char *fmt; 12444 if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0) 12445 { 12446 /* Unknown type. */ 12447 if (av->a_un.a_val == 0) 12448 printf (" %" PRIu64 "\n", av->a_type); 12449 else 12450 printf (" %" PRIu64 ": %#" PRIx64 "\n", 12451 av->a_type, av->a_un.a_val); 12452 } 12453 else 12454 switch (fmt[0]) 12455 { 12456 case '\0': /* Normally zero. */ 12457 if (av->a_un.a_val == 0) 12458 { 12459 printf (" %s\n", name); 12460 break; 12461 } 12462 FALLTHROUGH; 12463 case 'x': /* hex */ 12464 case 'p': /* address */ 12465 case 's': /* address of string */ 12466 printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val); 12467 break; 12468 case 'u': 12469 printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val); 12470 break; 12471 case 'd': 12472 printf (" %s: %" PRId64 "\n", name, av->a_un.a_val); 12473 break; 12474 12475 case 'b': 12476 printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val); 12477 GElf_Xword bit = 1; 12478 const char *pfx = "<"; 12479 for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1) 12480 { 12481 if (av->a_un.a_val & bit) 12482 { 12483 printf ("%s%s", pfx, p); 12484 pfx = " "; 12485 } 12486 bit <<= 1; 12487 } 12488 printf (">\n"); 12489 break; 12490 12491 default: 12492 abort (); 12493 } 12494 } 12495} 12496 12497static bool 12498buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz) 12499{ 12500 return ptr < end && (size_t) (end - ptr) >= sz; 12501} 12502 12503static bool 12504buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end, 12505 int *retp) 12506{ 12507 if (! buf_has_data (*ptrp, end, 4)) 12508 return false; 12509 12510 *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4); 12511 return true; 12512} 12513 12514static bool 12515buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end, 12516 uint64_t *retp) 12517{ 12518 size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT); 12519 if (! buf_has_data (*ptrp, end, sz)) 12520 return false; 12521 12522 union 12523 { 12524 uint64_t u64; 12525 uint32_t u32; 12526 } u; 12527 12528 *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz); 12529 12530 if (sz == 4) 12531 *retp = u.u32; 12532 else 12533 *retp = u.u64; 12534 return true; 12535} 12536 12537static void 12538handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos) 12539{ 12540 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE); 12541 if (data == NULL) 12542 error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1)); 12543 12544 unsigned char const *ptr = data->d_buf; 12545 unsigned char const *const end = data->d_buf + data->d_size; 12546 12547 /* Siginfo head is three ints: signal number, error number, origin 12548 code. */ 12549 int si_signo, si_errno, si_code; 12550 if (! buf_read_int (core, &ptr, end, &si_signo) 12551 || ! buf_read_int (core, &ptr, end, &si_errno) 12552 || ! buf_read_int (core, &ptr, end, &si_code)) 12553 { 12554 fail: 12555 printf (" Not enough data in NT_SIGINFO note.\n"); 12556 return; 12557 } 12558 12559 /* Next is a pointer-aligned union of structures. On 64-bit 12560 machines, that implies a word of padding. */ 12561 if (gelf_getclass (core) == ELFCLASS64) 12562 ptr += 4; 12563 12564 printf (" si_signo: %d, si_errno: %d, si_code: %d\n", 12565 si_signo, si_errno, si_code); 12566 12567 if (si_code > 0) 12568 switch (si_signo) 12569 { 12570 case CORE_SIGILL: 12571 case CORE_SIGFPE: 12572 case CORE_SIGSEGV: 12573 case CORE_SIGBUS: 12574 { 12575 uint64_t addr; 12576 if (! buf_read_ulong (core, &ptr, end, &addr)) 12577 goto fail; 12578 printf (" fault address: %#" PRIx64 "\n", addr); 12579 break; 12580 } 12581 default: 12582 ; 12583 } 12584 else if (si_code == CORE_SI_USER) 12585 { 12586 int pid, uid; 12587 if (! buf_read_int (core, &ptr, end, &pid) 12588 || ! buf_read_int (core, &ptr, end, &uid)) 12589 goto fail; 12590 printf (" sender PID: %d, sender UID: %d\n", pid, uid); 12591 } 12592} 12593 12594static void 12595handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos) 12596{ 12597 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE); 12598 if (data == NULL) 12599 error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1)); 12600 12601 unsigned char const *ptr = data->d_buf; 12602 unsigned char const *const end = data->d_buf + data->d_size; 12603 12604 uint64_t count, page_size; 12605 if (! buf_read_ulong (core, &ptr, end, &count) 12606 || ! buf_read_ulong (core, &ptr, end, &page_size)) 12607 { 12608 fail: 12609 printf (" Not enough data in NT_FILE note.\n"); 12610 return; 12611 } 12612 12613 size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT); 12614 uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize); 12615 if (count > maxcount) 12616 goto fail; 12617 12618 /* Where file names are stored. */ 12619 unsigned char const *const fstart = ptr + 3 * count * addrsize; 12620 char const *fptr = (char *) fstart; 12621 12622 printf (" %" PRId64 " files:\n", count); 12623 for (uint64_t i = 0; i < count; ++i) 12624 { 12625 uint64_t mstart, mend, moffset; 12626 if (! buf_read_ulong (core, &ptr, fstart, &mstart) 12627 || ! buf_read_ulong (core, &ptr, fstart, &mend) 12628 || ! buf_read_ulong (core, &ptr, fstart, &moffset)) 12629 goto fail; 12630 12631 const char *fnext = memchr (fptr, '\0', (char *) end - fptr); 12632 if (fnext == NULL) 12633 goto fail; 12634 12635 int ct = printf (" %08" PRIx64 "-%08" PRIx64 12636 " %08" PRIx64 " %" PRId64, 12637 mstart, mend, moffset * page_size, mend - mstart); 12638 printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr); 12639 12640 fptr = fnext + 1; 12641 } 12642} 12643 12644static void 12645handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr, 12646 const char *name, const void *desc) 12647{ 12648 GElf_Word regs_offset; 12649 size_t nregloc; 12650 const Ebl_Register_Location *reglocs; 12651 size_t nitems; 12652 const Ebl_Core_Item *items; 12653 12654 if (! ebl_core_note (ebl, nhdr, name, desc, 12655 ®s_offset, &nregloc, ®locs, &nitems, &items)) 12656 return; 12657 12658 /* Pass 0 for DESCSZ when there are registers in the note, 12659 so that the ITEMS array does not describe the whole thing. 12660 For non-register notes, the actual descsz might be a multiple 12661 of the unit size, not just exactly the unit size. */ 12662 unsigned int colno = handle_core_items (ebl->elf, desc, 12663 nregloc == 0 ? nhdr->n_descsz : 0, 12664 items, nitems); 12665 if (colno != 0) 12666 putchar_unlocked ('\n'); 12667 12668 colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset, 12669 reglocs, nregloc); 12670 if (colno != 0) 12671 putchar_unlocked ('\n'); 12672} 12673 12674static void 12675handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr, 12676 GElf_Off start, Elf_Data *data) 12677{ 12678 fputs_unlocked (_(" Owner Data size Type\n"), stdout); 12679 12680 if (data == NULL) 12681 goto bad_note; 12682 12683 size_t offset = 0; 12684 GElf_Nhdr nhdr; 12685 size_t name_offset; 12686 size_t desc_offset; 12687 while (offset < data->d_size 12688 && (offset = gelf_getnote (data, offset, 12689 &nhdr, &name_offset, &desc_offset)) > 0) 12690 { 12691 const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset; 12692 const char *desc = data->d_buf + desc_offset; 12693 12694 /* GNU Build Attributes are weird, they store most of their data 12695 into the owner name field. Extract just the owner name 12696 prefix here, then use the rest later as data. */ 12697 bool is_gnu_build_attr 12698 = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX); 12699 const char *print_name = (is_gnu_build_attr 12700 ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name); 12701 size_t print_namesz = (is_gnu_build_attr 12702 ? strlen (print_name) : nhdr.n_namesz); 12703 12704 char buf[100]; 12705 char buf2[100]; 12706 printf (_(" %-13.*s %9" PRId32 " %s\n"), 12707 (int) print_namesz, print_name, nhdr.n_descsz, 12708 ehdr->e_type == ET_CORE 12709 ? ebl_core_note_type_name (ebl, nhdr.n_type, 12710 buf, sizeof (buf)) 12711 : ebl_object_note_type_name (ebl, name, nhdr.n_type, 12712 nhdr.n_descsz, 12713 buf2, sizeof (buf2))); 12714 12715 /* Filter out invalid entries. */ 12716 if (memchr (name, '\0', nhdr.n_namesz) != NULL 12717 /* XXX For now help broken Linux kernels. */ 12718 || 1) 12719 { 12720 if (ehdr->e_type == ET_CORE) 12721 { 12722 if (nhdr.n_type == NT_AUXV 12723 && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */ 12724 || (nhdr.n_namesz == 5 && name[4] == '\0')) 12725 && !memcmp (name, "CORE", 4)) 12726 handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz, 12727 start + desc_offset); 12728 else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0) 12729 switch (nhdr.n_type) 12730 { 12731 case NT_SIGINFO: 12732 handle_siginfo_note (ebl->elf, nhdr.n_descsz, 12733 start + desc_offset); 12734 break; 12735 12736 case NT_FILE: 12737 handle_file_note (ebl->elf, nhdr.n_descsz, 12738 start + desc_offset); 12739 break; 12740 12741 default: 12742 handle_core_note (ebl, &nhdr, name, desc); 12743 } 12744 else 12745 handle_core_note (ebl, &nhdr, name, desc); 12746 } 12747 else 12748 ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type, 12749 nhdr.n_descsz, desc); 12750 } 12751 } 12752 12753 if (offset == data->d_size) 12754 return; 12755 12756 bad_note: 12757 error (0, 0, 12758 _("cannot get content of note: %s"), 12759 data != NULL ? "garbage data" : elf_errmsg (-1)); 12760} 12761 12762static void 12763handle_notes (Ebl *ebl, GElf_Ehdr *ehdr) 12764{ 12765 /* If we have section headers, just look for SHT_NOTE sections. 12766 In a debuginfo file, the program headers are not reliable. */ 12767 if (shnum != 0) 12768 { 12769 /* Get the section header string table index. */ 12770 size_t shstrndx; 12771 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0) 12772 error_exit (0, _("cannot get section header string table index")); 12773 12774 Elf_Scn *scn = NULL; 12775 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 12776 { 12777 GElf_Shdr shdr_mem; 12778 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 12779 12780 if (shdr == NULL || shdr->sh_type != SHT_NOTE) 12781 /* Not what we are looking for. */ 12782 continue; 12783 12784 if (notes_section != NULL) 12785 { 12786 char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name); 12787 if (sname == NULL || strcmp (sname, notes_section) != 0) 12788 continue; 12789 } 12790 12791 printf (_("\ 12792\nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"), 12793 elf_ndxscn (scn), 12794 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), 12795 shdr->sh_size, shdr->sh_offset); 12796 12797 handle_notes_data (ebl, ehdr, shdr->sh_offset, 12798 elf_getdata (scn, NULL)); 12799 } 12800 return; 12801 } 12802 12803 /* We have to look through the program header to find the note 12804 sections. There can be more than one. */ 12805 for (size_t cnt = 0; cnt < phnum; ++cnt) 12806 { 12807 GElf_Phdr mem; 12808 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem); 12809 12810 if (phdr == NULL || phdr->p_type != PT_NOTE) 12811 /* Not what we are looking for. */ 12812 continue; 12813 12814 printf (_("\ 12815\nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"), 12816 phdr->p_filesz, phdr->p_offset); 12817 12818 handle_notes_data (ebl, ehdr, phdr->p_offset, 12819 elf_getdata_rawchunk (ebl->elf, 12820 phdr->p_offset, phdr->p_filesz, 12821 (phdr->p_align == 8 12822 ? ELF_T_NHDR8 : ELF_T_NHDR))); 12823 } 12824} 12825 12826 12827static void 12828hex_dump (const uint8_t *data, size_t len) 12829{ 12830 size_t pos = 0; 12831 while (pos < len) 12832 { 12833 printf (" 0x%08zx ", pos); 12834 12835 const size_t chunk = MIN (len - pos, 16); 12836 12837 for (size_t i = 0; i < chunk; ++i) 12838 if (i % 4 == 3) 12839 printf ("%02x ", data[pos + i]); 12840 else 12841 printf ("%02x", data[pos + i]); 12842 12843 if (chunk < 16) 12844 printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), ""); 12845 12846 for (size_t i = 0; i < chunk; ++i) 12847 { 12848 unsigned char b = data[pos + i]; 12849 printf ("%c", isprint (b) ? b : '.'); 12850 } 12851 12852 putchar ('\n'); 12853 pos += chunk; 12854 } 12855} 12856 12857static void 12858dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name) 12859{ 12860 if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS) 12861 printf (_("\nSection [%zu] '%s' has no data to dump.\n"), 12862 elf_ndxscn (scn), name); 12863 else 12864 { 12865 if (print_decompress) 12866 { 12867 /* We try to decompress the section, but keep the old shdr around 12868 so we can show both the original shdr size and the uncompressed 12869 data size. */ 12870 if ((shdr->sh_flags & SHF_COMPRESSED) != 0) 12871 { 12872 if (elf_compress (scn, 0, 0) < 0) 12873 printf ("WARNING: %s [%zd]\n", 12874 _("Couldn't uncompress section"), 12875 elf_ndxscn (scn)); 12876 } 12877 else if (startswith (name, ".zdebug")) 12878 { 12879 if (elf_compress_gnu (scn, 0, 0) < 0) 12880 printf ("WARNING: %s [%zd]\n", 12881 _("Couldn't uncompress section"), 12882 elf_ndxscn (scn)); 12883 } 12884 } 12885 12886 Elf_Data *data = elf_rawdata (scn, NULL); 12887 if (data == NULL) 12888 error (0, 0, _("cannot get data for section [%zu] '%s': %s"), 12889 elf_ndxscn (scn), name, elf_errmsg (-1)); 12890 else 12891 { 12892 if (data->d_size == shdr->sh_size) 12893 printf (_("\nHex dump of section [%zu] '%s', %" PRIu64 12894 " bytes at offset %#0" PRIx64 ":\n"), 12895 elf_ndxscn (scn), name, 12896 shdr->sh_size, shdr->sh_offset); 12897 else 12898 printf (_("\nHex dump of section [%zu] '%s', %" PRIu64 12899 " bytes (%zd uncompressed) at offset %#0" 12900 PRIx64 ":\n"), 12901 elf_ndxscn (scn), name, 12902 shdr->sh_size, data->d_size, shdr->sh_offset); 12903 hex_dump (data->d_buf, data->d_size); 12904 } 12905 } 12906} 12907 12908static void 12909print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name) 12910{ 12911 if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS) 12912 printf (_("\nSection [%zu] '%s' has no strings to dump.\n"), 12913 elf_ndxscn (scn), name); 12914 else 12915 { 12916 if (print_decompress) 12917 { 12918 /* We try to decompress the section, but keep the old shdr around 12919 so we can show both the original shdr size and the uncompressed 12920 data size. */ 12921 if ((shdr->sh_flags & SHF_COMPRESSED) != 0) 12922 { 12923 if (elf_compress (scn, 0, 0) < 0) 12924 printf ("WARNING: %s [%zd]\n", 12925 _("Couldn't uncompress section"), 12926 elf_ndxscn (scn)); 12927 } 12928 else if (startswith (name, ".zdebug")) 12929 { 12930 if (elf_compress_gnu (scn, 0, 0) < 0) 12931 printf ("WARNING: %s [%zd]\n", 12932 _("Couldn't uncompress section"), 12933 elf_ndxscn (scn)); 12934 } 12935 } 12936 12937 Elf_Data *data = elf_rawdata (scn, NULL); 12938 if (data == NULL) 12939 error (0, 0, _("cannot get data for section [%zu] '%s': %s"), 12940 elf_ndxscn (scn), name, elf_errmsg (-1)); 12941 else 12942 { 12943 if (data->d_size == shdr->sh_size) 12944 printf (_("\nString section [%zu] '%s' contains %" PRIu64 12945 " bytes at offset %#0" PRIx64 ":\n"), 12946 elf_ndxscn (scn), name, 12947 shdr->sh_size, shdr->sh_offset); 12948 else 12949 printf (_("\nString section [%zu] '%s' contains %" PRIu64 12950 " bytes (%zd uncompressed) at offset %#0" 12951 PRIx64 ":\n"), 12952 elf_ndxscn (scn), name, 12953 shdr->sh_size, data->d_size, shdr->sh_offset); 12954 12955 const char *start = data->d_buf; 12956 const char *const limit = start + data->d_size; 12957 do 12958 { 12959 const char *end = memchr (start, '\0', limit - start); 12960 const size_t pos = start - (const char *) data->d_buf; 12961 if (unlikely (end == NULL)) 12962 { 12963 printf (" [%6zx]- %.*s\n", 12964 pos, (int) (limit - start), start); 12965 break; 12966 } 12967 printf (" [%6zx] %s\n", pos, start); 12968 start = end + 1; 12969 } while (start < limit); 12970 } 12971 } 12972} 12973 12974static void 12975for_each_section_argument (Elf *elf, const struct section_argument *list, 12976 void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr, 12977 const char *name)) 12978{ 12979 /* Get the section header string table index. */ 12980 size_t shstrndx; 12981 if (elf_getshdrstrndx (elf, &shstrndx) < 0) 12982 error_exit (0, _("cannot get section header string table index")); 12983 12984 for (const struct section_argument *a = list; a != NULL; a = a->next) 12985 { 12986 Elf_Scn *scn; 12987 GElf_Shdr shdr_mem; 12988 const char *name = NULL; 12989 12990 char *endp = NULL; 12991 unsigned long int shndx = strtoul (a->arg, &endp, 0); 12992 if (endp != a->arg && *endp == '\0') 12993 { 12994 scn = elf_getscn (elf, shndx); 12995 if (scn == NULL) 12996 { 12997 error (0, 0, _("\nsection [%lu] does not exist"), shndx); 12998 continue; 12999 } 13000 13001 if (gelf_getshdr (scn, &shdr_mem) == NULL) 13002 error_exit (0, _("cannot get section header: %s"), 13003 elf_errmsg (-1)); 13004 name = elf_strptr (elf, shstrndx, shdr_mem.sh_name); 13005 (*dump) (scn, &shdr_mem, name); 13006 } 13007 else 13008 { 13009 /* Need to look up the section by name. */ 13010 scn = NULL; 13011 bool found = false; 13012 while ((scn = elf_nextscn (elf, scn)) != NULL) 13013 { 13014 if (gelf_getshdr (scn, &shdr_mem) == NULL) 13015 continue; 13016 name = elf_strptr (elf, shstrndx, shdr_mem.sh_name); 13017 if (name == NULL) 13018 continue; 13019 if (!strcmp (name, a->arg)) 13020 { 13021 found = true; 13022 (*dump) (scn, &shdr_mem, name); 13023 } 13024 } 13025 13026 if (unlikely (!found) && !a->implicit) 13027 error (0, 0, _("\nsection '%s' does not exist"), a->arg); 13028 } 13029 } 13030} 13031 13032static void 13033dump_data (Ebl *ebl) 13034{ 13035 for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section); 13036} 13037 13038static void 13039dump_strings (Ebl *ebl) 13040{ 13041 for_each_section_argument (ebl->elf, string_sections, &print_string_section); 13042} 13043 13044static void 13045print_strings (Ebl *ebl) 13046{ 13047 /* Get the section header string table index. */ 13048 size_t shstrndx; 13049 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)) 13050 error_exit (0, _("cannot get section header string table index")); 13051 13052 Elf_Scn *scn; 13053 GElf_Shdr shdr_mem; 13054 const char *name; 13055 scn = NULL; 13056 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) 13057 { 13058 if (gelf_getshdr (scn, &shdr_mem) == NULL) 13059 continue; 13060 13061 if (shdr_mem.sh_type != SHT_PROGBITS 13062 || !(shdr_mem.sh_flags & SHF_STRINGS)) 13063 continue; 13064 13065 name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name); 13066 if (name == NULL) 13067 continue; 13068 13069 print_string_section (scn, &shdr_mem, name); 13070 } 13071} 13072 13073static void 13074dump_archive_index (Elf *elf, const char *fname) 13075{ 13076 size_t narsym; 13077 const Elf_Arsym *arsym = elf_getarsym (elf, &narsym); 13078 if (arsym == NULL) 13079 { 13080 int result = elf_errno (); 13081 if (unlikely (result != ELF_E_NO_INDEX)) 13082 error_exit (0, _("cannot get symbol index of archive '%s': %s"), 13083 fname, elf_errmsg (result)); 13084 else 13085 printf (_("\nArchive '%s' has no symbol index\n"), fname); 13086 return; 13087 } 13088 13089 printf (_("\nIndex of archive '%s' has %zu entries:\n"), 13090 fname, narsym); 13091 13092 size_t as_off = 0; 13093 for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s) 13094 { 13095 if (s->as_off != as_off) 13096 { 13097 as_off = s->as_off; 13098 13099 Elf *subelf = NULL; 13100 if (unlikely (elf_rand (elf, as_off) == 0) 13101 || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf)) 13102 == NULL)) 13103#if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7) 13104 while (1) 13105#endif 13106 error_exit (0, 13107 _("cannot extract member at offset %zu in '%s': %s"), 13108 as_off, fname, elf_errmsg (-1)); 13109 13110 const Elf_Arhdr *h = elf_getarhdr (subelf); 13111 13112 printf (_("Archive member '%s' contains:\n"), h->ar_name); 13113 13114 elf_end (subelf); 13115 } 13116 13117 printf ("\t%s\n", s->as_name); 13118 } 13119} 13120 13121#include "debugpred.h" 13122