1 /* Compressed section support (intended for debug sections).
2 Copyright (C) 2008-2019 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
25 #include "safe-ctype.h"
27 #define MAX_COMPRESSION_HEADER_SIZE 24
30 decompress_contents (bfd_byte *compressed_buffer,
31 bfd_size_type compressed_size,
32 bfd_byte *uncompressed_buffer,
33 bfd_size_type uncompressed_size)
38 /* It is possible the section consists of several compressed
39 buffers concatenated together, so we uncompress in a loop. */
40 /* PR 18313: The state field in the z_stream structure is supposed
41 to be invisible to the user (ie us), but some compilers will
42 still complain about it being used without initialisation. So
43 we first zero the entire z_stream structure and then set the fields
45 memset (& strm, 0, sizeof strm);
46 strm.avail_in = compressed_size;
47 strm.next_in = (Bytef*) compressed_buffer;
48 strm.avail_out = uncompressed_size;
50 BFD_ASSERT (Z_OK == 0);
51 rc = inflateInit (&strm);
52 while (strm.avail_in > 0 && strm.avail_out > 0)
56 strm.next_out = ((Bytef*) uncompressed_buffer
57 + (uncompressed_size - strm.avail_out));
58 rc = inflate (&strm, Z_FINISH);
59 if (rc != Z_STREAM_END)
61 rc = inflateReset (&strm);
63 rc |= inflateEnd (&strm);
64 return rc == Z_OK && strm.avail_out == 0;
67 /* Compress data of the size specified in @var{uncompressed_size}
68 and pointed to by @var{uncompressed_buffer} using zlib and store
69 as the contents field. This function assumes the contents
70 field was allocated using bfd_malloc() or equivalent.
72 Return the uncompressed size if the full section contents is
73 compressed successfully. Otherwise return 0. */
76 bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
77 bfd_byte *uncompressed_buffer,
78 bfd_size_type uncompressed_size)
80 uLong compressed_size;
82 bfd_size_type buffer_size;
83 bfd_boolean decompress;
85 int orig_compression_header_size;
86 bfd_size_type orig_uncompressed_size;
87 unsigned int orig_uncompressed_alignment_pow;
88 int header_size = bfd_get_compression_header_size (abfd, NULL);
89 bfd_boolean compressed
90 = bfd_is_section_compressed_with_header (abfd, sec,
91 &orig_compression_header_size,
92 &orig_uncompressed_size,
93 &orig_uncompressed_alignment_pow);
95 /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
96 overhead in .zdebug* section. */
102 /* We shouldn't decompress unsupported compressed section. */
103 if (orig_compression_header_size < 0)
106 /* Different compression schemes. Just move the compressed section
107 contents to the right position. */
108 if (orig_compression_header_size == 0)
110 /* Convert it from .zdebug* section. Get the uncompressed
111 size first. We need to subtract the 12-byte overhead in
112 .zdebug* section. Set orig_compression_header_size to
113 the 12-bye overhead. */
114 orig_compression_header_size = 12;
115 zlib_size = uncompressed_size - 12;
119 /* Convert it to .zdebug* section. */
120 zlib_size = uncompressed_size - orig_compression_header_size;
123 /* Add the header size. */
124 compressed_size = zlib_size + header_size;
127 compressed_size = compressBound (uncompressed_size) + header_size;
129 /* Uncompress if it leads to smaller size. */
130 if (compressed && compressed_size > orig_uncompressed_size)
133 buffer_size = orig_uncompressed_size;
138 buffer_size = compressed_size;
140 buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
146 sec->size = orig_uncompressed_size;
149 if (!decompress_contents (uncompressed_buffer
150 + orig_compression_header_size,
151 zlib_size, buffer, buffer_size))
153 bfd_set_error (bfd_error_bad_value);
154 bfd_release (abfd, buffer);
157 free (uncompressed_buffer);
158 bfd_set_section_alignment (abfd, sec,
159 orig_uncompressed_alignment_pow);
161 sec->contents = buffer;
162 sec->compress_status = COMPRESS_SECTION_DONE;
163 return orig_uncompressed_size;
167 bfd_update_compression_header (abfd, buffer, sec);
168 memmove (buffer + header_size,
169 uncompressed_buffer + orig_compression_header_size,
175 if (compress ((Bytef*) buffer + header_size,
177 (const Bytef*) uncompressed_buffer,
178 uncompressed_size) != Z_OK)
180 bfd_release (abfd, buffer);
181 bfd_set_error (bfd_error_bad_value);
185 compressed_size += header_size;
186 /* PR binutils/18087: If compression didn't make the section smaller,
187 just keep it uncompressed. */
188 if (compressed_size < uncompressed_size)
189 bfd_update_compression_header (abfd, buffer, sec);
192 /* NOTE: There is a small memory leak here since
193 uncompressed_buffer is malloced and won't be freed. */
194 bfd_release (abfd, buffer);
195 sec->contents = uncompressed_buffer;
196 sec->compress_status = COMPRESS_SECTION_NONE;
197 return uncompressed_size;
201 free (uncompressed_buffer);
202 sec->contents = buffer;
203 sec->size = compressed_size;
204 sec->compress_status = COMPRESS_SECTION_DONE;
206 return uncompressed_size;
211 bfd_get_full_section_contents
214 bfd_boolean bfd_get_full_section_contents
215 (bfd *abfd, asection *section, bfd_byte **ptr);
218 Read all data from @var{section} in BFD @var{abfd}, decompress
219 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
220 return @var{*ptr} with memory malloc'd by this function.
222 Return @code{TRUE} if the full section contents is retrieved
223 successfully. If the section has no contents then this function
224 returns @code{TRUE} but @var{*ptr} is set to NULL.
228 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
233 bfd_size_type save_size;
234 bfd_size_type save_rawsize;
235 bfd_byte *compressed_buffer;
236 unsigned int compression_header_size;
238 if (abfd->direction != write_direction && sec->rawsize != 0)
248 switch (sec->compress_status)
250 case COMPRESS_SECTION_NONE:
253 ufile_ptr filesize = bfd_get_file_size (abfd);
256 /* The MMO file format supports its own special compression
257 technique, but it uses COMPRESS_SECTION_NONE when loading
258 a section's contents. */
259 && bfd_get_flavour (abfd) != bfd_target_mmo_flavour)
261 /* PR 24708: Avoid attempts to allocate a ridiculous amount
263 bfd_set_error (bfd_error_no_memory);
265 /* xgettext:c-format */
266 (_("error: %pB(%pA) section size (%#" PRIx64 " bytes) is larger than file size (%#" PRIx64 " bytes)"),
267 abfd, sec, (uint64_t) sz, (uint64_t) filesize);
270 p = (bfd_byte *) bfd_malloc (sz);
273 /* PR 20801: Provide a more helpful error message. */
274 if (bfd_get_error () == bfd_error_no_memory)
276 /* xgettext:c-format */
277 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
278 abfd, sec, (uint64_t) sz);
283 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
292 case DECOMPRESS_SECTION_SIZED:
293 /* Read in the full compressed section contents. */
294 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
295 if (compressed_buffer == NULL)
297 save_rawsize = sec->rawsize;
298 save_size = sec->size;
299 /* Clear rawsize, set size to compressed size and set compress_status
300 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
301 the uncompressed size, bfd_get_section_contents will fail. */
303 sec->size = sec->compressed_size;
304 sec->compress_status = COMPRESS_SECTION_NONE;
305 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
306 0, sec->compressed_size);
307 /* Restore rawsize and size. */
308 sec->rawsize = save_rawsize;
309 sec->size = save_size;
310 sec->compress_status = DECOMPRESS_SECTION_SIZED;
312 goto fail_compressed;
315 p = (bfd_byte *) bfd_malloc (sz);
317 goto fail_compressed;
319 compression_header_size = bfd_get_compression_header_size (abfd, sec);
320 if (compression_header_size == 0)
321 /* Set header size to the zlib header size if it is a
322 SHF_COMPRESSED section. */
323 compression_header_size = 12;
324 if (!decompress_contents (compressed_buffer + compression_header_size,
325 sec->compressed_size - compression_header_size, p, sz))
327 bfd_set_error (bfd_error_bad_value);
331 free (compressed_buffer);
335 free (compressed_buffer);
339 case COMPRESS_SECTION_DONE:
340 if (sec->contents == NULL)
344 p = (bfd_byte *) bfd_malloc (sz);
349 /* PR 17512; file: 5bc29788. */
350 if (p != sec->contents)
351 memcpy (p, sec->contents, sz);
361 bfd_cache_section_contents
364 void bfd_cache_section_contents
365 (asection *sec, void *contents);
368 Stash @var(contents) so any following reads of @var(sec) do
369 not need to decompress again.
373 bfd_cache_section_contents (asection *sec, void *contents)
375 if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
376 sec->compress_status = COMPRESS_SECTION_DONE;
377 sec->contents = contents;
378 sec->flags |= SEC_IN_MEMORY;
383 bfd_is_section_compressed_with_header
386 bfd_boolean bfd_is_section_compressed_with_header
387 (bfd *abfd, asection *section,
388 int *compression_header_size_p,
389 bfd_size_type *uncompressed_size_p,
390 unsigned int *uncompressed_alignment_power_p);
393 Return @code{TRUE} if @var{section} is compressed. Compression
394 header size is returned in @var{compression_header_size_p},
395 uncompressed size is returned in @var{uncompressed_size_p}
396 and the uncompressed data alignement power is returned in
397 @var{uncompressed_align_pow_p}. If compression is
398 unsupported, compression header size is returned with -1
399 and uncompressed size is returned with 0.
403 bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
404 int *compression_header_size_p,
405 bfd_size_type *uncompressed_size_p,
406 unsigned int *uncompressed_align_pow_p)
408 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
409 int compression_header_size;
411 unsigned int saved = sec->compress_status;
412 bfd_boolean compressed;
414 *uncompressed_align_pow_p = 0;
416 compression_header_size = bfd_get_compression_header_size (abfd, sec);
417 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
419 header_size = compression_header_size ? compression_header_size : 12;
421 /* Don't decompress the section. */
422 sec->compress_status = COMPRESS_SECTION_NONE;
424 /* Read the header. */
425 if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
427 if (compression_header_size == 0)
428 /* In this case, it should be "ZLIB" followed by the uncompressed
429 section size, 8 bytes in big-endian order. */
430 compressed = CONST_STRNEQ ((char*) header , "ZLIB");
437 *uncompressed_size_p = sec->size;
440 if (compression_header_size != 0)
442 if (!bfd_check_compression_header (abfd, header, sec,
444 uncompressed_align_pow_p))
445 compression_header_size = -1;
447 /* Check for the pathalogical case of a debug string section that
448 contains the string ZLIB.... as the first entry. We assume that
449 no uncompressed .debug_str section would ever be big enough to
450 have the first byte of its (big-endian) size be non-zero. */
451 else if (strcmp (sec->name, ".debug_str") == 0
452 && ISPRINT (header[4]))
455 *uncompressed_size_p = bfd_getb64 (header + 4);
458 /* Restore compress_status. */
459 sec->compress_status = saved;
460 *compression_header_size_p = compression_header_size;
466 bfd_is_section_compressed
469 bfd_boolean bfd_is_section_compressed
470 (bfd *abfd, asection *section);
473 Return @code{TRUE} if @var{section} is compressed.
477 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
479 int compression_header_size;
480 bfd_size_type uncompressed_size;
481 unsigned int uncompressed_align_power;
482 return (bfd_is_section_compressed_with_header (abfd, sec,
483 &compression_header_size,
485 &uncompressed_align_power)
486 && compression_header_size >= 0
487 && uncompressed_size > 0);
492 bfd_init_section_decompress_status
495 bfd_boolean bfd_init_section_decompress_status
496 (bfd *abfd, asection *section);
499 Record compressed section size, update section size with
500 decompressed size and set compress_status to
501 DECOMPRESS_SECTION_SIZED.
503 Return @code{FALSE} if the section is not a valid compressed
504 section. Otherwise, return @code{TRUE}.
508 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
510 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
511 int compression_header_size;
513 bfd_size_type uncompressed_size;
514 unsigned int uncompressed_alignment_power = 0;
516 compression_header_size = bfd_get_compression_header_size (abfd, sec);
517 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
519 header_size = compression_header_size ? compression_header_size : 12;
521 /* Read the header. */
522 if (sec->rawsize != 0
523 || sec->contents != NULL
524 || sec->compress_status != COMPRESS_SECTION_NONE
525 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
527 bfd_set_error (bfd_error_invalid_operation);
531 if (compression_header_size == 0)
533 /* In this case, it should be "ZLIB" followed by the uncompressed
534 section size, 8 bytes in big-endian order. */
535 if (! CONST_STRNEQ ((char*) header, "ZLIB"))
537 bfd_set_error (bfd_error_wrong_format);
540 uncompressed_size = bfd_getb64 (header + 4);
542 else if (!bfd_check_compression_header (abfd, header, sec,
544 &uncompressed_alignment_power))
546 bfd_set_error (bfd_error_wrong_format);
550 sec->compressed_size = sec->size;
551 sec->size = uncompressed_size;
552 bfd_set_section_alignment (abfd, sec, uncompressed_alignment_power);
553 sec->compress_status = DECOMPRESS_SECTION_SIZED;
560 bfd_init_section_compress_status
563 bfd_boolean bfd_init_section_compress_status
564 (bfd *abfd, asection *section);
567 If open for read, compress section, update section size with
568 compressed size and set compress_status to COMPRESS_SECTION_DONE.
570 Return @code{FALSE} if the section is not a valid compressed
571 section. Otherwise, return @code{TRUE}.
575 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
577 bfd_size_type uncompressed_size;
578 bfd_byte *uncompressed_buffer;
580 /* Error if not opened for read. */
581 if (abfd->direction != read_direction
584 || sec->contents != NULL
585 || sec->compress_status != COMPRESS_SECTION_NONE)
587 bfd_set_error (bfd_error_invalid_operation);
591 /* Read in the full section contents and compress it. */
592 uncompressed_size = sec->size;
593 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
595 if (uncompressed_buffer == NULL)
598 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
599 0, uncompressed_size))
602 uncompressed_size = bfd_compress_section_contents (abfd, sec,
605 return uncompressed_size != 0;
613 bfd_boolean bfd_compress_section
614 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
617 If open for write, compress section, update section size with
618 compressed size and set compress_status to COMPRESS_SECTION_DONE.
620 Return @code{FALSE} if compression fail. Otherwise, return
625 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
627 bfd_size_type uncompressed_size = sec->size;
629 /* Error if not opened for write. */
630 if (abfd->direction != write_direction
631 || uncompressed_size == 0
632 || uncompressed_buffer == NULL
633 || sec->contents != NULL
634 || sec->compressed_size != 0
635 || sec->compress_status != COMPRESS_SECTION_NONE)
637 bfd_set_error (bfd_error_invalid_operation);
642 return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
643 uncompressed_size) != 0;