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;
63 get_uncompressed_size (bfd_byte
*compressed_size_buffer
)
65 bfd_size_type uncompressed_size
= compressed_size_buffer
[0];
66 uncompressed_size
<<= 8;
67 uncompressed_size
+= compressed_size_buffer
[1];
68 uncompressed_size
<<= 8;
69 uncompressed_size
+= compressed_size_buffer
[2];
70 uncompressed_size
<<= 8;
71 uncompressed_size
+= compressed_size_buffer
[3];
72 uncompressed_size
<<= 8;
73 uncompressed_size
+= compressed_size_buffer
[4];
74 uncompressed_size
<<= 8;
75 uncompressed_size
+= compressed_size_buffer
[5];
76 uncompressed_size
<<= 8;
77 uncompressed_size
+= compressed_size_buffer
[6];
78 uncompressed_size
<<= 8;
79 uncompressed_size
+= compressed_size_buffer
[7];
80 return uncompressed_size
;;
83 /* Compress data of the size specified in @var{uncompressed_size}
84 and pointed to by @var{uncompressed_buffer} using zlib and store
85 as the contents field. This function assumes the contents
86 field was allocated using bfd_malloc() or equivalent. If zlib
87 is not installed on this machine, the input is unmodified.
89 Return @code{TRUE} if the full section contents is compressed
93 bfd_compress_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
, sec_ptr sec
,
94 bfd_byte
*uncompressed_buffer
,
95 bfd_size_type uncompressed_size
)
97 uLong compressed_size
;
98 bfd_byte
*compressed_buffer
;
100 compressed_size
= compressBound (uncompressed_size
) + 12;
101 compressed_buffer
= (bfd_byte
*) bfd_malloc (compressed_size
);
103 if (compressed_buffer
== NULL
)
106 if (compress ((Bytef
*) compressed_buffer
+ 12,
108 (const Bytef
*) uncompressed_buffer
,
109 uncompressed_size
) != Z_OK
)
111 free (compressed_buffer
);
112 bfd_set_error (bfd_error_bad_value
);
116 compressed_size
+= 12;
118 /* PR binutils/18087: If compression didn't make the section smaller,
119 just keep it uncompressed. */
120 if (compressed_size
< uncompressed_size
)
122 /* Write the zlib header. In this case, it should be "ZLIB" followed
123 by the uncompressed section size, 8 bytes in big-endian order. */
124 memcpy (compressed_buffer
, "ZLIB", 4);
125 compressed_buffer
[11] = uncompressed_size
; uncompressed_size
>>= 8;
126 compressed_buffer
[10] = uncompressed_size
; uncompressed_size
>>= 8;
127 compressed_buffer
[9] = uncompressed_size
; uncompressed_size
>>= 8;
128 compressed_buffer
[8] = uncompressed_size
; uncompressed_size
>>= 8;
129 compressed_buffer
[7] = uncompressed_size
; uncompressed_size
>>= 8;
130 compressed_buffer
[6] = uncompressed_size
; uncompressed_size
>>= 8;
131 compressed_buffer
[5] = uncompressed_size
; uncompressed_size
>>= 8;
132 compressed_buffer
[4] = uncompressed_size
;
134 free (uncompressed_buffer
);
135 sec
->contents
= compressed_buffer
;
136 sec
->size
= compressed_size
;
137 sec
->compress_status
= COMPRESS_SECTION_DONE
;
141 sec
->contents
= uncompressed_buffer
;
142 sec
->compress_status
= COMPRESS_SECTION_NONE
;
150 bfd_get_full_section_contents
153 bfd_boolean bfd_get_full_section_contents
154 (bfd *abfd, asection *section, bfd_byte **ptr);
157 Read all data from @var{section} in BFD @var{abfd}, decompress
158 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
159 return @var{*ptr} with memory malloc'd by this function.
161 Return @code{TRUE} if the full section contents is retrieved
162 successfully. If the section has no contents then this function
163 returns @code{TRUE} but @var{*ptr} is set to NULL.
167 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
172 bfd_size_type save_size
;
173 bfd_size_type save_rawsize
;
174 bfd_byte
*compressed_buffer
;
176 if (abfd
->direction
!= write_direction
&& sec
->rawsize
!= 0)
186 switch (sec
->compress_status
)
188 case COMPRESS_SECTION_NONE
:
191 p
= (bfd_byte
*) bfd_malloc (sz
);
196 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
205 case DECOMPRESS_SECTION_SIZED
:
206 /* Read in the full compressed section contents. */
207 compressed_buffer
= (bfd_byte
*) bfd_malloc (sec
->compressed_size
);
208 if (compressed_buffer
== NULL
)
210 save_rawsize
= sec
->rawsize
;
211 save_size
= sec
->size
;
212 /* Clear rawsize, set size to compressed size and set compress_status
213 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
214 the uncompressed size, bfd_get_section_contents will fail. */
216 sec
->size
= sec
->compressed_size
;
217 sec
->compress_status
= COMPRESS_SECTION_NONE
;
218 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
219 0, sec
->compressed_size
);
220 /* Restore rawsize and size. */
221 sec
->rawsize
= save_rawsize
;
222 sec
->size
= save_size
;
223 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
225 goto fail_compressed
;
228 p
= (bfd_byte
*) bfd_malloc (sz
);
230 goto fail_compressed
;
232 if (!decompress_contents (compressed_buffer
, sec
->compressed_size
, p
, sz
))
234 bfd_set_error (bfd_error_bad_value
);
238 free (compressed_buffer
);
242 free (compressed_buffer
);
246 case COMPRESS_SECTION_DONE
:
247 if (sec
->contents
== NULL
)
251 p
= (bfd_byte
*) bfd_malloc (sz
);
256 /* PR 17512; file: 5bc29788. */
257 if (p
!= sec
->contents
)
258 memcpy (p
, sec
->contents
, sz
);
268 bfd_cache_section_contents
271 void bfd_cache_section_contents
272 (asection *sec, void *contents);
275 Stash @var(contents) so any following reads of @var(sec) do
276 not need to decompress again.
280 bfd_cache_section_contents (asection
*sec
, void *contents
)
282 if (sec
->compress_status
== DECOMPRESS_SECTION_SIZED
)
283 sec
->compress_status
= COMPRESS_SECTION_DONE
;
284 sec
->contents
= contents
;
285 sec
->flags
|= SEC_IN_MEMORY
;
291 bfd_is_section_compressed
294 bfd_boolean bfd_is_section_compressed
295 (bfd *abfd, asection *section);
298 Return @code{TRUE} if @var{section} is compressed.
302 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
304 bfd_byte compressed_buffer
[12];
305 unsigned int saved
= sec
->compress_status
;
306 bfd_boolean compressed
;
308 /* Don't decompress the section. */
309 sec
->compress_status
= COMPRESS_SECTION_NONE
;
311 /* Read the zlib header. In this case, it should be "ZLIB" followed
312 by the uncompressed section size, 8 bytes in big-endian order. */
313 compressed
= (bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12)
314 && CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"));
316 /* Check for the pathalogical case of a debug string section that
317 contains the string ZLIB.... as the first entry. We assume that
318 no uncompressed .debug_str section would ever be big enough to
319 have the first byte of its (big-endian) size be non-zero. */
321 && strcmp (sec
->name
, ".debug_str") == 0
322 && ISPRINT (compressed_buffer
[4]))
325 /* Restore compress_status. */
326 sec
->compress_status
= saved
;
332 bfd_init_section_decompress_status
335 bfd_boolean bfd_init_section_decompress_status
336 (bfd *abfd, asection *section);
339 Record compressed section size, update section size with
340 decompressed size and set compress_status to
341 DECOMPRESS_SECTION_SIZED.
343 Return @code{FALSE} if the section is not a valid compressed
344 section or zlib is not installed on this machine. Otherwise,
349 bfd_init_section_decompress_status (bfd
*abfd
, sec_ptr sec
)
351 bfd_byte compressed_buffer
[12];
352 bfd_size_type uncompressed_size
;
354 if (sec
->rawsize
!= 0
355 || sec
->contents
!= NULL
356 || sec
->compress_status
!= COMPRESS_SECTION_NONE
357 || !bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12))
359 bfd_set_error (bfd_error_invalid_operation
);
363 /* Read the zlib header. In this case, it should be "ZLIB" followed
364 by the uncompressed section size, 8 bytes in big-endian order. */
365 if (! CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"))
367 bfd_set_error (bfd_error_wrong_format
);
371 uncompressed_size
= get_uncompressed_size (compressed_buffer
+ 4);
372 sec
->compressed_size
= sec
->size
;
373 sec
->size
= uncompressed_size
;
374 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
381 bfd_init_section_compress_status
384 bfd_boolean bfd_init_section_compress_status
385 (bfd *abfd, asection *section);
388 If open for read, compress section, update section size with
389 compressed size and set compress_status to COMPRESS_SECTION_DONE.
391 Return @code{FALSE} if the section is not a valid compressed
392 section or zlib is not installed on this machine. Otherwise,
397 bfd_init_section_compress_status (bfd
*abfd
, sec_ptr sec
)
399 bfd_size_type uncompressed_size
;
400 bfd_byte
*uncompressed_buffer
;
403 /* Error if not opened for read. */
404 if (abfd
->direction
!= read_direction
407 || sec
->contents
!= NULL
408 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
410 bfd_set_error (bfd_error_invalid_operation
);
414 /* Read in the full section contents and compress it. */
415 uncompressed_size
= sec
->size
;
416 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
417 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
418 0, uncompressed_size
))
421 ret
= bfd_compress_section_contents (abfd
, sec
,