Commit | Line | Data |
---|---|---|
a2e47362 | 1 | // compressed_output.cc -- manage compressed debug sections for gold |
9a0910c3 | 2 | |
219d1afa | 3 | // Copyright (C) 2007-2018 Free Software Foundation, Inc. |
9a0910c3 ILT |
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" | |
9a0910c3 | 24 | #include <zlib.h> |
9a0910c3 | 25 | #include "parameters.h" |
14144f39 | 26 | #include "options.h" |
96803768 | 27 | #include "compressed_output.h" |
9a0910c3 ILT |
28 | |
29 | namespace gold | |
30 | { | |
31 | ||
32 | // Compress UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE. Returns true | |
33 | // if it successfully compressed, false if it failed for any reason | |
34 | // (including not having zlib support in the library). If it returns | |
35 | // true, it allocates memory for the compressed data using new, and | |
36 | // sets *COMPRESSED_DATA and *COMPRESSED_SIZE to appropriate values. | |
126f3ece ILT |
37 | // It also writes a header before COMPRESSED_DATA: 4 bytes saying |
38 | // "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian | |
39 | // order. | |
9a0910c3 ILT |
40 | |
41 | static bool | |
fb8b9dbc L |
42 | zlib_compress(int header_size, |
43 | const unsigned char* uncompressed_data, | |
126f3ece ILT |
44 | unsigned long uncompressed_size, |
45 | unsigned char** compressed_data, | |
46 | unsigned long* compressed_size) | |
9a0910c3 | 47 | { |
9a0910c3 | 48 | *compressed_size = uncompressed_size + uncompressed_size / 1000 + 128; |
126f3ece | 49 | *compressed_data = new unsigned char[*compressed_size + header_size]; |
9a0910c3 ILT |
50 | |
51 | int compress_level; | |
8851ecca | 52 | if (parameters->options().optimize() >= 1) |
9a0910c3 ILT |
53 | compress_level = 9; |
54 | else | |
55 | compress_level = 1; | |
56 | ||
126f3ece | 57 | int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data) + header_size, |
9a0910c3 ILT |
58 | compressed_size, |
59 | reinterpret_cast<const Bytef*>(uncompressed_data), | |
60 | uncompressed_size, | |
61 | compress_level); | |
62 | if (rc == Z_OK) | |
126f3ece | 63 | { |
126f3ece ILT |
64 | *compressed_size += header_size; |
65 | return true; | |
66 | } | |
9a0910c3 ILT |
67 | else |
68 | { | |
69 | delete[] *compressed_data; | |
70 | *compressed_data = NULL; | |
71 | return false; | |
72 | } | |
9a0910c3 ILT |
73 | } |
74 | ||
a2e47362 CC |
75 | // Decompress COMPRESSED_DATA of size COMPRESSED_SIZE, into a buffer |
76 | // UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE. Returns TRUE if it | |
77 | // decompressed successfully, false if it failed. The buffer, of | |
78 | // appropriate size, is provided by the caller, and is typically part | |
79 | // of the memory-mapped output file. | |
80 | ||
81 | static bool | |
82 | zlib_decompress(const unsigned char* compressed_data, | |
83 | unsigned long compressed_size, | |
84 | unsigned char* uncompressed_data, | |
85 | unsigned long uncompressed_size) | |
86 | { | |
87 | z_stream strm; | |
88 | int rc; | |
89 | ||
90 | /* It is possible the section consists of several compressed | |
91 | buffers concatenated together, so we uncompress in a loop. */ | |
92 | strm.zalloc = NULL; | |
93 | strm.zfree = NULL; | |
94 | strm.opaque = NULL; | |
95 | strm.avail_in = compressed_size; | |
96 | strm.next_in = const_cast<Bytef*>(compressed_data); | |
97 | strm.avail_out = uncompressed_size; | |
98 | ||
99 | rc = inflateInit(&strm); | |
100 | while (strm.avail_in > 0) | |
101 | { | |
102 | if (rc != Z_OK) | |
103 | return false; | |
104 | strm.next_out = ((Bytef*) uncompressed_data | |
105 | + (uncompressed_size - strm.avail_out)); | |
106 | rc = inflate(&strm, Z_FINISH); | |
107 | if (rc != Z_STREAM_END) | |
108 | return false; | |
109 | rc = inflateReset(&strm); | |
110 | } | |
111 | rc = inflateEnd(&strm); | |
112 | if (rc != Z_OK || strm.avail_out != 0) | |
113 | return false; | |
114 | ||
115 | return true; | |
116 | } | |
117 | ||
a2e47362 CC |
118 | // Read the compression header of a compressed debug section and return |
119 | // the uncompressed size. | |
120 | ||
121 | uint64_t | |
122 | get_uncompressed_size(const unsigned char* compressed_data, | |
123 | section_size_type compressed_size) | |
124 | { | |
125 | const unsigned int zlib_header_size = 12; | |
126 | ||
127 | /* Verify the compression header. Currently, we support only zlib | |
128 | compression, so it should be "ZLIB" followed by the uncompressed | |
129 | section size, 8 bytes in big-endian order. */ | |
130 | if (compressed_size >= zlib_header_size | |
131 | && strncmp(reinterpret_cast<const char*>(compressed_data), | |
132 | "ZLIB", 4) == 0) | |
133 | return elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4); | |
134 | return -1ULL; | |
135 | } | |
136 | ||
137 | // Decompress a compressed debug section directly into the output file. | |
138 | ||
139 | bool | |
140 | decompress_input_section(const unsigned char* compressed_data, | |
141 | unsigned long compressed_size, | |
142 | unsigned char* uncompressed_data, | |
48058663 L |
143 | unsigned long uncompressed_size, |
144 | int size, | |
145 | bool big_endian, | |
146 | elfcpp::Elf_Xword sh_flags) | |
a2e47362 | 147 | { |
48058663 L |
148 | if ((sh_flags & elfcpp::SHF_COMPRESSED) != 0) |
149 | { | |
150 | unsigned int compression_header_size; | |
151 | if (size == 32) | |
152 | { | |
153 | compression_header_size = elfcpp::Elf_sizes<32>::chdr_size; | |
154 | if (big_endian) | |
155 | { | |
156 | elfcpp::Chdr<32, true> chdr(compressed_data); | |
157 | if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB) | |
158 | return false; | |
159 | } | |
160 | else | |
161 | { | |
162 | elfcpp::Chdr<32, false> chdr(compressed_data); | |
163 | if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB) | |
164 | return false; | |
165 | } | |
166 | } | |
167 | else if (size == 64) | |
168 | { | |
169 | compression_header_size = elfcpp::Elf_sizes<64>::chdr_size; | |
170 | if (big_endian) | |
171 | { | |
172 | elfcpp::Chdr<64, true> chdr(compressed_data); | |
173 | if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB) | |
174 | return false; | |
175 | } | |
176 | else | |
177 | { | |
178 | elfcpp::Chdr<64, false> chdr(compressed_data); | |
179 | if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB) | |
180 | return false; | |
181 | } | |
182 | } | |
183 | else | |
184 | gold_unreachable(); | |
185 | ||
186 | return zlib_decompress(compressed_data + compression_header_size, | |
187 | compressed_size - compression_header_size, | |
188 | uncompressed_data, | |
189 | uncompressed_size); | |
190 | } | |
191 | ||
a2e47362 CC |
192 | const unsigned int zlib_header_size = 12; |
193 | ||
194 | /* Verify the compression header. Currently, we support only zlib | |
195 | compression, so it should be "ZLIB" followed by the uncompressed | |
196 | section size, 8 bytes in big-endian order. */ | |
197 | if (compressed_size >= zlib_header_size | |
198 | && strncmp(reinterpret_cast<const char*>(compressed_data), | |
199 | "ZLIB", 4) == 0) | |
200 | { | |
201 | unsigned long uncompressed_size_check = | |
202 | elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4); | |
203 | gold_assert(uncompressed_size_check == uncompressed_size); | |
204 | return zlib_decompress(compressed_data + zlib_header_size, | |
205 | compressed_size - zlib_header_size, | |
206 | uncompressed_data, | |
207 | uncompressed_size); | |
208 | } | |
209 | return false; | |
210 | } | |
211 | ||
96803768 | 212 | // Class Output_compressed_section. |
9a0910c3 ILT |
213 | |
214 | // Set the final data size of a compressed section. This is where | |
215 | // we actually compress the section data. | |
216 | ||
217 | void | |
96803768 | 218 | Output_compressed_section::set_final_data_size() |
9a0910c3 | 219 | { |
96803768 | 220 | off_t uncompressed_size = this->postprocessing_buffer_size(); |
9a0910c3 ILT |
221 | |
222 | // (Try to) compress the data. | |
223 | unsigned long compressed_size; | |
126f3ece | 224 | unsigned char* uncompressed_data = this->postprocessing_buffer(); |
96803768 ILT |
225 | |
226 | // At this point the contents of all regular input sections will | |
227 | // have been copied into the postprocessing buffer, and relocations | |
228 | // will have been applied. Now we need to copy in the contents of | |
229 | // anything other than a regular input section. | |
230 | this->write_to_postprocessing_buffer(); | |
9a0910c3 ILT |
231 | |
232 | bool success = false; | |
fb8b9dbc L |
233 | enum { none, gnu_zlib, gabi_zlib } compress; |
234 | int compression_header_size = 12; | |
235 | const int size = parameters->target().get_size(); | |
19a7fe52 | 236 | if (strcmp(this->options_->compress_debug_sections(), "zlib-gnu") == 0) |
fb8b9dbc | 237 | compress = gnu_zlib; |
19a7fe52 L |
238 | else if (strcmp(this->options_->compress_debug_sections(), "zlib-gabi") == 0 |
239 | || strcmp(this->options_->compress_debug_sections(), "zlib") == 0) | |
fb8b9dbc L |
240 | { |
241 | compress = gabi_zlib; | |
242 | if (size == 32) | |
243 | compression_header_size = elfcpp::Elf_sizes<32>::chdr_size; | |
244 | else if (size == 64) | |
245 | compression_header_size = elfcpp::Elf_sizes<64>::chdr_size; | |
246 | else | |
247 | gold_unreachable(); | |
248 | } | |
249 | else | |
250 | compress = none; | |
251 | if (compress != none) | |
252 | success = zlib_compress(compression_header_size, uncompressed_data, | |
253 | uncompressed_size, &this->data_, | |
254 | &compressed_size); | |
9a0910c3 ILT |
255 | if (success) |
256 | { | |
fb8b9dbc L |
257 | elfcpp::Elf_Xword flags = this->flags(); |
258 | if (compress == gabi_zlib) | |
259 | { | |
260 | // Set the SHF_COMPRESSED bit. | |
261 | flags |= elfcpp::SHF_COMPRESSED; | |
262 | const bool is_big_endian = parameters->target().is_big_endian(); | |
263 | uint64_t addralign = this->addralign(); | |
264 | if (size == 32) | |
265 | { | |
266 | if (is_big_endian) | |
267 | { | |
268 | elfcpp::Chdr_write<32, true> chdr(this->data_); | |
269 | chdr.put_ch_type(elfcpp::ELFCOMPRESS_ZLIB); | |
270 | chdr.put_ch_size(uncompressed_size); | |
271 | chdr.put_ch_addralign(addralign); | |
272 | } | |
273 | else | |
274 | { | |
275 | elfcpp::Chdr_write<32, false> chdr(this->data_); | |
276 | chdr.put_ch_type(elfcpp::ELFCOMPRESS_ZLIB); | |
277 | chdr.put_ch_size(uncompressed_size); | |
278 | chdr.put_ch_addralign(addralign); | |
279 | } | |
280 | } | |
281 | else if (size == 64) | |
282 | { | |
283 | if (is_big_endian) | |
284 | { | |
285 | elfcpp::Chdr_write<64, true> chdr(this->data_); | |
286 | chdr.put_ch_type(elfcpp::ELFCOMPRESS_ZLIB); | |
287 | chdr.put_ch_size(uncompressed_size); | |
288 | chdr.put_ch_addralign(addralign); | |
49ba15a2 L |
289 | // Clear the reserved field. |
290 | chdr.put_ch_reserved(0); | |
fb8b9dbc L |
291 | } |
292 | else | |
293 | { | |
294 | elfcpp::Chdr_write<64, false> chdr(this->data_); | |
295 | chdr.put_ch_type(elfcpp::ELFCOMPRESS_ZLIB); | |
296 | chdr.put_ch_size(uncompressed_size); | |
297 | chdr.put_ch_addralign(addralign); | |
49ba15a2 L |
298 | // Clear the reserved field. |
299 | chdr.put_ch_reserved(0); | |
fb8b9dbc L |
300 | } |
301 | } | |
302 | else | |
303 | gold_unreachable(); | |
304 | } | |
305 | else | |
306 | { | |
307 | // Write out the zlib header. | |
308 | memcpy(this->data_, "ZLIB", 4); | |
309 | elfcpp::Swap_unaligned<64, true>::writeval(this->data_ + 4, | |
310 | uncompressed_size); | |
311 | // This converts .debug_foo to .zdebug_foo | |
312 | this->new_section_name_ = std::string(".z") + (this->name() + 1); | |
313 | this->set_name(this->new_section_name_.c_str()); | |
314 | } | |
315 | this->set_flags(flags); | |
9a0910c3 | 316 | this->set_data_size(compressed_size); |
9a0910c3 ILT |
317 | } |
318 | else | |
319 | { | |
96803768 | 320 | gold_warning(_("not compressing section data: zlib error")); |
9a0910c3 | 321 | gold_assert(this->data_ == NULL); |
9a0910c3 ILT |
322 | this->set_data_size(uncompressed_size); |
323 | } | |
324 | } | |
325 | ||
9a0910c3 ILT |
326 | // Write out a compressed section. If we couldn't compress, we just |
327 | // write it out as normal, uncompressed data. | |
328 | ||
329 | void | |
96803768 | 330 | Output_compressed_section::do_write(Output_file* of) |
9a0910c3 | 331 | { |
2ea97941 ILT |
332 | off_t offset = this->offset(); |
333 | off_t data_size = this->data_size(); | |
334 | unsigned char* view = of->get_output_view(offset, data_size); | |
96803768 | 335 | if (this->data_ == NULL) |
2ea97941 | 336 | memcpy(view, this->postprocessing_buffer(), data_size); |
9a0910c3 | 337 | else |
2ea97941 ILT |
338 | memcpy(view, this->data_, data_size); |
339 | of->write_output_view(offset, data_size, view); | |
9a0910c3 ILT |
340 | } |
341 | ||
9a0910c3 | 342 | } // End namespace gold. |