PR29855, ch_type in bfd_init_section_decompress_status can be uninitialized
[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
31 #define MAX_COMPRESSION_HEADER_SIZE 24
32
33 /*
34 CODE_FRAGMENT
35 .static inline char *
36 .bfd_debug_name_to_zdebug (bfd *abfd, const char *name)
37 .{
38 . size_t len = strlen (name);
39 . char *new_name = (char *) bfd_alloc (abfd, len + 2);
40 . if (new_name == NULL)
41 . return NULL;
42 . new_name[0] = '.';
43 . new_name[1] = 'z';
44 . memcpy (new_name + 2, name + 1, len);
45 . return new_name;
46 .}
47 .
48 .static inline char *
49 .bfd_zdebug_name_to_debug (bfd *abfd, const char *name)
50 .{
51 . size_t len = strlen (name);
52 . char *new_name = (char *) bfd_alloc (abfd, len);
53 . if (new_name == NULL)
54 . return NULL;
55 . new_name[0] = '.';
56 . memcpy (new_name + 1, name + 2, len - 1);
57 . return new_name;
58 .}
59 .
60 */
61
62 static bool
63 decompress_contents (bool is_zstd, bfd_byte *compressed_buffer,
64 bfd_size_type compressed_size,
65 bfd_byte *uncompressed_buffer,
66 bfd_size_type uncompressed_size)
67 {
68 if (is_zstd)
69 {
70 #ifdef HAVE_ZSTD
71 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
72 compressed_buffer, compressed_size);
73 return !ZSTD_isError (ret);
74 #endif
75 }
76
77 z_stream strm;
78 int rc;
79
80 /* It is possible the section consists of several compressed
81 buffers concatenated together, so we uncompress in a loop. */
82 /* PR 18313: The state field in the z_stream structure is supposed
83 to be invisible to the user (ie us), but some compilers will
84 still complain about it being used without initialisation. So
85 we first zero the entire z_stream structure and then set the fields
86 that we need. */
87 memset (& strm, 0, sizeof strm);
88 strm.avail_in = compressed_size;
89 strm.next_in = (Bytef*) compressed_buffer;
90 strm.avail_out = uncompressed_size;
91 /* FIXME: strm.avail_in and strm.avail_out are typically unsigned
92 int. Supporting sizes that don't fit in an unsigned int is
93 possible but will require some rewriting of this function. */
94 if (strm.avail_in != compressed_size || strm.avail_out != uncompressed_size)
95 return false;
96
97 BFD_ASSERT (Z_OK == 0);
98 rc = inflateInit (&strm);
99 while (strm.avail_in > 0 && strm.avail_out > 0)
100 {
101 if (rc != Z_OK)
102 break;
103 strm.next_out = ((Bytef*) uncompressed_buffer
104 + (uncompressed_size - strm.avail_out));
105 rc = inflate (&strm, Z_FINISH);
106 if (rc != Z_STREAM_END)
107 break;
108 rc = inflateReset (&strm);
109 }
110 return inflateEnd (&strm) == Z_OK && rc == Z_OK && strm.avail_out == 0;
111 }
112
113 /* Compress section contents using zlib/zstd and store
114 as the contents field. This function assumes the contents
115 field was allocated using bfd_malloc() or equivalent.
116
117 Return the uncompressed size if the full section contents is
118 compressed successfully. Otherwise return 0. */
119
120 static bfd_size_type
121 bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
122 {
123 bfd_byte *input_buffer;
124 uLong compressed_size;
125 bfd_byte *buffer;
126 bfd_size_type buffer_size;
127 int zlib_size = 0;
128 int orig_header_size;
129 bfd_size_type uncompressed_size;
130 unsigned int uncompressed_alignment_pow;
131 unsigned int ch_type = 0;
132 int new_header_size = bfd_get_compression_header_size (abfd, NULL);
133 bool compressed
134 = bfd_is_section_compressed_info (abfd, sec,
135 &orig_header_size,
136 &uncompressed_size,
137 &uncompressed_alignment_pow,
138 &ch_type);
139 bool update = false;
140
141 /* We shouldn't be trying to decompress unsupported compressed sections. */
142 if (compressed && orig_header_size < 0)
143 abort ();
144
145 /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
146 overhead in .zdebug* section. */
147 if (!new_header_size)
148 new_header_size = 12;
149 if (ch_type == 0)
150 orig_header_size = 12;
151
152 input_buffer = sec->contents;
153 if (compressed)
154 {
155 zlib_size = sec->size - orig_header_size;
156 compressed_size = zlib_size + new_header_size;
157
158 /* If we are converting between zlib-gnu and zlib-gabi then the
159 compressed contents just need to be moved. */
160 update = (ch_type < ELFCOMPRESS_ZSTD
161 && (abfd->flags & BFD_COMPRESS_ZSTD) == 0);
162
163 /* Uncompress when not just moving contents or when compressed
164 is not smaller than uncompressed. */
165 if (!update || compressed_size >= uncompressed_size)
166 {
167 buffer_size = uncompressed_size;
168 buffer = bfd_malloc (buffer_size);
169 if (buffer == NULL)
170 return 0;
171
172 if (!decompress_contents (ch_type == ELFCOMPRESS_ZSTD,
173 input_buffer + orig_header_size,
174 zlib_size, buffer, buffer_size))
175 {
176 bfd_set_error (bfd_error_bad_value);
177 free (buffer);
178 return 0;
179 }
180 free (input_buffer);
181 bfd_set_section_alignment (sec, uncompressed_alignment_pow);
182 sec->contents = buffer;
183 sec->flags |= SEC_IN_MEMORY;
184 sec->compress_status = COMPRESS_SECTION_NONE;
185 sec->size = uncompressed_size;
186 input_buffer = buffer;
187 }
188 }
189
190 if (!update)
191 compressed_size = compressBound (uncompressed_size) + new_header_size;
192
193 buffer_size = compressed_size;
194 buffer = bfd_alloc (abfd, buffer_size);
195 if (buffer == NULL)
196 return 0;
197
198 if (update)
199 {
200 if (compressed_size < uncompressed_size)
201 memcpy (buffer + new_header_size,
202 input_buffer + orig_header_size,
203 zlib_size);
204 }
205 else
206 {
207 if (abfd->flags & BFD_COMPRESS_ZSTD)
208 {
209 #if HAVE_ZSTD
210 compressed_size = ZSTD_compress (buffer + new_header_size,
211 compressed_size,
212 input_buffer,
213 uncompressed_size,
214 ZSTD_CLEVEL_DEFAULT);
215 if (ZSTD_isError (compressed_size))
216 {
217 bfd_release (abfd, buffer);
218 bfd_set_error (bfd_error_bad_value);
219 return 0;
220 }
221 #endif
222 }
223 else if (compress ((Bytef *) buffer + new_header_size, &compressed_size,
224 (const Bytef *) input_buffer, uncompressed_size)
225 != Z_OK)
226 {
227 bfd_release (abfd, buffer);
228 bfd_set_error (bfd_error_bad_value);
229 return 0;
230 }
231
232 compressed_size += new_header_size;
233 }
234
235 /* If compression didn't make the section smaller, keep it uncompressed. */
236 if (compressed_size >= uncompressed_size)
237 {
238 memcpy (buffer, input_buffer, uncompressed_size);
239 elf_section_flags (sec) &= ~SHF_COMPRESSED;
240 sec->compress_status = COMPRESS_SECTION_NONE;
241 }
242 else
243 {
244 sec->size = uncompressed_size;
245 bfd_update_compression_header (abfd, buffer, sec);
246 sec->size = compressed_size;
247 sec->compress_status = COMPRESS_SECTION_DONE;
248 }
249 sec->contents = buffer;
250 sec->flags |= SEC_IN_MEMORY;
251 free (input_buffer);
252 return uncompressed_size;
253 }
254
255 /*
256 FUNCTION
257 bfd_get_full_section_contents
258
259 SYNOPSIS
260 bool bfd_get_full_section_contents
261 (bfd *abfd, asection *section, bfd_byte **ptr);
262
263 DESCRIPTION
264 Read all data from @var{section} in BFD @var{abfd}, decompress
265 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
266 return @var{*ptr} with memory malloc'd by this function.
267
268 Return @code{TRUE} if the full section contents is retrieved
269 successfully. If the section has no contents then this function
270 returns @code{TRUE} but @var{*ptr} is set to NULL.
271 */
272
273 bool
274 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
275 {
276 bfd_size_type sz = bfd_get_section_limit_octets (abfd, sec);
277 bfd_byte *p = *ptr;
278 bool ret;
279 bfd_size_type save_size;
280 bfd_size_type save_rawsize;
281 bfd_byte *compressed_buffer;
282 unsigned int compression_header_size;
283 const unsigned int compress_status = sec->compress_status;
284
285 if (sz == 0)
286 {
287 *ptr = NULL;
288 return true;
289 }
290
291 if (p == NULL
292 && compress_status != COMPRESS_SECTION_DONE
293 && _bfd_section_size_insane (abfd, sec))
294 {
295 /* PR 24708: Avoid attempts to allocate a ridiculous amount
296 of memory. */
297 _bfd_error_handler
298 /* xgettext:c-format */
299 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
300 abfd, sec, (uint64_t) sz);
301 return false;
302 }
303
304 switch (compress_status)
305 {
306 case COMPRESS_SECTION_NONE:
307 if (p == NULL)
308 {
309 p = (bfd_byte *) bfd_malloc (sz);
310 if (p == NULL)
311 {
312 /* PR 20801: Provide a more helpful error message. */
313 if (bfd_get_error () == bfd_error_no_memory)
314 _bfd_error_handler
315 /* xgettext:c-format */
316 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
317 abfd, sec, (uint64_t) sz);
318 return false;
319 }
320 }
321
322 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
323 {
324 if (*ptr != p)
325 free (p);
326 return false;
327 }
328 *ptr = p;
329 return true;
330
331 case DECOMPRESS_SECTION_ZLIB:
332 case DECOMPRESS_SECTION_ZSTD:
333 /* Read in the full compressed section contents. */
334 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
335 if (compressed_buffer == NULL)
336 return false;
337 save_rawsize = sec->rawsize;
338 save_size = sec->size;
339 /* Clear rawsize, set size to compressed size and set compress_status
340 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
341 the uncompressed size, bfd_get_section_contents will fail. */
342 sec->rawsize = 0;
343 sec->size = sec->compressed_size;
344 sec->compress_status = COMPRESS_SECTION_NONE;
345 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
346 0, sec->compressed_size);
347 /* Restore rawsize and size. */
348 sec->rawsize = save_rawsize;
349 sec->size = save_size;
350 sec->compress_status = compress_status;
351 if (!ret)
352 goto fail_compressed;
353
354 if (p == NULL)
355 p = (bfd_byte *) bfd_malloc (sz);
356 if (p == NULL)
357 goto fail_compressed;
358
359 compression_header_size = bfd_get_compression_header_size (abfd, sec);
360 if (compression_header_size == 0)
361 /* Set header size to the zlib header size if it is a
362 SHF_COMPRESSED section. */
363 compression_header_size = 12;
364 bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
365 if (!decompress_contents (
366 is_zstd, compressed_buffer + compression_header_size,
367 sec->compressed_size - compression_header_size, p, sz))
368 {
369 bfd_set_error (bfd_error_bad_value);
370 if (p != *ptr)
371 free (p);
372 fail_compressed:
373 free (compressed_buffer);
374 return false;
375 }
376
377 free (compressed_buffer);
378 *ptr = p;
379 return true;
380
381 case COMPRESS_SECTION_DONE:
382 if (sec->contents == NULL)
383 return false;
384 if (p == NULL)
385 {
386 p = (bfd_byte *) bfd_malloc (sz);
387 if (p == NULL)
388 return false;
389 *ptr = p;
390 }
391 /* PR 17512; file: 5bc29788. */
392 if (p != sec->contents)
393 memcpy (p, sec->contents, sz);
394 return true;
395
396 default:
397 abort ();
398 }
399 }
400
401 /*
402 FUNCTION
403 bfd_is_section_compressed_info
404
405 SYNOPSIS
406 bool bfd_is_section_compressed_info
407 (bfd *abfd, asection *section,
408 int *compression_header_size_p,
409 bfd_size_type *uncompressed_size_p,
410 unsigned int *uncompressed_alignment_power_p,
411 unsigned int *ch_type);
412
413 DESCRIPTION
414 Return @code{TRUE} if @var{section} is compressed. Compression
415 header size is returned in @var{compression_header_size_p},
416 uncompressed size is returned in @var{uncompressed_size_p}
417 and the uncompressed data alignement power is returned in
418 @var{uncompressed_align_pow_p}. If compression is
419 unsupported, compression header size is returned with -1
420 and uncompressed size is returned with 0.
421 */
422
423 bool
424 bfd_is_section_compressed_info (bfd *abfd, sec_ptr sec,
425 int *compression_header_size_p,
426 bfd_size_type *uncompressed_size_p,
427 unsigned int *uncompressed_align_pow_p,
428 unsigned int *ch_type)
429 {
430 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
431 int compression_header_size;
432 int header_size;
433 unsigned int saved = sec->compress_status;
434 bool compressed;
435
436 *uncompressed_align_pow_p = 0;
437
438 compression_header_size = bfd_get_compression_header_size (abfd, sec);
439 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
440 abort ();
441 header_size = compression_header_size ? compression_header_size : 12;
442
443 /* Don't decompress the section. */
444 sec->compress_status = COMPRESS_SECTION_NONE;
445
446 /* Read the header. */
447 if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
448 {
449 if (compression_header_size == 0)
450 /* In this case, it should be "ZLIB" followed by the uncompressed
451 section size, 8 bytes in big-endian order. */
452 compressed = startswith ((char*) header , "ZLIB");
453 else
454 compressed = true;
455 }
456 else
457 compressed = false;
458
459 *uncompressed_size_p = sec->size;
460 if (compressed)
461 {
462 if (compression_header_size != 0)
463 {
464 if (!bfd_check_compression_header (abfd, header, sec, ch_type,
465 uncompressed_size_p,
466 uncompressed_align_pow_p))
467 compression_header_size = -1;
468 }
469 /* Check for the pathalogical case of a debug string section that
470 contains the string ZLIB.... as the first entry. We assume that
471 no uncompressed .debug_str section would ever be big enough to
472 have the first byte of its (big-endian) size be non-zero. */
473 else if (strcmp (sec->name, ".debug_str") == 0
474 && ISPRINT (header[4]))
475 compressed = false;
476 else
477 *uncompressed_size_p = bfd_getb64 (header + 4);
478 }
479
480 /* Restore compress_status. */
481 sec->compress_status = saved;
482 *compression_header_size_p = compression_header_size;
483 return compressed;
484 }
485
486 /*
487 FUNCTION
488 bfd_is_section_compressed
489
490 SYNOPSIS
491 bool bfd_is_section_compressed
492 (bfd *abfd, asection *section);
493
494 DESCRIPTION
495 Return @code{TRUE} if @var{section} is compressed.
496 */
497
498 bool
499 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
500 {
501 int compression_header_size;
502 bfd_size_type uncompressed_size;
503 unsigned int uncompressed_align_power;
504 unsigned int ch_type;
505 return (bfd_is_section_compressed_info (abfd, sec,
506 &compression_header_size,
507 &uncompressed_size,
508 &uncompressed_align_power,
509 &ch_type)
510 && compression_header_size >= 0
511 && uncompressed_size > 0);
512 }
513
514 /*
515 FUNCTION
516 bfd_init_section_decompress_status
517
518 SYNOPSIS
519 bool bfd_init_section_decompress_status
520 (bfd *abfd, asection *section);
521
522 DESCRIPTION
523 Record compressed section size, update section size with
524 decompressed size and set compress_status to
525 DECOMPRESS_SECTION_{ZLIB,ZSTD}.
526
527 Return @code{FALSE} if the section is not a valid compressed
528 section. Otherwise, return @code{TRUE}.
529 */
530
531 bool
532 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
533 {
534 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
535 int compression_header_size;
536 int header_size;
537 bfd_size_type uncompressed_size;
538 unsigned int uncompressed_alignment_power = 0;
539 unsigned int ch_type;
540 z_stream strm;
541
542 compression_header_size = bfd_get_compression_header_size (abfd, sec);
543 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
544 abort ();
545 header_size = compression_header_size ? compression_header_size : 12;
546
547 /* Read the header. */
548 if (sec->rawsize != 0
549 || sec->contents != NULL
550 || sec->compress_status != COMPRESS_SECTION_NONE
551 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
552 {
553 bfd_set_error (bfd_error_invalid_operation);
554 return false;
555 }
556
557 if (compression_header_size == 0)
558 {
559 /* In this case, it should be "ZLIB" followed by the uncompressed
560 section size, 8 bytes in big-endian order. */
561 if (! startswith ((char*) header, "ZLIB"))
562 {
563 bfd_set_error (bfd_error_wrong_format);
564 return false;
565 }
566 uncompressed_size = bfd_getb64 (header + 4);
567 ch_type = 0;
568 }
569 else if (!bfd_check_compression_header (abfd, header, sec,
570 &ch_type,
571 &uncompressed_size,
572 &uncompressed_alignment_power))
573 {
574 bfd_set_error (bfd_error_wrong_format);
575 return false;
576 }
577
578 /* PR28530, reject sizes unsupported by decompress_contents. */
579 strm.avail_in = sec->size;
580 strm.avail_out = uncompressed_size;
581 if (strm.avail_in != sec->size || strm.avail_out != uncompressed_size)
582 {
583 bfd_set_error (bfd_error_nonrepresentable_section);
584 return false;
585 }
586
587 sec->compressed_size = sec->size;
588 sec->size = uncompressed_size;
589 bfd_set_section_alignment (sec, uncompressed_alignment_power);
590 sec->compress_status = (ch_type == ELFCOMPRESS_ZSTD
591 ? DECOMPRESS_SECTION_ZSTD : DECOMPRESS_SECTION_ZLIB);
592
593 return true;
594 }
595
596 /*
597 FUNCTION
598 bfd_init_section_compress_status
599
600 SYNOPSIS
601 bool bfd_init_section_compress_status
602 (bfd *abfd, asection *section);
603
604 DESCRIPTION
605 If open for read, compress section, update section size with
606 compressed size and set compress_status to COMPRESS_SECTION_DONE.
607
608 Return @code{FALSE} if the section is not a valid compressed
609 section. Otherwise, return @code{TRUE}.
610 */
611
612 bool
613 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
614 {
615 bfd_size_type uncompressed_size;
616 bfd_byte *uncompressed_buffer;
617
618 /* Error if not opened for read. */
619 if (abfd->direction != read_direction
620 || sec->size == 0
621 || sec->rawsize != 0
622 || sec->contents != NULL
623 || sec->compress_status != COMPRESS_SECTION_NONE)
624 {
625 bfd_set_error (bfd_error_invalid_operation);
626 return false;
627 }
628
629 /* Read in the full section contents and compress it. */
630 uncompressed_size = sec->size;
631 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
632 /* PR 21431 */
633 if (uncompressed_buffer == NULL)
634 return false;
635
636 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
637 0, uncompressed_size))
638 return false;
639
640 sec->contents = uncompressed_buffer;
641 if (bfd_compress_section_contents (abfd, sec) == 0)
642 {
643 free (sec->contents);
644 sec->contents = NULL;
645 return false;
646 }
647 return true;
648 }
649
650 /*
651 FUNCTION
652 bfd_compress_section
653
654 SYNOPSIS
655 bool bfd_compress_section
656 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
657
658 DESCRIPTION
659 If open for write, compress section, update section size with
660 compressed size and set compress_status to COMPRESS_SECTION_DONE.
661
662 Return @code{FALSE} if compression fail. Otherwise, return
663 @code{TRUE}. UNCOMPRESSED_BUFFER is freed in both cases.
664 */
665
666 bool
667 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
668 {
669 bfd_size_type uncompressed_size = sec->size;
670
671 /* Error if not opened for write. */
672 if (abfd->direction != write_direction
673 || uncompressed_size == 0
674 || uncompressed_buffer == NULL
675 || sec->contents != NULL
676 || sec->compressed_size != 0
677 || sec->compress_status != COMPRESS_SECTION_NONE)
678 {
679 bfd_set_error (bfd_error_invalid_operation);
680 return false;
681 }
682
683 sec->contents = uncompressed_buffer;
684 if (bfd_compress_section_contents (abfd, sec) == 0)
685 {
686 free (sec->contents);
687 sec->contents = NULL;
688 return false;
689 }
690 return true;
691 }