summaryrefslogtreecommitdiff
path: root/tests/zstrptr.c
blob: 6d8e19f7adc720b239aaea7530c5d2ca959b8f0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Test program for elf_strptr function.
   Copyright (C) 2015 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include ELFUTILS_HEADER(elf)
#include <gelf.h>

int
main (int argc, char *argv[])
{
  if (argc != 2)
    {
      printf ("No ELF file given as argument");
      exit (1);
    }

  const char *fname = argv[1];

  // Initialize libelf.
  elf_version (EV_CURRENT);

  /* Read the ELF from disk now.  */
  int fd = open (fname, O_RDONLY);
  if (fd == -1)
    {
      printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
      exit (1);
    }

  Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
  if (elf == NULL)
    {
      printf ("cannot create ELF descriptor read-only: %s\n", elf_errmsg (-1));
      exit (1);
    }

  size_t ndx;
  if (elf_getshdrstrndx (elf, &ndx) != 0)
    {
      printf ("cannot get section header table index: %s\n", elf_errmsg (-1));
      exit (1);
    }

  if (ndx == SHN_UNDEF)
    {
      printf ("ELF file `%s' doesn't have a section header table index", fname);
      exit (1);
    }

  Elf_Scn *scn = elf_getscn (elf, ndx);
  if (scn == NULL)
    {
      printf ("Couldn't get section %zd: %s\n", ndx, elf_errmsg (-1));
      exit (1);
    }

  void print_strings (void)
  {
    GElf_Shdr shdr_mem;
    GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);

    printf ("Strings in section %zd (%s):\n", ndx,
	    ((shdr->sh_flags & SHF_COMPRESSED) != 0
	     ? "compressed" : "uncompressed"));

    size_t off = 0;
    const char *str = elf_strptr (elf, ndx, off);
    while (str != NULL)
      {
	printf ("[%zx] '%s'\n", off, str);
	off += strlen (str) + 1;
	str = elf_strptr (elf, ndx, off);
      }
  }

  if (elf_compress (scn, ELFCOMPRESS_ZLIB, 0) < 0)
    {
      printf ("Couldn't compress section %zd: %s\n", ndx, elf_errmsg (-1));
      exit (1);
    }
  print_strings ();

  if (elf_compress (scn, 0, 0) < 0)
    {
      printf ("Couldn't decompress section %zd: %s\n", ndx, elf_errmsg (-1));
      exit (1);
    }
  print_strings ();

  if (elf_end (elf) != 0)
    {
      printf ("failure in elf_end: %s\n", elf_errmsg (-1));
      exit (1);
    }

  close (fd);

  return 0;
}