Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/bfd/compress.c
Line
Count
Source (jump to first uncovered line)
1
/* Compressed section support (intended for debug sections).
2
   Copyright (C) 2008-2024 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
#ifdef HAVE_ZSTD
24
#include <zstd.h>
25
#endif
26
#include "bfd.h"
27
#include "elf-bfd.h"
28
#include "libbfd.h"
29
#include "safe-ctype.h"
30
#include "libiberty.h"
31
32
1.57M
#define MAX_COMPRESSION_HEADER_SIZE 24
33
34
/*
35
EXTERNAL
36
.{* Types of compressed DWARF debug sections.  *}
37
.enum compressed_debug_section_type
38
.{
39
.  COMPRESS_DEBUG_NONE = 0,
40
.  COMPRESS_DEBUG_GNU_ZLIB = 1 << 1,
41
.  COMPRESS_DEBUG_GABI_ZLIB = 1 << 2,
42
.  COMPRESS_DEBUG_ZSTD = 1 << 3,
43
.  COMPRESS_UNKNOWN = 1 << 4
44
.};
45
.
46
.{* Tuple for compressed_debug_section_type and their name.  *}
47
.struct compressed_type_tuple
48
.{
49
.  enum compressed_debug_section_type type;
50
.  const char *name;
51
.};
52
.
53
.{* Compression header ch_type values.  *}
54
.enum compression_type
55
.{
56
.  ch_none = 0,
57
.  ch_compress_zlib = 1 , {* Compressed with zlib.  *}
58
.  ch_compress_zstd = 2   {* Compressed with zstd (www.zstandard.org).  *}
59
.};
60
.
61
.static inline char *
62
.bfd_debug_name_to_zdebug (bfd *abfd, const char *name)
63
.{
64
.  size_t len = strlen (name);
65
.  char *new_name = (char *) bfd_alloc (abfd, len + 2);
66
.  if (new_name == NULL)
67
.    return NULL;
68
.  new_name[0] = '.';
69
.  new_name[1] = 'z';
70
.  memcpy (new_name + 2, name + 1, len);
71
.  return new_name;
72
.}
73
.
74
.static inline char *
75
.bfd_zdebug_name_to_debug (bfd *abfd, const char *name)
76
.{
77
.  size_t len = strlen (name);
78
.  char *new_name = (char *) bfd_alloc (abfd, len);
79
.  if (new_name == NULL)
80
.    return NULL;
81
.  new_name[0] = '.';
82
.  memcpy (new_name + 1, name + 2, len - 1);
83
.  return new_name;
84
.}
85
.
86
*/
87
88
/* Display texts for type of compressed DWARF debug sections.  */
89
static const struct compressed_type_tuple compressed_debug_section_names[] =
90
{
91
  { COMPRESS_DEBUG_NONE, "none" },
92
  { COMPRESS_DEBUG_GABI_ZLIB, "zlib" },
93
  { COMPRESS_DEBUG_GNU_ZLIB, "zlib-gnu" },
94
  { COMPRESS_DEBUG_GABI_ZLIB, "zlib-gabi" },
95
  { COMPRESS_DEBUG_ZSTD, "zstd" },
96
};
97
98
/*
99
FUNCTION
100
  bfd_get_compression_algorithm
101
SYNOPSIS
102
  enum compressed_debug_section_type
103
    bfd_get_compression_algorithm (const char *name);
104
DESCRIPTION
105
  Return compressed_debug_section_type from a string representation.
106
*/
107
enum compressed_debug_section_type
108
bfd_get_compression_algorithm (const char *name)
109
0
{
110
0
  for (unsigned i = 0; i < ARRAY_SIZE (compressed_debug_section_names); ++i)
111
0
    if (strcasecmp (compressed_debug_section_names[i].name, name) == 0)
112
0
      return compressed_debug_section_names[i].type;
113
114
0
  return COMPRESS_UNKNOWN;
115
0
}
116
117
/*
118
FUNCTION
119
  bfd_get_compression_algorithm_name
120
SYNOPSIS
121
  const char *bfd_get_compression_algorithm_name
122
    (enum compressed_debug_section_type type);
123
DESCRIPTION
124
  Return compression algorithm name based on the type.
125
*/
126
const char *
127
bfd_get_compression_algorithm_name (enum compressed_debug_section_type type)
128
0
{
129
0
  for (unsigned i = 0; i < ARRAY_SIZE (compressed_debug_section_names); ++i)
130
0
    if (type == compressed_debug_section_names[i].type)
131
0
      return compressed_debug_section_names[i].name;
132
133
0
  return NULL;
134
0
}
135
136
/*
137
FUNCTION
138
  bfd_update_compression_header
139
140
SYNOPSIS
141
  void bfd_update_compression_header
142
    (bfd *abfd, bfd_byte *contents, asection *sec);
143
144
DESCRIPTION
145
  Set the compression header at CONTENTS of SEC in ABFD and update
146
  elf_section_flags for compression.
147
*/
148
149
void
150
bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
151
             asection *sec)
152
265
{
153
265
  if ((abfd->flags & BFD_COMPRESS) == 0)
154
0
    abort ();
155
156
265
  switch (bfd_get_flavour (abfd))
157
265
    {
158
265
    case bfd_target_elf_flavour:
159
265
      if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
160
265
  {
161
265
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
162
265
    struct bfd_elf_section_data * esd = elf_section_data (sec);
163
265
    enum compression_type ch_type = (abfd->flags & BFD_COMPRESS_ZSTD
164
265
             ? ch_compress_zstd
165
265
             : ch_compress_zlib);
166
167
    /* Set the SHF_COMPRESSED bit.  */
168
265
    elf_section_flags (sec) |= SHF_COMPRESSED;
169
170
265
    if (bed->s->elfclass == ELFCLASS32)
171
173
      {
172
173
        Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
173
173
        bfd_put_32 (abfd, ch_type, &echdr->ch_type);
174
173
        bfd_put_32 (abfd, sec->size, &echdr->ch_size);
175
173
        bfd_put_32 (abfd, 1u << sec->alignment_power,
176
173
        &echdr->ch_addralign);
177
        /* bfd_log2 (alignof (Elf32_Chdr)) */
178
173
        bfd_set_section_alignment (sec, 2);
179
173
        esd->this_hdr.sh_addralign = 4;
180
173
      }
181
92
    else
182
92
      {
183
92
        Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
184
92
        bfd_put_32 (abfd, ch_type, &echdr->ch_type);
185
92
        bfd_put_32 (abfd, 0, &echdr->ch_reserved);
186
92
        bfd_put_64 (abfd, sec->size, &echdr->ch_size);
187
92
        bfd_put_64 (abfd, UINT64_C (1) << sec->alignment_power,
188
92
        &echdr->ch_addralign);
189
        /* bfd_log2 (alignof (Elf64_Chdr)) */
190
92
        bfd_set_section_alignment (sec, 3);
191
92
        esd->this_hdr.sh_addralign = 8;
192
92
      }
193
265
    break;
194
265
  }
195
196
      /* Clear the SHF_COMPRESSED bit.  */
197
0
      elf_section_flags (sec) &= ~SHF_COMPRESSED;
198
      /* Fall through.  */
199
200
0
    default:
201
      /* Write the zlib header.  It should be "ZLIB" followed by
202
   the uncompressed section size, 8 bytes in big-endian
203
   order.  */
204
0
      memcpy (contents, "ZLIB", 4);
205
0
      bfd_putb64 (sec->size, contents + 4);
206
      /* No way to keep the original alignment, just use 1 always. */
207
0
      bfd_set_section_alignment (sec, 0);
208
0
      break;
209
265
    }
210
265
}
211
212
/* Check the compression header at CONTENTS of SEC in ABFD and store the
213
   ch_type in CH_TYPE, uncompressed size in UNCOMPRESSED_SIZE, and the
214
   uncompressed data alignment in UNCOMPRESSED_ALIGNMENT_POWER if the
215
   compression header is valid.  */
216
217
static bool
218
bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
219
            asection *sec,
220
            enum compression_type *ch_type,
221
            bfd_size_type *uncompressed_size,
222
            unsigned int *uncompressed_alignment_power)
223
3.20k
{
224
3.20k
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
225
3.20k
      && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
226
3.20k
    {
227
3.20k
      Elf_Internal_Chdr chdr;
228
3.20k
      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
229
3.20k
      if (bed->s->elfclass == ELFCLASS32)
230
66
  {
231
66
    Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
232
66
    chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
233
66
    chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
234
66
    chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
235
66
  }
236
3.13k
      else
237
3.13k
  {
238
3.13k
    Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
239
3.13k
    chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
240
3.13k
    chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
241
3.13k
    chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
242
3.13k
  }
243
3.20k
      *ch_type = chdr.ch_type;
244
3.20k
      if ((chdr.ch_type == ch_compress_zlib
245
3.20k
     || chdr.ch_type == ch_compress_zstd)
246
3.20k
    && chdr.ch_addralign == (chdr.ch_addralign & -chdr.ch_addralign))
247
342
  {
248
342
    *uncompressed_size = chdr.ch_size;
249
342
    *uncompressed_alignment_power = bfd_log2 (chdr.ch_addralign);
250
342
    return true;
251
342
  }
252
3.20k
    }
253
254
2.86k
  return false;
255
3.20k
}
256
257
/*
258
FUNCTION
259
  bfd_get_compression_header_size
260
261
SYNOPSIS
262
  int bfd_get_compression_header_size (bfd *abfd, asection *sec);
263
264
DESCRIPTION
265
  Return the size of the compression header of SEC in ABFD.
266
*/
267
268
int
269
bfd_get_compression_header_size (bfd *abfd, asection *sec)
270
1.57M
{
271
1.57M
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
272
256k
    {
273
256k
      if (sec == NULL)
274
269
  {
275
269
    if (!(abfd->flags & BFD_COMPRESS_GABI))
276
0
      return 0;
277
269
  }
278
256k
      else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
279
251k
  return 0;
280
281
5.02k
      if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
282
311
  return sizeof (Elf32_External_Chdr);
283
4.71k
      else
284
4.71k
  return sizeof (Elf64_External_Chdr);
285
5.02k
    }
286
287
1.31M
  return 0;
288
1.57M
}
289
290
/*
291
FUNCTION
292
  bfd_convert_section_setup
293
294
SYNOPSIS
295
  bool bfd_convert_section_setup
296
    (bfd *ibfd, asection *isec, bfd *obfd,
297
     const char **new_name, bfd_size_type *new_size);
298
299
DESCRIPTION
300
  Do early setup for objcopy, when copying @var{isec} in input
301
  BFD @var{ibfd} to output BFD @var{obfd}.  Returns the name and
302
  size of the output section.
303
*/
304
305
bool
306
bfd_convert_section_setup (bfd *ibfd, asection *isec, bfd *obfd,
307
         const char **new_name, bfd_size_type *new_size)
308
1.21k
{
309
1.21k
  bfd_size_type hdr_size;
310
311
1.21k
  if ((isec->flags & SEC_DEBUGGING) != 0
312
1.21k
      && (isec->flags & SEC_HAS_CONTENTS) != 0)
313
33
    {
314
33
      const char *name = *new_name;
315
316
33
      if ((obfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)) != 0)
317
33
  {
318
    /* When we decompress or compress with SHF_COMPRESSED,
319
       convert section name from .zdebug_* to .debug_*.  */
320
33
    if (startswith (name, ".zdebug_"))
321
0
      {
322
0
        name = bfd_zdebug_name_to_debug (obfd, name);
323
0
        if (name == NULL)
324
0
    return false;
325
0
      }
326
33
  }
327
328
      /* PR binutils/18087: Compression does not always make a
329
   section smaller.  So only rename the section when
330
   compression has actually taken place.  If input section
331
   name is .zdebug_*, we should never compress it again.  */
332
0
      else if (isec->compress_status == COMPRESS_SECTION_DONE
333
0
         && startswith (name, ".debug_"))
334
0
  {
335
0
    name = bfd_debug_name_to_zdebug (obfd, name);
336
0
    if (name == NULL)
337
0
      return false;
338
0
  }
339
33
      *new_name = name;
340
33
    }
341
1.21k
  *new_size = bfd_section_size (isec);
342
343
  /* Do nothing if either input or output aren't ELF.  */
344
1.21k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
345
1.21k
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
346
35
    return true;
347
348
  /* Do nothing if ELF classes of input and output are the same. */
349
1.18k
  if (get_elf_backend_data (ibfd)->s->elfclass
350
1.18k
      == get_elf_backend_data (obfd)->s->elfclass)
351
1.18k
    return true;
352
353
  /* Convert GNU property size.  */
354
0
  if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
355
0
    {
356
0
      *new_size = _bfd_elf_convert_gnu_property_size (ibfd, obfd);
357
0
      return true;
358
0
    }
359
360
  /* Do nothing if input file will be decompressed.  */
361
0
  if ((ibfd->flags & BFD_DECOMPRESS))
362
0
    return true;
363
364
  /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
365
0
  hdr_size = bfd_get_compression_header_size (ibfd, isec);
366
0
  if (hdr_size == 0)
367
0
    return true;
368
369
  /* Adjust the size of the output SHF_COMPRESSED section.  */
370
0
  if (hdr_size == sizeof (Elf32_External_Chdr))
371
0
    *new_size += sizeof (Elf64_External_Chdr) - sizeof (Elf32_External_Chdr);
372
0
  else
373
0
    *new_size -= sizeof (Elf64_External_Chdr) - sizeof (Elf32_External_Chdr);
374
0
  return true;
375
0
}
376
377
/*
378
FUNCTION
379
  bfd_convert_section_contents
380
381
SYNOPSIS
382
  bool bfd_convert_section_contents
383
    (bfd *ibfd, asection *isec, bfd *obfd,
384
     bfd_byte **ptr, bfd_size_type *ptr_size);
385
386
DESCRIPTION
387
  Convert the contents, stored in @var{*ptr}, of the section
388
  @var{isec} in input BFD @var{ibfd} to output BFD @var{obfd}
389
  if needed.  The original buffer pointed to by @var{*ptr} may
390
  be freed and @var{*ptr} is returned with memory malloc'd by this
391
  function, and the new size written to @var{ptr_size}.
392
*/
393
394
bool
395
bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd,
396
            bfd_byte **ptr, bfd_size_type *ptr_size)
397
938
{
398
938
  bfd_byte *contents;
399
938
  bfd_size_type ihdr_size, ohdr_size, size;
400
938
  Elf_Internal_Chdr chdr;
401
938
  bool use_memmove;
402
403
  /* Do nothing if either input or output aren't ELF.  */
404
938
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
405
938
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
406
26
    return true;
407
408
  /* Do nothing if ELF classes of input and output are the same.  */
409
912
  if (get_elf_backend_data (ibfd)->s->elfclass
410
912
      == get_elf_backend_data (obfd)->s->elfclass)
411
912
    return true;
412
413
  /* Convert GNU properties.  */
414
0
  if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
415
0
    return _bfd_elf_convert_gnu_properties (ibfd, isec, obfd, ptr,
416
0
              ptr_size);
417
418
  /* Do nothing if input file will be decompressed.  */
419
0
  if ((ibfd->flags & BFD_DECOMPRESS))
420
0
    return true;
421
422
  /* Do nothing if the input section isn't a SHF_COMPRESSED section.  */
423
0
  ihdr_size = bfd_get_compression_header_size (ibfd, isec);
424
0
  if (ihdr_size == 0)
425
0
    return true;
426
427
  /* PR 25221.  Check for corrupt input sections.  */
428
0
  if (ihdr_size > bfd_get_section_limit (ibfd, isec))
429
    /* FIXME: Issue a warning about a corrupt
430
       compression header size field ?  */
431
0
    return false;
432
433
0
  contents = *ptr;
434
435
  /* Convert the contents of the input SHF_COMPRESSED section to
436
     output.  Get the input compression header and the size of the
437
     output compression header.  */
438
0
  if (ihdr_size == sizeof (Elf32_External_Chdr))
439
0
    {
440
0
      Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
441
0
      chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
442
0
      chdr.ch_size = bfd_get_32 (ibfd, &echdr->ch_size);
443
0
      chdr.ch_addralign = bfd_get_32 (ibfd, &echdr->ch_addralign);
444
445
0
      ohdr_size = sizeof (Elf64_External_Chdr);
446
447
0
      use_memmove = false;
448
0
    }
449
0
  else if (ihdr_size != sizeof (Elf64_External_Chdr))
450
0
    {
451
      /* FIXME: Issue a warning about a corrupt
452
   compression header size field ?  */
453
0
      return false;
454
0
    }
455
0
  else
456
0
    {
457
0
      Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
458
0
      chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
459
0
      chdr.ch_size = bfd_get_64 (ibfd, &echdr->ch_size);
460
0
      chdr.ch_addralign = bfd_get_64 (ibfd, &echdr->ch_addralign);
461
462
0
      ohdr_size = sizeof (Elf32_External_Chdr);
463
0
      use_memmove = true;
464
0
    }
465
466
0
  size = bfd_section_size (isec) - ihdr_size + ohdr_size;
467
0
  if (!use_memmove)
468
0
    {
469
0
      contents = (bfd_byte *) bfd_malloc (size);
470
0
      if (contents == NULL)
471
0
  return false;
472
0
    }
473
474
  /* Write out the output compression header.  */
475
0
  if (ohdr_size == sizeof (Elf32_External_Chdr))
476
0
    {
477
0
      Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
478
0
      bfd_put_32 (obfd, chdr.ch_type, &echdr->ch_type);
479
0
      bfd_put_32 (obfd, chdr.ch_size, &echdr->ch_size);
480
0
      bfd_put_32 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
481
0
    }
482
0
  else
483
0
    {
484
0
      Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
485
0
      bfd_put_32 (obfd, chdr.ch_type, &echdr->ch_type);
486
0
      bfd_put_32 (obfd, 0, &echdr->ch_reserved);
487
0
      bfd_put_64 (obfd, chdr.ch_size, &echdr->ch_size);
488
0
      bfd_put_64 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
489
0
    }
490
491
  /* Copy the compressed contents.  */
492
0
  if (use_memmove)
493
0
    memmove (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
494
0
  else
495
0
    {
496
0
      memcpy (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
497
0
      free (*ptr);
498
0
      *ptr = contents;
499
0
    }
500
501
0
  *ptr_size = size;
502
0
  return true;
503
0
}
504
505
static bool
506
decompress_contents (bool is_zstd, bfd_byte *compressed_buffer,
507
         bfd_size_type compressed_size,
508
         bfd_byte *uncompressed_buffer,
509
         bfd_size_type uncompressed_size)
510
0
{
511
0
  if (is_zstd)
512
0
    {
513
#ifdef HAVE_ZSTD
514
      size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
515
            compressed_buffer, compressed_size);
516
      return !ZSTD_isError (ret);
517
#endif
518
0
    }
519
520
0
  z_stream strm;
521
0
  int rc;
522
523
  /* It is possible the section consists of several compressed
524
     buffers concatenated together, so we uncompress in a loop.  */
525
  /* PR 18313: The state field in the z_stream structure is supposed
526
     to be invisible to the user (ie us), but some compilers will
527
     still complain about it being used without initialisation.  So
528
     we first zero the entire z_stream structure and then set the fields
529
     that we need.  */
530
0
  memset (& strm, 0, sizeof strm);
531
0
  strm.avail_in = compressed_size;
532
0
  strm.next_in = (Bytef*) compressed_buffer;
533
0
  strm.avail_out = uncompressed_size;
534
  /* FIXME: strm.avail_in and strm.avail_out are typically unsigned
535
     int.  Supporting sizes that don't fit in an unsigned int is
536
     possible but will require some rewriting of this function.  */
537
0
  if (strm.avail_in != compressed_size || strm.avail_out != uncompressed_size)
538
0
    return false;
539
540
0
  BFD_ASSERT (Z_OK == 0);
541
0
  rc = inflateInit (&strm);
542
0
  while (strm.avail_in > 0 && strm.avail_out > 0)
543
0
    {
544
0
      if (rc != Z_OK)
545
0
  break;
546
0
      strm.next_out = ((Bytef*) uncompressed_buffer
547
0
           + (uncompressed_size - strm.avail_out));
548
0
      rc = inflate (&strm, Z_FINISH);
549
0
      if (rc != Z_STREAM_END)
550
0
  break;
551
0
      rc = inflateReset (&strm);
552
0
    }
553
0
  return inflateEnd (&strm) == Z_OK && rc == Z_OK && strm.avail_out == 0;
554
0
}
555
556
/* Compress section contents using zlib/zstd and store
557
   as the contents field.  This function assumes the contents
558
   field was allocated using bfd_malloc() or equivalent.
559
560
   Return the uncompressed size if the full section contents is
561
   compressed successfully.  Otherwise return (bfd_size_type) -1.  */
562
563
static bfd_size_type
564
bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
565
269
{
566
269
  bfd_byte *input_buffer;
567
269
  uLong compressed_size;
568
269
  bfd_byte *buffer;
569
269
  bfd_size_type buffer_size;
570
269
  int zlib_size = 0;
571
269
  int orig_header_size;
572
269
  bfd_size_type uncompressed_size;
573
269
  unsigned int uncompressed_alignment_pow;
574
269
  enum compression_type ch_type = ch_none;
575
269
  int new_header_size = bfd_get_compression_header_size (abfd, NULL);
576
269
  bool compressed
577
269
    = bfd_is_section_compressed_info (abfd, sec,
578
269
              &orig_header_size,
579
269
              &uncompressed_size,
580
269
              &uncompressed_alignment_pow,
581
269
              &ch_type);
582
269
  bool update = false;
583
584
  /* We shouldn't be trying to decompress unsupported compressed sections.  */
585
269
  if (compressed && orig_header_size < 0)
586
0
    abort ();
587
588
  /* PR 31455: Check for a corrupt uncompressed size.  */
589
269
  if (uncompressed_size == (bfd_size_type) -1)
590
0
    return uncompressed_size;
591
592
  /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
593
     overhead in .zdebug* section.  */
594
269
  if (!new_header_size)
595
0
    new_header_size = 12;
596
269
  if (ch_type == ch_none)
597
269
    orig_header_size = 12;
598
599
269
  input_buffer = sec->contents;
600
269
  if (compressed)
601
0
    {
602
0
      zlib_size = sec->size - orig_header_size;
603
0
      compressed_size = zlib_size + new_header_size;
604
605
      /* If we are converting between zlib-gnu and zlib-gabi then the
606
   compressed contents just need to be moved.  */
607
0
      update = (ch_type < ch_compress_zstd
608
0
    && (abfd->flags & BFD_COMPRESS_ZSTD) == 0);
609
610
      /* Uncompress when not just moving contents or when compressed
611
   is not smaller than uncompressed.  */
612
0
      if (!update || compressed_size >= uncompressed_size)
613
0
  {
614
0
    buffer_size = uncompressed_size;
615
0
    buffer = bfd_malloc (buffer_size);
616
0
    if (buffer == NULL)
617
0
      return (bfd_size_type) -1;
618
619
0
    if (!decompress_contents (ch_type == ch_compress_zstd,
620
0
            input_buffer + orig_header_size,
621
0
            zlib_size, buffer, buffer_size))
622
0
      {
623
0
        bfd_set_error (bfd_error_bad_value);
624
0
        free (buffer);
625
0
        return (bfd_size_type) -1;
626
0
      }
627
0
    free (input_buffer);
628
0
    bfd_set_section_alignment (sec, uncompressed_alignment_pow);
629
0
    sec->contents = buffer;
630
0
    sec->flags |= SEC_IN_MEMORY;
631
0
    sec->compress_status = COMPRESS_SECTION_NONE;
632
0
    sec->size = uncompressed_size;
633
0
    input_buffer = buffer;
634
0
  }
635
0
    }
636
637
269
  if (!update)
638
269
    compressed_size = compressBound (uncompressed_size) + new_header_size;
639
640
269
  buffer_size = compressed_size;
641
269
  buffer = bfd_alloc (abfd, buffer_size);
642
269
  if (buffer == NULL)
643
0
    return (bfd_size_type) -1;
644
645
269
  if (update)
646
0
    {
647
0
      if (compressed_size < uncompressed_size)
648
0
  memcpy (buffer + new_header_size,
649
0
    input_buffer + orig_header_size,
650
0
    zlib_size);
651
0
    }
652
269
  else
653
269
    {
654
269
      if (abfd->flags & BFD_COMPRESS_ZSTD)
655
0
  {
656
#if HAVE_ZSTD
657
    compressed_size = ZSTD_compress (buffer + new_header_size,
658
             compressed_size,
659
             input_buffer,
660
             uncompressed_size,
661
             ZSTD_CLEVEL_DEFAULT);
662
    if (ZSTD_isError (compressed_size))
663
      {
664
        bfd_release (abfd, buffer);
665
        bfd_set_error (bfd_error_bad_value);
666
        return (bfd_size_type) -1;
667
      }
668
#endif
669
0
  }
670
269
      else if (compress ((Bytef *) buffer + new_header_size, &compressed_size,
671
269
       (const Bytef *) input_buffer, uncompressed_size)
672
269
         != Z_OK)
673
0
  {
674
0
    bfd_release (abfd, buffer);
675
0
    bfd_set_error (bfd_error_bad_value);
676
0
    return (bfd_size_type) -1;
677
0
  }
678
679
269
      compressed_size += new_header_size;
680
269
    }
681
682
  /* If compression didn't make the section smaller, keep it uncompressed.  */
683
269
  if (compressed_size >= uncompressed_size)
684
4
    {
685
4
      memcpy (buffer, input_buffer, uncompressed_size);
686
4
      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
687
4
  elf_section_flags (sec) &= ~SHF_COMPRESSED;
688
4
      sec->compress_status = COMPRESS_SECTION_NONE;
689
4
    }
690
265
  else
691
265
    {
692
265
      sec->size = uncompressed_size;
693
265
      bfd_update_compression_header (abfd, buffer, sec);
694
265
      sec->size = compressed_size;
695
265
      sec->compress_status = COMPRESS_SECTION_DONE;
696
265
    }
697
269
  sec->contents = buffer;
698
269
  sec->flags |= SEC_IN_MEMORY;
699
269
  free (input_buffer);
700
269
  return uncompressed_size;
701
269
}
702
703
/*
704
FUNCTION
705
  bfd_get_full_section_contents
706
707
SYNOPSIS
708
  bool bfd_get_full_section_contents
709
    (bfd *abfd, asection *section, bfd_byte **ptr);
710
711
DESCRIPTION
712
  Read all data from @var{section} in BFD @var{abfd}, decompress
713
  if needed, and store in @var{*ptr}.  If @var{*ptr} is NULL,
714
  return @var{*ptr} with memory malloc'd by this function.
715
716
  Return @code{TRUE} if the full section contents is retrieved
717
  successfully.  If the section has no contents then this function
718
  returns @code{TRUE} but @var{*ptr} is set to NULL.
719
*/
720
721
bool
722
bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
723
1.23M
{
724
1.23M
  bfd_size_type readsz = bfd_get_section_limit_octets (abfd, sec);
725
1.23M
  bfd_size_type allocsz = bfd_get_section_alloc_size (abfd, sec);
726
1.23M
  bfd_byte *p = *ptr;
727
1.23M
  bool ret;
728
1.23M
  bfd_size_type save_size;
729
1.23M
  bfd_size_type save_rawsize;
730
1.23M
  bfd_byte *compressed_buffer;
731
1.23M
  unsigned int compression_header_size;
732
1.23M
  const unsigned int compress_status = sec->compress_status;
733
734
1.23M
  if (allocsz == 0)
735
888
    {
736
888
      *ptr = NULL;
737
888
      return true;
738
888
    }
739
740
1.23M
  if (p == NULL
741
1.23M
      && compress_status != COMPRESS_SECTION_DONE
742
1.23M
      && bfd_section_size_insane (abfd, sec))
743
1.03M
    {
744
      /* PR 24708: Avoid attempts to allocate a ridiculous amount
745
   of memory.  */
746
1.03M
      _bfd_error_handler
747
  /* xgettext:c-format */
748
1.03M
  (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
749
1.03M
   abfd, sec, (uint64_t) readsz);
750
1.03M
      return false;
751
1.03M
    }
752
753
197k
  switch (compress_status)
754
197k
    {
755
197k
    case COMPRESS_SECTION_NONE:
756
197k
      if (p == NULL && !sec->mmapped_p)
757
191k
  {
758
191k
    p = (bfd_byte *) bfd_malloc (allocsz);
759
191k
    if (p == NULL)
760
0
      {
761
        /* PR 20801: Provide a more helpful error message.  */
762
0
        if (bfd_get_error () == bfd_error_no_memory)
763
0
    _bfd_error_handler
764
      /* xgettext:c-format */
765
0
      (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
766
0
      abfd, sec, (uint64_t) allocsz);
767
0
        return false;
768
0
      }
769
191k
  }
770
771
197k
      if (!bfd_get_section_contents (abfd, sec, p, 0, readsz))
772
1.61k
  {
773
1.61k
    if (*ptr != p)
774
1.61k
      free (p);
775
1.61k
    return false;
776
1.61k
  }
777
196k
      *ptr = p;
778
196k
      return true;
779
780
0
    case DECOMPRESS_SECTION_ZLIB:
781
0
    case DECOMPRESS_SECTION_ZSTD:
782
      /* Read in the full compressed section contents.  */
783
0
      compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
784
0
      if (compressed_buffer == NULL)
785
0
  return false;
786
0
      save_rawsize = sec->rawsize;
787
0
      save_size = sec->size;
788
      /* Clear rawsize, set size to compressed size and set compress_status
789
   to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
790
   the uncompressed size, bfd_get_section_contents will fail.  */
791
0
      sec->rawsize = 0;
792
0
      sec->size = sec->compressed_size;
793
0
      sec->compress_status = COMPRESS_SECTION_NONE;
794
0
      ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
795
0
              0, sec->compressed_size);
796
      /* Restore rawsize and size.  */
797
0
      sec->rawsize = save_rawsize;
798
0
      sec->size = save_size;
799
0
      sec->compress_status = compress_status;
800
0
      if (!ret)
801
0
  goto fail_compressed;
802
803
0
      if (p == NULL)
804
0
  p = (bfd_byte *) bfd_malloc (allocsz);
805
0
      if (p == NULL)
806
0
  goto fail_compressed;
807
808
0
      compression_header_size = bfd_get_compression_header_size (abfd, sec);
809
0
      if (compression_header_size == 0)
810
  /* Set header size to the zlib header size if it is a
811
     SHF_COMPRESSED section.  */
812
0
  compression_header_size = 12;
813
0
      bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
814
0
      if (!decompress_contents (
815
0
        is_zstd, compressed_buffer + compression_header_size,
816
0
        sec->compressed_size - compression_header_size, p, readsz))
817
0
  {
818
0
    bfd_set_error (bfd_error_bad_value);
819
0
    if (p != *ptr)
820
0
      free (p);
821
0
  fail_compressed:
822
0
    free (compressed_buffer);
823
0
    return false;
824
0
  }
825
826
0
      free (compressed_buffer);
827
0
      *ptr = p;
828
0
      return true;
829
830
33
    case COMPRESS_SECTION_DONE:
831
33
      if (sec->contents == NULL)
832
0
  return false;
833
33
      if (p == NULL)
834
33
  {
835
33
    p = (bfd_byte *) bfd_malloc (allocsz);
836
33
    if (p == NULL)
837
0
      return false;
838
33
    *ptr = p;
839
33
  }
840
      /* PR 17512; file: 5bc29788.  */
841
33
      if (p != sec->contents)
842
33
  memcpy (p, sec->contents, readsz);
843
33
      return true;
844
845
0
    default:
846
0
      abort ();
847
197k
    }
848
197k
}
849
850
/*
851
FUNCTION
852
  bfd_is_section_compressed_info
853
854
SYNOPSIS
855
  bool bfd_is_section_compressed_info
856
    (bfd *abfd, asection *section,
857
     int *compression_header_size_p,
858
     bfd_size_type *uncompressed_size_p,
859
     unsigned int *uncompressed_alignment_power_p,
860
     enum compression_type *ch_type);
861
862
DESCRIPTION
863
  Return @code{TRUE} if @var{section} is compressed.  Compression
864
  header size is returned in @var{compression_header_size_p},
865
  uncompressed size is returned in @var{uncompressed_size_p}
866
  and the uncompressed data alignement power is returned in
867
  @var{uncompressed_align_pow_p}.  If compression is
868
  unsupported, compression header size is returned with -1
869
  and uncompressed size is returned with 0.
870
*/
871
872
bool
873
bfd_is_section_compressed_info (bfd *abfd, sec_ptr sec,
874
        int *compression_header_size_p,
875
        bfd_size_type *uncompressed_size_p,
876
        unsigned int *uncompressed_align_pow_p,
877
        enum compression_type *ch_type)
878
1.57M
{
879
1.57M
  bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
880
1.57M
  int compression_header_size;
881
1.57M
  int header_size;
882
1.57M
  unsigned int saved = sec->compress_status;
883
1.57M
  bool compressed;
884
885
1.57M
  *uncompressed_align_pow_p = 0;
886
887
1.57M
  compression_header_size = bfd_get_compression_header_size (abfd, sec);
888
1.57M
  if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
889
0
    abort ();
890
1.57M
  header_size = compression_header_size ? compression_header_size : 12;
891
892
  /* Don't decompress the section.  */
893
1.57M
  sec->compress_status = COMPRESS_SECTION_NONE;
894
895
  /* Read the header.  */
896
1.57M
  if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
897
372k
    {
898
372k
      if (compression_header_size == 0)
899
  /* In this case, it should be "ZLIB" followed by the uncompressed
900
     section size, 8 bytes in big-endian order.  */
901
368k
  compressed = startswith ((char*) header , "ZLIB");
902
3.15k
      else
903
3.15k
  compressed = true;
904
372k
    }
905
1.19M
  else
906
1.19M
    compressed = false;
907
908
1.57M
  *uncompressed_size_p = sec->size;
909
1.57M
  if (compressed)
910
3.68k
    {
911
3.68k
      if (compression_header_size != 0)
912
3.15k
  {
913
3.15k
    if (!bfd_check_compression_header (abfd, header, sec, ch_type,
914
3.15k
               uncompressed_size_p,
915
3.15k
               uncompressed_align_pow_p))
916
2.82k
      compression_header_size = -1;
917
3.15k
  }
918
      /* Check for the pathalogical case of a debug string section that
919
   contains the string ZLIB.... as the first entry.  We assume that
920
   no uncompressed .debug_str section would ever be big enough to
921
   have the first byte of its (big-endian) size be non-zero.  */
922
533
      else if (strcmp (sec->name, ".debug_str") == 0
923
533
         && ISPRINT (header[4]))
924
0
  compressed = false;
925
533
      else
926
533
  *uncompressed_size_p = bfd_getb64 (header + 4);
927
3.68k
    }
928
929
  /* Restore compress_status.  */
930
1.57M
  sec->compress_status = saved;
931
1.57M
  *compression_header_size_p = compression_header_size;
932
1.57M
  return compressed;
933
1.57M
}
934
935
/*
936
FUNCTION
937
  bfd_is_section_compressed
938
939
SYNOPSIS
940
  bool bfd_is_section_compressed
941
    (bfd *abfd, asection *section);
942
943
DESCRIPTION
944
  Return @code{TRUE} if @var{section} is compressed.
945
*/
946
947
bool
948
bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
949
1.54M
{
950
1.54M
  int compression_header_size;
951
1.54M
  bfd_size_type uncompressed_size;
952
1.54M
  unsigned int uncompressed_align_power;
953
1.54M
  enum compression_type ch_type;
954
1.54M
  return (bfd_is_section_compressed_info (abfd, sec,
955
1.54M
            &compression_header_size,
956
1.54M
            &uncompressed_size,
957
1.54M
            &uncompressed_align_power,
958
1.54M
            &ch_type)
959
1.54M
    && compression_header_size >= 0
960
1.54M
    && uncompressed_size > 0);
961
1.54M
}
962
963
/*
964
FUNCTION
965
  bfd_init_section_decompress_status
966
967
SYNOPSIS
968
  bool bfd_init_section_decompress_status
969
    (bfd *abfd, asection *section);
970
971
DESCRIPTION
972
  Record compressed section size, update section size with
973
  decompressed size and set compress_status to
974
  DECOMPRESS_SECTION_{ZLIB,ZSTD}.
975
976
  Return @code{FALSE} if the section is not a valid compressed
977
  section.  Otherwise, return @code{TRUE}.
978
*/
979
980
bool
981
bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
982
68
{
983
68
  bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
984
68
  int compression_header_size;
985
68
  int header_size;
986
68
  bfd_size_type uncompressed_size;
987
68
  unsigned int uncompressed_alignment_power = 0;
988
68
  enum compression_type ch_type;
989
68
  z_stream strm;
990
991
68
  compression_header_size = bfd_get_compression_header_size (abfd, sec);
992
68
  if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
993
0
    abort ();
994
68
  header_size = compression_header_size ? compression_header_size : 12;
995
996
  /* Read the header.  */
997
68
  if (sec->rawsize != 0
998
68
      || sec->contents != NULL
999
68
      || sec->compress_status != COMPRESS_SECTION_NONE
1000
68
      || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
1001
0
    {
1002
0
      bfd_set_error (bfd_error_invalid_operation);
1003
0
      return false;
1004
0
    }
1005
1006
68
  if (compression_header_size == 0)
1007
21
    {
1008
      /* In this case, it should be "ZLIB" followed by the uncompressed
1009
   section size, 8 bytes in big-endian order.  */
1010
21
      if (! startswith ((char*) header, "ZLIB"))
1011
0
  {
1012
0
    bfd_set_error (bfd_error_wrong_format);
1013
0
    return false;
1014
0
  }
1015
21
      uncompressed_size = bfd_getb64 (header + 4);
1016
21
      ch_type = ch_none;
1017
21
    }
1018
47
  else if (!bfd_check_compression_header (abfd, header, sec,
1019
47
            &ch_type,
1020
47
            &uncompressed_size,
1021
47
            &uncompressed_alignment_power))
1022
39
    {
1023
39
      bfd_set_error (bfd_error_wrong_format);
1024
39
      return false;
1025
39
    }
1026
1027
  /* PR28530, reject sizes unsupported by decompress_contents.  */
1028
29
  strm.avail_in = sec->size;
1029
29
  strm.avail_out = uncompressed_size;
1030
29
  if (strm.avail_in != sec->size || strm.avail_out != uncompressed_size)
1031
23
    {
1032
23
      bfd_set_error (bfd_error_nonrepresentable_section);
1033
23
      return false;
1034
23
    }
1035
1036
6
  sec->compressed_size = sec->size;
1037
6
  sec->size = uncompressed_size;
1038
6
  bfd_set_section_alignment (sec, uncompressed_alignment_power);
1039
6
  sec->compress_status = (ch_type == ch_compress_zstd
1040
6
        ? DECOMPRESS_SECTION_ZSTD : DECOMPRESS_SECTION_ZLIB);
1041
1042
6
  return true;
1043
29
}
1044
1045
/*
1046
FUNCTION
1047
  bfd_init_section_compress_status
1048
1049
SYNOPSIS
1050
  bool bfd_init_section_compress_status
1051
    (bfd *abfd, asection *section);
1052
1053
DESCRIPTION
1054
  If open for read, compress section, update section size with
1055
  compressed size and set compress_status to COMPRESS_SECTION_DONE.
1056
1057
  Return @code{FALSE} if the section is not a valid compressed
1058
  section.  Otherwise, return @code{TRUE}.
1059
*/
1060
1061
bool
1062
bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
1063
269
{
1064
269
  bfd_size_type uncompressed_size;
1065
269
  bfd_byte *uncompressed_buffer;
1066
1067
  /* Error if not opened for read.  */
1068
269
  if (abfd->direction != read_direction
1069
269
      || sec->size == 0
1070
269
      || sec->rawsize != 0
1071
269
      || sec->contents != NULL
1072
269
      || sec->compress_status != COMPRESS_SECTION_NONE
1073
269
      || bfd_section_size_insane (abfd, sec))
1074
0
    {
1075
0
      bfd_set_error (bfd_error_invalid_operation);
1076
0
      return false;
1077
0
    }
1078
1079
  /* Read in the full section contents and compress it.  */
1080
269
  uncompressed_size = sec->size;
1081
269
  uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
1082
  /* PR 21431 */
1083
269
  if (uncompressed_buffer == NULL)
1084
0
    return false;
1085
1086
269
  if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
1087
269
         0, uncompressed_size))
1088
0
    {
1089
0
      free (uncompressed_buffer);
1090
0
      return false;
1091
0
    }
1092
1093
269
  sec->contents = uncompressed_buffer;
1094
269
  if (bfd_compress_section_contents (abfd, sec) == (bfd_size_type) -1)
1095
0
    {
1096
0
      free (sec->contents);
1097
0
      sec->contents = NULL;
1098
0
      return false;
1099
0
    }
1100
269
  return true;
1101
269
}
1102
1103
/*
1104
FUNCTION
1105
  bfd_compress_section
1106
1107
SYNOPSIS
1108
  bool bfd_compress_section
1109
    (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
1110
1111
DESCRIPTION
1112
  If open for write, compress section, update section size with
1113
  compressed size and set compress_status to COMPRESS_SECTION_DONE.
1114
1115
  Return @code{FALSE} if compression fail.  Otherwise, return
1116
  @code{TRUE}.  UNCOMPRESSED_BUFFER is freed in both cases.
1117
*/
1118
1119
bool
1120
bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
1121
0
{
1122
0
  bfd_size_type uncompressed_size = sec->size;
1123
1124
  /* Error if not opened for write.  */
1125
0
  if (abfd->direction != write_direction
1126
0
      || uncompressed_size == 0
1127
0
      || uncompressed_buffer == NULL
1128
0
      || sec->contents != NULL
1129
0
      || sec->compressed_size != 0
1130
0
      || sec->compress_status != COMPRESS_SECTION_NONE)
1131
0
    {
1132
0
      bfd_set_error (bfd_error_invalid_operation);
1133
0
      return false;
1134
0
    }
1135
1136
0
  sec->contents = uncompressed_buffer;
1137
0
  if (bfd_compress_section_contents (abfd, sec) == (bfd_size_type) -1)
1138
0
    {
1139
0
      free (sec->contents);
1140
0
      sec->contents = NULL;
1141
0
      return false;
1142
0
    }
1143
0
  return true;
1144
0
}