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