Subversion Repositories Kolibri OS

Rev

Rev 5197 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5197 Rev 6324
1
/* Compressed section support (intended for debug sections).
1
/* Compressed section support (intended for debug sections).
2
   Copyright 2008, 2010, 2011, 2012
-
 
3
   Free Software Foundation, Inc.
2
   Copyright (C) 2008-2015 Free Software Foundation, Inc.
4
 
3
 
5
   This file is part of BFD, the Binary File Descriptor library.
4
   This file is part of BFD, the Binary File Descriptor library.
6
 
5
 
7
   This program is free software; you can redistribute it and/or modify
6
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
7
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
8
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
9
   (at your option) any later version.
11
 
10
 
12
   This program is distributed in the hope that it will be useful,
11
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
14
   GNU General Public License for more details.
16
 
15
 
17
   You should have received a copy of the GNU General Public License
16
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
17
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
19
   MA 02110-1301, USA.  */
21
 
20
 
22
#include "sysdep.h"
21
#include "sysdep.h"
-
 
22
#include 
23
#include "bfd.h"
23
#include "bfd.h"
24
#include "libbfd.h"
24
#include "libbfd.h"
25
#ifdef HAVE_ZLIB_H
-
 
26
#include 
25
#include "safe-ctype.h"
27
#endif
26
 
-
 
27
#define MAX_COMPRESSION_HEADER_SIZE 24
28
 
-
 
29
#ifdef HAVE_ZLIB_H
28
 
30
static bfd_boolean
29
static bfd_boolean
31
decompress_contents (bfd_byte *compressed_buffer,
30
decompress_contents (bfd_byte *compressed_buffer,
32
		     bfd_size_type compressed_size,
31
		     bfd_size_type compressed_size,
33
		     bfd_byte *uncompressed_buffer,
32
		     bfd_byte *uncompressed_buffer,
34
		     bfd_size_type uncompressed_size)
33
		     bfd_size_type uncompressed_size)
35
{
34
{
36
  z_stream strm;
35
  z_stream strm;
37
  int rc;
36
  int rc;
38
 
37
 
39
  /* It is possible the section consists of several compressed
38
  /* It is possible the section consists of several compressed
40
     buffers concatenated together, so we uncompress in a loop.  */
39
     buffers concatenated together, so we uncompress in a loop.  */
-
 
40
  /* PR 18313: The state field in the z_stream structure is supposed
-
 
41
     to be invisible to the user (ie us), but some compilers will
41
  strm.zalloc = NULL;
42
     still complain about it being used without initialisation.  So
-
 
43
     we first zero the entire z_stream structure and then set the fields
42
  strm.zfree = NULL;
44
     that we need.  */
43
  strm.opaque = NULL;
45
  memset (& strm, 0, sizeof strm);
44
  strm.avail_in = compressed_size - 12;
46
  strm.avail_in = compressed_size;
45
  strm.next_in = (Bytef*) compressed_buffer + 12;
47
  strm.next_in = (Bytef*) compressed_buffer;
46
  strm.avail_out = uncompressed_size;
48
  strm.avail_out = uncompressed_size;
47
 
49
 
48
  BFD_ASSERT (Z_OK == 0);
50
  BFD_ASSERT (Z_OK == 0);
49
  rc = inflateInit (&strm);
51
  rc = inflateInit (&strm);
50
  while (strm.avail_in > 0 && strm.avail_out > 0)
52
  while (strm.avail_in > 0 && strm.avail_out > 0)
51
    {
53
    {
52
      if (rc != Z_OK)
54
      if (rc != Z_OK)
53
	break;
55
	break;
54
      strm.next_out = ((Bytef*) uncompressed_buffer
56
      strm.next_out = ((Bytef*) uncompressed_buffer
55
                       + (uncompressed_size - strm.avail_out));
57
                       + (uncompressed_size - strm.avail_out));
56
      rc = inflate (&strm, Z_FINISH);
58
      rc = inflate (&strm, Z_FINISH);
57
      if (rc != Z_STREAM_END)
59
      if (rc != Z_STREAM_END)
58
	break;
60
	break;
59
      rc = inflateReset (&strm);
61
      rc = inflateReset (&strm);
60
    }
62
    }
61
  rc |= inflateEnd (&strm);
63
  rc |= inflateEnd (&strm);
62
  return rc == Z_OK && strm.avail_out == 0;
64
  return rc == Z_OK && strm.avail_out == 0;
63
}
65
}
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
 
66
 
77
	Compress data of the size specified in @var{uncompressed_size}
67
/* Compress data of the size specified in @var{uncompressed_size}
78
	and pointed to by @var{uncompressed_buffer} using zlib and store
68
	and pointed to by @var{uncompressed_buffer} using zlib and store
79
	as the contents field.  This function assumes the contents
69
	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.
70
   field was allocated using bfd_malloc() or equivalent.
82
 
71
 
83
	Return @code{TRUE} if the full section contents is compressed
-
 
84
	successfully.
72
   Return the uncompressed size if the full section contents is
85
*/
73
   compressed successfully.  Otherwise return 0.  */
86
 
-
 
87
bfd_boolean
74
 
88
bfd_compress_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
75
static bfd_size_type
89
			       sec_ptr sec ATTRIBUTE_UNUSED,
76
bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
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);
77
			       bfd_byte *uncompressed_buffer,
95
  return FALSE;
78
			       bfd_size_type uncompressed_size)
-
 
79
{
-
 
80
  uLong compressed_size;
-
 
81
  bfd_byte *buffer;
-
 
82
  bfd_size_type buffer_size;
-
 
83
  bfd_boolean decompress;
-
 
84
  int zlib_size = 0;
-
 
85
  int orig_compression_header_size;
-
 
86
  bfd_size_type orig_uncompressed_size;
-
 
87
  int header_size = bfd_get_compression_header_size (abfd, NULL);
-
 
88
  bfd_boolean compressed
-
 
89
    = bfd_is_section_compressed_with_header (abfd, sec,
-
 
90
					     &orig_compression_header_size,
-
 
91
					     &orig_uncompressed_size);
-
 
92
 
-
 
93
  /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
-
 
94
     overhead in .zdebug* section.  */
-
 
95
  if (!header_size)
96
#else
96
     header_size = 12;
97
  uLong compressed_size;
97
 
-
 
98
  if (compressed)
-
 
99
    {
-
 
100
      /* We shouldn't decompress unsupported compressed section.  */
98
  bfd_byte *compressed_buffer;
101
      if (orig_compression_header_size < 0)
-
 
102
	abort ();
-
 
103
 
-
 
104
      /* Different compression schemes.  Just move the compressed section
-
 
105
	 contents to the right position. */
-
 
106
      if (orig_compression_header_size == 0)
-
 
107
	{
-
 
108
	  /* Convert it from .zdebug* section.  Get the uncompressed
-
 
109
	     size first.  We need to substract the 12-byte overhead in
99
 
110
	     .zdebug* section.  Set orig_compression_header_size to
-
 
111
	     the 12-bye overhead.  */
-
 
112
	  orig_compression_header_size = 12;
-
 
113
	  zlib_size = uncompressed_size - 12;
-
 
114
	}
-
 
115
      else
-
 
116
	{
-
 
117
	  /* Convert it to .zdebug* section.  */
-
 
118
	  zlib_size = uncompressed_size - orig_compression_header_size;
-
 
119
	}
-
 
120
 
-
 
121
      /* Add the header size.  */
-
 
122
      compressed_size = zlib_size + header_size;
-
 
123
    }
-
 
124
  else
-
 
125
    compressed_size = compressBound (uncompressed_size) + header_size;
-
 
126
 
-
 
127
  /* Uncompress if it leads to smaller size.  */
-
 
128
  if (compressed && compressed_size > orig_uncompressed_size)
-
 
129
    {
-
 
130
      decompress = TRUE;
-
 
131
      buffer_size = orig_uncompressed_size;
-
 
132
    }
-
 
133
  else
-
 
134
    {
-
 
135
      decompress = FALSE;
-
 
136
      buffer_size = compressed_size;
-
 
137
    }
-
 
138
  buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
-
 
139
  if (buffer == NULL)
-
 
140
    return 0;
100
  compressed_size = compressBound (uncompressed_size) + 12;
141
 
-
 
142
  if (compressed)
-
 
143
    {
-
 
144
      sec->size = orig_uncompressed_size;
-
 
145
      if (decompress)
-
 
146
	{
-
 
147
	  if (!decompress_contents (uncompressed_buffer
-
 
148
				    + orig_compression_header_size,
-
 
149
				    zlib_size, buffer, buffer_size))
-
 
150
	    {
-
 
151
	      bfd_set_error (bfd_error_bad_value);
-
 
152
	      bfd_release (abfd, buffer);
-
 
153
	      return 0;
-
 
154
	    }
-
 
155
	  free (uncompressed_buffer);
-
 
156
	  sec->contents = buffer;
-
 
157
	  sec->compress_status = COMPRESS_SECTION_DONE;
-
 
158
	  return orig_uncompressed_size;
-
 
159
	}
-
 
160
      else
-
 
161
	{
-
 
162
	  bfd_update_compression_header (abfd, buffer, sec);
-
 
163
	  memmove (buffer + header_size,
-
 
164
		   uncompressed_buffer + orig_compression_header_size,
101
  compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
165
		   zlib_size);
102
 
166
	}
103
  if (compressed_buffer == NULL)
167
    }
104
    return FALSE;
168
  else
105
 
169
    {
106
  if (compress ((Bytef*) compressed_buffer + 12,
170
      if (compress ((Bytef*) buffer + header_size,
107
		&compressed_size,
171
		&compressed_size,
108
		(const Bytef*) uncompressed_buffer,
172
		(const Bytef*) uncompressed_buffer,
109
		uncompressed_size) != Z_OK)
173
		uncompressed_size) != Z_OK)
110
    {
174
    {
111
      free (compressed_buffer);
175
	  bfd_release (abfd, buffer);
112
      bfd_set_error (bfd_error_bad_value);
176
      bfd_set_error (bfd_error_bad_value);
113
      return FALSE;
177
	  return 0;
114
    }
178
    }
115
 
179
 
116
  /* Write the zlib header.  In this case, it should be "ZLIB" followed
180
      compressed_size += header_size;
117
     by the uncompressed section size, 8 bytes in big-endian order.  */
181
      /* PR binutils/18087: If compression didn't make the section smaller,
118
  memcpy (compressed_buffer, "ZLIB", 4);
182
	 just keep it uncompressed.  */
119
  compressed_buffer[11] = uncompressed_size; uncompressed_size >>= 8;
183
      if (compressed_size < uncompressed_size)
-
 
184
	bfd_update_compression_header (abfd, buffer, sec);
-
 
185
      else
120
  compressed_buffer[10] = uncompressed_size; uncompressed_size >>= 8;
186
	{
121
  compressed_buffer[9] = uncompressed_size; uncompressed_size >>= 8;
187
	  /* NOTE: There is a small memory leak here since
122
  compressed_buffer[8] = uncompressed_size; uncompressed_size >>= 8;
188
	     uncompressed_buffer is malloced and won't be freed.  */
123
  compressed_buffer[7] = uncompressed_size; uncompressed_size >>= 8;
189
	  bfd_release (abfd, buffer);
124
  compressed_buffer[6] = uncompressed_size; uncompressed_size >>= 8;
190
	  sec->contents = uncompressed_buffer;
125
  compressed_buffer[5] = uncompressed_size; uncompressed_size >>= 8;
191
	  sec->compress_status = COMPRESS_SECTION_NONE;
-
 
192
	  return uncompressed_size;
126
  compressed_buffer[4] = uncompressed_size;
193
	}
127
  compressed_size += 12;
-
 
128
 
-
 
129
  /* Free the uncompressed contents if we compress in place.  */
194
    }
130
  if (uncompressed_buffer == sec->contents)
-
 
131
    free (uncompressed_buffer);
195
 
132
 
196
    free (uncompressed_buffer);
133
  sec->contents = compressed_buffer;
197
  sec->contents = buffer;
134
  sec->size = compressed_size;
198
  sec->size = compressed_size;
135
  sec->compress_status = COMPRESS_SECTION_DONE;
199
  sec->compress_status = COMPRESS_SECTION_DONE;
136
 
200
 
137
  return TRUE;
-
 
138
#endif  /* HAVE_ZLIB_H */
201
  return uncompressed_size;
139
}
202
}
140
 
203
 
141
/*
204
/*
142
FUNCTION
205
FUNCTION
143
	bfd_get_full_section_contents
206
	bfd_get_full_section_contents
144
 
207
 
145
SYNOPSIS
208
SYNOPSIS
146
	bfd_boolean bfd_get_full_section_contents
209
	bfd_boolean bfd_get_full_section_contents
147
	  (bfd *abfd, asection *section, bfd_byte **ptr);
210
	  (bfd *abfd, asection *section, bfd_byte **ptr);
148
 
211
 
149
DESCRIPTION
212
DESCRIPTION
150
	Read all data from @var{section} in BFD @var{abfd}, decompress
213
	Read all data from @var{section} in BFD @var{abfd}, decompress
151
	if needed, and store in @var{*ptr}.  If @var{*ptr} is NULL,
214
	if needed, and store in @var{*ptr}.  If @var{*ptr} is NULL,
152
	return @var{*ptr} with memory malloc'd by this function.
215
	return @var{*ptr} with memory malloc'd by this function.
153
 
216
 
154
	Return @code{TRUE} if the full section contents is retrieved
217
	Return @code{TRUE} if the full section contents is retrieved
155
	successfully.
218
	successfully.  If the section has no contents then this function
-
 
219
	returns @code{TRUE} but @var{*ptr} is set to NULL.
156
*/
220
*/
157
 
221
 
158
bfd_boolean
222
bfd_boolean
159
bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
223
bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
160
{
224
{
161
  bfd_size_type sz;
225
  bfd_size_type sz;
162
  bfd_byte *p = *ptr;
226
  bfd_byte *p = *ptr;
163
#ifdef HAVE_ZLIB_H
-
 
164
  bfd_boolean ret;
227
  bfd_boolean ret;
165
  bfd_size_type save_size;
228
  bfd_size_type save_size;
166
  bfd_size_type save_rawsize;
229
  bfd_size_type save_rawsize;
167
  bfd_byte *compressed_buffer;
230
  bfd_byte *compressed_buffer;
168
#endif
231
  unsigned int compression_header_size;
169
 
232
 
170
  if (abfd->direction != write_direction && sec->rawsize != 0)
233
  if (abfd->direction != write_direction && sec->rawsize != 0)
171
    sz = sec->rawsize;
234
    sz = sec->rawsize;
172
  else
235
  else
173
    sz = sec->size;
236
    sz = sec->size;
174
  if (sz == 0)
237
  if (sz == 0)
-
 
238
    {
-
 
239
      *ptr = NULL;
175
    return TRUE;
240
    return TRUE;
-
 
241
    }
176
 
242
 
177
  switch (sec->compress_status)
243
  switch (sec->compress_status)
178
    {
244
    {
179
    case COMPRESS_SECTION_NONE:
245
    case COMPRESS_SECTION_NONE:
180
      if (p == NULL)
246
      if (p == NULL)
181
	{
247
	{
182
	  p = (bfd_byte *) bfd_malloc (sz);
248
	  p = (bfd_byte *) bfd_malloc (sz);
183
	  if (p == NULL)
249
	  if (p == NULL)
184
	    return FALSE;
250
	    return FALSE;
185
	}
251
	}
-
 
252
 
186
      if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
253
      if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
187
	{
254
	{
188
	  if (*ptr != p)
255
	  if (*ptr != p)
189
	    free (p);
256
	    free (p);
190
	  return FALSE;
257
	  return FALSE;
191
	}
258
	}
192
      *ptr = p;
259
      *ptr = p;
193
      return TRUE;
260
      return TRUE;
194
 
261
 
195
    case DECOMPRESS_SECTION_SIZED:
262
    case DECOMPRESS_SECTION_SIZED:
196
#ifndef HAVE_ZLIB_H
-
 
197
      bfd_set_error (bfd_error_invalid_operation);
-
 
198
      return FALSE;
-
 
199
#else
-
 
200
      /* Read in the full compressed section contents.  */
263
      /* Read in the full compressed section contents.  */
201
      compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
264
      compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
202
      if (compressed_buffer == NULL)
265
      if (compressed_buffer == NULL)
203
	return FALSE;
266
	return FALSE;
204
      save_rawsize = sec->rawsize;
267
      save_rawsize = sec->rawsize;
205
      save_size = sec->size;
268
      save_size = sec->size;
206
      /* Clear rawsize, set size to compressed size and set compress_status
269
      /* Clear rawsize, set size to compressed size and set compress_status
207
	 to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
270
	 to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
208
	 the uncompressed size, bfd_get_section_contents will fail.  */
271
	 the uncompressed size, bfd_get_section_contents will fail.  */
209
      sec->rawsize = 0;
272
      sec->rawsize = 0;
210
      sec->size = sec->compressed_size;
273
      sec->size = sec->compressed_size;
211
      sec->compress_status = COMPRESS_SECTION_NONE;
274
      sec->compress_status = COMPRESS_SECTION_NONE;
212
      ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
275
      ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
213
				      0, sec->compressed_size);
276
				      0, sec->compressed_size);
214
      /* Restore rawsize and size.  */
277
      /* Restore rawsize and size.  */
215
      sec->rawsize = save_rawsize;
278
      sec->rawsize = save_rawsize;
216
      sec->size = save_size;
279
      sec->size = save_size;
217
      sec->compress_status = DECOMPRESS_SECTION_SIZED;
280
      sec->compress_status = DECOMPRESS_SECTION_SIZED;
218
      if (!ret)
281
      if (!ret)
219
	goto fail_compressed;
282
	goto fail_compressed;
220
 
283
 
221
      if (p == NULL)
284
      if (p == NULL)
222
	p = (bfd_byte *) bfd_malloc (sz);
285
	p = (bfd_byte *) bfd_malloc (sz);
223
      if (p == NULL)
286
      if (p == NULL)
224
	goto fail_compressed;
287
	goto fail_compressed;
-
 
288
 
-
 
289
      compression_header_size = bfd_get_compression_header_size (abfd, sec);
-
 
290
      if (compression_header_size == 0)
-
 
291
	/* Set header size to the zlib header size if it is a
-
 
292
	   SHF_COMPRESSED section.  */
225
 
293
	compression_header_size = 12;
-
 
294
      if (!decompress_contents (compressed_buffer + compression_header_size,
226
      if (!decompress_contents (compressed_buffer, sec->compressed_size, p, sz))
295
				sec->compressed_size, p, sz))
227
	{
296
	{
228
	  bfd_set_error (bfd_error_bad_value);
297
	  bfd_set_error (bfd_error_bad_value);
229
	  if (p != *ptr)
298
	  if (p != *ptr)
230
	    free (p);
299
	    free (p);
231
	fail_compressed:
300
	fail_compressed:
232
	  free (compressed_buffer);
301
	  free (compressed_buffer);
233
	  return FALSE;
302
	  return FALSE;
234
	}
303
	}
235
 
304
 
236
      free (compressed_buffer);
305
      free (compressed_buffer);
237
      *ptr = p;
306
      *ptr = p;
238
      return TRUE;
307
      return TRUE;
239
#endif
-
 
240
 
308
 
-
 
309
    case COMPRESS_SECTION_DONE:
-
 
310
      if (sec->contents == NULL)
241
    case COMPRESS_SECTION_DONE:
311
	return FALSE;
242
      if (p == NULL)
312
      if (p == NULL)
243
	{
313
	{
244
	  p = (bfd_byte *) bfd_malloc (sz);
314
	  p = (bfd_byte *) bfd_malloc (sz);
245
	  if (p == NULL)
315
	  if (p == NULL)
246
	    return FALSE;
316
	    return FALSE;
247
	  *ptr = p;
317
	  *ptr = p;
248
	}
318
	}
-
 
319
      /* PR 17512; file: 5bc29788.  */
-
 
320
      if (p != sec->contents)
249
      memcpy (p, sec->contents, sz);
321
      memcpy (p, sec->contents, sz);
250
      return TRUE;
322
      return TRUE;
251
 
323
 
252
    default:
324
    default:
253
      abort ();
325
      abort ();
254
    }
326
    }
255
}
327
}
256
 
328
 
257
/*
329
/*
258
FUNCTION
330
FUNCTION
259
	bfd_cache_section_contents
331
	bfd_cache_section_contents
260
 
332
 
261
SYNOPSIS
333
SYNOPSIS
262
	void bfd_cache_section_contents
334
	void bfd_cache_section_contents
263
	  (asection *sec, void *contents);
335
	  (asection *sec, void *contents);
264
 
336
 
265
DESCRIPTION
337
DESCRIPTION
266
	Stash @var(contents) so any following reads of @var(sec) do
338
	Stash @var(contents) so any following reads of @var(sec) do
267
	not need to decompress again.
339
	not need to decompress again.
268
*/
340
*/
269
 
341
 
270
void
342
void
271
bfd_cache_section_contents (asection *sec, void *contents)
343
bfd_cache_section_contents (asection *sec, void *contents)
272
{
344
{
273
  if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
345
  if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
274
    sec->compress_status = COMPRESS_SECTION_DONE;
346
    sec->compress_status = COMPRESS_SECTION_DONE;
275
  sec->contents = contents;
347
  sec->contents = contents;
276
  sec->flags |= SEC_IN_MEMORY;
348
  sec->flags |= SEC_IN_MEMORY;
277
}
349
}
278
 
-
 
279
 
350
 
280
/*
351
/*
281
FUNCTION
352
FUNCTION
282
	bfd_is_section_compressed
353
	bfd_is_section_compressed_with_header
283
 
354
 
284
SYNOPSIS
355
SYNOPSIS
-
 
356
	bfd_boolean bfd_is_section_compressed_with_header
-
 
357
	  (bfd *abfd, asection *section,
285
	bfd_boolean bfd_is_section_compressed
358
	  int *compression_header_size_p,
286
	  (bfd *abfd, asection *section);
359
	  bfd_size_type *uncompressed_size_p);
-
 
360
 
-
 
361
DESCRIPTION
-
 
362
	Return @code{TRUE} if @var{section} is compressed.  Compression
-
 
363
	header size is returned in @var{compression_header_size_p} and
287
 
364
	uncompressed size is returned in @var{uncompressed_size_p}.  If
288
DESCRIPTION
365
	compression is unsupported, compression header size is returned
289
	Return @code{TRUE} if @var{section} is compressed.
366
	with -1 and uncompressed size is returned with 0.
290
*/
367
*/
291
 
368
 
292
bfd_boolean
369
bfd_boolean
293
bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
370
bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
-
 
371
				       int *compression_header_size_p,
-
 
372
				       bfd_size_type *uncompressed_size_p)
294
{
373
{
-
 
374
  bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
295
  bfd_byte compressed_buffer [12];
375
  int compression_header_size;
-
 
376
  int header_size;
296
  unsigned int saved = sec->compress_status;
377
  unsigned int saved = sec->compress_status;
297
  bfd_boolean compressed;
378
  bfd_boolean compressed;
-
 
379
 
-
 
380
  compression_header_size = bfd_get_compression_header_size (abfd, sec);
-
 
381
  if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
-
 
382
    abort ();
-
 
383
  header_size = compression_header_size ? compression_header_size : 12;
298
 
384
 
299
  /* Don't decompress the section.  */
385
  /* Don't decompress the section.  */
300
  sec->compress_status = COMPRESS_SECTION_NONE;
386
  sec->compress_status = COMPRESS_SECTION_NONE;
-
 
387
 
-
 
388
  /* Read the header.  */
-
 
389
  if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
-
 
390
    {
301
 
391
      if (compression_header_size == 0)
302
  /* Read the zlib header.  In this case, it should be "ZLIB" followed
392
        /* In this case, it should be "ZLIB" followed by the uncompressed
-
 
393
	   section size, 8 bytes in big-endian order.  */
-
 
394
	compressed = CONST_STRNEQ ((char*) header , "ZLIB");
-
 
395
      else
-
 
396
	compressed = TRUE;
-
 
397
    }
-
 
398
  else
-
 
399
    compressed = FALSE;
-
 
400
 
-
 
401
  *uncompressed_size_p = sec->size;
-
 
402
  if (compressed)
-
 
403
    {
-
 
404
      if (compression_header_size != 0)
-
 
405
	{
-
 
406
	  if (!bfd_check_compression_header (abfd, header, sec,
-
 
407
					     uncompressed_size_p))
-
 
408
	    compression_header_size = -1;
-
 
409
	}
-
 
410
      /* Check for the pathalogical case of a debug string section that
303
     by the uncompressed section size, 8 bytes in big-endian order.  */
411
	 contains the string ZLIB.... as the first entry.  We assume that
-
 
412
	 no uncompressed .debug_str section would ever be big enough to
304
  compressed = (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
413
	 have the first byte of its (big-endian) size be non-zero.  */
-
 
414
      else if (strcmp (sec->name, ".debug_str") == 0
-
 
415
	       && ISPRINT (header[4]))
-
 
416
	compressed = FALSE;
-
 
417
      else
-
 
418
	*uncompressed_size_p = bfd_getb64 (header + 4);
305
		&& CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
419
    }
306
 
420
 
307
  /* Restore compress_status.  */
421
  /* Restore compress_status.  */
308
  sec->compress_status = saved;
422
  sec->compress_status = saved;
-
 
423
  *compression_header_size_p = compression_header_size;
309
  return compressed;
424
  return compressed;
310
}
425
}
311
 
426
 
312
/*
427
/*
313
FUNCTION
428
FUNCTION
-
 
429
	bfd_is_section_compressed
-
 
430
 
-
 
431
SYNOPSIS
-
 
432
	bfd_boolean bfd_is_section_compressed
-
 
433
	  (bfd *abfd, asection *section);
-
 
434
 
-
 
435
DESCRIPTION
-
 
436
	Return @code{TRUE} if @var{section} is compressed.
-
 
437
*/
-
 
438
 
-
 
439
bfd_boolean
-
 
440
bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
-
 
441
{
-
 
442
  int compression_header_size;
-
 
443
  bfd_size_type uncompressed_size;
-
 
444
  return (bfd_is_section_compressed_with_header (abfd, sec,
-
 
445
						 &compression_header_size,
-
 
446
						 &uncompressed_size)
-
 
447
	  && compression_header_size >= 0
-
 
448
	  && uncompressed_size > 0);
-
 
449
}
-
 
450
 
-
 
451
/*
-
 
452
FUNCTION
314
	bfd_init_section_decompress_status
453
	bfd_init_section_decompress_status
315
 
454
 
316
SYNOPSIS
455
SYNOPSIS
317
	bfd_boolean bfd_init_section_decompress_status
456
	bfd_boolean bfd_init_section_decompress_status
318
	  (bfd *abfd, asection *section);
457
	  (bfd *abfd, asection *section);
319
 
458
 
320
DESCRIPTION
459
DESCRIPTION
321
	Record compressed section size, update section size with
460
	Record compressed section size, update section size with
322
	decompressed size and set compress_status to
461
	decompressed size and set compress_status to
323
	DECOMPRESS_SECTION_SIZED.
462
	DECOMPRESS_SECTION_SIZED.
324
 
463
 
325
	Return @code{FALSE} if the section is not a valid compressed
464
	Return @code{FALSE} if the section is not a valid compressed
326
	section or zlib is not installed on this machine.  Otherwise,
-
 
327
	return @code{TRUE}.
465
	section.  Otherwise, return @code{TRUE}.
328
*/
466
*/
329
 
467
 
330
bfd_boolean
468
bfd_boolean
331
bfd_init_section_decompress_status (bfd *abfd ATTRIBUTE_UNUSED,
469
bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
332
				    sec_ptr sec ATTRIBUTE_UNUSED)
-
 
333
{
470
{
334
#ifndef HAVE_ZLIB_H
-
 
335
  bfd_set_error (bfd_error_invalid_operation);
471
  bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
336
  return FALSE;
472
  int compression_header_size;
337
#else
-
 
338
  bfd_byte compressed_buffer [12];
473
  int header_size;
339
  bfd_size_type uncompressed_size;
474
  bfd_size_type uncompressed_size;
-
 
475
 
-
 
476
  compression_header_size = bfd_get_compression_header_size (abfd, sec);
-
 
477
  if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
-
 
478
    abort ();
-
 
479
  header_size = compression_header_size ? compression_header_size : 12;
-
 
480
 
340
 
481
  /* Read the header.  */
341
  if (sec->rawsize != 0
482
  if (sec->rawsize != 0
342
      || sec->contents != NULL
483
      || sec->contents != NULL
343
      || sec->compress_status != COMPRESS_SECTION_NONE
484
      || sec->compress_status != COMPRESS_SECTION_NONE
344
      || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12))
485
      || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
345
    {
486
    {
346
      bfd_set_error (bfd_error_invalid_operation);
487
      bfd_set_error (bfd_error_invalid_operation);
347
      return FALSE;
488
      return FALSE;
348
    }
489
    }
-
 
490
 
-
 
491
  if (compression_header_size == 0)
349
 
492
    {
350
  /* Read the zlib header.  In this case, it should be "ZLIB" followed
493
      /* In this case, it should be "ZLIB" followed by the uncompressed
351
     by the uncompressed section size, 8 bytes in big-endian order.  */
494
	 section size, 8 bytes in big-endian order.  */
-
 
495
      if (! CONST_STRNEQ ((char*) header, "ZLIB"))
-
 
496
    {
-
 
497
      bfd_set_error (bfd_error_wrong_format);
-
 
498
      return FALSE;
-
 
499
    }
-
 
500
      uncompressed_size = bfd_getb64 (header + 4);
-
 
501
    }
-
 
502
  else if (!bfd_check_compression_header (abfd, header, sec,
352
  if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
503
					 &uncompressed_size))
353
    {
504
    {
354
      bfd_set_error (bfd_error_wrong_format);
505
      bfd_set_error (bfd_error_wrong_format);
355
      return FALSE;
506
      return FALSE;
356
    }
507
    }
357
 
-
 
358
  uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
-
 
359
  uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
-
 
360
  uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
-
 
361
  uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
-
 
362
  uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
-
 
363
  uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
-
 
364
  uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
-
 
365
  uncompressed_size += compressed_buffer[11];
-
 
366
 
508
 
367
  sec->compressed_size = sec->size;
509
  sec->compressed_size = sec->size;
368
  sec->size = uncompressed_size;
510
  sec->size = uncompressed_size;
369
  sec->compress_status = DECOMPRESS_SECTION_SIZED;
511
  sec->compress_status = DECOMPRESS_SECTION_SIZED;
370
 
512
 
371
  return TRUE;
513
  return TRUE;
372
#endif
-
 
373
}
514
}
374
 
515
 
375
/*
516
/*
376
FUNCTION
517
FUNCTION
377
	bfd_init_section_compress_status
518
	bfd_init_section_compress_status
378
 
519
 
379
SYNOPSIS
520
SYNOPSIS
380
	bfd_boolean bfd_init_section_compress_status
521
	bfd_boolean bfd_init_section_compress_status
381
	  (bfd *abfd, asection *section);
522
	  (bfd *abfd, asection *section);
382
 
523
 
383
DESCRIPTION
524
DESCRIPTION
384
	If open for read, compress section, update section size with
525
	If open for read, compress section, update section size with
385
	compressed size and set compress_status to COMPRESS_SECTION_DONE.
526
	compressed size and set compress_status to COMPRESS_SECTION_DONE.
386
 
527
 
387
	Return @code{FALSE} if the section is not a valid compressed
528
	Return @code{FALSE} if the section is not a valid compressed
388
	section or zlib is not installed on this machine.  Otherwise,
-
 
389
	return @code{TRUE}.
529
	section.  Otherwise, return @code{TRUE}.
390
*/
530
*/
391
 
531
 
392
bfd_boolean
532
bfd_boolean
393
bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED,
533
bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
394
				  sec_ptr sec ATTRIBUTE_UNUSED)
-
 
395
{
534
{
396
#ifndef HAVE_ZLIB_H
-
 
397
  bfd_set_error (bfd_error_invalid_operation);
-
 
398
  return FALSE;
-
 
399
#else
-
 
400
  bfd_size_type uncompressed_size;
535
  bfd_size_type uncompressed_size;
401
  bfd_byte *uncompressed_buffer;
536
  bfd_byte *uncompressed_buffer;
402
  bfd_boolean ret;
537
  bfd_boolean ret;
403
 
538
 
404
  /* Error if not opened for read.  */
539
  /* Error if not opened for read.  */
405
  if (abfd->direction != read_direction
540
  if (abfd->direction != read_direction
406
      || sec->size == 0
541
      || sec->size == 0
407
      || sec->rawsize != 0
542
      || sec->rawsize != 0
408
      || sec->contents != NULL
543
      || sec->contents != NULL
409
      || sec->compress_status != COMPRESS_SECTION_NONE)
544
      || sec->compress_status != COMPRESS_SECTION_NONE)
410
    {
545
    {
411
      bfd_set_error (bfd_error_invalid_operation);
546
      bfd_set_error (bfd_error_invalid_operation);
412
      return FALSE;
547
      return FALSE;
413
    }
548
    }
414
 
549
 
415
  /* Read in the full section contents and compress it.  */
550
  /* Read in the full section contents and compress it.  */
416
  uncompressed_size = sec->size;
551
  uncompressed_size = sec->size;
417
  uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
552
  uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
418
  if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
553
  if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
419
				 0, uncompressed_size))
554
				 0, uncompressed_size))
420
    ret = FALSE;
555
    ret = FALSE;
421
  else
556
  else
-
 
557
    {
422
    ret = bfd_compress_section_contents (abfd, sec,
558
      uncompressed_size = bfd_compress_section_contents (abfd, sec,
423
					 uncompressed_buffer,
559
					 uncompressed_buffer,
424
					 uncompressed_size);
560
					 uncompressed_size);
-
 
561
      ret = uncompressed_size != 0;
-
 
562
    }
425
 
-
 
426
  free (uncompressed_buffer);
563
 
-
 
564
  return ret;
-
 
565
}
-
 
566
 
-
 
567
/*
-
 
568
FUNCTION
-
 
569
	bfd_compress_section
-
 
570
 
-
 
571
SYNOPSIS
-
 
572
	bfd_boolean bfd_compress_section
-
 
573
	  (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
-
 
574
 
-
 
575
DESCRIPTION
-
 
576
	If open for write, compress section, update section size with
-
 
577
	compressed size and set compress_status to COMPRESS_SECTION_DONE.
-
 
578
 
-
 
579
	Return @code{FALSE} if compression fail.  Otherwise, return
-
 
580
	@code{TRUE}.
-
 
581
*/
-
 
582
 
-
 
583
bfd_boolean
-
 
584
bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
-
 
585
{
-
 
586
  bfd_size_type uncompressed_size = sec->size;
-
 
587
 
-
 
588
  /* Error if not opened for write.  */
-
 
589
  if (abfd->direction != write_direction
-
 
590
      || uncompressed_size == 0
-
 
591
      || uncompressed_buffer == NULL
-
 
592
      || sec->contents != NULL
-
 
593
      || sec->compressed_size != 0
-
 
594
      || sec->compress_status != COMPRESS_SECTION_NONE)
-
 
595
    {
-
 
596
      bfd_set_error (bfd_error_invalid_operation);
427
  return ret;
597
      return FALSE;
-
 
598
    }
-
 
599
 
-
 
600
  /* Compress it.  */
-
 
601
  return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
428
#endif
602
					uncompressed_size) != 0;
429
}
603
}