| 1 | // compressed_output.cc -- manage compressed output sections for gold |
| 2 | |
| 3 | // Copyright 2007 Free Software Foundation, Inc. |
| 4 | // Written by Ian Lance Taylor <iant@google.com>. |
| 5 | |
| 6 | // This file is part of gold. |
| 7 | |
| 8 | // This program is free software; you can redistribute it and/or modify |
| 9 | // it under the terms of the GNU General Public License as published by |
| 10 | // the Free Software Foundation; either version 3 of the License, or |
| 11 | // (at your option) any later version. |
| 12 | |
| 13 | // This program is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | // GNU General Public License for more details. |
| 17 | |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 21 | // MA 02110-1301, USA. |
| 22 | |
| 23 | #include "gold.h" |
| 24 | |
| 25 | #ifdef HAVE_ZLIB_H |
| 26 | #include <zlib.h> |
| 27 | #endif |
| 28 | |
| 29 | #include "parameters.h" |
| 30 | #include "options.h" |
| 31 | #include "compressed_output.h" |
| 32 | |
| 33 | namespace gold |
| 34 | { |
| 35 | |
| 36 | // Compress UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE. Returns true |
| 37 | // if it successfully compressed, false if it failed for any reason |
| 38 | // (including not having zlib support in the library). If it returns |
| 39 | // true, it allocates memory for the compressed data using new, and |
| 40 | // sets *COMPRESSED_DATA and *COMPRESSED_SIZE to appropriate values. |
| 41 | |
| 42 | #ifdef HAVE_ZLIB_H |
| 43 | |
| 44 | static bool |
| 45 | zlib_compress(const char* uncompressed_data, unsigned long uncompressed_size, |
| 46 | char** compressed_data, unsigned long* compressed_size) |
| 47 | { |
| 48 | *compressed_size = uncompressed_size + uncompressed_size / 1000 + 128; |
| 49 | *compressed_data = new char[*compressed_size]; |
| 50 | |
| 51 | int compress_level; |
| 52 | if (parameters->options().optimize() >= 1) |
| 53 | compress_level = 9; |
| 54 | else |
| 55 | compress_level = 1; |
| 56 | |
| 57 | int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data), |
| 58 | compressed_size, |
| 59 | reinterpret_cast<const Bytef*>(uncompressed_data), |
| 60 | uncompressed_size, |
| 61 | compress_level); |
| 62 | if (rc == Z_OK) |
| 63 | return true; |
| 64 | else |
| 65 | { |
| 66 | delete[] *compressed_data; |
| 67 | *compressed_data = NULL; |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #else // !defined(HAVE_ZLIB_H) |
| 73 | |
| 74 | static bool |
| 75 | zlib_compress(const char*, unsigned long, char**, unsigned long*) |
| 76 | { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | #endif // !defined(HAVE_ZLIB_H) |
| 81 | |
| 82 | // After compressing an output section, we rename it from foo to |
| 83 | // foo.zlib.nnnn, where nnnn is the uncompressed size of the section. |
| 84 | |
| 85 | static std::string |
| 86 | zlib_compressed_suffix(unsigned long uncompressed_size) |
| 87 | { |
| 88 | char size_string[64]; |
| 89 | snprintf(size_string, sizeof(size_string), "%lu", uncompressed_size); |
| 90 | return std::string(".zlib.") + size_string; |
| 91 | } |
| 92 | |
| 93 | // Class Output_compressed_section. |
| 94 | |
| 95 | // Set the final data size of a compressed section. This is where |
| 96 | // we actually compress the section data. |
| 97 | |
| 98 | void |
| 99 | Output_compressed_section::set_final_data_size() |
| 100 | { |
| 101 | off_t uncompressed_size = this->postprocessing_buffer_size(); |
| 102 | |
| 103 | // (Try to) compress the data. |
| 104 | unsigned long compressed_size; |
| 105 | unsigned char* u_uncompressed_data = this->postprocessing_buffer(); |
| 106 | char* uncompressed_data = reinterpret_cast<char*>(u_uncompressed_data); |
| 107 | |
| 108 | // At this point the contents of all regular input sections will |
| 109 | // have been copied into the postprocessing buffer, and relocations |
| 110 | // will have been applied. Now we need to copy in the contents of |
| 111 | // anything other than a regular input section. |
| 112 | this->write_to_postprocessing_buffer(); |
| 113 | |
| 114 | bool success = false; |
| 115 | if (strcmp(this->options_->compress_debug_sections(), "zlib") == 0) |
| 116 | success = zlib_compress(uncompressed_data, uncompressed_size, |
| 117 | &this->data_, &compressed_size); |
| 118 | if (success) |
| 119 | { |
| 120 | std::string suffix(zlib_compressed_suffix(uncompressed_size)); |
| 121 | this->new_section_name_ = std::string(this->name()) + suffix; |
| 122 | this->set_name(this->new_section_name_.c_str()); |
| 123 | this->set_data_size(compressed_size); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | gold_warning(_("not compressing section data: zlib error")); |
| 128 | gold_assert(this->data_ == NULL); |
| 129 | this->set_data_size(uncompressed_size); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Write out a compressed section. If we couldn't compress, we just |
| 134 | // write it out as normal, uncompressed data. |
| 135 | |
| 136 | void |
| 137 | Output_compressed_section::do_write(Output_file* of) |
| 138 | { |
| 139 | off_t offset = this->offset(); |
| 140 | off_t data_size = this->data_size(); |
| 141 | unsigned char* view = of->get_output_view(offset, data_size); |
| 142 | if (this->data_ == NULL) |
| 143 | memcpy(view, this->postprocessing_buffer(), data_size); |
| 144 | else |
| 145 | memcpy(view, this->data_, data_size); |
| 146 | of->write_output_view(offset, data_size, view); |
| 147 | } |
| 148 | |
| 149 | } // End namespace gold. |