1 /* Compressed section support (intended for debug sections).
2 Copyright (C) 2008-2022 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
29 #include "safe-ctype.h"
31 #define MAX_COMPRESSION_HEADER_SIZE 24
36 .bfd_debug_name_to_zdebug (bfd *abfd, const char *name)
38 . size_t len = strlen (name);
39 . char *new_name = (char *) bfd_alloc (abfd, len + 2);
40 . if (new_name == NULL)
44 . memcpy (new_name + 2, name + 1, len);
49 .bfd_zdebug_name_to_debug (bfd *abfd, const char *name)
51 . size_t len = strlen (name);
52 . char *new_name = (char *) bfd_alloc (abfd, len);
53 . if (new_name == NULL)
56 . memcpy (new_name + 1, name + 2, len - 1);
63 decompress_contents (bool is_zstd
, bfd_byte
*compressed_buffer
,
64 bfd_size_type compressed_size
,
65 bfd_byte
*uncompressed_buffer
,
66 bfd_size_type uncompressed_size
)
71 size_t ret
= ZSTD_decompress (uncompressed_buffer
, uncompressed_size
,
72 compressed_buffer
, compressed_size
);
73 return !ZSTD_isError (ret
);
80 /* It is possible the section consists of several compressed
81 buffers concatenated together, so we uncompress in a loop. */
82 /* PR 18313: The state field in the z_stream structure is supposed
83 to be invisible to the user (ie us), but some compilers will
84 still complain about it being used without initialisation. So
85 we first zero the entire z_stream structure and then set the fields
87 memset (& strm
, 0, sizeof strm
);
88 strm
.avail_in
= compressed_size
;
89 strm
.next_in
= (Bytef
*) compressed_buffer
;
90 strm
.avail_out
= uncompressed_size
;
91 /* FIXME: strm.avail_in and strm.avail_out are typically unsigned
92 int. Supporting sizes that don't fit in an unsigned int is
93 possible but will require some rewriting of this function. */
94 if (strm
.avail_in
!= compressed_size
|| strm
.avail_out
!= uncompressed_size
)
97 BFD_ASSERT (Z_OK
== 0);
98 rc
= inflateInit (&strm
);
99 while (strm
.avail_in
> 0 && strm
.avail_out
> 0)
103 strm
.next_out
= ((Bytef
*) uncompressed_buffer
104 + (uncompressed_size
- strm
.avail_out
));
105 rc
= inflate (&strm
, Z_FINISH
);
106 if (rc
!= Z_STREAM_END
)
108 rc
= inflateReset (&strm
);
110 return inflateEnd (&strm
) == Z_OK
&& rc
== Z_OK
&& strm
.avail_out
== 0;
113 /* Compress section contents using zlib/zstd and store
114 as the contents field. This function assumes the contents
115 field was allocated using bfd_malloc() or equivalent.
117 Return the uncompressed size if the full section contents is
118 compressed successfully. Otherwise return 0. */
121 bfd_compress_section_contents (bfd
*abfd
, sec_ptr sec
)
123 bfd_byte
*input_buffer
;
124 uLong compressed_size
;
126 bfd_size_type buffer_size
;
128 int orig_header_size
;
129 bfd_size_type uncompressed_size
;
130 unsigned int uncompressed_alignment_pow
;
131 unsigned int ch_type
= 0;
132 int new_header_size
= bfd_get_compression_header_size (abfd
, NULL
);
134 = bfd_is_section_compressed_info (abfd
, sec
,
137 &uncompressed_alignment_pow
,
141 /* We shouldn't be trying to decompress unsupported compressed sections. */
142 if (compressed
&& orig_header_size
< 0)
145 /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
146 overhead in .zdebug* section. */
147 if (!new_header_size
)
148 new_header_size
= 12;
150 orig_header_size
= 12;
152 input_buffer
= sec
->contents
;
155 zlib_size
= sec
->size
- orig_header_size
;
156 compressed_size
= zlib_size
+ new_header_size
;
158 /* If we are converting between zlib-gnu and zlib-gabi then the
159 compressed contents just need to be moved. */
160 update
= (ch_type
< ELFCOMPRESS_ZSTD
161 && (abfd
->flags
& BFD_COMPRESS_ZSTD
) == 0);
163 /* Uncompress when not just moving contents or when compressed
164 is not smaller than uncompressed. */
165 if (!update
|| compressed_size
>= uncompressed_size
)
167 buffer_size
= uncompressed_size
;
168 buffer
= bfd_malloc (buffer_size
);
172 if (!decompress_contents (ch_type
== ELFCOMPRESS_ZSTD
,
173 input_buffer
+ orig_header_size
,
174 zlib_size
, buffer
, buffer_size
))
176 bfd_set_error (bfd_error_bad_value
);
181 bfd_set_section_alignment (sec
, uncompressed_alignment_pow
);
182 sec
->contents
= buffer
;
183 sec
->flags
|= SEC_IN_MEMORY
;
184 sec
->compress_status
= COMPRESS_SECTION_NONE
;
185 sec
->size
= uncompressed_size
;
186 input_buffer
= buffer
;
191 compressed_size
= compressBound (uncompressed_size
) + new_header_size
;
193 buffer_size
= compressed_size
;
194 buffer
= bfd_alloc (abfd
, buffer_size
);
200 if (compressed_size
< uncompressed_size
)
201 memcpy (buffer
+ new_header_size
,
202 input_buffer
+ orig_header_size
,
207 if (abfd
->flags
& BFD_COMPRESS_ZSTD
)
210 compressed_size
= ZSTD_compress (buffer
+ new_header_size
,
214 ZSTD_CLEVEL_DEFAULT
);
215 if (ZSTD_isError (compressed_size
))
217 bfd_release (abfd
, buffer
);
218 bfd_set_error (bfd_error_bad_value
);
223 else if (compress ((Bytef
*) buffer
+ new_header_size
, &compressed_size
,
224 (const Bytef
*) input_buffer
, uncompressed_size
)
227 bfd_release (abfd
, buffer
);
228 bfd_set_error (bfd_error_bad_value
);
232 compressed_size
+= new_header_size
;
235 /* If compression didn't make the section smaller, keep it uncompressed. */
236 if (compressed_size
>= uncompressed_size
)
238 memcpy (buffer
, input_buffer
, uncompressed_size
);
239 elf_section_flags (sec
) &= ~SHF_COMPRESSED
;
240 sec
->compress_status
= COMPRESS_SECTION_NONE
;
244 sec
->size
= uncompressed_size
;
245 bfd_update_compression_header (abfd
, buffer
, sec
);
246 sec
->size
= compressed_size
;
247 sec
->compress_status
= COMPRESS_SECTION_DONE
;
249 sec
->contents
= buffer
;
250 sec
->flags
|= SEC_IN_MEMORY
;
252 return uncompressed_size
;
257 bfd_get_full_section_contents
260 bool bfd_get_full_section_contents
261 (bfd *abfd, asection *section, bfd_byte **ptr);
264 Read all data from @var{section} in BFD @var{abfd}, decompress
265 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
266 return @var{*ptr} with memory malloc'd by this function.
268 Return @code{TRUE} if the full section contents is retrieved
269 successfully. If the section has no contents then this function
270 returns @code{TRUE} but @var{*ptr} is set to NULL.
274 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
276 bfd_size_type sz
= bfd_get_section_limit_octets (abfd
, sec
);
279 bfd_size_type save_size
;
280 bfd_size_type save_rawsize
;
281 bfd_byte
*compressed_buffer
;
282 unsigned int compression_header_size
;
283 const unsigned int compress_status
= sec
->compress_status
;
292 && compress_status
!= COMPRESS_SECTION_DONE
293 && _bfd_section_size_insane (abfd
, sec
))
295 /* PR 24708: Avoid attempts to allocate a ridiculous amount
298 /* xgettext:c-format */
299 (_("error: %pB(%pA) is too large (%#" PRIx64
" bytes)"),
300 abfd
, sec
, (uint64_t) sz
);
304 switch (compress_status
)
306 case COMPRESS_SECTION_NONE
:
309 p
= (bfd_byte
*) bfd_malloc (sz
);
312 /* PR 20801: Provide a more helpful error message. */
313 if (bfd_get_error () == bfd_error_no_memory
)
315 /* xgettext:c-format */
316 (_("error: %pB(%pA) is too large (%#" PRIx64
" bytes)"),
317 abfd
, sec
, (uint64_t) sz
);
322 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
331 case DECOMPRESS_SECTION_ZLIB
:
332 case DECOMPRESS_SECTION_ZSTD
:
333 /* Read in the full compressed section contents. */
334 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
335 if (compressed_buffer
== NULL
)
337 save_rawsize
= sec
->rawsize
;
338 save_size
= sec
->size
;
339 /* Clear rawsize, set size to compressed size and set compress_status
340 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
341 the uncompressed size, bfd_get_section_contents will fail. */
343 sec
->size
= sec
->compressed_size
;
344 sec
->compress_status
= COMPRESS_SECTION_NONE
;
345 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
346 0, sec
->compressed_size
);
347 /* Restore rawsize and size. */
348 sec
->rawsize
= save_rawsize
;
349 sec
->size
= save_size
;
350 sec
->compress_status
= compress_status
;
352 goto fail_compressed
;
355 p
= (bfd_byte
*) bfd_malloc (sz
);
357 goto fail_compressed
;
359 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
360 if (compression_header_size
== 0)
361 /* Set header size to the zlib header size if it is a
362 SHF_COMPRESSED section. */
363 compression_header_size
= 12;
364 bool is_zstd
= compress_status
== DECOMPRESS_SECTION_ZSTD
;
365 if (!decompress_contents (
366 is_zstd
, compressed_buffer
+ compression_header_size
,
367 sec
->compressed_size
- compression_header_size
, p
, sz
))
369 bfd_set_error (bfd_error_bad_value
);
373 free (compressed_buffer
);
377 free (compressed_buffer
);
381 case COMPRESS_SECTION_DONE
:
382 if (sec
->contents
== NULL
)
386 p
= (bfd_byte
*) bfd_malloc (sz
);
391 /* PR 17512; file: 5bc29788. */
392 if (p
!= sec
->contents
)
393 memcpy (p
, sec
->contents
, sz
);
403 bfd_is_section_compressed_info
406 bool bfd_is_section_compressed_info
407 (bfd *abfd, asection *section,
408 int *compression_header_size_p,
409 bfd_size_type *uncompressed_size_p,
410 unsigned int *uncompressed_alignment_power_p,
411 unsigned int *ch_type);
414 Return @code{TRUE} if @var{section} is compressed. Compression
415 header size is returned in @var{compression_header_size_p},
416 uncompressed size is returned in @var{uncompressed_size_p}
417 and the uncompressed data alignement power is returned in
418 @var{uncompressed_align_pow_p}. If compression is
419 unsupported, compression header size is returned with -1
420 and uncompressed size is returned with 0.
424 bfd_is_section_compressed_info (bfd
*abfd
, sec_ptr sec
,
425 int *compression_header_size_p
,
426 bfd_size_type
*uncompressed_size_p
,
427 unsigned int *uncompressed_align_pow_p
,
428 unsigned int *ch_type
)
430 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
];
431 int compression_header_size
;
433 unsigned int saved
= sec
->compress_status
;
436 *uncompressed_align_pow_p
= 0;
438 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
439 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
441 header_size
= compression_header_size
? compression_header_size
: 12;
443 /* Don't decompress the section. */
444 sec
->compress_status
= COMPRESS_SECTION_NONE
;
446 /* Read the header. */
447 if (bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
449 if (compression_header_size
== 0)
450 /* In this case, it should be "ZLIB" followed by the uncompressed
451 section size, 8 bytes in big-endian order. */
452 compressed
= startswith ((char*) header
, "ZLIB");
459 *uncompressed_size_p
= sec
->size
;
462 if (compression_header_size
!= 0)
464 if (!bfd_check_compression_header (abfd
, header
, sec
, ch_type
,
466 uncompressed_align_pow_p
))
467 compression_header_size
= -1;
469 /* Check for the pathalogical case of a debug string section that
470 contains the string ZLIB.... as the first entry. We assume that
471 no uncompressed .debug_str section would ever be big enough to
472 have the first byte of its (big-endian) size be non-zero. */
473 else if (strcmp (sec
->name
, ".debug_str") == 0
474 && ISPRINT (header
[4]))
477 *uncompressed_size_p
= bfd_getb64 (header
+ 4);
480 /* Restore compress_status. */
481 sec
->compress_status
= saved
;
482 *compression_header_size_p
= compression_header_size
;
488 bfd_is_section_compressed
491 bool bfd_is_section_compressed
492 (bfd *abfd, asection *section);
495 Return @code{TRUE} if @var{section} is compressed.
499 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
501 int compression_header_size
;
502 bfd_size_type uncompressed_size
;
503 unsigned int uncompressed_align_power
;
504 unsigned int ch_type
;
505 return (bfd_is_section_compressed_info (abfd
, sec
,
506 &compression_header_size
,
508 &uncompressed_align_power
,
510 && compression_header_size
>= 0
511 && uncompressed_size
> 0);
516 bfd_init_section_decompress_status
519 bool bfd_init_section_decompress_status
520 (bfd *abfd, asection *section);
523 Record compressed section size, update section size with
524 decompressed size and set compress_status to
525 DECOMPRESS_SECTION_{ZLIB,ZSTD}.
527 Return @code{FALSE} if the section is not a valid compressed
528 section. Otherwise, return @code{TRUE}.
532 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
534 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
];
535 int compression_header_size
;
537 bfd_size_type uncompressed_size
;
538 unsigned int uncompressed_alignment_power
= 0;
539 unsigned int ch_type
;
542 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
543 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
545 header_size
= compression_header_size
? compression_header_size
: 12;
547 /* Read the header. */
548 if (sec
->rawsize
!= 0
549 || sec
->contents
!= NULL
550 || sec
->compress_status
!= COMPRESS_SECTION_NONE
551 || !bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
553 bfd_set_error (bfd_error_invalid_operation
);
557 if (compression_header_size
== 0)
559 /* In this case, it should be "ZLIB" followed by the uncompressed
560 section size, 8 bytes in big-endian order. */
561 if (! startswith ((char*) header
, "ZLIB"))
563 bfd_set_error (bfd_error_wrong_format
);
566 uncompressed_size
= bfd_getb64 (header
+ 4);
569 else if (!bfd_check_compression_header (abfd
, header
, sec
,
572 &uncompressed_alignment_power
))
574 bfd_set_error (bfd_error_wrong_format
);
578 /* PR28530, reject sizes unsupported by decompress_contents. */
579 strm
.avail_in
= sec
->size
;
580 strm
.avail_out
= uncompressed_size
;
581 if (strm
.avail_in
!= sec
->size
|| strm
.avail_out
!= uncompressed_size
)
583 bfd_set_error (bfd_error_nonrepresentable_section
);
587 sec
->compressed_size
= sec
->size
;
588 sec
->size
= uncompressed_size
;
589 bfd_set_section_alignment (sec
, uncompressed_alignment_power
);
590 sec
->compress_status
= (ch_type
== ELFCOMPRESS_ZSTD
591 ? DECOMPRESS_SECTION_ZSTD
: DECOMPRESS_SECTION_ZLIB
);
598 bfd_init_section_compress_status
601 bool bfd_init_section_compress_status
602 (bfd *abfd, asection *section);
605 If open for read, compress section, update section size with
606 compressed size and set compress_status to COMPRESS_SECTION_DONE.
608 Return @code{FALSE} if the section is not a valid compressed
609 section. Otherwise, return @code{TRUE}.
613 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
615 bfd_size_type uncompressed_size
;
616 bfd_byte
*uncompressed_buffer
;
618 /* Error if not opened for read. */
619 if (abfd
->direction
!= read_direction
622 || sec
->contents
!= NULL
623 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
625 bfd_set_error (bfd_error_invalid_operation
);
629 /* Read in the full section contents and compress it. */
630 uncompressed_size
= sec
->size
;
631 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
633 if (uncompressed_buffer
== NULL
)
636 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
637 0, uncompressed_size
))
640 sec
->contents
= uncompressed_buffer
;
641 if (bfd_compress_section_contents (abfd
, sec
) == 0)
643 free (sec
->contents
);
644 sec
->contents
= NULL
;
655 bool bfd_compress_section
656 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
659 If open for write, compress section, update section size with
660 compressed size and set compress_status to COMPRESS_SECTION_DONE.
662 Return @code{FALSE} if compression fail. Otherwise, return
663 @code{TRUE}. UNCOMPRESSED_BUFFER is freed in both cases.
667 bfd_compress_section (bfd
*abfd
, sec_ptr sec
, bfd_byte
*uncompressed_buffer
)
669 bfd_size_type uncompressed_size
= sec
->size
;
671 /* Error if not opened for write. */
672 if (abfd
->direction
!= write_direction
673 || uncompressed_size
== 0
674 || uncompressed_buffer
== NULL
675 || sec
->contents
!= NULL
676 || sec
->compressed_size
!= 0
677 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
679 bfd_set_error (bfd_error_invalid_operation
);
683 sec
->contents
= uncompressed_buffer
;
684 if (bfd_compress_section_contents (abfd
, sec
) == 0)
686 free (sec
->contents
);
687 sec
->contents
= NULL
;