Compression header enum
[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 .enum compression_type
36 .{
37 . ch_none = 0,
38 . ch_compress_zlib = 1 , {* Compressed with zlib. *}
39 . ch_compress_zstd = 2 {* Compressed with zstd (www.zstandard.org). *}
40 .};
41 .
42 .static inline char *
43 .bfd_debug_name_to_zdebug (bfd *abfd, const char *name)
44 .{
45 . size_t len = strlen (name);
46 . char *new_name = (char *) bfd_alloc (abfd, len + 2);
47 . if (new_name == NULL)
48 . return NULL;
49 . new_name[0] = '.';
50 . new_name[1] = 'z';
51 . memcpy (new_name + 2, name + 1, len);
52 . return new_name;
53 .}
54 .
55 .static inline char *
56 .bfd_zdebug_name_to_debug (bfd *abfd, const char *name)
57 .{
58 . size_t len = strlen (name);
59 . char *new_name = (char *) bfd_alloc (abfd, len);
60 . if (new_name == NULL)
61 . return NULL;
62 . new_name[0] = '.';
63 . memcpy (new_name + 1, name + 2, len - 1);
64 . return new_name;
65 .}
66 .
67 */
68
69 /*
70 FUNCTION
71 bfd_update_compression_header
72
73 SYNOPSIS
74 void bfd_update_compression_header
75 (bfd *abfd, bfd_byte *contents, asection *sec);
76
77 DESCRIPTION
78 Set the compression header at CONTENTS of SEC in ABFD and update
79 elf_section_flags for compression.
80 */
81
82 void
83 bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
84 asection *sec)
85 {
86 if ((abfd->flags & BFD_COMPRESS) == 0)
87 abort ();
88
89 switch (bfd_get_flavour (abfd))
90 {
91 case bfd_target_elf_flavour:
92 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
93 {
94 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
95 struct bfd_elf_section_data * esd = elf_section_data (sec);
96 enum compression_type ch_type = (abfd->flags & BFD_COMPRESS_ZSTD
97 ? ch_compress_zstd
98 : ch_compress_zlib);
99
100 /* Set the SHF_COMPRESSED bit. */
101 elf_section_flags (sec) |= SHF_COMPRESSED;
102
103 if (bed->s->elfclass == ELFCLASS32)
104 {
105 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
106 bfd_put_32 (abfd, ch_type, &echdr->ch_type);
107 bfd_put_32 (abfd, sec->size, &echdr->ch_size);
108 bfd_put_32 (abfd, 1u << sec->alignment_power,
109 &echdr->ch_addralign);
110 /* bfd_log2 (alignof (Elf32_Chdr)) */
111 bfd_set_section_alignment (sec, 2);
112 esd->this_hdr.sh_addralign = 4;
113 }
114 else
115 {
116 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
117 bfd_put_32 (abfd, ch_type, &echdr->ch_type);
118 bfd_put_32 (abfd, 0, &echdr->ch_reserved);
119 bfd_put_64 (abfd, sec->size, &echdr->ch_size);
120 bfd_put_64 (abfd, UINT64_C (1) << sec->alignment_power,
121 &echdr->ch_addralign);
122 /* bfd_log2 (alignof (Elf64_Chdr)) */
123 bfd_set_section_alignment (sec, 3);
124 esd->this_hdr.sh_addralign = 8;
125 }
126 break;
127 }
128
129 /* Clear the SHF_COMPRESSED bit. */
130 elf_section_flags (sec) &= ~SHF_COMPRESSED;
131 /* Fall through. */
132
133 default:
134 /* Write the zlib header. It should be "ZLIB" followed by
135 the uncompressed section size, 8 bytes in big-endian
136 order. */
137 memcpy (contents, "ZLIB", 4);
138 bfd_putb64 (sec->size, contents + 4);
139 /* No way to keep the original alignment, just use 1 always. */
140 bfd_set_section_alignment (sec, 0);
141 break;
142 }
143 }
144
145 /* Check the compression header at CONTENTS of SEC in ABFD and store the
146 ch_type in CH_TYPE, uncompressed size in UNCOMPRESSED_SIZE, and the
147 uncompressed data alignment in UNCOMPRESSED_ALIGNMENT_POWER if the
148 compression header is valid. */
149
150 static bool
151 bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
152 asection *sec,
153 enum compression_type *ch_type,
154 bfd_size_type *uncompressed_size,
155 unsigned int *uncompressed_alignment_power)
156 {
157 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
158 && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
159 {
160 Elf_Internal_Chdr chdr;
161 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
162 if (bed->s->elfclass == ELFCLASS32)
163 {
164 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
165 chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
166 chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
167 chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
168 }
169 else
170 {
171 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
172 chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
173 chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
174 chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
175 }
176 *ch_type = chdr.ch_type;
177 if ((chdr.ch_type == ch_compress_zlib
178 || chdr.ch_type == ch_compress_zstd)
179 && chdr.ch_addralign == (chdr.ch_addralign & -chdr.ch_addralign))
180 {
181 *uncompressed_size = chdr.ch_size;
182 *uncompressed_alignment_power = bfd_log2 (chdr.ch_addralign);
183 return true;
184 }
185 }
186
187 return false;
188 }
189
190 /*
191 FUNCTION
192 bfd_get_compression_header_size
193
194 SYNOPSIS
195 int bfd_get_compression_header_size (bfd *abfd, asection *sec);
196
197 DESCRIPTION
198 Return the size of the compression header of SEC in ABFD.
199
200 RETURNS
201 Return the size of the compression header in bytes.
202 */
203
204 int
205 bfd_get_compression_header_size (bfd *abfd, asection *sec)
206 {
207 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
208 {
209 if (sec == NULL)
210 {
211 if (!(abfd->flags & BFD_COMPRESS_GABI))
212 return 0;
213 }
214 else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
215 return 0;
216
217 if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
218 return sizeof (Elf32_External_Chdr);
219 else
220 return sizeof (Elf64_External_Chdr);
221 }
222
223 return 0;
224 }
225
226 /*
227 FUNCTION
228 bfd_convert_section_size
229
230 SYNOPSIS
231 bfd_size_type bfd_convert_section_size
232 (bfd *ibfd, asection *isec, bfd *obfd, bfd_size_type size);
233
234 DESCRIPTION
235 Convert the size @var{size} of the section @var{isec} in input
236 BFD @var{ibfd} to the section size in output BFD @var{obfd}.
237 */
238
239 bfd_size_type
240 bfd_convert_section_size (bfd *ibfd, sec_ptr isec, bfd *obfd,
241 bfd_size_type size)
242 {
243 bfd_size_type hdr_size;
244
245 /* Do nothing if either input or output aren't ELF. */
246 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
247 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
248 return size;
249
250 /* Do nothing if ELF classes of input and output are the same. */
251 if (get_elf_backend_data (ibfd)->s->elfclass
252 == get_elf_backend_data (obfd)->s->elfclass)
253 return size;
254
255 /* Convert GNU property size. */
256 if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
257 return _bfd_elf_convert_gnu_property_size (ibfd, obfd);
258
259 /* Do nothing if input file will be decompressed. */
260 if ((ibfd->flags & BFD_DECOMPRESS))
261 return size;
262
263 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
264 hdr_size = bfd_get_compression_header_size (ibfd, isec);
265 if (hdr_size == 0)
266 return size;
267
268 /* Adjust the size of the output SHF_COMPRESSED section. */
269 if (hdr_size == sizeof (Elf32_External_Chdr))
270 return (size - sizeof (Elf32_External_Chdr)
271 + sizeof (Elf64_External_Chdr));
272 else
273 return (size - sizeof (Elf64_External_Chdr)
274 + sizeof (Elf32_External_Chdr));
275 }
276
277 /*
278 FUNCTION
279 bfd_convert_section_contents
280
281 SYNOPSIS
282 bool bfd_convert_section_contents
283 (bfd *ibfd, asection *isec, bfd *obfd,
284 bfd_byte **ptr, bfd_size_type *ptr_size);
285
286 DESCRIPTION
287 Convert the contents, stored in @var{*ptr}, of the section
288 @var{isec} in input BFD @var{ibfd} to output BFD @var{obfd}
289 if needed. The original buffer pointed to by @var{*ptr} may
290 be freed and @var{*ptr} is returned with memory malloc'd by this
291 function, and the new size written to @var{ptr_size}.
292 */
293
294 bool
295 bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd,
296 bfd_byte **ptr, bfd_size_type *ptr_size)
297 {
298 bfd_byte *contents;
299 bfd_size_type ihdr_size, ohdr_size, size;
300 Elf_Internal_Chdr chdr;
301 bool use_memmove;
302
303 /* Do nothing if either input or output aren't ELF. */
304 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
305 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
306 return true;
307
308 /* Do nothing if ELF classes of input and output are the same. */
309 if (get_elf_backend_data (ibfd)->s->elfclass
310 == get_elf_backend_data (obfd)->s->elfclass)
311 return true;
312
313 /* Convert GNU properties. */
314 if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
315 return _bfd_elf_convert_gnu_properties (ibfd, isec, obfd, ptr,
316 ptr_size);
317
318 /* Do nothing if input file will be decompressed. */
319 if ((ibfd->flags & BFD_DECOMPRESS))
320 return true;
321
322 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
323 ihdr_size = bfd_get_compression_header_size (ibfd, isec);
324 if (ihdr_size == 0)
325 return true;
326
327 /* PR 25221. Check for corrupt input sections. */
328 if (ihdr_size > bfd_get_section_limit (ibfd, isec))
329 /* FIXME: Issue a warning about a corrupt
330 compression header size field ? */
331 return false;
332
333 contents = *ptr;
334
335 /* Convert the contents of the input SHF_COMPRESSED section to
336 output. Get the input compression header and the size of the
337 output compression header. */
338 if (ihdr_size == sizeof (Elf32_External_Chdr))
339 {
340 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
341 chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
342 chdr.ch_size = bfd_get_32 (ibfd, &echdr->ch_size);
343 chdr.ch_addralign = bfd_get_32 (ibfd, &echdr->ch_addralign);
344
345 ohdr_size = sizeof (Elf64_External_Chdr);
346
347 use_memmove = false;
348 }
349 else if (ihdr_size != sizeof (Elf64_External_Chdr))
350 {
351 /* FIXME: Issue a warning about a corrupt
352 compression header size field ? */
353 return false;
354 }
355 else
356 {
357 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
358 chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
359 chdr.ch_size = bfd_get_64 (ibfd, &echdr->ch_size);
360 chdr.ch_addralign = bfd_get_64 (ibfd, &echdr->ch_addralign);
361
362 ohdr_size = sizeof (Elf32_External_Chdr);
363 use_memmove = true;
364 }
365
366 size = bfd_section_size (isec) - ihdr_size + ohdr_size;
367 if (!use_memmove)
368 {
369 contents = (bfd_byte *) bfd_malloc (size);
370 if (contents == NULL)
371 return false;
372 }
373
374 /* Write out the output compression header. */
375 if (ohdr_size == sizeof (Elf32_External_Chdr))
376 {
377 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
378 bfd_put_32 (obfd, chdr.ch_type, &echdr->ch_type);
379 bfd_put_32 (obfd, chdr.ch_size, &echdr->ch_size);
380 bfd_put_32 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
381 }
382 else
383 {
384 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
385 bfd_put_32 (obfd, chdr.ch_type, &echdr->ch_type);
386 bfd_put_32 (obfd, 0, &echdr->ch_reserved);
387 bfd_put_64 (obfd, chdr.ch_size, &echdr->ch_size);
388 bfd_put_64 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
389 }
390
391 /* Copy the compressed contents. */
392 if (use_memmove)
393 memmove (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
394 else
395 {
396 memcpy (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
397 free (*ptr);
398 *ptr = contents;
399 }
400
401 *ptr_size = size;
402 return true;
403 }
404
405 static bool
406 decompress_contents (bool is_zstd, bfd_byte *compressed_buffer,
407 bfd_size_type compressed_size,
408 bfd_byte *uncompressed_buffer,
409 bfd_size_type uncompressed_size)
410 {
411 if (is_zstd)
412 {
413 #ifdef HAVE_ZSTD
414 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
415 compressed_buffer, compressed_size);
416 return !ZSTD_isError (ret);
417 #endif
418 }
419
420 z_stream strm;
421 int rc;
422
423 /* It is possible the section consists of several compressed
424 buffers concatenated together, so we uncompress in a loop. */
425 /* PR 18313: The state field in the z_stream structure is supposed
426 to be invisible to the user (ie us), but some compilers will
427 still complain about it being used without initialisation. So
428 we first zero the entire z_stream structure and then set the fields
429 that we need. */
430 memset (& strm, 0, sizeof strm);
431 strm.avail_in = compressed_size;
432 strm.next_in = (Bytef*) compressed_buffer;
433 strm.avail_out = uncompressed_size;
434 /* FIXME: strm.avail_in and strm.avail_out are typically unsigned
435 int. Supporting sizes that don't fit in an unsigned int is
436 possible but will require some rewriting of this function. */
437 if (strm.avail_in != compressed_size || strm.avail_out != uncompressed_size)
438 return false;
439
440 BFD_ASSERT (Z_OK == 0);
441 rc = inflateInit (&strm);
442 while (strm.avail_in > 0 && strm.avail_out > 0)
443 {
444 if (rc != Z_OK)
445 break;
446 strm.next_out = ((Bytef*) uncompressed_buffer
447 + (uncompressed_size - strm.avail_out));
448 rc = inflate (&strm, Z_FINISH);
449 if (rc != Z_STREAM_END)
450 break;
451 rc = inflateReset (&strm);
452 }
453 return inflateEnd (&strm) == Z_OK && rc == Z_OK && strm.avail_out == 0;
454 }
455
456 /* Compress section contents using zlib/zstd and store
457 as the contents field. This function assumes the contents
458 field was allocated using bfd_malloc() or equivalent.
459
460 Return the uncompressed size if the full section contents is
461 compressed successfully. Otherwise return 0. */
462
463 static bfd_size_type
464 bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
465 {
466 bfd_byte *input_buffer;
467 uLong compressed_size;
468 bfd_byte *buffer;
469 bfd_size_type buffer_size;
470 int zlib_size = 0;
471 int orig_header_size;
472 bfd_size_type uncompressed_size;
473 unsigned int uncompressed_alignment_pow;
474 enum compression_type ch_type = ch_none;
475 int new_header_size = bfd_get_compression_header_size (abfd, NULL);
476 bool compressed
477 = bfd_is_section_compressed_info (abfd, sec,
478 &orig_header_size,
479 &uncompressed_size,
480 &uncompressed_alignment_pow,
481 &ch_type);
482 bool update = false;
483
484 /* We shouldn't be trying to decompress unsupported compressed sections. */
485 if (compressed && orig_header_size < 0)
486 abort ();
487
488 /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
489 overhead in .zdebug* section. */
490 if (!new_header_size)
491 new_header_size = 12;
492 if (ch_type == ch_none)
493 orig_header_size = 12;
494
495 input_buffer = sec->contents;
496 if (compressed)
497 {
498 zlib_size = sec->size - orig_header_size;
499 compressed_size = zlib_size + new_header_size;
500
501 /* If we are converting between zlib-gnu and zlib-gabi then the
502 compressed contents just need to be moved. */
503 update = (ch_type < ch_compress_zstd
504 && (abfd->flags & BFD_COMPRESS_ZSTD) == 0);
505
506 /* Uncompress when not just moving contents or when compressed
507 is not smaller than uncompressed. */
508 if (!update || compressed_size >= uncompressed_size)
509 {
510 buffer_size = uncompressed_size;
511 buffer = bfd_malloc (buffer_size);
512 if (buffer == NULL)
513 return 0;
514
515 if (!decompress_contents (ch_type == ch_compress_zstd,
516 input_buffer + orig_header_size,
517 zlib_size, buffer, buffer_size))
518 {
519 bfd_set_error (bfd_error_bad_value);
520 free (buffer);
521 return 0;
522 }
523 free (input_buffer);
524 bfd_set_section_alignment (sec, uncompressed_alignment_pow);
525 sec->contents = buffer;
526 sec->flags |= SEC_IN_MEMORY;
527 sec->compress_status = COMPRESS_SECTION_NONE;
528 sec->size = uncompressed_size;
529 input_buffer = buffer;
530 }
531 }
532
533 if (!update)
534 compressed_size = compressBound (uncompressed_size) + new_header_size;
535
536 buffer_size = compressed_size;
537 buffer = bfd_alloc (abfd, buffer_size);
538 if (buffer == NULL)
539 return 0;
540
541 if (update)
542 {
543 if (compressed_size < uncompressed_size)
544 memcpy (buffer + new_header_size,
545 input_buffer + orig_header_size,
546 zlib_size);
547 }
548 else
549 {
550 if (abfd->flags & BFD_COMPRESS_ZSTD)
551 {
552 #if HAVE_ZSTD
553 compressed_size = ZSTD_compress (buffer + new_header_size,
554 compressed_size,
555 input_buffer,
556 uncompressed_size,
557 ZSTD_CLEVEL_DEFAULT);
558 if (ZSTD_isError (compressed_size))
559 {
560 bfd_release (abfd, buffer);
561 bfd_set_error (bfd_error_bad_value);
562 return 0;
563 }
564 #endif
565 }
566 else if (compress ((Bytef *) buffer + new_header_size, &compressed_size,
567 (const Bytef *) input_buffer, uncompressed_size)
568 != Z_OK)
569 {
570 bfd_release (abfd, buffer);
571 bfd_set_error (bfd_error_bad_value);
572 return 0;
573 }
574
575 compressed_size += new_header_size;
576 }
577
578 /* If compression didn't make the section smaller, keep it uncompressed. */
579 if (compressed_size >= uncompressed_size)
580 {
581 memcpy (buffer, input_buffer, uncompressed_size);
582 elf_section_flags (sec) &= ~SHF_COMPRESSED;
583 sec->compress_status = COMPRESS_SECTION_NONE;
584 }
585 else
586 {
587 sec->size = uncompressed_size;
588 bfd_update_compression_header (abfd, buffer, sec);
589 sec->size = compressed_size;
590 sec->compress_status = COMPRESS_SECTION_DONE;
591 }
592 sec->contents = buffer;
593 sec->flags |= SEC_IN_MEMORY;
594 free (input_buffer);
595 return uncompressed_size;
596 }
597
598 /*
599 FUNCTION
600 bfd_get_full_section_contents
601
602 SYNOPSIS
603 bool bfd_get_full_section_contents
604 (bfd *abfd, asection *section, bfd_byte **ptr);
605
606 DESCRIPTION
607 Read all data from @var{section} in BFD @var{abfd}, decompress
608 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
609 return @var{*ptr} with memory malloc'd by this function.
610
611 Return @code{TRUE} if the full section contents is retrieved
612 successfully. If the section has no contents then this function
613 returns @code{TRUE} but @var{*ptr} is set to NULL.
614 */
615
616 bool
617 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
618 {
619 bfd_size_type sz = bfd_get_section_limit_octets (abfd, sec);
620 bfd_byte *p = *ptr;
621 bool ret;
622 bfd_size_type save_size;
623 bfd_size_type save_rawsize;
624 bfd_byte *compressed_buffer;
625 unsigned int compression_header_size;
626 const unsigned int compress_status = sec->compress_status;
627
628 if (sz == 0)
629 {
630 *ptr = NULL;
631 return true;
632 }
633
634 if (p == NULL
635 && compress_status != COMPRESS_SECTION_DONE
636 && _bfd_section_size_insane (abfd, sec))
637 {
638 /* PR 24708: Avoid attempts to allocate a ridiculous amount
639 of memory. */
640 _bfd_error_handler
641 /* xgettext:c-format */
642 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
643 abfd, sec, (uint64_t) sz);
644 return false;
645 }
646
647 switch (compress_status)
648 {
649 case COMPRESS_SECTION_NONE:
650 if (p == NULL)
651 {
652 p = (bfd_byte *) bfd_malloc (sz);
653 if (p == NULL)
654 {
655 /* PR 20801: Provide a more helpful error message. */
656 if (bfd_get_error () == bfd_error_no_memory)
657 _bfd_error_handler
658 /* xgettext:c-format */
659 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
660 abfd, sec, (uint64_t) sz);
661 return false;
662 }
663 }
664
665 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
666 {
667 if (*ptr != p)
668 free (p);
669 return false;
670 }
671 *ptr = p;
672 return true;
673
674 case DECOMPRESS_SECTION_ZLIB:
675 case DECOMPRESS_SECTION_ZSTD:
676 /* Read in the full compressed section contents. */
677 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
678 if (compressed_buffer == NULL)
679 return false;
680 save_rawsize = sec->rawsize;
681 save_size = sec->size;
682 /* Clear rawsize, set size to compressed size and set compress_status
683 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
684 the uncompressed size, bfd_get_section_contents will fail. */
685 sec->rawsize = 0;
686 sec->size = sec->compressed_size;
687 sec->compress_status = COMPRESS_SECTION_NONE;
688 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
689 0, sec->compressed_size);
690 /* Restore rawsize and size. */
691 sec->rawsize = save_rawsize;
692 sec->size = save_size;
693 sec->compress_status = compress_status;
694 if (!ret)
695 goto fail_compressed;
696
697 if (p == NULL)
698 p = (bfd_byte *) bfd_malloc (sz);
699 if (p == NULL)
700 goto fail_compressed;
701
702 compression_header_size = bfd_get_compression_header_size (abfd, sec);
703 if (compression_header_size == 0)
704 /* Set header size to the zlib header size if it is a
705 SHF_COMPRESSED section. */
706 compression_header_size = 12;
707 bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
708 if (!decompress_contents (
709 is_zstd, compressed_buffer + compression_header_size,
710 sec->compressed_size - compression_header_size, p, sz))
711 {
712 bfd_set_error (bfd_error_bad_value);
713 if (p != *ptr)
714 free (p);
715 fail_compressed:
716 free (compressed_buffer);
717 return false;
718 }
719
720 free (compressed_buffer);
721 *ptr = p;
722 return true;
723
724 case COMPRESS_SECTION_DONE:
725 if (sec->contents == NULL)
726 return false;
727 if (p == NULL)
728 {
729 p = (bfd_byte *) bfd_malloc (sz);
730 if (p == NULL)
731 return false;
732 *ptr = p;
733 }
734 /* PR 17512; file: 5bc29788. */
735 if (p != sec->contents)
736 memcpy (p, sec->contents, sz);
737 return true;
738
739 default:
740 abort ();
741 }
742 }
743
744 /*
745 FUNCTION
746 bfd_is_section_compressed_info
747
748 SYNOPSIS
749 bool bfd_is_section_compressed_info
750 (bfd *abfd, asection *section,
751 int *compression_header_size_p,
752 bfd_size_type *uncompressed_size_p,
753 unsigned int *uncompressed_alignment_power_p,
754 enum compression_type *ch_type);
755
756 DESCRIPTION
757 Return @code{TRUE} if @var{section} is compressed. Compression
758 header size is returned in @var{compression_header_size_p},
759 uncompressed size is returned in @var{uncompressed_size_p}
760 and the uncompressed data alignement power is returned in
761 @var{uncompressed_align_pow_p}. If compression is
762 unsupported, compression header size is returned with -1
763 and uncompressed size is returned with 0.
764 */
765
766 bool
767 bfd_is_section_compressed_info (bfd *abfd, sec_ptr sec,
768 int *compression_header_size_p,
769 bfd_size_type *uncompressed_size_p,
770 unsigned int *uncompressed_align_pow_p,
771 enum compression_type *ch_type)
772 {
773 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
774 int compression_header_size;
775 int header_size;
776 unsigned int saved = sec->compress_status;
777 bool compressed;
778
779 *uncompressed_align_pow_p = 0;
780
781 compression_header_size = bfd_get_compression_header_size (abfd, sec);
782 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
783 abort ();
784 header_size = compression_header_size ? compression_header_size : 12;
785
786 /* Don't decompress the section. */
787 sec->compress_status = COMPRESS_SECTION_NONE;
788
789 /* Read the header. */
790 if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
791 {
792 if (compression_header_size == 0)
793 /* In this case, it should be "ZLIB" followed by the uncompressed
794 section size, 8 bytes in big-endian order. */
795 compressed = startswith ((char*) header , "ZLIB");
796 else
797 compressed = true;
798 }
799 else
800 compressed = false;
801
802 *uncompressed_size_p = sec->size;
803 if (compressed)
804 {
805 if (compression_header_size != 0)
806 {
807 if (!bfd_check_compression_header (abfd, header, sec, ch_type,
808 uncompressed_size_p,
809 uncompressed_align_pow_p))
810 compression_header_size = -1;
811 }
812 /* Check for the pathalogical case of a debug string section that
813 contains the string ZLIB.... as the first entry. We assume that
814 no uncompressed .debug_str section would ever be big enough to
815 have the first byte of its (big-endian) size be non-zero. */
816 else if (strcmp (sec->name, ".debug_str") == 0
817 && ISPRINT (header[4]))
818 compressed = false;
819 else
820 *uncompressed_size_p = bfd_getb64 (header + 4);
821 }
822
823 /* Restore compress_status. */
824 sec->compress_status = saved;
825 *compression_header_size_p = compression_header_size;
826 return compressed;
827 }
828
829 /*
830 FUNCTION
831 bfd_is_section_compressed
832
833 SYNOPSIS
834 bool bfd_is_section_compressed
835 (bfd *abfd, asection *section);
836
837 DESCRIPTION
838 Return @code{TRUE} if @var{section} is compressed.
839 */
840
841 bool
842 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
843 {
844 int compression_header_size;
845 bfd_size_type uncompressed_size;
846 unsigned int uncompressed_align_power;
847 enum compression_type ch_type;
848 return (bfd_is_section_compressed_info (abfd, sec,
849 &compression_header_size,
850 &uncompressed_size,
851 &uncompressed_align_power,
852 &ch_type)
853 && compression_header_size >= 0
854 && uncompressed_size > 0);
855 }
856
857 /*
858 FUNCTION
859 bfd_init_section_decompress_status
860
861 SYNOPSIS
862 bool bfd_init_section_decompress_status
863 (bfd *abfd, asection *section);
864
865 DESCRIPTION
866 Record compressed section size, update section size with
867 decompressed size and set compress_status to
868 DECOMPRESS_SECTION_{ZLIB,ZSTD}.
869
870 Return @code{FALSE} if the section is not a valid compressed
871 section. Otherwise, return @code{TRUE}.
872 */
873
874 bool
875 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
876 {
877 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
878 int compression_header_size;
879 int header_size;
880 bfd_size_type uncompressed_size;
881 unsigned int uncompressed_alignment_power = 0;
882 enum compression_type ch_type;
883 z_stream strm;
884
885 compression_header_size = bfd_get_compression_header_size (abfd, sec);
886 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
887 abort ();
888 header_size = compression_header_size ? compression_header_size : 12;
889
890 /* Read the header. */
891 if (sec->rawsize != 0
892 || sec->contents != NULL
893 || sec->compress_status != COMPRESS_SECTION_NONE
894 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
895 {
896 bfd_set_error (bfd_error_invalid_operation);
897 return false;
898 }
899
900 if (compression_header_size == 0)
901 {
902 /* In this case, it should be "ZLIB" followed by the uncompressed
903 section size, 8 bytes in big-endian order. */
904 if (! startswith ((char*) header, "ZLIB"))
905 {
906 bfd_set_error (bfd_error_wrong_format);
907 return false;
908 }
909 uncompressed_size = bfd_getb64 (header + 4);
910 ch_type = ch_none;
911 }
912 else if (!bfd_check_compression_header (abfd, header, sec,
913 &ch_type,
914 &uncompressed_size,
915 &uncompressed_alignment_power))
916 {
917 bfd_set_error (bfd_error_wrong_format);
918 return false;
919 }
920
921 /* PR28530, reject sizes unsupported by decompress_contents. */
922 strm.avail_in = sec->size;
923 strm.avail_out = uncompressed_size;
924 if (strm.avail_in != sec->size || strm.avail_out != uncompressed_size)
925 {
926 bfd_set_error (bfd_error_nonrepresentable_section);
927 return false;
928 }
929
930 sec->compressed_size = sec->size;
931 sec->size = uncompressed_size;
932 bfd_set_section_alignment (sec, uncompressed_alignment_power);
933 sec->compress_status = (ch_type == ch_compress_zstd
934 ? DECOMPRESS_SECTION_ZSTD : DECOMPRESS_SECTION_ZLIB);
935
936 return true;
937 }
938
939 /*
940 FUNCTION
941 bfd_init_section_compress_status
942
943 SYNOPSIS
944 bool bfd_init_section_compress_status
945 (bfd *abfd, asection *section);
946
947 DESCRIPTION
948 If open for read, compress section, update section size with
949 compressed size and set compress_status to COMPRESS_SECTION_DONE.
950
951 Return @code{FALSE} if the section is not a valid compressed
952 section. Otherwise, return @code{TRUE}.
953 */
954
955 bool
956 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
957 {
958 bfd_size_type uncompressed_size;
959 bfd_byte *uncompressed_buffer;
960
961 /* Error if not opened for read. */
962 if (abfd->direction != read_direction
963 || sec->size == 0
964 || sec->rawsize != 0
965 || sec->contents != NULL
966 || sec->compress_status != COMPRESS_SECTION_NONE)
967 {
968 bfd_set_error (bfd_error_invalid_operation);
969 return false;
970 }
971
972 /* Read in the full section contents and compress it. */
973 uncompressed_size = sec->size;
974 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
975 /* PR 21431 */
976 if (uncompressed_buffer == NULL)
977 return false;
978
979 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
980 0, uncompressed_size))
981 return false;
982
983 sec->contents = uncompressed_buffer;
984 if (bfd_compress_section_contents (abfd, sec) == 0)
985 {
986 free (sec->contents);
987 sec->contents = NULL;
988 return false;
989 }
990 return true;
991 }
992
993 /*
994 FUNCTION
995 bfd_compress_section
996
997 SYNOPSIS
998 bool bfd_compress_section
999 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
1000
1001 DESCRIPTION
1002 If open for write, compress section, update section size with
1003 compressed size and set compress_status to COMPRESS_SECTION_DONE.
1004
1005 Return @code{FALSE} if compression fail. Otherwise, return
1006 @code{TRUE}. UNCOMPRESSED_BUFFER is freed in both cases.
1007 */
1008
1009 bool
1010 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
1011 {
1012 bfd_size_type uncompressed_size = sec->size;
1013
1014 /* Error if not opened for write. */
1015 if (abfd->direction != write_direction
1016 || uncompressed_size == 0
1017 || uncompressed_buffer == NULL
1018 || sec->contents != NULL
1019 || sec->compressed_size != 0
1020 || sec->compress_status != COMPRESS_SECTION_NONE)
1021 {
1022 bfd_set_error (bfd_error_invalid_operation);
1023 return false;
1024 }
1025
1026 sec->contents = uncompressed_buffer;
1027 if (bfd_compress_section_contents (abfd, sec) == 0)
1028 {
1029 free (sec->contents);
1030 sec->contents = NULL;
1031 return false;
1032 }
1033 return true;
1034 }