Coverage Report

Created: 2023-08-28 06:31

/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-2023 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
62.3k
#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
2.73k
{
153
2.73k
  if ((abfd->flags & BFD_COMPRESS) == 0)
154
0
    abort ();
155
156
2.73k
  switch (bfd_get_flavour (abfd))
157
2.73k
    {
158
2.28k
    case bfd_target_elf_flavour:
159
2.28k
      if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
160
2.28k
  {
161
2.28k
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
162
2.28k
    struct bfd_elf_section_data * esd = elf_section_data (sec);
163
2.28k
    enum compression_type ch_type = (abfd->flags & BFD_COMPRESS_ZSTD
164
2.28k
             ? ch_compress_zstd
165
2.28k
             : ch_compress_zlib);
166
167
    /* Set the SHF_COMPRESSED bit.  */
168
2.28k
    elf_section_flags (sec) |= SHF_COMPRESSED;
169
170
2.28k
    if (bed->s->elfclass == ELFCLASS32)
171
1.14k
      {
172
1.14k
        Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
173
1.14k
        bfd_put_32 (abfd, ch_type, &echdr->ch_type);
174
1.14k
        bfd_put_32 (abfd, sec->size, &echdr->ch_size);
175
1.14k
        bfd_put_32 (abfd, 1u << sec->alignment_power,
176
1.14k
        &echdr->ch_addralign);
177
        /* bfd_log2 (alignof (Elf32_Chdr)) */
178
1.14k
        bfd_set_section_alignment (sec, 2);
179
1.14k
        esd->this_hdr.sh_addralign = 4;
180
1.14k
      }
181
1.14k
    else
182
1.14k
      {
183
1.14k
        Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
184
1.14k
        bfd_put_32 (abfd, ch_type, &echdr->ch_type);
185
1.14k
        bfd_put_32 (abfd, 0, &echdr->ch_reserved);
186
1.14k
        bfd_put_64 (abfd, sec->size, &echdr->ch_size);
187
1.14k
        bfd_put_64 (abfd, UINT64_C (1) << sec->alignment_power,
188
1.14k
        &echdr->ch_addralign);
189
        /* bfd_log2 (alignof (Elf64_Chdr)) */
190
1.14k
        bfd_set_section_alignment (sec, 3);
191
1.14k
        esd->this_hdr.sh_addralign = 8;
192
1.14k
      }
193
2.28k
    break;
194
2.28k
  }
195
196
      /* Clear the SHF_COMPRESSED bit.  */
197
0
      elf_section_flags (sec) &= ~SHF_COMPRESSED;
198
      /* Fall through.  */
199
200
454
    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
454
      memcpy (contents, "ZLIB", 4);
205
454
      bfd_putb64 (sec->size, contents + 4);
206
      /* No way to keep the original alignment, just use 1 always. */
207
454
      bfd_set_section_alignment (sec, 0);
208
454
      break;
209
2.73k
    }
210
2.73k
}
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.65k
{
224
3.65k
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
225
3.65k
      && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
226
3.65k
    {
227
3.65k
      Elf_Internal_Chdr chdr;
228
3.65k
      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
229
3.65k
      if (bed->s->elfclass == ELFCLASS32)
230
45
  {
231
45
    Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
232
45
    chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
233
45
    chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
234
45
    chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
235
45
  }
236
3.60k
      else
237
3.60k
  {
238
3.60k
    Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
239
3.60k
    chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
240
3.60k
    chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
241
3.60k
    chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
242
3.60k
  }
243
3.65k
      *ch_type = chdr.ch_type;
244
3.65k
      if ((chdr.ch_type == ch_compress_zlib
245
3.65k
     || chdr.ch_type == ch_compress_zstd)
246
3.65k
    && chdr.ch_addralign == (chdr.ch_addralign & -chdr.ch_addralign))
247
37
  {
248
37
    *uncompressed_size = chdr.ch_size;
249
37
    *uncompressed_alignment_power = bfd_log2 (chdr.ch_addralign);
250
37
    return true;
251
37
  }
252
3.65k
    }
253
254
3.61k
  return false;
255
3.65k
}
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
65.6k
{
271
65.6k
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
272
45.6k
    {
273
45.6k
      if (sec == NULL)
274
2.74k
  {
275
2.74k
    if (!(abfd->flags & BFD_COMPRESS_GABI))
276
0
      return 0;
277
2.74k
  }
278
42.8k
      else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
279
37.4k
  return 0;
280
281
8.20k
      if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
282
1.52k
  return sizeof (Elf32_External_Chdr);
283
6.68k
      else
284
6.68k
  return sizeof (Elf64_External_Chdr);
285
8.20k
    }
286
287
20.0k
  return 0;
288
65.6k
}
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
10.9k
{
309
10.9k
  bfd_size_type hdr_size;
310
311
10.9k
  if ((isec->flags & SEC_DEBUGGING) != 0
312
10.9k
      && (isec->flags & SEC_HAS_CONTENTS) != 0)
313
286
    {
314
286
      const char *name = *new_name;
315
316
286
      if ((obfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)) != 0)
317
211
  {
318
    /* When we decompress or compress with SHF_COMPRESSED,
319
       convert section name from .zdebug_* to .debug_*.  */
320
211
    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
211
  }
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
75
      else if (isec->compress_status == COMPRESS_SECTION_DONE
333
75
         && startswith (name, ".debug_"))
334
22
  {
335
22
    name = bfd_debug_name_to_zdebug (obfd, name);
336
22
    if (name == NULL)
337
0
      return false;
338
22
  }
339
286
      *new_name = name;
340
286
    }
341
10.9k
  *new_size = bfd_section_size (isec);
342
343
  /* Do nothing if either input or output aren't ELF.  */
344
10.9k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
345
10.9k
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
346
8.43k
    return true;
347
348
  /* Do nothing if ELF classes of input and output are the same. */
349
2.55k
  if (get_elf_backend_data (ibfd)->s->elfclass
350
2.55k
      == get_elf_backend_data (obfd)->s->elfclass)
351
2.55k
    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
2.15k
{
398
2.15k
  bfd_byte *contents;
399
2.15k
  bfd_size_type ihdr_size, ohdr_size, size;
400
2.15k
  Elf_Internal_Chdr chdr;
401
2.15k
  bool use_memmove;
402
403
  /* Do nothing if either input or output aren't ELF.  */
404
2.15k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
405
2.15k
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
406
274
    return true;
407
408
  /* Do nothing if ELF classes of input and output are the same.  */
409
1.87k
  if (get_elf_backend_data (ibfd)->s->elfclass
410
1.87k
      == get_elf_backend_data (obfd)->s->elfclass)
411
1.87k
    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 0.  */
562
563
static bfd_size_type
564
bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
565
3.30k
{
566
3.30k
  bfd_byte *input_buffer;
567
3.30k
  uLong compressed_size;
568
3.30k
  bfd_byte *buffer;
569
3.30k
  bfd_size_type buffer_size;
570
3.30k
  int zlib_size = 0;
571
3.30k
  int orig_header_size;
572
3.30k
  bfd_size_type uncompressed_size;
573
3.30k
  unsigned int uncompressed_alignment_pow;
574
3.30k
  enum compression_type ch_type = ch_none;
575
3.30k
  int new_header_size = bfd_get_compression_header_size (abfd, NULL);
576
3.30k
  bool compressed
577
3.30k
    = bfd_is_section_compressed_info (abfd, sec,
578
3.30k
              &orig_header_size,
579
3.30k
              &uncompressed_size,
580
3.30k
              &uncompressed_alignment_pow,
581
3.30k
              &ch_type);
582
3.30k
  bool update = false;
583
584
  /* We shouldn't be trying to decompress unsupported compressed sections.  */
585
3.30k
  if (compressed && orig_header_size < 0)
586
0
    abort ();
587
588
  /* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
589
     overhead in .zdebug* section.  */
590
3.30k
  if (!new_header_size)
591
562
    new_header_size = 12;
592
3.30k
  if (ch_type == ch_none)
593
3.30k
    orig_header_size = 12;
594
595
3.30k
  input_buffer = sec->contents;
596
3.30k
  if (compressed)
597
0
    {
598
0
      zlib_size = sec->size - orig_header_size;
599
0
      compressed_size = zlib_size + new_header_size;
600
601
      /* If we are converting between zlib-gnu and zlib-gabi then the
602
   compressed contents just need to be moved.  */
603
0
      update = (ch_type < ch_compress_zstd
604
0
    && (abfd->flags & BFD_COMPRESS_ZSTD) == 0);
605
606
      /* Uncompress when not just moving contents or when compressed
607
   is not smaller than uncompressed.  */
608
0
      if (!update || compressed_size >= uncompressed_size)
609
0
  {
610
0
    buffer_size = uncompressed_size;
611
0
    buffer = bfd_malloc (buffer_size);
612
0
    if (buffer == NULL)
613
0
      return 0;
614
615
0
    if (!decompress_contents (ch_type == ch_compress_zstd,
616
0
            input_buffer + orig_header_size,
617
0
            zlib_size, buffer, buffer_size))
618
0
      {
619
0
        bfd_set_error (bfd_error_bad_value);
620
0
        free (buffer);
621
0
        return 0;
622
0
      }
623
0
    free (input_buffer);
624
0
    bfd_set_section_alignment (sec, uncompressed_alignment_pow);
625
0
    sec->contents = buffer;
626
0
    sec->flags |= SEC_IN_MEMORY;
627
0
    sec->compress_status = COMPRESS_SECTION_NONE;
628
0
    sec->size = uncompressed_size;
629
0
    input_buffer = buffer;
630
0
  }
631
0
    }
632
633
3.30k
  if (!update)
634
3.30k
    compressed_size = compressBound (uncompressed_size) + new_header_size;
635
636
3.30k
  buffer_size = compressed_size;
637
3.30k
  buffer = bfd_alloc (abfd, buffer_size);
638
3.30k
  if (buffer == NULL)
639
0
    return 0;
640
641
3.30k
  if (update)
642
0
    {
643
0
      if (compressed_size < uncompressed_size)
644
0
  memcpy (buffer + new_header_size,
645
0
    input_buffer + orig_header_size,
646
0
    zlib_size);
647
0
    }
648
3.30k
  else
649
3.30k
    {
650
3.30k
      if (abfd->flags & BFD_COMPRESS_ZSTD)
651
0
  {
652
#if HAVE_ZSTD
653
    compressed_size = ZSTD_compress (buffer + new_header_size,
654
             compressed_size,
655
             input_buffer,
656
             uncompressed_size,
657
             ZSTD_CLEVEL_DEFAULT);
658
    if (ZSTD_isError (compressed_size))
659
      {
660
        bfd_release (abfd, buffer);
661
        bfd_set_error (bfd_error_bad_value);
662
        return 0;
663
      }
664
#endif
665
0
  }
666
3.30k
      else if (compress ((Bytef *) buffer + new_header_size, &compressed_size,
667
3.30k
       (const Bytef *) input_buffer, uncompressed_size)
668
3.30k
         != Z_OK)
669
0
  {
670
0
    bfd_release (abfd, buffer);
671
0
    bfd_set_error (bfd_error_bad_value);
672
0
    return 0;
673
0
  }
674
675
3.30k
      compressed_size += new_header_size;
676
3.30k
    }
677
678
  /* If compression didn't make the section smaller, keep it uncompressed.  */
679
3.30k
  if (compressed_size >= uncompressed_size)
680
568
    {
681
568
      memcpy (buffer, input_buffer, uncompressed_size);
682
568
      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
683
460
  elf_section_flags (sec) &= ~SHF_COMPRESSED;
684
568
      sec->compress_status = COMPRESS_SECTION_NONE;
685
568
    }
686
2.73k
  else
687
2.73k
    {
688
2.73k
      sec->size = uncompressed_size;
689
2.73k
      bfd_update_compression_header (abfd, buffer, sec);
690
2.73k
      sec->size = compressed_size;
691
2.73k
      sec->compress_status = COMPRESS_SECTION_DONE;
692
2.73k
    }
693
3.30k
  sec->contents = buffer;
694
3.30k
  sec->flags |= SEC_IN_MEMORY;
695
3.30k
  free (input_buffer);
696
3.30k
  return uncompressed_size;
697
3.30k
}
698
699
/*
700
FUNCTION
701
  bfd_get_full_section_contents
702
703
SYNOPSIS
704
  bool bfd_get_full_section_contents
705
    (bfd *abfd, asection *section, bfd_byte **ptr);
706
707
DESCRIPTION
708
  Read all data from @var{section} in BFD @var{abfd}, decompress
709
  if needed, and store in @var{*ptr}.  If @var{*ptr} is NULL,
710
  return @var{*ptr} with memory malloc'd by this function.
711
712
  Return @code{TRUE} if the full section contents is retrieved
713
  successfully.  If the section has no contents then this function
714
  returns @code{TRUE} but @var{*ptr} is set to NULL.
715
*/
716
717
bool
718
bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
719
1.28M
{
720
1.28M
  bfd_size_type readsz = bfd_get_section_limit_octets (abfd, sec);
721
1.28M
  bfd_size_type allocsz = bfd_get_section_alloc_size (abfd, sec);
722
1.28M
  bfd_byte *p = *ptr;
723
1.28M
  bool ret;
724
1.28M
  bfd_size_type save_size;
725
1.28M
  bfd_size_type save_rawsize;
726
1.28M
  bfd_byte *compressed_buffer;
727
1.28M
  unsigned int compression_header_size;
728
1.28M
  const unsigned int compress_status = sec->compress_status;
729
730
1.28M
  if (allocsz == 0)
731
1.15k
    {
732
1.15k
      *ptr = NULL;
733
1.15k
      return true;
734
1.15k
    }
735
736
1.27M
  if (p == NULL
737
1.27M
      && compress_status != COMPRESS_SECTION_DONE
738
1.27M
      && _bfd_section_size_insane (abfd, sec))
739
1.00M
    {
740
      /* PR 24708: Avoid attempts to allocate a ridiculous amount
741
   of memory.  */
742
1.00M
      _bfd_error_handler
743
  /* xgettext:c-format */
744
1.00M
  (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
745
1.00M
   abfd, sec, (uint64_t) readsz);
746
1.00M
      return false;
747
1.00M
    }
748
749
276k
  switch (compress_status)
750
276k
    {
751
276k
    case COMPRESS_SECTION_NONE:
752
276k
      if (p == NULL)
753
261k
  {
754
261k
    p = (bfd_byte *) bfd_malloc (allocsz);
755
261k
    if (p == NULL)
756
0
      {
757
        /* PR 20801: Provide a more helpful error message.  */
758
0
        if (bfd_get_error () == bfd_error_no_memory)
759
0
    _bfd_error_handler
760
      /* xgettext:c-format */
761
0
      (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
762
0
      abfd, sec, (uint64_t) allocsz);
763
0
        return false;
764
0
      }
765
261k
  }
766
767
276k
      if (!bfd_get_section_contents (abfd, sec, p, 0, readsz))
768
1.76k
  {
769
1.76k
    if (*ptr != p)
770
1.33k
      free (p);
771
1.76k
    return false;
772
1.76k
  }
773
274k
      *ptr = p;
774
274k
      return true;
775
776
0
    case DECOMPRESS_SECTION_ZLIB:
777
0
    case DECOMPRESS_SECTION_ZSTD:
778
      /* Read in the full compressed section contents.  */
779
0
      compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
780
0
      if (compressed_buffer == NULL)
781
0
  return false;
782
0
      save_rawsize = sec->rawsize;
783
0
      save_size = sec->size;
784
      /* Clear rawsize, set size to compressed size and set compress_status
785
   to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
786
   the uncompressed size, bfd_get_section_contents will fail.  */
787
0
      sec->rawsize = 0;
788
0
      sec->size = sec->compressed_size;
789
0
      sec->compress_status = COMPRESS_SECTION_NONE;
790
0
      ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
791
0
              0, sec->compressed_size);
792
      /* Restore rawsize and size.  */
793
0
      sec->rawsize = save_rawsize;
794
0
      sec->size = save_size;
795
0
      sec->compress_status = compress_status;
796
0
      if (!ret)
797
0
  goto fail_compressed;
798
799
0
      if (p == NULL)
800
0
  p = (bfd_byte *) bfd_malloc (allocsz);
801
0
      if (p == NULL)
802
0
  goto fail_compressed;
803
804
0
      compression_header_size = bfd_get_compression_header_size (abfd, sec);
805
0
      if (compression_header_size == 0)
806
  /* Set header size to the zlib header size if it is a
807
     SHF_COMPRESSED section.  */
808
0
  compression_header_size = 12;
809
0
      bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
810
0
      if (!decompress_contents (
811
0
        is_zstd, compressed_buffer + compression_header_size,
812
0
        sec->compressed_size - compression_header_size, p, readsz))
813
0
  {
814
0
    bfd_set_error (bfd_error_bad_value);
815
0
    if (p != *ptr)
816
0
      free (p);
817
0
  fail_compressed:
818
0
    free (compressed_buffer);
819
0
    return false;
820
0
  }
821
822
0
      free (compressed_buffer);
823
0
      *ptr = p;
824
0
      return true;
825
826
183
    case COMPRESS_SECTION_DONE:
827
183
      if (sec->contents == NULL)
828
0
  return false;
829
183
      if (p == NULL)
830
183
  {
831
183
    p = (bfd_byte *) bfd_malloc (allocsz);
832
183
    if (p == NULL)
833
0
      return false;
834
183
    *ptr = p;
835
183
  }
836
      /* PR 17512; file: 5bc29788.  */
837
183
      if (p != sec->contents)
838
183
  memcpy (p, sec->contents, readsz);
839
183
      return true;
840
841
0
    default:
842
0
      abort ();
843
276k
    }
844
276k
}
845
846
/*
847
FUNCTION
848
  bfd_is_section_compressed_info
849
850
SYNOPSIS
851
  bool bfd_is_section_compressed_info
852
    (bfd *abfd, asection *section,
853
     int *compression_header_size_p,
854
     bfd_size_type *uncompressed_size_p,
855
     unsigned int *uncompressed_alignment_power_p,
856
     enum compression_type *ch_type);
857
858
DESCRIPTION
859
  Return @code{TRUE} if @var{section} is compressed.  Compression
860
  header size is returned in @var{compression_header_size_p},
861
  uncompressed size is returned in @var{uncompressed_size_p}
862
  and the uncompressed data alignement power is returned in
863
  @var{uncompressed_align_pow_p}.  If compression is
864
  unsupported, compression header size is returned with -1
865
  and uncompressed size is returned with 0.
866
*/
867
868
bool
869
bfd_is_section_compressed_info (bfd *abfd, sec_ptr sec,
870
        int *compression_header_size_p,
871
        bfd_size_type *uncompressed_size_p,
872
        unsigned int *uncompressed_align_pow_p,
873
        enum compression_type *ch_type)
874
62.2k
{
875
62.2k
  bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
876
62.2k
  int compression_header_size;
877
62.2k
  int header_size;
878
62.2k
  unsigned int saved = sec->compress_status;
879
62.2k
  bool compressed;
880
881
62.2k
  *uncompressed_align_pow_p = 0;
882
883
62.2k
  compression_header_size = bfd_get_compression_header_size (abfd, sec);
884
62.2k
  if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
885
0
    abort ();
886
62.2k
  header_size = compression_header_size ? compression_header_size : 12;
887
888
  /* Don't decompress the section.  */
889
62.2k
  sec->compress_status = COMPRESS_SECTION_NONE;
890
891
  /* Read the header.  */
892
62.2k
  if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
893
39.1k
    {
894
39.1k
      if (compression_header_size == 0)
895
  /* In this case, it should be "ZLIB" followed by the uncompressed
896
     section size, 8 bytes in big-endian order.  */
897
35.5k
  compressed = startswith ((char*) header , "ZLIB");
898
3.52k
      else
899
3.52k
  compressed = true;
900
39.1k
    }
901
23.1k
  else
902
23.1k
    compressed = false;
903
904
62.2k
  *uncompressed_size_p = sec->size;
905
62.2k
  if (compressed)
906
3.81k
    {
907
3.81k
      if (compression_header_size != 0)
908
3.52k
  {
909
3.52k
    if (!bfd_check_compression_header (abfd, header, sec, ch_type,
910
3.52k
               uncompressed_size_p,
911
3.52k
               uncompressed_align_pow_p))
912
3.49k
      compression_header_size = -1;
913
3.52k
  }
914
      /* Check for the pathalogical case of a debug string section that
915
   contains the string ZLIB.... as the first entry.  We assume that
916
   no uncompressed .debug_str section would ever be big enough to
917
   have the first byte of its (big-endian) size be non-zero.  */
918
289
      else if (strcmp (sec->name, ".debug_str") == 0
919
289
         && ISPRINT (header[4]))
920
13
  compressed = false;
921
276
      else
922
276
  *uncompressed_size_p = bfd_getb64 (header + 4);
923
3.81k
    }
924
925
  /* Restore compress_status.  */
926
62.2k
  sec->compress_status = saved;
927
62.2k
  *compression_header_size_p = compression_header_size;
928
62.2k
  return compressed;
929
62.2k
}
930
931
/*
932
FUNCTION
933
  bfd_is_section_compressed
934
935
SYNOPSIS
936
  bool bfd_is_section_compressed
937
    (bfd *abfd, asection *section);
938
939
DESCRIPTION
940
  Return @code{TRUE} if @var{section} is compressed.
941
*/
942
943
bool
944
bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
945
18.9k
{
946
18.9k
  int compression_header_size;
947
18.9k
  bfd_size_type uncompressed_size;
948
18.9k
  unsigned int uncompressed_align_power;
949
18.9k
  enum compression_type ch_type;
950
18.9k
  return (bfd_is_section_compressed_info (abfd, sec,
951
18.9k
            &compression_header_size,
952
18.9k
            &uncompressed_size,
953
18.9k
            &uncompressed_align_power,
954
18.9k
            &ch_type)
955
18.9k
    && compression_header_size >= 0
956
18.9k
    && uncompressed_size > 0);
957
18.9k
}
958
959
/*
960
FUNCTION
961
  bfd_init_section_decompress_status
962
963
SYNOPSIS
964
  bool bfd_init_section_decompress_status
965
    (bfd *abfd, asection *section);
966
967
DESCRIPTION
968
  Record compressed section size, update section size with
969
  decompressed size and set compress_status to
970
  DECOMPRESS_SECTION_{ZLIB,ZSTD}.
971
972
  Return @code{FALSE} if the section is not a valid compressed
973
  section.  Otherwise, return @code{TRUE}.
974
*/
975
976
bool
977
bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
978
137
{
979
137
  bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
980
137
  int compression_header_size;
981
137
  int header_size;
982
137
  bfd_size_type uncompressed_size;
983
137
  unsigned int uncompressed_alignment_power = 0;
984
137
  enum compression_type ch_type;
985
137
  z_stream strm;
986
987
137
  compression_header_size = bfd_get_compression_header_size (abfd, sec);
988
137
  if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
989
0
    abort ();
990
137
  header_size = compression_header_size ? compression_header_size : 12;
991
992
  /* Read the header.  */
993
137
  if (sec->rawsize != 0
994
137
      || sec->contents != NULL
995
137
      || sec->compress_status != COMPRESS_SECTION_NONE
996
137
      || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
997
0
    {
998
0
      bfd_set_error (bfd_error_invalid_operation);
999
0
      return false;
1000
0
    }
1001
1002
137
  if (compression_header_size == 0)
1003
11
    {
1004
      /* In this case, it should be "ZLIB" followed by the uncompressed
1005
   section size, 8 bytes in big-endian order.  */
1006
11
      if (! startswith ((char*) header, "ZLIB"))
1007
0
  {
1008
0
    bfd_set_error (bfd_error_wrong_format);
1009
0
    return false;
1010
0
  }
1011
11
      uncompressed_size = bfd_getb64 (header + 4);
1012
11
      ch_type = ch_none;
1013
11
    }
1014
126
  else if (!bfd_check_compression_header (abfd, header, sec,
1015
126
            &ch_type,
1016
126
            &uncompressed_size,
1017
126
            &uncompressed_alignment_power))
1018
123
    {
1019
123
      bfd_set_error (bfd_error_wrong_format);
1020
123
      return false;
1021
123
    }
1022
1023
  /* PR28530, reject sizes unsupported by decompress_contents.  */
1024
14
  strm.avail_in = sec->size;
1025
14
  strm.avail_out = uncompressed_size;
1026
14
  if (strm.avail_in != sec->size || strm.avail_out != uncompressed_size)
1027
12
    {
1028
12
      bfd_set_error (bfd_error_nonrepresentable_section);
1029
12
      return false;
1030
12
    }
1031
1032
2
  sec->compressed_size = sec->size;
1033
2
  sec->size = uncompressed_size;
1034
2
  bfd_set_section_alignment (sec, uncompressed_alignment_power);
1035
2
  sec->compress_status = (ch_type == ch_compress_zstd
1036
2
        ? DECOMPRESS_SECTION_ZSTD : DECOMPRESS_SECTION_ZLIB);
1037
1038
2
  return true;
1039
14
}
1040
1041
/*
1042
FUNCTION
1043
  bfd_init_section_compress_status
1044
1045
SYNOPSIS
1046
  bool bfd_init_section_compress_status
1047
    (bfd *abfd, asection *section);
1048
1049
DESCRIPTION
1050
  If open for read, compress section, update section size with
1051
  compressed size and set compress_status to COMPRESS_SECTION_DONE.
1052
1053
  Return @code{FALSE} if the section is not a valid compressed
1054
  section.  Otherwise, return @code{TRUE}.
1055
*/
1056
1057
bool
1058
bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
1059
3.34k
{
1060
3.34k
  bfd_size_type uncompressed_size;
1061
3.34k
  bfd_byte *uncompressed_buffer;
1062
1063
  /* Error if not opened for read.  */
1064
3.34k
  if (abfd->direction != read_direction
1065
3.34k
      || sec->size == 0
1066
3.34k
      || sec->rawsize != 0
1067
3.34k
      || sec->contents != NULL
1068
3.34k
      || sec->compress_status != COMPRESS_SECTION_NONE
1069
3.34k
      || _bfd_section_size_insane (abfd, sec))
1070
44
    {
1071
44
      bfd_set_error (bfd_error_invalid_operation);
1072
44
      return false;
1073
44
    }
1074
1075
  /* Read in the full section contents and compress it.  */
1076
3.30k
  uncompressed_size = sec->size;
1077
3.30k
  uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
1078
  /* PR 21431 */
1079
3.30k
  if (uncompressed_buffer == NULL)
1080
0
    return false;
1081
1082
3.30k
  if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
1083
3.30k
         0, uncompressed_size))
1084
2
    {
1085
2
      free (uncompressed_buffer);
1086
2
      return false;
1087
2
    }
1088
1089
3.30k
  sec->contents = uncompressed_buffer;
1090
3.30k
  if (bfd_compress_section_contents (abfd, sec) == 0)
1091
0
    {
1092
0
      free (sec->contents);
1093
0
      sec->contents = NULL;
1094
0
      return false;
1095
0
    }
1096
3.30k
  return true;
1097
3.30k
}
1098
1099
/*
1100
FUNCTION
1101
  bfd_compress_section
1102
1103
SYNOPSIS
1104
  bool bfd_compress_section
1105
    (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
1106
1107
DESCRIPTION
1108
  If open for write, compress section, update section size with
1109
  compressed size and set compress_status to COMPRESS_SECTION_DONE.
1110
1111
  Return @code{FALSE} if compression fail.  Otherwise, return
1112
  @code{TRUE}.  UNCOMPRESSED_BUFFER is freed in both cases.
1113
*/
1114
1115
bool
1116
bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
1117
0
{
1118
0
  bfd_size_type uncompressed_size = sec->size;
1119
1120
  /* Error if not opened for write.  */
1121
0
  if (abfd->direction != write_direction
1122
0
      || uncompressed_size == 0
1123
0
      || uncompressed_buffer == NULL
1124
0
      || sec->contents != NULL
1125
0
      || sec->compressed_size != 0
1126
0
      || sec->compress_status != COMPRESS_SECTION_NONE)
1127
0
    {
1128
0
      bfd_set_error (bfd_error_invalid_operation);
1129
0
      return false;
1130
0
    }
1131
1132
0
  sec->contents = uncompressed_buffer;
1133
0
  if (bfd_compress_section_contents (abfd, sec) == 0)
1134
0
    {
1135
0
      free (sec->contents);
1136
0
      sec->contents = NULL;
1137
0
      return false;
1138
0
    }
1139
0
  return true;
1140
0
}