binutils, gdb: support zstd compressed debug sections

PR29397 PR29563: Add new configure option --with-zstd which defaults to
auto.  If pkgconfig/libzstd.pc is found, define HAVE_ZSTD and support
zstd compressed debug sections for most tools.

* bfd: for addr2line, objdump --dwarf, gdb, etc
* gas: support --compress-debug-sections=zstd
* ld: support ELFCOMPRESS_ZSTD input and --compress-debug-sections=zstd
* objcopy: support ELFCOMPRESS_ZSTD input for
  --decompress-debug-sections and --compress-debug-sections=zstd
* gdb: support ELFCOMPRESS_ZSTD input.  The bfd change references zstd
  symbols, so gdb has to link against -lzstd in this patch.

If zstd is not supported, ELFCOMPRESS_ZSTD input triggers an error.  We
can avoid HAVE_ZSTD if binutils-gdb imports zstd/ like zlib/, but this
is too heavyweight, so don't do it for now.

```
% ld/ld-new a.o
ld/ld-new: a.o: section .debug_abbrev is compressed with zstd, but BFD is not built with zstd support
...

% ld/ld-new a.o --compress-debug-sections=zstd
ld/ld-new: --compress-debug-sections=zstd: ld is not built with zstd support

% binutils/objcopy --compress-debug-sections=zstd a.o b.o
binutils/objcopy: --compress-debug-sections=zstd: binutils is not built with zstd support

% binutils/objcopy b.o --decompress-debug-sections
binutils/objcopy: zstd.o: section .debug_abbrev is compressed with zstd, but BFD is not built with zstd support
...
```
This commit is contained in:
Fangrui Song
2022-09-26 19:50:13 -07:00
committed by Fangrui Song
parent e122316b7c
commit 2cac01e3ff
56 changed files with 1490 additions and 254 deletions

View File

@ -232,7 +232,8 @@ static enum
compress_zlib = compress | 1 << 1,
compress_gnu_zlib = compress | 1 << 2,
compress_gabi_zlib = compress | 1 << 3,
decompress = 1 << 4
compress_zstd = compress | 1 << 4,
decompress = 1 << 5
} do_debug_sections = nothing;
/* Whether to generate ELF common symbols with the STT_COMMON type. */
@ -678,8 +679,8 @@ copy_usage (FILE *stream, int exit_status)
<commit>\n\
--subsystem <name>[:<version>]\n\
Set PE subsystem to <name> [& <version>]\n\
--compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
Compress DWARF debug sections using zlib\n\
--compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\
Compress DWARF debug sections\n\
--decompress-debug-sections Decompress DWARF debug sections using zlib\n\
--elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
type\n\
@ -2659,7 +2660,8 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
if ((do_debug_sections & compress) != 0
&& do_debug_sections != compress)
{
non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
non_fatal (_ ("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi|"
"zstd] is unsupported on `%s'"),
bfd_get_archive_filename (ibfd));
return false;
}
@ -3807,6 +3809,13 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
if (do_debug_sections != compress_gnu_zlib)
ibfd->flags |= BFD_COMPRESS_GABI;
break;
case compress_zstd:
ibfd->flags |= BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
#ifndef HAVE_ZSTD
fatal (_ ("--compress-debug-sections=zstd: binutils is not built with "
"zstd support"));
#endif
break;
case decompress:
ibfd->flags |= BFD_DECOMPRESS;
break;
@ -5469,6 +5478,8 @@ copy_main (int argc, char *argv[])
do_debug_sections = compress_gnu_zlib;
else if (strcasecmp (optarg, "zlib-gabi") == 0)
do_debug_sections = compress_gabi_zlib;
else if (strcasecmp (optarg, "zstd") == 0)
do_debug_sections = compress_zstd;
else
fatal (_("unrecognized --compress-debug-sections type `%s'"),
optarg);