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
;
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_malloc (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
);
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
)
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. */
181 if (compressed_size
< uncompressed_size
)
183 bfd_update_compression_header (abfd
, buffer
, sec
);
185 /* Write the zlib header. In this case, it should be "ZLIB"
186 followed by the uncompressed section size, 8 bytes in
188 memcpy (buffer
+ compression_header_size
, "ZLIB", 4);
189 bfd_putb64 (size
, buffer
+ compression_header_size
+ 4);
193 sec
->contents
= uncompressed_buffer
;
194 sec
->compress_status
= COMPRESS_SECTION_NONE
;
195 return uncompressed_size
;
199 free (uncompressed_buffer
);
200 sec
->contents
= buffer
;
201 sec
->size
= compressed_size
;
202 sec
->compress_status
= COMPRESS_SECTION_DONE
;
204 return uncompressed_size
;
209 bfd_get_full_section_contents
212 bfd_boolean bfd_get_full_section_contents
213 (bfd *abfd, asection *section, bfd_byte **ptr);
216 Read all data from @var{section} in BFD @var{abfd}, decompress
217 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
218 return @var{*ptr} with memory malloc'd by this function.
220 Return @code{TRUE} if the full section contents is retrieved
221 successfully. If the section has no contents then this function
222 returns @code{TRUE} but @var{*ptr} is set to NULL.
226 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
231 bfd_size_type save_size
;
232 bfd_size_type save_rawsize
;
233 bfd_byte
*compressed_buffer
;
234 unsigned int compression_header_size
;
236 if (abfd
->direction
!= write_direction
&& sec
->rawsize
!= 0)
246 switch (sec
->compress_status
)
248 case COMPRESS_SECTION_NONE
:
251 p
= (bfd_byte
*) bfd_malloc (sz
);
256 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
265 case DECOMPRESS_SECTION_SIZED
:
266 /* Read in the full compressed section contents. */
267 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
268 if (compressed_buffer
== NULL
)
270 save_rawsize
= sec
->rawsize
;
271 save_size
= sec
->size
;
272 /* Clear rawsize, set size to compressed size and set compress_status
273 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
274 the uncompressed size, bfd_get_section_contents will fail. */
276 sec
->size
= sec
->compressed_size
;
277 sec
->compress_status
= COMPRESS_SECTION_NONE
;
278 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
279 0, sec
->compressed_size
);
280 /* Restore rawsize and size. */
281 sec
->rawsize
= save_rawsize
;
282 sec
->size
= save_size
;
283 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
285 goto fail_compressed
;
288 p
= (bfd_byte
*) bfd_malloc (sz
);
290 goto fail_compressed
;
292 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
293 if (!decompress_contents (compressed_buffer
+ compression_header_size
,
294 sec
->compressed_size
, p
, sz
))
296 bfd_set_error (bfd_error_bad_value
);
300 free (compressed_buffer
);
304 free (compressed_buffer
);
308 case COMPRESS_SECTION_DONE
:
309 if (sec
->contents
== NULL
)
313 p
= (bfd_byte
*) bfd_malloc (sz
);
318 /* PR 17512; file: 5bc29788. */
319 if (p
!= sec
->contents
)
320 memcpy (p
, sec
->contents
, sz
);
330 bfd_cache_section_contents
333 void bfd_cache_section_contents
334 (asection *sec, void *contents);
337 Stash @var(contents) so any following reads of @var(sec) do
338 not need to decompress again.
342 bfd_cache_section_contents (asection
*sec
, void *contents
)
344 if (sec
->compress_status
== DECOMPRESS_SECTION_SIZED
)
345 sec
->compress_status
= COMPRESS_SECTION_DONE
;
346 sec
->contents
= contents
;
347 sec
->flags
|= SEC_IN_MEMORY
;
352 bfd_is_section_compressed_with_header
355 bfd_boolean bfd_is_section_compressed_with_header
356 (bfd *abfd, asection *section,
357 int *compression_header_size_p);
360 Return @code{TRUE} if @var{section} is compressed. Compression
361 header size is returned in @var{compression_header_size_p}. If
362 compression is unsupported, compression header size is returned
367 bfd_is_section_compressed_with_header (bfd
*abfd
, sec_ptr sec
,
368 int *compression_header_size_p
)
370 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
+ 12];
371 int compression_header_size
;
372 int header_size
= 12;
373 unsigned int saved
= sec
->compress_status
;
374 bfd_boolean compressed
;
376 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
377 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
379 header_size
+= compression_header_size
;
381 /* Don't decompress the section. */
382 sec
->compress_status
= COMPRESS_SECTION_NONE
;
384 /* Read the zlib header. In this case, it should be "ZLIB" followed
385 by the uncompressed section size, 8 bytes in big-endian order. */
386 compressed
= bfd_get_section_contents (abfd
, sec
, header
, 0,
388 && CONST_STRNEQ ((char*) header
+ compression_header_size
,
393 if (compression_header_size
!= 0)
395 bfd_size_type uncompressed_size
396 = bfd_getb64 ((bfd_byte
*) header
397 + compression_header_size
+ 4);
398 if (!bfd_check_compression_header (abfd
, header
, sec
,
400 compression_header_size
= -1;
402 /* Check for the pathalogical case of a debug string section that
403 contains the string ZLIB.... as the first entry. We assume that
404 no uncompressed .debug_str section would ever be big enough to
405 have the first byte of its (big-endian) size be non-zero. */
406 else if (strcmp (sec
->name
, ".debug_str") == 0
407 && ISPRINT (header
[compression_header_size
+ 4]))
411 /* Restore compress_status. */
412 sec
->compress_status
= saved
;
413 *compression_header_size_p
= compression_header_size
;
419 bfd_is_section_compressed
422 bfd_boolean bfd_is_section_compressed
423 (bfd *abfd, asection *section);
426 Return @code{TRUE} if @var{section} is compressed.
430 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
432 int compression_header_size
;
433 return (bfd_is_section_compressed_with_header (abfd
, sec
,
434 &compression_header_size
)
435 && compression_header_size
>= 0);
440 bfd_init_section_decompress_status
443 bfd_boolean bfd_init_section_decompress_status
444 (bfd *abfd, asection *section);
447 Record compressed section size, update section size with
448 decompressed size and set compress_status to
449 DECOMPRESS_SECTION_SIZED.
451 Return @code{FALSE} if the section is not a valid compressed
452 section or zlib is not installed on this machine. Otherwise,
457 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
459 bfd_byte header
[MAX_COMPRESSION_HEADER_SIZE
+ 12];
460 int compression_header_size
;
461 int header_size
= 12;
462 bfd_size_type uncompressed_size
;
464 compression_header_size
= bfd_get_compression_header_size (abfd
, sec
);
465 if (compression_header_size
> MAX_COMPRESSION_HEADER_SIZE
)
467 header_size
+= compression_header_size
;
469 if (sec
->rawsize
!= 0
470 || sec
->contents
!= NULL
471 || sec
->compress_status
!= COMPRESS_SECTION_NONE
472 || !bfd_get_section_contents (abfd
, sec
, header
, 0, header_size
))
474 bfd_set_error (bfd_error_invalid_operation
);
478 /* Read the zlib header. In this case, it should be "ZLIB" followed
479 by the uncompressed section size, 8 bytes in big-endian order. */
480 if (! CONST_STRNEQ ((char*) header
+ compression_header_size
, "ZLIB"))
482 bfd_set_error (bfd_error_wrong_format
);
486 uncompressed_size
= bfd_getb64 (header
+ compression_header_size
+ 4);
487 if (compression_header_size
!= 0
488 && !bfd_check_compression_header (abfd
, header
, sec
,
491 bfd_set_error (bfd_error_wrong_format
);
494 sec
->compressed_size
= sec
->size
;
495 sec
->size
= uncompressed_size
;
496 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
503 bfd_init_section_compress_status
506 bfd_boolean bfd_init_section_compress_status
507 (bfd *abfd, asection *section);
510 If open for read, compress section, update section size with
511 compressed size and set compress_status to COMPRESS_SECTION_DONE.
513 Return @code{FALSE} if the section is not a valid compressed
514 section or zlib is not installed on this machine. Otherwise,
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;