1 /* Compressed section support (intended for debug sections).
2 Copyright (C) 2008-2015 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. */
43 strm.avail_in = compressed_size - 12;
44 strm.next_in = (Bytef*) compressed_buffer + 12;
45 strm.avail_out = uncompressed_size;
47 BFD_ASSERT (Z_OK == 0);
48 rc = inflateInit (&strm);
49 while (strm.avail_in > 0 && strm.avail_out > 0)
53 strm.next_out = ((Bytef*) uncompressed_buffer
54 + (uncompressed_size - strm.avail_out));
55 rc = inflate (&strm, Z_FINISH);
56 if (rc != Z_STREAM_END)
58 rc = inflateReset (&strm);
60 rc |= inflateEnd (&strm);
61 return rc == Z_OK && strm.avail_out == 0;
64 /* Compress data of the size specified in @var{uncompressed_size}
65 and pointed to by @var{uncompressed_buffer} using zlib and store
66 as the contents field. This function assumes the contents
67 field was allocated using bfd_malloc() or equivalent.
69 Return the uncompressed size if the full section contents is
70 compressed successfully. Otherwise return 0. */
73 bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
74 bfd_byte *uncompressed_buffer,
75 bfd_size_type uncompressed_size)
77 uLong compressed_size;
79 bfd_size_type buffer_size;
80 bfd_boolean decompress;
81 #if defined(__GNUC__) && GCC_VERSION < 4007
82 /* Work around a GCC uninitialized warning bug fixed in GCC 4.7. */
87 int orig_compression_header_size;
88 int compression_header_size
89 = bfd_get_compression_header_size (abfd, NULL);
90 bfd_boolean compressed
91 = bfd_is_section_compressed_with_header (abfd, sec,
92 &orig_compression_header_size);
96 /* We shouldn't decompress unsupported compressed section. */
97 if (orig_compression_header_size < 0)
100 /* Different compression schemes. Just move the compressed section
101 contents to the right position. */
102 if (orig_compression_header_size == 0)
104 /* Convert it from .zdebug* section. Get the uncompressed
106 zlib_size = uncompressed_size;
107 compressed_size = zlib_size + compression_header_size;
108 uncompressed_size = bfd_getb64 (uncompressed_buffer + 4);
112 /* Convert it to .zdebug* section. */
113 zlib_size = uncompressed_size - orig_compression_header_size;
114 compressed_size = zlib_size;
118 compressed_size = compressBound (uncompressed_size) + 12;
120 /* When converting from .zdebug* section, uncompress if it leads to
123 && orig_compression_header_size == 0
124 && compressed_size > uncompressed_size)
127 buffer_size = uncompressed_size;
132 buffer_size = compressed_size + compression_header_size;
134 buffer = (bfd_byte *) bfd_malloc (buffer_size);
140 sec->size = uncompressed_size;
143 if (!decompress_contents (uncompressed_buffer, zlib_size,
144 buffer, uncompressed_size))
146 bfd_set_error (bfd_error_bad_value);
150 free (uncompressed_buffer);
151 sec->contents = buffer;
152 sec->compress_status = COMPRESS_SECTION_DONE;
153 return uncompressed_size;
157 bfd_update_compression_header (abfd, buffer, sec);
158 memmove (buffer + compression_header_size,
159 uncompressed_buffer + orig_compression_header_size,
165 bfd_size_type size = uncompressed_size;
166 int header_size = 12 + compression_header_size;
167 if (compress ((Bytef*) buffer + header_size,
169 (const Bytef*) uncompressed_buffer,
170 uncompressed_size) != Z_OK)
173 bfd_set_error (bfd_error_bad_value);
177 compressed_size += header_size;
178 /* PR binutils/18087: If compression didn't make the section smaller,
179 just keep it uncompressed. */
180 if (compressed_size < uncompressed_size)
182 bfd_update_compression_header (abfd, buffer, sec);
184 /* Write the zlib header. In this case, it should be "ZLIB"
185 followed by the uncompressed section size, 8 bytes in
187 memcpy (buffer + compression_header_size, "ZLIB", 4);
188 bfd_putb64 (size, buffer + compression_header_size + 4);
192 sec->contents = uncompressed_buffer;
193 sec->compress_status = COMPRESS_SECTION_NONE;
194 return uncompressed_size;
198 free (uncompressed_buffer);
199 sec->contents = buffer;
200 sec->size = compressed_size;
201 sec->compress_status = COMPRESS_SECTION_DONE;
203 return uncompressed_size;
208 bfd_get_full_section_contents
211 bfd_boolean bfd_get_full_section_contents
212 (bfd *abfd, asection *section, bfd_byte **ptr);
215 Read all data from @var{section} in BFD @var{abfd}, decompress
216 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
217 return @var{*ptr} with memory malloc'd by this function.
219 Return @code{TRUE} if the full section contents is retrieved
220 successfully. If the section has no contents then this function
221 returns @code{TRUE} but @var{*ptr} is set to NULL.
225 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
230 bfd_size_type save_size;
231 bfd_size_type save_rawsize;
232 bfd_byte *compressed_buffer;
233 unsigned int compression_header_size;
235 if (abfd->direction != write_direction && sec->rawsize != 0)
245 switch (sec->compress_status)
247 case COMPRESS_SECTION_NONE:
250 p = (bfd_byte *) bfd_malloc (sz);
255 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
264 case DECOMPRESS_SECTION_SIZED:
265 /* Read in the full compressed section contents. */
266 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
267 if (compressed_buffer == NULL)
269 save_rawsize = sec->rawsize;
270 save_size = sec->size;
271 /* Clear rawsize, set size to compressed size and set compress_status
272 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
273 the uncompressed size, bfd_get_section_contents will fail. */
275 sec->size = sec->compressed_size;
276 sec->compress_status = COMPRESS_SECTION_NONE;
277 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
278 0, sec->compressed_size);
279 /* Restore rawsize and size. */
280 sec->rawsize = save_rawsize;
281 sec->size = save_size;
282 sec->compress_status = DECOMPRESS_SECTION_SIZED;
284 goto fail_compressed;
287 p = (bfd_byte *) bfd_malloc (sz);
289 goto fail_compressed;
291 compression_header_size = bfd_get_compression_header_size (abfd, sec);
292 if (!decompress_contents (compressed_buffer + compression_header_size,
293 sec->compressed_size, p, sz))
295 bfd_set_error (bfd_error_bad_value);
299 free (compressed_buffer);
303 free (compressed_buffer);
307 case COMPRESS_SECTION_DONE:
308 if (sec->contents == NULL)
312 p = (bfd_byte *) bfd_malloc (sz);
317 /* PR 17512; file: 5bc29788. */
318 if (p != sec->contents)
319 memcpy (p, sec->contents, sz);
329 bfd_cache_section_contents
332 void bfd_cache_section_contents
333 (asection *sec, void *contents);
336 Stash @var(contents) so any following reads of @var(sec) do
337 not need to decompress again.
341 bfd_cache_section_contents (asection *sec, void *contents)
343 if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
344 sec->compress_status = COMPRESS_SECTION_DONE;
345 sec->contents = contents;
346 sec->flags |= SEC_IN_MEMORY;
351 bfd_is_section_compressed_with_header
354 bfd_boolean bfd_is_section_compressed_with_header
355 (bfd *abfd, asection *section,
356 int *compression_header_size_p);
359 Return @code{TRUE} if @var{section} is compressed. Compression
360 header size is returned in @var{compression_header_size_p}. If
361 compression is unsupported, compression header size is returned
366 bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
367 int *compression_header_size_p)
369 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
370 int compression_header_size;
371 int header_size = 12;
372 unsigned int saved = sec->compress_status;
373 bfd_boolean compressed;
375 compression_header_size = bfd_get_compression_header_size (abfd, sec);
376 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
378 header_size += compression_header_size;
380 /* Don't decompress the section. */
381 sec->compress_status = COMPRESS_SECTION_NONE;
383 /* Read the zlib header. In this case, it should be "ZLIB" followed
384 by the uncompressed section size, 8 bytes in big-endian order. */
385 compressed = bfd_get_section_contents (abfd, sec, header, 0,
387 && CONST_STRNEQ ((char*) header + compression_header_size,
392 if (compression_header_size != 0)
394 bfd_size_type uncompressed_size
395 = bfd_getb64 ((bfd_byte *) header
396 + compression_header_size + 4);
397 if (!bfd_check_compression_header (abfd, header, sec,
399 compression_header_size = -1;
401 /* Check for the pathalogical case of a debug string section that
402 contains the string ZLIB.... as the first entry. We assume that
403 no uncompressed .debug_str section would ever be big enough to
404 have the first byte of its (big-endian) size be non-zero. */
405 else if (strcmp (sec->name, ".debug_str") == 0
406 && ISPRINT (header[compression_header_size + 4]))
410 /* Restore compress_status. */
411 sec->compress_status = saved;
412 *compression_header_size_p = compression_header_size;
418 bfd_is_section_compressed
421 bfd_boolean bfd_is_section_compressed
422 (bfd *abfd, asection *section);
425 Return @code{TRUE} if @var{section} is compressed.
429 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
431 int compression_header_size;
432 return (bfd_is_section_compressed_with_header (abfd, sec,
433 &compression_header_size)
434 && compression_header_size >= 0);
439 bfd_init_section_decompress_status
442 bfd_boolean bfd_init_section_decompress_status
443 (bfd *abfd, asection *section);
446 Record compressed section size, update section size with
447 decompressed size and set compress_status to
448 DECOMPRESS_SECTION_SIZED.
450 Return @code{FALSE} if the section is not a valid compressed
451 section. Otherwise, return @code{TRUE}.
455 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
457 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
458 int compression_header_size;
459 int header_size = 12;
460 bfd_size_type uncompressed_size;
462 compression_header_size = bfd_get_compression_header_size (abfd, sec);
463 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
465 header_size += compression_header_size;
467 if (sec->rawsize != 0
468 || sec->contents != NULL
469 || sec->compress_status != COMPRESS_SECTION_NONE
470 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
472 bfd_set_error (bfd_error_invalid_operation);
476 /* Read the zlib header. In this case, it should be "ZLIB" followed
477 by the uncompressed section size, 8 bytes in big-endian order. */
478 if (! CONST_STRNEQ ((char*) header + compression_header_size, "ZLIB"))
480 bfd_set_error (bfd_error_wrong_format);
484 uncompressed_size = bfd_getb64 (header + compression_header_size + 4);
485 if (compression_header_size != 0
486 && !bfd_check_compression_header (abfd, header, sec,
489 bfd_set_error (bfd_error_wrong_format);
492 sec->compressed_size = sec->size;
493 sec->size = uncompressed_size;
494 sec->compress_status = DECOMPRESS_SECTION_SIZED;
501 bfd_init_section_compress_status
504 bfd_boolean bfd_init_section_compress_status
505 (bfd *abfd, asection *section);
508 If open for read, compress section, update section size with
509 compressed size and set compress_status to COMPRESS_SECTION_DONE.
511 Return @code{FALSE} if the section is not a valid compressed
512 section. Otherwise, return @code{TRUE}.
516 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
518 bfd_size_type uncompressed_size;
519 bfd_byte *uncompressed_buffer;
522 /* Error if not opened for read. */
523 if (abfd->direction != read_direction
526 || sec->contents != NULL
527 || sec->compress_status != COMPRESS_SECTION_NONE)
529 bfd_set_error (bfd_error_invalid_operation);
533 /* Read in the full section contents and compress it. */
534 uncompressed_size = sec->size;
535 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
536 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
537 0, uncompressed_size))
541 uncompressed_size = bfd_compress_section_contents (abfd, sec,
544 ret = uncompressed_size != 0;