LCOV - code coverage report
Current view: top level - elfutils/src - objdump.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 101 305 33.1 %
Date: 2013-03-08 Functions: 7 14 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 48 220 21.8 %

           Branch data     Line data    Source code
       1                 :            : /* Print information from ELF file in human-readable form.
       2                 :            :    Copyright (C) 2005, 2006, 2007, 2009, 2011, 2012 Red Hat, Inc.
       3                 :            :    This file is part of elfutils.
       4                 :            :    Written by Ulrich Drepper <drepper@redhat.com>, 2005.
       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 <error.h>
      25                 :            : #include <fcntl.h>
      26                 :            : #include <inttypes.h>
      27                 :            : #include <libintl.h>
      28                 :            : #include <locale.h>
      29                 :            : #include <mcheck.h>
      30                 :            : #include <stdbool.h>
      31                 :            : #include <stdio.h>
      32                 :            : #include <stdio_ext.h>
      33                 :            : #include <stdlib.h>
      34                 :            : #include <string.h>
      35                 :            : #include <unistd.h>
      36                 :            : #include <sys/param.h>
      37                 :            : 
      38                 :            : #include <system.h>
      39                 :            : #include "../libebl/libeblP.h"
      40                 :            : 
      41                 :            : 
      42                 :            : /* Name and version of program.  */
      43                 :            : static void print_version (FILE *stream, struct argp_state *state);
      44                 :            : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      45                 :            : 
      46                 :            : /* Bug report address.  */
      47                 :            : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      48                 :            : 
      49                 :            : 
      50                 :            : /* Definitions of arguments for argp functions.  */
      51                 :            : static const struct argp_option options[] =
      52                 :            : {
      53                 :            :   { NULL, 0, NULL, 0, N_("Mode selection:"), 0 },
      54                 :            :   { "reloc", 'r', NULL, 0, N_("Display relocation information."), 0 },
      55                 :            :   { "full-contents", 's', NULL, 0,
      56                 :            :     N_("Display the full contents of all sections requested"), 0 },
      57                 :            :   { "disassemble", 'd', NULL, 0,
      58                 :            :     N_("Display assembler code of executable sections"), 0 },
      59                 :            : 
      60                 :            :   { NULL, 0, NULL, 0, N_("Output content selection:"), 0 },
      61                 :            :   { "section", 'j', "NAME", 0,
      62                 :            :     N_("Only display information for section NAME."), 0 },
      63                 :            : 
      64                 :            :   { NULL, 0, NULL, 0, NULL, 0 }
      65                 :            : };
      66                 :            : 
      67                 :            : /* Short description of program.  */
      68                 :            : static const char doc[] = N_("\
      69                 :            : Show information from FILEs (a.out by default).");
      70                 :            : 
      71                 :            : /* Strings for arguments in help texts.  */
      72                 :            : static const char args_doc[] = N_("[FILE...]");
      73                 :            : 
      74                 :            : /* Prototype for option handler.  */
      75                 :            : static error_t parse_opt (int key, char *arg, struct argp_state *state);
      76                 :            : 
      77                 :            : /* Parser children.  */
      78                 :            : static struct argp_child argp_children[] =
      79                 :            :   {
      80                 :            :     { &color_argp, 0, N_("Output formatting"), 2 },
      81                 :            :     { NULL, 0, NULL, 0}
      82                 :            :   };
      83                 :            : 
      84                 :            : /* Data structure to communicate with argp functions.  */
      85                 :            : static const struct argp argp =
      86                 :            : {
      87                 :            :   options, parse_opt, args_doc, doc, argp_children, NULL, NULL
      88                 :            : };
      89                 :            : 
      90                 :            : 
      91                 :            : /* Print symbols in file named FNAME.  */
      92                 :            : static int process_file (const char *fname, bool more_than_one);
      93                 :            : 
      94                 :            : /* Handle content of archive.  */
      95                 :            : static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
      96                 :            :                       const char *suffix);
      97                 :            : 
      98                 :            : /* Handle ELF file.  */
      99                 :            : static int handle_elf (Elf *elf, const char *prefix, const char *fname,
     100                 :            :                        const char *suffix);
     101                 :            : 
     102                 :            : 
     103                 :            : #define INTERNAL_ERROR(fname) \
     104                 :            :   error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s-%s): %s"),      \
     105                 :            :          fname, __LINE__, PACKAGE_VERSION, __DATE__, elf_errmsg (-1))
     106                 :            : 
     107                 :            : 
     108                 :            : /* List of sections which should be used.  */
     109                 :            : static struct section_list
     110                 :            : {
     111                 :            :   bool is_name;
     112                 :            :   union
     113                 :            :   {
     114                 :            :     const char *name;
     115                 :            :     uint32_t scnndx;
     116                 :            :   };
     117                 :            :   struct section_list *next;
     118                 :            : } *section_list;
     119                 :            : 
     120                 :            : 
     121                 :            : /* If true print archive index.  */
     122                 :            : static bool print_relocs;
     123                 :            : 
     124                 :            : /* If true print full contents of requested sections.  */
     125                 :            : static bool print_full_content;
     126                 :            : 
     127                 :            : /* If true print disassembled output..  */
     128                 :            : static bool print_disasm;
     129                 :            : 
     130                 :            : 
     131                 :            : int
     132                 :          2 : main (int argc, char *argv[])
     133                 :            : {
     134                 :            :   /* Make memory leak detection possible.  */
     135                 :          2 :   mtrace ();
     136                 :            : 
     137                 :            :   /* We use no threads here which can interfere with handling a stream.  */
     138                 :          2 :   (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
     139                 :          2 :   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
     140                 :          2 :   (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
     141                 :            : 
     142                 :            :   /* Set locale.  */
     143                 :          2 :   (void) setlocale (LC_ALL, "");
     144                 :            : 
     145                 :            :   /* Make sure the message catalog can be found.  */
     146                 :          2 :   (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
     147                 :            : 
     148                 :            :   /* Initialize the message catalog.  */
     149                 :          2 :   (void) textdomain (PACKAGE_TARNAME);
     150                 :            : 
     151                 :            :   /* Parse and process arguments.  */
     152                 :            :   int remaining;
     153                 :          2 :   (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL);
     154                 :            : 
     155                 :            :   /* Tell the library which version we are expecting.  */
     156                 :          2 :   (void) elf_version (EV_CURRENT);
     157                 :            : 
     158                 :          2 :   int result = 0;
     159         [ -  + ]:          2 :   if (remaining == argc)
     160                 :            :     /* The user didn't specify a name so we use a.out.  */
     161                 :          0 :     result = process_file ("a.out", false);
     162                 :            :   else
     163                 :            :     {
     164                 :            :       /* Process all the remaining files.  */
     165                 :          2 :       const bool more_than_one = remaining + 1 < argc;
     166                 :            : 
     167                 :            :       do
     168                 :          2 :         result |= process_file (argv[remaining], more_than_one);
     169         [ -  + ]:          2 :       while (++remaining < argc);
     170                 :            :     }
     171                 :            : 
     172                 :            :   return result;
     173                 :            : }
     174                 :            : 
     175                 :            : 
     176                 :            : /* Print the version information.  */
     177                 :            : static void
     178                 :          0 : print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
     179                 :            : {
     180                 :          0 :   fprintf (stream, "objdump (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
     181                 :          0 :   fprintf (stream, gettext ("\
     182                 :            : Copyright (C) %s Red Hat, Inc.\n\
     183                 :            : This is free software; see the source for copying conditions.  There is NO\n\
     184                 :            : warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
     185                 :            : "), "2012");
     186                 :          0 :   fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
     187                 :          0 : }
     188                 :            : 
     189                 :            : 
     190                 :            : /* Handle program arguments.  */
     191                 :            : static error_t
     192                 :         12 : parse_opt (int key, char *arg,
     193                 :            :            struct argp_state *state __attribute__ ((unused)))
     194                 :            : {
     195                 :            :   /* True if any of the control options is set.  */
     196                 :            :   static bool any_control_option;
     197                 :            : 
     198   [ -  +  -  -  :         12 :   switch (key)
                   +  + ]
     199                 :            :     {
     200                 :            :     case 'j':
     201                 :            :       {
     202                 :          0 :         struct section_list *newp = xmalloc (sizeof (*newp));
     203                 :            :         char *endp;
     204                 :          0 :         newp->scnndx = strtoul (arg, &endp, 0);
     205         [ #  # ]:          0 :         if (*endp == 0)
     206                 :          0 :           newp->is_name = false;
     207                 :            :         else
     208                 :            :           {
     209                 :          0 :             newp->name = arg;
     210                 :          0 :             newp->is_name = true;
     211                 :            :           }
     212                 :          0 :         newp->next = section_list;
     213                 :          0 :         section_list = newp;
     214                 :            :       }
     215                 :          0 :       any_control_option = true;
     216                 :          0 :       break;
     217                 :            : 
     218                 :            :     case 'd':
     219                 :          2 :       print_disasm = true;
     220                 :          2 :       any_control_option = true;
     221                 :          2 :       break;
     222                 :            : 
     223                 :            :     case 'r':
     224                 :          0 :       print_relocs = true;
     225                 :          0 :       any_control_option = true;
     226                 :          0 :       break;
     227                 :            : 
     228                 :            :     case 's':
     229                 :          0 :       print_full_content = true;
     230                 :          0 :       any_control_option = true;
     231                 :          0 :       break;
     232                 :            : 
     233                 :            :     case ARGP_KEY_FINI:
     234         [ -  + ]:          2 :       if (! any_control_option)
     235                 :            :         {
     236                 :          0 :           fputs (gettext ("No operation specified.\n"), stderr);
     237                 :          0 :           argp_help (&argp, stderr, ARGP_HELP_SEE,
     238                 :            :                      program_invocation_short_name);
     239                 :         12 :           exit (EXIT_FAILURE);
     240                 :            :         }
     241                 :            : 
     242                 :            :     default:
     243                 :            :       return ARGP_ERR_UNKNOWN;
     244                 :            :     }
     245                 :            :   return 0;
     246                 :            : }
     247                 :            : 
     248                 :            : 
     249                 :            : /* Open the file and determine the type.  */
     250                 :            : static int
     251                 :          2 : process_file (const char *fname, bool more_than_one)
     252                 :            : {
     253                 :            :   /* Open the file.  */
     254                 :          2 :   int fd = open (fname, O_RDONLY);
     255         [ -  + ]:          2 :   if (fd == -1)
     256                 :            :     {
     257                 :          0 :       error (0, errno, gettext ("cannot open %s"), fname);
     258                 :          0 :       return 1;
     259                 :            :     }
     260                 :            : 
     261                 :            :   /* Now get the ELF descriptor.  */
     262                 :          2 :   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
     263         [ +  - ]:          2 :   if (elf != NULL)
     264                 :            :     {
     265         [ +  - ]:          2 :       if (elf_kind (elf) == ELF_K_ELF)
     266                 :            :         {
     267         [ +  - ]:          2 :           int result = handle_elf (elf, more_than_one ? "" : NULL,
     268                 :            :                                    fname, NULL);
     269                 :            : 
     270         [ -  + ]:          2 :           if (elf_end (elf) != 0)
     271                 :          0 :             INTERNAL_ERROR (fname);
     272                 :            : 
     273         [ -  + ]:          2 :           if (close (fd) != 0)
     274                 :          0 :             error (EXIT_FAILURE, errno, gettext ("while close `%s'"), fname);
     275                 :            : 
     276                 :            :           return result;
     277                 :            :         }
     278         [ #  # ]:          0 :       else if (elf_kind (elf) == ELF_K_AR)
     279                 :            :         {
     280                 :          0 :           int result = handle_ar (fd, elf, NULL, fname, NULL);
     281                 :            : 
     282         [ #  # ]:          0 :           if (elf_end (elf) != 0)
     283                 :          0 :             INTERNAL_ERROR (fname);
     284                 :            : 
     285         [ #  # ]:          0 :           if (close (fd) != 0)
     286                 :          0 :             error (EXIT_FAILURE, errno, gettext ("while close `%s'"), fname);
     287                 :            : 
     288                 :            :           return result;
     289                 :            :         }
     290                 :            : 
     291                 :            :       /* We cannot handle this type.  Close the descriptor anyway.  */
     292         [ #  # ]:          0 :       if (elf_end (elf) != 0)
     293                 :          0 :         INTERNAL_ERROR (fname);
     294                 :            :     }
     295                 :            : 
     296                 :          0 :   error (0, 0, gettext ("%s: File format not recognized"), fname);
     297                 :            : 
     298                 :          2 :   return 1;
     299                 :            : }
     300                 :            : 
     301                 :            : 
     302                 :            : static int
     303                 :          0 : handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
     304                 :            :            const char *suffix)
     305                 :            : {
     306                 :          0 :   size_t fname_len = strlen (fname) + 1;
     307         [ #  # ]:          0 :   size_t prefix_len = prefix != NULL ? strlen (prefix) : 0;
     308                 :          0 :   char new_prefix[prefix_len + fname_len + 2];
     309         [ #  # ]:          0 :   size_t suffix_len = suffix != NULL ? strlen (suffix) : 0;
     310                 :          0 :   char new_suffix[suffix_len + 2];
     311                 :            :   Elf *subelf;
     312                 :          0 :   Elf_Cmd cmd = ELF_C_READ_MMAP;
     313                 :          0 :   int result = 0;
     314                 :            : 
     315                 :          0 :   char *cp = new_prefix;
     316         [ #  # ]:          0 :   if (prefix != NULL)
     317                 :          0 :     cp = stpcpy (cp, prefix);
     318                 :          0 :   cp = stpcpy (cp, fname);
     319                 :          0 :   stpcpy (cp, "[");
     320                 :            : 
     321                 :          0 :   cp = new_suffix;
     322         [ #  # ]:          0 :   if (suffix != NULL)
     323                 :          0 :     cp = stpcpy (cp, suffix);
     324                 :          0 :   stpcpy (cp, "]");
     325                 :            : 
     326                 :            :   /* Process all the files contained in the archive.  */
     327         [ #  # ]:          0 :   while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
     328                 :            :     {
     329                 :            :       /* The the header for this element.  */
     330                 :          0 :       Elf_Arhdr *arhdr = elf_getarhdr (subelf);
     331                 :            : 
     332                 :            :       /* Skip over the index entries.  */
     333 [ #  # ][ #  # ]:          0 :       if (strcmp (arhdr->ar_name, "/") != 0
     334 [ #  # ][ #  # ]:          0 :           && strcmp (arhdr->ar_name, "//") != 0)
                 [ #  # ]
     335                 :            :         {
     336         [ #  # ]:          0 :           if (elf_kind (subelf) == ELF_K_ELF)
     337                 :          0 :             result |= handle_elf (subelf, new_prefix, arhdr->ar_name,
     338                 :            :                                   new_suffix);
     339         [ #  # ]:          0 :           else if (elf_kind (subelf) == ELF_K_AR)
     340                 :          0 :             result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name,
     341                 :            :                                  new_suffix);
     342                 :            :           else
     343                 :            :             {
     344                 :          0 :               error (0, 0, gettext ("%s%s%s: file format not recognized"),
     345                 :            :                      new_prefix, arhdr->ar_name, new_suffix);
     346                 :          0 :               result = 1;
     347                 :            :             }
     348                 :            :         }
     349                 :            : 
     350                 :            :       /* Get next archive element.  */
     351                 :          0 :       cmd = elf_next (subelf);
     352         [ #  # ]:          0 :       if (elf_end (subelf) != 0)
     353                 :          0 :         INTERNAL_ERROR (fname);
     354                 :            :     }
     355                 :            : 
     356                 :          0 :   return result;
     357                 :            : }
     358                 :            : 
     359                 :            : 
     360                 :            : static void
     361                 :          0 : show_relocs_x (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *symdata,
     362                 :            :                Elf_Data *xndxdata, size_t symstrndx, size_t shstrndx,
     363                 :            :                GElf_Addr r_offset, GElf_Xword r_info, GElf_Sxword r_addend)
     364                 :            : {
     365                 :          0 :   int elfclass = gelf_getclass (ebl->elf);
     366                 :            :   char buf[128];
     367                 :            : 
     368         [ #  # ]:          0 :   printf ("%0*" PRIx64 " %-20s ",
     369                 :            :           elfclass == ELFCLASS32 ? 8 : 16, r_offset,
     370                 :            :           ebl_reloc_type_name (ebl, GELF_R_TYPE (r_info), buf, sizeof (buf)));
     371                 :            : 
     372                 :            :   Elf32_Word xndx;
     373                 :            :   GElf_Sym symmem;
     374                 :          0 :   GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata, GELF_R_SYM (r_info),
     375                 :            :                                     &symmem, &xndx);
     376                 :            : 
     377         [ #  # ]:          0 :   if (sym == NULL)
     378                 :          0 :     printf ("<%s %ld>",
     379                 :            :             gettext ("INVALID SYMBOL"), (long int) GELF_R_SYM (r_info));
     380         [ #  # ]:          0 :   else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
     381                 :          0 :     printf ("%s",
     382                 :          0 :             elf_strptr (ebl->elf, symstrndx, sym->st_name));
     383                 :            :   else
     384                 :            :     {
     385                 :            :       GElf_Shdr destshdr_mem;
     386                 :            :       GElf_Shdr *destshdr;
     387         [ #  # ]:          0 :       destshdr = gelf_getshdr (elf_getscn (ebl->elf,
     388                 :          0 :                                            sym->st_shndx == SHN_XINDEX
     389                 :            :                                            ? xndx : sym->st_shndx),
     390                 :            :                                &destshdr_mem);
     391                 :            : 
     392         [ #  # ]:          0 :       if (shdr == NULL)
     393         [ #  # ]:          0 :         printf ("<%s %ld>",
     394                 :            :                 gettext ("INVALID SECTION"),
     395                 :          0 :                 (long int) (sym->st_shndx == SHN_XINDEX
     396                 :            :                             ? xndx : sym->st_shndx));
     397                 :            :       else
     398                 :          0 :         printf ("%s",
     399                 :          0 :                 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
     400                 :            :     }
     401                 :            : 
     402         [ #  # ]:          0 :   if (r_addend != 0)
     403                 :            :     {
     404                 :          0 :       char sign = '+';
     405         [ #  # ]:          0 :       if (r_addend < 0)
     406                 :            :         {
     407                 :          0 :           sign = '-';
     408                 :          0 :           r_addend = -r_addend;
     409                 :            :         }
     410                 :          0 :       printf ("%c%#" PRIx64, sign, r_addend);
     411                 :            :     }
     412                 :            :   putchar ('\n');
     413                 :          0 : }
     414                 :            : 
     415                 :            : 
     416                 :            : static void
     417                 :          0 : show_relocs_rel (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *data,
     418                 :            :                  Elf_Data *symdata, Elf_Data *xndxdata, size_t symstrndx,
     419                 :            :                  size_t shstrndx)
     420                 :            : {
     421                 :          0 :   int nentries = shdr->sh_size / shdr->sh_entsize;
     422                 :            : 
     423         [ #  # ]:          0 :   for (int cnt = 0; cnt < nentries; ++cnt)
     424                 :            :     {
     425                 :            :       GElf_Rel relmem;
     426                 :            :       GElf_Rel *rel;
     427                 :            : 
     428                 :          0 :       rel = gelf_getrel (data, cnt, &relmem);
     429         [ #  # ]:          0 :       if (rel != NULL)
     430                 :          0 :         show_relocs_x (ebl, shdr, symdata, xndxdata, symstrndx, shstrndx,
     431                 :            :                        rel->r_offset, rel->r_info, 0);
     432                 :            :     }
     433                 :          0 : }
     434                 :            : 
     435                 :            : 
     436                 :            : static void
     437                 :          0 : show_relocs_rela (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *data,
     438                 :            :                   Elf_Data *symdata, Elf_Data *xndxdata, size_t symstrndx,
     439                 :            :                   size_t shstrndx)
     440                 :            : {
     441                 :          0 :   int nentries = shdr->sh_size / shdr->sh_entsize;
     442                 :            : 
     443         [ #  # ]:          0 :   for (int cnt = 0; cnt < nentries; ++cnt)
     444                 :            :     {
     445                 :            :       GElf_Rela relmem;
     446                 :            :       GElf_Rela *rel;
     447                 :            : 
     448                 :          0 :       rel = gelf_getrela (data, cnt, &relmem);
     449         [ #  # ]:          0 :       if (rel != NULL)
     450                 :          0 :         show_relocs_x (ebl, shdr, symdata, xndxdata, symstrndx, shstrndx,
     451                 :            :                        rel->r_offset, rel->r_info, rel->r_addend);
     452                 :            :     }
     453                 :          0 : }
     454                 :            : 
     455                 :            : 
     456                 :            : static bool
     457                 :          2 : section_match (Elf *elf, uint32_t scnndx, GElf_Shdr *shdr, size_t shstrndx)
     458                 :            : {
     459         [ -  + ]:          2 :   if (section_list == NULL)
     460                 :            :     return true;
     461                 :            : 
     462                 :            :   struct section_list *runp = section_list;
     463                 :            : 
     464                 :            :   do
     465                 :            :     {
     466         [ #  # ]:          0 :       if (runp->is_name)
     467                 :            :         {
     468         [ #  # ]:          0 :           if (strcmp (runp->name,
     469                 :            :                       elf_strptr (elf, shstrndx, shdr->sh_name)) == 0)
     470                 :            :             return true;
     471                 :            :         }
     472                 :            :       else
     473                 :            :         {
     474         [ #  # ]:          0 :           if (runp->scnndx == scnndx)
     475                 :            :             return true;
     476                 :            :         }
     477                 :            : 
     478                 :          0 :       runp = runp->next;
     479                 :            :     }
     480         [ #  # ]:          2 :   while (runp != NULL);
     481                 :            : 
     482                 :            :   return false;
     483                 :            : }
     484                 :            : 
     485                 :            : 
     486                 :            : static int
     487                 :          0 : show_relocs (Ebl *ebl, const char *fname, uint32_t shstrndx)
     488                 :            : {
     489                 :          0 :   int elfclass = gelf_getclass (ebl->elf);
     490                 :            : 
     491                 :          0 :   Elf_Scn *scn = NULL;
     492         [ #  # ]:          0 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
     493                 :            :     {
     494                 :            :       GElf_Shdr shdr_mem;
     495                 :          0 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     496                 :            : 
     497         [ #  # ]:          0 :       if (shdr == NULL)
     498                 :          0 :         INTERNAL_ERROR (fname);
     499                 :            : 
     500         [ #  # ]:          0 :       if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
     501                 :            :         {
     502         [ #  # ]:          0 :           if  (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx))
     503                 :          0 :             continue;
     504                 :            : 
     505                 :            :           GElf_Shdr destshdr_mem;
     506                 :          0 :           GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf,
     507                 :          0 :                                                           shdr->sh_info),
     508                 :            :                                               &destshdr_mem);
     509                 :            : 
     510         [ #  # ]:          0 :           printf (gettext ("\nRELOCATION RECORDS FOR [%s]:\n"
     511                 :            :                            "%-*s TYPE                 VALUE\n"),
     512                 :          0 :                   elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
     513                 :            :                   elfclass == ELFCLASS32 ? 8 : 16, gettext ("OFFSET"));
     514                 :            : 
     515                 :            :           /* Get the data of the section.  */
     516                 :          0 :           Elf_Data *data = elf_getdata (scn, NULL);
     517         [ #  # ]:          0 :           if (data == NULL)
     518                 :          0 :             continue;
     519                 :            : 
     520                 :            :           /* Get the symbol table information.  */
     521                 :          0 :           Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
     522                 :            :           GElf_Shdr symshdr_mem;
     523                 :          0 :           GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
     524                 :          0 :           Elf_Data *symdata = elf_getdata (symscn, NULL);
     525                 :            : 
     526                 :            :           /* Search for the optional extended section index table.  */
     527                 :          0 :           Elf_Data *xndxdata = NULL;
     528                 :          0 :           Elf_Scn *xndxscn = NULL;
     529         [ #  # ]:          0 :           while ((xndxscn = elf_nextscn (ebl->elf, xndxscn)) != NULL)
     530                 :            :             {
     531                 :            :               GElf_Shdr xndxshdr_mem;
     532                 :            :               GElf_Shdr *xndxshdr;
     533                 :            : 
     534                 :          0 :               xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem);
     535 [ #  # ][ #  # ]:          0 :               if (xndxshdr != NULL && xndxshdr->sh_type == SHT_SYMTAB_SHNDX
     536         [ #  # ]:          0 :                   && xndxshdr->sh_link == elf_ndxscn (symscn))
     537                 :            :                 {
     538                 :            :                   /* Found it.  */
     539                 :          0 :                   xndxdata = elf_getdata (xndxscn, NULL);
     540                 :            :                   break;
     541                 :            :                 }
     542                 :            :             }
     543                 :            : 
     544         [ #  # ]:          0 :           if (shdr->sh_type == SHT_REL)
     545                 :          0 :             show_relocs_rel (ebl, shdr, data, symdata, xndxdata,
     546                 :          0 :                              symshdr->sh_link, shstrndx);
     547                 :            :           else
     548                 :          0 :             show_relocs_rela (ebl, shdr, data, symdata, xndxdata,
     549                 :          0 :                               symshdr->sh_link, shstrndx);
     550                 :            : 
     551                 :            :           putchar ('\n');
     552                 :            :         }
     553                 :            :     }
     554                 :            : 
     555                 :          0 :   return 0;
     556                 :            : }
     557                 :            : 
     558                 :            : 
     559                 :            : static int
     560                 :          0 : show_full_content (Ebl *ebl, const char *fname, uint32_t shstrndx)
     561                 :            : {
     562                 :            :   Elf_Scn *scn = NULL;
     563         [ #  # ]:          0 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
     564                 :            :     {
     565                 :            :       GElf_Shdr shdr_mem;
     566                 :          0 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     567                 :            : 
     568         [ #  # ]:          0 :       if (shdr == NULL)
     569                 :          0 :         INTERNAL_ERROR (fname);
     570                 :            : 
     571 [ #  # ][ #  # ]:          0 :       if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size > 0)
     572                 :            :         {
     573         [ #  # ]:          0 :           if  (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx))
     574                 :          0 :             continue;
     575                 :            : 
     576                 :          0 :           printf (gettext ("Contents of section %s:\n"),
     577                 :          0 :                   elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
     578                 :            : 
     579                 :            :           /* Get the data of the section.  */
     580                 :          0 :           Elf_Data *data = elf_getdata (scn, NULL);
     581         [ #  # ]:          0 :           if (data == NULL)
     582                 :          0 :             continue;
     583                 :            : 
     584                 :          0 :           unsigned char *cp = data->d_buf;
     585                 :            :           size_t cnt;
     586         [ #  # ]:          0 :           for (cnt = 0; cnt + 16 < data->d_size; cp += 16, cnt += 16)
     587                 :            :             {
     588                 :          0 :               printf (" %04zx ", cnt);
     589                 :            : 
     590         [ #  # ]:          0 :               for (size_t inner = 0; inner < 16; inner += 4)
     591                 :          0 :                 printf ("%02hhx%02hhx%02hhx%02hhx ",
     592                 :          0 :                         cp[inner], cp[inner + 1], cp[inner + 2],
     593                 :          0 :                         cp[inner + 3]);
     594                 :          0 :               fputc_unlocked (' ', stdout);
     595                 :            : 
     596         [ #  # ]:          0 :               for (size_t inner = 0; inner < 16; ++inner)
     597 [ #  # ][ #  # ]:          0 :                 fputc_unlocked (isascii (cp[inner]) && isprint (cp[inner])
     598                 :            :                                 ? cp[inner] : '.', stdout);
     599                 :          0 :               fputc_unlocked ('\n', stdout);
     600                 :            :             }
     601                 :            : 
     602                 :          0 :           printf (" %04zx ", cnt);
     603                 :            : 
     604                 :          0 :           size_t remaining = data->d_size - cnt;
     605                 :            :           size_t inner;
     606         [ #  # ]:          0 :           for (inner = 0; inner + 4 <= remaining; inner += 4)
     607                 :          0 :             printf ("%02hhx%02hhx%02hhx%02hhx ",
     608                 :          0 :                     cp[inner], cp[inner + 1], cp[inner + 2], cp[inner + 3]);
     609                 :            : 
     610         [ #  # ]:          0 :           for (; inner < remaining; ++inner)
     611                 :          0 :             printf ("%02hhx", cp[inner]);
     612                 :            : 
     613         [ #  # ]:          0 :           for (inner = 2 * (16 - inner) + (16 - inner + 3) / 4 + 1; inner > 0;
     614                 :          0 :                --inner)
     615                 :          0 :             fputc_unlocked (' ', stdout);
     616                 :            : 
     617         [ #  # ]:          0 :           for (inner = 0; inner < remaining; ++inner)
     618 [ #  # ][ #  # ]:          0 :             fputc_unlocked (isascii (cp[inner]) && isprint (cp[inner])
     619                 :            :                             ? cp[inner] : '.', stdout);
     620                 :          0 :           fputc_unlocked ('\n', stdout);
     621                 :            : 
     622                 :          0 :           fputc_unlocked ('\n', stdout);
     623                 :            :         }
     624                 :            :     }
     625                 :            : 
     626                 :          0 :   return 0;
     627                 :            : }
     628                 :            : 
     629                 :            : 
     630                 :            : struct disasm_info
     631                 :            : {
     632                 :            :   GElf_Addr addr;
     633                 :            :   const uint8_t *cur;
     634                 :            :   const uint8_t *last_end;
     635                 :            :   const char *address_color;
     636                 :            :   const char *bytes_color;
     637                 :            : };
     638                 :            : 
     639                 :            : 
     640                 :            : // XXX This is not the preferred output for all architectures.  Needs
     641                 :            : // XXX customization, too.
     642                 :            : static int
     643                 :      18944 : disasm_output (char *buf, size_t buflen, void *arg)
     644                 :            : {
     645                 :      18944 :   struct disasm_info *info = (struct disasm_info *) arg;
     646                 :            : 
     647         [ -  + ]:      18944 :   if (info->address_color != NULL)
     648                 :          0 :     printf ("%s%8" PRIx64 "%s:   ",
     649                 :            :             info->address_color, (uint64_t) info->addr, color_off);
     650                 :            :   else
     651                 :      18944 :     printf ("%8" PRIx64 ":   ", (uint64_t) info->addr);
     652                 :            : 
     653         [ -  + ]:      18944 :   if (info->bytes_color != NULL)
     654                 :      18944 :     fputs_unlocked (info->bytes_color, stdout);
     655                 :            :   size_t cnt;
     656         [ +  + ]:     103992 :   for (cnt = 0; cnt < (size_t) MIN (info->cur - info->last_end, 8); ++cnt)
     657                 :      85048 :     printf (" %02" PRIx8, info->last_end[cnt]);
     658         [ -  + ]:      18944 :   if (info->bytes_color != NULL)
     659                 :          0 :     fputs_unlocked (color_off, stdout);
     660                 :            : 
     661                 :      18944 :   printf ("%*s %.*s\n",
     662                 :      18944 :           (int) (8 - cnt) * 3 + 1, "", (int) buflen, buf);
     663                 :            : 
     664                 :      18944 :   info->addr += cnt;
     665                 :            : 
     666                 :            :   /* We limit the number of bytes printed before the mnemonic to 8.
     667                 :            :      Print the rest on a separate, following line.  */
     668         [ +  + ]:      18944 :   if (info->cur - info->last_end > 8)
     669                 :            :     {
     670         [ -  + ]:        452 :       if (info->address_color != NULL)
     671                 :          0 :         printf ("%s%8" PRIx64 "%s:   ",
     672                 :            :                 info->address_color, (uint64_t) info->addr, color_off);
     673                 :            :       else
     674                 :        452 :         printf ("%8" PRIx64 ":   ", (uint64_t) info->addr);
     675                 :            : 
     676         [ -  + ]:        452 :       if (info->bytes_color != NULL)
     677                 :        452 :         fputs_unlocked (info->bytes_color, stdout);
     678         [ +  + ]:       1292 :       for (; cnt < (size_t) (info->cur - info->last_end); ++cnt)
     679                 :        840 :         printf (" %02" PRIx8, info->last_end[cnt]);
     680         [ -  + ]:        452 :       if (info->bytes_color != NULL)
     681                 :          0 :         fputs_unlocked (color_off, stdout);
     682                 :            :       putchar_unlocked ('\n');
     683                 :        452 :       info->addr += info->cur - info->last_end - 8;
     684                 :            :     }
     685                 :            : 
     686                 :      18944 :   info->last_end = info->cur;
     687                 :            : 
     688                 :      18944 :   return 0;
     689                 :            : }
     690                 :            : 
     691                 :            : 
     692                 :            : static int
     693                 :          2 : show_disasm (Ebl *ebl, const char *fname, uint32_t shstrndx)
     694                 :            : {
     695                 :          2 :   DisasmCtx_t *ctx = disasm_begin (ebl, ebl->elf, NULL /* XXX TODO */);
     696         [ +  - ]:          2 :   if (ctx == NULL)
     697                 :          0 :     error (EXIT_FAILURE, 0, gettext ("cannot disassemble"));
     698                 :            : 
     699                 :            :   Elf_Scn *scn = NULL;
     700         [ +  + ]:         14 :   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
     701                 :            :     {
     702                 :            :       GElf_Shdr shdr_mem;
     703                 :         12 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     704                 :            : 
     705         [ -  + ]:         12 :       if (shdr == NULL)
     706                 :          0 :         INTERNAL_ERROR (fname);
     707                 :            : 
     708 [ +  + ][ +  + ]:         12 :       if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size > 0
     709         [ +  - ]:          2 :           && (shdr->sh_flags & SHF_EXECINSTR) != 0)
     710                 :            :         {
     711         [ -  + ]:          2 :           if  (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx))
     712                 :          0 :             continue;
     713                 :            : 
     714                 :          2 :           Elf_Data *data = elf_getdata (scn, NULL);
     715         [ -  + ]:          2 :           if (data == NULL)
     716                 :          0 :             continue;
     717                 :            : 
     718                 :          2 :           printf ("Disassembly of section %s:\n\n",
     719                 :          2 :                   elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
     720                 :            : 
     721                 :            :           struct disasm_info info;
     722                 :          2 :           info.addr = shdr->sh_addr;
     723                 :          2 :           info.last_end = info.cur = data->d_buf;
     724                 :            :           char *fmt;
     725         [ -  + ]:          2 :           if (color_mode)
     726                 :            :             {
     727                 :          0 :               info.address_color = color_address;
     728                 :          0 :               info.bytes_color = color_bytes;
     729                 :            : 
     730 [ #  # ][ #  # ]:          0 :               if (asprintf (&fmt, "%s%%7m %s%%.1o,%s%%.2o,%s%%.3o%%34a %s%%l",
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     731                 :          0 :                             color_mnemonic ?: "",
     732                 :          0 :                             color_operand1 ?: "",
     733                 :          0 :                             color_operand2 ?: "",
     734                 :          0 :                             color_operand3 ?: "",
     735                 :          0 :                             color_label ?: "") < 0)
     736                 :          0 :                 error (EXIT_FAILURE, errno, _("cannot allocate memory"));
     737                 :            :             }
     738                 :            :           else
     739                 :            :             {
     740                 :          2 :               info.address_color = info.bytes_color = NULL;
     741                 :            : 
     742                 :          2 :               fmt = "%7m %.1o,%.2o,%.3o%34a %l";
     743                 :            :             }
     744                 :            : 
     745                 :          2 :           disasm_cb (ctx, &info.cur, info.cur + data->d_size, info.addr,
     746                 :            :                      fmt, disasm_output, &info, NULL /* XXX */);
     747                 :            : 
     748         [ -  + ]:          2 :           if (color_mode)
     749                 :         12 :             free (fmt);
     750                 :            :         }
     751                 :            :     }
     752                 :            : 
     753                 :          2 :   (void) disasm_end (ctx);
     754                 :            : 
     755                 :          2 :   return 0;
     756                 :            : }
     757                 :            : 
     758                 :            : 
     759                 :            : static int
     760                 :          2 : handle_elf (Elf *elf, const char *prefix, const char *fname,
     761                 :            :             const char *suffix)
     762                 :            : {
     763                 :            : 
     764                 :            :   /* Get the backend for this object file type.  */
     765                 :          2 :   Ebl *ebl = ebl_openbackend (elf);
     766                 :            : 
     767         [ +  + ]:          4 :   printf ("%s: elf%d-%s\n\n",
     768                 :          2 :           fname, gelf_getclass (elf) == ELFCLASS32 ? 32 : 64,
     769                 :            :           ebl_backend_name (ebl));
     770                 :            : 
     771                 :            :   /* Create the full name of the file.  */
     772         [ -  + ]:          2 :   size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
     773         [ -  + ]:          2 :   size_t suffix_len = suffix == NULL ? 0 : strlen (suffix);
     774                 :          2 :   size_t fname_len = strlen (fname) + 1;
     775                 :          2 :   char fullname[prefix_len + 1 + fname_len + suffix_len];
     776                 :          2 :   char *cp = fullname;
     777         [ -  + ]:          2 :   if (prefix != NULL)
     778                 :          0 :     cp = mempcpy (cp, prefix, prefix_len);
     779                 :          2 :   cp = mempcpy (cp, fname, fname_len);
     780         [ -  + ]:          2 :   if (suffix != NULL)
     781                 :          0 :     memcpy (cp - 1, suffix, suffix_len + 1);
     782                 :            : 
     783                 :            :   /* Get the section header string table index.  */
     784                 :            :   size_t shstrndx;
     785         [ -  + ]:          2 :   if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
     786                 :            :     error (EXIT_FAILURE, 0,
     787                 :          0 :            gettext ("cannot get section header string table index"));
     788                 :            : 
     789                 :          2 :   int result = 0;
     790         [ +  - ]:          2 :   if (print_disasm)
     791                 :          2 :     result = show_disasm (ebl, fullname, shstrndx);
     792 [ -  + ][ #  # ]:          2 :   if (print_relocs && !print_disasm)
     793                 :          0 :     result = show_relocs (ebl, fullname, shstrndx);
     794         [ -  + ]:          2 :   if (print_full_content)
     795                 :          0 :     result = show_full_content (ebl, fullname, shstrndx);
     796                 :            : 
     797                 :            :   /* Close the ELF backend library descriptor.  */
     798                 :          2 :   ebl_closebackend (ebl);
     799                 :            : 
     800                 :          2 :   return result;
     801                 :            : }
     802                 :            : 
     803                 :            : 
     804                 :          0 : #include "debugpred.h"

Generated by: LCOV version 1.9