1 /* Test program for elf_strptr function.
2    Copyright (C) 2015 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 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include ELFUTILS_HEADER(elf)
31 #include <gelf.h>
32 
33 int
main(int argc,char * argv[])34 main (int argc, char *argv[])
35 {
36   if (argc != 2)
37     {
38       printf ("No ELF file given as argument");
39       exit (1);
40     }
41 
42   const char *fname = argv[1];
43 
44   // Initialize libelf.
45   elf_version (EV_CURRENT);
46 
47   /* Read the ELF from disk now.  */
48   int fd = open (fname, O_RDONLY);
49   if (fd == -1)
50     {
51       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
52       exit (1);
53     }
54 
55   Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
56   if (elf == NULL)
57     {
58       printf ("cannot create ELF descriptor read-only: %s\n", elf_errmsg (-1));
59       exit (1);
60     }
61 
62   size_t ndx;
63   if (elf_getshdrstrndx (elf, &ndx) != 0)
64     {
65       printf ("cannot get section header table index: %s\n", elf_errmsg (-1));
66       exit (1);
67     }
68 
69   if (ndx == SHN_UNDEF)
70     {
71       printf ("ELF file `%s' doesn't have a section header table index", fname);
72       exit (1);
73     }
74 
75   Elf_Scn *scn = elf_getscn (elf, ndx);
76   if (scn == NULL)
77     {
78       printf ("Couldn't get section %zd: %s\n", ndx, elf_errmsg (-1));
79       exit (1);
80     }
81 
82   void print_strings (void)
83   {
84     GElf_Shdr shdr_mem;
85     GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
86 
87     printf ("Strings in section %zd (%s):\n", ndx,
88 	    ((shdr->sh_flags & SHF_COMPRESSED) != 0
89 	     ? "compressed" : "uncompressed"));
90 
91     size_t off = 0;
92     const char *str = elf_strptr (elf, ndx, off);
93     while (str != NULL)
94       {
95 	printf ("[%zx] '%s'\n", off, str);
96 	off += strlen (str) + 1;
97 	str = elf_strptr (elf, ndx, off);
98       }
99   }
100 
101   if (elf_compress (scn, ELFCOMPRESS_ZLIB, 0) < 0)
102     {
103       printf ("Couldn't compress section %zd: %s\n", ndx, elf_errmsg (-1));
104       exit (1);
105     }
106   print_strings ();
107 
108   if (elf_compress (scn, 0, 0) < 0)
109     {
110       printf ("Couldn't decompress section %zd: %s\n", ndx, elf_errmsg (-1));
111       exit (1);
112     }
113   print_strings ();
114 
115   if (elf_end (elf) != 0)
116     {
117       printf ("failure in elf_end: %s\n", elf_errmsg (-1));
118       exit (1);
119     }
120 
121   close (fd);
122 
123   return 0;
124 }
125