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