diff options
author | Mark Wielaard <mjw@redhat.com> | 2015-12-30 01:39:29 +0100 |
---|---|---|
committer | Mark Wielaard <mjw@redhat.com> | 2016-01-06 14:27:10 +0100 |
commit | 519c13c9c7fc591609eed405a3e952707c60ec7e (patch) | |
tree | 00c3afe184607b9d62f6fd81553de14b90a09bc6 /tests/zstrptr.c | |
parent | f5013e83d5c8fcd9309c8976c3f1167d28cd07aa (diff) | |
download | elfutils-519c13c9c7fc591609eed405a3e952707c60ec7e.tar.gz |
libelf: Make elf_strptr index correctly into compressed section data.
elf_strptr indexes into the section data. This is defined as index into
the uncompressed data of the section. If the section is compressed make
sure the uncompressed data is available, but don't really decompress the
section header (elf_getdata will still return compressed data).
Signed-off-by: Mark Wielaard <mjw@redhat.com>
Diffstat (limited to 'tests/zstrptr.c')
-rw-r--r-- | tests/zstrptr.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/tests/zstrptr.c b/tests/zstrptr.c new file mode 100644 index 00000000..6d8e19f7 --- /dev/null +++ b/tests/zstrptr.c @@ -0,0 +1,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; +} |