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