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