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. */
27 #include "safe-ctype.h"
31 decompress_contents (bfd_byte
*compressed_buffer
,
32 bfd_size_type compressed_size
,
33 bfd_byte
*uncompressed_buffer
,
34 bfd_size_type uncompressed_size
)
39 /* It is possible the section consists of several compressed
40 buffers concatenated together, so we uncompress in a loop. */
44 strm
.avail_in
= compressed_size
- 12;
45 strm
.next_in
= (Bytef
*) compressed_buffer
+ 12;
46 strm
.avail_out
= uncompressed_size
;
48 BFD_ASSERT (Z_OK
== 0);
49 rc
= inflateInit (&strm
);
50 while (strm
.avail_in
> 0 && strm
.avail_out
> 0)
54 strm
.next_out
= ((Bytef
*) uncompressed_buffer
55 + (uncompressed_size
- strm
.avail_out
));
56 rc
= inflate (&strm
, Z_FINISH
);
57 if (rc
!= Z_STREAM_END
)
59 rc
= inflateReset (&strm
);
61 rc
|= inflateEnd (&strm
);
62 return rc
== Z_OK
&& strm
.avail_out
== 0;
65 /* Compress data of the size specified in @var{uncompressed_size}
66 and pointed to by @var{uncompressed_buffer} using zlib and store
67 as the contents field. This function assumes the contents
68 field was allocated using bfd_malloc() or equivalent. If zlib
69 is not installed on this machine, the input is unmodified.
71 Return @code{TRUE} if the full section contents is compressed
75 bfd_compress_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
76 sec_ptr sec ATTRIBUTE_UNUSED
,
77 bfd_byte
*uncompressed_buffer ATTRIBUTE_UNUSED
,
78 bfd_size_type uncompressed_size ATTRIBUTE_UNUSED
)
80 uLong compressed_size
;
81 bfd_byte
*compressed_buffer
;
83 compressed_size
= compressBound (uncompressed_size
) + 12;
84 compressed_buffer
= (bfd_byte
*) bfd_malloc (compressed_size
);
86 if (compressed_buffer
== NULL
)
89 if (compress ((Bytef
*) compressed_buffer
+ 12,
91 (const Bytef
*) uncompressed_buffer
,
92 uncompressed_size
) != Z_OK
)
94 free (compressed_buffer
);
95 bfd_set_error (bfd_error_bad_value
);
99 /* Write the zlib header. In this case, it should be "ZLIB" followed
100 by the uncompressed section size, 8 bytes in big-endian order. */
101 memcpy (compressed_buffer
, "ZLIB", 4);
102 compressed_buffer
[11] = uncompressed_size
; uncompressed_size
>>= 8;
103 compressed_buffer
[10] = uncompressed_size
; uncompressed_size
>>= 8;
104 compressed_buffer
[9] = uncompressed_size
; uncompressed_size
>>= 8;
105 compressed_buffer
[8] = uncompressed_size
; uncompressed_size
>>= 8;
106 compressed_buffer
[7] = uncompressed_size
; uncompressed_size
>>= 8;
107 compressed_buffer
[6] = uncompressed_size
; uncompressed_size
>>= 8;
108 compressed_buffer
[5] = uncompressed_size
; uncompressed_size
>>= 8;
109 compressed_buffer
[4] = uncompressed_size
;
110 compressed_size
+= 12;
112 /* Free the uncompressed contents if we compress in place. */
113 if (uncompressed_buffer
== sec
->contents
)
114 free (uncompressed_buffer
);
116 sec
->contents
= compressed_buffer
;
117 sec
->size
= compressed_size
;
118 sec
->compress_status
= COMPRESS_SECTION_DONE
;
122 #endif /* HAVE_ZLIB_H */
126 bfd_get_full_section_contents
129 bfd_boolean bfd_get_full_section_contents
130 (bfd *abfd, asection *section, bfd_byte **ptr);
133 Read all data from @var{section} in BFD @var{abfd}, decompress
134 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
135 return @var{*ptr} with memory malloc'd by this function.
137 Return @code{TRUE} if the full section contents is retrieved
138 successfully. If the section has no contents then this function
139 returns @code{TRUE} but @var{*ptr} is set to NULL.
143 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
149 bfd_size_type save_size
;
150 bfd_size_type save_rawsize
;
151 bfd_byte
*compressed_buffer
;
154 if (abfd
->direction
!= write_direction
&& sec
->rawsize
!= 0)
164 switch (sec
->compress_status
)
166 case COMPRESS_SECTION_NONE
:
169 p
= (bfd_byte
*) bfd_malloc (sz
);
174 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
183 case DECOMPRESS_SECTION_SIZED
:
185 bfd_set_error (bfd_error_invalid_operation
);
188 /* Read in the full compressed section contents. */
189 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
190 if (compressed_buffer
== NULL
)
192 save_rawsize
= sec
->rawsize
;
193 save_size
= sec
->size
;
194 /* Clear rawsize, set size to compressed size and set compress_status
195 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
196 the uncompressed size, bfd_get_section_contents will fail. */
198 sec
->size
= sec
->compressed_size
;
199 sec
->compress_status
= COMPRESS_SECTION_NONE
;
200 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
201 0, sec
->compressed_size
);
202 /* Restore rawsize and size. */
203 sec
->rawsize
= save_rawsize
;
204 sec
->size
= save_size
;
205 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
207 goto fail_compressed
;
210 p
= (bfd_byte
*) bfd_malloc (sz
);
212 goto fail_compressed
;
214 if (!decompress_contents (compressed_buffer
, sec
->compressed_size
, p
, sz
))
216 bfd_set_error (bfd_error_bad_value
);
220 free (compressed_buffer
);
224 free (compressed_buffer
);
229 case COMPRESS_SECTION_DONE
:
230 if (sec
->contents
== NULL
)
234 p
= (bfd_byte
*) bfd_malloc (sz
);
239 /* PR 17512; file: 5bc29788. */
240 if (p
!= sec
->contents
)
241 memcpy (p
, sec
->contents
, sz
);
251 bfd_cache_section_contents
254 void bfd_cache_section_contents
255 (asection *sec, void *contents);
258 Stash @var(contents) so any following reads of @var(sec) do
259 not need to decompress again.
263 bfd_cache_section_contents (asection
*sec
, void *contents
)
265 if (sec
->compress_status
== DECOMPRESS_SECTION_SIZED
)
266 sec
->compress_status
= COMPRESS_SECTION_DONE
;
267 sec
->contents
= contents
;
268 sec
->flags
|= SEC_IN_MEMORY
;
274 bfd_is_section_compressed
277 bfd_boolean bfd_is_section_compressed
278 (bfd *abfd, asection *section);
281 Return @code{TRUE} if @var{section} is compressed.
285 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
287 bfd_byte compressed_buffer
[12];
288 unsigned int saved
= sec
->compress_status
;
289 bfd_boolean compressed
;
291 /* Don't decompress the section. */
292 sec
->compress_status
= COMPRESS_SECTION_NONE
;
294 /* Read the zlib header. In this case, it should be "ZLIB" followed
295 by the uncompressed section size, 8 bytes in big-endian order. */
296 compressed
= (bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12)
297 && CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"));
299 /* Check for the pathalogical case of a debug string section that
300 contains the string ZLIB.... as the first entry. We assume that
301 no uncompressed .debug_str section would ever be big enough to
302 have the first byte of its (big-endian) size be non-zero. */
304 && strcmp (sec
->name
, ".debug_str") == 0
305 && ISPRINT (compressed_buffer
[4]))
308 /* Restore compress_status. */
309 sec
->compress_status
= saved
;
315 bfd_init_section_decompress_status
318 bfd_boolean bfd_init_section_decompress_status
319 (bfd *abfd, asection *section);
322 Record compressed section size, update section size with
323 decompressed size and set compress_status to
324 DECOMPRESS_SECTION_SIZED.
326 Return @code{FALSE} if the section is not a valid compressed
327 section or zlib is not installed on this machine. Otherwise,
332 bfd_init_section_decompress_status (bfd
*abfd ATTRIBUTE_UNUSED
,
333 sec_ptr sec ATTRIBUTE_UNUSED
)
336 bfd_set_error (bfd_error_invalid_operation
);
339 bfd_byte compressed_buffer
[12];
340 bfd_size_type uncompressed_size
;
342 if (sec
->rawsize
!= 0
343 || sec
->contents
!= NULL
344 || sec
->compress_status
!= COMPRESS_SECTION_NONE
345 || !bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12))
347 bfd_set_error (bfd_error_invalid_operation
);
351 /* Read the zlib header. In this case, it should be "ZLIB" followed
352 by the uncompressed section size, 8 bytes in big-endian order. */
353 if (! CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"))
355 bfd_set_error (bfd_error_wrong_format
);
359 uncompressed_size
= compressed_buffer
[4]; uncompressed_size
<<= 8;
360 uncompressed_size
+= compressed_buffer
[5]; uncompressed_size
<<= 8;
361 uncompressed_size
+= compressed_buffer
[6]; uncompressed_size
<<= 8;
362 uncompressed_size
+= compressed_buffer
[7]; uncompressed_size
<<= 8;
363 uncompressed_size
+= compressed_buffer
[8]; uncompressed_size
<<= 8;
364 uncompressed_size
+= compressed_buffer
[9]; uncompressed_size
<<= 8;
365 uncompressed_size
+= compressed_buffer
[10]; uncompressed_size
<<= 8;
366 uncompressed_size
+= compressed_buffer
[11];
368 sec
->compressed_size
= sec
->size
;
369 sec
->size
= uncompressed_size
;
370 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
378 bfd_init_section_compress_status
381 bfd_boolean bfd_init_section_compress_status
382 (bfd *abfd, asection *section);
385 If open for read, compress section, update section size with
386 compressed size and set compress_status to COMPRESS_SECTION_DONE.
388 Return @code{FALSE} if the section is not a valid compressed
389 section or zlib is not installed on this machine. Otherwise,
394 bfd_init_section_compress_status (bfd
*abfd ATTRIBUTE_UNUSED
,
395 sec_ptr sec ATTRIBUTE_UNUSED
)
398 bfd_set_error (bfd_error_invalid_operation
);
401 bfd_size_type uncompressed_size
;
402 bfd_byte
*uncompressed_buffer
;
405 /* Error if not opened for read. */
406 if (abfd
->direction
!= read_direction
409 || sec
->contents
!= NULL
410 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
412 bfd_set_error (bfd_error_invalid_operation
);
416 /* Read in the full section contents and compress it. */
417 uncompressed_size
= sec
->size
;
418 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
419 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
420 0, uncompressed_size
))
423 ret
= bfd_compress_section_contents (abfd
, sec
,
427 /* PR binutils/18087: If compression didn't make
428 the section smaller, just keep it uncompressed. */
429 if (ret
&& uncompressed_size
< sec
->size
)
431 free (sec
->contents
);
432 sec
->contents
= uncompressed_buffer
;
433 sec
->size
= uncompressed_size
;
434 sec
->compress_status
= COMPRESS_SECTION_NONE
;
437 free (uncompressed_buffer
);