From: "Martin Liška" <mliska@suse.cz>
To: Mark Wielaard <mark@klomp.org>
Cc: elfutils-devel@sourceware.org
Subject: Re: [PATCHv2] support ZSTD compression algorithm
Date: Mon, 19 Dec 2022 15:21:19 +0100 [thread overview]
Message-ID: <53071eba-0cbc-fb7e-c78c-dfc52cc2843f@suse.cz> (raw)
In-Reply-To: <Y5u8gdTqkxEI3FAK@wildebeest.org>
Hi.
>> + if (use_zstd)
>> +#ifdef USE_ZSTD
>> + return __libelf_compress_zstd (scn, hsize, ei_data, orig_size,
>> + orig_addralign, new_size, force,
>> + data, next_data, out_buf, out_size,
>> + block);
>> +#else
>> + return NULL;
>
> Should this also call __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE) ?
Yes, will fix that.
>
>> +#endif
>> + else
>> + return __libelf_compress_zlib (scn, hsize, ei_data, orig_size,
>> + orig_addralign, new_size, force,
>> + data, next_data, out_buf, out_size,
>> + block);
>> +}
>> +
>> +void *
>> +internal_function
>> +__libelf_decompress_zlib (void *buf_in, size_t size_in, size_t size_out)
>> {
>> /* Catch highly unlikely compression ratios so we don't allocate
>> some giant amount of memory for nothing. The max compression
>> @@ -218,7 +360,7 @@ __libelf_decompress (void *buf_in, size_t size_in, size_t size_out)
>> return NULL;
>> }
>> - /* Malloc might return NULL when requestion zero size. This is highly
>> + /* Malloc might return NULL when requesting zero size. This is highly
>> unlikely, it would only happen when the compression was forced.
>> But we do need a non-NULL buffer to return and set as result.
>> Just make sure to always allocate at least 1 byte. */
>
> OK, typo fix.
>
>> @@ -260,6 +402,51 @@ __libelf_decompress (void *buf_in, size_t size_in, size_t size_out)
>> return buf_out;
>> }
>> +#ifdef USE_ZSTD
>> +void *
>> +internal_function
>> +__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
>> + unlikely, it would only happen when the compression was forced.
>> + But we do need a non-NULL buffer to return and set as result.
>> + Just make sure to always allocate at least 1 byte. */
>> + void *buf_out = malloc (size_out ?: 1);
>> + if (unlikely (buf_out == NULL))
>> + {
>> + __libelf_seterrno (ELF_E_NOMEM);
>> + return NULL;
>> + }
>> +
>> + size_t ret = ZSTD_decompress (buf_out, size_out, buf_in, size_in);
>> + if (ZSTD_isError (ret))
>> + {
>> + free (buf_out);
>> + __libelf_seterrno (ELF_E_DECOMPRESS_ERROR);
>> + return NULL;
>> + }
>> + else
>> + return buf_out;
>> +}
>> +#endif
>
> OK.
>
>> +void *
>> +internal_function
>> +__libelf_decompress (int chtype, void *buf_in, size_t size_in, size_t size_out)
>> +{
>> + if (chtype == ELFCOMPRESS_ZLIB)
>> + return __libelf_decompress_zlib (buf_in, size_in, size_out);
>> + else
>> + {
>> +#ifdef USE_ZSTD
>> + return __libelf_decompress_zstd (buf_in, size_in, size_out);
>> +#else
>> + __libelf_seterrno (ELF_E_DECOMPRESS_ERROR);
>
> Maybe ELF_E_UNKNOWN_COMPRESSION_TYPE ?
Yes, will fix that.
>
>> + return NULL;
>> +#endif
>> + }
>> +}
>> +
>> void *
>> internal_function
>> __libelf_decompress_elf (Elf_Scn *scn, size_t *size_out, size_t *addralign)
>> @@ -268,7 +455,7 @@ __libelf_decompress_elf (Elf_Scn *scn, size_t *size_out, size_t *addralign)
>> if (gelf_getchdr (scn, &chdr) == NULL)
>> return NULL;
>> - if (chdr.ch_type != ELFCOMPRESS_ZLIB)
>> + if (chdr.ch_type != ELFCOMPRESS_ZLIB && chdr.ch_type != ELFCOMPRESS_ZSTD)
>> {
>> __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);
>> return NULL;
>
> Should the chdr.ch_type != ELFCOMPRESS_ZSTD be guarded by #ifdef USE_ZSTD ?
Yes, will fix that.
>> + else
>> + error (0, 0, "Couldn't get chdr for section %zd", ndx);
>
> Shouldn't this error be fatal?
What do you use for fatal errors?
>
>> @@ -1378,7 +1404,7 @@ main (int argc, char **argv)
>> N_("Place (de)compressed output into FILE"),
>> 0 },
>> { "type", 't', "TYPE", 0,
>> - N_("What type of compression to apply. TYPE can be 'none' (decompress), 'zlib' (ELF ZLIB compression, the default, 'zlib-gabi' is an alias) or 'zlib-gnu' (.zdebug GNU style compression, 'gnu' is an alias)"),
>> + N_("What type of compression to apply. TYPE can be 'none' (decompress), 'zlib' (ELF ZLIB compression, the default, 'zlib-gabi' is an alias), 'zlib-gnu' (.zdebug GNU style compression, 'gnu' is an alias) or 'zstd'"),
>> 0 },
>> { "name", 'n', "SECTION", 0,
>> N_("SECTION name to (de)compress, SECTION is an extended wildcard pattern (defaults to '.?(z)debug*')"),
>
> I would say or 'zstd' (ELF ZSTD compression)" to match the 'zlib; type
> description.
Works for me.
Cheers,
Martin
>
>> diff --git a/src/readelf.c b/src/readelf.c
>> index cc3e0229..451f8400 100644
>> --- a/src/readelf.c
>> +++ b/src/readelf.c
>> @@ -1238,13 +1238,17 @@ get_visibility_type (int value)
>> static const char *
>> elf_ch_type_name (unsigned int code)
>> {
>> - if (code == 0)
>> - return "NONE";
>> -
>> - if (code == ELFCOMPRESS_ZLIB)
>> - return "ZLIB";
>> -
>> - return "UNKNOWN";
>> + switch (code)
>> + {
>> + case 0:
>> + return "NONE";
>> + case ELFCOMPRESS_ZLIB:
>> + return "ZLIB";
>> + case ELFCOMPRESS_ZSTD:
>> + return "ZSTD";
>> + default:
>> + return "UNKNOWN";
>> + }
>> }
>> /* Print the section headers. */
>
> OK.
>
>> diff --git a/tests/run-compress-test.sh b/tests/run-compress-test.sh
>> index a6a298f5..3f9c990e 100755
>> --- a/tests/run-compress-test.sh
>> +++ b/tests/run-compress-test.sh
>> @@ -61,6 +61,30 @@ testrun_elfcompress_file()
>> echo "uncompress $elfcompressedfile -> $elfuncompressedfile"
>> testrun ${abs_top_builddir}/src/elfcompress -v -t none -o ${elfuncompressedfile} ${elfcompressedfile}
>> testrun ${abs_top_builddir}/src/elfcmp ${uncompressedfile} ${elfuncompressedfile}
>> +
>> + outputfile="${infile}.gabi.zstd"
>> + tempfiles "$outputfile"
>> + echo "zstd compress $elfcompressedfile -> $outputfile"
>> + testrun ${abs_top_builddir}/src/elfcompress -v -t zstd -o ${outputfile} ${elfcompressedfile}
>> + testrun ${abs_top_builddir}/src/elfcmp ${uncompressedfile} ${outputfile}
>> + echo "checking compressed section header" $outputfile
>> + testrun ${abs_top_builddir}/src/readelf -Sz ${outputfile} | grep "ELF ZSTD" >/dev/null
>> +
>> + zstdfile="${infile}.zstd"
>> + tempfiles "$zstdfile"
>> + echo "zstd compress $uncompressedfile -> $zstdfile"
>> + testrun ${abs_top_builddir}/src/elfcompress -v -t zstd -o ${zstdfile} ${elfuncompressedfile}
>> + testrun ${abs_top_builddir}/src/elfcmp ${uncompressedfile} ${zstdfile}
>> + echo "checking compressed section header" $zstdfile
>> + testrun ${abs_top_builddir}/src/readelf -Sz ${zstdfile} | grep "ELF ZSTD" >/dev/null
>> +
>> + zstdgnufile="${infile}.zstd.gnu"
>> + tempfiles "$zstdgnufile"
>> + echo "zstd re-compress to GNU ZLIB $zstdfile -> $zstdgnufile"
>> + testrun ${abs_top_builddir}/src/elfcompress -v -t zlib-gnu -o ${zstdgnufile} ${zstdfile}
>> + testrun ${abs_top_builddir}/src/elfcmp ${uncompressedfile} ${zstdgnufile}
>> + echo "checking .zdebug section name" $zstdgnufile
>> + testrun ${abs_top_builddir}/src/readelf -S ${zstdgnufile} | grep ".zdebug" >/dev/null
>> }
>> testrun_elfcompress()
>
> You might add these to a separate run test file or pass some ZSTD flag
> through the test environment to conditionally run these new tests.
>
> Cheers,
>
> Mark
next prev parent reply other threads:[~2022-12-19 14:21 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-24 11:09 [PATCH][RFC] readelf: partial support of ZSTD compression Martin Liška
2022-10-24 11:41 ` Dmitry V. Levin
2022-10-24 12:17 ` Martin Liška
2022-10-24 16:48 ` Dmitry V. Levin
2022-10-24 18:16 ` Martin Liška
2022-10-28 22:21 ` Mark Wielaard
2022-11-28 13:16 ` Martin Liška
2022-11-28 22:29 ` Mark Wielaard
2022-11-29 9:34 ` Martin Liška
2022-11-29 12:05 ` [PATCHv2] support ZSTD compression algorithm Martin Liška
2022-12-09 10:17 ` Martin Liška
2022-12-15 13:17 ` Mark Wielaard
2022-12-19 14:19 ` Martin Liška
2022-12-19 15:09 ` Mark Wielaard
2022-12-21 11:13 ` Martin Liška
2023-01-10 17:44 ` [PATCH] readelf: Check compression status of .debug section data Mark Wielaard
2023-01-16 19:39 ` Mark Wielaard
2022-12-16 0:32 ` [PATCHv2] support ZSTD compression algorithm Mark Wielaard
2022-12-19 14:21 ` Martin Liška [this message]
2022-12-19 15:16 ` Mark Wielaard
2022-12-21 11:09 ` Martin Liška
2022-12-21 23:14 ` Mark Wielaard
2022-12-22 9:17 ` Martin Liška
2022-12-22 18:36 ` Mark Wielaard
2022-12-23 8:44 ` Martin Liška
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=53071eba-0cbc-fb7e-c78c-dfc52cc2843f@suse.cz \
--to=mliska@suse.cz \
--cc=elfutils-devel@sourceware.org \
--cc=mark@klomp.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).