summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMark Wielaard <[email protected]>2015-12-19 01:29:54 +0100
committerMark Wielaard <[email protected]>2016-01-06 14:27:10 +0100
commit272018bba1f253bae00b5ba280ad0e0f18c04006 (patch)
treef0b20b43f9caf6d193ae9c05f5583699f34bc59d /tests
parentb7105b40ccd73a8e6b7fce6c11d2088eb1298b33 (diff)
libelf: Add elf_compress and elf_compress_gnu.
Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r--tests/ChangeLog15
-rw-r--r--tests/Makefile.am10
-rw-r--r--tests/elfgetzdata.c113
-rw-r--r--tests/elfputzdata.c232
-rw-r--r--tests/msg_tst.c6
-rwxr-xr-xtests/run-elfgetzdata.sh214
-rwxr-xr-xtests/run-elfputzdata.sh340
7 files changed, 926 insertions, 4 deletions
diff --git a/tests/ChangeLog b/tests/ChangeLog
index d9b92d3b..25407c22 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,18 @@
+2015-10-21 Mark Wielaard <[email protected]>
+
+ * Makefile.am (check_PROGRAMS): Add elfgetzdata and elfputzdata.
+ (TESTS): Add run-elfgetzdata.sh and run-elfputzdata.sh.
+ (EXTRA_DIST: Likewise.
+ (elfgetzdata_LDADD): New variable.
+ (elfputzdata_LDADD): Likewise.
+ * elfgetzdata.c: New file.
+ * elfputzdata.c: Likewise.
+ * msg_tst.c: Handle ELF_E_ALREADY_COMPRESSED,
+ ELF_E_UNKNOWN_COMPRESSION_TYPE, ELF_E_COMPRESS_ERROR and
+ ELF_E_DECOMPRESS_ERROR.
+ * run-elfgetzdata.sh: New test.
+ * run-elfputzdata.sh: Likewise.
+
2015-10-28 Mark Wielaard <[email protected]>
* run-readelf-z.sh: New test.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8ebb687b..5131cd05 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -52,7 +52,8 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
backtrace-data backtrace-dwarf debuglink debugaltlink \
buildid deleted deleted-lib.so aggregate_size vdsosyms \
getsrc_die strptr newdata elfstrtab dwfl-proc-attach \
- elfshphehdr elfstrmerge dwelfgnucompressed elfgetchdr
+ elfshphehdr elfstrmerge dwelfgnucompressed elfgetchdr \
+ elfgetzdata elfputzdata
asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -121,7 +122,8 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \
run-linkmap-cut.sh run-aggregate-size.sh vdsosyms run-readelf-A.sh \
run-getsrc-die.sh run-strptr.sh newdata elfstrtab dwfl-proc-attach \
elfshphehdr run-lfs-symbols.sh run-dwelfgnucompressed.sh \
- run-elfgetchdr.sh
+ run-elfgetchdr.sh \
+ run-elfgetzdata.sh run-elfputzdata.sh
if !BIARCH
export ELFUTILS_DISABLE_BIARCH = 1
@@ -314,7 +316,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
run-dwelfgnucompressed.sh \
testfile-zgabi32.bz2 testfile-zgabi64.bz2 \
testfile-zgabi32be.bz2 testfile-zgabi64be.bz2 \
- run-elfgetchdr.sh
+ run-elfgetchdr.sh run-elfgetzdata.sh run-elfputzdata.sh
if USE_VALGRIND
valgrind_cmd='valgrind -q --leak-check=full --error-exitcode=1'
@@ -465,6 +467,8 @@ elfshphehdr_LDADD =$(libelf)
elfstrmerge_LDADD = $(libebl) $(libelf)
dwelfgnucompressed_LDADD = $(libelf) $(libdw)
elfgetchdr_LDADD = $(libelf) $(libdw)
+elfgetzdata_LDADD = $(libelf)
+elfputzdata_LDADD = $(libelf)
if GCOV
check: check-am coverage
diff --git a/tests/elfgetzdata.c b/tests/elfgetzdata.c
new file mode 100644
index 00000000..82afbe52
--- /dev/null
+++ b/tests/elfgetzdata.c
@@ -0,0 +1,113 @@
+/* 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 <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+
+int
+main (int argc, char *argv[])
+{
+ int result = 0;
+ int cnt;
+
+ if (argc < 3
+ || (strcmp (argv[1], "read") != 0
+ && strcmp (argv[1], "mmap") != 0))
+ {
+ printf ("Usage: (read|mmap) files...\n");
+ return -1;
+ }
+
+ bool mmap = strcmp (argv[1], "mmap") == 0;
+
+ elf_version (EV_CURRENT);
+
+ for (cnt = 2; cnt < argc; ++cnt)
+ {
+ int fd = open (argv[cnt], O_RDONLY);
+
+ Elf *elf = elf_begin (fd, mmap ? ELF_C_READ_MMAP : 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 ((shdr->sh_flags & SHF_COMPRESSED) != 0)
+ {
+ /* Real compressed section. */
+ if (elf_compress (scn, 0, 0) < 0)
+ {
+ printf ("elf_compress failed for section %zd: %s\n",
+ idx, elf_errmsg (-1));
+ return -1;
+ }
+ Elf_Data *d = elf_getdata (scn, NULL);
+ printf ("%zd: %s, ELF compressed, size: %zx\n",
+ idx, name, d->d_size);
+ }
+ else
+ {
+ /* Maybe an old GNU compressed .z section? */
+ if (name[0] == '.' && name[1] == 'z')
+ {
+ if (elf_compress_gnu (scn, 0, 0) < 0)
+ {
+ printf ("elf_compress_gnu failed for section %zd: %s\n",
+ idx, elf_errmsg (-1));
+ return -1;
+ }
+ Elf_Data *d = elf_getdata (scn, NULL);
+ printf ("%zd: %s, GNU compressed, size: %zx\n",
+ idx, name, d->d_size);
+ }
+ else
+ printf ("%zd: %s, NOT compressed\n", idx, name);
+ }
+ }
+
+ elf_end (elf);
+ close (fd);
+ }
+
+ return result;
+}
diff --git a/tests/elfputzdata.c b/tests/elfputzdata.c
new file mode 100644
index 00000000..65654f88
--- /dev/null
+++ b/tests/elfputzdata.c
@@ -0,0 +1,232 @@
+/* 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 <stdbool.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_size > 0 && orig_buf == NULL)
+ {
+ printf ("No memory to copy section %zd data\n", idx);
+ return -1;
+ }
+ if (orig_size > 0)
+ memcpy (orig_buf, d->d_buf, orig_size);
+
+ bool forced = false;
+ if (gnu)
+ {
+ int res = elf_compress_gnu (scn, 1, 0);
+ if (res == 0)
+ {
+ forced = true;
+ res = elf_compress_gnu (scn, 1, ELF_CHF_FORCE);
+ }
+ if (res < 0)
+ {
+ printf ("elf_compress_gnu%sfailed for section %zd: %s\n",
+ forced ? " (forced) " : " ",
+ idx, elf_errmsg (-1));
+ return -1;
+ }
+ }
+ else
+ {
+ int res = elf_compress (scn, ELFCOMPRESS_ZLIB, 0);
+ if (res == 0)
+ {
+ forced = true;
+ res = elf_compress (scn, ELFCOMPRESS_ZLIB, ELF_CHF_FORCE);
+ }
+ if (res < 0)
+ {
+ printf ("elf_compress%sfailed for section %zd: %s\n",
+ forced ? " (forced) " : " ",
+ 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 (forced && new_size < orig_size)
+ {
+ printf ("section %zd forced to compress, but size smaller\n",
+ idx);
+ return -1;
+ }
+
+ if (! forced && new_size >= orig_size)
+ {
+ printf ("section %zd compressed to bigger size\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, 0) < 0)
+ {
+ printf ("elf_[un]compress_gnu failed for section %zd: %s\n",
+ idx, elf_errmsg (-1));
+ return -1;
+ }
+ }
+ else
+ {
+ if (elf_compress (scn, 0, 0) < 0)
+ {
+ 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;
+}
diff --git a/tests/msg_tst.c b/tests/msg_tst.c
index 539c5ddb..7baea0a2 100644
--- a/tests/msg_tst.c
+++ b/tests/msg_tst.c
@@ -77,7 +77,11 @@ and core files" },
{ ELF_E_INVALID_OFFSET, "invalid offset" },
{ ELF_E_INVALID_SECTION_TYPE , "invalid section type" },
{ ELF_E_INVALID_SECTION_FLAGS , "invalid section flags" },
- { ELF_E_NOT_COMPRESSED, "section does not contain compressed data" }
+ { ELF_E_NOT_COMPRESSED, "section does not contain compressed data" },
+ { ELF_E_ALREADY_COMPRESSED, "section contains compressed data" },
+ { ELF_E_UNKNOWN_COMPRESSION_TYPE, "unknown compression type" },
+ { ELF_E_COMPRESS_ERROR, "cannot compress data" },
+ { ELF_E_DECOMPRESS_ERROR, "cannot decompress data" }
};
diff --git a/tests/run-elfgetzdata.sh b/tests/run-elfgetzdata.sh
new file mode 100755
index 00000000..e2df3081
--- /dev/null
+++ b/tests/run-elfgetzdata.sh
@@ -0,0 +1,214 @@
+#! /bin/sh
+# 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/>.
+
+. $srcdir/test-subr.sh
+
+# See run-elfgetchdr.sh for testfiles.
+
+testfiles testfile-zgnu64
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata read testfile-zgnu64 <<\EOF
+1: .text, NOT compressed
+2: .zdebug_aranges, GNU compressed, size: 60
+3: .zdebug_info, GNU compressed, size: aa
+4: .debug_abbrev, NOT compressed
+5: .zdebug_line, GNU compressed, size: 8d
+6: .shstrtab, NOT compressed
+7: .symtab, NOT compressed
+8: .strtab, NOT compressed
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata mmap testfile-zgnu64 <<\EOF
+1: .text, NOT compressed
+2: .zdebug_aranges, GNU compressed, size: 60
+3: .zdebug_info, GNU compressed, size: aa
+4: .debug_abbrev, NOT compressed
+5: .zdebug_line, GNU compressed, size: 8d
+6: .shstrtab, NOT compressed
+7: .symtab, NOT compressed
+8: .strtab, NOT compressed
+EOF
+
+testfiles testfile-zgnu64be
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata read testfile-zgnu64be <<\EOF
+1: .text, NOT compressed
+2: .eh_frame, NOT compressed
+3: .zdebug_aranges, GNU compressed, size: 60
+4: .zdebug_info, GNU compressed, size: 7e
+5: .debug_abbrev, NOT compressed
+6: .zdebug_line, GNU compressed, size: 8d
+7: .shstrtab, NOT compressed
+8: .symtab, NOT compressed
+9: .strtab, NOT compressed
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata mmap testfile-zgnu64be <<\EOF
+1: .text, NOT compressed
+2: .eh_frame, NOT compressed
+3: .zdebug_aranges, GNU compressed, size: 60
+4: .zdebug_info, GNU compressed, size: 7e
+5: .debug_abbrev, NOT compressed
+6: .zdebug_line, GNU compressed, size: 8d
+7: .shstrtab, NOT compressed
+8: .symtab, NOT compressed
+9: .strtab, NOT compressed
+EOF
+
+testfiles testfile-zgabi64
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata read testfile-zgabi64 <<\EOF
+1: .text, NOT compressed
+2: .debug_aranges, ELF compressed, size: 60
+3: .debug_info, ELF compressed, size: aa
+4: .debug_abbrev, NOT compressed
+5: .debug_line, ELF compressed, size: 8d
+6: .shstrtab, NOT compressed
+7: .symtab, NOT compressed
+8: .strtab, NOT compressed
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata mmap testfile-zgabi64 <<\EOF
+1: .text, NOT compressed
+2: .debug_aranges, ELF compressed, size: 60
+3: .debug_info, ELF compressed, size: aa
+4: .debug_abbrev, NOT compressed
+5: .debug_line, ELF compressed, size: 8d
+6: .shstrtab, NOT compressed
+7: .symtab, NOT compressed
+8: .strtab, NOT compressed
+EOF
+
+testfiles testfile-zgabi64be
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata read testfile-zgabi64be <<\EOF
+1: .text, NOT compressed
+2: .eh_frame, NOT compressed
+3: .debug_aranges, ELF compressed, size: 60
+4: .debug_info, ELF compressed, size: 7e
+5: .debug_abbrev, NOT compressed
+6: .debug_line, ELF compressed, size: 8d
+7: .shstrtab, NOT compressed
+8: .symtab, NOT compressed
+9: .strtab, NOT compressed
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata mmap testfile-zgabi64be <<\EOF
+1: .text, NOT compressed
+2: .eh_frame, NOT compressed
+3: .debug_aranges, ELF compressed, size: 60
+4: .debug_info, ELF compressed, size: 7e
+5: .debug_abbrev, NOT compressed
+6: .debug_line, ELF compressed, size: 8d
+7: .shstrtab, NOT compressed
+8: .symtab, NOT compressed
+9: .strtab, NOT compressed
+EOF
+
+testfiles testfile-zgnu32
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata read testfile-zgnu32 <<\EOF
+1: .text, NOT compressed
+2: .zdebug_aranges, GNU compressed, size: 40
+3: .zdebug_info, GNU compressed, size: 9a
+4: .debug_abbrev, NOT compressed
+5: .zdebug_line, GNU compressed, size: 85
+6: .shstrtab, NOT compressed
+7: .symtab, NOT compressed
+8: .strtab, NOT compressed
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata mmap testfile-zgnu32 <<\EOF
+1: .text, NOT compressed
+2: .zdebug_aranges, GNU compressed, size: 40
+3: .zdebug_info, GNU compressed, size: 9a
+4: .debug_abbrev, NOT compressed
+5: .zdebug_line, GNU compressed, size: 85
+6: .shstrtab, NOT compressed
+7: .symtab, NOT compressed
+8: .strtab, NOT compressed
+EOF
+
+testfiles testfile-zgnu32be
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata read testfile-zgnu32be <<\EOF
+1: .text, NOT compressed
+2: .eh_frame, NOT compressed
+3: .zdebug_aranges, GNU compressed, size: 40
+4: .zdebug_info, GNU compressed, size: 6e
+5: .debug_abbrev, NOT compressed
+6: .zdebug_line, GNU compressed, size: 85
+7: .shstrtab, NOT compressed
+8: .symtab, NOT compressed
+9: .strtab, NOT compressed
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata mmap testfile-zgnu32be <<\EOF
+1: .text, NOT compressed
+2: .eh_frame, NOT compressed
+3: .zdebug_aranges, GNU compressed, size: 40
+4: .zdebug_info, GNU compressed, size: 6e
+5: .debug_abbrev, NOT compressed
+6: .zdebug_line, GNU compressed, size: 85
+7: .shstrtab, NOT compressed
+8: .symtab, NOT compressed
+9: .strtab, NOT compressed
+EOF
+
+testfiles testfile-zgabi32
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata read testfile-zgabi32 <<\EOF
+1: .text, NOT compressed
+2: .debug_aranges, ELF compressed, size: 40
+3: .debug_info, ELF compressed, size: 9a
+4: .debug_abbrev, NOT compressed
+5: .debug_line, ELF compressed, size: 85
+6: .shstrtab, NOT compressed
+7: .symtab, NOT compressed
+8: .strtab, NOT compressed
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata mmap testfile-zgabi32 <<\EOF
+1: .text, NOT compressed
+2: .debug_aranges, ELF compressed, size: 40
+3: .debug_info, ELF compressed, size: 9a
+4: .debug_abbrev, NOT compressed
+5: .debug_line, ELF compressed, size: 85
+6: .shstrtab, NOT compressed
+7: .symtab, NOT compressed
+8: .strtab, NOT compressed
+EOF
+
+testfiles testfile-zgabi32be
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata read testfile-zgabi32be <<\EOF
+1: .text, NOT compressed
+2: .eh_frame, NOT compressed
+3: .debug_aranges, ELF compressed, size: 40
+4: .debug_info, ELF compressed, size: 6e
+5: .debug_abbrev, NOT compressed
+6: .debug_line, ELF compressed, size: 85
+7: .shstrtab, NOT compressed
+8: .symtab, NOT compressed
+9: .strtab, NOT compressed
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata mmap testfile-zgabi32be <<\EOF
+1: .text, NOT compressed
+2: .eh_frame, NOT compressed
+3: .debug_aranges, ELF compressed, size: 40
+4: .debug_info, ELF compressed, size: 6e
+5: .debug_abbrev, NOT compressed
+6: .debug_line, ELF compressed, size: 85
+7: .shstrtab, NOT compressed
+8: .symtab, NOT compressed
+9: .strtab, NOT compressed
+EOF
+
+exit 0
diff --git a/tests/run-elfputzdata.sh b/tests/run-elfputzdata.sh
new file mode 100755
index 00000000..e9a352e5
--- /dev/null
+++ b/tests/run-elfputzdata.sh
@@ -0,0 +1,340 @@
+#! /bin/sh
+# 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/>.
+
+. $srcdir/test-subr.sh
+
+# Random ELF32 testfile
+testfiles testfile4
+
+testrun_compare ${abs_top_builddir}/tests/elfputzdata gnu testfile4 <<\EOF
+Cannot compress 1 .interp
+Cannot compress 2 .note.ABI-tag
+Cannot compress 3 .hash
+Cannot compress 4 .dynsym
+Cannot compress 5 .dynstr
+Cannot compress 6 .gnu.version
+Cannot compress 7 .gnu.version_r
+Cannot compress 8 .rel.got
+Cannot compress 9 .rel.plt
+Cannot compress 10 .init
+Cannot compress 11 .plt
+Cannot compress 12 .text
+Cannot compress 13 .fini
+Cannot compress 14 .rodata
+Cannot compress 15 .data
+Cannot compress 16 .eh_frame
+Cannot compress 17 .gcc_except_table
+Cannot compress 18 .ctors
+Cannot compress 19 .dtors
+Cannot compress 20 .got
+Cannot compress 21 .dynamic
+Lets compress 22 .sbss, size: 0
+Cannot compress 23 .bss
+Lets compress 24 .stab, size: 21540
+Lets compress 25 .stabstr, size: 57297
+Lets compress 26 .comment, size: 648
+Lets compress 27 .debug_aranges, size: 56
+Lets compress 28 .debug_pubnames, size: 93
+Lets compress 29 .debug_info, size: 960
+Lets compress 30 .debug_abbrev, size: 405
+Lets compress 31 .debug_line, size: 189
+Lets compress 32 .note, size: 240
+Not compressing section string table 33
+Lets compress 34 .symtab, size: 5488
+Lets compress 35 .strtab, size: 5727
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfputzdata elf testfile4 <<\EOF
+Cannot compress 1 .interp
+Cannot compress 2 .note.ABI-tag
+Cannot compress 3 .hash
+Cannot compress 4 .dynsym
+Cannot compress 5 .dynstr
+Cannot compress 6 .gnu.version
+Cannot compress 7 .gnu.version_r
+Cannot compress 8 .rel.got
+Cannot compress 9 .rel.plt
+Cannot compress 10 .init
+Cannot compress 11 .plt
+Cannot compress 12 .text
+Cannot compress 13 .fini
+Cannot compress 14 .rodata
+Cannot compress 15 .data
+Cannot compress 16 .eh_frame
+Cannot compress 17 .gcc_except_table
+Cannot compress 18 .ctors
+Cannot compress 19 .dtors
+Cannot compress 20 .got
+Cannot compress 21 .dynamic
+Lets compress 22 .sbss, size: 0
+Cannot compress 23 .bss
+Lets compress 24 .stab, size: 21540
+Lets compress 25 .stabstr, size: 57297
+Lets compress 26 .comment, size: 648
+Lets compress 27 .debug_aranges, size: 56
+Lets compress 28 .debug_pubnames, size: 93
+Lets compress 29 .debug_info, size: 960
+Lets compress 30 .debug_abbrev, size: 405
+Lets compress 31 .debug_line, size: 189
+Lets compress 32 .note, size: 240
+Not compressing section string table 33
+Lets compress 34 .symtab, size: 5488
+Lets compress 35 .strtab, size: 5727
+EOF
+
+# Random ELF64 testfile
+testfiles testfile12
+
+testrun_compare ${abs_top_builddir}/tests/elfputzdata gnu testfile12 <<\EOF
+Cannot compress 1 .hash
+Cannot compress 2 .dynsym
+Cannot compress 3 .dynstr
+Cannot compress 4 .gnu.version
+Cannot compress 5 .gnu.version_r
+Cannot compress 6 .rela.dyn
+Cannot compress 7 .rela.plt
+Cannot compress 8 .init
+Cannot compress 9 .plt
+Cannot compress 10 .text
+Cannot compress 11 .fini
+Cannot compress 12 .rodata
+Cannot compress 13 .eh_frame_hdr
+Cannot compress 14 .eh_frame
+Cannot compress 15 .data
+Cannot compress 16 .dynamic
+Cannot compress 17 .ctors
+Cannot compress 18 .dtors
+Cannot compress 19 .jcr
+Cannot compress 20 .got
+Cannot compress 21 .bss
+Lets compress 22 .comment, size: 246
+Lets compress 23 .debug_aranges, size: 192
+Lets compress 24 .debug_pubnames, size: 26
+Lets compress 25 .debug_info, size: 3468
+Lets compress 26 .debug_abbrev, size: 341
+Lets compress 27 .debug_line, size: 709
+Lets compress 28 .debug_frame, size: 56
+Lets compress 29 .debug_str, size: 2235
+Lets compress 30 .debug_macinfo, size: 10518
+Not compressing section string table 31
+Lets compress 32 .symtab, size: 1944
+Lets compress 33 .strtab, size: 757
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfputzdata elf testfile12 <<\EOF
+Cannot compress 1 .hash
+Cannot compress 2 .dynsym
+Cannot compress 3 .dynstr
+Cannot compress 4 .gnu.version
+Cannot compress 5 .gnu.version_r
+Cannot compress 6 .rela.dyn
+Cannot compress 7 .rela.plt
+Cannot compress 8 .init
+Cannot compress 9 .plt
+Cannot compress 10 .text
+Cannot compress 11 .fini
+Cannot compress 12 .rodata
+Cannot compress 13 .eh_frame_hdr
+Cannot compress 14 .eh_frame
+Cannot compress 15 .data
+Cannot compress 16 .dynamic
+Cannot compress 17 .ctors
+Cannot compress 18 .dtors
+Cannot compress 19 .jcr
+Cannot compress 20 .got
+Cannot compress 21 .bss
+Lets compress 22 .comment, size: 246
+Lets compress 23 .debug_aranges, size: 192
+Lets compress 24 .debug_pubnames, size: 26
+Lets compress 25 .debug_info, size: 3468
+Lets compress 26 .debug_abbrev, size: 341
+Lets compress 27 .debug_line, size: 709
+Lets compress 28 .debug_frame, size: 56
+Lets compress 29 .debug_str, size: 2235
+Lets compress 30 .debug_macinfo, size: 10518
+Not compressing section string table 31
+Lets compress 32 .symtab, size: 1944
+Lets compress 33 .strtab, size: 757
+EOF
+
+# Random ELF64BE testfile
+testfiles testfileppc64
+
+testrun_compare ${abs_top_builddir}/tests/elfputzdata gnu testfileppc64 <<\EOF
+Cannot compress 1 .interp
+Cannot compress 2 .note.ABI-tag
+Cannot compress 3 .note.gnu.build-id
+Cannot compress 4 .gnu.hash
+Cannot compress 5 .dynsym
+Cannot compress 6 .dynstr
+Cannot compress 7 .gnu.version
+Cannot compress 8 .gnu.version_r
+Cannot compress 9 .rela.plt
+Cannot compress 10 .init
+Cannot compress 11 .text
+Cannot compress 12 .fini
+Cannot compress 13 .rodata
+Cannot compress 14 .eh_frame_hdr
+Cannot compress 15 .eh_frame
+Cannot compress 16 .init_array
+Cannot compress 17 .fini_array
+Cannot compress 18 .jcr
+Cannot compress 19 .dynamic
+Cannot compress 20 .data
+Cannot compress 21 .opd
+Cannot compress 22 .got
+Cannot compress 23 .plt
+Cannot compress 24 .bss
+Lets compress 25 .comment, size: 88
+Lets compress 26 .debug_aranges, size: 96
+Lets compress 27 .debug_info, size: 363
+Lets compress 28 .debug_abbrev, size: 315
+Lets compress 29 .debug_line, size: 119
+Lets compress 30 .debug_frame, size: 96
+Lets compress 31 .debug_str, size: 174
+Lets compress 32 .debug_loc, size: 171
+Lets compress 33 .debug_ranges, size: 32
+Not compressing section string table 34
+Lets compress 35 .symtab, size: 1800
+Lets compress 36 .strtab, size: 602
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfputzdata elf testfileppc64 <<\EOF
+Cannot compress 1 .interp
+Cannot compress 2 .note.ABI-tag
+Cannot compress 3 .note.gnu.build-id
+Cannot compress 4 .gnu.hash
+Cannot compress 5 .dynsym
+Cannot compress 6 .dynstr
+Cannot compress 7 .gnu.version
+Cannot compress 8 .gnu.version_r
+Cannot compress 9 .rela.plt
+Cannot compress 10 .init
+Cannot compress 11 .text
+Cannot compress 12 .fini
+Cannot compress 13 .rodata
+Cannot compress 14 .eh_frame_hdr
+Cannot compress 15 .eh_frame
+Cannot compress 16 .init_array
+Cannot compress 17 .fini_array
+Cannot compress 18 .jcr
+Cannot compress 19 .dynamic
+Cannot compress 20 .data
+Cannot compress 21 .opd
+Cannot compress 22 .got
+Cannot compress 23 .plt
+Cannot compress 24 .bss
+Lets compress 25 .comment, size: 88
+Lets compress 26 .debug_aranges, size: 96
+Lets compress 27 .debug_info, size: 363
+Lets compress 28 .debug_abbrev, size: 315
+Lets compress 29 .debug_line, size: 119
+Lets compress 30 .debug_frame, size: 96
+Lets compress 31 .debug_str, size: 174
+Lets compress 32 .debug_loc, size: 171
+Lets compress 33 .debug_ranges, size: 32
+Not compressing section string table 34
+Lets compress 35 .symtab, size: 1800
+Lets compress 36 .strtab, size: 602
+EOF
+
+# Random ELF32BE testfile
+testfiles testfileppc32
+
+testrun_compare ${abs_top_builddir}/tests/elfputzdata gnu testfileppc32 <<\EOF
+Cannot compress 1 .interp
+Cannot compress 2 .note.ABI-tag
+Cannot compress 3 .note.gnu.build-id
+Cannot compress 4 .gnu.hash
+Cannot compress 5 .dynsym
+Cannot compress 6 .dynstr
+Cannot compress 7 .gnu.version
+Cannot compress 8 .gnu.version_r
+Cannot compress 9 .rela.dyn
+Cannot compress 10 .rela.plt
+Cannot compress 11 .init
+Cannot compress 12 .text
+Cannot compress 13 .fini
+Cannot compress 14 .rodata
+Cannot compress 15 .eh_frame_hdr
+Cannot compress 16 .eh_frame
+Cannot compress 17 .init_array
+Cannot compress 18 .fini_array
+Cannot compress 19 .jcr
+Cannot compress 20 .got2
+Cannot compress 21 .dynamic
+Cannot compress 22 .got
+Cannot compress 23 .plt
+Cannot compress 24 .data
+Cannot compress 25 .sdata
+Cannot compress 26 .bss
+Lets compress 27 .comment, size: 88
+Lets compress 28 .debug_aranges, size: 64
+Lets compress 29 .debug_info, size: 319
+Lets compress 30 .debug_abbrev, size: 318
+Lets compress 31 .debug_line, size: 109
+Lets compress 32 .debug_frame, size: 64
+Lets compress 33 .debug_str, size: 179
+Lets compress 34 .debug_loc, size: 99
+Lets compress 35 .debug_ranges, size: 16
+Not compressing section string table 36
+Lets compress 37 .symtab, size: 1232
+Lets compress 38 .strtab, size: 569
+EOF
+
+testrun_compare ${abs_top_builddir}/tests/elfputzdata elf testfileppc32 <<\EOF
+Cannot compress 1 .interp
+Cannot compress 2 .note.ABI-tag
+Cannot compress 3 .note.gnu.build-id
+Cannot compress 4 .gnu.hash
+Cannot compress 5 .dynsym
+Cannot compress 6 .dynstr
+Cannot compress 7 .gnu.version
+Cannot compress 8 .gnu.version_r
+Cannot compress 9 .rela.dyn
+Cannot compress 10 .rela.plt
+Cannot compress 11 .init
+Cannot compress 12 .text
+Cannot compress 13 .fini
+Cannot compress 14 .rodata
+Cannot compress 15 .eh_frame_hdr
+Cannot compress 16 .eh_frame
+Cannot compress 17 .init_array
+Cannot compress 18 .fini_array
+Cannot compress 19 .jcr
+Cannot compress 20 .got2
+Cannot compress 21 .dynamic
+Cannot compress 22 .got
+Cannot compress 23 .plt
+Cannot compress 24 .data
+Cannot compress 25 .sdata
+Cannot compress 26 .bss
+Lets compress 27 .comment, size: 88
+Lets compress 28 .debug_aranges, size: 64
+Lets compress 29 .debug_info, size: 319
+Lets compress 30 .debug_abbrev, size: 318
+Lets compress 31 .debug_line, size: 109
+Lets compress 32 .debug_frame, size: 64
+Lets compress 33 .debug_str, size: 179
+Lets compress 34 .debug_loc, size: 99
+Lets compress 35 .debug_ranges, size: 16
+Not compressing section string table 36
+Lets compress 37 .symtab, size: 1232
+Lets compress 38 .strtab, size: 569
+EOF
+
+exit 0