1 // compressed_output.cc -- manage compressed debug sections for gold
3 // Copyright (C) 2007-2024 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.
28 #include "parameters.h"
30 #include "compressed_output.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.
40 // It also writes a header before COMPRESSED_DATA: 4 bytes saying
41 // "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian
45 zlib_compress(int header_size
,
46 const unsigned char* uncompressed_data
,
47 unsigned long uncompressed_size
,
48 unsigned char** compressed_data
,
49 unsigned long* compressed_size
)
51 *compressed_size
= uncompressed_size
+ uncompressed_size
/ 1000 + 128;
52 *compressed_data
= new unsigned char[*compressed_size
+ header_size
];
55 if (parameters
->options().optimize() >= 1)
60 int rc
= compress2(reinterpret_cast<Bytef
*>(*compressed_data
) + header_size
,
62 reinterpret_cast<const Bytef
*>(uncompressed_data
),
67 *compressed_size
+= header_size
;
72 delete[] *compressed_data
;
73 *compressed_data
= NULL
;
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
)
84 size_t size
= ZSTD_compressBound(uncompressed_size
);
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
))
90 delete[] *compressed_data
;
93 *compressed_size
= header_size
+ size
;
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.
105 zlib_decompress(const unsigned char* compressed_data
,
106 unsigned long compressed_size
,
107 unsigned char* uncompressed_data
,
108 unsigned long uncompressed_size
)
113 /* It is possible the section consists of several compressed
114 buffers concatenated together, so we uncompress in a loop. */
118 strm
.avail_in
= compressed_size
;
119 strm
.next_in
= const_cast<Bytef
*>(compressed_data
);
120 strm
.avail_out
= uncompressed_size
;
122 rc
= inflateInit(&strm
);
123 while (strm
.avail_in
> 0)
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
)
132 rc
= inflateReset(&strm
);
134 rc
= inflateEnd(&strm
);
135 if (rc
!= Z_OK
|| strm
.avail_out
!= 0)
141 // Read the compression header of a compressed debug section and return
142 // the uncompressed size.
145 get_uncompressed_size(const unsigned char* compressed_data
,
146 section_size_type compressed_size
)
148 const unsigned int zlib_header_size
= 12;
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
),
156 return elfcpp::Swap_unaligned
<64, true>::readval(compressed_data
+ 4);
160 // Decompress a compressed debug section directly into the output file.
163 decompress_input_section(const unsigned char* compressed_data
,
164 unsigned long compressed_size
,
165 unsigned char* uncompressed_data
,
166 unsigned long uncompressed_size
,
169 elfcpp::Elf_Xword sh_flags
)
171 if ((sh_flags
& elfcpp::SHF_COMPRESSED
) != 0)
173 unsigned int compression_header_size
;
174 unsigned int ch_type
;
177 compression_header_size
= elfcpp::Elf_sizes
<32>::chdr_size
;
179 ch_type
= elfcpp::Chdr
<32, true> (compressed_data
).get_ch_type();
181 ch_type
= elfcpp::Chdr
<32, false>(compressed_data
).get_ch_type();
185 compression_header_size
= elfcpp::Elf_sizes
<64>::chdr_size
;
187 ch_type
= elfcpp::Chdr
<64, true>(compressed_data
).get_ch_type();
189 ch_type
= elfcpp::Chdr
<64, false>(compressed_data
).get_ch_type();
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
));
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
);
208 const unsigned int zlib_header_size
= 12;
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
),
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
,
228 // Class Output_compressed_section.
230 // Set the final data size of a compressed section. This is where
231 // we actually compress the section data.
234 Output_compressed_section::set_final_data_size()
236 off_t uncompressed_size
= this->postprocessing_buffer_size();
238 // (Try to) compress the data.
239 unsigned long compressed_size
;
240 unsigned char* uncompressed_data
= this->postprocessing_buffer();
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();
248 bool success
= false;
249 enum { none
, gnu_zlib
, gabi_zlib
, zstd
} compress
;
250 int compression_header_size
= 12;
251 const int size
= parameters
->target().get_size();
252 if (strcmp(this->options_
->compress_debug_sections(), "zlib-gnu") == 0)
254 else if (strcmp(this->options_
->compress_debug_sections(), "none") == 0)
258 if (strcmp(this->options_
->compress_debug_sections(), "zstd") == 0)
261 compress
= gabi_zlib
;
263 compression_header_size
= elfcpp::Elf_sizes
<32>::chdr_size
;
265 compression_header_size
= elfcpp::Elf_sizes
<64>::chdr_size
;
269 if (compress
== gnu_zlib
|| compress
== gabi_zlib
)
270 success
= zlib_compress(compression_header_size
, uncompressed_data
,
271 uncompressed_size
, &this->data_
,
274 else if (compress
== zstd
)
275 success
= zstd_compress(compression_header_size
, uncompressed_data
,
276 uncompressed_size
, &this->data_
,
281 elfcpp::Elf_Xword flags
= this->flags();
282 if (compress
== gabi_zlib
|| compress
== zstd
)
284 // Set the SHF_COMPRESSED bit.
285 flags
|= elfcpp::SHF_COMPRESSED
;
286 const bool is_big_endian
= parameters
->target().is_big_endian();
287 const unsigned int ch_type
= compress
== zstd
288 ? elfcpp::ELFCOMPRESS_ZSTD
289 : elfcpp::ELFCOMPRESS_ZLIB
;
290 uint64_t addralign
= this->addralign ();
295 elfcpp::Chdr_write
<32, true> chdr(this->data_
);
296 chdr
.put_ch_type(ch_type
);
297 chdr
.put_ch_size(uncompressed_size
);
298 chdr
.put_ch_addralign(addralign
);
302 elfcpp::Chdr_write
<32, false> chdr(this->data_
);
303 chdr
.put_ch_type(ch_type
);
304 chdr
.put_ch_size(uncompressed_size
);
305 chdr
.put_ch_addralign(addralign
);
312 elfcpp::Chdr_write
<64, true> chdr(this->data_
);
313 chdr
.put_ch_type(ch_type
);
314 chdr
.put_ch_size(uncompressed_size
);
315 chdr
.put_ch_addralign(addralign
);
316 // Clear the reserved field.
317 chdr
.put_ch_reserved(0);
321 elfcpp::Chdr_write
<64, false> chdr(this->data_
);
322 chdr
.put_ch_type(ch_type
);
323 chdr
.put_ch_size(uncompressed_size
);
324 chdr
.put_ch_addralign(addralign
);
325 // Clear the reserved field.
326 chdr
.put_ch_reserved(0);
334 // Write out the zlib header.
335 memcpy(this->data_
, "ZLIB", 4);
336 elfcpp::Swap_unaligned
<64, true>::writeval(this->data_
+ 4,
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());
342 this->set_flags(flags
);
343 this->set_data_size(compressed_size
);
347 gold_warning(_("not compressing section data: zlib error"));
348 gold_assert(this->data_
== NULL
);
349 this->set_data_size(uncompressed_size
);
353 // Write out a compressed section. If we couldn't compress, we just
354 // write it out as normal, uncompressed data.
357 Output_compressed_section::do_write(Output_file
* of
)
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
);
362 if (this->data_
== NULL
)
363 memcpy(view
, this->postprocessing_buffer(), data_size
);
365 memcpy(view
, this->data_
, data_size
);
366 of
->write_output_view(offset
, data_size
, view
);
369 } // End namespace gold.