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