• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1998, 1999, 2000, 2002 Red Hat, Inc.
2    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
3 
4    This program is Open Source software; you can redistribute it and/or
5    modify it under the terms of the Open Software License version 1.0 as
6    published by the Open Source Initiative.
7 
8    You should have received a copy of the Open Software License along
9    with this program; if not, you may obtain a copy of the Open Software
10    License version 1.0 from http://www.opensource.org/licenses/osl.php or
11    by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12    3001 King Ranch Road, Ukiah, CA 95482.   */
13 
14 #include <config.h>
15 
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <gelf.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 int
main(int argc,char * argv[])24 main (int argc, char *argv[])
25 {
26   Elf *elf;
27   int fd;
28   GElf_Ehdr ehdr;
29   size_t strndx;
30   Elf_Scn *scn;
31 
32   fd = open (argv[1], O_RDONLY);
33   if (fd == -1)
34     {
35       printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
36       exit (1);
37     }
38 
39   elf_version (EV_CURRENT);
40 
41   elf = elf_begin (fd, ELF_C_READ, NULL);
42   if (elf == NULL)
43     {
44       printf ("cannot open ELF file: %s\n", elf_errmsg (-1));
45       exit (1);
46     }
47 
48   if (elf_kind (elf) != ELF_K_ELF)
49     {
50       printf ("\"%s\" is not an ELF file\n", argv[1]);
51       exit (1);
52     }
53 
54   if (gelf_getehdr (elf, &ehdr) == NULL)
55     {
56       printf ("cannot get the ELF header: %s\n", elf_errmsg (-1));
57       exit (1);
58     }
59 
60   strndx = ehdr.e_shstrndx;
61 
62   scn = NULL;
63   while ((scn = elf_nextscn (elf, scn)) != NULL)
64     {
65       char *name = NULL;
66       GElf_Shdr shdr;
67 
68       if (gelf_getshdr (scn, &shdr) != NULL)
69 	name = elf_strptr (elf, strndx, (size_t) shdr.sh_name);
70 
71       printf ("section: `%s'\n", name);
72     }
73 
74   if (elf_end (elf) != 0)
75     {
76       printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
77       exit (1);
78     }
79 
80   return 0;
81 }
82