]>
Commit | Line | Data |
---|---|---|
0acf065b | 1 | /* Compressed section support (intended for debug sections). |
2571583a | 2 | Copyright (C) 2008-2017 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 | |
151411f8 L |
27 | #define MAX_COMPRESSION_HEADER_SIZE 24 |
28 | ||
4a114e3e L |
29 | static bfd_boolean |
30 | decompress_contents (bfd_byte *compressed_buffer, | |
31 | bfd_size_type compressed_size, | |
32 | bfd_byte *uncompressed_buffer, | |
33 | bfd_size_type uncompressed_size) | |
34 | { | |
35 | z_stream strm; | |
36 | int rc; | |
37 | ||
38 | /* It is possible the section consists of several compressed | |
39 | buffers concatenated together, so we uncompress in a loop. */ | |
de13ef81 NC |
40 | /* PR 18313: The state field in the z_stream structure is supposed |
41 | to be invisible to the user (ie us), but some compilers will | |
42 | still complain about it being used without initialisation. So | |
43 | we first zero the entire z_stream structure and then set the fields | |
44 | that we need. */ | |
45 | memset (& strm, 0, sizeof strm); | |
dab394de L |
46 | strm.avail_in = compressed_size; |
47 | strm.next_in = (Bytef*) compressed_buffer; | |
4a114e3e L |
48 | strm.avail_out = uncompressed_size; |
49 | ||
a253d456 | 50 | BFD_ASSERT (Z_OK == 0); |
4a114e3e | 51 | rc = inflateInit (&strm); |
a29a8af8 | 52 | while (strm.avail_in > 0 && strm.avail_out > 0) |
4a114e3e L |
53 | { |
54 | if (rc != Z_OK) | |
a253d456 | 55 | break; |
4a114e3e L |
56 | strm.next_out = ((Bytef*) uncompressed_buffer |
57 | + (uncompressed_size - strm.avail_out)); | |
58 | rc = inflate (&strm, Z_FINISH); | |
59 | if (rc != Z_STREAM_END) | |
a253d456 | 60 | break; |
4a114e3e L |
61 | rc = inflateReset (&strm); |
62 | } | |
a253d456 | 63 | rc |= inflateEnd (&strm); |
82c6068a | 64 | return rc == Z_OK && strm.avail_out == 0; |
4a114e3e | 65 | } |
4a114e3e | 66 | |
0b0732e1 L |
67 | /* Compress data of the size specified in @var{uncompressed_size} |
68 | and pointed to by @var{uncompressed_buffer} using zlib and store | |
69 | as the contents field. This function assumes the contents | |
18ece1de | 70 | field was allocated using bfd_malloc() or equivalent. |
1b315056 | 71 | |
151411f8 L |
72 | Return the uncompressed size if the full section contents is |
73 | compressed successfully. Otherwise return 0. */ | |
1b315056 | 74 | |
151411f8 L |
75 | static bfd_size_type |
76 | bfd_compress_section_contents (bfd *abfd, sec_ptr sec, | |
243340ad | 77 | bfd_byte *uncompressed_buffer, |
3e19fb8f | 78 | bfd_size_type uncompressed_size) |
1b315056 | 79 | { |
ae6a0217 | 80 | uLong compressed_size; |
151411f8 L |
81 | bfd_byte *buffer; |
82 | bfd_size_type buffer_size; | |
83 | bfd_boolean decompress; | |
51509926 | 84 | int zlib_size = 0; |
151411f8 | 85 | int orig_compression_header_size; |
dab394de L |
86 | bfd_size_type orig_uncompressed_size; |
87 | int header_size = bfd_get_compression_header_size (abfd, NULL); | |
151411f8 L |
88 | bfd_boolean compressed |
89 | = bfd_is_section_compressed_with_header (abfd, sec, | |
dab394de L |
90 | &orig_compression_header_size, |
91 | &orig_uncompressed_size); | |
92 | ||
93 | /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size, | |
94 | overhead in .zdebug* section. */ | |
95 | if (!header_size) | |
96 | header_size = 12; | |
151411f8 L |
97 | |
98 | if (compressed) | |
99 | { | |
100 | /* We shouldn't decompress unsupported compressed section. */ | |
101 | if (orig_compression_header_size < 0) | |
102 | abort (); | |
4a114e3e | 103 | |
151411f8 L |
104 | /* Different compression schemes. Just move the compressed section |
105 | contents to the right position. */ | |
106 | if (orig_compression_header_size == 0) | |
107 | { | |
108 | /* Convert it from .zdebug* section. Get the uncompressed | |
dab394de L |
109 | size first. We need to substract the 12-byte overhead in |
110 | .zdebug* section. Set orig_compression_header_size to | |
111 | the 12-bye overhead. */ | |
112 | orig_compression_header_size = 12; | |
113 | zlib_size = uncompressed_size - 12; | |
151411f8 L |
114 | } |
115 | else | |
116 | { | |
dab394de | 117 | /* Convert it to .zdebug* section. */ |
151411f8 | 118 | zlib_size = uncompressed_size - orig_compression_header_size; |
151411f8 | 119 | } |
dab394de L |
120 | |
121 | /* Add the header size. */ | |
122 | compressed_size = zlib_size + header_size; | |
151411f8 L |
123 | } |
124 | else | |
dab394de | 125 | compressed_size = compressBound (uncompressed_size) + header_size; |
4281caad | 126 | |
dab394de L |
127 | /* Uncompress if it leads to smaller size. */ |
128 | if (compressed && compressed_size > orig_uncompressed_size) | |
4a114e3e | 129 | { |
151411f8 | 130 | decompress = TRUE; |
dab394de | 131 | buffer_size = orig_uncompressed_size; |
4a114e3e | 132 | } |
151411f8 L |
133 | else |
134 | { | |
135 | decompress = FALSE; | |
dab394de | 136 | buffer_size = compressed_size; |
151411f8 | 137 | } |
030aeb75 | 138 | buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size); |
151411f8 L |
139 | if (buffer == NULL) |
140 | return 0; | |
4a114e3e | 141 | |
151411f8 | 142 | if (compressed) |
8d001214 | 143 | { |
dab394de | 144 | sec->size = orig_uncompressed_size; |
151411f8 L |
145 | if (decompress) |
146 | { | |
dab394de L |
147 | if (!decompress_contents (uncompressed_buffer |
148 | + orig_compression_header_size, | |
149 | zlib_size, buffer, buffer_size)) | |
151411f8 L |
150 | { |
151 | bfd_set_error (bfd_error_bad_value); | |
030aeb75 | 152 | bfd_release (abfd, buffer); |
151411f8 L |
153 | return 0; |
154 | } | |
155 | free (uncompressed_buffer); | |
156 | sec->contents = buffer; | |
157 | sec->compress_status = COMPRESS_SECTION_DONE; | |
dab394de | 158 | return orig_uncompressed_size; |
151411f8 L |
159 | } |
160 | else | |
161 | { | |
162 | bfd_update_compression_header (abfd, buffer, sec); | |
dab394de | 163 | memmove (buffer + header_size, |
151411f8 L |
164 | uncompressed_buffer + orig_compression_header_size, |
165 | zlib_size); | |
166 | } | |
8d001214 L |
167 | } |
168 | else | |
169 | { | |
151411f8 L |
170 | if (compress ((Bytef*) buffer + header_size, |
171 | &compressed_size, | |
172 | (const Bytef*) uncompressed_buffer, | |
173 | uncompressed_size) != Z_OK) | |
174 | { | |
030aeb75 | 175 | bfd_release (abfd, buffer); |
151411f8 L |
176 | bfd_set_error (bfd_error_bad_value); |
177 | return 0; | |
178 | } | |
179 | ||
180 | compressed_size += header_size; | |
181 | /* PR binutils/18087: If compression didn't make the section smaller, | |
3e19fb8f L |
182 | just keep it uncompressed. */ |
183 | if (compressed_size < uncompressed_size) | |
dab394de | 184 | bfd_update_compression_header (abfd, buffer, sec); |
151411f8 L |
185 | else |
186 | { | |
030aeb75 L |
187 | /* NOTE: There is a small memory leak here since |
188 | uncompressed_buffer is malloced and won't be freed. */ | |
189 | bfd_release (abfd, buffer); | |
151411f8 L |
190 | sec->contents = uncompressed_buffer; |
191 | sec->compress_status = COMPRESS_SECTION_NONE; | |
192 | return uncompressed_size; | |
193 | } | |
8d001214 | 194 | } |
4a114e3e | 195 | |
151411f8 L |
196 | free (uncompressed_buffer); |
197 | sec->contents = buffer; | |
198 | sec->size = compressed_size; | |
199 | sec->compress_status = COMPRESS_SECTION_DONE; | |
200 | ||
201 | return uncompressed_size; | |
4a114e3e L |
202 | } |
203 | ||
204 | /* | |
205 | FUNCTION | |
206 | bfd_get_full_section_contents | |
207 | ||
208 | SYNOPSIS | |
209 | bfd_boolean bfd_get_full_section_contents | |
210 | (bfd *abfd, asection *section, bfd_byte **ptr); | |
211 | ||
212 | DESCRIPTION | |
213 | Read all data from @var{section} in BFD @var{abfd}, decompress | |
214 | if needed, and store in @var{*ptr}. If @var{*ptr} is NULL, | |
68ffbac6 | 215 | return @var{*ptr} with memory malloc'd by this function. |
4a114e3e L |
216 | |
217 | Return @code{TRUE} if the full section contents is retrieved | |
06614111 NC |
218 | successfully. If the section has no contents then this function |
219 | returns @code{TRUE} but @var{*ptr} is set to NULL. | |
4a114e3e L |
220 | */ |
221 | ||
222 | bfd_boolean | |
223 | bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr) | |
224 | { | |
e57278ef | 225 | bfd_size_type sz; |
4a114e3e | 226 | bfd_byte *p = *ptr; |
82c6068a | 227 | bfd_boolean ret; |
38b774d2 AM |
228 | bfd_size_type save_size; |
229 | bfd_size_type save_rawsize; | |
4a114e3e | 230 | bfd_byte *compressed_buffer; |
151411f8 | 231 | unsigned int compression_header_size; |
4a114e3e | 232 | |
e57278ef AM |
233 | if (abfd->direction != write_direction && sec->rawsize != 0) |
234 | sz = sec->rawsize; | |
235 | else | |
236 | sz = sec->size; | |
4a114e3e | 237 | if (sz == 0) |
06614111 NC |
238 | { |
239 | *ptr = NULL; | |
240 | return TRUE; | |
241 | } | |
cfd14a50 NC |
242 | else if (bfd_get_file_size (abfd) > 0 |
243 | && sz > (bfd_size_type) bfd_get_file_size (abfd)) | |
244 | { | |
245 | *ptr = NULL; | |
246 | return FALSE; | |
247 | } | |
4a114e3e L |
248 | |
249 | switch (sec->compress_status) | |
250 | { | |
251 | case COMPRESS_SECTION_NONE: | |
252 | if (p == NULL) | |
253 | { | |
0d13c96b | 254 | p = (bfd_byte *) bfd_malloc (sz); |
4a114e3e | 255 | if (p == NULL) |
a18590c3 NC |
256 | { |
257 | /* PR 20801: Provide a more helpful error message. */ | |
258 | if (bfd_get_error () == bfd_error_no_memory) | |
259 | _bfd_error_handler | |
260 | /* xgettext:c-format */ | |
261 | (_("error: %B(%A) is too large (%#lx bytes)"), | |
262 | abfd, sec, (long) sz); | |
4a114e3e | 263 | return FALSE; |
a18590c3 | 264 | } |
4a114e3e | 265 | } |
06614111 | 266 | |
82c6068a AM |
267 | if (!bfd_get_section_contents (abfd, sec, p, 0, sz)) |
268 | { | |
269 | if (*ptr != p) | |
270 | free (p); | |
271 | return FALSE; | |
272 | } | |
273 | *ptr = p; | |
4a114e3e L |
274 | return TRUE; |
275 | ||
276 | case DECOMPRESS_SECTION_SIZED: | |
82c6068a | 277 | /* Read in the full compressed section contents. */ |
38b774d2 | 278 | compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size); |
82c6068a AM |
279 | if (compressed_buffer == NULL) |
280 | return FALSE; | |
38b774d2 AM |
281 | save_rawsize = sec->rawsize; |
282 | save_size = sec->size; | |
82c6068a AM |
283 | /* Clear rawsize, set size to compressed size and set compress_status |
284 | to COMPRESS_SECTION_NONE. If the compressed size is bigger than | |
285 | the uncompressed size, bfd_get_section_contents will fail. */ | |
286 | sec->rawsize = 0; | |
38b774d2 | 287 | sec->size = sec->compressed_size; |
82c6068a AM |
288 | sec->compress_status = COMPRESS_SECTION_NONE; |
289 | ret = bfd_get_section_contents (abfd, sec, compressed_buffer, | |
38b774d2 | 290 | 0, sec->compressed_size); |
82c6068a | 291 | /* Restore rawsize and size. */ |
38b774d2 AM |
292 | sec->rawsize = save_rawsize; |
293 | sec->size = save_size; | |
4a114e3e | 294 | sec->compress_status = DECOMPRESS_SECTION_SIZED; |
82c6068a AM |
295 | if (!ret) |
296 | goto fail_compressed; | |
4a114e3e | 297 | |
38b774d2 AM |
298 | if (p == NULL) |
299 | p = (bfd_byte *) bfd_malloc (sz); | |
300 | if (p == NULL) | |
4a114e3e | 301 | goto fail_compressed; |
4a114e3e | 302 | |
151411f8 | 303 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
dab394de L |
304 | if (compression_header_size == 0) |
305 | /* Set header size to the zlib header size if it is a | |
306 | SHF_COMPRESSED section. */ | |
307 | compression_header_size = 12; | |
151411f8 | 308 | if (!decompress_contents (compressed_buffer + compression_header_size, |
6438d1be | 309 | sec->compressed_size - compression_header_size, p, sz)) |
82c6068a AM |
310 | { |
311 | bfd_set_error (bfd_error_bad_value); | |
38b774d2 AM |
312 | if (p != *ptr) |
313 | free (p); | |
82c6068a AM |
314 | fail_compressed: |
315 | free (compressed_buffer); | |
316 | return FALSE; | |
317 | } | |
4a114e3e | 318 | |
82c6068a | 319 | free (compressed_buffer); |
38b774d2 AM |
320 | *ptr = p; |
321 | return TRUE; | |
4a114e3e | 322 | |
82c6068a | 323 | case COMPRESS_SECTION_DONE: |
db6b071a NC |
324 | if (sec->contents == NULL) |
325 | return FALSE; | |
82c6068a AM |
326 | if (p == NULL) |
327 | { | |
328 | p = (bfd_byte *) bfd_malloc (sz); | |
329 | if (p == NULL) | |
330 | return FALSE; | |
331 | *ptr = p; | |
332 | } | |
06614111 NC |
333 | /* PR 17512; file: 5bc29788. */ |
334 | if (p != sec->contents) | |
335 | memcpy (p, sec->contents, sz); | |
82c6068a | 336 | return TRUE; |
4a114e3e | 337 | |
82c6068a AM |
338 | default: |
339 | abort (); | |
340 | } | |
4a114e3e L |
341 | } |
342 | ||
8a72cc6e AM |
343 | /* |
344 | FUNCTION | |
345 | bfd_cache_section_contents | |
346 | ||
347 | SYNOPSIS | |
348 | void bfd_cache_section_contents | |
349 | (asection *sec, void *contents); | |
350 | ||
351 | DESCRIPTION | |
352 | Stash @var(contents) so any following reads of @var(sec) do | |
353 | not need to decompress again. | |
354 | */ | |
355 | ||
356 | void | |
357 | bfd_cache_section_contents (asection *sec, void *contents) | |
358 | { | |
359 | if (sec->compress_status == DECOMPRESS_SECTION_SIZED) | |
360 | sec->compress_status = COMPRESS_SECTION_DONE; | |
361 | sec->contents = contents; | |
362 | sec->flags |= SEC_IN_MEMORY; | |
363 | } | |
364 | ||
4a114e3e L |
365 | /* |
366 | FUNCTION | |
151411f8 | 367 | bfd_is_section_compressed_with_header |
4a114e3e L |
368 | |
369 | SYNOPSIS | |
151411f8 L |
370 | bfd_boolean bfd_is_section_compressed_with_header |
371 | (bfd *abfd, asection *section, | |
dab394de L |
372 | int *compression_header_size_p, |
373 | bfd_size_type *uncompressed_size_p); | |
4a114e3e L |
374 | |
375 | DESCRIPTION | |
151411f8 | 376 | Return @code{TRUE} if @var{section} is compressed. Compression |
dab394de L |
377 | header size is returned in @var{compression_header_size_p} and |
378 | uncompressed size is returned in @var{uncompressed_size_p}. If | |
151411f8 | 379 | compression is unsupported, compression header size is returned |
dab394de | 380 | with -1 and uncompressed size is returned with 0. |
4a114e3e L |
381 | */ |
382 | ||
383 | bfd_boolean | |
151411f8 | 384 | bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec, |
dab394de L |
385 | int *compression_header_size_p, |
386 | bfd_size_type *uncompressed_size_p) | |
4a114e3e | 387 | { |
dab394de | 388 | bfd_byte header[MAX_COMPRESSION_HEADER_SIZE]; |
151411f8 | 389 | int compression_header_size; |
dab394de | 390 | int header_size; |
64f40162 L |
391 | unsigned int saved = sec->compress_status; |
392 | bfd_boolean compressed; | |
393 | ||
151411f8 L |
394 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
395 | if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE) | |
396 | abort (); | |
dab394de | 397 | header_size = compression_header_size ? compression_header_size : 12; |
151411f8 | 398 | |
64f40162 L |
399 | /* Don't decompress the section. */ |
400 | sec->compress_status = COMPRESS_SECTION_NONE; | |
4a114e3e | 401 | |
dab394de L |
402 | /* Read the header. */ |
403 | if (bfd_get_section_contents (abfd, sec, header, 0, header_size)) | |
404 | { | |
405 | if (compression_header_size == 0) | |
406 | /* In this case, it should be "ZLIB" followed by the uncompressed | |
407 | section size, 8 bytes in big-endian order. */ | |
408 | compressed = CONST_STRNEQ ((char*) header , "ZLIB"); | |
409 | else | |
410 | compressed = TRUE; | |
411 | } | |
412 | else | |
413 | compressed = FALSE; | |
64f40162 | 414 | |
dab394de | 415 | *uncompressed_size_p = sec->size; |
151411f8 L |
416 | if (compressed) |
417 | { | |
418 | if (compression_header_size != 0) | |
419 | { | |
151411f8 | 420 | if (!bfd_check_compression_header (abfd, header, sec, |
dab394de | 421 | uncompressed_size_p)) |
151411f8 L |
422 | compression_header_size = -1; |
423 | } | |
424 | /* Check for the pathalogical case of a debug string section that | |
425 | contains the string ZLIB.... as the first entry. We assume that | |
426 | no uncompressed .debug_str section would ever be big enough to | |
427 | have the first byte of its (big-endian) size be non-zero. */ | |
428 | else if (strcmp (sec->name, ".debug_str") == 0 | |
dab394de | 429 | && ISPRINT (header[4])) |
151411f8 | 430 | compressed = FALSE; |
dab394de L |
431 | else |
432 | *uncompressed_size_p = bfd_getb64 (header + 4); | |
151411f8 | 433 | } |
a953eec9 | 434 | |
64f40162 L |
435 | /* Restore compress_status. */ |
436 | sec->compress_status = saved; | |
151411f8 | 437 | *compression_header_size_p = compression_header_size; |
64f40162 | 438 | return compressed; |
4a114e3e L |
439 | } |
440 | ||
151411f8 L |
441 | /* |
442 | FUNCTION | |
443 | bfd_is_section_compressed | |
444 | ||
445 | SYNOPSIS | |
446 | bfd_boolean bfd_is_section_compressed | |
447 | (bfd *abfd, asection *section); | |
448 | ||
449 | DESCRIPTION | |
450 | Return @code{TRUE} if @var{section} is compressed. | |
451 | */ | |
452 | ||
453 | bfd_boolean | |
454 | bfd_is_section_compressed (bfd *abfd, sec_ptr sec) | |
455 | { | |
456 | int compression_header_size; | |
dab394de | 457 | bfd_size_type uncompressed_size; |
151411f8 | 458 | return (bfd_is_section_compressed_with_header (abfd, sec, |
dab394de L |
459 | &compression_header_size, |
460 | &uncompressed_size) | |
461 | && compression_header_size >= 0 | |
462 | && uncompressed_size > 0); | |
151411f8 L |
463 | } |
464 | ||
4a114e3e L |
465 | /* |
466 | FUNCTION | |
467 | bfd_init_section_decompress_status | |
468 | ||
469 | SYNOPSIS | |
470 | bfd_boolean bfd_init_section_decompress_status | |
471 | (bfd *abfd, asection *section); | |
472 | ||
473 | DESCRIPTION | |
474 | Record compressed section size, update section size with | |
475 | decompressed size and set compress_status to | |
476 | DECOMPRESS_SECTION_SIZED. | |
477 | ||
478 | Return @code{FALSE} if the section is not a valid compressed | |
18ece1de | 479 | section. Otherwise, return @code{TRUE}. |
4a114e3e L |
480 | */ |
481 | ||
482 | bfd_boolean | |
243340ad | 483 | bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec) |
4a114e3e | 484 | { |
dab394de | 485 | bfd_byte header[MAX_COMPRESSION_HEADER_SIZE]; |
151411f8 | 486 | int compression_header_size; |
dab394de | 487 | int header_size; |
4a114e3e L |
488 | bfd_size_type uncompressed_size; |
489 | ||
151411f8 L |
490 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
491 | if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE) | |
492 | abort (); | |
dab394de | 493 | header_size = compression_header_size ? compression_header_size : 12; |
151411f8 | 494 | |
dab394de | 495 | /* Read the header. */ |
4a114e3e L |
496 | if (sec->rawsize != 0 |
497 | || sec->contents != NULL | |
498 | || sec->compress_status != COMPRESS_SECTION_NONE | |
151411f8 | 499 | || !bfd_get_section_contents (abfd, sec, header, 0, header_size)) |
4a114e3e L |
500 | { |
501 | bfd_set_error (bfd_error_invalid_operation); | |
502 | return FALSE; | |
503 | } | |
1b315056 | 504 | |
dab394de | 505 | if (compression_header_size == 0) |
4a114e3e | 506 | { |
dab394de L |
507 | /* In this case, it should be "ZLIB" followed by the uncompressed |
508 | section size, 8 bytes in big-endian order. */ | |
509 | if (! CONST_STRNEQ ((char*) header, "ZLIB")) | |
510 | { | |
511 | bfd_set_error (bfd_error_wrong_format); | |
512 | return FALSE; | |
513 | } | |
514 | uncompressed_size = bfd_getb64 (header + 4); | |
4a114e3e | 515 | } |
dab394de L |
516 | else if (!bfd_check_compression_header (abfd, header, sec, |
517 | &uncompressed_size)) | |
151411f8 L |
518 | { |
519 | bfd_set_error (bfd_error_wrong_format); | |
520 | return FALSE; | |
521 | } | |
dab394de | 522 | |
4a114e3e L |
523 | sec->compressed_size = sec->size; |
524 | sec->size = uncompressed_size; | |
525 | sec->compress_status = DECOMPRESS_SECTION_SIZED; | |
1b315056 | 526 | |
4a114e3e | 527 | return TRUE; |
4a114e3e L |
528 | } |
529 | ||
530 | /* | |
531 | FUNCTION | |
532 | bfd_init_section_compress_status | |
533 | ||
534 | SYNOPSIS | |
535 | bfd_boolean bfd_init_section_compress_status | |
536 | (bfd *abfd, asection *section); | |
537 | ||
538 | DESCRIPTION | |
539 | If open for read, compress section, update section size with | |
540 | compressed size and set compress_status to COMPRESS_SECTION_DONE. | |
541 | ||
542 | Return @code{FALSE} if the section is not a valid compressed | |
18ece1de | 543 | section. Otherwise, return @code{TRUE}. |
4a114e3e L |
544 | */ |
545 | ||
546 | bfd_boolean | |
243340ad | 547 | bfd_init_section_compress_status (bfd *abfd, sec_ptr sec) |
4a114e3e | 548 | { |
4a114e3e L |
549 | bfd_size_type uncompressed_size; |
550 | bfd_byte *uncompressed_buffer; | |
4a114e3e L |
551 | |
552 | /* Error if not opened for read. */ | |
553 | if (abfd->direction != read_direction | |
554 | || sec->size == 0 | |
555 | || sec->rawsize != 0 | |
556 | || sec->contents != NULL | |
557 | || sec->compress_status != COMPRESS_SECTION_NONE) | |
1b315056 | 558 | { |
4a114e3e L |
559 | bfd_set_error (bfd_error_invalid_operation); |
560 | return FALSE; | |
1b315056 | 561 | } |
1b315056 | 562 | |
4a114e3e L |
563 | /* Read in the full section contents and compress it. */ |
564 | uncompressed_size = sec->size; | |
565 | uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size); | |
e63d1232 NC |
566 | /* PR 21431 */ |
567 | if (uncompressed_buffer == NULL) | |
568 | return FALSE; | |
569 | ||
4a114e3e L |
570 | if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer, |
571 | 0, uncompressed_size)) | |
e63d1232 | 572 | return FALSE; |
1b315056 | 573 | |
e63d1232 NC |
574 | uncompressed_size = bfd_compress_section_contents (abfd, sec, |
575 | uncompressed_buffer, | |
576 | uncompressed_size); | |
577 | return uncompressed_size != 0; | |
1b315056 | 578 | } |
0ce398f1 L |
579 | |
580 | /* | |
581 | FUNCTION | |
582 | bfd_compress_section | |
583 | ||
584 | SYNOPSIS | |
585 | bfd_boolean bfd_compress_section | |
586 | (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer); | |
587 | ||
588 | DESCRIPTION | |
589 | If open for write, compress section, update section size with | |
590 | compressed size and set compress_status to COMPRESS_SECTION_DONE. | |
591 | ||
592 | Return @code{FALSE} if compression fail. Otherwise, return | |
593 | @code{TRUE}. | |
594 | */ | |
595 | ||
596 | bfd_boolean | |
597 | bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer) | |
598 | { | |
599 | bfd_size_type uncompressed_size = sec->size; | |
600 | ||
601 | /* Error if not opened for write. */ | |
602 | if (abfd->direction != write_direction | |
603 | || uncompressed_size == 0 | |
604 | || uncompressed_buffer == NULL | |
605 | || sec->contents != NULL | |
606 | || sec->compressed_size != 0 | |
607 | || sec->compress_status != COMPRESS_SECTION_NONE) | |
608 | { | |
609 | bfd_set_error (bfd_error_invalid_operation); | |
610 | return FALSE; | |
611 | } | |
612 | ||
613 | /* Compress it. */ | |
614 | return bfd_compress_section_contents (abfd, sec, uncompressed_buffer, | |
3e19fb8f | 615 | uncompressed_size) != 0; |
0ce398f1 | 616 | } |