Add a fast_hash function in common-utils
[binutils-gdb.git] / bfd / compress.c
blob8f96d064bde1369a8956a3d3ed9d8599b86ead86
1 /* Compressed section support (intended for debug sections).
2 Copyright (C) 2008-2019 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #include "sysdep.h"
22 #include <zlib.h>
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "safe-ctype.h"
27 #define MAX_COMPRESSION_HEADER_SIZE 24
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)
35 z_stream strm;
36 int rc;
38 /* It is possible the section consists of several compressed
39 buffers concatenated together, so we uncompress in a loop. */
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);
46 strm.avail_in = compressed_size;
47 strm.next_in = (Bytef*) compressed_buffer;
48 strm.avail_out = uncompressed_size;
50 BFD_ASSERT (Z_OK == 0);
51 rc = inflateInit (&strm);
52 while (strm.avail_in > 0 && strm.avail_out > 0)
54 if (rc != Z_OK)
55 break;
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)
60 break;
61 rc = inflateReset (&strm);
63 rc |= inflateEnd (&strm);
64 return rc == Z_OK && strm.avail_out == 0;
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
70 field was allocated using bfd_malloc() or equivalent.
72 Return the uncompressed size if the full section contents is
73 compressed successfully. Otherwise return 0. */
75 static bfd_size_type
76 bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
77 bfd_byte *uncompressed_buffer,
78 bfd_size_type uncompressed_size)
80 uLong compressed_size;
81 bfd_byte *buffer;
82 bfd_size_type buffer_size;
83 bfd_boolean decompress;
84 int zlib_size = 0;
85 int orig_compression_header_size;
86 bfd_size_type orig_uncompressed_size;
87 unsigned int orig_uncompressed_alignment_pow;
88 int header_size = bfd_get_compression_header_size (abfd, NULL);
89 bfd_boolean compressed
90 = bfd_is_section_compressed_with_header (abfd, sec,
91 &orig_compression_header_size,
92 &orig_uncompressed_size,
93 &orig_uncompressed_alignment_pow);
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;
100 if (compressed)
102 /* We shouldn't decompress unsupported compressed section. */
103 if (orig_compression_header_size < 0)
104 abort ();
106 /* Different compression schemes. Just move the compressed section
107 contents to the right position. */
108 if (orig_compression_header_size == 0)
110 /* Convert it from .zdebug* section. Get the uncompressed
111 size first. We need to subtract the 12-byte overhead in
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;
117 else
119 /* Convert it to .zdebug* section. */
120 zlib_size = uncompressed_size - orig_compression_header_size;
123 /* Add the header size. */
124 compressed_size = zlib_size + header_size;
126 else
127 compressed_size = compressBound (uncompressed_size) + header_size;
129 /* Uncompress if it leads to smaller size. */
130 if (compressed && compressed_size > orig_uncompressed_size)
132 decompress = TRUE;
133 buffer_size = orig_uncompressed_size;
135 else
137 decompress = FALSE;
138 buffer_size = compressed_size;
140 buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
141 if (buffer == NULL)
142 return 0;
144 if (compressed)
146 sec->size = orig_uncompressed_size;
147 if (decompress)
149 if (!decompress_contents (uncompressed_buffer
150 + orig_compression_header_size,
151 zlib_size, buffer, buffer_size))
153 bfd_set_error (bfd_error_bad_value);
154 bfd_release (abfd, buffer);
155 return 0;
157 free (uncompressed_buffer);
158 bfd_set_section_alignment (sec, orig_uncompressed_alignment_pow);
160 sec->contents = buffer;
161 sec->compress_status = COMPRESS_SECTION_DONE;
162 return orig_uncompressed_size;
164 else
166 bfd_update_compression_header (abfd, buffer, sec);
167 memmove (buffer + header_size,
168 uncompressed_buffer + orig_compression_header_size,
169 zlib_size);
172 else
174 if (compress ((Bytef*) buffer + header_size,
175 &compressed_size,
176 (const Bytef*) uncompressed_buffer,
177 uncompressed_size) != Z_OK)
179 bfd_release (abfd, buffer);
180 bfd_set_error (bfd_error_bad_value);
181 return 0;
184 compressed_size += header_size;
185 /* PR binutils/18087: If compression didn't make the section smaller,
186 just keep it uncompressed. */
187 if (compressed_size < uncompressed_size)
188 bfd_update_compression_header (abfd, buffer, sec);
189 else
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);
194 sec->contents = uncompressed_buffer;
195 sec->compress_status = COMPRESS_SECTION_NONE;
196 return uncompressed_size;
200 free (uncompressed_buffer);
201 sec->contents = buffer;
202 sec->size = compressed_size;
203 sec->compress_status = COMPRESS_SECTION_DONE;
205 return uncompressed_size;
209 FUNCTION
210 bfd_get_full_section_contents
212 SYNOPSIS
213 bfd_boolean bfd_get_full_section_contents
214 (bfd *abfd, asection *section, bfd_byte **ptr);
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,
219 return @var{*ptr} with memory malloc'd by this function.
221 Return @code{TRUE} if the full section contents is retrieved
222 successfully. If the section has no contents then this function
223 returns @code{TRUE} but @var{*ptr} is set to NULL.
226 bfd_boolean
227 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
229 bfd_size_type sz;
230 bfd_byte *p = *ptr;
231 bfd_boolean ret;
232 bfd_size_type save_size;
233 bfd_size_type save_rawsize;
234 bfd_byte *compressed_buffer;
235 unsigned int compression_header_size;
237 if (abfd->direction != write_direction && sec->rawsize != 0)
238 sz = sec->rawsize;
239 else
240 sz = sec->size;
241 if (sz == 0)
243 *ptr = NULL;
244 return TRUE;
247 switch (sec->compress_status)
249 case COMPRESS_SECTION_NONE:
250 if (p == NULL)
252 ufile_ptr filesize = bfd_get_file_size (abfd);
253 if (filesize > 0
254 && filesize < sz
255 /* PR 24753: Linker created sections can be larger than
256 the file size, eg if they are being used to hold stubs. */
257 && (bfd_section_flags (sec) & SEC_LINKER_CREATED) == 0
258 /* The MMO file format supports its own special compression
259 technique, but it uses COMPRESS_SECTION_NONE when loading
260 a section's contents. */
261 && bfd_get_flavour (abfd) != bfd_target_mmo_flavour)
263 /* PR 24708: Avoid attempts to allocate a ridiculous amount
264 of memory. */
265 bfd_set_error (bfd_error_no_memory);
266 _bfd_error_handler
267 /* xgettext:c-format */
268 (_("error: %pB(%pA) section size (%#" PRIx64 " bytes) is larger than file size (%#" PRIx64 " bytes)"),
269 abfd, sec, (uint64_t) sz, (uint64_t) filesize);
270 return FALSE;
272 p = (bfd_byte *) bfd_malloc (sz);
273 if (p == NULL)
275 /* PR 20801: Provide a more helpful error message. */
276 if (bfd_get_error () == bfd_error_no_memory)
277 _bfd_error_handler
278 /* xgettext:c-format */
279 (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
280 abfd, sec, (uint64_t) sz);
281 return FALSE;
285 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
287 if (*ptr != p)
288 free (p);
289 return FALSE;
291 *ptr = p;
292 return TRUE;
294 case DECOMPRESS_SECTION_SIZED:
295 /* Read in the full compressed section contents. */
296 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
297 if (compressed_buffer == NULL)
298 return FALSE;
299 save_rawsize = sec->rawsize;
300 save_size = sec->size;
301 /* Clear rawsize, set size to compressed size and set compress_status
302 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
303 the uncompressed size, bfd_get_section_contents will fail. */
304 sec->rawsize = 0;
305 sec->size = sec->compressed_size;
306 sec->compress_status = COMPRESS_SECTION_NONE;
307 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
308 0, sec->compressed_size);
309 /* Restore rawsize and size. */
310 sec->rawsize = save_rawsize;
311 sec->size = save_size;
312 sec->compress_status = DECOMPRESS_SECTION_SIZED;
313 if (!ret)
314 goto fail_compressed;
316 if (p == NULL)
317 p = (bfd_byte *) bfd_malloc (sz);
318 if (p == NULL)
319 goto fail_compressed;
321 compression_header_size = bfd_get_compression_header_size (abfd, sec);
322 if (compression_header_size == 0)
323 /* Set header size to the zlib header size if it is a
324 SHF_COMPRESSED section. */
325 compression_header_size = 12;
326 if (!decompress_contents (compressed_buffer + compression_header_size,
327 sec->compressed_size - compression_header_size, p, sz))
329 bfd_set_error (bfd_error_bad_value);
330 if (p != *ptr)
331 free (p);
332 fail_compressed:
333 free (compressed_buffer);
334 return FALSE;
337 free (compressed_buffer);
338 *ptr = p;
339 return TRUE;
341 case COMPRESS_SECTION_DONE:
342 if (sec->contents == NULL)
343 return FALSE;
344 if (p == NULL)
346 p = (bfd_byte *) bfd_malloc (sz);
347 if (p == NULL)
348 return FALSE;
349 *ptr = p;
351 /* PR 17512; file: 5bc29788. */
352 if (p != sec->contents)
353 memcpy (p, sec->contents, sz);
354 return TRUE;
356 default:
357 abort ();
362 FUNCTION
363 bfd_cache_section_contents
365 SYNOPSIS
366 void bfd_cache_section_contents
367 (asection *sec, void *contents);
369 DESCRIPTION
370 Stash @var(contents) so any following reads of @var(sec) do
371 not need to decompress again.
374 void
375 bfd_cache_section_contents (asection *sec, void *contents)
377 if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
378 sec->compress_status = COMPRESS_SECTION_DONE;
379 sec->contents = contents;
380 sec->flags |= SEC_IN_MEMORY;
384 FUNCTION
385 bfd_is_section_compressed_with_header
387 SYNOPSIS
388 bfd_boolean bfd_is_section_compressed_with_header
389 (bfd *abfd, asection *section,
390 int *compression_header_size_p,
391 bfd_size_type *uncompressed_size_p,
392 unsigned int *uncompressed_alignment_power_p);
394 DESCRIPTION
395 Return @code{TRUE} if @var{section} is compressed. Compression
396 header size is returned in @var{compression_header_size_p},
397 uncompressed size is returned in @var{uncompressed_size_p}
398 and the uncompressed data alignement power is returned in
399 @var{uncompressed_align_pow_p}. If compression is
400 unsupported, compression header size is returned with -1
401 and uncompressed size is returned with 0.
404 bfd_boolean
405 bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
406 int *compression_header_size_p,
407 bfd_size_type *uncompressed_size_p,
408 unsigned int *uncompressed_align_pow_p)
410 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
411 int compression_header_size;
412 int header_size;
413 unsigned int saved = sec->compress_status;
414 bfd_boolean compressed;
416 *uncompressed_align_pow_p = 0;
418 compression_header_size = bfd_get_compression_header_size (abfd, sec);
419 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
420 abort ();
421 header_size = compression_header_size ? compression_header_size : 12;
423 /* Don't decompress the section. */
424 sec->compress_status = COMPRESS_SECTION_NONE;
426 /* Read the header. */
427 if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
429 if (compression_header_size == 0)
430 /* In this case, it should be "ZLIB" followed by the uncompressed
431 section size, 8 bytes in big-endian order. */
432 compressed = CONST_STRNEQ ((char*) header , "ZLIB");
433 else
434 compressed = TRUE;
436 else
437 compressed = FALSE;
439 *uncompressed_size_p = sec->size;
440 if (compressed)
442 if (compression_header_size != 0)
444 if (!bfd_check_compression_header (abfd, header, sec,
445 uncompressed_size_p,
446 uncompressed_align_pow_p))
447 compression_header_size = -1;
449 /* Check for the pathalogical case of a debug string section that
450 contains the string ZLIB.... as the first entry. We assume that
451 no uncompressed .debug_str section would ever be big enough to
452 have the first byte of its (big-endian) size be non-zero. */
453 else if (strcmp (sec->name, ".debug_str") == 0
454 && ISPRINT (header[4]))
455 compressed = FALSE;
456 else
457 *uncompressed_size_p = bfd_getb64 (header + 4);
460 /* Restore compress_status. */
461 sec->compress_status = saved;
462 *compression_header_size_p = compression_header_size;
463 return compressed;
467 FUNCTION
468 bfd_is_section_compressed
470 SYNOPSIS
471 bfd_boolean bfd_is_section_compressed
472 (bfd *abfd, asection *section);
474 DESCRIPTION
475 Return @code{TRUE} if @var{section} is compressed.
478 bfd_boolean
479 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
481 int compression_header_size;
482 bfd_size_type uncompressed_size;
483 unsigned int uncompressed_align_power;
484 return (bfd_is_section_compressed_with_header (abfd, sec,
485 &compression_header_size,
486 &uncompressed_size,
487 &uncompressed_align_power)
488 && compression_header_size >= 0
489 && uncompressed_size > 0);
493 FUNCTION
494 bfd_init_section_decompress_status
496 SYNOPSIS
497 bfd_boolean bfd_init_section_decompress_status
498 (bfd *abfd, asection *section);
500 DESCRIPTION
501 Record compressed section size, update section size with
502 decompressed size and set compress_status to
503 DECOMPRESS_SECTION_SIZED.
505 Return @code{FALSE} if the section is not a valid compressed
506 section. Otherwise, return @code{TRUE}.
509 bfd_boolean
510 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
512 bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
513 int compression_header_size;
514 int header_size;
515 bfd_size_type uncompressed_size;
516 unsigned int uncompressed_alignment_power = 0;
518 compression_header_size = bfd_get_compression_header_size (abfd, sec);
519 if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
520 abort ();
521 header_size = compression_header_size ? compression_header_size : 12;
523 /* Read the header. */
524 if (sec->rawsize != 0
525 || sec->contents != NULL
526 || sec->compress_status != COMPRESS_SECTION_NONE
527 || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
529 bfd_set_error (bfd_error_invalid_operation);
530 return FALSE;
533 if (compression_header_size == 0)
535 /* In this case, it should be "ZLIB" followed by the uncompressed
536 section size, 8 bytes in big-endian order. */
537 if (! CONST_STRNEQ ((char*) header, "ZLIB"))
539 bfd_set_error (bfd_error_wrong_format);
540 return FALSE;
542 uncompressed_size = bfd_getb64 (header + 4);
544 else if (!bfd_check_compression_header (abfd, header, sec,
545 &uncompressed_size,
546 &uncompressed_alignment_power))
548 bfd_set_error (bfd_error_wrong_format);
549 return FALSE;
552 sec->compressed_size = sec->size;
553 sec->size = uncompressed_size;
554 bfd_set_section_alignment (sec, uncompressed_alignment_power);
555 sec->compress_status = DECOMPRESS_SECTION_SIZED;
557 return TRUE;
561 FUNCTION
562 bfd_init_section_compress_status
564 SYNOPSIS
565 bfd_boolean bfd_init_section_compress_status
566 (bfd *abfd, asection *section);
568 DESCRIPTION
569 If open for read, compress section, update section size with
570 compressed size and set compress_status to COMPRESS_SECTION_DONE.
572 Return @code{FALSE} if the section is not a valid compressed
573 section. Otherwise, return @code{TRUE}.
576 bfd_boolean
577 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
579 bfd_size_type uncompressed_size;
580 bfd_byte *uncompressed_buffer;
582 /* Error if not opened for read. */
583 if (abfd->direction != read_direction
584 || sec->size == 0
585 || sec->rawsize != 0
586 || sec->contents != NULL
587 || sec->compress_status != COMPRESS_SECTION_NONE)
589 bfd_set_error (bfd_error_invalid_operation);
590 return FALSE;
593 /* Read in the full section contents and compress it. */
594 uncompressed_size = sec->size;
595 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
596 /* PR 21431 */
597 if (uncompressed_buffer == NULL)
598 return FALSE;
600 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
601 0, uncompressed_size))
602 return FALSE;
604 uncompressed_size = bfd_compress_section_contents (abfd, sec,
605 uncompressed_buffer,
606 uncompressed_size);
607 return uncompressed_size != 0;
611 FUNCTION
612 bfd_compress_section
614 SYNOPSIS
615 bfd_boolean bfd_compress_section
616 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
618 DESCRIPTION
619 If open for write, compress section, update section size with
620 compressed size and set compress_status to COMPRESS_SECTION_DONE.
622 Return @code{FALSE} if compression fail. Otherwise, return
623 @code{TRUE}.
626 bfd_boolean
627 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
629 bfd_size_type uncompressed_size = sec->size;
631 /* Error if not opened for write. */
632 if (abfd->direction != write_direction
633 || uncompressed_size == 0
634 || uncompressed_buffer == NULL
635 || sec->contents != NULL
636 || sec->compressed_size != 0
637 || sec->compress_status != COMPRESS_SECTION_NONE)
639 bfd_set_error (bfd_error_invalid_operation);
640 return FALSE;
643 /* Compress it. */
644 return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
645 uncompressed_size) != 0;