summaryrefslogtreecommitdiffstats
path: root/libdwelf
diff options
context:
space:
mode:
Diffstat (limited to 'libdwelf')
-rw-r--r--libdwelf/ChangeLog7
-rw-r--r--libdwelf/Makefile.am4
-rw-r--r--libdwelf/dwelf_scn_gnu_compressed_size.c77
-rw-r--r--libdwelf/libdwelf.h9
4 files changed, 94 insertions, 3 deletions
diff --git a/libdwelf/ChangeLog b/libdwelf/ChangeLog
index f182ecf4..fe8af1b4 100644
--- a/libdwelf/ChangeLog
+++ b/libdwelf/ChangeLog
@@ -1,3 +1,10 @@
+2015-10-28 Mark Wielaard <[email protected]>
+
+ * Makefile.am (libdwelf_a_SOURCES): Add
+ dwelf_scn_gnu_compressed_size.c.
+ * dwelf_scn_gnu_compressed_size.c: Likewise.
+ * libdwelf.h (dwelf_scn_gnu_compressed_size): New declaration.
+
2015-10-14 Chih-Hung Hsieh <[email protected]>
* dwelf_elf_gnu_build_id.c (find_elf_build_id): Move nested function
diff --git a/libdwelf/Makefile.am b/libdwelf/Makefile.am
index cd4b7ddf..4de4b2ed 100644
--- a/libdwelf/Makefile.am
+++ b/libdwelf/Makefile.am
@@ -2,7 +2,7 @@
##
## Process this file with automake to create Makefile.in
##
-## Copyright (C) 2014 Red Hat, Inc.
+## Copyright (C) 2014, 2015 Red Hat, Inc.
## This file is part of elfutils.
##
## This file is free software; you can redistribute it and/or modify
@@ -40,7 +40,7 @@ pkginclude_HEADERS = libdwelf.h
noinst_HEADERS = libdwelfP.h
libdwelf_a_SOURCES = dwelf_elf_gnu_debuglink.c dwelf_dwarf_gnu_debugaltlink.c \
- dwelf_elf_gnu_build_id.c
+ dwelf_elf_gnu_build_id.c dwelf_scn_gnu_compressed_size.c
libdwelf = $(libdw)
diff --git a/libdwelf/dwelf_scn_gnu_compressed_size.c b/libdwelf/dwelf_scn_gnu_compressed_size.c
new file mode 100644
index 00000000..d39b702a
--- /dev/null
+++ b/libdwelf/dwelf_scn_gnu_compressed_size.c
@@ -0,0 +1,77 @@
+/* Return size of GNU compressed 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 "libdwelfP.h"
+#include "libelfP.h"
+
+ssize_t
+dwelf_scn_gnu_compressed_size (Elf_Scn *scn)
+{
+ if (scn == NULL)
+ return -1;
+
+ GElf_Shdr shdr;
+ if (gelf_getshdr (scn, &shdr) == NULL)
+ return -1;
+
+ /* Allocated or no bits sections can never be compressed. */
+ if ((shdr.sh_flags & SHF_ALLOC) != 0
+ || shdr.sh_type == SHT_NULL
+ || shdr.sh_type == SHT_NOBITS)
+ return -1;
+
+ Elf_Data *d = elf_rawdata (scn, NULL);
+ if (d == NULL)
+ return -1;
+
+ if (d->d_size >= 4 + 8
+ && memcmp (d->d_buf, "ZLIB", 4) == 0)
+ {
+ /* There is a 12-byte header of "ZLIB" followed by
+ an 8-byte big-endian size. There is only one type and
+ alignment isn't preserved separately. */
+ uint64_t size;
+ memcpy (&size, d->d_buf + 4, sizeof size);
+ size = be64toh (size);
+
+ /* One more sanity check, size should be bigger than original
+ data size plus some overhead (4 chars ZLIB + 8 bytes size + 6
+ bytes zlib stream overhead + 5 bytes overhead max for one 16K
+ block) and should fit into a size_t. */
+ if (size + 4 + 8 + 6 + 5 < d->d_size || size > SIZE_MAX)
+ return -1;
+
+ return size;
+ }
+
+ return -1;
+}
diff --git a/libdwelf/libdwelf.h b/libdwelf/libdwelf.h
index e16dc0f3..7f7f679a 100644
--- a/libdwelf/libdwelf.h
+++ b/libdwelf/libdwelf.h
@@ -1,5 +1,5 @@
/* Interfaces for libdwelf. DWARF ELF Low-level Functions.
- Copyright (C) 2014 Red Hat, Inc.
+ Copyright (C) 2014, 2015 Red Hat, Inc.
This file is part of elfutils.
This file is free software; you can redistribute it and/or modify
@@ -65,6 +65,13 @@ extern ssize_t dwelf_dwarf_gnu_debugaltlink (Dwarf *dwarf,
note. Returns -1 in case of malformed data or other errors. */
extern ssize_t dwelf_elf_gnu_build_id (Elf *elf, const void **build_idp);
+/* Returns the size of the uncompressed data of a GNU compressed
+ section. The section name should start with .zdebug (but this
+ isn't checked by this function). If the section isn't compressed
+ (the section data doesn't start with ZLIB) -1 is returned. If an
+ error occured -1 is returned and elf_errno is set. */
+extern ssize_t dwelf_scn_gnu_compressed_size (Elf_Scn *scn);
+
#ifdef __cplusplus
}
#endif