Commit | Line | Data |
---|---|---|
9a0910c3 ILT |
1 | // compressed_output.cc -- manage compressed output sections for gold |
2 | ||
3 | // Copyright 2007 Free Software Foundation, Inc. | |
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" | |
24 | ||
25 | #ifdef HAVE_ZLIB_H | |
26 | #include <zlib.h> | |
27 | #endif | |
28 | ||
9a0910c3 | 29 | #include "parameters.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. | |
40 | ||
b589a5bc ILT |
41 | #ifdef HAVE_ZLIB_H |
42 | ||
9a0910c3 ILT |
43 | static bool |
44 | zlib_compress(const char* uncompressed_data, unsigned long uncompressed_size, | |
45 | char** compressed_data, unsigned long* compressed_size) | |
46 | { | |
9a0910c3 ILT |
47 | *compressed_size = uncompressed_size + uncompressed_size / 1000 + 128; |
48 | *compressed_data = new char[*compressed_size]; | |
49 | ||
50 | int compress_level; | |
51 | if (parameters->optimization_level() >= 1) | |
52 | compress_level = 9; | |
53 | else | |
54 | compress_level = 1; | |
55 | ||
56 | int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data), | |
57 | compressed_size, | |
58 | reinterpret_cast<const Bytef*>(uncompressed_data), | |
59 | uncompressed_size, | |
60 | compress_level); | |
61 | if (rc == Z_OK) | |
62 | return true; | |
63 | else | |
64 | { | |
65 | delete[] *compressed_data; | |
66 | *compressed_data = NULL; | |
67 | return false; | |
68 | } | |
9a0910c3 ILT |
69 | } |
70 | ||
b589a5bc ILT |
71 | #else // !defined(HAVE_ZLIB_H) |
72 | ||
73 | static bool | |
74 | zlib_compress(const char*, unsigned long, char**, unsigned long*) | |
75 | { | |
76 | return false; | |
77 | } | |
78 | ||
79 | #endif // !defined(HAVE_ZLIB_H) | |
80 | ||
9a0910c3 ILT |
81 | // After compressing an output section, we rename it from foo to |
82 | // foo.zlib.nnnn, where nnnn is the uncompressed size of the section. | |
83 | ||
84 | static std::string | |
85 | zlib_compressed_suffix(unsigned long uncompressed_size) | |
86 | { | |
87 | char size_string[64]; | |
88 | snprintf(size_string, sizeof(size_string), "%lu", uncompressed_size); | |
89 | return std::string(".zlib.") + size_string; | |
90 | } | |
91 | ||
96803768 | 92 | // Class Output_compressed_section. |
9a0910c3 ILT |
93 | |
94 | // Set the final data size of a compressed section. This is where | |
95 | // we actually compress the section data. | |
96 | ||
97 | void | |
96803768 | 98 | Output_compressed_section::set_final_data_size() |
9a0910c3 | 99 | { |
96803768 | 100 | off_t uncompressed_size = this->postprocessing_buffer_size(); |
9a0910c3 ILT |
101 | |
102 | // (Try to) compress the data. | |
103 | unsigned long compressed_size; | |
96803768 ILT |
104 | unsigned char* u_uncompressed_data = this->postprocessing_buffer(); |
105 | char* uncompressed_data = reinterpret_cast<char*>(u_uncompressed_data); | |
106 | ||
107 | // At this point the contents of all regular input sections will | |
108 | // have been copied into the postprocessing buffer, and relocations | |
109 | // will have been applied. Now we need to copy in the contents of | |
110 | // anything other than a regular input section. | |
111 | this->write_to_postprocessing_buffer(); | |
9a0910c3 ILT |
112 | |
113 | bool success = false; | |
96803768 | 114 | if (this->options_->zlib_compress_debug_sections()) |
9a0910c3 ILT |
115 | success = zlib_compress(uncompressed_data, uncompressed_size, |
116 | &this->data_, &compressed_size); | |
117 | if (success) | |
118 | { | |
96803768 ILT |
119 | std::string suffix(zlib_compressed_suffix(uncompressed_size)); |
120 | this->new_section_name_ = std::string(this->name()) + suffix; | |
121 | this->set_name(this->new_section_name_.c_str()); | |
9a0910c3 | 122 | this->set_data_size(compressed_size); |
9a0910c3 ILT |
123 | } |
124 | else | |
125 | { | |
96803768 | 126 | gold_warning(_("not compressing section data: zlib error")); |
9a0910c3 | 127 | gold_assert(this->data_ == NULL); |
9a0910c3 ILT |
128 | this->set_data_size(uncompressed_size); |
129 | } | |
130 | } | |
131 | ||
9a0910c3 ILT |
132 | // Write out a compressed section. If we couldn't compress, we just |
133 | // write it out as normal, uncompressed data. | |
134 | ||
135 | void | |
96803768 | 136 | Output_compressed_section::do_write(Output_file* of) |
9a0910c3 | 137 | { |
96803768 ILT |
138 | off_t offset = this->offset(); |
139 | off_t data_size = this->data_size(); | |
140 | unsigned char* view = of->get_output_view(offset, data_size); | |
141 | if (this->data_ == NULL) | |
142 | memcpy(view, this->postprocessing_buffer(), data_size); | |
9a0910c3 | 143 | else |
96803768 ILT |
144 | memcpy(view, this->data_, data_size); |
145 | of->write_output_view(offset, data_size, view); | |
9a0910c3 ILT |
146 | } |
147 | ||
9a0910c3 | 148 | } // End namespace gold. |