]>
Commit | Line | Data |
---|---|---|
0acf065b | 1 | /* Compressed section support (intended for debug sections). |
b90efa5b | 2 | Copyright (C) 2008-2015 Free Software Foundation, Inc. |
1b315056 CS |
3 | |
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. */ | |
20 | ||
1b315056 | 21 | #include "sysdep.h" |
243340ad | 22 | #include <zlib.h> |
1b315056 CS |
23 | #include "bfd.h" |
24 | #include "libbfd.h" | |
a953eec9 | 25 | #include "safe-ctype.h" |
1b315056 | 26 | |
4a114e3e L |
27 | static bfd_boolean |
28 | decompress_contents (bfd_byte *compressed_buffer, | |
29 | bfd_size_type compressed_size, | |
30 | bfd_byte *uncompressed_buffer, | |
31 | bfd_size_type uncompressed_size) | |
32 | { | |
33 | z_stream strm; | |
34 | int rc; | |
35 | ||
36 | /* It is possible the section consists of several compressed | |
37 | buffers concatenated together, so we uncompress in a loop. */ | |
38 | strm.zalloc = NULL; | |
39 | strm.zfree = NULL; | |
40 | strm.opaque = NULL; | |
41 | strm.avail_in = compressed_size - 12; | |
42 | strm.next_in = (Bytef*) compressed_buffer + 12; | |
43 | strm.avail_out = uncompressed_size; | |
44 | ||
a253d456 | 45 | BFD_ASSERT (Z_OK == 0); |
4a114e3e | 46 | rc = inflateInit (&strm); |
a29a8af8 | 47 | while (strm.avail_in > 0 && strm.avail_out > 0) |
4a114e3e L |
48 | { |
49 | if (rc != Z_OK) | |
a253d456 | 50 | break; |
4a114e3e L |
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) | |
a253d456 | 55 | break; |
4a114e3e L |
56 | rc = inflateReset (&strm); |
57 | } | |
a253d456 | 58 | rc |= inflateEnd (&strm); |
82c6068a | 59 | return rc == Z_OK && strm.avail_out == 0; |
4a114e3e | 60 | } |
4a114e3e | 61 | |
0b0732e1 L |
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. | |
1b315056 | 67 | |
0b0732e1 L |
68 | Return @code{TRUE} if the full section contents is compressed |
69 | successfully. */ | |
1b315056 | 70 | |
0b0732e1 | 71 | static bfd_boolean |
243340ad L |
72 | bfd_compress_section_contents (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec, |
73 | bfd_byte *uncompressed_buffer, | |
74 | bfd_size_type uncompressed_size) | |
1b315056 | 75 | { |
ae6a0217 | 76 | uLong compressed_size; |
4a114e3e L |
77 | bfd_byte *compressed_buffer; |
78 | ||
79 | compressed_size = compressBound (uncompressed_size) + 12; | |
80 | compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size); | |
81 | ||
4281caad MS |
82 | if (compressed_buffer == NULL) |
83 | return FALSE; | |
84 | ||
4a114e3e L |
85 | if (compress ((Bytef*) compressed_buffer + 12, |
86 | &compressed_size, | |
87 | (const Bytef*) uncompressed_buffer, | |
88 | uncompressed_size) != Z_OK) | |
89 | { | |
90 | free (compressed_buffer); | |
91 | bfd_set_error (bfd_error_bad_value); | |
92 | return FALSE; | |
93 | } | |
94 | ||
4a114e3e L |
95 | compressed_size += 12; |
96 | ||
8d001214 L |
97 | /* PR binutils/18087: If compression didn't make the section smaller, |
98 | just keep it uncompressed. */ | |
99 | if (compressed_size < uncompressed_size) | |
100 | { | |
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; | |
112 | ||
113 | free (uncompressed_buffer); | |
114 | sec->contents = compressed_buffer; | |
115 | sec->size = compressed_size; | |
116 | sec->compress_status = COMPRESS_SECTION_DONE; | |
117 | } | |
118 | else | |
119 | { | |
120 | sec->contents = uncompressed_buffer; | |
121 | sec->compress_status = COMPRESS_SECTION_NONE; | |
122 | } | |
4a114e3e L |
123 | |
124 | return TRUE; | |
4a114e3e L |
125 | } |
126 | ||
127 | /* | |
128 | FUNCTION | |
129 | bfd_get_full_section_contents | |
130 | ||
131 | SYNOPSIS | |
132 | bfd_boolean bfd_get_full_section_contents | |
133 | (bfd *abfd, asection *section, bfd_byte **ptr); | |
134 | ||
135 | DESCRIPTION | |
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, | |
68ffbac6 | 138 | return @var{*ptr} with memory malloc'd by this function. |
4a114e3e L |
139 | |
140 | Return @code{TRUE} if the full section contents is retrieved | |
06614111 NC |
141 | successfully. If the section has no contents then this function |
142 | returns @code{TRUE} but @var{*ptr} is set to NULL. | |
4a114e3e L |
143 | */ |
144 | ||
145 | bfd_boolean | |
146 | bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr) | |
147 | { | |
e57278ef | 148 | bfd_size_type sz; |
4a114e3e | 149 | bfd_byte *p = *ptr; |
82c6068a | 150 | bfd_boolean ret; |
38b774d2 AM |
151 | bfd_size_type save_size; |
152 | bfd_size_type save_rawsize; | |
4a114e3e | 153 | bfd_byte *compressed_buffer; |
4a114e3e | 154 | |
e57278ef AM |
155 | if (abfd->direction != write_direction && sec->rawsize != 0) |
156 | sz = sec->rawsize; | |
157 | else | |
158 | sz = sec->size; | |
4a114e3e | 159 | if (sz == 0) |
06614111 NC |
160 | { |
161 | *ptr = NULL; | |
162 | return TRUE; | |
163 | } | |
4a114e3e L |
164 | |
165 | switch (sec->compress_status) | |
166 | { | |
167 | case COMPRESS_SECTION_NONE: | |
168 | if (p == NULL) | |
169 | { | |
0d13c96b | 170 | p = (bfd_byte *) bfd_malloc (sz); |
4a114e3e L |
171 | if (p == NULL) |
172 | return FALSE; | |
4a114e3e | 173 | } |
06614111 | 174 | |
82c6068a AM |
175 | if (!bfd_get_section_contents (abfd, sec, p, 0, sz)) |
176 | { | |
177 | if (*ptr != p) | |
178 | free (p); | |
179 | return FALSE; | |
180 | } | |
181 | *ptr = p; | |
4a114e3e L |
182 | return TRUE; |
183 | ||
184 | case DECOMPRESS_SECTION_SIZED: | |
82c6068a | 185 | /* Read in the full compressed section contents. */ |
38b774d2 | 186 | compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size); |
82c6068a AM |
187 | if (compressed_buffer == NULL) |
188 | return FALSE; | |
38b774d2 AM |
189 | save_rawsize = sec->rawsize; |
190 | save_size = sec->size; | |
82c6068a AM |
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. */ | |
194 | sec->rawsize = 0; | |
38b774d2 | 195 | sec->size = sec->compressed_size; |
82c6068a AM |
196 | sec->compress_status = COMPRESS_SECTION_NONE; |
197 | ret = bfd_get_section_contents (abfd, sec, compressed_buffer, | |
38b774d2 | 198 | 0, sec->compressed_size); |
82c6068a | 199 | /* Restore rawsize and size. */ |
38b774d2 AM |
200 | sec->rawsize = save_rawsize; |
201 | sec->size = save_size; | |
4a114e3e | 202 | sec->compress_status = DECOMPRESS_SECTION_SIZED; |
82c6068a AM |
203 | if (!ret) |
204 | goto fail_compressed; | |
4a114e3e | 205 | |
38b774d2 AM |
206 | if (p == NULL) |
207 | p = (bfd_byte *) bfd_malloc (sz); | |
208 | if (p == NULL) | |
4a114e3e | 209 | goto fail_compressed; |
4a114e3e | 210 | |
38b774d2 | 211 | if (!decompress_contents (compressed_buffer, sec->compressed_size, p, sz)) |
82c6068a AM |
212 | { |
213 | bfd_set_error (bfd_error_bad_value); | |
38b774d2 AM |
214 | if (p != *ptr) |
215 | free (p); | |
82c6068a AM |
216 | fail_compressed: |
217 | free (compressed_buffer); | |
218 | return FALSE; | |
219 | } | |
4a114e3e | 220 | |
82c6068a | 221 | free (compressed_buffer); |
38b774d2 AM |
222 | *ptr = p; |
223 | return TRUE; | |
4a114e3e | 224 | |
82c6068a | 225 | case COMPRESS_SECTION_DONE: |
db6b071a NC |
226 | if (sec->contents == NULL) |
227 | return FALSE; | |
82c6068a AM |
228 | if (p == NULL) |
229 | { | |
230 | p = (bfd_byte *) bfd_malloc (sz); | |
231 | if (p == NULL) | |
232 | return FALSE; | |
233 | *ptr = p; | |
234 | } | |
06614111 NC |
235 | /* PR 17512; file: 5bc29788. */ |
236 | if (p != sec->contents) | |
237 | memcpy (p, sec->contents, sz); | |
82c6068a | 238 | return TRUE; |
4a114e3e | 239 | |
82c6068a AM |
240 | default: |
241 | abort (); | |
242 | } | |
4a114e3e L |
243 | } |
244 | ||
8a72cc6e AM |
245 | /* |
246 | FUNCTION | |
247 | bfd_cache_section_contents | |
248 | ||
249 | SYNOPSIS | |
250 | void bfd_cache_section_contents | |
251 | (asection *sec, void *contents); | |
252 | ||
253 | DESCRIPTION | |
254 | Stash @var(contents) so any following reads of @var(sec) do | |
255 | not need to decompress again. | |
256 | */ | |
257 | ||
258 | void | |
259 | bfd_cache_section_contents (asection *sec, void *contents) | |
260 | { | |
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; | |
265 | } | |
266 | ||
267 | ||
4a114e3e L |
268 | /* |
269 | FUNCTION | |
270 | bfd_is_section_compressed | |
271 | ||
272 | SYNOPSIS | |
273 | bfd_boolean bfd_is_section_compressed | |
274 | (bfd *abfd, asection *section); | |
275 | ||
276 | DESCRIPTION | |
277 | Return @code{TRUE} if @var{section} is compressed. | |
278 | */ | |
279 | ||
280 | bfd_boolean | |
281 | bfd_is_section_compressed (bfd *abfd, sec_ptr sec) | |
282 | { | |
283 | bfd_byte compressed_buffer [12]; | |
64f40162 L |
284 | unsigned int saved = sec->compress_status; |
285 | bfd_boolean compressed; | |
286 | ||
287 | /* Don't decompress the section. */ | |
288 | sec->compress_status = COMPRESS_SECTION_NONE; | |
4a114e3e L |
289 | |
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. */ | |
64f40162 L |
292 | compressed = (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12) |
293 | && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB")); | |
294 | ||
a953eec9 NC |
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. */ | |
299 | if (compressed | |
300 | && strcmp (sec->name, ".debug_str") == 0 | |
301 | && ISPRINT (compressed_buffer[4])) | |
302 | compressed = FALSE; | |
303 | ||
64f40162 L |
304 | /* Restore compress_status. */ |
305 | sec->compress_status = saved; | |
306 | return compressed; | |
4a114e3e L |
307 | } |
308 | ||
309 | /* | |
310 | FUNCTION | |
311 | bfd_init_section_decompress_status | |
312 | ||
313 | SYNOPSIS | |
314 | bfd_boolean bfd_init_section_decompress_status | |
315 | (bfd *abfd, asection *section); | |
316 | ||
317 | DESCRIPTION | |
318 | Record compressed section size, update section size with | |
319 | decompressed size and set compress_status to | |
320 | DECOMPRESS_SECTION_SIZED. | |
321 | ||
322 | Return @code{FALSE} if the section is not a valid compressed | |
323 | section or zlib is not installed on this machine. Otherwise, | |
324 | return @code{TRUE}. | |
325 | */ | |
326 | ||
327 | bfd_boolean | |
243340ad | 328 | bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec) |
4a114e3e | 329 | { |
4a114e3e L |
330 | bfd_byte compressed_buffer [12]; |
331 | bfd_size_type uncompressed_size; | |
332 | ||
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)) | |
337 | { | |
338 | bfd_set_error (bfd_error_invalid_operation); | |
339 | return FALSE; | |
340 | } | |
1b315056 CS |
341 | |
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. */ | |
4a114e3e L |
344 | if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB")) |
345 | { | |
346 | bfd_set_error (bfd_error_wrong_format); | |
347 | return FALSE; | |
348 | } | |
349 | ||
1b315056 CS |
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]; | |
358 | ||
4a114e3e L |
359 | sec->compressed_size = sec->size; |
360 | sec->size = uncompressed_size; | |
361 | sec->compress_status = DECOMPRESS_SECTION_SIZED; | |
1b315056 | 362 | |
4a114e3e | 363 | return TRUE; |
4a114e3e L |
364 | } |
365 | ||
366 | /* | |
367 | FUNCTION | |
368 | bfd_init_section_compress_status | |
369 | ||
370 | SYNOPSIS | |
371 | bfd_boolean bfd_init_section_compress_status | |
372 | (bfd *abfd, asection *section); | |
373 | ||
374 | DESCRIPTION | |
375 | If open for read, compress section, update section size with | |
376 | compressed size and set compress_status to COMPRESS_SECTION_DONE. | |
377 | ||
378 | Return @code{FALSE} if the section is not a valid compressed | |
379 | section or zlib is not installed on this machine. Otherwise, | |
380 | return @code{TRUE}. | |
381 | */ | |
382 | ||
383 | bfd_boolean | |
243340ad | 384 | bfd_init_section_compress_status (bfd *abfd, sec_ptr sec) |
4a114e3e | 385 | { |
4a114e3e L |
386 | bfd_size_type uncompressed_size; |
387 | bfd_byte *uncompressed_buffer; | |
388 | bfd_boolean ret; | |
389 | ||
390 | /* Error if not opened for read. */ | |
391 | if (abfd->direction != read_direction | |
392 | || sec->size == 0 | |
393 | || sec->rawsize != 0 | |
394 | || sec->contents != NULL | |
395 | || sec->compress_status != COMPRESS_SECTION_NONE) | |
1b315056 | 396 | { |
4a114e3e L |
397 | bfd_set_error (bfd_error_invalid_operation); |
398 | return FALSE; | |
1b315056 | 399 | } |
1b315056 | 400 | |
4a114e3e L |
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)) | |
406 | ret = FALSE; | |
407 | else | |
408 | ret = bfd_compress_section_contents (abfd, sec, | |
409 | uncompressed_buffer, | |
410 | uncompressed_size); | |
1b315056 | 411 | |
4a114e3e | 412 | return ret; |
1b315056 | 413 | } |