compress.c revision 1.1.1.12 1 /* Compressed section support (intended for debug sections).
2 Copyright (C) 2008-2025 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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. */
20
21 #include "sysdep.h"
22 #include <zlib.h>
23 #ifdef HAVE_ZSTD
24 #include <zstd.h>
25 #endif
26 #include "bfd.h"
27 #include "elf-bfd.h"
28 #include "libbfd.h"
29 #include "safe-ctype.h"
30 #include "libiberty.h"
31
32 #define MAX_COMPRESSION_HEADER_SIZE 24
33
34 /*
35 EXTERNAL
36 .{* Types of compressed DWARF debug sections. *}
37 .enum compressed_debug_section_type
38 .{
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
44 .};
45 .
46 .{* Tuple for compressed_debug_section_type and their name. *}
47 .struct compressed_type_tuple
48 .{
49 . enum compressed_debug_section_type type;
50 . const char *name;
51 .};
52 .
53 .{* Compression header ch_type values. *}
54 .enum compression_type
55 .{
56 . ch_none = 0,
57 . ch_compress_zlib = 1 , {* Compressed with zlib. *}
58 . ch_compress_zstd = 2 {* Compressed with zstd (www.zstandard.org). *}
59 .};
60 .
61 .static inline char *
62 .bfd_debug_name_to_zdebug (bfd *abfd, const char *name)
63 .{
64 . size_t len = strlen (name);
65 . char *new_name = (char *) bfd_alloc (abfd, len + 2);
66 . if (new_name == NULL)
67 . return NULL;
68 . new_name[0] = '.';
69 . new_name[1] = 'z';
70 . memcpy (new_name + 2, name + 1, len);
71 . return new_name;
72 .}
73 .
74 .static inline char *
75 .bfd_zdebug_name_to_debug (bfd *abfd, const char *name)
76 .{
77 . size_t len = strlen (name);
78 . char *new_name = (char *) bfd_alloc (abfd, len);
79 . if (new_name == NULL)
80 . return NULL;
81 . new_name[0] = '.';
82 . memcpy (new_name + 1, name + 2, len - 1);
83 . return new_name;
84 .}
85 .
86 */
87
88 /* Display texts for type of compressed DWARF debug sections. */
89 static const struct compressed_type_tuple compressed_debug_section_names[] =
90 {
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" },
96 };
97
98 /*
99 FUNCTION
100 bfd_get_compression_algorithm
101 SYNOPSIS
102 enum compressed_debug_section_type
103 bfd_get_compression_algorithm (const char *name);
104 DESCRIPTION
105 Return compressed_debug_section_type from a string representation.
106 */
107 enum compressed_debug_section_type
108 bfd_get_compression_algorithm (const char *name)
109 {
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;
113
114 return COMPRESS_UNKNOWN;
115 }
116
117 /*
118 FUNCTION
119 bfd_get_compression_algorithm_name
120 SYNOPSIS
121 const char *bfd_get_compression_algorithm_name
122 (enum compressed_debug_section_type type);
123 DESCRIPTION
124 Return compression algorithm name based on the type.
125 */
126 const char *
127 bfd_get_compression_algorithm_name (enum compressed_debug_section_type type)
128 {
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;
132
133 return NULL;
134 }
135
136 /*
137 FUNCTION
138 bfd_update_compression_header
139
140 SYNOPSIS
141 void bfd_update_compression_header
142 (bfd *abfd, bfd_byte *contents, asection *sec);
143
144 DESCRIPTION
145 Set the compression header at CONTENTS of SEC in ABFD and update
146 elf_section_flags for compression.
147 */
148
149 void
150 bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
151 asection *sec)
152 {
153 if ((abfd->flags & BFD_COMPRESS) == 0)
154 abort ();
155
156 switch (bfd_get_flavour (abfd))
157 {
158 case bfd_target_elf_flavour:
159 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
160 {
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
164 ? ch_compress_zstd
165 : ch_compress_zlib);
166
167 /* Set the SHF_COMPRESSED bit. */
168 elf_section_flags (sec) |= SHF_COMPRESSED;
169
170 if (bed->s->elfclass == ELFCLASS32)
171 {
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;
180 }
181 else
182 {
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;
192 }
193 break;
194 }
195
196 /* Clear the SHF_COMPRESSED bit. */
197 elf_section_flags (sec) &= ~SHF_COMPRESSED;
198 /* Fall through. */
199
200 default:
201 /* Write the zlib header. It should be "ZLIB" followed by
202 the uncompressed section size, 8 bytes in big-endian
203 order. */
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);
208 break;
209 }
210 }
211
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. */
216
217 static bool
218 bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
219 asection *sec,
220 enum compression_type *ch_type,
221 bfd_size_type *uncompressed_size,
222 unsigned int *uncompressed_alignment_power)
223 {
224 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
225 && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
226 {
227 Elf_Internal_Chdr chdr;
228 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
229 if (bed->s->elfclass == ELFCLASS32)
230 {
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);
235 }
236 else
237 {
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);
242 }
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))
247 {
248 *uncompressed_size = chdr.ch_size;
249 *uncompressed_alignment_power = bfd_log2 (chdr.ch_addralign);
250 return true;
251 }
252 }
253
254 return false;
255 }
256
257 /*
258 FUNCTION
259 bfd_get_compression_header_size
260
261 SYNOPSIS
262 int bfd_get_compression_header_size (bfd *abfd, asection *sec);
263
264 DESCRIPTION
265 Return the size of the compression header of SEC in ABFD.
266 */
267
268 int
269 bfd_get_compression_header_size (bfd *abfd, asection *sec)
270 {
271 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
272 {
273 if (sec == NULL)
274 {
275 if (!(abfd->flags & BFD_COMPRESS_GABI))
276 return 0;
277 }
278 else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
279 return 0;
280
281 if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
282 return sizeof (Elf32_External_Chdr);
283 else
284 return sizeof (Elf64_External_Chdr);
285 }
286
287 return 0;
288 }
289
290 /*
291 FUNCTION
292 bfd_convert_section_setup
293
294 SYNOPSIS
295 bool bfd_convert_section_setup
296 (bfd *ibfd, asection *isec, bfd *obfd,
297 const char **new_name, bfd_size_type *new_size);
298
299 DESCRIPTION
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.
303 */
304
305 bool
306 bfd_convert_section_setup (bfd *ibfd, asection *isec, bfd *obfd,
307 const char **new_name, bfd_size_type *new_size)
308 {
309 bfd_size_type hdr_size;
310
311 if ((isec->flags & SEC_DEBUGGING) != 0
312 && (isec->flags & SEC_HAS_CONTENTS) != 0)
313 {
314 const char *name = *new_name;
315
316 if ((obfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)) != 0)
317 {
318 /* When we decompress or compress with SHF_COMPRESSED,
319 convert section name from .zdebug_* to .debug_*. */
320 if (startswith (name, ".zdebug_"))
321 {
322 name = bfd_zdebug_name_to_debug (obfd, name);
323 if (name == NULL)
324 return false;
325 }
326 }
327
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_"))
334 {
335 name = bfd_debug_name_to_zdebug (obfd, name);
336 if (name == NULL)
337 return false;
338 }
339 *new_name = name;
340 }
341 *new_size = bfd_section_size (isec);
342
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)
346 return true;
347
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)
351 return true;
352
353 /* Convert GNU property size. */
354 if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
355 {
356 *new_size = _bfd_elf_convert_gnu_property_size (ibfd, obfd);
357 return true;
358 }
359
360 /* Do nothing if input file will be decompressed. */
361 if ((ibfd->flags & BFD_DECOMPRESS))
362 return true;
363
364 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
365 hdr_size = bfd_get_compression_header_size (ibfd, isec);
366 if (hdr_size == 0)
367 return true;
368
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);
372 else
373 *new_size -= sizeof (Elf64_External_Chdr) - sizeof (Elf32_External_Chdr);
374 return true;
375 }
376
377 /*
378 FUNCTION
379 bfd_convert_section_contents
380
381 SYNOPSIS
382 bool bfd_convert_section_contents
383 (bfd *ibfd, asection *isec, bfd *obfd,
384 bfd_byte **ptr, bfd_size_type *ptr_size);
385
386 DESCRIPTION
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}.
392 */
393
394 bool
395 bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd,
396 bfd_byte **ptr, bfd_size_type *ptr_size)
397 {
398 bfd_byte *contents;
399 bfd_size_type ihdr_size, ohdr_size, size;
400 Elf_Internal_Chdr chdr;
401 bool use_memmove;
402
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)
406 return true;
407
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)
411 return true;
412
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,
416 ptr_size);
417
418 /* Do nothing if input file will be decompressed. */
419 if ((ibfd->flags & BFD_DECOMPRESS))
420 return true;
421
422 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
423 ihdr_size = bfd_get_compression_header_size (ibfd, isec);
424 if (ihdr_size == 0)
425 return true;
426
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 ? */
431 return false;
432
433 contents = *ptr;
434
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))
439 {
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);
444
445 ohdr_size = sizeof (Elf64_External_Chdr);
446
447 use_memmove = false;
448 }
449 else if (ihdr_size != sizeof (Elf64_External_Chdr))
450 {
451 /* FIXME: Issue a warning about a corrupt
452 compression header size field ? */
453 return false;
454 }
455 else
456 {
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);
461
462 ohdr_size = sizeof (Elf32_External_Chdr);
463 use_memmove = true;
464 }
465
466 size = bfd_section_size (isec) - ihdr_size + ohdr_size;
467 if (!use_memmove)
468 {
469 contents = (bfd_byte *) bfd_malloc (size);
470 if (contents == NULL)
471 return false;
472 }
473
474 /* Write out the output compression header. */
475 if (ohdr_size == sizeof (Elf32_External_Chdr))
476 {
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);
481 }
482 else
483 {
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);
489 }
490
491 /* Copy the compressed contents. */
492 if (use_memmove)
493 memmove (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
494 else
495 {
496 memcpy (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
497 free (*ptr);
498 *ptr = contents;
499 }
500
501 *ptr_size = size;
502 return true;
503 }
504
505 static bool
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)
510 {
511 if (is_zstd)
512 {
513 #ifdef HAVE_ZSTD
514 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
515 compressed_buffer, compressed_size);
516 return !ZSTD_isError (ret);
517 #endif
518 }
519
520 /* It is possible the section consists of several compressed
521 buffers concatenated together, so we uncompress in a loop. */
522 do
523 {
524 uLongf dst_len = uncompressed_size;
525 uLong src_len = compressed_size;
526 int rc = uncompress2 ((Bytef *) uncompressed_buffer, &dst_len,
527 (Bytef *) compressed_buffer, &src_len);
528 if (rc != Z_OK)
529 return false;
530 uncompressed_buffer += dst_len;
531 uncompressed_size -= dst_len;
532 compressed_buffer += src_len;
533 compressed_size -= src_len;
534 }
535 while (compressed_size > 0 && uncompressed_size > 0);
536 return compressed_size == 0 && uncompressed_size == 0;
537 }
538
539 /* Compress section contents using zlib/zstd and store
540 as the contents field. This function assumes the contents
541 field was allocated using bfd_malloc() or equivalent.
542
543 Return the uncompressed size if the full section contents is
544 compressed successfully. Otherwise return (bfd_size_type) -1. */
545
546 static bfd_size_type
547 bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
548 {
549 bfd_byte *input_buffer;
550 uLong compressed_size;
551 bfd_byte *buffer;
552 bfd_size_type buffer_size;
553 int zlib_size = 0;
554 int orig_header_size;
555 bfd_size_type uncompressed_size;
556 unsigned int uncompressed_alignment_pow;
557 enum compression_type ch_type = ch_none;
558 int new_header_size = bfd_get_compression_header_size (abfd, NULL);
559 bool compressed
560 = bfd_is_section_compressed_info (abfd, sec,
561 &orig_header_size,
562 &uncompressed_size,
563 &uncompressed_alignment_pow,
564 &ch_type);
565 bool update = false;
566
567 /* We shouldn't be trying to decompress unsupported compressed sections. */
568 if (compressed && orig_header_size < 0)
569 abort ();
570
571 /* PR 31455: Check for a corrupt uncompressed size. */
572 if (uncompressed_size == (bfd_size_type) -1)
573 return uncompressed_size;
574
575 /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
576 overhead in .zdebug* section. */
577 if (!new_header_size)
578 new_header_size = 12;
579 if (ch_type == ch_none)
580 orig_header_size = 12;
581
582 input_buffer = sec->contents;
583 if (compressed)
584 {
585 zlib_size = sec->size - orig_header_size;
586 compressed_size = zlib_size + new_header_size;
587
588 /* If we are converting between zlib-gnu and zlib-gabi then the
589 compressed contents just need to be moved. */
590 update = (ch_type < ch_compress_zstd
591 && (abfd->flags & BFD_COMPRESS_ZSTD) == 0);
592
593 /* Uncompress when not just moving contents or when compressed
594 is not smaller than uncompressed. */
595 if (!update || compressed_size >= uncompressed_size)
596 {
597 buffer_size = uncompressed_size;
598 buffer = bfd_malloc (buffer_size);
599 if (buffer == NULL)
600 return (bfd_size_type) -1;
601
602 if (!decompress_contents (ch_type == ch_compress_zstd,
603 input_buffer + orig_header_size,
604 zlib_size, buffer, buffer_size))
605 {
606 bfd_set_error (bfd_error_bad_value);
607 free (buffer);
608 return (bfd_size_type) -1;
609 }
610 free (input_buffer);
611 bfd_set_section_alignment (sec, uncompressed_alignment_pow);
612 sec->contents = buffer;
613 sec->flags |= SEC_IN_MEMORY;
614 sec->compress_status = COMPRESS_SECTION_NONE;
615 sec->size = uncompressed_size;
616 input_buffer = buffer;
617 }
618 }
619
620 if (!update)
621 compressed_size = compressBound (uncompressed_size) + new_header_size;
622
623 buffer_size = compressed_size;
624 buffer = bfd_alloc (abfd, buffer_size);
625 if (buffer == NULL)
626 return (bfd_size_type) -1;
627
628 if (update)
629 {
630 if (compressed_size < uncompressed_size)
631 memcpy (buffer + new_header_size,
632 input_buffer + orig_header_size,
633 zlib_size);
634 }
635 else
636 {
637 if (abfd->flags & BFD_COMPRESS_ZSTD)
638 {
639 #if HAVE_ZSTD
640 compressed_size = ZSTD_compress (buffer + new_header_size,
641 compressed_size,
642 input_buffer,
643 uncompressed_size,
644 ZSTD_CLEVEL_DEFAULT);
645 if (ZSTD_isError (compressed_size))
646 {
647 bfd_release (abfd, buffer);
648 bfd_set_error (bfd_error_bad_value);
649 return (bfd_size_type) -1;
650 }
651 #endif
652 }
653 else if (compress ((Bytef *) buffer + new_header_size, &compressed_size,
654 (const Bytef *) input_buffer, uncompressed_size)
655 != Z_OK)
656 {
657 bfd_release (abfd, buffer);
658 bfd_set_error (bfd_error_bad_value);
659 return (bfd_size_type) -1;
660 }
661
662 compressed_size += new_header_size;
663 }
664
665 /* If compression didn't make the section smaller, keep it uncompressed. */
666 if (compressed_size >= uncompressed_size)
667 {
668 memcpy (buffer, input_buffer, uncompressed_size);
669 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
670 elf_section_flags (sec) &= ~SHF_COMPRESSED;
671 sec->compress_status = COMPRESS_SECTION_NONE;
672 }
673 else
674 {
675 sec->size = uncompressed_size;
676 bfd_update_compression_header (abfd, buffer, sec);
677 sec->size = compressed_size;
678 sec->compress_status = COMPRESS_SECTION_DONE;
679 }
680 sec->alloced = 1;
681 sec->contents = buffer;
682 sec->flags |= SEC_IN_MEMORY;
683 free (input_buffer);
684 return uncompressed_size;
685 }
686
687 /*
688 FUNCTION
689 bfd_get_full_section_contents
690
691 SYNOPSIS
692 bool bfd_get_full_section_contents
693 (bfd *abfd, asection *section, bfd_byte **ptr);
694
695 DESCRIPTION
696 Read all data from @var{section} in BFD @var{abfd}, decompress
697 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
698 return @var{*ptr} with memory malloc'd by this function.
699
700 Return @code{TRUE} if the full section contents is retrieved
701 successfully. If the section has no contents then this function
702 returns @code{TRUE} but @var{*ptr} is set to NULL.
703 */
704
705 bool
706 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
707 {
708 bfd_size_type readsz = bfd_get_section_limit_octets (abfd, sec);
709 bfd_size_type allocsz = bfd_get_section_alloc_size (abfd, sec);
710 bfd_byte *p = *ptr;
711 bool ret;
712 bfd_size_type save_size;
713 bfd_size_type save_rawsize;
714 bfd_byte *compressed_buffer;
715 unsigned int compression_header_size;
716 const unsigned int compress_status = sec->compress_status;
717
718 if (allocsz == 0)
719 {
720 *ptr = NULL;
721 return true;
722 }
723
724 if (p == NULL
725 && compress_status != COMPRESS_SECTION_DONE
726 && bfd_section_size_insane (abfd, sec))
727 {
728 /* PR 24708: Avoid attempts to allocate a ridiculous amount
729 of memory. */
730 _bfd_error_handler
731 /* xgettext:c-format */
732 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
733 abfd, sec, (uint64_t) readsz);
734 return false;
735 }
736
737 switch (compress_status)
738 {
739 case COMPRESS_SECTION_NONE:
740 if (p == NULL && !sec->mmapped_p)
741 {
742 p = (bfd_byte *) bfd_malloc (allocsz);
743 if (p == NULL)
744 {
745 /* PR 20801: Provide a more helpful error message. */
746 if (bfd_get_error () == bfd_error_no_memory)
747 _bfd_error_handler
748 /* xgettext:c-format */
749 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
750 abfd, sec, (uint64_t) allocsz);
751 return false;
752 }
753 }
754
755 if (!bfd_get_section_contents (abfd, sec, p, 0, readsz))
756 {
757 if (*ptr != p)
758 free (p);
759 return false;
760 }
761 *ptr = p;
762 return true;
763
764 case DECOMPRESS_SECTION_ZLIB:
765 case DECOMPRESS_SECTION_ZSTD:
766 /* Read in the full compressed section contents. */
767 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
768 if (compressed_buffer == NULL)
769 return false;
770 save_rawsize = sec->rawsize;
771 save_size = sec->size;
772 /* Clear rawsize, set size to compressed size and set compress_status
773 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
774 the uncompressed size, bfd_get_section_contents will fail. */
775 sec->rawsize = 0;
776 sec->size = sec->compressed_size;
777 sec->compress_status = COMPRESS_SECTION_NONE;
778 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
779 0, sec->compressed_size);
780 /* Restore rawsize and size. */
781 sec->rawsize = save_rawsize;
782 sec->size = save_size;
783 sec->compress_status = compress_status;
784 if (!ret)
785 goto fail_compressed;
786
787 if (p == NULL)
788 p = (bfd_byte *) bfd_malloc (allocsz);
789 if (p == NULL)
790 goto fail_compressed;
791
792 compression_header_size = bfd_get_compression_header_size (abfd, sec);
793 if (compression_header_size == 0)
794 /* Set header size to the zlib header size if it is a
795 SHF_COMPRESSED section. */
796 compression_header_size = 12;
797 bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
798 if (!decompress_contents (
799 is_zstd, compressed_buffer + compression_header_size,
800 sec->compressed_size - compression_header_size, p, readsz))
801 {
802 bfd_set_error (bfd_error_bad_value);
803 if (p != *ptr)
804 free (p);
805 fail_compressed:
806 free (compressed_buffer);
807 return false;
808 }
809
810 free (compressed_buffer);
811 *ptr = p;
812 return true;
813
814 case COMPRESS_SECTION_DONE:
815 if (sec->contents == NULL)
816 return false;
817 if (p == NULL)
818 {
819 p = (bfd_byte *) bfd_malloc (allocsz);
820 if (p == NULL)
821 return false;
822 *ptr = p;
823 }
824 /* PR 17512; file: 5bc29788. */
825 if (p != sec->contents)
826 memcpy (p, sec->contents, readsz);
827 return true;
828
829 default:
830 abort ();
831 }
832 }
833
834 /*
835 FUNCTION
836 bfd_is_section_compressed_info
837
838 SYNOPSIS
839 bool bfd_is_section_compressed_info
840 (bfd *abfd, asection *section,
841 int *compression_header_size_p,
842 bfd_size_type *uncompressed_size_p,
843 unsigned int *uncompressed_alignment_power_p,
844 enum compression_type *ch_type);
845
846 DESCRIPTION
847 Return @code{TRUE} if @var{section} is compressed. Compression
848 header size is returned in @var{compression_header_size_p},
849 uncompressed size is returned in @var{uncompressed_size_p}
850 and the uncompressed data alignement power is returned in
851 @var{uncompressed_align_pow_p}. If compression is
852 unsupported, compression header size is returned with -1
853 and uncompressed size is returned with 0.
854 */
855
856 bool
857 bfd_is_section_compressed_info (bfd *abfd, sec_ptr sec,
858 int *compression_header_size_p,
859 bfd_size_type *uncompressed_size_p,
860 unsigned int *uncompressed_align_pow_p,
861 enum compression_type *ch_type)
862 {
863 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
864 int compression_header_size;
865 int header_size;
866 unsigned int saved = sec->compress_status;
867 bool compressed;
868
869 *uncompressed_align_pow_p = 0;
870
871 compression_header_size = bfd_get_compression_header_size (abfd, sec);
872 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
873 abort ();
874 header_size = compression_header_size ? compression_header_size : 12;
875
876 /* Don't decompress the section. */
877 sec->compress_status = COMPRESS_SECTION_NONE;
878
879 /* Read the header. */
880 if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
881 {
882 if (compression_header_size == 0)
883 /* In this case, it should be "ZLIB" followed by the uncompressed
884 section size, 8 bytes in big-endian order. */
885 compressed = startswith ((char*) header , "ZLIB");
886 else
887 compressed = true;
888 }
889 else
890 compressed = false;
891
892 *uncompressed_size_p = sec->size;
893 if (compressed)
894 {
895 if (compression_header_size != 0)
896 {
897 if (!bfd_check_compression_header (abfd, header, sec, ch_type,
898 uncompressed_size_p,
899 uncompressed_align_pow_p))
900 compression_header_size = -1;
901 }
902 /* Check for the pathalogical case of a debug string section that
903 contains the string ZLIB.... as the first entry. We assume that
904 no uncompressed .debug_str section would ever be big enough to
905 have the first byte of its (big-endian) size be non-zero. */
906 else if (strcmp (sec->name, ".debug_str") == 0
907 && ISPRINT (header[4]))
908 compressed = false;
909 else
910 *uncompressed_size_p = bfd_getb64 (header + 4);
911 }
912
913 /* Restore compress_status. */
914 sec->compress_status = saved;
915 *compression_header_size_p = compression_header_size;
916 return compressed;
917 }
918
919 /*
920 FUNCTION
921 bfd_is_section_compressed
922
923 SYNOPSIS
924 bool bfd_is_section_compressed
925 (bfd *abfd, asection *section);
926
927 DESCRIPTION
928 Return @code{TRUE} if @var{section} is compressed.
929 */
930
931 bool
932 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
933 {
934 int compression_header_size;
935 bfd_size_type uncompressed_size;
936 unsigned int uncompressed_align_power;
937 enum compression_type ch_type;
938 return (bfd_is_section_compressed_info (abfd, sec,
939 &compression_header_size,
940 &uncompressed_size,
941 &uncompressed_align_power,
942 &ch_type)
943 && compression_header_size >= 0
944 && uncompressed_size > 0);
945 }
946
947 /*
948 FUNCTION
949 bfd_init_section_decompress_status
950
951 SYNOPSIS
952 bool bfd_init_section_decompress_status
953 (bfd *abfd, asection *section);
954
955 DESCRIPTION
956 Record compressed section size, update section size with
957 decompressed size and set compress_status to
958 DECOMPRESS_SECTION_{ZLIB,ZSTD}.
959
960 Return @code{FALSE} if the section is not a valid compressed
961 section. Otherwise, return @code{TRUE}.
962 */
963
964 bool
965 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
966 {
967 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
968 int compression_header_size;
969 int header_size;
970 bfd_size_type uncompressed_size;
971 unsigned int uncompressed_alignment_power = 0;
972 enum compression_type ch_type;
973
974 compression_header_size = bfd_get_compression_header_size (abfd, sec);
975 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
976 abort ();
977 header_size = compression_header_size ? compression_header_size : 12;
978
979 /* Read the header. */
980 if (sec->rawsize != 0
981 || sec->contents != NULL
982 || sec->compress_status != COMPRESS_SECTION_NONE
983 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
984 {
985 bfd_set_error (bfd_error_invalid_operation);
986 return false;
987 }
988
989 if (compression_header_size == 0)
990 {
991 /* In this case, it should be "ZLIB" followed by the uncompressed
992 section size, 8 bytes in big-endian order. */
993 if (! startswith ((char*) header, "ZLIB"))
994 {
995 bfd_set_error (bfd_error_wrong_format);
996 return false;
997 }
998 uncompressed_size = bfd_getb64 (header + 4);
999 ch_type = ch_none;
1000 }
1001 else if (!bfd_check_compression_header (abfd, header, sec,
1002 &ch_type,
1003 &uncompressed_size,
1004 &uncompressed_alignment_power))
1005 {
1006 bfd_set_error (bfd_error_wrong_format);
1007 return false;
1008 }
1009
1010 /* PR28530, reject sizes unsupported by decompress_contents. zlib
1011 supports only up to 4 GiB input on machines whose long is 32 bits. */
1012 if (ch_type == ch_compress_zlib
1013 && (sec->size != (uLong) sec->size
1014 || uncompressed_size != (uLongf) uncompressed_size))
1015 {
1016 bfd_set_error (bfd_error_nonrepresentable_section);
1017 return false;
1018 }
1019
1020 sec->compressed_size = sec->size;
1021 sec->size = uncompressed_size;
1022 bfd_set_section_alignment (sec, uncompressed_alignment_power);
1023 sec->compress_status = (ch_type == ch_compress_zstd
1024 ? DECOMPRESS_SECTION_ZSTD : DECOMPRESS_SECTION_ZLIB);
1025
1026 return true;
1027 }
1028
1029 /*
1030 FUNCTION
1031 bfd_init_section_compress_status
1032
1033 SYNOPSIS
1034 bool bfd_init_section_compress_status
1035 (bfd *abfd, asection *section);
1036
1037 DESCRIPTION
1038 If open for read, compress section, update section size with
1039 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1040
1041 Return @code{FALSE} if the section is not a valid compressed
1042 section. Otherwise, return @code{TRUE}.
1043 */
1044
1045 bool
1046 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
1047 {
1048 bfd_size_type uncompressed_size;
1049 bfd_byte *uncompressed_buffer;
1050
1051 /* Error if not opened for read. */
1052 if (abfd->direction != read_direction
1053 || sec->size == 0
1054 || sec->rawsize != 0
1055 || sec->contents != NULL
1056 || sec->compress_status != COMPRESS_SECTION_NONE
1057 || bfd_section_size_insane (abfd, sec))
1058 {
1059 bfd_set_error (bfd_error_invalid_operation);
1060 return false;
1061 }
1062
1063 /* Read in the full section contents and compress it. */
1064 uncompressed_size = sec->size;
1065 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
1066 /* PR 21431 */
1067 if (uncompressed_buffer == NULL)
1068 return false;
1069
1070 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
1071 0, uncompressed_size))
1072 {
1073 free (uncompressed_buffer);
1074 return false;
1075 }
1076
1077 sec->contents = uncompressed_buffer;
1078 if (bfd_compress_section_contents (abfd, sec) == (bfd_size_type) -1)
1079 {
1080 free (sec->contents);
1081 sec->contents = NULL;
1082 return false;
1083 }
1084 return true;
1085 }
1086
1087 /*
1088 FUNCTION
1089 bfd_compress_section
1090
1091 SYNOPSIS
1092 bool bfd_compress_section
1093 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
1094
1095 DESCRIPTION
1096 If open for write, compress section, update section size with
1097 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1098
1099 Return @code{FALSE} if compression fail. Otherwise, return
1100 @code{TRUE}. UNCOMPRESSED_BUFFER is freed in both cases.
1101 */
1102
1103 bool
1104 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
1105 {
1106 bfd_size_type uncompressed_size = sec->size;
1107
1108 /* Error if not opened for write. */
1109 if (abfd->direction != write_direction
1110 || uncompressed_size == 0
1111 || uncompressed_buffer == NULL
1112 || sec->contents != NULL
1113 || sec->compressed_size != 0
1114 || sec->compress_status != COMPRESS_SECTION_NONE)
1115 {
1116 bfd_set_error (bfd_error_invalid_operation);
1117 return false;
1118 }
1119
1120 sec->contents = uncompressed_buffer;
1121 if (bfd_compress_section_contents (abfd, sec) == (bfd_size_type) -1)
1122 {
1123 free (sec->contents);
1124 sec->contents = NULL;
1125 return false;
1126 }
1127 return true;
1128 }
1129