This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 5/6] libelf: Add elf32_zscn_compress, elf64_zscn_compress and gelf_zscn_compress.


Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
 libelf/ChangeLog             |  23 ++++
 libelf/Makefile.am           |   4 +-
 libelf/elf32_zscn_compress.c | 316 +++++++++++++++++++++++++++++++++++++++++++
 libelf/elf64_zscn_compress.c |  30 ++++
 libelf/elf_end.c             |  11 +-
 libelf/elf_error.c           |  26 +++-
 libelf/elf_getdata.c         |  36 +++--
 libelf/gelf.h                |   2 +
 libelf/gelf_zscn_compress.c  |  78 +++++++++++
 libelf/libelf.h              |  10 +-
 libelf/libelf.map            |   4 +
 libelf/libelfP.h             |  11 ++
 tests/ChangeLog              |  15 ++
 tests/Makefile.am            |   9 +-
 tests/elfgetzdata.c          | 110 +++++++++++++++
 tests/elfputzdata.c          | 173 +++++++++++++++++++++++
 tests/msg_tst.c              |   6 +-
 tests/run-elfgetzdata.sh     |  70 ++++++++++
 tests/run-elfputzdata.sh     | 174 ++++++++++++++++++++++++
 19 files changed, 1087 insertions(+), 21 deletions(-)
 create mode 100644 libelf/elf32_zscn_compress.c
 create mode 100644 libelf/elf64_zscn_compress.c
 create mode 100644 libelf/gelf_zscn_compress.c
 create mode 100644 tests/elfgetzdata.c
 create mode 100644 tests/elfputzdata.c
 create mode 100755 tests/run-elfgetzdata.sh
 create mode 100755 tests/run-elfputzdata.sh

diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index ea76c2d..daf1e30 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,26 @@
+2015-10-21  Mark Wielaard  <mjw@redhat.com>
+
+	* Makefile.am (libelf_a_SOURCES): Add elf32_zscn_compress.c,
+	elf64_zscn_compress.c and gelf_zscn_compress.c.
+	* elf32_zscn_compress.c: New file.
+	* elf64_zscn_compress.c: Likewise.
+	* gelf_zscn_compress.c: Likewise.
+	* elf_end.c (elf_end): Free zdata_base.
+	* elf_error.c: Add ELF_E_ALREADY_COMPRESSED,
+	ELF_E_UNKNOWN_COMPRESSION_TYPE, ELF_E_COMPRESS_ERROR and
+	ELF_E_DECOMPRESS_ERROR.
+	* elf_data.c (__libelf_data_type): New internal function extracted
+	from convert_data.
+	(convert_data): Handle SHF_COMPRESSED.
+	* gelf.h:  Define gelf_zscn_compress.
+	* libelf.h: Define elf32_zscn_compress and elf64_zscn_compress.
+	* libelf.map (ELFUTILS_1.7): Add elf32_zscn_compress,
+	elf64_zscn_compress and gelf_zscn_compress.
+	* libelfP.h: Add ELF_E_ALREADY_COMPRESSED,
+	ELF_E_UNKNOWN_COMPRESSION_TYPE, ELF_E_COMPRESS_ERROR and
+	ELF_E_DECOMPRESS_ERROR. Define __libelf_data_type.
+	(struct Elf_Scn): Add zdata_base.
+
 2015-10-16  Mark Wielaard  <mjw@redhat.com>
 
 	* Makefile.am (libelf_so_LDLIBS): Add -lz.
diff --git a/libelf/Makefile.am b/libelf/Makefile.am
index e2d7549..db5de24 100644
--- a/libelf/Makefile.am
+++ b/libelf/Makefile.am
@@ -89,7 +89,9 @@ libelf_a_SOURCES = elf_version.c elf_hash.c elf_error.c elf_fill.c \
 		   elf_getaroff.c \
 		   elf_gnu_hash.c \
 		   elf_scnshndx.c \
-		   elf32_getchdr.c elf64_getchdr.c gelf_getchdr.c
+		   elf32_getchdr.c elf64_getchdr.c gelf_getchdr.c \
+		   elf32_zscn_compress.c elf64_zscn_compress.c \
+		   gelf_zscn_compress.c
 
 libelf_pic_a_SOURCES =
 am_libelf_pic_a_OBJECTS = $(libelf_a_SOURCES:.c=.os)
diff --git a/libelf/elf32_zscn_compress.c b/libelf/elf32_zscn_compress.c
new file mode 100644
index 0000000..6e816db
--- /dev/null
+++ b/libelf/elf32_zscn_compress.c
@@ -0,0 +1,316 @@
+/* Compress or decompress a section.
+   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 either
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at
+       your option) any later version
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at
+       your option) any later version
+
+   or both in parallel, as here.
+
+   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 copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <libelf.h>
+#include "libelfP.h"
+#include "common.h"
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <zlib.h>
+
+#ifndef LIBELFBITS
+# define LIBELFBITS 32
+#endif
+
+
+ElfW2(LIBELFBITS,Shdr) *
+elfw2(LIBELFBITS,zscn_compress) (Elf_Scn *scn, int type, int ch_type)
+{
+  if (scn == NULL)
+    return NULL;
+
+  ElfW2(LIBELFBITS,Shdr) *shdr;
+  shdr = elfw2(LIBELFBITS,getshdr) (scn);
+  if (shdr == NULL)
+    return NULL;
+
+  int compressed;
+  ElfW2(LIBELFBITS,Chdr) *chdr;
+  chdr = elfw2(LIBELFBITS,getchdr) (scn, &compressed);
+  if (chdr == NULL && compressed == -1)
+    return NULL;
+
+  if (compressed == ELF_ZSCN_T_NONE)
+    {
+      if (type != ELF_ZSCN_T_GNU && type != ELF_ZSCN_T_ELF)
+	{
+	  __libelf_seterrno (ELF_E_NOT_COMPRESSED);
+	  return NULL;
+	}
+
+      if (ch_type != ELFCOMPRESS_ZLIB)
+	{
+	  __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);
+	  return NULL;
+	}
+
+      /* The compressed data is the raw/unconverted data.  We simplify
+	 the implementation a bit by asking for the (converted) data
+	 and then convert back to raw if needed.  Should be made a bit
+	 more clever to directly use raw if that is all that is
+	 available.  */
+      Elf_Data *data = elf_getdata (scn, NULL);
+      if (data == NULL)
+	return NULL;
+
+      /* Guess an output size. 1/8th of the original sh_size at least 8K.  */
+      size_t block = shdr->sh_size / 8;
+      if (block < 8 * 1024)
+	block = 8 * 1024;
+      size_t out_size = block;
+      void *out_buf = malloc (out_size);
+      if (out_buf == NULL)
+	{
+	  __libelf_seterrno (ELF_E_NOMEM);
+	  return NULL;
+	}
+      /* Reserve space for the GNU/ELF Chdr header.  */
+      size_t hsize = (type == ELF_ZSCN_T_GNU
+		      ? 4 + 8 /* GNU "ZLIB" + 8 byte size.  */
+		      : sizeof (ElfW2(LIBELFBITS,Chdr)));
+      size_t used = hsize;
+
+      z_stream z;
+      z.zalloc = Z_NULL;
+      z.zfree = Z_NULL;
+      z.opaque = Z_NULL;
+      int zrc = deflateInit (&z, Z_BEST_COMPRESSION);
+      if (zrc != Z_OK)
+	{
+	  __libelf_seterrno (ELF_E_COMPRESS_ERROR);
+	  return NULL;
+	}
+
+      /* Cleanup and NULL return on error.  Don't leak memory.  */
+      void *deflate_error (int err)
+      {
+	__libelf_seterrno (err);
+	deflateEnd (&z);
+	free (out_buf);
+	return NULL;
+      }
+
+      /* Loop over data buffers.  */
+      int flush;
+      size_t orig_size = 0;
+      do
+	{
+	  /* XXX convert to raw if different endianess.  */
+	  orig_size += data->d_size;
+	  z.avail_in = data->d_size;
+	  z.next_in = data->d_buf;
+	  data = elf_getdata (scn, data);
+	  flush = data == NULL ? Z_FINISH : Z_NO_FLUSH;
+	  /* Flush one data buffer.  */
+	  do
+	    {
+	      z.avail_out = out_size - used;
+	      z.next_out = out_buf + used;
+	      zrc = deflate (&z, flush);
+	      if (zrc == Z_STREAM_ERROR)
+		return deflate_error (ELF_E_COMPRESS_ERROR);
+	      used += (out_size - used) - z.avail_out;
+	      if (z.avail_out == 0)
+		{
+		  void *bigger = realloc (out_buf, out_size + block);
+		  if (bigger == NULL)
+		    return deflate_error (ELF_E_NOMEM);
+		  out_buf = bigger;
+		  out_size += block;
+		}
+	    }
+	  while (z.avail_out == 0); /* Need more output buffer.  */
+	}
+      while (flush != Z_FINISH); /* More data blocks.  */
+
+      zrc = deflateEnd (&z);
+      if (zrc != Z_OK)
+	return deflate_error (ELF_E_COMPRESS_ERROR);
+
+      if (type == ELF_ZSCN_T_GNU)
+	{
+	  uint64_t be64_size = htobe64 (orig_size);
+	  memmove (out_buf, "ZLIB", 4);
+	  memmove (out_buf + 4, &be64_size, sizeof (be64_size));
+	}
+      else
+	{
+	  Elf *elf = scn->elf;
+	  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
+
+	  ElfW2(LIBELFBITS,Chdr) bchdr;
+	  bchdr.ch_type = ELFCOMPRESS_ZLIB;
+#if LIBELFBITS == 64
+	  bchdr.ch_reserved = 0;
+#endif
+	  bchdr.ch_size = orig_size;
+	  bchdr.ch_addralign = shdr->sh_addralign;
+	  if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
+	    {
+	      CONVERT (bchdr.ch_type);
+	      CONVERT (bchdr.ch_size);
+	      CONVERT (bchdr.ch_type);
+	    }
+	  memmove (out_buf, &bchdr, sizeof (ElfW2(LIBELFBITS,Chdr)));
+	}
+
+      /* This is the new raw data, replace and possibly free old data.  */
+      scn->rawdata.d.d_buf = out_buf;
+      scn->rawdata.d.d_type = ELF_T_BYTE;
+      scn->rawdata.d.d_size = used;
+      scn->rawdata.d.d_align = 1;
+
+      /* Note we keep the sh_entsize as is, we assume it is setup
+	 correctly and ignored when SHF_COMPRESSED is set.  */
+      shdr->sh_size = used;
+      shdr->sh_addralign = 1;
+      if (type == ELF_ZSCN_T_ELF)
+	shdr->sh_flags |= SHF_COMPRESSED;
+
+      /* The section is now compressed, but let the user call getchdr
+	 to get it if they want.  */
+      if ((void *) chdr != scn->rawdata_base)
+        free (chdr);
+      scn->chdr.ELFW(e,LIBELFBITS) = NULL;
+
+      /* Existing existing data is no longer valid.  */
+      scn->data_list_rear = NULL;
+      if (scn->data_base != scn->rawdata_base)
+	free (scn->data_base);
+      scn->data_base = NULL;
+      if (scn->elf->map_address == NULL
+	  || scn->rawdata_base == scn->zdata_base)
+	free (scn->rawdata_base);
+      scn->zdata_base = NULL;
+      scn->rawdata_base = out_buf;
+
+      return shdr;
+    }
+  else
+    {
+      if (type != ELF_ZSCN_T_NONE)
+	{
+	  __libelf_seterrno (ELF_E_ALREADY_COMPRESSED);
+	  return NULL;
+	}
+
+      if ((compressed != ELF_ZSCN_T_GNU && compressed != ELF_ZSCN_T_ELF)
+	  || chdr->ch_type != ELFCOMPRESS_ZLIB)
+	{
+	  __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);
+	  return NULL;
+	}
+
+      Elf_Data *rawdata = elf_rawdata (scn, NULL);
+      if (rawdata == NULL)
+	return NULL;
+
+      size_t size = chdr->ch_size;
+      size_t hsize = (compressed == ELF_ZSCN_T_GNU
+		      ? 4 + 8 /* GNU "ZLIB" + 8 byte size.  */
+		      : sizeof (ElfW2(LIBELFBITS,Chdr)));
+
+      void *buf_out = malloc (size);
+      if (unlikely (buf_out == NULL))
+	{
+	  __libelf_seterrno (ELF_E_NOMEM);
+	  return NULL;
+	}
+
+      /* rawdata_base must be non-NULL since we found a compressed header.  */
+      void *buf_in = rawdata->d_buf + hsize;
+
+      z_stream z =
+	{
+	  .next_in = buf_in,
+	  .avail_in = rawdata->d_size - hsize,
+	  .next_out = buf_out,
+	  .avail_out = size
+	};
+      int zrc = inflateInit (&z);
+      while (z.avail_in > 0 && likely (zrc == Z_OK))
+	{
+	  z.next_out = buf_out + (size - z.avail_out);
+	  zrc = inflate (&z, Z_FINISH);
+	  if (unlikely (zrc != Z_STREAM_END))
+	    {
+	      zrc = Z_DATA_ERROR;
+	      break;
+	    }
+	  zrc = inflateReset (&z);
+	}
+      if (likely (zrc == Z_OK))
+	zrc = inflateEnd (&z);
+
+      if (unlikely (zrc != Z_OK) || unlikely (z.avail_out != 0))
+	{
+	  free (buf_out);
+	  __libelf_seterrno (ELF_E_DECOMPRESS_ERROR);
+	  return NULL;
+	}
+
+      /* This is the new raw data, replace and possibly free old data.  */
+      scn->rawdata.d.d_buf = buf_out;
+      scn->rawdata.d.d_type = __libelf_data_type (scn->elf, shdr->sh_type);
+      scn->rawdata.d.d_size = size;
+      scn->rawdata.d.d_align = chdr->ch_addralign;
+
+      /* Note we keep the sh_entsize as is, we assume it is setup
+	 correctly and ignored when SHF_COMPRESSED is set.  */
+      shdr->sh_size = size;
+      shdr->sh_addralign = chdr->ch_addralign;
+      shdr->sh_flags &= ~SHF_COMPRESSED;
+
+      /* The section isn't compressed anymore.  */
+      if ((void *) chdr != scn->rawdata_base)
+        free (chdr);
+      scn->chdr.ELFW(e,LIBELFBITS) = (void *) -1;
+      scn->chdr_type = ELF_ZSCN_T_NONE;
+
+      /* The (converted) data list must be reloaded, existing data is
+	 no longer valid.  */
+      scn->data_list_rear = NULL;
+      if (scn->data_base != scn->rawdata_base)
+	free (scn->data_base);
+      scn->data_base = NULL;
+      if (scn->elf->map_address == NULL
+	  || scn->rawdata_base == scn->zdata_base)
+	free (scn->rawdata_base);
+
+      scn->rawdata_base = scn->zdata_base = buf_out;
+
+      return shdr;
+    }
+}
diff --git a/libelf/elf64_zscn_compress.c b/libelf/elf64_zscn_compress.c
new file mode 100644
index 0000000..cf24486
--- /dev/null
+++ b/libelf/elf64_zscn_compress.c
@@ -0,0 +1,30 @@
+/* Return section compression header.
+   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 either
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at
+       your option) any later version
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at
+       your option) any later version
+
+   or both in parallel, as here.
+
+   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 copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#define LIBELFBITS 64
+#include "elf32_zscn_compress.c"
diff --git a/libelf/elf_end.c b/libelf/elf_end.c
index 3d5ed57..2da72bc 100644
--- a/libelf/elf_end.c
+++ b/libelf/elf_end.c
@@ -156,6 +156,12 @@ elf_end (Elf *elf)
 		if (chdr != (void *) -1 && chdr != scn->data_base)
 		  free (chdr);
 
+		/* Free zdata if uncompressed, but not yet used as
+		   rawdata_base.  If it is already used it will be
+		   freed below.  */
+		if (scn->zdata_base != scn->rawdata_base)
+		  free (scn->zdata_base);
+
 		/* If the file has the same byte order and the
 		   architecture doesn't require overly stringent
 		   alignment the raw data buffer is the same as the
@@ -164,8 +170,9 @@ elf_end (Elf *elf)
 		  free (scn->data_base);
 
 		/* The section data is allocated if we couldn't mmap
-		   the file.  */
-		if (elf->map_address == NULL)
+		   the file.  Or if we had to decompress.  */
+		if (elf->map_address == NULL
+		    || scn->rawdata_base == scn->zdata_base)
 		  free (scn->rawdata_base);
 
 		/* Free the list of data buffers for the section.
diff --git a/libelf/elf_error.c b/libelf/elf_error.c
index 9618da4..20c2f17 100644
--- a/libelf/elf_error.c
+++ b/libelf/elf_error.c
@@ -235,6 +235,26 @@ core files")
   (ELF_E_INVALID_OFFSET_IDX \
    + sizeof "invalid offset")
   N_("section does not contain compressed data")
+  "\0"
+#define ELF_E_ALREADY_COMPRESSED_IDX \
+  (ELF_E_NOT_COMPRESSED_IDX \
+   + sizeof "section does not contain compressed data")
+  N_("section contains compressed data")
+  "\0"
+#define ELF_E_UNKNOWN_COMPRESSION_TYPE_IDX \
+  (ELF_E_ALREADY_COMPRESSED_IDX \
+   + sizeof "section contains compressed data")
+  N_("unknown compression type")
+  "\0"
+#define ELF_E_COMPRESS_ERROR_IDX \
+  (ELF_E_UNKNOWN_COMPRESSION_TYPE_IDX \
+   + sizeof "unknown compression type")
+  N_("cannot compress data")
+  "\0"
+#define ELF_E_DECOMPRESS_ERROR_IDX \
+  (ELF_E_COMPRESS_ERROR_IDX \
+   + sizeof "cannot compress data")
+  N_("cannot decompress data")
 };
 
 
@@ -283,7 +303,11 @@ static const uint_fast16_t msgidx[ELF_E_NUM] =
   [ELF_E_INVALID_PHDR] = ELF_E_INVALID_PHDR_IDX,
   [ELF_E_NO_PHDR] = ELF_E_NO_PHDR_IDX,
   [ELF_E_INVALID_OFFSET] = ELF_E_INVALID_OFFSET_IDX,
-  [ELF_E_NOT_COMPRESSED] = ELF_E_NOT_COMPRESSED_IDX
+  [ELF_E_NOT_COMPRESSED] = ELF_E_NOT_COMPRESSED_IDX,
+  [ELF_E_ALREADY_COMPRESSED] = ELF_E_ALREADY_COMPRESSED_IDX,
+  [ELF_E_UNKNOWN_COMPRESSION_TYPE] = ELF_E_UNKNOWN_COMPRESSION_TYPE_IDX,
+  [ELF_E_COMPRESS_ERROR] = ELF_E_COMPRESS_ERROR_IDX,
+  [ELF_E_DECOMPRESS_ERROR] = ELF_E_DECOMPRESS_ERROR_IDX
 };
 #define nmsgidx ((int) (sizeof (msgidx) / sizeof (msgidx[0])))
 
diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
index 9a567e5..7451375 100644
--- a/libelf/elf_getdata.c
+++ b/libelf/elf_getdata.c
@@ -117,6 +117,22 @@ const uint_fast8_t __libelf_type_aligns[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM]
 #endif
 
 
+Elf_Type
+internal_function
+__libelf_data_type (Elf *elf, int sh_type)
+{
+  /* Some broken ELF ABI for 64-bit machines use the wrong hash table
+     entry size.  See elf-knowledge.h for more information.  */
+  if (sh_type == SHT_HASH && elf->class == ELFCLASS64)
+    {
+      GElf_Ehdr ehdr_mem;
+      GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
+      return (SH_ENTSIZE_HASH (ehdr) == 4 ? ELF_T_WORD : ELF_T_XWORD);
+    }
+  else
+    return shtype_map[LIBELF_EV_IDX][TYPEIDX (sh_type)];
+}
+
 /* Convert the data in the current section.  */
 static void
 convert_data (Elf_Scn *scn, int version __attribute__ ((unused)), int eclass,
@@ -204,6 +220,7 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
   Elf64_Off offset;
   Elf64_Xword size;
   Elf64_Xword align;
+  Elf64_Xword flags;
   int type;
   Elf *elf = scn->elf;
 
@@ -220,6 +237,7 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
       size = shdr->sh_size;
       type = shdr->sh_type;
       align = shdr->sh_addralign;
+      flags = shdr->sh_flags;
     }
   else
     {
@@ -234,6 +252,7 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
       size = shdr->sh_size;
       type = shdr->sh_type;
       align = shdr->sh_addralign;
+      flags = shdr->sh_flags;
     }
 
   /* If the section has no data (for whatever reason), leave the `d_buf'
@@ -243,7 +262,9 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
       /* First a test whether the section is valid at all.  */
       size_t entsize;
 
-      if (type == SHT_HASH)
+      if ((flags & SHF_COMPRESSED) != 0)
+	entsize = 1;
+      else if (type == SHT_HASH)
 	{
 	  GElf_Ehdr ehdr_mem;
 	  GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
@@ -320,17 +341,10 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
     }
 
   scn->rawdata.d.d_size = size;
-  /* Some broken ELF ABI for 64-bit machines use the wrong hash table
-     entry size.  See elf-knowledge.h for more information.  */
-  if (type == SHT_HASH && elf->class == ELFCLASS64)
-    {
-      GElf_Ehdr ehdr_mem;
-      GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
-      scn->rawdata.d.d_type
-	= (SH_ENTSIZE_HASH (ehdr) == 4 ? ELF_T_WORD : ELF_T_XWORD);
-    }
+  if ((flags & SHF_COMPRESSED) != 0)
+    scn->rawdata.d.d_type = ELF_T_BYTE;
   else
-    scn->rawdata.d.d_type = shtype_map[LIBELF_EV_IDX][TYPEIDX (type)];
+    scn->rawdata.d.d_type = __libelf_data_type (elf, type);
   scn->rawdata.d.d_off = 0;
 
   /* Make sure the alignment makes sense.  d_align should be aligned both
diff --git a/libelf/gelf.h b/libelf/gelf.h
index cf9dc40..026c43a 100644
--- a/libelf/gelf.h
+++ b/libelf/gelf.h
@@ -192,6 +192,8 @@ extern unsigned long int gelf_newphdr (Elf *__elf, size_t __phnum);
    set.  */
 extern GElf_Chdr *gelf_getchdr (Elf_Scn *__scn, GElf_Chdr *__dst, int *__type);
 
+extern GElf_Shdr *gelf_zscn_compress (Elf_Scn *scn, GElf_Shdr *shdr,
+				      int type, int ch_type);
 
 /* Convert data structure from the representation in the file represented
    by ELF to their memory representation.  */
diff --git a/libelf/gelf_zscn_compress.c b/libelf/gelf_zscn_compress.c
new file mode 100644
index 0000000..fbc02aa
--- /dev/null
+++ b/libelf/gelf_zscn_compress.c
@@ -0,0 +1,78 @@
+/* Compress or decompress a section.
+   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 either
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at
+       your option) any later version
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at
+       your option) any later version
+
+   or both in parallel, as here.
+
+   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 copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "libelfP.h"
+#include <gelf.h>
+#include <stddef.h>
+
+
+GElf_Shdr *
+gelf_zscn_compress (Elf_Scn *scn, GElf_Shdr *dest, int type, int ch_type)
+{
+  if (scn == NULL)
+    return NULL;
+
+  if (dest == NULL)
+    {
+      __libelf_seterrno (ELF_E_INVALID_OPERAND);
+      return NULL;
+    }
+
+  if (scn->elf->class == ELFCLASS32)
+    {
+      Elf32_Shdr *shdr = elf32_zscn_compress (scn, type, ch_type);
+      if (shdr == NULL)
+	return NULL;
+
+      dest->sh_name = shdr->sh_name;
+      dest->sh_type = shdr->sh_type;
+      dest->sh_flags = shdr->sh_flags;
+      dest->sh_addr = shdr->sh_addr;
+      dest->sh_offset = shdr->sh_offset;
+      dest->sh_size = shdr->sh_size;
+      dest->sh_link = shdr->sh_link;
+      dest->sh_info = shdr->sh_info;
+      dest->sh_addralign = shdr->sh_addralign;
+      dest->sh_entsize = shdr->sh_entsize;
+    }
+  else
+    {
+      Elf64_Shdr *shdr = elf64_zscn_compress (scn, type, ch_type);
+      if (shdr == NULL)
+	return NULL;
+
+      *dest = *shdr;
+    }
+
+  return dest;
+}
+INTDEF(gelf_zscn_compress)
diff --git a/libelf/libelf.h b/libelf/libelf.h
index 72a11c7..d6041d5 100644
--- a/libelf/libelf.h
+++ b/libelf/libelf.h
@@ -291,6 +291,9 @@ extern Elf64_Shdr *elf64_getshdr (Elf_Scn *__scn);
 extern Elf32_Chdr *elf32_getchdr (Elf_Scn *__scn, int *__type);
 extern Elf64_Chdr *elf64_getchdr (Elf_Scn *__scn, int *__type);
 
+extern Elf32_Shdr *elf32_zscn_compress (Elf_Scn *scn, int type, int ch_type);
+extern Elf64_Shdr *elf64_zscn_compress (Elf_Scn *scn, int type, int ch_type);
+
 /* Set or clear flags for ELF file.  */
 extern unsigned int elf_flagelf (Elf *__elf, Elf_Cmd __cmd,
 				 unsigned int __flags);
@@ -311,8 +314,11 @@ extern unsigned int elf_flagshdr (Elf_Scn *__scn, Elf_Cmd __cmd,
 				  unsigned int __flags);
 
 
-/* Get data from section while translating from file representation
-   to memory representation.  */
+/* Get data from section while translating from file representation to
+   memory representation.  The Elf_Data d_type is set based on the
+   section type if known.  Otherwise d_type is set to ELF_T_BYTE.  If
+   the section contains compressed data d_type is also set to
+   ELF_T_BYTE.  */
 extern Elf_Data *elf_getdata (Elf_Scn *__scn, Elf_Data *__data);
 
 /* Get uninterpreted section content.  */
diff --git a/libelf/libelf.map b/libelf/libelf.map
index 8525294..81c134b 100644
--- a/libelf/libelf.map
+++ b/libelf/libelf.map
@@ -144,4 +144,8 @@ ELFUTILS_1.7 {
     elf32_getchdr;
     elf64_getchdr;
     gelf_getchdr;
+
+    elf32_zscn_compress;
+    elf64_zscn_compress;
+    gelf_zscn_compress;
 } ELFUTILS_1.6;
diff --git a/libelf/libelfP.h b/libelf/libelfP.h
index 3adbed7..941c016 100644
--- a/libelf/libelfP.h
+++ b/libelf/libelfP.h
@@ -139,6 +139,10 @@ enum
   ELF_E_NO_PHDR,
   ELF_E_INVALID_OFFSET,
   ELF_E_NOT_COMPRESSED,
+  ELF_E_ALREADY_COMPRESSED,
+  ELF_E_UNKNOWN_COMPRESSION_TYPE,
+  ELF_E_COMPRESS_ERROR,
+  ELF_E_DECOMPRESS_ERROR,
   /* Keep this as the last entry.  */
   ELF_E_NUM
 };
@@ -239,6 +243,8 @@ struct Elf_Scn
   char *rawdata_base;		/* The unmodified data of the section.  */
   char *data_base;		/* The converted data of the section.  */
 
+  char *zdata_base;		/* The uncompressed data of the section.  */
+
   struct Elf_ScnList *list;	/* Pointer to the section list element the
 				   data is in.  */
 };
@@ -447,6 +453,11 @@ extern const uint_fast8_t __libelf_type_aligns[EV_NUM - 1][ELFCLASSNUM - 1][ELF_
 # define __libelf_type_align(class, type)	1
 #endif
 
+/* Given an Elf handle and a section type returns the Elf_Data d_type.
+   Should not be called when SHF_COMPRESSED is set, the d_type should
+   be ELF_T_BYTE.  */
+extern Elf_Type __libelf_data_type (Elf *elf, int sh_type) internal_function;
+
 /* The libelf API does not have such a function but it is still useful.
    Get the memory size for the given type.
 
diff --git a/tests/ChangeLog b/tests/ChangeLog
index a3e5a16..5a493cf 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,18 @@
+2015-10-21  Mark Wielaard  <mjw@redhat.com>
+
+	* 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-16  Mark Wielaard  <mjw@redhat.com>
 
 	* Makefile.am [BUILD_STATIC] (libdw): Add -lz.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 364d0f2..aae2166 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -52,7 +52,7 @@ 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 elfgetchdr
+		  elfshphehdr elfstrmerge elfgetchdr elfgetzdata elfputzdata
 
 asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
 	    asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -120,7 +120,8 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \
 	run-readelf-dwz-multi.sh run-allfcts-multi.sh run-deleted.sh \
 	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-elfgetchdr.sh
+	elfshphehdr run-lfs-symbols.sh run-elfgetchdr.sh run-elfgetzdata.sh \
+	run-elfputzdata.sh
 
 if !BIARCH
 export ELFUTILS_DISABLE_BIARCH = 1
@@ -308,7 +309,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
 	     run-lfs-symbols.sh lfs-symbols testfile-nolfs.bz2 \
 	     testfile-zgnu32.bz2 testfile-zgnu64.bz2 \
 	     testfile-zgabi32.bz2 testfile-zgabi64.bz2 \
-	     run-elfgetchdr.sh
+	     run-elfgetchdr.sh run-elfgetzdata.sh run-elfputzdata
 
 if USE_VALGRIND
 valgrind_cmd='valgrind -q --error-exitcode=1 --run-libc-freeres=no'
@@ -458,6 +459,8 @@ dwfl_proc_attach_LDFLAGS = -pthread $(AM_LDFLAGS)
 elfshphehdr_LDADD =$(libelf)
 elfstrmerge_LDADD = $(libebl) $(libelf)
 elfgetchdr_LDADD = $(libelf)
+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 0000000..445ecd0
--- /dev/null
+++ b/tests/elfgetzdata.c
@@ -0,0 +1,110 @@
+/* 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 <unistd.h>
+
+
+int
+main (int argc, char *argv[])
+{
+  int result = 0;
+  int cnt;
+
+  elf_version (EV_CURRENT);
+
+  for (cnt = 1; 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 ((shdr->sh_flags & SHF_COMPRESSED) != 0)
+	    {
+	      /* Real compressed section.  */
+	      if (gelf_zscn_compress (scn, &mem, ELF_ZSCN_T_NONE, 0) == NULL)
+		{
+		  printf ("gelf_zscn_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 (gelf_zscn_compress (scn, &mem, ELF_ZSCN_T_NONE,
+					  0) == NULL)
+		    {
+		      printf ("gelf_zscn_compress 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
+		{
+		  /* Assume not compressed.  */
+		  if (gelf_zscn_compress (scn, &mem, ELF_ZSCN_T_NONE,
+					  0) != NULL)
+		    printf ("%zd: %s, ERROR compressed\n",
+			    idx, name);
+		  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 0000000..12e72af
--- /dev/null
+++ b/tests/elfputzdata.c
@@ -0,0 +1,173 @@
+/* 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 compress;
+  if (strcmp (argv[1], "gnu") == 0)
+    compress = ELF_ZSCN_T_GNU;
+  else
+    compress = ELF_ZSCN_T_ELF;
+
+  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 (gelf_zscn_compress (scn, &mem, compress,
+				      ELFCOMPRESS_ZLIB) == NULL)
+		{
+		  printf ("gelf_zscn_compress failed for section %zd: %s\n",
+			  idx, elf_errmsg (-1));
+		  return -1;
+		}
+	      d = elf_getdata (scn, NULL);
+	      size_t new_size = shdr->sh_size;
+	      // 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 (gelf_zscn_compress (scn, &mem, ELF_ZSCN_T_NONE, 0) == NULL)
+		{
+		  printf ("gelf_zscn_[un]compress failed for section %zd: %s\n",
+			  idx, elf_errmsg (-1));
+		  return -1;
+		}
+	      d = elf_getdata (scn, NULL);
+	      new_size = shdr->sh_size;
+	      // fprintf (stderr, "  newer_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 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 38f57f3..ce8746e 100644
--- a/tests/msg_tst.c
+++ b/tests/msg_tst.c
@@ -75,7 +75,11 @@ static struct
 and core files" },
     { ELF_E_NO_PHDR, "file has no program header" },
     { ELF_E_INVALID_OFFSET, "invalid offset" },
-    { 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 0000000..c03ef6a
--- /dev/null
+++ b/tests/run-elfgetzdata.sh
@@ -0,0 +1,70 @@
+#! /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 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-zgabi64
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata 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-zgnu32
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata 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-zgabi32
+testrun_compare ${abs_top_builddir}/tests/elfgetzdata 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
+
+exit 0
diff --git a/tests/run-elfputzdata.sh b/tests/run-elfputzdata.sh
new file mode 100755
index 0000000..da423b5
--- /dev/null
+++ b/tests/run-elfputzdata.sh
@@ -0,0 +1,174 @@
+#! /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
+
+exit 0
-- 
1.8.3.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]