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