1 // compressed_output.cc -- manage compressed output sections for gold
3 // Copyright 2007 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.
29 #include "compressed_output.h"
30 #include "parameters.h"
35 // Compress UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE. Returns true
36 // if it successfully compressed, false if it failed for any reason
37 // (including not having zlib support in the library). If it returns
38 // true, it allocates memory for the compressed data using new, and
39 // sets *COMPRESSED_DATA and *COMPRESSED_SIZE to appropriate values.
44 zlib_compress(const char* uncompressed_data
, unsigned long uncompressed_size
,
45 char** compressed_data
, unsigned long* compressed_size
)
47 *compressed_size
= uncompressed_size
+ uncompressed_size
/ 1000 + 128;
48 *compressed_data
= new char[*compressed_size
];
51 if (parameters
->optimization_level() >= 1)
56 int rc
= compress2(reinterpret_cast<Bytef
*>(*compressed_data
),
58 reinterpret_cast<const Bytef
*>(uncompressed_data
),
65 delete[] *compressed_data
;
66 *compressed_data
= NULL
;
71 #else // !defined(HAVE_ZLIB_H)
74 zlib_compress(const char*, unsigned long, char**, unsigned long*)
79 #endif // !defined(HAVE_ZLIB_H)
81 // After compressing an output section, we rename it from foo to
82 // foo.zlib.nnnn, where nnnn is the uncompressed size of the section.
85 zlib_compressed_suffix(unsigned long uncompressed_size
)
88 snprintf(size_string
, sizeof(size_string
), "%lu", uncompressed_size
);
89 return std::string(".zlib.") + size_string
;
92 // Class Output_compressed_section_data.
94 // Add an input section. In this case, we just keep track of the sections.
97 Output_compressed_section_data::do_add_input_section(Relobj
* obj
,
100 this->objects_
.push_back(Object_entry(obj
, shndx
));
104 // Set the final data size of a compressed section. This is where
105 // we actually compress the section data.
108 Output_compressed_section_data::set_final_data_size()
110 // FIXME: assert that relocations have already been applied.
112 off_t uncompressed_size
= 0;
113 for (std::vector
<Object_entry
>::iterator it
= this->objects_
.begin();
114 it
!= this->objects_
.end();
118 = it
->object
->section_contents(it
->shndx
, &it
->length
, false);
119 uncompressed_size
+= it
->length
;
122 // (Try to) compress the data.
123 unsigned long compressed_size
;
124 char* uncompressed_data
= new char[uncompressed_size
];
126 for (std::vector
<Object_entry
>::const_iterator it
= this->objects_
.begin();
127 it
!= this->objects_
.end();
130 memcpy(uncompressed_data
+ pos
,
131 reinterpret_cast<const char*>(it
->contents
),
136 bool success
= false;
137 if (options_
.zlib_compress_debug_sections())
138 success
= zlib_compress(uncompressed_data
, uncompressed_size
,
139 &this->data_
, &compressed_size
);
142 delete[] uncompressed_data
;
143 this->set_data_size(compressed_size
);
144 this->new_section_name_
= zlib_compressed_suffix(uncompressed_size
);
148 gold_warning(_("Not compressing section data: zlib error"));
149 gold_assert(this->data_
== NULL
);
150 this->data_
= uncompressed_data
;
151 this->set_data_size(uncompressed_size
);
155 // Change the name of the output section to reflect it's compressed.
156 // The layout routines call into this right before finalizing the
160 Output_compressed_section_data::do_modified_output_section_name(
163 // This mean we never compressed the data.
164 if (this->new_section_name_
.empty())
166 this->new_section_name_
= std::string(name
) + this->new_section_name_
;
167 return this->new_section_name_
.c_str();
170 // Write out a compressed section. If we couldn't compress, we just
171 // write it out as normal, uncompressed data.
174 Output_compressed_section_data::do_write(Output_file
* of
)
176 unsigned char* uview
= of
->get_output_view(this->offset(),
178 char* view
= reinterpret_cast<char*>(uview
);
179 memcpy(view
, this->data_
, this->data_size());
180 of
->write_output_view(this->offset(), this->data_size(), uview
);
183 // Class Output_compressed_string.
185 // Add an input section. We don't do anything special here.
187 template<typename Char_type
>
189 Output_compressed_string
<Char_type
>::do_add_input_section(Relobj
* object
,
192 return Output_merge_string
<Char_type
>::do_add_input_section(object
, shndx
);
195 // Set the final data size of a compressed section. This is where
196 // we actually compress the section data.
198 template<typename Char_type
>
200 Output_compressed_string
<Char_type
>::set_final_data_size()
202 // First let the superclass finalize all its data, then write it to
204 unsigned long uncompressed_size
= this->finalize_merged_data();
205 char* uncompressed_data
= new char[uncompressed_size
];
206 this->stringpool_to_buffer(uncompressed_data
, uncompressed_size
);
208 // (Try to) compress the data.
209 unsigned long compressed_size
;
210 if (options_
.zlib_compress_debug_sections()
211 && zlib_compress(uncompressed_data
, uncompressed_size
,
212 &this->compressed_data_
, &compressed_size
))
214 this->set_data_size(compressed_size
);
216 this->clear_stringpool();
217 // We will be renaming the section to name.zlib.uncompressed_size.
218 this->new_section_name_
= zlib_compressed_suffix(uncompressed_size
);
222 this->compressed_data_
= NULL
;
223 this->set_data_size(uncompressed_size
);
226 delete[] uncompressed_data
;
229 // Change the name of the output section to reflect it's compressed.
230 // The layout routines call into this right before finalizing the
233 template<typename Char_type
>
235 Output_compressed_string
<Char_type
>::do_modified_output_section_name(
238 // This mean we never compressed the data
239 if (this->new_section_name_
.empty())
241 this->new_section_name_
= std::string(name
) + this->new_section_name_
;
242 return this->new_section_name_
.c_str();
245 // Write out a compressed string section. If we couldn't compress,
246 // we just write out the normal string section.
248 template<typename Char_type
>
250 Output_compressed_string
<Char_type
>::do_write(Output_file
* of
)
252 if (this->compressed_data_
== NULL
)
253 Output_merge_string
<Char_type
>::do_write(of
);
256 unsigned char* uview
= of
->get_output_view(this->offset(),
258 char* view
= reinterpret_cast<char*>(uview
);
259 memcpy(view
, this->compressed_data_
, this->data_size());
260 of
->write_output_view(this->offset(), this->data_size(), uview
);
264 // Instantiate the templates we need.
267 class Output_compressed_string
<char>;
270 class Output_compressed_string
<uint16_t>;
273 class Output_compressed_string
<uint32_t>;
275 } // End namespace gold.
This page took 0.059897 seconds and 5 git commands to generate.