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