Home | History | Annotate | Download | only in tests
      1 /* Test program for dwelf_elf_gnu_debuglink, print name and crc.
      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 <errno.h>
     22 #include ELFUTILS_HEADER(dwelf)
     23 #include <stdio.h>
     24 #include <error.h>
     25 #include <string.h>
     26 #include <stdlib.h>
     27 #include <sys/types.h>
     28 #include <sys/stat.h>
     29 #include <fcntl.h>
     30 #include <unistd.h>
     31 
     32 int
     33 main (int argc, char *argv[])
     34 {
     35   if (argc < 2)
     36     error (EXIT_FAILURE, 0, "No input file given");
     37 
     38   elf_version (EV_CURRENT);
     39 
     40   for (int i = 1; i < argc; i++)
     41     {
     42       const char *file = argv[i];
     43       int fd = open (file, O_RDONLY);
     44       if (fd < 0)
     45 	error (EXIT_FAILURE, errno, "couldn't open file '%s'", file);
     46 
     47       Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
     48       if (elf == NULL)
     49 	error (EXIT_FAILURE, 0, "elf_begin failed for '%s': %s",
     50 	       file, elf_errmsg (-1));
     51 
     52       GElf_Word crc;
     53       const char *debug = dwelf_elf_gnu_debuglink (elf, &crc);
     54       if (debug == NULL)
     55 	printf ("%s: <no gnu_debuglink file>\n", file);
     56       else
     57 	printf ("%s: %s, crc: %" PRIx32 "\n", file, debug, crc);
     58 
     59       elf_end (elf);
     60       close (fd);
     61     }
     62 
     63   return 0;
     64 }
     65