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,
76 bfd_boolean write_compress)
78 uLong compressed_size;
80 bfd_size_type buffer_size;
81 bfd_boolean decompress;
82 #if defined(__GNUC__) && GCC_VERSION < 4007
83 /* Work around a GCC uninitialized warning bug fixed in GCC 4.7. */
88 int orig_compression_header_size;
89 int compression_header_size
90 = bfd_get_compression_header_size (abfd, NULL);
91 bfd_boolean compressed
92 = bfd_is_section_compressed_with_header (abfd, sec,
93 &orig_compression_header_size);
97 /* We shouldn't decompress unsupported compressed section. */
98 if (orig_compression_header_size < 0)
101 /* Different compression schemes. Just move the compressed section
102 contents to the right position. */
103 if (orig_compression_header_size == 0)
105 /* Convert it from .zdebug* section. Get the uncompressed
107 zlib_size = uncompressed_size;
108 compressed_size = zlib_size + compression_header_size;
109 uncompressed_size = bfd_getb64 (uncompressed_buffer + 4);
113 /* Convert it to .zdebug* section. */
114 zlib_size = uncompressed_size - orig_compression_header_size;
115 compressed_size = zlib_size;
119 compressed_size = compressBound (uncompressed_size) + 12;
121 /* When converting from .zdebug* section, uncompress if it leads to
124 && orig_compression_header_size == 0
125 && compressed_size > uncompressed_size)
128 buffer_size = uncompressed_size;
133 buffer_size = compressed_size + compression_header_size;
135 buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
141 sec->size = uncompressed_size;
144 if (!decompress_contents (uncompressed_buffer, zlib_size,
145 buffer, uncompressed_size))
147 bfd_set_error (bfd_error_bad_value);
148 bfd_release (abfd, buffer);
151 free (uncompressed_buffer);
152 sec->contents = buffer;
153 sec->compress_status = COMPRESS_SECTION_DONE;
154 return uncompressed_size;
158 bfd_update_compression_header (abfd, buffer, sec);
159 memmove (buffer + compression_header_size,
160 uncompressed_buffer + orig_compression_header_size,
166 bfd_size_type size = uncompressed_size;
167 int header_size = 12 + compression_header_size;
168 if (compress ((Bytef*) buffer + header_size,
170 (const Bytef*) uncompressed_buffer,
171 uncompressed_size) != Z_OK)
173 bfd_release (abfd, buffer);
174 bfd_set_error (bfd_error_bad_value);
178 compressed_size += header_size;
179 /* PR binutils/18087: If compression didn't make the section smaller,
180 just keep it uncompressed. We always generate .zdebug_* section
181 when linking since section names have been finalized and they
182 can't be changed easily. */
183 if ((write_compress && compression_header_size == 0)
184 || compressed_size < uncompressed_size)
186 bfd_update_compression_header (abfd, buffer, sec);
188 /* Write the zlib header. In this case, it should be "ZLIB"
189 followed by the uncompressed section size, 8 bytes in
191 memcpy (buffer + compression_header_size, "ZLIB", 4);
192 bfd_putb64 (size, buffer + compression_header_size + 4);
196 /* NOTE: There is a small memory leak here since
197 uncompressed_buffer is malloced and won't be freed. */
198 bfd_release (abfd, buffer);
199 sec->contents = uncompressed_buffer;
200 sec->compress_status = COMPRESS_SECTION_NONE;
201 return uncompressed_size;
205 free (uncompressed_buffer);
206 sec->contents = buffer;
207 sec->size = compressed_size;
208 sec->compress_status = COMPRESS_SECTION_DONE;
210 return uncompressed_size;
215 bfd_get_full_section_contents
218 bfd_boolean bfd_get_full_section_contents
219 (bfd *abfd, asection *section, bfd_byte **ptr);
222 Read all data from @var{section} in BFD @var{abfd}, decompress
223 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
224 return @var{*ptr} with memory malloc'd by this function.
226 Return @code{TRUE} if the full section contents is retrieved
227 successfully. If the section has no contents then this function
228 returns @code{TRUE} but @var{*ptr} is set to NULL.
232 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
237 bfd_size_type save_size;
238 bfd_size_type save_rawsize;
239 bfd_byte *compressed_buffer;
240 unsigned int compression_header_size;
242 if (abfd->direction != write_direction && sec->rawsize != 0)
252 switch (sec->compress_status)
254 case COMPRESS_SECTION_NONE:
257 p = (bfd_byte *) bfd_malloc (sz);
262 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
271 case DECOMPRESS_SECTION_SIZED:
272 /* Read in the full compressed section contents. */
273 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
274 if (compressed_buffer == NULL)
276 save_rawsize = sec->rawsize;
277 save_size = sec->size;
278 /* Clear rawsize, set size to compressed size and set compress_status
279 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
280 the uncompressed size, bfd_get_section_contents will fail. */
282 sec->size = sec->compressed_size;
283 sec->compress_status = COMPRESS_SECTION_NONE;
284 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
285 0, sec->compressed_size);
286 /* Restore rawsize and size. */
287 sec->rawsize = save_rawsize;
288 sec->size = save_size;
289 sec->compress_status = DECOMPRESS_SECTION_SIZED;
291 goto fail_compressed;
294 p = (bfd_byte *) bfd_malloc (sz);
296 goto fail_compressed;
298 compression_header_size = bfd_get_compression_header_size (abfd, sec);
299 if (!decompress_contents (compressed_buffer + compression_header_size,
300 sec->compressed_size, p, sz))
302 bfd_set_error (bfd_error_bad_value);
306 free (compressed_buffer);
310 free (compressed_buffer);
314 case COMPRESS_SECTION_DONE:
315 if (sec->contents == NULL)
319 p = (bfd_byte *) bfd_malloc (sz);
324 /* PR 17512; file: 5bc29788. */
325 if (p != sec->contents)
326 memcpy (p, sec->contents, sz);
336 bfd_cache_section_contents
339 void bfd_cache_section_contents
340 (asection *sec, void *contents);
343 Stash @var(contents) so any following reads of @var(sec) do
344 not need to decompress again.
348 bfd_cache_section_contents (asection *sec, void *contents)
350 if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
351 sec->compress_status = COMPRESS_SECTION_DONE;
352 sec->contents = contents;
353 sec->flags |= SEC_IN_MEMORY;
358 bfd_is_section_compressed_with_header
361 bfd_boolean bfd_is_section_compressed_with_header
362 (bfd *abfd, asection *section,
363 int *compression_header_size_p);
366 Return @code{TRUE} if @var{section} is compressed. Compression
367 header size is returned in @var{compression_header_size_p}. If
368 compression is unsupported, compression header size is returned
373 bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
374 int *compression_header_size_p)
376 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
377 int compression_header_size;
378 int header_size = 12;
379 unsigned int saved = sec->compress_status;
380 bfd_boolean compressed;
382 compression_header_size = bfd_get_compression_header_size (abfd, sec);
383 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
385 header_size += compression_header_size;
387 /* Don't decompress the section. */
388 sec->compress_status = COMPRESS_SECTION_NONE;
390 /* Read the zlib header. In this case, it should be "ZLIB" followed
391 by the uncompressed section size, 8 bytes in big-endian order. */
392 compressed = bfd_get_section_contents (abfd, sec, header, 0,
394 && CONST_STRNEQ ((char*) header + compression_header_size,
399 if (compression_header_size != 0)
401 bfd_size_type uncompressed_size
402 = bfd_getb64 ((bfd_byte *) header
403 + compression_header_size + 4);
404 if (!bfd_check_compression_header (abfd, header, sec,
406 compression_header_size = -1;
408 /* Check for the pathalogical case of a debug string section that
409 contains the string ZLIB.... as the first entry. We assume that
410 no uncompressed .debug_str section would ever be big enough to
411 have the first byte of its (big-endian) size be non-zero. */
412 else if (strcmp (sec->name, ".debug_str") == 0
413 && ISPRINT (header[compression_header_size + 4]))
417 /* Restore compress_status. */
418 sec->compress_status = saved;
419 *compression_header_size_p = compression_header_size;
425 bfd_is_section_compressed
428 bfd_boolean bfd_is_section_compressed
429 (bfd *abfd, asection *section);
432 Return @code{TRUE} if @var{section} is compressed.
436 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
438 int compression_header_size;
439 return (bfd_is_section_compressed_with_header (abfd, sec,
440 &compression_header_size)
441 && compression_header_size >= 0);
446 bfd_init_section_decompress_status
449 bfd_boolean bfd_init_section_decompress_status
450 (bfd *abfd, asection *section);
453 Record compressed section size, update section size with
454 decompressed size and set compress_status to
455 DECOMPRESS_SECTION_SIZED.
457 Return @code{FALSE} if the section is not a valid compressed
458 section. Otherwise, return @code{TRUE}.
462 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
464 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
465 int compression_header_size;
466 int header_size = 12;
467 bfd_size_type uncompressed_size;
469 compression_header_size = bfd_get_compression_header_size (abfd, sec);
470 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
472 header_size += compression_header_size;
474 if (sec->rawsize != 0
475 || sec->contents != NULL
476 || sec->compress_status != COMPRESS_SECTION_NONE
477 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
479 bfd_set_error (bfd_error_invalid_operation);
483 /* Read the zlib header. In this case, it should be "ZLIB" followed
484 by the uncompressed section size, 8 bytes in big-endian order. */
485 if (! CONST_STRNEQ ((char*) header + compression_header_size, "ZLIB"))
487 bfd_set_error (bfd_error_wrong_format);
491 uncompressed_size = bfd_getb64 (header + compression_header_size + 4);
492 if (compression_header_size != 0
493 && !bfd_check_compression_header (abfd, header, sec,
496 bfd_set_error (bfd_error_wrong_format);
499 sec->compressed_size = sec->size;
500 sec->size = uncompressed_size;
501 sec->compress_status = DECOMPRESS_SECTION_SIZED;
508 bfd_init_section_compress_status
511 bfd_boolean bfd_init_section_compress_status
512 (bfd *abfd, asection *section);
515 If open for read, compress section, update section size with
516 compressed size and set compress_status to COMPRESS_SECTION_DONE.
518 Return @code{FALSE} if the section is not a valid compressed
519 section. Otherwise, return @code{TRUE}.
523 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
525 bfd_size_type uncompressed_size;
526 bfd_byte *uncompressed_buffer;
529 /* Error if not opened for read. */
530 if (abfd->direction != read_direction
533 || sec->contents != NULL
534 || sec->compress_status != COMPRESS_SECTION_NONE)
536 bfd_set_error (bfd_error_invalid_operation);
540 /* Read in the full section contents and compress it. */
541 uncompressed_size = sec->size;
542 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
543 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
544 0, uncompressed_size))
548 uncompressed_size = bfd_compress_section_contents (abfd, sec,
552 ret = uncompressed_size != 0;
563 bfd_boolean bfd_compress_section
564 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
567 If open for write, compress section, update section size with
568 compressed size and set compress_status to COMPRESS_SECTION_DONE.
570 Return @code{FALSE} if compression fail. Otherwise, return
575 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
577 bfd_size_type uncompressed_size = sec->size;
579 /* Error if not opened for write. */
580 if (abfd->direction != write_direction
581 || uncompressed_size == 0
582 || uncompressed_buffer == NULL
583 || sec->contents != NULL
584 || sec->compressed_size != 0
585 || sec->compress_status != COMPRESS_SECTION_NONE)
587 bfd_set_error (bfd_error_invalid_operation);
592 return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
593 uncompressed_size, TRUE) != 0;