This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
RFC: Add --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi] to ld
- From: "H.J. Lu" <hongjiu dot lu at intel dot com>
- To: binutils at sourceware dot org
- Date: Thu, 9 Apr 2015 15:07:20 -0700
- Subject: RFC: Add --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi] to ld
- Authentication-results: sourceware.org; auth=none
- Reply-to: "H.J. Lu" <hjl dot tools at gmail dot com>
Hi,
This patch adds --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi]
to ld. Any comments, suggestions?
Thanks.
H.J.
---
bfd/bfd-in2.h | 3 ++
bfd/compress.c | 54 ++++++++++++++++++++++++--
bfd/elf.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++---
bfd/merge.c | 69 +++++++++++++++++++++++++++------
gas/as.h | 11 +-----
include/bfdlink.h | 13 +++++++
ld/emultempl/elf32.em | 15 ++++++++
ld/ld.texinfo | 16 ++++++++
ld/ldmain.c | 7 ++++
ld/lexsup.c | 3 ++
10 files changed, 263 insertions(+), 31 deletions(-)
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 679595e..d3cc164 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -7316,6 +7316,9 @@ bfd_boolean bfd_init_section_decompress_status
bfd_boolean bfd_init_section_compress_status
(bfd *abfd, asection *section);
+bfd_boolean bfd_compress_section
+ (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
+
#ifdef __cplusplus
}
#endif
diff --git a/bfd/compress.c b/bfd/compress.c
index 7945344..5b9d32b 100644
--- a/bfd/compress.c
+++ b/bfd/compress.c
@@ -72,7 +72,8 @@ decompress_contents (bfd_byte *compressed_buffer,
static bfd_size_type
bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
bfd_byte *uncompressed_buffer,
- bfd_size_type uncompressed_size)
+ bfd_size_type uncompressed_size,
+ bfd_boolean write_compress)
{
uLong compressed_size;
bfd_byte *buffer;
@@ -131,7 +132,13 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
decompress = FALSE;
buffer_size = compressed_size + compression_header_size;
}
- buffer = (bfd_byte *) bfd_malloc (buffer_size);
+
+ if (write_compress)
+ /* Use bfd_alloc since it will be stored in "contents" of
+ Elf_Internal_Shdr. */
+ buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
+ else
+ buffer = (bfd_byte *) bfd_malloc (buffer_size);
if (buffer == NULL)
return 0;
@@ -177,7 +184,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
compressed_size += header_size;
/* PR binutils/18087: If compression didn't make the section smaller,
just keep it uncompressed. */
- if (compressed_size < uncompressed_size)
+ if (write_compress || compressed_size < uncompressed_size)
{
bfd_update_compression_header (abfd, buffer, sec);
@@ -540,9 +547,48 @@ bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
{
uncompressed_size = bfd_compress_section_contents (abfd, sec,
uncompressed_buffer,
- uncompressed_size);
+ uncompressed_size,
+ FALSE);
ret = uncompressed_size != 0;
}
return ret;
}
+
+/*
+FUNCTION
+ bfd_compress_section
+
+SYNOPSIS
+ bfd_boolean bfd_compress_section
+ (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
+
+DESCRIPTION
+ If open for write, compress section, update section size with
+ compressed size and set compress_status to COMPRESS_SECTION_DONE.
+
+ Return @code{FALSE} if compression fail. Otherwise, return
+ @code{TRUE}.
+*/
+
+bfd_boolean
+bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
+{
+ bfd_size_type uncompressed_size = sec->size;
+
+ /* Error if not opened for write. */
+ if (abfd->direction != write_direction
+ || uncompressed_size == 0
+ || uncompressed_buffer == NULL
+ || sec->contents != NULL
+ || sec->compressed_size != 0
+ || sec->compress_status != COMPRESS_SECTION_NONE)
+ {
+ bfd_set_error (bfd_error_invalid_operation);
+ return FALSE;
+ }
+
+ /* Compress it. */
+ return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
+ uncompressed_size, TRUE) != 0;
+}
diff --git a/bfd/elf.c b/bfd/elf.c
index a031b9e..e4d333a 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -2752,6 +2752,7 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
struct bfd_elf_section_data *esd = elf_section_data (asect);
Elf_Internal_Shdr *this_hdr;
unsigned int sh_type;
+ const char *name = asect->name;
if (arg->failed)
{
@@ -2762,8 +2763,31 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
this_hdr = &esd->this_hdr;
+ if (arg->link_info
+ && (arg->link_info->compress_debug & COMPRESS_DEBUG)
+ && arg->link_info->compress_debug != COMPRESS_DEBUG_GABI_ZLIB
+ && (asect->flags & SEC_DEBUGGING)
+ && name[1] == 'd'
+ && name[6] == '_')
+ {
+ /* Rename compressed DWARF debug section from .debug_* to
+ .zdebug_*. */
+ unsigned int len = strlen (name);
+ char *new_name = bfd_alloc (abfd, len + 2);
+ if (new_name == NULL)
+ {
+ arg->failed = TRUE;
+ return;
+ }
+ new_name[0] = '.';
+ new_name[1] = 'z';
+ memcpy (new_name + 2, name + 1, len);
+ bfd_rename_section (abfd, asect, new_name);
+ name = asect->name;
+ }
+
this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
- asect->name, FALSE);
+ name, FALSE);
if (this_hdr->sh_name == (unsigned int) -1)
{
arg->failed = TRUE;
@@ -5081,6 +5105,9 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
unsigned int num_sec;
unsigned int i;
unsigned int count;
+ bfd_boolean compress_debug
+ = (link_info != NULL
+ && (link_info->compress_debug & COMPRESS_DEBUG) != 0);
i_shdrpp = elf_elfsections (abfd);
num_sec = elf_numsections (abfd);
@@ -5116,6 +5143,16 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
}
else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
&& hdr->bfd_section == NULL)
+ || (compress_debug
+ && hdr->sh_type == SHT_PROGBITS
+ && hdr->bfd_section != NULL
+ && (hdr->bfd_section->flags & SEC_DEBUGGING)
+ && ((hdr->bfd_section->name[1] == 'd'
+ && hdr->bfd_section->name[6] == '_')
+ || (hdr->bfd_section->name[1] == 'z'
+ && hdr->bfd_section->name[7] == '_')))
+ /* Compress DWARF debug sections with names:
+ .debug_* and .zdebug_*. */
|| hdr == i_shdrpp[elf_onesymtab (abfd)]
|| hdr == i_shdrpp[elf_symtab_shndx (abfd)]
|| hdr == i_shdrpp[elf_strtab_sec (abfd)])
@@ -5351,6 +5388,9 @@ assign_file_positions_except_relocs (bfd *abfd,
Elf_Internal_Shdr **hdrpp;
unsigned int i;
file_ptr off;
+ bfd_boolean compress_debug
+ = (link_info != NULL
+ && (link_info->compress_debug & COMPRESS_DEBUG) != 0);
/* Start after the ELF header. */
off = i_ehdrp->e_ehsize;
@@ -5365,6 +5405,16 @@ assign_file_positions_except_relocs (bfd *abfd,
hdr = *hdrpp;
if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
&& hdr->bfd_section == NULL)
+ || (compress_debug
+ && hdr->sh_type == SHT_PROGBITS
+ && hdr->bfd_section != NULL
+ && (hdr->bfd_section->flags & SEC_DEBUGGING)
+ && ((hdr->bfd_section->name[1] == 'd'
+ && hdr->bfd_section->name[6] == '_')
+ || (hdr->bfd_section->name[1] == 'z'
+ && hdr->bfd_section->name[7] == '_')))
+ /* Compress DWARF debug sections with names:
+ .debug_* and .zdebug_*. */
|| i == elf_onesymtab (abfd)
|| i == elf_symtab_shndx (abfd)
|| i == elf_strtab_sec (abfd))
@@ -5518,8 +5568,8 @@ prep_headers (bfd *abfd)
/* Assign file positions for all the reloc sections which are not part
of the loadable file image, and the file position of section headers. */
-static void
-_bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
+static bfd_boolean
+_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
{
file_ptr off;
unsigned int i, num_sec;
@@ -5535,9 +5585,26 @@ _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
Elf_Internal_Shdr *shdrp;
shdrp = *shdrpp;
- if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
+ if ((shdrp->sh_type == SHT_REL
+ || shdrp->sh_type == SHT_RELA
+ || shdrp->sh_type == SHT_PROGBITS)
&& shdrp->sh_offset == -1)
- off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
+ {
+ if (shdrp->sh_type == SHT_PROGBITS
+ && (abfd->flags & BFD_COMPRESS))
+ {
+ /* Compress DWARF debug sections. */
+ if (!bfd_compress_section (abfd, shdrp->bfd_section,
+ shdrp->contents))
+ return FALSE;
+ /* Update section size and contents. */
+ shdrp->sh_size = shdrp->bfd_section->size;
+ shdrp->contents = shdrp->bfd_section->contents;
+ shdrp->bfd_section->contents = NULL;
+ }
+ off = _bfd_elf_assign_file_position_for_section (shdrp, off,
+ TRUE);
+ }
}
/* Place the section headers. */
@@ -5547,6 +5614,8 @@ _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
i_ehdrp->e_shoff = off;
off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
elf_next_file_pos (abfd) = off;
+
+ return TRUE;
}
bfd_boolean
@@ -5569,7 +5638,8 @@ _bfd_elf_write_object_contents (bfd *abfd)
if (failed)
return FALSE;
- _bfd_elf_assign_file_positions_for_relocs (abfd);
+ if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
+ return FALSE;
/* After writing the headers, we need to write the sections too... */
num_sec = elf_numsections (abfd);
@@ -7922,7 +7992,28 @@ _bfd_elf_set_section_contents (bfd *abfd,
&& ! _bfd_elf_compute_section_file_positions (abfd, NULL))
return FALSE;
+ if (!count)
+ return TRUE;
+
hdr = &elf_section_data (section)->this_hdr;
+ if (hdr->sh_offset == (file_ptr) -1)
+ {
+ unsigned char *contents = hdr->contents;
+ if ((offset + count) > hdr->sh_size
+ || (abfd->flags & BFD_COMPRESS) == 0)
+ abort ();
+ if (contents == NULL)
+ {
+ /* Use bfd_malloc since it will be freed by
+ bfd_compress_section_contents. */
+ contents = (unsigned char *) bfd_malloc (hdr->sh_size);
+ if (contents == NULL)
+ return FALSE;
+ hdr->contents = contents;
+ }
+ memcpy (contents + offset, location, count);
+ return TRUE;
+ }
pos = hdr->sh_offset + offset;
if (bfd_seek (abfd, pos, SEEK_SET) != 0
|| bfd_bwrite (location, count, abfd) != count)
diff --git a/bfd/merge.c b/bfd/merge.c
index 5f45ba6..d39d1db 100644
--- a/bfd/merge.c
+++ b/bfd/merge.c
@@ -25,6 +25,7 @@
#include "sysdep.h"
#include "bfd.h"
+#include "elf-bfd.h"
#include "libbfd.h"
#include "hashtab.h"
#include "libiberty.h"
@@ -288,8 +289,9 @@ sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry)
struct sec_merge_sec_info *secinfo = entry->secinfo;
asection *sec = secinfo->sec;
char *pad = NULL;
- bfd_size_type off = 0;
+ bfd_size_type len, off = 0;
int alignment_power = sec->output_section->alignment_power;
+ unsigned char *contents = NULL;
if (alignment_power)
{
@@ -298,15 +300,24 @@ sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry)
return FALSE;
}
+ if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+ {
+ Elf_Internal_Shdr *hdr
+ = &elf_section_data (sec->output_section)->this_hdr;
+ if (hdr->sh_offset == (file_ptr) -1)
+ contents = hdr->contents;
+ }
+
for (; entry != NULL && entry->secinfo == secinfo; entry = entry->next)
{
const char *str;
- bfd_size_type len;
len = -off & (entry->alignment - 1);
if (len != 0)
{
- if (bfd_bwrite (pad, len, abfd) != len)
+ if (contents)
+ memcpy (contents + off, pad, len);
+ else if (bfd_bwrite (pad, len, abfd) != len)
goto err;
off += len;
}
@@ -314,17 +325,23 @@ sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry)
str = entry->root.string;
len = entry->len;
- if (bfd_bwrite (str, len, abfd) != len)
+ if (contents)
+ memcpy (contents + off, str, len);
+ else if (bfd_bwrite (str, len, abfd) != len)
goto err;
off += len;
}
/* Trailing alignment needed? */
- off = sec->size - off;
- if (off != 0
- && bfd_bwrite (pad, off, abfd) != off)
- goto err;
+ len = sec->size - off;
+ if (len != 0)
+ {
+ if (contents)
+ memcpy (contents + off, pad, len);
+ else if (bfd_bwrite (pad, len, abfd) != len)
+ goto err;
+ }
if (pad != NULL)
free (pad);
@@ -785,6 +802,7 @@ _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
{
struct sec_merge_sec_info *secinfo;
file_ptr pos;
+ unsigned char *contents;
secinfo = (struct sec_merge_sec_info *) psecinfo;
@@ -794,10 +812,39 @@ _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo)
if (secinfo->first_str == NULL)
return TRUE;
+ contents = NULL;
+
/* FIXME: octets_per_byte. */
- pos = sec->output_section->filepos + sec->output_offset;
- if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
- return FALSE;
+ if (bfd_get_flavour (output_bfd) == bfd_target_elf_flavour)
+ {
+ Elf_Internal_Shdr *hdr
+ = &elf_section_data (sec->output_section)->this_hdr;
+ if (hdr->sh_offset == (file_ptr) -1)
+ {
+ if ((output_bfd->flags & BFD_COMPRESS) == 0)
+ abort ();
+
+ /* Cache the section contents so that they can be compressed
+ later. */
+ contents = hdr->contents;
+ if (contents == NULL)
+ {
+ /* Use bfd_malloc since it will be freed by
+ bfd_compress_section_contents. */
+ contents = (unsigned char *) bfd_malloc (hdr->sh_size);
+ if (contents == NULL)
+ return FALSE;
+ hdr->contents = contents;
+ }
+ }
+ }
+
+ if (contents == NULL)
+ {
+ pos = sec->output_section->filepos + sec->output_offset;
+ if (bfd_seek (output_bfd, pos, SEEK_SET) != 0)
+ return FALSE;
+ }
if (! sec_merge_emit (output_bfd, secinfo->first_str))
return FALSE;
diff --git a/gas/as.h b/gas/as.h
index e04cc0f..059f421 100644
--- a/gas/as.h
+++ b/gas/as.h
@@ -93,6 +93,7 @@
/* Now GNU header files... */
#include "ansidecl.h"
#include "bfd.h"
+#include "bfdlink.h"
#include "libiberty.h"
/* Define the standard progress macros. */
@@ -370,16 +371,6 @@ COMMON int flag_strip_local_absolute;
/* True if we should generate a traditional format object file. */
COMMON int flag_traditional_format;
-/* Types of compressed debug sections. We currently support zlib. */
-enum compressed_debug_section_type
-{
- COMPRESS_DEBUG_NONE = 0,
- COMPRESS_DEBUG,
- COMPRESS_DEBUG_ZLIB,
- COMPRESS_DEBUG_GNU_ZLIB,
- COMPRESS_DEBUG_GABI_ZLIB
-};
-
/* Type of compressed debug sections we should generate. */
COMMON enum compressed_debug_section_type flag_compress_debug;
diff --git a/include/bfdlink.h b/include/bfdlink.h
index 6a02a3c..99ea0bf 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -259,6 +259,16 @@ struct flag_info
struct bfd_elf_dynamic_list;
struct bfd_elf_version_tree;
+/* Types of compressed DWARF debug sections. We currently support zlib. */
+enum compressed_debug_section_type
+{
+ COMPRESS_DEBUG_NONE = 0,
+ COMPRESS_DEBUG = 1 << 0,
+ COMPRESS_DEBUG_ZLIB = COMPRESS_DEBUG | 1 << 1,
+ COMPRESS_DEBUG_GNU_ZLIB = COMPRESS_DEBUG | 1 << 2,
+ COMPRESS_DEBUG_GABI_ZLIB = COMPRESS_DEBUG | 1 << 3
+};
+
/* This structure holds all the information needed to communicate
between BFD and the linker when doing a link. */
@@ -431,6 +441,9 @@ struct bfd_link_info
/* Separator between archive and filename in linker script filespecs. */
char path_separator;
+ /* Compress DWARF debug sections. */
+ enum compressed_debug_section_type compress_debug;
+
/* Default stack size. Zero means default (often zero itself), -1
means explicitly zero-sized. */
bfd_signed_vma stacksize;
diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em
index 5db5a93..0802d76 100644
--- a/ld/emultempl/elf32.em
+++ b/ld/emultempl/elf32.em
@@ -2110,6 +2110,7 @@ fragment <<EOF
#define OPTION_HASH_STYLE (OPTION_EXCLUDE_LIBS + 1)
#define OPTION_BUILD_ID (OPTION_HASH_STYLE + 1)
#define OPTION_AUDIT (OPTION_BUILD_ID + 1)
+#define OPTION_COMPRESS_DEBUG (OPTION_AUDIT + 1)
static void
gld${EMULATION_NAME}_add_options
@@ -2137,6 +2138,7 @@ EOF
fi
fragment <<EOF
{"build-id", optional_argument, NULL, OPTION_BUILD_ID},
+ {"compress-debug-sections", required_argument, NULL, OPTION_COMPRESS_DEBUG},
EOF
if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
fragment <<EOF
@@ -2186,6 +2188,19 @@ gld${EMULATION_NAME}_handle_option (int optc)
emit_note_gnu_build_id = xstrdup (optarg);
break;
+ case OPTION_COMPRESS_DEBUG:
+ if (strcasecmp (optarg, "none") == 0)
+ link_info.compress_debug = COMPRESS_DEBUG_NONE;
+ else if (strcasecmp (optarg, "zlib") == 0)
+ link_info.compress_debug = COMPRESS_DEBUG_ZLIB;
+ else if (strcasecmp (optarg, "zlib-gnu") == 0)
+ link_info.compress_debug = COMPRESS_DEBUG_GNU_ZLIB;
+ else if (strcasecmp (optarg, "zlib-gabi") == 0)
+ link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
+ else
+ einfo (_("%P%F: invalid --compress-debug-sections option: \`%s'\n"),
+ optarg);
+ break;
EOF
if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
diff --git a/ld/ld.texinfo b/ld/ld.texinfo
index 82735e9..7cc0ae3 100644
--- a/ld/ld.texinfo
+++ b/ld/ld.texinfo
@@ -2193,6 +2193,22 @@ new style GNU @code{.gnu.hash} section or @code{both} for both
the classic ELF @code{.hash} and new style GNU @code{.gnu.hash}
hash tables. The default is @code{sysv}.
+@kindex --compress-debug-sections=none
+@kindex --compress-debug-sections=zlib
+@kindex --compress-debug-sections=zlib-gnu
+@kindex --compress-debug-sections=zlib-gabi
+@item --compress-debug-sections=none
+@itemx --compress-debug-sections=zlib
+@itemx --compress-debug-sections=zlib-gnu
+@itemx --compress-debug-sections=zlib-gabi
+On ELF platforms , these options control how DWARF debug sections are
+compressed using zlib. @option{--compress-debug-sections=none} doesn't
+compress DWARF debug sections. @option{--compress-debug-sections=zlib}
+and @option{--compress-debug-sections=zlib-gnu} compress DWARF debug
+sections and rename debug section names to begin with @samp{.zdebug}
+instead of @samp{.debug}. @option{--compress-debug-sections=zlib-gabi}
+compresses DWARF debug sections with SHF_COMPRESSED from the ELF ABI.
+
@kindex --reduce-memory-overheads
@item --reduce-memory-overheads
This option reduces memory requirements at ld runtime, at the expense of
diff --git a/ld/ldmain.c b/ld/ldmain.c
index 6674a80..224ba53 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -424,6 +424,13 @@ main (int argc, char **argv)
else
link_info.output_bfd->flags |= EXEC_P;
+ if ((link_info.compress_debug & COMPRESS_DEBUG))
+ {
+ link_info.output_bfd->flags |= BFD_COMPRESS;
+ if (link_info.compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
+ link_info.output_bfd->flags |= BFD_COMPRESS_GABI;
+ }
+
ldwrite ();
if (config.map_file != NULL)
diff --git a/ld/lexsup.c b/ld/lexsup.c
index 4a71ba4..b618241 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -1722,6 +1722,9 @@ elf_static_list_options (FILE *file)
fprintf (file, _("\
--build-id[=STYLE] Generate build ID note\n"));
fprintf (file, _("\
+ --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi]\n\
+ Compress DWARF debug sections using zlib\n"));
+ fprintf (file, _("\
-z common-page-size=SIZE Set common page size to SIZE\n"));
fprintf (file, _("\
-z max-page-size=SIZE Set maximum page size to SIZE\n"));
--
1.9.3