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