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. If zlib
68 is not installed on this machine, the input is unmodified.
70 Return the uncompressed size if the full section contents is
71 compressed successfully. Otherwise return 0. */
74 bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
75 bfd_byte *uncompressed_buffer,
76 bfd_size_type uncompressed_size)
78 uLong compressed_size;
80 bfd_size_type buffer_size;
81 bfd_boolean decompress;
83 int orig_compression_header_size;
84 int compression_header_size
85 = bfd_get_compression_header_size (abfd, NULL);
86 bfd_boolean compressed
87 = bfd_is_section_compressed_with_header (abfd, sec,
88 &orig_compression_header_size);
92 /* We shouldn't decompress unsupported compressed section. */
93 if (orig_compression_header_size < 0)
96 /* Different compression schemes. Just move the compressed section
97 contents to the right position. */
98 if (orig_compression_header_size == 0)
100 /* Convert it from .zdebug* section. Get the uncompressed
102 zlib_size = uncompressed_size;
103 compressed_size = zlib_size + compression_header_size;
104 uncompressed_size = bfd_getb64 (uncompressed_buffer + 4);
108 /* Convert it to .zdebug* section. */
109 zlib_size = uncompressed_size - orig_compression_header_size;
110 compressed_size = zlib_size;
114 compressed_size = compressBound (uncompressed_size) + 12;
116 /* When converting from .zdebug* section, uncompress if it leads to
119 && orig_compression_header_size == 0
120 && compressed_size > uncompressed_size)
123 buffer_size = uncompressed_size;
128 buffer_size = compressed_size + compression_header_size;
130 buffer = (bfd_byte *) bfd_malloc (buffer_size);
136 sec->size = uncompressed_size;
139 if (!decompress_contents (uncompressed_buffer, zlib_size,
140 buffer, uncompressed_size))
142 bfd_set_error (bfd_error_bad_value);
146 free (uncompressed_buffer);
147 sec->contents = buffer;
148 sec->compress_status = COMPRESS_SECTION_DONE;
149 return uncompressed_size;
153 bfd_update_compression_header (abfd, buffer, sec);
154 memmove (buffer + compression_header_size,
155 uncompressed_buffer + orig_compression_header_size,
161 bfd_size_type size = uncompressed_size;
162 int header_size = 12 + compression_header_size;
163 if (compress ((Bytef*) buffer + header_size,
165 (const Bytef*) uncompressed_buffer,
166 uncompressed_size) != Z_OK)
169 bfd_set_error (bfd_error_bad_value);
173 compressed_size += header_size;
174 /* PR binutils/18087: If compression didn't make the section smaller,
175 just keep it uncompressed. */
176 if (compressed_size < uncompressed_size)
178 bfd_update_compression_header (abfd, buffer, sec);
180 /* Write the zlib header. In this case, it should be "ZLIB"
181 followed by the uncompressed section size, 8 bytes in
183 memcpy (buffer + compression_header_size, "ZLIB", 4);
184 bfd_putb64 (size, buffer + compression_header_size + 4);
188 sec->contents = uncompressed_buffer;
189 sec->compress_status = COMPRESS_SECTION_NONE;
190 return uncompressed_size;
194 free (uncompressed_buffer);
195 sec->contents = buffer;
196 sec->size = compressed_size;
197 sec->compress_status = COMPRESS_SECTION_DONE;
199 return uncompressed_size;
204 bfd_get_full_section_contents
207 bfd_boolean bfd_get_full_section_contents
208 (bfd *abfd, asection *section, bfd_byte **ptr);
211 Read all data from @var{section} in BFD @var{abfd}, decompress
212 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
213 return @var{*ptr} with memory malloc'd by this function.
215 Return @code{TRUE} if the full section contents is retrieved
216 successfully. If the section has no contents then this function
217 returns @code{TRUE} but @var{*ptr} is set to NULL.
221 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
226 bfd_size_type save_size;
227 bfd_size_type save_rawsize;
228 bfd_byte *compressed_buffer;
229 unsigned int compression_header_size;
231 if (abfd->direction != write_direction && sec->rawsize != 0)
241 switch (sec->compress_status)
243 case COMPRESS_SECTION_NONE:
246 p = (bfd_byte *) bfd_malloc (sz);
251 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
260 case DECOMPRESS_SECTION_SIZED:
261 /* Read in the full compressed section contents. */
262 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
263 if (compressed_buffer == NULL)
265 save_rawsize = sec->rawsize;
266 save_size = sec->size;
267 /* Clear rawsize, set size to compressed size and set compress_status
268 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
269 the uncompressed size, bfd_get_section_contents will fail. */
271 sec->size = sec->compressed_size;
272 sec->compress_status = COMPRESS_SECTION_NONE;
273 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
274 0, sec->compressed_size);
275 /* Restore rawsize and size. */
276 sec->rawsize = save_rawsize;
277 sec->size = save_size;
278 sec->compress_status = DECOMPRESS_SECTION_SIZED;
280 goto fail_compressed;
283 p = (bfd_byte *) bfd_malloc (sz);
285 goto fail_compressed;
287 compression_header_size = bfd_get_compression_header_size (abfd, sec);
288 if (!decompress_contents (compressed_buffer + compression_header_size,
289 sec->compressed_size, p, sz))
291 bfd_set_error (bfd_error_bad_value);
295 free (compressed_buffer);
299 free (compressed_buffer);
303 case COMPRESS_SECTION_DONE:
304 if (sec->contents == NULL)
308 p = (bfd_byte *) bfd_malloc (sz);
313 /* PR 17512; file: 5bc29788. */
314 if (p != sec->contents)
315 memcpy (p, sec->contents, sz);
325 bfd_cache_section_contents
328 void bfd_cache_section_contents
329 (asection *sec, void *contents);
332 Stash @var(contents) so any following reads of @var(sec) do
333 not need to decompress again.
337 bfd_cache_section_contents (asection *sec, void *contents)
339 if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
340 sec->compress_status = COMPRESS_SECTION_DONE;
341 sec->contents = contents;
342 sec->flags |= SEC_IN_MEMORY;
347 bfd_is_section_compressed_with_header
350 bfd_boolean bfd_is_section_compressed_with_header
351 (bfd *abfd, asection *section,
352 int *compression_header_size_p);
355 Return @code{TRUE} if @var{section} is compressed. Compression
356 header size is returned in @var{compression_header_size_p}. If
357 compression is unsupported, compression header size is returned
362 bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
363 int *compression_header_size_p)
365 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
366 int compression_header_size;
367 int header_size = 12;
368 unsigned int saved = sec->compress_status;
369 bfd_boolean compressed;
371 compression_header_size = bfd_get_compression_header_size (abfd, sec);
372 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
374 header_size += compression_header_size;
376 /* Don't decompress the section. */
377 sec->compress_status = COMPRESS_SECTION_NONE;
379 /* Read the zlib header. In this case, it should be "ZLIB" followed
380 by the uncompressed section size, 8 bytes in big-endian order. */
381 compressed = bfd_get_section_contents (abfd, sec, header, 0,
383 && CONST_STRNEQ ((char*) header + compression_header_size,
388 if (compression_header_size != 0)
390 bfd_size_type uncompressed_size
391 = bfd_getb64 ((bfd_byte *) header
392 + compression_header_size + 4);
393 if (!bfd_check_compression_header (abfd, header, sec,
395 compression_header_size = -1;
397 /* Check for the pathalogical case of a debug string section that
398 contains the string ZLIB.... as the first entry. We assume that
399 no uncompressed .debug_str section would ever be big enough to
400 have the first byte of its (big-endian) size be non-zero. */
401 else if (strcmp (sec->name, ".debug_str") == 0
402 && ISPRINT (header[compression_header_size + 4]))
406 /* Restore compress_status. */
407 sec->compress_status = saved;
408 *compression_header_size_p = compression_header_size;
414 bfd_is_section_compressed
417 bfd_boolean bfd_is_section_compressed
418 (bfd *abfd, asection *section);
421 Return @code{TRUE} if @var{section} is compressed.
425 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
427 int compression_header_size;
428 return (bfd_is_section_compressed_with_header (abfd, sec,
429 &compression_header_size)
430 && compression_header_size >= 0);
435 bfd_init_section_decompress_status
438 bfd_boolean bfd_init_section_decompress_status
439 (bfd *abfd, asection *section);
442 Record compressed section size, update section size with
443 decompressed size and set compress_status to
444 DECOMPRESS_SECTION_SIZED.
446 Return @code{FALSE} if the section is not a valid compressed
447 section or zlib is not installed on this machine. Otherwise,
452 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
454 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
455 int compression_header_size;
456 int header_size = 12;
457 bfd_size_type uncompressed_size;
459 compression_header_size = bfd_get_compression_header_size (abfd, sec);
460 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
462 header_size += compression_header_size;
464 if (sec->rawsize != 0
465 || sec->contents != NULL
466 || sec->compress_status != COMPRESS_SECTION_NONE
467 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
469 bfd_set_error (bfd_error_invalid_operation);
473 /* Read the zlib header. In this case, it should be "ZLIB" followed
474 by the uncompressed section size, 8 bytes in big-endian order. */
475 if (! CONST_STRNEQ ((char*) header + compression_header_size, "ZLIB"))
477 bfd_set_error (bfd_error_wrong_format);
481 uncompressed_size = bfd_getb64 (header + compression_header_size + 4);
482 if (compression_header_size != 0
483 && !bfd_check_compression_header (abfd, header, sec,
486 bfd_set_error (bfd_error_wrong_format);
489 sec->compressed_size = sec->size;
490 sec->size = uncompressed_size;
491 sec->compress_status = DECOMPRESS_SECTION_SIZED;
498 bfd_init_section_compress_status
501 bfd_boolean bfd_init_section_compress_status
502 (bfd *abfd, asection *section);
505 If open for read, compress section, update section size with
506 compressed size and set compress_status to COMPRESS_SECTION_DONE.
508 Return @code{FALSE} if the section is not a valid compressed
509 section or zlib is not installed on this machine. Otherwise,
514 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
516 bfd_size_type uncompressed_size;
517 bfd_byte *uncompressed_buffer;
520 /* Error if not opened for read. */
521 if (abfd->direction != read_direction
524 || sec->contents != NULL
525 || sec->compress_status != COMPRESS_SECTION_NONE)
527 bfd_set_error (bfd_error_invalid_operation);
531 /* Read in the full section contents and compress it. */
532 uncompressed_size = sec->size;
533 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
534 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
535 0, uncompressed_size))
539 uncompressed_size = bfd_compress_section_contents (abfd, sec,
542 ret = uncompressed_size != 0;