1 /* Test program for elf_strptr function.
2 Copyright (C) 2015 Red Hat, Inc.
3 This file is part of elfutils.
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.
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.
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/>. */
30 #include ELFUTILS_HEADER(elf)
34 main (int argc, char *argv[])
38 printf ("No ELF file given as argument");
42 const char *fname = argv[1];
45 elf_version (EV_CURRENT);
47 /* Read the ELF from disk now. */
48 int fd = open (fname, O_RDONLY);
51 printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
55 Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
58 printf ("cannot create ELF descriptor read-only: %s\n", elf_errmsg (-1));
63 if (elf_getshdrstrndx (elf, &ndx) != 0)
65 printf ("cannot get section header table index: %s\n", elf_errmsg (-1));
71 printf ("ELF file `%s' doesn't have a section header table index", fname);
75 printf ("Strings in section %zd:\n", ndx);
78 const char *str = elf_strptr (elf, ndx, off);
81 printf ("[%zx] '%s'\n", off, str);
82 off += strlen (str) + 1;
83 str = elf_strptr (elf, ndx, off);
86 if (elf_end (elf) != 0)
88 printf ("failure in elf_end: %s\n", elf_errmsg (-1));