1 /* Compressed section support (intended for debug sections).
2 Copyright (C) 2008-2024 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.
269 bfd_get_compression_header_size (bfd
*abfd
, asection
*sec
)
271 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
275 if (!(abfd
->flags
& BFD_COMPRESS_GABI
))
278 else if (!(elf_section_flags (sec
) & SHF_COMPRESSED
))
281 if (get_elf_backend_data (abfd
)->s
->elfclass
== ELFCLASS32
)
282 return sizeof (Elf32_External_Chdr
);
284 return sizeof (Elf64_External_Chdr
);
292 bfd_convert_section_setup
295 bool bfd_convert_section_setup
296 (bfd *ibfd, asection *isec, bfd *obfd,
297 const char **new_name, bfd_size_type *new_size);
300 Do early setup for objcopy, when copying @var{isec} in input
301 BFD @var{ibfd} to output BFD @var{obfd}. Returns the name and
302 size of the output section.
306 bfd_convert_section_setup (bfd
*ibfd
, asection
*isec
, bfd
*obfd
,
307 const char **new_name
, bfd_size_type
*new_size
)
309 bfd_size_type hdr_size
;
311 if ((isec
->flags
& SEC_DEBUGGING
) != 0
312 && (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
314 const char *name
= *new_name
;
316 if ((obfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)) != 0)
318 /* When we decompress or compress with SHF_COMPRESSED,
319 convert section name from .zdebug_* to .debug_*. */
320 if (startswith (name
, ".zdebug_"))
322 name
= bfd_zdebug_name_to_debug (obfd
, name
);
328 /* PR binutils/18087: Compression does not always make a
329 section smaller. So only rename the section when
330 compression has actually taken place. If input section
331 name is .zdebug_*, we should never compress it again. */
332 else if (isec
->compress_status
== COMPRESS_SECTION_DONE
333 && startswith (name
, ".debug_"))
335 name
= bfd_debug_name_to_zdebug (obfd
, name
);
341 *new_size
= bfd_section_size (isec
);
343 /* Do nothing if either input or output aren't ELF. */
344 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
345 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
348 /* Do nothing if ELF classes of input and output are the same. */
349 if (get_elf_backend_data (ibfd
)->s
->elfclass
350 == get_elf_backend_data (obfd
)->s
->elfclass
)
353 /* Convert GNU property size. */
354 if (startswith (isec
->name
, NOTE_GNU_PROPERTY_SECTION_NAME
))
356 *new_size
= _bfd_elf_convert_gnu_property_size (ibfd
, obfd
);
360 /* Do nothing if input file will be decompressed. */
361 if ((ibfd
->flags
& BFD_DECOMPRESS
))
364 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
365 hdr_size
= bfd_get_compression_header_size (ibfd
, isec
);
369 /* Adjust the size of the output SHF_COMPRESSED section. */
370 if (hdr_size
== sizeof (Elf32_External_Chdr
))
371 *new_size
+= sizeof (Elf64_External_Chdr
) - sizeof (Elf32_External_Chdr
);
373 *new_size
-= sizeof (Elf64_External_Chdr
) - sizeof (Elf32_External_Chdr
);
379 bfd_convert_section_contents
382 bool bfd_convert_section_contents
383 (bfd *ibfd, asection *isec, bfd *obfd,
384 bfd_byte **ptr, bfd_size_type *ptr_size);
387 Convert the contents, stored in @var{*ptr}, of the section
388 @var{isec} in input BFD @var{ibfd} to output BFD @var{obfd}
389 if needed. The original buffer pointed to by @var{*ptr} may
390 be freed and @var{*ptr} is returned with memory malloc'd by this
391 function, and the new size written to @var{ptr_size}.
395 bfd_convert_section_contents (bfd
*ibfd
, sec_ptr isec
, bfd
*obfd
,
396 bfd_byte
**ptr
, bfd_size_type
*ptr_size
)
399 bfd_size_type ihdr_size
, ohdr_size
, size
;
400 Elf_Internal_Chdr chdr
;
403 /* Do nothing if either input or output aren't ELF. */
404 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
405 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
408 /* Do nothing if ELF classes of input and output are the same. */
409 if (get_elf_backend_data (ibfd
)->s
->elfclass
410 == get_elf_backend_data (obfd
)->s
->elfclass
)
413 /* Convert GNU properties. */
414 if (startswith (isec
->name
, NOTE_GNU_PROPERTY_SECTION_NAME
))
415 return _bfd_elf_convert_gnu_properties (ibfd
, isec
, obfd
, ptr
,
418 /* Do nothing if input file will be decompressed. */
419 if ((ibfd
->flags
& BFD_DECOMPRESS
))
422 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
423 ihdr_size
= bfd_get_compression_header_size (ibfd
, isec
);
427 /* PR 25221. Check for corrupt input sections. */
428 if (ihdr_size
> bfd_get_section_limit (ibfd
, isec
))
429 /* FIXME: Issue a warning about a corrupt
430 compression header size field ? */
435 /* Convert the contents of the input SHF_COMPRESSED section to
436 output. Get the input compression header and the size of the
437 output compression header. */
438 if (ihdr_size
== sizeof (Elf32_External_Chdr
))
440 Elf32_External_Chdr
*echdr
= (Elf32_External_Chdr
*) contents
;
441 chdr
.ch_type
= bfd_get_32 (ibfd
, &echdr
->ch_type
);
442 chdr
.ch_size
= bfd_get_32 (ibfd
, &echdr
->ch_size
);
443 chdr
.ch_addralign
= bfd_get_32 (ibfd
, &echdr
->ch_addralign
);
445 ohdr_size
= sizeof (Elf64_External_Chdr
);
449 else if (ihdr_size
!= sizeof (Elf64_External_Chdr
))
451 /* FIXME: Issue a warning about a corrupt
452 compression header size field ? */
457 Elf64_External_Chdr
*echdr
= (Elf64_External_Chdr
*) contents
;
458 chdr
.ch_type
= bfd_get_32 (ibfd
, &echdr
->ch_type
);
459 chdr
.ch_size
= bfd_get_64 (ibfd
, &echdr
->ch_size
);
460 chdr
.ch_addralign
= bfd_get_64 (ibfd
, &echdr
->ch_addralign
);
462 ohdr_size
= sizeof (Elf32_External_Chdr
);
466 size
= bfd_section_size (isec
) - ihdr_size
+ ohdr_size
;
469 contents
= (bfd_byte
*) bfd_malloc (size
);
470 if (contents
== NULL
)
474 /* Write out the output compression header. */
475 if (ohdr_size
== sizeof (Elf32_External_Chdr
))
477 Elf32_External_Chdr
*echdr
= (Elf32_External_Chdr
*) contents
;
478 bfd_put_32 (obfd
, chdr
.ch_type
, &echdr
->ch_type
);
479 bfd_put_32 (obfd
, chdr
.ch_size
, &echdr
->ch_size
);
480 bfd_put_32 (obfd
, chdr
.ch_addralign
, &echdr
->ch_addralign
);
484 Elf64_External_Chdr
*echdr
= (Elf64_External_Chdr
*) contents
;
485 bfd_put_32 (obfd
, chdr
.ch_type
, &echdr
->ch_type
);
486 bfd_put_32 (obfd
, 0, &echdr
->ch_reserved
);
487 bfd_put_64 (obfd
, chdr
.ch_size
, &echdr
->ch_size
);
488 bfd_put_64 (obfd
, chdr
.ch_addralign
, &echdr
->ch_addralign
);
491 /* Copy the compressed contents. */
493 memmove (contents
+ ohdr_size
, *ptr
+ ihdr_size
, size
- ohdr_size
);
496 memcpy (contents
+ ohdr_size
, *ptr
+ ihdr_size
, size
- ohdr_size
);
506 decompress_contents (bool is_zstd
, bfd_byte
*compressed_buffer
,
507 bfd_size_type compressed_size
,
508 bfd_byte
*uncompressed_buffer
,
509 bfd_size_type uncompressed_size
)
514 size_t ret
= ZSTD_decompress (uncompressed_buffer
, uncompressed_size
,
515 compressed_buffer
, compressed_size
);
516 return !ZSTD_isError (ret
);
523 /* It is possible the section consists of several compressed
524 buffers concatenated together, so we uncompress in a loop. */
525 /* PR 18313: The state field in the z_stream structure is supposed
526 to be invisible to the user (ie us), but some compilers will
527 still complain about it being used without initialisation. So
528 we first zero the entire z_stream structure and then set the fields
530 memset (& strm
, 0, sizeof strm
);
531 strm
.avail_in
= compressed_size
;
532 strm
.next_in
= (Bytef
*) compressed_buffer
;
533 strm
.avail_out
= uncompressed_size
;
534 /* FIXME: strm.avail_in and strm.avail_out are typically unsigned
535 int. Supporting sizes that don't fit in an unsigned int is
536 possible but will require some rewriting of this function. */
537 if (strm
.avail_in
!= compressed_size
|| strm
.avail_out
!= uncompressed_size
)
540 BFD_ASSERT (Z_OK
== 0);
541 rc
= inflateInit (&strm
);
542 while (strm
.avail_in
> 0 && strm
.avail_out
> 0)
546 strm
.next_out
= ((Bytef
*) uncompressed_buffer
547 + (uncompressed_size
- strm
.avail_out
));
548 rc
= inflate (&strm
, Z_FINISH
);
549 if (rc
!= Z_STREAM_END
)
551 rc
= inflateReset (&strm
);
553 return inflateEnd (&strm
) == Z_OK
&& rc
== Z_OK
&& strm
.avail_out
== 0;
556 /* Compress section contents using zlib/zstd and store
557 as the contents field. This function assumes the contents
558 field was allocated using bfd_malloc() or equivalent.
560 Return the uncompressed size if the full section contents is
561 compressed successfully. Otherwise return (bfd_size_type) -1. */
564 bfd_compress_section_contents (bfd
*abfd
, sec_ptr sec
)
566 bfd_byte
*input_buffer
;
567 uLong compressed_size
;
569 bfd_size_type buffer_size
;
571 int orig_header_size
;
572 bfd_size_type uncompressed_size
;
573 unsigned int uncompressed_alignment_pow
;
574 enum compression_type ch_type
= ch_none
;
575 int new_header_size
= bfd_get_compression_header_size (abfd
, NULL
);
577 = bfd_is_section_compressed_info (abfd
, sec
,
580 &uncompressed_alignment_pow
,
584 /* We shouldn't be trying to decompress unsupported compressed sections. */
585 if (compressed
&& orig_header_size
< 0)
588 /* PR 31455: Check for a corrupt uncompressed size. */
589 if (uncompressed_size
== (bfd_size_type
) -1)
590 return uncompressed_size
;
592 /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
593 overhead in .zdebug* section. */
594 if (!new_header_size
)
595 new_header_size
= 12;
596 if (ch_type
== ch_none
)
597 orig_header_size
= 12;
599 input_buffer
= sec
->contents
;
602 zlib_size
= sec
->size
- orig_header_size
;
603 compressed_size
= zlib_size
+ new_header_size
;
605 /* If we are converting between zlib-gnu and zlib-gabi then the
606 compressed contents just need to be moved. */
607 update
= (ch_type
< ch_compress_zstd
608 && (abfd
->flags
& BFD_COMPRESS_ZSTD
) == 0);
610 /* Uncompress when not just moving contents or when compressed
611 is not smaller than uncompressed. */
612 if (!update
|| compressed_size
>= uncompressed_size
)
614 buffer_size
= uncompressed_size
;
615 buffer
= bfd_malloc (buffer_size
);
617 return (bfd_size_type
) -1;
619 if (!decompress_contents (ch_type
== ch_compress_zstd
,
620 input_buffer
+ orig_header_size
,
621 zlib_size
, buffer
, buffer_size
))
623 bfd_set_error (bfd_error_bad_value
);
625 return (bfd_size_type
) -1;
628 bfd_set_section_alignment (sec
, uncompressed_alignment_pow
);
629 sec
->contents
= buffer
;
630 sec
->flags
|= SEC_IN_MEMORY
;
631 sec
->compress_status
= COMPRESS_SECTION_NONE
;
632 sec
->size
= uncompressed_size
;
633 input_buffer
= buffer
;
638 compressed_size
= compressBound (uncompressed_size
) + new_header_size
;
640 buffer_size
= compressed_size
;
641 buffer
= bfd_alloc (abfd
, buffer_size
);
643 return (bfd_size_type
) -1;
647 if (compressed_size
< uncompressed_size
)
648 memcpy (buffer
+ new_header_size
,
649 input_buffer
+ orig_header_size
,
654 if (abfd
->flags
& BFD_COMPRESS_ZSTD
)
657 compressed_size
= ZSTD_compress (buffer
+ new_header_size
,
661 ZSTD_CLEVEL_DEFAULT
);
662 if (ZSTD_isError (compressed_size
))
664 bfd_release (abfd
, buffer
);
665 bfd_set_error (bfd_error_bad_value
);
666 return (bfd_size_type
) -1;
670 else if (compress ((Bytef
*) buffer
+ new_header_size
, &compressed_size
,
671 (const Bytef
*) input_buffer
, uncompressed_size
)
674 bfd_release (abfd
, buffer
);
675 bfd_set_error (bfd_error_bad_value
);
676 return (bfd_size_type
) -1;
679 compressed_size
+= new_header_size
;
682 /* If compression didn't make the section smaller, keep it uncompressed. */
683 if (compressed_size
>= uncompressed_size
)
685 memcpy (buffer
, input_buffer
, uncompressed_size
);
686 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
687 elf_section_flags (sec
) &= ~SHF_COMPRESSED
;
688 sec
->compress_status
= COMPRESS_SECTION_NONE
;
692 sec
->size
= uncompressed_size
;
693 bfd_update_compression_header (abfd
, buffer
, sec
);
694 sec
->size
= compressed_size
;
695 sec
->compress_status
= COMPRESS_SECTION_DONE
;
697 sec
->contents
= buffer
;
698 sec
->flags
|= SEC_IN_MEMORY
;
700 return uncompressed_size
;
705 bfd_get_full_section_contents
708 bool bfd_get_full_section_contents
709 (bfd *abfd, asection *section, bfd_byte **ptr);
712 Read all data from @var{section} in BFD @var{abfd}, decompress
713 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
714 return @var{*ptr} with memory malloc'd by this function.
716 Return @code{TRUE} if the full section contents is retrieved
717 successfully. If the section has no contents then this function
718 returns @code{TRUE} but @var{*ptr} is set to NULL.
722 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
724 bfd_size_type readsz
= bfd_get_section_limit_octets (abfd
, sec
);
725 bfd_size_type allocsz
= bfd_get_section_alloc_size (abfd
, sec
);
728 bfd_size_type save_size
;
729 bfd_size_type save_rawsize
;
730 bfd_byte
*compressed_buffer
;
731 unsigned int compression_header_size
;
732 const unsigned int compress_status
= sec
->compress_status
;
741 && compress_status
!= COMPRESS_SECTION_DONE
742 && bfd_section_size_insane (abfd
, sec
))
744 /* PR 24708: Avoid attempts to allocate a ridiculous amount
747 /* xgettext:c-format */
748 (_("error: %pB(%pA) is too large (%#" PRIx64
" bytes)"),
749 abfd
, sec
, (uint64_t) readsz
);
753 switch (compress_status
)
755 case COMPRESS_SECTION_NONE
:
756 if (p
== NULL
&& !sec
->mmapped_p
)
758 p
= (bfd_byte
*) bfd_malloc (allocsz
);
761 /* PR 20801: Provide a more helpful error message. */
762 if (bfd_get_error () == bfd_error_no_memory
)
764 /* xgettext:c-format */
765 (_("error: %pB(%pA) is too large (%#" PRIx64
" bytes)"),
766 abfd
, sec
, (uint64_t) allocsz
);
771 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, readsz
))
780 case DECOMPRESS_SECTION_ZLIB
:
781 case DECOMPRESS_SECTION_ZSTD
:
782 /* Read in the full compressed section contents. */
783 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
784 if (compressed_buffer
== NULL
)
786 save_rawsize
= sec
->rawsize
;
787 save_size
= sec
->size
;
788 /* Clear rawsize, set size to compressed size and set compress_status
789 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
790 the uncompressed size, bfd_get_section_contents will fail. */
792 sec
->size
= sec
->compressed_size
;
793 sec
->compress_status
= COMPRESS_SECTION_NONE
;
794 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
795 0, sec
->compressed_size
);
796 /* Restore rawsize and size. */
797 sec
->rawsize
= save_rawsize
;
798 sec
->size
= save_size
;
799 sec
->compress_status
= compress_status
;
801 goto fail_compressed
;
804 p
= (bfd_byte
*) bfd_malloc (allocsz
);
806 goto fail_compressed
;
808 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
809 if (compression_header_size
== 0)
810 /* Set header size to the zlib header size if it is a
811 SHF_COMPRESSED section. */
812 compression_header_size
= 12;
813 bool is_zstd
= compress_status
== DECOMPRESS_SECTION_ZSTD
;
814 if (!decompress_contents (
815 is_zstd
, compressed_buffer
+ compression_header_size
,
816 sec
->compressed_size
- compression_header_size
, p
, readsz
))
818 bfd_set_error (bfd_error_bad_value
);
822 free (compressed_buffer
);
826 free (compressed_buffer
);
830 case COMPRESS_SECTION_DONE
:
831 if (sec
->contents
== NULL
)
835 p
= (bfd_byte
*) bfd_malloc (allocsz
);
840 /* PR 17512; file: 5bc29788. */
841 if (p
!= sec
->contents
)
842 memcpy (p
, sec
->contents
, readsz
);
852 bfd_is_section_compressed_info
855 bool bfd_is_section_compressed_info
856 (bfd *abfd, asection *section,
857 int *compression_header_size_p,
858 bfd_size_type *uncompressed_size_p,
859 unsigned int *uncompressed_alignment_power_p,
860 enum compression_type *ch_type);
863 Return @code{TRUE} if @var{section} is compressed. Compression
864 header size is returned in @var{compression_header_size_p},
865 uncompressed size is returned in @var{uncompressed_size_p}
866 and the uncompressed data alignement power is returned in
867 @var{uncompressed_align_pow_p}. If compression is
868 unsupported, compression header size is returned with -1
869 and uncompressed size is returned with 0.
873 bfd_is_section_compressed_info (bfd
*abfd
, sec_ptr sec
,
874 int *compression_header_size_p
,
875 bfd_size_type
*uncompressed_size_p
,
876 unsigned int *uncompressed_align_pow_p
,
877 enum compression_type
*ch_type
)
879 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
];
880 int compression_header_size
;
882 unsigned int saved
= sec
->compress_status
;
885 *uncompressed_align_pow_p
= 0;
887 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
888 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
890 header_size
= compression_header_size
? compression_header_size
: 12;
892 /* Don't decompress the section. */
893 sec
->compress_status
= COMPRESS_SECTION_NONE
;
895 /* Read the header. */
896 if (bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
898 if (compression_header_size
== 0)
899 /* In this case, it should be "ZLIB" followed by the uncompressed
900 section size, 8 bytes in big-endian order. */
901 compressed
= startswith ((char*) header
, "ZLIB");
908 *uncompressed_size_p
= sec
->size
;
911 if (compression_header_size
!= 0)
913 if (!bfd_check_compression_header (abfd
, header
, sec
, ch_type
,
915 uncompressed_align_pow_p
))
916 compression_header_size
= -1;
918 /* Check for the pathalogical case of a debug string section that
919 contains the string ZLIB.... as the first entry. We assume that
920 no uncompressed .debug_str section would ever be big enough to
921 have the first byte of its (big-endian) size be non-zero. */
922 else if (strcmp (sec
->name
, ".debug_str") == 0
923 && ISPRINT (header
[4]))
926 *uncompressed_size_p
= bfd_getb64 (header
+ 4);
929 /* Restore compress_status. */
930 sec
->compress_status
= saved
;
931 *compression_header_size_p
= compression_header_size
;
937 bfd_is_section_compressed
940 bool bfd_is_section_compressed
941 (bfd *abfd, asection *section);
944 Return @code{TRUE} if @var{section} is compressed.
948 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
950 int compression_header_size
;
951 bfd_size_type uncompressed_size
;
952 unsigned int uncompressed_align_power
;
953 enum compression_type ch_type
;
954 return (bfd_is_section_compressed_info (abfd
, sec
,
955 &compression_header_size
,
957 &uncompressed_align_power
,
959 && compression_header_size
>= 0
960 && uncompressed_size
> 0);
965 bfd_init_section_decompress_status
968 bool bfd_init_section_decompress_status
969 (bfd *abfd, asection *section);
972 Record compressed section size, update section size with
973 decompressed size and set compress_status to
974 DECOMPRESS_SECTION_{ZLIB,ZSTD}.
976 Return @code{FALSE} if the section is not a valid compressed
977 section. Otherwise, return @code{TRUE}.
981 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
983 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
];
984 int compression_header_size
;
986 bfd_size_type uncompressed_size
;
987 unsigned int uncompressed_alignment_power
= 0;
988 enum compression_type ch_type
;
991 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
992 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
994 header_size
= compression_header_size
? compression_header_size
: 12;
996 /* Read the header. */
997 if (sec
->rawsize
!= 0
998 || sec
->contents
!= NULL
999 || sec
->compress_status
!= COMPRESS_SECTION_NONE
1000 || !bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
1002 bfd_set_error (bfd_error_invalid_operation
);
1006 if (compression_header_size
== 0)
1008 /* In this case, it should be "ZLIB" followed by the uncompressed
1009 section size, 8 bytes in big-endian order. */
1010 if (! startswith ((char*) header
, "ZLIB"))
1012 bfd_set_error (bfd_error_wrong_format
);
1015 uncompressed_size
= bfd_getb64 (header
+ 4);
1018 else if (!bfd_check_compression_header (abfd
, header
, sec
,
1021 &uncompressed_alignment_power
))
1023 bfd_set_error (bfd_error_wrong_format
);
1027 /* PR28530, reject sizes unsupported by decompress_contents. */
1028 strm
.avail_in
= sec
->size
;
1029 strm
.avail_out
= uncompressed_size
;
1030 if (strm
.avail_in
!= sec
->size
|| strm
.avail_out
!= uncompressed_size
)
1032 bfd_set_error (bfd_error_nonrepresentable_section
);
1036 sec
->compressed_size
= sec
->size
;
1037 sec
->size
= uncompressed_size
;
1038 bfd_set_section_alignment (sec
, uncompressed_alignment_power
);
1039 sec
->compress_status
= (ch_type
== ch_compress_zstd
1040 ? DECOMPRESS_SECTION_ZSTD
: DECOMPRESS_SECTION_ZLIB
);
1047 bfd_init_section_compress_status
1050 bool bfd_init_section_compress_status
1051 (bfd *abfd, asection *section);
1054 If open for read, compress section, update section size with
1055 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1057 Return @code{FALSE} if the section is not a valid compressed
1058 section. Otherwise, return @code{TRUE}.
1062 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
1064 bfd_size_type uncompressed_size
;
1065 bfd_byte
*uncompressed_buffer
;
1067 /* Error if not opened for read. */
1068 if (abfd
->direction
!= read_direction
1070 || sec
->rawsize
!= 0
1071 || sec
->contents
!= NULL
1072 || sec
->compress_status
!= COMPRESS_SECTION_NONE
1073 || bfd_section_size_insane (abfd
, sec
))
1075 bfd_set_error (bfd_error_invalid_operation
);
1079 /* Read in the full section contents and compress it. */
1080 uncompressed_size
= sec
->size
;
1081 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
1083 if (uncompressed_buffer
== NULL
)
1086 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
1087 0, uncompressed_size
))
1089 free (uncompressed_buffer
);
1093 sec
->contents
= uncompressed_buffer
;
1094 if (bfd_compress_section_contents (abfd
, sec
) == (bfd_size_type
) -1)
1096 free (sec
->contents
);
1097 sec
->contents
= NULL
;
1105 bfd_compress_section
1108 bool bfd_compress_section
1109 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
1112 If open for write, compress section, update section size with
1113 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1115 Return @code{FALSE} if compression fail. Otherwise, return
1116 @code{TRUE}. UNCOMPRESSED_BUFFER is freed in both cases.
1120 bfd_compress_section (bfd
*abfd
, sec_ptr sec
, bfd_byte
*uncompressed_buffer
)
1122 bfd_size_type uncompressed_size
= sec
->size
;
1124 /* Error if not opened for write. */
1125 if (abfd
->direction
!= write_direction
1126 || uncompressed_size
== 0
1127 || uncompressed_buffer
== NULL
1128 || sec
->contents
!= NULL
1129 || sec
->compressed_size
!= 0
1130 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
1132 bfd_set_error (bfd_error_invalid_operation
);
1136 sec
->contents
= uncompressed_buffer
;
1137 if (bfd_compress_section_contents (abfd
, sec
) == (bfd_size_type
) -1)
1139 free (sec
->contents
);
1140 sec
->contents
= NULL
;