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