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
34 decompress_contents (bool is_zstd
, bfd_byte
*compressed_buffer
,
35 bfd_size_type compressed_size
,
36 bfd_byte
*uncompressed_buffer
,
37 bfd_size_type uncompressed_size
)
42 size_t ret
= ZSTD_decompress (uncompressed_buffer
, uncompressed_size
,
43 compressed_buffer
, compressed_size
);
44 return !ZSTD_isError (ret
);
51 /* It is possible the section consists of several compressed
52 buffers concatenated together, so we uncompress in a loop. */
53 /* PR 18313: The state field in the z_stream structure is supposed
54 to be invisible to the user (ie us), but some compilers will
55 still complain about it being used without initialisation. So
56 we first zero the entire z_stream structure and then set the fields
58 memset (& strm
, 0, sizeof strm
);
59 strm
.avail_in
= compressed_size
;
60 strm
.next_in
= (Bytef
*) compressed_buffer
;
61 strm
.avail_out
= uncompressed_size
;
62 /* FIXME: strm.avail_in and strm.avail_out are typically unsigned
63 int. Supporting sizes that don't fit in an unsigned int is
64 possible but will require some rewriting of this function. */
65 if (strm
.avail_in
!= compressed_size
|| strm
.avail_out
!= uncompressed_size
)
68 BFD_ASSERT (Z_OK
== 0);
69 rc
= inflateInit (&strm
);
70 while (strm
.avail_in
> 0 && strm
.avail_out
> 0)
74 strm
.next_out
= ((Bytef
*) uncompressed_buffer
75 + (uncompressed_size
- strm
.avail_out
));
76 rc
= inflate (&strm
, Z_FINISH
);
77 if (rc
!= Z_STREAM_END
)
79 rc
= inflateReset (&strm
);
81 return inflateEnd (&strm
) == Z_OK
&& rc
== Z_OK
&& strm
.avail_out
== 0;
84 /* Compress section contents using zlib/zstd and store
85 as the contents field. This function assumes the contents
86 field was allocated using bfd_malloc() or equivalent.
88 Return the uncompressed size if the full section contents is
89 compressed successfully. Otherwise return 0. */
92 bfd_compress_section_contents (bfd
*abfd
, sec_ptr sec
)
94 bfd_byte
*input_buffer
;
95 uLong compressed_size
;
97 bfd_size_type buffer_size
;
100 bfd_size_type uncompressed_size
;
101 unsigned int uncompressed_alignment_pow
;
102 unsigned int ch_type
= 0;
103 int new_header_size
= bfd_get_compression_header_size (abfd
, NULL
);
105 = bfd_is_section_compressed_info (abfd
, sec
,
108 &uncompressed_alignment_pow
,
112 /* We shouldn't be trying to decompress unsupported compressed sections. */
113 if (compressed
&& orig_header_size
< 0)
116 /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
117 overhead in .zdebug* section. */
118 if (!new_header_size
)
119 new_header_size
= 12;
121 orig_header_size
= 12;
123 input_buffer
= sec
->contents
;
126 zlib_size
= sec
->size
- orig_header_size
;
127 compressed_size
= zlib_size
+ new_header_size
;
129 /* If we are converting between zlib-gnu and zlib-gabi then the
130 compressed contents just need to be moved. */
131 update
= (ch_type
< ELFCOMPRESS_ZSTD
132 && (abfd
->flags
& BFD_COMPRESS_ZSTD
) == 0);
134 /* Uncompress when not just moving contents or when compressed
135 is not smaller than uncompressed. */
136 if (!update
|| compressed_size
>= uncompressed_size
)
138 buffer_size
= uncompressed_size
;
139 buffer
= bfd_malloc (buffer_size
);
143 if (!decompress_contents (ch_type
== ELFCOMPRESS_ZSTD
,
144 input_buffer
+ orig_header_size
,
145 zlib_size
, buffer
, buffer_size
))
147 bfd_set_error (bfd_error_bad_value
);
152 bfd_set_section_alignment (sec
, uncompressed_alignment_pow
);
153 sec
->contents
= buffer
;
154 sec
->flags
|= SEC_IN_MEMORY
;
155 sec
->compress_status
= COMPRESS_SECTION_NONE
;
156 sec
->size
= uncompressed_size
;
157 input_buffer
= buffer
;
162 compressed_size
= compressBound (uncompressed_size
) + new_header_size
;
164 buffer_size
= compressed_size
;
165 buffer
= bfd_alloc (abfd
, buffer_size
);
171 if (compressed_size
< uncompressed_size
)
172 memcpy (buffer
+ new_header_size
,
173 input_buffer
+ orig_header_size
,
178 if (abfd
->flags
& BFD_COMPRESS_ZSTD
)
181 compressed_size
= ZSTD_compress (buffer
+ new_header_size
,
185 ZSTD_CLEVEL_DEFAULT
);
186 if (ZSTD_isError (compressed_size
))
188 bfd_release (abfd
, buffer
);
189 bfd_set_error (bfd_error_bad_value
);
194 else if (compress ((Bytef
*) buffer
+ new_header_size
, &compressed_size
,
195 (const Bytef
*) input_buffer
, uncompressed_size
)
198 bfd_release (abfd
, buffer
);
199 bfd_set_error (bfd_error_bad_value
);
203 compressed_size
+= new_header_size
;
206 /* If compression didn't make the section smaller, keep it uncompressed. */
207 if (compressed_size
>= uncompressed_size
)
209 memcpy (buffer
, input_buffer
, uncompressed_size
);
210 elf_section_flags (sec
) &= ~SHF_COMPRESSED
;
211 sec
->compress_status
= COMPRESS_SECTION_NONE
;
215 sec
->size
= uncompressed_size
;
216 bfd_update_compression_header (abfd
, buffer
, sec
);
217 sec
->size
= compressed_size
;
218 sec
->compress_status
= COMPRESS_SECTION_DONE
;
220 sec
->contents
= buffer
;
221 sec
->flags
|= SEC_IN_MEMORY
;
223 return uncompressed_size
;
228 bfd_get_full_section_contents
231 bool bfd_get_full_section_contents
232 (bfd *abfd, asection *section, bfd_byte **ptr);
235 Read all data from @var{section} in BFD @var{abfd}, decompress
236 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
237 return @var{*ptr} with memory malloc'd by this function.
239 Return @code{TRUE} if the full section contents is retrieved
240 successfully. If the section has no contents then this function
241 returns @code{TRUE} but @var{*ptr} is set to NULL.
245 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
247 bfd_size_type sz
= bfd_get_section_limit_octets (abfd
, sec
);
250 bfd_size_type save_size
;
251 bfd_size_type save_rawsize
;
252 bfd_byte
*compressed_buffer
;
253 unsigned int compression_header_size
;
254 const unsigned int compress_status
= sec
->compress_status
;
263 && compress_status
!= COMPRESS_SECTION_DONE
264 && _bfd_section_size_insane (abfd
, sec
))
266 /* PR 24708: Avoid attempts to allocate a ridiculous amount
269 /* xgettext:c-format */
270 (_("error: %pB(%pA) is too large (%#" PRIx64
" bytes)"),
271 abfd
, sec
, (uint64_t) sz
);
275 switch (compress_status
)
277 case COMPRESS_SECTION_NONE
:
280 p
= (bfd_byte
*) bfd_malloc (sz
);
283 /* PR 20801: Provide a more helpful error message. */
284 if (bfd_get_error () == bfd_error_no_memory
)
286 /* xgettext:c-format */
287 (_("error: %pB(%pA) is too large (%#" PRIx64
" bytes)"),
288 abfd
, sec
, (uint64_t) sz
);
293 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
302 case DECOMPRESS_SECTION_ZLIB
:
303 case DECOMPRESS_SECTION_ZSTD
:
304 /* Read in the full compressed section contents. */
305 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
306 if (compressed_buffer
== NULL
)
308 save_rawsize
= sec
->rawsize
;
309 save_size
= sec
->size
;
310 /* Clear rawsize, set size to compressed size and set compress_status
311 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
312 the uncompressed size, bfd_get_section_contents will fail. */
314 sec
->size
= sec
->compressed_size
;
315 sec
->compress_status
= COMPRESS_SECTION_NONE
;
316 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
317 0, sec
->compressed_size
);
318 /* Restore rawsize and size. */
319 sec
->rawsize
= save_rawsize
;
320 sec
->size
= save_size
;
321 sec
->compress_status
= compress_status
;
323 goto fail_compressed
;
326 p
= (bfd_byte
*) bfd_malloc (sz
);
328 goto fail_compressed
;
330 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
331 if (compression_header_size
== 0)
332 /* Set header size to the zlib header size if it is a
333 SHF_COMPRESSED section. */
334 compression_header_size
= 12;
335 bool is_zstd
= compress_status
== DECOMPRESS_SECTION_ZSTD
;
336 if (!decompress_contents (
337 is_zstd
, compressed_buffer
+ compression_header_size
,
338 sec
->compressed_size
- compression_header_size
, p
, sz
))
340 bfd_set_error (bfd_error_bad_value
);
344 free (compressed_buffer
);
348 free (compressed_buffer
);
352 case COMPRESS_SECTION_DONE
:
353 if (sec
->contents
== NULL
)
357 p
= (bfd_byte
*) bfd_malloc (sz
);
362 /* PR 17512; file: 5bc29788. */
363 if (p
!= sec
->contents
)
364 memcpy (p
, sec
->contents
, sz
);
374 bfd_is_section_compressed_info
377 bool bfd_is_section_compressed_info
378 (bfd *abfd, asection *section,
379 int *compression_header_size_p,
380 bfd_size_type *uncompressed_size_p,
381 unsigned int *uncompressed_alignment_power_p,
382 unsigned int *ch_type);
385 Return @code{TRUE} if @var{section} is compressed. Compression
386 header size is returned in @var{compression_header_size_p},
387 uncompressed size is returned in @var{uncompressed_size_p}
388 and the uncompressed data alignement power is returned in
389 @var{uncompressed_align_pow_p}. If compression is
390 unsupported, compression header size is returned with -1
391 and uncompressed size is returned with 0.
395 bfd_is_section_compressed_info (bfd
*abfd
, sec_ptr sec
,
396 int *compression_header_size_p
,
397 bfd_size_type
*uncompressed_size_p
,
398 unsigned int *uncompressed_align_pow_p
,
399 unsigned int *ch_type
)
401 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
];
402 int compression_header_size
;
404 unsigned int saved
= sec
->compress_status
;
407 *uncompressed_align_pow_p
= 0;
409 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
410 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
412 header_size
= compression_header_size
? compression_header_size
: 12;
414 /* Don't decompress the section. */
415 sec
->compress_status
= COMPRESS_SECTION_NONE
;
417 /* Read the header. */
418 if (bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
420 if (compression_header_size
== 0)
421 /* In this case, it should be "ZLIB" followed by the uncompressed
422 section size, 8 bytes in big-endian order. */
423 compressed
= startswith ((char*) header
, "ZLIB");
430 *uncompressed_size_p
= sec
->size
;
433 if (compression_header_size
!= 0)
435 if (!bfd_check_compression_header (abfd
, header
, sec
, ch_type
,
437 uncompressed_align_pow_p
))
438 compression_header_size
= -1;
440 /* Check for the pathalogical case of a debug string section that
441 contains the string ZLIB.... as the first entry. We assume that
442 no uncompressed .debug_str section would ever be big enough to
443 have the first byte of its (big-endian) size be non-zero. */
444 else if (strcmp (sec
->name
, ".debug_str") == 0
445 && ISPRINT (header
[4]))
448 *uncompressed_size_p
= bfd_getb64 (header
+ 4);
451 /* Restore compress_status. */
452 sec
->compress_status
= saved
;
453 *compression_header_size_p
= compression_header_size
;
459 bfd_is_section_compressed
462 bool bfd_is_section_compressed
463 (bfd *abfd, asection *section);
466 Return @code{TRUE} if @var{section} is compressed.
470 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
472 int compression_header_size
;
473 bfd_size_type uncompressed_size
;
474 unsigned int uncompressed_align_power
;
475 unsigned int ch_type
;
476 return (bfd_is_section_compressed_info (abfd
, sec
,
477 &compression_header_size
,
479 &uncompressed_align_power
,
481 && compression_header_size
>= 0
482 && uncompressed_size
> 0);
487 bfd_init_section_decompress_status
490 bool bfd_init_section_decompress_status
491 (bfd *abfd, asection *section);
494 Record compressed section size, update section size with
495 decompressed size and set compress_status to
496 DECOMPRESS_SECTION_{ZLIB,ZSTD}.
498 Return @code{FALSE} if the section is not a valid compressed
499 section. Otherwise, return @code{TRUE}.
503 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
505 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
];
506 int compression_header_size
;
508 bfd_size_type uncompressed_size
;
509 unsigned int uncompressed_alignment_power
= 0;
510 unsigned int ch_type
;
513 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
514 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
516 header_size
= compression_header_size
? compression_header_size
: 12;
518 /* Read the header. */
519 if (sec
->rawsize
!= 0
520 || sec
->contents
!= NULL
521 || sec
->compress_status
!= COMPRESS_SECTION_NONE
522 || !bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
524 bfd_set_error (bfd_error_invalid_operation
);
528 if (compression_header_size
== 0)
530 /* In this case, it should be "ZLIB" followed by the uncompressed
531 section size, 8 bytes in big-endian order. */
532 if (! startswith ((char*) header
, "ZLIB"))
534 bfd_set_error (bfd_error_wrong_format
);
537 uncompressed_size
= bfd_getb64 (header
+ 4);
539 else if (!bfd_check_compression_header (abfd
, header
, sec
,
542 &uncompressed_alignment_power
))
544 bfd_set_error (bfd_error_wrong_format
);
548 /* PR28530, reject sizes unsupported by decompress_contents. */
549 strm
.avail_in
= sec
->size
;
550 strm
.avail_out
= uncompressed_size
;
551 if (strm
.avail_in
!= sec
->size
|| strm
.avail_out
!= uncompressed_size
)
553 bfd_set_error (bfd_error_nonrepresentable_section
);
557 sec
->compressed_size
= sec
->size
;
558 sec
->size
= uncompressed_size
;
559 bfd_set_section_alignment (sec
, uncompressed_alignment_power
);
560 sec
->compress_status
= (ch_type
== ELFCOMPRESS_ZSTD
561 ? DECOMPRESS_SECTION_ZSTD
: DECOMPRESS_SECTION_ZLIB
);
568 bfd_init_section_compress_status
571 bool bfd_init_section_compress_status
572 (bfd *abfd, asection *section);
575 If open for read, compress section, update section size with
576 compressed size and set compress_status to COMPRESS_SECTION_DONE.
578 Return @code{FALSE} if the section is not a valid compressed
579 section. Otherwise, return @code{TRUE}.
583 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
585 bfd_size_type uncompressed_size
;
586 bfd_byte
*uncompressed_buffer
;
588 /* Error if not opened for read. */
589 if (abfd
->direction
!= read_direction
592 || sec
->contents
!= NULL
593 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
595 bfd_set_error (bfd_error_invalid_operation
);
599 /* Read in the full section contents and compress it. */
600 uncompressed_size
= sec
->size
;
601 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
603 if (uncompressed_buffer
== NULL
)
606 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
607 0, uncompressed_size
))
610 sec
->contents
= uncompressed_buffer
;
611 if (bfd_compress_section_contents (abfd
, sec
) == 0)
613 free (sec
->contents
);
614 sec
->contents
= NULL
;
625 bool bfd_compress_section
626 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
629 If open for write, compress section, update section size with
630 compressed size and set compress_status to COMPRESS_SECTION_DONE.
632 Return @code{FALSE} if compression fail. Otherwise, return
633 @code{TRUE}. UNCOMPRESSED_BUFFER is freed in both cases.
637 bfd_compress_section (bfd
*abfd
, sec_ptr sec
, bfd_byte
*uncompressed_buffer
)
639 bfd_size_type uncompressed_size
= sec
->size
;
641 /* Error if not opened for write. */
642 if (abfd
->direction
!= write_direction
643 || uncompressed_size
== 0
644 || uncompressed_buffer
== NULL
645 || sec
->contents
!= NULL
646 || sec
->compressed_size
!= 0
647 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
649 bfd_set_error (bfd_error_invalid_operation
);
653 sec
->contents
= uncompressed_buffer
;
654 if (bfd_compress_section_contents (abfd
, sec
) == 0)
656 free (sec
->contents
);
657 sec
->contents
= NULL
;