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