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 compressed_buffer
[11] = uncompressed_size
; uncompressed_size
>>= 8;
105 compressed_buffer
[10] = uncompressed_size
; uncompressed_size
>>= 8;
106 compressed_buffer
[9] = uncompressed_size
; uncompressed_size
>>= 8;
107 compressed_buffer
[8] = uncompressed_size
; uncompressed_size
>>= 8;
108 compressed_buffer
[7] = uncompressed_size
; uncompressed_size
>>= 8;
109 compressed_buffer
[6] = uncompressed_size
; uncompressed_size
>>= 8;
110 compressed_buffer
[5] = uncompressed_size
; uncompressed_size
>>= 8;
111 compressed_buffer
[4] = uncompressed_size
;
113 free (uncompressed_buffer
);
114 sec
->contents
= compressed_buffer
;
115 sec
->size
= compressed_size
;
116 sec
->compress_status
= COMPRESS_SECTION_DONE
;
120 sec
->contents
= uncompressed_buffer
;
121 sec
->compress_status
= COMPRESS_SECTION_NONE
;
129 bfd_get_full_section_contents
132 bfd_boolean bfd_get_full_section_contents
133 (bfd *abfd, asection *section, bfd_byte **ptr);
136 Read all data from @var{section} in BFD @var{abfd}, decompress
137 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
138 return @var{*ptr} with memory malloc'd by this function.
140 Return @code{TRUE} if the full section contents is retrieved
141 successfully. If the section has no contents then this function
142 returns @code{TRUE} but @var{*ptr} is set to NULL.
146 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
151 bfd_size_type save_size
;
152 bfd_size_type save_rawsize
;
153 bfd_byte
*compressed_buffer
;
155 if (abfd
->direction
!= write_direction
&& sec
->rawsize
!= 0)
165 switch (sec
->compress_status
)
167 case COMPRESS_SECTION_NONE
:
170 p
= (bfd_byte
*) bfd_malloc (sz
);
175 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
184 case DECOMPRESS_SECTION_SIZED
:
185 /* Read in the full compressed section contents. */
186 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
187 if (compressed_buffer
== NULL
)
189 save_rawsize
= sec
->rawsize
;
190 save_size
= sec
->size
;
191 /* Clear rawsize, set size to compressed size and set compress_status
192 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
193 the uncompressed size, bfd_get_section_contents will fail. */
195 sec
->size
= sec
->compressed_size
;
196 sec
->compress_status
= COMPRESS_SECTION_NONE
;
197 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
198 0, sec
->compressed_size
);
199 /* Restore rawsize and size. */
200 sec
->rawsize
= save_rawsize
;
201 sec
->size
= save_size
;
202 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
204 goto fail_compressed
;
207 p
= (bfd_byte
*) bfd_malloc (sz
);
209 goto fail_compressed
;
211 if (!decompress_contents (compressed_buffer
, sec
->compressed_size
, p
, sz
))
213 bfd_set_error (bfd_error_bad_value
);
217 free (compressed_buffer
);
221 free (compressed_buffer
);
225 case COMPRESS_SECTION_DONE
:
226 if (sec
->contents
== NULL
)
230 p
= (bfd_byte
*) bfd_malloc (sz
);
235 /* PR 17512; file: 5bc29788. */
236 if (p
!= sec
->contents
)
237 memcpy (p
, sec
->contents
, sz
);
247 bfd_cache_section_contents
250 void bfd_cache_section_contents
251 (asection *sec, void *contents);
254 Stash @var(contents) so any following reads of @var(sec) do
255 not need to decompress again.
259 bfd_cache_section_contents (asection
*sec
, void *contents
)
261 if (sec
->compress_status
== DECOMPRESS_SECTION_SIZED
)
262 sec
->compress_status
= COMPRESS_SECTION_DONE
;
263 sec
->contents
= contents
;
264 sec
->flags
|= SEC_IN_MEMORY
;
270 bfd_is_section_compressed
273 bfd_boolean bfd_is_section_compressed
274 (bfd *abfd, asection *section);
277 Return @code{TRUE} if @var{section} is compressed.
281 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
283 bfd_byte compressed_buffer
[12];
284 unsigned int saved
= sec
->compress_status
;
285 bfd_boolean compressed
;
287 /* Don't decompress the section. */
288 sec
->compress_status
= COMPRESS_SECTION_NONE
;
290 /* Read the zlib header. In this case, it should be "ZLIB" followed
291 by the uncompressed section size, 8 bytes in big-endian order. */
292 compressed
= (bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12)
293 && CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"));
295 /* Check for the pathalogical case of a debug string section that
296 contains the string ZLIB.... as the first entry. We assume that
297 no uncompressed .debug_str section would ever be big enough to
298 have the first byte of its (big-endian) size be non-zero. */
300 && strcmp (sec
->name
, ".debug_str") == 0
301 && ISPRINT (compressed_buffer
[4]))
304 /* Restore compress_status. */
305 sec
->compress_status
= saved
;
311 bfd_init_section_decompress_status
314 bfd_boolean bfd_init_section_decompress_status
315 (bfd *abfd, asection *section);
318 Record compressed section size, update section size with
319 decompressed size and set compress_status to
320 DECOMPRESS_SECTION_SIZED.
322 Return @code{FALSE} if the section is not a valid compressed
323 section or zlib is not installed on this machine. Otherwise,
328 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
330 bfd_byte compressed_buffer
[12];
331 bfd_size_type uncompressed_size
;
333 if (sec
->rawsize
!= 0
334 || sec
->contents
!= NULL
335 || sec
->compress_status
!= COMPRESS_SECTION_NONE
336 || !bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12))
338 bfd_set_error (bfd_error_invalid_operation
);
342 /* Read the zlib header. In this case, it should be "ZLIB" followed
343 by the uncompressed section size, 8 bytes in big-endian order. */
344 if (! CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"))
346 bfd_set_error (bfd_error_wrong_format
);
350 uncompressed_size
= compressed_buffer
[4]; uncompressed_size
<<= 8;
351 uncompressed_size
+= compressed_buffer
[5]; uncompressed_size
<<= 8;
352 uncompressed_size
+= compressed_buffer
[6]; uncompressed_size
<<= 8;
353 uncompressed_size
+= compressed_buffer
[7]; uncompressed_size
<<= 8;
354 uncompressed_size
+= compressed_buffer
[8]; uncompressed_size
<<= 8;
355 uncompressed_size
+= compressed_buffer
[9]; uncompressed_size
<<= 8;
356 uncompressed_size
+= compressed_buffer
[10]; uncompressed_size
<<= 8;
357 uncompressed_size
+= compressed_buffer
[11];
359 sec
->compressed_size
= sec
->size
;
360 sec
->size
= uncompressed_size
;
361 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
368 bfd_init_section_compress_status
371 bfd_boolean bfd_init_section_compress_status
372 (bfd *abfd, asection *section);
375 If open for read, compress section, update section size with
376 compressed size and set compress_status to COMPRESS_SECTION_DONE.
378 Return @code{FALSE} if the section is not a valid compressed
379 section or zlib is not installed on this machine. Otherwise,
384 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
386 bfd_size_type uncompressed_size
;
387 bfd_byte
*uncompressed_buffer
;
390 /* Error if not opened for read. */
391 if (abfd
->direction
!= read_direction
394 || sec
->contents
!= NULL
395 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
397 bfd_set_error (bfd_error_invalid_operation
);
401 /* Read in the full section contents and compress it. */
402 uncompressed_size
= sec
->size
;
403 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
404 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
405 0, uncompressed_size
))
408 ret
= bfd_compress_section_contents (abfd
, sec
,