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