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