Add SHF_COMPRESSED section decompression to gold
[deliverable/binutils-gdb.git] / gold / compressed_output.cc
CommitLineData
a2e47362 1// compressed_output.cc -- manage compressed debug sections for gold
9a0910c3 2
b90efa5b 3// Copyright (C) 2007-2015 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
29namespace 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
41static bool
126f3ece
ILT
42zlib_compress(const unsigned char* uncompressed_data,
43 unsigned long uncompressed_size,
44 unsigned char** compressed_data,
45 unsigned long* compressed_size)
9a0910c3 46{
126f3ece 47 const int header_size = 12;
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
ILT
63 {
64 memcpy(*compressed_data, "ZLIB", 4);
65 elfcpp::Swap_unaligned<64, true>::writeval(*compressed_data + 4,
66 uncompressed_size);
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
a2e47362
CC
78// Decompress COMPRESSED_DATA of size COMPRESSED_SIZE, into a buffer
79// UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE. Returns TRUE if it
80// decompressed successfully, false if it failed. The buffer, of
81// appropriate size, is provided by the caller, and is typically part
82// of the memory-mapped output file.
83
84static bool
85zlib_decompress(const unsigned char* compressed_data,
86 unsigned long compressed_size,
87 unsigned char* uncompressed_data,
88 unsigned long uncompressed_size)
89{
90 z_stream strm;
91 int rc;
92
93 /* It is possible the section consists of several compressed
94 buffers concatenated together, so we uncompress in a loop. */
95 strm.zalloc = NULL;
96 strm.zfree = NULL;
97 strm.opaque = NULL;
98 strm.avail_in = compressed_size;
99 strm.next_in = const_cast<Bytef*>(compressed_data);
100 strm.avail_out = uncompressed_size;
101
102 rc = inflateInit(&strm);
103 while (strm.avail_in > 0)
104 {
105 if (rc != Z_OK)
106 return false;
107 strm.next_out = ((Bytef*) uncompressed_data
108 + (uncompressed_size - strm.avail_out));
109 rc = inflate(&strm, Z_FINISH);
110 if (rc != Z_STREAM_END)
111 return false;
112 rc = inflateReset(&strm);
113 }
114 rc = inflateEnd(&strm);
115 if (rc != Z_OK || strm.avail_out != 0)
116 return false;
117
118 return true;
119}
120
a2e47362
CC
121// Read the compression header of a compressed debug section and return
122// the uncompressed size.
123
124uint64_t
125get_uncompressed_size(const unsigned char* compressed_data,
126 section_size_type compressed_size)
127{
128 const unsigned int zlib_header_size = 12;
129
130 /* Verify the compression header. Currently, we support only zlib
131 compression, so it should be "ZLIB" followed by the uncompressed
132 section size, 8 bytes in big-endian order. */
133 if (compressed_size >= zlib_header_size
134 && strncmp(reinterpret_cast<const char*>(compressed_data),
135 "ZLIB", 4) == 0)
136 return elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4);
137 return -1ULL;
138}
139
140// Decompress a compressed debug section directly into the output file.
141
142bool
143decompress_input_section(const unsigned char* compressed_data,
144 unsigned long compressed_size,
145 unsigned char* uncompressed_data,
48058663
L
146 unsigned long uncompressed_size,
147 int size,
148 bool big_endian,
149 elfcpp::Elf_Xword sh_flags)
a2e47362 150{
48058663
L
151 if ((sh_flags & elfcpp::SHF_COMPRESSED) != 0)
152 {
153 unsigned int compression_header_size;
154 if (size == 32)
155 {
156 compression_header_size = elfcpp::Elf_sizes<32>::chdr_size;
157 if (big_endian)
158 {
159 elfcpp::Chdr<32, true> chdr(compressed_data);
160 if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB)
161 return false;
162 }
163 else
164 {
165 elfcpp::Chdr<32, false> chdr(compressed_data);
166 if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB)
167 return false;
168 }
169 }
170 else if (size == 64)
171 {
172 compression_header_size = elfcpp::Elf_sizes<64>::chdr_size;
173 if (big_endian)
174 {
175 elfcpp::Chdr<64, true> chdr(compressed_data);
176 if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB)
177 return false;
178 }
179 else
180 {
181 elfcpp::Chdr<64, false> chdr(compressed_data);
182 if (chdr.get_ch_type() != elfcpp::ELFCOMPRESS_ZLIB)
183 return false;
184 }
185 }
186 else
187 gold_unreachable();
188
189 return zlib_decompress(compressed_data + compression_header_size,
190 compressed_size - compression_header_size,
191 uncompressed_data,
192 uncompressed_size);
193 }
194
a2e47362
CC
195 const unsigned int zlib_header_size = 12;
196
197 /* Verify the compression header. Currently, we support only zlib
198 compression, so it should be "ZLIB" followed by the uncompressed
199 section size, 8 bytes in big-endian order. */
200 if (compressed_size >= zlib_header_size
201 && strncmp(reinterpret_cast<const char*>(compressed_data),
202 "ZLIB", 4) == 0)
203 {
204 unsigned long uncompressed_size_check =
205 elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4);
206 gold_assert(uncompressed_size_check == uncompressed_size);
207 return zlib_decompress(compressed_data + zlib_header_size,
208 compressed_size - zlib_header_size,
209 uncompressed_data,
210 uncompressed_size);
211 }
212 return false;
213}
214
96803768 215// Class Output_compressed_section.
9a0910c3
ILT
216
217// Set the final data size of a compressed section. This is where
218// we actually compress the section data.
219
220void
96803768 221Output_compressed_section::set_final_data_size()
9a0910c3 222{
96803768 223 off_t uncompressed_size = this->postprocessing_buffer_size();
9a0910c3
ILT
224
225 // (Try to) compress the data.
226 unsigned long compressed_size;
126f3ece 227 unsigned char* uncompressed_data = this->postprocessing_buffer();
96803768
ILT
228
229 // At this point the contents of all regular input sections will
230 // have been copied into the postprocessing buffer, and relocations
231 // will have been applied. Now we need to copy in the contents of
232 // anything other than a regular input section.
233 this->write_to_postprocessing_buffer();
9a0910c3
ILT
234
235 bool success = false;
ee1fe73e 236 if (strcmp(this->options_->compress_debug_sections(), "zlib") == 0)
9a0910c3
ILT
237 success = zlib_compress(uncompressed_data, uncompressed_size,
238 &this->data_, &compressed_size);
239 if (success)
240 {
126f3ece
ILT
241 // This converts .debug_foo to .zdebug_foo
242 this->new_section_name_ = std::string(".z") + (this->name() + 1);
96803768 243 this->set_name(this->new_section_name_.c_str());
9a0910c3 244 this->set_data_size(compressed_size);
9a0910c3
ILT
245 }
246 else
247 {
96803768 248 gold_warning(_("not compressing section data: zlib error"));
9a0910c3 249 gold_assert(this->data_ == NULL);
9a0910c3
ILT
250 this->set_data_size(uncompressed_size);
251 }
252}
253
9a0910c3
ILT
254// Write out a compressed section. If we couldn't compress, we just
255// write it out as normal, uncompressed data.
256
257void
96803768 258Output_compressed_section::do_write(Output_file* of)
9a0910c3 259{
2ea97941
ILT
260 off_t offset = this->offset();
261 off_t data_size = this->data_size();
262 unsigned char* view = of->get_output_view(offset, data_size);
96803768 263 if (this->data_ == NULL)
2ea97941 264 memcpy(view, this->postprocessing_buffer(), data_size);
9a0910c3 265 else
2ea97941
ILT
266 memcpy(view, this->data_, data_size);
267 of->write_output_view(offset, data_size, view);
9a0910c3
ILT
268}
269
9a0910c3 270} // End namespace gold.
This page took 0.304105 seconds and 4 git commands to generate.