Remove is_zlib_supported
[deliverable/binutils-gdb.git] / bfd / compress.c
CommitLineData
0acf065b 1/* Compressed section support (intended for debug sections).
b90efa5b 2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
1b315056
CS
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
1b315056 21#include "sysdep.h"
243340ad 22#include <zlib.h>
1b315056
CS
23#include "bfd.h"
24#include "libbfd.h"
a953eec9 25#include "safe-ctype.h"
1b315056 26
4a114e3e
L
27static bfd_boolean
28decompress_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
a253d456 45 BFD_ASSERT (Z_OK == 0);
4a114e3e 46 rc = inflateInit (&strm);
a29a8af8 47 while (strm.avail_in > 0 && strm.avail_out > 0)
4a114e3e
L
48 {
49 if (rc != Z_OK)
a253d456 50 break;
4a114e3e
L
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)
a253d456 55 break;
4a114e3e
L
56 rc = inflateReset (&strm);
57 }
a253d456 58 rc |= inflateEnd (&strm);
82c6068a 59 return rc == Z_OK && strm.avail_out == 0;
4a114e3e 60}
4a114e3e 61
0b0732e1
L
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.
1b315056 67
0b0732e1
L
68 Return @code{TRUE} if the full section contents is compressed
69 successfully. */
1b315056 70
0b0732e1 71static bfd_boolean
243340ad
L
72bfd_compress_section_contents (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
73 bfd_byte *uncompressed_buffer,
74 bfd_size_type uncompressed_size)
1b315056 75{
ae6a0217 76 uLong compressed_size;
4a114e3e
L
77 bfd_byte *compressed_buffer;
78
79 compressed_size = compressBound (uncompressed_size) + 12;
80 compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
81
4281caad
MS
82 if (compressed_buffer == NULL)
83 return FALSE;
84
4a114e3e
L
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
4a114e3e
L
95 compressed_size += 12;
96
8d001214
L
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);
4aa90cc0 104 bfd_putb64 (uncompressed_size, compressed_buffer + 4);
8d001214
L
105 free (uncompressed_buffer);
106 sec->contents = compressed_buffer;
107 sec->size = compressed_size;
108 sec->compress_status = COMPRESS_SECTION_DONE;
109 }
110 else
111 {
112 sec->contents = uncompressed_buffer;
113 sec->compress_status = COMPRESS_SECTION_NONE;
114 }
4a114e3e
L
115
116 return TRUE;
4a114e3e
L
117}
118
119/*
120FUNCTION
121 bfd_get_full_section_contents
122
123SYNOPSIS
124 bfd_boolean bfd_get_full_section_contents
125 (bfd *abfd, asection *section, bfd_byte **ptr);
126
127DESCRIPTION
128 Read all data from @var{section} in BFD @var{abfd}, decompress
129 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
68ffbac6 130 return @var{*ptr} with memory malloc'd by this function.
4a114e3e
L
131
132 Return @code{TRUE} if the full section contents is retrieved
06614111
NC
133 successfully. If the section has no contents then this function
134 returns @code{TRUE} but @var{*ptr} is set to NULL.
4a114e3e
L
135*/
136
137bfd_boolean
138bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
139{
e57278ef 140 bfd_size_type sz;
4a114e3e 141 bfd_byte *p = *ptr;
82c6068a 142 bfd_boolean ret;
38b774d2
AM
143 bfd_size_type save_size;
144 bfd_size_type save_rawsize;
4a114e3e 145 bfd_byte *compressed_buffer;
4a114e3e 146
e57278ef
AM
147 if (abfd->direction != write_direction && sec->rawsize != 0)
148 sz = sec->rawsize;
149 else
150 sz = sec->size;
4a114e3e 151 if (sz == 0)
06614111
NC
152 {
153 *ptr = NULL;
154 return TRUE;
155 }
4a114e3e
L
156
157 switch (sec->compress_status)
158 {
159 case COMPRESS_SECTION_NONE:
160 if (p == NULL)
161 {
0d13c96b 162 p = (bfd_byte *) bfd_malloc (sz);
4a114e3e
L
163 if (p == NULL)
164 return FALSE;
4a114e3e 165 }
06614111 166
82c6068a
AM
167 if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
168 {
169 if (*ptr != p)
170 free (p);
171 return FALSE;
172 }
173 *ptr = p;
4a114e3e
L
174 return TRUE;
175
176 case DECOMPRESS_SECTION_SIZED:
82c6068a 177 /* Read in the full compressed section contents. */
38b774d2 178 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
82c6068a
AM
179 if (compressed_buffer == NULL)
180 return FALSE;
38b774d2
AM
181 save_rawsize = sec->rawsize;
182 save_size = sec->size;
82c6068a
AM
183 /* Clear rawsize, set size to compressed size and set compress_status
184 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
185 the uncompressed size, bfd_get_section_contents will fail. */
186 sec->rawsize = 0;
38b774d2 187 sec->size = sec->compressed_size;
82c6068a
AM
188 sec->compress_status = COMPRESS_SECTION_NONE;
189 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
38b774d2 190 0, sec->compressed_size);
82c6068a 191 /* Restore rawsize and size. */
38b774d2
AM
192 sec->rawsize = save_rawsize;
193 sec->size = save_size;
4a114e3e 194 sec->compress_status = DECOMPRESS_SECTION_SIZED;
82c6068a
AM
195 if (!ret)
196 goto fail_compressed;
4a114e3e 197
38b774d2
AM
198 if (p == NULL)
199 p = (bfd_byte *) bfd_malloc (sz);
200 if (p == NULL)
4a114e3e 201 goto fail_compressed;
4a114e3e 202
38b774d2 203 if (!decompress_contents (compressed_buffer, sec->compressed_size, p, sz))
82c6068a
AM
204 {
205 bfd_set_error (bfd_error_bad_value);
38b774d2
AM
206 if (p != *ptr)
207 free (p);
82c6068a
AM
208 fail_compressed:
209 free (compressed_buffer);
210 return FALSE;
211 }
4a114e3e 212
82c6068a 213 free (compressed_buffer);
38b774d2
AM
214 *ptr = p;
215 return TRUE;
4a114e3e 216
82c6068a 217 case COMPRESS_SECTION_DONE:
db6b071a
NC
218 if (sec->contents == NULL)
219 return FALSE;
82c6068a
AM
220 if (p == NULL)
221 {
222 p = (bfd_byte *) bfd_malloc (sz);
223 if (p == NULL)
224 return FALSE;
225 *ptr = p;
226 }
06614111
NC
227 /* PR 17512; file: 5bc29788. */
228 if (p != sec->contents)
229 memcpy (p, sec->contents, sz);
82c6068a 230 return TRUE;
4a114e3e 231
82c6068a
AM
232 default:
233 abort ();
234 }
4a114e3e
L
235}
236
8a72cc6e
AM
237/*
238FUNCTION
239 bfd_cache_section_contents
240
241SYNOPSIS
242 void bfd_cache_section_contents
243 (asection *sec, void *contents);
244
245DESCRIPTION
246 Stash @var(contents) so any following reads of @var(sec) do
247 not need to decompress again.
248*/
249
250void
251bfd_cache_section_contents (asection *sec, void *contents)
252{
253 if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
254 sec->compress_status = COMPRESS_SECTION_DONE;
255 sec->contents = contents;
256 sec->flags |= SEC_IN_MEMORY;
257}
258
259
4a114e3e
L
260/*
261FUNCTION
262 bfd_is_section_compressed
263
264SYNOPSIS
265 bfd_boolean bfd_is_section_compressed
266 (bfd *abfd, asection *section);
267
268DESCRIPTION
269 Return @code{TRUE} if @var{section} is compressed.
270*/
271
272bfd_boolean
273bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
274{
275 bfd_byte compressed_buffer [12];
64f40162
L
276 unsigned int saved = sec->compress_status;
277 bfd_boolean compressed;
278
279 /* Don't decompress the section. */
280 sec->compress_status = COMPRESS_SECTION_NONE;
4a114e3e
L
281
282 /* Read the zlib header. In this case, it should be "ZLIB" followed
283 by the uncompressed section size, 8 bytes in big-endian order. */
64f40162
L
284 compressed = (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
285 && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
286
a953eec9
NC
287 /* Check for the pathalogical case of a debug string section that
288 contains the string ZLIB.... as the first entry. We assume that
289 no uncompressed .debug_str section would ever be big enough to
290 have the first byte of its (big-endian) size be non-zero. */
291 if (compressed
292 && strcmp (sec->name, ".debug_str") == 0
293 && ISPRINT (compressed_buffer[4]))
294 compressed = FALSE;
295
64f40162
L
296 /* Restore compress_status. */
297 sec->compress_status = saved;
298 return compressed;
4a114e3e
L
299}
300
301/*
302FUNCTION
303 bfd_init_section_decompress_status
304
305SYNOPSIS
306 bfd_boolean bfd_init_section_decompress_status
307 (bfd *abfd, asection *section);
308
309DESCRIPTION
310 Record compressed section size, update section size with
311 decompressed size and set compress_status to
312 DECOMPRESS_SECTION_SIZED.
313
314 Return @code{FALSE} if the section is not a valid compressed
315 section or zlib is not installed on this machine. Otherwise,
316 return @code{TRUE}.
317*/
318
319bfd_boolean
243340ad 320bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
4a114e3e 321{
4a114e3e
L
322 bfd_byte compressed_buffer [12];
323 bfd_size_type uncompressed_size;
324
325 if (sec->rawsize != 0
326 || sec->contents != NULL
327 || sec->compress_status != COMPRESS_SECTION_NONE
328 || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12))
329 {
330 bfd_set_error (bfd_error_invalid_operation);
331 return FALSE;
332 }
1b315056
CS
333
334 /* Read the zlib header. In this case, it should be "ZLIB" followed
335 by the uncompressed section size, 8 bytes in big-endian order. */
4a114e3e
L
336 if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
337 {
338 bfd_set_error (bfd_error_wrong_format);
339 return FALSE;
340 }
341
4aa90cc0 342 uncompressed_size = bfd_getb64 (compressed_buffer + 4);
4a114e3e
L
343 sec->compressed_size = sec->size;
344 sec->size = uncompressed_size;
345 sec->compress_status = DECOMPRESS_SECTION_SIZED;
1b315056 346
4a114e3e 347 return TRUE;
4a114e3e
L
348}
349
350/*
351FUNCTION
352 bfd_init_section_compress_status
353
354SYNOPSIS
355 bfd_boolean bfd_init_section_compress_status
356 (bfd *abfd, asection *section);
357
358DESCRIPTION
359 If open for read, compress section, update section size with
360 compressed size and set compress_status to COMPRESS_SECTION_DONE.
361
362 Return @code{FALSE} if the section is not a valid compressed
363 section or zlib is not installed on this machine. Otherwise,
364 return @code{TRUE}.
365*/
366
367bfd_boolean
243340ad 368bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
4a114e3e 369{
4a114e3e
L
370 bfd_size_type uncompressed_size;
371 bfd_byte *uncompressed_buffer;
372 bfd_boolean ret;
373
374 /* Error if not opened for read. */
375 if (abfd->direction != read_direction
376 || sec->size == 0
377 || sec->rawsize != 0
378 || sec->contents != NULL
379 || sec->compress_status != COMPRESS_SECTION_NONE)
1b315056 380 {
4a114e3e
L
381 bfd_set_error (bfd_error_invalid_operation);
382 return FALSE;
1b315056 383 }
1b315056 384
4a114e3e
L
385 /* Read in the full section contents and compress it. */
386 uncompressed_size = sec->size;
387 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
388 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
389 0, uncompressed_size))
390 ret = FALSE;
391 else
392 ret = bfd_compress_section_contents (abfd, sec,
393 uncompressed_buffer,
394 uncompressed_size);
1b315056 395
4a114e3e 396 return ret;
1b315056 397}
This page took 0.328163 seconds and 4 git commands to generate.