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