Home | History | Annotate | Download | only in tests
      1 /* Test program for dwelf_dwarf_gnu_debugaltlink, print name and build ID.
      2    Copyright (C) 2014 Red Hat, Inc.
      3    This file is part of elfutils.
      4 
      5    This file is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #include <config.h>
     19 #include <assert.h>
     20 #include <inttypes.h>
     21 #include <err.h>
     22 #include <errno.h>
     23 #include ELFUTILS_HEADER(dw)
     24 #include ELFUTILS_HEADER(dwelf)
     25 #include <stdio.h>
     26 #include <error.h>
     27 #include <string.h>
     28 #include <stdlib.h>
     29 #include <sys/types.h>
     30 #include <sys/stat.h>
     31 #include <fcntl.h>
     32 #include <unistd.h>
     33 
     34 int
     35 main (int argc, char *argv[])
     36 {
     37   if (argc < 2)
     38     error (EXIT_FAILURE, 0, "No input file given");
     39 
     40   elf_version (EV_CURRENT);
     41 
     42   for (int i = 1; i < argc; i++)
     43     {
     44       const char *file = argv[i];
     45       int fd = open (file, O_RDONLY);
     46       if (fd < 0)
     47 	error (EXIT_FAILURE, errno, "couldn't open file '%s'", file);
     48 
     49       Dwarf *dwarf = dwarf_begin (fd, DWARF_C_READ);
     50       if (dwarf == NULL)
     51 	{
     52 	  printf("%s: dwarf_begin failed: %s\n", file, dwarf_errmsg (-1));
     53 	  close (fd);
     54 	  continue;
     55 	}
     56 
     57       const char *name;
     58       const void *build_id;
     59       ssize_t ret = dwelf_dwarf_gnu_debugaltlink
     60 	(dwarf, &name, &build_id);
     61       switch (ret)
     62 	{
     63 	case 0:
     64 	  printf ("%s: <no .gnu_debugaltlink section>\n", file);
     65 	  break;
     66 	case -1:
     67 	  errx (1, "dwelf_dwarf_gnu_debugaltlink (%s): %s",
     68 		file, dwarf_errmsg (-1));
     69 	default:
     70 	  printf ("%s: %s, build ID: ", file, name);
     71 	  const unsigned char *p = build_id;
     72 	  const unsigned char *end = p + ret;
     73 	  while (p < end)
     74 	      printf("%02x", (unsigned)*p++);
     75 	  putchar('\n');
     76 	}
     77 
     78       dwarf_end (dwarf);
     79       close (fd);
     80     }
     81 
     82   return 0;
     83 }
     84