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