Fix typo in ChangeLog entry.
[binutils.git] / gold / compressed_output.cc
bloba0f8ed181ace455f9db37188f9fa3a1822561ed4
1 // compressed_output.cc -- manage compressed output sections for gold
3 // Copyright 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #ifdef HAVE_ZLIB_H
26 #include <zlib.h>
27 #endif
29 #include "parameters.h"
30 #include "options.h"
31 #include "compressed_output.h"
33 namespace gold
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 // It also writes a header before COMPRESSED_DATA: 4 bytes saying
42 // "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian
43 // order.
45 #ifdef HAVE_ZLIB_H
47 static bool
48 zlib_compress(const unsigned char* uncompressed_data,
49 unsigned long uncompressed_size,
50 unsigned char** compressed_data,
51 unsigned long* compressed_size)
53 const int header_size = 12;
54 *compressed_size = uncompressed_size + uncompressed_size / 1000 + 128;
55 *compressed_data = new unsigned char[*compressed_size + header_size];
57 int compress_level;
58 if (parameters->options().optimize() >= 1)
59 compress_level = 9;
60 else
61 compress_level = 1;
63 int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data) + header_size,
64 compressed_size,
65 reinterpret_cast<const Bytef*>(uncompressed_data),
66 uncompressed_size,
67 compress_level);
68 if (rc == Z_OK)
70 memcpy(*compressed_data, "ZLIB", 4);
71 elfcpp::Swap_unaligned<64, true>::writeval(*compressed_data + 4,
72 uncompressed_size);
73 *compressed_size += header_size;
74 return true;
76 else
78 delete[] *compressed_data;
79 *compressed_data = NULL;
80 return false;
84 #else // !defined(HAVE_ZLIB_H)
86 static bool
87 zlib_compress(const unsigned char*, unsigned long,
88 unsigned char**, unsigned long*)
90 return false;
93 #endif // !defined(HAVE_ZLIB_H)
95 // Class Output_compressed_section.
97 // Set the final data size of a compressed section. This is where
98 // we actually compress the section data.
100 void
101 Output_compressed_section::set_final_data_size()
103 off_t uncompressed_size = this->postprocessing_buffer_size();
105 // (Try to) compress the data.
106 unsigned long compressed_size;
107 unsigned char* uncompressed_data = this->postprocessing_buffer();
109 // At this point the contents of all regular input sections will
110 // have been copied into the postprocessing buffer, and relocations
111 // will have been applied. Now we need to copy in the contents of
112 // anything other than a regular input section.
113 this->write_to_postprocessing_buffer();
115 bool success = false;
116 if (strcmp(this->options_->compress_debug_sections(), "zlib") == 0)
117 success = zlib_compress(uncompressed_data, uncompressed_size,
118 &this->data_, &compressed_size);
119 if (success)
121 // This converts .debug_foo to .zdebug_foo
122 this->new_section_name_ = std::string(".z") + (this->name() + 1);
123 this->set_name(this->new_section_name_.c_str());
124 this->set_data_size(compressed_size);
126 else
128 gold_warning(_("not compressing section data: zlib error"));
129 gold_assert(this->data_ == NULL);
130 this->set_data_size(uncompressed_size);
134 // Write out a compressed section. If we couldn't compress, we just
135 // write it out as normal, uncompressed data.
137 void
138 Output_compressed_section::do_write(Output_file* of)
140 off_t offset = this->offset();
141 off_t data_size = this->data_size();
142 unsigned char* view = of->get_output_view(offset, data_size);
143 if (this->data_ == NULL)
144 memcpy(view, this->postprocessing_buffer(), data_size);
145 else
146 memcpy(view, this->data_, data_size);
147 of->write_output_view(offset, data_size, view);
150 } // End namespace gold.