public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: "Martin Liška" <mliska@suse.cz>
Cc: Fangrui Song <maskray@google.com>, binutils@sourceware.org
Subject: Re: [PATCH] readelf: support zstd compressed debug sections [PR 29640]
Date: Fri, 14 Oct 2022 22:39:17 +1030	[thread overview]
Message-ID: <Y0lRbWe0tVzEQCwG@squeak.grove.modra.org> (raw)
In-Reply-To: <Y0lH6Ym3G0FBsfxd@squeak.grove.modra.org>

On Fri, Oct 14, 2022 at 09:58:41PM +1030, Alan Modra wrote:
> So we have a zlib-gabi .debug_info section that increases in size with
> zstd, so much so that it's better to leave the section uncompressed.
> Things go horribly wrong due to leaving compress_status as
> COMPRESS_SECTION_NONE.  The section is read again off disk using the
> uncompressed size.  So you get the zlib section again with some
> garbage at the end.
> 
> Also, if the section is to be left uncompressed, the input
> SHF_COMPRESSED flag needs to be reset otherwise it is copied to
> output.
> 
> I'm not ready to commit this, just thought I'd post the results of a
> bit of debugging.

And if I'd run the testsuite before posting, I may have posted a
better patch..  Using COMPRESS_SECTION_DONE led to .debug -> .zdebug
renaming of sections, so it appears we want another compress_status
that says the final section contents are in sec->contents.

diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 25e1806e689..4f4658021c6 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -974,11 +974,12 @@ typedef struct bfd_section
   unsigned int gc_mark : 1;
 
   /* Section compression status.  */
-  unsigned int compress_status : 2;
+  unsigned int compress_status : 3;
 #define COMPRESS_SECTION_NONE    0
 #define COMPRESS_SECTION_DONE    1
 #define DECOMPRESS_SECTION_ZLIB  2
 #define DECOMPRESS_SECTION_ZSTD  3
+#define DECOMPRESS_SECTION_DONE  4
 
   /* The following flags are used by the ELF linker. */
 
diff --git a/bfd/compress.c b/bfd/compress.c
index 364df14142b..94bc5412a89 100644
--- a/bfd/compress.c
+++ b/bfd/compress.c
@@ -151,7 +151,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
 	  free (input_buffer);
 	  bfd_set_section_alignment (sec, uncompressed_alignment_pow);
 	  sec->contents = buffer;
-	  sec->compress_status = COMPRESS_SECTION_DONE;
+	  sec->compress_status = DECOMPRESS_SECTION_DONE;
 	  sec->size = uncompressed_size;
 	  input_buffer = buffer;
 	}
@@ -206,7 +206,8 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
   if (compressed_size >= uncompressed_size)
     {
       memcpy (buffer, input_buffer, uncompressed_size);
-      sec->compress_status = COMPRESS_SECTION_NONE;
+      elf_section_flags (sec) &= ~SHF_COMPRESSED;
+      sec->compress_status = DECOMPRESS_SECTION_DONE;
     }
   else
     {
@@ -361,6 +362,7 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
       return true;
 
     case COMPRESS_SECTION_DONE:
+    case DECOMPRESS_SECTION_DONE:
       if (sec->contents == NULL)
 	return false;
       if (p == NULL)
diff --git a/bfd/section.c b/bfd/section.c
index 614570e976e..52cf7e042cd 100644
--- a/bfd/section.c
+++ b/bfd/section.c
@@ -389,11 +389,12 @@ CODE_FRAGMENT
 .  unsigned int gc_mark : 1;
 .
 .  {* Section compression status.  *}
-.  unsigned int compress_status : 2;
+.  unsigned int compress_status : 3;
 .#define COMPRESS_SECTION_NONE    0
 .#define COMPRESS_SECTION_DONE    1
 .#define DECOMPRESS_SECTION_ZLIB  2
 .#define DECOMPRESS_SECTION_ZSTD  3
+.#define DECOMPRESS_SECTION_DONE  4
 .
 .  {* The following flags are used by the ELF linker. *}
 .


-- 
Alan Modra
Australia Development Lab, IBM

  reply	other threads:[~2022-10-14 12:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-01  6:20 Fangrui Song
2022-10-06  2:20 ` Fangrui Song
2022-10-20  8:04   ` Martin Liška
2022-10-20  8:05   ` Martin Liška
2022-10-13 11:38 ` Martin Liška
2022-10-14  3:35   ` Fangrui Song
2022-10-14  7:47     ` Martin Liška
2022-10-14  8:26       ` Martin Liška
2022-10-14 11:28         ` Alan Modra
2022-10-14 12:09           ` Alan Modra [this message]
2022-10-16  4:42             ` Alan Modra
2022-10-16 20:46               ` Fangrui Song
2022-10-21  9:16                 ` Alan Modra
2022-10-17  7:52               ` 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=Y0lRbWe0tVzEQCwG@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=maskray@google.com \
    --cc=mliska@suse.cz \
    /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).