diff options
author | H.J. Lu <hjl.tools@gmail.com> | 2015-04-14 22:01:25 -0700 |
---|---|---|
committer | H.J. Lu <hjl.tools@gmail.com> | 2015-04-14 22:01:38 -0700 |
commit | 0ce398f106dac65c3a1d2f7d254213fa652af089 (patch) | |
tree | 6ab688c878a7eac3183247b29dd9add73bf1dec6 /bfd | |
parent | ecd272101d27ed8d654cbab9972aa05c124f981c (diff) | |
download | gdb-0ce398f106dac65c3a1d2f7d254213fa652af089.zip gdb-0ce398f106dac65c3a1d2f7d254213fa652af089.tar.gz gdb-0ce398f106dac65c3a1d2f7d254213fa652af089.tar.bz2 |
Add --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi]
This patch adds --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi]
to ld for ELF targets to support generating compressed DWARF debug
sections. We always generate .zdebug_* section since section names have
been finalized and they can't be changed easily when compression is
being performed.
bfd/
* bfd-in.h (compressed_debug_section_type): New.
* compress.c (bfd_compress_section_contents): Add an argument
for linker write compression and always generate .zdebug_*
section when linking.
(bfd_init_section_compress_status): Pass FALSE to
bfd_compress_section_contents.
(bfd_compress_section): New function.
* elf.c (elf_fake_sections): For linking, set SEC_ELF_COMPRESS
on DWARF debug sections if COMPRESS_DEBUG is set and rename
section if COMPRESS_DEBUG_GABI_ZLIB isn't set.
(assign_file_positions_for_non_load_sections): Set sh_offset
to -1 if SEC_ELF_COMPRESS is set.
(assign_file_positions_except_relocs): Likwise.
(_bfd_elf_assign_file_positions_for_relocs): Renamed to ...
(_bfd_elf_assign_file_positions_for_non_load): This. Change
return time to bfd_boolean. Compress the section if
SEC_ELF_COMPRESS is set.
(_bfd_elf_write_object_contents): Updated.
(_bfd_elf_set_section_contents): Write section contents to
the buffer if SEC_ELF_COMPRESS is set.
* merge.c: Include "elf-bfd.h".
(sec_merge_emit): Add arguments for contents and offset. Write
to contents with offset if contents isn't NULL.
(_bfd_write_merged_section): Write section contents to the
buffer if SEC_ELF_COMPRESS is set. Pass contents and
output_offset to sec_merge_emit.
* elflink.c (bfd_elf_final_link): Allocate the buffer for
output section contents if SEC_ELF_COMPRESS is set.
* section.c (SEC_ELF_COMPRESS): New.
* bfd-in2.h: Regenerated.
gas/
* as.h (compressed_debug_section_type): Removed.
include/
* bfdlink.h (bfd_link_info): Add compress_debug.
ld/
* ld.texinfo: Document --compress-debug-sections=.
* ldmain.c (main): Set BFD_COMPRESS on output_bfd if
COMPRESS_DEBUG is set. Set BFD_COMPRESS_GABI on output_bfd
for COMPRESS_DEBUG_GABI_ZLIB.
* lexsup.c (elf_static_list_options): Add
--compress-debug-sections=.
* emultempl/elf32.em (OPTION_COMPRESS_DEBUG): New.
(xtra_long): Add "compress-debug-sections".
(gld${EMULATION_NAME}_handle_option): Handle
OPTION_COMPRESS_DEBUG.
ld/testsuite/
* ld-elf/compress.exp (build_tests): Add tests for
--compress-debug-sections=.
(run_tests): Likewise.
Add additonal tests for --compress-debug-sections=.
* ld-elf/gabiend.rt: New file.
* ld-elf/gabinormal.rt: Likewise.
* ld-elf/gnubegin.rS: Likewise.
* ld-elf/gnunormal.rS: Likewise.
* ld-elf/zlibbegin.rS: Likewise.
* ld-elf/zlibnormal.rS: Likewise.
Diffstat (limited to 'bfd')
-rw-r--r-- | bfd/ChangeLog | 33 | ||||
-rw-r--r-- | bfd/bfd-in.h | 11 | ||||
-rw-r--r-- | bfd/bfd-in2.h | 18 | ||||
-rw-r--r-- | bfd/compress.c | 51 | ||||
-rw-r--r-- | bfd/elf.c | 89 | ||||
-rw-r--r-- | bfd/elflink.c | 15 | ||||
-rw-r--r-- | bfd/merge.c | 53 | ||||
-rw-r--r-- | bfd/section.c | 4 |
8 files changed, 253 insertions, 21 deletions
diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 17610e0..abb87dc 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,36 @@ +2015-04-14 H.J. Lu <hongjiu.lu@intel.com> + + * bfd-in.h (compressed_debug_section_type): New. + * compress.c (bfd_compress_section_contents): Add an argument + for linker write compression and always generate .zdebug_* + section when linking. + (bfd_init_section_compress_status): Pass FALSE to + bfd_compress_section_contents. + (bfd_compress_section): New function. + * elf.c (elf_fake_sections): For linking, set SEC_ELF_COMPRESS + on DWARF debug sections if COMPRESS_DEBUG is set and rename + section if COMPRESS_DEBUG_GABI_ZLIB isn't set. + (assign_file_positions_for_non_load_sections): Set sh_offset + to -1 if SEC_ELF_COMPRESS is set. + (assign_file_positions_except_relocs): Likwise. + (_bfd_elf_assign_file_positions_for_relocs): Renamed to ... + (_bfd_elf_assign_file_positions_for_non_load): This. Change + return time to bfd_boolean. Compress the section if + SEC_ELF_COMPRESS is set. + (_bfd_elf_write_object_contents): Updated. + (_bfd_elf_set_section_contents): Write section contents to + the buffer if SEC_ELF_COMPRESS is set. + * merge.c: Include "elf-bfd.h". + (sec_merge_emit): Add arguments for contents and offset. Write + to contents with offset if contents isn't NULL. + (_bfd_write_merged_section): Write section contents to the + buffer if SEC_ELF_COMPRESS is set. Pass contents and + output_offset to sec_merge_emit. + * elflink.c (bfd_elf_final_link): Allocate the buffer for + output section contents if SEC_ELF_COMPRESS is set. + * section.c (SEC_ELF_COMPRESS): New. + * bfd-in2.h: Regenerated. + 2015-04-15 Alan Modra <amodra@gmail.com> * elf32-rl78.c (rl78_elf_relocate_section): Typo fix. diff --git a/bfd/bfd-in.h b/bfd/bfd-in.h index 1f8a72c..2e324cd 100644 --- a/bfd/bfd-in.h +++ b/bfd/bfd-in.h @@ -437,6 +437,17 @@ extern void bfd_hash_traverse this size. */ extern unsigned long bfd_hash_set_default_size (unsigned long); +/* 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 is used to keep track of stabs in sections information while linking. */ diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 679595e..122caa0 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -444,6 +444,17 @@ extern void bfd_hash_traverse this size. */ extern unsigned long bfd_hash_set_default_size (unsigned long); +/* 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 is used to keep track of stabs in sections information while linking. */ @@ -1378,6 +1389,10 @@ typedef struct bfd_section executables or shared objects. This is for COFF only. */ #define SEC_COFF_SHARED 0x8000000 + /* This section should be compressed. This is for ELF linker + internal use only. */ +#define SEC_ELF_COMPRESS 0x8000000 + /* When a section with this flag is being linked, then if the size of the input section is less than a page, it should not cross a page boundary. If the size of the input section is one page or more, @@ -7316,6 +7331,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 770ea57..dfde50e 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; @@ -176,8 +177,11 @@ 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) + just keep it uncompressed. We always generate .zdebug_* section + when linking since section names have been finalized and they + can't be changed easily. */ + if ((write_compress && compression_header_size == 0) + || compressed_size < uncompressed_size) { bfd_update_compression_header (abfd, buffer, sec); @@ -543,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; +} @@ -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,38 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) this_hdr = &esd->this_hdr; + /* For linking, compress DWARF debug sections with names: .debug_*. */ + if (arg->link_info + && (arg->link_info->compress_debug & COMPRESS_DEBUG) + && (asect->flags & SEC_DEBUGGING) + && name[1] == 'd' + && name[6] == '_') + { + /* Set SEC_ELF_COMPRESS to indicate this section should be + compressed. */ + asect->flags |= SEC_ELF_COMPRESS; + + if (arg->link_info->compress_debug != COMPRESS_DEBUG_GABI_ZLIB) + { + /* If SHF_COMPRESSED isn't used, rename compressed DWARF + debug section 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; @@ -5116,6 +5147,9 @@ 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) + || (hdr->bfd_section != NULL + && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) + /* Compress DWARF debug sections. */ || hdr == i_shdrpp[elf_onesymtab (abfd)] || hdr == i_shdrpp[elf_symtab_shndx (abfd)] || hdr == i_shdrpp[elf_strtab_sec (abfd)]) @@ -5365,6 +5399,9 @@ assign_file_positions_except_relocs (bfd *abfd, hdr = *hdrpp; if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) && hdr->bfd_section == NULL) + || (hdr->bfd_section != NULL + && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) + /* Compress DWARF debug sections. */ || i == elf_onesymtab (abfd) || i == elf_symtab_shndx (abfd) || i == elf_strtab_sec (abfd)) @@ -5518,8 +5555,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 +5572,30 @@ _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) - && shdrp->sh_offset == -1) - off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); + if (shdrp->sh_offset == -1) + { + bfd_boolean is_rel = (shdrp->sh_type == SHT_REL + || shdrp->sh_type == SHT_RELA); + if (is_rel + || (shdrp->bfd_section != NULL + && (shdrp->bfd_section->flags & SEC_ELF_COMPRESS))) + { + if (!is_rel) + { + /* 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 +5605,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 +5629,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 +7983,21 @@ _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) + { + /* We must compress this section. Write output to the buffer. */ + unsigned char *contents = hdr->contents; + if ((offset + count) > hdr->sh_size + || (section->flags & SEC_ELF_COMPRESS) == 0 + || contents == NULL) + abort (); + 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/elflink.c b/bfd/elflink.c index ea9246b..6efe1e4 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -10890,6 +10890,21 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) to count upwards while actually outputting the relocations. */ esdo->rel.count = 0; esdo->rela.count = 0; + + if (esdo->this_hdr.sh_offset == (file_ptr) -1) + { + /* Cache the section contents so that they can be compressed + later. Use bfd_malloc since it will be freed by + bfd_compress_section_contents. */ + unsigned char *contents = esdo->this_hdr.contents; + if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) + abort (); + contents + = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); + if (contents == NULL) + goto error_return; + esdo->this_hdr.contents = contents; + } } /* We have now assigned file positions for all the sections except diff --git a/bfd/merge.c b/bfd/merge.c index 5f45ba6..174ec90 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" @@ -283,7 +284,8 @@ sec_merge_add (struct sec_merge_hash *tab, const char *str, } static bfd_boolean -sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry) +sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry, + unsigned char *contents, file_ptr offset) { struct sec_merge_sec_info *secinfo = entry->secinfo; asection *sec = secinfo->sec; @@ -306,7 +308,12 @@ sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry) len = -off & (entry->alignment - 1); if (len != 0) { - if (bfd_bwrite (pad, len, abfd) != len) + if (contents) + { + memcpy (contents + offset, pad, len); + offset += len; + } + else if (bfd_bwrite (pad, len, abfd) != len) goto err; off += len; } @@ -314,7 +321,12 @@ 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 + offset, str, len); + offset += len; + } + else if (bfd_bwrite (str, len, abfd) != len) goto err; off += len; @@ -322,9 +334,13 @@ sec_merge_emit (bfd *abfd, struct sec_merge_hash_entry *entry) /* Trailing alignment needed? */ off = sec->size - off; - if (off != 0 - && bfd_bwrite (pad, off, abfd) != off) - goto err; + if (off != 0) + { + if (contents) + memcpy (contents + offset, pad, off); + else if (bfd_bwrite (pad, off, abfd) != off) + goto err; + } if (pad != NULL) free (pad); @@ -785,6 +801,8 @@ _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo) { struct sec_merge_sec_info *secinfo; file_ptr pos; + unsigned char *contents; + Elf_Internal_Shdr *hdr; secinfo = (struct sec_merge_sec_info *) psecinfo; @@ -795,11 +813,26 @@ _bfd_write_merged_section (bfd *output_bfd, asection *sec, void *psecinfo) return TRUE; /* FIXME: octets_per_byte. */ - pos = sec->output_section->filepos + sec->output_offset; - if (bfd_seek (output_bfd, pos, SEEK_SET) != 0) - return FALSE; + hdr = &elf_section_data (sec->output_section)->this_hdr; + if (hdr->sh_offset == (file_ptr) -1) + { + /* We must compress this section. Write output to the + buffer. */ + contents = hdr->contents; + if ((sec->output_section->flags & SEC_ELF_COMPRESS) == 0 + || contents == NULL) + abort (); + } + else + { + 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)) + if (! sec_merge_emit (output_bfd, secinfo->first_str, contents, + sec->output_offset)) return FALSE; return TRUE; diff --git a/bfd/section.c b/bfd/section.c index 4a6f2e4..d59a0e3 100644 --- a/bfd/section.c +++ b/bfd/section.c @@ -334,6 +334,10 @@ CODE_FRAGMENT . executables or shared objects. This is for COFF only. *} .#define SEC_COFF_SHARED 0x8000000 . +. {* This section should be compressed. This is for ELF linker +. internal use only. *} +.#define SEC_ELF_COMPRESS 0x8000000 +. . {* When a section with this flag is being linked, then if the size of . the input section is less than a page, it should not cross a page . boundary. If the size of the input section is one page or more, |