LCOV - code coverage report
Current view: top level - src - readelf.c (source / functions) Hit Total Coverage
Test: elfutils-0.189 Lines: 4588 6812 67.4 %
Date: 2023-05-09 17:18:35 Functions: 139 155 89.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2580 4886 52.8 %

           Branch data     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 <locale.h>
      34                 :            : #include <stdarg.h>
      35                 :            : #include <stdbool.h>
      36                 :            : #include <stdio.h>
      37                 :            : #include <stdio_ext.h>
      38                 :            : #include <stdlib.h>
      39                 :            : #include <string.h>
      40                 :            : #include <strings.h>
      41                 :            : #include <time.h>
      42                 :            : #include <unistd.h>
      43                 :            : #include <sys/stat.h>
      44                 :            : #include <signal.h>
      45                 :            : 
      46                 :            : #include <libeu.h>
      47                 :            : #include <system.h>
      48                 :            : #include <printversion.h>
      49                 :            : #include "../libelf/libelfP.h"
      50                 :            : #include "../libelf/common.h"
      51                 :            : #include "../libebl/libeblP.h"
      52                 :            : #include "../libdwelf/libdwelf.h"
      53                 :            : #include "../libdw/libdwP.h"
      54                 :            : #include "../libdwfl/libdwflP.h"
      55                 :            : #include "../libdw/memory-access.h"
      56                 :            : 
      57                 :            : #include "../libdw/known-dwarf.h"
      58                 :            : 
      59                 :            : #ifdef __linux__
      60                 :            : #define CORE_SIGILL  SIGILL
      61                 :            : #define CORE_SIGBUS  SIGBUS
      62                 :            : #define CORE_SIGFPE  SIGFPE
      63                 :            : #define CORE_SIGSEGV SIGSEGV
      64                 :            : #define CORE_SI_USER SI_USER
      65                 :            : #else
      66                 :            : /* We want the linux version of those as that is what shows up in the core files. */
      67                 :            : #define CORE_SIGILL  4  /* Illegal instruction (ANSI).  */
      68                 :            : #define CORE_SIGBUS  7  /* BUS error (4.2 BSD).  */
      69                 :            : #define CORE_SIGFPE  8  /* Floating-point exception (ANSI).  */
      70                 :            : #define CORE_SIGSEGV 11 /* Segmentation violation (ANSI).  */
      71                 :            : #define CORE_SI_USER 0  /* Sent by kill, sigsend.  */
      72                 :            : #endif
      73                 :            : 
      74                 :            : /* Name and version of program.  */
      75                 :            : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      76                 :            : 
      77                 :            : /* Bug report address.  */
      78                 :            : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      79                 :            : 
      80                 :            : /* argp key value for --elf-section, non-ascii.  */
      81                 :            : #define ELF_INPUT_SECTION 256
      82                 :            : 
      83                 :            : /* argp key value for --dwarf-skeleton, non-ascii.  */
      84                 :            : #define DWARF_SKELETON 257
      85                 :            : 
      86                 :            : /* argp key value for --dyn-syms, non-ascii.  */
      87                 :            : #define PRINT_DYNSYM_TABLE 258
      88                 :            : 
      89                 :            : /* Terrible hack for hooking unrelated skeleton/split compile units,
      90                 :            :    see __libdw_link_skel_split in print_debug.  */
      91                 :            : static bool do_not_close_dwfl = false;
      92                 :            : 
      93                 :            : /* Definitions of arguments for argp functions.  */
      94                 :            : static const struct argp_option options[] =
      95                 :            : {
      96                 :            :   { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 },
      97                 :            :   { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL,
      98                 :            :     N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF "
      99                 :            :        "input data"), 0 },
     100                 :            :   { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0,
     101                 :            :     N_("Used with -w to find the skeleton Compile Units in FILE associated "
     102                 :            :        "with the Split Compile units in a .dwo input file"), 0 },
     103                 :            :   { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 },
     104                 :            :   { "all", 'a', NULL, 0,
     105                 :            :     N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 },
     106                 :            :   { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 },
     107                 :            :   { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 },
     108                 :            :   { "histogram", 'I', NULL, 0,
     109                 :            :     N_("Display histogram of bucket list lengths"), 0 },
     110                 :            :   { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 },
     111                 :            :   { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     112                 :            :   { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
     113                 :            :   { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 },
     114                 :            :   { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
     115                 :            :   { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     116                 :            :   { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
     117                 :            :     N_("Display the symbol table sections"), 0 },
     118                 :            :   { "syms", 's', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     119                 :            :   { "dyn-syms", PRINT_DYNSYM_TABLE, NULL, 0,
     120                 :            :     N_("Display (only) the dynamic symbol table"), 0 },
     121                 :            :   { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
     122                 :            :   { "notes", 'n', "SECTION", OPTION_ARG_OPTIONAL, N_("Display the ELF notes"), 0 },
     123                 :            :   { "arch-specific", 'A', NULL, 0,
     124                 :            :     N_("Display architecture specific information, if any"), 0 },
     125                 :            :   { "exception", 'e', NULL, 0,
     126                 :            :     N_("Display sections for exception handling"), 0 },
     127                 :            : 
     128                 :            :   { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
     129                 :            :   { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
     130                 :            :     N_("Display DWARF section content.  SECTION can be one of abbrev, addr, "
     131                 :            :        "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, "
     132                 :            :        "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 },
     133                 :            :   { "hex-dump", 'x', "SECTION", 0,
     134                 :            :     N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
     135                 :            :   { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
     136                 :            :     N_("Print string contents of sections"), 0 },
     137                 :            :   { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     138                 :            :   { "archive-index", 'c', NULL, 0,
     139                 :            :     N_("Display the symbol index of an archive"), 0 },
     140                 :            :   { "use-dynamic", 'D', NULL, 0,
     141                 :            :     N_("Use the dynamic segment when possible for displaying info"), 0 },
     142                 :            : 
     143                 :            :   { NULL, 0, NULL, 0, N_("Output control:"), 0 },
     144                 :            :   { "numeric-addresses", 'N', NULL, 0,
     145                 :            :     N_("Do not find symbol names for addresses in DWARF data"), 0 },
     146                 :            :   { "unresolved-address-offsets", 'U', NULL, 0,
     147                 :            :     N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
     148                 :            :   { "wide", 'W', NULL, 0,
     149                 :            :     N_("Ignored for compatibility (lines always wide)"), 0 },
     150                 :            :   { "decompress", 'z', NULL, 0,
     151                 :            :     N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
     152                 :            :   { NULL, 0, NULL, 0, NULL, 0 }
     153                 :            : };
     154                 :            : 
     155                 :            : /* Short description of program.  */
     156                 :            : static const char doc[] = N_("\
     157                 :            : Print information from ELF file in human-readable form.");
     158                 :            : 
     159                 :            : /* Strings for arguments in help texts.  */
     160                 :            : static const char args_doc[] = N_("FILE...");
     161                 :            : 
     162                 :            : /* Prototype for option handler.  */
     163                 :            : static error_t parse_opt (int key, char *arg, struct argp_state *state);
     164                 :            : 
     165                 :            : /* Data structure to communicate with argp functions.  */
     166                 :            : static struct argp argp =
     167                 :            : {
     168                 :            :   options, parse_opt, args_doc, doc, NULL, NULL, NULL
     169                 :            : };
     170                 :            : 
     171                 :            : /* If non-null, the section from which we should read to (compressed) ELF.  */
     172                 :            : static const char *elf_input_section = NULL;
     173                 :            : 
     174                 :            : /* If non-null, the file that contains the skeleton CUs.  */
     175                 :            : static const char *dwarf_skeleton = NULL;
     176                 :            : 
     177                 :            : /* Flags set by the option controlling the output.  */
     178                 :            : 
     179                 :            : /* True if dynamic segment should be printed.  */
     180                 :            : static bool print_dynamic_table;
     181                 :            : 
     182                 :            : /* True if the file header should be printed.  */
     183                 :            : static bool print_file_header;
     184                 :            : 
     185                 :            : /* True if the program headers should be printed.  */
     186                 :            : static bool print_program_header;
     187                 :            : 
     188                 :            : /* True if relocations should be printed.  */
     189                 :            : static bool print_relocations;
     190                 :            : 
     191                 :            : /* True if the section headers should be printed.  */
     192                 :            : static bool print_section_header;
     193                 :            : 
     194                 :            : /* True if the symbol table should be printed.  */
     195                 :            : static bool print_symbol_table;
     196                 :            : 
     197                 :            : /* True if (only) the dynsym table should be printed.  */
     198                 :            : static bool print_dynsym_table;
     199                 :            : 
     200                 :            : /* True if reconstruct dynamic symbol table from the PT_DYNAMIC segment.  */
     201                 :            : static bool use_dynamic_segment;
     202                 :            : 
     203                 :            : /* A specific section name, or NULL to print all symbol tables.  */
     204                 :            : static char *symbol_table_section;
     205                 :            : 
     206                 :            : /* A specific section name, or NULL to print all ELF notes.  */
     207                 :            : static char *notes_section;
     208                 :            : 
     209                 :            : /* True if the version information should be printed.  */
     210                 :            : static bool print_version_info;
     211                 :            : 
     212                 :            : /* True if section groups should be printed.  */
     213                 :            : static bool print_section_groups;
     214                 :            : 
     215                 :            : /* True if bucket list length histogram should be printed.  */
     216                 :            : static bool print_histogram;
     217                 :            : 
     218                 :            : /* True if the architecture specific data should be printed.  */
     219                 :            : static bool print_arch;
     220                 :            : 
     221                 :            : /* True if note section content should be printed.  */
     222                 :            : static bool print_notes;
     223                 :            : 
     224                 :            : /* True if SHF_STRINGS section content should be printed.  */
     225                 :            : static bool print_string_sections;
     226                 :            : 
     227                 :            : /* True if archive index should be printed.  */
     228                 :            : static bool print_archive_index;
     229                 :            : 
     230                 :            : /* True if any of the control options except print_archive_index is set.  */
     231                 :            : static bool any_control_option;
     232                 :            : 
     233                 :            : /* True if we should print addresses from DWARF in symbolic form.  */
     234                 :            : static bool print_address_names = true;
     235                 :            : 
     236                 :            : /* True if we should print raw values instead of relativized addresses.  */
     237                 :            : static bool print_unresolved_addresses = false;
     238                 :            : 
     239                 :            : /* True if we should print the .debug_aranges section using libdw.  */
     240                 :            : static bool decodedaranges = false;
     241                 :            : 
     242                 :            : /* True if we should print the .debug_aranges section using libdw.  */
     243                 :            : static bool decodedline = false;
     244                 :            : 
     245                 :            : /* True if we want to show more information about compressed sections.  */
     246                 :            : static bool print_decompress = false;
     247                 :            : 
     248                 :            : /* True if we want to show split compile units for debug_info skeletons.  */
     249                 :            : static bool show_split_units = false;
     250                 :            : 
     251                 :            : /* Select printing of debugging sections.  */
     252                 :            : static enum section_e
     253                 :            : {
     254                 :            :   section_abbrev = 1,           /* .debug_abbrev  */
     255                 :            :   section_aranges = 2,          /* .debug_aranges  */
     256                 :            :   section_frame = 4,            /* .debug_frame or .eh_frame & al.  */
     257                 :            :   section_info = 8,             /* .debug_info, (implies .debug_types)  */
     258                 :            :   section_line = 16,            /* .debug_line  */
     259                 :            :   section_loc = 32,             /* .debug_loc  */
     260                 :            :   section_pubnames = 64,        /* .debug_pubnames  */
     261                 :            :   section_str = 128,            /* .debug_str  */
     262                 :            :   section_macinfo = 256,        /* .debug_macinfo  */
     263                 :            :   section_ranges = 512,         /* .debug_ranges  */
     264                 :            :   section_exception = 1024,     /* .eh_frame & al.  */
     265                 :            :   section_gdb_index = 2048,     /* .gdb_index  */
     266                 :            :   section_macro = 4096,         /* .debug_macro  */
     267                 :            :   section_addr = 8192,          /* .debug_addr  */
     268                 :            :   section_types = 16384,        /* .debug_types (implied by .debug_info)  */
     269                 :            :   section_all = (section_abbrev | section_aranges | section_frame
     270                 :            :                  | section_info | section_line | section_loc
     271                 :            :                  | section_pubnames | section_str | section_macinfo
     272                 :            :                  | section_ranges | section_exception | section_gdb_index
     273                 :            :                  | section_macro | section_addr | section_types)
     274                 :            : } print_debug_sections, implicit_debug_sections;
     275                 :            : 
     276                 :            : /* Select hex dumping of sections.  */
     277                 :            : static struct section_argument *dump_data_sections;
     278                 :            : static struct section_argument **dump_data_sections_tail = &dump_data_sections;
     279                 :            : 
     280                 :            : /* Select string dumping of sections.  */
     281                 :            : static struct section_argument *string_sections;
     282                 :            : static struct section_argument **string_sections_tail = &string_sections;
     283                 :            : 
     284                 :            : struct section_argument
     285                 :            : {
     286                 :            :   struct section_argument *next;
     287                 :            :   const char *arg;
     288                 :            :   bool implicit;
     289                 :            : };
     290                 :            : 
     291                 :            : /* Numbers of sections and program headers in the file.  */
     292                 :            : static size_t shnum;
     293                 :            : static size_t phnum;
     294                 :            : 
     295                 :            : 
     296                 :            : /* Declarations of local functions.  */
     297                 :            : static void process_file (int fd, const char *fname, bool only_one);
     298                 :            : static void process_elf_file (Dwfl_Module *dwflmod, int fd);
     299                 :            : static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
     300                 :            : static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
     301                 :            : static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
     302                 :            : static void print_scngrp (Ebl *ebl);
     303                 :            : static void print_dynamic (Ebl *ebl);
     304                 :            : static void print_relocs (Ebl *ebl, GElf_Ehdr *ehdr);
     305                 :            : static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
     306                 :            :                                GElf_Shdr *shdr);
     307                 :            : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
     308                 :            :                                 GElf_Shdr *shdr);
     309                 :            : static bool print_symtab (Ebl *ebl, int type);
     310                 :            : static bool handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     311                 :            : static bool handle_dynamic_symtab (Ebl *ebl);
     312                 :            : static void
     313                 :            : process_symtab(
     314                 :            :         Ebl * ebl,
     315                 :            :         unsigned int nsyms,
     316                 :            :         Elf64_Word idx,
     317                 :            :         Elf32_Word verneed_stridx,
     318                 :            :         Elf32_Word verdef_stridx,
     319                 :            :         Elf_Data * symdata,
     320                 :            :         Elf_Data * versym_data,
     321                 :            :         Elf_Data * symstr_data,
     322                 :            :         Elf_Data * verneed_data,
     323                 :            :         Elf_Data * verdef_data,
     324                 :            :         Elf_Data * xndx_data);
     325                 :            : static void print_verinfo (Ebl *ebl);
     326                 :            : static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     327                 :            : static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     328                 :            : static void handle_versym (Ebl *ebl, Elf_Scn *scn,
     329                 :            :                            GElf_Shdr *shdr);
     330                 :            : static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
     331                 :            : static void handle_hash (Ebl *ebl);
     332                 :            : static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
     333                 :            : static void print_liblist (Ebl *ebl);
     334                 :            : static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
     335                 :            : static void dump_data (Ebl *ebl);
     336                 :            : static void dump_strings (Ebl *ebl);
     337                 :            : static void print_strings (Ebl *ebl);
     338                 :            : static void dump_archive_index (Elf *, const char *);
     339                 :            : 
     340                 :            : enum dyn_idx
     341                 :            : {
     342                 :            :   i_symtab_shndx,
     343                 :            :   i_strsz,
     344                 :            :   i_verneed,
     345                 :            :   i_verneednum,
     346                 :            :   i_verdef,
     347                 :            :   i_verdefnum,
     348                 :            :   i_versym,
     349                 :            :   i_symtab,
     350                 :            :   i_strtab,
     351                 :            :   i_hash,
     352                 :            :   i_gnu_hash,
     353                 :            :   i_max
     354                 :            : };
     355                 :            : 
     356                 :            : /* Declarations of local functions for use-dynamic.  */
     357                 :            : static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr);
     358                 :            : static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]);
     359                 :            : static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n,
     360                 :            :                           GElf_Addr addrs[n], GElf_Off offs[n]);
     361                 :            : 
     362                 :            : /* Looked up once with gettext in main.  */
     363                 :            : static char *yes_str;
     364                 :            : static char *no_str;
     365                 :            : 
     366                 :            : static void
     367                 :        970 : cleanup_list (struct section_argument *list)
     368                 :            : {
     369   [ +  +  +  + ]:       1403 :   while (list != NULL)
     370                 :            :     {
     371                 :        433 :       struct section_argument *a = list;
     372                 :        433 :       list = a->next;
     373                 :        433 :       free (a);
     374                 :            :     }
     375                 :            : }
     376                 :            : 
     377                 :            : int
     378                 :        485 : main (int argc, char *argv[])
     379                 :            : {
     380                 :            :   /* We use no threads here which can interfere with handling a stream.  */
     381                 :        485 :   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
     382                 :            : 
     383                 :            :   /* Set locale.  */
     384                 :        485 :   setlocale (LC_ALL, "");
     385                 :            : 
     386                 :            :   /* Initialize the message catalog.  */
     387                 :        485 :   textdomain (PACKAGE_TARNAME);
     388                 :            : 
     389                 :            :   /* Look up once.  */
     390                 :        485 :   yes_str = _("yes");
     391                 :        485 :   no_str = _("no");
     392                 :            : 
     393                 :            :   /* Parse and process arguments.  */
     394                 :        485 :   int remaining;
     395                 :        485 :   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
     396                 :            : 
     397                 :            :   /* Before we start tell the ELF library which version we are using.  */
     398                 :        485 :   elf_version (EV_CURRENT);
     399                 :            : 
     400                 :            :   /* Now process all the files given at the command line.  */
     401                 :        485 :   bool only_one = remaining + 1 == argc;
     402                 :        524 :   do
     403                 :            :     {
     404                 :            :       /* Open the file.  */
     405                 :        524 :       int fd = open (argv[remaining], O_RDONLY);
     406         [ -  + ]:        524 :       if (fd == -1)
     407                 :            :         {
     408                 :          0 :           error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
     409                 :          0 :           continue;
     410                 :            :         }
     411                 :            : 
     412                 :        524 :       process_file (fd, argv[remaining], only_one);
     413                 :            : 
     414                 :        524 :       close (fd);
     415                 :            :     }
     416         [ +  + ]:        524 :   while (++remaining < argc);
     417                 :            : 
     418                 :        485 :   cleanup_list (dump_data_sections);
     419                 :        485 :   cleanup_list (string_sections);
     420                 :            : 
     421                 :        485 :   return error_message_count != 0;
     422                 :            : }
     423                 :            : 
     424                 :            : static void
     425                 :        433 : add_dump_section (const char *name,
     426                 :            :                   int key,
     427                 :            :                   bool implicit)
     428                 :            : {
     429                 :        433 :   struct section_argument *a = xmalloc (sizeof *a);
     430                 :        433 :   a->arg = name;
     431                 :        433 :   a->next = NULL;
     432                 :        433 :   a->implicit = implicit;
     433                 :        866 :   struct section_argument ***tailp
     434         [ +  + ]:        433 :     = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
     435                 :        433 :   **tailp = a;
     436                 :        433 :   *tailp = &a->next;
     437                 :        433 : }
     438                 :            : 
     439                 :            : /* Handle program arguments.  */
     440                 :            : static error_t
     441                 :       3029 : parse_opt (int key, char *arg,
     442                 :            :            struct argp_state *state __attribute__ ((unused)))
     443                 :            : {
     444   [ +  +  +  +  :       3029 :   switch (key)
          -  +  +  -  +  
          +  +  +  +  +  
          -  +  +  +  +  
          +  +  -  +  +  
             +  +  -  + ]
     445                 :            :     {
     446                 :        142 :     case 'a':
     447                 :        142 :       print_file_header = true;
     448                 :        142 :       print_program_header = true;
     449                 :        142 :       print_relocations = true;
     450                 :        142 :       print_section_header = true;
     451                 :        142 :       print_symbol_table = true;
     452                 :        142 :       print_version_info = true;
     453                 :        142 :       print_dynamic_table = true;
     454                 :        142 :       print_section_groups = true;
     455                 :        142 :       print_histogram = true;
     456                 :        142 :       print_arch = true;
     457                 :        142 :       print_notes = true;
     458                 :        142 :       implicit_debug_sections |= section_exception;
     459                 :        142 :       add_dump_section (".strtab", key, true);
     460                 :        142 :       add_dump_section (".dynstr", key, true);
     461                 :        142 :       add_dump_section (".comment", key, true);
     462                 :        142 :       any_control_option = true;
     463                 :        142 :       break;
     464                 :          4 :     case 'A':
     465                 :          4 :       print_arch = true;
     466                 :          4 :       any_control_option = true;
     467                 :          4 :       break;
     468                 :          3 :     case 'd':
     469                 :          3 :       print_dynamic_table = true;
     470                 :          3 :       any_control_option = true;
     471                 :          3 :       break;
     472                 :          1 :     case 'D':
     473                 :          1 :       use_dynamic_segment = true;
     474                 :          1 :       break;
     475                 :          0 :     case 'e':
     476                 :          0 :       print_debug_sections |= section_exception;
     477                 :          0 :       any_control_option = true;
     478                 :          0 :       break;
     479                 :          9 :     case 'g':
     480                 :          9 :       print_section_groups = true;
     481                 :          9 :       any_control_option = true;
     482                 :          9 :       break;
     483                 :          3 :     case 'h':
     484                 :          3 :       print_file_header = true;
     485                 :          3 :       any_control_option = true;
     486                 :          3 :       break;
     487                 :          0 :     case 'I':
     488                 :          0 :       print_histogram = true;
     489                 :          0 :       any_control_option = true;
     490                 :          0 :       break;
     491                 :          2 :     case 'l':
     492                 :          2 :       print_program_header = true;
     493                 :          2 :       any_control_option = true;
     494                 :          2 :       break;
     495                 :         23 :     case 'n':
     496                 :         23 :       print_notes = true;
     497                 :         23 :       any_control_option = true;
     498                 :         23 :       notes_section = arg;
     499                 :         23 :       break;
     500                 :          1 :     case 'r':
     501                 :          1 :       print_relocations = true;
     502                 :          1 :       any_control_option = true;
     503                 :          1 :      break;
     504                 :        161 :     case 'S':
     505                 :        161 :       print_section_header = true;
     506                 :        161 :       any_control_option = true;
     507                 :        161 :       break;
     508                 :         17 :     case 's':
     509                 :         17 :       print_symbol_table = true;
     510                 :         17 :       any_control_option = true;
     511                 :         17 :       symbol_table_section = arg;
     512                 :         17 :       break;
     513                 :          1 :     case PRINT_DYNSYM_TABLE:
     514                 :          1 :       print_dynsym_table = true;
     515                 :          1 :       any_control_option = true;
     516                 :          1 :       break;
     517                 :          0 :     case 'V':
     518                 :          0 :       print_version_info = true;
     519                 :          0 :       any_control_option = true;
     520                 :          0 :       break;
     521                 :          2 :     case 'c':
     522                 :          2 :       print_archive_index = true;
     523                 :          2 :       break;
     524                 :        126 :     case 'w':
     525         [ +  + ]:        126 :       if (arg == NULL)
     526                 :            :         {
     527                 :         41 :           print_debug_sections = section_all;
     528                 :         41 :           implicit_debug_sections = section_info;
     529                 :         41 :           show_split_units = true;
     530                 :            :         }
     531         [ -  + ]:         85 :       else if (strcmp (arg, "abbrev") == 0)
     532                 :          0 :         print_debug_sections |= section_abbrev;
     533         [ +  + ]:         85 :       else if (strcmp (arg, "addr") == 0)
     534                 :            :         {
     535                 :          2 :           print_debug_sections |= section_addr;
     536                 :          2 :           implicit_debug_sections |= section_info;
     537                 :            :         }
     538         [ +  + ]:         83 :       else if (strcmp (arg, "aranges") == 0)
     539                 :          3 :         print_debug_sections |= section_aranges;
     540         [ +  + ]:         80 :       else if (strcmp (arg, "decodedaranges") == 0)
     541                 :            :         {
     542                 :          1 :           print_debug_sections |= section_aranges;
     543                 :          1 :           decodedaranges = true;
     544                 :            :         }
     545         [ +  + ]:         79 :       else if (strcmp (arg, "ranges") == 0)
     546                 :            :         {
     547                 :         13 :           print_debug_sections |= section_ranges;
     548                 :         13 :           implicit_debug_sections |= section_info;
     549                 :            :         }
     550   [ +  +  +  + ]:         66 :       else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
     551                 :          3 :         print_debug_sections |= section_frame;
     552         [ +  + ]:         63 :       else if (strcmp (arg, "info") == 0)
     553                 :            :         {
     554                 :         19 :           print_debug_sections |= section_info;
     555                 :         19 :           print_debug_sections |= section_types;
     556                 :            :         }
     557         [ +  + ]:         44 :       else if (strcmp (arg, "info+") == 0)
     558                 :            :         {
     559                 :          2 :           print_debug_sections |= section_info;
     560                 :          2 :           print_debug_sections |= section_types;
     561                 :          2 :           show_split_units = true;
     562                 :            :         }
     563         [ +  + ]:         42 :       else if (strcmp (arg, "loc") == 0)
     564                 :            :         {
     565                 :         19 :           print_debug_sections |= section_loc;
     566                 :         19 :           implicit_debug_sections |= section_info;
     567                 :            :         }
     568         [ +  + ]:         23 :       else if (strcmp (arg, "line") == 0)
     569                 :          8 :         print_debug_sections |= section_line;
     570         [ +  + ]:         15 :       else if (strcmp (arg, "decodedline") == 0)
     571                 :            :         {
     572                 :          7 :           print_debug_sections |= section_line;
     573                 :          7 :           decodedline = true;
     574                 :            :         }
     575         [ -  + ]:          8 :       else if (strcmp (arg, "pubnames") == 0)
     576                 :          0 :         print_debug_sections |= section_pubnames;
     577         [ +  + ]:          8 :       else if (strcmp (arg, "str") == 0)
     578                 :            :         {
     579                 :          3 :           print_debug_sections |= section_str;
     580                 :            :           /* For mapping string offset tables to CUs.  */
     581                 :          3 :           implicit_debug_sections |= section_info;
     582                 :            :         }
     583         [ -  + ]:          5 :       else if (strcmp (arg, "macinfo") == 0)
     584                 :          0 :         print_debug_sections |= section_macinfo;
     585         [ +  + ]:          5 :       else if (strcmp (arg, "macro") == 0)
     586                 :          3 :         print_debug_sections |= section_macro;
     587         [ -  + ]:          2 :       else if (strcmp (arg, "exception") == 0)
     588                 :          0 :         print_debug_sections |= section_exception;
     589         [ +  - ]:          2 :       else if (strcmp (arg, "gdb_index") == 0)
     590                 :          2 :         print_debug_sections |= section_gdb_index;
     591                 :            :       else
     592                 :            :         {
     593                 :          0 :           fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
     594                 :            :                    arg);
     595                 :          0 :           argp_help (&argp, stderr, ARGP_HELP_SEE,
     596                 :            :                      program_invocation_short_name);
     597                 :          0 :           exit (1);
     598                 :            :         }
     599                 :        126 :       any_control_option = true;
     600                 :        126 :       break;
     601                 :          1 :     case 'p':
     602                 :          1 :       any_control_option = true;
     603         [ -  + ]:          1 :       if (arg == NULL)
     604                 :            :         {
     605                 :          0 :           print_string_sections = true;
     606                 :          0 :           break;
     607                 :            :         }
     608                 :          7 :       FALLTHROUGH;
     609                 :            :     case 'x':
     610                 :          7 :       add_dump_section (arg, key, false);
     611                 :          7 :       any_control_option = true;
     612                 :          7 :       break;
     613                 :          3 :     case 'N':
     614                 :          3 :       print_address_names = false;
     615                 :          3 :       break;
     616                 :         28 :     case 'U':
     617                 :         28 :       print_unresolved_addresses = true;
     618                 :         28 :       break;
     619                 :          0 :     case ARGP_KEY_NO_ARGS:
     620                 :          0 :       fputs (_("Missing file name.\n"), stderr);
     621                 :          0 :       goto do_argp_help;
     622                 :        485 :     case ARGP_KEY_FINI:
     623   [ +  +  -  + ]:        485 :       if (! any_control_option && ! print_archive_index)
     624                 :            :         {
     625                 :          0 :           fputs (_("No operation specified.\n"), stderr);
     626                 :          0 :         do_argp_help:
     627                 :          0 :           argp_help (&argp, stderr, ARGP_HELP_SEE,
     628                 :            :                      program_invocation_short_name);
     629                 :          0 :           exit (EXIT_FAILURE);
     630                 :            :         }
     631                 :            :       break;
     632                 :            :     case 'W':                   /* Ignored.  */
     633                 :            :       break;
     634                 :         61 :     case 'z':
     635                 :         61 :       print_decompress = true;
     636                 :         61 :       break;
     637                 :          5 :     case ELF_INPUT_SECTION:
     638         [ +  - ]:          5 :       if (arg == NULL)
     639                 :          5 :         elf_input_section = ".gnu_debugdata";
     640                 :            :       else
     641                 :          0 :         elf_input_section = arg;
     642                 :            :       break;
     643                 :          5 :     case DWARF_SKELETON:
     644                 :          5 :       dwarf_skeleton = arg;
     645                 :          5 :       break;
     646                 :            :     default:
     647                 :            :       return ARGP_ERR_UNKNOWN;
     648                 :            :     }
     649                 :            :   return 0;
     650                 :            : }
     651                 :            : 
     652                 :            : 
     653                 :            : /* Create a file descriptor to read the data from the
     654                 :            :    elf_input_section given a file descriptor to an ELF file.  */
     655                 :            : static int
     656                 :          5 : open_input_section (int fd)
     657                 :            : {
     658                 :          5 :   size_t shnums;
     659                 :          5 :   size_t cnt;
     660                 :          5 :   size_t shstrndx;
     661                 :          5 :   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
     662         [ -  + ]:          5 :   if (elf == NULL)
     663                 :            :     {
     664                 :          0 :       error (0, 0, _("cannot generate Elf descriptor: %s"),
     665                 :            :              elf_errmsg (-1));
     666                 :          0 :       return -1;
     667                 :            :     }
     668                 :            : 
     669         [ -  + ]:          5 :   if (elf_getshdrnum (elf, &shnums) < 0)
     670                 :            :     {
     671                 :          0 :       error (0, 0, _("cannot determine number of sections: %s"),
     672                 :            :              elf_errmsg (-1));
     673                 :          0 :     open_error:
     674                 :          0 :       elf_end (elf);
     675                 :          0 :       return -1;
     676                 :            :     }
     677                 :            : 
     678         [ -  + ]:          5 :   if (elf_getshdrstrndx (elf, &shstrndx) < 0)
     679                 :            :     {
     680                 :          0 :       error (0, 0, _("cannot get section header string table index"));
     681                 :          0 :       goto open_error;
     682                 :            :     }
     683                 :            : 
     684         [ +  - ]:        118 :   for (cnt = 0; cnt < shnums; ++cnt)
     685                 :            :     {
     686                 :        118 :       Elf_Scn *scn = elf_getscn (elf, cnt);
     687         [ -  + ]:        118 :       if (scn == NULL)
     688                 :            :         {
     689                 :          0 :           error (0, 0, _("cannot get section: %s"),
     690                 :            :                  elf_errmsg (-1));
     691                 :          0 :           goto open_error;
     692                 :            :         }
     693                 :            : 
     694                 :        118 :       GElf_Shdr shdr_mem;
     695                 :        118 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     696         [ -  + ]:        118 :       if (unlikely (shdr == NULL))
     697                 :            :         {
     698                 :          0 :           error (0, 0, _("cannot get section header: %s"),
     699                 :            :                  elf_errmsg (-1));
     700                 :          0 :           goto open_error;
     701                 :            :         }
     702                 :            : 
     703                 :        118 :       const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
     704         [ -  + ]:        118 :       if (sname == NULL)
     705                 :            :         {
     706                 :          0 :           error (0, 0, _("cannot get section name"));
     707                 :          0 :           goto open_error;
     708                 :            :         }
     709                 :            : 
     710         [ +  + ]:        118 :       if (strcmp (sname, elf_input_section) == 0)
     711                 :            :         {
     712                 :          5 :           Elf_Data *data = elf_rawdata (scn, NULL);
     713         [ -  + ]:          5 :           if (data == NULL)
     714                 :            :             {
     715                 :          0 :               error (0, 0, _("cannot get %s content: %s"),
     716                 :            :                      sname, elf_errmsg (-1));
     717                 :          0 :               goto open_error;
     718                 :            :             }
     719                 :            : 
     720                 :            :           /* Create (and immediately unlink) a temporary file to store
     721                 :            :              section data in to create a file descriptor for it.  */
     722         [ +  - ]:          5 :           const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
     723                 :          5 :           static const char suffix[] = "/readelfXXXXXX";
     724                 :          5 :           int tmplen = strlen (tmpdir) + sizeof (suffix);
     725                 :          5 :           char *tempname = alloca (tmplen);
     726                 :          5 :           sprintf (tempname, "%s%s", tmpdir, suffix);
     727                 :            : 
     728                 :          5 :           int sfd = mkstemp (tempname);
     729         [ -  + ]:          5 :           if (sfd == -1)
     730                 :            :             {
     731                 :          0 :               error (0, 0, _("cannot create temp file '%s'"),
     732                 :            :                      tempname);
     733                 :          0 :               goto open_error;
     734                 :            :             }
     735                 :          5 :           unlink (tempname);
     736                 :            : 
     737                 :          5 :           ssize_t size = data->d_size;
     738         [ -  + ]:          5 :           if (write_retry (sfd, data->d_buf, size) != size)
     739                 :            :             {
     740                 :          0 :               error (0, 0, _("cannot write section data"));
     741                 :          0 :               goto open_error;
     742                 :            :             }
     743                 :            : 
     744         [ -  + ]:          5 :           if (elf_end (elf) != 0)
     745                 :            :             {
     746                 :          0 :               error (0, 0, _("error while closing Elf descriptor: %s"),
     747                 :            :                      elf_errmsg (-1));
     748                 :          5 :               return -1;
     749                 :            :             }
     750                 :            : 
     751         [ -  + ]:          5 :           if (lseek (sfd, 0, SEEK_SET) == -1)
     752                 :            :             {
     753                 :          0 :               error (0, 0, _("error while rewinding file descriptor"));
     754                 :          0 :               return -1;
     755                 :            :             }
     756                 :            : 
     757                 :            :           return sfd;
     758                 :            :         }
     759                 :            :     }
     760                 :            : 
     761                 :            :   /* Named section not found.  */
     762         [ #  # ]:          0 :   if (elf_end (elf) != 0)
     763                 :          0 :     error (0, 0, _("error while closing Elf descriptor: %s"),
     764                 :            :            elf_errmsg (-1));
     765                 :            :   return -1;
     766                 :            : }
     767                 :            : 
     768                 :            : /* Check if the file is an archive, and if so dump its index.  */
     769                 :            : static void
     770                 :          2 : check_archive_index (int fd, const char *fname, bool only_one)
     771                 :            : {
     772                 :            :   /* Create an `Elf' descriptor.  */
     773                 :          2 :   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
     774         [ -  + ]:          2 :   if (elf == NULL)
     775                 :          0 :     error (0, 0, _("cannot generate Elf descriptor: %s"),
     776                 :            :            elf_errmsg (-1));
     777                 :            :   else
     778                 :            :     {
     779         [ +  - ]:          2 :       if (elf_kind (elf) == ELF_K_AR)
     780                 :            :         {
     781         [ -  + ]:          2 :           if (!only_one)
     782                 :          0 :             printf ("\n%s:\n\n", fname);
     783                 :          2 :           dump_archive_index (elf, fname);
     784                 :            :         }
     785                 :            :       else
     786                 :          2 :         error (0, 0,
     787                 :          0 :                _("'%s' is not an archive, cannot print archive index"),
     788                 :            :                fname);
     789                 :            : 
     790                 :            :       /* Now we can close the descriptor.  */
     791         [ -  + ]:          2 :       if (elf_end (elf) != 0)
     792                 :          0 :         error (0, 0, _("error while closing Elf descriptor: %s"),
     793                 :            :                elf_errmsg (-1));
     794                 :            :     }
     795                 :          2 : }
     796                 :            : 
     797                 :            : /* Trivial callback used for checking if we opened an archive.  */
     798                 :            : static int
     799                 :        474 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
     800                 :            :                void **userdata __attribute__ ((unused)),
     801                 :            :                const char *name __attribute__ ((unused)),
     802                 :            :                Dwarf_Addr base __attribute__ ((unused)),
     803                 :            :                void *arg)
     804                 :            : {
     805         [ +  - ]:        474 :   if (*(bool *) arg)
     806                 :            :     return DWARF_CB_ABORT;
     807                 :        474 :   *(bool *) arg = true;
     808                 :        474 :   return DWARF_CB_OK;
     809                 :            : }
     810                 :            : 
     811                 :            : struct process_dwflmod_args
     812                 :            : {
     813                 :            :   int fd;
     814                 :            :   bool only_one;
     815                 :            : };
     816                 :            : 
     817                 :            : static int
     818                 :        522 : process_dwflmod (Dwfl_Module *dwflmod,
     819                 :            :                  void **userdata __attribute__ ((unused)),
     820                 :            :                  const char *name __attribute__ ((unused)),
     821                 :            :                  Dwarf_Addr base __attribute__ ((unused)),
     822                 :            :                  void *arg)
     823                 :            : {
     824                 :        522 :   const struct process_dwflmod_args *a = arg;
     825                 :            : 
     826                 :            :   /* Print the file name.  */
     827         [ +  + ]:        522 :   if (!a->only_one)
     828                 :            :     {
     829                 :         48 :       const char *fname;
     830                 :         48 :       dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
     831                 :            : 
     832                 :         48 :       printf ("\n%s:\n\n", fname);
     833                 :            :     }
     834                 :            : 
     835                 :        522 :   process_elf_file (dwflmod, a->fd);
     836                 :            : 
     837                 :        522 :   return DWARF_CB_OK;
     838                 :            : }
     839                 :            : 
     840                 :            : /* Stub libdwfl callback, only the ELF handle already open is ever used.
     841                 :            :    Only used for finding the alternate debug file if the Dwarf comes from
     842                 :            :    the main file.  We are not interested in separate debuginfo.  */
     843                 :            : static int
     844                 :        176 : find_no_debuginfo (Dwfl_Module *mod,
     845                 :            :                    void **userdata,
     846                 :            :                    const char *modname,
     847                 :            :                    Dwarf_Addr base,
     848                 :            :                    const char *file_name,
     849                 :            :                    const char *debuglink_file,
     850                 :            :                    GElf_Word debuglink_crc,
     851                 :            :                    char **debuginfo_file_name)
     852                 :            : {
     853                 :        176 :   Dwarf_Addr dwbias;
     854                 :        176 :   dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
     855                 :            : 
     856                 :            :   /* We are only interested if the Dwarf has been setup on the main
     857                 :            :      elf file but is only missing the alternate debug link.  If dwbias
     858                 :            :      hasn't even been setup, this is searching for separate debuginfo
     859                 :            :      for the main elf.  We don't care in that case.  */
     860         [ +  + ]:        176 :   if (dwbias == (Dwarf_Addr) -1)
     861                 :            :     return -1;
     862                 :            : 
     863                 :         22 :   return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
     864                 :            :                                        file_name, debuglink_file,
     865                 :            :                                        debuglink_crc, debuginfo_file_name);
     866                 :            : }
     867                 :            : 
     868                 :            : static Dwfl *
     869                 :        531 : create_dwfl (int fd, const char *fname)
     870                 :            : {
     871                 :            :   /* Duplicate an fd for dwfl_report_offline to swallow.  */
     872                 :        531 :   int dwfl_fd = dup (fd);
     873         [ -  + ]:        531 :   if (unlikely (dwfl_fd < 0))
     874                 :          0 :     error_exit (errno, "dup");
     875                 :            : 
     876                 :            :   /* Use libdwfl in a trivial way to open the libdw handle for us.
     877                 :            :      This takes care of applying relocations to DWARF data in ET_REL files.  */
     878                 :        531 :   static const Dwfl_Callbacks callbacks =
     879                 :            :     {
     880                 :            :       .section_address = dwfl_offline_section_address,
     881                 :            :       .find_debuginfo = find_no_debuginfo
     882                 :            :     };
     883                 :        531 :   Dwfl *dwfl = dwfl_begin (&callbacks);
     884         [ +  - ]:        531 :   if (likely (dwfl != NULL))
     885                 :            :     /* Let 0 be the logical address of the file (or first in archive).  */
     886                 :        531 :     dwfl->offline_next_address = 0;
     887         [ -  + ]:        531 :   if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
     888                 :            :     {
     889                 :          0 :       struct stat st;
     890         [ #  # ]:          0 :       if (fstat (dwfl_fd, &st) != 0)
     891                 :          0 :         error (0, errno, _("cannot stat input file"));
     892         [ #  # ]:          0 :       else if (unlikely (st.st_size == 0))
     893                 :          0 :         error (0, 0, _("input file is empty"));
     894                 :            :       else
     895                 :          0 :         error (0, 0, _("failed reading '%s': %s"),
     896                 :            :                fname, dwfl_errmsg (-1));
     897                 :          0 :       close (dwfl_fd);          /* Consumed on success, not on failure.  */
     898                 :          0 :       dwfl = NULL;
     899                 :            :     }
     900                 :            :   else
     901                 :        531 :     dwfl_report_end (dwfl, NULL, NULL);
     902                 :            : 
     903                 :        531 :   return dwfl;
     904                 :            : }
     905                 :            : 
     906                 :            : /* Process one input file.  */
     907                 :            : static void
     908                 :        524 : process_file (int fd, const char *fname, bool only_one)
     909                 :            : {
     910         [ +  + ]:        524 :   if (print_archive_index)
     911                 :          2 :     check_archive_index (fd, fname, only_one);
     912                 :            : 
     913         [ +  + ]:        524 :   if (!any_control_option)
     914                 :            :     return;
     915                 :            : 
     916         [ +  + ]:        522 :   if (elf_input_section != NULL)
     917                 :            :     {
     918                 :            :       /* Replace fname and fd with section content. */
     919                 :          5 :       char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
     920                 :          5 :       sprintf (fnname, "%s:%s", fname, elf_input_section);
     921                 :          5 :       fd = open_input_section (fd);
     922         [ -  + ]:          5 :       if (fd == -1)
     923                 :            :         {
     924                 :          0 :           error (0, 0, _("No such section '%s' in '%s'"),
     925                 :            :                  elf_input_section, fname);
     926                 :          0 :           return;
     927                 :            :         }
     928                 :            :       fname = fnname;
     929                 :            :     }
     930                 :            : 
     931                 :        522 :   Dwfl *dwfl = create_dwfl (fd, fname);
     932         [ +  - ]:        522 :   if (dwfl != NULL)
     933                 :            :     {
     934         [ +  + ]:        522 :       if (only_one)
     935                 :            :         {
     936                 :            :           /* Clear ONLY_ONE if we have multiple modules, from an archive.  */
     937                 :        474 :           bool seen = false;
     938                 :        474 :           only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
     939                 :            :         }
     940                 :            : 
     941                 :            :       /* Process the one or more modules gleaned from this file.  */
     942                 :        522 :       struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
     943                 :        522 :       dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
     944                 :            :     }
     945                 :            :   /* Terrible hack for hooking unrelated skeleton/split compile units,
     946                 :            :      see __libdw_link_skel_split in print_debug.  */
     947         [ +  - ]:        522 :   if (! do_not_close_dwfl)
     948                 :        522 :     dwfl_end (dwfl);
     949                 :            : 
     950                 :            :   /* Need to close the replaced fd if we created it.  Caller takes
     951                 :            :      care of original.  */
     952         [ +  + ]:        522 :   if (elf_input_section != NULL)
     953                 :          5 :     close (fd);
     954                 :            : }
     955                 :            : 
     956                 :            : /* Check whether there are any compressed sections in the ELF file.  */
     957                 :            : static bool
     958                 :        266 : elf_contains_chdrs (Elf *elf)
     959                 :            : {
     960                 :        266 :   Elf_Scn *scn = NULL;
     961         [ +  + ]:     792754 :   while ((scn = elf_nextscn (elf, scn)) != NULL)
     962                 :            :     {
     963                 :     792545 :       GElf_Shdr shdr_mem;
     964                 :     792545 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     965   [ +  -  +  + ]:     792545 :       if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
     966                 :         57 :         return true;
     967                 :            :     }
     968                 :            :   return false;
     969                 :            : }
     970                 :            : 
     971                 :            : /* Process one ELF file.  */
     972                 :            : static void
     973                 :        522 : process_elf_file (Dwfl_Module *dwflmod, int fd)
     974                 :            : {
     975                 :        522 :   GElf_Addr dwflbias;
     976                 :        522 :   Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
     977                 :            : 
     978                 :        522 :   GElf_Ehdr ehdr_mem;
     979                 :        522 :   GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
     980                 :            : 
     981         [ -  + ]:        522 :   if (ehdr == NULL)
     982                 :            :     {
     983                 :          0 :       error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
     984                 :          0 :       return;
     985                 :            :     }
     986                 :            : 
     987                 :        522 :   Ebl *ebl = ebl_openbackend (elf);
     988         [ -  + ]:        522 :   if (unlikely (ebl == NULL))
     989                 :            :     {
     990                 :          0 :     ebl_error:
     991                 :          0 :       error (0, errno, _("cannot create EBL handle"));
     992                 :          0 :       return;
     993                 :            :     }
     994                 :            : 
     995                 :            :   /* Determine the number of sections.  */
     996         [ -  + ]:        522 :   if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
     997                 :          0 :     error_exit (0, _("cannot determine number of sections: %s"),
     998                 :            :                 elf_errmsg (-1));
     999                 :            : 
    1000                 :            :   /* Determine the number of phdrs.  */
    1001         [ -  + ]:        522 :   if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
    1002                 :          0 :     error_exit (0, _("cannot determine number of program headers: %s"),
    1003                 :            :                 elf_errmsg (-1));
    1004                 :            : 
    1005                 :            :   /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
    1006                 :            :      may have applied relocation to some sections.  If there are any
    1007                 :            :      compressed sections, any pass (or libdw/libdwfl) might have
    1008                 :            :      uncompressed them.  So we need to get a fresh Elf handle on the
    1009                 :            :      file to display those.  */
    1010                 :       1044 :   bool print_unchanged = ((print_section_header
    1011         [ +  + ]:        219 :                            || print_relocations
    1012         [ +  + ]:        218 :                            || dump_data_sections != NULL
    1013         [ +  + ]:        212 :                            || print_notes)
    1014   [ +  +  +  + ]:        552 :                           && (ehdr->e_type == ET_REL
    1015         [ +  + ]:        266 :                               || elf_contains_chdrs (ebl->elf)));
    1016                 :            : 
    1017                 :        522 :   Elf *pure_elf = NULL;
    1018                 :        522 :   Ebl *pure_ebl = ebl;
    1019         [ +  + ]:        522 :   if (print_unchanged)
    1020                 :            :     {
    1021                 :            :       /* Read the file afresh.  */
    1022                 :        124 :       off_t aroff = elf_getaroff (elf);
    1023                 :        124 :       pure_elf = dwelf_elf_begin (fd);
    1024         [ -  + ]:        124 :       if (aroff > 0)
    1025                 :            :         {
    1026                 :            :           /* Archive member.  */
    1027                 :          0 :           (void) elf_rand (pure_elf, aroff);
    1028                 :          0 :           Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
    1029                 :          0 :           elf_end (pure_elf);
    1030                 :          0 :           pure_elf = armem;
    1031                 :            :         }
    1032         [ -  + ]:        124 :       if (pure_elf == NULL)
    1033                 :            :         {
    1034                 :          0 :           error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
    1035                 :          0 :           return;
    1036                 :            :         }
    1037                 :        124 :       pure_ebl = ebl_openbackend (pure_elf);
    1038         [ -  + ]:        124 :       if (pure_ebl == NULL)
    1039                 :          0 :         goto ebl_error;
    1040                 :            :     }
    1041                 :            : 
    1042                 :        522 :   bool symtab_printed = false;
    1043                 :            : 
    1044         [ +  + ]:        522 :   if (print_file_header)
    1045                 :        145 :     print_ehdr (ebl, ehdr);
    1046         [ +  + ]:        522 :   if (print_section_header)
    1047                 :        303 :     print_shdr (pure_ebl, ehdr);
    1048         [ +  + ]:        522 :   if (print_program_header)
    1049                 :        144 :     print_phdr (ebl, ehdr);
    1050         [ +  + ]:        522 :   if (print_section_groups)
    1051                 :        151 :     print_scngrp (ebl);
    1052         [ +  + ]:        522 :   if (print_dynamic_table)
    1053                 :        145 :     print_dynamic (ebl);
    1054         [ +  + ]:        522 :   if (print_relocations)
    1055                 :        143 :     print_relocs (pure_ebl, ehdr);
    1056         [ +  + ]:        522 :   if (print_histogram)
    1057                 :        142 :     handle_hash (ebl);
    1058   [ +  +  +  + ]:        522 :   if (print_symbol_table || print_dynsym_table)
    1059                 :        160 :     symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
    1060         [ +  + ]:        522 :   if (print_version_info)
    1061                 :        142 :     print_verinfo (ebl);
    1062   [ +  +  +  - ]:        522 :   if (print_symbol_table && !use_dynamic_segment)
    1063                 :        159 :     symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
    1064                 :            : 
    1065   [ +  +  +  + ]:        522 :   if ((print_symbol_table || print_dynsym_table)
    1066   [ +  +  +  + ]:        160 :       && !symtab_printed && symbol_table_section != NULL)
    1067                 :          1 :     printf ("WARNING: %s: '%s'\n", _("cannot find section"),
    1068                 :            :         symbol_table_section);
    1069                 :            : 
    1070         [ +  + ]:        522 :   if (print_arch)
    1071                 :        146 :     print_liblist (ebl);
    1072         [ +  + ]:        522 :   if (print_arch)
    1073                 :        146 :     print_attributes (ebl, ehdr);
    1074         [ +  + ]:        522 :   if (dump_data_sections != NULL)
    1075                 :          6 :     dump_data (pure_ebl);
    1076         [ +  + ]:        522 :   if (string_sections != NULL)
    1077                 :        143 :     dump_strings (ebl);
    1078         [ +  + ]:        522 :   if ((print_debug_sections | implicit_debug_sections) != 0)
    1079                 :        293 :     print_debug (dwflmod, ebl, ehdr);
    1080         [ +  + ]:        522 :   if (print_notes)
    1081                 :        165 :     handle_notes (pure_ebl, ehdr);
    1082         [ -  + ]:        522 :   if (print_string_sections)
    1083                 :          0 :     print_strings (ebl);
    1084                 :            : 
    1085         [ +  + ]:        522 :   if (pure_ebl != ebl)
    1086                 :            :     {
    1087                 :        124 :       ebl_closebackend (ebl);
    1088                 :        124 :       ebl_closebackend (pure_ebl);
    1089                 :        124 :       elf_end (pure_elf);
    1090                 :            :     }
    1091                 :            :   else
    1092                 :        398 :     ebl_closebackend (ebl);
    1093                 :            : }
    1094                 :            : 
    1095                 :            : 
    1096                 :            : /* Print file type.  */
    1097                 :            : static void
    1098                 :        145 : print_file_type (unsigned short int e_type)
    1099                 :            : {
    1100         [ +  - ]:        145 :   if (likely (e_type <= ET_CORE))
    1101                 :            :     {
    1102                 :        145 :       static const char *const knowntypes[] =
    1103                 :            :       {
    1104                 :            :         N_("NONE (None)"),
    1105                 :            :         N_("REL (Relocatable file)"),
    1106                 :            :         N_("EXEC (Executable file)"),
    1107                 :            :         N_("DYN (Shared object file)"),
    1108                 :            :         N_("CORE (Core file)")
    1109                 :            :       };
    1110                 :        145 :       puts (_(knowntypes[e_type]));
    1111                 :            :     }
    1112         [ #  # ]:          0 :   else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
    1113                 :          0 :     printf (_("OS Specific: (%x)\n"),  e_type);
    1114         [ #  # ]:          0 :   else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
    1115                 :          0 :     printf (_("Processor Specific: (%x)\n"),  e_type);
    1116                 :            :   else
    1117                 :          0 :     puts ("???");
    1118                 :        145 : }
    1119                 :            : 
    1120                 :            : 
    1121                 :            : /* Print ELF header.  */
    1122                 :            : static void
    1123                 :        145 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1124                 :            : {
    1125                 :        145 :   fputs_unlocked (_("ELF Header:\n  Magic:  "), stdout);
    1126         [ +  + ]:       2465 :   for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
    1127                 :       2320 :     printf (" %02hhx", ehdr->e_ident[cnt]);
    1128                 :            : 
    1129                 :        145 :   printf (_("\n  Class:                             %s\n"),
    1130         [ +  + ]:        145 :           ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
    1131                 :            :           : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
    1132         [ -  + ]:        117 :           : "\?\?\?");
    1133                 :            : 
    1134                 :        145 :   printf (_("  Data:                              %s\n"),
    1135         [ +  + ]:        145 :           ehdr->e_ident[EI_DATA] == ELFDATA2LSB
    1136                 :            :           ? "2's complement, little endian"
    1137                 :            :           : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
    1138         [ -  + ]:         12 :           ? "2's complement, big endian" : "\?\?\?");
    1139                 :            : 
    1140                 :        435 :   printf (_("  Ident Version:                     %hhd %s\n"),
    1141                 :        145 :           ehdr->e_ident[EI_VERSION],
    1142         [ +  - ]:        145 :           ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
    1143                 :            :           : "(\?\?\?)");
    1144                 :            : 
    1145                 :        145 :   char buf[512];
    1146                 :        145 :   printf (_("  OS/ABI:                            %s\n"),
    1147                 :        145 :           ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
    1148                 :            : 
    1149                 :        290 :   printf (_("  ABI Version:                       %hhd\n"),
    1150                 :        145 :           ehdr->e_ident[EI_ABIVERSION]);
    1151                 :            : 
    1152                 :        145 :   fputs_unlocked (_("  Type:                              "), stdout);
    1153                 :        145 :   print_file_type (ehdr->e_type);
    1154                 :            : 
    1155                 :        145 :   const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
    1156         [ +  - ]:        145 :   if (machine != NULL)
    1157                 :        145 :     printf (_("  Machine:                           %s\n"), machine);
    1158                 :            :   else
    1159                 :        145 :     printf (_("  Machine:                           <unknown>: 0x%x\n"),
    1160                 :          0 :             ehdr->e_machine);
    1161                 :            : 
    1162                 :        145 :   printf (_("  Version:                           %d %s\n"),
    1163                 :            :           ehdr->e_version,
    1164         [ +  - ]:        145 :           ehdr->e_version  == EV_CURRENT ? _("(current)") : "(\?\?\?)");
    1165                 :            : 
    1166                 :        145 :   printf (_("  Entry point address:               %#" PRIx64 "\n"),
    1167                 :            :           ehdr->e_entry);
    1168                 :            : 
    1169                 :        145 :   printf (_("  Start of program headers:          %" PRId64 " %s\n"),
    1170                 :            :           ehdr->e_phoff, _("(bytes into file)"));
    1171                 :            : 
    1172                 :        145 :   printf (_("  Start of section headers:          %" PRId64 " %s\n"),
    1173                 :            :           ehdr->e_shoff, _("(bytes into file)"));
    1174                 :            : 
    1175                 :        145 :   printf (_("  Flags:                             %s\n"),
    1176                 :            :           ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
    1177                 :            : 
    1178                 :        290 :   printf (_("  Size of this header:               %" PRId16 " %s\n"),
    1179                 :        145 :           ehdr->e_ehsize, _("(bytes)"));
    1180                 :            : 
    1181                 :        290 :   printf (_("  Size of program header entries:    %" PRId16 " %s\n"),
    1182                 :        145 :           ehdr->e_phentsize, _("(bytes)"));
    1183                 :            : 
    1184                 :        290 :   printf (_("  Number of program headers entries: %" PRId16),
    1185                 :        145 :           ehdr->e_phnum);
    1186         [ +  + ]:        145 :   if (ehdr->e_phnum == PN_XNUM)
    1187                 :            :     {
    1188                 :          1 :       GElf_Shdr shdr_mem;
    1189                 :          1 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1190         [ +  - ]:          1 :       if (shdr != NULL)
    1191                 :          1 :         printf (_(" (%" PRIu32 " in [0].sh_info)"),
    1192                 :          1 :                 (uint32_t) shdr->sh_info);
    1193                 :            :       else
    1194                 :          0 :         fputs_unlocked (_(" ([0] not available)"), stdout);
    1195                 :            :     }
    1196         [ -  + ]:        145 :   fputc_unlocked ('\n', stdout);
    1197                 :            : 
    1198                 :        290 :   printf (_("  Size of section header entries:    %" PRId16 " %s\n"),
    1199                 :        145 :           ehdr->e_shentsize, _("(bytes)"));
    1200                 :            : 
    1201                 :        290 :   printf (_("  Number of section headers entries: %" PRId16),
    1202                 :        145 :           ehdr->e_shnum);
    1203         [ -  + ]:        145 :   if (ehdr->e_shnum == 0)
    1204                 :            :     {
    1205                 :          0 :       GElf_Shdr shdr_mem;
    1206                 :          0 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1207         [ #  # ]:          0 :       if (shdr != NULL)
    1208                 :          0 :         printf (_(" (%" PRIu32 " in [0].sh_size)"),
    1209                 :          0 :                 (uint32_t) shdr->sh_size);
    1210                 :            :       else
    1211                 :          0 :         fputs_unlocked (_(" ([0] not available)"), stdout);
    1212                 :            :     }
    1213         [ -  + ]:        145 :   fputc_unlocked ('\n', stdout);
    1214                 :            : 
    1215         [ -  + ]:        145 :   if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
    1216                 :            :     {
    1217                 :          0 :       GElf_Shdr shdr_mem;
    1218                 :          0 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1219         [ #  # ]:          0 :       if (shdr != NULL)
    1220                 :            :         /* We managed to get the zeroth section.  */
    1221                 :          0 :         snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
    1222                 :          0 :                   (uint32_t) shdr->sh_link);
    1223                 :            :       else
    1224                 :            :         {
    1225                 :          0 :           strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
    1226                 :          0 :           buf[sizeof (buf) - 1] = '\0';
    1227                 :            :         }
    1228                 :            : 
    1229                 :          0 :       printf (_("  Section header string table index: XINDEX%s\n\n"),
    1230                 :            :               buf);
    1231                 :            :     }
    1232                 :            :   else
    1233                 :        145 :     printf (_("  Section header string table index: %" PRId16 "\n\n"),
    1234                 :            :             ehdr->e_shstrndx);
    1235                 :        145 : }
    1236                 :            : 
    1237                 :            : 
    1238                 :            : static const char *
    1239                 :      40264 : get_visibility_type (int value)
    1240                 :            : {
    1241   [ -  +  -  -  :      40264 :   switch (value)
                      + ]
    1242                 :            :     {
    1243                 :            :     case STV_DEFAULT:
    1244                 :            :       return "DEFAULT";
    1245                 :          0 :     case STV_INTERNAL:
    1246                 :          0 :       return "INTERNAL";
    1247                 :        534 :     case STV_HIDDEN:
    1248                 :        534 :       return "HIDDEN";
    1249                 :          0 :     case STV_PROTECTED:
    1250                 :          0 :       return "PROTECTED";
    1251                 :          0 :     default:
    1252                 :          0 :       return "???";
    1253                 :            :     }
    1254                 :            : }
    1255                 :            : 
    1256                 :            : static const char *
    1257                 :        192 : elf_ch_type_name (unsigned int code)
    1258                 :            : {
    1259                 :        192 :   switch (code)
    1260                 :            :     {
    1261                 :            :     case 0:
    1262                 :            :       return "NONE";
    1263                 :         12 :     case ELFCOMPRESS_ZLIB:
    1264                 :         12 :       return "ZLIB";
    1265                 :        180 :     case ELFCOMPRESS_ZSTD:
    1266                 :        180 :       return "ZSTD";
    1267                 :          0 :     default:
    1268                 :          0 :       return "UNKNOWN";
    1269                 :            :     }
    1270                 :            : }
    1271                 :            : 
    1272                 :            : /* Print the section headers.  */
    1273                 :            : static void
    1274                 :        303 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1275                 :            : {
    1276                 :        303 :   size_t cnt;
    1277                 :        303 :   size_t shstrndx;
    1278                 :            : 
    1279         [ +  + ]:        303 :   if (! print_file_header)
    1280                 :            :     {
    1281                 :        161 :       size_t sections;
    1282         [ -  + ]:        161 :       if (unlikely (elf_getshdrnum (ebl->elf, &sections) < 0))
    1283                 :          0 :         error_exit (0, _("cannot get number of sections: %s"),
    1284                 :            :                     elf_errmsg (-1));
    1285                 :            : 
    1286                 :        161 :       printf (_("\
    1287                 :            : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
    1288                 :            : \n"),
    1289                 :            :               sections, ehdr->e_shoff);
    1290                 :            :     }
    1291                 :            : 
    1292                 :            :   /* Get the section header string table index.  */
    1293         [ -  + ]:        303 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1294                 :          0 :     error_exit (0, _("cannot get section header string table index: %s"),
    1295                 :            :                 elf_errmsg (-1));
    1296                 :            : 
    1297                 :        303 :   puts (_("Section Headers:"));
    1298                 :            : 
    1299         [ +  + ]:        303 :   if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1300                 :        109 :     puts (_("[Nr] Name                 Type         Addr     Off    Size   ES Flags Lk Inf Al"));
    1301                 :            :   else
    1302                 :        194 :     puts (_("[Nr] Name                 Type         Addr             Off      Size     ES Flags Lk Inf Al"));
    1303                 :            : 
    1304         [ +  + ]:        303 :   if (print_decompress)
    1305                 :            :     {
    1306         [ +  + ]:         56 :       if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1307                 :         28 :         puts (_("     [Compression  Size   Al]"));
    1308                 :            :       else
    1309                 :         28 :         puts (_("     [Compression  Size     Al]"));
    1310                 :            :     }
    1311                 :            : 
    1312         [ +  + ]:    1580809 :   for (cnt = 0; cnt < shnum; ++cnt)
    1313                 :            :     {
    1314                 :    1580506 :       Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
    1315                 :            : 
    1316         [ -  + ]:    1580506 :       if (unlikely (scn == NULL))
    1317                 :          0 :         error_exit (0, _("cannot get section: %s"),
    1318                 :            :                     elf_errmsg (-1));
    1319                 :            : 
    1320                 :            :       /* Get the section header.  */
    1321                 :    1580506 :       GElf_Shdr shdr_mem;
    1322                 :    1580506 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1323         [ -  + ]:    1580506 :       if (unlikely (shdr == NULL))
    1324                 :          0 :         error_exit (0, _("cannot get section header: %s"),
    1325                 :            :                     elf_errmsg (-1));
    1326                 :            : 
    1327                 :    1580506 :       char flagbuf[20];
    1328                 :    1580506 :       char *cp = flagbuf;
    1329         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_WRITE)
    1330                 :       1672 :         *cp++ = 'W';
    1331         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_ALLOC)
    1332                 :       5023 :         *cp++ = 'A';
    1333         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_EXECINSTR)
    1334                 :        922 :         *cp++ = 'X';
    1335         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_MERGE)
    1336                 :        264 :         *cp++ = 'M';
    1337         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_STRINGS)
    1338                 :        246 :         *cp++ = 'S';
    1339         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_INFO_LINK)
    1340                 :        213 :         *cp++ = 'I';
    1341         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_LINK_ORDER)
    1342                 :          2 :         *cp++ = 'L';
    1343         [ -  + ]:    1580506 :       if (shdr->sh_flags & SHF_OS_NONCONFORMING)
    1344                 :          0 :         *cp++ = 'N';
    1345         [ -  + ]:    1580506 :       if (shdr->sh_flags & SHF_GROUP)
    1346                 :          0 :         *cp++ = 'G';
    1347         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_TLS)
    1348                 :         19 :         *cp++ = 'T';
    1349         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_COMPRESSED)
    1350                 :        207 :         *cp++ = 'C';
    1351         [ -  + ]:    1580506 :       if (shdr->sh_flags & SHF_ORDERED)
    1352                 :          0 :         *cp++ = 'O';
    1353         [ -  + ]:    1580506 :       if (shdr->sh_flags & SHF_EXCLUDE)
    1354                 :          0 :         *cp++ = 'E';
    1355         [ +  + ]:    1580506 :       if (shdr->sh_flags & SHF_GNU_RETAIN)
    1356                 :          1 :         *cp++ = 'R';
    1357                 :    1580506 :       *cp = '\0';
    1358                 :            : 
    1359                 :    1580506 :       const char *sname;
    1360                 :    1580506 :       char buf[128];
    1361         [ -  + ]:    1580506 :       sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
    1362         [ +  + ]:    3161012 :       printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
    1363                 :            :               " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
    1364                 :            :               " %2" PRId64 "\n",
    1365                 :            :               cnt, sname,
    1366                 :    1580506 :               ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
    1367                 :            :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
    1368                 :            :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
    1369         [ +  + ]:    1580506 :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
    1370                 :            :               shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
    1371                 :            :               shdr->sh_addralign);
    1372                 :            : 
    1373         [ +  + ]:    1580506 :       if (print_decompress)
    1374                 :            :         {
    1375         [ +  + ]:        940 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    1376                 :            :             {
    1377                 :        192 :               GElf_Chdr chdr;
    1378         [ +  - ]:        192 :               if (gelf_getchdr (scn, &chdr) != NULL)
    1379   [ +  +  -  - ]:        384 :                 printf ("     [ELF %s (%" PRId32 ") %0*" PRIx64
    1380                 :            :                         " %2" PRId64 "]\n",
    1381                 :            :                         elf_ch_type_name (chdr.ch_type),
    1382                 :            :                         chdr.ch_type,
    1383         [ +  + ]:        192 :                         ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
    1384                 :            :                         chdr.ch_size, chdr.ch_addralign);
    1385                 :            :               else
    1386                 :        192 :                 error (0, 0,
    1387                 :          0 :                        _("bad compression header for section %zd: %s"),
    1388                 :            :                        elf_ndxscn (scn), elf_errmsg (-1));
    1389                 :            :             }
    1390         [ +  + ]:        748 :           else if (startswith (sname, ".zdebug"))
    1391                 :            :             {
    1392                 :         12 :               ssize_t size;
    1393         [ +  - ]:         12 :               if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
    1394                 :    1580518 :                 printf ("     [GNU ZLIB     %0*zx   ]\n",
    1395         [ +  + ]:         12 :                         ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
    1396                 :            :               else
    1397                 :    1580506 :                 error (0, 0,
    1398                 :          0 :                        _("bad gnu compressed size for section %zd: %s"),
    1399                 :            :                        elf_ndxscn (scn), elf_errmsg (-1));
    1400                 :            :             }
    1401                 :            :         }
    1402                 :            :     }
    1403                 :            : 
    1404         [ -  + ]:        303 :   fputc_unlocked ('\n', stdout);
    1405                 :        303 : }
    1406                 :            : 
    1407                 :            : 
    1408                 :            : /* Print the program header.  */
    1409                 :            : static void
    1410                 :        144 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1411                 :            : {
    1412         [ +  + ]:        144 :   if (phnum == 0)
    1413                 :            :     /* No program header, this is OK in relocatable objects.  */
    1414                 :         36 :     return;
    1415                 :            : 
    1416                 :        108 :   puts (_("Program Headers:"));
    1417         [ +  + ]:        108 :   if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1418                 :         16 :     puts (_("\
    1419                 :            :   Type           Offset   VirtAddr   PhysAddr   FileSiz  MemSiz   Flg Align"));
    1420                 :            :   else
    1421                 :         92 :     puts (_("\
    1422                 :            :   Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align"));
    1423                 :            : 
    1424                 :            :   /* Process all program headers.  */
    1425                 :            :   bool has_relro = false;
    1426                 :            :   GElf_Addr relro_from = 0;
    1427                 :            :   GElf_Addr relro_to = 0;
    1428         [ +  + ]:       1189 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
    1429                 :            :     {
    1430                 :       1081 :       char buf[128];
    1431                 :       1081 :       GElf_Phdr mem;
    1432                 :       1081 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
    1433                 :            : 
    1434                 :            :       /* If for some reason the header cannot be returned show this.  */
    1435         [ -  + ]:       1081 :       if (unlikely (phdr == NULL))
    1436                 :            :         {
    1437                 :          0 :           puts ("  ???");
    1438                 :          0 :           continue;
    1439                 :            :         }
    1440                 :            : 
    1441                 :       1081 :       printf ("  %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
    1442                 :            :               " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
    1443                 :       1081 :               ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
    1444                 :            :               phdr->p_offset,
    1445                 :            :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
    1446         [ +  + ]:       1081 :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
    1447                 :            :               phdr->p_filesz,
    1448                 :            :               phdr->p_memsz,
    1449         [ -  + ]:       1081 :               phdr->p_flags & PF_R ? 'R' : ' ',
    1450         [ +  + ]:       1081 :               phdr->p_flags & PF_W ? 'W' : ' ',
    1451         [ +  + ]:       1081 :               phdr->p_flags & PF_X ? 'E' : ' ',
    1452                 :            :               phdr->p_align);
    1453                 :            : 
    1454         [ +  + ]:       1081 :       if (phdr->p_type == PT_INTERP)
    1455                 :            :         {
    1456                 :            :           /* If we are sure the file offset is valid then we can show
    1457                 :            :              the user the name of the interpreter.  We check whether
    1458                 :            :              there is a section at the file offset.  Normally there
    1459                 :            :              would be a section called ".interp".  But in separate
    1460                 :            :              .debug files it is a NOBITS section (and so doesn't match
    1461                 :            :              with gelf_offscn).  Which probably means the offset is
    1462                 :            :              not valid another reason could be because the ELF file
    1463                 :            :              just doesn't contain any section headers, in that case
    1464                 :            :              just play it safe and don't display anything.  */
    1465                 :            : 
    1466                 :        106 :           Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
    1467                 :        106 :           GElf_Shdr shdr_mem;
    1468                 :        106 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1469                 :            : 
    1470                 :        106 :           size_t maxsize;
    1471                 :        106 :           char *filedata = elf_rawfile (ebl->elf, &maxsize);
    1472                 :            : 
    1473   [ +  -  +  + ]:        106 :           if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
    1474   [ +  -  +  - ]:         79 :               && filedata != NULL && phdr->p_offset < maxsize
    1475         [ +  - ]:         79 :               && phdr->p_filesz <= maxsize - phdr->p_offset
    1476         [ +  - ]:         79 :               && memchr (filedata + phdr->p_offset, '\0',
    1477                 :            :                          phdr->p_filesz) != NULL)
    1478                 :        106 :             printf (_("\t[Requesting program interpreter: %s]\n"),
    1479                 :            :                     filedata + phdr->p_offset);
    1480                 :            :         }
    1481         [ +  + ]:        975 :       else if (phdr->p_type == PT_GNU_RELRO)
    1482                 :            :         {
    1483                 :         93 :           has_relro = true;
    1484                 :         93 :           relro_from = phdr->p_vaddr;
    1485                 :         93 :           relro_to = relro_from + phdr->p_memsz;
    1486                 :            :         }
    1487                 :            :     }
    1488                 :            : 
    1489                 :        108 :   size_t sections;
    1490         [ -  + ]:        108 :   if (unlikely (elf_getshdrnum (ebl->elf, &sections) < 0))
    1491                 :          0 :     error_exit (0, _("cannot get number of sections: %s"),
    1492                 :            :                 elf_errmsg (-1));
    1493                 :            : 
    1494         [ +  - ]:        108 :   if (sections == 0)
    1495                 :            :     /* No sections in the file.  Punt.  */
    1496                 :            :     return;
    1497                 :            : 
    1498                 :            :   /* Get the section header string table index.  */
    1499                 :        108 :   size_t shstrndx;
    1500         [ -  + ]:        108 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1501                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    1502                 :            : 
    1503                 :        108 :   puts (_("\n Section to Segment mapping:\n  Segment Sections..."));
    1504                 :            : 
    1505         [ +  + ]:       1189 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
    1506                 :            :     {
    1507                 :            :       /* Print the segment number.  */
    1508                 :       1081 :       printf ("   %2.2zu     ", cnt);
    1509                 :            : 
    1510                 :       1081 :       GElf_Phdr phdr_mem;
    1511                 :       1081 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
    1512                 :            :       /* This must not happen.  */
    1513         [ -  + ]:       1081 :       if (unlikely (phdr == NULL))
    1514                 :          0 :         error_exit (0, _("cannot get program header: %s"),
    1515                 :            :                     elf_errmsg (-1));
    1516                 :            : 
    1517                 :            :       /* Iterate over the sections.  */
    1518                 :            :       bool in_relro = false;
    1519                 :            :       bool in_ro = false;
    1520         [ +  + ]:      34658 :       for (size_t inner = 1; inner < shnum; ++inner)
    1521                 :            :         {
    1522                 :      33577 :           Elf_Scn *scn = elf_getscn (ebl->elf, inner);
    1523                 :            :           /* This should not happen.  */
    1524         [ -  + ]:      33577 :           if (unlikely (scn == NULL))
    1525                 :          0 :             error_exit (0, _("cannot get section: %s"),
    1526                 :            :                         elf_errmsg (-1));
    1527                 :            : 
    1528                 :            :           /* Get the section header.  */
    1529                 :      33577 :           GElf_Shdr shdr_mem;
    1530                 :      33577 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1531         [ -  + ]:      33577 :           if (unlikely (shdr == NULL))
    1532                 :          0 :             error_exit (0, _("cannot get section header: %s"),
    1533                 :            :                         elf_errmsg (-1));
    1534                 :            : 
    1535         [ +  - ]:      33577 :           if (shdr->sh_size > 0
    1536                 :            :               /* Compare allocated sections by VMA, unallocated
    1537                 :            :                  sections by file offset.  */
    1538   [ +  +  +  + ]:      67154 :               && (shdr->sh_flags & SHF_ALLOC
    1539                 :      26778 :                   ? (shdr->sh_addr >= phdr->p_vaddr
    1540         [ +  + ]:      26778 :                      && (shdr->sh_addr + shdr->sh_size
    1541         [ +  + ]:      17871 :                          <= phdr->p_vaddr + phdr->p_memsz))
    1542                 :       6799 :                   : (shdr->sh_offset >= phdr->p_offset
    1543         [ +  + ]:       6799 :                      && (shdr->sh_offset + shdr->sh_size
    1544         [ +  + ]:       5828 :                          <= phdr->p_offset + phdr->p_filesz))))
    1545                 :            :             {
    1546         [ +  + ]:       3771 :               if (has_relro && !in_relro
    1547         [ +  + ]:       2508 :                   && shdr->sh_addr >= relro_from
    1548         [ +  + ]:        467 :                   && shdr->sh_addr + shdr->sh_size <= relro_to)
    1549                 :            :                 {
    1550                 :        290 :                   fputs_unlocked (" [RELRO:", stdout);
    1551                 :        290 :                   in_relro = true;
    1552                 :            :                 }
    1553   [ +  +  +  + ]:       3481 :               else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
    1554                 :            :                 {
    1555                 :         93 :                   fputs_unlocked ("]", stdout);
    1556                 :         93 :                   in_relro =  false;
    1557                 :            :                 }
    1558         [ +  + ]:       3388 :               else if (has_relro && in_relro
    1559         [ -  + ]:        680 :                        && shdr->sh_addr + shdr->sh_size > relro_to)
    1560                 :          0 :                 fputs_unlocked ("] <RELRO:", stdout);
    1561         [ +  + ]:       3388 :               else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
    1562                 :            :                 {
    1563         [ +  + ]:       1964 :                   if (!in_ro)
    1564                 :            :                     {
    1565                 :        222 :                       fputs_unlocked (" [RO:", stdout);
    1566                 :        222 :                       in_ro = true;
    1567                 :            :                     }
    1568                 :            :                 }
    1569                 :            :               else
    1570                 :            :                 {
    1571                 :            :                   /* Determine the segment this section is part of.  */
    1572                 :            :                   size_t cnt2;
    1573                 :            :                   GElf_Phdr phdr2_mem;
    1574                 :            :                   GElf_Phdr *phdr2 = NULL;
    1575         [ +  - ]:       6370 :                   for (cnt2 = 0; cnt2 < phnum; ++cnt2)
    1576                 :            :                     {
    1577                 :       6370 :                       phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
    1578                 :            : 
    1579   [ +  -  +  + ]:       6370 :                       if (phdr2 != NULL && phdr2->p_type == PT_LOAD
    1580         [ +  - ]:       3578 :                           && shdr->sh_addr >= phdr2->p_vaddr
    1581                 :       3578 :                           && (shdr->sh_addr + shdr->sh_size
    1582         [ +  + ]:       3578 :                               <= phdr2->p_vaddr + phdr2->p_memsz))
    1583                 :            :                         break;
    1584                 :            :                     }
    1585                 :            : 
    1586         [ +  - ]:       1424 :                   if (cnt2 < phnum)
    1587                 :            :                     {
    1588   [ +  +  +  + ]:       1424 :                       if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
    1589                 :            :                         {
    1590                 :        325 :                           fputs_unlocked (" [RO:", stdout);
    1591                 :        325 :                           in_ro = true;
    1592                 :            :                         }
    1593   [ +  +  -  + ]:       1099 :                       else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
    1594                 :            :                         {
    1595                 :          0 :                           fputs_unlocked ("]", stdout);
    1596                 :          0 :                           in_ro = false;
    1597                 :            :                         }
    1598                 :            :                     }
    1599                 :            :                 }
    1600                 :            : 
    1601                 :       3771 :               printf (" %s",
    1602                 :       3771 :                       elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
    1603                 :            : 
    1604                 :            :               /* Signal that this section is only partially covered.  */
    1605         [ +  + ]:       3771 :               if (has_relro && in_relro
    1606         [ -  + ]:        970 :                        && shdr->sh_addr + shdr->sh_size > relro_to)
    1607                 :            :                 {
    1608                 :          0 :                   fputs_unlocked (">", stdout);
    1609                 :          0 :                   in_relro =  false;
    1610                 :            :                 }
    1611                 :            :             }
    1612                 :            :         }
    1613         [ +  + ]:       1081 :       if (in_relro || in_ro)
    1614                 :        744 :         fputs_unlocked ("]", stdout);
    1615                 :            : 
    1616                 :            :       /* Finish the line.  */
    1617         [ -  + ]:       2162 :       fputc_unlocked ('\n', stdout);
    1618                 :            :     }
    1619                 :            : }
    1620                 :            : 
    1621                 :            : 
    1622                 :            : static const char *
    1623                 :        472 : section_name (Ebl *ebl, GElf_Shdr *shdr)
    1624                 :            : {
    1625                 :        472 :   size_t shstrndx;
    1626   [ +  -  -  + ]:        472 :   if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
    1627                 :          0 :     return "???";
    1628         [ -  + ]:        472 :   return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
    1629                 :            : }
    1630                 :            : 
    1631                 :            : 
    1632                 :            : static void
    1633                 :         16 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    1634                 :            : {
    1635                 :            :   /* Get the data of the section.  */
    1636                 :         16 :   Elf_Data *data = elf_getdata (scn, NULL);
    1637                 :            : 
    1638                 :         16 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    1639                 :         16 :   GElf_Shdr symshdr_mem;
    1640                 :         16 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    1641                 :         16 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    1642                 :            : 
    1643   [ +  -  +  - ]:         16 :   if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
    1644         [ -  + ]:         16 :       || symdata == NULL)
    1645                 :          0 :     return;
    1646                 :            : 
    1647                 :            :   /* Get the section header string table index.  */
    1648                 :         16 :   size_t shstrndx;
    1649         [ -  + ]:         16 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1650                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    1651                 :            : 
    1652                 :         16 :   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
    1653                 :            : 
    1654                 :         16 :   GElf_Sym sym_mem;
    1655                 :         16 :   GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
    1656                 :            : 
    1657   [ -  +  +  + ]:         32 :   printf ((grpref[0] & GRP_COMDAT)
    1658                 :          4 :           ? ngettext ("\
    1659                 :            : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
    1660                 :            :                       "\
    1661                 :            : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
    1662                 :            :                       data->d_size / sizeof (Elf32_Word) - 1)
    1663                 :         12 :           : ngettext ("\
    1664                 :            : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
    1665                 :            : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
    1666                 :            :                       data->d_size / sizeof (Elf32_Word) - 1),
    1667                 :            :           elf_ndxscn (scn),
    1668                 :         16 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    1669                 :            :           (sym == NULL ? NULL
    1670                 :         16 :            : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
    1671                 :          0 :           ?: _("<INVALID SYMBOL>"),
    1672         [ +  - ]:         16 :           data->d_size / sizeof (Elf32_Word) - 1);
    1673                 :            : 
    1674         [ +  + ]:         52 :   for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
    1675                 :            :     {
    1676                 :         36 :       GElf_Shdr grpshdr_mem;
    1677                 :         36 :       GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
    1678                 :            :                                          &grpshdr_mem);
    1679                 :            : 
    1680                 :         36 :       const char *str;
    1681         [ +  - ]:         72 :       printf ("  [%2u] %s\n",
    1682                 :            :               grpref[cnt],
    1683                 :            :               grpshdr != NULL
    1684         [ -  + ]:         36 :               && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
    1685                 :          0 :               ? str : _("<INVALID SECTION>"));
    1686                 :            :     }
    1687                 :            : }
    1688                 :            : 
    1689                 :            : 
    1690                 :            : static void
    1691                 :        151 : print_scngrp (Ebl *ebl)
    1692                 :            : {
    1693                 :            :   /* Find all relocation sections and handle them.  */
    1694                 :        151 :   Elf_Scn *scn = NULL;
    1695                 :            : 
    1696         [ +  + ]:       4457 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    1697                 :            :     {
    1698                 :            :        /* Handle the section if it is a symbol table.  */
    1699                 :       4306 :       GElf_Shdr shdr_mem;
    1700                 :       4306 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1701                 :            : 
    1702   [ +  -  +  + ]:       4306 :       if (shdr != NULL && shdr->sh_type == SHT_GROUP)
    1703                 :            :         {
    1704         [ -  + ]:         16 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    1705                 :            :             {
    1706         [ #  # ]:          0 :               if (elf_compress (scn, 0, 0) < 0)
    1707                 :          0 :                 printf ("WARNING: %s [%zd]\n",
    1708                 :            :                         _("Couldn't uncompress section"),
    1709                 :            :                         elf_ndxscn (scn));
    1710                 :          0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    1711         [ #  # ]:          0 :               if (unlikely (shdr == NULL))
    1712                 :          0 :                 error_exit (0, _("cannot get section [%zd] header: %s"),
    1713                 :            :                             elf_ndxscn (scn),
    1714                 :            :                             elf_errmsg (-1));
    1715                 :            :             }
    1716                 :         16 :           handle_scngrp (ebl, scn, shdr);
    1717                 :            :         }
    1718                 :            :     }
    1719                 :        151 : }
    1720                 :            : 
    1721                 :            : 
    1722                 :            : static const struct flags
    1723                 :            : {
    1724                 :            :   int mask;
    1725                 :            :   const char *str;
    1726                 :            : } dt_flags[] =
    1727                 :            :   {
    1728                 :            :     { DF_ORIGIN, "ORIGIN" },
    1729                 :            :     { DF_SYMBOLIC, "SYMBOLIC" },
    1730                 :            :     { DF_TEXTREL, "TEXTREL" },
    1731                 :            :     { DF_BIND_NOW, "BIND_NOW" },
    1732                 :            :     { DF_STATIC_TLS, "STATIC_TLS" }
    1733                 :            :   };
    1734                 :            : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
    1735                 :            : 
    1736                 :            : static const struct flags dt_flags_1[] =
    1737                 :            :   {
    1738                 :            :     { DF_1_NOW, "NOW" },
    1739                 :            :     { DF_1_GLOBAL, "GLOBAL" },
    1740                 :            :     { DF_1_GROUP, "GROUP" },
    1741                 :            :     { DF_1_NODELETE, "NODELETE" },
    1742                 :            :     { DF_1_LOADFLTR, "LOADFLTR" },
    1743                 :            :     { DF_1_INITFIRST, "INITFIRST" },
    1744                 :            :     { DF_1_NOOPEN, "NOOPEN" },
    1745                 :            :     { DF_1_ORIGIN, "ORIGIN" },
    1746                 :            :     { DF_1_DIRECT, "DIRECT" },
    1747                 :            :     { DF_1_TRANS, "TRANS" },
    1748                 :            :     { DF_1_INTERPOSE, "INTERPOSE" },
    1749                 :            :     { DF_1_NODEFLIB, "NODEFLIB" },
    1750                 :            :     { DF_1_NODUMP, "NODUMP" },
    1751                 :            :     { DF_1_CONFALT, "CONFALT" },
    1752                 :            :     { DF_1_ENDFILTEE, "ENDFILTEE" },
    1753                 :            :     { DF_1_DISPRELDNE, "DISPRELDNE" },
    1754                 :            :     { DF_1_DISPRELPND, "DISPRELPND" },
    1755                 :            :   };
    1756                 :            : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
    1757                 :            : 
    1758                 :            : static const struct flags dt_feature_1[] =
    1759                 :            :   {
    1760                 :            :     { DTF_1_PARINIT, "PARINIT" },
    1761                 :            :     { DTF_1_CONFEXP, "CONFEXP" }
    1762                 :            :   };
    1763                 :            : static const int ndt_feature_1 = (sizeof (dt_feature_1)
    1764                 :            :                                   / sizeof (dt_feature_1[0]));
    1765                 :            : 
    1766                 :            : static const struct flags dt_posflag_1[] =
    1767                 :            :   {
    1768                 :            :     { DF_P1_LAZYLOAD, "LAZYLOAD" },
    1769                 :            :     { DF_P1_GROUPPERM, "GROUPPERM" }
    1770                 :            :   };
    1771                 :            : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
    1772                 :            :                                   / sizeof (dt_posflag_1[0]));
    1773                 :            : 
    1774                 :            : 
    1775                 :            : static void
    1776                 :         39 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
    1777                 :            :                 int nflags)
    1778                 :            : {
    1779                 :         39 :   bool first = true;
    1780                 :         39 :   int cnt;
    1781                 :            : 
    1782         [ +  + ]:        588 :   for (cnt = 0; cnt < nflags; ++cnt)
    1783         [ +  + ]:        549 :     if (d_val & flags[cnt].mask)
    1784                 :            :       {
    1785         [ +  + ]:         34 :         if (!first)
    1786         [ -  + ]:         18 :           putchar_unlocked (' ');
    1787                 :         34 :         fputs_unlocked (flags[cnt].str, stdout);
    1788                 :         34 :         d_val &= ~flags[cnt].mask;
    1789                 :         34 :         first = false;
    1790                 :            :       }
    1791                 :            : 
    1792         [ +  + ]:         39 :   if (d_val != 0)
    1793                 :            :     {
    1794         [ +  + ]:         33 :       if (!first)
    1795         [ -  + ]:         10 :         putchar_unlocked (' ');
    1796         [ +  + ]:         33 :       printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
    1797                 :            :     }
    1798                 :            : 
    1799         [ -  + ]:         39 :   putchar_unlocked ('\n');
    1800                 :         39 : }
    1801                 :            : 
    1802                 :            : 
    1803                 :            : static void
    1804                 :          7 : print_dt_flags (int class, GElf_Xword d_val)
    1805                 :            : {
    1806                 :          7 :   print_flags (class, d_val, dt_flags, ndt_flags);
    1807                 :          7 : }
    1808                 :            : 
    1809                 :            : 
    1810                 :            : static void
    1811                 :         30 : print_dt_flags_1 (int class, GElf_Xword d_val)
    1812                 :            : {
    1813                 :         30 :   print_flags (class, d_val, dt_flags_1, ndt_flags_1);
    1814                 :         30 : }
    1815                 :            : 
    1816                 :            : 
    1817                 :            : static void
    1818                 :          1 : print_dt_feature_1 (int class, GElf_Xword d_val)
    1819                 :            : {
    1820                 :          1 :   print_flags (class, d_val, dt_feature_1, ndt_feature_1);
    1821                 :          1 : }
    1822                 :            : 
    1823                 :            : 
    1824                 :            : static void
    1825                 :          1 : print_dt_posflag_1 (int class, GElf_Xword d_val)
    1826                 :            : {
    1827                 :          1 :   print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
    1828                 :          1 : }
    1829                 :            : 
    1830                 :            : 
    1831                 :            : static size_t
    1832                 :         82 : get_dyn_ents (Elf_Data * dyn_data)
    1833                 :            : {
    1834                 :         82 :   GElf_Dyn *dyn;
    1835                 :         82 :   GElf_Dyn dyn_mem;
    1836                 :         82 :   size_t dyn_idx = 0;
    1837                 :       2060 :   do
    1838                 :            :     {
    1839                 :       2060 :       dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
    1840         [ +  - ]:       2060 :       if (dyn != NULL)
    1841                 :       2060 :         ++dyn_idx;
    1842                 :            :     }
    1843   [ +  -  +  + ]:       2060 :   while (dyn != NULL && dyn->d_tag != DT_NULL);
    1844                 :            : 
    1845                 :         82 :   return dyn_idx;
    1846                 :            : }
    1847                 :            : 
    1848                 :            : 
    1849                 :            : static void
    1850                 :         82 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
    1851                 :            : {
    1852                 :         82 :   int class = gelf_getclass (ebl->elf);
    1853                 :         82 :   GElf_Shdr glink_mem;
    1854                 :         82 :   GElf_Shdr *glink;
    1855                 :         82 :   Elf_Data *data;
    1856                 :         82 :   size_t cnt;
    1857                 :         82 :   size_t shstrndx;
    1858                 :         82 :   size_t dyn_ents;
    1859                 :            : 
    1860                 :            :   /* Get the data of the section.  */
    1861   [ +  +  +  - ]:         82 :   if (use_dynamic_segment && phdr != NULL)
    1862                 :          1 :     data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
    1863                 :            :                                 phdr->p_filesz, ELF_T_DYN);
    1864                 :            :   else
    1865                 :         81 :     data = elf_getdata (scn, NULL);
    1866                 :            : 
    1867         [ -  + ]:         82 :   if (data == NULL)
    1868                 :          0 :     return;
    1869                 :            : 
    1870                 :            :   /* Get the dynamic section entry number */
    1871                 :         82 :   dyn_ents = get_dyn_ents (data);
    1872                 :            : 
    1873   [ +  +  +  - ]:         82 :   if (!use_dynamic_segment && shdr != NULL)
    1874                 :            :     {
    1875                 :            :       /* Get the section header string table index.  */
    1876         [ -  + ]:         81 :       if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1877                 :          0 :         error_exit (0, _("cannot get section header string table index"));
    1878                 :            : 
    1879                 :         81 :       glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
    1880         [ -  + ]:         81 :       if (glink == NULL)
    1881                 :          0 :         error_exit (0, _("invalid sh_link value in section %zu"),
    1882                 :            :                     elf_ndxscn (scn));
    1883                 :            : 
    1884                 :         81 :       printf (ngettext ("\
    1885                 :            : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    1886                 :            :                     "\
    1887                 :            : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    1888                 :            :                         dyn_ents),
    1889                 :            :               (unsigned long int) dyn_ents,
    1890                 :            :               class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    1891                 :            :               shdr->sh_offset,
    1892         [ +  + ]:         81 :               (int) shdr->sh_link,
    1893                 :         81 :               elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    1894                 :            :     }
    1895         [ +  - ]:          1 :   else if (phdr != NULL)
    1896                 :            :     {
    1897         [ +  - ]:          2 :       printf (ngettext ("\
    1898                 :            : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "\n",
    1899                 :            :                     "\
    1900                 :            : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "\n",
    1901                 :            :                         dyn_ents),
    1902                 :            :               (unsigned long int) dyn_ents,
    1903                 :            :               class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
    1904                 :            :               phdr->p_offset);
    1905                 :            :     }
    1906                 :            : 
    1907                 :         82 :   fputs_unlocked (_("  Type              Value\n"), stdout);
    1908                 :            : 
    1909                 :            :   /* if --use-dynamic option is enabled,
    1910                 :            :      use the string table to get the related library info.  */
    1911                 :         82 :   Elf_Data *strtab_data = NULL;
    1912   [ +  +  +  - ]:         82 :   if (use_dynamic_segment && phdr != NULL)
    1913                 :            :     {
    1914                 :          1 :       strtab_data = get_dynscn_strtab(ebl->elf, phdr);
    1915         [ -  + ]:          1 :       if (strtab_data == NULL)
    1916                 :          0 :         error_exit (0, _("cannot get string table by using dynamic segment"));
    1917                 :            :     }
    1918                 :            : 
    1919         [ +  + ]:       2142 :   for (cnt = 0; cnt < dyn_ents; ++cnt)
    1920                 :            :     {
    1921                 :       2060 :       GElf_Dyn dynmem;
    1922                 :       2060 :       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
    1923         [ +  - ]:       2060 :       if (dyn == NULL)
    1924                 :            :         break;
    1925                 :            : 
    1926                 :       2060 :       char buf[64];
    1927                 :       2060 :       printf ("  %-17s ",
    1928                 :            :               ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
    1929                 :            : 
    1930                 :       2060 :       char *name = NULL;
    1931                 :       2060 :       if (dyn->d_tag == DT_NEEDED
    1932         [ +  + ]:       2060 :           || dyn->d_tag == DT_SONAME
    1933         [ +  + ]:       1909 :           || dyn->d_tag == DT_RPATH
    1934         [ +  + ]:       1908 :           || dyn->d_tag == DT_RUNPATH)
    1935                 :            :         {
    1936   [ +  +  +  - ]:        153 :           if (! use_dynamic_segment && shdr != NULL)
    1937                 :        151 :             name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
    1938         [ +  - ]:          2 :           else if (dyn->d_un.d_val < strtab_data->d_size
    1939                 :          2 :                    && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
    1940         [ +  - ]:          2 :                                strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
    1941                 :          2 :             name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
    1942                 :            :         }
    1943                 :            : 
    1944   [ +  +  +  +  :       2060 :       switch (dyn->d_tag)
          +  +  +  +  +  
             +  +  +  + ]
    1945                 :            :         {
    1946                 :        162 :         case DT_NULL:
    1947                 :            :         case DT_DEBUG:
    1948                 :            :         case DT_BIND_NOW:
    1949                 :            :         case DT_TEXTREL:
    1950                 :            :           /* No further output.  */
    1951         [ -  + ]:        162 :           fputc_unlocked ('\n', stdout);
    1952                 :            :           break;
    1953                 :            : 
    1954                 :        148 :         case DT_NEEDED:
    1955                 :        148 :           printf (_("Shared library: [%s]\n"), name);
    1956                 :            :           break;
    1957                 :            : 
    1958                 :          3 :         case DT_SONAME:
    1959                 :          3 :           printf (_("Library soname: [%s]\n"), name);
    1960                 :            :           break;
    1961                 :            : 
    1962                 :          1 :         case DT_RPATH:
    1963                 :          1 :           printf (_("Library rpath: [%s]\n"), name);
    1964                 :            :           break;
    1965                 :            : 
    1966                 :          1 :         case DT_RUNPATH:
    1967                 :          1 :           printf (_("Library runpath: [%s]\n"), name);
    1968                 :            :           break;
    1969                 :            : 
    1970                 :        545 :         case DT_PLTRELSZ:
    1971                 :            :         case DT_RELASZ:
    1972                 :            :         case DT_STRSZ:
    1973                 :            :         case DT_RELSZ:
    1974                 :            :         case DT_RELAENT:
    1975                 :            :         case DT_SYMENT:
    1976                 :            :         case DT_RELENT:
    1977                 :            :         case DT_PLTPADSZ:
    1978                 :            :         case DT_MOVEENT:
    1979                 :            :         case DT_MOVESZ:
    1980                 :            :         case DT_INIT_ARRAYSZ:
    1981                 :            :         case DT_FINI_ARRAYSZ:
    1982                 :            :         case DT_SYMINSZ:
    1983                 :            :         case DT_SYMINENT:
    1984                 :            :         case DT_GNU_CONFLICTSZ:
    1985                 :            :         case DT_GNU_LIBLISTSZ:
    1986                 :        545 :           printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
    1987                 :            :           break;
    1988                 :            : 
    1989                 :        122 :         case DT_VERDEFNUM:
    1990                 :            :         case DT_VERNEEDNUM:
    1991                 :            :         case DT_RELACOUNT:
    1992                 :            :         case DT_RELCOUNT:
    1993                 :        122 :           printf ("%" PRId64 "\n", dyn->d_un.d_val);
    1994                 :            :           break;
    1995                 :            : 
    1996                 :         68 :         case DT_PLTREL:;
    1997                 :         68 :           const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
    1998                 :            :                                                       NULL, 0);
    1999         [ +  + ]:         68 :           puts (tagname ?: "???");
    2000                 :         68 :           break;
    2001                 :            : 
    2002                 :          7 :         case DT_FLAGS:
    2003                 :          7 :           print_dt_flags (class, dyn->d_un.d_val);
    2004                 :            :           break;
    2005                 :            : 
    2006                 :         30 :         case DT_FLAGS_1:
    2007                 :         30 :           print_dt_flags_1 (class, dyn->d_un.d_val);
    2008                 :            :           break;
    2009                 :            : 
    2010                 :          1 :         case DT_FEATURE_1:
    2011                 :          1 :           print_dt_feature_1 (class, dyn->d_un.d_val);
    2012                 :            :           break;
    2013                 :            : 
    2014                 :          1 :         case DT_POSFLAG_1:
    2015                 :          1 :           print_dt_posflag_1 (class, dyn->d_un.d_val);
    2016                 :            :           break;
    2017                 :            : 
    2018                 :        971 :         default:
    2019         [ +  + ]:       3031 :           printf ("%#0*" PRIx64 "\n",
    2020                 :            :                   class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
    2021                 :            :           break;
    2022                 :            :         }
    2023                 :            :     }
    2024                 :            : }
    2025                 :            : 
    2026                 :            : 
    2027                 :            : /* Print the dynamic segment.  */
    2028                 :            : static void
    2029                 :        145 : print_dynamic (Ebl *ebl)
    2030                 :            : {
    2031         [ +  + ]:        682 :   for (size_t i = 0; i < phnum; ++i)
    2032                 :            :     {
    2033                 :        646 :       GElf_Phdr phdr_mem;
    2034                 :        646 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
    2035                 :            : 
    2036   [ +  -  +  + ]:        646 :       if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
    2037                 :            :         {
    2038                 :        109 :           Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
    2039                 :        109 :           GElf_Shdr shdr_mem;
    2040                 :        109 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2041         [ +  + ]:        109 :           if ((use_dynamic_segment && phdr != NULL)
    2042   [ +  +  +  + ]:        108 :               || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
    2043                 :         82 :             handle_dynamic (ebl, scn, shdr, phdr);
    2044                 :        109 :           break;
    2045                 :            :         }
    2046                 :            :     }
    2047                 :        145 : }
    2048                 :            : 
    2049                 :            : 
    2050                 :            : /* Print relocations.  */
    2051                 :            : static void
    2052                 :        143 : print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
    2053                 :            : {
    2054                 :            :   /* Find all relocation sections and handle them.  */
    2055                 :        143 :   Elf_Scn *scn = NULL;
    2056                 :            : 
    2057         [ +  + ]:       4233 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    2058                 :            :     {
    2059                 :            :        /* Handle the section if it is a symbol table.  */
    2060                 :       4090 :       GElf_Shdr shdr_mem;
    2061                 :       4090 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2062                 :            : 
    2063         [ +  - ]:       4090 :       if (likely (shdr != NULL))
    2064                 :            :         {
    2065         [ +  + ]:       4090 :           if (shdr->sh_type == SHT_REL)
    2066                 :         56 :             handle_relocs_rel (ebl, ehdr, scn, shdr);
    2067         [ +  + ]:       4034 :           else if (shdr->sh_type == SHT_RELA)
    2068                 :        283 :             handle_relocs_rela (ebl, ehdr, scn, shdr);
    2069                 :            :         }
    2070                 :            :     }
    2071                 :        143 : }
    2072                 :            : 
    2073                 :            : 
    2074                 :            : /* Handle a relocation section.  */
    2075                 :            : static void
    2076                 :         56 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
    2077                 :            : {
    2078                 :         56 :   int class = gelf_getclass (ebl->elf);
    2079                 :         56 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
    2080                 :         56 :   int nentries = shdr->sh_size / sh_entsize;
    2081                 :            : 
    2082                 :            :   /* Get the data of the section.  */
    2083                 :         56 :   Elf_Data *data = elf_getdata (scn, NULL);
    2084         [ +  - ]:         56 :   if (data == NULL)
    2085                 :          0 :     return;
    2086                 :            : 
    2087                 :            :   /* Get the symbol table information.  */
    2088                 :         56 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    2089                 :         56 :   GElf_Shdr symshdr_mem;
    2090                 :         56 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    2091                 :         56 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    2092                 :            : 
    2093                 :            :   /* Get the section header of the section the relocations are for.  */
    2094                 :         56 :   GElf_Shdr destshdr_mem;
    2095                 :         56 :   GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
    2096                 :            :                                       &destshdr_mem);
    2097                 :            : 
    2098   [ +  -  -  + ]:         56 :   if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
    2099                 :            :     {
    2100                 :          0 :       printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
    2101                 :            :               shdr->sh_offset);
    2102                 :          0 :       return;
    2103                 :            :     }
    2104                 :            : 
    2105                 :            :   /* Search for the optional extended section index table.  */
    2106                 :         56 :   Elf_Data *xndxdata = NULL;
    2107                 :         56 :   int xndxscnidx = elf_scnshndx (scn);
    2108         [ -  + ]:         56 :   if (unlikely (xndxscnidx > 0))
    2109                 :          0 :     xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
    2110                 :            : 
    2111                 :            :   /* Get the section header string table index.  */
    2112                 :         56 :   size_t shstrndx;
    2113         [ -  + ]:         56 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2114                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    2115                 :            : 
    2116         [ +  + ]:         56 :   if (shdr->sh_info != 0)
    2117                 :         46 :     printf (ngettext ("\
    2118                 :            : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2119                 :            :                     "\
    2120                 :            : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2121                 :            :                       nentries),
    2122                 :            :             elf_ndxscn (scn),
    2123                 :         46 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2124                 :         46 :             (unsigned int) shdr->sh_info,
    2125                 :         46 :             elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
    2126                 :            :             shdr->sh_offset,
    2127                 :            :             nentries);
    2128                 :            :   else
    2129                 :            :     /* The .rel.dyn section does not refer to a specific section but
    2130                 :            :        instead of section index zero.  Do not try to print a section
    2131                 :            :        name.  */
    2132                 :         10 :     printf (ngettext ("\
    2133                 :            : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2134                 :            :                     "\
    2135                 :            : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2136                 :            :                       nentries),
    2137                 :         10 :             (unsigned int) elf_ndxscn (scn),
    2138                 :         10 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2139                 :            :             shdr->sh_offset,
    2140                 :            :             nentries);
    2141         [ +  - ]:         56 :   fputs_unlocked (class == ELFCLASS32
    2142                 :         56 :                   ? _("\
    2143                 :            :   Offset      Type                 Value       Name\n")
    2144                 :          0 :                   : _("\
    2145                 :            :   Offset              Type                 Value               Name\n"),
    2146                 :            :          stdout);
    2147                 :            : 
    2148                 :         56 :   int is_statically_linked = 0;
    2149         [ +  + ]:      13009 :   for (int cnt = 0; cnt < nentries; ++cnt)
    2150                 :            :     {
    2151                 :      12953 :       GElf_Rel relmem;
    2152                 :      12953 :       GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
    2153         [ +  - ]:      12953 :       if (likely (rel != NULL))
    2154                 :            :         {
    2155                 :      12953 :           char buf[128];
    2156                 :      12953 :           GElf_Sym symmem;
    2157                 :      12953 :           Elf32_Word xndx;
    2158                 :      25906 :           GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
    2159                 :      12953 :                                             GELF_R_SYM (rel->r_info),
    2160                 :            :                                             &symmem, &xndx);
    2161         [ -  + ]:      12953 :           if (unlikely (sym == NULL))
    2162                 :            :             {
    2163                 :            :               /* As a special case we have to handle relocations in static
    2164                 :            :                  executables.  This only happens for IRELATIVE relocations
    2165                 :            :                  (so far).  There is no symbol table.  */
    2166         [ #  # ]:          0 :               if (is_statically_linked == 0)
    2167                 :            :                 {
    2168                 :            :                   /* Find the program header and look for a PT_INTERP entry. */
    2169                 :          0 :                   is_statically_linked = -1;
    2170         [ #  # ]:          0 :                   if (ehdr->e_type == ET_EXEC)
    2171                 :            :                     {
    2172                 :          0 :                       is_statically_linked = 1;
    2173                 :            : 
    2174         [ #  # ]:          0 :                       for (size_t inner = 0; inner < phnum; ++inner)
    2175                 :            :                         {
    2176                 :          0 :                           GElf_Phdr phdr_mem;
    2177                 :          0 :                           GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
    2178                 :            :                                                           &phdr_mem);
    2179   [ #  #  #  # ]:          0 :                           if (phdr != NULL && phdr->p_type == PT_INTERP)
    2180                 :            :                             {
    2181                 :          0 :                               is_statically_linked = -1;
    2182                 :          0 :                               break;
    2183                 :            :                             }
    2184                 :            :                         }
    2185                 :            :                     }
    2186                 :            :                 }
    2187                 :            : 
    2188   [ #  #  #  # ]:          0 :               if (is_statically_linked > 0 && shdr->sh_link == 0)
    2189   [ #  #  #  # ]:          0 :                 printf ("\
    2190                 :            :   %#0*" PRIx64 "  %-20s %*s  %s\n",
    2191                 :            :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2192                 :          0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2193                 :            :                         /* Avoid the leading R_ which isn't carrying any
    2194                 :            :                            information.  */
    2195                 :          0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2196                 :            :                                                buf, sizeof (buf)) + 2
    2197                 :          0 :                         : _("<INVALID RELOC>"),
    2198                 :            :                         class == ELFCLASS32 ? 10 : 18, "",
    2199                 :          0 :                         elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
    2200                 :            :               else
    2201   [ #  #  #  # ]:          0 :                 printf ("  %#0*" PRIx64 "  %-20s <%s %ld>\n",
    2202                 :            :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2203                 :          0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2204                 :            :                         /* Avoid the leading R_ which isn't carrying any
    2205                 :            :                            information.  */
    2206                 :          0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2207                 :            :                                                buf, sizeof (buf)) + 2
    2208                 :          0 :                         : _("<INVALID RELOC>"),
    2209                 :            :                         _("INVALID SYMBOL"),
    2210                 :          0 :                         (long int) GELF_R_SYM (rel->r_info));
    2211                 :            :             }
    2212         [ +  + ]:      12953 :           else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
    2213         [ -  + ]:      13038 :             printf ("  %#0*" PRIx64 "  %-20s %#0*" PRIx64 "  %s\n",
    2214                 :            :                     class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2215         [ +  - ]:         85 :                     likely (ebl_reloc_type_check (ebl,
    2216                 :            :                                                   GELF_R_TYPE (rel->r_info)))
    2217                 :            :                     /* Avoid the leading R_ which isn't carrying any
    2218                 :            :                        information.  */
    2219                 :         85 :                     ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2220                 :            :                                            buf, sizeof (buf)) + 2
    2221                 :          0 :                     : _("<INVALID RELOC>"),
    2222                 :            :                     class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2223                 :         85 :                     elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
    2224                 :            :           else
    2225                 :            :             {
    2226                 :            :               /* This is a relocation against a STT_SECTION symbol.  */
    2227                 :      12868 :               GElf_Shdr secshdr_mem;
    2228                 :      12868 :               GElf_Shdr *secshdr;
    2229                 :      12868 :               secshdr = gelf_getshdr (elf_getscn (ebl->elf,
    2230         [ +  - ]:      12868 :                                                   sym->st_shndx == SHN_XINDEX
    2231                 :          0 :                                                   ? xndx : sym->st_shndx),
    2232                 :            :                                       &secshdr_mem);
    2233                 :            : 
    2234         [ -  + ]:      12868 :               if (unlikely (secshdr == NULL))
    2235   [ #  #  #  # ]:          0 :                 printf ("  %#0*" PRIx64 "  %-20s <%s %ld>\n",
    2236                 :            :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2237                 :          0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2238                 :            :                         /* Avoid the leading R_ which isn't carrying any
    2239                 :            :                            information.  */
    2240                 :          0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2241                 :            :                                                buf, sizeof (buf)) + 2
    2242                 :          0 :                         : _("<INVALID RELOC>"),
    2243                 :            :                         _("INVALID SECTION"),
    2244         [ #  # ]:          0 :                         (long int) (sym->st_shndx == SHN_XINDEX
    2245                 :          0 :                                     ? xndx : sym->st_shndx));
    2246                 :            :               else
    2247   [ -  +  +  - ]:      38604 :                 printf ("  %#0*" PRIx64 "  %-20s %#0*" PRIx64 "  %s\n",
    2248                 :            :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2249                 :      12868 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2250                 :            :                         /* Avoid the leading R_ which isn't carrying any
    2251                 :            :                            information.  */
    2252                 :      12868 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2253                 :            :                                                buf, sizeof (buf)) + 2
    2254                 :          0 :                         : _("<INVALID RELOC>"),
    2255                 :            :                         class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2256                 :      12868 :                         elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
    2257                 :            :             }
    2258                 :            :         }
    2259                 :            :     }
    2260                 :            : }
    2261                 :            : 
    2262                 :            : 
    2263                 :            : /* Handle a relocation section.  */
    2264                 :            : static void
    2265                 :        283 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
    2266                 :            : {
    2267                 :        283 :   int class = gelf_getclass (ebl->elf);
    2268                 :        283 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
    2269                 :        283 :   int nentries = shdr->sh_size / sh_entsize;
    2270                 :            : 
    2271                 :            :   /* Get the data of the section.  */
    2272                 :        283 :   Elf_Data *data = elf_getdata (scn, NULL);
    2273         [ +  - ]:        283 :   if (data == NULL)
    2274                 :          0 :     return;
    2275                 :            : 
    2276                 :            :   /* Get the symbol table information.  */
    2277                 :        283 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    2278                 :        283 :   GElf_Shdr symshdr_mem;
    2279                 :        283 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    2280                 :        283 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    2281                 :            : 
    2282                 :            :   /* Get the section header of the section the relocations are for.  */
    2283                 :        283 :   GElf_Shdr destshdr_mem;
    2284                 :        283 :   GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
    2285                 :            :                                       &destshdr_mem);
    2286                 :            : 
    2287   [ +  -  -  + ]:        283 :   if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
    2288                 :            :     {
    2289                 :          0 :       printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
    2290                 :            :               shdr->sh_offset);
    2291                 :          0 :       return;
    2292                 :            :     }
    2293                 :            : 
    2294                 :            :   /* Search for the optional extended section index table.  */
    2295                 :        283 :   Elf_Data *xndxdata = NULL;
    2296                 :        283 :   int xndxscnidx = elf_scnshndx (scn);
    2297         [ -  + ]:        283 :   if (unlikely (xndxscnidx > 0))
    2298                 :          0 :     xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
    2299                 :            : 
    2300                 :            :   /* Get the section header string table index.  */
    2301                 :        283 :   size_t shstrndx;
    2302         [ -  + ]:        283 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2303                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    2304                 :            : 
    2305         [ +  + ]:        283 :   if (shdr->sh_info != 0)
    2306                 :        214 :     printf (ngettext ("\
    2307                 :            : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2308                 :            :                     "\
    2309                 :            : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2310                 :            :                     nentries),
    2311                 :            :           elf_ndxscn (scn),
    2312                 :        214 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2313                 :        214 :           (unsigned int) shdr->sh_info,
    2314                 :        214 :           elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
    2315                 :            :           shdr->sh_offset,
    2316                 :            :           nentries);
    2317                 :            :   else
    2318                 :            :     /* The .rela.dyn section does not refer to a specific section but
    2319                 :            :        instead of section index zero.  Do not try to print a section
    2320                 :            :        name.  */
    2321                 :         69 :     printf (ngettext ("\
    2322                 :            : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2323                 :            :                     "\
    2324                 :            : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2325                 :            :                       nentries),
    2326                 :         69 :             (unsigned int) elf_ndxscn (scn),
    2327                 :         69 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2328                 :            :             shdr->sh_offset,
    2329                 :            :             nentries);
    2330         [ +  + ]:        283 :   fputs_unlocked (class == ELFCLASS32
    2331                 :         16 :                   ? _("\
    2332                 :            :   Offset      Type            Value       Addend Name\n")
    2333                 :        267 :                   : _("\
    2334                 :            :   Offset              Type            Value               Addend Name\n"),
    2335                 :            :                   stdout);
    2336                 :            : 
    2337                 :        283 :   int is_statically_linked = 0;
    2338         [ +  + ]:      87577 :   for (int cnt = 0; cnt < nentries; ++cnt)
    2339                 :            :     {
    2340                 :      87294 :       GElf_Rela relmem;
    2341                 :      87294 :       GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
    2342         [ +  - ]:      87294 :       if (likely (rel != NULL))
    2343                 :            :         {
    2344                 :      87294 :           char buf[64];
    2345                 :      87294 :           GElf_Sym symmem;
    2346                 :      87294 :           Elf32_Word xndx;
    2347                 :     174588 :           GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
    2348                 :      87294 :                                             GELF_R_SYM (rel->r_info),
    2349                 :            :                                             &symmem, &xndx);
    2350                 :            : 
    2351         [ -  + ]:      87294 :           if (unlikely (sym == NULL))
    2352                 :            :             {
    2353                 :            :               /* As a special case we have to handle relocations in static
    2354                 :            :                  executables.  This only happens for IRELATIVE relocations
    2355                 :            :                  (so far).  There is no symbol table.  */
    2356         [ #  # ]:          0 :               if (is_statically_linked == 0)
    2357                 :            :                 {
    2358                 :            :                   /* Find the program header and look for a PT_INTERP entry. */
    2359                 :          0 :                   is_statically_linked = -1;
    2360         [ #  # ]:          0 :                   if (ehdr->e_type == ET_EXEC)
    2361                 :            :                     {
    2362                 :          0 :                       is_statically_linked = 1;
    2363                 :            : 
    2364         [ #  # ]:          0 :                       for (size_t inner = 0; inner < phnum; ++inner)
    2365                 :            :                         {
    2366                 :          0 :                           GElf_Phdr phdr_mem;
    2367                 :          0 :                           GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
    2368                 :            :                                                           &phdr_mem);
    2369   [ #  #  #  # ]:          0 :                           if (phdr != NULL && phdr->p_type == PT_INTERP)
    2370                 :            :                             {
    2371                 :          0 :                               is_statically_linked = -1;
    2372                 :          0 :                               break;
    2373                 :            :                             }
    2374                 :            :                         }
    2375                 :            :                     }
    2376                 :            :                 }
    2377                 :            : 
    2378   [ #  #  #  # ]:          0 :               if (is_statically_linked > 0 && shdr->sh_link == 0)
    2379   [ #  #  #  # ]:          0 :                 printf ("\
    2380                 :            :   %#0*" PRIx64 "  %-15s %*s  %#6" PRIx64 " %s\n",
    2381                 :            :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2382                 :          0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2383                 :            :                         /* Avoid the leading R_ which isn't carrying any
    2384                 :            :                            information.  */
    2385                 :          0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2386                 :            :                                                buf, sizeof (buf)) + 2
    2387                 :          0 :                         : _("<INVALID RELOC>"),
    2388                 :            :                         class == ELFCLASS32 ? 10 : 18, "",
    2389                 :            :                         rel->r_addend,
    2390                 :          0 :                         elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
    2391                 :            :               else
    2392   [ #  #  #  # ]:          0 :                 printf ("  %#0*" PRIx64 "  %-15s <%s %ld>\n",
    2393                 :            :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2394                 :          0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2395                 :            :                         /* Avoid the leading R_ which isn't carrying any
    2396                 :            :                            information.  */
    2397                 :          0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2398                 :            :                                                buf, sizeof (buf)) + 2
    2399                 :          0 :                         : _("<INVALID RELOC>"),
    2400                 :            :                         _("INVALID SYMBOL"),
    2401                 :          0 :                         (long int) GELF_R_SYM (rel->r_info));
    2402                 :            :             }
    2403         [ +  + ]:      87294 :           else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
    2404         [ +  + ]:     152355 :             printf ("\
    2405                 :            :   %#0*" PRIx64 "  %-15s %#0*" PRIx64 "  %+6" PRId64 " %s\n",
    2406                 :            :                     class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2407         [ +  - ]:      65061 :                     likely (ebl_reloc_type_check (ebl,
    2408                 :            :                                                   GELF_R_TYPE (rel->r_info)))
    2409                 :            :                     /* Avoid the leading R_ which isn't carrying any
    2410                 :            :                        information.  */
    2411                 :      65061 :                     ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2412                 :            :                                            buf, sizeof (buf)) + 2
    2413                 :          0 :                     : _("<INVALID RELOC>"),
    2414                 :            :                     class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2415                 :            :                     rel->r_addend,
    2416                 :      65061 :                     elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
    2417                 :            :           else
    2418                 :            :             {
    2419                 :            :               /* This is a relocation against a STT_SECTION symbol.  */
    2420                 :      22233 :               GElf_Shdr secshdr_mem;
    2421                 :      22233 :               GElf_Shdr *secshdr;
    2422                 :      22233 :               secshdr = gelf_getshdr (elf_getscn (ebl->elf,
    2423         [ +  - ]:      22233 :                                                   sym->st_shndx == SHN_XINDEX
    2424                 :          0 :                                                   ? xndx : sym->st_shndx),
    2425                 :            :                                       &secshdr_mem);
    2426                 :            : 
    2427         [ -  + ]:      22233 :               if (unlikely (secshdr == NULL))
    2428   [ #  #  #  # ]:          0 :                 printf ("  %#0*" PRIx64 "  %-15s <%s %ld>\n",
    2429                 :            :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2430                 :          0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2431                 :            :                         /* Avoid the leading R_ which isn't carrying any
    2432                 :            :                            information.  */
    2433                 :          0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2434                 :            :                                                buf, sizeof (buf)) + 2
    2435                 :          0 :                         : _("<INVALID RELOC>"),
    2436                 :            :                         _("INVALID SECTION"),
    2437         [ #  # ]:          0 :                         (long int) (sym->st_shndx == SHN_XINDEX
    2438                 :          0 :                                     ? xndx : sym->st_shndx));
    2439                 :            :               else
    2440   [ +  +  +  - ]:      66699 :                 printf ("\
    2441                 :            :   %#0*" PRIx64 "  %-15s %#0*" PRIx64 "  %+6" PRId64 " %s\n",
    2442                 :            :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2443                 :      22233 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2444                 :            :                         /* Avoid the leading R_ which isn't carrying any
    2445                 :            :                            information.  */
    2446                 :      22233 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2447                 :            :                                                buf, sizeof (buf)) + 2
    2448                 :          0 :                         : _("<INVALID RELOC>"),
    2449                 :            :                         class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2450                 :            :                         rel->r_addend,
    2451                 :      22233 :                         elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
    2452                 :            :             }
    2453                 :            :         }
    2454                 :            :     }
    2455                 :            : }
    2456                 :            : 
    2457                 :            : 
    2458                 :            : /* Print the program header.  Return true if a symtab is printed,
    2459                 :            :    false otherwise.  */
    2460                 :            : static bool
    2461                 :        319 : print_symtab (Ebl *ebl, int type)
    2462                 :            : {
    2463                 :            :   /* Use the dynamic section info to display symbol tables.  */
    2464   [ -  +  -  - ]:        319 :   if (use_dynamic_segment && type == SHT_DYNSYM)
    2465                 :          0 :     return handle_dynamic_symtab(ebl);
    2466                 :            : 
    2467                 :            :   /* Find the symbol table(s).  For this we have to search through the
    2468                 :            :      section table.  */
    2469                 :            :   Elf_Scn *scn = NULL;
    2470                 :            :   bool symtab_printed = false;
    2471                 :            : 
    2472         [ +  + ]:       9420 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    2473                 :            :     {
    2474                 :            :       /* Handle the section if it is a symbol table.  */
    2475                 :       9101 :       GElf_Shdr shdr_mem;
    2476                 :       9101 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2477                 :            : 
    2478   [ +  -  +  + ]:       9101 :       if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
    2479                 :            :         {
    2480         [ +  + ]:        187 :           if (symbol_table_section != NULL)
    2481                 :            :             {
    2482                 :            :               /* Get the section header string table index.  */
    2483                 :          7 :               size_t shstrndx;
    2484                 :          7 :               const char *sname;
    2485         [ -  + ]:          7 :               if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2486                 :          0 :                 error_exit (0,
    2487                 :            :                             _("cannot get section header string table index"));
    2488                 :          7 :               sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
    2489   [ +  -  +  + ]:          7 :               if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
    2490                 :          4 :                 continue;
    2491                 :            :             }
    2492                 :            : 
    2493         [ -  + ]:        183 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    2494                 :            :             {
    2495         [ #  # ]:          0 :               if (elf_compress (scn, 0, 0) < 0)
    2496                 :          0 :                 printf ("WARNING: %s [%zd]\n",
    2497                 :            :                         _("Couldn't uncompress section"),
    2498                 :            :                         elf_ndxscn (scn));
    2499                 :          0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    2500         [ #  # ]:          0 :               if (unlikely (shdr == NULL))
    2501                 :          0 :                 error_exit (0,
    2502                 :            :                             _("cannot get section [%zd] header: %s"),
    2503                 :            :                             elf_ndxscn (scn), elf_errmsg (-1));
    2504                 :            :             }
    2505                 :        183 :           symtab_printed = handle_symtab (ebl, scn, shdr);
    2506                 :            :         }
    2507                 :            :     }
    2508                 :            : 
    2509                 :            :   return symtab_printed;
    2510                 :            : }
    2511                 :            : 
    2512                 :            : 
    2513                 :            : static void
    2514                 :        183 : process_symtab (Ebl *ebl, unsigned int nsyms, Elf64_Word idx,
    2515                 :            :                 Elf32_Word verneed_stridx, Elf32_Word verdef_stridx,
    2516                 :            :                 Elf_Data *symdata, Elf_Data *versym_data,
    2517                 :            :                 Elf_Data *symstr_data, Elf_Data *verneed_data,
    2518                 :            :                 Elf_Data *verdef_data, Elf_Data *xndx_data)
    2519                 :            : {
    2520         [ +  + ]:      40447 :   for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
    2521                 :            :     {
    2522                 :      40264 :       char typebuf[64];
    2523                 :      40264 :       char bindbuf[64];
    2524                 :      40264 :       char scnbuf[64];
    2525                 :      40264 :       Elf32_Word xndx;
    2526                 :      40264 :       GElf_Sym sym_mem;
    2527                 :      40264 :       GElf_Sym *sym
    2528                 :      40264 :           = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
    2529                 :            : 
    2530         [ -  + ]:      40264 :       if (unlikely (sym == NULL))
    2531                 :          0 :         continue;
    2532                 :            : 
    2533                 :            :       /* Determine the real section index.  */
    2534         [ +  - ]:      40264 :       if (likely (sym->st_shndx != SHN_XINDEX))
    2535                 :      40264 :         xndx = sym->st_shndx;
    2536                 :            : 
    2537         [ +  + ]:      79815 :       printf (_ ("\
    2538                 :            : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
    2539                 :      40264 :               cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
    2540                 :            :               sym->st_value, sym->st_size,
    2541                 :      40264 :               ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
    2542                 :            :                                     sizeof (typebuf)),
    2543                 :      40264 :               ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
    2544                 :            :                                        bindbuf, sizeof (bindbuf)),
    2545                 :      40264 :               get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
    2546                 :      40264 :               ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
    2547                 :            :                                 sizeof (scnbuf), NULL, shnum),
    2548         [ -  + ]:      40264 :               use_dynamic_segment == true
    2549                 :          0 :                   ? (char *)symstr_data->d_buf + sym->st_name
    2550                 :      40264 :                   : elf_strptr (ebl->elf, idx, sym->st_name));
    2551                 :            : 
    2552         [ +  + ]:      40264 :       if (versym_data != NULL)
    2553                 :            :         {
    2554                 :            :           /* Get the version information.  */
    2555                 :       1961 :           GElf_Versym versym_mem;
    2556                 :       1961 :           GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
    2557                 :            : 
    2558   [ +  -  +  + ]:       1961 :           if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
    2559                 :            :             {
    2560                 :       1417 :               bool is_nobits = false;
    2561                 :       1417 :               bool check_def = xndx != SHN_UNDEF;
    2562                 :            : 
    2563   [ +  +  -  + ]:       1417 :               if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
    2564                 :            :                 {
    2565                 :       1407 :                   GElf_Shdr symshdr_mem;
    2566                 :       1407 :                   GElf_Shdr *symshdr = gelf_getshdr (
    2567                 :            :                       elf_getscn (ebl->elf, xndx), &symshdr_mem);
    2568                 :            : 
    2569                 :       1407 :                   is_nobits
    2570   [ +  -  +  + ]:       2796 :                       = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
    2571                 :            :                 }
    2572                 :            : 
    2573         [ +  + ]:       1417 :               if (is_nobits || !check_def)
    2574                 :            :                 {
    2575                 :            :                   /* We must test both.  */
    2576                 :       1265 :                   GElf_Vernaux vernaux_mem;
    2577                 :       1265 :                   GElf_Vernaux *vernaux = NULL;
    2578                 :       1265 :                   size_t vn_offset = 0;
    2579                 :            : 
    2580                 :       1265 :                   GElf_Verneed verneed_mem;
    2581                 :       1265 :                   GElf_Verneed *verneed
    2582                 :       1265 :                       = gelf_getverneed (verneed_data, 0, &verneed_mem);
    2583         [ +  - ]:       5775 :                   while (verneed != NULL)
    2584                 :            :                     {
    2585                 :       5775 :                       size_t vna_offset = vn_offset;
    2586                 :            : 
    2587                 :       5775 :                       vernaux = gelf_getvernaux (verneed_data,
    2588                 :       5775 :                                                  vna_offset += verneed->vn_aux,
    2589                 :            :                                                  &vernaux_mem);
    2590   [ +  -  +  + ]:       9844 :                       while (vernaux != NULL && vernaux->vna_other != *versym
    2591         [ +  + ]:       8579 :                              && vernaux->vna_next != 0
    2592                 :       4069 :                              && (verneed_data->d_size - vna_offset
    2593         [ +  - ]:       4069 :                                  >= vernaux->vna_next))
    2594                 :            :                         {
    2595                 :            :                           /* Update the offset.  */
    2596                 :       4069 :                           vna_offset += vernaux->vna_next;
    2597                 :            : 
    2598                 :       4069 :                           vernaux = (vernaux->vna_next == 0
    2599                 :            :                                          ? NULL
    2600                 :       4069 :                                          : gelf_getvernaux (verneed_data,
    2601                 :            :                                                             vna_offset,
    2602                 :            :                                                             &vernaux_mem));
    2603                 :            :                         }
    2604                 :            : 
    2605                 :            :                       /* Check whether we found the version.  */
    2606   [ +  -  +  + ]:       5775 :                       if (vernaux != NULL && vernaux->vna_other == *versym)
    2607                 :            :                         /* Found it.  */
    2608                 :            :                         break;
    2609                 :            : 
    2610         [ +  - ]:       4510 :                       if (verneed_data->d_size - vn_offset < verneed->vn_next)
    2611                 :            :                         break;
    2612                 :            : 
    2613                 :       4510 :                       vn_offset += verneed->vn_next;
    2614                 :       4510 :                       verneed
    2615                 :            :                           = (verneed->vn_next == 0
    2616                 :            :                                  ? NULL
    2617         [ +  - ]:       4510 :                                  : gelf_getverneed (verneed_data, vn_offset,
    2618                 :            :                                                     &verneed_mem));
    2619                 :            :                     }
    2620                 :            : 
    2621   [ +  -  +  - ]:       1265 :                   if (vernaux != NULL && vernaux->vna_other == *versym)
    2622                 :            :                     {
    2623                 :       2530 :                       printf ("@%s (%u)",
    2624         [ -  + ]:       1265 :                               use_dynamic_segment == true
    2625                 :          0 :                                   ? (char *)symstr_data->d_buf
    2626                 :          0 :                                         + vernaux->vna_name
    2627                 :       1265 :                                   : elf_strptr (ebl->elf, verneed_stridx,
    2628                 :       1265 :                                                 vernaux->vna_name),
    2629                 :            :                               (unsigned int)vernaux->vna_other);
    2630                 :       1265 :                       check_def = 0;
    2631                 :            :                     }
    2632         [ #  # ]:          0 :                   else if (unlikely (!is_nobits))
    2633                 :          0 :                     error (0, 0, _ ("bad dynamic symbol"));
    2634                 :            :                   else
    2635                 :            :                     check_def = 1;
    2636                 :            :                 }
    2637                 :            : 
    2638   [ +  -  +  - ]:       1417 :               if (check_def && *versym != 0x8001)
    2639                 :            :                 {
    2640                 :            :                   /* We must test both.  */
    2641                 :        152 :                   size_t vd_offset = 0;
    2642                 :            : 
    2643                 :        152 :                   GElf_Verdef verdef_mem;
    2644                 :        152 :                   GElf_Verdef *verdef
    2645                 :        152 :                       = gelf_getverdef (verdef_data, 0, &verdef_mem);
    2646         [ +  - ]:        433 :                   while (verdef != NULL)
    2647                 :            :                     {
    2648         [ +  + ]:        433 :                       if (verdef->vd_ndx == (*versym & 0x7fff))
    2649                 :            :                         /* Found the definition.  */
    2650                 :            :                         break;
    2651                 :            : 
    2652         [ +  - ]:        281 :                       if (verdef_data->d_size - vd_offset < verdef->vd_next)
    2653                 :            :                         break;
    2654                 :            : 
    2655                 :        281 :                       vd_offset += verdef->vd_next;
    2656                 :        281 :                       verdef = (verdef->vd_next == 0
    2657                 :            :                                     ? NULL
    2658         [ +  - ]:        281 :                                     : gelf_getverdef (verdef_data, vd_offset,
    2659                 :            :                                                       &verdef_mem));
    2660                 :            :                     }
    2661                 :            : 
    2662         [ +  - ]:        152 :                   if (verdef != NULL)
    2663                 :            :                     {
    2664                 :        152 :                       GElf_Verdaux verdaux_mem;
    2665                 :        304 :                       GElf_Verdaux *verdaux = gelf_getverdaux (
    2666                 :        152 :                           verdef_data, vd_offset + verdef->vd_aux,
    2667                 :            :                           &verdaux_mem);
    2668                 :            : 
    2669         [ +  - ]:        152 :                       if (verdaux != NULL)
    2670         [ +  - ]:        304 :                         printf ((*versym & 0x8000) ? "@%s" : "@@%s",
    2671         [ -  + ]:        152 :                                 use_dynamic_segment == true
    2672                 :          0 :                                     ? (char *)symstr_data->d_buf
    2673                 :          0 :                                           + verdaux->vda_name
    2674                 :        152 :                                     : elf_strptr (ebl->elf, verdef_stridx,
    2675                 :        152 :                                                   verdaux->vda_name));
    2676                 :            :                     }
    2677                 :            :                 }
    2678                 :            :             }
    2679                 :            :         }
    2680                 :            : 
    2681         [ +  + ]:      80528 :       putchar_unlocked ('\n');
    2682                 :            :     }
    2683                 :        183 : }
    2684                 :            : 
    2685                 :            : 
    2686                 :            : static bool
    2687                 :        183 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    2688                 :            : {
    2689                 :        183 :   Elf_Data *versym_data = NULL;
    2690                 :        183 :   Elf_Data *verneed_data = NULL;
    2691                 :        183 :   Elf_Data *verdef_data = NULL;
    2692                 :        183 :   Elf_Data *xndx_data = NULL;
    2693                 :        183 :   int class = gelf_getclass (ebl->elf);
    2694                 :        183 :   Elf32_Word verneed_stridx = 0;
    2695                 :        183 :   Elf32_Word verdef_stridx = 0;
    2696                 :            : 
    2697                 :            :   /* Get the data of the section.  */
    2698                 :        183 :   Elf_Data *data = elf_getdata (scn, NULL);
    2699         [ +  - ]:        183 :   if (data == NULL)
    2700                 :            :     return false;
    2701                 :            : 
    2702                 :            :   /* Find out whether we have other sections we might need.  */
    2703                 :            :   Elf_Scn *runscn = NULL;
    2704         [ +  + ]:       5554 :   while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
    2705                 :            :     {
    2706                 :       5371 :       GElf_Shdr runshdr_mem;
    2707                 :       5371 :       GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
    2708                 :            : 
    2709         [ +  - ]:       5371 :       if (likely (runshdr != NULL))
    2710                 :            :         {
    2711         [ +  + ]:       5371 :           if (runshdr->sh_type == SHT_GNU_versym
    2712         [ +  + ]:        115 :               && runshdr->sh_link == elf_ndxscn (scn))
    2713                 :            :             /* Bingo, found the version information.  Now get the data.  */
    2714                 :         89 :             versym_data = elf_getdata (runscn, NULL);
    2715         [ +  + ]:       5282 :           else if (runshdr->sh_type == SHT_GNU_verneed)
    2716                 :            :             {
    2717                 :            :               /* This is the information about the needed versions.  */
    2718                 :        115 :               verneed_data = elf_getdata (runscn, NULL);
    2719                 :        115 :               verneed_stridx = runshdr->sh_link;
    2720                 :            :             }
    2721         [ +  + ]:       5167 :           else if (runshdr->sh_type == SHT_GNU_verdef)
    2722                 :            :             {
    2723                 :            :               /* This is the information about the defined versions.  */
    2724                 :          4 :               verdef_data = elf_getdata (runscn, NULL);
    2725                 :          4 :               verdef_stridx = runshdr->sh_link;
    2726                 :            :             }
    2727         [ -  + ]:       5163 :           else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
    2728         [ #  # ]:          0 :               && runshdr->sh_link == elf_ndxscn (scn))
    2729                 :            :             /* Extended section index.  */
    2730                 :          0 :             xndx_data = elf_getdata (runscn, NULL);
    2731                 :            :         }
    2732                 :            :     }
    2733                 :            : 
    2734                 :            :   /* Get the section header string table index.  */
    2735                 :        183 :   size_t shstrndx;
    2736         [ -  + ]:        183 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2737                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    2738                 :            : 
    2739                 :        183 :   GElf_Shdr glink_mem;
    2740                 :        183 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    2741                 :            :                                    &glink_mem);
    2742         [ -  + ]:        183 :   if (glink == NULL)
    2743                 :          0 :     error_exit (0, _("invalid sh_link value in section %zu"),
    2744                 :            :                 elf_ndxscn (scn));
    2745                 :            : 
    2746                 :            :   /* Now we can compute the number of entries in the section.  */
    2747                 :        366 :   unsigned int nsyms = data->d_size / (class == ELFCLASS32
    2748                 :            :                                        ? sizeof (Elf32_Sym)
    2749         [ +  + ]:        183 :                                        : sizeof (Elf64_Sym));
    2750                 :            : 
    2751                 :        183 :   printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
    2752                 :            :                     "\nSymbol table [%2u] '%s' contains %u entries:\n",
    2753                 :            :                     nsyms),
    2754                 :        183 :           (unsigned int) elf_ndxscn (scn),
    2755                 :        183 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
    2756                 :        183 :   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
    2757                 :            :                     " %lu local symbols  String table: [%2u] '%s'\n",
    2758                 :            :                     shdr->sh_info),
    2759                 :        183 :           (unsigned long int) shdr->sh_info,
    2760                 :        183 :           (unsigned int) shdr->sh_link,
    2761                 :        183 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    2762                 :            : 
    2763         [ +  + ]:        366 :   fputs_unlocked (class == ELFCLASS32
    2764                 :         27 :                   ? _("\
    2765                 :            :   Num:    Value   Size Type    Bind   Vis          Ndx Name\n")
    2766                 :        156 :                   : _("\
    2767                 :            :   Num:            Value   Size Type    Bind   Vis          Ndx Name\n"),
    2768                 :            :                   stdout);
    2769                 :            : 
    2770                 :        183 :         process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
    2771                 :            :         data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
    2772                 :        183 :     return true;
    2773                 :            : }
    2774                 :            : 
    2775                 :            : 
    2776                 :            : static bool
    2777                 :          0 : handle_dynamic_symtab (Ebl *ebl)
    2778                 :            : {
    2779                 :          0 :   GElf_Phdr phdr_mem;
    2780                 :          0 :   GElf_Phdr *phdr = NULL;
    2781                 :            :   /* phnum is a static variable which was already fetched in function
    2782                 :            :      process_elf_file.  */
    2783         [ #  # ]:          0 :   for (size_t i = 0; i < phnum; ++i)
    2784                 :            :     {
    2785                 :          0 :       phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
    2786         [ #  # ]:          0 :       if (phdr->p_type == PT_DYNAMIC)
    2787                 :            :         break;
    2788                 :            :     }
    2789         [ #  # ]:          0 :   if (phdr == NULL)
    2790                 :            :     return false;
    2791                 :            : 
    2792                 :          0 :   GElf_Addr addrs[i_max] = {
    2793                 :            :     0,
    2794                 :            :   };
    2795                 :          0 :   GElf_Off offs[i_max] = {
    2796                 :            :     0,
    2797                 :            :   };
    2798                 :          0 :   get_dynscn_addrs (ebl->elf, phdr, addrs);
    2799                 :          0 :   find_offsets (ebl->elf, 0, i_max, addrs, offs);
    2800                 :            : 
    2801                 :          0 :   size_t syments = 0;
    2802                 :            : 
    2803                 :          0 :   GElf_Ehdr ehdr_mem;
    2804                 :          0 :   GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
    2805                 :            : 
    2806         [ #  # ]:          0 :   if (offs[i_hash] != 0)
    2807                 :            :     {
    2808                 :            :       /* In the original format, .hash says the size of .dynsym.  */
    2809                 :            : 
    2810   [ #  #  #  #  :          0 :       size_t entsz = SH_ENTSIZE_HASH (ehdr);
                   #  # ]
    2811                 :            :       Elf_Data *data
    2812                 :          0 :           = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
    2813                 :            :                                   (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
    2814         [ #  # ]:          0 :       if (data != NULL)
    2815                 :          0 :         syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
    2816         [ #  # ]:          0 :                               : *(const GElf_Xword *)data->d_buf);
    2817                 :            :     }
    2818   [ #  #  #  # ]:          0 :   if (offs[i_gnu_hash] != 0 && syments == 0)
    2819                 :            :     {
    2820                 :            :       /* In the new format, we can derive it with some work.  */
    2821                 :            : 
    2822                 :          0 :       const struct
    2823                 :            :       {
    2824                 :            :         Elf32_Word nbuckets;
    2825                 :            :         Elf32_Word symndx;
    2826                 :            :         Elf32_Word maskwords;
    2827                 :            :         Elf32_Word shift2;
    2828                 :            :       } * header;
    2829                 :            : 
    2830                 :          0 :       Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
    2831                 :            :                                              sizeof *header, ELF_T_WORD);
    2832         [ #  # ]:          0 :       if (data != NULL)
    2833                 :            :         {
    2834                 :          0 :           header = data->d_buf;
    2835                 :          0 :           Elf32_Word nbuckets = header->nbuckets;
    2836                 :          0 :           Elf32_Word symndx = header->symndx;
    2837                 :          0 :           GElf_Off buckets_at
    2838                 :          0 :               = (offs[i_gnu_hash] + sizeof *header
    2839                 :          0 :                  + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
    2840                 :          0 :                     * header->maskwords));
    2841                 :            : 
    2842                 :            :           // elf_getdata_rawchunk takes a size_t, make sure it
    2843                 :            :           // doesn't overflow.
    2844                 :            : #if SIZE_MAX <= UINT32_MAX
    2845                 :            :           if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
    2846                 :            :             data = NULL;
    2847                 :            :           else
    2848                 :            : #endif
    2849                 :          0 :             data = elf_getdata_rawchunk (ebl->elf, buckets_at,
    2850                 :            :                                          nbuckets * sizeof (Elf32_Word),
    2851                 :            :                                          ELF_T_WORD);
    2852         [ #  # ]:          0 :           if (data != NULL && symndx < nbuckets)
    2853                 :            :             {
    2854                 :          0 :               const Elf32_Word *const buckets = data->d_buf;
    2855                 :          0 :               Elf32_Word maxndx = symndx;
    2856         [ #  # ]:          0 :               for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
    2857                 :          0 :                 if (buckets[bucket] > maxndx)
    2858                 :            :                   maxndx = buckets[bucket];
    2859                 :            : 
    2860                 :          0 :               GElf_Off hasharr_at
    2861                 :            :                   = (buckets_at + nbuckets * sizeof (Elf32_Word));
    2862                 :          0 :               hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
    2863                 :          0 :               do
    2864                 :            :                 {
    2865                 :          0 :                   data = elf_getdata_rawchunk (
    2866                 :            :                       ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
    2867   [ #  #  #  # ]:          0 :                   if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
    2868                 :            :                     {
    2869                 :          0 :                       syments = maxndx + 1;
    2870                 :          0 :                       break;
    2871                 :            :                     }
    2872                 :          0 :                   ++maxndx;
    2873                 :          0 :                   hasharr_at += sizeof (Elf32_Word);
    2874                 :            :                 }
    2875         [ #  # ]:          0 :               while (data != NULL);
    2876                 :            :             }
    2877                 :            :         }
    2878                 :            :     }
    2879   [ #  #  #  # ]:          0 :   if (offs[i_strtab] > offs[i_symtab] && syments == 0)
    2880                 :          0 :     syments = ((offs[i_strtab] - offs[i_symtab])
    2881                 :          0 :                / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
    2882                 :            : 
    2883   [ #  #  #  #  :          0 :   if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
                   #  # ]
    2884                 :            :     {
    2885                 :          0 :       error_exit (0, _ ("Dynamic symbol information is not available for "
    2886                 :            :                         "displaying symbols."));
    2887                 :            :     }
    2888                 :            : 
    2889                 :            :   /* All the data chunk initializaion.  */
    2890                 :          0 :   Elf_Data *symdata = NULL;
    2891                 :          0 :   Elf_Data *symstrdata = NULL;
    2892                 :          0 :   Elf_Data *versym_data = NULL;
    2893                 :          0 :   Elf_Data *verdef_data = NULL;
    2894                 :          0 :   Elf_Data *verneed_data = NULL;
    2895                 :            : 
    2896                 :          0 :   symdata = elf_getdata_rawchunk (
    2897                 :          0 :       ebl->elf, offs[i_symtab],
    2898                 :            :       gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
    2899                 :          0 :   symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
    2900                 :            :                                      ELF_T_BYTE);
    2901                 :          0 :   versym_data = elf_getdata_rawchunk (
    2902                 :          0 :       ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
    2903                 :            : 
    2904                 :            :   /* Get the verneed_data without vernaux.  */
    2905                 :          0 :   verneed_data = elf_getdata_rawchunk (
    2906                 :          0 :       ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
    2907                 :            :       ELF_T_VNEED);
    2908                 :          0 :   size_t vernauxnum = 0;
    2909                 :          0 :   size_t vn_next_offset = 0;
    2910                 :            : 
    2911         [ #  # ]:          0 :   for (size_t i = 0; i < addrs[i_verneednum]; i++)
    2912                 :            :     {
    2913                 :          0 :       GElf_Verneed *verneed
    2914                 :          0 :           = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
    2915                 :          0 :       vernauxnum += verneed->vn_cnt;
    2916                 :          0 :       vn_next_offset += verneed->vn_next;
    2917                 :            :     }
    2918                 :            : 
    2919                 :            :   /* Update the verneed_data to include the vernaux.  */
    2920                 :          0 :   verneed_data = elf_getdata_rawchunk (
    2921                 :          0 :       ebl->elf, offs[i_verneed],
    2922                 :          0 :       (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed), ELF_T_VNEED);
    2923                 :            : 
    2924                 :            :   /* Get the verdef_data without verdaux.  */
    2925                 :          0 :   verdef_data = elf_getdata_rawchunk (
    2926                 :          0 :       ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
    2927                 :            :       ELF_T_VDEF);
    2928                 :          0 :   size_t verdauxnum = 0;
    2929                 :          0 :   size_t vd_next_offset = 0;
    2930                 :            : 
    2931         [ #  # ]:          0 :   for (size_t i = 0; i < addrs[i_verdefnum]; i++)
    2932                 :            :     {
    2933                 :          0 :       GElf_Verdef *verdef
    2934                 :          0 :           = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
    2935                 :          0 :       verdauxnum += verdef->vd_cnt;
    2936                 :          0 :       vd_next_offset += verdef->vd_next;
    2937                 :            :     }
    2938                 :            : 
    2939                 :            :   /* Update the verdef_data to include the verdaux.  */
    2940                 :          0 :   verdef_data = elf_getdata_rawchunk (
    2941                 :          0 :       ebl->elf, offs[i_verdef],
    2942                 :          0 :       (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
    2943                 :            : 
    2944                 :          0 :   unsigned int nsyms = (unsigned int)syments;
    2945                 :          0 :   process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
    2946                 :            :                   verneed_data, verdef_data, NULL);
    2947                 :          0 :   return true;
    2948                 :            : }
    2949                 :            : 
    2950                 :            : 
    2951                 :            : /* Print version information.  */
    2952                 :            : static void
    2953                 :        142 : print_verinfo (Ebl *ebl)
    2954                 :            : {
    2955                 :            :   /* Find the version information sections.  For this we have to
    2956                 :            :      search through the section table.  */
    2957                 :        142 :   Elf_Scn *scn = NULL;
    2958                 :            : 
    2959         [ +  + ]:       4198 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    2960                 :            :     {
    2961                 :            :       /* Handle the section if it is part of the versioning handling.  */
    2962                 :       4056 :       GElf_Shdr shdr_mem;
    2963                 :       4056 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2964                 :            : 
    2965         [ +  - ]:       4056 :       if (likely (shdr != NULL))
    2966                 :            :         {
    2967         [ +  + ]:       4056 :           if (shdr->sh_type == SHT_GNU_verneed)
    2968                 :         79 :             handle_verneed (ebl, scn, shdr);
    2969         [ +  + ]:       3977 :           else if (shdr->sh_type == SHT_GNU_verdef)
    2970                 :          2 :             handle_verdef (ebl, scn, shdr);
    2971         [ +  + ]:       3975 :           else if (shdr->sh_type == SHT_GNU_versym)
    2972                 :         79 :             handle_versym (ebl, scn, shdr);
    2973                 :            :         }
    2974                 :            :     }
    2975                 :        142 : }
    2976                 :            : 
    2977                 :            : 
    2978                 :            : static const char *
    2979                 :        190 : get_ver_flags (unsigned int flags)
    2980                 :            : {
    2981                 :        190 :   static char buf[32];
    2982                 :        190 :   char *endp;
    2983                 :            : 
    2984         [ +  + ]:        190 :   if (flags == 0)
    2985                 :        188 :     return _("none");
    2986                 :            : 
    2987         [ +  - ]:          2 :   if (flags & VER_FLG_BASE)
    2988                 :          2 :     endp = stpcpy (buf, "BASE ");
    2989                 :            :   else
    2990                 :            :     endp = buf;
    2991                 :            : 
    2992         [ -  + ]:          2 :   if (flags & VER_FLG_WEAK)
    2993                 :            :     {
    2994         [ #  # ]:          0 :       if (endp != buf)
    2995                 :          0 :         endp = stpcpy (endp, "| ");
    2996                 :            : 
    2997                 :          0 :       endp = stpcpy (endp, "WEAK ");
    2998                 :            :     }
    2999                 :            : 
    3000         [ -  + ]:          2 :   if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
    3001                 :            :     {
    3002                 :          0 :       strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
    3003                 :          0 :       buf[sizeof (buf) - 1] = '\0';
    3004                 :            :     }
    3005                 :            : 
    3006                 :            :   return buf;
    3007                 :            : }
    3008                 :            : 
    3009                 :            : 
    3010                 :            : static void
    3011                 :         79 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    3012                 :            : {
    3013                 :         79 :   int class = gelf_getclass (ebl->elf);
    3014                 :            : 
    3015                 :            :   /* Get the data of the section.  */
    3016                 :         79 :   Elf_Data *data = elf_getdata (scn, NULL);
    3017         [ -  + ]:         79 :   if (data == NULL)
    3018                 :          0 :     return;
    3019                 :            : 
    3020                 :            :   /* Get the section header string table index.  */
    3021                 :         79 :   size_t shstrndx;
    3022         [ -  + ]:         79 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3023                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    3024                 :            : 
    3025                 :         79 :   GElf_Shdr glink_mem;
    3026                 :         79 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    3027                 :            :                                    &glink_mem);
    3028         [ -  + ]:         79 :   if (glink == NULL)
    3029                 :          0 :     error_exit (0, _("invalid sh_link value in section %zu"),
    3030                 :            :                 elf_ndxscn (scn));
    3031                 :            : 
    3032                 :         79 :   printf (ngettext ("\
    3033                 :            : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    3034                 :            :                     "\
    3035                 :            : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    3036                 :            :                     shdr->sh_info),
    3037                 :         79 :           (unsigned int) elf_ndxscn (scn),
    3038                 :         79 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
    3039                 :            :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    3040                 :            :           shdr->sh_offset,
    3041         [ +  + ]:         79 :           (unsigned int) shdr->sh_link,
    3042                 :         79 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3043                 :            : 
    3044                 :         79 :   unsigned int offset = 0;
    3045         [ +  - ]:        123 :   for (int cnt = shdr->sh_info; --cnt >= 0; )
    3046                 :            :     {
    3047                 :            :       /* Get the data at the next offset.  */
    3048                 :        123 :       GElf_Verneed needmem;
    3049                 :        123 :       GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
    3050         [ +  - ]:        123 :       if (unlikely (need == NULL))
    3051                 :            :         break;
    3052                 :            : 
    3053                 :        369 :       printf (_("  %#06x: Version: %hu  File: %s  Cnt: %hu\n"),
    3054                 :        123 :               offset, (unsigned short int) need->vn_version,
    3055                 :        123 :               elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
    3056                 :        123 :               (unsigned short int) need->vn_cnt);
    3057                 :            : 
    3058                 :        123 :       unsigned int auxoffset = offset + need->vn_aux;
    3059         [ +  - ]:        178 :       for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
    3060                 :            :         {
    3061                 :        178 :           GElf_Vernaux auxmem;
    3062                 :        178 :           GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
    3063         [ +  - ]:        178 :           if (unlikely (aux == NULL))
    3064                 :            :             break;
    3065                 :            : 
    3066                 :        178 :           printf (_("  %#06x: Name: %s  Flags: %s  Version: %hu\n"),
    3067                 :            :                   auxoffset,
    3068                 :        178 :                   elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
    3069                 :        178 :                   get_ver_flags (aux->vna_flags),
    3070                 :        178 :                   (unsigned short int) aux->vna_other);
    3071                 :            : 
    3072         [ +  + ]:        178 :           if (aux->vna_next == 0)
    3073                 :            :             break;
    3074                 :            : 
    3075                 :         55 :           auxoffset += aux->vna_next;
    3076                 :            :         }
    3077                 :            : 
    3078                 :            :       /* Find the next offset.  */
    3079         [ +  + ]:        123 :       if (need->vn_next == 0)
    3080                 :            :         break;
    3081                 :            : 
    3082                 :         44 :       offset += need->vn_next;
    3083                 :            :     }
    3084                 :            : }
    3085                 :            : 
    3086                 :            : 
    3087                 :            : static void
    3088                 :          2 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    3089                 :            : {
    3090                 :            :   /* Get the data of the section.  */
    3091                 :          2 :   Elf_Data *data = elf_getdata (scn, NULL);
    3092         [ -  + ]:          2 :   if (data == NULL)
    3093                 :          0 :     return;
    3094                 :            : 
    3095                 :            :   /* Get the section header string table index.  */
    3096                 :          2 :   size_t shstrndx;
    3097         [ -  + ]:          2 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3098                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    3099                 :            : 
    3100                 :          2 :   GElf_Shdr glink_mem;
    3101                 :          2 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    3102                 :            :                                    &glink_mem);
    3103         [ -  + ]:          2 :   if (glink == NULL)
    3104                 :          0 :     error_exit (0, _("invalid sh_link value in section %zu"),
    3105                 :            :                 elf_ndxscn (scn));
    3106                 :            : 
    3107                 :          2 :   int class = gelf_getclass (ebl->elf);
    3108                 :          2 :   printf (ngettext ("\
    3109                 :            : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    3110                 :            :                     "\
    3111                 :            : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    3112                 :            :                     shdr->sh_info),
    3113                 :          2 :           (unsigned int) elf_ndxscn (scn),
    3114                 :          2 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3115                 :            :           shdr->sh_info,
    3116                 :            :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    3117                 :            :           shdr->sh_offset,
    3118         [ +  - ]:          2 :           (unsigned int) shdr->sh_link,
    3119                 :          2 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3120                 :            : 
    3121                 :          2 :   unsigned int offset = 0;
    3122         [ +  - ]:         12 :   for (int cnt = shdr->sh_info; --cnt >= 0; )
    3123                 :            :     {
    3124                 :            :       /* Get the data at the next offset.  */
    3125                 :         12 :       GElf_Verdef defmem;
    3126                 :         12 :       GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
    3127         [ +  - ]:         12 :       if (unlikely (def == NULL))
    3128                 :            :         break;
    3129                 :            : 
    3130                 :         12 :       unsigned int auxoffset = offset + def->vd_aux;
    3131                 :         12 :       GElf_Verdaux auxmem;
    3132                 :         12 :       GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
    3133         [ +  - ]:         12 :       if (unlikely (aux == NULL))
    3134                 :            :         break;
    3135                 :            : 
    3136                 :         48 :       printf (_("\
    3137                 :            :   %#06x: Version: %hd  Flags: %s  Index: %hd  Cnt: %hd  Name: %s\n"),
    3138                 :         12 :               offset, def->vd_version,
    3139                 :         12 :               get_ver_flags (def->vd_flags),
    3140                 :         12 :               def->vd_ndx,
    3141                 :         12 :               def->vd_cnt,
    3142                 :         12 :               elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
    3143                 :            : 
    3144                 :         12 :       auxoffset += aux->vda_next;
    3145         [ +  + ]:         12 :       for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
    3146                 :            :         {
    3147                 :          8 :           aux = gelf_getverdaux (data, auxoffset, &auxmem);
    3148         [ +  - ]:          8 :           if (unlikely (aux == NULL))
    3149                 :            :             break;
    3150                 :            : 
    3151                 :          8 :           printf (_("  %#06x: Parent %d: %s\n"),
    3152                 :            :                   auxoffset, cnt2,
    3153                 :          8 :                   elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
    3154                 :            : 
    3155         [ -  + ]:          8 :           if (aux->vda_next == 0)
    3156                 :            :             break;
    3157                 :            : 
    3158                 :          0 :           auxoffset += aux->vda_next;
    3159                 :            :         }
    3160                 :            : 
    3161                 :            :       /* Find the next offset.  */
    3162         [ +  + ]:         12 :       if (def->vd_next == 0)
    3163                 :            :         break;
    3164                 :         10 :       offset += def->vd_next;
    3165                 :            :     }
    3166                 :            : }
    3167                 :            : 
    3168                 :            : 
    3169                 :            : static void
    3170                 :         79 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    3171                 :            : {
    3172                 :         79 :   int class = gelf_getclass (ebl->elf);
    3173                 :         79 :   const char **vername;
    3174                 :         79 :   const char **filename;
    3175                 :            : 
    3176                 :            :   /* Get the data of the section.  */
    3177                 :         79 :   Elf_Data *data = elf_getdata (scn, NULL);
    3178         [ +  - ]:         79 :   if (data == NULL)
    3179                 :          0 :     return;
    3180                 :            : 
    3181                 :            :   /* Get the section header string table index.  */
    3182                 :         79 :   size_t shstrndx;
    3183         [ -  + ]:         79 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3184                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    3185                 :            : 
    3186                 :            :   /* We have to find the version definition section and extract the
    3187                 :            :      version names.  */
    3188                 :            :   Elf_Scn *defscn = NULL;
    3189                 :            :   Elf_Scn *needscn = NULL;
    3190                 :            : 
    3191                 :            :   Elf_Scn *verscn = NULL;
    3192         [ +  + ]:       2399 :   while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
    3193                 :            :     {
    3194                 :       2320 :       GElf_Shdr vershdr_mem;
    3195                 :       2320 :       GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
    3196                 :            : 
    3197         [ +  - ]:       2320 :       if (likely (vershdr != NULL))
    3198                 :            :         {
    3199         [ +  + ]:       2320 :           if (vershdr->sh_type == SHT_GNU_verdef)
    3200                 :            :             defscn = verscn;
    3201         [ +  + ]:       2318 :           else if (vershdr->sh_type == SHT_GNU_verneed)
    3202                 :         79 :             needscn = verscn;
    3203                 :            :         }
    3204                 :            :     }
    3205                 :            : 
    3206                 :         79 :   size_t nvername;
    3207         [ +  - ]:         79 :   if (defscn != NULL || needscn != NULL)
    3208                 :            :     {
    3209                 :            :       /* We have a version information (better should have).  Now get
    3210                 :            :          the version names.  First find the maximum version number.  */
    3211                 :         79 :       nvername = 0;
    3212         [ +  + ]:         79 :       if (defscn != NULL)
    3213                 :            :         {
    3214                 :            :           /* Run through the version definitions and find the highest
    3215                 :            :              index.  */
    3216                 :          2 :           unsigned int offset = 0;
    3217                 :          2 :           Elf_Data *defdata;
    3218                 :          2 :           GElf_Shdr defshdrmem;
    3219                 :          2 :           GElf_Shdr *defshdr;
    3220                 :            : 
    3221                 :          2 :           defdata = elf_getdata (defscn, NULL);
    3222         [ +  - ]:          2 :           if (unlikely (defdata == NULL))
    3223                 :          0 :             return;
    3224                 :            : 
    3225                 :          2 :           defshdr = gelf_getshdr (defscn, &defshdrmem);
    3226         [ +  - ]:          2 :           if (unlikely (defshdr == NULL))
    3227                 :            :             return;
    3228                 :            : 
    3229         [ +  - ]:         12 :           for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
    3230                 :            :             {
    3231                 :         12 :               GElf_Verdef defmem;
    3232                 :         12 :               GElf_Verdef *def;
    3233                 :            : 
    3234                 :            :               /* Get the data at the next offset.  */
    3235                 :         12 :               def = gelf_getverdef (defdata, offset, &defmem);
    3236         [ +  - ]:         12 :               if (unlikely (def == NULL))
    3237                 :            :                 break;
    3238                 :            : 
    3239                 :         12 :               nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
    3240                 :            : 
    3241         [ +  + ]:         12 :               if (def->vd_next == 0)
    3242                 :            :                 break;
    3243                 :         10 :               offset += def->vd_next;
    3244                 :            :             }
    3245                 :            :         }
    3246         [ +  - ]:         79 :       if (needscn != NULL)
    3247                 :            :         {
    3248                 :         79 :           unsigned int offset = 0;
    3249                 :         79 :           Elf_Data *needdata;
    3250                 :         79 :           GElf_Shdr needshdrmem;
    3251                 :         79 :           GElf_Shdr *needshdr;
    3252                 :            : 
    3253                 :         79 :           needdata = elf_getdata (needscn, NULL);
    3254         [ +  - ]:         79 :           if (unlikely (needdata == NULL))
    3255                 :          0 :             return;
    3256                 :            : 
    3257                 :         79 :           needshdr = gelf_getshdr (needscn, &needshdrmem);
    3258         [ +  - ]:         79 :           if (unlikely (needshdr == NULL))
    3259                 :            :             return;
    3260                 :            : 
    3261         [ +  - ]:        123 :           for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
    3262                 :            :             {
    3263                 :        123 :               GElf_Verneed needmem;
    3264                 :        123 :               GElf_Verneed *need;
    3265                 :        123 :               unsigned int auxoffset;
    3266                 :        123 :               int cnt2;
    3267                 :            : 
    3268                 :            :               /* Get the data at the next offset.  */
    3269                 :        123 :               need = gelf_getverneed (needdata, offset, &needmem);
    3270         [ +  - ]:        123 :               if (unlikely (need == NULL))
    3271                 :            :                 break;
    3272                 :            : 
    3273                 :            :               /* Run through the auxiliary entries.  */
    3274                 :        123 :               auxoffset = offset + need->vn_aux;
    3275         [ +  - ]:        178 :               for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
    3276                 :            :                 {
    3277                 :        178 :                   GElf_Vernaux auxmem;
    3278                 :        178 :                   GElf_Vernaux *aux;
    3279                 :            : 
    3280                 :        178 :                   aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
    3281         [ +  - ]:        178 :                   if (unlikely (aux == NULL))
    3282                 :            :                     break;
    3283                 :            : 
    3284                 :        178 :                   nvername = MAX (nvername,
    3285                 :            :                                   (size_t) (aux->vna_other & 0x7fff));
    3286                 :            : 
    3287         [ +  + ]:        178 :                   if (aux->vna_next == 0)
    3288                 :            :                     break;
    3289                 :         55 :                   auxoffset += aux->vna_next;
    3290                 :            :                 }
    3291                 :            : 
    3292         [ +  + ]:        123 :               if (need->vn_next == 0)
    3293                 :            :                 break;
    3294                 :         44 :               offset += need->vn_next;
    3295                 :            :             }
    3296                 :            :         }
    3297                 :            : 
    3298                 :            :       /* This is the number of versions we know about.  */
    3299                 :         79 :       ++nvername;
    3300                 :            : 
    3301                 :            :       /* Allocate the array.  */
    3302                 :         79 :       vername = (const char **) alloca (nvername * sizeof (const char *));
    3303         [ +  + ]:         79 :       memset(vername, 0, nvername * sizeof (const char *));
    3304                 :         79 :       filename = (const char **) alloca (nvername * sizeof (const char *));
    3305                 :         79 :       memset(filename, 0, nvername * sizeof (const char *));
    3306                 :            : 
    3307                 :            :       /* Run through the data structures again and collect the strings.  */
    3308         [ +  + ]:         79 :       if (defscn != NULL)
    3309                 :            :         {
    3310                 :            :           /* Run through the version definitions and find the highest
    3311                 :            :              index.  */
    3312                 :          2 :           unsigned int offset = 0;
    3313                 :          2 :           Elf_Data *defdata;
    3314                 :          2 :           GElf_Shdr defshdrmem;
    3315                 :          2 :           GElf_Shdr *defshdr;
    3316                 :            : 
    3317                 :          2 :           defdata = elf_getdata (defscn, NULL);
    3318         [ +  - ]:          2 :           if (unlikely (defdata == NULL))
    3319                 :          0 :             return;
    3320                 :            : 
    3321                 :          2 :           defshdr = gelf_getshdr (defscn, &defshdrmem);
    3322         [ +  - ]:          2 :           if (unlikely (defshdr == NULL))
    3323                 :            :             return;
    3324                 :            : 
    3325         [ +  - ]:         12 :           for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
    3326                 :            :             {
    3327                 :            : 
    3328                 :            :               /* Get the data at the next offset.  */
    3329                 :         12 :               GElf_Verdef defmem;
    3330                 :         12 :               GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
    3331         [ +  - ]:         12 :               if (unlikely (def == NULL))
    3332                 :            :                 break;
    3333                 :            : 
    3334                 :         12 :               GElf_Verdaux auxmem;
    3335                 :         24 :               GElf_Verdaux *aux = gelf_getverdaux (defdata,
    3336                 :         12 :                                                    offset + def->vd_aux,
    3337                 :            :                                                    &auxmem);
    3338         [ +  - ]:         12 :               if (unlikely (aux == NULL))
    3339                 :            :                 break;
    3340                 :            : 
    3341                 :         12 :               vername[def->vd_ndx & 0x7fff]
    3342                 :         12 :                 = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
    3343                 :         12 :               filename[def->vd_ndx & 0x7fff] = NULL;
    3344                 :            : 
    3345         [ +  + ]:         12 :               if (def->vd_next == 0)
    3346                 :            :                 break;
    3347                 :         10 :               offset += def->vd_next;
    3348                 :            :             }
    3349                 :            :         }
    3350         [ +  - ]:         79 :       if (needscn != NULL)
    3351                 :            :         {
    3352                 :         79 :           unsigned int offset = 0;
    3353                 :            : 
    3354                 :         79 :           Elf_Data *needdata = elf_getdata (needscn, NULL);
    3355                 :         79 :           GElf_Shdr needshdrmem;
    3356                 :         79 :           GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
    3357         [ -  + ]:         79 :           if (unlikely (needdata == NULL || needshdr == NULL))
    3358                 :          0 :             return;
    3359                 :            : 
    3360         [ +  - ]:        123 :           for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
    3361                 :            :             {
    3362                 :            :               /* Get the data at the next offset.  */
    3363                 :        123 :               GElf_Verneed needmem;
    3364                 :        123 :               GElf_Verneed *need = gelf_getverneed (needdata, offset,
    3365                 :            :                                                     &needmem);
    3366         [ +  - ]:        123 :               if (unlikely (need == NULL))
    3367                 :            :                 break;
    3368                 :            : 
    3369                 :            :               /* Run through the auxiliary entries.  */
    3370                 :        123 :               unsigned int auxoffset = offset + need->vn_aux;
    3371         [ +  - ]:        178 :               for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
    3372                 :            :                 {
    3373                 :        178 :                   GElf_Vernaux auxmem;
    3374                 :        178 :                   GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
    3375                 :            :                                                        &auxmem);
    3376         [ +  - ]:        178 :                   if (unlikely (aux == NULL))
    3377                 :            :                     break;
    3378                 :            : 
    3379                 :        178 :                   vername[aux->vna_other & 0x7fff]
    3380                 :        178 :                     = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
    3381                 :        178 :                   filename[aux->vna_other & 0x7fff]
    3382                 :        178 :                     = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
    3383                 :            : 
    3384         [ +  + ]:        178 :                   if (aux->vna_next == 0)
    3385                 :            :                     break;
    3386                 :         55 :                   auxoffset += aux->vna_next;
    3387                 :            :                 }
    3388                 :            : 
    3389         [ +  + ]:        123 :               if (need->vn_next == 0)
    3390                 :            :                 break;
    3391                 :         44 :               offset += need->vn_next;
    3392                 :            :             }
    3393                 :            :         }
    3394                 :            :     }
    3395                 :            :   else
    3396                 :            :     {
    3397                 :            :       vername = NULL;
    3398                 :            :       nvername = 1;
    3399                 :            :       filename = NULL;
    3400                 :            :     }
    3401                 :            : 
    3402                 :         79 :   GElf_Shdr glink_mem;
    3403                 :         79 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    3404                 :            :                                    &glink_mem);
    3405                 :         79 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
    3406         [ -  + ]:         79 :   if (glink == NULL)
    3407                 :          0 :     error_exit (0, _("invalid sh_link value in section %zu"),
    3408                 :            :                 elf_ndxscn (scn));
    3409                 :            : 
    3410                 :            :   /* Print the header.  */
    3411                 :         79 :   printf (ngettext ("\
    3412                 :            : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
    3413                 :            :                     "\
    3414                 :            : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
    3415                 :            :                     shdr->sh_size / sh_entsize),
    3416                 :         79 :           (unsigned int) elf_ndxscn (scn),
    3417                 :         79 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3418                 :         79 :           (int) (shdr->sh_size / sh_entsize),
    3419                 :            :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    3420                 :            :           shdr->sh_offset,
    3421         [ +  + ]:         79 :           (unsigned int) shdr->sh_link,
    3422                 :         79 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3423                 :            : 
    3424                 :            :   /* Now we can finally look at the actual contents of this section.  */
    3425         [ +  + ]:       1911 :   for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
    3426                 :            :     {
    3427         [ +  + ]:       1832 :       if (cnt % 2 == 0)
    3428                 :        926 :         printf ("\n %4d:", cnt);
    3429                 :            : 
    3430                 :       1832 :       GElf_Versym symmem;
    3431                 :       1832 :       GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
    3432         [ +  - ]:       1832 :       if (sym == NULL)
    3433                 :            :         break;
    3434                 :            : 
    3435      [ +  +  + ]:       1832 :       switch (*sym)
    3436                 :            :         {
    3437                 :        412 :           ssize_t n;
    3438                 :        412 :         case 0:
    3439                 :        412 :           fputs_unlocked (_("   0 *local*                     "),
    3440                 :            :                           stdout);
    3441                 :        412 :           break;
    3442                 :            : 
    3443                 :         22 :         case 1:
    3444                 :         22 :           fputs_unlocked (_("   1 *global*                    "),
    3445                 :            :                           stdout);
    3446                 :         22 :           break;
    3447                 :            : 
    3448                 :       1398 :         default:
    3449         [ +  - ]:       2796 :           n = printf ("%4d%c%s",
    3450         [ +  - ]:       1398 :                       *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
    3451                 :            :                       (vername != NULL
    3452         [ +  - ]:       1398 :                        && (unsigned int) (*sym & 0x7fff) < nvername)
    3453                 :       1398 :                       ? vername[*sym & 0x7fff] : "???");
    3454         [ +  - ]:       1398 :           if ((unsigned int) (*sym & 0x7fff) < nvername
    3455   [ +  -  +  + ]:       1398 :               && filename != NULL && filename[*sym & 0x7fff] != NULL)
    3456                 :       1246 :             n += printf ("(%s)", filename[*sym & 0x7fff]);
    3457                 :       1832 :           printf ("%*s", MAX (0, 33 - (int) n), " ");
    3458                 :            :           break;
    3459                 :            :         }
    3460                 :            :     }
    3461         [ -  + ]:        158 :   putchar_unlocked ('\n');
    3462                 :            : }
    3463                 :            : 
    3464                 :            : 
    3465                 :            : static void
    3466                 :         79 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
    3467                 :            :                  uint_fast32_t maxlength, Elf32_Word nbucket,
    3468                 :            :                  uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
    3469                 :            : {
    3470                 :         79 :   uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
    3471                 :            : 
    3472         [ +  + ]:        309 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3473                 :        230 :     ++counts[lengths[cnt]];
    3474                 :            : 
    3475                 :         79 :   GElf_Shdr glink_mem;
    3476                 :         79 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
    3477                 :         79 :                                                shdr->sh_link),
    3478                 :            :                                    &glink_mem);
    3479         [ -  + ]:         79 :   if (glink == NULL)
    3480                 :            :     {
    3481                 :          0 :       error (0, 0, _("invalid sh_link value in section %zu"),
    3482                 :            :              elf_ndxscn (scn));
    3483                 :          0 :       return;
    3484                 :            :     }
    3485                 :            : 
    3486         [ +  + ]:        158 :   printf (ngettext ("\
    3487                 :            : \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",
    3488                 :            :                     "\
    3489                 :            : \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",
    3490                 :            :                     nbucket),
    3491                 :         79 :           (unsigned int) elf_ndxscn (scn),
    3492                 :         79 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3493                 :            :           (int) nbucket,
    3494                 :         79 :           gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
    3495                 :            :           shdr->sh_addr,
    3496                 :            :           shdr->sh_offset,
    3497                 :         79 :           (unsigned int) shdr->sh_link,
    3498                 :         79 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3499                 :            : 
    3500         [ +  - ]:         79 :   if (extrastr != NULL)
    3501                 :         79 :     fputs (extrastr, stdout);
    3502                 :            : 
    3503         [ +  - ]:         79 :   if (likely (nbucket > 0))
    3504                 :            :     {
    3505                 :         79 :       uint64_t success = 0;
    3506                 :            : 
    3507                 :            :       /* xgettext:no-c-format */
    3508                 :         79 :       fputs_unlocked (_("\
    3509                 :            :  Length  Number  % of total  Coverage\n"), stdout);
    3510                 :         79 :       printf (_("      0  %6" PRIu32 "      %5.1f%%\n"),
    3511                 :         79 :               counts[0], (counts[0] * 100.0) / nbucket);
    3512                 :            : 
    3513                 :         79 :       uint64_t nzero_counts = 0;
    3514         [ +  + ]:        138 :       for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
    3515                 :            :         {
    3516                 :         59 :           nzero_counts += counts[cnt] * cnt;
    3517                 :         59 :           printf (_("\
    3518                 :            : %7d  %6" PRIu32 "      %5.1f%%    %5.1f%%\n"),
    3519                 :         59 :                   (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
    3520                 :         59 :                   (nzero_counts * 100.0) / nsyms);
    3521                 :            :         }
    3522                 :            : 
    3523                 :            :       Elf32_Word acc = 0;
    3524         [ +  + ]:        138 :       for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
    3525                 :            :         {
    3526                 :         59 :           acc += cnt;
    3527                 :         59 :           success += counts[cnt] * acc;
    3528                 :            :         }
    3529                 :            : 
    3530                 :        158 :       printf (_("\
    3531                 :            :  Average number of tests:   successful lookup: %f\n\
    3532                 :            :                           unsuccessful lookup: %f\n"),
    3533                 :         79 :               (double) success / (double) nzero_counts,
    3534                 :         79 :               (double) nzero_counts / (double) nbucket);
    3535                 :            :     }
    3536                 :            : 
    3537                 :         79 :   free (counts);
    3538                 :            : }
    3539                 :            : 
    3540                 :            : 
    3541                 :            : /* This function handles the traditional System V-style hash table format.  */
    3542                 :            : static void
    3543                 :          0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3544                 :            : {
    3545                 :          0 :   Elf_Data *data = elf_getdata (scn, NULL);
    3546         [ #  # ]:          0 :   if (unlikely (data == NULL))
    3547                 :            :     {
    3548                 :          0 :       error (0, 0, _("cannot get data for section %d: %s"),
    3549                 :          0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3550                 :          0 :       return;
    3551                 :            :     }
    3552                 :            : 
    3553         [ #  # ]:          0 :   if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
    3554                 :            :     {
    3555                 :          0 :     invalid_data:
    3556                 :          0 :       error (0, 0, _("invalid data in sysv.hash section %d"),
    3557                 :          0 :              (int) elf_ndxscn (scn));
    3558                 :          0 :       return;
    3559                 :            :     }
    3560                 :            : 
    3561                 :          0 :   Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
    3562                 :          0 :   Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
    3563                 :            : 
    3564                 :          0 :   uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
    3565         [ #  # ]:          0 :   if (used_buf > data->d_size)
    3566                 :          0 :     goto invalid_data;
    3567                 :            : 
    3568                 :          0 :   Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
    3569                 :          0 :   Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
    3570                 :            : 
    3571                 :          0 :   uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
    3572                 :            : 
    3573                 :          0 :   uint_fast32_t maxlength = 0;
    3574                 :          0 :   uint_fast32_t nsyms = 0;
    3575         [ #  # ]:          0 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3576                 :            :     {
    3577                 :          0 :       Elf32_Word inner = bucket[cnt];
    3578                 :          0 :       Elf32_Word chain_len = 0;
    3579         [ #  # ]:          0 :       while (inner > 0 && inner < nchain)
    3580                 :            :         {
    3581                 :          0 :           ++nsyms;
    3582                 :          0 :           ++chain_len;
    3583         [ #  # ]:          0 :           if (chain_len > nchain)
    3584                 :            :             {
    3585                 :          0 :               error (0, 0, _("invalid chain in sysv.hash section %d"),
    3586                 :          0 :                      (int) elf_ndxscn (scn));
    3587                 :          0 :               free (lengths);
    3588                 :          0 :               return;
    3589                 :            :             }
    3590         [ #  # ]:          0 :           if (maxlength < ++lengths[cnt])
    3591                 :          0 :             ++maxlength;
    3592                 :            : 
    3593                 :          0 :           inner = chain[inner];
    3594                 :            :         }
    3595                 :            :     }
    3596                 :            : 
    3597                 :          0 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    3598                 :            :                    lengths, NULL);
    3599                 :            : 
    3600                 :          0 :   free (lengths);
    3601                 :            : }
    3602                 :            : 
    3603                 :            : 
    3604                 :            : /* This function handles the incorrect, System V-style hash table
    3605                 :            :    format some 64-bit architectures use.  */
    3606                 :            : static void
    3607                 :          0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3608                 :            : {
    3609                 :          0 :   Elf_Data *data = elf_getdata (scn, NULL);
    3610         [ #  # ]:          0 :   if (unlikely (data == NULL))
    3611                 :            :     {
    3612                 :          0 :       error (0, 0, _("cannot get data for section %d: %s"),
    3613                 :          0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3614                 :          0 :       return;
    3615                 :            :     }
    3616                 :            : 
    3617         [ #  # ]:          0 :   if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
    3618                 :            :     {
    3619                 :          0 :     invalid_data:
    3620                 :          0 :       error (0, 0, _("invalid data in sysv.hash64 section %d"),
    3621                 :          0 :              (int) elf_ndxscn (scn));
    3622                 :          0 :       return;
    3623                 :            :     }
    3624                 :            : 
    3625                 :          0 :   Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
    3626                 :          0 :   Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
    3627                 :            : 
    3628                 :          0 :   uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
    3629         [ #  # ]:          0 :   if (maxwords < 2
    3630         [ #  # ]:          0 :       || maxwords - 2 < nbucket
    3631         [ #  # ]:          0 :       || maxwords - 2 - nbucket < nchain)
    3632                 :          0 :     goto invalid_data;
    3633                 :            : 
    3634                 :          0 :   Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
    3635                 :          0 :   Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
    3636                 :            : 
    3637                 :          0 :   uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
    3638                 :            : 
    3639                 :          0 :   uint_fast32_t maxlength = 0;
    3640                 :          0 :   uint_fast32_t nsyms = 0;
    3641         [ #  # ]:          0 :   for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
    3642                 :            :     {
    3643                 :          0 :       Elf64_Xword inner = bucket[cnt];
    3644                 :          0 :       Elf64_Xword chain_len = 0;
    3645         [ #  # ]:          0 :       while (inner > 0 && inner < nchain)
    3646                 :            :         {
    3647                 :          0 :           ++nsyms;
    3648                 :          0 :           ++chain_len;
    3649         [ #  # ]:          0 :           if (chain_len > nchain)
    3650                 :            :             {
    3651                 :          0 :               error (0, 0, _("invalid chain in sysv.hash64 section %d"),
    3652                 :          0 :                      (int) elf_ndxscn (scn));
    3653                 :          0 :               free (lengths);
    3654                 :          0 :               return;
    3655                 :            :             }
    3656         [ #  # ]:          0 :           if (maxlength < ++lengths[cnt])
    3657                 :          0 :             ++maxlength;
    3658                 :            : 
    3659                 :          0 :           inner = chain[inner];
    3660                 :            :         }
    3661                 :            :     }
    3662                 :            : 
    3663                 :          0 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    3664                 :            :                    lengths, NULL);
    3665                 :            : 
    3666                 :          0 :   free (lengths);
    3667                 :            : }
    3668                 :            : 
    3669                 :            : 
    3670                 :            : /* This function handles the GNU-style hash table format.  */
    3671                 :            : static void
    3672                 :         79 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3673                 :            : {
    3674                 :         79 :   uint32_t *lengths = NULL;
    3675                 :         79 :   Elf_Data *data = elf_getdata (scn, NULL);
    3676         [ -  + ]:         79 :   if (unlikely (data == NULL))
    3677                 :            :     {
    3678                 :          0 :       error (0, 0, _("cannot get data for section %d: %s"),
    3679                 :          0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3680                 :          0 :       return;
    3681                 :            :     }
    3682                 :            : 
    3683         [ -  + ]:         79 :   if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
    3684                 :            :     {
    3685                 :          0 :     invalid_data:
    3686                 :          0 :       free (lengths);
    3687                 :          0 :       error (0, 0, _("invalid data in gnu.hash section %d"),
    3688                 :          0 :              (int) elf_ndxscn (scn));
    3689                 :          0 :       return;
    3690                 :            :     }
    3691                 :            : 
    3692                 :         79 :   Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
    3693                 :         79 :   Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
    3694                 :            : 
    3695                 :            :   /* Next comes the size of the bitmap.  It's measured in words for
    3696                 :            :      the architecture.  It's 32 bits for 32 bit archs, and 64 bits for
    3697                 :            :      64 bit archs.  There is always a bloom filter present, so zero is
    3698                 :            :      an invalid value.  */
    3699                 :         79 :   Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
    3700         [ +  + ]:         79 :   if (gelf_getclass (ebl->elf) == ELFCLASS64)
    3701                 :         69 :     bitmask_words *= 2;
    3702                 :            : 
    3703         [ -  + ]:         79 :   if (bitmask_words == 0)
    3704                 :          0 :     goto invalid_data;
    3705                 :            : 
    3706                 :         79 :   Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
    3707                 :            : 
    3708                 :            :   /* Is there still room for the sym chain?
    3709                 :            :      Use uint64_t calculation to prevent 32bit overflow.  */
    3710                 :         79 :   uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
    3711                 :         79 :   uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
    3712         [ -  + ]:         79 :   if (used_buf > data->d_size)
    3713                 :          0 :     goto invalid_data;
    3714                 :            : 
    3715                 :         79 :   lengths = xcalloc (nbucket, sizeof (uint32_t));
    3716                 :            : 
    3717                 :         79 :   Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
    3718                 :         79 :   Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
    3719                 :         79 :   Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
    3720                 :         79 :                                                     + nbucket];
    3721                 :            : 
    3722                 :            :   /* Compute distribution of chain lengths.  */
    3723                 :         79 :   uint_fast32_t maxlength = 0;
    3724                 :         79 :   uint_fast32_t nsyms = 0;
    3725         [ +  + ]:        309 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3726         [ +  + ]:        230 :     if (bucket[cnt] != 0)
    3727                 :            :       {
    3728                 :        122 :         Elf32_Word inner = bucket[cnt] - symbias;
    3729                 :        223 :         do
    3730                 :            :           {
    3731                 :        223 :             ++nsyms;
    3732         [ +  + ]:        223 :             if (maxlength < ++lengths[cnt])
    3733                 :         59 :               ++maxlength;
    3734         [ -  + ]:        223 :             if (inner >= max_nsyms)
    3735                 :          0 :               goto invalid_data;
    3736                 :            :           }
    3737         [ +  + ]:        223 :         while ((chain[inner++] & 1) == 0);
    3738                 :            :       }
    3739                 :            : 
    3740                 :            :   /* Count bits in bitmask.  */
    3741                 :            :   uint_fast32_t nbits = 0;
    3742         [ +  + ]:        263 :   for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
    3743                 :            :     {
    3744                 :        184 :       uint_fast32_t word = bitmask[cnt];
    3745                 :            : 
    3746                 :        184 :       word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
    3747                 :        184 :       word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
    3748                 :        184 :       word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
    3749                 :        184 :       word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
    3750                 :        184 :       nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
    3751                 :            :     }
    3752                 :            : 
    3753                 :        158 :   char *str = xasprintf (_("\
    3754                 :            :  Symbol Bias: %u\n\
    3755                 :            :  Bitmask Size: %zu bytes  %" PRIuFAST32 "%% bits set  2nd hash shift: %u\n"),
    3756                 :            :                          (unsigned int) symbias,
    3757                 :            :                          bitmask_words * sizeof (Elf32_Word),
    3758                 :         79 :                          ((nbits * 100 + 50)
    3759                 :         79 :                           / (uint_fast32_t) (bitmask_words
    3760                 :            :                                               * sizeof (Elf32_Word) * 8)),
    3761                 :            :                           (unsigned int) shift);
    3762                 :            : 
    3763                 :         79 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    3764                 :            :                    lengths, str);
    3765                 :            : 
    3766                 :         79 :   free (str);
    3767                 :         79 :   free (lengths);
    3768                 :            : }
    3769                 :            : 
    3770                 :            : 
    3771                 :            : /* Find the symbol table(s).  For this we have to search through the
    3772                 :            :    section table.  */
    3773                 :            : static void
    3774                 :        142 : handle_hash (Ebl *ebl)
    3775                 :            : {
    3776                 :            :   /* Get the section header string table index.  */
    3777                 :        142 :   size_t shstrndx;
    3778         [ -  + ]:        142 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3779                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    3780                 :            : 
    3781                 :            :   Elf_Scn *scn = NULL;
    3782         [ +  + ]:       4198 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    3783                 :            :     {
    3784                 :            :       /* Handle the section if it is a symbol table.  */
    3785                 :       4056 :       GElf_Shdr shdr_mem;
    3786                 :       4056 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    3787                 :            : 
    3788         [ +  - ]:       4056 :       if (likely (shdr != NULL))
    3789                 :            :         {
    3790         [ +  + ]:       4056 :           if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
    3791         [ -  + ]:         79 :               && (shdr->sh_flags & SHF_COMPRESSED) != 0)
    3792                 :            :             {
    3793         [ #  # ]:          0 :               if (elf_compress (scn, 0, 0) < 0)
    3794                 :          0 :                 printf ("WARNING: %s [%zd]\n",
    3795                 :            :                         _("Couldn't uncompress section"),
    3796                 :            :                         elf_ndxscn (scn));
    3797                 :          0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    3798         [ #  # ]:          0 :               if (unlikely (shdr == NULL))
    3799                 :          0 :                 error_exit (0, _("cannot get section [%zd] header: %s"),
    3800                 :            :                             elf_ndxscn (scn), elf_errmsg (-1));
    3801                 :            :             }
    3802                 :            : 
    3803         [ -  + ]:       4056 :           if (shdr->sh_type == SHT_HASH)
    3804                 :            :             {
    3805         [ #  # ]:          0 :               if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
    3806                 :          0 :                 handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
    3807                 :            :               else
    3808                 :          0 :                 handle_sysv_hash (ebl, scn, shdr, shstrndx);
    3809                 :            :             }
    3810         [ +  + ]:       4056 :           else if (shdr->sh_type == SHT_GNU_HASH)
    3811                 :         79 :             handle_gnu_hash (ebl, scn, shdr, shstrndx);
    3812                 :            :         }
    3813                 :            :     }
    3814                 :        142 : }
    3815                 :            : 
    3816                 :            : 
    3817                 :            : static void
    3818                 :        146 : print_liblist (Ebl *ebl)
    3819                 :            : {
    3820                 :            :   /* Find the library list sections.  For this we have to search
    3821                 :            :      through the section table.  */
    3822                 :        146 :   Elf_Scn *scn = NULL;
    3823                 :            : 
    3824                 :            :   /* Get the section header string table index.  */
    3825                 :        146 :   size_t shstrndx;
    3826         [ -  + ]:        146 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3827                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    3828                 :            : 
    3829         [ +  + ]:       4261 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    3830                 :            :     {
    3831                 :       4115 :       GElf_Shdr shdr_mem;
    3832                 :       4115 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    3833                 :            : 
    3834   [ +  -  -  + ]:       4115 :       if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
    3835                 :            :         {
    3836                 :          0 :           size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
    3837                 :          0 :           int nentries = shdr->sh_size / sh_entsize;
    3838                 :          0 :           printf (ngettext ("\
    3839                 :            : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    3840                 :            :                             "\
    3841                 :            : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    3842                 :            :                             nentries),
    3843                 :            :                   elf_ndxscn (scn),
    3844                 :          0 :                   elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3845                 :            :                   shdr->sh_offset,
    3846                 :            :                   nentries);
    3847                 :            : 
    3848                 :          0 :           Elf_Data *data = elf_getdata (scn, NULL);
    3849         [ #  # ]:          0 :           if (data == NULL)
    3850                 :          0 :             return;
    3851                 :            : 
    3852                 :          0 :           puts (_("\
    3853                 :            :        Library                       Time Stamp          Checksum Version Flags"));
    3854                 :            : 
    3855         [ #  # ]:          0 :           for (int cnt = 0; cnt < nentries; ++cnt)
    3856                 :            :             {
    3857                 :          0 :               GElf_Lib lib_mem;
    3858                 :          0 :               GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
    3859         [ #  # ]:          0 :               if (unlikely (lib == NULL))
    3860                 :          0 :                 continue;
    3861                 :            : 
    3862                 :          0 :               time_t t = (time_t) lib->l_time_stamp;
    3863                 :          0 :               struct tm *tm = gmtime (&t);
    3864         [ #  # ]:          0 :               if (unlikely (tm == NULL))
    3865                 :          0 :                 continue;
    3866                 :            : 
    3867                 :          0 :               printf ("  [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
    3868                 :          0 :                       cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
    3869                 :          0 :                       tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
    3870                 :            :                       tm->tm_hour, tm->tm_min, tm->tm_sec,
    3871                 :          0 :                       (unsigned int) lib->l_checksum,
    3872                 :          0 :                       (unsigned int) lib->l_version,
    3873                 :          0 :                       (unsigned int) lib->l_flags);
    3874                 :            :             }
    3875                 :            :         }
    3876                 :            :     }
    3877                 :            : }
    3878                 :            : 
    3879                 :            : static inline size_t
    3880                 :         12 : left (Elf_Data *data,
    3881                 :            :       const unsigned char *p)
    3882                 :            : {
    3883                 :         12 :   return (const unsigned char *) data->d_buf + data->d_size - p;
    3884                 :            : }
    3885                 :            : 
    3886                 :            : static void
    3887                 :        146 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
    3888                 :            : {
    3889                 :            :   /* Find the object attributes sections.  For this we have to search
    3890                 :            :      through the section table.  */
    3891                 :        146 :   Elf_Scn *scn = NULL;
    3892                 :            : 
    3893                 :            :   /* Get the section header string table index.  */
    3894                 :        146 :   size_t shstrndx;
    3895         [ -  + ]:        146 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3896                 :          0 :     error_exit (0, _("cannot get section header string table index"));
    3897                 :            : 
    3898         [ +  + ]:       4261 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    3899                 :            :     {
    3900                 :       4115 :       GElf_Shdr shdr_mem;
    3901                 :       4115 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    3902                 :            : 
    3903   [ +  -  +  + ]:       4115 :       if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
    3904         [ +  + ]:       4112 :                            && (shdr->sh_type != SHT_ARM_ATTRIBUTES
    3905         [ -  + ]:          1 :                                || ehdr->e_machine != EM_ARM)
    3906         [ +  + ]:       4111 :                            && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
    3907         [ +  - ]:          1 :                                || ehdr->e_machine != EM_CSKY)
    3908         [ -  + ]:       4111 :                            && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
    3909         [ #  # ]:          0 :                                || ehdr->e_machine != EM_RISCV)))
    3910                 :       4111 :         continue;
    3911                 :            : 
    3912                 :          4 :       printf (_("\
    3913                 :            : \nObject attributes section [%2zu] '%s' of %" PRIu64
    3914                 :            :                        " bytes at offset %#0" PRIx64 ":\n"),
    3915                 :            :               elf_ndxscn (scn),
    3916                 :          4 :               elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3917                 :            :               shdr->sh_size, shdr->sh_offset);
    3918                 :            : 
    3919                 :          4 :       Elf_Data *data = elf_rawdata (scn, NULL);
    3920   [ +  -  +  - ]:          4 :       if (unlikely (data == NULL || data->d_size == 0))
    3921                 :          0 :         return;
    3922                 :            : 
    3923                 :          4 :       const unsigned char *p = data->d_buf;
    3924                 :            : 
    3925                 :            :       /* There is only one 'version', A.  */
    3926         [ +  - ]:          4 :       if (unlikely (*p++ != 'A'))
    3927                 :            :         return;
    3928                 :            : 
    3929                 :          4 :       fputs_unlocked (_("  Owner          Size\n"), stdout);
    3930                 :            : 
    3931                 :            :       /* Loop over the sections.  */
    3932         [ +  + ]:          8 :       while (left (data, p) >= 4)
    3933                 :            :         {
    3934                 :            :           /* Section length.  */
    3935                 :          4 :           uint32_t len;
    3936         [ +  + ]:          4 :           memcpy (&len, p, sizeof len);
    3937                 :            : 
    3938         [ +  + ]:          4 :           if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
    3939                 :          2 :             CONVERT (len);
    3940                 :            : 
    3941         [ +  - ]:          4 :           if (unlikely (len > left (data, p)))
    3942                 :            :             break;
    3943                 :            : 
    3944                 :            :           /* Section vendor name.  */
    3945                 :          4 :           const unsigned char *name = p + sizeof len;
    3946                 :          4 :           p += len;
    3947                 :            : 
    3948                 :          4 :           unsigned const char *q = memchr (name, '\0', len);
    3949         [ +  - ]:          4 :           if (unlikely (q == NULL))
    3950                 :            :             break;
    3951                 :          4 :           ++q;
    3952                 :            : 
    3953                 :          4 :           printf (_("  %-13s  %4" PRIu32 "\n"), name, len);
    3954                 :            : 
    3955                 :          8 :           bool gnu_vendor = (q - name == sizeof "gnu"
    3956   [ +  +  -  + ]:          4 :                              && !memcmp (name, "gnu", sizeof "gnu"));
    3957                 :            : 
    3958                 :            :           /* Loop over subsections.  */
    3959         [ +  + ]:          4 :           if (shdr->sh_type != SHT_GNU_ATTRIBUTES
    3960         [ +  - ]:          3 :               || gnu_vendor)
    3961         [ +  + ]:          8 :             while (q < p)
    3962                 :            :               {
    3963                 :          4 :                 const unsigned char *const sub = q;
    3964                 :            : 
    3965                 :          4 :                 unsigned int subsection_tag;
    3966                 :          4 :                 get_uleb128 (subsection_tag, q, p);
    3967         [ +  - ]:          4 :                 if (unlikely (q >= p))
    3968                 :            :                   break;
    3969                 :            : 
    3970                 :          4 :                 uint32_t subsection_len;
    3971         [ +  - ]:          4 :                 if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
    3972                 :            :                   break;
    3973                 :            : 
    3974         [ +  + ]:          4 :                 memcpy (&subsection_len, q, sizeof subsection_len);
    3975                 :            : 
    3976         [ +  + ]:          4 :                 if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
    3977                 :          2 :                   CONVERT (subsection_len);
    3978                 :            : 
    3979                 :            :                 /* Don't overflow, ptrdiff_t might be 32bits, but signed.  */
    3980   [ +  -  +  - ]:          4 :                 if (unlikely (subsection_len == 0
    3981                 :            :                               || subsection_len >= (uint32_t) PTRDIFF_MAX
    3982                 :            :                               || p - sub < (ptrdiff_t) subsection_len))
    3983                 :            :                   break;
    3984                 :            : 
    3985                 :          4 :                 const unsigned char *r = q + sizeof subsection_len;
    3986                 :          4 :                 q = sub + subsection_len;
    3987                 :            : 
    3988         [ -  + ]:          4 :                 switch (subsection_tag)
    3989                 :            :                   {
    3990                 :          0 :                   default:
    3991                 :            :                     /* Unknown subsection, print and skip.  */
    3992                 :          4 :                     printf (_("    %-4u %12" PRIu32 "\n"),
    3993                 :            :                             subsection_tag, subsection_len);
    3994                 :            :                     break;
    3995                 :            : 
    3996                 :          4 :                   case 1:       /* Tag_File */
    3997                 :          4 :                     printf (_("    File: %11" PRIu32 "\n"),
    3998                 :            :                             subsection_len);
    3999                 :            : 
    4000         [ +  + ]:         26 :                     while (r < q)
    4001                 :            :                       {
    4002                 :         22 :                         unsigned int tag;
    4003                 :         22 :                         get_uleb128 (tag, r, q);
    4004         [ +  - ]:         22 :                         if (unlikely (r >= q))
    4005                 :            :                           break;
    4006                 :            : 
    4007                 :            :                         /* GNU style tags have either a uleb128 value,
    4008                 :            :                            when lowest bit is not set, or a string
    4009                 :            :                            when the lowest bit is set.
    4010                 :            :                            "compatibility" (32) is special.  It has
    4011                 :            :                            both a string and a uleb128 value.  For
    4012                 :            :                            non-gnu we assume 6 till 31 only take ints.
    4013                 :            :                            XXX see arm backend, do we need a separate
    4014                 :            :                            hook?  */
    4015                 :         22 :                         uint64_t value = 0;
    4016                 :         22 :                         const char *string = NULL;
    4017   [ +  -  +  + ]:         22 :                         if (tag == 32 || (tag & 1) == 0
    4018   [ +  -  +  + ]:          8 :                             || (! gnu_vendor && (tag > 5 && tag < 32)))
    4019                 :            :                           {
    4020                 :            :                             // Note r >= q check above.
    4021                 :         21 :                             get_uleb128 (value, r, q);
    4022         [ +  - ]:         21 :                             if (r > q)
    4023                 :            :                               break;
    4024                 :            :                           }
    4025         [ +  - ]:         22 :                         if (tag == 32
    4026         [ +  + ]:         22 :                             || ((tag & 1) != 0
    4027         [ +  - ]:          8 :                                 && (gnu_vendor
    4028         [ +  - ]:          8 :                                     || (! gnu_vendor && tag > 32)))
    4029   [ +  +  +  + ]:         22 :                             || (! gnu_vendor && tag > 3 && tag < 6))
    4030                 :            :                           {
    4031                 :          1 :                             string = (const char *) r;
    4032                 :          1 :                             r = memchr (r, '\0', q - r);
    4033         [ +  - ]:          1 :                             if (r == NULL)
    4034                 :            :                               break;
    4035                 :          1 :                             ++r;
    4036                 :            :                           }
    4037                 :            : 
    4038                 :         22 :                         const char *tag_name = NULL;
    4039                 :         22 :                         const char *value_name = NULL;
    4040                 :         22 :                         ebl_check_object_attribute (ebl, (const char *) name,
    4041                 :            :                                                     tag, value,
    4042                 :            :                                                     &tag_name, &value_name);
    4043                 :            : 
    4044         [ +  - ]:         22 :                         if (tag_name != NULL)
    4045                 :            :                           {
    4046         [ -  + ]:         22 :                             if (tag == 32)
    4047                 :          0 :                               printf (_("      %s: %" PRId64 ", %s\n"),
    4048                 :            :                                       tag_name, value, string);
    4049   [ +  +  +  + ]:         22 :                             else if (string == NULL && value_name == NULL)
    4050                 :          1 :                               printf (_("      %s: %" PRId64 "\n"),
    4051                 :            :                                       tag_name, value);
    4052                 :            :                             else
    4053         [ +  + ]:         21 :                               printf (_("      %s: %s\n"),
    4054                 :            :                                       tag_name, string ?: value_name);
    4055                 :            :                           }
    4056                 :            :                         else
    4057                 :            :                           {
    4058                 :            :                             /* For "gnu" vendor 32 "compatibility" has
    4059                 :            :                                already been handled above.  */
    4060   [ #  #  #  # ]:          0 :                             assert (tag != 32
    4061                 :            :                                     || strcmp ((const char *) name, "gnu"));
    4062         [ #  # ]:          0 :                             if (string == NULL)
    4063                 :          0 :                               printf (_("      %u: %" PRId64 "\n"),
    4064                 :            :                                       tag, value);
    4065                 :            :                             else
    4066                 :         22 :                               printf (_("      %u: %s\n"),
    4067                 :            :                                       tag, string);
    4068                 :            :                           }
    4069                 :            :                       }
    4070                 :            :                   }
    4071                 :            :               }
    4072                 :            :         }
    4073                 :            :     }
    4074                 :            : }
    4075                 :            : 
    4076                 :            : /* Returns either the (relocated) data from the Dwarf, or tries to get
    4077                 :            :    the "raw" (uncompressed) data from the Elf section. Produces a
    4078                 :            :    warning if the data cannot be found (or decompressed).  */
    4079                 :            : static Elf_Data *
    4080                 :        371 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
    4081                 :            : {
    4082                 :            :   /* We prefer to get the section data from the Dwarf because that
    4083                 :            :      might have been relocated already.  Note this is subtly wrong if
    4084                 :            :      there are multiple sections with the same .debug name.  */
    4085         [ -  + ]:        371 :   if (dbg->sectiondata[idx] != NULL)
    4086                 :            :     return dbg->sectiondata[idx];
    4087                 :            : 
    4088                 :          0 :   GElf_Shdr shdr_mem;
    4089                 :          0 :   GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    4090   [ #  #  #  # ]:          0 :   if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
    4091                 :            :     {
    4092         [ #  # ]:          0 :       if (elf_compress (scn, 0, 0) < 0)
    4093                 :            :         {
    4094                 :          0 :           error (0, 0, "%s [%zd] '%s'\n",
    4095                 :            :                  _("Couldn't uncompress section"),
    4096                 :            :                  elf_ndxscn (scn), section_name (ebl, shdr));
    4097                 :          0 :           return NULL;
    4098                 :            :         }
    4099                 :            :     }
    4100                 :            : 
    4101                 :          0 :   Elf_Data *data = elf_getdata (scn, NULL);
    4102         [ #  # ]:          0 :   if (data == NULL)
    4103                 :          0 :     error (0, 0, "%s [%zd] '%s': %s\n",
    4104                 :            :            _("Couldn't get data from section"),
    4105                 :            :            elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
    4106                 :            : 
    4107                 :          0 :   return elf_getdata (scn, NULL);
    4108                 :            : }
    4109                 :            : 
    4110                 :            : void
    4111                 :    1037268 : print_dwarf_addr (Dwfl_Module *dwflmod,
    4112                 :            :                   int address_size, Dwarf_Addr address, Dwarf_Addr raw)
    4113                 :            : {
    4114                 :            :   /* See if there is a name we can give for this address.  */
    4115                 :    1037268 :   GElf_Sym sym;
    4116                 :    1037268 :   GElf_Off off = 0;
    4117         [ +  + ]:    1037238 :   const char *name = (print_address_names && ! print_unresolved_addresses)
    4118                 :    1037156 :     ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
    4119         [ +  + ]:    1037268 :     : NULL;
    4120                 :            : 
    4121                 :    1037268 :   const char *scn;
    4122         [ +  + ]:    1037268 :   if (print_unresolved_addresses)
    4123                 :            :     {
    4124                 :         84 :       address = raw;
    4125                 :         84 :       scn = NULL;
    4126                 :            :     }
    4127                 :            :   else
    4128                 :            :     {
    4129                 :            :       /* Relativize the address.  */
    4130                 :    1037184 :       int n = dwfl_module_relocations (dwflmod);
    4131         [ +  + ]:    1037184 :       int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
    4132                 :            : 
    4133                 :            :       /* In an ET_REL file there is a section name to refer to.  */
    4134                 :    1035772 :       scn = (i < 0 ? NULL
    4135         [ +  - ]:    1035772 :              : dwfl_module_relocation_info (dwflmod, i, NULL));
    4136                 :            :     }
    4137                 :            : 
    4138         [ -  + ]:    1037268 :   if ((name != NULL
    4139                 :    1019101 :        ? (off != 0
    4140                 :            :           ? (scn != NULL
    4141                 :            :              ? (address_size == 0
    4142                 :     351998 :                 ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
    4143                 :            :                           scn, address, name, off)
    4144                 :    1235620 :                 : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
    4145                 :     617810 :                           scn, 2 + address_size * 2, address,
    4146                 :            :                           name, off))
    4147                 :            :              : (address_size == 0
    4148                 :        182 :                 ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
    4149                 :            :                           address, name, off)
    4150                 :       1120 :                 : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
    4151                 :        560 :                           2 + address_size * 2, address,
    4152                 :            :                           name, off)))
    4153                 :            :           : (scn != NULL
    4154                 :            :              ? (address_size == 0
    4155                 :      15690 :                 ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
    4156                 :      64724 :                 : printf ("%s+%#0*" PRIx64 " <%s>",
    4157                 :      32362 :                            scn, 2 + address_size * 2, address, name))
    4158                 :            :              : (address_size == 0
    4159                 :         80 :                 ? printf ("%#" PRIx64 " <%s>", address, name)
    4160                 :        838 :                 : printf ("%#0*" PRIx64 " <%s>",
    4161                 :        419 :                           2 + address_size * 2, address, name))))
    4162                 :            :        : (scn != NULL
    4163                 :            :           ? (address_size == 0
    4164                 :       8024 :              ? printf ("%s+%#" PRIx64, scn, address)
    4165                 :       9888 :              : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
    4166                 :            :           : (address_size == 0
    4167                 :         35 :              ? printf ("%#" PRIx64, address)
    4168   [ +  +  +  +  :    2074316 :              : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
                +  +  + ]
    4169                 :          0 :     error_exit (0, _("sprintf failure"));
    4170                 :    1037268 : }
    4171                 :            : 
    4172                 :            : 
    4173                 :            : static const char *
    4174                 :    1034127 : dwarf_tag_string (unsigned int tag)
    4175                 :            : {
    4176   [ -  +  +  -  :    1034127 :   switch (tag)
          -  -  -  -  +  
          -  +  +  -  -  
          -  -  -  -  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  -  -  -  
          -  -  -  +  -  
          +  -  +  +  +  
          -  -  -  -  -  
          -  +  -  -  +  
          -  -  -  +  -  
          +  +  +  +  -  
          -  -  -  -  +  
          +  +  +  -  +  
          +  +  +  -  -  
                      - ]
    4177                 :            :     {
    4178                 :            : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
    4179                 :    1034127 :       DWARF_ALL_KNOWN_DW_TAG
    4180                 :            : #undef DWARF_ONE_KNOWN_DW_TAG
    4181                 :          0 :     default:
    4182                 :          0 :       return NULL;
    4183                 :            :     }
    4184                 :            : }
    4185                 :            : 
    4186                 :            : 
    4187                 :            : static const char *
    4188                 :    4059368 : dwarf_attr_string (unsigned int attrnum)
    4189                 :            : {
    4190   [ +  -  +  -  :    4059368 :   switch (attrnum)
          -  +  -  +  -  
          -  +  +  +  -  
          -  -  -  +  +  
          -  +  -  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  +  
          -  -  -  +  +  
          -  -  -  +  -  
          +  -  -  +  -  
          -  +  +  +  -  
          -  +  +  -  -  
          -  -  -  +  -  
          +  -  +  -  -  
          +  -  -  +  +  
          +  +  -  -  -  
          -  -  +  +  +  
          +  -  +  -  +  
          -  -  -  -  +  
          +  -  +  -  +  
          +  -  +  +  +  
          +  +  -  -  +  
          -  -  -  +  -  
          +  -  -  -  -  
          +  +  -  +  -  
          -  -  -  +  -  
          -  -  +  +  -  
          +  -  -  -  -  
          +  -  -  -  -  
          -  -  +  +  -  
          -  -  -  -  -  
                   -  + ]
    4191                 :            :     {
    4192                 :            : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
    4193                 :    4059367 :       DWARF_ALL_KNOWN_DW_AT
    4194                 :            : #undef DWARF_ONE_KNOWN_DW_AT
    4195                 :          0 :     default:
    4196                 :          0 :       return NULL;
    4197                 :            :     }
    4198                 :            : }
    4199                 :            : 
    4200                 :            : 
    4201                 :            : static const char *
    4202                 :    4059374 : dwarf_form_string (unsigned int form)
    4203                 :            : {
    4204   [ +  +  +  +  :    4059374 :   switch (form)
          +  -  -  -  -  
          -  +  -  -  +  
          -  +  +  +  +  
          +  +  +  -  +  
          +  -  -  +  -  
          -  +  -  -  +  
          +  +  +  +  +  
          -  +  -  -  -  
             -  +  -  + ]
    4205                 :            :     {
    4206                 :            : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
    4207                 :    4059372 :       DWARF_ALL_KNOWN_DW_FORM
    4208                 :            : #undef DWARF_ONE_KNOWN_DW_FORM
    4209                 :          0 :     default:
    4210                 :          0 :       return NULL;
    4211                 :            :     }
    4212                 :            : }
    4213                 :            : 
    4214                 :            : 
    4215                 :            : static const char *
    4216                 :       1765 : dwarf_lang_string (unsigned int lang)
    4217                 :            : {
    4218   [ +  -  -  +  :       1765 :   switch (lang)
          +  +  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  -  
                      - ]
    4219                 :            :     {
    4220                 :            : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
    4221                 :       1765 :       DWARF_ALL_KNOWN_DW_LANG
    4222                 :            : #undef DWARF_ONE_KNOWN_DW_LANG
    4223                 :          0 :     default:
    4224                 :          0 :       return NULL;
    4225                 :            :     }
    4226                 :            : }
    4227                 :            : 
    4228                 :            : 
    4229                 :            : static const char *
    4230                 :       3503 : dwarf_inline_string (unsigned int code)
    4231                 :            : {
    4232                 :       3503 :   static const char *const known[] =
    4233                 :            :     {
    4234                 :            : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
    4235                 :            :       DWARF_ALL_KNOWN_DW_INL
    4236                 :            : #undef DWARF_ONE_KNOWN_DW_INL
    4237                 :            :     };
    4238                 :            : 
    4239                 :       3503 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4240                 :       3503 :     return known[code];
    4241                 :            : 
    4242                 :            :   return NULL;
    4243                 :            : }
    4244                 :            : 
    4245                 :            : 
    4246                 :            : static const char *
    4247                 :      31342 : dwarf_encoding_string (unsigned int code)
    4248                 :            : {
    4249                 :      31342 :   static const char *const known[] =
    4250                 :            :     {
    4251                 :            : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
    4252                 :            :       DWARF_ALL_KNOWN_DW_ATE
    4253                 :            : #undef DWARF_ONE_KNOWN_DW_ATE
    4254                 :            :     };
    4255                 :            : 
    4256                 :      31342 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4257                 :      31342 :     return known[code];
    4258                 :            : 
    4259                 :            :   return NULL;
    4260                 :            : }
    4261                 :            : 
    4262                 :            : 
    4263                 :            : static const char *
    4264                 :          0 : dwarf_access_string (unsigned int code)
    4265                 :            : {
    4266                 :          0 :   static const char *const known[] =
    4267                 :            :     {
    4268                 :            : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
    4269                 :            :       DWARF_ALL_KNOWN_DW_ACCESS
    4270                 :            : #undef DWARF_ONE_KNOWN_DW_ACCESS
    4271                 :            :     };
    4272                 :            : 
    4273                 :          0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4274                 :          0 :     return known[code];
    4275                 :            : 
    4276                 :            :   return NULL;
    4277                 :            : }
    4278                 :            : 
    4279                 :            : 
    4280                 :            : static const char *
    4281                 :          0 : dwarf_defaulted_string (unsigned int code)
    4282                 :            : {
    4283                 :          0 :   static const char *const known[] =
    4284                 :            :     {
    4285                 :            : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
    4286                 :            :       DWARF_ALL_KNOWN_DW_DEFAULTED
    4287                 :            : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
    4288                 :            :     };
    4289                 :            : 
    4290                 :          0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4291                 :          0 :     return known[code];
    4292                 :            : 
    4293                 :            :   return NULL;
    4294                 :            : }
    4295                 :            : 
    4296                 :            : 
    4297                 :            : static const char *
    4298                 :          0 : dwarf_visibility_string (unsigned int code)
    4299                 :            : {
    4300                 :          0 :   static const char *const known[] =
    4301                 :            :     {
    4302                 :            : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
    4303                 :            :       DWARF_ALL_KNOWN_DW_VIS
    4304                 :            : #undef DWARF_ONE_KNOWN_DW_VIS
    4305                 :            :     };
    4306                 :            : 
    4307                 :          0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4308                 :          0 :     return known[code];
    4309                 :            : 
    4310                 :            :   return NULL;
    4311                 :            : }
    4312                 :            : 
    4313                 :            : 
    4314                 :            : static const char *
    4315                 :          0 : dwarf_virtuality_string (unsigned int code)
    4316                 :            : {
    4317                 :          0 :   static const char *const known[] =
    4318                 :            :     {
    4319                 :            : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
    4320                 :            :       DWARF_ALL_KNOWN_DW_VIRTUALITY
    4321                 :            : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
    4322                 :            :     };
    4323                 :            : 
    4324                 :          0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4325                 :          0 :     return known[code];
    4326                 :            : 
    4327                 :            :   return NULL;
    4328                 :            : }
    4329                 :            : 
    4330                 :            : 
    4331                 :            : static const char *
    4332                 :          0 : dwarf_identifier_case_string (unsigned int code)
    4333                 :            : {
    4334                 :          0 :   static const char *const known[] =
    4335                 :            :     {
    4336                 :            : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
    4337                 :            :       DWARF_ALL_KNOWN_DW_ID
    4338                 :            : #undef DWARF_ONE_KNOWN_DW_ID
    4339                 :            :     };
    4340                 :            : 
    4341                 :          0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4342                 :          0 :     return known[code];
    4343                 :            : 
    4344                 :            :   return NULL;
    4345                 :            : }
    4346                 :            : 
    4347                 :            : 
    4348                 :            : static const char *
    4349                 :          0 : dwarf_calling_convention_string (unsigned int code)
    4350                 :            : {
    4351                 :          0 :   static const char *const known[] =
    4352                 :            :     {
    4353                 :            : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
    4354                 :            :       DWARF_ALL_KNOWN_DW_CC
    4355                 :            : #undef DWARF_ONE_KNOWN_DW_CC
    4356                 :            :     };
    4357                 :            : 
    4358                 :          0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4359                 :          0 :     return known[code];
    4360                 :            : 
    4361                 :            :   return NULL;
    4362                 :            : }
    4363                 :            : 
    4364                 :            : 
    4365                 :            : static const char *
    4366                 :          0 : dwarf_ordering_string (unsigned int code)
    4367                 :            : {
    4368                 :          0 :   static const char *const known[] =
    4369                 :            :     {
    4370                 :            : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
    4371                 :            :       DWARF_ALL_KNOWN_DW_ORD
    4372                 :            : #undef DWARF_ONE_KNOWN_DW_ORD
    4373                 :            :     };
    4374                 :            : 
    4375                 :          0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4376                 :          0 :     return known[code];
    4377                 :            : 
    4378                 :            :   return NULL;
    4379                 :            : }
    4380                 :            : 
    4381                 :            : 
    4382                 :            : static const char *
    4383                 :          8 : dwarf_discr_list_string (unsigned int code)
    4384                 :            : {
    4385                 :          8 :   static const char *const known[] =
    4386                 :            :     {
    4387                 :            : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
    4388                 :            :       DWARF_ALL_KNOWN_DW_DSC
    4389                 :            : #undef DWARF_ONE_KNOWN_DW_DSC
    4390                 :            :     };
    4391                 :            : 
    4392                 :          8 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4393                 :          8 :     return known[code];
    4394                 :            : 
    4395                 :            :   return NULL;
    4396                 :            : }
    4397                 :            : 
    4398                 :            : 
    4399                 :            : static const char *
    4400                 :     539511 : dwarf_locexpr_opcode_string (unsigned int code)
    4401                 :            : {
    4402                 :     539511 :   static const char *const known[] =
    4403                 :            :     {
    4404                 :            :       /* Normally we can't afford building huge table of 64K entries,
    4405                 :            :          most of them zero, just because there are a couple defined
    4406                 :            :          values at the far end.  In case of opcodes, it's OK.  */
    4407                 :            : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
    4408                 :            :       DWARF_ALL_KNOWN_DW_OP
    4409                 :            : #undef DWARF_ONE_KNOWN_DW_OP
    4410                 :            :     };
    4411                 :            : 
    4412                 :     539511 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4413                 :     539511 :     return known[code];
    4414                 :            : 
    4415                 :            :   return NULL;
    4416                 :            : }
    4417                 :            : 
    4418                 :            : 
    4419                 :            : static const char *
    4420                 :          8 : dwarf_unit_string (unsigned int type)
    4421                 :            : {
    4422   [ -  +  -  -  :          8 :   switch (type)
                -  -  + ]
    4423                 :            :     {
    4424                 :            : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
    4425                 :          7 :       DWARF_ALL_KNOWN_DW_UT
    4426                 :            : #undef DWARF_ONE_KNOWN_DW_UT
    4427                 :          0 :     default:
    4428                 :          0 :       return NULL;
    4429                 :            :     }
    4430                 :            : }
    4431                 :            : 
    4432                 :            : 
    4433                 :            : static const char *
    4434                 :         16 : dwarf_range_list_encoding_string (unsigned int kind)
    4435                 :            : {
    4436   [ -  +  +  -  :         16 :   switch (kind)
             +  -  -  -  
                      + ]
    4437                 :            :     {
    4438                 :            : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
    4439                 :         14 :       DWARF_ALL_KNOWN_DW_RLE
    4440                 :            : #undef DWARF_ONE_KNOWN_DW_RLE
    4441                 :          0 :     default:
    4442                 :          0 :       return NULL;
    4443                 :            :     }
    4444                 :            : }
    4445                 :            : 
    4446                 :            : 
    4447                 :            : static const char *
    4448                 :         91 : dwarf_loc_list_encoding_string (unsigned int kind)
    4449                 :            : {
    4450   [ -  -  +  +  :         91 :   switch (kind)
          -  +  -  +  -  
                   -  + ]
    4451                 :            :     {
    4452                 :            : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
    4453                 :         86 :       DWARF_ALL_KNOWN_DW_LLE
    4454                 :            : #undef DWARF_ONE_KNOWN_DW_LLE
    4455                 :            :     /* DW_LLE_GNU_view_pair is special/incompatible with default codes.  */
    4456                 :          0 :     case DW_LLE_GNU_view_pair: return "GNU_view_pair";
    4457                 :          0 :     default:
    4458                 :          0 :       return NULL;
    4459                 :            :     }
    4460                 :            : }
    4461                 :            : 
    4462                 :            : 
    4463                 :            : static const char *
    4464                 :          6 : dwarf_line_content_description_string (unsigned int kind)
    4465                 :            : {
    4466   [ +  +  -  -  :          6 :   switch (kind)
                   -  - ]
    4467                 :            :     {
    4468                 :            : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
    4469                 :          6 :       DWARF_ALL_KNOWN_DW_LNCT
    4470                 :            : #undef DWARF_ONE_KNOWN_DW_LNCT
    4471                 :          0 :     default:
    4472                 :          0 :       return NULL;
    4473                 :            :     }
    4474                 :            : }
    4475                 :            : 
    4476                 :            : 
    4477                 :            : /* Used by all dwarf_foo_name functions.  */
    4478                 :            : static const char *
    4479                 :    9189608 : string_or_unknown (const char *known, unsigned int code,
    4480                 :            :                    unsigned int lo_user, unsigned int hi_user,
    4481                 :            :                    bool print_unknown_num)
    4482                 :            : {
    4483                 :    9189608 :   static char unknown_buf[20];
    4484                 :            : 
    4485         [ -  + ]:    9189608 :   if (likely (known != NULL))
    4486                 :            :     return known;
    4487                 :            : 
    4488   [ #  #  #  # ]:          0 :   if (lo_user != 0 && code >= lo_user && code <= hi_user)
    4489                 :            :     {
    4490                 :          0 :       snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
    4491                 :            :                 code - lo_user);
    4492                 :          0 :       return unknown_buf;
    4493                 :            :     }
    4494                 :            : 
    4495         [ #  # ]:          0 :   if (print_unknown_num)
    4496                 :            :     {
    4497                 :          0 :       snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
    4498                 :          0 :       return unknown_buf;
    4499                 :            :     }
    4500                 :            : 
    4501                 :            :   return "???";
    4502                 :            : }
    4503                 :            : 
    4504                 :            : 
    4505                 :            : static const char *
    4506                 :    1034127 : dwarf_tag_name (unsigned int tag)
    4507                 :            : {
    4508                 :    1034127 :   const char *ret = dwarf_tag_string (tag);
    4509                 :    1034127 :   return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
    4510                 :            : }
    4511                 :            : 
    4512                 :            : static const char *
    4513                 :    4059368 : dwarf_attr_name (unsigned int attr)
    4514                 :            : {
    4515                 :    4059368 :   const char *ret = dwarf_attr_string (attr);
    4516                 :    4059368 :   return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
    4517                 :            : }
    4518                 :            : 
    4519                 :            : 
    4520                 :            : static const char *
    4521                 :    4059374 : dwarf_form_name (unsigned int form)
    4522                 :            : {
    4523                 :    4059374 :   const char *ret = dwarf_form_string (form);
    4524                 :    4059374 :   return string_or_unknown (ret, form, 0, 0, true);
    4525                 :            : }
    4526                 :            : 
    4527                 :            : 
    4528                 :            : static const char *
    4529                 :       1765 : dwarf_lang_name (unsigned int lang)
    4530                 :            : {
    4531                 :       1765 :   const char *ret = dwarf_lang_string (lang);
    4532                 :       1765 :   return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
    4533                 :            : }
    4534                 :            : 
    4535                 :            : 
    4536                 :            : static const char *
    4537                 :       3503 : dwarf_inline_name (unsigned int code)
    4538                 :            : {
    4539         [ +  - ]:       3503 :   const char *ret = dwarf_inline_string (code);
    4540                 :       3503 :   return string_or_unknown (ret, code, 0, 0, false);
    4541                 :            : }
    4542                 :            : 
    4543                 :            : 
    4544                 :            : static const char *
    4545                 :      31342 : dwarf_encoding_name (unsigned int code)
    4546                 :            : {
    4547         [ +  - ]:      31342 :   const char *ret = dwarf_encoding_string (code);
    4548                 :      31342 :   return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
    4549                 :            : }
    4550                 :            : 
    4551                 :            : 
    4552                 :            : static const char *
    4553                 :          0 : dwarf_access_name (unsigned int code)
    4554                 :            : {
    4555         [ #  # ]:          0 :   const char *ret = dwarf_access_string (code);
    4556                 :          0 :   return string_or_unknown (ret, code, 0, 0, false);
    4557                 :            : }
    4558                 :            : 
    4559                 :            : 
    4560                 :            : static const char *
    4561                 :          0 : dwarf_defaulted_name (unsigned int code)
    4562                 :            : {
    4563         [ #  # ]:          0 :   const char *ret = dwarf_defaulted_string (code);
    4564                 :          0 :   return string_or_unknown (ret, code, 0, 0, false);
    4565                 :            : }
    4566                 :            : 
    4567                 :            : 
    4568                 :            : static const char *
    4569                 :          0 : dwarf_visibility_name (unsigned int code)
    4570                 :            : {
    4571         [ #  # ]:          0 :   const char *ret = dwarf_visibility_string (code);
    4572                 :          0 :   return string_or_unknown (ret, code, 0, 0, false);
    4573                 :            : }
    4574                 :            : 
    4575                 :            : 
    4576                 :            : static const char *
    4577                 :          0 : dwarf_virtuality_name (unsigned int code)
    4578                 :            : {
    4579         [ #  # ]:          0 :   const char *ret = dwarf_virtuality_string (code);
    4580                 :          0 :   return string_or_unknown (ret, code, 0, 0, false);
    4581                 :            : }
    4582                 :            : 
    4583                 :            : 
    4584                 :            : static const char *
    4585                 :          0 : dwarf_identifier_case_name (unsigned int code)
    4586                 :            : {
    4587         [ #  # ]:          0 :   const char *ret = dwarf_identifier_case_string (code);
    4588                 :          0 :   return string_or_unknown (ret, code, 0, 0, false);
    4589                 :            : }
    4590                 :            : 
    4591                 :            : 
    4592                 :            : static const char *
    4593                 :          0 : dwarf_calling_convention_name (unsigned int code)
    4594                 :            : {
    4595         [ #  # ]:          0 :   const char *ret = dwarf_calling_convention_string (code);
    4596                 :          0 :   return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
    4597                 :            : }
    4598                 :            : 
    4599                 :            : 
    4600                 :            : static const char *
    4601                 :          0 : dwarf_ordering_name (unsigned int code)
    4602                 :            : {
    4603         [ #  # ]:          0 :   const char *ret = dwarf_ordering_string (code);
    4604                 :          0 :   return string_or_unknown (ret, code, 0, 0, false);
    4605                 :            : }
    4606                 :            : 
    4607                 :            : 
    4608                 :            : static const char *
    4609                 :          8 : dwarf_discr_list_name (unsigned int code)
    4610                 :            : {
    4611         [ +  - ]:          8 :   const char *ret = dwarf_discr_list_string (code);
    4612                 :          8 :   return string_or_unknown (ret, code, 0, 0, false);
    4613                 :            : }
    4614                 :            : 
    4615                 :            : 
    4616                 :            : static const char *
    4617                 :          8 : dwarf_unit_name (unsigned int type)
    4618                 :            : {
    4619                 :          8 :   const char *ret = dwarf_unit_string (type);
    4620                 :          8 :   return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
    4621                 :            : }
    4622                 :            : 
    4623                 :            : 
    4624                 :            : static const char *
    4625                 :         16 : dwarf_range_list_encoding_name (unsigned int kind)
    4626                 :            : {
    4627                 :         16 :   const char *ret = dwarf_range_list_encoding_string (kind);
    4628                 :         16 :   return string_or_unknown (ret, kind, 0, 0, false);
    4629                 :            : }
    4630                 :            : 
    4631                 :            : 
    4632                 :            : static const char *
    4633                 :         91 : dwarf_loc_list_encoding_name (unsigned int kind)
    4634                 :            : {
    4635                 :         91 :   const char *ret = dwarf_loc_list_encoding_string (kind);
    4636                 :         91 :   return string_or_unknown (ret, kind, 0, 0, false);
    4637                 :            : }
    4638                 :            : 
    4639                 :            : 
    4640                 :            : static const char *
    4641                 :          6 : dwarf_line_content_description_name (unsigned int kind)
    4642                 :            : {
    4643                 :          6 :   const char *ret = dwarf_line_content_description_string (kind);
    4644                 :          6 :   return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
    4645                 :            :                             false);
    4646                 :            : }
    4647                 :            : 
    4648                 :            : 
    4649                 :            : static void
    4650                 :        434 : print_block (size_t n, const void *block)
    4651                 :            : {
    4652         [ -  + ]:        434 :   if (n == 0)
    4653                 :          0 :     puts (_("empty block"));
    4654                 :            :   else
    4655                 :            :     {
    4656                 :        434 :       printf (_("%zu byte block:"), n);
    4657                 :        434 :       const unsigned char *data = block;
    4658                 :       2386 :       do
    4659                 :       2386 :         printf (" %02x", *data++);
    4660         [ +  + ]:       2386 :       while (--n > 0);
    4661                 :        434 :       putchar ('\n');
    4662                 :            :     }
    4663                 :        434 : }
    4664                 :            : 
    4665                 :            : static void
    4666                 :          0 : print_bytes (size_t n, const unsigned char *bytes)
    4667                 :            : {
    4668                 :          0 :   while (n-- > 0)
    4669                 :            :     {
    4670                 :          0 :       printf ("%02x", *bytes++);
    4671         [ #  # ]:          0 :       if (n > 0)
    4672         [ #  # ]:          0 :         printf (" ");
    4673                 :            :     }
    4674                 :          0 : }
    4675                 :            : 
    4676                 :            : static int
    4677                 :         68 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
    4678                 :            : {
    4679         [ +  - ]:         68 :   if (cu == NULL)
    4680                 :            :     return -1;
    4681                 :            : 
    4682                 :         68 :   Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
    4683         [ +  - ]:         68 :   if (debug_addr == NULL)
    4684                 :            :     return -1;
    4685                 :            : 
    4686                 :         68 :   Dwarf_Off base = __libdw_cu_addr_base (cu);
    4687                 :         68 :   Dwarf_Word off = idx * cu->address_size;
    4688         [ +  - ]:         68 :   if (base > debug_addr->d_size
    4689         [ +  - ]:         68 :       || off > debug_addr->d_size - base
    4690         [ +  - ]:         68 :       || cu->address_size > debug_addr->d_size - base - off)
    4691                 :            :     return -1;
    4692                 :            : 
    4693                 :         68 :   const unsigned char *addrp = debug_addr->d_buf + base + off;
    4694         [ -  + ]:         68 :   if (cu->address_size == 4)
    4695         [ #  # ]:          0 :     *addr = read_4ubyte_unaligned (cu->dbg, addrp);
    4696                 :            :   else
    4697         [ -  + ]:         68 :     *addr = read_8ubyte_unaligned (cu->dbg, addrp);
    4698                 :            : 
    4699                 :            :   return 0;
    4700                 :            : }
    4701                 :            : 
    4702                 :            : static void
    4703                 :     376674 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
    4704                 :            :            unsigned int vers, unsigned int addrsize, unsigned int offset_size,
    4705                 :            :            struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
    4706                 :            : {
    4707         [ +  + ]:     376674 :   const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
    4708                 :            : 
    4709         [ -  + ]:     376674 :   if (len == 0)
    4710                 :            :     {
    4711                 :          0 :       printf ("%*s(empty)\n", indent, "");
    4712                 :          0 :       return;
    4713                 :            :     }
    4714                 :            : 
    4715                 :            : #define NEED(n)         if (len < (Dwarf_Word) (n)) goto invalid
    4716                 :            : #define CONSUME(n)      NEED (n); else len -= (n)
    4717                 :            : 
    4718                 :            :   Dwarf_Word offset = 0;
    4719         [ +  + ]:     916185 :   while (len-- > 0)
    4720                 :            :     {
    4721                 :     539511 :       uint_fast8_t op = *data++;
    4722                 :            : 
    4723         [ +  - ]:     539511 :       const char *op_name = dwarf_locexpr_opcode_string (op);
    4724         [ -  + ]:     539511 :       if (unlikely (op_name == NULL))
    4725                 :            :         {
    4726                 :          0 :           static char buf[20];
    4727         [ #  # ]:          0 :           if (op >= DW_OP_lo_user)
    4728                 :          0 :             snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
    4729                 :            :           else
    4730                 :          0 :             snprintf (buf, sizeof buf, "??? (%#x)", op);
    4731                 :            :           op_name = buf;
    4732                 :            :         }
    4733                 :            : 
    4734   [ +  -  +  +  :     539511 :       switch (op)
          +  +  +  +  -  
          -  +  +  -  +  
          -  -  +  +  +  
          +  +  +  -  -  
             -  +  +  + ]
    4735                 :            :         {
    4736                 :      11962 :         case DW_OP_addr:;
    4737                 :            :           /* Address operand.  */
    4738                 :      11962 :           Dwarf_Word addr;
    4739         [ -  + ]:      11962 :           NEED (addrsize);
    4740         [ +  + ]:      11962 :           if (addrsize == 4)
    4741         [ +  + ]:         42 :             addr = read_4ubyte_unaligned (dbg, data);
    4742         [ +  - ]:      11920 :           else if (addrsize == 8)
    4743         [ +  + ]:      11920 :             addr = read_8ubyte_unaligned (dbg, data);
    4744                 :            :           else
    4745                 :          0 :             goto invalid;
    4746                 :      11962 :           data += addrsize;
    4747                 :      11962 :           CONSUME (addrsize);
    4748                 :            : 
    4749                 :      11962 :           printf ("%*s[%2" PRIuMAX "] %s ",
    4750                 :            :                   indent, "", (uintmax_t) offset, op_name);
    4751                 :      11962 :           print_dwarf_addr (dwflmod, 0, addr, addr);
    4752                 :      11962 :           printf ("\n");
    4753                 :            : 
    4754                 :      11962 :           offset += 1 + addrsize;
    4755                 :      11962 :           break;
    4756                 :            : 
    4757                 :          0 :         case DW_OP_call_ref:
    4758                 :            :         case DW_OP_GNU_variable_value:
    4759                 :            :           /* Offset operand.  */
    4760         [ #  # ]:          0 :           if (ref_size != 4 && ref_size != 8)
    4761                 :          0 :             goto invalid; /* Cannot be used in CFA.  */
    4762         [ #  # ]:          0 :           NEED (ref_size);
    4763         [ #  # ]:          0 :           if (ref_size == 4)
    4764         [ #  # ]:          0 :             addr = read_4ubyte_unaligned (dbg, data);
    4765                 :            :           else
    4766         [ #  # ]:          0 :             addr = read_8ubyte_unaligned (dbg, data);
    4767                 :          0 :           data += ref_size;
    4768                 :          0 :           CONSUME (ref_size);
    4769                 :            :           /* addr is a DIE offset, so format it as one.  */
    4770                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    4771                 :            :                   indent, "", (uintmax_t) offset,
    4772                 :            :                   op_name, (uintmax_t) addr);
    4773                 :          0 :           offset += 1 + ref_size;
    4774                 :          0 :           break;
    4775                 :            : 
    4776                 :      11800 :         case DW_OP_deref_size:
    4777                 :            :         case DW_OP_xderef_size:
    4778                 :            :         case DW_OP_pick:
    4779                 :            :         case DW_OP_const1u:
    4780                 :            :           // XXX value might be modified by relocation
    4781         [ -  + ]:      11800 :           NEED (1);
    4782                 :      23600 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
    4783                 :            :                   indent, "", (uintmax_t) offset,
    4784                 :      11800 :                   op_name, *((uint8_t *) data));
    4785                 :      11800 :           ++data;
    4786                 :      11800 :           --len;
    4787                 :      11800 :           offset += 2;
    4788                 :      11800 :           break;
    4789                 :            : 
    4790                 :       1631 :         case DW_OP_const2u:
    4791         [ -  + ]:       1631 :           NEED (2);
    4792                 :            :           // XXX value might be modified by relocation
    4793                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
    4794                 :            :                   indent, "", (uintmax_t) offset,
    4795         [ -  + ]:       1631 :                   op_name, read_2ubyte_unaligned (dbg, data));
    4796                 :       1631 :           CONSUME (2);
    4797                 :       1631 :           data += 2;
    4798                 :       1631 :           offset += 3;
    4799                 :       1631 :           break;
    4800                 :            : 
    4801                 :       1132 :         case DW_OP_const4u:
    4802         [ -  + ]:       1132 :           NEED (4);
    4803                 :            :           // XXX value might be modified by relocation
    4804                 :       3396 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
    4805                 :            :                   indent, "", (uintmax_t) offset,
    4806         [ -  + ]:       1132 :                   op_name, read_4ubyte_unaligned (dbg, data));
    4807                 :       1132 :           CONSUME (4);
    4808                 :       1132 :           data += 4;
    4809                 :       1132 :           offset += 5;
    4810                 :       1132 :           break;
    4811                 :            : 
    4812                 :         56 :         case DW_OP_const8u:
    4813         [ -  + ]:         56 :           NEED (8);
    4814                 :            :           // XXX value might be modified by relocation
    4815                 :        168 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
    4816                 :            :                   indent, "", (uintmax_t) offset,
    4817         [ -  + ]:         56 :                   op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
    4818                 :         56 :           CONSUME (8);
    4819                 :         56 :           data += 8;
    4820                 :         56 :           offset += 9;
    4821                 :         56 :           break;
    4822                 :            : 
    4823                 :       2768 :         case DW_OP_const1s:
    4824         [ -  + ]:       2768 :           NEED (1);
    4825                 :            :           // XXX value might be modified by relocation
    4826                 :       5536 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
    4827                 :            :                   indent, "", (uintmax_t) offset,
    4828                 :       2768 :                   op_name, *((int8_t *) data));
    4829                 :       2768 :           ++data;
    4830                 :       2768 :           --len;
    4831                 :       2768 :           offset += 2;
    4832                 :       2768 :           break;
    4833                 :            : 
    4834                 :          5 :         case DW_OP_const2s:
    4835         [ -  + ]:          5 :           NEED (2);
    4836                 :            :           // XXX value might be modified by relocation
    4837                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
    4838                 :            :                   indent, "", (uintmax_t) offset,
    4839         [ -  + ]:          5 :                   op_name, read_2sbyte_unaligned (dbg, data));
    4840                 :          5 :           CONSUME (2);
    4841                 :          5 :           data += 2;
    4842                 :          5 :           offset += 3;
    4843                 :          5 :           break;
    4844                 :            : 
    4845                 :          0 :         case DW_OP_const4s:
    4846         [ #  # ]:          0 :           NEED (4);
    4847                 :            :           // XXX value might be modified by relocation
    4848                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
    4849                 :            :                   indent, "", (uintmax_t) offset,
    4850         [ #  # ]:          0 :                   op_name, read_4sbyte_unaligned (dbg, data));
    4851                 :          0 :           CONSUME (4);
    4852                 :          0 :           data += 4;
    4853                 :          0 :           offset += 5;
    4854                 :          0 :           break;
    4855                 :            : 
    4856                 :          0 :         case DW_OP_const8s:
    4857         [ #  # ]:          0 :           NEED (8);
    4858                 :            :           // XXX value might be modified by relocation
    4859                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
    4860                 :            :                   indent, "", (uintmax_t) offset,
    4861         [ #  # ]:          0 :                   op_name, read_8sbyte_unaligned (dbg, data));
    4862                 :          0 :           CONSUME (8);
    4863                 :          0 :           data += 8;
    4864                 :          0 :           offset += 9;
    4865                 :          0 :           break;
    4866                 :            : 
    4867                 :       7151 :         case DW_OP_piece:
    4868                 :            :         case DW_OP_regx:
    4869                 :            :         case DW_OP_plus_uconst:
    4870                 :       7151 :         case DW_OP_constu:;
    4871                 :       7151 :           const unsigned char *start = data;
    4872                 :       7151 :           uint64_t uleb;
    4873         [ -  + ]:       7151 :           NEED (1);
    4874                 :       7151 :           get_uleb128 (uleb, data, data + len);
    4875                 :       7151 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
    4876                 :            :                   indent, "", (uintmax_t) offset, op_name, uleb);
    4877         [ -  + ]:       7151 :           CONSUME (data - start);
    4878                 :       7151 :           offset += 1 + (data - start);
    4879                 :       7151 :           break;
    4880                 :            : 
    4881                 :          2 :         case DW_OP_addrx:
    4882                 :            :         case DW_OP_GNU_addr_index:
    4883                 :            :         case DW_OP_constx:
    4884                 :          2 :         case DW_OP_GNU_const_index:;
    4885                 :          2 :           start = data;
    4886         [ -  + ]:          2 :           NEED (1);
    4887                 :          2 :           get_uleb128 (uleb, data, data + len);
    4888                 :          2 :           printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
    4889                 :            :                   indent, "", (uintmax_t) offset, op_name, uleb);
    4890         [ -  + ]:          2 :           CONSUME (data - start);
    4891                 :          2 :           offset += 1 + (data - start);
    4892         [ -  + ]:          2 :           if (get_indexed_addr (cu, uleb, &addr) != 0)
    4893                 :          0 :             printf ("???\n");
    4894                 :            :           else
    4895                 :            :             {
    4896                 :          2 :               print_dwarf_addr (dwflmod, 0, addr, addr);
    4897                 :          2 :               printf ("\n");
    4898                 :            :             }
    4899                 :            :           break;
    4900                 :            : 
    4901                 :          0 :         case DW_OP_bit_piece:
    4902                 :          0 :           start = data;
    4903                 :          0 :           uint64_t uleb2;
    4904         [ #  # ]:          0 :           NEED (1);
    4905                 :          0 :           get_uleb128 (uleb, data, data + len);
    4906                 :          0 :           NEED (1);
    4907                 :          0 :           get_uleb128 (uleb2, data, data + len);
    4908                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
    4909                 :            :                   indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
    4910         [ #  # ]:          0 :           CONSUME (data - start);
    4911                 :          0 :           offset += 1 + (data - start);
    4912                 :          0 :           break;
    4913                 :            : 
    4914                 :      94396 :         case DW_OP_fbreg:
    4915                 :            :         case DW_OP_breg0 ... DW_OP_breg31:
    4916                 :            :         case DW_OP_consts:
    4917                 :      94396 :           start = data;
    4918                 :      94396 :           int64_t sleb;
    4919         [ -  + ]:      94396 :           NEED (1);
    4920                 :      94396 :           get_sleb128 (sleb, data, data + len);
    4921                 :      94396 :           printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
    4922                 :            :                   indent, "", (uintmax_t) offset, op_name, sleb);
    4923         [ -  + ]:      94396 :           CONSUME (data - start);
    4924                 :      94396 :           offset += 1 + (data - start);
    4925                 :      94396 :           break;
    4926                 :            : 
    4927                 :          0 :         case DW_OP_bregx:
    4928                 :          0 :           start = data;
    4929         [ #  # ]:          0 :           NEED (1);
    4930                 :          0 :           get_uleb128 (uleb, data, data + len);
    4931                 :          0 :           NEED (1);
    4932                 :          0 :           get_sleb128 (sleb, data, data + len);
    4933                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
    4934                 :            :                   indent, "", (uintmax_t) offset, op_name, uleb, sleb);
    4935         [ #  # ]:          0 :           CONSUME (data - start);
    4936                 :          0 :           offset += 1 + (data - start);
    4937                 :          0 :           break;
    4938                 :            : 
    4939                 :          0 :         case DW_OP_call2:
    4940         [ #  # ]:          0 :           NEED (2);
    4941                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
    4942                 :            :                   indent, "", (uintmax_t) offset, op_name,
    4943         [ #  # ]:          0 :                   read_2ubyte_unaligned (dbg, data));
    4944                 :          0 :           CONSUME (2);
    4945                 :          0 :           data += 2;
    4946                 :          0 :           offset += 3;
    4947                 :          0 :           break;
    4948                 :            : 
    4949                 :          4 :         case DW_OP_call4:
    4950         [ -  + ]:          4 :           NEED (4);
    4951                 :         12 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
    4952                 :            :                   indent, "", (uintmax_t) offset, op_name,
    4953         [ -  + ]:          4 :                   read_4ubyte_unaligned (dbg, data));
    4954                 :          4 :           CONSUME (4);
    4955                 :          4 :           data += 4;
    4956                 :          4 :           offset += 5;
    4957                 :          4 :           break;
    4958                 :            : 
    4959                 :        208 :         case DW_OP_skip:
    4960                 :            :         case DW_OP_bra:
    4961         [ -  + ]:        208 :           NEED (2);
    4962                 :        416 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
    4963                 :            :                   indent, "", (uintmax_t) offset, op_name,
    4964         [ -  + ]:        208 :                   (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
    4965                 :        208 :           CONSUME (2);
    4966                 :        208 :           data += 2;
    4967                 :        208 :           offset += 3;
    4968                 :        208 :           break;
    4969                 :            : 
    4970                 :        332 :         case DW_OP_implicit_value:
    4971                 :        332 :           start = data;
    4972         [ -  + ]:        332 :           NEED (1);
    4973                 :        332 :           get_uleb128 (uleb, data, data + len);
    4974                 :        332 :           printf ("%*s[%2" PRIuMAX "] %s: ",
    4975                 :            :                   indent, "", (uintmax_t) offset, op_name);
    4976         [ -  + ]:        332 :           NEED (uleb);
    4977                 :        332 :           print_block (uleb, data);
    4978                 :        332 :           data += uleb;
    4979         [ -  + ]:        332 :           CONSUME (data - start);
    4980                 :        332 :           offset += 1 + (data - start);
    4981                 :        332 :           break;
    4982                 :            : 
    4983                 :       3268 :         case DW_OP_implicit_pointer:
    4984                 :            :         case DW_OP_GNU_implicit_pointer:
    4985                 :            :           /* DIE offset operand.  */
    4986                 :       3268 :           start = data;
    4987         [ -  + ]:       3268 :           NEED (ref_size);
    4988         [ -  + ]:       3268 :           if (ref_size != 4 && ref_size != 8)
    4989                 :          0 :             goto invalid; /* Cannot be used in CFA.  */
    4990         [ +  - ]:       3268 :           if (ref_size == 4)
    4991         [ -  + ]:       3268 :             addr = read_4ubyte_unaligned (dbg, data);
    4992                 :            :           else
    4993         [ #  # ]:          0 :             addr = read_8ubyte_unaligned (dbg, data);
    4994                 :       3268 :           data += ref_size;
    4995                 :            :           /* Byte offset operand.  */
    4996         [ -  + ]:       3268 :           NEED (1);
    4997                 :       3268 :           get_sleb128 (sleb, data, data + len);
    4998                 :            : 
    4999                 :       3268 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
    5000                 :            :                   indent, "", (intmax_t) offset,
    5001                 :            :                   op_name, (uintmax_t) addr, sleb);
    5002         [ -  + ]:       3268 :           CONSUME (data - start);
    5003                 :       3268 :           offset += 1 + (data - start);
    5004                 :       3268 :           break;
    5005                 :            : 
    5006                 :      31246 :         case DW_OP_entry_value:
    5007                 :            :         case DW_OP_GNU_entry_value:
    5008                 :            :           /* Size plus expression block.  */
    5009                 :      31246 :           start = data;
    5010         [ -  + ]:      31246 :           NEED (1);
    5011                 :      31246 :           get_uleb128 (uleb, data, data + len);
    5012                 :      31246 :           printf ("%*s[%2" PRIuMAX "] %s:\n",
    5013                 :            :                   indent, "", (uintmax_t) offset, op_name);
    5014         [ -  + ]:      31246 :           NEED (uleb);
    5015                 :      31246 :           print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
    5016                 :            :                      addrsize, offset_size, cu, uleb, data);
    5017                 :      31246 :           data += uleb;
    5018         [ -  + ]:      31246 :           CONSUME (data - start);
    5019                 :      31246 :           offset += 1 + (data - start);
    5020                 :      31246 :           break;
    5021                 :            : 
    5022                 :        102 :         case DW_OP_const_type:
    5023                 :            :         case DW_OP_GNU_const_type:
    5024                 :            :           /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
    5025                 :            :              unsigned size plus block.  */
    5026                 :        102 :           start = data;
    5027         [ -  + ]:        102 :           NEED (1);
    5028                 :        102 :           get_uleb128 (uleb, data, data + len);
    5029   [ +  -  +  - ]:        102 :           if (! print_unresolved_addresses && cu != NULL)
    5030                 :        102 :             uleb += cu->start;
    5031                 :        102 :           NEED (1);
    5032                 :        102 :           uint8_t usize = *(uint8_t *) data++;
    5033         [ -  + ]:        102 :           NEED (usize);
    5034                 :        102 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
    5035                 :            :                   indent, "", (uintmax_t) offset, op_name, uleb);
    5036                 :        102 :           print_block (usize, data);
    5037                 :        102 :           data += usize;
    5038         [ -  + ]:        102 :           CONSUME (data - start);
    5039                 :        102 :           offset += 1 + (data - start);
    5040                 :        102 :           break;
    5041                 :            : 
    5042                 :          0 :         case DW_OP_regval_type:
    5043                 :            :         case DW_OP_GNU_regval_type:
    5044                 :            :           /* uleb128 register number, uleb128 CU relative
    5045                 :            :              DW_TAG_base_type DIE offset.  */
    5046                 :          0 :           start = data;
    5047         [ #  # ]:          0 :           NEED (1);
    5048                 :          0 :           get_uleb128 (uleb, data, data + len);
    5049                 :          0 :           NEED (1);
    5050                 :          0 :           get_uleb128 (uleb2, data, data + len);
    5051   [ #  #  #  # ]:          0 :           if (! print_unresolved_addresses && cu != NULL)
    5052                 :          0 :             uleb2 += cu->start;
    5053                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
    5054                 :            :                   indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
    5055         [ #  # ]:          0 :           CONSUME (data - start);
    5056                 :          0 :           offset += 1 + (data - start);
    5057                 :          0 :           break;
    5058                 :            : 
    5059                 :          0 :         case DW_OP_deref_type:
    5060                 :            :         case DW_OP_GNU_deref_type:
    5061                 :            :           /* 1-byte unsigned size of value, uleb128 CU relative
    5062                 :            :              DW_TAG_base_type DIE offset.  */
    5063                 :          0 :           start = data;
    5064         [ #  # ]:          0 :           NEED (1);
    5065                 :          0 :           usize = *(uint8_t *) data++;
    5066                 :          0 :           NEED (1);
    5067                 :          0 :           get_uleb128 (uleb, data, data + len);
    5068   [ #  #  #  # ]:          0 :           if (! print_unresolved_addresses && cu != NULL)
    5069                 :          0 :             uleb += cu->start;
    5070                 :          0 :           printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
    5071                 :            :                   indent, "", (uintmax_t) offset,
    5072                 :            :                   op_name, usize, uleb);
    5073         [ #  # ]:          0 :           CONSUME (data - start);
    5074                 :          0 :           offset += 1 + (data - start);
    5075                 :          0 :           break;
    5076                 :            : 
    5077                 :          0 :         case DW_OP_xderef_type:
    5078                 :            :           /* 1-byte unsigned size of value, uleb128 base_type DIE offset.  */
    5079                 :          0 :           start = data;
    5080         [ #  # ]:          0 :           NEED (1);
    5081                 :          0 :           usize = *(uint8_t *) data++;
    5082                 :          0 :           NEED (1);
    5083                 :          0 :           get_uleb128 (uleb, data, data + len);
    5084                 :          0 :           printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
    5085                 :            :                   indent, "", (uintmax_t) offset,
    5086                 :            :                   op_name, usize, uleb);
    5087         [ #  # ]:          0 :           CONSUME (data - start);
    5088                 :          0 :           offset += 1 + (data - start);
    5089                 :          0 :           break;
    5090                 :            : 
    5091                 :       1222 :         case DW_OP_convert:
    5092                 :            :         case DW_OP_GNU_convert:
    5093                 :            :         case DW_OP_reinterpret:
    5094                 :            :         case DW_OP_GNU_reinterpret:
    5095                 :            :           /* uleb128 CU relative offset to DW_TAG_base_type, or zero
    5096                 :            :              for conversion to untyped.  */
    5097                 :       1222 :           start = data;
    5098         [ -  + ]:       1222 :           NEED (1);
    5099                 :       1222 :           get_uleb128 (uleb, data, data + len);
    5100   [ +  +  +  -  :       1222 :           if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
                   +  - ]
    5101                 :        927 :             uleb += cu->start;
    5102                 :       1222 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    5103                 :            :                   indent, "", (uintmax_t) offset, op_name, uleb);
    5104         [ -  + ]:       1222 :           CONSUME (data - start);
    5105                 :       1222 :           offset += 1 + (data - start);
    5106                 :       1222 :           break;
    5107                 :            : 
    5108                 :          3 :         case DW_OP_GNU_parameter_ref:
    5109                 :            :           /* 4 byte CU relative reference to the abstract optimized away
    5110                 :            :              DW_TAG_formal_parameter.  */
    5111         [ -  + ]:          3 :           NEED (4);
    5112         [ -  + ]:          3 :           uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
    5113   [ +  -  +  - ]:          3 :           if (! print_unresolved_addresses && cu != NULL)
    5114                 :          3 :             param_off += cu->start;
    5115                 :          3 :           printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    5116                 :            :                   indent, "", (uintmax_t) offset, op_name, param_off);
    5117                 :          3 :           CONSUME (4);
    5118                 :          3 :           data += 4;
    5119                 :          3 :           offset += 5;
    5120                 :          3 :           break;
    5121                 :            : 
    5122                 :            :         default:
    5123                 :            :           /* No Operand.  */
    5124                 :     372223 :           printf ("%*s[%2" PRIuMAX "] %s\n",
    5125                 :            :                   indent, "", (uintmax_t) offset, op_name);
    5126                 :     372223 :           ++offset;
    5127                 :     372223 :           break;
    5128                 :            :         }
    5129                 :            : 
    5130                 :     539511 :       indent = indentrest;
    5131                 :     539511 :       continue;
    5132                 :            : 
    5133                 :          0 :     invalid:
    5134                 :          0 :       printf (_("%*s[%2" PRIuMAX "] %s  <TRUNCATED>\n"),
    5135                 :            :               indent, "", (uintmax_t) offset, op_name);
    5136                 :            :       break;
    5137                 :            :     }
    5138                 :            : }
    5139                 :            : 
    5140                 :            : 
    5141                 :            : /* Turn the addresses into file offsets by using the phdrs.  */
    5142                 :            : static void
    5143                 :          1 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
    5144                 :            :                   GElf_Addr addrs[n], GElf_Off offs[n])
    5145                 :            : {
    5146                 :          1 :   size_t unsolved = n;
    5147         [ +  + ]:          9 :   for (size_t i = 0; i < phnum; ++i) {
    5148                 :          8 :     GElf_Phdr phdr_mem;
    5149                 :          8 :     GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
    5150   [ +  -  +  +  :          8 :     if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
                   +  - ]
    5151         [ +  + ]:         23 :       for (size_t j = 0; j < n; ++j)
    5152   [ +  +  +  + ]:         22 :         if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
    5153         [ +  - ]:         11 :             addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
    5154                 :         11 :           offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
    5155         [ +  + ]:         11 :           if (--unsolved == 0)
    5156                 :            :             break;
    5157                 :            :         }
    5158                 :            :   }
    5159                 :          1 : }
    5160                 :            : 
    5161                 :            : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
    5162                 :            :    And .dynamic section contains an array of the dynamic structures.
    5163                 :            :    We use the array to get:
    5164                 :            :     DT_STRTAB: the address of the string table
    5165                 :            :     DT_SYMTAB: the address of the symbol table
    5166                 :            :     DT_STRSZ: the size, in bytes, of the string table
    5167                 :            :     ...  */
    5168                 :            : static void
    5169                 :          1 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
    5170                 :            : {
    5171                 :          1 :   Elf_Data *data = elf_getdata_rawchunk(
    5172                 :          1 :     elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
    5173                 :            : 
    5174                 :          1 :   int dyn_idx = 0;
    5175                 :         45 :   for (;; ++dyn_idx) {
    5176                 :         23 :     GElf_Dyn dyn_mem;
    5177                 :         23 :     GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
    5178                 :            :     /* DT_NULL Marks end of dynamic section.  */
    5179   [ +  -  +  + ]:         23 :     if (dyn == NULL || dyn->d_tag == DT_NULL)
    5180                 :            :       break;
    5181                 :            : 
    5182   [ +  -  +  +  :         22 :     switch (dyn->d_tag) {
          +  +  +  +  +  
                +  -  + ]
    5183                 :          1 :     case DT_SYMTAB:
    5184                 :          1 :       addrs[i_symtab] = dyn->d_un.d_ptr;
    5185                 :          1 :       break;
    5186                 :            : 
    5187                 :          0 :     case DT_HASH:
    5188                 :          0 :       addrs[i_hash] = dyn->d_un.d_ptr;
    5189                 :          0 :       break;
    5190                 :            : 
    5191                 :          1 :     case DT_GNU_HASH:
    5192                 :          1 :       addrs[i_gnu_hash] = dyn->d_un.d_ptr;
    5193                 :          1 :       break;
    5194                 :            : 
    5195                 :          1 :     case DT_STRTAB:
    5196                 :          1 :       addrs[i_strtab] = dyn->d_un.d_ptr;
    5197                 :          1 :       break;
    5198                 :            : 
    5199                 :          1 :     case DT_VERSYM:
    5200                 :          1 :       addrs[i_versym] = dyn->d_un.d_ptr;
    5201                 :          1 :       break;
    5202                 :            : 
    5203                 :          1 :     case DT_VERDEF:
    5204                 :          1 :       addrs[i_verdef] = dyn->d_un.d_ptr;
    5205                 :          1 :       break;
    5206                 :            : 
    5207                 :          1 :     case DT_VERDEFNUM:
    5208                 :          1 :       addrs[i_verdefnum] = dyn->d_un.d_val;
    5209                 :          1 :       break;
    5210                 :            : 
    5211                 :          1 :     case DT_VERNEED:
    5212                 :          1 :       addrs[i_verneed] = dyn->d_un.d_ptr;
    5213                 :          1 :       break;
    5214                 :            : 
    5215                 :          1 :     case DT_VERNEEDNUM:
    5216                 :          1 :       addrs[i_verneednum] = dyn->d_un.d_val;
    5217                 :          1 :       break;
    5218                 :            : 
    5219                 :          1 :     case DT_STRSZ:
    5220                 :          1 :       addrs[i_strsz] = dyn->d_un.d_val;
    5221                 :          1 :       break;
    5222                 :            : 
    5223                 :          0 :     case DT_SYMTAB_SHNDX:
    5224                 :          0 :       addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
    5225                 :          0 :       break;
    5226                 :            :     }
    5227                 :         22 :   }
    5228                 :          1 : }
    5229                 :            : 
    5230                 :            : 
    5231                 :            : /* Use dynamic segment to get data for the string table section.  */
    5232                 :            : static Elf_Data *
    5233                 :          1 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
    5234                 :            : {
    5235                 :          1 :   Elf_Data *strtab_data;
    5236                 :          1 :   GElf_Addr addrs[i_max] = {0,};
    5237                 :          1 :   GElf_Off offs[i_max] = {0,};
    5238                 :          1 :   get_dynscn_addrs(elf, phdr, addrs);
    5239                 :          1 :   find_offsets(elf, 0, i_max, addrs, offs);
    5240                 :          2 :   strtab_data = elf_getdata_rawchunk(
    5241                 :          1 :           elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
    5242                 :          1 :   return strtab_data;
    5243                 :            : }
    5244                 :            : 
    5245                 :            : 
    5246                 :            : struct listptr
    5247                 :            : {
    5248                 :            :   Dwarf_Off offset:(64 - 3);
    5249                 :            :   bool addr64:1;
    5250                 :            :   bool dwarf64:1;
    5251                 :            :   bool warned:1;
    5252                 :            :   struct Dwarf_CU *cu;
    5253                 :            :   unsigned int attr;
    5254                 :            : };
    5255                 :            : 
    5256                 :            : #define listptr_offset_size(p)  ((p)->dwarf64 ? 8 : 4)
    5257                 :            : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
    5258                 :            : 
    5259                 :            : static Dwarf_Addr
    5260                 :     125686 : cudie_base (Dwarf_Die *cudie)
    5261                 :            : {
    5262                 :     125686 :   Dwarf_Addr base;
    5263                 :            :   /* Find the base address of the compilation unit.  It will normally
    5264                 :            :      be specified by DW_AT_low_pc.  In DWARF-3 draft 4, the base
    5265                 :            :      address could be overridden by DW_AT_entry_pc.  It's been
    5266                 :            :      removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
    5267                 :            :      compilation units with discontinuous ranges.  */
    5268         [ -  + ]:     125686 :   if (unlikely (dwarf_lowpc (cudie, &base) != 0))
    5269                 :            :     {
    5270                 :          0 :       Dwarf_Attribute attr_mem;
    5271         [ #  # ]:          0 :       if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
    5272                 :            :                           &base) != 0)
    5273                 :          0 :         base = 0;
    5274                 :            :     }
    5275                 :     125686 :   return base;
    5276                 :            : }
    5277                 :            : 
    5278                 :            : static Dwarf_Addr
    5279                 :     125686 : listptr_base (struct listptr *p)
    5280                 :            : {
    5281                 :     125686 :   Dwarf_Die cu = CUDIE (p->cu);
    5282                 :     125686 :   return cudie_base (&cu);
    5283                 :            : }
    5284                 :            : 
    5285                 :            : /* To store the name used in compare_listptr */
    5286                 :            : static const char *sort_listptr_name;
    5287                 :            : 
    5288                 :            : static int
    5289                 :     836304 : compare_listptr (const void *a, const void *b)
    5290                 :            : {
    5291                 :     836304 :   const char *name = sort_listptr_name;
    5292                 :     836304 :   struct listptr *p1 = (void *) a;
    5293                 :     836304 :   struct listptr *p2 = (void *) b;
    5294                 :            : 
    5295         [ +  + ]:     836304 :   if (p1->offset < p2->offset)
    5296                 :            :     return -1;
    5297         [ +  + ]:      81235 :   if (p1->offset > p2->offset)
    5298                 :            :     return 1;
    5299                 :            : 
    5300   [ +  -  +  - ]:       3788 :   if (!p1->warned && !p2->warned)
    5301                 :            :     {
    5302         [ -  + ]:       3788 :       if (p1->addr64 != p2->addr64)
    5303                 :            :         {
    5304                 :          0 :           p1->warned = p2->warned = true;
    5305                 :       3788 :           error (0, 0,
    5306                 :          0 :                  _("%s %#" PRIx64 " used with different address sizes"),
    5307                 :            :                  name, (uint64_t) p1->offset);
    5308                 :            :         }
    5309         [ -  + ]:       3788 :       if (p1->dwarf64 != p2->dwarf64)
    5310                 :            :         {
    5311                 :          0 :           p1->warned = p2->warned = true;
    5312                 :       3788 :           error (0, 0,
    5313                 :          0 :                  _("%s %#" PRIx64 " used with different offset sizes"),
    5314                 :          0 :                  name, (uint64_t) p1->offset);
    5315                 :            :         }
    5316         [ -  + ]:       3788 :       if (listptr_base (p1) != listptr_base (p2))
    5317                 :            :         {
    5318                 :          0 :           p1->warned = p2->warned = true;
    5319                 :       3788 :           error (0, 0,
    5320                 :          0 :                  _("%s %#" PRIx64 " used with different base addresses"),
    5321                 :          0 :                  name, (uint64_t) p1->offset);
    5322                 :            :         }
    5323         [ -  + ]:       3788 :       if (p1->attr != p2 ->attr)
    5324                 :            :         {
    5325                 :          0 :           p1->warned = p2->warned = true;
    5326                 :          0 :           error (0, 0,
    5327                 :          0 :                  _("%s %#" PRIx64
    5328                 :            :                           " used with different attribute %s and %s"),
    5329                 :          0 :                  name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
    5330                 :            :                  dwarf_attr_name (p2->attr));
    5331                 :            :         }
    5332                 :            :     }
    5333                 :            : 
    5334                 :            :   return 0;
    5335                 :            : }
    5336                 :            : 
    5337                 :            : struct listptr_table
    5338                 :            : {
    5339                 :            :   size_t n;
    5340                 :            :   size_t alloc;
    5341                 :            :   struct listptr *table;
    5342                 :            : };
    5343                 :            : 
    5344                 :            : static struct listptr_table known_locsptr;
    5345                 :            : static struct listptr_table known_loclistsptr;
    5346                 :            : static struct listptr_table known_rangelistptr;
    5347                 :            : static struct listptr_table known_rnglistptr;
    5348                 :            : static struct listptr_table known_addrbases;
    5349                 :            : static struct listptr_table known_stroffbases;
    5350                 :            : 
    5351                 :            : static void
    5352                 :        293 : reset_listptr (struct listptr_table *table)
    5353                 :            : {
    5354                 :        293 :   free (table->table);
    5355                 :        293 :   table->table = NULL;
    5356                 :        293 :   table->n = table->alloc = 0;
    5357                 :            : }
    5358                 :            : 
    5359                 :            : /* Returns false if offset doesn't fit.  See struct listptr.  */
    5360                 :            : static bool
    5361                 :     121720 : notice_listptr (enum section_e section, struct listptr_table *table,
    5362                 :            :                 uint_fast8_t address_size, uint_fast8_t offset_size,
    5363                 :            :                 struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
    5364                 :            : {
    5365         [ +  + ]:     121720 :   if (print_debug_sections & section)
    5366                 :            :     {
    5367         [ +  + ]:     121594 :       if (table->n == table->alloc)
    5368                 :            :         {
    5369         [ +  + ]:        200 :           if (table->alloc == 0)
    5370                 :         83 :             table->alloc = 128;
    5371                 :            :           else
    5372                 :        117 :             table->alloc *= 2;
    5373                 :        200 :           table->table = xrealloc (table->table,
    5374                 :        200 :                                    table->alloc * sizeof table->table[0]);
    5375                 :            :         }
    5376                 :            : 
    5377                 :     121594 :       struct listptr *p = &table->table[table->n++];
    5378                 :            : 
    5379                 :     121594 :       *p = (struct listptr)
    5380                 :            :         {
    5381                 :     121594 :           .addr64 = address_size == 8,
    5382                 :     121594 :           .dwarf64 = offset_size == 8,
    5383                 :            :           .offset = offset,
    5384                 :            :           .cu = cu,
    5385                 :            :           .attr = attr
    5386                 :            :         };
    5387                 :            : 
    5388         [ -  + ]:     121594 :       if (p->offset != offset)
    5389                 :            :         {
    5390                 :          0 :           table->n--;
    5391                 :          0 :           return false;
    5392                 :            :         }
    5393                 :            :     }
    5394                 :            :   return true;
    5395                 :            : }
    5396                 :            : 
    5397                 :            : static void
    5398                 :         87 : sort_listptr (struct listptr_table *table, const char *name)
    5399                 :            : {
    5400         [ +  + ]:         87 :   if (table->n > 0)
    5401                 :            :     {
    5402                 :         83 :       sort_listptr_name = name;
    5403                 :         83 :       qsort (table->table, table->n, sizeof table->table[0],
    5404                 :            :              &compare_listptr);
    5405                 :            :     }
    5406                 :         87 : }
    5407                 :            : 
    5408                 :            : static bool
    5409                 :     118113 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
    5410                 :            :                    uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
    5411                 :            :                    Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
    5412                 :            :                    unsigned char **readp, unsigned char *endp,
    5413                 :            :                    unsigned int *attr)
    5414                 :            : {
    5415         [ +  - ]:     118113 :   if (table->n == 0)
    5416                 :            :     return false;
    5417                 :            : 
    5418   [ +  -  +  + ]:     187205 :   while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
    5419                 :      69092 :     ++*idxp;
    5420                 :            : 
    5421                 :     118113 :   struct listptr *p = &table->table[*idxp];
    5422                 :            : 
    5423         [ +  - ]:     118113 :   if (*idxp == table->n
    5424         [ -  + ]:     118113 :       || p->offset >= (Dwarf_Off) (endp - *readp + offset))
    5425                 :            :     {
    5426                 :          0 :       *readp = endp;
    5427                 :          0 :       printf (_(" [%6tx]  <UNUSED GARBAGE IN REST OF SECTION>\n"),
    5428                 :            :               offset);
    5429                 :          0 :       return true;
    5430                 :            :     }
    5431                 :            : 
    5432         [ +  + ]:     118113 :   if (p->offset != (Dwarf_Off) offset)
    5433                 :            :     {
    5434                 :         11 :       *readp += p->offset - offset;
    5435                 :         11 :       printf (_(" [%6tx]  <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
    5436                 :            :               offset, (Dwarf_Off) p->offset - offset);
    5437                 :         11 :       return true;
    5438                 :            :     }
    5439                 :            : 
    5440         [ +  - ]:     118102 :   if (address_sizep != NULL)
    5441         [ +  + ]:     118110 :     *address_sizep = listptr_address_size (p);
    5442         [ +  + ]:     118102 :   if (offset_sizep != NULL)
    5443         [ +  - ]:     208048 :     *offset_sizep = listptr_offset_size (p);
    5444         [ +  - ]:     118102 :   if (base != NULL)
    5445                 :     118102 :     *base = listptr_base (p);
    5446         [ +  - ]:     118102 :   if (cu != NULL)
    5447                 :     118102 :     *cu = p->cu;
    5448         [ +  + ]:     118102 :   if (attr != NULL)
    5449                 :     104024 :     *attr = p->attr;
    5450                 :            : 
    5451                 :            :   return false;
    5452                 :            : }
    5453                 :            : 
    5454                 :            : static Dwarf_Off
    5455                 :      51976 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
    5456                 :            : {
    5457                 :            :   /* Note that multiple attributes could in theory point to the same loclist
    5458                 :            :      offset, so make sure we pick one that is bigger than the current one.
    5459                 :            :      The table is sorted on offset.  */
    5460         [ +  - ]:      51976 :   if (*idxp < table->n)
    5461                 :            :     {
    5462         [ +  - ]:      52399 :       while (++*idxp < table->n)
    5463                 :            :         {
    5464                 :      52399 :           Dwarf_Off next = table->table[*idxp].offset;
    5465         [ +  + ]:      52399 :           if (next > off)
    5466                 :      51976 :             return next;
    5467                 :            :         }
    5468                 :            :     }
    5469                 :            :   return 0;
    5470                 :            : }
    5471                 :            : 
    5472                 :            : /* Returns the listptr associated with the given index, or NULL.  */
    5473                 :            : static struct listptr *
    5474                 :        356 : get_listptr (struct listptr_table *table, size_t idx)
    5475                 :            : {
    5476                 :        356 :   if (idx >= table->n)
    5477                 :            :     return NULL;
    5478                 :        303 :   return &table->table[idx];
    5479                 :            : }
    5480                 :            : 
    5481                 :            : /* Returns the next index, base address and CU associated with the
    5482                 :            :    list unit offsets.  If there is none false is returned, otherwise
    5483                 :            :    true.  Assumes the table has been sorted.  */
    5484                 :            : static bool
    5485                 :          8 : listptr_cu (struct listptr_table *table, size_t *idxp,
    5486                 :            :             Dwarf_Off start, Dwarf_Off end,
    5487                 :            :             Dwarf_Addr *base, struct Dwarf_CU **cu)
    5488                 :            : {
    5489         [ +  - ]:         17 :   while (*idxp < table->n
    5490         [ +  + ]:         17 :          && table->table[*idxp].offset < start)
    5491                 :          9 :     ++*idxp;
    5492                 :            : 
    5493         [ +  - ]:          8 :   if (*idxp < table->n
    5494         [ +  - ]:          8 :       && table->table[*idxp].offset >= start
    5495         [ +  - ]:          8 :       && table->table[*idxp].offset < end)
    5496                 :            :     {
    5497                 :          8 :       struct listptr *p = &table->table[*idxp];
    5498                 :          8 :       *base = listptr_base (p);
    5499                 :          8 :       *cu = p->cu;
    5500                 :          8 :       return true;
    5501                 :            :     }
    5502                 :            : 
    5503                 :            :   return false;
    5504                 :            : }
    5505                 :            : 
    5506                 :            : /* Returns the next index with the current CU for the given attribute.
    5507                 :            :    If there is none false is returned, otherwise true.  Assumes the
    5508                 :            :    table has been sorted.  */
    5509                 :            : static bool
    5510                 :         92 : listptr_attr (struct listptr_table *table, size_t idxp,
    5511                 :            :               Dwarf_Off offset, unsigned int attr)
    5512                 :            : {
    5513                 :        346 :   struct listptr *listptr;
    5514                 :        346 :   do
    5515                 :            :     {
    5516         [ +  + ]:        346 :       listptr = get_listptr (table, idxp);
    5517         [ +  - ]:        298 :       if (listptr == NULL)
    5518                 :            :         return false;
    5519                 :            : 
    5520   [ +  +  +  + ]:        298 :       if (listptr->offset == offset && listptr->attr == attr)
    5521                 :            :         return true;
    5522                 :            : 
    5523                 :        297 :       idxp++;
    5524                 :            :     }
    5525         [ +  + ]:        297 :   while (listptr->offset <= offset);
    5526                 :            : 
    5527                 :            :   return false;
    5528                 :            : }
    5529                 :            : 
    5530                 :            : static void
    5531                 :         42 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    5532                 :            :                             Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
    5533                 :            :                             Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    5534                 :            : {
    5535                 :         42 :   Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
    5536         [ +  - ]:         42 :   if (elf_data == NULL)
    5537                 :            :     return;
    5538                 :            : 
    5539                 :         42 :   const size_t sh_size = elf_data->d_size;
    5540                 :            : 
    5541                 :         42 :   printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
    5542                 :            :                    " [ Code]\n"),
    5543                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    5544                 :         42 :           (uint64_t) shdr->sh_offset);
    5545                 :            : 
    5546                 :         42 :   Dwarf_Off offset = 0;
    5547         [ +  + ]:       1738 :   while (offset < sh_size)
    5548                 :            :     {
    5549                 :       1696 :       printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
    5550                 :            :               offset);
    5551                 :            : 
    5552                 :     152562 :       while (1)
    5553                 :      75433 :         {
    5554                 :      77129 :           size_t length;
    5555                 :      77129 :           Dwarf_Abbrev abbrev;
    5556                 :            : 
    5557                 :      77129 :           int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
    5558         [ +  + ]:      77129 :           if (res != 0)
    5559                 :            :             {
    5560         [ -  + ]:       1696 :               if (unlikely (res < 0))
    5561                 :            :                 {
    5562                 :          0 :                   printf (_("\
    5563                 :            :  *** error while reading abbreviation: %s\n"),
    5564                 :            :                           dwarf_errmsg (-1));
    5565                 :          0 :                   return;
    5566                 :            :                 }
    5567                 :            : 
    5568                 :            :               /* This is the NUL byte at the end of the section.  */
    5569                 :       1696 :               ++offset;
    5570                 :       1696 :               break;
    5571                 :            :             }
    5572                 :            : 
    5573                 :            :           /* We know these calls can never fail.  */
    5574                 :      75433 :           unsigned int code = dwarf_getabbrevcode (&abbrev);
    5575                 :      75433 :           unsigned int tag = dwarf_getabbrevtag (&abbrev);
    5576                 :      75433 :           int has_children = dwarf_abbrevhaschildren (&abbrev);
    5577                 :            : 
    5578         [ +  + ]:      75433 :           printf (_(" [%5u] offset: %" PRId64
    5579                 :            :                            ", children: %s, tag: %s\n"),
    5580                 :            :                   code, (int64_t) offset,
    5581                 :            :                   has_children ? yes_str : no_str,
    5582                 :            :                   dwarf_tag_name (tag));
    5583                 :            : 
    5584                 :      75433 :           size_t cnt = 0;
    5585                 :      75433 :           unsigned int name;
    5586                 :      75433 :           unsigned int form;
    5587                 :      75433 :           Dwarf_Sword data;
    5588                 :      75433 :           Dwarf_Off enoffset;
    5589         [ +  + ]:     428618 :           while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
    5590                 :            :                                            &data, &enoffset) == 0)
    5591                 :            :             {
    5592                 :     353185 :               printf ("          attr: %s, form: %s",
    5593                 :            :                       dwarf_attr_name (name), dwarf_form_name (form));
    5594         [ -  + ]:     353185 :               if (form == DW_FORM_implicit_const)
    5595                 :          0 :                 printf (" (%" PRId64 ")", data);
    5596                 :     353185 :               printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
    5597                 :     353185 :               ++cnt;
    5598                 :            :             }
    5599                 :            : 
    5600                 :      75433 :           offset += length;
    5601                 :            :         }
    5602                 :            :     }
    5603                 :            : }
    5604                 :            : 
    5605                 :            : 
    5606                 :            : static void
    5607                 :          2 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    5608                 :            :                           Ebl *ebl, GElf_Ehdr *ehdr,
    5609                 :            :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    5610                 :            : {
    5611                 :          2 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
    5612         [ +  - ]:          2 :   if (data == NULL)
    5613                 :            :     return;
    5614                 :            : 
    5615                 :          2 :   printf (_("\
    5616                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    5617                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    5618                 :          2 :           (uint64_t) shdr->sh_offset);
    5619                 :            : 
    5620         [ +  - ]:          2 :   if (shdr->sh_size == 0)
    5621                 :            :     return;
    5622                 :            : 
    5623                 :          2 :   size_t idx = 0;
    5624                 :          2 :   sort_listptr (&known_addrbases, "addr_base");
    5625                 :            : 
    5626                 :          2 :   const unsigned char *start = (const unsigned char *) data->d_buf;
    5627                 :          2 :   const unsigned char *readp = start;
    5628                 :          2 :   const unsigned char *readendp = ((const unsigned char *) data->d_buf
    5629                 :          2 :                                    + data->d_size);
    5630                 :            : 
    5631                 :          2 :   while (readp < readendp)
    5632                 :            :     {
    5633                 :            :       /* We cannot really know whether or not there is an header.  The
    5634                 :            :          DebugFission extension to DWARF4 doesn't add one.  The DWARF5
    5635                 :            :          .debug_addr variant does.  Whether or not we have an header,
    5636                 :            :          DW_AT_[GNU_]addr_base points at "index 0".  So if the current
    5637                 :            :          offset equals the CU addr_base then we can just start
    5638                 :            :          printing addresses.  If there is no CU with an exact match
    5639                 :            :          then we'll try to parse the header first.  */
    5640                 :          4 :       Dwarf_Off off = (Dwarf_Off) (readp
    5641                 :          4 :                                    - (const unsigned char *) data->d_buf);
    5642                 :            : 
    5643                 :          4 :       printf ("Table at offset %" PRIx64 " ", off);
    5644                 :            : 
    5645         [ +  - ]:          4 :       struct listptr *listptr = get_listptr (&known_addrbases, idx++);
    5646                 :          4 :       const unsigned char *next_unitp;
    5647                 :            : 
    5648                 :          4 :       uint64_t unit_length;
    5649                 :          4 :       uint16_t version;
    5650                 :          4 :       uint8_t address_size;
    5651                 :          4 :       uint8_t segment_size;
    5652         [ -  + ]:          4 :       if (listptr == NULL)
    5653                 :            :         {
    5654                 :          0 :           error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
    5655                 :            :                  off);
    5656                 :            : 
    5657                 :            :           /* We will have to assume it is just addresses to the end... */
    5658         [ #  # ]:          0 :           address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    5659                 :          0 :           next_unitp = readendp;
    5660                 :          0 :           printf ("Unknown CU:\n");
    5661                 :            :         }
    5662                 :            :       else
    5663                 :            :         {
    5664                 :          4 :           Dwarf_Die cudie;
    5665         [ -  + ]:          4 :           if (dwarf_cu_die (listptr->cu, &cudie,
    5666                 :            :                             NULL, NULL, NULL, NULL,
    5667                 :            :                             NULL, NULL) == NULL)
    5668                 :          0 :             printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
    5669                 :            :           else
    5670                 :          4 :             printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
    5671                 :            : 
    5672         [ +  + ]:          4 :           if (listptr->offset == off)
    5673                 :            :             {
    5674         [ -  + ]:          2 :               address_size = listptr_address_size (listptr);
    5675                 :          2 :               segment_size = 0;
    5676                 :          2 :               version = 4;
    5677                 :            : 
    5678                 :            :               /* The addresses start here, but where do they end?  */
    5679         [ +  + ]:          2 :               listptr = get_listptr (&known_addrbases, idx);
    5680         [ +  - ]:          1 :               if (listptr == NULL)
    5681                 :            :                 next_unitp = readendp;
    5682         [ +  - ]:          1 :               else if (listptr->cu->version < 5)
    5683                 :            :                 {
    5684                 :          1 :                   next_unitp = start + listptr->offset;
    5685   [ +  -  -  + ]:          1 :                   if (listptr->offset < off || listptr->offset > data->d_size)
    5686                 :            :                     {
    5687                 :          0 :                       error (0, 0,
    5688                 :            :                              "Warning: Bad address base for next unit at %"
    5689                 :            :                              PRIx64, off);
    5690                 :          0 :                       next_unitp = readendp;
    5691                 :            :                     }
    5692                 :            :                 }
    5693                 :            :               else
    5694                 :            :                 {
    5695                 :            :                   /* Tricky, we don't have a header for this unit, but
    5696                 :            :                      there is one for the next.  We will have to
    5697                 :            :                      "guess" how big it is and subtract it from the
    5698                 :            :                      offset (because that points after the header).  */
    5699         [ #  # ]:          0 :                   unsigned int offset_size = listptr_offset_size (listptr);
    5700                 :          0 :                   Dwarf_Off next_off = (listptr->offset
    5701                 :          0 :                                         - (offset_size == 4 ? 4 : 12) /* len */
    5702                 :            :                                         - 2 /* version */
    5703                 :            :                                         - 1 /* address size */
    5704                 :          0 :                                         - 1); /* segment selector size */
    5705                 :          0 :                   next_unitp = start + next_off;
    5706   [ #  #  #  # ]:          0 :                   if (next_off < off || next_off > data->d_size)
    5707                 :            :                     {
    5708                 :          0 :                       error (0, 0,
    5709                 :            :                              "Warning: Couldn't calculate .debug_addr "
    5710                 :            :                              " unit length at %" PRIx64, off);
    5711                 :          0 :                       next_unitp = readendp;
    5712                 :            :                     }
    5713                 :            :                 }
    5714                 :          2 :               unit_length = (uint64_t) (next_unitp - readp);
    5715                 :            : 
    5716                 :            :               /* Pretend we have a header.  */
    5717                 :          2 :               printf ("\n");
    5718                 :          2 :               printf (_(" Length:         %8" PRIu64 "\n"),
    5719                 :            :                       unit_length);
    5720                 :          2 :               printf (_(" DWARF version:  %8" PRIu16 "\n"), version);
    5721                 :          2 :               printf (_(" Address size:   %8" PRIu64 "\n"),
    5722                 :            :                       (uint64_t) address_size);
    5723                 :          2 :               printf (_(" Segment size:   %8" PRIu64 "\n"),
    5724                 :            :                       (uint64_t) segment_size);
    5725                 :          4 :               printf ("\n");
    5726                 :            :             }
    5727                 :            :           else
    5728                 :            :             {
    5729                 :            :               /* OK, we have to parse an header first.  */
    5730         [ -  + ]:          2 :               unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    5731         [ -  + ]:          2 :               if (unlikely (unit_length == 0xffffffff))
    5732                 :            :                 {
    5733         [ #  # ]:          0 :                   if (unlikely (readp > readendp - 8))
    5734                 :            :                     {
    5735                 :          0 :                     invalid_data:
    5736                 :          0 :                       error (0, 0, "Invalid data");
    5737                 :          0 :                       return;
    5738                 :            :                     }
    5739         [ #  # ]:          0 :                   unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    5740                 :            :                 }
    5741                 :          2 :               printf ("\n");
    5742                 :          2 :               printf (_(" Length:         %8" PRIu64 "\n"),
    5743                 :            :                       unit_length);
    5744                 :            : 
    5745                 :            :               /* We need at least 2-bytes (version) + 1-byte
    5746                 :            :                  (addr_size) + 1-byte (segment_size) = 4 bytes to
    5747                 :            :                  complete the header.  And this unit cannot go beyond
    5748                 :            :                  the section data.  */
    5749         [ +  - ]:          2 :               if (readp > readendp - 4
    5750         [ +  - ]:          2 :                   || unit_length < 4
    5751         [ -  + ]:          2 :                   || unit_length > (uint64_t) (readendp - readp))
    5752                 :          0 :                 goto invalid_data;
    5753                 :            : 
    5754                 :          2 :               next_unitp = readp + unit_length;
    5755                 :            : 
    5756         [ -  + ]:          2 :               version = read_2ubyte_unaligned_inc (dbg, readp);
    5757                 :          2 :               printf (_(" DWARF version:  %8" PRIu16 "\n"), version);
    5758                 :            : 
    5759         [ -  + ]:          2 :               if (version != 5)
    5760                 :            :                 {
    5761                 :          0 :                   error (0, 0, _("Unknown version"));
    5762                 :          0 :                   goto next_unit;
    5763                 :            :                 }
    5764                 :            : 
    5765                 :          2 :               address_size = *readp++;
    5766                 :          2 :               printf (_(" Address size:   %8" PRIu64 "\n"),
    5767                 :            :                       (uint64_t) address_size);
    5768                 :            : 
    5769         [ -  + ]:          2 :               if (address_size != 4 && address_size != 8)
    5770                 :            :                 {
    5771                 :          0 :                   error (0, 0, _("unsupported address size"));
    5772                 :          0 :                   goto next_unit;
    5773                 :            :                 }
    5774                 :            : 
    5775                 :          2 :               segment_size = *readp++;
    5776                 :          2 :               printf (_(" Segment size:   %8" PRIu64 "\n"),
    5777                 :            :                       (uint64_t) segment_size);
    5778                 :          2 :               printf ("\n");
    5779                 :            : 
    5780         [ -  + ]:          2 :               if (segment_size != 0)
    5781                 :            :                 {
    5782                 :          0 :                   error (0, 0, _("unsupported segment size"));
    5783                 :          0 :                   goto next_unit;
    5784                 :            :                 }
    5785                 :            : 
    5786         [ -  + ]:          2 :               if (listptr->offset != (Dwarf_Off) (readp - start))
    5787                 :            :                 {
    5788                 :          0 :                   error (0, 0, "Address index doesn't start after header");
    5789                 :          0 :                   goto next_unit;
    5790                 :            :                 }
    5791                 :            :             }
    5792                 :            :         }
    5793                 :            : 
    5794                 :          4 :       int digits = 1;
    5795                 :          4 :       size_t addresses = (next_unitp - readp) / address_size;
    5796         [ +  + ]:          8 :       while (addresses >= 10)
    5797                 :            :         {
    5798                 :          4 :           ++digits;
    5799                 :          4 :           addresses /= 10;
    5800                 :            :         }
    5801                 :            : 
    5802                 :          4 :       unsigned int uidx = 0;
    5803                 :          4 :       size_t index_offset =  readp - (const unsigned char *) data->d_buf;
    5804                 :          4 :       printf (" Addresses start at offset 0x%zx:\n", index_offset);
    5805                 :         76 :       while (readp <= next_unitp - address_size)
    5806                 :            :         {
    5807   [ -  +  -  +  :         72 :           Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
             -  -  -  + ]
    5808                 :            :                                                      readp);
    5809                 :         72 :           printf (" [%*u] ", digits, uidx++);
    5810                 :         72 :           print_dwarf_addr (dwflmod, address_size, addr, addr);
    5811         [ +  + ]:         76 :           printf ("\n");
    5812                 :            :         }
    5813                 :          4 :       printf ("\n");
    5814                 :            : 
    5815         [ +  - ]:          4 :       if (readp != next_unitp)
    5816         [ +  + ]:          6 :         error (0, 0, "extra %zd bytes at end of unit",
    5817                 :          0 :                (size_t) (next_unitp - readp));
    5818                 :            : 
    5819                 :          4 :     next_unit:
    5820                 :            :       readp = next_unitp;
    5821                 :            :     }
    5822                 :            : }
    5823                 :            : 
    5824                 :            : /* Print content of DWARF .debug_aranges section.  We fortunately do
    5825                 :            :    not have to know a bit about the structure of the section, libdwarf
    5826                 :            :    takes care of it.  */
    5827                 :            : static void
    5828                 :          1 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
    5829                 :            :                                GElf_Shdr *shdr, Dwarf *dbg)
    5830                 :            : {
    5831                 :          1 :   Dwarf_Aranges *aranges;
    5832                 :          1 :   size_t cnt;
    5833         [ -  + ]:          1 :   if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
    5834                 :            :     {
    5835                 :          0 :       error (0, 0, _("cannot get .debug_aranges content: %s"),
    5836                 :            :              dwarf_errmsg (-1));
    5837                 :          0 :       return;
    5838                 :            :     }
    5839                 :            : 
    5840                 :          1 :   GElf_Shdr glink_mem;
    5841                 :          1 :   GElf_Shdr *glink;
    5842                 :          1 :   glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
    5843         [ -  + ]:          1 :   if (glink == NULL)
    5844                 :            :     {
    5845                 :          0 :       error (0, 0, _("invalid sh_link value in section %zu"),
    5846                 :            :              elf_ndxscn (scn));
    5847                 :          0 :       return;
    5848                 :            :     }
    5849                 :            : 
    5850                 :          1 :   printf (ngettext ("\
    5851                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
    5852                 :            :                     "\
    5853                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
    5854                 :            :                     cnt),
    5855                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    5856                 :          1 :           (uint64_t) shdr->sh_offset, cnt);
    5857                 :            : 
    5858                 :            :   /* Compute floor(log16(cnt)).  */
    5859                 :          1 :   size_t tmp = cnt;
    5860                 :          1 :   int digits = 1;
    5861         [ -  + ]:          1 :   while (tmp >= 16)
    5862                 :            :     {
    5863                 :          0 :       ++digits;
    5864                 :          0 :       tmp >>= 4;
    5865                 :            :     }
    5866                 :            : 
    5867         [ +  + ]:          6 :   for (size_t n = 0; n < cnt; ++n)
    5868                 :            :     {
    5869                 :          5 :       Dwarf_Arange *runp = dwarf_onearange (aranges, n);
    5870         [ -  + ]:          5 :       if (unlikely (runp == NULL))
    5871                 :            :         {
    5872                 :          0 :           printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
    5873                 :          0 :           return;
    5874                 :            :         }
    5875                 :            : 
    5876                 :          5 :       Dwarf_Addr start;
    5877                 :          5 :       Dwarf_Word length;
    5878                 :          5 :       Dwarf_Off offset;
    5879                 :            : 
    5880         [ -  + ]:          5 :       if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
    5881                 :          0 :         printf (_(" [%*zu] ???\n"), digits, n);
    5882                 :            :       else
    5883                 :          5 :         printf (_(" [%*zu] start: %0#*" PRIx64
    5884                 :            :                          ", length: %5" PRIu64 ", CU DIE offset: %6"
    5885                 :            :                          PRId64 "\n"),
    5886         [ -  + ]:          5 :                 digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
    5887                 :            :                 (uint64_t) start, (uint64_t) length, (int64_t) offset);
    5888                 :            :     }
    5889                 :            : }
    5890                 :            : 
    5891                 :            : 
    5892                 :            : /* Print content of DWARF .debug_aranges section.  */
    5893                 :            : static void
    5894                 :         46 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    5895                 :            :                              Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
    5896                 :            :                              GElf_Shdr *shdr, Dwarf *dbg)
    5897                 :            : {
    5898         [ +  + ]:         46 :   if (decodedaranges)
    5899                 :            :     {
    5900                 :          1 :       print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
    5901                 :          1 :       return;
    5902                 :            :     }
    5903                 :            : 
    5904                 :         45 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
    5905         [ +  - ]:         45 :   if (data == NULL)
    5906                 :            :     return;
    5907                 :            : 
    5908                 :         45 :   printf (_("\
    5909                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    5910                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    5911                 :         45 :           (uint64_t) shdr->sh_offset);
    5912                 :            : 
    5913                 :         45 :   const unsigned char *readp = data->d_buf;
    5914                 :         45 :   const unsigned char *readendp = readp + data->d_size;
    5915                 :            : 
    5916         [ +  + ]:       1738 :   while (readp < readendp)
    5917                 :            :     {
    5918                 :       1693 :       const unsigned char *hdrstart = readp;
    5919                 :       1693 :       size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
    5920                 :            : 
    5921                 :       1693 :       printf (_("\nTable at offset %zu:\n"), start_offset);
    5922         [ -  + ]:       1693 :       if (readp + 4 > readendp)
    5923                 :            :         {
    5924                 :          0 :         invalid_data:
    5925                 :          0 :           error (0, 0, _("invalid data in section [%zu] '%s'"),
    5926                 :            :                  elf_ndxscn (scn), section_name (ebl, shdr));
    5927                 :          0 :           return;
    5928                 :            :         }
    5929                 :            : 
    5930         [ +  + ]:       1693 :       Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
    5931                 :       1693 :       unsigned int length_bytes = 4;
    5932         [ -  + ]:       1693 :       if (length == DWARF3_LENGTH_64_BIT)
    5933                 :            :         {
    5934         [ #  # ]:          0 :           if (readp + 8 > readendp)
    5935                 :          0 :             goto invalid_data;
    5936         [ #  # ]:          0 :           length = read_8ubyte_unaligned_inc (dbg, readp);
    5937                 :          0 :           length_bytes = 8;
    5938                 :            :         }
    5939                 :            : 
    5940                 :       1693 :       const unsigned char *nexthdr = readp + length;
    5941                 :       1693 :       printf (_("\n Length:        %6" PRIu64 "\n"),
    5942                 :            :               (uint64_t) length);
    5943                 :            : 
    5944         [ -  + ]:       1693 :       if (unlikely (length > (size_t) (readendp - readp)))
    5945                 :          0 :         goto invalid_data;
    5946                 :            : 
    5947         [ -  + ]:       1693 :       if (length == 0)
    5948                 :          0 :         continue;
    5949                 :            : 
    5950         [ -  + ]:       1693 :       if (readp + 2 > readendp)
    5951                 :          0 :         goto invalid_data;
    5952         [ +  + ]:       1693 :       uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    5953                 :       1693 :       printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
    5954                 :            :               version);
    5955         [ -  + ]:       1693 :       if (version != 2)
    5956                 :            :         {
    5957                 :          0 :           error (0, 0, _("unsupported aranges version"));
    5958                 :          0 :           goto next_table;
    5959                 :            :         }
    5960                 :            : 
    5961                 :       1693 :       Dwarf_Word offset;
    5962         [ -  + ]:       1693 :       if (readp + length_bytes > readendp)
    5963                 :          0 :         goto invalid_data;
    5964         [ -  + ]:       1693 :       if (length_bytes == 8)
    5965         [ #  # ]:          0 :         offset = read_8ubyte_unaligned_inc (dbg, readp);
    5966                 :            :       else
    5967         [ +  + ]:       1693 :         offset = read_4ubyte_unaligned_inc (dbg, readp);
    5968                 :       1693 :       printf (_(" CU offset:     %6" PRIx64 "\n"),
    5969                 :            :               (uint64_t) offset);
    5970                 :            : 
    5971         [ -  + ]:       1693 :       if (readp + 1 > readendp)
    5972                 :          0 :         goto invalid_data;
    5973                 :       1693 :       unsigned int address_size = *readp++;
    5974                 :       1693 :       printf (_(" Address size:  %6" PRIu64 "\n"),
    5975                 :            :               (uint64_t) address_size);
    5976         [ -  + ]:       1693 :       if (address_size != 4 && address_size != 8)
    5977                 :            :         {
    5978                 :          0 :           error (0, 0, _("unsupported address size"));
    5979                 :          0 :           goto next_table;
    5980                 :            :         }
    5981                 :            : 
    5982         [ -  + ]:       1693 :       if (readp + 1 > readendp)
    5983                 :          0 :         goto invalid_data;
    5984                 :       1693 :       unsigned int segment_size = *readp++;
    5985                 :       1693 :       printf (_(" Segment size:  %6" PRIu64 "\n\n"),
    5986                 :            :               (uint64_t) segment_size);
    5987   [ -  +  -  - ]:       1693 :       if (segment_size != 0 && segment_size != 4 && segment_size != 8)
    5988                 :            :         {
    5989                 :          0 :           error (0, 0, _("unsupported segment size"));
    5990                 :          0 :           goto next_table;
    5991                 :            :         }
    5992                 :            : 
    5993                 :            :       /* Round the address to the next multiple of 2*address_size.  */
    5994                 :       1693 :       readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
    5995                 :       1693 :                 % (2 * address_size));
    5996                 :            : 
    5997                 :       1693 :       while (readp < nexthdr)
    5998                 :            :         {
    5999                 :       3435 :           Dwarf_Word range_address;
    6000                 :       3435 :           Dwarf_Word range_length;
    6001                 :       3435 :           Dwarf_Word segment = 0;
    6002         [ -  + ]:       3435 :           if (readp + 2 * address_size + segment_size > readendp)
    6003                 :          0 :             goto invalid_data;
    6004         [ +  + ]:       3435 :           if (address_size == 4)
    6005                 :            :             {
    6006         [ +  + ]:         46 :               range_address = read_4ubyte_unaligned_inc (dbg, readp);
    6007         [ +  + ]:         46 :               range_length = read_4ubyte_unaligned_inc (dbg, readp);
    6008                 :            :             }
    6009                 :            :           else
    6010                 :            :             {
    6011         [ +  + ]:       3389 :               range_address = read_8ubyte_unaligned_inc (dbg, readp);
    6012         [ +  + ]:       3389 :               range_length = read_8ubyte_unaligned_inc (dbg, readp);
    6013                 :            :             }
    6014                 :            : 
    6015         [ -  + ]:       3435 :           if (segment_size == 4)
    6016         [ #  # ]:          0 :             segment = read_4ubyte_unaligned_inc (dbg, readp);
    6017         [ -  + ]:       3435 :           else if (segment_size == 8)
    6018         [ #  # ]:          0 :             segment = read_8ubyte_unaligned_inc (dbg, readp);
    6019                 :            : 
    6020         [ +  + ]:       3435 :           if (range_address == 0 && range_length == 0 && segment == 0)
    6021                 :            :             break;
    6022                 :            : 
    6023                 :       1742 :           printf ("   ");
    6024                 :       1742 :           print_dwarf_addr (dwflmod, address_size, range_address,
    6025                 :            :                             range_address);
    6026                 :       1742 :           printf ("..");
    6027                 :       1742 :           print_dwarf_addr (dwflmod, address_size,
    6028                 :       1742 :                             range_address + range_length - 1,
    6029                 :            :                             range_length);
    6030         [ -  + ]:       1742 :           if (segment_size != 0)
    6031                 :          0 :             printf (" (%" PRIx64 ")\n", (uint64_t) segment);
    6032                 :            :           else
    6033         [ +  - ]:       5177 :             printf ("\n");
    6034                 :            :         }
    6035                 :            : 
    6036                 :       1693 :     next_table:
    6037         [ -  + ]:       1693 :       if (readp != nexthdr)
    6038                 :            :         {
    6039                 :          0 :           size_t padding = nexthdr - readp;
    6040                 :          0 :           printf (_("   %zu padding bytes\n"), padding);
    6041                 :          0 :           readp = nexthdr;
    6042                 :            :         }
    6043                 :            :     }
    6044                 :            : }
    6045                 :            : 
    6046                 :            : 
    6047                 :            : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
    6048                 :            : 
    6049                 :            : /* Returns true and sets cu and cu_base if the given Dwarf is a split
    6050                 :            :    DWARF (.dwo) file.  */
    6051                 :            : static bool
    6052                 :          0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
    6053                 :            : {
    6054                 :          0 :   uint64_t id;
    6055         [ #  # ]:          0 :   if (is_split_dwarf (dbg, &id, cu))
    6056                 :            :     {
    6057                 :          0 :       Dwarf_Die cudie;
    6058         [ #  # ]:          0 :       if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
    6059                 :            :         {
    6060                 :          0 :           *cu_base = cudie_base (&cudie);
    6061                 :          0 :           return true;
    6062                 :            :         }
    6063                 :            :     }
    6064                 :            :   return false;
    6065                 :            : }
    6066                 :            : 
    6067                 :            : /* Print content of DWARF .debug_rnglists section.  */
    6068                 :            : static void
    6069                 :          3 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
    6070                 :            :                               Ebl *ebl,
    6071                 :            :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
    6072                 :            :                               Elf_Scn *scn, GElf_Shdr *shdr,
    6073                 :            :                               Dwarf *dbg __attribute__((unused)))
    6074                 :            : {
    6075                 :          3 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
    6076         [ +  - ]:          3 :   if (data == NULL)
    6077                 :          0 :     return;
    6078                 :            : 
    6079                 :          3 :   printf (_("\
    6080                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    6081                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    6082                 :          3 :           (uint64_t) shdr->sh_offset);
    6083                 :            : 
    6084                 :            :   /* For the listptr to get the base address/CU.  */
    6085                 :          3 :   sort_listptr (&known_rnglistptr, "rnglistptr");
    6086                 :          3 :   size_t listptr_idx = 0;
    6087                 :            : 
    6088                 :          3 :   const unsigned char *readp = data->d_buf;
    6089                 :          3 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    6090                 :          3 :                                         + data->d_size);
    6091         [ +  + ]:          6 :   while (readp < dataend)
    6092                 :            :     {
    6093         [ -  + ]:          3 :       if (unlikely (readp > dataend - 4))
    6094                 :            :         {
    6095                 :          0 :         invalid_data:
    6096                 :          0 :           error (0, 0, _("invalid data in section [%zu] '%s'"),
    6097                 :            :                  elf_ndxscn (scn), section_name (ebl, shdr));
    6098                 :          0 :           return;
    6099                 :            :         }
    6100                 :            : 
    6101                 :          3 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    6102                 :          3 :       printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
    6103                 :            :               (uint64_t) offset);
    6104                 :            : 
    6105         [ -  + ]:          3 :       uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    6106                 :          3 :       unsigned int offset_size = 4;
    6107         [ -  + ]:          3 :       if (unlikely (unit_length == 0xffffffff))
    6108                 :            :         {
    6109         [ #  # ]:          0 :           if (unlikely (readp > dataend - 8))
    6110                 :          0 :             goto invalid_data;
    6111                 :            : 
    6112         [ #  # ]:          0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    6113                 :          0 :           offset_size = 8;
    6114                 :            :         }
    6115                 :          3 :       printf (_(" Length:         %8" PRIu64 "\n"), unit_length);
    6116                 :            : 
    6117                 :            :       /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
    6118                 :            :          bytes to complete the header.  And this unit cannot go beyond
    6119                 :            :          the section data.  */
    6120         [ +  - ]:          3 :       if (readp > dataend - 8
    6121         [ +  - ]:          3 :           || unit_length < 8
    6122         [ -  + ]:          3 :           || unit_length > (uint64_t) (dataend - readp))
    6123                 :          0 :         goto invalid_data;
    6124                 :            : 
    6125                 :          3 :       const unsigned char *nexthdr = readp + unit_length;
    6126                 :            : 
    6127         [ -  + ]:          3 :       uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    6128                 :          3 :       printf (_(" DWARF version:  %8" PRIu16 "\n"), version);
    6129                 :            : 
    6130         [ -  + ]:          3 :       if (version != 5)
    6131                 :            :         {
    6132                 :          0 :           error (0, 0, _("Unknown version"));
    6133                 :          0 :           goto next_table;
    6134                 :            :         }
    6135                 :            : 
    6136                 :          3 :       uint8_t address_size = *readp++;
    6137                 :          3 :       printf (_(" Address size:   %8" PRIu64 "\n"),
    6138                 :            :               (uint64_t) address_size);
    6139                 :            : 
    6140         [ -  + ]:          3 :       if (address_size != 4 && address_size != 8)
    6141                 :            :         {
    6142                 :          0 :           error (0, 0, _("unsupported address size"));
    6143                 :          0 :           goto next_table;
    6144                 :            :         }
    6145                 :            : 
    6146                 :          3 :       uint8_t segment_size = *readp++;
    6147                 :          3 :       printf (_(" Segment size:   %8" PRIu64 "\n"),
    6148                 :            :               (uint64_t) segment_size);
    6149                 :            : 
    6150   [ -  +  -  - ]:          3 :       if (segment_size != 0 && segment_size != 4 && segment_size != 8)
    6151                 :            :         {
    6152                 :          0 :           error (0, 0, _("unsupported segment size"));
    6153                 :          0 :           goto next_table;
    6154                 :            :         }
    6155                 :            : 
    6156         [ -  + ]:          3 :       uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
    6157                 :          3 :       printf (_(" Offset entries: %8" PRIu64 "\n"),
    6158                 :            :               (uint64_t) offset_entry_count);
    6159                 :            : 
    6160                 :            :       /* We need the CU that uses this unit to get the initial base address. */
    6161                 :          3 :       Dwarf_Addr cu_base = 0;
    6162                 :          3 :       struct Dwarf_CU *cu = NULL;
    6163         [ -  + ]:          3 :       if (listptr_cu (&known_rnglistptr, &listptr_idx,
    6164                 :            :                       (Dwarf_Off) offset,
    6165                 :          3 :                       (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
    6166                 :            :                       &cu_base, &cu)
    6167         [ #  # ]:          0 :           || split_dwarf_cu_base (dbg, &cu, &cu_base))
    6168                 :          3 :         {
    6169                 :          3 :           Dwarf_Die cudie;
    6170         [ -  + ]:          3 :           if (dwarf_cu_die (cu, &cudie,
    6171                 :            :                             NULL, NULL, NULL, NULL,
    6172                 :            :                             NULL, NULL) == NULL)
    6173                 :          0 :             printf (_(" Unknown CU base: "));
    6174                 :            :           else
    6175                 :          3 :             printf (_(" CU [%6" PRIx64 "] base: "),
    6176                 :            :                     dwarf_dieoffset (&cudie));
    6177                 :          3 :           print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
    6178                 :          3 :           printf ("\n");
    6179                 :            :         }
    6180                 :            :       else
    6181                 :          0 :         printf (_(" Not associated with a CU.\n"));
    6182                 :            : 
    6183                 :          3 :       printf ("\n");
    6184                 :            : 
    6185                 :          3 :       const unsigned char *offset_array_start = readp;
    6186         [ +  + ]:          3 :       if (offset_entry_count > 0)
    6187                 :            :         {
    6188                 :          1 :           uint64_t max_entries = (unit_length - 8) / offset_size;
    6189         [ -  + ]:          1 :           if (offset_entry_count > max_entries)
    6190                 :            :             {
    6191                 :          0 :               error (0, 0,
    6192                 :          0 :                      _("too many offset entries for unit length"));
    6193                 :          0 :               offset_entry_count = max_entries;
    6194                 :            :             }
    6195                 :            : 
    6196                 :          1 :           printf (_("  Offsets starting at 0x%" PRIx64 ":\n"),
    6197                 :            :                   (uint64_t) (offset_array_start
    6198                 :          1 :                               - (unsigned char *) data->d_buf));
    6199         [ +  + ]:          3 :           for (uint32_t idx = 0; idx < offset_entry_count; idx++)
    6200                 :            :             {
    6201                 :          2 :               printf ("   [%6" PRIu32 "] ", idx);
    6202         [ +  - ]:          2 :               if (offset_size == 4)
    6203                 :            :                 {
    6204         [ -  + ]:          2 :                   uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
    6205                 :          2 :                   printf ("0x%" PRIx32 "\n", off);
    6206                 :            :                 }
    6207                 :            :               else
    6208                 :            :                 {
    6209         [ #  # ]:          0 :                   uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
    6210                 :          2 :                   printf ("0x%" PRIx64 "\n", off);
    6211                 :            :                 }
    6212                 :            :             }
    6213                 :          1 :           printf ("\n");
    6214                 :            :         }
    6215                 :            : 
    6216                 :          3 :       Dwarf_Addr base = cu_base;
    6217                 :          3 :       bool start_of_list = true;
    6218                 :          3 :       while (readp < nexthdr)
    6219                 :            :         {
    6220                 :         16 :           uint8_t kind = *readp++;
    6221                 :         16 :           uint64_t op1, op2;
    6222                 :            : 
    6223                 :            :           /* Skip padding.  */
    6224         [ -  + ]:         16 :           if (start_of_list && kind == DW_RLE_end_of_list)
    6225                 :          0 :             continue;
    6226                 :            : 
    6227         [ +  + ]:         16 :           if (start_of_list)
    6228                 :            :             {
    6229                 :          5 :               base = cu_base;
    6230                 :          5 :               printf ("  Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
    6231                 :          5 :                       (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
    6232                 :          5 :                       (uint64_t) (readp - offset_array_start - 1));
    6233                 :          5 :               start_of_list = false;
    6234                 :            :             }
    6235                 :            : 
    6236                 :         16 :           printf ("    %s", dwarf_range_list_encoding_name (kind));
    6237   [ +  -  -  -  :         16 :           switch (kind)
             +  +  -  +  
                      - ]
    6238                 :            :             {
    6239                 :          5 :             case DW_RLE_end_of_list:
    6240                 :          5 :               start_of_list = true;
    6241                 :          5 :               printf ("\n\n");
    6242                 :            :               break;
    6243                 :            : 
    6244                 :          0 :             case DW_RLE_base_addressx:
    6245         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6246                 :            :                 {
    6247                 :          0 :                 invalid_range:
    6248                 :          0 :                   error (0, 0, _("invalid range list data"));
    6249                 :          0 :                   goto next_table;
    6250                 :            :                 }
    6251                 :          0 :               get_uleb128 (op1, readp, nexthdr);
    6252                 :          0 :               printf (" %" PRIx64 "\n", op1);
    6253         [ #  # ]:          0 :               if (! print_unresolved_addresses)
    6254                 :            :                 {
    6255                 :          0 :                   Dwarf_Addr addr;
    6256         [ #  # ]:          0 :                   if (get_indexed_addr (cu, op1, &addr) != 0)
    6257                 :          0 :                     printf ("      ???\n");
    6258                 :            :                   else
    6259                 :            :                     {
    6260                 :          0 :                       printf ("      ");
    6261                 :          0 :                       print_dwarf_addr (dwflmod, address_size, addr, addr);
    6262                 :          0 :                       printf ("\n");
    6263                 :            :                     }
    6264                 :            :                 }
    6265                 :            :               break;
    6266                 :            : 
    6267                 :          0 :             case DW_RLE_startx_endx:
    6268         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6269                 :          0 :                 goto invalid_range;
    6270                 :          0 :               get_uleb128 (op1, readp, nexthdr);
    6271         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6272                 :          0 :                 goto invalid_range;
    6273                 :          0 :               get_uleb128 (op2, readp, nexthdr);
    6274                 :          0 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    6275         [ #  # ]:          0 :               if (! print_unresolved_addresses)
    6276                 :            :                 {
    6277                 :          0 :                   Dwarf_Addr addr1;
    6278                 :          0 :                   Dwarf_Addr addr2;
    6279         [ #  # ]:          0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0
    6280         [ #  # ]:          0 :                       || get_indexed_addr (cu, op2, &addr2) != 0)
    6281                 :            :                     {
    6282                 :          0 :                       printf ("      ???..\n");
    6283                 :          0 :                       printf ("      ???\n");
    6284                 :            :                     }
    6285                 :            :                   else
    6286                 :            :                     {
    6287                 :          0 :                       printf ("      ");
    6288                 :          0 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1);
    6289                 :          0 :                       printf ("..\n      ");
    6290                 :          0 :                       print_dwarf_addr (dwflmod, address_size,
    6291                 :            :                                         addr2 - 1, addr2);
    6292                 :          0 :                       printf ("\n");
    6293                 :            :                     }
    6294                 :            :                 }
    6295                 :            :               break;
    6296                 :            : 
    6297                 :          0 :             case DW_RLE_startx_length:
    6298         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6299                 :          0 :                 goto invalid_range;
    6300                 :          0 :               get_uleb128 (op1, readp, nexthdr);
    6301         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6302                 :          0 :                 goto invalid_range;
    6303                 :          0 :               get_uleb128 (op2, readp, nexthdr);
    6304                 :          0 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    6305         [ #  # ]:          0 :               if (! print_unresolved_addresses)
    6306                 :            :                 {
    6307                 :          0 :                   Dwarf_Addr addr1;
    6308                 :          0 :                   Dwarf_Addr addr2;
    6309         [ #  # ]:          0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0)
    6310                 :            :                     {
    6311                 :          0 :                       printf ("      ???..\n");
    6312                 :          0 :                       printf ("      ???\n");
    6313                 :            :                     }
    6314                 :            :                   else
    6315                 :            :                     {
    6316                 :          0 :                       addr2 = addr1 + op2;
    6317                 :          0 :                       printf ("      ");
    6318                 :          0 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1);
    6319                 :          0 :                       printf ("..\n      ");
    6320                 :          0 :                       print_dwarf_addr (dwflmod, address_size,
    6321                 :            :                                         addr2 - 1, addr2);
    6322                 :          0 :                       printf ("\n");
    6323                 :            :                     }
    6324                 :            :                 }
    6325                 :            :               break;
    6326                 :            : 
    6327                 :          4 :             case DW_RLE_offset_pair:
    6328         [ -  + ]:          4 :               if ((uint64_t) (nexthdr - readp) < 1)
    6329                 :          0 :                 goto invalid_range;
    6330                 :          4 :               get_uleb128 (op1, readp, nexthdr);
    6331         [ -  + ]:          4 :               if ((uint64_t) (nexthdr - readp) < 1)
    6332                 :          0 :                 goto invalid_range;
    6333                 :          4 :               get_uleb128 (op2, readp, nexthdr);
    6334                 :          4 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    6335         [ +  - ]:          4 :               if (! print_unresolved_addresses)
    6336                 :            :                 {
    6337                 :          4 :                   op1 += base;
    6338                 :          4 :                   op2 += base;
    6339                 :          4 :                   printf ("      ");
    6340                 :          4 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    6341                 :          4 :                   printf ("..\n      ");
    6342                 :          4 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    6343                 :          4 :                   printf ("\n");
    6344                 :            :                 }
    6345                 :            :               break;
    6346                 :            : 
    6347                 :          2 :             case DW_RLE_base_address:
    6348         [ -  + ]:          2 :               if (address_size == 4)
    6349                 :            :                 {
    6350         [ #  # ]:          0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    6351                 :          0 :                     goto invalid_range;
    6352         [ #  # ]:          0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    6353                 :            :                 }
    6354                 :            :               else
    6355                 :            :                 {
    6356         [ -  + ]:          2 :                   if ((uint64_t) (nexthdr - readp) < 8)
    6357                 :          0 :                     goto invalid_range;
    6358         [ -  + ]:          2 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    6359                 :            :                 }
    6360                 :          2 :               base = op1;
    6361                 :          2 :               printf (" 0x%" PRIx64 "\n", base);
    6362         [ +  - ]:          2 :               if (! print_unresolved_addresses)
    6363                 :            :                 {
    6364                 :          2 :                   printf ("      ");
    6365                 :          2 :                   print_dwarf_addr (dwflmod, address_size, base, base);
    6366                 :          2 :                   printf ("\n");
    6367                 :            :                 }
    6368                 :            :               break;
    6369                 :            : 
    6370                 :          0 :             case DW_RLE_start_end:
    6371         [ #  # ]:          0 :               if (address_size == 4)
    6372                 :            :                 {
    6373         [ #  # ]:          0 :                   if ((uint64_t) (nexthdr - readp) < 8)
    6374                 :          0 :                     goto invalid_range;
    6375         [ #  # ]:          0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    6376         [ #  # ]:          0 :                   op2 = read_4ubyte_unaligned_inc (dbg, readp);
    6377                 :            :                 }
    6378                 :            :               else
    6379                 :            :                 {
    6380         [ #  # ]:          0 :                   if ((uint64_t) (nexthdr - readp) < 16)
    6381                 :          0 :                     goto invalid_range;
    6382         [ #  # ]:          0 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    6383         [ #  # ]:          0 :                   op2 = read_8ubyte_unaligned_inc (dbg, readp);
    6384                 :            :                 }
    6385                 :          0 :               printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
    6386         [ #  # ]:          0 :               if (! print_unresolved_addresses)
    6387                 :            :                 {
    6388                 :          0 :                   printf ("      ");
    6389                 :          0 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    6390                 :          0 :                   printf ("..\n      ");
    6391                 :          0 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    6392                 :          0 :                   printf ("\n");
    6393                 :            :                 }
    6394                 :            :               break;
    6395                 :            : 
    6396                 :          5 :             case DW_RLE_start_length:
    6397         [ -  + ]:          5 :               if (address_size == 4)
    6398                 :            :                 {
    6399         [ #  # ]:          0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    6400                 :          0 :                     goto invalid_range;
    6401         [ #  # ]:          0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    6402                 :            :                 }
    6403                 :            :               else
    6404                 :            :                 {
    6405         [ -  + ]:          5 :                   if ((uint64_t) (nexthdr - readp) < 8)
    6406                 :          0 :                     goto invalid_range;
    6407         [ -  + ]:          5 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    6408                 :            :                 }
    6409         [ -  + ]:          5 :               if ((uint64_t) (nexthdr - readp) < 1)
    6410                 :          0 :                 goto invalid_range;
    6411                 :          5 :               get_uleb128 (op2, readp, nexthdr);
    6412                 :          5 :               printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
    6413         [ +  + ]:          5 :               if (! print_unresolved_addresses)
    6414                 :            :                 {
    6415                 :          4 :                   op2 = op1 + op2;
    6416                 :          4 :                   printf ("      ");
    6417                 :          4 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    6418                 :          4 :                   printf ("..\n      ");
    6419                 :          4 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    6420         [ +  + ]:         23 :                   printf ("\n");
    6421                 :            :                 }
    6422                 :            :               break;
    6423                 :            : 
    6424                 :          0 :             default:
    6425                 :          0 :               goto invalid_range;
    6426                 :            :             }
    6427                 :            :         }
    6428                 :            : 
    6429                 :          3 :     next_table:
    6430         [ -  + ]:          3 :       if (readp != nexthdr)
    6431                 :            :         {
    6432                 :          0 :           size_t padding = nexthdr - readp;
    6433                 :          0 :           printf (_("   %zu padding bytes\n\n"), padding);
    6434                 :          0 :           readp = nexthdr;
    6435                 :            :         }
    6436                 :            :     }
    6437                 :            : }
    6438                 :            : 
    6439                 :            : /* Print content of DWARF .debug_ranges section.  */
    6440                 :            : static void
    6441                 :         32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
    6442                 :            :                             Ebl *ebl, GElf_Ehdr *ehdr,
    6443                 :            :                             Elf_Scn *scn, GElf_Shdr *shdr,
    6444                 :            :                             Dwarf *dbg)
    6445                 :            : {
    6446                 :         32 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
    6447         [ -  + ]:         32 :   if (data == NULL)
    6448                 :          0 :     return;
    6449                 :            : 
    6450                 :         32 :   printf (_("\
    6451                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    6452                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    6453                 :         32 :           (uint64_t) shdr->sh_offset);
    6454                 :            : 
    6455                 :         32 :   sort_listptr (&known_rangelistptr, "rangelistptr");
    6456                 :         32 :   size_t listptr_idx = 0;
    6457                 :            : 
    6458         [ +  + ]:         32 :   uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    6459                 :            : 
    6460                 :         32 :   bool first = true;
    6461                 :         32 :   Dwarf_Addr base = 0;
    6462                 :         32 :   unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
    6463                 :         32 :   unsigned char *readp = data->d_buf;
    6464                 :         32 :   Dwarf_CU *last_cu = NULL;
    6465         [ +  + ]:      62868 :   while (readp < endp)
    6466                 :            :     {
    6467                 :      62836 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    6468                 :      62836 :       Dwarf_CU *cu = last_cu;
    6469                 :            : 
    6470   [ +  +  +  + ]:      62836 :       if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
    6471                 :            :                                       &address_size, NULL, &base, &cu,
    6472                 :            :                                       offset, &readp, endp, NULL))
    6473                 :         11 :         continue;
    6474                 :            : 
    6475         [ +  + ]:      62825 :       if (last_cu != cu)
    6476                 :            :         {
    6477                 :        968 :           Dwarf_Die cudie;
    6478         [ -  + ]:        968 :           if (dwarf_cu_die (cu, &cudie,
    6479                 :            :                             NULL, NULL, NULL, NULL,
    6480                 :            :                             NULL, NULL) == NULL)
    6481                 :          0 :             printf (_("\n Unknown CU base: "));
    6482                 :            :           else
    6483                 :        968 :             printf (_("\n CU [%6" PRIx64 "] base: "),
    6484                 :            :                     dwarf_dieoffset (&cudie));
    6485                 :        968 :           print_dwarf_addr (dwflmod, address_size, base, base);
    6486                 :        968 :           printf ("\n");
    6487                 :            :         }
    6488                 :      62825 :       last_cu = cu;
    6489                 :            : 
    6490         [ -  + ]:      62825 :       if (unlikely (data->d_size - offset < (size_t) address_size * 2))
    6491                 :            :         {
    6492                 :          0 :           printf (_(" [%6tx]  <INVALID DATA>\n"), offset);
    6493                 :          0 :           break;
    6494                 :            :         }
    6495                 :            : 
    6496                 :      62825 :       Dwarf_Addr begin;
    6497                 :      62825 :       Dwarf_Addr end;
    6498         [ +  + ]:      62825 :       if (address_size == 8)
    6499                 :            :         {
    6500         [ -  + ]:      62809 :           begin = read_8ubyte_unaligned_inc (dbg, readp);
    6501         [ -  + ]:      62809 :           end = read_8ubyte_unaligned_inc (dbg, readp);
    6502                 :            :         }
    6503                 :            :       else
    6504                 :            :         {
    6505         [ -  + ]:         16 :           begin = read_4ubyte_unaligned_inc (dbg, readp);
    6506         [ -  + ]:         16 :           end = read_4ubyte_unaligned_inc (dbg, readp);
    6507         [ +  - ]:         16 :           if (begin == (Dwarf_Addr) (uint32_t) -1)
    6508                 :            :             begin = (Dwarf_Addr) -1l;
    6509                 :            :         }
    6510                 :            : 
    6511         [ -  + ]:      62825 :       if (begin == (Dwarf_Addr) -1l) /* Base address entry.  */
    6512                 :            :         {
    6513         [ #  # ]:          0 :           if (first)
    6514                 :          0 :             printf (" [%6tx] ", offset);
    6515                 :            :           else
    6516                 :          0 :             printf ("          ");
    6517                 :          0 :           puts (_("base address"));
    6518                 :          0 :           printf ("          ");
    6519                 :          0 :           print_dwarf_addr (dwflmod, address_size, end, end);
    6520                 :          0 :           printf ("\n");
    6521                 :          0 :           base = end;
    6522                 :          0 :           first = false;
    6523                 :            :         }
    6524         [ +  + ]:      62825 :       else if (begin == 0 && end == 0) /* End of list entry.  */
    6525                 :            :         {
    6526         [ +  + ]:      14078 :           if (first)
    6527                 :         11 :             printf (_(" [%6tx] empty list\n"), offset);
    6528                 :            :           first = true;
    6529                 :            :         }
    6530                 :            :       else
    6531                 :            :         {
    6532                 :            :           /* We have an address range entry.  */
    6533         [ +  + ]:      48747 :           if (first)            /* First address range entry in a list.  */
    6534                 :      14067 :             printf (" [%6tx] ", offset);
    6535                 :            :           else
    6536                 :      34680 :             printf ("          ");
    6537                 :            : 
    6538                 :      48747 :           printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
    6539         [ +  + ]:      48747 :           if (! print_unresolved_addresses)
    6540                 :            :             {
    6541                 :      48737 :               printf ("          ");
    6542                 :      48737 :               print_dwarf_addr (dwflmod, address_size, base + begin,
    6543                 :            :                                 base + begin);
    6544                 :      48737 :               printf ("..\n          ");
    6545                 :      48737 :               print_dwarf_addr (dwflmod, address_size,
    6546                 :            :                                 base + end - 1, base + end);
    6547                 :      62825 :               printf ("\n");
    6548                 :            :             }
    6549                 :            : 
    6550                 :            :           first = false;
    6551                 :            :         }
    6552                 :            :     }
    6553                 :            : }
    6554                 :            : 
    6555                 :            : #define REGNAMESZ 16
    6556                 :            : static const char *
    6557                 :      19080 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
    6558                 :            :                char name[REGNAMESZ], int *bits, int *type)
    6559                 :            : {
    6560                 :      19080 :   const char *set;
    6561                 :      19080 :   const char *pfx;
    6562                 :      19080 :   int ignore;
    6563   [ +  +  +  + ]:      55544 :   ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
    6564                 :            :                                  bits ?: &ignore, type ?: &ignore);
    6565         [ -  + ]:      19080 :   if (n <= 0)
    6566                 :            :     {
    6567         [ #  # ]:          0 :       if (loc != NULL)
    6568                 :          0 :         snprintf (name, REGNAMESZ, "reg%u", loc->regno);
    6569                 :            :       else
    6570                 :          0 :         snprintf (name, REGNAMESZ, "??? 0x%x", regno);
    6571         [ #  # ]:          0 :       if (bits != NULL)
    6572         [ #  # ]:          0 :         *bits = loc != NULL ? loc->bits : 0;
    6573         [ #  # ]:          0 :       if (type != NULL)
    6574                 :          0 :         *type = DW_ATE_unsigned;
    6575                 :          0 :       set = "??? unrecognized";
    6576                 :            :     }
    6577                 :            :   else
    6578                 :            :     {
    6579   [ +  +  -  + ]:      19080 :       if (bits != NULL && *bits <= 0)
    6580         [ #  # ]:          0 :         *bits = loc != NULL ? loc->bits : 0;
    6581   [ +  +  -  + ]:      19080 :       if (type != NULL && *type == DW_ATE_void)
    6582                 :          0 :         *type = DW_ATE_unsigned;
    6583                 :            : 
    6584                 :            :     }
    6585                 :      19080 :   return set;
    6586                 :            : }
    6587                 :            : 
    6588                 :            : static const unsigned char *
    6589                 :        168 : read_encoded (unsigned int encoding, const unsigned char *readp,
    6590                 :            :               const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
    6591                 :            : {
    6592         [ -  + ]:        168 :   if ((encoding & 0xf) == DW_EH_PE_absptr)
    6593                 :          0 :     encoding = gelf_getclass (dbg->elf) == ELFCLASS32
    6594         [ #  # ]:          0 :       ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
    6595                 :            : 
    6596   [ -  -  -  +  :        168 :   switch (encoding & 0xf)
             -  -  +  -  
                      - ]
    6597                 :            :     {
    6598                 :          0 :     case DW_EH_PE_uleb128:
    6599         [ #  # ]:          0 :       if (readp >= endp)
    6600                 :          0 :         goto invalid;
    6601                 :          0 :       get_uleb128 (*res, readp, endp);
    6602                 :          0 :       break;
    6603                 :          0 :     case DW_EH_PE_sleb128:
    6604         [ #  # ]:          0 :       if (readp >= endp)
    6605                 :          0 :         goto invalid;
    6606                 :          0 :       get_sleb128 (*res, readp, endp);
    6607                 :          0 :       break;
    6608                 :          0 :     case DW_EH_PE_udata2:
    6609         [ #  # ]:          0 :       if (readp + 2 > endp)
    6610                 :          0 :         goto invalid;
    6611         [ #  # ]:          0 :       *res = read_2ubyte_unaligned_inc (dbg, readp);
    6612                 :          0 :       break;
    6613                 :         84 :     case DW_EH_PE_udata4:
    6614         [ -  + ]:         84 :       if (readp + 4 > endp)
    6615                 :          0 :         goto invalid;
    6616         [ -  + ]:         84 :       *res = read_4ubyte_unaligned_inc (dbg, readp);
    6617                 :         84 :       break;
    6618                 :          0 :     case DW_EH_PE_udata8:
    6619         [ #  # ]:          0 :       if (readp + 8 > endp)
    6620                 :          0 :         goto invalid;
    6621         [ #  # ]:          0 :       *res = read_8ubyte_unaligned_inc (dbg, readp);
    6622                 :          0 :       break;
    6623                 :          0 :     case DW_EH_PE_sdata2:
    6624         [ #  # ]:          0 :       if (readp + 2 > endp)
    6625                 :          0 :         goto invalid;
    6626         [ #  # ]:          0 :       *res = read_2sbyte_unaligned_inc (dbg, readp);
    6627                 :          0 :       break;
    6628                 :         84 :     case DW_EH_PE_sdata4:
    6629         [ -  + ]:         84 :       if (readp + 4 > endp)
    6630                 :          0 :         goto invalid;
    6631         [ -  + ]:         84 :       *res = read_4sbyte_unaligned_inc (dbg, readp);
    6632                 :         84 :       break;
    6633                 :          0 :     case DW_EH_PE_sdata8:
    6634         [ #  # ]:          0 :       if (readp + 8 > endp)
    6635                 :          0 :         goto invalid;
    6636         [ #  # ]:          0 :       *res = read_8sbyte_unaligned_inc (dbg, readp);
    6637                 :          0 :       break;
    6638                 :            :     default:
    6639                 :          0 :     invalid:
    6640                 :          0 :       error (1, 0,
    6641                 :          0 :              _("invalid encoding"));
    6642                 :            :     }
    6643                 :            : 
    6644                 :        168 :   return readp;
    6645                 :            : }
    6646                 :            : 
    6647                 :            : static const char *
    6648                 :      18232 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
    6649                 :            : {
    6650                 :      18232 :   register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
    6651                 :            : 
    6652                 :      18232 :   return regnamebuf;
    6653                 :            : }
    6654                 :            : 
    6655                 :            : static void
    6656                 :      13362 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
    6657                 :            :                    Dwarf_Word vma_base, unsigned int code_align,
    6658                 :            :                    int data_align,
    6659                 :            :                    unsigned int version, unsigned int ptr_size,
    6660                 :            :                    unsigned int encoding,
    6661                 :            :                    Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
    6662                 :            : {
    6663                 :      13362 :   char regnamebuf[REGNAMESZ];
    6664                 :            : 
    6665                 :      13362 :   puts ("\n   Program:");
    6666                 :      13362 :   Dwarf_Word pc = vma_base;
    6667                 :      13362 :   while (readp < endp)
    6668                 :            :     {
    6669                 :     194104 :       unsigned int opcode = *readp++;
    6670                 :            : 
    6671         [ +  + ]:     194104 :       if (opcode < DW_CFA_advance_loc)
    6672                 :            :         /* Extended opcode.  */
    6673   [ +  -  +  +  :     117437 :         switch (opcode)
          +  +  -  +  -  
          -  +  +  +  +  
          +  +  -  +  -  
          -  -  -  -  -  
                +  -  - ]
    6674                 :            :           {
    6675                 :      38639 :             uint64_t op1;
    6676                 :      38639 :             int64_t sop1;
    6677                 :      38639 :             uint64_t op2;
    6678                 :      38639 :             int64_t sop2;
    6679                 :            : 
    6680                 :      38639 :           case DW_CFA_nop:
    6681                 :      38639 :             puts ("     nop");
    6682                 :      38639 :             break;
    6683                 :          0 :           case DW_CFA_set_loc:
    6684         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6685                 :          0 :               goto invalid;
    6686                 :          0 :             readp = read_encoded (encoding, readp, endp, &op1, dbg);
    6687                 :     117437 :             printf ("     set_loc %#" PRIx64 " to %#" PRIx64 "\n",
    6688                 :          0 :                     op1, pc = vma_base + op1);
    6689                 :            :             break;
    6690                 :       4583 :           case DW_CFA_advance_loc1:
    6691         [ -  + ]:       4583 :             if ((uint64_t) (endp - readp) < 1)
    6692                 :          0 :               goto invalid;
    6693                 :       9166 :             printf ("     advance_loc1 %u to %#" PRIx64 "\n",
    6694                 :       4583 :                     *readp, pc += *readp * code_align);
    6695                 :       4583 :             ++readp;
    6696                 :       4583 :             break;
    6697                 :       1347 :           case DW_CFA_advance_loc2:
    6698         [ -  + ]:       1347 :             if ((uint64_t) (endp - readp) < 2)
    6699                 :          0 :               goto invalid;
    6700         [ -  + ]:       1347 :             op1 = read_2ubyte_unaligned_inc (dbg, readp);
    6701                 :     118784 :             printf ("     advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
    6702                 :       1347 :                     op1, pc += op1 * code_align);
    6703                 :            :             break;
    6704                 :         24 :           case DW_CFA_advance_loc4:
    6705         [ -  + ]:         24 :             if ((uint64_t) (endp - readp) < 4)
    6706                 :          0 :               goto invalid;
    6707         [ +  - ]:         24 :             op1 = read_4ubyte_unaligned_inc (dbg, readp);
    6708                 :     117461 :             printf ("     advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
    6709                 :         24 :                     op1, pc += op1 * code_align);
    6710                 :            :             break;
    6711                 :          4 :           case DW_CFA_offset_extended:
    6712         [ -  + ]:          4 :             if ((uint64_t) (endp - readp) < 1)
    6713                 :          0 :               goto invalid;
    6714                 :          4 :             get_uleb128 (op1, readp, endp);
    6715         [ -  + ]:          4 :             if ((uint64_t) (endp - readp) < 1)
    6716                 :          0 :               goto invalid;
    6717                 :          4 :             get_uleb128 (op2, readp, endp);
    6718                 :          4 :             printf ("     offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
    6719                 :            :                     "\n",
    6720                 :            :                     op1, regname (ebl, op1, regnamebuf), op2 * data_align);
    6721                 :            :             break;
    6722                 :          0 :           case DW_CFA_restore_extended:
    6723         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6724                 :          0 :               goto invalid;
    6725                 :          0 :             get_uleb128 (op1, readp, endp);
    6726                 :          0 :             printf ("     restore_extended r%" PRIu64 " (%s)\n",
    6727                 :            :                     op1, regname (ebl, op1, regnamebuf));
    6728                 :            :             break;
    6729                 :         67 :           case DW_CFA_undefined:
    6730         [ -  + ]:         67 :             if ((uint64_t) (endp - readp) < 1)
    6731                 :          0 :               goto invalid;
    6732                 :         67 :             get_uleb128 (op1, readp, endp);
    6733                 :         67 :             printf ("     undefined r%" PRIu64 " (%s)\n", op1,
    6734                 :            :                     regname (ebl, op1, regnamebuf));
    6735                 :            :             break;
    6736                 :          0 :           case DW_CFA_same_value:
    6737         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6738                 :          0 :               goto invalid;
    6739                 :          0 :             get_uleb128 (op1, readp, endp);
    6740                 :          0 :             printf ("     same_value r%" PRIu64 " (%s)\n", op1,
    6741                 :            :                     regname (ebl, op1, regnamebuf));
    6742                 :            :             break;
    6743                 :          0 :           case DW_CFA_register:
    6744         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6745                 :          0 :               goto invalid;
    6746                 :          0 :             get_uleb128 (op1, readp, endp);
    6747         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6748                 :          0 :               goto invalid;
    6749                 :          0 :             get_uleb128 (op2, readp, endp);
    6750                 :          0 :             printf ("     register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
    6751                 :            :                     op1, regname (ebl, op1, regnamebuf), op2,
    6752                 :            :                     regname (ebl, op2, regnamebuf));
    6753                 :            :             break;
    6754                 :       7835 :           case DW_CFA_remember_state:
    6755                 :       7835 :             puts ("     remember_state");
    6756                 :       7835 :             break;
    6757                 :       7835 :           case DW_CFA_restore_state:
    6758                 :       7835 :             puts ("     restore_state");
    6759                 :       7835 :             break;
    6760                 :        328 :           case DW_CFA_def_cfa:
    6761         [ -  + ]:        328 :             if ((uint64_t) (endp - readp) < 1)
    6762                 :          0 :               goto invalid;
    6763                 :        328 :             get_uleb128 (op1, readp, endp);
    6764         [ -  + ]:        328 :             if ((uint64_t) (endp - readp) < 1)
    6765                 :          0 :               goto invalid;
    6766                 :        328 :             get_uleb128 (op2, readp, endp);
    6767                 :        328 :             printf ("     def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
    6768                 :            :                     op1, regname (ebl, op1, regnamebuf), op2);
    6769                 :            :             break;
    6770                 :         75 :           case DW_CFA_def_cfa_register:
    6771         [ -  + ]:         75 :             if ((uint64_t) (endp - readp) < 1)
    6772                 :          0 :               goto invalid;
    6773                 :         75 :             get_uleb128 (op1, readp, endp);
    6774                 :         75 :             printf ("     def_cfa_register r%" PRIu64 " (%s)\n",
    6775                 :            :                     op1, regname (ebl, op1, regnamebuf));
    6776                 :            :             break;
    6777                 :      56609 :           case DW_CFA_def_cfa_offset:
    6778         [ -  + ]:      56609 :             if ((uint64_t) (endp - readp) < 1)
    6779                 :          0 :               goto invalid;
    6780                 :      56609 :             get_uleb128 (op1, readp, endp);
    6781                 :      56609 :             printf ("     def_cfa_offset %" PRIu64 "\n", op1);
    6782                 :            :             break;
    6783                 :         71 :           case DW_CFA_def_cfa_expression:
    6784         [ -  + ]:         71 :             if ((uint64_t) (endp - readp) < 1)
    6785                 :          0 :               goto invalid;
    6786                 :         71 :             get_uleb128 (op1, readp, endp);     /* Length of DW_FORM_block.  */
    6787                 :         71 :             printf ("     def_cfa_expression %" PRIu64 "\n", op1);
    6788         [ -  + ]:         71 :             if ((uint64_t) (endp - readp) < op1)
    6789                 :            :               {
    6790                 :          0 :             invalid:
    6791                 :          0 :                 fputs (_("         <INVALID DATA>\n"), stdout);
    6792                 :          0 :                 return;
    6793                 :            :               }
    6794                 :         71 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
    6795                 :            :                        op1, readp);
    6796                 :         71 :             readp += op1;
    6797                 :         71 :             break;
    6798                 :          0 :           case DW_CFA_expression:
    6799         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6800                 :          0 :               goto invalid;
    6801                 :          0 :             get_uleb128 (op1, readp, endp);
    6802         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6803                 :          0 :               goto invalid;
    6804                 :          0 :             get_uleb128 (op2, readp, endp);     /* Length of DW_FORM_block.  */
    6805                 :          0 :             printf ("     expression r%" PRIu64 " (%s) \n",
    6806                 :            :                     op1, regname (ebl, op1, regnamebuf));
    6807         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < op2)
    6808                 :          0 :               goto invalid;
    6809                 :          0 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
    6810                 :            :                        op2, readp);
    6811                 :          0 :             readp += op2;
    6812                 :          0 :             break;
    6813                 :         16 :           case DW_CFA_offset_extended_sf:
    6814         [ -  + ]:         16 :             if ((uint64_t) (endp - readp) < 1)
    6815                 :          0 :               goto invalid;
    6816                 :         16 :             get_uleb128 (op1, readp, endp);
    6817         [ -  + ]:         16 :             if ((uint64_t) (endp - readp) < 1)
    6818                 :          0 :               goto invalid;
    6819                 :         16 :             get_sleb128 (sop2, readp, endp);
    6820                 :         16 :             printf ("     offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
    6821                 :            :                     PRId64 "\n",
    6822                 :            :                     op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
    6823                 :            :             break;
    6824                 :          0 :           case DW_CFA_def_cfa_sf:
    6825         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6826                 :          0 :               goto invalid;
    6827                 :          0 :             get_uleb128 (op1, readp, endp);
    6828         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6829                 :          0 :               goto invalid;
    6830                 :          0 :             get_sleb128 (sop2, readp, endp);
    6831                 :          0 :             printf ("     def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
    6832                 :            :                     op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
    6833                 :            :             break;
    6834                 :          0 :           case DW_CFA_def_cfa_offset_sf:
    6835         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6836                 :          0 :               goto invalid;
    6837                 :          0 :             get_sleb128 (sop1, readp, endp);
    6838                 :          0 :             printf ("     def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
    6839                 :            :             break;
    6840                 :          0 :           case DW_CFA_val_offset:
    6841         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6842                 :          0 :               goto invalid;
    6843                 :          0 :             get_uleb128 (op1, readp, endp);
    6844         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6845                 :          0 :               goto invalid;
    6846                 :          0 :             get_uleb128 (op2, readp, endp);
    6847                 :          0 :             printf ("     val_offset %" PRIu64 " at offset %" PRIu64 "\n",
    6848                 :            :                     op1, op2 * data_align);
    6849                 :            :             break;
    6850                 :          0 :           case DW_CFA_val_offset_sf:
    6851         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6852                 :          0 :               goto invalid;
    6853                 :          0 :             get_uleb128 (op1, readp, endp);
    6854         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6855                 :          0 :               goto invalid;
    6856                 :          0 :             get_sleb128 (sop2, readp, endp);
    6857                 :          0 :             printf ("     val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
    6858                 :            :                     op1, sop2 * data_align);
    6859                 :            :             break;
    6860                 :          0 :           case DW_CFA_val_expression:
    6861         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6862                 :          0 :               goto invalid;
    6863                 :          0 :             get_uleb128 (op1, readp, endp);
    6864         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6865                 :          0 :               goto invalid;
    6866                 :          0 :             get_uleb128 (op2, readp, endp);     /* Length of DW_FORM_block.  */
    6867                 :          0 :             printf ("     val_expression r%" PRIu64 " (%s)\n",
    6868                 :            :                     op1, regname (ebl, op1, regnamebuf));
    6869         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < op2)
    6870                 :          0 :               goto invalid;
    6871                 :          0 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
    6872                 :            :                        NULL, op2, readp);
    6873                 :          0 :             readp += op2;
    6874                 :          0 :             break;
    6875                 :          0 :           case DW_CFA_MIPS_advance_loc8:
    6876         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 8)
    6877                 :          0 :               goto invalid;
    6878         [ #  # ]:          0 :             op1 = read_8ubyte_unaligned_inc (dbg, readp);
    6879                 :     117437 :             printf ("     MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
    6880                 :          0 :                     op1, pc += op1 * code_align);
    6881                 :            :             break;
    6882                 :          4 :           case DW_CFA_GNU_window_save:  /* DW_CFA_AARCH64_negate_ra_state  */
    6883         [ +  - ]:          4 :             if (ehdr->e_machine == EM_AARCH64)
    6884                 :          4 :               puts ("     AARCH64_negate_ra_state");
    6885                 :            :             else
    6886                 :          0 :               puts ("     GNU_window_save");
    6887                 :            :             break;
    6888                 :          0 :           case DW_CFA_GNU_args_size:
    6889         [ #  # ]:          0 :             if ((uint64_t) (endp - readp) < 1)
    6890                 :          0 :               goto invalid;
    6891                 :          0 :             get_uleb128 (op1, readp, endp);
    6892                 :          0 :             printf ("     args_size %" PRIu64 "\n", op1);
    6893                 :            :             break;
    6894                 :            :           default:
    6895                 :     117437 :             printf ("     ??? (%u)\n", opcode);
    6896                 :            :             break;
    6897                 :            :           }
    6898         [ +  + ]:      76667 :       else if (opcode < DW_CFA_offset)
    6899                 :     266391 :         printf ("     advance_loc %u to %#" PRIx64 "\n",
    6900                 :      58925 :                 opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
    6901         [ +  + ]:      17742 :       else if (opcode < DW_CFA_restore)
    6902                 :            :         {
    6903                 :      16035 :           uint64_t offset;
    6904         [ -  + ]:      16035 :           if ((uint64_t) (endp - readp) < 1)
    6905                 :          0 :             goto invalid;
    6906                 :      16035 :           get_uleb128 (offset, readp, endp);
    6907                 :      16035 :           printf ("     offset r%u (%s) at cfa%+" PRId64 "\n",
    6908                 :            :                   opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
    6909                 :            :                   offset * data_align);
    6910                 :            :         }
    6911                 :            :       else
    6912         [ +  + ]:     209173 :         printf ("     restore r%u (%s)\n",
    6913                 :            :                 opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
    6914                 :            :     }
    6915                 :            : }
    6916                 :            : 
    6917                 :            : 
    6918                 :            : static unsigned int
    6919                 :      13095 : encoded_ptr_size (int encoding, unsigned int ptr_size)
    6920                 :            : {
    6921   [ -  +  -  + ]:      13095 :   switch (encoding & 7)
    6922                 :            :     {
    6923                 :            :     case DW_EH_PE_udata4:
    6924                 :            :       return 4;
    6925                 :          0 :     case DW_EH_PE_udata8:
    6926                 :          0 :       return 8;
    6927                 :         48 :     case 0:
    6928                 :         48 :       return ptr_size;
    6929                 :            :     }
    6930                 :            : 
    6931                 :          0 :   fprintf (stderr, "Unsupported pointer encoding: %#x, "
    6932                 :            :            "assuming pointer size of %d.\n", encoding, ptr_size);
    6933                 :          0 :   return ptr_size;
    6934                 :            : }
    6935                 :            : 
    6936                 :            : 
    6937                 :            : static unsigned int
    6938                 :        479 : print_encoding (unsigned int val)
    6939                 :            : {
    6940   [ -  -  -  +  :        479 :   switch (val & 0xf)
          -  -  -  +  -  
                      - ]
    6941                 :            :     {
    6942                 :          0 :     case DW_EH_PE_absptr:
    6943                 :          0 :       fputs ("absptr", stdout);
    6944                 :          0 :       break;
    6945                 :          0 :     case DW_EH_PE_uleb128:
    6946                 :          0 :       fputs ("uleb128", stdout);
    6947                 :          0 :       break;
    6948                 :          0 :     case DW_EH_PE_udata2:
    6949                 :          0 :       fputs ("udata2", stdout);
    6950                 :          0 :       break;
    6951                 :         84 :     case DW_EH_PE_udata4:
    6952                 :         84 :       fputs ("udata4", stdout);
    6953                 :         84 :       break;
    6954                 :          0 :     case DW_EH_PE_udata8:
    6955                 :          0 :       fputs ("udata8", stdout);
    6956                 :          0 :       break;
    6957                 :          0 :     case DW_EH_PE_sleb128:
    6958                 :          0 :       fputs ("sleb128", stdout);
    6959                 :          0 :       break;
    6960                 :          0 :     case DW_EH_PE_sdata2:
    6961                 :          0 :       fputs ("sdata2", stdout);
    6962                 :          0 :       break;
    6963                 :        395 :     case DW_EH_PE_sdata4:
    6964                 :        395 :       fputs ("sdata4", stdout);
    6965                 :        395 :       break;
    6966                 :          0 :     case DW_EH_PE_sdata8:
    6967                 :          0 :       fputs ("sdata8", stdout);
    6968                 :          0 :       break;
    6969                 :            :     default:
    6970                 :            :       /* We did not use any of the bits after all.  */
    6971                 :            :       return val;
    6972                 :            :     }
    6973                 :            : 
    6974                 :        479 :   return val & ~0xf;
    6975                 :            : }
    6976                 :            : 
    6977                 :            : 
    6978                 :            : static unsigned int
    6979                 :        395 : print_relinfo (unsigned int val)
    6980                 :            : {
    6981   [ +  -  +  -  :        395 :   switch (val & 0x70)
                   -  - ]
    6982                 :            :     {
    6983                 :        311 :     case DW_EH_PE_pcrel:
    6984                 :        311 :       fputs ("pcrel", stdout);
    6985                 :        311 :       break;
    6986                 :          0 :     case DW_EH_PE_textrel:
    6987                 :          0 :       fputs ("textrel", stdout);
    6988                 :          0 :       break;
    6989                 :         84 :     case DW_EH_PE_datarel:
    6990                 :         84 :       fputs ("datarel", stdout);
    6991                 :         84 :       break;
    6992                 :          0 :     case DW_EH_PE_funcrel:
    6993                 :          0 :       fputs ("funcrel", stdout);
    6994                 :          0 :       break;
    6995                 :          0 :     case DW_EH_PE_aligned:
    6996                 :          0 :       fputs ("aligned", stdout);
    6997                 :          0 :       break;
    6998                 :            :     default:
    6999                 :            :       return val;
    7000                 :            :     }
    7001                 :            : 
    7002                 :        395 :   return val & ~0x70;
    7003                 :            : }
    7004                 :            : 
    7005                 :            : 
    7006                 :            : static void
    7007                 :        479 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
    7008                 :            : {
    7009                 :        479 :   printf ("(%s", pfx);
    7010                 :            : 
    7011         [ -  + ]:        479 :   if (fde_encoding == DW_EH_PE_omit)
    7012                 :          0 :     puts ("omit)");
    7013                 :            :   else
    7014                 :            :     {
    7015                 :        479 :       unsigned int w = fde_encoding;
    7016                 :            : 
    7017                 :        479 :       w = print_encoding (w);
    7018                 :            : 
    7019         [ +  + ]:        479 :       if (w & 0x70)
    7020                 :            :         {
    7021         [ +  - ]:        395 :           if (w != fde_encoding)
    7022         [ -  + ]:        395 :             fputc_unlocked (' ', stdout);
    7023                 :            : 
    7024                 :        395 :           w = print_relinfo (w);
    7025                 :            :         }
    7026                 :            : 
    7027         [ -  + ]:        479 :       if (w != 0)
    7028         [ #  # ]:          0 :         printf ("%s%x", w != fde_encoding ? " " : "", w);
    7029                 :            : 
    7030                 :        479 :       puts (")");
    7031                 :            :     }
    7032                 :        479 : }
    7033                 :            : 
    7034                 :            : 
    7035                 :            : static void
    7036                 :        122 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    7037                 :            :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    7038                 :            : {
    7039                 :        122 :   size_t shstrndx;
    7040                 :            :   /* We know this call will succeed since it did in the caller.  */
    7041                 :        122 :   (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
    7042                 :        122 :   const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
    7043                 :            : 
    7044                 :            :   /* Needed if we find PC-relative addresses.  */
    7045                 :        122 :   GElf_Addr bias;
    7046         [ -  + ]:        122 :   if (dwfl_module_getelf (dwflmod, &bias) == NULL)
    7047                 :            :     {
    7048                 :          0 :       error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
    7049                 :          0 :       return;
    7050                 :            :     }
    7051                 :            : 
    7052                 :        122 :   bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
    7053                 :        122 :   Elf_Data *data;
    7054         [ +  + ]:        122 :   if (is_eh_frame)
    7055                 :            :     {
    7056                 :         98 :       data = elf_rawdata (scn, NULL);
    7057         [ -  + ]:         98 :       if (data == NULL)
    7058                 :            :         {
    7059                 :          0 :           error (0, 0, _("cannot get %s content: %s"),
    7060                 :            :                  scnname, elf_errmsg (-1));
    7061                 :          0 :           return;
    7062                 :            :         }
    7063                 :            :     }
    7064                 :            :   else
    7065                 :            :     {
    7066                 :         24 :       data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
    7067         [ +  - ]:         24 :       if (data == NULL)
    7068                 :            :         return;
    7069                 :            :     }
    7070                 :            : 
    7071         [ +  + ]:        122 :   if (is_eh_frame)
    7072                 :         98 :     printf (_("\
    7073                 :            : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    7074                 :         98 :             elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
    7075                 :            :   else
    7076                 :         24 :     printf (_("\
    7077                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    7078                 :         24 :             elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
    7079                 :            : 
    7080                 :        122 :   struct cieinfo
    7081                 :            :   {
    7082                 :            :     ptrdiff_t cie_offset;
    7083                 :            :     const char *augmentation;
    7084                 :            :     unsigned int code_alignment_factor;
    7085                 :            :     unsigned int data_alignment_factor;
    7086                 :            :     uint8_t address_size;
    7087                 :            :     uint8_t fde_encoding;
    7088                 :            :     uint8_t lsda_encoding;
    7089                 :            :     struct cieinfo *next;
    7090                 :        122 :   } *cies = NULL;
    7091                 :            : 
    7092                 :        122 :   const unsigned char *readp = data->d_buf;
    7093                 :        122 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    7094                 :        122 :                                         + data->d_size);
    7095         [ +  + ]:      13568 :   while (readp < dataend)
    7096                 :            :     {
    7097         [ -  + ]:      13446 :       if (unlikely (readp + 4 > dataend))
    7098                 :            :         {
    7099                 :          0 :         invalid_data:
    7100                 :          0 :           error (0, 0, _("invalid data in section [%zu] '%s'"),
    7101                 :            :                      elf_ndxscn (scn), scnname);
    7102                 :          0 :               return;
    7103                 :            :         }
    7104                 :            : 
    7105                 :            :       /* At the beginning there must be a CIE.  There can be multiple,
    7106                 :            :          hence we test tis in a loop.  */
    7107                 :      13446 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    7108                 :            : 
    7109         [ +  + ]:      13446 :       Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    7110                 :      13446 :       unsigned int length = 4;
    7111         [ -  + ]:      13446 :       if (unlikely (unit_length == 0xffffffff))
    7112                 :            :         {
    7113         [ #  # ]:          0 :           if (unlikely (readp + 8 > dataend))
    7114                 :          0 :             goto invalid_data;
    7115                 :            : 
    7116         [ #  # ]:          0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    7117                 :          0 :           length = 8;
    7118                 :            :         }
    7119                 :            : 
    7120         [ +  + ]:      13446 :       if (unlikely (unit_length == 0))
    7121                 :            :         {
    7122                 :         84 :           printf (_("\n [%6tx] Zero terminator\n"), offset);
    7123                 :         84 :           continue;
    7124                 :            :         }
    7125                 :            : 
    7126                 :      13362 :       Dwarf_Word maxsize = dataend - readp;
    7127         [ -  + ]:      13362 :       if (unlikely (unit_length > maxsize))
    7128                 :          0 :         goto invalid_data;
    7129                 :            : 
    7130         [ +  + ]:      13362 :       unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    7131                 :            : 
    7132                 :      13362 :       ptrdiff_t start = readp - (unsigned char *) data->d_buf;
    7133                 :      13362 :       const unsigned char *const cieend = readp + unit_length;
    7134         [ -  + ]:      13362 :       if (unlikely (cieend > dataend))
    7135                 :          0 :         goto invalid_data;
    7136                 :            : 
    7137                 :      13362 :       Dwarf_Off cie_id;
    7138         [ +  - ]:      13362 :       if (length == 4)
    7139                 :            :         {
    7140         [ -  + ]:      13362 :           if (unlikely (cieend - readp < 4))
    7141                 :          0 :             goto invalid_data;
    7142         [ +  + ]:      13362 :           cie_id = read_4ubyte_unaligned_inc (dbg, readp);
    7143         [ +  + ]:      13362 :           if (!is_eh_frame && cie_id == DW_CIE_ID_32)
    7144                 :         38 :             cie_id = DW_CIE_ID_64;
    7145                 :            :         }
    7146                 :            :       else
    7147                 :            :         {
    7148         [ #  # ]:          0 :           if (unlikely (cieend - readp < 8))
    7149                 :          0 :             goto invalid_data;
    7150         [ #  # ]:          0 :           cie_id = read_8ubyte_unaligned_inc (dbg, readp);
    7151                 :            :         }
    7152                 :            : 
    7153                 :      13362 :       uint_fast8_t version = 2;
    7154                 :      13362 :       unsigned int code_alignment_factor;
    7155                 :      13362 :       int data_alignment_factor;
    7156                 :      13362 :       unsigned int fde_encoding = 0;
    7157                 :      13362 :       unsigned int lsda_encoding = 0;
    7158                 :      13362 :       Dwarf_Word initial_location = 0;
    7159                 :      13362 :       Dwarf_Word vma_base = 0;
    7160                 :            : 
    7161   [ +  +  +  + ]:      13442 :       if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
    7162                 :            :         {
    7163         [ -  + ]:        267 :           if (unlikely (cieend - readp < 2))
    7164                 :          0 :             goto invalid_data;
    7165                 :        267 :           version = *readp++;
    7166                 :        267 :           const char *const augmentation = (const char *) readp;
    7167                 :        267 :           readp = memchr (readp, '\0', cieend - readp);
    7168         [ -  + ]:        267 :           if (unlikely (readp == NULL))
    7169                 :          0 :             goto invalid_data;
    7170                 :        267 :           ++readp;
    7171                 :            : 
    7172                 :        267 :           uint_fast8_t segment_size = 0;
    7173         [ -  + ]:        267 :           if (version >= 4)
    7174                 :            :             {
    7175         [ #  # ]:          0 :               if (cieend - readp < 5)
    7176                 :          0 :                 goto invalid_data;
    7177                 :          0 :               ptr_size = *readp++;
    7178                 :          0 :               segment_size = *readp++;
    7179                 :            :             }
    7180                 :            : 
    7181         [ -  + ]:        267 :           if (cieend - readp < 1)
    7182                 :          0 :             goto invalid_data;
    7183                 :        267 :           get_uleb128 (code_alignment_factor, readp, cieend);
    7184         [ -  + ]:        267 :           if (cieend - readp < 1)
    7185                 :          0 :             goto invalid_data;
    7186                 :        267 :           get_sleb128 (data_alignment_factor, readp, cieend);
    7187                 :            : 
    7188                 :            :           /* In some variant for unwind data there is another field.  */
    7189         [ -  + ]:        267 :           if (strcmp (augmentation, "eh") == 0)
    7190         [ #  # ]:          0 :             readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    7191                 :            : 
    7192                 :        267 :           unsigned int return_address_register;
    7193         [ -  + ]:        267 :           if (cieend - readp < 1)
    7194                 :          0 :             goto invalid_data;
    7195         [ +  + ]:        267 :           if (unlikely (version == 1))
    7196                 :        255 :             return_address_register = *readp++;
    7197                 :            :           else
    7198                 :         12 :             get_uleb128 (return_address_register, readp, cieend);
    7199                 :            : 
    7200                 :        267 :           printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
    7201                 :            :                   "   CIE_id:                   %" PRIu64 "\n"
    7202                 :            :                   "   version:                  %u\n"
    7203                 :            :                   "   augmentation:             \"%s\"\n",
    7204                 :            :                   offset, (uint64_t) unit_length, (uint64_t) cie_id,
    7205                 :            :                   version, augmentation);
    7206         [ -  + ]:        267 :           if (version >= 4)
    7207                 :          0 :             printf ("   address_size:             %u\n"
    7208                 :            :                     "   segment_size:             %u\n",
    7209                 :            :                     ptr_size, segment_size);
    7210                 :        267 :           printf ("   code_alignment_factor:    %u\n"
    7211                 :            :                   "   data_alignment_factor:    %d\n"
    7212                 :            :                   "   return_address_register:  %u\n",
    7213                 :            :                   code_alignment_factor,
    7214                 :            :                   data_alignment_factor, return_address_register);
    7215                 :            : 
    7216         [ +  + ]:        267 :           if (augmentation[0] == 'z')
    7217                 :            :             {
    7218         [ -  + ]:        227 :               if (cieend - readp < 1)
    7219                 :          0 :                 goto invalid_data;
    7220                 :            : 
    7221                 :        227 :               unsigned int augmentationlen;
    7222                 :        227 :               get_uleb128 (augmentationlen, readp, cieend);
    7223                 :            : 
    7224         [ -  + ]:        227 :               if (augmentationlen > (size_t) (cieend - readp))
    7225                 :            :                 {
    7226                 :          0 :                   error (0, 0, _("invalid augmentation length"));
    7227                 :          0 :                   readp = cieend;
    7228                 :          0 :                   continue;
    7229                 :            :                 }
    7230                 :            : 
    7231                 :        227 :               const char *hdr = "Augmentation data:";
    7232                 :        227 :               const char *cp = augmentation + 1;
    7233   [ +  +  +  - ]:        454 :               while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
    7234                 :            :                 {
    7235                 :        227 :                   printf ("   %-26s%#x ", hdr, *readp);
    7236                 :        227 :                   hdr = "";
    7237                 :            : 
    7238         [ +  - ]:        227 :                   if (*cp == 'R')
    7239                 :            :                     {
    7240                 :        227 :                       fde_encoding = *readp++;
    7241                 :        227 :                       print_encoding_base (_("FDE address encoding: "),
    7242                 :            :                                            fde_encoding);
    7243                 :            :                     }
    7244         [ #  # ]:          0 :                   else if (*cp == 'L')
    7245                 :            :                     {
    7246                 :          0 :                       lsda_encoding = *readp++;
    7247                 :          0 :                       print_encoding_base (_("LSDA pointer encoding: "),
    7248                 :            :                                            lsda_encoding);
    7249                 :            :                     }
    7250         [ #  # ]:          0 :                   else if (*cp == 'P')
    7251                 :            :                     {
    7252                 :            :                       /* Personality.  This field usually has a relocation
    7253                 :            :                          attached pointing to __gcc_personality_v0.  */
    7254                 :          0 :                       const unsigned char *startp = readp;
    7255                 :          0 :                       unsigned int encoding = *readp++;
    7256                 :          0 :                       uint64_t val = 0;
    7257                 :          0 :                       readp = read_encoded (encoding, readp,
    7258                 :          0 :                                             readp - 1 + augmentationlen,
    7259                 :            :                                             &val, dbg);
    7260                 :            : 
    7261                 :          0 :                       while (++startp < readp)
    7262         [ #  # ]:          0 :                         printf ("%#x ", *startp);
    7263                 :            : 
    7264                 :          0 :                       putchar ('(');
    7265                 :          0 :                       print_encoding (encoding);
    7266                 :          0 :                       putchar (' ');
    7267         [ #  # ]:          0 :                       switch (encoding & 0xf)
    7268                 :            :                         {
    7269                 :          0 :                         case DW_EH_PE_sleb128:
    7270                 :            :                         case DW_EH_PE_sdata2:
    7271                 :            :                         case DW_EH_PE_sdata4:
    7272                 :          0 :                           printf ("%" PRId64 ")\n", val);
    7273                 :            :                           break;
    7274                 :          0 :                         default:
    7275                 :          0 :                           printf ("%#" PRIx64 ")\n", val);
    7276                 :            :                           break;
    7277                 :            :                         }
    7278                 :            :                     }
    7279                 :            :                   else
    7280                 :          0 :                     printf ("(%x)\n", *readp++);
    7281                 :            : 
    7282                 :        227 :                   ++cp;
    7283                 :            :                 }
    7284                 :            :             }
    7285                 :            : 
    7286         [ +  - ]:        267 :           if (likely (ptr_size == 4 || ptr_size == 8))
    7287                 :            :             {
    7288                 :        267 :               struct cieinfo *newp = alloca (sizeof (*newp));
    7289                 :        267 :               newp->cie_offset = offset;
    7290                 :        267 :               newp->augmentation = augmentation;
    7291                 :        267 :               newp->fde_encoding = fde_encoding;
    7292                 :        267 :               newp->lsda_encoding = lsda_encoding;
    7293                 :        267 :               newp->address_size = ptr_size;
    7294                 :        267 :               newp->code_alignment_factor = code_alignment_factor;
    7295                 :        267 :               newp->data_alignment_factor = data_alignment_factor;
    7296                 :        267 :               newp->next = cies;
    7297                 :        267 :               cies = newp;
    7298                 :            :             }
    7299                 :            :         }
    7300                 :            :       else
    7301                 :            :         {
    7302                 :            :           struct cieinfo *cie = cies;
    7303         [ +  - ]:      59432 :           while (cie != NULL)
    7304   [ +  +  +  + ]:     118864 :             if (is_eh_frame
    7305                 :      59390 :                 ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
    7306                 :         42 :                 : cie_id == (Dwarf_Off) cie->cie_offset)
    7307                 :            :               break;
    7308                 :            :             else
    7309                 :      46337 :               cie = cie->next;
    7310         [ -  + ]:      13095 :           if (unlikely (cie == NULL))
    7311                 :            :             {
    7312                 :          0 :               puts ("invalid CIE reference in FDE");
    7313                 :          0 :               return;
    7314                 :            :             }
    7315                 :            : 
    7316                 :            :           /* Initialize from CIE data.  */
    7317                 :      13095 :           fde_encoding = cie->fde_encoding;
    7318                 :      13095 :           lsda_encoding = cie->lsda_encoding;
    7319                 :      13095 :           ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
    7320                 :      13095 :           code_alignment_factor = cie->code_alignment_factor;
    7321                 :      13095 :           data_alignment_factor = cie->data_alignment_factor;
    7322                 :            : 
    7323                 :      13095 :           const unsigned char *base = readp;
    7324                 :            :           // XXX There are sometimes relocations for this value
    7325   [ -  +  +  +  :      13095 :           initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
             +  +  +  + ]
    7326                 :      26190 :           Dwarf_Word address_range
    7327   [ +  +  +  +  :      13095 :             = read_addr_unaligned_inc (ptr_size, dbg, readp);
                   +  + ]
    7328                 :            : 
    7329                 :            :           /* pcrel for an FDE address is relative to the runtime
    7330                 :            :              address of the start_address field itself.  Sign extend
    7331                 :            :              if necessary to make sure the calculation is done on the
    7332                 :            :              full 64 bit address even when initial_location only holds
    7333                 :            :              the lower 32 bits.  */
    7334                 :      13095 :           Dwarf_Addr pc_start = initial_location;
    7335         [ +  + ]:      13095 :           if (ptr_size == 4)
    7336                 :      13055 :             pc_start = (uint64_t) (int32_t) pc_start;
    7337         [ +  + ]:      13095 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    7338                 :      13047 :             pc_start += ((uint64_t) shdr->sh_addr
    7339                 :      13047 :                          + (base - (const unsigned char *) data->d_buf)
    7340                 :      13047 :                          - bias);
    7341                 :            : 
    7342                 :      13095 :           printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
    7343                 :            :                   "   CIE_pointer:              %" PRIu64 "\n"
    7344                 :            :                   "   initial_location:         ",
    7345                 :            :                   offset, (uint64_t) unit_length,
    7346                 :            :                   cie->cie_offset, (uint64_t) cie_id);
    7347                 :      13095 :           print_dwarf_addr (dwflmod, cie->address_size,
    7348                 :            :                             pc_start, initial_location);
    7349         [ +  + ]:      13095 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    7350                 :            :             {
    7351                 :      26094 :               vma_base = (((uint64_t) shdr->sh_offset
    7352                 :      13047 :                            + (base - (const unsigned char *) data->d_buf)
    7353                 :      13047 :                            + (uint64_t) initial_location)
    7354                 :            :                           & (ptr_size == 4
    7355                 :            :                              ? UINT64_C (0xffffffff)
    7356         [ -  + ]:      13047 :                              : UINT64_C (0xffffffffffffffff)));
    7357                 :      13047 :               printf (_(" (offset: %#" PRIx64 ")"),
    7358                 :            :                       (uint64_t) vma_base);
    7359                 :            :             }
    7360                 :            : 
    7361                 :      13095 :           printf ("\n   address_range:            %#" PRIx64,
    7362                 :            :                   (uint64_t) address_range);
    7363         [ +  + ]:      13095 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    7364                 :      13047 :             printf (_(" (end offset: %#" PRIx64 ")"),
    7365                 :      13047 :                     ((uint64_t) vma_base + (uint64_t) address_range)
    7366                 :            :                     & (ptr_size == 4
    7367                 :            :                        ? UINT64_C (0xffffffff)
    7368         [ -  + ]:      13047 :                        : UINT64_C (0xffffffffffffffff)));
    7369                 :      13095 :           putchar ('\n');
    7370                 :            : 
    7371         [ +  + ]:      13095 :           if (cie->augmentation[0] == 'z')
    7372                 :            :             {
    7373                 :      13047 :               unsigned int augmentationlen;
    7374         [ -  + ]:      13047 :               if (cieend - readp < 1)
    7375                 :          0 :                 goto invalid_data;
    7376                 :      13047 :               get_uleb128 (augmentationlen, readp, cieend);
    7377                 :            : 
    7378         [ -  + ]:      13047 :               if (augmentationlen > (size_t) (cieend - readp))
    7379                 :            :                 {
    7380                 :          0 :                   error (0, 0, _("invalid augmentation length"));
    7381                 :          0 :                   readp = cieend;
    7382                 :          0 :                   continue;
    7383                 :            :                 }
    7384                 :            : 
    7385         [ -  + ]:      13047 :               if (augmentationlen > 0)
    7386                 :            :                 {
    7387                 :          0 :                   const char *hdr = "Augmentation data:";
    7388                 :          0 :                   const char *cp = cie->augmentation + 1;
    7389                 :          0 :                   unsigned int u = 0;
    7390         [ #  # ]:          0 :                   while (*cp != '\0'
    7391         [ #  # ]:          0 :                          && cp < cie->augmentation + augmentationlen + 1)
    7392                 :            :                     {
    7393         [ #  # ]:          0 :                       if (*cp == 'L')
    7394                 :            :                         {
    7395                 :          0 :                           uint64_t lsda_pointer;
    7396                 :          0 :                           const unsigned char *p
    7397                 :          0 :                             = read_encoded (lsda_encoding, &readp[u],
    7398                 :            :                                             &readp[augmentationlen],
    7399                 :            :                                             &lsda_pointer, dbg);
    7400                 :          0 :                           u = p - readp;
    7401                 :          0 :                           printf (_("\
    7402                 :            :    %-26sLSDA pointer: %#" PRIx64 "\n"),
    7403                 :            :                                   hdr, lsda_pointer);
    7404                 :          0 :                           hdr = "";
    7405                 :            :                         }
    7406                 :          0 :                       ++cp;
    7407                 :            :                     }
    7408                 :            : 
    7409         [ #  # ]:          0 :                   while (u < augmentationlen)
    7410                 :            :                     {
    7411                 :          0 :                       printf ("   %-26s%#x\n", hdr, readp[u++]);
    7412                 :          0 :                       hdr = "";
    7413                 :            :                     }
    7414                 :            :                 }
    7415                 :            : 
    7416                 :      13047 :               readp += augmentationlen;
    7417                 :            :             }
    7418                 :            :         }
    7419                 :            : 
    7420                 :            :       /* Handle the initialization instructions.  */
    7421         [ -  + ]:      13362 :       if (ptr_size != 4 && ptr_size !=8)
    7422                 :          0 :         printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
    7423                 :            :       else
    7424                 :      13362 :         print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
    7425                 :            :                            data_alignment_factor, version, ptr_size,
    7426                 :            :                            fde_encoding, dwflmod, ebl, ehdr, dbg);
    7427                 :      13362 :       readp = cieend;
    7428                 :            :     }
    7429                 :            : }
    7430                 :            : 
    7431                 :            : 
    7432                 :            : /* Returns the signedness (or false if it cannot be determined) and
    7433                 :            :    the byte size (or zero if it cannot be gotten) of the given DIE
    7434                 :            :    DW_AT_type attribute.  Uses dwarf_peel_type and dwarf_aggregate_size.  */
    7435                 :            : static void
    7436                 :     164199 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
    7437                 :            : {
    7438                 :     164199 :   Dwarf_Attribute attr;
    7439                 :     164199 :   Dwarf_Die type;
    7440                 :            : 
    7441                 :     164199 :   *bytes = 0;
    7442                 :     164199 :   *is_signed = false;
    7443                 :            : 
    7444         [ +  + ]:     164199 :   if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
    7445                 :            :                                                                 DW_AT_type,
    7446                 :            :                                                                 &attr), &type),
    7447                 :            :                        &type) == 0)
    7448                 :            :     {
    7449                 :        440 :       Dwarf_Word val;
    7450                 :        440 :       *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
    7451                 :            :                                                  &attr), &val) == 0
    7452   [ +  +  +  + ]:        440 :                     && (val == DW_ATE_signed || val == DW_ATE_signed_char));
    7453                 :            : 
    7454         [ +  - ]:        440 :       if (dwarf_aggregate_size (&type, &val) == 0)
    7455                 :        440 :         *bytes = val;
    7456                 :            :     }
    7457                 :     164199 : }
    7458                 :            : 
    7459                 :            : struct attrcb_args
    7460                 :            : {
    7461                 :            :   Dwfl_Module *dwflmod;
    7462                 :            :   Dwarf *dbg;
    7463                 :            :   Dwarf_Die *dies;
    7464                 :            :   int level;
    7465                 :            :   bool silent;
    7466                 :            :   bool is_split;
    7467                 :            :   unsigned int version;
    7468                 :            :   unsigned int addrsize;
    7469                 :            :   unsigned int offset_size;
    7470                 :            :   struct Dwarf_CU *cu;
    7471                 :            : };
    7472                 :            : 
    7473                 :            : 
    7474                 :            : static int
    7475                 :    3710576 : attr_callback (Dwarf_Attribute *attrp, void *arg)
    7476                 :            : {
    7477                 :    3710576 :   struct attrcb_args *cbargs = (struct attrcb_args *) arg;
    7478                 :    3710576 :   const int level = cbargs->level;
    7479                 :    3710576 :   Dwarf_Die *die = &cbargs->dies[level];
    7480                 :    3710576 :   bool is_split = cbargs->is_split;
    7481                 :            : 
    7482         [ +  - ]:    3710576 :   unsigned int attr = dwarf_whatattr (attrp);
    7483         [ -  + ]:    3710576 :   if (unlikely (attr == 0))
    7484                 :            :     {
    7485         [ #  # ]:          0 :       if (!cbargs->silent)
    7486                 :          0 :         error (0, 0, _("DIE [%" PRIx64 "] "
    7487                 :            :                               "cannot get attribute code: %s"),
    7488                 :            :                dwarf_dieoffset (die), dwarf_errmsg (-1));
    7489                 :          0 :       return DWARF_CB_ABORT;
    7490                 :            :     }
    7491                 :            : 
    7492         [ +  - ]:    3710576 :   unsigned int form = dwarf_whatform (attrp);
    7493         [ -  + ]:    3710576 :   if (unlikely (form == 0))
    7494                 :            :     {
    7495         [ #  # ]:          0 :       if (!cbargs->silent)
    7496                 :          0 :         error (0, 0, _("DIE [%" PRIx64 "] "
    7497                 :            :                               "cannot get attribute form: %s"),
    7498                 :            :                dwarf_dieoffset (die), dwarf_errmsg (-1));
    7499                 :          0 :       return DWARF_CB_ABORT;
    7500                 :            :     }
    7501                 :            : 
    7502   [ +  +  +  +  :    3710576 :   switch (form)
             +  +  +  +  
                      - ]
    7503                 :            :     {
    7504                 :      48145 :     case DW_FORM_addr:
    7505                 :            :     case DW_FORM_addrx:
    7506                 :            :     case DW_FORM_addrx1:
    7507                 :            :     case DW_FORM_addrx2:
    7508                 :            :     case DW_FORM_addrx3:
    7509                 :            :     case DW_FORM_addrx4:
    7510                 :            :     case DW_FORM_GNU_addr_index:
    7511         [ +  + ]:      48145 :       if (!cbargs->silent)
    7512                 :            :         {
    7513                 :      47874 :           Dwarf_Addr addr;
    7514         [ -  + ]:      47874 :           if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
    7515                 :            :             {
    7516                 :          0 :             attrval_out:
    7517         [ #  # ]:          0 :               if (!cbargs->silent)
    7518                 :          0 :                 error (0, 0, _("DIE [%" PRIx64 "] "
    7519                 :            :                                       "cannot get attribute '%s' (%s) value: "
    7520                 :            :                                       "%s"),
    7521                 :            :                        dwarf_dieoffset (die),
    7522                 :            :                        dwarf_attr_name (attr),
    7523                 :            :                        dwarf_form_name (form),
    7524                 :            :                        dwarf_errmsg (-1));
    7525                 :            :               /* Don't ABORT, it might be other attributes can be resolved.  */
    7526                 :          0 :               return DWARF_CB_OK;
    7527                 :            :             }
    7528         [ +  + ]:      47874 :           if (form != DW_FORM_addr )
    7529                 :            :             {
    7530                 :          8 :               Dwarf_Word word;
    7531         [ -  + ]:          8 :               if (dwarf_formudata (attrp, &word) != 0)
    7532                 :          0 :                 goto attrval_out;
    7533                 :          8 :               printf ("           %*s%-20s (%s) [%" PRIx64 "] ",
    7534                 :            :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7535                 :            :                       dwarf_form_name (form), word);
    7536                 :            :             }
    7537                 :            :           else
    7538                 :      47866 :             printf ("           %*s%-20s (%s) ",
    7539                 :            :                     (int) (level * 2), "", dwarf_attr_name (attr),
    7540                 :            :                     dwarf_form_name (form));
    7541                 :      47874 :           print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
    7542                 :      47874 :           printf ("\n");
    7543                 :            :         }
    7544                 :    3329848 :       break;
    7545                 :            : 
    7546                 :     629191 :     case DW_FORM_indirect:
    7547                 :            :     case DW_FORM_strp:
    7548                 :            :     case DW_FORM_line_strp:
    7549                 :            :     case DW_FORM_strx:
    7550                 :            :     case DW_FORM_strx1:
    7551                 :            :     case DW_FORM_strx2:
    7552                 :            :     case DW_FORM_strx3:
    7553                 :            :     case DW_FORM_strx4:
    7554                 :            :     case DW_FORM_string:
    7555                 :            :     case DW_FORM_GNU_strp_alt:
    7556                 :            :     case DW_FORM_GNU_str_index:
    7557         [ +  + ]:     629191 :       if (cbargs->silent)
    7558                 :            :         break;
    7559                 :     628436 :       const char *str = dwarf_formstring (attrp);
    7560         [ -  + ]:     628436 :       if (unlikely (str == NULL))
    7561                 :          0 :         goto attrval_out;
    7562                 :     628436 :       printf ("           %*s%-20s (%s) \"%s\"\n",
    7563                 :            :               (int) (level * 2), "", dwarf_attr_name (attr),
    7564                 :            :               dwarf_form_name (form), str);
    7565                 :            :       break;
    7566                 :            : 
    7567                 :     769595 :     case DW_FORM_ref_addr:
    7568                 :            :     case DW_FORM_ref_udata:
    7569                 :            :     case DW_FORM_ref8:
    7570                 :            :     case DW_FORM_ref4:
    7571                 :            :     case DW_FORM_ref2:
    7572                 :            :     case DW_FORM_ref1:
    7573                 :            :     case DW_FORM_GNU_ref_alt:
    7574                 :            :     case DW_FORM_ref_sup4:
    7575                 :            :     case DW_FORM_ref_sup8:
    7576         [ +  + ]:     769595 :       if (cbargs->silent)
    7577                 :            :         break;
    7578                 :     768805 :       Dwarf_Die ref;
    7579         [ -  + ]:     768805 :       if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
    7580                 :          0 :         goto attrval_out;
    7581                 :            : 
    7582                 :     768805 :       printf ("           %*s%-20s (%s) ",
    7583                 :            :               (int) (level * 2), "", dwarf_attr_name (attr),
    7584                 :            :               dwarf_form_name (form));
    7585         [ +  + ]:     768805 :       if (is_split)
    7586                 :         33 :         printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
    7587                 :            :       else
    7588                 :     768772 :         printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
    7589                 :            :       break;
    7590                 :            : 
    7591                 :          4 :     case DW_FORM_ref_sig8:
    7592         [ +  - ]:          4 :       if (cbargs->silent)
    7593                 :            :         break;
    7594                 :          4 :       printf ("           %*s%-20s (%s) {%6" PRIx64 "}\n",
    7595                 :            :               (int) (level * 2), "", dwarf_attr_name (attr),
    7596                 :            :               dwarf_form_name (form),
    7597         [ -  + ]:          4 :               (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
    7598                 :            :       break;
    7599                 :            : 
    7600                 :    2089838 :     case DW_FORM_sec_offset:
    7601                 :            :     case DW_FORM_rnglistx:
    7602                 :            :     case DW_FORM_loclistx:
    7603                 :            :     case DW_FORM_implicit_const:
    7604                 :            :     case DW_FORM_udata:
    7605                 :            :     case DW_FORM_sdata:
    7606                 :            :     case DW_FORM_data8: /* Note no data16 here, we see that as block. */
    7607                 :            :     case DW_FORM_data4:
    7608                 :            :     case DW_FORM_data2:
    7609                 :    2089838 :     case DW_FORM_data1:;
    7610                 :    2089838 :       Dwarf_Word num;
    7611         [ -  + ]:    2089838 :       if (unlikely (dwarf_formudata (attrp, &num) != 0))
    7612                 :          0 :         goto attrval_out;
    7613                 :            : 
    7614                 :    2089838 :       const char *valuestr = NULL;
    7615                 :    2089838 :       bool as_hex_id = false;
    7616   [ +  +  +  +  :    2089838 :       switch (attr)
          +  +  -  +  +  
          -  -  -  -  -  
          -  +  -  +  +  
                      + ]
    7617                 :            :         {
    7618                 :            :           /* This case can take either a constant or a loclistptr.  */
    7619                 :     258943 :         case DW_AT_data_member_location:
    7620         [ +  - ]:     258943 :           if (form != DW_FORM_sec_offset
    7621         [ +  + ]:     258943 :               && (cbargs->version >= 4
    7622         [ +  - ]:      23806 :                   || (form != DW_FORM_data4 && form != DW_FORM_data8)))
    7623                 :            :             {
    7624         [ +  - ]:     258943 :               if (!cbargs->silent)
    7625                 :     258943 :                 printf ("           %*s%-20s (%s) %" PRIuMAX "\n",
    7626                 :            :                         (int) (level * 2), "", dwarf_attr_name (attr),
    7627                 :            :                         dwarf_form_name (form), (uintmax_t) num);
    7628                 :     258943 :               return DWARF_CB_OK;
    7629                 :            :             }
    7630                 :     105031 :           FALLTHROUGH;
    7631                 :            : 
    7632                 :            :         /* These cases always take a loclist[ptr] and no constant. */
    7633                 :            :         case DW_AT_location:
    7634                 :            :         case DW_AT_data_location:
    7635                 :            :         case DW_AT_vtable_elem_location:
    7636                 :            :         case DW_AT_string_length:
    7637                 :            :         case DW_AT_use_location:
    7638                 :            :         case DW_AT_frame_base:
    7639                 :            :         case DW_AT_return_addr:
    7640                 :            :         case DW_AT_static_link:
    7641                 :            :         case DW_AT_segment:
    7642                 :            :         case DW_AT_GNU_call_site_value:
    7643                 :            :         case DW_AT_GNU_call_site_data_value:
    7644                 :            :         case DW_AT_GNU_call_site_target:
    7645                 :            :         case DW_AT_GNU_call_site_target_clobbered:
    7646                 :            :         case DW_AT_GNU_locviews:
    7647                 :            :           {
    7648                 :     105031 :             bool nlpt;
    7649         [ +  + ]:     105031 :             if (cbargs->cu->version < 5)
    7650                 :            :               {
    7651         [ +  + ]:     104958 :                 if (! cbargs->is_split)
    7652                 :            :                   {
    7653                 :     104938 :                     nlpt = notice_listptr (section_loc, &known_locsptr,
    7654                 :     104938 :                                            cbargs->addrsize,
    7655                 :     104938 :                                            cbargs->offset_size,
    7656                 :            :                                            cbargs->cu, num, attr);
    7657                 :            :                   }
    7658                 :            :                 else
    7659                 :            :                   nlpt = true;
    7660                 :            :               }
    7661                 :            :             else
    7662                 :            :               {
    7663                 :            :                 /* Only register for a real section offset.  Otherwise
    7664                 :            :                    it is a DW_FORM_loclistx which is just an index
    7665                 :            :                    number and we should already have registered the
    7666                 :            :                    section offset for the index when we saw the
    7667                 :            :                    DW_AT_loclists_base CU attribute.  */
    7668         [ +  + ]:         73 :                 if (form == DW_FORM_sec_offset)
    7669                 :         34 :                   nlpt = notice_listptr (section_loc, &known_loclistsptr,
    7670                 :         34 :                                          cbargs->addrsize, cbargs->offset_size,
    7671                 :            :                                          cbargs->cu, num, attr);
    7672                 :            :                 else
    7673                 :            :                   nlpt = true;
    7674                 :            : 
    7675                 :            :               }
    7676                 :            : 
    7677         [ +  + ]:     105031 :             if (!cbargs->silent)
    7678                 :            :               {
    7679   [ +  +  -  + ]:     104848 :                 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
    7680         [ -  + ]:     104841 :                   printf ("           %*s%-20s (%s) location list [%6"
    7681                 :            :                           PRIxMAX "]%s\n",
    7682                 :            :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7683                 :            :                           dwarf_form_name (form), (uintmax_t) num,
    7684                 :            :                           nlpt ? "" : " <WARNING offset too big>");
    7685                 :            :                 else
    7686                 :          7 :                   printf ("           %*s%-20s (%s) location index [%6"
    7687                 :            :                           PRIxMAX "]\n",
    7688                 :            :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7689                 :            :                           dwarf_form_name (form), (uintmax_t) num);
    7690                 :            :               }
    7691                 :            :           }
    7692                 :            :           return DWARF_CB_OK;
    7693                 :            : 
    7694                 :          5 :         case DW_AT_loclists_base:
    7695                 :            :           {
    7696                 :         10 :             bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
    7697                 :          5 :                                         cbargs->addrsize, cbargs->offset_size,
    7698                 :            :                                         cbargs->cu, num, attr);
    7699                 :            : 
    7700         [ +  + ]:          5 :             if (!cbargs->silent)
    7701         [ -  + ]:          1 :               printf ("           %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
    7702                 :            :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7703                 :            :                       dwarf_form_name (form), (uintmax_t) num,
    7704                 :            :                       nlpt ? "" : " <WARNING offset too big>");
    7705                 :            :           }
    7706                 :            :           return DWARF_CB_OK;
    7707                 :            : 
    7708                 :      16732 :         case DW_AT_ranges:
    7709                 :            :         case DW_AT_start_scope:
    7710                 :            :           {
    7711                 :      16732 :             bool nlpt;
    7712         [ +  + ]:      16732 :             if (cbargs->cu->version < 5)
    7713                 :      16715 :               nlpt = notice_listptr (section_ranges, &known_rangelistptr,
    7714                 :      16715 :                                      cbargs->addrsize, cbargs->offset_size,
    7715                 :            :                                      cbargs->cu, num, attr);
    7716                 :            :             else
    7717                 :            :               {
    7718                 :            :                 /* Only register for a real section offset.  Otherwise
    7719                 :            :                    it is a DW_FORM_rangelistx which is just an index
    7720                 :            :                    number and we should already have registered the
    7721                 :            :                    section offset for the index when we saw the
    7722                 :            :                    DW_AT_rnglists_base CU attribute.  */
    7723         [ +  + ]:         17 :                 if (form == DW_FORM_sec_offset)
    7724                 :         11 :                   nlpt = notice_listptr (section_ranges, &known_rnglistptr,
    7725                 :         11 :                                          cbargs->addrsize, cbargs->offset_size,
    7726                 :            :                                          cbargs->cu, num, attr);
    7727                 :            :                 else
    7728                 :            :                   nlpt = true;
    7729                 :            :               }
    7730                 :            : 
    7731         [ +  + ]:      16732 :             if (!cbargs->silent)
    7732                 :            :               {
    7733   [ +  +  +  + ]:      16689 :                 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
    7734         [ -  + ]:      16687 :                   printf ("           %*s%-20s (%s) range list [%6"
    7735                 :            :                           PRIxMAX "]%s\n",
    7736                 :            :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7737                 :            :                           dwarf_form_name (form), (uintmax_t) num,
    7738                 :            :                           nlpt ? "" : " <WARNING offset too big>");
    7739                 :            :                 else
    7740                 :          2 :                   printf ("           %*s%-20s (%s) range index [%6"
    7741                 :            :                           PRIxMAX "]\n",
    7742                 :            :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7743                 :            :                           dwarf_form_name (form), (uintmax_t) num);
    7744                 :            :               }
    7745                 :            :           }
    7746                 :            :           return DWARF_CB_OK;
    7747                 :            : 
    7748                 :          4 :         case DW_AT_rnglists_base:
    7749                 :            :           {
    7750                 :          8 :             bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
    7751                 :          4 :                                         cbargs->addrsize, cbargs->offset_size,
    7752                 :            :                                         cbargs->cu, num, attr);
    7753         [ +  + ]:          4 :             if (!cbargs->silent)
    7754         [ -  + ]:          2 :               printf ("           %*s%-20s (%s) range list [%6"
    7755                 :            :                       PRIxMAX "]%s\n",
    7756                 :            :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7757                 :            :                       dwarf_form_name (form), (uintmax_t) num,
    7758                 :            :                       nlpt ? "" : " <WARNING offset too big>");
    7759                 :            :           }
    7760                 :            :           return DWARF_CB_OK;
    7761                 :            : 
    7762                 :         13 :         case DW_AT_addr_base:
    7763                 :            :         case DW_AT_GNU_addr_base:
    7764                 :            :           {
    7765                 :         26 :             bool addrbase = notice_listptr (section_addr, &known_addrbases,
    7766                 :         13 :                                             cbargs->addrsize,
    7767                 :         13 :                                             cbargs->offset_size,
    7768                 :            :                                             cbargs->cu, num, attr);
    7769         [ +  + ]:         13 :             if (!cbargs->silent)
    7770         [ -  + ]:          5 :               printf ("           %*s%-20s (%s) address base [%6"
    7771                 :            :                       PRIxMAX "]%s\n",
    7772                 :            :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7773                 :            :                       dwarf_form_name (form), (uintmax_t) num,
    7774                 :            :                       addrbase ? "" : " <WARNING offset too big>");
    7775                 :            :           }
    7776                 :            :           return DWARF_CB_OK;
    7777                 :            : 
    7778                 :          0 :         case DW_AT_str_offsets_base:
    7779                 :            :           {
    7780                 :          0 :             bool stroffbase = notice_listptr (section_str, &known_stroffbases,
    7781                 :          0 :                                               cbargs->addrsize,
    7782                 :          0 :                                               cbargs->offset_size,
    7783                 :            :                                               cbargs->cu, num, attr);
    7784         [ #  # ]:          0 :             if (!cbargs->silent)
    7785         [ -  - ]:     380728 :               printf ("           %*s%-20s (%s) str offsets base [%6"
    7786                 :            :                       PRIxMAX "]%s\n",
    7787                 :            :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7788                 :            :                       dwarf_form_name (form), (uintmax_t) num,
    7789                 :            :                       stroffbase ? "" : " <WARNING offset too big>");
    7790                 :            :           }
    7791                 :            :           return DWARF_CB_OK;
    7792                 :            : 
    7793                 :       1765 :         case DW_AT_language:
    7794                 :       1765 :           valuestr = dwarf_lang_name (num);
    7795                 :       1765 :           break;
    7796                 :      31342 :         case DW_AT_encoding:
    7797                 :      31342 :           valuestr = dwarf_encoding_name (num);
    7798                 :      31342 :           break;
    7799                 :          0 :         case DW_AT_accessibility:
    7800                 :          0 :           valuestr = dwarf_access_name (num);
    7801                 :          0 :           break;
    7802                 :          0 :         case DW_AT_defaulted:
    7803                 :          0 :           valuestr = dwarf_defaulted_name (num);
    7804                 :          0 :           break;
    7805                 :          0 :         case DW_AT_visibility:
    7806                 :          0 :           valuestr = dwarf_visibility_name (num);
    7807                 :          0 :           break;
    7808                 :          0 :         case DW_AT_virtuality:
    7809                 :          0 :           valuestr = dwarf_virtuality_name (num);
    7810                 :          0 :           break;
    7811                 :          0 :         case DW_AT_identifier_case:
    7812                 :          0 :           valuestr = dwarf_identifier_case_name (num);
    7813                 :          0 :           break;
    7814                 :          0 :         case DW_AT_calling_convention:
    7815                 :          0 :           valuestr = dwarf_calling_convention_name (num);
    7816                 :          0 :           break;
    7817                 :       3503 :         case DW_AT_inline:
    7818                 :       3503 :           valuestr = dwarf_inline_name (num);
    7819                 :       3503 :           break;
    7820                 :          0 :         case DW_AT_ordering:
    7821                 :          0 :           valuestr = dwarf_ordering_name (num);
    7822                 :          0 :           break;
    7823                 :     455072 :         case DW_AT_decl_file:
    7824                 :            :         case DW_AT_call_file:
    7825                 :            :           {
    7826         [ +  + ]:     455072 :             if (cbargs->silent)
    7827                 :            :               break;
    7828                 :            : 
    7829                 :            :             /* Try to get the actual file, the current interface only
    7830                 :            :                gives us full paths, but we only want to show the file
    7831                 :            :                name for now.  */
    7832                 :     454648 :             Dwarf_Die cudie;
    7833         [ +  - ]:     454648 :             if (dwarf_cu_die (cbargs->cu, &cudie,
    7834                 :            :                               NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
    7835                 :            :               {
    7836                 :     454648 :                 Dwarf_Files *files;
    7837                 :     454648 :                 size_t nfiles;
    7838         [ +  - ]:     454648 :                 if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
    7839                 :            :                   {
    7840                 :     454648 :                     valuestr = dwarf_filesrc (files, num, NULL, NULL);
    7841         [ +  - ]:     454648 :                     if (valuestr != NULL)
    7842                 :            :                       {
    7843                 :     454648 :                         char *filename = strrchr (valuestr, '/');
    7844         [ +  - ]:     454648 :                         if (filename != NULL)
    7845                 :     454648 :                           valuestr = filename + 1;
    7846                 :            :                       }
    7847                 :            :                     else
    7848                 :          0 :                       error (0, 0, _("invalid file (%" PRId64 "): %s"),
    7849                 :            :                              num, dwarf_errmsg (-1));
    7850                 :            :                   }
    7851                 :            :                 else
    7852                 :          0 :                   error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
    7853                 :            :                          dwarf_dieoffset (&cudie));
    7854                 :            :               }
    7855                 :            :             else
    7856                 :          0 :              error (0, 0, _("couldn't get DWARF CU: %s"),
    7857                 :            :                     dwarf_errmsg (-1));
    7858         [ -  + ]:     454648 :             if (valuestr == NULL)
    7859                 :            :               valuestr = "???";
    7860                 :            :           }
    7861                 :     454648 :           break;
    7862                 :         13 :         case DW_AT_GNU_dwo_id:
    7863                 :         13 :           as_hex_id = true;
    7864                 :         13 :           break;
    7865                 :            : 
    7866                 :            :         default:
    7867                 :            :           /* Nothing.  */
    7868                 :            :           break;
    7869                 :            :         }
    7870                 :            : 
    7871         [ +  + ]:    1709110 :       if (cbargs->silent)
    7872                 :            :         break;
    7873                 :            : 
    7874                 :            :       /* When highpc is in constant form it is relative to lowpc.
    7875                 :            :          In that case also show the address.  */
    7876                 :    1707255 :       Dwarf_Addr highpc;
    7877   [ +  +  +  - ]:    1707255 :       if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
    7878                 :            :         {
    7879                 :      11501 :           printf ("           %*s%-20s (%s) %" PRIuMAX " (",
    7880                 :            :                   (int) (level * 2), "", dwarf_attr_name (attr),
    7881                 :            :                   dwarf_form_name (form), (uintmax_t) num);
    7882                 :      11501 :           print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
    7883                 :      11501 :           printf (")\n");
    7884                 :            :         }
    7885                 :            :       else
    7886                 :            :         {
    7887         [ +  + ]:    1695754 :           if (as_hex_id)
    7888                 :            :             {
    7889                 :          2 :               printf ("           %*s%-20s (%s) 0x%.16" PRIx64 "\n",
    7890                 :            :                       (int) (level * 2), "", dwarf_attr_name (attr),
    7891                 :            :                       dwarf_form_name (form), num);
    7892                 :            :             }
    7893                 :            :           else
    7894                 :            :             {
    7895                 :    1695752 :               Dwarf_Sword snum = 0;
    7896                 :    1695752 :               bool is_signed;
    7897                 :    1695752 :               int bytes = 0;
    7898         [ +  + ]:    1695752 :               if (attr == DW_AT_const_value)
    7899                 :     164195 :                 die_type_sign_bytes (die, &is_signed, &bytes);
    7900                 :            :               else
    7901                 :    1531557 :                 is_signed = (form == DW_FORM_sdata
    7902                 :    1531557 :                              || form == DW_FORM_implicit_const);
    7903                 :            : 
    7904         [ +  + ]:    1695752 :               if (is_signed)
    7905         [ -  + ]:        147 :                 if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
    7906                 :          0 :                   goto attrval_out;
    7907                 :            : 
    7908         [ +  + ]:    1695752 :               if (valuestr == NULL)
    7909                 :            :                 {
    7910                 :    1204799 :                   printf ("           %*s%-20s (%s) ",
    7911                 :            :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7912                 :            :                           dwarf_form_name (form));
    7913                 :            :                 }
    7914                 :            :               else
    7915                 :            :                 {
    7916                 :     490953 :                   printf ("           %*s%-20s (%s) %s (",
    7917                 :            :                           (int) (level * 2), "", dwarf_attr_name (attr),
    7918                 :            :                           dwarf_form_name (form), valuestr);
    7919                 :            :                 }
    7920                 :            : 
    7921   [ +  +  +  +  :    1695752 :               switch (bytes)
                      + ]
    7922                 :            :                 {
    7923                 :          2 :                 case 1:
    7924         [ +  + ]:          2 :                   if (is_signed)
    7925                 :          1 :                     printf ("%" PRId8, (int8_t) snum);
    7926                 :            :                   else
    7927                 :          1 :                     printf ("%" PRIu8, (uint8_t) num);
    7928                 :            :                   break;
    7929                 :            : 
    7930                 :          2 :                 case 2:
    7931         [ +  + ]:          2 :                   if (is_signed)
    7932                 :          1 :                     printf ("%" PRId16, (int16_t) snum);
    7933                 :            :                   else
    7934                 :          1 :                     printf ("%" PRIu16, (uint16_t) num);
    7935                 :            :                   break;
    7936                 :            : 
    7937                 :        130 :                 case 4:
    7938         [ +  + ]:        130 :                   if (is_signed)
    7939                 :        123 :                     printf ("%" PRId32, (int32_t) snum);
    7940                 :            :                   else
    7941                 :          7 :                     printf ("%" PRIu32, (uint32_t) num);
    7942                 :            :                   break;
    7943                 :            : 
    7944                 :        302 :                 case 8:
    7945         [ +  + ]:        302 :                   if (is_signed)
    7946                 :          1 :                     printf ("%" PRId64, (int64_t) snum);
    7947                 :            :                   else
    7948                 :        301 :                     printf ("%" PRIu64, (uint64_t) num);
    7949                 :            :                   break;
    7950                 :            : 
    7951                 :    1695316 :                 default:
    7952         [ +  + ]:    1695316 :                   if (is_signed)
    7953                 :         21 :                     printf ("%" PRIdMAX, (intmax_t) snum);
    7954                 :            :                   else
    7955                 :    1695295 :                     printf ("%" PRIuMAX, (uintmax_t) num);
    7956                 :            :                   break;
    7957                 :            :                 }
    7958                 :            : 
    7959                 :            :               /* Make clear if we switched from a signed encoding to
    7960                 :            :                  an unsigned value.  */
    7961         [ +  + ]:    1695752 :               if (attr == DW_AT_const_value
    7962         [ +  + ]:     164195 :                   && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
    7963         [ +  + ]:       1218 :                   && !is_signed)
    7964                 :       1212 :                 printf (" (%" PRIdMAX ")", (intmax_t) num);
    7965                 :            : 
    7966         [ +  + ]:    1695752 :               if (valuestr == NULL)
    7967                 :    1204799 :                 printf ("\n");
    7968                 :            :               else
    7969                 :    1695752 :                 printf (")\n");
    7970                 :            :             }
    7971                 :            :         }
    7972                 :            :       break;
    7973                 :            : 
    7974                 :       9122 :     case DW_FORM_flag:
    7975         [ +  + ]:       9122 :       if (cbargs->silent)
    7976                 :            :         break;
    7977                 :       9048 :       bool flag;
    7978         [ -  + ]:       9048 :       if (unlikely (dwarf_formflag (attrp, &flag) != 0))
    7979                 :          0 :         goto attrval_out;
    7980                 :            : 
    7981                 :       9048 :       printf ("           %*s%-20s (%s) %s\n",
    7982                 :            :               (int) (level * 2), "", dwarf_attr_name (attr),
    7983         [ +  - ]:       9048 :               dwarf_form_name (form), flag ? yes_str : no_str);
    7984                 :            :       break;
    7985                 :            : 
    7986                 :      61700 :     case DW_FORM_flag_present:
    7987         [ +  + ]:      61700 :       if (cbargs->silent)
    7988                 :            :         break;
    7989                 :      61423 :       printf ("           %*s%-20s (%s) %s\n",
    7990                 :            :               (int) (level * 2), "", dwarf_attr_name (attr),
    7991                 :            :               dwarf_form_name (form), yes_str);
    7992                 :            :       break;
    7993                 :            : 
    7994                 :     102981 :     case DW_FORM_exprloc:
    7995                 :            :     case DW_FORM_block4:
    7996                 :            :     case DW_FORM_block2:
    7997                 :            :     case DW_FORM_block1:
    7998                 :            :     case DW_FORM_block:
    7999                 :            :     case DW_FORM_data16: /* DWARF5 calls this a constant class.  */
    8000         [ +  + ]:     102981 :       if (cbargs->silent)
    8001                 :            :         break;
    8002                 :     102850 :       Dwarf_Block block;
    8003         [ -  + ]:     102850 :       if (unlikely (dwarf_formblock (attrp, &block) != 0))
    8004                 :          0 :         goto attrval_out;
    8005                 :            : 
    8006                 :     102850 :       printf ("           %*s%-20s (%s) ",
    8007                 :            :               (int) (level * 2), "", dwarf_attr_name (attr),
    8008                 :            :               dwarf_form_name (form));
    8009                 :            : 
    8010      [ -  +  + ]:     102850 :       switch (attr)
    8011                 :            :         {
    8012                 :          0 :         default:
    8013         [ #  # ]:          0 :           if (form != DW_FORM_exprloc)
    8014                 :            :             {
    8015                 :          0 :               print_block (block.length, block.data);
    8016                 :          0 :               break;
    8017                 :            :             }
    8018                 :     102846 :           FALLTHROUGH;
    8019                 :            : 
    8020                 :            :         case DW_AT_location:
    8021                 :            :         case DW_AT_data_location:
    8022                 :            :         case DW_AT_data_member_location:
    8023                 :            :         case DW_AT_vtable_elem_location:
    8024                 :            :         case DW_AT_string_length:
    8025                 :            :         case DW_AT_use_location:
    8026                 :            :         case DW_AT_frame_base:
    8027                 :            :         case DW_AT_return_addr:
    8028                 :            :         case DW_AT_static_link:
    8029                 :            :         case DW_AT_allocated:
    8030                 :            :         case DW_AT_associated:
    8031                 :            :         case DW_AT_bit_size:
    8032                 :            :         case DW_AT_bit_offset:
    8033                 :            :         case DW_AT_bit_stride:
    8034                 :            :         case DW_AT_byte_size:
    8035                 :            :         case DW_AT_byte_stride:
    8036                 :            :         case DW_AT_count:
    8037                 :            :         case DW_AT_lower_bound:
    8038                 :            :         case DW_AT_upper_bound:
    8039                 :            :         case DW_AT_GNU_call_site_value:
    8040                 :            :         case DW_AT_GNU_call_site_data_value:
    8041                 :            :         case DW_AT_GNU_call_site_target:
    8042                 :            :         case DW_AT_GNU_call_site_target_clobbered:
    8043         [ +  + ]:     102846 :           if (form == DW_FORM_exprloc
    8044         [ +  - ]:         70 :               || (form != DW_FORM_data16
    8045         [ +  - ]:         70 :                   && attrp->cu->version < 4)) /* blocks were expressions.  */
    8046                 :            :             {
    8047                 :     102846 :               putchar ('\n');
    8048                 :     102846 :               print_ops (cbargs->dwflmod, cbargs->dbg,
    8049                 :     102846 :                          12 + level * 2, 12 + level * 2,
    8050                 :            :                          cbargs->version, cbargs->addrsize, cbargs->offset_size,
    8051                 :     102846 :                          attrp->cu, block.length, block.data);
    8052                 :            :             }
    8053                 :            :           else
    8054                 :          0 :             print_block (block.length, block.data);
    8055                 :            :           break;
    8056                 :            : 
    8057                 :          4 :         case DW_AT_discr_list:
    8058         [ -  + ]:          4 :           if (block.length == 0)
    8059                 :          0 :             puts ("<default>");
    8060         [ +  - ]:          4 :           else if (form != DW_FORM_data16)
    8061                 :            :             {
    8062                 :          4 :               const unsigned char *readp = block.data;
    8063                 :          4 :               const unsigned char *readendp = readp + block.length;
    8064                 :            : 
    8065                 :            :               /* See if we are dealing with a signed or unsigned
    8066                 :            :                  values.  If the parent of this variant DIE is a
    8067                 :            :                  variant_part then it will either have a discriminant
    8068                 :            :                  which points to the member which type is the
    8069                 :            :                  discriminant type.  Or the variant_part itself has a
    8070                 :            :                  type representing the discriminant.  */
    8071                 :          4 :               bool is_signed = false;
    8072         [ +  - ]:          4 :               if (level > 0)
    8073                 :            :                 {
    8074                 :          4 :                   Dwarf_Die *parent = &cbargs->dies[level - 1];
    8075         [ +  - ]:          4 :                   if (dwarf_tag (die) == DW_TAG_variant
    8076         [ +  - ]:          4 :                       && dwarf_tag (parent) == DW_TAG_variant_part)
    8077                 :            :                     {
    8078                 :          4 :                       Dwarf_Die member;
    8079                 :          4 :                       Dwarf_Attribute discr_attr;
    8080                 :          4 :                       int bytes;
    8081         [ +  - ]:          4 :                       if (dwarf_formref_die (dwarf_attr (parent,
    8082                 :            :                                                          DW_AT_discr,
    8083                 :            :                                                          &discr_attr),
    8084                 :            :                                              &member) != NULL)
    8085                 :          4 :                         die_type_sign_bytes (&member, &is_signed, &bytes);
    8086                 :            :                       else
    8087                 :          0 :                         die_type_sign_bytes (parent, &is_signed, &bytes);
    8088                 :            :                     }
    8089                 :            :                 }
    8090                 :         12 :               while (readp < readendp)
    8091                 :            :                 {
    8092                 :          8 :                   int d = (int) *readp++;
    8093                 :          8 :                   printf ("%s ", dwarf_discr_list_name (d));
    8094         [ -  + ]:          8 :                   if (readp >= readendp)
    8095                 :          0 :                     goto attrval_out;
    8096                 :            : 
    8097                 :          8 :                   Dwarf_Word val;
    8098                 :          8 :                   Dwarf_Sword sval;
    8099         [ +  + ]:          8 :                   if (d == DW_DSC_label)
    8100                 :            :                     {
    8101         [ +  + ]:          4 :                       if (is_signed)
    8102                 :            :                         {
    8103                 :          2 :                           get_sleb128 (sval, readp, readendp);
    8104                 :          2 :                           printf ("%" PRId64 "", sval);
    8105                 :            :                         }
    8106                 :            :                       else
    8107                 :            :                         {
    8108                 :          2 :                           get_uleb128 (val, readp, readendp);
    8109                 :          2 :                           printf ("%" PRIu64 "", val);
    8110                 :            :                         }
    8111                 :            :                     }
    8112         [ +  - ]:          4 :                   else if (d == DW_DSC_range)
    8113                 :            :                     {
    8114         [ +  + ]:          4 :                       if (is_signed)
    8115                 :            :                         {
    8116                 :          3 :                           get_sleb128 (sval, readp, readendp);
    8117                 :          3 :                           printf ("%" PRId64 "..", sval);
    8118         [ -  + ]:          3 :                           if (readp >= readendp)
    8119                 :          0 :                             goto attrval_out;
    8120                 :          3 :                           get_sleb128 (sval, readp, readendp);
    8121                 :          3 :                           printf ("%" PRId64 "", sval);
    8122                 :            :                         }
    8123                 :            :                       else
    8124                 :            :                         {
    8125                 :          1 :                           get_uleb128 (val, readp, readendp);
    8126                 :          1 :                           printf ("%" PRIu64 "..", val);
    8127         [ -  + ]:          1 :                           if (readp >= readendp)
    8128                 :          0 :                             goto attrval_out;
    8129                 :          1 :                           get_uleb128 (val, readp, readendp);
    8130                 :          1 :                           printf ("%" PRIu64 "", val);
    8131                 :            :                         }
    8132                 :            :                     }
    8133                 :            :                   else
    8134                 :            :                     {
    8135                 :          0 :                       print_block (readendp - readp, readp);
    8136                 :          0 :                       break;
    8137                 :            :                     }
    8138         [ +  + ]:          8 :                   if (readp < readendp)
    8139         [ +  + ]:         16 :                     printf (", ");
    8140                 :            :                 }
    8141                 :          4 :               putchar ('\n');
    8142                 :            :             }
    8143                 :            :           else
    8144                 :          0 :             print_block (block.length, block.data);
    8145                 :            :           break;
    8146                 :            :         }
    8147                 :            :       break;
    8148                 :            : 
    8149                 :          0 :     default:
    8150         [ #  # ]:          0 :       if (cbargs->silent)
    8151                 :            :         break;
    8152                 :          0 :       printf ("           %*s%-20s (%s) ???\n",
    8153                 :            :               (int) (level * 2), "", dwarf_attr_name (attr),
    8154                 :            :               dwarf_form_name (form));
    8155                 :            :       break;
    8156                 :            :     }
    8157                 :            : 
    8158                 :    3329848 :   return DWARF_CB_OK;
    8159                 :            : }
    8160                 :            : 
    8161                 :            : static void
    8162                 :        101 : print_debug_units (Dwfl_Module *dwflmod,
    8163                 :            :                    Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
    8164                 :            :                    Elf_Scn *scn, GElf_Shdr *shdr,
    8165                 :            :                    Dwarf *dbg, bool debug_types)
    8166                 :            : {
    8167   [ +  +  +  + ]:        101 :   const bool silent = !(print_debug_sections & section_info) && !debug_types;
    8168                 :        101 :   const char *secname = section_name (ebl, shdr);
    8169                 :            : 
    8170                 :            :   /* Check section actually exists.  */
    8171         [ +  + ]:        101 :   if (!silent)
    8172         [ +  - ]:         65 :     if (get_debug_elf_data (dbg, ebl,
    8173                 :            :                             debug_types ? IDX_debug_types : IDX_debug_info,
    8174                 :            :                             scn) == NULL)
    8175                 :          0 :       return;
    8176                 :            : 
    8177         [ +  + ]:        101 :   if (!silent)
    8178                 :         65 :     printf (_("\
    8179                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
    8180                 :         65 :             elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
    8181                 :            : 
    8182                 :            :   /* If the section is empty we don't have to do anything.  */
    8183   [ +  +  +  - ]:        101 :   if (!silent && shdr->sh_size == 0)
    8184                 :            :     return;
    8185                 :            : 
    8186                 :        101 :   int maxdies = 20;
    8187                 :        101 :   Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
    8188                 :            : 
    8189                 :            :   /* New compilation unit.  */
    8190                 :        101 :   Dwarf_Half version;
    8191                 :            : 
    8192                 :        101 :   Dwarf_Die result;
    8193                 :        101 :   Dwarf_Off abbroffset;
    8194                 :        101 :   uint8_t addrsize;
    8195                 :        101 :   uint8_t offsize;
    8196                 :        101 :   uint64_t unit_id;
    8197                 :        101 :   Dwarf_Off subdie_off;
    8198                 :            : 
    8199                 :        101 :   int unit_res;
    8200                 :        101 :   Dwarf_CU *cu;
    8201                 :        101 :   Dwarf_CU cu_mem;
    8202                 :        101 :   uint8_t unit_type;
    8203                 :        101 :   Dwarf_Die cudie;
    8204                 :            : 
    8205                 :            :   /* We cheat a little because we want to see only the CUs from .debug_info
    8206                 :            :      or .debug_types.  We know the Dwarf_CU struct layout.  Set it up at
    8207                 :            :      the end of .debug_info if we want .debug_types only.  Check the returned
    8208                 :            :      Dwarf_CU is still in the expected section.  */
    8209         [ +  + ]:        101 :   if (debug_types)
    8210                 :            :     {
    8211                 :          1 :       cu_mem.dbg = dbg;
    8212                 :          1 :       cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
    8213                 :          1 :       cu_mem.sec_idx = IDX_debug_info;
    8214                 :          1 :       cu = &cu_mem;
    8215                 :            :     }
    8216                 :            :   else
    8217                 :        100 :     cu = NULL;
    8218                 :            : 
    8219                 :            :  next_cu:
    8220                 :       1875 :   unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
    8221                 :            :                               &cudie, NULL);
    8222         [ +  + ]:       1875 :   if (unit_res == 1)
    8223                 :        100 :     goto do_return;
    8224                 :            : 
    8225         [ -  + ]:       1775 :   if (unit_res == -1)
    8226                 :            :     {
    8227         [ #  # ]:          0 :       if (!silent)
    8228                 :          0 :         error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
    8229                 :          0 :       goto do_return;
    8230                 :            :     }
    8231                 :            : 
    8232   [ +  +  +  + ]:       3548 :   if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
    8233                 :          1 :     goto do_return;
    8234                 :            : 
    8235                 :       1774 :   dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
    8236                 :            :                 &unit_id, &subdie_off);
    8237                 :            : 
    8238         [ +  + ]:       1774 :   if (!silent)
    8239                 :            :     {
    8240                 :       1724 :       Dwarf_Off offset = cu->start;
    8241   [ +  +  +  - ]:       1724 :       if (debug_types && version < 5)
    8242                 :          2 :         {
    8243                 :          2 :           Dwarf_Die typedie;
    8244                 :          2 :           Dwarf_Off dieoffset;
    8245                 :          2 :           dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
    8246                 :            :                                                            + subdie_off,
    8247                 :            :                                                            &typedie));
    8248                 :          2 :           printf (_(" Type unit at offset %" PRIu64 ":\n"
    8249                 :            :                            " Version: %" PRIu16
    8250                 :            :                            ", Abbreviation section offset: %" PRIu64
    8251                 :            :                            ", Address size: %" PRIu8
    8252                 :            :                            ", Offset size: %" PRIu8
    8253                 :            :                            "\n Type signature: %#" PRIx64
    8254                 :            :                            ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
    8255                 :            :                   (uint64_t) offset, version, abbroffset, addrsize, offsize,
    8256                 :            :                   unit_id, (uint64_t) subdie_off, dieoffset);
    8257                 :            :         }
    8258                 :            :       else
    8259                 :            :         {
    8260                 :       1722 :           printf (_(" Compilation unit at offset %" PRIu64 ":\n"
    8261                 :            :                            " Version: %" PRIu16
    8262                 :            :                            ", Abbreviation section offset: %" PRIu64
    8263                 :            :                            ", Address size: %" PRIu8
    8264                 :            :                            ", Offset size: %" PRIu8 "\n"),
    8265                 :            :                   (uint64_t) offset, version, abbroffset, addrsize, offsize);
    8266                 :            : 
    8267         [ +  + ]:       1722 :           if (version >= 5 || (unit_type != DW_UT_compile
    8268         [ +  + ]:       1717 :                                && unit_type != DW_UT_partial))
    8269                 :            :             {
    8270                 :          6 :               printf (_(" Unit type: %s (%" PRIu8 ")"),
    8271                 :            :                                dwarf_unit_name (unit_type), unit_type);
    8272                 :          6 :               if (unit_type == DW_UT_type
    8273         [ +  + ]:          6 :                   || unit_type == DW_UT_skeleton
    8274         [ +  - ]:          1 :                   || unit_type == DW_UT_split_compile
    8275         [ -  + ]:          1 :                   || unit_type == DW_UT_split_type)
    8276                 :          5 :                 printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
    8277                 :          6 :               if (unit_type == DW_UT_type
    8278         [ -  + ]:          6 :                   || unit_type == DW_UT_split_type)
    8279                 :            :                 {
    8280                 :          0 :                   Dwarf_Die typedie;
    8281                 :          0 :                   Dwarf_Off dieoffset;
    8282                 :          0 :                   dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
    8283                 :            :                                  NULL, NULL, NULL);
    8284                 :          0 :                   dieoffset = dwarf_dieoffset (&typedie);
    8285                 :          0 :                   printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
    8286                 :            :                           subdie_off, dieoffset);
    8287                 :            :                 }
    8288                 :          6 :               printf ("\n");
    8289                 :            :             }
    8290                 :            :         }
    8291                 :            :     }
    8292                 :            : 
    8293         [ +  - ]:       1774 :   if (version < 2 || version > 5
    8294   [ +  -  -  + ]:       1774 :       || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
    8295                 :            :     {
    8296         [ #  # ]:          0 :       if (!silent)
    8297                 :          0 :         error (0, 0, _("unknown version (%d) or unit type (%d)"),
    8298                 :            :                version, unit_type);
    8299                 :          0 :       goto next_cu;
    8300                 :            :     }
    8301                 :            : 
    8302                 :       1774 :   struct attrcb_args args =
    8303                 :            :     {
    8304                 :            :       .dwflmod = dwflmod,
    8305                 :            :       .silent = silent,
    8306                 :            :       .version = version,
    8307                 :            :       .addrsize = addrsize,
    8308                 :            :       .offset_size = offsize
    8309                 :            :     };
    8310                 :            : 
    8311                 :       1774 :   bool is_split = false;
    8312                 :       1774 :   int level = 0;
    8313                 :       1774 :   dies[0] = cudie;
    8314                 :       1774 :   args.cu = dies[0].cu;
    8315                 :       1774 :   args.dbg = dbg;
    8316                 :       1774 :   args.is_split = is_split;
    8317                 :            : 
    8318                 :            :   /* We might return here again for the split CU subdie.  */
    8319                 :            :   do_cu:
    8320                 :     959687 :   do
    8321                 :            :     {
    8322                 :     959687 :       Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
    8323         [ -  + ]:     959687 :       if (unlikely (offset == (Dwarf_Off) -1))
    8324                 :            :         {
    8325         [ #  # ]:          0 :           if (!silent)
    8326                 :          0 :             error (0, 0, _("cannot get DIE offset: %s"),
    8327                 :            :                    dwarf_errmsg (-1));
    8328                 :          0 :           goto do_return;
    8329                 :            :         }
    8330                 :            : 
    8331                 :     959687 :       int tag = dwarf_tag (&dies[level]);
    8332         [ -  + ]:     959687 :       if (unlikely (tag == DW_TAG_invalid))
    8333                 :            :         {
    8334         [ #  # ]:          0 :           if (!silent)
    8335                 :          0 :             error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
    8336                 :            :                                   "] in section '%s': %s"),
    8337                 :            :                    (uint64_t) offset, secname, dwarf_errmsg (-1));
    8338                 :          0 :           goto do_return;
    8339                 :            :         }
    8340                 :            : 
    8341         [ +  + ]:     959687 :       if (!silent)
    8342                 :            :         {
    8343                 :     958694 :           unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
    8344         [ +  + ]:     958694 :           if (is_split)
    8345                 :         48 :             printf (" {%6" PRIx64 "}  ", (uint64_t) offset);
    8346                 :            :           else
    8347                 :     958646 :             printf (" [%6" PRIx64 "]  ", (uint64_t) offset);
    8348                 :     958694 :           printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
    8349                 :            :                   dwarf_tag_name (tag), code);
    8350                 :            :         }
    8351                 :            : 
    8352                 :            :       /* Print the attribute values.  */
    8353                 :     959687 :       args.level = level;
    8354                 :     959687 :       args.dies = dies;
    8355                 :     959687 :       (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
    8356                 :            : 
    8357                 :            :       /* Make room for the next level's DIE.  */
    8358         [ -  + ]:     959687 :       if (level + 1 == maxdies)
    8359                 :          0 :         dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
    8360                 :            : 
    8361                 :     959687 :       int res = dwarf_child (&dies[level], &dies[level + 1]);
    8362         [ +  + ]:     959687 :       if (res > 0)
    8363                 :            :         {
    8364         [ +  + ]:     959687 :           while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
    8365         [ +  + ]:     128957 :             if (level-- == 0)
    8366                 :            :               break;
    8367                 :            : 
    8368         [ -  + ]:     832508 :           if (unlikely (res == -1))
    8369                 :            :             {
    8370         [ #  # ]:          0 :               if (!silent)
    8371                 :          0 :                 error (0, 0, _("cannot get next DIE: %s\n"),
    8372                 :            :                        dwarf_errmsg (-1));
    8373                 :          0 :               goto do_return;
    8374                 :            :             }
    8375                 :            :         }
    8376         [ -  + ]:     127179 :       else if (unlikely (res < 0))
    8377                 :            :         {
    8378         [ #  # ]:          0 :           if (!silent)
    8379                 :          0 :             error (0, 0, _("cannot get next DIE: %s"),
    8380                 :            :                    dwarf_errmsg (-1));
    8381                 :          0 :           goto do_return;
    8382                 :            :         }
    8383                 :            :       else
    8384                 :            :         ++level;
    8385                 :            :     }
    8386         [ +  + ]:     959687 :   while (level >= 0);
    8387                 :            : 
    8388                 :            :   /* We might want to show the split compile unit if this was a skeleton.
    8389                 :            :      We need to scan it if we are requesting printing .debug_ranges for
    8390                 :            :      DWARF4 since GNU DebugFission uses "offsets" into the main ranges
    8391                 :            :      section.  */
    8392         [ +  + ]:       1778 :   if (unit_type == DW_UT_skeleton
    8393   [ +  +  +  + ]:         13 :       && ((!silent && show_split_units)
    8394   [ +  +  +  + ]:         10 :           || (version < 5 && (print_debug_sections & section_ranges) != 0)))
    8395                 :            :     {
    8396                 :          5 :       Dwarf_Die subdie;
    8397         [ +  - ]:          5 :       if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
    8398         [ +  + ]:          5 :           || dwarf_tag (&subdie) == DW_TAG_invalid)
    8399                 :            :         {
    8400         [ +  - ]:          1 :           if (!silent)
    8401                 :            :             {
    8402                 :          1 :               Dwarf_Attribute dwo_at;
    8403                 :          2 :               const char *dwo_name =
    8404                 :          1 :                 (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
    8405                 :            :                                                &dwo_at))
    8406         [ -  + ]:          1 :                  ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
    8407                 :            :                                                    &dwo_at))
    8408         [ #  # ]:          0 :                      ?: "<unknown>"));
    8409                 :          1 :               fprintf (stderr,
    8410                 :            :                        "Could not find split unit '%s', id: %" PRIx64 "\n",
    8411                 :            :                        dwo_name, unit_id);
    8412                 :            :             }
    8413                 :            :         }
    8414                 :            :       else
    8415                 :            :         {
    8416                 :          4 :           Dwarf_CU *split_cu = subdie.cu;
    8417                 :          4 :           dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
    8418                 :            :                         &addrsize, &offsize, &unit_id, &subdie_off);
    8419                 :          4 :           Dwarf_Off offset = cu->start;
    8420                 :            : 
    8421         [ +  + ]:          4 :           if (!silent)
    8422                 :            :             {
    8423                 :          2 :               printf (_(" Split compilation unit at offset %"
    8424                 :            :                                PRIu64 ":\n"
    8425                 :            :                                " Version: %" PRIu16
    8426                 :            :                                ", Abbreviation section offset: %" PRIu64
    8427                 :            :                                ", Address size: %" PRIu8
    8428                 :            :                                ", Offset size: %" PRIu8 "\n"),
    8429                 :            :                       (uint64_t) offset, version, abbroffset,
    8430                 :            :                       addrsize, offsize);
    8431                 :          2 :               printf (_(" Unit type: %s (%" PRIu8 ")"),
    8432                 :            :                       dwarf_unit_name (unit_type), unit_type);
    8433                 :          2 :               printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
    8434                 :          2 :               printf ("\n");
    8435                 :            :             }
    8436                 :            : 
    8437                 :          4 :           unit_type = DW_UT_split_compile;
    8438                 :          4 :           is_split = true;
    8439                 :          4 :           level = 0;
    8440                 :          4 :           dies[0] = subdie;
    8441                 :          4 :           args.cu = dies[0].cu;
    8442                 :          4 :           args.dbg = split_cu->dbg;
    8443                 :          4 :           args.is_split = is_split;
    8444                 :          4 :           goto do_cu;
    8445                 :            :         }
    8446                 :            :     }
    8447                 :            : 
    8448                 :            :   /* And again... */
    8449                 :       1774 :   goto next_cu;
    8450                 :            : 
    8451                 :        101 :  do_return:
    8452                 :        101 :   free (dies);
    8453                 :            : }
    8454                 :            : 
    8455                 :            : static void
    8456                 :        100 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    8457                 :            :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    8458                 :            : {
    8459                 :         20 :   print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
    8460                 :         20 : }
    8461                 :            : 
    8462                 :            : static void
    8463                 :          1 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    8464                 :            :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    8465                 :            : {
    8466                 :          1 :   print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
    8467                 :          1 : }
    8468                 :            : 
    8469                 :            : 
    8470                 :            : static void
    8471                 :         39 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
    8472                 :            :                             GElf_Ehdr *ehdr __attribute__ ((unused)),
    8473                 :            :                             Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    8474                 :            : {
    8475                 :         39 :   printf (_("\
    8476                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
    8477                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    8478                 :         39 :           (uint64_t) shdr->sh_offset);
    8479                 :            : 
    8480                 :         78 :   size_t address_size
    8481         [ +  + ]:         39 :     = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    8482                 :            : 
    8483                 :         39 :   Dwarf_Lines *lines;
    8484                 :         39 :   size_t nlines;
    8485                 :         39 :   Dwarf_Off off, next_off = 0;
    8486                 :         39 :   Dwarf_CU *cu = NULL;
    8487         [ +  + ]:         84 :   while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
    8488                 :            :                            &lines, &nlines) == 0)
    8489                 :            :     {
    8490                 :         45 :       Dwarf_Die cudie;
    8491   [ +  +  +  - ]:         45 :       if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
    8492                 :            :                                        NULL, NULL, NULL, NULL) == 0)
    8493                 :         43 :         printf (" CU [%" PRIx64 "] %s\n",
    8494                 :            :                 dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
    8495                 :            :       else
    8496                 :            :         {
    8497                 :            :           /* DWARF5 lines can be independent of any CU, but they probably
    8498                 :            :              are used by some CU.  Determine the CU this block is for.  */
    8499                 :          2 :           Dwarf_Off cuoffset;
    8500                 :          2 :           Dwarf_Off ncuoffset = 0;
    8501                 :          2 :           size_t hsize;
    8502         [ +  - ]:          3 :           while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
    8503                 :            :                                NULL, NULL, NULL) == 0)
    8504                 :            :             {
    8505         [ -  + ]:          3 :               if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
    8506                 :          0 :                 continue;
    8507                 :          3 :               Dwarf_Attribute stmt_list;
    8508         [ -  + ]:          3 :               if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
    8509                 :          0 :                 continue;
    8510                 :          3 :               Dwarf_Word lineoff;
    8511         [ -  + ]:          3 :               if (dwarf_formudata (&stmt_list, &lineoff) != 0)
    8512                 :          0 :                 continue;
    8513         [ +  + ]:          3 :               if (lineoff == off)
    8514                 :            :                 {
    8515                 :            :                   /* Found the CU.  */
    8516                 :          2 :                   cu = cudie.cu;
    8517                 :          2 :                   break;
    8518                 :            :                 }
    8519                 :            :             }
    8520                 :            : 
    8521         [ +  - ]:          2 :           if (cu != NULL)
    8522                 :          2 :             printf (" CU [%" PRIx64 "] %s\n",
    8523                 :            :                     dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
    8524                 :            :           else
    8525                 :          2 :             printf (" No CU\n");
    8526                 :            :         }
    8527                 :            : 
    8528                 :         45 :       printf ("  line:col SBPE* disc isa op address"
    8529                 :            :               " (Statement Block Prologue Epilogue *End)\n");
    8530                 :         45 :       const char *last_file = "";
    8531         [ +  + ]:        348 :       for (size_t n = 0; n < nlines; n++)
    8532                 :            :         {
    8533                 :        303 :           Dwarf_Line *line = dwarf_onesrcline (lines, n);
    8534         [ -  + ]:        303 :           if (line == NULL)
    8535                 :            :             {
    8536                 :          0 :               printf ("  dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
    8537                 :          0 :               continue;
    8538                 :            :             }
    8539                 :        303 :           Dwarf_Word mtime, length;
    8540                 :        303 :           const char *file = dwarf_linesrc (line, &mtime, &length);
    8541         [ -  + ]:        303 :           if (file == NULL)
    8542                 :            :             {
    8543                 :          0 :               printf ("  <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
    8544                 :          0 :               last_file = "";
    8545                 :            :             }
    8546         [ +  + ]:        303 :           else if (strcmp (last_file, file) != 0)
    8547                 :            :             {
    8548                 :         53 :               printf ("  %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
    8549                 :            :                       file, mtime, length);
    8550                 :         53 :               last_file = file;
    8551                 :            :             }
    8552                 :            : 
    8553                 :        303 :           int lineno, colno;
    8554                 :        303 :           bool statement, endseq, block, prologue_end, epilogue_begin;
    8555                 :        303 :           unsigned int lineop, isa, disc;
    8556                 :        303 :           Dwarf_Addr address;
    8557                 :        303 :           dwarf_lineaddr (line, &address);
    8558                 :        303 :           dwarf_lineno (line, &lineno);
    8559                 :        303 :           dwarf_linecol (line, &colno);
    8560                 :        303 :           dwarf_lineop_index (line, &lineop);
    8561                 :        303 :           dwarf_linebeginstatement (line, &statement);
    8562                 :        303 :           dwarf_lineendsequence (line, &endseq);
    8563                 :        303 :           dwarf_lineblock (line, &block);
    8564                 :        303 :           dwarf_lineprologueend (line, &prologue_end);
    8565                 :        303 :           dwarf_lineepiloguebegin (line, &epilogue_begin);
    8566                 :        303 :           dwarf_lineisa (line, &isa);
    8567                 :        303 :           dwarf_linediscriminator (line, &disc);
    8568                 :            : 
    8569                 :            :           /* End sequence is special, it is one byte past.  */
    8570                 :        303 :           printf ("  %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
    8571                 :            :                   lineno, colno,
    8572         [ +  + ]:        303 :                   (statement ? 'S' : ' '),
    8573         [ +  - ]:        303 :                   (block ? 'B' : ' '),
    8574         [ +  + ]:        303 :                   (prologue_end ? 'P' : ' '),
    8575         [ +  - ]:        303 :                   (epilogue_begin ? 'E' : ' '),
    8576         [ +  + ]:        303 :                   (endseq ? '*' : ' '),
    8577                 :            :                   disc, isa, lineop);
    8578                 :        303 :           print_dwarf_addr (dwflmod, address_size,
    8579         [ +  + ]:        303 :                             address - (endseq ? 1 : 0), address);
    8580                 :        303 :           printf ("\n");
    8581                 :            : 
    8582         [ +  + ]:        303 :           if (endseq)
    8583                 :        303 :             printf("\n");
    8584                 :            :         }
    8585                 :            :     }
    8586                 :         39 : }
    8587                 :            : 
    8588                 :            : 
    8589                 :            : /* Print the value of a form.
    8590                 :            :    Returns new value of readp, or readendp on failure.  */
    8591                 :            : static const unsigned char *
    8592                 :         20 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
    8593                 :            :                  const unsigned char *readendp, unsigned int offset_len,
    8594                 :            :                  Dwarf_Off str_offsets_base)
    8595                 :            : {
    8596                 :         20 :   Dwarf_Word val;
    8597                 :         20 :   unsigned char *endp;
    8598                 :         20 :   Elf_Data *data;
    8599                 :         20 :   char *str;
    8600   [ +  -  -  -  :         20 :   switch (form)
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  -  
                   -  - ]
    8601                 :            :     {
    8602                 :          8 :     case DW_FORM_data1:
    8603         [ -  + ]:          8 :       if (readendp - readp < 1)
    8604                 :            :         {
    8605                 :          0 :         invalid_data:
    8606                 :          0 :           error (0, 0, "invalid data");
    8607                 :          0 :           return readendp;
    8608                 :            :         }
    8609                 :          8 :       val = *readp++;
    8610                 :          8 :       printf (" %" PRIx8, (unsigned int) val);
    8611                 :            :       break;
    8612                 :            : 
    8613                 :          0 :     case DW_FORM_data2:
    8614         [ #  # ]:          0 :       if (readendp - readp < 2)
    8615                 :          0 :         goto invalid_data;
    8616         [ #  # ]:          0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    8617                 :          0 :       printf(" %" PRIx16, (unsigned int) val);
    8618                 :            :       break;
    8619                 :            : 
    8620                 :          0 :     case DW_FORM_data4:
    8621         [ #  # ]:          0 :       if (readendp - readp < 4)
    8622                 :          0 :         goto invalid_data;
    8623         [ #  # ]:          0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    8624                 :          0 :       printf (" %" PRIx32, (unsigned int) val);
    8625                 :            :       break;
    8626                 :            : 
    8627                 :          0 :     case DW_FORM_data8:
    8628         [ #  # ]:          0 :       if (readendp - readp < 8)
    8629                 :          0 :         goto invalid_data;
    8630         [ #  # ]:          0 :       val = read_8ubyte_unaligned_inc (dbg, readp);
    8631                 :          0 :       printf (" %" PRIx64, val);
    8632                 :            :       break;
    8633                 :            : 
    8634                 :          0 :     case DW_FORM_sdata:
    8635         [ #  # ]:          0 :       if (readendp - readp < 1)
    8636                 :          0 :         goto invalid_data;
    8637                 :          0 :       get_sleb128 (val, readp, readendp);
    8638                 :          0 :       printf (" %" PRIx64, val);
    8639                 :            :       break;
    8640                 :            : 
    8641                 :          0 :     case DW_FORM_udata:
    8642         [ #  # ]:          0 :       if (readendp - readp < 1)
    8643                 :          0 :         goto invalid_data;
    8644                 :          0 :       get_uleb128 (val, readp, readendp);
    8645                 :          0 :       printf (" %" PRIx64, val);
    8646                 :            :       break;
    8647                 :            : 
    8648                 :          0 :     case DW_FORM_block:
    8649         [ #  # ]:          0 :       if (readendp - readp < 1)
    8650                 :          0 :         goto invalid_data;
    8651                 :          0 :       get_uleb128 (val, readp, readendp);
    8652         [ #  # ]:          0 :       if ((size_t) (readendp - readp) < val)
    8653                 :          0 :         goto invalid_data;
    8654                 :          0 :       print_bytes (val, readp);
    8655                 :          0 :       readp += val;
    8656                 :          0 :       break;
    8657                 :            : 
    8658                 :          0 :     case DW_FORM_block1:
    8659         [ #  # ]:          0 :       if (readendp - readp < 1)
    8660                 :          0 :         goto invalid_data;
    8661                 :          0 :       val = *readp++;
    8662         [ #  # ]:          0 :       if ((size_t) (readendp - readp) < val)
    8663                 :          0 :         goto invalid_data;
    8664                 :          0 :       print_bytes (val, readp);
    8665                 :          0 :       readp += val;
    8666                 :          0 :       break;
    8667                 :            : 
    8668                 :          0 :     case DW_FORM_block2:
    8669         [ #  # ]:          0 :       if (readendp - readp < 2)
    8670                 :          0 :         goto invalid_data;
    8671         [ #  # ]:          0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    8672         [ #  # ]:          0 :       if ((size_t) (readendp - readp) < val)
    8673                 :          0 :         goto invalid_data;
    8674                 :          0 :       print_bytes (val, readp);
    8675                 :          0 :       readp += val;
    8676                 :          0 :       break;
    8677                 :            : 
    8678                 :          0 :     case DW_FORM_block4:
    8679         [ #  # ]:          0 :       if (readendp - readp < 4)
    8680                 :          0 :         goto invalid_data;
    8681         [ #  # ]:          0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    8682         [ #  # ]:          0 :       if ((size_t) (readendp - readp) < val)
    8683                 :          0 :         goto invalid_data;
    8684                 :          0 :       print_bytes (val, readp);
    8685                 :          0 :       readp += val;
    8686                 :          0 :       break;
    8687                 :            : 
    8688                 :          0 :     case DW_FORM_data16:
    8689         [ #  # ]:          0 :       if (readendp - readp < 16)
    8690                 :          0 :         goto invalid_data;
    8691                 :          0 :       print_bytes (16, readp);
    8692                 :          0 :       readp += 16;
    8693                 :          0 :       break;
    8694                 :            : 
    8695                 :          0 :     case DW_FORM_flag:
    8696         [ #  # ]:          0 :       if (readendp - readp < 1)
    8697                 :          0 :         goto invalid_data;
    8698                 :          0 :       val = *readp++;
    8699         [ #  # ]:          0 :       printf ("%s", val != 0 ? yes_str : no_str);
    8700                 :            :       break;
    8701                 :            : 
    8702                 :          0 :     case DW_FORM_string:
    8703                 :          0 :       endp = memchr (readp, '\0', readendp - readp);
    8704         [ #  # ]:          0 :       if (endp == NULL)
    8705                 :          0 :         goto invalid_data;
    8706                 :          0 :       printf ("%s", readp);
    8707                 :          0 :       readp = endp + 1;
    8708                 :          0 :       break;
    8709                 :            : 
    8710                 :         12 :     case DW_FORM_strp:
    8711                 :            :     case DW_FORM_line_strp:
    8712                 :            :     case DW_FORM_strp_sup:
    8713         [ -  + ]:         12 :       if ((size_t) (readendp - readp) < offset_len)
    8714                 :          0 :         goto invalid_data;
    8715         [ -  + ]:         12 :       if (offset_len == 8)
    8716         [ #  # ]:          0 :         val = read_8ubyte_unaligned_inc (dbg, readp);
    8717                 :            :       else
    8718         [ -  + ]:         12 :         val = read_4ubyte_unaligned_inc (dbg, readp);
    8719         [ -  + ]:         12 :       if (form == DW_FORM_strp)
    8720                 :          0 :         data = dbg->sectiondata[IDX_debug_str];
    8721         [ +  - ]:         12 :       else if (form == DW_FORM_line_strp)
    8722                 :         12 :         data = dbg->sectiondata[IDX_debug_line_str];
    8723                 :            :       else /* form == DW_FORM_strp_sup */
    8724                 :            :         {
    8725                 :          0 :           Dwarf *alt = dwarf_getalt (dbg);
    8726         [ #  # ]:          0 :           data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
    8727                 :            :         }
    8728   [ +  -  +  - ]:         12 :       if (data == NULL || val >= data->d_size
    8729         [ +  - ]:         12 :           || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
    8730                 :            :         str = "???";
    8731                 :            :       else
    8732                 :         12 :         str = (char *) data->d_buf + val;
    8733                 :         12 :       printf ("%s (%" PRIu64 ")", str, val);
    8734                 :            :       break;
    8735                 :            : 
    8736                 :          0 :     case DW_FORM_sec_offset:
    8737         [ #  # ]:          0 :       if ((size_t) (readendp - readp) < offset_len)
    8738                 :          0 :         goto invalid_data;
    8739         [ #  # ]:          0 :       if (offset_len == 8)
    8740         [ #  # ]:          0 :         val = read_8ubyte_unaligned_inc (dbg, readp);
    8741                 :            :       else
    8742         [ #  # ]:          0 :         val = read_4ubyte_unaligned_inc (dbg, readp);
    8743                 :          0 :       printf ("[%" PRIx64 "]", val);
    8744                 :            :       break;
    8745                 :            : 
    8746                 :          0 :     case DW_FORM_strx:
    8747                 :            :     case DW_FORM_GNU_str_index:
    8748         [ #  # ]:          0 :       if (readendp - readp < 1)
    8749                 :          0 :         goto invalid_data;
    8750                 :          0 :       get_uleb128 (val, readp, readendp);
    8751                 :          0 :     strx_val:
    8752                 :          0 :       data = dbg->sectiondata[IDX_debug_str_offsets];
    8753         [ #  # ]:          0 :       if (data == NULL
    8754         [ #  # ]:          0 :           || data->d_size - str_offsets_base < val)
    8755                 :            :         str = "???";
    8756                 :            :       else
    8757                 :            :         {
    8758                 :          0 :           const unsigned char *strreadp = data->d_buf + str_offsets_base + val;
    8759                 :          0 :           const unsigned char *strreadendp = data->d_buf + data->d_size;
    8760         [ #  # ]:          0 :           if ((size_t) (strreadendp - strreadp) < offset_len)
    8761                 :            :             str = "???";
    8762                 :            :           else
    8763                 :            :             {
    8764                 :          0 :               Dwarf_Off idx;
    8765         [ #  # ]:          0 :               if (offset_len == 8)
    8766         [ #  # ]:          0 :                 idx = read_8ubyte_unaligned (dbg, strreadp);
    8767                 :            :               else
    8768         [ #  # ]:          0 :                 idx = read_4ubyte_unaligned (dbg, strreadp);
    8769                 :            : 
    8770                 :          0 :               data = dbg->sectiondata[IDX_debug_str];
    8771   [ #  #  #  # ]:          0 :               if (data == NULL || idx >= data->d_size
    8772         [ #  # ]:          0 :                   || memchr (data->d_buf + idx, '\0',
    8773                 :            :                              data->d_size - idx) == NULL)
    8774                 :            :                 str = "???";
    8775                 :            :               else
    8776                 :          0 :                 str = (char *) data->d_buf + idx;
    8777                 :            :             }
    8778                 :            :         }
    8779                 :          0 :       printf ("%s (%" PRIu64 ")", str, val);
    8780                 :            :       break;
    8781                 :            : 
    8782                 :          0 :     case DW_FORM_strx1:
    8783         [ #  # ]:          0 :       if (readendp - readp < 1)
    8784                 :          0 :         goto invalid_data;
    8785                 :          0 :       val = *readp++;
    8786                 :          0 :       goto strx_val;
    8787                 :            : 
    8788                 :          0 :     case DW_FORM_strx2:
    8789         [ #  # ]:          0 :       if (readendp - readp < 2)
    8790                 :          0 :         goto invalid_data;
    8791         [ #  # ]:          0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    8792                 :          0 :       goto strx_val;
    8793                 :            : 
    8794                 :          0 :     case DW_FORM_strx3:
    8795         [ #  # ]:          0 :       if (readendp - readp < 3)
    8796                 :          0 :         goto invalid_data;
    8797                 :          0 :       val = read_3ubyte_unaligned_inc (dbg, readp);
    8798                 :          0 :       goto strx_val;
    8799                 :            : 
    8800                 :          0 :     case DW_FORM_strx4:
    8801         [ #  # ]:          0 :       if (readendp - readp < 4)
    8802                 :          0 :         goto invalid_data;
    8803         [ #  # ]:          0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    8804                 :          0 :       goto strx_val;
    8805                 :            : 
    8806                 :          0 :     default:
    8807                 :          0 :       error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
    8808                 :          0 :       return readendp;
    8809                 :            :     }
    8810                 :            : 
    8811                 :         20 :   return readp;
    8812                 :            : }
    8813                 :            : 
    8814                 :            : /* Only used via run_advance_pc() macro */
    8815                 :            : static inline void
    8816                 :     361695 : run_advance_pc (unsigned int op_advance,
    8817                 :            :                 unsigned int minimum_instr_len,
    8818                 :            :                 unsigned int max_ops_per_instr,
    8819                 :            :                 unsigned int *op_addr_advance,
    8820                 :            :                 Dwarf_Word *address,
    8821                 :            :                 unsigned int *op_index)
    8822                 :            : {
    8823                 :     361695 :   const unsigned int advanced_op_index = (*op_index) + op_advance;
    8824                 :            : 
    8825                 :     361695 :   *op_addr_advance = minimum_instr_len * (advanced_op_index
    8826                 :     361695 :                                          / max_ops_per_instr);
    8827                 :     361695 :   *address = *address + *op_addr_advance;
    8828                 :     361695 :   *op_index = advanced_op_index % max_ops_per_instr;
    8829                 :            : }
    8830                 :            : 
    8831                 :            : static void
    8832                 :         89 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    8833                 :            :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
    8834                 :            : {
    8835         [ +  + ]:         89 :   if (decodedline)
    8836                 :            :     {
    8837                 :         39 :       print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
    8838                 :         78 :       return;
    8839                 :            :     }
    8840                 :            : 
    8841                 :         50 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
    8842         [ +  - ]:         50 :   if (data == NULL)
    8843                 :            :     return;
    8844                 :            : 
    8845                 :         50 :   printf (_("\
    8846                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    8847                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    8848                 :         50 :           (uint64_t) shdr->sh_offset);
    8849                 :            : 
    8850         [ +  - ]:         50 :   if (shdr->sh_size == 0)
    8851                 :            :     return;
    8852                 :            : 
    8853                 :            :   /* There is no functionality in libdw to read the information in the
    8854                 :            :      way it is represented here.  Hardcode the decoder.  */
    8855                 :            : 
    8856                 :         50 :   const unsigned char *linep = (const unsigned char *) data->d_buf;
    8857                 :         50 :   const unsigned char *lineendp;
    8858                 :            : 
    8859                 :         50 :   while (linep
    8860         [ +  + ]:       1760 :          < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
    8861                 :            :     {
    8862                 :       1710 :       size_t start_offset = linep - (const unsigned char *) data->d_buf;
    8863                 :            : 
    8864                 :       1710 :       printf (_("\nTable at offset %zu:\n"), start_offset);
    8865                 :            : 
    8866         [ -  + ]:       1710 :       if (unlikely (linep + 4 > lineendp))
    8867                 :          0 :         goto invalid_data;
    8868         [ +  + ]:       1710 :       Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
    8869                 :       1710 :       unsigned int length = 4;
    8870         [ -  + ]:       1710 :       if (unlikely (unit_length == 0xffffffff))
    8871                 :            :         {
    8872         [ #  # ]:          0 :           if (unlikely (linep + 8 > lineendp))
    8873                 :            :             {
    8874                 :          0 :             invalid_data:
    8875                 :          0 :               error (0, 0, _("invalid data in section [%zu] '%s'"),
    8876                 :            :                      elf_ndxscn (scn), section_name (ebl, shdr));
    8877                 :          0 :               return;
    8878                 :            :             }
    8879         [ #  # ]:          0 :           unit_length = read_8ubyte_unaligned_inc (dbg, linep);
    8880                 :          0 :           length = 8;
    8881                 :            :         }
    8882                 :            : 
    8883                 :            :       /* Check whether we have enough room in the section.  */
    8884         [ -  + ]:       1710 :       if (unlikely (unit_length > (size_t) (lineendp - linep)))
    8885                 :          0 :         goto invalid_data;
    8886                 :       1710 :       lineendp = linep + unit_length;
    8887                 :            : 
    8888                 :            :       /* The next element of the header is the version identifier.  */
    8889         [ -  + ]:       1710 :       if ((size_t) (lineendp - linep) < 2)
    8890                 :          0 :         goto invalid_data;
    8891         [ +  + ]:       1710 :       uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
    8892                 :            : 
    8893                 :       3420 :       size_t address_size
    8894         [ +  + ]:       1710 :         = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    8895                 :       1710 :       unsigned char segment_selector_size = 0;
    8896         [ +  + ]:       1710 :       if (version > 4)
    8897                 :            :         {
    8898         [ -  + ]:          2 :           if ((size_t) (lineendp - linep) < 2)
    8899                 :          0 :             goto invalid_data;
    8900                 :          2 :           address_size = *linep++;
    8901                 :          2 :           segment_selector_size = *linep++;
    8902                 :            :         }
    8903                 :            : 
    8904                 :            :       /* Next comes the header length.  */
    8905                 :       1710 :       Dwarf_Word header_length;
    8906         [ +  - ]:       1710 :       if (length == 4)
    8907                 :            :         {
    8908         [ -  + ]:       1710 :           if ((size_t) (lineendp - linep) < 4)
    8909                 :          0 :             goto invalid_data;
    8910         [ +  + ]:       1710 :           header_length = read_4ubyte_unaligned_inc (dbg, linep);
    8911                 :            :         }
    8912                 :            :       else
    8913                 :            :         {
    8914         [ #  # ]:          0 :           if ((size_t) (lineendp - linep) < 8)
    8915                 :          0 :             goto invalid_data;
    8916         [ #  # ]:          0 :           header_length = read_8ubyte_unaligned_inc (dbg, linep);
    8917                 :            :         }
    8918                 :            : 
    8919                 :       1710 :       const unsigned char *header_start = linep;
    8920                 :            : 
    8921                 :            :       /* Next the minimum instruction length.  */
    8922         [ -  + ]:       1710 :       if ((size_t) (lineendp - linep) < 1)
    8923                 :          0 :         goto invalid_data;
    8924                 :       1710 :       uint_fast8_t minimum_instr_len = *linep++;
    8925                 :            : 
    8926                 :            :       /* Next the maximum operations per instruction, in version 4 format.  */
    8927                 :       1710 :       uint_fast8_t max_ops_per_instr;
    8928         [ +  + ]:       1710 :       if (version < 4)
    8929                 :            :         max_ops_per_instr = 1;
    8930                 :            :       else
    8931                 :            :         {
    8932         [ -  + ]:         10 :           if ((size_t) (lineendp - linep) < 1)
    8933                 :          0 :             goto invalid_data;
    8934                 :         10 :           max_ops_per_instr = *linep++;
    8935                 :            :         }
    8936                 :            : 
    8937                 :            :       /* We need at least 4 more bytes.  */
    8938         [ -  + ]:       1710 :       if ((size_t) (lineendp - linep) < 4)
    8939                 :          0 :         goto invalid_data;
    8940                 :            : 
    8941                 :            :       /* Then the flag determining the default value of the is_stmt
    8942                 :            :          register.  */
    8943                 :       1710 :       uint_fast8_t default_is_stmt = *linep++;
    8944                 :            : 
    8945                 :            :       /* Now the line base.  */
    8946                 :       1710 :       int_fast8_t line_base = *linep++;
    8947                 :            : 
    8948                 :            :       /* And the line range.  */
    8949                 :       1710 :       uint_fast8_t line_range = *linep++;
    8950                 :            : 
    8951                 :            :       /* The opcode base.  */
    8952                 :       1710 :       uint_fast8_t opcode_base = *linep++;
    8953                 :            : 
    8954                 :            :       /* Print what we got so far.  */
    8955                 :       1710 :       printf (_("\n"
    8956                 :            :                        " Length:                         %" PRIu64 "\n"
    8957                 :            :                        " DWARF version:                  %" PRIuFAST16 "\n"
    8958                 :            :                        " Prologue length:                %" PRIu64 "\n"
    8959                 :            :                        " Address size:                   %zd\n"
    8960                 :            :                        " Segment selector size:          %zd\n"
    8961                 :            :                        " Min instruction length:         %" PRIuFAST8 "\n"
    8962                 :            :                        " Max operations per instruction: %" PRIuFAST8 "\n"
    8963                 :            :                        " Initial value if 'is_stmt':     %" PRIuFAST8 "\n"
    8964                 :            :                        " Line base:                      %" PRIdFAST8 "\n"
    8965                 :            :                        " Line range:                     %" PRIuFAST8 "\n"
    8966                 :            :                        " Opcode base:                    %" PRIuFAST8 "\n"
    8967                 :            :                        "\n"
    8968                 :            :                        "Opcodes:\n"),
    8969                 :            :               (uint64_t) unit_length, version, (uint64_t) header_length,
    8970                 :            :               address_size, (size_t) segment_selector_size,
    8971                 :            :               minimum_instr_len, max_ops_per_instr,
    8972                 :            :               default_is_stmt, line_base,
    8973                 :            :               line_range, opcode_base);
    8974                 :            : 
    8975         [ -  + ]:       1710 :       if (version < 2 || version > 5)
    8976                 :            :         {
    8977                 :          0 :           error (0, 0, _("cannot handle .debug_line version: %u\n"),
    8978                 :            :                  (unsigned int) version);
    8979                 :          0 :           linep = lineendp;
    8980                 :          0 :           continue;
    8981                 :            :         }
    8982                 :            : 
    8983         [ -  + ]:       1710 :       if (address_size != 4 && address_size != 8)
    8984                 :            :         {
    8985                 :          0 :           error (0, 0, _("cannot handle address size: %u\n"),
    8986                 :            :                  (unsigned int) address_size);
    8987                 :          0 :           linep = lineendp;
    8988                 :          0 :           continue;
    8989                 :            :         }
    8990                 :            : 
    8991         [ -  + ]:       1710 :       if (segment_selector_size != 0)
    8992                 :            :         {
    8993                 :          0 :           error (0, 0, _("cannot handle segment selector size: %u\n"),
    8994                 :            :                  (unsigned int) segment_selector_size);
    8995                 :          0 :           linep = lineendp;
    8996                 :          0 :           continue;
    8997                 :            :         }
    8998                 :            : 
    8999         [ -  + ]:       1710 :       if (unlikely (linep + opcode_base - 1 >= lineendp))
    9000                 :            :         {
    9001                 :          0 :         invalid_unit:
    9002                 :          0 :           error (0, 0,
    9003                 :          0 :                  _("invalid data at offset %tu in section [%zu] '%s'"),
    9004                 :          0 :                  linep - (const unsigned char *) data->d_buf,
    9005                 :            :                  elf_ndxscn (scn), section_name (ebl, shdr));
    9006                 :          0 :           linep = lineendp;
    9007                 :          0 :           continue;
    9008                 :            :         }
    9009                 :       1710 :       int opcode_base_l10 = 1;
    9010                 :       1710 :       unsigned int tmp = opcode_base;
    9011         [ +  + ]:       3417 :       while (tmp > 10)
    9012                 :            :         {
    9013                 :       1707 :           tmp /= 10;
    9014                 :       1707 :           ++opcode_base_l10;
    9015                 :            :         }
    9016                 :       1710 :       const uint8_t *standard_opcode_lengths = linep - 1;
    9017         [ +  + ]:      22221 :       for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
    9018                 :      20511 :         printf (ngettext ("  [%*" PRIuFAST8 "]  %hhu argument\n",
    9019                 :            :                           "  [%*" PRIuFAST8 "]  %hhu arguments\n",
    9020                 :            :                           (int) linep[cnt - 1]),
    9021                 :      20511 :                 opcode_base_l10, cnt, linep[cnt - 1]);
    9022                 :       1710 :       linep += opcode_base - 1;
    9023                 :            : 
    9024         [ -  + ]:       1710 :       if (unlikely (linep >= lineendp))
    9025                 :          0 :         goto invalid_unit;
    9026                 :            : 
    9027                 :       1710 :       Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
    9028                 :            : 
    9029                 :       1710 :       puts (_("\nDirectory table:"));
    9030         [ +  + ]:       1710 :       if (version > 4)
    9031                 :            :         {
    9032                 :          2 :           struct encpair { uint16_t desc; uint16_t form; };
    9033                 :          2 :           struct encpair enc[256];
    9034                 :            : 
    9035                 :          2 :           printf (_("      ["));
    9036         [ -  + ]:          2 :           if ((size_t) (lineendp - linep) < 1)
    9037                 :          0 :             goto invalid_data;
    9038                 :          2 :           unsigned char directory_entry_format_count = *linep++;
    9039                 :          2 :           for (int i = 0; i < directory_entry_format_count; i++)
    9040                 :            :             {
    9041                 :          2 :               uint16_t desc, form;
    9042         [ -  + ]:          2 :               if ((size_t) (lineendp - linep) < 1)
    9043                 :          0 :                 goto invalid_data;
    9044                 :          2 :               get_uleb128 (desc, linep, lineendp);
    9045         [ -  + ]:          2 :               if ((size_t) (lineendp - linep) < 1)
    9046                 :          0 :                 goto invalid_data;
    9047                 :          2 :               get_uleb128 (form, linep, lineendp);
    9048                 :            : 
    9049                 :          2 :               enc[i].desc = desc;
    9050                 :          2 :               enc[i].form = form;
    9051                 :            : 
    9052                 :          2 :               printf ("%s(%s)",
    9053                 :            :                       dwarf_line_content_description_name (desc),
    9054                 :            :                       dwarf_form_name (form));
    9055         [ -  + ]:          2 :               if (i + 1 < directory_entry_format_count)
    9056         [ +  + ]:          4 :                 printf (", ");
    9057                 :            :             }
    9058                 :          2 :           printf ("]\n");
    9059                 :            : 
    9060                 :          2 :           uint64_t directories_count;
    9061         [ -  + ]:          2 :           if ((size_t) (lineendp - linep) < 1)
    9062                 :          0 :             goto invalid_data;
    9063                 :          2 :           get_uleb128 (directories_count, linep, lineendp);
    9064                 :            : 
    9065                 :          2 :           if (directory_entry_format_count == 0
    9066         [ -  + ]:          2 :               && directories_count != 0)
    9067                 :          0 :             goto invalid_data;
    9068                 :            : 
    9069         [ +  + ]:          6 :           for (uint64_t i = 0; i < directories_count; i++)
    9070                 :            :             {
    9071                 :          4 :               printf (" %-5" PRIu64 " ", i);
    9072                 :          4 :               for (int j = 0; j < directory_entry_format_count; j++)
    9073                 :            :                 {
    9074                 :          4 :                   linep = print_form_data (dbg, enc[j].form,
    9075                 :            :                                            linep, lineendp, length,
    9076                 :            :                                            str_offsets_base);
    9077         [ -  + ]:          4 :                   if (j + 1 < directory_entry_format_count)
    9078         [ +  + ]:          8 :                     printf (", ");
    9079                 :            :                 }
    9080                 :          4 :               printf ("\n");
    9081         [ -  + ]:          4 :               if (linep >= lineendp)
    9082                 :          0 :                 goto invalid_unit;
    9083                 :            :             }
    9084                 :            :         }
    9085                 :            :       else
    9086                 :            :         {
    9087   [ +  -  +  + ]:      12441 :           while (linep < lineendp && *linep != 0)
    9088                 :            :             {
    9089                 :      10733 :               unsigned char *endp = memchr (linep, '\0', lineendp - linep);
    9090         [ -  + ]:      10733 :               if (unlikely (endp == NULL))
    9091                 :          0 :                 goto invalid_unit;
    9092                 :            : 
    9093                 :      10733 :               printf (" %s\n", (char *) linep);
    9094                 :            : 
    9095                 :      10733 :               linep = endp + 1;
    9096                 :            :             }
    9097   [ +  -  -  + ]:       1708 :           if (linep >= lineendp || *linep != 0)
    9098                 :          0 :             goto invalid_unit;
    9099                 :            :           /* Skip the final NUL byte.  */
    9100                 :       1708 :           ++linep;
    9101                 :            :         }
    9102                 :            : 
    9103         [ -  + ]:       1710 :       if (unlikely (linep >= lineendp))
    9104                 :          0 :         goto invalid_unit;
    9105                 :            : 
    9106                 :       1710 :       puts (_("\nFile name table:"));
    9107         [ +  + ]:       1710 :       if (version > 4)
    9108                 :            :         {
    9109                 :          2 :           struct encpair { uint16_t desc; uint16_t form; };
    9110                 :          2 :           struct encpair enc[256];
    9111                 :            : 
    9112                 :          2 :           printf (_("      ["));
    9113         [ -  + ]:          2 :           if ((size_t) (lineendp - linep) < 1)
    9114                 :          0 :             goto invalid_data;
    9115                 :          2 :           unsigned char file_name_format_count = *linep++;
    9116                 :          2 :           for (int i = 0; i < file_name_format_count; i++)
    9117                 :            :             {
    9118                 :          4 :               uint64_t desc, form;
    9119         [ -  + ]:          4 :               if ((size_t) (lineendp - linep) < 1)
    9120                 :          0 :                 goto invalid_data;
    9121                 :          4 :               get_uleb128 (desc, linep, lineendp);
    9122         [ -  + ]:          4 :               if ((size_t) (lineendp - linep) < 1)
    9123                 :          0 :                 goto invalid_data;
    9124                 :          4 :               get_uleb128 (form, linep, lineendp);
    9125                 :            : 
    9126         [ -  + ]:          4 :               if (! libdw_valid_user_form (form))
    9127                 :          0 :                 goto invalid_data;
    9128                 :            : 
    9129                 :          4 :               enc[i].desc = desc;
    9130                 :          4 :               enc[i].form = form;
    9131                 :            : 
    9132                 :          4 :               printf ("%s(%s)",
    9133                 :            :                       dwarf_line_content_description_name (desc),
    9134                 :            :                       dwarf_form_name (form));
    9135         [ +  + ]:          4 :               if (i + 1 < file_name_format_count)
    9136         [ +  + ]:          8 :                 printf (", ");
    9137                 :            :             }
    9138                 :          2 :           printf ("]\n");
    9139                 :            : 
    9140                 :          2 :           uint64_t file_name_count;
    9141         [ -  + ]:          2 :           if ((size_t) (lineendp - linep) < 1)
    9142                 :          0 :             goto invalid_data;
    9143                 :          2 :           get_uleb128 (file_name_count, linep, lineendp);
    9144                 :            : 
    9145                 :          2 :           if (file_name_format_count == 0
    9146         [ -  + ]:          2 :               && file_name_count != 0)
    9147                 :          0 :             goto invalid_data;
    9148                 :            : 
    9149         [ +  + ]:         10 :           for (uint64_t i = 0; i < file_name_count; i++)
    9150                 :            :             {
    9151                 :          8 :               printf (" %-5" PRIu64 " ", i);
    9152                 :          8 :               for (int j = 0; j < file_name_format_count; j++)
    9153                 :            :                 {
    9154                 :         16 :                   linep = print_form_data (dbg, enc[j].form,
    9155                 :            :                                            linep, lineendp, length,
    9156                 :            :                                            str_offsets_base);
    9157         [ +  + ]:         16 :                   if (j + 1 < file_name_format_count)
    9158         [ +  + ]:         32 :                     printf (", ");
    9159                 :            :                 }
    9160                 :          8 :               printf ("\n");
    9161         [ -  + ]:          8 :               if (linep > lineendp)
    9162                 :          0 :                 goto invalid_unit;
    9163                 :            :             }
    9164                 :            :         }
    9165                 :            :       else
    9166                 :            :         {
    9167                 :       1708 :           puts (_(" Entry Dir   Time      Size      Name"));
    9168   [ +  -  +  + ]:      28681 :           for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
    9169                 :            :             {
    9170                 :            :               /* First comes the file name.  */
    9171                 :      26973 :               char *fname = (char *) linep;
    9172                 :      26973 :               unsigned char *endp = memchr (fname, '\0', lineendp - linep);
    9173         [ -  + ]:      26973 :               if (unlikely (endp == NULL))
    9174                 :          0 :                 goto invalid_unit;
    9175                 :      26973 :               linep = endp + 1;
    9176                 :            : 
    9177                 :            :               /* Then the index.  */
    9178                 :      26973 :               unsigned int diridx;
    9179         [ -  + ]:      26973 :               if (lineendp - linep < 1)
    9180                 :          0 :                 goto invalid_unit;
    9181                 :      26973 :               get_uleb128 (diridx, linep, lineendp);
    9182                 :            : 
    9183                 :            :               /* Next comes the modification time.  */
    9184                 :      26973 :               unsigned int mtime;
    9185         [ -  + ]:      26973 :               if (lineendp - linep < 1)
    9186                 :          0 :                 goto invalid_unit;
    9187                 :      26973 :               get_uleb128 (mtime, linep, lineendp);
    9188                 :            : 
    9189                 :            :               /* Finally the length of the file.  */
    9190                 :      26973 :               unsigned int fsize;
    9191         [ -  + ]:      26973 :               if (lineendp - linep < 1)
    9192                 :          0 :                 goto invalid_unit;
    9193                 :      26973 :               get_uleb128 (fsize, linep, lineendp);
    9194                 :            : 
    9195                 :      26973 :               printf (" %-5u %-5u %-9u %-9u %s\n",
    9196                 :            :                       cnt, diridx, mtime, fsize, fname);
    9197                 :            :             }
    9198   [ +  -  -  + ]:       1708 :           if (linep >= lineendp || *linep != '\0')
    9199                 :          0 :             goto invalid_unit;
    9200                 :            :           /* Skip the final NUL byte.  */
    9201                 :       1708 :           ++linep;
    9202                 :            :         }
    9203                 :            : 
    9204                 :       1710 :       unsigned int debug_str_offset = 0;
    9205         [ +  + ]:       1710 :       if (unlikely (linep == header_start + header_length - 4))
    9206                 :            :         {
    9207                 :            :           /* CUBINs contain an unsigned 4-byte offset */
    9208         [ -  + ]:          1 :           debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
    9209                 :            :         }
    9210                 :            : 
    9211         [ +  + ]:       1710 :       if (linep == lineendp)
    9212                 :            :         {
    9213                 :         18 :           puts (_("\nNo line number statements."));
    9214                 :         18 :           continue;
    9215                 :            :         }
    9216                 :            : 
    9217                 :       1692 :       puts (_("\nLine number statements:"));
    9218                 :       1692 :       Dwarf_Word address = 0;
    9219                 :       1692 :       unsigned int op_index = 0;
    9220                 :       1692 :       size_t line = 1;
    9221                 :       1692 :       uint_fast8_t is_stmt = default_is_stmt;
    9222                 :            : 
    9223                 :            :       /* Apply the "operation advance" from a special opcode
    9224                 :            :          or DW_LNS_advance_pc (as per DWARF4 6.2.5.1).  */
    9225                 :       1692 :       unsigned int op_addr_advance;
    9226                 :            : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
    9227                 :            :                       max_ops_per_instr, &op_addr_advance, &address, &op_index)
    9228                 :            : 
    9229         [ -  + ]:       1692 :       if (max_ops_per_instr == 0)
    9230                 :            :         {
    9231                 :          0 :           error (0, 0,
    9232                 :          0 :                  _("invalid maximum operations per instruction is zero"));
    9233                 :          0 :           linep = lineendp;
    9234                 :          0 :           continue;
    9235                 :            :         }
    9236                 :            : 
    9237                 :    1145758 :       while (linep < lineendp)
    9238                 :            :         {
    9239                 :    1144066 :           size_t offset = linep - (const unsigned char *) data->d_buf;
    9240                 :    1144066 :           unsigned int u128;
    9241                 :    1144066 :           int s128;
    9242                 :            : 
    9243                 :            :           /* Read the opcode.  */
    9244                 :    1144066 :           unsigned int opcode = *linep++;
    9245                 :            : 
    9246                 :    1144066 :           printf (" [%6" PRIx64 "]", (uint64_t)offset);
    9247                 :            :           /* Is this a special opcode?  */
    9248         [ +  + ]:    1144066 :           if (likely (opcode >= opcode_base))
    9249                 :            :             {
    9250         [ -  + ]:     332759 :               if (unlikely (line_range == 0))
    9251                 :          0 :                 goto invalid_unit;
    9252                 :            : 
    9253                 :            :               /* Yes.  Handling this is quite easy since the opcode value
    9254                 :            :                  is computed with
    9255                 :            : 
    9256                 :            :                  opcode = (desired line increment - line_base)
    9257                 :            :                            + (line_range * address advance) + opcode_base
    9258                 :            :               */
    9259                 :     332759 :               int line_increment = (line_base
    9260                 :     332759 :                                     + (opcode - opcode_base) % line_range);
    9261                 :            : 
    9262                 :            :               /* Perform the increments.  */
    9263                 :     332759 :               line += line_increment;
    9264                 :     332759 :               advance_pc ((opcode - opcode_base) / line_range);
    9265                 :            : 
    9266                 :     332759 :               printf (_(" special opcode %u: address+%u = "),
    9267                 :            :                       opcode, op_addr_advance);
    9268                 :     332759 :               print_dwarf_addr (dwflmod, 0, address, address);
    9269         [ -  + ]:     332759 :               if (op_index > 0)
    9270                 :          0 :                 printf (_(", op_index = %u, line%+d = %zu\n"),
    9271                 :            :                         op_index, line_increment, line);
    9272                 :            :               else
    9273                 :     332759 :                 printf (_(", line%+d = %zu\n"),
    9274                 :            :                         line_increment, line);
    9275                 :            :             }
    9276         [ +  + ]:     811307 :           else if (opcode == 0)
    9277                 :            :             {
    9278                 :            :               /* This an extended opcode.  */
    9279         [ -  + ]:      56392 :               if (unlikely (linep + 2 > lineendp))
    9280                 :          0 :                 goto invalid_unit;
    9281                 :            : 
    9282                 :            :               /* The length.  */
    9283                 :      56392 :               unsigned int len = *linep++;
    9284                 :            : 
    9285         [ -  + ]:      56392 :               if (unlikely (linep + len > lineendp))
    9286                 :          0 :                 goto invalid_unit;
    9287                 :            : 
    9288                 :            :               /* The sub-opcode.  */
    9289                 :      56392 :               opcode = *linep++;
    9290                 :            : 
    9291                 :      56392 :               printf (_(" extended opcode %u: "), opcode);
    9292                 :            : 
    9293   [ +  +  -  +  :      56392 :               switch (opcode)
                +  -  - ]
    9294                 :            :                 {
    9295                 :       1753 :                 case DW_LNE_end_sequence:
    9296                 :       1753 :                   puts (_(" end of sequence"));
    9297                 :            : 
    9298                 :            :                   /* Reset the registers we care about.  */
    9299                 :       1753 :                   address = 0;
    9300                 :       1753 :                   op_index = 0;
    9301                 :       1753 :                   line = 1;
    9302                 :       1753 :                   is_stmt = default_is_stmt;
    9303                 :       1753 :                   break;
    9304                 :            : 
    9305                 :       2328 :                 case DW_LNE_set_address:
    9306                 :       2328 :                   op_index = 0;
    9307         [ -  + ]:       2328 :                   if (unlikely ((size_t) (lineendp - linep) < address_size))
    9308                 :          0 :                     goto invalid_unit;
    9309         [ +  + ]:       2328 :                   if (address_size == 4)
    9310         [ +  + ]:         21 :                     address = read_4ubyte_unaligned_inc (dbg, linep);
    9311                 :            :                   else
    9312         [ +  + ]:       2307 :                     address = read_8ubyte_unaligned_inc (dbg, linep);
    9313                 :            :                   {
    9314                 :       2328 :                     printf (_(" set address to "));
    9315                 :       2328 :                     print_dwarf_addr (dwflmod, 0, address, address);
    9316                 :       2328 :                     printf ("\n");
    9317                 :            :                   }
    9318                 :            :                   break;
    9319                 :            : 
    9320                 :          0 :                 case DW_LNE_define_file:
    9321                 :            :                   {
    9322                 :          0 :                     char *fname = (char *) linep;
    9323                 :          0 :                     unsigned char *endp = memchr (linep, '\0',
    9324                 :          0 :                                                   lineendp - linep);
    9325         [ #  # ]:          0 :                     if (unlikely (endp == NULL))
    9326                 :          0 :                       goto invalid_unit;
    9327                 :          0 :                     linep = endp + 1;
    9328                 :            : 
    9329                 :          0 :                     unsigned int diridx;
    9330         [ #  # ]:          0 :                     if (lineendp - linep < 1)
    9331                 :          0 :                       goto invalid_unit;
    9332                 :          0 :                     get_uleb128 (diridx, linep, lineendp);
    9333                 :          0 :                     Dwarf_Word mtime;
    9334         [ #  # ]:          0 :                     if (lineendp - linep < 1)
    9335                 :          0 :                       goto invalid_unit;
    9336                 :          0 :                     get_uleb128 (mtime, linep, lineendp);
    9337                 :          0 :                     Dwarf_Word filelength;
    9338         [ #  # ]:          0 :                     if (lineendp - linep < 1)
    9339                 :          0 :                       goto invalid_unit;
    9340                 :          0 :                     get_uleb128 (filelength, linep, lineendp);
    9341                 :            : 
    9342                 :          0 :                     printf (_("\
    9343                 :            :  define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
    9344                 :            :                             diridx, (uint64_t) mtime, (uint64_t) filelength,
    9345                 :            :                             fname);
    9346                 :            :                   }
    9347                 :            :                   break;
    9348                 :            : 
    9349                 :      52304 :                 case DW_LNE_set_discriminator:
    9350                 :            :                   /* Takes one ULEB128 parameter, the discriminator.  */
    9351   [ +  -  -  + ]:      52304 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    9352                 :            :                                 || lineendp - linep < 1))
    9353                 :          0 :                     goto invalid_unit;
    9354                 :            : 
    9355                 :      52304 :                   get_uleb128 (u128, linep, lineendp);
    9356                 :      52304 :                   printf (_(" set discriminator to %u\n"), u128);
    9357                 :            :                   break;
    9358                 :            : 
    9359                 :          7 :                 case DW_LNE_NVIDIA_inlined_call:
    9360                 :            :                   {
    9361         [ -  + ]:          7 :                     if (unlikely (linep >= lineendp))
    9362                 :          0 :                       goto invalid_data;
    9363                 :            : 
    9364                 :          7 :                     unsigned int context;
    9365                 :          7 :                     get_uleb128 (context, linep, lineendp);
    9366                 :            : 
    9367         [ -  + ]:          7 :                     if (unlikely (linep >= lineendp))
    9368                 :          0 :                       goto invalid_data;
    9369                 :            : 
    9370                 :          7 :                     unsigned int function_name;
    9371                 :          7 :                     get_uleb128 (function_name, linep, lineendp);
    9372                 :          7 :                     function_name += debug_str_offset;
    9373                 :            : 
    9374                 :          7 :                     Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
    9375                 :          7 :                     char *function_str;
    9376   [ +  -  +  - ]:          7 :                     if (str_data == NULL || function_name >= str_data->d_size
    9377         [ +  - ]:          7 :                         || memchr (str_data->d_buf + function_name, '\0',
    9378                 :            :                                    str_data->d_size - function_name) == NULL)
    9379                 :            :                       function_str = "???";
    9380                 :            :                     else
    9381                 :          7 :                       function_str = (char *) str_data->d_buf + function_name;
    9382                 :            : 
    9383                 :          7 :                     printf (_(" set inlined context %u,"
    9384                 :            :                               " function name %s (0x%x)\n"),
    9385                 :            :                             context, function_str, function_name);
    9386                 :            :                     break;
    9387                 :            :                   }
    9388                 :            : 
    9389                 :          0 :                 case DW_LNE_NVIDIA_set_function_name:
    9390                 :            :                   {
    9391         [ #  # ]:          0 :                     if (unlikely (linep >= lineendp))
    9392                 :          0 :                       goto invalid_data;
    9393                 :            : 
    9394                 :          0 :                     unsigned int function_name;
    9395                 :          0 :                     get_uleb128 (function_name, linep, lineendp);
    9396                 :          0 :                     function_name += debug_str_offset;
    9397                 :            : 
    9398                 :          0 :                     Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
    9399                 :          0 :                     char *function_str;
    9400   [ #  #  #  # ]:          0 :                     if (str_data == NULL || function_name >= str_data->d_size
    9401         [ #  # ]:          0 :                         || memchr (str_data->d_buf + function_name, '\0',
    9402                 :            :                                    str_data->d_size - function_name) == NULL)
    9403                 :            :                       function_str = "???";
    9404                 :            :                     else
    9405                 :          0 :                       function_str = (char *) str_data->d_buf + function_name;
    9406                 :            : 
    9407                 :          0 :                     printf (_(" set function name %s (0x%x)\n"),
    9408                 :            :                             function_str, function_name);
    9409                 :            :                   }
    9410                 :            :                   break;
    9411                 :            : 
    9412                 :          0 :                 default:
    9413                 :            :                   /* Unknown, ignore it.  */
    9414                 :          0 :                   puts (_(" unknown opcode"));
    9415                 :          0 :                   linep += len - 1;
    9416                 :          0 :                   break;
    9417                 :            :                 }
    9418                 :            :             }
    9419         [ +  - ]:     754915 :           else if (opcode <= DW_LNS_set_isa)
    9420                 :            :             {
    9421                 :            :               /* This is a known standard opcode.  */
    9422   [ +  +  +  +  :     754915 :               switch (opcode)
          +  +  -  +  +  
                +  -  - ]
    9423                 :            :                 {
    9424                 :     114389 :                 case DW_LNS_copy:
    9425                 :            :                   /* Takes no argument.  */
    9426                 :     114389 :                   puts (_(" copy"));
    9427                 :     114389 :                   break;
    9428                 :            : 
    9429                 :       4401 :                 case DW_LNS_advance_pc:
    9430                 :            :                   /* Takes one uleb128 parameter which is added to the
    9431                 :            :                      address.  */
    9432         [ -  + ]:       4401 :                   if (lineendp - linep < 1)
    9433                 :          0 :                     goto invalid_unit;
    9434                 :       4401 :                   get_uleb128 (u128, linep, lineendp);
    9435                 :       4401 :                   advance_pc (u128);
    9436                 :            :                   {
    9437                 :       4401 :                     printf (_(" advance address by %u to "),
    9438                 :            :                             op_addr_advance);
    9439                 :       4401 :                     print_dwarf_addr (dwflmod, 0, address, address);
    9440         [ -  + ]:       4401 :                     if (op_index > 0)
    9441                 :          0 :                       printf (_(", op_index to %u"), op_index);
    9442                 :       4401 :                     printf ("\n");
    9443                 :            :                   }
    9444                 :            :                   break;
    9445                 :            : 
    9446                 :      85153 :                 case DW_LNS_advance_line:
    9447                 :            :                   /* Takes one sleb128 parameter which is added to the
    9448                 :            :                      line.  */
    9449         [ -  + ]:      85153 :                   if (lineendp - linep < 1)
    9450                 :          0 :                     goto invalid_unit;
    9451                 :      85153 :                   get_sleb128 (s128, linep, lineendp);
    9452                 :      85153 :                   line += s128;
    9453                 :      85153 :                   printf (_("\
    9454                 :            :  advance line by constant %d to %" PRId64 "\n"),
    9455                 :            :                           s128, (int64_t) line);
    9456                 :            :                   break;
    9457                 :            : 
    9458                 :      31682 :                 case DW_LNS_set_file:
    9459                 :            :                   /* Takes one uleb128 parameter which is stored in file.  */
    9460         [ -  + ]:      31682 :                   if (lineendp - linep < 1)
    9461                 :          0 :                     goto invalid_unit;
    9462                 :      31682 :                   get_uleb128 (u128, linep, lineendp);
    9463                 :      31682 :                   printf (_(" set file to %" PRIu64 "\n"),
    9464                 :            :                           (uint64_t) u128);
    9465                 :            :                   break;
    9466                 :            : 
    9467                 :     291834 :                 case DW_LNS_set_column:
    9468                 :            :                   /* Takes one uleb128 parameter which is stored in column.  */
    9469   [ +  -  -  + ]:     291834 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    9470                 :            :                                 || lineendp - linep < 1))
    9471                 :          0 :                     goto invalid_unit;
    9472                 :            : 
    9473                 :     291834 :                   get_uleb128 (u128, linep, lineendp);
    9474                 :     291834 :                   printf (_(" set column to %" PRIu64 "\n"),
    9475                 :            :                           (uint64_t) u128);
    9476                 :            :                   break;
    9477                 :            : 
    9478                 :     202898 :                 case DW_LNS_negate_stmt:
    9479                 :            :                   /* Takes no argument.  */
    9480                 :     202898 :                   is_stmt = 1 - is_stmt;
    9481                 :     202898 :                   printf (_(" set '%s' to %" PRIuFAST8 "\n"),
    9482                 :            :                           "is_stmt", is_stmt);
    9483                 :            :                   break;
    9484                 :            : 
    9485                 :          0 :                 case DW_LNS_set_basic_block:
    9486                 :            :                   /* Takes no argument.  */
    9487                 :          0 :                   puts (_(" set basic block flag"));
    9488                 :          0 :                   break;
    9489                 :            : 
    9490                 :      24535 :                 case DW_LNS_const_add_pc:
    9491                 :            :                   /* Takes no argument.  */
    9492                 :            : 
    9493         [ -  + ]:      24535 :                   if (unlikely (line_range == 0))
    9494                 :          0 :                     goto invalid_unit;
    9495                 :            : 
    9496                 :      24535 :                   advance_pc ((255 - opcode_base) / line_range);
    9497                 :            :                   {
    9498                 :      24535 :                     printf (_(" advance address by constant %u to "),
    9499                 :            :                             op_addr_advance);
    9500                 :      24535 :                     print_dwarf_addr (dwflmod, 0, address, address);
    9501         [ -  + ]:      24535 :                     if (op_index > 0)
    9502                 :          0 :                       printf (_(", op_index to %u"), op_index);
    9503                 :      24535 :                     printf ("\n");
    9504                 :            :                   }
    9505                 :            :                   break;
    9506                 :            : 
    9507                 :         22 :                 case DW_LNS_fixed_advance_pc:
    9508                 :            :                   /* Takes one 16 bit parameter which is added to the
    9509                 :            :                      address.  */
    9510   [ +  -  -  + ]:         22 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    9511                 :            :                                 || lineendp - linep < 2))
    9512                 :          0 :                     goto invalid_unit;
    9513                 :            : 
    9514         [ -  + ]:         22 :                   u128 = read_2ubyte_unaligned_inc (dbg, linep);
    9515                 :         22 :                   address += u128;
    9516                 :         22 :                   op_index = 0;
    9517                 :            :                   {
    9518                 :         22 :                     printf (_("\
    9519                 :            :  advance address by fixed value %u to \n"),
    9520                 :            :                             u128);
    9521                 :         22 :                     print_dwarf_addr (dwflmod, 0, address, address);
    9522                 :         22 :                     printf ("\n");
    9523                 :            :                   }
    9524                 :            :                   break;
    9525                 :            : 
    9526                 :          1 :                 case DW_LNS_set_prologue_end:
    9527                 :            :                   /* Takes no argument.  */
    9528                 :          1 :                   puts (_(" set prologue end flag"));
    9529                 :          1 :                   break;
    9530                 :            : 
    9531                 :          0 :                 case DW_LNS_set_epilogue_begin:
    9532                 :            :                   /* Takes no argument.  */
    9533                 :          0 :                   puts (_(" set epilogue begin flag"));
    9534                 :          0 :                   break;
    9535                 :            : 
    9536                 :          0 :                 case DW_LNS_set_isa:
    9537                 :            :                   /* Takes one uleb128 parameter which is stored in isa.  */
    9538   [ #  #  #  # ]:          0 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    9539                 :            :                                 || lineendp - linep < 1))
    9540                 :          0 :                     goto invalid_unit;
    9541                 :            : 
    9542                 :          0 :                   get_uleb128 (u128, linep, lineendp);
    9543         [ +  + ]:    1145758 :                   printf (_(" set isa to %u\n"), u128);
    9544                 :            :                   break;
    9545                 :            :                 }
    9546                 :            :             }
    9547                 :            :           else
    9548                 :            :             {
    9549                 :            :               /* This is a new opcode the generator but not we know about.
    9550                 :            :                  Read the parameters associated with it but then discard
    9551                 :            :                  everything.  Read all the parameters for this opcode.  */
    9552                 :          0 :               printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
    9553                 :            :                                 " unknown opcode with %" PRIu8 " parameters:",
    9554                 :            :                                 standard_opcode_lengths[opcode]),
    9555                 :          0 :                       standard_opcode_lengths[opcode]);
    9556                 :          0 :               for (int n = standard_opcode_lengths[opcode];
    9557   [ #  #  #  # ]:          0 :                    n > 0 && linep < lineendp; --n)
    9558                 :            :                 {
    9559                 :          0 :                   get_uleb128 (u128, linep, lineendp);
    9560         [ #  # ]:          0 :                   if (n != standard_opcode_lengths[opcode])
    9561         [ #  # ]:          0 :                     putc_unlocked (',', stdout);
    9562                 :          0 :                   printf (" %u", u128);
    9563                 :            :                 }
    9564                 :            : 
    9565                 :            :               /* Next round, ignore this opcode.  */
    9566                 :          0 :               continue;
    9567                 :            :             }
    9568                 :            :         }
    9569                 :            :     }
    9570                 :            : 
    9571                 :            :   /* There must only be one data block.  */
    9572         [ -  + ]:         50 :   assert (elf_getdata (scn, data) == NULL);
    9573                 :            : }
    9574                 :            : 
    9575                 :            : 
    9576                 :            : static void
    9577                 :          4 : print_debug_loclists_section (Dwfl_Module *dwflmod,
    9578                 :            :                               Ebl *ebl,
    9579                 :            :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
    9580                 :            :                               Elf_Scn *scn, GElf_Shdr *shdr,
    9581                 :            :                               Dwarf *dbg)
    9582                 :            : {
    9583                 :          4 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
    9584         [ +  - ]:          4 :   if (data == NULL)
    9585                 :          0 :     return;
    9586                 :            : 
    9587                 :          4 :   printf (_("\
    9588                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    9589                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
    9590                 :          4 :           (uint64_t) shdr->sh_offset);
    9591                 :            : 
    9592                 :            :   /* For the listptr to get the base address/CU.  */
    9593                 :          4 :   sort_listptr (&known_loclistsptr, "loclistsptr");
    9594                 :          4 :   size_t listptr_idx = 0;
    9595                 :            : 
    9596                 :          4 :   const unsigned char *readp = data->d_buf;
    9597                 :          4 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    9598                 :          4 :                                         + data->d_size);
    9599         [ +  + ]:          9 :   while (readp < dataend)
    9600                 :            :     {
    9601         [ -  + ]:          5 :       if (unlikely (readp > dataend - 4))
    9602                 :            :         {
    9603                 :          0 :         invalid_data:
    9604                 :          0 :           error (0, 0, _("invalid data in section [%zu] '%s'"),
    9605                 :            :                  elf_ndxscn (scn), section_name (ebl, shdr));
    9606                 :          0 :           return;
    9607                 :            :         }
    9608                 :            : 
    9609                 :          5 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    9610                 :          5 :       printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
    9611                 :            :               (uint64_t) offset);
    9612                 :            : 
    9613         [ -  + ]:          5 :       uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    9614                 :          5 :       unsigned int offset_size = 4;
    9615         [ -  + ]:          5 :       if (unlikely (unit_length == 0xffffffff))
    9616                 :            :         {
    9617         [ #  # ]:          0 :           if (unlikely (readp > dataend - 8))
    9618                 :          0 :             goto invalid_data;
    9619                 :            : 
    9620         [ #  # ]:          0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    9621                 :          0 :           offset_size = 8;
    9622                 :            :         }
    9623                 :          5 :       printf (_(" Length:         %8" PRIu64 "\n"), unit_length);
    9624                 :            : 
    9625                 :            :       /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
    9626                 :            :          bytes to complete the header.  And this unit cannot go beyond
    9627                 :            :          the section data.  */
    9628         [ +  - ]:          5 :       if (readp > dataend - 8
    9629         [ +  - ]:          5 :           || unit_length < 8
    9630         [ -  + ]:          5 :           || unit_length > (uint64_t) (dataend - readp))
    9631                 :          0 :         goto invalid_data;
    9632                 :            : 
    9633                 :          5 :       const unsigned char *nexthdr = readp + unit_length;
    9634                 :            : 
    9635         [ -  + ]:          5 :       uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    9636                 :          5 :       printf (_(" DWARF version:  %8" PRIu16 "\n"), version);
    9637                 :            : 
    9638         [ -  + ]:          5 :       if (version != 5)
    9639                 :            :         {
    9640                 :          0 :           error (0, 0, _("Unknown version"));
    9641                 :          0 :           goto next_table;
    9642                 :            :         }
    9643                 :            : 
    9644                 :          5 :       uint8_t address_size = *readp++;
    9645                 :          5 :       printf (_(" Address size:   %8" PRIu64 "\n"),
    9646                 :            :               (uint64_t) address_size);
    9647                 :            : 
    9648         [ -  + ]:          5 :       if (address_size != 4 && address_size != 8)
    9649                 :            :         {
    9650                 :          0 :           error (0, 0, _("unsupported address size"));
    9651                 :          0 :           goto next_table;
    9652                 :            :         }
    9653                 :            : 
    9654                 :          5 :       uint8_t segment_size = *readp++;
    9655                 :          5 :       printf (_(" Segment size:   %8" PRIu64 "\n"),
    9656                 :            :               (uint64_t) segment_size);
    9657                 :            : 
    9658         [ -  + ]:          5 :       if (segment_size != 0)
    9659                 :            :         {
    9660                 :          0 :           error (0, 0, _("unsupported segment size"));
    9661                 :          0 :           goto next_table;
    9662                 :            :         }
    9663                 :            : 
    9664         [ -  + ]:          5 :       uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
    9665                 :          5 :       printf (_(" Offset entries: %8" PRIu64 "\n"),
    9666                 :            :               (uint64_t) offset_entry_count);
    9667                 :            : 
    9668                 :            :       /* We need the CU that uses this unit to get the initial base address. */
    9669                 :          5 :       Dwarf_Addr cu_base = 0;
    9670                 :          5 :       struct Dwarf_CU *cu = NULL;
    9671         [ -  + ]:          5 :       if (listptr_cu (&known_loclistsptr, &listptr_idx,
    9672                 :            :                       (Dwarf_Off) offset,
    9673                 :          5 :                       (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
    9674                 :            :                       &cu_base, &cu)
    9675         [ #  # ]:          0 :           || split_dwarf_cu_base (dbg, &cu, &cu_base))
    9676                 :          5 :         {
    9677                 :          5 :           Dwarf_Die cudie;
    9678         [ -  + ]:          5 :           if (dwarf_cu_die (cu, &cudie,
    9679                 :            :                             NULL, NULL, NULL, NULL,
    9680                 :            :                             NULL, NULL) == NULL)
    9681                 :          0 :             printf (_(" Unknown CU base: "));
    9682                 :            :           else
    9683                 :          5 :             printf (_(" CU [%6" PRIx64 "] base: "),
    9684                 :            :                     dwarf_dieoffset (&cudie));
    9685                 :          5 :           print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
    9686                 :          5 :           printf ("\n");
    9687                 :            :         }
    9688                 :            :       else
    9689                 :          0 :         printf (_(" Not associated with a CU.\n"));
    9690                 :            : 
    9691                 :          5 :       printf ("\n");
    9692                 :            : 
    9693                 :          5 :       const unsigned char *offset_array_start = readp;
    9694         [ +  + ]:          5 :       if (offset_entry_count > 0)
    9695                 :            :         {
    9696                 :          2 :           uint64_t max_entries = (unit_length - 8) / offset_size;
    9697         [ -  + ]:          2 :           if (offset_entry_count > max_entries)
    9698                 :            :             {
    9699                 :          0 :               error (0, 0,
    9700                 :          0 :                      _("too many offset entries for unit length"));
    9701                 :          0 :               offset_entry_count = max_entries;
    9702                 :            :             }
    9703                 :            : 
    9704                 :          2 :           printf (_("  Offsets starting at 0x%" PRIx64 ":\n"),
    9705                 :            :                   (uint64_t) (offset_array_start
    9706                 :          2 :                               - (unsigned char *) data->d_buf));
    9707         [ +  + ]:         18 :           for (uint32_t idx = 0; idx < offset_entry_count; idx++)
    9708                 :            :             {
    9709                 :         16 :               printf ("   [%6" PRIu32 "] ", idx);
    9710         [ +  - ]:         16 :               if (offset_size == 4)
    9711                 :            :                 {
    9712         [ -  + ]:         16 :                   uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
    9713                 :         16 :                   printf ("0x%" PRIx32 "\n", off);
    9714                 :            :                 }
    9715                 :            :               else
    9716                 :            :                 {
    9717         [ #  # ]:          0 :                   uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
    9718                 :         16 :                   printf ("0x%" PRIx64 "\n", off);
    9719                 :            :                 }
    9720                 :            :             }
    9721                 :          2 :           printf ("\n");
    9722                 :            :         }
    9723                 :            : 
    9724                 :          5 :       Dwarf_Addr base = cu_base;
    9725                 :          5 :       bool start_of_list = true;
    9726                 :          5 :       while (readp < nexthdr)
    9727                 :            :         {
    9728                 :         92 :           Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
    9729         [ +  + ]:         92 :           if (listptr_attr (&known_loclistsptr, listptr_idx, off,
    9730                 :            :                             DW_AT_GNU_locviews))
    9731                 :            :             {
    9732                 :          1 :               Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
    9733                 :            :                                                         &listptr_idx, off);
    9734                 :          1 :               const unsigned char *locp = readp;
    9735                 :          1 :               const unsigned char *locendp;
    9736         [ +  - ]:          1 :               if (next_off == 0
    9737                 :          1 :                   || next_off > (size_t) (nexthdr - ((const unsigned char *)
    9738         [ +  - ]:          1 :                                                      data->d_buf)))
    9739                 :            :                 locendp = nexthdr;
    9740                 :            :               else
    9741                 :          1 :                 locendp = (const unsigned char *) data->d_buf + next_off;
    9742                 :            : 
    9743                 :          3 :               printf ("  Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
    9744                 :          1 :                       (uint64_t) (readp - (unsigned char *) data->d_buf),
    9745                 :          1 :                       (uint64_t) (readp - offset_array_start));
    9746                 :            : 
    9747                 :          2 :               while (locp < locendp)
    9748                 :            :                 {
    9749                 :          1 :                   uint64_t v1, v2;
    9750                 :          1 :                   get_uleb128 (v1, locp, locendp);
    9751         [ -  + ]:          1 :                   if (locp >= locendp)
    9752                 :            :                     {
    9753                 :          0 :                       printf (_("    <INVALID DATA>\n"));
    9754                 :            :                       break;
    9755                 :            :                     }
    9756                 :          1 :                   get_uleb128 (v2, locp, locendp);
    9757         [ +  + ]:          2 :                   printf ("    view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
    9758                 :            :                 }
    9759                 :            : 
    9760                 :          1 :               printf ("\n");
    9761                 :          1 :               readp = (unsigned char *) locendp;
    9762                 :          1 :               continue;
    9763                 :            :             }
    9764                 :            : 
    9765                 :         91 :           uint8_t kind = *readp++;
    9766                 :         91 :           uint64_t op1, op2, len;
    9767                 :            : 
    9768                 :            :           /* Skip padding.  */
    9769         [ -  + ]:         91 :           if (start_of_list && kind == DW_LLE_end_of_list)
    9770                 :          0 :             continue;
    9771                 :            : 
    9772         [ +  + ]:         91 :           if (start_of_list)
    9773                 :            :             {
    9774                 :         33 :               base = cu_base;
    9775                 :         33 :               printf ("  Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
    9776                 :         33 :                       (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
    9777                 :         33 :                       (uint64_t) (readp - offset_array_start - 1));
    9778                 :         33 :               start_of_list = false;
    9779                 :            :             }
    9780                 :            : 
    9781                 :         91 :           printf ("    %s", dwarf_loc_list_encoding_name (kind));
    9782   [ +  -  -  +  :         91 :           switch (kind)
          +  -  +  -  +  
                   -  - ]
    9783                 :            :             {
    9784                 :         33 :             case DW_LLE_end_of_list:
    9785                 :         33 :               start_of_list = true;
    9786                 :         33 :               printf ("\n\n");
    9787                 :            :               break;
    9788                 :            : 
    9789                 :          0 :             case DW_LLE_base_addressx:
    9790         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9791                 :            :                 {
    9792                 :          0 :                 invalid_entry:
    9793                 :          0 :                   error (0, 0, _("invalid loclists data"));
    9794                 :          0 :                   goto next_table;
    9795                 :            :                 }
    9796                 :          0 :               get_uleb128 (op1, readp, nexthdr);
    9797                 :          0 :               printf (" %" PRIx64 "\n", op1);
    9798         [ #  # ]:          0 :               if (! print_unresolved_addresses)
    9799                 :            :                 {
    9800                 :          0 :                   Dwarf_Addr addr;
    9801         [ #  # ]:          0 :                   if (get_indexed_addr (cu, op1, &addr) != 0)
    9802                 :          0 :                     printf ("      ???\n");
    9803                 :            :                   else
    9804                 :            :                     {
    9805                 :          0 :                       printf ("      ");
    9806                 :          0 :                       print_dwarf_addr (dwflmod, address_size, addr, addr);
    9807                 :          0 :                       printf ("\n");
    9808                 :            :                     }
    9809                 :            :                 }
    9810                 :            :               break;
    9811                 :            : 
    9812                 :          0 :             case DW_LLE_startx_endx:
    9813         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9814                 :          0 :                 goto invalid_entry;
    9815                 :          0 :               get_uleb128 (op1, readp, nexthdr);
    9816         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9817                 :          0 :                 goto invalid_entry;
    9818                 :          0 :               get_uleb128 (op2, readp, nexthdr);
    9819                 :          0 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    9820         [ #  # ]:          0 :               if (! print_unresolved_addresses)
    9821                 :            :                 {
    9822                 :          0 :                   Dwarf_Addr addr1;
    9823                 :          0 :                   Dwarf_Addr addr2;
    9824         [ #  # ]:          0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0
    9825         [ #  # ]:          0 :                       || get_indexed_addr (cu, op2, &addr2) != 0)
    9826                 :            :                     {
    9827                 :          0 :                       printf ("      ???..\n");
    9828                 :          0 :                       printf ("      ???\n");
    9829                 :            :                     }
    9830                 :            :                   else
    9831                 :            :                     {
    9832                 :          0 :                       printf ("      ");
    9833                 :          0 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1);
    9834                 :          0 :                       printf ("..\n      ");
    9835                 :          0 :                       print_dwarf_addr (dwflmod, address_size,
    9836                 :            :                                         addr2 - 1, addr2);
    9837                 :          0 :                       printf ("\n");
    9838                 :            :                     }
    9839                 :            :                 }
    9840         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9841                 :          0 :                 goto invalid_entry;
    9842                 :          0 :               get_uleb128 (len, readp, nexthdr);
    9843         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < len)
    9844                 :          0 :                 goto invalid_entry;
    9845                 :          0 :               print_ops (dwflmod, dbg, 8, 8, version,
    9846                 :            :                          address_size, offset_size, cu, len, readp);
    9847                 :          0 :               readp += len;
    9848                 :          0 :               break;
    9849                 :            : 
    9850                 :         26 :             case DW_LLE_startx_length:
    9851         [ -  + ]:         26 :               if ((uint64_t) (nexthdr - readp) < 1)
    9852                 :          0 :                 goto invalid_entry;
    9853                 :         26 :               get_uleb128 (op1, readp, nexthdr);
    9854         [ -  + ]:         26 :               if ((uint64_t) (nexthdr - readp) < 1)
    9855                 :          0 :                 goto invalid_entry;
    9856                 :         26 :               get_uleb128 (op2, readp, nexthdr);
    9857                 :         26 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    9858         [ +  - ]:         26 :               if (! print_unresolved_addresses)
    9859                 :            :                 {
    9860                 :         26 :                   Dwarf_Addr addr1;
    9861                 :         26 :                   Dwarf_Addr addr2;
    9862         [ -  + ]:         26 :                   if (get_indexed_addr (cu, op1, &addr1) != 0)
    9863                 :            :                     {
    9864                 :          0 :                       printf ("      ???..\n");
    9865                 :          0 :                       printf ("      ???\n");
    9866                 :            :                     }
    9867                 :            :                   else
    9868                 :            :                     {
    9869                 :         26 :                       addr2 = addr1 + op2;
    9870                 :         26 :                       printf ("      ");
    9871                 :         26 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1);
    9872                 :         26 :                       printf ("..\n      ");
    9873                 :         26 :                       print_dwarf_addr (dwflmod, address_size,
    9874                 :            :                                         addr2 - 1, addr2);
    9875                 :         26 :                       printf ("\n");
    9876                 :            :                     }
    9877                 :            :                 }
    9878         [ -  + ]:         26 :               if ((uint64_t) (nexthdr - readp) < 1)
    9879                 :          0 :                 goto invalid_entry;
    9880                 :         26 :               get_uleb128 (len, readp, nexthdr);
    9881         [ -  + ]:         26 :               if ((uint64_t) (nexthdr - readp) < len)
    9882                 :          0 :                 goto invalid_entry;
    9883                 :         26 :               print_ops (dwflmod, dbg, 8, 8, version,
    9884                 :            :                          address_size, offset_size, cu, len, readp);
    9885                 :         26 :               readp += len;
    9886                 :         26 :               break;
    9887                 :            : 
    9888                 :         24 :             case DW_LLE_offset_pair:
    9889         [ -  + ]:         24 :               if ((uint64_t) (nexthdr - readp) < 1)
    9890                 :          0 :                 goto invalid_entry;
    9891                 :         24 :               get_uleb128 (op1, readp, nexthdr);
    9892         [ -  + ]:         24 :               if ((uint64_t) (nexthdr - readp) < 1)
    9893                 :          0 :                 goto invalid_entry;
    9894                 :         24 :               get_uleb128 (op2, readp, nexthdr);
    9895                 :         24 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    9896         [ +  - ]:         24 :               if (! print_unresolved_addresses)
    9897                 :            :                 {
    9898                 :         24 :                   op1 += base;
    9899                 :         24 :                   op2 += base;
    9900                 :         24 :                   printf ("      ");
    9901                 :         24 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    9902                 :         24 :                   printf ("..\n      ");
    9903                 :         24 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    9904                 :         24 :                   printf ("\n");
    9905                 :            :                 }
    9906         [ -  + ]:         24 :               if ((uint64_t) (nexthdr - readp) < 1)
    9907                 :          0 :                 goto invalid_entry;
    9908                 :         24 :               get_uleb128 (len, readp, nexthdr);
    9909         [ -  + ]:         24 :               if ((uint64_t) (nexthdr - readp) < len)
    9910                 :          0 :                 goto invalid_entry;
    9911                 :         24 :               print_ops (dwflmod, dbg, 8, 8, version,
    9912                 :            :                          address_size, offset_size, cu, len, readp);
    9913                 :         24 :               readp += len;
    9914                 :         24 :               break;
    9915                 :            : 
    9916                 :          0 :             case DW_LLE_default_location:
    9917         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9918                 :          0 :                 goto invalid_entry;
    9919                 :          0 :               get_uleb128 (len, readp, nexthdr);
    9920         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < len)
    9921                 :          0 :                 goto invalid_entry;
    9922                 :          0 :               print_ops (dwflmod, dbg, 8, 8, version,
    9923                 :            :                          address_size, offset_size, cu, len, readp);
    9924                 :          0 :               readp += len;
    9925                 :          0 :               break;
    9926                 :            : 
    9927                 :          5 :             case DW_LLE_base_address:
    9928         [ -  + ]:          5 :               if (address_size == 4)
    9929                 :            :                 {
    9930         [ #  # ]:          0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    9931                 :          0 :                     goto invalid_entry;
    9932         [ #  # ]:          0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    9933                 :            :                 }
    9934                 :            :               else
    9935                 :            :                 {
    9936         [ -  + ]:          5 :                   if ((uint64_t) (nexthdr - readp) < 8)
    9937                 :          0 :                     goto invalid_entry;
    9938         [ -  + ]:          5 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    9939                 :            :                 }
    9940                 :          5 :               base = op1;
    9941                 :          5 :               printf (" 0x%" PRIx64 "\n", base);
    9942         [ +  - ]:          5 :               if (! print_unresolved_addresses)
    9943                 :            :                 {
    9944                 :          5 :                   printf ("      ");
    9945                 :          5 :                   print_dwarf_addr (dwflmod, address_size, base, base);
    9946                 :          5 :                   printf ("\n");
    9947                 :            :                 }
    9948                 :            :               break;
    9949                 :            : 
    9950                 :          0 :             case DW_LLE_start_end:
    9951         [ #  # ]:          0 :               if (address_size == 4)
    9952                 :            :                 {
    9953         [ #  # ]:          0 :                   if ((uint64_t) (nexthdr - readp) < 8)
    9954                 :          0 :                     goto invalid_entry;
    9955         [ #  # ]:          0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    9956         [ #  # ]:          0 :                   op2 = read_4ubyte_unaligned_inc (dbg, readp);
    9957                 :            :                 }
    9958                 :            :               else
    9959                 :            :                 {
    9960         [ #  # ]:          0 :                   if ((uint64_t) (nexthdr - readp) < 16)
    9961                 :          0 :                     goto invalid_entry;
    9962         [ #  # ]:          0 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    9963         [ #  # ]:          0 :                   op2 = read_8ubyte_unaligned_inc (dbg, readp);
    9964                 :            :                 }
    9965                 :          0 :               printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
    9966         [ #  # ]:          0 :               if (! print_unresolved_addresses)
    9967                 :            :                 {
    9968                 :          0 :                   printf ("      ");
    9969                 :          0 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
    9970                 :          0 :                   printf ("..\n      ");
    9971                 :          0 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
    9972                 :          0 :                   printf ("\n");
    9973                 :            :                 }
    9974         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
    9975                 :          0 :                 goto invalid_entry;
    9976                 :          0 :               get_uleb128 (len, readp, nexthdr);
    9977         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < len)
    9978                 :          0 :                 goto invalid_entry;
    9979                 :          0 :               print_ops (dwflmod, dbg, 8, 8, version,
    9980                 :            :                          address_size, offset_size, cu, len, readp);
    9981                 :          0 :               readp += len;
    9982                 :          0 :               break;
    9983                 :            : 
    9984                 :          3 :             case DW_LLE_start_length:
    9985         [ -  + ]:          3 :               if (address_size == 4)
    9986                 :            :                 {
    9987         [ #  # ]:          0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    9988                 :          0 :                     goto invalid_entry;
    9989         [ #  # ]:          0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    9990                 :            :                 }
    9991                 :            :               else
    9992                 :            :                 {
    9993         [ -  + ]:          3 :                   if ((uint64_t) (nexthdr - readp) < 8)
    9994                 :          0 :                     goto invalid_entry;
    9995         [ -  + ]:          3 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    9996                 :            :                 }
    9997         [ -  + ]:          3 :               if ((uint64_t) (nexthdr - readp) < 1)
    9998                 :          0 :                 goto invalid_entry;
    9999                 :          3 :               get_uleb128 (op2, readp, nexthdr);
   10000                 :          3 :               printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
   10001         [ +  + ]:          3 :               if (! print_unresolved_addresses)
   10002                 :            :                 {
   10003                 :          2 :                   op2 = op1 + op2;
   10004                 :          2 :                   printf ("      ");
   10005                 :          2 :                   print_dwarf_addr (dwflmod, address_size, op1, op1);
   10006                 :          2 :                   printf ("..\n      ");
   10007                 :          2 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
   10008                 :          2 :                   printf ("\n");
   10009                 :            :                 }
   10010         [ -  + ]:          3 :               if ((uint64_t) (nexthdr - readp) < 1)
   10011                 :          0 :                 goto invalid_entry;
   10012                 :          3 :               get_uleb128 (len, readp, nexthdr);
   10013         [ -  + ]:          3 :               if ((uint64_t) (nexthdr - readp) < len)
   10014                 :          0 :                 goto invalid_entry;
   10015                 :          3 :               print_ops (dwflmod, dbg, 8, 8, version,
   10016                 :            :                          address_size, offset_size, cu, len, readp);
   10017                 :          3 :               readp += len;
   10018                 :          3 :               break;
   10019                 :            : 
   10020                 :          0 :             case DW_LLE_GNU_view_pair:
   10021         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10022                 :          0 :                 goto invalid_entry;
   10023                 :          0 :               get_uleb128 (op1, readp, nexthdr);
   10024         [ #  # ]:          0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10025                 :          0 :                 goto invalid_entry;
   10026                 :          0 :               get_uleb128 (op2, readp, nexthdr);
   10027         [ +  + ]:         97 :               printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
   10028                 :            :               break;
   10029                 :            : 
   10030                 :          0 :             default:
   10031                 :          0 :               goto invalid_entry;
   10032                 :            :             }
   10033                 :            :         }
   10034                 :            : 
   10035                 :          5 :     next_table:
   10036         [ -  + ]:          5 :       if (readp != nexthdr)
   10037                 :            :         {
   10038                 :          0 :           size_t padding = nexthdr - readp;
   10039                 :          0 :           printf (_("   %zu padding bytes\n\n"), padding);
   10040                 :          0 :           readp = nexthdr;
   10041                 :            :         }
   10042                 :            :     }
   10043                 :            : }
   10044                 :            : 
   10045                 :            : 
   10046                 :            : static void
   10047                 :         42 : print_debug_loc_section (Dwfl_Module *dwflmod,
   10048                 :            :                          Ebl *ebl, GElf_Ehdr *ehdr,
   10049                 :            :                          Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10050                 :            : {
   10051                 :         42 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
   10052         [ -  + ]:         42 :   if (data == NULL)
   10053                 :          0 :     return;
   10054                 :            : 
   10055                 :         42 :   printf (_("\
   10056                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10057                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
   10058                 :         42 :           (uint64_t) shdr->sh_offset);
   10059                 :            : 
   10060                 :         42 :   sort_listptr (&known_locsptr, "loclistptr");
   10061                 :         42 :   size_t listptr_idx = 0;
   10062                 :            : 
   10063         [ +  - ]:         42 :   uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
   10064                 :         42 :   uint_fast8_t offset_size = 4;
   10065                 :            : 
   10066                 :         42 :   bool first = true;
   10067                 :         42 :   Dwarf_Addr base = 0;
   10068                 :         42 :   unsigned char *readp = data->d_buf;
   10069                 :         42 :   unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
   10070                 :         42 :   Dwarf_CU *last_cu = NULL;
   10071         [ +  + ]:     346524 :   while (readp < endp)
   10072                 :            :     {
   10073                 :     346482 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
   10074                 :     346482 :       Dwarf_CU *cu = last_cu;
   10075                 :     346482 :       unsigned int attr = 0;
   10076                 :            : 
   10077   [ +  +  -  + ]:     346482 :       if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
   10078                 :            :                                       &address_size, &offset_size, &base,
   10079                 :            :                                       &cu, offset, &readp, endp, &attr))
   10080                 :      51975 :         continue;
   10081                 :            : 
   10082         [ +  + ]:     346482 :       if (last_cu != cu)
   10083                 :            :        {
   10084                 :       1489 :         Dwarf_Die cudie;
   10085         [ -  + ]:       1489 :         if (dwarf_cu_die (cu, &cudie,
   10086                 :            :                           NULL, NULL, NULL, NULL,
   10087                 :            :                           NULL, NULL) == NULL)
   10088                 :          0 :           printf (_("\n Unknown CU base: "));
   10089                 :            :         else
   10090                 :       1489 :           printf (_("\n CU [%6" PRIx64 "] base: "),
   10091                 :            :                   dwarf_dieoffset (&cudie));
   10092                 :       1489 :         print_dwarf_addr (dwflmod, address_size, base, base);
   10093                 :       1489 :         printf ("\n");
   10094                 :            :        }
   10095                 :     346482 :       last_cu = cu;
   10096                 :            : 
   10097         [ +  + ]:     346482 :       if (attr == DW_AT_GNU_locviews)
   10098                 :            :         {
   10099                 :      51975 :           Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
   10100                 :            :                                                     &listptr_idx, offset);
   10101                 :      51975 :           const unsigned char *locp = readp;
   10102                 :      51975 :           const unsigned char *locendp;
   10103         [ +  - ]:      51975 :           if (next_off == 0
   10104                 :      51975 :               || next_off > (size_t) (endp
   10105         [ +  - ]:      51975 :                                       - (const unsigned char *) data->d_buf))
   10106                 :      51975 :             locendp = endp;
   10107                 :            :           else
   10108                 :      51975 :             locendp = (const unsigned char *) data->d_buf + next_off;
   10109                 :            : 
   10110         [ +  + ]:     294288 :           while (locp < locendp)
   10111                 :            :             {
   10112                 :     242313 :               uint64_t v1, v2;
   10113                 :     242313 :               get_uleb128 (v1, locp, locendp);
   10114         [ -  + ]:     242313 :               if (locp >= locendp)
   10115                 :            :                 {
   10116                 :          0 :                   printf (_(" [%6tx]  <INVALID DATA>\n"), offset);
   10117                 :            :                   break;
   10118                 :            :                 }
   10119                 :     242313 :               get_uleb128 (v2, locp, locendp);
   10120         [ +  + ]:     242313 :               if (first)                /* First view pair in a list.  */
   10121                 :      51975 :                 printf (" [%6tx] ", offset);
   10122                 :            :               else
   10123                 :     190338 :                 printf ("          ");
   10124                 :     242313 :               printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
   10125                 :     242313 :               first = false;
   10126                 :            :             }
   10127                 :            : 
   10128                 :      51975 :           first = true;
   10129                 :      51975 :           readp = (unsigned char *) locendp;
   10130                 :      51975 :           continue;
   10131                 :            :         }
   10132                 :            : 
   10133                 :            :       /* GNU DebugFission encoded addresses as addrx.  */
   10134                 :     589014 :       bool is_debugfission = ((cu != NULL
   10135         [ #  # ]:          0 :                                || split_dwarf_cu_base (dbg, &cu, &base))
   10136   [ -  +  +  - ]:     294507 :                               && (cu->version < 5
   10137         [ +  + ]:     294507 :                                   && cu->unit_type == DW_UT_split_compile));
   10138         [ +  + ]:     294507 :       if (!is_debugfission
   10139         [ -  + ]:     294447 :           && unlikely (data->d_size - offset < (size_t) address_size * 2))
   10140                 :            :         {
   10141                 :          0 :         invalid_data:
   10142                 :          0 :           printf (_(" [%6tx]  <INVALID DATA>\n"), offset);
   10143                 :          0 :           break;
   10144                 :            :         }
   10145                 :            : 
   10146                 :     294507 :       Dwarf_Addr begin;
   10147                 :     294507 :       Dwarf_Addr end;
   10148                 :     294507 :       bool use_base = true;
   10149         [ +  + ]:     294507 :       if (is_debugfission)
   10150                 :            :         {
   10151                 :         60 :           const unsigned char *locp = readp;
   10152                 :         60 :           const unsigned char *locendp = readp + data->d_size;
   10153         [ -  + ]:         60 :           if (locp >= locendp)
   10154                 :          0 :             goto invalid_data;
   10155                 :            : 
   10156                 :         60 :           Dwarf_Word idx;
   10157                 :         60 :           unsigned char code = *locp++;
   10158   [ +  -  -  +  :         60 :           switch (code)
                      - ]
   10159                 :            :             {
   10160                 :         20 :             case DW_LLE_GNU_end_of_list_entry:
   10161                 :         20 :               begin = 0;
   10162                 :         20 :               end = 0;
   10163                 :         20 :               break;
   10164                 :            : 
   10165                 :          0 :             case DW_LLE_GNU_base_address_selection_entry:
   10166         [ #  # ]:          0 :               if (locp >= locendp)
   10167                 :          0 :                 goto invalid_data;
   10168                 :          0 :               begin = (Dwarf_Addr) -1;
   10169                 :          0 :               get_uleb128 (idx, locp, locendp);
   10170         [ #  # ]:          0 :               if (get_indexed_addr (cu, idx, &end) != 0)
   10171                 :          0 :                 end = idx; /* ... */
   10172                 :            :               break;
   10173                 :            : 
   10174                 :          0 :             case DW_LLE_GNU_start_end_entry:
   10175         [ #  # ]:          0 :               if (locp >= locendp)
   10176                 :          0 :                 goto invalid_data;
   10177                 :          0 :               get_uleb128 (idx, locp, locendp);
   10178         [ #  # ]:          0 :               if (get_indexed_addr (cu, idx, &begin) != 0)
   10179                 :          0 :                 begin = idx; /* ... */
   10180         [ #  # ]:          0 :               if (locp >= locendp)
   10181                 :          0 :                 goto invalid_data;
   10182                 :          0 :               get_uleb128 (idx, locp, locendp);
   10183         [ #  # ]:          0 :               if (get_indexed_addr (cu, idx, &end) != 0)
   10184                 :          0 :                 end = idx; /* ... */
   10185                 :            :               use_base = false;
   10186                 :            :               break;
   10187                 :            : 
   10188                 :         40 :             case DW_LLE_GNU_start_length_entry:
   10189         [ -  + ]:         40 :               if (locp >= locendp)
   10190                 :          0 :                 goto invalid_data;
   10191                 :         40 :               get_uleb128 (idx, locp, locendp);
   10192         [ -  + ]:         40 :               if (get_indexed_addr (cu, idx, &begin) != 0)
   10193                 :          0 :                 begin = idx; /* ... */
   10194         [ -  + ]:         40 :               if (locendp - locp < 4)
   10195                 :          0 :                 goto invalid_data;
   10196         [ -  + ]:         40 :               end = read_4ubyte_unaligned_inc (dbg, locp);
   10197                 :         40 :               end += begin;
   10198                 :         40 :               use_base = false;
   10199                 :         40 :               break;
   10200                 :            : 
   10201                 :          0 :             default:
   10202                 :          0 :                 goto invalid_data;
   10203                 :            :             }
   10204                 :            : 
   10205                 :         60 :           readp = (unsigned char *) locp;
   10206                 :            :         }
   10207         [ +  - ]:     294447 :       else if (address_size == 8)
   10208                 :            :         {
   10209         [ +  + ]:     294447 :           begin = read_8ubyte_unaligned_inc (dbg, readp);
   10210         [ +  + ]:     294447 :           end = read_8ubyte_unaligned_inc (dbg, readp);
   10211                 :            :         }
   10212                 :            :       else
   10213                 :            :         {
   10214         [ #  # ]:          0 :           begin = read_4ubyte_unaligned_inc (dbg, readp);
   10215         [ #  # ]:          0 :           end = read_4ubyte_unaligned_inc (dbg, readp);
   10216         [ #  # ]:          0 :           if (begin == (Dwarf_Addr) (uint32_t) -1)
   10217                 :          0 :             begin = (Dwarf_Addr) -1l;
   10218                 :            :         }
   10219                 :            : 
   10220         [ -  + ]:     294507 :       if (begin == (Dwarf_Addr) -1l) /* Base address entry.  */
   10221                 :            :         {
   10222         [ #  # ]:          0 :           if (first)
   10223                 :          0 :             printf (" [%6tx] ", offset);
   10224                 :            :           else
   10225                 :          0 :             printf ("          ");
   10226                 :          0 :           puts (_("base address"));
   10227                 :          0 :           printf ("          ");
   10228                 :          0 :           print_dwarf_addr (dwflmod, address_size, end, end);
   10229                 :          0 :           printf ("\n");
   10230                 :          0 :           base = end;
   10231                 :          0 :           first = false;
   10232                 :            :         }
   10233   [ +  +  +  + ]:     294507 :       else if (begin == 0 && end == 0) /* End of list entry.  */
   10234                 :            :         {
   10235         [ -  + ]:      52049 :           if (first)
   10236                 :     294507 :             printf (_(" [%6tx] empty list\n"), offset);
   10237                 :            :           first = true;
   10238                 :            :         }
   10239                 :            :       else
   10240                 :            :         {
   10241                 :            :           /* We have a location expression entry.  */
   10242         [ +  + ]:     242458 :           uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
   10243                 :            : 
   10244         [ +  + ]:     242458 :           if (first)            /* First entry in a list.  */
   10245                 :      52049 :             printf (" [%6tx] ", offset);
   10246                 :            :           else
   10247                 :     190409 :             printf ("          ");
   10248                 :            : 
   10249                 :     242458 :           printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
   10250         [ +  + ]:     242458 :           if (! print_unresolved_addresses)
   10251                 :            :             {
   10252         [ +  + ]:     242428 :               Dwarf_Addr dab = use_base ? base + begin : begin;
   10253         [ +  + ]:     242428 :               Dwarf_Addr dae = use_base ? base + end : end;
   10254                 :     242428 :               printf ("          ");
   10255                 :     242428 :               print_dwarf_addr (dwflmod, address_size, dab, dab);
   10256                 :     242428 :               printf ("..\n          ");
   10257                 :     242428 :               print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
   10258                 :     242428 :               printf ("\n");
   10259                 :            :             }
   10260                 :            : 
   10261         [ -  + ]:     242458 :           if (endp - readp <= (ptrdiff_t) len)
   10262                 :            :             {
   10263                 :          0 :               fputs (_("   <INVALID DATA>\n"), stdout);
   10264                 :          0 :               break;
   10265                 :            :             }
   10266                 :            : 
   10267         [ +  - ]:     484916 :           print_ops (dwflmod, dbg, 11, 11,
   10268                 :     242458 :                      cu != NULL ? cu->version : 3,
   10269                 :            :                      address_size, offset_size, cu, len, readp);
   10270                 :            : 
   10271                 :     242458 :           first = false;
   10272                 :     242458 :           readp += len;
   10273                 :            :         }
   10274                 :            :     }
   10275                 :            : }
   10276                 :            : 
   10277                 :            : struct mac_culist
   10278                 :            : {
   10279                 :            :   Dwarf_Die die;
   10280                 :            :   Dwarf_Off offset;
   10281                 :            :   Dwarf_Files *files;
   10282                 :            :   struct mac_culist *next;
   10283                 :            : };
   10284                 :            : 
   10285                 :            : 
   10286                 :            : static int
   10287                 :          0 : mac_compare (const void *p1, const void *p2)
   10288                 :            : {
   10289                 :          0 :   struct mac_culist *m1 = (struct mac_culist *) p1;
   10290                 :          0 :   struct mac_culist *m2 = (struct mac_culist *) p2;
   10291                 :            : 
   10292         [ #  # ]:          0 :   if (m1->offset < m2->offset)
   10293                 :            :     return -1;
   10294         [ #  # ]:          0 :   if (m1->offset > m2->offset)
   10295                 :          0 :     return 1;
   10296                 :            :   return 0;
   10297                 :            : }
   10298                 :            : 
   10299                 :            : 
   10300                 :            : static void
   10301                 :          0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10302                 :            :                              Ebl *ebl,
   10303                 :            :                              GElf_Ehdr *ehdr __attribute__ ((unused)),
   10304                 :            :                              Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10305                 :            : {
   10306                 :          0 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
   10307         [ #  # ]:          0 :   if (data == NULL)
   10308                 :          0 :     return;
   10309                 :            : 
   10310                 :          0 :   printf (_("\
   10311                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10312                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
   10313                 :          0 :           (uint64_t) shdr->sh_offset);
   10314         [ #  # ]:          0 :   putc_unlocked ('\n', stdout);
   10315                 :            : 
   10316                 :            :   /* There is no function in libdw to iterate over the raw content of
   10317                 :            :      the section but it is easy enough to do.  */
   10318                 :            : 
   10319                 :            :   /* Get the source file information for all CUs.  */
   10320                 :          0 :   Dwarf_Off offset;
   10321                 :          0 :   Dwarf_Off ncu = 0;
   10322                 :          0 :   size_t hsize;
   10323                 :          0 :   struct mac_culist *culist = NULL;
   10324                 :          0 :   size_t nculist = 0;
   10325         [ #  # ]:          0 :   while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
   10326                 :            :     {
   10327                 :          0 :       Dwarf_Die cudie;
   10328         [ #  # ]:          0 :       if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
   10329                 :          0 :         continue;
   10330                 :            : 
   10331                 :          0 :       Dwarf_Attribute attr;
   10332         [ #  # ]:          0 :       if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
   10333                 :          0 :         continue;
   10334                 :            : 
   10335                 :          0 :       Dwarf_Word macoff;
   10336         [ #  # ]:          0 :       if (dwarf_formudata (&attr, &macoff) != 0)
   10337                 :          0 :         continue;
   10338                 :            : 
   10339                 :          0 :       struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
   10340                 :          0 :       newp->die = cudie;
   10341                 :          0 :       newp->offset = macoff;
   10342                 :          0 :       newp->files = NULL;
   10343                 :          0 :       newp->next = culist;
   10344                 :          0 :       culist = newp;
   10345                 :          0 :       ++nculist;
   10346                 :            :     }
   10347                 :            : 
   10348                 :            :   /* Convert the list into an array for easier consumption.  */
   10349                 :          0 :   struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
   10350                 :            :                                                          * sizeof (*cus));
   10351                 :            :   /* Add sentinel.  */
   10352                 :          0 :   cus[nculist].offset = data->d_size;
   10353                 :          0 :   cus[nculist].files = (Dwarf_Files *) -1l;
   10354         [ #  # ]:          0 :   if (nculist > 0)
   10355                 :            :     {
   10356         [ #  # ]:          0 :       for (size_t cnt = nculist - 1; culist != NULL; --cnt)
   10357                 :            :         {
   10358         [ #  # ]:          0 :           assert (cnt < nculist);
   10359                 :          0 :           cus[cnt] = *culist;
   10360                 :          0 :           culist = culist->next;
   10361                 :            :         }
   10362                 :            : 
   10363                 :            :       /* Sort the array according to the offset in the .debug_macinfo
   10364                 :            :          section.  Note we keep the sentinel at the end.  */
   10365                 :          0 :       qsort (cus, nculist, sizeof (*cus), mac_compare);
   10366                 :            :     }
   10367                 :            : 
   10368                 :          0 :   const unsigned char *readp = (const unsigned char *) data->d_buf;
   10369                 :          0 :   const unsigned char *readendp = readp + data->d_size;
   10370                 :          0 :   int level = 1;
   10371                 :            : 
   10372                 :          0 :   while (readp < readendp)
   10373                 :            :     {
   10374                 :          0 :       unsigned int opcode = *readp++;
   10375                 :          0 :       unsigned int u128;
   10376                 :          0 :       unsigned int u128_2;
   10377                 :          0 :       const unsigned char *endp;
   10378                 :            : 
   10379   [ #  #  #  # ]:          0 :       switch (opcode)
   10380                 :            :         {
   10381                 :          0 :         case DW_MACINFO_define:
   10382                 :            :         case DW_MACINFO_undef:
   10383                 :            :         case DW_MACINFO_vendor_ext:
   10384                 :            :           /*  For the first two opcodes the parameters are
   10385                 :            :                 line, string
   10386                 :            :               For the latter
   10387                 :            :                 number, string.
   10388                 :            :               We can treat these cases together.  */
   10389                 :          0 :           get_uleb128 (u128, readp, readendp);
   10390                 :            : 
   10391                 :          0 :           endp = memchr (readp, '\0', readendp - readp);
   10392         [ #  # ]:          0 :           if (unlikely (endp == NULL))
   10393                 :            :             {
   10394                 :          0 :               printf (_("\
   10395                 :            : %*s*** non-terminated string at end of section"),
   10396                 :            :                       level, "");
   10397                 :          0 :               return;
   10398                 :            :             }
   10399                 :            : 
   10400         [ #  # ]:          0 :           if (opcode == DW_MACINFO_define)
   10401                 :          0 :             printf ("%*s#define %s, line %u\n",
   10402                 :            :                     level, "", (char *) readp, u128);
   10403         [ #  # ]:          0 :           else if (opcode == DW_MACINFO_undef)
   10404                 :          0 :             printf ("%*s#undef %s, line %u\n",
   10405                 :            :                     level, "", (char *) readp, u128);
   10406                 :            :           else
   10407                 :          0 :             printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
   10408                 :            : 
   10409                 :          0 :           readp = endp + 1;
   10410                 :          0 :           break;
   10411                 :            : 
   10412                 :          0 :         case DW_MACINFO_start_file:
   10413                 :            :           /* The two parameters are line and file index, in this order.  */
   10414                 :          0 :           get_uleb128 (u128, readp, readendp);
   10415         [ #  # ]:          0 :           if (readendp - readp < 1)
   10416                 :            :             {
   10417                 :          0 :               printf (_("\
   10418                 :            : %*s*** missing DW_MACINFO_start_file argument at end of section"),
   10419                 :            :                       level, "");
   10420                 :          0 :               return;
   10421                 :            :             }
   10422                 :          0 :           get_uleb128 (u128_2, readp, readendp);
   10423                 :            : 
   10424                 :            :           /* Find the CU DIE for this file.  */
   10425                 :          0 :           size_t macoff = readp - (const unsigned char *) data->d_buf;
   10426                 :          0 :           const char *fname = "???";
   10427   [ #  #  #  # ]:          0 :           if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
   10428                 :            :             {
   10429   [ #  #  #  # ]:          0 :               while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
   10430                 :          0 :                 ++cus;
   10431                 :            : 
   10432         [ #  # ]:          0 :               if (cus[0].files == NULL
   10433         [ #  # ]:          0 :                   && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
   10434                 :          0 :                 cus[0].files = (Dwarf_Files *) -1l;
   10435                 :            : 
   10436         [ #  # ]:          0 :               if (cus[0].files != (Dwarf_Files *) -1l)
   10437                 :          0 :                 fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
   10438         [ #  # ]:          0 :                          ?: "???");
   10439                 :            :             }
   10440                 :            : 
   10441                 :          0 :           printf ("%*sstart_file %u, [%u] %s\n",
   10442                 :            :                   level, "", u128, u128_2, fname);
   10443                 :          0 :           ++level;
   10444                 :          0 :           break;
   10445                 :            : 
   10446                 :          0 :         case DW_MACINFO_end_file:
   10447                 :          0 :           --level;
   10448                 :          0 :           printf ("%*send_file\n", level, "");
   10449                 :            :           /* Nothing more to do.  */
   10450                 :            :           break;
   10451                 :            : 
   10452                 :          0 :         default:
   10453                 :            :           // XXX gcc seems to generate files with a trailing zero.
   10454   [ #  #  #  # ]:          0 :           if (unlikely (opcode != 0 || readp != readendp))
   10455         [ #  # ]:          0 :             printf ("%*s*** invalid opcode %u\n", level, "", opcode);
   10456                 :            :           break;
   10457                 :            :         }
   10458                 :            :     }
   10459                 :            : }
   10460                 :            : 
   10461                 :            : 
   10462                 :            : static void
   10463                 :          3 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10464                 :            :                            Ebl *ebl,
   10465                 :            :                            GElf_Ehdr *ehdr __attribute__ ((unused)),
   10466                 :            :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10467                 :            : {
   10468                 :          3 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
   10469         [ +  - ]:          3 :   if (data == NULL)
   10470                 :          0 :     return;
   10471                 :            : 
   10472                 :          3 :   printf (_("\
   10473                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10474                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
   10475                 :          3 :           (uint64_t) shdr->sh_offset);
   10476         [ -  + ]:          3 :   putc_unlocked ('\n', stdout);
   10477                 :            : 
   10478                 :            :   /* Get the source file information for all CUs.  Uses same
   10479                 :            :      datastructure as macinfo.  But uses offset field to directly
   10480                 :            :      match .debug_line offset.  And just stored in a list.  */
   10481                 :          3 :   Dwarf_Off offset;
   10482                 :          3 :   Dwarf_Off ncu = 0;
   10483                 :          3 :   size_t hsize;
   10484                 :          3 :   struct mac_culist *culist = NULL;
   10485         [ +  + ]:          7 :   while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
   10486                 :            :     {
   10487                 :          4 :       Dwarf_Die cudie;
   10488         [ -  + ]:          4 :       if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
   10489                 :          0 :         continue;
   10490                 :            : 
   10491                 :          4 :       Dwarf_Attribute attr;
   10492         [ -  + ]:          4 :       if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
   10493                 :          0 :         continue;
   10494                 :            : 
   10495                 :          4 :       Dwarf_Word lineoff;
   10496         [ -  + ]:          4 :       if (dwarf_formudata (&attr, &lineoff) != 0)
   10497                 :          0 :         continue;
   10498                 :            : 
   10499                 :          4 :       struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
   10500                 :          4 :       newp->die = cudie;
   10501                 :          4 :       newp->offset = lineoff;
   10502                 :          4 :       newp->files = NULL;
   10503                 :          4 :       newp->next = culist;
   10504                 :          4 :       culist = newp;
   10505                 :            :     }
   10506                 :            : 
   10507                 :          3 :   const unsigned char *readp = (const unsigned char *) data->d_buf;
   10508                 :          3 :   const unsigned char *readendp = readp + data->d_size;
   10509                 :            : 
   10510         [ +  + ]:         11 :   while (readp < readendp)
   10511                 :            :     {
   10512                 :         16 :       printf (_(" Offset:             0x%" PRIx64 "\n"),
   10513                 :          8 :               (uint64_t) (readp - (const unsigned char *) data->d_buf));
   10514                 :            : 
   10515                 :            :       // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
   10516                 :            :       // optional vendor extension macro entry table.
   10517         [ -  + ]:          8 :       if (readp + 2 > readendp)
   10518                 :            :         {
   10519                 :          0 :         invalid_data:
   10520                 :          0 :           error (0, 0, _("invalid data"));
   10521                 :          0 :           return;
   10522                 :            :         }
   10523         [ -  + ]:          8 :       const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
   10524                 :          8 :       printf (_(" Version:            %" PRIu16 "\n"), vers);
   10525                 :            : 
   10526                 :            :       // Version 4 is the GNU extension for DWARF4.  DWARF5 will use version
   10527                 :            :       // 5 when it gets standardized.
   10528         [ -  + ]:          8 :       if (vers != 4 && vers != 5)
   10529                 :            :         {
   10530                 :          0 :           printf (_("  unknown version, cannot parse section\n"));
   10531                 :          0 :           return;
   10532                 :            :         }
   10533                 :            : 
   10534         [ -  + ]:          8 :       if (readp + 1 > readendp)
   10535                 :          0 :         goto invalid_data;
   10536                 :          8 :       const unsigned char flag = *readp++;
   10537                 :          8 :       printf (_(" Flag:               0x%" PRIx8), flag);
   10538         [ +  + ]:          8 :       if (flag != 0)
   10539                 :            :         {
   10540                 :          4 :           printf (" (");
   10541         [ -  + ]:          4 :           if ((flag & 0x01) != 0)
   10542                 :            :             {
   10543                 :          0 :               printf ("offset_size");
   10544         [ #  # ]:          0 :               if ((flag & 0xFE) !=  0)
   10545                 :          0 :                 printf (", ");
   10546                 :            :             }
   10547         [ +  - ]:          4 :           if ((flag & 0x02) != 0)
   10548                 :            :             {
   10549                 :          4 :               printf ("debug_line_offset");
   10550         [ -  + ]:          4 :               if ((flag & 0xFC) !=  0)
   10551                 :          0 :                 printf (", ");
   10552                 :            :             }
   10553         [ -  + ]:          4 :           if ((flag & 0x04) != 0)
   10554                 :            :             {
   10555                 :          0 :               printf ("operands_table");
   10556         [ #  # ]:          0 :               if ((flag & 0xF8) !=  0)
   10557                 :          0 :                 printf (", ");
   10558                 :            :             }
   10559         [ -  + ]:          4 :           if ((flag & 0xF8) != 0)
   10560                 :          0 :             printf ("unknown");
   10561                 :          4 :           printf (")");
   10562                 :            :         }
   10563                 :          8 :       printf ("\n");
   10564                 :            : 
   10565         [ +  - ]:          8 :       unsigned int offset_len = (flag & 0x01) ? 8 : 4;
   10566                 :          8 :       printf (_(" Offset length:      %" PRIu8 "\n"), offset_len);
   10567                 :          8 :       Dwarf_Off line_offset = -1;
   10568         [ +  + ]:          8 :       if (flag & 0x02)
   10569                 :            :         {
   10570         [ -  + ]:          4 :           if (offset_len == 8)
   10571         [ #  # ]:          0 :             line_offset = read_8ubyte_unaligned_inc (dbg, readp);
   10572                 :            :           else
   10573         [ -  + ]:          4 :             line_offset = read_4ubyte_unaligned_inc (dbg, readp);
   10574                 :          4 :           printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
   10575                 :            :                   line_offset);
   10576                 :            :         }
   10577                 :            : 
   10578                 :          4 :       struct mac_culist *cu = NULL;
   10579         [ +  - ]:          4 :       if (line_offset != (Dwarf_Off) -1)
   10580                 :            :         {
   10581                 :            :           cu = culist;
   10582   [ +  -  +  + ]:          5 :           while (cu != NULL && line_offset != cu->offset)
   10583                 :          1 :             cu = cu->next;
   10584                 :            :         }
   10585                 :            : 
   10586         [ +  + ]:          8 :       Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
   10587                 :            :                                                                ? cu->die.cu
   10588                 :            :                                                                : NULL));
   10589                 :            : 
   10590                 :          8 :       const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
   10591         [ -  + ]:          8 :       memset (vendor, 0, sizeof vendor);
   10592         [ -  + ]:          8 :       if (flag & 0x04)
   10593                 :            :         {
   10594                 :            :           // 1 byte length, for each item, 1 byte opcode, uleb128 number
   10595                 :            :           // of arguments, for each argument 1 byte form code.
   10596         [ #  # ]:          0 :           if (readp + 1 > readendp)
   10597                 :          0 :             goto invalid_data;
   10598                 :          0 :           unsigned int tlen = *readp++;
   10599                 :          0 :           printf (_("  extension opcode table, %" PRIu8 " items:\n"),
   10600                 :            :                   tlen);
   10601         [ #  # ]:          0 :           for (unsigned int i = 0; i < tlen; i++)
   10602                 :            :             {
   10603         [ #  # ]:          0 :               if (readp + 1 > readendp)
   10604                 :          0 :                 goto invalid_data;
   10605                 :          0 :               unsigned int opcode = *readp++;
   10606                 :          0 :               printf (_("    [%" PRIx8 "]"), opcode);
   10607                 :          0 :               if (opcode < DW_MACRO_lo_user
   10608         [ #  # ]:          0 :                   || opcode > DW_MACRO_hi_user)
   10609                 :          0 :                 goto invalid_data;
   10610                 :            :               // Record the start of description for this vendor opcode.
   10611                 :            :               // uleb128 nr args, 1 byte per arg form.
   10612                 :          0 :               vendor[opcode - DW_MACRO_lo_user] = readp;
   10613         [ #  # ]:          0 :               if (readp + 1 > readendp)
   10614                 :          0 :                 goto invalid_data;
   10615                 :          0 :               unsigned int args = *readp++;
   10616         [ #  # ]:          0 :               if (args > 0)
   10617                 :            :                 {
   10618                 :          0 :                   printf (_(" %" PRIu8 " arguments:"), args);
   10619                 :          0 :                   while (args > 0)
   10620                 :            :                     {
   10621         [ #  # ]:          0 :                       if (readp + 1 > readendp)
   10622                 :          0 :                         goto invalid_data;
   10623                 :          0 :                       unsigned int form = *readp++;
   10624                 :          0 :                       printf (" %s", dwarf_form_name (form));
   10625         [ #  # ]:          0 :                       if (! libdw_valid_user_form (form))
   10626                 :          0 :                         goto invalid_data;
   10627                 :          0 :                       args--;
   10628         [ #  # ]:          0 :                       if (args > 0)
   10629   [ #  #  #  # ]:          0 :                         putchar_unlocked (',');
   10630                 :            :                     }
   10631                 :            :                 }
   10632                 :            :               else
   10633                 :          0 :                 printf (_(" no arguments."));
   10634         [ #  # ]:          0 :               putchar_unlocked ('\n');
   10635                 :            :             }
   10636                 :            :         }
   10637         [ -  + ]:          8 :       putchar_unlocked ('\n');
   10638                 :            : 
   10639                 :          8 :       int level = 1;
   10640         [ -  + ]:          8 :       if (readp + 1 > readendp)
   10641                 :          0 :         goto invalid_data;
   10642                 :          8 :       unsigned int opcode = *readp++;
   10643                 :          8 :       while (opcode != 0)
   10644                 :            :         {
   10645                 :        727 :           unsigned int u128;
   10646                 :        727 :           unsigned int u128_2;
   10647                 :        727 :           const unsigned char *endp;
   10648                 :        727 :           uint64_t off;
   10649                 :            : 
   10650   [ +  +  +  -  :        727 :           switch (opcode)
          +  +  +  -  -  
             -  -  -  - ]
   10651                 :            :             {
   10652                 :          6 :             case DW_MACRO_start_file:
   10653                 :          6 :               get_uleb128 (u128, readp, readendp);
   10654         [ -  + ]:          6 :               if (readp >= readendp)
   10655                 :          0 :                 goto invalid_data;
   10656                 :          6 :               get_uleb128 (u128_2, readp, readendp);
   10657                 :            : 
   10658                 :            :               /* Find the CU DIE that matches this line offset.  */
   10659                 :          6 :               const char *fname = "???";
   10660         [ +  - ]:          6 :               if (cu != NULL)
   10661                 :            :                 {
   10662         [ +  + ]:          6 :                   if (cu->files == NULL
   10663         [ -  + ]:          4 :                       && dwarf_getsrcfiles (&cu->die, &cu->files,
   10664                 :            :                                             NULL) != 0)
   10665                 :          0 :                     cu->files = (Dwarf_Files *) -1l;
   10666                 :            : 
   10667         [ +  - ]:          6 :                   if (cu->files != (Dwarf_Files *) -1l)
   10668                 :          6 :                     fname = (dwarf_filesrc (cu->files, u128_2,
   10669         [ -  + ]:          6 :                                             NULL, NULL) ?: "???");
   10670                 :            :                 }
   10671                 :          6 :               printf ("%*sstart_file %u, [%u] %s\n",
   10672                 :            :                       level, "", u128, u128_2, fname);
   10673                 :          6 :               ++level;
   10674                 :          6 :               break;
   10675                 :            : 
   10676                 :          6 :             case DW_MACRO_end_file:
   10677                 :          6 :               --level;
   10678                 :          6 :               printf ("%*send_file\n", level, "");
   10679                 :            :               break;
   10680                 :            : 
   10681                 :          1 :             case DW_MACRO_define:
   10682                 :          1 :               get_uleb128 (u128, readp, readendp);
   10683                 :          1 :               endp = memchr (readp, '\0', readendp - readp);
   10684         [ -  + ]:          1 :               if (endp == NULL)
   10685                 :          0 :                 goto invalid_data;
   10686                 :          1 :               printf ("%*s#define %s, line %u\n",
   10687                 :            :                       level, "", readp, u128);
   10688                 :          1 :               readp = endp + 1;
   10689                 :          1 :               break;
   10690                 :            : 
   10691                 :          0 :             case DW_MACRO_undef:
   10692                 :          0 :               get_uleb128 (u128, readp, readendp);
   10693                 :          0 :               endp = memchr (readp, '\0', readendp - readp);
   10694         [ #  # ]:          0 :               if (endp == NULL)
   10695                 :          0 :                 goto invalid_data;
   10696                 :          0 :               printf ("%*s#undef %s, line %u\n",
   10697                 :            :                       level, "", readp, u128);
   10698                 :          0 :               readp = endp + 1;
   10699                 :          0 :               break;
   10700                 :            : 
   10701                 :        707 :             case DW_MACRO_define_strp:
   10702                 :        707 :               get_uleb128 (u128, readp, readendp);
   10703         [ -  + ]:        707 :               if (readp + offset_len > readendp)
   10704                 :          0 :                 goto invalid_data;
   10705         [ -  + ]:        707 :               if (offset_len == 8)
   10706         [ #  # ]:          0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   10707                 :            :               else
   10708         [ -  + ]:        707 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   10709                 :        707 :               printf ("%*s#define %s, line %u (indirect)\n",
   10710                 :            :                       level, "", dwarf_getstring (dbg, off, NULL), u128);
   10711                 :            :               break;
   10712                 :            : 
   10713                 :          1 :             case DW_MACRO_undef_strp:
   10714                 :          1 :               get_uleb128 (u128, readp, readendp);
   10715         [ -  + ]:          1 :               if (readp + offset_len > readendp)
   10716                 :          0 :                 goto invalid_data;
   10717         [ -  + ]:          1 :               if (offset_len == 8)
   10718         [ #  # ]:          0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   10719                 :            :               else
   10720         [ -  + ]:          1 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   10721                 :          1 :               printf ("%*s#undef %s, line %u (indirect)\n",
   10722                 :            :                       level, "", dwarf_getstring (dbg, off, NULL), u128);
   10723                 :            :               break;
   10724                 :            : 
   10725                 :          6 :             case DW_MACRO_import:
   10726         [ -  + ]:          6 :               if (readp + offset_len > readendp)
   10727                 :          0 :                 goto invalid_data;
   10728         [ -  + ]:          6 :               if (offset_len == 8)
   10729         [ #  # ]:          0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   10730                 :            :               else
   10731         [ -  + ]:          6 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   10732                 :          6 :               printf ("%*s#include offset 0x%" PRIx64 "\n",
   10733                 :            :                       level, "", off);
   10734                 :            :               break;
   10735                 :            : 
   10736                 :          0 :             case DW_MACRO_define_sup:
   10737                 :          0 :               get_uleb128 (u128, readp, readendp);
   10738         [ #  # ]:          0 :               if (readp + offset_len > readendp)
   10739                 :          0 :                 goto invalid_data;
   10740                 :          0 :               printf ("%*s#define ", level, "");
   10741                 :          0 :               readp =  print_form_data (dbg, DW_FORM_strp_sup,
   10742                 :            :                                         readp, readendp, offset_len,
   10743                 :            :                                         str_offsets_base);
   10744                 :          0 :               printf (", line %u (sup)\n", u128);
   10745                 :            :               break;
   10746                 :            : 
   10747                 :          0 :             case DW_MACRO_undef_sup:
   10748                 :          0 :               get_uleb128 (u128, readp, readendp);
   10749         [ #  # ]:          0 :               if (readp + offset_len > readendp)
   10750                 :          0 :                 goto invalid_data;
   10751                 :          0 :               printf ("%*s#undef ", level, "");
   10752                 :          0 :               readp =  print_form_data (dbg, DW_FORM_strp_sup,
   10753                 :            :                                         readp, readendp, offset_len,
   10754                 :            :                                         str_offsets_base);
   10755                 :          0 :               printf (", line %u (sup)\n", u128);
   10756                 :            :               break;
   10757                 :            : 
   10758                 :          0 :             case DW_MACRO_import_sup:
   10759         [ #  # ]:          0 :               if (readp + offset_len > readendp)
   10760                 :          0 :                 goto invalid_data;
   10761         [ #  # ]:          0 :               if (offset_len == 8)
   10762         [ #  # ]:          0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   10763                 :            :               else
   10764         [ #  # ]:          0 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   10765                 :            :               // XXX Needs support for reading from supplementary object file.
   10766                 :          0 :               printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
   10767                 :            :                       level, "", off);
   10768                 :            :               break;
   10769                 :            : 
   10770                 :          0 :             case DW_MACRO_define_strx:
   10771                 :          0 :               get_uleb128 (u128, readp, readendp);
   10772         [ #  # ]:          0 :               if (readp + offset_len > readendp)
   10773                 :          0 :                 goto invalid_data;
   10774                 :          0 :               printf ("%*s#define ", level, "");
   10775                 :          0 :               readp =  print_form_data (dbg, DW_FORM_strx,
   10776                 :            :                                         readp, readendp, offset_len,
   10777                 :            :                                         str_offsets_base);
   10778                 :          0 :               printf (", line %u (strx)\n", u128);
   10779                 :            :               break;
   10780                 :            : 
   10781                 :          0 :             case DW_MACRO_undef_strx:
   10782                 :          0 :               get_uleb128 (u128, readp, readendp);
   10783         [ #  # ]:          0 :               if (readp + offset_len > readendp)
   10784                 :          0 :                 goto invalid_data;
   10785                 :          0 :               printf ("%*s#undef ", level, "");
   10786                 :          0 :               readp =  print_form_data (dbg, DW_FORM_strx,
   10787                 :            :                                         readp, readendp, offset_len,
   10788                 :            :                                         str_offsets_base);
   10789                 :          0 :               printf (", line %u (strx)\n", u128);
   10790                 :            :               break;
   10791                 :            : 
   10792                 :            :             default:
   10793                 :          0 :               printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
   10794         [ #  # ]:          0 :               if (opcode < DW_MACRO_lo_user
   10795                 :            :                   || opcode > DW_MACRO_lo_user
   10796         [ #  # ]:          0 :                   || vendor[opcode - DW_MACRO_lo_user] == NULL)
   10797                 :          0 :                 goto invalid_data;
   10798                 :            : 
   10799                 :          0 :               const unsigned char *op_desc;
   10800                 :          0 :               op_desc = vendor[opcode - DW_MACRO_lo_user];
   10801                 :            : 
   10802                 :            :               // Just skip the arguments, we cannot really interpret them,
   10803                 :            :               // but print as much as we can.
   10804                 :          0 :               unsigned int args = *op_desc++;
   10805         [ #  # ]:          0 :               while (args > 0 && readp < readendp)
   10806                 :            :                 {
   10807                 :          0 :                   unsigned int form = *op_desc++;
   10808                 :          0 :                   readp = print_form_data (dbg, form, readp, readendp,
   10809                 :            :                                            offset_len, str_offsets_base);
   10810                 :          0 :                   args--;
   10811         [ #  # ]:          0 :                   if (args > 0)
   10812         [ #  # ]:          0 :                     printf (", ");
   10813                 :            :                 }
   10814         [ #  # ]:          0 :               putchar_unlocked ('\n');
   10815                 :            :             }
   10816                 :            : 
   10817         [ -  + ]:        727 :           if (readp + 1 > readendp)
   10818                 :          0 :             goto invalid_data;
   10819                 :        727 :           opcode = *readp++;
   10820         [ +  + ]:        727 :           if (opcode == 0)
   10821   [ -  +  +  + ]:        743 :             putchar_unlocked ('\n');
   10822                 :            :         }
   10823                 :            :     }
   10824                 :            : }
   10825                 :            : 
   10826                 :            : 
   10827                 :            : /* Callback for printing global names.  */
   10828                 :            : static int
   10829                 :         34 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
   10830                 :            :                 void *arg)
   10831                 :            : {
   10832                 :         34 :   int *np = (int *) arg;
   10833                 :            : 
   10834                 :         68 :   printf (_(" [%5d] DIE offset: %6" PRId64
   10835                 :            :                    ", CU DIE offset: %6" PRId64 ", name: %s\n"),
   10836                 :         34 :           (*np)++, global->die_offset, global->cu_offset, global->name);
   10837                 :            : 
   10838                 :         34 :   return 0;
   10839                 :            : }
   10840                 :            : 
   10841                 :            : 
   10842                 :            : /* Print the known exported symbols in the DWARF section '.debug_pubnames'.  */
   10843                 :            : static void
   10844                 :          8 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10845                 :            :                               Ebl *ebl,
   10846                 :            :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
   10847                 :            :                               Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10848                 :            : {
   10849                 :            :   /* Check section actually exists.  */
   10850         [ -  + ]:          8 :   if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
   10851                 :          0 :       return;
   10852                 :            : 
   10853                 :          8 :   printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10854                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
   10855                 :          8 :           (uint64_t) shdr->sh_offset);
   10856                 :            : 
   10857                 :          8 :   int n = 0;
   10858                 :          8 :   (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
   10859                 :            : }
   10860                 :            : 
   10861                 :            : /* Print the content of the DWARF string section '.debug_str'
   10862                 :            :    or 'debug_line_str'.  */
   10863                 :            : static void
   10864                 :         47 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10865                 :            :                          Ebl *ebl,
   10866                 :            :                          GElf_Ehdr *ehdr __attribute__ ((unused)),
   10867                 :            :                          Elf_Scn *scn, GElf_Shdr *shdr,
   10868                 :            :                          Dwarf *dbg __attribute__ ((unused)))
   10869                 :            : {
   10870                 :         47 :   const char *name = section_name (ebl, shdr);
   10871         [ +  - ]:         47 :   int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
   10872         [ +  - ]:         47 :              ? IDX_debug_line_str : IDX_debug_str);
   10873                 :         47 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
   10874         [ +  - ]:         47 :   if (data == NULL)
   10875                 :            :     return;
   10876                 :            : 
   10877                 :         47 :   const size_t sh_size = data->d_size;
   10878                 :            : 
   10879                 :            :   /* Compute floor(log16(shdr->sh_size)).  */
   10880                 :         47 :   GElf_Addr tmp = sh_size;
   10881                 :         47 :   int digits = 1;
   10882         [ +  + ]:        168 :   while (tmp >= 16)
   10883                 :            :     {
   10884                 :        121 :       ++digits;
   10885                 :        121 :       tmp >>= 4;
   10886                 :            :     }
   10887                 :         47 :   digits = MAX (4, digits);
   10888                 :            : 
   10889                 :         47 :   printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
   10890                 :            :                    " %*s  String\n"),
   10891                 :            :           elf_ndxscn (scn),
   10892                 :         47 :           section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
   10893                 :            :           /* TRANS: the debugstr| prefix makes the string unique.  */
   10894         [ +  - ]:         47 :           digits + 2, sgettext ("debugstr|Offset"));
   10895                 :            : 
   10896                 :         47 :   Dwarf_Off offset = 0;
   10897         [ +  + ]:      76306 :   while (offset < sh_size)
   10898                 :            :     {
   10899                 :      76259 :       size_t len;
   10900                 :      76259 :       const char *str = (const char *) data->d_buf + offset;
   10901                 :      76259 :       const char *endp = memchr (str, '\0', sh_size - offset);
   10902         [ -  + ]:      76259 :       if (unlikely (endp == NULL))
   10903                 :            :         {
   10904                 :          0 :           printf (_(" *** error, missing string terminator\n"));
   10905                 :            :           break;
   10906                 :            :         }
   10907                 :            : 
   10908                 :      76259 :       printf (" [%*" PRIx64 "]  \"%s\"\n", digits, (uint64_t) offset, str);
   10909                 :      76259 :       len = endp - str;
   10910                 :      76259 :       offset += len + 1;
   10911                 :            :     }
   10912                 :            : }
   10913                 :            : 
   10914                 :            : static void
   10915                 :          4 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10916                 :            :                                  Ebl *ebl,
   10917                 :            :                                  GElf_Ehdr *ehdr __attribute__ ((unused)),
   10918                 :            :                                  Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   10919                 :            : {
   10920                 :          4 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
   10921         [ +  - ]:          4 :   if (data == NULL)
   10922                 :            :     return;
   10923                 :            : 
   10924                 :          4 :   printf (_("\
   10925                 :            : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10926                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
   10927                 :          4 :           (uint64_t) shdr->sh_offset);
   10928                 :            : 
   10929         [ +  - ]:          4 :   if (shdr->sh_size == 0)
   10930                 :            :     return;
   10931                 :            : 
   10932                 :          4 :   size_t idx = 0;
   10933                 :          4 :   sort_listptr (&known_stroffbases, "str_offsets");
   10934                 :            : 
   10935                 :          4 :   const unsigned char *start = (const unsigned char *) data->d_buf;
   10936                 :          4 :   const unsigned char *readp = start;
   10937                 :          4 :   const unsigned char *readendp = ((const unsigned char *) data->d_buf
   10938                 :          4 :                                    + data->d_size);
   10939                 :            : 
   10940                 :          4 :   while (readp < readendp)
   10941                 :            :     {
   10942                 :            :       /* Most string offset tables will have a header.  For split
   10943                 :            :          dwarf unit GNU DebugFission didn't add one.  But they were
   10944                 :            :          also only defined for split units (main or skeleton units
   10945                 :            :          didn't have indirect strings).  So if we don't have a
   10946                 :            :          DW_AT_str_offsets_base at all and this is offset zero, then
   10947                 :            :          just start printing offsets immediately, if this is a .dwo
   10948                 :            :          section.  */
   10949                 :          4 :       Dwarf_Off off = (Dwarf_Off) (readp
   10950                 :          4 :                                    - (const unsigned char *) data->d_buf);
   10951                 :            : 
   10952                 :          4 :       printf ("Table at offset %" PRIx64 " ", off);
   10953                 :            : 
   10954         [ -  + ]:          4 :       struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
   10955                 :          4 :       const unsigned char *next_unitp = readendp;
   10956                 :          0 :       uint8_t offset_size;
   10957                 :          0 :       bool has_header;
   10958         [ #  # ]:          0 :       if (listptr == NULL)
   10959                 :            :         {
   10960                 :            :           /* This can happen for .dwo files.  There is only an header
   10961                 :            :              in the case this is a version 5 split DWARF file.  */
   10962                 :          4 :           Dwarf_CU *cu;
   10963                 :          4 :           uint8_t unit_type;
   10964         [ -  + ]:          4 :           if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
   10965                 :            :                                NULL, NULL) != 0)
   10966                 :            :             {
   10967                 :          0 :               error (0, 0, "Warning: Cannot find any DWARF unit.");
   10968                 :            :               /* Just guess some values.  */
   10969                 :          0 :               has_header = false;
   10970                 :          0 :               offset_size = 4;
   10971                 :            :             }
   10972         [ +  - ]:          4 :           else if (off == 0
   10973                 :          4 :                    && (unit_type == DW_UT_split_type
   10974         [ +  - ]:          4 :                        || unit_type == DW_UT_split_compile))
   10975                 :            :             {
   10976                 :          4 :               has_header = cu->version > 4;
   10977                 :          4 :               offset_size = cu->offset_size;
   10978                 :            :             }
   10979                 :            :           else
   10980                 :            :             {
   10981                 :          0 :               error (0, 0,
   10982                 :            :                      "Warning: No CU references .debug_str_offsets after %"
   10983                 :            :                      PRIx64, off);
   10984                 :          0 :               has_header = cu->version > 4;
   10985                 :          0 :               offset_size = cu->offset_size;
   10986                 :            :             }
   10987                 :          4 :           printf ("\n");
   10988                 :            :         }
   10989                 :            :       else
   10990                 :            :         {
   10991                 :            :           /* This must be DWARF5, since GNU DebugFission didn't define
   10992                 :            :              DW_AT_str_offsets_base.  */
   10993                 :          0 :           has_header = true;
   10994                 :            : 
   10995                 :          0 :           Dwarf_Die cudie;
   10996         [ #  # ]:          0 :           if (dwarf_cu_die (listptr->cu, &cudie,
   10997                 :            :                             NULL, NULL, NULL, NULL,
   10998                 :            :                             NULL, NULL) == NULL)
   10999                 :          0 :             printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
   11000                 :            :           else
   11001                 :          0 :             printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
   11002                 :            :         }
   11003                 :            : 
   11004         [ +  + ]:          4 :       if (has_header)
   11005                 :            :         {
   11006                 :          2 :           uint64_t unit_length;
   11007                 :          2 :           uint16_t version;
   11008                 :          2 :           uint16_t padding;
   11009                 :            : 
   11010         [ -  + ]:          2 :           unit_length = read_4ubyte_unaligned_inc (dbg, readp);
   11011         [ -  + ]:          2 :           if (unlikely (unit_length == 0xffffffff))
   11012                 :            :             {
   11013         [ #  # ]:          0 :               if (unlikely (readp > readendp - 8))
   11014                 :            :                 {
   11015                 :          0 :                 invalid_data:
   11016                 :          0 :                   error (0, 0, "Invalid data");
   11017                 :          0 :                   return;
   11018                 :            :                 }
   11019         [ #  # ]:          0 :               unit_length = read_8ubyte_unaligned_inc (dbg, readp);
   11020                 :          0 :               offset_size = 8;
   11021                 :            :             }
   11022                 :            :           else
   11023                 :            :             offset_size = 4;
   11024                 :            : 
   11025                 :          2 :           printf ("\n");
   11026                 :          2 :           printf (_(" Length:        %8" PRIu64 "\n"),
   11027                 :            :                   unit_length);
   11028                 :          2 :           printf (_(" Offset size:   %8" PRIu8 "\n"),
   11029                 :            :                   offset_size);
   11030                 :            : 
   11031                 :            :           /* We need at least 2-bytes (version) + 2-bytes (padding) =
   11032                 :            :              4 bytes to complete the header.  And this unit cannot go
   11033                 :            :              beyond the section data.  */
   11034         [ +  - ]:          2 :           if (readp > readendp - 4
   11035         [ +  - ]:          2 :               || unit_length < 4
   11036         [ -  + ]:          2 :               || unit_length > (uint64_t) (readendp - readp))
   11037                 :          0 :             goto invalid_data;
   11038                 :            : 
   11039                 :          2 :           next_unitp = readp + unit_length;
   11040                 :            : 
   11041         [ -  + ]:          2 :           version = read_2ubyte_unaligned_inc (dbg, readp);
   11042                 :          2 :           printf (_(" DWARF version: %8" PRIu16 "\n"), version);
   11043                 :            : 
   11044         [ -  + ]:          2 :           if (version != 5)
   11045                 :            :             {
   11046                 :          0 :               error (0, 0, _("Unknown version"));
   11047                 :          0 :               goto next_unit;
   11048                 :            :             }
   11049                 :            : 
   11050         [ -  + ]:          2 :           padding = read_2ubyte_unaligned_inc (dbg, readp);
   11051                 :          2 :           printf (_(" Padding:       %8" PRIx16 "\n"), padding);
   11052                 :            : 
   11053         [ -  + ]:          2 :           if (listptr != NULL
   11054         [ #  # ]:          0 :               && listptr->offset != (Dwarf_Off) (readp - start))
   11055                 :            :             {
   11056                 :          0 :               error (0, 0, "String offsets index doesn't start after header");
   11057                 :          0 :               goto next_unit;
   11058                 :            :             }
   11059                 :            : 
   11060                 :          2 :           printf ("\n");
   11061                 :            :         }
   11062                 :            : 
   11063                 :          4 :       int digits = 1;
   11064                 :          4 :       size_t offsets = (next_unitp - readp) / offset_size;
   11065         [ +  + ]:          8 :       while (offsets >= 10)
   11066                 :            :         {
   11067                 :          4 :           ++digits;
   11068                 :          4 :           offsets /= 10;
   11069                 :            :         }
   11070                 :            : 
   11071                 :          4 :       unsigned int uidx = 0;
   11072                 :          4 :       size_t index_offset =  readp - (const unsigned char *) data->d_buf;
   11073                 :          4 :       printf (" Offsets start at 0x%zx:\n", index_offset);
   11074                 :         64 :       while (readp <= next_unitp - offset_size)
   11075                 :            :         {
   11076                 :         60 :           Dwarf_Word offset;
   11077         [ +  - ]:         60 :           if (offset_size == 4)
   11078         [ -  + ]:         60 :             offset = read_4ubyte_unaligned_inc (dbg, readp);
   11079                 :            :           else
   11080         [ #  # ]:          0 :             offset = read_8ubyte_unaligned_inc (dbg, readp);
   11081                 :         60 :           const char *str = dwarf_getstring (dbg, offset, NULL);
   11082   [ -  +  +  + ]:        124 :           printf (" [%*u] [%*" PRIx64 "]  \"%s\"\n",
   11083                 :         60 :                   digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
   11084                 :            :         }
   11085                 :          4 :       printf ("\n");
   11086                 :            : 
   11087         [ +  - ]:          4 :       if (readp != next_unitp)
   11088         [ +  + ]:          8 :         error (0, 0, "extra %zd bytes at end of unit",
   11089                 :          0 :                (size_t) (next_unitp - readp));
   11090                 :            : 
   11091                 :          4 :     next_unit:
   11092                 :            :       readp = next_unitp;
   11093                 :            :     }
   11094                 :            : }
   11095                 :            : 
   11096                 :            : 
   11097                 :            : /* Print the content of the call frame search table section
   11098                 :            :    '.eh_frame_hdr'.  */
   11099                 :            : static void
   11100                 :         84 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   11101                 :            :                                Ebl *ebl __attribute__ ((unused)),
   11102                 :            :                                GElf_Ehdr *ehdr __attribute__ ((unused)),
   11103                 :            :                                Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   11104                 :            : {
   11105                 :         84 :   printf (_("\
   11106                 :            : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
   11107                 :            :           elf_ndxscn (scn));
   11108                 :            : 
   11109                 :         84 :   Elf_Data *data = elf_rawdata (scn, NULL);
   11110                 :            : 
   11111         [ -  + ]:         84 :   if (unlikely (data == NULL))
   11112                 :            :     {
   11113                 :          0 :       error (0, 0, _("cannot get %s content: %s"),
   11114                 :            :              ".eh_frame_hdr", elf_errmsg (-1));
   11115                 :          0 :       return;
   11116                 :            :     }
   11117                 :            : 
   11118                 :         84 :   const unsigned char *readp = data->d_buf;
   11119                 :         84 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
   11120                 :         84 :                                         + data->d_size);
   11121                 :            : 
   11122         [ -  + ]:         84 :   if (unlikely (readp + 4 > dataend))
   11123                 :            :     {
   11124                 :          0 :     invalid_data:
   11125                 :          0 :       error (0, 0, _("invalid data"));
   11126                 :          0 :       return;
   11127                 :            :     }
   11128                 :            : 
   11129                 :         84 :   unsigned int version = *readp++;
   11130                 :         84 :   unsigned int eh_frame_ptr_enc = *readp++;
   11131                 :         84 :   unsigned int fde_count_enc = *readp++;
   11132                 :         84 :   unsigned int table_enc = *readp++;
   11133                 :            : 
   11134                 :         84 :   printf (" version:          %u\n"
   11135                 :            :           " eh_frame_ptr_enc: %#x ",
   11136                 :            :           version, eh_frame_ptr_enc);
   11137                 :         84 :   print_encoding_base ("", eh_frame_ptr_enc);
   11138                 :         84 :   printf (" fde_count_enc:    %#x ", fde_count_enc);
   11139                 :         84 :   print_encoding_base ("", fde_count_enc);
   11140                 :         84 :   printf (" table_enc:        %#x ", table_enc);
   11141                 :         84 :   print_encoding_base ("", table_enc);
   11142                 :            : 
   11143                 :         84 :   uint64_t eh_frame_ptr = 0;
   11144         [ +  - ]:         84 :   if (eh_frame_ptr_enc != DW_EH_PE_omit)
   11145                 :            :     {
   11146                 :         84 :       readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
   11147                 :            :                             dbg);
   11148         [ -  + ]:         84 :       if (unlikely (readp == NULL))
   11149                 :          0 :         goto invalid_data;
   11150                 :            : 
   11151                 :         84 :       printf (" eh_frame_ptr:     %#" PRIx64, eh_frame_ptr);
   11152         [ +  - ]:         84 :       if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
   11153                 :        168 :         printf (" (offset: %#" PRIx64 ")",
   11154                 :            :                 /* +4 because of the 4 byte header of the section.  */
   11155                 :         84 :                 (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
   11156                 :            : 
   11157         [ -  + ]:         84 :       putchar_unlocked ('\n');
   11158                 :            :     }
   11159                 :            : 
   11160                 :         84 :   uint64_t fde_count = 0;
   11161         [ +  - ]:         84 :   if (fde_count_enc != DW_EH_PE_omit)
   11162                 :            :     {
   11163                 :         84 :       readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
   11164         [ -  + ]:         84 :       if (unlikely (readp == NULL))
   11165                 :          0 :         goto invalid_data;
   11166                 :            : 
   11167                 :         84 :       printf (" fde_count:        %" PRIu64 "\n", fde_count);
   11168                 :            :     }
   11169                 :            : 
   11170   [ +  -  +  - ]:         84 :   if (fde_count == 0 || table_enc == DW_EH_PE_omit)
   11171                 :            :     return;
   11172                 :            : 
   11173                 :         84 :   puts (" Table:");
   11174                 :            : 
   11175                 :            :   /* Optimize for the most common case.  */
   11176         [ +  - ]:         84 :   if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
   11177         [ +  + ]:      13020 :     while (fde_count > 0 && readp + 8 <= dataend)
   11178                 :            :       {
   11179         [ -  + ]:      12936 :         int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
   11180                 :      12936 :         uint64_t initial_offset = ((uint64_t) shdr->sh_offset
   11181                 :      12936 :                                    + (int64_t) initial_location);
   11182         [ -  + ]:      12936 :         int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
   11183                 :            :         // XXX Possibly print symbol name or section offset for initial_offset
   11184         [ +  - ]:      13020 :         printf ("  %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
   11185                 :            :                 " fde=[%6" PRIx64 "]\n",
   11186                 :            :                 initial_location, initial_offset,
   11187                 :      12936 :                 address, address - (eh_frame_ptr + 4));
   11188                 :            :       }
   11189                 :            :   else
   11190                 :         84 :     while (0 && readp < dataend)
   11191                 :            :       {
   11192                 :            : 
   11193                 :         84 :       }
   11194                 :            : }
   11195                 :            : 
   11196                 :            : 
   11197                 :            : /* Print the content of the exception handling table section
   11198                 :            :    '.eh_frame_hdr'.  */
   11199                 :            : static void
   11200                 :          0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
   11201                 :            :                              Ebl *ebl __attribute__ ((unused)),
   11202                 :            :                              GElf_Ehdr *ehdr __attribute__ ((unused)),
   11203                 :            :                              Elf_Scn *scn,
   11204                 :            :                              GElf_Shdr *shdr __attribute__ ((unused)),
   11205                 :            :                              Dwarf *dbg __attribute__ ((unused)))
   11206                 :            : {
   11207                 :          0 :   printf (_("\
   11208                 :            : \nException handling table section [%2zu] '.gcc_except_table':\n"),
   11209                 :            :           elf_ndxscn (scn));
   11210                 :            : 
   11211                 :          0 :   Elf_Data *data = elf_rawdata (scn, NULL);
   11212                 :            : 
   11213         [ #  # ]:          0 :   if (unlikely (data == NULL))
   11214                 :            :     {
   11215                 :          0 :       error (0, 0, _("cannot get %s content: %s"),
   11216                 :            :              ".gcc_except_table", elf_errmsg (-1));
   11217                 :          0 :       return;
   11218                 :            :     }
   11219                 :            : 
   11220                 :          0 :   const unsigned char *readp = data->d_buf;
   11221                 :          0 :   const unsigned char *const dataend = readp + data->d_size;
   11222                 :            : 
   11223         [ #  # ]:          0 :   if (unlikely (readp + 1 > dataend))
   11224                 :            :     {
   11225                 :          0 :     invalid_data:
   11226                 :          0 :       error (0, 0, _("invalid data"));
   11227                 :          0 :       return;
   11228                 :            :     }
   11229                 :          0 :   unsigned int lpstart_encoding = *readp++;
   11230                 :          0 :   printf (_(" LPStart encoding:    %#x "), lpstart_encoding);
   11231                 :          0 :   print_encoding_base ("", lpstart_encoding);
   11232         [ #  # ]:          0 :   if (lpstart_encoding != DW_EH_PE_omit)
   11233                 :            :     {
   11234                 :          0 :       uint64_t lpstart;
   11235                 :          0 :       readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
   11236                 :          0 :       printf (" LPStart:             %#" PRIx64 "\n", lpstart);
   11237                 :            :     }
   11238                 :            : 
   11239         [ #  # ]:          0 :   if (unlikely (readp + 1 > dataend))
   11240                 :          0 :     goto invalid_data;
   11241                 :          0 :   unsigned int ttype_encoding = *readp++;
   11242                 :          0 :   printf (_(" TType encoding:      %#x "), ttype_encoding);
   11243                 :          0 :   print_encoding_base ("", ttype_encoding);
   11244                 :          0 :   const unsigned char *ttype_base = NULL;
   11245         [ #  # ]:          0 :   if (ttype_encoding != DW_EH_PE_omit)
   11246                 :            :     {
   11247                 :          0 :       unsigned int ttype_base_offset;
   11248         [ #  # ]:          0 :       if (readp >= dataend)
   11249                 :          0 :         goto invalid_data;
   11250                 :          0 :       get_uleb128 (ttype_base_offset, readp, dataend);
   11251                 :          0 :       printf (" TType base offset:   %#x\n", ttype_base_offset);
   11252         [ #  # ]:          0 :       if ((size_t) (dataend - readp) > ttype_base_offset)
   11253                 :          0 :         ttype_base = readp + ttype_base_offset;
   11254                 :            :     }
   11255                 :            : 
   11256         [ #  # ]:          0 :   if (unlikely (readp + 1 > dataend))
   11257                 :          0 :     goto invalid_data;
   11258                 :          0 :   unsigned int call_site_encoding = *readp++;
   11259                 :          0 :   printf (_(" Call site encoding:  %#x "), call_site_encoding);
   11260                 :          0 :   print_encoding_base ("", call_site_encoding);
   11261                 :          0 :   unsigned int call_site_table_len;
   11262         [ #  # ]:          0 :   if (readp >= dataend)
   11263                 :          0 :     goto invalid_data;
   11264                 :          0 :   get_uleb128 (call_site_table_len, readp, dataend);
   11265                 :            : 
   11266                 :          0 :   const unsigned char *const action_table = readp + call_site_table_len;
   11267         [ #  # ]:          0 :   if (unlikely (action_table > dataend))
   11268                 :          0 :     goto invalid_data;
   11269                 :            :   unsigned int u = 0;
   11270                 :            :   unsigned int max_action = 0;
   11271         [ #  # ]:          0 :   while (readp < action_table)
   11272                 :            :     {
   11273         [ #  # ]:          0 :       if (u == 0)
   11274                 :          0 :         puts (_("\n Call site table:"));
   11275                 :            : 
   11276                 :          0 :       uint64_t call_site_start;
   11277                 :          0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   11278                 :            :                             &call_site_start, dbg);
   11279                 :          0 :       uint64_t call_site_length;
   11280                 :          0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   11281                 :            :                             &call_site_length, dbg);
   11282                 :          0 :       uint64_t landing_pad;
   11283                 :          0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   11284                 :            :                             &landing_pad, dbg);
   11285                 :          0 :       unsigned int action;
   11286         [ #  # ]:          0 :       if (readp >= dataend)
   11287                 :          0 :         goto invalid_data;
   11288                 :          0 :       get_uleb128 (action, readp, dataend);
   11289                 :          0 :       max_action = MAX (action, max_action);
   11290                 :          0 :       printf (_(" [%4u] Call site start:   %#" PRIx64 "\n"
   11291                 :            :                        "        Call site length:  %" PRIu64 "\n"
   11292                 :            :                        "        Landing pad:       %#" PRIx64 "\n"
   11293                 :            :                        "        Action:            %u\n"),
   11294                 :            :               u++, call_site_start, call_site_length, landing_pad, action);
   11295                 :            :     }
   11296         [ #  # ]:          0 :   if (readp != action_table)
   11297                 :          0 :     goto invalid_data;
   11298                 :            : 
   11299                 :          0 :   unsigned int max_ar_filter = 0;
   11300         [ #  # ]:          0 :   if (max_action > 0)
   11301                 :            :     {
   11302                 :          0 :       puts ("\n Action table:");
   11303                 :            : 
   11304                 :          0 :       size_t maxdata = (size_t) (dataend - action_table);
   11305   [ #  #  #  # ]:          0 :       if (max_action > maxdata || maxdata - max_action < 1)
   11306                 :            :         {
   11307                 :          0 :         invalid_action_table:
   11308                 :          0 :           fputs (_("   <INVALID DATA>\n"), stdout);
   11309                 :          0 :           return;
   11310                 :            :         }
   11311                 :            : 
   11312                 :          0 :       const unsigned char *const action_table_end
   11313                 :          0 :         = action_table + max_action + 1;
   11314                 :            : 
   11315                 :          0 :       u = 0;
   11316                 :          0 :       do
   11317                 :            :         {
   11318                 :          0 :           int ar_filter;
   11319                 :          0 :           get_sleb128 (ar_filter, readp, action_table_end);
   11320         [ #  # ]:          0 :           if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
   11321                 :          0 :             max_ar_filter = ar_filter;
   11322                 :          0 :           int ar_disp;
   11323         [ #  # ]:          0 :           if (readp >= action_table_end)
   11324                 :          0 :             goto invalid_action_table;
   11325                 :          0 :           get_sleb128 (ar_disp, readp, action_table_end);
   11326                 :            : 
   11327                 :          0 :           printf (" [%4u] ar_filter:  % d\n"
   11328                 :            :                   "        ar_disp:    % -5d",
   11329                 :            :                   u, ar_filter, ar_disp);
   11330         [ #  # ]:          0 :           if (abs (ar_disp) & 1)
   11331                 :          0 :             printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
   11332         [ #  # ]:          0 :           else if (ar_disp != 0)
   11333                 :          0 :             puts (" -> ???");
   11334                 :            :           else
   11335         [ #  # ]:          0 :             putchar_unlocked ('\n');
   11336                 :          0 :           ++u;
   11337                 :            :         }
   11338         [ #  # ]:          0 :       while (readp < action_table_end);
   11339                 :            :     }
   11340                 :            : 
   11341         [ #  # ]:          0 :   if (max_ar_filter > 0 && ttype_base != NULL)
   11342                 :            :     {
   11343                 :          0 :       unsigned char dsize;
   11344                 :          0 :       puts ("\n TType table:");
   11345                 :            : 
   11346                 :            :       // XXX Not *4, size of encoding;
   11347         [ #  # ]:          0 :       switch (ttype_encoding & 7)
   11348                 :            :         {
   11349                 :            :         case DW_EH_PE_udata2:
   11350                 :            :         case DW_EH_PE_sdata2:
   11351                 :            :           dsize = 2;
   11352                 :            :           break;
   11353                 :            :         case DW_EH_PE_udata4:
   11354                 :            :         case DW_EH_PE_sdata4:
   11355                 :            :           dsize = 4;
   11356                 :            :           break;
   11357                 :            :         case DW_EH_PE_udata8:
   11358                 :            :         case DW_EH_PE_sdata8:
   11359                 :            :           dsize = 8;
   11360                 :            :           break;
   11361                 :          0 :         default:
   11362                 :          0 :           dsize = 0;
   11363                 :          0 :           error (1, 0, _("invalid TType encoding"));
   11364                 :            :         }
   11365                 :            : 
   11366                 :          0 :       if (max_ar_filter
   11367         [ #  # ]:          0 :           > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
   11368                 :          0 :         goto invalid_data;
   11369                 :            : 
   11370                 :          0 :       readp = ttype_base - max_ar_filter * dsize;
   11371                 :          0 :       do
   11372                 :            :         {
   11373                 :          0 :           uint64_t ttype;
   11374                 :          0 :           readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
   11375                 :            :                                 dbg);
   11376                 :          0 :           printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
   11377                 :            :         }
   11378         [ #  # ]:          0 :       while (readp < ttype_base);
   11379                 :            :     }
   11380                 :            : }
   11381                 :            : 
   11382                 :            : /* Print the content of the '.gdb_index' section.
   11383                 :            :    http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
   11384                 :            : */
   11385                 :            : static void
   11386                 :          2 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
   11387                 :            :                          GElf_Ehdr *ehdr __attribute__ ((unused)),
   11388                 :            :                          Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
   11389                 :            : {
   11390                 :          2 :   printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
   11391                 :            :                    " contains %" PRId64 " bytes :\n"),
   11392                 :            :           elf_ndxscn (scn), section_name (ebl, shdr),
   11393                 :          2 :           (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
   11394                 :            : 
   11395                 :          2 :   Elf_Data *data = elf_rawdata (scn, NULL);
   11396                 :            : 
   11397         [ -  + ]:          2 :   if (unlikely (data == NULL))
   11398                 :            :     {
   11399                 :          0 :       error (0, 0, _("cannot get %s content: %s"),
   11400                 :            :              ".gdb_index", elf_errmsg (-1));
   11401                 :          0 :       return;
   11402                 :            :     }
   11403                 :            : 
   11404                 :            :   // .gdb_index is always in little endian.
   11405                 :          2 :   Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
   11406                 :          2 :   dbg = &dummy_dbg;
   11407                 :            : 
   11408                 :          2 :   const unsigned char *readp = data->d_buf;
   11409                 :          2 :   const unsigned char *const dataend = readp + data->d_size;
   11410                 :            : 
   11411         [ -  + ]:          2 :   if (unlikely (readp + 4 > dataend))
   11412                 :            :     {
   11413                 :          0 :     invalid_data:
   11414                 :          0 :       error (0, 0, _("invalid data"));
   11415                 :          0 :       return;
   11416                 :            :     }
   11417                 :            : 
   11418                 :          2 :   int32_t vers = read_4ubyte_unaligned (dbg, readp);
   11419                 :          2 :   printf (_(" Version:         %" PRId32 "\n"), vers);
   11420                 :            : 
   11421                 :            :   // The only difference between version 4 and version 5 is the
   11422                 :            :   // hash used for generating the table.  Version 6 contains symbols
   11423                 :            :   // for inlined functions, older versions didn't.  Version 7 adds
   11424                 :            :   // symbol kinds.  Version 8 just indicates that it correctly includes
   11425                 :            :   // TUs for symbols.
   11426         [ -  + ]:          2 :   if (vers < 4 || vers > 8)
   11427                 :            :     {
   11428                 :          0 :       printf (_("  unknown version, cannot parse section\n"));
   11429                 :          0 :       return;
   11430                 :            :     }
   11431                 :            : 
   11432                 :          2 :   readp += 4;
   11433         [ -  + ]:          2 :   if (unlikely (readp + 4 > dataend))
   11434                 :          0 :     goto invalid_data;
   11435                 :            : 
   11436                 :          2 :   uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
   11437                 :          2 :   printf (_(" CU offset:       %#" PRIx32 "\n"), cu_off);
   11438                 :            : 
   11439                 :          2 :   readp += 4;
   11440         [ -  + ]:          2 :   if (unlikely (readp + 4 > dataend))
   11441                 :          0 :     goto invalid_data;
   11442                 :            : 
   11443                 :          2 :   uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
   11444                 :          2 :   printf (_(" TU offset:       %#" PRIx32 "\n"), tu_off);
   11445                 :            : 
   11446                 :          2 :   readp += 4;
   11447         [ -  + ]:          2 :   if (unlikely (readp + 4 > dataend))
   11448                 :          0 :     goto invalid_data;
   11449                 :            : 
   11450                 :          2 :   uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
   11451                 :          2 :   printf (_(" address offset:  %#" PRIx32 "\n"), addr_off);
   11452                 :            : 
   11453                 :          2 :   readp += 4;
   11454         [ -  + ]:          2 :   if (unlikely (readp + 4 > dataend))
   11455                 :          0 :     goto invalid_data;
   11456                 :            : 
   11457                 :          2 :   uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
   11458                 :          2 :   printf (_(" symbol offset:   %#" PRIx32 "\n"), sym_off);
   11459                 :            : 
   11460                 :          2 :   readp += 4;
   11461         [ -  + ]:          2 :   if (unlikely (readp + 4 > dataend))
   11462                 :          0 :     goto invalid_data;
   11463                 :            : 
   11464                 :          2 :   uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
   11465                 :          2 :   printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
   11466                 :            : 
   11467         [ -  + ]:          2 :   if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
   11468                 :            :                 < const_off))
   11469                 :          0 :     goto invalid_data;
   11470                 :            : 
   11471                 :          2 :   readp = data->d_buf + cu_off;
   11472                 :            : 
   11473                 :          2 :   const unsigned char *nextp = data->d_buf + tu_off;
   11474         [ -  + ]:          2 :   if (tu_off >= data->d_size)
   11475                 :          0 :     goto invalid_data;
   11476                 :            : 
   11477                 :          2 :   size_t cu_nr = (nextp - readp) / 16;
   11478                 :            : 
   11479                 :          2 :   printf (_("\n CU list at offset %#" PRIx32
   11480                 :            :                    " contains %zu entries:\n"),
   11481                 :            :           cu_off, cu_nr);
   11482                 :            : 
   11483                 :          2 :   size_t n = 0;
   11484   [ +  -  +  + ]:          6 :   while (dataend - readp >= 16 && n < cu_nr)
   11485                 :            :     {
   11486                 :          4 :       uint64_t off = read_8ubyte_unaligned (dbg, readp);
   11487                 :          4 :       readp += 8;
   11488                 :            : 
   11489                 :          4 :       uint64_t len = read_8ubyte_unaligned (dbg, readp);
   11490                 :          4 :       readp += 8;
   11491                 :            : 
   11492                 :          4 :       printf (" [%4zu] start: %0#8" PRIx64
   11493                 :            :               ", length: %5" PRIu64 "\n", n, off, len);
   11494                 :          4 :       n++;
   11495                 :            :     }
   11496                 :            : 
   11497                 :          2 :   readp = data->d_buf + tu_off;
   11498                 :          2 :   nextp = data->d_buf + addr_off;
   11499         [ -  + ]:          2 :   if (addr_off >= data->d_size)
   11500                 :          0 :     goto invalid_data;
   11501                 :            : 
   11502                 :          2 :   size_t tu_nr = (nextp - readp) / 24;
   11503                 :            : 
   11504                 :          2 :   printf (_("\n TU list at offset %#" PRIx32
   11505                 :            :                    " contains %zu entries:\n"),
   11506                 :            :           tu_off, tu_nr);
   11507                 :            : 
   11508                 :          2 :   n = 0;
   11509   [ +  -  +  + ]:          4 :   while (dataend - readp >= 24 && n < tu_nr)
   11510                 :            :     {
   11511                 :          2 :       uint64_t off = read_8ubyte_unaligned (dbg, readp);
   11512                 :          2 :       readp += 8;
   11513                 :            : 
   11514                 :          2 :       uint64_t type = read_8ubyte_unaligned (dbg, readp);
   11515                 :          2 :       readp += 8;
   11516                 :            : 
   11517                 :          2 :       uint64_t sig = read_8ubyte_unaligned (dbg, readp);
   11518                 :          2 :       readp += 8;
   11519                 :            : 
   11520                 :          2 :       printf (" [%4zu] CU offset: %5" PRId64
   11521                 :            :               ", type offset: %5" PRId64
   11522                 :            :               ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
   11523                 :          2 :       n++;
   11524                 :            :     }
   11525                 :            : 
   11526                 :          2 :   readp = data->d_buf + addr_off;
   11527                 :          2 :   nextp = data->d_buf + sym_off;
   11528         [ -  + ]:          2 :   if (sym_off >= data->d_size)
   11529                 :          0 :     goto invalid_data;
   11530                 :            : 
   11531                 :          2 :   size_t addr_nr = (nextp - readp) / 20;
   11532                 :            : 
   11533                 :          2 :   printf (_("\n Address list at offset %#" PRIx32
   11534                 :            :                    " contains %zu entries:\n"),
   11535                 :            :           addr_off, addr_nr);
   11536                 :            : 
   11537                 :          2 :   n = 0;
   11538   [ +  -  +  + ]:          6 :   while (dataend - readp >= 20 && n < addr_nr)
   11539                 :            :     {
   11540                 :          4 :       uint64_t low = read_8ubyte_unaligned (dbg, readp);
   11541                 :          4 :       readp += 8;
   11542                 :            : 
   11543                 :          4 :       uint64_t high = read_8ubyte_unaligned (dbg, readp);
   11544                 :          4 :       readp += 8;
   11545                 :            : 
   11546                 :          4 :       uint32_t idx = read_4ubyte_unaligned (dbg, readp);
   11547                 :          4 :       readp += 4;
   11548                 :            : 
   11549                 :          4 :       printf (" [%4zu] ", n);
   11550                 :          4 :       print_dwarf_addr (dwflmod, 8, low, low);
   11551                 :          4 :       printf ("..");
   11552                 :          4 :       print_dwarf_addr (dwflmod, 8, high - 1, high);
   11553                 :          4 :       printf (", CU index: %5" PRId32 "\n", idx);
   11554                 :          4 :       n++;
   11555                 :            :     }
   11556                 :            : 
   11557                 :          2 :   const unsigned char *const_start = data->d_buf + const_off;
   11558         [ -  + ]:          2 :   if (const_off >= data->d_size)
   11559                 :          0 :     goto invalid_data;
   11560                 :            : 
   11561                 :          2 :   readp = data->d_buf + sym_off;
   11562                 :          2 :   nextp = const_start;
   11563                 :          2 :   size_t sym_nr = (nextp - readp) / 8;
   11564                 :            : 
   11565                 :          2 :   printf (_("\n Symbol table at offset %#" PRIx32
   11566                 :            :                    " contains %zu slots:\n"),
   11567                 :            :           addr_off, sym_nr);
   11568                 :            : 
   11569                 :          2 :   n = 0;
   11570   [ +  -  +  + ]:       2050 :   while (dataend - readp >= 8 && n < sym_nr)
   11571                 :            :     {
   11572                 :       2048 :       uint32_t name = read_4ubyte_unaligned (dbg, readp);
   11573                 :       2048 :       readp += 4;
   11574                 :            : 
   11575                 :       2048 :       uint32_t vector = read_4ubyte_unaligned (dbg, readp);
   11576                 :       2048 :       readp += 4;
   11577                 :            : 
   11578         [ +  + ]:       2048 :       if (name != 0 || vector != 0)
   11579                 :            :         {
   11580                 :         14 :           const unsigned char *sym = const_start + name;
   11581   [ +  -  -  + ]:         14 :           if (unlikely ((size_t) (dataend - const_start) < name
   11582                 :            :                         || memchr (sym, '\0', dataend - sym) == NULL))
   11583                 :          0 :             goto invalid_data;
   11584                 :            : 
   11585                 :         14 :           printf (" [%4zu] symbol: %s, CUs: ", n, sym);
   11586                 :            : 
   11587                 :         14 :           const unsigned char *readcus = const_start + vector;
   11588         [ -  + ]:         14 :           if (unlikely ((size_t) (dataend - const_start) < vector))
   11589                 :          0 :             goto invalid_data;
   11590                 :         14 :           uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
   11591                 :         14 :           while (cus--)
   11592                 :            :             {
   11593                 :         16 :               uint32_t cu_kind, cu, kind;
   11594                 :         16 :               bool is_static;
   11595                 :         16 :               readcus += 4;
   11596         [ -  + ]:         16 :               if (unlikely (readcus + 4 > dataend))
   11597                 :          0 :                 goto invalid_data;
   11598                 :         16 :               cu_kind = read_4ubyte_unaligned (dbg, readcus);
   11599                 :         16 :               cu = cu_kind & ((1 << 24) - 1);
   11600                 :         16 :               kind = (cu_kind >> 28) & 7;
   11601                 :         16 :               is_static = cu_kind & (1U << 31);
   11602         [ +  + ]:         16 :               if (cu > cu_nr - 1)
   11603                 :          2 :                 printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
   11604                 :            :               else
   11605                 :         14 :                 printf ("%" PRId32, cu);
   11606         [ +  + ]:         16 :               if (kind != 0)
   11607                 :            :                 {
   11608                 :          8 :                   printf (" (");
   11609   [ +  +  +  -  :          8 :                   switch (kind)
                      - ]
   11610                 :            :                     {
   11611                 :            :                     case 1:
   11612                 :          3 :                       printf ("type");
   11613                 :            :                       break;
   11614                 :            :                     case 2:
   11615                 :          2 :                       printf ("var");
   11616                 :            :                       break;
   11617                 :            :                     case 3:
   11618                 :          3 :                       printf ("func");
   11619                 :            :                       break;
   11620                 :            :                     case 4:
   11621                 :          0 :                       printf ("other");
   11622                 :            :                       break;
   11623                 :            :                     default:
   11624                 :          0 :                       printf ("unknown-0x%" PRIx32, kind);
   11625                 :            :                       break;
   11626                 :            :                     }
   11627         [ +  + ]:          8 :                   printf (":%c)", (is_static ? 'S' : 'G'));
   11628                 :            :                 }
   11629         [ +  + ]:         16 :               if (cus > 0)
   11630         [ +  + ]:         32 :                 printf (", ");
   11631                 :            :             }
   11632                 :         14 :           printf ("\n");
   11633                 :            :         }
   11634                 :       2048 :       n++;
   11635                 :            :     }
   11636                 :            : }
   11637                 :            : 
   11638                 :            : /* Returns true and sets split DWARF CU id if there is a split compile
   11639                 :            :    unit in the given Dwarf, and no non-split units are found (before it).  */
   11640                 :            : static bool
   11641                 :        196 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
   11642                 :            : {
   11643                 :        196 :   Dwarf_CU *cu = NULL;
   11644         [ +  + ]:        196 :   while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
   11645                 :            :     {
   11646                 :        195 :       uint8_t unit_type;
   11647         [ +  - ]:        195 :       if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
   11648                 :            :                          id, NULL, NULL) != 0)
   11649                 :        195 :         return false;
   11650                 :            : 
   11651         [ +  + ]:        195 :       if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
   11652                 :            :         return false;
   11653                 :            : 
   11654                 :            :       /* We really only care about the split compile unit, the types
   11655                 :            :          should be fine and self sufficient.  Also they don't have an
   11656                 :            :          id that we can match with a skeleton unit.  */
   11657         [ +  - ]:          9 :       if (unit_type == DW_UT_split_compile)
   11658                 :            :         {
   11659                 :          9 :           *split_cu = cu;
   11660                 :          9 :           return true;
   11661                 :            :         }
   11662                 :            :     }
   11663                 :            : 
   11664                 :            :   return false;
   11665                 :            : }
   11666                 :            : 
   11667                 :            : /* Check that there is one and only one Dwfl_Module, return in arg.  */
   11668                 :            : static int
   11669                 :          9 : getone_dwflmod (Dwfl_Module *dwflmod,
   11670                 :            :                void **userdata __attribute__ ((unused)),
   11671                 :            :                const char *name __attribute__ ((unused)),
   11672                 :            :                Dwarf_Addr base __attribute__ ((unused)),
   11673                 :            :                void *arg)
   11674                 :            : {
   11675                 :          9 :   Dwfl_Module **m = (Dwfl_Module **) arg;
   11676         [ +  - ]:          9 :   if (*m != NULL)
   11677                 :            :     return DWARF_CB_ABORT;
   11678                 :          9 :   *m = dwflmod;
   11679                 :          9 :   return DWARF_CB_OK;
   11680                 :            : }
   11681                 :            : 
   11682                 :            : static void
   11683                 :        293 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
   11684                 :            : {
   11685                 :            :   /* Used for skeleton file, if necessary for split DWARF.  */
   11686                 :        293 :   Dwfl *skel_dwfl = NULL;
   11687                 :        293 :   Dwfl_Module *skel_mod = NULL;
   11688                 :        293 :   char *skel_name = NULL;
   11689                 :        293 :   Dwarf *split_dbg = NULL;
   11690                 :        293 :   Dwarf_CU *split_cu = NULL;
   11691                 :            : 
   11692                 :            :   /* Before we start the real work get a debug context descriptor.  */
   11693                 :        293 :   Dwarf_Addr dwbias;
   11694                 :        293 :   Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
   11695                 :        293 :   Dwarf dummy_dbg =
   11696                 :            :     {
   11697                 :        293 :       .elf = ebl->elf,
   11698                 :        293 :       .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
   11699                 :            :     };
   11700         [ +  + ]:        293 :   if (dbg == NULL)
   11701                 :            :     {
   11702         [ -  + ]:         97 :       if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
   11703                 :          0 :         error (0, 0, _("cannot get debug context descriptor: %s"),
   11704                 :            :                dwfl_errmsg (-1));
   11705                 :            :       dbg = &dummy_dbg;
   11706                 :            :     }
   11707                 :            :   else
   11708                 :            :     {
   11709                 :            :       /* If we are asked about a split dwarf (.dwo) file, use the user
   11710                 :            :          provided, or find the corresponding skeleton file. If we got
   11711                 :            :          a skeleton file, replace the given dwflmod and dbg, with one
   11712                 :            :          derived from the skeleton file to provide enough context.  */
   11713                 :        196 :       uint64_t split_id;
   11714         [ +  + ]:        196 :       if (is_split_dwarf (dbg, &split_id, &split_cu))
   11715                 :            :         {
   11716         [ +  - ]:          9 :           if (dwarf_skeleton != NULL)
   11717                 :          9 :             skel_name = strdup (dwarf_skeleton);
   11718                 :            :           else
   11719                 :            :             {
   11720                 :            :               /* Replace file.dwo with file.o and see if that matches. */
   11721                 :          0 :               const char *fname;
   11722                 :          0 :               dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
   11723                 :            :                                 &fname, NULL);
   11724         [ #  # ]:          0 :               if (fname != NULL)
   11725                 :            :                 {
   11726                 :          0 :                   size_t flen = strlen (fname);
   11727   [ #  #  #  # ]:          0 :                   if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
   11728                 :            :                     {
   11729                 :          0 :                       skel_name = strdup (fname);
   11730         [ #  # ]:          0 :                       if (skel_name != NULL)
   11731                 :            :                         {
   11732                 :          0 :                           skel_name[flen - 3] = 'o';
   11733                 :          0 :                           skel_name[flen - 2] = '\0';
   11734                 :            :                         }
   11735                 :            :                     }
   11736                 :            :                 }
   11737                 :            :             }
   11738                 :            : 
   11739         [ +  - ]:          9 :           if (skel_name != NULL)
   11740                 :            :             {
   11741                 :          9 :               int skel_fd = open (skel_name, O_RDONLY);
   11742         [ -  + ]:          9 :               if (skel_fd == -1)
   11743                 :          0 :                 fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
   11744                 :            :                          " '%s'\n", skel_name);
   11745                 :            :               else
   11746                 :          9 :                 skel_dwfl = create_dwfl (skel_fd, skel_name);
   11747                 :            : 
   11748         [ +  - ]:          9 :               if (skel_dwfl != NULL)
   11749                 :            :                 {
   11750         [ -  + ]:          9 :                   if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
   11751                 :            :                                        &skel_mod, 0) != 0)
   11752                 :            :                     {
   11753                 :          0 :                       fprintf (stderr, "Warning: Bad DWARF skeleton,"
   11754                 :            :                                " multiple modules '%s'\n", skel_name);
   11755                 :          0 :                       dwfl_end (skel_dwfl);
   11756                 :          0 :                       skel_mod = NULL;
   11757                 :            :                     }
   11758                 :            :                 }
   11759         [ #  # ]:          0 :               else if (skel_fd != -1)
   11760                 :          0 :                 fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
   11761                 :            :                          " '%s': %s\n", skel_name, dwfl_errmsg (-1));
   11762                 :            : 
   11763         [ +  - ]:          9 :               if (skel_mod != NULL)
   11764                 :            :                 {
   11765                 :          9 :                   Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
   11766         [ +  - ]:          9 :                   if (skel_dbg != NULL)
   11767                 :            :                     {
   11768                 :            :                       /* First check the skeleton CU DIE, only fetch
   11769                 :            :                          the split DIE if we know the id matches to
   11770                 :            :                          not unnecessary search for any split DIEs we
   11771                 :            :                          don't need. */
   11772                 :          9 :                       Dwarf_CU *cu = NULL;
   11773         [ +  - ]:         13 :                       while (dwarf_get_units (skel_dbg, cu, &cu,
   11774                 :            :                                               NULL, NULL, NULL, NULL) == 0)
   11775                 :            :                         {
   11776                 :         13 :                           uint8_t unit_type;
   11777                 :         13 :                           uint64_t skel_id;
   11778         [ +  - ]:         13 :                           if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
   11779                 :            :                                              &skel_id, NULL, NULL) == 0
   11780         [ +  - ]:         13 :                               && unit_type == DW_UT_skeleton
   11781         [ +  + ]:         13 :                               && split_id == skel_id)
   11782                 :            :                             {
   11783                 :          9 :                               Dwarf_Die subdie;
   11784         [ +  - ]:          9 :                               if (dwarf_cu_info (cu, NULL, NULL, NULL,
   11785                 :            :                                                  &subdie,
   11786                 :            :                                                  NULL, NULL, NULL) == 0
   11787         [ +  - ]:          9 :                                   && dwarf_tag (&subdie) != DW_TAG_invalid)
   11788                 :            :                                 {
   11789                 :          9 :                                   split_dbg = dwarf_cu_getdwarf (subdie.cu);
   11790         [ -  + ]:          9 :                                   if (split_dbg == NULL)
   11791                 :          0 :                                     fprintf (stderr,
   11792                 :            :                                              "Warning: Couldn't get split_dbg:"
   11793                 :            :                                              " %s\n", dwarf_errmsg (-1));
   11794                 :          9 :                                   break;
   11795                 :            :                                 }
   11796                 :            :                               else
   11797                 :            :                                 {
   11798                 :            :                                   /* Everything matches up, but not
   11799                 :            :                                      according to libdw. Which means
   11800                 :            :                                      the user knew better.  So...
   11801                 :            :                                      Terrible hack... We can never
   11802                 :            :                                      destroy the underlying dwfl
   11803                 :            :                                      because it would free the wrong
   11804                 :            :                                      Dwarfs... So we leak memory...*/
   11805         [ #  # ]:          0 :                                   if (cu->split == NULL
   11806         [ #  # ]:          0 :                                       && dwarf_skeleton != NULL)
   11807                 :            :                                     {
   11808                 :          0 :                                       do_not_close_dwfl = true;
   11809                 :          0 :                                       __libdw_link_skel_split (cu, split_cu);
   11810                 :          0 :                                       split_dbg = dwarf_cu_getdwarf (split_cu);
   11811                 :          0 :                                       break;
   11812                 :            :                                     }
   11813                 :            :                                   else
   11814                 :          0 :                                     fprintf (stderr, "Warning: Couldn't get"
   11815                 :            :                                              " skeleton subdie: %s\n",
   11816                 :            :                                              dwarf_errmsg (-1));
   11817                 :            :                                 }
   11818                 :            :                             }
   11819                 :            :                         }
   11820         [ -  + ]:          9 :                       if (split_dbg == NULL)
   11821                 :          9 :                         fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
   11822                 :            :                     }
   11823                 :            :                   else
   11824                 :          0 :                     fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
   11825                 :            :                              " %s\n", dwfl_errmsg (-1));
   11826                 :            :                 }
   11827                 :            :             }
   11828                 :            : 
   11829         [ +  - ]:          9 :           if (split_dbg != NULL)
   11830                 :            :             {
   11831                 :          9 :               dbg = split_dbg;
   11832                 :          9 :               dwflmod = skel_mod;
   11833                 :            :             }
   11834         [ #  # ]:          0 :           else if (skel_name == NULL)
   11835                 :          0 :             fprintf (stderr,
   11836                 :            :                      "Warning: split DWARF file, but no skeleton found.\n");
   11837                 :            :         }
   11838         [ -  + ]:        187 :       else if (dwarf_skeleton != NULL)
   11839                 :        196 :         fprintf (stderr, "Warning: DWARF skeleton given,"
   11840                 :            :                  " but not a split DWARF file\n");
   11841                 :            :     }
   11842                 :            : 
   11843                 :            :   /* Get the section header string table index.  */
   11844                 :        293 :   size_t shstrndx;
   11845         [ -  + ]:        293 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
   11846                 :          0 :     error_exit (0, _("cannot get section header string table index"));
   11847                 :            : 
   11848                 :            :   /* If the .debug_info section is listed as implicitly required then
   11849                 :            :      we must make sure to handle it before handling any other debug
   11850                 :            :      section.  Various other sections depend on the CU DIEs being
   11851                 :            :      scanned (silently) first.  */
   11852                 :        293 :   bool implicit_info = (implicit_debug_sections & section_info) != 0;
   11853                 :        293 :   bool explicit_info = (print_debug_sections & section_info) != 0;
   11854         [ +  + ]:        293 :   if (implicit_info)
   11855                 :            :     {
   11856                 :            :       Elf_Scn *scn = NULL;
   11857         [ +  - ]:       1534 :       while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   11858                 :            :         {
   11859                 :       1534 :           GElf_Shdr shdr_mem;
   11860                 :       1534 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   11861                 :            : 
   11862   [ +  -  +  + ]:       1534 :           if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
   11863                 :            :             {
   11864                 :       1212 :               const char *name = elf_strptr (ebl->elf, shstrndx,
   11865                 :        606 :                                              shdr->sh_name);
   11866         [ -  + ]:        606 :               if (name == NULL)
   11867                 :          0 :                 continue;
   11868                 :            : 
   11869         [ +  + ]:        606 :               if (strcmp (name, ".debug_info") == 0
   11870         [ +  + ]:        542 :                   || strcmp (name, ".debug_info.dwo") == 0
   11871         [ +  + ]:        533 :                   || strcmp (name, ".zdebug_info") == 0
   11872         [ +  - ]:        527 :                   || strcmp (name, ".zdebug_info.dwo") == 0
   11873         [ +  + ]:        527 :                   || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
   11874                 :            :                 {
   11875                 :         80 :                   print_debug_info_section (dwflmod, ebl, ehdr,
   11876                 :            :                                             scn, shdr, dbg);
   11877                 :         80 :                   break;
   11878                 :            :                 }
   11879                 :            :             }
   11880                 :            :         }
   11881                 :         80 :       print_debug_sections &= ~section_info;
   11882                 :         80 :       implicit_debug_sections &= ~section_info;
   11883                 :            :     }
   11884                 :            : 
   11885                 :            :   /* Look through all the sections for the debugging sections to print.  */
   11886                 :            :   Elf_Scn *scn = NULL;
   11887         [ +  + ]:       9113 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   11888                 :            :     {
   11889                 :       8820 :       GElf_Shdr shdr_mem;
   11890                 :       8820 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   11891                 :            : 
   11892   [ +  -  +  + ]:       8820 :       if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
   11893                 :            :         {
   11894                 :       3747 :           static const struct
   11895                 :            :           {
   11896                 :            :             const char *name;
   11897                 :            :             enum section_e bitmask;
   11898                 :            :             void (*fp) (Dwfl_Module *, Ebl *,
   11899                 :            :                         GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
   11900                 :            :           } debug_sections[] =
   11901                 :            :             {
   11902                 :            : #define NEW_SECTION(name) \
   11903                 :            :               { ".debug_" #name, section_##name, print_debug_##name##_section }
   11904                 :            :               NEW_SECTION (abbrev),
   11905                 :            :               NEW_SECTION (addr),
   11906                 :            :               NEW_SECTION (aranges),
   11907                 :            :               NEW_SECTION (frame),
   11908                 :            :               NEW_SECTION (info),
   11909                 :            :               NEW_SECTION (types),
   11910                 :            :               NEW_SECTION (line),
   11911                 :            :               NEW_SECTION (loc),
   11912                 :            :               /* loclists is loc for DWARF5.  */
   11913                 :            :               { ".debug_loclists", section_loc,
   11914                 :            :                 print_debug_loclists_section },
   11915                 :            :               NEW_SECTION (pubnames),
   11916                 :            :               NEW_SECTION (str),
   11917                 :            :               /* A DWARF5 specialised debug string section.  */
   11918                 :            :               { ".debug_line_str", section_str,
   11919                 :            :                 print_debug_str_section },
   11920                 :            :               /* DWARF5 string offsets table.  */
   11921                 :            :               { ".debug_str_offsets", section_str,
   11922                 :            :                 print_debug_str_offsets_section },
   11923                 :            :               NEW_SECTION (macinfo),
   11924                 :            :               NEW_SECTION (macro),
   11925                 :            :               NEW_SECTION (ranges),
   11926                 :            :               /* rnglists is ranges for DWARF5.  */
   11927                 :            :               { ".debug_rnglists", section_ranges,
   11928                 :            :                 print_debug_rnglists_section },
   11929                 :            :               { ".eh_frame", section_frame | section_exception,
   11930                 :            :                 print_debug_frame_section },
   11931                 :            :               { ".eh_frame_hdr", section_frame | section_exception,
   11932                 :            :                 print_debug_frame_hdr_section },
   11933                 :            :               { ".gcc_except_table", section_frame | section_exception,
   11934                 :            :                 print_debug_exception_table },
   11935                 :            :               { ".gdb_index", section_gdb_index, print_gdb_index_section }
   11936                 :            :             };
   11937                 :       3747 :           const int ndebug_sections = (sizeof (debug_sections)
   11938                 :            :                                        / sizeof (debug_sections[0]));
   11939                 :       7494 :           const char *name = elf_strptr (ebl->elf, shstrndx,
   11940                 :       3747 :                                          shdr->sh_name);
   11941         [ -  + ]:       3747 :           if (name == NULL)
   11942                 :          0 :             continue;
   11943                 :            : 
   11944                 :            :           int n;
   11945         [ +  + ]:      61634 :           for (n = 0; n < ndebug_sections; ++n)
   11946                 :            :             {
   11947                 :      59520 :               size_t dbglen = strlen (debug_sections[n].name);
   11948                 :      59520 :               size_t scnlen = strlen (name);
   11949         [ +  + ]:      59520 :               if ((strncmp (name, debug_sections[n].name, dbglen) == 0
   11950         [ +  + ]:       1704 :                    && (dbglen == scnlen
   11951         [ +  + ]:        212 :                        || (scnlen == dbglen + 4
   11952         [ +  + ]:        194 :                            && strstr (name, ".dwo") == name + dbglen)))
   11953   [ +  +  +  + ]:      57974 :                   || (name[0] == '.' && name[1] == 'z'
   11954         [ +  - ]:        566 :                       && debug_sections[n].name[1] == 'd'
   11955         [ +  + ]:        566 :                       && strncmp (&name[2], &debug_sections[n].name[1],
   11956                 :            :                                   dbglen - 1) == 0
   11957         [ -  + ]:         82 :                       && (scnlen == dbglen + 1
   11958         [ #  # ]:          0 :                           || (scnlen == dbglen + 5
   11959         [ #  # ]:          0 :                               && strstr (name, ".dwo") == name + dbglen + 1)))
   11960         [ +  + ]:      57892 :                   || (scnlen > 14 /* .gnu.debuglto_ prefix. */
   11961         [ +  + ]:       4094 :                       && startswith (name, ".gnu.debuglto_")
   11962         [ +  + ]:         36 :                       && strcmp (&name[14], debug_sections[n].name) == 0)
   11963                 :            : )
   11964                 :            :                 {
   11965                 :       1633 :                   if ((print_debug_sections | implicit_debug_sections)
   11966         [ +  + ]:       1633 :                       & debug_sections[n].bitmask)
   11967                 :        551 :                     debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
   11968                 :            :                   break;
   11969                 :            :                 }
   11970                 :            :             }
   11971                 :            :         }
   11972                 :            :     }
   11973                 :            : 
   11974                 :        293 :   dwfl_end (skel_dwfl);
   11975                 :        293 :   free (skel_name);
   11976                 :            : 
   11977                 :            :   /* Turn implicit and/or explicit back on in case we go over another file.  */
   11978         [ +  + ]:        293 :   if (implicit_info)
   11979                 :         80 :     implicit_debug_sections |= section_info;
   11980         [ +  + ]:        293 :   if (explicit_info)
   11981                 :         64 :     print_debug_sections |= section_info;
   11982                 :            : 
   11983                 :        293 :   reset_listptr (&known_locsptr);
   11984                 :        293 :   reset_listptr (&known_loclistsptr);
   11985                 :        293 :   reset_listptr (&known_rangelistptr);
   11986                 :        293 :   reset_listptr (&known_rnglistptr);
   11987                 :        293 :   reset_listptr (&known_addrbases);
   11988                 :        293 :   reset_listptr (&known_stroffbases);
   11989                 :        293 : }
   11990                 :            : 
   11991                 :            : 
   11992                 :            : #define ITEM_INDENT             4
   11993                 :            : #define WRAP_COLUMN             75
   11994                 :            : 
   11995                 :            : /* Print "NAME: FORMAT", wrapping when output text would make the line
   11996                 :            :    exceed WRAP_COLUMN.  Unpadded numbers look better for the core items
   11997                 :            :    but this function is also used for registers which should be printed
   11998                 :            :    aligned.  Fortunately registers output uses fixed fields width (such
   11999                 :            :    as %11d) for the alignment.
   12000                 :            : 
   12001                 :            :    Line breaks should not depend on the particular values although that
   12002                 :            :    may happen in some cases of the core items.  */
   12003                 :            : 
   12004                 :            : static unsigned int
   12005                 :            : __attribute__ ((format (printf, 6, 7)))
   12006                 :        821 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
   12007                 :            :                  size_t name_width, const char *name, const char *format, ...)
   12008                 :            : {
   12009                 :        821 :   size_t len = strlen (name);
   12010                 :        821 :   if (name_width < len)
   12011                 :            :     name_width = len;
   12012                 :            : 
   12013                 :        821 :   char *out;
   12014                 :        821 :   va_list ap;
   12015                 :        821 :   va_start (ap, format);
   12016                 :        821 :   int out_len = vasprintf (&out, format, ap);
   12017                 :        821 :   va_end (ap);
   12018         [ -  + ]:        821 :   if (out_len == -1)
   12019                 :          0 :     error_exit (0, _("memory exhausted"));
   12020                 :            : 
   12021                 :        821 :   size_t n = name_width + sizeof ": " - 1 + out_len;
   12022                 :            : 
   12023         [ +  + ]:        821 :   if (colno == 0)
   12024                 :            :     {
   12025                 :         51 :       printf ("%*s", ITEM_INDENT, "");
   12026                 :         51 :       colno = ITEM_INDENT + n;
   12027                 :            :     }
   12028         [ +  + ]:        770 :   else if (colno + 2 + n < wrap)
   12029                 :            :     {
   12030                 :        468 :       printf ("%c ", sep);
   12031                 :        468 :       colno += 2 + n;
   12032                 :            :     }
   12033                 :            :   else
   12034                 :            :     {
   12035                 :        302 :       printf ("\n%*s", ITEM_INDENT, "");
   12036                 :        302 :       colno = ITEM_INDENT + n;
   12037                 :            :     }
   12038                 :            : 
   12039                 :        821 :   printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
   12040                 :            : 
   12041                 :        821 :   free (out);
   12042                 :            : 
   12043                 :        821 :   return colno;
   12044                 :            : }
   12045                 :            : 
   12046                 :            : static const void *
   12047                 :        931 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
   12048                 :            :          void *value, const void *data, size_t size)
   12049                 :            : {
   12050                 :       1862 :   Elf_Data valuedata =
   12051                 :            :     {
   12052                 :            :       .d_type = type,
   12053                 :            :       .d_buf = value,
   12054         [ +  + ]:        931 :       .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
   12055                 :            :       .d_version = EV_CURRENT,
   12056                 :            :     };
   12057                 :        931 :   Elf_Data indata =
   12058                 :            :     {
   12059                 :            :       .d_type = type,
   12060                 :            :       .d_buf = (void *) data,
   12061                 :            :       .d_size = valuedata.d_size,
   12062                 :            :       .d_version = EV_CURRENT,
   12063                 :            :     };
   12064                 :            : 
   12065                 :        931 :   Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
   12066         [ +  + ]:        931 :                  ? elf32_xlatetom : elf64_xlatetom)
   12067                 :        931 :     (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
   12068         [ -  + ]:        931 :   if (d == NULL)
   12069                 :          0 :     error_exit (0, _("cannot convert core note data: %s"),
   12070                 :            :                 elf_errmsg (-1));
   12071                 :            : 
   12072                 :        931 :   return data + indata.d_size;
   12073                 :            : }
   12074                 :            : 
   12075                 :            : typedef uint8_t GElf_Byte;
   12076                 :            : 
   12077                 :            : static unsigned int
   12078                 :        400 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
   12079                 :            :                   unsigned int colno, size_t *repeated_size)
   12080                 :            : {
   12081         [ +  + ]:        400 :   uint_fast16_t count = item->count ?: 1;
   12082                 :            :   /* Ebl_Core_Item count is always a small number.
   12083                 :            :      Make sure the backend didn't put in some large bogus value.  */
   12084         [ -  + ]:         62 :   assert (count < 128);
   12085                 :            : 
   12086                 :            : #define TYPES                                                                 \
   12087                 :            :   DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8);                         \
   12088                 :            :   DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16);                       \
   12089                 :            :   DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32);                       \
   12090                 :            :   DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32);                         \
   12091                 :            :   DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64);                            \
   12092                 :            :   DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
   12093                 :            : 
   12094                 :            : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
   12095                 :        400 :   typedef union { TYPES; } value_t;
   12096                 :        400 :   void *data = alloca (count * sizeof (value_t));
   12097                 :            : #undef DO_TYPE
   12098                 :            : 
   12099                 :            : #define DO_TYPE(NAME, Name, hex, dec) \
   12100                 :            :     GElf_##Name *value_##Name __attribute__((unused)) = data
   12101                 :        400 :   TYPES;
   12102                 :            : #undef DO_TYPE
   12103                 :            : 
   12104                 :        400 :   size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
   12105                 :        400 :   size_t convsize = size;
   12106         [ +  + ]:        400 :   if (repeated_size != NULL)
   12107                 :            :     {
   12108   [ +  -  -  + ]:          1 :       if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
   12109                 :            :         {
   12110                 :          0 :           data = alloca (*repeated_size);
   12111                 :          0 :           count *= *repeated_size / size;
   12112                 :          0 :           convsize = count * size;
   12113                 :          0 :           *repeated_size -= convsize;
   12114                 :            :         }
   12115   [ +  -  -  + ]:          1 :       else if (item->count != 0 || item->format != '\n')
   12116                 :          0 :         *repeated_size -= size;
   12117                 :            :     }
   12118                 :            : 
   12119                 :        400 :   convert (core, item->type, count, data, desc + item->offset, convsize);
   12120                 :            : 
   12121                 :        400 :   Elf_Type type = item->type;
   12122         [ +  + ]:        400 :   if (type == ELF_T_ADDR)
   12123         [ +  - ]:          1 :     type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
   12124                 :            : 
   12125   [ +  +  +  +  :        400 :   switch (item->format)
             +  +  +  -  
                      + ]
   12126                 :            :     {
   12127                 :        193 :     case 'd':
   12128         [ -  + ]:        193 :       assert (count == 1);
   12129   [ +  +  +  +  :        193 :       switch (type)
                -  +  - ]
   12130                 :            :         {
   12131                 :            : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   12132                 :            :           case ELF_T_##NAME:                                                  \
   12133                 :            :             colno = print_core_item (colno, ',', WRAP_COLUMN,                 \
   12134                 :            :                                      0, item->name, dec, value_##Name[0]); \
   12135                 :            :             break
   12136                 :        193 :           TYPES;
   12137                 :            : #undef DO_TYPE
   12138                 :          0 :         default:
   12139                 :          0 :           abort ();
   12140                 :            :         }
   12141                 :            :       break;
   12142                 :            : 
   12143                 :        111 :     case 'x':
   12144         [ -  + ]:        111 :       assert (count == 1);
   12145   [ -  -  +  -  :        111 :       switch (type)
                +  -  - ]
   12146                 :            :         {
   12147                 :            : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   12148                 :            :           case ELF_T_##NAME:                                                  \
   12149                 :            :             colno = print_core_item (colno, ',', WRAP_COLUMN,                 \
   12150                 :            :                                      0, item->name, hex, value_##Name[0]);      \
   12151                 :            :             break
   12152                 :        111 :           TYPES;
   12153                 :            : #undef DO_TYPE
   12154                 :          0 :         default:
   12155                 :          0 :           abort ();
   12156                 :            :         }
   12157                 :            :       break;
   12158                 :            : 
   12159                 :         22 :     case 'b':
   12160                 :            :     case 'B':
   12161         [ -  + ]:         22 :       assert (size % sizeof (unsigned int) == 0);
   12162                 :         22 :       unsigned int nbits = count * size * 8;
   12163                 :         22 :       unsigned int pop = 0;
   12164         [ +  + ]:         56 :       for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
   12165                 :         34 :         pop += __builtin_popcount (*i);
   12166                 :         22 :       bool negate = pop > nbits / 2;
   12167                 :         22 :       const unsigned int bias = item->format == 'b';
   12168                 :            : 
   12169                 :         22 :       {
   12170         [ -  + ]:         22 :         char printed[(negate ? nbits - pop : pop) * 16 + 1];
   12171                 :         22 :         char *p = printed;
   12172                 :         22 :         *p = '\0';
   12173                 :            : 
   12174                 :         22 :         if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
   12175                 :            :           {
   12176                 :            :             assert (size == sizeof (unsigned int) * 2);
   12177                 :            :             for (unsigned int *i = data;
   12178                 :            :                  (void *) i < data + count * size; i += 2)
   12179                 :            :               {
   12180                 :            :                 unsigned int w = i[1];
   12181                 :            :                 i[1] = i[0];
   12182                 :            :                 i[0] = w;
   12183                 :            :               }
   12184                 :            :           }
   12185                 :            : 
   12186                 :         22 :         unsigned int lastbit = 0;
   12187                 :         22 :         unsigned int run = 0;
   12188                 :         22 :         for (const unsigned int *i = data;
   12189         [ +  + ]:         56 :              (void *) i < data + count * size; ++i)
   12190                 :            :           {
   12191                 :         34 :             unsigned int bit = ((void *) i - data) * 8;
   12192         [ -  + ]:         34 :             unsigned int w = negate ? ~*i : *i;
   12193         [ -  + ]:         34 :             while (w != 0)
   12194                 :            :               {
   12195                 :            :                 /* Note that a right shift equal to (or greater than)
   12196                 :            :                    the number of bits of w is undefined behaviour.  In
   12197                 :            :                    particular when the least significant bit is bit 32
   12198                 :            :                    (w = 0x8000000) then w >>= n is undefined.  So
   12199                 :            :                    explicitly handle that case separately.  */
   12200                 :          0 :                 unsigned int n = ffs (w);
   12201         [ #  # ]:          0 :                 if (n < sizeof (w) * 8)
   12202                 :          0 :                   w >>= n;
   12203                 :            :                 else
   12204                 :            :                   w = 0;
   12205                 :          0 :                 bit += n;
   12206                 :            : 
   12207   [ #  #  #  # ]:          0 :                 if (lastbit != 0 && lastbit + 1 == bit)
   12208                 :          0 :                   ++run;
   12209                 :            :                 else
   12210                 :            :                   {
   12211         [ #  # ]:          0 :                     if (lastbit == 0)
   12212                 :          0 :                       p += sprintf (p, "%u", bit - bias);
   12213         [ #  # ]:          0 :                     else if (run == 0)
   12214                 :          0 :                       p += sprintf (p, ",%u", bit - bias);
   12215                 :            :                     else
   12216                 :          0 :                       p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
   12217                 :            :                     run = 0;
   12218                 :            :                   }
   12219                 :            : 
   12220                 :            :                 lastbit = bit;
   12221                 :            :               }
   12222                 :            :           }
   12223   [ -  +  -  - ]:         22 :         if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
   12224                 :          0 :           p += sprintf (p, "-%u", lastbit - bias);
   12225                 :            : 
   12226         [ +  - ]:         44 :         colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   12227                 :            :                                  negate ? "~<%s>" : "<%s>", printed);
   12228                 :            :       }
   12229                 :         22 :       break;
   12230                 :            : 
   12231                 :         44 :     case 'T':
   12232                 :            :     case (char) ('T'|0x80):
   12233         [ -  + ]:         44 :       assert (count == 2);
   12234                 :         44 :       Dwarf_Word sec;
   12235                 :         44 :       Dwarf_Word usec;
   12236   [ -  -  +  -  :         44 :       switch (type)
                +  -  - ]
   12237                 :            :         {
   12238                 :            : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   12239                 :            :           case ELF_T_##NAME:                                                  \
   12240                 :            :             sec = value_##Name[0];                                            \
   12241                 :            :             usec = value_##Name[1];                                           \
   12242                 :            :             break
   12243                 :         44 :           TYPES;
   12244                 :            : #undef DO_TYPE
   12245                 :          0 :         default:
   12246                 :          0 :           abort ();
   12247                 :            :         }
   12248         [ -  + ]:         44 :       if (unlikely (item->format == (char) ('T'|0x80)))
   12249                 :            :         {
   12250                 :            :           /* This is a hack for an ill-considered 64-bit ABI where
   12251                 :            :              tv_usec is actually a 32-bit field with 32 bits of padding
   12252                 :            :              rounding out struct timeval.  We've already converted it as
   12253                 :            :              a 64-bit field.  For little-endian, this just means the
   12254                 :            :              high half is the padding; it's presumably zero, but should
   12255                 :            :              be ignored anyway.  For big-endian, it means the 32-bit
   12256                 :            :              field went into the high half of USEC.  */
   12257                 :          0 :           GElf_Ehdr ehdr_mem;
   12258                 :          0 :           GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
   12259         [ #  # ]:          0 :           if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
   12260                 :          0 :             usec >>= 32;
   12261                 :            :           else
   12262                 :          0 :             usec &= UINT32_MAX;
   12263                 :            :         }
   12264                 :         44 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   12265                 :            :                                "%" PRIu64 ".%.6" PRIu64, sec, usec);
   12266                 :         44 :       break;
   12267                 :            : 
   12268                 :          9 :     case 'c':
   12269         [ -  + ]:          9 :       assert (count == 1);
   12270                 :          9 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   12271                 :          9 :                                "%c", value_Byte[0]);
   12272                 :          9 :       break;
   12273                 :            : 
   12274                 :         18 :     case 's':
   12275                 :         18 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   12276                 :            :                                "%.*s", (int) count, value_Byte);
   12277                 :         18 :       break;
   12278                 :            : 
   12279                 :          1 :     case '\n':
   12280                 :            :       /* This is a list of strings separated by '\n'.  */
   12281         [ -  + ]:          1 :       assert (item->count == 0);
   12282         [ -  + ]:          1 :       assert (repeated_size != NULL);
   12283         [ -  + ]:          1 :       assert (item->name == NULL);
   12284         [ -  + ]:          1 :       if (unlikely (item->offset >= *repeated_size))
   12285                 :            :         break;
   12286                 :            : 
   12287                 :          1 :       const char *s = desc + item->offset;
   12288                 :          1 :       size = *repeated_size - item->offset;
   12289                 :          1 :       *repeated_size = 0;
   12290         [ +  - ]:         48 :       while (size > 0)
   12291                 :            :         {
   12292                 :         48 :           const char *eol = memchr (s, '\n', size);
   12293                 :         48 :           int len = size;
   12294         [ +  + ]:         48 :           if (eol != NULL)
   12295                 :         47 :             len = eol - s;
   12296                 :         48 :           printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
   12297         [ +  + ]:         48 :           if (eol == NULL)
   12298                 :            :             break;
   12299                 :         47 :           size -= eol + 1 - s;
   12300                 :         47 :           s = eol + 1;
   12301                 :            :         }
   12302                 :            : 
   12303                 :            :       colno = WRAP_COLUMN;
   12304                 :            :       break;
   12305                 :            : 
   12306                 :            :     case 'h':
   12307                 :            :       break;
   12308                 :            : 
   12309                 :          0 :     default:
   12310                 :          0 :       error (0, 0, "XXX not handling format '%c' for %s",
   12311                 :            :              item->format, item->name);
   12312                 :            :       break;
   12313                 :            :     }
   12314                 :            : 
   12315                 :            : #undef TYPES
   12316                 :            : 
   12317                 :          0 :   return colno;
   12318                 :            : }
   12319                 :            : 
   12320                 :            : 
   12321                 :            : /* Sort items by group, and by layout offset within each group.  */
   12322                 :            : static int
   12323                 :        895 : compare_core_items (const void *a, const void *b)
   12324                 :            : {
   12325                 :        895 :   const Ebl_Core_Item *const *p1 = a;
   12326                 :        895 :   const Ebl_Core_Item *const *p2 = b;
   12327                 :        895 :   const Ebl_Core_Item *item1 = *p1;
   12328                 :        895 :   const Ebl_Core_Item *item2 = *p2;
   12329                 :            : 
   12330                 :        895 :   return ((item1->group == item2->group ? 0
   12331         [ +  + ]:        895 :            : strcmp (item1->group, item2->group))
   12332         [ -  + ]:        895 :           ?: (int) item1->offset - (int) item2->offset);
   12333                 :            : }
   12334                 :            : 
   12335                 :            : /* Sort item groups by layout offset of the first item in the group.  */
   12336                 :            : static int
   12337                 :        139 : compare_core_item_groups (const void *a, const void *b)
   12338                 :            : {
   12339                 :        139 :   const Ebl_Core_Item *const *const *p1 = a;
   12340                 :        139 :   const Ebl_Core_Item *const *const *p2 = b;
   12341                 :        139 :   const Ebl_Core_Item *const *group1 = *p1;
   12342                 :        139 :   const Ebl_Core_Item *const *group2 = *p2;
   12343                 :        139 :   const Ebl_Core_Item *item1 = *group1;
   12344                 :        139 :   const Ebl_Core_Item *item2 = *group2;
   12345                 :            : 
   12346                 :        139 :   return (int) item1->offset - (int) item2->offset;
   12347                 :            : }
   12348                 :            : 
   12349                 :            : static unsigned int
   12350                 :         37 : handle_core_items (Elf *core, const void *desc, size_t descsz,
   12351                 :            :                    const Ebl_Core_Item *items, size_t nitems)
   12352                 :            : {
   12353         [ +  + ]:         37 :   if (nitems == 0)
   12354                 :            :     return 0;
   12355                 :         34 :   unsigned int colno = 0;
   12356                 :            : 
   12357                 :            :   /* FORMAT '\n' makes sense to be present only as a single item as it
   12358                 :            :      processes all the data of a note.  FORMATs 'b' and 'B' have a special case
   12359                 :            :      if present as a single item but they can be also processed with other
   12360                 :            :      items below.  */
   12361   [ +  +  +  + ]:         34 :   if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
   12362         [ +  - ]:          8 :                       || items[0].format == 'B'))
   12363                 :            :     {
   12364         [ -  + ]:          1 :       assert (items[0].offset == 0);
   12365                 :          1 :       size_t size = descsz;
   12366                 :          1 :       colno = handle_core_item (core, items, desc, colno, &size);
   12367                 :            :       /* If SIZE is not zero here there is some remaining data.  But we do not
   12368                 :            :          know how to process it anyway.  */
   12369                 :          1 :       return colno;
   12370                 :            :     }
   12371         [ +  + ]:        424 :   for (size_t i = 0; i < nitems; ++i)
   12372         [ -  + ]:        391 :     assert (items[i].format != '\n');
   12373                 :            : 
   12374                 :            :   /* Sort to collect the groups together.  */
   12375                 :         33 :   const Ebl_Core_Item *sorted_items[nitems];
   12376         [ +  + ]:        424 :   for (size_t i = 0; i < nitems; ++i)
   12377                 :        391 :     sorted_items[i] = &items[i];
   12378                 :         33 :   qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
   12379                 :            : 
   12380                 :            :   /* Collect the unique groups and sort them.  */
   12381                 :         33 :   const Ebl_Core_Item **groups[nitems];
   12382                 :         33 :   groups[0] = &sorted_items[0];
   12383                 :         33 :   size_t ngroups = 1;
   12384         [ +  + ]:        391 :   for (size_t i = 1; i < nitems; ++i)
   12385         [ +  + ]:        358 :     if (sorted_items[i]->group != sorted_items[i - 1]->group
   12386         [ +  - ]:         75 :         && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
   12387                 :         75 :       groups[ngroups++] = &sorted_items[i];
   12388                 :         33 :   qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
   12389                 :            : 
   12390                 :            :   /* Write out all the groups.  */
   12391                 :         33 :   const void *last = desc;
   12392                 :         35 :   do
   12393                 :            :     {
   12394         [ +  + ]:        145 :       for (size_t i = 0; i < ngroups; ++i)
   12395                 :            :         {
   12396                 :        110 :           for (const Ebl_Core_Item **item = groups[i];
   12397         [ +  + ]:        509 :                (item < &sorted_items[nitems]
   12398         [ +  + ]:        474 :                 && ((*item)->group == groups[i][0]->group
   12399         [ -  + ]:         75 :                     || !strcmp ((*item)->group, groups[i][0]->group)));
   12400                 :        399 :                ++item)
   12401                 :        399 :             colno = handle_core_item (core, *item, desc, colno, NULL);
   12402                 :            : 
   12403                 :            :           /* Force a line break at the end of the group.  */
   12404                 :        110 :           colno = WRAP_COLUMN;
   12405                 :            :         }
   12406                 :            : 
   12407         [ +  + ]:         35 :       if (descsz == 0)
   12408                 :            :         break;
   12409                 :            : 
   12410                 :            :       /* This set of items consumed a certain amount of the note's data.
   12411                 :            :          If there is more data there, we have another unit of the same size.
   12412                 :            :          Loop to print that out too.  */
   12413                 :         20 :       const Ebl_Core_Item *item = &items[nitems - 1];
   12414                 :         40 :       size_t eltsz = item->offset + gelf_fsize (core, item->type,
   12415                 :         20 :                                                 item->count ?: 1, EV_CURRENT);
   12416                 :            : 
   12417                 :         20 :       int reps = -1;
   12418                 :         20 :       do
   12419                 :            :         {
   12420                 :         20 :           ++reps;
   12421                 :         20 :           desc += eltsz;
   12422                 :         20 :           descsz -= eltsz;
   12423                 :            :         }
   12424   [ +  +  -  + ]:         20 :       while (descsz >= eltsz && !memcmp (desc, last, eltsz));
   12425                 :            : 
   12426         [ +  - ]:         20 :       if (reps == 1)
   12427                 :            :         {
   12428                 :            :           /* For just one repeat, print it unabridged twice.  */
   12429                 :         20 :           desc -= eltsz;
   12430                 :         20 :           descsz += eltsz;
   12431                 :            :         }
   12432         [ -  + ]:         20 :       else if (reps > 1)
   12433                 :          0 :         printf (_("\n%*s... <repeats %u more times> ..."),
   12434                 :            :                 ITEM_INDENT, "", reps);
   12435                 :            : 
   12436                 :         20 :       last = desc;
   12437                 :            :     }
   12438         [ +  + ]:         20 :   while (descsz > 0);
   12439                 :            : 
   12440                 :         33 :   return colno;
   12441                 :            : }
   12442                 :            : 
   12443                 :            : static unsigned int
   12444                 :        162 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
   12445                 :            :                       const Ebl_Register_Location *regloc, const void *desc,
   12446                 :            :                       unsigned int colno)
   12447                 :            : {
   12448         [ -  + ]:        162 :   if (regloc->bits % 8 != 0)
   12449                 :            :     {
   12450                 :          0 :       error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
   12451                 :            :              regloc->bits);
   12452                 :          0 :       return colno;
   12453                 :            :     }
   12454                 :            : 
   12455                 :        162 :   desc += regloc->offset;
   12456                 :            : 
   12457         [ +  + ]:        586 :   for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
   12458                 :            :     {
   12459                 :        424 :       char name[REGNAMESZ];
   12460                 :        424 :       int bits;
   12461                 :        424 :       int type;
   12462                 :        424 :       register_info (ebl, reg, regloc, name, &bits, &type);
   12463                 :            : 
   12464                 :            : #define TYPES                                                                 \
   12465                 :            :       BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8);                          \
   12466                 :            :       BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16);                       \
   12467                 :            :       BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32);                             \
   12468                 :            :       BITS (64, XWORD, "%20" PRId64, "  0x%.16" PRIx64)
   12469                 :            : 
   12470                 :            : #define BITS(bits, xtype, sfmt, ufmt)                           \
   12471                 :            :       uint##bits##_t b##bits; int##bits##_t b##bits##s
   12472                 :        424 :       union { TYPES; uint64_t b128[2]; } value;
   12473                 :            : #undef  BITS
   12474                 :            : 
   12475         [ +  + ]:        424 :       switch (type)
   12476                 :            :         {
   12477                 :        336 :         case DW_ATE_unsigned:
   12478                 :            :         case DW_ATE_signed:
   12479                 :            :         case DW_ATE_address:
   12480   [ -  +  +  +  :        336 :           switch (bits)
                   +  - ]
   12481                 :            :             {
   12482                 :            : #define BITS(bits, xtype, sfmt, ufmt)                                         \
   12483                 :            :             case bits:                                                        \
   12484                 :            :               desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0);           \
   12485                 :            :               if (type == DW_ATE_signed)                                      \
   12486                 :            :                 colno = print_core_item (colno, ' ', WRAP_COLUMN,             \
   12487                 :            :                                          maxregname, name,                    \
   12488                 :            :                                          sfmt, value.b##bits##s);             \
   12489                 :            :               else                                                            \
   12490                 :            :                 colno = print_core_item (colno, ' ', WRAP_COLUMN,             \
   12491                 :            :                                          maxregname, name,                    \
   12492                 :            :                                          ufmt, value.b##bits);                \
   12493                 :            :               break
   12494                 :            : 
   12495   [ -  -  -  +  :        288 :             TYPES;
             +  +  +  + ]
   12496                 :            : 
   12497                 :         48 :             case 128:
   12498         [ -  + ]:         48 :               assert (type == DW_ATE_unsigned);
   12499                 :         48 :               desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
   12500                 :         48 :               int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
   12501                 :         48 :               colno = print_core_item (colno, ' ', WRAP_COLUMN,
   12502                 :            :                                        maxregname, name,
   12503                 :            :                                        "0x%.16" PRIx64 "%.16" PRIx64,
   12504                 :         48 :                                        value.b128[!be], value.b128[be]);
   12505                 :         48 :               break;
   12506                 :            : 
   12507                 :          0 :             default:
   12508                 :          0 :               abort ();
   12509                 :            : #undef  BITS
   12510                 :            :             }
   12511                 :            :           break;
   12512                 :            : 
   12513                 :         88 :         default:
   12514                 :            :           /* Print each byte in hex, the whole thing in native byte order.  */
   12515         [ -  + ]:         88 :           assert (bits % 8 == 0);
   12516                 :         88 :           const uint8_t *bytes = desc;
   12517                 :         88 :           desc += bits / 8;
   12518                 :         88 :           char hex[bits / 4 + 1];
   12519                 :         88 :           hex[bits / 4] = '\0';
   12520                 :         88 :           int incr = 1;
   12521         [ +  + ]:         88 :           if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
   12522                 :            :             {
   12523                 :         48 :               bytes += bits / 8 - 1;
   12524                 :         48 :               incr = -1;
   12525                 :            :             }
   12526                 :         88 :           size_t idx = 0;
   12527         [ +  + ]:        872 :           for (char *h = hex; bits > 0; bits -= 8, idx += incr)
   12528                 :            :             {
   12529                 :        784 :               *h++ = "0123456789abcdef"[bytes[idx] >> 4];
   12530                 :        784 :               *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
   12531                 :            :             }
   12532                 :         88 :           colno = print_core_item (colno, ' ', WRAP_COLUMN,
   12533                 :            :                                    maxregname, name, "0x%s", hex);
   12534                 :         88 :           break;
   12535                 :            :         }
   12536                 :        424 :       desc += regloc->pad;
   12537                 :            : 
   12538                 :            : #undef TYPES
   12539                 :            :     }
   12540                 :            : 
   12541                 :            :   return colno;
   12542                 :            : }
   12543                 :            : 
   12544                 :            : 
   12545                 :            : struct register_info
   12546                 :            : {
   12547                 :            :   const Ebl_Register_Location *regloc;
   12548                 :            :   const char *set;
   12549                 :            :   char name[REGNAMESZ];
   12550                 :            :   int regno;
   12551                 :            :   int bits;
   12552                 :            :   int type;
   12553                 :            : };
   12554                 :            : 
   12555                 :            : static int
   12556                 :       2066 : register_bitpos (const struct register_info *r)
   12557                 :            : {
   12558                 :       2066 :   return (r->regloc->offset * 8
   12559                 :       2066 :           + ((r->regno - r->regloc->regno)
   12560                 :       2066 :              * (r->regloc->bits + r->regloc->pad * 8)));
   12561                 :            : }
   12562                 :            : 
   12563                 :            : static int
   12564                 :       1062 : compare_sets_by_info (const struct register_info *r1,
   12565                 :            :                       const struct register_info *r2)
   12566                 :            : {
   12567                 :       1062 :   return ((int) r2->bits - (int) r1->bits
   12568         [ +  + ]:       1062 :           ?: register_bitpos (r1) - register_bitpos (r2));
   12569                 :            : }
   12570                 :            : 
   12571                 :            : /* Sort registers by set, and by size and layout offset within each set.  */
   12572                 :            : static int
   12573                 :       4595 : compare_registers (const void *a, const void *b)
   12574                 :            : {
   12575                 :       4595 :   const struct register_info *r1 = a;
   12576                 :       4595 :   const struct register_info *r2 = b;
   12577                 :            : 
   12578                 :            :   /* Unused elements sort last.  */
   12579         [ +  + ]:       4595 :   if (r1->regloc == NULL)
   12580                 :       3309 :     return r2->regloc == NULL ? 0 : 1;
   12581         [ +  + ]:       1286 :   if (r2->regloc == NULL)
   12582                 :            :     return -1;
   12583                 :            : 
   12584         [ +  + ]:       1121 :   return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
   12585         [ -  + ]:       1121 :           ?: compare_sets_by_info (r1, r2));
   12586                 :            : }
   12587                 :            : 
   12588                 :            : /* Sort register sets by layout offset of the first register in the set.  */
   12589                 :            : static int
   12590                 :         20 : compare_register_sets (const void *a, const void *b)
   12591                 :            : {
   12592                 :         20 :   const struct register_info *const *p1 = a;
   12593                 :         20 :   const struct register_info *const *p2 = b;
   12594                 :         20 :   return compare_sets_by_info (*p1, *p2);
   12595                 :            : }
   12596                 :            : 
   12597                 :            : static inline bool
   12598                 :        864 : same_set (const struct register_info *a,
   12599                 :            :           const struct register_info *b,
   12600                 :            :           const struct register_info *regs,
   12601                 :            :           size_t maxnreg)
   12602                 :            : {
   12603         [ +  - ]:        864 :   return (a < &regs[maxnreg] && a->regloc != NULL
   12604   [ +  -  +  + ]:        864 :           && b < &regs[maxnreg] && b->regloc != NULL
   12605         [ +  + ]:        846 :           && a->bits == b->bits
   12606   [ +  -  +  +  :       1692 :           && (a->set == b->set || !strcmp (a->set, b->set)));
                   +  - ]
   12607                 :            : }
   12608                 :            : 
   12609                 :            : static unsigned int
   12610                 :         37 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
   12611                 :            :                        const Ebl_Register_Location *reglocs, size_t nregloc)
   12612                 :            : {
   12613         [ +  + ]:         37 :   if (nregloc == 0)
   12614                 :            :     return 0;
   12615                 :            : 
   12616                 :         18 :   ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
   12617         [ -  + ]:         18 :   if (maxnreg <= 0)
   12618                 :            :     {
   12619         [ #  # ]:          0 :       for (size_t i = 0; i < nregloc; ++i)
   12620                 :          0 :         if (maxnreg < reglocs[i].regno + reglocs[i].count)
   12621                 :            :           maxnreg = reglocs[i].regno + reglocs[i].count;
   12622         [ #  # ]:          0 :       assert (maxnreg > 0);
   12623                 :            :     }
   12624                 :            : 
   12625                 :         18 :   struct register_info regs[maxnreg];
   12626                 :         18 :   memset (regs, 0, sizeof regs);
   12627                 :            : 
   12628                 :            :   /* Sort to collect the sets together.  */
   12629                 :         18 :   int maxreg = 0;
   12630         [ +  + ]:        180 :   for (size_t i = 0; i < nregloc; ++i)
   12631                 :        162 :     for (int reg = reglocs[i].regno;
   12632         [ +  + ]:        586 :          reg < reglocs[i].regno + reglocs[i].count;
   12633                 :        424 :          ++reg)
   12634                 :            :       {
   12635         [ -  + ]:        424 :         assert (reg < maxnreg);
   12636                 :        424 :         if (reg > maxreg)
   12637                 :            :           maxreg = reg;
   12638                 :        424 :         struct register_info *info = &regs[reg];
   12639                 :        424 :         info->regloc = &reglocs[i];
   12640                 :        424 :         info->regno = reg;
   12641                 :        424 :         info->set = register_info (ebl, reg, &reglocs[i],
   12642                 :        424 :                                    info->name, &info->bits, &info->type);
   12643                 :            :       }
   12644                 :         18 :   qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
   12645                 :            : 
   12646                 :            :   /* Collect the unique sets and sort them.  */
   12647                 :         18 :   struct register_info *sets[maxreg + 1];
   12648                 :         18 :   sets[0] = &regs[0];
   12649                 :         18 :   size_t nsets = 1;
   12650         [ +  + ]:       1279 :   for (int i = 1; i <= maxreg; ++i)
   12651         [ +  + ]:       1261 :     if (regs[i].regloc != NULL
   12652         [ +  + ]:        406 :         && !same_set (&regs[i], &regs[i - 1], regs, maxnreg))
   12653                 :         16 :       sets[nsets++] = &regs[i];
   12654                 :         18 :   qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
   12655                 :            : 
   12656                 :            :   /* Write out all the sets.  */
   12657                 :         18 :   unsigned int colno = 0;
   12658         [ +  + ]:         52 :   for (size_t i = 0; i < nsets; ++i)
   12659                 :            :     {
   12660                 :            :       /* Find the longest name of a register in this set.  */
   12661                 :         34 :       size_t maxname = 0;
   12662                 :         34 :       const struct register_info *end;
   12663         [ +  + ]:        458 :       for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
   12664                 :            :         {
   12665                 :        424 :           size_t len = strlen (end->name);
   12666                 :        424 :           if (len > maxname)
   12667                 :            :             maxname = len;
   12668                 :            :         }
   12669                 :            : 
   12670                 :        196 :       for (const struct register_info *reg = sets[i];
   12671         [ +  + ]:        196 :            reg < end;
   12672                 :        162 :            reg += reg->regloc->count ?: 1)
   12673         [ +  - ]:        162 :         colno = handle_core_register (ebl, core, maxname,
   12674                 :            :                                       reg->regloc, desc, colno);
   12675                 :            : 
   12676                 :            :       /* Force a line break at the end of the group.  */
   12677                 :         34 :       colno = WRAP_COLUMN;
   12678                 :            :     }
   12679                 :            : 
   12680                 :         18 :   return colno;
   12681                 :            : }
   12682                 :            : 
   12683                 :            : static void
   12684                 :          9 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   12685                 :            : {
   12686                 :          9 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
   12687         [ -  + ]:          9 :   if (data == NULL)
   12688                 :          0 :   elf_error:
   12689                 :          0 :     error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
   12690                 :            : 
   12691                 :          9 :   const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
   12692         [ +  + ]:        177 :   for (size_t i = 0; i < nauxv; ++i)
   12693                 :            :     {
   12694                 :        168 :       GElf_auxv_t av_mem;
   12695                 :        168 :       GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
   12696         [ -  + ]:        168 :       if (av == NULL)
   12697                 :          0 :         goto elf_error;
   12698                 :            : 
   12699                 :        168 :       const char *name;
   12700                 :        168 :       const char *fmt;
   12701         [ -  + ]:        168 :       if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
   12702                 :            :         {
   12703                 :            :           /* Unknown type.  */
   12704         [ #  # ]:          0 :           if (av->a_un.a_val == 0)
   12705                 :          0 :             printf ("    %" PRIu64 "\n", av->a_type);
   12706                 :            :           else
   12707                 :          0 :             printf ("    %" PRIu64 ": %#" PRIx64 "\n",
   12708                 :            :                     av->a_type, av->a_un.a_val);
   12709                 :            :         }
   12710                 :            :       else
   12711   [ +  +  +  -  :        168 :         switch (fmt[0])
                   +  - ]
   12712                 :            :           {
   12713                 :          9 :           case '\0':            /* Normally zero.  */
   12714         [ +  - ]:          9 :             if (av->a_un.a_val == 0)
   12715                 :            :               {
   12716                 :          9 :                 printf ("    %s\n", name);
   12717                 :            :                 break;
   12718                 :            :               }
   12719                 :         74 :             FALLTHROUGH;
   12720                 :            :           case 'x':             /* hex */
   12721                 :            :           case 'p':             /* address */
   12722                 :            :           case 's':             /* address of string */
   12723                 :         74 :             printf ("    %s: %#" PRIx64 "\n", name, av->a_un.a_val);
   12724                 :            :             break;
   12725                 :         81 :           case 'u':
   12726                 :         81 :             printf ("    %s: %" PRIu64 "\n", name, av->a_un.a_val);
   12727                 :            :             break;
   12728                 :          0 :           case 'd':
   12729                 :          0 :             printf ("    %s: %" PRId64 "\n", name, av->a_un.a_val);
   12730                 :            :             break;
   12731                 :            : 
   12732                 :          4 :           case 'b':
   12733                 :          4 :             printf ("    %s: %#" PRIx64 "  ", name, av->a_un.a_val);
   12734                 :          4 :             GElf_Xword bit = 1;
   12735                 :          4 :             const char *pfx = "<";
   12736         [ +  + ]:        110 :             for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
   12737                 :            :               {
   12738         [ +  + ]:        106 :                 if (av->a_un.a_val & bit)
   12739                 :            :                   {
   12740                 :         77 :                     printf ("%s%s", pfx, p);
   12741                 :         77 :                     pfx = " ";
   12742                 :            :                   }
   12743                 :        106 :                 bit <<= 1;
   12744                 :            :               }
   12745                 :        168 :             printf (">\n");
   12746                 :            :             break;
   12747                 :            : 
   12748                 :          0 :           default:
   12749                 :          0 :             abort ();
   12750                 :            :           }
   12751                 :            :     }
   12752                 :          9 : }
   12753                 :            : 
   12754                 :            : static bool
   12755                 :        195 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
   12756                 :            : {
   12757   [ -  +  -  + ]:        195 :   return ptr < end && (size_t) (end - ptr) >= sz;
   12758                 :            : }
   12759                 :            : 
   12760                 :            : static bool
   12761                 :         18 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
   12762                 :            :               int *retp)
   12763                 :            : {
   12764   [ +  -  +  - ]:         36 :   if (! buf_has_data (*ptrp, end, 4))
   12765                 :            :     return false;
   12766                 :            : 
   12767                 :         18 :   *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
   12768                 :         18 :   return true;
   12769                 :            : }
   12770                 :            : 
   12771                 :            : static bool
   12772                 :        177 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
   12773                 :            :                 uint64_t *retp)
   12774                 :            : {
   12775                 :        177 :   size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
   12776   [ +  -  +  - ]:        354 :   if (! buf_has_data (*ptrp, end, sz))
   12777                 :            :     return false;
   12778                 :            : 
   12779                 :        177 :   union
   12780                 :            :   {
   12781                 :            :     uint64_t u64;
   12782                 :            :     uint32_t u32;
   12783                 :            :   } u;
   12784                 :            : 
   12785                 :        177 :   *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
   12786                 :            : 
   12787         [ +  + ]:        177 :   if (sz == 4)
   12788                 :         93 :     *retp = u.u32;
   12789                 :            :   else
   12790                 :         84 :     *retp = u.u64;
   12791                 :            :   return true;
   12792                 :            : }
   12793                 :            : 
   12794                 :            : static void
   12795                 :          6 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   12796                 :            : {
   12797                 :          6 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
   12798         [ -  + ]:          6 :   if (data == NULL)
   12799                 :          0 :     error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
   12800                 :            : 
   12801                 :          6 :   unsigned char const *ptr = data->d_buf;
   12802                 :          6 :   unsigned char const *const end = data->d_buf + data->d_size;
   12803                 :            : 
   12804                 :            :   /* Siginfo head is three ints: signal number, error number, origin
   12805                 :            :      code.  */
   12806                 :          6 :   int si_signo, si_errno, si_code;
   12807         [ -  + ]:          6 :   if (! buf_read_int (core, &ptr, end, &si_signo)
   12808         [ -  + ]:          6 :       || ! buf_read_int (core, &ptr, end, &si_errno)
   12809         [ -  + ]:          6 :       || ! buf_read_int (core, &ptr, end, &si_code))
   12810                 :            :     {
   12811                 :          0 :     fail:
   12812                 :          0 :       printf ("    Not enough data in NT_SIGINFO note.\n");
   12813                 :          0 :       return;
   12814                 :            :     }
   12815                 :            : 
   12816                 :            :   /* Next is a pointer-aligned union of structures.  On 64-bit
   12817                 :            :      machines, that implies a word of padding.  */
   12818         [ +  + ]:          6 :   if (gelf_getclass (core) == ELFCLASS64)
   12819                 :          3 :     ptr += 4;
   12820                 :            : 
   12821                 :          6 :   printf ("    si_signo: %d, si_errno: %d, si_code: %d\n",
   12822                 :            :           si_signo, si_errno, si_code);
   12823                 :            : 
   12824         [ +  - ]:          6 :   if (si_code > 0)
   12825         [ +  - ]:          6 :     switch (si_signo)
   12826                 :            :       {
   12827                 :          6 :       case CORE_SIGILL:
   12828                 :            :       case CORE_SIGFPE:
   12829                 :            :       case CORE_SIGSEGV:
   12830                 :            :       case CORE_SIGBUS:
   12831                 :            :         {
   12832                 :          6 :           uint64_t addr;
   12833         [ -  + ]:          6 :           if (! buf_read_ulong (core, &ptr, end, &addr))
   12834                 :          0 :             goto fail;
   12835                 :          6 :           printf ("    fault address: %#" PRIx64 "\n", addr);
   12836                 :          6 :           break;
   12837                 :            :         }
   12838                 :            :       default:
   12839                 :            :         ;
   12840                 :            :       }
   12841         [ #  # ]:          0 :   else if (si_code == CORE_SI_USER)
   12842                 :            :     {
   12843                 :          0 :       int pid, uid;
   12844         [ #  # ]:          0 :       if (! buf_read_int (core, &ptr, end, &pid)
   12845         [ #  # ]:          0 :           || ! buf_read_int (core, &ptr, end, &uid))
   12846                 :          0 :         goto fail;
   12847                 :          0 :       printf ("    sender PID: %d, sender UID: %d\n", pid, uid);
   12848                 :            :     }
   12849                 :            : }
   12850                 :            : 
   12851                 :            : static void
   12852                 :          6 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   12853                 :            : {
   12854                 :          6 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
   12855         [ -  + ]:          6 :   if (data == NULL)
   12856                 :          0 :     error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
   12857                 :            : 
   12858                 :          6 :   unsigned char const *ptr = data->d_buf;
   12859                 :          6 :   unsigned char const *const end = data->d_buf + data->d_size;
   12860                 :            : 
   12861                 :          6 :   uint64_t count, page_size;
   12862         [ -  + ]:          6 :   if (! buf_read_ulong (core, &ptr, end, &count)
   12863         [ -  + ]:          6 :       || ! buf_read_ulong (core, &ptr, end, &page_size))
   12864                 :            :     {
   12865                 :          0 :     fail:
   12866                 :          0 :       printf ("    Not enough data in NT_FILE note.\n");
   12867                 :          0 :       return;
   12868                 :            :     }
   12869                 :            : 
   12870                 :          6 :   size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
   12871                 :          6 :   uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
   12872         [ -  + ]:          6 :   if (count > maxcount)
   12873                 :          0 :     goto fail;
   12874                 :            : 
   12875                 :            :   /* Where file names are stored.  */
   12876                 :          6 :   unsigned char const *const fstart = ptr + 3 * count * addrsize;
   12877                 :          6 :   char const *fptr = (char *) fstart;
   12878                 :            : 
   12879                 :          6 :   printf ("    %" PRId64 " files:\n", count);
   12880         [ +  + ]:         59 :   for (uint64_t i = 0; i < count; ++i)
   12881                 :            :     {
   12882                 :         53 :       uint64_t mstart, mend, moffset;
   12883         [ +  - ]:         53 :       if (! buf_read_ulong (core, &ptr, fstart, &mstart)
   12884         [ +  - ]:         53 :           || ! buf_read_ulong (core, &ptr, fstart, &mend)
   12885         [ -  + ]:         53 :           || ! buf_read_ulong (core, &ptr, fstart, &moffset))
   12886                 :          0 :         goto fail;
   12887                 :            : 
   12888                 :         53 :       const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
   12889         [ -  + ]:         53 :       if (fnext == NULL)
   12890                 :          0 :         goto fail;
   12891                 :            : 
   12892                 :         53 :       int ct = printf ("      %08" PRIx64 "-%08" PRIx64
   12893                 :            :                        " %08" PRIx64 " %" PRId64,
   12894                 :            :                        mstart, mend, moffset * page_size, mend - mstart);
   12895         [ +  - ]:         53 :       printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
   12896                 :            : 
   12897                 :         53 :       fptr = fnext + 1;
   12898                 :            :     }
   12899                 :            : }
   12900                 :            : 
   12901                 :            : static void
   12902                 :         38 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
   12903                 :            :                   const char *name, const void *desc)
   12904                 :            : {
   12905                 :         38 :   GElf_Word regs_offset;
   12906                 :         38 :   size_t nregloc;
   12907                 :         38 :   const Ebl_Register_Location *reglocs;
   12908                 :         38 :   size_t nitems;
   12909                 :         38 :   const Ebl_Core_Item *items;
   12910                 :            : 
   12911         [ +  + ]:         38 :   if (! ebl_core_note (ebl, nhdr, name, desc,
   12912                 :            :                        &regs_offset, &nregloc, &reglocs, &nitems, &items))
   12913                 :          1 :     return;
   12914                 :            : 
   12915                 :            :   /* Pass 0 for DESCSZ when there are registers in the note,
   12916                 :            :      so that the ITEMS array does not describe the whole thing.
   12917                 :            :      For non-register notes, the actual descsz might be a multiple
   12918                 :            :      of the unit size, not just exactly the unit size.  */
   12919                 :         37 :   unsigned int colno = handle_core_items (ebl->elf, desc,
   12920         [ +  + ]:         37 :                                           nregloc == 0 ? nhdr->n_descsz : 0,
   12921                 :            :                                           items, nitems);
   12922         [ +  + ]:         37 :   if (colno != 0)
   12923         [ -  + ]:         34 :     putchar_unlocked ('\n');
   12924                 :            : 
   12925                 :         37 :   colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
   12926                 :            :                                  reglocs, nregloc);
   12927         [ +  + ]:         37 :   if (colno != 0)
   12928         [ -  + ]:         55 :     putchar_unlocked ('\n');
   12929                 :            : }
   12930                 :            : 
   12931                 :            : static void
   12932                 :        298 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
   12933                 :            :                    GElf_Off start, Elf_Data *data)
   12934                 :            : {
   12935                 :        298 :   fputs_unlocked (_("  Owner          Data size  Type\n"), stdout);
   12936                 :            : 
   12937         [ -  + ]:        298 :   if (data == NULL)
   12938                 :          0 :     goto bad_note;
   12939                 :            : 
   12940                 :            :   size_t offset = 0;
   12941                 :            :   GElf_Nhdr nhdr;
   12942                 :            :   size_t name_offset;
   12943                 :            :   size_t desc_offset;
   12944         [ +  + ]:       3558 :   while (offset < data->d_size
   12945         [ +  - ]:       3260 :          && (offset = gelf_getnote (data, offset,
   12946                 :            :                                     &nhdr, &name_offset, &desc_offset)) > 0)
   12947                 :            :     {
   12948         [ +  - ]:       3260 :       const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
   12949                 :       3260 :       const char *desc = data->d_buf + desc_offset;
   12950                 :            : 
   12951                 :            :       /* GNU Build Attributes are weird, they store most of their data
   12952                 :            :          into the owner name field.  Extract just the owner name
   12953                 :            :          prefix here, then use the rest later as data.  */
   12954                 :       3260 :       bool is_gnu_build_attr
   12955                 :       3260 :         = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
   12956                 :       6520 :       const char *print_name = (is_gnu_build_attr
   12957         [ +  + ]:       3260 :                                 ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
   12958                 :       6520 :       size_t print_namesz = (is_gnu_build_attr
   12959         [ +  + ]:       3260 :                              ? strlen (print_name) : nhdr.n_namesz);
   12960                 :            : 
   12961                 :       3260 :       char buf[100];
   12962                 :       3260 :       char buf2[100];
   12963                 :       3260 :       printf (_("  %-13.*s  %9" PRId32 "  %s\n"),
   12964                 :            :               (int) print_namesz, print_name, nhdr.n_descsz,
   12965         [ +  + ]:       3260 :               ehdr->e_type == ET_CORE
   12966                 :         59 :               ? ebl_core_note_type_name (ebl, nhdr.n_type,
   12967                 :            :                                          buf, sizeof (buf))
   12968                 :       3201 :               : ebl_object_note_type_name (ebl, name, nhdr.n_type,
   12969                 :            :                                            nhdr.n_descsz,
   12970                 :            :                                            buf2, sizeof (buf2)));
   12971                 :            : 
   12972                 :            :       /* Filter out invalid entries.  */
   12973                 :       3260 :       if (memchr (name, '\0', nhdr.n_namesz) != NULL
   12974                 :            :           /* XXX For now help broken Linux kernels.  */
   12975                 :            :           || 1)
   12976                 :            :         {
   12977         [ +  + ]:       3260 :           if (ehdr->e_type == ET_CORE)
   12978                 :            :             {
   12979         [ +  + ]:         59 :               if (nhdr.n_type == NT_AUXV
   12980         [ +  - ]:          9 :                   && (nhdr.n_namesz == 4 /* Broken old Linux kernels.  */
   12981   [ +  -  +  - ]:          9 :                       || (nhdr.n_namesz == 5 && name[4] == '\0'))
   12982         [ +  - ]:          9 :                   && !memcmp (name, "CORE", 4))
   12983                 :          9 :                 handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
   12984                 :            :                                   start + desc_offset);
   12985   [ +  +  +  - ]:         50 :               else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
   12986      [ +  +  + ]:         38 :                 switch (nhdr.n_type)
   12987                 :            :                   {
   12988                 :          6 :                   case NT_SIGINFO:
   12989                 :          6 :                     handle_siginfo_note (ebl->elf, nhdr.n_descsz,
   12990                 :            :                                          start + desc_offset);
   12991                 :          6 :                     break;
   12992                 :            : 
   12993                 :          6 :                   case NT_FILE:
   12994                 :          6 :                     handle_file_note (ebl->elf, nhdr.n_descsz,
   12995                 :            :                                       start + desc_offset);
   12996                 :          6 :                     break;
   12997                 :            : 
   12998                 :         26 :                   default:
   12999                 :         26 :                     handle_core_note (ebl, &nhdr, name, desc);
   13000                 :            :                   }
   13001                 :            :               else
   13002                 :         12 :                 handle_core_note (ebl, &nhdr, name, desc);
   13003                 :            :             }
   13004                 :            :           else
   13005                 :       3201 :             ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
   13006                 :            :                              nhdr.n_descsz, desc);
   13007                 :            :         }
   13008                 :            :     }
   13009                 :            : 
   13010         [ +  - ]:        298 :   if (offset == data->d_size)
   13011                 :        298 :     return;
   13012                 :            : 
   13013                 :          0 :  bad_note:
   13014         [ #  # ]:          0 :   error (0, 0,
   13015                 :          0 :          _("cannot get content of note: %s"),
   13016                 :          0 :          data != NULL ? "garbage data" : elf_errmsg (-1));
   13017                 :            : }
   13018                 :            : 
   13019                 :            : static void
   13020                 :        165 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
   13021                 :            : {
   13022                 :            :   /* If we have section headers, just look for SHT_NOTE sections.
   13023                 :            :      In a debuginfo file, the program headers are not reliable.  */
   13024         [ +  + ]:        165 :   if (shnum != 0)
   13025                 :            :     {
   13026                 :            :       /* Get the section header string table index.  */
   13027                 :        154 :       size_t shstrndx;
   13028         [ -  + ]:        154 :       if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
   13029                 :          0 :         error_exit (0, _("cannot get section header string table index"));
   13030                 :            : 
   13031                 :            :       Elf_Scn *scn = NULL;
   13032         [ +  + ]:       4419 :       while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   13033                 :            :         {
   13034                 :       4265 :           GElf_Shdr shdr_mem;
   13035                 :       4265 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   13036                 :            : 
   13037   [ +  -  +  + ]:       4265 :           if (shdr == NULL || shdr->sh_type != SHT_NOTE)
   13038                 :            :             /* Not what we are looking for.  */
   13039                 :       3979 :             continue;
   13040                 :            : 
   13041         [ -  + ]:        286 :           if (notes_section != NULL)
   13042                 :            :             {
   13043                 :          0 :               char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
   13044   [ #  #  #  # ]:          0 :               if (sname == NULL || strcmp (sname, notes_section) != 0)
   13045                 :          0 :                 continue;
   13046                 :            :             }
   13047                 :            : 
   13048                 :        286 :           printf (_("\
   13049                 :            : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
   13050                 :            :                   elf_ndxscn (scn),
   13051                 :        286 :                   elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
   13052                 :            :                   shdr->sh_size, shdr->sh_offset);
   13053                 :            : 
   13054                 :        286 :           handle_notes_data (ebl, ehdr, shdr->sh_offset,
   13055                 :            :                              elf_getdata (scn, NULL));
   13056                 :            :         }
   13057                 :        154 :       return;
   13058                 :            :     }
   13059                 :            : 
   13060                 :            :   /* We have to look through the program header to find the note
   13061                 :            :      sections.  There can be more than one.  */
   13062         [ +  + ]:        140 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
   13063                 :            :     {
   13064                 :        129 :       GElf_Phdr mem;
   13065                 :        129 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
   13066                 :            : 
   13067   [ +  -  +  + ]:        129 :       if (phdr == NULL || phdr->p_type != PT_NOTE)
   13068                 :            :         /* Not what we are looking for.  */
   13069                 :        117 :         continue;
   13070                 :            : 
   13071                 :         12 :       printf (_("\
   13072                 :            : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
   13073                 :            :               phdr->p_filesz, phdr->p_offset);
   13074                 :            : 
   13075                 :         12 :       handle_notes_data (ebl, ehdr, phdr->p_offset,
   13076                 :            :                          elf_getdata_rawchunk (ebl->elf,
   13077                 :         12 :                                                phdr->p_offset, phdr->p_filesz,
   13078         [ +  + ]:         12 :                                                (phdr->p_align == 8
   13079                 :            :                                                 ? ELF_T_NHDR8 : ELF_T_NHDR)));
   13080                 :            :     }
   13081                 :            : }
   13082                 :            : 
   13083                 :            : 
   13084                 :            : static void
   13085                 :          6 : hex_dump (const uint8_t *data, size_t len)
   13086                 :            : {
   13087                 :          6 :   size_t pos = 0;
   13088         [ +  + ]:         28 :   while (pos < len)
   13089                 :            :     {
   13090                 :         22 :       printf ("  0x%08zx ", pos);
   13091                 :            : 
   13092                 :         22 :       const size_t chunk = MIN (len - pos, 16);
   13093                 :            : 
   13094         [ +  + ]:        344 :       for (size_t i = 0; i < chunk; ++i)
   13095         [ +  + ]:        322 :         if (i % 4 == 3)
   13096                 :         80 :           printf ("%02x ", data[pos + i]);
   13097                 :            :         else
   13098                 :        322 :           printf ("%02x", data[pos + i]);
   13099                 :            : 
   13100         [ +  + ]:         22 :       if (chunk < 16)
   13101                 :          2 :         printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
   13102                 :            : 
   13103         [ +  + ]:        344 :       for (size_t i = 0; i < chunk; ++i)
   13104                 :            :         {
   13105                 :        322 :           unsigned char b = data[pos + i];
   13106         [ +  + ]:        644 :           printf ("%c", isprint (b) ? b : '.');
   13107                 :            :         }
   13108                 :            : 
   13109                 :         22 :       putchar ('\n');
   13110                 :         22 :       pos += chunk;
   13111                 :            :     }
   13112                 :          6 : }
   13113                 :            : 
   13114                 :            : static void
   13115                 :          6 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
   13116                 :            : {
   13117   [ +  -  -  + ]:          6 :   if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
   13118                 :          0 :     printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
   13119                 :            :             elf_ndxscn (scn), name);
   13120                 :            :   else
   13121                 :            :     {
   13122         [ +  + ]:          6 :       if (print_decompress)
   13123                 :            :         {
   13124                 :            :           /* We try to decompress the section, but keep the old shdr around
   13125                 :            :              so we can show both the original shdr size and the uncompressed
   13126                 :            :              data size.   */
   13127         [ +  + ]:          4 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
   13128                 :            :             {
   13129         [ -  + ]:          2 :               if (elf_compress (scn, 0, 0) < 0)
   13130                 :          0 :                 printf ("WARNING: %s [%zd]\n",
   13131                 :            :                         _("Couldn't uncompress section"),
   13132                 :            :                         elf_ndxscn (scn));
   13133                 :            :             }
   13134         [ +  - ]:          2 :           else if (startswith (name, ".zdebug"))
   13135                 :            :             {
   13136         [ -  + ]:          2 :               if (elf_compress_gnu (scn, 0, 0) < 0)
   13137                 :          0 :                 printf ("WARNING: %s [%zd]\n",
   13138                 :            :                         _("Couldn't uncompress section"),
   13139                 :            :                         elf_ndxscn (scn));
   13140                 :            :             }
   13141                 :            :         }
   13142                 :            : 
   13143                 :          6 :       Elf_Data *data = elf_rawdata (scn, NULL);
   13144         [ -  + ]:          6 :       if (data == NULL)
   13145                 :          0 :         error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
   13146                 :            :                elf_ndxscn (scn), name, elf_errmsg (-1));
   13147                 :            :       else
   13148                 :            :         {
   13149         [ +  + ]:          6 :           if (data->d_size == shdr->sh_size)
   13150                 :          2 :             printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
   13151                 :            :                              " bytes at offset %#0" PRIx64 ":\n"),
   13152                 :            :                     elf_ndxscn (scn), name,
   13153                 :            :                     shdr->sh_size, shdr->sh_offset);
   13154                 :            :           else
   13155                 :          4 :             printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
   13156                 :            :                              " bytes (%zd uncompressed) at offset %#0"
   13157                 :            :                              PRIx64 ":\n"),
   13158                 :            :                     elf_ndxscn (scn), name,
   13159                 :            :                     shdr->sh_size, data->d_size, shdr->sh_offset);
   13160                 :          6 :           hex_dump (data->d_buf, data->d_size);
   13161                 :            :         }
   13162                 :            :     }
   13163                 :          6 : }
   13164                 :            : 
   13165                 :            : static void
   13166                 :        281 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
   13167                 :            : {
   13168   [ +  -  +  + ]:        281 :   if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
   13169                 :         27 :     printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
   13170                 :            :             elf_ndxscn (scn), name);
   13171                 :            :   else
   13172                 :            :     {
   13173         [ +  + ]:        254 :       if (print_decompress)
   13174                 :            :         {
   13175                 :            :           /* We try to decompress the section, but keep the old shdr around
   13176                 :            :              so we can show both the original shdr size and the uncompressed
   13177                 :            :              data size.  */
   13178         [ -  + ]:          1 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
   13179                 :            :             {
   13180         [ #  # ]:          0 :               if (elf_compress (scn, 0, 0) < 0)
   13181                 :          0 :                 printf ("WARNING: %s [%zd]\n",
   13182                 :            :                         _("Couldn't uncompress section"),
   13183                 :            :                         elf_ndxscn (scn));
   13184                 :            :             }
   13185         [ +  - ]:          1 :           else if (startswith (name, ".zdebug"))
   13186                 :            :             {
   13187         [ -  + ]:          1 :               if (elf_compress_gnu (scn, 0, 0) < 0)
   13188                 :          0 :                 printf ("WARNING: %s [%zd]\n",
   13189                 :            :                         _("Couldn't uncompress section"),
   13190                 :            :                         elf_ndxscn (scn));
   13191                 :            :             }
   13192                 :            :         }
   13193                 :            : 
   13194                 :        254 :       Elf_Data *data = elf_rawdata (scn, NULL);
   13195         [ -  + ]:        254 :       if (data == NULL)
   13196                 :          0 :         error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
   13197                 :            :                elf_ndxscn (scn), name, elf_errmsg (-1));
   13198                 :            :       else
   13199                 :            :         {
   13200         [ +  + ]:        254 :           if (data->d_size == shdr->sh_size)
   13201                 :        253 :             printf (_("\nString section [%zu] '%s' contains %" PRIu64
   13202                 :            :                              " bytes at offset %#0" PRIx64 ":\n"),
   13203                 :            :                     elf_ndxscn (scn), name,
   13204                 :            :                     shdr->sh_size, shdr->sh_offset);
   13205                 :            :           else
   13206                 :          1 :             printf (_("\nString section [%zu] '%s' contains %" PRIu64
   13207                 :            :                              " bytes (%zd uncompressed) at offset %#0"
   13208                 :            :                              PRIx64 ":\n"),
   13209                 :            :                     elf_ndxscn (scn), name,
   13210                 :            :                     shdr->sh_size, data->d_size, shdr->sh_offset);
   13211                 :            : 
   13212                 :        254 :           const char *start = data->d_buf;
   13213                 :        254 :           const char *const limit = start + data->d_size;
   13214                 :      28391 :           do
   13215                 :            :             {
   13216                 :      28391 :               const char *end = memchr (start, '\0', limit - start);
   13217                 :      28391 :               const size_t pos = start - (const char *) data->d_buf;
   13218         [ -  + ]:      28391 :               if (unlikely (end == NULL))
   13219                 :            :                 {
   13220                 :          0 :                   printf ("  [%6zx]- %.*s\n",
   13221                 :            :                           pos, (int) (limit - start), start);
   13222                 :            :                   break;
   13223                 :            :                 }
   13224                 :      28391 :               printf ("  [%6zx]  %s\n", pos, start);
   13225                 :      28391 :               start = end + 1;
   13226         [ +  + ]:      28391 :             } while (start < limit);
   13227                 :            :         }
   13228                 :            :     }
   13229                 :        281 : }
   13230                 :            : 
   13231                 :            : static void
   13232                 :        149 : for_each_section_argument (Elf *elf, const struct section_argument *list,
   13233                 :            :                            void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
   13234                 :            :                                          const char *name))
   13235                 :            : {
   13236                 :            :   /* Get the section header string table index.  */
   13237                 :        149 :   size_t shstrndx;
   13238         [ -  + ]:        149 :   if (elf_getshdrstrndx (elf, &shstrndx) < 0)
   13239                 :          0 :     error_exit (0, _("cannot get section header string table index"));
   13240                 :            : 
   13241         [ +  + ]:        582 :   for (const struct section_argument *a = list; a != NULL; a = a->next)
   13242                 :            :     {
   13243                 :        433 :       Elf_Scn *scn;
   13244                 :        433 :       GElf_Shdr shdr_mem;
   13245                 :        433 :       const char *name = NULL;
   13246                 :            : 
   13247                 :        433 :       char *endp = NULL;
   13248                 :        433 :       unsigned long int shndx = strtoul (a->arg, &endp, 0);
   13249   [ +  +  -  + ]:        433 :       if (endp != a->arg && *endp == '\0')
   13250                 :            :         {
   13251                 :          1 :           scn = elf_getscn (elf, shndx);
   13252         [ -  + ]:          1 :           if (scn == NULL)
   13253                 :            :             {
   13254                 :          0 :               error (0, 0, _("\nsection [%lu] does not exist"), shndx);
   13255                 :          0 :               continue;
   13256                 :            :             }
   13257                 :            : 
   13258         [ -  + ]:          1 :           if (gelf_getshdr (scn, &shdr_mem) == NULL)
   13259                 :          0 :             error_exit (0, _("cannot get section header: %s"),
   13260                 :            :                         elf_errmsg (-1));
   13261                 :          1 :           name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
   13262                 :          1 :           (*dump) (scn, &shdr_mem, name);
   13263                 :            :         }
   13264                 :            :       else
   13265                 :            :         {
   13266                 :            :           /* Need to look up the section by name.  */
   13267                 :            :           scn = NULL;
   13268                 :            :           bool found = false;
   13269         [ +  + ]:      12678 :           while ((scn = elf_nextscn (elf, scn)) != NULL)
   13270                 :            :             {
   13271         [ -  + ]:      12246 :               if (gelf_getshdr (scn, &shdr_mem) == NULL)
   13272                 :          0 :                 continue;
   13273                 :      12246 :               name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
   13274         [ -  + ]:      12246 :               if (name == NULL)
   13275                 :          0 :                 continue;
   13276         [ +  + ]:      12246 :               if (!strcmp (name, a->arg))
   13277                 :            :                 {
   13278                 :        286 :                   found = true;
   13279                 :        286 :                   (*dump) (scn, &shdr_mem, name);
   13280                 :            :                 }
   13281                 :            :             }
   13282                 :            : 
   13283   [ +  +  -  + ]:        432 :           if (unlikely (!found) && !a->implicit)
   13284                 :        433 :             error (0, 0, _("\nsection '%s' does not exist"), a->arg);
   13285                 :            :         }
   13286                 :            :     }
   13287                 :        149 : }
   13288                 :            : 
   13289                 :            : static void
   13290                 :          6 : dump_data (Ebl *ebl)
   13291                 :            : {
   13292                 :          6 :   for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
   13293                 :          6 : }
   13294                 :            : 
   13295                 :            : static void
   13296                 :        143 : dump_strings (Ebl *ebl)
   13297                 :            : {
   13298                 :        143 :   for_each_section_argument (ebl->elf, string_sections, &print_string_section);
   13299                 :        143 : }
   13300                 :            : 
   13301                 :            : static void
   13302                 :          0 : print_strings (Ebl *ebl)
   13303                 :            : {
   13304                 :            :   /* Get the section header string table index.  */
   13305                 :          0 :   size_t shstrndx;
   13306         [ #  # ]:          0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
   13307                 :          0 :     error_exit (0, _("cannot get section header string table index"));
   13308                 :            : 
   13309                 :            :   Elf_Scn *scn;
   13310                 :            :   GElf_Shdr shdr_mem;
   13311                 :            :   const char *name;
   13312                 :            :   scn = NULL;
   13313         [ #  # ]:          0 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   13314                 :            :     {
   13315         [ #  # ]:          0 :       if (gelf_getshdr (scn, &shdr_mem) == NULL)
   13316                 :          0 :         continue;
   13317                 :            : 
   13318         [ #  # ]:          0 :       if (shdr_mem.sh_type != SHT_PROGBITS
   13319         [ #  # ]:          0 :           || !(shdr_mem.sh_flags & SHF_STRINGS))
   13320                 :          0 :         continue;
   13321                 :            : 
   13322                 :          0 :       name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
   13323         [ #  # ]:          0 :       if (name == NULL)
   13324                 :          0 :         continue;
   13325                 :            : 
   13326                 :          0 :       print_string_section (scn, &shdr_mem, name);
   13327                 :            :     }
   13328                 :          0 : }
   13329                 :            : 
   13330                 :            : static void
   13331                 :          2 : dump_archive_index (Elf *elf, const char *fname)
   13332                 :            : {
   13333                 :          2 :   size_t narsym;
   13334                 :          2 :   const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
   13335         [ -  + ]:          2 :   if (arsym == NULL)
   13336                 :            :     {
   13337                 :          0 :       int result = elf_errno ();
   13338         [ #  # ]:          0 :       if (unlikely (result != ELF_E_NO_INDEX))
   13339                 :          0 :         error_exit (0, _("cannot get symbol index of archive '%s': %s"),
   13340                 :            :                     fname, elf_errmsg (result));
   13341                 :            :       else
   13342                 :          0 :         printf (_("\nArchive '%s' has no symbol index\n"), fname);
   13343                 :          0 :       return;
   13344                 :            :     }
   13345                 :            : 
   13346                 :          2 :   printf (_("\nIndex of archive '%s' has %zu entries:\n"),
   13347                 :            :           fname, narsym);
   13348                 :            : 
   13349                 :          2 :   size_t as_off = 0;
   13350         [ +  + ]:         11 :   for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
   13351                 :            :     {
   13352         [ +  + ]:          9 :       if (s->as_off != as_off)
   13353                 :            :         {
   13354                 :          6 :           as_off = s->as_off;
   13355                 :            : 
   13356                 :          6 :           Elf *subelf = NULL;
   13357         [ +  - ]:          6 :           if (unlikely (elf_rand (elf, as_off) == 0)
   13358         [ -  + ]:          6 :               || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
   13359                 :            :                            == NULL))
   13360                 :            : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
   13361                 :            :             while (1)
   13362                 :            : #endif
   13363                 :          0 :               error_exit (0,
   13364                 :            :                           _("cannot extract member at offset %zu in '%s': %s"),
   13365                 :            :                           as_off, fname, elf_errmsg (-1));
   13366                 :            : 
   13367                 :          6 :           const Elf_Arhdr *h = elf_getarhdr (subelf);
   13368                 :            : 
   13369                 :          6 :           printf (_("Archive member '%s' contains:\n"), h->ar_name);
   13370                 :            : 
   13371                 :          6 :           elf_end (subelf);
   13372                 :            :         }
   13373                 :            : 
   13374                 :          9 :       printf ("\t%s\n", s->as_name);
   13375                 :            :     }
   13376                 :            : }
   13377                 :            : 
   13378                 :            : #include "debugpred.h"

Generated by: LCOV version 1.14