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_alloc (abfd
, 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
);
147 bfd_release (abfd
, buffer
);
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
)
172 bfd_release (abfd
, buffer
);
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 /* 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 p
= (bfd_byte
*) bfd_malloc (sz
);
258 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
267 case DECOMPRESS_SECTION_SIZED
:
268 /* Read in the full compressed section contents. */
269 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
270 if (compressed_buffer
== NULL
)
272 save_rawsize
= sec
->rawsize
;
273 save_size
= sec
->size
;
274 /* Clear rawsize, set size to compressed size and set compress_status
275 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
276 the uncompressed size, bfd_get_section_contents will fail. */
278 sec
->size
= sec
->compressed_size
;
279 sec
->compress_status
= COMPRESS_SECTION_NONE
;
280 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
281 0, sec
->compressed_size
);
282 /* Restore rawsize and size. */
283 sec
->rawsize
= save_rawsize
;
284 sec
->size
= save_size
;
285 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
287 goto fail_compressed
;
290 p
= (bfd_byte
*) bfd_malloc (sz
);
292 goto fail_compressed
;
294 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
295 if (!decompress_contents (compressed_buffer
+ compression_header_size
,
296 sec
->compressed_size
, p
, sz
))
298 bfd_set_error (bfd_error_bad_value
);
302 free (compressed_buffer
);
306 free (compressed_buffer
);
310 case COMPRESS_SECTION_DONE
:
311 if (sec
->contents
== NULL
)
315 p
= (bfd_byte
*) bfd_malloc (sz
);
320 /* PR 17512; file: 5bc29788. */
321 if (p
!= sec
->contents
)
322 memcpy (p
, sec
->contents
, sz
);
332 bfd_cache_section_contents
335 void bfd_cache_section_contents
336 (asection *sec, void *contents);
339 Stash @var(contents) so any following reads of @var(sec) do
340 not need to decompress again.
344 bfd_cache_section_contents (asection
*sec
, void *contents
)
346 if (sec
->compress_status
== DECOMPRESS_SECTION_SIZED
)
347 sec
->compress_status
= COMPRESS_SECTION_DONE
;
348 sec
->contents
= contents
;
349 sec
->flags
|= SEC_IN_MEMORY
;
354 bfd_is_section_compressed_with_header
357 bfd_boolean bfd_is_section_compressed_with_header
358 (bfd *abfd, asection *section,
359 int *compression_header_size_p);
362 Return @code{TRUE} if @var{section} is compressed. Compression
363 header size is returned in @var{compression_header_size_p}. If
364 compression is unsupported, compression header size is returned
369 bfd_is_section_compressed_with_header (bfd
*abfd
, sec_ptr sec
,
370 int *compression_header_size_p
)
372 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
+ 12];
373 int compression_header_size
;
374 int header_size
= 12;
375 unsigned int saved
= sec
->compress_status
;
376 bfd_boolean compressed
;
378 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
379 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
381 header_size
+= compression_header_size
;
383 /* Don't decompress the section. */
384 sec
->compress_status
= COMPRESS_SECTION_NONE
;
386 /* Read the zlib header. In this case, it should be "ZLIB" followed
387 by the uncompressed section size, 8 bytes in big-endian order. */
388 compressed
= bfd_get_section_contents (abfd
, sec
, header
, 0,
390 && CONST_STRNEQ ((char*) header
+ compression_header_size
,
395 if (compression_header_size
!= 0)
397 bfd_size_type uncompressed_size
398 = bfd_getb64 ((bfd_byte
*) header
399 + compression_header_size
+ 4);
400 if (!bfd_check_compression_header (abfd
, header
, sec
,
402 compression_header_size
= -1;
404 /* Check for the pathalogical case of a debug string section that
405 contains the string ZLIB.... as the first entry. We assume that
406 no uncompressed .debug_str section would ever be big enough to
407 have the first byte of its (big-endian) size be non-zero. */
408 else if (strcmp (sec
->name
, ".debug_str") == 0
409 && ISPRINT (header
[compression_header_size
+ 4]))
413 /* Restore compress_status. */
414 sec
->compress_status
= saved
;
415 *compression_header_size_p
= compression_header_size
;
421 bfd_is_section_compressed
424 bfd_boolean bfd_is_section_compressed
425 (bfd *abfd, asection *section);
428 Return @code{TRUE} if @var{section} is compressed.
432 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
434 int compression_header_size
;
435 return (bfd_is_section_compressed_with_header (abfd
, sec
,
436 &compression_header_size
)
437 && compression_header_size
>= 0);
442 bfd_init_section_decompress_status
445 bfd_boolean bfd_init_section_decompress_status
446 (bfd *abfd, asection *section);
449 Record compressed section size, update section size with
450 decompressed size and set compress_status to
451 DECOMPRESS_SECTION_SIZED.
453 Return @code{FALSE} if the section is not a valid compressed
454 section. Otherwise, return @code{TRUE}.
458 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
460 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
+ 12];
461 int compression_header_size
;
462 int header_size
= 12;
463 bfd_size_type uncompressed_size
;
465 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
466 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
468 header_size
+= compression_header_size
;
470 if (sec
->rawsize
!= 0
471 || sec
->contents
!= NULL
472 || sec
->compress_status
!= COMPRESS_SECTION_NONE
473 || !bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
475 bfd_set_error (bfd_error_invalid_operation
);
479 /* Read the zlib header. In this case, it should be "ZLIB" followed
480 by the uncompressed section size, 8 bytes in big-endian order. */
481 if (! CONST_STRNEQ ((char*) header
+ compression_header_size
, "ZLIB"))
483 bfd_set_error (bfd_error_wrong_format
);
487 uncompressed_size
= bfd_getb64 (header
+ compression_header_size
+ 4);
488 if (compression_header_size
!= 0
489 && !bfd_check_compression_header (abfd
, header
, sec
,
492 bfd_set_error (bfd_error_wrong_format
);
495 sec
->compressed_size
= sec
->size
;
496 sec
->size
= uncompressed_size
;
497 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
504 bfd_init_section_compress_status
507 bfd_boolean bfd_init_section_compress_status
508 (bfd *abfd, asection *section);
511 If open for read, compress section, update section size with
512 compressed size and set compress_status to COMPRESS_SECTION_DONE.
514 Return @code{FALSE} if the section is not a valid compressed
515 section. Otherwise, return @code{TRUE}.
519 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
521 bfd_size_type uncompressed_size
;
522 bfd_byte
*uncompressed_buffer
;
525 /* Error if not opened for read. */
526 if (abfd
->direction
!= read_direction
529 || sec
->contents
!= NULL
530 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
532 bfd_set_error (bfd_error_invalid_operation
);
536 /* Read in the full section contents and compress it. */
537 uncompressed_size
= sec
->size
;
538 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
539 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
540 0, uncompressed_size
))
544 uncompressed_size
= bfd_compress_section_contents (abfd
, sec
,
547 ret
= uncompressed_size
!= 0;