Skip to content

Commit

Permalink
libbacktrace: support zstd decompression
Browse files Browse the repository at this point in the history
	Support decompressing --compress-debug-sections=zstd.
	* configure.ac: Check for zstd library and
	--compress-debug-sections=zstd linker option.
	* Makefile.am (zstdtest_*): New targets.
	(zstdtest_alloc_*, ctestzstd_*): New targets.
	(BUILDTESTS): Add zstdtest, zstdtest_alloc, ctestzstd as
	appropriate.
	* elf.c (ELFCOMPRESS_ZSTD): Define.
	(elf_fetch_bits): Rename from elf_zlib_fetch.  Update uses.
	(elf_fetch_bits_backward): New static function.
	(ZLIB_HUFFMAN_*): Rename from HUFFMAN_*.  Update uses.
	(ZLIB_TABLE_*): Rename from ZDEBUG_TABLE_*.  Update uses.
	(ZSTD_TABLE_*): Define.
	(struct elf_zstd_fse_entry): Define.
	(elf_zstd_read_fse): New static function.
	(elf_zstd_build_fse): Likewise.
	(lit): Define if BACKTRACE_GENERATE_ZSTD_FSE_TABLES.
	(match, offset, next, print_table, main): Likewise.
	(elf_zstd_lit_table): New static const array.
	(elf_zstd_match_table, elf_zstd_offset_table): Likewise.
	(elf_zstd_read_huff): New static function.
	(struct elf_zstd_seq_decode): Define.
	(elf_zstd_unpack_seq_decode): New static function.
	(ZSTD_LIT_*): Define.
	(struct elf_zstd_literals): Define.
	(elf_zstd_literal_output): New static function.
	(ZSTD_LITERAL_LENGTH_BASELINE_OFFSET): Define.
	(elf_zstd_literal_length_baseline): New static const array.
	(elf_zstd_literal_length_bits): Likewise.
	(ZSTD_MATCH_LENGTH_BASELINE_OFFSET): Define.
	(elf_zstd_match_length_baseline): New static const array.
	(elf_zstd_match_length_bits): Likewise.
	(elf_zstd_decompress): New static function.
	(ZDEBUG_TABLE_SIZE): New definition.
	(elf_uncompress_chdr): Support ELF_COMPRESS_ZSTD.
	(backtrace_uncompress_zstd): New function.
	(elf_add): Use ZLIB_TABLE_SIZE for zlib-gnu sections.
	* internal.h (backtrace_uncompress_zstd): Declare.
	* zstdtest.c: New file.
	* configure, config.h.in, Makefile.in: Regenerate.
  • Loading branch information
ianlancetaylor committed Dec 8, 2022
1 parent 4bc2d9f commit 9df1ba9
Show file tree
Hide file tree
Showing 8 changed files with 3,352 additions and 297 deletions.
30 changes: 30 additions & 0 deletions libbacktrace/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,25 @@ ztest_alloc_CFLAGS = $(ztest_CFLAGS)

BUILDTESTS += ztest_alloc

zstdtest_SOURCES = zstdtest.c testlib.c
zstdtest_CFLAGS = $(libbacktrace_TEST_CFLAGS) -DSRCDIR=\"$(srcdir)\"
zstdtest_LDADD = libbacktrace.la
zstdtest_alloc_LDADD = libbacktrace_alloc.la

if HAVE_ZSTD
zstdtest_LDADD += -lzstd
zstdtest_alloc_LDADD += -lzstd
endif
zstdtest_LDADD += $(CLOCK_GETTIME_LINK)
zstdtest_alloc_LDADD += $(CLOCK_GETTIME_LINK)

BUILDTESTS += zstdtest

zstdtest_alloc_SOURCES = $(zstdtest_SOURCES)
zstdtest_alloc_CFLAGS = $(zstdtest_CFLAGS)

BUILDTESTS += zstdtest_alloc

endif HAVE_ELF

edtest_SOURCES = edtest.c edtest2_build.c testlib.c
Expand Down Expand Up @@ -450,6 +469,17 @@ ctesta_LDADD = libbacktrace.la

BUILDTESTS += ctestg ctesta

if HAVE_COMPRESSED_DEBUG_ZSTD

ctestzstd_SOURCES = btest.c testlib.c
ctestzstd_CFLAGS = $(libbacktrace_TEST_CFLAGS)
ctestzstd_LDFLAGS = -Wl,--compress-debug-sections=zstd
ctestzstd_LDADD = libbacktrace.la

BUILDTESTS += ctestzstd

endif

ctestg_alloc_SOURCES = $(ctestg_SOURCES)
ctestg_alloc_CFLAGS = $(ctestg_CFLAGS)
ctestg_alloc_LDFLAGS = $(ctestg_LDFLAGS)
Expand Down
198 changes: 160 additions & 38 deletions libbacktrace/Makefile.in

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions libbacktrace/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
/* Define if -lz is available. */
#undef HAVE_ZLIB

/* Define if -lzstd is available. */
#undef HAVE_ZSTD

/* Define to the sub-directory in which libtool stores uninstalled libraries.
*/
#undef LT_OBJDIR
Expand Down
105 changes: 103 additions & 2 deletions libbacktrace/configure
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,10 @@ HAVE_OBJCOPY_DEBUGLINK_FALSE
HAVE_OBJCOPY_DEBUGLINK_TRUE
READELF
OBJCOPY
HAVE_COMPRESSED_DEBUG_ZSTD_FALSE
HAVE_COMPRESSED_DEBUG_ZSTD_TRUE
HAVE_ZSTD_FALSE
HAVE_ZSTD_TRUE
HAVE_COMPRESSED_DEBUG_FALSE
HAVE_COMPRESSED_DEBUG_TRUE
HAVE_ZLIB_FALSE
Expand Down Expand Up @@ -11535,7 +11539,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
#line 11538 "configure"
#line 11542 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
Expand Down Expand Up @@ -11641,7 +11645,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
#line 11644 "configure"
#line 11648 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
Expand Down Expand Up @@ -13776,6 +13780,95 @@ else
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ZSTD_compress in -lzstd" >&5
$as_echo_n "checking for ZSTD_compress in -lzstd... " >&6; }
if ${ac_cv_lib_zstd_ZSTD_compress+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lzstd $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char ZSTD_compress ();
int
main ()
{
return ZSTD_compress ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_zstd_ZSTD_compress=yes
else
ac_cv_lib_zstd_ZSTD_compress=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_zstd_ZSTD_compress" >&5
$as_echo "$ac_cv_lib_zstd_ZSTD_compress" >&6; }
if test "x$ac_cv_lib_zstd_ZSTD_compress" = xyes; then :
$as_echo "#define HAVE_ZSTD 1" >>confdefs.h
fi
if test "$ac_cv_lib_zstd_ZSTD_compress" = yes; then
HAVE_ZSTD_TRUE=
HAVE_ZSTD_FALSE='#'
else
HAVE_ZSTD_TRUE='#'
HAVE_ZSTD_FALSE=
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether --compress-debug-sections=zstd is supported" >&5
$as_echo_n "checking whether --compress-debug-sections=zstd is supported... " >&6; }
if ${libgo_cv_ld_compress_zstd+:} false; then :
$as_echo_n "(cached) " >&6
else
LDFLAGS_hold=$LDFLAGS
LDFLAGS="$LDFLAGS -Wl,--compress-debug-sections=zstd"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
int
main ()
{
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
libgo_cv_ld_compress_zstd=yes
else
libgo_cv_ld_compress_zstd=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LDFLAGS=$LDFLAGS_hold
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libgo_cv_ld_compress_zstd" >&5
$as_echo "$libgo_cv_ld_compress_zstd" >&6; }
if test "$libgo_cv_ld_compress_zstd" = yes; then
HAVE_COMPRESSED_DEBUG_ZSTD_TRUE=
HAVE_COMPRESSED_DEBUG_ZSTD_FALSE='#'
else
HAVE_COMPRESSED_DEBUG_ZSTD_TRUE='#'
HAVE_COMPRESSED_DEBUG_ZSTD_FALSE=
fi
# Extract the first word of "objcopy", so it can be a program name with args.
set dummy objcopy; ac_word=$2
Expand Down Expand Up @@ -14322,6 +14415,14 @@ if test -z "${HAVE_COMPRESSED_DEBUG_TRUE}" && test -z "${HAVE_COMPRESSED_DEBUG_F
as_fn_error $? "conditional \"HAVE_COMPRESSED_DEBUG\" was never defined.
Usually this means the macro was only invoked conditionally." "$LINENO" 5
fi
if test -z "${HAVE_ZSTD_TRUE}" && test -z "${HAVE_ZSTD_FALSE}"; then
as_fn_error $? "conditional \"HAVE_ZSTD\" was never defined.
Usually this means the macro was only invoked conditionally." "$LINENO" 5
fi
if test -z "${HAVE_COMPRESSED_DEBUG_ZSTD_TRUE}" && test -z "${HAVE_COMPRESSED_DEBUG_ZSTD_FALSE}"; then
as_fn_error $? "conditional \"HAVE_COMPRESSED_DEBUG_ZSTD\" was never defined.
Usually this means the macro was only invoked conditionally." "$LINENO" 5
fi
if test -z "${HAVE_OBJCOPY_DEBUGLINK_TRUE}" && test -z "${HAVE_OBJCOPY_DEBUGLINK_FALSE}"; then
as_fn_error $? "conditional \"HAVE_OBJCOPY_DEBUGLINK\" was never defined.
Usually this means the macro was only invoked conditionally." "$LINENO" 5
Expand Down
15 changes: 15 additions & 0 deletions libbacktrace/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,21 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM(,)],
LDFLAGS=$LDFLAGS_hold])
AM_CONDITIONAL(HAVE_COMPRESSED_DEBUG, test "$libgo_cv_ld_compress" = yes)

AC_CHECK_LIB([zstd], [ZSTD_compress],
[AC_DEFINE(HAVE_ZSTD, 1, [Define if -lzstd is available.])])
AM_CONDITIONAL(HAVE_ZSTD, test "$ac_cv_lib_zstd_ZSTD_compress" = yes)

dnl Test whether the linker supports --compress-debug-sections=zstd option.
AC_CACHE_CHECK([whether --compress-debug-sections=zstd is supported],
[libgo_cv_ld_compress_zstd],
[LDFLAGS_hold=$LDFLAGS
LDFLAGS="$LDFLAGS -Wl,--compress-debug-sections=zstd"
AC_LINK_IFELSE([AC_LANG_PROGRAM(,)],
[libgo_cv_ld_compress_zstd=yes],
[libgo_cv_ld_compress_zstd=no])
LDFLAGS=$LDFLAGS_hold])
AM_CONDITIONAL(HAVE_COMPRESSED_DEBUG_ZSTD, test "$libgo_cv_ld_compress_zstd" = yes)

AC_ARG_VAR(OBJCOPY, [location of objcopy])
AC_CHECK_PROG(OBJCOPY, objcopy, objcopy,)
AC_CHECK_PROG(READELF, readelf, readelf)
Expand Down
Loading

0 comments on commit 9df1ba9

Please sign in to comment.