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"
30 #include "libiberty.h"
32 #define MAX_COMPRESSION_HEADER_SIZE 24
36 .{* Types of compressed DWARF debug sections. *}
37 .enum compressed_debug_section_type
39 . COMPRESS_DEBUG_NONE = 0,
40 . COMPRESS_DEBUG_GNU_ZLIB = 1 << 1,
41 . COMPRESS_DEBUG_GABI_ZLIB = 1 << 2,
42 . COMPRESS_DEBUG_ZSTD = 1 << 3,
43 . COMPRESS_UNKNOWN = 1 << 4
46 .{* Tuple for compressed_debug_section_type and their name. *}
47 .struct compressed_type_tuple
49 . enum compressed_debug_section_type type;
53 .{* Compression header ch_type values. *}
54 .enum compression_type
57 . ch_compress_zlib = 1 , {* Compressed with zlib. *}
58 . ch_compress_zstd = 2 {* Compressed with zstd (www.zstandard.org). *}
62 .bfd_debug_name_to_zdebug (bfd *abfd, const char *name)
64 . size_t len = strlen (name);
65 . char *new_name = (char *) bfd_alloc (abfd, len + 2);
66 . if (new_name == NULL)
70 . memcpy (new_name + 2, name + 1, len);
75 .bfd_zdebug_name_to_debug (bfd *abfd, const char *name)
77 . size_t len = strlen (name);
78 . char *new_name = (char *) bfd_alloc (abfd, len);
79 . if (new_name == NULL)
82 . memcpy (new_name + 1, name + 2, len - 1);
88 /* Display texts for type of compressed DWARF debug sections. */
89 static const struct compressed_type_tuple compressed_debug_section_names
[] =
91 { COMPRESS_DEBUG_NONE
, "none" },
92 { COMPRESS_DEBUG_GABI_ZLIB
, "zlib" },
93 { COMPRESS_DEBUG_GNU_ZLIB
, "zlib-gnu" },
94 { COMPRESS_DEBUG_GABI_ZLIB
, "zlib-gabi" },
95 { COMPRESS_DEBUG_ZSTD
, "zstd" },
100 bfd_get_compression_algorithm
102 enum compressed_debug_section_type
103 bfd_get_compression_algorithm (const char *name);
105 Return compressed_debug_section_type from a string representation.
107 enum compressed_debug_section_type
108 bfd_get_compression_algorithm (const char *name
)
110 for (unsigned i
= 0; i
< ARRAY_SIZE (compressed_debug_section_names
); ++i
)
111 if (strcasecmp (compressed_debug_section_names
[i
].name
, name
) == 0)
112 return compressed_debug_section_names
[i
].type
;
114 return COMPRESS_UNKNOWN
;
119 bfd_get_compression_algorithm_name
121 const char *bfd_get_compression_algorithm_name
122 (enum compressed_debug_section_type type);
124 Return compression algorithm name based on the type.
127 bfd_get_compression_algorithm_name (enum compressed_debug_section_type type
)
129 for (unsigned i
= 0; i
< ARRAY_SIZE (compressed_debug_section_names
); ++i
)
130 if (type
== compressed_debug_section_names
[i
].type
)
131 return compressed_debug_section_names
[i
].name
;
138 bfd_update_compression_header
141 void bfd_update_compression_header
142 (bfd *abfd, bfd_byte *contents, asection *sec);
145 Set the compression header at CONTENTS of SEC in ABFD and update
146 elf_section_flags for compression.
150 bfd_update_compression_header (bfd
*abfd
, bfd_byte
*contents
,
153 if ((abfd
->flags
& BFD_COMPRESS
) == 0)
156 switch (bfd_get_flavour (abfd
))
158 case bfd_target_elf_flavour
:
159 if ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0)
161 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
162 struct bfd_elf_section_data
* esd
= elf_section_data (sec
);
163 enum compression_type ch_type
= (abfd
->flags
& BFD_COMPRESS_ZSTD
167 /* Set the SHF_COMPRESSED bit. */
168 elf_section_flags (sec
) |= SHF_COMPRESSED
;
170 if (bed
->s
->elfclass
== ELFCLASS32
)
172 Elf32_External_Chdr
*echdr
= (Elf32_External_Chdr
*) contents
;
173 bfd_put_32 (abfd
, ch_type
, &echdr
->ch_type
);
174 bfd_put_32 (abfd
, sec
->size
, &echdr
->ch_size
);
175 bfd_put_32 (abfd
, 1u << sec
->alignment_power
,
176 &echdr
->ch_addralign
);
177 /* bfd_log2 (alignof (Elf32_Chdr)) */
178 bfd_set_section_alignment (sec
, 2);
179 esd
->this_hdr
.sh_addralign
= 4;
183 Elf64_External_Chdr
*echdr
= (Elf64_External_Chdr
*) contents
;
184 bfd_put_32 (abfd
, ch_type
, &echdr
->ch_type
);
185 bfd_put_32 (abfd
, 0, &echdr
->ch_reserved
);
186 bfd_put_64 (abfd
, sec
->size
, &echdr
->ch_size
);
187 bfd_put_64 (abfd
, UINT64_C (1) << sec
->alignment_power
,
188 &echdr
->ch_addralign
);
189 /* bfd_log2 (alignof (Elf64_Chdr)) */
190 bfd_set_section_alignment (sec
, 3);
191 esd
->this_hdr
.sh_addralign
= 8;
196 /* Clear the SHF_COMPRESSED bit. */
197 elf_section_flags (sec
) &= ~SHF_COMPRESSED
;
201 /* Write the zlib header. It should be "ZLIB" followed by
202 the uncompressed section size, 8 bytes in big-endian
204 memcpy (contents
, "ZLIB", 4);
205 bfd_putb64 (sec
->size
, contents
+ 4);
206 /* No way to keep the original alignment, just use 1 always. */
207 bfd_set_section_alignment (sec
, 0);
212 /* Check the compression header at CONTENTS of SEC in ABFD and store the
213 ch_type in CH_TYPE, uncompressed size in UNCOMPRESSED_SIZE, and the
214 uncompressed data alignment in UNCOMPRESSED_ALIGNMENT_POWER if the
215 compression header is valid. */
218 bfd_check_compression_header (bfd
*abfd
, bfd_byte
*contents
,
220 enum compression_type
*ch_type
,
221 bfd_size_type
*uncompressed_size
,
222 unsigned int *uncompressed_alignment_power
)
224 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
225 && (elf_section_flags (sec
) & SHF_COMPRESSED
) != 0)
227 Elf_Internal_Chdr chdr
;
228 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
229 if (bed
->s
->elfclass
== ELFCLASS32
)
231 Elf32_External_Chdr
*echdr
= (Elf32_External_Chdr
*) contents
;
232 chdr
.ch_type
= bfd_get_32 (abfd
, &echdr
->ch_type
);
233 chdr
.ch_size
= bfd_get_32 (abfd
, &echdr
->ch_size
);
234 chdr
.ch_addralign
= bfd_get_32 (abfd
, &echdr
->ch_addralign
);
238 Elf64_External_Chdr
*echdr
= (Elf64_External_Chdr
*) contents
;
239 chdr
.ch_type
= bfd_get_32 (abfd
, &echdr
->ch_type
);
240 chdr
.ch_size
= bfd_get_64 (abfd
, &echdr
->ch_size
);
241 chdr
.ch_addralign
= bfd_get_64 (abfd
, &echdr
->ch_addralign
);
243 *ch_type
= chdr
.ch_type
;
244 if ((chdr
.ch_type
== ch_compress_zlib
245 || chdr
.ch_type
== ch_compress_zstd
)
246 && chdr
.ch_addralign
== (chdr
.ch_addralign
& -chdr
.ch_addralign
))
248 *uncompressed_size
= chdr
.ch_size
;
249 *uncompressed_alignment_power
= bfd_log2 (chdr
.ch_addralign
);
259 bfd_get_compression_header_size
262 int bfd_get_compression_header_size (bfd *abfd, asection *sec);
265 Return the size of the compression header of SEC in ABFD.
268 Return the size of the compression header in bytes.
272 bfd_get_compression_header_size (bfd
*abfd
, asection
*sec
)
274 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
278 if (!(abfd
->flags
& BFD_COMPRESS_GABI
))
281 else if (!(elf_section_flags (sec
) & SHF_COMPRESSED
))
284 if (get_elf_backend_data (abfd
)->s
->elfclass
== ELFCLASS32
)
285 return sizeof (Elf32_External_Chdr
);
287 return sizeof (Elf64_External_Chdr
);
295 bfd_convert_section_setup
298 bool bfd_convert_section_setup
299 (bfd *ibfd, asection *isec, bfd *obfd,
300 const char **new_name, bfd_size_type *new_size);
303 Do early setup for objcopy, when copying @var{isec} in input
304 BFD @var{ibfd} to output BFD @var{obfd}. Returns the name and
305 size of the output section.
309 bfd_convert_section_setup (bfd
*ibfd
, asection
*isec
, bfd
*obfd
,
310 const char **new_name
, bfd_size_type
*new_size
)
312 bfd_size_type hdr_size
;
314 if ((isec
->flags
& SEC_DEBUGGING
) != 0
315 && (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
317 const char *name
= *new_name
;
319 if ((obfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)) != 0)
321 /* When we decompress or compress with SHF_COMPRESSED,
322 convert section name from .zdebug_* to .debug_*. */
323 if (startswith (name
, ".zdebug_"))
325 name
= bfd_zdebug_name_to_debug (obfd
, name
);
331 /* PR binutils/18087: Compression does not always make a
332 section smaller. So only rename the section when
333 compression has actually taken place. If input section
334 name is .zdebug_*, we should never compress it again. */
335 else if (isec
->compress_status
== COMPRESS_SECTION_DONE
336 && startswith (name
, ".debug_"))
338 name
= bfd_debug_name_to_zdebug (obfd
, name
);
344 *new_size
= bfd_section_size (isec
);
346 /* Do nothing if either input or output aren't ELF. */
347 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
348 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
351 /* Do nothing if ELF classes of input and output are the same. */
352 if (get_elf_backend_data (ibfd
)->s
->elfclass
353 == get_elf_backend_data (obfd
)->s
->elfclass
)
356 /* Convert GNU property size. */
357 if (startswith (isec
->name
, NOTE_GNU_PROPERTY_SECTION_NAME
))
359 *new_size
= _bfd_elf_convert_gnu_property_size (ibfd
, obfd
);
363 /* Do nothing if input file will be decompressed. */
364 if ((ibfd
->flags
& BFD_DECOMPRESS
))
367 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
368 hdr_size
= bfd_get_compression_header_size (ibfd
, isec
);
372 /* Adjust the size of the output SHF_COMPRESSED section. */
373 if (hdr_size
== sizeof (Elf32_External_Chdr
))
374 *new_size
+= sizeof (Elf64_External_Chdr
) - sizeof (Elf32_External_Chdr
);
376 *new_size
-= sizeof (Elf64_External_Chdr
) - sizeof (Elf32_External_Chdr
);
382 bfd_convert_section_contents
385 bool bfd_convert_section_contents
386 (bfd *ibfd, asection *isec, bfd *obfd,
387 bfd_byte **ptr, bfd_size_type *ptr_size);
390 Convert the contents, stored in @var{*ptr}, of the section
391 @var{isec} in input BFD @var{ibfd} to output BFD @var{obfd}
392 if needed. The original buffer pointed to by @var{*ptr} may
393 be freed and @var{*ptr} is returned with memory malloc'd by this
394 function, and the new size written to @var{ptr_size}.
398 bfd_convert_section_contents (bfd
*ibfd
, sec_ptr isec
, bfd
*obfd
,
399 bfd_byte
**ptr
, bfd_size_type
*ptr_size
)
402 bfd_size_type ihdr_size
, ohdr_size
, size
;
403 Elf_Internal_Chdr chdr
;
406 /* Do nothing if either input or output aren't ELF. */
407 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
408 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
411 /* Do nothing if ELF classes of input and output are the same. */
412 if (get_elf_backend_data (ibfd
)->s
->elfclass
413 == get_elf_backend_data (obfd
)->s
->elfclass
)
416 /* Convert GNU properties. */
417 if (startswith (isec
->name
, NOTE_GNU_PROPERTY_SECTION_NAME
))
418 return _bfd_elf_convert_gnu_properties (ibfd
, isec
, obfd
, ptr
,
421 /* Do nothing if input file will be decompressed. */
422 if ((ibfd
->flags
& BFD_DECOMPRESS
))
425 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
426 ihdr_size
= bfd_get_compression_header_size (ibfd
, isec
);
430 /* PR 25221. Check for corrupt input sections. */
431 if (ihdr_size
> bfd_get_section_limit (ibfd
, isec
))
432 /* FIXME: Issue a warning about a corrupt
433 compression header size field ? */
438 /* Convert the contents of the input SHF_COMPRESSED section to
439 output. Get the input compression header and the size of the
440 output compression header. */
441 if (ihdr_size
== sizeof (Elf32_External_Chdr
))
443 Elf32_External_Chdr
*echdr
= (Elf32_External_Chdr
*) contents
;
444 chdr
.ch_type
= bfd_get_32 (ibfd
, &echdr
->ch_type
);
445 chdr
.ch_size
= bfd_get_32 (ibfd
, &echdr
->ch_size
);
446 chdr
.ch_addralign
= bfd_get_32 (ibfd
, &echdr
->ch_addralign
);
448 ohdr_size
= sizeof (Elf64_External_Chdr
);
452 else if (ihdr_size
!= sizeof (Elf64_External_Chdr
))
454 /* FIXME: Issue a warning about a corrupt
455 compression header size field ? */
460 Elf64_External_Chdr
*echdr
= (Elf64_External_Chdr
*) contents
;
461 chdr
.ch_type
= bfd_get_32 (ibfd
, &echdr
->ch_type
);
462 chdr
.ch_size
= bfd_get_64 (ibfd
, &echdr
->ch_size
);
463 chdr
.ch_addralign
= bfd_get_64 (ibfd
, &echdr
->ch_addralign
);
465 ohdr_size
= sizeof (Elf32_External_Chdr
);
469 size
= bfd_section_size (isec
) - ihdr_size
+ ohdr_size
;
472 contents
= (bfd_byte
*) bfd_malloc (size
);
473 if (contents
== NULL
)
477 /* Write out the output compression header. */
478 if (ohdr_size
== sizeof (Elf32_External_Chdr
))
480 Elf32_External_Chdr
*echdr
= (Elf32_External_Chdr
*) contents
;
481 bfd_put_32 (obfd
, chdr
.ch_type
, &echdr
->ch_type
);
482 bfd_put_32 (obfd
, chdr
.ch_size
, &echdr
->ch_size
);
483 bfd_put_32 (obfd
, chdr
.ch_addralign
, &echdr
->ch_addralign
);
487 Elf64_External_Chdr
*echdr
= (Elf64_External_Chdr
*) contents
;
488 bfd_put_32 (obfd
, chdr
.ch_type
, &echdr
->ch_type
);
489 bfd_put_32 (obfd
, 0, &echdr
->ch_reserved
);
490 bfd_put_64 (obfd
, chdr
.ch_size
, &echdr
->ch_size
);
491 bfd_put_64 (obfd
, chdr
.ch_addralign
, &echdr
->ch_addralign
);
494 /* Copy the compressed contents. */
496 memmove (contents
+ ohdr_size
, *ptr
+ ihdr_size
, size
- ohdr_size
);
499 memcpy (contents
+ ohdr_size
, *ptr
+ ihdr_size
, size
- ohdr_size
);
509 decompress_contents (bool is_zstd
, bfd_byte
*compressed_buffer
,
510 bfd_size_type compressed_size
,
511 bfd_byte
*uncompressed_buffer
,
512 bfd_size_type uncompressed_size
)
517 size_t ret
= ZSTD_decompress (uncompressed_buffer
, uncompressed_size
,
518 compressed_buffer
, compressed_size
);
519 return !ZSTD_isError (ret
);
526 /* It is possible the section consists of several compressed
527 buffers concatenated together, so we uncompress in a loop. */
528 /* PR 18313: The state field in the z_stream structure is supposed
529 to be invisible to the user (ie us), but some compilers will
530 still complain about it being used without initialisation. So
531 we first zero the entire z_stream structure and then set the fields
533 memset (& strm
, 0, sizeof strm
);
534 strm
.avail_in
= compressed_size
;
535 strm
.next_in
= (Bytef
*) compressed_buffer
;
536 strm
.avail_out
= uncompressed_size
;
537 /* FIXME: strm.avail_in and strm.avail_out are typically unsigned
538 int. Supporting sizes that don't fit in an unsigned int is
539 possible but will require some rewriting of this function. */
540 if (strm
.avail_in
!= compressed_size
|| strm
.avail_out
!= uncompressed_size
)
543 BFD_ASSERT (Z_OK
== 0);
544 rc
= inflateInit (&strm
);
545 while (strm
.avail_in
> 0 && strm
.avail_out
> 0)
549 strm
.next_out
= ((Bytef
*) uncompressed_buffer
550 + (uncompressed_size
- strm
.avail_out
));
551 rc
= inflate (&strm
, Z_FINISH
);
552 if (rc
!= Z_STREAM_END
)
554 rc
= inflateReset (&strm
);
556 return inflateEnd (&strm
) == Z_OK
&& rc
== Z_OK
&& strm
.avail_out
== 0;
559 /* Compress section contents using zlib/zstd and store
560 as the contents field. This function assumes the contents
561 field was allocated using bfd_malloc() or equivalent.
563 Return the uncompressed size if the full section contents is
564 compressed successfully. Otherwise return 0. */
567 bfd_compress_section_contents (bfd
*abfd
, sec_ptr sec
)
569 bfd_byte
*input_buffer
;
570 uLong compressed_size
;
572 bfd_size_type buffer_size
;
574 int orig_header_size
;
575 bfd_size_type uncompressed_size
;
576 unsigned int uncompressed_alignment_pow
;
577 enum compression_type ch_type
= ch_none
;
578 int new_header_size
= bfd_get_compression_header_size (abfd
, NULL
);
580 = bfd_is_section_compressed_info (abfd
, sec
,
583 &uncompressed_alignment_pow
,
587 /* We shouldn't be trying to decompress unsupported compressed sections. */
588 if (compressed
&& orig_header_size
< 0)
591 /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
592 overhead in .zdebug* section. */
593 if (!new_header_size
)
594 new_header_size
= 12;
595 if (ch_type
== ch_none
)
596 orig_header_size
= 12;
598 input_buffer
= sec
->contents
;
601 zlib_size
= sec
->size
- orig_header_size
;
602 compressed_size
= zlib_size
+ new_header_size
;
604 /* If we are converting between zlib-gnu and zlib-gabi then the
605 compressed contents just need to be moved. */
606 update
= (ch_type
< ch_compress_zstd
607 && (abfd
->flags
& BFD_COMPRESS_ZSTD
) == 0);
609 /* Uncompress when not just moving contents or when compressed
610 is not smaller than uncompressed. */
611 if (!update
|| compressed_size
>= uncompressed_size
)
613 buffer_size
= uncompressed_size
;
614 buffer
= bfd_malloc (buffer_size
);
618 if (!decompress_contents (ch_type
== ch_compress_zstd
,
619 input_buffer
+ orig_header_size
,
620 zlib_size
, buffer
, buffer_size
))
622 bfd_set_error (bfd_error_bad_value
);
627 bfd_set_section_alignment (sec
, uncompressed_alignment_pow
);
628 sec
->contents
= buffer
;
629 sec
->flags
|= SEC_IN_MEMORY
;
630 sec
->compress_status
= COMPRESS_SECTION_NONE
;
631 sec
->size
= uncompressed_size
;
632 input_buffer
= buffer
;
637 compressed_size
= compressBound (uncompressed_size
) + new_header_size
;
639 buffer_size
= compressed_size
;
640 buffer
= bfd_alloc (abfd
, buffer_size
);
646 if (compressed_size
< uncompressed_size
)
647 memcpy (buffer
+ new_header_size
,
648 input_buffer
+ orig_header_size
,
653 if (abfd
->flags
& BFD_COMPRESS_ZSTD
)
656 compressed_size
= ZSTD_compress (buffer
+ new_header_size
,
660 ZSTD_CLEVEL_DEFAULT
);
661 if (ZSTD_isError (compressed_size
))
663 bfd_release (abfd
, buffer
);
664 bfd_set_error (bfd_error_bad_value
);
669 else if (compress ((Bytef
*) buffer
+ new_header_size
, &compressed_size
,
670 (const Bytef
*) input_buffer
, uncompressed_size
)
673 bfd_release (abfd
, buffer
);
674 bfd_set_error (bfd_error_bad_value
);
678 compressed_size
+= new_header_size
;
681 /* If compression didn't make the section smaller, keep it uncompressed. */
682 if (compressed_size
>= uncompressed_size
)
684 memcpy (buffer
, input_buffer
, uncompressed_size
);
685 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
686 elf_section_flags (sec
) &= ~SHF_COMPRESSED
;
687 sec
->compress_status
= COMPRESS_SECTION_NONE
;
691 sec
->size
= uncompressed_size
;
692 bfd_update_compression_header (abfd
, buffer
, sec
);
693 sec
->size
= compressed_size
;
694 sec
->compress_status
= COMPRESS_SECTION_DONE
;
696 sec
->contents
= buffer
;
697 sec
->flags
|= SEC_IN_MEMORY
;
699 return uncompressed_size
;
704 bfd_get_full_section_contents
707 bool bfd_get_full_section_contents
708 (bfd *abfd, asection *section, bfd_byte **ptr);
711 Read all data from @var{section} in BFD @var{abfd}, decompress
712 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
713 return @var{*ptr} with memory malloc'd by this function.
715 Return @code{TRUE} if the full section contents is retrieved
716 successfully. If the section has no contents then this function
717 returns @code{TRUE} but @var{*ptr} is set to NULL.
721 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
723 bfd_size_type sz
= bfd_get_section_limit_octets (abfd
, sec
);
726 bfd_size_type save_size
;
727 bfd_size_type save_rawsize
;
728 bfd_byte
*compressed_buffer
;
729 unsigned int compression_header_size
;
730 const unsigned int compress_status
= sec
->compress_status
;
739 && compress_status
!= COMPRESS_SECTION_DONE
740 && _bfd_section_size_insane (abfd
, sec
))
742 /* PR 24708: Avoid attempts to allocate a ridiculous amount
745 /* xgettext:c-format */
746 (_("error: %pB(%pA) is too large (%#" PRIx64
" bytes)"),
747 abfd
, sec
, (uint64_t) sz
);
751 switch (compress_status
)
753 case COMPRESS_SECTION_NONE
:
756 p
= (bfd_byte
*) bfd_malloc (sz
);
759 /* PR 20801: Provide a more helpful error message. */
760 if (bfd_get_error () == bfd_error_no_memory
)
762 /* xgettext:c-format */
763 (_("error: %pB(%pA) is too large (%#" PRIx64
" bytes)"),
764 abfd
, sec
, (uint64_t) sz
);
769 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
778 case DECOMPRESS_SECTION_ZLIB
:
779 case DECOMPRESS_SECTION_ZSTD
:
780 /* Read in the full compressed section contents. */
781 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
782 if (compressed_buffer
== NULL
)
784 save_rawsize
= sec
->rawsize
;
785 save_size
= sec
->size
;
786 /* Clear rawsize, set size to compressed size and set compress_status
787 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
788 the uncompressed size, bfd_get_section_contents will fail. */
790 sec
->size
= sec
->compressed_size
;
791 sec
->compress_status
= COMPRESS_SECTION_NONE
;
792 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
793 0, sec
->compressed_size
);
794 /* Restore rawsize and size. */
795 sec
->rawsize
= save_rawsize
;
796 sec
->size
= save_size
;
797 sec
->compress_status
= compress_status
;
799 goto fail_compressed
;
802 p
= (bfd_byte
*) bfd_malloc (sz
);
804 goto fail_compressed
;
806 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
807 if (compression_header_size
== 0)
808 /* Set header size to the zlib header size if it is a
809 SHF_COMPRESSED section. */
810 compression_header_size
= 12;
811 bool is_zstd
= compress_status
== DECOMPRESS_SECTION_ZSTD
;
812 if (!decompress_contents (
813 is_zstd
, compressed_buffer
+ compression_header_size
,
814 sec
->compressed_size
- compression_header_size
, p
, sz
))
816 bfd_set_error (bfd_error_bad_value
);
820 free (compressed_buffer
);
824 free (compressed_buffer
);
828 case COMPRESS_SECTION_DONE
:
829 if (sec
->contents
== NULL
)
833 p
= (bfd_byte
*) bfd_malloc (sz
);
838 /* PR 17512; file: 5bc29788. */
839 if (p
!= sec
->contents
)
840 memcpy (p
, sec
->contents
, sz
);
850 bfd_is_section_compressed_info
853 bool bfd_is_section_compressed_info
854 (bfd *abfd, asection *section,
855 int *compression_header_size_p,
856 bfd_size_type *uncompressed_size_p,
857 unsigned int *uncompressed_alignment_power_p,
858 enum compression_type *ch_type);
861 Return @code{TRUE} if @var{section} is compressed. Compression
862 header size is returned in @var{compression_header_size_p},
863 uncompressed size is returned in @var{uncompressed_size_p}
864 and the uncompressed data alignement power is returned in
865 @var{uncompressed_align_pow_p}. If compression is
866 unsupported, compression header size is returned with -1
867 and uncompressed size is returned with 0.
871 bfd_is_section_compressed_info (bfd
*abfd
, sec_ptr sec
,
872 int *compression_header_size_p
,
873 bfd_size_type
*uncompressed_size_p
,
874 unsigned int *uncompressed_align_pow_p
,
875 enum compression_type
*ch_type
)
877 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
];
878 int compression_header_size
;
880 unsigned int saved
= sec
->compress_status
;
883 *uncompressed_align_pow_p
= 0;
885 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
886 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
888 header_size
= compression_header_size
? compression_header_size
: 12;
890 /* Don't decompress the section. */
891 sec
->compress_status
= COMPRESS_SECTION_NONE
;
893 /* Read the header. */
894 if (bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
896 if (compression_header_size
== 0)
897 /* In this case, it should be "ZLIB" followed by the uncompressed
898 section size, 8 bytes in big-endian order. */
899 compressed
= startswith ((char*) header
, "ZLIB");
906 *uncompressed_size_p
= sec
->size
;
909 if (compression_header_size
!= 0)
911 if (!bfd_check_compression_header (abfd
, header
, sec
, ch_type
,
913 uncompressed_align_pow_p
))
914 compression_header_size
= -1;
916 /* Check for the pathalogical case of a debug string section that
917 contains the string ZLIB.... as the first entry. We assume that
918 no uncompressed .debug_str section would ever be big enough to
919 have the first byte of its (big-endian) size be non-zero. */
920 else if (strcmp (sec
->name
, ".debug_str") == 0
921 && ISPRINT (header
[4]))
924 *uncompressed_size_p
= bfd_getb64 (header
+ 4);
927 /* Restore compress_status. */
928 sec
->compress_status
= saved
;
929 *compression_header_size_p
= compression_header_size
;
935 bfd_is_section_compressed
938 bool bfd_is_section_compressed
939 (bfd *abfd, asection *section);
942 Return @code{TRUE} if @var{section} is compressed.
946 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
948 int compression_header_size
;
949 bfd_size_type uncompressed_size
;
950 unsigned int uncompressed_align_power
;
951 enum compression_type ch_type
;
952 return (bfd_is_section_compressed_info (abfd
, sec
,
953 &compression_header_size
,
955 &uncompressed_align_power
,
957 && compression_header_size
>= 0
958 && uncompressed_size
> 0);
963 bfd_init_section_decompress_status
966 bool bfd_init_section_decompress_status
967 (bfd *abfd, asection *section);
970 Record compressed section size, update section size with
971 decompressed size and set compress_status to
972 DECOMPRESS_SECTION_{ZLIB,ZSTD}.
974 Return @code{FALSE} if the section is not a valid compressed
975 section. Otherwise, return @code{TRUE}.
979 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
981 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
];
982 int compression_header_size
;
984 bfd_size_type uncompressed_size
;
985 unsigned int uncompressed_alignment_power
= 0;
986 enum compression_type ch_type
;
989 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
990 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
992 header_size
= compression_header_size
? compression_header_size
: 12;
994 /* Read the header. */
995 if (sec
->rawsize
!= 0
996 || sec
->contents
!= NULL
997 || sec
->compress_status
!= COMPRESS_SECTION_NONE
998 || !bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
1000 bfd_set_error (bfd_error_invalid_operation
);
1004 if (compression_header_size
== 0)
1006 /* In this case, it should be "ZLIB" followed by the uncompressed
1007 section size, 8 bytes in big-endian order. */
1008 if (! startswith ((char*) header
, "ZLIB"))
1010 bfd_set_error (bfd_error_wrong_format
);
1013 uncompressed_size
= bfd_getb64 (header
+ 4);
1016 else if (!bfd_check_compression_header (abfd
, header
, sec
,
1019 &uncompressed_alignment_power
))
1021 bfd_set_error (bfd_error_wrong_format
);
1025 /* PR28530, reject sizes unsupported by decompress_contents. */
1026 strm
.avail_in
= sec
->size
;
1027 strm
.avail_out
= uncompressed_size
;
1028 if (strm
.avail_in
!= sec
->size
|| strm
.avail_out
!= uncompressed_size
)
1030 bfd_set_error (bfd_error_nonrepresentable_section
);
1034 sec
->compressed_size
= sec
->size
;
1035 sec
->size
= uncompressed_size
;
1036 bfd_set_section_alignment (sec
, uncompressed_alignment_power
);
1037 sec
->compress_status
= (ch_type
== ch_compress_zstd
1038 ? DECOMPRESS_SECTION_ZSTD
: DECOMPRESS_SECTION_ZLIB
);
1045 bfd_init_section_compress_status
1048 bool bfd_init_section_compress_status
1049 (bfd *abfd, asection *section);
1052 If open for read, compress section, update section size with
1053 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1055 Return @code{FALSE} if the section is not a valid compressed
1056 section. Otherwise, return @code{TRUE}.
1060 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
1062 bfd_size_type uncompressed_size
;
1063 bfd_byte
*uncompressed_buffer
;
1065 /* Error if not opened for read. */
1066 if (abfd
->direction
!= read_direction
1068 || sec
->rawsize
!= 0
1069 || sec
->contents
!= NULL
1070 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
1072 bfd_set_error (bfd_error_invalid_operation
);
1076 /* Read in the full section contents and compress it. */
1077 uncompressed_size
= sec
->size
;
1078 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
1080 if (uncompressed_buffer
== NULL
)
1083 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
1084 0, uncompressed_size
))
1087 sec
->contents
= uncompressed_buffer
;
1088 if (bfd_compress_section_contents (abfd
, sec
) == 0)
1090 free (sec
->contents
);
1091 sec
->contents
= NULL
;
1099 bfd_compress_section
1102 bool bfd_compress_section
1103 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
1106 If open for write, compress section, update section size with
1107 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1109 Return @code{FALSE} if compression fail. Otherwise, return
1110 @code{TRUE}. UNCOMPRESSED_BUFFER is freed in both cases.
1114 bfd_compress_section (bfd
*abfd
, sec_ptr sec
, bfd_byte
*uncompressed_buffer
)
1116 bfd_size_type uncompressed_size
= sec
->size
;
1118 /* Error if not opened for write. */
1119 if (abfd
->direction
!= write_direction
1120 || uncompressed_size
== 0
1121 || uncompressed_buffer
== NULL
1122 || sec
->contents
!= NULL
1123 || sec
->compressed_size
!= 0
1124 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
1126 bfd_set_error (bfd_error_invalid_operation
);
1130 sec
->contents
= uncompressed_buffer
;
1131 if (bfd_compress_section_contents (abfd
, sec
) == 0)
1133 free (sec
->contents
);
1134 sec
->contents
= NULL
;