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. */
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
- 12;
47 strm
.next_in
= (Bytef
*) compressed_buffer
+ 12;
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
;
84 #if defined(__GNUC__) && GCC_VERSION < 4007
85 /* Work around a GCC uninitialized warning bug fixed in GCC 4.7. */
90 int orig_compression_header_size
;
91 int compression_header_size
92 = bfd_get_compression_header_size (abfd
, NULL
);
93 bfd_boolean compressed
94 = bfd_is_section_compressed_with_header (abfd
, sec
,
95 &orig_compression_header_size
);
99 /* We shouldn't decompress unsupported compressed section. */
100 if (orig_compression_header_size
< 0)
103 /* Different compression schemes. Just move the compressed section
104 contents to the right position. */
105 if (orig_compression_header_size
== 0)
107 /* Convert it from .zdebug* section. Get the uncompressed
109 zlib_size
= uncompressed_size
;
110 compressed_size
= zlib_size
+ compression_header_size
;
111 uncompressed_size
= bfd_getb64 (uncompressed_buffer
+ 4);
115 /* Convert it to .zdebug* section. */
116 zlib_size
= uncompressed_size
- orig_compression_header_size
;
117 compressed_size
= zlib_size
;
121 compressed_size
= compressBound (uncompressed_size
) + 12;
123 /* When converting from .zdebug* section, uncompress if it leads to
126 && orig_compression_header_size
== 0
127 && compressed_size
> uncompressed_size
)
130 buffer_size
= uncompressed_size
;
135 buffer_size
= compressed_size
+ compression_header_size
;
137 buffer
= (bfd_byte
*) bfd_alloc (abfd
, buffer_size
);
143 sec
->size
= uncompressed_size
;
146 if (!decompress_contents (uncompressed_buffer
, zlib_size
,
147 buffer
, uncompressed_size
))
149 bfd_set_error (bfd_error_bad_value
);
150 bfd_release (abfd
, buffer
);
153 free (uncompressed_buffer
);
154 sec
->contents
= buffer
;
155 sec
->compress_status
= COMPRESS_SECTION_DONE
;
156 return uncompressed_size
;
160 bfd_update_compression_header (abfd
, buffer
, sec
);
161 memmove (buffer
+ compression_header_size
,
162 uncompressed_buffer
+ orig_compression_header_size
,
168 bfd_size_type size
= uncompressed_size
;
169 int header_size
= 12 + compression_header_size
;
170 if (compress ((Bytef
*) buffer
+ header_size
,
172 (const Bytef
*) uncompressed_buffer
,
173 uncompressed_size
) != Z_OK
)
175 bfd_release (abfd
, buffer
);
176 bfd_set_error (bfd_error_bad_value
);
180 compressed_size
+= header_size
;
181 /* PR binutils/18087: If compression didn't make the section smaller,
182 just keep it uncompressed. */
183 if (compressed_size
< uncompressed_size
)
185 bfd_update_compression_header (abfd
, buffer
, sec
);
187 /* Write the zlib header. In this case, it should be "ZLIB"
188 followed by the uncompressed section size, 8 bytes in
190 memcpy (buffer
+ compression_header_size
, "ZLIB", 4);
191 bfd_putb64 (size
, buffer
+ compression_header_size
+ 4);
195 /* NOTE: There is a small memory leak here since
196 uncompressed_buffer is malloced and won't be freed. */
197 bfd_release (abfd
, buffer
);
198 sec
->contents
= uncompressed_buffer
;
199 sec
->compress_status
= COMPRESS_SECTION_NONE
;
200 return uncompressed_size
;
204 free (uncompressed_buffer
);
205 sec
->contents
= buffer
;
206 sec
->size
= compressed_size
;
207 sec
->compress_status
= COMPRESS_SECTION_DONE
;
209 return uncompressed_size
;
214 bfd_get_full_section_contents
217 bfd_boolean bfd_get_full_section_contents
218 (bfd *abfd, asection *section, bfd_byte **ptr);
221 Read all data from @var{section} in BFD @var{abfd}, decompress
222 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
223 return @var{*ptr} with memory malloc'd by this function.
225 Return @code{TRUE} if the full section contents is retrieved
226 successfully. If the section has no contents then this function
227 returns @code{TRUE} but @var{*ptr} is set to NULL.
231 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
236 bfd_size_type save_size
;
237 bfd_size_type save_rawsize
;
238 bfd_byte
*compressed_buffer
;
239 unsigned int compression_header_size
;
241 if (abfd
->direction
!= write_direction
&& sec
->rawsize
!= 0)
251 switch (sec
->compress_status
)
253 case COMPRESS_SECTION_NONE
:
256 p
= (bfd_byte
*) bfd_malloc (sz
);
261 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
270 case DECOMPRESS_SECTION_SIZED
:
271 /* Read in the full compressed section contents. */
272 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
273 if (compressed_buffer
== NULL
)
275 save_rawsize
= sec
->rawsize
;
276 save_size
= sec
->size
;
277 /* Clear rawsize, set size to compressed size and set compress_status
278 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
279 the uncompressed size, bfd_get_section_contents will fail. */
281 sec
->size
= sec
->compressed_size
;
282 sec
->compress_status
= COMPRESS_SECTION_NONE
;
283 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
284 0, sec
->compressed_size
);
285 /* Restore rawsize and size. */
286 sec
->rawsize
= save_rawsize
;
287 sec
->size
= save_size
;
288 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
290 goto fail_compressed
;
293 p
= (bfd_byte
*) bfd_malloc (sz
);
295 goto fail_compressed
;
297 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
298 if (!decompress_contents (compressed_buffer
+ compression_header_size
,
299 sec
->compressed_size
, p
, sz
))
301 bfd_set_error (bfd_error_bad_value
);
305 free (compressed_buffer
);
309 free (compressed_buffer
);
313 case COMPRESS_SECTION_DONE
:
314 if (sec
->contents
== NULL
)
318 p
= (bfd_byte
*) bfd_malloc (sz
);
323 /* PR 17512; file: 5bc29788. */
324 if (p
!= sec
->contents
)
325 memcpy (p
, sec
->contents
, sz
);
335 bfd_cache_section_contents
338 void bfd_cache_section_contents
339 (asection *sec, void *contents);
342 Stash @var(contents) so any following reads of @var(sec) do
343 not need to decompress again.
347 bfd_cache_section_contents (asection
*sec
, void *contents
)
349 if (sec
->compress_status
== DECOMPRESS_SECTION_SIZED
)
350 sec
->compress_status
= COMPRESS_SECTION_DONE
;
351 sec
->contents
= contents
;
352 sec
->flags
|= SEC_IN_MEMORY
;
357 bfd_is_section_compressed_with_header
360 bfd_boolean bfd_is_section_compressed_with_header
361 (bfd *abfd, asection *section,
362 int *compression_header_size_p);
365 Return @code{TRUE} if @var{section} is compressed. Compression
366 header size is returned in @var{compression_header_size_p}. If
367 compression is unsupported, compression header size is returned
372 bfd_is_section_compressed_with_header (bfd
*abfd
, sec_ptr sec
,
373 int *compression_header_size_p
)
375 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
+ 12];
376 int compression_header_size
;
377 int header_size
= 12;
378 unsigned int saved
= sec
->compress_status
;
379 bfd_boolean compressed
;
381 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
382 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
384 header_size
+= compression_header_size
;
386 /* Don't decompress the section. */
387 sec
->compress_status
= COMPRESS_SECTION_NONE
;
389 /* Read the zlib header. In this case, it should be "ZLIB" followed
390 by the uncompressed section size, 8 bytes in big-endian order. */
391 compressed
= bfd_get_section_contents (abfd
, sec
, header
, 0,
393 && CONST_STRNEQ ((char*) header
+ compression_header_size
,
398 if (compression_header_size
!= 0)
400 bfd_size_type uncompressed_size
401 = bfd_getb64 ((bfd_byte
*) header
402 + compression_header_size
+ 4);
403 if (!bfd_check_compression_header (abfd
, header
, sec
,
405 compression_header_size
= -1;
407 /* Check for the pathalogical case of a debug string section that
408 contains the string ZLIB.... as the first entry. We assume that
409 no uncompressed .debug_str section would ever be big enough to
410 have the first byte of its (big-endian) size be non-zero. */
411 else if (strcmp (sec
->name
, ".debug_str") == 0
412 && ISPRINT (header
[compression_header_size
+ 4]))
416 /* Restore compress_status. */
417 sec
->compress_status
= saved
;
418 *compression_header_size_p
= compression_header_size
;
424 bfd_is_section_compressed
427 bfd_boolean bfd_is_section_compressed
428 (bfd *abfd, asection *section);
431 Return @code{TRUE} if @var{section} is compressed.
435 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
437 int compression_header_size
;
438 return (bfd_is_section_compressed_with_header (abfd
, sec
,
439 &compression_header_size
)
440 && compression_header_size
>= 0);
445 bfd_init_section_decompress_status
448 bfd_boolean bfd_init_section_decompress_status
449 (bfd *abfd, asection *section);
452 Record compressed section size, update section size with
453 decompressed size and set compress_status to
454 DECOMPRESS_SECTION_SIZED.
456 Return @code{FALSE} if the section is not a valid compressed
457 section. Otherwise, return @code{TRUE}.
461 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
463 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
+ 12];
464 int compression_header_size
;
465 int header_size
= 12;
466 bfd_size_type uncompressed_size
;
468 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
469 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
471 header_size
+= compression_header_size
;
473 if (sec
->rawsize
!= 0
474 || sec
->contents
!= NULL
475 || sec
->compress_status
!= COMPRESS_SECTION_NONE
476 || !bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
478 bfd_set_error (bfd_error_invalid_operation
);
482 /* Read the zlib header. In this case, it should be "ZLIB" followed
483 by the uncompressed section size, 8 bytes in big-endian order. */
484 if (! CONST_STRNEQ ((char*) header
+ compression_header_size
, "ZLIB"))
486 bfd_set_error (bfd_error_wrong_format
);
490 uncompressed_size
= bfd_getb64 (header
+ compression_header_size
+ 4);
491 if (compression_header_size
!= 0
492 && !bfd_check_compression_header (abfd
, header
, sec
,
495 bfd_set_error (bfd_error_wrong_format
);
498 sec
->compressed_size
= sec
->size
;
499 sec
->size
= uncompressed_size
;
500 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
507 bfd_init_section_compress_status
510 bfd_boolean bfd_init_section_compress_status
511 (bfd *abfd, asection *section);
514 If open for read, compress section, update section size with
515 compressed size and set compress_status to COMPRESS_SECTION_DONE.
517 Return @code{FALSE} if the section is not a valid compressed
518 section. Otherwise, return @code{TRUE}.
522 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
524 bfd_size_type uncompressed_size
;
525 bfd_byte
*uncompressed_buffer
;
528 /* Error if not opened for read. */
529 if (abfd
->direction
!= read_direction
532 || sec
->contents
!= NULL
533 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
535 bfd_set_error (bfd_error_invalid_operation
);
539 /* Read in the full section contents and compress it. */
540 uncompressed_size
= sec
->size
;
541 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
542 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
543 0, uncompressed_size
))
547 uncompressed_size
= bfd_compress_section_contents (abfd
, sec
,
550 ret
= uncompressed_size
!= 0;
561 bfd_boolean bfd_compress_section
562 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
565 If open for write, compress section, update section size with
566 compressed size and set compress_status to COMPRESS_SECTION_DONE.
568 Return @code{FALSE} if compression fail. Otherwise, return
573 bfd_compress_section (bfd
*abfd
, sec_ptr sec
, bfd_byte
*uncompressed_buffer
)
575 bfd_size_type uncompressed_size
= sec
->size
;
577 /* Error if not opened for write. */
578 if (abfd
->direction
!= write_direction
579 || uncompressed_size
== 0
580 || uncompressed_buffer
== NULL
581 || sec
->contents
!= NULL
582 || sec
->compressed_size
!= 0
583 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
585 bfd_set_error (bfd_error_invalid_operation
);
590 return bfd_compress_section_contents (abfd
, sec
, uncompressed_buffer
,
591 uncompressed_size
) != 0;