diff options
Diffstat (limited to 'tests/elfputzdata.c')
-rw-r--r-- | tests/elfputzdata.c | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/tests/elfputzdata.c b/tests/elfputzdata.c new file mode 100644 index 00000000..2ab512c1 --- /dev/null +++ b/tests/elfputzdata.c @@ -0,0 +1,200 @@ +/* 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 <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <inttypes.h> +#include <libelf.h> +#include <gelf.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +int +main (int argc, char *argv[]) +{ + int result = 0; + int cnt; + + if (argc < 3 + || (strcmp (argv[1], "elf") != 0 + && strcmp (argv[1], "gnu") != 0)) + { + printf ("Usage: (elf|gnu) files...\n"); + return -1; + } + + int gnu; + if (strcmp (argv[1], "gnu") == 0) + gnu = 1; + else + gnu = 0; + + elf_version (EV_CURRENT); + + for (cnt = 2; cnt < argc; ++cnt) + { + int fd = open (argv[cnt], O_RDONLY); + + Elf *elf = elf_begin (fd, ELF_C_READ, NULL); + if (elf == NULL) + { + printf ("%s not usable %s\n", argv[cnt], elf_errmsg (-1)); + result = 1; + close (fd); + continue; + } + + /* To get the section names. */ + size_t strndx; + elf_getshdrstrndx (elf, &strndx); + + Elf_Scn *scn = NULL; + while ((scn = elf_nextscn (elf, scn)) != NULL) + { + size_t idx = elf_ndxscn (scn); + GElf_Shdr mem; + GElf_Shdr *shdr = gelf_getshdr (scn, &mem); + const char *name = elf_strptr (elf, strndx, shdr->sh_name); + if (idx == strndx) + { + printf ("Not compressing section string table %zd\n", idx); + } + else if (shdr->sh_type == SHT_NOBITS + || (shdr->sh_flags & SHF_ALLOC) != 0) + { + printf ("Cannot compress %zd %s\n", idx, name); + } + else if ((shdr->sh_flags & SHF_COMPRESSED) != 0 + || strncmp (name, ".zdebug", strlen (".zdebug") == 0)) + { + printf ("Already compressed %zd %s\n", idx, name); + } + else + { + size_t orig_size = shdr->sh_size; + printf ("Lets compress %zd %s, size: %zd\n", + idx, name, shdr->sh_size); + Elf_Data *d = elf_getdata (scn, NULL); + if (d == NULL) + { + printf ("Couldn't get orig data for section %zd\n", idx); + return -1; + } + /* Make a copy so we can compare after + compression/decompression. */ + if (d->d_size != orig_size) + { + printf ("Unexpected data size for orig section %zd\n", idx); + return -1; + } + char *orig_buf = malloc (d->d_size); + if (orig_buf == NULL) + { + printf ("No memory to copy section %zd data\n", idx); + return -1; + } + memcpy (orig_buf, d->d_buf, orig_size); + + if (gnu) + { + if (elf_compress_gnu (scn, 1) == -1) + { + printf ("elf_compress_gnu failed for section %zd: %s\n", + idx, elf_errmsg (-1)); + return -1; + } + } + else + { + if (elf_compress (scn, ELFCOMPRESS_ZLIB) == -1) + { + printf ("elf_compress failed for section %zd: %s\n", + idx, elf_errmsg (-1)); + return -1; + } + } + GElf_Shdr newmem; + GElf_Shdr *newshdr = gelf_getshdr (scn, &newmem); + size_t new_size = newshdr->sh_size; + d = elf_getdata (scn, NULL); + // Don't check this, might depend on zlib implementation. + // fprintf (stderr, " new_size: %zd\n", new_size); + if (d->d_size != new_size) + { + printf ("Unexpected data size for compressed section %zd\n", + idx); + return -1; + } + if (new_size == orig_size + && memcmp (orig_buf, d->d_buf, orig_size) == 0) + { + printf ("section %zd didn't compress\n", idx); + return -1; + } + + if (gnu) + { + if (elf_compress_gnu (scn, 0) == -1) + { + printf ("elf_[un]compress_gnu failed for section %zd: %s\n", + idx, elf_errmsg (-1)); + return -1; + } + } + else + { + if (elf_compress (scn, 0) == -1) + { + printf ("elf_[un]compress failed for section %zd: %s\n", + idx, elf_errmsg (-1)); + return -1; + } + } + GElf_Shdr newermem; + GElf_Shdr *newershdr = gelf_getshdr (scn, &newermem); + size_t newer_size = newershdr->sh_size; + d = elf_getdata (scn, NULL); + // fprintf (stderr, " newer_size: %zd\n", newer_size); + if (d->d_size != newer_size) + { + printf ("Unexpected data size for compressed section %zd\n", + idx); + return -1; + } + if (newer_size != orig_size + && memcmp (orig_buf, d->d_buf, orig_size) != 0) + { + printf ("section %zd didn't correctly uncompress\n", idx); + return -1; + } + free (orig_buf); + } + } + + elf_end (elf); + close (fd); + } + + return result; +} |