Delay setting up compressed debug section names
[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_alloc (abfd, 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               bfd_release (abfd, 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           bfd_release (abfd, 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           /* 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           p = (bfd_byte *) bfd_malloc (sz);
254           if (p == NULL)
255             return FALSE;
256         }
257
258       if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
259         {
260           if (*ptr != p)
261             free (p);
262           return FALSE;
263         }
264       *ptr = p;
265       return TRUE;
266
267     case DECOMPRESS_SECTION_SIZED:
268       /* Read in the full compressed section contents.  */
269       compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
270       if (compressed_buffer == NULL)
271         return FALSE;
272       save_rawsize = sec->rawsize;
273       save_size = sec->size;
274       /* Clear rawsize, set size to compressed size and set compress_status
275          to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
276          the uncompressed size, bfd_get_section_contents will fail.  */
277       sec->rawsize = 0;
278       sec->size = sec->compressed_size;
279       sec->compress_status = COMPRESS_SECTION_NONE;
280       ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
281                                       0, sec->compressed_size);
282       /* Restore rawsize and size.  */
283       sec->rawsize = save_rawsize;
284       sec->size = save_size;
285       sec->compress_status = DECOMPRESS_SECTION_SIZED;
286       if (!ret)
287         goto fail_compressed;
288
289       if (p == NULL)
290         p = (bfd_byte *) bfd_malloc (sz);
291       if (p == NULL)
292         goto fail_compressed;
293
294       compression_header_size = bfd_get_compression_header_size (abfd, sec);
295       if (!decompress_contents (compressed_buffer + compression_header_size,
296                                 sec->compressed_size, p, sz))
297         {
298           bfd_set_error (bfd_error_bad_value);
299           if (p != *ptr)
300             free (p);
301         fail_compressed:
302           free (compressed_buffer);
303           return FALSE;
304         }
305
306       free (compressed_buffer);
307       *ptr = p;
308       return TRUE;
309
310     case COMPRESS_SECTION_DONE:
311       if (sec->contents == NULL)
312         return FALSE;
313       if (p == NULL)
314         {
315           p = (bfd_byte *) bfd_malloc (sz);
316           if (p == NULL)
317             return FALSE;
318           *ptr = p;
319         }
320       /* PR 17512; file: 5bc29788.  */
321       if (p != sec->contents)
322         memcpy (p, sec->contents, sz);
323       return TRUE;
324
325     default:
326       abort ();
327     }
328 }
329
330 /*
331 FUNCTION
332         bfd_cache_section_contents
333
334 SYNOPSIS
335         void bfd_cache_section_contents
336           (asection *sec, void *contents);
337
338 DESCRIPTION
339         Stash @var(contents) so any following reads of @var(sec) do
340         not need to decompress again.
341 */
342
343 void
344 bfd_cache_section_contents (asection *sec, void *contents)
345 {
346   if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
347     sec->compress_status = COMPRESS_SECTION_DONE;
348   sec->contents = contents;
349   sec->flags |= SEC_IN_MEMORY;
350 }
351
352 /*
353 FUNCTION
354         bfd_is_section_compressed_with_header
355
356 SYNOPSIS
357         bfd_boolean bfd_is_section_compressed_with_header
358           (bfd *abfd, asection *section,
359           int *compression_header_size_p);
360
361 DESCRIPTION
362         Return @code{TRUE} if @var{section} is compressed.  Compression
363         header size is returned in @var{compression_header_size_p}.  If
364         compression is unsupported, compression header size is returned
365         with -1.
366 */
367
368 bfd_boolean
369 bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
370                                        int *compression_header_size_p)
371 {
372   bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
373   int compression_header_size;
374   int header_size = 12;
375   unsigned int saved = sec->compress_status;
376   bfd_boolean compressed;
377
378   compression_header_size = bfd_get_compression_header_size (abfd, sec);
379   if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
380     abort ();
381   header_size += compression_header_size;
382
383   /* Don't decompress the section.  */
384   sec->compress_status = COMPRESS_SECTION_NONE;
385
386   /* Read the zlib header.  In this case, it should be "ZLIB" followed
387      by the uncompressed section size, 8 bytes in big-endian order.  */
388   compressed = bfd_get_section_contents (abfd, sec, header, 0,
389                                          header_size)
390                 && CONST_STRNEQ ((char*) header + compression_header_size,
391                                  "ZLIB");
392
393   if (compressed)
394     {
395       if (compression_header_size != 0)
396         {
397           bfd_size_type uncompressed_size
398             = bfd_getb64 ((bfd_byte *) header
399                           + compression_header_size + 4);
400           if (!bfd_check_compression_header (abfd, header, sec,
401                                              uncompressed_size))
402             compression_header_size = -1;
403         }
404       /* Check for the pathalogical case of a debug string section that
405          contains the string ZLIB.... as the first entry.  We assume that
406          no uncompressed .debug_str section would ever be big enough to
407          have the first byte of its (big-endian) size be non-zero.  */
408       else if (strcmp (sec->name, ".debug_str") == 0
409                && ISPRINT (header[compression_header_size + 4]))
410         compressed = FALSE;
411     }
412
413   /* Restore compress_status.  */
414   sec->compress_status = saved;
415   *compression_header_size_p = compression_header_size;
416   return compressed;
417 }
418
419 /*
420 FUNCTION
421         bfd_is_section_compressed
422
423 SYNOPSIS
424         bfd_boolean bfd_is_section_compressed
425           (bfd *abfd, asection *section);
426
427 DESCRIPTION
428         Return @code{TRUE} if @var{section} is compressed.
429 */
430
431 bfd_boolean
432 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
433 {
434   int compression_header_size;
435   return (bfd_is_section_compressed_with_header (abfd, sec,
436                                                  &compression_header_size)
437           && compression_header_size >= 0);
438 }
439
440 /*
441 FUNCTION
442         bfd_init_section_decompress_status
443
444 SYNOPSIS
445         bfd_boolean bfd_init_section_decompress_status
446           (bfd *abfd, asection *section);
447
448 DESCRIPTION
449         Record compressed section size, update section size with
450         decompressed size and set compress_status to
451         DECOMPRESS_SECTION_SIZED.
452
453         Return @code{FALSE} if the section is not a valid compressed
454         section.  Otherwise, return @code{TRUE}.
455 */
456
457 bfd_boolean
458 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
459 {
460   bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
461   int compression_header_size;
462   int header_size = 12;
463   bfd_size_type uncompressed_size;
464
465   compression_header_size = bfd_get_compression_header_size (abfd, sec);
466   if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
467     abort ();
468   header_size += compression_header_size;
469
470   if (sec->rawsize != 0
471       || sec->contents != NULL
472       || sec->compress_status != COMPRESS_SECTION_NONE
473       || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
474     {
475       bfd_set_error (bfd_error_invalid_operation);
476       return FALSE;
477     }
478
479   /* Read the zlib header.  In this case, it should be "ZLIB" followed
480      by the uncompressed section size, 8 bytes in big-endian order.  */
481   if (! CONST_STRNEQ ((char*) header + compression_header_size, "ZLIB"))
482     {
483       bfd_set_error (bfd_error_wrong_format);
484       return FALSE;
485     }
486
487   uncompressed_size = bfd_getb64 (header + compression_header_size + 4);
488   if (compression_header_size != 0
489       && !bfd_check_compression_header (abfd, header, sec,
490                                         uncompressed_size))
491     {
492       bfd_set_error (bfd_error_wrong_format);
493       return FALSE;
494     }
495   sec->compressed_size = sec->size;
496   sec->size = uncompressed_size;
497   sec->compress_status = DECOMPRESS_SECTION_SIZED;
498
499   return TRUE;
500 }
501
502 /*
503 FUNCTION
504         bfd_init_section_compress_status
505
506 SYNOPSIS
507         bfd_boolean bfd_init_section_compress_status
508           (bfd *abfd, asection *section);
509
510 DESCRIPTION
511         If open for read, compress section, update section size with
512         compressed size and set compress_status to COMPRESS_SECTION_DONE.
513
514         Return @code{FALSE} if the section is not a valid compressed
515         section.  Otherwise, return @code{TRUE}.
516 */
517
518 bfd_boolean
519 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
520 {
521   bfd_size_type uncompressed_size;
522   bfd_byte *uncompressed_buffer;
523   bfd_boolean ret;
524
525   /* Error if not opened for read.  */
526   if (abfd->direction != read_direction
527       || sec->size == 0
528       || sec->rawsize != 0
529       || sec->contents != NULL
530       || sec->compress_status != COMPRESS_SECTION_NONE)
531     {
532       bfd_set_error (bfd_error_invalid_operation);
533       return FALSE;
534     }
535
536   /* Read in the full section contents and compress it.  */
537   uncompressed_size = sec->size;
538   uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
539   if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
540                                  0, uncompressed_size))
541     ret = FALSE;
542   else
543     {
544       uncompressed_size = bfd_compress_section_contents (abfd, sec,
545                                                          uncompressed_buffer,
546                                                          uncompressed_size);
547       ret = uncompressed_size != 0;
548     }
549
550   return ret;
551 }
552
553 /*
554 FUNCTION
555         bfd_compress_section
556
557 SYNOPSIS
558         bfd_boolean bfd_compress_section
559           (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
560
561 DESCRIPTION
562         If open for write, compress section, update section size with
563         compressed size and set compress_status to COMPRESS_SECTION_DONE.
564
565         Return @code{FALSE} if compression fail.  Otherwise, return
566         @code{TRUE}.
567 */
568
569 bfd_boolean
570 bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
571 {
572   bfd_size_type uncompressed_size = sec->size;
573
574   /* Error if not opened for write.  */
575   if (abfd->direction != write_direction
576       || uncompressed_size == 0
577       || uncompressed_buffer == NULL
578       || sec->contents != NULL
579       || sec->compressed_size != 0
580       || sec->compress_status != COMPRESS_SECTION_NONE)
581     {
582       bfd_set_error (bfd_error_invalid_operation);
583       return FALSE;
584     }
585
586   /* Compress it.  */
587   return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
588                                         uncompressed_size) != 0;
589 }