Fix building and testing dwarf debug section compression feature when zlib is not...
[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   /* Write the zlib header.  In this case, it should be "ZLIB" followed
100      by the uncompressed section size, 8 bytes in big-endian order.  */
101   memcpy (compressed_buffer, "ZLIB", 4);
102   compressed_buffer[11] = uncompressed_size; uncompressed_size >>= 8;
103   compressed_buffer[10] = uncompressed_size; uncompressed_size >>= 8;
104   compressed_buffer[9] = uncompressed_size; uncompressed_size >>= 8;
105   compressed_buffer[8] = uncompressed_size; uncompressed_size >>= 8;
106   compressed_buffer[7] = uncompressed_size; uncompressed_size >>= 8;
107   compressed_buffer[6] = uncompressed_size; uncompressed_size >>= 8;
108   compressed_buffer[5] = uncompressed_size; uncompressed_size >>= 8;
109   compressed_buffer[4] = uncompressed_size;
110   compressed_size += 12;
111
112   /* Free the uncompressed contents if we compress in place.  */
113   if (uncompressed_buffer == sec->contents)
114     free (uncompressed_buffer);
115
116   sec->contents = compressed_buffer;
117   sec->size = compressed_size;
118   sec->compress_status = COMPRESS_SECTION_DONE;
119
120   return TRUE;
121 }
122 #endif  /* HAVE_ZLIB_H */
123
124 /*
125 FUNCTION
126         bfd_get_full_section_contents
127
128 SYNOPSIS
129         bfd_boolean bfd_get_full_section_contents
130           (bfd *abfd, asection *section, bfd_byte **ptr);
131
132 DESCRIPTION
133         Read all data from @var{section} in BFD @var{abfd}, decompress
134         if needed, and store in @var{*ptr}.  If @var{*ptr} is NULL,
135         return @var{*ptr} with memory malloc'd by this function.
136
137         Return @code{TRUE} if the full section contents is retrieved
138         successfully.  If the section has no contents then this function
139         returns @code{TRUE} but @var{*ptr} is set to NULL.
140 */
141
142 bfd_boolean
143 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
144 {
145   bfd_size_type sz;
146   bfd_byte *p = *ptr;
147 #ifdef HAVE_ZLIB_H
148   bfd_boolean ret;
149   bfd_size_type save_size;
150   bfd_size_type save_rawsize;
151   bfd_byte *compressed_buffer;
152 #endif
153
154   if (abfd->direction != write_direction && sec->rawsize != 0)
155     sz = sec->rawsize;
156   else
157     sz = sec->size;
158   if (sz == 0)
159     {
160       *ptr = NULL;
161       return TRUE;
162     }
163
164   switch (sec->compress_status)
165     {
166     case COMPRESS_SECTION_NONE:
167       if (p == NULL)
168         {
169           p = (bfd_byte *) bfd_malloc (sz);
170           if (p == NULL)
171             return FALSE;
172         }
173
174       if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
175         {
176           if (*ptr != p)
177             free (p);
178           return FALSE;
179         }
180       *ptr = p;
181       return TRUE;
182
183     case DECOMPRESS_SECTION_SIZED:
184 #ifndef HAVE_ZLIB_H
185       bfd_set_error (bfd_error_invalid_operation);
186       return FALSE;
187 #else
188       /* Read in the full compressed section contents.  */
189       compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
190       if (compressed_buffer == NULL)
191         return FALSE;
192       save_rawsize = sec->rawsize;
193       save_size = sec->size;
194       /* Clear rawsize, set size to compressed size and set compress_status
195          to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
196          the uncompressed size, bfd_get_section_contents will fail.  */
197       sec->rawsize = 0;
198       sec->size = sec->compressed_size;
199       sec->compress_status = COMPRESS_SECTION_NONE;
200       ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
201                                       0, sec->compressed_size);
202       /* Restore rawsize and size.  */
203       sec->rawsize = save_rawsize;
204       sec->size = save_size;
205       sec->compress_status = DECOMPRESS_SECTION_SIZED;
206       if (!ret)
207         goto fail_compressed;
208
209       if (p == NULL)
210         p = (bfd_byte *) bfd_malloc (sz);
211       if (p == NULL)
212         goto fail_compressed;
213
214       if (!decompress_contents (compressed_buffer, sec->compressed_size, p, sz))
215         {
216           bfd_set_error (bfd_error_bad_value);
217           if (p != *ptr)
218             free (p);
219         fail_compressed:
220           free (compressed_buffer);
221           return FALSE;
222         }
223
224       free (compressed_buffer);
225       *ptr = p;
226       return TRUE;
227 #endif
228
229     case COMPRESS_SECTION_DONE:
230       if (sec->contents == NULL)
231         return FALSE;
232       if (p == NULL)
233         {
234           p = (bfd_byte *) bfd_malloc (sz);
235           if (p == NULL)
236             return FALSE;
237           *ptr = p;
238         }
239       /* PR 17512; file: 5bc29788.  */
240       if (p != sec->contents)
241         memcpy (p, sec->contents, sz);
242       return TRUE;
243
244     default:
245       abort ();
246     }
247 }
248
249 /*
250 FUNCTION
251         bfd_cache_section_contents
252
253 SYNOPSIS
254         void bfd_cache_section_contents
255           (asection *sec, void *contents);
256
257 DESCRIPTION
258         Stash @var(contents) so any following reads of @var(sec) do
259         not need to decompress again.
260 */
261
262 void
263 bfd_cache_section_contents (asection *sec, void *contents)
264 {
265   if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
266     sec->compress_status = COMPRESS_SECTION_DONE;
267   sec->contents = contents;
268   sec->flags |= SEC_IN_MEMORY;
269 }
270
271
272 /*
273 FUNCTION
274         bfd_is_section_compressed
275
276 SYNOPSIS
277         bfd_boolean bfd_is_section_compressed
278           (bfd *abfd, asection *section);
279
280 DESCRIPTION
281         Return @code{TRUE} if @var{section} is compressed.
282 */
283
284 bfd_boolean
285 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
286 {
287   bfd_byte compressed_buffer [12];
288   unsigned int saved = sec->compress_status;
289   bfd_boolean compressed;
290
291   /* Don't decompress the section.  */
292   sec->compress_status = COMPRESS_SECTION_NONE;
293
294   /* Read the zlib header.  In this case, it should be "ZLIB" followed
295      by the uncompressed section size, 8 bytes in big-endian order.  */
296   compressed = (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
297                 && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
298
299   /* Check for the pathalogical case of a debug string section that
300      contains the string ZLIB.... as the first entry.  We assume that
301      no uncompressed .debug_str section would ever be big enough to
302      have the first byte of its (big-endian) size be non-zero.  */
303   if (compressed
304       && strcmp (sec->name, ".debug_str") == 0
305       && ISPRINT (compressed_buffer[4]))
306     compressed = FALSE;
307
308   /* Restore compress_status.  */
309   sec->compress_status = saved;
310   return compressed;
311 }
312
313 /*
314 FUNCTION
315         bfd_init_section_decompress_status
316
317 SYNOPSIS
318         bfd_boolean bfd_init_section_decompress_status
319           (bfd *abfd, asection *section);
320
321 DESCRIPTION
322         Record compressed section size, update section size with
323         decompressed size and set compress_status to
324         DECOMPRESS_SECTION_SIZED.
325
326         Return @code{FALSE} if the section is not a valid compressed
327         section or zlib is not installed on this machine.  Otherwise,
328         return @code{TRUE}.
329 */
330
331 bfd_boolean
332 bfd_init_section_decompress_status (bfd *abfd ATTRIBUTE_UNUSED,
333                                     sec_ptr sec ATTRIBUTE_UNUSED)
334 {
335 #ifndef HAVE_ZLIB_H
336   bfd_set_error (bfd_error_invalid_operation);
337   return FALSE;
338 #else
339   bfd_byte compressed_buffer [12];
340   bfd_size_type uncompressed_size;
341
342   if (sec->rawsize != 0
343       || sec->contents != NULL
344       || sec->compress_status != COMPRESS_SECTION_NONE
345       || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12))
346     {
347       bfd_set_error (bfd_error_invalid_operation);
348       return FALSE;
349     }
350
351   /* Read the zlib header.  In this case, it should be "ZLIB" followed
352      by the uncompressed section size, 8 bytes in big-endian order.  */
353   if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
354     {
355       bfd_set_error (bfd_error_wrong_format);
356       return FALSE;
357     }
358
359   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
360   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
361   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
362   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
363   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
364   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
365   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
366   uncompressed_size += compressed_buffer[11];
367
368   sec->compressed_size = sec->size;
369   sec->size = uncompressed_size;
370   sec->compress_status = DECOMPRESS_SECTION_SIZED;
371
372   return TRUE;
373 #endif
374 }
375
376 /*
377 FUNCTION
378         bfd_init_section_compress_status
379
380 SYNOPSIS
381         bfd_boolean bfd_init_section_compress_status
382           (bfd *abfd, asection *section);
383
384 DESCRIPTION
385         If open for read, compress section, update section size with
386         compressed size and set compress_status to COMPRESS_SECTION_DONE.
387
388         Return @code{FALSE} if the section is not a valid compressed
389         section or zlib is not installed on this machine.  Otherwise,
390         return @code{TRUE}.
391 */
392
393 bfd_boolean
394 bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED,
395                                   sec_ptr sec ATTRIBUTE_UNUSED)
396 {
397 #ifndef HAVE_ZLIB_H
398   bfd_set_error (bfd_error_invalid_operation);
399   return FALSE;
400 #else
401   bfd_size_type uncompressed_size;
402   bfd_byte *uncompressed_buffer;
403   bfd_boolean ret;
404
405   /* Error if not opened for read.  */
406   if (abfd->direction != read_direction
407       || sec->size == 0
408       || sec->rawsize != 0
409       || sec->contents != NULL
410       || sec->compress_status != COMPRESS_SECTION_NONE)
411     {
412       bfd_set_error (bfd_error_invalid_operation);
413       return FALSE;
414     }
415
416   /* Read in the full section contents and compress it.  */
417   uncompressed_size = sec->size;
418   uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
419   if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
420                                  0, uncompressed_size))
421     ret = FALSE;
422   else
423     ret = bfd_compress_section_contents (abfd, sec,
424                                          uncompressed_buffer,
425                                          uncompressed_size);
426
427   /* PR binutils/18087: If compression didn't make
428      the section smaller, just keep it uncompressed.  */
429   if (ret && uncompressed_size < sec->size)
430     {
431       free (sec->contents);
432       sec->contents = uncompressed_buffer;
433       sec->size = uncompressed_size;
434       sec->compress_status = COMPRESS_SECTION_NONE;
435     }
436   else
437     free (uncompressed_buffer);
438
439   return ret;
440 #endif
441 }