Compression tidy and fixes
[binutils-gdb.git] / bfd / compress.c
1 /* Compressed section support (intended for debug sections).
2 Copyright (C) 2008-2022 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 CODE_FRAGMENT
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 RETURNS
268 Return the size of the compression header in bytes.
269 */
270
271 int
272 bfd_get_compression_header_size (bfd *abfd, asection *sec)
273 {
274 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
275 {
276 if (sec == NULL)
277 {
278 if (!(abfd->flags & BFD_COMPRESS_GABI))
279 return 0;
280 }
281 else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
282 return 0;
283
284 if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
285 return sizeof (Elf32_External_Chdr);
286 else
287 return sizeof (Elf64_External_Chdr);
288 }
289
290 return 0;
291 }
292
293 /*
294 FUNCTION
295 bfd_convert_section_setup
296
297 SYNOPSIS
298 bool bfd_convert_section_setup
299 (bfd *ibfd, asection *isec, bfd *obfd,
300 const char **new_name, bfd_size_type *new_size);
301
302 DESCRIPTION
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.
306 */
307
308 bool
309 bfd_convert_section_setup (bfd *ibfd, asection *isec, bfd *obfd,
310 const char **new_name, bfd_size_type *new_size)
311 {
312 bfd_size_type hdr_size;
313
314 if ((isec->flags & SEC_DEBUGGING) != 0
315 && (isec->flags & SEC_HAS_CONTENTS) != 0)
316 {
317 const char *name = *new_name;
318
319 if ((obfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)) != 0)
320 {
321 /* When we decompress or compress with SHF_COMPRESSED,
322 convert section name from .zdebug_* to .debug_*. */
323 if (startswith (name, ".zdebug_"))
324 {
325 name = bfd_zdebug_name_to_debug (obfd, name);
326 if (name == NULL)
327 return false;
328 }
329 }
330
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_"))
337 {
338 name = bfd_debug_name_to_zdebug (obfd, name);
339 if (name == NULL)
340 return false;
341 }
342 *new_name = name;
343 }
344 *new_size = bfd_section_size (isec);
345
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)
349 return true;
350
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)
354 return true;
355
356 /* Convert GNU property size. */
357 if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
358 {
359 *new_size = _bfd_elf_convert_gnu_property_size (ibfd, obfd);
360 return true;
361 }
362
363 /* Do nothing if input file will be decompressed. */
364 if ((ibfd->flags & BFD_DECOMPRESS))
365 return true;
366
367 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
368 hdr_size = bfd_get_compression_header_size (ibfd, isec);
369 if (hdr_size == 0)
370 return true;
371
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);
375 else
376 *new_size -= sizeof (Elf64_External_Chdr) - sizeof (Elf32_External_Chdr);
377 return true;
378 }
379
380 /*
381 FUNCTION
382 bfd_convert_section_contents
383
384 SYNOPSIS
385 bool bfd_convert_section_contents
386 (bfd *ibfd, asection *isec, bfd *obfd,
387 bfd_byte **ptr, bfd_size_type *ptr_size);
388
389 DESCRIPTION
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}.
395 */
396
397 bool
398 bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd,
399 bfd_byte **ptr, bfd_size_type *ptr_size)
400 {
401 bfd_byte *contents;
402 bfd_size_type ihdr_size, ohdr_size, size;
403 Elf_Internal_Chdr chdr;
404 bool use_memmove;
405
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)
409 return true;
410
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)
414 return true;
415
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,
419 ptr_size);
420
421 /* Do nothing if input file will be decompressed. */
422 if ((ibfd->flags & BFD_DECOMPRESS))
423 return true;
424
425 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
426 ihdr_size = bfd_get_compression_header_size (ibfd, isec);
427 if (ihdr_size == 0)
428 return true;
429
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 ? */
434 return false;
435
436 contents = *ptr;
437
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))
442 {
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);
447
448 ohdr_size = sizeof (Elf64_External_Chdr);
449
450 use_memmove = false;
451 }
452 else if (ihdr_size != sizeof (Elf64_External_Chdr))
453 {
454 /* FIXME: Issue a warning about a corrupt
455 compression header size field ? */
456 return false;
457 }
458 else
459 {
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);
464
465 ohdr_size = sizeof (Elf32_External_Chdr);
466 use_memmove = true;
467 }
468
469 size = bfd_section_size (isec) - ihdr_size + ohdr_size;
470 if (!use_memmove)
471 {
472 contents = (bfd_byte *) bfd_malloc (size);
473 if (contents == NULL)
474 return false;
475 }
476
477 /* Write out the output compression header. */
478 if (ohdr_size == sizeof (Elf32_External_Chdr))
479 {
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);
484 }
485 else
486 {
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);
492 }
493
494 /* Copy the compressed contents. */
495 if (use_memmove)
496 memmove (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
497 else
498 {
499 memcpy (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
500 free (*ptr);
501 *ptr = contents;
502 }
503
504 *ptr_size = size;
505 return true;
506 }
507
508 static bool
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)
513 {
514 if (is_zstd)
515 {
516 #ifdef HAVE_ZSTD
517 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
518 compressed_buffer, compressed_size);
519 return !ZSTD_isError (ret);
520 #endif
521 }
522
523 z_stream strm;
524 int rc;
525
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
532 that we need. */
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)
541 return false;
542
543 BFD_ASSERT (Z_OK == 0);
544 rc = inflateInit (&strm);
545 while (strm.avail_in > 0 && strm.avail_out > 0)
546 {
547 if (rc != Z_OK)
548 break;
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)
553 break;
554 rc = inflateReset (&strm);
555 }
556 return inflateEnd (&strm) == Z_OK && rc == Z_OK && strm.avail_out == 0;
557 }
558
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.
562
563 Return the uncompressed size if the full section contents is
564 compressed successfully. Otherwise return 0. */
565
566 static bfd_size_type
567 bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
568 {
569 bfd_byte *input_buffer;
570 uLong compressed_size;
571 bfd_byte *buffer;
572 bfd_size_type buffer_size;
573 int zlib_size = 0;
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);
579 bool compressed
580 = bfd_is_section_compressed_info (abfd, sec,
581 &orig_header_size,
582 &uncompressed_size,
583 &uncompressed_alignment_pow,
584 &ch_type);
585 bool update = false;
586
587 /* We shouldn't be trying to decompress unsupported compressed sections. */
588 if (compressed && orig_header_size < 0)
589 abort ();
590
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;
597
598 input_buffer = sec->contents;
599 if (compressed)
600 {
601 zlib_size = sec->size - orig_header_size;
602 compressed_size = zlib_size + new_header_size;
603
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);
608
609 /* Uncompress when not just moving contents or when compressed
610 is not smaller than uncompressed. */
611 if (!update || compressed_size >= uncompressed_size)
612 {
613 buffer_size = uncompressed_size;
614 buffer = bfd_malloc (buffer_size);
615 if (buffer == NULL)
616 return 0;
617
618 if (!decompress_contents (ch_type == ch_compress_zstd,
619 input_buffer + orig_header_size,
620 zlib_size, buffer, buffer_size))
621 {
622 bfd_set_error (bfd_error_bad_value);
623 free (buffer);
624 return 0;
625 }
626 free (input_buffer);
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;
633 }
634 }
635
636 if (!update)
637 compressed_size = compressBound (uncompressed_size) + new_header_size;
638
639 buffer_size = compressed_size;
640 buffer = bfd_alloc (abfd, buffer_size);
641 if (buffer == NULL)
642 return 0;
643
644 if (update)
645 {
646 if (compressed_size < uncompressed_size)
647 memcpy (buffer + new_header_size,
648 input_buffer + orig_header_size,
649 zlib_size);
650 }
651 else
652 {
653 if (abfd->flags & BFD_COMPRESS_ZSTD)
654 {
655 #if HAVE_ZSTD
656 compressed_size = ZSTD_compress (buffer + new_header_size,
657 compressed_size,
658 input_buffer,
659 uncompressed_size,
660 ZSTD_CLEVEL_DEFAULT);
661 if (ZSTD_isError (compressed_size))
662 {
663 bfd_release (abfd, buffer);
664 bfd_set_error (bfd_error_bad_value);
665 return 0;
666 }
667 #endif
668 }
669 else if (compress ((Bytef *) buffer + new_header_size, &compressed_size,
670 (const Bytef *) input_buffer, uncompressed_size)
671 != Z_OK)
672 {
673 bfd_release (abfd, buffer);
674 bfd_set_error (bfd_error_bad_value);
675 return 0;
676 }
677
678 compressed_size += new_header_size;
679 }
680
681 /* If compression didn't make the section smaller, keep it uncompressed. */
682 if (compressed_size >= uncompressed_size)
683 {
684 memcpy (buffer, input_buffer, uncompressed_size);
685 elf_section_flags (sec) &= ~SHF_COMPRESSED;
686 sec->compress_status = COMPRESS_SECTION_NONE;
687 }
688 else
689 {
690 sec->size = uncompressed_size;
691 bfd_update_compression_header (abfd, buffer, sec);
692 sec->size = compressed_size;
693 sec->compress_status = COMPRESS_SECTION_DONE;
694 }
695 sec->contents = buffer;
696 sec->flags |= SEC_IN_MEMORY;
697 free (input_buffer);
698 return uncompressed_size;
699 }
700
701 /*
702 FUNCTION
703 bfd_get_full_section_contents
704
705 SYNOPSIS
706 bool bfd_get_full_section_contents
707 (bfd *abfd, asection *section, bfd_byte **ptr);
708
709 DESCRIPTION
710 Read all data from @var{section} in BFD @var{abfd}, decompress
711 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
712 return @var{*ptr} with memory malloc'd by this function.
713
714 Return @code{TRUE} if the full section contents is retrieved
715 successfully. If the section has no contents then this function
716 returns @code{TRUE} but @var{*ptr} is set to NULL.
717 */
718
719 bool
720 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
721 {
722 bfd_size_type sz = bfd_get_section_limit_octets (abfd, sec);
723 bfd_byte *p = *ptr;
724 bool ret;
725 bfd_size_type save_size;
726 bfd_size_type save_rawsize;
727 bfd_byte *compressed_buffer;
728 unsigned int compression_header_size;
729 const unsigned int compress_status = sec->compress_status;
730
731 if (sz == 0)
732 {
733 *ptr = NULL;
734 return true;
735 }
736
737 if (p == NULL
738 && compress_status != COMPRESS_SECTION_DONE
739 && _bfd_section_size_insane (abfd, sec))
740 {
741 /* PR 24708: Avoid attempts to allocate a ridiculous amount
742 of memory. */
743 _bfd_error_handler
744 /* xgettext:c-format */
745 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
746 abfd, sec, (uint64_t) sz);
747 return false;
748 }
749
750 switch (compress_status)
751 {
752 case COMPRESS_SECTION_NONE:
753 if (p == NULL)
754 {
755 p = (bfd_byte *) bfd_malloc (sz);
756 if (p == NULL)
757 {
758 /* PR 20801: Provide a more helpful error message. */
759 if (bfd_get_error () == bfd_error_no_memory)
760 _bfd_error_handler
761 /* xgettext:c-format */
762 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
763 abfd, sec, (uint64_t) sz);
764 return false;
765 }
766 }
767
768 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
769 {
770 if (*ptr != p)
771 free (p);
772 return false;
773 }
774 *ptr = p;
775 return true;
776
777 case DECOMPRESS_SECTION_ZLIB:
778 case DECOMPRESS_SECTION_ZSTD:
779 /* Read in the full compressed section contents. */
780 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
781 if (compressed_buffer == NULL)
782 return false;
783 save_rawsize = sec->rawsize;
784 save_size = sec->size;
785 /* Clear rawsize, set size to compressed size and set compress_status
786 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
787 the uncompressed size, bfd_get_section_contents will fail. */
788 sec->rawsize = 0;
789 sec->size = sec->compressed_size;
790 sec->compress_status = COMPRESS_SECTION_NONE;
791 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
792 0, sec->compressed_size);
793 /* Restore rawsize and size. */
794 sec->rawsize = save_rawsize;
795 sec->size = save_size;
796 sec->compress_status = compress_status;
797 if (!ret)
798 goto fail_compressed;
799
800 if (p == NULL)
801 p = (bfd_byte *) bfd_malloc (sz);
802 if (p == NULL)
803 goto fail_compressed;
804
805 compression_header_size = bfd_get_compression_header_size (abfd, sec);
806 if (compression_header_size == 0)
807 /* Set header size to the zlib header size if it is a
808 SHF_COMPRESSED section. */
809 compression_header_size = 12;
810 bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
811 if (!decompress_contents (
812 is_zstd, compressed_buffer + compression_header_size,
813 sec->compressed_size - compression_header_size, p, sz))
814 {
815 bfd_set_error (bfd_error_bad_value);
816 if (p != *ptr)
817 free (p);
818 fail_compressed:
819 free (compressed_buffer);
820 return false;
821 }
822
823 free (compressed_buffer);
824 *ptr = p;
825 return true;
826
827 case COMPRESS_SECTION_DONE:
828 if (sec->contents == NULL)
829 return false;
830 if (p == NULL)
831 {
832 p = (bfd_byte *) bfd_malloc (sz);
833 if (p == NULL)
834 return false;
835 *ptr = p;
836 }
837 /* PR 17512; file: 5bc29788. */
838 if (p != sec->contents)
839 memcpy (p, sec->contents, sz);
840 return true;
841
842 default:
843 abort ();
844 }
845 }
846
847 /*
848 FUNCTION
849 bfd_is_section_compressed_info
850
851 SYNOPSIS
852 bool bfd_is_section_compressed_info
853 (bfd *abfd, asection *section,
854 int *compression_header_size_p,
855 bfd_size_type *uncompressed_size_p,
856 unsigned int *uncompressed_alignment_power_p,
857 enum compression_type *ch_type);
858
859 DESCRIPTION
860 Return @code{TRUE} if @var{section} is compressed. Compression
861 header size is returned in @var{compression_header_size_p},
862 uncompressed size is returned in @var{uncompressed_size_p}
863 and the uncompressed data alignement power is returned in
864 @var{uncompressed_align_pow_p}. If compression is
865 unsupported, compression header size is returned with -1
866 and uncompressed size is returned with 0.
867 */
868
869 bool
870 bfd_is_section_compressed_info (bfd *abfd, sec_ptr sec,
871 int *compression_header_size_p,
872 bfd_size_type *uncompressed_size_p,
873 unsigned int *uncompressed_align_pow_p,
874 enum compression_type *ch_type)
875 {
876 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
877 int compression_header_size;
878 int header_size;
879 unsigned int saved = sec->compress_status;
880 bool compressed;
881
882 *uncompressed_align_pow_p = 0;
883
884 compression_header_size = bfd_get_compression_header_size (abfd, sec);
885 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
886 abort ();
887 header_size = compression_header_size ? compression_header_size : 12;
888
889 /* Don't decompress the section. */
890 sec->compress_status = COMPRESS_SECTION_NONE;
891
892 /* Read the header. */
893 if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
894 {
895 if (compression_header_size == 0)
896 /* In this case, it should be "ZLIB" followed by the uncompressed
897 section size, 8 bytes in big-endian order. */
898 compressed = startswith ((char*) header , "ZLIB");
899 else
900 compressed = true;
901 }
902 else
903 compressed = false;
904
905 *uncompressed_size_p = sec->size;
906 if (compressed)
907 {
908 if (compression_header_size != 0)
909 {
910 if (!bfd_check_compression_header (abfd, header, sec, ch_type,
911 uncompressed_size_p,
912 uncompressed_align_pow_p))
913 compression_header_size = -1;
914 }
915 /* Check for the pathalogical case of a debug string section that
916 contains the string ZLIB.... as the first entry. We assume that
917 no uncompressed .debug_str section would ever be big enough to
918 have the first byte of its (big-endian) size be non-zero. */
919 else if (strcmp (sec->name, ".debug_str") == 0
920 && ISPRINT (header[4]))
921 compressed = false;
922 else
923 *uncompressed_size_p = bfd_getb64 (header + 4);
924 }
925
926 /* Restore compress_status. */
927 sec->compress_status = saved;
928 *compression_header_size_p = compression_header_size;
929 return compressed;
930 }
931
932 /*
933 FUNCTION
934 bfd_is_section_compressed
935
936 SYNOPSIS
937 bool bfd_is_section_compressed
938 (bfd *abfd, asection *section);
939
940 DESCRIPTION
941 Return @code{TRUE} if @var{section} is compressed.
942 */
943
944 bool
945 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
946 {
947 int compression_header_size;
948 bfd_size_type uncompressed_size;
949 unsigned int uncompressed_align_power;
950 enum compression_type ch_type;
951 return (bfd_is_section_compressed_info (abfd, sec,
952 &compression_header_size,
953 &uncompressed_size,
954 &uncompressed_align_power,
955 &ch_type)
956 && compression_header_size >= 0
957 && uncompressed_size > 0);
958 }
959
960 /*
961 FUNCTION
962 bfd_init_section_decompress_status
963
964 SYNOPSIS
965 bool bfd_init_section_decompress_status
966 (bfd *abfd, asection *section);
967
968 DESCRIPTION
969 Record compressed section size, update section size with
970 decompressed size and set compress_status to
971 DECOMPRESS_SECTION_{ZLIB,ZSTD}.
972
973 Return @code{FALSE} if the section is not a valid compressed
974 section. Otherwise, return @code{TRUE}.
975 */
976
977 bool
978 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
979 {
980 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
981 int compression_header_size;
982 int header_size;
983 bfd_size_type uncompressed_size;
984 unsigned int uncompressed_alignment_power = 0;
985 enum compression_type ch_type;
986 z_stream strm;
987
988 compression_header_size = bfd_get_compression_header_size (abfd, sec);
989 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
990 abort ();
991 header_size = compression_header_size ? compression_header_size : 12;
992
993 /* Read the header. */
994 if (sec->rawsize != 0
995 || sec->contents != NULL
996 || sec->compress_status != COMPRESS_SECTION_NONE
997 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
998 {
999 bfd_set_error (bfd_error_invalid_operation);
1000 return false;
1001 }
1002
1003 if (compression_header_size == 0)
1004 {
1005 /* In this case, it should be "ZLIB" followed by the uncompressed
1006 section size, 8 bytes in big-endian order. */
1007 if (! startswith ((char*) header, "ZLIB"))
1008 {
1009 bfd_set_error (bfd_error_wrong_format);
1010 return false;
1011 }
1012 uncompressed_size = bfd_getb64 (header + 4);
1013 ch_type = ch_none;
1014 }
1015 else if (!bfd_check_compression_header (abfd, header, sec,
1016 &ch_type,
1017 &uncompressed_size,
1018 &uncompressed_alignment_power))
1019 {
1020 bfd_set_error (bfd_error_wrong_format);
1021 return false;
1022 }
1023
1024 /* PR28530, reject sizes unsupported by decompress_contents. */
1025 strm.avail_in = sec->size;
1026 strm.avail_out = uncompressed_size;
1027 if (strm.avail_in != sec->size || strm.avail_out != uncompressed_size)
1028 {
1029 bfd_set_error (bfd_error_nonrepresentable_section);
1030 return false;
1031 }
1032
1033 sec->compressed_size = sec->size;
1034 sec->size = uncompressed_size;
1035 bfd_set_section_alignment (sec, uncompressed_alignment_power);
1036 sec->compress_status = (ch_type == ch_compress_zstd
1037 ? DECOMPRESS_SECTION_ZSTD : DECOMPRESS_SECTION_ZLIB);
1038
1039 return true;
1040 }
1041
1042 /*
1043 FUNCTION
1044 bfd_init_section_compress_status
1045
1046 SYNOPSIS
1047 bool bfd_init_section_compress_status
1048 (bfd *abfd, asection *section);
1049
1050 DESCRIPTION
1051 If open for read, compress section, update section size with
1052 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1053
1054 Return @code{FALSE} if the section is not a valid compressed
1055 section. Otherwise, return @code{TRUE}.
1056 */
1057
1058 bool
1059 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
1060 {
1061 bfd_size_type uncompressed_size;
1062 bfd_byte *uncompressed_buffer;
1063
1064 /* Error if not opened for read. */
1065 if (abfd->direction != read_direction
1066 || sec->size == 0
1067 || sec->rawsize != 0
1068 || sec->contents != NULL
1069 || sec->compress_status != COMPRESS_SECTION_NONE)
1070 {
1071 bfd_set_error (bfd_error_invalid_operation);
1072 return false;
1073 }
1074
1075 /* Read in the full section contents and compress it. */
1076 uncompressed_size = sec->size;
1077 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
1078 /* PR 21431 */
1079 if (uncompressed_buffer == NULL)
1080 return false;
1081
1082 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
1083 0, uncompressed_size))
1084 return false;
1085
1086 sec->contents = uncompressed_buffer;
1087 if (bfd_compress_section_contents (abfd, sec) == 0)
1088 {
1089 free (sec->contents);
1090 sec->contents = NULL;
1091 return false;
1092 }
1093 return true;
1094 }
1095
1096 /*
1097 FUNCTION
1098 bfd_compress_section
1099
1100 SYNOPSIS
1101 bool bfd_compress_section
1102 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
1103
1104 DESCRIPTION
1105 If open for write, compress section, update section size with
1106 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1107
1108 Return @code{FALSE} if compression fail. Otherwise, return
1109 @code{TRUE}. UNCOMPRESSED_BUFFER is freed in both cases.
1110 */
1111
1112 bool
1113 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
1114 {
1115 bfd_size_type uncompressed_size = sec->size;
1116
1117 /* Error if not opened for write. */
1118 if (abfd->direction != write_direction
1119 || uncompressed_size == 0
1120 || uncompressed_buffer == NULL
1121 || sec->contents != NULL
1122 || sec->compressed_size != 0
1123 || sec->compress_status != COMPRESS_SECTION_NONE)
1124 {
1125 bfd_set_error (bfd_error_invalid_operation);
1126 return false;
1127 }
1128
1129 sec->contents = uncompressed_buffer;
1130 if (bfd_compress_section_contents (abfd, sec) == 0)
1131 {
1132 free (sec->contents);
1133 sec->contents = NULL;
1134 return false;
1135 }
1136 return true;
1137 }