Commit | Line | Data |
---|---|---|
0acf065b | 1 | /* Compressed section support (intended for debug sections). |
b3adc24a | 2 | Copyright (C) 2008-2020 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 | 56 | strm.next_out = ((Bytef*) uncompressed_buffer |
07d6d2b8 | 57 | + (uncompressed_size - strm.avail_out)); |
4a114e3e L |
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 | 86 | bfd_size_type orig_uncompressed_size; |
4207142d | 87 | unsigned int orig_uncompressed_alignment_pow; |
dab394de | 88 | int header_size = bfd_get_compression_header_size (abfd, NULL); |
151411f8 L |
89 | bfd_boolean compressed |
90 | = bfd_is_section_compressed_with_header (abfd, sec, | |
dab394de | 91 | &orig_compression_header_size, |
4207142d MW |
92 | &orig_uncompressed_size, |
93 | &orig_uncompressed_alignment_pow); | |
dab394de L |
94 | |
95 | /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size, | |
96 | overhead in .zdebug* section. */ | |
97 | if (!header_size) | |
98 | header_size = 12; | |
151411f8 L |
99 | |
100 | if (compressed) | |
101 | { | |
102 | /* We shouldn't decompress unsupported compressed section. */ | |
103 | if (orig_compression_header_size < 0) | |
104 | abort (); | |
4a114e3e | 105 | |
151411f8 L |
106 | /* Different compression schemes. Just move the compressed section |
107 | contents to the right position. */ | |
108 | if (orig_compression_header_size == 0) | |
109 | { | |
110 | /* Convert it from .zdebug* section. Get the uncompressed | |
de194d85 | 111 | size first. We need to subtract the 12-byte overhead in |
dab394de L |
112 | .zdebug* section. Set orig_compression_header_size to |
113 | the 12-bye overhead. */ | |
114 | orig_compression_header_size = 12; | |
115 | zlib_size = uncompressed_size - 12; | |
151411f8 L |
116 | } |
117 | else | |
118 | { | |
dab394de | 119 | /* Convert it to .zdebug* section. */ |
151411f8 | 120 | zlib_size = uncompressed_size - orig_compression_header_size; |
151411f8 | 121 | } |
dab394de L |
122 | |
123 | /* Add the header size. */ | |
124 | compressed_size = zlib_size + header_size; | |
151411f8 L |
125 | } |
126 | else | |
dab394de | 127 | compressed_size = compressBound (uncompressed_size) + header_size; |
4281caad | 128 | |
dab394de L |
129 | /* Uncompress if it leads to smaller size. */ |
130 | if (compressed && compressed_size > orig_uncompressed_size) | |
4a114e3e | 131 | { |
151411f8 | 132 | decompress = TRUE; |
dab394de | 133 | buffer_size = orig_uncompressed_size; |
4a114e3e | 134 | } |
151411f8 L |
135 | else |
136 | { | |
137 | decompress = FALSE; | |
dab394de | 138 | buffer_size = compressed_size; |
151411f8 | 139 | } |
030aeb75 | 140 | buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size); |
151411f8 L |
141 | if (buffer == NULL) |
142 | return 0; | |
4a114e3e | 143 | |
151411f8 | 144 | if (compressed) |
8d001214 | 145 | { |
dab394de | 146 | sec->size = orig_uncompressed_size; |
151411f8 L |
147 | if (decompress) |
148 | { | |
dab394de L |
149 | if (!decompress_contents (uncompressed_buffer |
150 | + orig_compression_header_size, | |
151 | zlib_size, buffer, buffer_size)) | |
151411f8 L |
152 | { |
153 | bfd_set_error (bfd_error_bad_value); | |
030aeb75 | 154 | bfd_release (abfd, buffer); |
151411f8 L |
155 | return 0; |
156 | } | |
157 | free (uncompressed_buffer); | |
fd361982 | 158 | bfd_set_section_alignment (sec, orig_uncompressed_alignment_pow); |
4207142d | 159 | |
151411f8 L |
160 | sec->contents = buffer; |
161 | sec->compress_status = COMPRESS_SECTION_DONE; | |
dab394de | 162 | return orig_uncompressed_size; |
151411f8 L |
163 | } |
164 | else | |
165 | { | |
166 | bfd_update_compression_header (abfd, buffer, sec); | |
dab394de | 167 | memmove (buffer + header_size, |
151411f8 L |
168 | uncompressed_buffer + orig_compression_header_size, |
169 | zlib_size); | |
170 | } | |
8d001214 L |
171 | } |
172 | else | |
173 | { | |
151411f8 L |
174 | if (compress ((Bytef*) buffer + header_size, |
175 | &compressed_size, | |
176 | (const Bytef*) uncompressed_buffer, | |
177 | uncompressed_size) != Z_OK) | |
178 | { | |
030aeb75 | 179 | bfd_release (abfd, buffer); |
151411f8 L |
180 | bfd_set_error (bfd_error_bad_value); |
181 | return 0; | |
182 | } | |
183 | ||
184 | compressed_size += header_size; | |
185 | /* PR binutils/18087: If compression didn't make the section smaller, | |
3e19fb8f L |
186 | just keep it uncompressed. */ |
187 | if (compressed_size < uncompressed_size) | |
dab394de | 188 | bfd_update_compression_header (abfd, buffer, sec); |
151411f8 L |
189 | else |
190 | { | |
030aeb75 L |
191 | /* NOTE: There is a small memory leak here since |
192 | uncompressed_buffer is malloced and won't be freed. */ | |
193 | bfd_release (abfd, buffer); | |
151411f8 L |
194 | sec->contents = uncompressed_buffer; |
195 | sec->compress_status = COMPRESS_SECTION_NONE; | |
196 | return uncompressed_size; | |
197 | } | |
8d001214 | 198 | } |
4a114e3e | 199 | |
151411f8 L |
200 | free (uncompressed_buffer); |
201 | sec->contents = buffer; | |
202 | sec->size = compressed_size; | |
203 | sec->compress_status = COMPRESS_SECTION_DONE; | |
204 | ||
205 | return uncompressed_size; | |
4a114e3e L |
206 | } |
207 | ||
208 | /* | |
209 | FUNCTION | |
210 | bfd_get_full_section_contents | |
211 | ||
212 | SYNOPSIS | |
213 | bfd_boolean bfd_get_full_section_contents | |
214 | (bfd *abfd, asection *section, bfd_byte **ptr); | |
215 | ||
216 | DESCRIPTION | |
217 | Read all data from @var{section} in BFD @var{abfd}, decompress | |
218 | if needed, and store in @var{*ptr}. If @var{*ptr} is NULL, | |
68ffbac6 | 219 | return @var{*ptr} with memory malloc'd by this function. |
4a114e3e L |
220 | |
221 | Return @code{TRUE} if the full section contents is retrieved | |
06614111 NC |
222 | successfully. If the section has no contents then this function |
223 | returns @code{TRUE} but @var{*ptr} is set to NULL. | |
4a114e3e L |
224 | */ |
225 | ||
226 | bfd_boolean | |
227 | bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr) | |
228 | { | |
e57278ef | 229 | bfd_size_type sz; |
4a114e3e | 230 | bfd_byte *p = *ptr; |
82c6068a | 231 | bfd_boolean ret; |
38b774d2 AM |
232 | bfd_size_type save_size; |
233 | bfd_size_type save_rawsize; | |
4a114e3e | 234 | bfd_byte *compressed_buffer; |
151411f8 | 235 | unsigned int compression_header_size; |
4a114e3e | 236 | |
e57278ef AM |
237 | if (abfd->direction != write_direction && sec->rawsize != 0) |
238 | sz = sec->rawsize; | |
239 | else | |
240 | sz = sec->size; | |
4a114e3e | 241 | if (sz == 0) |
06614111 NC |
242 | { |
243 | *ptr = NULL; | |
244 | return TRUE; | |
245 | } | |
4a114e3e L |
246 | |
247 | switch (sec->compress_status) | |
248 | { | |
249 | case COMPRESS_SECTION_NONE: | |
250 | if (p == NULL) | |
251 | { | |
7e56c51c NC |
252 | ufile_ptr filesize = bfd_get_file_size (abfd); |
253 | if (filesize > 0 | |
254 | && filesize < sz | |
125f83f6 NC |
255 | /* PR 24753: Linker created sections can be larger than |
256 | the file size, eg if they are being used to hold stubs. */ | |
fd361982 | 257 | && (bfd_section_flags (sec) & SEC_LINKER_CREATED) == 0 |
c36876fe TC |
258 | /* PR 24753: Sections which have no content should also be |
259 | excluded as they contain no size on disk. */ | |
260 | && (bfd_section_flags (sec) & SEC_HAS_CONTENTS) != 0 | |
7e56c51c NC |
261 | /* The MMO file format supports its own special compression |
262 | technique, but it uses COMPRESS_SECTION_NONE when loading | |
263 | a section's contents. */ | |
264 | && bfd_get_flavour (abfd) != bfd_target_mmo_flavour) | |
265 | { | |
266 | /* PR 24708: Avoid attempts to allocate a ridiculous amount | |
267 | of memory. */ | |
268 | bfd_set_error (bfd_error_no_memory); | |
269 | _bfd_error_handler | |
270 | /* xgettext:c-format */ | |
271 | (_("error: %pB(%pA) section size (%#" PRIx64 " bytes) is larger than file size (%#" PRIx64 " bytes)"), | |
272 | abfd, sec, (uint64_t) sz, (uint64_t) filesize); | |
273 | return FALSE; | |
274 | } | |
0d13c96b | 275 | p = (bfd_byte *) bfd_malloc (sz); |
4a114e3e | 276 | if (p == NULL) |
a18590c3 NC |
277 | { |
278 | /* PR 20801: Provide a more helpful error message. */ | |
279 | if (bfd_get_error () == bfd_error_no_memory) | |
280 | _bfd_error_handler | |
281 | /* xgettext:c-format */ | |
2dcf00ce AM |
282 | (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"), |
283 | abfd, sec, (uint64_t) sz); | |
0630b49c | 284 | return FALSE; |
a18590c3 | 285 | } |
4a114e3e | 286 | } |
06614111 | 287 | |
82c6068a AM |
288 | if (!bfd_get_section_contents (abfd, sec, p, 0, sz)) |
289 | { | |
290 | if (*ptr != p) | |
291 | free (p); | |
292 | return FALSE; | |
293 | } | |
294 | *ptr = p; | |
4a114e3e L |
295 | return TRUE; |
296 | ||
297 | case DECOMPRESS_SECTION_SIZED: | |
82c6068a | 298 | /* Read in the full compressed section contents. */ |
38b774d2 | 299 | compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size); |
82c6068a AM |
300 | if (compressed_buffer == NULL) |
301 | return FALSE; | |
38b774d2 AM |
302 | save_rawsize = sec->rawsize; |
303 | save_size = sec->size; | |
82c6068a AM |
304 | /* Clear rawsize, set size to compressed size and set compress_status |
305 | to COMPRESS_SECTION_NONE. If the compressed size is bigger than | |
306 | the uncompressed size, bfd_get_section_contents will fail. */ | |
307 | sec->rawsize = 0; | |
38b774d2 | 308 | sec->size = sec->compressed_size; |
82c6068a AM |
309 | sec->compress_status = COMPRESS_SECTION_NONE; |
310 | ret = bfd_get_section_contents (abfd, sec, compressed_buffer, | |
38b774d2 | 311 | 0, sec->compressed_size); |
82c6068a | 312 | /* Restore rawsize and size. */ |
38b774d2 AM |
313 | sec->rawsize = save_rawsize; |
314 | sec->size = save_size; | |
4a114e3e | 315 | sec->compress_status = DECOMPRESS_SECTION_SIZED; |
82c6068a AM |
316 | if (!ret) |
317 | goto fail_compressed; | |
4a114e3e | 318 | |
38b774d2 AM |
319 | if (p == NULL) |
320 | p = (bfd_byte *) bfd_malloc (sz); | |
321 | if (p == NULL) | |
4a114e3e | 322 | goto fail_compressed; |
4a114e3e | 323 | |
151411f8 | 324 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
dab394de L |
325 | if (compression_header_size == 0) |
326 | /* Set header size to the zlib header size if it is a | |
327 | SHF_COMPRESSED section. */ | |
328 | compression_header_size = 12; | |
151411f8 | 329 | if (!decompress_contents (compressed_buffer + compression_header_size, |
6438d1be | 330 | sec->compressed_size - compression_header_size, p, sz)) |
82c6068a AM |
331 | { |
332 | bfd_set_error (bfd_error_bad_value); | |
38b774d2 AM |
333 | if (p != *ptr) |
334 | free (p); | |
82c6068a AM |
335 | fail_compressed: |
336 | free (compressed_buffer); | |
337 | return FALSE; | |
338 | } | |
4a114e3e | 339 | |
82c6068a | 340 | free (compressed_buffer); |
38b774d2 AM |
341 | *ptr = p; |
342 | return TRUE; | |
4a114e3e | 343 | |
82c6068a | 344 | case COMPRESS_SECTION_DONE: |
db6b071a NC |
345 | if (sec->contents == NULL) |
346 | return FALSE; | |
82c6068a AM |
347 | if (p == NULL) |
348 | { | |
349 | p = (bfd_byte *) bfd_malloc (sz); | |
350 | if (p == NULL) | |
351 | return FALSE; | |
352 | *ptr = p; | |
353 | } | |
06614111 NC |
354 | /* PR 17512; file: 5bc29788. */ |
355 | if (p != sec->contents) | |
356 | memcpy (p, sec->contents, sz); | |
82c6068a | 357 | return TRUE; |
4a114e3e | 358 | |
82c6068a AM |
359 | default: |
360 | abort (); | |
361 | } | |
4a114e3e L |
362 | } |
363 | ||
8a72cc6e AM |
364 | /* |
365 | FUNCTION | |
366 | bfd_cache_section_contents | |
367 | ||
368 | SYNOPSIS | |
369 | void bfd_cache_section_contents | |
370 | (asection *sec, void *contents); | |
371 | ||
372 | DESCRIPTION | |
373 | Stash @var(contents) so any following reads of @var(sec) do | |
374 | not need to decompress again. | |
375 | */ | |
376 | ||
377 | void | |
378 | bfd_cache_section_contents (asection *sec, void *contents) | |
379 | { | |
380 | if (sec->compress_status == DECOMPRESS_SECTION_SIZED) | |
381 | sec->compress_status = COMPRESS_SECTION_DONE; | |
382 | sec->contents = contents; | |
383 | sec->flags |= SEC_IN_MEMORY; | |
384 | } | |
385 | ||
4a114e3e L |
386 | /* |
387 | FUNCTION | |
151411f8 | 388 | bfd_is_section_compressed_with_header |
4a114e3e L |
389 | |
390 | SYNOPSIS | |
151411f8 L |
391 | bfd_boolean bfd_is_section_compressed_with_header |
392 | (bfd *abfd, asection *section, | |
dab394de | 393 | int *compression_header_size_p, |
4207142d MW |
394 | bfd_size_type *uncompressed_size_p, |
395 | unsigned int *uncompressed_alignment_power_p); | |
4a114e3e L |
396 | |
397 | DESCRIPTION | |
151411f8 | 398 | Return @code{TRUE} if @var{section} is compressed. Compression |
4207142d MW |
399 | header size is returned in @var{compression_header_size_p}, |
400 | uncompressed size is returned in @var{uncompressed_size_p} | |
401 | and the uncompressed data alignement power is returned in | |
402 | @var{uncompressed_align_pow_p}. If compression is | |
403 | unsupported, compression header size is returned with -1 | |
404 | and uncompressed size is returned with 0. | |
4a114e3e L |
405 | */ |
406 | ||
407 | bfd_boolean | |
151411f8 | 408 | bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec, |
dab394de | 409 | int *compression_header_size_p, |
4207142d MW |
410 | bfd_size_type *uncompressed_size_p, |
411 | unsigned int *uncompressed_align_pow_p) | |
4a114e3e | 412 | { |
dab394de | 413 | bfd_byte header[MAX_COMPRESSION_HEADER_SIZE]; |
151411f8 | 414 | int compression_header_size; |
dab394de | 415 | int header_size; |
64f40162 L |
416 | unsigned int saved = sec->compress_status; |
417 | bfd_boolean compressed; | |
418 | ||
131a5a64 L |
419 | *uncompressed_align_pow_p = 0; |
420 | ||
151411f8 L |
421 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
422 | if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE) | |
423 | abort (); | |
dab394de | 424 | header_size = compression_header_size ? compression_header_size : 12; |
151411f8 | 425 | |
64f40162 L |
426 | /* Don't decompress the section. */ |
427 | sec->compress_status = COMPRESS_SECTION_NONE; | |
4a114e3e | 428 | |
dab394de L |
429 | /* Read the header. */ |
430 | if (bfd_get_section_contents (abfd, sec, header, 0, header_size)) | |
431 | { | |
432 | if (compression_header_size == 0) | |
07d6d2b8 | 433 | /* In this case, it should be "ZLIB" followed by the uncompressed |
dab394de L |
434 | section size, 8 bytes in big-endian order. */ |
435 | compressed = CONST_STRNEQ ((char*) header , "ZLIB"); | |
436 | else | |
437 | compressed = TRUE; | |
438 | } | |
439 | else | |
440 | compressed = FALSE; | |
64f40162 | 441 | |
dab394de | 442 | *uncompressed_size_p = sec->size; |
151411f8 L |
443 | if (compressed) |
444 | { | |
445 | if (compression_header_size != 0) | |
446 | { | |
151411f8 | 447 | if (!bfd_check_compression_header (abfd, header, sec, |
4207142d MW |
448 | uncompressed_size_p, |
449 | uncompressed_align_pow_p)) | |
151411f8 L |
450 | compression_header_size = -1; |
451 | } | |
452 | /* Check for the pathalogical case of a debug string section that | |
453 | contains the string ZLIB.... as the first entry. We assume that | |
454 | no uncompressed .debug_str section would ever be big enough to | |
455 | have the first byte of its (big-endian) size be non-zero. */ | |
456 | else if (strcmp (sec->name, ".debug_str") == 0 | |
dab394de | 457 | && ISPRINT (header[4])) |
151411f8 | 458 | compressed = FALSE; |
dab394de L |
459 | else |
460 | *uncompressed_size_p = bfd_getb64 (header + 4); | |
151411f8 | 461 | } |
a953eec9 | 462 | |
64f40162 L |
463 | /* Restore compress_status. */ |
464 | sec->compress_status = saved; | |
151411f8 | 465 | *compression_header_size_p = compression_header_size; |
64f40162 | 466 | return compressed; |
4a114e3e L |
467 | } |
468 | ||
151411f8 L |
469 | /* |
470 | FUNCTION | |
471 | bfd_is_section_compressed | |
472 | ||
473 | SYNOPSIS | |
474 | bfd_boolean bfd_is_section_compressed | |
475 | (bfd *abfd, asection *section); | |
476 | ||
477 | DESCRIPTION | |
478 | Return @code{TRUE} if @var{section} is compressed. | |
479 | */ | |
480 | ||
481 | bfd_boolean | |
482 | bfd_is_section_compressed (bfd *abfd, sec_ptr sec) | |
483 | { | |
484 | int compression_header_size; | |
dab394de | 485 | bfd_size_type uncompressed_size; |
4207142d | 486 | unsigned int uncompressed_align_power; |
151411f8 | 487 | return (bfd_is_section_compressed_with_header (abfd, sec, |
dab394de | 488 | &compression_header_size, |
4207142d MW |
489 | &uncompressed_size, |
490 | &uncompressed_align_power) | |
dab394de L |
491 | && compression_header_size >= 0 |
492 | && uncompressed_size > 0); | |
151411f8 L |
493 | } |
494 | ||
4a114e3e L |
495 | /* |
496 | FUNCTION | |
497 | bfd_init_section_decompress_status | |
498 | ||
499 | SYNOPSIS | |
500 | bfd_boolean bfd_init_section_decompress_status | |
501 | (bfd *abfd, asection *section); | |
502 | ||
503 | DESCRIPTION | |
504 | Record compressed section size, update section size with | |
505 | decompressed size and set compress_status to | |
506 | DECOMPRESS_SECTION_SIZED. | |
507 | ||
508 | Return @code{FALSE} if the section is not a valid compressed | |
18ece1de | 509 | section. Otherwise, return @code{TRUE}. |
4a114e3e L |
510 | */ |
511 | ||
512 | bfd_boolean | |
243340ad | 513 | bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec) |
4a114e3e | 514 | { |
dab394de | 515 | bfd_byte header[MAX_COMPRESSION_HEADER_SIZE]; |
151411f8 | 516 | int compression_header_size; |
dab394de | 517 | int header_size; |
4a114e3e | 518 | bfd_size_type uncompressed_size; |
4207142d | 519 | unsigned int uncompressed_alignment_power = 0; |
4a114e3e | 520 | |
151411f8 L |
521 | compression_header_size = bfd_get_compression_header_size (abfd, sec); |
522 | if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE) | |
523 | abort (); | |
dab394de | 524 | header_size = compression_header_size ? compression_header_size : 12; |
151411f8 | 525 | |
dab394de | 526 | /* Read the header. */ |
4a114e3e L |
527 | if (sec->rawsize != 0 |
528 | || sec->contents != NULL | |
529 | || sec->compress_status != COMPRESS_SECTION_NONE | |
151411f8 | 530 | || !bfd_get_section_contents (abfd, sec, header, 0, header_size)) |
4a114e3e L |
531 | { |
532 | bfd_set_error (bfd_error_invalid_operation); | |
533 | return FALSE; | |
534 | } | |
1b315056 | 535 | |
dab394de | 536 | if (compression_header_size == 0) |
4a114e3e | 537 | { |
dab394de L |
538 | /* In this case, it should be "ZLIB" followed by the uncompressed |
539 | section size, 8 bytes in big-endian order. */ | |
540 | if (! CONST_STRNEQ ((char*) header, "ZLIB")) | |
541 | { | |
542 | bfd_set_error (bfd_error_wrong_format); | |
543 | return FALSE; | |
544 | } | |
545 | uncompressed_size = bfd_getb64 (header + 4); | |
4a114e3e | 546 | } |
dab394de | 547 | else if (!bfd_check_compression_header (abfd, header, sec, |
4207142d MW |
548 | &uncompressed_size, |
549 | &uncompressed_alignment_power)) | |
151411f8 L |
550 | { |
551 | bfd_set_error (bfd_error_wrong_format); | |
552 | return FALSE; | |
553 | } | |
dab394de | 554 | |
4a114e3e L |
555 | sec->compressed_size = sec->size; |
556 | sec->size = uncompressed_size; | |
fd361982 | 557 | bfd_set_section_alignment (sec, uncompressed_alignment_power); |
4a114e3e | 558 | sec->compress_status = DECOMPRESS_SECTION_SIZED; |
1b315056 | 559 | |
4a114e3e | 560 | return TRUE; |
4a114e3e L |
561 | } |
562 | ||
563 | /* | |
564 | FUNCTION | |
565 | bfd_init_section_compress_status | |
566 | ||
567 | SYNOPSIS | |
568 | bfd_boolean bfd_init_section_compress_status | |
569 | (bfd *abfd, asection *section); | |
570 | ||
571 | DESCRIPTION | |
572 | If open for read, compress section, update section size with | |
573 | compressed size and set compress_status to COMPRESS_SECTION_DONE. | |
574 | ||
575 | Return @code{FALSE} if the section is not a valid compressed | |
18ece1de | 576 | section. Otherwise, return @code{TRUE}. |
4a114e3e L |
577 | */ |
578 | ||
579 | bfd_boolean | |
243340ad | 580 | bfd_init_section_compress_status (bfd *abfd, sec_ptr sec) |
4a114e3e | 581 | { |
4a114e3e L |
582 | bfd_size_type uncompressed_size; |
583 | bfd_byte *uncompressed_buffer; | |
4a114e3e L |
584 | |
585 | /* Error if not opened for read. */ | |
586 | if (abfd->direction != read_direction | |
587 | || sec->size == 0 | |
588 | || sec->rawsize != 0 | |
589 | || sec->contents != NULL | |
590 | || sec->compress_status != COMPRESS_SECTION_NONE) | |
1b315056 | 591 | { |
4a114e3e L |
592 | bfd_set_error (bfd_error_invalid_operation); |
593 | return FALSE; | |
1b315056 | 594 | } |
1b315056 | 595 | |
4a114e3e L |
596 | /* Read in the full section contents and compress it. */ |
597 | uncompressed_size = sec->size; | |
598 | uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size); | |
e63d1232 NC |
599 | /* PR 21431 */ |
600 | if (uncompressed_buffer == NULL) | |
601 | return FALSE; | |
602 | ||
4a114e3e L |
603 | if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer, |
604 | 0, uncompressed_size)) | |
e63d1232 | 605 | return FALSE; |
1b315056 | 606 | |
e63d1232 NC |
607 | uncompressed_size = bfd_compress_section_contents (abfd, sec, |
608 | uncompressed_buffer, | |
609 | uncompressed_size); | |
610 | return uncompressed_size != 0; | |
1b315056 | 611 | } |
0ce398f1 L |
612 | |
613 | /* | |
614 | FUNCTION | |
615 | bfd_compress_section | |
616 | ||
617 | SYNOPSIS | |
618 | bfd_boolean bfd_compress_section | |
619 | (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer); | |
620 | ||
621 | DESCRIPTION | |
622 | If open for write, compress section, update section size with | |
623 | compressed size and set compress_status to COMPRESS_SECTION_DONE. | |
624 | ||
625 | Return @code{FALSE} if compression fail. Otherwise, return | |
626 | @code{TRUE}. | |
627 | */ | |
628 | ||
629 | bfd_boolean | |
630 | bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer) | |
631 | { | |
632 | bfd_size_type uncompressed_size = sec->size; | |
633 | ||
634 | /* Error if not opened for write. */ | |
635 | if (abfd->direction != write_direction | |
636 | || uncompressed_size == 0 | |
637 | || uncompressed_buffer == NULL | |
638 | || sec->contents != NULL | |
639 | || sec->compressed_size != 0 | |
640 | || sec->compress_status != COMPRESS_SECTION_NONE) | |
641 | { | |
642 | bfd_set_error (bfd_error_invalid_operation); | |
643 | return FALSE; | |
644 | } | |
645 | ||
646 | /* Compress it. */ | |
647 | return bfd_compress_section_contents (abfd, sec, uncompressed_buffer, | |
3e19fb8f | 648 | uncompressed_size) != 0; |
0ce398f1 | 649 | } |