Remove is_zlib_supported
[deliverable/binutils-gdb.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 bfd_putb64 (uncompressed_size, compressed_buffer + 4);
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 }
115
116 return TRUE;
117 }
118
119 /*
120 FUNCTION
121 bfd_get_full_section_contents
122
123 SYNOPSIS
124 bfd_boolean bfd_get_full_section_contents
125 (bfd *abfd, asection *section, bfd_byte **ptr);
126
127 DESCRIPTION
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,
130 return @var{*ptr} with memory malloc'd by this function.
131
132 Return @code{TRUE} if the full section contents is retrieved
133 successfully. If the section has no contents then this function
134 returns @code{TRUE} but @var{*ptr} is set to NULL.
135 */
136
137 bfd_boolean
138 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
139 {
140 bfd_size_type sz;
141 bfd_byte *p = *ptr;
142 bfd_boolean ret;
143 bfd_size_type save_size;
144 bfd_size_type save_rawsize;
145 bfd_byte *compressed_buffer;
146
147 if (abfd->direction != write_direction && sec->rawsize != 0)
148 sz = sec->rawsize;
149 else
150 sz = sec->size;
151 if (sz == 0)
152 {
153 *ptr = NULL;
154 return TRUE;
155 }
156
157 switch (sec->compress_status)
158 {
159 case COMPRESS_SECTION_NONE:
160 if (p == NULL)
161 {
162 p = (bfd_byte *) bfd_malloc (sz);
163 if (p == NULL)
164 return FALSE;
165 }
166
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;
174 return TRUE;
175
176 case DECOMPRESS_SECTION_SIZED:
177 /* Read in the full compressed section contents. */
178 compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
179 if (compressed_buffer == NULL)
180 return FALSE;
181 save_rawsize = sec->rawsize;
182 save_size = sec->size;
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;
187 sec->size = sec->compressed_size;
188 sec->compress_status = COMPRESS_SECTION_NONE;
189 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
190 0, sec->compressed_size);
191 /* Restore rawsize and size. */
192 sec->rawsize = save_rawsize;
193 sec->size = save_size;
194 sec->compress_status = DECOMPRESS_SECTION_SIZED;
195 if (!ret)
196 goto fail_compressed;
197
198 if (p == NULL)
199 p = (bfd_byte *) bfd_malloc (sz);
200 if (p == NULL)
201 goto fail_compressed;
202
203 if (!decompress_contents (compressed_buffer, sec->compressed_size, p, sz))
204 {
205 bfd_set_error (bfd_error_bad_value);
206 if (p != *ptr)
207 free (p);
208 fail_compressed:
209 free (compressed_buffer);
210 return FALSE;
211 }
212
213 free (compressed_buffer);
214 *ptr = p;
215 return TRUE;
216
217 case COMPRESS_SECTION_DONE:
218 if (sec->contents == NULL)
219 return FALSE;
220 if (p == NULL)
221 {
222 p = (bfd_byte *) bfd_malloc (sz);
223 if (p == NULL)
224 return FALSE;
225 *ptr = p;
226 }
227 /* PR 17512; file: 5bc29788. */
228 if (p != sec->contents)
229 memcpy (p, sec->contents, sz);
230 return TRUE;
231
232 default:
233 abort ();
234 }
235 }
236
237 /*
238 FUNCTION
239 bfd_cache_section_contents
240
241 SYNOPSIS
242 void bfd_cache_section_contents
243 (asection *sec, void *contents);
244
245 DESCRIPTION
246 Stash @var(contents) so any following reads of @var(sec) do
247 not need to decompress again.
248 */
249
250 void
251 bfd_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
260 /*
261 FUNCTION
262 bfd_is_section_compressed
263
264 SYNOPSIS
265 bfd_boolean bfd_is_section_compressed
266 (bfd *abfd, asection *section);
267
268 DESCRIPTION
269 Return @code{TRUE} if @var{section} is compressed.
270 */
271
272 bfd_boolean
273 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
274 {
275 bfd_byte compressed_buffer [12];
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;
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. */
284 compressed = (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
285 && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
286
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
296 /* Restore compress_status. */
297 sec->compress_status = saved;
298 return compressed;
299 }
300
301 /*
302 FUNCTION
303 bfd_init_section_decompress_status
304
305 SYNOPSIS
306 bfd_boolean bfd_init_section_decompress_status
307 (bfd *abfd, asection *section);
308
309 DESCRIPTION
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
319 bfd_boolean
320 bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
321 {
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 }
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. */
336 if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
337 {
338 bfd_set_error (bfd_error_wrong_format);
339 return FALSE;
340 }
341
342 uncompressed_size = bfd_getb64 (compressed_buffer + 4);
343 sec->compressed_size = sec->size;
344 sec->size = uncompressed_size;
345 sec->compress_status = DECOMPRESS_SECTION_SIZED;
346
347 return TRUE;
348 }
349
350 /*
351 FUNCTION
352 bfd_init_section_compress_status
353
354 SYNOPSIS
355 bfd_boolean bfd_init_section_compress_status
356 (bfd *abfd, asection *section);
357
358 DESCRIPTION
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
367 bfd_boolean
368 bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
369 {
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)
380 {
381 bfd_set_error (bfd_error_invalid_operation);
382 return FALSE;
383 }
384
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);
395
396 return ret;
397 }
This page took 0.036582 seconds and 4 git commands to generate.