X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=gold%2Fcompressed_output.cc;h=a8db3b6f04b5d3dfac6f07fde29167d203fde49e;hb=053af8c9034f92d6e36a1180655ba22a65c56437;hp=0bbcb559f36f67422d951083f75a5079664d481b;hpb=45aa233bdcc27243a2b3e2c699ad0ab59f83901b;p=deliverable%2Fbinutils-gdb.git diff --git a/gold/compressed_output.cc b/gold/compressed_output.cc index 0bbcb559f3..a8db3b6f04 100644 --- a/gold/compressed_output.cc +++ b/gold/compressed_output.cc @@ -1,6 +1,6 @@ -// compressed_output.cc -- manage compressed output sections for gold +// compressed_output.cc -- manage compressed debug sections for gold -// Copyright 2007 Free Software Foundation, Inc. +// Copyright (C) 2007-2019 Free Software Foundation, Inc. // Written by Ian Lance Taylor . // This file is part of gold. @@ -21,11 +21,7 @@ // MA 02110-1301, USA. #include "gold.h" - -#ifdef HAVE_ZLIB_H #include -#endif - #include "parameters.h" #include "options.h" #include "compressed_output.h" @@ -38,29 +34,36 @@ namespace gold // (including not having zlib support in the library). If it returns // true, it allocates memory for the compressed data using new, and // sets *COMPRESSED_DATA and *COMPRESSED_SIZE to appropriate values. - -#ifdef HAVE_ZLIB_H +// It also writes a header before COMPRESSED_DATA: 4 bytes saying +// "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian +// order. static bool -zlib_compress(const char* uncompressed_data, unsigned long uncompressed_size, - char** compressed_data, unsigned long* compressed_size) +zlib_compress(int header_size, + const unsigned char* uncompressed_data, + unsigned long uncompressed_size, + unsigned char** compressed_data, + unsigned long* compressed_size) { *compressed_size = uncompressed_size + uncompressed_size / 1000 + 128; - *compressed_data = new char[*compressed_size]; + *compressed_data = new unsigned char[*compressed_size + header_size]; int compress_level; - if (parameters->optimize() >= 1) + if (parameters->options().optimize() >= 1) compress_level = 9; else compress_level = 1; - int rc = compress2(reinterpret_cast(*compressed_data), + int rc = compress2(reinterpret_cast(*compressed_data) + header_size, compressed_size, reinterpret_cast(uncompressed_data), uncompressed_size, compress_level); if (rc == Z_OK) - return true; + { + *compressed_size += header_size; + return true; + } else { delete[] *compressed_data; @@ -69,25 +72,141 @@ zlib_compress(const char* uncompressed_data, unsigned long uncompressed_size, } } -#else // !defined(HAVE_ZLIB_H) +// Decompress COMPRESSED_DATA of size COMPRESSED_SIZE, into a buffer +// UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE. Returns TRUE if it +// decompressed successfully, false if it failed. The buffer, of +// appropriate size, is provided by the caller, and is typically part +// of the memory-mapped output file. static bool -zlib_compress(const char*, unsigned long, char**, unsigned long*) +zlib_decompress(const unsigned char* compressed_data, + unsigned long compressed_size, + unsigned char* uncompressed_data, + unsigned long uncompressed_size) { - return false; + z_stream strm; + int rc; + + /* It is possible the section consists of several compressed + buffers concatenated together, so we uncompress in a loop. */ + strm.zalloc = NULL; + strm.zfree = NULL; + strm.opaque = NULL; + strm.avail_in = compressed_size; + strm.next_in = const_cast(compressed_data); + strm.avail_out = uncompressed_size; + + rc = inflateInit(&strm); + while (strm.avail_in > 0) + { + if (rc != Z_OK) + return false; + strm.next_out = ((Bytef*) uncompressed_data + + (uncompressed_size - strm.avail_out)); + rc = inflate(&strm, Z_FINISH); + if (rc != Z_STREAM_END) + return false; + rc = inflateReset(&strm); + } + rc = inflateEnd(&strm); + if (rc != Z_OK || strm.avail_out != 0) + return false; + + return true; } -#endif // !defined(HAVE_ZLIB_H) +// Read the compression header of a compressed debug section and return +// the uncompressed size. + +uint64_t +get_uncompressed_size(const unsigned char* compressed_data, + section_size_type compressed_size) +{ + const unsigned int zlib_header_size = 12; + + /* Verify the compression header. Currently, we support only zlib + compression, so it should be "ZLIB" followed by the uncompressed + section size, 8 bytes in big-endian order. */ + if (compressed_size >= zlib_header_size + && strncmp(reinterpret_cast(compressed_data), + "ZLIB", 4) == 0) + return elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4); + return -1ULL; +} -// After compressing an output section, we rename it from foo to -// foo.zlib.nnnn, where nnnn is the uncompressed size of the section. +// Decompress a compressed debug section directly into the output file. -static std::string -zlib_compressed_suffix(unsigned long uncompressed_size) +bool +decompress_input_section(const unsigned char* compressed_data, + unsigned long compressed_size, + unsigned char* uncompressed_data, + unsigned long uncompressed_size, + int size, + bool big_endian, + elfcpp::Elf_Xword sh_flags) { - char size_string[64]; - snprintf(size_string, sizeof(size_string), "%lu", uncompressed_size); - return std::string(".zlib.") + size_string; + if ((sh_flags & elfcpp::SHF_COMPRESSED) != 0) + { + unsigned int compression_header_size; + if (size == 32) + { + compression_header_size = elfcpp::Elf_sizes<32>::chdr_size; + if (big_endian) + { + elfcpp::Chdr<32, true> chdr(compressed_data); + if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB) + return false; + } + else + { + elfcpp::Chdr<32, false> chdr(compressed_data); + if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB) + return false; + } + } + else if (size == 64) + { + compression_header_size = elfcpp::Elf_sizes<64>::chdr_size; + if (big_endian) + { + elfcpp::Chdr<64, true> chdr(compressed_data); + if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB) + return false; + } + else + { + elfcpp::Chdr<64, false> chdr(compressed_data); + if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB) + return false; + } + } + else + gold_unreachable(); + + return zlib_decompress(compressed_data + compression_header_size, + compressed_size - compression_header_size, + uncompressed_data, + uncompressed_size); + } + + const unsigned int zlib_header_size = 12; + + /* Verify the compression header. Currently, we support only zlib + compression, so it should be "ZLIB" followed by the uncompressed + section size, 8 bytes in big-endian order. */ + if (compressed_size >= zlib_header_size + && strncmp(reinterpret_cast(compressed_data), + "ZLIB", 4) == 0) + { + unsigned long uncompressed_size_check = + elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4); + gold_assert(uncompressed_size_check == uncompressed_size); + return zlib_decompress(compressed_data + zlib_header_size, + compressed_size - zlib_header_size, + uncompressed_data, + uncompressed_size); + } + return false; } // Class Output_compressed_section. @@ -102,8 +221,7 @@ Output_compressed_section::set_final_data_size() // (Try to) compress the data. unsigned long compressed_size; - unsigned char* u_uncompressed_data = this->postprocessing_buffer(); - char* uncompressed_data = reinterpret_cast(u_uncompressed_data); + unsigned char* uncompressed_data = this->postprocessing_buffer(); // At this point the contents of all regular input sections will // have been copied into the postprocessing buffer, and relocations @@ -112,14 +230,89 @@ Output_compressed_section::set_final_data_size() this->write_to_postprocessing_buffer(); bool success = false; - if (this->options_->zlib_compress_debug_sections()) - success = zlib_compress(uncompressed_data, uncompressed_size, - &this->data_, &compressed_size); + enum { none, gnu_zlib, gabi_zlib } compress; + int compression_header_size = 12; + const int size = parameters->target().get_size(); + if (strcmp(this->options_->compress_debug_sections(), "zlib-gnu") == 0) + compress = gnu_zlib; + else if (strcmp(this->options_->compress_debug_sections(), "zlib-gabi") == 0 + || strcmp(this->options_->compress_debug_sections(), "zlib") == 0) + { + compress = gabi_zlib; + if (size == 32) + compression_header_size = elfcpp::Elf_sizes<32>::chdr_size; + else if (size == 64) + compression_header_size = elfcpp::Elf_sizes<64>::chdr_size; + else + gold_unreachable(); + } + else + compress = none; + if (compress != none) + success = zlib_compress(compression_header_size, uncompressed_data, + uncompressed_size, &this->data_, + &compressed_size); if (success) { - std::string suffix(zlib_compressed_suffix(uncompressed_size)); - this->new_section_name_ = std::string(this->name()) + suffix; - this->set_name(this->new_section_name_.c_str()); + elfcpp::Elf_Xword flags = this->flags(); + if (compress == gabi_zlib) + { + // Set the SHF_COMPRESSED bit. + flags |= elfcpp::SHF_COMPRESSED; + const bool is_big_endian = parameters->target().is_big_endian(); + uint64_t addralign = this->addralign(); + if (size == 32) + { + if (is_big_endian) + { + elfcpp::Chdr_write<32, true> chdr(this->data_); + chdr.put_ch_type(elfcpp::ELFCOMPRESS_ZLIB); + chdr.put_ch_size(uncompressed_size); + chdr.put_ch_addralign(addralign); + } + else + { + elfcpp::Chdr_write<32, false> chdr(this->data_); + chdr.put_ch_type(elfcpp::ELFCOMPRESS_ZLIB); + chdr.put_ch_size(uncompressed_size); + chdr.put_ch_addralign(addralign); + } + } + else if (size == 64) + { + if (is_big_endian) + { + elfcpp::Chdr_write<64, true> chdr(this->data_); + chdr.put_ch_type(elfcpp::ELFCOMPRESS_ZLIB); + chdr.put_ch_size(uncompressed_size); + chdr.put_ch_addralign(addralign); + // Clear the reserved field. + chdr.put_ch_reserved(0); + } + else + { + elfcpp::Chdr_write<64, false> chdr(this->data_); + chdr.put_ch_type(elfcpp::ELFCOMPRESS_ZLIB); + chdr.put_ch_size(uncompressed_size); + chdr.put_ch_addralign(addralign); + // Clear the reserved field. + chdr.put_ch_reserved(0); + } + } + else + gold_unreachable(); + } + else + { + // Write out the zlib header. + memcpy(this->data_, "ZLIB", 4); + elfcpp::Swap_unaligned<64, true>::writeval(this->data_ + 4, + uncompressed_size); + // This converts .debug_foo to .zdebug_foo + this->new_section_name_ = std::string(".z") + (this->name() + 1); + this->set_name(this->new_section_name_.c_str()); + } + this->set_flags(flags); this->set_data_size(compressed_size); } else