Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 1998, 1999, 2000, 2002, 2005 Red Hat, Inc.
      2    This file is part of Red Hat elfutils.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 1998.
      4 
      5    Red Hat elfutils is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by the
      7    Free Software Foundation; version 2 of the License.
      8 
      9    Red Hat elfutils is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12    General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License along
     15    with Red Hat elfutils; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     17 
     18    Red Hat elfutils is an included package of the Open Invention Network.
     19    An included package of the Open Invention Network is a package for which
     20    Open Invention Network licensees cross-license their patents.  No patent
     21    license is granted, either expressly or impliedly, by designation as an
     22    included package.  Should you wish to participate in the Open Invention
     23    Network licensing program, please visit www.openinventionnetwork.com
     24    <http://www.openinventionnetwork.com>.  */
     25 
     26 #include <config.h>
     27 
     28 #include <errno.h>
     29 #include <fcntl.h>
     30 #include <gelf.h>
     31 #include <stdlib.h>
     32 #include <stdio.h>
     33 #include <string.h>
     34 
     35 int
     36 main (int argc, char *argv[])
     37 {
     38   Elf *elf;
     39   int fd;
     40   GElf_Ehdr ehdr;
     41   size_t strndx;
     42   Elf_Scn *scn;
     43 
     44   if (argc < 2)
     45     {
     46       puts ("missing parameter");
     47       exit (1);
     48     }
     49 
     50   fd = open (argv[1], O_RDONLY);
     51   if (fd == -1)
     52     {
     53       printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
     54       exit (1);
     55     }
     56 
     57   elf_version (EV_CURRENT);
     58 
     59   elf = elf_begin (fd, ELF_C_READ, NULL);
     60   if (elf == NULL)
     61     {
     62       printf ("cannot open ELF file: %s\n", elf_errmsg (-1));
     63       exit (1);
     64     }
     65 
     66   if (elf_kind (elf) != ELF_K_ELF)
     67     {
     68       printf ("\"%s\" is not an ELF file\n", argv[1]);
     69       exit (1);
     70     }
     71 
     72   if (gelf_getehdr (elf, &ehdr) == NULL)
     73     {
     74       printf ("cannot get the ELF header: %s\n", elf_errmsg (-1));
     75       exit (1);
     76     }
     77 
     78   strndx = ehdr.e_shstrndx;
     79 
     80   scn = NULL;
     81   while ((scn = elf_nextscn (elf, scn)) != NULL)
     82     {
     83       char *name = NULL;
     84       GElf_Shdr shdr;
     85 
     86       if (gelf_getshdr (scn, &shdr) != NULL)
     87 	name = elf_strptr (elf, strndx, (size_t) shdr.sh_name);
     88 
     89       printf ("section: `%s'\n", name);
     90     }
     91 
     92   if (elf_end (elf) != 0)
     93     {
     94       printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
     95       exit (1);
     96     }
     97 
     98   return 0;
     99 }
    100