Don't write the zlib header if not used
[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 "bfd.h"
23 #include "libbfd.h"
24 #ifdef HAVE_ZLIB_H
25 #include <zlib.h>
26 #endif
27 #include "safe-ctype.h"
28
29 #ifdef HAVE_ZLIB_H
30 static bfd_boolean
31 decompress_contents (bfd_byte *compressed_buffer,
32                      bfd_size_type compressed_size,
33                      bfd_byte *uncompressed_buffer,
34                      bfd_size_type uncompressed_size)
35 {
36   z_stream strm;
37   int rc;
38
39   /* It is possible the section consists of several compressed
40      buffers concatenated together, so we uncompress in a loop.  */
41   strm.zalloc = NULL;
42   strm.zfree = NULL;
43   strm.opaque = NULL;
44   strm.avail_in = compressed_size - 12;
45   strm.next_in = (Bytef*) compressed_buffer + 12;
46   strm.avail_out = uncompressed_size;
47
48   BFD_ASSERT (Z_OK == 0);
49   rc = inflateInit (&strm);
50   while (strm.avail_in > 0 && strm.avail_out > 0)
51     {
52       if (rc != Z_OK)
53         break;
54       strm.next_out = ((Bytef*) uncompressed_buffer
55                        + (uncompressed_size - strm.avail_out));
56       rc = inflate (&strm, Z_FINISH);
57       if (rc != Z_STREAM_END)
58         break;
59       rc = inflateReset (&strm);
60     }
61   rc |= inflateEnd (&strm);
62   return rc == Z_OK && strm.avail_out == 0;
63 }
64
65 /* Compress data of the size specified in @var{uncompressed_size}
66    and pointed to by @var{uncompressed_buffer} using zlib and store
67    as the contents field.  This function assumes the contents
68    field was allocated using bfd_malloc() or equivalent.  If zlib
69    is not installed on this machine, the input is unmodified.
70
71    Return @code{TRUE} if the full section contents is compressed
72    successfully.  */
73
74 static bfd_boolean
75 bfd_compress_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
76                                sec_ptr sec ATTRIBUTE_UNUSED,
77                                bfd_byte *uncompressed_buffer ATTRIBUTE_UNUSED,
78                                bfd_size_type uncompressed_size ATTRIBUTE_UNUSED)
79 {
80   uLong compressed_size;
81   bfd_byte *compressed_buffer;
82
83   compressed_size = compressBound (uncompressed_size) + 12;
84   compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
85
86   if (compressed_buffer == NULL)
87     return FALSE;
88
89   if (compress ((Bytef*) compressed_buffer + 12,
90                 &compressed_size,
91                 (const Bytef*) uncompressed_buffer,
92                 uncompressed_size) != Z_OK)
93     {
94       free (compressed_buffer);
95       bfd_set_error (bfd_error_bad_value);
96       return FALSE;
97     }
98
99   compressed_size += 12;
100
101   /* PR binutils/18087: If compression didn't make the section smaller,
102      just keep it uncompressed.  */
103   if (compressed_size < uncompressed_size)
104     {
105       /* Write the zlib header.  In this case, it should be "ZLIB" followed
106          by the uncompressed section size, 8 bytes in big-endian order.  */
107       memcpy (compressed_buffer, "ZLIB", 4);
108       compressed_buffer[11] = uncompressed_size; uncompressed_size >>= 8;
109       compressed_buffer[10] = uncompressed_size; uncompressed_size >>= 8;
110       compressed_buffer[9] = uncompressed_size; uncompressed_size >>= 8;
111       compressed_buffer[8] = uncompressed_size; uncompressed_size >>= 8;
112       compressed_buffer[7] = uncompressed_size; uncompressed_size >>= 8;
113       compressed_buffer[6] = uncompressed_size; uncompressed_size >>= 8;
114       compressed_buffer[5] = uncompressed_size; uncompressed_size >>= 8;
115       compressed_buffer[4] = uncompressed_size;
116
117       free (uncompressed_buffer);
118       sec->contents = compressed_buffer;
119       sec->size = compressed_size;
120       sec->compress_status = COMPRESS_SECTION_DONE;
121     }
122   else
123     {
124       sec->contents = uncompressed_buffer;
125       sec->compress_status = COMPRESS_SECTION_NONE;
126     }
127
128   return TRUE;
129 }
130 #endif  /* HAVE_ZLIB_H */
131
132 /*
133 FUNCTION
134         bfd_get_full_section_contents
135
136 SYNOPSIS
137         bfd_boolean bfd_get_full_section_contents
138           (bfd *abfd, asection *section, bfd_byte **ptr);
139
140 DESCRIPTION
141         Read all data from @var{section} in BFD @var{abfd}, decompress
142         if needed, and store in @var{*ptr}.  If @var{*ptr} is NULL,
143         return @var{*ptr} with memory malloc'd by this function.
144
145         Return @code{TRUE} if the full section contents is retrieved
146         successfully.  If the section has no contents then this function
147         returns @code{TRUE} but @var{*ptr} is set to NULL.
148 */
149
150 bfd_boolean
151 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
152 {
153   bfd_size_type sz;
154   bfd_byte *p = *ptr;
155 #ifdef HAVE_ZLIB_H
156   bfd_boolean ret;
157   bfd_size_type save_size;
158   bfd_size_type save_rawsize;
159   bfd_byte *compressed_buffer;
160 #endif
161
162   if (abfd->direction != write_direction && sec->rawsize != 0)
163     sz = sec->rawsize;
164   else
165     sz = sec->size;
166   if (sz == 0)
167     {
168       *ptr = NULL;
169       return TRUE;
170     }
171
172   switch (sec->compress_status)
173     {
174     case COMPRESS_SECTION_NONE:
175       if (p == NULL)
176         {
177           p = (bfd_byte *) bfd_malloc (sz);
178           if (p == NULL)
179             return FALSE;
180         }
181
182       if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
183         {
184           if (*ptr != p)
185             free (p);
186           return FALSE;
187         }
188       *ptr = p;
189       return TRUE;
190
191     case DECOMPRESS_SECTION_SIZED:
192 #ifndef HAVE_ZLIB_H
193       bfd_set_error (bfd_error_invalid_operation);
194       return FALSE;
195 #else
196       /* Read in the full compressed section contents.  */
197       compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
198       if (compressed_buffer == NULL)
199         return FALSE;
200       save_rawsize = sec->rawsize;
201       save_size = sec->size;
202       /* Clear rawsize, set size to compressed size and set compress_status
203          to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
204          the uncompressed size, bfd_get_section_contents will fail.  */
205       sec->rawsize = 0;
206       sec->size = sec->compressed_size;
207       sec->compress_status = COMPRESS_SECTION_NONE;
208       ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
209                                       0, sec->compressed_size);
210       /* Restore rawsize and size.  */
211       sec->rawsize = save_rawsize;
212       sec->size = save_size;
213       sec->compress_status = DECOMPRESS_SECTION_SIZED;
214       if (!ret)
215         goto fail_compressed;
216
217       if (p == NULL)
218         p = (bfd_byte *) bfd_malloc (sz);
219       if (p == NULL)
220         goto fail_compressed;
221
222       if (!decompress_contents (compressed_buffer, sec->compressed_size, p, sz))
223         {
224           bfd_set_error (bfd_error_bad_value);
225           if (p != *ptr)
226             free (p);
227         fail_compressed:
228           free (compressed_buffer);
229           return FALSE;
230         }
231
232       free (compressed_buffer);
233       *ptr = p;
234       return TRUE;
235 #endif
236
237     case COMPRESS_SECTION_DONE:
238       if (sec->contents == NULL)
239         return FALSE;
240       if (p == NULL)
241         {
242           p = (bfd_byte *) bfd_malloc (sz);
243           if (p == NULL)
244             return FALSE;
245           *ptr = p;
246         }
247       /* PR 17512; file: 5bc29788.  */
248       if (p != sec->contents)
249         memcpy (p, sec->contents, sz);
250       return TRUE;
251
252     default:
253       abort ();
254     }
255 }
256
257 /*
258 FUNCTION
259         bfd_cache_section_contents
260
261 SYNOPSIS
262         void bfd_cache_section_contents
263           (asection *sec, void *contents);
264
265 DESCRIPTION
266         Stash @var(contents) so any following reads of @var(sec) do
267         not need to decompress again.
268 */
269
270 void
271 bfd_cache_section_contents (asection *sec, void *contents)
272 {
273   if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
274     sec->compress_status = COMPRESS_SECTION_DONE;
275   sec->contents = contents;
276   sec->flags |= SEC_IN_MEMORY;
277 }
278
279
280 /*
281 FUNCTION
282         bfd_is_section_compressed
283
284 SYNOPSIS
285         bfd_boolean bfd_is_section_compressed
286           (bfd *abfd, asection *section);
287
288 DESCRIPTION
289         Return @code{TRUE} if @var{section} is compressed.
290 */
291
292 bfd_boolean
293 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
294 {
295   bfd_byte compressed_buffer [12];
296   unsigned int saved = sec->compress_status;
297   bfd_boolean compressed;
298
299   /* Don't decompress the section.  */
300   sec->compress_status = COMPRESS_SECTION_NONE;
301
302   /* Read the zlib header.  In this case, it should be "ZLIB" followed
303      by the uncompressed section size, 8 bytes in big-endian order.  */
304   compressed = (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
305                 && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
306
307   /* Check for the pathalogical case of a debug string section that
308      contains the string ZLIB.... as the first entry.  We assume that
309      no uncompressed .debug_str section would ever be big enough to
310      have the first byte of its (big-endian) size be non-zero.  */
311   if (compressed
312       && strcmp (sec->name, ".debug_str") == 0
313       && ISPRINT (compressed_buffer[4]))
314     compressed = FALSE;
315
316   /* Restore compress_status.  */
317   sec->compress_status = saved;
318   return compressed;
319 }
320
321 /*
322 FUNCTION
323         bfd_init_section_decompress_status
324
325 SYNOPSIS
326         bfd_boolean bfd_init_section_decompress_status
327           (bfd *abfd, asection *section);
328
329 DESCRIPTION
330         Record compressed section size, update section size with
331         decompressed size and set compress_status to
332         DECOMPRESS_SECTION_SIZED.
333
334         Return @code{FALSE} if the section is not a valid compressed
335         section or zlib is not installed on this machine.  Otherwise,
336         return @code{TRUE}.
337 */
338
339 bfd_boolean
340 bfd_init_section_decompress_status (bfd *abfd ATTRIBUTE_UNUSED,
341                                     sec_ptr sec ATTRIBUTE_UNUSED)
342 {
343 #ifndef HAVE_ZLIB_H
344   bfd_set_error (bfd_error_invalid_operation);
345   return FALSE;
346 #else
347   bfd_byte compressed_buffer [12];
348   bfd_size_type uncompressed_size;
349
350   if (sec->rawsize != 0
351       || sec->contents != NULL
352       || sec->compress_status != COMPRESS_SECTION_NONE
353       || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12))
354     {
355       bfd_set_error (bfd_error_invalid_operation);
356       return FALSE;
357     }
358
359   /* Read the zlib header.  In this case, it should be "ZLIB" followed
360      by the uncompressed section size, 8 bytes in big-endian order.  */
361   if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
362     {
363       bfd_set_error (bfd_error_wrong_format);
364       return FALSE;
365     }
366
367   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
368   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
369   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
370   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
371   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
372   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
373   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
374   uncompressed_size += compressed_buffer[11];
375
376   sec->compressed_size = sec->size;
377   sec->size = uncompressed_size;
378   sec->compress_status = DECOMPRESS_SECTION_SIZED;
379
380   return TRUE;
381 #endif
382 }
383
384 /*
385 FUNCTION
386         bfd_init_section_compress_status
387
388 SYNOPSIS
389         bfd_boolean bfd_init_section_compress_status
390           (bfd *abfd, asection *section);
391
392 DESCRIPTION
393         If open for read, compress section, update section size with
394         compressed size and set compress_status to COMPRESS_SECTION_DONE.
395
396         Return @code{FALSE} if the section is not a valid compressed
397         section or zlib is not installed on this machine.  Otherwise,
398         return @code{TRUE}.
399 */
400
401 bfd_boolean
402 bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED,
403                                   sec_ptr sec ATTRIBUTE_UNUSED)
404 {
405 #ifndef HAVE_ZLIB_H
406   bfd_set_error (bfd_error_invalid_operation);
407   return FALSE;
408 #else
409   bfd_size_type uncompressed_size;
410   bfd_byte *uncompressed_buffer;
411   bfd_boolean ret;
412
413   /* Error if not opened for read.  */
414   if (abfd->direction != read_direction
415       || sec->size == 0
416       || sec->rawsize != 0
417       || sec->contents != NULL
418       || sec->compress_status != COMPRESS_SECTION_NONE)
419     {
420       bfd_set_error (bfd_error_invalid_operation);
421       return FALSE;
422     }
423
424   /* Read in the full section contents and compress it.  */
425   uncompressed_size = sec->size;
426   uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
427   if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
428                                  0, uncompressed_size))
429     ret = FALSE;
430   else
431     ret = bfd_compress_section_contents (abfd, sec,
432                                          uncompressed_buffer,
433                                          uncompressed_size);
434
435   return ret;
436 #endif
437 }