LCOV - code coverage report
Current view: top level - src - readelf.c (source / functions) Hit Total Coverage
Test: elfutils- Lines: 3542 6282 56.4 %
Date: 2020-06-11 18:20:19 Functions: 102 136 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <libintl.h>
      34             : #include <locale.h>
      35             : #include <stdarg.h>
      36             : #include <stdbool.h>
      37             : #include <stdio.h>
      38             : #include <stdio_ext.h>
      39             : #include <stdlib.h>
      40             : #include <string.h>
      41             : #include <strings.h>
      42             : #include <time.h>
      43             : #include <unistd.h>
      44             : #include <sys/stat.h>
      45             : #include <signal.h>
      46             : 
      47             : #include <libeu.h>
      48             : #include <system.h>
      49             : #include <printversion.h>
      50             : #include "../libelf/libelfP.h"
      51             : #include "../libelf/common.h"
      52             : #include "../libebl/libeblP.h"
      53             : #include "../libdwelf/libdwelf.h"
      54             : #include "../libdw/libdwP.h"
      55             : #include "../libdwfl/libdwflP.h"
      56             : #include "../libdw/memory-access.h"
      57             : 
      58             : #include "../libdw/known-dwarf.h"
      59             : 
      60             : #ifdef __linux__
      61             : #define CORE_SIGILL  SIGILL
      62             : #define CORE_SIGBUS  SIGBUS
      63             : #define CORE_SIGFPE  SIGFPE
      64             : #define CORE_SIGSEGV SIGSEGV
      65             : #define CORE_SI_USER SI_USER
      66             : #else
      67             : /* We want the linux version of those as that is what shows up in the core files. */
      68             : #define CORE_SIGILL  4  /* Illegal instruction (ANSI).  */
      69             : #define CORE_SIGBUS  7  /* BUS error (4.2 BSD).  */
      70             : #define CORE_SIGFPE  8  /* Floating-point exception (ANSI).  */
      71             : #define CORE_SIGSEGV 11 /* Segmentation violation (ANSI).  */
      72             : #define CORE_SI_USER 0  /* Sent by kill, sigsend.  */
      73             : #endif
      74             : 
      75             : /* Name and version of program.  */
      76             : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      77             : 
      78             : /* Bug report address.  */
      79             : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      80             : 
      81             : /* argp key value for --elf-section, non-ascii.  */
      82             : #define ELF_INPUT_SECTION 256
      83             : 
      84             : /* argp key value for --dwarf-skeleton, non-ascii.  */
      85             : #define DWARF_SKELETON 257
      86             : 
      87             : /* argp key value for --dyn-syms, non-ascii.  */
      88             : #define PRINT_DYNSYM_TABLE 258
      89             : 
      90             : /* Terrible hack for hooking unrelated skeleton/split compile units,
      91             :    see __libdw_link_skel_split in print_debug.  */
      92             : static bool do_not_close_dwfl = false;
      93             : 
      94             : /* Definitions of arguments for argp functions.  */
      95             : static const struct argp_option options[] =
      96             : {
      97             :   { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 },
      98             :   { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL,
      99             :     N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF "
     100             :        "input data"), 0 },
     101             :   { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0,
     102             :     N_("Used with -w to find the skeleton Compile Units in FILE associated "
     103             :        "with the Split Compile units in a .dwo input file"), 0 },
     104             :   { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 },
     105             :   { "all", 'a', NULL, 0,
     106             :     N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 },
     107             :   { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 },
     108             :   { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 },
     109             :   { "histogram", 'I', NULL, 0,
     110             :     N_("Display histogram of bucket list lengths"), 0 },
     111             :   { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 },
     112             :   { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     113             :   { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
     114             :   { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 },
     115             :   { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
     116             :   { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     117             :   { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
     118             :     N_("Display the symbol table sections"), 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             : 
     141             :   { NULL, 0, NULL, 0, N_("Output control:"), 0 },
     142             :   { "numeric-addresses", 'N', NULL, 0,
     143             :     N_("Do not find symbol names for addresses in DWARF data"), 0 },
     144             :   { "unresolved-address-offsets", 'U', NULL, 0,
     145             :     N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
     146             :   { "wide", 'W', NULL, 0,
     147             :     N_("Ignored for compatibility (lines always wide)"), 0 },
     148             :   { "decompress", 'z', NULL, 0,
     149             :     N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
     150             :   { NULL, 0, NULL, 0, NULL, 0 }
     151             : };
     152             : 
     153             : /* Short description of program.  */
     154             : static const char doc[] = N_("\
     155             : Print information from ELF file in human-readable form.");
     156             : 
     157             : /* Strings for arguments in help texts.  */
     158             : static const char args_doc[] = N_("FILE...");
     159             : 
     160             : /* Prototype for option handler.  */
     161             : static error_t parse_opt (int key, char *arg, struct argp_state *state);
     162             : 
     163             : /* Data structure to communicate with argp functions.  */
     164             : static struct argp argp =
     165             : {
     166             :   options, parse_opt, args_doc, doc, NULL, NULL, NULL
     167             : };
     168             : 
     169             : /* If non-null, the section from which we should read to (compressed) ELF.  */
     170             : static const char *elf_input_section = NULL;
     171             : 
     172             : /* If non-null, the file that contains the skeleton CUs.  */
     173             : static const char *dwarf_skeleton = NULL;
     174             : 
     175             : /* Flags set by the option controlling the output.  */
     176             : 
     177             : /* True if dynamic segment should be printed.  */
     178             : static bool print_dynamic_table;
     179             : 
     180             : /* True if the file header should be printed.  */
     181             : static bool print_file_header;
     182             : 
     183             : /* True if the program headers should be printed.  */
     184             : static bool print_program_header;
     185             : 
     186             : /* True if relocations should be printed.  */
     187             : static bool print_relocations;
     188             : 
     189             : /* True if the section headers should be printed.  */
     190             : static bool print_section_header;
     191             : 
     192             : /* True if the symbol table should be printed.  */
     193             : static bool print_symbol_table;
     194             : 
     195             : /* True if (only) the dynsym table should be printed.  */
     196             : static bool print_dynsym_table;
     197             : 
     198             : /* A specific section name, or NULL to print all symbol tables.  */
     199             : static char *symbol_table_section;
     200             : 
     201             : /* A specific section name, or NULL to print all ELF notes.  */
     202             : static char *notes_section;
     203             : 
     204             : /* True if the version information should be printed.  */
     205             : static bool print_version_info;
     206             : 
     207             : /* True if section groups should be printed.  */
     208             : static bool print_section_groups;
     209             : 
     210             : /* True if bucket list length histogram should be printed.  */
     211             : static bool print_histogram;
     212             : 
     213             : /* True if the architecture specific data should be printed.  */
     214             : static bool print_arch;
     215             : 
     216             : /* True if note section content should be printed.  */
     217             : static bool print_notes;
     218             : 
     219             : /* True if SHF_STRINGS section content should be printed.  */
     220             : static bool print_string_sections;
     221             : 
     222             : /* True if archive index should be printed.  */
     223             : static bool print_archive_index;
     224             : 
     225             : /* True if any of the control options except print_archive_index is set.  */
     226             : static bool any_control_option;
     227             : 
     228             : /* True if we should print addresses from DWARF in symbolic form.  */
     229             : static bool print_address_names = true;
     230             : 
     231             : /* True if we should print raw values instead of relativized addresses.  */
     232             : static bool print_unresolved_addresses = false;
     233             : 
     234             : /* True if we should print the .debug_aranges section using libdw.  */
     235             : static bool decodedaranges = false;
     236             : 
     237             : /* True if we should print the .debug_aranges section using libdw.  */
     238             : static bool decodedline = false;
     239             : 
     240             : /* True if we want to show more information about compressed sections.  */
     241             : static bool print_decompress = false;
     242             : 
     243             : /* True if we want to show split compile units for debug_info skeletons.  */
     244             : static bool show_split_units = false;
     245             : 
     246             : /* Select printing of debugging sections.  */
     247             : static enum section_e
     248             : {
     249             :   section_abbrev = 1,           /* .debug_abbrev  */
     250             :   section_aranges = 2,          /* .debug_aranges  */
     251             :   section_frame = 4,            /* .debug_frame or .eh_frame & al.  */
     252             :   section_info = 8,             /* .debug_info, (implies .debug_types)  */
     253             :   section_line = 16,            /* .debug_line  */
     254             :   section_loc = 32,             /* .debug_loc  */
     255             :   section_pubnames = 64,        /* .debug_pubnames  */
     256             :   section_str = 128,            /* .debug_str  */
     257             :   section_macinfo = 256,        /* .debug_macinfo  */
     258             :   section_ranges = 512,         /* .debug_ranges  */
     259             :   section_exception = 1024,     /* .eh_frame & al.  */
     260             :   section_gdb_index = 2048,     /* .gdb_index  */
     261             :   section_macro = 4096,         /* .debug_macro  */
     262             :   section_addr = 8192,          /* .debug_addr  */
     263             :   section_types = 16384,        /* .debug_types (implied by .debug_info)  */
     264             :   section_all = (section_abbrev | section_aranges | section_frame
     265             :                  | section_info | section_line | section_loc
     266             :                  | section_pubnames | section_str | section_macinfo
     267             :                  | section_ranges | section_exception | section_gdb_index
     268             :                  | section_macro | section_addr | section_types)
     269             : } print_debug_sections, implicit_debug_sections;
     270             : 
     271             : /* Select hex dumping of sections.  */
     272             : static struct section_argument *dump_data_sections;
     273             : static struct section_argument **dump_data_sections_tail = &dump_data_sections;
     274             : 
     275             : /* Select string dumping of sections.  */
     276             : static struct section_argument *string_sections;
     277             : static struct section_argument **string_sections_tail = &string_sections;
     278             : 
     279             : struct section_argument
     280             : {
     281             :   struct section_argument *next;
     282             :   const char *arg;
     283             :   bool implicit;
     284             : };
     285             : 
     286             : /* Numbers of sections and program headers in the file.  */
     287             : static size_t shnum;
     288             : static size_t phnum;
     289             : 
     290             : 
     291             : /* Declarations of local functions.  */
     292             : static void process_file (int fd, const char *fname, bool only_one);
     293             : static void process_elf_file (Dwfl_Module *dwflmod, int fd);
     294             : static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
     295             : static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
     296             : static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
     297             : static void print_scngrp (Ebl *ebl);
     298             : static void print_dynamic (Ebl *ebl);
     299             : static void print_relocs (Ebl *ebl, GElf_Ehdr *ehdr);
     300             : static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
     301             :                                GElf_Shdr *shdr);
     302             : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
     303             :                                 GElf_Shdr *shdr);
     304             : static void print_symtab (Ebl *ebl, int type);
     305             : static void handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     306             : static void print_verinfo (Ebl *ebl);
     307             : static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     308             : static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     309             : static void handle_versym (Ebl *ebl, Elf_Scn *scn,
     310             :                            GElf_Shdr *shdr);
     311             : static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
     312             : static void handle_hash (Ebl *ebl);
     313             : static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
     314             : static void print_liblist (Ebl *ebl);
     315             : static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
     316             : static void dump_data (Ebl *ebl);
     317             : static void dump_strings (Ebl *ebl);
     318             : static void print_strings (Ebl *ebl);
     319             : static void dump_archive_index (Elf *, const char *);
     320             : 
     321             : 
     322             : /* Looked up once with gettext in main.  */
     323             : static char *yes_str;
     324             : static char *no_str;
     325             : 
     326             : static void
     327             : cleanup_list (struct section_argument *list)
     328             : {
     329         841 :   while (list != NULL)
     330             :     {
     331         199 :       struct section_argument *a = list;
     332         199 :       list = a->next;
     333         199 :       free (a);
     334             :     }
     335             : }
     336             : 
     337             : int
     338         321 : main (int argc, char *argv[])
     339             : {
     340             :   /* We use no threads here which can interfere with handling a stream.  */
     341         321 :   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
     342             : 
     343             :   /* Set locale.  */
     344         321 :   setlocale (LC_ALL, "");
     345             : 
     346             :   /* Initialize the message catalog.  */
     347         321 :   textdomain (PACKAGE_TARNAME);
     348             : 
     349             :   /* Look up once.  */
     350         321 :   yes_str = gettext ("yes");
     351         321 :   no_str = gettext ("no");
     352             : 
     353             :   /* Parse and process arguments.  */
     354         321 :   int remaining;
     355         321 :   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
     356             : 
     357             :   /* Before we start tell the ELF library which version we are using.  */
     358         321 :   elf_version (EV_CURRENT);
     359             : 
     360             :   /* Now process all the files given at the command line.  */
     361         321 :   bool only_one = remaining + 1 == argc;
     362         340 :   do
     363             :     {
     364             :       /* Open the file.  */
     365         340 :       int fd = open (argv[remaining], O_RDONLY);
     366         340 :       if (fd == -1)
     367             :         {
     368           0 :           error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
     369           0 :           continue;
     370             :         }
     371             : 
     372         340 :       process_file (fd, argv[remaining], only_one);
     373             : 
     374         340 :       close (fd);
     375             :     }
     376         340 :   while (++remaining < argc);
     377             : 
     378         321 :   cleanup_list (dump_data_sections);
     379         321 :   cleanup_list (string_sections);
     380             : 
     381         321 :   return error_message_count != 0;
     382             : }
     383             : 
     384             : 
     385             : /* Handle program arguments.  */
     386             : static error_t
     387        1992 : parse_opt (int key, char *arg,
     388             :            struct argp_state *state __attribute__ ((unused)))
     389             : {
     390        2191 :   void add_dump_section (const char *name, bool implicit)
     391             :   {
     392         199 :     struct section_argument *a = xmalloc (sizeof *a);
     393         199 :     a->arg = name;
     394         199 :     a->next = NULL;
     395         199 :     a->implicit = implicit;
     396         398 :     struct section_argument ***tailp
     397         199 :       = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
     398         199 :     **tailp = a;
     399         199 :     *tailp = &a->next;
     400         199 :   }
     401             : 
     402        1992 :   switch (key)
     403             :     {
     404          64 :     case 'a':
     405          64 :       print_file_header = true;
     406          64 :       print_program_header = true;
     407          64 :       print_relocations = true;
     408          64 :       print_section_header = true;
     409          64 :       print_symbol_table = true;
     410          64 :       print_version_info = true;
     411          64 :       print_dynamic_table = true;
     412          64 :       print_section_groups = true;
     413          64 :       print_histogram = true;
     414          64 :       print_arch = true;
     415          64 :       print_notes = true;
     416          64 :       implicit_debug_sections |= section_exception;
     417          64 :       add_dump_section (".strtab", true);
     418          64 :       add_dump_section (".dynstr", true);
     419          64 :       add_dump_section (".comment", true);
     420          64 :       any_control_option = true;
     421          64 :       break;
     422           4 :     case 'A':
     423           4 :       print_arch = true;
     424           4 :       any_control_option = true;
     425           4 :       break;
     426           2 :     case 'd':
     427           2 :       print_dynamic_table = true;
     428           2 :       any_control_option = true;
     429           2 :       break;
     430           0 :     case 'e':
     431           0 :       print_debug_sections |= section_exception;
     432           0 :       any_control_option = true;
     433           0 :       break;
     434           9 :     case 'g':
     435           9 :       print_section_groups = true;
     436           9 :       any_control_option = true;
     437           9 :       break;
     438           1 :     case 'h':
     439           1 :       print_file_header = true;
     440           1 :       any_control_option = true;
     441           1 :       break;
     442           0 :     case 'I':
     443           0 :       print_histogram = true;
     444           0 :       any_control_option = true;
     445           0 :       break;
     446           2 :     case 'l':
     447           2 :       print_program_header = true;
     448           2 :       any_control_option = true;
     449           2 :       break;
     450          22 :     case 'n':
     451          22 :       print_notes = true;
     452          22 :       any_control_option = true;
     453          22 :       notes_section = arg;
     454          22 :       break;
     455           1 :     case 'r':
     456           1 :       print_relocations = true;
     457           1 :       any_control_option = true;
     458           1 :      break;
     459          91 :     case 'S':
     460          91 :       print_section_header = true;
     461          91 :       any_control_option = true;
     462          91 :       break;
     463          14 :     case 's':
     464          14 :       print_symbol_table = true;
     465          14 :       any_control_option = true;
     466          14 :       symbol_table_section = arg;
     467          14 :       break;
     468           1 :     case PRINT_DYNSYM_TABLE:
     469           1 :       print_dynsym_table = true;
     470           1 :       any_control_option = true;
     471           1 :       break;
     472           0 :     case 'V':
     473           0 :       print_version_info = true;
     474           0 :       any_control_option = true;
     475           0 :       break;
     476           2 :     case 'c':
     477           2 :       print_archive_index = true;
     478           2 :       break;
     479         116 :     case 'w':
     480         116 :       if (arg == NULL)
     481             :         {
     482          39 :           print_debug_sections = section_all;
     483          39 :           implicit_debug_sections = section_info;
     484          39 :           show_split_units = true;
     485             :         }
     486          77 :       else if (strcmp (arg, "abbrev") == 0)
     487           0 :         print_debug_sections |= section_abbrev;
     488          77 :       else if (strcmp (arg, "addr") == 0)
     489             :         {
     490           2 :           print_debug_sections |= section_addr;
     491           2 :           implicit_debug_sections |= section_info;
     492             :         }
     493          75 :       else if (strcmp (arg, "aranges") == 0)
     494           3 :         print_debug_sections |= section_aranges;
     495          72 :       else if (strcmp (arg, "decodedaranges") == 0)
     496             :         {
     497           1 :           print_debug_sections |= section_aranges;
     498           1 :           decodedaranges = true;
     499             :         }
     500          71 :       else if (strcmp (arg, "ranges") == 0)
     501             :         {
     502          12 :           print_debug_sections |= section_ranges;
     503          12 :           implicit_debug_sections |= section_info;
     504             :         }
     505          59 :       else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
     506           2 :         print_debug_sections |= section_frame;
     507          57 :       else if (strcmp (arg, "info") == 0)
     508             :         {
     509          18 :           print_debug_sections |= section_info;
     510          18 :           print_debug_sections |= section_types;
     511             :         }
     512          39 :       else if (strcmp (arg, "info+") == 0)
     513             :         {
     514           2 :           print_debug_sections |= section_info;
     515           2 :           print_debug_sections |= section_types;
     516           2 :           show_split_units = true;
     517             :         }
     518          37 :       else if (strcmp (arg, "loc") == 0)
     519             :         {
     520          18 :           print_debug_sections |= section_loc;
     521          18 :           implicit_debug_sections |= section_info;
     522             :         }
     523          19 :       else if (strcmp (arg, "line") == 0)
     524           6 :         print_debug_sections |= section_line;
     525          13 :       else if (strcmp (arg, "decodedline") == 0)
     526             :         {
     527           5 :           print_debug_sections |= section_line;
     528           5 :           decodedline = true;
     529             :         }
     530           8 :       else if (strcmp (arg, "pubnames") == 0)
     531           0 :         print_debug_sections |= section_pubnames;
     532           8 :       else if (strcmp (arg, "str") == 0)
     533             :         {
     534           3 :           print_debug_sections |= section_str;
     535             :           /* For mapping string offset tables to CUs.  */
     536           3 :           implicit_debug_sections |= section_info;
     537             :         }
     538           5 :       else if (strcmp (arg, "macinfo") == 0)
     539           0 :         print_debug_sections |= section_macinfo;
     540           5 :       else if (strcmp (arg, "macro") == 0)
     541           3 :         print_debug_sections |= section_macro;
     542           2 :       else if (strcmp (arg, "exception") == 0)
     543           0 :         print_debug_sections |= section_exception;
     544           2 :       else if (strcmp (arg, "gdb_index") == 0)
     545           2 :         print_debug_sections |= section_gdb_index;
     546             :       else
     547             :         {
     548           0 :           fprintf (stderr, gettext ("Unknown DWARF debug section `%s'.\n"),
     549             :                    arg);
     550           0 :           argp_help (&argp, stderr, ARGP_HELP_SEE,
     551             :                      program_invocation_short_name);
     552           0 :           exit (1);
     553             :         }
     554         116 :       any_control_option = true;
     555         116 :       break;
     556           1 :     case 'p':
     557           1 :       any_control_option = true;
     558           1 :       if (arg == NULL)
     559             :         {
     560           0 :           print_string_sections = true;
     561           0 :           break;
     562             :         }
     563           7 :       FALLTHROUGH;
     564             :     case 'x':
     565           7 :       add_dump_section (arg, false);
     566           7 :       any_control_option = true;
     567           7 :       break;
     568           2 :     case 'N':
     569           2 :       print_address_names = false;
     570           2 :       break;
     571          27 :     case 'U':
     572          27 :       print_unresolved_addresses = true;
     573          27 :       break;
     574           0 :     case ARGP_KEY_NO_ARGS:
     575           0 :       fputs (gettext ("Missing file name.\n"), stderr);
     576           0 :       goto do_argp_help;
     577         321 :     case ARGP_KEY_FINI:
     578         321 :       if (! any_control_option && ! print_archive_index)
     579             :         {
     580           0 :           fputs (gettext ("No operation specified.\n"), stderr);
     581           0 :         do_argp_help:
     582           0 :           argp_help (&argp, stderr, ARGP_HELP_SEE,
     583             :                      program_invocation_short_name);
     584           0 :           exit (EXIT_FAILURE);
     585             :         }
     586             :       break;
     587             :     case 'W':                   /* Ignored.  */
     588             :       break;
     589          13 :     case 'z':
     590          13 :       print_decompress = true;
     591          13 :       break;
     592           4 :     case ELF_INPUT_SECTION:
     593           4 :       if (arg == NULL)
     594           4 :         elf_input_section = ".gnu_debugdata";
     595             :       else
     596           0 :         elf_input_section = arg;
     597             :       break;
     598           5 :     case DWARF_SKELETON:
     599           5 :       dwarf_skeleton = arg;
     600           5 :       break;
     601             :     default:
     602             :       return ARGP_ERR_UNKNOWN;
     603             :     }
     604             :   return 0;
     605             : }
     606             : 
     607             : 
     608             : /* Create a file descriptor to read the data from the
     609             :    elf_input_section given a file descriptor to an ELF file.  */
     610             : static int
     611           4 : open_input_section (int fd)
     612             : {
     613           4 :   size_t shnums;
     614           4 :   size_t cnt;
     615           4 :   size_t shstrndx;
     616           4 :   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
     617           4 :   if (elf == NULL)
     618             :     {
     619           0 :       error (0, 0, gettext ("cannot generate Elf descriptor: %s"),
     620             :              elf_errmsg (-1));
     621           0 :       return -1;
     622             :     }
     623             : 
     624           4 :   if (elf_getshdrnum (elf, &shnums) < 0)
     625             :     {
     626           0 :       error (0, 0, gettext ("cannot determine number of sections: %s"),
     627             :              elf_errmsg (-1));
     628           0 :     open_error:
     629           0 :       elf_end (elf);
     630           0 :       return -1;
     631             :     }
     632             : 
     633           4 :   if (elf_getshdrstrndx (elf, &shstrndx) < 0)
     634             :     {
     635           0 :       error (0, 0, gettext ("cannot get section header string table index"));
     636           0 :       goto open_error;
     637             :     }
     638             : 
     639          91 :   for (cnt = 0; cnt < shnums; ++cnt)
     640             :     {
     641          91 :       Elf_Scn *scn = elf_getscn (elf, cnt);
     642          91 :       if (scn == NULL)
     643             :         {
     644           0 :           error (0, 0, gettext ("cannot get section: %s"),
     645             :                  elf_errmsg (-1));
     646           0 :           goto open_error;
     647             :         }
     648             : 
     649          91 :       GElf_Shdr shdr_mem;
     650          91 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     651          91 :       if (unlikely (shdr == NULL))
     652             :         {
     653           0 :           error (0, 0, gettext ("cannot get section header: %s"),
     654             :                  elf_errmsg (-1));
     655           0 :           goto open_error;
     656             :         }
     657             : 
     658          91 :       const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
     659          91 :       if (sname == NULL)
     660             :         {
     661           0 :           error (0, 0, gettext ("cannot get section name"));
     662           0 :           goto open_error;
     663             :         }
     664             : 
     665          91 :       if (strcmp (sname, elf_input_section) == 0)
     666             :         {
     667           4 :           Elf_Data *data = elf_rawdata (scn, NULL);
     668           4 :           if (data == NULL)
     669             :             {
     670           0 :               error (0, 0, gettext ("cannot get %s content: %s"),
     671             :                      sname, elf_errmsg (-1));
     672           0 :               goto open_error;
     673             :             }
     674             : 
     675             :           /* Create (and immediately unlink) a temporary file to store
     676             :              section data in to create a file descriptor for it.  */
     677           4 :           const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
     678           4 :           static const char suffix[] = "/readelfXXXXXX";
     679           4 :           int tmplen = strlen (tmpdir) + sizeof (suffix);
     680           4 :           char *tempname = alloca (tmplen);
     681           4 :           sprintf (tempname, "%s%s", tmpdir, suffix);
     682             : 
     683           4 :           int sfd = mkstemp (tempname);
     684           4 :           if (sfd == -1)
     685             :             {
     686           0 :               error (0, 0, gettext ("cannot create temp file '%s'"),
     687             :                      tempname);
     688           0 :               goto open_error;
     689             :             }
     690           4 :           unlink (tempname);
     691             : 
     692           4 :           ssize_t size = data->d_size;
     693           4 :           if (write_retry (sfd, data->d_buf, size) != size)
     694             :             {
     695           0 :               error (0, 0, gettext ("cannot write section data"));
     696           0 :               goto open_error;
     697             :             }
     698             : 
     699           4 :           if (elf_end (elf) != 0)
     700             :             {
     701           0 :               error (0, 0, gettext ("error while closing Elf descriptor: %s"),
     702             :                      elf_errmsg (-1));
     703           4 :               return -1;
     704             :             }
     705             : 
     706           4 :           if (lseek (sfd, 0, SEEK_SET) == -1)
     707             :             {
     708           0 :               error (0, 0, gettext ("error while rewinding file descriptor"));
     709           0 :               return -1;
     710             :             }
     711             : 
     712             :           return sfd;
     713             :         }
     714             :     }
     715             : 
     716             :   /* Named section not found.  */
     717           0 :   if (elf_end (elf) != 0)
     718           0 :     error (0, 0, gettext ("error while closing Elf descriptor: %s"),
     719             :            elf_errmsg (-1));
     720             :   return -1;
     721             : }
     722             : 
     723             : /* Check if the file is an archive, and if so dump its index.  */
     724             : static void
     725           2 : check_archive_index (int fd, const char *fname, bool only_one)
     726             : {
     727             :   /* Create an `Elf' descriptor.  */
     728           2 :   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
     729           2 :   if (elf == NULL)
     730           0 :     error (0, 0, gettext ("cannot generate Elf descriptor: %s"),
     731             :            elf_errmsg (-1));
     732             :   else
     733             :     {
     734           2 :       if (elf_kind (elf) == ELF_K_AR)
     735             :         {
     736           2 :           if (!only_one)
     737           0 :             printf ("\n%s:\n\n", fname);
     738           2 :           dump_archive_index (elf, fname);
     739             :         }
     740             :       else
     741           0 :         error (0, 0,
     742           0 :                gettext ("'%s' is not an archive, cannot print archive index"),
     743             :                fname);
     744             : 
     745             :       /* Now we can close the descriptor.  */
     746           2 :       if (elf_end (elf) != 0)
     747           0 :         error (0, 0, gettext ("error while closing Elf descriptor: %s"),
     748             :                elf_errmsg (-1));
     749             :     }
     750           2 : }
     751             : 
     752             : /* Trivial callback used for checking if we opened an archive.  */
     753             : static int
     754         311 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
     755             :                void **userdata __attribute__ ((unused)),
     756             :                const char *name __attribute__ ((unused)),
     757             :                Dwarf_Addr base __attribute__ ((unused)),
     758             :                void *arg)
     759             : {
     760         311 :   if (*(bool *) arg)
     761             :     return DWARF_CB_ABORT;
     762         311 :   *(bool *) arg = true;
     763         311 :   return DWARF_CB_OK;
     764             : }
     765             : 
     766             : struct process_dwflmod_args
     767             : {
     768             :   int fd;
     769             :   bool only_one;
     770             : };
     771             : 
     772             : static int
     773         338 : process_dwflmod (Dwfl_Module *dwflmod,
     774             :                  void **userdata __attribute__ ((unused)),
     775             :                  const char *name __attribute__ ((unused)),
     776             :                  Dwarf_Addr base __attribute__ ((unused)),
     777             :                  void *arg)
     778             : {
     779         338 :   const struct process_dwflmod_args *a = arg;
     780             : 
     781             :   /* Print the file name.  */
     782         338 :   if (!a->only_one)
     783             :     {
     784          27 :       const char *fname;
     785          27 :       dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
     786             : 
     787          27 :       printf ("\n%s:\n\n", fname);
     788             :     }
     789             : 
     790         338 :   process_elf_file (dwflmod, a->fd);
     791             : 
     792         338 :   return DWARF_CB_OK;
     793             : }
     794             : 
     795             : /* Stub libdwfl callback, only the ELF handle already open is ever used.
     796             :    Only used for finding the alternate debug file if the Dwarf comes from
     797             :    the main file.  We are not interested in separate debuginfo.  */
     798             : static int
     799          72 : find_no_debuginfo (Dwfl_Module *mod,
     800             :                    void **userdata,
     801             :                    const char *modname,
     802             :                    Dwarf_Addr base,
     803             :                    const char *file_name,
     804             :                    const char *debuglink_file,
     805             :                    GElf_Word debuglink_crc,
     806             :                    char **debuginfo_file_name)
     807             : {
     808          72 :   Dwarf_Addr dwbias;
     809          72 :   dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
     810             : 
     811             :   /* We are only interested if the Dwarf has been setup on the main
     812             :      elf file but is only missing the alternate debug link.  If dwbias
     813             :      hasn't even been setup, this is searching for separate debuginfo
     814             :      for the main elf.  We don't care in that case.  */
     815          72 :   if (dwbias == (Dwarf_Addr) -1)
     816             :     return -1;
     817             : 
     818          11 :   return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
     819             :                                        file_name, debuglink_file,
     820             :                                        debuglink_crc, debuginfo_file_name);
     821             : }
     822             : 
     823             : static Dwfl *
     824         347 : create_dwfl (int fd, const char *fname)
     825             : {
     826             :   /* Duplicate an fd for dwfl_report_offline to swallow.  */
     827         347 :   int dwfl_fd = dup (fd);
     828         347 :   if (unlikely (dwfl_fd < 0))
     829           0 :     error (EXIT_FAILURE, errno, "dup");
     830             : 
     831             :   /* Use libdwfl in a trivial way to open the libdw handle for us.
     832             :      This takes care of applying relocations to DWARF data in ET_REL files.  */
     833         347 :   static const Dwfl_Callbacks callbacks =
     834             :     {
     835             :       .section_address = dwfl_offline_section_address,
     836             :       .find_debuginfo = find_no_debuginfo
     837             :     };
     838         347 :   Dwfl *dwfl = dwfl_begin (&callbacks);
     839         347 :   if (likely (dwfl != NULL))
     840             :     /* Let 0 be the logical address of the file (or first in archive).  */
     841         347 :     dwfl->offline_next_address = 0;
     842         347 :   if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
     843             :     {
     844           0 :       struct stat st;
     845           0 :       if (fstat (dwfl_fd, &st) != 0)
     846           0 :         error (0, errno, gettext ("cannot stat input file"));
     847           0 :       else if (unlikely (st.st_size == 0))
     848           0 :         error (0, 0, gettext ("input file is empty"));
     849             :       else
     850           0 :         error (0, 0, gettext ("failed reading '%s': %s"),
     851             :                fname, dwfl_errmsg (-1));
     852           0 :       close (dwfl_fd);          /* Consumed on success, not on failure.  */
     853           0 :       dwfl = NULL;
     854             :     }
     855             :   else
     856         347 :     dwfl_report_end (dwfl, NULL, NULL);
     857             : 
     858         347 :   return dwfl;
     859             : }
     860             : 
     861             : /* Process one input file.  */
     862             : static void
     863         340 : process_file (int fd, const char *fname, bool only_one)
     864             : {
     865         340 :   if (print_archive_index)
     866           2 :     check_archive_index (fd, fname, only_one);
     867             : 
     868         340 :   if (!any_control_option)
     869             :     return;
     870             : 
     871         338 :   if (elf_input_section != NULL)
     872             :     {
     873             :       /* Replace fname and fd with section content. */
     874           4 :       char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
     875           4 :       sprintf (fnname, "%s:%s", fname, elf_input_section);
     876           4 :       fd = open_input_section (fd);
     877           4 :       if (fd == -1)
     878             :         {
     879           0 :           error (0, 0, gettext ("No such section '%s' in '%s'"),
     880             :                  elf_input_section, fname);
     881           0 :           return;
     882             :         }
     883             :       fname = fnname;
     884             :     }
     885             : 
     886         338 :   Dwfl *dwfl = create_dwfl (fd, fname);
     887         338 :   if (dwfl != NULL)
     888             :     {
     889         338 :       if (only_one)
     890             :         {
     891             :           /* Clear ONLY_ONE if we have multiple modules, from an archive.  */
     892         311 :           bool seen = false;
     893         311 :           only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
     894             :         }
     895             : 
     896             :       /* Process the one or more modules gleaned from this file.  */
     897         338 :       struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
     898         338 :       dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
     899             :     }
     900             :   /* Terrible hack for hooking unrelated skeleton/split compile units,
     901             :      see __libdw_link_skel_split in print_debug.  */
     902         338 :   if (! do_not_close_dwfl)
     903         338 :     dwfl_end (dwfl);
     904             : 
     905             :   /* Need to close the replaced fd if we created it.  Caller takes
     906             :      care of original.  */
     907         338 :   if (elf_input_section != NULL)
     908           4 :     close (fd);
     909             : }
     910             : 
     911             : /* Check whether there are any compressed sections in the ELF file.  */
     912             : static bool
     913         120 : elf_contains_chdrs (Elf *elf)
     914             : {
     915         120 :   Elf_Scn *scn = NULL;
     916      789502 :   while ((scn = elf_nextscn (elf, scn)) != NULL)
     917             :     {
     918      789388 :       GElf_Shdr shdr_mem;
     919      789388 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     920      789388 :       if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
     921           6 :         return true;
     922             :     }
     923             :   return false;
     924             : }
     925             : 
     926             : /* Process one ELF file.  */
     927             : static void
     928         338 : process_elf_file (Dwfl_Module *dwflmod, int fd)
     929             : {
     930         338 :   GElf_Addr dwflbias;
     931         338 :   Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
     932             : 
     933         338 :   GElf_Ehdr ehdr_mem;
     934         338 :   GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
     935             : 
     936         338 :   if (ehdr == NULL)
     937             :     {
     938           0 :       error (0, 0, gettext ("cannot read ELF header: %s"), elf_errmsg (-1));
     939           0 :       return;
     940             :     }
     941             : 
     942         338 :   Ebl *ebl = ebl_openbackend (elf);
     943         338 :   if (unlikely (ebl == NULL))
     944             :     {
     945           0 :     ebl_error:
     946           0 :       error (0, errno, gettext ("cannot create EBL handle"));
     947           0 :       return;
     948             :     }
     949             : 
     950             :   /* Determine the number of sections.  */
     951         338 :   if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
     952           0 :     error (EXIT_FAILURE, 0,
     953           0 :            gettext ("cannot determine number of sections: %s"),
     954             :            elf_errmsg (-1));
     955             : 
     956             :   /* Determine the number of phdrs.  */
     957         338 :   if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
     958           0 :     error (EXIT_FAILURE, 0,
     959           0 :            gettext ("cannot determine number of program headers: %s"),
     960             :            elf_errmsg (-1));
     961             : 
     962             :   /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
     963             :      may have applied relocation to some sections.  If there are any
     964             :      compressed sections, any pass (or libdw/libdwfl) might have
     965             :      uncompressed them.  So we need to get a fresh Elf handle on the
     966             :      file to display those.  */
     967         676 :   bool print_unchanged = ((print_section_header
     968         183 :                            || print_relocations
     969         182 :                            || dump_data_sections != NULL
     970         176 :                            || print_notes)
     971         367 :                           && (ehdr->e_type == ET_REL
     972         120 :                               || elf_contains_chdrs (ebl->elf)));
     973             : 
     974         338 :   Elf *pure_elf = NULL;
     975         338 :   Ebl *pure_ebl = ebl;
     976         338 :   if (print_unchanged)
     977             :     {
     978             :       /* Read the file afresh.  */
     979          70 :       off_t aroff = elf_getaroff (elf);
     980          70 :       pure_elf = dwelf_elf_begin (fd);
     981          70 :       if (aroff > 0)
     982             :         {
     983             :           /* Archive member.  */
     984           0 :           (void) elf_rand (pure_elf, aroff);
     985           0 :           Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
     986           0 :           elf_end (pure_elf);
     987           0 :           pure_elf = armem;
     988             :         }
     989          70 :       if (pure_elf == NULL)
     990             :         {
     991           0 :           error (0, 0, gettext ("cannot read ELF: %s"), elf_errmsg (-1));
     992           0 :           return;
     993             :         }
     994          70 :       pure_ebl = ebl_openbackend (pure_elf);
     995          70 :       if (pure_ebl == NULL)
     996             :         goto ebl_error;
     997             :     }
     998             : 
     999         338 :   if (print_file_header)
    1000          65 :     print_ehdr (ebl, ehdr);
    1001         338 :   if (print_section_header)
    1002         155 :     print_shdr (pure_ebl, ehdr);
    1003         338 :   if (print_program_header)
    1004          66 :     print_phdr (ebl, ehdr);
    1005         338 :   if (print_section_groups)
    1006          73 :     print_scngrp (ebl);
    1007         338 :   if (print_dynamic_table)
    1008          66 :     print_dynamic (ebl);
    1009         338 :   if (print_relocations)
    1010          65 :     print_relocs (pure_ebl, ehdr);
    1011         338 :   if (print_histogram)
    1012          64 :     handle_hash (ebl);
    1013         338 :   if (print_symbol_table || print_dynsym_table)
    1014          79 :     print_symtab (ebl, SHT_DYNSYM);
    1015         338 :   if (print_version_info)
    1016          64 :     print_verinfo (ebl);
    1017         338 :   if (print_symbol_table)
    1018          78 :     print_symtab (ebl, SHT_SYMTAB);
    1019         338 :   if (print_arch)
    1020          68 :     print_liblist (ebl);
    1021         338 :   if (print_arch)
    1022          68 :     print_attributes (ebl, ehdr);
    1023         338 :   if (dump_data_sections != NULL)
    1024           6 :     dump_data (pure_ebl);
    1025         338 :   if (string_sections != NULL)
    1026          65 :     dump_strings (ebl);
    1027         338 :   if ((print_debug_sections | implicit_debug_sections) != 0)
    1028         186 :     print_debug (dwflmod, ebl, ehdr);
    1029         338 :   if (print_notes)
    1030          86 :     handle_notes (pure_ebl, ehdr);
    1031         338 :   if (print_string_sections)
    1032           0 :     print_strings (ebl);
    1033             : 
    1034         338 :   ebl_closebackend (ebl);
    1035             : 
    1036         338 :   if (pure_ebl != ebl)
    1037             :     {
    1038          70 :       ebl_closebackend (pure_ebl);
    1039          70 :       elf_end (pure_elf);
    1040             :     }
    1041             : }
    1042             : 
    1043             : 
    1044             : /* Print file type.  */
    1045             : static void
    1046          65 : print_file_type (unsigned short int e_type)
    1047             : {
    1048          65 :   if (likely (e_type <= ET_CORE))
    1049             :     {
    1050          65 :       static const char *const knowntypes[] =
    1051             :       {
    1052             :         N_("NONE (None)"),
    1053             :         N_("REL (Relocatable file)"),
    1054             :         N_("EXEC (Executable file)"),
    1055             :         N_("DYN (Shared object file)"),
    1056             :         N_("CORE (Core file)")
    1057             :       };
    1058          65 :       puts (gettext (knowntypes[e_type]));
    1059             :     }
    1060           0 :   else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
    1061           0 :     printf (gettext ("OS Specific: (%x)\n"),  e_type);
    1062           0 :   else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
    1063           0 :     printf (gettext ("Processor Specific: (%x)\n"),  e_type);
    1064             :   else
    1065           0 :     puts ("???");
    1066          65 : }
    1067             : 
    1068             : 
    1069             : /* Print ELF header.  */
    1070             : static void
    1071          65 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1072             : {
    1073          65 :   fputs_unlocked (gettext ("ELF Header:\n  Magic:  "), stdout);
    1074        1105 :   for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
    1075        1040 :     printf (" %02hhx", ehdr->e_ident[cnt]);
    1076             : 
    1077          65 :   printf (gettext ("\n  Class:                             %s\n"),
    1078          65 :           ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
    1079             :           : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
    1080          51 :           : "\?\?\?");
    1081             : 
    1082          65 :   printf (gettext ("  Data:                              %s\n"),
    1083          65 :           ehdr->e_ident[EI_DATA] == ELFDATA2LSB
    1084             :           ? "2's complement, little endian"
    1085             :           : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
    1086          12 :           ? "2's complement, big endian" : "\?\?\?");
    1087             : 
    1088         260 :   printf (gettext ("  Ident Version:                     %hhd %s\n"),
    1089          65 :           ehdr->e_ident[EI_VERSION],
    1090          65 :           ehdr->e_ident[EI_VERSION] == EV_CURRENT ? gettext ("(current)")
    1091             :           : "(\?\?\?)");
    1092             : 
    1093          65 :   char buf[512];
    1094          65 :   printf (gettext ("  OS/ABI:                            %s\n"),
    1095          65 :           ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
    1096             : 
    1097         195 :   printf (gettext ("  ABI Version:                       %hhd\n"),
    1098          65 :           ehdr->e_ident[EI_ABIVERSION]);
    1099             : 
    1100          65 :   fputs_unlocked (gettext ("  Type:                              "), stdout);
    1101          65 :   print_file_type (ehdr->e_type);
    1102             : 
    1103          65 :   const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
    1104          65 :   if (machine != NULL)
    1105          65 :     printf (gettext ("  Machine:                           %s\n"), machine);
    1106             :   else
    1107           0 :     printf (gettext ("  Machine:                           <unknown>: 0x%x\n"),
    1108           0 :             ehdr->e_machine);
    1109             : 
    1110          65 :   printf (gettext ("  Version:                           %d %s\n"),
    1111             :           ehdr->e_version,
    1112          65 :           ehdr->e_version  == EV_CURRENT ? gettext ("(current)") : "(\?\?\?)");
    1113             : 
    1114          65 :   printf (gettext ("  Entry point address:               %#" PRIx64 "\n"),
    1115             :           ehdr->e_entry);
    1116             : 
    1117          65 :   printf (gettext ("  Start of program headers:          %" PRId64 " %s\n"),
    1118             :           ehdr->e_phoff, gettext ("(bytes into file)"));
    1119             : 
    1120          65 :   printf (gettext ("  Start of section headers:          %" PRId64 " %s\n"),
    1121             :           ehdr->e_shoff, gettext ("(bytes into file)"));
    1122             : 
    1123          65 :   printf (gettext ("  Flags:                             %s\n"),
    1124             :           ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
    1125             : 
    1126         195 :   printf (gettext ("  Size of this header:               %" PRId16 " %s\n"),
    1127          65 :           ehdr->e_ehsize, gettext ("(bytes)"));
    1128             : 
    1129         195 :   printf (gettext ("  Size of program header entries:    %" PRId16 " %s\n"),
    1130          65 :           ehdr->e_phentsize, gettext ("(bytes)"));
    1131             : 
    1132         195 :   printf (gettext ("  Number of program headers entries: %" PRId16),
    1133          65 :           ehdr->e_phnum);
    1134          65 :   if (ehdr->e_phnum == PN_XNUM)
    1135             :     {
    1136           1 :       GElf_Shdr shdr_mem;
    1137           1 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1138           1 :       if (shdr != NULL)
    1139           2 :         printf (gettext (" (%" PRIu32 " in [0].sh_info)"),
    1140           1 :                 (uint32_t) shdr->sh_info);
    1141             :       else
    1142           0 :         fputs_unlocked (gettext (" ([0] not available)"), stdout);
    1143             :     }
    1144          65 :   fputc_unlocked ('\n', stdout);
    1145             : 
    1146         195 :   printf (gettext ("  Size of section header entries:    %" PRId16 " %s\n"),
    1147          65 :           ehdr->e_shentsize, gettext ("(bytes)"));
    1148             : 
    1149         195 :   printf (gettext ("  Number of section headers entries: %" PRId16),
    1150          65 :           ehdr->e_shnum);
    1151          65 :   if (ehdr->e_shnum == 0)
    1152             :     {
    1153           0 :       GElf_Shdr shdr_mem;
    1154           0 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1155           0 :       if (shdr != NULL)
    1156           0 :         printf (gettext (" (%" PRIu32 " in [0].sh_size)"),
    1157           0 :                 (uint32_t) shdr->sh_size);
    1158             :       else
    1159           0 :         fputs_unlocked (gettext (" ([0] not available)"), stdout);
    1160             :     }
    1161          65 :   fputc_unlocked ('\n', stdout);
    1162             : 
    1163          65 :   if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
    1164             :     {
    1165           0 :       GElf_Shdr shdr_mem;
    1166           0 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1167           0 :       if (shdr != NULL)
    1168             :         /* We managed to get the zeroth section.  */
    1169           0 :         snprintf (buf, sizeof (buf), gettext (" (%" PRIu32 " in [0].sh_link)"),
    1170           0 :                   (uint32_t) shdr->sh_link);
    1171             :       else
    1172             :         {
    1173           0 :           strncpy (buf, gettext (" ([0] not available)"), sizeof (buf));
    1174           0 :           buf[sizeof (buf) - 1] = '\0';
    1175             :         }
    1176             : 
    1177           0 :       printf (gettext ("  Section header string table index: XINDEX%s\n\n"),
    1178             :               buf);
    1179             :     }
    1180             :   else
    1181          65 :     printf (gettext ("  Section header string table index: %" PRId16 "\n\n"),
    1182             :             ehdr->e_shstrndx);
    1183          65 : }
    1184             : 
    1185             : 
    1186             : static const char *
    1187             : get_visibility_type (int value)
    1188             : {
    1189       25618 :   switch (value)
    1190             :     {
    1191             :     case STV_DEFAULT:
    1192             :       return "DEFAULT";
    1193           0 :     case STV_INTERNAL:
    1194           0 :       return "INTERNAL";
    1195         179 :     case STV_HIDDEN:
    1196         179 :       return "HIDDEN";
    1197           0 :     case STV_PROTECTED:
    1198           0 :       return "PROTECTED";
    1199           0 :     default:
    1200           0 :       return "???";
    1201             :     }
    1202             : }
    1203             : 
    1204             : static const char *
    1205             : elf_ch_type_name (unsigned int code)
    1206             : {
    1207           0 :   if (code == 0)
    1208             :     return "NONE";
    1209             : 
    1210           0 :   if (code == ELFCOMPRESS_ZLIB)
    1211           0 :     return "ZLIB";
    1212             : 
    1213             :   return "UNKNOWN";
    1214             : }
    1215             : 
    1216             : /* Print the section headers.  */
    1217             : static void
    1218           0 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1219             : {
    1220           0 :   size_t cnt;
    1221           0 :   size_t shstrndx;
    1222             : 
    1223           0 :   if (! print_file_header)
    1224             :     {
    1225           0 :       size_t sections;
    1226           0 :       if (unlikely (elf_getshdrnum (ebl->elf, &sections) < 0))
    1227           0 :         error (EXIT_FAILURE, 0,
    1228           0 :                gettext ("cannot get number of sections: %s"),
    1229             :                elf_errmsg (-1));
    1230             : 
    1231           0 :       printf (gettext ("\
    1232             : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
    1233             : \n"),
    1234             :               sections, ehdr->e_shoff);
    1235             :     }
    1236             : 
    1237             :   /* Get the section header string table index.  */
    1238           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1239           0 :     error (EXIT_FAILURE, 0,
    1240           0 :            gettext ("cannot get section header string table index: %s"),
    1241             :            elf_errmsg (-1));
    1242             : 
    1243           0 :   puts (gettext ("Section Headers:"));
    1244             : 
    1245           0 :   if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1246           0 :     puts (gettext ("[Nr] Name                 Type         Addr     Off    Size   ES Flags Lk Inf Al"));
    1247             :   else
    1248           0 :     puts (gettext ("[Nr] Name                 Type         Addr             Off      Size     ES Flags Lk Inf Al"));
    1249             : 
    1250           0 :   if (print_decompress)
    1251             :     {
    1252           0 :       if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1253           0 :         puts (gettext ("     [Compression  Size   Al]"));
    1254             :       else
    1255           0 :         puts (gettext ("     [Compression  Size     Al]"));
    1256             :     }
    1257             : 
    1258           0 :   for (cnt = 0; cnt < shnum; ++cnt)
    1259             :     {
    1260           0 :       Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
    1261             : 
    1262           0 :       if (unlikely (scn == NULL))
    1263           0 :         error (EXIT_FAILURE, 0, gettext ("cannot get section: %s"),
    1264             :                elf_errmsg (-1));
    1265             : 
    1266             :       /* Get the section header.  */
    1267           0 :       GElf_Shdr shdr_mem;
    1268           0 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1269           0 :       if (unlikely (shdr == NULL))
    1270           0 :         error (EXIT_FAILURE, 0, gettext ("cannot get section header: %s"),
    1271             :                elf_errmsg (-1));
    1272             : 
    1273           0 :       char flagbuf[20];
    1274           0 :       char *cp = flagbuf;
    1275           0 :       if (shdr->sh_flags & SHF_WRITE)
    1276           0 :         *cp++ = 'W';
    1277           0 :       if (shdr->sh_flags & SHF_ALLOC)
    1278           0 :         *cp++ = 'A';
    1279           0 :       if (shdr->sh_flags & SHF_EXECINSTR)
    1280           0 :         *cp++ = 'X';
    1281           0 :       if (shdr->sh_flags & SHF_MERGE)
    1282           0 :         *cp++ = 'M';
    1283           0 :       if (shdr->sh_flags & SHF_STRINGS)
    1284           0 :         *cp++ = 'S';
    1285           0 :       if (shdr->sh_flags & SHF_INFO_LINK)
    1286           0 :         *cp++ = 'I';
    1287           0 :       if (shdr->sh_flags & SHF_LINK_ORDER)
    1288           0 :         *cp++ = 'L';
    1289           0 :       if (shdr->sh_flags & SHF_OS_NONCONFORMING)
    1290           0 :         *cp++ = 'N';
    1291           0 :       if (shdr->sh_flags & SHF_GROUP)
    1292           0 :         *cp++ = 'G';
    1293           0 :       if (shdr->sh_flags & SHF_TLS)
    1294           0 :         *cp++ = 'T';
    1295           0 :       if (shdr->sh_flags & SHF_COMPRESSED)
    1296           0 :         *cp++ = 'C';
    1297           0 :       if (shdr->sh_flags & SHF_ORDERED)
    1298           0 :         *cp++ = 'O';
    1299           0 :       if (shdr->sh_flags & SHF_EXCLUDE)
    1300           0 :         *cp++ = 'E';
    1301           0 :       *cp = '\0';
    1302             : 
    1303           0 :       const char *sname;
    1304           0 :       char buf[128];
    1305           0 :       sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
    1306           0 :       printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
    1307             :               " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
    1308             :               " %2" PRId64 "\n",
    1309             :               cnt, sname,
    1310           0 :               ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
    1311             :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
    1312             :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
    1313           0 :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
    1314             :               shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
    1315             :               shdr->sh_addralign);
    1316             : 
    1317           0 :       if (print_decompress)
    1318             :         {
    1319           0 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    1320             :             {
    1321           0 :               GElf_Chdr chdr;
    1322           0 :               if (gelf_getchdr (scn, &chdr) != NULL)
    1323           0 :                 printf ("     [ELF %s (%" PRId32 ") %0*" PRIx64
    1324             :                         " %2" PRId64 "]\n",
    1325             :                         elf_ch_type_name (chdr.ch_type),
    1326             :                         chdr.ch_type,
    1327           0 :                         ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
    1328             :                         chdr.ch_size, chdr.ch_addralign);
    1329             :               else
    1330           0 :                 error (0, 0,
    1331           0 :                        gettext ("bad compression header for section %zd: %s"),
    1332             :                        elf_ndxscn (scn), elf_errmsg (-1));
    1333             :             }
    1334           0 :           else if (strncmp(".zdebug", sname, strlen (".zdebug")) == 0)
    1335             :             {
    1336           0 :               ssize_t size;
    1337           0 :               if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
    1338           0 :                 printf ("     [GNU ZLIB     %0*zx   ]\n",
    1339           0 :                         ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
    1340             :               else
    1341           0 :                 error (0, 0,
    1342           0 :                        gettext ("bad gnu compressed size for section %zd: %s"),
    1343             :                        elf_ndxscn (scn), elf_errmsg (-1));
    1344             :             }
    1345             :         }
    1346             :     }
    1347             : 
    1348           0 :   fputc_unlocked ('\n', stdout);
    1349           0 : }
    1350             : 
    1351             : 
    1352             : /* Print the program header.  */
    1353             : static void
    1354           0 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1355             : {
    1356           0 :   if (phnum == 0)
    1357             :     /* No program header, this is OK in relocatable objects.  */
    1358           0 :     return;
    1359             : 
    1360           0 :   puts (gettext ("Program Headers:"));
    1361           0 :   if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1362           0 :     puts (gettext ("\
    1363             :   Type           Offset   VirtAddr   PhysAddr   FileSiz  MemSiz   Flg Align"));
    1364             :   else
    1365           0 :     puts (gettext ("\
    1366             :   Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align"));
    1367             : 
    1368             :   /* Process all program headers.  */
    1369             :   bool has_relro = false;
    1370             :   GElf_Addr relro_from = 0;
    1371             :   GElf_Addr relro_to = 0;
    1372           0 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
    1373             :     {
    1374           0 :       char buf[128];
    1375           0 :       GElf_Phdr mem;
    1376           0 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
    1377             : 
    1378             :       /* If for some reason the header cannot be returned show this.  */
    1379           0 :       if (unlikely (phdr == NULL))
    1380             :         {
    1381           0 :           puts ("  ???");
    1382           0 :           continue;
    1383             :         }
    1384             : 
    1385           0 :       printf ("  %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
    1386             :               " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
    1387           0 :               ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
    1388             :               phdr->p_offset,
    1389             :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
    1390           0 :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
    1391             :               phdr->p_filesz,
    1392             :               phdr->p_memsz,
    1393           0 :               phdr->p_flags & PF_R ? 'R' : ' ',
    1394           0 :               phdr->p_flags & PF_W ? 'W' : ' ',
    1395           0 :               phdr->p_flags & PF_X ? 'E' : ' ',
    1396             :               phdr->p_align);
    1397             : 
    1398           0 :       if (phdr->p_type == PT_INTERP)
    1399             :         {
    1400             :           /* If we are sure the file offset is valid then we can show
    1401             :              the user the name of the interpreter.  We check whether
    1402             :              there is a section at the file offset.  Normally there
    1403             :              would be a section called ".interp".  But in separate
    1404             :              .debug files it is a NOBITS section (and so doesn't match
    1405             :              with gelf_offscn).  Which probably means the offset is
    1406             :              not valid another reason could be because the ELF file
    1407             :              just doesn't contain any section headers, in that case
    1408             :              just play it safe and don't display anything.  */
    1409             : 
    1410           0 :           Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
    1411           0 :           GElf_Shdr shdr_mem;
    1412           0 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1413             : 
    1414           0 :           size_t maxsize;
    1415           0 :           char *filedata = elf_rawfile (ebl->elf, &maxsize);
    1416             : 
    1417           0 :           if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
    1418           0 :               && filedata != NULL && phdr->p_offset < maxsize
    1419           0 :               && phdr->p_filesz <= maxsize - phdr->p_offset
    1420           0 :               && memchr (filedata + phdr->p_offset, '\0',
    1421             :                          phdr->p_filesz) != NULL)
    1422           0 :             printf (gettext ("\t[Requesting program interpreter: %s]\n"),
    1423             :                     filedata + phdr->p_offset);
    1424             :         }
    1425           0 :       else if (phdr->p_type == PT_GNU_RELRO)
    1426             :         {
    1427           0 :           has_relro = true;
    1428           0 :           relro_from = phdr->p_vaddr;
    1429           0 :           relro_to = relro_from + phdr->p_memsz;
    1430             :         }
    1431             :     }
    1432             : 
    1433           0 :   size_t sections;
    1434           0 :   if (unlikely (elf_getshdrnum (ebl->elf, &sections) < 0))
    1435           0 :     error (EXIT_FAILURE, 0,
    1436           0 :            gettext ("cannot get number of sections: %s"),
    1437             :            elf_errmsg (-1));
    1438             : 
    1439           0 :   if (sections == 0)
    1440             :     /* No sections in the file.  Punt.  */
    1441           0 :     return;
    1442             : 
    1443             :   /* Get the section header string table index.  */
    1444           0 :   size_t shstrndx;
    1445           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1446           0 :     error (EXIT_FAILURE, 0,
    1447           0 :            gettext ("cannot get section header string table index"));
    1448             : 
    1449           0 :   puts (gettext ("\n Section to Segment mapping:\n  Segment Sections..."));
    1450             : 
    1451           0 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
    1452             :     {
    1453             :       /* Print the segment number.  */
    1454           0 :       printf ("   %2.2zu     ", cnt);
    1455             : 
    1456           0 :       GElf_Phdr phdr_mem;
    1457           0 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
    1458             :       /* This must not happen.  */
    1459           0 :       if (unlikely (phdr == NULL))
    1460           0 :         error (EXIT_FAILURE, 0, gettext ("cannot get program header: %s"),
    1461             :                elf_errmsg (-1));
    1462             : 
    1463             :       /* Iterate over the sections.  */
    1464             :       bool in_relro = false;
    1465             :       bool in_ro = false;
    1466           0 :       for (size_t inner = 1; inner < shnum; ++inner)
    1467             :         {
    1468           0 :           Elf_Scn *scn = elf_getscn (ebl->elf, inner);
    1469             :           /* This should not happen.  */
    1470           0 :           if (unlikely (scn == NULL))
    1471           0 :             error (EXIT_FAILURE, 0, gettext ("cannot get section: %s"),
    1472             :                    elf_errmsg (-1));
    1473             : 
    1474             :           /* Get the section header.  */
    1475           0 :           GElf_Shdr shdr_mem;
    1476           0 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1477           0 :           if (unlikely (shdr == NULL))
    1478           0 :             error (EXIT_FAILURE, 0,
    1479           0 :                    gettext ("cannot get section header: %s"),
    1480             :                    elf_errmsg (-1));
    1481             : 
    1482           0 :           if (shdr->sh_size > 0
    1483             :               /* Compare allocated sections by VMA, unallocated
    1484             :                  sections by file offset.  */
    1485           0 :               && (shdr->sh_flags & SHF_ALLOC
    1486           0 :                   ? (shdr->sh_addr >= phdr->p_vaddr
    1487           0 :                      && (shdr->sh_addr + shdr->sh_size
    1488           0 :                          <= phdr->p_vaddr + phdr->p_memsz))
    1489           0 :                   : (shdr->sh_offset >= phdr->p_offset
    1490           0 :                      && (shdr->sh_offset + shdr->sh_size
    1491           0 :                          <= phdr->p_offset + phdr->p_filesz))))
    1492             :             {
    1493           0 :               if (has_relro && !in_relro
    1494           0 :                   && shdr->sh_addr >= relro_from
    1495           0 :                   && shdr->sh_addr + shdr->sh_size <= relro_to)
    1496             :                 {
    1497           0 :                   fputs_unlocked (" [RELRO:", stdout);
    1498           0 :                   in_relro = true;
    1499             :                 }
    1500           0 :               else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
    1501             :                 {
    1502           0 :                   fputs_unlocked ("]", stdout);
    1503           0 :                   in_relro =  false;
    1504             :                 }
    1505           0 :               else if (has_relro && in_relro
    1506           0 :                        && shdr->sh_addr + shdr->sh_size > relro_to)
    1507           0 :                 fputs_unlocked ("] <RELRO:", stdout);
    1508           0 :               else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
    1509             :                 {
    1510           0 :                   if (!in_ro)
    1511             :                     {
    1512           0 :                       fputs_unlocked (" [RO:", stdout);
    1513           0 :                       in_ro = true;
    1514             :                     }
    1515             :                 }
    1516             :               else
    1517             :                 {
    1518             :                   /* Determine the segment this section is part of.  */
    1519             :                   size_t cnt2;
    1520             :                   GElf_Phdr phdr2_mem;
    1521             :                   GElf_Phdr *phdr2 = NULL;
    1522           0 :                   for (cnt2 = 0; cnt2 < phnum; ++cnt2)
    1523             :                     {
    1524           0 :                       phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
    1525             : 
    1526           0 :                       if (phdr2 != NULL && phdr2->p_type == PT_LOAD
    1527           0 :                           && shdr->sh_addr >= phdr2->p_vaddr
    1528           0 :                           && (shdr->sh_addr + shdr->sh_size
    1529           0 :                               <= phdr2->p_vaddr + phdr2->p_memsz))
    1530             :                         break;
    1531             :                     }
    1532             : 
    1533           0 :                   if (cnt2 < phnum)
    1534             :                     {
    1535           0 :                       if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
    1536             :                         {
    1537           0 :                           fputs_unlocked (" [RO:", stdout);
    1538           0 :                           in_ro = true;
    1539             :                         }
    1540           0 :                       else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
    1541             :                         {
    1542           0 :                           fputs_unlocked ("]", stdout);
    1543           0 :                           in_ro = false;
    1544             :                         }
    1545             :                     }
    1546             :                 }
    1547             : 
    1548           0 :               printf (" %s",
    1549           0 :                       elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
    1550             : 
    1551             :               /* Signal that this sectin is only partially covered.  */
    1552           0 :               if (has_relro && in_relro
    1553           0 :                        && shdr->sh_addr + shdr->sh_size > relro_to)
    1554             :                 {
    1555           0 :                   fputs_unlocked (">", stdout);
    1556           0 :                   in_relro =  false;
    1557             :                 }
    1558             :             }
    1559             :         }
    1560           0 :       if (in_relro || in_ro)
    1561           0 :         fputs_unlocked ("]", stdout);
    1562             : 
    1563             :       /* Finish the line.  */
    1564           0 :       fputc_unlocked ('\n', stdout);
    1565             :     }
    1566             : }
    1567             : 
    1568             : 
    1569             : static const char *
    1570           0 : section_name (Ebl *ebl, GElf_Shdr *shdr)
    1571             : {
    1572           0 :   size_t shstrndx;
    1573           0 :   if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
    1574           0 :     return "???";
    1575           0 :   return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
    1576             : }
    1577             : 
    1578             : 
    1579             : static void
    1580           0 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    1581             : {
    1582             :   /* Get the data of the section.  */
    1583           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    1584             : 
    1585           0 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    1586           0 :   GElf_Shdr symshdr_mem;
    1587           0 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    1588           0 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    1589             : 
    1590           0 :   if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
    1591           0 :       || symdata == NULL)
    1592           0 :     return;
    1593             : 
    1594             :   /* Get the section header string table index.  */
    1595           0 :   size_t shstrndx;
    1596           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1597           0 :     error (EXIT_FAILURE, 0,
    1598           0 :            gettext ("cannot get section header string table index"));
    1599             : 
    1600           0 :   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
    1601             : 
    1602           0 :   GElf_Sym sym_mem;
    1603           0 :   GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
    1604             : 
    1605           0 :   printf ((grpref[0] & GRP_COMDAT)
    1606           0 :           ? ngettext ("\
    1607             : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
    1608             :                       "\
    1609             : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
    1610             :                       data->d_size / sizeof (Elf32_Word) - 1)
    1611           0 :           : ngettext ("\
    1612             : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
    1613             : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
    1614             :                       data->d_size / sizeof (Elf32_Word) - 1),
    1615             :           elf_ndxscn (scn),
    1616           0 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    1617             :           (sym == NULL ? NULL
    1618           0 :            : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
    1619           0 :           ?: gettext ("<INVALID SYMBOL>"),
    1620           0 :           data->d_size / sizeof (Elf32_Word) - 1);
    1621             : 
    1622           0 :   for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
    1623             :     {
    1624           0 :       GElf_Shdr grpshdr_mem;
    1625           0 :       GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
    1626             :                                          &grpshdr_mem);
    1627             : 
    1628           0 :       const char *str;
    1629           0 :       printf ("  [%2u] %s\n",
    1630             :               grpref[cnt],
    1631             :               grpshdr != NULL
    1632           0 :               && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
    1633           0 :               ? str : gettext ("<INVALID SECTION>"));
    1634             :     }
    1635             : }
    1636             : 
    1637             : 
    1638             : static void
    1639          73 : print_scngrp (Ebl *ebl)
    1640             : {
    1641             :   /* Find all relocation sections and handle them.  */
    1642          73 :   Elf_Scn *scn = NULL;
    1643             : 
    1644        2018 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    1645             :     {
    1646             :        /* Handle the section if it is a symbol table.  */
    1647        1945 :       GElf_Shdr shdr_mem;
    1648        1945 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1649             : 
    1650        1945 :       if (shdr != NULL && shdr->sh_type == SHT_GROUP)
    1651             :         {
    1652          16 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    1653             :             {
    1654           0 :               if (elf_compress (scn, 0, 0) < 0)
    1655           0 :                 printf ("WARNING: %s [%zd]\n",
    1656             :                         gettext ("Couldn't uncompress section"),
    1657             :                         elf_ndxscn (scn));
    1658           0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    1659           0 :               if (unlikely (shdr == NULL))
    1660           0 :                 error (EXIT_FAILURE, 0,
    1661           0 :                        gettext ("cannot get section [%zd] header: %s"),
    1662             :                        elf_ndxscn (scn),
    1663             :                        elf_errmsg (-1));
    1664             :             }
    1665          16 :           handle_scngrp (ebl, scn, shdr);
    1666             :         }
    1667             :     }
    1668          73 : }
    1669             : 
    1670             : 
    1671             : static const struct flags
    1672             : {
    1673             :   int mask;
    1674             :   const char *str;
    1675             : } dt_flags[] =
    1676             :   {
    1677             :     { DF_ORIGIN, "ORIGIN" },
    1678             :     { DF_SYMBOLIC, "SYMBOLIC" },
    1679             :     { DF_TEXTREL, "TEXTREL" },
    1680             :     { DF_BIND_NOW, "BIND_NOW" },
    1681             :     { DF_STATIC_TLS, "STATIC_TLS" }
    1682             :   };
    1683             : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
    1684             : 
    1685             : static const struct flags dt_flags_1[] =
    1686             :   {
    1687             :     { DF_1_NOW, "NOW" },
    1688             :     { DF_1_GLOBAL, "GLOBAL" },
    1689             :     { DF_1_GROUP, "GROUP" },
    1690             :     { DF_1_NODELETE, "NODELETE" },
    1691             :     { DF_1_LOADFLTR, "LOADFLTR" },
    1692             :     { DF_1_INITFIRST, "INITFIRST" },
    1693             :     { DF_1_NOOPEN, "NOOPEN" },
    1694             :     { DF_1_ORIGIN, "ORIGIN" },
    1695             :     { DF_1_DIRECT, "DIRECT" },
    1696             :     { DF_1_TRANS, "TRANS" },
    1697             :     { DF_1_INTERPOSE, "INTERPOSE" },
    1698             :     { DF_1_NODEFLIB, "NODEFLIB" },
    1699             :     { DF_1_NODUMP, "NODUMP" },
    1700             :     { DF_1_CONFALT, "CONFALT" },
    1701             :     { DF_1_ENDFILTEE, "ENDFILTEE" },
    1702             :     { DF_1_DISPRELDNE, "DISPRELDNE" },
    1703             :     { DF_1_DISPRELPND, "DISPRELPND" },
    1704             :   };
    1705             : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
    1706             : 
    1707             : static const struct flags dt_feature_1[] =
    1708             :   {
    1709             :     { DTF_1_PARINIT, "PARINIT" },
    1710             :     { DTF_1_CONFEXP, "CONFEXP" }
    1711             :   };
    1712             : static const int ndt_feature_1 = (sizeof (dt_feature_1)
    1713             :                                   / sizeof (dt_feature_1[0]));
    1714             : 
    1715             : static const struct flags dt_posflag_1[] =
    1716             :   {
    1717             :     { DF_P1_LAZYLOAD, "LAZYLOAD" },
    1718             :     { DF_P1_GROUPPERM, "GROUPPERM" }
    1719             :   };
    1720             : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
    1721             :                                   / sizeof (dt_posflag_1[0]));
    1722             : 
    1723             : 
    1724             : static void
    1725          15 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
    1726             :                 int nflags)
    1727             : {
    1728          15 :   bool first = true;
    1729          15 :   int cnt;
    1730             : 
    1731         228 :   for (cnt = 0; cnt < nflags; ++cnt)
    1732         213 :     if (d_val & flags[cnt].mask)
    1733             :       {
    1734          24 :         if (!first)
    1735          18 :           putchar_unlocked (' ');
    1736          24 :         fputs_unlocked (flags[cnt].str, stdout);
    1737          24 :         d_val &= ~flags[cnt].mask;
    1738          24 :         first = false;
    1739             :       }
    1740             : 
    1741          15 :   if (d_val != 0)
    1742             :     {
    1743          15 :       if (!first)
    1744           6 :         putchar_unlocked (' ');
    1745          26 :       printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
    1746             :     }
    1747             : 
    1748          15 :   putchar_unlocked ('\n');
    1749          15 : }
    1750             : 
    1751             : 
    1752             : static void
    1753             : print_dt_flags (int class, GElf_Xword d_val)
    1754             : {
    1755           1 :   print_flags (class, d_val, dt_flags, ndt_flags);
    1756             : }
    1757             : 
    1758             : 
    1759             : static void
    1760             : print_dt_flags_1 (int class, GElf_Xword d_val)
    1761             : {
    1762          12 :   print_flags (class, d_val, dt_flags_1, ndt_flags_1);
    1763             : }
    1764             : 
    1765             : 
    1766             : static void
    1767             : print_dt_feature_1 (int class, GElf_Xword d_val)
    1768             : {
    1769           1 :   print_flags (class, d_val, dt_feature_1, ndt_feature_1);
    1770             : }
    1771             : 
    1772             : 
    1773             : static void
    1774             : print_dt_posflag_1 (int class, GElf_Xword d_val)
    1775             : {
    1776           1 :   print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
    1777             : }
    1778             : 
    1779             : 
    1780             : static void
    1781          27 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    1782             : {
    1783          27 :   int class = gelf_getclass (ebl->elf);
    1784          27 :   GElf_Shdr glink_mem;
    1785          27 :   GElf_Shdr *glink;
    1786          27 :   Elf_Data *data;
    1787          27 :   size_t cnt;
    1788          27 :   size_t shstrndx;
    1789          27 :   size_t sh_entsize;
    1790             : 
    1791             :   /* Get the data of the section.  */
    1792          27 :   data = elf_getdata (scn, NULL);
    1793          27 :   if (data == NULL)
    1794           0 :     return;
    1795             : 
    1796             :   /* Get the section header string table index.  */
    1797          27 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1798           0 :     error (EXIT_FAILURE, 0,
    1799           0 :            gettext ("cannot get section header string table index"));
    1800             : 
    1801          27 :   sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
    1802             : 
    1803          27 :   glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
    1804          27 :   if (glink == NULL)
    1805           0 :     error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
    1806             :            elf_ndxscn (scn));
    1807             : 
    1808          27 :   printf (ngettext ("\
    1809             : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    1810             :                     "\
    1811             : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    1812             :                     shdr->sh_size / sh_entsize),
    1813          27 :           (unsigned long int) (shdr->sh_size / sh_entsize),
    1814             :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    1815             :           shdr->sh_offset,
    1816          27 :           (int) shdr->sh_link,
    1817          27 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    1818          27 :   fputs_unlocked (gettext ("  Type              Value\n"), stdout);
    1819             : 
    1820         882 :   for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
    1821             :     {
    1822         855 :       GElf_Dyn dynmem;
    1823         855 :       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
    1824         855 :       if (dyn == NULL)
    1825             :         break;
    1826             : 
    1827         855 :       char buf[64];
    1828         855 :       printf ("  %-17s ",
    1829             :               ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
    1830             : 
    1831         855 :       switch (dyn->d_tag)
    1832             :         {
    1833         172 :         case DT_NULL:
    1834             :         case DT_DEBUG:
    1835             :         case DT_BIND_NOW:
    1836             :         case DT_TEXTREL:
    1837             :           /* No further output.  */
    1838         172 :           fputc_unlocked ('\n', stdout);
    1839             :           break;
    1840             : 
    1841          77 :         case DT_NEEDED:
    1842          77 :           printf (gettext ("Shared library: [%s]\n"),
    1843          77 :                   elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
    1844             :           break;
    1845             : 
    1846           3 :         case DT_SONAME:
    1847           3 :           printf (gettext ("Library soname: [%s]\n"),
    1848           3 :                   elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
    1849             :           break;
    1850             : 
    1851           1 :         case DT_RPATH:
    1852           1 :           printf (gettext ("Library rpath: [%s]\n"),
    1853           1 :                   elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
    1854             :           break;
    1855             : 
    1856           1 :         case DT_RUNPATH:
    1857           1 :           printf (gettext ("Library runpath: [%s]\n"),
    1858           1 :                   elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
    1859             :           break;
    1860             : 
    1861         184 :         case DT_PLTRELSZ:
    1862             :         case DT_RELASZ:
    1863             :         case DT_STRSZ:
    1864             :         case DT_RELSZ:
    1865             :         case DT_RELAENT:
    1866             :         case DT_SYMENT:
    1867             :         case DT_RELENT:
    1868             :         case DT_PLTPADSZ:
    1869             :         case DT_MOVEENT:
    1870             :         case DT_MOVESZ:
    1871             :         case DT_INIT_ARRAYSZ:
    1872             :         case DT_FINI_ARRAYSZ:
    1873             :         case DT_SYMINSZ:
    1874             :         case DT_SYMINENT:
    1875             :         case DT_GNU_CONFLICTSZ:
    1876             :         case DT_GNU_LIBLISTSZ:
    1877         184 :           printf (gettext ("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
    1878             :           break;
    1879             : 
    1880          47 :         case DT_VERDEFNUM:
    1881             :         case DT_VERNEEDNUM:
    1882             :         case DT_RELACOUNT:
    1883             :         case DT_RELCOUNT:
    1884          47 :           printf ("%" PRId64 "\n", dyn->d_un.d_val);
    1885             :           break;
    1886             : 
    1887          23 :         case DT_PLTREL:;
    1888          23 :           const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
    1889             :                                                       NULL, 0);
    1890          23 :           puts (tagname ?: "???");
    1891          23 :           break;
    1892             : 
    1893           1 :         case DT_FLAGS:
    1894           1 :           print_dt_flags (class, dyn->d_un.d_val);
    1895             :           break;
    1896             : 
    1897          12 :         case DT_FLAGS_1:
    1898          12 :           print_dt_flags_1 (class, dyn->d_un.d_val);
    1899             :           break;
    1900             : 
    1901           1 :         case DT_FEATURE_1:
    1902           1 :           print_dt_feature_1 (class, dyn->d_un.d_val);
    1903             :           break;
    1904             : 
    1905           1 :         case DT_POSFLAG_1:
    1906           1 :           print_dt_posflag_1 (class, dyn->d_un.d_val);
    1907             :           break;
    1908             : 
    1909         332 :         default:
    1910         332 :           printf ("%#0*" PRIx64 "\n",
    1911             :                   class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
    1912             :           break;
    1913             :         }
    1914             :     }
    1915             : }
    1916             : 
    1917             : 
    1918             : /* Print the dynamic segment.  */
    1919             : static void
    1920          66 : print_dynamic (Ebl *ebl)
    1921             : {
    1922         234 :   for (size_t i = 0; i < phnum; ++i)
    1923             :     {
    1924         202 :       GElf_Phdr phdr_mem;
    1925         202 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
    1926             : 
    1927         202 :       if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
    1928             :         {
    1929          34 :           Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
    1930          34 :           GElf_Shdr shdr_mem;
    1931          34 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1932          34 :           if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)
    1933          27 :             handle_dynamic (ebl, scn, shdr);
    1934          34 :           break;
    1935             :         }
    1936             :     }
    1937          66 : }
    1938             : 
    1939             : 
    1940             : /* Print relocations.  */
    1941             : static void
    1942          65 : print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
    1943             : {
    1944             :   /* Find all relocation sections and handle them.  */
    1945          65 :   Elf_Scn *scn = NULL;
    1946             : 
    1947        1794 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    1948             :     {
    1949             :        /* Handle the section if it is a symbol table.  */
    1950        1729 :       GElf_Shdr shdr_mem;
    1951        1729 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1952             : 
    1953        1729 :       if (likely (shdr != NULL))
    1954             :         {
    1955        1729 :           if (shdr->sh_type == SHT_REL)
    1956          30 :             handle_relocs_rel (ebl, ehdr, scn, shdr);
    1957        1699 :           else if (shdr->sh_type == SHT_RELA)
    1958         190 :             handle_relocs_rela (ebl, ehdr, scn, shdr);
    1959             :         }
    1960             :     }
    1961          65 : }
    1962             : 
    1963             : 
    1964             : /* Handle a relocation section.  */
    1965             : static void
    1966           0 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
    1967             : {
    1968           0 :   int class = gelf_getclass (ebl->elf);
    1969           0 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
    1970           0 :   int nentries = shdr->sh_size / sh_entsize;
    1971             : 
    1972             :   /* Get the data of the section.  */
    1973           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    1974           0 :   if (data == NULL)
    1975           0 :     return;
    1976             : 
    1977             :   /* Get the symbol table information.  */
    1978           0 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    1979           0 :   GElf_Shdr symshdr_mem;
    1980           0 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    1981           0 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    1982             : 
    1983             :   /* Get the section header of the section the relocations are for.  */
    1984           0 :   GElf_Shdr destshdr_mem;
    1985           0 :   GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
    1986             :                                       &destshdr_mem);
    1987             : 
    1988           0 :   if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
    1989             :     {
    1990           0 :       printf (gettext ("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
    1991             :               shdr->sh_offset);
    1992           0 :       return;
    1993             :     }
    1994             : 
    1995             :   /* Search for the optional extended section index table.  */
    1996           0 :   Elf_Data *xndxdata = NULL;
    1997           0 :   int xndxscnidx = elf_scnshndx (scn);
    1998           0 :   if (unlikely (xndxscnidx > 0))
    1999           0 :     xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
    2000             : 
    2001             :   /* Get the section header string table index.  */
    2002           0 :   size_t shstrndx;
    2003           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2004           0 :     error (EXIT_FAILURE, 0,
    2005           0 :            gettext ("cannot get section header string table index"));
    2006             : 
    2007           0 :   if (shdr->sh_info != 0)
    2008           0 :     printf (ngettext ("\
    2009             : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2010             :                     "\
    2011             : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2012             :                       nentries),
    2013             :             elf_ndxscn (scn),
    2014           0 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2015           0 :             (unsigned int) shdr->sh_info,
    2016           0 :             elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
    2017             :             shdr->sh_offset,
    2018             :             nentries);
    2019             :   else
    2020             :     /* The .rel.dyn section does not refer to a specific section but
    2021             :        instead of section index zero.  Do not try to print a section
    2022             :        name.  */
    2023           0 :     printf (ngettext ("\
    2024             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2025             :                     "\
    2026             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2027             :                       nentries),
    2028           0 :             (unsigned int) elf_ndxscn (scn),
    2029           0 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2030             :             shdr->sh_offset,
    2031             :             nentries);
    2032           0 :   fputs_unlocked (class == ELFCLASS32
    2033           0 :                   ? gettext ("\
    2034             :   Offset      Type                 Value       Name\n")
    2035           0 :                   : gettext ("\
    2036             :   Offset              Type                 Value               Name\n"),
    2037             :          stdout);
    2038             : 
    2039           0 :   int is_statically_linked = 0;
    2040           0 :   for (int cnt = 0; cnt < nentries; ++cnt)
    2041             :     {
    2042           0 :       GElf_Rel relmem;
    2043           0 :       GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
    2044           0 :       if (likely (rel != NULL))
    2045             :         {
    2046           0 :           char buf[128];
    2047           0 :           GElf_Sym symmem;
    2048           0 :           Elf32_Word xndx;
    2049           0 :           GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
    2050           0 :                                             GELF_R_SYM (rel->r_info),
    2051             :                                             &symmem, &xndx);
    2052           0 :           if (unlikely (sym == NULL))
    2053             :             {
    2054             :               /* As a special case we have to handle relocations in static
    2055             :                  executables.  This only happens for IRELATIVE relocations
    2056             :                  (so far).  There is no symbol table.  */
    2057           0 :               if (is_statically_linked == 0)
    2058             :                 {
    2059             :                   /* Find the program header and look for a PT_INTERP entry. */
    2060           0 :                   is_statically_linked = -1;
    2061           0 :                   if (ehdr->e_type == ET_EXEC)
    2062             :                     {
    2063             :                       is_statically_linked = 1;
    2064             : 
    2065           0 :                       for (size_t inner = 0; inner < phnum; ++inner)
    2066             :                         {
    2067           0 :                           GElf_Phdr phdr_mem;
    2068           0 :                           GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
    2069             :                                                           &phdr_mem);
    2070           0 :                           if (phdr != NULL && phdr->p_type == PT_INTERP)
    2071             :                             {
    2072           0 :                               is_statically_linked = -1;
    2073           0 :                               break;
    2074             :                             }
    2075             :                         }
    2076             :                     }
    2077             :                 }
    2078             : 
    2079           0 :               if (is_statically_linked > 0 && shdr->sh_link == 0)
    2080           0 :                 printf ("\
    2081             :   %#0*" PRIx64 "  %-20s %*s  %s\n",
    2082             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2083           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2084             :                         /* Avoid the leading R_ which isn't carrying any
    2085             :                            information.  */
    2086           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2087             :                                                buf, sizeof (buf)) + 2
    2088           0 :                         : gettext ("<INVALID RELOC>"),
    2089             :                         class == ELFCLASS32 ? 10 : 18, "",
    2090           0 :                         elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
    2091             :               else
    2092           0 :                 printf ("  %#0*" PRIx64 "  %-20s <%s %ld>\n",
    2093             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2094           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2095             :                         /* Avoid the leading R_ which isn't carrying any
    2096             :                            information.  */
    2097           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2098             :                                                buf, sizeof (buf)) + 2
    2099           0 :                         : gettext ("<INVALID RELOC>"),
    2100             :                         gettext ("INVALID SYMBOL"),
    2101           0 :                         (long int) GELF_R_SYM (rel->r_info));
    2102             :             }
    2103           0 :           else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
    2104           0 :             printf ("  %#0*" PRIx64 "  %-20s %#0*" PRIx64 "  %s\n",
    2105             :                     class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2106           0 :                     likely (ebl_reloc_type_check (ebl,
    2107             :                                                   GELF_R_TYPE (rel->r_info)))
    2108             :                     /* Avoid the leading R_ which isn't carrying any
    2109             :                        information.  */
    2110           0 :                     ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2111             :                                            buf, sizeof (buf)) + 2
    2112           0 :                     : gettext ("<INVALID RELOC>"),
    2113             :                     class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2114           0 :                     elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
    2115             :           else
    2116             :             {
    2117             :               /* This is a relocation against a STT_SECTION symbol.  */
    2118           0 :               GElf_Shdr secshdr_mem;
    2119           0 :               GElf_Shdr *secshdr;
    2120           0 :               secshdr = gelf_getshdr (elf_getscn (ebl->elf,
    2121           0 :                                                   sym->st_shndx == SHN_XINDEX
    2122           0 :                                                   ? xndx : sym->st_shndx),
    2123             :                                       &secshdr_mem);
    2124             : 
    2125           0 :               if (unlikely (secshdr == NULL))
    2126           0 :                 printf ("  %#0*" PRIx64 "  %-20s <%s %ld>\n",
    2127             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2128           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2129             :                         /* Avoid the leading R_ which isn't carrying any
    2130             :                            information.  */
    2131           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2132             :                                                buf, sizeof (buf)) + 2
    2133           0 :                         : gettext ("<INVALID RELOC>"),
    2134             :                         gettext ("INVALID SECTION"),
    2135           0 :                         (long int) (sym->st_shndx == SHN_XINDEX
    2136           0 :                                     ? xndx : sym->st_shndx));
    2137             :               else
    2138           0 :                 printf ("  %#0*" PRIx64 "  %-20s %#0*" PRIx64 "  %s\n",
    2139             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2140           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2141             :                         /* Avoid the leading R_ which isn't carrying any
    2142             :                            information.  */
    2143           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2144             :                                                buf, sizeof (buf)) + 2
    2145           0 :                         : gettext ("<INVALID RELOC>"),
    2146             :                         class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2147           0 :                         elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
    2148             :             }
    2149             :         }
    2150             :     }
    2151             : }
    2152             : 
    2153             : 
    2154             : /* Handle a relocation section.  */
    2155             : static void
    2156           0 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
    2157             : {
    2158           0 :   int class = gelf_getclass (ebl->elf);
    2159           0 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
    2160           0 :   int nentries = shdr->sh_size / sh_entsize;
    2161             : 
    2162             :   /* Get the data of the section.  */
    2163           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    2164           0 :   if (data == NULL)
    2165           0 :     return;
    2166             : 
    2167             :   /* Get the symbol table information.  */
    2168           0 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    2169           0 :   GElf_Shdr symshdr_mem;
    2170           0 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    2171           0 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    2172             : 
    2173             :   /* Get the section header of the section the relocations are for.  */
    2174           0 :   GElf_Shdr destshdr_mem;
    2175           0 :   GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
    2176             :                                       &destshdr_mem);
    2177             : 
    2178           0 :   if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
    2179             :     {
    2180           0 :       printf (gettext ("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
    2181             :               shdr->sh_offset);
    2182           0 :       return;
    2183             :     }
    2184             : 
    2185             :   /* Search for the optional extended section index table.  */
    2186           0 :   Elf_Data *xndxdata = NULL;
    2187           0 :   int xndxscnidx = elf_scnshndx (scn);
    2188           0 :   if (unlikely (xndxscnidx > 0))
    2189           0 :     xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
    2190             : 
    2191             :   /* Get the section header string table index.  */
    2192           0 :   size_t shstrndx;
    2193           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2194           0 :     error (EXIT_FAILURE, 0,
    2195           0 :            gettext ("cannot get section header string table index"));
    2196             : 
    2197           0 :   if (shdr->sh_info != 0)
    2198           0 :     printf (ngettext ("\
    2199             : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2200             :                     "\
    2201             : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2202             :                     nentries),
    2203             :           elf_ndxscn (scn),
    2204           0 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2205           0 :           (unsigned int) shdr->sh_info,
    2206           0 :           elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
    2207             :           shdr->sh_offset,
    2208             :           nentries);
    2209             :   else
    2210             :     /* The .rela.dyn section does not refer to a specific section but
    2211             :        instead of section index zero.  Do not try to print a section
    2212             :        name.  */
    2213           0 :     printf (ngettext ("\
    2214             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2215             :                     "\
    2216             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2217             :                       nentries),
    2218           0 :             (unsigned int) elf_ndxscn (scn),
    2219           0 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2220             :             shdr->sh_offset,
    2221             :             nentries);
    2222           0 :   fputs_unlocked (class == ELFCLASS32
    2223           0 :                   ? gettext ("\
    2224             :   Offset      Type            Value       Addend Name\n")
    2225           0 :                   : gettext ("\
    2226             :   Offset              Type            Value               Addend Name\n"),
    2227             :                   stdout);
    2228             : 
    2229           0 :   int is_statically_linked = 0;
    2230           0 :   for (int cnt = 0; cnt < nentries; ++cnt)
    2231             :     {
    2232           0 :       GElf_Rela relmem;
    2233           0 :       GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
    2234           0 :       if (likely (rel != NULL))
    2235             :         {
    2236           0 :           char buf[64];
    2237           0 :           GElf_Sym symmem;
    2238           0 :           Elf32_Word xndx;
    2239           0 :           GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
    2240           0 :                                             GELF_R_SYM (rel->r_info),
    2241             :                                             &symmem, &xndx);
    2242             : 
    2243           0 :           if (unlikely (sym == NULL))
    2244             :             {
    2245             :               /* As a special case we have to handle relocations in static
    2246             :                  executables.  This only happens for IRELATIVE relocations
    2247             :                  (so far).  There is no symbol table.  */
    2248           0 :               if (is_statically_linked == 0)
    2249             :                 {
    2250             :                   /* Find the program header and look for a PT_INTERP entry. */
    2251           0 :                   is_statically_linked = -1;
    2252           0 :                   if (ehdr->e_type == ET_EXEC)
    2253             :                     {
    2254             :                       is_statically_linked = 1;
    2255             : 
    2256           0 :                       for (size_t inner = 0; inner < phnum; ++inner)
    2257             :                         {
    2258           0 :                           GElf_Phdr phdr_mem;
    2259           0 :                           GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
    2260             :                                                           &phdr_mem);
    2261           0 :                           if (phdr != NULL && phdr->p_type == PT_INTERP)
    2262             :                             {
    2263           0 :                               is_statically_linked = -1;
    2264           0 :                               break;
    2265             :                             }
    2266             :                         }
    2267             :                     }
    2268             :                 }
    2269             : 
    2270           0 :               if (is_statically_linked > 0 && shdr->sh_link == 0)
    2271           0 :                 printf ("\
    2272             :   %#0*" PRIx64 "  %-15s %*s  %#6" PRIx64 " %s\n",
    2273             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2274           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2275             :                         /* Avoid the leading R_ which isn't carrying any
    2276             :                            information.  */
    2277           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2278             :                                                buf, sizeof (buf)) + 2
    2279           0 :                         : gettext ("<INVALID RELOC>"),
    2280             :                         class == ELFCLASS32 ? 10 : 18, "",
    2281             :                         rel->r_addend,
    2282           0 :                         elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
    2283             :               else
    2284           0 :                 printf ("  %#0*" PRIx64 "  %-15s <%s %ld>\n",
    2285             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2286           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2287             :                         /* Avoid the leading R_ which isn't carrying any
    2288             :                            information.  */
    2289           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2290             :                                                buf, sizeof (buf)) + 2
    2291           0 :                         : gettext ("<INVALID RELOC>"),
    2292             :                         gettext ("INVALID SYMBOL"),
    2293           0 :                         (long int) GELF_R_SYM (rel->r_info));
    2294             :             }
    2295           0 :           else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
    2296           0 :             printf ("\
    2297             :   %#0*" PRIx64 "  %-15s %#0*" PRIx64 "  %+6" PRId64 " %s\n",
    2298             :                     class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2299           0 :                     likely (ebl_reloc_type_check (ebl,
    2300             :                                                   GELF_R_TYPE (rel->r_info)))
    2301             :                     /* Avoid the leading R_ which isn't carrying any
    2302             :                        information.  */
    2303           0 :                     ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2304             :                                            buf, sizeof (buf)) + 2
    2305           0 :                     : gettext ("<INVALID RELOC>"),
    2306             :                     class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2307             :                     rel->r_addend,
    2308           0 :                     elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
    2309             :           else
    2310             :             {
    2311             :               /* This is a relocation against a STT_SECTION symbol.  */
    2312           0 :               GElf_Shdr secshdr_mem;
    2313           0 :               GElf_Shdr *secshdr;
    2314           0 :               secshdr = gelf_getshdr (elf_getscn (ebl->elf,
    2315           0 :                                                   sym->st_shndx == SHN_XINDEX
    2316           0 :                                                   ? xndx : sym->st_shndx),
    2317             :                                       &secshdr_mem);
    2318             : 
    2319           0 :               if (unlikely (secshdr == NULL))
    2320           0 :                 printf ("  %#0*" PRIx64 "  %-15s <%s %ld>\n",
    2321             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2322           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2323             :                         /* Avoid the leading R_ which isn't carrying any
    2324             :                            information.  */
    2325           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2326             :                                                buf, sizeof (buf)) + 2
    2327           0 :                         : gettext ("<INVALID RELOC>"),
    2328             :                         gettext ("INVALID SECTION"),
    2329           0 :                         (long int) (sym->st_shndx == SHN_XINDEX
    2330           0 :                                     ? xndx : sym->st_shndx));
    2331             :               else
    2332           0 :                 printf ("\
    2333             :   %#0*" PRIx64 "  %-15s %#0*" PRIx64 "  %+6" PRId64 " %s\n",
    2334             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2335           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2336             :                         /* Avoid the leading R_ which isn't carrying any
    2337             :                            information.  */
    2338           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2339             :                                                buf, sizeof (buf)) + 2
    2340           0 :                         : gettext ("<INVALID RELOC>"),
    2341             :                         class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2342             :                         rel->r_addend,
    2343           0 :                         elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
    2344             :             }
    2345             :         }
    2346             :     }
    2347             : }
    2348             : 
    2349             : 
    2350             : /* Print the program header.  */
    2351             : static void
    2352         157 : print_symtab (Ebl *ebl, int type)
    2353             : {
    2354             :   /* Find the symbol table(s).  For this we have to search through the
    2355             :      section table.  */
    2356         157 :   Elf_Scn *scn = NULL;
    2357             : 
    2358        4340 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    2359             :     {
    2360             :       /* Handle the section if it is a symbol table.  */
    2361        4183 :       GElf_Shdr shdr_mem;
    2362        4183 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2363             : 
    2364        4183 :       if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
    2365             :         {
    2366          90 :           if (symbol_table_section != NULL)
    2367             :             {
    2368             :               /* Get the section header string table index.  */
    2369           4 :               size_t shstrndx;
    2370           4 :               const char *sname;
    2371           4 :               if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2372           0 :                 error (EXIT_FAILURE, 0,
    2373           0 :                        gettext ("cannot get section header string table index"));
    2374           4 :               sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
    2375           4 :               if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
    2376           2 :                 continue;
    2377             :             }
    2378             : 
    2379          88 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    2380             :             {
    2381           0 :               if (elf_compress (scn, 0, 0) < 0)
    2382           0 :                 printf ("WARNING: %s [%zd]\n",
    2383             :                         gettext ("Couldn't uncompress section"),
    2384             :                         elf_ndxscn (scn));
    2385           0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    2386           0 :               if (unlikely (shdr == NULL))
    2387           0 :                 error (EXIT_FAILURE, 0,
    2388           0 :                        gettext ("cannot get section [%zd] header: %s"),
    2389             :                        elf_ndxscn (scn), elf_errmsg (-1));
    2390             :             }
    2391          88 :           handle_symtab (ebl, scn, shdr);
    2392             :         }
    2393             :     }
    2394         157 : }
    2395             : 
    2396             : 
    2397             : static void
    2398          88 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    2399             : {
    2400          88 :   Elf_Data *versym_data = NULL;
    2401          88 :   Elf_Data *verneed_data = NULL;
    2402          88 :   Elf_Data *verdef_data = NULL;
    2403          88 :   Elf_Data *xndx_data = NULL;
    2404          88 :   int class = gelf_getclass (ebl->elf);
    2405          88 :   Elf32_Word verneed_stridx = 0;
    2406          88 :   Elf32_Word verdef_stridx = 0;
    2407             : 
    2408             :   /* Get the data of the section.  */
    2409          88 :   Elf_Data *data = elf_getdata (scn, NULL);
    2410          88 :   if (data == NULL)
    2411           0 :     return;
    2412             : 
    2413             :   /* Find out whether we have other sections we might need.  */
    2414             :   Elf_Scn *runscn = NULL;
    2415        2555 :   while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
    2416             :     {
    2417        2467 :       GElf_Shdr runshdr_mem;
    2418        2467 :       GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
    2419             : 
    2420        2467 :       if (likely (runshdr != NULL))
    2421             :         {
    2422        2467 :           if (runshdr->sh_type == SHT_GNU_versym
    2423          44 :               && runshdr->sh_link == elf_ndxscn (scn))
    2424             :             /* Bingo, found the version information.  Now get the data.  */
    2425          33 :             versym_data = elf_getdata (runscn, NULL);
    2426        2434 :           else if (runshdr->sh_type == SHT_GNU_verneed)
    2427             :             {
    2428             :               /* This is the information about the needed versions.  */
    2429          44 :               verneed_data = elf_getdata (runscn, NULL);
    2430          44 :               verneed_stridx = runshdr->sh_link;
    2431             :             }
    2432        2390 :           else if (runshdr->sh_type == SHT_GNU_verdef)
    2433             :             {
    2434             :               /* This is the information about the defined versions.  */
    2435           4 :               verdef_data = elf_getdata (runscn, NULL);
    2436           4 :               verdef_stridx = runshdr->sh_link;
    2437             :             }
    2438        2386 :           else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
    2439           0 :               && runshdr->sh_link == elf_ndxscn (scn))
    2440             :             /* Extended section index.  */
    2441           0 :             xndx_data = elf_getdata (runscn, NULL);
    2442             :         }
    2443             :     }
    2444             : 
    2445             :   /* Get the section header string table index.  */
    2446          88 :   size_t shstrndx;
    2447          88 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2448           0 :     error (EXIT_FAILURE, 0,
    2449           0 :            gettext ("cannot get section header string table index"));
    2450             : 
    2451          88 :   GElf_Shdr glink_mem;
    2452          88 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    2453             :                                    &glink_mem);
    2454          88 :   if (glink == NULL)
    2455           0 :     error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
    2456             :            elf_ndxscn (scn));
    2457             : 
    2458             :   /* Now we can compute the number of entries in the section.  */
    2459         176 :   unsigned int nsyms = data->d_size / (class == ELFCLASS32
    2460             :                                        ? sizeof (Elf32_Sym)
    2461          88 :                                        : sizeof (Elf64_Sym));
    2462             : 
    2463          88 :   printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
    2464             :                     "\nSymbol table [%2u] '%s' contains %u entries:\n",
    2465             :                     nsyms),
    2466          88 :           (unsigned int) elf_ndxscn (scn),
    2467          88 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
    2468          88 :   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
    2469             :                     " %lu local symbols  String table: [%2u] '%s'\n",
    2470             :                     shdr->sh_info),
    2471          88 :           (unsigned long int) shdr->sh_info,
    2472          88 :           (unsigned int) shdr->sh_link,
    2473          88 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    2474             : 
    2475          88 :   fputs_unlocked (class == ELFCLASS32
    2476          14 :                   ? gettext ("\
    2477             :   Num:    Value   Size Type    Bind   Vis          Ndx Name\n")
    2478          74 :                   : gettext ("\
    2479             :   Num:            Value   Size Type    Bind   Vis          Ndx Name\n"),
    2480             :                   stdout);
    2481             : 
    2482       25706 :   for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
    2483             :     {
    2484       25618 :       char typebuf[64];
    2485       25618 :       char bindbuf[64];
    2486       25618 :       char scnbuf[64];
    2487       25618 :       Elf32_Word xndx;
    2488       25618 :       GElf_Sym sym_mem;
    2489       25618 :       GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx);
    2490             : 
    2491       25618 :       if (unlikely (sym == NULL))
    2492           0 :         continue;
    2493             : 
    2494             :       /* Determine the real section index.  */
    2495       25618 :       if (likely (sym->st_shndx != SHN_XINDEX))
    2496       25618 :         xndx = sym->st_shndx;
    2497             : 
    2498       50878 :       printf (gettext ("\
    2499             : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
    2500             :               cnt,
    2501             :               class == ELFCLASS32 ? 8 : 16,
    2502             :               sym->st_value,
    2503             :               sym->st_size,
    2504       25618 :               ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info),
    2505             :                                     typebuf, sizeof (typebuf)),
    2506       25618 :               ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
    2507             :                                        bindbuf, sizeof (bindbuf)),
    2508       25618 :               get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
    2509       25618 :               ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
    2510             :                                 sizeof (scnbuf), NULL, shnum),
    2511       25618 :               elf_strptr (ebl->elf, shdr->sh_link, sym->st_name));
    2512             : 
    2513       25618 :       if (versym_data != NULL)
    2514             :         {
    2515             :           /* Get the version information.  */
    2516        1314 :           GElf_Versym versym_mem;
    2517        1314 :           GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
    2518             : 
    2519        1314 :           if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
    2520             :             {
    2521        1043 :               bool is_nobits = false;
    2522        1043 :               bool check_def = xndx != SHN_UNDEF;
    2523             : 
    2524        1043 :               if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
    2525             :                 {
    2526        1033 :                   GElf_Shdr symshdr_mem;
    2527        1033 :                   GElf_Shdr *symshdr =
    2528        1033 :                     gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem);
    2529             : 
    2530        1033 :                   is_nobits = (symshdr != NULL
    2531        1033 :                                && symshdr->sh_type == SHT_NOBITS);
    2532             :                 }
    2533             : 
    2534        1043 :               if (is_nobits || ! check_def)
    2535             :                 {
    2536             :                   /* We must test both.  */
    2537         891 :                   GElf_Vernaux vernaux_mem;
    2538         891 :                   GElf_Vernaux *vernaux = NULL;
    2539         891 :                   size_t vn_offset = 0;
    2540             : 
    2541         891 :                   GElf_Verneed verneed_mem;
    2542         891 :                   GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0,
    2543             :                                                            &verneed_mem);
    2544        4190 :                   while (verneed != NULL)
    2545             :                     {
    2546        4190 :                       size_t vna_offset = vn_offset;
    2547             : 
    2548        8380 :                       vernaux = gelf_getvernaux (verneed_data,
    2549        4190 :                                                  vna_offset += verneed->vn_aux,
    2550             :                                                  &vernaux_mem);
    2551        6922 :                       while (vernaux != NULL
    2552        6922 :                              && vernaux->vna_other != *versym
    2553        6031 :                              && vernaux->vna_next != 0)
    2554             :                         {
    2555             :                           /* Update the offset.  */
    2556        2732 :                           vna_offset += vernaux->vna_next;
    2557             : 
    2558        5464 :                           vernaux = (vernaux->vna_next == 0
    2559             :                                      ? NULL
    2560        2732 :                                      : gelf_getvernaux (verneed_data,
    2561             :                                                         vna_offset,
    2562             :                                                         &vernaux_mem));
    2563             :                         }
    2564             : 
    2565             :                       /* Check whether we found the version.  */
    2566        4190 :                       if (vernaux != NULL && vernaux->vna_other == *versym)
    2567             :                         /* Found it.  */
    2568             :                         break;
    2569             : 
    2570        3299 :                       vn_offset += verneed->vn_next;
    2571        3299 :                       verneed = (verneed->vn_next == 0
    2572             :                                  ? NULL
    2573        3299 :                                  : gelf_getverneed (verneed_data, vn_offset,
    2574             :                                                     &verneed_mem));
    2575             :                     }
    2576             : 
    2577         891 :                   if (vernaux != NULL && vernaux->vna_other == *versym)
    2578             :                     {
    2579         891 :                       printf ("@%s (%u)",
    2580             :                               elf_strptr (ebl->elf, verneed_stridx,
    2581         891 :                                           vernaux->vna_name),
    2582             :                               (unsigned int) vernaux->vna_other);
    2583         891 :                       check_def = 0;
    2584             :                     }
    2585           0 :                   else if (unlikely (! is_nobits))
    2586           0 :                     error (0, 0, gettext ("bad dynamic symbol"));
    2587             :                   else
    2588             :                     check_def = 1;
    2589             :                 }
    2590             : 
    2591        1043 :               if (check_def && *versym != 0x8001)
    2592             :                 {
    2593             :                   /* We must test both.  */
    2594         152 :                   size_t vd_offset = 0;
    2595             : 
    2596         152 :                   GElf_Verdef verdef_mem;
    2597         152 :                   GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0,
    2598             :                                                         &verdef_mem);
    2599         433 :                   while (verdef != NULL)
    2600             :                     {
    2601         433 :                       if (verdef->vd_ndx == (*versym & 0x7fff))
    2602             :                         /* Found the definition.  */
    2603             :                         break;
    2604             : 
    2605         281 :                       vd_offset += verdef->vd_next;
    2606         281 :                       verdef = (verdef->vd_next == 0
    2607             :                                 ? NULL
    2608         281 :                                 : gelf_getverdef (verdef_data, vd_offset,
    2609             :                                                   &verdef_mem));
    2610             :                     }
    2611             : 
    2612         152 :                   if (verdef != NULL)
    2613             :                     {
    2614         152 :                       GElf_Verdaux verdaux_mem;
    2615         152 :                       GElf_Verdaux *verdaux
    2616         304 :                         = gelf_getverdaux (verdef_data,
    2617         152 :                                            vd_offset + verdef->vd_aux,
    2618             :                                            &verdaux_mem);
    2619             : 
    2620         152 :                       if (verdaux != NULL)
    2621         304 :                         printf ((*versym & 0x8000) ? "@%s" : "@@%s",
    2622             :                                 elf_strptr (ebl->elf, verdef_stridx,
    2623         152 :                                             verdaux->vda_name));
    2624             :                     }
    2625             :                 }
    2626             :             }
    2627             :         }
    2628             : 
    2629       51236 :       putchar_unlocked ('\n');
    2630             :     }
    2631             : }
    2632             : 
    2633             : 
    2634             : /* Print version information.  */
    2635             : static void
    2636          64 : print_verinfo (Ebl *ebl)
    2637             : {
    2638             :   /* Find the version information sections.  For this we have to
    2639             :      search through the section table.  */
    2640          64 :   Elf_Scn *scn = NULL;
    2641             : 
    2642        1759 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    2643             :     {
    2644             :       /* Handle the section if it is part of the versioning handling.  */
    2645        1695 :       GElf_Shdr shdr_mem;
    2646        1695 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2647             : 
    2648        1695 :       if (likely (shdr != NULL))
    2649             :         {
    2650        1695 :           if (shdr->sh_type == SHT_GNU_verneed)
    2651          25 :             handle_verneed (ebl, scn, shdr);
    2652        1670 :           else if (shdr->sh_type == SHT_GNU_verdef)
    2653           2 :             handle_verdef (ebl, scn, shdr);
    2654        1668 :           else if (shdr->sh_type == SHT_GNU_versym)
    2655          25 :             handle_versym (ebl, scn, shdr);
    2656             :         }
    2657             :     }
    2658          64 : }
    2659             : 
    2660             : 
    2661             : static const char *
    2662         111 : get_ver_flags (unsigned int flags)
    2663             : {
    2664         111 :   static char buf[32];
    2665         111 :   char *endp;
    2666             : 
    2667         111 :   if (flags == 0)
    2668         109 :     return gettext ("none");
    2669             : 
    2670           2 :   if (flags & VER_FLG_BASE)
    2671           4 :     endp = stpcpy (buf, "BASE ");
    2672             :   else
    2673             :     endp = buf;
    2674             : 
    2675           2 :   if (flags & VER_FLG_WEAK)
    2676             :     {
    2677           0 :       if (endp != buf)
    2678           0 :         endp = stpcpy (endp, "| ");
    2679             : 
    2680           0 :       endp = stpcpy (endp, "WEAK ");
    2681             :     }
    2682             : 
    2683           2 :   if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
    2684             :     {
    2685           0 :       strncpy (endp, gettext ("| <unknown>"), buf + sizeof (buf) - endp);
    2686           0 :       buf[sizeof (buf) - 1] = '\0';
    2687             :     }
    2688             : 
    2689             :   return buf;
    2690             : }
    2691             : 
    2692             : 
    2693             : static void
    2694           0 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    2695             : {
    2696           0 :   int class = gelf_getclass (ebl->elf);
    2697             : 
    2698             :   /* Get the data of the section.  */
    2699           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    2700           0 :   if (data == NULL)
    2701           0 :     return;
    2702             : 
    2703             :   /* Get the section header string table index.  */
    2704           0 :   size_t shstrndx;
    2705           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2706           0 :     error (EXIT_FAILURE, 0,
    2707           0 :            gettext ("cannot get section header string table index"));
    2708             : 
    2709           0 :   GElf_Shdr glink_mem;
    2710           0 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    2711             :                                    &glink_mem);
    2712           0 :   if (glink == NULL)
    2713           0 :     error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
    2714             :            elf_ndxscn (scn));
    2715             : 
    2716           0 :   printf (ngettext ("\
    2717             : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    2718             :                     "\
    2719             : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    2720             :                     shdr->sh_info),
    2721           0 :           (unsigned int) elf_ndxscn (scn),
    2722           0 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
    2723             :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    2724             :           shdr->sh_offset,
    2725           0 :           (unsigned int) shdr->sh_link,
    2726           0 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    2727             : 
    2728           0 :   unsigned int offset = 0;
    2729           0 :   for (int cnt = shdr->sh_info; --cnt >= 0; )
    2730             :     {
    2731             :       /* Get the data at the next offset.  */
    2732           0 :       GElf_Verneed needmem;
    2733           0 :       GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
    2734           0 :       if (unlikely (need == NULL))
    2735             :         break;
    2736             : 
    2737           0 :       printf (gettext ("  %#06x: Version: %hu  File: %s  Cnt: %hu\n"),
    2738           0 :               offset, (unsigned short int) need->vn_version,
    2739           0 :               elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
    2740           0 :               (unsigned short int) need->vn_cnt);
    2741             : 
    2742           0 :       unsigned int auxoffset = offset + need->vn_aux;
    2743           0 :       for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
    2744             :         {
    2745           0 :           GElf_Vernaux auxmem;
    2746           0 :           GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
    2747           0 :           if (unlikely (aux == NULL))
    2748             :             break;
    2749             : 
    2750           0 :           printf (gettext ("  %#06x: Name: %s  Flags: %s  Version: %hu\n"),
    2751             :                   auxoffset,
    2752           0 :                   elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
    2753           0 :                   get_ver_flags (aux->vna_flags),
    2754           0 :                   (unsigned short int) aux->vna_other);
    2755             : 
    2756           0 :           if (aux->vna_next == 0)
    2757             :             break;
    2758             : 
    2759           0 :           auxoffset += aux->vna_next;
    2760             :         }
    2761             : 
    2762             :       /* Find the next offset.  */
    2763           0 :       if (need->vn_next == 0)
    2764             :         break;
    2765             : 
    2766           0 :       offset += need->vn_next;
    2767             :     }
    2768             : }
    2769             : 
    2770             : 
    2771             : static void
    2772           0 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    2773             : {
    2774             :   /* Get the data of the section.  */
    2775           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    2776           0 :   if (data == NULL)
    2777           0 :     return;
    2778             : 
    2779             :   /* Get the section header string table index.  */
    2780           0 :   size_t shstrndx;
    2781           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2782           0 :     error (EXIT_FAILURE, 0,
    2783           0 :            gettext ("cannot get section header string table index"));
    2784             : 
    2785           0 :   GElf_Shdr glink_mem;
    2786           0 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    2787             :                                    &glink_mem);
    2788           0 :   if (glink == NULL)
    2789           0 :     error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
    2790             :            elf_ndxscn (scn));
    2791             : 
    2792           0 :   int class = gelf_getclass (ebl->elf);
    2793           0 :   printf (ngettext ("\
    2794             : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    2795             :                     "\
    2796             : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    2797             :                     shdr->sh_info),
    2798           0 :           (unsigned int) elf_ndxscn (scn),
    2799           0 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2800             :           shdr->sh_info,
    2801             :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    2802             :           shdr->sh_offset,
    2803           0 :           (unsigned int) shdr->sh_link,
    2804           0 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    2805             : 
    2806           0 :   unsigned int offset = 0;
    2807           0 :   for (int cnt = shdr->sh_info; --cnt >= 0; )
    2808             :     {
    2809             :       /* Get the data at the next offset.  */
    2810           0 :       GElf_Verdef defmem;
    2811           0 :       GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
    2812           0 :       if (unlikely (def == NULL))
    2813             :         break;
    2814             : 
    2815           0 :       unsigned int auxoffset = offset + def->vd_aux;
    2816           0 :       GElf_Verdaux auxmem;
    2817           0 :       GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
    2818           0 :       if (unlikely (aux == NULL))
    2819             :         break;
    2820             : 
    2821           0 :       printf (gettext ("\
    2822             :   %#06x: Version: %hd  Flags: %s  Index: %hd  Cnt: %hd  Name: %s\n"),
    2823           0 :               offset, def->vd_version,
    2824           0 :               get_ver_flags (def->vd_flags),
    2825           0 :               def->vd_ndx,
    2826           0 :               def->vd_cnt,
    2827           0 :               elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
    2828             : 
    2829           0 :       auxoffset += aux->vda_next;
    2830           0 :       for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
    2831             :         {
    2832           0 :           aux = gelf_getverdaux (data, auxoffset, &auxmem);
    2833           0 :           if (unlikely (aux == NULL))
    2834             :             break;
    2835             : 
    2836           0 :           printf (gettext ("  %#06x: Parent %d: %s\n"),
    2837             :                   auxoffset, cnt2,
    2838           0 :                   elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
    2839             : 
    2840           0 :           if (aux->vda_next == 0)
    2841             :             break;
    2842             : 
    2843           0 :           auxoffset += aux->vda_next;
    2844             :         }
    2845             : 
    2846             :       /* Find the next offset.  */
    2847           0 :       if (def->vd_next == 0)
    2848             :         break;
    2849           0 :       offset += def->vd_next;
    2850             :     }
    2851             : }
    2852             : 
    2853             : 
    2854             : static void
    2855           0 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    2856             : {
    2857           0 :   int class = gelf_getclass (ebl->elf);
    2858           0 :   const char **vername;
    2859           0 :   const char **filename;
    2860             : 
    2861             :   /* Get the data of the section.  */
    2862           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    2863           0 :   if (data == NULL)
    2864           0 :     return;
    2865             : 
    2866             :   /* Get the section header string table index.  */
    2867           0 :   size_t shstrndx;
    2868           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2869           0 :     error (EXIT_FAILURE, 0,
    2870           0 :            gettext ("cannot get section header string table index"));
    2871             : 
    2872             :   /* We have to find the version definition section and extract the
    2873             :      version names.  */
    2874             :   Elf_Scn *defscn = NULL;
    2875             :   Elf_Scn *needscn = NULL;
    2876             : 
    2877             :   Elf_Scn *verscn = NULL;
    2878           0 :   while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
    2879             :     {
    2880           0 :       GElf_Shdr vershdr_mem;
    2881           0 :       GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
    2882             : 
    2883           0 :       if (likely (vershdr != NULL))
    2884             :         {
    2885           0 :           if (vershdr->sh_type == SHT_GNU_verdef)
    2886             :             defscn = verscn;
    2887           0 :           else if (vershdr->sh_type == SHT_GNU_verneed)
    2888           0 :             needscn = verscn;
    2889             :         }
    2890             :     }
    2891             : 
    2892           0 :   size_t nvername;
    2893           0 :   if (defscn != NULL || needscn != NULL)
    2894             :     {
    2895             :       /* We have a version information (better should have).  Now get
    2896             :          the version names.  First find the maximum version number.  */
    2897           0 :       nvername = 0;
    2898           0 :       if (defscn != NULL)
    2899             :         {
    2900             :           /* Run through the version definitions and find the highest
    2901             :              index.  */
    2902           0 :           unsigned int offset = 0;
    2903           0 :           Elf_Data *defdata;
    2904           0 :           GElf_Shdr defshdrmem;
    2905           0 :           GElf_Shdr *defshdr;
    2906             : 
    2907           0 :           defdata = elf_getdata (defscn, NULL);
    2908           0 :           if (unlikely (defdata == NULL))
    2909           0 :             return;
    2910             : 
    2911           0 :           defshdr = gelf_getshdr (defscn, &defshdrmem);
    2912           0 :           if (unlikely (defshdr == NULL))
    2913           0 :             return;
    2914             : 
    2915           0 :           for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
    2916             :             {
    2917           0 :               GElf_Verdef defmem;
    2918           0 :               GElf_Verdef *def;
    2919             : 
    2920             :               /* Get the data at the next offset.  */
    2921           0 :               def = gelf_getverdef (defdata, offset, &defmem);
    2922           0 :               if (unlikely (def == NULL))
    2923             :                 break;
    2924             : 
    2925           0 :               nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
    2926             : 
    2927           0 :               if (def->vd_next == 0)
    2928             :                 break;
    2929           0 :               offset += def->vd_next;
    2930             :             }
    2931             :         }
    2932           0 :       if (needscn != NULL)
    2933             :         {
    2934           0 :           unsigned int offset = 0;
    2935           0 :           Elf_Data *needdata;
    2936           0 :           GElf_Shdr needshdrmem;
    2937           0 :           GElf_Shdr *needshdr;
    2938             : 
    2939           0 :           needdata = elf_getdata (needscn, NULL);
    2940           0 :           if (unlikely (needdata == NULL))
    2941           0 :             return;
    2942             : 
    2943           0 :           needshdr = gelf_getshdr (needscn, &needshdrmem);
    2944           0 :           if (unlikely (needshdr == NULL))
    2945           0 :             return;
    2946             : 
    2947           0 :           for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
    2948             :             {
    2949           0 :               GElf_Verneed needmem;
    2950           0 :               GElf_Verneed *need;
    2951           0 :               unsigned int auxoffset;
    2952           0 :               int cnt2;
    2953             : 
    2954             :               /* Get the data at the next offset.  */
    2955           0 :               need = gelf_getverneed (needdata, offset, &needmem);
    2956           0 :               if (unlikely (need == NULL))
    2957             :                 break;
    2958             : 
    2959             :               /* Run through the auxiliary entries.  */
    2960           0 :               auxoffset = offset + need->vn_aux;
    2961           0 :               for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
    2962             :                 {
    2963           0 :                   GElf_Vernaux auxmem;
    2964           0 :                   GElf_Vernaux *aux;
    2965             : 
    2966           0 :                   aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
    2967           0 :                   if (unlikely (aux == NULL))
    2968             :                     break;
    2969             : 
    2970           0 :                   nvername = MAX (nvername,
    2971             :                                   (size_t) (aux->vna_other & 0x7fff));
    2972             : 
    2973           0 :                   if (aux->vna_next == 0)
    2974             :                     break;
    2975           0 :                   auxoffset += aux->vna_next;
    2976             :                 }
    2977             : 
    2978           0 :               if (need->vn_next == 0)
    2979             :                 break;
    2980           0 :               offset += need->vn_next;
    2981             :             }
    2982             :         }
    2983             : 
    2984             :       /* This is the number of versions we know about.  */
    2985           0 :       ++nvername;
    2986             : 
    2987             :       /* Allocate the array.  */
    2988           0 :       vername = (const char **) alloca (nvername * sizeof (const char *));
    2989           0 :       memset(vername, 0, nvername * sizeof (const char *));
    2990           0 :       filename = (const char **) alloca (nvername * sizeof (const char *));
    2991           0 :       memset(filename, 0, nvername * sizeof (const char *));
    2992             : 
    2993             :       /* Run through the data structures again and collect the strings.  */
    2994           0 :       if (defscn != NULL)
    2995             :         {
    2996             :           /* Run through the version definitions and find the highest
    2997             :              index.  */
    2998           0 :           unsigned int offset = 0;
    2999           0 :           Elf_Data *defdata;
    3000           0 :           GElf_Shdr defshdrmem;
    3001           0 :           GElf_Shdr *defshdr;
    3002             : 
    3003           0 :           defdata = elf_getdata (defscn, NULL);
    3004           0 :           if (unlikely (defdata == NULL))
    3005           0 :             return;
    3006             : 
    3007           0 :           defshdr = gelf_getshdr (defscn, &defshdrmem);
    3008           0 :           if (unlikely (defshdr == NULL))
    3009           0 :             return;
    3010             : 
    3011           0 :           for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
    3012             :             {
    3013             : 
    3014             :               /* Get the data at the next offset.  */
    3015           0 :               GElf_Verdef defmem;
    3016           0 :               GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
    3017           0 :               if (unlikely (def == NULL))
    3018             :                 break;
    3019             : 
    3020           0 :               GElf_Verdaux auxmem;
    3021           0 :               GElf_Verdaux *aux = gelf_getverdaux (defdata,
    3022           0 :                                                    offset + def->vd_aux,
    3023             :                                                    &auxmem);
    3024           0 :               if (unlikely (aux == NULL))
    3025             :                 break;
    3026             : 
    3027           0 :               vername[def->vd_ndx & 0x7fff]
    3028           0 :                 = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
    3029           0 :               filename[def->vd_ndx & 0x7fff] = NULL;
    3030             : 
    3031           0 :               if (def->vd_next == 0)
    3032             :                 break;
    3033           0 :               offset += def->vd_next;
    3034             :             }
    3035             :         }
    3036           0 :       if (needscn != NULL)
    3037             :         {
    3038           0 :           unsigned int offset = 0;
    3039             : 
    3040           0 :           Elf_Data *needdata = elf_getdata (needscn, NULL);
    3041           0 :           GElf_Shdr needshdrmem;
    3042           0 :           GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
    3043           0 :           if (unlikely (needdata == NULL || needshdr == NULL))
    3044           0 :             return;
    3045             : 
    3046           0 :           for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
    3047             :             {
    3048             :               /* Get the data at the next offset.  */
    3049           0 :               GElf_Verneed needmem;
    3050           0 :               GElf_Verneed *need = gelf_getverneed (needdata, offset,
    3051             :                                                     &needmem);
    3052           0 :               if (unlikely (need == NULL))
    3053             :                 break;
    3054             : 
    3055             :               /* Run through the auxiliary entries.  */
    3056           0 :               unsigned int auxoffset = offset + need->vn_aux;
    3057           0 :               for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
    3058             :                 {
    3059           0 :                   GElf_Vernaux auxmem;
    3060           0 :                   GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
    3061             :                                                        &auxmem);
    3062           0 :                   if (unlikely (aux == NULL))
    3063             :                     break;
    3064             : 
    3065           0 :                   vername[aux->vna_other & 0x7fff]
    3066           0 :                     = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
    3067           0 :                   filename[aux->vna_other & 0x7fff]
    3068           0 :                     = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
    3069             : 
    3070           0 :                   if (aux->vna_next == 0)
    3071             :                     break;
    3072           0 :                   auxoffset += aux->vna_next;
    3073             :                 }
    3074             : 
    3075           0 :               if (need->vn_next == 0)
    3076             :                 break;
    3077           0 :               offset += need->vn_next;
    3078             :             }
    3079             :         }
    3080             :     }
    3081             :   else
    3082             :     {
    3083             :       vername = NULL;
    3084             :       nvername = 1;
    3085             :       filename = NULL;
    3086             :     }
    3087             : 
    3088           0 :   GElf_Shdr glink_mem;
    3089           0 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    3090             :                                    &glink_mem);
    3091           0 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
    3092           0 :   if (glink == NULL)
    3093           0 :     error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
    3094             :            elf_ndxscn (scn));
    3095             : 
    3096             :   /* Print the header.  */
    3097           0 :   printf (ngettext ("\
    3098             : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
    3099             :                     "\
    3100             : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
    3101             :                     shdr->sh_size / sh_entsize),
    3102           0 :           (unsigned int) elf_ndxscn (scn),
    3103           0 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3104           0 :           (int) (shdr->sh_size / sh_entsize),
    3105             :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    3106             :           shdr->sh_offset,
    3107           0 :           (unsigned int) shdr->sh_link,
    3108           0 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3109             : 
    3110             :   /* Now we can finally look at the actual contents of this section.  */
    3111           0 :   for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
    3112             :     {
    3113           0 :       if (cnt % 2 == 0)
    3114           0 :         printf ("\n %4d:", cnt);
    3115             : 
    3116           0 :       GElf_Versym symmem;
    3117           0 :       GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
    3118           0 :       if (sym == NULL)
    3119             :         break;
    3120             : 
    3121           0 :       switch (*sym)
    3122             :         {
    3123           0 :           ssize_t n;
    3124           0 :         case 0:
    3125           0 :           fputs_unlocked (gettext ("   0 *local*                     "),
    3126             :                           stdout);
    3127           0 :           break;
    3128             : 
    3129           0 :         case 1:
    3130           0 :           fputs_unlocked (gettext ("   1 *global*                    "),
    3131             :                           stdout);
    3132           0 :           break;
    3133             : 
    3134           0 :         default:
    3135           0 :           n = printf ("%4d%c%s",
    3136           0 :                       *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
    3137             :                       (vername != NULL
    3138           0 :                        && (unsigned int) (*sym & 0x7fff) < nvername)
    3139           0 :                       ? vername[*sym & 0x7fff] : "???");
    3140           0 :           if ((unsigned int) (*sym & 0x7fff) < nvername
    3141           0 :               && filename != NULL && filename[*sym & 0x7fff] != NULL)
    3142           0 :             n += printf ("(%s)", filename[*sym & 0x7fff]);
    3143           0 :           printf ("%*s", MAX (0, 33 - (int) n), " ");
    3144             :           break;
    3145             :         }
    3146             :     }
    3147           0 :   putchar_unlocked ('\n');
    3148             : }
    3149             : 
    3150             : 
    3151             : static void
    3152           0 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
    3153             :                  uint_fast32_t maxlength, Elf32_Word nbucket,
    3154             :                  uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
    3155             : {
    3156           0 :   uint32_t *counts = (uint32_t *) xcalloc (maxlength + 1, sizeof (uint32_t));
    3157             : 
    3158           0 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3159           0 :     ++counts[lengths[cnt]];
    3160             : 
    3161           0 :   GElf_Shdr glink_mem;
    3162           0 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
    3163           0 :                                                shdr->sh_link),
    3164             :                                    &glink_mem);
    3165           0 :   if (glink == NULL)
    3166             :     {
    3167           0 :       error (0, 0, gettext ("invalid sh_link value in section %zu"),
    3168             :              elf_ndxscn (scn));
    3169           0 :       return;
    3170             :     }
    3171             : 
    3172           0 :   printf (ngettext ("\
    3173             : \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",
    3174             :                     "\
    3175             : \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",
    3176             :                     nbucket),
    3177           0 :           (unsigned int) elf_ndxscn (scn),
    3178           0 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3179             :           (int) nbucket,
    3180           0 :           gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
    3181             :           shdr->sh_addr,
    3182             :           shdr->sh_offset,
    3183           0 :           (unsigned int) shdr->sh_link,
    3184           0 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3185             : 
    3186           0 :   if (extrastr != NULL)
    3187           0 :     fputs (extrastr, stdout);
    3188             : 
    3189           0 :   if (likely (nbucket > 0))
    3190             :     {
    3191           0 :       uint64_t success = 0;
    3192             : 
    3193             :       /* xgettext:no-c-format */
    3194           0 :       fputs_unlocked (gettext ("\
    3195             :  Length  Number  % of total  Coverage\n"), stdout);
    3196           0 :       printf (gettext ("      0  %6" PRIu32 "      %5.1f%%\n"),
    3197           0 :               counts[0], (counts[0] * 100.0) / nbucket);
    3198             : 
    3199           0 :       uint64_t nzero_counts = 0;
    3200           0 :       for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
    3201             :         {
    3202           0 :           nzero_counts += counts[cnt] * cnt;
    3203           0 :           printf (gettext ("\
    3204             : %7d  %6" PRIu32 "      %5.1f%%    %5.1f%%\n"),
    3205           0 :                   (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
    3206           0 :                   (nzero_counts * 100.0) / nsyms);
    3207             :         }
    3208             : 
    3209             :       Elf32_Word acc = 0;
    3210           0 :       for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
    3211             :         {
    3212           0 :           acc += cnt;
    3213           0 :           success += counts[cnt] * acc;
    3214             :         }
    3215             : 
    3216           0 :       printf (gettext ("\
    3217             :  Average number of tests:   successful lookup: %f\n\
    3218             :                           unsuccessful lookup: %f\n"),
    3219           0 :               (double) success / (double) nzero_counts,
    3220           0 :               (double) nzero_counts / (double) nbucket);
    3221             :     }
    3222             : 
    3223           0 :   free (counts);
    3224             : }
    3225             : 
    3226             : 
    3227             : /* This function handles the traditional System V-style hash table format.  */
    3228             : static void
    3229           0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3230             : {
    3231           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    3232           0 :   if (unlikely (data == NULL))
    3233             :     {
    3234           0 :       error (0, 0, gettext ("cannot get data for section %d: %s"),
    3235           0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3236           0 :       return;
    3237             :     }
    3238             : 
    3239           0 :   if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
    3240             :     {
    3241           0 :     invalid_data:
    3242           0 :       error (0, 0, gettext ("invalid data in sysv.hash section %d"),
    3243           0 :              (int) elf_ndxscn (scn));
    3244           0 :       return;
    3245             :     }
    3246             : 
    3247           0 :   Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
    3248           0 :   Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
    3249             : 
    3250           0 :   uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
    3251           0 :   if (used_buf > data->d_size)
    3252             :     goto invalid_data;
    3253             : 
    3254           0 :   Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
    3255           0 :   Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
    3256             : 
    3257           0 :   uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
    3258             : 
    3259           0 :   uint_fast32_t maxlength = 0;
    3260           0 :   uint_fast32_t nsyms = 0;
    3261           0 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3262             :     {
    3263           0 :       Elf32_Word inner = bucket[cnt];
    3264           0 :       Elf32_Word chain_len = 0;
    3265           0 :       while (inner > 0 && inner < nchain)
    3266             :         {
    3267           0 :           ++nsyms;
    3268           0 :           ++chain_len;
    3269           0 :           if (chain_len > nchain)
    3270             :             {
    3271           0 :               error (0, 0, gettext ("invalid chain in sysv.hash section %d"),
    3272           0 :                      (int) elf_ndxscn (scn));
    3273           0 :               free (lengths);
    3274           0 :               return;
    3275             :             }
    3276           0 :           if (maxlength < ++lengths[cnt])
    3277           0 :             ++maxlength;
    3278             : 
    3279           0 :           inner = chain[inner];
    3280             :         }
    3281             :     }
    3282             : 
    3283           0 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    3284             :                    lengths, NULL);
    3285             : 
    3286           0 :   free (lengths);
    3287             : }
    3288             : 
    3289             : 
    3290             : /* This function handles the incorrect, System V-style hash table
    3291             :    format some 64-bit architectures use.  */
    3292             : static void
    3293           0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3294             : {
    3295           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    3296           0 :   if (unlikely (data == NULL))
    3297             :     {
    3298           0 :       error (0, 0, gettext ("cannot get data for section %d: %s"),
    3299           0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3300           0 :       return;
    3301             :     }
    3302             : 
    3303           0 :   if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
    3304             :     {
    3305           0 :     invalid_data:
    3306           0 :       error (0, 0, gettext ("invalid data in sysv.hash64 section %d"),
    3307           0 :              (int) elf_ndxscn (scn));
    3308           0 :       return;
    3309             :     }
    3310             : 
    3311           0 :   Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
    3312           0 :   Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
    3313             : 
    3314           0 :   uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
    3315           0 :   if (maxwords < 2
    3316           0 :       || maxwords - 2 < nbucket
    3317           0 :       || maxwords - 2 - nbucket < nchain)
    3318             :     goto invalid_data;
    3319             : 
    3320           0 :   Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
    3321           0 :   Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
    3322             : 
    3323           0 :   uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
    3324             : 
    3325           0 :   uint_fast32_t maxlength = 0;
    3326           0 :   uint_fast32_t nsyms = 0;
    3327           0 :   for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
    3328             :     {
    3329           0 :       Elf64_Xword inner = bucket[cnt];
    3330           0 :       Elf64_Xword chain_len = 0;
    3331           0 :       while (inner > 0 && inner < nchain)
    3332             :         {
    3333           0 :           ++nsyms;
    3334           0 :           ++chain_len;
    3335           0 :           if (chain_len > nchain)
    3336             :             {
    3337           0 :               error (0, 0, gettext ("invalid chain in sysv.hash64 section %d"),
    3338           0 :                      (int) elf_ndxscn (scn));
    3339           0 :               free (lengths);
    3340           0 :               return;
    3341             :             }
    3342           0 :           if (maxlength < ++lengths[cnt])
    3343           0 :             ++maxlength;
    3344             : 
    3345           0 :           inner = chain[inner];
    3346             :         }
    3347             :     }
    3348             : 
    3349           0 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    3350             :                    lengths, NULL);
    3351             : 
    3352           0 :   free (lengths);
    3353             : }
    3354             : 
    3355             : 
    3356             : /* This function handles the GNU-style hash table format.  */
    3357             : static void
    3358          25 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3359             : {
    3360          25 :   uint32_t *lengths = NULL;
    3361          25 :   Elf_Data *data = elf_getdata (scn, NULL);
    3362          25 :   if (unlikely (data == NULL))
    3363             :     {
    3364           0 :       error (0, 0, gettext ("cannot get data for section %d: %s"),
    3365           0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3366           0 :       return;
    3367             :     }
    3368             : 
    3369          25 :   if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
    3370             :     {
    3371           0 :     invalid_data:
    3372           0 :       free (lengths);
    3373           0 :       error (0, 0, gettext ("invalid data in gnu.hash section %d"),
    3374           0 :              (int) elf_ndxscn (scn));
    3375           0 :       return;
    3376             :     }
    3377             : 
    3378          25 :   Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
    3379          25 :   Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
    3380             : 
    3381             :   /* Next comes the size of the bitmap.  It's measured in words for
    3382             :      the architecture.  It's 32 bits for 32 bit archs, and 64 bits for
    3383             :      64 bit archs.  There is always a bloom filter present, so zero is
    3384             :      an invalid value.  */
    3385          25 :   Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
    3386          25 :   if (gelf_getclass (ebl->elf) == ELFCLASS64)
    3387          21 :     bitmask_words *= 2;
    3388             : 
    3389          25 :   if (bitmask_words == 0)
    3390             :     goto invalid_data;
    3391             : 
    3392          25 :   Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
    3393             : 
    3394             :   /* Is there still room for the sym chain?
    3395             :      Use uint64_t calculation to prevent 32bit overlow.  */
    3396          25 :   uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
    3397          25 :   uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
    3398          25 :   if (used_buf > data->d_size)
    3399             :     goto invalid_data;
    3400             : 
    3401          25 :   lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
    3402             : 
    3403          25 :   Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
    3404          25 :   Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
    3405          50 :   Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
    3406          25 :                                                     + nbucket];
    3407             : 
    3408             :   /* Compute distribution of chain lengths.  */
    3409          25 :   uint_fast32_t maxlength = 0;
    3410          25 :   uint_fast32_t nsyms = 0;
    3411         181 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3412         156 :     if (bucket[cnt] != 0)
    3413             :       {
    3414         100 :         Elf32_Word inner = bucket[cnt] - symbias;
    3415         201 :         do
    3416             :           {
    3417         201 :             ++nsyms;
    3418         201 :             if (maxlength < ++lengths[cnt])
    3419          39 :               ++maxlength;
    3420         201 :             if (inner >= max_nsyms)
    3421             :               goto invalid_data;
    3422             :           }
    3423         201 :         while ((chain[inner++] & 1) == 0);
    3424             :       }
    3425             : 
    3426             :   /* Count bits in bitmask.  */
    3427             :   uint_fast32_t nbits = 0;
    3428         107 :   for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
    3429             :     {
    3430          82 :       uint_fast32_t word = bitmask[cnt];
    3431             : 
    3432          82 :       word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
    3433          82 :       word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
    3434          82 :       word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
    3435          82 :       word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
    3436          82 :       nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
    3437             :     }
    3438             : 
    3439          25 :   char *str;
    3440          25 :   if (unlikely (asprintf (&str, gettext ("\
    3441             :  Symbol Bias: %u\n\
    3442             :  Bitmask Size: %zu bytes  %" PRIuFAST32 "%% bits set  2nd hash shift: %u\n"),
    3443             :                           (unsigned int) symbias,
    3444             :                           bitmask_words * sizeof (Elf32_Word),
    3445             :                           ((nbits * 100 + 50)
    3446             :                            / (uint_fast32_t) (bitmask_words
    3447             :                                               * sizeof (Elf32_Word) * 8)),
    3448             :                           (unsigned int) shift) == -1))
    3449           0 :     error (EXIT_FAILURE, 0, gettext ("memory exhausted"));
    3450             : 
    3451          25 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    3452             :                    lengths, str);
    3453             : 
    3454          25 :   free (str);
    3455          25 :   free (lengths);
    3456             : }
    3457             : 
    3458             : 
    3459             : /* Find the symbol table(s).  For this we have to search through the
    3460             :    section table.  */
    3461             : static void
    3462          64 : handle_hash (Ebl *ebl)
    3463             : {
    3464             :   /* Get the section header string table index.  */
    3465          64 :   size_t shstrndx;
    3466          64 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3467           0 :     error (EXIT_FAILURE, 0,
    3468           0 :            gettext ("cannot get section header string table index"));
    3469             : 
    3470             :   Elf_Scn *scn = NULL;
    3471        1759 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    3472             :     {
    3473             :       /* Handle the section if it is a symbol table.  */
    3474        1695 :       GElf_Shdr shdr_mem;
    3475        1695 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    3476             : 
    3477        1695 :       if (likely (shdr != NULL))
    3478             :         {
    3479        1695 :           if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
    3480          25 :               && (shdr->sh_flags & SHF_COMPRESSED) != 0)
    3481             :             {
    3482           0 :               if (elf_compress (scn, 0, 0) < 0)
    3483           0 :                 printf ("WARNING: %s [%zd]\n",
    3484             :                         gettext ("Couldn't uncompress section"),
    3485             :                         elf_ndxscn (scn));
    3486           0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    3487           0 :               if (unlikely (shdr == NULL))
    3488           0 :                 error (EXIT_FAILURE, 0,
    3489           0 :                        gettext ("cannot get section [%zd] header: %s"),
    3490             :                        elf_ndxscn (scn), elf_errmsg (-1));
    3491             :             }
    3492             : 
    3493        1695 :           if (shdr->sh_type == SHT_HASH)
    3494             :             {
    3495           0 :               if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
    3496           0 :                 handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
    3497             :               else
    3498           0 :                 handle_sysv_hash (ebl, scn, shdr, shstrndx);
    3499             :             }
    3500        1695 :           else if (shdr->sh_type == SHT_GNU_HASH)
    3501          25 :             handle_gnu_hash (ebl, scn, shdr, shstrndx);
    3502             :         }
    3503             :     }
    3504          64 : }
    3505             : 
    3506             : 
    3507             : static void
    3508           0 : print_liblist (Ebl *ebl)
    3509             : {
    3510             :   /* Find the library list sections.  For this we have to search
    3511             :      through the section table.  */
    3512           0 :   Elf_Scn *scn = NULL;
    3513             : 
    3514             :   /* Get the section header string table index.  */
    3515           0 :   size_t shstrndx;
    3516           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3517           0 :     error (EXIT_FAILURE, 0,
    3518           0 :            gettext ("cannot get section header string table index"));
    3519             : 
    3520           0 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    3521             :     {
    3522           0 :       GElf_Shdr shdr_mem;
    3523           0 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    3524             : 
    3525           0 :       if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
    3526             :         {
    3527           0 :           size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
    3528           0 :           int nentries = shdr->sh_size / sh_entsize;
    3529           0 :           printf (ngettext ("\
    3530             : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    3531             :                             "\
    3532             : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    3533             :                             nentries),
    3534             :                   elf_ndxscn (scn),
    3535           0 :                   elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3536             :                   shdr->sh_offset,
    3537             :                   nentries);
    3538             : 
    3539           0 :           Elf_Data *data = elf_getdata (scn, NULL);
    3540           0 :           if (data == NULL)
    3541           0 :             return;
    3542             : 
    3543           0 :           puts (gettext ("\
    3544             :        Library                       Time Stamp          Checksum Version Flags"));
    3545             : 
    3546           0 :           for (int cnt = 0; cnt < nentries; ++cnt)
    3547             :             {
    3548           0 :               GElf_Lib lib_mem;
    3549           0 :               GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
    3550           0 :               if (unlikely (lib == NULL))
    3551           0 :                 continue;
    3552             : 
    3553           0 :               time_t t = (time_t) lib->l_time_stamp;
    3554           0 :               struct tm *tm = gmtime (&t);
    3555           0 :               if (unlikely (tm == NULL))
    3556           0 :                 continue;
    3557             : 
    3558           0 :               printf ("  [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
    3559           0 :                       cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
    3560           0 :                       tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
    3561             :                       tm->tm_hour, tm->tm_min, tm->tm_sec,
    3562           0 :                       (unsigned int) lib->l_checksum,
    3563           0 :                       (unsigned int) lib->l_version,
    3564           0 :                       (unsigned int) lib->l_flags);
    3565             :             }
    3566             :         }
    3567             :     }
    3568             : }
    3569             : 
    3570             : static void
    3571           0 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
    3572             : {
    3573             :   /* Find the object attributes sections.  For this we have to search
    3574             :      through the section table.  */
    3575           0 :   Elf_Scn *scn = NULL;
    3576             : 
    3577             :   /* Get the section header string table index.  */
    3578           0 :   size_t shstrndx;
    3579           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3580           0 :     error (EXIT_FAILURE, 0,
    3581           0 :            gettext ("cannot get section header string table index"));
    3582             : 
    3583           0 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    3584             :     {
    3585           0 :       GElf_Shdr shdr_mem;
    3586           0 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    3587             : 
    3588           0 :       if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
    3589           0 :                            && (shdr->sh_type != SHT_ARM_ATTRIBUTES
    3590           0 :                                || ehdr->e_machine != EM_ARM)
    3591           0 :                            && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
    3592           0 :                                || ehdr->e_machine != EM_CSKY)))
    3593           0 :         continue;
    3594             : 
    3595           0 :       printf (gettext ("\
    3596             : \nObject attributes section [%2zu] '%s' of %" PRIu64
    3597             :                        " bytes at offset %#0" PRIx64 ":\n"),
    3598             :               elf_ndxscn (scn),
    3599           0 :               elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3600             :               shdr->sh_size, shdr->sh_offset);
    3601             : 
    3602           0 :       Elf_Data *data = elf_rawdata (scn, NULL);
    3603           0 :       if (unlikely (data == NULL || data->d_size == 0))
    3604           0 :         return;
    3605             : 
    3606           0 :       const unsigned char *p = data->d_buf;
    3607             : 
    3608             :       /* There is only one 'version', A.  */
    3609           0 :       if (unlikely (*p++ != 'A'))
    3610           0 :         return;
    3611             : 
    3612           0 :       fputs_unlocked (gettext ("  Owner          Size\n"), stdout);
    3613             : 
    3614           0 :       inline size_t left (void)
    3615             :       {
    3616           0 :         return (const unsigned char *) data->d_buf + data->d_size - p;
    3617             :       }
    3618             : 
    3619             :       /* Loop over the sections.  */
    3620           0 :       while (left () >= 4)
    3621             :         {
    3622             :           /* Section length.  */
    3623           0 :           uint32_t len;
    3624           0 :           memcpy (&len, p, sizeof len);
    3625             : 
    3626           0 :           if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
    3627           0 :             CONVERT (len);
    3628             : 
    3629           0 :           if (unlikely (len > left ()))
    3630             :             break;
    3631             : 
    3632             :           /* Section vendor name.  */
    3633           0 :           const unsigned char *name = p + sizeof len;
    3634           0 :           p += len;
    3635             : 
    3636           0 :           unsigned const char *q = memchr (name, '\0', len);
    3637           0 :           if (unlikely (q == NULL))
    3638             :             break;
    3639           0 :           ++q;
    3640             : 
    3641           0 :           printf (gettext ("  %-13s  %4" PRIu32 "\n"), name, len);
    3642             : 
    3643           0 :           bool gnu_vendor = (q - name == sizeof "gnu"
    3644           0 :                              && !memcmp (name, "gnu", sizeof "gnu"));
    3645             : 
    3646             :           /* Loop over subsections.  */
    3647           0 :           if (shdr->sh_type != SHT_GNU_ATTRIBUTES
    3648           0 :               || gnu_vendor)
    3649           0 :             while (q < p)
    3650             :               {
    3651           0 :                 const unsigned char *const sub = q;
    3652             : 
    3653           0 :                 unsigned int subsection_tag;
    3654           0 :                 get_uleb128 (subsection_tag, q, p);
    3655           0 :                 if (unlikely (q >= p))
    3656             :                   break;
    3657             : 
    3658           0 :                 uint32_t subsection_len;
    3659           0 :                 if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
    3660             :                   break;
    3661             : 
    3662           0 :                 memcpy (&subsection_len, q, sizeof subsection_len);
    3663             : 
    3664           0 :                 if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
    3665           0 :                   CONVERT (subsection_len);
    3666             : 
    3667             :                 /* Don't overflow, ptrdiff_t might be 32bits, but signed.  */
    3668           0 :                 if (unlikely (subsection_len == 0
    3669             :                               || subsection_len >= (uint32_t) PTRDIFF_MAX
    3670             :                               || p - sub < (ptrdiff_t) subsection_len))
    3671             :                   break;
    3672             : 
    3673           0 :                 const unsigned char *r = q + sizeof subsection_len;
    3674           0 :                 q = sub + subsection_len;
    3675             : 
    3676           0 :                 switch (subsection_tag)
    3677             :                   {
    3678           0 :                   default:
    3679             :                     /* Unknown subsection, print and skip.  */
    3680           0 :                     printf (gettext ("    %-4u %12" PRIu32 "\n"),
    3681             :                             subsection_tag, subsection_len);
    3682             :                     break;
    3683             : 
    3684           0 :                   case 1:       /* Tag_File */
    3685           0 :                     printf (gettext ("    File: %11" PRIu32 "\n"),
    3686             :                             subsection_len);
    3687             : 
    3688           0 :                     while (r < q)
    3689           0 :                       {
    3690           0 :                         unsigned int tag;
    3691           0 :                         get_uleb128 (tag, r, q);
    3692           0 :                         if (unlikely (r >= q))
    3693             :                           break;
    3694             : 
    3695             :                         /* GNU style tags have either a uleb128 value,
    3696             :                            when lowest bit is not set, or a string
    3697             :                            when the lowest bit is set.
    3698             :                            "compatibility" (32) is special.  It has
    3699             :                            both a string and a uleb128 value.  For
    3700             :                            non-gnu we assume 6 till 31 only take ints.
    3701             :                            XXX see arm backend, do we need a separate
    3702             :                            hook?  */
    3703           0 :                         uint64_t value = 0;
    3704           0 :                         const char *string = NULL;
    3705           0 :                         if (tag == 32 || (tag & 1) == 0
    3706           0 :                             || (! gnu_vendor && (tag > 5 && tag < 32)))
    3707             :                           {
    3708           0 :                             get_uleb128 (value, r, q);
    3709           0 :                             if (r > q)
    3710             :                               break;
    3711             :                           }
    3712           0 :                         if (tag == 32
    3713           0 :                             || ((tag & 1) != 0
    3714           0 :                                 && (gnu_vendor
    3715           0 :                                     || (! gnu_vendor && tag > 32)))
    3716           0 :                             || (! gnu_vendor && tag > 3 && tag < 6))
    3717             :                           {
    3718           0 :                             string = (const char *) r;
    3719           0 :                             r = memchr (r, '\0', q - r);
    3720           0 :                             if (r == NULL)
    3721             :                               break;
    3722           0 :                             ++r;
    3723             :                           }
    3724             : 
    3725           0 :                         const char *tag_name = NULL;
    3726           0 :                         const char *value_name = NULL;
    3727           0 :                         ebl_check_object_attribute (ebl, (const char *) name,
    3728             :                                                     tag, value,
    3729             :                                                     &tag_name, &value_name);
    3730             : 
    3731           0 :                         if (tag_name != NULL)
    3732             :                           {
    3733           0 :                             if (tag == 32)
    3734           0 :                               printf (gettext ("      %s: %" PRId64 ", %s\n"),
    3735             :                                       tag_name, value, string);
    3736           0 :                             else if (string == NULL && value_name == NULL)
    3737           0 :                               printf (gettext ("      %s: %" PRId64 "\n"),
    3738             :                                       tag_name, value);
    3739             :                             else
    3740           0 :                               printf (gettext ("      %s: %s\n"),
    3741             :                                       tag_name, string ?: value_name);
    3742             :                           }
    3743             :                         else
    3744             :                           {
    3745             :                             /* For "gnu" vendor 32 "compatibility" has
    3746             :                                already been handled above.  */
    3747           0 :                             assert (tag != 32
    3748             :                                     || strcmp ((const char *) name, "gnu"));
    3749           0 :                             if (string == NULL)
    3750           0 :                               printf (gettext ("      %u: %" PRId64 "\n"),
    3751             :                                       tag, value);
    3752             :                             else
    3753           0 :                               printf (gettext ("      %u: %s\n"),
    3754             :                                       tag, string);
    3755             :                           }
    3756             :                       }
    3757             :                   }
    3758             :               }
    3759             :         }
    3760             :     }
    3761             : }
    3762             : 
    3763             : 
    3764             : void
    3765     1029806 : print_dwarf_addr (Dwfl_Module *dwflmod,
    3766             :                   int address_size, Dwarf_Addr address, Dwarf_Addr raw)
    3767             : {
    3768             :   /* See if there is a name we can give for this address.  */
    3769     1029806 :   GElf_Sym sym;
    3770     1029806 :   GElf_Off off = 0;
    3771     1029778 :   const char *name = (print_address_names && ! print_unresolved_addresses)
    3772     1029696 :     ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
    3773     2059502 :     : NULL;
    3774             : 
    3775     1029806 :   const char *scn;
    3776     1029806 :   if (print_unresolved_addresses)
    3777             :     {
    3778          82 :       address = raw;
    3779          82 :       scn = NULL;
    3780             :     }
    3781             :   else
    3782             :     {
    3783             :       /* Relativize the address.  */
    3784     1029724 :       int n = dwfl_module_relocations (dwflmod);
    3785     1029724 :       int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
    3786             : 
    3787             :       /* In an ET_REL file there is a section name to refer to.  */
    3788     1028618 :       scn = (i < 0 ? NULL
    3789     1028618 :              : dwfl_module_relocation_info (dwflmod, i, NULL));
    3790             :     }
    3791             : 
    3792     1029806 :   if ((name != NULL
    3793     1012797 :        ? (off != 0
    3794             :           ? (scn != NULL
    3795             :              ? (address_size == 0
    3796      343238 :                 ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
    3797             :                           scn, address, name, off)
    3798     1879362 :                 : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
    3799      626454 :                           scn, 2 + address_size * 2, address,
    3800             :                           name, off))
    3801             :              : (address_size == 0
    3802         169 :                 ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
    3803             :                           address, name, off)
    3804        1506 :                 : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
    3805         502 :                           2 + address_size * 2, address,
    3806             :                           name, off)))
    3807             :           : (scn != NULL
    3808             :              ? (address_size == 0
    3809       14481 :                 ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
    3810       82878 :                 : printf ("%s+%#0*" PRIx64 " <%s>",
    3811       27626 :                            scn, 2 + address_size * 2, address, name))
    3812             :              : (address_size == 0
    3813          79 :                 ? printf ("%#" PRIx64 " <%s>", address, name)
    3814         744 :                 : printf ("%#0*" PRIx64 " <%s>",
    3815         248 :                           2 + address_size * 2, address, name))))
    3816             :        : (scn != NULL
    3817             :           ? (address_size == 0
    3818        7560 :              ? printf ("%s+%#" PRIx64, scn, address)
    3819        9259 :              : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
    3820             :           : (address_size == 0
    3821          34 :              ? printf ("%#" PRIx64, address)
    3822     2059456 :              : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
    3823           0 :     error (EXIT_FAILURE, 0, _("sprintf failure"));
    3824     1029806 : }
    3825             : 
    3826             : 
    3827             : static const char *
    3828     1109268 : dwarf_tag_string (unsigned int tag)
    3829             : {
    3830     1109268 :   switch (tag)
    3831             :     {
    3832             : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
    3833     1109268 :       DWARF_ALL_KNOWN_DW_TAG
    3834             : #undef DWARF_ONE_KNOWN_DW_TAG
    3835           0 :     default:
    3836           0 :       return NULL;
    3837             :     }
    3838             : }
    3839             : 
    3840             : 
    3841             : static const char *
    3842     4368384 : dwarf_attr_string (unsigned int attrnum)
    3843             : {
    3844     4368384 :   switch (attrnum)
    3845             :     {
    3846             : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
    3847     4368383 :       DWARF_ALL_KNOWN_DW_AT
    3848             : #undef DWARF_ONE_KNOWN_DW_AT
    3849           0 :     default:
    3850           0 :       return NULL;
    3851             :     }
    3852             : }
    3853             : 
    3854             : 
    3855             : static const char *
    3856     4368390 : dwarf_form_string (unsigned int form)
    3857             : {
    3858     4368390 :   switch (form)
    3859             :     {
    3860             : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
    3861     4368388 :       DWARF_ALL_KNOWN_DW_FORM
    3862             : #undef DWARF_ONE_KNOWN_DW_FORM
    3863           0 :     default:
    3864           0 :       return NULL;
    3865             :     }
    3866             : }
    3867             : 
    3868             : 
    3869             : static const char *
    3870        1735 : dwarf_lang_string (unsigned int lang)
    3871             : {
    3872        1735 :   switch (lang)
    3873             :     {
    3874             : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
    3875        1735 :       DWARF_ALL_KNOWN_DW_LANG
    3876             : #undef DWARF_ONE_KNOWN_DW_LANG
    3877           0 :     default:
    3878           0 :       return NULL;
    3879             :     }
    3880             : }
    3881             : 
    3882             : 
    3883             : static const char *
    3884             : dwarf_inline_string (unsigned int code)
    3885             : {
    3886        3458 :   static const char *const known[] =
    3887             :     {
    3888             : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
    3889             :       DWARF_ALL_KNOWN_DW_INL
    3890             : #undef DWARF_ONE_KNOWN_DW_INL
    3891             :     };
    3892             : 
    3893        3458 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    3894        3458 :     return known[code];
    3895             : 
    3896             :   return NULL;
    3897             : }
    3898             : 
    3899             : 
    3900             : static const char *
    3901             : dwarf_encoding_string (unsigned int code)
    3902             : {
    3903       30450 :   static const char *const known[] =
    3904             :     {
    3905             : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
    3906             :       DWARF_ALL_KNOWN_DW_ATE
    3907             : #undef DWARF_ONE_KNOWN_DW_ATE
    3908             :     };
    3909             : 
    3910       30450 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    3911       30450 :     return known[code];
    3912             : 
    3913             :   return NULL;
    3914             : }
    3915             : 
    3916             : 
    3917             : static const char *
    3918             : dwarf_access_string (unsigned int code)
    3919             : {
    3920           0 :   static const char *const known[] =
    3921             :     {
    3922             : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
    3923             :       DWARF_ALL_KNOWN_DW_ACCESS
    3924             : #undef DWARF_ONE_KNOWN_DW_ACCESS
    3925             :     };
    3926             : 
    3927           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    3928           0 :     return known[code];
    3929             : 
    3930             :   return NULL;
    3931             : }
    3932             : 
    3933             : 
    3934             : static const char *
    3935             : dwarf_defaulted_string (unsigned int code)
    3936             : {
    3937           0 :   static const char *const known[] =
    3938             :     {
    3939             : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
    3940             :       DWARF_ALL_KNOWN_DW_DEFAULTED
    3941             : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
    3942             :     };
    3943             : 
    3944           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    3945           0 :     return known[code];
    3946             : 
    3947             :   return NULL;
    3948             : }
    3949             : 
    3950             : 
    3951             : static const char *
    3952             : dwarf_visibility_string (unsigned int code)
    3953             : {
    3954           0 :   static const char *const known[] =
    3955             :     {
    3956             : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
    3957             :       DWARF_ALL_KNOWN_DW_VIS
    3958             : #undef DWARF_ONE_KNOWN_DW_VIS
    3959             :     };
    3960             : 
    3961           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    3962           0 :     return known[code];
    3963             : 
    3964             :   return NULL;
    3965             : }
    3966             : 
    3967             : 
    3968             : static const char *
    3969             : dwarf_virtuality_string (unsigned int code)
    3970             : {
    3971           0 :   static const char *const known[] =
    3972             :     {
    3973             : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
    3974             :       DWARF_ALL_KNOWN_DW_VIRTUALITY
    3975             : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
    3976             :     };
    3977             : 
    3978           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    3979           0 :     return known[code];
    3980             : 
    3981             :   return NULL;
    3982             : }
    3983             : 
    3984             : 
    3985             : static const char *
    3986             : dwarf_identifier_case_string (unsigned int code)
    3987             : {
    3988           0 :   static const char *const known[] =
    3989             :     {
    3990             : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
    3991             :       DWARF_ALL_KNOWN_DW_ID
    3992             : #undef DWARF_ONE_KNOWN_DW_ID
    3993             :     };
    3994             : 
    3995           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    3996           0 :     return known[code];
    3997             : 
    3998             :   return NULL;
    3999             : }
    4000             : 
    4001             : 
    4002             : static const char *
    4003             : dwarf_calling_convention_string (unsigned int code)
    4004             : {
    4005           0 :   static const char *const known[] =
    4006             :     {
    4007             : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
    4008             :       DWARF_ALL_KNOWN_DW_CC
    4009             : #undef DWARF_ONE_KNOWN_DW_CC
    4010             :     };
    4011             : 
    4012           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4013           0 :     return known[code];
    4014             : 
    4015             :   return NULL;
    4016             : }
    4017             : 
    4018             : 
    4019             : static const char *
    4020             : dwarf_ordering_string (unsigned int code)
    4021             : {
    4022           0 :   static const char *const known[] =
    4023             :     {
    4024             : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
    4025             :       DWARF_ALL_KNOWN_DW_ORD
    4026             : #undef DWARF_ONE_KNOWN_DW_ORD
    4027             :     };
    4028             : 
    4029           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4030           0 :     return known[code];
    4031             : 
    4032             :   return NULL;
    4033             : }
    4034             : 
    4035             : 
    4036             : static const char *
    4037             : dwarf_discr_list_string (unsigned int code)
    4038             : {
    4039           8 :   static const char *const known[] =
    4040             :     {
    4041             : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
    4042             :       DWARF_ALL_KNOWN_DW_DSC
    4043             : #undef DWARF_ONE_KNOWN_DW_DSC
    4044             :     };
    4045             : 
    4046           8 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4047           8 :     return known[code];
    4048             : 
    4049             :   return NULL;
    4050             : }
    4051             : 
    4052             : 
    4053             : static const char *
    4054             : dwarf_locexpr_opcode_string (unsigned int code)
    4055             : {
    4056      557473 :   static const char *const known[] =
    4057             :     {
    4058             :       /* Normally we can't affort building huge table of 64K entries,
    4059             :          most of them zero, just because there are a couple defined
    4060             :          values at the far end.  In case of opcodes, it's OK.  */
    4061             : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
    4062             :       DWARF_ALL_KNOWN_DW_OP
    4063             : #undef DWARF_ONE_KNOWN_DW_OP
    4064             :     };
    4065             : 
    4066      557473 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4067      557473 :     return known[code];
    4068             : 
    4069             :   return NULL;
    4070             : }
    4071             : 
    4072             : 
    4073             : static const char *
    4074             : dwarf_unit_string (unsigned int type)
    4075             : {
    4076           8 :   switch (type)
    4077             :     {
    4078             : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
    4079           7 :       DWARF_ALL_KNOWN_DW_UT
    4080             : #undef DWARF_ONE_KNOWN_DW_UT
    4081           0 :     default:
    4082           0 :       return NULL;
    4083             :     }
    4084             : }
    4085             : 
    4086             : 
    4087             : static const char *
    4088          14 : dwarf_range_list_encoding_string (unsigned int kind)
    4089             : {
    4090          14 :   switch (kind)
    4091             :     {
    4092             : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
    4093          12 :       DWARF_ALL_KNOWN_DW_RLE
    4094             : #undef DWARF_ONE_KNOWN_DW_RLE
    4095           0 :     default:
    4096           0 :       return NULL;
    4097             :     }
    4098             : }
    4099             : 
    4100             : 
    4101             : static const char *
    4102          89 : dwarf_loc_list_encoding_string (unsigned int kind)
    4103             : {
    4104          89 :   switch (kind)
    4105             :     {
    4106             : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
    4107          84 :       DWARF_ALL_KNOWN_DW_LLE
    4108             : #undef DWARF_ONE_KNOWN_DW_LLE
    4109           0 :     default:
    4110           0 :       return NULL;
    4111             :     }
    4112             : }
    4113             : 
    4114             : 
    4115             : static const char *
    4116             : dwarf_line_content_description_string (unsigned int kind)
    4117             : {
    4118           6 :   switch (kind)
    4119             :     {
    4120             : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
    4121           6 :       DWARF_ALL_KNOWN_DW_LNCT
    4122             : #undef DWARF_ONE_KNOWN_DW_LNCT
    4123           0 :     default:
    4124           0 :       return NULL;
    4125             :     }
    4126             : }
    4127             : 
    4128             : 
    4129             : /* Used by all dwarf_foo_name functions.  */
    4130             : static const char *
    4131     9881810 : string_or_unknown (const char *known, unsigned int code,
    4132             :                    unsigned int lo_user, unsigned int hi_user,
    4133             :                    bool print_unknown_num)
    4134             : {
    4135     9881810 :   static char unknown_buf[20];
    4136             : 
    4137     9881810 :   if (likely (known != NULL))
    4138             :     return known;
    4139             : 
    4140           0 :   if (lo_user != 0 && code >= lo_user && code <= hi_user)
    4141             :     {
    4142           0 :       snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
    4143             :                 code - lo_user);
    4144           0 :       return unknown_buf;
    4145             :     }
    4146             : 
    4147           0 :   if (print_unknown_num)
    4148             :     {
    4149           0 :       snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
    4150           0 :       return unknown_buf;
    4151             :     }
    4152             : 
    4153             :   return "???";
    4154             : }
    4155             : 
    4156             : 
    4157             : static const char *
    4158     1109268 : dwarf_tag_name (unsigned int tag)
    4159             : {
    4160     1109268 :   const char *ret = dwarf_tag_string (tag);
    4161     1109268 :   return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
    4162             : }
    4163             : 
    4164             : static const char *
    4165     4368384 : dwarf_attr_name (unsigned int attr)
    4166             : {
    4167     4368384 :   const char *ret = dwarf_attr_string (attr);
    4168     4368384 :   return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
    4169             : }
    4170             : 
    4171             : 
    4172             : static const char *
    4173     4368390 : dwarf_form_name (unsigned int form)
    4174             : {
    4175     4368390 :   const char *ret = dwarf_form_string (form);
    4176     4368390 :   return string_or_unknown (ret, form, 0, 0, true);
    4177             : }
    4178             : 
    4179             : 
    4180             : static const char *
    4181        1735 : dwarf_lang_name (unsigned int lang)
    4182             : {
    4183        1735 :   const char *ret = dwarf_lang_string (lang);
    4184        1735 :   return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
    4185             : }
    4186             : 
    4187             : 
    4188             : static const char *
    4189             : dwarf_inline_name (unsigned int code)
    4190             : {
    4191        6916 :   const char *ret = dwarf_inline_string (code);
    4192        3458 :   return string_or_unknown (ret, code, 0, 0, false);
    4193             : }
    4194             : 
    4195             : 
    4196             : static const char *
    4197             : dwarf_encoding_name (unsigned int code)
    4198             : {
    4199       60900 :   const char *ret = dwarf_encoding_string (code);
    4200       30450 :   return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
    4201             : }
    4202             : 
    4203             : 
    4204             : static const char *
    4205             : dwarf_access_name (unsigned int code)
    4206             : {
    4207           0 :   const char *ret = dwarf_access_string (code);
    4208           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4209             : }
    4210             : 
    4211             : 
    4212             : static const char *
    4213             : dwarf_defaulted_name (unsigned int code)
    4214             : {
    4215           0 :   const char *ret = dwarf_defaulted_string (code);
    4216           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4217             : }
    4218             : 
    4219             : 
    4220             : static const char *
    4221             : dwarf_visibility_name (unsigned int code)
    4222             : {
    4223           0 :   const char *ret = dwarf_visibility_string (code);
    4224           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4225             : }
    4226             : 
    4227             : 
    4228             : static const char *
    4229             : dwarf_virtuality_name (unsigned int code)
    4230             : {
    4231           0 :   const char *ret = dwarf_virtuality_string (code);
    4232           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4233             : }
    4234             : 
    4235             : 
    4236             : static const char *
    4237             : dwarf_identifier_case_name (unsigned int code)
    4238             : {
    4239           0 :   const char *ret = dwarf_identifier_case_string (code);
    4240           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4241             : }
    4242             : 
    4243             : 
    4244             : static const char *
    4245             : dwarf_calling_convention_name (unsigned int code)
    4246             : {
    4247           0 :   const char *ret = dwarf_calling_convention_string (code);
    4248           0 :   return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
    4249             : }
    4250             : 
    4251             : 
    4252             : static const char *
    4253             : dwarf_ordering_name (unsigned int code)
    4254             : {
    4255           0 :   const char *ret = dwarf_ordering_string (code);
    4256           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4257             : }
    4258             : 
    4259             : 
    4260             : static const char *
    4261             : dwarf_discr_list_name (unsigned int code)
    4262             : {
    4263          16 :   const char *ret = dwarf_discr_list_string (code);
    4264           8 :   return string_or_unknown (ret, code, 0, 0, false);
    4265             : }
    4266             : 
    4267             : 
    4268             : static const char *
    4269           8 : dwarf_unit_name (unsigned int type)
    4270             : {
    4271           8 :   const char *ret = dwarf_unit_string (type);
    4272           8 :   return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
    4273             : }
    4274             : 
    4275             : 
    4276             : static const char *
    4277          14 : dwarf_range_list_encoding_name (unsigned int kind)
    4278             : {
    4279          14 :   const char *ret = dwarf_range_list_encoding_string (kind);
    4280          14 :   return string_or_unknown (ret, kind, 0, 0, false);
    4281             : }
    4282             : 
    4283             : 
    4284             : static const char *
    4285          89 : dwarf_loc_list_encoding_name (unsigned int kind)
    4286             : {
    4287          89 :   const char *ret = dwarf_loc_list_encoding_string (kind);
    4288          89 :   return string_or_unknown (ret, kind, 0, 0, false);
    4289             : }
    4290             : 
    4291             : 
    4292             : static const char *
    4293           6 : dwarf_line_content_description_name (unsigned int kind)
    4294             : {
    4295           6 :   const char *ret = dwarf_line_content_description_string (kind);
    4296           6 :   return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
    4297             :                             false);
    4298             : }
    4299             : 
    4300             : 
    4301             : static void
    4302         328 : print_block (size_t n, const void *block)
    4303             : {
    4304         328 :   if (n == 0)
    4305           0 :     puts (_("empty block"));
    4306             :   else
    4307             :     {
    4308         328 :       printf (_("%zu byte block:"), n);
    4309         328 :       const unsigned char *data = block;
    4310        1528 :       do
    4311        1528 :         printf (" %02x", *data++);
    4312        1528 :       while (--n > 0);
    4313         328 :       putchar ('\n');
    4314             :     }
    4315         328 : }
    4316             : 
    4317             : static void
    4318           0 : print_bytes (size_t n, const unsigned char *bytes)
    4319             : {
    4320           0 :   while (n-- > 0)
    4321             :     {
    4322           0 :       printf ("%02x", *bytes++);
    4323           0 :       if (n > 0)
    4324           0 :         printf (" ");
    4325             :     }
    4326           0 : }
    4327             : 
    4328             : static int
    4329          68 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
    4330             : {
    4331          68 :   if (cu == NULL)
    4332             :     return -1;
    4333             : 
    4334          68 :   Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
    4335          68 :   if (debug_addr == NULL)
    4336             :     return -1;
    4337             : 
    4338          68 :   Dwarf_Off base = __libdw_cu_addr_base (cu);
    4339          68 :   Dwarf_Word off = idx * cu->address_size;
    4340          68 :   if (base > debug_addr->d_size
    4341          68 :       || off > debug_addr->d_size - base
    4342          68 :       || cu->address_size > debug_addr->d_size - base - off)
    4343             :     return -1;
    4344             : 
    4345          68 :   const unsigned char *addrp = debug_addr->d_buf + base + off;
    4346          68 :   if (cu->address_size == 4)
    4347           0 :     *addr = read_4ubyte_unaligned (cu->dbg, addrp);
    4348             :   else
    4349          68 :     *addr = read_8ubyte_unaligned (cu->dbg, addrp);
    4350             : 
    4351             :   return 0;
    4352             : }
    4353             : 
    4354             : static void
    4355      374056 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
    4356             :            unsigned int vers, unsigned int addrsize, unsigned int offset_size,
    4357             :            struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
    4358             : {
    4359      374056 :   const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
    4360             : 
    4361      374056 :   if (len == 0)
    4362             :     {
    4363           0 :       printf ("%*s(empty)\n", indent, "");
    4364           0 :       return;
    4365             :     }
    4366             : 
    4367             : #define NEED(n)         if (len < (Dwarf_Word) (n)) goto invalid
    4368             : #define CONSUME(n)      NEED (n); else len -= (n)
    4369             : 
    4370             :   Dwarf_Word offset = 0;
    4371      931529 :   while (len-- > 0)
    4372             :     {
    4373      557473 :       uint_fast8_t op = *data++;
    4374             : 
    4375      557473 :       const char *op_name = dwarf_locexpr_opcode_string (op);
    4376      557473 :       if (unlikely (op_name == NULL))
    4377             :         {
    4378           0 :           static char buf[20];
    4379           0 :           if (op >= DW_OP_lo_user)
    4380           0 :             snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
    4381             :           else
    4382           0 :             snprintf (buf, sizeof buf, "??? (%#x)", op);
    4383             :           op_name = buf;
    4384             :         }
    4385             : 
    4386      557473 :       switch (op)
    4387             :         {
    4388       10929 :         case DW_OP_addr:;
    4389             :           /* Address operand.  */
    4390       10929 :           Dwarf_Word addr;
    4391       10929 :           NEED (addrsize);
    4392       10929 :           if (addrsize == 4)
    4393          24 :             addr = read_4ubyte_unaligned (dbg, data);
    4394       10905 :           else if (addrsize == 8)
    4395       10905 :             addr = read_8ubyte_unaligned (dbg, data);
    4396             :           else
    4397           0 :             goto invalid;
    4398       10929 :           data += addrsize;
    4399       10929 :           CONSUME (addrsize);
    4400             : 
    4401       10929 :           printf ("%*s[%2" PRIuMAX "] %s ",
    4402             :                   indent, "", (uintmax_t) offset, op_name);
    4403       10929 :           print_dwarf_addr (dwflmod, 0, addr, addr);
    4404       10929 :           printf ("\n");
    4405             : 
    4406       10929 :           offset += 1 + addrsize;
    4407       10929 :           break;
    4408             : 
    4409           0 :         case DW_OP_call_ref:
    4410             :         case DW_OP_GNU_variable_value:
    4411             :           /* Offset operand.  */
    4412           0 :           if (ref_size != 4 && ref_size != 8)
    4413           0 :             goto invalid; /* Cannot be used in CFA.  */
    4414           0 :           NEED (ref_size);
    4415           0 :           if (ref_size == 4)
    4416           0 :             addr = read_4ubyte_unaligned (dbg, data);
    4417             :           else
    4418           0 :             addr = read_8ubyte_unaligned (dbg, data);
    4419           0 :           data += ref_size;
    4420           0 :           CONSUME (ref_size);
    4421             :           /* addr is a DIE offset, so format it as one.  */
    4422           0 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    4423             :                   indent, "", (uintmax_t) offset,
    4424             :                   op_name, (uintmax_t) addr);
    4425           0 :           offset += 1 + ref_size;
    4426           0 :           break;
    4427             : 
    4428       16457 :         case DW_OP_deref_size:
    4429             :         case DW_OP_xderef_size:
    4430             :         case DW_OP_pick:
    4431             :         case DW_OP_const1u:
    4432             :           // XXX value might be modified by relocation
    4433       16457 :           NEED (1);
    4434       49371 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
    4435             :                   indent, "", (uintmax_t) offset,
    4436       16457 :                   op_name, *((uint8_t *) data));
    4437       16457 :           ++data;
    4438       16457 :           --len;
    4439       16457 :           offset += 2;
    4440       16457 :           break;
    4441             : 
    4442        1670 :         case DW_OP_const2u:
    4443        1670 :           NEED (2);
    4444             :           // XXX value might be modified by relocation
    4445        1670 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
    4446             :                   indent, "", (uintmax_t) offset,
    4447        1670 :                   op_name, read_2ubyte_unaligned (dbg, data));
    4448        1670 :           CONSUME (2);
    4449        1670 :           data += 2;
    4450        1670 :           offset += 3;
    4451        1670 :           break;
    4452             : 
    4453        1095 :         case DW_OP_const4u:
    4454        1095 :           NEED (4);
    4455             :           // XXX value might be modified by relocation
    4456        1095 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
    4457             :                   indent, "", (uintmax_t) offset,
    4458        1095 :                   op_name, read_4ubyte_unaligned (dbg, data));
    4459        1095 :           CONSUME (4);
    4460        1095 :           data += 4;
    4461        1095 :           offset += 5;
    4462        1095 :           break;
    4463             : 
    4464          56 :         case DW_OP_const8u:
    4465          56 :           NEED (8);
    4466             :           // XXX value might be modified by relocation
    4467          56 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
    4468             :                   indent, "", (uintmax_t) offset,
    4469          56 :                   op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
    4470          56 :           CONSUME (8);
    4471          56 :           data += 8;
    4472          56 :           offset += 9;
    4473          56 :           break;
    4474             : 
    4475        2669 :         case DW_OP_const1s:
    4476        2669 :           NEED (1);
    4477             :           // XXX value might be modified by relocation
    4478        8007 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
    4479             :                   indent, "", (uintmax_t) offset,
    4480        2669 :                   op_name, *((int8_t *) data));
    4481        2669 :           ++data;
    4482        2669 :           --len;
    4483        2669 :           offset += 2;
    4484        2669 :           break;
    4485             : 
    4486           5 :         case DW_OP_const2s:
    4487           5 :           NEED (2);
    4488             :           // XXX value might be modified by relocation
    4489           5 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
    4490             :                   indent, "", (uintmax_t) offset,
    4491           5 :                   op_name, read_2sbyte_unaligned (dbg, data));
    4492           5 :           CONSUME (2);
    4493           5 :           data += 2;
    4494           5 :           offset += 3;
    4495           5 :           break;
    4496             : 
    4497           0 :         case DW_OP_const4s:
    4498           0 :           NEED (4);
    4499             :           // XXX value might be modified by relocation
    4500           0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
    4501             :                   indent, "", (uintmax_t) offset,
    4502           0 :                   op_name, read_4sbyte_unaligned (dbg, data));
    4503           0 :           CONSUME (4);
    4504           0 :           data += 4;
    4505           0 :           offset += 5;
    4506           0 :           break;
    4507             : 
    4508           0 :         case DW_OP_const8s:
    4509           0 :           NEED (8);
    4510             :           // XXX value might be modified by relocation
    4511           0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
    4512             :                   indent, "", (uintmax_t) offset,
    4513           0 :                   op_name, read_8sbyte_unaligned (dbg, data));
    4514           0 :           CONSUME (8);
    4515           0 :           data += 8;
    4516           0 :           offset += 9;
    4517           0 :           break;
    4518             : 
    4519       12199 :         case DW_OP_piece:
    4520             :         case DW_OP_regx:
    4521             :         case DW_OP_plus_uconst:
    4522       12199 :         case DW_OP_constu:;
    4523       12199 :           const unsigned char *start = data;
    4524       12199 :           uint64_t uleb;
    4525       12199 :           NEED (1);
    4526       12199 :           get_uleb128 (uleb, data, data + len);
    4527       12199 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
    4528             :                   indent, "", (uintmax_t) offset, op_name, uleb);
    4529       12199 :           CONSUME (data - start);
    4530       12199 :           offset += 1 + (data - start);
    4531       12199 :           break;
    4532             : 
    4533           2 :         case DW_OP_addrx:
    4534             :         case DW_OP_GNU_addr_index:
    4535             :         case DW_OP_constx:
    4536           2 :         case DW_OP_GNU_const_index:;
    4537           2 :           start = data;
    4538           2 :           NEED (1);
    4539           2 :           get_uleb128 (uleb, data, data + len);
    4540           2 :           printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
    4541             :                   indent, "", (uintmax_t) offset, op_name, uleb);
    4542           2 :           CONSUME (data - start);
    4543           2 :           offset += 1 + (data - start);
    4544           2 :           if (get_indexed_addr (cu, uleb, &addr) != 0)
    4545           0 :             printf ("???\n");
    4546             :           else
    4547             :             {
    4548           2 :               print_dwarf_addr (dwflmod, 0, addr, addr);
    4549           2 :               printf ("\n");
    4550             :             }
    4551             :           break;
    4552             : 
    4553           0 :         case DW_OP_bit_piece:
    4554           0 :           start = data;
    4555           0 :           uint64_t uleb2;
    4556           0 :           NEED (1);
    4557           0 :           get_uleb128 (uleb, data, data + len);
    4558           0 :           NEED (1);
    4559           0 :           get_uleb128 (uleb2, data, data + len);
    4560           0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
    4561             :                   indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
    4562           0 :           CONSUME (data - start);
    4563           0 :           offset += 1 + (data - start);
    4564           0 :           break;
    4565             : 
    4566       94692 :         case DW_OP_fbreg:
    4567             :         case DW_OP_breg0 ... DW_OP_breg31:
    4568             :         case DW_OP_consts:
    4569       94692 :           start = data;
    4570       94692 :           int64_t sleb;
    4571       94692 :           NEED (1);
    4572       94692 :           get_sleb128 (sleb, data, data + len);
    4573       94692 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
    4574             :                   indent, "", (uintmax_t) offset, op_name, sleb);
    4575       94692 :           CONSUME (data - start);
    4576       94692 :           offset += 1 + (data - start);
    4577       94692 :           break;
    4578             : 
    4579           0 :         case DW_OP_bregx:
    4580           0 :           start = data;
    4581           0 :           NEED (1);
    4582           0 :           get_uleb128 (uleb, data, data + len);
    4583           0 :           NEED (1);
    4584           0 :           get_sleb128 (sleb, data, data + len);
    4585           0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
    4586             :                   indent, "", (uintmax_t) offset, op_name, uleb, sleb);
    4587           0 :           CONSUME (data - start);
    4588           0 :           offset += 1 + (data - start);
    4589           0 :           break;
    4590             : 
    4591           0 :         case DW_OP_call2:
    4592           0 :           NEED (2);
    4593           0 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
    4594             :                   indent, "", (uintmax_t) offset, op_name,
    4595           0 :                   read_2ubyte_unaligned (dbg, data));
    4596           0 :           CONSUME (2);
    4597           0 :           data += 2;
    4598           0 :           offset += 3;
    4599           0 :           break;
    4600             : 
    4601           4 :         case DW_OP_call4:
    4602           4 :           NEED (4);
    4603           4 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
    4604             :                   indent, "", (uintmax_t) offset, op_name,
    4605           4 :                   read_4ubyte_unaligned (dbg, data));
    4606           4 :           CONSUME (4);
    4607           4 :           data += 4;
    4608           4 :           offset += 5;
    4609           4 :           break;
    4610             : 
    4611         122 :         case DW_OP_skip:
    4612             :         case DW_OP_bra:
    4613         122 :           NEED (2);
    4614         366 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
    4615             :                   indent, "", (uintmax_t) offset, op_name,
    4616         122 :                   (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
    4617         122 :           CONSUME (2);
    4618         122 :           data += 2;
    4619         122 :           offset += 3;
    4620         122 :           break;
    4621             : 
    4622         318 :         case DW_OP_implicit_value:
    4623         318 :           start = data;
    4624         318 :           NEED (1);
    4625         318 :           get_uleb128 (uleb, data, data + len);
    4626         318 :           printf ("%*s[%2" PRIuMAX "] %s: ",
    4627             :                   indent, "", (uintmax_t) offset, op_name);
    4628         318 :           NEED (uleb);
    4629         318 :           print_block (uleb, data);
    4630         318 :           data += uleb;
    4631         318 :           CONSUME (data - start);
    4632         318 :           offset += 1 + (data - start);
    4633         318 :           break;
    4634             : 
    4635        4571 :         case DW_OP_implicit_pointer:
    4636             :         case DW_OP_GNU_implicit_pointer:
    4637             :           /* DIE offset operand.  */
    4638        4571 :           start = data;
    4639        4571 :           NEED (ref_size);
    4640        4571 :           if (ref_size != 4 && ref_size != 8)
    4641           0 :             goto invalid; /* Cannot be used in CFA.  */
    4642        4571 :           if (ref_size == 4)
    4643        4571 :             addr = read_4ubyte_unaligned (dbg, data);
    4644             :           else
    4645           0 :             addr = read_8ubyte_unaligned (dbg, data);
    4646        4571 :           data += ref_size;
    4647             :           /* Byte offset operand.  */
    4648        4571 :           NEED (1);
    4649        4571 :           get_sleb128 (sleb, data, data + len);
    4650             : 
    4651        4571 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
    4652             :                   indent, "", (intmax_t) offset,
    4653             :                   op_name, (uintmax_t) addr, sleb);
    4654        4571 :           CONSUME (data - start);
    4655        4571 :           offset += 1 + (data - start);
    4656        4571 :           break;
    4657             : 
    4658       31095 :         case DW_OP_entry_value:
    4659             :         case DW_OP_GNU_entry_value:
    4660             :           /* Size plus expression block.  */
    4661       31095 :           start = data;
    4662       31095 :           NEED (1);
    4663       31095 :           get_uleb128 (uleb, data, data + len);
    4664       31095 :           printf ("%*s[%2" PRIuMAX "] %s:\n",
    4665             :                   indent, "", (uintmax_t) offset, op_name);
    4666       31095 :           NEED (uleb);
    4667       31095 :           print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
    4668             :                      addrsize, offset_size, cu, uleb, data);
    4669       31095 :           data += uleb;
    4670       31095 :           CONSUME (data - start);
    4671       31095 :           offset += 1 + (data - start);
    4672       31095 :           break;
    4673             : 
    4674           0 :         case DW_OP_const_type:
    4675             :         case DW_OP_GNU_const_type:
    4676             :           /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
    4677             :              unsigned size plus block.  */
    4678           0 :           start = data;
    4679           0 :           NEED (1);
    4680           0 :           get_uleb128 (uleb, data, data + len);
    4681           0 :           if (! print_unresolved_addresses && cu != NULL)
    4682           0 :             uleb += cu->start;
    4683           0 :           NEED (1);
    4684           0 :           uint8_t usize = *(uint8_t *) data++;
    4685           0 :           NEED (usize);
    4686           0 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
    4687             :                   indent, "", (uintmax_t) offset, op_name, uleb);
    4688           0 :           print_block (usize, data);
    4689           0 :           data += usize;
    4690           0 :           CONSUME (data - start);
    4691           0 :           offset += 1 + (data - start);
    4692           0 :           break;
    4693             : 
    4694           0 :         case DW_OP_regval_type:
    4695             :         case DW_OP_GNU_regval_type:
    4696             :           /* uleb128 register number, uleb128 CU relative
    4697             :              DW_TAG_base_type DIE offset.  */
    4698           0 :           start = data;
    4699           0 :           NEED (1);
    4700           0 :           get_uleb128 (uleb, data, data + len);
    4701           0 :           NEED (1);
    4702           0 :           get_uleb128 (uleb2, data, data + len);
    4703           0 :           if (! print_unresolved_addresses && cu != NULL)
    4704           0 :             uleb2 += cu->start;
    4705           0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
    4706             :                   indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
    4707           0 :           CONSUME (data - start);
    4708           0 :           offset += 1 + (data - start);
    4709           0 :           break;
    4710             : 
    4711           0 :         case DW_OP_deref_type:
    4712             :         case DW_OP_GNU_deref_type:
    4713             :           /* 1-byte unsigned size of value, uleb128 CU relative
    4714             :              DW_TAG_base_type DIE offset.  */
    4715           0 :           start = data;
    4716           0 :           NEED (1);
    4717           0 :           usize = *(uint8_t *) data++;
    4718           0 :           NEED (1);
    4719           0 :           get_uleb128 (uleb, data, data + len);
    4720           0 :           if (! print_unresolved_addresses && cu != NULL)
    4721           0 :             uleb += cu->start;
    4722           0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
    4723             :                   indent, "", (uintmax_t) offset,
    4724             :                   op_name, usize, uleb);
    4725           0 :           CONSUME (data - start);
    4726           0 :           offset += 1 + (data - start);
    4727           0 :           break;
    4728             : 
    4729           0 :         case DW_OP_xderef_type:
    4730             :           /* 1-byte unsigned size of value, uleb128 base_type DIE offset.  */
    4731           0 :           start = data;
    4732           0 :           NEED (1);
    4733           0 :           usize = *(uint8_t *) data++;
    4734           0 :           NEED (1);
    4735           0 :           get_uleb128 (uleb, data, data + len);
    4736           0 :           printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
    4737             :                   indent, "", (uintmax_t) offset,
    4738             :                   op_name, usize, uleb);
    4739           0 :           CONSUME (data - start);
    4740           0 :           offset += 1 + (data - start);
    4741           0 :           break;
    4742             : 
    4743         342 :         case DW_OP_convert:
    4744             :         case DW_OP_GNU_convert:
    4745             :         case DW_OP_reinterpret:
    4746             :         case DW_OP_GNU_reinterpret:
    4747             :           /* uleb128 CU relative offset to DW_TAG_base_type, or zero
    4748             :              for conversion to untyped.  */
    4749         342 :           start = data;
    4750         342 :           NEED (1);
    4751         342 :           get_uleb128 (uleb, data, data + len);
    4752         342 :           if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
    4753         228 :             uleb += cu->start;
    4754         342 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    4755             :                   indent, "", (uintmax_t) offset, op_name, uleb);
    4756         342 :           CONSUME (data - start);
    4757         342 :           offset += 1 + (data - start);
    4758         342 :           break;
    4759             : 
    4760           3 :         case DW_OP_GNU_parameter_ref:
    4761             :           /* 4 byte CU relative reference to the abstract optimized away
    4762             :              DW_TAG_formal_parameter.  */
    4763           3 :           NEED (4);
    4764           3 :           uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
    4765           3 :           if (! print_unresolved_addresses && cu != NULL)
    4766           3 :             param_off += cu->start;
    4767           3 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    4768             :                   indent, "", (uintmax_t) offset, op_name, param_off);
    4769           3 :           CONSUME (4);
    4770           3 :           data += 4;
    4771           3 :           offset += 5;
    4772           3 :           break;
    4773             : 
    4774             :         default:
    4775             :           /* No Operand.  */
    4776      381244 :           printf ("%*s[%2" PRIuMAX "] %s\n",
    4777             :                   indent, "", (uintmax_t) offset, op_name);
    4778      381244 :           ++offset;
    4779      381244 :           break;
    4780             :         }
    4781             : 
    4782      557473 :       indent = indentrest;
    4783      557473 :       continue;
    4784             : 
    4785           0 :     invalid:
    4786           0 :       printf (gettext ("%*s[%2" PRIuMAX "] %s  <TRUNCATED>\n"),
    4787             :               indent, "", (uintmax_t) offset, op_name);
    4788             :       break;
    4789             :     }
    4790             : }
    4791             : 
    4792             : 
    4793             : struct listptr
    4794             : {
    4795             :   Dwarf_Off offset:(64 - 3);
    4796             :   bool addr64:1;
    4797             :   bool dwarf64:1;
    4798             :   bool warned:1;
    4799             :   struct Dwarf_CU *cu;
    4800             :   unsigned int attr;
    4801             : };
    4802             : 
    4803             : #define listptr_offset_size(p)  ((p)->dwarf64 ? 8 : 4)
    4804             : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
    4805             : 
    4806             : static Dwarf_Addr
    4807      125312 : cudie_base (Dwarf_Die *cudie)
    4808             : {
    4809      125312 :   Dwarf_Addr base;
    4810             :   /* Find the base address of the compilation unit.  It will normally
    4811             :      be specified by DW_AT_low_pc.  In DWARF-3 draft 4, the base
    4812             :      address could be overridden by DW_AT_entry_pc.  It's been
    4813             :      removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
    4814             :      compilation units with discontinuous ranges.  */
    4815      125312 :   if (unlikely (dwarf_lowpc (cudie, &base) != 0))
    4816             :     {
    4817           0 :       Dwarf_Attribute attr_mem;
    4818           0 :       if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
    4819             :                           &base) != 0)
    4820           0 :         base = 0;
    4821             :     }
    4822      125312 :   return base;
    4823             : }
    4824             : 
    4825             : static Dwarf_Addr
    4826           0 : listptr_base (struct listptr *p)
    4827             : {
    4828           0 :   Dwarf_Die cu = CUDIE (p->cu);
    4829           0 :   return cudie_base (&cu);
    4830             : }
    4831             : 
    4832             : static int
    4833      846120 : compare_listptr (const void *a, const void *b, void *arg)
    4834             : {
    4835      846120 :   const char *name = arg;
    4836      846120 :   struct listptr *p1 = (void *) a;
    4837      846120 :   struct listptr *p2 = (void *) b;
    4838             : 
    4839      846120 :   if (p1->offset < p2->offset)
    4840             :     return -1;
    4841       79373 :   if (p1->offset > p2->offset)
    4842             :     return 1;
    4843             : 
    4844        3221 :   if (!p1->warned && !p2->warned)
    4845             :     {
    4846        3221 :       if (p1->addr64 != p2->addr64)
    4847             :         {
    4848           0 :           p1->warned = p2->warned = true;
    4849           0 :           error (0, 0,
    4850           0 :                  gettext ("%s %#" PRIx64 " used with different address sizes"),
    4851             :                  name, (uint64_t) p1->offset);
    4852             :         }
    4853        3221 :       if (p1->dwarf64 != p2->dwarf64)
    4854             :         {
    4855           0 :           p1->warned = p2->warned = true;
    4856           0 :           error (0, 0,
    4857           0 :                  gettext ("%s %#" PRIx64 " used with different offset sizes"),
    4858           0 :                  name, (uint64_t) p1->offset);
    4859             :         }
    4860        3221 :       if (listptr_base (p1) != listptr_base (p2))
    4861             :         {
    4862           0 :           p1->warned = p2->warned = true;
    4863           0 :           error (0, 0,
    4864           0 :                  gettext ("%s %#" PRIx64 " used with different base addresses"),
    4865           0 :                  name, (uint64_t) p1->offset);
    4866             :         }
    4867        3221 :       if (p1->attr != p2 ->attr)
    4868             :         {
    4869           0 :           p1->warned = p2->warned = true;
    4870           0 :           error (0, 0,
    4871           0 :                  gettext ("%s %#" PRIx64
    4872             :                           " used with different attribute %s and %s"),
    4873           0 :                  name, (uint64_t) p1->offset, dwarf_attr_name (p2->attr),
    4874             :                  dwarf_attr_name (p2->attr));
    4875             :         }
    4876             :     }
    4877             : 
    4878             :   return 0;
    4879             : }
    4880             : 
    4881             : struct listptr_table
    4882             : {
    4883             :   size_t n;
    4884             :   size_t alloc;
    4885             :   struct listptr *table;
    4886             : };
    4887             : 
    4888             : static struct listptr_table known_locsptr;
    4889             : static struct listptr_table known_loclistsptr;
    4890             : static struct listptr_table known_rangelistptr;
    4891             : static struct listptr_table known_rnglistptr;
    4892             : static struct listptr_table known_addrbases;
    4893             : static struct listptr_table known_stroffbases;
    4894             : 
    4895             : static void
    4896             : reset_listptr (struct listptr_table *table)
    4897             : {
    4898        1116 :   free (table->table);
    4899        1116 :   table->table = NULL;
    4900        1116 :   table->n = table->alloc = 0;
    4901             : }
    4902             : 
    4903             : /* Returns false if offset doesn't fit.  See struct listptr.  */
    4904             : static bool
    4905      122270 : notice_listptr (enum section_e section, struct listptr_table *table,
    4906             :                 uint_fast8_t address_size, uint_fast8_t offset_size,
    4907             :                 struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
    4908             : {
    4909      122270 :   if (print_debug_sections & section)
    4910             :     {
    4911      122169 :       if (table->n == table->alloc)
    4912             :         {
    4913         189 :           if (table->alloc == 0)
    4914          79 :             table->alloc = 128;
    4915             :           else
    4916         110 :             table->alloc *= 2;
    4917         189 :           table->table = xrealloc (table->table,
    4918         189 :                                    table->alloc * sizeof table->table[0]);
    4919             :         }
    4920             : 
    4921      122169 :       struct listptr *p = &table->table[table->n++];
    4922             : 
    4923      244338 :       *p = (struct listptr)
    4924             :         {
    4925      122169 :           .addr64 = address_size == 8,
    4926      122169 :           .dwarf64 = offset_size == 8,
    4927             :           .offset = offset,
    4928             :           .cu = cu,
    4929             :           .attr = attr
    4930             :         };
    4931             : 
    4932      122169 :       if (p->offset != offset)
    4933             :         {
    4934           0 :           table->n--;
    4935           0 :           return false;
    4936             :         }
    4937             :     }
    4938             :   return true;
    4939             : }
    4940             : 
    4941             : static void
    4942           0 : sort_listptr (struct listptr_table *table, const char *name)
    4943             : {
    4944           0 :   if (table->n > 0)
    4945          79 :     qsort_r (table->table, table->n, sizeof table->table[0],
    4946             :              &compare_listptr, (void *) name);
    4947           0 : }
    4948             : 
    4949             : static bool
    4950           0 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
    4951             :                    uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
    4952             :                    Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
    4953             :                    unsigned char **readp, unsigned char *endp,
    4954             :                    unsigned int *attr)
    4955             : {
    4956           0 :   if (table->n == 0)
    4957           0 :     return false;
    4958             : 
    4959           0 :   while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
    4960           0 :     ++*idxp;
    4961             : 
    4962           0 :   struct listptr *p = &table->table[*idxp];
    4963             : 
    4964           0 :   if (*idxp == table->n
    4965           0 :       || p->offset >= (Dwarf_Off) (endp - *readp + offset))
    4966             :     {
    4967           0 :       *readp = endp;
    4968           0 :       printf (gettext (" [%6tx]  <UNUSED GARBAGE IN REST OF SECTION>\n"),
    4969             :               offset);
    4970           0 :       return true;
    4971             :     }
    4972             : 
    4973           0 :   if (p->offset != (Dwarf_Off) offset)
    4974             :     {
    4975           0 :       *readp += p->offset - offset;
    4976           0 :       printf (gettext (" [%6tx]  <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
    4977             :               offset, (Dwarf_Off) p->offset - offset);
    4978           0 :       return true;
    4979             :     }
    4980             : 
    4981           0 :   if (address_sizep != NULL)
    4982           0 :     *address_sizep = listptr_address_size (p);
    4983           0 :   if (offset_sizep != NULL)
    4984           0 :     *offset_sizep = listptr_offset_size (p);
    4985           0 :   if (base != NULL)
    4986           0 :     *base = listptr_base (p);
    4987           0 :   if (cu != NULL)
    4988           0 :     *cu = p->cu;
    4989           0 :   if (attr != NULL)
    4990           0 :     *attr = p->attr;
    4991             : 
    4992             :   return false;
    4993             : }
    4994             : 
    4995             : static Dwarf_Off
    4996           0 : next_listptr_offset (struct listptr_table *table, size_t idx)
    4997             : {
    4998             :   /* Note that multiple attributes could in theory point to the same loclist
    4999             :      offset, so make sure we pick one that is bigger than the current one.
    5000             :      The table is sorted on offset.  */
    5001       52680 :   Dwarf_Off offset = table->table[idx].offset;
    5002       53358 :   while (++idx < table->n)
    5003             :     {
    5004       53358 :       Dwarf_Off next = table->table[idx].offset;
    5005       53358 :       if (next > offset)
    5006           0 :         return next;
    5007             :     }
    5008             :   return 0;
    5009             : }
    5010             : 
    5011             : /* Returns the listptr associated with the given index, or NULL.  */
    5012             : static struct listptr *
    5013           0 : get_listptr (struct listptr_table *table, size_t idx)
    5014             : {
    5015           0 :   if (idx >= table->n)
    5016           0 :     return NULL;
    5017           0 :   return &table->table[idx];
    5018             : }
    5019             : 
    5020             : /* Returns the next index, base address and CU associated with the
    5021             :    list unit offsets.  If there is none false is returned, otherwise
    5022             :    true.  Assumes the table has been sorted.  */
    5023             : static bool
    5024           0 : listptr_cu (struct listptr_table *table, size_t *idxp,
    5025             :             Dwarf_Off start, Dwarf_Off end,
    5026             :             Dwarf_Addr *base, struct Dwarf_CU **cu)
    5027             : {
    5028           0 :   while (*idxp < table->n
    5029           0 :          && table->table[*idxp].offset < start)
    5030           0 :     ++*idxp;
    5031             : 
    5032           0 :   if (*idxp < table->n
    5033           0 :       && table->table[*idxp].offset >= start
    5034           0 :       && table->table[*idxp].offset < end)
    5035             :     {
    5036           0 :       struct listptr *p = &table->table[*idxp];
    5037           0 :       *base = listptr_base (p);
    5038           0 :       *cu = p->cu;
    5039           0 :       ++*idxp;
    5040           0 :       return true;
    5041             :     }
    5042             : 
    5043             :   return false;
    5044             : }
    5045             : 
    5046             : static void
    5047          40 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    5048             :                             Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
    5049             :                             Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    5050             : {
    5051          80 :   const size_t sh_size = (dbg->sectiondata[IDX_debug_abbrev] ?
    5052          40 :                           dbg->sectiondata[IDX_debug_abbrev]->d_size : 0);
    5053             : 
    5054          40 :   printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
    5055             :                    " [ Code]\n"),
    5056             :           elf_ndxscn (scn), section_name (ebl, shdr),
    5057          40 :           (uint64_t) shdr->sh_offset);
    5058             : 
    5059          40 :   Dwarf_Off offset = 0;
    5060        1708 :   while (offset < sh_size)
    5061             :     {
    5062        1668 :       printf (gettext ("\nAbbreviation section at offset %" PRIu64 ":\n"),
    5063             :               offset);
    5064             : 
    5065       83182 :       while (1)
    5066       81514 :         {
    5067       83182 :           size_t length;
    5068       83182 :           Dwarf_Abbrev abbrev;
    5069             : 
    5070       83182 :           int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
    5071       83182 :           if (res != 0)
    5072             :             {
    5073        1668 :               if (unlikely (res < 0))
    5074             :                 {
    5075           0 :                   printf (gettext ("\
    5076             :  *** error while reading abbreviation: %s\n"),
    5077             :                           dwarf_errmsg (-1));
    5078           0 :                   return;
    5079             :                 }
    5080             : 
    5081             :               /* This is the NUL byte at the end of the section.  */
    5082        1668 :               ++offset;
    5083        1668 :               break;
    5084             :             }
    5085             : 
    5086             :           /* We know these calls can never fail.  */
    5087       81514 :           unsigned int code = dwarf_getabbrevcode (&abbrev);
    5088       81514 :           unsigned int tag = dwarf_getabbrevtag (&abbrev);
    5089       81514 :           int has_children = dwarf_abbrevhaschildren (&abbrev);
    5090             : 
    5091       81514 :           printf (gettext (" [%5u] offset: %" PRId64
    5092             :                            ", children: %s, tag: %s\n"),
    5093             :                   code, (int64_t) offset,
    5094             :                   has_children ? yes_str : no_str,
    5095             :                   dwarf_tag_name (tag));
    5096             : 
    5097       81514 :           size_t cnt = 0;
    5098       81514 :           unsigned int name;
    5099       81514 :           unsigned int form;
    5100       81514 :           Dwarf_Sword data;
    5101       81514 :           Dwarf_Off enoffset;
    5102      450934 :           while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
    5103             :                                            &data, &enoffset) == 0)
    5104             :             {
    5105      369420 :               printf ("          attr: %s, form: %s",
    5106             :                       dwarf_attr_name (name), dwarf_form_name (form));
    5107      369420 :               if (form == DW_FORM_implicit_const)
    5108           0 :                 printf (" (%" PRId64 ")", data);
    5109      369420 :               printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
    5110      369420 :               ++cnt;
    5111             :             }
    5112             : 
    5113       81514 :           offset += length;
    5114             :         }
    5115             :     }
    5116             : }
    5117             : 
    5118             : 
    5119             : static void
    5120           2 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    5121             :                           Ebl *ebl, GElf_Ehdr *ehdr,
    5122             :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    5123             : {
    5124           2 :   printf (gettext ("\
    5125             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    5126             :           elf_ndxscn (scn), section_name (ebl, shdr),
    5127           2 :           (uint64_t) shdr->sh_offset);
    5128             : 
    5129           2 :   if (shdr->sh_size == 0)
    5130             :     return;
    5131             : 
    5132             :   /* We like to get the section from libdw to make sure they are relocated.  */
    5133           4 :   Elf_Data *data = (dbg->sectiondata[IDX_debug_addr]
    5134           2 :                     ?: elf_rawdata (scn, NULL));
    5135           2 :   if (unlikely (data == NULL))
    5136             :     {
    5137           0 :       error (0, 0, gettext ("cannot get .debug_addr section data: %s"),
    5138             :              elf_errmsg (-1));
    5139           0 :       return;
    5140             :     }
    5141             : 
    5142           2 :   size_t idx = 0;
    5143           2 :   sort_listptr (&known_addrbases, "addr_base");
    5144             : 
    5145           2 :   const unsigned char *start = (const unsigned char *) data->d_buf;
    5146           2 :   const unsigned char *readp = start;
    5147           4 :   const unsigned char *readendp = ((const unsigned char *) data->d_buf
    5148           2 :                                    + data->d_size);
    5149             : 
    5150           6 :   while (readp < readendp)
    5151             :     {
    5152             :       /* We cannot really know whether or not there is an header.  The
    5153             :          DebugFission extension to DWARF4 doesn't add one.  The DWARF5
    5154             :          .debug_addr variant does.  Whether or not we have an header,
    5155             :          DW_AT_[GNU_]addr_base points at "index 0".  So if the current
    5156             :          offset equals the CU addr_base then we can just start
    5157             :          printing addresses.  If there is no CU with an exact match
    5158             :          then we'll try to parse the header first.  */
    5159           8 :       Dwarf_Off off = (Dwarf_Off) (readp
    5160           4 :                                    - (const unsigned char *) data->d_buf);
    5161             : 
    5162           4 :       printf ("Table at offset %" PRIx64 " ", off);
    5163             : 
    5164           4 :       struct listptr *listptr = get_listptr (&known_addrbases, idx++);
    5165           4 :       const unsigned char *next_unitp;
    5166             : 
    5167           4 :       uint64_t unit_length;
    5168           4 :       uint16_t version;
    5169           4 :       uint8_t address_size;
    5170           4 :       uint8_t segment_size;
    5171           4 :       if (listptr == NULL)
    5172             :         {
    5173           0 :           error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
    5174             :                  off);
    5175             : 
    5176             :           /* We will have to assume it is just addresses to the end... */
    5177           0 :           address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    5178           0 :           next_unitp = readendp;
    5179           0 :           printf ("Unknown CU:\n");
    5180             :         }
    5181             :       else
    5182             :         {
    5183           4 :           Dwarf_Die cudie;
    5184           4 :           if (dwarf_cu_die (listptr->cu, &cudie,
    5185             :                             NULL, NULL, NULL, NULL,
    5186             :                             NULL, NULL) == NULL)
    5187           0 :             printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
    5188             :           else
    5189           4 :             printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
    5190             : 
    5191           4 :           if (listptr->offset == off)
    5192             :             {
    5193           2 :               address_size = listptr_address_size (listptr);
    5194           2 :               segment_size = 0;
    5195           2 :               version = 4;
    5196             : 
    5197             :               /* The addresses start here, but where do they end?  */
    5198           2 :               listptr = get_listptr (&known_addrbases, idx);
    5199           1 :               if (listptr == NULL)
    5200             :                 next_unitp = readendp;
    5201           1 :               else if (listptr->cu->version < 5)
    5202             :                 {
    5203           1 :                   next_unitp = start + listptr->offset;
    5204           1 :                   if (listptr->offset < off || listptr->offset > data->d_size)
    5205             :                     {
    5206           0 :                       error (0, 0,
    5207             :                              "Warning: Bad address base for next unit at %"
    5208             :                              PRIx64, off);
    5209           0 :                       next_unitp = readendp;
    5210             :                     }
    5211             :                 }
    5212             :               else
    5213             :                 {
    5214             :                   /* Tricky, we don't have a header for this unit, but
    5215             :                      there is one for the next.  We will have to
    5216             :                      "guess" how big it is and subtract it from the
    5217             :                      offset (because that points after the header).  */
    5218           0 :                   unsigned int offset_size = listptr_offset_size (listptr);
    5219           0 :                   Dwarf_Off next_off = (listptr->offset
    5220           0 :                                         - (offset_size == 4 ? 4 : 12) /* len */
    5221             :                                         - 2 /* version */
    5222             :                                         - 1 /* address size */
    5223           0 :                                         - 1); /* segment selector size */
    5224           0 :                   next_unitp = start + next_off;
    5225           0 :                   if (next_off < off || next_off > data->d_size)
    5226             :                     {
    5227           0 :                       error (0, 0,
    5228             :                              "Warning: Couldn't calculate .debug_addr "
    5229             :                              " unit lenght at %" PRIx64, off);
    5230           0 :                       next_unitp = readendp;
    5231             :                     }
    5232             :                 }
    5233           2 :               unit_length = (uint64_t) (next_unitp - readp);
    5234             : 
    5235             :               /* Pretend we have a header.  */
    5236           2 :               printf ("\n");
    5237           2 :               printf (gettext (" Length:         %8" PRIu64 "\n"),
    5238             :                       unit_length);
    5239           2 :               printf (gettext (" DWARF version:  %8" PRIu16 "\n"), version);
    5240           2 :               printf (gettext (" Address size:   %8" PRIu64 "\n"),
    5241             :                       (uint64_t) address_size);
    5242           2 :               printf (gettext (" Segment size:   %8" PRIu64 "\n"),
    5243             :                       (uint64_t) segment_size);
    5244           2 :               printf ("\n");
    5245             :             }
    5246             :           else
    5247             :             {
    5248             :               /* OK, we have to parse an header first.  */
    5249           2 :               unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    5250           2 :               if (unlikely (unit_length == 0xffffffff))
    5251             :                 {
    5252           0 :                   if (unlikely (readp > readendp - 8))
    5253             :                     {
    5254           0 :                     invalid_data:
    5255           0 :                       error (0, 0, "Invalid data");
    5256           0 :                       return;
    5257             :                     }
    5258           0 :                   unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    5259             :                 }
    5260           2 :               printf ("\n");
    5261           2 :               printf (gettext (" Length:         %8" PRIu64 "\n"),
    5262             :                       unit_length);
    5263             : 
    5264             :               /* We need at least 2-bytes (version) + 1-byte
    5265             :                  (addr_size) + 1-byte (segment_size) = 4 bytes to
    5266             :                  complete the header.  And this unit cannot go beyond
    5267             :                  the section data.  */
    5268           2 :               if (readp > readendp - 4
    5269           2 :                   || unit_length < 4
    5270           2 :                   || unit_length > (uint64_t) (readendp - readp))
    5271             :                 goto invalid_data;
    5272             : 
    5273           2 :               next_unitp = readp + unit_length;
    5274             : 
    5275           2 :               version = read_2ubyte_unaligned_inc (dbg, readp);
    5276           2 :               printf (gettext (" DWARF version:  %8" PRIu16 "\n"), version);
    5277             : 
    5278           2 :               if (version != 5)
    5279             :                 {
    5280           0 :                   error (0, 0, gettext ("Unknown version"));
    5281           0 :                   goto next_unit;
    5282             :                 }
    5283             : 
    5284           2 :               address_size = *readp++;
    5285           2 :               printf (gettext (" Address size:   %8" PRIu64 "\n"),
    5286             :                       (uint64_t) address_size);
    5287             : 
    5288           2 :               if (address_size != 4 && address_size != 8)
    5289             :                 {
    5290           0 :                   error (0, 0, gettext ("unsupported address size"));
    5291           0 :                   goto next_unit;
    5292             :                 }
    5293             : 
    5294           2 :               segment_size = *readp++;
    5295           2 :               printf (gettext (" Segment size:   %8" PRIu64 "\n"),
    5296             :                       (uint64_t) segment_size);
    5297           2 :               printf ("\n");
    5298             : 
    5299           2 :               if (segment_size != 0)
    5300             :                 {
    5301           0 :                   error (0, 0, gettext ("unsupported segment size"));
    5302           0 :                   goto next_unit;
    5303             :                 }
    5304             : 
    5305           2 :               if (listptr->offset != (Dwarf_Off) (readp - start))
    5306             :                 {
    5307           0 :                   error (0, 0, "Address index doesn't start after header");
    5308           0 :                   goto next_unit;
    5309             :                 }
    5310             :             }
    5311             :         }
    5312             : 
    5313           4 :       int digits = 1;
    5314           4 :       size_t addresses = (next_unitp - readp) / address_size;
    5315           8 :       while (addresses >= 10)
    5316             :         {
    5317           4 :           ++digits;
    5318           4 :           addresses /= 10;
    5319             :         }
    5320             : 
    5321           4 :       unsigned int uidx = 0;
    5322           4 :       size_t index_offset =  readp - (const unsigned char *) data->d_buf;
    5323           4 :       printf (" Addresses start at offset 0x%zx:\n", index_offset);
    5324          76 :       while (readp <= next_unitp - address_size)
    5325             :         {
    5326          72 :           Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
    5327             :                                                      readp);
    5328          72 :           printf (" [%*u] ", digits, uidx++);
    5329          72 :           print_dwarf_addr (dwflmod, address_size, addr, addr);
    5330          72 :           printf ("\n");
    5331             :         }
    5332           4 :       printf ("\n");
    5333             : 
    5334           4 :       if (readp != next_unitp)
    5335           0 :         error (0, 0, "extra %zd bytes at end of unit",
    5336           0 :                (size_t) (next_unitp - readp));
    5337             : 
    5338           4 :     next_unit:
    5339             :       readp = next_unitp;
    5340             :     }
    5341             : }
    5342             : 
    5343             : /* Print content of DWARF .debug_aranges section.  We fortunately do
    5344             :    not have to know a bit about the structure of the section, libdwarf
    5345             :    takes care of it.  */
    5346             : static void
    5347           0 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
    5348             :                                GElf_Shdr *shdr, Dwarf *dbg)
    5349             : {
    5350           0 :   Dwarf_Aranges *aranges;
    5351           0 :   size_t cnt;
    5352           0 :   if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
    5353             :     {
    5354           0 :       error (0, 0, gettext ("cannot get .debug_aranges content: %s"),
    5355             :              dwarf_errmsg (-1));
    5356           0 :       return;
    5357             :     }
    5358             : 
    5359           0 :   GElf_Shdr glink_mem;
    5360           0 :   GElf_Shdr *glink;
    5361           0 :   glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
    5362           0 :   if (glink == NULL)
    5363             :     {
    5364           0 :       error (0, 0, gettext ("invalid sh_link value in section %zu"),
    5365             :              elf_ndxscn (scn));
    5366           0 :       return;
    5367             :     }
    5368             : 
    5369           0 :   printf (ngettext ("\
    5370             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
    5371             :                     "\
    5372             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
    5373             :                     cnt),
    5374             :           elf_ndxscn (scn), section_name (ebl, shdr),
    5375           0 :           (uint64_t) shdr->sh_offset, cnt);
    5376             : 
    5377             :   /* Compute floor(log16(cnt)).  */
    5378           0 :   size_t tmp = cnt;
    5379           0 :   int digits = 1;
    5380           0 :   while (tmp >= 16)
    5381             :     {
    5382           0 :       ++digits;
    5383           0 :       tmp >>= 4;
    5384             :     }
    5385             : 
    5386           0 :   for (size_t n = 0; n < cnt; ++n)
    5387             :     {
    5388           0 :       Dwarf_Arange *runp = dwarf_onearange (aranges, n);
    5389           0 :       if (unlikely (runp == NULL))
    5390             :         {
    5391           0 :           printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
    5392           0 :           return;
    5393             :         }
    5394             : 
    5395           0 :       Dwarf_Addr start;
    5396           0 :       Dwarf_Word length;
    5397           0 :       Dwarf_Off offset;
    5398             : 
    5399           0 :       if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
    5400           0 :         printf (gettext (" [%*zu] ???\n"), digits, n);
    5401             :       else
    5402           0 :         printf (gettext (" [%*zu] start: %0#*" PRIx64
    5403             :                          ", length: %5" PRIu64 ", CU DIE offset: %6"
    5404             :                          PRId64 "\n"),
    5405           0 :                 digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
    5406             :                 (uint64_t) start, (uint64_t) length, (int64_t) offset);
    5407             :     }
    5408             : }
    5409             : 
    5410             : 
    5411             : /* Print content of DWARF .debug_aranges section.  */
    5412             : static void
    5413          44 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    5414             :                              Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
    5415             :                              GElf_Shdr *shdr, Dwarf *dbg)
    5416             : {
    5417          44 :   if (decodedaranges)
    5418             :     {
    5419           1 :       print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
    5420           1 :       return;
    5421             :     }
    5422             : 
    5423          86 :   Elf_Data *data = (dbg->sectiondata[IDX_debug_aranges]
    5424          43 :                     ?: elf_rawdata (scn, NULL));
    5425             : 
    5426          43 :   if (unlikely (data == NULL))
    5427             :     {
    5428           0 :       error (0, 0, gettext ("cannot get .debug_aranges content: %s"),
    5429             :              elf_errmsg (-1));
    5430           0 :       return;
    5431             :     }
    5432             : 
    5433          43 :   printf (gettext ("\
    5434             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    5435             :           elf_ndxscn (scn), section_name (ebl, shdr),
    5436          43 :           (uint64_t) shdr->sh_offset);
    5437             : 
    5438          43 :   const unsigned char *readp = data->d_buf;
    5439          43 :   const unsigned char *readendp = readp + data->d_size;
    5440             : 
    5441        1708 :   while (readp < readendp)
    5442             :     {
    5443        1665 :       const unsigned char *hdrstart = readp;
    5444        1665 :       size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
    5445             : 
    5446        1665 :       printf (gettext ("\nTable at offset %zu:\n"), start_offset);
    5447        1665 :       if (readp + 4 > readendp)
    5448             :         {
    5449           0 :         invalid_data:
    5450           0 :           error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
    5451             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    5452           0 :           return;
    5453             :         }
    5454             : 
    5455        1665 :       Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
    5456        1665 :       unsigned int length_bytes = 4;
    5457        1665 :       if (length == DWARF3_LENGTH_64_BIT)
    5458             :         {
    5459           0 :           if (readp + 8 > readendp)
    5460             :             goto invalid_data;
    5461           0 :           length = read_8ubyte_unaligned_inc (dbg, readp);
    5462           0 :           length_bytes = 8;
    5463             :         }
    5464             : 
    5465        1665 :       const unsigned char *nexthdr = readp + length;
    5466        1665 :       printf (gettext ("\n Length:        %6" PRIu64 "\n"),
    5467             :               (uint64_t) length);
    5468             : 
    5469        1665 :       if (unlikely (length > (size_t) (readendp - readp)))
    5470             :         goto invalid_data;
    5471             : 
    5472        1665 :       if (length == 0)
    5473             :         continue;
    5474             : 
    5475        1665 :       if (readp + 2 > readendp)
    5476             :         goto invalid_data;
    5477        1665 :       uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    5478        1665 :       printf (gettext (" DWARF version: %6" PRIuFAST16 "\n"),
    5479             :               version);
    5480        1665 :       if (version != 2)
    5481             :         {
    5482           0 :           error (0, 0, gettext ("unsupported aranges version"));
    5483           0 :           goto next_table;
    5484             :         }
    5485             : 
    5486        1665 :       Dwarf_Word offset;
    5487        1665 :       if (readp + length_bytes > readendp)
    5488             :         goto invalid_data;
    5489        1665 :       if (length_bytes == 8)
    5490           0 :         offset = read_8ubyte_unaligned_inc (dbg, readp);
    5491             :       else
    5492        1665 :         offset = read_4ubyte_unaligned_inc (dbg, readp);
    5493        1665 :       printf (gettext (" CU offset:     %6" PRIx64 "\n"),
    5494             :               (uint64_t) offset);
    5495             : 
    5496        1665 :       if (readp + 1 > readendp)
    5497             :         goto invalid_data;
    5498        1665 :       unsigned int address_size = *readp++;
    5499        1665 :       printf (gettext (" Address size:  %6" PRIu64 "\n"),
    5500             :               (uint64_t) address_size);
    5501        1665 :       if (address_size != 4 && address_size != 8)
    5502             :         {
    5503           0 :           error (0, 0, gettext ("unsupported address size"));
    5504           0 :           goto next_table;
    5505             :         }
    5506             : 
    5507        1665 :       if (readp + 1 > readendp)
    5508             :         goto invalid_data;
    5509        1665 :       unsigned int segment_size = *readp++;
    5510        1665 :       printf (gettext (" Segment size:  %6" PRIu64 "\n\n"),
    5511             :               (uint64_t) segment_size);
    5512        1665 :       if (segment_size != 0 && segment_size != 4 && segment_size != 8)
    5513             :         {
    5514           0 :           error (0, 0, gettext ("unsupported segment size"));
    5515           0 :           goto next_table;
    5516             :         }
    5517             : 
    5518             :       /* Round the address to the next multiple of 2*address_size.  */
    5519        3330 :       readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
    5520        1665 :                 % (2 * address_size));
    5521             : 
    5522        3383 :       while (readp < nexthdr)
    5523             :         {
    5524        3383 :           Dwarf_Word range_address;
    5525        3383 :           Dwarf_Word range_length;
    5526        3383 :           Dwarf_Word segment = 0;
    5527        3383 :           if (readp + 2 * address_size + segment_size > readendp)
    5528             :             goto invalid_data;
    5529        3383 :           if (address_size == 4)
    5530             :             {
    5531          36 :               range_address = read_4ubyte_unaligned_inc (dbg, readp);
    5532          36 :               range_length = read_4ubyte_unaligned_inc (dbg, readp);
    5533             :             }
    5534             :           else
    5535             :             {
    5536        3347 :               range_address = read_8ubyte_unaligned_inc (dbg, readp);
    5537        3347 :               range_length = read_8ubyte_unaligned_inc (dbg, readp);
    5538             :             }
    5539             : 
    5540        3383 :           if (segment_size == 4)
    5541           0 :             segment = read_4ubyte_unaligned_inc (dbg, readp);
    5542        3383 :           else if (segment_size == 8)
    5543           0 :             segment = read_8ubyte_unaligned_inc (dbg, readp);
    5544             : 
    5545        3383 :           if (range_address == 0 && range_length == 0 && segment == 0)
    5546             :             break;
    5547             : 
    5548        1718 :           printf ("   ");
    5549        1718 :           print_dwarf_addr (dwflmod, address_size, range_address,
    5550             :                             range_address);
    5551        1718 :           printf ("..");
    5552        3436 :           print_dwarf_addr (dwflmod, address_size,
    5553        1718 :                             range_address + range_length - 1,
    5554             :                             range_length);
    5555        1718 :           if (segment_size != 0)
    5556           0 :             printf (" (%" PRIx64 ")\n", (uint64_t) segment);
    5557             :           else
    5558        1718 :             printf ("\n");
    5559             :         }
    5560             : 
    5561        1665 :     next_table:
    5562        1665 :       if (readp != nexthdr)
    5563             :         {
    5564           0 :           size_t padding = nexthdr - readp;
    5565           0 :           printf (gettext ("   %zu padding bytes\n"), padding);
    5566           0 :           readp = nexthdr;
    5567             :         }
    5568             :     }
    5569             : }
    5570             : 
    5571             : 
    5572             : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
    5573             : 
    5574             : /* Returns true and sets cu and cu_base if the given Dwarf is a split
    5575             :    DWARF (.dwo) file.  */
    5576             : static bool
    5577           0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
    5578             : {
    5579           0 :   uint64_t id;
    5580           0 :   if (is_split_dwarf (dbg, &id, cu))
    5581             :     {
    5582           0 :       Dwarf_Die cudie;
    5583           0 :       if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
    5584             :         {
    5585           0 :           *cu_base = cudie_base (&cudie);
    5586           0 :           return true;
    5587             :         }
    5588             :     }
    5589             :   return false;
    5590             : }
    5591             : 
    5592             : /* Print content of DWARF .debug_rnglists section.  */
    5593             : static void
    5594           2 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
    5595             :                               Ebl *ebl,
    5596             :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
    5597             :                               Elf_Scn *scn, GElf_Shdr *shdr,
    5598             :                               Dwarf *dbg __attribute__((unused)))
    5599             : {
    5600           2 :   printf (gettext ("\
    5601             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    5602             :           elf_ndxscn (scn), section_name (ebl, shdr),
    5603           2 :           (uint64_t) shdr->sh_offset);
    5604             : 
    5605           4 :   Elf_Data *data =(dbg->sectiondata[IDX_debug_rnglists]
    5606           2 :                    ?: elf_rawdata (scn, NULL));
    5607           2 :   if (unlikely (data == NULL))
    5608             :     {
    5609           0 :       error (0, 0, gettext ("cannot get .debug_rnglists content: %s"),
    5610             :              elf_errmsg (-1));
    5611           0 :       return;
    5612             :     }
    5613             : 
    5614             :   /* For the listptr to get the base address/CU.  */
    5615           2 :   sort_listptr (&known_rnglistptr, "rnglistptr");
    5616           2 :   size_t listptr_idx = 0;
    5617             : 
    5618           2 :   const unsigned char *readp = data->d_buf;
    5619           4 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    5620           2 :                                         + data->d_size);
    5621           4 :   while (readp < dataend)
    5622             :     {
    5623           2 :       if (unlikely (readp > dataend - 4))
    5624             :         {
    5625           0 :         invalid_data:
    5626           0 :           error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
    5627             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    5628           0 :           return;
    5629             :         }
    5630             : 
    5631           2 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    5632           2 :       printf (gettext ("Table at Offset 0x%" PRIx64 ":\n\n"),
    5633             :               (uint64_t) offset);
    5634             : 
    5635           2 :       uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    5636           2 :       unsigned int offset_size = 4;
    5637           2 :       if (unlikely (unit_length == 0xffffffff))
    5638             :         {
    5639           0 :           if (unlikely (readp > dataend - 8))
    5640             :             goto invalid_data;
    5641             : 
    5642           0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    5643           0 :           offset_size = 8;
    5644             :         }
    5645           2 :       printf (gettext (" Length:         %8" PRIu64 "\n"), unit_length);
    5646             : 
    5647             :       /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
    5648             :          bytes to complete the header.  And this unit cannot go beyond
    5649             :          the section data.  */
    5650           2 :       if (readp > dataend - 8
    5651           2 :           || unit_length < 8
    5652           2 :           || unit_length > (uint64_t) (dataend - readp))
    5653             :         goto invalid_data;
    5654             : 
    5655           2 :       const unsigned char *nexthdr = readp + unit_length;
    5656             : 
    5657           2 :       uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    5658           2 :       printf (gettext (" DWARF version:  %8" PRIu16 "\n"), version);
    5659             : 
    5660           2 :       if (version != 5)
    5661             :         {
    5662           0 :           error (0, 0, gettext ("Unknown version"));
    5663           0 :           goto next_table;
    5664             :         }
    5665             : 
    5666           2 :       uint8_t address_size = *readp++;
    5667           2 :       printf (gettext (" Address size:   %8" PRIu64 "\n"),
    5668             :               (uint64_t) address_size);
    5669             : 
    5670           2 :       if (address_size != 4 && address_size != 8)
    5671             :         {
    5672           0 :           error (0, 0, gettext ("unsupported address size"));
    5673           0 :           goto next_table;
    5674             :         }
    5675             : 
    5676           2 :       uint8_t segment_size = *readp++;
    5677           2 :       printf (gettext (" Segment size:   %8" PRIu64 "\n"),
    5678             :               (uint64_t) segment_size);
    5679             : 
    5680           2 :       if (segment_size != 0 && segment_size != 4 && segment_size != 8)
    5681             :         {
    5682           0 :           error (0, 0, gettext ("unsupported segment size"));
    5683           0 :           goto next_table;
    5684             :         }
    5685             : 
    5686           2 :       uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
    5687           2 :       printf (gettext (" Offset entries: %8" PRIu64 "\n"),
    5688             :               (uint64_t) offset_entry_count);
    5689             : 
    5690             :       /* We need the CU that uses this unit to get the initial base address. */
    5691           2 :       Dwarf_Addr cu_base = 0;
    5692           2 :       struct Dwarf_CU *cu = NULL;
    5693           2 :       if (listptr_cu (&known_rnglistptr, &listptr_idx,
    5694             :                       (Dwarf_Off) offset,
    5695           2 :                       (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
    5696             :                       &cu_base, &cu)
    5697           0 :           || split_dwarf_cu_base (dbg, &cu, &cu_base))
    5698           2 :         {
    5699           2 :           Dwarf_Die cudie;
    5700           2 :           if (dwarf_cu_die (cu, &cudie,
    5701             :                             NULL, NULL, NULL, NULL,
    5702             :                             NULL, NULL) == NULL)
    5703           0 :             printf (gettext (" Unknown CU base: "));
    5704             :           else
    5705           2 :             printf (gettext (" CU [%6" PRIx64 "] base: "),
    5706             :                     dwarf_dieoffset (&cudie));
    5707           2 :           print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
    5708           2 :           printf ("\n");
    5709             :         }
    5710             :       else
    5711           0 :         printf (gettext (" Not associated with a CU.\n"));
    5712             : 
    5713           2 :       printf ("\n");
    5714             : 
    5715           2 :       const unsigned char *offset_array_start = readp;
    5716           2 :       if (offset_entry_count > 0)
    5717             :         {
    5718           1 :           uint64_t max_entries = (unit_length - 8) / offset_size;
    5719           1 :           if (offset_entry_count > max_entries)
    5720             :             {
    5721           0 :               error (0, 0,
    5722           0 :                      gettext ("too many offset entries for unit length"));
    5723           0 :               offset_entry_count = max_entries;
    5724             :             }
    5725             : 
    5726           3 :           printf (gettext ("  Offsets starting at 0x%" PRIx64 ":\n"),
    5727             :                   (uint64_t) (offset_array_start
    5728           1 :                               - (unsigned char *) data->d_buf));
    5729           3 :           for (uint32_t idx = 0; idx < offset_entry_count; idx++)
    5730             :             {
    5731           2 :               printf ("   [%6" PRIu32 "] ", idx);
    5732           2 :               if (offset_size == 4)
    5733             :                 {
    5734           2 :                   uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
    5735           2 :                   printf ("0x%" PRIx32 "\n", off);
    5736             :                 }
    5737             :               else
    5738             :                 {
    5739           0 :                   uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
    5740           0 :                   printf ("0x%" PRIx64 "\n", off);
    5741             :                 }
    5742             :             }
    5743           1 :           printf ("\n");
    5744             :         }
    5745             : 
    5746           2 :       Dwarf_Addr base = cu_base;
    5747           2 :       bool start_of_list = true;
    5748          16 :       while (readp < nexthdr)
    5749             :         {
    5750          14 :           uint8_t kind = *readp++;
    5751          14 :           uint64_t op1, op2;
    5752             : 
    5753             :           /* Skip padding.  */
    5754          14 :           if (start_of_list && kind == DW_RLE_end_of_list)
    5755             :             continue;
    5756             : 
    5757          14 :           if (start_of_list)
    5758             :             {
    5759           4 :               base = cu_base;
    5760          16 :               printf ("  Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
    5761           4 :                       (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
    5762           4 :                       (uint64_t) (readp - offset_array_start - 1));
    5763           4 :               start_of_list = false;
    5764             :             }
    5765             : 
    5766          14 :           printf ("    %s", dwarf_range_list_encoding_name (kind));
    5767          14 :           switch (kind)
    5768             :             {
    5769           4 :             case DW_RLE_end_of_list:
    5770           4 :               start_of_list = true;
    5771           4 :               printf ("\n\n");
    5772             :               break;
    5773             : 
    5774           0 :             case DW_RLE_base_addressx:
    5775           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    5776             :                 {
    5777           0 :                 invalid_range:
    5778           0 :                   error (0, 0, gettext ("invalid range list data"));
    5779           0 :                   goto next_table;
    5780             :                 }
    5781           0 :               get_uleb128 (op1, readp, nexthdr);
    5782           0 :               printf (" %" PRIx64 "\n", op1);
    5783           0 :               if (! print_unresolved_addresses)
    5784             :                 {
    5785           0 :                   Dwarf_Addr addr;
    5786           0 :                   if (get_indexed_addr (cu, op1, &addr) != 0)
    5787           0 :                     printf ("      ???\n");
    5788             :                   else
    5789             :                     {
    5790           0 :                       printf ("      ");
    5791           0 :                       print_dwarf_addr (dwflmod, address_size, addr, addr);
    5792           0 :                       printf ("\n");
    5793             :                     }
    5794             :                 }
    5795             :               break;
    5796             : 
    5797           0 :             case DW_RLE_startx_endx:
    5798           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    5799             :                 goto invalid_range;
    5800           0 :               get_uleb128 (op1, readp, nexthdr);
    5801           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    5802             :                 goto invalid_range;
    5803           0 :               get_uleb128 (op2, readp, nexthdr);
    5804           0 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    5805           0 :               if (! print_unresolved_addresses)
    5806             :                 {
    5807           0 :                   Dwarf_Addr addr1;
    5808           0 :                   Dwarf_Addr addr2;
    5809           0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0
    5810           0 :                       || get_indexed_addr (cu, op2, &addr2) != 0)
    5811             :                     {
    5812           0 :                       printf ("      ???..\n");
    5813           0 :                       printf ("      ???\n");
    5814             :                     }
    5815             :                   else
    5816             :                     {
    5817           0 :                       printf ("      ");
    5818           0 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1);
    5819           0 :                       printf ("..\n      ");
    5820           0 :                       print_dwarf_addr (dwflmod, address_size,
    5821             :                                         addr2 - 1, addr2);
    5822           0 :                       printf ("\n");
    5823             :                     }
    5824             :                 }
    5825             :               break;
    5826             : 
    5827           0 :             case DW_RLE_startx_length:
    5828           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    5829             :                 goto invalid_range;
    5830           0 :               get_uleb128 (op1, readp, nexthdr);
    5831           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    5832             :                 goto invalid_range;
    5833           0 :               get_uleb128 (op2, readp, nexthdr);
    5834           0 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    5835           0 :               if (! print_unresolved_addresses)
    5836             :                 {
    5837           0 :                   Dwarf_Addr addr1;
    5838           0 :                   Dwarf_Addr addr2;
    5839           0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0)
    5840             :                     {
    5841           0 :                       printf ("      ???..\n");
    5842           0 :                       printf ("      ???\n");
    5843             :                     }
    5844             :                   else
    5845             :                     {
    5846           0 :                       addr2 = addr1 + op2;
    5847           0 :                       printf ("      ");
    5848           0 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1);
    5849           0 :                       printf ("..\n      ");
    5850           0 :                       print_dwarf_addr (dwflmod, address_size,
    5851             :                                         addr2 - 1, addr2);
    5852           0 :                       printf ("\n");
    5853             :                     }
    5854             :                 }
    5855             :               break;
    5856             : 
    5857           4 :             case DW_RLE_offset_pair:
    5858           4 :               if ((uint64_t) (nexthdr - readp) < 1)
    5859             :                 goto invalid_range;
    5860           4 :               get_uleb128 (op1, readp, nexthdr);
    5861           4 :               if ((uint64_t) (nexthdr - readp) < 1)
    5862             :                 goto invalid_range;
    5863           4 :               get_uleb128 (op2, readp, nexthdr);
    5864           4 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    5865           4 :               if (! print_unresolved_addresses)
    5866             :                 {
    5867           4 :                   op1 += base;
    5868           4 :                   op2 += base;
    5869           4 :                   printf ("      ");
    5870           4 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    5871           4 :                   printf ("..\n      ");
    5872           4 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    5873           4 :                   printf ("\n");
    5874             :                 }
    5875             :               break;
    5876             : 
    5877           2 :             case DW_RLE_base_address:
    5878           2 :               if (address_size == 4)
    5879             :                 {
    5880           0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    5881             :                     goto invalid_range;
    5882           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    5883             :                 }
    5884             :               else
    5885             :                 {
    5886           2 :                   if ((uint64_t) (nexthdr - readp) < 8)
    5887             :                     goto invalid_range;
    5888           2 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    5889             :                 }
    5890           2 :               base = op1;
    5891           2 :               printf (" 0x%" PRIx64 "\n", base);
    5892           2 :               if (! print_unresolved_addresses)
    5893             :                 {
    5894           2 :                   printf ("      ");
    5895           2 :                   print_dwarf_addr (dwflmod, address_size, base, base);
    5896           2 :                   printf ("\n");
    5897             :                 }
    5898             :               break;
    5899             : 
    5900           0 :             case DW_RLE_start_end:
    5901           0 :               if (address_size == 4)
    5902             :                 {
    5903           0 :                   if ((uint64_t) (nexthdr - readp) < 8)
    5904             :                     goto invalid_range;
    5905           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    5906           0 :                   op2 = read_4ubyte_unaligned_inc (dbg, readp);
    5907             :                 }
    5908             :               else
    5909             :                 {
    5910           0 :                   if ((uint64_t) (nexthdr - readp) < 16)
    5911             :                     goto invalid_range;
    5912           0 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    5913           0 :                   op2 = read_8ubyte_unaligned_inc (dbg, readp);
    5914             :                 }
    5915           0 :               printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
    5916           0 :               if (! print_unresolved_addresses)
    5917             :                 {
    5918           0 :                   printf ("      ");
    5919           0 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    5920           0 :                   printf ("..\n      ");
    5921           0 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    5922           0 :                   printf ("\n");
    5923             :                 }
    5924             :               break;
    5925             : 
    5926           4 :             case DW_RLE_start_length:
    5927           4 :               if (address_size == 4)
    5928             :                 {
    5929           0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    5930             :                     goto invalid_range;
    5931           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    5932             :                 }
    5933             :               else
    5934             :                 {
    5935           4 :                   if ((uint64_t) (nexthdr - readp) < 8)
    5936             :                     goto invalid_range;
    5937           4 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    5938             :                 }
    5939           4 :               if ((uint64_t) (nexthdr - readp) < 1)
    5940             :                 goto invalid_range;
    5941           4 :               get_uleb128 (op2, readp, nexthdr);
    5942           4 :               printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
    5943           4 :               if (! print_unresolved_addresses)
    5944             :                 {
    5945           4 :                   op2 = op1 + op2;
    5946           4 :                   printf ("      ");
    5947           4 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    5948           4 :                   printf ("..\n      ");
    5949           4 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    5950           4 :                   printf ("\n");
    5951             :                 }
    5952             :               break;
    5953             : 
    5954             :             default:
    5955             :               goto invalid_range;
    5956             :             }
    5957             :         }
    5958             : 
    5959           2 :     next_table:
    5960           2 :       if (readp != nexthdr)
    5961             :         {
    5962           0 :           size_t padding = nexthdr - readp;
    5963           0 :           printf (gettext ("   %zu padding bytes\n\n"), padding);
    5964           0 :           readp = nexthdr;
    5965             :         }
    5966             :     }
    5967             : }
    5968             : 
    5969             : /* Print content of DWARF .debug_ranges section.  */
    5970             : static void
    5971          30 : print_debug_ranges_section (Dwfl_Module *dwflmod,
    5972             :                             Ebl *ebl, GElf_Ehdr *ehdr,
    5973             :                             Elf_Scn *scn, GElf_Shdr *shdr,
    5974             :                             Dwarf *dbg)
    5975             : {
    5976          60 :   Elf_Data *data = (dbg->sectiondata[IDX_debug_ranges]
    5977          30 :                     ?: elf_rawdata (scn, NULL));
    5978          30 :   if (unlikely (data == NULL))
    5979             :     {
    5980           0 :       error (0, 0, gettext ("cannot get .debug_ranges content: %s"),
    5981             :              elf_errmsg (-1));
    5982           0 :       return;
    5983             :     }
    5984             : 
    5985          30 :   printf (gettext ("\
    5986             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    5987             :           elf_ndxscn (scn), section_name (ebl, shdr),
    5988          30 :           (uint64_t) shdr->sh_offset);
    5989             : 
    5990          30 :   sort_listptr (&known_rangelistptr, "rangelistptr");
    5991          30 :   size_t listptr_idx = 0;
    5992             : 
    5993          30 :   uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    5994             : 
    5995          30 :   bool first = true;
    5996          30 :   Dwarf_Addr base = 0;
    5997          30 :   unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
    5998          30 :   unsigned char *readp = data->d_buf;
    5999          30 :   Dwarf_CU *last_cu = NULL;
    6000       60659 :   while (readp < endp)
    6001             :     {
    6002       60629 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    6003       60629 :       Dwarf_CU *cu = last_cu;
    6004             : 
    6005       60629 :       if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
    6006             :                                       &address_size, NULL, &base, &cu,
    6007             :                                       offset, &readp, endp, NULL))
    6008           5 :         continue;
    6009             : 
    6010       60624 :       if (last_cu != cu)
    6011             :         {
    6012         933 :           Dwarf_Die cudie;
    6013         933 :           if (dwarf_cu_die (cu, &cudie,
    6014             :                             NULL, NULL, NULL, NULL,
    6015             :                             NULL, NULL) == NULL)
    6016           0 :             printf (gettext ("\n Unknown CU base: "));
    6017             :           else
    6018         933 :             printf (gettext ("\n CU [%6" PRIx64 "] base: "),
    6019             :                     dwarf_dieoffset (&cudie));
    6020         933 :           print_dwarf_addr (dwflmod, address_size, base, base);
    6021         933 :           printf ("\n");
    6022             :         }
    6023       60624 :       last_cu = cu;
    6024             : 
    6025       60624 :       if (unlikely (data->d_size - offset < (size_t) address_size * 2))
    6026             :         {
    6027           0 :           printf (gettext (" [%6tx]  <INVALID DATA>\n"), offset);
    6028           0 :           break;
    6029             :         }
    6030             : 
    6031       60624 :       Dwarf_Addr begin;
    6032       60624 :       Dwarf_Addr end;
    6033       60624 :       if (address_size == 8)
    6034             :         {
    6035       60616 :           begin = read_8ubyte_unaligned_inc (dbg, readp);
    6036       60616 :           end = read_8ubyte_unaligned_inc (dbg, readp);
    6037             :         }
    6038             :       else
    6039             :         {
    6040           8 :           begin = read_4ubyte_unaligned_inc (dbg, readp);
    6041           8 :           end = read_4ubyte_unaligned_inc (dbg, readp);
    6042           8 :           if (begin == (Dwarf_Addr) (uint32_t) -1)
    6043             :             begin = (Dwarf_Addr) -1l;
    6044             :         }
    6045             : 
    6046       60624 :       if (begin == (Dwarf_Addr) -1l) /* Base address entry.  */
    6047             :         {
    6048           0 :           printf (gettext (" [%6tx] base address\n          "), offset);
    6049           0 :           print_dwarf_addr (dwflmod, address_size, end, end);
    6050           0 :           printf ("\n");
    6051           0 :           base = end;
    6052             :         }
    6053       60624 :       else if (begin == 0 && end == 0) /* End of list entry.  */
    6054             :         {
    6055       13430 :           if (first)
    6056           5 :             printf (gettext (" [%6tx] empty list\n"), offset);
    6057             :           first = true;
    6058             :         }
    6059             :       else
    6060             :         {
    6061             :           /* We have an address range entry.  */
    6062       47194 :           if (first)            /* First address range entry in a list.  */
    6063       13425 :             printf (" [%6tx] ", offset);
    6064             :           else
    6065       33769 :             printf ("          ");
    6066             : 
    6067       47194 :           printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
    6068       47194 :           if (! print_unresolved_addresses)
    6069             :             {
    6070       47184 :               printf ("          ");
    6071       47184 :               print_dwarf_addr (dwflmod, address_size, base + begin,
    6072             :                                 base + begin);
    6073       47184 :               printf ("..\n          ");
    6074       47184 :               print_dwarf_addr (dwflmod, address_size,
    6075             :                                 base + end - 1, base + end);
    6076       47184 :               printf ("\n");
    6077             :             }
    6078             : 
    6079             :           first = false;
    6080             :         }
    6081             :     }
    6082             : }
    6083             : 
    6084             : #define REGNAMESZ 16
    6085             : static const char *
    6086       14015 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
    6087             :                char name[REGNAMESZ], int *bits, int *type)
    6088             : {
    6089       14015 :   const char *set;
    6090       14015 :   const char *pfx;
    6091       14015 :   int ignore;
    6092       40349 :   ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
    6093             :                                  bits ?: &ignore, type ?: &ignore);
    6094       14015 :   if (n <= 0)
    6095             :     {
    6096           0 :       if (loc != NULL)
    6097           0 :         snprintf (name, REGNAMESZ, "reg%u", loc->regno);
    6098             :       else
    6099           0 :         snprintf (name, REGNAMESZ, "??? 0x%x", regno);
    6100           0 :       if (bits != NULL)
    6101           0 :         *bits = loc != NULL ? loc->bits : 0;
    6102           0 :       if (type != NULL)
    6103           0 :         *type = DW_ATE_unsigned;
    6104           0 :       set = "??? unrecognized";
    6105             :     }
    6106             :   else
    6107             :     {
    6108       14015 :       if (bits != NULL && *bits <= 0)
    6109           0 :         *bits = loc != NULL ? loc->bits : 0;
    6110       14015 :       if (type != NULL && *type == DW_ATE_void)
    6111           0 :         *type = DW_ATE_unsigned;
    6112             : 
    6113             :     }
    6114       14015 :   return set;
    6115             : }
    6116             : 
    6117             : static const unsigned char *
    6118           0 : read_encoded (unsigned int encoding, const unsigned char *readp,
    6119             :               const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
    6120             : {
    6121           0 :   if ((encoding & 0xf) == DW_EH_PE_absptr)
    6122           0 :     encoding = gelf_getclass (dbg->elf) == ELFCLASS32
    6123           0 :       ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
    6124             : 
    6125           0 :   switch (encoding & 0xf)
    6126             :     {
    6127           0 :     case DW_EH_PE_uleb128:
    6128           0 :       get_uleb128 (*res, readp, endp);
    6129           0 :       break;
    6130           0 :     case DW_EH_PE_sleb128:
    6131           0 :       get_sleb128 (*res, readp, endp);
    6132           0 :       break;
    6133           0 :     case DW_EH_PE_udata2:
    6134           0 :       if (readp + 2 > endp)
    6135           0 :         goto invalid;
    6136           0 :       *res = read_2ubyte_unaligned_inc (dbg, readp);
    6137           0 :       break;
    6138           0 :     case DW_EH_PE_udata4:
    6139           0 :       if (readp + 4 > endp)
    6140           0 :         goto invalid;
    6141           0 :       *res = read_4ubyte_unaligned_inc (dbg, readp);
    6142           0 :       break;
    6143           0 :     case DW_EH_PE_udata8:
    6144           0 :       if (readp + 8 > endp)
    6145           0 :         goto invalid;
    6146           0 :       *res = read_8ubyte_unaligned_inc (dbg, readp);
    6147           0 :       break;
    6148           0 :     case DW_EH_PE_sdata2:
    6149           0 :       if (readp + 2 > endp)
    6150           0 :         goto invalid;
    6151           0 :       *res = read_2sbyte_unaligned_inc (dbg, readp);
    6152           0 :       break;
    6153           0 :     case DW_EH_PE_sdata4:
    6154           0 :       if (readp + 4 > endp)
    6155           0 :         goto invalid;
    6156           0 :       *res = read_4sbyte_unaligned_inc (dbg, readp);
    6157           0 :       break;
    6158           0 :     case DW_EH_PE_sdata8:
    6159           0 :       if (readp + 8 > endp)
    6160           0 :         goto invalid;
    6161           0 :       *res = read_8sbyte_unaligned_inc (dbg, readp);
    6162           0 :       break;
    6163             :     default:
    6164           0 :     invalid:
    6165           0 :       error (1, 0,
    6166           0 :              gettext ("invalid encoding"));
    6167             :     }
    6168             : 
    6169           0 :   return readp;
    6170             : }
    6171             : 
    6172             : 
    6173             : static void
    6174        9422 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
    6175             :                    Dwarf_Word vma_base, unsigned int code_align,
    6176             :                    int data_align,
    6177             :                    unsigned int version, unsigned int ptr_size,
    6178             :                    unsigned int encoding,
    6179             :                    Dwfl_Module *dwflmod, Ebl *ebl, Dwarf *dbg)
    6180             : {
    6181        9422 :   char regnamebuf[REGNAMESZ];
    6182        9422 :   const char *regname (unsigned int regno)
    6183             :   {
    6184       13167 :     register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
    6185       13167 :     return regnamebuf;
    6186             :   }
    6187             : 
    6188        9422 :   puts ("\n   Program:");
    6189        9422 :   Dwarf_Word pc = vma_base;
    6190      146148 :   while (readp < endp)
    6191             :     {
    6192      136726 :       unsigned int opcode = *readp++;
    6193             : 
    6194      136726 :       if (opcode < DW_CFA_advance_loc)
    6195             :         /* Extended opcode.  */
    6196       82894 :         switch (opcode)
    6197             :           {
    6198       27685 :             uint64_t op1;
    6199       27685 :             int64_t sop1;
    6200       27685 :             uint64_t op2;
    6201       27685 :             int64_t sop2;
    6202             : 
    6203       27685 :           case DW_CFA_nop:
    6204       27685 :             puts ("     nop");
    6205       82894 :             break;
    6206           0 :           case DW_CFA_set_loc:
    6207           0 :             if ((uint64_t) (endp - readp) < 1)
    6208             :               goto invalid;
    6209           0 :             readp = read_encoded (encoding, readp, endp, &op1, dbg);
    6210           0 :             printf ("     set_loc %#" PRIx64 " to %#" PRIx64 "\n",
    6211           0 :                     op1, pc = vma_base + op1);
    6212             :             break;
    6213        3194 :           case DW_CFA_advance_loc1:
    6214        3194 :             if ((uint64_t) (endp - readp) < 1)
    6215             :               goto invalid;
    6216        9582 :             printf ("     advance_loc1 %u to %#" PRIx64 "\n",
    6217        3194 :                     *readp, pc += *readp * code_align);
    6218        3194 :             ++readp;
    6219        3194 :             break;
    6220        1157 :           case DW_CFA_advance_loc2:
    6221        1157 :             if ((uint64_t) (endp - readp) < 2)
    6222             :               goto invalid;
    6223        1157 :             op1 = read_2ubyte_unaligned_inc (dbg, readp);
    6224        2314 :             printf ("     advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
    6225        1157 :                     op1, pc += op1 * code_align);
    6226             :             break;
    6227          24 :           case DW_CFA_advance_loc4:
    6228          24 :             if ((uint64_t) (endp - readp) < 4)
    6229             :               goto invalid;
    6230          24 :             op1 = read_4ubyte_unaligned_inc (dbg, readp);
    6231          48 :             printf ("     advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
    6232          24 :                     op1, pc += op1 * code_align);
    6233             :             break;
    6234           4 :           case DW_CFA_offset_extended:
    6235           4 :             if ((uint64_t) (endp - readp) < 1)
    6236             :               goto invalid;
    6237           4 :             get_uleb128 (op1, readp, endp);
    6238           4 :             if ((uint64_t) (endp - readp) < 1)
    6239             :               goto invalid;
    6240           4 :             get_uleb128 (op2, readp, endp);
    6241           4 :             printf ("     offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
    6242             :                     "\n",
    6243             :                     op1, regname (op1), op2 * data_align);
    6244             :             break;
    6245           0 :           case DW_CFA_restore_extended:
    6246           0 :             if ((uint64_t) (endp - readp) < 1)
    6247             :               goto invalid;
    6248           0 :             get_uleb128 (op1, readp, endp);
    6249           0 :             printf ("     restore_extended r%" PRIu64 " (%s)\n",
    6250             :                     op1, regname (op1));
    6251             :             break;
    6252          19 :           case DW_CFA_undefined:
    6253          19 :             if ((uint64_t) (endp - readp) < 1)
    6254             :               goto invalid;
    6255          19 :             get_uleb128 (op1, readp, endp);
    6256          19 :             printf ("     undefined r%" PRIu64 " (%s)\n", op1, regname (op1));
    6257             :             break;
    6258           0 :           case DW_CFA_same_value:
    6259           0 :             if ((uint64_t) (endp - readp) < 1)
    6260             :               goto invalid;
    6261           0 :             get_uleb128 (op1, readp, endp);
    6262           0 :             printf ("     same_value r%" PRIu64 " (%s)\n", op1, regname (op1));
    6263             :             break;
    6264           0 :           case DW_CFA_register:
    6265           0 :             if ((uint64_t) (endp - readp) < 1)
    6266             :               goto invalid;
    6267           0 :             get_uleb128 (op1, readp, endp);
    6268           0 :             if ((uint64_t) (endp - readp) < 1)
    6269             :               goto invalid;
    6270           0 :             get_uleb128 (op2, readp, endp);
    6271           0 :             printf ("     register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
    6272             :                     op1, regname (op1), op2, regname (op2));
    6273             :             break;
    6274        5528 :           case DW_CFA_remember_state:
    6275        5528 :             puts ("     remember_state");
    6276        5528 :             break;
    6277        5528 :           case DW_CFA_restore_state:
    6278        5528 :             puts ("     restore_state");
    6279        5528 :             break;
    6280         189 :           case DW_CFA_def_cfa:
    6281         189 :             if ((uint64_t) (endp - readp) < 1)
    6282             :               goto invalid;
    6283         189 :             get_uleb128 (op1, readp, endp);
    6284         189 :             if ((uint64_t) (endp - readp) < 1)
    6285             :               goto invalid;
    6286         189 :             get_uleb128 (op2, readp, endp);
    6287         189 :             printf ("     def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
    6288             :                     op1, regname (op1), op2);
    6289             :             break;
    6290          52 :           case DW_CFA_def_cfa_register:
    6291          52 :             if ((uint64_t) (endp - readp) < 1)
    6292             :               goto invalid;
    6293          52 :             get_uleb128 (op1, readp, endp);
    6294          52 :             printf ("     def_cfa_register r%" PRIu64 " (%s)\n",
    6295             :                     op1, regname (op1));
    6296             :             break;
    6297       39477 :           case DW_CFA_def_cfa_offset:
    6298       39477 :             if ((uint64_t) (endp - readp) < 1)
    6299             :               goto invalid;
    6300       39477 :             get_uleb128 (op1, readp, endp);
    6301       39477 :             printf ("     def_cfa_offset %" PRIu64 "\n", op1);
    6302             :             break;
    6303          21 :           case DW_CFA_def_cfa_expression:
    6304          21 :             if ((uint64_t) (endp - readp) < 1)
    6305             :               goto invalid;
    6306          21 :             get_uleb128 (op1, readp, endp);     /* Length of DW_FORM_block.  */
    6307          21 :             printf ("     def_cfa_expression %" PRIu64 "\n", op1);
    6308          21 :             if ((uint64_t) (endp - readp) < op1)
    6309             :               {
    6310           0 :             invalid:
    6311           0 :                 fputs (gettext ("         <INVALID DATA>\n"), stdout);
    6312           0 :                 return;
    6313             :               }
    6314          21 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
    6315             :                        op1, readp);
    6316          21 :             readp += op1;
    6317          21 :             break;
    6318           0 :           case DW_CFA_expression:
    6319           0 :             if ((uint64_t) (endp - readp) < 1)
    6320             :               goto invalid;
    6321           0 :             get_uleb128 (op1, readp, endp);
    6322           0 :             if ((uint64_t) (endp - readp) < 1)
    6323             :               goto invalid;
    6324           0 :             get_uleb128 (op2, readp, endp);     /* Length of DW_FORM_block.  */
    6325           0 :             printf ("     expression r%" PRIu64 " (%s) \n",
    6326             :                     op1, regname (op1));
    6327           0 :             if ((uint64_t) (endp - readp) < op2)
    6328             :               goto invalid;
    6329           0 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
    6330             :                        op2, readp);
    6331           0 :             readp += op2;
    6332           0 :             break;
    6333          16 :           case DW_CFA_offset_extended_sf:
    6334          16 :             if ((uint64_t) (endp - readp) < 1)
    6335             :               goto invalid;
    6336          16 :             get_uleb128 (op1, readp, endp);
    6337          16 :             if ((uint64_t) (endp - readp) < 1)
    6338             :               goto invalid;
    6339          16 :             get_sleb128 (sop2, readp, endp);
    6340          16 :             printf ("     offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
    6341             :                     PRId64 "\n",
    6342             :                     op1, regname (op1), sop2 * data_align);
    6343             :             break;
    6344           0 :           case DW_CFA_def_cfa_sf:
    6345           0 :             if ((uint64_t) (endp - readp) < 1)
    6346             :               goto invalid;
    6347           0 :             get_uleb128 (op1, readp, endp);
    6348           0 :             if ((uint64_t) (endp - readp) < 1)
    6349             :               goto invalid;
    6350           0 :             get_sleb128 (sop2, readp, endp);
    6351           0 :             printf ("     def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
    6352             :                     op1, regname (op1), sop2 * data_align);
    6353             :             break;
    6354           0 :           case DW_CFA_def_cfa_offset_sf:
    6355           0 :             if ((uint64_t) (endp - readp) < 1)
    6356             :               goto invalid;
    6357           0 :             get_sleb128 (sop1, readp, endp);
    6358           0 :             printf ("     def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
    6359             :             break;
    6360           0 :           case DW_CFA_val_offset:
    6361           0 :             if ((uint64_t) (endp - readp) < 1)
    6362             :               goto invalid;
    6363           0 :             get_uleb128 (op1, readp, endp);
    6364           0 :             if ((uint64_t) (endp - readp) < 1)
    6365             :               goto invalid;
    6366           0 :             get_uleb128 (op2, readp, endp);
    6367           0 :             printf ("     val_offset %" PRIu64 " at offset %" PRIu64 "\n",
    6368             :                     op1, op2 * data_align);
    6369             :             break;
    6370           0 :           case DW_CFA_val_offset_sf:
    6371           0 :             if ((uint64_t) (endp - readp) < 1)
    6372             :               goto invalid;
    6373           0 :             get_uleb128 (op1, readp, endp);
    6374           0 :             if ((uint64_t) (endp - readp) < 1)
    6375             :               goto invalid;
    6376           0 :             get_sleb128 (sop2, readp, endp);
    6377           0 :             printf ("     val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
    6378             :                     op1, sop2 * data_align);
    6379             :             break;
    6380           0 :           case DW_CFA_val_expression:
    6381           0 :             if ((uint64_t) (endp - readp) < 1)
    6382             :               goto invalid;
    6383           0 :             get_uleb128 (op1, readp, endp);
    6384           0 :             if ((uint64_t) (endp - readp) < 1)
    6385             :               goto invalid;
    6386           0 :             get_uleb128 (op2, readp, endp);     /* Length of DW_FORM_block.  */
    6387           0 :             printf ("     val_expression r%" PRIu64 " (%s)\n",
    6388             :                     op1, regname (op1));
    6389           0 :             if ((uint64_t) (endp - readp) < op2)
    6390             :               goto invalid;
    6391           0 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
    6392             :                        NULL, op2, readp);
    6393           0 :             readp += op2;
    6394           0 :             break;
    6395           0 :           case DW_CFA_MIPS_advance_loc8:
    6396           0 :             if ((uint64_t) (endp - readp) < 8)
    6397             :               goto invalid;
    6398           0 :             op1 = read_8ubyte_unaligned_inc (dbg, readp);
    6399           0 :             printf ("     MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
    6400           0 :                     op1, pc += op1 * code_align);
    6401             :             break;
    6402           0 :           case DW_CFA_GNU_window_save:
    6403           0 :             puts ("     GNU_window_save");
    6404           0 :             break;
    6405           0 :           case DW_CFA_GNU_args_size:
    6406           0 :             if ((uint64_t) (endp - readp) < 1)
    6407             :               goto invalid;
    6408           0 :             get_uleb128 (op1, readp, endp);
    6409           0 :             printf ("     args_size %" PRIu64 "\n", op1);
    6410             :             break;
    6411             :           default:
    6412           0 :             printf ("     ??? (%u)\n", opcode);
    6413             :             break;
    6414             :           }
    6415       53832 :       else if (opcode < DW_CFA_offset)
    6416       81890 :         printf ("     advance_loc %u to %#" PRIx64 "\n",
    6417       40945 :                 opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
    6418       12887 :       else if (opcode < DW_CFA_restore)
    6419             :         {
    6420       11354 :           uint64_t offset;
    6421       11354 :           if ((uint64_t) (endp - readp) < 1)
    6422             :             goto invalid;
    6423       11354 :           get_uleb128 (offset, readp, endp);
    6424       11354 :           printf ("     offset r%u (%s) at cfa%+" PRId64 "\n",
    6425             :                   opcode & 0x3f, regname (opcode & 0x3f), offset * data_align);
    6426             :         }
    6427             :       else
    6428        1533 :         printf ("     restore r%u (%s)\n",
    6429             :                 opcode & 0x3f, regname (opcode & 0x3f));
    6430             :     }
    6431             : }
    6432             : 
    6433             : 
    6434             : static unsigned int
    6435        9274 : encoded_ptr_size (int encoding, unsigned int ptr_size)
    6436             : {
    6437        9274 :   switch (encoding & 7)
    6438             :     {
    6439             :     case DW_EH_PE_udata4:
    6440             :       return 4;
    6441           0 :     case DW_EH_PE_udata8:
    6442           0 :       return 8;
    6443          48 :     case 0:
    6444          48 :       return ptr_size;
    6445             :     }
    6446             : 
    6447           0 :   fprintf (stderr, "Unsupported pointer encoding: %#x, "
    6448             :            "assuming pointer size of %d.\n", encoding, ptr_size);
    6449           0 :   return ptr_size;
    6450             : }
    6451             : 
    6452             : 
    6453             : static unsigned int
    6454         195 : print_encoding (unsigned int val)
    6455             : {
    6456         195 :   switch (val & 0xf)
    6457             :     {
    6458           0 :     case DW_EH_PE_absptr:
    6459           0 :       fputs ("absptr", stdout);
    6460           0 :       break;
    6461           0 :     case DW_EH_PE_uleb128:
    6462           0 :       fputs ("uleb128", stdout);
    6463           0 :       break;
    6464           0 :     case DW_EH_PE_udata2:
    6465           0 :       fputs ("udata2", stdout);
    6466           0 :       break;
    6467          29 :     case DW_EH_PE_udata4:
    6468          29 :       fputs ("udata4", stdout);
    6469          29 :       break;
    6470           0 :     case DW_EH_PE_udata8:
    6471           0 :       fputs ("udata8", stdout);
    6472           0 :       break;
    6473           0 :     case DW_EH_PE_sleb128:
    6474           0 :       fputs ("sleb128", stdout);
    6475           0 :       break;
    6476           0 :     case DW_EH_PE_sdata2:
    6477           0 :       fputs ("sdata2", stdout);
    6478           0 :       break;
    6479         166 :     case DW_EH_PE_sdata4:
    6480         166 :       fputs ("sdata4", stdout);
    6481         166 :       break;
    6482           0 :     case DW_EH_PE_sdata8:
    6483           0 :       fputs ("sdata8", stdout);
    6484           0 :       break;
    6485             :     default:
    6486             :       /* We did not use any of the bits after all.  */
    6487             :       return val;
    6488             :     }
    6489             : 
    6490         195 :   return val & ~0xf;
    6491             : }
    6492             : 
    6493             : 
    6494             : static unsigned int
    6495         166 : print_relinfo (unsigned int val)
    6496             : {
    6497         166 :   switch (val & 0x70)
    6498             :     {
    6499         137 :     case DW_EH_PE_pcrel:
    6500         137 :       fputs ("pcrel", stdout);
    6501         137 :       break;
    6502           0 :     case DW_EH_PE_textrel:
    6503           0 :       fputs ("textrel", stdout);
    6504           0 :       break;
    6505          29 :     case DW_EH_PE_datarel:
    6506          29 :       fputs ("datarel", stdout);
    6507          29 :       break;
    6508           0 :     case DW_EH_PE_funcrel:
    6509           0 :       fputs ("funcrel", stdout);
    6510           0 :       break;
    6511           0 :     case DW_EH_PE_aligned:
    6512           0 :       fputs ("aligned", stdout);
    6513           0 :       break;
    6514             :     default:
    6515             :       return val;
    6516             :     }
    6517             : 
    6518         166 :   return val & ~0x70;
    6519             : }
    6520             : 
    6521             : 
    6522             : static void
    6523         195 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
    6524             : {
    6525         195 :   printf ("(%s", pfx);
    6526             : 
    6527         195 :   if (fde_encoding == DW_EH_PE_omit)
    6528           0 :     puts ("omit)");
    6529             :   else
    6530             :     {
    6531         195 :       unsigned int w = fde_encoding;
    6532             : 
    6533         195 :       w = print_encoding (w);
    6534             : 
    6535         195 :       if (w & 0x70)
    6536             :         {
    6537         166 :           if (w != fde_encoding)
    6538         166 :             fputc_unlocked (' ', stdout);
    6539             : 
    6540         166 :           w = print_relinfo (w);
    6541             :         }
    6542             : 
    6543         195 :       if (w != 0)
    6544           0 :         printf ("%s%x", w != fde_encoding ? " " : "", w);
    6545             : 
    6546         195 :       puts (")");
    6547             :     }
    6548         195 : }
    6549             : 
    6550             : 
    6551             : static void
    6552          65 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    6553             :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    6554             : {
    6555          65 :   size_t shstrndx;
    6556             :   /* We know this call will succeed since it did in the caller.  */
    6557          65 :   (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
    6558          65 :   const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
    6559             : 
    6560             :   /* Needed if we find PC-relative addresses.  */
    6561          65 :   GElf_Addr bias;
    6562          65 :   if (dwfl_module_getelf (dwflmod, &bias) == NULL)
    6563             :     {
    6564           0 :       error (0, 0, gettext ("cannot get ELF: %s"), dwfl_errmsg (-1));
    6565           0 :       return;
    6566             :     }
    6567             : 
    6568          65 :   bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
    6569         130 :   Elf_Data *data = (is_eh_frame
    6570          41 :                     ? elf_rawdata (scn, NULL)
    6571          65 :                     : (dbg->sectiondata[IDX_debug_frame]
    6572          24 :                        ?: elf_rawdata (scn, NULL)));
    6573             : 
    6574          65 :   if (unlikely (data == NULL))
    6575             :     {
    6576           0 :       error (0, 0, gettext ("cannot get %s content: %s"),
    6577             :              scnname, elf_errmsg (-1));
    6578           0 :       return;
    6579             :     }
    6580             : 
    6581          65 :   if (is_eh_frame)
    6582          41 :     printf (gettext ("\
    6583             : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    6584          41 :             elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
    6585             :   else
    6586          24 :     printf (gettext ("\
    6587             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    6588          24 :             elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
    6589             : 
    6590          65 :   struct cieinfo
    6591             :   {
    6592             :     ptrdiff_t cie_offset;
    6593             :     const char *augmentation;
    6594             :     unsigned int code_alignment_factor;
    6595             :     unsigned int data_alignment_factor;
    6596             :     uint8_t address_size;
    6597             :     uint8_t fde_encoding;
    6598             :     uint8_t lsda_encoding;
    6599             :     struct cieinfo *next;
    6600          65 :   } *cies = NULL;
    6601             : 
    6602          65 :   const unsigned char *readp = data->d_buf;
    6603         130 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    6604          65 :                                         + data->d_size);
    6605        9516 :   while (readp < dataend)
    6606             :     {
    6607        9451 :       if (unlikely (readp + 4 > dataend))
    6608             :         {
    6609           0 :         invalid_data:
    6610           0 :           error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
    6611             :                      elf_ndxscn (scn), scnname);
    6612           0 :               return;
    6613             :         }
    6614             : 
    6615             :       /* At the beginning there must be a CIE.  There can be multiple,
    6616             :          hence we test tis in a loop.  */
    6617        9451 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    6618             : 
    6619        9451 :       Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    6620        9451 :       unsigned int length = 4;
    6621        9451 :       if (unlikely (unit_length == 0xffffffff))
    6622             :         {
    6623           0 :           if (unlikely (readp + 8 > dataend))
    6624             :             goto invalid_data;
    6625             : 
    6626           0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    6627           0 :           length = 8;
    6628             :         }
    6629             : 
    6630        9451 :       if (unlikely (unit_length == 0))
    6631             :         {
    6632          29 :           printf (gettext ("\n [%6tx] Zero terminator\n"), offset);
    6633          29 :           continue;
    6634             :         }
    6635             : 
    6636        9422 :       Dwarf_Word maxsize = dataend - readp;
    6637        9422 :       if (unlikely (unit_length > maxsize))
    6638             :         goto invalid_data;
    6639             : 
    6640        9422 :       unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    6641             : 
    6642        9422 :       ptrdiff_t start = readp - (unsigned char *) data->d_buf;
    6643        9422 :       const unsigned char *const cieend = readp + unit_length;
    6644        9422 :       if (unlikely (cieend > dataend))
    6645             :         goto invalid_data;
    6646             : 
    6647        9422 :       Dwarf_Off cie_id;
    6648        9422 :       if (length == 4)
    6649             :         {
    6650        9422 :           if (unlikely (cieend - readp < 4))
    6651             :             goto invalid_data;
    6652        9422 :           cie_id = read_4ubyte_unaligned_inc (dbg, readp);
    6653        9422 :           if (!is_eh_frame && cie_id == DW_CIE_ID_32)
    6654          38 :             cie_id = DW_CIE_ID_64;
    6655             :         }
    6656             :       else
    6657             :         {
    6658           0 :           if (unlikely (cieend - readp < 8))
    6659             :             goto invalid_data;
    6660           0 :           cie_id = read_8ubyte_unaligned_inc (dbg, readp);
    6661             :         }
    6662             : 
    6663        9422 :       uint_fast8_t version = 2;
    6664        9422 :       unsigned int code_alignment_factor;
    6665        9422 :       int data_alignment_factor;
    6666        9422 :       unsigned int fde_encoding = 0;
    6667        9422 :       unsigned int lsda_encoding = 0;
    6668        9422 :       Dwarf_Word initial_location = 0;
    6669        9422 :       Dwarf_Word vma_base = 0;
    6670             : 
    6671        9502 :       if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
    6672             :         {
    6673         148 :           if (unlikely (cieend - readp < 2))
    6674             :             goto invalid_data;
    6675         148 :           version = *readp++;
    6676         148 :           const char *const augmentation = (const char *) readp;
    6677         148 :           readp = memchr (readp, '\0', cieend - readp);
    6678         148 :           if (unlikely (readp == NULL))
    6679             :             goto invalid_data;
    6680         148 :           ++readp;
    6681             : 
    6682         148 :           uint_fast8_t segment_size = 0;
    6683         148 :           if (version >= 4)
    6684             :             {
    6685           0 :               if (cieend - readp < 5)
    6686             :                 goto invalid_data;
    6687           0 :               ptr_size = *readp++;
    6688           0 :               segment_size = *readp++;
    6689             :             }
    6690             : 
    6691         148 :           if (cieend - readp < 1)
    6692             :             goto invalid_data;
    6693         148 :           get_uleb128 (code_alignment_factor, readp, cieend);
    6694         148 :           if (cieend - readp < 1)
    6695             :             goto invalid_data;
    6696         148 :           get_sleb128 (data_alignment_factor, readp, cieend);
    6697             : 
    6698             :           /* In some variant for unwind data there is another field.  */
    6699         148 :           if (strcmp (augmentation, "eh") == 0)
    6700           0 :             readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    6701             : 
    6702         148 :           unsigned int return_address_register;
    6703         148 :           if (cieend - readp < 1)
    6704             :             goto invalid_data;
    6705         148 :           if (unlikely (version == 1))
    6706         136 :             return_address_register = *readp++;
    6707             :           else
    6708          12 :             get_uleb128 (return_address_register, readp, cieend);
    6709             : 
    6710         148 :           printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
    6711             :                   "   CIE_id:                   %" PRIu64 "\n"
    6712             :                   "   version:                  %u\n"
    6713             :                   "   augmentation:             \"%s\"\n",
    6714             :                   offset, (uint64_t) unit_length, (uint64_t) cie_id,
    6715             :                   version, augmentation);
    6716         148 :           if (version >= 4)
    6717           0 :             printf ("   address_size:             %u\n"
    6718             :                     "   segment_size:             %u\n",
    6719             :                     ptr_size, segment_size);
    6720         148 :           printf ("   code_alignment_factor:    %u\n"
    6721             :                   "   data_alignment_factor:    %d\n"
    6722             :                   "   return_address_register:  %u\n",
    6723             :                   code_alignment_factor,
    6724             :                   data_alignment_factor, return_address_register);
    6725             : 
    6726         148 :           if (augmentation[0] == 'z')
    6727             :             {
    6728         108 :               unsigned int augmentationlen;
    6729         108 :               get_uleb128 (augmentationlen, readp, cieend);
    6730             : 
    6731         108 :               if (augmentationlen > (size_t) (cieend - readp))
    6732             :                 {
    6733           0 :                   error (0, 0, gettext ("invalid augmentation length"));
    6734           0 :                   readp = cieend;
    6735           0 :                   continue;
    6736             :                 }
    6737             : 
    6738         108 :               const char *hdr = "Augmentation data:";
    6739         108 :               const char *cp = augmentation + 1;
    6740         216 :               while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
    6741             :                 {
    6742         108 :                   printf ("   %-26s%#x ", hdr, *readp);
    6743         108 :                   hdr = "";
    6744             : 
    6745         108 :                   if (*cp == 'R')
    6746             :                     {
    6747         108 :                       fde_encoding = *readp++;
    6748         108 :                       print_encoding_base (gettext ("FDE address encoding: "),
    6749             :                                            fde_encoding);
    6750             :                     }
    6751           0 :                   else if (*cp == 'L')
    6752             :                     {
    6753           0 :                       lsda_encoding = *readp++;
    6754           0 :                       print_encoding_base (gettext ("LSDA pointer encoding: "),
    6755             :                                            lsda_encoding);
    6756             :                     }
    6757           0 :                   else if (*cp == 'P')
    6758             :                     {
    6759             :                       /* Personality.  This field usually has a relocation
    6760             :                          attached pointing to __gcc_personality_v0.  */
    6761           0 :                       const unsigned char *startp = readp;
    6762           0 :                       unsigned int encoding = *readp++;
    6763           0 :                       uint64_t val = 0;
    6764           0 :                       readp = read_encoded (encoding, readp,
    6765           0 :                                             readp - 1 + augmentationlen,
    6766             :                                             &val, dbg);
    6767             : 
    6768           0 :                       while (++startp < readp)
    6769           0 :                         printf ("%#x ", *startp);
    6770             : 
    6771           0 :                       putchar ('(');
    6772           0 :                       print_encoding (encoding);
    6773           0 :                       putchar (' ');
    6774           0 :                       switch (encoding & 0xf)
    6775             :                         {
    6776           0 :                         case DW_EH_PE_sleb128:
    6777             :                         case DW_EH_PE_sdata2:
    6778             :                         case DW_EH_PE_sdata4:
    6779           0 :                           printf ("%" PRId64 ")\n", val);
    6780             :                           break;
    6781           0 :                         default:
    6782           0 :                           printf ("%#" PRIx64 ")\n", val);
    6783             :                           break;
    6784             :                         }
    6785             :                     }
    6786             :                   else
    6787           0 :                     printf ("(%x)\n", *readp++);
    6788             : 
    6789         108 :                   ++cp;
    6790             :                 }
    6791             :             }
    6792             : 
    6793         148 :           if (likely (ptr_size == 4 || ptr_size == 8))
    6794             :             {
    6795         148 :               struct cieinfo *newp = alloca (sizeof (*newp));
    6796         148 :               newp->cie_offset = offset;
    6797         148 :               newp->augmentation = augmentation;
    6798         148 :               newp->fde_encoding = fde_encoding;
    6799         148 :               newp->lsda_encoding = lsda_encoding;
    6800         148 :               newp->address_size = ptr_size;
    6801         148 :               newp->code_alignment_factor = code_alignment_factor;
    6802         148 :               newp->data_alignment_factor = data_alignment_factor;
    6803         148 :               newp->next = cies;
    6804         148 :               cies = newp;
    6805             :             }
    6806             :         }
    6807             :       else
    6808             :         {
    6809             :           struct cieinfo *cie = cies;
    6810       36061 :           while (cie != NULL)
    6811       72122 :             if (is_eh_frame
    6812       36019 :                 ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
    6813          42 :                 : cie_id == (Dwarf_Off) cie->cie_offset)
    6814             :               break;
    6815             :             else
    6816       26787 :               cie = cie->next;
    6817        9274 :           if (unlikely (cie == NULL))
    6818             :             {
    6819           0 :               puts ("invalid CIE reference in FDE");
    6820           0 :               return;
    6821             :             }
    6822             : 
    6823             :           /* Initialize from CIE data.  */
    6824        9274 :           fde_encoding = cie->fde_encoding;
    6825        9274 :           lsda_encoding = cie->lsda_encoding;
    6826        9274 :           ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
    6827        9274 :           code_alignment_factor = cie->code_alignment_factor;
    6828        9274 :           data_alignment_factor = cie->data_alignment_factor;
    6829             : 
    6830        9274 :           const unsigned char *base = readp;
    6831             :           // XXX There are sometimes relocations for this value
    6832        9274 :           initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
    6833       18548 :           Dwarf_Word address_range
    6834        9274 :             = read_addr_unaligned_inc (ptr_size, dbg, readp);
    6835             : 
    6836             :           /* pcrel for an FDE address is relative to the runtime
    6837             :              address of the start_address field itself.  Sign extend
    6838             :              if necessary to make sure the calculation is done on the
    6839             :              full 64 bit address even when initial_location only holds
    6840             :              the lower 32 bits.  */
    6841        9274 :           Dwarf_Addr pc_start = initial_location;
    6842        9274 :           if (ptr_size == 4)
    6843        9234 :             pc_start = (uint64_t) (int32_t) pc_start;
    6844        9274 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    6845       18452 :             pc_start += ((uint64_t) shdr->sh_addr
    6846        9226 :                          + (base - (const unsigned char *) data->d_buf)
    6847        9226 :                          - bias);
    6848             : 
    6849        9274 :           printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
    6850             :                   "   CIE_pointer:              %" PRIu64 "\n"
    6851             :                   "   initial_location:         ",
    6852             :                   offset, (uint64_t) unit_length,
    6853             :                   cie->cie_offset, (uint64_t) cie_id);
    6854        9274 :           print_dwarf_addr (dwflmod, cie->address_size,
    6855             :                             pc_start, initial_location);
    6856        9274 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    6857             :             {
    6858       18452 :               vma_base = (((uint64_t) shdr->sh_offset
    6859        9226 :                            + (base - (const unsigned char *) data->d_buf)
    6860        9226 :                            + (uint64_t) initial_location)
    6861             :                           & (ptr_size == 4
    6862             :                              ? UINT64_C (0xffffffff)
    6863        9226 :                              : UINT64_C (0xffffffffffffffff)));
    6864        9226 :               printf (gettext (" (offset: %#" PRIx64 ")"),
    6865             :                       (uint64_t) vma_base);
    6866             :             }
    6867             : 
    6868        9274 :           printf ("\n   address_range:            %#" PRIx64,
    6869             :                   (uint64_t) address_range);
    6870        9274 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    6871        9226 :             printf (gettext (" (end offset: %#" PRIx64 ")"),
    6872        9226 :                     ((uint64_t) vma_base + (uint64_t) address_range)
    6873             :                     & (ptr_size == 4
    6874             :                        ? UINT64_C (0xffffffff)
    6875        9226 :                        : UINT64_C (0xffffffffffffffff)));
    6876        9274 :           putchar ('\n');
    6877             : 
    6878        9274 :           if (cie->augmentation[0] == 'z')
    6879             :             {
    6880        9226 :               unsigned int augmentationlen;
    6881        9226 :               if (cieend - readp < 1)
    6882             :                 goto invalid_data;
    6883        9226 :               get_uleb128 (augmentationlen, readp, cieend);
    6884             : 
    6885        9226 :               if (augmentationlen > (size_t) (cieend - readp))
    6886             :                 {
    6887           0 :                   error (0, 0, gettext ("invalid augmentation length"));
    6888           0 :                   readp = cieend;
    6889           0 :                   continue;
    6890             :                 }
    6891             : 
    6892        9226 :               if (augmentationlen > 0)
    6893             :                 {
    6894           0 :                   const char *hdr = "Augmentation data:";
    6895           0 :                   const char *cp = cie->augmentation + 1;
    6896           0 :                   unsigned int u = 0;
    6897           0 :                   while (*cp != '\0'
    6898           0 :                          && cp < cie->augmentation + augmentationlen + 1)
    6899             :                     {
    6900           0 :                       if (*cp == 'L')
    6901             :                         {
    6902           0 :                           uint64_t lsda_pointer;
    6903           0 :                           const unsigned char *p
    6904           0 :                             = read_encoded (lsda_encoding, &readp[u],
    6905             :                                             &readp[augmentationlen],
    6906             :                                             &lsda_pointer, dbg);
    6907           0 :                           u = p - readp;
    6908           0 :                           printf (gettext ("\
    6909             :    %-26sLSDA pointer: %#" PRIx64 "\n"),
    6910             :                                   hdr, lsda_pointer);
    6911           0 :                           hdr = "";
    6912             :                         }
    6913           0 :                       ++cp;
    6914             :                     }
    6915             : 
    6916           0 :                   while (u < augmentationlen)
    6917             :                     {
    6918           0 :                       printf ("   %-26s%#x\n", hdr, readp[u++]);
    6919           0 :                       hdr = "";
    6920             :                     }
    6921             :                 }
    6922             : 
    6923        9226 :               readp += augmentationlen;
    6924             :             }
    6925             :         }
    6926             : 
    6927             :       /* Handle the initialization instructions.  */
    6928        9422 :       if (ptr_size != 4 && ptr_size !=8)
    6929           0 :         printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
    6930             :       else
    6931        9422 :         print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
    6932             :                            data_alignment_factor, version, ptr_size,
    6933             :                            fde_encoding, dwflmod, ebl, dbg);
    6934        9422 :       readp = cieend;
    6935             :     }
    6936             : }
    6937             : 
    6938             : 
    6939             : /* Returns the signedness (or false if it cannot be determined) and
    6940             :    the byte size (or zero if it cannot be gotten) of the given DIE
    6941             :    DW_AT_type attribute.  Uses dwarf_peel_type and dwarf_aggregate_size.  */
    6942             : static void
    6943      160722 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
    6944             : {
    6945      160722 :   Dwarf_Attribute attr;
    6946      160722 :   Dwarf_Die type;
    6947             : 
    6948      160722 :   *bytes = 0;
    6949      160722 :   *is_signed = false;
    6950             : 
    6951      160722 :   if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
    6952             :                                                                 DW_AT_type,
    6953             :                                                                 &attr), &type),
    6954             :                        &type) == 0)
    6955             :     {
    6956         217 :       Dwarf_Word val;
    6957         217 :       *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
    6958             :                                                  &attr), &val) == 0
    6959         217 :                     && (val == DW_ATE_signed || val == DW_ATE_signed_char));
    6960             : 
    6961         217 :       if (dwarf_aggregate_size (&type, &val) == 0)
    6962         217 :         *bytes = val;
    6963             :     }
    6964      160722 : }
    6965             : 
    6966             : struct attrcb_args
    6967             : {
    6968             :   Dwfl_Module *dwflmod;
    6969             :   Dwarf *dbg;
    6970             :   Dwarf_Die *dies;
    6971             :   int level;
    6972             :   bool silent;
    6973             :   bool is_split;
    6974             :   unsigned int version;
    6975             :   unsigned int addrsize;
    6976             :   unsigned int offset_size;
    6977             :   struct Dwarf_CU *cu;
    6978             : };
    6979             : 
    6980             : 
    6981             : static int
    6982     4003323 : attr_callback (Dwarf_Attribute *attrp, void *arg)
    6983             : {
    6984     4003323 :   struct attrcb_args *cbargs = (struct attrcb_args *) arg;
    6985     4003323 :   const int level = cbargs->level;
    6986     4003323 :   Dwarf_Die *die = &cbargs->dies[level];
    6987     4003323 :   bool is_split = cbargs->is_split;
    6988             : 
    6989     4003323 :   unsigned int attr = dwarf_whatattr (attrp);
    6990     4003323 :   if (unlikely (attr == 0))
    6991             :     {
    6992           0 :       if (!cbargs->silent)
    6993           0 :         error (0, 0, gettext ("DIE [%" PRIx64 "] "
    6994             :                               "cannot get attribute code: %s"),
    6995             :                dwarf_dieoffset (die), dwarf_errmsg (-1));
    6996           0 :       return DWARF_CB_ABORT;
    6997             :     }
    6998             : 
    6999     4003323 :   unsigned int form = dwarf_whatform (attrp);
    7000     4003323 :   if (unlikely (form == 0))
    7001             :     {
    7002           0 :       if (!cbargs->silent)
    7003           0 :         error (0, 0, gettext ("DIE [%" PRIx64 "] "
    7004             :                               "cannot get attribute form: %s"),
    7005             :                dwarf_dieoffset (die), dwarf_errmsg (-1));
    7006           0 :       return DWARF_CB_ABORT;
    7007             :     }
    7008             : 
    7009     4003323 :   switch (form)
    7010             :     {
    7011       50686 :     case DW_FORM_addr:
    7012             :     case DW_FORM_addrx:
    7013             :     case DW_FORM_addrx1:
    7014             :     case DW_FORM_addrx2:
    7015             :     case DW_FORM_addrx3:
    7016             :     case DW_FORM_addrx4:
    7017             :     case DW_FORM_GNU_addr_index:
    7018       50686 :       if (!cbargs->silent)
    7019             :         {
    7020       50417 :           Dwarf_Addr addr;
    7021       50417 :           if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
    7022             :             {
    7023           0 :             attrval_out:
    7024           0 :               if (!cbargs->silent)
    7025           0 :                 error (0, 0, gettext ("DIE [%" PRIx64 "] "
    7026             :                                       "cannot get attribute '%s' (%s) value: "
    7027             :                                       "%s"),
    7028             :                        dwarf_dieoffset (die),
    7029             :                        dwarf_attr_name (attr),
    7030             :                        dwarf_form_name (form),
    7031             :                        dwarf_errmsg (-1));
    7032             :               /* Don't ABORT, it might be other attributes can be resolved.  */
    7033           0 :               return DWARF_CB_OK;
    7034             :             }
    7035       50417 :           if (form != DW_FORM_addr )
    7036             :             {
    7037           8 :               Dwarf_Word word;
    7038           8 :               if (dwarf_formudata (attrp, &word) != 0)
    7039           0 :                 goto attrval_out;
    7040           8 :               printf ("           %*s%-20s (%s) [%" PRIx64 "] ",
    7041             :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7042             :                       dwarf_form_name (form), word);
    7043             :             }
    7044             :           else
    7045       50409 :             printf ("           %*s%-20s (%s) ",
    7046             :                     (int) (level * 2), "", dwarf_attr_name (attr),
    7047             :                     dwarf_form_name (form));
    7048       50417 :           print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
    7049       50417 :           printf ("\n");
    7050             :         }
    7051     3601588 :       break;
    7052             : 
    7053      666826 :     case DW_FORM_indirect:
    7054             :     case DW_FORM_strp:
    7055             :     case DW_FORM_line_strp:
    7056             :     case DW_FORM_strx:
    7057             :     case DW_FORM_strx1:
    7058             :     case DW_FORM_strx2:
    7059             :     case DW_FORM_strx3:
    7060             :     case DW_FORM_strx4:
    7061             :     case DW_FORM_string:
    7062             :     case DW_FORM_GNU_strp_alt:
    7063             :     case DW_FORM_GNU_str_index:
    7064      666826 :       if (cbargs->silent)
    7065             :         break;
    7066      666078 :       const char *str = dwarf_formstring (attrp);
    7067      666078 :       if (unlikely (str == NULL))
    7068             :         goto attrval_out;
    7069      666078 :       printf ("           %*s%-20s (%s) \"%s\"\n",
    7070             :               (int) (level * 2), "", dwarf_attr_name (attr),
    7071             :               dwarf_form_name (form), str);
    7072             :       break;
    7073             : 
    7074      842459 :     case DW_FORM_ref_addr:
    7075             :     case DW_FORM_ref_udata:
    7076             :     case DW_FORM_ref8:
    7077             :     case DW_FORM_ref4:
    7078             :     case DW_FORM_ref2:
    7079             :     case DW_FORM_ref1:
    7080             :     case DW_FORM_GNU_ref_alt:
    7081             :     case DW_FORM_ref_sup4:
    7082             :     case DW_FORM_ref_sup8:
    7083      842459 :       if (cbargs->silent)
    7084             :         break;
    7085      841672 :       Dwarf_Die ref;
    7086      841672 :       if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
    7087             :         goto attrval_out;
    7088             : 
    7089      841672 :       printf ("           %*s%-20s (%s) ",
    7090             :               (int) (level * 2), "", dwarf_attr_name (attr),
    7091             :               dwarf_form_name (form));
    7092      841672 :       if (is_split)
    7093          33 :         printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
    7094             :       else
    7095      841639 :         printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
    7096             :       break;
    7097             : 
    7098           4 :     case DW_FORM_ref_sig8:
    7099           4 :       if (cbargs->silent)
    7100             :         break;
    7101           4 :       printf ("           %*s%-20s (%s) {%6" PRIx64 "}\n",
    7102             :               (int) (level * 2), "", dwarf_attr_name (attr),
    7103             :               dwarf_form_name (form),
    7104           4 :               (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
    7105             :       break;
    7106             : 
    7107     2238262 :     case DW_FORM_sec_offset:
    7108             :     case DW_FORM_rnglistx:
    7109             :     case DW_FORM_loclistx:
    7110             :     case DW_FORM_implicit_const:
    7111             :     case DW_FORM_udata:
    7112             :     case DW_FORM_sdata:
    7113             :     case DW_FORM_data8: /* Note no data16 here, we see that as block. */
    7114             :     case DW_FORM_data4:
    7115             :     case DW_FORM_data2:
    7116     2238262 :     case DW_FORM_data1:;
    7117     2238262 :       Dwarf_Word num;
    7118     2238262 :       if (unlikely (dwarf_formudata (attrp, &num) != 0))
    7119             :         goto attrval_out;
    7120             : 
    7121     2238262 :       const char *valuestr = NULL;
    7122     2238262 :       bool as_hex_id = false;
    7123     2238262 :       switch (attr)
    7124             :         {
    7125             :           /* This case can take either a constant or a loclistptr.  */
    7126      279400 :         case DW_AT_data_member_location:
    7127      279400 :           if (form != DW_FORM_sec_offset
    7128      279400 :               && (cbargs->version >= 4
    7129       23806 :                   || (form != DW_FORM_data4 && form != DW_FORM_data8)))
    7130             :             {
    7131      279400 :               if (!cbargs->silent)
    7132      279400 :                 printf ("           %*s%-20s (%s) %" PRIuMAX "\n",
    7133             :                         (int) (level * 2), "", dwarf_attr_name (attr),
    7134             :                         dwarf_form_name (form), (uintmax_t) num);
    7135      279400 :               return DWARF_CB_OK;
    7136             :             }
    7137      106933 :           FALLTHROUGH;
    7138             : 
    7139             :         /* These cases always take a loclist[ptr] and no constant. */
    7140             :         case DW_AT_location:
    7141             :         case DW_AT_data_location:
    7142             :         case DW_AT_vtable_elem_location:
    7143             :         case DW_AT_string_length:
    7144             :         case DW_AT_use_location:
    7145             :         case DW_AT_frame_base:
    7146             :         case DW_AT_return_addr:
    7147             :         case DW_AT_static_link:
    7148             :         case DW_AT_segment:
    7149             :         case DW_AT_GNU_call_site_value:
    7150             :         case DW_AT_GNU_call_site_data_value:
    7151             :         case DW_AT_GNU_call_site_target:
    7152             :         case DW_AT_GNU_call_site_target_clobbered:
    7153             :         case DW_AT_GNU_locviews:
    7154             :           {
    7155      106933 :             bool nlpt;
    7156      106933 :             if (cbargs->cu->version < 5)
    7157             :               {
    7158      106862 :                 if (! cbargs->is_split)
    7159             :                   {
    7160      320526 :                     nlpt = notice_listptr (section_loc, &known_locsptr,
    7161      106842 :                                            cbargs->addrsize,
    7162      106842 :                                            cbargs->offset_size,
    7163             :                                            cbargs->cu, num, attr);
    7164             :                   }
    7165             :                 else
    7166             :                   nlpt = true;
    7167             :               }
    7168             :             else
    7169             :               {
    7170             :                 /* Only register for a real section offset.  Otherwise
    7171             :                    it is a DW_FORM_loclistx which is just an index
    7172             :                    number and we should already have registered the
    7173             :                    section offset for the index when we saw the
    7174             :                    DW_AT_loclists_base CU attribute.  */
    7175          71 :                 if (form == DW_FORM_sec_offset)
    7176          96 :                   nlpt = notice_listptr (section_loc, &known_loclistsptr,
    7177          32 :                                          cbargs->addrsize, cbargs->offset_size,
    7178             :                                          cbargs->cu, num, attr);
    7179             :                 else
    7180             :                   nlpt = true;
    7181             : 
    7182             :               }
    7183             : 
    7184      106933 :             if (!cbargs->silent)
    7185             :               {
    7186      106752 :                 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
    7187      106745 :                   printf ("           %*s%-20s (%s) location list [%6"
    7188             :                           PRIxMAX "]%s\n",
    7189             :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7190             :                           dwarf_form_name (form), (uintmax_t) num,
    7191             :                           nlpt ? "" : " <WARNING offset too big>");
    7192             :                 else
    7193           7 :                   printf ("           %*s%-20s (%s) location index [%6"
    7194             :                           PRIxMAX "]\n",
    7195             :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7196             :                           dwarf_form_name (form), (uintmax_t) num);
    7197             :               }
    7198             :           }
    7199             :           return DWARF_CB_OK;
    7200             : 
    7201           5 :         case DW_AT_loclists_base:
    7202           4 :           {
    7203          20 :             bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
    7204           5 :                                         cbargs->addrsize, cbargs->offset_size,
    7205             :                                         cbargs->cu, num, attr);
    7206             : 
    7207           5 :             if (!cbargs->silent)
    7208           1 :               printf ("           %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
    7209             :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7210             :                       dwarf_form_name (form), (uintmax_t) num,
    7211             :                       nlpt ? "" : " <WARNING offset too big>");
    7212             :           }
    7213             :           return DWARF_CB_OK;
    7214             : 
    7215       15380 :         case DW_AT_ranges:
    7216             :         case DW_AT_start_scope:
    7217             :           {
    7218       15380 :             bool nlpt;
    7219       15380 :             if (cbargs->cu->version < 5)
    7220       46092 :               nlpt = notice_listptr (section_ranges, &known_rangelistptr,
    7221       15364 :                                      cbargs->addrsize, cbargs->offset_size,
    7222             :                                      cbargs->cu, num, attr);
    7223             :             else
    7224             :               {
    7225             :                 /* Only register for a real section offset.  Otherwise
    7226             :                    it is a DW_FORM_rangelistx which is just an index
    7227             :                    number and we should already have registered the
    7228             :                    section offset for the index when we saw the
    7229             :                    DW_AT_rnglists_base CU attribute.  */
    7230          16 :                 if (form == DW_FORM_sec_offset)
    7231          30 :                   nlpt = notice_listptr (section_ranges, &known_rnglistptr,
    7232          10 :                                          cbargs->addrsize, cbargs->offset_size,
    7233             :                                          cbargs->cu, num, attr);
    7234             :                 else
    7235             :                   nlpt = true;
    7236             :               }
    7237             : 
    7238       15380 :             if (!cbargs->silent)
    7239             :               {
    7240       15338 :                 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
    7241       15336 :                   printf ("           %*s%-20s (%s) range list [%6"
    7242             :                           PRIxMAX "]%s\n",
    7243             :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7244             :                           dwarf_form_name (form), (uintmax_t) num,
    7245             :                           nlpt ? "" : " <WARNING offset too big>");
    7246             :                 else
    7247           2 :                   printf ("           %*s%-20s (%s) range index [%6"
    7248             :                           PRIxMAX "]\n",
    7249             :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7250             :                           dwarf_form_name (form), (uintmax_t) num);
    7251             :               }
    7252             :           }
    7253             :           return DWARF_CB_OK;
    7254             : 
    7255           4 :         case DW_AT_rnglists_base:
    7256           2 :           {
    7257          16 :             bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
    7258           4 :                                         cbargs->addrsize, cbargs->offset_size,
    7259             :                                         cbargs->cu, num, attr);
    7260           4 :             if (!cbargs->silent)
    7261           2 :               printf ("           %*s%-20s (%s) range list [%6"
    7262             :                       PRIxMAX "]%s\n",
    7263             :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7264             :                       dwarf_form_name (form), (uintmax_t) num,
    7265             :                       nlpt ? "" : " <WARNING offset too big>");
    7266             :           }
    7267             :           return DWARF_CB_OK;
    7268             : 
    7269          13 :         case DW_AT_addr_base:
    7270             :         case DW_AT_GNU_addr_base:
    7271           8 :           {
    7272          52 :             bool addrbase = notice_listptr (section_addr, &known_addrbases,
    7273          13 :                                             cbargs->addrsize,
    7274          13 :                                             cbargs->offset_size,
    7275             :                                             cbargs->cu, num, attr);
    7276          13 :             if (!cbargs->silent)
    7277           5 :               printf ("           %*s%-20s (%s) address base [%6"
    7278             :                       PRIxMAX "]%s\n",
    7279             :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7280             :                       dwarf_form_name (form), (uintmax_t) num,
    7281             :                       addrbase ? "" : " <WARNING offset too big>");
    7282             :           }
    7283             :           return DWARF_CB_OK;
    7284             : 
    7285           0 :         case DW_AT_str_offsets_base:
    7286           0 :           {
    7287           0 :             bool stroffbase = notice_listptr (section_str, &known_stroffbases,
    7288           0 :                                               cbargs->addrsize,
    7289           0 :                                               cbargs->offset_size,
    7290             :                                               cbargs->cu, num, attr);
    7291           0 :             if (!cbargs->silent)
    7292           0 :               printf ("           %*s%-20s (%s) str offsets base [%6"
    7293             :                       PRIxMAX "]%s\n",
    7294             :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7295             :                       dwarf_form_name (form), (uintmax_t) num,
    7296             :                       stroffbase ? "" : " <WARNING offset too big>");
    7297             :           }
    7298             :           return DWARF_CB_OK;
    7299             : 
    7300        1735 :         case DW_AT_language:
    7301        1735 :           valuestr = dwarf_lang_name (num);
    7302        1735 :           break;
    7303       30450 :         case DW_AT_encoding:
    7304       30450 :           valuestr = dwarf_encoding_name (num);
    7305       30450 :           break;
    7306           0 :         case DW_AT_accessibility:
    7307           0 :           valuestr = dwarf_access_name (num);
    7308           0 :           break;
    7309           0 :         case DW_AT_defaulted:
    7310           0 :           valuestr = dwarf_defaulted_name (num);
    7311           0 :           break;
    7312           0 :         case DW_AT_visibility:
    7313           0 :           valuestr = dwarf_visibility_name (num);
    7314           0 :           break;
    7315           0 :         case DW_AT_virtuality:
    7316           0 :           valuestr = dwarf_virtuality_name (num);
    7317           0 :           break;
    7318           0 :         case DW_AT_identifier_case:
    7319           0 :           valuestr = dwarf_identifier_case_name (num);
    7320           0 :           break;
    7321           0 :         case DW_AT_calling_convention:
    7322           0 :           valuestr = dwarf_calling_convention_name (num);
    7323           0 :           break;
    7324        3458 :         case DW_AT_inline:
    7325        3458 :           valuestr = dwarf_inline_name (num);
    7326        3458 :           break;
    7327           0 :         case DW_AT_ordering:
    7328           0 :           valuestr = dwarf_ordering_name (num);
    7329           0 :           break;
    7330      494605 :         case DW_AT_decl_file:
    7331             :         case DW_AT_call_file:
    7332         421 :           {
    7333      494605 :             if (cbargs->silent)
    7334             :               break;
    7335             : 
    7336             :             /* Try to get the actual file, the current interface only
    7337             :                gives us full paths, but we only want to show the file
    7338             :                name for now.  */
    7339      494184 :             Dwarf_Die cudie;
    7340      494184 :             if (dwarf_cu_die (cbargs->cu, &cudie,
    7341             :                               NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
    7342             :               {
    7343      494184 :                 Dwarf_Files *files;
    7344      494184 :                 size_t nfiles;
    7345      494184 :                 if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
    7346             :                   {
    7347      494184 :                     valuestr = dwarf_filesrc (files, num, NULL, NULL);
    7348      494184 :                     if (valuestr != NULL)
    7349             :                       {
    7350      494184 :                         char *filename = strrchr (valuestr, '/');
    7351      494184 :                         if (filename != NULL)
    7352      494184 :                           valuestr = filename + 1;
    7353             :                       }
    7354             :                     else
    7355           0 :                       error (0, 0, gettext ("invalid file (%" PRId64 "): %s"),
    7356             :                              num, dwarf_errmsg (-1));
    7357             :                   }
    7358             :                 else
    7359           0 :                   error (0, 0, gettext ("no srcfiles for CU [%" PRIx64 "]"),
    7360             :                          dwarf_dieoffset (&cudie));
    7361             :               }
    7362             :             else
    7363           0 :              error (0, 0, gettext ("couldn't get DWARF CU: %s"),
    7364             :                     dwarf_errmsg (-1));
    7365      494184 :             if (valuestr == NULL)
    7366             :               valuestr = "???";
    7367             :           }
    7368      494184 :           break;
    7369          13 :         case DW_AT_GNU_dwo_id:
    7370          13 :           as_hex_id = true;
    7371          13 :           break;
    7372             : 
    7373             :         default:
    7374             :           /* Nothing.  */
    7375             :           break;
    7376             :         }
    7377             : 
    7378     1836527 :       if (cbargs->silent)
    7379             :         break;
    7380             : 
    7381             :       /* When highpc is in constant form it is relative to lowpc.
    7382             :          In that case also show the address.  */
    7383     1834686 :       Dwarf_Addr highpc;
    7384     1834686 :       if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
    7385             :         {
    7386       14220 :           printf ("           %*s%-20s (%s) %" PRIuMAX " (",
    7387             :                   (int) (level * 2), "", dwarf_attr_name (attr),
    7388             :                   dwarf_form_name (form), (uintmax_t) num);
    7389       14220 :           print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
    7390       14220 :           printf (")\n");
    7391             :         }
    7392             :       else
    7393             :         {
    7394     1820466 :           if (as_hex_id)
    7395             :             {
    7396           2 :               printf ("           %*s%-20s (%s) 0x%.16" PRIx64 "\n",
    7397             :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7398             :                       dwarf_form_name (form), num);
    7399             :             }
    7400             :           else
    7401     1820464 :             {
    7402     1820464 :               Dwarf_Sword snum = 0;
    7403     1820464 :               bool is_signed;
    7404     1820464 :               int bytes = 0;
    7405     1820464 :               if (attr == DW_AT_const_value)
    7406      160718 :                 die_type_sign_bytes (die, &is_signed, &bytes);
    7407             :               else
    7408     3319492 :                 is_signed = (form == DW_FORM_sdata
    7409     1659746 :                              || form == DW_FORM_implicit_const);
    7410             : 
    7411     1820464 :               if (is_signed)
    7412         144 :                 if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
    7413           0 :                   goto attrval_out;
    7414             : 
    7415     1820464 :               if (valuestr == NULL)
    7416             :                 {
    7417     1290940 :                   printf ("           %*s%-20s (%s) ",
    7418             :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7419             :                           dwarf_form_name (form));
    7420             :                 }
    7421             :               else
    7422             :                 {
    7423      529524 :                   printf ("           %*s%-20s (%s) %s (",
    7424             :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7425             :                           dwarf_form_name (form), valuestr);
    7426             :                 }
    7427             : 
    7428     1820464 :               switch (bytes)
    7429             :                 {
    7430           2 :                 case 1:
    7431           2 :                   if (is_signed)
    7432           1 :                     printf ("%" PRId8, (int8_t) snum);
    7433             :                   else
    7434           1 :                     printf ("%" PRIu8, (uint8_t) num);
    7435             :                   break;
    7436             : 
    7437           2 :                 case 2:
    7438           2 :                   if (is_signed)
    7439           1 :                     printf ("%" PRId16, (int16_t) snum);
    7440             :                   else
    7441           1 :                     printf ("%" PRIu16, (uint16_t) num);
    7442             :                   break;
    7443             : 
    7444         121 :                 case 4:
    7445         121 :                   if (is_signed)
    7446         120 :                     printf ("%" PRId32, (int32_t) snum);
    7447             :                   else
    7448           1 :                     printf ("%" PRIu32, (uint32_t) num);
    7449             :                   break;
    7450             : 
    7451          88 :                 case 8:
    7452          88 :                   if (is_signed)
    7453           1 :                     printf ("%" PRId64, (int64_t) snum);
    7454             :                   else
    7455          87 :                     printf ("%" PRIu64, (uint64_t) num);
    7456             :                   break;
    7457             : 
    7458     1820251 :                 default:
    7459     1820251 :                   if (is_signed)
    7460          21 :                     printf ("%" PRIdMAX, (intmax_t) snum);
    7461             :                   else
    7462     1820230 :                     printf ("%" PRIuMAX, (uintmax_t) num);
    7463             :                   break;
    7464             :                 }
    7465             : 
    7466             :               /* Make clear if we switched from a signed encoding to
    7467             :                  an unsigned value.  */
    7468     1820464 :               if (attr == DW_AT_const_value
    7469      160718 :                   && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
    7470        1218 :                   && !is_signed)
    7471        1212 :                 printf (" (%" PRIdMAX ")", (intmax_t) num);
    7472             : 
    7473     1820464 :               if (valuestr == NULL)
    7474     1290940 :                 printf ("\n");
    7475             :               else
    7476      529524 :                 printf (")\n");
    7477             :             }
    7478             :         }
    7479             :       break;
    7480             : 
    7481        9122 :     case DW_FORM_flag:
    7482        9122 :       if (cbargs->silent)
    7483             :         break;
    7484        9048 :       bool flag;
    7485        9048 :       if (unlikely (dwarf_formflag (attrp, &flag) != 0))
    7486             :         goto attrval_out;
    7487             : 
    7488        9048 :       printf ("           %*s%-20s (%s) %s\n",
    7489             :               (int) (level * 2), "", dwarf_attr_name (attr),
    7490        9048 :               dwarf_form_name (form), flag ? yes_str : no_str);
    7491             :       break;
    7492             : 
    7493       97819 :     case DW_FORM_flag_present:
    7494       97819 :       if (cbargs->silent)
    7495             :         break;
    7496       97545 :       printf ("           %*s%-20s (%s) %s\n",
    7497             :               (int) (level * 2), "", dwarf_attr_name (attr),
    7498             :               dwarf_form_name (form), yes_str);
    7499             :       break;
    7500             : 
    7501       98145 :     case DW_FORM_exprloc:
    7502             :     case DW_FORM_block4:
    7503             :     case DW_FORM_block2:
    7504             :     case DW_FORM_block1:
    7505             :     case DW_FORM_block:
    7506             :     case DW_FORM_data16: /* DWARF5 calls this a constant class.  */
    7507       98145 :       if (cbargs->silent)
    7508             :         break;
    7509       98016 :       Dwarf_Block block;
    7510       98016 :       if (unlikely (dwarf_formblock (attrp, &block) != 0))
    7511             :         goto attrval_out;
    7512             : 
    7513       98016 :       printf ("           %*s%-20s (%s) ",
    7514             :               (int) (level * 2), "", dwarf_attr_name (attr),
    7515             :               dwarf_form_name (form));
    7516             : 
    7517       98016 :       switch (attr)
    7518             :         {
    7519          10 :         default:
    7520          10 :           if (form != DW_FORM_exprloc)
    7521             :             {
    7522          10 :               print_block (block.length, block.data);
    7523          10 :               break;
    7524             :             }
    7525       98002 :           FALLTHROUGH;
    7526             : 
    7527             :         case DW_AT_location:
    7528             :         case DW_AT_data_location:
    7529             :         case DW_AT_data_member_location:
    7530             :         case DW_AT_vtable_elem_location:
    7531             :         case DW_AT_string_length:
    7532             :         case DW_AT_use_location:
    7533             :         case DW_AT_frame_base:
    7534             :         case DW_AT_return_addr:
    7535             :         case DW_AT_static_link:
    7536             :         case DW_AT_allocated:
    7537             :         case DW_AT_associated:
    7538             :         case DW_AT_bit_size:
    7539             :         case DW_AT_bit_offset:
    7540             :         case DW_AT_bit_stride:
    7541             :         case DW_AT_byte_size:
    7542             :         case DW_AT_byte_stride:
    7543             :         case DW_AT_count:
    7544             :         case DW_AT_lower_bound:
    7545             :         case DW_AT_upper_bound:
    7546             :         case DW_AT_GNU_call_site_value:
    7547             :         case DW_AT_GNU_call_site_data_value:
    7548             :         case DW_AT_GNU_call_site_target:
    7549             :         case DW_AT_GNU_call_site_target_clobbered:
    7550       98002 :           if (form != DW_FORM_data16)
    7551             :             {
    7552       98002 :               putchar ('\n');
    7553      294006 :               print_ops (cbargs->dwflmod, cbargs->dbg,
    7554       98002 :                          12 + level * 2, 12 + level * 2,
    7555             :                          cbargs->version, cbargs->addrsize, cbargs->offset_size,
    7556       98002 :                          attrp->cu, block.length, block.data);
    7557             :             }
    7558             :           else
    7559           0 :             print_block (block.length, block.data);
    7560             :           break;
    7561             : 
    7562           4 :         case DW_AT_discr_list:
    7563           4 :           if (block.length == 0)
    7564           0 :             puts ("<default>");
    7565           4 :           else if (form != DW_FORM_data16)
    7566             :             {
    7567           4 :               const unsigned char *readp = block.data;
    7568           4 :               const unsigned char *readendp = readp + block.length;
    7569             : 
    7570             :               /* See if we are dealing with a signed or unsigned
    7571             :                  values.  If the parent of this variant DIE is a
    7572             :                  variant_part then it will either have a discriminant
    7573             :                  which points to the member which type is the
    7574             :                  discriminant type.  Or the variant_part itself has a
    7575             :                  type representing the discriminant.  */
    7576           4 :               bool is_signed = false;
    7577           4 :               if (level > 0)
    7578             :                 {
    7579           4 :                   Dwarf_Die *parent = &cbargs->dies[level - 1];
    7580           4 :                   if (dwarf_tag (die) == DW_TAG_variant
    7581           4 :                       && dwarf_tag (parent) == DW_TAG_variant_part)
    7582             :                     {
    7583           4 :                       Dwarf_Die member;
    7584           4 :                       Dwarf_Attribute discr_attr;
    7585           4 :                       int bytes;
    7586           4 :                       if (dwarf_formref_die (dwarf_attr (parent,
    7587             :                                                          DW_AT_discr,
    7588             :                                                          &discr_attr),
    7589             :                                              &member) != NULL)
    7590           4 :                         die_type_sign_bytes (&member, &is_signed, &bytes);
    7591             :                       else
    7592           0 :                         die_type_sign_bytes (parent, &is_signed, &bytes);
    7593             :                     }
    7594             :                 }
    7595          12 :               while (readp < readendp)
    7596             :                 {
    7597           8 :                   int d = (int) *readp++;
    7598          16 :                   printf ("%s ", dwarf_discr_list_name (d));
    7599           8 :                   if (readp >= readendp)
    7600           0 :                     goto attrval_out;
    7601             : 
    7602           8 :                   Dwarf_Word val;
    7603           8 :                   Dwarf_Sword sval;
    7604           8 :                   if (d == DW_DSC_label)
    7605             :                     {
    7606           4 :                       if (is_signed)
    7607             :                         {
    7608           2 :                           get_sleb128 (sval, readp, readendp);
    7609           2 :                           printf ("%" PRId64 "", sval);
    7610             :                         }
    7611             :                       else
    7612             :                         {
    7613           2 :                           get_uleb128 (val, readp, readendp);
    7614           2 :                           printf ("%" PRIu64 "", val);
    7615             :                         }
    7616             :                     }
    7617           4 :                   else if (d == DW_DSC_range)
    7618             :                     {
    7619           4 :                       if (is_signed)
    7620             :                         {
    7621           3 :                           get_sleb128 (sval, readp, readendp);
    7622           3 :                           printf ("%" PRId64 "..", sval);
    7623           3 :                           if (readp >= readendp)
    7624             :                             goto attrval_out;
    7625           3 :                           get_sleb128 (sval, readp, readendp);
    7626           3 :                           printf ("%" PRId64 "", sval);
    7627             :                         }
    7628             :                       else
    7629             :                         {
    7630           1 :                           get_uleb128 (val, readp, readendp);
    7631           1 :                           printf ("%" PRIu64 "..", val);
    7632           1 :                           if (readp >= readendp)
    7633             :                             goto attrval_out;
    7634           1 :                           get_uleb128 (val, readp, readendp);
    7635           1 :                           printf ("%" PRIu64 "", val);
    7636             :                         }
    7637             :                     }
    7638             :                   else
    7639             :                     {
    7640           0 :                       print_block (readendp - readp, readp);
    7641           0 :                       break;
    7642             :                     }
    7643           8 :                   if (readp < readendp)
    7644           4 :                     printf (", ");
    7645             :                 }
    7646           4 :               putchar ('\n');
    7647             :             }
    7648             :           else
    7649           0 :             print_block (block.length, block.data);
    7650             :           break;
    7651             :         }
    7652             :       break;
    7653             : 
    7654           0 :     default:
    7655           0 :       if (cbargs->silent)
    7656             :         break;
    7657           0 :       printf ("           %*s%-20s (%s) ???\n",
    7658             :               (int) (level * 2), "", dwarf_attr_name (attr),
    7659             :               dwarf_form_name (form));
    7660             :       break;
    7661             :     }
    7662             : 
    7663     3601588 :   return DWARF_CB_OK;
    7664             : }
    7665             : 
    7666             : static void
    7667          97 : print_debug_units (Dwfl_Module *dwflmod,
    7668             :                    Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
    7669             :                    Elf_Scn *scn, GElf_Shdr *shdr,
    7670             :                    Dwarf *dbg, bool debug_types)
    7671             : {
    7672          97 :   const bool silent = !(print_debug_sections & section_info) && !debug_types;
    7673          97 :   const char *secname = section_name (ebl, shdr);
    7674             : 
    7675          97 :   if (!silent)
    7676          62 :     printf (gettext ("\
    7677             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
    7678          62 :             elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
    7679             : 
    7680             :   /* If the section is empty we don't have to do anything.  */
    7681          97 :   if (!silent && shdr->sh_size == 0)
    7682           0 :     return;
    7683             : 
    7684          97 :   int maxdies = 20;
    7685          97 :   Dwarf_Die *dies = (Dwarf_Die *) xmalloc (maxdies * sizeof (Dwarf_Die));
    7686             : 
    7687             :   /* New compilation unit.  */
    7688          97 :   Dwarf_Half version;
    7689             : 
    7690          97 :   Dwarf_Die result;
    7691          97 :   Dwarf_Off abbroffset;
    7692          97 :   uint8_t addrsize;
    7693          97 :   uint8_t offsize;
    7694          97 :   uint64_t unit_id;
    7695          97 :   Dwarf_Off subdie_off;
    7696             : 
    7697          97 :   int unit_res;
    7698          97 :   Dwarf_CU *cu;
    7699          97 :   Dwarf_CU cu_mem;
    7700          97 :   uint8_t unit_type;
    7701          97 :   Dwarf_Die cudie;
    7702             : 
    7703             :   /* We cheat a little because we want to see only the CUs from .debug_info
    7704             :      or .debug_types.  We know the Dwarf_CU struct layout.  Set it up at
    7705             :      the end of .debug_info if we want .debug_types only.  Check the returned
    7706             :      Dwarf_CU is still in the expected section.  */
    7707          97 :   if (debug_types)
    7708             :     {
    7709           1 :       cu_mem.dbg = dbg;
    7710           1 :       cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
    7711           1 :       cu_mem.sec_idx = IDX_debug_info;
    7712           1 :       cu = &cu_mem;
    7713             :     }
    7714             :   else
    7715          96 :     cu = NULL;
    7716             : 
    7717             :  next_cu:
    7718        1841 :   unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
    7719             :                               &cudie, NULL);
    7720        1841 :   if (unit_res == 1)
    7721          96 :     goto do_return;
    7722             : 
    7723        1745 :   if (unit_res == -1)
    7724             :     {
    7725           0 :       if (!silent)
    7726           0 :         error (0, 0, gettext ("cannot get next unit: %s"), dwarf_errmsg (-1));
    7727           0 :       goto do_return;
    7728             :     }
    7729             : 
    7730        3488 :   if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
    7731           1 :     goto do_return;
    7732             : 
    7733        1744 :   dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
    7734             :                 &unit_id, &subdie_off);
    7735             : 
    7736        1744 :   if (!silent)
    7737             :     {
    7738        1695 :       Dwarf_Off offset = cu->start;
    7739        1695 :       if (debug_types && version < 5)
    7740           2 :         {
    7741           2 :           Dwarf_Die typedie;
    7742           2 :           Dwarf_Off dieoffset;
    7743           2 :           dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, subdie_off,
    7744             :                                                            &typedie));
    7745           2 :           printf (gettext (" Type unit at offset %" PRIu64 ":\n"
    7746             :                            " Version: %" PRIu16
    7747             :                            ", Abbreviation section offset: %" PRIu64
    7748             :                            ", Address size: %" PRIu8
    7749             :                            ", Offset size: %" PRIu8
    7750             :                            "\n Type signature: %#" PRIx64
    7751             :                            ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
    7752             :                   (uint64_t) offset, version, abbroffset, addrsize, offsize,
    7753             :                   unit_id, (uint64_t) subdie_off, dieoffset);
    7754             :         }
    7755             :       else
    7756             :         {
    7757        1693 :           printf (gettext (" Compilation unit at offset %" PRIu64 ":\n"
    7758             :                            " Version: %" PRIu16
    7759             :                            ", Abbreviation section offset: %" PRIu64
    7760             :                            ", Address size: %" PRIu8
    7761             :                            ", Offset size: %" PRIu8 "\n"),
    7762             :                   (uint64_t) offset, version, abbroffset, addrsize, offsize);
    7763             : 
    7764        1693 :           if (version >= 5 || (unit_type != DW_UT_compile
    7765        1688 :                                && unit_type != DW_UT_partial))
    7766             :             {
    7767           6 :               printf (gettext (" Unit type: %s (%" PRIu8 ")"),
    7768             :                                dwarf_unit_name (unit_type), unit_type);
    7769          12 :               if (unit_type == DW_UT_type
    7770           6 :                   || unit_type == DW_UT_skeleton
    7771           1 :                   || unit_type == DW_UT_split_compile
    7772           1 :                   || unit_type == DW_UT_split_type)
    7773           5 :                 printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
    7774          12 :               if (unit_type == DW_UT_type
    7775           6 :                   || unit_type == DW_UT_split_type)
    7776             :                 {
    7777           0 :                   Dwarf_Die typedie;
    7778           0 :                   Dwarf_Off dieoffset;
    7779           0 :                   dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
    7780             :                                  NULL, NULL, NULL);
    7781           0 :                   dieoffset = dwarf_dieoffset (&typedie);
    7782           0 :                   printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
    7783             :                           subdie_off, dieoffset);
    7784             :                 }
    7785           6 :               printf ("\n");
    7786             :             }
    7787             :         }
    7788             :     }
    7789             : 
    7790        1744 :   if (version < 2 || version > 5
    7791        1744 :       || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
    7792             :     {
    7793           0 :       if (!silent)
    7794           0 :         error (0, 0, gettext ("unknown version (%d) or unit type (%d)"),
    7795             :                version, unit_type);
    7796           0 :       goto next_cu;
    7797             :     }
    7798             : 
    7799        1744 :   struct attrcb_args args =
    7800             :     {
    7801             :       .dwflmod = dwflmod,
    7802             :       .silent = silent,
    7803             :       .version = version,
    7804             :       .addrsize = addrsize,
    7805             :       .offset_size = offsize
    7806             :     };
    7807             : 
    7808        1744 :   bool is_split = false;
    7809        1744 :   int level = 0;
    7810        1744 :   dies[0] = cudie;
    7811        1744 :   args.cu = dies[0].cu;
    7812        1744 :   args.dbg = dbg;
    7813        1744 :   args.is_split = is_split;
    7814             : 
    7815             :   /* We might return here again for the split CU subdie.  */
    7816     1026994 :   do_cu:
    7817     1028742 :   do
    7818             :     {
    7819     1028742 :       Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
    7820     1028742 :       if (unlikely (offset == (Dwarf_Off) -1))
    7821             :         {
    7822           0 :           if (!silent)
    7823           0 :             error (0, 0, gettext ("cannot get DIE offset: %s"),
    7824             :                    dwarf_errmsg (-1));
    7825           0 :           goto do_return;
    7826             :         }
    7827             : 
    7828     1028742 :       int tag = dwarf_tag (&dies[level]);
    7829     1028742 :       if (unlikely (tag == DW_TAG_invalid))
    7830             :         {
    7831           0 :           if (!silent)
    7832           0 :             error (0, 0, gettext ("cannot get tag of DIE at offset [%" PRIx64
    7833             :                                   "] in section '%s': %s"),
    7834             :                    (uint64_t) offset, secname, dwarf_errmsg (-1));
    7835           0 :           goto do_return;
    7836             :         }
    7837             : 
    7838     1028742 :       if (!silent)
    7839             :         {
    7840     1027754 :           unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
    7841     1027754 :           if (is_split)
    7842          48 :             printf (" {%6" PRIx64 "}  ", (uint64_t) offset);
    7843             :           else
    7844     1027706 :             printf (" [%6" PRIx64 "]  ", (uint64_t) offset);
    7845     1027754 :           printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
    7846             :                   dwarf_tag_name (tag), code);
    7847             :         }
    7848             : 
    7849             :       /* Print the attribute values.  */
    7850     1028742 :       args.level = level;
    7851     1028742 :       args.dies = dies;
    7852     1028742 :       (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
    7853             : 
    7854             :       /* Make room for the next level's DIE.  */
    7855     1028742 :       if (level + 1 == maxdies)
    7856           0 :         dies = (Dwarf_Die *) xrealloc (dies,
    7857           0 :                                        (maxdies += 10)
    7858             :                                        * sizeof (Dwarf_Die));
    7859             : 
    7860     1028742 :       int res = dwarf_child (&dies[level], &dies[level + 1]);
    7861     1028742 :       if (res > 0)
    7862             :         {
    7863     1028742 :           while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
    7864      135269 :             if (level-- == 0)
    7865             :               break;
    7866             : 
    7867      895221 :           if (unlikely (res == -1))
    7868             :             {
    7869           0 :               if (!silent)
    7870           0 :                 error (0, 0, gettext ("cannot get next DIE: %s\n"),
    7871             :                        dwarf_errmsg (-1));
    7872           0 :               goto do_return;
    7873             :             }
    7874             :         }
    7875      133521 :       else if (unlikely (res < 0))
    7876             :         {
    7877           0 :           if (!silent)
    7878           0 :             error (0, 0, gettext ("cannot get next DIE: %s"),
    7879             :                    dwarf_errmsg (-1));
    7880           0 :           goto do_return;
    7881             :         }
    7882             :       else
    7883             :         ++level;
    7884             :     }
    7885     1028742 :   while (level >= 0);
    7886             : 
    7887             :   /* We might want to show the split compile unit if this was a skeleton.
    7888             :      We need to scan it if we are requesting printing .debug_ranges for
    7889             :      DWARF4 since GNU DebugFission uses "offsets" into the main ranges
    7890             :      section.  */
    7891        1748 :   if (unit_type == DW_UT_skeleton
    7892          13 :       && ((!silent && show_split_units)
    7893          10 :           || (version < 5 && (print_debug_sections & section_ranges) != 0)))
    7894             :     {
    7895           5 :       Dwarf_Die subdie;
    7896           5 :       if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
    7897           5 :           || dwarf_tag (&subdie) == DW_TAG_invalid)
    7898             :         {
    7899           1 :           if (!silent)
    7900             :             {
    7901           1 :               Dwarf_Attribute dwo_at;
    7902           2 :               const char *dwo_name =
    7903           1 :                 (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
    7904             :                                                &dwo_at))
    7905           1 :                  ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
    7906             :                                                    &dwo_at))
    7907           0 :                      ?: "<unknown>"));
    7908           1 :               fprintf (stderr,
    7909             :                        "Could not find split unit '%s', id: %" PRIx64 "\n",
    7910             :                        dwo_name, unit_id);
    7911             :             }
    7912             :         }
    7913             :       else
    7914             :         {
    7915           4 :           Dwarf_CU *split_cu = subdie.cu;
    7916           4 :           dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
    7917             :                         &addrsize, &offsize, &unit_id, &subdie_off);
    7918           4 :           Dwarf_Off offset = cu->start;
    7919             : 
    7920           4 :           if (!silent)
    7921             :             {
    7922           2 :               printf (gettext (" Split compilation unit at offset %"
    7923             :                                PRIu64 ":\n"
    7924             :                                " Version: %" PRIu16
    7925             :                                ", Abbreviation section offset: %" PRIu64
    7926             :                                ", Address size: %" PRIu8
    7927             :                                ", Offset size: %" PRIu8 "\n"),
    7928             :                       (uint64_t) offset, version, abbroffset,
    7929             :                       addrsize, offsize);
    7930           2 :               printf (gettext (" Unit type: %s (%" PRIu8 ")"),
    7931             :                       dwarf_unit_name (unit_type), unit_type);
    7932           2 :               printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
    7933           2 :               printf ("\n");
    7934             :             }
    7935             : 
    7936           4 :           unit_type = DW_UT_split_compile;
    7937           4 :           is_split = true;
    7938           4 :           level = 0;
    7939           4 :           dies[0] = subdie;
    7940           4 :           args.cu = dies[0].cu;
    7941           4 :           args.dbg = split_cu->dbg;
    7942           4 :           args.is_split = is_split;
    7943           4 :           goto do_cu;
    7944             :         }
    7945             :     }
    7946             : 
    7947             :   /* And again... */
    7948        1744 :   goto next_cu;
    7949             : 
    7950          97 :  do_return:
    7951          97 :   free (dies);
    7952             : }
    7953             : 
    7954             : static void
    7955          19 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    7956             :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    7957             : {
    7958          19 :   print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
    7959          19 : }
    7960             : 
    7961             : static void
    7962           1 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    7963             :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    7964             : {
    7965           1 :   print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
    7966           1 : }
    7967             : 
    7968             : 
    7969             : static void
    7970           0 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
    7971             :                             GElf_Ehdr *ehdr __attribute__ ((unused)),
    7972             :                             Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    7973             : {
    7974           0 :   printf (gettext ("\
    7975             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
    7976             :           elf_ndxscn (scn), section_name (ebl, shdr),
    7977           0 :           (uint64_t) shdr->sh_offset);
    7978             : 
    7979           0 :   size_t address_size
    7980           0 :     = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    7981             : 
    7982           0 :   Dwarf_Lines *lines;
    7983           0 :   size_t nlines;
    7984           0 :   Dwarf_Off off, next_off = 0;
    7985           0 :   Dwarf_CU *cu = NULL;
    7986           0 :   while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
    7987             :                            &lines, &nlines) == 0)
    7988             :     {
    7989           0 :       Dwarf_Die cudie;
    7990           0 :       if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
    7991             :                                        NULL, NULL, NULL, NULL) == 0)
    7992           0 :         printf (" CU [%" PRIx64 "] %s\n",
    7993             :                 dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
    7994             :       else
    7995             :         {
    7996             :           /* DWARF5 lines can be independent of any CU, but they probably
    7997             :              are used by some CU.  Determine the CU this block is for.  */
    7998           0 :           Dwarf_Off cuoffset;
    7999           0 :           Dwarf_Off ncuoffset = 0;
    8000           0 :           size_t hsize;
    8001           0 :           while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
    8002             :                                NULL, NULL, NULL) == 0)
    8003             :             {
    8004           0 :               if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
    8005           0 :                 continue;
    8006           0 :               Dwarf_Attribute stmt_list;
    8007           0 :               if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
    8008           0 :                 continue;
    8009           0 :               Dwarf_Word lineoff;
    8010           0 :               if (dwarf_formudata (&stmt_list, &lineoff) != 0)
    8011           0 :                 continue;
    8012           0 :               if (lineoff == off)
    8013             :                 {
    8014             :                   /* Found the CU.  */
    8015           0 :                   cu = cudie.cu;
    8016           0 :                   break;
    8017             :                 }
    8018             :             }
    8019             : 
    8020           0 :           if (cu != NULL)
    8021           0 :             printf (" CU [%" PRIx64 "] %s\n",
    8022             :                     dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
    8023             :           else
    8024           0 :             printf (" No CU\n");
    8025             :         }
    8026             : 
    8027           0 :       printf ("  line:col SBPE* disc isa op address"
    8028             :               " (Statement Block Prologue Epilogue *End)\n");
    8029           0 :       const char *last_file = "";
    8030           0 :       for (size_t n = 0; n < nlines; n++)
    8031             :         {
    8032           0 :           Dwarf_Line *line = dwarf_onesrcline (lines, n);
    8033           0 :           if (line == NULL)
    8034             :             {
    8035           0 :               printf ("  dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
    8036           0 :               continue;
    8037             :             }
    8038           0 :           Dwarf_Word mtime, length;
    8039           0 :           const char *file = dwarf_linesrc (line, &mtime, &length);
    8040           0 :           if (file == NULL)
    8041             :             {
    8042           0 :               printf ("  <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
    8043           0 :               last_file = "";
    8044             :             }
    8045           0 :           else if (strcmp (last_file, file) != 0)
    8046             :             {
    8047           0 :               printf ("  %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
    8048             :                       file, mtime, length);
    8049           0 :               last_file = file;
    8050             :             }
    8051             : 
    8052           0 :           int lineno, colno;
    8053           0 :           bool statement, endseq, block, prologue_end, epilogue_begin;
    8054           0 :           unsigned int lineop, isa, disc;
    8055           0 :           Dwarf_Addr address;
    8056           0 :           dwarf_lineaddr (line, &address);
    8057           0 :           dwarf_lineno (line, &lineno);
    8058           0 :           dwarf_linecol (line, &colno);
    8059           0 :           dwarf_lineop_index (line, &lineop);
    8060           0 :           dwarf_linebeginstatement (line, &statement);
    8061           0 :           dwarf_lineendsequence (line, &endseq);
    8062           0 :           dwarf_lineblock (line, &block);
    8063           0 :           dwarf_lineprologueend (line, &prologue_end);
    8064           0 :           dwarf_lineepiloguebegin (line, &epilogue_begin);
    8065           0 :           dwarf_lineisa (line, &isa);
    8066           0 :           dwarf_linediscriminator (line, &disc);
    8067             : 
    8068             :           /* End sequence is special, it is one byte past.  */
    8069           0 :           printf ("  %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
    8070             :                   lineno, colno,
    8071           0 :                   (statement ? 'S' : ' '),
    8072           0 :                   (block ? 'B' : ' '),
    8073           0 :                   (prologue_end ? 'P' : ' '),
    8074           0 :                   (epilogue_begin ? 'E' : ' '),
    8075           0 :                   (endseq ? '*' : ' '),
    8076             :                   disc, isa, lineop);
    8077           0 :           print_dwarf_addr (dwflmod, address_size,
    8078           0 :                             address - (endseq ? 1 : 0), address);
    8079           0 :           printf ("\n");
    8080             : 
    8081           0 :           if (endseq)
    8082           0 :             printf("\n");
    8083             :         }
    8084             :     }
    8085           0 : }
    8086             : 
    8087             : 
    8088             : /* Print the value of a form.
    8089             :    Returns new value of readp, or readendp on failure.  */
    8090             : static const unsigned char *
    8091          20 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
    8092             :                  const unsigned char *readendp, unsigned int offset_len,
    8093             :                  Dwarf_Off str_offsets_base)
    8094             : {
    8095          20 :   Dwarf_Word val;
    8096          20 :   unsigned char *endp;
    8097          20 :   Elf_Data *data;
    8098          20 :   char *str;
    8099          20 :   switch (form)
    8100             :     {
    8101           8 :     case DW_FORM_data1:
    8102           8 :       if (readendp - readp < 1)
    8103             :         {
    8104           0 :         invalid_data:
    8105           0 :           error (0, 0, "invalid data");
    8106           0 :           return readendp;
    8107             :         }
    8108           8 :       val = *readp++;
    8109           8 :       printf (" %" PRIx8, (unsigned int) val);
    8110             :       break;
    8111             : 
    8112           0 :     case DW_FORM_data2:
    8113           0 :       if (readendp - readp < 2)
    8114             :         goto invalid_data;
    8115           0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    8116           0 :       printf(" %" PRIx16, (unsigned int) val);
    8117             :       break;
    8118             : 
    8119           0 :     case DW_FORM_data4:
    8120           0 :       if (readendp - readp < 4)
    8121             :         goto invalid_data;
    8122           0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    8123           0 :       printf (" %" PRIx32, (unsigned int) val);
    8124             :       break;
    8125             : 
    8126           0 :     case DW_FORM_data8:
    8127           0 :       if (readendp - readp < 8)
    8128             :         goto invalid_data;
    8129           0 :       val = read_8ubyte_unaligned_inc (dbg, readp);
    8130           0 :       printf (" %" PRIx64, val);
    8131             :       break;
    8132             : 
    8133           0 :     case DW_FORM_sdata:
    8134           0 :       if (readendp - readp < 1)
    8135             :         goto invalid_data;
    8136           0 :       get_sleb128 (val, readp, readendp);
    8137           0 :       printf (" %" PRIx64, val);
    8138             :       break;
    8139             : 
    8140           0 :     case DW_FORM_udata:
    8141           0 :       if (readendp - readp < 1)
    8142             :         goto invalid_data;
    8143           0 :       get_uleb128 (val, readp, readendp);
    8144           0 :       printf (" %" PRIx64, val);
    8145             :       break;
    8146             : 
    8147           0 :     case DW_FORM_block:
    8148           0 :       if (readendp - readp < 1)
    8149             :         goto invalid_data;
    8150           0 :       get_uleb128 (val, readp, readendp);
    8151           0 :       if ((size_t) (readendp - readp) < val)
    8152             :         goto invalid_data;
    8153           0 :       print_bytes (val, readp);
    8154           0 :       readp += val;
    8155           0 :       break;
    8156             : 
    8157           0 :     case DW_FORM_block1:
    8158           0 :       if (readendp - readp < 1)
    8159             :         goto invalid_data;
    8160           0 :       val = *readp++;
    8161           0 :       if ((size_t) (readendp - readp) < val)
    8162             :         goto invalid_data;
    8163           0 :       print_bytes (val, readp);
    8164           0 :       readp += val;
    8165           0 :       break;
    8166             : 
    8167           0 :     case DW_FORM_block2:
    8168           0 :       if (readendp - readp < 2)
    8169             :         goto invalid_data;
    8170           0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    8171           0 :       if ((size_t) (readendp - readp) < val)
    8172             :         goto invalid_data;
    8173           0 :       print_bytes (val, readp);
    8174           0 :       readp += val;
    8175           0 :       break;
    8176             : 
    8177           0 :     case DW_FORM_block4:
    8178           0 :       if (readendp - readp < 4)
    8179             :         goto invalid_data;
    8180           0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    8181           0 :       if ((size_t) (readendp - readp) < val)
    8182             :         goto invalid_data;
    8183           0 :       print_bytes (val, readp);
    8184           0 :       readp += val;
    8185           0 :       break;
    8186             : 
    8187           0 :     case DW_FORM_data16:
    8188           0 :       if (readendp - readp < 16)
    8189             :         goto invalid_data;
    8190           0 :       print_bytes (16, readp);
    8191           0 :       readp += 16;
    8192           0 :       break;
    8193             : 
    8194           0 :     case DW_FORM_flag:
    8195           0 :       if (readendp - readp < 1)
    8196             :         goto invalid_data;
    8197           0 :       val = *readp++;
    8198           0 :       printf ("%s", val != 0 ? yes_str : no_str);
    8199             :       break;
    8200             : 
    8201           0 :     case DW_FORM_string:
    8202           0 :       endp = memchr (readp, '\0', readendp - readp);
    8203           0 :       if (endp == NULL)
    8204             :         goto invalid_data;
    8205           0 :       printf ("%s", readp);
    8206           0 :       readp = endp + 1;
    8207           0 :       break;
    8208             : 
    8209          12 :     case DW_FORM_strp:
    8210             :     case DW_FORM_line_strp:
    8211             :     case DW_FORM_strp_sup:
    8212          12 :       if ((size_t) (readendp - readp) < offset_len)
    8213             :         goto invalid_data;
    8214          12 :       if (offset_len == 8)
    8215           0 :         val = read_8ubyte_unaligned_inc (dbg, readp);
    8216             :       else
    8217          12 :         val = read_4ubyte_unaligned_inc (dbg, readp);
    8218          12 :       if (form == DW_FORM_strp)
    8219           0 :         data = dbg->sectiondata[IDX_debug_str];
    8220          12 :       else if (form == DW_FORM_line_strp)
    8221          12 :         data = dbg->sectiondata[IDX_debug_line_str];
    8222             :       else /* form == DW_FORM_strp_sup */
    8223             :         {
    8224           0 :           Dwarf *alt = dwarf_getalt (dbg);
    8225           0 :           data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
    8226             :         }
    8227          12 :       if (data == NULL || val >= data->d_size
    8228          12 :           || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
    8229             :         str = "???";
    8230             :       else
    8231          12 :         str = (char *) data->d_buf + val;
    8232          12 :       printf ("%s (%" PRIu64 ")", str, val);
    8233             :       break;
    8234             : 
    8235           0 :     case DW_FORM_sec_offset:
    8236           0 :       if ((size_t) (readendp - readp) < offset_len)
    8237             :         goto invalid_data;
    8238           0 :       if (offset_len == 8)
    8239           0 :         val = read_8ubyte_unaligned_inc (dbg, readp);
    8240             :       else
    8241           0 :         val = read_4ubyte_unaligned_inc (dbg, readp);
    8242           0 :       printf ("[%" PRIx64 "]", val);
    8243             :       break;
    8244             : 
    8245           0 :     case DW_FORM_strx:
    8246             :     case DW_FORM_GNU_str_index:
    8247           0 :       if (readendp - readp < 1)
    8248             :         goto invalid_data;
    8249           0 :       get_uleb128 (val, readp, readendp);
    8250           0 :     strx_val:
    8251           0 :       data = dbg->sectiondata[IDX_debug_str_offsets];
    8252           0 :       if (data == NULL
    8253           0 :           || data->d_size - str_offsets_base < val)
    8254             :         str = "???";
    8255             :       else
    8256             :         {
    8257           0 :           const unsigned char *strreadp = data->d_buf + str_offsets_base + val;
    8258           0 :           const unsigned char *strreadendp = data->d_buf + data->d_size;
    8259           0 :           if ((size_t) (strreadendp - strreadp) < offset_len)
    8260             :             str = "???";
    8261             :           else
    8262             :             {
    8263           0 :               Dwarf_Off idx;
    8264           0 :               if (offset_len == 8)
    8265           0 :                 idx = read_8ubyte_unaligned (dbg, strreadp);
    8266             :               else
    8267           0 :                 idx = read_4ubyte_unaligned (dbg, strreadp);
    8268             : 
    8269           0 :               data = dbg->sectiondata[IDX_debug_str];
    8270           0 :               if (data == NULL || idx >= data->d_size
    8271           0 :                   || memchr (data->d_buf + idx, '\0',
    8272             :                              data->d_size - idx) == NULL)
    8273             :                 str = "???";
    8274             :               else
    8275           0 :                 str = (char *) data->d_buf + idx;
    8276             :             }
    8277             :         }
    8278           0 :       printf ("%s (%" PRIu64 ")", str, val);
    8279             :       break;
    8280             : 
    8281           0 :     case DW_FORM_strx1:
    8282           0 :       if (readendp - readp < 1)
    8283             :         goto invalid_data;
    8284           0 :       val = *readp++;
    8285           0 :       goto strx_val;
    8286             : 
    8287           0 :     case DW_FORM_strx2:
    8288           0 :       if (readendp - readp < 2)
    8289             :         goto invalid_data;
    8290           0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    8291           0 :       goto strx_val;
    8292             : 
    8293           0 :     case DW_FORM_strx3:
    8294           0 :       if (readendp - readp < 3)
    8295             :         goto invalid_data;
    8296           0 :       val = read_3ubyte_unaligned_inc (dbg, readp);
    8297           0 :       goto strx_val;
    8298             : 
    8299           0 :     case DW_FORM_strx4:
    8300           0 :       if (readendp - readp < 4)
    8301             :         goto invalid_data;
    8302           0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    8303           0 :       goto strx_val;
    8304             : 
    8305           0 :     default:
    8306           0 :       error (0, 0, gettext ("unknown form: %s"), dwarf_form_name (form));
    8307           0 :       return readendp;
    8308             :     }
    8309             : 
    8310          20 :   return readp;
    8311             : }
    8312             : 
    8313             : static void
    8314          63 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    8315             :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    8316             : {
    8317          63 :   if (decodedline)
    8318             :     {
    8319          17 :       print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
    8320          17 :       return;
    8321             :     }
    8322             : 
    8323          46 :   printf (gettext ("\
    8324             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    8325             :           elf_ndxscn (scn), section_name (ebl, shdr),
    8326          46 :           (uint64_t) shdr->sh_offset);
    8327             : 
    8328          46 :   if (shdr->sh_size == 0)
    8329             :     return;
    8330             : 
    8331             :   /* There is no functionality in libdw to read the information in the
    8332             :      way it is represented here.  Hardcode the decoder.  */
    8333          92 :   Elf_Data *data = (dbg->sectiondata[IDX_debug_line]
    8334          46 :                     ?: elf_rawdata (scn, NULL));
    8335          46 :   if (unlikely (data == NULL))
    8336             :     {
    8337           0 :       error (0, 0, gettext ("cannot get line data section data: %s"),
    8338             :              elf_errmsg (-1));
    8339           0 :       return;
    8340             :     }
    8341             : 
    8342          46 :   const unsigned char *linep = (const unsigned char *) data->d_buf;
    8343          46 :   const unsigned char *lineendp;
    8344             : 
    8345          46 :   while (linep
    8346        1724 :          < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
    8347             :     {
    8348        1678 :       size_t start_offset = linep - (const unsigned char *) data->d_buf;
    8349             : 
    8350        1678 :       printf (gettext ("\nTable at offset %zu:\n"), start_offset);
    8351             : 
    8352        1678 :       if (unlikely (linep + 4 > lineendp))
    8353             :         goto invalid_data;
    8354        1678 :       Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
    8355        1678 :       unsigned int length = 4;
    8356        1678 :       if (unlikely (unit_length == 0xffffffff))
    8357             :         {
    8358           0 :           if (unlikely (linep + 8 > lineendp))
    8359             :             {
    8360           0 :             invalid_data:
    8361           0 :               error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
    8362             :                      elf_ndxscn (scn), section_name (ebl, shdr));
    8363           0 :               return;
    8364             :             }
    8365           0 :           unit_length = read_8ubyte_unaligned_inc (dbg, linep);
    8366           0 :           length = 8;
    8367             :         }
    8368             : 
    8369             :       /* Check whether we have enough room in the section.  */
    8370        1678 :       if (unlikely (unit_length > (size_t) (lineendp - linep)))
    8371             :         goto invalid_data;
    8372        1678 :       lineendp = linep + unit_length;
    8373             : 
    8374             :       /* The next element of the header is the version identifier.  */
    8375        1678 :       if ((size_t) (lineendp - linep) < 2)
    8376             :         goto invalid_data;
    8377        1678 :       uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
    8378             : 
    8379        3356 :       size_t address_size
    8380        1678 :         = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    8381        1678 :       unsigned char segment_selector_size = 0;
    8382        1678 :       if (version > 4)
    8383             :         {
    8384           2 :           if ((size_t) (lineendp - linep) < 2)
    8385             :             goto invalid_data;
    8386           2 :           address_size = *linep++;
    8387           2 :           segment_selector_size = *linep++;
    8388             :         }
    8389             : 
    8390             :       /* Next comes the header length.  */
    8391        1678 :       Dwarf_Word header_length;
    8392        1678 :       if (length == 4)
    8393             :         {
    8394        1678 :           if ((size_t) (lineendp - linep) < 4)
    8395             :             goto invalid_data;
    8396        1678 :           header_length = read_4ubyte_unaligned_inc (dbg, linep);
    8397             :         }
    8398             :       else
    8399             :         {
    8400           0 :           if ((size_t) (lineendp - linep) < 8)
    8401             :             goto invalid_data;
    8402           0 :           header_length = read_8ubyte_unaligned_inc (dbg, linep);
    8403             :         }
    8404             : 
    8405             :       /* Next the minimum instruction length.  */
    8406        1678 :       if ((size_t) (lineendp - linep) < 1)
    8407             :         goto invalid_data;
    8408        1678 :       uint_fast8_t minimum_instr_len = *linep++;
    8409             : 
    8410             :       /* Next the maximum operations per instruction, in version 4 format.  */
    8411        1678 :       uint_fast8_t max_ops_per_instr;
    8412        1678 :       if (version < 4)
    8413             :         max_ops_per_instr = 1;
    8414             :       else
    8415             :         {
    8416          10 :           if ((size_t) (lineendp - linep) < 1)
    8417             :             goto invalid_data;
    8418          10 :           max_ops_per_instr = *linep++;
    8419             :         }
    8420             : 
    8421             :       /* We need at least 4 more bytes.  */
    8422        1678 :       if ((size_t) (lineendp - linep) < 4)
    8423             :         goto invalid_data;
    8424             : 
    8425             :       /* Then the flag determining the default value of the is_stmt
    8426             :          register.  */
    8427        1678 :       uint_fast8_t default_is_stmt = *linep++;
    8428             : 
    8429             :       /* Now the line base.  */
    8430        1678 :       int_fast8_t line_base = *linep++;
    8431             : 
    8432             :       /* And the line range.  */
    8433        1678 :       uint_fast8_t line_range = *linep++;
    8434             : 
    8435             :       /* The opcode base.  */
    8436        1678 :       uint_fast8_t opcode_base = *linep++;
    8437             : 
    8438             :       /* Print what we got so far.  */
    8439        1678 :       printf (gettext ("\n"
    8440             :                        " Length:                         %" PRIu64 "\n"
    8441             :                        " DWARF version:                  %" PRIuFAST16 "\n"
    8442             :                        " Prologue length:                %" PRIu64 "\n"
    8443             :                        " Address size:                   %zd\n"
    8444             :                        " Segment selector size:          %zd\n"
    8445             :                        " Min instruction length:         %" PRIuFAST8 "\n"
    8446             :                        " Max operations per instruction: %" PRIuFAST8 "\n"
    8447             :                        " Initial value if 'is_stmt':     %" PRIuFAST8 "\n"
    8448             :                        " Line base:                      %" PRIdFAST8 "\n"
    8449             :                        " Line range:                     %" PRIuFAST8 "\n"
    8450             :                        " Opcode base:                    %" PRIuFAST8 "\n"
    8451             :                        "\n"
    8452             :                        "Opcodes:\n"),
    8453             :               (uint64_t) unit_length, version, (uint64_t) header_length,
    8454             :               address_size, (size_t) segment_selector_size,
    8455             :               minimum_instr_len, max_ops_per_instr,
    8456             :               default_is_stmt, line_base,
    8457             :               line_range, opcode_base);
    8458             : 
    8459        1678 :       if (version < 2 || version > 5)
    8460             :         {
    8461           0 :           error (0, 0, gettext ("cannot handle .debug_line version: %u\n"),
    8462             :                  (unsigned int) version);
    8463           0 :           linep = lineendp;
    8464           0 :           continue;
    8465             :         }
    8466             : 
    8467        1678 :       if (address_size != 4 && address_size != 8)
    8468             :         {
    8469           0 :           error (0, 0, gettext ("cannot handle address size: %u\n"),
    8470             :                  (unsigned int) address_size);
    8471           0 :           linep = lineendp;
    8472           0 :           continue;
    8473             :         }
    8474             : 
    8475        1678 :       if (segment_selector_size != 0)
    8476             :         {
    8477           0 :           error (0, 0, gettext ("cannot handle segment selector size: %u\n"),
    8478             :                  (unsigned int) segment_selector_size);
    8479           0 :           linep = lineendp;
    8480           0 :           continue;
    8481             :         }
    8482             : 
    8483        1678 :       if (unlikely (linep + opcode_base - 1 >= lineendp))
    8484             :         {
    8485           0 :         invalid_unit:
    8486           0 :           error (0, 0,
    8487           0 :                  gettext ("invalid data at offset %tu in section [%zu] '%s'"),
    8488           0 :                  linep - (const unsigned char *) data->d_buf,
    8489             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    8490           0 :           linep = lineendp;
    8491           0 :           continue;
    8492             :         }
    8493        1678 :       int opcode_base_l10 = 1;
    8494        1678 :       unsigned int tmp = opcode_base;
    8495        3354 :       while (tmp > 10)
    8496             :         {
    8497        1676 :           tmp /= 10;
    8498        1676 :           ++opcode_base_l10;
    8499             :         }
    8500        1678 :       const uint8_t *standard_opcode_lengths = linep - 1;
    8501       21808 :       for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
    8502       20130 :         printf (ngettext ("  [%*" PRIuFAST8 "]  %hhu argument\n",
    8503             :                           "  [%*" PRIuFAST8 "]  %hhu arguments\n",
    8504             :                           (int) linep[cnt - 1]),
    8505       20130 :                 opcode_base_l10, cnt, linep[cnt - 1]);
    8506        1678 :       linep += opcode_base - 1;
    8507             : 
    8508        1678 :       if (unlikely (linep >= lineendp))
    8509             :         goto invalid_unit;
    8510             : 
    8511        1678 :       Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
    8512             : 
    8513        1678 :       puts (gettext ("\nDirectory table:"));
    8514        1678 :       if (version > 4)
    8515             :         {
    8516           2 :           struct encpair { uint16_t desc; uint16_t form; };
    8517           2 :           struct encpair enc[256];
    8518             : 
    8519           2 :           printf (gettext ("      ["));
    8520           2 :           if ((size_t) (lineendp - linep) < 1)
    8521           0 :             goto invalid_data;
    8522           2 :           unsigned char directory_entry_format_count = *linep++;
    8523           4 :           for (int i = 0; i < directory_entry_format_count; i++)
    8524             :             {
    8525           2 :               uint16_t desc, form;
    8526           2 :               if ((size_t) (lineendp - linep) < 1)
    8527             :                 goto invalid_data;
    8528           2 :               get_uleb128 (desc, linep, lineendp);
    8529           2 :               if ((size_t) (lineendp - linep) < 1)
    8530             :                 goto invalid_data;
    8531           2 :               get_uleb128 (form, linep, lineendp);
    8532             : 
    8533           2 :               enc[i].desc = desc;
    8534           2 :               enc[i].form = form;
    8535             : 
    8536           2 :               printf ("%s(%s)",
    8537             :                       dwarf_line_content_description_name (desc),
    8538             :                       dwarf_form_name (form));
    8539           2 :               if (i + 1 < directory_entry_format_count)
    8540           0 :                 printf (", ");
    8541             :             }
    8542           2 :           printf ("]\n");
    8543             : 
    8544           2 :           uint64_t directories_count;
    8545           2 :           if ((size_t) (lineendp - linep) < 1)
    8546             :             goto invalid_data;
    8547           2 :           get_uleb128 (directories_count, linep, lineendp);
    8548             : 
    8549           4 :           if (directory_entry_format_count == 0
    8550           2 :               && directories_count != 0)
    8551             :             goto invalid_data;
    8552             : 
    8553           6 :           for (uint64_t i = 0; i < directories_count; i++)
    8554             :             {
    8555           4 :               printf (" %-5" PRIu64 " ", i);
    8556           8 :               for (int j = 0; j < directory_entry_format_count; j++)
    8557             :                 {
    8558           4 :                   linep = print_form_data (dbg, enc[j].form,
    8559             :                                            linep, lineendp, length,
    8560             :                                            str_offsets_base);
    8561           4 :                   if (j + 1 < directory_entry_format_count)
    8562           0 :                     printf (", ");
    8563             :                 }
    8564           4 :               printf ("\n");
    8565           4 :               if (linep >= lineendp)
    8566           0 :                 goto invalid_unit;
    8567             :             }
    8568             :         }
    8569             :       else
    8570             :         {
    8571       13321 :           while (linep < lineendp && *linep != 0)
    8572             :             {
    8573       11645 :               unsigned char *endp = memchr (linep, '\0', lineendp - linep);
    8574       11645 :               if (unlikely (endp == NULL))
    8575             :                 goto invalid_unit;
    8576             : 
    8577       11645 :               printf (" %s\n", (char *) linep);
    8578             : 
    8579       11645 :               linep = endp + 1;
    8580             :             }
    8581        1676 :           if (linep >= lineendp || *linep != 0)
    8582             :             goto invalid_unit;
    8583             :           /* Skip the final NUL byte.  */
    8584        1676 :           ++linep;
    8585             :         }
    8586             : 
    8587        1678 :       if (unlikely (linep >= lineendp))
    8588             :         goto invalid_unit;
    8589             : 
    8590        1678 :       puts (gettext ("\nFile name table:"));
    8591        1678 :       if (version > 4)
    8592             :         {
    8593           2 :           struct encpair { uint16_t desc; uint16_t form; };
    8594           2 :           struct encpair enc[256];
    8595             : 
    8596           2 :           printf (gettext ("      ["));
    8597           2 :           if ((size_t) (lineendp - linep) < 1)
    8598           0 :             goto invalid_data;
    8599           2 :           unsigned char file_name_format_count = *linep++;
    8600           6 :           for (int i = 0; i < file_name_format_count; i++)
    8601             :             {
    8602           4 :               uint64_t desc, form;
    8603           4 :               if ((size_t) (lineendp - linep) < 1)
    8604             :                 goto invalid_data;
    8605           4 :               get_uleb128 (desc, linep, lineendp);
    8606           4 :               if ((size_t) (lineendp - linep) < 1)
    8607             :                 goto invalid_data;
    8608           4 :               get_uleb128 (form, linep, lineendp);
    8609             : 
    8610           4 :               if (! libdw_valid_user_form (form))
    8611             :                 goto invalid_data;
    8612             : 
    8613           4 :               enc[i].desc = desc;
    8614           4 :               enc[i].form = form;
    8615             : 
    8616           4 :               printf ("%s(%s)",
    8617             :                       dwarf_line_content_description_name (desc),
    8618             :                       dwarf_form_name (form));
    8619           4 :               if (i + 1 < file_name_format_count)
    8620           2 :                 printf (", ");
    8621             :             }
    8622           2 :           printf ("]\n");
    8623             : 
    8624           2 :           uint64_t file_name_count;
    8625           2 :           if ((size_t) (lineendp - linep) < 1)
    8626             :             goto invalid_data;
    8627           2 :           get_uleb128 (file_name_count, linep, lineendp);
    8628             : 
    8629           4 :           if (file_name_format_count == 0
    8630           2 :               && file_name_count != 0)
    8631             :             goto invalid_data;
    8632             : 
    8633          10 :           for (uint64_t i = 0; i < file_name_count; i++)
    8634             :             {
    8635           8 :               printf (" %-5" PRIu64 " ", i);
    8636          24 :               for (int j = 0; j < file_name_format_count; j++)
    8637             :                 {
    8638          16 :                   linep = print_form_data (dbg, enc[j].form,
    8639             :                                            linep, lineendp, length,
    8640             :                                            str_offsets_base);
    8641          16 :                   if (j + 1 < file_name_format_count)
    8642           8 :                     printf (", ");
    8643             :                 }
    8644           8 :               printf ("\n");
    8645           8 :               if (linep >= lineendp)
    8646           0 :                 goto invalid_unit;
    8647             :             }
    8648             :         }
    8649             :       else
    8650             :         {
    8651        1676 :           puts (gettext (" Entry Dir   Time      Size      Name"));
    8652       34765 :           for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
    8653             :             {
    8654             :               /* First comes the file name.  */
    8655       33089 :               char *fname = (char *) linep;
    8656       33089 :               unsigned char *endp = memchr (fname, '\0', lineendp - linep);
    8657       33089 :               if (unlikely (endp == NULL))
    8658             :                 goto invalid_unit;
    8659       33089 :               linep = endp + 1;
    8660             : 
    8661             :               /* Then the index.  */
    8662       33089 :               unsigned int diridx;
    8663       33089 :               if (lineendp - linep < 1)
    8664             :                 goto invalid_unit;
    8665       33089 :               get_uleb128 (diridx, linep, lineendp);
    8666             : 
    8667             :               /* Next comes the modification time.  */
    8668       33089 :               unsigned int mtime;
    8669       33089 :               if (lineendp - linep < 1)
    8670             :                 goto invalid_unit;
    8671       33089 :               get_uleb128 (mtime, linep, lineendp);
    8672             : 
    8673             :               /* Finally the length of the file.  */
    8674       33089 :               unsigned int fsize;
    8675       33089 :               if (lineendp - linep < 1)
    8676             :                 goto invalid_unit;
    8677       33089 :               get_uleb128 (fsize, linep, lineendp);
    8678             : 
    8679       33089 :               printf (" %-5u %-5u %-9u %-9u %s\n",
    8680             :                       cnt, diridx, mtime, fsize, fname);
    8681             :             }
    8682        1676 :           if (linep >= lineendp || *linep != '\0')
    8683             :             goto invalid_unit;
    8684             :           /* Skip the final NUL byte.  */
    8685        1676 :           ++linep;
    8686             :         }
    8687             : 
    8688        1678 :       puts (gettext ("\nLine number statements:"));
    8689        1678 :       Dwarf_Word address = 0;
    8690        1678 :       unsigned int op_index = 0;
    8691        1678 :       size_t line = 1;
    8692        1678 :       uint_fast8_t is_stmt = default_is_stmt;
    8693             : 
    8694             :       /* Apply the "operation advance" from a special opcode
    8695             :          or DW_LNS_advance_pc (as per DWARF4 6.2.5.1).  */
    8696        1678 :       unsigned int op_addr_advance;
    8697        1678 :       bool show_op_index;
    8698        1678 :       inline void advance_pc (unsigned int op_advance)
    8699             :       {
    8700      704866 :         op_addr_advance = minimum_instr_len * ((op_index + op_advance)
    8701      352433 :                                                / max_ops_per_instr);
    8702      352433 :         address += op_addr_advance;
    8703     1057299 :         show_op_index = (op_index > 0 ||
    8704      352433 :                          (op_index + op_advance) % max_ops_per_instr > 0);
    8705      352433 :         op_index = (op_index + op_advance) % max_ops_per_instr;
    8706             :       }
    8707             : 
    8708        1678 :       if (max_ops_per_instr == 0)
    8709             :         {
    8710           0 :           error (0, 0,
    8711           0 :                  gettext ("invalid maximum operations per instruction is zero"));
    8712           0 :           linep = lineendp;
    8713           0 :           continue;
    8714             :         }
    8715             : 
    8716     1089440 :       while (linep < lineendp)
    8717             :         {
    8718     1087762 :           size_t offset = linep - (const unsigned char *) data->d_buf;
    8719     1087762 :           unsigned int u128;
    8720     1087762 :           int s128;
    8721             : 
    8722             :           /* Read the opcode.  */
    8723     1087762 :           unsigned int opcode = *linep++;
    8724             : 
    8725     1087762 :           printf (" [%6" PRIx64 "]", (uint64_t)offset);
    8726             :           /* Is this a special opcode?  */
    8727     1087762 :           if (likely (opcode >= opcode_base))
    8728             :             {
    8729      322097 :               if (unlikely (line_range == 0))
    8730             :                 goto invalid_unit;
    8731             : 
    8732             :               /* Yes.  Handling this is quite easy since the opcode value
    8733             :                  is computed with
    8734             : 
    8735             :                  opcode = (desired line increment - line_base)
    8736             :                            + (line_range * address advance) + opcode_base
    8737             :               */
    8738      644194 :               int line_increment = (line_base
    8739      322097 :                                     + (opcode - opcode_base) % line_range);
    8740             : 
    8741             :               /* Perform the increments.  */
    8742      322097 :               line += line_increment;
    8743      322097 :               advance_pc ((opcode - opcode_base) / line_range);
    8744             : 
    8745      322097 :               printf (gettext (" special opcode %u: address+%u = "),
    8746             :                       opcode, op_addr_advance);
    8747      322097 :               print_dwarf_addr (dwflmod, 0, address, address);
    8748      322097 :               if (show_op_index)
    8749           0 :                 printf (gettext (", op_index = %u, line%+d = %zu\n"),
    8750             :                         op_index, line_increment, line);
    8751             :               else
    8752      322097 :                 printf (gettext (", line%+d = %zu\n"),
    8753             :                         line_increment, line);
    8754             :             }
    8755      765665 :           else if (opcode == 0)
    8756             :             {
    8757             :               /* This an extended opcode.  */
    8758       47339 :               if (unlikely (linep + 2 > lineendp))
    8759             :                 goto invalid_unit;
    8760             : 
    8761             :               /* The length.  */
    8762       47339 :               unsigned int len = *linep++;
    8763             : 
    8764       47339 :               if (unlikely (linep + len > lineendp))
    8765             :                 goto invalid_unit;
    8766             : 
    8767             :               /* The sub-opcode.  */
    8768       47339 :               opcode = *linep++;
    8769             : 
    8770       47339 :               printf (gettext (" extended opcode %u: "), opcode);
    8771             : 
    8772       47339 :               switch (opcode)
    8773             :                 {
    8774        1727 :                 case DW_LNE_end_sequence:
    8775        1727 :                   puts (gettext (" end of sequence"));
    8776             : 
    8777             :                   /* Reset the registers we care about.  */
    8778        1727 :                   address = 0;
    8779        1727 :                   op_index = 0;
    8780        1727 :                   line = 1;
    8781        1727 :                   is_stmt = default_is_stmt;
    8782        1727 :                   break;
    8783             : 
    8784        2175 :                 case DW_LNE_set_address:
    8785        2175 :                   op_index = 0;
    8786        2175 :                   if (unlikely ((size_t) (lineendp - linep) < address_size))
    8787             :                     goto invalid_unit;
    8788        2175 :                   if (address_size == 4)
    8789          17 :                     address = read_4ubyte_unaligned_inc (dbg, linep);
    8790             :                   else
    8791        2158 :                     address = read_8ubyte_unaligned_inc (dbg, linep);
    8792             :                   {
    8793        2175 :                     printf (gettext (" set address to "));
    8794        2175 :                     print_dwarf_addr (dwflmod, 0, address, address);
    8795        2175 :                     printf ("\n");
    8796             :                   }
    8797             :                   break;
    8798             : 
    8799           0 :                 case DW_LNE_define_file:
    8800             :                   {
    8801           0 :                     char *fname = (char *) linep;
    8802           0 :                     unsigned char *endp = memchr (linep, '\0',
    8803           0 :                                                   lineendp - linep);
    8804           0 :                     if (unlikely (endp == NULL))
    8805             :                       goto invalid_unit;
    8806           0 :                     linep = endp + 1;
    8807             : 
    8808           0 :                     unsigned int diridx;
    8809           0 :                     if (lineendp - linep < 1)
    8810             :                       goto invalid_unit;
    8811           0 :                     get_uleb128 (diridx, linep, lineendp);
    8812           0 :                     Dwarf_Word mtime;
    8813           0 :                     if (lineendp - linep < 1)
    8814             :                       goto invalid_unit;
    8815           0 :                     get_uleb128 (mtime, linep, lineendp);
    8816           0 :                     Dwarf_Word filelength;
    8817           0 :                     if (lineendp - linep < 1)
    8818             :                       goto invalid_unit;
    8819           0 :                     get_uleb128 (filelength, linep, lineendp);
    8820             : 
    8821           0 :                     printf (gettext ("\
    8822             :  define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
    8823             :                             diridx, (uint64_t) mtime, (uint64_t) filelength,
    8824             :                             fname);
    8825             :                   }
    8826             :                   break;
    8827             : 
    8828       43437 :                 case DW_LNE_set_discriminator:
    8829             :                   /* Takes one ULEB128 parameter, the discriminator.  */
    8830       43437 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    8831             :                                 || lineendp - linep < 1))
    8832             :                     goto invalid_unit;
    8833             : 
    8834       43437 :                   get_uleb128 (u128, linep, lineendp);
    8835       43437 :                   printf (gettext (" set discriminator to %u\n"), u128);
    8836             :                   break;
    8837             : 
    8838           0 :                 default:
    8839             :                   /* Unknown, ignore it.  */
    8840           0 :                   puts (gettext (" unknown opcode"));
    8841           0 :                   linep += len - 1;
    8842           0 :                   break;
    8843             :                 }
    8844             :             }
    8845      718326 :           else if (opcode <= DW_LNS_set_isa)
    8846             :             {
    8847             :               /* This is a known standard opcode.  */
    8848      718326 :               switch (opcode)
    8849             :                 {
    8850      109694 :                 case DW_LNS_copy:
    8851             :                   /* Takes no argument.  */
    8852      109694 :                   puts (gettext (" copy"));
    8853      109694 :                   break;
    8854             : 
    8855        4312 :                 case DW_LNS_advance_pc:
    8856             :                   /* Takes one uleb128 parameter which is added to the
    8857             :                      address.  */
    8858        4312 :                   if (lineendp - linep < 1)
    8859             :                     goto invalid_unit;
    8860        4312 :                   get_uleb128 (u128, linep, lineendp);
    8861        4312 :                   advance_pc (u128);
    8862             :                   {
    8863        4312 :                     printf (gettext (" advance address by %u to "),
    8864             :                             op_addr_advance);
    8865        4312 :                     print_dwarf_addr (dwflmod, 0, address, address);
    8866        4312 :                     if (show_op_index)
    8867           0 :                       printf (gettext (", op_index to %u"), op_index);
    8868        4312 :                     printf ("\n");
    8869             :                   }
    8870             :                   break;
    8871             : 
    8872       79738 :                 case DW_LNS_advance_line:
    8873             :                   /* Takes one sleb128 parameter which is added to the
    8874             :                      line.  */
    8875       79738 :                   if (lineendp - linep < 1)
    8876             :                     goto invalid_unit;
    8877       79738 :                   get_sleb128 (s128, linep, lineendp);
    8878       79738 :                   line += s128;
    8879       79738 :                   printf (gettext ("\
    8880             :  advance line by constant %d to %" PRId64 "\n"),
    8881             :                           s128, (int64_t) line);
    8882             :                   break;
    8883             : 
    8884       29663 :                 case DW_LNS_set_file:
    8885             :                   /* Takes one uleb128 parameter which is stored in file.  */
    8886       29663 :                   if (lineendp - linep < 1)
    8887             :                     goto invalid_unit;
    8888       29663 :                   get_uleb128 (u128, linep, lineendp);
    8889       29663 :                   printf (gettext (" set file to %" PRIu64 "\n"),
    8890             :                           (uint64_t) u128);
    8891             :                   break;
    8892             : 
    8893      278402 :                 case DW_LNS_set_column:
    8894             :                   /* Takes one uleb128 parameter which is stored in column.  */
    8895      278402 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    8896             :                                 || lineendp - linep < 1))
    8897             :                     goto invalid_unit;
    8898             : 
    8899      278402 :                   get_uleb128 (u128, linep, lineendp);
    8900      278402 :                   printf (gettext (" set column to %" PRIu64 "\n"),
    8901             :                           (uint64_t) u128);
    8902             :                   break;
    8903             : 
    8904      190470 :                 case DW_LNS_negate_stmt:
    8905             :                   /* Takes no argument.  */
    8906      190470 :                   is_stmt = 1 - is_stmt;
    8907      190470 :                   printf (gettext (" set '%s' to %" PRIuFAST8 "\n"),
    8908             :                           "is_stmt", is_stmt);
    8909             :                   break;
    8910             : 
    8911           0 :                 case DW_LNS_set_basic_block:
    8912             :                   /* Takes no argument.  */
    8913           0 :                   puts (gettext (" set basic block flag"));
    8914           0 :                   break;
    8915             : 
    8916       26024 :                 case DW_LNS_const_add_pc:
    8917             :                   /* Takes no argument.  */
    8918             : 
    8919       26024 :                   if (unlikely (line_range == 0))
    8920             :                     goto invalid_unit;
    8921             : 
    8922       26024 :                   advance_pc ((255 - opcode_base) / line_range);
    8923             :                   {
    8924       26024 :                     printf (gettext (" advance address by constant %u to "),
    8925             :                             op_addr_advance);
    8926       26024 :                     print_dwarf_addr (dwflmod, 0, address, address);
    8927       26024 :                     if (show_op_index)
    8928           0 :                       printf (gettext (", op_index to %u"), op_index);
    8929       26024 :                     printf ("\n");
    8930             :                   }
    8931             :                   break;
    8932             : 
    8933          22 :                 case DW_LNS_fixed_advance_pc:
    8934             :                   /* Takes one 16 bit parameter which is added to the
    8935             :                      address.  */
    8936          22 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    8937             :                                 || lineendp - linep < 2))
    8938             :                     goto invalid_unit;
    8939             : 
    8940          22 :                   u128 = read_2ubyte_unaligned_inc (dbg, linep);
    8941          22 :                   address += u128;
    8942          22 :                   op_index = 0;
    8943             :                   {
    8944          22 :                     printf (gettext ("\
    8945             :  advance address by fixed value %u to \n"),
    8946             :                             u128);
    8947          22 :                     print_dwarf_addr (dwflmod, 0, address, address);
    8948          22 :                     printf ("\n");
    8949             :                   }
    8950             :                   break;
    8951             : 
    8952           1 :                 case DW_LNS_set_prologue_end:
    8953             :                   /* Takes no argument.  */
    8954           1 :                   puts (gettext (" set prologue end flag"));
    8955           1 :                   break;
    8956             : 
    8957           0 :                 case DW_LNS_set_epilogue_begin:
    8958             :                   /* Takes no argument.  */
    8959           0 :                   puts (gettext (" set epilogue begin flag"));
    8960           0 :                   break;
    8961             : 
    8962           0 :                 case DW_LNS_set_isa:
    8963             :                   /* Takes one uleb128 parameter which is stored in isa.  */
    8964           0 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    8965             :                                 || lineendp - linep < 1))
    8966             :                     goto invalid_unit;
    8967             : 
    8968           0 :                   get_uleb128 (u128, linep, lineendp);
    8969           0 :                   printf (gettext (" set isa to %u\n"), u128);
    8970             :                   break;
    8971             :                 }
    8972             :             }
    8973             :           else
    8974             :             {
    8975             :               /* This is a new opcode the generator but not we know about.
    8976             :                  Read the parameters associated with it but then discard
    8977             :                  everything.  Read all the parameters for this opcode.  */
    8978           0 :               printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
    8979             :                                 " unknown opcode with %" PRIu8 " parameters:",
    8980             :                                 standard_opcode_lengths[opcode]),
    8981           0 :                       standard_opcode_lengths[opcode]);
    8982           0 :               for (int n = standard_opcode_lengths[opcode];
    8983           0 :                    n > 0 && linep < lineendp; --n)
    8984             :                 {
    8985           0 :                   get_uleb128 (u128, linep, lineendp);
    8986           0 :                   if (n != standard_opcode_lengths[opcode])
    8987           0 :                     putc_unlocked (',', stdout);
    8988           0 :                   printf (" %u", u128);
    8989             :                 }
    8990             : 
    8991             :               /* Next round, ignore this opcode.  */
    8992             :               continue;
    8993             :             }
    8994             :         }
    8995             :     }
    8996             : 
    8997             :   /* There must only be one data block.  */
    8998          46 :   assert (elf_getdata (scn, data) == NULL);
    8999             : }
    9000             : 
    9001             : 
    9002             : static void
    9003           3 : print_debug_loclists_section (Dwfl_Module *dwflmod,
    9004             :                               Ebl *ebl,
    9005             :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
    9006             :                               Elf_Scn *scn, GElf_Shdr *shdr,
    9007             :                               Dwarf *dbg)
    9008             : {
    9009           3 :   printf (gettext ("\
    9010             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    9011             :           elf_ndxscn (scn), section_name (ebl, shdr),
    9012           3 :           (uint64_t) shdr->sh_offset);
    9013             : 
    9014           6 :   Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists]
    9015           3 :                     ?: elf_rawdata (scn, NULL));
    9016           3 :   if (unlikely (data == NULL))
    9017             :     {
    9018           0 :       error (0, 0, gettext ("cannot get .debug_loclists content: %s"),
    9019             :              elf_errmsg (-1));
    9020           0 :       return;
    9021             :     }
    9022             : 
    9023             :   /* For the listptr to get the base address/CU.  */
    9024           3 :   sort_listptr (&known_loclistsptr, "loclistsptr");
    9025           3 :   size_t listptr_idx = 0;
    9026             : 
    9027           3 :   const unsigned char *readp = data->d_buf;
    9028           6 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    9029           3 :                                         + data->d_size);
    9030           7 :   while (readp < dataend)
    9031             :     {
    9032           4 :       if (unlikely (readp > dataend - 4))
    9033             :         {
    9034           0 :         invalid_data:
    9035           0 :           error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
    9036             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    9037           0 :           return;
    9038             :         }
    9039             : 
    9040           4 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    9041           4 :       printf (gettext ("Table at Offset 0x%" PRIx64 ":\n\n"),
    9042             :               (uint64_t) offset);
    9043             : 
    9044           4 :       uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    9045           4 :       unsigned int offset_size = 4;
    9046           4 :       if (unlikely (unit_length == 0xffffffff))
    9047             :         {
    9048           0 :           if (unlikely (readp > dataend - 8))
    9049             :             goto invalid_data;
    9050             : 
    9051           0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    9052           0 :           offset_size = 8;
    9053             :         }
    9054           4 :       printf (gettext (" Length:         %8" PRIu64 "\n"), unit_length);
    9055             : 
    9056             :       /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
    9057             :          bytes to complete the header.  And this unit cannot go beyond
    9058             :          the section data.  */
    9059           4 :       if (readp > dataend - 8
    9060           4 :           || unit_length < 8
    9061           4 :           || unit_length > (uint64_t) (dataend - readp))
    9062             :         goto invalid_data;
    9063             : 
    9064           4 :       const unsigned char *nexthdr = readp + unit_length;
    9065             : 
    9066           4 :       uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    9067           4 :       printf (gettext (" DWARF version:  %8" PRIu16 "\n"), version);
    9068             : 
    9069           4 :       if (version != 5)
    9070             :         {
    9071           0 :           error (0, 0, gettext ("Unknown version"));
    9072           0 :           goto next_table;
    9073             :         }
    9074             : 
    9075           4 :       uint8_t address_size = *readp++;
    9076           4 :       printf (gettext (" Address size:   %8" PRIu64 "\n"),
    9077             :               (uint64_t) address_size);
    9078             : 
    9079           4 :       if (address_size != 4 && address_size != 8)
    9080             :         {
    9081           0 :           error (0, 0, gettext ("unsupported address size"));
    9082           0 :           goto next_table;
    9083             :         }
    9084             : 
    9085           4 :       uint8_t segment_size = *readp++;
    9086           4 :       printf (gettext (" Segment size:   %8" PRIu64 "\n"),
    9087             :               (uint64_t) segment_size);
    9088             : 
    9089           4 :       if (segment_size != 0)
    9090             :         {
    9091           0 :           error (0, 0, gettext ("unsupported segment size"));
    9092           0 :           goto next_table;
    9093             :         }
    9094             : 
    9095           4 :       uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
    9096           4 :       printf (gettext (" Offset entries: %8" PRIu64 "\n"),
    9097             :               (uint64_t) offset_entry_count);
    9098             : 
    9099             :       /* We need the CU that uses this unit to get the initial base address. */
    9100           4 :       Dwarf_Addr cu_base = 0;
    9101           4 :       struct Dwarf_CU *cu = NULL;
    9102           4 :       if (listptr_cu (&known_loclistsptr, &listptr_idx,
    9103             :                       (Dwarf_Off) offset,
    9104           4 :                       (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
    9105             :                       &cu_base, &cu)
    9106           0 :           || split_dwarf_cu_base (dbg, &cu, &cu_base))
    9107           4 :         {
    9108           4 :           Dwarf_Die cudie;
    9109           4 :           if (dwarf_cu_die (cu, &cudie,
    9110             :                             NULL, NULL, NULL, NULL,
    9111             :                             NULL, NULL) == NULL)
    9112           0 :             printf (gettext (" Unknown CU base: "));
    9113             :           else
    9114           4 :             printf (gettext (" CU [%6" PRIx64 "] base: "),
    9115             :                     dwarf_dieoffset (&cudie));
    9116           4 :           print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
    9117           4 :           printf ("\n");
    9118             :         }
    9119             :       else
    9120           0 :         printf (gettext (" Not associated with a CU.\n"));
    9121             : 
    9122           4 :       printf ("\n");
    9123             : 
    9124           4 :       const unsigned char *offset_array_start = readp;
    9125           4 :       if (offset_entry_count > 0)
    9126             :         {
    9127           2 :           uint64_t max_entries = (unit_length - 8) / offset_size;
    9128           2 :           if (offset_entry_count > max_entries)
    9129             :             {
    9130           0 :               error (0, 0,
    9131           0 :                      gettext ("too many offset entries for unit length"));
    9132           0 :               offset_entry_count = max_entries;
    9133             :             }
    9134             : 
    9135           6 :           printf (gettext ("  Offsets starting at 0x%" PRIx64 ":\n"),
    9136             :                   (uint64_t) (offset_array_start
    9137           2 :                               - (unsigned char *) data->d_buf));
    9138          18 :           for (uint32_t idx = 0; idx < offset_entry_count; idx++)
    9139             :             {
    9140          16 :               printf ("   [%6" PRIu32 "] ", idx);
    9141          16 :               if (offset_size == 4)
    9142             :                 {
    9143          16 :                   uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
    9144          16 :                   printf ("0x%" PRIx32 "\n", off);
    9145             :                 }
    9146             :               else
    9147             :                 {
    9148           0 :                   uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
    9149           0 :                   printf ("0x%" PRIx64 "\n", off);
    9150             :                 }
    9151             :             }
    9152           2 :           printf ("\n");
    9153             :         }
    9154             : 
    9155           4 :       Dwarf_Addr base = cu_base;
    9156           4 :       bool start_of_list = true;
    9157          93 :       while (readp < nexthdr)
    9158             :         {
    9159          89 :           uint8_t kind = *readp++;
    9160          89 :           uint64_t op1, op2, len;
    9161             : 
    9162             :           /* Skip padding.  */
    9163          89 :           if (start_of_list && kind == DW_LLE_end_of_list)
    9164             :             continue;
    9165             : 
    9166          89 :           if (start_of_list)
    9167             :             {
    9168          32 :               base = cu_base;
    9169         128 :               printf ("  Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
    9170          32 :                       (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
    9171          32 :                       (uint64_t) (readp - offset_array_start - 1));
    9172          32 :               start_of_list = false;
    9173             :             }
    9174             : 
    9175          89 :           printf ("    %s", dwarf_loc_list_encoding_name (kind));
    9176          89 :           switch (kind)
    9177             :             {
    9178          32 :             case DW_LLE_end_of_list:
    9179          32 :               start_of_list = true;
    9180          32 :               printf ("\n\n");
    9181             :               break;
    9182             : 
    9183           0 :             case DW_LLE_base_addressx:
    9184           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9185             :                 {
    9186           0 :                 invalid_entry:
    9187           0 :                   error (0, 0, gettext ("invalid loclists data"));
    9188           0 :                   goto next_table;
    9189             :                 }
    9190           0 :               get_uleb128 (op1, readp, nexthdr);
    9191           0 :               printf (" %" PRIx64 "\n", op1);
    9192           0 :               if (! print_unresolved_addresses)
    9193             :                 {
    9194           0 :                   Dwarf_Addr addr;
    9195           0 :                   if (get_indexed_addr (cu, op1, &addr) != 0)
    9196           0 :                     printf ("      ???\n");
    9197             :                   else
    9198             :                     {
    9199           0 :                       printf ("      ");
    9200           0 :                       print_dwarf_addr (dwflmod, address_size, addr, addr);
    9201           0 :                       printf ("\n");
    9202             :                     }
    9203             :                 }
    9204             :               break;
    9205             : 
    9206           0 :             case DW_LLE_startx_endx:
    9207           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9208             :                 goto invalid_entry;
    9209           0 :               get_uleb128 (op1, readp, nexthdr);
    9210           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9211             :                 goto invalid_entry;
    9212           0 :               get_uleb128 (op2, readp, nexthdr);
    9213           0 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    9214           0 :               if (! print_unresolved_addresses)
    9215             :                 {
    9216           0 :                   Dwarf_Addr addr1;
    9217           0 :                   Dwarf_Addr addr2;
    9218           0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0
    9219           0 :                       || get_indexed_addr (cu, op2, &addr2) != 0)
    9220             :                     {
    9221           0 :                       printf ("      ???..\n");
    9222           0 :                       printf ("      ???\n");
    9223             :                     }
    9224             :                   else
    9225             :                     {
    9226           0 :                       printf ("      ");
    9227           0 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1);
    9228           0 :                       printf ("..\n      ");
    9229           0 :                       print_dwarf_addr (dwflmod, address_size,
    9230             :                                         addr2 - 1, addr2);
    9231           0 :                       printf ("\n");
    9232             :                     }
    9233             :                 }
    9234           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9235             :                 goto invalid_entry;
    9236           0 :               get_uleb128 (len, readp, nexthdr);
    9237           0 :               if ((uint64_t) (nexthdr - readp) < len)
    9238             :                 goto invalid_entry;
    9239           0 :               print_ops (dwflmod, dbg, 8, 8, version,
    9240             :                          address_size, offset_size, cu, len, readp);
    9241           0 :               readp += len;
    9242           0 :               break;
    9243             : 
    9244          26 :             case DW_LLE_startx_length:
    9245          26 :               if ((uint64_t) (nexthdr - readp) < 1)
    9246             :                 goto invalid_entry;
    9247          26 :               get_uleb128 (op1, readp, nexthdr);
    9248          26 :               if ((uint64_t) (nexthdr - readp) < 1)
    9249             :                 goto invalid_entry;
    9250          26 :               get_uleb128 (op2, readp, nexthdr);
    9251          26 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    9252          26 :               if (! print_unresolved_addresses)
    9253             :                 {
    9254          26 :                   Dwarf_Addr addr1;
    9255          26 :                   Dwarf_Addr addr2;
    9256          26 :                   if (get_indexed_addr (cu, op1, &addr1) != 0)
    9257             :                     {
    9258           0 :                       printf ("      ???..\n");
    9259           0 :                       printf ("      ???\n");
    9260             :                     }
    9261             :                   else
    9262             :                     {
    9263          26 :                       addr2 = addr1 + op2;
    9264          26 :                       printf ("      ");
    9265          26 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1);
    9266          26 :                       printf ("..\n      ");
    9267          26 :                       print_dwarf_addr (dwflmod, address_size,
    9268             :                                         addr2 - 1, addr2);
    9269          26 :                       printf ("\n");
    9270             :                     }
    9271             :                 }
    9272          26 :               if ((uint64_t) (nexthdr - readp) < 1)
    9273             :                 goto invalid_entry;
    9274          26 :               get_uleb128 (len, readp, nexthdr);
    9275          26 :               if ((uint64_t) (nexthdr - readp) < len)
    9276             :                 goto invalid_entry;
    9277          26 :               print_ops (dwflmod, dbg, 8, 8, version,
    9278             :                          address_size, offset_size, cu, len, readp);
    9279          26 :               readp += len;
    9280          26 :               break;
    9281             : 
    9282          24 :             case DW_LLE_offset_pair:
    9283          24 :               if ((uint64_t) (nexthdr - readp) < 1)
    9284             :                 goto invalid_entry;
    9285          24 :               get_uleb128 (op1, readp, nexthdr);
    9286          24 :               if ((uint64_t) (nexthdr - readp) < 1)
    9287             :                 goto invalid_entry;
    9288          24 :               get_uleb128 (op2, readp, nexthdr);
    9289          24 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    9290          24 :               if (! print_unresolved_addresses)
    9291             :                 {
    9292          24 :                   op1 += base;
    9293          24 :                   op2 += base;
    9294          24 :                   printf ("      ");
    9295          24 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    9296          24 :                   printf ("..\n      ");
    9297          24 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    9298          24 :                   printf ("\n");
    9299             :                 }
    9300          24 :               if ((uint64_t) (nexthdr - readp) < 1)
    9301             :                 goto invalid_entry;
    9302          24 :               get_uleb128 (len, readp, nexthdr);
    9303          24 :               if ((uint64_t) (nexthdr - readp) < len)
    9304             :                 goto invalid_entry;
    9305          24 :               print_ops (dwflmod, dbg, 8, 8, version,
    9306             :                          address_size, offset_size, cu, len, readp);
    9307          24 :               readp += len;
    9308          24 :               break;
    9309             : 
    9310           0 :             case DW_LLE_default_location:
    9311           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9312             :                 goto invalid_entry;
    9313           0 :               get_uleb128 (len, readp, nexthdr);
    9314           0 :               if ((uint64_t) (nexthdr - readp) < len)
    9315             :                 goto invalid_entry;
    9316           0 :               print_ops (dwflmod, dbg, 8, 8, version,
    9317             :                          address_size, offset_size, cu, len, readp);
    9318           0 :               readp += len;
    9319           0 :               break;
    9320             : 
    9321           5 :             case DW_LLE_base_address:
    9322           5 :               if (address_size == 4)
    9323             :                 {
    9324           0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    9325             :                     goto invalid_entry;
    9326           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    9327             :                 }
    9328             :               else
    9329             :                 {
    9330           5 :                   if ((uint64_t) (nexthdr - readp) < 8)
    9331             :                     goto invalid_entry;
    9332           5 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    9333             :                 }
    9334           5 :               base = op1;
    9335           5 :               printf (" 0x%" PRIx64 "\n", base);
    9336           5 :               if (! print_unresolved_addresses)
    9337             :                 {
    9338           5 :                   printf ("      ");
    9339           5 :                   print_dwarf_addr (dwflmod, address_size, base, base);
    9340           5 :                   printf ("\n");
    9341             :                 }
    9342             :               break;
    9343             : 
    9344           0 :             case DW_LLE_start_end:
    9345           0 :               if (address_size == 4)
    9346             :                 {
    9347           0 :                   if ((uint64_t) (nexthdr - readp) < 8)
    9348             :                     goto invalid_entry;
    9349           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    9350           0 :                   op2 = read_4ubyte_unaligned_inc (dbg, readp);
    9351             :                 }
    9352             :               else
    9353             :                 {
    9354           0 :                   if ((uint64_t) (nexthdr - readp) < 16)
    9355             :                     goto invalid_entry;
    9356           0 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    9357           0 :                   op2 = read_8ubyte_unaligned_inc (dbg, readp);
    9358             :                 }
    9359           0 :               printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
    9360           0 :               if (! print_unresolved_addresses)
    9361             :                 {
    9362           0 :                   printf ("      ");
    9363           0 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    9364           0 :                   printf ("..\n      ");
    9365           0 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    9366           0 :                   printf ("\n");
    9367             :                 }
    9368           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9369             :                 goto invalid_entry;
    9370           0 :               get_uleb128 (len, readp, nexthdr);
    9371           0 :               if ((uint64_t) (nexthdr - readp) < len)
    9372             :                 goto invalid_entry;
    9373           0 :               print_ops (dwflmod, dbg, 8, 8, version,
    9374             :                          address_size, offset_size, cu, len, readp);
    9375           0 :               readp += len;
    9376           0 :               break;
    9377             : 
    9378           2 :             case DW_LLE_start_length:
    9379           2 :               if (address_size == 4)
    9380             :                 {
    9381           0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    9382             :                     goto invalid_entry;
    9383           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    9384             :                 }
    9385             :               else
    9386             :                 {
    9387           2 :                   if ((uint64_t) (nexthdr - readp) < 8)
    9388             :                     goto invalid_entry;
    9389           2 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    9390             :                 }
    9391           2 :               if ((uint64_t) (nexthdr - readp) < 1)
    9392             :                 goto invalid_entry;
    9393           2 :               get_uleb128 (op2, readp, nexthdr);
    9394           2 :               printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
    9395           2 :               if (! print_unresolved_addresses)
    9396             :                 {
    9397           2 :                   op2 = op1 + op2;
    9398           2 :                   printf ("      ");
    9399           2 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    9400           2 :                   printf ("..\n      ");
    9401           2 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    9402           2 :                   printf ("\n");
    9403             :                 }
    9404           2 :               if ((uint64_t) (nexthdr - readp) < 1)
    9405             :                 goto invalid_entry;
    9406           2 :               get_uleb128 (len, readp, nexthdr);
    9407           2 :               if ((uint64_t) (nexthdr - readp) < len)
    9408             :                 goto invalid_entry;
    9409           2 :               print_ops (dwflmod, dbg, 8, 8, version,
    9410             :                          address_size, offset_size, cu, len, readp);
    9411           2 :               readp += len;
    9412           2 :               break;
    9413             : 
    9414             :             default:
    9415             :               goto invalid_entry;
    9416             :             }
    9417             :         }
    9418             : 
    9419           4 :     next_table:
    9420           4 :       if (readp != nexthdr)
    9421             :         {
    9422           0 :           size_t padding = nexthdr - readp;
    9423           0 :           printf (gettext ("   %zu padding bytes\n\n"), padding);
    9424           0 :           readp = nexthdr;
    9425             :         }
    9426             :     }
    9427             : }
    9428             : 
    9429             : 
    9430             : static void
    9431          42 : print_debug_loc_section (Dwfl_Module *dwflmod,
    9432             :                          Ebl *ebl, GElf_Ehdr *ehdr,
    9433             :                          Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    9434             : {
    9435          84 :   Elf_Data *data = (dbg->sectiondata[IDX_debug_loc]
    9436          42 :                     ?: elf_rawdata (scn, NULL));
    9437             : 
    9438          42 :   if (unlikely (data == NULL))
    9439             :     {
    9440           0 :       error (0, 0, gettext ("cannot get .debug_loc content: %s"),
    9441             :              elf_errmsg (-1));
    9442           0 :       return;
    9443             :     }
    9444             : 
    9445          42 :   printf (gettext ("\
    9446             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    9447             :           elf_ndxscn (scn), section_name (ebl, shdr),
    9448          42 :           (uint64_t) shdr->sh_offset);
    9449             : 
    9450          42 :   sort_listptr (&known_locsptr, "loclistptr");
    9451          42 :   size_t listptr_idx = 0;
    9452             : 
    9453          42 :   uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    9454          42 :   uint_fast8_t offset_size = 4;
    9455             : 
    9456          42 :   bool first = true;
    9457          42 :   Dwarf_Addr base = 0;
    9458          42 :   unsigned char *readp = data->d_buf;
    9459          42 :   unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
    9460          42 :   Dwarf_CU *last_cu = NULL;
    9461      350362 :   while (readp < endp)
    9462             :     {
    9463      350320 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    9464      350320 :       Dwarf_CU *cu = last_cu;
    9465      350320 :       unsigned int attr = 0;
    9466             : 
    9467      350320 :       if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
    9468             :                                       &address_size, &offset_size, &base,
    9469             :                                       &cu, offset, &readp, endp, &attr))
    9470       52680 :         continue;
    9471             : 
    9472      350320 :       if (last_cu != cu)
    9473             :        {
    9474        1462 :         Dwarf_Die cudie;
    9475        1462 :         if (dwarf_cu_die (cu, &cudie,
    9476             :                           NULL, NULL, NULL, NULL,
    9477             :                           NULL, NULL) == NULL)
    9478           0 :           printf (gettext ("\n Unknown CU base: "));
    9479             :         else
    9480        1462 :           printf (gettext ("\n CU [%6" PRIx64 "] base: "),
    9481             :                   dwarf_dieoffset (&cudie));
    9482        1462 :         print_dwarf_addr (dwflmod, address_size, base, base);
    9483        1462 :         printf ("\n");
    9484             :        }
    9485      350320 :       last_cu = cu;
    9486             : 
    9487      350320 :       if (attr == DW_AT_GNU_locviews)
    9488             :         {
    9489       52680 :           Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
    9490             :                                                     listptr_idx);
    9491       52680 :           const unsigned char *locp = readp;
    9492       52680 :           const unsigned char *locendp;
    9493       52680 :           if (next_off == 0
    9494       52680 :               || next_off > (size_t) (endp
    9495       52680 :                                       - (const unsigned char *) data->d_buf))
    9496             :             locendp = endp;
    9497             :           else
    9498       52680 :             locendp = (const unsigned char *) data->d_buf + next_off;
    9499             : 
    9500      297421 :           while (locp < locendp)
    9501             :             {
    9502      244741 :               uint64_t v1, v2;
    9503      244741 :               get_uleb128 (v1, locp, locendp);
    9504      244741 :               if (locp >= locendp)
    9505             :                 {
    9506           0 :                   printf (gettext (" [%6tx]  <INVALID DATA>\n"), offset);
    9507             :                   break;
    9508             :                 }
    9509      244741 :               get_uleb128 (v2, locp, locendp);
    9510      244741 :               if (first)                /* First view pair in a list.  */
    9511       52680 :                 printf (" [%6tx] ", offset);
    9512             :               else
    9513      192061 :                 printf ("          ");
    9514      244741 :               printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
    9515      244741 :               first = false;
    9516             :             }
    9517             : 
    9518       52680 :           first = true;
    9519       52680 :           readp = (unsigned char *) locendp;
    9520       52680 :           continue;
    9521             :         }
    9522             : 
    9523             :       /* GNU DebugFission encoded addresses as addrx.  */
    9524      595280 :       bool is_debugfission = ((cu != NULL
    9525           0 :                                || split_dwarf_cu_base (dbg, &cu, &base))
    9526      297640 :                               && (cu->version < 5
    9527      297640 :                                   && cu->unit_type == DW_UT_split_compile));
    9528      297640 :       if (!is_debugfission
    9529      297580 :           && unlikely (data->d_size - offset < (size_t) address_size * 2))
    9530             :         {
    9531           0 :         invalid_data:
    9532           0 :           printf (gettext (" [%6tx]  <INVALID DATA>\n"), offset);
    9533           0 :           break;
    9534             :         }
    9535             : 
    9536      297640 :       Dwarf_Addr begin;
    9537      297640 :       Dwarf_Addr end;
    9538      297640 :       bool use_base = true;
    9539      297640 :       if (is_debugfission)
    9540             :         {
    9541          60 :           const unsigned char *locp = readp;
    9542          60 :           const unsigned char *locendp = readp + data->d_size;
    9543          60 :           if (locp >= locendp)
    9544           0 :             goto invalid_data;
    9545             : 
    9546          60 :           Dwarf_Word idx;
    9547          60 :           unsigned char code = *locp++;
    9548          60 :           switch (code)
    9549             :             {
    9550          20 :             case DW_LLE_GNU_end_of_list_entry:
    9551          20 :               begin = 0;
    9552          20 :               end = 0;
    9553          20 :               break;
    9554             : 
    9555           0 :             case DW_LLE_GNU_base_address_selection_entry:
    9556           0 :               if (locp >= locendp)
    9557           0 :                 goto invalid_data;
    9558           0 :               begin = (Dwarf_Addr) -1;
    9559           0 :               get_uleb128 (idx, locp, locendp);
    9560           0 :               if (get_indexed_addr (cu, idx, &end) != 0)
    9561           0 :                 end = idx; /* ... */
    9562             :               break;
    9563             : 
    9564           0 :             case DW_LLE_GNU_start_end_entry:
    9565           0 :               if (locp >= locendp)
    9566           0 :                 goto invalid_data;
    9567           0 :               get_uleb128 (idx, locp, locendp);
    9568           0 :               if (get_indexed_addr (cu, idx, &begin) != 0)
    9569           0 :                 begin = idx; /* ... */
    9570           0 :               if (locp >= locendp)
    9571           0 :                 goto invalid_data;
    9572           0 :               get_uleb128 (idx, locp, locendp);
    9573           0 :               if (get_indexed_addr (cu, idx, &end) != 0)
    9574           0 :                 end = idx; /* ... */
    9575             :               use_base = false;
    9576             :               break;
    9577             : 
    9578          40 :             case DW_LLE_GNU_start_length_entry:
    9579          40 :               if (locp >= locendp)
    9580           0 :                 goto invalid_data;
    9581          40 :               get_uleb128 (idx, locp, locendp);
    9582          40 :               if (get_indexed_addr (cu, idx, &begin) != 0)
    9583           0 :                 begin = idx; /* ... */
    9584          40 :               if (locendp - locp < 4)
    9585           0 :                 goto invalid_data;
    9586          40 :               end = read_4ubyte_unaligned_inc (dbg, locp);
    9587          40 :               end += begin;
    9588          40 :               use_base = false;
    9589          40 :               break;
    9590             : 
    9591           0 :             default:
    9592           0 :                 goto invalid_data;
    9593             :             }
    9594             : 
    9595          60 :           readp = (unsigned char *) locp;
    9596             :         }
    9597      297580 :       else if (address_size == 8)
    9598             :         {
    9599      297580 :           begin = read_8ubyte_unaligned_inc (dbg, readp);
    9600      297580 :           end = read_8ubyte_unaligned_inc (dbg, readp);
    9601             :         }
    9602             :       else
    9603             :         {
    9604           0 :           begin = read_4ubyte_unaligned_inc (dbg, readp);
    9605           0 :           end = read_4ubyte_unaligned_inc (dbg, readp);
    9606           0 :           if (begin == (Dwarf_Addr) (uint32_t) -1)
    9607           0 :             begin = (Dwarf_Addr) -1l;
    9608             :         }
    9609             : 
    9610      297640 :       if (begin == (Dwarf_Addr) -1l) /* Base address entry.  */
    9611             :         {
    9612           0 :           printf (gettext (" [%6tx] base address\n          "), offset);
    9613           0 :           print_dwarf_addr (dwflmod, address_size, end, end);
    9614           0 :           printf ("\n");
    9615           0 :           base = end;
    9616             :         }
    9617      297640 :       else if (begin == 0 && end == 0) /* End of list entry.  */
    9618             :         {
    9619       52754 :           if (first)
    9620           0 :             printf (gettext (" [%6tx] empty list\n"), offset);
    9621             :           first = true;
    9622             :         }
    9623             :       else
    9624             :         {
    9625             :           /* We have a location expression entry.  */
    9626      244886 :           uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
    9627             : 
    9628      244886 :           if (first)            /* First entry in a list.  */
    9629       52754 :             printf (" [%6tx] ", offset);
    9630             :           else
    9631      192132 :             printf ("          ");
    9632             : 
    9633      244886 :           printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
    9634      244886 :           if (! print_unresolved_addresses)
    9635             :             {
    9636      244856 :               Dwarf_Addr dab = use_base ? base + begin : begin;
    9637      244856 :               Dwarf_Addr dae = use_base ? base + end : end;
    9638      244856 :               printf ("          ");
    9639      244856 :               print_dwarf_addr (dwflmod, address_size, dab, dab);
    9640      244856 :               printf ("..\n          ");
    9641      244856 :               print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
    9642      244856 :               printf ("\n");
    9643             :             }
    9644             : 
    9645      244886 :           if (endp - readp <= (ptrdiff_t) len)
    9646             :             {
    9647           0 :               fputs (gettext ("   <INVALID DATA>\n"), stdout);
    9648           0 :               break;
    9649             :             }
    9650             : 
    9651      489772 :           print_ops (dwflmod, dbg, 11, 11,
    9652      244886 :                      cu != NULL ? cu->version : 3,
    9653             :                      address_size, offset_size, cu, len, readp);
    9654             : 
    9655      244886 :           first = false;
    9656      244886 :           readp += len;
    9657             :         }
    9658             :     }
    9659             : }
    9660             : 
    9661             : struct mac_culist
    9662             : {
    9663             :   Dwarf_Die die;
    9664             :   Dwarf_Off offset;
    9665             :   Dwarf_Files *files;
    9666             :   struct mac_culist *next;
    9667             : };
    9668             : 
    9669             : 
    9670             : static int
    9671           0 : mac_compare (const void *p1, const void *p2)
    9672             : {
    9673           0 :   struct mac_culist *m1 = (struct mac_culist *) p1;
    9674           0 :   struct mac_culist *m2 = (struct mac_culist *) p2;
    9675             : 
    9676           0 :   if (m1->offset < m2->offset)
    9677             :     return -1;
    9678           0 :   if (m1->offset > m2->offset)
    9679           0 :     return 1;
    9680             :   return 0;
    9681             : }
    9682             : 
    9683             : 
    9684             : static void
    9685           0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    9686             :                              Ebl *ebl,
    9687             :                              GElf_Ehdr *ehdr __attribute__ ((unused)),
    9688             :                              Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    9689             : {
    9690           0 :   printf (gettext ("\
    9691             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    9692             :           elf_ndxscn (scn), section_name (ebl, shdr),
    9693           0 :           (uint64_t) shdr->sh_offset);
    9694           0 :   putc_unlocked ('\n', stdout);
    9695             : 
    9696             :   /* There is no function in libdw to iterate over the raw content of
    9697             :      the section but it is easy enough to do.  */
    9698           0 :   Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo]
    9699           0 :                     ?: elf_rawdata (scn, NULL));
    9700           0 :   if (unlikely (data == NULL))
    9701             :     {
    9702           0 :       error (0, 0, gettext ("cannot get macro information section data: %s"),
    9703             :              elf_errmsg (-1));
    9704           0 :       return;
    9705             :     }
    9706             : 
    9707             :   /* Get the source file information for all CUs.  */
    9708           0 :   Dwarf_Off offset;
    9709           0 :   Dwarf_Off ncu = 0;
    9710           0 :   size_t hsize;
    9711           0 :   struct mac_culist *culist = NULL;
    9712           0 :   size_t nculist = 0;
    9713           0 :   while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
    9714             :     {
    9715           0 :       Dwarf_Die cudie;
    9716           0 :       if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
    9717           0 :         continue;
    9718             : 
    9719           0 :       Dwarf_Attribute attr;
    9720           0 :       if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
    9721             :         continue;
    9722             : 
    9723           0 :       Dwarf_Word macoff;
    9724           0 :       if (dwarf_formudata (&attr, &macoff) != 0)
    9725             :         continue;
    9726             : 
    9727           0 :       struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
    9728           0 :       newp->die = cudie;
    9729           0 :       newp->offset = macoff;
    9730           0 :       newp->files = NULL;
    9731           0 :       newp->next = culist;
    9732           0 :       culist = newp;
    9733           0 :       ++nculist;
    9734             :     }
    9735             : 
    9736             :   /* Convert the list into an array for easier consumption.  */
    9737           0 :   struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
    9738             :                                                          * sizeof (*cus));
    9739             :   /* Add sentinel.  */
    9740           0 :   cus[nculist].offset = data->d_size;
    9741           0 :   cus[nculist].files = (Dwarf_Files *) -1l;
    9742           0 :   if (nculist > 0)
    9743             :     {
    9744           0 :       for (size_t cnt = nculist - 1; culist != NULL; --cnt)
    9745             :         {
    9746           0 :           assert (cnt < nculist);
    9747           0 :           cus[cnt] = *culist;
    9748           0 :           culist = culist->next;
    9749             :         }
    9750             : 
    9751             :       /* Sort the array according to the offset in the .debug_macinfo
    9752             :          section.  Note we keep the sentinel at the end.  */
    9753           0 :       qsort (cus, nculist, sizeof (*cus), mac_compare);
    9754             :     }
    9755             : 
    9756           0 :   const unsigned char *readp = (const unsigned char *) data->d_buf;
    9757           0 :   const unsigned char *readendp = readp + data->d_size;
    9758           0 :   int level = 1;
    9759             : 
    9760           0 :   while (readp < readendp)
    9761             :     {
    9762           0 :       unsigned int opcode = *readp++;
    9763           0 :       unsigned int u128;
    9764           0 :       unsigned int u128_2;
    9765           0 :       const unsigned char *endp;
    9766             : 
    9767           0 :       switch (opcode)
    9768             :         {
    9769           0 :         case DW_MACINFO_define:
    9770             :         case DW_MACINFO_undef:
    9771             :         case DW_MACINFO_vendor_ext:
    9772             :           /*  For the first two opcodes the parameters are
    9773             :                 line, string
    9774             :               For the latter
    9775             :                 number, string.
    9776             :               We can treat these cases together.  */
    9777           0 :           get_uleb128 (u128, readp, readendp);
    9778             : 
    9779           0 :           endp = memchr (readp, '\0', readendp - readp);
    9780           0 :           if (unlikely (endp == NULL))
    9781             :             {
    9782           0 :               printf (gettext ("\
    9783             : %*s*** non-terminated string at end of section"),
    9784             :                       level, "");
    9785           0 :               return;
    9786             :             }
    9787             : 
    9788           0 :           if (opcode == DW_MACINFO_define)
    9789           0 :             printf ("%*s#define %s, line %u\n",
    9790             :                     level, "", (char *) readp, u128);
    9791           0 :           else if (opcode == DW_MACINFO_undef)
    9792           0 :             printf ("%*s#undef %s, line %u\n",
    9793             :                     level, "", (char *) readp, u128);
    9794             :           else
    9795           0 :             printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
    9796             : 
    9797           0 :           readp = endp + 1;
    9798           0 :           break;
    9799             : 
    9800           0 :         case DW_MACINFO_start_file:
    9801             :           /* The two parameters are line and file index, in this order.  */
    9802           0 :           get_uleb128 (u128, readp, readendp);
    9803           0 :           if (readendp - readp < 1)
    9804             :             {
    9805           0 :               printf (gettext ("\
    9806             : %*s*** missing DW_MACINFO_start_file argument at end of section"),
    9807             :                       level, "");
    9808           0 :               return;
    9809             :             }
    9810           0 :           get_uleb128 (u128_2, readp, readendp);
    9811             : 
    9812             :           /* Find the CU DIE for this file.  */
    9813           0 :           size_t macoff = readp - (const unsigned char *) data->d_buf;
    9814           0 :           const char *fname = "???";
    9815           0 :           if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
    9816             :             {
    9817           0 :               while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
    9818           0 :                 ++cus;
    9819             : 
    9820           0 :               if (cus[0].files == NULL
    9821           0 :                   && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
    9822           0 :                 cus[0].files = (Dwarf_Files *) -1l;
    9823             : 
    9824           0 :               if (cus[0].files != (Dwarf_Files *) -1l)
    9825           0 :                 fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
    9826           0 :                          ?: "???");
    9827             :             }
    9828             : 
    9829           0 :           printf ("%*sstart_file %u, [%u] %s\n",
    9830             :                   level, "", u128, u128_2, fname);
    9831           0 :           ++level;
    9832           0 :           break;
    9833             : 
    9834           0 :         case DW_MACINFO_end_file:
    9835           0 :           --level;
    9836           0 :           printf ("%*send_file\n", level, "");
    9837             :           /* Nothing more to do.  */
    9838             :           break;
    9839             : 
    9840           0 :         default:
    9841             :           // XXX gcc seems to generate files with a trailing zero.
    9842           0 :           if (unlikely (opcode != 0 || readp != readendp))
    9843           0 :             printf ("%*s*** invalid opcode %u\n", level, "", opcode);
    9844             :           break;
    9845             :         }
    9846             :     }
    9847             : }
    9848             : 
    9849             : 
    9850             : static void
    9851           3 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    9852             :                            Ebl *ebl,
    9853             :                            GElf_Ehdr *ehdr __attribute__ ((unused)),
    9854             :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    9855             : {
    9856           3 :   printf (gettext ("\
    9857             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    9858             :           elf_ndxscn (scn), section_name (ebl, shdr),
    9859           3 :           (uint64_t) shdr->sh_offset);
    9860           3 :   putc_unlocked ('\n', stdout);
    9861             : 
    9862           3 :   Elf_Data *data =  elf_getdata (scn, NULL);
    9863           3 :   if (unlikely (data == NULL))
    9864             :     {
    9865           0 :       error (0, 0, gettext ("cannot get macro information section data: %s"),
    9866             :              elf_errmsg (-1));
    9867           0 :       return;
    9868             :     }
    9869             : 
    9870             :   /* Get the source file information for all CUs.  Uses same
    9871             :      datastructure as macinfo.  But uses offset field to directly
    9872             :      match .debug_line offset.  And just stored in a list.  */
    9873           3 :   Dwarf_Off offset;
    9874           3 :   Dwarf_Off ncu = 0;
    9875           3 :   size_t hsize;
    9876           3 :   struct mac_culist *culist = NULL;
    9877           3 :   size_t nculist = 0;
    9878           7 :   while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
    9879             :     {
    9880           4 :       Dwarf_Die cudie;
    9881           4 :       if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
    9882           0 :         continue;
    9883             : 
    9884           4 :       Dwarf_Attribute attr;
    9885           4 :       if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
    9886             :         continue;
    9887             : 
    9888           4 :       Dwarf_Word lineoff;
    9889           4 :       if (dwarf_formudata (&attr, &lineoff) != 0)
    9890             :         continue;
    9891             : 
    9892           4 :       struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
    9893           4 :       newp->die = cudie;
    9894           4 :       newp->offset = lineoff;
    9895           4 :       newp->files = NULL;
    9896           4 :       newp->next = culist;
    9897           4 :       culist = newp;
    9898           4 :       ++nculist;
    9899             :     }
    9900             : 
    9901           3 :   const unsigned char *readp = (const unsigned char *) data->d_buf;
    9902           3 :   const unsigned char *readendp = readp + data->d_size;
    9903             : 
    9904          11 :   while (readp < readendp)
    9905             :     {
    9906          24 :       printf (gettext (" Offset:             0x%" PRIx64 "\n"),
    9907           8 :               (uint64_t) (readp - (const unsigned char *) data->d_buf));
    9908             : 
    9909             :       // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
    9910             :       // optional vendor extension macro entry table.
    9911           8 :       if (readp + 2 > readendp)
    9912             :         {
    9913           0 :         invalid_data:
    9914           0 :           error (0, 0, gettext ("invalid data"));
    9915           0 :           return;
    9916             :         }
    9917           8 :       const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
    9918           8 :       printf (gettext (" Version:            %" PRIu16 "\n"), vers);
    9919             : 
    9920             :       // Version 4 is the GNU extension for DWARF4.  DWARF5 will use version
    9921             :       // 5 when it gets standardized.
    9922           8 :       if (vers != 4 && vers != 5)
    9923             :         {
    9924           0 :           printf (gettext ("  unknown version, cannot parse section\n"));
    9925           0 :           return;
    9926             :         }
    9927             : 
    9928           8 :       if (readp + 1 > readendp)
    9929             :         goto invalid_data;
    9930           8 :       const unsigned char flag = *readp++;
    9931           8 :       printf (gettext (" Flag:               0x%" PRIx8), flag);
    9932           8 :       if (flag != 0)
    9933             :         {
    9934           4 :           printf (" (");
    9935           4 :           if ((flag & 0x01) != 0)
    9936             :             {
    9937           0 :               printf ("offset_size");
    9938           0 :               if ((flag & 0xFE) !=  0)
    9939           0 :                 printf (", ");
    9940             :             }
    9941           4 :           if ((flag & 0x02) != 0)
    9942             :             {
    9943           4 :               printf ("debug_line_offset");
    9944           4 :               if ((flag & 0xFC) !=  0)
    9945           0 :                 printf (", ");
    9946             :             }
    9947           4 :           if ((flag & 0x04) != 0)
    9948             :             {
    9949           0 :               printf ("operands_table");
    9950           0 :               if ((flag & 0xF8) !=  0)
    9951           0 :                 printf (", ");
    9952             :             }
    9953           4 :           if ((flag & 0xF8) != 0)
    9954           0 :             printf ("unknown");
    9955           4 :           printf (")");
    9956             :         }
    9957           8 :       printf ("\n");
    9958             : 
    9959           8 :       unsigned int offset_len = (flag & 0x01) ? 8 : 4;
    9960           8 :       printf (gettext (" Offset length:      %" PRIu8 "\n"), offset_len);
    9961           8 :       Dwarf_Off line_offset = -1;
    9962           8 :       if (flag & 0x02)
    9963             :         {
    9964           4 :           if (offset_len == 8)
    9965           0 :             line_offset = read_8ubyte_unaligned_inc (dbg, readp);
    9966             :           else
    9967           4 :             line_offset = read_4ubyte_unaligned_inc (dbg, readp);
    9968           4 :           printf (gettext (" .debug_line offset: 0x%" PRIx64 "\n"),
    9969             :                   line_offset);
    9970             :         }
    9971             : 
    9972           4 :       struct mac_culist *cu = NULL;
    9973           4 :       if (line_offset != (Dwarf_Off) -1)
    9974             :         {
    9975             :           cu = culist;
    9976           5 :           while (cu != NULL && line_offset != cu->offset)
    9977           1 :             cu = cu->next;
    9978             :         }
    9979             : 
    9980           8 :       Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
    9981             :                                                                ? cu->die.cu
    9982             :                                                                : NULL));
    9983             : 
    9984           8 :       const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
    9985           8 :       memset (vendor, 0, sizeof vendor);
    9986           8 :       if (flag & 0x04)
    9987             :         {
    9988             :           // 1 byte length, for each item, 1 byte opcode, uleb128 number
    9989             :           // of arguments, for each argument 1 byte form code.
    9990           0 :           if (readp + 1 > readendp)
    9991             :             goto invalid_data;
    9992           0 :           unsigned int tlen = *readp++;
    9993           0 :           printf (gettext ("  extension opcode table, %" PRIu8 " items:\n"),
    9994             :                   tlen);
    9995           0 :           for (unsigned int i = 0; i < tlen; i++)
    9996             :             {
    9997           0 :               if (readp + 1 > readendp)
    9998             :                 goto invalid_data;
    9999           0 :               unsigned int opcode = *readp++;
   10000           0 :               printf (gettext ("    [%" PRIx8 "]"), opcode);
   10001           0 :               if (opcode < DW_MACRO_lo_user
   10002           0 :                   || opcode > DW_MACRO_hi_user)
   10003             :                 goto invalid_data;
   10004             :               // Record the start of description for this vendor opcode.
   10005             :               // uleb128 nr args, 1 byte per arg form.
   10006           0 :               vendor[opcode - DW_MACRO_lo_user] = readp;
   10007           0 :               if (readp + 1 > readendp)
   10008             :                 goto invalid_data;
   10009           0 :               unsigned int args = *readp++;
   10010           0 :               if (args > 0)
   10011             :                 {
   10012           0 :                   printf (gettext (" %" PRIu8 " arguments:"), args);
   10013           0 :                   while (args > 0)
   10014             :                     {
   10015           0 :                       if (readp + 1 > readendp)
   10016             :                         goto invalid_data;
   10017           0 :                       unsigned int form = *readp++;
   10018           0 :                       printf (" %s", dwarf_form_name (form));
   10019           0 :                       if (! libdw_valid_user_form (form))
   10020             :                         goto invalid_data;
   10021           0 :                       args--;
   10022           0 :                       if (args > 0)
   10023           0 :                         putchar_unlocked (',');
   10024             :                     }
   10025             :                 }
   10026             :               else
   10027           0 :                 printf (gettext (" no arguments."));
   10028           0 :               putchar_unlocked ('\n');
   10029             :             }
   10030             :         }
   10031           8 :       putchar_unlocked ('\n');
   10032             : 
   10033           8 :       int level = 1;
   10034           8 :       if (readp + 1 > readendp)
   10035             :         goto invalid_data;
   10036           8 :       unsigned int opcode = *readp++;
   10037         735 :       while (opcode != 0)
   10038             :         {
   10039         727 :           unsigned int u128;
   10040         727 :           unsigned int u128_2;
   10041         727 :           const unsigned char *endp;
   10042         727 :           uint64_t off;
   10043             : 
   10044         727 :           switch (opcode)
   10045             :             {
   10046           6 :             case DW_MACRO_start_file:
   10047           6 :               get_uleb128 (u128, readp, readendp);
   10048           6 :               if (readp >= readendp)
   10049             :                 goto invalid_data;
   10050           6 :               get_uleb128 (u128_2, readp, readendp);
   10051             : 
   10052             :               /* Find the CU DIE that matches this line offset.  */
   10053           6 :               const char *fname = "???";
   10054           6 :               if (cu != NULL)
   10055             :                 {
   10056           6 :                   if (cu->files == NULL
   10057           4 :                       && dwarf_getsrcfiles (&cu->die, &cu->files,
   10058             :                                             NULL) != 0)
   10059           0 :                     cu->files = (Dwarf_Files *) -1l;
   10060             : 
   10061           6 :                   if (cu->files != (Dwarf_Files *) -1l)
   10062           6 :                     fname = (dwarf_filesrc (cu->files, u128_2,
   10063           6 :                                             NULL, NULL) ?: "???");
   10064             :                 }
   10065           6 :               printf ("%*sstart_file %u, [%u] %s\n",
   10066             :                       level, "", u128, u128_2, fname);
   10067           6 :               ++level;
   10068           6 :               break;
   10069             : 
   10070           6 :             case DW_MACRO_end_file:
   10071           6 :               --level;
   10072           6 :               printf ("%*send_file\n", level, "");
   10073             :               break;
   10074             : 
   10075           1 :             case DW_MACRO_define:
   10076           1 :               get_uleb128 (u128, readp, readendp);
   10077           1 :               endp = memchr (readp, '\0', readendp - readp);
   10078           1 :               if (endp == NULL)
   10079             :                 goto invalid_data;
   10080           1 :               printf ("%*s#define %s, line %u\n",
   10081             :                       level, "", readp, u128);
   10082           1 :               readp = endp + 1;
   10083           1 :               break;
   10084             : 
   10085           0 :             case DW_MACRO_undef:
   10086           0 :               get_uleb128 (u128, readp, readendp);
   10087           0 :               endp = memchr (readp, '\0', readendp - readp);
   10088           0 :               if (endp == NULL)
   10089             :                 goto invalid_data;
   10090           0 :               printf ("%*s#undef %s, line %u\n",
   10091             :                       level, "", readp, u128);
   10092           0 :               readp = endp + 1;
   10093           0 :               break;
   10094             : 
   10095         707 :             case DW_MACRO_define_strp:
   10096         707 :               get_uleb128 (u128, readp, readendp);
   10097         707 :               if (readp + offset_len > readendp)
   10098             :                 goto invalid_data;
   10099         707 :               if (offset_len == 8)
   10100           0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   10101             :               else
   10102         707 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   10103         707 :               printf ("%*s#define %s, line %u (indirect)\n",
   10104             :                       level, "", dwarf_getstring (dbg, off, NULL), u128);
   10105             :               break;
   10106             : 
   10107           1 :             case DW_MACRO_undef_strp:
   10108           1 :               get_uleb128 (u128, readp, readendp);
   10109           1 :               if (readp + offset_len > readendp)
   10110             :                 goto invalid_data;
   10111           1 :               if (offset_len == 8)
   10112           0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   10113             :               else
   10114           1 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   10115           1 :               printf ("%*s#undef %s, line %u (indirect)\n",
   10116             :                       level, "", dwarf_getstring (dbg, off, NULL), u128);
   10117             :               break;
   10118             : 
   10119           6 :             case DW_MACRO_import:
   10120           6 :               if (readp + offset_len > readendp)
   10121             :                 goto invalid_data;
   10122           6 :               if (offset_len == 8)
   10123           0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   10124             :               else
   10125           6 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   10126           6 :               printf ("%*s#include offset 0x%" PRIx64 "\n",
   10127             :                       level, "", off);
   10128             :               break;
   10129             : 
   10130           0 :             case DW_MACRO_define_sup:
   10131           0 :               get_uleb128 (u128, readp, readendp);
   10132           0 :               if (readp + offset_len > readendp)
   10133             :                 goto invalid_data;
   10134           0 :               printf ("%*s#define ", level, "");
   10135           0 :               readp =  print_form_data (dbg, DW_FORM_strp_sup,
   10136             :                                         readp, readendp, offset_len,
   10137             :                                         str_offsets_base);
   10138           0 :               printf (", line %u (sup)\n", u128);
   10139             :               break;
   10140             : 
   10141           0 :             case DW_MACRO_undef_sup:
   10142           0 :               get_uleb128 (u128, readp, readendp);
   10143           0 :               if (readp + offset_len > readendp)
   10144             :                 goto invalid_data;
   10145           0 :               printf ("%*s#undef ", level, "");
   10146           0 :               readp =  print_form_data (dbg, DW_FORM_strp_sup,
   10147             :                                         readp, readendp, offset_len,
   10148             :                                         str_offsets_base);
   10149           0 :               printf (", line %u (sup)\n", u128);
   10150             :               break;
   10151             : 
   10152           0 :             case DW_MACRO_import_sup:
   10153           0 :               if (readp + offset_len > readendp)
   10154             :                 goto invalid_data;
   10155           0 :               if (offset_len == 8)
   10156           0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   10157             :               else
   10158           0 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   10159             :               // XXX Needs support for reading from supplementary object file.
   10160           0 :               printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
   10161             :                       level, "", off);
   10162             :               break;
   10163             : 
   10164           0 :             case DW_MACRO_define_strx:
   10165           0 :               get_uleb128 (u128, readp, readendp);
   10166           0 :               if (readp + offset_len > readendp)
   10167             :                 goto invalid_data;
   10168           0 :               printf ("%*s#define ", level, "");
   10169           0 :               readp =  print_form_data (dbg, DW_FORM_strx,
   10170             :                                         readp, readendp, offset_len,
   10171             :                                         str_offsets_base);
   10172           0 :               printf (", line %u (strx)\n", u128);
   10173             :               break;
   10174             : 
   10175           0 :             case DW_MACRO_undef_strx:
   10176           0 :               get_uleb128 (u128, readp, readendp);
   10177           0 :               if (readp + offset_len > readendp)
   10178             :                 goto invalid_data;
   10179           0 :               printf ("%*s#undef ", level, "");
   10180           0 :               readp =  print_form_data (dbg, DW_FORM_strx,
   10181             :                                         readp, readendp, offset_len,
   10182             :                                         str_offsets_base);
   10183           0 :               printf (", line %u (strx)\n", u128);
   10184             :               break;
   10185             : 
   10186             :             default:
   10187           0 :               printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
   10188           0 :               if (opcode < DW_MACRO_lo_user
   10189             :                   || opcode > DW_MACRO_lo_user
   10190           0 :                   || vendor[opcode - DW_MACRO_lo_user] == NULL)
   10191             :                 goto invalid_data;
   10192             : 
   10193           0 :               const unsigned char *op_desc;
   10194           0 :               op_desc = vendor[opcode - DW_MACRO_lo_user];
   10195             : 
   10196             :               // Just skip the arguments, we cannot really interpret them,
   10197             :               // but print as much as we can.
   10198           0 :               unsigned int args = *op_desc++;
   10199           0 :               while (args > 0 && readp < readendp)
   10200           0 :                 {
   10201           0 :                   unsigned int form = *op_desc++;
   10202           0 :                   readp = print_form_data (dbg, form, readp, readendp,
   10203             :                                            offset_len, str_offsets_base);
   10204           0 :                   args--;
   10205           0 :                   if (args > 0)
   10206           0 :                     printf (", ");
   10207             :                 }
   10208           0 :               putchar_unlocked ('\n');
   10209             :             }
   10210             : 
   10211         727 :           if (readp + 1 > readendp)
   10212             :             goto invalid_data;
   10213         727 :           opcode = *readp++;
   10214         727 :           if (opcode == 0)
   10215           8 :             putchar_unlocked ('\n');
   10216             :         }
   10217             :     }
   10218             : }
   10219             : 
   10220             : 
   10221             : /* Callback for printing global names.  */
   10222             : static int
   10223          34 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
   10224             :                 void *arg)
   10225             : {
   10226          34 :   int *np = (int *) arg;
   10227             : 
   10228         102 :   printf (gettext (" [%5d] DIE offset: %6" PRId64
   10229             :                    ", CU DIE offset: %6" PRId64 ", name: %s\n"),
   10230          34 :           (*np)++, global->die_offset, global->cu_offset, global->name);
   10231             : 
   10232          34 :   return 0;
   10233             : }
   10234             : 
   10235             : 
   10236             : /* Print the known exported symbols in the DWARF section '.debug_pubnames'.  */
   10237             : static void
   10238           8 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10239             :                               Ebl *ebl,
   10240             :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
   10241             :                               Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10242             : {
   10243           8 :   printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10244             :           elf_ndxscn (scn), section_name (ebl, shdr),
   10245           8 :           (uint64_t) shdr->sh_offset);
   10246             : 
   10247           8 :   int n = 0;
   10248           8 :   (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
   10249           8 : }
   10250             : 
   10251             : /* Print the content of the DWARF string section '.debug_str'.  */
   10252             : static void
   10253          45 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10254             :                          Ebl *ebl,
   10255             :                          GElf_Ehdr *ehdr __attribute__ ((unused)),
   10256             :                          Elf_Scn *scn, GElf_Shdr *shdr,
   10257             :                          Dwarf *dbg __attribute__ ((unused)))
   10258             : {
   10259          45 :   Elf_Data *data = elf_rawdata (scn, NULL);
   10260          45 :   const size_t sh_size = data ? data->d_size : 0;
   10261             : 
   10262             :   /* Compute floor(log16(shdr->sh_size)).  */
   10263          45 :   GElf_Addr tmp = sh_size;
   10264          45 :   int digits = 1;
   10265         160 :   while (tmp >= 16)
   10266             :     {
   10267         115 :       ++digits;
   10268         115 :       tmp >>= 4;
   10269             :     }
   10270          45 :   digits = MAX (4, digits);
   10271             : 
   10272          45 :   printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
   10273             :                    " %*s  String\n"),
   10274             :           elf_ndxscn (scn),
   10275          45 :           section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
   10276             :           /* TRANS: the debugstr| prefix makes the string unique.  */
   10277          45 :           digits + 2, sgettext ("debugstr|Offset"));
   10278             : 
   10279          45 :   Dwarf_Off offset = 0;
   10280       74702 :   while (offset < sh_size)
   10281             :     {
   10282       74657 :       size_t len;
   10283       74657 :       const char *str = (const char *) data->d_buf + offset;
   10284       74657 :       const char *endp = memchr (str, '\0', sh_size - offset);
   10285       74657 :       if (unlikely (endp == NULL))
   10286             :         {
   10287           0 :           printf (gettext (" *** error, missing string terminator\n"));
   10288             :           break;
   10289             :         }
   10290             : 
   10291       74657 :       printf (" [%*" PRIx64 "]  \"%s\"\n", digits, (uint64_t) offset, str);
   10292       74657 :       len = endp - str;
   10293       74657 :       offset += len + 1;
   10294             :     }
   10295          45 : }
   10296             : 
   10297             : static void
   10298           4 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10299             :                                  Ebl *ebl,
   10300             :                                  GElf_Ehdr *ehdr __attribute__ ((unused)),
   10301             :                                  Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10302             : {
   10303           4 :   printf (gettext ("\
   10304             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10305             :           elf_ndxscn (scn), section_name (ebl, shdr),
   10306           4 :           (uint64_t) shdr->sh_offset);
   10307             : 
   10308           4 :   if (shdr->sh_size == 0)
   10309             :     return;
   10310             : 
   10311             :   /* We like to get the section from libdw to make sure they are relocated.  */
   10312           8 :   Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets]
   10313           4 :                     ?: elf_rawdata (scn, NULL));
   10314           4 :   if (unlikely (data == NULL))
   10315             :     {
   10316           0 :       error (0, 0, gettext ("cannot get .debug_str_offsets section data: %s"),
   10317             :              elf_errmsg (-1));
   10318           0 :       return;
   10319             :     }
   10320             : 
   10321           4 :   size_t idx = 0;
   10322           4 :   sort_listptr (&known_stroffbases, "str_offsets");
   10323             : 
   10324           4 :   const unsigned char *start = (const unsigned char *) data->d_buf;
   10325           4 :   const unsigned char *readp = start;
   10326           8 :   const unsigned char *readendp = ((const unsigned char *) data->d_buf
   10327           4 :                                    + data->d_size);
   10328             : 
   10329           8 :   while (readp < readendp)
   10330             :     {
   10331             :       /* Most string offset tables will have a header.  For split
   10332             :          dwarf unit GNU DebugFission didn't add one.  But they were
   10333             :          also only defined for split units (main or skeleton units
   10334             :          didn't have indirect strings).  So if we don't have a
   10335             :          DW_AT_str_offsets_base at all and this is offset zero, then
   10336             :          just start printing offsets immediately, if this is a .dwo
   10337             :          section.  */
   10338           8 :       Dwarf_Off off = (Dwarf_Off) (readp
   10339           4 :                                    - (const unsigned char *) data->d_buf);
   10340             : 
   10341           4 :       printf ("Table at offset %" PRIx64 " ", off);
   10342             : 
   10343           4 :       struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
   10344           0 :       const unsigned char *next_unitp = readendp;
   10345           0 :       uint8_t offset_size;
   10346           0 :       bool has_header;
   10347           0 :       if (listptr == NULL)
   10348             :         {
   10349             :           /* This can happen for .dwo files.  There is only an header
   10350             :              in the case this is a version 5 split DWARF file.  */
   10351           4 :           Dwarf_CU *cu;
   10352           4 :           uint8_t unit_type;
   10353           4 :           if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
   10354             :                                NULL, NULL) != 0)
   10355             :             {
   10356           0 :               error (0, 0, "Warning: Cannot find any DWARF unit.");
   10357             :               /* Just guess some values.  */
   10358           0 :               has_header = false;
   10359           0 :               offset_size = 4;
   10360             :             }
   10361           4 :           else if (off == 0
   10362           4 :                    && (unit_type == DW_UT_split_type
   10363           4 :                        || unit_type == DW_UT_split_compile))
   10364             :             {
   10365           4 :               has_header = cu->version > 4;
   10366           4 :               offset_size = cu->offset_size;
   10367             :             }
   10368             :           else
   10369             :             {
   10370           0 :               error (0, 0,
   10371             :                      "Warning: No CU references .debug_str_offsets after %"
   10372             :                      PRIx64, off);
   10373           0 :               has_header = cu->version > 4;
   10374           0 :               offset_size = cu->offset_size;
   10375             :             }
   10376           4 :           printf ("\n");
   10377             :         }
   10378             :       else
   10379             :         {
   10380             :           /* This must be DWARF5, since GNU DebugFission didn't define
   10381             :              DW_AT_str_offsets_base.  */
   10382           0 :           has_header = true;
   10383             : 
   10384           0 :           Dwarf_Die cudie;
   10385           0 :           if (dwarf_cu_die (listptr->cu, &cudie,
   10386             :                             NULL, NULL, NULL, NULL,
   10387             :                             NULL, NULL) == NULL)
   10388           0 :             printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
   10389             :           else
   10390           0 :             printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
   10391             :         }
   10392             : 
   10393           4 :       if (has_header)
   10394             :         {
   10395           2 :           uint64_t unit_length;
   10396           2 :           uint16_t version;
   10397           2 :           uint16_t padding;
   10398             : 
   10399           2 :           unit_length = read_4ubyte_unaligned_inc (dbg, readp);
   10400           2 :           if (unlikely (unit_length == 0xffffffff))
   10401             :             {
   10402           0 :               if (unlikely (readp > readendp - 8))
   10403             :                 {
   10404           0 :                 invalid_data:
   10405           0 :                   error (0, 0, "Invalid data");
   10406           0 :                   return;
   10407             :                 }
   10408           0 :               unit_length = read_8ubyte_unaligned_inc (dbg, readp);
   10409           0 :               offset_size = 8;
   10410             :             }
   10411             :           else
   10412             :             offset_size = 4;
   10413             : 
   10414           2 :           printf ("\n");
   10415           2 :           printf (gettext (" Length:        %8" PRIu64 "\n"),
   10416             :                   unit_length);
   10417           2 :           printf (gettext (" Offset size:   %8" PRIu8 "\n"),
   10418             :                   offset_size);
   10419             : 
   10420             :           /* We need at least 2-bytes (version) + 2-bytes (padding) =
   10421             :              4 bytes to complete the header.  And this unit cannot go
   10422             :              beyond the section data.  */
   10423           2 :           if (readp > readendp - 4
   10424           2 :               || unit_length < 4
   10425           2 :               || unit_length > (uint64_t) (readendp - readp))
   10426             :             goto invalid_data;
   10427             : 
   10428           2 :           next_unitp = readp + unit_length;
   10429             : 
   10430           2 :           version = read_2ubyte_unaligned_inc (dbg, readp);
   10431           2 :           printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
   10432             : 
   10433           2 :           if (version != 5)
   10434             :             {
   10435           0 :               error (0, 0, gettext ("Unknown version"));
   10436           0 :               goto next_unit;
   10437             :             }
   10438             : 
   10439           2 :           padding = read_2ubyte_unaligned_inc (dbg, readp);
   10440           2 :           printf (gettext (" Padding:       %8" PRIx16 "\n"), padding);
   10441             : 
   10442           2 :           if (listptr != NULL
   10443           0 :               && listptr->offset != (Dwarf_Off) (readp - start))
   10444             :             {
   10445           0 :               error (0, 0, "String offsets index doesn't start after header");
   10446           0 :               goto next_unit;
   10447             :             }
   10448             : 
   10449           2 :           printf ("\n");
   10450             :         }
   10451             : 
   10452           4 :       int digits = 1;
   10453           4 :       size_t offsets = (next_unitp - readp) / offset_size;
   10454           8 :       while (offsets >= 10)
   10455             :         {
   10456           4 :           ++digits;
   10457           4 :           offsets /= 10;
   10458             :         }
   10459             : 
   10460           4 :       unsigned int uidx = 0;
   10461           4 :       size_t index_offset =  readp - (const unsigned char *) data->d_buf;
   10462           4 :       printf (" Offsets start at 0x%zx:\n", index_offset);
   10463          64 :       while (readp <= next_unitp - offset_size)
   10464             :         {
   10465          60 :           Dwarf_Word offset;
   10466          60 :           if (offset_size == 4)
   10467          60 :             offset = read_4ubyte_unaligned_inc (dbg, readp);
   10468             :           else
   10469           0 :             offset = read_8ubyte_unaligned_inc (dbg, readp);
   10470          60 :           const char *str = dwarf_getstring (dbg, offset, NULL);
   10471          60 :           printf (" [%*u] [%*" PRIx64 "]  \"%s\"\n",
   10472          60 :                   digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
   10473             :         }
   10474           4 :       printf ("\n");
   10475             : 
   10476           4 :       if (readp != next_unitp)
   10477           0 :         error (0, 0, "extra %zd bytes at end of unit",
   10478           0 :                (size_t) (next_unitp - readp));
   10479             : 
   10480           4 :     next_unit:
   10481             :       readp = next_unitp;
   10482             :     }
   10483             : }
   10484             : 
   10485             : 
   10486             : /* Print the content of the call frame search table section
   10487             :    '.eh_frame_hdr'.  */
   10488             : static void
   10489          29 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10490             :                                Ebl *ebl __attribute__ ((unused)),
   10491             :                                GElf_Ehdr *ehdr __attribute__ ((unused)),
   10492             :                                Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10493             : {
   10494          29 :   printf (gettext ("\
   10495             : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
   10496             :           elf_ndxscn (scn));
   10497             : 
   10498          29 :   Elf_Data *data = elf_rawdata (scn, NULL);
   10499             : 
   10500          29 :   if (unlikely (data == NULL))
   10501             :     {
   10502           0 :       error (0, 0, gettext ("cannot get %s content: %s"),
   10503             :              ".eh_frame_hdr", elf_errmsg (-1));
   10504           0 :       return;
   10505             :     }
   10506             : 
   10507          29 :   const unsigned char *readp = data->d_buf;
   10508          58 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
   10509          29 :                                         + data->d_size);
   10510             : 
   10511          29 :   if (unlikely (readp + 4 > dataend))
   10512             :     {
   10513           0 :     invalid_data:
   10514           0 :       error (0, 0, gettext ("invalid data"));
   10515           0 :       return;
   10516             :     }
   10517             : 
   10518          29 :   unsigned int version = *readp++;
   10519          29 :   unsigned int eh_frame_ptr_enc = *readp++;
   10520          29 :   unsigned int fde_count_enc = *readp++;
   10521          29 :   unsigned int table_enc = *readp++;
   10522             : 
   10523          29 :   printf (" version:          %u\n"
   10524             :           " eh_frame_ptr_enc: %#x ",
   10525             :           version, eh_frame_ptr_enc);
   10526          29 :   print_encoding_base ("", eh_frame_ptr_enc);
   10527          29 :   printf (" fde_count_enc:    %#x ", fde_count_enc);
   10528          29 :   print_encoding_base ("", fde_count_enc);
   10529          29 :   printf (" table_enc:        %#x ", table_enc);
   10530          29 :   print_encoding_base ("", table_enc);
   10531             : 
   10532          29 :   uint64_t eh_frame_ptr = 0;
   10533          29 :   if (eh_frame_ptr_enc != DW_EH_PE_omit)
   10534             :     {
   10535          29 :       readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
   10536             :                             dbg);
   10537          29 :       if (unlikely (readp == NULL))
   10538             :         goto invalid_data;
   10539             : 
   10540          29 :       printf (" eh_frame_ptr:     %#" PRIx64, eh_frame_ptr);
   10541          29 :       if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
   10542          58 :         printf (" (offset: %#" PRIx64 ")",
   10543             :                 /* +4 because of the 4 byte header of the section.  */
   10544          29 :                 (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
   10545             : 
   10546          29 :       putchar_unlocked ('\n');
   10547             :     }
   10548             : 
   10549          29 :   uint64_t fde_count = 0;
   10550          29 :   if (fde_count_enc != DW_EH_PE_omit)
   10551             :     {
   10552          29 :       readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
   10553          29 :       if (unlikely (readp == NULL))
   10554             :         goto invalid_data;
   10555             : 
   10556          29 :       printf (" fde_count:        %" PRIu64 "\n", fde_count);
   10557             :     }
   10558             : 
   10559          29 :   if (fde_count == 0 || table_enc == DW_EH_PE_omit)
   10560             :     return;
   10561             : 
   10562          29 :   puts (" Table:");
   10563             : 
   10564             :   /* Optimize for the most common case.  */
   10565          29 :   if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
   10566        9148 :     while (fde_count > 0 && readp + 8 <= dataend)
   10567             :       {
   10568        9119 :         int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
   10569       18238 :         uint64_t initial_offset = ((uint64_t) shdr->sh_offset
   10570        9119 :                                    + (int64_t) initial_location);
   10571        9119 :         int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
   10572             :         // XXX Possibly print symbol name or section offset for initial_offset
   10573       18238 :         printf ("  %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
   10574             :                 " fde=[%6" PRIx64 "]\n",
   10575             :                 initial_location, initial_offset,
   10576        9119 :                 address, address - (eh_frame_ptr + 4));
   10577             :       }
   10578             :   else
   10579             :     while (0 && readp < dataend)
   10580             :       {
   10581             : 
   10582          29 :       }
   10583             : }
   10584             : 
   10585             : 
   10586             : /* Print the content of the exception handling table section
   10587             :    '.eh_frame_hdr'.  */
   10588             : static void
   10589           0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10590             :                              Ebl *ebl __attribute__ ((unused)),
   10591             :                              GElf_Ehdr *ehdr __attribute__ ((unused)),
   10592             :                              Elf_Scn *scn,
   10593             :                              GElf_Shdr *shdr __attribute__ ((unused)),
   10594             :                              Dwarf *dbg __attribute__ ((unused)))
   10595             : {
   10596           0 :   printf (gettext ("\
   10597             : \nException handling table section [%2zu] '.gcc_except_table':\n"),
   10598             :           elf_ndxscn (scn));
   10599             : 
   10600           0 :   Elf_Data *data = elf_rawdata (scn, NULL);
   10601             : 
   10602           0 :   if (unlikely (data == NULL))
   10603             :     {
   10604           0 :       error (0, 0, gettext ("cannot get %s content: %s"),
   10605             :              ".gcc_except_table", elf_errmsg (-1));
   10606           0 :       return;
   10607             :     }
   10608             : 
   10609           0 :   const unsigned char *readp = data->d_buf;
   10610           0 :   const unsigned char *const dataend = readp + data->d_size;
   10611             : 
   10612           0 :   if (unlikely (readp + 1 > dataend))
   10613             :     {
   10614           0 :     invalid_data:
   10615           0 :       error (0, 0, gettext ("invalid data"));
   10616           0 :       return;
   10617             :     }
   10618           0 :   unsigned int lpstart_encoding = *readp++;
   10619           0 :   printf (gettext (" LPStart encoding:    %#x "), lpstart_encoding);
   10620           0 :   print_encoding_base ("", lpstart_encoding);
   10621           0 :   if (lpstart_encoding != DW_EH_PE_omit)
   10622             :     {
   10623           0 :       uint64_t lpstart;
   10624           0 :       readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
   10625           0 :       printf (" LPStart:             %#" PRIx64 "\n", lpstart);
   10626             :     }
   10627             : 
   10628           0 :   if (unlikely (readp + 1 > dataend))
   10629             :     goto invalid_data;
   10630           0 :   unsigned int ttype_encoding = *readp++;
   10631           0 :   printf (gettext (" TType encoding:      %#x "), ttype_encoding);
   10632           0 :   print_encoding_base ("", ttype_encoding);
   10633           0 :   const unsigned char *ttype_base = NULL;
   10634           0 :   if (ttype_encoding != DW_EH_PE_omit)
   10635             :     {
   10636           0 :       unsigned int ttype_base_offset;
   10637           0 :       get_uleb128 (ttype_base_offset, readp, dataend);
   10638           0 :       printf (" TType base offset:   %#x\n", ttype_base_offset);
   10639           0 :       if ((size_t) (dataend - readp) > ttype_base_offset)
   10640           0 :         ttype_base = readp + ttype_base_offset;
   10641             :     }
   10642             : 
   10643           0 :   if (unlikely (readp + 1 > dataend))
   10644             :     goto invalid_data;
   10645           0 :   unsigned int call_site_encoding = *readp++;
   10646           0 :   printf (gettext (" Call site encoding:  %#x "), call_site_encoding);
   10647           0 :   print_encoding_base ("", call_site_encoding);
   10648           0 :   unsigned int call_site_table_len;
   10649           0 :   get_uleb128 (call_site_table_len, readp, dataend);
   10650             : 
   10651           0 :   const unsigned char *const action_table = readp + call_site_table_len;
   10652           0 :   if (unlikely (action_table > dataend))
   10653             :     goto invalid_data;
   10654             :   unsigned int u = 0;
   10655             :   unsigned int max_action = 0;
   10656           0 :   while (readp < action_table)
   10657             :     {
   10658           0 :       if (u == 0)
   10659           0 :         puts (gettext ("\n Call site table:"));
   10660             : 
   10661           0 :       uint64_t call_site_start;
   10662           0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   10663             :                             &call_site_start, dbg);
   10664           0 :       uint64_t call_site_length;
   10665           0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   10666             :                             &call_site_length, dbg);
   10667           0 :       uint64_t landing_pad;
   10668           0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   10669             :                             &landing_pad, dbg);
   10670           0 :       unsigned int action;
   10671           0 :       get_uleb128 (action, readp, dataend);
   10672           0 :       max_action = MAX (action, max_action);
   10673           0 :       printf (gettext (" [%4u] Call site start:   %#" PRIx64 "\n"
   10674             :                        "        Call site length:  %" PRIu64 "\n"
   10675             :                        "        Landing pad:       %#" PRIx64 "\n"
   10676             :                        "        Action:            %u\n"),
   10677             :               u++, call_site_start, call_site_length, landing_pad, action);
   10678             :     }
   10679           0 :   if (readp != action_table)
   10680             :     goto invalid_data;
   10681             : 
   10682           0 :   unsigned int max_ar_filter = 0;
   10683           0 :   if (max_action > 0)
   10684             :     {
   10685           0 :       puts ("\n Action table:");
   10686             : 
   10687           0 :       size_t maxdata = (size_t) (dataend - action_table);
   10688           0 :       if (max_action > maxdata || maxdata - max_action < 1)
   10689             :         {
   10690           0 :         invalid_action_table:
   10691           0 :           fputs (gettext ("   <INVALID DATA>\n"), stdout);
   10692           0 :           return;
   10693             :         }
   10694             : 
   10695           0 :       const unsigned char *const action_table_end
   10696           0 :         = action_table + max_action + 1;
   10697             : 
   10698           0 :       u = 0;
   10699           0 :       do
   10700             :         {
   10701           0 :           int ar_filter;
   10702           0 :           get_sleb128 (ar_filter, readp, action_table_end);
   10703           0 :           if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
   10704           0 :             max_ar_filter = ar_filter;
   10705           0 :           int ar_disp;
   10706           0 :           if (readp >= action_table_end)
   10707             :             goto invalid_action_table;
   10708           0 :           get_sleb128 (ar_disp, readp, action_table_end);
   10709             : 
   10710           0 :           printf (" [%4u] ar_filter:  % d\n"
   10711             :                   "        ar_disp:    % -5d",
   10712             :                   u, ar_filter, ar_disp);
   10713           0 :           if (abs (ar_disp) & 1)
   10714           0 :             printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
   10715           0 :           else if (ar_disp != 0)
   10716           0 :             puts (" -> ???");
   10717             :           else
   10718           0 :             putchar_unlocked ('\n');
   10719           0 :           ++u;
   10720             :         }
   10721           0 :       while (readp < action_table_end);
   10722             :     }
   10723             : 
   10724           0 :   if (max_ar_filter > 0 && ttype_base != NULL)
   10725             :     {
   10726           0 :       unsigned char dsize;
   10727           0 :       puts ("\n TType table:");
   10728             : 
   10729             :       // XXX Not *4, size of encoding;
   10730           0 :       switch (ttype_encoding & 7)
   10731             :         {
   10732             :         case DW_EH_PE_udata2:
   10733             :         case DW_EH_PE_sdata2:
   10734             :           dsize = 2;
   10735             :           break;
   10736             :         case DW_EH_PE_udata4:
   10737             :         case DW_EH_PE_sdata4:
   10738             :           dsize = 4;
   10739             :           break;
   10740             :         case DW_EH_PE_udata8:
   10741             :         case DW_EH_PE_sdata8:
   10742             :           dsize = 8;
   10743             :           break;
   10744           0 :         default:
   10745           0 :           dsize = 0;
   10746           0 :           error (1, 0, gettext ("invalid TType encoding"));
   10747             :         }
   10748             : 
   10749           0 :       if (max_ar_filter
   10750           0 :           > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
   10751             :         goto invalid_data;
   10752             : 
   10753           0 :       readp = ttype_base - max_ar_filter * dsize;
   10754           0 :       do
   10755             :         {
   10756           0 :           uint64_t ttype;
   10757           0 :           readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
   10758             :                                 dbg);
   10759           0 :           printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
   10760             :         }
   10761           0 :       while (readp < ttype_base);
   10762             :     }
   10763             : }
   10764             : 
   10765             : /* Print the content of the '.gdb_index' section.
   10766             :    http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
   10767             : */
   10768             : static void
   10769           2 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
   10770             :                          GElf_Ehdr *ehdr __attribute__ ((unused)),
   10771             :                          Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10772             : {
   10773           2 :   printf (gettext ("\nGDB section [%2zu] '%s' at offset %#" PRIx64
   10774             :                    " contains %" PRId64 " bytes :\n"),
   10775             :           elf_ndxscn (scn), section_name (ebl, shdr),
   10776           2 :           (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
   10777             : 
   10778           2 :   Elf_Data *data = elf_rawdata (scn, NULL);
   10779             : 
   10780           2 :   if (unlikely (data == NULL))
   10781             :     {
   10782           0 :       error (0, 0, gettext ("cannot get %s content: %s"),
   10783             :              ".gdb_index", elf_errmsg (-1));
   10784           0 :       return;
   10785             :     }
   10786             : 
   10787             :   // .gdb_index is always in little endian.
   10788           2 :   Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
   10789           2 :   dbg = &dummy_dbg;
   10790             : 
   10791           2 :   const unsigned char *readp = data->d_buf;
   10792           2 :   const unsigned char *const dataend = readp + data->d_size;
   10793             : 
   10794           2 :   if (unlikely (readp + 4 > dataend))
   10795             :     {
   10796           0 :     invalid_data:
   10797           0 :       error (0, 0, gettext ("invalid data"));
   10798           0 :       return;
   10799             :     }
   10800             : 
   10801           2 :   int32_t vers = read_4ubyte_unaligned (dbg, readp);
   10802           2 :   printf (gettext (" Version:         %" PRId32 "\n"), vers);
   10803             : 
   10804             :   // The only difference between version 4 and version 5 is the
   10805             :   // hash used for generating the table.  Version 6 contains symbols
   10806             :   // for inlined functions, older versions didn't.  Version 7 adds
   10807             :   // symbol kinds.  Version 8 just indicates that it correctly includes
   10808             :   // TUs for symbols.
   10809           2 :   if (vers < 4 || vers > 8)
   10810             :     {
   10811           0 :       printf (gettext ("  unknown version, cannot parse section\n"));
   10812           0 :       return;
   10813             :     }
   10814             : 
   10815           2 :   readp += 4;
   10816           2 :   if (unlikely (readp + 4 > dataend))
   10817             :     goto invalid_data;
   10818             : 
   10819           2 :   uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
   10820           2 :   printf (gettext (" CU offset:       %#" PRIx32 "\n"), cu_off);
   10821             : 
   10822           2 :   readp += 4;
   10823           2 :   if (unlikely (readp + 4 > dataend))
   10824             :     goto invalid_data;
   10825             : 
   10826           2 :   uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
   10827           2 :   printf (gettext (" TU offset:       %#" PRIx32 "\n"), tu_off);
   10828             : 
   10829           2 :   readp += 4;
   10830           2 :   if (unlikely (readp + 4 > dataend))
   10831             :     goto invalid_data;
   10832             : 
   10833           2 :   uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
   10834           2 :   printf (gettext (" address offset:  %#" PRIx32 "\n"), addr_off);
   10835             : 
   10836           2 :   readp += 4;
   10837           2 :   if (unlikely (readp + 4 > dataend))
   10838             :     goto invalid_data;
   10839             : 
   10840           2 :   uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
   10841           2 :   printf (gettext (" symbol offset:   %#" PRIx32 "\n"), sym_off);
   10842             : 
   10843           2 :   readp += 4;
   10844           2 :   if (unlikely (readp + 4 > dataend))
   10845             :     goto invalid_data;
   10846             : 
   10847           2 :   uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
   10848           2 :   printf (gettext (" constant offset: %#" PRIx32 "\n"), const_off);
   10849             : 
   10850           2 :   if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
   10851             :                 < const_off))
   10852             :     goto invalid_data;
   10853             : 
   10854           2 :   readp = data->d_buf + cu_off;
   10855             : 
   10856           2 :   const unsigned char *nextp = data->d_buf + tu_off;
   10857           2 :   if (tu_off >= data->d_size)
   10858             :     goto invalid_data;
   10859             : 
   10860           2 :   size_t cu_nr = (nextp - readp) / 16;
   10861             : 
   10862           2 :   printf (gettext ("\n CU list at offset %#" PRIx32
   10863             :                    " contains %zu entries:\n"),
   10864             :           cu_off, cu_nr);
   10865             : 
   10866           2 :   size_t n = 0;
   10867           6 :   while (dataend - readp >= 16 && n < cu_nr)
   10868             :     {
   10869           4 :       uint64_t off = read_8ubyte_unaligned (dbg, readp);
   10870           4 :       readp += 8;
   10871             : 
   10872           4 :       uint64_t len = read_8ubyte_unaligned (dbg, readp);
   10873           4 :       readp += 8;
   10874             : 
   10875           4 :       printf (" [%4zu] start: %0#8" PRIx64
   10876             :               ", length: %5" PRIu64 "\n", n, off, len);
   10877           4 :       n++;
   10878             :     }
   10879             : 
   10880           2 :   readp = data->d_buf + tu_off;
   10881           2 :   nextp = data->d_buf + addr_off;
   10882           2 :   if (addr_off >= data->d_size)
   10883             :     goto invalid_data;
   10884             : 
   10885           2 :   size_t tu_nr = (nextp - readp) / 24;
   10886             : 
   10887           2 :   printf (gettext ("\n TU list at offset %#" PRIx32
   10888             :                    " contains %zu entries:\n"),
   10889             :           tu_off, tu_nr);
   10890             : 
   10891           2 :   n = 0;
   10892           4 :   while (dataend - readp >= 24 && n < tu_nr)
   10893             :     {
   10894           2 :       uint64_t off = read_8ubyte_unaligned (dbg, readp);
   10895           2 :       readp += 8;
   10896             : 
   10897           2 :       uint64_t type = read_8ubyte_unaligned (dbg, readp);
   10898           2 :       readp += 8;
   10899             : 
   10900           2 :       uint64_t sig = read_8ubyte_unaligned (dbg, readp);
   10901           2 :       readp += 8;
   10902             : 
   10903           2 :       printf (" [%4zu] CU offset: %5" PRId64
   10904             :               ", type offset: %5" PRId64
   10905             :               ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
   10906           2 :       n++;
   10907             :     }
   10908             : 
   10909           2 :   readp = data->d_buf + addr_off;
   10910           2 :   nextp = data->d_buf + sym_off;
   10911           2 :   if (sym_off >= data->d_size)
   10912             :     goto invalid_data;
   10913             : 
   10914           2 :   size_t addr_nr = (nextp - readp) / 20;
   10915             : 
   10916           2 :   printf (gettext ("\n Address list at offset %#" PRIx32
   10917             :                    " contains %zu entries:\n"),
   10918             :           addr_off, addr_nr);
   10919             : 
   10920           2 :   n = 0;
   10921           6 :   while (dataend - readp >= 20 && n < addr_nr)
   10922             :     {
   10923           4 :       uint64_t low = read_8ubyte_unaligned (dbg, readp);
   10924           4 :       readp += 8;
   10925             : 
   10926           4 :       uint64_t high = read_8ubyte_unaligned (dbg, readp);
   10927           4 :       readp += 8;
   10928             : 
   10929           4 :       uint32_t idx = read_4ubyte_unaligned (dbg, readp);
   10930           4 :       readp += 4;
   10931             : 
   10932           4 :       printf (" [%4zu] ", n);
   10933           4 :       print_dwarf_addr (dwflmod, 8, low, low);
   10934           4 :       printf ("..");
   10935           4 :       print_dwarf_addr (dwflmod, 8, high - 1, high);
   10936           4 :       printf (", CU index: %5" PRId32 "\n", idx);
   10937           4 :       n++;
   10938             :     }
   10939             : 
   10940           2 :   const unsigned char *const_start = data->d_buf + const_off;
   10941           2 :   if (const_off >= data->d_size)
   10942             :     goto invalid_data;
   10943             : 
   10944           2 :   readp = data->d_buf + sym_off;
   10945           2 :   nextp = const_start;
   10946           2 :   size_t sym_nr = (nextp - readp) / 8;
   10947             : 
   10948           2 :   printf (gettext ("\n Symbol table at offset %#" PRIx32
   10949             :                    " contains %zu slots:\n"),
   10950             :           addr_off, sym_nr);
   10951             : 
   10952           2 :   n = 0;
   10953        2050 :   while (dataend - readp >= 8 && n < sym_nr)
   10954             :     {
   10955        2048 :       uint32_t name = read_4ubyte_unaligned (dbg, readp);
   10956        2048 :       readp += 4;
   10957             : 
   10958        2048 :       uint32_t vector = read_4ubyte_unaligned (dbg, readp);
   10959        2048 :       readp += 4;
   10960             : 
   10961        2048 :       if (name != 0 || vector != 0)
   10962             :         {
   10963          14 :           const unsigned char *sym = const_start + name;
   10964          14 :           if (unlikely ((size_t) (dataend - const_start) < name
   10965             :                         || memchr (sym, '\0', dataend - sym) == NULL))
   10966             :             goto invalid_data;
   10967             : 
   10968          14 :           printf (" [%4zu] symbol: %s, CUs: ", n, sym);
   10969             : 
   10970          14 :           const unsigned char *readcus = const_start + vector;
   10971          14 :           if (unlikely ((size_t) (dataend - const_start) < vector))
   10972             :             goto invalid_data;
   10973          14 :           uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
   10974          30 :           while (cus--)
   10975             :             {
   10976          16 :               uint32_t cu_kind, cu, kind;
   10977          16 :               bool is_static;
   10978          16 :               readcus += 4;
   10979          16 :               if (unlikely (readcus + 4 > dataend))
   10980             :                 goto invalid_data;
   10981          16 :               cu_kind = read_4ubyte_unaligned (dbg, readcus);
   10982          16 :               cu = cu_kind & ((1 << 24) - 1);
   10983          16 :               kind = (cu_kind >> 28) & 7;
   10984          16 :               is_static = cu_kind & (1U << 31);
   10985          16 :               if (cu > cu_nr - 1)
   10986           2 :                 printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
   10987             :               else
   10988          14 :                 printf ("%" PRId32, cu);
   10989          16 :               if (kind != 0)
   10990             :                 {
   10991           8 :                   printf (" (");
   10992           8 :                   switch (kind)
   10993             :                     {
   10994             :                     case 1:
   10995           3 :                       printf ("type");
   10996             :                       break;
   10997             :                     case 2:
   10998           2 :                       printf ("var");
   10999             :                       break;
   11000             :                     case 3:
   11001           3 :                       printf ("func");
   11002             :                       break;
   11003             :                     case 4:
   11004           0 :                       printf ("other");
   11005             :                       break;
   11006             :                     default:
   11007           0 :                       printf ("unknown-0x%" PRIx32, kind);
   11008             :                       break;
   11009             :                     }
   11010          11 :                   printf (":%c)", (is_static ? 'S' : 'G'));
   11011             :                 }
   11012          16 :               if (cus > 0)
   11013           2 :                 printf (", ");
   11014             :             }
   11015          14 :           printf ("\n");
   11016             :         }
   11017        2048 :       n++;
   11018             :     }
   11019             : }
   11020             : 
   11021             : /* Returns true and sets split DWARF CU id if there is a split compile
   11022             :    unit in the given Dwarf, and no non-split units are found (before it).  */
   11023             : static bool
   11024         141 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
   11025             : {
   11026         141 :   Dwarf_CU *cu = NULL;
   11027         141 :   while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
   11028             :     {
   11029         141 :       uint8_t unit_type;
   11030         141 :       if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
   11031             :                          id, NULL, NULL) != 0)
   11032         141 :         return false;
   11033             : 
   11034         141 :       if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
   11035             :         return false;
   11036             : 
   11037             :       /* We really only care about the split compile unit, the types
   11038             :          should be fine and self sufficient.  Also they don't have an
   11039             :          id that we can match with a skeleton unit.  */
   11040           9 :       if (unit_type == DW_UT_split_compile)
   11041             :         {
   11042           9 :           *split_cu = cu;
   11043           9 :           return true;
   11044             :         }
   11045             :     }
   11046             : 
   11047             :   return false;
   11048             : }
   11049             : 
   11050             : /* Check that there is one and only one Dwfl_Module, return in arg.  */
   11051             : static int
   11052           9 : getone_dwflmod (Dwfl_Module *dwflmod,
   11053             :                void **userdata __attribute__ ((unused)),
   11054             :                const char *name __attribute__ ((unused)),
   11055             :                Dwarf_Addr base __attribute__ ((unused)),
   11056             :                void *arg)
   11057             : {
   11058           9 :   Dwfl_Module **m = (Dwfl_Module **) arg;
   11059           9 :   if (*m != NULL)
   11060             :     return DWARF_CB_ABORT;
   11061           9 :   *m = dwflmod;
   11062           9 :   return DWARF_CB_OK;
   11063             : }
   11064             : 
   11065             : static void
   11066         186 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
   11067             : {
   11068             :   /* Used for skeleton file, if necessary for split DWARF.  */
   11069         186 :   Dwfl *skel_dwfl = NULL;
   11070         186 :   Dwfl_Module *skel_mod = NULL;
   11071         186 :   char *skel_name = NULL;
   11072         186 :   Dwarf *split_dbg = NULL;
   11073         186 :   Dwarf_CU *split_cu = NULL;
   11074             : 
   11075             :   /* Before we start the real work get a debug context descriptor.  */
   11076         186 :   Dwarf_Addr dwbias;
   11077         186 :   Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
   11078         558 :   Dwarf dummy_dbg =
   11079             :     {
   11080         186 :       .elf = ebl->elf,
   11081         186 :       .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
   11082             :     };
   11083         186 :   if (dbg == NULL)
   11084             :     {
   11085          45 :       if ((print_debug_sections & ~section_exception) != 0)
   11086           0 :         error (0, 0, gettext ("cannot get debug context descriptor: %s"),
   11087             :                dwfl_errmsg (-1));
   11088             :       dbg = &dummy_dbg;
   11089             :     }
   11090             :   else
   11091             :     {
   11092             :       /* If we are asked about a split dwarf (.dwo) file, use the user
   11093             :          provided, or find the corresponding skeleton file. If we got
   11094             :          a skeleton file, replace the given dwflmod and dbg, with one
   11095             :          derived from the skeleton file to provide enough context.  */
   11096         141 :       uint64_t split_id;
   11097         141 :       if (is_split_dwarf (dbg, &split_id, &split_cu))
   11098             :         {
   11099           9 :           if (dwarf_skeleton != NULL)
   11100           9 :             skel_name = strdup (dwarf_skeleton);
   11101             :           else
   11102             :             {
   11103             :               /* Replace file.dwo with file.o and see if that matches. */
   11104           0 :               const char *fname;
   11105           0 :               dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
   11106             :                                 &fname, NULL);
   11107           0 :               if (fname != NULL)
   11108             :                 {
   11109           0 :                   size_t flen = strlen (fname);
   11110           0 :                   if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
   11111             :                     {
   11112           0 :                       skel_name = strdup (fname);
   11113           0 :                       if (skel_name != NULL)
   11114             :                         {
   11115           0 :                           skel_name[flen - 3] = 'o';
   11116           0 :                           skel_name[flen - 2] = '\0';
   11117             :                         }
   11118             :                     }
   11119             :                 }
   11120             :             }
   11121             : 
   11122           9 :           if (skel_name != NULL)
   11123             :             {
   11124           9 :               int skel_fd = open (skel_name, O_RDONLY);
   11125           9 :               if (skel_fd == -1)
   11126           0 :                 fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
   11127             :                          " '%s'\n", skel_name);
   11128             :               else
   11129           9 :                 skel_dwfl = create_dwfl (skel_fd, skel_name);
   11130             : 
   11131           9 :               if (skel_dwfl != NULL)
   11132             :                 {
   11133           9 :                   if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
   11134             :                                        &skel_mod, 0) != 0)
   11135             :                     {
   11136           0 :                       fprintf (stderr, "Warning: Bad DWARF skeleton,"
   11137             :                                " multiple modules '%s'\n", skel_name);
   11138           0 :                       dwfl_end (skel_dwfl);
   11139           0 :                       skel_mod = NULL;
   11140             :                     }
   11141             :                 }
   11142           0 :               else if (skel_fd != -1)
   11143           0 :                 fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
   11144             :                          " '%s': %s\n", skel_name, dwfl_errmsg (-1));
   11145             : 
   11146           9 :               if (skel_mod != NULL)
   11147             :                 {
   11148           9 :                   Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
   11149           9 :                   if (skel_dbg != NULL)
   11150             :                     {
   11151             :                       /* First check the skeleton CU DIE, only fetch
   11152             :                          the split DIE if we know the id matches to
   11153             :                          not unnecessary search for any split DIEs we
   11154             :                          don't need. */
   11155           9 :                       Dwarf_CU *cu = NULL;
   11156          13 :                       while (dwarf_get_units (skel_dbg, cu, &cu,
   11157             :                                               NULL, NULL, NULL, NULL) == 0)
   11158             :                         {
   11159          13 :                           uint8_t unit_type;
   11160          13 :                           uint64_t skel_id;
   11161          13 :                           if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
   11162             :                                              &skel_id, NULL, NULL) == 0
   11163          13 :                               && unit_type == DW_UT_skeleton
   11164          13 :                               && split_id == skel_id)
   11165             :                             {
   11166           9 :                               Dwarf_Die subdie;
   11167           9 :                               if (dwarf_cu_info (cu, NULL, NULL, NULL,
   11168             :                                                  &subdie,
   11169             :                                                  NULL, NULL, NULL) == 0
   11170           9 :                                   && dwarf_tag (&subdie) != DW_TAG_invalid)
   11171             :                                 {
   11172           9 :                                   split_dbg = dwarf_cu_getdwarf (subdie.cu);
   11173           9 :                                   if (split_dbg == NULL)
   11174           0 :                                     fprintf (stderr,
   11175             :                                              "Warning: Couldn't get split_dbg:"
   11176             :                                              " %s\n", dwarf_errmsg (-1));
   11177           9 :                                   break;
   11178             :                                 }
   11179             :                               else
   11180             :                                 {
   11181             :                                   /* Everything matches up, but not
   11182             :                                      according to libdw. Which means
   11183             :                                      the user knew better.  So...
   11184             :                                      Terrible hack... We can never
   11185             :                                      destroy the underlying dwfl
   11186             :                                      because it would free the wrong
   11187             :                                      Dwarfs... So we leak memory...*/
   11188           0 :                                   if (cu->split == NULL
   11189           0 :                                       && dwarf_skeleton != NULL)
   11190             :                                     {
   11191           0 :                                       do_not_close_dwfl = true;
   11192           0 :                                       __libdw_link_skel_split (cu, split_cu);
   11193           0 :                                       split_dbg = dwarf_cu_getdwarf (split_cu);
   11194           0 :                                       break;
   11195             :                                     }
   11196             :                                   else
   11197           0 :                                     fprintf (stderr, "Warning: Couldn't get"
   11198             :                                              " skeleton subdie: %s\n",
   11199             :                                              dwarf_errmsg (-1));
   11200             :                                 }
   11201             :                             }
   11202             :                         }
   11203           9 :                       if (split_dbg == NULL)
   11204           0 :                         fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
   11205             :                     }
   11206             :                   else
   11207           0 :                     fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
   11208             :                              " %s\n", dwfl_errmsg (-1));
   11209             :                 }
   11210             :             }
   11211             : 
   11212           9 :           if (split_dbg != NULL)
   11213             :             {
   11214           9 :               dbg = split_dbg;
   11215           9 :               dwflmod = skel_mod;
   11216             :             }
   11217           0 :           else if (skel_name == NULL)
   11218           0 :             fprintf (stderr,
   11219             :                      "Warning: split DWARF file, but no skeleton found.\n");
   11220             :         }
   11221         132 :       else if (dwarf_skeleton != NULL)
   11222           0 :         fprintf (stderr, "Warning: DWARF skeleton given,"
   11223             :                  " but not a split DWARF file\n");
   11224             :     }
   11225             : 
   11226             :   /* Get the section header string table index.  */
   11227         186 :   size_t shstrndx;
   11228         186 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
   11229           0 :     error (EXIT_FAILURE, 0,
   11230           0 :            gettext ("cannot get section header string table index"));
   11231             : 
   11232             :   /* If the .debug_info section is listed as implicitly required then
   11233             :      we must make sure to handle it before handling any other debug
   11234             :      section.  Various other sections depend on the CU DIEs being
   11235             :      scanned (silently) first.  */
   11236         186 :   bool implicit_info = (implicit_debug_sections & section_info) != 0;
   11237         186 :   bool explicit_info = (print_debug_sections & section_info) != 0;
   11238         186 :   if (implicit_info)
   11239             :     {
   11240             :       Elf_Scn *scn = NULL;
   11241        1491 :       while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   11242             :         {
   11243        1491 :           GElf_Shdr shdr_mem;
   11244        1491 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   11245             : 
   11246        1491 :           if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
   11247             :             {
   11248        1791 :               const char *name = elf_strptr (ebl->elf, shstrndx,
   11249         597 :                                              shdr->sh_name);
   11250         597 :               if (name == NULL)
   11251           0 :                 continue;
   11252             : 
   11253         597 :               if (strcmp (name, ".debug_info") == 0
   11254         535 :                   || strcmp (name, ".debug_info.dwo") == 0
   11255         526 :                   || strcmp (name, ".zdebug_info") == 0
   11256         520 :                   || strcmp (name, ".zdebug_info.dwo") == 0
   11257         520 :                   || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
   11258             :                 {
   11259          77 :                   print_debug_info_section (dwflmod, ebl, ehdr,
   11260             :                                             scn, shdr, dbg);
   11261          77 :                   break;
   11262             :                 }
   11263             :             }
   11264             :         }
   11265          77 :       print_debug_sections &= ~section_info;
   11266          77 :       implicit_debug_sections &= ~section_info;
   11267             :     }
   11268             : 
   11269             :   /* Look through all the sections for the debugging sections to print.  */
   11270             :   Elf_Scn *scn = NULL;
   11271        5668 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   11272             :     {
   11273        5482 :       GElf_Shdr shdr_mem;
   11274        5482 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   11275             : 
   11276        5482 :       if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
   11277             :         {
   11278        2485 :           static const struct
   11279             :           {
   11280             :             const char *name;
   11281             :             enum section_e bitmask;
   11282             :             void (*fp) (Dwfl_Module *, Ebl *,
   11283             :                         GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
   11284             :           } debug_sections[] =
   11285             :             {
   11286             : #define NEW_SECTION(name) \
   11287             :               { ".debug_" #name, section_##name, print_debug_##name##_section }
   11288             :               NEW_SECTION (abbrev),
   11289             :               NEW_SECTION (addr),
   11290             :               NEW_SECTION (aranges),
   11291             :               NEW_SECTION (frame),
   11292             :               NEW_SECTION (info),
   11293             :               NEW_SECTION (types),
   11294             :               NEW_SECTION (line),
   11295             :               NEW_SECTION (loc),
   11296             :               /* loclists is loc for DWARF5.  */
   11297             :               { ".debug_loclists", section_loc,
   11298             :                 print_debug_loclists_section },
   11299             :               NEW_SECTION (pubnames),
   11300             :               NEW_SECTION (str),
   11301             :               /* A DWARF5 specialised debug string section.  */
   11302             :               { ".debug_line_str", section_str,
   11303             :                 print_debug_str_section },
   11304             :               /* DWARF5 string offsets table.  */
   11305             :               { ".debug_str_offsets", section_str,
   11306             :                 print_debug_str_offsets_section },
   11307             :               NEW_SECTION (macinfo),
   11308             :               NEW_SECTION (macro),
   11309             :               NEW_SECTION (ranges),
   11310             :               /* rnglists is ranges for DWARF5.  */
   11311             :               { ".debug_rnglists", section_ranges,
   11312             :                 print_debug_rnglists_section },
   11313             :               { ".eh_frame", section_frame | section_exception,
   11314             :                 print_debug_frame_section },
   11315             :               { ".eh_frame_hdr", section_frame | section_exception,
   11316             :                 print_debug_frame_hdr_section },
   11317             :               { ".gcc_except_table", section_frame | section_exception,
   11318             :                 print_debug_exception_table },
   11319             :               { ".gdb_index", section_gdb_index, print_gdb_index_section }
   11320             :             };
   11321        2485 :           const int ndebug_sections = (sizeof (debug_sections)
   11322             :                                        / sizeof (debug_sections[0]));
   11323        7455 :           const char *name = elf_strptr (ebl->elf, shstrndx,
   11324        2485 :                                          shdr->sh_name);
   11325        2485 :           if (name == NULL)
   11326           0 :             continue;
   11327             : 
   11328             :           int n;
   11329       39380 :           for (n = 0; n < ndebug_sections; ++n)
   11330             :             {
   11331       38047 :               size_t dbglen = strlen (debug_sections[n].name);
   11332       38047 :               size_t scnlen = strlen (name);
   11333       38047 :               if ((strncmp (name, debug_sections[n].name, dbglen) == 0
   11334        1169 :                    && (dbglen == scnlen
   11335         153 :                        || (scnlen == dbglen + 4
   11336         136 :                            && strstr (name, ".dwo") == name + dbglen)))
   11337       36977 :                   || (name[0] == '.' && name[1] == 'z'
   11338         566 :                       && debug_sections[n].name[1] == 'd'
   11339         566 :                       && strncmp (&name[2], &debug_sections[n].name[1],
   11340             :                                   dbglen - 1) == 0
   11341          82 :                       && (scnlen == dbglen + 1
   11342           0 :                           || (scnlen == dbglen + 5
   11343           0 :                               && strstr (name, ".dwo") == name + dbglen + 1)))
   11344       36895 :                   || (scnlen > 14 /* .gnu.debuglto_ prefix. */
   11345        3194 :                       && strncmp (name, ".gnu.debuglto_", 14) == 0
   11346           0 :                       && strcmp (&name[14], debug_sections[n].name) == 0)
   11347             : )
   11348             :                 {
   11349        2304 :                   if ((print_debug_sections | implicit_debug_sections)
   11350        1152 :                       & debug_sections[n].bitmask)
   11351         402 :                     debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
   11352             :                   break;
   11353             :                 }
   11354             :             }
   11355             :         }
   11356             :     }
   11357             : 
   11358         186 :   dwfl_end (skel_dwfl);
   11359         186 :   free (skel_name);
   11360             : 
   11361             :   /* Turn implicit and/or explicit back on in case we go over another file.  */
   11362         186 :   if (implicit_info)
   11363          77 :     implicit_debug_sections |= section_info;
   11364         186 :   if (explicit_info)
   11365          61 :     print_debug_sections |= section_info;
   11366             : 
   11367         372 :   reset_listptr (&known_locsptr);
   11368         372 :   reset_listptr (&known_loclistsptr);
   11369         372 :   reset_listptr (&known_rangelistptr);
   11370         372 :   reset_listptr (&known_rnglistptr);
   11371         372 :   reset_listptr (&known_addrbases);
   11372         372 :   reset_listptr (&known_stroffbases);
   11373         186 : }
   11374             : 
   11375             : 
   11376             : #define ITEM_INDENT             4
   11377             : #define WRAP_COLUMN             75
   11378             : 
   11379             : /* Print "NAME: FORMAT", wrapping when output text would make the line
   11380             :    exceed WRAP_COLUMN.  Unpadded numbers look better for the core items
   11381             :    but this function is also used for registers which should be printed
   11382             :    aligned.  Fortunately registers output uses fixed fields width (such
   11383             :    as %11d) for the alignment.
   11384             : 
   11385             :    Line breaks should not depend on the particular values although that
   11386             :    may happen in some cases of the core items.  */
   11387             : 
   11388             : static unsigned int
   11389             : __attribute__ ((format (printf, 6, 7)))
   11390         821 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
   11391             :                  size_t name_width, const char *name, const char *format, ...)
   11392             : {
   11393         821 :   size_t len = strlen (name);
   11394         821 :   if (name_width < len)
   11395         397 :     name_width = len;
   11396             : 
   11397         821 :   char *out;
   11398         821 :   va_list ap;
   11399         821 :   va_start (ap, format);
   11400         821 :   int out_len = vasprintf (&out, format, ap);
   11401         821 :   va_end (ap);
   11402         821 :   if (out_len == -1)
   11403           0 :     error (EXIT_FAILURE, 0, _("memory exhausted"));
   11404             : 
   11405         821 :   size_t n = name_width + sizeof ": " - 1 + out_len;
   11406             : 
   11407         821 :   if (colno == 0)
   11408             :     {
   11409          51 :       printf ("%*s", ITEM_INDENT, "");
   11410          51 :       colno = ITEM_INDENT + n;
   11411             :     }
   11412         770 :   else if (colno + 2 + n < wrap)
   11413             :     {
   11414         468 :       printf ("%c ", sep);
   11415         468 :       colno += 2 + n;
   11416             :     }
   11417             :   else
   11418             :     {
   11419         302 :       printf ("\n%*s", ITEM_INDENT, "");
   11420         302 :       colno = ITEM_INDENT + n;
   11421             :     }
   11422             : 
   11423         821 :   printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
   11424             : 
   11425         821 :   free (out);
   11426             : 
   11427         821 :   return colno;
   11428             : }
   11429             : 
   11430             : static const void *
   11431         931 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
   11432             :          void *value, const void *data, size_t size)
   11433             : {
   11434        1862 :   Elf_Data valuedata =
   11435             :     {
   11436             :       .d_type = type,
   11437             :       .d_buf = value,
   11438         931 :       .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
   11439             :       .d_version = EV_CURRENT,
   11440             :     };
   11441         931 :   Elf_Data indata =
   11442             :     {
   11443             :       .d_type = type,
   11444             :       .d_buf = (void *) data,
   11445             :       .d_size = valuedata.d_size,
   11446             :       .d_version = EV_CURRENT,
   11447             :     };
   11448             : 
   11449         931 :   Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
   11450         931 :                  ? elf32_xlatetom : elf64_xlatetom)
   11451         931 :     (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
   11452         931 :   if (d == NULL)
   11453           0 :     error (EXIT_FAILURE, 0,
   11454           0 :            gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
   11455             : 
   11456         931 :   return data + indata.d_size;
   11457             : }
   11458             : 
   11459             : typedef uint8_t GElf_Byte;
   11460             : 
   11461             : static unsigned int
   11462         400 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
   11463             :                   unsigned int colno, size_t *repeated_size)
   11464             : {
   11465         400 :   uint_fast16_t count = item->count ?: 1;
   11466             :   /* Ebl_Core_Item count is always a small number.
   11467             :      Make sure the backend didn't put in some large bogus value.  */
   11468          62 :   assert (count < 128);
   11469             : 
   11470             : #define TYPES                                                                 \
   11471             :   DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8);                         \
   11472             :   DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16);                       \
   11473             :   DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32);                       \
   11474             :   DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32);                         \
   11475             :   DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64);                            \
   11476             :   DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
   11477             : 
   11478             : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
   11479         400 :   typedef union { TYPES; } value_t;
   11480         400 :   void *data = alloca (count * sizeof (value_t));
   11481             : #undef DO_TYPE
   11482             : 
   11483             : #define DO_TYPE(NAME, Name, hex, dec) \
   11484             :     GElf_##Name *value_##Name __attribute__((unused)) = data
   11485         400 :   TYPES;
   11486             : #undef DO_TYPE
   11487             : 
   11488         400 :   size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
   11489         400 :   size_t convsize = size;
   11490         400 :   if (repeated_size != NULL)
   11491             :     {
   11492           1 :       if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
   11493             :         {
   11494           0 :           data = alloca (*repeated_size);
   11495           0 :           count *= *repeated_size / size;
   11496           0 :           convsize = count * size;
   11497           0 :           *repeated_size -= convsize;
   11498             :         }
   11499           1 :       else if (item->count != 0 || item->format != '\n')
   11500           0 :         *repeated_size -= size;
   11501             :     }
   11502             : 
   11503         400 :   convert (core, item->type, count, data, desc + item->offset, convsize);
   11504             : 
   11505         400 :   Elf_Type type = item->type;
   11506         400 :   if (type == ELF_T_ADDR)
   11507           1 :     type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
   11508             : 
   11509         400 :   switch (item->format)
   11510             :     {
   11511         193 :     case 'd':
   11512         193 :       assert (count == 1);
   11513         193 :       switch (type)
   11514             :         {
   11515             : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   11516             :           case ELF_T_##NAME:                                                  \
   11517             :             colno = print_core_item (colno, ',', WRAP_COLUMN,                 \
   11518             :                                      0, item->name, dec, value_##Name[0]); \
   11519             :             break
   11520         193 :           TYPES;
   11521             : #undef DO_TYPE
   11522           0 :         default:
   11523           0 :           abort ();
   11524             :         }
   11525             :       break;
   11526             : 
   11527         111 :     case 'x':
   11528         111 :       assert (count == 1);
   11529         111 :       switch (type)
   11530             :         {
   11531             : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   11532             :           case ELF_T_##NAME:                                                  \
   11533             :             colno = print_core_item (colno, ',', WRAP_COLUMN,                 \
   11534             :                                      0, item->name, hex, value_##Name[0]);      \
   11535             :             break
   11536         111 :           TYPES;
   11537             : #undef DO_TYPE
   11538           0 :         default:
   11539           0 :           abort ();
   11540             :         }
   11541             :       break;
   11542             : 
   11543          22 :     case 'b':
   11544             :     case 'B':
   11545          22 :       assert (size % sizeof (unsigned int) == 0);
   11546          22 :       unsigned int nbits = count * size * 8;
   11547          22 :       unsigned int pop = 0;
   11548          56 :       for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
   11549          34 :         pop += __builtin_popcount (*i);
   11550          22 :       bool negate = pop > nbits / 2;
   11551          22 :       const unsigned int bias = item->format == 'b';
   11552             : 
   11553          44 :       {
   11554          22 :         char printed[(negate ? nbits - pop : pop) * 16 + 1];
   11555          22 :         char *p = printed;
   11556          22 :         *p = '\0';
   11557             : 
   11558          22 :         if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
   11559             :           {
   11560             :             assert (size == sizeof (unsigned int) * 2);
   11561             :             for (unsigned int *i = data;
   11562             :                  (void *) i < data + count * size; i += 2)
   11563             :               {
   11564             :                 unsigned int w = i[1];
   11565             :                 i[1] = i[0];
   11566             :                 i[0] = w;
   11567             :               }
   11568             :           }
   11569             : 
   11570          22 :         unsigned int lastbit = 0;
   11571          22 :         unsigned int run = 0;
   11572          56 :         for (const unsigned int *i = data;
   11573          34 :              (void *) i < data + count * size; ++i)
   11574             :           {
   11575          34 :             unsigned int bit = ((void *) i - data) * 8;
   11576          34 :             unsigned int w = negate ? ~*i : *i;
   11577          34 :             while (w != 0)
   11578             :               {
   11579             :                 /* Note that a right shift equal to (or greater than)
   11580             :                    the number of bits of w is undefined behaviour.  In
   11581             :                    particular when the least significant bit is bit 32
   11582             :                    (w = 0x8000000) then w >>= n is undefined.  So
   11583             :                    explicitly handle that case separately.  */
   11584           0 :                 unsigned int n = ffs (w);
   11585           0 :                 if (n < sizeof (w) * 8)
   11586           0 :                   w >>= n;
   11587             :                 else
   11588             :                   w = 0;
   11589           0 :                 bit += n;
   11590             : 
   11591           0 :                 if (lastbit != 0 && lastbit + 1 == bit)
   11592           0 :                   ++run;
   11593             :                 else
   11594             :                   {
   11595           0 :                     if (lastbit == 0)
   11596           0 :                       p += sprintf (p, "%u", bit - bias);
   11597           0 :                     else if (run == 0)
   11598           0 :                       p += sprintf (p, ",%u", bit - bias);
   11599             :                     else
   11600           0 :                       p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
   11601             :                     run = 0;
   11602             :                   }
   11603             : 
   11604             :                 lastbit = bit;
   11605             :               }
   11606             :           }
   11607          22 :         if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
   11608           0 :           p += sprintf (p, "-%u", lastbit - bias);
   11609             : 
   11610          44 :         colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   11611             :                                  negate ? "~<%s>" : "<%s>", printed);
   11612             :       }
   11613          22 :       break;
   11614             : 
   11615          44 :     case 'T':
   11616             :     case (char) ('T'|0x80):
   11617          44 :       assert (count == 2);
   11618          44 :       Dwarf_Word sec;
   11619          44 :       Dwarf_Word usec;
   11620          44 :       switch (type)
   11621             :         {
   11622             : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   11623             :           case ELF_T_##NAME:                                                  \
   11624             :             sec = value_##Name[0];                                            \
   11625             :             usec = value_##Name[1];                                           \
   11626             :             break
   11627          44 :           TYPES;
   11628             : #undef DO_TYPE
   11629           0 :         default:
   11630           0 :           abort ();
   11631             :         }
   11632          44 :       if (unlikely (item->format == (char) ('T'|0x80)))
   11633             :         {
   11634             :           /* This is a hack for an ill-considered 64-bit ABI where
   11635             :              tv_usec is actually a 32-bit field with 32 bits of padding
   11636             :              rounding out struct timeval.  We've already converted it as
   11637             :              a 64-bit field.  For little-endian, this just means the
   11638             :              high half is the padding; it's presumably zero, but should
   11639             :              be ignored anyway.  For big-endian, it means the 32-bit
   11640             :              field went into the high half of USEC.  */
   11641           0 :           GElf_Ehdr ehdr_mem;
   11642           0 :           GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
   11643           0 :           if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
   11644           0 :             usec >>= 32;
   11645             :           else
   11646           0 :             usec &= UINT32_MAX;
   11647             :         }
   11648          44 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   11649             :                                "%" PRIu64 ".%.6" PRIu64, sec, usec);
   11650          44 :       break;
   11651             : 
   11652           9 :     case 'c':
   11653           9 :       assert (count == 1);
   11654          18 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   11655           9 :                                "%c", value_Byte[0]);
   11656           9 :       break;
   11657             : 
   11658          18 :     case 's':
   11659          18 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   11660             :                                "%.*s", (int) count, value_Byte);
   11661          18 :       break;
   11662             : 
   11663           1 :     case '\n':
   11664             :       /* This is a list of strings separated by '\n'.  */
   11665           1 :       assert (item->count == 0);
   11666           1 :       assert (repeated_size != NULL);
   11667           1 :       assert (item->name == NULL);
   11668           1 :       if (unlikely (item->offset >= *repeated_size))
   11669             :         break;
   11670             : 
   11671           1 :       const char *s = desc + item->offset;
   11672           1 :       size = *repeated_size - item->offset;
   11673           1 :       *repeated_size = 0;
   11674          48 :       while (size > 0)
   11675             :         {
   11676          48 :           const char *eol = memchr (s, '\n', size);
   11677          48 :           int len = size;
   11678          48 :           if (eol != NULL)
   11679          47 :             len = eol - s;
   11680          48 :           printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
   11681          48 :           if (eol == NULL)
   11682             :             break;
   11683          47 :           size -= eol + 1 - s;
   11684          47 :           s = eol + 1;
   11685             :         }
   11686             : 
   11687             :       colno = WRAP_COLUMN;
   11688             :       break;
   11689             : 
   11690             :     case 'h':
   11691             :       break;
   11692             : 
   11693           0 :     default:
   11694           0 :       error (0, 0, "XXX not handling format '%c' for %s",
   11695             :              item->format, item->name);
   11696             :       break;
   11697             :     }
   11698             : 
   11699             : #undef TYPES
   11700             : 
   11701           0 :   return colno;
   11702             : }
   11703             : 
   11704             : 
   11705             : /* Sort items by group, and by layout offset within each group.  */
   11706             : static int
   11707         895 : compare_core_items (const void *a, const void *b)
   11708             : {
   11709         895 :   const Ebl_Core_Item *const *p1 = a;
   11710         895 :   const Ebl_Core_Item *const *p2 = b;
   11711         895 :   const Ebl_Core_Item *item1 = *p1;
   11712         895 :   const Ebl_Core_Item *item2 = *p2;
   11713             : 
   11714         895 :   return ((item1->group == item2->group ? 0
   11715         895 :            : strcmp (item1->group, item2->group))
   11716         895 :           ?: (int) item1->offset - (int) item2->offset);
   11717             : }
   11718             : 
   11719             : /* Sort item groups by layout offset of the first item in the group.  */
   11720             : static int
   11721         139 : compare_core_item_groups (const void *a, const void *b)
   11722             : {
   11723         139 :   const Ebl_Core_Item *const *const *p1 = a;
   11724         139 :   const Ebl_Core_Item *const *const *p2 = b;
   11725         139 :   const Ebl_Core_Item *const *group1 = *p1;
   11726         139 :   const Ebl_Core_Item *const *group2 = *p2;
   11727         139 :   const Ebl_Core_Item *item1 = *group1;
   11728         139 :   const Ebl_Core_Item *item2 = *group2;
   11729             : 
   11730         139 :   return (int) item1->offset - (int) item2->offset;
   11731             : }
   11732             : 
   11733             : static unsigned int
   11734          37 : handle_core_items (Elf *core, const void *desc, size_t descsz,
   11735             :                    const Ebl_Core_Item *items, size_t nitems)
   11736             : {
   11737          37 :   if (nitems == 0)
   11738             :     return 0;
   11739          34 :   unsigned int colno = 0;
   11740             : 
   11741             :   /* FORMAT '\n' makes sense to be present only as a single item as it
   11742             :      processes all the data of a note.  FORMATs 'b' and 'B' have a special case
   11743             :      if present as a single item but they can be also processed with other
   11744             :      items below.  */
   11745          34 :   if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
   11746           8 :                       || items[0].format == 'B'))
   11747             :     {
   11748           1 :       assert (items[0].offset == 0);
   11749           1 :       size_t size = descsz;
   11750           1 :       colno = handle_core_item (core, items, desc, colno, &size);
   11751             :       /* If SIZE is not zero here there is some remaining data.  But we do not
   11752             :          know how to process it anyway.  */
   11753           1 :       return colno;
   11754             :     }
   11755         424 :   for (size_t i = 0; i < nitems; ++i)
   11756         391 :     assert (items[i].format != '\n');
   11757             : 
   11758             :   /* Sort to collect the groups together.  */
   11759          33 :   const Ebl_Core_Item *sorted_items[nitems];
   11760         424 :   for (size_t i = 0; i < nitems; ++i)
   11761         391 :     sorted_items[i] = &items[i];
   11762          33 :   qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
   11763             : 
   11764             :   /* Collect the unique groups and sort them.  */
   11765          33 :   const Ebl_Core_Item **groups[nitems];
   11766          33 :   groups[0] = &sorted_items[0];
   11767          33 :   size_t ngroups = 1;
   11768         391 :   for (size_t i = 1; i < nitems; ++i)
   11769         358 :     if (sorted_items[i]->group != sorted_items[i - 1]->group
   11770          75 :         && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
   11771          75 :       groups[ngroups++] = &sorted_items[i];
   11772          33 :   qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
   11773             : 
   11774             :   /* Write out all the groups.  */
   11775          33 :   const void *last = desc;
   11776          35 :   do
   11777             :     {
   11778         145 :       for (size_t i = 0; i < ngroups; ++i)
   11779             :         {
   11780         110 :           for (const Ebl_Core_Item **item = groups[i];
   11781         509 :                (item < &sorted_items[nitems]
   11782         474 :                 && ((*item)->group == groups[i][0]->group
   11783          75 :                     || !strcmp ((*item)->group, groups[i][0]->group)));
   11784         399 :                ++item)
   11785         399 :             colno = handle_core_item (core, *item, desc, colno, NULL);
   11786             : 
   11787             :           /* Force a line break at the end of the group.  */
   11788         110 :           colno = WRAP_COLUMN;
   11789             :         }
   11790             : 
   11791          35 :       if (descsz == 0)
   11792             :         break;
   11793             : 
   11794             :       /* This set of items consumed a certain amount of the note's data.
   11795             :          If there is more data there, we have another unit of the same size.
   11796             :          Loop to print that out too.  */
   11797          20 :       const Ebl_Core_Item *item = &items[nitems - 1];
   11798          20 :       size_t eltsz = item->offset + gelf_fsize (core, item->type,
   11799          20 :                                                 item->count ?: 1, EV_CURRENT);
   11800             : 
   11801          20 :       int reps = -1;
   11802          20 :       do
   11803             :         {
   11804          20 :           ++reps;
   11805          20 :           desc += eltsz;
   11806          20 :           descsz -= eltsz;
   11807             :         }
   11808          20 :       while (descsz >= eltsz && !memcmp (desc, last, eltsz));
   11809             : 
   11810          20 :       if (reps == 1)
   11811             :         {
   11812             :           /* For just one repeat, print it unabridged twice.  */
   11813             :           desc -= eltsz;
   11814             :           descsz += eltsz;
   11815             :         }
   11816          20 :       else if (reps > 1)
   11817           0 :         printf (gettext ("\n%*s... <repeats %u more times> ..."),
   11818             :                 ITEM_INDENT, "", reps);
   11819             : 
   11820          20 :       last = desc;
   11821             :     }
   11822          20 :   while (descsz > 0);
   11823             : 
   11824          33 :   return colno;
   11825             : }
   11826             : 
   11827             : static unsigned int
   11828           0 : handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc,
   11829             :                       unsigned int colno)
   11830             : {
   11831           0 :   desc += regloc->offset;
   11832             : 
   11833           0 :   abort ();                     /* XXX */
   11834             :   return colno;
   11835             : }
   11836             : 
   11837             : 
   11838             : static unsigned int
   11839         162 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
   11840             :                       const Ebl_Register_Location *regloc, const void *desc,
   11841             :                       unsigned int colno)
   11842             : {
   11843         162 :   if (regloc->bits % 8 != 0)
   11844           0 :     return handle_bit_registers (regloc, desc, colno);
   11845             : 
   11846         162 :   desc += regloc->offset;
   11847             : 
   11848         586 :   for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
   11849             :     {
   11850         424 :       char name[REGNAMESZ];
   11851         424 :       int bits;
   11852         424 :       int type;
   11853         424 :       register_info (ebl, reg, regloc, name, &bits, &type);
   11854             : 
   11855             : #define TYPES                                                                 \
   11856             :       BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8);                          \
   11857             :       BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16);                       \
   11858             :       BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32);                             \
   11859             :       BITS (64, XWORD, "%20" PRId64, "  0x%.16" PRIx64)
   11860             : 
   11861             : #define BITS(bits, xtype, sfmt, ufmt)                           \
   11862             :       uint##bits##_t b##bits; int##bits##_t b##bits##s
   11863         424 :       union { TYPES; uint64_t b128[2]; } value;
   11864             : #undef  BITS
   11865             : 
   11866         424 :       switch (type)
   11867             :         {
   11868         336 :         case DW_ATE_unsigned:
   11869             :         case DW_ATE_signed:
   11870             :         case DW_ATE_address:
   11871         336 :           switch (bits)
   11872             :             {
   11873             : #define BITS(bits, xtype, sfmt, ufmt)                                         \
   11874             :             case bits:                                                        \
   11875             :               desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0);           \
   11876             :               if (type == DW_ATE_signed)                                      \
   11877             :                 colno = print_core_item (colno, ' ', WRAP_COLUMN,             \
   11878             :                                          maxregname, name,                    \
   11879             :                                          sfmt, value.b##bits##s);             \
   11880             :               else                                                            \
   11881             :                 colno = print_core_item (colno, ' ', WRAP_COLUMN,             \
   11882             :                                          maxregname, name,                    \
   11883             :                                          ufmt, value.b##bits);                \
   11884             :               break
   11885             : 
   11886         288 :             TYPES;
   11887             : 
   11888          48 :             case 128:
   11889          48 :               assert (type == DW_ATE_unsigned);
   11890          48 :               desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
   11891          48 :               int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
   11892          96 :               colno = print_core_item (colno, ' ', WRAP_COLUMN,
   11893             :                                        maxregname, name,
   11894             :                                        "0x%.16" PRIx64 "%.16" PRIx64,
   11895          48 :                                        value.b128[!be], value.b128[be]);
   11896          48 :               break;
   11897             : 
   11898           0 :             default:
   11899           0 :               abort ();
   11900             : #undef  BITS
   11901             :             }
   11902             :           break;
   11903             : 
   11904          88 :         default:
   11905             :           /* Print each byte in hex, the whole thing in native byte order.  */
   11906          88 :           assert (bits % 8 == 0);
   11907          88 :           const uint8_t *bytes = desc;
   11908          88 :           desc += bits / 8;
   11909          88 :           char hex[bits / 4 + 1];
   11910          88 :           hex[bits / 4] = '\0';
   11911          88 :           int incr = 1;
   11912          88 :           if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
   11913             :             {
   11914          48 :               bytes += bits / 8 - 1;
   11915          48 :               incr = -1;
   11916             :             }
   11917          88 :           size_t idx = 0;
   11918         872 :           for (char *h = hex; bits > 0; bits -= 8, idx += incr)
   11919             :             {
   11920         784 :               *h++ = "0123456789abcdef"[bytes[idx] >> 4];
   11921         784 :               *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
   11922             :             }
   11923          88 :           colno = print_core_item (colno, ' ', WRAP_COLUMN,
   11924             :                                    maxregname, name, "0x%s", hex);
   11925          88 :           break;
   11926             :         }
   11927         424 :       desc += regloc->pad;
   11928             : 
   11929             : #undef TYPES
   11930             :     }
   11931             : 
   11932         162 :   return colno;
   11933             : }
   11934             : 
   11935             : 
   11936             : struct register_info
   11937             : {
   11938             :   const Ebl_Register_Location *regloc;
   11939             :   const char *set;
   11940             :   char name[REGNAMESZ];
   11941             :   int regno;
   11942             :   int bits;
   11943             :   int type;
   11944             : };
   11945             : 
   11946             : static int
   11947           0 : register_bitpos (const struct register_info *r)
   11948             : {
   11949        2066 :   return (r->regloc->offset * 8
   11950        4132 :           + ((r->regno - r->regloc->regno)
   11951        2066 :              * (r->regloc->bits + r->regloc->pad * 8)));
   11952             : }
   11953             : 
   11954             : static int
   11955        1062 : compare_sets_by_info (const struct register_info *r1,
   11956             :                       const struct register_info *r2)
   11957             : {
   11958        1062 :   return ((int) r2->bits - (int) r1->bits
   11959        1062 :           ?: register_bitpos (r1) - register_bitpos (r2));
   11960             : }
   11961             : 
   11962             : /* Sort registers by set, and by size and layout offset within each set.  */
   11963             : static int
   11964        4595 : compare_registers (const void *a, const void *b)
   11965             : {
   11966        4595 :   const struct register_info *r1 = a;
   11967        4595 :   const struct register_info *r2 = b;
   11968             : 
   11969             :   /* Unused elements sort last.  */
   11970        4595 :   if (r1->regloc == NULL)
   11971        3309 :     return r2->regloc == NULL ? 0 : 1;
   11972        1286 :   if (r2->regloc == NULL)
   11973             :     return -1;
   11974             : 
   11975        1121 :   return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
   11976        1121 :           ?: compare_sets_by_info (r1, r2));
   11977             : }
   11978             : 
   11979             : /* Sort register sets by layout offset of the first register in the set.  */
   11980             : static int
   11981          20 : compare_register_sets (const void *a, const void *b)
   11982             : {
   11983          20 :   const struct register_info *const *p1 = a;
   11984          20 :   const struct register_info *const *p2 = b;
   11985          20 :   return compare_sets_by_info (*p1, *p2);
   11986             : }
   11987             : 
   11988             : static unsigned int
   11989          37 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
   11990             :                        const Ebl_Register_Location *reglocs, size_t nregloc)
   11991             : {
   11992          37 :   if (nregloc == 0)
   11993             :     return 0;
   11994             : 
   11995          18 :   ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
   11996          18 :   if (maxnreg <= 0)
   11997             :     {
   11998           0 :       for (size_t i = 0; i < nregloc; ++i)
   11999           0 :         if (maxnreg < reglocs[i].regno + reglocs[i].count)
   12000           0 :           maxnreg = reglocs[i].regno + reglocs[i].count;
   12001           0 :       assert (maxnreg > 0);
   12002             :     }
   12003             : 
   12004          18 :   struct register_info regs[maxnreg];
   12005          18 :   memset (regs, 0, sizeof regs);
   12006             : 
   12007             :   /* Sort to collect the sets together.  */
   12008          18 :   int maxreg = 0;
   12009         180 :   for (size_t i = 0; i < nregloc; ++i)
   12010         162 :     for (int reg = reglocs[i].regno;
   12011         586 :          reg < reglocs[i].regno + reglocs[i].count;
   12012         424 :          ++reg)
   12013             :       {
   12014         424 :         assert (reg < maxnreg);
   12015         424 :         if (reg > maxreg)
   12016         230 :           maxreg = reg;
   12017         424 :         struct register_info *info = &regs[reg];
   12018         424 :         info->regloc = &reglocs[i];
   12019         424 :         info->regno = reg;
   12020         424 :         info->set = register_info (ebl, reg, &reglocs[i],
   12021         424 :                                    info->name, &info->bits, &info->type);
   12022             :       }
   12023          18 :   qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
   12024             : 
   12025             :   /* Collect the unique sets and sort them.  */
   12026         882 :   inline bool same_set (const struct register_info *a,
   12027             :                         const struct register_info *b)
   12028             :   {
   12029         864 :     return (a < &regs[maxnreg] && a->regloc != NULL
   12030         864 :             && b < &regs[maxnreg] && b->regloc != NULL
   12031         846 :             && a->bits == b->bits
   12032        1692 :             && (a->set == b->set || !strcmp (a->set, b->set)));
   12033             :   }
   12034          18 :   struct register_info *sets[maxreg + 1];
   12035          18 :   sets[0] = &regs[0];
   12036          18 :   size_t nsets = 1;
   12037        1279 :   for (int i = 1; i <= maxreg; ++i)
   12038        1261 :     if (regs[i].regloc != NULL && !same_set (&regs[i], &regs[i - 1]))
   12039          16 :       sets[nsets++] = &regs[i];
   12040          18 :   qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
   12041             : 
   12042             :   /* Write out all the sets.  */
   12043          18 :   unsigned int colno = 0;
   12044          52 :   for (size_t i = 0; i < nsets; ++i)
   12045             :     {
   12046             :       /* Find the longest name of a register in this set.  */
   12047          34 :       size_t maxname = 0;
   12048          34 :       const struct register_info *end;
   12049         458 :       for (end = sets[i]; same_set (sets[i], end); ++end)
   12050             :         {
   12051         424 :           size_t len = strlen (end->name);
   12052         424 :           if (len > maxname)
   12053          52 :             maxname = len;
   12054             :         }
   12055             : 
   12056         196 :       for (const struct register_info *reg = sets[i];
   12057             :            reg < end;
   12058         162 :            reg += reg->regloc->count ?: 1)
   12059         162 :         colno = handle_core_register (ebl, core, maxname,
   12060             :                                       reg->regloc, desc, colno);
   12061             : 
   12062             :       /* Force a line break at the end of the group.  */
   12063          34 :       colno = WRAP_COLUMN;
   12064             :     }
   12065             : 
   12066          18 :   return colno;
   12067             : }
   12068             : 
   12069             : static void
   12070           9 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   12071             : {
   12072           9 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
   12073           9 :   if (data == NULL)
   12074           0 :   elf_error:
   12075           0 :     error (EXIT_FAILURE, 0,
   12076           0 :            gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
   12077             : 
   12078           9 :   const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
   12079         177 :   for (size_t i = 0; i < nauxv; ++i)
   12080             :     {
   12081         168 :       GElf_auxv_t av_mem;
   12082         168 :       GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
   12083         168 :       if (av == NULL)
   12084           0 :         goto elf_error;
   12085             : 
   12086         168 :       const char *name;
   12087         168 :       const char *fmt;
   12088         168 :       if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
   12089             :         {
   12090             :           /* Unknown type.  */
   12091           0 :           if (av->a_un.a_val == 0)
   12092           0 :             printf ("    %" PRIu64 "\n", av->a_type);
   12093             :           else
   12094           0 :             printf ("    %" PRIu64 ": %#" PRIx64 "\n",
   12095             :                     av->a_type, av->a_un.a_val);
   12096             :         }
   12097             :       else
   12098         168 :         switch (fmt[0])
   12099             :           {
   12100           9 :           case '\0':            /* Normally zero.  */
   12101           9 :             if (av->a_un.a_val == 0)
   12102             :               {
   12103           9 :                 printf ("    %s\n", name);
   12104             :                 break;
   12105             :               }
   12106          74 :             FALLTHROUGH;
   12107             :           case 'x':             /* hex */
   12108             :           case 'p':             /* address */
   12109             :           case 's':             /* address of string */
   12110          74 :             printf ("    %s: %#" PRIx64 "\n", name, av->a_un.a_val);
   12111             :             break;
   12112          81 :           case 'u':
   12113          81 :             printf ("    %s: %" PRIu64 "\n", name, av->a_un.a_val);
   12114             :             break;
   12115           0 :           case 'd':
   12116           0 :             printf ("    %s: %" PRId64 "\n", name, av->a_un.a_val);
   12117             :             break;
   12118             : 
   12119           4 :           case 'b':
   12120           4 :             printf ("    %s: %#" PRIx64 "  ", name, av->a_un.a_val);
   12121           4 :             GElf_Xword bit = 1;
   12122           4 :             const char *pfx = "<";
   12123         110 :             for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
   12124             :               {
   12125         106 :                 if (av->a_un.a_val & bit)
   12126             :                   {
   12127          77 :                     printf ("%s%s", pfx, p);
   12128          77 :                     pfx = " ";
   12129             :                   }
   12130         106 :                 bit <<= 1;
   12131             :               }
   12132           4 :             printf (">\n");
   12133             :             break;
   12134             : 
   12135           0 :           default:
   12136           0 :             abort ();
   12137             :           }
   12138             :     }
   12139           9 : }
   12140             : 
   12141             : static bool
   12142             : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
   12143             : {
   12144         195 :   return ptr < end && (size_t) (end - ptr) >= sz;
   12145             : }
   12146             : 
   12147             : static bool
   12148          18 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
   12149             :               int *retp)
   12150             : {
   12151          36 :   if (! buf_has_data (*ptrp, end, 4))
   12152             :     return false;
   12153             : 
   12154          18 :   *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
   12155          18 :   return true;
   12156             : }
   12157             : 
   12158             : static bool
   12159         177 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
   12160             :                 uint64_t *retp)
   12161             : {
   12162         177 :   size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
   12163         354 :   if (! buf_has_data (*ptrp, end, sz))
   12164             :     return false;
   12165             : 
   12166         177 :   union
   12167             :   {
   12168             :     uint64_t u64;
   12169             :     uint32_t u32;
   12170             :   } u;
   12171             : 
   12172         177 :   *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
   12173             : 
   12174         177 :   if (sz == 4)
   12175          93 :     *retp = u.u32;
   12176             :   else
   12177          84 :     *retp = u.u64;
   12178             :   return true;
   12179             : }
   12180             : 
   12181             : static void
   12182           6 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   12183             : {
   12184           6 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
   12185           6 :   if (data == NULL)
   12186           0 :     error (EXIT_FAILURE, 0,
   12187           0 :            gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
   12188             : 
   12189           6 :   unsigned char const *ptr = data->d_buf;
   12190           6 :   unsigned char const *const end = data->d_buf + data->d_size;
   12191             : 
   12192             :   /* Siginfo head is three ints: signal number, error number, origin
   12193             :      code.  */
   12194           6 :   int si_signo, si_errno, si_code;
   12195           6 :   if (! buf_read_int (core, &ptr, end, &si_signo)
   12196           6 :       || ! buf_read_int (core, &ptr, end, &si_errno)
   12197           6 :       || ! buf_read_int (core, &ptr, end, &si_code))
   12198             :     {
   12199           0 :     fail:
   12200           0 :       printf ("    Not enough data in NT_SIGINFO note.\n");
   12201           0 :       return;
   12202             :     }
   12203             : 
   12204             :   /* Next is a pointer-aligned union of structures.  On 64-bit
   12205             :      machines, that implies a word of padding.  */
   12206           6 :   if (gelf_getclass (core) == ELFCLASS64)
   12207           3 :     ptr += 4;
   12208             : 
   12209           6 :   printf ("    si_signo: %d, si_errno: %d, si_code: %d\n",
   12210             :           si_signo, si_errno, si_code);
   12211             : 
   12212           6 :   if (si_code > 0)
   12213           6 :     switch (si_signo)
   12214             :       {
   12215           6 :       case CORE_SIGILL:
   12216             :       case CORE_SIGFPE:
   12217             :       case CORE_SIGSEGV:
   12218             :       case CORE_SIGBUS:
   12219             :         {
   12220           6 :           uint64_t addr;
   12221           6 :           if (! buf_read_ulong (core, &ptr, end, &addr))
   12222           0 :             goto fail;
   12223           6 :           printf ("    fault address: %#" PRIx64 "\n", addr);
   12224           6 :           break;
   12225             :         }
   12226             :       default:
   12227             :         ;
   12228             :       }
   12229           0 :   else if (si_code == CORE_SI_USER)
   12230             :     {
   12231           0 :       int pid, uid;
   12232           0 :       if (! buf_read_int (core, &ptr, end, &pid)
   12233           0 :           || ! buf_read_int (core, &ptr, end, &uid))
   12234           0 :         goto fail;
   12235           0 :       printf ("    sender PID: %d, sender UID: %d\n", pid, uid);
   12236             :     }
   12237             : }
   12238             : 
   12239             : static void
   12240           6 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   12241             : {
   12242           6 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
   12243           6 :   if (data == NULL)
   12244           0 :     error (EXIT_FAILURE, 0,
   12245           0 :            gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
   12246             : 
   12247           6 :   unsigned char const *ptr = data->d_buf;
   12248           6 :   unsigned char const *const end = data->d_buf + data->d_size;
   12249             : 
   12250           6 :   uint64_t count, page_size;
   12251           6 :   if (! buf_read_ulong (core, &ptr, end, &count)
   12252           6 :       || ! buf_read_ulong (core, &ptr, end, &page_size))
   12253             :     {
   12254           0 :     fail:
   12255           0 :       printf ("    Not enough data in NT_FILE note.\n");
   12256           0 :       return;
   12257             :     }
   12258             : 
   12259           6 :   size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
   12260           6 :   uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
   12261           6 :   if (count > maxcount)
   12262             :     goto fail;
   12263             : 
   12264             :   /* Where file names are stored.  */
   12265           6 :   unsigned char const *const fstart = ptr + 3 * count * addrsize;
   12266           6 :   char const *fptr = (char *) fstart;
   12267             : 
   12268           6 :   printf ("    %" PRId64 " files:\n", count);
   12269          59 :   for (uint64_t i = 0; i < count; ++i)
   12270             :     {
   12271          53 :       uint64_t mstart, mend, moffset;
   12272          53 :       if (! buf_read_ulong (core, &ptr, fstart, &mstart)
   12273          53 :           || ! buf_read_ulong (core, &ptr, fstart, &mend)
   12274          53 :           || ! buf_read_ulong (core, &ptr, fstart, &moffset))
   12275           0 :         goto fail;
   12276             : 
   12277          53 :       const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
   12278          53 :       if (fnext == NULL)
   12279             :         goto fail;
   12280             : 
   12281          53 :       int ct = printf ("      %08" PRIx64 "-%08" PRIx64
   12282             :                        " %08" PRIx64 " %" PRId64,
   12283             :                        mstart, mend, moffset * page_size, mend - mstart);
   12284          53 :       printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
   12285             : 
   12286          53 :       fptr = fnext + 1;
   12287             :     }
   12288             : }
   12289             : 
   12290             : static void
   12291          38 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
   12292             :                   const char *name, const void *desc)
   12293             : {
   12294          38 :   GElf_Word regs_offset;
   12295          38 :   size_t nregloc;
   12296          38 :   const Ebl_Register_Location *reglocs;
   12297          38 :   size_t nitems;
   12298          38 :   const Ebl_Core_Item *items;
   12299             : 
   12300          38 :   if (! ebl_core_note (ebl, nhdr, name, desc,
   12301             :                        &regs_offset, &nregloc, &reglocs, &nitems, &items))
   12302           1 :     return;
   12303             : 
   12304             :   /* Pass 0 for DESCSZ when there are registers in the note,
   12305             :      so that the ITEMS array does not describe the whole thing.
   12306             :      For non-register notes, the actual descsz might be a multiple
   12307             :      of the unit size, not just exactly the unit size.  */
   12308          37 :   unsigned int colno = handle_core_items (ebl->elf, desc,
   12309          37 :                                           nregloc == 0 ? nhdr->n_descsz : 0,
   12310             :                                           items, nitems);
   12311          37 :   if (colno != 0)
   12312          34 :     putchar_unlocked ('\n');
   12313             : 
   12314          37 :   colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
   12315             :                                  reglocs, nregloc);
   12316          37 :   if (colno != 0)
   12317          18 :     putchar_unlocked ('\n');
   12318             : }
   12319             : 
   12320             : static void
   12321           0 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
   12322             :                    GElf_Off start, Elf_Data *data)
   12323             : {
   12324           0 :   fputs_unlocked (gettext ("  Owner          Data size  Type\n"), stdout);
   12325             : 
   12326           0 :   if (data == NULL)
   12327           0 :     goto bad_note;
   12328             : 
   12329             :   size_t offset = 0;
   12330             :   GElf_Nhdr nhdr;
   12331             :   size_t name_offset;
   12332             :   size_t desc_offset;
   12333           0 :   while (offset < data->d_size
   12334           0 :          && (offset = gelf_getnote (data, offset,
   12335             :                                     &nhdr, &name_offset, &desc_offset)) > 0)
   12336             :     {
   12337           0 :       const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
   12338           0 :       const char *desc = data->d_buf + desc_offset;
   12339             : 
   12340             :       /* GNU Build Attributes are weird, they store most of their data
   12341             :          into the owner name field.  Extract just the owner name
   12342             :          prefix here, then use the rest later as data.  */
   12343           0 :       bool is_gnu_build_attr
   12344           0 :         = strncmp (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX,
   12345             :                    strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX)) == 0;
   12346           0 :       const char *print_name = (is_gnu_build_attr
   12347           0 :                                 ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
   12348           0 :       size_t print_namesz = (is_gnu_build_attr
   12349           0 :                              ? strlen (print_name) : nhdr.n_namesz);
   12350             : 
   12351           0 :       char buf[100];
   12352           0 :       char buf2[100];
   12353           0 :       printf (gettext ("  %-13.*s  %9" PRId32 "  %s\n"),
   12354             :               (int) print_namesz, print_name, nhdr.n_descsz,
   12355           0 :               ehdr->e_type == ET_CORE
   12356           0 :               ? ebl_core_note_type_name (ebl, nhdr.n_type,
   12357             :                                          buf, sizeof (buf))
   12358           0 :               : ebl_object_note_type_name (ebl, name, nhdr.n_type,
   12359             :                                            nhdr.n_descsz,
   12360             :                                            buf2, sizeof (buf2)));
   12361             : 
   12362             :       /* Filter out invalid entries.  */
   12363           0 :       if (memchr (name, '\0', nhdr.n_namesz) != NULL
   12364             :           /* XXX For now help broken Linux kernels.  */
   12365             :           || 1)
   12366             :         {
   12367           0 :           if (ehdr->e_type == ET_CORE)
   12368             :             {
   12369           0 :               if (nhdr.n_type == NT_AUXV
   12370           0 :                   && (nhdr.n_namesz == 4 /* Broken old Linux kernels.  */
   12371           0 :                       || (nhdr.n_namesz == 5 && name[4] == '\0'))
   12372           0 :                   && !memcmp (name, "CORE", 4))
   12373           0 :                 handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
   12374             :                                   start + desc_offset);
   12375           0 :               else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
   12376           0 :                 switch (nhdr.n_type)
   12377             :                   {
   12378           0 :                   case NT_SIGINFO:
   12379           0 :                     handle_siginfo_note (ebl->elf, nhdr.n_descsz,
   12380             :                                          start + desc_offset);
   12381           0 :                     break;
   12382             : 
   12383           0 :                   case NT_FILE:
   12384           0 :                     handle_file_note (ebl->elf, nhdr.n_descsz,
   12385             :                                       start + desc_offset);
   12386           0 :                     break;
   12387             : 
   12388           0 :                   default:
   12389           0 :                     handle_core_note (ebl, &nhdr, name, desc);
   12390             :                   }
   12391             :               else
   12392           0 :                 handle_core_note (ebl, &nhdr, name, desc);
   12393             :             }
   12394             :           else
   12395           0 :             ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
   12396             :                              nhdr.n_descsz, desc);
   12397             :         }
   12398             :     }
   12399             : 
   12400           0 :   if (offset == data->d_size)
   12401           0 :     return;
   12402             : 
   12403           0 :  bad_note:
   12404           0 :   error (0, 0,
   12405           0 :          gettext ("cannot get content of note: %s"),
   12406           0 :          data != NULL ? "garbage data" : elf_errmsg (-1));
   12407             : }
   12408             : 
   12409             : static void
   12410          86 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
   12411             : {
   12412             :   /* If we have section headers, just look for SHT_NOTE sections.
   12413             :      In a debuginfo file, the program headers are not reliable.  */
   12414          86 :   if (shnum != 0)
   12415             :     {
   12416             :       /* Get the section header string table index.  */
   12417          75 :       size_t shstrndx;
   12418          75 :       if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
   12419           0 :         error (EXIT_FAILURE, 0,
   12420           0 :                gettext ("cannot get section header string table index"));
   12421             : 
   12422             :       Elf_Scn *scn = NULL;
   12423        1952 :       while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   12424             :         {
   12425        1877 :           GElf_Shdr shdr_mem;
   12426        1877 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   12427             : 
   12428        1877 :           if (shdr == NULL || shdr->sh_type != SHT_NOTE)
   12429             :             /* Not what we are looking for.  */
   12430        1772 :             continue;
   12431             : 
   12432         105 :           if (notes_section != NULL)
   12433             :             {
   12434           0 :               char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
   12435           0 :               if (sname == NULL || strcmp (sname, notes_section) != 0)
   12436             :                 continue;
   12437             :             }
   12438             : 
   12439         105 :           printf (gettext ("\
   12440             : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
   12441             :                   elf_ndxscn (scn),
   12442         105 :                   elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
   12443             :                   shdr->sh_size, shdr->sh_offset);
   12444             : 
   12445         105 :           handle_notes_data (ebl, ehdr, shdr->sh_offset,
   12446             :                              elf_getdata (scn, NULL));
   12447             :         }
   12448          75 :       return;
   12449             :     }
   12450             : 
   12451             :   /* We have to look through the program header to find the note
   12452             :      sections.  There can be more than one.  */
   12453         140 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
   12454             :     {
   12455         129 :       GElf_Phdr mem;
   12456         129 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
   12457             : 
   12458         129 :       if (phdr == NULL || phdr->p_type != PT_NOTE)
   12459             :         /* Not what we are looking for.  */
   12460         117 :         continue;
   12461             : 
   12462          12 :       printf (gettext ("\
   12463             : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
   12464             :               phdr->p_filesz, phdr->p_offset);
   12465             : 
   12466          12 :       handle_notes_data (ebl, ehdr, phdr->p_offset,
   12467             :                          elf_getdata_rawchunk (ebl->elf,
   12468          12 :                                                phdr->p_offset, phdr->p_filesz,
   12469          12 :                                                (phdr->p_align == 8
   12470             :                                                 ? ELF_T_NHDR8 : ELF_T_NHDR)));
   12471             :     }
   12472             : }
   12473             : 
   12474             : 
   12475             : static void
   12476           6 : hex_dump (const uint8_t *data, size_t len)
   12477             : {
   12478           6 :   size_t pos = 0;
   12479          28 :   while (pos < len)
   12480             :     {
   12481          22 :       printf ("  0x%08zx ", pos);
   12482             : 
   12483          22 :       const size_t chunk = MIN (len - pos, 16);
   12484             : 
   12485         344 :       for (size_t i = 0; i < chunk; ++i)
   12486         322 :         if (i % 4 == 3)
   12487          80 :           printf ("%02x ", data[pos + i]);
   12488             :         else
   12489         242 :           printf ("%02x", data[pos + i]);
   12490             : 
   12491          22 :       if (chunk < 16)
   12492           2 :         printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
   12493             : 
   12494         344 :       for (size_t i = 0; i < chunk; ++i)
   12495             :         {
   12496         322 :           unsigned char b = data[pos + i];
   12497         322 :           printf ("%c", isprint (b) ? b : '.');
   12498             :         }
   12499             : 
   12500          22 :       putchar ('\n');
   12501          22 :       pos += chunk;
   12502             :     }
   12503           6 : }
   12504             : 
   12505             : static void
   12506           6 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
   12507             : {
   12508           6 :   if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
   12509           0 :     printf (gettext ("\nSection [%zu] '%s' has no data to dump.\n"),
   12510             :             elf_ndxscn (scn), name);
   12511             :   else
   12512             :     {
   12513           6 :       if (print_decompress)
   12514             :         {
   12515             :           /* We try to decompress the section, but keep the old shdr around
   12516             :              so we can show both the original shdr size and the uncompressed
   12517             :              data size.   */
   12518           4 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
   12519             :             {
   12520           2 :               if (elf_compress (scn, 0, 0) < 0)
   12521           0 :                 printf ("WARNING: %s [%zd]\n",
   12522             :                         gettext ("Couldn't uncompress section"),
   12523             :                         elf_ndxscn (scn));
   12524             :             }
   12525           2 :           else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
   12526             :             {
   12527           2 :               if (elf_compress_gnu (scn, 0, 0) < 0)
   12528           0 :                 printf ("WARNING: %s [%zd]\n",
   12529             :                         gettext ("Couldn't uncompress section"),
   12530             :                         elf_ndxscn (scn));
   12531             :             }
   12532             :         }
   12533             : 
   12534           6 :       Elf_Data *data = elf_rawdata (scn, NULL);
   12535           6 :       if (data == NULL)
   12536           0 :         error (0, 0, gettext ("cannot get data for section [%zu] '%s': %s"),
   12537             :                elf_ndxscn (scn), name, elf_errmsg (-1));
   12538             :       else
   12539             :         {
   12540           6 :           if (data->d_size == shdr->sh_size)
   12541           2 :             printf (gettext ("\nHex dump of section [%zu] '%s', %" PRIu64
   12542             :                              " bytes at offset %#0" PRIx64 ":\n"),
   12543             :                     elf_ndxscn (scn), name,
   12544             :                     shdr->sh_size, shdr->sh_offset);
   12545             :           else
   12546           4 :             printf (gettext ("\nHex dump of section [%zu] '%s', %" PRIu64
   12547             :                              " bytes (%zd uncompressed) at offset %#0"
   12548             :                              PRIx64 ":\n"),
   12549             :                     elf_ndxscn (scn), name,
   12550             :                     shdr->sh_size, data->d_size, shdr->sh_offset);
   12551           6 :           hex_dump (data->d_buf, data->d_size);
   12552             :         }
   12553             :     }
   12554           6 : }
   12555             : 
   12556             : static void
   12557         133 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
   12558             : {
   12559         133 :   if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
   12560           7 :     printf (gettext ("\nSection [%zu] '%s' has no strings to dump.\n"),
   12561             :             elf_ndxscn (scn), name);
   12562             :   else
   12563             :     {
   12564         126 :       if (print_decompress)
   12565             :         {
   12566             :           /* We try to decompress the section, but keep the old shdr around
   12567             :              so we can show both the original shdr size and the uncompressed
   12568             :              data size.  */
   12569           1 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
   12570             :             {
   12571           0 :               if (elf_compress (scn, 0, 0) < 0)
   12572           0 :                 printf ("WARNING: %s [%zd]\n",
   12573             :                         gettext ("Couldn't uncompress section"),
   12574             :                         elf_ndxscn (scn));
   12575             :             }
   12576           1 :           else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
   12577             :             {
   12578           1 :               if (elf_compress_gnu (scn, 0, 0) < 0)
   12579           0 :                 printf ("WARNING: %s [%zd]\n",
   12580             :                         gettext ("Couldn't uncompress section"),
   12581             :                         elf_ndxscn (scn));
   12582             :             }
   12583             :         }
   12584             : 
   12585         126 :       Elf_Data *data = elf_rawdata (scn, NULL);
   12586         126 :       if (data == NULL)
   12587           0 :         error (0, 0, gettext ("cannot get data for section [%zu] '%s': %s"),
   12588             :                elf_ndxscn (scn), name, elf_errmsg (-1));
   12589             :       else
   12590             :         {
   12591         126 :           if (data->d_size == shdr->sh_size)
   12592         125 :             printf (gettext ("\nString section [%zu] '%s' contains %" PRIu64
   12593             :                              " bytes at offset %#0" PRIx64 ":\n"),
   12594             :                     elf_ndxscn (scn), name,
   12595             :                     shdr->sh_size, shdr->sh_offset);
   12596             :           else
   12597           1 :             printf (gettext ("\nString section [%zu] '%s' contains %" PRIu64
   12598             :                              " bytes (%zd uncompressed) at offset %#0"
   12599             :                              PRIx64 ":\n"),
   12600             :                     elf_ndxscn (scn), name,
   12601             :                     shdr->sh_size, data->d_size, shdr->sh_offset);
   12602             : 
   12603         126 :           const char *start = data->d_buf;
   12604         126 :           const char *const limit = start + data->d_size;
   12605       22387 :           do
   12606             :             {
   12607       22387 :               const char *end = memchr (start, '\0', limit - start);
   12608       22387 :               const size_t pos = start - (const char *) data->d_buf;
   12609       22387 :               if (unlikely (end == NULL))
   12610             :                 {
   12611           0 :                   printf ("  [%6zx]- %.*s\n",
   12612             :                           pos, (int) (limit - start), start);
   12613             :                   break;
   12614             :                 }
   12615       22387 :               printf ("  [%6zx]  %s\n", pos, start);
   12616       22387 :               start = end + 1;
   12617       22387 :             } while (start < limit);
   12618             :         }
   12619             :     }
   12620         133 : }
   12621             : 
   12622             : static void
   12623          71 : for_each_section_argument (Elf *elf, const struct section_argument *list,
   12624             :                            void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
   12625             :                                          const char *name))
   12626             : {
   12627             :   /* Get the section header string table index.  */
   12628          71 :   size_t shstrndx;
   12629          71 :   if (elf_getshdrstrndx (elf, &shstrndx) < 0)
   12630           0 :     error (EXIT_FAILURE, 0,
   12631           0 :            gettext ("cannot get section header string table index"));
   12632             : 
   12633         270 :   for (const struct section_argument *a = list; a != NULL; a = a->next)
   12634             :     {
   12635         199 :       Elf_Scn *scn;
   12636         199 :       GElf_Shdr shdr_mem;
   12637         199 :       const char *name = NULL;
   12638             : 
   12639         199 :       char *endp = NULL;
   12640         199 :       unsigned long int shndx = strtoul (a->arg, &endp, 0);
   12641         199 :       if (endp != a->arg && *endp == '\0')
   12642             :         {
   12643           1 :           scn = elf_getscn (elf, shndx);
   12644           1 :           if (scn == NULL)
   12645             :             {
   12646           0 :               error (0, 0, gettext ("\nsection [%lu] does not exist"), shndx);
   12647           0 :               continue;
   12648             :             }
   12649             : 
   12650           1 :           if (gelf_getshdr (scn, &shdr_mem) == NULL)
   12651           0 :             error (EXIT_FAILURE, 0, gettext ("cannot get section header: %s"),
   12652             :                    elf_errmsg (-1));
   12653           1 :           name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
   12654           1 :           (*dump) (scn, &shdr_mem, name);
   12655             :         }
   12656             :       else
   12657             :         {
   12658             :           /* Need to look up the section by name.  */
   12659             :           scn = NULL;
   12660             :           bool found = false;
   12661        5361 :           while ((scn = elf_nextscn (elf, scn)) != NULL)
   12662             :             {
   12663        5163 :               if (gelf_getshdr (scn, &shdr_mem) == NULL)
   12664             :                 continue;
   12665        5163 :               name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
   12666        5163 :               if (name == NULL)
   12667             :                 continue;
   12668        5163 :               if (!strcmp (name, a->arg))
   12669             :                 {
   12670         138 :                   found = true;
   12671         138 :                   (*dump) (scn, &shdr_mem, name);
   12672             :                 }
   12673             :             }
   12674             : 
   12675         198 :           if (unlikely (!found) && !a->implicit)
   12676           0 :             error (0, 0, gettext ("\nsection '%s' does not exist"), a->arg);
   12677             :         }
   12678             :     }
   12679          71 : }
   12680             : 
   12681             : static void
   12682           0 : dump_data (Ebl *ebl)
   12683             : {
   12684           6 :   for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
   12685           0 : }
   12686             : 
   12687             : static void
   12688           0 : dump_strings (Ebl *ebl)
   12689             : {
   12690          65 :   for_each_section_argument (ebl->elf, string_sections, &print_string_section);
   12691           0 : }
   12692             : 
   12693             : static void
   12694           0 : print_strings (Ebl *ebl)
   12695             : {
   12696             :   /* Get the section header string table index.  */
   12697           0 :   size_t shstrndx;
   12698           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
   12699           0 :     error (EXIT_FAILURE, 0,
   12700           0 :            gettext ("cannot get section header string table index"));
   12701             : 
   12702             :   Elf_Scn *scn;
   12703             :   GElf_Shdr shdr_mem;
   12704             :   const char *name;
   12705             :   scn = NULL;
   12706           0 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   12707             :     {
   12708           0 :       if (gelf_getshdr (scn, &shdr_mem) == NULL)
   12709           0 :         continue;
   12710             : 
   12711           0 :       if (shdr_mem.sh_type != SHT_PROGBITS
   12712           0 :           || !(shdr_mem.sh_flags & SHF_STRINGS))
   12713           0 :         continue;
   12714             : 
   12715           0 :       name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
   12716           0 :       if (name == NULL)
   12717           0 :         continue;
   12718             : 
   12719           0 :       print_string_section (scn, &shdr_mem, name);
   12720             :     }
   12721           0 : }
   12722             : 
   12723             : static void
   12724           2 : dump_archive_index (Elf *elf, const char *fname)
   12725             : {
   12726           2 :   size_t narsym;
   12727           2 :   const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
   12728           2 :   if (arsym == NULL)
   12729             :     {
   12730           0 :       int result = elf_errno ();
   12731           0 :       if (unlikely (result != ELF_E_NO_INDEX))
   12732           0 :         error (EXIT_FAILURE, 0,
   12733           0 :                gettext ("cannot get symbol index of archive '%s': %s"),
   12734             :                fname, elf_errmsg (result));
   12735             :       else
   12736           0 :         printf (gettext ("\nArchive '%s' has no symbol index\n"), fname);
   12737           0 :       return;
   12738             :     }
   12739             : 
   12740           2 :   printf (gettext ("\nIndex of archive '%s' has %zu entries:\n"),
   12741             :           fname, narsym);
   12742             : 
   12743           2 :   size_t as_off = 0;
   12744          11 :   for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
   12745             :     {
   12746           9 :       if (s->as_off != as_off)
   12747             :         {
   12748           6 :           as_off = s->as_off;
   12749             : 
   12750           6 :           Elf *subelf = NULL;
   12751           6 :           if (unlikely (elf_rand (elf, as_off) == 0)
   12752           6 :               || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
   12753             :                            == NULL))
   12754             : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
   12755             :             while (1)
   12756             : #endif
   12757           0 :               error (EXIT_FAILURE, 0,
   12758           0 :                      gettext ("cannot extract member at offset %zu in '%s': %s"),
   12759             :                      as_off, fname, elf_errmsg (-1));
   12760             : 
   12761           6 :           const Elf_Arhdr *h = elf_getarhdr (subelf);
   12762             : 
   12763           6 :           printf (gettext ("Archive member '%s' contains:\n"), h->ar_name);
   12764             : 
   12765           6 :           elf_end (subelf);
   12766             :         }
   12767             : 
   12768           9 :       printf ("\t%s\n", s->as_name);
   12769             :     }
   12770             : }
   12771             : 
   12772             : #include "debugpred.h"

Generated by: LCOV version 1.13