LCOV - code coverage report
Current view: top level - src - readelf.c (source / functions) Coverage Total Hit
Test: elfutils-0.193 Lines: 67.1 % 7038 4721
Test Date: 2025-08-30 14:31:09 Functions: 89.9 % 159 143
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 52.2 % 4988 2606

             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                 :             :    Copyright (C) 2023, 2025 Mark J. Wielaard <mark@klomp.org>
       4                 :             :    This file is part of elfutils.
       5                 :             : 
       6                 :             :    This file is free software; you can redistribute it and/or modify
       7                 :             :    it under the terms of the GNU General Public License as published by
       8                 :             :    the Free Software Foundation; either version 3 of the License, or
       9                 :             :    (at your option) any later version.
      10                 :             : 
      11                 :             :    elfutils is distributed in the hope that it will be useful, but
      12                 :             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :             :    GNU General Public License for more details.
      15                 :             : 
      16                 :             :    You should have received a copy of the GNU General Public License
      17                 :             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      18                 :             : 
      19                 :             : #ifdef HAVE_CONFIG_H
      20                 :             : # include <config.h>
      21                 :             : #endif
      22                 :             : 
      23                 :             : #include <argp.h>
      24                 :             : #include <assert.h>
      25                 :             : #include <ctype.h>
      26                 :             : #include <dwarf.h>
      27                 :             : #include <errno.h>
      28                 :             : #include <fcntl.h>
      29                 :             : #include <gelf.h>
      30                 :             : #include <inttypes.h>
      31                 :             : #include <langinfo.h>
      32                 :             : #include <libdw.h>
      33                 :             : #include <libdwfl.h>
      34                 :             : #include <locale.h>
      35                 :             : #include <stdarg.h>
      36                 :             : #include <stdbool.h>
      37                 :             : #include <stdio.h>
      38                 :             : #include <stdio_ext.h>
      39                 :             : #include <stdlib.h>
      40                 :             : #include <string.h>
      41                 :             : #include <strings.h>
      42                 :             : #include <time.h>
      43                 :             : #include <unistd.h>
      44                 :             : #include <sys/stat.h>
      45                 :             : #include <signal.h>
      46                 :             : 
      47                 :             : #include <libeu.h>
      48                 :             : #include <system.h>
      49                 :             : #include <printversion.h>
      50                 :             : #include "../libelf/libelfP.h"
      51                 :             : #include "../libelf/common.h"
      52                 :             : #include "../libebl/libeblP.h"
      53                 :             : #include "../libdwelf/libdwelf.h"
      54                 :             : #include "../libdw/libdwP.h"
      55                 :             : #include "../libdwfl/libdwflP.h"
      56                 :             : #include "../libdw/memory-access.h"
      57                 :             : 
      58                 :             : #include "../libdw/known-dwarf.h"
      59                 :             : 
      60                 :             : #ifdef USE_LOCKS
      61                 :             : #include "threadlib.h"
      62                 :             : #endif
      63                 :             : 
      64                 :             : #ifdef HAVE_SCHED_H
      65                 :             : #include <sched.h>
      66                 :             : #endif
      67                 :             : 
      68                 :             : #ifdef HAVE_SYS_RESOURCE_H
      69                 :             : #include <sys/resource.h>
      70                 :             : #endif
      71                 :             : 
      72                 :             : #ifdef __linux__
      73                 :             : #define CORE_SIGILL  SIGILL
      74                 :             : #define CORE_SIGBUS  SIGBUS
      75                 :             : #define CORE_SIGFPE  SIGFPE
      76                 :             : #define CORE_SIGSEGV SIGSEGV
      77                 :             : #define CORE_SI_USER SI_USER
      78                 :             : #else
      79                 :             : /* We want the linux version of those as that is what shows up in the core files. */
      80                 :             : #define CORE_SIGILL  4  /* Illegal instruction (ANSI).  */
      81                 :             : #define CORE_SIGBUS  7  /* BUS error (4.2 BSD).  */
      82                 :             : #define CORE_SIGFPE  8  /* Floating-point exception (ANSI).  */
      83                 :             : #define CORE_SIGSEGV 11 /* Segmentation violation (ANSI).  */
      84                 :             : #define CORE_SI_USER 0  /* Sent by kill, sigsend.  */
      85                 :             : #endif
      86                 :             : 
      87                 :             : /* Name and version of program.  */
      88                 :             : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      89                 :             : 
      90                 :             : /* Bug report address.  */
      91                 :             : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      92                 :             : 
      93                 :             : /* argp key value for --elf-section, non-ascii.  */
      94                 :             : #define ELF_INPUT_SECTION 256
      95                 :             : 
      96                 :             : /* argp key value for --dwarf-skeleton, non-ascii.  */
      97                 :             : #define DWARF_SKELETON 257
      98                 :             : 
      99                 :             : /* argp key value for --dyn-syms, non-ascii.  */
     100                 :             : #define PRINT_DYNSYM_TABLE 258
     101                 :             : 
     102                 :             : /* Terrible hack for hooking unrelated skeleton/split compile units,
     103                 :             :    see __libdw_link_skel_split in print_debug.  */
     104                 :             : static bool do_not_close_dwfl = false;
     105                 :             : 
     106                 :             : /* Definitions of arguments for argp functions.  */
     107                 :             : static const struct argp_option options[] =
     108                 :             : {
     109                 :             :   { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 },
     110                 :             :   { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL,
     111                 :             :     N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF "
     112                 :             :        "input data"), 0 },
     113                 :             :   { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0,
     114                 :             :     N_("Used with -w to find the skeleton Compile Units in FILE associated "
     115                 :             :        "with the Split Compile units in a .dwo input file"), 0 },
     116                 :             :   { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 },
     117                 :             :   { "all", 'a', NULL, 0,
     118                 :             :     N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 },
     119                 :             :   { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 },
     120                 :             :   { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 },
     121                 :             :   { "histogram", 'I', NULL, 0,
     122                 :             :     N_("Display histogram of bucket list lengths"), 0 },
     123                 :             :   { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 },
     124                 :             :   { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     125                 :             :   { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
     126                 :             :   { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 },
     127                 :             :   { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
     128                 :             :   { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     129                 :             :   { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
     130                 :             :     N_("Display the symbol table sections"), 0 },
     131                 :             :   { "syms", 's', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     132                 :             :   { "dyn-syms", PRINT_DYNSYM_TABLE, NULL, 0,
     133                 :             :     N_("Display (only) the dynamic symbol table"), 0 },
     134                 :             :   { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
     135                 :             :   { "notes", 'n', "SECTION", OPTION_ARG_OPTIONAL, N_("Display the ELF notes"), 0 },
     136                 :             :   { "arch-specific", 'A', NULL, 0,
     137                 :             :     N_("Display architecture specific information, if any"), 0 },
     138                 :             :   { "exception", 'e', NULL, 0,
     139                 :             :     N_("Display sections for exception handling"), 0 },
     140                 :             : 
     141                 :             :   { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
     142                 :             :   { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
     143                 :             :     N_("Display DWARF section content.  SECTION can be one of abbrev, addr, "
     144                 :             :        "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, "
     145                 :             :        "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 },
     146                 :             :   { "hex-dump", 'x', "SECTION", 0,
     147                 :             :     N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
     148                 :             :   { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
     149                 :             :     N_("Print string contents of sections"), 0 },
     150                 :             :   { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
     151                 :             :   { "archive-index", 'c', NULL, 0,
     152                 :             :     N_("Display the symbol index of an archive"), 0 },
     153                 :             :   { "use-dynamic", 'D', NULL, 0,
     154                 :             :     N_("Use the dynamic segment when possible for displaying info"), 0 },
     155                 :             : 
     156                 :             :   { NULL, 0, NULL, 0, N_("Output control:"), 0 },
     157                 :             :   { "numeric-addresses", 'N', NULL, 0,
     158                 :             :     N_("Do not find symbol names for addresses in DWARF data"), 0 },
     159                 :             :   { "unresolved-address-offsets", 'U', NULL, 0,
     160                 :             :     N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
     161                 :             :   { "wide", 'W', NULL, 0,
     162                 :             :     N_("Ignored for compatibility (lines always wide)"), 0 },
     163                 :             :   { "decompress", 'z', NULL, 0,
     164                 :             :     N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
     165                 :             : #ifdef USE_LOCKS
     166                 :             :   { "concurrency", 'C', "NUM", 0,
     167                 :             :     N_("Set maximum number of threads. Defaults to the number of CPUs."), 0 },
     168                 :             : #endif
     169                 :             :   { NULL, 0, NULL, 0, NULL, 0 }
     170                 :             : };
     171                 :             : 
     172                 :             : /* Short description of program.  */
     173                 :             : static const char doc[] = N_("\
     174                 :             : Print information from ELF file in human-readable form.");
     175                 :             : 
     176                 :             : /* Strings for arguments in help texts.  */
     177                 :             : static const char args_doc[] = N_("FILE...");
     178                 :             : 
     179                 :             : /* Prototype for option handler.  */
     180                 :             : static error_t parse_opt (int key, char *arg, struct argp_state *state);
     181                 :             : 
     182                 :             : /* Data structure to communicate with argp functions.  */
     183                 :             : static struct argp argp =
     184                 :             : {
     185                 :             :   options, parse_opt, args_doc, doc, NULL, NULL, NULL
     186                 :             : };
     187                 :             : 
     188                 :             : /* If non-null, the section from which we should read to (compressed) ELF.  */
     189                 :             : static const char *elf_input_section = NULL;
     190                 :             : 
     191                 :             : /* If non-null, the file that contains the skeleton CUs.  */
     192                 :             : static const char *dwarf_skeleton = NULL;
     193                 :             : 
     194                 :             : /* Flags set by the option controlling the output.  */
     195                 :             : 
     196                 :             : /* True if dynamic segment should be printed.  */
     197                 :             : static bool print_dynamic_table;
     198                 :             : 
     199                 :             : /* True if the file header should be printed.  */
     200                 :             : static bool print_file_header;
     201                 :             : 
     202                 :             : /* True if the program headers should be printed.  */
     203                 :             : static bool print_program_header;
     204                 :             : 
     205                 :             : /* True if relocations should be printed.  */
     206                 :             : static bool print_relocations;
     207                 :             : 
     208                 :             : /* True if the section headers should be printed.  */
     209                 :             : static bool print_section_header;
     210                 :             : 
     211                 :             : /* True if the symbol table should be printed.  */
     212                 :             : static bool print_symbol_table;
     213                 :             : 
     214                 :             : /* True if (only) the dynsym table should be printed.  */
     215                 :             : static bool print_dynsym_table;
     216                 :             : 
     217                 :             : /* True if reconstruct dynamic symbol table from the PT_DYNAMIC segment.  */
     218                 :             : static bool use_dynamic_segment;
     219                 :             : 
     220                 :             : /* A specific section name, or NULL to print all symbol tables.  */
     221                 :             : static char *symbol_table_section;
     222                 :             : 
     223                 :             : /* A specific section name, or NULL to print all ELF notes.  */
     224                 :             : static char *notes_section;
     225                 :             : 
     226                 :             : /* True if the version information should be printed.  */
     227                 :             : static bool print_version_info;
     228                 :             : 
     229                 :             : /* True if section groups should be printed.  */
     230                 :             : static bool print_section_groups;
     231                 :             : 
     232                 :             : /* True if bucket list length histogram should be printed.  */
     233                 :             : static bool print_histogram;
     234                 :             : 
     235                 :             : /* True if the architecture specific data should be printed.  */
     236                 :             : static bool print_arch;
     237                 :             : 
     238                 :             : /* True if note section content should be printed.  */
     239                 :             : static bool print_notes;
     240                 :             : 
     241                 :             : /* True if SHF_STRINGS section content should be printed.  */
     242                 :             : static bool print_string_sections;
     243                 :             : 
     244                 :             : /* True if archive index should be printed.  */
     245                 :             : static bool print_archive_index;
     246                 :             : 
     247                 :             : /* True if any of the control options except print_archive_index is set.  */
     248                 :             : static bool any_control_option;
     249                 :             : 
     250                 :             : /* True if we should print addresses from DWARF in symbolic form.  */
     251                 :             : static bool print_address_names = true;
     252                 :             : 
     253                 :             : /* True if we should print raw values instead of relativized addresses.  */
     254                 :             : static bool print_unresolved_addresses = false;
     255                 :             : 
     256                 :             : /* True if we should print the .debug_aranges section using libdw.  */
     257                 :             : static bool decodedaranges = false;
     258                 :             : 
     259                 :             : /* True if we should print the .debug_aranges section using libdw.  */
     260                 :             : static bool decodedline = false;
     261                 :             : 
     262                 :             : /* True if we want to show more information about compressed sections.  */
     263                 :             : static bool print_decompress = false;
     264                 :             : 
     265                 :             : /* True if we want to show split compile units for debug_info skeletons.  */
     266                 :             : static bool show_split_units = false;
     267                 :             : 
     268                 :             : #if USE_LOCKS
     269                 :             : /* Maximum number of threads.  */
     270                 :             : static int max_threads = 0;
     271                 :             : #endif
     272                 :             : 
     273                 :             : /* Select printing of debugging sections.  */
     274                 :             : static enum section_e
     275                 :             : {
     276                 :             :   section_abbrev = 1,           /* .debug_abbrev  */
     277                 :             :   section_aranges = 2,          /* .debug_aranges  */
     278                 :             :   section_frame = 4,            /* .debug_frame or .eh_frame & al.  */
     279                 :             :   section_info = 8,             /* .debug_info, (implies .debug_types)  */
     280                 :             :   section_line = 16,            /* .debug_line  */
     281                 :             :   section_loc = 32,             /* .debug_loc  */
     282                 :             :   section_pubnames = 64,        /* .debug_pubnames  */
     283                 :             :   section_str = 128,            /* .debug_str  */
     284                 :             :   section_macinfo = 256,        /* .debug_macinfo  */
     285                 :             :   section_ranges = 512,         /* .debug_ranges  */
     286                 :             :   section_exception = 1024,     /* .eh_frame & al.  */
     287                 :             :   section_gdb_index = 2048,     /* .gdb_index  */
     288                 :             :   section_macro = 4096,         /* .debug_macro  */
     289                 :             :   section_addr = 8192,          /* .debug_addr  */
     290                 :             :   section_types = 16384,        /* .debug_types (implied by .debug_info)  */
     291                 :             :   section_all = (section_abbrev | section_aranges | section_frame
     292                 :             :                  | section_info | section_line | section_loc
     293                 :             :                  | section_pubnames | section_str | section_macinfo
     294                 :             :                  | section_ranges | section_exception | section_gdb_index
     295                 :             :                  | section_macro | section_addr | section_types)
     296                 :             : } print_debug_sections, implicit_debug_sections;
     297                 :             : 
     298                 :             : /* Select hex dumping of sections.  */
     299                 :             : static struct section_argument *dump_data_sections;
     300                 :             : static struct section_argument **dump_data_sections_tail = &dump_data_sections;
     301                 :             : 
     302                 :             : /* Select string dumping of sections.  */
     303                 :             : static struct section_argument *string_sections;
     304                 :             : static struct section_argument **string_sections_tail = &string_sections;
     305                 :             : 
     306                 :             : struct section_argument
     307                 :             : {
     308                 :             :   struct section_argument *next;
     309                 :             :   const char *arg;
     310                 :             :   bool implicit;
     311                 :             : };
     312                 :             : 
     313                 :             : /* Numbers of sections and program headers in the file.  */
     314                 :             : static size_t shnum;
     315                 :             : static size_t phnum;
     316                 :             : 
     317                 :             : 
     318                 :             : /* Declarations of local functions.  */
     319                 :             : static void process_file (int fd, const char *fname, bool only_one);
     320                 :             : static void process_elf_file (Dwfl_Module *dwflmod, int fd);
     321                 :             : static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
     322                 :             : static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
     323                 :             : static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
     324                 :             : static void print_scngrp (Ebl *ebl);
     325                 :             : static void print_dynamic (Ebl *ebl);
     326                 :             : static void print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr);
     327                 :             : static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
     328                 :             :                                GElf_Shdr *shdr);
     329                 :             : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
     330                 :             :                                 GElf_Shdr *shdr);
     331                 :             : static void handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn,
     332                 :             :                                 GElf_Shdr *shdr);
     333                 :             : static bool print_symtab (Ebl *ebl, int type);
     334                 :             : static bool handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     335                 :             : static bool handle_dynamic_symtab (Ebl *ebl);
     336                 :             : static void
     337                 :             : process_symtab(
     338                 :             :         Ebl * ebl,
     339                 :             :         unsigned int nsyms,
     340                 :             :         Elf64_Word idx,
     341                 :             :         Elf32_Word verneed_stridx,
     342                 :             :         Elf32_Word verdef_stridx,
     343                 :             :         Elf_Data * symdata,
     344                 :             :         Elf_Data * versym_data,
     345                 :             :         Elf_Data * symstr_data,
     346                 :             :         Elf_Data * verneed_data,
     347                 :             :         Elf_Data * verdef_data,
     348                 :             :         Elf_Data * xndx_data);
     349                 :             : static void print_verinfo (Ebl *ebl);
     350                 :             : static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     351                 :             : static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
     352                 :             : static void handle_versym (Ebl *ebl, Elf_Scn *scn,
     353                 :             :                            GElf_Shdr *shdr);
     354                 :             : static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
     355                 :             : static void handle_hash (Ebl *ebl);
     356                 :             : static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
     357                 :             : static void print_liblist (Ebl *ebl);
     358                 :             : static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
     359                 :             : static void dump_data (Ebl *ebl);
     360                 :             : static void dump_strings (Ebl *ebl);
     361                 :             : static void print_strings (Ebl *ebl);
     362                 :             : static void dump_archive_index (Elf *, const char *);
     363                 :             : static void print_dwarf_addr (Dwfl_Module *dwflmod, int address_size,
     364                 :             :                               Dwarf_Addr address, Dwarf_Addr raw, FILE *out);
     365                 :             : static void print_flag_info(void);
     366                 :             : 
     367                 :             : enum dyn_idx
     368                 :             : {
     369                 :             :   i_symtab_shndx,
     370                 :             :   i_strsz,
     371                 :             :   i_verneed,
     372                 :             :   i_verneednum,
     373                 :             :   i_verdef,
     374                 :             :   i_verdefnum,
     375                 :             :   i_versym,
     376                 :             :   i_symtab,
     377                 :             :   i_strtab,
     378                 :             :   i_hash,
     379                 :             :   i_gnu_hash,
     380                 :             :   i_max
     381                 :             : };
     382                 :             : 
     383                 :             : /* Declarations of local functions for use-dynamic.  */
     384                 :             : static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr);
     385                 :             : static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]);
     386                 :             : static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n,
     387                 :             :                           GElf_Addr addrs[n], GElf_Off offs[n]);
     388                 :             : 
     389                 :             : /* Looked up once with gettext in main.  */
     390                 :             : static char *yes_str;
     391                 :             : static char *no_str;
     392                 :             : 
     393                 :             : static void
     394                 :        1780 : cleanup_list (struct section_argument *list)
     395                 :             : {
     396   [ +  +  +  + ]:        2346 :   while (list != NULL)
     397                 :             :     {
     398                 :         566 :       struct section_argument *a = list;
     399                 :         566 :       list = a->next;
     400                 :         566 :       free (a);
     401                 :             :     }
     402                 :             : }
     403                 :             : 
     404                 :             : #ifdef USE_LOCKS
     405                 :             : /* Estimate the maximum number of threads. This is normally
     406                 :             :    #CPU.  Return value is guaranteed to be at least 1.  */
     407                 :             : static int
     408                 :             : default_concurrency (void)
     409                 :             : {
     410                 :             :   unsigned aff = 0;
     411                 :             : #ifdef HAVE_SCHED_GETAFFINITY
     412                 :             :   {
     413                 :             :     int ret;
     414                 :             :     cpu_set_t mask;
     415                 :             :     CPU_ZERO (&mask);
     416                 :             :     ret = sched_getaffinity (0, sizeof(mask), &mask);
     417                 :             :     if (ret == 0)
     418                 :             :       aff = CPU_COUNT (&mask);
     419                 :             :   }
     420                 :             : #endif
     421                 :             : 
     422                 :             :   unsigned fn = 0;
     423                 :             : #ifdef HAVE_GETRLIMIT
     424                 :             :   {
     425                 :             :     struct rlimit rlim;
     426                 :             :     int rc = getrlimit (RLIMIT_NOFILE, &rlim);
     427                 :             :     if (rc == 0)
     428                 :             :       fn = MAX ((rlim_t) 1, (rlim.rlim_cur - 100) / 2);
     429                 :             :     /* Conservatively estimate that at least 2 fds are used
     430                 :             :        by each thread.  */
     431                 :             :   }
     432                 :             : #endif
     433                 :             : 
     434                 :             :   unsigned d = MIN (MAX (aff, 1U),
     435                 :             :                     MAX (fn, 1U));
     436                 :             : 
     437                 :             :   return d;
     438                 :             : }
     439                 :             : #endif
     440                 :             : 
     441                 :             : int
     442                 :         890 : main (int argc, char *argv[])
     443                 :             : {
     444                 :             :   /* We use no threads here which can interfere with handling a stream.  */
     445                 :         890 :   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
     446                 :             : 
     447                 :             :   /* Set locale.  */
     448                 :         890 :   setlocale (LC_ALL, "");
     449                 :             : 
     450                 :             :   /* Initialize the message catalog.  */
     451                 :         890 :   textdomain (PACKAGE_TARNAME);
     452                 :             : 
     453                 :             :   /* Look up once.  */
     454                 :         890 :   yes_str = _("yes");
     455                 :         890 :   no_str = _("no");
     456                 :             : 
     457                 :             :   /* Parse and process arguments.  */
     458                 :         890 :   int remaining;
     459                 :         890 :   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
     460                 :             : 
     461                 :             :   /* Before we start tell the ELF library which version we are using.  */
     462                 :         890 :   elf_version (EV_CURRENT);
     463                 :             : 
     464                 :             : #ifdef USE_LOCKS
     465                 :             :   /* If concurrency wasn't set by argp_parse, then set a default value.  */
     466                 :             :   if (max_threads == 0)
     467                 :             :     max_threads = default_concurrency ();
     468                 :             : #endif
     469                 :             : 
     470                 :             :   /* Now process all the files given at the command line.  */
     471                 :         890 :   bool only_one = remaining + 1 == argc;
     472                 :         972 :   do
     473                 :             :     {
     474                 :             :       /* Open the file.  */
     475                 :         972 :       int fd = open (argv[remaining], O_RDONLY);
     476         [ -  + ]:         972 :       if (fd == -1)
     477                 :             :         {
     478                 :           0 :           error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
     479                 :           0 :           continue;
     480                 :             :         }
     481                 :             : 
     482                 :         972 :       process_file (fd, argv[remaining], only_one);
     483                 :             : 
     484                 :         972 :       close (fd);
     485                 :             :     }
     486         [ +  + ]:         972 :   while (++remaining < argc);
     487                 :             : 
     488                 :         890 :   cleanup_list (dump_data_sections);
     489                 :         890 :   cleanup_list (string_sections);
     490                 :             : 
     491                 :         890 :   return error_message_count != 0;
     492                 :             : }
     493                 :             : 
     494                 :             : static void
     495                 :         566 : add_dump_section (const char *name,
     496                 :             :                   int key,
     497                 :             :                   bool implicit)
     498                 :             : {
     499                 :         566 :   struct section_argument *a = xmalloc (sizeof *a);
     500                 :         566 :   a->arg = name;
     501                 :         566 :   a->next = NULL;
     502                 :         566 :   a->implicit = implicit;
     503                 :        1132 :   struct section_argument ***tailp
     504         [ +  + ]:         566 :     = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
     505                 :         566 :   **tailp = a;
     506                 :         566 :   *tailp = &a->next;
     507                 :         566 : }
     508                 :             : 
     509                 :             : /* Handle program arguments.  */
     510                 :             : static error_t
     511                 :        5662 : parse_opt (int key, char *arg,
     512                 :             :            struct argp_state *state __attribute__ ((unused)))
     513                 :             : {
     514   [ +  +  -  +  :        5662 :   switch (key)
          +  -  +  +  -  
          +  +  +  +  +  
          +  -  +  +  +  
          +  +  +  -  +  
             +  +  +  + ]
     515                 :             :     {
     516                 :         184 :     case 'a':
     517                 :         184 :       print_file_header = true;
     518                 :         184 :       print_program_header = true;
     519                 :         184 :       print_relocations = true;
     520                 :         184 :       print_section_header = true;
     521                 :         184 :       print_symbol_table = true;
     522                 :         184 :       print_version_info = true;
     523                 :         184 :       print_dynamic_table = true;
     524                 :         184 :       print_section_groups = true;
     525                 :         184 :       print_histogram = true;
     526                 :         184 :       print_arch = true;
     527                 :         184 :       print_notes = true;
     528                 :         184 :       implicit_debug_sections |= section_exception;
     529                 :         184 :       add_dump_section (".strtab", key, true);
     530                 :         184 :       add_dump_section (".dynstr", key, true);
     531                 :         184 :       add_dump_section (".comment", key, true);
     532                 :         184 :       any_control_option = true;
     533                 :         184 :       break;
     534                 :           8 :     case 'A':
     535                 :           8 :       print_arch = true;
     536                 :           8 :       any_control_option = true;
     537                 :           8 :       break;
     538                 :           6 :     case 'd':
     539                 :           6 :       print_dynamic_table = true;
     540                 :           6 :       any_control_option = true;
     541                 :           6 :       break;
     542                 :           2 :     case 'D':
     543                 :           2 :       use_dynamic_segment = true;
     544                 :           2 :       break;
     545                 :           0 :     case 'e':
     546                 :           0 :       print_debug_sections |= section_exception;
     547                 :           0 :       any_control_option = true;
     548                 :           0 :       break;
     549                 :          18 :     case 'g':
     550                 :          18 :       print_section_groups = true;
     551                 :          18 :       any_control_option = true;
     552                 :          18 :       break;
     553                 :           6 :     case 'h':
     554                 :           6 :       print_file_header = true;
     555                 :           6 :       any_control_option = true;
     556                 :           6 :       break;
     557                 :           0 :     case 'I':
     558                 :           0 :       print_histogram = true;
     559                 :           0 :       any_control_option = true;
     560                 :           0 :       break;
     561                 :           4 :     case 'l':
     562                 :           4 :       print_program_header = true;
     563                 :           4 :       any_control_option = true;
     564                 :           4 :       break;
     565                 :          46 :     case 'n':
     566                 :          46 :       print_notes = true;
     567                 :          46 :       any_control_option = true;
     568                 :          46 :       notes_section = arg;
     569                 :          46 :       break;
     570                 :          10 :     case 'r':
     571                 :          10 :       print_relocations = true;
     572                 :          10 :       any_control_option = true;
     573                 :          10 :      break;
     574                 :         322 :     case 'S':
     575                 :         322 :       print_section_header = true;
     576                 :         322 :       any_control_option = true;
     577                 :         322 :       break;
     578                 :          34 :     case 's':
     579                 :          34 :       print_symbol_table = true;
     580                 :          34 :       any_control_option = true;
     581                 :          34 :       symbol_table_section = arg;
     582                 :          34 :       break;
     583                 :           2 :     case PRINT_DYNSYM_TABLE:
     584                 :           2 :       print_dynsym_table = true;
     585                 :           2 :       any_control_option = true;
     586                 :           2 :       break;
     587                 :           0 :     case 'V':
     588                 :           0 :       print_version_info = true;
     589                 :           0 :       any_control_option = true;
     590                 :           0 :       break;
     591                 :           4 :     case 'c':
     592                 :           4 :       print_archive_index = true;
     593                 :           4 :       break;
     594                 :             : #if USE_LOCKS
     595                 :             :     case 'C':
     596                 :             :       if (arg != NULL)
     597                 :             :         {
     598                 :             :           max_threads = atoi (arg);
     599                 :             :           if (max_threads < 1)
     600                 :             :             {
     601                 :             :               argp_error (state, _("-C NUM minimum 1"));
     602                 :             :               return EINVAL;
     603                 :             :             }
     604                 :             :         }
     605                 :             :       break;
     606                 :             : #endif
     607                 :         264 :     case 'w':
     608         [ +  + ]:         264 :       if (arg == NULL)
     609                 :             :         {
     610                 :          86 :           print_debug_sections = section_all;
     611                 :          86 :           implicit_debug_sections = section_info;
     612                 :          86 :           show_split_units = true;
     613                 :             :         }
     614         [ -  + ]:         178 :       else if (strcmp (arg, "abbrev") == 0)
     615                 :           0 :         print_debug_sections |= section_abbrev;
     616         [ +  + ]:         178 :       else if (strcmp (arg, "addr") == 0)
     617                 :             :         {
     618                 :           4 :           print_debug_sections |= section_addr;
     619                 :           4 :           implicit_debug_sections |= section_info;
     620                 :             :         }
     621         [ +  + ]:         174 :       else if (strcmp (arg, "aranges") == 0)
     622                 :           6 :         print_debug_sections |= section_aranges;
     623         [ +  + ]:         168 :       else if (strcmp (arg, "decodedaranges") == 0)
     624                 :             :         {
     625                 :           2 :           print_debug_sections |= section_aranges;
     626                 :           2 :           decodedaranges = true;
     627                 :             :         }
     628         [ +  + ]:         166 :       else if (strcmp (arg, "ranges") == 0)
     629                 :             :         {
     630                 :          26 :           print_debug_sections |= section_ranges;
     631                 :          26 :           implicit_debug_sections |= section_info;
     632                 :             :         }
     633   [ +  +  +  + ]:         140 :       else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
     634                 :           6 :         print_debug_sections |= section_frame;
     635         [ +  + ]:         134 :       else if (strcmp (arg, "info") == 0)
     636                 :             :         {
     637                 :          40 :           print_debug_sections |= section_info;
     638                 :          40 :           print_debug_sections |= section_types;
     639                 :             :         }
     640         [ +  + ]:          94 :       else if (strcmp (arg, "info+") == 0)
     641                 :             :         {
     642                 :           4 :           print_debug_sections |= section_info;
     643                 :           4 :           print_debug_sections |= section_types;
     644                 :           4 :           show_split_units = true;
     645                 :             :         }
     646         [ +  + ]:          90 :       else if (strcmp (arg, "loc") == 0)
     647                 :             :         {
     648                 :          38 :           print_debug_sections |= section_loc;
     649                 :          38 :           implicit_debug_sections |= section_info;
     650                 :             :         }
     651         [ +  + ]:          52 :       else if (strcmp (arg, "line") == 0)
     652                 :          16 :         print_debug_sections |= section_line;
     653         [ +  + ]:          36 :       else if (strcmp (arg, "decodedline") == 0)
     654                 :             :         {
     655                 :          14 :           print_debug_sections |= section_line;
     656                 :          14 :           decodedline = true;
     657                 :             :         }
     658         [ -  + ]:          22 :       else if (strcmp (arg, "pubnames") == 0)
     659                 :           0 :         print_debug_sections |= section_pubnames;
     660         [ +  + ]:          22 :       else if (strcmp (arg, "str") == 0)
     661                 :             :         {
     662                 :           6 :           print_debug_sections |= section_str;
     663                 :             :           /* For mapping string offset tables to CUs.  */
     664                 :           6 :           implicit_debug_sections |= section_info;
     665                 :             :         }
     666         [ -  + ]:          16 :       else if (strcmp (arg, "macinfo") == 0)
     667                 :           0 :         print_debug_sections |= section_macinfo;
     668         [ +  + ]:          16 :       else if (strcmp (arg, "macro") == 0)
     669                 :           8 :         print_debug_sections |= section_macro;
     670         [ -  + ]:           8 :       else if (strcmp (arg, "exception") == 0)
     671                 :           0 :         print_debug_sections |= section_exception;
     672         [ +  - ]:           8 :       else if (strcmp (arg, "gdb_index") == 0)
     673                 :           8 :         print_debug_sections |= section_gdb_index;
     674                 :             :       else
     675                 :             :         {
     676                 :           0 :           fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
     677                 :             :                    arg);
     678                 :           0 :           argp_help (&argp, stderr, ARGP_HELP_SEE,
     679                 :             :                      program_invocation_short_name);
     680                 :           0 :           exit (1);
     681                 :             :         }
     682                 :         264 :       any_control_option = true;
     683                 :         264 :       break;
     684                 :           2 :     case 'p':
     685                 :           2 :       any_control_option = true;
     686         [ -  + ]:           2 :       if (arg == NULL)
     687                 :             :         {
     688                 :           0 :           print_string_sections = true;
     689                 :           0 :           break;
     690                 :             :         }
     691                 :          14 :       FALLTHROUGH;
     692                 :             :     case 'x':
     693                 :          14 :       add_dump_section (arg, key, false);
     694                 :          14 :       any_control_option = true;
     695                 :          14 :       break;
     696                 :          90 :     case 'N':
     697                 :          90 :       print_address_names = false;
     698                 :          90 :       break;
     699                 :          56 :     case 'U':
     700                 :          56 :       print_unresolved_addresses = true;
     701                 :          56 :       break;
     702                 :           0 :     case ARGP_KEY_NO_ARGS:
     703                 :           0 :       fputs (_("Missing file name.\n"), stderr);
     704                 :           0 :       goto do_argp_help;
     705                 :         890 :     case ARGP_KEY_FINI:
     706   [ +  +  +  - ]:         890 :       if (! any_control_option && ! print_archive_index)
     707                 :             :         {
     708                 :           0 :           fputs (_("No operation specified.\n"), stderr);
     709                 :           0 :         do_argp_help:
     710                 :           0 :           argp_help (&argp, stderr, ARGP_HELP_SEE,
     711                 :             :                      program_invocation_short_name);
     712                 :           0 :           exit (EXIT_FAILURE);
     713                 :             :         }
     714                 :             :       break;
     715                 :             :     case 'W':                   /* Ignored.  */
     716                 :             :       break;
     717                 :         122 :     case 'z':
     718                 :         122 :       print_decompress = true;
     719                 :         122 :       break;
     720                 :          10 :     case ELF_INPUT_SECTION:
     721         [ +  - ]:          10 :       if (arg == NULL)
     722                 :          10 :         elf_input_section = ".gnu_debugdata";
     723                 :             :       else
     724                 :           0 :         elf_input_section = arg;
     725                 :             :       break;
     726                 :          10 :     case DWARF_SKELETON:
     727                 :          10 :       dwarf_skeleton = arg;
     728                 :          10 :       break;
     729                 :             :     default:
     730                 :             :       return ARGP_ERR_UNKNOWN;
     731                 :             :     }
     732                 :             :   return 0;
     733                 :             : }
     734                 :             : 
     735                 :             : 
     736                 :             : /* Create a file descriptor to read the data from the
     737                 :             :    elf_input_section given a file descriptor to an ELF file.  */
     738                 :             : static int
     739                 :          10 : open_input_section (int fd)
     740                 :             : {
     741                 :          10 :   size_t shnums;
     742                 :          10 :   size_t cnt;
     743                 :          10 :   size_t shstrndx;
     744                 :          10 :   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
     745         [ -  + ]:          10 :   if (elf == NULL)
     746                 :             :     {
     747                 :           0 :       error (0, 0, _("cannot generate Elf descriptor: %s"),
     748                 :             :              elf_errmsg (-1));
     749                 :           0 :       return -1;
     750                 :             :     }
     751                 :             : 
     752         [ -  + ]:          10 :   if (elf_getshdrnum (elf, &shnums) < 0)
     753                 :             :     {
     754                 :           0 :       error (0, 0, _("cannot determine number of sections: %s"),
     755                 :             :              elf_errmsg (-1));
     756                 :           0 :     open_error:
     757                 :           0 :       elf_end (elf);
     758                 :           0 :       return -1;
     759                 :             :     }
     760                 :             : 
     761         [ -  + ]:          10 :   if (elf_getshdrstrndx (elf, &shstrndx) < 0)
     762                 :             :     {
     763                 :           0 :       error (0, 0, _("cannot get section header string table index"));
     764                 :           0 :       goto open_error;
     765                 :             :     }
     766                 :             : 
     767         [ +  - ]:         236 :   for (cnt = 0; cnt < shnums; ++cnt)
     768                 :             :     {
     769                 :         236 :       Elf_Scn *scn = elf_getscn (elf, cnt);
     770         [ -  + ]:         236 :       if (scn == NULL)
     771                 :             :         {
     772                 :           0 :           error (0, 0, _("cannot get section: %s"),
     773                 :             :                  elf_errmsg (-1));
     774                 :           0 :           goto open_error;
     775                 :             :         }
     776                 :             : 
     777                 :         236 :       GElf_Shdr shdr_mem;
     778                 :         236 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     779         [ -  + ]:         236 :       if (unlikely (shdr == NULL))
     780                 :             :         {
     781                 :           0 :           error (0, 0, _("cannot get section header: %s"),
     782                 :             :                  elf_errmsg (-1));
     783                 :           0 :           goto open_error;
     784                 :             :         }
     785                 :             : 
     786                 :         236 :       const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
     787         [ -  + ]:         236 :       if (sname == NULL)
     788                 :             :         {
     789                 :           0 :           error (0, 0, _("cannot get section name"));
     790                 :           0 :           goto open_error;
     791                 :             :         }
     792                 :             : 
     793         [ +  + ]:         236 :       if (strcmp (sname, elf_input_section) == 0)
     794                 :             :         {
     795                 :          10 :           Elf_Data *data = elf_rawdata (scn, NULL);
     796         [ -  + ]:          10 :           if (data == NULL)
     797                 :             :             {
     798                 :           0 :               error (0, 0, _("cannot get %s content: %s"),
     799                 :             :                      sname, elf_errmsg (-1));
     800                 :           0 :               goto open_error;
     801                 :             :             }
     802                 :             : 
     803                 :             :           /* Create (and immediately unlink) a temporary file to store
     804                 :             :              section data in to create a file descriptor for it.  */
     805         [ +  - ]:          10 :           const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
     806                 :          10 :           static const char suffix[] = "/readelfXXXXXX";
     807                 :          10 :           int tmplen = strlen (tmpdir) + sizeof (suffix);
     808                 :          10 :           char *tempname = alloca (tmplen);
     809                 :          10 :           sprintf (tempname, "%s%s", tmpdir, suffix);
     810                 :             : 
     811                 :          10 :           int sfd = mkstemp (tempname);
     812         [ -  + ]:          10 :           if (sfd == -1)
     813                 :             :             {
     814                 :           0 :               error (0, 0, _("cannot create temp file '%s'"),
     815                 :             :                      tempname);
     816                 :           0 :               goto open_error;
     817                 :             :             }
     818                 :          10 :           unlink (tempname);
     819                 :             : 
     820                 :          10 :           ssize_t size = data->d_size;
     821         [ -  + ]:          10 :           if (write_retry (sfd, data->d_buf, size) != size)
     822                 :             :             {
     823                 :           0 :               error (0, 0, _("cannot write section data"));
     824                 :           0 :               goto open_error;
     825                 :             :             }
     826                 :             : 
     827         [ -  + ]:          10 :           if (elf_end (elf) != 0)
     828                 :             :             {
     829                 :           0 :               error (0, 0, _("error while closing Elf descriptor: %s"),
     830                 :             :                      elf_errmsg (-1));
     831                 :          10 :               return -1;
     832                 :             :             }
     833                 :             : 
     834         [ -  + ]:          10 :           if (lseek (sfd, 0, SEEK_SET) == -1)
     835                 :             :             {
     836                 :           0 :               error (0, 0, _("error while rewinding file descriptor"));
     837                 :           0 :               return -1;
     838                 :             :             }
     839                 :             : 
     840                 :             :           return sfd;
     841                 :             :         }
     842                 :             :     }
     843                 :             : 
     844                 :             :   /* Named section not found.  */
     845         [ #  # ]:           0 :   if (elf_end (elf) != 0)
     846                 :           0 :     error (0, 0, _("error while closing Elf descriptor: %s"),
     847                 :             :            elf_errmsg (-1));
     848                 :             :   return -1;
     849                 :             : }
     850                 :             : 
     851                 :             : /* Check if the file is an archive, and if so dump its index.  */
     852                 :             : static void
     853                 :           4 : check_archive_index (int fd, const char *fname, bool only_one)
     854                 :             : {
     855                 :             :   /* Create an `Elf' descriptor.  */
     856                 :           4 :   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
     857         [ -  + ]:           4 :   if (elf == NULL)
     858                 :           0 :     error (0, 0, _("cannot generate Elf descriptor: %s"),
     859                 :             :            elf_errmsg (-1));
     860                 :             :   else
     861                 :             :     {
     862         [ +  - ]:           4 :       if (elf_kind (elf) == ELF_K_AR)
     863                 :             :         {
     864         [ -  + ]:           4 :           if (!only_one)
     865                 :           0 :             printf ("\n%s:\n\n", fname);
     866                 :           4 :           dump_archive_index (elf, fname);
     867                 :             :         }
     868                 :             :       else
     869                 :           0 :         error (0, 0,
     870                 :           0 :                _("'%s' is not an archive, cannot print archive index"),
     871                 :             :                fname);
     872                 :             : 
     873                 :             :       /* Now we can close the descriptor.  */
     874         [ -  + ]:           4 :       if (elf_end (elf) != 0)
     875                 :           0 :         error (0, 0, _("error while closing Elf descriptor: %s"),
     876                 :             :                elf_errmsg (-1));
     877                 :             :     }
     878                 :           4 : }
     879                 :             : 
     880                 :             : /* Trivial callback used for checking if we opened an archive.  */
     881                 :             : static int
     882                 :         868 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
     883                 :             :                void **userdata __attribute__ ((unused)),
     884                 :             :                const char *name __attribute__ ((unused)),
     885                 :             :                Dwarf_Addr base __attribute__ ((unused)),
     886                 :             :                void *arg)
     887                 :             : {
     888         [ +  - ]:         868 :   if (*(bool *) arg)
     889                 :             :     return DWARF_CB_ABORT;
     890                 :         868 :   *(bool *) arg = true;
     891                 :         868 :   return DWARF_CB_OK;
     892                 :             : }
     893                 :             : 
     894                 :             : struct process_dwflmod_args
     895                 :             : {
     896                 :             :   int fd;
     897                 :             :   bool only_one;
     898                 :             : };
     899                 :             : 
     900                 :             : static int
     901                 :         968 : process_dwflmod (Dwfl_Module *dwflmod,
     902                 :             :                  void **userdata __attribute__ ((unused)),
     903                 :             :                  const char *name __attribute__ ((unused)),
     904                 :             :                  Dwarf_Addr base __attribute__ ((unused)),
     905                 :             :                  void *arg)
     906                 :             : {
     907                 :         968 :   const struct process_dwflmod_args *a = arg;
     908                 :             : 
     909                 :             :   /* Print the file name.  */
     910         [ +  + ]:         968 :   if (!a->only_one)
     911                 :             :     {
     912                 :         100 :       const char *fname;
     913                 :         100 :       dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
     914                 :             : 
     915                 :         100 :       printf ("\n%s:\n\n", fname);
     916                 :             :     }
     917                 :             : 
     918                 :         968 :   process_elf_file (dwflmod, a->fd);
     919                 :             : 
     920                 :         968 :   return DWARF_CB_OK;
     921                 :             : }
     922                 :             : 
     923                 :             : /* Stub libdwfl callback, only the ELF handle already open is ever used.
     924                 :             :    Only used for finding the alternate debug file if the Dwarf comes from
     925                 :             :    the main file.  We are not interested in separate debuginfo.  */
     926                 :             : static int
     927                 :         262 : find_no_debuginfo (Dwfl_Module *mod,
     928                 :             :                    void **userdata,
     929                 :             :                    const char *modname,
     930                 :             :                    Dwarf_Addr base,
     931                 :             :                    const char *file_name,
     932                 :             :                    const char *debuglink_file,
     933                 :             :                    GElf_Word debuglink_crc,
     934                 :             :                    char **debuginfo_file_name)
     935                 :             : {
     936                 :         262 :   Dwarf_Addr dwbias;
     937                 :         262 :   dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
     938                 :             : 
     939                 :             :   /* We are only interested if the Dwarf has been setup on the main
     940                 :             :      elf file but is only missing the alternate debug link.  If dwbias
     941                 :             :      hasn't even been setup, this is searching for separate debuginfo
     942                 :             :      for the main elf.  We don't care in that case.  */
     943         [ +  + ]:         262 :   if (dwbias == (Dwarf_Addr) -1)
     944                 :             :     return -1;
     945                 :             : 
     946                 :          70 :   return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
     947                 :             :                                        file_name, debuglink_file,
     948                 :             :                                        debuglink_crc, debuginfo_file_name);
     949                 :             : }
     950                 :             : 
     951                 :             : static Dwfl *
     952                 :         986 : create_dwfl (int fd, const char *fname)
     953                 :             : {
     954                 :             :   /* Duplicate an fd for dwfl_report_offline to swallow.  */
     955                 :         986 :   int dwfl_fd = dup (fd);
     956         [ -  + ]:         986 :   if (unlikely (dwfl_fd < 0))
     957                 :           0 :     error_exit (errno, "dup");
     958                 :             : 
     959                 :             :   /* Use libdwfl in a trivial way to open the libdw handle for us.
     960                 :             :      This takes care of applying relocations to DWARF data in ET_REL files.  */
     961                 :         986 :   static const Dwfl_Callbacks callbacks =
     962                 :             :     {
     963                 :             :       .section_address = dwfl_offline_section_address,
     964                 :             :       .find_debuginfo = find_no_debuginfo
     965                 :             :     };
     966                 :         986 :   Dwfl *dwfl = dwfl_begin (&callbacks);
     967         [ +  - ]:         986 :   if (likely (dwfl != NULL))
     968                 :             :     /* Let 0 be the logical address of the file (or first in archive).  */
     969                 :         986 :     dwfl->offline_next_address = 0;
     970         [ -  + ]:         986 :   if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
     971                 :             :     {
     972                 :           0 :       struct stat st;
     973         [ #  # ]:           0 :       if (fstat (dwfl_fd, &st) != 0)
     974                 :           0 :         error (0, errno, _("cannot stat input file"));
     975         [ #  # ]:           0 :       else if (unlikely (st.st_size == 0))
     976                 :           0 :         error (0, 0, _("input file is empty"));
     977                 :             :       else
     978                 :           0 :         error (0, 0, _("failed reading '%s': %s"),
     979                 :             :                fname, dwfl_errmsg (-1));
     980                 :           0 :       close (dwfl_fd);          /* Consumed on success, not on failure.  */
     981                 :           0 :       dwfl = NULL;
     982                 :             :     }
     983                 :             :   else
     984                 :         986 :     dwfl_report_end (dwfl, NULL, NULL);
     985                 :             : 
     986                 :         986 :   return dwfl;
     987                 :             : }
     988                 :             : 
     989                 :             : /* Process one input file.  */
     990                 :             : static void
     991                 :         972 : process_file (int fd, const char *fname, bool only_one)
     992                 :             : {
     993         [ +  + ]:         972 :   if (print_archive_index)
     994                 :           4 :     check_archive_index (fd, fname, only_one);
     995                 :             : 
     996         [ +  + ]:         972 :   if (!any_control_option)
     997                 :             :     return;
     998                 :             : 
     999         [ +  + ]:         968 :   if (elf_input_section != NULL)
    1000                 :             :     {
    1001                 :             :       /* Replace fname and fd with section content. */
    1002                 :          10 :       char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
    1003                 :          10 :       sprintf (fnname, "%s:%s", fname, elf_input_section);
    1004                 :          10 :       fd = open_input_section (fd);
    1005         [ -  + ]:          10 :       if (fd == -1)
    1006                 :             :         {
    1007                 :           0 :           error (0, 0, _("No such section '%s' in '%s'"),
    1008                 :             :                  elf_input_section, fname);
    1009                 :           0 :           return;
    1010                 :             :         }
    1011                 :             :       fname = fnname;
    1012                 :             :     }
    1013                 :             : 
    1014                 :         968 :   Dwfl *dwfl = create_dwfl (fd, fname);
    1015         [ +  - ]:         968 :   if (dwfl != NULL)
    1016                 :             :     {
    1017         [ +  + ]:         968 :       if (only_one)
    1018                 :             :         {
    1019                 :             :           /* Clear ONLY_ONE if we have multiple modules, from an archive.  */
    1020                 :         868 :           bool seen = false;
    1021                 :         868 :           only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
    1022                 :             :         }
    1023                 :             : 
    1024                 :             :       /* Process the one or more modules gleaned from this file.  */
    1025                 :         968 :       struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
    1026                 :         968 :       dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
    1027                 :             :     }
    1028                 :             :   /* Terrible hack for hooking unrelated skeleton/split compile units,
    1029                 :             :      see __libdw_link_skel_split in print_debug.  */
    1030         [ +  - ]:         968 :   if (! do_not_close_dwfl)
    1031                 :         968 :     dwfl_end (dwfl);
    1032                 :             : 
    1033                 :             :   /* Need to close the replaced fd if we created it.  Caller takes
    1034                 :             :      care of original.  */
    1035         [ +  + ]:         968 :   if (elf_input_section != NULL)
    1036                 :          10 :     close (fd);
    1037                 :             : }
    1038                 :             : 
    1039                 :             : /* Check whether there are any compressed sections in the ELF file.  */
    1040                 :             : static bool
    1041                 :         492 : elf_contains_chdrs (Elf *elf)
    1042                 :             : {
    1043                 :         492 :   Elf_Scn *scn = NULL;
    1044         [ +  + ]:      797906 :   while ((scn = elf_nextscn (elf, scn)) != NULL)
    1045                 :             :     {
    1046                 :      797528 :       GElf_Shdr shdr_mem;
    1047                 :      797528 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1048   [ +  -  +  + ]:      797528 :       if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
    1049                 :         114 :         return true;
    1050                 :             :     }
    1051                 :             :   return false;
    1052                 :             : }
    1053                 :             : 
    1054                 :             : /* Process one ELF file.  */
    1055                 :             : static void
    1056                 :         968 : process_elf_file (Dwfl_Module *dwflmod, int fd)
    1057                 :             : {
    1058                 :         968 :   GElf_Addr dwflbias;
    1059                 :         968 :   Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
    1060                 :             : 
    1061                 :         968 :   GElf_Ehdr ehdr_mem;
    1062                 :         968 :   GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
    1063                 :             : 
    1064         [ -  + ]:         968 :   if (ehdr == NULL)
    1065                 :             :     {
    1066                 :           0 :       error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
    1067                 :           0 :       return;
    1068                 :             :     }
    1069                 :             : 
    1070                 :         968 :   Ebl *ebl = ebl_openbackend (elf);
    1071         [ -  + ]:         968 :   if (unlikely (ebl == NULL))
    1072                 :             :     {
    1073                 :           0 :     ebl_error:
    1074                 :           0 :       error (0, errno, _("cannot create EBL handle"));
    1075                 :           0 :       return;
    1076                 :             :     }
    1077                 :             : 
    1078                 :             :   /* Determine the number of sections.  */
    1079         [ -  + ]:         968 :   if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
    1080                 :           0 :     error_exit (0, _("cannot determine number of sections: %s"),
    1081                 :             :                 elf_errmsg (-1));
    1082                 :             : 
    1083                 :             :   /* Determine the number of phdrs.  */
    1084         [ -  + ]:         968 :   if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
    1085                 :           0 :     error_exit (0, _("cannot determine number of program headers: %s"),
    1086                 :             :                 elf_errmsg (-1));
    1087                 :             : 
    1088                 :             :   /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
    1089                 :             :      may have applied relocation to some sections.  If there are any
    1090                 :             :      compressed sections, any pass (or libdw/libdwfl) might have
    1091                 :             :      uncompressed them.  So we need to get a fresh Elf handle on the
    1092                 :             :      file to display those.  */
    1093                 :        2132 :   bool print_unchanged = ((print_section_header
    1094         [ +  + ]:         462 :                            || print_relocations
    1095         [ +  + ]:         452 :                            || dump_data_sections != NULL
    1096         [ +  + ]:         440 :                            || print_notes)
    1097   [ +  +  +  + ]:        1036 :                           && (ehdr->e_type == ET_REL
    1098         [ +  + ]:         492 :                               || elf_contains_chdrs (ebl->elf)));
    1099                 :             : 
    1100                 :         196 :   Elf *pure_elf = NULL;
    1101                 :         196 :   Ebl *pure_ebl = ebl;
    1102                 :         196 :   if (print_unchanged)
    1103                 :             :     {
    1104                 :             :       /* Read the file afresh.  */
    1105                 :         196 :       off_t aroff = elf_getaroff (elf);
    1106                 :         196 :       pure_elf = dwelf_elf_begin (fd);
    1107         [ -  + ]:         196 :       if (aroff > 0)
    1108                 :             :         {
    1109                 :             :           /* Archive member.  */
    1110                 :           0 :           (void) elf_rand (pure_elf, aroff);
    1111                 :           0 :           Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
    1112                 :           0 :           elf_end (pure_elf);
    1113                 :           0 :           pure_elf = armem;
    1114                 :             :         }
    1115         [ -  + ]:         196 :       if (pure_elf == NULL)
    1116                 :             :         {
    1117                 :           0 :           error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
    1118                 :           0 :           return;
    1119                 :             :         }
    1120                 :         196 :       pure_ebl = ebl_openbackend (pure_elf);
    1121         [ -  + ]:         196 :       if (pure_ebl == NULL)
    1122                 :           0 :         goto ebl_error;
    1123                 :             :     }
    1124                 :             : 
    1125                 :         968 :   bool symtab_printed = false;
    1126                 :             : 
    1127         [ +  + ]:         968 :   if (print_file_header)
    1128                 :         190 :     print_ehdr (ebl, ehdr);
    1129         [ +  + ]:         968 :   if (print_section_header)
    1130                 :         506 :     print_shdr (pure_ebl, ehdr);
    1131         [ +  + ]:         968 :   if (print_program_header)
    1132                 :         188 :     print_phdr (ebl, ehdr);
    1133         [ +  + ]:         968 :   if (print_section_groups)
    1134                 :         202 :     print_scngrp (ebl);
    1135         [ +  + ]:         968 :   if (print_dynamic_table)
    1136                 :         190 :     print_dynamic (ebl);
    1137         [ +  + ]:         968 :   if (print_relocations)
    1138                 :         194 :     print_relocs (pure_ebl, dwflmod, ehdr);
    1139         [ +  + ]:         968 :   if (print_histogram)
    1140                 :         184 :     handle_hash (ebl);
    1141   [ +  +  +  + ]:         968 :   if (print_symbol_table || print_dynsym_table)
    1142                 :         220 :     symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
    1143         [ +  + ]:         968 :   if (print_version_info)
    1144                 :         184 :     print_verinfo (ebl);
    1145   [ +  +  +  - ]:         968 :   if (print_symbol_table && !use_dynamic_segment)
    1146                 :         218 :     symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
    1147                 :             : 
    1148   [ +  +  +  + ]:         968 :   if ((print_symbol_table || print_dynsym_table)
    1149   [ +  +  +  + ]:         220 :       && !symtab_printed && symbol_table_section != NULL)
    1150                 :           2 :     printf ("WARNING: %s: '%s'\n", _("cannot find section"),
    1151                 :             :         symbol_table_section);
    1152                 :             : 
    1153         [ +  + ]:         968 :   if (print_arch)
    1154                 :         192 :     print_liblist (ebl);
    1155         [ +  + ]:         968 :   if (print_arch)
    1156                 :         192 :     print_attributes (ebl, ehdr);
    1157         [ +  + ]:         968 :   if (dump_data_sections != NULL)
    1158                 :          12 :     dump_data (pure_ebl);
    1159         [ +  + ]:         968 :   if (string_sections != NULL)
    1160                 :         186 :     dump_strings (ebl);
    1161         [ +  + ]:         968 :   if ((print_debug_sections | implicit_debug_sections) != 0)
    1162                 :         502 :     print_debug (dwflmod, ebl, ehdr);
    1163         [ +  + ]:         968 :   if (print_notes)
    1164                 :         230 :     handle_notes (pure_ebl, ehdr);
    1165         [ -  + ]:         968 :   if (print_string_sections)
    1166                 :           0 :     print_strings (ebl);
    1167                 :             : 
    1168         [ +  + ]:         968 :   if (pure_ebl != ebl)
    1169                 :             :     {
    1170                 :         196 :       ebl_closebackend (ebl);
    1171                 :         196 :       ebl_closebackend (pure_ebl);
    1172                 :         196 :       elf_end (pure_elf);
    1173                 :             :     }
    1174                 :             :   else
    1175                 :         772 :     ebl_closebackend (ebl);
    1176                 :             : }
    1177                 :             : 
    1178                 :             : 
    1179                 :             : /* Print file type.  */
    1180                 :             : static void
    1181                 :         190 : print_file_type (unsigned short int e_type)
    1182                 :             : {
    1183         [ +  - ]:         190 :   if (likely (e_type <= ET_CORE))
    1184                 :             :     {
    1185                 :         190 :       static const char *const knowntypes[] =
    1186                 :             :       {
    1187                 :             :         N_("NONE (None)"),
    1188                 :             :         N_("REL (Relocatable file)"),
    1189                 :             :         N_("EXEC (Executable file)"),
    1190                 :             :         N_("DYN (Shared object file)"),
    1191                 :             :         N_("CORE (Core file)")
    1192                 :             :       };
    1193                 :         190 :       puts (_(knowntypes[e_type]));
    1194                 :             :     }
    1195         [ #  # ]:           0 :   else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
    1196                 :           0 :     printf (_("OS Specific: (%x)\n"),  e_type);
    1197         [ #  # ]:           0 :   else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
    1198                 :           0 :     printf (_("Processor Specific: (%x)\n"),  e_type);
    1199                 :             :   else
    1200                 :           0 :     puts ("???");
    1201                 :         190 : }
    1202                 :             : 
    1203                 :             : 
    1204                 :             : /* Print ELF header.  */
    1205                 :             : static void
    1206                 :         190 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1207                 :             : {
    1208                 :         190 :   fputs (_("ELF Header:\n  Magic:  "), stdout);
    1209         [ +  + ]:        3230 :   for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
    1210                 :        3040 :     printf (" %02hhx", ehdr->e_ident[cnt]);
    1211                 :             : 
    1212                 :         190 :   printf (_("\n  Class:                             %s\n"),
    1213         [ +  + ]:         190 :           ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
    1214                 :             :           : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
    1215         [ -  + ]:         156 :           : "\?\?\?");
    1216                 :             : 
    1217                 :         190 :   printf (_("  Data:                              %s\n"),
    1218         [ -  + ]:         190 :           ehdr->e_ident[EI_DATA] == ELFDATA2LSB
    1219                 :             :           ? "2's complement, little endian"
    1220                 :             :           : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
    1221         [ #  # ]:           0 :           ? "2's complement, big endian" : "\?\?\?");
    1222                 :             : 
    1223                 :         570 :   printf (_("  Ident Version:                     %hhd %s\n"),
    1224                 :         190 :           ehdr->e_ident[EI_VERSION],
    1225         [ +  - ]:         190 :           ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
    1226                 :             :           : "(\?\?\?)");
    1227                 :             : 
    1228                 :         190 :   char buf[512];
    1229                 :         190 :   printf (_("  OS/ABI:                            %s\n"),
    1230                 :         190 :           ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
    1231                 :             : 
    1232                 :         380 :   printf (_("  ABI Version:                       %hhd\n"),
    1233                 :         190 :           ehdr->e_ident[EI_ABIVERSION]);
    1234                 :             : 
    1235                 :         190 :   fputs (_("  Type:                              "), stdout);
    1236                 :         190 :   print_file_type (ehdr->e_type);
    1237                 :             : 
    1238                 :         190 :   const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
    1239         [ +  - ]:         190 :   if (machine != NULL)
    1240                 :         190 :     printf (_("  Machine:                           %s\n"), machine);
    1241                 :             :   else
    1242                 :           0 :     printf (_("  Machine:                           <unknown>: 0x%x\n"),
    1243                 :           0 :             ehdr->e_machine);
    1244                 :             : 
    1245                 :         190 :   printf (_("  Version:                           %d %s\n"),
    1246                 :             :           ehdr->e_version,
    1247         [ +  - ]:         190 :           ehdr->e_version  == EV_CURRENT ? _("(current)") : "(\?\?\?)");
    1248                 :             : 
    1249                 :         190 :   printf (_("  Entry point address:               %#" PRIx64 "\n"),
    1250                 :             :           ehdr->e_entry);
    1251                 :             : 
    1252                 :         190 :   printf (_("  Start of program headers:          %" PRId64 " %s\n"),
    1253                 :             :           ehdr->e_phoff, _("(bytes into file)"));
    1254                 :             : 
    1255                 :         190 :   printf (_("  Start of section headers:          %" PRId64 " %s\n"),
    1256                 :             :           ehdr->e_shoff, _("(bytes into file)"));
    1257                 :             : 
    1258                 :         190 :   printf (_("  Flags:                             %s\n"),
    1259                 :             :           ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
    1260                 :             : 
    1261                 :         380 :   printf (_("  Size of this header:               %" PRId16 " %s\n"),
    1262                 :         190 :           ehdr->e_ehsize, _("(bytes)"));
    1263                 :             : 
    1264                 :         380 :   printf (_("  Size of program header entries:    %" PRId16 " %s\n"),
    1265                 :         190 :           ehdr->e_phentsize, _("(bytes)"));
    1266                 :             : 
    1267                 :         380 :   printf (_("  Number of program headers entries: %" PRId16),
    1268                 :         190 :           ehdr->e_phnum);
    1269         [ +  + ]:         190 :   if (ehdr->e_phnum == PN_XNUM)
    1270                 :             :     {
    1271                 :           2 :       GElf_Shdr shdr_mem;
    1272                 :           2 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1273         [ +  - ]:           2 :       if (shdr != NULL)
    1274                 :           2 :         printf (_(" (%" PRIu32 " in [0].sh_info)"),
    1275                 :           2 :                 (uint32_t) shdr->sh_info);
    1276                 :             :       else
    1277                 :           0 :         fputs (_(" ([0] not available)"), stdout);
    1278                 :             :     }
    1279                 :         190 :   fputc ('\n', stdout);
    1280                 :             : 
    1281                 :         380 :   printf (_("  Size of section header entries:    %" PRId16 " %s\n"),
    1282                 :         190 :           ehdr->e_shentsize, _("(bytes)"));
    1283                 :             : 
    1284                 :         380 :   printf (_("  Number of section headers entries: %" PRId16),
    1285                 :         190 :           ehdr->e_shnum);
    1286         [ -  + ]:         190 :   if (ehdr->e_shnum == 0)
    1287                 :             :     {
    1288                 :           0 :       GElf_Shdr shdr_mem;
    1289                 :           0 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1290         [ #  # ]:           0 :       if (shdr != NULL)
    1291                 :           0 :         printf (_(" (%" PRIu32 " in [0].sh_size)"),
    1292                 :           0 :                 (uint32_t) shdr->sh_size);
    1293                 :             :       else
    1294                 :           0 :         fputs (_(" ([0] not available)"), stdout);
    1295                 :             :     }
    1296                 :         190 :   fputc ('\n', stdout);
    1297                 :             : 
    1298         [ -  + ]:         190 :   if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
    1299                 :             :     {
    1300                 :           0 :       GElf_Shdr shdr_mem;
    1301                 :           0 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
    1302         [ #  # ]:           0 :       if (shdr != NULL)
    1303                 :             :         /* We managed to get the zeroth section.  */
    1304                 :           0 :         snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
    1305                 :           0 :                   (uint32_t) shdr->sh_link);
    1306                 :             :       else
    1307                 :             :         {
    1308                 :           0 :           strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
    1309                 :           0 :           buf[sizeof (buf) - 1] = '\0';
    1310                 :             :         }
    1311                 :             : 
    1312                 :           0 :       printf (_("  Section header string table index: XINDEX%s\n\n"),
    1313                 :             :               buf);
    1314                 :             :     }
    1315                 :             :   else
    1316                 :         190 :     printf (_("  Section header string table index: %" PRId16 "\n\n"),
    1317                 :             :             ehdr->e_shstrndx);
    1318                 :         190 : }
    1319                 :             : 
    1320                 :             : 
    1321                 :             : static const char *
    1322                 :       80566 : get_visibility_type (int value)
    1323                 :             : {
    1324   [ -  +  -  -  :       80566 :   switch (value)
                      + ]
    1325                 :             :     {
    1326                 :             :     case STV_DEFAULT:
    1327                 :             :       return "DEFAULT";
    1328                 :           0 :     case STV_INTERNAL:
    1329                 :           0 :       return "INTERNAL";
    1330                 :         956 :     case STV_HIDDEN:
    1331                 :         956 :       return "HIDDEN";
    1332                 :           0 :     case STV_PROTECTED:
    1333                 :           0 :       return "PROTECTED";
    1334                 :           0 :     default:
    1335                 :           0 :       return "???";
    1336                 :             :     }
    1337                 :             : }
    1338                 :             : 
    1339                 :             : static const char *
    1340                 :         384 : elf_ch_type_name (unsigned int code)
    1341                 :             : {
    1342                 :         384 :   switch (code)
    1343                 :             :     {
    1344                 :             :     case 0:
    1345                 :             :       return "NONE";
    1346                 :          24 :     case ELFCOMPRESS_ZLIB:
    1347                 :          24 :       return "ZLIB";
    1348                 :         360 :     case ELFCOMPRESS_ZSTD:
    1349                 :         360 :       return "ZSTD";
    1350                 :           0 :     default:
    1351                 :           0 :       return "UNKNOWN";
    1352                 :             :     }
    1353                 :             : }
    1354                 :             : 
    1355                 :             : /* Print the section headers.  */
    1356                 :             : static void
    1357                 :         506 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1358                 :             : {
    1359                 :         506 :   size_t cnt;
    1360                 :         506 :   size_t shstrndx;
    1361                 :             : 
    1362         [ +  + ]:         506 :   if (! print_file_header)
    1363                 :             :     {
    1364                 :         322 :       size_t sections;
    1365         [ -  + ]:         322 :       if (unlikely (elf_getshdrnum (ebl->elf, &sections) < 0))
    1366                 :           0 :         error_exit (0, _("cannot get number of sections: %s"),
    1367                 :             :                     elf_errmsg (-1));
    1368                 :             : 
    1369                 :         322 :       printf (_("\
    1370                 :             : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
    1371                 :             : \n"),
    1372                 :             :               sections, ehdr->e_shoff);
    1373                 :             :     }
    1374                 :             : 
    1375                 :             :   /* Get the section header string table index.  */
    1376         [ -  + ]:         506 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1377                 :           0 :     error_exit (0, _("cannot get section header string table index: %s"),
    1378                 :             :                 elf_errmsg (-1));
    1379                 :             : 
    1380                 :         506 :   puts (_("Section Headers:"));
    1381                 :             : 
    1382         [ +  + ]:         506 :   if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1383                 :         196 :     puts (_("[Nr] Name                 Type         Addr     Off    Size   ES Flags Lk Inf Al"));
    1384                 :             :   else
    1385                 :         310 :     puts (_("[Nr] Name                 Type         Addr             Off      Size     ES Flags Lk Inf Al"));
    1386                 :             : 
    1387         [ +  + ]:         506 :   if (print_decompress)
    1388                 :             :     {
    1389         [ +  + ]:         112 :       if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1390                 :          56 :         puts (_("     [Compression  Size   Al]"));
    1391                 :             :       else
    1392                 :          56 :         puts (_("     [Compression  Size     Al]"));
    1393                 :             :     }
    1394                 :             : 
    1395         [ +  + ]:     1586316 :   for (cnt = 0; cnt < shnum; ++cnt)
    1396                 :             :     {
    1397                 :     1585810 :       Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
    1398                 :             : 
    1399         [ -  + ]:     1585810 :       if (unlikely (scn == NULL))
    1400                 :           0 :         error_exit (0, _("cannot get section: %s"),
    1401                 :             :                     elf_errmsg (-1));
    1402                 :             : 
    1403                 :             :       /* Get the section header.  */
    1404                 :     1585810 :       GElf_Shdr shdr_mem;
    1405                 :     1585810 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1406         [ -  + ]:     1585810 :       if (unlikely (shdr == NULL))
    1407                 :           0 :         error_exit (0, _("cannot get section header: %s"),
    1408                 :             :                     elf_errmsg (-1));
    1409                 :             : 
    1410                 :     1585810 :       char flagbuf[20];
    1411                 :     1585810 :       char *cp = flagbuf;
    1412         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_WRITE)
    1413                 :        2764 :         *cp++ = 'W';
    1414         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_ALLOC)
    1415                 :        8520 :         *cp++ = 'A';
    1416         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_EXECINSTR)
    1417                 :        1564 :         *cp++ = 'X';
    1418         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_MERGE)
    1419                 :         420 :         *cp++ = 'M';
    1420         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_STRINGS)
    1421                 :         380 :         *cp++ = 'S';
    1422         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_INFO_LINK)
    1423                 :         196 :         *cp++ = 'I';
    1424         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_LINK_ORDER)
    1425                 :           4 :         *cp++ = 'L';
    1426         [ -  + ]:     1585810 :       if (shdr->sh_flags & SHF_OS_NONCONFORMING)
    1427                 :           0 :         *cp++ = 'N';
    1428         [ -  + ]:     1585810 :       if (shdr->sh_flags & SHF_GROUP)
    1429                 :           0 :         *cp++ = 'G';
    1430         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_TLS)
    1431                 :          30 :         *cp++ = 'T';
    1432         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_COMPRESSED)
    1433                 :         414 :         *cp++ = 'C';
    1434         [ -  + ]:     1585810 :       if (shdr->sh_flags & SHF_ORDERED)
    1435                 :           0 :         *cp++ = 'O';
    1436         [ -  + ]:     1585810 :       if (shdr->sh_flags & SHF_EXCLUDE)
    1437                 :           0 :         *cp++ = 'E';
    1438         [ +  + ]:     1585810 :       if (shdr->sh_flags & SHF_GNU_RETAIN)
    1439                 :           2 :         *cp++ = 'R';
    1440                 :     1585810 :       *cp = '\0';
    1441                 :             : 
    1442                 :     1585810 :       const char *sname;
    1443                 :     1585810 :       char buf[128];
    1444         [ -  + ]:     1585810 :       sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
    1445         [ +  + ]:     3171620 :       printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
    1446                 :             :               " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
    1447                 :             :               " %2" PRId64 "\n",
    1448                 :             :               cnt, sname,
    1449                 :     1585810 :               ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
    1450                 :             :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
    1451                 :             :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
    1452         [ +  + ]:     1585810 :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
    1453                 :             :               shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
    1454                 :             :               shdr->sh_addralign);
    1455                 :             : 
    1456         [ +  + ]:     1585810 :       if (print_decompress)
    1457                 :             :         {
    1458         [ +  + ]:        1880 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    1459                 :             :             {
    1460                 :         384 :               GElf_Chdr chdr;
    1461         [ +  - ]:         384 :               if (gelf_getchdr (scn, &chdr) != NULL)
    1462   [ +  +  -  - ]:         768 :                 printf ("     [ELF %s (%" PRId32 ") %0*" PRIx64
    1463                 :             :                         " %2" PRId64 "]\n",
    1464                 :             :                         elf_ch_type_name (chdr.ch_type),
    1465                 :             :                         chdr.ch_type,
    1466         [ +  + ]:         384 :                         ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
    1467                 :             :                         chdr.ch_size, chdr.ch_addralign);
    1468                 :             :               else
    1469                 :           0 :                 error (0, 0,
    1470                 :           0 :                        _("bad compression header for section %zd: %s"),
    1471                 :             :                        elf_ndxscn (scn), elf_errmsg (-1));
    1472                 :             :             }
    1473         [ +  + ]:        1496 :           else if (startswith (sname, ".zdebug"))
    1474                 :             :             {
    1475                 :          24 :               ssize_t size;
    1476         [ +  - ]:          24 :               if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
    1477                 :          24 :                 printf ("     [GNU ZLIB     %0*zx   ]\n",
    1478         [ +  + ]:          24 :                         ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
    1479                 :             :               else
    1480                 :           0 :                 error (0, 0,
    1481                 :           0 :                        _("bad gnu compressed size for section %zd: %s"),
    1482                 :             :                        elf_ndxscn (scn), elf_errmsg (-1));
    1483                 :             :             }
    1484                 :             :         }
    1485                 :             :     }
    1486                 :             : 
    1487                 :         506 :   print_flag_info();
    1488                 :         506 :   fputc ('\n', stdout);
    1489                 :         506 : }
    1490                 :             : 
    1491                 :             : /* Print flag information.  */
    1492                 :             : static void
    1493                 :         506 : print_flag_info (void)
    1494                 :             : {
    1495                 :         506 :         puts ("Key to Flags:");
    1496                 :         506 :         puts ("  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),");
    1497                 :         506 :         puts ("  L (link order), N (extra OS processing required), G (group), T (TLS),");
    1498                 :         506 :         puts ("  C (compressed), O (ordered), R (GNU retain), E (exclude)");
    1499                 :         506 : }
    1500                 :             : 
    1501                 :             : /* Print the program header.  */
    1502                 :             : static void
    1503                 :         188 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
    1504                 :             : {
    1505         [ +  + ]:         188 :   if (phnum == 0)
    1506                 :             :     /* No program header, this is OK in relocatable objects.  */
    1507                 :          12 :     return;
    1508                 :             : 
    1509                 :         176 :   puts (_("Program Headers:"));
    1510         [ +  + ]:         176 :   if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    1511                 :          26 :     puts (_("\
    1512                 :             :   Type           Offset   VirtAddr   PhysAddr   FileSiz  MemSiz   Flg Align"));
    1513                 :             :   else
    1514                 :         150 :     puts (_("\
    1515                 :             :   Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align"));
    1516                 :             : 
    1517                 :             :   /* Process all program headers.  */
    1518                 :             :   bool has_relro = false;
    1519                 :             :   GElf_Addr relro_from = 0;
    1520                 :             :   GElf_Addr relro_to = 0;
    1521         [ +  + ]:        2074 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
    1522                 :             :     {
    1523                 :        1898 :       char buf[128];
    1524                 :        1898 :       GElf_Phdr mem;
    1525                 :        1898 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
    1526                 :             : 
    1527                 :             :       /* If for some reason the header cannot be returned show this.  */
    1528         [ -  + ]:        1898 :       if (unlikely (phdr == NULL))
    1529                 :             :         {
    1530                 :           0 :           puts ("  ???");
    1531                 :           0 :           continue;
    1532                 :             :         }
    1533                 :             : 
    1534                 :        1898 :       printf ("  %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
    1535                 :             :               " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
    1536                 :        1898 :               ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
    1537                 :             :               phdr->p_offset,
    1538                 :             :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
    1539         [ +  + ]:        1898 :               ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
    1540                 :             :               phdr->p_filesz,
    1541                 :             :               phdr->p_memsz,
    1542         [ -  + ]:        1898 :               phdr->p_flags & PF_R ? 'R' : ' ',
    1543         [ +  + ]:        1898 :               phdr->p_flags & PF_W ? 'W' : ' ',
    1544         [ +  + ]:        1898 :               phdr->p_flags & PF_X ? 'E' : ' ',
    1545                 :             :               phdr->p_align);
    1546                 :             : 
    1547         [ +  + ]:        1898 :       if (phdr->p_type == PT_INTERP)
    1548                 :             :         {
    1549                 :             :           /* If we are sure the file offset is valid then we can show
    1550                 :             :              the user the name of the interpreter.  We check whether
    1551                 :             :              there is a section at the file offset.  Normally there
    1552                 :             :              would be a section called ".interp".  But in separate
    1553                 :             :              .debug files it is a NOBITS section (and so doesn't match
    1554                 :             :              with gelf_offscn).  Which probably means the offset is
    1555                 :             :              not valid another reason could be because the ELF file
    1556                 :             :              just doesn't contain any section headers, in that case
    1557                 :             :              just play it safe and don't display anything.  */
    1558                 :             : 
    1559                 :         172 :           Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
    1560                 :         172 :           GElf_Shdr shdr_mem;
    1561                 :         172 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1562                 :             : 
    1563                 :         172 :           size_t maxsize;
    1564                 :         172 :           char *filedata = elf_rawfile (ebl->elf, &maxsize);
    1565                 :             : 
    1566   [ +  -  +  + ]:         172 :           if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
    1567   [ +  -  +  - ]:         130 :               && filedata != NULL && phdr->p_offset < maxsize
    1568         [ +  - ]:         130 :               && phdr->p_filesz <= maxsize - phdr->p_offset
    1569         [ +  - ]:         130 :               && memchr (filedata + phdr->p_offset, '\0',
    1570                 :             :                          phdr->p_filesz) != NULL)
    1571                 :         130 :             printf (_("\t[Requesting program interpreter: %s]\n"),
    1572                 :             :                     filedata + phdr->p_offset);
    1573                 :             :         }
    1574         [ +  + ]:        1726 :       else if (phdr->p_type == PT_GNU_RELRO)
    1575                 :             :         {
    1576                 :         152 :           has_relro = true;
    1577                 :         152 :           relro_from = phdr->p_vaddr;
    1578                 :         152 :           relro_to = relro_from + phdr->p_memsz;
    1579                 :             :         }
    1580                 :             :     }
    1581                 :             : 
    1582                 :         176 :   size_t sections;
    1583         [ -  + ]:         176 :   if (unlikely (elf_getshdrnum (ebl->elf, &sections) < 0))
    1584                 :           0 :     error_exit (0, _("cannot get number of sections: %s"),
    1585                 :             :                 elf_errmsg (-1));
    1586                 :             : 
    1587         [ +  - ]:         176 :   if (sections == 0)
    1588                 :             :     /* No sections in the file.  Punt.  */
    1589                 :             :     return;
    1590                 :             : 
    1591                 :             :   /* Get the section header string table index.  */
    1592                 :         176 :   size_t shstrndx;
    1593         [ -  + ]:         176 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1594                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    1595                 :             : 
    1596                 :         176 :   puts (_("\n Section to Segment mapping:\n  Segment Sections..."));
    1597                 :             : 
    1598         [ +  + ]:        2074 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
    1599                 :             :     {
    1600                 :             :       /* Print the segment number.  */
    1601                 :        1898 :       printf ("   %2.2zu     ", cnt);
    1602                 :             : 
    1603                 :        1898 :       GElf_Phdr phdr_mem;
    1604                 :        1898 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
    1605                 :             :       /* This must not happen.  */
    1606         [ -  + ]:        1898 :       if (unlikely (phdr == NULL))
    1607                 :           0 :         error_exit (0, _("cannot get program header: %s"),
    1608                 :             :                     elf_errmsg (-1));
    1609                 :             : 
    1610                 :             :       /* Iterate over the sections.  */
    1611                 :             :       bool in_relro = false;
    1612                 :             :       bool in_ro = false;
    1613         [ +  + ]:       62206 :       for (size_t inner = 1; inner < shnum; ++inner)
    1614                 :             :         {
    1615                 :       60308 :           Elf_Scn *scn = elf_getscn (ebl->elf, inner);
    1616                 :             :           /* This should not happen.  */
    1617         [ -  + ]:       60308 :           if (unlikely (scn == NULL))
    1618                 :           0 :             error_exit (0, _("cannot get section: %s"),
    1619                 :             :                         elf_errmsg (-1));
    1620                 :             : 
    1621                 :             :           /* Get the section header.  */
    1622                 :       60308 :           GElf_Shdr shdr_mem;
    1623                 :       60308 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1624         [ -  + ]:       60308 :           if (unlikely (shdr == NULL))
    1625                 :           0 :             error_exit (0, _("cannot get section header: %s"),
    1626                 :             :                         elf_errmsg (-1));
    1627                 :             : 
    1628         [ +  - ]:       60308 :           if (shdr->sh_size > 0
    1629                 :             :               /* Compare allocated sections by VMA, unallocated
    1630                 :             :                  sections by file offset.  */
    1631         [ +  + ]:       60308 :               && (shdr->sh_flags & SHF_ALLOC
    1632                 :       47582 :                   ? (shdr->sh_addr >= phdr->p_vaddr
    1633         [ +  + ]:       47582 :                      && (shdr->sh_addr + shdr->sh_size
    1634         [ +  + ]:       31986 :                          <= phdr->p_vaddr + phdr->p_memsz))
    1635                 :       12726 :                   : (shdr->sh_offset >= phdr->p_offset
    1636         [ +  + ]:       12726 :                      && (shdr->sh_offset + shdr->sh_size
    1637         [ +  + ]:       11200 :                          <= phdr->p_offset + phdr->p_filesz))))
    1638                 :             :             {
    1639         [ +  + ]:        6262 :               if (has_relro && !in_relro
    1640         [ +  + ]:        4178 :                   && shdr->sh_addr >= relro_from
    1641         [ +  + ]:         732 :                   && shdr->sh_addr + shdr->sh_size <= relro_to)
    1642                 :             :                 {
    1643                 :         474 :                   fputs (" [RELRO:", stdout);
    1644                 :         474 :                   in_relro = true;
    1645                 :             :                 }
    1646   [ +  +  +  + ]:        5788 :               else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
    1647                 :             :                 {
    1648                 :         134 :                   fputs ("]", stdout);
    1649                 :         134 :                   in_relro =  false;
    1650                 :             :                 }
    1651         [ +  + ]:        5654 :               else if (has_relro && in_relro
    1652         [ +  + ]:        1166 :                        && shdr->sh_addr + shdr->sh_size > relro_to)
    1653                 :          18 :                 fputs ("] <RELRO:", stdout);
    1654         [ +  + ]:        5636 :               else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
    1655                 :             :                 {
    1656         [ +  + ]:        3226 :                   if (!in_ro)
    1657                 :             :                     {
    1658                 :         372 :                       fputs (" [RO:", stdout);
    1659                 :         372 :                       in_ro = true;
    1660                 :             :                     }
    1661                 :             :                 }
    1662                 :             :               else
    1663                 :             :                 {
    1664                 :             :                   /* Determine the segment this section is part of.  */
    1665                 :             :                   size_t cnt2;
    1666                 :             :                   GElf_Phdr phdr2_mem;
    1667                 :             :                   GElf_Phdr *phdr2 = NULL;
    1668         [ +  - ]:       10860 :                   for (cnt2 = 0; cnt2 < phnum; ++cnt2)
    1669                 :             :                     {
    1670                 :       10860 :                       phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
    1671                 :             : 
    1672   [ +  -  +  + ]:       10860 :                       if (phdr2 != NULL && phdr2->p_type == PT_LOAD
    1673         [ +  - ]:        6152 :                           && shdr->sh_addr >= phdr2->p_vaddr
    1674                 :        6152 :                           && (shdr->sh_addr + shdr->sh_size
    1675         [ +  + ]:        6152 :                               <= phdr2->p_vaddr + phdr2->p_memsz))
    1676                 :             :                         break;
    1677                 :             :                     }
    1678                 :             : 
    1679         [ +  - ]:        2410 :                   if (cnt2 < phnum)
    1680                 :             :                     {
    1681   [ +  +  +  + ]:        2410 :                       if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
    1682                 :             :                         {
    1683                 :         656 :                           fputs (" [RO:", stdout);
    1684                 :         656 :                           in_ro = true;
    1685                 :             :                         }
    1686   [ +  +  +  - ]:        1754 :                       else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
    1687                 :             :                         {
    1688                 :           0 :                           fputs ("]", stdout);
    1689                 :           0 :                           in_ro = false;
    1690                 :             :                         }
    1691                 :             :                     }
    1692                 :             :                 }
    1693                 :             : 
    1694                 :        6262 :               printf (" %s",
    1695                 :        6262 :                       elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
    1696                 :             : 
    1697                 :             :               /* Signal that this section is only partially covered.  */
    1698         [ +  + ]:        6262 :               if (has_relro && in_relro
    1699         [ +  + ]:        1640 :                        && shdr->sh_addr + shdr->sh_size > relro_to)
    1700                 :             :                 {
    1701                 :          18 :                   fputs (">", stdout);
    1702                 :          18 :                   in_relro =  false;
    1703                 :             :                 }
    1704                 :             :             }
    1705                 :             :         }
    1706         [ +  + ]:        1898 :       if (in_relro || in_ro)
    1707                 :        1350 :         fputs ("]", stdout);
    1708                 :             : 
    1709                 :             :       /* Finish the line.  */
    1710                 :        1898 :       fputc ('\n', stdout);
    1711                 :             :     }
    1712                 :             : }
    1713                 :             : 
    1714                 :             : 
    1715                 :             : static const char *
    1716                 :        1252 : section_name (Ebl *ebl, GElf_Shdr *shdr)
    1717                 :             : {
    1718                 :        1252 :   size_t shstrndx;
    1719   [ +  -  -  + ]:        1252 :   if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
    1720                 :           0 :     return "???";
    1721         [ -  + ]:        1252 :   return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
    1722                 :             : }
    1723                 :             : 
    1724                 :             : 
    1725                 :             : static void
    1726                 :          32 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    1727                 :             : {
    1728                 :             :   /* Get the data of the section.  */
    1729                 :          32 :   Elf_Data *data = elf_getdata (scn, NULL);
    1730                 :             : 
    1731                 :          32 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    1732                 :          32 :   GElf_Shdr symshdr_mem;
    1733                 :          32 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    1734                 :          32 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    1735                 :             : 
    1736   [ +  -  +  - ]:          32 :   if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
    1737         [ -  + ]:          32 :       || symdata == NULL)
    1738                 :           0 :     return;
    1739                 :             : 
    1740                 :             :   /* Get the section header string table index.  */
    1741                 :          32 :   size_t shstrndx;
    1742         [ -  + ]:          32 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1743                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    1744                 :             : 
    1745                 :          32 :   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
    1746                 :             : 
    1747                 :          32 :   GElf_Sym sym_mem;
    1748                 :          32 :   GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
    1749                 :             : 
    1750   [ -  +  +  + ]:          64 :   printf ((grpref[0] & GRP_COMDAT)
    1751                 :           8 :           ? ngettext ("\
    1752                 :             : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
    1753                 :             :                       "\
    1754                 :             : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
    1755                 :             :                       data->d_size / sizeof (Elf32_Word) - 1)
    1756                 :          24 :           : ngettext ("\
    1757                 :             : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
    1758                 :             : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
    1759                 :             :                       data->d_size / sizeof (Elf32_Word) - 1),
    1760                 :             :           elf_ndxscn (scn),
    1761                 :          32 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    1762                 :             :           (sym == NULL ? NULL
    1763                 :          32 :            : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
    1764                 :           0 :           ?: _("<INVALID SYMBOL>"),
    1765         [ +  - ]:          32 :           data->d_size / sizeof (Elf32_Word) - 1);
    1766                 :             : 
    1767         [ +  + ]:         104 :   for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
    1768                 :             :     {
    1769                 :          72 :       GElf_Shdr grpshdr_mem;
    1770                 :          72 :       GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
    1771                 :             :                                          &grpshdr_mem);
    1772                 :             : 
    1773                 :          72 :       const char *str;
    1774         [ +  - ]:         144 :       printf ("  [%2u] %s\n",
    1775                 :             :               grpref[cnt],
    1776                 :             :               grpshdr != NULL
    1777         [ -  + ]:          72 :               && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
    1778                 :           0 :               ? str : _("<INVALID SECTION>"));
    1779                 :             :     }
    1780                 :             : }
    1781                 :             : 
    1782                 :             : 
    1783                 :             : static void
    1784                 :         202 : print_scngrp (Ebl *ebl)
    1785                 :             : {
    1786                 :             :   /* Find all relocation sections and handle them.  */
    1787                 :         202 :   Elf_Scn *scn = NULL;
    1788                 :             : 
    1789         [ +  + ]:        6522 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    1790                 :             :     {
    1791                 :             :        /* Handle the section if it is a symbol table.  */
    1792                 :        6320 :       GElf_Shdr shdr_mem;
    1793                 :        6320 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1794                 :             : 
    1795   [ +  -  +  + ]:        6320 :       if (shdr != NULL && shdr->sh_type == SHT_GROUP)
    1796                 :             :         {
    1797         [ -  + ]:          32 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    1798                 :             :             {
    1799         [ #  # ]:           0 :               if (elf_compress (scn, 0, 0) < 0)
    1800                 :           0 :                 printf ("WARNING: %s [%zd]\n",
    1801                 :             :                         _("Couldn't uncompress section"),
    1802                 :             :                         elf_ndxscn (scn));
    1803                 :           0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    1804         [ #  # ]:           0 :               if (unlikely (shdr == NULL))
    1805                 :           0 :                 error_exit (0, _("cannot get section [%zd] header: %s"),
    1806                 :             :                             elf_ndxscn (scn),
    1807                 :             :                             elf_errmsg (-1));
    1808                 :             :             }
    1809                 :          32 :           handle_scngrp (ebl, scn, shdr);
    1810                 :             :         }
    1811                 :             :     }
    1812                 :         202 : }
    1813                 :             : 
    1814                 :             : 
    1815                 :             : static const struct flags
    1816                 :             : {
    1817                 :             :   int mask;
    1818                 :             :   const char *str;
    1819                 :             : } dt_flags[] =
    1820                 :             :   {
    1821                 :             :     { DF_ORIGIN, "ORIGIN" },
    1822                 :             :     { DF_SYMBOLIC, "SYMBOLIC" },
    1823                 :             :     { DF_TEXTREL, "TEXTREL" },
    1824                 :             :     { DF_BIND_NOW, "BIND_NOW" },
    1825                 :             :     { DF_STATIC_TLS, "STATIC_TLS" }
    1826                 :             :   };
    1827                 :             : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
    1828                 :             : 
    1829                 :             : static const struct flags dt_flags_1[] =
    1830                 :             :   {
    1831                 :             :     { DF_1_NOW, "NOW" },
    1832                 :             :     { DF_1_GLOBAL, "GLOBAL" },
    1833                 :             :     { DF_1_GROUP, "GROUP" },
    1834                 :             :     { DF_1_NODELETE, "NODELETE" },
    1835                 :             :     { DF_1_LOADFLTR, "LOADFLTR" },
    1836                 :             :     { DF_1_INITFIRST, "INITFIRST" },
    1837                 :             :     { DF_1_NOOPEN, "NOOPEN" },
    1838                 :             :     { DF_1_ORIGIN, "ORIGIN" },
    1839                 :             :     { DF_1_DIRECT, "DIRECT" },
    1840                 :             :     { DF_1_TRANS, "TRANS" },
    1841                 :             :     { DF_1_INTERPOSE, "INTERPOSE" },
    1842                 :             :     { DF_1_NODEFLIB, "NODEFLIB" },
    1843                 :             :     { DF_1_NODUMP, "NODUMP" },
    1844                 :             :     { DF_1_CONFALT, "CONFALT" },
    1845                 :             :     { DF_1_ENDFILTEE, "ENDFILTEE" },
    1846                 :             :     { DF_1_DISPRELDNE, "DISPRELDNE" },
    1847                 :             :     { DF_1_DISPRELPND, "DISPRELPND" },
    1848                 :             :   };
    1849                 :             : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
    1850                 :             : 
    1851                 :             : static const struct flags dt_feature_1[] =
    1852                 :             :   {
    1853                 :             :     { DTF_1_PARINIT, "PARINIT" },
    1854                 :             :     { DTF_1_CONFEXP, "CONFEXP" }
    1855                 :             :   };
    1856                 :             : static const int ndt_feature_1 = (sizeof (dt_feature_1)
    1857                 :             :                                   / sizeof (dt_feature_1[0]));
    1858                 :             : 
    1859                 :             : static const struct flags dt_posflag_1[] =
    1860                 :             :   {
    1861                 :             :     { DF_P1_LAZYLOAD, "LAZYLOAD" },
    1862                 :             :     { DF_P1_GROUPPERM, "GROUPPERM" }
    1863                 :             :   };
    1864                 :             : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
    1865                 :             :                                   / sizeof (dt_posflag_1[0]));
    1866                 :             : 
    1867                 :             : 
    1868                 :             : static void
    1869                 :          74 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
    1870                 :             :                 int nflags)
    1871                 :             : {
    1872                 :          74 :   bool first = true;
    1873                 :          74 :   int cnt;
    1874                 :             : 
    1875         [ +  + ]:        1104 :   for (cnt = 0; cnt < nflags; ++cnt)
    1876         [ +  + ]:        1030 :     if (d_val & flags[cnt].mask)
    1877                 :             :       {
    1878         [ +  + ]:          68 :         if (!first)
    1879                 :          36 :           putchar (' ');
    1880                 :          68 :         fputs (flags[cnt].str, stdout);
    1881                 :          68 :         d_val &= ~flags[cnt].mask;
    1882                 :          68 :         first = false;
    1883                 :             :       }
    1884                 :             : 
    1885         [ +  + ]:          74 :   if (d_val != 0)
    1886                 :             :     {
    1887         [ +  + ]:          62 :       if (!first)
    1888                 :          20 :         putchar (' ');
    1889         [ +  + ]:         116 :       printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
    1890                 :             :     }
    1891                 :             : 
    1892                 :          74 :   putchar ('\n');
    1893                 :          74 : }
    1894                 :             : 
    1895                 :             : 
    1896                 :             : static void
    1897                 :          14 : print_dt_flags (int class, GElf_Xword d_val)
    1898                 :             : {
    1899                 :          14 :   print_flags (class, d_val, dt_flags, ndt_flags);
    1900                 :          14 : }
    1901                 :             : 
    1902                 :             : 
    1903                 :             : static void
    1904                 :          56 : print_dt_flags_1 (int class, GElf_Xword d_val)
    1905                 :             : {
    1906                 :          56 :   print_flags (class, d_val, dt_flags_1, ndt_flags_1);
    1907                 :          56 : }
    1908                 :             : 
    1909                 :             : 
    1910                 :             : static void
    1911                 :           2 : print_dt_feature_1 (int class, GElf_Xword d_val)
    1912                 :             : {
    1913                 :           2 :   print_flags (class, d_val, dt_feature_1, ndt_feature_1);
    1914                 :           2 : }
    1915                 :             : 
    1916                 :             : 
    1917                 :             : static void
    1918                 :           2 : print_dt_posflag_1 (int class, GElf_Xword d_val)
    1919                 :             : {
    1920                 :           2 :   print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
    1921                 :           2 : }
    1922                 :             : 
    1923                 :             : 
    1924                 :             : static size_t
    1925                 :         136 : get_dyn_ents (Elf_Data * dyn_data)
    1926                 :             : {
    1927                 :         136 :   GElf_Dyn *dyn;
    1928                 :         136 :   GElf_Dyn dyn_mem;
    1929                 :         136 :   size_t dyn_idx = 0;
    1930                 :        3414 :   do
    1931                 :             :     {
    1932                 :        3414 :       dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
    1933         [ +  - ]:        3414 :       if (dyn != NULL)
    1934                 :        3414 :         ++dyn_idx;
    1935                 :             :     }
    1936         [ +  + ]:        3414 :   while (dyn != NULL && dyn->d_tag != DT_NULL);
    1937                 :             : 
    1938                 :         136 :   return dyn_idx;
    1939                 :             : }
    1940                 :             : 
    1941                 :             : 
    1942                 :             : static void
    1943                 :         136 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
    1944                 :             : {
    1945                 :         136 :   int class = gelf_getclass (ebl->elf);
    1946                 :         136 :   GElf_Shdr glink_mem;
    1947                 :         136 :   GElf_Shdr *glink;
    1948                 :         136 :   Elf_Data *data;
    1949                 :         136 :   size_t cnt;
    1950                 :         136 :   size_t shstrndx;
    1951                 :         136 :   size_t dyn_ents;
    1952                 :             : 
    1953                 :             :   /* Get the data of the section.  */
    1954   [ +  +  +  - ]:         136 :   if (use_dynamic_segment && phdr != NULL)
    1955                 :           2 :     data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
    1956                 :             :                                 phdr->p_filesz, ELF_T_DYN);
    1957                 :             :   else
    1958                 :         134 :     data = elf_getdata (scn, NULL);
    1959                 :             : 
    1960         [ -  + ]:         136 :   if (data == NULL)
    1961                 :           0 :     return;
    1962                 :             : 
    1963                 :             :   /* Get the dynamic section entry number */
    1964                 :         136 :   dyn_ents = get_dyn_ents (data);
    1965                 :             : 
    1966   [ +  +  +  - ]:         136 :   if (!use_dynamic_segment && shdr != NULL)
    1967                 :             :     {
    1968                 :             :       /* Get the section header string table index.  */
    1969         [ -  + ]:         134 :       if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    1970                 :           0 :         error_exit (0, _("cannot get section header string table index"));
    1971                 :             : 
    1972                 :         134 :       glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
    1973         [ -  + ]:         134 :       if (glink == NULL)
    1974                 :           0 :         error_exit (0, _("invalid sh_link value in section %zu"),
    1975                 :             :                     elf_ndxscn (scn));
    1976                 :             : 
    1977                 :         134 :       printf (ngettext ("\
    1978                 :             : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    1979                 :             :                     "\
    1980                 :             : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    1981                 :             :                         dyn_ents),
    1982                 :             :               (unsigned long int) dyn_ents,
    1983                 :             :               class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    1984                 :             :               shdr->sh_offset,
    1985         [ +  + ]:         134 :               (int) shdr->sh_link,
    1986                 :         134 :               elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    1987                 :             :     }
    1988         [ +  - ]:           2 :   else if (phdr != NULL)
    1989                 :             :     {
    1990         [ +  - ]:           4 :       printf (ngettext ("\
    1991                 :             : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "\n",
    1992                 :             :                     "\
    1993                 :             : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "\n",
    1994                 :             :                         dyn_ents),
    1995                 :             :               (unsigned long int) dyn_ents,
    1996                 :             :               class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
    1997                 :             :               phdr->p_offset);
    1998                 :             :     }
    1999                 :             : 
    2000                 :         136 :   fputs (_("  Type              Value\n"), stdout);
    2001                 :             : 
    2002                 :             :   /* if --use-dynamic option is enabled,
    2003                 :             :      use the string table to get the related library info.  */
    2004                 :         136 :   Elf_Data *strtab_data = NULL;
    2005   [ +  +  +  - ]:         136 :   if (use_dynamic_segment && phdr != NULL)
    2006                 :             :     {
    2007                 :           2 :       strtab_data = get_dynscn_strtab(ebl->elf, phdr);
    2008         [ -  + ]:           2 :       if (strtab_data == NULL)
    2009                 :           0 :         error_exit (0, _("cannot get string table by using dynamic segment"));
    2010                 :             :     }
    2011                 :             : 
    2012         [ +  + ]:        3550 :   for (cnt = 0; cnt < dyn_ents; ++cnt)
    2013                 :             :     {
    2014                 :        3414 :       GElf_Dyn dynmem;
    2015                 :        3414 :       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
    2016         [ +  - ]:        3414 :       if (dyn == NULL)
    2017                 :             :         break;
    2018                 :             : 
    2019                 :        3414 :       char buf[64];
    2020                 :        3414 :       printf ("  %-17s ",
    2021                 :             :               ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
    2022                 :             : 
    2023                 :        3414 :       char *name = NULL;
    2024                 :        3414 :       if (dyn->d_tag == DT_NEEDED
    2025         [ +  + ]:        3414 :           || dyn->d_tag == DT_SONAME
    2026                 :             :           || dyn->d_tag == DT_RPATH
    2027                 :             :           || dyn->d_tag == DT_RUNPATH)
    2028                 :             :         {
    2029   [ +  +  +  - ]:         236 :           if (! use_dynamic_segment && shdr != NULL)
    2030                 :         232 :             name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
    2031         [ -  + ]:           4 :           else if (dyn->d_un.d_val < strtab_data->d_size
    2032                 :           4 :                    && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
    2033         [ -  + ]:           4 :                                strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
    2034                 :        3414 :             name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
    2035                 :             :         }
    2036                 :             : 
    2037   [ +  +  +  +  :        3414 :       switch (dyn->d_tag)
          +  +  +  +  +  
             +  +  +  + ]
    2038                 :             :         {
    2039                 :         268 :         case DT_NULL:
    2040                 :             :         case DT_DEBUG:
    2041                 :             :         case DT_BIND_NOW:
    2042                 :             :         case DT_TEXTREL:
    2043                 :             :           /* No further output.  */
    2044                 :         268 :           fputc ('\n', stdout);
    2045                 :         268 :           break;
    2046                 :             : 
    2047                 :         226 :         case DT_NEEDED:
    2048                 :         226 :           printf (_("Shared library: [%s]\n"), name);
    2049                 :         226 :           break;
    2050                 :             : 
    2051                 :           6 :         case DT_SONAME:
    2052                 :           6 :           printf (_("Library soname: [%s]\n"), name);
    2053                 :           6 :           break;
    2054                 :             : 
    2055                 :           2 :         case DT_RPATH:
    2056                 :           2 :           printf (_("Library rpath: [%s]\n"), name);
    2057                 :           2 :           break;
    2058                 :             : 
    2059                 :           2 :         case DT_RUNPATH:
    2060                 :           2 :           printf (_("Library runpath: [%s]\n"), name);
    2061                 :           2 :           break;
    2062                 :             : 
    2063                 :         902 :         case DT_PLTRELSZ:
    2064                 :             :         case DT_RELASZ:
    2065                 :             :         case DT_STRSZ:
    2066                 :             :         case DT_RELSZ:
    2067                 :             :         case DT_RELRSZ:
    2068                 :             :         case DT_RELAENT:
    2069                 :             :         case DT_SYMENT:
    2070                 :             :         case DT_RELENT:
    2071                 :             :         case DT_RELRENT:
    2072                 :             :         case DT_PLTPADSZ:
    2073                 :             :         case DT_MOVEENT:
    2074                 :             :         case DT_MOVESZ:
    2075                 :             :         case DT_INIT_ARRAYSZ:
    2076                 :             :         case DT_FINI_ARRAYSZ:
    2077                 :             :         case DT_SYMINSZ:
    2078                 :             :         case DT_SYMINENT:
    2079                 :             :         case DT_GNU_CONFLICTSZ:
    2080                 :             :         case DT_GNU_LIBLISTSZ:
    2081                 :         902 :           printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
    2082                 :         902 :           break;
    2083                 :             : 
    2084                 :         212 :         case DT_VERDEFNUM:
    2085                 :             :         case DT_VERNEEDNUM:
    2086                 :             :         case DT_RELACOUNT:
    2087                 :             :         case DT_RELCOUNT:
    2088                 :         212 :           printf ("%" PRId64 "\n", dyn->d_un.d_val);
    2089                 :         212 :           break;
    2090                 :             : 
    2091                 :         108 :         case DT_PLTREL:;
    2092                 :         108 :           const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
    2093                 :             :                                                       NULL, 0);
    2094         [ +  + ]:         108 :           puts (tagname ?: "???");
    2095                 :         108 :           break;
    2096                 :             : 
    2097                 :          14 :         case DT_FLAGS:
    2098                 :          14 :           print_dt_flags (class, dyn->d_un.d_val);
    2099                 :          14 :           break;
    2100                 :             : 
    2101                 :          56 :         case DT_FLAGS_1:
    2102                 :          56 :           print_dt_flags_1 (class, dyn->d_un.d_val);
    2103                 :          56 :           break;
    2104                 :             : 
    2105                 :           2 :         case DT_FEATURE_1:
    2106                 :           2 :           print_dt_feature_1 (class, dyn->d_un.d_val);
    2107                 :           2 :           break;
    2108                 :             : 
    2109                 :           2 :         case DT_POSFLAG_1:
    2110                 :           2 :           print_dt_posflag_1 (class, dyn->d_un.d_val);
    2111                 :           2 :           break;
    2112                 :             : 
    2113                 :        1614 :         default:
    2114         [ +  + ]:        1614 :           printf ("%#0*" PRIx64 "\n",
    2115                 :             :                   class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
    2116                 :        1614 :           break;
    2117                 :             :         }
    2118                 :             :     }
    2119                 :             : }
    2120                 :             : 
    2121                 :             : 
    2122                 :             : /* Print the dynamic segment.  */
    2123                 :             : static void
    2124                 :         190 : print_dynamic (Ebl *ebl)
    2125                 :             : {
    2126         [ +  + ]:        1072 :   for (size_t i = 0; i < phnum; ++i)
    2127                 :             :     {
    2128                 :        1060 :       GElf_Phdr phdr_mem;
    2129                 :        1060 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
    2130                 :             : 
    2131   [ +  -  +  + ]:        1060 :       if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
    2132                 :             :         {
    2133                 :         178 :           Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
    2134                 :         178 :           GElf_Shdr shdr_mem;
    2135                 :         178 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2136         [ +  + ]:         178 :           if ((use_dynamic_segment && phdr != NULL)
    2137   [ +  +  +  + ]:         176 :               || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
    2138                 :         136 :             handle_dynamic (ebl, scn, shdr, phdr);
    2139                 :         178 :           break;
    2140                 :             :         }
    2141                 :             :     }
    2142                 :         190 : }
    2143                 :             : 
    2144                 :             : 
    2145                 :             : /* Print relocations.  */
    2146                 :             : static void
    2147                 :         194 : print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
    2148                 :             : {
    2149                 :             :   /* Find all relocation sections and handle them.  */
    2150                 :         194 :   Elf_Scn *scn = NULL;
    2151                 :             : 
    2152         [ +  + ]:      530466 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    2153                 :             :     {
    2154                 :             :        /* Handle the section if it is a symbol table.  */
    2155                 :      530272 :       GElf_Shdr shdr_mem;
    2156                 :      530272 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2157                 :             : 
    2158         [ +  - ]:      530272 :       if (likely (shdr != NULL))
    2159                 :             :         {
    2160         [ +  + ]:      530272 :           if (shdr->sh_type == SHT_REL)
    2161                 :          92 :             handle_relocs_rel (ebl, ehdr, scn, shdr);
    2162         [ +  + ]:      530180 :           else if (shdr->sh_type == SHT_RELA)
    2163                 :         282 :             handle_relocs_rela (ebl, ehdr, scn, shdr);
    2164         [ -  + ]:      529898 :           else if (shdr->sh_type == SHT_RELR)
    2165                 :           0 :             handle_relocs_relr (ebl, mod, scn, shdr);
    2166                 :             :         }
    2167                 :             :     }
    2168                 :         194 : }
    2169                 :             : 
    2170                 :             : 
    2171                 :             : /* Handle a relocation section.  */
    2172                 :             : static void
    2173                 :          92 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
    2174                 :             : {
    2175                 :          92 :   int class = gelf_getclass (ebl->elf);
    2176                 :          92 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
    2177                 :          92 :   int nentries = shdr->sh_size / sh_entsize;
    2178                 :             : 
    2179                 :             :   /* Get the data of the section.  */
    2180                 :          92 :   Elf_Data *data = elf_getdata (scn, NULL);
    2181         [ +  - ]:          92 :   if (data == NULL)
    2182                 :           0 :     return;
    2183                 :             : 
    2184                 :             :   /* Get the symbol table information.  */
    2185                 :          92 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    2186                 :          92 :   GElf_Shdr symshdr_mem;
    2187                 :          92 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    2188                 :          92 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    2189                 :             : 
    2190                 :             :   /* Get the section header of the section the relocations are for.  */
    2191                 :          92 :   GElf_Shdr destshdr_mem;
    2192                 :          92 :   GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
    2193                 :             :                                       &destshdr_mem);
    2194                 :             : 
    2195   [ +  -  -  + ]:          92 :   if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
    2196                 :             :     {
    2197                 :           0 :       printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
    2198                 :             :               shdr->sh_offset);
    2199                 :           0 :       return;
    2200                 :             :     }
    2201                 :             : 
    2202                 :             :   /* Search for the optional extended section index table if there are
    2203                 :             :      more than 64k sections.  */
    2204                 :          92 :   Elf_Data *xndxdata = NULL;
    2205         [ -  + ]:          92 :   int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
    2206         [ #  # ]:           0 :   if (unlikely (xndxscnidx > 0))
    2207                 :           0 :     xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
    2208                 :             : 
    2209                 :             :   /* Get the section header string table index.  */
    2210                 :          92 :   size_t shstrndx;
    2211         [ -  + ]:          92 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2212                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    2213                 :             : 
    2214         [ +  + ]:          92 :   if (shdr->sh_info != 0)
    2215                 :          76 :     printf (ngettext ("\
    2216                 :             : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2217                 :             :                     "\
    2218                 :             : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2219                 :             :                       nentries),
    2220                 :             :             elf_ndxscn (scn),
    2221                 :          76 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2222                 :          76 :             (unsigned int) shdr->sh_info,
    2223                 :          76 :             elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
    2224                 :             :             shdr->sh_offset,
    2225                 :             :             nentries);
    2226                 :             :   else
    2227                 :             :     /* The .rel.dyn section does not refer to a specific section but
    2228                 :             :        instead of section index zero.  Do not try to print a section
    2229                 :             :        name.  */
    2230                 :          16 :     printf (ngettext ("\
    2231                 :             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2232                 :             :                     "\
    2233                 :             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2234                 :             :                       nentries),
    2235                 :          16 :             (unsigned int) elf_ndxscn (scn),
    2236                 :          16 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2237                 :             :             shdr->sh_offset,
    2238                 :             :             nentries);
    2239         [ +  - ]:          92 :   fputs (class == ELFCLASS32
    2240                 :          92 :                   ? _("\
    2241                 :             :   Offset      Type                 Value       Name\n")
    2242                 :           0 :                   : _("\
    2243                 :             :   Offset              Type                 Value               Name\n"),
    2244                 :             :          stdout);
    2245                 :             : 
    2246                 :          92 :   int is_statically_linked = 0;
    2247         [ +  + ]:       25942 :   for (int cnt = 0; cnt < nentries; ++cnt)
    2248                 :             :     {
    2249                 :       25850 :       GElf_Rel relmem;
    2250                 :       25850 :       GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
    2251         [ +  - ]:       25850 :       if (likely (rel != NULL))
    2252                 :             :         {
    2253                 :       25850 :           char buf[128];
    2254                 :       25850 :           GElf_Sym symmem;
    2255                 :       25850 :           Elf32_Word xndx;
    2256                 :       51700 :           GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
    2257                 :       25850 :                                             GELF_R_SYM (rel->r_info),
    2258                 :             :                                             &symmem, &xndx);
    2259         [ -  + ]:       25850 :           if (unlikely (sym == NULL))
    2260                 :             :             {
    2261                 :             :               /* As a special case we have to handle relocations in static
    2262                 :             :                  executables.  This only happens for IRELATIVE relocations
    2263                 :             :                  (so far).  There is no symbol table.  */
    2264         [ #  # ]:           0 :               if (is_statically_linked == 0)
    2265                 :             :                 {
    2266                 :             :                   /* Find the program header and look for a PT_INTERP entry. */
    2267                 :           0 :                   is_statically_linked = -1;
    2268         [ #  # ]:           0 :                   if (ehdr->e_type == ET_EXEC)
    2269                 :             :                     {
    2270                 :           0 :                       is_statically_linked = 1;
    2271                 :             : 
    2272         [ #  # ]:           0 :                       for (size_t inner = 0; inner < phnum; ++inner)
    2273                 :             :                         {
    2274                 :           0 :                           GElf_Phdr phdr_mem;
    2275                 :           0 :                           GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
    2276                 :             :                                                           &phdr_mem);
    2277   [ #  #  #  # ]:           0 :                           if (phdr != NULL && phdr->p_type == PT_INTERP)
    2278                 :             :                             {
    2279                 :           0 :                               is_statically_linked = -1;
    2280                 :           0 :                               break;
    2281                 :             :                             }
    2282                 :             :                         }
    2283                 :             :                     }
    2284                 :             :                 }
    2285                 :             : 
    2286   [ #  #  #  # ]:           0 :               if (is_statically_linked > 0 && shdr->sh_link == 0)
    2287   [ #  #  #  # ]:           0 :                 printf ("\
    2288                 :             :   %#0*" PRIx64 "  %-20s %*s  %s\n",
    2289                 :             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2290                 :           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2291                 :             :                         /* Avoid the leading R_ which isn't carrying any
    2292                 :             :                            information.  */
    2293                 :           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2294                 :             :                                                buf, sizeof (buf)) + 2
    2295                 :           0 :                         : _("<INVALID RELOC>"),
    2296                 :             :                         class == ELFCLASS32 ? 10 : 18, "",
    2297                 :           0 :                         elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
    2298                 :             :               else
    2299   [ #  #  #  # ]:           0 :                 printf ("  %#0*" PRIx64 "  %-20s <%s %ld>\n",
    2300                 :             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2301                 :           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2302                 :             :                         /* Avoid the leading R_ which isn't carrying any
    2303                 :             :                            information.  */
    2304                 :           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2305                 :             :                                                buf, sizeof (buf)) + 2
    2306                 :           0 :                         : _("<INVALID RELOC>"),
    2307                 :             :                         _("INVALID SYMBOL"),
    2308                 :           0 :                         (long int) GELF_R_SYM (rel->r_info));
    2309                 :             :             }
    2310         [ +  + ]:       25850 :           else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
    2311         [ +  + ]:         130 :                    && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
    2312         [ -  + ]:          66 :                         && GELF_ST_BIND (sym->st_info) == STB_LOCAL
    2313         [ #  # ]:           0 :                         && sym->st_shndx != SHN_UNDEF
    2314         [ #  # ]:           0 :                         && sym->st_value == 0)) // local start section label
    2315         [ -  + ]:         260 :             printf ("  %#0*" PRIx64 "  %-20s %#0*" PRIx64 "  %s\n",
    2316                 :             :                     class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2317         [ +  - ]:         130 :                     likely (ebl_reloc_type_check (ebl,
    2318                 :             :                                                   GELF_R_TYPE (rel->r_info)))
    2319                 :             :                     /* Avoid the leading R_ which isn't carrying any
    2320                 :             :                        information.  */
    2321                 :         130 :                     ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2322                 :             :                                            buf, sizeof (buf)) + 2
    2323                 :           0 :                     : _("<INVALID RELOC>"),
    2324                 :             :                     class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2325                 :         130 :                     elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
    2326                 :             :           else
    2327                 :             :             {
    2328                 :             :               /* This is a relocation against a STT_SECTION symbol
    2329                 :             :                  or a local start section label for which we print
    2330                 :             :                  section name.  */
    2331                 :       25720 :               GElf_Shdr secshdr_mem;
    2332                 :       25720 :               GElf_Shdr *secshdr;
    2333                 :       25720 :               secshdr = gelf_getshdr (elf_getscn (ebl->elf,
    2334         [ +  - ]:       25720 :                                                   sym->st_shndx == SHN_XINDEX
    2335                 :           0 :                                                   ? xndx : sym->st_shndx),
    2336                 :             :                                       &secshdr_mem);
    2337                 :             : 
    2338         [ -  + ]:       25720 :               if (unlikely (secshdr == NULL))
    2339   [ #  #  #  # ]:           0 :                 printf ("  %#0*" PRIx64 "  %-20s <%s %ld>\n",
    2340                 :             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2341                 :           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2342                 :             :                         /* Avoid the leading R_ which isn't carrying any
    2343                 :             :                            information.  */
    2344                 :           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2345                 :             :                                                buf, sizeof (buf)) + 2
    2346                 :           0 :                         : _("<INVALID RELOC>"),
    2347                 :             :                         _("INVALID SECTION"),
    2348         [ #  # ]:           0 :                         (long int) (sym->st_shndx == SHN_XINDEX
    2349                 :           0 :                                     ? xndx : sym->st_shndx));
    2350                 :             :               else
    2351   [ -  +  +  - ]:       77160 :                 printf ("  %#0*" PRIx64 "  %-20s %#0*" PRIx64 "  %s\n",
    2352                 :             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2353                 :       25720 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2354                 :             :                         /* Avoid the leading R_ which isn't carrying any
    2355                 :             :                            information.  */
    2356                 :       25720 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2357                 :             :                                                buf, sizeof (buf)) + 2
    2358                 :           0 :                         : _("<INVALID RELOC>"),
    2359                 :             :                         class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2360                 :       25720 :                         elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
    2361                 :             :             }
    2362                 :             :         }
    2363                 :             :     }
    2364                 :             : }
    2365                 :             : 
    2366                 :             : 
    2367                 :             : /* Handle a relocation section.  */
    2368                 :             : static void
    2369                 :         282 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
    2370                 :             : {
    2371                 :         282 :   int class = gelf_getclass (ebl->elf);
    2372                 :         282 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
    2373                 :         282 :   int nentries = shdr->sh_size / sh_entsize;
    2374                 :             : 
    2375                 :             :   /* Get the data of the section.  */
    2376                 :         282 :   Elf_Data *data = elf_getdata (scn, NULL);
    2377         [ +  - ]:         282 :   if (data == NULL)
    2378                 :           0 :     return;
    2379                 :             : 
    2380                 :             :   /* Get the symbol table information.  */
    2381                 :         282 :   Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
    2382                 :         282 :   GElf_Shdr symshdr_mem;
    2383                 :         282 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    2384                 :         282 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    2385                 :             : 
    2386                 :             :   /* Get the section header of the section the relocations are for.  */
    2387                 :         282 :   GElf_Shdr destshdr_mem;
    2388                 :         282 :   GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
    2389                 :             :                                       &destshdr_mem);
    2390                 :             : 
    2391   [ +  -  -  + ]:         282 :   if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
    2392                 :             :     {
    2393                 :           0 :       printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
    2394                 :             :               shdr->sh_offset);
    2395                 :           0 :       return;
    2396                 :             :     }
    2397                 :             : 
    2398                 :             :   /* Search for the optional extended section index table if there are
    2399                 :             :      more than 64k sections.  */
    2400                 :         282 :   Elf_Data *xndxdata = NULL;
    2401         [ +  + ]:         282 :   int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
    2402         [ -  + ]:          16 :   if (unlikely (xndxscnidx > 0))
    2403                 :          16 :     xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
    2404                 :             : 
    2405                 :             :   /* Get the section header string table index.  */
    2406                 :         282 :   size_t shstrndx;
    2407         [ -  + ]:         282 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2408                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    2409                 :             : 
    2410         [ +  + ]:         282 :   if (shdr->sh_info != 0)
    2411                 :         168 :     printf (ngettext ("\
    2412                 :             : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2413                 :             :                     "\
    2414                 :             : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2415                 :             :                     nentries),
    2416                 :             :           elf_ndxscn (scn),
    2417                 :         168 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2418                 :         168 :           (unsigned int) shdr->sh_info,
    2419                 :         168 :           elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
    2420                 :             :           shdr->sh_offset,
    2421                 :             :           nentries);
    2422                 :             :   else
    2423                 :             :     /* The .rela.dyn section does not refer to a specific section but
    2424                 :             :        instead of section index zero.  Do not try to print a section
    2425                 :             :        name.  */
    2426                 :         114 :     printf (ngettext ("\
    2427                 :             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2428                 :             :                     "\
    2429                 :             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2430                 :             :                       nentries),
    2431                 :         114 :             (unsigned int) elf_ndxscn (scn),
    2432                 :         114 :             elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2433                 :             :             shdr->sh_offset,
    2434                 :             :             nentries);
    2435         [ -  + ]:         282 :   fputs (class == ELFCLASS32
    2436                 :           0 :                   ? _("\
    2437                 :             :   Offset      Type            Value       Addend Name\n")
    2438                 :         282 :                   : _("\
    2439                 :             :   Offset              Type            Value               Addend Name\n"),
    2440                 :             :                   stdout);
    2441                 :             : 
    2442                 :         282 :   int is_statically_linked = 0;
    2443         [ +  + ]:     1168710 :   for (int cnt = 0; cnt < nentries; ++cnt)
    2444                 :             :     {
    2445                 :     1168428 :       GElf_Rela relmem;
    2446                 :     1168428 :       GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
    2447         [ +  - ]:     1168428 :       if (likely (rel != NULL))
    2448                 :             :         {
    2449                 :     1168428 :           char buf[64];
    2450                 :     1168428 :           GElf_Sym symmem;
    2451                 :     1168428 :           Elf32_Word xndx;
    2452                 :     2336856 :           GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
    2453                 :     1168428 :                                             GELF_R_SYM (rel->r_info),
    2454                 :             :                                             &symmem, &xndx);
    2455                 :             : 
    2456         [ -  + ]:     1168428 :           if (unlikely (sym == NULL))
    2457                 :             :             {
    2458                 :             :               /* As a special case we have to handle relocations in static
    2459                 :             :                  executables.  This only happens for IRELATIVE relocations
    2460                 :             :                  (so far).  There is no symbol table.  */
    2461         [ #  # ]:           0 :               if (is_statically_linked == 0)
    2462                 :             :                 {
    2463                 :             :                   /* Find the program header and look for a PT_INTERP entry. */
    2464                 :           0 :                   is_statically_linked = -1;
    2465         [ #  # ]:           0 :                   if (ehdr->e_type == ET_EXEC)
    2466                 :             :                     {
    2467                 :           0 :                       is_statically_linked = 1;
    2468                 :             : 
    2469         [ #  # ]:           0 :                       for (size_t inner = 0; inner < phnum; ++inner)
    2470                 :             :                         {
    2471                 :           0 :                           GElf_Phdr phdr_mem;
    2472                 :           0 :                           GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
    2473                 :             :                                                           &phdr_mem);
    2474   [ #  #  #  # ]:           0 :                           if (phdr != NULL && phdr->p_type == PT_INTERP)
    2475                 :             :                             {
    2476                 :           0 :                               is_statically_linked = -1;
    2477                 :           0 :                               break;
    2478                 :             :                             }
    2479                 :             :                         }
    2480                 :             :                     }
    2481                 :             :                 }
    2482                 :             : 
    2483   [ #  #  #  # ]:           0 :               if (is_statically_linked > 0 && shdr->sh_link == 0)
    2484   [ #  #  #  # ]:           0 :                 printf ("\
    2485                 :             :   %#0*" PRIx64 "  %-15s %*s  %#6" PRIx64 " %s\n",
    2486                 :             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2487                 :           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2488                 :             :                         /* Avoid the leading R_ which isn't carrying any
    2489                 :             :                            information.  */
    2490                 :           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2491                 :             :                                                buf, sizeof (buf)) + 2
    2492                 :           0 :                         : _("<INVALID RELOC>"),
    2493                 :             :                         class == ELFCLASS32 ? 10 : 18, "",
    2494                 :             :                         rel->r_addend,
    2495                 :           0 :                         elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
    2496                 :             :               else
    2497   [ #  #  #  # ]:           0 :                 printf ("  %#0*" PRIx64 "  %-15s <%s %ld>\n",
    2498                 :             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2499                 :           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2500                 :             :                         /* Avoid the leading R_ which isn't carrying any
    2501                 :             :                            information.  */
    2502                 :           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2503                 :             :                                                buf, sizeof (buf)) + 2
    2504                 :           0 :                         : _("<INVALID RELOC>"),
    2505                 :             :                         _("INVALID SYMBOL"),
    2506                 :           0 :                         (long int) GELF_R_SYM (rel->r_info));
    2507                 :             :             }
    2508         [ +  + ]:     1168428 :           else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
    2509         [ +  + ]:      630544 :                    && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
    2510         [ +  + ]:      103788 :                         && GELF_ST_BIND (sym->st_info) == STB_LOCAL
    2511         [ +  + ]:      102250 :                         && sym->st_shndx != SHN_UNDEF
    2512         [ +  + ]:         716 :                         && sym->st_value == 0)) // local start section label
    2513         [ +  - ]:     1261064 :             printf ("\
    2514                 :             :   %#0*" PRIx64 "  %-15s %#0*" PRIx64 "  %+6" PRId64 " %s\n",
    2515                 :             :                     class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2516         [ +  - ]:      630532 :                     likely (ebl_reloc_type_check (ebl,
    2517                 :             :                                                   GELF_R_TYPE (rel->r_info)))
    2518                 :             :                     /* Avoid the leading R_ which isn't carrying any
    2519                 :             :                        information.  */
    2520                 :      630532 :                     ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2521                 :             :                                            buf, sizeof (buf)) + 2
    2522                 :           0 :                     : _("<INVALID RELOC>"),
    2523                 :             :                     class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2524                 :             :                     rel->r_addend,
    2525                 :      630532 :                     elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
    2526                 :             :           else
    2527                 :             :             {
    2528                 :             :               /* This is a relocation against a STT_SECTION symbol
    2529                 :             :                  or a local start section label for which we print
    2530                 :             :                  section name.  */
    2531                 :      537896 :               GElf_Shdr secshdr_mem;
    2532                 :      537896 :               GElf_Shdr *secshdr;
    2533                 :      537896 :               secshdr = gelf_getshdr (elf_getscn (ebl->elf,
    2534         [ +  + ]:      537896 :                                                   sym->st_shndx == SHN_XINDEX
    2535                 :        2088 :                                                   ? xndx : sym->st_shndx),
    2536                 :             :                                       &secshdr_mem);
    2537                 :             : 
    2538         [ -  + ]:      537896 :               if (unlikely (secshdr == NULL))
    2539   [ #  #  #  # ]:           0 :                 printf ("  %#0*" PRIx64 "  %-15s <%s %ld>\n",
    2540                 :             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2541                 :           0 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2542                 :             :                         /* Avoid the leading R_ which isn't carrying any
    2543                 :             :                            information.  */
    2544                 :           0 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2545                 :             :                                                buf, sizeof (buf)) + 2
    2546                 :           0 :                         : _("<INVALID RELOC>"),
    2547                 :             :                         _("INVALID SECTION"),
    2548         [ #  # ]:           0 :                         (long int) (sym->st_shndx == SHN_XINDEX
    2549                 :           0 :                                     ? xndx : sym->st_shndx));
    2550                 :             :               else
    2551   [ +  -  +  - ]:     1613688 :                 printf ("\
    2552                 :             :   %#0*" PRIx64 "  %-15s %#0*" PRIx64 "  %+6" PRId64 " %s\n",
    2553                 :             :                         class == ELFCLASS32 ? 10 : 18, rel->r_offset,
    2554                 :      537896 :                         ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
    2555                 :             :                         /* Avoid the leading R_ which isn't carrying any
    2556                 :             :                            information.  */
    2557                 :      537896 :                         ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
    2558                 :             :                                                buf, sizeof (buf)) + 2
    2559                 :           0 :                         : _("<INVALID RELOC>"),
    2560                 :             :                         class == ELFCLASS32 ? 10 : 18, sym->st_value,
    2561                 :             :                         rel->r_addend,
    2562                 :      537896 :                         elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
    2563                 :             :             }
    2564                 :             :         }
    2565                 :             :     }
    2566                 :             : }
    2567                 :             : 
    2568                 :             : /* Handle a relocation section.  */
    2569                 :             : static void
    2570                 :           0 : handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn, GElf_Shdr *shdr)
    2571                 :             : {
    2572                 :           0 :   int class = gelf_getclass (ebl->elf);
    2573                 :           0 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELR, 1, EV_CURRENT);
    2574                 :           0 :   int nentries = shdr->sh_size / sh_entsize;
    2575                 :             : 
    2576                 :             :   /* Get the data of the section.  */
    2577                 :           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    2578         [ #  # ]:           0 :   if (data == NULL)
    2579                 :           0 :     return;
    2580                 :             : 
    2581                 :             :   /* Get the section header string table index.  */
    2582                 :           0 :   size_t shstrndx;
    2583         [ #  # ]:           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2584                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    2585                 :             : 
    2586                 :             :   /* A .relr.dyn section does not refer to a specific section.  */
    2587                 :           0 :   printf (ngettext ("\
    2588                 :             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    2589                 :             :                     "\
    2590                 :             : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    2591                 :             :                     nentries),
    2592                 :           0 :           (unsigned int) elf_ndxscn (scn),
    2593                 :           0 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    2594                 :             :           shdr->sh_offset,
    2595                 :             :           nentries);
    2596                 :             : 
    2597         [ #  # ]:           0 :   if (class == ELFCLASS32)
    2598                 :             :     {
    2599                 :             :       uint32_t base = 0;
    2600         [ #  # ]:           0 :       for (int cnt = 0; cnt < nentries; ++cnt)
    2601                 :             :         {
    2602                 :           0 :           Elf32_Word *words = data->d_buf;
    2603                 :           0 :           Elf32_Word entry = words[cnt];
    2604                 :             : 
    2605                 :             :           /* Just the raw entries?  */
    2606         [ #  # ]:           0 :           if (print_unresolved_addresses)
    2607                 :           0 :             printf ("  %#010" PRIx32 "%s\n", entry,
    2608         [ #  # ]:           0 :                     (entry & 1) == 0 ? " *" : "");
    2609                 :             :           else
    2610                 :             :             {
    2611                 :             :               /* A real address, also sets base.  */
    2612         [ #  # ]:           0 :               if ((entry & 1) == 0)
    2613                 :             :                 {
    2614                 :           0 :                   printf ("  ");
    2615                 :           0 :                   print_dwarf_addr (mod, 4, entry, entry, stdout);
    2616                 :           0 :                   printf (" *\n");
    2617                 :             : 
    2618                 :           0 :                   base = entry + 4;
    2619                 :             :                 }
    2620                 :             :               else
    2621                 :             :                 {
    2622                 :             :                   /* Untangle address from base and bits.  */
    2623                 :             :                   uint32_t addr;
    2624         [ #  # ]:           0 :                   for (addr = base; (entry >>= 1) != 0; addr += 4)
    2625         [ #  # ]:           0 :                     if ((entry & 1) != 0)
    2626                 :             :                       {
    2627                 :           0 :                         printf ("  ");
    2628                 :           0 :                         print_dwarf_addr (mod, 4, addr, addr, stdout);
    2629                 :           0 :                         printf ("\n");
    2630                 :             :                       }
    2631                 :           0 :                   base += 4 * (4 * 8 - 1);
    2632                 :             :                 }
    2633                 :             :             }
    2634                 :             :         }
    2635                 :             :     }
    2636                 :             :   else
    2637                 :             :     {
    2638                 :             :       uint64_t base = 0;
    2639         [ #  # ]:           0 :       for (int cnt = 0; cnt < nentries; ++cnt)
    2640                 :             :         {
    2641                 :           0 :           Elf64_Xword *xwords = data->d_buf;
    2642                 :           0 :           Elf64_Xword entry = xwords[cnt];
    2643                 :             : 
    2644                 :             :           /* Just the raw entries?  */
    2645         [ #  # ]:           0 :           if (print_unresolved_addresses)
    2646                 :           0 :             printf ("  %#018" PRIx64 "%s\n", entry,
    2647         [ #  # ]:           0 :                     (entry & 1) == 0 ? " *" : "");
    2648                 :             :           else
    2649                 :             :             {
    2650                 :             :               /* A real address, also sets base.  */
    2651         [ #  # ]:           0 :               if ((entry & 1) == 0)
    2652                 :             :                 {
    2653                 :           0 :                   printf ("  ");
    2654                 :           0 :                   print_dwarf_addr (mod, 8, entry, entry, stdout);
    2655                 :           0 :                   printf (" *\n");
    2656                 :             : 
    2657                 :           0 :                   base = entry + 8;
    2658                 :             :                 }
    2659                 :             :               else
    2660                 :             :                 {
    2661                 :             :                   /* Untangle address from base and bits.  */
    2662                 :             :                   uint64_t addr;
    2663         [ #  # ]:           0 :                   for (addr = base; (entry >>= 1) != 0; addr += 8)
    2664         [ #  # ]:           0 :                     if ((entry & 1) != 0)
    2665                 :             :                       {
    2666                 :           0 :                         printf ("  ");
    2667                 :           0 :                         print_dwarf_addr (mod, 8, addr, addr, stdout);
    2668                 :           0 :                         printf ("\n");
    2669                 :             :                       }
    2670                 :           0 :                   base += 8 * (8 * 8 - 1);
    2671                 :             :                 }
    2672                 :             :             }
    2673                 :             :         }
    2674                 :             :     }
    2675                 :             : }
    2676                 :             : 
    2677                 :             : /* Print the program header.  Return true if a symtab is printed,
    2678                 :             :    false otherwise.  */
    2679                 :             : static bool
    2680                 :         438 : print_symtab (Ebl *ebl, int type)
    2681                 :             : {
    2682                 :             :   /* Use the dynamic section info to display symbol tables.  */
    2683   [ +  -  -  - ]:         438 :   if (use_dynamic_segment && type == SHT_DYNSYM)
    2684                 :           0 :     return handle_dynamic_symtab(ebl);
    2685                 :             : 
    2686                 :             :   /* Find the symbol table(s).  For this we have to search through the
    2687                 :             :      section table.  */
    2688                 :             :   Elf_Scn *scn = NULL;
    2689                 :             :   bool symtab_printed = false;
    2690                 :             : 
    2691         [ +  + ]:       14056 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    2692                 :             :     {
    2693                 :             :       /* Handle the section if it is a symbol table.  */
    2694                 :       13618 :       GElf_Shdr shdr_mem;
    2695                 :       13618 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2696                 :             : 
    2697   [ +  -  +  + ]:       13618 :       if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
    2698                 :             :         {
    2699         [ +  + ]:         274 :           if (symbol_table_section != NULL)
    2700                 :             :             {
    2701                 :             :               /* Get the section header string table index.  */
    2702                 :          14 :               size_t shstrndx;
    2703                 :          14 :               const char *sname;
    2704         [ -  + ]:          14 :               if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2705                 :           0 :                 error_exit (0,
    2706                 :             :                             _("cannot get section header string table index"));
    2707                 :          14 :               sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
    2708   [ +  -  +  + ]:          14 :               if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
    2709                 :           8 :                 continue;
    2710                 :             :             }
    2711                 :             : 
    2712         [ -  + ]:         266 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
    2713                 :             :             {
    2714         [ #  # ]:           0 :               if (elf_compress (scn, 0, 0) < 0)
    2715                 :           0 :                 printf ("WARNING: %s [%zd]\n",
    2716                 :             :                         _("Couldn't uncompress section"),
    2717                 :             :                         elf_ndxscn (scn));
    2718                 :           0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    2719         [ #  # ]:           0 :               if (unlikely (shdr == NULL))
    2720                 :           0 :                 error_exit (0,
    2721                 :             :                             _("cannot get section [%zd] header: %s"),
    2722                 :             :                             elf_ndxscn (scn), elf_errmsg (-1));
    2723                 :             :             }
    2724                 :         266 :           symtab_printed = handle_symtab (ebl, scn, shdr);
    2725                 :             :         }
    2726                 :             :     }
    2727                 :             : 
    2728                 :             :   return symtab_printed;
    2729                 :             : }
    2730                 :             : 
    2731                 :             : 
    2732                 :             : static void
    2733                 :         266 : process_symtab (Ebl *ebl, unsigned int nsyms, Elf64_Word idx,
    2734                 :             :                 Elf32_Word verneed_stridx, Elf32_Word verdef_stridx,
    2735                 :             :                 Elf_Data *symdata, Elf_Data *versym_data,
    2736                 :             :                 Elf_Data *symstr_data, Elf_Data *verneed_data,
    2737                 :             :                 Elf_Data *verdef_data, Elf_Data *xndx_data)
    2738                 :             : {
    2739         [ +  + ]:       80832 :   for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
    2740                 :             :     {
    2741                 :       80566 :       char typebuf[64];
    2742                 :       80566 :       char bindbuf[64];
    2743                 :       80566 :       char scnbuf[64];
    2744                 :       80566 :       const char *sym_name;
    2745                 :       80566 :       Elf32_Word xndx;
    2746                 :       80566 :       GElf_Sym sym_mem;
    2747                 :       80566 :       GElf_Sym *sym
    2748                 :       80566 :           = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
    2749                 :             : 
    2750         [ -  + ]:       80566 :       if (unlikely (sym == NULL))
    2751                 :           0 :         continue;
    2752                 :             : 
    2753                 :             :       /* Determine the real section index.  */
    2754         [ +  - ]:       80566 :       if (likely (sym->st_shndx != SHN_XINDEX))
    2755                 :       80566 :         xndx = sym->st_shndx;
    2756         [ -  + ]:       80566 :       if (use_dynamic_segment == true)
    2757                 :             :         {
    2758         [ #  # ]:           0 :           if (validate_str (symstr_data->d_buf, sym->st_name,
    2759                 :             :                             symstr_data->d_size))
    2760                 :           0 :             sym_name = (char *)symstr_data->d_buf + sym->st_name;
    2761                 :             :           else
    2762                 :             :             sym_name = NULL;
    2763                 :             :         }
    2764                 :             :       else
    2765                 :       80566 :         sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
    2766                 :             : 
    2767         [ -  + ]:       80566 :       if (sym_name == NULL)
    2768                 :             :         sym_name = "???";
    2769                 :             : 
    2770         [ +  + ]:      160244 :       printf (_ ("\
    2771                 :             : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
    2772                 :       80566 :               cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
    2773                 :             :               sym->st_value, sym->st_size,
    2774                 :       80566 :               ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
    2775                 :             :                                     sizeof (typebuf)),
    2776                 :       80566 :               ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
    2777                 :             :                                        bindbuf, sizeof (bindbuf)),
    2778                 :       80566 :               get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
    2779                 :       80566 :               ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
    2780                 :             :                                 sizeof (scnbuf), NULL, shnum),
    2781                 :             :               sym_name);
    2782                 :             : 
    2783         [ +  + ]:       80566 :       if (versym_data != NULL)
    2784                 :             :         {
    2785                 :             :           /* Get the version information.  */
    2786                 :        3436 :           GElf_Versym versym_mem;
    2787                 :        3436 :           GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
    2788                 :             : 
    2789   [ +  -  +  + ]:        3436 :           if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
    2790                 :             :             {
    2791                 :        2476 :               bool is_nobits = false;
    2792                 :        2476 :               bool check_def = xndx != SHN_UNDEF;
    2793                 :             : 
    2794   [ +  +  -  + ]:        2476 :               if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
    2795                 :             :                 {
    2796                 :        2456 :                   GElf_Shdr symshdr_mem;
    2797                 :        2456 :                   GElf_Shdr *symshdr = gelf_getshdr (
    2798                 :             :                       elf_getscn (ebl->elf, xndx), &symshdr_mem);
    2799                 :             : 
    2800                 :        2456 :                   is_nobits
    2801   [ +  -  +  + ]:        4888 :                       = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
    2802                 :             :                 }
    2803                 :             : 
    2804         [ +  + ]:        2476 :               if (is_nobits || !check_def)
    2805                 :             :                 {
    2806                 :             :                   /* We must test both.  */
    2807                 :        2172 :                   GElf_Vernaux vernaux_mem;
    2808                 :        2172 :                   GElf_Vernaux *vernaux = NULL;
    2809                 :        2172 :                   size_t vn_offset = 0;
    2810                 :             : 
    2811                 :        2172 :                   GElf_Verneed verneed_mem;
    2812                 :        2172 :                   GElf_Verneed *verneed
    2813                 :        2172 :                       = gelf_getverneed (verneed_data, 0, &verneed_mem);
    2814         [ +  - ]:        7902 :                   while (verneed != NULL)
    2815                 :             :                     {
    2816                 :        7902 :                       size_t vna_offset = vn_offset;
    2817                 :             : 
    2818                 :        7902 :                       vernaux = gelf_getvernaux (verneed_data,
    2819                 :        7902 :                                                  vna_offset += verneed->vn_aux,
    2820                 :             :                                                  &vernaux_mem);
    2821         [ +  + ]:       28762 :                       while (vernaux != NULL && vernaux->vna_other != *versym
    2822         [ +  + ]:       18688 :                              && vernaux->vna_next != 0
    2823         [ +  - ]:       20860 :                              && (verneed_data->d_size - vna_offset
    2824         [ +  - ]:       12958 :                                  >= vernaux->vna_next))
    2825                 :             :                         {
    2826                 :             :                           /* Update the offset.  */
    2827                 :       12958 :                           vna_offset += vernaux->vna_next;
    2828                 :             : 
    2829                 :       12958 :                           vernaux = (vernaux->vna_next == 0
    2830                 :             :                                          ? NULL
    2831                 :       12958 :                                          : gelf_getvernaux (verneed_data,
    2832                 :             :                                                             vna_offset,
    2833                 :             :                                                             &vernaux_mem));
    2834                 :             :                         }
    2835                 :             : 
    2836                 :             :                       /* Check whether we found the version.  */
    2837   [ +  -  +  + ]:        7902 :                       if (vernaux != NULL && vernaux->vna_other == *versym)
    2838                 :             :                         /* Found it.  */
    2839                 :             :                         break;
    2840                 :             : 
    2841         [ +  - ]:        5730 :                       if (verneed_data->d_size - vn_offset < verneed->vn_next)
    2842                 :             :                         break;
    2843                 :             : 
    2844                 :        5730 :                       vn_offset += verneed->vn_next;
    2845                 :        5730 :                       verneed
    2846                 :             :                           = (verneed->vn_next == 0
    2847                 :             :                                  ? NULL
    2848         [ +  - ]:        5730 :                                  : gelf_getverneed (verneed_data, vn_offset,
    2849                 :             :                                                     &verneed_mem));
    2850                 :             :                     }
    2851                 :             : 
    2852   [ +  -  +  - ]:        2172 :                   if (vernaux != NULL && vernaux->vna_other == *versym)
    2853                 :             :                     {
    2854                 :        4344 :                       printf ("@%s (%u)",
    2855         [ -  + ]:        2172 :                               use_dynamic_segment == true
    2856                 :           0 :                                   ? (char *)symstr_data->d_buf
    2857                 :           0 :                                         + vernaux->vna_name
    2858                 :        2172 :                                   : elf_strptr (ebl->elf, verneed_stridx,
    2859                 :        2172 :                                                 vernaux->vna_name),
    2860                 :             :                               (unsigned int)vernaux->vna_other);
    2861                 :        2172 :                       check_def = 0;
    2862                 :             :                     }
    2863         [ #  # ]:           0 :                   else if (unlikely (!is_nobits))
    2864                 :           0 :                     error (0, 0, _ ("bad dynamic symbol"));
    2865                 :             :                   else
    2866                 :             :                     check_def = 1;
    2867                 :             :                 }
    2868                 :             : 
    2869   [ +  -  +  - ]:        2476 :               if (check_def && *versym != 0x8001)
    2870                 :             :                 {
    2871                 :             :                   /* We must test both.  */
    2872                 :         304 :                   size_t vd_offset = 0;
    2873                 :             : 
    2874                 :         304 :                   GElf_Verdef verdef_mem;
    2875                 :         304 :                   GElf_Verdef *verdef
    2876                 :         304 :                       = gelf_getverdef (verdef_data, 0, &verdef_mem);
    2877         [ +  - ]:         866 :                   while (verdef != NULL)
    2878                 :             :                     {
    2879         [ +  + ]:         866 :                       if (verdef->vd_ndx == (*versym & 0x7fff))
    2880                 :             :                         /* Found the definition.  */
    2881                 :             :                         break;
    2882                 :             : 
    2883         [ +  - ]:         562 :                       if (verdef_data->d_size - vd_offset < verdef->vd_next)
    2884                 :             :                         break;
    2885                 :             : 
    2886                 :         562 :                       vd_offset += verdef->vd_next;
    2887                 :         562 :                       verdef = (verdef->vd_next == 0
    2888                 :             :                                     ? NULL
    2889         [ +  - ]:         562 :                                     : gelf_getverdef (verdef_data, vd_offset,
    2890                 :             :                                                       &verdef_mem));
    2891                 :             :                     }
    2892                 :             : 
    2893         [ +  - ]:         304 :                   if (verdef != NULL)
    2894                 :             :                     {
    2895                 :         304 :                       GElf_Verdaux verdaux_mem;
    2896                 :         608 :                       GElf_Verdaux *verdaux = gelf_getverdaux (
    2897                 :         304 :                           verdef_data, vd_offset + verdef->vd_aux,
    2898                 :             :                           &verdaux_mem);
    2899                 :             : 
    2900         [ +  - ]:         304 :                       if (verdaux != NULL)
    2901         [ +  - ]:         608 :                         printf ((*versym & 0x8000) ? "@%s" : "@@%s",
    2902         [ -  + ]:         304 :                                 use_dynamic_segment == true
    2903                 :           0 :                                     ? (char *)symstr_data->d_buf
    2904                 :           0 :                                           + verdaux->vda_name
    2905                 :         304 :                                     : elf_strptr (ebl->elf, verdef_stridx,
    2906                 :         304 :                                                   verdaux->vda_name));
    2907                 :             :                     }
    2908                 :             :                 }
    2909                 :             :             }
    2910                 :             :         }
    2911                 :             : 
    2912                 :       80566 :       putchar ('\n');
    2913                 :             :     }
    2914                 :         266 : }
    2915                 :             : 
    2916                 :             : 
    2917                 :             : static bool
    2918                 :         266 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    2919                 :             : {
    2920                 :         266 :   Elf_Data *versym_data = NULL;
    2921                 :         266 :   Elf_Data *verneed_data = NULL;
    2922                 :         266 :   Elf_Data *verdef_data = NULL;
    2923                 :         266 :   Elf_Data *xndx_data = NULL;
    2924                 :         266 :   int class = gelf_getclass (ebl->elf);
    2925                 :         266 :   Elf32_Word verneed_stridx = 0;
    2926                 :         266 :   Elf32_Word verdef_stridx = 0;
    2927                 :             : 
    2928                 :             :   /* Get the data of the section.  */
    2929                 :         266 :   Elf_Data *data = elf_getdata (scn, NULL);
    2930         [ +  - ]:         266 :   if (data == NULL)
    2931                 :             :     return false;
    2932                 :             : 
    2933                 :             :   /* Find out whether we have other sections we might need.  */
    2934                 :             :   Elf_Scn *runscn = NULL;
    2935         [ +  + ]:        8784 :   while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
    2936                 :             :     {
    2937                 :        8518 :       GElf_Shdr runshdr_mem;
    2938                 :        8518 :       GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
    2939                 :             : 
    2940         [ +  - ]:        8518 :       if (likely (runshdr != NULL))
    2941                 :             :         {
    2942         [ +  + ]:        8518 :           if (runshdr->sh_type == SHT_GNU_versym
    2943         [ +  + ]:         202 :               && runshdr->sh_link == elf_ndxscn (scn))
    2944                 :             :             /* Bingo, found the version information.  Now get the data.  */
    2945                 :         150 :             versym_data = elf_getdata (runscn, NULL);
    2946         [ +  + ]:        8368 :           else if (runshdr->sh_type == SHT_GNU_verneed)
    2947                 :             :             {
    2948                 :             :               /* This is the information about the needed versions.  */
    2949                 :         202 :               verneed_data = elf_getdata (runscn, NULL);
    2950                 :         202 :               verneed_stridx = runshdr->sh_link;
    2951                 :             :             }
    2952         [ +  + ]:        8166 :           else if (runshdr->sh_type == SHT_GNU_verdef)
    2953                 :             :             {
    2954                 :             :               /* This is the information about the defined versions.  */
    2955                 :           8 :               verdef_data = elf_getdata (runscn, NULL);
    2956                 :           8 :               verdef_stridx = runshdr->sh_link;
    2957                 :             :             }
    2958         [ +  - ]:        8158 :           else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
    2959         [ #  # ]:           0 :               && runshdr->sh_link == elf_ndxscn (scn))
    2960                 :             :             /* Extended section index.  */
    2961                 :           0 :             xndx_data = elf_getdata (runscn, NULL);
    2962                 :             :         }
    2963                 :             :     }
    2964                 :             : 
    2965                 :             :   /* Get the section header string table index.  */
    2966                 :         266 :   size_t shstrndx;
    2967         [ -  + ]:         266 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    2968                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    2969                 :             : 
    2970                 :         266 :   GElf_Shdr glink_mem;
    2971                 :         266 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    2972                 :             :                                    &glink_mem);
    2973         [ -  + ]:         266 :   if (glink == NULL)
    2974                 :           0 :     error_exit (0, _("invalid sh_link value in section %zu"),
    2975                 :             :                 elf_ndxscn (scn));
    2976                 :             : 
    2977                 :             :   /* Now we can compute the number of entries in the section.  */
    2978                 :         532 :   unsigned int nsyms = data->d_size / (class == ELFCLASS32
    2979                 :             :                                        ? sizeof (Elf32_Sym)
    2980         [ +  + ]:         266 :                                        : sizeof (Elf64_Sym));
    2981                 :             : 
    2982                 :         266 :   printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
    2983                 :             :                     "\nSymbol table [%2u] '%s' contains %u entries:\n",
    2984                 :             :                     nsyms),
    2985                 :         266 :           (unsigned int) elf_ndxscn (scn),
    2986                 :         266 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
    2987                 :         266 :   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
    2988                 :             :                     " %lu local symbols  String table: [%2u] '%s'\n",
    2989                 :             :                     shdr->sh_info),
    2990                 :         266 :           (unsigned long int) shdr->sh_info,
    2991                 :         266 :           (unsigned int) shdr->sh_link,
    2992                 :         266 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    2993                 :             : 
    2994         [ +  + ]:         532 :   fputs (class == ELFCLASS32
    2995                 :          32 :                   ? _("\
    2996                 :             :   Num:    Value   Size Type    Bind   Vis          Ndx Name\n")
    2997                 :         234 :                   : _("\
    2998                 :             :   Num:            Value   Size Type    Bind   Vis          Ndx Name\n"),
    2999                 :             :                   stdout);
    3000                 :             : 
    3001                 :         266 :         process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
    3002                 :             :         data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
    3003                 :         266 :     return true;
    3004                 :             : }
    3005                 :             : 
    3006                 :             : 
    3007                 :             : static bool
    3008                 :           0 : handle_dynamic_symtab (Ebl *ebl)
    3009                 :             : {
    3010                 :           0 :   GElf_Phdr phdr_mem;
    3011                 :           0 :   GElf_Phdr *phdr = NULL;
    3012                 :             :   /* phnum is a static variable which was already fetched in function
    3013                 :             :      process_elf_file.  */
    3014         [ #  # ]:           0 :   for (size_t i = 0; i < phnum; ++i)
    3015                 :             :     {
    3016                 :           0 :       phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
    3017   [ #  #  #  # ]:           0 :       if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
    3018                 :             :         break;
    3019                 :             :     }
    3020         [ #  # ]:           0 :   if (phdr == NULL)
    3021                 :             :     return false;
    3022                 :             : 
    3023                 :           0 :   GElf_Addr addrs[i_max] = {
    3024                 :             :     0,
    3025                 :             :   };
    3026                 :           0 :   GElf_Off offs[i_max] = {
    3027                 :             :     0,
    3028                 :             :   };
    3029                 :           0 :   get_dynscn_addrs (ebl->elf, phdr, addrs);
    3030                 :           0 :   find_offsets (ebl->elf, 0, i_max, addrs, offs);
    3031                 :             : 
    3032                 :           0 :   size_t syments = 0;
    3033                 :             : 
    3034                 :           0 :   GElf_Ehdr ehdr_mem;
    3035                 :           0 :   GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
    3036                 :             : 
    3037         [ #  # ]:           0 :   if (offs[i_hash] != 0)
    3038                 :             :     {
    3039                 :             :       /* In the original format, .hash says the size of .dynsym.  */
    3040                 :             : 
    3041   [ #  #  #  #  :           0 :       size_t entsz = SH_ENTSIZE_HASH (ehdr);
                   #  # ]
    3042                 :             :       Elf_Data *data
    3043                 :           0 :           = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
    3044                 :             :                                   (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
    3045         [ #  # ]:           0 :       if (data != NULL)
    3046                 :           0 :         syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
    3047         [ #  # ]:           0 :                               : *(const GElf_Xword *)data->d_buf);
    3048                 :             :     }
    3049   [ #  #  #  # ]:           0 :   if (offs[i_gnu_hash] != 0 && syments == 0)
    3050                 :             :     {
    3051                 :             :       /* In the new format, we can derive it with some work.  */
    3052                 :             : 
    3053                 :           0 :       const struct
    3054                 :             :       {
    3055                 :             :         Elf32_Word nbuckets;
    3056                 :             :         Elf32_Word symndx;
    3057                 :             :         Elf32_Word maskwords;
    3058                 :             :         Elf32_Word shift2;
    3059                 :             :       } * header;
    3060                 :             : 
    3061                 :           0 :       Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
    3062                 :             :                                              sizeof *header, ELF_T_WORD);
    3063         [ #  # ]:           0 :       if (data != NULL)
    3064                 :             :         {
    3065                 :           0 :           header = data->d_buf;
    3066                 :           0 :           Elf32_Word nbuckets = header->nbuckets;
    3067                 :           0 :           Elf32_Word symndx = header->symndx;
    3068                 :           0 :           GElf_Off buckets_at
    3069                 :             :               = (offs[i_gnu_hash] + sizeof *header
    3070                 :           0 :                  + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
    3071                 :           0 :                     * header->maskwords));
    3072                 :             : 
    3073                 :             :           // elf_getdata_rawchunk takes a size_t, make sure it
    3074                 :             :           // doesn't overflow.
    3075                 :             : #if SIZE_MAX <= UINT32_MAX
    3076                 :             :           if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
    3077                 :             :             data = NULL;
    3078                 :             :           else
    3079                 :             : #endif
    3080                 :           0 :             data = elf_getdata_rawchunk (ebl->elf, buckets_at,
    3081                 :             :                                          nbuckets * sizeof (Elf32_Word),
    3082                 :             :                                          ELF_T_WORD);
    3083         [ #  # ]:           0 :           if (data != NULL && symndx < nbuckets)
    3084                 :             :             {
    3085                 :           0 :               const Elf32_Word *const buckets = data->d_buf;
    3086                 :           0 :               Elf32_Word maxndx = symndx;
    3087         [ #  # ]:           0 :               for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
    3088                 :           0 :                 if (buckets[bucket] > maxndx)
    3089                 :             :                   maxndx = buckets[bucket];
    3090                 :             : 
    3091                 :           0 :               GElf_Off hasharr_at
    3092                 :             :                   = (buckets_at + nbuckets * sizeof (Elf32_Word));
    3093                 :           0 :               hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
    3094                 :           0 :               do
    3095                 :             :                 {
    3096                 :           0 :                   data = elf_getdata_rawchunk (
    3097                 :             :                       ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
    3098   [ #  #  #  # ]:           0 :                   if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
    3099                 :             :                     {
    3100                 :           0 :                       syments = maxndx + 1;
    3101                 :           0 :                       break;
    3102                 :             :                     }
    3103                 :           0 :                   ++maxndx;
    3104                 :           0 :                   hasharr_at += sizeof (Elf32_Word);
    3105                 :             :                 }
    3106         [ #  # ]:           0 :               while (data != NULL);
    3107                 :             :             }
    3108                 :             :         }
    3109                 :             :     }
    3110   [ #  #  #  # ]:           0 :   if (offs[i_strtab] > offs[i_symtab] && syments == 0)
    3111                 :           0 :     syments = ((offs[i_strtab] - offs[i_symtab])
    3112                 :           0 :                / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
    3113                 :             : 
    3114   [ #  #  #  #  :           0 :   if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
                   #  # ]
    3115                 :             :     {
    3116                 :           0 :       error_exit (0, _ ("Dynamic symbol information is not available for "
    3117                 :             :                         "displaying symbols."));
    3118                 :             :     }
    3119                 :             : 
    3120                 :             :   /* All the data chunk initializaion.  */
    3121                 :           0 :   Elf_Data *symdata = NULL;
    3122                 :           0 :   Elf_Data *symstrdata = NULL;
    3123                 :           0 :   Elf_Data *versym_data = NULL;
    3124                 :           0 :   Elf_Data *verdef_data = NULL;
    3125                 :           0 :   Elf_Data *verneed_data = NULL;
    3126                 :             : 
    3127                 :           0 :   if (offs[i_symtab] != 0)
    3128                 :           0 :     symdata = elf_getdata_rawchunk (
    3129                 :             :         ebl->elf, offs[i_symtab],
    3130                 :             :         gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
    3131                 :             : 
    3132         [ #  # ]:           0 :   if (offs[i_strtab] != 0 && addrs[i_strsz] != 0)
    3133                 :           0 :     symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
    3134                 :             :                                        ELF_T_BYTE);
    3135                 :             : 
    3136         [ #  # ]:           0 :   if (offs[i_versym] != 0)
    3137                 :           0 :     versym_data = elf_getdata_rawchunk (
    3138                 :             :         ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
    3139                 :             : 
    3140                 :             :   /* Get the verneed_data without vernaux.  */
    3141   [ #  #  #  # ]:           0 :   if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
    3142                 :             :     {
    3143                 :           0 :       verneed_data = elf_getdata_rawchunk (
    3144                 :             :         ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
    3145                 :             :         ELF_T_VNEED);
    3146                 :             : 
    3147         [ #  # ]:           0 :       if (verneed_data->d_size < sizeof (GElf_Verneed))
    3148                 :           0 :         error_exit (0, _("malformed SHT_GNU_verneed data"));
    3149                 :             :     }
    3150                 :             : 
    3151                 :           0 :   size_t vernauxnum = 0;
    3152                 :           0 :   size_t vn_next_offset = 0;
    3153                 :             : 
    3154         [ #  # ]:           0 :   if (verneed_data != NULL && verneed_data->d_buf != NULL)
    3155         [ #  # ]:           0 :     for (size_t i = 0; i < addrs[i_verneednum]; i++)
    3156                 :             :       {
    3157         [ #  # ]:           0 :         if (vn_next_offset > (verneed_data->d_size - sizeof (GElf_Verneed)))
    3158                 :           0 :           error_exit (0, _("invalid SHT_GNU_verneed data"));
    3159                 :             : 
    3160                 :           0 :         GElf_Verneed *verneed
    3161                 :             :           = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
    3162                 :           0 :         vernauxnum += verneed->vn_cnt;
    3163                 :           0 :         vn_next_offset += verneed->vn_next;
    3164                 :             :       }
    3165                 :             : 
    3166                 :             :   /* Update the verneed_data to include the vernaux.  */
    3167   [ #  #  #  # ]:           0 :   if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
    3168                 :           0 :     verneed_data = elf_getdata_rawchunk (
    3169                 :             :         ebl->elf, offs[i_verneed],
    3170                 :           0 :         (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed),
    3171                 :             :         ELF_T_VNEED);
    3172                 :             : 
    3173                 :             :   /* Get the verdef_data without verdaux.  */
    3174   [ #  #  #  # ]:           0 :   if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
    3175                 :             :     {
    3176                 :           0 :       verdef_data = elf_getdata_rawchunk (
    3177                 :             :         ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
    3178                 :             :         ELF_T_VDEF);
    3179                 :             : 
    3180         [ #  # ]:           0 :       if (verdef_data->d_size < sizeof (GElf_Verdef))
    3181                 :           0 :         error_exit (0, _("malformed SHT_GNU_verdef data"));
    3182                 :             :     }
    3183                 :             : 
    3184                 :           0 :   size_t verdauxnum = 0;
    3185                 :           0 :   size_t vd_next_offset = 0;
    3186                 :             : 
    3187         [ #  # ]:           0 :   if (verdef_data != NULL && verdef_data->d_buf != NULL)
    3188         [ #  # ]:           0 :     for (size_t i = 0; i < addrs[i_verdefnum]; i++)
    3189                 :             :       {
    3190         [ #  # ]:           0 :         if (vd_next_offset > (verdef_data->d_size - sizeof (GElf_Verdef)))
    3191                 :           0 :           error_exit (0, _("invalid SHT_GNU_verdef data"));
    3192                 :             : 
    3193                 :           0 :         GElf_Verdef *verdef
    3194                 :             :           = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
    3195                 :           0 :         verdauxnum += verdef->vd_cnt;
    3196                 :           0 :         vd_next_offset += verdef->vd_next;
    3197                 :             :       }
    3198                 :             : 
    3199                 :             :   /* Update the verdef_data to include the verdaux.  */
    3200   [ #  #  #  # ]:           0 :   if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
    3201                 :           0 :     verdef_data = elf_getdata_rawchunk (
    3202                 :             :         ebl->elf, offs[i_verdef],
    3203                 :           0 :         (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
    3204                 :             : 
    3205                 :           0 :   unsigned int nsyms = (unsigned int)syments;
    3206                 :           0 :   process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
    3207                 :             :                   verneed_data, verdef_data, NULL);
    3208                 :           0 :   return true;
    3209                 :             : }
    3210                 :             : 
    3211                 :             : 
    3212                 :             : /* Print version information.  */
    3213                 :             : static void
    3214                 :         184 : print_verinfo (Ebl *ebl)
    3215                 :             : {
    3216                 :             :   /* Find the version information sections.  For this we have to
    3217                 :             :      search through the section table.  */
    3218                 :         184 :   Elf_Scn *scn = NULL;
    3219                 :             : 
    3220         [ +  + ]:        6004 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    3221                 :             :     {
    3222                 :             :       /* Handle the section if it is part of the versioning handling.  */
    3223                 :        5820 :       GElf_Shdr shdr_mem;
    3224                 :        5820 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    3225                 :             : 
    3226         [ +  - ]:        5820 :       if (likely (shdr != NULL))
    3227                 :             :         {
    3228         [ +  + ]:        5820 :           if (shdr->sh_type == SHT_GNU_verneed)
    3229                 :         130 :             handle_verneed (ebl, scn, shdr);
    3230         [ +  + ]:        5690 :           else if (shdr->sh_type == SHT_GNU_verdef)
    3231                 :           4 :             handle_verdef (ebl, scn, shdr);
    3232         [ +  + ]:        5686 :           else if (shdr->sh_type == SHT_GNU_versym)
    3233                 :         130 :             handle_versym (ebl, scn, shdr);
    3234                 :             :         }
    3235                 :             :     }
    3236                 :         184 : }
    3237                 :             : 
    3238                 :             : 
    3239                 :             : static const char *
    3240                 :         380 : get_ver_flags (unsigned int flags)
    3241                 :             : {
    3242                 :         380 :   static char buf[32];
    3243                 :         380 :   char *endp;
    3244                 :             : 
    3245         [ +  + ]:         380 :   if (flags == 0)
    3246                 :         376 :     return _("none");
    3247                 :             : 
    3248         [ +  - ]:           4 :   if (flags & VER_FLG_BASE)
    3249                 :           4 :     endp = stpcpy (buf, "BASE ");
    3250                 :             :   else
    3251                 :             :     endp = buf;
    3252                 :             : 
    3253         [ -  + ]:           4 :   if (flags & VER_FLG_WEAK)
    3254                 :             :     {
    3255         [ #  # ]:           0 :       if (endp != buf)
    3256                 :           0 :         endp = stpcpy (endp, "| ");
    3257                 :             : 
    3258                 :           0 :       endp = stpcpy (endp, "WEAK ");
    3259                 :             :     }
    3260                 :             : 
    3261         [ -  + ]:           4 :   if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
    3262                 :             :     {
    3263                 :           0 :       strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
    3264                 :           0 :       buf[sizeof (buf) - 1] = '\0';
    3265                 :             :     }
    3266                 :             : 
    3267                 :             :   return buf;
    3268                 :             : }
    3269                 :             : 
    3270                 :             : 
    3271                 :             : static void
    3272                 :         130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    3273                 :             : {
    3274                 :         130 :   int class = gelf_getclass (ebl->elf);
    3275                 :             : 
    3276                 :             :   /* Get the data of the section.  */
    3277                 :         130 :   Elf_Data *data = elf_getdata (scn, NULL);
    3278         [ -  + ]:         130 :   if (data == NULL)
    3279                 :           0 :     return;
    3280                 :             : 
    3281                 :             :   /* Get the section header string table index.  */
    3282                 :         130 :   size_t shstrndx;
    3283         [ -  + ]:         130 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3284                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    3285                 :             : 
    3286                 :         130 :   GElf_Shdr glink_mem;
    3287                 :         130 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    3288                 :             :                                    &glink_mem);
    3289         [ -  + ]:         130 :   if (glink == NULL)
    3290                 :           0 :     error_exit (0, _("invalid sh_link value in section %zu"),
    3291                 :             :                 elf_ndxscn (scn));
    3292                 :             : 
    3293                 :         130 :   printf (ngettext ("\
    3294                 :             : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    3295                 :             :                     "\
    3296                 :             : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    3297                 :             :                     shdr->sh_info),
    3298                 :         130 :           (unsigned int) elf_ndxscn (scn),
    3299                 :         130 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
    3300                 :             :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    3301                 :             :           shdr->sh_offset,
    3302         [ +  + ]:         130 :           (unsigned int) shdr->sh_link,
    3303                 :         130 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3304                 :             : 
    3305                 :         130 :   unsigned int offset = 0;
    3306         [ +  - ]:         184 :   for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
    3307                 :             :     {
    3308                 :             :       /* Get the data at the next offset.  */
    3309                 :         184 :       GElf_Verneed needmem;
    3310                 :         184 :       GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
    3311         [ +  - ]:         184 :       if (unlikely (need == NULL))
    3312                 :             :         break;
    3313                 :             : 
    3314                 :         552 :       printf (_("  %#06x: Version: %hu  File: %s  Cnt: %hu\n"),
    3315                 :         184 :               offset, (unsigned short int) need->vn_version,
    3316                 :         184 :               elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
    3317                 :         184 :               (unsigned short int) need->vn_cnt);
    3318                 :             : 
    3319                 :         184 :       unsigned int auxoffset = offset + need->vn_aux;
    3320         [ +  - ]:         356 :       for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
    3321                 :             :         {
    3322                 :         356 :           GElf_Vernaux auxmem;
    3323                 :         356 :           GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
    3324         [ +  - ]:         356 :           if (unlikely (aux == NULL))
    3325                 :             :             break;
    3326                 :             : 
    3327                 :         356 :           printf (_("  %#06x: Name: %s  Flags: %s  Version: %hu\n"),
    3328                 :             :                   auxoffset,
    3329                 :         356 :                   elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
    3330                 :         356 :                   get_ver_flags (aux->vna_flags),
    3331                 :         356 :                   (unsigned short int) aux->vna_other);
    3332                 :             : 
    3333         [ +  + ]:         356 :           if (aux->vna_next == 0)
    3334                 :             :             break;
    3335                 :             : 
    3336                 :         172 :           auxoffset += aux->vna_next;
    3337                 :             :         }
    3338                 :             : 
    3339                 :             :       /* Find the next offset.  */
    3340         [ +  + ]:         184 :       if (need->vn_next == 0)
    3341                 :             :         break;
    3342                 :             : 
    3343                 :          54 :       offset += need->vn_next;
    3344                 :             :     }
    3345                 :             : }
    3346                 :             : 
    3347                 :             : 
    3348                 :             : static void
    3349                 :           4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    3350                 :             : {
    3351                 :             :   /* Get the data of the section.  */
    3352                 :           4 :   Elf_Data *data = elf_getdata (scn, NULL);
    3353         [ -  + ]:           4 :   if (data == NULL)
    3354                 :           0 :     return;
    3355                 :             : 
    3356                 :             :   /* Get the section header string table index.  */
    3357                 :           4 :   size_t shstrndx;
    3358         [ -  + ]:           4 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3359                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    3360                 :             : 
    3361                 :           4 :   GElf_Shdr glink_mem;
    3362                 :           4 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    3363                 :             :                                    &glink_mem);
    3364         [ -  + ]:           4 :   if (glink == NULL)
    3365                 :           0 :     error_exit (0, _("invalid sh_link value in section %zu"),
    3366                 :             :                 elf_ndxscn (scn));
    3367                 :             : 
    3368                 :           4 :   int class = gelf_getclass (ebl->elf);
    3369                 :           4 :   printf (ngettext ("\
    3370                 :             : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    3371                 :             :                     "\
    3372                 :             : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
    3373                 :             :                     shdr->sh_info),
    3374                 :           4 :           (unsigned int) elf_ndxscn (scn),
    3375                 :           4 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3376                 :             :           shdr->sh_info,
    3377                 :             :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    3378                 :             :           shdr->sh_offset,
    3379         [ +  - ]:           4 :           (unsigned int) shdr->sh_link,
    3380                 :           4 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3381                 :             : 
    3382                 :           4 :   unsigned int offset = 0;
    3383         [ +  - ]:          24 :   for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
    3384                 :             :     {
    3385                 :             :       /* Get the data at the next offset.  */
    3386                 :          24 :       GElf_Verdef defmem;
    3387                 :          24 :       GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
    3388         [ +  - ]:          24 :       if (unlikely (def == NULL))
    3389                 :             :         break;
    3390                 :             : 
    3391                 :          24 :       unsigned int auxoffset = offset + def->vd_aux;
    3392                 :          24 :       GElf_Verdaux auxmem;
    3393                 :          24 :       GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
    3394         [ +  - ]:          24 :       if (unlikely (aux == NULL))
    3395                 :             :         break;
    3396                 :             : 
    3397                 :          96 :       printf (_("\
    3398                 :             :   %#06x: Version: %hd  Flags: %s  Index: %hd  Cnt: %hd  Name: %s\n"),
    3399                 :          24 :               offset, def->vd_version,
    3400                 :          24 :               get_ver_flags (def->vd_flags),
    3401                 :          24 :               def->vd_ndx,
    3402                 :          24 :               def->vd_cnt,
    3403                 :          24 :               elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
    3404                 :             : 
    3405                 :          24 :       auxoffset += aux->vda_next;
    3406         [ +  + ]:          24 :       for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
    3407                 :             :         {
    3408                 :          16 :           aux = gelf_getverdaux (data, auxoffset, &auxmem);
    3409         [ +  - ]:          16 :           if (unlikely (aux == NULL))
    3410                 :             :             break;
    3411                 :             : 
    3412                 :          16 :           printf (_("  %#06x: Parent %d: %s\n"),
    3413                 :             :                   auxoffset, cnt2,
    3414                 :          16 :                   elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
    3415                 :             : 
    3416         [ -  + ]:          16 :           if (aux->vda_next == 0)
    3417                 :             :             break;
    3418                 :             : 
    3419                 :           0 :           auxoffset += aux->vda_next;
    3420                 :             :         }
    3421                 :             : 
    3422                 :             :       /* Find the next offset.  */
    3423         [ +  + ]:          24 :       if (def->vd_next == 0)
    3424                 :             :         break;
    3425                 :          20 :       offset += def->vd_next;
    3426                 :             :     }
    3427                 :             : }
    3428                 :             : 
    3429                 :             : 
    3430                 :             : static void
    3431                 :         130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
    3432                 :             : {
    3433                 :         130 :   int class = gelf_getclass (ebl->elf);
    3434                 :         130 :   const char **vername;
    3435                 :         130 :   const char **filename;
    3436                 :             : 
    3437                 :             :   /* Get the data of the section.  */
    3438                 :         130 :   Elf_Data *data = elf_getdata (scn, NULL);
    3439         [ +  - ]:         130 :   if (data == NULL)
    3440                 :           0 :     return;
    3441                 :             : 
    3442                 :             :   /* Get the section header string table index.  */
    3443                 :         130 :   size_t shstrndx;
    3444         [ -  + ]:         130 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    3445                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    3446                 :             : 
    3447                 :             :   /* We have to find the version definition section and extract the
    3448                 :             :      version names.  */
    3449                 :             :   Elf_Scn *defscn = NULL;
    3450                 :             :   Elf_Scn *needscn = NULL;
    3451                 :             : 
    3452                 :             :   Elf_Scn *verscn = NULL;
    3453         [ +  + ]:        4046 :   while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
    3454                 :             :     {
    3455                 :        3916 :       GElf_Shdr vershdr_mem;
    3456                 :        3916 :       GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
    3457                 :             : 
    3458         [ -  + ]:        3916 :       if (likely (vershdr != NULL))
    3459                 :             :         {
    3460         [ +  + ]:        3916 :           if (vershdr->sh_type == SHT_GNU_verdef)
    3461                 :             :             defscn = verscn;
    3462         [ +  + ]:        3912 :           else if (vershdr->sh_type == SHT_GNU_verneed)
    3463                 :        3916 :             needscn = verscn;
    3464                 :             :         }
    3465                 :             :     }
    3466                 :             : 
    3467                 :         130 :   size_t nvername;
    3468         [ +  - ]:         130 :   if (defscn != NULL || needscn != NULL)
    3469                 :             :     {
    3470                 :             :       /* We have a version information (better should have).  Now get
    3471                 :             :          the version names.  First find the maximum version number.  */
    3472                 :         130 :       nvername = 0;
    3473         [ +  + ]:         130 :       if (defscn != NULL)
    3474                 :             :         {
    3475                 :             :           /* Run through the version definitions and find the highest
    3476                 :             :              index.  */
    3477                 :           4 :           unsigned int offset = 0;
    3478                 :           4 :           Elf_Data *defdata;
    3479                 :           4 :           GElf_Shdr defshdrmem;
    3480                 :           4 :           GElf_Shdr *defshdr;
    3481                 :             : 
    3482                 :           4 :           defdata = elf_getdata (defscn, NULL);
    3483         [ +  - ]:           4 :           if (unlikely (defdata == NULL))
    3484                 :           0 :             return;
    3485                 :             : 
    3486                 :           4 :           defshdr = gelf_getshdr (defscn, &defshdrmem);
    3487         [ +  - ]:           4 :           if (unlikely (defshdr == NULL))
    3488                 :             :             return;
    3489                 :             : 
    3490         [ +  - ]:          24 :           for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
    3491                 :             :             {
    3492                 :          24 :               GElf_Verdef defmem;
    3493                 :          24 :               GElf_Verdef *def;
    3494                 :             : 
    3495                 :             :               /* Get the data at the next offset.  */
    3496                 :          24 :               def = gelf_getverdef (defdata, offset, &defmem);
    3497         [ +  - ]:          24 :               if (unlikely (def == NULL))
    3498                 :             :                 break;
    3499                 :             : 
    3500                 :          24 :               nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
    3501                 :             : 
    3502         [ +  + ]:          24 :               if (def->vd_next == 0)
    3503                 :             :                 break;
    3504                 :          20 :               offset += def->vd_next;
    3505                 :             :             }
    3506                 :             :         }
    3507         [ +  - ]:         130 :       if (needscn != NULL)
    3508                 :             :         {
    3509                 :         130 :           unsigned int offset = 0;
    3510                 :         130 :           Elf_Data *needdata;
    3511                 :         130 :           GElf_Shdr needshdrmem;
    3512                 :         130 :           GElf_Shdr *needshdr;
    3513                 :             : 
    3514                 :         130 :           needdata = elf_getdata (needscn, NULL);
    3515         [ +  - ]:         130 :           if (unlikely (needdata == NULL))
    3516                 :           0 :             return;
    3517                 :             : 
    3518                 :         130 :           needshdr = gelf_getshdr (needscn, &needshdrmem);
    3519         [ +  - ]:         130 :           if (unlikely (needshdr == NULL))
    3520                 :             :             return;
    3521                 :             : 
    3522         [ +  - ]:         184 :           for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
    3523                 :             :             {
    3524                 :         184 :               GElf_Verneed needmem;
    3525                 :         184 :               GElf_Verneed *need;
    3526                 :         184 :               unsigned int auxoffset;
    3527                 :         184 :               int cnt2;
    3528                 :             : 
    3529                 :             :               /* Get the data at the next offset.  */
    3530                 :         184 :               need = gelf_getverneed (needdata, offset, &needmem);
    3531         [ +  - ]:         184 :               if (unlikely (need == NULL))
    3532                 :             :                 break;
    3533                 :             : 
    3534                 :             :               /* Run through the auxiliary entries.  */
    3535                 :         184 :               auxoffset = offset + need->vn_aux;
    3536         [ +  - ]:         356 :               for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
    3537                 :             :                 {
    3538                 :         356 :                   GElf_Vernaux auxmem;
    3539                 :         356 :                   GElf_Vernaux *aux;
    3540                 :             : 
    3541                 :         356 :                   aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
    3542         [ +  - ]:         356 :                   if (unlikely (aux == NULL))
    3543                 :             :                     break;
    3544                 :             : 
    3545                 :         356 :                   nvername = MAX (nvername,
    3546                 :             :                                   (size_t) (aux->vna_other & 0x7fff));
    3547                 :             : 
    3548         [ +  + ]:         356 :                   if (aux->vna_next == 0)
    3549                 :             :                     break;
    3550                 :         172 :                   auxoffset += aux->vna_next;
    3551                 :             :                 }
    3552                 :             : 
    3553         [ +  + ]:         184 :               if (need->vn_next == 0)
    3554                 :             :                 break;
    3555                 :          54 :               offset += need->vn_next;
    3556                 :             :             }
    3557                 :             :         }
    3558                 :             : 
    3559                 :             :       /* This is the number of versions we know about.  */
    3560                 :         130 :       ++nvername;
    3561                 :             : 
    3562                 :             :       /* Allocate the array.  */
    3563                 :         130 :       vername = (const char **) alloca (nvername * sizeof (const char *));
    3564         [ +  + ]:         130 :       memset(vername, 0, nvername * sizeof (const char *));
    3565                 :         130 :       filename = (const char **) alloca (nvername * sizeof (const char *));
    3566                 :         130 :       memset(filename, 0, nvername * sizeof (const char *));
    3567                 :             : 
    3568                 :             :       /* Run through the data structures again and collect the strings.  */
    3569         [ +  + ]:         130 :       if (defscn != NULL)
    3570                 :             :         {
    3571                 :             :           /* Run through the version definitions and find the highest
    3572                 :             :              index.  */
    3573                 :           4 :           unsigned int offset = 0;
    3574                 :           4 :           Elf_Data *defdata;
    3575                 :           4 :           GElf_Shdr defshdrmem;
    3576                 :           4 :           GElf_Shdr *defshdr;
    3577                 :             : 
    3578                 :           4 :           defdata = elf_getdata (defscn, NULL);
    3579         [ +  - ]:           4 :           if (unlikely (defdata == NULL))
    3580                 :           0 :             return;
    3581                 :             : 
    3582                 :           4 :           defshdr = gelf_getshdr (defscn, &defshdrmem);
    3583         [ +  - ]:           4 :           if (unlikely (defshdr == NULL))
    3584                 :             :             return;
    3585                 :             : 
    3586         [ +  - ]:          24 :           for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
    3587                 :             :             {
    3588                 :             : 
    3589                 :             :               /* Get the data at the next offset.  */
    3590                 :          24 :               GElf_Verdef defmem;
    3591                 :          24 :               GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
    3592         [ +  - ]:          24 :               if (unlikely (def == NULL))
    3593                 :             :                 break;
    3594                 :             : 
    3595                 :          24 :               GElf_Verdaux auxmem;
    3596                 :          48 :               GElf_Verdaux *aux = gelf_getverdaux (defdata,
    3597                 :          24 :                                                    offset + def->vd_aux,
    3598                 :             :                                                    &auxmem);
    3599         [ +  - ]:          24 :               if (unlikely (aux == NULL))
    3600                 :             :                 break;
    3601                 :             : 
    3602                 :          24 :               vername[def->vd_ndx & 0x7fff]
    3603                 :          24 :                 = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
    3604                 :          24 :               filename[def->vd_ndx & 0x7fff] = NULL;
    3605                 :             : 
    3606         [ +  + ]:          24 :               if (def->vd_next == 0)
    3607                 :             :                 break;
    3608                 :          20 :               offset += def->vd_next;
    3609                 :             :             }
    3610                 :             :         }
    3611         [ +  - ]:         130 :       if (needscn != NULL)
    3612                 :             :         {
    3613                 :         130 :           unsigned int offset = 0;
    3614                 :             : 
    3615                 :         130 :           Elf_Data *needdata = elf_getdata (needscn, NULL);
    3616                 :         130 :           GElf_Shdr needshdrmem;
    3617                 :         130 :           GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
    3618         [ -  + ]:         130 :           if (unlikely (needdata == NULL || needshdr == NULL))
    3619                 :           0 :             return;
    3620                 :             : 
    3621         [ +  - ]:         184 :           for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
    3622                 :             :             {
    3623                 :             :               /* Get the data at the next offset.  */
    3624                 :         184 :               GElf_Verneed needmem;
    3625                 :         184 :               GElf_Verneed *need = gelf_getverneed (needdata, offset,
    3626                 :             :                                                     &needmem);
    3627         [ +  - ]:         184 :               if (unlikely (need == NULL))
    3628                 :             :                 break;
    3629                 :             : 
    3630                 :             :               /* Run through the auxiliary entries.  */
    3631                 :         184 :               unsigned int auxoffset = offset + need->vn_aux;
    3632         [ +  - ]:         356 :               for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
    3633                 :             :                 {
    3634                 :         356 :                   GElf_Vernaux auxmem;
    3635                 :         356 :                   GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
    3636                 :             :                                                        &auxmem);
    3637         [ +  - ]:         356 :                   if (unlikely (aux == NULL))
    3638                 :             :                     break;
    3639                 :             : 
    3640                 :         356 :                   vername[aux->vna_other & 0x7fff]
    3641                 :         356 :                     = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
    3642                 :         356 :                   filename[aux->vna_other & 0x7fff]
    3643                 :         356 :                     = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
    3644                 :             : 
    3645         [ +  + ]:         356 :                   if (aux->vna_next == 0)
    3646                 :             :                     break;
    3647                 :         172 :                   auxoffset += aux->vna_next;
    3648                 :             :                 }
    3649                 :             : 
    3650         [ +  + ]:         184 :               if (need->vn_next == 0)
    3651                 :             :                 break;
    3652                 :          54 :               offset += need->vn_next;
    3653                 :             :             }
    3654                 :             :         }
    3655                 :             :     }
    3656                 :             :   else
    3657                 :             :     {
    3658                 :             :       vername = NULL;
    3659                 :             :       nvername = 1;
    3660                 :             :       filename = NULL;
    3661                 :             :     }
    3662                 :             : 
    3663                 :         130 :   GElf_Shdr glink_mem;
    3664                 :         130 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
    3665                 :             :                                    &glink_mem);
    3666                 :         130 :   size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
    3667         [ -  + ]:         130 :   if (glink == NULL)
    3668                 :           0 :     error_exit (0, _("invalid sh_link value in section %zu"),
    3669                 :             :                 elf_ndxscn (scn));
    3670                 :             : 
    3671                 :             :   /* Print the header.  */
    3672                 :         130 :   printf (ngettext ("\
    3673                 :             : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
    3674                 :             :                     "\
    3675                 :             : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
    3676                 :             :                     shdr->sh_size / sh_entsize),
    3677                 :         130 :           (unsigned int) elf_ndxscn (scn),
    3678                 :         130 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3679                 :         130 :           (int) (shdr->sh_size / sh_entsize),
    3680                 :             :           class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
    3681                 :             :           shdr->sh_offset,
    3682         [ +  + ]:         130 :           (unsigned int) shdr->sh_link,
    3683                 :         130 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3684                 :             : 
    3685                 :             :   /* Now we can finally look at the actual contents of this section.  */
    3686         [ +  + ]:        3308 :   for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
    3687                 :             :     {
    3688         [ +  + ]:        3178 :       if (cnt % 2 == 0)
    3689                 :        1608 :         printf ("\n %4d:", cnt);
    3690                 :             : 
    3691                 :        3178 :       GElf_Versym symmem;
    3692                 :        3178 :       GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
    3693         [ +  - ]:        3178 :       if (sym == NULL)
    3694                 :             :         break;
    3695                 :             : 
    3696      [ +  +  + ]:        3178 :       switch (*sym)
    3697                 :             :         {
    3698                 :         238 :           ssize_t n;
    3699                 :         238 :         case 0:
    3700                 :         238 :           fputs (_("   0 *local*                     "),
    3701                 :             :                           stdout);
    3702                 :         238 :           break;
    3703                 :             : 
    3704                 :         502 :         case 1:
    3705                 :         502 :           fputs (_("   1 *global*                    "),
    3706                 :             :                           stdout);
    3707                 :         502 :           break;
    3708                 :             : 
    3709                 :        2438 :         default:
    3710         [ +  - ]:        4876 :           n = printf ("%4d%c%s",
    3711         [ +  - ]:        2438 :                       *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
    3712                 :             :                       (vername != NULL
    3713         [ +  - ]:        2438 :                        && (unsigned int) (*sym & 0x7fff) < nvername)
    3714                 :        2438 :                       ? vername[*sym & 0x7fff] : "???");
    3715         [ +  - ]:        2438 :           if ((unsigned int) (*sym & 0x7fff) < nvername
    3716   [ +  -  +  + ]:        2438 :               && filename != NULL && filename[*sym & 0x7fff] != NULL)
    3717                 :        2134 :             n += printf ("(%s)", filename[*sym & 0x7fff]);
    3718                 :        2438 :           printf ("%*s", MAX (0, 33 - (int) n), " ");
    3719                 :        2438 :           break;
    3720                 :             :         }
    3721                 :             :     }
    3722                 :         130 :   putchar ('\n');
    3723                 :             : }
    3724                 :             : 
    3725                 :             : 
    3726                 :             : static void
    3727                 :         130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
    3728                 :             :                  uint_fast32_t maxlength, Elf32_Word nbucket,
    3729                 :             :                  uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
    3730                 :             : {
    3731                 :         130 :   uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
    3732                 :             : 
    3733         [ +  + ]:         550 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3734                 :         420 :     ++counts[lengths[cnt]];
    3735                 :             : 
    3736                 :         130 :   GElf_Shdr glink_mem;
    3737                 :         130 :   GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
    3738                 :         130 :                                                shdr->sh_link),
    3739                 :             :                                    &glink_mem);
    3740         [ -  + ]:         130 :   if (glink == NULL)
    3741                 :             :     {
    3742                 :           0 :       error (0, 0, _("invalid sh_link value in section %zu"),
    3743                 :             :              elf_ndxscn (scn));
    3744                 :           0 :       free (counts);
    3745                 :           0 :       return;
    3746                 :             :     }
    3747                 :             : 
    3748         [ +  + ]:         260 :   printf (ngettext ("\
    3749                 :             : \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",
    3750                 :             :                     "\
    3751                 :             : \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",
    3752                 :             :                     nbucket),
    3753                 :         130 :           (unsigned int) elf_ndxscn (scn),
    3754                 :         130 :           elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    3755                 :             :           (int) nbucket,
    3756                 :         130 :           gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
    3757                 :             :           shdr->sh_addr,
    3758                 :             :           shdr->sh_offset,
    3759                 :         130 :           (unsigned int) shdr->sh_link,
    3760                 :         130 :           elf_strptr (ebl->elf, shstrndx, glink->sh_name));
    3761                 :             : 
    3762         [ +  - ]:         130 :   if (extrastr != NULL)
    3763                 :         130 :     fputs (extrastr, stdout);
    3764                 :             : 
    3765         [ +  - ]:         130 :   if (likely (nbucket > 0))
    3766                 :             :     {
    3767                 :         130 :       uint64_t success = 0;
    3768                 :             : 
    3769                 :             :       /* xgettext:no-c-format */
    3770                 :         130 :       fputs (_("\
    3771                 :             :  Length  Number  % of total  Coverage\n"), stdout);
    3772                 :         130 :       printf (_("      0  %6" PRIu32 "      %5.1f%%\n"),
    3773                 :         130 :               counts[0], (counts[0] * 100.0) / nbucket);
    3774                 :             : 
    3775                 :         130 :       uint64_t nzero_counts = 0;
    3776         [ +  + ]:         228 :       for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
    3777                 :             :         {
    3778                 :          98 :           nzero_counts += counts[cnt] * cnt;
    3779                 :          98 :           printf (_("\
    3780                 :             : %7d  %6" PRIu32 "      %5.1f%%    %5.1f%%\n"),
    3781                 :          98 :                   (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
    3782                 :          98 :                   (nzero_counts * 100.0) / nsyms);
    3783                 :             :         }
    3784                 :             : 
    3785                 :             :       Elf32_Word acc = 0;
    3786         [ +  + ]:         228 :       for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
    3787                 :             :         {
    3788                 :          98 :           acc += cnt;
    3789                 :          98 :           success += counts[cnt] * acc;
    3790                 :             :         }
    3791                 :             : 
    3792         [ +  + ]:         130 :       if (nzero_counts > 0)
    3793                 :          62 :         printf (_("\
    3794                 :             :  Average number of tests:   successful lookup: %f\n\
    3795                 :             :                           unsuccessful lookup: %f\n"),
    3796                 :          62 :                 (double) success / (double) nzero_counts,
    3797                 :          62 :                 (double) nzero_counts / (double) nbucket);
    3798                 :             :     }
    3799                 :             : 
    3800                 :         130 :   free (counts);
    3801                 :             : }
    3802                 :             : 
    3803                 :             : 
    3804                 :             : /* This function handles the traditional System V-style hash table format.  */
    3805                 :             : static void
    3806                 :           0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3807                 :             : {
    3808                 :           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    3809         [ #  # ]:           0 :   if (unlikely (data == NULL))
    3810                 :             :     {
    3811                 :           0 :       error (0, 0, _("cannot get data for section %d: %s"),
    3812                 :           0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3813                 :           0 :       return;
    3814                 :             :     }
    3815                 :             : 
    3816         [ #  # ]:           0 :   if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
    3817                 :             :     {
    3818                 :           0 :     invalid_data:
    3819                 :           0 :       error (0, 0, _("invalid data in sysv.hash section %d"),
    3820                 :           0 :              (int) elf_ndxscn (scn));
    3821                 :           0 :       return;
    3822                 :             :     }
    3823                 :             : 
    3824                 :           0 :   Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
    3825                 :           0 :   Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
    3826                 :             : 
    3827                 :           0 :   uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
    3828         [ #  # ]:           0 :   if (used_buf > data->d_size)
    3829                 :           0 :     goto invalid_data;
    3830                 :             : 
    3831                 :           0 :   Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
    3832                 :           0 :   Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
    3833                 :             : 
    3834                 :           0 :   uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
    3835                 :             : 
    3836                 :           0 :   uint_fast32_t maxlength = 0;
    3837                 :           0 :   uint_fast32_t nsyms = 0;
    3838         [ #  # ]:           0 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3839                 :             :     {
    3840                 :           0 :       Elf32_Word inner = bucket[cnt];
    3841                 :           0 :       Elf32_Word chain_len = 0;
    3842         [ #  # ]:           0 :       while (inner > 0 && inner < nchain)
    3843                 :             :         {
    3844                 :           0 :           ++nsyms;
    3845                 :           0 :           ++chain_len;
    3846         [ #  # ]:           0 :           if (chain_len > nchain)
    3847                 :             :             {
    3848                 :           0 :               error (0, 0, _("invalid chain in sysv.hash section %d"),
    3849                 :           0 :                      (int) elf_ndxscn (scn));
    3850                 :           0 :               free (lengths);
    3851                 :           0 :               return;
    3852                 :             :             }
    3853         [ #  # ]:           0 :           if (maxlength < ++lengths[cnt])
    3854                 :           0 :             ++maxlength;
    3855                 :             : 
    3856                 :           0 :           inner = chain[inner];
    3857                 :             :         }
    3858                 :             :     }
    3859                 :             : 
    3860                 :           0 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    3861                 :             :                    lengths, NULL);
    3862                 :             : 
    3863                 :           0 :   free (lengths);
    3864                 :             : }
    3865                 :             : 
    3866                 :             : 
    3867                 :             : /* This function handles the incorrect, System V-style hash table
    3868                 :             :    format some 64-bit architectures use.  */
    3869                 :             : static void
    3870                 :           0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3871                 :             : {
    3872                 :           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    3873         [ #  # ]:           0 :   if (unlikely (data == NULL))
    3874                 :             :     {
    3875                 :           0 :       error (0, 0, _("cannot get data for section %d: %s"),
    3876                 :           0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3877                 :           0 :       return;
    3878                 :             :     }
    3879                 :             : 
    3880         [ #  # ]:           0 :   if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
    3881                 :             :     {
    3882                 :           0 :     invalid_data:
    3883                 :           0 :       error (0, 0, _("invalid data in sysv.hash64 section %d"),
    3884                 :           0 :              (int) elf_ndxscn (scn));
    3885                 :           0 :       return;
    3886                 :             :     }
    3887                 :             : 
    3888                 :           0 :   Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
    3889                 :           0 :   Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
    3890                 :             : 
    3891                 :           0 :   uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
    3892                 :           0 :   if (maxwords < 2
    3893         [ #  # ]:           0 :       || maxwords - 2 < nbucket
    3894         [ #  # ]:           0 :       || maxwords - 2 - nbucket < nchain)
    3895                 :           0 :     goto invalid_data;
    3896                 :             : 
    3897                 :           0 :   Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
    3898                 :           0 :   Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
    3899                 :             : 
    3900                 :           0 :   uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
    3901                 :             : 
    3902                 :           0 :   uint_fast32_t maxlength = 0;
    3903                 :           0 :   uint_fast32_t nsyms = 0;
    3904         [ #  # ]:           0 :   for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
    3905                 :             :     {
    3906                 :           0 :       Elf64_Xword inner = bucket[cnt];
    3907                 :           0 :       Elf64_Xword chain_len = 0;
    3908         [ #  # ]:           0 :       while (inner > 0 && inner < nchain)
    3909                 :             :         {
    3910                 :           0 :           ++nsyms;
    3911                 :           0 :           ++chain_len;
    3912         [ #  # ]:           0 :           if (chain_len > nchain)
    3913                 :             :             {
    3914                 :           0 :               error (0, 0, _("invalid chain in sysv.hash64 section %d"),
    3915                 :           0 :                      (int) elf_ndxscn (scn));
    3916                 :           0 :               free (lengths);
    3917                 :           0 :               return;
    3918                 :             :             }
    3919         [ #  # ]:           0 :           if (maxlength < ++lengths[cnt])
    3920                 :           0 :             ++maxlength;
    3921                 :             : 
    3922                 :           0 :           inner = chain[inner];
    3923                 :             :         }
    3924                 :             :     }
    3925                 :             : 
    3926                 :           0 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    3927                 :             :                    lengths, NULL);
    3928                 :             : 
    3929                 :           0 :   free (lengths);
    3930                 :             : }
    3931                 :             : 
    3932                 :             : 
    3933                 :             : /* This function handles the GNU-style hash table format.  */
    3934                 :             : static void
    3935                 :         130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
    3936                 :             : {
    3937                 :         130 :   uint32_t *lengths = NULL;
    3938                 :         130 :   Elf_Data *data = elf_getdata (scn, NULL);
    3939         [ -  + ]:         130 :   if (unlikely (data == NULL))
    3940                 :             :     {
    3941                 :           0 :       error (0, 0, _("cannot get data for section %d: %s"),
    3942                 :           0 :              (int) elf_ndxscn (scn), elf_errmsg (-1));
    3943                 :           0 :       return;
    3944                 :             :     }
    3945                 :             : 
    3946         [ -  + ]:         130 :   if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
    3947                 :             :     {
    3948                 :           0 :     invalid_data:
    3949                 :           0 :       free (lengths);
    3950                 :           0 :       error (0, 0, _("invalid data in gnu.hash section %d"),
    3951                 :           0 :              (int) elf_ndxscn (scn));
    3952                 :           0 :       return;
    3953                 :             :     }
    3954                 :             : 
    3955                 :         130 :   Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
    3956                 :         130 :   Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
    3957                 :             : 
    3958                 :             :   /* Next comes the size of the bitmap.  It's measured in words for
    3959                 :             :      the architecture.  It's 32 bits for 32 bit archs, and 64 bits for
    3960                 :             :      64 bit archs.  There is always a bloom filter present, so zero is
    3961                 :             :      an invalid value.  */
    3962                 :         130 :   Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
    3963         [ +  + ]:         130 :   if (gelf_getclass (ebl->elf) == ELFCLASS64)
    3964                 :         114 :     bitmask_words *= 2;
    3965                 :             : 
    3966         [ -  + ]:         130 :   if (bitmask_words == 0)
    3967                 :           0 :     goto invalid_data;
    3968                 :             : 
    3969                 :         130 :   Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
    3970                 :             : 
    3971                 :             :   /* Is there still room for the sym chain?
    3972                 :             :      Use uint64_t calculation to prevent 32bit overflow.  */
    3973                 :         130 :   uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
    3974                 :         130 :   uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
    3975         [ -  + ]:         130 :   if (used_buf > data->d_size)
    3976                 :           0 :     goto invalid_data;
    3977                 :             : 
    3978                 :         130 :   lengths = xcalloc (nbucket, sizeof (uint32_t));
    3979                 :             : 
    3980                 :         130 :   Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
    3981                 :         130 :   Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
    3982                 :         130 :   Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
    3983                 :         130 :                                                     + nbucket];
    3984                 :             : 
    3985                 :             :   /* Compute distribution of chain lengths.  */
    3986                 :         130 :   uint_fast32_t maxlength = 0;
    3987                 :         130 :   uint_fast32_t nsyms = 0;
    3988         [ +  + ]:         550 :   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
    3989         [ +  + ]:         420 :     if (bucket[cnt] != 0)
    3990                 :             :       {
    3991                 :         232 :         Elf32_Word inner = bucket[cnt] - symbias;
    3992                 :         414 :         do
    3993                 :             :           {
    3994                 :         414 :             ++nsyms;
    3995         [ +  + ]:         414 :             if (maxlength < ++lengths[cnt])
    3996                 :          98 :               ++maxlength;
    3997         [ -  + ]:         414 :             if (inner >= max_nsyms)
    3998                 :           0 :               goto invalid_data;
    3999                 :             :           }
    4000         [ +  + ]:         414 :         while ((chain[inner++] & 1) == 0);
    4001                 :             :       }
    4002                 :             : 
    4003                 :             :   /* Count bits in bitmask.  */
    4004                 :             :   uint_fast32_t nbits = 0;
    4005         [ +  + ]:         446 :   for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
    4006                 :             :     {
    4007                 :         316 :       uint_fast32_t word = bitmask[cnt];
    4008                 :             : 
    4009                 :         316 :       word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
    4010                 :         316 :       word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
    4011                 :         316 :       word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
    4012                 :         316 :       word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
    4013                 :         316 :       nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
    4014                 :             :     }
    4015                 :             : 
    4016                 :         260 :   char *str = xasprintf (_("\
    4017                 :             :  Symbol Bias: %u\n\
    4018                 :             :  Bitmask Size: %zu bytes  %" PRIuFAST32 "%% bits set  2nd hash shift: %u\n"),
    4019                 :             :                          (unsigned int) symbias,
    4020                 :             :                          bitmask_words * sizeof (Elf32_Word),
    4021                 :         130 :                          ((nbits * 100 + 50)
    4022                 :         130 :                           / (uint_fast32_t) (bitmask_words
    4023                 :             :                                               * sizeof (Elf32_Word) * 8)),
    4024                 :             :                           (unsigned int) shift);
    4025                 :             : 
    4026                 :         130 :   print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
    4027                 :             :                    lengths, str);
    4028                 :             : 
    4029                 :         130 :   free (str);
    4030                 :         130 :   free (lengths);
    4031                 :             : }
    4032                 :             : 
    4033                 :             : 
    4034                 :             : /* Find the symbol table(s).  For this we have to search through the
    4035                 :             :    section table.  */
    4036                 :             : static void
    4037                 :         184 : handle_hash (Ebl *ebl)
    4038                 :             : {
    4039                 :             :   /* Get the section header string table index.  */
    4040                 :         184 :   size_t shstrndx;
    4041         [ -  + ]:         184 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    4042                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    4043                 :             : 
    4044                 :             :   Elf_Scn *scn = NULL;
    4045         [ +  + ]:        6004 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    4046                 :             :     {
    4047                 :             :       /* Handle the section if it is a symbol table.  */
    4048                 :        5820 :       GElf_Shdr shdr_mem;
    4049                 :        5820 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    4050                 :             : 
    4051         [ +  - ]:        5820 :       if (likely (shdr != NULL))
    4052                 :             :         {
    4053         [ +  + ]:        5820 :           if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
    4054         [ -  + ]:         130 :               && (shdr->sh_flags & SHF_COMPRESSED) != 0)
    4055                 :             :             {
    4056         [ #  # ]:           0 :               if (elf_compress (scn, 0, 0) < 0)
    4057                 :           0 :                 printf ("WARNING: %s [%zd]\n",
    4058                 :             :                         _("Couldn't uncompress section"),
    4059                 :             :                         elf_ndxscn (scn));
    4060                 :           0 :               shdr = gelf_getshdr (scn, &shdr_mem);
    4061         [ #  # ]:           0 :               if (unlikely (shdr == NULL))
    4062                 :           0 :                 error_exit (0, _("cannot get section [%zd] header: %s"),
    4063                 :             :                             elf_ndxscn (scn), elf_errmsg (-1));
    4064                 :             :             }
    4065                 :             : 
    4066         [ -  + ]:        5820 :           if (shdr->sh_type == SHT_HASH)
    4067                 :             :             {
    4068         [ #  # ]:           0 :               if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
    4069                 :           0 :                 handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
    4070                 :             :               else
    4071                 :           0 :                 handle_sysv_hash (ebl, scn, shdr, shstrndx);
    4072                 :             :             }
    4073         [ +  + ]:        5820 :           else if (shdr->sh_type == SHT_GNU_HASH)
    4074                 :         130 :             handle_gnu_hash (ebl, scn, shdr, shstrndx);
    4075                 :             :         }
    4076                 :             :     }
    4077                 :         184 : }
    4078                 :             : 
    4079                 :             : 
    4080                 :             : static void
    4081                 :         192 : print_liblist (Ebl *ebl)
    4082                 :             : {
    4083                 :             :   /* Find the library list sections.  For this we have to search
    4084                 :             :      through the section table.  */
    4085                 :         192 :   Elf_Scn *scn = NULL;
    4086                 :             : 
    4087                 :             :   /* Get the section header string table index.  */
    4088                 :         192 :   size_t shstrndx;
    4089         [ -  + ]:         192 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    4090                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    4091                 :             : 
    4092         [ +  + ]:        6130 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    4093                 :             :     {
    4094                 :        5938 :       GElf_Shdr shdr_mem;
    4095                 :        5938 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    4096                 :             : 
    4097   [ +  -  -  + ]:        5938 :       if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
    4098                 :             :         {
    4099                 :           0 :           size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
    4100                 :           0 :           int nentries = shdr->sh_size / sh_entsize;
    4101                 :           0 :           printf (ngettext ("\
    4102                 :             : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
    4103                 :             :                             "\
    4104                 :             : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
    4105                 :             :                             nentries),
    4106                 :             :                   elf_ndxscn (scn),
    4107                 :           0 :                   elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    4108                 :             :                   shdr->sh_offset,
    4109                 :             :                   nentries);
    4110                 :             : 
    4111                 :           0 :           Elf_Data *data = elf_getdata (scn, NULL);
    4112         [ #  # ]:           0 :           if (data == NULL)
    4113                 :           0 :             return;
    4114                 :             : 
    4115                 :           0 :           puts (_("\
    4116                 :             :        Library                       Time Stamp          Checksum Version Flags"));
    4117                 :             : 
    4118         [ #  # ]:           0 :           for (int cnt = 0; cnt < nentries; ++cnt)
    4119                 :             :             {
    4120                 :           0 :               GElf_Lib lib_mem;
    4121                 :           0 :               GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
    4122         [ #  # ]:           0 :               if (unlikely (lib == NULL))
    4123                 :           0 :                 continue;
    4124                 :             : 
    4125                 :           0 :               time_t t = (time_t) lib->l_time_stamp;
    4126                 :           0 :               struct tm *tm = gmtime (&t);
    4127         [ #  # ]:           0 :               if (unlikely (tm == NULL))
    4128                 :           0 :                 continue;
    4129                 :             : 
    4130                 :           0 :               printf ("  [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
    4131                 :           0 :                       cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
    4132                 :           0 :                       tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
    4133                 :             :                       tm->tm_hour, tm->tm_min, tm->tm_sec,
    4134                 :           0 :                       (unsigned int) lib->l_checksum,
    4135                 :           0 :                       (unsigned int) lib->l_version,
    4136                 :           0 :                       (unsigned int) lib->l_flags);
    4137                 :             :             }
    4138                 :             :         }
    4139                 :             :     }
    4140                 :             : }
    4141                 :             : 
    4142                 :             : static inline size_t
    4143                 :          16 : left (Elf_Data *data,
    4144                 :             :       const unsigned char *p)
    4145                 :             : {
    4146                 :          16 :   return (const unsigned char *) data->d_buf + data->d_size - p;
    4147                 :             : }
    4148                 :             : 
    4149                 :             : static void
    4150                 :         192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
    4151                 :             : {
    4152                 :             :   /* Find the object attributes sections.  For this we have to search
    4153                 :             :      through the section table.  */
    4154                 :         192 :   Elf_Scn *scn = NULL;
    4155                 :             : 
    4156                 :             :   /* Get the section header string table index.  */
    4157                 :         192 :   size_t shstrndx;
    4158         [ -  + ]:         192 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
    4159                 :           0 :     error_exit (0, _("cannot get section header string table index"));
    4160                 :             : 
    4161         [ +  + ]:        6130 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
    4162                 :             :     {
    4163                 :        5938 :       GElf_Shdr shdr_mem;
    4164                 :        5938 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    4165                 :             : 
    4166   [ +  -  +  + ]:        5938 :       if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
    4167         [ +  + ]:        5932 :                            && (shdr->sh_type != SHT_ARM_ATTRIBUTES
    4168         [ -  + ]:           2 :                                || ehdr->e_machine != EM_ARM)
    4169         [ +  + ]:        5930 :                            && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
    4170         [ +  - ]:           2 :                                || ehdr->e_machine != EM_CSKY)
    4171         [ -  + ]:        5930 :                            && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
    4172         [ #  # ]:           0 :                                || ehdr->e_machine != EM_RISCV)))
    4173                 :        5930 :         continue;
    4174                 :             : 
    4175                 :           8 :       printf (_("\
    4176                 :             : \nObject attributes section [%2zu] '%s' of %" PRIu64
    4177                 :             :                        " bytes at offset %#0" PRIx64 ":\n"),
    4178                 :             :               elf_ndxscn (scn),
    4179                 :           8 :               elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
    4180                 :             :               shdr->sh_size, shdr->sh_offset);
    4181                 :             : 
    4182                 :           8 :       Elf_Data *data = elf_rawdata (scn, NULL);
    4183   [ +  -  +  - ]:           8 :       if (unlikely (data == NULL || data->d_size == 0))
    4184                 :           0 :         return;
    4185                 :             : 
    4186                 :           8 :       const unsigned char *p = data->d_buf;
    4187                 :             : 
    4188                 :             :       /* There is only one 'version', A.  */
    4189         [ +  - ]:           8 :       if (unlikely (*p++ != 'A'))
    4190                 :             :         return;
    4191                 :             : 
    4192                 :           8 :       fputs (_("  Owner          Size\n"), stdout);
    4193                 :             : 
    4194                 :             :       /* Loop over the sections.  */
    4195         [ +  + ]:          16 :       while (left (data, p) >= 4)
    4196                 :             :         {
    4197                 :             :           /* Section length.  */
    4198                 :           8 :           uint32_t len;
    4199         [ +  + ]:           8 :           memcpy (&len, p, sizeof len);
    4200                 :             : 
    4201         [ +  + ]:           8 :           if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
    4202                 :           4 :             CONVERT (len);
    4203                 :             : 
    4204         [ +  - ]:           8 :           if (unlikely (len > left (data, p)))
    4205                 :             :             break;
    4206                 :             : 
    4207                 :             :           /* Section vendor name.  */
    4208                 :           8 :           const unsigned char *name = p + sizeof len;
    4209                 :           8 :           p += len;
    4210                 :             : 
    4211                 :           8 :           unsigned const char *q = memchr (name, '\0', len);
    4212         [ +  - ]:           8 :           if (unlikely (q == NULL))
    4213                 :             :             break;
    4214                 :           8 :           ++q;
    4215                 :             : 
    4216                 :           8 :           printf (_("  %-13s  %4" PRIu32 "\n"), name, len);
    4217                 :             : 
    4218                 :          16 :           bool gnu_vendor = (q - name == sizeof "gnu"
    4219   [ +  +  -  + ]:           8 :                              && !memcmp (name, "gnu", sizeof "gnu"));
    4220                 :             : 
    4221                 :             :           /* Loop over subsections.  */
    4222         [ +  + ]:           8 :           if (shdr->sh_type != SHT_GNU_ATTRIBUTES
    4223         [ +  - ]:           6 :               || gnu_vendor)
    4224         [ +  + ]:          16 :             while (q < p)
    4225                 :             :               {
    4226                 :           8 :                 const unsigned char *const sub = q;
    4227                 :             : 
    4228                 :           8 :                 unsigned int subsection_tag;
    4229                 :           8 :                 get_uleb128 (subsection_tag, q, p);
    4230         [ +  - ]:           8 :                 if (unlikely (q >= p))
    4231                 :             :                   break;
    4232                 :             : 
    4233                 :           8 :                 uint32_t subsection_len;
    4234         [ +  - ]:           8 :                 if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
    4235                 :             :                   break;
    4236                 :             : 
    4237         [ +  + ]:           8 :                 memcpy (&subsection_len, q, sizeof subsection_len);
    4238                 :             : 
    4239         [ +  + ]:           8 :                 if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
    4240                 :           4 :                   CONVERT (subsection_len);
    4241                 :             : 
    4242                 :             :                 /* Don't overflow, ptrdiff_t might be 32bits, but signed.  */
    4243   [ +  -  +  - ]:           8 :                 if (unlikely (subsection_len == 0
    4244                 :             :                               || subsection_len >= (uint32_t) PTRDIFF_MAX
    4245                 :             :                               || p - sub < (ptrdiff_t) subsection_len))
    4246                 :             :                   break;
    4247                 :             : 
    4248                 :           8 :                 const unsigned char *r = q + sizeof subsection_len;
    4249                 :           8 :                 q = sub + subsection_len;
    4250                 :             : 
    4251         [ -  + ]:           8 :                 switch (subsection_tag)
    4252                 :             :                   {
    4253                 :           0 :                   default:
    4254                 :             :                     /* Unknown subsection, print and skip.  */
    4255                 :           0 :                     printf (_("    %-4u %12" PRIu32 "\n"),
    4256                 :             :                             subsection_tag, subsection_len);
    4257                 :           0 :                     break;
    4258                 :             : 
    4259                 :           8 :                   case 1:       /* Tag_File */
    4260                 :           8 :                     printf (_("    File: %11" PRIu32 "\n"),
    4261                 :             :                             subsection_len);
    4262                 :             : 
    4263         [ +  + ]:          52 :                     while (r < q)
    4264                 :             :                       {
    4265                 :          44 :                         unsigned int tag;
    4266                 :          44 :                         get_uleb128 (tag, r, q);
    4267         [ +  - ]:          44 :                         if (unlikely (r >= q))
    4268                 :             :                           break;
    4269                 :             : 
    4270                 :             :                         /* GNU style tags have either a uleb128 value,
    4271                 :             :                            when lowest bit is not set, or a string
    4272                 :             :                            when the lowest bit is set.
    4273                 :             :                            "compatibility" (32) is special.  It has
    4274                 :             :                            both a string and a uleb128 value.  For
    4275                 :             :                            non-gnu we assume 6 till 31 only take ints.
    4276                 :             :                            XXX see arm backend, do we need a separate
    4277                 :             :                            hook?  */
    4278                 :          44 :                         uint64_t value = 0;
    4279                 :          44 :                         const char *string = NULL;
    4280   [ +  -  +  + ]:          44 :                         if (tag == 32 || (tag & 1) == 0
    4281   [ +  -  +  + ]:          16 :                             || (! gnu_vendor && (tag > 5 && tag < 32)))
    4282                 :             :                           {
    4283                 :             :                             // Note r >= q check above.
    4284                 :          42 :                             get_uleb128 (value, r, q);
    4285         [ +  - ]:          42 :                             if (r > q)
    4286                 :             :                               break;
    4287                 :             :                           }
    4288         [ +  - ]:          44 :                         if (tag == 32
    4289         [ +  + ]:          44 :                             || ((tag & 1) != 0
    4290         [ +  - ]:          16 :                                 && (gnu_vendor
    4291         [ +  - ]:          16 :                                     || (! gnu_vendor && tag > 32)))
    4292   [ +  +  +  + ]:          44 :                             || (! gnu_vendor && tag > 3 && tag < 6))
    4293                 :             :                           {
    4294                 :           2 :                             string = (const char *) r;
    4295                 :           2 :                             r = memchr (r, '\0', q - r);
    4296         [ +  - ]:           2 :                             if (r == NULL)
    4297                 :             :                               break;
    4298                 :           2 :                             ++r;
    4299                 :             :                           }
    4300                 :             : 
    4301                 :          44 :                         const char *tag_name = NULL;
    4302                 :          44 :                         const char *value_name = NULL;
    4303                 :          44 :                         ebl_check_object_attribute (ebl, (const char *) name,
    4304                 :             :                                                     tag, value,
    4305                 :             :                                                     &tag_name, &value_name);
    4306                 :             : 
    4307         [ +  - ]:          44 :                         if (tag_name != NULL)
    4308                 :             :                           {
    4309         [ -  + ]:          44 :                             if (tag == 32)
    4310                 :           0 :                               printf (_("      %s: %" PRId64 ", %s\n"),
    4311                 :             :                                       tag_name, value, string);
    4312   [ +  +  +  + ]:          44 :                             else if (string == NULL && value_name == NULL)
    4313                 :           2 :                               printf (_("      %s: %" PRId64 "\n"),
    4314                 :             :                                       tag_name, value);
    4315                 :             :                             else
    4316                 :          42 :                               printf (_("      %s: %s\n"),
    4317                 :             :                                       tag_name, string ?: value_name);
    4318                 :             :                           }
    4319                 :             :                         else
    4320                 :             :                           {
    4321                 :             :                             /* For "gnu" vendor 32 "compatibility" has
    4322                 :             :                                already been handled above.  */
    4323   [ #  #  #  # ]:           0 :                             assert (tag != 32
    4324                 :             :                                     || strcmp ((const char *) name, "gnu"));
    4325         [ #  # ]:           0 :                             if (string == NULL)
    4326                 :           0 :                               printf (_("      %u: %" PRId64 "\n"),
    4327                 :             :                                       tag, value);
    4328                 :             :                             else
    4329                 :           0 :                               printf (_("      %u: %s\n"),
    4330                 :             :                                       tag, string);
    4331                 :             :                           }
    4332                 :             :                       }
    4333                 :             :                   }
    4334                 :             :               }
    4335                 :             :         }
    4336                 :             :     }
    4337                 :             : }
    4338                 :             : 
    4339                 :             : /* Returns either the (relocated) data from the Dwarf, or tries to get
    4340                 :             :    the "raw" (uncompressed) data from the Elf section. Produces a
    4341                 :             :    warning if the data cannot be found (or decompressed).  */
    4342                 :             : static Elf_Data *
    4343                 :         810 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
    4344                 :             : {
    4345                 :             :   /* We prefer to get the section data from the Dwarf because that
    4346                 :             :      might have been relocated already.  Note this is subtly wrong if
    4347                 :             :      there are multiple sections with the same .debug name.  */
    4348         [ -  + ]:         810 :   if (dbg->sectiondata[idx] != NULL)
    4349                 :             :     return dbg->sectiondata[idx];
    4350                 :             : 
    4351                 :           0 :   GElf_Shdr shdr_mem;
    4352                 :           0 :   GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    4353   [ #  #  #  # ]:           0 :   if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
    4354                 :             :     {
    4355         [ #  # ]:           0 :       if (elf_compress (scn, 0, 0) < 0)
    4356                 :             :         {
    4357                 :           0 :           error (0, 0, "%s [%zd] '%s'\n",
    4358                 :             :                  _("Couldn't uncompress section"),
    4359                 :             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    4360                 :           0 :           return NULL;
    4361                 :             :         }
    4362                 :             :     }
    4363                 :             : 
    4364                 :           0 :   Elf_Data *data = elf_getdata (scn, NULL);
    4365         [ #  # ]:           0 :   if (data == NULL)
    4366                 :           0 :     error (0, 0, "%s [%zd] '%s': %s\n",
    4367                 :             :            _("Couldn't get data from section"),
    4368                 :             :            elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
    4369                 :             : 
    4370                 :           0 :   return elf_getdata (scn, NULL);
    4371                 :             : }
    4372                 :             : 
    4373                 :             : static void
    4374                 :     2254950 : print_dwarf_addr (Dwfl_Module *dwflmod,
    4375                 :             :                   int address_size, Dwarf_Addr address, Dwarf_Addr raw,
    4376                 :             :                   FILE *out)
    4377                 :             : {
    4378                 :             :   /* See if there is a name we can give for this address.  */
    4379                 :     2254950 :   GElf_Sym sym;
    4380                 :     2254950 :   GElf_Off off = 0;
    4381         [ +  + ]:       14162 :   const char *name = (print_address_names && ! print_unresolved_addresses)
    4382                 :       13998 :     ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
    4383         [ +  + ]:     2254950 :     : NULL;
    4384                 :             : 
    4385                 :     2254950 :   const char *scn;
    4386         [ +  + ]:     2254950 :   if (print_unresolved_addresses)
    4387                 :             :     {
    4388                 :         168 :       address = raw;
    4389                 :         168 :       scn = NULL;
    4390                 :             :     }
    4391                 :             :   else
    4392                 :             :     {
    4393                 :             :       /* Relativize the address.  */
    4394                 :     2254782 :       int n = dwfl_module_relocations (dwflmod);
    4395         [ +  + ]:     2254782 :       int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
    4396                 :             : 
    4397                 :             :       /* In an ET_REL file there is a section name to refer to.  */
    4398                 :     2252030 :       scn = (i < 0 ? NULL
    4399         [ -  + ]:     2252030 :              : dwfl_module_relocation_info (dwflmod, i, NULL));
    4400                 :             :     }
    4401                 :             : 
    4402         [ -  + ]:     2254950 :   if ((name != NULL
    4403                 :       13590 :        ? (off != 0
    4404                 :             :           ? (scn != NULL
    4405                 :             :              ? (address_size == 0
    4406                 :           2 :                 ? fprintf (out, "%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
    4407                 :             :                            scn, address, name, off)
    4408                 :           8 :                 : fprintf (out, "%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
    4409                 :           4 :                            scn, 2 + address_size * 2, address,
    4410                 :             :                            name, off))
    4411                 :             :              : (address_size == 0
    4412                 :         364 :                 ? fprintf (out, "%#" PRIx64 " <%s+%#" PRIx64 ">",
    4413                 :             :                            address, name, off)
    4414                 :        2156 :                 : fprintf (out, "%#0*" PRIx64 " <%s+%#" PRIx64 ">",
    4415                 :        1078 :                            2 + address_size * 2, address,
    4416                 :             :                            name, off)))
    4417                 :             :           : (scn != NULL
    4418                 :             :              ? (address_size == 0
    4419                 :           2 :                 ? fprintf (out, "%s+%#" PRIx64 " <%s>", scn, address, name)
    4420                 :       22280 :                 : fprintf (out, "%s+%#0*" PRIx64 " <%s>",
    4421                 :       11140 :                            scn, 2 + address_size * 2, address, name))
    4422                 :             :              : (address_size == 0
    4423                 :         160 :                 ? fprintf (out, "%#" PRIx64 " <%s>", address, name)
    4424                 :        1680 :                 : fprintf (out, "%#0*" PRIx64 " <%s>",
    4425                 :         840 :                            2 + address_size * 2, address, name))))
    4426                 :             :        : (scn != NULL
    4427                 :             :           ? (address_size == 0
    4428                 :      822688 :              ? fprintf (out, "%s+%#" PRIx64, scn, address)
    4429                 :     2836388 :              : fprintf (out, "%s+%#0*" PRIx64,
    4430                 :     1418194 :                         scn, 2 + address_size * 2, address))
    4431                 :             :           : (address_size == 0
    4432                 :          70 :              ? fprintf (out, "%#" PRIx64, address)
    4433                 :         816 :              : fprintf (out, "%#0*" PRIx64,
    4434   [ +  +  +  +  :     4509492 :                         2 + address_size * 2, address)))) < 0)
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
                +  +  + ]
    4435                 :           0 :     error_exit (0, _("sprintf failure"));
    4436                 :     2254950 : }
    4437                 :             : 
    4438                 :             : 
    4439                 :             : static const char *
    4440                 :     2290212 : dwarf_tag_string (unsigned int tag)
    4441                 :             : {
    4442   [ -  +  +  -  :     2290212 :   switch (tag)
          -  -  -  -  +  
          +  +  +  +  -  
          -  -  -  -  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  -  -  -  
          -  -  -  +  -  
          +  -  +  +  +  
          -  -  -  -  -  
          -  +  -  -  +  
          -  -  -  +  -  
          +  +  +  +  -  
          -  -  -  -  +  
          +  +  +  -  +  
          +  +  +  -  -  
                      - ]
    4443                 :             :     {
    4444                 :             : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
    4445                 :     2290212 :       DWARF_ALL_KNOWN_DW_TAG
    4446                 :             : #undef DWARF_ONE_KNOWN_DW_TAG
    4447                 :           0 :     default:
    4448                 :           0 :       return NULL;
    4449                 :             :     }
    4450                 :             : }
    4451                 :             : 
    4452                 :             : 
    4453                 :             : static const char *
    4454                 :     8886730 : dwarf_attr_string (unsigned int attrnum)
    4455                 :             : {
    4456   [ +  -  +  -  :     8886730 :   switch (attrnum)
          -  -  -  +  -  
          -  +  +  +  -  
          -  -  -  +  +  
          -  +  -  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  +  
          -  -  -  +  +  
          -  -  -  +  -  
          +  -  +  +  -  
          -  +  +  +  +  
          -  +  +  +  -  
          +  -  -  +  -  
          +  -  +  +  -  
          +  -  -  +  +  
          +  +  -  -  -  
          -  -  +  +  +  
          +  -  +  -  +  
          -  -  +  -  +  
          +  -  +  -  +  
          +  -  +  +  +  
          +  +  +  +  -  
          -  +  -  -  -  
          +  -  +  -  -  
          -  -  +  +  -  
          +  -  -  -  -  
          +  -  -  -  +  
          +  -  +  -  -  
          -  -  +  +  -  
          -  -  -  -  +  
          +  -  -  -  -  
             -  -  -  + ]
    4457                 :             :     {
    4458                 :             : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
    4459                 :     8886728 :       DWARF_ALL_KNOWN_DW_AT
    4460                 :             : #undef DWARF_ONE_KNOWN_DW_AT
    4461                 :           0 :     default:
    4462                 :           0 :       return NULL;
    4463                 :             :     }
    4464                 :             : }
    4465                 :             : 
    4466                 :             : 
    4467                 :             : static const char *
    4468                 :     8897148 : dwarf_form_string (unsigned int form)
    4469                 :             : {
    4470   [ +  +  +  +  :     8897148 :   switch (form)
          +  -  -  -  -  
          -  +  -  -  +  
          +  +  +  +  +  
          +  +  +  -  +  
          +  -  -  +  -  
          -  +  -  -  +  
          +  +  +  +  +  
          -  +  +  -  -  
             -  +  -  + ]
    4471                 :             :     {
    4472                 :             : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
    4473                 :     8897144 :       DWARF_ALL_KNOWN_DW_FORM
    4474                 :             : #undef DWARF_ONE_KNOWN_DW_FORM
    4475                 :           0 :     default:
    4476                 :           0 :       return NULL;
    4477                 :             :     }
    4478                 :             : }
    4479                 :             : 
    4480                 :             : 
    4481                 :             : static const char *
    4482                 :        3740 : dwarf_lang_string (unsigned int lang)
    4483                 :             : {
    4484   [ -  -  +  -  :        3740 :   switch (lang)
          -  -  -  +  -  
          -  +  +  -  +  
          -  -  +  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
                      - ]
    4485                 :             :     {
    4486                 :             : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
    4487                 :        3738 :       DWARF_ALL_KNOWN_DW_LANG
    4488                 :             : #undef DWARF_ONE_KNOWN_DW_LANG
    4489                 :           2 :     default:
    4490                 :           2 :       return NULL;
    4491                 :             :     }
    4492                 :             : }
    4493                 :             : 
    4494                 :             : 
    4495                 :             : static const char *
    4496                 :           4 : dwarf_lname_string (unsigned int lname)
    4497                 :             : {
    4498   [ -  -  -  +  :           4 :   switch (lname)
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
    4499                 :             :     {
    4500                 :             : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
    4501                 :           4 :       DWARF_ALL_KNOWN_DW_LNAME
    4502                 :             : #undef DWARF_ONE_KNOWN_DW_LNAME
    4503                 :           0 :     default:
    4504                 :           0 :       return NULL;
    4505                 :             :     }
    4506                 :             : }
    4507                 :             : 
    4508                 :             : 
    4509                 :             : static const char *
    4510                 :        7270 : dwarf_inline_string (unsigned int code)
    4511                 :             : {
    4512                 :        7270 :   static const char *const known[] =
    4513                 :             :     {
    4514                 :             : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
    4515                 :             :       DWARF_ALL_KNOWN_DW_INL
    4516                 :             : #undef DWARF_ONE_KNOWN_DW_INL
    4517                 :             :     };
    4518                 :             : 
    4519                 :        7270 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4520                 :        7270 :     return known[code];
    4521                 :             : 
    4522                 :             :   return NULL;
    4523                 :             : }
    4524                 :             : 
    4525                 :             : 
    4526                 :             : static const char *
    4527                 :       59402 : dwarf_encoding_string (unsigned int code)
    4528                 :             : {
    4529                 :       59402 :   static const char *const known[] =
    4530                 :             :     {
    4531                 :             : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
    4532                 :             :       DWARF_ALL_KNOWN_DW_ATE
    4533                 :             : #undef DWARF_ONE_KNOWN_DW_ATE
    4534                 :             :     };
    4535                 :             : 
    4536                 :       59402 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4537                 :       59402 :     return known[code];
    4538                 :             : 
    4539                 :             :   return NULL;
    4540                 :             : }
    4541                 :             : 
    4542                 :             : 
    4543                 :             : static const char *
    4544                 :           0 : dwarf_access_string (unsigned int code)
    4545                 :             : {
    4546                 :           0 :   static const char *const known[] =
    4547                 :             :     {
    4548                 :             : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
    4549                 :             :       DWARF_ALL_KNOWN_DW_ACCESS
    4550                 :             : #undef DWARF_ONE_KNOWN_DW_ACCESS
    4551                 :             :     };
    4552                 :             : 
    4553                 :           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4554                 :           0 :     return known[code];
    4555                 :             : 
    4556                 :             :   return NULL;
    4557                 :             : }
    4558                 :             : 
    4559                 :             : 
    4560                 :             : static const char *
    4561                 :           0 : dwarf_defaulted_string (unsigned int code)
    4562                 :             : {
    4563                 :           0 :   static const char *const known[] =
    4564                 :             :     {
    4565                 :             : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
    4566                 :             :       DWARF_ALL_KNOWN_DW_DEFAULTED
    4567                 :             : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
    4568                 :             :     };
    4569                 :             : 
    4570                 :           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4571                 :           0 :     return known[code];
    4572                 :             : 
    4573                 :             :   return NULL;
    4574                 :             : }
    4575                 :             : 
    4576                 :             : 
    4577                 :             : static const char *
    4578                 :           0 : dwarf_visibility_string (unsigned int code)
    4579                 :             : {
    4580                 :           0 :   static const char *const known[] =
    4581                 :             :     {
    4582                 :             : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
    4583                 :             :       DWARF_ALL_KNOWN_DW_VIS
    4584                 :             : #undef DWARF_ONE_KNOWN_DW_VIS
    4585                 :             :     };
    4586                 :             : 
    4587                 :           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4588                 :           0 :     return known[code];
    4589                 :             : 
    4590                 :             :   return NULL;
    4591                 :             : }
    4592                 :             : 
    4593                 :             : 
    4594                 :             : static const char *
    4595                 :           0 : dwarf_virtuality_string (unsigned int code)
    4596                 :             : {
    4597                 :           0 :   static const char *const known[] =
    4598                 :             :     {
    4599                 :             : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
    4600                 :             :       DWARF_ALL_KNOWN_DW_VIRTUALITY
    4601                 :             : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
    4602                 :             :     };
    4603                 :             : 
    4604                 :           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4605                 :           0 :     return known[code];
    4606                 :             : 
    4607                 :             :   return NULL;
    4608                 :             : }
    4609                 :             : 
    4610                 :             : 
    4611                 :             : static const char *
    4612                 :           0 : dwarf_identifier_case_string (unsigned int code)
    4613                 :             : {
    4614                 :           0 :   static const char *const known[] =
    4615                 :             :     {
    4616                 :             : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
    4617                 :             :       DWARF_ALL_KNOWN_DW_ID
    4618                 :             : #undef DWARF_ONE_KNOWN_DW_ID
    4619                 :             :     };
    4620                 :             : 
    4621                 :           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4622                 :           0 :     return known[code];
    4623                 :             : 
    4624                 :             :   return NULL;
    4625                 :             : }
    4626                 :             : 
    4627                 :             : 
    4628                 :             : static const char *
    4629                 :           0 : dwarf_calling_convention_string (unsigned int code)
    4630                 :             : {
    4631                 :           0 :   static const char *const known[] =
    4632                 :             :     {
    4633                 :             : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
    4634                 :             :       DWARF_ALL_KNOWN_DW_CC
    4635                 :             : #undef DWARF_ONE_KNOWN_DW_CC
    4636                 :             :     };
    4637                 :             : 
    4638                 :           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4639                 :           0 :     return known[code];
    4640                 :             : 
    4641                 :             :   return NULL;
    4642                 :             : }
    4643                 :             : 
    4644                 :             : 
    4645                 :             : static const char *
    4646                 :           0 : dwarf_ordering_string (unsigned int code)
    4647                 :             : {
    4648                 :           0 :   static const char *const known[] =
    4649                 :             :     {
    4650                 :             : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
    4651                 :             :       DWARF_ALL_KNOWN_DW_ORD
    4652                 :             : #undef DWARF_ONE_KNOWN_DW_ORD
    4653                 :             :     };
    4654                 :             : 
    4655                 :           0 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4656                 :           0 :     return known[code];
    4657                 :             : 
    4658                 :             :   return NULL;
    4659                 :             : }
    4660                 :             : 
    4661                 :             : 
    4662                 :             : static const char *
    4663                 :          16 : dwarf_discr_list_string (unsigned int code)
    4664                 :             : {
    4665                 :          16 :   static const char *const known[] =
    4666                 :             :     {
    4667                 :             : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
    4668                 :             :       DWARF_ALL_KNOWN_DW_DSC
    4669                 :             : #undef DWARF_ONE_KNOWN_DW_DSC
    4670                 :             :     };
    4671                 :             : 
    4672                 :          16 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4673                 :          16 :     return known[code];
    4674                 :             : 
    4675                 :             :   return NULL;
    4676                 :             : }
    4677                 :             : 
    4678                 :             : 
    4679                 :             : static const char *
    4680                 :     1116746 : dwarf_locexpr_opcode_string (unsigned int code)
    4681                 :             : {
    4682                 :     1116746 :   static const char *const known[] =
    4683                 :             :     {
    4684                 :             :       /* Normally we can't afford building huge table of 64K entries,
    4685                 :             :          most of them zero, just because there are a couple defined
    4686                 :             :          values at the far end.  In case of opcodes, it's OK.  */
    4687                 :             : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
    4688                 :             :       DWARF_ALL_KNOWN_DW_OP
    4689                 :             : #undef DWARF_ONE_KNOWN_DW_OP
    4690                 :             :     };
    4691                 :             : 
    4692                 :     1116746 :   if (likely (code < sizeof (known) / sizeof (known[0])))
    4693                 :     1116746 :     return known[code];
    4694                 :             : 
    4695                 :             :   return NULL;
    4696                 :             : }
    4697                 :             : 
    4698                 :             : 
    4699                 :             : static const char *
    4700                 :        3486 : dwarf_unit_string (unsigned int type)
    4701                 :             : {
    4702   [ -  +  -  -  :        3486 :   switch (type)
                -  -  + ]
    4703                 :             :     {
    4704                 :             : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
    4705                 :          14 :       DWARF_ALL_KNOWN_DW_UT
    4706                 :             : #undef DWARF_ONE_KNOWN_DW_UT
    4707                 :           0 :     default:
    4708                 :           0 :       return NULL;
    4709                 :             :     }
    4710                 :             : }
    4711                 :             : 
    4712                 :             : 
    4713                 :             : static const char *
    4714                 :      148614 : dwarf_range_list_encoding_string (unsigned int kind)
    4715                 :             : {
    4716   [ -  +  +  -  :      148614 :   switch (kind)
             +  -  -  -  
                      + ]
    4717                 :             :     {
    4718                 :             : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
    4719                 :      131630 :       DWARF_ALL_KNOWN_DW_RLE
    4720                 :             : #undef DWARF_ONE_KNOWN_DW_RLE
    4721                 :           0 :     default:
    4722                 :           0 :       return NULL;
    4723                 :             :     }
    4724                 :             : }
    4725                 :             : 
    4726                 :             : 
    4727                 :             : static const char *
    4728                 :      654136 : dwarf_loc_list_encoding_string (unsigned int kind)
    4729                 :             : {
    4730   [ -  -  +  +  :      654136 :   switch (kind)
          -  +  -  +  -  
                   -  + ]
    4731                 :             :     {
    4732                 :             : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
    4733                 :      608582 :       DWARF_ALL_KNOWN_DW_LLE
    4734                 :             : #undef DWARF_ONE_KNOWN_DW_LLE
    4735                 :             :     /* DW_LLE_GNU_view_pair is special/incompatible with default codes.  */
    4736                 :           0 :     case DW_LLE_GNU_view_pair: return "GNU_view_pair";
    4737                 :           0 :     default:
    4738                 :           0 :       return NULL;
    4739                 :             :     }
    4740                 :             : }
    4741                 :             : 
    4742                 :             : 
    4743                 :             : static const char *
    4744                 :       10418 : dwarf_line_content_description_string (unsigned int kind)
    4745                 :             : {
    4746   [ +  +  -  -  :       10418 :   switch (kind)
                   -  + ]
    4747                 :             :     {
    4748                 :             : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
    4749                 :       10410 :       DWARF_ALL_KNOWN_DW_LNCT
    4750                 :             : #undef DWARF_ONE_KNOWN_DW_LNCT
    4751                 :           0 :     default:
    4752                 :           0 :       return NULL;
    4753                 :             :     }
    4754                 :             : }
    4755                 :             : 
    4756                 :             : 
    4757                 :             : /* Used by all dwarf_foo_name functions.  */
    4758                 :             : static const char *
    4759                 :    20961176 : string_or_unknown (const char *known, unsigned int code,
    4760                 :             :                    unsigned int lo_user, unsigned int hi_user,
    4761                 :             :                    bool print_unknown_num)
    4762                 :             : {
    4763                 :    20961176 :   static char unknown_buf[20];
    4764                 :             : 
    4765         [ +  + ]:    20961176 :   if (likely (known != NULL))
    4766                 :             :     return known;
    4767                 :             : 
    4768   [ -  +  -  - ]:           2 :   if (lo_user != 0 && code >= lo_user && code <= hi_user)
    4769                 :             :     {
    4770                 :           0 :       snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
    4771                 :             :                 code - lo_user);
    4772                 :           0 :       return unknown_buf;
    4773                 :             :     }
    4774                 :             : 
    4775         [ +  - ]:           2 :   if (print_unknown_num)
    4776                 :             :     {
    4777                 :           2 :       snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
    4778                 :           2 :       return unknown_buf;
    4779                 :             :     }
    4780                 :             : 
    4781                 :             :   return "???";
    4782                 :             : }
    4783                 :             : 
    4784                 :             : 
    4785                 :             : static const char *
    4786                 :     2290212 : dwarf_tag_name (unsigned int tag)
    4787                 :             : {
    4788                 :     2290212 :   const char *ret = dwarf_tag_string (tag);
    4789                 :     2290212 :   return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
    4790                 :             : }
    4791                 :             : 
    4792                 :             : static const char *
    4793                 :     8886730 : dwarf_attr_name (unsigned int attr)
    4794                 :             : {
    4795                 :     8886730 :   const char *ret = dwarf_attr_string (attr);
    4796                 :     8886730 :   return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
    4797                 :             : }
    4798                 :             : 
    4799                 :             : 
    4800                 :             : static const char *
    4801                 :     8897148 : dwarf_form_name (unsigned int form)
    4802                 :             : {
    4803                 :     8897148 :   const char *ret = dwarf_form_string (form);
    4804                 :     8897148 :   return string_or_unknown (ret, form, 0, 0, true);
    4805                 :             : }
    4806                 :             : 
    4807                 :             : 
    4808                 :             : static const char *
    4809                 :        3736 : dwarf_lang_name (unsigned int lang)
    4810                 :             : {
    4811                 :        3736 :   const char *ret = dwarf_lang_string (lang);
    4812                 :        3736 :   return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
    4813                 :             : }
    4814                 :             : 
    4815                 :             : 
    4816                 :             : static const char *
    4817                 :           4 : dwarf_lname_name (unsigned int lname)
    4818                 :             : {
    4819                 :           4 :   const char *ret = dwarf_lname_string (lname);
    4820                 :           4 :   return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
    4821                 :             :                             false);
    4822                 :             : }
    4823                 :             : 
    4824                 :             : 
    4825                 :             : static const char *
    4826                 :        7270 : dwarf_inline_name (unsigned int code)
    4827                 :             : {
    4828         [ +  - ]:        7270 :   const char *ret = dwarf_inline_string (code);
    4829                 :        7270 :   return string_or_unknown (ret, code, 0, 0, false);
    4830                 :             : }
    4831                 :             : 
    4832                 :             : 
    4833                 :             : static const char *
    4834                 :       59402 : dwarf_encoding_name (unsigned int code)
    4835                 :             : {
    4836         [ +  - ]:       59402 :   const char *ret = dwarf_encoding_string (code);
    4837                 :       59402 :   return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
    4838                 :             : }
    4839                 :             : 
    4840                 :             : 
    4841                 :             : static const char *
    4842                 :           0 : dwarf_access_name (unsigned int code)
    4843                 :             : {
    4844         [ #  # ]:           0 :   const char *ret = dwarf_access_string (code);
    4845                 :           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4846                 :             : }
    4847                 :             : 
    4848                 :             : 
    4849                 :             : static const char *
    4850                 :           0 : dwarf_defaulted_name (unsigned int code)
    4851                 :             : {
    4852         [ #  # ]:           0 :   const char *ret = dwarf_defaulted_string (code);
    4853                 :           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4854                 :             : }
    4855                 :             : 
    4856                 :             : 
    4857                 :             : static const char *
    4858                 :           0 : dwarf_visibility_name (unsigned int code)
    4859                 :             : {
    4860         [ #  # ]:           0 :   const char *ret = dwarf_visibility_string (code);
    4861                 :           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4862                 :             : }
    4863                 :             : 
    4864                 :             : 
    4865                 :             : static const char *
    4866                 :           0 : dwarf_virtuality_name (unsigned int code)
    4867                 :             : {
    4868         [ #  # ]:           0 :   const char *ret = dwarf_virtuality_string (code);
    4869                 :           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4870                 :             : }
    4871                 :             : 
    4872                 :             : 
    4873                 :             : static const char *
    4874                 :           0 : dwarf_identifier_case_name (unsigned int code)
    4875                 :             : {
    4876         [ #  # ]:           0 :   const char *ret = dwarf_identifier_case_string (code);
    4877                 :           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4878                 :             : }
    4879                 :             : 
    4880                 :             : 
    4881                 :             : static const char *
    4882                 :           0 : dwarf_calling_convention_name (unsigned int code)
    4883                 :             : {
    4884         [ #  # ]:           0 :   const char *ret = dwarf_calling_convention_string (code);
    4885                 :           0 :   return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
    4886                 :             : }
    4887                 :             : 
    4888                 :             : 
    4889                 :             : static const char *
    4890                 :           0 : dwarf_ordering_name (unsigned int code)
    4891                 :             : {
    4892         [ #  # ]:           0 :   const char *ret = dwarf_ordering_string (code);
    4893                 :           0 :   return string_or_unknown (ret, code, 0, 0, false);
    4894                 :             : }
    4895                 :             : 
    4896                 :             : 
    4897                 :             : static const char *
    4898                 :          16 : dwarf_discr_list_name (unsigned int code)
    4899                 :             : {
    4900         [ +  - ]:          16 :   const char *ret = dwarf_discr_list_string (code);
    4901                 :          16 :   return string_or_unknown (ret, code, 0, 0, false);
    4902                 :             : }
    4903                 :             : 
    4904                 :             : 
    4905                 :             : static const char *
    4906                 :        3486 : dwarf_unit_name (unsigned int type)
    4907                 :             : {
    4908                 :        3486 :   const char *ret = dwarf_unit_string (type);
    4909                 :        3486 :   return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
    4910                 :             : }
    4911                 :             : 
    4912                 :             : 
    4913                 :             : static const char *
    4914                 :      148614 : dwarf_range_list_encoding_name (unsigned int kind)
    4915                 :             : {
    4916                 :      148614 :   const char *ret = dwarf_range_list_encoding_string (kind);
    4917                 :      148614 :   return string_or_unknown (ret, kind, 0, 0, false);
    4918                 :             : }
    4919                 :             : 
    4920                 :             : 
    4921                 :             : static const char *
    4922                 :      654136 : dwarf_loc_list_encoding_name (unsigned int kind)
    4923                 :             : {
    4924                 :      654136 :   const char *ret = dwarf_loc_list_encoding_string (kind);
    4925                 :      654136 :   return string_or_unknown (ret, kind, 0, 0, false);
    4926                 :             : }
    4927                 :             : 
    4928                 :             : 
    4929                 :             : static const char *
    4930                 :       10418 : dwarf_line_content_description_name (unsigned int kind)
    4931                 :             : {
    4932                 :       10418 :   const char *ret = dwarf_line_content_description_string (kind);
    4933                 :       10418 :   return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
    4934                 :             :                             false);
    4935                 :             : }
    4936                 :             : 
    4937                 :             : 
    4938                 :             : static void
    4939                 :         510 : print_block (size_t n, const void *block, FILE *out)
    4940                 :             : {
    4941         [ -  + ]:         510 :   if (n == 0)
    4942                 :           0 :     fputs (_("empty block\n"), out);
    4943                 :             :   else
    4944                 :             :     {
    4945                 :         510 :       fprintf (out, _("%zu byte block:"), n);
    4946                 :         510 :       const unsigned char *data = block;
    4947                 :        2250 :       do
    4948                 :        2250 :         fprintf (out, " %02x", *data++);
    4949         [ +  + ]:        2250 :       while (--n > 0);
    4950                 :         510 :       fputc ('\n', out);
    4951                 :             :     }
    4952                 :         510 : }
    4953                 :             : 
    4954                 :             : static void
    4955                 :          24 : print_bytes (size_t n, const unsigned char *bytes, FILE *out)
    4956                 :             : {
    4957         [ +  + ]:         408 :   while (n-- > 0)
    4958                 :             :     {
    4959                 :         384 :       fprintf (out, "%02x", *bytes++);
    4960         [ +  + ]:         384 :       if (n > 0)
    4961                 :         360 :         fprintf (out, " ");
    4962                 :             :     }
    4963                 :          24 : }
    4964                 :             : 
    4965                 :             : static int
    4966                 :         148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
    4967                 :             : {
    4968         [ -  + ]:         148 :   if (cu == NULL)
    4969                 :             :     return -1;
    4970                 :             : 
    4971                 :         148 :   Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
    4972         [ -  + ]:         148 :   if (debug_addr == NULL)
    4973                 :             :     return -1;
    4974                 :             : 
    4975                 :         148 :   Dwarf_Off base = __libdw_cu_addr_base (cu);
    4976                 :         148 :   Dwarf_Word off = idx * cu->address_size;
    4977         [ -  + ]:         148 :   if (base > debug_addr->d_size
    4978         [ -  + ]:         148 :       || off > debug_addr->d_size - base
    4979         [ -  + ]:         148 :       || cu->address_size > debug_addr->d_size - base - off)
    4980                 :             :     return -1;
    4981                 :             : 
    4982                 :         148 :   const unsigned char *addrp = debug_addr->d_buf + base + off;
    4983         [ +  + ]:         148 :   if (cu->address_size == 4)
    4984         [ -  + ]:          12 :     *addr = read_4ubyte_unaligned (cu->dbg, addrp);
    4985                 :             :   else
    4986         [ -  + ]:         136 :     *addr = read_8ubyte_unaligned (cu->dbg, addrp);
    4987                 :             : 
    4988                 :             :   return 0;
    4989                 :             : }
    4990                 :             : 
    4991                 :             : static void
    4992                 :      785094 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
    4993                 :             :            unsigned int vers, unsigned int addrsize, unsigned int offset_size,
    4994                 :             :            struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data,
    4995                 :             :            FILE *out)
    4996                 :             : {
    4997         [ +  + ]:      785094 :   const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
    4998                 :             : 
    4999         [ -  + ]:      785094 :   if (len == 0)
    5000                 :             :     {
    5001                 :           0 :       fprintf (out, "%*s(empty)\n", indent, "");
    5002                 :           0 :       return;
    5003                 :             :     }
    5004                 :             : 
    5005                 :             : #define NEED(n)         if (len < (Dwarf_Word) (n)) goto invalid
    5006                 :             : #define CONSUME(n)      NEED (n); else len -= (n)
    5007                 :             : 
    5008                 :             :   Dwarf_Word offset = 0;
    5009         [ +  + ]:     1901840 :   while (len-- > 0)
    5010                 :     1116746 :     {
    5011                 :     1116746 :       uint_fast8_t op = *data++;
    5012                 :             : 
    5013         [ +  - ]:     1116746 :       const char *op_name = dwarf_locexpr_opcode_string (op);
    5014         [ -  + ]:     1116746 :       if (unlikely (op_name == NULL))
    5015                 :             :         {
    5016                 :           0 :           static char buf[20];
    5017         [ #  # ]:           0 :           if (op >= DW_OP_lo_user)
    5018                 :           0 :             snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
    5019                 :             :           else
    5020                 :           0 :             snprintf (buf, sizeof buf, "??? (%#x)", op);
    5021                 :             :           op_name = buf;
    5022                 :             :         }
    5023                 :             : 
    5024   [ +  -  +  +  :     1116746 :       switch (op)
          +  +  +  +  -  
          -  +  +  -  +  
          -  -  +  +  +  
          +  +  -  -  -  
             -  +  +  + ]
    5025                 :             :         {
    5026                 :       26970 :         case DW_OP_addr:;
    5027                 :             :           /* Address operand.  */
    5028                 :       26970 :           Dwarf_Word addr;
    5029         [ -  + ]:       26970 :           NEED (addrsize);
    5030         [ +  + ]:       26970 :           if (addrsize == 4)
    5031         [ +  + ]:         108 :             addr = read_4ubyte_unaligned (dbg, data);
    5032         [ +  - ]:       26862 :           else if (addrsize == 8)
    5033         [ +  + ]:       26862 :             addr = read_8ubyte_unaligned (dbg, data);
    5034                 :             :           else
    5035                 :           0 :             goto invalid;
    5036                 :       26970 :           data += addrsize;
    5037                 :       26970 :           CONSUME (addrsize);
    5038                 :             : 
    5039                 :       26970 :           fprintf (out, "%*s[%2" PRIuMAX "] %s ",
    5040                 :             :                    indent, "", (uintmax_t) offset, op_name);
    5041                 :       26970 :           print_dwarf_addr (dwflmod, 0, addr, addr, out);
    5042                 :       26970 :           fprintf (out, "\n");
    5043                 :             : 
    5044                 :       26970 :           offset += 1 + addrsize;
    5045                 :       26970 :           break;
    5046                 :             : 
    5047                 :           0 :         case DW_OP_call_ref:
    5048                 :             :         case DW_OP_GNU_variable_value:
    5049                 :             :           /* Offset operand.  */
    5050         [ #  # ]:           0 :           if (ref_size != 4 && ref_size != 8)
    5051                 :           0 :             goto invalid; /* Cannot be used in CFA.  */
    5052         [ #  # ]:           0 :           NEED (ref_size);
    5053         [ #  # ]:           0 :           if (ref_size == 4)
    5054         [ #  # ]:           0 :             addr = read_4ubyte_unaligned (dbg, data);
    5055                 :             :           else
    5056         [ #  # ]:           0 :             addr = read_8ubyte_unaligned (dbg, data);
    5057                 :           0 :           data += ref_size;
    5058                 :           0 :           CONSUME (ref_size);
    5059                 :             :           /* addr is a DIE offset, so format it as one.  */
    5060                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    5061                 :             :                    indent, "", (uintmax_t) offset,
    5062                 :             :                    op_name, (uintmax_t) addr);
    5063                 :           0 :           offset += 1 + ref_size;
    5064                 :           0 :           break;
    5065                 :             : 
    5066                 :       24764 :         case DW_OP_deref_size:
    5067                 :             :         case DW_OP_xderef_size:
    5068                 :             :         case DW_OP_pick:
    5069                 :             :         case DW_OP_const1u:
    5070                 :             :           // XXX value might be modified by relocation
    5071         [ -  + ]:       24764 :           NEED (1);
    5072                 :       49528 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
    5073                 :             :                    indent, "", (uintmax_t) offset,
    5074                 :       24764 :                    op_name, *((uint8_t *) data));
    5075                 :       24764 :           ++data;
    5076                 :       24764 :           --len;
    5077                 :       24764 :           offset += 2;
    5078                 :       24764 :           break;
    5079                 :             : 
    5080                 :        3318 :         case DW_OP_const2u:
    5081         [ -  + ]:        3318 :           NEED (2);
    5082                 :             :           // XXX value might be modified by relocation
    5083                 :        6636 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
    5084                 :             :                    indent, "", (uintmax_t) offset,
    5085         [ -  + ]:        3318 :                    op_name, read_2ubyte_unaligned (dbg, data));
    5086                 :        3318 :           CONSUME (2);
    5087                 :        3318 :           data += 2;
    5088                 :        3318 :           offset += 3;
    5089                 :        3318 :           break;
    5090                 :             : 
    5091                 :        2906 :         case DW_OP_const4u:
    5092         [ -  + ]:        2906 :           NEED (4);
    5093                 :             :           // XXX value might be modified by relocation
    5094                 :        2906 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
    5095                 :             :                    indent, "", (uintmax_t) offset,
    5096         [ -  + ]:        2906 :                    op_name, read_4ubyte_unaligned (dbg, data));
    5097                 :        2906 :           CONSUME (4);
    5098                 :        2906 :           data += 4;
    5099                 :        2906 :           offset += 5;
    5100                 :        2906 :           break;
    5101                 :             : 
    5102                 :         126 :         case DW_OP_const8u:
    5103         [ -  + ]:         126 :           NEED (8);
    5104                 :             :           // XXX value might be modified by relocation
    5105                 :         126 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
    5106                 :             :                    indent, "", (uintmax_t) offset,
    5107         [ -  + ]:         126 :                    op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
    5108                 :         126 :           CONSUME (8);
    5109                 :         126 :           data += 8;
    5110                 :         126 :           offset += 9;
    5111                 :         126 :           break;
    5112                 :             : 
    5113                 :        5162 :         case DW_OP_const1s:
    5114         [ -  + ]:        5162 :           NEED (1);
    5115                 :             :           // XXX value might be modified by relocation
    5116                 :       10324 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
    5117                 :             :                    indent, "", (uintmax_t) offset,
    5118                 :        5162 :                    op_name, *((int8_t *) data));
    5119                 :        5162 :           ++data;
    5120                 :        5162 :           --len;
    5121                 :        5162 :           offset += 2;
    5122                 :        5162 :           break;
    5123                 :             : 
    5124                 :          10 :         case DW_OP_const2s:
    5125         [ -  + ]:          10 :           NEED (2);
    5126                 :             :           // XXX value might be modified by relocation
    5127                 :          20 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
    5128                 :             :                    indent, "", (uintmax_t) offset,
    5129         [ -  + ]:          10 :                    op_name, read_2sbyte_unaligned (dbg, data));
    5130                 :          10 :           CONSUME (2);
    5131                 :          10 :           data += 2;
    5132                 :          10 :           offset += 3;
    5133                 :          10 :           break;
    5134                 :             : 
    5135                 :           0 :         case DW_OP_const4s:
    5136         [ #  # ]:           0 :           NEED (4);
    5137                 :             :           // XXX value might be modified by relocation
    5138                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
    5139                 :             :                    indent, "", (uintmax_t) offset,
    5140         [ #  # ]:           0 :                    op_name, read_4sbyte_unaligned (dbg, data));
    5141                 :           0 :           CONSUME (4);
    5142                 :           0 :           data += 4;
    5143                 :           0 :           offset += 5;
    5144                 :           0 :           break;
    5145                 :             : 
    5146                 :           0 :         case DW_OP_const8s:
    5147         [ #  # ]:           0 :           NEED (8);
    5148                 :             :           // XXX value might be modified by relocation
    5149                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
    5150                 :             :                    indent, "", (uintmax_t) offset,
    5151         [ #  # ]:           0 :                    op_name, read_8sbyte_unaligned (dbg, data));
    5152                 :           0 :           CONSUME (8);
    5153                 :           0 :           data += 8;
    5154                 :           0 :           offset += 9;
    5155                 :           0 :           break;
    5156                 :             : 
    5157                 :       25586 :         case DW_OP_piece:
    5158                 :             :         case DW_OP_regx:
    5159                 :             :         case DW_OP_plus_uconst:
    5160                 :       25586 :         case DW_OP_constu:;
    5161                 :       25586 :           const unsigned char *start = data;
    5162                 :       25586 :           uint64_t uleb;
    5163         [ -  + ]:       25586 :           NEED (1);
    5164                 :       25586 :           get_uleb128 (uleb, data, data + len);
    5165                 :       25586 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
    5166                 :             :                    indent, "", (uintmax_t) offset, op_name, uleb);
    5167         [ -  + ]:       25586 :           CONSUME (data - start);
    5168                 :       25586 :           offset += 1 + (data - start);
    5169                 :       25586 :           break;
    5170                 :             : 
    5171                 :          16 :         case DW_OP_addrx:
    5172                 :             :         case DW_OP_GNU_addr_index:
    5173                 :             :         case DW_OP_constx:
    5174                 :          16 :         case DW_OP_GNU_const_index:;
    5175                 :          16 :           start = data;
    5176         [ -  + ]:          16 :           NEED (1);
    5177                 :          16 :           get_uleb128 (uleb, data, data + len);
    5178                 :          16 :           fprintf (out, "%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
    5179                 :             :                    indent, "", (uintmax_t) offset, op_name, uleb);
    5180         [ -  + ]:          16 :           CONSUME (data - start);
    5181                 :          16 :           offset += 1 + (data - start);
    5182         [ -  + ]:          16 :           if (get_indexed_addr (cu, uleb, &addr) != 0)
    5183                 :           0 :             fprintf (out, "???\n");
    5184                 :             :           else
    5185                 :             :             {
    5186                 :          16 :               print_dwarf_addr (dwflmod, 0, addr, addr, out);
    5187                 :          16 :               fprintf (out, "\n");
    5188                 :             :             }
    5189                 :             :           break;
    5190                 :             : 
    5191                 :           0 :         case DW_OP_bit_piece:
    5192                 :           0 :           start = data;
    5193                 :           0 :           uint64_t uleb2;
    5194         [ #  # ]:           0 :           NEED (1);
    5195                 :           0 :           get_uleb128 (uleb, data, data + len);
    5196                 :           0 :           NEED (1);
    5197                 :           0 :           get_uleb128 (uleb2, data, data + len);
    5198                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
    5199                 :             :                    indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
    5200         [ #  # ]:           0 :           CONSUME (data - start);
    5201                 :           0 :           offset += 1 + (data - start);
    5202                 :           0 :           break;
    5203                 :             : 
    5204                 :      188662 :         case DW_OP_fbreg:
    5205                 :             :         case DW_OP_breg0 ... DW_OP_breg31:
    5206                 :             :         case DW_OP_consts:
    5207                 :      188662 :           start = data;
    5208                 :      188662 :           int64_t sleb;
    5209         [ -  + ]:      188662 :           NEED (1);
    5210                 :      188662 :           get_sleb128 (sleb, data, data + len);
    5211                 :      188662 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
    5212                 :             :                    indent, "", (uintmax_t) offset, op_name, sleb);
    5213         [ -  + ]:      188662 :           CONSUME (data - start);
    5214                 :      188662 :           offset += 1 + (data - start);
    5215                 :      188662 :           break;
    5216                 :             : 
    5217                 :           0 :         case DW_OP_bregx:
    5218                 :           0 :           start = data;
    5219         [ #  # ]:           0 :           NEED (1);
    5220                 :           0 :           get_uleb128 (uleb, data, data + len);
    5221                 :           0 :           NEED (1);
    5222                 :           0 :           get_sleb128 (sleb, data, data + len);
    5223                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
    5224                 :             :                    indent, "", (uintmax_t) offset, op_name, uleb, sleb);
    5225         [ #  # ]:           0 :           CONSUME (data - start);
    5226                 :           0 :           offset += 1 + (data - start);
    5227                 :           0 :           break;
    5228                 :             : 
    5229                 :           0 :         case DW_OP_call2:
    5230         [ #  # ]:           0 :           NEED (2);
    5231                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
    5232                 :             :                    indent, "", (uintmax_t) offset, op_name,
    5233         [ #  # ]:           0 :                    read_2ubyte_unaligned (dbg, data));
    5234                 :           0 :           CONSUME (2);
    5235                 :           0 :           data += 2;
    5236                 :           0 :           offset += 3;
    5237                 :           0 :           break;
    5238                 :             : 
    5239                 :           8 :         case DW_OP_call4:
    5240         [ -  + ]:           8 :           NEED (4);
    5241                 :           8 :           fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
    5242                 :             :                    indent, "", (uintmax_t) offset, op_name,
    5243         [ -  + ]:           8 :                    read_4ubyte_unaligned (dbg, data));
    5244                 :           8 :           CONSUME (4);
    5245                 :           8 :           data += 4;
    5246                 :           8 :           offset += 5;
    5247                 :           8 :           break;
    5248                 :             : 
    5249                 :        1120 :         case DW_OP_skip:
    5250                 :             :         case DW_OP_bra:
    5251         [ -  + ]:        1120 :           NEED (2);
    5252                 :        2240 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
    5253                 :             :                    indent, "", (uintmax_t) offset, op_name,
    5254         [ -  + ]:        1120 :                    (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
    5255                 :        1120 :           CONSUME (2);
    5256                 :        1120 :           data += 2;
    5257                 :        1120 :           offset += 3;
    5258                 :        1120 :           break;
    5259                 :             : 
    5260                 :         510 :         case DW_OP_implicit_value:
    5261                 :         510 :           start = data;
    5262         [ -  + ]:         510 :           NEED (1);
    5263                 :         510 :           get_uleb128 (uleb, data, data + len);
    5264                 :         510 :           fprintf (out, "%*s[%2" PRIuMAX "] %s: ",
    5265                 :             :                    indent, "", (uintmax_t) offset, op_name);
    5266         [ -  + ]:         510 :           NEED (uleb);
    5267                 :         510 :           print_block (uleb, data, out);
    5268                 :         510 :           data += uleb;
    5269         [ -  + ]:         510 :           CONSUME (data - start);
    5270                 :         510 :           offset += 1 + (data - start);
    5271                 :         510 :           break;
    5272                 :             : 
    5273                 :        6034 :         case DW_OP_implicit_pointer:
    5274                 :             :         case DW_OP_GNU_implicit_pointer:
    5275                 :             :           /* DIE offset operand.  */
    5276                 :        6034 :           start = data;
    5277         [ -  + ]:        6034 :           NEED (ref_size);
    5278         [ -  + ]:        6034 :           if (ref_size != 4 && ref_size != 8)
    5279                 :           0 :             goto invalid; /* Cannot be used in CFA.  */
    5280         [ +  - ]:        6034 :           if (ref_size == 4)
    5281         [ -  + ]:        6034 :             addr = read_4ubyte_unaligned (dbg, data);
    5282                 :             :           else
    5283         [ #  # ]:           0 :             addr = read_8ubyte_unaligned (dbg, data);
    5284                 :        6034 :           data += ref_size;
    5285                 :             :           /* Byte offset operand.  */
    5286         [ -  + ]:        6034 :           NEED (1);
    5287                 :        6034 :           get_sleb128 (sleb, data, data + len);
    5288                 :             : 
    5289                 :        6034 :           fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
    5290                 :             :                    indent, "", (intmax_t) offset,
    5291                 :             :                    op_name, (uintmax_t) addr, sleb);
    5292         [ -  + ]:        6034 :           CONSUME (data - start);
    5293                 :        6034 :           offset += 1 + (data - start);
    5294                 :        6034 :           break;
    5295                 :             : 
    5296                 :       62546 :         case DW_OP_entry_value:
    5297                 :             :         case DW_OP_GNU_entry_value:
    5298                 :             :           /* Size plus expression block.  */
    5299                 :       62546 :           start = data;
    5300         [ -  + ]:       62546 :           NEED (1);
    5301                 :       62546 :           get_uleb128 (uleb, data, data + len);
    5302                 :       62546 :           fprintf (out, "%*s[%2" PRIuMAX "] %s:\n",
    5303                 :             :                    indent, "", (uintmax_t) offset, op_name);
    5304         [ -  + ]:       62546 :           NEED (uleb);
    5305                 :       62546 :           print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
    5306                 :             :                      addrsize, offset_size, cu, uleb, data, out);
    5307                 :       62546 :           data += uleb;
    5308         [ -  + ]:       62546 :           CONSUME (data - start);
    5309                 :       62546 :           offset += 1 + (data - start);
    5310                 :       62546 :           break;
    5311                 :             : 
    5312                 :           0 :         case DW_OP_const_type:
    5313                 :             :         case DW_OP_GNU_const_type:
    5314                 :             :           /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
    5315                 :             :              unsigned size plus block.  */
    5316                 :           0 :           start = data;
    5317         [ #  # ]:           0 :           NEED (1);
    5318                 :           0 :           get_uleb128 (uleb, data, data + len);
    5319   [ #  #  #  # ]:           0 :           if (! print_unresolved_addresses && cu != NULL)
    5320                 :           0 :             uleb += cu->start;
    5321                 :           0 :           NEED (1);
    5322                 :           0 :           uint8_t usize = *(uint8_t *) data++;
    5323         [ #  # ]:           0 :           NEED (usize);
    5324                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
    5325                 :             :                    indent, "", (uintmax_t) offset, op_name, uleb);
    5326                 :           0 :           print_block (usize, data, out);
    5327                 :           0 :           data += usize;
    5328         [ #  # ]:           0 :           CONSUME (data - start);
    5329                 :           0 :           offset += 1 + (data - start);
    5330                 :           0 :           break;
    5331                 :             : 
    5332                 :           0 :         case DW_OP_regval_type:
    5333                 :             :         case DW_OP_GNU_regval_type:
    5334                 :             :           /* uleb128 register number, uleb128 CU relative
    5335                 :             :              DW_TAG_base_type DIE offset.  */
    5336                 :           0 :           start = data;
    5337         [ #  # ]:           0 :           NEED (1);
    5338                 :           0 :           get_uleb128 (uleb, data, data + len);
    5339                 :           0 :           NEED (1);
    5340                 :           0 :           get_uleb128 (uleb2, data, data + len);
    5341   [ #  #  #  # ]:           0 :           if (! print_unresolved_addresses && cu != NULL)
    5342                 :           0 :             uleb2 += cu->start;
    5343                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
    5344                 :             :                    indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
    5345         [ #  # ]:           0 :           CONSUME (data - start);
    5346                 :           0 :           offset += 1 + (data - start);
    5347                 :           0 :           break;
    5348                 :             : 
    5349                 :           0 :         case DW_OP_deref_type:
    5350                 :             :         case DW_OP_GNU_deref_type:
    5351                 :             :           /* 1-byte unsigned size of value, uleb128 CU relative
    5352                 :             :              DW_TAG_base_type DIE offset.  */
    5353                 :           0 :           start = data;
    5354         [ #  # ]:           0 :           NEED (1);
    5355                 :           0 :           usize = *(uint8_t *) data++;
    5356                 :           0 :           NEED (1);
    5357                 :           0 :           get_uleb128 (uleb, data, data + len);
    5358   [ #  #  #  # ]:           0 :           if (! print_unresolved_addresses && cu != NULL)
    5359                 :           0 :             uleb += cu->start;
    5360                 :           0 :           fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
    5361                 :             :                    indent, "", (uintmax_t) offset,
    5362                 :             :                    op_name, usize, uleb);
    5363         [ #  # ]:           0 :           CONSUME (data - start);
    5364                 :           0 :           offset += 1 + (data - start);
    5365                 :           0 :           break;
    5366                 :             : 
    5367                 :           0 :         case DW_OP_xderef_type:
    5368                 :             :           /* 1-byte unsigned size of value, uleb128 base_type DIE offset.  */
    5369                 :           0 :           start = data;
    5370         [ #  # ]:           0 :           NEED (1);
    5371                 :           0 :           usize = *(uint8_t *) data++;
    5372                 :           0 :           NEED (1);
    5373                 :           0 :           get_uleb128 (uleb, data, data + len);
    5374                 :           0 :           fprintf (out, "%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
    5375                 :             :                    indent, "", (uintmax_t) offset,
    5376                 :             :                    op_name, usize, uleb);
    5377         [ #  # ]:           0 :           CONSUME (data - start);
    5378                 :           0 :           offset += 1 + (data - start);
    5379                 :           0 :           break;
    5380                 :             : 
    5381                 :         786 :         case DW_OP_convert:
    5382                 :             :         case DW_OP_GNU_convert:
    5383                 :             :         case DW_OP_reinterpret:
    5384                 :             :         case DW_OP_GNU_reinterpret:
    5385                 :             :           /* uleb128 CU relative offset to DW_TAG_base_type, or zero
    5386                 :             :              for conversion to untyped.  */
    5387                 :         786 :           start = data;
    5388         [ -  + ]:         786 :           NEED (1);
    5389                 :         786 :           get_uleb128 (uleb, data, data + len);
    5390   [ +  +  +  -  :         786 :           if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
                   +  - ]
    5391                 :         524 :             uleb += cu->start;
    5392                 :         786 :           fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    5393                 :             :                    indent, "", (uintmax_t) offset, op_name, uleb);
    5394         [ -  + ]:         786 :           CONSUME (data - start);
    5395                 :         786 :           offset += 1 + (data - start);
    5396                 :         786 :           break;
    5397                 :             : 
    5398                 :           8 :         case DW_OP_GNU_parameter_ref:
    5399                 :             :           /* 4 byte CU relative reference to the abstract optimized away
    5400                 :             :              DW_TAG_formal_parameter.  */
    5401         [ -  + ]:           8 :           NEED (4);
    5402         [ -  + ]:           8 :           uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
    5403   [ +  -  +  - ]:           8 :           if (! print_unresolved_addresses && cu != NULL)
    5404                 :           8 :             param_off += cu->start;
    5405                 :           8 :           fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
    5406                 :             :                    indent, "", (uintmax_t) offset, op_name, param_off);
    5407                 :           8 :           CONSUME (4);
    5408                 :           8 :           data += 4;
    5409                 :           8 :           offset += 5;
    5410                 :           8 :           break;
    5411                 :             : 
    5412                 :             :         default:
    5413                 :             :           /* No Operand.  */
    5414                 :      768214 :           fprintf (out, "%*s[%2" PRIuMAX "] %s\n",
    5415                 :             :                    indent, "", (uintmax_t) offset, op_name);
    5416                 :      768214 :           ++offset;
    5417                 :      768214 :           break;
    5418                 :             :         }
    5419                 :             : 
    5420                 :     1116746 :       indent = indentrest;
    5421                 :     1116746 :       continue;
    5422                 :             : 
    5423                 :           0 :     invalid:
    5424                 :           0 :       fprintf (out, _("%*s[%2" PRIuMAX "] %s  <TRUNCATED>\n"),
    5425                 :             :                indent, "", (uintmax_t) offset, op_name);
    5426                 :           0 :       break;
    5427                 :             :     }
    5428                 :             : }
    5429                 :             : 
    5430                 :             : 
    5431                 :             : /* Turn the addresses into file offsets by using the phdrs.  */
    5432                 :             : static void
    5433                 :           2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
    5434                 :             :                   GElf_Addr addrs[n], GElf_Off offs[n])
    5435                 :             : {
    5436                 :           2 :   size_t unsolved = n;
    5437         [ +  + ]:          18 :   for (size_t i = 0; i < phnum; ++i) {
    5438                 :          16 :     GElf_Phdr phdr_mem;
    5439                 :          16 :     GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
    5440   [ +  -  +  +  :          16 :     if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
                   +  - ]
    5441         [ +  + ]:          46 :       for (size_t j = 0; j < n; ++j)
    5442   [ +  +  +  + ]:          44 :         if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
    5443         [ +  - ]:          22 :             addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
    5444                 :          22 :           offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
    5445         [ +  + ]:          22 :           if (--unsolved == 0)
    5446                 :             :             break;
    5447                 :             :         }
    5448                 :             :   }
    5449                 :           2 : }
    5450                 :             : 
    5451                 :             : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
    5452                 :             :    And .dynamic section contains an array of the dynamic structures.
    5453                 :             :    We use the array to get:
    5454                 :             :     DT_STRTAB: the address of the string table
    5455                 :             :     DT_SYMTAB: the address of the symbol table
    5456                 :             :     DT_STRSZ: the size, in bytes, of the string table
    5457                 :             :     ...  */
    5458                 :             : static void
    5459                 :           2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
    5460                 :             : {
    5461                 :           2 :   Elf_Data *data = elf_getdata_rawchunk(
    5462                 :           2 :     elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
    5463                 :             : 
    5464                 :           2 :   int dyn_idx = 0;
    5465                 :          44 :   for (;; ++dyn_idx) {
    5466                 :          46 :     GElf_Dyn dyn_mem;
    5467                 :          46 :     GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
    5468                 :             :     /* DT_NULL Marks end of dynamic section.  */
    5469   [ +  -  +  + ]:          46 :     if (dyn == NULL || dyn->d_tag == DT_NULL)
    5470                 :             :       break;
    5471                 :             : 
    5472   [ +  -  +  +  :          44 :     switch (dyn->d_tag) {
          +  +  +  +  +  
                +  -  + ]
    5473                 :           2 :     case DT_SYMTAB:
    5474                 :           2 :       addrs[i_symtab] = dyn->d_un.d_ptr;
    5475                 :           2 :       break;
    5476                 :             : 
    5477                 :           0 :     case DT_HASH:
    5478                 :           0 :       addrs[i_hash] = dyn->d_un.d_ptr;
    5479                 :           0 :       break;
    5480                 :             : 
    5481                 :           2 :     case DT_GNU_HASH:
    5482                 :           2 :       addrs[i_gnu_hash] = dyn->d_un.d_ptr;
    5483                 :           2 :       break;
    5484                 :             : 
    5485                 :           2 :     case DT_STRTAB:
    5486                 :           2 :       addrs[i_strtab] = dyn->d_un.d_ptr;
    5487                 :           2 :       break;
    5488                 :             : 
    5489                 :           2 :     case DT_VERSYM:
    5490                 :           2 :       addrs[i_versym] = dyn->d_un.d_ptr;
    5491                 :           2 :       break;
    5492                 :             : 
    5493                 :           2 :     case DT_VERDEF:
    5494                 :           2 :       addrs[i_verdef] = dyn->d_un.d_ptr;
    5495                 :           2 :       break;
    5496                 :             : 
    5497                 :           2 :     case DT_VERDEFNUM:
    5498                 :           2 :       addrs[i_verdefnum] = dyn->d_un.d_val;
    5499                 :           2 :       break;
    5500                 :             : 
    5501                 :           2 :     case DT_VERNEED:
    5502                 :           2 :       addrs[i_verneed] = dyn->d_un.d_ptr;
    5503                 :           2 :       break;
    5504                 :             : 
    5505                 :           2 :     case DT_VERNEEDNUM:
    5506                 :           2 :       addrs[i_verneednum] = dyn->d_un.d_val;
    5507                 :           2 :       break;
    5508                 :             : 
    5509                 :           2 :     case DT_STRSZ:
    5510                 :           2 :       addrs[i_strsz] = dyn->d_un.d_val;
    5511                 :           2 :       break;
    5512                 :             : 
    5513                 :           0 :     case DT_SYMTAB_SHNDX:
    5514                 :           0 :       addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
    5515                 :           0 :       break;
    5516                 :             :     }
    5517                 :             :   }
    5518                 :           2 : }
    5519                 :             : 
    5520                 :             : 
    5521                 :             : /* Use dynamic segment to get data for the string table section.  */
    5522                 :             : static Elf_Data *
    5523                 :           2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
    5524                 :             : {
    5525                 :           2 :   Elf_Data *strtab_data;
    5526                 :           2 :   GElf_Addr addrs[i_max] = {0,};
    5527                 :           2 :   GElf_Off offs[i_max] = {0,};
    5528                 :           2 :   get_dynscn_addrs(elf, phdr, addrs);
    5529                 :           2 :   find_offsets(elf, 0, i_max, addrs, offs);
    5530                 :           4 :   strtab_data = elf_getdata_rawchunk(
    5531                 :           2 :           elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
    5532                 :           2 :   return strtab_data;
    5533                 :             : }
    5534                 :             : 
    5535                 :             : 
    5536                 :             : struct listptr
    5537                 :             : {
    5538                 :             :   Dwarf_Off offset:(64 - 3);
    5539                 :             :   bool addr64:1;
    5540                 :             :   bool dwarf64:1;
    5541                 :             :   bool warned:1;
    5542                 :             :   struct Dwarf_CU *cu;
    5543                 :             :   unsigned int attr;
    5544                 :             : };
    5545                 :             : 
    5546                 :             : #define listptr_offset_size(p)  ((p)->dwarf64 ? 8 : 4)
    5547                 :             : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
    5548                 :             : 
    5549                 :             : static Dwarf_Addr
    5550                 :       21870 : cudie_base (Dwarf_Die *cudie)
    5551                 :             : {
    5552                 :       21870 :   Dwarf_Addr base;
    5553                 :             :   /* Find the base address of the compilation unit.  It will normally
    5554                 :             :      be specified by DW_AT_low_pc.  In DWARF-3 draft 4, the base
    5555                 :             :      address could be overridden by DW_AT_entry_pc.  It's been
    5556                 :             :      removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
    5557                 :             :      compilation units with discontinuous ranges.  */
    5558         [ -  + ]:       21870 :   if (unlikely (dwarf_lowpc (cudie, &base) != 0))
    5559                 :             :     {
    5560                 :           0 :       Dwarf_Attribute attr_mem;
    5561         [ #  # ]:           0 :       if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
    5562                 :             :                           &base) != 0)
    5563                 :           0 :         base = 0;
    5564                 :             :     }
    5565                 :       21870 :   return base;
    5566                 :             : }
    5567                 :             : 
    5568                 :             : static Dwarf_Addr
    5569                 :       21870 : listptr_base (struct listptr *p)
    5570                 :             : {
    5571                 :       21870 :   Dwarf_Die cu = CUDIE (p->cu);
    5572                 :       21870 :   return cudie_base (&cu);
    5573                 :             : }
    5574                 :             : 
    5575                 :             : /* To store the name used in compare_listptr */
    5576                 :             : static _Thread_local const char *sort_listptr_name;
    5577                 :             : 
    5578                 :             : static int
    5579                 :     1789340 : compare_listptr (const void *a, const void *b)
    5580                 :             : {
    5581                 :     1789340 :   const char *name = sort_listptr_name;
    5582                 :     1789340 :   struct listptr *p1 = (void *) a;
    5583                 :     1789340 :   struct listptr *p2 = (void *) b;
    5584                 :             : 
    5585         [ +  + ]:     1789340 :   if (p1->offset < p2->offset)
    5586                 :             :     return -1;
    5587         [ +  + ]:      176124 :   if (p1->offset > p2->offset)
    5588                 :             :     return 1;
    5589                 :             : 
    5590   [ +  -  -  + ]:        8266 :   if (!p1->warned && !p2->warned)
    5591                 :             :     {
    5592         [ -  + ]:        8266 :       if (p1->addr64 != p2->addr64)
    5593                 :             :         {
    5594                 :           0 :           p1->warned = p2->warned = true;
    5595                 :           0 :           error (0, 0,
    5596                 :           0 :                  _("%s %#" PRIx64 " used with different address sizes"),
    5597                 :             :                  name, (uint64_t) p1->offset);
    5598                 :             :         }
    5599         [ -  + ]:        8266 :       if (p1->dwarf64 != p2->dwarf64)
    5600                 :             :         {
    5601                 :           0 :           p1->warned = p2->warned = true;
    5602                 :           0 :           error (0, 0,
    5603                 :           0 :                  _("%s %#" PRIx64 " used with different offset sizes"),
    5604                 :           0 :                  name, (uint64_t) p1->offset);
    5605                 :             :         }
    5606         [ -  + ]:        8266 :       if (listptr_base (p1) != listptr_base (p2))
    5607                 :             :         {
    5608                 :           0 :           p1->warned = p2->warned = true;
    5609                 :           0 :           error (0, 0,
    5610                 :           0 :                  _("%s %#" PRIx64 " used with different base addresses"),
    5611                 :           0 :                  name, (uint64_t) p1->offset);
    5612                 :             :         }
    5613         [ +  - ]:        8266 :       if (p1->attr != p2 ->attr)
    5614                 :             :         {
    5615                 :           0 :           p1->warned = p2->warned = true;
    5616                 :           0 :           error (0, 0,
    5617                 :           0 :                  _("%s %#" PRIx64
    5618                 :             :                           " used with different attribute %s and %s"),
    5619                 :           0 :                  name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
    5620                 :             :                  dwarf_attr_name (p2->attr));
    5621                 :             :         }
    5622                 :             :     }
    5623                 :             : 
    5624                 :             :   return 0;
    5625                 :             : }
    5626                 :             : 
    5627                 :             : struct listptr_table
    5628                 :             : {
    5629                 :             :   size_t n;
    5630                 :             :   size_t alloc;
    5631                 :             :   struct listptr *table;
    5632                 :             : };
    5633                 :             : 
    5634                 :             : static struct listptr_table known_locsptr;
    5635                 :             : static struct listptr_table known_loclistsptr;
    5636                 :             : static struct listptr_table known_rangelistptr;
    5637                 :             : static struct listptr_table known_rnglistptr;
    5638                 :             : static struct listptr_table known_addrbases;
    5639                 :             : static struct listptr_table known_stroffbases;
    5640                 :             : 
    5641                 :             : static void
    5642                 :         502 : reset_listptr (struct listptr_table *table)
    5643                 :             : {
    5644                 :         502 :   free (table->table);
    5645                 :         502 :   table->table = NULL;
    5646                 :         502 :   table->n = table->alloc = 0;
    5647                 :             : }
    5648                 :             : 
    5649                 :             : /* Returns false if offset doesn't fit.  See struct listptr.  */
    5650                 :             : static bool
    5651                 :      257642 : notice_listptr (enum section_e section, struct listptr_table *table,
    5652                 :             :                 uint_fast8_t address_size, uint_fast8_t offset_size,
    5653                 :             :                 struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
    5654                 :             : {
    5655         [ +  + ]:      257642 :   if (print_debug_sections & section)
    5656                 :             :     {
    5657         [ +  + ]:      257390 :       if (table->n == table->alloc)
    5658                 :             :         {
    5659         [ +  + ]:         410 :           if (table->alloc == 0)
    5660                 :         174 :             table->alloc = 128;
    5661                 :             :           else
    5662                 :         236 :             table->alloc *= 2;
    5663                 :         410 :           table->table = xrealloc (table->table,
    5664                 :         410 :                                    table->alloc * sizeof table->table[0]);
    5665                 :             :         }
    5666                 :             : 
    5667                 :      257390 :       struct listptr *p = &table->table[table->n++];
    5668                 :             : 
    5669                 :      257390 :       *p = (struct listptr)
    5670                 :             :         {
    5671                 :      257390 :           .addr64 = address_size == 8,
    5672                 :      257390 :           .dwarf64 = offset_size == 8,
    5673                 :             :           .offset = offset,
    5674                 :             :           .cu = cu,
    5675                 :             :           .attr = attr
    5676                 :             :         };
    5677                 :             : 
    5678         [ +  - ]:      257390 :       if (p->offset != offset)
    5679                 :             :         {
    5680                 :           0 :           table->n--;
    5681                 :           0 :           return false;
    5682                 :             :         }
    5683                 :             :     }
    5684                 :             :   return true;
    5685                 :             : }
    5686                 :             : 
    5687                 :             : static void
    5688                 :         182 : sort_listptr (struct listptr_table *table, const char *name)
    5689                 :             : {
    5690         [ +  + ]:         182 :   if (table->n > 0)
    5691                 :             :     {
    5692                 :         174 :       sort_listptr_name = name;
    5693                 :         174 :       qsort (table->table, table->n, sizeof table->table[0],
    5694                 :             :              &compare_listptr);
    5695                 :             :     }
    5696                 :         182 : }
    5697                 :             : 
    5698                 :             : static bool
    5699                 :         208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
    5700                 :             :                    uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
    5701                 :             :                    Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
    5702                 :             :                    unsigned char **readp, unsigned char *endp,
    5703                 :             :                    unsigned int *attr)
    5704                 :             : {
    5705         [ -  + ]:         208 :   if (table->n == 0)
    5706                 :             :     return false;
    5707                 :             : 
    5708   [ +  -  +  + ]:         336 :   while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
    5709                 :         128 :     ++*idxp;
    5710                 :             : 
    5711                 :         208 :   struct listptr *p = &table->table[*idxp];
    5712                 :             : 
    5713         [ +  - ]:         208 :   if (*idxp == table->n
    5714         [ -  + ]:         208 :       || p->offset >= (Dwarf_Off) (endp - *readp + offset))
    5715                 :             :     {
    5716                 :           0 :       *readp = endp;
    5717                 :           0 :       printf (_(" [%6tx]  <UNUSED GARBAGE IN REST OF SECTION>\n"),
    5718                 :             :               offset);
    5719                 :           0 :       return true;
    5720                 :             :     }
    5721                 :             : 
    5722         [ -  + ]:         208 :   if (p->offset != (Dwarf_Off) offset)
    5723                 :             :     {
    5724                 :           0 :       *readp += p->offset - offset;
    5725                 :           0 :       printf (_(" [%6tx]  <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
    5726                 :             :               offset, (Dwarf_Off) p->offset - offset);
    5727                 :           0 :       return true;
    5728                 :             :     }
    5729                 :             : 
    5730         [ +  - ]:         208 :   if (address_sizep != NULL)
    5731         [ +  + ]:         224 :     *address_sizep = listptr_address_size (p);
    5732         [ +  + ]:         208 :   if (offset_sizep != NULL)
    5733         [ +  - ]:         296 :     *offset_sizep = listptr_offset_size (p);
    5734         [ +  - ]:         208 :   if (base != NULL)
    5735                 :         208 :     *base = listptr_base (p);
    5736         [ +  - ]:         208 :   if (cu != NULL)
    5737                 :         208 :     *cu = p->cu;
    5738         [ +  + ]:         208 :   if (attr != NULL)
    5739                 :         148 :     *attr = p->attr;
    5740                 :             : 
    5741                 :             :   return false;
    5742                 :             : }
    5743                 :             : 
    5744                 :             : static Dwarf_Off
    5745                 :      109742 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
    5746                 :             : {
    5747                 :             :   /* Note that multiple attributes could in theory point to the same loclist
    5748                 :             :      offset, so make sure we pick one that is bigger than the current one.
    5749                 :             :      The table is sorted on offset.  */
    5750         [ +  - ]:      109742 :   if (*idxp < table->n)
    5751                 :             :     {
    5752         [ +  - ]:      218308 :       while (++*idxp < table->n)
    5753                 :             :         {
    5754                 :      218308 :           Dwarf_Off next = table->table[*idxp].offset;
    5755         [ +  + ]:      218308 :           if (next > off)
    5756                 :             :             return next;
    5757                 :             :         }
    5758                 :             :     }
    5759                 :             :   return 0;
    5760                 :             : }
    5761                 :             : 
    5762                 :             : /* Returns the listptr associated with the given index, or NULL.  */
    5763                 :             : static struct listptr *
    5764                 :     1528460 : get_listptr (struct listptr_table *table, size_t idx)
    5765                 :             : {
    5766                 :     1528460 :   if (idx >= table->n)
    5767                 :             :     return NULL;
    5768                 :     1528208 :   return &table->table[idx];
    5769                 :             : }
    5770                 :             : 
    5771                 :             : /* Returns the next index, base address and CU associated with the
    5772                 :             :    list unit offsets.  If there is none false is returned, otherwise
    5773                 :             :    true.  Assumes the table has been sorted.  */
    5774                 :             : static bool
    5775                 :        5130 : listptr_cu (struct listptr_table *table, size_t *idxp,
    5776                 :             :             Dwarf_Off start, Dwarf_Off end,
    5777                 :             :             Dwarf_Addr *base, struct Dwarf_CU **cu)
    5778                 :             : {
    5779                 :        5130 :   while (*idxp < table->n
    5780   [ +  -  +  + ]:       41186 :          && table->table[*idxp].offset < start)
    5781                 :       36056 :     ++*idxp;
    5782                 :             : 
    5783         [ +  - ]:        5130 :   if (*idxp < table->n
    5784         [ +  - ]:        5130 :       && table->table[*idxp].offset >= start
    5785         [ +  - ]:        5130 :       && table->table[*idxp].offset < end)
    5786                 :             :     {
    5787                 :        5130 :       struct listptr *p = &table->table[*idxp];
    5788                 :        5130 :       *base = listptr_base (p);
    5789                 :        5130 :       *cu = p->cu;
    5790                 :        5130 :       return true;
    5791                 :             :     }
    5792                 :             : 
    5793                 :             :   return false;
    5794                 :             : }
    5795                 :             : 
    5796                 :             : /* Returns the next index with the current CU for the given attribute.
    5797                 :             :    If there is none false is returned, otherwise true.  Assumes the
    5798                 :             :    table has been sorted.  */
    5799                 :             : static bool
    5800                 :      763878 : listptr_attr (struct listptr_table *table, size_t idxp,
    5801                 :             :               Dwarf_Off offset, unsigned int attr)
    5802                 :             : {
    5803                 :     1528424 :   struct listptr *listptr;
    5804                 :     1528424 :   do
    5805                 :             :     {
    5806         [ +  + ]:     1528424 :       listptr = get_listptr (table, idxp);
    5807         [ +  - ]:     1528182 :       if (listptr == NULL)
    5808                 :             :         return false;
    5809                 :             : 
    5810   [ +  +  +  + ]:     1528182 :       if (listptr->offset == offset && listptr->attr == attr)
    5811                 :             :         return true;
    5812                 :             : 
    5813                 :     1418440 :       idxp++;
    5814                 :             :     }
    5815         [ +  + ]:     1418440 :   while (listptr->offset <= offset);
    5816                 :             : 
    5817                 :             :   return false;
    5818                 :             : }
    5819                 :             : 
    5820                 :             : static void
    5821                 :          88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    5822                 :             :                             Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
    5823                 :             :                             Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
    5824                 :             :                             FILE *out)
    5825                 :             : {
    5826                 :          88 :   Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
    5827         [ +  - ]:          88 :   if (elf_data == NULL)
    5828                 :             :     return;
    5829                 :             : 
    5830                 :          88 :   const size_t sh_size = elf_data->d_size;
    5831                 :             : 
    5832                 :          88 :   fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
    5833                 :             :                   " [ Code]\n"),
    5834                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    5835                 :          88 :            (uint64_t) shdr->sh_offset);
    5836                 :             : 
    5837                 :          88 :   Dwarf_Off offset = 0;
    5838         [ +  + ]:        3682 :   while (offset < sh_size)
    5839                 :             :     {
    5840                 :        3594 :       fprintf (out, _("\nAbbreviation section at offset %" PRIu64 ":\n"),
    5841                 :             :                offset);
    5842                 :             : 
    5843                 :      332062 :       while (1)
    5844                 :      164234 :         {
    5845                 :      167828 :           size_t length;
    5846                 :      167828 :           Dwarf_Abbrev abbrev;
    5847                 :             : 
    5848                 :      167828 :           int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
    5849         [ +  + ]:      167828 :           if (res != 0)
    5850                 :             :             {
    5851         [ -  + ]:        3594 :               if (unlikely (res < 0))
    5852                 :             :                 {
    5853                 :           0 :                   fprintf (out, _("\
    5854                 :             :  *** error while reading abbreviation: %s\n"),
    5855                 :             :                            dwarf_errmsg (-1));
    5856                 :           0 :                   return;
    5857                 :             :                 }
    5858                 :             : 
    5859                 :             :               /* This is the NUL byte at the end of the section.  */
    5860                 :        3594 :               ++offset;
    5861                 :        3594 :               break;
    5862                 :             :             }
    5863                 :             : 
    5864                 :             :           /* We know these calls can never fail.  */
    5865                 :      164234 :           unsigned int code = dwarf_getabbrevcode (&abbrev);
    5866                 :      164234 :           unsigned int tag = dwarf_getabbrevtag (&abbrev);
    5867                 :      164234 :           int has_children = dwarf_abbrevhaschildren (&abbrev);
    5868                 :             : 
    5869         [ +  + ]:      164234 :           fprintf (out, _(" [%5u] offset: %" PRId64
    5870                 :             :                            ", children: %s, tag: %s\n"),
    5871                 :             :                    code, (int64_t) offset,
    5872                 :             :                    has_children ? yes_str : no_str,
    5873                 :             :                    dwarf_tag_name (tag));
    5874                 :             : 
    5875                 :      164234 :           size_t cnt = 0;
    5876                 :      164234 :           unsigned int name;
    5877                 :      164234 :           unsigned int form;
    5878                 :      164234 :           Dwarf_Sword data;
    5879                 :      164234 :           Dwarf_Off enoffset;
    5880                 :      164234 :           while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
    5881         [ +  + ]:      941564 :                                            &data, &enoffset) == 0)
    5882                 :             :             {
    5883                 :      777330 :               fprintf (out, "          attr: %s, form: %s",
    5884                 :             :                        dwarf_attr_name (name), dwarf_form_name (form));
    5885         [ +  + ]:      777330 :               if (form == DW_FORM_implicit_const)
    5886                 :       55122 :                 fprintf (out, " (%" PRId64 ")", data);
    5887                 :      777330 :               fprintf (out, ", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
    5888                 :      777330 :               ++cnt;
    5889                 :             :             }
    5890                 :             : 
    5891                 :      164234 :           offset += length;
    5892                 :             :         }
    5893                 :             :     }
    5894                 :             : }
    5895                 :             : 
    5896                 :             : 
    5897                 :             : static void
    5898                 :           8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    5899                 :             :                           Ebl *ebl, GElf_Ehdr *ehdr,
    5900                 :             :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
    5901                 :             :                           FILE *out)
    5902                 :             : {
    5903                 :           8 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
    5904         [ +  - ]:           8 :   if (data == NULL)
    5905                 :             :     return;
    5906                 :             : 
    5907                 :           8 :   fprintf (out, _("\
    5908                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    5909                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    5910                 :           8 :            (uint64_t) shdr->sh_offset);
    5911                 :             : 
    5912         [ +  - ]:           8 :   if (shdr->sh_size == 0)
    5913                 :             :     return;
    5914                 :             : 
    5915                 :           8 :   size_t idx = 0;
    5916                 :           8 :   sort_listptr (&known_addrbases, "addr_base");
    5917                 :             : 
    5918                 :           8 :   const unsigned char *start = (const unsigned char *) data->d_buf;
    5919                 :           8 :   const unsigned char *readp = start;
    5920                 :           8 :   const unsigned char *readendp = ((const unsigned char *) data->d_buf
    5921                 :           8 :                                    + data->d_size);
    5922                 :             : 
    5923         [ +  + ]:          24 :   while (readp < readendp)
    5924                 :             :     {
    5925                 :             :       /* We cannot really know whether or not there is an header.  The
    5926                 :             :          DebugFission extension to DWARF4 doesn't add one.  The DWARF5
    5927                 :             :          .debug_addr variant does.  Whether or not we have an header,
    5928                 :             :          DW_AT_[GNU_]addr_base points at "index 0".  So if the current
    5929                 :             :          offset equals the CU addr_base then we can just start
    5930                 :             :          printing addresses.  If there is no CU with an exact match
    5931                 :             :          then we'll try to parse the header first.  */
    5932                 :          16 :       Dwarf_Off off = (Dwarf_Off) (readp
    5933                 :          16 :                                    - (const unsigned char *) data->d_buf);
    5934                 :             : 
    5935                 :          16 :       fprintf (out, "Table at offset %" PRIx64 " ", off);
    5936                 :             : 
    5937         [ +  - ]:          16 :       struct listptr *listptr = get_listptr (&known_addrbases, idx++);
    5938                 :          16 :       const unsigned char *next_unitp;
    5939                 :             : 
    5940                 :          16 :       uint64_t unit_length;
    5941                 :          16 :       uint16_t version;
    5942                 :          16 :       uint8_t address_size;
    5943                 :          16 :       uint8_t segment_size;
    5944         [ -  + ]:          16 :       if (listptr == NULL)
    5945                 :             :         {
    5946                 :           0 :           error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
    5947                 :             :                  off);
    5948                 :             : 
    5949                 :             :           /* We will have to assume it is just addresses to the end... */
    5950         [ #  # ]:           0 :           address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    5951                 :           0 :           next_unitp = readendp;
    5952                 :           0 :           fprintf (out, "Unknown CU:\n");
    5953                 :             :         }
    5954                 :             :       else
    5955                 :             :         {
    5956                 :          16 :           Dwarf_Die cudie;
    5957         [ -  + ]:          16 :           if (dwarf_cu_die (listptr->cu, &cudie,
    5958                 :             :                             NULL, NULL, NULL, NULL,
    5959                 :             :                             NULL, NULL) == NULL)
    5960                 :           0 :             fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
    5961                 :             :           else
    5962                 :          16 :             fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
    5963                 :             : 
    5964         [ +  + ]:          16 :           if (listptr->offset == off)
    5965                 :             :             {
    5966         [ -  + ]:           4 :               address_size = listptr_address_size (listptr);
    5967                 :           4 :               segment_size = 0;
    5968                 :           4 :               version = 4;
    5969                 :             : 
    5970                 :             :               /* The addresses start here, but where do they end?  */
    5971         [ +  + ]:           4 :               listptr = get_listptr (&known_addrbases, idx);
    5972         [ -  + ]:           2 :               if (listptr == NULL)
    5973                 :             :                 next_unitp = readendp;
    5974         [ +  - ]:           2 :               else if (listptr->cu->version < 5)
    5975                 :             :                 {
    5976                 :           2 :                   next_unitp = start + listptr->offset;
    5977   [ +  -  -  + ]:           2 :                   if (listptr->offset < off || listptr->offset > data->d_size)
    5978                 :             :                     {
    5979                 :           0 :                       error (0, 0,
    5980                 :             :                              "Warning: Bad address base for next unit at %"
    5981                 :             :                              PRIx64, off);
    5982                 :           0 :                       next_unitp = readendp;
    5983                 :             :                     }
    5984                 :             :                 }
    5985                 :             :               else
    5986                 :             :                 {
    5987                 :             :                   /* Tricky, we don't have a header for this unit, but
    5988                 :             :                      there is one for the next.  We will have to
    5989                 :             :                      "guess" how big it is and subtract it from the
    5990                 :             :                      offset (because that points after the header).  */
    5991         [ #  # ]:           0 :                   unsigned int offset_size = listptr_offset_size (listptr);
    5992                 :           0 :                   Dwarf_Off next_off = (listptr->offset
    5993                 :           0 :                                         - (offset_size == 4 ? 4 : 12) /* len */
    5994                 :             :                                         - 2 /* version */
    5995                 :             :                                         - 1 /* address size */
    5996                 :           0 :                                         - 1); /* segment selector size */
    5997                 :           0 :                   next_unitp = start + next_off;
    5998   [ #  #  #  # ]:           0 :                   if (next_off < off || next_off > data->d_size)
    5999                 :             :                     {
    6000                 :           0 :                       error (0, 0,
    6001                 :             :                              "Warning: Couldn't calculate .debug_addr "
    6002                 :             :                              " unit length at %" PRIx64, off);
    6003                 :           0 :                       next_unitp = readendp;
    6004                 :             :                     }
    6005                 :             :                 }
    6006                 :           4 :               unit_length = (uint64_t) (next_unitp - readp);
    6007                 :             : 
    6008                 :             :               /* Pretend we have a header.  */
    6009                 :           4 :               fprintf (out, "\n");
    6010                 :           4 :               fprintf (out, _(" Length:         %8" PRIu64 "\n"),
    6011                 :             :                        unit_length);
    6012                 :           4 :               fprintf (out, _(" DWARF version:  %8" PRIu16 "\n"), version);
    6013                 :           4 :               fprintf (out, _(" Address size:   %8" PRIu64 "\n"),
    6014                 :             :                        (uint64_t) address_size);
    6015                 :           4 :               fprintf (out, _(" Segment size:   %8" PRIu64 "\n"),
    6016                 :             :                        (uint64_t) segment_size);
    6017                 :           4 :               fprintf (out, "\n");
    6018                 :             :             }
    6019                 :             :           else
    6020                 :             :             {
    6021                 :             :               /* OK, we have to parse an header first.  */
    6022         [ -  + ]:          12 :               unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    6023         [ -  + ]:          12 :               if (unlikely (unit_length == 0xffffffff))
    6024                 :             :                 {
    6025         [ #  # ]:           0 :                   if (unlikely (readp > readendp - 8))
    6026                 :             :                     {
    6027                 :           0 :                     invalid_data:
    6028                 :           0 :                       error (0, 0, "Invalid data");
    6029                 :           0 :                       return;
    6030                 :             :                     }
    6031         [ #  # ]:           0 :                   unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    6032                 :             :                 }
    6033                 :          12 :               fprintf (out, "\n");
    6034                 :          12 :               fprintf (out, _(" Length:         %8" PRIu64 "\n"),
    6035                 :             :                        unit_length);
    6036                 :             : 
    6037                 :             :               /* We need at least 2-bytes (version) + 1-byte
    6038                 :             :                  (addr_size) + 1-byte (segment_size) = 4 bytes to
    6039                 :             :                  complete the header.  And this unit cannot go beyond
    6040                 :             :                  the section data.  */
    6041         [ +  - ]:          12 :               if (readp > readendp - 4
    6042         [ +  - ]:          12 :                   || unit_length < 4
    6043         [ -  + ]:          12 :                   || unit_length > (uint64_t) (readendp - readp))
    6044                 :           0 :                 goto invalid_data;
    6045                 :             : 
    6046                 :          12 :               next_unitp = readp + unit_length;
    6047                 :             : 
    6048         [ -  + ]:          12 :               version = read_2ubyte_unaligned_inc (dbg, readp);
    6049                 :          12 :               fprintf (out, _(" DWARF version:  %8" PRIu16 "\n"), version);
    6050                 :             : 
    6051         [ -  + ]:          12 :               if (version != 5)
    6052                 :             :                 {
    6053                 :           0 :                   error (0, 0, _("Unknown version"));
    6054                 :           0 :                   goto next_unit;
    6055                 :             :                 }
    6056                 :             : 
    6057                 :          12 :               address_size = *readp++;
    6058                 :          12 :               fprintf (out, _(" Address size:   %8" PRIu64 "\n"),
    6059                 :             :                        (uint64_t) address_size);
    6060                 :             : 
    6061         [ -  + ]:          12 :               if (address_size != 4 && address_size != 8)
    6062                 :             :                 {
    6063                 :           0 :                   error (0, 0, _("unsupported address size"));
    6064                 :           0 :                   goto next_unit;
    6065                 :             :                 }
    6066                 :             : 
    6067                 :          12 :               segment_size = *readp++;
    6068                 :          12 :               fprintf (out, _(" Segment size:   %8" PRIu64 "\n"),
    6069                 :             :                        (uint64_t) segment_size);
    6070                 :          12 :               fprintf (out, "\n");
    6071                 :             : 
    6072         [ -  + ]:          12 :               if (segment_size != 0)
    6073                 :             :                 {
    6074                 :           0 :                   error (0, 0, _("unsupported segment size"));
    6075                 :           0 :                   goto next_unit;
    6076                 :             :                 }
    6077                 :             : 
    6078         [ -  + ]:          12 :               if (listptr->offset != (Dwarf_Off) (readp - start))
    6079                 :             :                 {
    6080                 :           0 :                   error (0, 0, "Address index doesn't start after header");
    6081                 :           0 :                   goto next_unit;
    6082                 :             :                 }
    6083                 :             :             }
    6084                 :             :         }
    6085                 :             : 
    6086                 :          16 :       int digits = 1;
    6087                 :          16 :       size_t addresses = (next_unitp - readp) / address_size;
    6088         [ +  + ]:          24 :       while (addresses >= 10)
    6089                 :             :         {
    6090                 :           8 :           ++digits;
    6091                 :           8 :           addresses /= 10;
    6092                 :             :         }
    6093                 :             : 
    6094                 :          16 :       unsigned int uidx = 0;
    6095                 :          16 :       size_t index_offset =  readp - (const unsigned char *) data->d_buf;
    6096                 :          16 :       fprintf (out, " Addresses start at offset 0x%zx:\n", index_offset);
    6097         [ +  + ]:         188 :       while (readp <= next_unitp - address_size)
    6098                 :             :         {
    6099   [ -  +  +  +  :         172 :           Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
             -  +  -  + ]
    6100                 :             :                                                      readp);
    6101                 :         172 :           fprintf (out, " [%*u] ", digits, uidx++);
    6102                 :         172 :           print_dwarf_addr (dwflmod, address_size, addr, addr, out);
    6103                 :         172 :           fprintf (out, "\n");
    6104                 :             :         }
    6105                 :          16 :       fprintf (out, "\n");
    6106                 :             : 
    6107         [ +  - ]:          16 :       if (readp != next_unitp)
    6108                 :           0 :         error (0, 0, "extra %zd bytes at end of unit",
    6109                 :           0 :                (size_t) (next_unitp - readp));
    6110                 :             : 
    6111                 :          16 :     next_unit:
    6112                 :             :       readp = next_unitp;
    6113                 :             :     }
    6114                 :             : }
    6115                 :             : 
    6116                 :             : /* Print content of DWARF .debug_aranges section.  We fortunately do
    6117                 :             :    not have to know a bit about the structure of the section, libdwarf
    6118                 :             :    takes care of it.  */
    6119                 :             : static void
    6120                 :           2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
    6121                 :             :                                GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
    6122                 :             : {
    6123                 :           2 :   Dwarf_Aranges *aranges;
    6124                 :           2 :   size_t cnt;
    6125         [ -  + ]:           2 :   if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
    6126                 :             :     {
    6127                 :           0 :       error (0, 0, _("cannot get .debug_aranges content: %s"),
    6128                 :             :              dwarf_errmsg (-1));
    6129                 :           0 :       return;
    6130                 :             :     }
    6131                 :             : 
    6132                 :           2 :   GElf_Shdr glink_mem;
    6133                 :           2 :   GElf_Shdr *glink;
    6134                 :           2 :   glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
    6135         [ -  + ]:           2 :   if (glink == NULL)
    6136                 :             :     {
    6137                 :           0 :       error (0, 0, _("invalid sh_link value in section %zu"),
    6138                 :             :              elf_ndxscn (scn));
    6139                 :           0 :       return;
    6140                 :             :     }
    6141                 :             : 
    6142                 :           2 :   fprintf (out, ngettext ("\
    6143                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
    6144                 :             :                      "\
    6145                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
    6146                 :             :                      cnt),
    6147                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    6148                 :           2 :            (uint64_t) shdr->sh_offset, cnt);
    6149                 :             : 
    6150                 :             :   /* Compute floor(log16(cnt)).  */
    6151                 :           2 :   size_t tmp = cnt;
    6152                 :           2 :   int digits = 1;
    6153         [ -  + ]:           2 :   while (tmp >= 16)
    6154                 :             :     {
    6155                 :           0 :       ++digits;
    6156                 :           0 :       tmp >>= 4;
    6157                 :             :     }
    6158                 :             : 
    6159         [ +  + ]:          12 :   for (size_t n = 0; n < cnt; ++n)
    6160                 :             :     {
    6161                 :          10 :       Dwarf_Arange *runp = dwarf_onearange (aranges, n);
    6162         [ -  + ]:          10 :       if (unlikely (runp == NULL))
    6163                 :             :         {
    6164                 :           0 :           fprintf (out, "cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
    6165                 :           0 :           return;
    6166                 :             :         }
    6167                 :             : 
    6168                 :          10 :       Dwarf_Addr start;
    6169                 :          10 :       Dwarf_Word length;
    6170                 :          10 :       Dwarf_Off offset;
    6171                 :             : 
    6172         [ -  + ]:          10 :       if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
    6173                 :           0 :         fprintf (out, _(" [%*zu] ???\n"), digits, n);
    6174                 :             :       else
    6175                 :          10 :         fprintf (out, _(" [%*zu] start: %0#*" PRIx64
    6176                 :             :                           ", length: %5" PRIu64 ", CU DIE offset: %6"
    6177                 :             :                           PRId64 "\n"),
    6178         [ -  + ]:          10 :                  digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
    6179                 :             :                  (uint64_t) start, (uint64_t) length, (int64_t) offset);
    6180                 :             :     }
    6181                 :             : }
    6182                 :             : 
    6183                 :             : 
    6184                 :             : /* Print content of DWARF .debug_aranges section.  */
    6185                 :             : static void
    6186                 :          92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
    6187                 :             :                              Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
    6188                 :             :                              GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
    6189                 :             : {
    6190         [ +  + ]:          92 :   if (decodedaranges)
    6191                 :             :     {
    6192                 :           2 :       print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg, out);
    6193                 :           2 :       return;
    6194                 :             :     }
    6195                 :             : 
    6196                 :          90 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
    6197         [ +  - ]:          90 :   if (data == NULL)
    6198                 :             :     return;
    6199                 :             : 
    6200                 :          90 :   fprintf (out, _("\
    6201                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    6202                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    6203                 :          90 :            (uint64_t) shdr->sh_offset);
    6204                 :             : 
    6205                 :          90 :   const unsigned char *readp = data->d_buf;
    6206                 :          90 :   const unsigned char *readendp = readp + data->d_size;
    6207                 :             : 
    6208         [ +  + ]:        3666 :   while (readp < readendp)
    6209                 :             :     {
    6210                 :        3576 :       const unsigned char *hdrstart = readp;
    6211                 :        3576 :       size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
    6212                 :             : 
    6213                 :        3576 :       fprintf (out, _("\nTable at offset %zu:\n"), start_offset);
    6214         [ -  + ]:        3576 :       if (readp + 4 > readendp)
    6215                 :             :         {
    6216                 :           0 :         invalid_data:
    6217                 :           0 :           error (0, 0, _("invalid data in section [%zu] '%s'"),
    6218                 :             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    6219                 :           0 :           return;
    6220                 :             :         }
    6221                 :             : 
    6222         [ +  + ]:        3576 :       Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
    6223                 :        3576 :       unsigned int length_bytes = 4;
    6224         [ -  + ]:        3576 :       if (length == DWARF3_LENGTH_64_BIT)
    6225                 :             :         {
    6226         [ #  # ]:           0 :           if (readp + 8 > readendp)
    6227                 :           0 :             goto invalid_data;
    6228         [ #  # ]:           0 :           length = read_8ubyte_unaligned_inc (dbg, readp);
    6229                 :           0 :           length_bytes = 8;
    6230                 :             :         }
    6231                 :             : 
    6232                 :        3576 :       const unsigned char *nexthdr = readp + length;
    6233                 :        3576 :       fprintf (out, _("\n Length:        %6" PRIu64 "\n"),
    6234                 :             :                (uint64_t) length);
    6235                 :             : 
    6236         [ -  + ]:        3576 :       if (unlikely (length > (size_t) (readendp - readp)))
    6237                 :           0 :         goto invalid_data;
    6238                 :             : 
    6239         [ -  + ]:        3576 :       if (length == 0)
    6240                 :           0 :         continue;
    6241                 :             : 
    6242         [ -  + ]:        3576 :       if (readp + 2 > readendp)
    6243                 :           0 :         goto invalid_data;
    6244         [ +  + ]:        3576 :       uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    6245                 :        3576 :       fprintf (out, _(" DWARF version: %6" PRIuFAST16 "\n"),
    6246                 :             :                version);
    6247         [ -  + ]:        3576 :       if (version != 2)
    6248                 :             :         {
    6249                 :           0 :           error (0, 0, _("unsupported aranges version"));
    6250                 :           0 :           goto next_table;
    6251                 :             :         }
    6252                 :             : 
    6253                 :        3576 :       Dwarf_Word offset;
    6254         [ -  + ]:        3576 :       if (readp + length_bytes > readendp)
    6255                 :           0 :         goto invalid_data;
    6256         [ -  + ]:        3576 :       if (length_bytes == 8)
    6257         [ #  # ]:           0 :         offset = read_8ubyte_unaligned_inc (dbg, readp);
    6258                 :             :       else
    6259         [ +  + ]:        3576 :         offset = read_4ubyte_unaligned_inc (dbg, readp);
    6260                 :        3576 :       fprintf (out, _(" CU offset:     %6" PRIx64 "\n"),
    6261                 :             :                (uint64_t) offset);
    6262                 :             : 
    6263         [ -  + ]:        3576 :       if (readp + 1 > readendp)
    6264                 :           0 :         goto invalid_data;
    6265                 :        3576 :       unsigned int address_size = *readp++;
    6266                 :        3576 :       fprintf (out, _(" Address size:  %6" PRIu64 "\n"),
    6267                 :             :                (uint64_t) address_size);
    6268         [ -  + ]:        3576 :       if (address_size != 4 && address_size != 8)
    6269                 :             :         {
    6270                 :           0 :           error (0, 0, _("unsupported address size"));
    6271                 :           0 :           goto next_table;
    6272                 :             :         }
    6273                 :             : 
    6274         [ -  + ]:        3576 :       if (readp + 1 > readendp)
    6275                 :           0 :         goto invalid_data;
    6276                 :        3576 :       unsigned int segment_size = *readp++;
    6277                 :        3576 :       fprintf (out, _(" Segment size:  %6" PRIu64 "\n\n"),
    6278                 :             :                (uint64_t) segment_size);
    6279   [ -  +  -  - ]:        3576 :       if (segment_size != 0 && segment_size != 4 && segment_size != 8)
    6280                 :             :         {
    6281                 :           0 :           error (0, 0, _("unsupported segment size"));
    6282                 :           0 :           goto next_table;
    6283                 :             :         }
    6284                 :             : 
    6285                 :             :       /* Round the address to the next multiple of 2*address_size.  */
    6286                 :        3576 :       readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
    6287                 :        3576 :                 % (2 * address_size));
    6288                 :             : 
    6289         [ +  - ]:        9170 :       while (readp < nexthdr)
    6290                 :             :         {
    6291                 :        9170 :           Dwarf_Word range_address;
    6292                 :        9170 :           Dwarf_Word range_length;
    6293                 :        9170 :           Dwarf_Word segment = 0;
    6294         [ -  + ]:        9170 :           if (readp + 2 * address_size + segment_size > readendp)
    6295                 :           0 :             goto invalid_data;
    6296         [ +  + ]:        9170 :           if (address_size == 4)
    6297                 :             :             {
    6298         [ +  + ]:          92 :               range_address = read_4ubyte_unaligned_inc (dbg, readp);
    6299         [ +  + ]:          92 :               range_length = read_4ubyte_unaligned_inc (dbg, readp);
    6300                 :             :             }
    6301                 :             :           else
    6302                 :             :             {
    6303         [ +  + ]:        9078 :               range_address = read_8ubyte_unaligned_inc (dbg, readp);
    6304         [ +  + ]:        9078 :               range_length = read_8ubyte_unaligned_inc (dbg, readp);
    6305                 :             :             }
    6306                 :             : 
    6307         [ -  + ]:        9170 :           if (segment_size == 4)
    6308         [ #  # ]:           0 :             segment = read_4ubyte_unaligned_inc (dbg, readp);
    6309         [ -  + ]:        9170 :           else if (segment_size == 8)
    6310         [ #  # ]:           0 :             segment = read_8ubyte_unaligned_inc (dbg, readp);
    6311                 :             : 
    6312         [ +  + ]:        9170 :           if (range_address == 0 && range_length == 0 && segment == 0)
    6313                 :             :             break;
    6314                 :             : 
    6315                 :        5594 :           fprintf (out, "   ");
    6316                 :        5594 :           print_dwarf_addr (dwflmod, address_size, range_address,
    6317                 :             :                             range_address, out);
    6318                 :        5594 :           fprintf (out, "..");
    6319                 :        5594 :           print_dwarf_addr (dwflmod, address_size,
    6320                 :        5594 :                             range_address + range_length - 1,
    6321                 :             :                             range_length, out);
    6322         [ -  + ]:        5594 :           if (segment_size != 0)
    6323                 :           0 :             fprintf (out, " (%" PRIx64 ")\n", (uint64_t) segment);
    6324                 :             :           else
    6325                 :        5594 :             fprintf (out, "\n");
    6326                 :             :         }
    6327                 :             : 
    6328                 :        3576 :     next_table:
    6329         [ -  + ]:        3576 :       if (readp != nexthdr)
    6330                 :             :         {
    6331                 :           0 :           size_t padding = nexthdr - readp;
    6332                 :           0 :           fprintf (out, _("   %zu padding bytes\n"), padding);
    6333                 :           0 :           readp = nexthdr;
    6334                 :             :         }
    6335                 :             :     }
    6336                 :             : }
    6337                 :             : 
    6338                 :             : 
    6339                 :             : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
    6340                 :             : 
    6341                 :             : /* Returns true and sets cu and cu_base if the given Dwarf is a split
    6342                 :             :    DWARF (.dwo) file.  */
    6343                 :             : static bool
    6344                 :           0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
    6345                 :             : {
    6346                 :           0 :   uint64_t id;
    6347         [ #  # ]:           0 :   if (is_split_dwarf (dbg, &id, cu))
    6348                 :             :     {
    6349                 :           0 :       Dwarf_Die cudie;
    6350         [ #  # ]:           0 :       if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
    6351                 :             :         {
    6352                 :           0 :           *cu_base = cudie_base (&cudie);
    6353                 :           0 :           return true;
    6354                 :             :         }
    6355                 :             :     }
    6356                 :             :   return false;
    6357                 :             : }
    6358                 :             : 
    6359                 :             : /* Print content of DWARF .debug_rnglists section.  */
    6360                 :             : static void
    6361                 :          38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
    6362                 :             :                               Ebl *ebl,
    6363                 :             :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
    6364                 :             :                               Elf_Scn *scn, GElf_Shdr *shdr,
    6365                 :             :                               Dwarf *dbg __attribute__((unused)),
    6366                 :             :                               FILE *out)
    6367                 :             : {
    6368                 :          38 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
    6369         [ +  - ]:          38 :   if (data == NULL)
    6370                 :           0 :     return;
    6371                 :             : 
    6372                 :          38 :   fprintf (out, _("\
    6373                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    6374                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    6375                 :          38 :            (uint64_t) shdr->sh_offset);
    6376                 :             : 
    6377                 :             :   /* For the listptr to get the base address/CU.  */
    6378                 :          38 :   sort_listptr (&known_rnglistptr, "rnglistptr");
    6379                 :          38 :   size_t listptr_idx = 0;
    6380                 :             : 
    6381                 :          38 :   const unsigned char *readp = data->d_buf;
    6382                 :          38 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    6383                 :          38 :                                         + data->d_size);
    6384         [ +  + ]:        2076 :   while (readp < dataend)
    6385                 :             :     {
    6386         [ -  + ]:        2038 :       if (unlikely (readp > dataend - 4))
    6387                 :             :         {
    6388                 :           0 :         invalid_data:
    6389                 :           0 :           error (0, 0, _("invalid data in section [%zu] '%s'"),
    6390                 :             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    6391                 :           0 :           return;
    6392                 :             :         }
    6393                 :             : 
    6394                 :        2038 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    6395                 :        2038 :       fprintf (out, _("Table at Offset 0x%" PRIx64 ":\n\n"),
    6396                 :             :                (uint64_t) offset);
    6397                 :             : 
    6398         [ -  + ]:        2038 :       uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    6399                 :        2038 :       unsigned int offset_size = 4;
    6400         [ -  + ]:        2038 :       if (unlikely (unit_length == 0xffffffff))
    6401                 :             :         {
    6402         [ #  # ]:           0 :           if (unlikely (readp > dataend - 8))
    6403                 :           0 :             goto invalid_data;
    6404                 :             : 
    6405         [ #  # ]:           0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    6406                 :           0 :           offset_size = 8;
    6407                 :             :         }
    6408                 :        2038 :       fprintf (out, _(" Length:         %8" PRIu64 "\n"), unit_length);
    6409                 :             : 
    6410                 :             :       /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
    6411                 :             :          bytes to complete the header.  And this unit cannot go beyond
    6412                 :             :          the section data.  */
    6413         [ +  - ]:        2038 :       if (readp > dataend - 8
    6414         [ +  - ]:        2038 :           || unit_length < 8
    6415         [ -  + ]:        2038 :           || unit_length > (uint64_t) (dataend - readp))
    6416                 :           0 :         goto invalid_data;
    6417                 :             : 
    6418                 :        2038 :       const unsigned char *nexthdr = readp + unit_length;
    6419                 :             : 
    6420         [ -  + ]:        2038 :       uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    6421                 :        2038 :       fprintf (out, _(" DWARF version:  %8" PRIu16 "\n"), version);
    6422                 :             : 
    6423         [ -  + ]:        2038 :       if (version != 5)
    6424                 :             :         {
    6425                 :           0 :           error (0, 0, _("Unknown version"));
    6426                 :           0 :           goto next_table;
    6427                 :             :         }
    6428                 :             : 
    6429                 :        2038 :       uint8_t address_size = *readp++;
    6430                 :        2038 :       fprintf (out, _(" Address size:   %8" PRIu64 "\n"),
    6431                 :             :                (uint64_t) address_size);
    6432                 :             : 
    6433         [ -  + ]:        2038 :       if (address_size != 4 && address_size != 8)
    6434                 :             :         {
    6435                 :           0 :           error (0, 0, _("unsupported address size"));
    6436                 :           0 :           goto next_table;
    6437                 :             :         }
    6438                 :             : 
    6439                 :        2038 :       uint8_t segment_size = *readp++;
    6440                 :        2038 :       fprintf (out, _(" Segment size:   %8" PRIu64 "\n"),
    6441                 :             :                (uint64_t) segment_size);
    6442                 :             : 
    6443   [ -  +  -  - ]:        2038 :       if (segment_size != 0 && segment_size != 4 && segment_size != 8)
    6444                 :             :         {
    6445                 :           0 :           error (0, 0, _("unsupported segment size"));
    6446                 :           0 :           goto next_table;
    6447                 :             :         }
    6448                 :             : 
    6449         [ -  + ]:        2038 :       uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
    6450                 :        2038 :       fprintf (out, _(" Offset entries: %8" PRIu64 "\n"),
    6451                 :             :                (uint64_t) offset_entry_count);
    6452                 :             : 
    6453                 :             :       /* We need the CU that uses this unit to get the initial base address. */
    6454                 :        2038 :       Dwarf_Addr cu_base = 0;
    6455                 :        2038 :       struct Dwarf_CU *cu = NULL;
    6456         [ -  + ]:        2038 :       if (listptr_cu (&known_rnglistptr, &listptr_idx,
    6457                 :             :                       (Dwarf_Off) offset,
    6458                 :        2038 :                       (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
    6459                 :             :                       &cu_base, &cu)
    6460         [ #  # ]:           0 :           || split_dwarf_cu_base (dbg, &cu, &cu_base))
    6461                 :        2038 :         {
    6462                 :        2038 :           Dwarf_Die cudie;
    6463         [ -  + ]:        2038 :           if (dwarf_cu_die (cu, &cudie,
    6464                 :             :                             NULL, NULL, NULL, NULL,
    6465                 :             :                             NULL, NULL) == NULL)
    6466                 :           0 :             fprintf (out, _(" Unknown CU base: "));
    6467                 :             :           else
    6468                 :        2038 :             fprintf (out, _(" CU [%6" PRIx64 "] base: "),
    6469                 :             :                      dwarf_dieoffset (&cudie));
    6470                 :        2038 :           print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
    6471                 :        2038 :           fprintf (out, "\n");
    6472                 :             :         }
    6473                 :             :       else
    6474                 :           0 :         fprintf (out, _(" Not associated with a CU.\n"));
    6475                 :             : 
    6476                 :        2038 :       fprintf (out, "\n");
    6477                 :             : 
    6478                 :        2038 :       const unsigned char *offset_array_start = readp;
    6479         [ +  + ]:        2038 :       if (offset_entry_count > 0)
    6480                 :             :         {
    6481                 :           2 :           uint64_t max_entries = (unit_length - 8) / offset_size;
    6482         [ -  + ]:           2 :           if (offset_entry_count > max_entries)
    6483                 :             :             {
    6484                 :           0 :               error (0, 0,
    6485                 :           0 :                      _("too many offset entries for unit length"));
    6486                 :           0 :               offset_entry_count = max_entries;
    6487                 :             :             }
    6488                 :             : 
    6489                 :           2 :           fprintf (out, _("  Offsets starting at 0x%" PRIx64 ":\n"),
    6490                 :             :                    (uint64_t) (offset_array_start
    6491                 :           2 :                                - (unsigned char *) data->d_buf));
    6492         [ +  + ]:           6 :           for (uint32_t idx = 0; idx < offset_entry_count; idx++)
    6493                 :             :             {
    6494                 :           4 :               fprintf (out, "   [%6" PRIu32 "] ", idx);
    6495         [ +  - ]:           4 :               if (offset_size == 4)
    6496                 :             :                 {
    6497         [ -  + ]:           4 :                   uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
    6498                 :           4 :                   fprintf (out, "0x%" PRIx32 "\n", off);
    6499                 :             :                 }
    6500                 :             :               else
    6501                 :             :                 {
    6502         [ #  # ]:           0 :                   uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
    6503                 :           0 :                   fprintf (out, "0x%" PRIx64 "\n", off);
    6504                 :             :                 }
    6505                 :             :             }
    6506                 :           2 :           fprintf (out, "\n");
    6507                 :             :         }
    6508                 :             : 
    6509                 :        2038 :       Dwarf_Addr base = cu_base;
    6510                 :        2038 :       bool start_of_list = true;
    6511         [ +  + ]:      150652 :       while (readp < nexthdr)
    6512                 :             :         {
    6513                 :      148614 :           uint8_t kind = *readp++;
    6514                 :      148614 :           uint64_t op1, op2;
    6515                 :             : 
    6516                 :             :           /* Skip padding.  */
    6517         [ -  + ]:      148614 :           if (start_of_list && kind == DW_RLE_end_of_list)
    6518                 :           0 :             continue;
    6519                 :             : 
    6520         [ +  + ]:      148614 :           if (start_of_list)
    6521                 :             :             {
    6522                 :       30156 :               base = cu_base;
    6523                 :       30156 :               fprintf (out, "  Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
    6524                 :       30156 :                        (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
    6525                 :       30156 :                        (uint64_t) (readp - offset_array_start - 1));
    6526                 :       30156 :               start_of_list = false;
    6527                 :             :             }
    6528                 :             : 
    6529                 :      148614 :           fprintf (out, "    %s", dwarf_range_list_encoding_name (kind));
    6530   [ +  -  -  -  :      148614 :           switch (kind)
             +  +  -  +  
                      - ]
    6531                 :             :             {
    6532                 :       30156 :             case DW_RLE_end_of_list:
    6533                 :       30156 :               start_of_list = true;
    6534                 :       30156 :               fprintf (out, "\n\n");
    6535                 :       30156 :               break;
    6536                 :             : 
    6537                 :           0 :             case DW_RLE_base_addressx:
    6538         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6539                 :             :                 {
    6540                 :           0 :                 invalid_range:
    6541                 :           0 :                   error (0, 0, _("invalid range list data"));
    6542                 :           0 :                   goto next_table;
    6543                 :             :                 }
    6544                 :           0 :               get_uleb128 (op1, readp, nexthdr);
    6545                 :           0 :               fprintf (out, " %" PRIx64 "\n", op1);
    6546         [ #  # ]:           0 :               if (! print_unresolved_addresses)
    6547                 :             :                 {
    6548                 :           0 :                   Dwarf_Addr addr;
    6549         [ #  # ]:           0 :                   if (get_indexed_addr (cu, op1, &addr) != 0)
    6550                 :           0 :                     fprintf (out, "      ???\n");
    6551                 :             :                   else
    6552                 :             :                     {
    6553                 :           0 :                       fprintf (out, "      ");
    6554                 :           0 :                       print_dwarf_addr (dwflmod, address_size, addr,
    6555                 :             :                                         addr, out);
    6556                 :           0 :                       fprintf (out, "\n");
    6557                 :             :                     }
    6558                 :             :                 }
    6559                 :             :               break;
    6560                 :             : 
    6561                 :           0 :             case DW_RLE_startx_endx:
    6562         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6563                 :           0 :                 goto invalid_range;
    6564                 :           0 :               get_uleb128 (op1, readp, nexthdr);
    6565         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6566                 :           0 :                 goto invalid_range;
    6567                 :           0 :               get_uleb128 (op2, readp, nexthdr);
    6568                 :           0 :               fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    6569         [ #  # ]:           0 :               if (! print_unresolved_addresses)
    6570                 :             :                 {
    6571                 :           0 :                   Dwarf_Addr addr1;
    6572                 :           0 :                   Dwarf_Addr addr2;
    6573         [ #  # ]:           0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0
    6574         [ #  # ]:           0 :                       || get_indexed_addr (cu, op2, &addr2) != 0)
    6575                 :             :                     {
    6576                 :           0 :                       fprintf (out, "      ???..\n");
    6577                 :           0 :                       fprintf (out, "      ???\n");
    6578                 :             :                     }
    6579                 :             :                   else
    6580                 :             :                     {
    6581                 :           0 :                       fprintf (out, "      ");
    6582                 :           0 :                       print_dwarf_addr (dwflmod, address_size,
    6583                 :             :                                         addr1, addr1, out);
    6584                 :           0 :                       fprintf (out, "..\n      ");
    6585                 :           0 :                       print_dwarf_addr (dwflmod, address_size,
    6586                 :             :                                         addr2 - 1, addr2, out);
    6587                 :           0 :                       fprintf (out, "\n");
    6588                 :             :                     }
    6589                 :             :                 }
    6590                 :             :               break;
    6591                 :             : 
    6592                 :           0 :             case DW_RLE_startx_length:
    6593         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6594                 :           0 :                 goto invalid_range;
    6595                 :           0 :               get_uleb128 (op1, readp, nexthdr);
    6596         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
    6597                 :           0 :                 goto invalid_range;
    6598                 :           0 :               get_uleb128 (op2, readp, nexthdr);
    6599                 :           0 :               fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    6600         [ #  # ]:           0 :               if (! print_unresolved_addresses)
    6601                 :             :                 {
    6602                 :           0 :                   Dwarf_Addr addr1;
    6603                 :           0 :                   Dwarf_Addr addr2;
    6604         [ #  # ]:           0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0)
    6605                 :             :                     {
    6606                 :           0 :                       fprintf (out, "      ???..\n");
    6607                 :           0 :                       fprintf (out, "      ???\n");
    6608                 :             :                     }
    6609                 :             :                   else
    6610                 :             :                     {
    6611                 :           0 :                       addr2 = addr1 + op2;
    6612                 :           0 :                       fprintf (out, "      ");
    6613                 :           0 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
    6614                 :           0 :                       fprintf (out, "..\n      ");
    6615                 :           0 :                       print_dwarf_addr (dwflmod, address_size,
    6616                 :             :                                         addr2 - 1, addr2, out);
    6617                 :           0 :                       fprintf (out, "\n");
    6618                 :             :                     }
    6619                 :             :                 }
    6620                 :             :               break;
    6621                 :             : 
    6622                 :       94106 :             case DW_RLE_offset_pair:
    6623         [ -  + ]:       94106 :               if ((uint64_t) (nexthdr - readp) < 1)
    6624                 :           0 :                 goto invalid_range;
    6625                 :       94106 :               get_uleb128 (op1, readp, nexthdr);
    6626         [ -  + ]:       94106 :               if ((uint64_t) (nexthdr - readp) < 1)
    6627                 :           0 :                 goto invalid_range;
    6628                 :       94106 :               get_uleb128 (op2, readp, nexthdr);
    6629                 :       94106 :               fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
    6630         [ +  - ]:       94106 :               if (! print_unresolved_addresses)
    6631                 :             :                 {
    6632                 :       94106 :                   op1 += base;
    6633                 :       94106 :                   op2 += base;
    6634                 :       94106 :                   fprintf (out, "      ");
    6635                 :       94106 :                   print_dwarf_addr (dwflmod, address_size, op1, op1, out);
    6636                 :       94106 :                   fprintf (out, "..\n      ");
    6637                 :       94106 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
    6638                 :       94106 :                   fprintf (out, "\n");
    6639                 :             :                 }
    6640                 :             :               break;
    6641                 :             : 
    6642                 :       16984 :             case DW_RLE_base_address:
    6643         [ -  + ]:       16984 :               if (address_size == 4)
    6644                 :             :                 {
    6645         [ #  # ]:           0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    6646                 :           0 :                     goto invalid_range;
    6647         [ #  # ]:           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    6648                 :             :                 }
    6649                 :             :               else
    6650                 :             :                 {
    6651         [ -  + ]:       16984 :                   if ((uint64_t) (nexthdr - readp) < 8)
    6652                 :           0 :                     goto invalid_range;
    6653         [ -  + ]:       16984 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    6654                 :             :                 }
    6655                 :       16984 :               base = op1;
    6656                 :       16984 :               fprintf (out, " 0x%" PRIx64 "\n", base);
    6657         [ +  - ]:       16984 :               if (! print_unresolved_addresses)
    6658                 :             :                 {
    6659                 :       16984 :                   fprintf (out, "      ");
    6660                 :       16984 :                   print_dwarf_addr (dwflmod, address_size, base, base, out);
    6661                 :       16984 :                   fprintf (out, "\n");
    6662                 :             :                 }
    6663                 :             :               break;
    6664                 :             : 
    6665                 :           0 :             case DW_RLE_start_end:
    6666         [ #  # ]:           0 :               if (address_size == 4)
    6667                 :             :                 {
    6668         [ #  # ]:           0 :                   if ((uint64_t) (nexthdr - readp) < 8)
    6669                 :           0 :                     goto invalid_range;
    6670         [ #  # ]:           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    6671         [ #  # ]:           0 :                   op2 = read_4ubyte_unaligned_inc (dbg, readp);
    6672                 :             :                 }
    6673                 :             :               else
    6674                 :             :                 {
    6675         [ #  # ]:           0 :                   if ((uint64_t) (nexthdr - readp) < 16)
    6676                 :           0 :                     goto invalid_range;
    6677         [ #  # ]:           0 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    6678         [ #  # ]:           0 :                   op2 = read_8ubyte_unaligned_inc (dbg, readp);
    6679                 :             :                 }
    6680                 :           0 :               fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
    6681         [ #  # ]:           0 :               if (! print_unresolved_addresses)
    6682                 :             :                 {
    6683                 :           0 :                   fprintf (out, "      ");
    6684                 :           0 :                   print_dwarf_addr (dwflmod, address_size, op1, op1, out);
    6685                 :           0 :                   fprintf (out, "..\n      ");
    6686                 :           0 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
    6687                 :           0 :                   fprintf (out, "\n");
    6688                 :             :                 }
    6689                 :             :               break;
    6690                 :             : 
    6691                 :        7368 :             case DW_RLE_start_length:
    6692         [ -  + ]:        7368 :               if (address_size == 4)
    6693                 :             :                 {
    6694         [ #  # ]:           0 :                   if ((uint64_t) (nexthdr - readp) < 4)
    6695                 :           0 :                     goto invalid_range;
    6696         [ #  # ]:           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
    6697                 :             :                 }
    6698                 :             :               else
    6699                 :             :                 {
    6700         [ -  + ]:        7368 :                   if ((uint64_t) (nexthdr - readp) < 8)
    6701                 :           0 :                     goto invalid_range;
    6702         [ -  + ]:        7368 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
    6703                 :             :                 }
    6704         [ -  + ]:        7368 :               if ((uint64_t) (nexthdr - readp) < 1)
    6705                 :           0 :                 goto invalid_range;
    6706                 :        7368 :               get_uleb128 (op2, readp, nexthdr);
    6707                 :        7368 :               fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
    6708         [ +  + ]:        7368 :               if (! print_unresolved_addresses)
    6709                 :             :                 {
    6710                 :        7366 :                   op2 = op1 + op2;
    6711                 :        7366 :                   fprintf (out, "      ");
    6712                 :        7366 :                   print_dwarf_addr (dwflmod, address_size, op1, op1, out);
    6713                 :        7366 :                   fprintf (out, "..\n      ");
    6714                 :        7366 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
    6715                 :        7366 :                   fprintf (out, "\n");
    6716                 :             :                 }
    6717                 :             :               break;
    6718                 :             : 
    6719                 :           0 :             default:
    6720                 :           0 :               goto invalid_range;
    6721                 :             :             }
    6722                 :             :         }
    6723                 :             : 
    6724                 :        2038 :     next_table:
    6725         [ -  + ]:        2038 :       if (readp != nexthdr)
    6726                 :             :         {
    6727                 :           0 :           size_t padding = nexthdr - readp;
    6728                 :           0 :           fprintf (out, _("   %zu padding bytes\n\n"), padding);
    6729                 :           0 :           readp = nexthdr;
    6730                 :             :         }
    6731                 :             :     }
    6732                 :             : }
    6733                 :             : 
    6734                 :             : /* Print content of DWARF .debug_ranges section.  */
    6735                 :             : static void
    6736                 :          32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
    6737                 :             :                             Ebl *ebl, GElf_Ehdr *ehdr,
    6738                 :             :                             Elf_Scn *scn, GElf_Shdr *shdr,
    6739                 :             :                             Dwarf *dbg, FILE *out)
    6740                 :             : {
    6741                 :          32 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
    6742         [ -  + ]:          32 :   if (data == NULL)
    6743                 :           0 :     return;
    6744                 :             : 
    6745                 :          32 :   fprintf (out, _("\
    6746                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    6747                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    6748                 :          32 :            (uint64_t) shdr->sh_offset);
    6749                 :             : 
    6750                 :          32 :   sort_listptr (&known_rangelistptr, "rangelistptr");
    6751                 :          32 :   size_t listptr_idx = 0;
    6752                 :             : 
    6753         [ +  + ]:          32 :   uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    6754                 :             : 
    6755                 :          32 :   bool first = true;
    6756                 :          32 :   Dwarf_Addr base = 0;
    6757                 :          32 :   unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
    6758                 :          32 :   unsigned char *readp = data->d_buf;
    6759                 :          32 :   Dwarf_CU *last_cu = NULL;
    6760         [ +  + ]:         184 :   while (readp < endp)
    6761                 :             :     {
    6762                 :         152 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    6763                 :         152 :       Dwarf_CU *cu = last_cu;
    6764                 :             : 
    6765   [ +  +  -  + ]:         152 :       if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
    6766                 :             :                                       &address_size, NULL, &base, &cu,
    6767                 :             :                                       offset, &readp, endp, NULL))
    6768                 :           0 :         continue;
    6769                 :             : 
    6770         [ +  + ]:         152 :       if (last_cu != cu)
    6771                 :             :         {
    6772                 :          58 :           Dwarf_Die cudie;
    6773         [ -  + ]:          58 :           if (dwarf_cu_die (cu, &cudie,
    6774                 :             :                             NULL, NULL, NULL, NULL,
    6775                 :             :                             NULL, NULL) == NULL)
    6776                 :           0 :             fprintf (out, _("\n Unknown CU base: "));
    6777                 :             :           else
    6778                 :          58 :             fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
    6779                 :             :                      dwarf_dieoffset (&cudie));
    6780                 :          58 :           print_dwarf_addr (dwflmod, address_size, base, base, out);
    6781                 :          58 :           fprintf (out, "\n");
    6782                 :             :         }
    6783                 :         152 :       last_cu = cu;
    6784                 :             : 
    6785         [ -  + ]:         152 :       if (unlikely (data->d_size - offset < (size_t) address_size * 2))
    6786                 :             :         {
    6787                 :           0 :           fprintf (out, _(" [%6tx]  <INVALID DATA>\n"), offset);
    6788                 :           0 :           break;
    6789                 :             :         }
    6790                 :             : 
    6791                 :         152 :       Dwarf_Addr begin;
    6792                 :         152 :       Dwarf_Addr end;
    6793         [ +  + ]:         152 :       if (address_size == 8)
    6794                 :             :         {
    6795         [ -  + ]:         120 :           begin = read_8ubyte_unaligned_inc (dbg, readp);
    6796         [ -  + ]:         120 :           end = read_8ubyte_unaligned_inc (dbg, readp);
    6797                 :             :         }
    6798                 :             :       else
    6799                 :             :         {
    6800         [ -  + ]:          32 :           begin = read_4ubyte_unaligned_inc (dbg, readp);
    6801         [ -  + ]:          32 :           end = read_4ubyte_unaligned_inc (dbg, readp);
    6802         [ +  - ]:          32 :           if (begin == (Dwarf_Addr) (uint32_t) -1)
    6803                 :             :             begin = (Dwarf_Addr) -1l;
    6804                 :             :         }
    6805                 :             : 
    6806         [ -  + ]:         152 :       if (begin == (Dwarf_Addr) -1l) /* Base address entry.  */
    6807                 :             :         {
    6808         [ #  # ]:           0 :           if (first)
    6809                 :           0 :             fprintf (out, " [%6tx] ", offset);
    6810                 :             :           else
    6811                 :           0 :             fprintf (out, "          ");
    6812                 :           0 :           fputs (_("base address\n"), out);
    6813                 :           0 :           fprintf (out, "          ");
    6814                 :           0 :           print_dwarf_addr (dwflmod, address_size, end, end, out);
    6815                 :           0 :           fprintf (out, "\n");
    6816                 :           0 :           base = end;
    6817                 :           0 :           first = false;
    6818                 :             :         }
    6819         [ +  + ]:         152 :       else if (begin == 0 && end == 0) /* End of list entry.  */
    6820                 :             :         {
    6821         [ -  + ]:          60 :           if (first)
    6822                 :           0 :             fprintf (out, _(" [%6tx] empty list\n"), offset);
    6823                 :             :           first = true;
    6824                 :             :         }
    6825                 :             :       else
    6826                 :             :         {
    6827                 :             :           /* We have an address range entry.  */
    6828         [ +  + ]:          92 :           if (first)            /* First address range entry in a list.  */
    6829                 :          60 :             fprintf (out, " [%6tx] ", offset);
    6830                 :             :           else
    6831                 :          32 :             fprintf (out, "          ");
    6832                 :             : 
    6833                 :          92 :           fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
    6834         [ +  + ]:          92 :           if (! print_unresolved_addresses)
    6835                 :             :             {
    6836                 :          72 :               fprintf (out, "          ");
    6837                 :          72 :               print_dwarf_addr (dwflmod, address_size, base + begin,
    6838                 :             :                                 base + begin, out);
    6839                 :          72 :               fprintf (out, "..\n          ");
    6840                 :          72 :               print_dwarf_addr (dwflmod, address_size,
    6841                 :             :                                 base + end - 1, base + end, out);
    6842                 :          72 :               fprintf (out, "\n");
    6843                 :             :             }
    6844                 :             : 
    6845                 :             :           first = false;
    6846                 :             :         }
    6847                 :             :     }
    6848                 :             : }
    6849                 :             : 
    6850                 :             : #define REGNAMESZ 16
    6851                 :             : static const char *
    6852                 :       39562 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
    6853                 :             :                char name[REGNAMESZ], int *bits, int *type)
    6854                 :             : {
    6855                 :       39562 :   const char *set;
    6856                 :       39562 :   const char *pfx;
    6857                 :       39562 :   int ignore;
    6858   [ +  +  +  + ]:      115294 :   ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
    6859                 :             :                                  bits ?: &ignore, type ?: &ignore);
    6860         [ -  + ]:       39562 :   if (n <= 0)
    6861                 :             :     {
    6862         [ #  # ]:           0 :       if (loc != NULL)
    6863                 :           0 :         snprintf (name, REGNAMESZ, "reg%u", loc->regno);
    6864                 :             :       else
    6865                 :           0 :         snprintf (name, REGNAMESZ, "??? 0x%x", regno);
    6866         [ #  # ]:           0 :       if (bits != NULL)
    6867         [ #  # ]:           0 :         *bits = loc != NULL ? loc->bits : 0;
    6868         [ #  # ]:           0 :       if (type != NULL)
    6869                 :           0 :         *type = DW_ATE_unsigned;
    6870                 :           0 :       set = "??? unrecognized";
    6871                 :             :     }
    6872                 :             :   else
    6873                 :             :     {
    6874   [ +  +  -  + ]:       39562 :       if (bits != NULL && *bits <= 0)
    6875         [ #  # ]:           0 :         *bits = loc != NULL ? loc->bits : 0;
    6876   [ +  +  -  + ]:       39562 :       if (type != NULL && *type == DW_ATE_void)
    6877                 :           0 :         *type = DW_ATE_unsigned;
    6878                 :             : 
    6879                 :             :     }
    6880                 :       39562 :   return set;
    6881                 :             : }
    6882                 :             : 
    6883                 :             : static const unsigned char *
    6884                 :         280 : read_encoded (unsigned int encoding, const unsigned char *readp,
    6885                 :             :               const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
    6886                 :             : {
    6887         [ -  + ]:         280 :   if ((encoding & 0xf) == DW_EH_PE_absptr)
    6888                 :           0 :     encoding = gelf_getclass (dbg->elf) == ELFCLASS32
    6889         [ #  # ]:           0 :       ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
    6890                 :             : 
    6891   [ -  -  -  +  :         280 :   switch (encoding & 0xf)
             -  -  +  -  
                      - ]
    6892                 :             :     {
    6893                 :           0 :     case DW_EH_PE_uleb128:
    6894         [ #  # ]:           0 :       if (readp >= endp)
    6895                 :           0 :         goto invalid;
    6896                 :           0 :       get_uleb128 (*res, readp, endp);
    6897                 :           0 :       break;
    6898                 :           0 :     case DW_EH_PE_sleb128:
    6899         [ #  # ]:           0 :       if (readp >= endp)
    6900                 :           0 :         goto invalid;
    6901                 :           0 :       get_sleb128 (*res, readp, endp);
    6902                 :           0 :       break;
    6903                 :           0 :     case DW_EH_PE_udata2:
    6904         [ #  # ]:           0 :       if (readp + 2 > endp)
    6905                 :           0 :         goto invalid;
    6906         [ #  # ]:           0 :       *res = read_2ubyte_unaligned_inc (dbg, readp);
    6907                 :           0 :       break;
    6908                 :         140 :     case DW_EH_PE_udata4:
    6909         [ -  + ]:         140 :       if (readp + 4 > endp)
    6910                 :           0 :         goto invalid;
    6911         [ -  + ]:         140 :       *res = read_4ubyte_unaligned_inc (dbg, readp);
    6912                 :         140 :       break;
    6913                 :           0 :     case DW_EH_PE_udata8:
    6914         [ #  # ]:           0 :       if (readp + 8 > endp)
    6915                 :           0 :         goto invalid;
    6916         [ #  # ]:           0 :       *res = read_8ubyte_unaligned_inc (dbg, readp);
    6917                 :           0 :       break;
    6918                 :           0 :     case DW_EH_PE_sdata2:
    6919         [ #  # ]:           0 :       if (readp + 2 > endp)
    6920                 :           0 :         goto invalid;
    6921         [ #  # ]:           0 :       *res = read_2sbyte_unaligned_inc (dbg, readp);
    6922                 :           0 :       break;
    6923                 :         140 :     case DW_EH_PE_sdata4:
    6924         [ -  + ]:         140 :       if (readp + 4 > endp)
    6925                 :           0 :         goto invalid;
    6926         [ -  + ]:         140 :       *res = read_4sbyte_unaligned_inc (dbg, readp);
    6927                 :         140 :       break;
    6928                 :           0 :     case DW_EH_PE_sdata8:
    6929         [ #  # ]:           0 :       if (readp + 8 > endp)
    6930                 :           0 :         goto invalid;
    6931         [ #  # ]:           0 :       *res = read_8sbyte_unaligned_inc (dbg, readp);
    6932                 :           0 :       break;
    6933                 :             :     default:
    6934                 :           0 :     invalid:
    6935                 :           0 :       error (1, 0,
    6936                 :           0 :              _("invalid encoding"));
    6937                 :             :     }
    6938                 :             : 
    6939                 :         280 :   return readp;
    6940                 :             : }
    6941                 :             : 
    6942                 :             : static const char *
    6943                 :       37866 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
    6944                 :             : {
    6945                 :       37866 :   register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
    6946                 :             : 
    6947                 :       37866 :   return regnamebuf;
    6948                 :             : }
    6949                 :             : 
    6950                 :             : static void
    6951                 :       24508 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
    6952                 :             :                    Dwarf_Word vma_base, unsigned int code_align,
    6953                 :             :                    int data_align,
    6954                 :             :                    unsigned int version, unsigned int ptr_size,
    6955                 :             :                    unsigned int encoding,
    6956                 :             :                    Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg,
    6957                 :             :                    FILE *out)
    6958                 :             : {
    6959                 :       24508 :   char regnamebuf[REGNAMESZ];
    6960                 :             : 
    6961                 :       24508 :   fputs ("\n   Program:\n", out);
    6962                 :       24508 :   Dwarf_Word pc = vma_base;
    6963         [ +  + ]:      356498 :   while (readp < endp)
    6964                 :             :     {
    6965                 :      331990 :       unsigned int opcode = *readp++;
    6966                 :             : 
    6967         [ +  + ]:      331990 :       if (opcode < DW_CFA_advance_loc)
    6968                 :             :         /* Extended opcode.  */
    6969   [ +  -  +  +  :      197870 :         switch (opcode)
          +  +  -  +  -  
          -  +  +  +  +  
          +  +  -  +  -  
          -  -  -  -  -  
                +  -  - ]
    6970                 :             :           {
    6971                 :       67932 :             uint64_t op1;
    6972                 :       67932 :             int64_t sop1;
    6973                 :       67932 :             uint64_t op2;
    6974                 :       67932 :             int64_t sop2;
    6975                 :             : 
    6976                 :       67932 :           case DW_CFA_nop:
    6977                 :       67932 :             fputs ("     nop\n", out);
    6978                 :      265802 :             break;
    6979                 :           0 :           case DW_CFA_set_loc:
    6980         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    6981                 :           0 :               goto invalid;
    6982                 :           0 :             readp = read_encoded (encoding, readp, endp, &op1, dbg);
    6983                 :           0 :             fprintf (out, "     set_loc %#" PRIx64 " to %#" PRIx64 "\n",
    6984                 :           0 :                      op1, pc = vma_base + op1);
    6985                 :           0 :             break;
    6986                 :        6802 :           case DW_CFA_advance_loc1:
    6987         [ -  + ]:        6802 :             if ((uint64_t) (endp - readp) < 1)
    6988                 :           0 :               goto invalid;
    6989                 :       13604 :             fprintf (out, "     advance_loc1 %u to %#" PRIx64 "\n",
    6990                 :        6802 :                      *readp, pc += *readp * code_align);
    6991                 :        6802 :             ++readp;
    6992                 :        6802 :             break;
    6993                 :        2884 :           case DW_CFA_advance_loc2:
    6994         [ -  + ]:        2884 :             if ((uint64_t) (endp - readp) < 2)
    6995                 :           0 :               goto invalid;
    6996         [ -  + ]:        2884 :             op1 = read_2ubyte_unaligned_inc (dbg, readp);
    6997                 :        2884 :             fprintf (out, "     advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
    6998                 :        2884 :                      op1, pc += op1 * code_align);
    6999                 :        2884 :             break;
    7000                 :          36 :           case DW_CFA_advance_loc4:
    7001         [ -  + ]:          36 :             if ((uint64_t) (endp - readp) < 4)
    7002                 :           0 :               goto invalid;
    7003         [ +  - ]:          36 :             op1 = read_4ubyte_unaligned_inc (dbg, readp);
    7004                 :          36 :             fprintf (out, "     advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
    7005                 :          36 :                      op1, pc += op1 * code_align);
    7006                 :          36 :             break;
    7007                 :           8 :           case DW_CFA_offset_extended:
    7008         [ -  + ]:           8 :             if ((uint64_t) (endp - readp) < 1)
    7009                 :           0 :               goto invalid;
    7010                 :           8 :             get_uleb128 (op1, readp, endp);
    7011         [ -  + ]:           8 :             if ((uint64_t) (endp - readp) < 1)
    7012                 :           0 :               goto invalid;
    7013                 :           8 :             get_uleb128 (op2, readp, endp);
    7014                 :           8 :             fprintf (out, "     offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
    7015                 :             :                      "\n",
    7016                 :             :                      op1, regname (ebl, op1, regnamebuf), op2 * data_align);
    7017                 :           8 :             break;
    7018                 :           0 :           case DW_CFA_restore_extended:
    7019         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7020                 :           0 :               goto invalid;
    7021                 :           0 :             get_uleb128 (op1, readp, endp);
    7022                 :           0 :             fprintf (out, "     restore_extended r%" PRIu64 " (%s)\n",
    7023                 :             :                      op1, regname (ebl, op1, regnamebuf));
    7024                 :           0 :             break;
    7025                 :         110 :           case DW_CFA_undefined:
    7026         [ -  + ]:         110 :             if ((uint64_t) (endp - readp) < 1)
    7027                 :           0 :               goto invalid;
    7028                 :         110 :             get_uleb128 (op1, readp, endp);
    7029                 :         110 :             fprintf (out, "     undefined r%" PRIu64 " (%s)\n", op1,
    7030                 :             :                      regname (ebl, op1, regnamebuf));
    7031                 :         110 :             break;
    7032                 :           0 :           case DW_CFA_same_value:
    7033         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7034                 :           0 :               goto invalid;
    7035                 :           0 :             get_uleb128 (op1, readp, endp);
    7036                 :           0 :             fprintf (out, "     same_value r%" PRIu64 " (%s)\n", op1,
    7037                 :             :                      regname (ebl, op1, regnamebuf));
    7038                 :           0 :             break;
    7039                 :           0 :           case DW_CFA_register:
    7040         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7041                 :           0 :               goto invalid;
    7042                 :           0 :             get_uleb128 (op1, readp, endp);
    7043         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7044                 :           0 :               goto invalid;
    7045                 :           0 :             get_uleb128 (op2, readp, endp);
    7046                 :           0 :             fprintf (out,
    7047                 :             :                      "     register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
    7048                 :             :                      op1, regname (ebl, op1, regnamebuf), op2,
    7049                 :             :                      regname (ebl, op2, regnamebuf));
    7050                 :           0 :             break;
    7051                 :       11882 :           case DW_CFA_remember_state:
    7052                 :       11882 :             fputs ("     remember_state\n", out);
    7053                 :       11882 :             break;
    7054                 :       11882 :           case DW_CFA_restore_state:
    7055                 :       11882 :             fputs ("     restore_state\n", out);
    7056                 :       11882 :             break;
    7057                 :         418 :           case DW_CFA_def_cfa:
    7058         [ -  + ]:         418 :             if ((uint64_t) (endp - readp) < 1)
    7059                 :           0 :               goto invalid;
    7060                 :         418 :             get_uleb128 (op1, readp, endp);
    7061         [ -  + ]:         418 :             if ((uint64_t) (endp - readp) < 1)
    7062                 :           0 :               goto invalid;
    7063                 :         418 :             get_uleb128 (op2, readp, endp);
    7064                 :         418 :             fprintf (out,
    7065                 :             :                      "     def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
    7066                 :             :                      op1, regname (ebl, op1, regnamebuf), op2);
    7067                 :         418 :             break;
    7068                 :         126 :           case DW_CFA_def_cfa_register:
    7069         [ -  + ]:         126 :             if ((uint64_t) (endp - readp) < 1)
    7070                 :           0 :               goto invalid;
    7071                 :         126 :             get_uleb128 (op1, readp, endp);
    7072                 :         126 :             fprintf (out, "     def_cfa_register r%" PRIu64 " (%s)\n",
    7073                 :             :                      op1, regname (ebl, op1, regnamebuf));
    7074                 :         126 :             break;
    7075                 :       95640 :           case DW_CFA_def_cfa_offset:
    7076         [ -  + ]:       95640 :             if ((uint64_t) (endp - readp) < 1)
    7077                 :           0 :               goto invalid;
    7078                 :       95640 :             get_uleb128 (op1, readp, endp);
    7079                 :       95640 :             fprintf (out, "     def_cfa_offset %" PRIu64 "\n", op1);
    7080                 :       95640 :             break;
    7081                 :         118 :           case DW_CFA_def_cfa_expression:
    7082         [ -  + ]:         118 :             if ((uint64_t) (endp - readp) < 1)
    7083                 :           0 :               goto invalid;
    7084                 :         118 :             get_uleb128 (op1, readp, endp);     /* Length of DW_FORM_block.  */
    7085                 :         118 :             fprintf (out, "     def_cfa_expression %" PRIu64 "\n", op1);
    7086         [ -  + ]:         118 :             if ((uint64_t) (endp - readp) < op1)
    7087                 :             :               {
    7088                 :           0 :             invalid:
    7089                 :           0 :                 fputs (_("         <INVALID DATA>\n"), out);
    7090                 :           0 :                 return;
    7091                 :             :               }
    7092                 :         118 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
    7093                 :             :                        op1, readp, out);
    7094                 :         118 :             readp += op1;
    7095                 :         118 :             break;
    7096                 :           0 :           case DW_CFA_expression:
    7097         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7098                 :           0 :               goto invalid;
    7099                 :           0 :             get_uleb128 (op1, readp, endp);
    7100         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7101                 :           0 :               goto invalid;
    7102                 :           0 :             get_uleb128 (op2, readp, endp);     /* Length of DW_FORM_block.  */
    7103                 :           0 :             fprintf (out, "     expression r%" PRIu64 " (%s) \n",
    7104                 :             :                      op1, regname (ebl, op1, regnamebuf));
    7105         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < op2)
    7106                 :           0 :               goto invalid;
    7107                 :           0 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
    7108                 :             :                        op2, readp, out);
    7109                 :           0 :             readp += op2;
    7110                 :           0 :             break;
    7111                 :          24 :           case DW_CFA_offset_extended_sf:
    7112         [ -  + ]:          24 :             if ((uint64_t) (endp - readp) < 1)
    7113                 :           0 :               goto invalid;
    7114                 :          24 :             get_uleb128 (op1, readp, endp);
    7115         [ -  + ]:          24 :             if ((uint64_t) (endp - readp) < 1)
    7116                 :           0 :               goto invalid;
    7117                 :          24 :             get_sleb128 (sop2, readp, endp);
    7118                 :          24 :             fprintf (out, "     offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
    7119                 :             :                      PRId64 "\n",
    7120                 :             :                      op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
    7121                 :          24 :             break;
    7122                 :           0 :           case DW_CFA_def_cfa_sf:
    7123         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7124                 :           0 :               goto invalid;
    7125                 :           0 :             get_uleb128 (op1, readp, endp);
    7126         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7127                 :           0 :               goto invalid;
    7128                 :           0 :             get_sleb128 (sop2, readp, endp);
    7129                 :           0 :             fprintf (out,
    7130                 :             :                      "     def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
    7131                 :             :                      op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
    7132                 :           0 :             break;
    7133                 :           0 :           case DW_CFA_def_cfa_offset_sf:
    7134         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7135                 :           0 :               goto invalid;
    7136                 :           0 :             get_sleb128 (sop1, readp, endp);
    7137                 :           0 :             fprintf (out,
    7138                 :             :                      "     def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
    7139                 :           0 :             break;
    7140                 :           0 :           case DW_CFA_val_offset:
    7141         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7142                 :           0 :               goto invalid;
    7143                 :           0 :             get_uleb128 (op1, readp, endp);
    7144         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7145                 :           0 :               goto invalid;
    7146                 :           0 :             get_uleb128 (op2, readp, endp);
    7147                 :           0 :             fprintf (out, "     val_offset %" PRIu64 " at offset %" PRIu64 "\n",
    7148                 :             :                      op1, op2 * data_align);
    7149                 :           0 :             break;
    7150                 :           0 :           case DW_CFA_val_offset_sf:
    7151         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7152                 :           0 :               goto invalid;
    7153                 :           0 :             get_uleb128 (op1, readp, endp);
    7154         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7155                 :           0 :               goto invalid;
    7156                 :           0 :             get_sleb128 (sop2, readp, endp);
    7157                 :           0 :             fprintf (out,
    7158                 :             :                      "     val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
    7159                 :             :                      op1, sop2 * data_align);
    7160                 :           0 :             break;
    7161                 :           0 :           case DW_CFA_val_expression:
    7162         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7163                 :           0 :               goto invalid;
    7164                 :           0 :             get_uleb128 (op1, readp, endp);
    7165         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7166                 :           0 :               goto invalid;
    7167                 :           0 :             get_uleb128 (op2, readp, endp);     /* Length of DW_FORM_block.  */
    7168                 :           0 :             fprintf (out, "     val_expression r%" PRIu64 " (%s)\n",
    7169                 :             :                      op1, regname (ebl, op1, regnamebuf));
    7170         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < op2)
    7171                 :           0 :               goto invalid;
    7172                 :           0 :             print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
    7173                 :             :                        NULL, op2, readp, out);
    7174                 :           0 :             readp += op2;
    7175                 :           0 :             break;
    7176                 :           0 :           case DW_CFA_MIPS_advance_loc8:
    7177         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 8)
    7178                 :           0 :               goto invalid;
    7179         [ #  # ]:           0 :             op1 = read_8ubyte_unaligned_inc (dbg, readp);
    7180                 :           0 :             fprintf (out,
    7181                 :             :                      "     MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
    7182                 :           0 :                      op1, pc += op1 * code_align);
    7183                 :           0 :             break;
    7184                 :           8 :           case DW_CFA_GNU_window_save:  /* DW_CFA_AARCH64_negate_ra_state  */
    7185         [ +  - ]:           8 :             if (ehdr->e_machine == EM_AARCH64)
    7186                 :           8 :               fputs ("     AARCH64_negate_ra_state\n", out);
    7187                 :             :             else
    7188                 :           0 :               fputs ("     GNU_window_save\n", out);
    7189                 :             :             break;
    7190                 :           0 :           case DW_CFA_GNU_args_size:
    7191         [ #  # ]:           0 :             if ((uint64_t) (endp - readp) < 1)
    7192                 :           0 :               goto invalid;
    7193                 :           0 :             get_uleb128 (op1, readp, endp);
    7194                 :           0 :             fprintf (out, "     args_size %" PRIu64 "\n", op1);
    7195                 :           0 :             break;
    7196                 :             :           default:
    7197                 :           0 :             fprintf (out, "     ??? (%u)\n", opcode);
    7198                 :           0 :             break;
    7199                 :             :           }
    7200         [ +  + ]:      134120 :       else if (opcode < DW_CFA_offset)
    7201                 :       96940 :         fprintf (out, "     advance_loc %u to %#" PRIx64 "\n",
    7202                 :       96940 :                  opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
    7203         [ +  + ]:       37180 :       else if (opcode < DW_CFA_restore)
    7204                 :             :         {
    7205                 :       33874 :           uint64_t offset;
    7206         [ -  + ]:       33874 :           if ((uint64_t) (endp - readp) < 1)
    7207                 :           0 :             goto invalid;
    7208                 :       33874 :           get_uleb128 (offset, readp, endp);
    7209                 :       33874 :           fprintf (out, "     offset r%u (%s) at cfa%+" PRId64 "\n",
    7210                 :             :                    opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
    7211                 :             :                    offset * data_align);
    7212                 :             :         }
    7213                 :             :       else
    7214                 :        3306 :         fprintf (out, "     restore r%u (%s)\n",
    7215                 :             :                  opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
    7216                 :             :     }
    7217                 :             : }
    7218                 :             : 
    7219                 :             : 
    7220                 :             : static unsigned int
    7221                 :       24198 : encoded_ptr_size (int encoding, unsigned int ptr_size)
    7222                 :             : {
    7223   [ -  +  -  + ]:       24198 :   switch (encoding & 7)
    7224                 :             :     {
    7225                 :             :     case DW_EH_PE_udata4:
    7226                 :             :       return 4;
    7227                 :           0 :     case DW_EH_PE_udata8:
    7228                 :           0 :       return 8;
    7229                 :             :     case 0:
    7230                 :             :       return ptr_size;
    7231                 :             :     }
    7232                 :             : 
    7233                 :           0 :   fprintf (stderr, "Unsupported pointer encoding: %#x, "
    7234                 :             :            "assuming pointer size of %d.\n", encoding, ptr_size);
    7235                 :           0 :   return ptr_size;
    7236                 :             : }
    7237                 :             : 
    7238                 :             : 
    7239                 :             : static unsigned int
    7240                 :         642 : print_encoding (unsigned int val, FILE *out)
    7241                 :             : {
    7242   [ -  -  -  +  :         642 :   switch (val & 0xf)
          -  -  -  +  -  
                      - ]
    7243                 :             :     {
    7244                 :           0 :     case DW_EH_PE_absptr:
    7245                 :           0 :       fputs ("absptr", out);
    7246                 :           0 :       break;
    7247                 :           0 :     case DW_EH_PE_uleb128:
    7248                 :           0 :       fputs ("uleb128", out);
    7249                 :           0 :       break;
    7250                 :           0 :     case DW_EH_PE_udata2:
    7251                 :           0 :       fputs ("udata2", out);
    7252                 :           0 :       break;
    7253                 :         140 :     case DW_EH_PE_udata4:
    7254                 :         140 :       fputs ("udata4", out);
    7255                 :         140 :       break;
    7256                 :           0 :     case DW_EH_PE_udata8:
    7257                 :           0 :       fputs ("udata8", out);
    7258                 :           0 :       break;
    7259                 :           0 :     case DW_EH_PE_sleb128:
    7260                 :           0 :       fputs ("sleb128", out);
    7261                 :           0 :       break;
    7262                 :           0 :     case DW_EH_PE_sdata2:
    7263                 :           0 :       fputs ("sdata2", out);
    7264                 :           0 :       break;
    7265                 :         502 :     case DW_EH_PE_sdata4:
    7266                 :         502 :       fputs ("sdata4", out);
    7267                 :         502 :       break;
    7268                 :           0 :     case DW_EH_PE_sdata8:
    7269                 :           0 :       fputs ("sdata8", out);
    7270                 :           0 :       break;
    7271                 :             :     default:
    7272                 :             :       /* We did not use any of the bits after all.  */
    7273                 :             :       return val;
    7274                 :             :     }
    7275                 :             : 
    7276                 :         642 :   return val & ~0xf;
    7277                 :             : }
    7278                 :             : 
    7279                 :             : 
    7280                 :             : static unsigned int
    7281                 :         502 : print_relinfo (unsigned int val, FILE *out)
    7282                 :             : {
    7283   [ +  -  +  -  :         502 :   switch (val & 0x70)
                   -  - ]
    7284                 :             :     {
    7285                 :         362 :     case DW_EH_PE_pcrel:
    7286                 :         362 :       fputs ("pcrel", out);
    7287                 :         362 :       break;
    7288                 :           0 :     case DW_EH_PE_textrel:
    7289                 :           0 :       fputs ("textrel", out);
    7290                 :           0 :       break;
    7291                 :         140 :     case DW_EH_PE_datarel:
    7292                 :         140 :       fputs ("datarel", out);
    7293                 :         140 :       break;
    7294                 :           0 :     case DW_EH_PE_funcrel:
    7295                 :           0 :       fputs ("funcrel", out);
    7296                 :           0 :       break;
    7297                 :           0 :     case DW_EH_PE_aligned:
    7298                 :           0 :       fputs ("aligned", out);
    7299                 :           0 :       break;
    7300                 :             :     default:
    7301                 :             :       return val;
    7302                 :             :     }
    7303                 :             : 
    7304                 :         502 :   return val & ~0x70;
    7305                 :             : }
    7306                 :             : 
    7307                 :             : 
    7308                 :             : static void
    7309                 :         642 : print_encoding_base (const char *pfx, unsigned int fde_encoding,
    7310                 :             :                      FILE *out)
    7311                 :             : {
    7312                 :         642 :   fprintf (out, "(%s", pfx);
    7313                 :             : 
    7314         [ -  + ]:         642 :   if (fde_encoding == DW_EH_PE_omit)
    7315                 :           0 :     fputs ("omit)\n", out);
    7316                 :             :   else
    7317                 :             :     {
    7318                 :         642 :       unsigned int w = fde_encoding;
    7319                 :             : 
    7320                 :         642 :       w = print_encoding (w, out);
    7321                 :             : 
    7322         [ +  + ]:         642 :       if (w & 0x70)
    7323                 :             :         {
    7324         [ +  - ]:         502 :           if (w != fde_encoding)
    7325                 :         502 :             fputc (' ', out);
    7326                 :             : 
    7327                 :         502 :           w = print_relinfo (w, out);
    7328                 :             :         }
    7329                 :             : 
    7330         [ -  + ]:         642 :       if (w != 0)
    7331         [ #  # ]:           0 :         fprintf (out, "%s%x", w != fde_encoding ? " " : "", w);
    7332                 :             : 
    7333                 :         642 :       fputs (")\n", out);
    7334                 :             :     }
    7335                 :         642 : }
    7336                 :             : 
    7337                 :             : 
    7338                 :             : static void
    7339                 :         196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    7340                 :             :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
    7341                 :             :                            FILE *out)
    7342                 :             : {
    7343                 :         196 :   size_t shstrndx;
    7344                 :             :   /* We know this call will succeed since it did in the caller.  */
    7345                 :         196 :   (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
    7346                 :         196 :   const char *scnname = section_name (ebl, shdr);
    7347                 :             : 
    7348                 :             :   /* Needed if we find PC-relative addresses.  */
    7349                 :         196 :   GElf_Addr bias;
    7350         [ -  + ]:         196 :   if (dwfl_module_getelf (dwflmod, &bias) == NULL)
    7351                 :             :     {
    7352                 :           0 :       error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
    7353                 :           0 :       return;
    7354                 :             :     }
    7355                 :             : 
    7356                 :         196 :   bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
    7357                 :         196 :   Elf_Data *data;
    7358         [ +  + ]:         196 :   if (is_eh_frame)
    7359                 :             :     {
    7360                 :         144 :       data = elf_rawdata (scn, NULL);
    7361         [ -  + ]:         144 :       if (data == NULL)
    7362                 :             :         {
    7363                 :           0 :           error (0, 0, _("cannot get %s content: %s"),
    7364                 :             :                  scnname, elf_errmsg (-1));
    7365                 :           0 :           return;
    7366                 :             :         }
    7367                 :             :     }
    7368                 :             :   else
    7369                 :             :     {
    7370                 :          52 :       data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
    7371         [ +  - ]:          52 :       if (data == NULL)
    7372                 :             :         return;
    7373                 :             :     }
    7374                 :             : 
    7375         [ +  + ]:         196 :   if (is_eh_frame)
    7376                 :         144 :     fprintf (out, _("\
    7377                 :             : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    7378                 :         144 :              elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
    7379                 :             :   else
    7380                 :          52 :     fprintf (out, _("\
    7381                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    7382                 :          52 :              elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
    7383                 :             : 
    7384                 :         196 :   struct cieinfo
    7385                 :             :   {
    7386                 :             :     ptrdiff_t cie_offset;
    7387                 :             :     const char *augmentation;
    7388                 :             :     unsigned int code_alignment_factor;
    7389                 :             :     unsigned int data_alignment_factor;
    7390                 :             :     uint8_t address_size;
    7391                 :             :     uint8_t fde_encoding;
    7392                 :             :     uint8_t lsda_encoding;
    7393                 :             :     struct cieinfo *next;
    7394                 :         196 :   } *cies = NULL;
    7395                 :             : 
    7396                 :         196 :   const unsigned char *readp = data->d_buf;
    7397                 :         196 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    7398                 :         196 :                                         + data->d_size);
    7399         [ +  + ]:       24844 :   while (readp < dataend)
    7400                 :             :     {
    7401         [ -  + ]:       24648 :       if (unlikely (readp + 4 > dataend))
    7402                 :             :         {
    7403                 :           0 :         invalid_data:
    7404                 :           0 :           error (0, 0, _("invalid data in section [%zu] '%s'"),
    7405                 :             :                      elf_ndxscn (scn), scnname);
    7406                 :           0 :               return;
    7407                 :             :         }
    7408                 :             : 
    7409                 :             :       /* At the beginning there must be a CIE.  There can be multiple,
    7410                 :             :          hence we test tis in a loop.  */
    7411                 :       24648 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    7412                 :             : 
    7413         [ +  + ]:       24648 :       Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    7414                 :       24648 :       unsigned int length = 4;
    7415         [ -  + ]:       24648 :       if (unlikely (unit_length == 0xffffffff))
    7416                 :             :         {
    7417         [ #  # ]:           0 :           if (unlikely (readp + 8 > dataend))
    7418                 :           0 :             goto invalid_data;
    7419                 :             : 
    7420         [ #  # ]:           0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    7421                 :           0 :           length = 8;
    7422                 :             :         }
    7423                 :             : 
    7424         [ +  + ]:       24648 :       if (unlikely (unit_length == 0))
    7425                 :             :         {
    7426                 :         140 :           fprintf (out, _("\n [%6tx] Zero terminator\n"), offset);
    7427                 :         140 :           continue;
    7428                 :             :         }
    7429                 :             : 
    7430                 :       24508 :       Dwarf_Word maxsize = dataend - readp;
    7431         [ -  + ]:       24508 :       if (unlikely (unit_length > maxsize))
    7432                 :           0 :         goto invalid_data;
    7433                 :             : 
    7434         [ +  + ]:       24508 :       unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    7435                 :             : 
    7436                 :       24508 :       ptrdiff_t start = readp - (unsigned char *) data->d_buf;
    7437                 :       24508 :       const unsigned char *const cieend = readp + unit_length;
    7438         [ -  + ]:       24508 :       if (unlikely (cieend > dataend))
    7439                 :           0 :         goto invalid_data;
    7440                 :             : 
    7441                 :       24508 :       Dwarf_Off cie_id;
    7442         [ +  - ]:       24508 :       if (length == 4)
    7443                 :             :         {
    7444         [ -  + ]:       24508 :           if (unlikely (cieend - readp < 4))
    7445                 :           0 :             goto invalid_data;
    7446         [ +  + ]:       24508 :           cie_id = read_4ubyte_unaligned_inc (dbg, readp);
    7447         [ +  + ]:       24508 :           if (!is_eh_frame && cie_id == DW_CIE_ID_32)
    7448                 :             :             cie_id = DW_CIE_ID_64;
    7449                 :             :         }
    7450                 :             :       else
    7451                 :             :         {
    7452         [ #  # ]:           0 :           if (unlikely (cieend - readp < 8))
    7453                 :           0 :             goto invalid_data;
    7454         [ #  # ]:           0 :           cie_id = read_8ubyte_unaligned_inc (dbg, readp);
    7455                 :             :         }
    7456                 :             : 
    7457                 :       48932 :       uint_fast8_t version = 2;
    7458                 :       24424 :       unsigned int code_alignment_factor;
    7459                 :       24424 :       int data_alignment_factor;
    7460                 :       48932 :       unsigned int fde_encoding = 0;
    7461                 :       48932 :       unsigned int lsda_encoding = 0;
    7462                 :       48932 :       Dwarf_Word initial_location = 0;
    7463                 :       48932 :       Dwarf_Word vma_base = 0;
    7464                 :             : 
    7465   [ +  +  +  + ]:       24600 :       if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
    7466                 :             :         {
    7467         [ -  + ]:         310 :           if (unlikely (cieend - readp < 2))
    7468                 :           0 :             goto invalid_data;
    7469                 :         310 :           version = *readp++;
    7470                 :         310 :           const char *const augmentation = (const char *) readp;
    7471                 :         310 :           readp = memchr (readp, '\0', cieend - readp);
    7472         [ -  + ]:         310 :           if (unlikely (readp == NULL))
    7473                 :           0 :             goto invalid_data;
    7474                 :         310 :           ++readp;
    7475                 :             : 
    7476                 :         310 :           uint_fast8_t segment_size = 0;
    7477         [ +  + ]:         310 :           if (version >= 4)
    7478                 :             :             {
    7479         [ -  + ]:           8 :               if (cieend - readp < 5)
    7480                 :           0 :                 goto invalid_data;
    7481                 :           8 :               ptr_size = *readp++;
    7482                 :           8 :               segment_size = *readp++;
    7483                 :             :             }
    7484                 :             : 
    7485         [ -  + ]:         310 :           if (cieend - readp < 1)
    7486                 :           0 :             goto invalid_data;
    7487                 :         310 :           get_uleb128 (code_alignment_factor, readp, cieend);
    7488         [ -  + ]:         310 :           if (cieend - readp < 1)
    7489                 :           0 :             goto invalid_data;
    7490                 :         310 :           get_sleb128 (data_alignment_factor, readp, cieend);
    7491                 :             : 
    7492                 :             :           /* In some variant for unwind data there is another field.  */
    7493         [ -  + ]:         310 :           if (strcmp (augmentation, "eh") == 0)
    7494         [ #  # ]:           0 :             readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    7495                 :             : 
    7496                 :         310 :           unsigned int return_address_register;
    7497         [ -  + ]:         310 :           if (cieend - readp < 1)
    7498                 :           0 :             goto invalid_data;
    7499         [ +  + ]:         310 :           if (unlikely (version == 1))
    7500                 :         286 :             return_address_register = *readp++;
    7501                 :             :           else
    7502                 :          24 :             get_uleb128 (return_address_register, readp, cieend);
    7503                 :             : 
    7504                 :         310 :           fprintf (out, "\n [%6tx] CIE length=%" PRIu64 "\n"
    7505                 :             :                    "   CIE_id:                   %" PRIu64 "\n"
    7506                 :             :                    "   version:                  %u\n"
    7507                 :             :                    "   augmentation:             \"%s\"\n",
    7508                 :             :                    offset, (uint64_t) unit_length, (uint64_t) cie_id,
    7509                 :             :                    version, augmentation);
    7510         [ +  + ]:         310 :           if (version >= 4)
    7511                 :           8 :             fprintf (out, "   address_size:             %u\n"
    7512                 :             :                      "   segment_size:             %u\n",
    7513                 :             :                      ptr_size, segment_size);
    7514                 :         310 :           fprintf (out, "   code_alignment_factor:    %u\n"
    7515                 :             :                    "   data_alignment_factor:    %d\n"
    7516                 :             :                    "   return_address_register:  %u\n",
    7517                 :             :                    code_alignment_factor,
    7518                 :             :                    data_alignment_factor, return_address_register);
    7519                 :             : 
    7520         [ +  + ]:         310 :           if (augmentation[0] == 'z')
    7521                 :             :             {
    7522         [ -  + ]:         222 :               if (cieend - readp < 1)
    7523                 :           0 :                 goto invalid_data;
    7524                 :             : 
    7525                 :         222 :               unsigned int augmentationlen;
    7526                 :         222 :               get_uleb128 (augmentationlen, readp, cieend);
    7527                 :             : 
    7528         [ -  + ]:         222 :               if (augmentationlen > (size_t) (cieend - readp))
    7529                 :             :                 {
    7530                 :           0 :                   error (0, 0, _("invalid augmentation length"));
    7531                 :           0 :                   readp = cieend;
    7532                 :           0 :                   continue;
    7533                 :             :                 }
    7534                 :             : 
    7535                 :         222 :               const char *hdr = "Augmentation data:";
    7536                 :         222 :               const char *cp = augmentation + 1;
    7537   [ +  +  +  - ]:         444 :               while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
    7538                 :             :                 {
    7539                 :         222 :                   fprintf (out, "   %-26s%#x ", hdr, *readp);
    7540                 :         222 :                   hdr = "";
    7541                 :             : 
    7542         [ +  - ]:         222 :                   if (*cp == 'R')
    7543                 :             :                     {
    7544                 :         222 :                       fde_encoding = *readp++;
    7545                 :         222 :                       print_encoding_base (_("FDE address encoding: "),
    7546                 :             :                                            fde_encoding, out);
    7547                 :             :                     }
    7548         [ #  # ]:           0 :                   else if (*cp == 'L')
    7549                 :             :                     {
    7550                 :           0 :                       lsda_encoding = *readp++;
    7551                 :           0 :                       print_encoding_base (_("LSDA pointer encoding: "),
    7552                 :             :                                            lsda_encoding, out);
    7553                 :             :                     }
    7554         [ #  # ]:           0 :                   else if (*cp == 'P')
    7555                 :             :                     {
    7556                 :             :                       /* Personality.  This field usually has a relocation
    7557                 :             :                          attached pointing to __gcc_personality_v0.  */
    7558                 :           0 :                       const unsigned char *startp = readp;
    7559                 :           0 :                       unsigned int encoding = *readp++;
    7560                 :           0 :                       uint64_t val = 0;
    7561                 :           0 :                       readp = read_encoded (encoding, readp,
    7562                 :             :                                             readp - 1 + augmentationlen,
    7563                 :             :                                             &val, dbg);
    7564                 :             : 
    7565         [ #  # ]:           0 :                       while (++startp < readp)
    7566                 :           0 :                         fprintf (out, "%#x ", *startp);
    7567                 :             : 
    7568                 :           0 :                       fputc ('(', out);
    7569                 :           0 :                       print_encoding (encoding, out);
    7570                 :           0 :                       fputc (' ', out);
    7571         [ #  # ]:           0 :                       switch (encoding & 0xf)
    7572                 :             :                         {
    7573                 :           0 :                         case DW_EH_PE_sleb128:
    7574                 :             :                         case DW_EH_PE_sdata2:
    7575                 :             :                         case DW_EH_PE_sdata4:
    7576                 :           0 :                           fprintf (out, "%" PRId64 ")\n", val);
    7577                 :           0 :                           break;
    7578                 :           0 :                         default:
    7579                 :           0 :                           fprintf (out, "%#" PRIx64 ")\n", val);
    7580                 :           0 :                           break;
    7581                 :             :                         }
    7582                 :             :                     }
    7583                 :             :                   else
    7584                 :           0 :                     fprintf (out, "(%x)\n", *readp++);
    7585                 :             : 
    7586                 :         222 :                   ++cp;
    7587                 :             :                 }
    7588                 :             :             }
    7589                 :             : 
    7590         [ +  - ]:         310 :           if (likely (ptr_size == 4 || ptr_size == 8))
    7591                 :             :             {
    7592                 :         310 :               struct cieinfo *newp = alloca (sizeof (*newp));
    7593                 :         310 :               newp->cie_offset = offset;
    7594                 :         310 :               newp->augmentation = augmentation;
    7595                 :         310 :               newp->fde_encoding = fde_encoding;
    7596                 :         310 :               newp->lsda_encoding = lsda_encoding;
    7597                 :         310 :               newp->address_size = ptr_size;
    7598                 :         310 :               newp->code_alignment_factor = code_alignment_factor;
    7599                 :         310 :               newp->data_alignment_factor = data_alignment_factor;
    7600                 :         310 :               newp->next = cies;
    7601                 :         310 :               cies = newp;
    7602                 :             :             }
    7603                 :             :         }
    7604                 :             :       else
    7605                 :             :         {
    7606                 :             :           struct cieinfo *cie = cies;
    7607         [ +  - ]:       24198 :           while (cie != NULL)
    7608   [ +  +  -  + ]:       48396 :             if (is_eh_frame
    7609                 :       24106 :                 ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
    7610                 :          92 :                 : cie_id == (Dwarf_Off) cie->cie_offset)
    7611                 :             :               break;
    7612                 :             :             else
    7613                 :           0 :               cie = cie->next;
    7614                 :       24198 :           if (unlikely (cie == NULL))
    7615                 :             :             {
    7616                 :           0 :               fputs ("invalid CIE reference in FDE\n", out);
    7617                 :           0 :               return;
    7618                 :             :             }
    7619                 :             : 
    7620                 :             :           /* Initialize from CIE data.  */
    7621                 :       24198 :           fde_encoding = cie->fde_encoding;
    7622                 :       24198 :           lsda_encoding = cie->lsda_encoding;
    7623                 :       24198 :           ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
    7624                 :       24198 :           code_alignment_factor = cie->code_alignment_factor;
    7625                 :       24198 :           data_alignment_factor = cie->data_alignment_factor;
    7626                 :             : 
    7627                 :       24198 :           const unsigned char *base = readp;
    7628                 :             :           // XXX There are sometimes relocations for this value
    7629   [ -  +  +  +  :       24198 :           initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
             -  +  +  + ]
    7630                 :       48396 :           Dwarf_Word address_range
    7631   [ +  +  -  +  :       24198 :             = read_addr_unaligned_inc (ptr_size, dbg, readp);
                   +  + ]
    7632                 :             : 
    7633                 :             :           /* pcrel for an FDE address is relative to the runtime
    7634                 :             :              address of the start_address field itself.  Sign extend
    7635                 :             :              if necessary to make sure the calculation is done on the
    7636                 :             :              full 64 bit address even when initial_location only holds
    7637                 :             :              the lower 32 bits.  */
    7638                 :       24198 :           Dwarf_Addr pc_start = initial_location;
    7639         [ +  + ]:       24198 :           if (ptr_size == 4)
    7640                 :       24118 :             pc_start = (uint64_t) (int32_t) pc_start;
    7641         [ +  + ]:       24198 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    7642                 :       24094 :             pc_start += ((uint64_t) shdr->sh_addr
    7643                 :       24094 :                          + (base - (const unsigned char *) data->d_buf)
    7644                 :       24094 :                          - bias);
    7645                 :             : 
    7646                 :       24198 :           fprintf (out, "\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
    7647                 :             :                    "   CIE_pointer:              %" PRIu64 "\n"
    7648                 :             :                    "   initial_location:         ",
    7649                 :             :                    offset, (uint64_t) unit_length,
    7650                 :             :                    cie->cie_offset, (uint64_t) cie_id);
    7651                 :       24198 :           print_dwarf_addr (dwflmod, cie->address_size,
    7652                 :             :                             pc_start, initial_location, out);
    7653         [ +  + ]:       24198 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    7654                 :             :             {
    7655                 :       48188 :               vma_base = (((uint64_t) shdr->sh_offset
    7656                 :       24094 :                            + (base - (const unsigned char *) data->d_buf)
    7657                 :       24094 :                            + (uint64_t) initial_location)
    7658                 :             :                           & (ptr_size == 4
    7659                 :             :                              ? UINT64_C (0xffffffff)
    7660         [ -  + ]:       24094 :                              : UINT64_C (0xffffffffffffffff)));
    7661                 :       24094 :               fprintf (out, _(" (offset: %#" PRIx64 ")"),
    7662                 :             :                        (uint64_t) vma_base);
    7663                 :             :             }
    7664                 :             : 
    7665                 :       24198 :           fprintf (out, "\n   address_range:            %#" PRIx64,
    7666                 :             :                    (uint64_t) address_range);
    7667         [ +  + ]:       24198 :           if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
    7668                 :       24094 :             fprintf (out, _(" (end offset: %#" PRIx64 ")"),
    7669                 :       24094 :                      ((uint64_t) vma_base + (uint64_t) address_range)
    7670                 :             :                      & (ptr_size == 4
    7671                 :             :                         ? UINT64_C (0xffffffff)
    7672         [ -  + ]:       24094 :                         : UINT64_C (0xffffffffffffffff)));
    7673                 :       24198 :           fputc ('\n', out);
    7674                 :             : 
    7675         [ +  + ]:       24198 :           if (cie->augmentation[0] == 'z')
    7676                 :             :             {
    7677                 :       24094 :               unsigned int augmentationlen;
    7678         [ -  + ]:       24094 :               if (cieend - readp < 1)
    7679                 :           0 :                 goto invalid_data;
    7680                 :       24094 :               get_uleb128 (augmentationlen, readp, cieend);
    7681                 :             : 
    7682         [ -  + ]:       24094 :               if (augmentationlen > (size_t) (cieend - readp))
    7683                 :             :                 {
    7684                 :           0 :                   error (0, 0, _("invalid augmentation length"));
    7685                 :           0 :                   readp = cieend;
    7686                 :           0 :                   continue;
    7687                 :             :                 }
    7688                 :             : 
    7689         [ -  + ]:       24094 :               if (augmentationlen > 0)
    7690                 :             :                 {
    7691                 :           0 :                   const char *hdr = "Augmentation data:";
    7692                 :           0 :                   const char *cp = cie->augmentation + 1;
    7693                 :           0 :                   unsigned int u = 0;
    7694                 :           0 :                   while (*cp != '\0'
    7695   [ #  #  #  # ]:           0 :                          && cp < cie->augmentation + augmentationlen + 1)
    7696                 :             :                     {
    7697         [ #  # ]:           0 :                       if (*cp == 'L')
    7698                 :             :                         {
    7699                 :           0 :                           uint64_t lsda_pointer;
    7700                 :           0 :                           const unsigned char *p
    7701                 :           0 :                             = read_encoded (lsda_encoding, &readp[u],
    7702                 :             :                                             &readp[augmentationlen],
    7703                 :             :                                             &lsda_pointer, dbg);
    7704                 :           0 :                           u = p - readp;
    7705                 :           0 :                           fprintf (out, _("\
    7706                 :             :    %-26sLSDA pointer: %#" PRIx64 "\n"),
    7707                 :             :                                    hdr, lsda_pointer);
    7708                 :           0 :                           hdr = "";
    7709                 :             :                         }
    7710                 :           0 :                       ++cp;
    7711                 :             :                     }
    7712                 :             : 
    7713         [ #  # ]:           0 :                   while (u < augmentationlen)
    7714                 :             :                     {
    7715                 :           0 :                       fprintf (out, "   %-26s%#x\n", hdr, readp[u++]);
    7716                 :           0 :                       hdr = "";
    7717                 :             :                     }
    7718                 :             :                 }
    7719                 :             : 
    7720                 :       24094 :               readp += augmentationlen;
    7721                 :             :             }
    7722                 :             :         }
    7723                 :             : 
    7724                 :             :       /* Handle the initialization instructions.  */
    7725         [ -  + ]:       24508 :       if (ptr_size != 4 && ptr_size !=8)
    7726                 :           0 :         fprintf (out, "invalid CIE pointer size (%u), must be 4 or 8.\n",
    7727                 :             :                  ptr_size);
    7728                 :             :       else
    7729                 :       24508 :         print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
    7730                 :             :                            data_alignment_factor, version, ptr_size,
    7731                 :             :                            fde_encoding, dwflmod, ebl, ehdr, dbg, out);
    7732                 :       24508 :       readp = cieend;
    7733                 :             :     }
    7734                 :             : }
    7735                 :             : 
    7736                 :             : 
    7737                 :             : /* Returns the signedness (or false if it cannot be determined) and
    7738                 :             :    the byte size (or zero if it cannot be gotten) of the given DIE
    7739                 :             :    DW_AT_type attribute.  Uses dwarf_peel_type and dwarf_aggregate_size.  */
    7740                 :             : static void
    7741                 :      352974 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
    7742                 :             : {
    7743                 :      352974 :   Dwarf_Attribute attr;
    7744                 :      352974 :   Dwarf_Die type;
    7745                 :             : 
    7746                 :      352974 :   *bytes = 0;
    7747                 :      352974 :   *is_signed = false;
    7748                 :             : 
    7749         [ +  + ]:      352974 :   if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
    7750                 :             :                                                                 DW_AT_type,
    7751                 :             :                                                                 &attr), &type),
    7752                 :             :                        &type) == 0)
    7753                 :             :     {
    7754                 :         944 :       Dwarf_Word val;
    7755                 :         944 :       *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
    7756                 :             :                                                  &attr), &val) == 0
    7757   [ +  +  +  + ]:         944 :                     && (val == DW_ATE_signed || val == DW_ATE_signed_char));
    7758                 :             : 
    7759         [ +  - ]:         944 :       if (dwarf_aggregate_size (&type, &val) == 0)
    7760                 :         944 :         *bytes = val;
    7761                 :             :     }
    7762                 :      352974 : }
    7763                 :             : 
    7764                 :             : struct attrcb_args
    7765                 :             : {
    7766                 :             :   Dwfl_Module *dwflmod;
    7767                 :             :   Dwarf *dbg;
    7768                 :             :   Dwarf_Die *dies;
    7769                 :             :   int level;
    7770                 :             :   bool silent;
    7771                 :             :   bool is_split;
    7772                 :             :   unsigned int version;
    7773                 :             :   unsigned int addrsize;
    7774                 :             :   unsigned int offset_size;
    7775                 :             :   struct Dwarf_CU *cu;
    7776                 :             :   FILE *out;
    7777                 :             : };
    7778                 :             : 
    7779                 :             : 
    7780                 :             : static int
    7781                 :     8118186 : attr_callback (Dwarf_Attribute *attrp, void *arg)
    7782                 :             : {
    7783                 :     8118186 :   struct attrcb_args *cbargs = (struct attrcb_args *) arg;
    7784                 :     8118186 :   const int level = cbargs->level;
    7785                 :     8118186 :   Dwarf_Die *die = &cbargs->dies[level];
    7786                 :     8118186 :   bool is_split = cbargs->is_split;
    7787                 :     8118186 :   FILE *out = cbargs->out;
    7788                 :             : 
    7789         [ +  - ]:     8118186 :   unsigned int attr = dwarf_whatattr (attrp);
    7790         [ -  + ]:     8118186 :   if (unlikely (attr == 0))
    7791                 :             :     {
    7792         [ #  # ]:           0 :       if (!cbargs->silent)
    7793                 :           0 :         error (0, 0, _("DIE [%" PRIx64 "] "
    7794                 :             :                               "cannot get attribute code: %s"),
    7795                 :             :                dwarf_dieoffset (die), dwarf_errmsg (-1));
    7796                 :           0 :       return DWARF_CB_ABORT;
    7797                 :             :     }
    7798                 :             : 
    7799         [ -  + ]:     8118186 :   unsigned int form = dwarf_whatform (attrp);
    7800         [ -  + ]:     8118186 :   if (unlikely (form == 0))
    7801                 :             :     {
    7802         [ #  # ]:           0 :       if (!cbargs->silent)
    7803                 :           0 :         error (0, 0, _("DIE [%" PRIx64 "] "
    7804                 :             :                               "cannot get attribute form: %s"),
    7805                 :             :                dwarf_dieoffset (die), dwarf_errmsg (-1));
    7806                 :           0 :       return DWARF_CB_ABORT;
    7807                 :             :     }
    7808                 :             : 
    7809   [ +  +  +  +  :     8118186 :   switch (form)
             +  +  +  +  
                      - ]
    7810                 :             :     {
    7811                 :      103120 :     case DW_FORM_addr:
    7812                 :             :     case DW_FORM_addrx:
    7813                 :             :     case DW_FORM_addrx1:
    7814                 :             :     case DW_FORM_addrx2:
    7815                 :             :     case DW_FORM_addrx3:
    7816                 :             :     case DW_FORM_addrx4:
    7817                 :             :     case DW_FORM_GNU_addr_index:
    7818         [ +  + ]:      103120 :       if (!cbargs->silent)
    7819                 :             :         {
    7820                 :      102578 :           Dwarf_Addr addr;
    7821         [ -  + ]:      102578 :           if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
    7822                 :             :             {
    7823                 :           0 :             attrval_out:
    7824         [ #  # ]:           0 :               if (!cbargs->silent)
    7825                 :           0 :                 error (0, 0, _("DIE [%" PRIx64 "] "
    7826                 :             :                                       "cannot get attribute '%s' (%s) value: "
    7827                 :             :                                       "%s"),
    7828                 :             :                        dwarf_dieoffset (die),
    7829                 :             :                        dwarf_attr_name (attr),
    7830                 :             :                        dwarf_form_name (form),
    7831                 :             :                        dwarf_errmsg (-1));
    7832                 :             :               /* Don't ABORT, it might be other attributes can be resolved.  */
    7833                 :           0 :               return DWARF_CB_OK;
    7834                 :             :             }
    7835         [ +  + ]:      102578 :           if (form != DW_FORM_addr )
    7836                 :             :             {
    7837                 :          40 :               Dwarf_Word word;
    7838         [ -  + ]:          40 :               if (dwarf_formudata (attrp, &word) != 0)
    7839                 :           0 :                 goto attrval_out;
    7840                 :          40 :               fprintf (out, "           %*s%-20s (%s) [%" PRIx64 "] ",
    7841                 :             :                        (int) (level * 2), "", dwarf_attr_name (attr),
    7842                 :             :                        dwarf_form_name (form), word);
    7843                 :             :             }
    7844                 :             :           else
    7845                 :      102538 :             fprintf (out, "           %*s%-20s (%s) ",
    7846                 :             :                      (int) (level * 2), "", dwarf_attr_name (attr),
    7847                 :             :                      dwarf_form_name (form));
    7848                 :      102578 :           print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr, out);
    7849                 :      102578 :           fprintf (out, "\n");
    7850                 :             :         }
    7851                 :     7298310 :       break;
    7852                 :             : 
    7853                 :     1343998 :     case DW_FORM_indirect:
    7854                 :             :     case DW_FORM_strp:
    7855                 :             :     case DW_FORM_line_strp:
    7856                 :             :     case DW_FORM_strx:
    7857                 :             :     case DW_FORM_strx1:
    7858                 :             :     case DW_FORM_strx2:
    7859                 :             :     case DW_FORM_strx3:
    7860                 :             :     case DW_FORM_strx4:
    7861                 :             :     case DW_FORM_string:
    7862                 :             :     case DW_FORM_GNU_strp_alt:
    7863                 :             :     case DW_FORM_GNU_str_index:
    7864         [ +  + ]:     1343998 :       if (cbargs->silent)
    7865                 :             :         break;
    7866                 :     1342488 :       const char *str = dwarf_formstring (attrp);
    7867         [ -  + ]:     1342488 :       if (unlikely (str == NULL))
    7868                 :           0 :         goto attrval_out;
    7869                 :     1342488 :       fprintf (out, "           %*s%-20s (%s) \"%s\"\n",
    7870                 :             :                (int) (level * 2), "", dwarf_attr_name (attr),
    7871                 :             :                dwarf_form_name (form), str);
    7872                 :     1342488 :       break;
    7873                 :             : 
    7874                 :     1755136 :     case DW_FORM_ref_addr:
    7875                 :             :     case DW_FORM_ref_udata:
    7876                 :             :     case DW_FORM_ref8:
    7877                 :             :     case DW_FORM_ref4:
    7878                 :             :     case DW_FORM_ref2:
    7879                 :             :     case DW_FORM_ref1:
    7880                 :             :     case DW_FORM_GNU_ref_alt:
    7881                 :             :     case DW_FORM_ref_sup4:
    7882                 :             :     case DW_FORM_ref_sup8:
    7883         [ +  + ]:     1755136 :       if (cbargs->silent)
    7884                 :             :         break;
    7885                 :     1753556 :       Dwarf_Die ref;
    7886         [ -  + ]:     1753556 :       if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
    7887                 :           0 :         goto attrval_out;
    7888                 :             : 
    7889                 :     1753556 :       fprintf (out, "           %*s%-20s (%s) ",
    7890                 :             :                (int) (level * 2), "", dwarf_attr_name (attr),
    7891                 :             :                dwarf_form_name (form));
    7892         [ +  + ]:     1753556 :       if (is_split)
    7893                 :          66 :         fprintf (out, "{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
    7894                 :             :       else
    7895                 :     1753490 :         fprintf (out, "[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
    7896                 :             :       break;
    7897                 :             : 
    7898                 :           8 :     case DW_FORM_ref_sig8:
    7899         [ +  - ]:           8 :       if (cbargs->silent)
    7900                 :             :         break;
    7901                 :           8 :       fprintf (out, "           %*s%-20s (%s) {%6" PRIx64 "}\n",
    7902                 :             :                (int) (level * 2), "", dwarf_attr_name (attr),
    7903                 :             :                dwarf_form_name (form),
    7904         [ -  + ]:           8 :                (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
    7905                 :           8 :       break;
    7906                 :             : 
    7907                 :     4513342 :     case DW_FORM_sec_offset:
    7908                 :             :     case DW_FORM_rnglistx:
    7909                 :             :     case DW_FORM_loclistx:
    7910                 :             :     case DW_FORM_implicit_const:
    7911                 :             :     case DW_FORM_udata:
    7912                 :             :     case DW_FORM_sdata:
    7913                 :             :     case DW_FORM_data8: /* Note no data16 here, we see that as block. */
    7914                 :             :     case DW_FORM_data4:
    7915                 :             :     case DW_FORM_data2:
    7916                 :     4513342 :     case DW_FORM_data1:;
    7917                 :     4513342 :       Dwarf_Word num;
    7918         [ -  + ]:     4513342 :       if (unlikely (dwarf_formudata (attrp, &num) != 0))
    7919                 :           0 :         goto attrval_out;
    7920                 :             : 
    7921                 :     4513342 :       const char *valuestr = NULL;
    7922                 :     4513342 :       bool as_hex_id = false;
    7923   [ +  +  +  +  :     4513342 :       switch (attr)
          +  +  +  +  +  
          +  -  -  -  -  
          -  -  +  -  +  
                   +  + ]
    7924                 :             :         {
    7925                 :             :           /* This case can take either a constant or a loclistptr.  */
    7926                 :      562104 :         case DW_AT_data_member_location:
    7927         [ +  - ]:      562104 :           if (form != DW_FORM_sec_offset
    7928         [ +  + ]:      562104 :               && (cbargs->version >= 4
    7929         [ +  - ]:       47612 :                   || (form != DW_FORM_data4 && form != DW_FORM_data8)))
    7930                 :             :             {
    7931         [ +  - ]:      562104 :               if (!cbargs->silent)
    7932                 :      562104 :                 fprintf (out, "           %*s%-20s (%s) %" PRIuMAX "\n",
    7933                 :             :                          (int) (level * 2), "", dwarf_attr_name (attr),
    7934                 :             :                          dwarf_form_name (form), (uintmax_t) num);
    7935                 :      562104 :               return DWARF_CB_OK;
    7936                 :             :             }
    7937                 :      221858 :           FALLTHROUGH;
    7938                 :             : 
    7939                 :             :         /* These cases always take a loclist[ptr] and no constant. */
    7940                 :             :         case DW_AT_location:
    7941                 :             :         case DW_AT_data_location:
    7942                 :             :         case DW_AT_vtable_elem_location:
    7943                 :             :         case DW_AT_string_length:
    7944                 :             :         case DW_AT_use_location:
    7945                 :             :         case DW_AT_frame_base:
    7946                 :             :         case DW_AT_return_addr:
    7947                 :             :         case DW_AT_static_link:
    7948                 :             :         case DW_AT_segment:
    7949                 :             :         case DW_AT_GNU_call_site_value:
    7950                 :             :         case DW_AT_GNU_call_site_data_value:
    7951                 :             :         case DW_AT_GNU_call_site_target:
    7952                 :             :         case DW_AT_GNU_call_site_target_clobbered:
    7953                 :             :         case DW_AT_GNU_locviews:
    7954                 :             :           {
    7955                 :      221858 :             bool nlpt;
    7956         [ +  + ]:      221858 :             if (cbargs->cu->version < 5)
    7957                 :             :               {
    7958         [ +  + ]:         324 :                 if (! cbargs->is_split)
    7959                 :             :                   {
    7960                 :         284 :                     nlpt = notice_listptr (section_loc, &known_locsptr,
    7961                 :         284 :                                            cbargs->addrsize,
    7962                 :         284 :                                            cbargs->offset_size,
    7963                 :             :                                            cbargs->cu, num, attr);
    7964                 :             :                   }
    7965                 :             :                 else
    7966                 :             :                   nlpt = true;
    7967                 :             :               }
    7968                 :             :             else
    7969                 :             :               {
    7970                 :             :                 /* Only register for a real section offset.  Otherwise
    7971                 :             :                    it is a DW_FORM_loclistx which is just an index
    7972                 :             :                    number and we should already have registered the
    7973                 :             :                    section offset for the index when we saw the
    7974                 :             :                    DW_AT_loclists_base CU attribute.  */
    7975         [ +  + ]:      221534 :                 if (form == DW_FORM_sec_offset)
    7976                 :      221456 :                   nlpt = notice_listptr (section_loc, &known_loclistsptr,
    7977                 :      221456 :                                          cbargs->addrsize, cbargs->offset_size,
    7978                 :             :                                          cbargs->cu, num, attr);
    7979                 :             :                 else
    7980                 :             :                   nlpt = true;
    7981                 :             : 
    7982                 :             :               }
    7983                 :             : 
    7984         [ +  + ]:      221858 :             if (!cbargs->silent)
    7985                 :             :               {
    7986   [ +  +  +  + ]:      221492 :                 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
    7987         [ -  + ]:      221478 :                   fprintf (out, "           %*s%-20s (%s) location list [%6"
    7988                 :             :                            PRIxMAX "]%s\n",
    7989                 :             :                            (int) (level * 2), "", dwarf_attr_name (attr),
    7990                 :             :                            dwarf_form_name (form), (uintmax_t) num,
    7991                 :             :                            nlpt ? "" : " <WARNING offset too big>");
    7992                 :             :                 else
    7993                 :          14 :                   fprintf (out, "           %*s%-20s (%s) location index [%6"
    7994                 :             :                            PRIxMAX "]\n",
    7995                 :             :                            (int) (level * 2), "", dwarf_attr_name (attr),
    7996                 :             :                            dwarf_form_name (form), (uintmax_t) num);
    7997                 :             :               }
    7998                 :             :           }
    7999                 :             :           return DWARF_CB_OK;
    8000                 :             : 
    8001                 :          10 :         case DW_AT_loclists_base:
    8002                 :             :           {
    8003                 :          20 :             bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
    8004                 :          10 :                                         cbargs->addrsize, cbargs->offset_size,
    8005                 :             :                                         cbargs->cu, num, attr);
    8006                 :             : 
    8007         [ +  + ]:          10 :             if (!cbargs->silent)
    8008         [ -  + ]:           2 :               fprintf (out, "           %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
    8009                 :             :                        (int) (level * 2), "", dwarf_attr_name (attr),
    8010                 :             :                        dwarf_form_name (form), (uintmax_t) num,
    8011                 :             :                        nlpt ? "" : " <WARNING offset too big>");
    8012                 :             :           }
    8013                 :             :           return DWARF_CB_OK;
    8014                 :             : 
    8015                 :       35854 :         case DW_AT_ranges:
    8016                 :             :         case DW_AT_start_scope:
    8017                 :             :           {
    8018                 :       35854 :             bool nlpt;
    8019         [ +  + ]:       35854 :             if (cbargs->cu->version < 5)
    8020                 :         106 :               nlpt = notice_listptr (section_ranges, &known_rangelistptr,
    8021                 :         106 :                                      cbargs->addrsize, cbargs->offset_size,
    8022                 :             :                                      cbargs->cu, num, attr);
    8023                 :             :             else
    8024                 :             :               {
    8025                 :             :                 /* Only register for a real section offset.  Otherwise
    8026                 :             :                    it is a DW_FORM_rangelistx which is just an index
    8027                 :             :                    number and we should already have registered the
    8028                 :             :                    section offset for the index when we saw the
    8029                 :             :                    DW_AT_rnglists_base CU attribute.  */
    8030         [ +  + ]:       35748 :                 if (form == DW_FORM_sec_offset)
    8031                 :       35736 :                   nlpt = notice_listptr (section_ranges, &known_rnglistptr,
    8032                 :       35736 :                                          cbargs->addrsize, cbargs->offset_size,
    8033                 :             :                                          cbargs->cu, num, attr);
    8034                 :             :                 else
    8035                 :             :                   nlpt = true;
    8036                 :             :               }
    8037                 :             : 
    8038         [ +  + ]:       35854 :             if (!cbargs->silent)
    8039                 :             :               {
    8040   [ +  +  +  + ]:       35768 :                 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
    8041         [ -  + ]:       35764 :                   fprintf (out, "           %*s%-20s (%s) range list [%6"
    8042                 :             :                            PRIxMAX "]%s\n",
    8043                 :             :                            (int) (level * 2), "", dwarf_attr_name (attr),
    8044                 :             :                            dwarf_form_name (form), (uintmax_t) num,
    8045                 :             :                            nlpt ? "" : " <WARNING offset too big>");
    8046                 :             :                 else
    8047                 :           4 :                   fprintf (out, "           %*s%-20s (%s) range index [%6"
    8048                 :             :                            PRIxMAX "]\n",
    8049                 :             :                            (int) (level * 2), "", dwarf_attr_name (attr),
    8050                 :             :                            dwarf_form_name (form), (uintmax_t) num);
    8051                 :             :               }
    8052                 :             :           }
    8053                 :             :           return DWARF_CB_OK;
    8054                 :             : 
    8055                 :           8 :         case DW_AT_rnglists_base:
    8056                 :             :           {
    8057                 :          16 :             bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
    8058                 :           8 :                                         cbargs->addrsize, cbargs->offset_size,
    8059                 :             :                                         cbargs->cu, num, attr);
    8060         [ +  + ]:           8 :             if (!cbargs->silent)
    8061         [ -  + ]:           4 :               fprintf (out, "           %*s%-20s (%s) range list [%6"
    8062                 :             :                        PRIxMAX "]%s\n",
    8063                 :             :                        (int) (level * 2), "", dwarf_attr_name (attr),
    8064                 :             :                        dwarf_form_name (form), (uintmax_t) num,
    8065                 :             :                        nlpt ? "" : " <WARNING offset too big>");
    8066                 :             :           }
    8067                 :             :           return DWARF_CB_OK;
    8068                 :             : 
    8069                 :          34 :         case DW_AT_addr_base:
    8070                 :             :         case DW_AT_GNU_addr_base:
    8071                 :             :           {
    8072                 :          68 :             bool addrbase = notice_listptr (section_addr, &known_addrbases,
    8073                 :          34 :                                             cbargs->addrsize,
    8074                 :          34 :                                             cbargs->offset_size,
    8075                 :             :                                             cbargs->cu, num, attr);
    8076         [ +  + ]:          34 :             if (!cbargs->silent)
    8077         [ -  + ]:          18 :               fprintf (out, "           %*s%-20s (%s) address base [%6"
    8078                 :             :                        PRIxMAX "]%s\n",
    8079                 :             :                        (int) (level * 2), "", dwarf_attr_name (attr),
    8080                 :             :                        dwarf_form_name (form), (uintmax_t) num,
    8081                 :             :                        addrbase ? "" : " <WARNING offset too big>");
    8082                 :             :           }
    8083                 :             :           return DWARF_CB_OK;
    8084                 :             : 
    8085                 :           8 :         case DW_AT_str_offsets_base:
    8086                 :             :           {
    8087                 :          16 :             bool stroffbase = notice_listptr (section_str, &known_stroffbases,
    8088                 :           8 :                                               cbargs->addrsize,
    8089                 :           8 :                                               cbargs->offset_size,
    8090                 :             :                                               cbargs->cu, num, attr);
    8091         [ +  - ]:           8 :             if (!cbargs->silent)
    8092         [ -  + ]:           8 :               fprintf (out, "           %*s%-20s (%s) str offsets base [%6"
    8093                 :             :                        PRIxMAX "]%s\n",
    8094                 :             :                        (int) (level * 2), "", dwarf_attr_name (attr),
    8095                 :             :                        dwarf_form_name (form), (uintmax_t) num,
    8096                 :             :                        stroffbase ? "" : " <WARNING offset too big>");
    8097                 :             :           }
    8098                 :             :           return DWARF_CB_OK;
    8099                 :             : 
    8100                 :        3736 :         case DW_AT_language:
    8101                 :        3736 :           valuestr = dwarf_lang_name (num);
    8102                 :        3736 :           break;
    8103                 :           4 :         case DW_AT_language_name:
    8104                 :           4 :           valuestr = dwarf_lname_name (num);
    8105                 :           4 :           break;
    8106                 :       59402 :         case DW_AT_encoding:
    8107                 :       59402 :           valuestr = dwarf_encoding_name (num);
    8108                 :       59402 :           break;
    8109                 :           0 :         case DW_AT_accessibility:
    8110                 :           0 :           valuestr = dwarf_access_name (num);
    8111                 :           0 :           break;
    8112                 :           0 :         case DW_AT_defaulted:
    8113                 :           0 :           valuestr = dwarf_defaulted_name (num);
    8114                 :           0 :           break;
    8115                 :           0 :         case DW_AT_visibility:
    8116                 :           0 :           valuestr = dwarf_visibility_name (num);
    8117                 :           0 :           break;
    8118                 :           0 :         case DW_AT_virtuality:
    8119                 :           0 :           valuestr = dwarf_virtuality_name (num);
    8120                 :           0 :           break;
    8121                 :           0 :         case DW_AT_identifier_case:
    8122                 :           0 :           valuestr = dwarf_identifier_case_name (num);
    8123                 :           0 :           break;
    8124                 :           0 :         case DW_AT_calling_convention:
    8125                 :           0 :           valuestr = dwarf_calling_convention_name (num);
    8126                 :           0 :           break;
    8127                 :        7270 :         case DW_AT_inline:
    8128                 :        7270 :           valuestr = dwarf_inline_name (num);
    8129                 :        7270 :           break;
    8130                 :           0 :         case DW_AT_ordering:
    8131                 :           0 :           valuestr = dwarf_ordering_name (num);
    8132                 :           0 :           break;
    8133                 :      991722 :         case DW_AT_decl_file:
    8134                 :             :         case DW_AT_call_file:
    8135                 :             :           {
    8136         [ +  + ]:      991722 :             if (cbargs->silent)
    8137                 :             :               break;
    8138                 :             : 
    8139                 :             :             /* Try to get the actual file, the current interface only
    8140                 :             :                gives us full paths, but we only want to show the file
    8141                 :             :                name for now.  */
    8142                 :      990874 :             Dwarf_Die cudie;
    8143         [ +  - ]:      990874 :             if (dwarf_cu_die (cbargs->cu, &cudie,
    8144                 :             :                               NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
    8145                 :             :               {
    8146                 :      990874 :                 Dwarf_Files *files;
    8147                 :      990874 :                 size_t nfiles;
    8148         [ +  - ]:      990874 :                 if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
    8149                 :             :                   {
    8150                 :      990874 :                     valuestr = dwarf_filesrc (files, num, NULL, NULL);
    8151         [ +  - ]:      990874 :                     if (valuestr != NULL)
    8152                 :             :                       {
    8153                 :      990874 :                         char *filename = strrchr (valuestr, '/');
    8154         [ -  + ]:      990874 :                         if (filename != NULL)
    8155                 :      990874 :                           valuestr = filename + 1;
    8156                 :             :                       }
    8157                 :             :                     else
    8158                 :           0 :                       error (0, 0, _("invalid file (%" PRId64 "): %s"),
    8159                 :             :                              num, dwarf_errmsg (-1));
    8160                 :             :                   }
    8161                 :             :                 else
    8162                 :           0 :                   error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
    8163                 :             :                          dwarf_dieoffset (&cudie));
    8164                 :             :               }
    8165                 :             :             else
    8166                 :           0 :              error (0, 0, _("couldn't get DWARF CU: %s"),
    8167                 :             :                     dwarf_errmsg (-1));
    8168                 :      990874 :             if (valuestr == NULL)
    8169                 :             :               valuestr = "???";
    8170                 :             :           }
    8171                 :      990874 :           break;
    8172                 :          26 :         case DW_AT_GNU_dwo_id:
    8173                 :          26 :           as_hex_id = true;
    8174                 :          26 :           break;
    8175                 :             : 
    8176                 :             :         default:
    8177                 :             :           /* Nothing.  */
    8178                 :             :           break;
    8179                 :             :         }
    8180                 :             : 
    8181         [ +  + ]:     3693466 :       if (cbargs->silent)
    8182                 :             :         break;
    8183                 :             : 
    8184                 :             :       /* When highpc is in constant form it is relative to lowpc.
    8185                 :             :          In that case also show the address.  */
    8186                 :     3689756 :       Dwarf_Addr highpc;
    8187   [ +  +  +  - ]:     3689756 :       if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
    8188                 :             :         {
    8189                 :       23992 :           fprintf (out, "           %*s%-20s (%s) %" PRIuMAX " (",
    8190                 :             :                    (int) (level * 2), "", dwarf_attr_name (attr),
    8191                 :             :                    dwarf_form_name (form), (uintmax_t) num);
    8192                 :       23992 :           print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
    8193                 :             :                             highpc, highpc, out);
    8194                 :       23992 :           fprintf (out, ")\n");
    8195                 :             :         }
    8196                 :             :       else
    8197                 :             :         {
    8198         [ +  + ]:     3665764 :           if (as_hex_id)
    8199                 :             :             {
    8200                 :           4 :               fprintf (out, "           %*s%-20s (%s) 0x%.16" PRIx64 "\n",
    8201                 :             :                        (int) (level * 2), "", dwarf_attr_name (attr),
    8202                 :             :                        dwarf_form_name (form), num);
    8203                 :             :             }
    8204                 :             :           else
    8205                 :             :             {
    8206                 :     3665760 :               Dwarf_Sword snum = 0;
    8207                 :     3665760 :               bool is_signed;
    8208                 :     3665760 :               int bytes = 0;
    8209         [ +  + ]:     3665760 :               if (attr == DW_AT_const_value)
    8210                 :      352966 :                 die_type_sign_bytes (die, &is_signed, &bytes);
    8211                 :             :               else
    8212                 :     3312794 :                 is_signed = (form == DW_FORM_sdata
    8213                 :     3312794 :                              || form == DW_FORM_implicit_const);
    8214                 :             : 
    8215         [ +  + ]:     3665760 :               if (is_signed)
    8216         [ -  + ]:      381694 :                 if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
    8217                 :           0 :                   goto attrval_out;
    8218                 :             : 
    8219         [ +  + ]:     3665760 :               if (valuestr == NULL)
    8220                 :             :                 {
    8221                 :     2605084 :                   fprintf (out, "           %*s%-20s (%s) ",
    8222                 :             :                            (int) (level * 2), "", dwarf_attr_name (attr),
    8223                 :             :                            dwarf_form_name (form));
    8224                 :             :                 }
    8225                 :             :               else
    8226                 :             :                 {
    8227                 :     1060676 :                   fprintf (out, "           %*s%-20s (%s) %s (",
    8228                 :             :                            (int) (level * 2), "", dwarf_attr_name (attr),
    8229                 :             :                            dwarf_form_name (form), valuestr);
    8230                 :             :                 }
    8231                 :             : 
    8232   [ +  +  +  +  :     3665760 :               switch (bytes)
                      + ]
    8233                 :             :                 {
    8234                 :          10 :                 case 1:
    8235         [ +  + ]:          10 :                   if (is_signed)
    8236                 :           2 :                     fprintf (out, "%" PRId8, (int8_t) snum);
    8237                 :             :                   else
    8238                 :           8 :                     fprintf (out, "%" PRIu8, (uint8_t) num);
    8239                 :             :                   break;
    8240                 :             : 
    8241                 :           4 :                 case 2:
    8242         [ +  + ]:           4 :                   if (is_signed)
    8243                 :           2 :                     fprintf (out, "%" PRId16, (int16_t) snum);
    8244                 :             :                   else
    8245                 :           2 :                     fprintf (out, "%" PRIu16, (uint16_t) num);
    8246                 :             :                   break;
    8247                 :             : 
    8248                 :         234 :                 case 4:
    8249         [ +  + ]:         234 :                   if (is_signed)
    8250                 :         210 :                     fprintf (out, "%" PRId32, (int32_t) snum);
    8251                 :             :                   else
    8252                 :          24 :                     fprintf (out, "%" PRIu32, (uint32_t) num);
    8253                 :             :                   break;
    8254                 :             : 
    8255                 :         688 :                 case 8:
    8256         [ +  + ]:         688 :                   if (is_signed)
    8257                 :           2 :                     fprintf (out, "%" PRId64, (int64_t) snum);
    8258                 :             :                   else
    8259                 :         686 :                     fprintf (out, "%" PRIu64, (uint64_t) num);
    8260                 :             :                   break;
    8261                 :             : 
    8262                 :     3664824 :                 default:
    8263         [ +  + ]:     3664824 :                   if (is_signed)
    8264                 :      381478 :                     fprintf (out, "%" PRIdMAX, (intmax_t) snum);
    8265                 :             :                   else
    8266                 :     3283346 :                     fprintf (out, "%" PRIuMAX, (uintmax_t) num);
    8267                 :             :                   break;
    8268                 :             :                 }
    8269                 :             : 
    8270                 :             :               /* Make clear if we switched from a signed encoding to
    8271                 :             :                  an unsigned value.  */
    8272         [ +  + ]:     3665760 :               if (attr == DW_AT_const_value
    8273         [ +  + ]:      352966 :                   && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
    8274         [ +  + ]:        2650 :                   && !is_signed)
    8275                 :        2632 :                 fprintf (out, " (%" PRIdMAX ")", (intmax_t) num);
    8276                 :             : 
    8277         [ +  + ]:     3665760 :               if (valuestr == NULL)
    8278                 :     2605084 :                 fprintf (out, "\n");
    8279                 :             :               else
    8280                 :     1060676 :                 fprintf (out, ")\n");
    8281                 :             :             }
    8282                 :             :         }
    8283                 :             :       break;
    8284                 :             : 
    8285                 :       18244 :     case DW_FORM_flag:
    8286         [ +  + ]:       18244 :       if (cbargs->silent)
    8287                 :             :         break;
    8288                 :       18096 :       bool flag;
    8289         [ -  + ]:       18096 :       if (unlikely (dwarf_formflag (attrp, &flag) != 0))
    8290                 :           0 :         goto attrval_out;
    8291                 :             : 
    8292                 :       18096 :       fprintf (out, "           %*s%-20s (%s) %s\n",
    8293                 :             :                (int) (level * 2), "", dwarf_attr_name (attr),
    8294         [ +  - ]:       18096 :                dwarf_form_name (form), flag ? yes_str : no_str);
    8295                 :       18096 :       break;
    8296                 :             : 
    8297                 :      160704 :     case DW_FORM_flag_present:
    8298         [ +  + ]:      160704 :       if (cbargs->silent)
    8299                 :             :         break;
    8300                 :      160150 :       fprintf (out, "           %*s%-20s (%s) %s\n",
    8301                 :             :                (int) (level * 2), "", dwarf_attr_name (attr),
    8302                 :             :                dwarf_form_name (form), yes_str);
    8303                 :      160150 :       break;
    8304                 :             : 
    8305                 :      223634 :     case DW_FORM_exprloc:
    8306                 :             :     case DW_FORM_block4:
    8307                 :             :     case DW_FORM_block2:
    8308                 :             :     case DW_FORM_block1:
    8309                 :             :     case DW_FORM_block:
    8310                 :             :     case DW_FORM_data16: /* DWARF5 calls this a constant class.  */
    8311         [ +  + ]:      223634 :       if (cbargs->silent)
    8312                 :             :         break;
    8313                 :      223372 :       Dwarf_Block block;
    8314         [ -  + ]:      223372 :       if (unlikely (dwarf_formblock (attrp, &block) != 0))
    8315                 :           0 :         goto attrval_out;
    8316                 :             : 
    8317                 :      223372 :       fprintf (out, "           %*s%-20s (%s) ",
    8318                 :             :                (int) (level * 2), "", dwarf_attr_name (attr),
    8319                 :             :                dwarf_form_name (form));
    8320                 :             : 
    8321      [ +  +  + ]:      223372 :       switch (attr)
    8322                 :             :         {
    8323                 :       96332 :         default:
    8324         [ -  + ]:       96332 :           if (form != DW_FORM_exprloc)
    8325                 :             :             {
    8326                 :           0 :               print_block (block.length, block.data, out);
    8327                 :           0 :               break;
    8328                 :             :             }
    8329                 :      127032 :           FALLTHROUGH;
    8330                 :             : 
    8331                 :             :         case DW_AT_location:
    8332                 :             :         case DW_AT_data_location:
    8333                 :             :         case DW_AT_data_member_location:
    8334                 :             :         case DW_AT_vtable_elem_location:
    8335                 :             :         case DW_AT_string_length:
    8336                 :             :         case DW_AT_use_location:
    8337                 :             :         case DW_AT_frame_base:
    8338                 :             :         case DW_AT_return_addr:
    8339                 :             :         case DW_AT_static_link:
    8340                 :             :         case DW_AT_allocated:
    8341                 :             :         case DW_AT_associated:
    8342                 :             :         case DW_AT_bit_size:
    8343                 :             :         case DW_AT_bit_offset:
    8344                 :             :         case DW_AT_bit_stride:
    8345                 :             :         case DW_AT_byte_size:
    8346                 :             :         case DW_AT_byte_stride:
    8347                 :             :         case DW_AT_count:
    8348                 :             :         case DW_AT_lower_bound:
    8349                 :             :         case DW_AT_upper_bound:
    8350                 :             :         case DW_AT_GNU_call_site_value:
    8351                 :             :         case DW_AT_GNU_call_site_data_value:
    8352                 :             :         case DW_AT_GNU_call_site_target:
    8353                 :             :         case DW_AT_GNU_call_site_target_clobbered:
    8354         [ +  + ]:      127032 :           if (form == DW_FORM_exprloc
    8355         [ +  - ]:         140 :               || (form != DW_FORM_data16
    8356         [ +  - ]:         140 :                   && attrp->cu->version < 4)) /* blocks were expressions.  */
    8357                 :             :             {
    8358                 :      223364 :               fputc ('\n', out);
    8359                 :      223364 :               print_ops (cbargs->dwflmod, cbargs->dbg,
    8360                 :      223364 :                          12 + level * 2, 12 + level * 2,
    8361                 :             :                          cbargs->version, cbargs->addrsize, cbargs->offset_size,
    8362                 :      223364 :                          attrp->cu, block.length, block.data, out);
    8363                 :             :             }
    8364                 :             :           else
    8365                 :           0 :             print_block (block.length, block.data, out);
    8366                 :             :           break;
    8367                 :             : 
    8368                 :           8 :         case DW_AT_discr_list:
    8369         [ -  + ]:           8 :           if (block.length == 0)
    8370                 :           0 :             fputs ("<default>\n", out);
    8371         [ +  - ]:           8 :           else if (form != DW_FORM_data16)
    8372                 :             :             {
    8373                 :           8 :               const unsigned char *readp = block.data;
    8374                 :           8 :               const unsigned char *readendp = readp + block.length;
    8375                 :             : 
    8376                 :             :               /* See if we are dealing with a signed or unsigned
    8377                 :             :                  values.  If the parent of this variant DIE is a
    8378                 :             :                  variant_part then it will either have a discriminant
    8379                 :             :                  which points to the member which type is the
    8380                 :             :                  discriminant type.  Or the variant_part itself has a
    8381                 :             :                  type representing the discriminant.  */
    8382                 :           8 :               bool is_signed = false;
    8383         [ +  - ]:           8 :               if (level > 0)
    8384                 :             :                 {
    8385                 :           8 :                   Dwarf_Die *parent = &cbargs->dies[level - 1];
    8386         [ +  - ]:           8 :                   if (dwarf_tag (die) == DW_TAG_variant
    8387         [ +  - ]:           8 :                       && dwarf_tag (parent) == DW_TAG_variant_part)
    8388                 :             :                     {
    8389                 :           8 :                       Dwarf_Die member;
    8390                 :           8 :                       Dwarf_Attribute discr_attr;
    8391                 :           8 :                       int bytes;
    8392         [ +  - ]:           8 :                       if (dwarf_formref_die (dwarf_attr (parent,
    8393                 :             :                                                          DW_AT_discr,
    8394                 :             :                                                          &discr_attr),
    8395                 :             :                                              &member) != NULL)
    8396                 :           8 :                         die_type_sign_bytes (&member, &is_signed, &bytes);
    8397                 :             :                       else
    8398                 :           0 :                         die_type_sign_bytes (parent, &is_signed, &bytes);
    8399                 :             :                     }
    8400                 :             :                 }
    8401         [ +  + ]:          24 :               while (readp < readendp)
    8402                 :             :                 {
    8403                 :          16 :                   int d = (int) *readp++;
    8404                 :          16 :                   fprintf (out, "%s ", dwarf_discr_list_name (d));
    8405         [ -  + ]:          16 :                   if (readp >= readendp)
    8406                 :           0 :                     goto attrval_out;
    8407                 :             : 
    8408                 :          16 :                   Dwarf_Word val;
    8409                 :          16 :                   Dwarf_Sword sval;
    8410         [ +  + ]:          16 :                   if (d == DW_DSC_label)
    8411                 :             :                     {
    8412         [ +  + ]:           8 :                       if (is_signed)
    8413                 :             :                         {
    8414                 :           4 :                           get_sleb128 (sval, readp, readendp);
    8415                 :           4 :                           fprintf (out, "%" PRId64 "", sval);
    8416                 :             :                         }
    8417                 :             :                       else
    8418                 :             :                         {
    8419                 :           4 :                           get_uleb128 (val, readp, readendp);
    8420                 :           4 :                           fprintf (out, "%" PRIu64 "", val);
    8421                 :             :                         }
    8422                 :             :                     }
    8423         [ +  - ]:           8 :                   else if (d == DW_DSC_range)
    8424                 :             :                     {
    8425         [ +  + ]:           8 :                       if (is_signed)
    8426                 :             :                         {
    8427                 :           6 :                           get_sleb128 (sval, readp, readendp);
    8428                 :           6 :                           fprintf (out, "%" PRId64 "..", sval);
    8429         [ -  + ]:           6 :                           if (readp >= readendp)
    8430                 :           0 :                             goto attrval_out;
    8431                 :           6 :                           get_sleb128 (sval, readp, readendp);
    8432                 :           6 :                           fprintf (out, "%" PRId64 "", sval);
    8433                 :             :                         }
    8434                 :             :                       else
    8435                 :             :                         {
    8436                 :           2 :                           get_uleb128 (val, readp, readendp);
    8437                 :           2 :                           fprintf (out, "%" PRIu64 "..", val);
    8438         [ -  + ]:           2 :                           if (readp >= readendp)
    8439                 :           0 :                             goto attrval_out;
    8440                 :           2 :                           get_uleb128 (val, readp, readendp);
    8441                 :           2 :                           fprintf (out, "%" PRIu64 "", val);
    8442                 :             :                         }
    8443                 :             :                     }
    8444                 :             :                   else
    8445                 :             :                     {
    8446                 :           0 :                       print_block (readendp - readp, readp, out);
    8447                 :           0 :                       break;
    8448                 :             :                     }
    8449         [ +  + ]:          16 :                   if (readp < readendp)
    8450                 :           8 :                     fprintf (out, ", ");
    8451                 :             :                 }
    8452                 :           8 :               fputc ('\n', out);
    8453                 :             :             }
    8454                 :             :           else
    8455                 :           0 :             print_block (block.length, block.data, out);
    8456                 :             :           break;
    8457                 :             :         }
    8458                 :             :       break;
    8459                 :             : 
    8460                 :           0 :     default:
    8461         [ #  # ]:           0 :       if (cbargs->silent)
    8462                 :             :         break;
    8463                 :           0 :       fprintf (out, "           %*s%-20s (%s) ???\n",
    8464                 :             :                (int) (level * 2), "", dwarf_attr_name (attr),
    8465                 :             :                dwarf_form_name (form));
    8466                 :           0 :       break;
    8467                 :             :     }
    8468                 :             : 
    8469                 :     7298310 :   return DWARF_CB_OK;
    8470                 :             : }
    8471                 :             : 
    8472                 :             : static void
    8473                 :         208 : print_debug_units (Dwfl_Module *dwflmod,
    8474                 :             :                    Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
    8475                 :             :                    Elf_Scn *scn, GElf_Shdr *shdr,
    8476                 :             :                    Dwarf *dbg, bool debug_types, FILE *out)
    8477                 :             : {
    8478   [ +  +  +  + ]:         208 :   const bool silent = !(print_debug_sections & section_info) && !debug_types;
    8479                 :         208 :   const char *secname = section_name (ebl, shdr);
    8480                 :             : 
    8481                 :             :   /* Check section actually exists.  */
    8482         [ +  + ]:         208 :   if (!silent)
    8483         [ +  - ]:         136 :     if (get_debug_elf_data (dbg, ebl,
    8484                 :             :                             debug_types ? IDX_debug_types : IDX_debug_info,
    8485                 :             :                             scn) == NULL)
    8486                 :           0 :       return;
    8487                 :             : 
    8488                 :         136 :   if (!silent)
    8489                 :         136 :     fprintf (out, _("\
    8490                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
    8491                 :         136 :              elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
    8492                 :             : 
    8493                 :             :   /* If the section is empty we don't have to do anything.  */
    8494   [ +  +  +  - ]:         208 :   if (!silent && shdr->sh_size == 0)
    8495                 :             :     return;
    8496                 :             : 
    8497                 :         208 :   int maxdies = 20;
    8498                 :         208 :   Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
    8499                 :             : 
    8500                 :             :   /* New compilation unit.  */
    8501                 :         208 :   Dwarf_Half version;
    8502                 :             : 
    8503                 :         208 :   Dwarf_Die result;
    8504                 :         208 :   Dwarf_Off abbroffset;
    8505                 :         208 :   uint8_t addrsize;
    8506                 :         208 :   uint8_t offsize;
    8507                 :         208 :   uint64_t unit_id;
    8508                 :         208 :   Dwarf_Off subdie_off;
    8509                 :             : 
    8510                 :         208 :   int unit_res;
    8511                 :         208 :   Dwarf_CU *cu;
    8512                 :         208 :   Dwarf_CU cu_mem;
    8513                 :         208 :   uint8_t unit_type;
    8514                 :         208 :   Dwarf_Die cudie;
    8515                 :             : 
    8516                 :             :   /* We cheat a little because we want to see only the CUs from .debug_info
    8517                 :             :      or .debug_types.  We know the Dwarf_CU struct layout.  Set it up at
    8518                 :             :      the end of .debug_info if we want .debug_types only.  Check the returned
    8519                 :             :      Dwarf_CU is still in the expected section.  */
    8520         [ +  + ]:         208 :   if (debug_types)
    8521                 :             :     {
    8522                 :           2 :       cu_mem.dbg = dbg;
    8523                 :           2 :       cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
    8524                 :           2 :       cu_mem.sec_idx = IDX_debug_info;
    8525                 :           2 :       cu = &cu_mem;
    8526                 :             :     }
    8527                 :             :   else
    8528                 :         206 :     cu = NULL;
    8529                 :             : 
    8530                 :             :  next_cu:
    8531                 :        3962 :   unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
    8532                 :             :                               &cudie, NULL);
    8533         [ +  + ]:        3962 :   if (unit_res == 1)
    8534                 :         206 :     goto do_return;
    8535                 :             : 
    8536         [ -  + ]:        3756 :   if (unit_res == -1)
    8537                 :             :     {
    8538         [ #  # ]:           0 :       if (!silent)
    8539                 :           0 :         error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
    8540                 :           0 :       goto do_return;
    8541                 :             :     }
    8542                 :             : 
    8543   [ +  +  +  + ]:        7508 :   if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
    8544                 :           2 :     goto do_return;
    8545                 :             : 
    8546                 :        3754 :   dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
    8547                 :             :                 &unit_id, &subdie_off);
    8548                 :             : 
    8549         [ +  + ]:        3754 :   if (!silent)
    8550                 :             :     {
    8551                 :        3654 :       Dwarf_Off offset = cu->start;
    8552   [ +  +  +  - ]:        3654 :       if (debug_types && version < 5)
    8553                 :           4 :         {
    8554                 :           4 :           Dwarf_Die typedie;
    8555                 :           4 :           Dwarf_Off dieoffset;
    8556                 :           4 :           dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
    8557                 :             :                                                            + subdie_off,
    8558                 :             :                                                            &typedie));
    8559                 :           4 :           fprintf (out, _(" Type unit at offset %" PRIu64 ":\n"
    8560                 :             :                             " Version: %" PRIu16
    8561                 :             :                             ", Abbreviation section offset: %" PRIu64
    8562                 :             :                             ", Address size: %" PRIu8
    8563                 :             :                             ", Offset size: %" PRIu8
    8564                 :             :                             "\n Type signature: %#" PRIx64
    8565                 :             :                             ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
    8566                 :             :                    (uint64_t) offset, version, abbroffset, addrsize, offsize,
    8567                 :             :                    unit_id, (uint64_t) subdie_off, dieoffset);
    8568                 :             :         }
    8569                 :             :       else
    8570                 :             :         {
    8571                 :        3650 :           fprintf (out, _(" Compilation unit at offset %" PRIu64 ":\n"
    8572                 :             :                             " Version: %" PRIu16
    8573                 :             :                             ", Abbreviation section offset: %" PRIu64
    8574                 :             :                             ", Address size: %" PRIu8
    8575                 :             :                             ", Offset size: %" PRIu8 "\n"),
    8576                 :             :                    (uint64_t) offset, version, abbroffset, addrsize, offsize);
    8577                 :             : 
    8578         [ +  + ]:        3650 :           if (version >= 5 || (unit_type != DW_UT_compile
    8579         [ +  + ]:         170 :                                && unit_type != DW_UT_partial))
    8580                 :             :             {
    8581                 :        3482 :               fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
    8582                 :             :                                 dwarf_unit_name (unit_type), unit_type);
    8583                 :        3482 :               if (unit_type == DW_UT_type
    8584         [ +  + ]:        3482 :                   || unit_type == DW_UT_skeleton
    8585         [ +  - ]:        3472 :                   || unit_type == DW_UT_split_compile
    8586         [ -  + ]:        3472 :                   || unit_type == DW_UT_split_type)
    8587                 :          10 :                 fprintf (out, ", Unit id: 0x%.16" PRIx64 "", unit_id);
    8588                 :        3482 :               if (unit_type == DW_UT_type
    8589         [ -  + ]:        3482 :                   || unit_type == DW_UT_split_type)
    8590                 :             :                 {
    8591                 :           0 :                   Dwarf_Die typedie;
    8592                 :           0 :                   Dwarf_Off dieoffset;
    8593                 :           0 :                   dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
    8594                 :             :                                  NULL, NULL, NULL);
    8595                 :           0 :                   dieoffset = dwarf_dieoffset (&typedie);
    8596                 :           0 :                   fprintf (out, ", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
    8597                 :             :                            subdie_off, dieoffset);
    8598                 :             :                 }
    8599                 :        3482 :               fprintf (out, "\n");
    8600                 :             :             }
    8601                 :             :         }
    8602                 :             :     }
    8603                 :             : 
    8604         [ +  - ]:        3754 :   if (version < 2 || version > 5
    8605   [ +  -  -  + ]:        3754 :       || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
    8606                 :             :     {
    8607         [ #  # ]:           0 :       if (!silent)
    8608                 :           0 :         error (0, 0, _("unknown version (%d) or unit type (%d)"),
    8609                 :             :                version, unit_type);
    8610                 :           0 :       goto next_cu;
    8611                 :             :     }
    8612                 :             : 
    8613                 :        3754 :   struct attrcb_args args =
    8614                 :             :     {
    8615                 :             :       .dwflmod = dwflmod,
    8616                 :             :       .silent = silent,
    8617                 :             :       .version = version,
    8618                 :             :       .addrsize = addrsize,
    8619                 :             :       .offset_size = offsize
    8620                 :             :     };
    8621                 :             : 
    8622                 :        3754 :   bool is_split = false;
    8623                 :        3754 :   int level = 0;
    8624                 :        3754 :   dies[0] = cudie;
    8625                 :        3754 :   args.cu = dies[0].cu;
    8626                 :        3754 :   args.dbg = dbg;
    8627                 :        3754 :   args.is_split = is_split;
    8628                 :        3754 :   args.out = out;
    8629                 :             : 
    8630                 :             :   /* We might return here again for the split CU subdie.  */
    8631                 :             :   do_cu:
    8632                 :     2127964 :   do
    8633                 :             :     {
    8634                 :     2127964 :       Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
    8635         [ -  + ]:     2127964 :       if (unlikely (offset == (Dwarf_Off) -1))
    8636                 :             :         {
    8637         [ #  # ]:           0 :           if (!silent)
    8638                 :           0 :             error (0, 0, _("cannot get DIE offset: %s"),
    8639                 :             :                    dwarf_errmsg (-1));
    8640                 :           0 :           goto do_return;
    8641                 :             :         }
    8642                 :             : 
    8643                 :     2127964 :       int tag = dwarf_tag (&dies[level]);
    8644         [ -  + ]:     2127964 :       if (unlikely (tag == DW_TAG_invalid))
    8645                 :             :         {
    8646         [ #  # ]:           0 :           if (!silent)
    8647                 :           0 :             error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
    8648                 :             :                                   "] in section '%s': %s"),
    8649                 :             :                    (uint64_t) offset, secname, dwarf_errmsg (-1));
    8650                 :           0 :           goto do_return;
    8651                 :             :         }
    8652                 :             : 
    8653         [ +  + ]:     2127964 :       if (!silent)
    8654                 :             :         {
    8655                 :     2125978 :           unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
    8656         [ +  + ]:     2125978 :           if (is_split)
    8657                 :          96 :             fprintf (out, " {%6" PRIx64 "}  ", (uint64_t) offset);
    8658                 :             :           else
    8659                 :     2125882 :             fprintf (out, " [%6" PRIx64 "]  ", (uint64_t) offset);
    8660                 :     2125978 :           fprintf (out, "%*s%-20s abbrev: %u\n", (int) (level * 2), "",
    8661                 :             :                    dwarf_tag_name (tag), code);
    8662                 :             :         }
    8663                 :             : 
    8664                 :             :       /* Print the attribute values.  */
    8665                 :     2127964 :       args.level = level;
    8666                 :     2127964 :       args.dies = dies;
    8667                 :     2127964 :       (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
    8668                 :             : 
    8669                 :             :       /* Make room for the next level's DIE.  */
    8670         [ -  + ]:     2127964 :       if (level + 1 == maxdies)
    8671                 :           0 :         dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
    8672                 :             : 
    8673                 :     2127964 :       int res = dwarf_child (&dies[level], &dies[level + 1]);
    8674         [ +  + ]:     2127964 :       if (res > 0)
    8675                 :             :         {
    8676         [ +  + ]:     2127964 :           while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
    8677         [ +  + ]:      295440 :             if (level-- == 0)
    8678                 :             :               break;
    8679                 :             : 
    8680         [ -  + ]:     1836286 :           if (unlikely (res == -1))
    8681                 :             :             {
    8682         [ #  # ]:           0 :               if (!silent)
    8683                 :           0 :                 error (0, 0, _("cannot get next DIE: %s\n"),
    8684                 :             :                        dwarf_errmsg (-1));
    8685                 :           0 :               goto do_return;
    8686                 :             :             }
    8687                 :             :         }
    8688         [ -  + ]:      291678 :       else if (unlikely (res < 0))
    8689                 :             :         {
    8690         [ #  # ]:           0 :           if (!silent)
    8691                 :           0 :             error (0, 0, _("cannot get next DIE: %s"),
    8692                 :             :                    dwarf_errmsg (-1));
    8693                 :           0 :           goto do_return;
    8694                 :             :         }
    8695                 :             :       else
    8696                 :             :         ++level;
    8697                 :             :     }
    8698         [ +  + ]:     2127964 :   while (level >= 0);
    8699                 :             : 
    8700                 :             :   /* We might want to show the split compile unit if this was a skeleton.
    8701                 :             :      We need to scan it if we are requesting printing .debug_ranges for
    8702                 :             :      DWARF4 since GNU DebugFission uses "offsets" into the main ranges
    8703                 :             :      section.  */
    8704         [ +  + ]:        3762 :   if (unit_type == DW_UT_skeleton
    8705   [ +  +  +  + ]:          26 :       && ((!silent && show_split_units)
    8706   [ +  +  +  + ]:          20 :           || (version < 5 && (print_debug_sections & section_ranges) != 0)))
    8707                 :             :     {
    8708                 :          10 :       Dwarf_Die subdie;
    8709         [ +  - ]:          10 :       if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
    8710         [ +  + ]:          10 :           || dwarf_tag (&subdie) == DW_TAG_invalid)
    8711                 :             :         {
    8712         [ +  - ]:           2 :           if (!silent)
    8713                 :             :             {
    8714                 :           2 :               Dwarf_Attribute dwo_at;
    8715                 :           4 :               const char *dwo_name =
    8716                 :           2 :                 (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
    8717                 :             :                                                &dwo_at))
    8718         [ -  + ]:           2 :                  ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
    8719                 :             :                                                    &dwo_at))
    8720         [ #  # ]:           0 :                      ?: "<unknown>"));
    8721                 :           2 :               fprintf (stderr,
    8722                 :             :                        "Could not find split unit '%s', id: %" PRIx64 "\n",
    8723                 :             :                        dwo_name, unit_id);
    8724                 :             :             }
    8725                 :             :         }
    8726                 :             :       else
    8727                 :             :         {
    8728                 :           8 :           Dwarf_CU *split_cu = subdie.cu;
    8729                 :           8 :           dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
    8730                 :             :                         &addrsize, &offsize, &unit_id, &subdie_off);
    8731                 :           8 :           Dwarf_Off offset = cu->start;
    8732                 :             : 
    8733         [ +  + ]:           8 :           if (!silent)
    8734                 :             :             {
    8735                 :           4 :               fprintf (out, _(" Split compilation unit at offset %"
    8736                 :             :                                 PRIu64 ":\n"
    8737                 :             :                                 " Version: %" PRIu16
    8738                 :             :                                 ", Abbreviation section offset: %" PRIu64
    8739                 :             :                                 ", Address size: %" PRIu8
    8740                 :             :                                 ", Offset size: %" PRIu8 "\n"),
    8741                 :             :                        (uint64_t) offset, version, abbroffset,
    8742                 :             :                        addrsize, offsize);
    8743                 :           4 :               fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
    8744                 :             :                        dwarf_unit_name (unit_type), unit_type);
    8745                 :           4 :               fprintf (out, ", Unit id: 0x%.16" PRIx64 "", unit_id);
    8746                 :           4 :               fprintf (out, "\n");
    8747                 :             :             }
    8748                 :             : 
    8749                 :           8 :           unit_type = DW_UT_split_compile;
    8750                 :           8 :           is_split = true;
    8751                 :           8 :           level = 0;
    8752                 :           8 :           dies[0] = subdie;
    8753                 :           8 :           args.cu = dies[0].cu;
    8754                 :           8 :           args.dbg = split_cu->dbg;
    8755                 :           8 :           args.is_split = is_split;
    8756                 :           8 :           goto do_cu;
    8757                 :             :         }
    8758                 :             :     }
    8759                 :             : 
    8760                 :             :   /* And again... */
    8761                 :        3754 :   goto next_cu;
    8762                 :             : 
    8763                 :         208 :  do_return:
    8764                 :         208 :   free (dies);
    8765                 :             : }
    8766                 :             : 
    8767                 :             : static void
    8768                 :         206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    8769                 :             :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
    8770                 :             :                           FILE *out)
    8771                 :             : {
    8772                 :          42 :   print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false, out);
    8773                 :          42 : }
    8774                 :             : 
    8775                 :             : static void
    8776                 :           2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    8777                 :             :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
    8778                 :             :                            FILE *out)
    8779                 :             : {
    8780                 :           2 :   print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true, out);
    8781                 :           2 : }
    8782                 :             : 
    8783                 :             : 
    8784                 :             : static void
    8785                 :          82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
    8786                 :             :                             GElf_Ehdr *ehdr __attribute__ ((unused)),
    8787                 :             :                             Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
    8788                 :             :                             FILE *out)
    8789                 :             : {
    8790                 :          82 :   fprintf (out, _("\
    8791                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
    8792                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    8793                 :          82 :            (uint64_t) shdr->sh_offset);
    8794                 :             : 
    8795                 :         164 :   size_t address_size
    8796         [ +  + ]:          82 :     = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    8797                 :             : 
    8798                 :          82 :   Dwarf_Lines *lines;
    8799                 :          82 :   size_t nlines;
    8800                 :          82 :   Dwarf_Off off, next_off = 0;
    8801                 :          82 :   Dwarf_CU *cu = NULL;
    8802                 :          82 :   while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
    8803         [ +  + ]:         176 :                            &lines, &nlines) == 0)
    8804                 :             :     {
    8805                 :          94 :       Dwarf_Die cudie;
    8806   [ +  +  +  - ]:          94 :       if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
    8807                 :             :                                        NULL, NULL, NULL, NULL) == 0)
    8808                 :          50 :         fprintf (out, " CU [%" PRIx64 "] %s\n",
    8809                 :             :                  dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
    8810                 :             :       else
    8811                 :             :         {
    8812                 :             :           /* DWARF5 lines can be independent of any CU, but they probably
    8813                 :             :              are used by some CU.  Determine the CU this block is for.  */
    8814                 :          44 :           Dwarf_Off cuoffset;
    8815                 :          44 :           Dwarf_Off ncuoffset = 0;
    8816                 :          44 :           size_t hsize;
    8817                 :          44 :           while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
    8818         [ +  - ]:          46 :                                NULL, NULL, NULL) == 0)
    8819                 :             :             {
    8820         [ -  + ]:          46 :               if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
    8821                 :           0 :                 continue;
    8822                 :          46 :               Dwarf_Attribute stmt_list;
    8823         [ -  + ]:          46 :               if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
    8824                 :           0 :                 continue;
    8825                 :          46 :               Dwarf_Word lineoff;
    8826         [ -  + ]:          46 :               if (dwarf_formudata (&stmt_list, &lineoff) != 0)
    8827                 :           0 :                 continue;
    8828         [ +  + ]:          46 :               if (lineoff == off)
    8829                 :             :                 {
    8830                 :             :                   /* Found the CU.  */
    8831                 :          44 :                   cu = cudie.cu;
    8832                 :          44 :                   break;
    8833                 :             :                 }
    8834                 :             :             }
    8835                 :             : 
    8836         [ +  - ]:          44 :           if (cu != NULL)
    8837                 :          44 :             fprintf (out, " CU [%" PRIx64 "] %s\n",
    8838                 :             :                      dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
    8839                 :             :           else
    8840                 :           0 :             fprintf (out, " No CU\n");
    8841                 :             :         }
    8842                 :             : 
    8843                 :          94 :       fprintf (out, "  line:col SBPE* disc isa op address"
    8844                 :             :                " (Statement Block Prologue Epilogue *End)\n");
    8845                 :          94 :       const char *last_file = "";
    8846         [ +  + ]:         720 :       for (size_t n = 0; n < nlines; n++)
    8847                 :             :         {
    8848                 :         626 :           Dwarf_Line *line = dwarf_onesrcline (lines, n);
    8849         [ -  + ]:         626 :           if (line == NULL)
    8850                 :             :             {
    8851                 :           0 :               fprintf (out, "  dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
    8852                 :           0 :               continue;
    8853                 :             :             }
    8854                 :         626 :           Dwarf_Word mtime, length;
    8855                 :         626 :           const char *file = dwarf_linesrc (line, &mtime, &length);
    8856         [ -  + ]:         626 :           if (file == NULL)
    8857                 :             :             {
    8858                 :           0 :               fprintf (out, "  <%s> (mtime: ?, length: ?)\n",
    8859                 :             :                        dwarf_errmsg (-1));
    8860                 :           0 :               last_file = "";
    8861                 :             :             }
    8862         [ +  + ]:         626 :           else if (strcmp (last_file, file) != 0)
    8863                 :             :             {
    8864                 :         114 :               fprintf (out, "  %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
    8865                 :             :                        file, mtime, length);
    8866                 :         114 :               last_file = file;
    8867                 :             :             }
    8868                 :             : 
    8869                 :         626 :           int lineno, colno;
    8870                 :         626 :           bool statement, endseq, block, prologue_end, epilogue_begin;
    8871                 :         626 :           unsigned int lineop, isa, disc;
    8872                 :         626 :           Dwarf_Addr address;
    8873                 :         626 :           dwarf_lineaddr (line, &address);
    8874                 :         626 :           dwarf_lineno (line, &lineno);
    8875                 :         626 :           dwarf_linecol (line, &colno);
    8876                 :         626 :           dwarf_lineop_index (line, &lineop);
    8877                 :         626 :           dwarf_linebeginstatement (line, &statement);
    8878                 :         626 :           dwarf_lineendsequence (line, &endseq);
    8879                 :         626 :           dwarf_lineblock (line, &block);
    8880                 :         626 :           dwarf_lineprologueend (line, &prologue_end);
    8881                 :         626 :           dwarf_lineepiloguebegin (line, &epilogue_begin);
    8882                 :         626 :           dwarf_lineisa (line, &isa);
    8883                 :         626 :           dwarf_linediscriminator (line, &disc);
    8884                 :             : 
    8885                 :             :           /* End sequence is special, it is one byte past.  */
    8886                 :         626 :           fprintf (out, "  %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
    8887                 :             :                    lineno, colno,
    8888         [ +  + ]:         626 :                    (statement ? 'S' : ' '),
    8889         [ +  - ]:         626 :                    (block ? 'B' : ' '),
    8890         [ +  + ]:         626 :                    (prologue_end ? 'P' : ' '),
    8891         [ +  - ]:         626 :                    (epilogue_begin ? 'E' : ' '),
    8892         [ +  + ]:         626 :                    (endseq ? '*' : ' '),
    8893                 :             :                    disc, isa, lineop);
    8894                 :         626 :           print_dwarf_addr (dwflmod, address_size,
    8895         [ +  + ]:         626 :                             address - (endseq ? 1 : 0), address, out);
    8896                 :         626 :           fprintf (out, "\n");
    8897                 :             : 
    8898         [ +  + ]:         626 :           if (endseq)
    8899                 :          98 :             fprintf(out, "\n");
    8900                 :             :         }
    8901                 :             :     }
    8902                 :          82 : }
    8903                 :             : 
    8904                 :             : 
    8905                 :             : /* Print the value of a form.
    8906                 :             :    Returns new value of readp, or readendp on failure.  */
    8907                 :             : static const unsigned char *
    8908                 :      143590 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
    8909                 :             :                  const unsigned char *readendp, unsigned int offset_len,
    8910                 :             :                  Dwarf_Off str_offsets_base, FILE *out)
    8911                 :             : {
    8912                 :      143590 :   Dwarf_Word val;
    8913                 :      143590 :   unsigned char *endp;
    8914                 :      143590 :   Elf_Data *data;
    8915                 :      143590 :   char *str;
    8916   [ +  -  -  -  :      143590 :   switch (form)
          -  +  -  -  -  
          -  +  -  -  +  
          -  +  -  -  -  
                   -  - ]
    8917                 :             :     {
    8918                 :          16 :     case DW_FORM_data1:
    8919         [ -  + ]:          16 :       if (readendp - readp < 1)
    8920                 :             :         {
    8921                 :           0 :         invalid_data:
    8922                 :           0 :           error (0, 0, "invalid data");
    8923                 :           0 :           return readendp;
    8924                 :             :         }
    8925                 :          16 :       val = *readp++;
    8926                 :          16 :       fprintf (out, " %" PRIx8, (unsigned int) val);
    8927                 :          16 :       break;
    8928                 :             : 
    8929                 :           0 :     case DW_FORM_data2:
    8930         [ #  # ]:           0 :       if (readendp - readp < 2)
    8931                 :           0 :         goto invalid_data;
    8932         [ #  # ]:           0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    8933                 :           0 :       fprintf(out, " %" PRIx16, (unsigned int) val);
    8934                 :           0 :       break;
    8935                 :             : 
    8936                 :           0 :     case DW_FORM_data4:
    8937         [ #  # ]:           0 :       if (readendp - readp < 4)
    8938                 :           0 :         goto invalid_data;
    8939         [ #  # ]:           0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    8940                 :           0 :       fprintf (out, " %" PRIx32, (unsigned int) val);
    8941                 :           0 :       break;
    8942                 :             : 
    8943                 :           0 :     case DW_FORM_data8:
    8944         [ #  # ]:           0 :       if (readendp - readp < 8)
    8945                 :           0 :         goto invalid_data;
    8946         [ #  # ]:           0 :       val = read_8ubyte_unaligned_inc (dbg, readp);
    8947                 :           0 :       fprintf (out, " %" PRIx64, val);
    8948                 :           0 :       break;
    8949                 :             : 
    8950                 :           0 :     case DW_FORM_sdata:
    8951         [ #  # ]:           0 :       if (readendp - readp < 1)
    8952                 :           0 :         goto invalid_data;
    8953                 :           0 :       get_sleb128 (val, readp, readendp);
    8954                 :           0 :       fprintf (out, " %" PRIx64, val);
    8955                 :           0 :       break;
    8956                 :             : 
    8957                 :       57832 :     case DW_FORM_udata:
    8958         [ -  + ]:       57832 :       if (readendp - readp < 1)
    8959                 :           0 :         goto invalid_data;
    8960                 :       57832 :       get_uleb128 (val, readp, readendp);
    8961                 :       57832 :       fprintf (out, " %" PRIx64, val);
    8962                 :       57832 :       break;
    8963                 :             : 
    8964                 :           0 :     case DW_FORM_block:
    8965         [ #  # ]:           0 :       if (readendp - readp < 1)
    8966                 :           0 :         goto invalid_data;
    8967                 :           0 :       get_uleb128 (val, readp, readendp);
    8968         [ #  # ]:           0 :       if ((size_t) (readendp - readp) < val)
    8969                 :           0 :         goto invalid_data;
    8970                 :           0 :       print_bytes (val, readp, out);
    8971                 :           0 :       readp += val;
    8972                 :           0 :       break;
    8973                 :             : 
    8974                 :           0 :     case DW_FORM_block1:
    8975         [ #  # ]:           0 :       if (readendp - readp < 1)
    8976                 :           0 :         goto invalid_data;
    8977                 :           0 :       val = *readp++;
    8978         [ #  # ]:           0 :       if ((size_t) (readendp - readp) < val)
    8979                 :           0 :         goto invalid_data;
    8980                 :           0 :       print_bytes (val, readp, out);
    8981                 :           0 :       readp += val;
    8982                 :           0 :       break;
    8983                 :             : 
    8984                 :           0 :     case DW_FORM_block2:
    8985         [ #  # ]:           0 :       if (readendp - readp < 2)
    8986                 :           0 :         goto invalid_data;
    8987         [ #  # ]:           0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    8988         [ #  # ]:           0 :       if ((size_t) (readendp - readp) < val)
    8989                 :           0 :         goto invalid_data;
    8990                 :           0 :       print_bytes (val, readp, out);
    8991                 :           0 :       readp += val;
    8992                 :           0 :       break;
    8993                 :             : 
    8994                 :           0 :     case DW_FORM_block4:
    8995         [ #  # ]:           0 :       if (readendp - readp < 4)
    8996                 :           0 :         goto invalid_data;
    8997         [ #  # ]:           0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    8998         [ #  # ]:           0 :       if ((size_t) (readendp - readp) < val)
    8999                 :           0 :         goto invalid_data;
    9000                 :           0 :       print_bytes (val, readp, out);
    9001                 :           0 :       readp += val;
    9002                 :           0 :       break;
    9003                 :             : 
    9004                 :          24 :     case DW_FORM_data16:
    9005         [ -  + ]:          24 :       if (readendp - readp < 16)
    9006                 :           0 :         goto invalid_data;
    9007                 :          24 :       print_bytes (16, readp, out);
    9008                 :          24 :       readp += 16;
    9009                 :          24 :       break;
    9010                 :             : 
    9011                 :           0 :     case DW_FORM_flag:
    9012         [ #  # ]:           0 :       if (readendp - readp < 1)
    9013                 :           0 :         goto invalid_data;
    9014                 :           0 :       val = *readp++;
    9015         [ #  # ]:           0 :       fprintf (out, "%s", val != 0 ? yes_str : no_str);
    9016                 :           0 :       break;
    9017                 :             : 
    9018                 :           0 :     case DW_FORM_string:
    9019                 :           0 :       endp = memchr (readp, '\0', readendp - readp);
    9020         [ #  # ]:           0 :       if (endp == NULL)
    9021                 :           0 :         goto invalid_data;
    9022                 :           0 :       fprintf (out, "%s", readp);
    9023                 :           0 :       readp = endp + 1;
    9024                 :           0 :       break;
    9025                 :             : 
    9026                 :       84162 :     case DW_FORM_strp:
    9027                 :             :     case DW_FORM_line_strp:
    9028                 :             :     case DW_FORM_strp_sup:
    9029         [ -  + ]:       84162 :       if ((size_t) (readendp - readp) < offset_len)
    9030                 :           0 :         goto invalid_data;
    9031         [ -  + ]:       84162 :       if (offset_len == 8)
    9032         [ #  # ]:           0 :         val = read_8ubyte_unaligned_inc (dbg, readp);
    9033                 :             :       else
    9034         [ -  + ]:       84162 :         val = read_4ubyte_unaligned_inc (dbg, readp);
    9035         [ -  + ]:       84162 :       if (form == DW_FORM_strp)
    9036                 :           0 :         data = dbg->sectiondata[IDX_debug_str];
    9037         [ +  - ]:       84162 :       else if (form == DW_FORM_line_strp)
    9038                 :       84162 :         data = dbg->sectiondata[IDX_debug_line_str];
    9039                 :             :       else /* form == DW_FORM_strp_sup */
    9040                 :             :         {
    9041                 :           0 :           Dwarf *alt = dwarf_getalt (dbg);
    9042         [ #  # ]:           0 :           data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
    9043                 :             :         }
    9044   [ +  -  -  + ]:       84162 :       if (data == NULL || val >= data->d_size
    9045         [ -  + ]:       84162 :           || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
    9046                 :             :         str = "???";
    9047                 :             :       else
    9048                 :       84162 :         str = (char *) data->d_buf + val;
    9049                 :       84162 :       fprintf (out, "%s (%" PRIu64 ")", str, val);
    9050                 :       84162 :       break;
    9051                 :             : 
    9052                 :           0 :     case DW_FORM_sec_offset:
    9053         [ #  # ]:           0 :       if ((size_t) (readendp - readp) < offset_len)
    9054                 :           0 :         goto invalid_data;
    9055         [ #  # ]:           0 :       if (offset_len == 8)
    9056         [ #  # ]:           0 :         val = read_8ubyte_unaligned_inc (dbg, readp);
    9057                 :             :       else
    9058         [ #  # ]:           0 :         val = read_4ubyte_unaligned_inc (dbg, readp);
    9059                 :           0 :       fprintf (out, "[%" PRIx64 "]", val);
    9060                 :           0 :       break;
    9061                 :             : 
    9062                 :        1556 :     case DW_FORM_strx:
    9063                 :             :     case DW_FORM_GNU_str_index:
    9064         [ -  + ]:        1556 :       if (readendp - readp < 1)
    9065                 :           0 :         goto invalid_data;
    9066                 :        1556 :       get_uleb128 (val, readp, readendp);
    9067                 :        1556 :     strx_val:
    9068                 :        1556 :       data = dbg->sectiondata[IDX_debug_str_offsets];
    9069         [ +  - ]:        1556 :       if (data == NULL
    9070         [ -  + ]:        1556 :           || data->d_size - str_offsets_base < val * offset_len)
    9071                 :             :         str = "???";
    9072                 :             :       else
    9073                 :             :         {
    9074                 :        1556 :           const unsigned char *strreadp = (data->d_buf + str_offsets_base
    9075                 :        1556 :                                            + val * offset_len);
    9076                 :        1556 :           const unsigned char *strreadendp = data->d_buf + data->d_size;
    9077         [ -  + ]:        1556 :           if ((size_t) (strreadendp - strreadp) < offset_len)
    9078                 :             :             str = "???";
    9079                 :             :           else
    9080                 :             :             {
    9081                 :        1556 :               Dwarf_Off idx;
    9082         [ -  + ]:        1556 :               if (offset_len == 8)
    9083         [ #  # ]:           0 :                 idx = read_8ubyte_unaligned (dbg, strreadp);
    9084                 :             :               else
    9085         [ -  + ]:        1556 :                 idx = read_4ubyte_unaligned (dbg, strreadp);
    9086                 :             : 
    9087                 :        1556 :               data = dbg->sectiondata[IDX_debug_str];
    9088   [ -  +  -  + ]:        1556 :               if (data == NULL || idx >= data->d_size
    9089         [ -  + ]:        1556 :                   || memchr (data->d_buf + idx, '\0',
    9090                 :             :                              data->d_size - idx) == NULL)
    9091                 :             :                 str = "???";
    9092                 :             :               else
    9093                 :        1556 :                 str = (char *) data->d_buf + idx;
    9094                 :             :             }
    9095                 :             :         }
    9096                 :        1556 :       fprintf (out, "%s (%" PRIu64 ")", str, val);
    9097                 :        1556 :       break;
    9098                 :             : 
    9099                 :           0 :     case DW_FORM_strx1:
    9100         [ #  # ]:           0 :       if (readendp - readp < 1)
    9101                 :           0 :         goto invalid_data;
    9102                 :           0 :       val = *readp++;
    9103                 :           0 :       goto strx_val;
    9104                 :             : 
    9105                 :           0 :     case DW_FORM_strx2:
    9106         [ #  # ]:           0 :       if (readendp - readp < 2)
    9107                 :           0 :         goto invalid_data;
    9108         [ #  # ]:           0 :       val = read_2ubyte_unaligned_inc (dbg, readp);
    9109                 :           0 :       goto strx_val;
    9110                 :             : 
    9111                 :           0 :     case DW_FORM_strx3:
    9112         [ #  # ]:           0 :       if (readendp - readp < 3)
    9113                 :           0 :         goto invalid_data;
    9114                 :           0 :       val = read_3ubyte_unaligned_inc (dbg, readp);
    9115                 :           0 :       goto strx_val;
    9116                 :             : 
    9117                 :           0 :     case DW_FORM_strx4:
    9118         [ #  # ]:           0 :       if (readendp - readp < 4)
    9119                 :           0 :         goto invalid_data;
    9120         [ #  # ]:           0 :       val = read_4ubyte_unaligned_inc (dbg, readp);
    9121                 :           0 :       goto strx_val;
    9122                 :             : 
    9123                 :           0 :     default:
    9124                 :           0 :       error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
    9125                 :           0 :       return readendp;
    9126                 :             :     }
    9127                 :             : 
    9128                 :      143590 :   return readp;
    9129                 :             : }
    9130                 :             : 
    9131                 :             : /* Only used via run_advance_pc() macro */
    9132                 :             : static inline void
    9133                 :      783884 : run_advance_pc (unsigned int op_advance,
    9134                 :             :                 unsigned int minimum_instr_len,
    9135                 :             :                 unsigned int max_ops_per_instr,
    9136                 :             :                 unsigned int *op_addr_advance,
    9137                 :             :                 Dwarf_Word *address,
    9138                 :             :                 unsigned int *op_index)
    9139                 :             : {
    9140                 :      783884 :   const unsigned int advanced_op_index = (*op_index) + op_advance;
    9141                 :             : 
    9142                 :      783884 :   *op_addr_advance = minimum_instr_len * (advanced_op_index
    9143                 :      783884 :                                          / max_ops_per_instr);
    9144                 :      783884 :   *address = *address + *op_addr_advance;
    9145                 :      783884 :   *op_index = advanced_op_index % max_ops_per_instr;
    9146                 :             : }
    9147                 :             : 
    9148                 :             : static void
    9149                 :         186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
    9150                 :             :                           Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
    9151                 :             :                           FILE *out)
    9152                 :             : {
    9153         [ +  + ]:         186 :   if (decodedline)
    9154                 :             :     {
    9155                 :          82 :       print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg, out);
    9156                 :         164 :       return;
    9157                 :             :     }
    9158                 :             : 
    9159                 :         104 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
    9160         [ +  - ]:         104 :   if (data == NULL)
    9161                 :             :     return;
    9162                 :             : 
    9163                 :         104 :   fprintf (out, _("\
    9164                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    9165                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    9166                 :         104 :            (uint64_t) shdr->sh_offset);
    9167                 :             : 
    9168         [ +  - ]:         104 :   if (shdr->sh_size == 0)
    9169                 :             :     return;
    9170                 :             : 
    9171                 :             :   /* There is no functionality in libdw to read the information in the
    9172                 :             :      way it is represented here.  Hardcode the decoder.  */
    9173                 :             : 
    9174                 :         104 :   const unsigned char *linep = (const unsigned char *) data->d_buf;
    9175                 :         104 :   const unsigned char *lineendp;
    9176                 :             : 
    9177                 :         104 :   while (linep
    9178         [ +  + ]:        3726 :          < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
    9179                 :             :     {
    9180                 :        3622 :       size_t start_offset = linep - (const unsigned char *) data->d_buf;
    9181                 :             : 
    9182                 :        3622 :       fprintf (out, _("\nTable at offset %zu:\n"), start_offset);
    9183                 :             : 
    9184         [ -  + ]:        3622 :       if (unlikely (linep + 4 > lineendp))
    9185                 :           0 :         goto invalid_data;
    9186         [ +  + ]:        3622 :       Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
    9187                 :        3622 :       unsigned int length = 4;
    9188         [ -  + ]:        3622 :       if (unlikely (unit_length == 0xffffffff))
    9189                 :             :         {
    9190         [ #  # ]:           0 :           if (unlikely (linep + 8 > lineendp))
    9191                 :             :             {
    9192                 :           0 :             invalid_data:
    9193                 :           0 :               error (0, 0, _("invalid data in section [%zu] '%s'"),
    9194                 :             :                      elf_ndxscn (scn), section_name (ebl, shdr));
    9195                 :           0 :               return;
    9196                 :             :             }
    9197         [ #  # ]:           0 :           unit_length = read_8ubyte_unaligned_inc (dbg, linep);
    9198                 :           0 :           length = 8;
    9199                 :             :         }
    9200                 :             : 
    9201                 :             :       /* Check whether we have enough room in the section.  */
    9202         [ -  + ]:        3622 :       if (unlikely (unit_length > (size_t) (lineendp - linep)))
    9203                 :           0 :         goto invalid_data;
    9204                 :        3622 :       lineendp = linep + unit_length;
    9205                 :             : 
    9206                 :             :       /* The next element of the header is the version identifier.  */
    9207         [ -  + ]:        3622 :       if ((size_t) (lineendp - linep) < 2)
    9208                 :           0 :         goto invalid_data;
    9209         [ +  + ]:        3622 :       uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
    9210                 :             : 
    9211                 :        7244 :       size_t address_size
    9212         [ +  + ]:        3622 :         = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
    9213                 :        3622 :       unsigned char segment_selector_size = 0;
    9214         [ +  + ]:        3622 :       if (version > 4)
    9215                 :             :         {
    9216         [ -  + ]:        3470 :           if ((size_t) (lineendp - linep) < 2)
    9217                 :           0 :             goto invalid_data;
    9218                 :        3470 :           address_size = *linep++;
    9219                 :        3470 :           segment_selector_size = *linep++;
    9220                 :             :         }
    9221                 :             : 
    9222                 :             :       /* Next comes the header length.  */
    9223                 :        3622 :       Dwarf_Word header_length;
    9224         [ +  - ]:        3622 :       if (length == 4)
    9225                 :             :         {
    9226         [ -  + ]:        3622 :           if ((size_t) (lineendp - linep) < 4)
    9227                 :           0 :             goto invalid_data;
    9228         [ +  + ]:        3622 :           header_length = read_4ubyte_unaligned_inc (dbg, linep);
    9229                 :             :         }
    9230                 :             :       else
    9231                 :             :         {
    9232         [ #  # ]:           0 :           if ((size_t) (lineendp - linep) < 8)
    9233                 :           0 :             goto invalid_data;
    9234         [ #  # ]:           0 :           header_length = read_8ubyte_unaligned_inc (dbg, linep);
    9235                 :             :         }
    9236                 :             : 
    9237                 :        3622 :       const unsigned char *header_start = linep;
    9238                 :             : 
    9239                 :             :       /* Next the minimum instruction length.  */
    9240         [ -  + ]:        3622 :       if ((size_t) (lineendp - linep) < 1)
    9241                 :           0 :         goto invalid_data;
    9242                 :        3622 :       uint_fast8_t minimum_instr_len = *linep++;
    9243                 :             : 
    9244                 :             :       /* Next the maximum operations per instruction, in version 4 format.  */
    9245                 :        3622 :       uint_fast8_t max_ops_per_instr;
    9246         [ +  + ]:        3622 :       if (version < 4)
    9247                 :             :         max_ops_per_instr = 1;
    9248                 :             :       else
    9249                 :             :         {
    9250         [ -  + ]:        3490 :           if ((size_t) (lineendp - linep) < 1)
    9251                 :           0 :             goto invalid_data;
    9252                 :        3490 :           max_ops_per_instr = *linep++;
    9253                 :             :         }
    9254                 :             : 
    9255                 :             :       /* We need at least 4 more bytes.  */
    9256         [ -  + ]:        3622 :       if ((size_t) (lineendp - linep) < 4)
    9257                 :           0 :         goto invalid_data;
    9258                 :             : 
    9259                 :             :       /* Then the flag determining the default value of the is_stmt
    9260                 :             :          register.  */
    9261                 :        3622 :       uint_fast8_t default_is_stmt = *linep++;
    9262                 :             : 
    9263                 :             :       /* Now the line base.  */
    9264                 :        3622 :       int_fast8_t line_base = *linep++;
    9265                 :             : 
    9266                 :             :       /* And the line range.  */
    9267                 :        3622 :       uint_fast8_t line_range = *linep++;
    9268                 :             : 
    9269                 :             :       /* The opcode base.  */
    9270                 :        3622 :       uint_fast8_t opcode_base = *linep++;
    9271                 :             : 
    9272                 :             :       /* Print what we got so far.  */
    9273                 :        3622 :       fprintf (out, _("\n"
    9274                 :             :                         " Length:                         %" PRIu64 "\n"
    9275                 :             :                         " DWARF version:                  %" PRIuFAST16 "\n"
    9276                 :             :                         " Prologue length:                %" PRIu64 "\n"
    9277                 :             :                         " Address size:                   %zd\n"
    9278                 :             :                         " Segment selector size:          %zd\n"
    9279                 :             :                         " Min instruction length:         %" PRIuFAST8 "\n"
    9280                 :             :                         " Max operations per instruction: %" PRIuFAST8 "\n"
    9281                 :             :                         " Initial value if 'is_stmt':     %" PRIuFAST8 "\n"
    9282                 :             :                         " Line base:                      %" PRIdFAST8 "\n"
    9283                 :             :                         " Line range:                     %" PRIuFAST8 "\n"
    9284                 :             :                         " Opcode base:                    %" PRIuFAST8 "\n"
    9285                 :             :                         "\n"
    9286                 :             :                         "Opcodes:\n"),
    9287                 :             :                (uint64_t) unit_length, version, (uint64_t) header_length,
    9288                 :             :                address_size, (size_t) segment_selector_size,
    9289                 :             :                minimum_instr_len, max_ops_per_instr,
    9290                 :             :                default_is_stmt, line_base,
    9291                 :             :                line_range, opcode_base);
    9292                 :             : 
    9293         [ -  + ]:        3622 :       if (version < 2 || version > 5)
    9294                 :             :         {
    9295                 :           0 :           error (0, 0, _("cannot handle .debug_line version: %u\n"),
    9296                 :             :                  (unsigned int) version);
    9297                 :           0 :           linep = lineendp;
    9298                 :           0 :           continue;
    9299                 :             :         }
    9300                 :             : 
    9301         [ -  + ]:        3622 :       if (address_size != 4 && address_size != 8)
    9302                 :             :         {
    9303                 :           0 :           error (0, 0, _("cannot handle address size: %u\n"),
    9304                 :             :                  (unsigned int) address_size);
    9305                 :           0 :           linep = lineendp;
    9306                 :           0 :           continue;
    9307                 :             :         }
    9308                 :             : 
    9309         [ -  + ]:        3622 :       if (segment_selector_size != 0)
    9310                 :             :         {
    9311                 :           0 :           error (0, 0, _("cannot handle segment selector size: %u\n"),
    9312                 :             :                  (unsigned int) segment_selector_size);
    9313                 :           0 :           linep = lineendp;
    9314                 :           0 :           continue;
    9315                 :             :         }
    9316                 :             : 
    9317         [ -  + ]:        3622 :       if (unlikely (linep + opcode_base - 1 >= lineendp))
    9318                 :             :         {
    9319                 :           0 :         invalid_unit:
    9320                 :           0 :           error (0, 0,
    9321                 :           0 :                  _("invalid data at offset %tu in section [%zu] '%s'"),
    9322                 :           0 :                  linep - (const unsigned char *) data->d_buf,
    9323                 :             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    9324                 :           0 :           linep = lineendp;
    9325                 :           0 :           continue;
    9326                 :             :         }
    9327                 :        3622 :       int opcode_base_l10 = 1;
    9328                 :        3622 :       unsigned int tmp = opcode_base;
    9329         [ +  + ]:        7238 :       while (tmp > 10)
    9330                 :             :         {
    9331                 :        3616 :           tmp /= 10;
    9332                 :        3616 :           ++opcode_base_l10;
    9333                 :             :         }
    9334                 :             :       const uint8_t *standard_opcode_lengths = linep - 1;
    9335         [ +  + ]:       47068 :       for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
    9336                 :       43446 :         fprintf (out, ngettext ("  [%*" PRIuFAST8 "]  %hhu argument\n",
    9337                 :             :                            "  [%*" PRIuFAST8 "]  %hhu arguments\n",
    9338                 :             :                            (int) linep[cnt - 1]),
    9339                 :       43446 :                  opcode_base_l10, cnt, linep[cnt - 1]);
    9340                 :        3622 :       linep += opcode_base - 1;
    9341                 :             : 
    9342                 :        3622 :       if (unlikely (linep >= lineendp))
    9343                 :             :         goto invalid_unit;
    9344                 :             : 
    9345                 :        3622 :       Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
    9346                 :             : 
    9347                 :        3622 :       fputs (_("\nDirectory table:\n"), out);
    9348         [ +  + ]:        3622 :       if (version > 4)
    9349                 :             :         {
    9350                 :        3470 :           struct encpair { uint16_t desc; uint16_t form; };
    9351                 :        3470 :           struct encpair enc[256];
    9352                 :             : 
    9353                 :        3470 :           fprintf (out, _("      ["));
    9354                 :        3470 :           if ((size_t) (lineendp - linep) < 1)
    9355                 :           0 :             goto invalid_data;
    9356                 :        3470 :           unsigned char directory_entry_format_count = *linep++;
    9357         [ +  + ]:        6940 :           for (int i = 0; i < directory_entry_format_count; i++)
    9358                 :             :             {
    9359                 :        3470 :               uint16_t desc, form;
    9360         [ -  + ]:        3470 :               if ((size_t) (lineendp - linep) < 1)
    9361                 :           0 :                 goto invalid_data;
    9362                 :        3470 :               get_uleb128 (desc, linep, lineendp);
    9363         [ -  + ]:        3470 :               if ((size_t) (lineendp - linep) < 1)
    9364                 :           0 :                 goto invalid_data;
    9365                 :        3470 :               get_uleb128 (form, linep, lineendp);
    9366                 :             : 
    9367                 :        3470 :               enc[i].desc = desc;
    9368                 :        3470 :               enc[i].form = form;
    9369                 :             : 
    9370                 :        3470 :               fprintf (out, "%s(%s)",
    9371                 :             :                        dwarf_line_content_description_name (desc),
    9372                 :             :                        dwarf_form_name (form));
    9373         [ -  + ]:        3470 :               if (i + 1 < directory_entry_format_count)
    9374                 :           0 :                 fprintf (out, ", ");
    9375                 :             :             }
    9376                 :        3470 :           fprintf (out, "]\n");
    9377                 :             : 
    9378                 :        3470 :           uint64_t directories_count;
    9379         [ -  + ]:        3470 :           if ((size_t) (lineendp - linep) < 1)
    9380                 :           0 :             goto invalid_data;
    9381                 :        3470 :           get_uleb128 (directories_count, linep, lineendp);
    9382                 :             : 
    9383                 :        3470 :           if (directory_entry_format_count == 0
    9384         [ -  + ]:        3470 :               && directories_count != 0)
    9385                 :           0 :             goto invalid_data;
    9386                 :             : 
    9387         [ +  + ]:       29784 :           for (uint64_t i = 0; i < directories_count; i++)
    9388                 :             :             {
    9389                 :       26314 :               fprintf (out, " %-5" PRIu64 " ", i);
    9390         [ +  + ]:       52628 :               for (int j = 0; j < directory_entry_format_count; j++)
    9391                 :             :                 {
    9392                 :       26314 :                   linep = print_form_data (dbg, enc[j].form,
    9393                 :             :                                            linep, lineendp, length,
    9394                 :             :                                            str_offsets_base, out);
    9395         [ -  + ]:       26314 :                   if (j + 1 < directory_entry_format_count)
    9396                 :           0 :                     fprintf (out, ", ");
    9397                 :             :                 }
    9398                 :       26314 :               fprintf (out, "\n");
    9399         [ -  + ]:       26314 :               if (linep >= lineendp)
    9400                 :           0 :                 goto invalid_unit;
    9401                 :             :             }
    9402                 :             :         }
    9403                 :             :       else
    9404                 :             :         {
    9405   [ +  -  +  + ]:         766 :           while (linep < lineendp && *linep != 0)
    9406                 :             :             {
    9407                 :         614 :               unsigned char *endp = memchr (linep, '\0', lineendp - linep);
    9408         [ -  + ]:         614 :               if (unlikely (endp == NULL))
    9409                 :           0 :                 goto invalid_unit;
    9410                 :             : 
    9411                 :         614 :               fprintf (out, " %s\n", (char *) linep);
    9412                 :             : 
    9413                 :         614 :               linep = endp + 1;
    9414                 :             :             }
    9415   [ +  -  -  + ]:         152 :           if (linep >= lineendp || *linep != 0)
    9416                 :           0 :             goto invalid_unit;
    9417                 :             :           /* Skip the final NUL byte.  */
    9418                 :         152 :           ++linep;
    9419                 :             :         }
    9420                 :             : 
    9421         [ -  + ]:        3622 :       if (unlikely (linep >= lineendp))
    9422                 :           0 :         goto invalid_unit;
    9423                 :             : 
    9424                 :        3622 :       fputs (_("\nFile name table:\n"), out);
    9425         [ +  + ]:        3622 :       if (version > 4)
    9426                 :             :         {
    9427                 :        3470 :           struct encpair { uint16_t desc; uint16_t form; };
    9428                 :        3470 :           struct encpair enc[256];
    9429                 :             : 
    9430                 :        3470 :           fprintf (out, _("      ["));
    9431                 :        3470 :           if ((size_t) (lineendp - linep) < 1)
    9432                 :           0 :             goto invalid_data;
    9433                 :        3470 :           unsigned char file_name_format_count = *linep++;
    9434         [ +  + ]:       10418 :           for (int i = 0; i < file_name_format_count; i++)
    9435                 :             :             {
    9436                 :        6948 :               uint64_t desc, form;
    9437         [ -  + ]:        6948 :               if ((size_t) (lineendp - linep) < 1)
    9438                 :           0 :                 goto invalid_data;
    9439                 :        6948 :               get_uleb128 (desc, linep, lineendp);
    9440         [ -  + ]:        6948 :               if ((size_t) (lineendp - linep) < 1)
    9441                 :           0 :                 goto invalid_data;
    9442                 :        6948 :               get_uleb128 (form, linep, lineendp);
    9443                 :             : 
    9444         [ -  + ]:        6948 :               if (! libdw_valid_user_form (form))
    9445                 :           0 :                 goto invalid_data;
    9446                 :             : 
    9447                 :        6948 :               enc[i].desc = desc;
    9448                 :        6948 :               enc[i].form = form;
    9449                 :             : 
    9450                 :        6948 :               fprintf (out, "%s(%s)",
    9451                 :             :                        dwarf_line_content_description_name (desc),
    9452                 :             :                        dwarf_form_name (form));
    9453         [ +  + ]:        6948 :               if (i + 1 < file_name_format_count)
    9454                 :        3478 :                 fprintf (out, ", ");
    9455                 :             :             }
    9456                 :        3470 :           fprintf (out, "]\n");
    9457                 :             : 
    9458                 :        3470 :           uint64_t file_name_count;
    9459         [ -  + ]:        3470 :           if ((size_t) (lineendp - linep) < 1)
    9460                 :           0 :             goto invalid_data;
    9461                 :        3470 :           get_uleb128 (file_name_count, linep, lineendp);
    9462                 :             : 
    9463                 :        3470 :           if (file_name_format_count == 0
    9464         [ -  + ]:        3470 :               && file_name_count != 0)
    9465                 :           0 :             goto invalid_data;
    9466                 :             : 
    9467         [ +  + ]:       61318 :           for (uint64_t i = 0; i < file_name_count; i++)
    9468                 :             :             {
    9469                 :       57848 :               fprintf (out, " %-5" PRIu64 " ", i);
    9470         [ +  + ]:      173568 :               for (int j = 0; j < file_name_format_count; j++)
    9471                 :             :                 {
    9472                 :      115720 :                   linep = print_form_data (dbg, enc[j].form,
    9473                 :             :                                            linep, lineendp, length,
    9474                 :             :                                            str_offsets_base, out);
    9475         [ +  + ]:      115720 :                   if (j + 1 < file_name_format_count)
    9476                 :       57872 :                     fprintf (out, ", ");
    9477                 :             :                 }
    9478                 :       57848 :               fprintf (out, "\n");
    9479         [ -  + ]:       57848 :               if (linep > lineendp)
    9480                 :           0 :                 goto invalid_unit;
    9481                 :             :             }
    9482                 :             :         }
    9483                 :             :       else
    9484                 :             :         {
    9485                 :         152 :           fputs (_(" Entry Dir   Time      Size      Name\n"), out);
    9486   [ +  -  +  + ]:        6154 :           for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
    9487                 :             :             {
    9488                 :             :               /* First comes the file name.  */
    9489                 :        6002 :               char *fname = (char *) linep;
    9490                 :        6002 :               unsigned char *endp = memchr (fname, '\0', lineendp - linep);
    9491         [ -  + ]:        6002 :               if (unlikely (endp == NULL))
    9492                 :           0 :                 goto invalid_unit;
    9493                 :        6002 :               linep = endp + 1;
    9494                 :             : 
    9495                 :             :               /* Then the index.  */
    9496                 :        6002 :               unsigned int diridx;
    9497         [ -  + ]:        6002 :               if (lineendp - linep < 1)
    9498                 :           0 :                 goto invalid_unit;
    9499                 :        6002 :               get_uleb128 (diridx, linep, lineendp);
    9500                 :             : 
    9501                 :             :               /* Next comes the modification time.  */
    9502                 :        6002 :               unsigned int mtime;
    9503         [ -  + ]:        6002 :               if (lineendp - linep < 1)
    9504                 :           0 :                 goto invalid_unit;
    9505                 :        6002 :               get_uleb128 (mtime, linep, lineendp);
    9506                 :             : 
    9507                 :             :               /* Finally the length of the file.  */
    9508                 :        6002 :               unsigned int fsize;
    9509         [ -  + ]:        6002 :               if (lineendp - linep < 1)
    9510                 :           0 :                 goto invalid_unit;
    9511                 :        6002 :               get_uleb128 (fsize, linep, lineendp);
    9512                 :             : 
    9513                 :        6002 :               fprintf (out, " %-5u %-5u %-9u %-9u %s\n",
    9514                 :             :                        cnt, diridx, mtime, fsize, fname);
    9515                 :             :             }
    9516   [ +  -  -  + ]:         152 :           if (linep >= lineendp || *linep != '\0')
    9517                 :           0 :             goto invalid_unit;
    9518                 :             :           /* Skip the final NUL byte.  */
    9519                 :         152 :           ++linep;
    9520                 :             :         }
    9521                 :             : 
    9522                 :        3622 :       unsigned int debug_str_offset = 0;
    9523         [ +  + ]:        3622 :       if (unlikely (linep == header_start + header_length - 4))
    9524                 :             :         {
    9525                 :             :           /* CUBINs contain an unsigned 4-byte offset */
    9526         [ -  + ]:           2 :           debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
    9527                 :             :         }
    9528                 :             : 
    9529         [ +  + ]:        3622 :       if (linep == lineendp)
    9530                 :             :         {
    9531                 :          40 :           fputs (_("\nNo line number statements.\n"), out);
    9532                 :          40 :           continue;
    9533                 :             :         }
    9534                 :             : 
    9535                 :        3582 :       fputs (_("\nLine number statements:\n"), out);
    9536                 :        3582 :       Dwarf_Word address = 0;
    9537                 :        3582 :       unsigned int op_index = 0;
    9538                 :        3582 :       size_t line = 1;
    9539                 :        3582 :       uint_fast8_t is_stmt = default_is_stmt;
    9540                 :             : 
    9541                 :             :       /* Apply the "operation advance" from a special opcode
    9542                 :             :          or DW_LNS_advance_pc (as per DWARF4 6.2.5.1).  */
    9543                 :        3582 :       unsigned int op_addr_advance;
    9544                 :             : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
    9545                 :             :                       max_ops_per_instr, &op_addr_advance, &address, &op_index)
    9546                 :             : 
    9547         [ -  + ]:        3582 :       if (max_ops_per_instr == 0)
    9548                 :             :         {
    9549                 :           0 :           error (0, 0,
    9550                 :           0 :                  _("invalid maximum operations per instruction is zero"));
    9551                 :           0 :           linep = lineendp;
    9552                 :           0 :           continue;
    9553                 :             :         }
    9554                 :             : 
    9555         [ +  + ]:     2523936 :       while (linep < lineendp)
    9556                 :             :         {
    9557                 :     2520354 :           size_t offset = linep - (const unsigned char *) data->d_buf;
    9558                 :     2520354 :           unsigned int u128;
    9559                 :     2520354 :           int s128;
    9560                 :             : 
    9561                 :             :           /* Read the opcode.  */
    9562                 :     2520354 :           unsigned int opcode = *linep++;
    9563                 :             : 
    9564                 :     2520354 :           fprintf (out, " [%6" PRIx64 "]", (uint64_t)offset);
    9565                 :             :           /* Is this a special opcode?  */
    9566         [ +  + ]:     2520354 :           if (likely (opcode >= opcode_base))
    9567                 :             :             {
    9568         [ -  + ]:      714932 :               if (unlikely (line_range == 0))
    9569                 :           0 :                 goto invalid_unit;
    9570                 :             : 
    9571                 :             :               /* Yes.  Handling this is quite easy since the opcode value
    9572                 :             :                  is computed with
    9573                 :             : 
    9574                 :             :                  opcode = (desired line increment - line_base)
    9575                 :             :                            + (line_range * address advance) + opcode_base
    9576                 :             :               */
    9577                 :      714932 :               int line_increment = (line_base
    9578                 :      714932 :                                     + (opcode - opcode_base) % line_range);
    9579                 :             : 
    9580                 :             :               /* Perform the increments.  */
    9581                 :      714932 :               line += line_increment;
    9582                 :      714932 :               advance_pc ((opcode - opcode_base) / line_range);
    9583                 :             : 
    9584                 :      714932 :               fprintf (out, _(" special opcode %u: address+%u = "),
    9585                 :             :                        opcode, op_addr_advance);
    9586                 :      714932 :               print_dwarf_addr (dwflmod, 0, address, address, out);
    9587         [ -  + ]:      714932 :               if (op_index > 0)
    9588                 :           0 :                 fprintf (out, _(", op_index = %u, line%+d = %zu\n"),
    9589                 :             :                          op_index, line_increment, line);
    9590                 :             :               else
    9591                 :      714932 :                 fprintf (out, _(", line%+d = %zu\n"),
    9592                 :             :                          line_increment, line);
    9593                 :             :             }
    9594         [ +  + ]:     1805422 :           else if (opcode == 0)
    9595                 :             :             {
    9596                 :             :               /* This an extended opcode.  */
    9597         [ -  + ]:      190924 :               if (unlikely (linep + 2 > lineendp))
    9598                 :           0 :                 goto invalid_unit;
    9599                 :             : 
    9600                 :             :               /* The length.  */
    9601                 :      190924 :               unsigned int len = *linep++;
    9602                 :             : 
    9603         [ -  + ]:      190924 :               if (unlikely (linep + len > lineendp))
    9604                 :           0 :                 goto invalid_unit;
    9605                 :             : 
    9606                 :             :               /* The sub-opcode.  */
    9607                 :      190924 :               opcode = *linep++;
    9608                 :             : 
    9609                 :      190924 :               fprintf (out, _(" extended opcode %u: "), opcode);
    9610                 :             : 
    9611   [ +  +  -  +  :      190924 :               switch (opcode)
                +  -  - ]
    9612                 :             :                 {
    9613                 :       10608 :                 case DW_LNE_end_sequence:
    9614                 :       10608 :                   fputs (_(" end of sequence\n"), out);
    9615                 :             : 
    9616                 :             :                   /* Reset the registers we care about.  */
    9617                 :       10608 :                   address = 0;
    9618                 :       10608 :                   op_index = 0;
    9619                 :       10608 :                   line = 1;
    9620                 :       10608 :                   is_stmt = default_is_stmt;
    9621                 :       10608 :                   break;
    9622                 :             : 
    9623                 :       12372 :                 case DW_LNE_set_address:
    9624                 :       12372 :                   op_index = 0;
    9625         [ -  + ]:       12372 :                   if (unlikely ((size_t) (lineendp - linep) < address_size))
    9626                 :           0 :                     goto invalid_unit;
    9627         [ +  + ]:       12372 :                   if (address_size == 4)
    9628         [ +  + ]:          50 :                     address = read_4ubyte_unaligned_inc (dbg, linep);
    9629                 :             :                   else
    9630         [ +  + ]:       12322 :                     address = read_8ubyte_unaligned_inc (dbg, linep);
    9631                 :             :                   {
    9632                 :       12372 :                     fprintf (out, _(" set address to "));
    9633                 :       12372 :                     print_dwarf_addr (dwflmod, 0, address, address, out);
    9634                 :       12372 :                     fprintf (out, "\n");
    9635                 :             :                   }
    9636                 :       12372 :                   break;
    9637                 :             : 
    9638                 :           0 :                 case DW_LNE_define_file:
    9639                 :             :                   {
    9640                 :           0 :                     char *fname = (char *) linep;
    9641                 :           0 :                     unsigned char *endp = memchr (linep, '\0',
    9642                 :           0 :                                                   lineendp - linep);
    9643         [ #  # ]:           0 :                     if (unlikely (endp == NULL))
    9644                 :           0 :                       goto invalid_unit;
    9645                 :           0 :                     linep = endp + 1;
    9646                 :             : 
    9647                 :           0 :                     unsigned int diridx;
    9648         [ #  # ]:           0 :                     if (lineendp - linep < 1)
    9649                 :           0 :                       goto invalid_unit;
    9650                 :           0 :                     get_uleb128 (diridx, linep, lineendp);
    9651                 :           0 :                     Dwarf_Word mtime;
    9652         [ #  # ]:           0 :                     if (lineendp - linep < 1)
    9653                 :           0 :                       goto invalid_unit;
    9654                 :           0 :                     get_uleb128 (mtime, linep, lineendp);
    9655                 :           0 :                     Dwarf_Word filelength;
    9656         [ #  # ]:           0 :                     if (lineendp - linep < 1)
    9657                 :           0 :                       goto invalid_unit;
    9658                 :           0 :                     get_uleb128 (filelength, linep, lineendp);
    9659                 :             : 
    9660                 :           0 :                     fprintf (out, _("\
    9661                 :             :  define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
    9662                 :             :                              diridx, (uint64_t) mtime, (uint64_t) filelength,
    9663                 :             :                              fname);
    9664                 :             :                   }
    9665                 :           0 :                   break;
    9666                 :             : 
    9667                 :      167930 :                 case DW_LNE_set_discriminator:
    9668                 :             :                   /* Takes one ULEB128 parameter, the discriminator.  */
    9669   [ +  -  -  + ]:      167930 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    9670                 :             :                                 || lineendp - linep < 1))
    9671                 :           0 :                     goto invalid_unit;
    9672                 :             : 
    9673                 :      167930 :                   get_uleb128 (u128, linep, lineendp);
    9674                 :      167930 :                   fprintf (out, _(" set discriminator to %u\n"), u128);
    9675                 :      167930 :                   break;
    9676                 :             : 
    9677                 :          14 :                 case DW_LNE_NVIDIA_inlined_call:
    9678                 :             :                   {
    9679         [ -  + ]:          14 :                     if (unlikely (linep >= lineendp))
    9680                 :           0 :                       goto invalid_data;
    9681                 :             : 
    9682                 :          14 :                     unsigned int context;
    9683                 :          14 :                     get_uleb128 (context, linep, lineendp);
    9684                 :             : 
    9685         [ -  + ]:          14 :                     if (unlikely (linep >= lineendp))
    9686                 :           0 :                       goto invalid_data;
    9687                 :             : 
    9688                 :          14 :                     unsigned int function_name;
    9689                 :          14 :                     get_uleb128 (function_name, linep, lineendp);
    9690                 :          14 :                     function_name += debug_str_offset;
    9691                 :             : 
    9692                 :          14 :                     Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
    9693                 :          14 :                     char *function_str;
    9694   [ +  -  -  + ]:          14 :                     if (str_data == NULL || function_name >= str_data->d_size
    9695         [ -  + ]:          14 :                         || memchr (str_data->d_buf + function_name, '\0',
    9696                 :             :                                    str_data->d_size - function_name) == NULL)
    9697                 :             :                       function_str = "???";
    9698                 :             :                     else
    9699                 :          14 :                       function_str = (char *) str_data->d_buf + function_name;
    9700                 :             : 
    9701                 :          14 :                     fprintf (out, _(" set inlined context %u,"
    9702                 :             :                                " function name %s (0x%x)\n"),
    9703                 :             :                              context, function_str, function_name);
    9704                 :          14 :                     break;
    9705                 :             :                   }
    9706                 :             : 
    9707                 :           0 :                 case DW_LNE_NVIDIA_set_function_name:
    9708                 :             :                   {
    9709         [ #  # ]:           0 :                     if (unlikely (linep >= lineendp))
    9710                 :           0 :                       goto invalid_data;
    9711                 :             : 
    9712                 :           0 :                     unsigned int function_name;
    9713                 :           0 :                     get_uleb128 (function_name, linep, lineendp);
    9714                 :           0 :                     function_name += debug_str_offset;
    9715                 :             : 
    9716                 :           0 :                     Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
    9717                 :           0 :                     char *function_str;
    9718   [ #  #  #  # ]:           0 :                     if (str_data == NULL || function_name >= str_data->d_size
    9719         [ #  # ]:           0 :                         || memchr (str_data->d_buf + function_name, '\0',
    9720                 :             :                                    str_data->d_size - function_name) == NULL)
    9721                 :             :                       function_str = "???";
    9722                 :             :                     else
    9723                 :           0 :                       function_str = (char *) str_data->d_buf + function_name;
    9724                 :             : 
    9725                 :           0 :                     fprintf (out, _(" set function name %s (0x%x)\n"),
    9726                 :             :                              function_str, function_name);
    9727                 :             :                   }
    9728                 :           0 :                   break;
    9729                 :             : 
    9730                 :           0 :                 default:
    9731                 :             :                   /* Unknown, ignore it.  */
    9732                 :           0 :                   fputs (_(" unknown opcode\n"), out);
    9733                 :           0 :                   linep += len - 1;
    9734                 :           0 :                   break;
    9735                 :             :                 }
    9736                 :             :             }
    9737         [ +  - ]:     1614498 :           else if (opcode <= DW_LNS_set_isa)
    9738                 :             :             {
    9739                 :             :               /* This is a known standard opcode.  */
    9740   [ +  +  +  +  :     1614498 :               switch (opcode)
          +  +  -  +  +  
                +  -  - ]
    9741                 :             :                 {
    9742                 :      251906 :                 case DW_LNS_copy:
    9743                 :             :                   /* Takes no argument.  */
    9744                 :      251906 :                   fputs (_(" copy\n"), out);
    9745                 :      251906 :                   break;
    9746                 :             : 
    9747                 :       15388 :                 case DW_LNS_advance_pc:
    9748                 :             :                   /* Takes one uleb128 parameter which is added to the
    9749                 :             :                      address.  */
    9750         [ -  + ]:       15388 :                   if (lineendp - linep < 1)
    9751                 :           0 :                     goto invalid_unit;
    9752                 :       15388 :                   get_uleb128 (u128, linep, lineendp);
    9753                 :       15388 :                   advance_pc (u128);
    9754                 :             :                   {
    9755                 :       15388 :                     fprintf (out, _(" advance address by %u to "),
    9756                 :             :                              op_addr_advance);
    9757                 :       15388 :                     print_dwarf_addr (dwflmod, 0, address, address, out);
    9758         [ -  + ]:       15388 :                     if (op_index > 0)
    9759                 :           0 :                       fprintf (out, _(", op_index to %u"), op_index);
    9760                 :       15388 :                     fprintf (out, "\n");
    9761                 :             :                   }
    9762                 :       15388 :                   break;
    9763                 :             : 
    9764                 :      186238 :                 case DW_LNS_advance_line:
    9765                 :             :                   /* Takes one sleb128 parameter which is added to the
    9766                 :             :                      line.  */
    9767         [ -  + ]:      186238 :                   if (lineendp - linep < 1)
    9768                 :           0 :                     goto invalid_unit;
    9769                 :      186238 :                   get_sleb128 (s128, linep, lineendp);
    9770                 :      186238 :                   line += s128;
    9771                 :      186238 :                   fprintf (out, _("\
    9772                 :             :  advance line by constant %d to %" PRId64 "\n"),
    9773                 :             :                            s128, (int64_t) line);
    9774                 :      186238 :                   break;
    9775                 :             : 
    9776                 :       65042 :                 case DW_LNS_set_file:
    9777                 :             :                   /* Takes one uleb128 parameter which is stored in file.  */
    9778         [ -  + ]:       65042 :                   if (lineendp - linep < 1)
    9779                 :           0 :                     goto invalid_unit;
    9780                 :       65042 :                   get_uleb128 (u128, linep, lineendp);
    9781                 :       65042 :                   fprintf (out, _(" set file to %" PRIu64 "\n"),
    9782                 :             :                            (uint64_t) u128);
    9783                 :       65042 :                   break;
    9784                 :             : 
    9785                 :      610064 :                 case DW_LNS_set_column:
    9786                 :             :                   /* Takes one uleb128 parameter which is stored in column.  */
    9787   [ +  -  -  + ]:      610064 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    9788                 :             :                                 || lineendp - linep < 1))
    9789                 :           0 :                     goto invalid_unit;
    9790                 :             : 
    9791                 :      610064 :                   get_uleb128 (u128, linep, lineendp);
    9792                 :      610064 :                   fprintf (out, _(" set column to %" PRIu64 "\n"),
    9793                 :             :                            (uint64_t) u128);
    9794                 :      610064 :                   break;
    9795                 :             : 
    9796                 :      432242 :                 case DW_LNS_negate_stmt:
    9797                 :             :                   /* Takes no argument.  */
    9798                 :      432242 :                   is_stmt = 1 - is_stmt;
    9799                 :      432242 :                   fprintf (out, _(" set '%s' to %" PRIuFAST8 "\n"),
    9800                 :             :                            "is_stmt", is_stmt);
    9801                 :      432242 :                   break;
    9802                 :             : 
    9803                 :           0 :                 case DW_LNS_set_basic_block:
    9804                 :             :                   /* Takes no argument.  */
    9805                 :           0 :                   fputs (_(" set basic block flag\n"), out);
    9806                 :           0 :                   break;
    9807                 :             : 
    9808                 :       53564 :                 case DW_LNS_const_add_pc:
    9809                 :             :                   /* Takes no argument.  */
    9810                 :             : 
    9811         [ -  + ]:       53564 :                   if (unlikely (line_range == 0))
    9812                 :           0 :                     goto invalid_unit;
    9813                 :             : 
    9814                 :       53564 :                   advance_pc ((255 - opcode_base) / line_range);
    9815                 :             :                   {
    9816                 :       53564 :                     fprintf (out, _(" advance address by constant %u to "),
    9817                 :             :                              op_addr_advance);
    9818                 :       53564 :                     print_dwarf_addr (dwflmod, 0, address, address, out);
    9819         [ -  + ]:       53564 :                     if (op_index > 0)
    9820                 :           0 :                       fprintf (out, _(", op_index to %u"), op_index);
    9821                 :       53564 :                     fprintf (out, "\n");
    9822                 :             :                   }
    9823                 :       53564 :                   break;
    9824                 :             : 
    9825                 :          44 :                 case DW_LNS_fixed_advance_pc:
    9826                 :             :                   /* Takes one 16 bit parameter which is added to the
    9827                 :             :                      address.  */
    9828   [ +  -  -  + ]:          44 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    9829                 :             :                                 || lineendp - linep < 2))
    9830                 :           0 :                     goto invalid_unit;
    9831                 :             : 
    9832         [ -  + ]:          44 :                   u128 = read_2ubyte_unaligned_inc (dbg, linep);
    9833                 :          44 :                   address += u128;
    9834                 :          44 :                   op_index = 0;
    9835                 :             :                   {
    9836                 :          44 :                     fprintf (out, _("\
    9837                 :             :  advance address by fixed value %u to \n"),
    9838                 :             :                              u128);
    9839                 :          44 :                     print_dwarf_addr (dwflmod, 0, address, address, out);
    9840                 :          44 :                     fprintf (out, "\n");
    9841                 :             :                   }
    9842                 :          44 :                   break;
    9843                 :             : 
    9844                 :          10 :                 case DW_LNS_set_prologue_end:
    9845                 :             :                   /* Takes no argument.  */
    9846                 :          10 :                   fputs (_(" set prologue end flag\n"), out);
    9847                 :          10 :                   break;
    9848                 :             : 
    9849                 :           0 :                 case DW_LNS_set_epilogue_begin:
    9850                 :             :                   /* Takes no argument.  */
    9851                 :           0 :                   fputs (_(" set epilogue begin flag\n"), out);
    9852                 :           0 :                   break;
    9853                 :             : 
    9854                 :           0 :                 case DW_LNS_set_isa:
    9855                 :             :                   /* Takes one uleb128 parameter which is stored in isa.  */
    9856   [ #  #  #  # ]:           0 :                   if (unlikely (standard_opcode_lengths[opcode] != 1
    9857                 :             :                                 || lineendp - linep < 1))
    9858                 :           0 :                     goto invalid_unit;
    9859                 :             : 
    9860                 :           0 :                   get_uleb128 (u128, linep, lineendp);
    9861                 :           0 :                   fprintf (out, _(" set isa to %u\n"), u128);
    9862                 :           0 :                   break;
    9863                 :             :                 }
    9864                 :             :             }
    9865                 :             :           else
    9866                 :             :             {
    9867                 :             :               /* This is a new opcode the generator but not we know about.
    9868                 :             :                  Read the parameters associated with it but then discard
    9869                 :             :                  everything.  Read all the parameters for this opcode.  */
    9870                 :           0 :               fprintf (out,
    9871                 :           0 :                        ngettext (" unknown opcode with %" PRIu8 " parameter:",
    9872                 :             :                                  " unknown opcode with %" PRIu8 " parameters:",
    9873                 :             :                                  standard_opcode_lengths[opcode]),
    9874                 :           0 :                        standard_opcode_lengths[opcode]);
    9875                 :           0 :               for (int n = standard_opcode_lengths[opcode];
    9876   [ #  #  #  # ]:           0 :                    n > 0 && linep < lineendp; --n)
    9877                 :             :                 {
    9878                 :           0 :                   get_uleb128 (u128, linep, lineendp);
    9879         [ #  # ]:           0 :                   if (n != standard_opcode_lengths[opcode])
    9880                 :           0 :                     fputc (',', out);
    9881                 :           0 :                   fprintf (out, " %u", u128);
    9882                 :             :                 }
    9883                 :             : 
    9884                 :             :               /* Next round, ignore this opcode.  */
    9885                 :           0 :               continue;
    9886                 :             :             }
    9887                 :             :         }
    9888                 :             :     }
    9889                 :             : 
    9890                 :             :   /* There must only be one data block.  */
    9891         [ -  + ]:         104 :   assert (elf_getdata (scn, data) == NULL);
    9892                 :             : }
    9893                 :             : 
    9894                 :             : 
    9895                 :             : static void
    9896                 :          40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
    9897                 :             :                               Ebl *ebl,
    9898                 :             :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
    9899                 :             :                               Elf_Scn *scn, GElf_Shdr *shdr,
    9900                 :             :                               Dwarf *dbg, FILE *out)
    9901                 :             : {
    9902                 :          40 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
    9903         [ +  - ]:          40 :   if (data == NULL)
    9904                 :           0 :     return;
    9905                 :             : 
    9906                 :          40 :   fprintf (out, _("\
    9907                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
    9908                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
    9909                 :          40 :            (uint64_t) shdr->sh_offset);
    9910                 :             : 
    9911                 :             :   /* For the listptr to get the base address/CU.  */
    9912                 :          40 :   sort_listptr (&known_loclistsptr, "loclistsptr");
    9913                 :          40 :   size_t listptr_idx = 0;
    9914                 :             : 
    9915                 :          40 :   const unsigned char *readp = data->d_buf;
    9916                 :          40 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
    9917                 :          40 :                                         + data->d_size);
    9918         [ +  + ]:        3132 :   while (readp < dataend)
    9919                 :             :     {
    9920         [ -  + ]:        3092 :       if (unlikely (readp > dataend - 4))
    9921                 :             :         {
    9922                 :           0 :         invalid_data:
    9923                 :           0 :           error (0, 0, _("invalid data in section [%zu] '%s'"),
    9924                 :             :                  elf_ndxscn (scn), section_name (ebl, shdr));
    9925                 :           0 :           return;
    9926                 :             :         }
    9927                 :             : 
    9928                 :        3092 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
    9929                 :        3092 :       fprintf (out, _("Table at Offset 0x%" PRIx64 ":\n\n"),
    9930                 :             :                (uint64_t) offset);
    9931                 :             : 
    9932         [ -  + ]:        3092 :       uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    9933                 :        3092 :       unsigned int offset_size = 4;
    9934         [ -  + ]:        3092 :       if (unlikely (unit_length == 0xffffffff))
    9935                 :             :         {
    9936         [ #  # ]:           0 :           if (unlikely (readp > dataend - 8))
    9937                 :           0 :             goto invalid_data;
    9938                 :             : 
    9939         [ #  # ]:           0 :           unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    9940                 :           0 :           offset_size = 8;
    9941                 :             :         }
    9942                 :        3092 :       fprintf (out, _(" Length:         %8" PRIu64 "\n"), unit_length);
    9943                 :             : 
    9944                 :             :       /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
    9945                 :             :          bytes to complete the header.  And this unit cannot go beyond
    9946                 :             :          the section data.  */
    9947         [ +  - ]:        3092 :       if (readp > dataend - 8
    9948         [ +  - ]:        3092 :           || unit_length < 8
    9949         [ -  + ]:        3092 :           || unit_length > (uint64_t) (dataend - readp))
    9950                 :           0 :         goto invalid_data;
    9951                 :             : 
    9952                 :        3092 :       const unsigned char *nexthdr = readp + unit_length;
    9953                 :             : 
    9954         [ -  + ]:        3092 :       uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    9955                 :        3092 :       fprintf (out, _(" DWARF version:  %8" PRIu16 "\n"), version);
    9956                 :             : 
    9957         [ -  + ]:        3092 :       if (version != 5)
    9958                 :             :         {
    9959                 :           0 :           error (0, 0, _("Unknown version"));
    9960                 :           0 :           goto next_table;
    9961                 :             :         }
    9962                 :             : 
    9963                 :        3092 :       uint8_t address_size = *readp++;
    9964                 :        3092 :       fprintf (out, _(" Address size:   %8" PRIu64 "\n"),
    9965                 :             :                (uint64_t) address_size);
    9966                 :             : 
    9967         [ -  + ]:        3092 :       if (address_size != 4 && address_size != 8)
    9968                 :             :         {
    9969                 :           0 :           error (0, 0, _("unsupported address size"));
    9970                 :           0 :           goto next_table;
    9971                 :             :         }
    9972                 :             : 
    9973                 :        3092 :       uint8_t segment_size = *readp++;
    9974                 :        3092 :       fprintf (out, _(" Segment size:   %8" PRIu64 "\n"),
    9975                 :             :                (uint64_t) segment_size);
    9976                 :             : 
    9977         [ -  + ]:        3092 :       if (segment_size != 0)
    9978                 :             :         {
    9979                 :           0 :           error (0, 0, _("unsupported segment size"));
    9980                 :           0 :           goto next_table;
    9981                 :             :         }
    9982                 :             : 
    9983         [ -  + ]:        3092 :       uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
    9984                 :        3092 :       fprintf (out, _(" Offset entries: %8" PRIu64 "\n"),
    9985                 :             :                (uint64_t) offset_entry_count);
    9986                 :             : 
    9987                 :             :       /* We need the CU that uses this unit to get the initial base address. */
    9988                 :        3092 :       Dwarf_Addr cu_base = 0;
    9989                 :        3092 :       struct Dwarf_CU *cu = NULL;
    9990         [ -  + ]:        3092 :       if (listptr_cu (&known_loclistsptr, &listptr_idx,
    9991                 :             :                       (Dwarf_Off) offset,
    9992                 :        3092 :                       (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
    9993                 :             :                       &cu_base, &cu)
    9994         [ #  # ]:           0 :           || split_dwarf_cu_base (dbg, &cu, &cu_base))
    9995                 :        3092 :         {
    9996                 :        3092 :           Dwarf_Die cudie;
    9997         [ -  + ]:        3092 :           if (dwarf_cu_die (cu, &cudie,
    9998                 :             :                             NULL, NULL, NULL, NULL,
    9999                 :             :                             NULL, NULL) == NULL)
   10000                 :           0 :             fprintf (out, _(" Unknown CU base: "));
   10001                 :             :           else
   10002                 :        3092 :             fprintf (out, _(" CU [%6" PRIx64 "] base: "),
   10003                 :             :                      dwarf_dieoffset (&cudie));
   10004                 :        3092 :           print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
   10005                 :        3092 :           fprintf (out, "\n");
   10006                 :             :         }
   10007                 :             :       else
   10008                 :           0 :         fprintf (out, _(" Not associated with a CU.\n"));
   10009                 :             : 
   10010                 :        3092 :       fprintf (out, "\n");
   10011                 :             : 
   10012                 :        3092 :       const unsigned char *offset_array_start = readp;
   10013         [ +  + ]:        3092 :       if (offset_entry_count > 0)
   10014                 :             :         {
   10015                 :           4 :           uint64_t max_entries = (unit_length - 8) / offset_size;
   10016         [ -  + ]:           4 :           if (offset_entry_count > max_entries)
   10017                 :             :             {
   10018                 :           0 :               error (0, 0,
   10019                 :           0 :                      _("too many offset entries for unit length"));
   10020                 :           0 :               offset_entry_count = max_entries;
   10021                 :             :             }
   10022                 :             : 
   10023                 :           4 :           fprintf (out, _("  Offsets starting at 0x%" PRIx64 ":\n"),
   10024                 :             :                    (uint64_t) (offset_array_start
   10025                 :           4 :                                - (unsigned char *) data->d_buf));
   10026         [ +  + ]:          36 :           for (uint32_t idx = 0; idx < offset_entry_count; idx++)
   10027                 :             :             {
   10028                 :          32 :               fprintf (out, "   [%6" PRIu32 "] ", idx);
   10029         [ +  - ]:          32 :               if (offset_size == 4)
   10030                 :             :                 {
   10031         [ -  + ]:          32 :                   uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
   10032                 :          32 :                   fprintf (out, "0x%" PRIx32 "\n", off);
   10033                 :             :                 }
   10034                 :             :               else
   10035                 :             :                 {
   10036         [ #  # ]:           0 :                   uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
   10037                 :           0 :                   fprintf (out, "0x%" PRIx64 "\n", off);
   10038                 :             :                 }
   10039                 :             :             }
   10040                 :           4 :           fprintf (out, "\n");
   10041                 :             :         }
   10042                 :             : 
   10043                 :        3092 :       Dwarf_Addr base = cu_base;
   10044                 :        3092 :       bool start_of_list = true;
   10045         [ +  + ]:      766970 :       while (readp < nexthdr)
   10046                 :             :         {
   10047                 :      763878 :           Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
   10048         [ +  + ]:      763878 :           if (listptr_attr (&known_loclistsptr, listptr_idx, off,
   10049                 :             :                             DW_AT_GNU_locviews))
   10050                 :      109742 :             {
   10051                 :      109742 :               Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
   10052                 :             :                                                         &listptr_idx, off);
   10053                 :      109742 :               const unsigned char *locp = readp;
   10054                 :      109742 :               const unsigned char *locendp;
   10055         [ +  - ]:      109742 :               if (next_off == 0
   10056         [ +  - ]:      109742 :                   || next_off > (size_t) (nexthdr - ((const unsigned char *)
   10057                 :             :                                                      data->d_buf)))
   10058                 :             :                 locendp = nexthdr;
   10059                 :             :               else
   10060                 :      109742 :                 locendp = (const unsigned char *) data->d_buf + next_off;
   10061                 :             : 
   10062                 :      109742 :               fprintf (out, "  Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
   10063                 :             :                        (uint64_t) (readp - (unsigned char *) data->d_buf),
   10064                 :      109742 :                        (uint64_t) (readp - offset_array_start));
   10065                 :             : 
   10066         [ +  + ]:      608414 :               while (locp < locendp)
   10067                 :             :                 {
   10068                 :      498672 :                   uint64_t v1, v2;
   10069                 :      498672 :                   get_uleb128 (v1, locp, locendp);
   10070         [ -  + ]:      498672 :                   if (locp >= locendp)
   10071                 :             :                     {
   10072                 :           0 :                       fprintf (out, _("    <INVALID DATA>\n"));
   10073                 :           0 :                       break;
   10074                 :             :                     }
   10075                 :      498672 :                   get_uleb128 (v2, locp, locendp);
   10076                 :      498672 :                   fprintf (out,
   10077                 :             :                            "    view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
   10078                 :             :                 }
   10079                 :             : 
   10080                 :      109742 :               fprintf (out, "\n");
   10081                 :      109742 :               readp = (unsigned char *) locendp;
   10082                 :      109742 :               continue;
   10083                 :             :             }
   10084                 :             : 
   10085                 :      654136 :           uint8_t kind = *readp++;
   10086                 :      654136 :           uint64_t op1, op2, len;
   10087                 :             : 
   10088                 :             :           /* Skip padding.  */
   10089         [ -  + ]:      654136 :           if (start_of_list && kind == DW_LLE_end_of_list)
   10090                 :           0 :             continue;
   10091                 :             : 
   10092         [ +  + ]:      654136 :           if (start_of_list)
   10093                 :             :             {
   10094                 :      109806 :               base = cu_base;
   10095                 :      109806 :               fprintf (out, "  Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
   10096                 :      109806 :                        (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
   10097                 :      109806 :                        (uint64_t) (readp - offset_array_start - 1));
   10098                 :      109806 :               start_of_list = false;
   10099                 :             :             }
   10100                 :             : 
   10101                 :      654136 :           fprintf (out, "    %s", dwarf_loc_list_encoding_name (kind));
   10102   [ +  -  -  +  :      654136 :           switch (kind)
          +  -  +  -  +  
                   -  - ]
   10103                 :             :             {
   10104                 :      109806 :             case DW_LLE_end_of_list:
   10105                 :      109806 :               start_of_list = true;
   10106                 :      109806 :               fprintf (out, "\n\n");
   10107                 :      109806 :               break;
   10108                 :             : 
   10109                 :           0 :             case DW_LLE_base_addressx:
   10110         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10111                 :             :                 {
   10112                 :           0 :                 invalid_entry:
   10113                 :           0 :                   error (0, 0, _("invalid loclists data"));
   10114                 :           0 :                   goto next_table;
   10115                 :             :                 }
   10116                 :           0 :               get_uleb128 (op1, readp, nexthdr);
   10117                 :           0 :               fprintf (out, " %" PRIx64 "\n", op1);
   10118         [ #  # ]:           0 :               if (! print_unresolved_addresses)
   10119                 :             :                 {
   10120                 :           0 :                   Dwarf_Addr addr;
   10121         [ #  # ]:           0 :                   if (get_indexed_addr (cu, op1, &addr) != 0)
   10122                 :           0 :                     fprintf (out, "      ???\n");
   10123                 :             :                   else
   10124                 :             :                     {
   10125                 :           0 :                       fprintf (out, "      ");
   10126                 :           0 :                       print_dwarf_addr (dwflmod, address_size, addr, addr, out);
   10127                 :           0 :                       fprintf (out, "\n");
   10128                 :             :                     }
   10129                 :             :                 }
   10130                 :             :               break;
   10131                 :             : 
   10132                 :           0 :             case DW_LLE_startx_endx:
   10133         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10134                 :           0 :                 goto invalid_entry;
   10135                 :           0 :               get_uleb128 (op1, readp, nexthdr);
   10136         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10137                 :           0 :                 goto invalid_entry;
   10138                 :           0 :               get_uleb128 (op2, readp, nexthdr);
   10139                 :           0 :               fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
   10140         [ #  # ]:           0 :               if (! print_unresolved_addresses)
   10141                 :             :                 {
   10142                 :           0 :                   Dwarf_Addr addr1;
   10143                 :           0 :                   Dwarf_Addr addr2;
   10144         [ #  # ]:           0 :                   if (get_indexed_addr (cu, op1, &addr1) != 0
   10145         [ #  # ]:           0 :                       || get_indexed_addr (cu, op2, &addr2) != 0)
   10146                 :             :                     {
   10147                 :           0 :                       fprintf (out, "      ???..\n");
   10148                 :           0 :                       fprintf (out, "      ???\n");
   10149                 :             :                     }
   10150                 :             :                   else
   10151                 :             :                     {
   10152                 :           0 :                       fprintf (out, "      ");
   10153                 :           0 :                       print_dwarf_addr (dwflmod, address_size,
   10154                 :             :                                         addr1, addr1, out);
   10155                 :           0 :                       fprintf (out, "..\n      ");
   10156                 :           0 :                       print_dwarf_addr (dwflmod, address_size,
   10157                 :             :                                         addr2 - 1, addr2, out);
   10158                 :           0 :                       fprintf (out, "\n");
   10159                 :             :                     }
   10160                 :             :                 }
   10161         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10162                 :           0 :                 goto invalid_entry;
   10163                 :           0 :               get_uleb128 (len, readp, nexthdr);
   10164         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < len)
   10165                 :           0 :                 goto invalid_entry;
   10166                 :           0 :               print_ops (dwflmod, dbg, 8, 8, version,
   10167                 :             :                          address_size, offset_size, cu, len, readp, out);
   10168                 :           0 :               readp += len;
   10169                 :           0 :               break;
   10170                 :             : 
   10171                 :          52 :             case DW_LLE_startx_length:
   10172         [ -  + ]:          52 :               if ((uint64_t) (nexthdr - readp) < 1)
   10173                 :           0 :                 goto invalid_entry;
   10174                 :          52 :               get_uleb128 (op1, readp, nexthdr);
   10175         [ -  + ]:          52 :               if ((uint64_t) (nexthdr - readp) < 1)
   10176                 :           0 :                 goto invalid_entry;
   10177                 :          52 :               get_uleb128 (op2, readp, nexthdr);
   10178                 :          52 :               fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
   10179         [ +  - ]:          52 :               if (! print_unresolved_addresses)
   10180                 :             :                 {
   10181                 :          52 :                   Dwarf_Addr addr1;
   10182                 :          52 :                   Dwarf_Addr addr2;
   10183         [ -  + ]:          52 :                   if (get_indexed_addr (cu, op1, &addr1) != 0)
   10184                 :             :                     {
   10185                 :           0 :                       fprintf (out, "      ???..\n");
   10186                 :           0 :                       fprintf (out, "      ???\n");
   10187                 :             :                     }
   10188                 :             :                   else
   10189                 :             :                     {
   10190                 :          52 :                       addr2 = addr1 + op2;
   10191                 :          52 :                       fprintf (out, "      ");
   10192                 :          52 :                       print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
   10193                 :          52 :                       fprintf (out, "..\n      ");
   10194                 :          52 :                       print_dwarf_addr (dwflmod, address_size,
   10195                 :             :                                         addr2 - 1, addr2, out);
   10196                 :          52 :                       fprintf (out, "\n");
   10197                 :             :                     }
   10198                 :             :                 }
   10199         [ -  + ]:          52 :               if ((uint64_t) (nexthdr - readp) < 1)
   10200                 :           0 :                 goto invalid_entry;
   10201                 :          52 :               get_uleb128 (len, readp, nexthdr);
   10202         [ -  + ]:          52 :               if ((uint64_t) (nexthdr - readp) < len)
   10203                 :           0 :                 goto invalid_entry;
   10204                 :          52 :               print_ops (dwflmod, dbg, 8, 8, version,
   10205                 :             :                          address_size, offset_size, cu, len, readp, out);
   10206                 :          52 :               readp += len;
   10207                 :          52 :               break;
   10208                 :             : 
   10209                 :      476662 :             case DW_LLE_offset_pair:
   10210         [ -  + ]:      476662 :               if ((uint64_t) (nexthdr - readp) < 1)
   10211                 :           0 :                 goto invalid_entry;
   10212                 :      476662 :               get_uleb128 (op1, readp, nexthdr);
   10213         [ -  + ]:      476662 :               if ((uint64_t) (nexthdr - readp) < 1)
   10214                 :           0 :                 goto invalid_entry;
   10215                 :      476662 :               get_uleb128 (op2, readp, nexthdr);
   10216                 :      476662 :               fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
   10217         [ +  - ]:      476662 :               if (! print_unresolved_addresses)
   10218                 :             :                 {
   10219                 :      476662 :                   op1 += base;
   10220                 :      476662 :                   op2 += base;
   10221                 :      476662 :                   fprintf (out, "      ");
   10222                 :      476662 :                   print_dwarf_addr (dwflmod, address_size, op1, op1, out);
   10223                 :      476662 :                   fprintf (out, "..\n      ");
   10224                 :      476662 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
   10225                 :      476662 :                   fprintf (out, "\n");
   10226                 :             :                 }
   10227         [ -  + ]:      476662 :               if ((uint64_t) (nexthdr - readp) < 1)
   10228                 :           0 :                 goto invalid_entry;
   10229                 :      476662 :               get_uleb128 (len, readp, nexthdr);
   10230         [ -  + ]:      476662 :               if ((uint64_t) (nexthdr - readp) < len)
   10231                 :           0 :                 goto invalid_entry;
   10232                 :      476662 :               print_ops (dwflmod, dbg, 8, 8, version,
   10233                 :             :                          address_size, offset_size, cu, len, readp, out);
   10234                 :      476662 :               readp += len;
   10235                 :      476662 :               break;
   10236                 :             : 
   10237                 :           0 :             case DW_LLE_default_location:
   10238         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10239                 :           0 :                 goto invalid_entry;
   10240                 :           0 :               get_uleb128 (len, readp, nexthdr);
   10241         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < len)
   10242                 :           0 :                 goto invalid_entry;
   10243                 :           0 :               print_ops (dwflmod, dbg, 8, 8, version,
   10244                 :             :                          address_size, offset_size, cu, len, readp, out);
   10245                 :           0 :               readp += len;
   10246                 :           0 :               break;
   10247                 :             : 
   10248                 :       45554 :             case DW_LLE_base_address:
   10249         [ -  + ]:       45554 :               if (address_size == 4)
   10250                 :             :                 {
   10251         [ #  # ]:           0 :                   if ((uint64_t) (nexthdr - readp) < 4)
   10252                 :           0 :                     goto invalid_entry;
   10253         [ #  # ]:           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
   10254                 :             :                 }
   10255                 :             :               else
   10256                 :             :                 {
   10257         [ -  + ]:       45554 :                   if ((uint64_t) (nexthdr - readp) < 8)
   10258                 :           0 :                     goto invalid_entry;
   10259         [ -  + ]:       45554 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
   10260                 :             :                 }
   10261                 :       45554 :               base = op1;
   10262                 :       45554 :               fprintf (out, " 0x%" PRIx64 "\n", base);
   10263         [ +  - ]:       45554 :               if (! print_unresolved_addresses)
   10264                 :             :                 {
   10265                 :       45554 :                   fprintf (out, "      ");
   10266                 :       45554 :                   print_dwarf_addr (dwflmod, address_size, base, base, out);
   10267                 :       45554 :                   fprintf (out, "\n");
   10268                 :             :                 }
   10269                 :             :               break;
   10270                 :             : 
   10271                 :           0 :             case DW_LLE_start_end:
   10272         [ #  # ]:           0 :               if (address_size == 4)
   10273                 :             :                 {
   10274         [ #  # ]:           0 :                   if ((uint64_t) (nexthdr - readp) < 8)
   10275                 :           0 :                     goto invalid_entry;
   10276         [ #  # ]:           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
   10277         [ #  # ]:           0 :                   op2 = read_4ubyte_unaligned_inc (dbg, readp);
   10278                 :             :                 }
   10279                 :             :               else
   10280                 :             :                 {
   10281         [ #  # ]:           0 :                   if ((uint64_t) (nexthdr - readp) < 16)
   10282                 :           0 :                     goto invalid_entry;
   10283         [ #  # ]:           0 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
   10284         [ #  # ]:           0 :                   op2 = read_8ubyte_unaligned_inc (dbg, readp);
   10285                 :             :                 }
   10286                 :           0 :               fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
   10287         [ #  # ]:           0 :               if (! print_unresolved_addresses)
   10288                 :             :                 {
   10289                 :           0 :                   fprintf (out, "      ");
   10290                 :           0 :                   print_dwarf_addr (dwflmod, address_size, op1, op1, out);
   10291                 :           0 :                   fprintf (out, "..\n      ");
   10292                 :           0 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
   10293                 :           0 :                   fprintf (out, "\n");
   10294                 :             :                 }
   10295         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10296                 :           0 :                 goto invalid_entry;
   10297                 :           0 :               get_uleb128 (len, readp, nexthdr);
   10298         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < len)
   10299                 :           0 :                 goto invalid_entry;
   10300                 :           0 :               print_ops (dwflmod, dbg, 8, 8, version,
   10301                 :             :                          address_size, offset_size, cu, len, readp, out);
   10302                 :           0 :               readp += len;
   10303                 :           0 :               break;
   10304                 :             : 
   10305                 :       22062 :             case DW_LLE_start_length:
   10306         [ -  + ]:       22062 :               if (address_size == 4)
   10307                 :             :                 {
   10308         [ #  # ]:           0 :                   if ((uint64_t) (nexthdr - readp) < 4)
   10309                 :           0 :                     goto invalid_entry;
   10310         [ #  # ]:           0 :                   op1 = read_4ubyte_unaligned_inc (dbg, readp);
   10311                 :             :                 }
   10312                 :             :               else
   10313                 :             :                 {
   10314         [ -  + ]:       22062 :                   if ((uint64_t) (nexthdr - readp) < 8)
   10315                 :           0 :                     goto invalid_entry;
   10316         [ -  + ]:       22062 :                   op1 = read_8ubyte_unaligned_inc (dbg, readp);
   10317                 :             :                 }
   10318         [ -  + ]:       22062 :               if ((uint64_t) (nexthdr - readp) < 1)
   10319                 :           0 :                 goto invalid_entry;
   10320                 :       22062 :               get_uleb128 (op2, readp, nexthdr);
   10321                 :       22062 :               fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
   10322         [ +  + ]:       22062 :               if (! print_unresolved_addresses)
   10323                 :             :                 {
   10324                 :       22060 :                   op2 = op1 + op2;
   10325                 :       22060 :                   fprintf (out, "      ");
   10326                 :       22060 :                   print_dwarf_addr (dwflmod, address_size, op1, op1, out);
   10327                 :       22060 :                   fprintf (out, "..\n      ");
   10328                 :       22060 :                   print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
   10329                 :       22060 :                   fprintf (out, "\n");
   10330                 :             :                 }
   10331         [ -  + ]:       22062 :               if ((uint64_t) (nexthdr - readp) < 1)
   10332                 :           0 :                 goto invalid_entry;
   10333                 :       22062 :               get_uleb128 (len, readp, nexthdr);
   10334         [ -  + ]:       22062 :               if ((uint64_t) (nexthdr - readp) < len)
   10335                 :           0 :                 goto invalid_entry;
   10336                 :       22062 :               print_ops (dwflmod, dbg, 8, 8, version,
   10337                 :             :                          address_size, offset_size, cu, len, readp, out);
   10338                 :       22062 :               readp += len;
   10339                 :       22062 :               break;
   10340                 :             : 
   10341                 :           0 :             case DW_LLE_GNU_view_pair:
   10342         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10343                 :           0 :                 goto invalid_entry;
   10344                 :           0 :               get_uleb128 (op1, readp, nexthdr);
   10345         [ #  # ]:           0 :               if ((uint64_t) (nexthdr - readp) < 1)
   10346                 :           0 :                 goto invalid_entry;
   10347                 :           0 :               get_uleb128 (op2, readp, nexthdr);
   10348                 :           0 :               fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
   10349                 :           0 :               break;
   10350                 :             : 
   10351                 :           0 :             default:
   10352                 :           0 :               goto invalid_entry;
   10353                 :             :             }
   10354                 :             :         }
   10355                 :             : 
   10356                 :        3092 :     next_table:
   10357         [ -  + ]:        3092 :       if (readp != nexthdr)
   10358                 :             :         {
   10359                 :           0 :           size_t padding = nexthdr - readp;
   10360                 :           0 :           fprintf (out, _("   %zu padding bytes\n\n"), padding);
   10361                 :           0 :           readp = nexthdr;
   10362                 :             :         }
   10363                 :             :     }
   10364                 :             : }
   10365                 :             : 
   10366                 :             : 
   10367                 :             : static void
   10368                 :          52 : print_debug_loc_section (Dwfl_Module *dwflmod,
   10369                 :             :                          Ebl *ebl, GElf_Ehdr *ehdr,
   10370                 :             :                          Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
   10371                 :             :                          FILE *out)
   10372                 :             : {
   10373                 :          52 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
   10374         [ -  + ]:          52 :   if (data == NULL)
   10375                 :           0 :     return;
   10376                 :             : 
   10377                 :          52 :   fprintf (out, _("\
   10378                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10379                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
   10380                 :          52 :            (uint64_t) shdr->sh_offset);
   10381                 :             : 
   10382                 :          52 :   sort_listptr (&known_locsptr, "loclistptr");
   10383                 :          52 :   size_t listptr_idx = 0;
   10384                 :             : 
   10385         [ +  - ]:          52 :   uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
   10386                 :          52 :   uint_fast8_t offset_size = 4;
   10387                 :             : 
   10388                 :          52 :   bool first = true;
   10389                 :          52 :   Dwarf_Addr base = 0;
   10390                 :          52 :   unsigned char *readp = data->d_buf;
   10391                 :          52 :   unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
   10392                 :          52 :   Dwarf_CU *last_cu = NULL;
   10393         [ +  + ]:         490 :   while (readp < endp)
   10394                 :             :     {
   10395                 :         438 :       ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
   10396                 :         438 :       Dwarf_CU *cu = last_cu;
   10397                 :         438 :       unsigned int attr = 0;
   10398                 :             : 
   10399   [ +  +  -  + ]:         438 :       if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
   10400                 :             :                                       &address_size, &offset_size, &base,
   10401                 :             :                                       &cu, offset, &readp, endp, &attr))
   10402                 :           0 :         continue;
   10403                 :             : 
   10404         [ +  + ]:         438 :       if (last_cu != cu)
   10405                 :             :        {
   10406                 :          60 :         Dwarf_Die cudie;
   10407         [ -  + ]:          60 :         if (dwarf_cu_die (cu, &cudie,
   10408                 :             :                           NULL, NULL, NULL, NULL,
   10409                 :             :                           NULL, NULL) == NULL)
   10410                 :           0 :           fprintf (out, _("\n Unknown CU base: "));
   10411                 :             :         else
   10412                 :          60 :           fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
   10413                 :             :                    dwarf_dieoffset (&cudie));
   10414                 :          60 :         print_dwarf_addr (dwflmod, address_size, base, base, out);
   10415                 :          60 :         fprintf (out, "\n");
   10416                 :             :        }
   10417                 :         438 :       last_cu = cu;
   10418                 :             : 
   10419         [ -  + ]:         438 :       if (attr == DW_AT_GNU_locviews)
   10420                 :           0 :         {
   10421                 :           0 :           Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
   10422                 :             :                                                     &listptr_idx, offset);
   10423                 :           0 :           const unsigned char *locp = readp;
   10424                 :           0 :           const unsigned char *locendp;
   10425         [ #  # ]:           0 :           if (next_off == 0
   10426                 :           0 :               || next_off > (size_t) (endp
   10427         [ #  # ]:           0 :                                       - (const unsigned char *) data->d_buf))
   10428                 :           0 :             locendp = endp;
   10429                 :             :           else
   10430                 :           0 :             locendp = (const unsigned char *) data->d_buf + next_off;
   10431                 :             : 
   10432         [ #  # ]:           0 :           while (locp < locendp)
   10433                 :             :             {
   10434                 :           0 :               uint64_t v1, v2;
   10435                 :           0 :               get_uleb128 (v1, locp, locendp);
   10436         [ #  # ]:           0 :               if (locp >= locendp)
   10437                 :             :                 {
   10438                 :           0 :                   fprintf (out, _(" [%6tx]  <INVALID DATA>\n"), offset);
   10439                 :           0 :                   break;
   10440                 :             :                 }
   10441                 :           0 :               get_uleb128 (v2, locp, locendp);
   10442         [ #  # ]:           0 :               if (first)                /* First view pair in a list.  */
   10443                 :           0 :                 fprintf (out, " [%6tx] ", offset);
   10444                 :             :               else
   10445                 :           0 :                 fprintf (out, "          ");
   10446                 :           0 :               fprintf (out, "view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
   10447                 :           0 :               first = false;
   10448                 :             :             }
   10449                 :             : 
   10450                 :           0 :           first = true;
   10451                 :           0 :           readp = (unsigned char *) locendp;
   10452                 :           0 :           continue;
   10453                 :             :         }
   10454                 :             : 
   10455                 :             :       /* GNU DebugFission encoded addresses as addrx.  */
   10456                 :        1194 :       bool is_debugfission = ((cu != NULL
   10457         [ #  # ]:           0 :                                || split_dwarf_cu_base (dbg, &cu, &base))
   10458   [ -  +  +  - ]:         438 :                               && (cu->version < 5
   10459         [ +  + ]:         438 :                                   && cu->unit_type == DW_UT_split_compile));
   10460                 :         318 :       if (!is_debugfission
   10461         [ -  + ]:         318 :           && unlikely (data->d_size - offset < (size_t) address_size * 2))
   10462                 :             :         {
   10463                 :           0 :         invalid_data:
   10464                 :           0 :           fprintf (out, _(" [%6tx]  <INVALID DATA>\n"), offset);
   10465                 :           0 :           break;
   10466                 :             :         }
   10467                 :             : 
   10468                 :         438 :       Dwarf_Addr begin;
   10469                 :         438 :       Dwarf_Addr end;
   10470                 :         438 :       bool use_base = true;
   10471         [ +  + ]:         438 :       if (is_debugfission)
   10472                 :             :         {
   10473                 :         120 :           const unsigned char *locp = readp;
   10474                 :         120 :           const unsigned char *locendp = readp + data->d_size;
   10475         [ -  + ]:         120 :           if (locp >= locendp)
   10476                 :           0 :             goto invalid_data;
   10477                 :             : 
   10478                 :         120 :           Dwarf_Word idx;
   10479                 :         120 :           unsigned char code = *locp++;
   10480   [ +  -  -  +  :         120 :           switch (code)
                      - ]
   10481                 :             :             {
   10482                 :          40 :             case DW_LLE_GNU_end_of_list_entry:
   10483                 :          40 :               begin = 0;
   10484                 :          40 :               end = 0;
   10485                 :          40 :               break;
   10486                 :             : 
   10487                 :           0 :             case DW_LLE_GNU_base_address_selection_entry:
   10488         [ #  # ]:           0 :               if (locp >= locendp)
   10489                 :           0 :                 goto invalid_data;
   10490                 :           0 :               begin = (Dwarf_Addr) -1;
   10491                 :           0 :               get_uleb128 (idx, locp, locendp);
   10492         [ #  # ]:           0 :               if (get_indexed_addr (cu, idx, &end) != 0)
   10493                 :           0 :                 end = idx; /* ... */
   10494                 :             :               break;
   10495                 :             : 
   10496                 :           0 :             case DW_LLE_GNU_start_end_entry:
   10497         [ #  # ]:           0 :               if (locp >= locendp)
   10498                 :           0 :                 goto invalid_data;
   10499                 :           0 :               get_uleb128 (idx, locp, locendp);
   10500         [ #  # ]:           0 :               if (get_indexed_addr (cu, idx, &begin) != 0)
   10501                 :           0 :                 begin = idx; /* ... */
   10502         [ #  # ]:           0 :               if (locp >= locendp)
   10503                 :           0 :                 goto invalid_data;
   10504                 :           0 :               get_uleb128 (idx, locp, locendp);
   10505         [ #  # ]:           0 :               if (get_indexed_addr (cu, idx, &end) != 0)
   10506                 :           0 :                 end = idx; /* ... */
   10507                 :             :               use_base = false;
   10508                 :             :               break;
   10509                 :             : 
   10510                 :          80 :             case DW_LLE_GNU_start_length_entry:
   10511         [ -  + ]:          80 :               if (locp >= locendp)
   10512                 :           0 :                 goto invalid_data;
   10513                 :          80 :               get_uleb128 (idx, locp, locendp);
   10514         [ -  + ]:          80 :               if (get_indexed_addr (cu, idx, &begin) != 0)
   10515                 :           0 :                 begin = idx; /* ... */
   10516         [ -  + ]:          80 :               if (locendp - locp < 4)
   10517                 :           0 :                 goto invalid_data;
   10518         [ -  + ]:          80 :               end = read_4ubyte_unaligned_inc (dbg, locp);
   10519                 :          80 :               end += begin;
   10520                 :          80 :               use_base = false;
   10521                 :          80 :               break;
   10522                 :             : 
   10523                 :           0 :             default:
   10524                 :           0 :                 goto invalid_data;
   10525                 :             :             }
   10526                 :             : 
   10527                 :         120 :           readp = (unsigned char *) locp;
   10528                 :             :         }
   10529         [ +  - ]:         318 :       else if (address_size == 8)
   10530                 :             :         {
   10531         [ +  + ]:         318 :           begin = read_8ubyte_unaligned_inc (dbg, readp);
   10532         [ +  + ]:         318 :           end = read_8ubyte_unaligned_inc (dbg, readp);
   10533                 :             :         }
   10534                 :             :       else
   10535                 :             :         {
   10536         [ #  # ]:           0 :           begin = read_4ubyte_unaligned_inc (dbg, readp);
   10537         [ #  # ]:           0 :           end = read_4ubyte_unaligned_inc (dbg, readp);
   10538         [ #  # ]:           0 :           if (begin == (Dwarf_Addr) (uint32_t) -1)
   10539                 :           0 :             begin = (Dwarf_Addr) -1l;
   10540                 :             :         }
   10541                 :             : 
   10542         [ -  + ]:         438 :       if (begin == (Dwarf_Addr) -1l) /* Base address entry.  */
   10543                 :             :         {
   10544         [ #  # ]:           0 :           if (first)
   10545                 :           0 :             fprintf (out, " [%6tx] ", offset);
   10546                 :             :           else
   10547                 :           0 :             fprintf (out, "          ");
   10548                 :           0 :           fputs (_("base address\n"), out);
   10549                 :           0 :           fprintf (out, "          ");
   10550                 :           0 :           print_dwarf_addr (dwflmod, address_size, end, end, out);
   10551                 :           0 :           fprintf (out, "\n");
   10552                 :           0 :           base = end;
   10553                 :           0 :           first = false;
   10554                 :             :         }
   10555   [ +  +  +  + ]:         438 :       else if (begin == 0 && end == 0) /* End of list entry.  */
   10556                 :             :         {
   10557         [ -  + ]:         148 :           if (first)
   10558                 :           0 :             fprintf (out, _(" [%6tx] empty list\n"), offset);
   10559                 :             :           first = true;
   10560                 :             :         }
   10561                 :             :       else
   10562                 :             :         {
   10563                 :             :           /* We have a location expression entry.  */
   10564         [ +  + ]:         290 :           uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
   10565                 :             : 
   10566         [ +  + ]:         290 :           if (first)            /* First entry in a list.  */
   10567                 :         148 :             fprintf (out, " [%6tx] ", offset);
   10568                 :             :           else
   10569                 :         142 :             fprintf (out, "          ");
   10570                 :             : 
   10571                 :         290 :           fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
   10572         [ +  + ]:         290 :           if (! print_unresolved_addresses)
   10573                 :             :             {
   10574                 :         230 :               Dwarf_Addr dab = use_base ? base + begin : begin;
   10575                 :         230 :               Dwarf_Addr dae = use_base ? base + end : end;
   10576                 :         230 :               fprintf (out, "          ");
   10577                 :         230 :               print_dwarf_addr (dwflmod, address_size, dab, dab, out);
   10578                 :         230 :               fprintf (out, "..\n          ");
   10579                 :         230 :               print_dwarf_addr (dwflmod, address_size, dae - 1, dae, out);
   10580                 :         230 :               fprintf (out, "\n");
   10581                 :             :             }
   10582                 :             : 
   10583         [ -  + ]:         290 :           if (endp - readp <= (ptrdiff_t) len)
   10584                 :             :             {
   10585                 :           0 :               fputs (_("   <INVALID DATA>\n"), out);
   10586                 :           0 :               break;
   10587                 :             :             }
   10588                 :             : 
   10589         [ +  - ]:         580 :           print_ops (dwflmod, dbg, 11, 11,
   10590                 :         290 :                      cu != NULL ? cu->version : 3,
   10591                 :             :                      address_size, offset_size, cu, len, readp, out);
   10592                 :             : 
   10593                 :         290 :           first = false;
   10594                 :         290 :           readp += len;
   10595                 :             :         }
   10596                 :             :     }
   10597                 :             : }
   10598                 :             : 
   10599                 :             : struct mac_culist
   10600                 :             : {
   10601                 :             :   Dwarf_Die die;
   10602                 :             :   Dwarf_Off offset;
   10603                 :             :   Dwarf_Files *files;
   10604                 :             :   struct mac_culist *next;
   10605                 :             : };
   10606                 :             : 
   10607                 :             : 
   10608                 :             : static int
   10609                 :           0 : mac_compare (const void *p1, const void *p2)
   10610                 :             : {
   10611                 :           0 :   struct mac_culist *m1 = (struct mac_culist *) p1;
   10612                 :           0 :   struct mac_culist *m2 = (struct mac_culist *) p2;
   10613                 :             : 
   10614         [ #  # ]:           0 :   if (m1->offset < m2->offset)
   10615                 :             :     return -1;
   10616         [ #  # ]:           0 :   if (m1->offset > m2->offset)
   10617                 :           0 :     return 1;
   10618                 :             :   return 0;
   10619                 :             : }
   10620                 :             : 
   10621                 :             : 
   10622                 :             : static void
   10623                 :           0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10624                 :             :                              Ebl *ebl,
   10625                 :             :                              GElf_Ehdr *ehdr __attribute__ ((unused)),
   10626                 :             :                              Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
   10627                 :             :                              FILE *out)
   10628                 :             : {
   10629                 :           0 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
   10630         [ #  # ]:           0 :   if (data == NULL)
   10631                 :           0 :     return;
   10632                 :             : 
   10633                 :           0 :   fprintf (out, _("\
   10634                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10635                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
   10636                 :           0 :            (uint64_t) shdr->sh_offset);
   10637                 :           0 :   fputc ('\n', out);
   10638                 :             : 
   10639                 :             :   /* There is no function in libdw to iterate over the raw content of
   10640                 :             :      the section but it is easy enough to do.  */
   10641                 :             : 
   10642                 :             :   /* Get the source file information for all CUs.  */
   10643                 :           0 :   Dwarf_Off offset;
   10644                 :           0 :   Dwarf_Off ncu = 0;
   10645                 :           0 :   size_t hsize;
   10646                 :           0 :   struct mac_culist *culist = NULL;
   10647                 :           0 :   size_t nculist = 0;
   10648         [ #  # ]:           0 :   while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
   10649                 :             :     {
   10650                 :           0 :       Dwarf_Die cudie;
   10651         [ #  # ]:           0 :       if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
   10652                 :           0 :         continue;
   10653                 :             : 
   10654                 :           0 :       Dwarf_Attribute attr;
   10655         [ #  # ]:           0 :       if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
   10656                 :           0 :         continue;
   10657                 :             : 
   10658                 :           0 :       Dwarf_Word macoff;
   10659         [ #  # ]:           0 :       if (dwarf_formudata (&attr, &macoff) != 0)
   10660                 :           0 :         continue;
   10661                 :             : 
   10662                 :           0 :       struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
   10663                 :           0 :       newp->die = cudie;
   10664                 :           0 :       newp->offset = macoff;
   10665                 :           0 :       newp->files = NULL;
   10666                 :           0 :       newp->next = culist;
   10667                 :           0 :       culist = newp;
   10668                 :           0 :       ++nculist;
   10669                 :             :     }
   10670                 :             : 
   10671                 :             :   /* Convert the list into an array for easier consumption.  */
   10672                 :           0 :   struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
   10673                 :             :                                                          * sizeof (*cus));
   10674                 :             :   /* Add sentinel.  */
   10675                 :           0 :   cus[nculist].offset = data->d_size;
   10676                 :           0 :   cus[nculist].files = (Dwarf_Files *) -1l;
   10677         [ #  # ]:           0 :   if (nculist > 0)
   10678                 :             :     {
   10679         [ #  # ]:           0 :       for (size_t cnt = nculist - 1; culist != NULL; --cnt)
   10680                 :             :         {
   10681         [ #  # ]:           0 :           assert (cnt < nculist);
   10682                 :           0 :           cus[cnt] = *culist;
   10683                 :           0 :           culist = culist->next;
   10684                 :             :         }
   10685                 :             : 
   10686                 :             :       /* Sort the array according to the offset in the .debug_macinfo
   10687                 :             :          section.  Note we keep the sentinel at the end.  */
   10688                 :           0 :       qsort (cus, nculist, sizeof (*cus), mac_compare);
   10689                 :             :     }
   10690                 :             : 
   10691                 :           0 :   const unsigned char *readp = (const unsigned char *) data->d_buf;
   10692                 :           0 :   const unsigned char *readendp = readp + data->d_size;
   10693                 :           0 :   int level = 1;
   10694                 :             : 
   10695         [ #  # ]:           0 :   while (readp < readendp)
   10696                 :             :     {
   10697                 :           0 :       unsigned int opcode = *readp++;
   10698                 :           0 :       unsigned int u128;
   10699                 :           0 :       unsigned int u128_2;
   10700                 :           0 :       const unsigned char *endp;
   10701                 :             : 
   10702   [ #  #  #  # ]:           0 :       switch (opcode)
   10703                 :             :         {
   10704                 :           0 :         case DW_MACINFO_define:
   10705                 :             :         case DW_MACINFO_undef:
   10706                 :             :         case DW_MACINFO_vendor_ext:
   10707                 :             :           /*  For the first two opcodes the parameters are
   10708                 :             :                 line, string
   10709                 :             :               For the latter
   10710                 :             :                 number, string.
   10711                 :             :               We can treat these cases together.  */
   10712                 :           0 :           get_uleb128 (u128, readp, readendp);
   10713                 :             : 
   10714                 :           0 :           endp = memchr (readp, '\0', readendp - readp);
   10715         [ #  # ]:           0 :           if (unlikely (endp == NULL))
   10716                 :             :             {
   10717                 :           0 :               fprintf (out, _("\
   10718                 :             : %*s*** non-terminated string at end of section"),
   10719                 :             :                        level, "");
   10720                 :           0 :               return;
   10721                 :             :             }
   10722                 :             : 
   10723         [ #  # ]:           0 :           if (opcode == DW_MACINFO_define)
   10724                 :           0 :             fprintf (out, "%*s#define %s, line %u\n",
   10725                 :             :                      level, "", (char *) readp, u128);
   10726         [ #  # ]:           0 :           else if (opcode == DW_MACINFO_undef)
   10727                 :           0 :             fprintf (out, "%*s#undef %s, line %u\n",
   10728                 :             :                      level, "", (char *) readp, u128);
   10729                 :             :           else
   10730                 :           0 :             fprintf (out, " #vendor-ext %s, number %u\n", (char *) readp, u128);
   10731                 :             : 
   10732                 :           0 :           readp = endp + 1;
   10733                 :           0 :           break;
   10734                 :             : 
   10735                 :           0 :         case DW_MACINFO_start_file:
   10736                 :             :           /* The two parameters are line and file index, in this order.  */
   10737                 :           0 :           get_uleb128 (u128, readp, readendp);
   10738         [ #  # ]:           0 :           if (readendp - readp < 1)
   10739                 :             :             {
   10740                 :           0 :               fprintf (out, _("\
   10741                 :             : %*s*** missing DW_MACINFO_start_file argument at end of section"),
   10742                 :             :                        level, "");
   10743                 :           0 :               return;
   10744                 :             :             }
   10745                 :           0 :           get_uleb128 (u128_2, readp, readendp);
   10746                 :             : 
   10747                 :             :           /* Find the CU DIE for this file.  */
   10748                 :           0 :           size_t macoff = readp - (const unsigned char *) data->d_buf;
   10749                 :           0 :           const char *fname = "???";
   10750   [ #  #  #  # ]:           0 :           if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
   10751                 :             :             {
   10752   [ #  #  #  # ]:           0 :               while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
   10753                 :           0 :                 ++cus;
   10754                 :             : 
   10755         [ #  # ]:           0 :               if (cus[0].files == NULL
   10756         [ #  # ]:           0 :                   && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
   10757                 :           0 :                 cus[0].files = (Dwarf_Files *) -1l;
   10758                 :             : 
   10759         [ #  # ]:           0 :               if (cus[0].files != (Dwarf_Files *) -1l)
   10760                 :           0 :                 fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
   10761         [ #  # ]:           0 :                          ?: "???");
   10762                 :             :             }
   10763                 :             : 
   10764                 :           0 :           fprintf (out, "%*sstart_file %u, [%u] %s\n",
   10765                 :             :                    level, "", u128, u128_2, fname);
   10766                 :           0 :           ++level;
   10767                 :           0 :           break;
   10768                 :             : 
   10769                 :           0 :         case DW_MACINFO_end_file:
   10770                 :           0 :           --level;
   10771                 :           0 :           fprintf (out, "%*send_file\n", level, "");
   10772                 :             :           /* Nothing more to do.  */
   10773                 :           0 :           break;
   10774                 :             : 
   10775                 :           0 :         default:
   10776                 :             :           // XXX gcc seems to generate files with a trailing zero.
   10777   [ #  #  #  # ]:           0 :           if (unlikely (opcode != 0 || readp != readendp))
   10778                 :           0 :             fprintf (out, "%*s*** invalid opcode %u\n", level, "", opcode);
   10779                 :             :           break;
   10780                 :             :         }
   10781                 :             :     }
   10782                 :             : }
   10783                 :             : 
   10784                 :             : 
   10785                 :             : static void
   10786                 :           8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   10787                 :             :                            Ebl *ebl,
   10788                 :             :                            GElf_Ehdr *ehdr __attribute__ ((unused)),
   10789                 :             :                            Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
   10790                 :             :                            FILE *out)
   10791                 :             : {
   10792                 :           8 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
   10793         [ +  - ]:           8 :   if (data == NULL)
   10794                 :           0 :     return;
   10795                 :             : 
   10796                 :           8 :   fprintf (out, _("\
   10797                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   10798                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
   10799                 :           8 :            (uint64_t) shdr->sh_offset);
   10800                 :           8 :   fputc ('\n', out);
   10801                 :             : 
   10802                 :             :   /* Get the source file information for all CUs.  Uses same
   10803                 :             :      datastructure as macinfo.  But uses offset field to directly
   10804                 :             :      match .debug_line offset.  And just stored in a list.  */
   10805                 :           8 :   Dwarf_Off offset;
   10806                 :           8 :   Dwarf_Off ncu = 0;
   10807                 :           8 :   size_t hsize;
   10808                 :           8 :   struct mac_culist *culist = NULL;
   10809         [ +  + ]:          20 :   while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
   10810                 :             :     {
   10811                 :          12 :       Dwarf_Die cudie;
   10812         [ -  + ]:          12 :       if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
   10813                 :           0 :         continue;
   10814                 :             : 
   10815                 :          12 :       Dwarf_Attribute attr;
   10816         [ -  + ]:          12 :       if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
   10817                 :           0 :         continue;
   10818                 :             : 
   10819                 :          12 :       Dwarf_Word lineoff;
   10820         [ -  + ]:          12 :       if (dwarf_formudata (&attr, &lineoff) != 0)
   10821                 :           0 :         continue;
   10822                 :             : 
   10823                 :          12 :       struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
   10824                 :          12 :       newp->die = cudie;
   10825                 :          12 :       newp->offset = lineoff;
   10826                 :          12 :       newp->files = NULL;
   10827                 :          12 :       newp->next = culist;
   10828                 :          12 :       culist = newp;
   10829                 :             :     }
   10830                 :             : 
   10831                 :           8 :   const unsigned char *readp = (const unsigned char *) data->d_buf;
   10832                 :           8 :   const unsigned char *readendp = readp + data->d_size;
   10833                 :             : 
   10834         [ +  + ]:          28 :   while (readp < readendp)
   10835                 :             :     {
   10836                 :          40 :       fprintf (out, _(" Offset:             0x%" PRIx64 "\n"),
   10837                 :          20 :                (uint64_t) (readp - (const unsigned char *) data->d_buf));
   10838                 :             : 
   10839                 :             :       // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
   10840                 :             :       // optional vendor extension macro entry table.
   10841         [ -  + ]:          20 :       if (readp + 2 > readendp)
   10842                 :             :         {
   10843                 :           0 :         invalid_data:
   10844                 :           0 :           error (0, 0, _("invalid data"));
   10845                 :           0 :           return;
   10846                 :             :         }
   10847         [ -  + ]:          20 :       const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
   10848                 :          20 :       fprintf (out, _(" Version:            %" PRIu16 "\n"), vers);
   10849                 :             : 
   10850                 :             :       // Version 4 is the GNU extension for DWARF4.  DWARF5 will use version
   10851                 :             :       // 5 when it gets standardized.
   10852         [ -  + ]:          20 :       if (vers != 4 && vers != 5)
   10853                 :             :         {
   10854                 :           0 :           fprintf (out, _("  unknown version, cannot parse section\n"));
   10855                 :           0 :           return;
   10856                 :             :         }
   10857                 :             : 
   10858         [ -  + ]:          20 :       if (readp + 1 > readendp)
   10859                 :           0 :         goto invalid_data;
   10860                 :          20 :       const unsigned char flag = *readp++;
   10861                 :          20 :       fprintf (out, _(" Flag:               0x%" PRIx8), flag);
   10862         [ +  + ]:          20 :       if (flag != 0)
   10863                 :             :         {
   10864                 :          12 :           fprintf (out, " (");
   10865         [ -  + ]:          12 :           if ((flag & 0x01) != 0)
   10866                 :             :             {
   10867                 :           0 :               fprintf (out, "offset_size");
   10868         [ #  # ]:           0 :               if ((flag & 0xFE) !=  0)
   10869                 :           0 :                 fprintf (out, ", ");
   10870                 :             :             }
   10871         [ +  - ]:          12 :           if ((flag & 0x02) != 0)
   10872                 :             :             {
   10873                 :          12 :               fprintf (out, "debug_line_offset");
   10874         [ -  + ]:          12 :               if ((flag & 0xFC) !=  0)
   10875                 :           0 :                 fprintf (out, ", ");
   10876                 :             :             }
   10877         [ #  # ]:           0 :           if ((flag & 0x04) != 0)
   10878                 :             :             {
   10879                 :           0 :               fprintf (out, "operands_table");
   10880         [ #  # ]:           0 :               if ((flag & 0xF8) !=  0)
   10881                 :           0 :                 fprintf (out, ", ");
   10882                 :             :             }
   10883         [ -  + ]:          12 :           if ((flag & 0xF8) != 0)
   10884                 :           0 :             fprintf (out, "unknown");
   10885                 :          12 :           fprintf (out, ")");
   10886                 :             :         }
   10887                 :          20 :       fprintf (out, "\n");
   10888                 :             : 
   10889         [ +  - ]:          20 :       unsigned int offset_len = (flag & 0x01) ? 8 : 4;
   10890                 :          20 :       fprintf (out, _(" Offset length:      %" PRIu8 "\n"), offset_len);
   10891                 :          20 :       Dwarf_Off line_offset = -1;
   10892         [ +  + ]:          20 :       if (flag & 0x02)
   10893                 :             :         {
   10894         [ -  + ]:          12 :           if (offset_len == 8)
   10895         [ #  # ]:           0 :             line_offset = read_8ubyte_unaligned_inc (dbg, readp);
   10896                 :             :           else
   10897         [ -  + ]:          12 :             line_offset = read_4ubyte_unaligned_inc (dbg, readp);
   10898                 :          12 :           fprintf (out, _(" .debug_line offset: 0x%" PRIx64 "\n"),
   10899                 :             :                    line_offset);
   10900                 :             :         }
   10901                 :             : 
   10902                 :          12 :       struct mac_culist *cu = NULL;
   10903         [ +  - ]:          12 :       if (line_offset != (Dwarf_Off) -1)
   10904                 :             :         {
   10905                 :             :           cu = culist;
   10906   [ +  -  +  + ]:          16 :           while (cu != NULL && line_offset != cu->offset)
   10907                 :           4 :             cu = cu->next;
   10908                 :             :         }
   10909                 :             : 
   10910         [ -  + ]:          20 :       Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
   10911                 :             :                                                                ? cu->die.cu
   10912                 :             :                                                                : NULL));
   10913                 :             : 
   10914                 :          20 :       const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
   10915         [ -  + ]:          20 :       memset (vendor, 0, sizeof vendor);
   10916         [ -  + ]:          20 :       if (flag & 0x04)
   10917                 :             :         {
   10918                 :             :           // 1 byte length, for each item, 1 byte opcode, uleb128 number
   10919                 :             :           // of arguments, for each argument 1 byte form code.
   10920         [ #  # ]:           0 :           if (readp + 1 > readendp)
   10921                 :           0 :             goto invalid_data;
   10922                 :           0 :           unsigned int tlen = *readp++;
   10923                 :           0 :           fprintf (out, _("  extension opcode table, %" PRIu8 " items:\n"),
   10924                 :             :                    tlen);
   10925         [ #  # ]:           0 :           for (unsigned int i = 0; i < tlen; i++)
   10926                 :             :             {
   10927         [ #  # ]:           0 :               if (readp + 1 > readendp)
   10928                 :           0 :                 goto invalid_data;
   10929                 :           0 :               unsigned int opcode = *readp++;
   10930                 :           0 :               fprintf (out, _("    [%" PRIx8 "]"), opcode);
   10931                 :           0 :               if (opcode < DW_MACRO_lo_user
   10932         [ #  # ]:           0 :                   || opcode > DW_MACRO_hi_user)
   10933                 :           0 :                 goto invalid_data;
   10934                 :             :               // Record the start of description for this vendor opcode.
   10935                 :             :               // uleb128 nr args, 1 byte per arg form.
   10936                 :           0 :               vendor[opcode - DW_MACRO_lo_user] = readp;
   10937         [ #  # ]:           0 :               if (readp + 1 > readendp)
   10938                 :           0 :                 goto invalid_data;
   10939                 :           0 :               unsigned int args = *readp++;
   10940         [ #  # ]:           0 :               if (args > 0)
   10941                 :             :                 {
   10942                 :           0 :                   fprintf (out, _(" %" PRIu8 " arguments:"), args);
   10943                 :           0 :                   while (args > 0)
   10944                 :             :                     {
   10945         [ #  # ]:           0 :                       if (readp + 1 > readendp)
   10946                 :           0 :                         goto invalid_data;
   10947                 :           0 :                       unsigned int form = *readp++;
   10948                 :           0 :                       fprintf (out, " %s", dwarf_form_name (form));
   10949         [ #  # ]:           0 :                       if (! libdw_valid_user_form (form))
   10950                 :           0 :                         goto invalid_data;
   10951                 :           0 :                       args--;
   10952         [ #  # ]:           0 :                       if (args > 0)
   10953                 :           0 :                         fputc (',', out);
   10954                 :             :                     }
   10955                 :             :                 }
   10956                 :             :               else
   10957                 :           0 :                 fprintf (out, _(" no arguments."));
   10958                 :           0 :               fputc ('\n', out);
   10959                 :             :             }
   10960                 :             :         }
   10961                 :          20 :       fputc ('\n', out);
   10962                 :             : 
   10963                 :          20 :       int level = 1;
   10964         [ -  + ]:          20 :       if (readp + 1 > readendp)
   10965                 :           0 :         goto invalid_data;
   10966                 :          20 :       unsigned int opcode = *readp++;
   10967         [ +  + ]:        3046 :       while (opcode != 0)
   10968                 :             :         {
   10969                 :        3026 :           unsigned int u128;
   10970                 :        3026 :           unsigned int u128_2;
   10971                 :        3026 :           const unsigned char *endp;
   10972                 :        3026 :           uint64_t off;
   10973                 :             : 
   10974   [ +  +  +  -  :        3026 :           switch (opcode)
          +  +  +  -  -  
             -  +  +  - ]
   10975                 :             :             {
   10976                 :          20 :             case DW_MACRO_start_file:
   10977                 :          20 :               get_uleb128 (u128, readp, readendp);
   10978         [ -  + ]:          20 :               if (readp >= readendp)
   10979                 :           0 :                 goto invalid_data;
   10980                 :          20 :               get_uleb128 (u128_2, readp, readendp);
   10981                 :             : 
   10982                 :             :               /* Find the CU DIE that matches this line offset.  */
   10983                 :          20 :               const char *fname = "???";
   10984         [ -  + ]:          20 :               if (cu != NULL)
   10985                 :             :                 {
   10986         [ +  + ]:          20 :                   if (cu->files == NULL
   10987         [ -  + ]:          12 :                       && dwarf_getsrcfiles (&cu->die, &cu->files,
   10988                 :             :                                             NULL) != 0)
   10989                 :           0 :                     cu->files = (Dwarf_Files *) -1l;
   10990                 :             : 
   10991         [ -  + ]:          20 :                   if (cu->files != (Dwarf_Files *) -1l)
   10992                 :          20 :                     fname = (dwarf_filesrc (cu->files, u128_2,
   10993         [ -  + ]:          20 :                                             NULL, NULL) ?: "???");
   10994                 :             :                 }
   10995                 :          20 :               fprintf (out, "%*sstart_file %u, [%u] %s\n",
   10996                 :             :                        level, "", u128, u128_2, fname);
   10997                 :          20 :               ++level;
   10998                 :          20 :               break;
   10999                 :             : 
   11000                 :          20 :             case DW_MACRO_end_file:
   11001                 :          20 :               --level;
   11002                 :          20 :               fprintf (out, "%*send_file\n", level, "");
   11003                 :          20 :               break;
   11004                 :             : 
   11005                 :           2 :             case DW_MACRO_define:
   11006                 :           2 :               get_uleb128 (u128, readp, readendp);
   11007                 :           2 :               endp = memchr (readp, '\0', readendp - readp);
   11008         [ -  + ]:           2 :               if (endp == NULL)
   11009                 :           0 :                 goto invalid_data;
   11010                 :           2 :               fprintf (out, "%*s#define %s, line %u\n",
   11011                 :             :                        level, "", readp, u128);
   11012                 :           2 :               readp = endp + 1;
   11013                 :           2 :               break;
   11014                 :             : 
   11015                 :           0 :             case DW_MACRO_undef:
   11016                 :           0 :               get_uleb128 (u128, readp, readendp);
   11017                 :           0 :               endp = memchr (readp, '\0', readendp - readp);
   11018         [ #  # ]:           0 :               if (endp == NULL)
   11019                 :           0 :                 goto invalid_data;
   11020                 :           0 :               fprintf (out, "%*s#undef %s, line %u\n",
   11021                 :             :                        level, "", readp, u128);
   11022                 :           0 :               readp = endp + 1;
   11023                 :           0 :               break;
   11024                 :             : 
   11025                 :        1414 :             case DW_MACRO_define_strp:
   11026                 :        1414 :               get_uleb128 (u128, readp, readendp);
   11027         [ -  + ]:        1414 :               if (readp + offset_len > readendp)
   11028                 :           0 :                 goto invalid_data;
   11029         [ -  + ]:        1414 :               if (offset_len == 8)
   11030         [ #  # ]:           0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   11031                 :             :               else
   11032         [ -  + ]:        1414 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   11033                 :        1414 :               fprintf (out, "%*s#define %s, line %u (indirect)\n",
   11034                 :             :                        level, "", dwarf_getstring (dbg, off, NULL), u128);
   11035                 :        1414 :               break;
   11036                 :             : 
   11037                 :           2 :             case DW_MACRO_undef_strp:
   11038                 :           2 :               get_uleb128 (u128, readp, readendp);
   11039         [ -  + ]:           2 :               if (readp + offset_len > readendp)
   11040                 :           0 :                 goto invalid_data;
   11041         [ -  + ]:           2 :               if (offset_len == 8)
   11042         [ #  # ]:           0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   11043                 :             :               else
   11044         [ -  + ]:           2 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   11045                 :           2 :               fprintf (out, "%*s#undef %s, line %u (indirect)\n",
   11046                 :             :                        level, "", dwarf_getstring (dbg, off, NULL), u128);
   11047                 :           2 :               break;
   11048                 :             : 
   11049                 :          12 :             case DW_MACRO_import:
   11050         [ -  + ]:          12 :               if (readp + offset_len > readendp)
   11051                 :           0 :                 goto invalid_data;
   11052         [ -  + ]:          12 :               if (offset_len == 8)
   11053         [ #  # ]:           0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   11054                 :             :               else
   11055         [ -  + ]:          12 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   11056                 :          12 :               fprintf (out, "%*s#include offset 0x%" PRIx64 "\n",
   11057                 :             :                        level, "", off);
   11058                 :          12 :               break;
   11059                 :             : 
   11060                 :           0 :             case DW_MACRO_define_sup:
   11061                 :           0 :               get_uleb128 (u128, readp, readendp);
   11062                 :           0 :               fprintf (out, "%*s#define ", level, "");
   11063                 :           0 :               readp =  print_form_data (dbg, DW_FORM_strp_sup,
   11064                 :             :                                         readp, readendp, offset_len,
   11065                 :             :                                         str_offsets_base, out);
   11066                 :           0 :               fprintf (out, ", line %u (sup)\n", u128);
   11067                 :           0 :               break;
   11068                 :             : 
   11069                 :           0 :             case DW_MACRO_undef_sup:
   11070                 :           0 :               get_uleb128 (u128, readp, readendp);
   11071                 :           0 :               fprintf (out, "%*s#undef ", level, "");
   11072                 :           0 :               readp =  print_form_data (dbg, DW_FORM_strp_sup,
   11073                 :             :                                         readp, readendp, offset_len,
   11074                 :             :                                         str_offsets_base, out);
   11075                 :           0 :               fprintf (out, ", line %u (sup)\n", u128);
   11076                 :           0 :               break;
   11077                 :             : 
   11078                 :           0 :             case DW_MACRO_import_sup:
   11079         [ #  # ]:           0 :               if (readp + offset_len > readendp)
   11080                 :           0 :                 goto invalid_data;
   11081         [ #  # ]:           0 :               if (offset_len == 8)
   11082         [ #  # ]:           0 :                 off = read_8ubyte_unaligned_inc (dbg, readp);
   11083                 :             :               else
   11084         [ #  # ]:           0 :                 off = read_4ubyte_unaligned_inc (dbg, readp);
   11085                 :             :               // XXX Needs support for reading from supplementary object file.
   11086                 :           0 :               fprintf (out, "%*s#include offset 0x%" PRIx64 " (sup)\n",
   11087                 :             :                        level, "", off);
   11088                 :           0 :               break;
   11089                 :             : 
   11090                 :        1552 :             case DW_MACRO_define_strx:
   11091                 :        1552 :               get_uleb128 (u128, readp, readendp);
   11092                 :        1552 :               fprintf (out, "%*s#define ", level, "");
   11093                 :        1552 :               readp =  print_form_data (dbg, DW_FORM_strx,
   11094                 :             :                                         readp, readendp, offset_len,
   11095                 :             :                                         str_offsets_base, out);
   11096                 :        1552 :               fprintf (out, ", line %u (strx)\n", u128);
   11097                 :        1552 :               break;
   11098                 :             : 
   11099                 :           4 :             case DW_MACRO_undef_strx:
   11100                 :           4 :               get_uleb128 (u128, readp, readendp);
   11101                 :           4 :               fprintf (out, "%*s#undef ", level, "");
   11102                 :           4 :               readp =  print_form_data (dbg, DW_FORM_strx,
   11103                 :             :                                         readp, readendp, offset_len,
   11104                 :             :                                         str_offsets_base, out);
   11105                 :           4 :               fprintf (out, ", line %u (strx)\n", u128);
   11106                 :           4 :               break;
   11107                 :             : 
   11108                 :             :             default:
   11109                 :           0 :               fprintf (out, "%*svendor opcode 0x%" PRIx8, level, "", opcode);
   11110         [ #  # ]:           0 :               if (opcode < DW_MACRO_lo_user
   11111                 :             :                   || opcode > DW_MACRO_lo_user
   11112         [ #  # ]:           0 :                   || vendor[opcode - DW_MACRO_lo_user] == NULL)
   11113                 :           0 :                 goto invalid_data;
   11114                 :             : 
   11115                 :           0 :               const unsigned char *op_desc;
   11116                 :           0 :               op_desc = vendor[opcode - DW_MACRO_lo_user];
   11117                 :             : 
   11118                 :             :               // Just skip the arguments, we cannot really interpret them,
   11119                 :             :               // but print as much as we can.
   11120                 :           0 :               unsigned int args = *op_desc++;
   11121   [ #  #  #  # ]:           0 :               while (args > 0 && readp < readendp)
   11122                 :             :                 {
   11123                 :           0 :                   unsigned int form = *op_desc++;
   11124                 :           0 :                   readp = print_form_data (dbg, form, readp, readendp,
   11125                 :             :                                            offset_len, str_offsets_base, out);
   11126                 :           0 :                   args--;
   11127         [ #  # ]:           0 :                   if (args > 0)
   11128                 :           0 :                     fprintf (out, ", ");
   11129                 :             :                 }
   11130                 :           0 :               fputc ('\n', out);
   11131                 :             :             }
   11132                 :             : 
   11133         [ -  + ]:        3026 :           if (readp + 1 > readendp)
   11134                 :           0 :             goto invalid_data;
   11135                 :        3026 :           opcode = *readp++;
   11136         [ +  + ]:        3026 :           if (opcode == 0)
   11137                 :          20 :             fputc ('\n', out);
   11138                 :             :         }
   11139                 :             :     }
   11140                 :             : }
   11141                 :             : 
   11142                 :             : 
   11143                 :             : typedef struct {
   11144                 :             :   int n;
   11145                 :             :   FILE *out;
   11146                 :             : } pubnames_arg;
   11147                 :             : 
   11148                 :             : /* Callback for printing global names.  */
   11149                 :             : static int
   11150                 :          68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
   11151                 :             :                 void *arg)
   11152                 :             : {
   11153                 :          68 :   pubnames_arg *p = (pubnames_arg *) arg;
   11154                 :             : 
   11155                 :         136 :   fprintf (p->out, _(" [%5d] DIE offset: %6" PRId64
   11156                 :             :                     ", CU DIE offset: %6" PRId64 ", name: %s\n"),
   11157                 :          68 :            (p->n)++, global->die_offset, global->cu_offset, global->name);
   11158                 :             : 
   11159                 :          68 :   return 0;
   11160                 :             : }
   11161                 :             : 
   11162                 :             : 
   11163                 :             : /* Print the known exported symbols in the DWARF section '.debug_pubnames'.  */
   11164                 :             : static void
   11165                 :          16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   11166                 :             :                               Ebl *ebl,
   11167                 :             :                               GElf_Ehdr *ehdr __attribute__ ((unused)),
   11168                 :             :                               Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
   11169                 :             :                               FILE *out)
   11170                 :             : {
   11171                 :             :   /* Check section actually exists.  */
   11172         [ -  + ]:          16 :   if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
   11173                 :           0 :       return;
   11174                 :             : 
   11175                 :          16 :   fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   11176                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
   11177                 :          16 :            (uint64_t) shdr->sh_offset);
   11178                 :             : 
   11179                 :          16 :   pubnames_arg arg = { 0, out };
   11180                 :          16 :   (void) dwarf_getpubnames (dbg, print_pubnames, &arg, 0);
   11181                 :             : }
   11182                 :             : 
   11183                 :             : /* Print the content of the DWARF string section '.debug_str'
   11184                 :             :    or 'debug_line_str'.  */
   11185                 :             : static void
   11186                 :         134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   11187                 :             :                          Ebl *ebl,
   11188                 :             :                          GElf_Ehdr *ehdr __attribute__ ((unused)),
   11189                 :             :                          Elf_Scn *scn, GElf_Shdr *shdr,
   11190                 :             :                          Dwarf *dbg __attribute__ ((unused)),
   11191                 :             :                          FILE *out)
   11192                 :             : {
   11193                 :         134 :   const char *name = section_name (ebl, shdr);
   11194         [ +  + ]:         134 :   int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
   11195                 :             :              ? IDX_debug_line_str : IDX_debug_str);
   11196                 :         134 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
   11197         [ +  - ]:         134 :   if (data == NULL)
   11198                 :             :     return;
   11199                 :             : 
   11200                 :         134 :   const size_t sh_size = data->d_size;
   11201                 :             : 
   11202                 :             :   /* Compute floor(log16(shdr->sh_size)).  */
   11203                 :         134 :   GElf_Addr tmp = sh_size;
   11204                 :         134 :   int digits = 1;
   11205         [ +  + ]:         466 :   while (tmp >= 16)
   11206                 :             :     {
   11207                 :         332 :       ++digits;
   11208                 :         332 :       tmp >>= 4;
   11209                 :             :     }
   11210                 :         134 :   digits = MAX (4, digits);
   11211                 :             : 
   11212                 :         134 :   fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
   11213                 :             :                   " %*s  String\n"),
   11214                 :             :            elf_ndxscn (scn),
   11215                 :         134 :            section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
   11216                 :             :            /* TRANS: the debugstr| prefix makes the string unique.  */
   11217         [ +  - ]:         134 :            digits + 2, sgettext ("debugstr|Offset"));
   11218                 :             : 
   11219                 :         134 :   Dwarf_Off offset = 0;
   11220         [ +  + ]:      162300 :   while (offset < sh_size)
   11221                 :             :     {
   11222                 :      162166 :       size_t len;
   11223                 :      162166 :       const char *str = (const char *) data->d_buf + offset;
   11224                 :      162166 :       const char *endp = memchr (str, '\0', sh_size - offset);
   11225         [ -  + ]:      162166 :       if (unlikely (endp == NULL))
   11226                 :             :         {
   11227                 :           0 :           fprintf (out, _(" *** error, missing string terminator\n"));
   11228                 :           0 :           break;
   11229                 :             :         }
   11230                 :             : 
   11231                 :      162166 :       fprintf (out, " [%*" PRIx64 "]  \"%s\"\n",
   11232                 :             :                digits, (uint64_t) offset, str);
   11233                 :      162166 :       len = endp - str;
   11234                 :      162166 :       offset += len + 1;
   11235                 :             :     }
   11236                 :             : }
   11237                 :             : 
   11238                 :             : static void
   11239                 :          12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   11240                 :             :                                  Ebl *ebl,
   11241                 :             :                                  GElf_Ehdr *ehdr __attribute__ ((unused)),
   11242                 :             :                                  Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
   11243                 :             :                                  FILE *out)
   11244                 :             : {
   11245                 :          12 :   Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
   11246         [ +  - ]:          12 :   if (data == NULL)
   11247                 :             :     return;
   11248                 :             : 
   11249                 :          12 :   fprintf (out, _("\
   11250                 :             : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
   11251                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
   11252                 :          12 :            (uint64_t) shdr->sh_offset);
   11253                 :             : 
   11254         [ +  - ]:          12 :   if (shdr->sh_size == 0)
   11255                 :             :     return;
   11256                 :             : 
   11257                 :          12 :   size_t idx = 0;
   11258                 :          12 :   sort_listptr (&known_stroffbases, "str_offsets");
   11259                 :             : 
   11260                 :          12 :   const unsigned char *start = (const unsigned char *) data->d_buf;
   11261                 :          12 :   const unsigned char *readp = start;
   11262                 :          12 :   const unsigned char *readendp = ((const unsigned char *) data->d_buf
   11263                 :          12 :                                    + data->d_size);
   11264                 :             : 
   11265         [ +  + ]:          28 :   while (readp < readendp)
   11266                 :             :     {
   11267                 :             :       /* Most string offset tables will have a header.  For split
   11268                 :             :          dwarf unit GNU DebugFission didn't add one.  But they were
   11269                 :             :          also only defined for split units (main or skeleton units
   11270                 :             :          didn't have indirect strings).  So if we don't have a
   11271                 :             :          DW_AT_str_offsets_base at all and this is offset zero, then
   11272                 :             :          just start printing offsets immediately, if this is a .dwo
   11273                 :             :          section.  */
   11274                 :          16 :       Dwarf_Off off = (Dwarf_Off) (readp
   11275                 :          16 :                                    - (const unsigned char *) data->d_buf);
   11276                 :             : 
   11277                 :          16 :       fprintf (out, "Table at offset %" PRIx64 " ", off);
   11278                 :             : 
   11279         [ +  + ]:          16 :       struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
   11280                 :          16 :       const unsigned char *next_unitp = readendp;
   11281                 :           8 :       uint8_t offset_size;
   11282                 :           8 :       bool has_header;
   11283         [ -  + ]:           8 :       if (listptr == NULL)
   11284                 :             :         {
   11285                 :             :           /* This can happen for .dwo files.  There is only an header
   11286                 :             :              in the case this is a version 5 split DWARF file.  */
   11287                 :           8 :           Dwarf_CU *cu;
   11288                 :           8 :           uint8_t unit_type;
   11289         [ -  + ]:           8 :           if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
   11290                 :             :                                NULL, NULL) != 0)
   11291                 :             :             {
   11292                 :           0 :               error (0, 0, "Warning: Cannot find any DWARF unit.");
   11293                 :             :               /* Just guess some values.  */
   11294                 :           0 :               has_header = false;
   11295                 :           0 :               offset_size = 4;
   11296                 :             :             }
   11297         [ +  - ]:           8 :           else if (off == 0
   11298                 :           8 :                    && (unit_type == DW_UT_split_type
   11299         [ +  - ]:           8 :                        || unit_type == DW_UT_split_compile))
   11300                 :             :             {
   11301                 :           8 :               has_header = cu->version > 4;
   11302                 :           8 :               offset_size = cu->offset_size;
   11303                 :             :             }
   11304                 :             :           else
   11305                 :             :             {
   11306                 :           0 :               error (0, 0,
   11307                 :             :                      "Warning: No CU references .debug_str_offsets after %"
   11308                 :             :                      PRIx64, off);
   11309                 :           0 :               has_header = cu->version > 4;
   11310                 :           0 :               offset_size = cu->offset_size;
   11311                 :             :             }
   11312                 :           8 :           fprintf (out, "\n");
   11313                 :             :         }
   11314                 :             :       else
   11315                 :             :         {
   11316                 :             :           /* This must be DWARF5, since GNU DebugFission didn't define
   11317                 :             :              DW_AT_str_offsets_base.  */
   11318                 :           8 :           has_header = true;
   11319                 :             : 
   11320                 :           8 :           Dwarf_Die cudie;
   11321         [ -  + ]:           8 :           if (dwarf_cu_die (listptr->cu, &cudie,
   11322                 :             :                             NULL, NULL, NULL, NULL,
   11323                 :             :                             NULL, NULL) == NULL)
   11324                 :           0 :             fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
   11325                 :             :           else
   11326                 :           8 :             fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
   11327                 :             :         }
   11328                 :             : 
   11329         [ +  + ]:          16 :       if (has_header)
   11330                 :             :         {
   11331                 :          12 :           uint64_t unit_length;
   11332                 :          12 :           uint16_t version;
   11333                 :          12 :           uint16_t padding;
   11334                 :             : 
   11335         [ -  + ]:          12 :           unit_length = read_4ubyte_unaligned_inc (dbg, readp);
   11336         [ -  + ]:          12 :           if (unlikely (unit_length == 0xffffffff))
   11337                 :             :             {
   11338         [ #  # ]:           0 :               if (unlikely (readp > readendp - 8))
   11339                 :             :                 {
   11340                 :           0 :                 invalid_data:
   11341                 :           0 :                   error (0, 0, "Invalid data");
   11342                 :           0 :                   return;
   11343                 :             :                 }
   11344         [ #  # ]:           0 :               unit_length = read_8ubyte_unaligned_inc (dbg, readp);
   11345                 :           0 :               offset_size = 8;
   11346                 :             :             }
   11347                 :             :           else
   11348                 :             :             offset_size = 4;
   11349                 :             : 
   11350                 :          12 :           fprintf (out, "\n");
   11351                 :          12 :           fprintf (out, _(" Length:        %8" PRIu64 "\n"),
   11352                 :             :                    unit_length);
   11353                 :          12 :           fprintf (out, _(" Offset size:   %8" PRIu8 "\n"),
   11354                 :             :                    offset_size);
   11355                 :             : 
   11356                 :             :           /* We need at least 2-bytes (version) + 2-bytes (padding) =
   11357                 :             :              4 bytes to complete the header.  And this unit cannot go
   11358                 :             :              beyond the section data.  */
   11359         [ +  - ]:          12 :           if (readp > readendp - 4
   11360         [ +  - ]:          12 :               || unit_length < 4
   11361         [ -  + ]:          12 :               || unit_length > (uint64_t) (readendp - readp))
   11362                 :           0 :             goto invalid_data;
   11363                 :             : 
   11364                 :          12 :           next_unitp = readp + unit_length;
   11365                 :             : 
   11366         [ -  + ]:          12 :           version = read_2ubyte_unaligned_inc (dbg, readp);
   11367                 :          12 :           fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
   11368                 :             : 
   11369         [ -  + ]:          12 :           if (version != 5)
   11370                 :             :             {
   11371                 :           0 :               error (0, 0, _("Unknown version"));
   11372                 :           0 :               goto next_unit;
   11373                 :             :             }
   11374                 :             : 
   11375         [ -  + ]:          12 :           padding = read_2ubyte_unaligned_inc (dbg, readp);
   11376                 :          12 :           fprintf (out, _(" Padding:       %8" PRIx16 "\n"), padding);
   11377                 :             : 
   11378         [ +  + ]:          12 :           if (listptr != NULL
   11379         [ -  + ]:           8 :               && listptr->offset != (Dwarf_Off) (readp - start))
   11380                 :             :             {
   11381                 :           0 :               error (0, 0, "String offsets index doesn't start after header");
   11382                 :           0 :               goto next_unit;
   11383                 :             :             }
   11384                 :             : 
   11385                 :          12 :           fprintf (out, "\n");
   11386                 :             :         }
   11387                 :             : 
   11388                 :          16 :       int digits = 1;
   11389                 :          16 :       size_t offsets = (next_unitp - readp) / offset_size;
   11390         [ +  + ]:          24 :       while (offsets >= 10)
   11391                 :             :         {
   11392                 :           8 :           ++digits;
   11393                 :           8 :           offsets /= 10;
   11394                 :             :         }
   11395                 :             : 
   11396                 :          16 :       unsigned int uidx = 0;
   11397                 :          16 :       size_t index_offset =  readp - (const unsigned char *) data->d_buf;
   11398                 :          16 :       fprintf (out, " Offsets start at 0x%zx:\n", index_offset);
   11399         [ +  + ]:         204 :       while (readp <= next_unitp - offset_size)
   11400                 :             :         {
   11401                 :         188 :           Dwarf_Word offset;
   11402         [ +  - ]:         188 :           if (offset_size == 4)
   11403         [ -  + ]:         188 :             offset = read_4ubyte_unaligned_inc (dbg, readp);
   11404                 :             :           else
   11405         [ #  # ]:           0 :             offset = read_8ubyte_unaligned_inc (dbg, readp);
   11406                 :         188 :           const char *str = dwarf_getstring (dbg, offset, NULL);
   11407         [ -  + ]:         188 :           fprintf (out, " [%*u] [%*" PRIx64 "]  \"%s\"\n",
   11408                 :         188 :                    digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
   11409                 :             :         }
   11410                 :          16 :       fprintf (out, "\n");
   11411                 :             : 
   11412         [ +  - ]:          16 :       if (readp != next_unitp)
   11413                 :           0 :         error (0, 0, "extra %zd bytes at end of unit",
   11414                 :           0 :                (size_t) (next_unitp - readp));
   11415                 :             : 
   11416                 :          16 :     next_unit:
   11417                 :             :       readp = next_unitp;
   11418                 :             :     }
   11419                 :             : }
   11420                 :             : 
   11421                 :             : 
   11422                 :             : /* Print the content of the call frame search table section
   11423                 :             :    '.eh_frame_hdr'.  */
   11424                 :             : static void
   11425                 :         140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
   11426                 :             :                                Ebl *ebl __attribute__ ((unused)),
   11427                 :             :                                GElf_Ehdr *ehdr __attribute__ ((unused)),
   11428                 :             :                                Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
   11429                 :             :                                FILE *out)
   11430                 :             : {
   11431                 :         140 :   fprintf (out, _("\
   11432                 :             : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
   11433                 :             :            elf_ndxscn (scn));
   11434                 :             : 
   11435                 :         140 :   Elf_Data *data = elf_rawdata (scn, NULL);
   11436                 :             : 
   11437         [ -  + ]:         140 :   if (unlikely (data == NULL))
   11438                 :             :     {
   11439                 :           0 :       error (0, 0, _("cannot get %s content: %s"),
   11440                 :             :              ".eh_frame_hdr", elf_errmsg (-1));
   11441                 :           0 :       return;
   11442                 :             :     }
   11443                 :             : 
   11444                 :         140 :   const unsigned char *readp = data->d_buf;
   11445                 :         140 :   const unsigned char *const dataend = ((unsigned char *) data->d_buf
   11446                 :         140 :                                         + data->d_size);
   11447                 :             : 
   11448         [ -  + ]:         140 :   if (unlikely (readp + 4 > dataend))
   11449                 :             :     {
   11450                 :           0 :     invalid_data:
   11451                 :           0 :       error (0, 0, _("invalid data"));
   11452                 :           0 :       return;
   11453                 :             :     }
   11454                 :             : 
   11455                 :         140 :   unsigned int version = *readp++;
   11456                 :         140 :   unsigned int eh_frame_ptr_enc = *readp++;
   11457                 :         140 :   unsigned int fde_count_enc = *readp++;
   11458                 :         140 :   unsigned int table_enc = *readp++;
   11459                 :             : 
   11460                 :         140 :   fprintf (out, " version:          %u\n"
   11461                 :             :            " eh_frame_ptr_enc: %#x ",
   11462                 :             :            version, eh_frame_ptr_enc);
   11463                 :         140 :   print_encoding_base ("", eh_frame_ptr_enc, out);
   11464                 :         140 :   fprintf (out, " fde_count_enc:    %#x ", fde_count_enc);
   11465                 :         140 :   print_encoding_base ("", fde_count_enc, out);
   11466                 :         140 :   fprintf (out, " table_enc:        %#x ", table_enc);
   11467                 :         140 :   print_encoding_base ("", table_enc, out);
   11468                 :             : 
   11469                 :         140 :   uint64_t eh_frame_ptr = 0;
   11470         [ +  - ]:         140 :   if (eh_frame_ptr_enc != DW_EH_PE_omit)
   11471                 :             :     {
   11472                 :         140 :       readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
   11473                 :             :                             dbg);
   11474         [ -  + ]:         140 :       if (unlikely (readp == NULL))
   11475                 :           0 :         goto invalid_data;
   11476                 :             : 
   11477                 :         140 :       fprintf (out, " eh_frame_ptr:     %#" PRIx64, eh_frame_ptr);
   11478         [ +  - ]:         140 :       if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
   11479                 :         140 :         fprintf (out, " (offset: %#" PRIx64 ")",
   11480                 :             :                  /* +4 because of the 4 byte header of the section.  */
   11481                 :         140 :                  (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
   11482                 :             : 
   11483                 :         140 :       fputc ('\n', out);
   11484                 :             :     }
   11485                 :             : 
   11486                 :         140 :   uint64_t fde_count = 0;
   11487         [ +  - ]:         140 :   if (fde_count_enc != DW_EH_PE_omit)
   11488                 :             :     {
   11489                 :         140 :       readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
   11490         [ -  + ]:         140 :       if (unlikely (readp == NULL))
   11491                 :           0 :         goto invalid_data;
   11492                 :             : 
   11493                 :         140 :       fprintf (out, " fde_count:        %" PRIu64 "\n", fde_count);
   11494                 :             :     }
   11495                 :             : 
   11496   [ +  -  +  - ]:         140 :   if (fde_count == 0 || table_enc == DW_EH_PE_omit)
   11497                 :             :     return;
   11498                 :             : 
   11499                 :         140 :   fputs (" Table:\n", out);
   11500                 :             : 
   11501                 :             :   /* Optimize for the most common case.  */
   11502         [ +  - ]:         140 :   if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
   11503         [ +  + ]:       24202 :     while (fde_count > 0 && readp + 8 <= dataend)
   11504                 :             :       {
   11505         [ -  + ]:       24062 :         int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
   11506                 :       24062 :         uint64_t initial_offset = ((uint64_t) shdr->sh_offset
   11507                 :       24062 :                                    + (int64_t) initial_location);
   11508         [ -  + ]:       24062 :         int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
   11509                 :             :         // XXX Possibly print symbol name or section offset for initial_offset
   11510                 :       24062 :         fprintf (out, "  %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
   11511                 :             :                  " fde=[%6" PRIx64 "]\n",
   11512                 :             :                  initial_location, initial_offset,
   11513                 :       24062 :                  address, address - (eh_frame_ptr + 4));
   11514                 :             :       }
   11515                 :             :   else
   11516                 :         140 :     while (0 && readp < dataend)
   11517                 :             :       {
   11518                 :             : 
   11519                 :         140 :       }
   11520                 :             : }
   11521                 :             : 
   11522                 :             : 
   11523                 :             : /* Print the content of the exception handling table section
   11524                 :             :    '.eh_frame_hdr'.  */
   11525                 :             : static void
   11526                 :           0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
   11527                 :             :                              Ebl *ebl __attribute__ ((unused)),
   11528                 :             :                              GElf_Ehdr *ehdr __attribute__ ((unused)),
   11529                 :             :                              Elf_Scn *scn,
   11530                 :             :                              GElf_Shdr *shdr __attribute__ ((unused)),
   11531                 :             :                              Dwarf *dbg __attribute__ ((unused)),
   11532                 :             :                              FILE *out)
   11533                 :             : {
   11534                 :           0 :   fprintf (out, _("\
   11535                 :             :  \nException handling table section [%2zu] '.gcc_except_table':\n"),
   11536                 :             :            elf_ndxscn (scn));
   11537                 :             : 
   11538                 :           0 :   Elf_Data *data = elf_rawdata (scn, NULL);
   11539                 :             : 
   11540         [ #  # ]:           0 :   if (unlikely (data == NULL))
   11541                 :             :     {
   11542                 :           0 :       error (0, 0, _("cannot get %s content: %s"),
   11543                 :             :              ".gcc_except_table", elf_errmsg (-1));
   11544                 :           0 :       return;
   11545                 :             :     }
   11546                 :             : 
   11547                 :           0 :   const unsigned char *readp = data->d_buf;
   11548                 :           0 :   const unsigned char *const dataend = readp + data->d_size;
   11549                 :             : 
   11550         [ #  # ]:           0 :   if (unlikely (readp + 1 > dataend))
   11551                 :             :     {
   11552                 :           0 :     invalid_data:
   11553                 :           0 :       error (0, 0, _("invalid data"));
   11554                 :           0 :       return;
   11555                 :             :     }
   11556                 :           0 :   unsigned int lpstart_encoding = *readp++;
   11557                 :           0 :   fprintf (out, _(" LPStart encoding:    %#x "), lpstart_encoding);
   11558                 :           0 :   print_encoding_base ("", lpstart_encoding, out);
   11559         [ #  # ]:           0 :   if (lpstart_encoding != DW_EH_PE_omit)
   11560                 :             :     {
   11561                 :           0 :       uint64_t lpstart;
   11562                 :           0 :       readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
   11563                 :           0 :       fprintf (out, " LPStart:             %#" PRIx64 "\n", lpstart);
   11564                 :             :     }
   11565                 :             : 
   11566         [ #  # ]:           0 :   if (unlikely (readp + 1 > dataend))
   11567                 :           0 :     goto invalid_data;
   11568                 :           0 :   unsigned int ttype_encoding = *readp++;
   11569                 :           0 :   fprintf (out, _(" TType encoding:      %#x "), ttype_encoding);
   11570                 :           0 :   print_encoding_base ("", ttype_encoding, out);
   11571                 :           0 :   const unsigned char *ttype_base = NULL;
   11572         [ #  # ]:           0 :   if (ttype_encoding != DW_EH_PE_omit)
   11573                 :             :     {
   11574                 :           0 :       unsigned int ttype_base_offset;
   11575         [ #  # ]:           0 :       if (readp >= dataend)
   11576                 :           0 :         goto invalid_data;
   11577                 :           0 :       get_uleb128 (ttype_base_offset, readp, dataend);
   11578                 :           0 :       fprintf (out, " TType base offset:   %#x\n", ttype_base_offset);
   11579         [ #  # ]:           0 :       if ((size_t) (dataend - readp) > ttype_base_offset)
   11580                 :           0 :         ttype_base = readp + ttype_base_offset;
   11581                 :             :     }
   11582                 :             : 
   11583         [ #  # ]:           0 :   if (unlikely (readp + 1 > dataend))
   11584                 :           0 :     goto invalid_data;
   11585                 :           0 :   unsigned int call_site_encoding = *readp++;
   11586                 :           0 :   fprintf (out, _(" Call site encoding:  %#x "), call_site_encoding);
   11587                 :           0 :   print_encoding_base ("", call_site_encoding, out);
   11588                 :           0 :   unsigned int call_site_table_len;
   11589         [ #  # ]:           0 :   if (readp >= dataend)
   11590                 :           0 :     goto invalid_data;
   11591                 :           0 :   get_uleb128 (call_site_table_len, readp, dataend);
   11592                 :             : 
   11593                 :           0 :   const unsigned char *const action_table = readp + call_site_table_len;
   11594         [ #  # ]:           0 :   if (unlikely (action_table > dataend))
   11595                 :           0 :     goto invalid_data;
   11596                 :             :   unsigned int u = 0;
   11597                 :             :   unsigned int max_action = 0;
   11598         [ #  # ]:           0 :   while (readp < action_table)
   11599                 :             :     {
   11600         [ #  # ]:           0 :       if (u == 0)
   11601                 :           0 :         fputs (_("\n Call site table:"), out);
   11602                 :             : 
   11603                 :           0 :       uint64_t call_site_start;
   11604                 :           0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   11605                 :             :                             &call_site_start, dbg);
   11606                 :           0 :       uint64_t call_site_length;
   11607                 :           0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   11608                 :             :                             &call_site_length, dbg);
   11609                 :           0 :       uint64_t landing_pad;
   11610                 :           0 :       readp = read_encoded (call_site_encoding, readp, dataend,
   11611                 :             :                             &landing_pad, dbg);
   11612                 :           0 :       unsigned int action;
   11613         [ #  # ]:           0 :       if (readp >= dataend)
   11614                 :           0 :         goto invalid_data;
   11615                 :           0 :       get_uleb128 (action, readp, dataend);
   11616                 :           0 :       max_action = MAX (action, max_action);
   11617                 :           0 :       fprintf (out, _(" [%4u] Call site start:   %#" PRIx64 "\n"
   11618                 :             :                         "        Call site length:  %" PRIu64 "\n"
   11619                 :             :                         "        Landing pad:       %#" PRIx64 "\n"
   11620                 :             :                         "        Action:            %u\n"),
   11621                 :             :                u++, call_site_start, call_site_length, landing_pad, action);
   11622                 :             :     }
   11623         [ #  # ]:           0 :   if (readp != action_table)
   11624                 :           0 :     goto invalid_data;
   11625                 :             : 
   11626                 :           0 :   unsigned int max_ar_filter = 0;
   11627         [ #  # ]:           0 :   if (max_action > 0)
   11628                 :             :     {
   11629                 :           0 :       fputs ("\n Action table:", out);
   11630                 :             : 
   11631                 :           0 :       size_t maxdata = (size_t) (dataend - action_table);
   11632   [ #  #  #  # ]:           0 :       if (max_action > maxdata || maxdata - max_action < 1)
   11633                 :             :         {
   11634                 :           0 :         invalid_action_table:
   11635                 :           0 :           fputs (_("   <INVALID DATA>\n"), stdout);
   11636                 :           0 :           return;
   11637                 :             :         }
   11638                 :             : 
   11639                 :           0 :       const unsigned char *const action_table_end
   11640                 :           0 :         = action_table + max_action + 1;
   11641                 :             : 
   11642                 :           0 :       u = 0;
   11643                 :           0 :       do
   11644                 :             :         {
   11645                 :           0 :           int ar_filter;
   11646                 :           0 :           get_sleb128 (ar_filter, readp, action_table_end);
   11647         [ #  # ]:           0 :           if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
   11648                 :           0 :             max_ar_filter = ar_filter;
   11649                 :           0 :           int ar_disp;
   11650         [ #  # ]:           0 :           if (readp >= action_table_end)
   11651                 :           0 :             goto invalid_action_table;
   11652                 :           0 :           get_sleb128 (ar_disp, readp, action_table_end);
   11653                 :             : 
   11654                 :           0 :           fprintf (out, " [%4u] ar_filter:  % d\n"
   11655                 :             :                    "        ar_disp:    % -5d",
   11656                 :             :                    u, ar_filter, ar_disp);
   11657         [ #  # ]:           0 :           if (abs (ar_disp) & 1)
   11658                 :           0 :             fprintf (out, " -> [%4u]\n", u + (ar_disp + 1) / 2);
   11659         [ #  # ]:           0 :           else if (ar_disp != 0)
   11660                 :           0 :             fputs (" -> ???", out);
   11661                 :             :           else
   11662                 :           0 :             fputc ('\n', out);
   11663                 :           0 :           ++u;
   11664                 :             :         }
   11665         [ #  # ]:           0 :       while (readp < action_table_end);
   11666                 :             :     }
   11667                 :             : 
   11668         [ #  # ]:           0 :   if (max_ar_filter > 0 && ttype_base != NULL)
   11669                 :             :     {
   11670                 :           0 :       unsigned char dsize;
   11671                 :           0 :       fputs ("\n TType table:", out);
   11672                 :             : 
   11673                 :             :       // XXX Not *4, size of encoding;
   11674         [ #  # ]:           0 :       switch (ttype_encoding & 7)
   11675                 :             :         {
   11676                 :             :         case DW_EH_PE_udata2:
   11677                 :             :         case DW_EH_PE_sdata2:
   11678                 :             :           dsize = 2;
   11679                 :             :           break;
   11680                 :             :         case DW_EH_PE_udata4:
   11681                 :             :         case DW_EH_PE_sdata4:
   11682                 :             :           dsize = 4;
   11683                 :             :           break;
   11684                 :             :         case DW_EH_PE_udata8:
   11685                 :             :         case DW_EH_PE_sdata8:
   11686                 :             :           dsize = 8;
   11687                 :             :           break;
   11688                 :           0 :         default:
   11689                 :           0 :           dsize = 0;
   11690                 :           0 :           error (1, 0, _("invalid TType encoding"));
   11691                 :             :         }
   11692                 :             : 
   11693                 :           0 :       if (max_ar_filter
   11694         [ #  # ]:           0 :           > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
   11695                 :           0 :         goto invalid_data;
   11696                 :             : 
   11697                 :           0 :       readp = ttype_base - max_ar_filter * dsize;
   11698                 :           0 :       do
   11699                 :             :         {
   11700                 :           0 :           uint64_t ttype;
   11701                 :           0 :           readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
   11702                 :             :                                 dbg);
   11703                 :           0 :           fprintf (out, " [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
   11704                 :             :         }
   11705         [ #  # ]:           0 :       while (readp < ttype_base);
   11706                 :             :     }
   11707                 :             : }
   11708                 :             : 
   11709                 :             : /* Print the content of the '.gdb_index' section.
   11710                 :             :    http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
   11711                 :             : */
   11712                 :             : static void
   11713                 :           8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
   11714                 :             :                          GElf_Ehdr *ehdr __attribute__ ((unused)),
   11715                 :             :                          Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
   11716                 :             :                          FILE *out)
   11717                 :             : {
   11718                 :           8 :   fprintf (out, _("\nGDB section [%2zu] '%s' at offset %#" PRIx64
   11719                 :             :                     " contains %" PRId64 " bytes :\n"),
   11720                 :             :            elf_ndxscn (scn), section_name (ebl, shdr),
   11721                 :           8 :            (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
   11722                 :             : 
   11723                 :           8 :   Elf_Data *data = elf_rawdata (scn, NULL);
   11724                 :             : 
   11725         [ -  + ]:           8 :   if (unlikely (data == NULL))
   11726                 :             :     {
   11727                 :           0 :       error (0, 0, _("cannot get %s content: %s"),
   11728                 :             :              ".gdb_index", elf_errmsg (-1));
   11729                 :           4 :       return;
   11730                 :             :     }
   11731                 :             : 
   11732                 :             :   // .gdb_index is always in little endian.
   11733                 :           8 :   Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
   11734                 :           8 :   dbg = &dummy_dbg;
   11735                 :             : 
   11736                 :           8 :   const unsigned char *readp = data->d_buf;
   11737                 :           8 :   const unsigned char *const dataend = readp + data->d_size;
   11738                 :             : 
   11739         [ -  + ]:           8 :   if (unlikely (readp + 4 > dataend))
   11740                 :             :     {
   11741                 :           0 :     invalid_data:
   11742                 :           0 :       error (0, 0, _("invalid data"));
   11743                 :           0 :       return;
   11744                 :             :     }
   11745                 :             : 
   11746                 :           8 :   int32_t vers = read_4ubyte_unaligned (dbg, readp);
   11747                 :           8 :   fprintf (out, _(" Version:         %" PRId32 "\n"), vers);
   11748                 :             : 
   11749                 :             :   // The only difference between version 4 and version 5 is the
   11750                 :             :   // hash used for generating the table.  Version 6 contains symbols
   11751                 :             :   // for inlined functions, older versions didn't.  Version 7 adds
   11752                 :             :   // symbol kinds.  Version 8 just indicates that it correctly includes
   11753                 :             :   // TUs for symbols.  Version 9 adds shortcut table for information
   11754                 :             :   // regarding the main function.
   11755         [ -  + ]:           8 :   if (vers < 4 || vers > 9)
   11756                 :             :     {
   11757                 :           0 :       fprintf (out, _("  unknown version, cannot parse section\n"));
   11758                 :           0 :       return;
   11759                 :             :     }
   11760                 :             : 
   11761                 :           8 :   readp += 4;
   11762         [ -  + ]:           8 :   if (unlikely (readp + 4 > dataend))
   11763                 :           0 :     goto invalid_data;
   11764                 :             : 
   11765                 :           8 :   uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
   11766                 :           8 :   fprintf (out, _(" CU offset:       %#" PRIx32 "\n"), cu_off);
   11767                 :             : 
   11768                 :           8 :   readp += 4;
   11769         [ -  + ]:           8 :   if (unlikely (readp + 4 > dataend))
   11770                 :           0 :     goto invalid_data;
   11771                 :             : 
   11772                 :           8 :   uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
   11773                 :           8 :   fprintf (out, _(" TU offset:       %#" PRIx32 "\n"), tu_off);
   11774                 :             : 
   11775                 :           8 :   readp += 4;
   11776         [ -  + ]:           8 :   if (unlikely (readp + 4 > dataend))
   11777                 :           0 :     goto invalid_data;
   11778                 :             : 
   11779                 :           8 :   uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
   11780                 :           8 :   fprintf (out, _(" address offset:  %#" PRIx32 "\n"), addr_off);
   11781                 :             : 
   11782                 :           8 :   readp += 4;
   11783         [ -  + ]:           8 :   if (unlikely (readp + 4 > dataend))
   11784                 :           0 :     goto invalid_data;
   11785                 :             : 
   11786                 :           8 :   uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
   11787                 :           8 :   fprintf (out, _(" symbol offset:   %#" PRIx32 "\n"), sym_off);
   11788                 :             : 
   11789                 :           8 :   readp += 4;
   11790         [ -  + ]:           8 :   if (unlikely (readp + 4 > dataend))
   11791                 :           0 :     goto invalid_data;
   11792                 :             : 
   11793                 :           8 :   uint32_t shortcut_off = 0;
   11794         [ +  + ]:           8 :   if (vers >= 9)
   11795                 :             :     {
   11796                 :           4 :       shortcut_off = read_4ubyte_unaligned (dbg, readp);
   11797                 :           4 :       fprintf (out, _(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
   11798                 :             : 
   11799                 :           4 :       readp += 4;
   11800         [ -  + ]:           4 :       if (unlikely (readp + 4 > dataend))
   11801                 :           0 :         goto invalid_data;
   11802                 :             :     }
   11803                 :             : 
   11804                 :           8 :   uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
   11805                 :           8 :   fprintf (out, _(" constant offset: %#" PRIx32 "\n"), const_off);
   11806                 :             : 
   11807         [ -  + ]:           8 :   if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
   11808                 :             :                 < const_off))
   11809                 :           0 :     goto invalid_data;
   11810                 :             : 
   11811                 :           8 :   readp = data->d_buf + cu_off;
   11812                 :             : 
   11813                 :           8 :   const unsigned char *nextp = data->d_buf + tu_off;
   11814         [ -  + ]:           8 :   if (tu_off >= data->d_size)
   11815                 :           0 :     goto invalid_data;
   11816                 :             : 
   11817                 :           8 :   size_t cu_nr = (nextp - readp) / 16;
   11818                 :             : 
   11819                 :           8 :   fprintf (out, _("\n CU list at offset %#" PRIx32
   11820                 :             :                     " contains %zu entries:\n"),
   11821                 :             :            cu_off, cu_nr);
   11822                 :             : 
   11823                 :           8 :   size_t n = 0;
   11824   [ +  -  +  + ]:          22 :   while (dataend - readp >= 16 && n < cu_nr)
   11825                 :             :     {
   11826                 :          14 :       uint64_t off = read_8ubyte_unaligned (dbg, readp);
   11827                 :          14 :       readp += 8;
   11828                 :             : 
   11829                 :          14 :       uint64_t len = read_8ubyte_unaligned (dbg, readp);
   11830                 :          14 :       readp += 8;
   11831                 :             : 
   11832                 :          14 :       fprintf (out, " [%4zu] start: %0#8" PRIx64
   11833                 :             :                ", length: %5" PRIu64 "\n", n, off, len);
   11834                 :          14 :       n++;
   11835                 :             :     }
   11836                 :             : 
   11837                 :           8 :   readp = data->d_buf + tu_off;
   11838                 :           8 :   nextp = data->d_buf + addr_off;
   11839         [ -  + ]:           8 :   if (addr_off >= data->d_size)
   11840                 :           0 :     goto invalid_data;
   11841                 :             : 
   11842                 :           8 :   size_t tu_nr = (nextp - readp) / 24;
   11843                 :             : 
   11844                 :           8 :   fprintf (out, _("\n TU list at offset %#" PRIx32
   11845                 :             :                     " contains %zu entries:\n"),
   11846                 :             :            tu_off, tu_nr);
   11847                 :             : 
   11848                 :           8 :   n = 0;
   11849   [ +  -  +  + ]:          14 :   while (dataend - readp >= 24 && n < tu_nr)
   11850                 :             :     {
   11851                 :           6 :       uint64_t off = read_8ubyte_unaligned (dbg, readp);
   11852                 :           6 :       readp += 8;
   11853                 :             : 
   11854                 :           6 :       uint64_t type = read_8ubyte_unaligned (dbg, readp);
   11855                 :           6 :       readp += 8;
   11856                 :             : 
   11857                 :           6 :       uint64_t sig = read_8ubyte_unaligned (dbg, readp);
   11858                 :           6 :       readp += 8;
   11859                 :             : 
   11860                 :           6 :       fprintf (out, " [%4zu] CU offset: %5" PRId64
   11861                 :             :                ", type offset: %5" PRId64
   11862                 :             :                ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
   11863                 :           6 :       n++;
   11864                 :             :     }
   11865                 :             : 
   11866                 :           8 :   readp = data->d_buf + addr_off;
   11867                 :           8 :   nextp = data->d_buf + sym_off;
   11868         [ -  + ]:           8 :   if (sym_off >= data->d_size)
   11869                 :           0 :     goto invalid_data;
   11870                 :             : 
   11871                 :           8 :   size_t addr_nr = (nextp - readp) / 20;
   11872                 :             : 
   11873                 :           8 :   fprintf (out, _("\n Address list at offset %#" PRIx32
   11874                 :             :                     " contains %zu entries:\n"),
   11875                 :             :            addr_off, addr_nr);
   11876                 :             : 
   11877                 :           8 :   n = 0;
   11878   [ +  -  +  + ]:          22 :   while (dataend - readp >= 20 && n < addr_nr)
   11879                 :             :     {
   11880                 :          14 :       uint64_t low = read_8ubyte_unaligned (dbg, readp);
   11881                 :          14 :       readp += 8;
   11882                 :             : 
   11883                 :          14 :       uint64_t high = read_8ubyte_unaligned (dbg, readp);
   11884                 :          14 :       readp += 8;
   11885                 :             : 
   11886                 :          14 :       uint32_t idx = read_4ubyte_unaligned (dbg, readp);
   11887                 :          14 :       readp += 4;
   11888                 :             : 
   11889                 :          14 :       fprintf (out, " [%4zu] ", n);
   11890                 :          14 :       print_dwarf_addr (dwflmod, 8, low, low, out);
   11891                 :          14 :       fprintf (out, "..");
   11892                 :          14 :       print_dwarf_addr (dwflmod, 8, high - 1, high, out);
   11893                 :          14 :       fprintf (out, ", CU index: %5" PRId32 "\n", idx);
   11894                 :          14 :       n++;
   11895                 :             :     }
   11896                 :             : 
   11897                 :           8 :   const unsigned char *const_start = data->d_buf + const_off;
   11898         [ -  + ]:           8 :   if (const_off > data->d_size)
   11899                 :           0 :     goto invalid_data;
   11900                 :             : 
   11901                 :           8 :   const unsigned char *shortcut_start = NULL;
   11902         [ +  + ]:           8 :   if (vers >= 9)
   11903                 :             :     {
   11904         [ -  + ]:           4 :       if (shortcut_off >= data->d_size)
   11905                 :           0 :         goto invalid_data;
   11906                 :             : 
   11907                 :           4 :       shortcut_start = data->d_buf + shortcut_off;
   11908                 :           4 :       nextp = shortcut_start;
   11909                 :             :     }
   11910                 :             :   else
   11911                 :             :     nextp = const_start;
   11912                 :             : 
   11913                 :           8 :   readp = data->d_buf + sym_off;
   11914                 :           8 :   size_t sym_nr = (nextp - readp) / 8;
   11915                 :             : 
   11916                 :           8 :   fprintf (out, _("\n Symbol table at offset %#" PRIx32
   11917                 :             :                     " contains %zu slots:\n"),
   11918                 :             :            sym_off, sym_nr);
   11919                 :             : 
   11920                 :           8 :   n = 0;
   11921   [ +  -  +  + ]:        8200 :   while (dataend - readp >= 8 && n < sym_nr)
   11922                 :             :     {
   11923                 :        8192 :       uint32_t name = read_4ubyte_unaligned (dbg, readp);
   11924                 :        8192 :       readp += 4;
   11925                 :             : 
   11926                 :        8192 :       uint32_t vector = read_4ubyte_unaligned (dbg, readp);
   11927                 :        8192 :       readp += 4;
   11928                 :             : 
   11929         [ +  + ]:        8192 :       if (name != 0 || vector != 0)
   11930                 :             :         {
   11931                 :          60 :           const unsigned char *sym = const_start + name;
   11932   [ +  -  -  + ]:          60 :           if (unlikely ((size_t) (dataend - const_start) < name
   11933                 :             :                         || memchr (sym, '\0', dataend - sym) == NULL))
   11934                 :           0 :             goto invalid_data;
   11935                 :             : 
   11936                 :          60 :           fprintf (out, " [%4zu] symbol: %s, CUs: ", n, sym);
   11937                 :             : 
   11938                 :          60 :           const unsigned char *readcus = const_start + vector;
   11939         [ -  + ]:          60 :           if (unlikely ((size_t) (dataend - const_start) < vector))
   11940                 :           0 :             goto invalid_data;
   11941                 :          60 :           uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
   11942         [ +  + ]:         128 :           while (cus--)
   11943                 :             :             {
   11944                 :          68 :               uint32_t cu_kind, cu, kind;
   11945                 :          68 :               bool is_static;
   11946                 :          68 :               readcus += 4;
   11947         [ -  + ]:          68 :               if (unlikely (readcus + 4 > dataend))
   11948                 :           0 :                 goto invalid_data;
   11949                 :          68 :               cu_kind = read_4ubyte_unaligned (dbg, readcus);
   11950                 :          68 :               cu = cu_kind & ((1 << 24) - 1);
   11951                 :          68 :               kind = (cu_kind >> 28) & 7;
   11952                 :          68 :               is_static = cu_kind & (1U << 31);
   11953         [ +  + ]:          68 :               if (cu > cu_nr - 1)
   11954                 :          10 :                 fprintf (out, "%" PRId32 "T", cu - (uint32_t) cu_nr);
   11955                 :             :               else
   11956                 :          58 :                 fprintf (out, "%" PRId32, cu);
   11957         [ +  + ]:          68 :               if (kind != 0)
   11958                 :             :                 {
   11959                 :          52 :                   fprintf (out, " (");
   11960   [ +  +  +  -  :          52 :                   switch (kind)
                      - ]
   11961                 :             :                     {
   11962                 :             :                     case 1:
   11963                 :          20 :                       fprintf (out, "type");
   11964                 :          20 :                       break;
   11965                 :             :                     case 2:
   11966                 :          16 :                       fprintf (out, "var");
   11967                 :          16 :                       break;
   11968                 :             :                     case 3:
   11969                 :          16 :                       fprintf (out, "func");
   11970                 :          16 :                       break;
   11971                 :             :                     case 4:
   11972                 :           0 :                       fprintf (out, "other");
   11973                 :           0 :                       break;
   11974                 :             :                     default:
   11975                 :           0 :                       fprintf (out, "unknown-0x%" PRIx32, kind);
   11976                 :           0 :                       break;
   11977                 :             :                     }
   11978         [ +  + ]:          68 :                   fprintf (out, ":%c)", (is_static ? 'S' : 'G'));
   11979                 :             :                 }
   11980         [ +  + ]:          68 :               if (cus > 0)
   11981                 :           8 :                 fprintf (out, ", ");
   11982                 :             :             }
   11983                 :          60 :           fprintf (out, "\n");
   11984                 :             :         }
   11985                 :        8192 :       n++;
   11986                 :             :     }
   11987                 :             : 
   11988         [ +  + ]:           8 :   if (vers < 9)
   11989                 :             :     return;
   11990                 :             : 
   11991         [ -  + ]:           4 :   if (unlikely (shortcut_start == NULL))
   11992                 :           0 :     goto invalid_data;
   11993                 :             : 
   11994                 :           4 :   readp = shortcut_start;
   11995                 :           4 :   nextp = const_start;
   11996                 :           4 :   size_t shortcut_nr = (nextp - readp) / 4;
   11997                 :             : 
   11998         [ -  + ]:           4 :   if (unlikely (shortcut_nr != 2))
   11999                 :           0 :     goto invalid_data;
   12000                 :             : 
   12001                 :           8 :   fprintf (out,
   12002                 :           4 :            _("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
   12003                 :             :            shortcut_off, shortcut_nr);
   12004                 :             : 
   12005                 :           4 :   uint32_t lang = read_4ubyte_unaligned (dbg, readp);
   12006                 :           4 :   readp += 4;
   12007                 :             : 
   12008                 :             :   /* Include the hex number of LANG in the output if the language
   12009                 :             :      is unknown.  */
   12010                 :           4 :   const char *lang_str = dwarf_lang_string (lang);
   12011                 :           4 :   lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
   12012                 :             :                                 DW_LANG_hi_user, true);
   12013                 :             : 
   12014                 :           4 :   fprintf (out, _("Language of main: %s\n"), lang_str);
   12015                 :           4 :   fprintf (out, _("Name of main: "));
   12016                 :             : 
   12017         [ +  + ]:           4 :   if (lang != 0)
   12018                 :             :     {
   12019                 :           2 :       uint32_t name = read_4ubyte_unaligned (dbg, readp);
   12020                 :           2 :       readp += 4;
   12021                 :           2 :       const unsigned char *sym = const_start + name;
   12022                 :             : 
   12023   [ +  -  -  + ]:           2 :       if (unlikely ((size_t) (dataend - const_start) < name
   12024                 :             :                     || memchr (sym, '\0', dataend - sym) == NULL))
   12025                 :           0 :         goto invalid_data;
   12026                 :             : 
   12027                 :           2 :       fprintf (out, "%s\n", sym);
   12028                 :             :     }
   12029                 :             :   else
   12030                 :           2 :     fprintf (out, "<unknown>\n");
   12031                 :             : }
   12032                 :             : 
   12033                 :             : /* Returns true and sets split DWARF CU id if there is a split compile
   12034                 :             :    unit in the given Dwarf, and no non-split units are found (before it).  */
   12035                 :             : static bool
   12036                 :         396 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
   12037                 :             : {
   12038                 :         396 :   Dwarf_CU *cu = NULL;
   12039         [ +  + ]:         396 :   while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
   12040                 :             :     {
   12041                 :         394 :       uint8_t unit_type;
   12042         [ +  - ]:         394 :       if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
   12043                 :             :                          id, NULL, NULL) != 0)
   12044                 :         394 :         return false;
   12045                 :             : 
   12046         [ +  + ]:         394 :       if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
   12047                 :             :         return false;
   12048                 :             : 
   12049                 :             :       /* We really only care about the split compile unit, the types
   12050                 :             :          should be fine and self sufficient.  Also they don't have an
   12051                 :             :          id that we can match with a skeleton unit.  */
   12052         [ +  - ]:          18 :       if (unit_type == DW_UT_split_compile)
   12053                 :             :         {
   12054                 :          18 :           *split_cu = cu;
   12055                 :          18 :           return true;
   12056                 :             :         }
   12057                 :             :     }
   12058                 :             : 
   12059                 :             :   return false;
   12060                 :             : }
   12061                 :             : 
   12062                 :             : /* Check that there is one and only one Dwfl_Module, return in arg.  */
   12063                 :             : static int
   12064                 :          18 : getone_dwflmod (Dwfl_Module *dwflmod,
   12065                 :             :                void **userdata __attribute__ ((unused)),
   12066                 :             :                const char *name __attribute__ ((unused)),
   12067                 :             :                Dwarf_Addr base __attribute__ ((unused)),
   12068                 :             :                void *arg)
   12069                 :             : {
   12070                 :          18 :   Dwfl_Module **m = (Dwfl_Module **) arg;
   12071         [ +  - ]:          18 :   if (*m != NULL)
   12072                 :             :     return DWARF_CB_ABORT;
   12073                 :          18 :   *m = dwflmod;
   12074                 :          18 :   return DWARF_CB_OK;
   12075                 :             : }
   12076                 :             : 
   12077                 :             : typedef struct {
   12078                 :             :   Dwfl_Module *dwflmod;
   12079                 :             :   Ebl *ebl;
   12080                 :             :   GElf_Ehdr *ehdr;
   12081                 :             :   Elf_Scn scn;
   12082                 :             :   GElf_Shdr shdr;
   12083                 :             :   Dwarf *dbg;
   12084                 :             :   FILE *out;
   12085                 :             :   void (*fp) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
   12086                 :             :               Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
   12087                 :             : } job_data;
   12088                 :             : 
   12089                 :             : #ifdef USE_LOCKS
   12090                 :             : 
   12091                 :             : /* Thread entry point.  */
   12092                 :             : static void *
   12093                 :             : do_job (void *data, FILE *out)
   12094                 :             : {
   12095                 :             :   job_data *d = (job_data *) data;
   12096                 :             :   d->fp (d->dwflmod, d->ebl, d->ehdr, &d->scn, &d->shdr, d->dbg, out);
   12097                 :             :   return NULL;
   12098                 :             : }
   12099                 :             : #endif
   12100                 :             : 
   12101                 :             : /* If readelf is built with thread safety, then set up JDATA at index IDX
   12102                 :             :    and add it to the job queue.
   12103                 :             : 
   12104                 :             :    If thread safety is not supported or the maximum number of threads is set
   12105                 :             :    to 1, then immediately call START_ROUTINE with the given arguments.  */
   12106                 :             : static void
   12107                 :        1094 : schedule_job (job_data jdata[], size_t idx,
   12108                 :             :               void (*start_routine) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
   12109                 :             :                                      Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *),
   12110                 :             :               Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
   12111                 :             :               GElf_Shdr *shdr, Dwarf *dbg)
   12112                 :             : {
   12113                 :             : #ifdef USE_LOCKS
   12114                 :             :   if (max_threads > 1)
   12115                 :             :     {
   12116                 :             :       /* Add to the job queue.  */
   12117                 :             :       jdata[idx].dwflmod = dwflmod;
   12118                 :             :       jdata[idx].ebl = ebl;
   12119                 :             :       jdata[idx].ehdr = ehdr;
   12120                 :             :       jdata[idx].scn = *scn;
   12121                 :             :       jdata[idx].shdr = *shdr;
   12122                 :             :       jdata[idx].dbg = dbg;
   12123                 :             :       jdata[idx].fp = start_routine;
   12124                 :             : 
   12125                 :             :       add_job (do_job, (void *) &jdata[idx]);
   12126                 :             :     }
   12127                 :             :   else
   12128                 :             :     start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
   12129                 :             : #else
   12130                 :        1094 :   (void) jdata; (void) idx;
   12131                 :             : 
   12132                 :        1094 :   start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
   12133                 :             : #endif
   12134                 :        1094 : }
   12135                 :             : 
   12136                 :             : static void
   12137                 :         502 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
   12138                 :             : {
   12139                 :             :   /* Used for skeleton file, if necessary for split DWARF.  */
   12140                 :         502 :   Dwfl *skel_dwfl = NULL;
   12141                 :         502 :   Dwfl_Module *skel_mod = NULL;
   12142                 :         502 :   char *skel_name = NULL;
   12143                 :         502 :   Dwarf *split_dbg = NULL;
   12144                 :         502 :   Dwarf_CU *split_cu = NULL;
   12145                 :             : 
   12146                 :             :   /* Before we start the real work get a debug context descriptor.  */
   12147                 :         502 :   Dwarf_Addr dwbias;
   12148                 :         502 :   Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
   12149                 :         502 :   Dwarf dummy_dbg =
   12150                 :             :     {
   12151                 :         502 :       .elf = ebl->elf,
   12152                 :         502 :       .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
   12153                 :         502 :     };
   12154         [ +  + ]:         502 :   if (dbg == NULL)
   12155                 :             :     {
   12156         [ -  + ]:         106 :       if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
   12157                 :           0 :         error (0, 0, _("cannot get debug context descriptor: %s"),
   12158                 :             :                dwfl_errmsg (-1));
   12159                 :             :       dbg = &dummy_dbg;
   12160                 :             :     }
   12161                 :             :   else
   12162                 :             :     {
   12163                 :             :       /* If we are asked about a split dwarf (.dwo) file, use the user
   12164                 :             :          provided, or find the corresponding skeleton file. If we got
   12165                 :             :          a skeleton file, replace the given dwflmod and dbg, with one
   12166                 :             :          derived from the skeleton file to provide enough context.  */
   12167                 :         396 :       uint64_t split_id;
   12168         [ +  + ]:         396 :       if (is_split_dwarf (dbg, &split_id, &split_cu))
   12169                 :             :         {
   12170         [ +  - ]:          18 :           if (dwarf_skeleton != NULL)
   12171                 :          18 :             skel_name = strdup (dwarf_skeleton);
   12172                 :             :           else
   12173                 :             :             {
   12174                 :             :               /* Replace file.dwo with file.o and see if that matches. */
   12175                 :           0 :               const char *fname;
   12176                 :           0 :               dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
   12177                 :             :                                 &fname, NULL);
   12178         [ #  # ]:           0 :               if (fname != NULL)
   12179                 :             :                 {
   12180                 :           0 :                   size_t flen = strlen (fname);
   12181   [ #  #  #  # ]:           0 :                   if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
   12182                 :             :                     {
   12183                 :           0 :                       skel_name = strdup (fname);
   12184         [ #  # ]:           0 :                       if (skel_name != NULL)
   12185                 :             :                         {
   12186                 :           0 :                           skel_name[flen - 3] = 'o';
   12187                 :           0 :                           skel_name[flen - 2] = '\0';
   12188                 :             :                         }
   12189                 :             :                     }
   12190                 :             :                 }
   12191                 :             :             }
   12192                 :             : 
   12193         [ -  + ]:          18 :           if (skel_name != NULL)
   12194                 :             :             {
   12195                 :          18 :               int skel_fd = open (skel_name, O_RDONLY);
   12196         [ -  + ]:          18 :               if (skel_fd == -1)
   12197                 :           0 :                 fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
   12198                 :             :                          " '%s'\n", skel_name);
   12199                 :             :               else
   12200                 :             :                 {
   12201                 :          18 :                   skel_dwfl = create_dwfl (skel_fd, skel_name);
   12202                 :             : 
   12203                 :             :                   /* skel_fd was dup'ed by create_dwfl.  We can close the
   12204                 :             :                      original now.  */
   12205                 :          18 :                   close (skel_fd);
   12206                 :             :                 }
   12207                 :             : 
   12208         [ +  - ]:          18 :               if (skel_dwfl != NULL)
   12209                 :             :                 {
   12210         [ -  + ]:          18 :                   if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
   12211                 :             :                                        &skel_mod, 0) != 0)
   12212                 :             :                     {
   12213                 :           0 :                       fprintf (stderr, "Warning: Bad DWARF skeleton,"
   12214                 :             :                                " multiple modules '%s'\n", skel_name);
   12215                 :           0 :                       dwfl_end (skel_dwfl);
   12216                 :           0 :                       skel_mod = NULL;
   12217                 :             :                     }
   12218                 :             :                 }
   12219         [ #  # ]:           0 :               else if (skel_fd != -1)
   12220                 :           0 :                 fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
   12221                 :             :                          " '%s': %s\n", skel_name, dwfl_errmsg (-1));
   12222                 :             : 
   12223         [ +  - ]:          18 :               if (skel_mod != NULL)
   12224                 :             :                 {
   12225                 :          18 :                   Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
   12226         [ +  - ]:          18 :                   if (skel_dbg != NULL)
   12227                 :             :                     {
   12228                 :             :                       /* First check the skeleton CU DIE, only fetch
   12229                 :             :                          the split DIE if we know the id matches to
   12230                 :             :                          not unnecessary search for any split DIEs we
   12231                 :             :                          don't need. */
   12232                 :          18 :                       Dwarf_CU *cu = NULL;
   12233                 :          18 :                       while (dwarf_get_units (skel_dbg, cu, &cu,
   12234         [ +  - ]:          26 :                                               NULL, NULL, NULL, NULL) == 0)
   12235                 :             :                         {
   12236                 :          26 :                           uint8_t unit_type;
   12237                 :          26 :                           uint64_t skel_id;
   12238         [ +  - ]:          26 :                           if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
   12239                 :             :                                              &skel_id, NULL, NULL) == 0
   12240         [ +  - ]:          26 :                               && unit_type == DW_UT_skeleton
   12241         [ +  + ]:          26 :                               && split_id == skel_id)
   12242                 :             :                             {
   12243                 :          18 :                               Dwarf_Die subdie;
   12244         [ +  - ]:          18 :                               if (dwarf_cu_info (cu, NULL, NULL, NULL,
   12245                 :             :                                                  &subdie,
   12246                 :             :                                                  NULL, NULL, NULL) == 0
   12247         [ +  - ]:          18 :                                   && dwarf_tag (&subdie) != DW_TAG_invalid)
   12248                 :             :                                 {
   12249                 :          18 :                                   split_dbg = dwarf_cu_getdwarf (subdie.cu);
   12250         [ -  + ]:          18 :                                   if (split_dbg == NULL)
   12251                 :           0 :                                     fprintf (stderr,
   12252                 :             :                                              "Warning: Couldn't get split_dbg:"
   12253                 :             :                                              " %s\n", dwarf_errmsg (-1));
   12254                 :           0 :                                   break;
   12255                 :             :                                 }
   12256                 :             :                               else
   12257                 :             :                                 {
   12258                 :             :                                   /* Everything matches up, but not
   12259                 :             :                                      according to libdw. Which means
   12260                 :             :                                      the user knew better.  So...
   12261                 :             :                                      Terrible hack... We can never
   12262                 :             :                                      destroy the underlying dwfl
   12263                 :             :                                      because it would free the wrong
   12264                 :             :                                      Dwarfs... So we leak memory...*/
   12265         [ #  # ]:           0 :                                   if (cu->split == NULL
   12266         [ #  # ]:           0 :                                       && dwarf_skeleton != NULL)
   12267                 :             :                                     {
   12268                 :           0 :                                       do_not_close_dwfl = true;
   12269                 :           0 :                                       __libdw_link_skel_split (cu, split_cu);
   12270                 :           0 :                                       split_dbg = dwarf_cu_getdwarf (split_cu);
   12271                 :           0 :                                       break;
   12272                 :             :                                     }
   12273                 :             :                                   else
   12274                 :           0 :                                     fprintf (stderr, "Warning: Couldn't get"
   12275                 :             :                                              " skeleton subdie: %s\n",
   12276                 :             :                                              dwarf_errmsg (-1));
   12277                 :             :                                 }
   12278                 :             :                             }
   12279                 :             :                         }
   12280         [ -  - ]:          18 :                       if (split_dbg == NULL)
   12281                 :           0 :                         fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
   12282                 :             :                     }
   12283                 :             :                   else
   12284                 :           0 :                     fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
   12285                 :             :                              " %s\n", dwfl_errmsg (-1));
   12286                 :             :                 }
   12287                 :             :             }
   12288                 :             : 
   12289                 :           0 :           if (split_dbg != NULL)
   12290                 :             :             {
   12291                 :          18 :               dbg = split_dbg;
   12292                 :          18 :               dwflmod = skel_mod;
   12293                 :             :             }
   12294         [ #  # ]:           0 :           else if (skel_name == NULL)
   12295                 :           0 :             fprintf (stderr,
   12296                 :             :                      "Warning: split DWARF file, but no skeleton found.\n");
   12297                 :             :         }
   12298         [ +  - ]:         378 :       else if (dwarf_skeleton != NULL)
   12299                 :           0 :         fprintf (stderr, "Warning: DWARF skeleton given,"
   12300                 :             :                  " but not a split DWARF file\n");
   12301                 :             :     }
   12302                 :             : 
   12303                 :             :   /* Get the section header string table index.  */
   12304                 :         502 :   size_t shstrndx;
   12305         [ -  + ]:         502 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
   12306                 :           0 :     error_exit (0, _("cannot get section header string table index"));
   12307                 :             : 
   12308                 :         502 :   ssize_t num_jobs = 0;
   12309                 :         502 :   job_data *jdata = NULL;
   12310                 :             : 
   12311                 :             :   /* If the .debug_info section is listed as implicitly required then
   12312                 :             :      we must make sure to handle it before handling any other debug
   12313                 :             :      section.  Various other sections depend on the CU DIEs being
   12314                 :             :      scanned (silently) first.  */
   12315                 :         502 :   bool implicit_info = (implicit_debug_sections & section_info) != 0;
   12316                 :         502 :   bool explicit_info = (print_debug_sections & section_info) != 0;
   12317         [ +  + ]:         502 :   if (implicit_info)
   12318                 :             :     {
   12319                 :             :       Elf_Scn *scn = NULL;
   12320         [ +  - ]:        3132 :       while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   12321                 :             :         {
   12322                 :        3132 :           GElf_Shdr shdr_mem;
   12323                 :        3132 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   12324                 :             : 
   12325   [ +  -  +  + ]:        3132 :           if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
   12326                 :             :             {
   12327                 :        2448 :               const char *name = elf_strptr (ebl->elf, shstrndx,
   12328                 :        1224 :                                              shdr->sh_name);
   12329         [ -  + ]:        1224 :               if (name == NULL)
   12330                 :           0 :                 continue;
   12331                 :             : 
   12332         [ +  + ]:        1224 :               if (strcmp (name, ".debug_info") == 0
   12333         [ +  + ]:        1092 :                   || strcmp (name, ".debug_info.dwo") == 0
   12334         [ +  + ]:        1074 :                   || strcmp (name, ".zdebug_info") == 0
   12335         [ +  - ]:        1062 :                   || strcmp (name, ".zdebug_info.dwo") == 0
   12336         [ +  + ]:        1062 :                   || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
   12337                 :             :                 {
   12338                 :         164 :                   print_debug_info_section (dwflmod, ebl, ehdr,
   12339                 :             :                                             scn, shdr, dbg, stdout);
   12340                 :         164 :                   break;
   12341                 :             :                 }
   12342                 :             :             }
   12343                 :             :         }
   12344                 :             : 
   12345                 :         164 :       print_debug_sections &= ~section_info;
   12346                 :         164 :       implicit_debug_sections &= ~section_info;
   12347                 :             :     }
   12348                 :             : 
   12349                 :             :   /* Look through all the sections for the debugging sections to print.  */
   12350                 :             :   Elf_Scn *scn = NULL;
   12351         [ +  + ]:       16590 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   12352                 :             :     {
   12353                 :       16088 :       GElf_Shdr shdr_mem;
   12354                 :       16088 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   12355                 :             : 
   12356   [ +  -  +  + ]:       16088 :       if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
   12357                 :             :         {
   12358                 :        6758 :           static const struct
   12359                 :             :           {
   12360                 :             :             const char *name;
   12361                 :             :             enum section_e bitmask;
   12362                 :             :             void (*fp) (Dwfl_Module *, Ebl *,
   12363                 :             :                         GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
   12364                 :             :           } debug_sections[] =
   12365                 :             :             {
   12366                 :             : #define NEW_SECTION(name) \
   12367                 :             :               { ".debug_" #name, section_##name, print_debug_##name##_section }
   12368                 :             :               NEW_SECTION (abbrev),
   12369                 :             :               NEW_SECTION (addr),
   12370                 :             :               NEW_SECTION (aranges),
   12371                 :             :               NEW_SECTION (frame),
   12372                 :             :               NEW_SECTION (info),
   12373                 :             :               NEW_SECTION (types),
   12374                 :             :               NEW_SECTION (line),
   12375                 :             :               NEW_SECTION (loc),
   12376                 :             :               /* loclists is loc for DWARF5.  */
   12377                 :             :               { ".debug_loclists", section_loc,
   12378                 :             :                 print_debug_loclists_section },
   12379                 :             :               NEW_SECTION (pubnames),
   12380                 :             :               NEW_SECTION (str),
   12381                 :             :               /* A DWARF5 specialised debug string section.  */
   12382                 :             :               { ".debug_line_str", section_str,
   12383                 :             :                 print_debug_str_section },
   12384                 :             :               /* DWARF5 string offsets table.  */
   12385                 :             :               { ".debug_str_offsets", section_str,
   12386                 :             :                 print_debug_str_offsets_section },
   12387                 :             :               NEW_SECTION (macinfo),
   12388                 :             :               NEW_SECTION (macro),
   12389                 :             :               NEW_SECTION (ranges),
   12390                 :             :               /* rnglists is ranges for DWARF5.  */
   12391                 :             :               { ".debug_rnglists", section_ranges,
   12392                 :             :                 print_debug_rnglists_section },
   12393                 :             :               { ".eh_frame", section_frame | section_exception,
   12394                 :             :                 print_debug_frame_section },
   12395                 :             :               { ".eh_frame_hdr", section_frame | section_exception,
   12396                 :             :                 print_debug_frame_hdr_section },
   12397                 :             :               { ".gcc_except_table", section_frame | section_exception,
   12398                 :             :                 print_debug_exception_table },
   12399                 :             :               { ".gdb_index", section_gdb_index, print_gdb_index_section }
   12400                 :             :             };
   12401                 :        6758 :           const int ndebug_sections = (sizeof (debug_sections)
   12402                 :             :                                        / sizeof (debug_sections[0]));
   12403                 :       13516 :           const char *name = elf_strptr (ebl->elf, shstrndx,
   12404                 :        6758 :                                          shdr->sh_name);
   12405         [ -  + ]:        6758 :           if (name == NULL)
   12406                 :           0 :             continue;
   12407                 :             : 
   12408         [ +  + ]:        6758 :           if (jdata == NULL)
   12409                 :             :             {
   12410                 :         502 :               jdata = calloc (ndebug_sections, sizeof (*jdata));
   12411         [ -  + ]:         502 :               if (jdata == NULL)
   12412                 :           0 :                 error_exit (0, _("failed to allocate job data"));
   12413                 :             :             }
   12414                 :             : 
   12415                 :        6758 :           int n;
   12416         [ +  + ]:      106506 :           for (n = 0; n < ndebug_sections; ++n)
   12417                 :             :             {
   12418                 :      103042 :               size_t dbglen = strlen (debug_sections[n].name);
   12419                 :      103042 :               size_t scnlen = strlen (name);
   12420         [ +  + ]:      103042 :               if ((strncmp (name, debug_sections[n].name, dbglen) == 0
   12421         [ +  + ]:        3558 :                    && (dbglen == scnlen
   12422         [ +  + ]:         546 :                        || (scnlen == dbglen + 4
   12423         [ +  + ]:         466 :                            && strstr (name, ".dwo") == name + dbglen)))
   12424   [ +  +  +  + ]:       99922 :                   || (name[0] == '.' && name[1] == 'z'
   12425         [ +  - ]:        1132 :                       && debug_sections[n].name[1] == 'd'
   12426         [ +  + ]:        1132 :                       && strncmp (&name[2], &debug_sections[n].name[1],
   12427                 :             :                                   dbglen - 1) == 0
   12428         [ -  + ]:         164 :                       && (scnlen == dbglen + 1
   12429         [ #  # ]:           0 :                           || (scnlen == dbglen + 5
   12430         [ #  # ]:           0 :                               && strstr (name, ".dwo") == name + dbglen + 1)))
   12431         [ +  + ]:       99758 :                   || (scnlen > 14 /* .gnu.debuglto_ prefix. */
   12432         [ +  + ]:       10170 :                       && startswith (name, ".gnu.debuglto_")
   12433         [ +  + ]:          72 :                       && strcmp (&name[14], debug_sections[n].name) == 0)
   12434                 :             : )
   12435                 :             :                 {
   12436                 :        3294 :                   if (((print_debug_sections | implicit_debug_sections)
   12437         [ +  + ]:        3294 :                        & debug_sections[n].bitmask))
   12438                 :        1094 :                     schedule_job (jdata, num_jobs++, debug_sections[n].fp,
   12439                 :             :                                   dwflmod, ebl, ehdr, scn, shdr, dbg);
   12440                 :             : 
   12441         [ -  + ]:        3294 :                   assert (num_jobs <= ndebug_sections);
   12442                 :             :                   break;
   12443                 :             :                 }
   12444                 :             :             }
   12445                 :             :         }
   12446                 :             :     }
   12447                 :             : 
   12448                 :             : #ifdef USE_LOCKS
   12449                 :             :   /* If max_threads <= 1, then jobs were immediately run in schedule_job.  */
   12450                 :             :   if (max_threads > 1)
   12451                 :             :     run_jobs (max_threads);
   12452                 :             : #endif
   12453                 :             : 
   12454                 :         502 :   dwfl_end (skel_dwfl);
   12455                 :         502 :   free (skel_name);
   12456                 :         502 :   free (jdata);
   12457                 :             : 
   12458                 :             :   /* Turn implicit and/or explicit back on in case we go over another file.  */
   12459         [ +  + ]:         502 :   if (implicit_info)
   12460                 :         164 :     implicit_debug_sections |= section_info;
   12461         [ +  + ]:         502 :   if (explicit_info)
   12462                 :         134 :     print_debug_sections |= section_info;
   12463                 :             : 
   12464                 :         502 :   reset_listptr (&known_locsptr);
   12465                 :         502 :   reset_listptr (&known_loclistsptr);
   12466                 :         502 :   reset_listptr (&known_rangelistptr);
   12467                 :         502 :   reset_listptr (&known_rnglistptr);
   12468                 :         502 :   reset_listptr (&known_addrbases);
   12469                 :         502 :   reset_listptr (&known_stroffbases);
   12470                 :         502 : }
   12471                 :             : 
   12472                 :             : 
   12473                 :             : #define ITEM_INDENT             4
   12474                 :             : #define WRAP_COLUMN             75
   12475                 :             : 
   12476                 :             : /* Print "NAME: FORMAT", wrapping when output text would make the line
   12477                 :             :    exceed WRAP_COLUMN.  Unpadded numbers look better for the core items
   12478                 :             :    but this function is also used for registers which should be printed
   12479                 :             :    aligned.  Fortunately registers output uses fixed fields width (such
   12480                 :             :    as %11d) for the alignment.
   12481                 :             : 
   12482                 :             :    Line breaks should not depend on the particular values although that
   12483                 :             :    may happen in some cases of the core items.  */
   12484                 :             : 
   12485                 :             : static unsigned int
   12486                 :             : __attribute__ ((format (printf, 6, 7)))
   12487                 :        1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
   12488                 :             :                  size_t name_width, const char *name, const char *format, ...)
   12489                 :             : {
   12490                 :        1642 :   size_t len = strlen (name);
   12491                 :        1642 :   if (name_width < len)
   12492                 :             :     name_width = len;
   12493                 :             : 
   12494                 :        1642 :   char *out;
   12495                 :        1642 :   va_list ap;
   12496                 :        1642 :   va_start (ap, format);
   12497                 :        1642 :   int out_len = vasprintf (&out, format, ap);
   12498                 :        1642 :   va_end (ap);
   12499         [ -  + ]:        1642 :   if (out_len == -1)
   12500                 :           0 :     error_exit (0, _("memory exhausted"));
   12501                 :             : 
   12502                 :        1642 :   size_t n = name_width + sizeof ": " - 1 + out_len;
   12503                 :             : 
   12504         [ +  + ]:        1642 :   if (colno == 0)
   12505                 :             :     {
   12506                 :         102 :       printf ("%*s", ITEM_INDENT, "");
   12507                 :         102 :       colno = ITEM_INDENT + n;
   12508                 :             :     }
   12509         [ +  + ]:        1540 :   else if (colno + 2 + n < wrap)
   12510                 :             :     {
   12511                 :         936 :       printf ("%c ", sep);
   12512                 :         936 :       colno += 2 + n;
   12513                 :             :     }
   12514                 :             :   else
   12515                 :             :     {
   12516                 :         604 :       printf ("\n%*s", ITEM_INDENT, "");
   12517                 :         604 :       colno = ITEM_INDENT + n;
   12518                 :             :     }
   12519                 :             : 
   12520                 :        1642 :   printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
   12521                 :             : 
   12522                 :        1642 :   free (out);
   12523                 :             : 
   12524                 :        1642 :   return colno;
   12525                 :             : }
   12526                 :             : 
   12527                 :             : static const void *
   12528                 :        1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
   12529                 :             :          void *value, const void *data, size_t size)
   12530                 :             : {
   12531                 :        3724 :   Elf_Data valuedata =
   12532                 :             :     {
   12533                 :             :       .d_type = type,
   12534                 :             :       .d_buf = value,
   12535         [ +  + ]:        1862 :       .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
   12536                 :             :       .d_version = EV_CURRENT,
   12537                 :             :     };
   12538                 :        1862 :   Elf_Data indata =
   12539                 :             :     {
   12540                 :             :       .d_type = type,
   12541                 :             :       .d_buf = (void *) data,
   12542                 :             :       .d_size = valuedata.d_size,
   12543                 :             :       .d_version = EV_CURRENT,
   12544                 :             :     };
   12545                 :             : 
   12546                 :        1862 :   Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
   12547         [ +  + ]:        1862 :                  ? elf32_xlatetom : elf64_xlatetom)
   12548                 :        1862 :     (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
   12549         [ -  + ]:        1862 :   if (d == NULL)
   12550                 :           0 :     error_exit (0, _("cannot convert core note data: %s"),
   12551                 :             :                 elf_errmsg (-1));
   12552                 :             : 
   12553                 :        1862 :   return data + indata.d_size;
   12554                 :             : }
   12555                 :             : 
   12556                 :             : typedef uint8_t GElf_Byte;
   12557                 :             : 
   12558                 :             : static unsigned int
   12559                 :         800 : handle_core_item (Elf *core, const GElf_Ehdr *ehdr,
   12560                 :             :                   const Ebl_Core_Item *item, const void *desc,
   12561                 :             :                   unsigned int colno, size_t *repeated_size)
   12562                 :             : {
   12563         [ +  + ]:         800 :   uint_fast16_t count = item->count ?: 1;
   12564                 :             :   /* Ebl_Core_Item count is always a small number.
   12565                 :             :      Make sure the backend didn't put in some large bogus value.  */
   12566         [ -  + ]:         124 :   assert (count < 128);
   12567                 :             : 
   12568                 :             : #define TYPES                                                                 \
   12569                 :             :   DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8);                         \
   12570                 :             :   DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16);                       \
   12571                 :             :   DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32);                       \
   12572                 :             :   DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32);                         \
   12573                 :             :   DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64);                            \
   12574                 :             :   DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
   12575                 :             : 
   12576                 :             : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
   12577                 :         800 :   typedef union { TYPES; } value_t;
   12578                 :         800 :   void *data = alloca (count * sizeof (value_t));
   12579                 :             : #undef DO_TYPE
   12580                 :             : 
   12581                 :             : #define DO_TYPE(NAME, Name, hex, dec) \
   12582                 :             :     GElf_##Name *value_##Name __attribute__((unused)) = data
   12583                 :         800 :   TYPES;
   12584                 :             : #undef DO_TYPE
   12585                 :             : 
   12586                 :         800 :   size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
   12587                 :         800 :   size_t convsize = size;
   12588         [ +  + ]:         800 :   if (repeated_size != NULL)
   12589                 :             :     {
   12590   [ +  -  -  + ]:           2 :       if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
   12591                 :             :         {
   12592                 :           0 :           data = alloca (*repeated_size);
   12593                 :           0 :           count *= *repeated_size / size;
   12594                 :           0 :           convsize = count * size;
   12595                 :           0 :           *repeated_size -= convsize;
   12596                 :             :         }
   12597   [ +  -  +  - ]:           2 :       else if (item->count != 0 || item->format != '\n')
   12598                 :           0 :         *repeated_size -= size;
   12599                 :             :     }
   12600                 :             : 
   12601                 :         800 :   convert (core, item->type, count, data, desc + item->offset, convsize);
   12602                 :             : 
   12603                 :         800 :   Elf_Type type = item->type;
   12604         [ +  + ]:         800 :   if (type == ELF_T_ADDR)
   12605         [ +  - ]:           2 :     type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
   12606                 :             : 
   12607   [ +  +  +  +  :         800 :   switch (item->format)
             +  +  +  +  
                      - ]
   12608                 :             :     {
   12609                 :         386 :     case 'd':
   12610         [ -  + ]:         386 :       assert (count == 1);
   12611   [ +  +  +  +  :         386 :       switch (type)
                -  +  - ]
   12612                 :             :         {
   12613                 :             : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   12614                 :             :           case ELF_T_##NAME:                                                  \
   12615                 :             :             colno = print_core_item (colno, ',', WRAP_COLUMN,                 \
   12616                 :             :                                      0, item->name, dec, value_##Name[0]); \
   12617                 :             :             break
   12618                 :         386 :           TYPES;
   12619                 :             : #undef DO_TYPE
   12620                 :           0 :         default:
   12621                 :           0 :           abort ();
   12622                 :             :         }
   12623                 :             :       break;
   12624                 :             : 
   12625                 :         222 :     case 'x':
   12626         [ -  + ]:         222 :       assert (count == 1);
   12627   [ -  -  +  -  :         222 :       switch (type)
                +  -  - ]
   12628                 :             :         {
   12629                 :             : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   12630                 :             :           case ELF_T_##NAME:                                                  \
   12631                 :             :             colno = print_core_item (colno, ',', WRAP_COLUMN,                 \
   12632                 :             :                                      0, item->name, hex, value_##Name[0]);      \
   12633                 :             :             break
   12634                 :         222 :           TYPES;
   12635                 :             : #undef DO_TYPE
   12636                 :           0 :         default:
   12637                 :           0 :           abort ();
   12638                 :             :         }
   12639                 :             :       break;
   12640                 :             : 
   12641                 :          44 :     case 'b':
   12642                 :             :     case 'B':
   12643         [ -  + ]:          44 :       assert (size % sizeof (unsigned int) == 0);
   12644                 :          44 :       unsigned int nbits = count * size * 8;
   12645                 :          44 :       unsigned int pop = 0;
   12646         [ +  + ]:         112 :       for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
   12647                 :          68 :         pop += __builtin_popcount (*i);
   12648                 :          44 :       bool negate = pop > nbits / 2;
   12649                 :          44 :       const unsigned int bias = item->format == 'b';
   12650                 :             : 
   12651                 :          44 :       {
   12652         [ -  + ]:          44 :         char printed[(negate ? nbits - pop : pop) * 16 + 1];
   12653                 :          44 :         char *p = printed;
   12654                 :          44 :         *p = '\0';
   12655                 :             : 
   12656                 :          44 :         if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
   12657                 :             :           {
   12658                 :             :             assert (size == sizeof (unsigned int) * 2);
   12659                 :             :             for (unsigned int *i = data;
   12660                 :             :                  (void *) i < data + count * size; i += 2)
   12661                 :             :               {
   12662                 :             :                 unsigned int w = i[1];
   12663                 :             :                 i[1] = i[0];
   12664                 :             :                 i[0] = w;
   12665                 :             :               }
   12666                 :             :           }
   12667                 :             : 
   12668                 :          44 :         unsigned int lastbit = 0;
   12669                 :          44 :         unsigned int run = 0;
   12670                 :          44 :         for (const unsigned int *i = data;
   12671         [ +  + ]:         112 :              (void *) i < data + count * size; ++i)
   12672                 :             :           {
   12673                 :          68 :             unsigned int bit = ((void *) i - data) * 8;
   12674         [ -  + ]:          68 :             unsigned int w = negate ? ~*i : *i;
   12675         [ -  + ]:          68 :             while (w != 0)
   12676                 :             :               {
   12677                 :             :                 /* Note that a right shift equal to (or greater than)
   12678                 :             :                    the number of bits of w is undefined behaviour.  In
   12679                 :             :                    particular when the least significant bit is bit 32
   12680                 :             :                    (w = 0x8000000) then w >>= n is undefined.  So
   12681                 :             :                    explicitly handle that case separately.  */
   12682                 :           0 :                 unsigned int n = ffs (w);
   12683         [ #  # ]:           0 :                 if (n < sizeof (w) * 8)
   12684                 :           0 :                   w >>= n;
   12685                 :             :                 else
   12686                 :             :                   w = 0;
   12687                 :           0 :                 bit += n;
   12688                 :             : 
   12689   [ #  #  #  # ]:           0 :                 if (lastbit != 0 && lastbit + 1 == bit)
   12690                 :           0 :                   ++run;
   12691                 :             :                 else
   12692                 :             :                   {
   12693                 :           0 :                     if (lastbit == 0)
   12694                 :           0 :                       p += sprintf (p, "%u", bit - bias);
   12695         [ #  # ]:           0 :                     else if (run == 0)
   12696                 :           0 :                       p += sprintf (p, ",%u", bit - bias);
   12697                 :             :                     else
   12698                 :           0 :                       p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
   12699                 :             :                     run = 0;
   12700                 :             :                   }
   12701                 :             : 
   12702                 :             :                 lastbit = bit;
   12703                 :             :               }
   12704                 :             :           }
   12705   [ -  +  -  - ]:          44 :         if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
   12706                 :           0 :           p += sprintf (p, "-%u", lastbit - bias);
   12707                 :             : 
   12708         [ +  - ]:          88 :         colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   12709                 :             :                                  negate ? "~<%s>" : "<%s>", printed);
   12710                 :             :       }
   12711                 :          44 :       break;
   12712                 :             : 
   12713                 :          88 :     case 'T':
   12714                 :             :     case (char) ('T'|0x80):
   12715         [ -  + ]:          88 :       assert (count == 2);
   12716                 :          88 :       Dwarf_Word sec;
   12717                 :          88 :       Dwarf_Word usec;
   12718   [ -  -  +  -  :          88 :       switch (type)
                +  -  - ]
   12719                 :             :         {
   12720                 :             : #define DO_TYPE(NAME, Name, hex, dec)                                         \
   12721                 :             :           case ELF_T_##NAME:                                                  \
   12722                 :             :             sec = value_##Name[0];                                            \
   12723                 :             :             usec = value_##Name[1];                                           \
   12724                 :             :             break
   12725                 :          88 :           TYPES;
   12726                 :             : #undef DO_TYPE
   12727                 :           0 :         default:
   12728                 :           0 :           abort ();
   12729                 :             :         }
   12730         [ -  + ]:          88 :       if (unlikely (item->format == (char) ('T'|0x80)))
   12731                 :             :         {
   12732                 :             :           /* This is a hack for an ill-considered 64-bit ABI where
   12733                 :             :              tv_usec is actually a 32-bit field with 32 bits of padding
   12734                 :             :              rounding out struct timeval.  We've already converted it as
   12735                 :             :              a 64-bit field.  For little-endian, this just means the
   12736                 :             :              high half is the padding; it's presumably zero, but should
   12737                 :             :              be ignored anyway.  For big-endian, it means the 32-bit
   12738                 :             :              field went into the high half of USEC.  */
   12739         [ #  # ]:           0 :           if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
   12740                 :           0 :             usec >>= 32;
   12741                 :             :           else
   12742                 :           0 :             usec &= UINT32_MAX;
   12743                 :             :         }
   12744                 :          88 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   12745                 :             :                                "%" PRIu64 ".%.6" PRIu64, sec, usec);
   12746                 :          88 :       break;
   12747                 :             : 
   12748                 :          18 :     case 'c':
   12749         [ -  + ]:          18 :       assert (count == 1);
   12750                 :          18 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   12751                 :          18 :                                "%c", value_Byte[0]);
   12752                 :          18 :       break;
   12753                 :             : 
   12754                 :          36 :     case 's':
   12755                 :          36 :       colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
   12756                 :             :                                "%.*s", (int) count, value_Byte);
   12757                 :          36 :       break;
   12758                 :             : 
   12759                 :           2 :     case '\n':
   12760                 :             :       /* This is a list of strings separated by '\n'.  */
   12761         [ -  + ]:           2 :       assert (item->count == 0);
   12762         [ -  + ]:           2 :       assert (repeated_size != NULL);
   12763         [ -  + ]:           2 :       assert (item->name == NULL);
   12764         [ -  + ]:           2 :       if (unlikely (item->offset >= *repeated_size))
   12765                 :             :         break;
   12766                 :             : 
   12767                 :           2 :       const char *s = desc + item->offset;
   12768                 :           2 :       size = *repeated_size - item->offset;
   12769                 :           2 :       *repeated_size = 0;
   12770         [ +  - ]:          96 :       while (size > 0)
   12771                 :             :         {
   12772                 :          96 :           const char *eol = memchr (s, '\n', size);
   12773                 :          96 :           int len = size;
   12774         [ +  + ]:          96 :           if (eol != NULL)
   12775                 :          94 :             len = eol - s;
   12776                 :          96 :           printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
   12777         [ +  + ]:          96 :           if (eol == NULL)
   12778                 :             :             break;
   12779                 :          94 :           size -= eol + 1 - s;
   12780                 :          94 :           s = eol + 1;
   12781                 :             :         }
   12782                 :             : 
   12783                 :             :       colno = WRAP_COLUMN;
   12784                 :             :       break;
   12785                 :             : 
   12786                 :             :     case 'h':
   12787                 :             :       break;
   12788                 :             : 
   12789                 :           0 :     default:
   12790                 :           0 :       error (0, 0, "XXX not handling format '%c' for %s",
   12791                 :           0 :              item->format, item->name);
   12792                 :           0 :       break;
   12793                 :             :     }
   12794                 :             : 
   12795                 :             : #undef TYPES
   12796                 :             : 
   12797                 :         800 :   return colno;
   12798                 :             : }
   12799                 :             : 
   12800                 :             : 
   12801                 :             : /* Sort items by group, and by layout offset within each group.  */
   12802                 :             : static int
   12803                 :        1790 : compare_core_items (const void *a, const void *b)
   12804                 :             : {
   12805                 :        1790 :   const Ebl_Core_Item *const *p1 = a;
   12806                 :        1790 :   const Ebl_Core_Item *const *p2 = b;
   12807                 :        1790 :   const Ebl_Core_Item *item1 = *p1;
   12808                 :        1790 :   const Ebl_Core_Item *item2 = *p2;
   12809                 :             : 
   12810                 :        1790 :   return ((item1->group == item2->group ? 0
   12811         [ +  + ]:        1790 :            : strcmp (item1->group, item2->group))
   12812         [ -  + ]:        1790 :           ?: (int) item1->offset - (int) item2->offset);
   12813                 :             : }
   12814                 :             : 
   12815                 :             : /* Sort item groups by layout offset of the first item in the group.  */
   12816                 :             : static int
   12817                 :         278 : compare_core_item_groups (const void *a, const void *b)
   12818                 :             : {
   12819                 :         278 :   const Ebl_Core_Item *const *const *p1 = a;
   12820                 :         278 :   const Ebl_Core_Item *const *const *p2 = b;
   12821                 :         278 :   const Ebl_Core_Item *const *group1 = *p1;
   12822                 :         278 :   const Ebl_Core_Item *const *group2 = *p2;
   12823                 :         278 :   const Ebl_Core_Item *item1 = *group1;
   12824                 :         278 :   const Ebl_Core_Item *item2 = *group2;
   12825                 :             : 
   12826                 :         278 :   return (int) item1->offset - (int) item2->offset;
   12827                 :             : }
   12828                 :             : 
   12829                 :             : static unsigned int
   12830                 :          74 : handle_core_items (Elf *core, const GElf_Ehdr *ehdr,
   12831                 :             :                    const void *desc, size_t descsz,
   12832                 :             :                    const Ebl_Core_Item *items, size_t nitems)
   12833                 :          74 : {
   12834         [ +  + ]:          74 :   if (nitems == 0)
   12835                 :             :     return 0;
   12836                 :          68 :   unsigned int colno = 0;
   12837                 :             : 
   12838                 :             :   /* FORMAT '\n' makes sense to be present only as a single item as it
   12839                 :             :      processes all the data of a note.  FORMATs 'b' and 'B' have a special case
   12840                 :             :      if present as a single item but they can be also processed with other
   12841                 :             :      items below.  */
   12842   [ +  +  +  + ]:          68 :   if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
   12843         [ +  - ]:          16 :                       || items[0].format == 'B'))
   12844                 :             :     {
   12845         [ -  + ]:           2 :       assert (items[0].offset == 0);
   12846                 :           2 :       size_t size = descsz;
   12847                 :           2 :       colno = handle_core_item (core, ehdr, items, desc, colno, &size);
   12848                 :             :       /* If SIZE is not zero here there is some remaining data.  But we do not
   12849                 :             :          know how to process it anyway.  */
   12850                 :           2 :       return colno;
   12851                 :             :     }
   12852         [ +  + ]:         848 :   for (size_t i = 0; i < nitems; ++i)
   12853         [ -  + ]:         782 :     assert (items[i].format != '\n');
   12854                 :             : 
   12855                 :             :   /* Sort to collect the groups together.  */
   12856                 :          66 :   const Ebl_Core_Item *sorted_items[nitems];
   12857         [ +  + ]:         848 :   for (size_t i = 0; i < nitems; ++i)
   12858                 :         782 :     sorted_items[i] = &items[i];
   12859                 :          66 :   qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
   12860                 :             : 
   12861                 :             :   /* Collect the unique groups and sort them.  */
   12862                 :          66 :   const Ebl_Core_Item **groups[nitems];
   12863                 :          66 :   groups[0] = &sorted_items[0];
   12864                 :          66 :   size_t ngroups = 1;
   12865         [ +  + ]:         782 :   for (size_t i = 1; i < nitems; ++i)
   12866         [ +  + ]:         716 :     if (sorted_items[i]->group != sorted_items[i - 1]->group
   12867         [ +  - ]:         150 :         && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
   12868                 :         150 :       groups[ngroups++] = &sorted_items[i];
   12869                 :          66 :   qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
   12870                 :             : 
   12871                 :             :   /* Write out all the groups.  */
   12872                 :          66 :   const void *last = desc;
   12873                 :          70 :   do
   12874                 :             :     {
   12875         [ +  + ]:         290 :       for (size_t i = 0; i < ngroups; ++i)
   12876                 :             :         {
   12877                 :         220 :           for (const Ebl_Core_Item **item = groups[i];
   12878                 :        1018 :                (item < &sorted_items[nitems]
   12879   [ +  +  +  + ]:        1018 :                 && ((*item)->group == groups[i][0]->group
   12880         [ -  + ]:         150 :                     || !strcmp ((*item)->group, groups[i][0]->group)));
   12881                 :         798 :                ++item)
   12882                 :         798 :             colno = handle_core_item (core, ehdr, *item, desc, colno, NULL);
   12883                 :             : 
   12884                 :             :           /* Force a line break at the end of the group.  */
   12885                 :         220 :           colno = WRAP_COLUMN;
   12886                 :             :         }
   12887                 :             : 
   12888         [ +  + ]:          70 :       if (descsz == 0)
   12889                 :             :         break;
   12890                 :             : 
   12891                 :             :       /* This set of items consumed a certain amount of the note's data.
   12892                 :             :          If there is more data there, we have another unit of the same size.
   12893                 :             :          Loop to print that out too.  */
   12894                 :          40 :       const Ebl_Core_Item *item = &items[nitems - 1];
   12895                 :          80 :       size_t eltsz = item->offset + gelf_fsize (core, item->type,
   12896                 :          40 :                                                 item->count ?: 1, EV_CURRENT);
   12897                 :             : 
   12898                 :          40 :       int reps = -1;
   12899                 :          40 :       do
   12900                 :             :         {
   12901                 :          40 :           ++reps;
   12902                 :          40 :           desc += eltsz;
   12903                 :          40 :           descsz -= eltsz;
   12904                 :             :         }
   12905   [ +  +  -  + ]:          40 :       while (descsz >= eltsz && !memcmp (desc, last, eltsz));
   12906                 :             : 
   12907         [ +  - ]:          40 :       if (reps == 1)
   12908                 :             :         {
   12909                 :             :           /* For just one repeat, print it unabridged twice.  */
   12910                 :             :           desc -= eltsz;
   12911                 :             :           descsz += eltsz;
   12912                 :             :         }
   12913         [ +  - ]:          40 :       else if (reps > 1)
   12914                 :           0 :         printf (_("\n%*s... <repeats %u more times> ..."),
   12915                 :             :                 ITEM_INDENT, "", reps);
   12916                 :             : 
   12917                 :          40 :       last = desc;
   12918                 :             :     }
   12919         [ +  + ]:          40 :   while (descsz > 0);
   12920                 :             : 
   12921                 :             :   return colno;
   12922                 :             : }
   12923                 :             : 
   12924                 :             : static unsigned int
   12925                 :         324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
   12926                 :             :                       const Ebl_Register_Location *regloc, const void *desc,
   12927                 :             :                       unsigned int colno)
   12928                 :             : {
   12929         [ -  + ]:         324 :   if (regloc->bits % 8 != 0)
   12930                 :             :     {
   12931                 :           0 :       error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
   12932                 :             :              regloc->bits);
   12933                 :           0 :       return colno;
   12934                 :             :     }
   12935                 :             : 
   12936                 :         324 :   desc += regloc->offset;
   12937                 :             : 
   12938         [ +  + ]:        1172 :   for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
   12939                 :             :     {
   12940                 :         848 :       char name[REGNAMESZ];
   12941                 :         848 :       int bits;
   12942                 :         848 :       int type;
   12943                 :         848 :       register_info (ebl, reg, regloc, name, &bits, &type);
   12944                 :             : 
   12945                 :             : #define TYPES                                                                 \
   12946                 :             :       BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8);                          \
   12947                 :             :       BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16);                       \
   12948                 :             :       BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32);                             \
   12949                 :             :       BITS (64, XWORD, "%20" PRId64, "  0x%.16" PRIx64)
   12950                 :             : 
   12951                 :             : #define BITS(bits, xtype, sfmt, ufmt)                           \
   12952                 :             :       uint##bits##_t b##bits; int##bits##_t b##bits##s
   12953                 :         848 :       union { TYPES; uint64_t b128[2]; } value;
   12954                 :             : #undef  BITS
   12955                 :             : 
   12956         [ +  + ]:         848 :       switch (type)
   12957                 :             :         {
   12958                 :         672 :         case DW_ATE_unsigned:
   12959                 :             :         case DW_ATE_signed:
   12960                 :             :         case DW_ATE_address:
   12961   [ -  +  +  +  :         672 :           switch (bits)
                   +  - ]
   12962                 :             :             {
   12963                 :             : #define BITS(bits, xtype, sfmt, ufmt)                                         \
   12964                 :             :             case bits:                                                        \
   12965                 :             :               desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0);           \
   12966                 :             :               if (type == DW_ATE_signed)                                      \
   12967                 :             :                 colno = print_core_item (colno, ' ', WRAP_COLUMN,             \
   12968                 :             :                                          maxregname, name,                    \
   12969                 :             :                                          sfmt, value.b##bits##s);             \
   12970                 :             :               else                                                            \
   12971                 :             :                 colno = print_core_item (colno, ' ', WRAP_COLUMN,             \
   12972                 :             :                                          maxregname, name,                    \
   12973                 :             :                                          ufmt, value.b##bits);                \
   12974                 :             :               break
   12975                 :             : 
   12976   [ -  -  -  +  :         576 :             TYPES;
             +  +  +  + ]
   12977                 :             : 
   12978                 :          96 :             case 128:
   12979         [ -  + ]:          96 :               assert (type == DW_ATE_unsigned);
   12980                 :          96 :               desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
   12981                 :          96 :               int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
   12982                 :          96 :               colno = print_core_item (colno, ' ', WRAP_COLUMN,
   12983                 :             :                                        maxregname, name,
   12984                 :             :                                        "0x%.16" PRIx64 "%.16" PRIx64,
   12985                 :          96 :                                        value.b128[!be], value.b128[be]);
   12986                 :          96 :               break;
   12987                 :             : 
   12988                 :           0 :             default:
   12989                 :           0 :               abort ();
   12990                 :             : #undef  BITS
   12991                 :             :             }
   12992                 :             :           break;
   12993                 :             : 
   12994                 :         176 :         default:
   12995                 :             :           /* Print each byte in hex, the whole thing in native byte order.  */
   12996         [ -  + ]:         176 :           assert (bits % 8 == 0);
   12997                 :         176 :           const uint8_t *bytes = desc;
   12998                 :         176 :           desc += bits / 8;
   12999                 :         176 :           char hex[bits / 4 + 1];
   13000                 :         176 :           hex[bits / 4] = '\0';
   13001                 :         176 :           int incr = 1;
   13002         [ +  + ]:         176 :           if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
   13003                 :             :             {
   13004                 :          96 :               bytes += bits / 8 - 1;
   13005                 :          96 :               incr = -1;
   13006                 :             :             }
   13007                 :         176 :           size_t idx = 0;
   13008         [ +  + ]:        1744 :           for (char *h = hex; bits > 0; bits -= 8, idx += incr)
   13009                 :             :             {
   13010                 :        1568 :               *h++ = "0123456789abcdef"[bytes[idx] >> 4];
   13011                 :        1568 :               *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
   13012                 :             :             }
   13013                 :         176 :           colno = print_core_item (colno, ' ', WRAP_COLUMN,
   13014                 :             :                                    maxregname, name, "0x%s", hex);
   13015                 :         176 :           break;
   13016                 :             :         }
   13017                 :         848 :       desc += regloc->pad;
   13018                 :             : 
   13019                 :             : #undef TYPES
   13020                 :             :     }
   13021                 :             : 
   13022                 :             :   return colno;
   13023                 :             : }
   13024                 :             : 
   13025                 :             : 
   13026                 :             : struct register_info
   13027                 :             : {
   13028                 :             :   const Ebl_Register_Location *regloc;
   13029                 :             :   const char *set;
   13030                 :             :   char name[REGNAMESZ];
   13031                 :             :   int regno;
   13032                 :             :   int bits;
   13033                 :             :   int type;
   13034                 :             : };
   13035                 :             : 
   13036                 :             : static int
   13037                 :        4132 : register_bitpos (const struct register_info *r)
   13038                 :             : {
   13039                 :        4132 :   return (r->regloc->offset * 8
   13040                 :        4132 :           + ((r->regno - r->regloc->regno)
   13041                 :        4132 :              * (r->regloc->bits + r->regloc->pad * 8)));
   13042                 :             : }
   13043                 :             : 
   13044                 :             : static int
   13045                 :        2124 : compare_sets_by_info (const struct register_info *r1,
   13046                 :             :                       const struct register_info *r2)
   13047                 :             : {
   13048                 :        2124 :   return ((int) r2->bits - (int) r1->bits
   13049         [ +  + ]:        2124 :           ?: register_bitpos (r1) - register_bitpos (r2));
   13050                 :             : }
   13051                 :             : 
   13052                 :             : /* Sort registers by set, and by size and layout offset within each set.  */
   13053                 :             : static int
   13054                 :        9190 : compare_registers (const void *a, const void *b)
   13055                 :             : {
   13056                 :        9190 :   const struct register_info *r1 = a;
   13057                 :        9190 :   const struct register_info *r2 = b;
   13058                 :             : 
   13059                 :             :   /* Unused elements sort last.  */
   13060         [ +  + ]:        9190 :   if (r1->regloc == NULL)
   13061                 :        6618 :     return r2->regloc == NULL ? 0 : 1;
   13062         [ +  + ]:        2572 :   if (r2->regloc == NULL)
   13063                 :             :     return -1;
   13064                 :             : 
   13065         [ +  + ]:        2242 :   return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
   13066         [ -  + ]:        2242 :           ?: compare_sets_by_info (r1, r2));
   13067                 :             : }
   13068                 :             : 
   13069                 :             : /* Sort register sets by layout offset of the first register in the set.  */
   13070                 :             : static int
   13071                 :          40 : compare_register_sets (const void *a, const void *b)
   13072                 :             : {
   13073                 :          40 :   const struct register_info *const *p1 = a;
   13074                 :          40 :   const struct register_info *const *p2 = b;
   13075                 :          40 :   return compare_sets_by_info (*p1, *p2);
   13076                 :             : }
   13077                 :             : 
   13078                 :             : static inline bool
   13079                 :        1728 : same_set (const struct register_info *a,
   13080                 :             :           const struct register_info *b,
   13081                 :             :           const struct register_info *regs,
   13082                 :             :           size_t maxnreg)
   13083                 :             : {
   13084         [ +  - ]:        1728 :   return (a < &regs[maxnreg] && a->regloc != NULL
   13085   [ +  -  +  + ]:        1728 :           && b < &regs[maxnreg] && b->regloc != NULL
   13086         [ +  + ]:        1692 :           && a->bits == b->bits
   13087   [ +  -  +  +  :        3384 :           && (a->set == b->set || !strcmp (a->set, b->set)));
                   -  + ]
   13088                 :             : }
   13089                 :             : 
   13090                 :             : static unsigned int
   13091                 :          74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
   13092                 :             :                        const Ebl_Register_Location *reglocs, size_t nregloc)
   13093                 :          74 : {
   13094         [ +  + ]:          74 :   if (nregloc == 0)
   13095                 :             :     return 0;
   13096                 :             : 
   13097                 :          36 :   ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
   13098         [ -  + ]:          36 :   if (maxnreg <= 0)
   13099                 :             :     {
   13100         [ #  # ]:           0 :       for (size_t i = 0; i < nregloc; ++i)
   13101                 :           0 :         if (maxnreg < reglocs[i].regno + reglocs[i].count)
   13102                 :             :           maxnreg = reglocs[i].regno + reglocs[i].count;
   13103         [ #  # ]:           0 :       assert (maxnreg > 0);
   13104                 :             :     }
   13105                 :             : 
   13106                 :          36 :   struct register_info regs[maxnreg];
   13107                 :          36 :   memset (regs, 0, sizeof regs);
   13108                 :             : 
   13109                 :             :   /* Sort to collect the sets together.  */
   13110                 :          36 :   int maxreg = 0;
   13111         [ +  + ]:         360 :   for (size_t i = 0; i < nregloc; ++i)
   13112                 :         324 :     for (int reg = reglocs[i].regno;
   13113         [ +  + ]:        1172 :          reg < reglocs[i].regno + reglocs[i].count;
   13114                 :         848 :          ++reg)
   13115                 :             :       {
   13116         [ -  + ]:         848 :         assert (reg < maxnreg);
   13117                 :         848 :         if (reg > maxreg)
   13118                 :             :           maxreg = reg;
   13119                 :         848 :         struct register_info *info = &regs[reg];
   13120                 :         848 :         info->regloc = &reglocs[i];
   13121                 :         848 :         info->regno = reg;
   13122                 :         848 :         info->set = register_info (ebl, reg, &reglocs[i],
   13123                 :         848 :                                    info->name, &info->bits, &info->type);
   13124                 :             :       }
   13125                 :          36 :   qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
   13126                 :             : 
   13127                 :             :   /* Collect the unique sets and sort them.  */
   13128                 :          36 :   struct register_info *sets[maxreg + 1];
   13129                 :          36 :   sets[0] = &regs[0];
   13130                 :          36 :   size_t nsets = 1;
   13131         [ +  + ]:        2558 :   for (int i = 1; i <= maxreg; ++i)
   13132         [ +  + ]:        2522 :     if (regs[i].regloc != NULL
   13133         [ +  + ]:         812 :         && !same_set (&regs[i], &regs[i - 1], regs, maxnreg))
   13134                 :          32 :       sets[nsets++] = &regs[i];
   13135                 :          36 :   qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
   13136                 :             : 
   13137                 :             :   /* Write out all the sets.  */
   13138                 :          36 :   unsigned int colno = 0;
   13139         [ +  + ]:         104 :   for (size_t i = 0; i < nsets; ++i)
   13140                 :             :     {
   13141                 :             :       /* Find the longest name of a register in this set.  */
   13142                 :          68 :       size_t maxname = 0;
   13143                 :          68 :       const struct register_info *end;
   13144         [ +  + ]:         916 :       for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
   13145                 :             :         {
   13146                 :         848 :           size_t len = strlen (end->name);
   13147                 :         848 :           if (len > maxname)
   13148                 :             :             maxname = len;
   13149                 :             :         }
   13150                 :             : 
   13151                 :             :       for (const struct register_info *reg = sets[i];
   13152         [ +  + ]:         392 :            reg < end;
   13153                 :         324 :            reg += reg->regloc->count ?: 1)
   13154         [ +  - ]:         324 :         colno = handle_core_register (ebl, core, maxname,
   13155                 :         324 :                                       reg->regloc, desc, colno);
   13156                 :             : 
   13157                 :             :       /* Force a line break at the end of the group.  */
   13158                 :          68 :       colno = WRAP_COLUMN;
   13159                 :             :     }
   13160                 :             : 
   13161                 :             :   return colno;
   13162                 :             : }
   13163                 :             : 
   13164                 :             : static void
   13165                 :          18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   13166                 :             : {
   13167                 :          18 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
   13168         [ -  + ]:          18 :   if (data == NULL)
   13169                 :           0 :   elf_error:
   13170                 :           0 :     error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
   13171                 :             : 
   13172                 :          18 :   const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
   13173         [ +  + ]:         354 :   for (size_t i = 0; i < nauxv; ++i)
   13174                 :             :     {
   13175                 :         336 :       GElf_auxv_t av_mem;
   13176                 :         336 :       GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
   13177         [ -  + ]:         336 :       if (av == NULL)
   13178                 :           0 :         goto elf_error;
   13179                 :             : 
   13180                 :         336 :       const char *name;
   13181                 :         336 :       const char *fmt;
   13182         [ -  + ]:         336 :       if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
   13183                 :             :         {
   13184                 :             :           /* Unknown type.  */
   13185         [ #  # ]:           0 :           if (av->a_un.a_val == 0)
   13186                 :           0 :             printf ("    %" PRIu64 "\n", av->a_type);
   13187                 :             :           else
   13188                 :           0 :             printf ("    %" PRIu64 ": %#" PRIx64 "\n",
   13189                 :             :                     av->a_type, av->a_un.a_val);
   13190                 :             :         }
   13191                 :             :       else
   13192   [ +  +  +  -  :         336 :         switch (fmt[0])
                   +  - ]
   13193                 :             :           {
   13194                 :          18 :           case '\0':            /* Normally zero.  */
   13195         [ +  - ]:          18 :             if (av->a_un.a_val == 0)
   13196                 :             :               {
   13197                 :          18 :                 printf ("    %s\n", name);
   13198                 :          18 :                 break;
   13199                 :             :               }
   13200                 :         148 :             FALLTHROUGH;
   13201                 :             :           case 'x':             /* hex */
   13202                 :             :           case 'p':             /* address */
   13203                 :             :           case 's':             /* address of string */
   13204                 :         148 :             printf ("    %s: %#" PRIx64 "\n", name, av->a_un.a_val);
   13205                 :         148 :             break;
   13206                 :         162 :           case 'u':
   13207                 :         162 :             printf ("    %s: %" PRIu64 "\n", name, av->a_un.a_val);
   13208                 :         162 :             break;
   13209                 :           0 :           case 'd':
   13210                 :           0 :             printf ("    %s: %" PRId64 "\n", name, av->a_un.a_val);
   13211                 :           0 :             break;
   13212                 :             : 
   13213                 :           8 :           case 'b':
   13214                 :           8 :             printf ("    %s: %#" PRIx64 "  ", name, av->a_un.a_val);
   13215                 :           8 :             GElf_Xword bit = 1;
   13216                 :           8 :             const char *pfx = "<";
   13217         [ +  + ]:         220 :             for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
   13218                 :             :               {
   13219         [ +  + ]:         212 :                 if (av->a_un.a_val & bit)
   13220                 :             :                   {
   13221                 :         154 :                     printf ("%s%s", pfx, p);
   13222                 :         154 :                     pfx = " ";
   13223                 :             :                   }
   13224                 :         212 :                 bit <<= 1;
   13225                 :             :               }
   13226                 :           8 :             printf (">\n");
   13227                 :           8 :             break;
   13228                 :             : 
   13229                 :           0 :           default:
   13230                 :           0 :             abort ();
   13231                 :             :           }
   13232                 :             :     }
   13233                 :          18 : }
   13234                 :             : 
   13235                 :             : static bool
   13236                 :         390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
   13237                 :             : {
   13238   [ +  -  +  - ]:         390 :   return ptr < end && (size_t) (end - ptr) >= sz;
   13239                 :             : }
   13240                 :             : 
   13241                 :             : static bool
   13242                 :          36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
   13243                 :             :               int *retp)
   13244                 :             : {
   13245         [ +  - ]:          36 :   if (! buf_has_data (*ptrp, end, 4))
   13246                 :             :     return false;
   13247                 :             : 
   13248                 :          36 :   *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
   13249                 :          36 :   return true;
   13250                 :             : }
   13251                 :             : 
   13252                 :             : static bool
   13253                 :         354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
   13254                 :             :                 uint64_t *retp)
   13255                 :             : {
   13256                 :         354 :   size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
   13257         [ +  - ]:         354 :   if (! buf_has_data (*ptrp, end, sz))
   13258                 :             :     return false;
   13259                 :             : 
   13260                 :         354 :   union
   13261                 :             :   {
   13262                 :             :     uint64_t u64;
   13263                 :             :     uint32_t u32;
   13264                 :             :   } u;
   13265                 :             : 
   13266                 :         354 :   *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
   13267                 :             : 
   13268         [ +  + ]:         354 :   if (sz == 4)
   13269                 :         186 :     *retp = u.u32;
   13270                 :             :   else
   13271                 :         168 :     *retp = u.u64;
   13272                 :             :   return true;
   13273                 :             : }
   13274                 :             : 
   13275                 :             : static void
   13276                 :          12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   13277                 :             : {
   13278                 :          12 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
   13279         [ -  + ]:          12 :   if (data == NULL)
   13280                 :           0 :     error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
   13281                 :             : 
   13282                 :          12 :   unsigned char const *ptr = data->d_buf;
   13283                 :          12 :   unsigned char const *const end = data->d_buf + data->d_size;
   13284                 :             : 
   13285                 :             :   /* Siginfo head is three ints: signal number, error number, origin
   13286                 :             :      code.  */
   13287                 :          12 :   int si_signo, si_errno, si_code;
   13288         [ -  + ]:          12 :   if (! buf_read_int (core, &ptr, end, &si_signo)
   13289         [ -  + ]:          12 :       || ! buf_read_int (core, &ptr, end, &si_errno)
   13290         [ -  + ]:          12 :       || ! buf_read_int (core, &ptr, end, &si_code))
   13291                 :             :     {
   13292                 :           0 :     fail:
   13293                 :           0 :       printf ("    Not enough data in NT_SIGINFO note.\n");
   13294                 :           0 :       return;
   13295                 :             :     }
   13296                 :             : 
   13297                 :             :   /* Next is a pointer-aligned union of structures.  On 64-bit
   13298                 :             :      machines, that implies a word of padding.  */
   13299         [ +  + ]:          12 :   if (gelf_getclass (core) == ELFCLASS64)
   13300                 :           6 :     ptr += 4;
   13301                 :             : 
   13302                 :          12 :   printf ("    si_signo: %d, si_errno: %d, si_code: %d\n",
   13303                 :             :           si_signo, si_errno, si_code);
   13304                 :             : 
   13305         [ +  - ]:          12 :   if (si_code > 0)
   13306         [ +  - ]:          12 :     switch (si_signo)
   13307                 :             :       {
   13308                 :          12 :       case CORE_SIGILL:
   13309                 :             :       case CORE_SIGFPE:
   13310                 :             :       case CORE_SIGSEGV:
   13311                 :             :       case CORE_SIGBUS:
   13312                 :             :         {
   13313                 :          12 :           uint64_t addr;
   13314         [ -  + ]:          12 :           if (! buf_read_ulong (core, &ptr, end, &addr))
   13315                 :           0 :             goto fail;
   13316                 :          12 :           printf ("    fault address: %#" PRIx64 "\n", addr);
   13317                 :          12 :           break;
   13318                 :             :         }
   13319                 :             :       default:
   13320                 :             :         ;
   13321                 :             :       }
   13322         [ #  # ]:           0 :   else if (si_code == CORE_SI_USER)
   13323                 :             :     {
   13324                 :           0 :       int pid, uid;
   13325         [ #  # ]:           0 :       if (! buf_read_int (core, &ptr, end, &pid)
   13326         [ #  # ]:           0 :           || ! buf_read_int (core, &ptr, end, &uid))
   13327                 :           0 :         goto fail;
   13328                 :           0 :       printf ("    sender PID: %d, sender UID: %d\n", pid, uid);
   13329                 :             :     }
   13330                 :             : }
   13331                 :             : 
   13332                 :             : static void
   13333                 :          12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
   13334                 :             : {
   13335                 :          12 :   Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
   13336         [ -  + ]:          12 :   if (data == NULL)
   13337                 :           0 :     error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
   13338                 :             : 
   13339                 :          12 :   unsigned char const *ptr = data->d_buf;
   13340                 :          12 :   unsigned char const *const end = data->d_buf + data->d_size;
   13341                 :             : 
   13342                 :          12 :   uint64_t count, page_size;
   13343         [ -  + ]:          12 :   if (! buf_read_ulong (core, &ptr, end, &count)
   13344         [ -  + ]:          12 :       || ! buf_read_ulong (core, &ptr, end, &page_size))
   13345                 :             :     {
   13346                 :           0 :     fail:
   13347                 :           0 :       printf ("    Not enough data in NT_FILE note.\n");
   13348                 :           0 :       return;
   13349                 :             :     }
   13350                 :             : 
   13351                 :          12 :   size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
   13352                 :          12 :   uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
   13353         [ -  + ]:          12 :   if (count > maxcount)
   13354                 :           0 :     goto fail;
   13355                 :             : 
   13356                 :             :   /* Where file names are stored.  */
   13357                 :          12 :   unsigned char const *const fstart = ptr + 3 * count * addrsize;
   13358                 :          12 :   char const *fptr = (char *) fstart;
   13359                 :             : 
   13360                 :          12 :   printf ("    %" PRId64 " files:\n", count);
   13361         [ +  + ]:         118 :   for (uint64_t i = 0; i < count; ++i)
   13362                 :             :     {
   13363                 :         106 :       uint64_t mstart, mend, moffset;
   13364         [ +  - ]:         106 :       if (! buf_read_ulong (core, &ptr, fstart, &mstart)
   13365         [ +  - ]:         106 :           || ! buf_read_ulong (core, &ptr, fstart, &mend)
   13366         [ -  + ]:         106 :           || ! buf_read_ulong (core, &ptr, fstart, &moffset))
   13367                 :           0 :         goto fail;
   13368                 :             : 
   13369                 :         106 :       const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
   13370         [ -  + ]:         106 :       if (fnext == NULL)
   13371                 :           0 :         goto fail;
   13372                 :             : 
   13373                 :         106 :       int ct = printf ("      %08" PRIx64 "-%08" PRIx64
   13374                 :             :                        " %08" PRIx64 " %" PRId64,
   13375                 :             :                        mstart, mend, moffset * page_size, mend - mstart);
   13376         [ +  - ]:         106 :       printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
   13377                 :             : 
   13378                 :         106 :       fptr = fnext + 1;
   13379                 :             :     }
   13380                 :             : }
   13381                 :             : 
   13382                 :             : static void
   13383                 :          76 : handle_core_note (Ebl *ebl, const GElf_Ehdr *ehdr,  const GElf_Nhdr *nhdr,
   13384                 :             :                   const char *name, const void *desc)
   13385                 :             : {
   13386                 :          76 :   GElf_Word regs_offset;
   13387                 :          76 :   size_t nregloc;
   13388                 :          76 :   const Ebl_Register_Location *reglocs;
   13389                 :          76 :   size_t nitems;
   13390                 :          76 :   const Ebl_Core_Item *items;
   13391                 :             : 
   13392         [ +  + ]:          76 :   if (! ebl_core_note (ebl, nhdr, name, desc,
   13393                 :             :                        &regs_offset, &nregloc, &reglocs, &nitems, &items))
   13394                 :           2 :     return;
   13395                 :             : 
   13396                 :             :   /* Pass 0 for DESCSZ when there are registers in the note,
   13397                 :             :      so that the ITEMS array does not describe the whole thing.
   13398                 :             :      For non-register notes, the actual descsz might be a multiple
   13399                 :             :      of the unit size, not just exactly the unit size.  */
   13400                 :          74 :   unsigned int colno = handle_core_items (ebl->elf, ehdr, desc,
   13401         [ +  + ]:          74 :                                           nregloc == 0 ? nhdr->n_descsz : 0,
   13402                 :             :                                           items, nitems);
   13403         [ +  + ]:          74 :   if (colno != 0)
   13404                 :          68 :     putchar ('\n');
   13405                 :             : 
   13406                 :          74 :   colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
   13407                 :             :                                  reglocs, nregloc);
   13408         [ +  + ]:          74 :   if (colno != 0)
   13409                 :          36 :     putchar ('\n');
   13410                 :             : }
   13411                 :             : 
   13412                 :             : static void
   13413                 :         506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
   13414                 :             :                    GElf_Off start, Elf_Data *data)
   13415                 :             : {
   13416                 :         506 :   fputs (_("  Owner          Data size  Type\n"), stdout);
   13417                 :             : 
   13418         [ -  + ]:         506 :   if (data == NULL)
   13419                 :           0 :     goto bad_note;
   13420                 :             : 
   13421                 :             :   size_t offset = 0;
   13422                 :             :   GElf_Nhdr nhdr;
   13423                 :             :   size_t name_offset;
   13424                 :             :   size_t desc_offset;
   13425                 :        5120 :   while (offset < data->d_size
   13426   [ +  +  +  - ]:        5120 :          && (offset = gelf_getnote (data, offset,
   13427                 :             :                                     &nhdr, &name_offset, &desc_offset)) > 0)
   13428                 :             :     {
   13429         [ +  - ]:        4614 :       const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
   13430                 :        4614 :       const char *desc = data->d_buf + desc_offset;
   13431                 :             : 
   13432                 :             :       /* GNU Build Attributes are weird, they store most of their data
   13433                 :             :          into the owner name field.  Extract just the owner name
   13434                 :             :          prefix here, then use the rest later as data.  */
   13435                 :        4614 :       bool is_gnu_build_attr
   13436                 :        4614 :         = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
   13437                 :        9798 :       const char *print_name = (is_gnu_build_attr
   13438         [ +  + ]:        4614 :                                 ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
   13439                 :        5184 :       size_t print_namesz = (is_gnu_build_attr
   13440                 :         570 :                              ? strlen (print_name) : nhdr.n_namesz);
   13441                 :             : 
   13442                 :        4614 :       char buf[100];
   13443                 :        4614 :       char buf2[100];
   13444                 :        4614 :       printf (_("  %-13.*s  %9" PRId32 "  %s\n"),
   13445                 :             :               (int) print_namesz, print_name, nhdr.n_descsz,
   13446         [ +  + ]:        4614 :               ehdr->e_type == ET_CORE
   13447                 :         118 :               ? ebl_core_note_type_name (ebl, nhdr.n_type,
   13448                 :             :                                          buf, sizeof (buf))
   13449                 :        4496 :               : ebl_object_note_type_name (ebl, name, nhdr.n_type,
   13450                 :             :                                            nhdr.n_descsz,
   13451                 :             :                                            buf2, sizeof (buf2)));
   13452                 :             : 
   13453                 :             :       /* Filter out invalid entries.  */
   13454                 :        4614 :       if (memchr (name, '\0', nhdr.n_namesz) != NULL
   13455                 :             :           /* XXX For now help broken Linux kernels.  */
   13456                 :             :           || 1)
   13457                 :             :         {
   13458         [ +  + ]:        4614 :           if (ehdr->e_type == ET_CORE)
   13459                 :             :             {
   13460         [ +  + ]:         118 :               if (nhdr.n_type == NT_AUXV
   13461         [ +  - ]:          18 :                   && (nhdr.n_namesz == 4 /* Broken old Linux kernels.  */
   13462   [ +  -  +  - ]:          18 :                       || (nhdr.n_namesz == 5 && name[4] == '\0'))
   13463         [ +  - ]:          18 :                   && !memcmp (name, "CORE", 4))
   13464                 :          18 :                 handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
   13465                 :             :                                   start + desc_offset);
   13466   [ +  +  +  - ]:         100 :               else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
   13467      [ +  +  + ]:          76 :                 switch (nhdr.n_type)
   13468                 :             :                   {
   13469                 :          12 :                   case NT_SIGINFO:
   13470                 :          12 :                     handle_siginfo_note (ebl->elf, nhdr.n_descsz,
   13471                 :             :                                          start + desc_offset);
   13472                 :          12 :                     break;
   13473                 :             : 
   13474                 :          12 :                   case NT_FILE:
   13475                 :          12 :                     handle_file_note (ebl->elf, nhdr.n_descsz,
   13476                 :             :                                       start + desc_offset);
   13477                 :          12 :                     break;
   13478                 :             : 
   13479                 :          52 :                   default:
   13480                 :          52 :                     handle_core_note (ebl, ehdr, &nhdr, name, desc);
   13481                 :             :                   }
   13482                 :             :               else
   13483                 :          24 :                 handle_core_note (ebl, ehdr, &nhdr, name, desc);
   13484                 :             :             }
   13485                 :             :           else
   13486                 :        4496 :             ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
   13487                 :             :                              nhdr.n_descsz, desc);
   13488                 :             :         }
   13489                 :             :     }
   13490                 :             : 
   13491         [ +  - ]:         506 :   if (offset == data->d_size)
   13492                 :         506 :     return;
   13493                 :             : 
   13494                 :           0 :  bad_note:
   13495                 :           0 :   error (0, 0,
   13496                 :           0 :          _("cannot get content of note: %s"),
   13497                 :           0 :          data != NULL ? "garbage data" : elf_errmsg (-1));
   13498                 :             : }
   13499                 :             : 
   13500                 :             : static void
   13501                 :         230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
   13502                 :             : {
   13503                 :             :   /* If we have section headers, just look for SHT_NOTE sections.
   13504                 :             :      In a debuginfo file, the program headers are not reliable.  */
   13505         [ +  + ]:         230 :   if (shnum != 0)
   13506                 :             :     {
   13507                 :             :       /* Get the section header string table index.  */
   13508                 :         208 :       size_t shstrndx;
   13509         [ -  + ]:         208 :       if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
   13510                 :           0 :         error_exit (0, _("cannot get section header string table index"));
   13511                 :             : 
   13512                 :             :       Elf_Scn *scn = NULL;
   13513         [ +  + ]:        6446 :       while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   13514                 :             :         {
   13515                 :        6238 :           GElf_Shdr shdr_mem;
   13516                 :        6238 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
   13517                 :             : 
   13518   [ +  -  +  + ]:        6238 :           if (shdr == NULL || shdr->sh_type != SHT_NOTE)
   13519                 :             :             /* Not what we are looking for.  */
   13520                 :        5756 :             continue;
   13521                 :             : 
   13522         [ -  + ]:         482 :           if (notes_section != NULL)
   13523                 :             :             {
   13524                 :           0 :               char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
   13525   [ #  #  #  # ]:           0 :               if (sname == NULL || strcmp (sname, notes_section) != 0)
   13526                 :           0 :                 continue;
   13527                 :             :             }
   13528                 :             : 
   13529                 :         482 :           printf (_("\
   13530                 :             : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
   13531                 :             :                   elf_ndxscn (scn),
   13532                 :         482 :                   elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
   13533                 :             :                   shdr->sh_size, shdr->sh_offset);
   13534                 :             : 
   13535                 :         482 :           handle_notes_data (ebl, ehdr, shdr->sh_offset,
   13536                 :             :                              elf_getdata (scn, NULL));
   13537                 :             :         }
   13538                 :         208 :       return;
   13539                 :             :     }
   13540                 :             : 
   13541                 :             :   /* We have to look through the program header to find the note
   13542                 :             :      sections.  There can be more than one.  */
   13543         [ +  + ]:         280 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
   13544                 :             :     {
   13545                 :         258 :       GElf_Phdr mem;
   13546                 :         258 :       GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
   13547                 :             : 
   13548   [ +  -  +  + ]:         258 :       if (phdr == NULL || phdr->p_type != PT_NOTE)
   13549                 :             :         /* Not what we are looking for.  */
   13550                 :         234 :         continue;
   13551                 :             : 
   13552                 :          24 :       printf (_("\
   13553                 :             : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
   13554                 :             :               phdr->p_filesz, phdr->p_offset);
   13555                 :             : 
   13556                 :          24 :       handle_notes_data (ebl, ehdr, phdr->p_offset,
   13557                 :             :                          elf_getdata_rawchunk (ebl->elf,
   13558                 :          24 :                                                phdr->p_offset, phdr->p_filesz,
   13559         [ +  + ]:          24 :                                                (phdr->p_align == 8
   13560                 :             :                                                 ? ELF_T_NHDR8 : ELF_T_NHDR)));
   13561                 :             :     }
   13562                 :             : }
   13563                 :             : 
   13564                 :             : 
   13565                 :             : static void
   13566                 :          12 : hex_dump (const uint8_t *data, size_t len)
   13567                 :             : {
   13568                 :          12 :   size_t pos = 0;
   13569         [ +  + ]:          56 :   while (pos < len)
   13570                 :             :     {
   13571                 :          44 :       printf ("  0x%08zx ", pos);
   13572                 :             : 
   13573                 :          44 :       const size_t chunk = MIN (len - pos, 16);
   13574                 :             : 
   13575         [ +  + ]:         688 :       for (size_t i = 0; i < chunk; ++i)
   13576         [ +  + ]:         644 :         if (i % 4 == 3)
   13577                 :         160 :           printf ("%02x ", data[pos + i]);
   13578                 :             :         else
   13579                 :         484 :           printf ("%02x", data[pos + i]);
   13580                 :             : 
   13581         [ +  + ]:          44 :       if (chunk < 16)
   13582                 :           4 :         printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
   13583                 :             : 
   13584         [ +  + ]:         688 :       for (size_t i = 0; i < chunk; ++i)
   13585                 :             :         {
   13586                 :         644 :           unsigned char b = data[pos + i];
   13587         [ +  + ]:        1288 :           printf ("%c", isprint (b) ? b : '.');
   13588                 :             :         }
   13589                 :             : 
   13590                 :          44 :       putchar ('\n');
   13591                 :          44 :       pos += chunk;
   13592                 :             :     }
   13593                 :          12 : }
   13594                 :             : 
   13595                 :             : static void
   13596                 :          12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
   13597                 :             : {
   13598   [ +  -  -  + ]:          12 :   if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
   13599                 :           0 :     printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
   13600                 :             :             elf_ndxscn (scn), name);
   13601                 :             :   else
   13602                 :             :     {
   13603         [ +  + ]:          12 :       if (print_decompress)
   13604                 :             :         {
   13605                 :             :           /* We try to decompress the section, but keep the old shdr around
   13606                 :             :              so we can show both the original shdr size and the uncompressed
   13607                 :             :              data size.   */
   13608         [ +  + ]:           8 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
   13609                 :             :             {
   13610         [ -  + ]:           4 :               if (elf_compress (scn, 0, 0) < 0)
   13611                 :           0 :                 printf ("WARNING: %s [%zd]\n",
   13612                 :             :                         _("Couldn't uncompress section"),
   13613                 :             :                         elf_ndxscn (scn));
   13614                 :             :             }
   13615   [ +  -  +  - ]:           4 :           else if (name && startswith (name, ".zdebug"))
   13616                 :             :             {
   13617         [ -  + ]:           4 :               if (elf_compress_gnu (scn, 0, 0) < 0)
   13618                 :           0 :                 printf ("WARNING: %s [%zd]\n",
   13619                 :             :                         _("Couldn't uncompress section"),
   13620                 :             :                         elf_ndxscn (scn));
   13621                 :             :             }
   13622                 :             :         }
   13623                 :             : 
   13624                 :          12 :       Elf_Data *data = elf_rawdata (scn, NULL);
   13625         [ -  + ]:          12 :       if (data == NULL)
   13626                 :           0 :         error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
   13627                 :             :                elf_ndxscn (scn), name, elf_errmsg (-1));
   13628                 :             :       else
   13629                 :             :         {
   13630         [ +  + ]:          12 :           if (data->d_size == shdr->sh_size)
   13631                 :           4 :             printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
   13632                 :             :                              " bytes at offset %#0" PRIx64 ":\n"),
   13633                 :             :                     elf_ndxscn (scn), name,
   13634                 :           4 :                     shdr->sh_size, shdr->sh_offset);
   13635                 :             :           else
   13636                 :           8 :             printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
   13637                 :             :                              " bytes (%zd uncompressed) at offset %#0"
   13638                 :             :                              PRIx64 ":\n"),
   13639                 :             :                     elf_ndxscn (scn), name,
   13640                 :           8 :                     shdr->sh_size, data->d_size, shdr->sh_offset);
   13641                 :          12 :           hex_dump (data->d_buf, data->d_size);
   13642                 :             :         }
   13643                 :             :     }
   13644                 :          12 : }
   13645                 :             : 
   13646                 :             : static void
   13647                 :         374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
   13648                 :             : {
   13649   [ +  -  +  + ]:         374 :   if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
   13650                 :          42 :     printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
   13651                 :             :             elf_ndxscn (scn), name);
   13652                 :             :   else
   13653                 :             :     {
   13654         [ +  + ]:         332 :       if (print_decompress)
   13655                 :             :         {
   13656                 :             :           /* We try to decompress the section, but keep the old shdr around
   13657                 :             :              so we can show both the original shdr size and the uncompressed
   13658                 :             :              data size.  */
   13659         [ -  + ]:           2 :           if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
   13660                 :             :             {
   13661         [ #  # ]:           0 :               if (elf_compress (scn, 0, 0) < 0)
   13662                 :           0 :                 printf ("WARNING: %s [%zd]\n",
   13663                 :             :                         _("Couldn't uncompress section"),
   13664                 :             :                         elf_ndxscn (scn));
   13665                 :             :             }
   13666   [ +  -  +  - ]:           2 :           else if (name && startswith (name, ".zdebug"))
   13667                 :             :             {
   13668         [ -  + ]:           2 :               if (elf_compress_gnu (scn, 0, 0) < 0)
   13669                 :           0 :                 printf ("WARNING: %s [%zd]\n",
   13670                 :             :                         _("Couldn't uncompress section"),
   13671                 :             :                         elf_ndxscn (scn));
   13672                 :             :             }
   13673                 :             :         }
   13674                 :             : 
   13675                 :         332 :       Elf_Data *data = elf_rawdata (scn, NULL);
   13676         [ -  + ]:         332 :       if (data == NULL)
   13677                 :           0 :         error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
   13678                 :             :                elf_ndxscn (scn), name, elf_errmsg (-1));
   13679                 :             :       else
   13680                 :             :         {
   13681         [ +  + ]:         332 :           if (data->d_size == shdr->sh_size)
   13682                 :         330 :             printf (_("\nString section [%zu] '%s' contains %" PRIu64
   13683                 :             :                              " bytes at offset %#0" PRIx64 ":\n"),
   13684                 :             :                     elf_ndxscn (scn), name,
   13685                 :         330 :                     shdr->sh_size, shdr->sh_offset);
   13686                 :             :           else
   13687                 :           2 :             printf (_("\nString section [%zu] '%s' contains %" PRIu64
   13688                 :             :                              " bytes (%zd uncompressed) at offset %#0"
   13689                 :             :                              PRIx64 ":\n"),
   13690                 :             :                     elf_ndxscn (scn), name,
   13691                 :           2 :                     shdr->sh_size, data->d_size, shdr->sh_offset);
   13692                 :             : 
   13693                 :         332 :           const char *start = data->d_buf;
   13694                 :         332 :           const char *const limit = start + data->d_size;
   13695                 :       47014 :           do
   13696                 :             :             {
   13697                 :       47014 :               const char *end = memchr (start, '\0', limit - start);
   13698                 :       47014 :               const size_t pos = start - (const char *) data->d_buf;
   13699         [ -  + ]:       47014 :               if (unlikely (end == NULL))
   13700                 :             :                 {
   13701                 :           0 :                   printf ("  [%6zx]- %.*s\n",
   13702                 :             :                           pos, (int) (limit - start), start);
   13703                 :           0 :                   break;
   13704                 :             :                 }
   13705                 :       47014 :               printf ("  [%6zx]  %s\n", pos, start);
   13706                 :       47014 :               start = end + 1;
   13707         [ +  + ]:       47014 :             } while (start < limit);
   13708                 :             :         }
   13709                 :             :     }
   13710                 :         374 : }
   13711                 :             : 
   13712                 :             : static void
   13713                 :         198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
   13714                 :             :                            void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
   13715                 :             :                                          const char *name))
   13716                 :             : {
   13717                 :             :   /* Get the section header string table index.  */
   13718                 :         198 :   size_t shstrndx;
   13719         [ -  + ]:         198 :   if (elf_getshdrstrndx (elf, &shstrndx) < 0)
   13720                 :           0 :     error_exit (0, _("cannot get section header string table index"));
   13721                 :             : 
   13722         [ +  + ]:         764 :   for (const struct section_argument *a = list; a != NULL; a = a->next)
   13723                 :             :     {
   13724                 :         566 :       Elf_Scn *scn;
   13725                 :         566 :       GElf_Shdr shdr_mem;
   13726                 :         566 :       const char *name = NULL;
   13727                 :             : 
   13728                 :         566 :       char *endp = NULL;
   13729                 :         566 :       unsigned long int shndx = strtoul (a->arg, &endp, 0);
   13730   [ +  +  -  + ]:         566 :       if (endp != a->arg && *endp == '\0')
   13731                 :             :         {
   13732                 :           2 :           scn = elf_getscn (elf, shndx);
   13733         [ -  + ]:           2 :           if (scn == NULL)
   13734                 :             :             {
   13735                 :           0 :               error (0, 0, _("\nsection [%lu] does not exist"), shndx);
   13736                 :           0 :               continue;
   13737                 :             :             }
   13738                 :             : 
   13739         [ -  + ]:           2 :           if (gelf_getshdr (scn, &shdr_mem) == NULL)
   13740                 :           0 :             error_exit (0, _("cannot get section header: %s"),
   13741                 :             :                         elf_errmsg (-1));
   13742                 :           2 :           name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
   13743                 :           2 :           (*dump) (scn, &shdr_mem, name);
   13744                 :             :         }
   13745                 :             :       else
   13746                 :             :         {
   13747                 :             :           /* Need to look up the section by name.  */
   13748                 :             :           scn = NULL;
   13749                 :             :           bool found = false;
   13750         [ +  + ]:       18180 :           while ((scn = elf_nextscn (elf, scn)) != NULL)
   13751                 :             :             {
   13752         [ -  + ]:       17616 :               if (gelf_getshdr (scn, &shdr_mem) == NULL)
   13753                 :           0 :                 continue;
   13754                 :       17616 :               name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
   13755         [ -  + ]:       17616 :               if (name == NULL)
   13756                 :           0 :                 continue;
   13757         [ +  + ]:       17616 :               if (!strcmp (name, a->arg))
   13758                 :             :                 {
   13759                 :         384 :                   found = true;
   13760                 :         384 :                   (*dump) (scn, &shdr_mem, name);
   13761                 :             :                 }
   13762                 :             :             }
   13763                 :             : 
   13764   [ +  +  -  + ]:         564 :           if (unlikely (!found) && !a->implicit)
   13765                 :           0 :             error (0, 0, _("\nsection '%s' does not exist"), a->arg);
   13766                 :             :         }
   13767                 :             :     }
   13768                 :         198 : }
   13769                 :             : 
   13770                 :             : static void
   13771                 :          12 : dump_data (Ebl *ebl)
   13772                 :             : {
   13773                 :          12 :   for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
   13774                 :          12 : }
   13775                 :             : 
   13776                 :             : static void
   13777                 :         186 : dump_strings (Ebl *ebl)
   13778                 :             : {
   13779                 :         186 :   for_each_section_argument (ebl->elf, string_sections, &print_string_section);
   13780                 :         186 : }
   13781                 :             : 
   13782                 :             : static void
   13783                 :           0 : print_strings (Ebl *ebl)
   13784                 :             : {
   13785                 :             :   /* Get the section header string table index.  */
   13786                 :           0 :   size_t shstrndx;
   13787         [ #  # ]:           0 :   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
   13788                 :           0 :     error_exit (0, _("cannot get section header string table index"));
   13789                 :             : 
   13790                 :             :   Elf_Scn *scn;
   13791                 :             :   GElf_Shdr shdr_mem;
   13792                 :             :   const char *name;
   13793                 :             :   scn = NULL;
   13794         [ #  # ]:           0 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
   13795                 :             :     {
   13796         [ #  # ]:           0 :       if (gelf_getshdr (scn, &shdr_mem) == NULL)
   13797                 :           0 :         continue;
   13798                 :             : 
   13799         [ #  # ]:           0 :       if (shdr_mem.sh_type != SHT_PROGBITS
   13800         [ #  # ]:           0 :           || !(shdr_mem.sh_flags & SHF_STRINGS))
   13801                 :           0 :         continue;
   13802                 :             : 
   13803                 :           0 :       name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
   13804         [ #  # ]:           0 :       if (name == NULL)
   13805                 :           0 :         continue;
   13806                 :             : 
   13807                 :           0 :       print_string_section (scn, &shdr_mem, name);
   13808                 :             :     }
   13809                 :           0 : }
   13810                 :             : 
   13811                 :             : static void
   13812                 :           4 : dump_archive_index (Elf *elf, const char *fname)
   13813                 :             : {
   13814                 :           4 :   size_t narsym;
   13815                 :           4 :   const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
   13816         [ -  + ]:           4 :   if (arsym == NULL)
   13817                 :             :     {
   13818                 :           0 :       int result = elf_errno ();
   13819         [ #  # ]:           0 :       if (unlikely (result != ELF_E_NO_INDEX))
   13820                 :           0 :         error_exit (0, _("cannot get symbol index of archive '%s': %s"),
   13821                 :             :                     fname, elf_errmsg (result));
   13822                 :             :       else
   13823                 :           0 :         printf (_("\nArchive '%s' has no symbol index\n"), fname);
   13824                 :           0 :       return;
   13825                 :             :     }
   13826                 :             : 
   13827                 :           4 :   printf (_("\nIndex of archive '%s' has %zu entries:\n"),
   13828                 :             :           fname, narsym);
   13829                 :             : 
   13830                 :           4 :   size_t as_off = 0;
   13831         [ +  + ]:          22 :   for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
   13832                 :             :     {
   13833         [ +  + ]:          18 :       if (s->as_off != as_off)
   13834                 :             :         {
   13835                 :          12 :           as_off = s->as_off;
   13836                 :             : 
   13837                 :          12 :           Elf *subelf = NULL;
   13838         [ +  - ]:          12 :           if (unlikely (elf_rand (elf, as_off) == 0)
   13839         [ -  + ]:          12 :               || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
   13840                 :             :                            == NULL))
   13841                 :             : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
   13842                 :             :             while (1)
   13843                 :             : #endif
   13844                 :           0 :               error_exit (0,
   13845                 :             :                           _("cannot extract member at offset %zu in '%s': %s"),
   13846                 :             :                           as_off, fname, elf_errmsg (-1));
   13847                 :             : 
   13848                 :          12 :           const Elf_Arhdr *h = elf_getarhdr (subelf);
   13849         [ +  - ]:          12 :           if (h != NULL)
   13850                 :          12 :             printf (_("Archive member '%s' contains:\n"), h->ar_name);
   13851                 :             : 
   13852                 :          12 :           elf_end (subelf);
   13853                 :             :         }
   13854                 :             : 
   13855                 :          18 :       printf ("\t%s\n", s->as_name);
   13856                 :             :     }
   13857                 :             : }
   13858                 :             : 
   13859                 :             : #include "debugpred.h"
        

Generated by: LCOV version 2.0-1