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"
28 decompress_contents (bfd_byte
*compressed_buffer
,
29 bfd_size_type compressed_size
,
30 bfd_byte
*uncompressed_buffer
,
31 bfd_size_type uncompressed_size
)
36 /* It is possible the section consists of several compressed
37 buffers concatenated together, so we uncompress in a loop. */
41 strm
.avail_in
= compressed_size
- 12;
42 strm
.next_in
= (Bytef
*) compressed_buffer
+ 12;
43 strm
.avail_out
= uncompressed_size
;
45 BFD_ASSERT (Z_OK
== 0);
46 rc
= inflateInit (&strm
);
47 while (strm
.avail_in
> 0 && strm
.avail_out
> 0)
51 strm
.next_out
= ((Bytef
*) uncompressed_buffer
52 + (uncompressed_size
- strm
.avail_out
));
53 rc
= inflate (&strm
, Z_FINISH
);
54 if (rc
!= Z_STREAM_END
)
56 rc
= inflateReset (&strm
);
58 rc
|= inflateEnd (&strm
);
59 return rc
== Z_OK
&& strm
.avail_out
== 0;
62 /* Compress data of the size specified in @var{uncompressed_size}
63 and pointed to by @var{uncompressed_buffer} using zlib and store
64 as the contents field. This function assumes the contents
65 field was allocated using bfd_malloc() or equivalent. If zlib
66 is not installed on this machine, the input is unmodified.
68 Return @code{TRUE} if the full section contents is compressed
72 bfd_compress_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
, sec_ptr sec
,
73 bfd_byte
*uncompressed_buffer
,
74 bfd_size_type uncompressed_size
)
76 uLong compressed_size
;
77 bfd_byte
*compressed_buffer
;
79 compressed_size
= compressBound (uncompressed_size
) + 12;
80 compressed_buffer
= (bfd_byte
*) bfd_malloc (compressed_size
);
82 if (compressed_buffer
== NULL
)
85 if (compress ((Bytef
*) compressed_buffer
+ 12,
87 (const Bytef
*) uncompressed_buffer
,
88 uncompressed_size
) != Z_OK
)
90 free (compressed_buffer
);
91 bfd_set_error (bfd_error_bad_value
);
95 compressed_size
+= 12;
97 /* PR binutils/18087: If compression didn't make the section smaller,
98 just keep it uncompressed. */
99 if (compressed_size
< uncompressed_size
)
101 /* Write the zlib header. In this case, it should be "ZLIB" followed
102 by the uncompressed section size, 8 bytes in big-endian order. */
103 memcpy (compressed_buffer
, "ZLIB", 4);
104 bfd_putb64 (uncompressed_size
, compressed_buffer
+ 4);
105 free (uncompressed_buffer
);
106 sec
->contents
= compressed_buffer
;
107 sec
->size
= compressed_size
;
108 sec
->compress_status
= COMPRESS_SECTION_DONE
;
112 sec
->contents
= uncompressed_buffer
;
113 sec
->compress_status
= COMPRESS_SECTION_NONE
;
121 bfd_get_full_section_contents
124 bfd_boolean bfd_get_full_section_contents
125 (bfd *abfd, asection *section, bfd_byte **ptr);
128 Read all data from @var{section} in BFD @var{abfd}, decompress
129 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
130 return @var{*ptr} with memory malloc'd by this function.
132 Return @code{TRUE} if the full section contents is retrieved
133 successfully. If the section has no contents then this function
134 returns @code{TRUE} but @var{*ptr} is set to NULL.
138 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
143 bfd_size_type save_size
;
144 bfd_size_type save_rawsize
;
145 bfd_byte
*compressed_buffer
;
147 if (abfd
->direction
!= write_direction
&& sec
->rawsize
!= 0)
157 switch (sec
->compress_status
)
159 case COMPRESS_SECTION_NONE
:
162 p
= (bfd_byte
*) bfd_malloc (sz
);
167 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
176 case DECOMPRESS_SECTION_SIZED
:
177 /* Read in the full compressed section contents. */
178 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
179 if (compressed_buffer
== NULL
)
181 save_rawsize
= sec
->rawsize
;
182 save_size
= sec
->size
;
183 /* Clear rawsize, set size to compressed size and set compress_status
184 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
185 the uncompressed size, bfd_get_section_contents will fail. */
187 sec
->size
= sec
->compressed_size
;
188 sec
->compress_status
= COMPRESS_SECTION_NONE
;
189 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
190 0, sec
->compressed_size
);
191 /* Restore rawsize and size. */
192 sec
->rawsize
= save_rawsize
;
193 sec
->size
= save_size
;
194 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
196 goto fail_compressed
;
199 p
= (bfd_byte
*) bfd_malloc (sz
);
201 goto fail_compressed
;
203 if (!decompress_contents (compressed_buffer
, sec
->compressed_size
, p
, sz
))
205 bfd_set_error (bfd_error_bad_value
);
209 free (compressed_buffer
);
213 free (compressed_buffer
);
217 case COMPRESS_SECTION_DONE
:
218 if (sec
->contents
== NULL
)
222 p
= (bfd_byte
*) bfd_malloc (sz
);
227 /* PR 17512; file: 5bc29788. */
228 if (p
!= sec
->contents
)
229 memcpy (p
, sec
->contents
, sz
);
239 bfd_cache_section_contents
242 void bfd_cache_section_contents
243 (asection *sec, void *contents);
246 Stash @var(contents) so any following reads of @var(sec) do
247 not need to decompress again.
251 bfd_cache_section_contents (asection
*sec
, void *contents
)
253 if (sec
->compress_status
== DECOMPRESS_SECTION_SIZED
)
254 sec
->compress_status
= COMPRESS_SECTION_DONE
;
255 sec
->contents
= contents
;
256 sec
->flags
|= SEC_IN_MEMORY
;
262 bfd_is_section_compressed
265 bfd_boolean bfd_is_section_compressed
266 (bfd *abfd, asection *section);
269 Return @code{TRUE} if @var{section} is compressed.
273 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
275 bfd_byte compressed_buffer
[12];
276 unsigned int saved
= sec
->compress_status
;
277 bfd_boolean compressed
;
279 /* Don't decompress the section. */
280 sec
->compress_status
= COMPRESS_SECTION_NONE
;
282 /* Read the zlib header. In this case, it should be "ZLIB" followed
283 by the uncompressed section size, 8 bytes in big-endian order. */
284 compressed
= (bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12)
285 && CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"));
287 /* Check for the pathalogical case of a debug string section that
288 contains the string ZLIB.... as the first entry. We assume that
289 no uncompressed .debug_str section would ever be big enough to
290 have the first byte of its (big-endian) size be non-zero. */
292 && strcmp (sec
->name
, ".debug_str") == 0
293 && ISPRINT (compressed_buffer
[4]))
296 /* Restore compress_status. */
297 sec
->compress_status
= saved
;
303 bfd_init_section_decompress_status
306 bfd_boolean bfd_init_section_decompress_status
307 (bfd *abfd, asection *section);
310 Record compressed section size, update section size with
311 decompressed size and set compress_status to
312 DECOMPRESS_SECTION_SIZED.
314 Return @code{FALSE} if the section is not a valid compressed
315 section or zlib is not installed on this machine. Otherwise,
320 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
322 bfd_byte compressed_buffer
[12];
323 bfd_size_type uncompressed_size
;
325 if (sec
->rawsize
!= 0
326 || sec
->contents
!= NULL
327 || sec
->compress_status
!= COMPRESS_SECTION_NONE
328 || !bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12))
330 bfd_set_error (bfd_error_invalid_operation
);
334 /* Read the zlib header. In this case, it should be "ZLIB" followed
335 by the uncompressed section size, 8 bytes in big-endian order. */
336 if (! CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"))
338 bfd_set_error (bfd_error_wrong_format
);
342 uncompressed_size
= bfd_getb64 (compressed_buffer
+ 4);
343 sec
->compressed_size
= sec
->size
;
344 sec
->size
= uncompressed_size
;
345 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
352 bfd_init_section_compress_status
355 bfd_boolean bfd_init_section_compress_status
356 (bfd *abfd, asection *section);
359 If open for read, compress section, update section size with
360 compressed size and set compress_status to COMPRESS_SECTION_DONE.
362 Return @code{FALSE} if the section is not a valid compressed
363 section or zlib is not installed on this machine. Otherwise,
368 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
370 bfd_size_type uncompressed_size
;
371 bfd_byte
*uncompressed_buffer
;
374 /* Error if not opened for read. */
375 if (abfd
->direction
!= read_direction
378 || sec
->contents
!= NULL
379 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
381 bfd_set_error (bfd_error_invalid_operation
);
385 /* Read in the full section contents and compress it. */
386 uncompressed_size
= sec
->size
;
387 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
388 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
389 0, uncompressed_size
))
392 ret
= bfd_compress_section_contents (abfd
, sec
,