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