[PATCH v2 1/3] bfd: fix reading compressed debug sections of pei-* images

Tobias Deiminger tobias.deiminger@linutronix.de
Thu Nov 26 08:51:53 GMT 2026


Since 0921b99dafd5 ("PR 33033, Support compressed debug sections larger
than 4 GiB"), compressed debug sections of pei-* images can no longer be
read. For example:

 $ x86_64-w64-mingw32-gcc -g -o testprog testprog.c
 $ objcopy --compress-debug-sections testprog.exe testprog_z.exe
 $ gdb --batch -ex "break fn" -ex "info line fn" testprog_z.exe
 warning: Can't read data for section '.zdebug_info' in file '/.../testprog_z.exe'
 ...
 No line number information available for address 0x140001490 <fn>

It worked prior to 0921b99dafd5, because padding that generally exists
after zlib streams in compressed PE image sections was tolerated during
decompression. 0921b99dafd5 turned padding into an error condition, most
likely unintentionally, and decompression has failed since then.

The padding is an expected consequence of PE having no section field
that could communicate the compressed size. See PR 18087 for discussion
and withdrawn alternatives.

Fix it by restoring support for padding after compressed streams, and
additionally sanitize decompression by allowing only zero padding, and
by allowing it only for non-ELF files.

Signed-off-by: Tobias Deiminger <tobias.deiminger@linutronix.de>
---
 bfd/compress.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/bfd/compress.c b/bfd/compress.c
index c9710178f39..21ccd36ac0f 100644
--- a/bfd/compress.c
+++ b/bfd/compress.c
@@ -525,7 +525,8 @@ static bool
 decompress_contents (bool is_zstd, bfd_byte *compressed_buffer,
 		     bfd_size_type compressed_size,
 		     bfd_byte *uncompressed_buffer,
-		     bfd_size_type uncompressed_size)
+		     bfd_size_type uncompressed_size,
+		     bool allow_padding)
 {
   if (is_zstd)
     {
@@ -552,7 +553,21 @@ decompress_contents (bool is_zstd, bfd_byte *compressed_buffer,
       compressed_size -= src_len;
     }
   while (compressed_size > 0 && uncompressed_size > 0);
-  return compressed_size == 0 && uncompressed_size == 0;
+
+  if (allow_padding)
+    {
+      while (compressed_size > 0)
+	{
+	  if (*compressed_buffer != 0)
+	    return false;
+	  compressed_buffer++;
+	  compressed_size--;
+	}
+    }
+  else if (compressed_size > 0)
+    return false;
+
+  return uncompressed_size == 0;
 }
 
 /* Compress section contents using zlib/zstd and store
@@ -617,10 +632,13 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
 	  buffer = bfd_malloc (buffer_size);
 	  if (buffer == NULL)
 	    return (bfd_size_type) -1;
+	  bool allow_padding
+	    = bfd_get_flavour (abfd) != bfd_target_elf_flavour;
 
 	  if (!decompress_contents (ch_type == ch_compress_zstd,
 				    input_buffer + orig_header_size,
-				    zlib_size, buffer, buffer_size))
+				    zlib_size, buffer, buffer_size,
+				    allow_padding))
 	    {
 	      bfd_set_error (bfd_error_bad_value);
 	      free (buffer);
@@ -816,9 +834,11 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
 	   SHF_COMPRESSED section.  */
 	compression_header_size = 12;
       bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
+      bool allow_padding = bfd_get_flavour (abfd) != bfd_target_elf_flavour;
       if (!decompress_contents (
 	      is_zstd, compressed_buffer + compression_header_size,
-	      sec->compressed_size - compression_header_size, p, readsz))
+	      sec->compressed_size - compression_header_size, p, readsz,
+	      allow_padding))
 	{
 	  bfd_set_error (bfd_error_bad_value);
 	  if (p != *ptr)
-- 
2.55.0



More information about the Binutils mailing list