On Thu, 2022-12-22 at 10:09 +0100, Martin Liška wrote: > On 12/22/22 00:38, Mark Wielaard wrote: > > Hi Martin, > > > > On Wed, Dec 21, 2022 at 07:29:19PM +0100, Martin Liška wrote: > > > > What goes wrong is that this debian old stable arm setup has > > > > libzstd > > > > containing a ZSTD_compressStream2 symbol... > > > > > > > > 40: 0000d349 264 FUNC GLOBAL DEFAULT 11 > > > > ZSTD_compressStream2 > > > > > > > > But the zstd.h header doesn't expose it... > > > > > > Yeah, that's their way of how to move an API from "staging" into > > > a stable state. > > > It was changed in: > > > https://github.com/facebook/zstd/commit/d7d89513d6a21 > > > > > > and so it's present in zstd since 1.4.0. Can we somehow specify > > > library version > > > in configure.ac? > > Hi Mark. > > > It isn't as nice, but since there seems to be a pkgconfig > > libzstd.pc > > you could use PKG_CHECK_MODULES. See for example how we check for > > libcurl. > > I can see it, however, it not easily addable to eu_ZIPLIB function :/ > I must confess, > this autoconf machinery is always something that makes me crazy :) > > Do you have an elegant way how to handle it?
Maybe we should simply split it from the "decompress" support. Trying to make eu_ZIPLIB handle it means we also suddenly require a newer libzstd for the simpler dwfl zstd file decompression support. So lets keep the "normal" zstd detection in place and add something like the attached (on top of your latest patch). Cheers, Mark
diff --git a/configure.ac b/configure.ac index ef16f79e..b2597f8b 100644 --- a/configure.ac +++ b/configure.ac @@ -417,7 +417,7 @@ AC_SUBST([BZ2_LIB]) eu_ZIPLIB(lzma,LZMA,lzma,lzma_auto_decoder,[LZMA (xz)]) AS_IF([test "x$with_lzma" = xyes], [LIBLZMA="liblzma"], [LIBLZMA=""]) AC_SUBST([LIBLZMA]) -eu_ZIPLIB(zstd,ZSTD,zstd,ZSTD_compressStream2,[ZSTD (zst)]) +eu_ZIPLIB(zstd,ZSTD,zstd,ZSTD_decompress,[ZSTD (zst)]) AS_IF([test "x$with_zstd" = xyes], [LIBZSTD="libzstd"], [LIBLZSTD=""]) AC_SUBST([LIBZSTD]) zstd_LIBS="$LIBS" @@ -426,6 +426,16 @@ zip_LIBS="$LIBS" LIBS="$save_LIBS" AC_SUBST([zip_LIBS]) +dnl zstd compression support requires libzstd 1.4.0+ +AS_IF([test "x$with_zstd" = xyes], [ + PKG_PROG_PKG_CONFIG + PKG_CHECK_MODULES([ZSTD_COMPRESS],[libzstd >= 1.4.0], + [with_zstd_compress="yes"],[with_zstd_compress="no"])], + [with_zstd_compress="no"]) +AM_CONDITIONAL(USE_ZSTD_COMPRESS, test "x$with_zstd_compress" = "xyes") +AS_IF([test "x$with_zstd_compress" = "xyes"], + [AC_DEFINE([USE_ZSTD_COMPRESS], [1], [zstd compression support])]) + AC_CHECK_DECLS([memrchr, rawmemchr],[],[], [#define _GNU_SOURCE #include <string.h>]) @@ -831,6 +841,7 @@ AC_MSG_NOTICE([ bzip2 support : ${with_bzlib} lzma/xz support : ${with_lzma} zstd support : ${with_zstd} + zstd compression support : ${with_zstd_compress} libstdc++ demangle support : ${enable_demangler} File textrel check : ${enable_textrelcheck} Symbol versioning : ${enable_symbol_versioning} diff --git a/libelf/elf_compress.c b/libelf/elf_compress.c index deb585b7..5a9d3da1 100644 --- a/libelf/elf_compress.c +++ b/libelf/elf_compress.c @@ -170,7 +170,7 @@ __libelf_compress_zlib (Elf_Scn *scn, size_t hsize, int ei_data, return out_buf; } -#ifdef USE_ZSTD +#ifdef USE_ZSTD_COMPRESS /* Cleanup and return result. Don't leak memory. */ static void * do_zstd_cleanup (void *result, ZSTD_CCtx * const cctx, void *out_buf, @@ -333,7 +333,7 @@ __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data, if (use_zstd) { -#ifdef USE_ZSTD +#ifdef USE_ZSTD_COMPRESS return __libelf_compress_zstd (scn, hsize, ei_data, orig_size, orig_addralign, new_size, force, data, next_data, out_buf, out_size, @@ -406,8 +406,7 @@ __libelf_decompress_zlib (void *buf_in, size_t size_in, size_t size_out) } #ifdef USE_ZSTD -void * -internal_function +static void * __libelf_decompress_zstd (void *buf_in, size_t size_in, size_t size_out) { /* Malloc might return NULL when requesting zero size. This is highly @@ -425,7 +424,7 @@ __libelf_decompress_zstd (void *buf_in, size_t size_in, size_t size_out) if (ZSTD_isError (ret)) { free (buf_out); - __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE); + __libelf_seterrno (ELF_E_DECOMPRESS_ERROR); return NULL; } else @@ -444,7 +443,7 @@ __libelf_decompress (int chtype, void *buf_in, size_t size_in, size_t size_out) #ifdef USE_ZSTD return __libelf_decompress_zstd (buf_in, size_in, size_out); #else - __libelf_seterrno (ELF_E_DECOMPRESS_ERROR); + __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE); return NULL; #endif } diff --git a/src/elfcompress.c b/src/elfcompress.c index bfdac2b4..1f32331c 100644 --- a/src/elfcompress.c +++ b/src/elfcompress.c @@ -230,7 +230,8 @@ compress_section (Elf_Scn *scn, size_t orig_size, const char *name, res = elf_compress (scn, dchtype, flags); if (res < 0) - error (0, 0, "Couldn't decompress section [%zd] %s: %s", + error (0, 0, "Couldn't %s section [%zd] %s: %s", + compress ? "compress" : "decompress", ndx, name, elf_errmsg (-1)); else { diff --git a/tests/Makefile.am b/tests/Makefile.am index cb70229e..71b19601 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -218,6 +218,9 @@ endif if HAVE_ZSTD TESTS += run-readelf-compressed-zstd.sh +endif + +if USE_ZSTD_COMPRESS export ELFUTILS_ZSTD = 1 endif