Commit | Line | Data |
---|---|---|
62b01cb5 ILT |
1 | // reduced_debug_output.cc -- output reduced debugging information to save space |
2 | ||
9b547ce6 | 3 | // Copyright 2008, 2010 Free Software Foundation, Inc. |
62b01cb5 ILT |
4 | // Written by Caleb Howe <cshowe@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 | #include "parameters.h" | |
26 | #include "options.h" | |
27 | #include "dwarf.h" | |
28 | #include "dwarf_reader.h" | |
29 | #include "reduced_debug_output.h" | |
4f787271 | 30 | #include "int_encoding.h" |
62b01cb5 ILT |
31 | |
32 | #include <vector> | |
33 | ||
34 | namespace gold | |
35 | { | |
36 | ||
62b01cb5 ILT |
37 | // Given a pointer to the beginning of a die and the beginning of the associated |
38 | // abbreviation fills in die_end with the end of the information entry. If | |
39 | // successful returns true. Get_die_end also takes a pointer to the end of the | |
40 | // buffer containing the die. If die_end would be beyond the end of the | |
41 | // buffer, or if an unsupported dwarf form is encountered returns false. | |
42 | bool | |
43 | Output_reduced_debug_info_section::get_die_end( | |
44 | unsigned char* die, unsigned char* abbrev, unsigned char** die_end, | |
45 | unsigned char* buffer_end, int address_size, bool is64) | |
46 | { | |
47 | size_t LEB_size; | |
48 | uint64_t LEB_decoded; | |
49 | for(;;) | |
50 | { | |
51 | uint64_t attribute = read_unsigned_LEB_128(abbrev, &LEB_size); | |
52 | abbrev += LEB_size; | |
53 | elfcpp::DW_FORM form = | |
54 | static_cast<elfcpp::DW_FORM>(read_unsigned_LEB_128(abbrev, | |
55 | &LEB_size)); | |
56 | abbrev += LEB_size; | |
57 | if (!(attribute || form)) | |
58 | break; | |
59 | if (die >= buffer_end) | |
60 | return false; | |
61 | switch(form) | |
62 | { | |
63 | case elfcpp::DW_FORM_null: | |
feb5f3b0 | 64 | case elfcpp::DW_FORM_flag_present: |
62b01cb5 ILT |
65 | break; |
66 | case elfcpp::DW_FORM_strp: | |
feb5f3b0 | 67 | case elfcpp::DW_FORM_sec_offset: |
62b01cb5 ILT |
68 | die += is64 ? 8 : 4; |
69 | break; | |
70 | case elfcpp::DW_FORM_addr: | |
71 | case elfcpp::DW_FORM_ref_addr: | |
72 | die += address_size; | |
73 | break; | |
74 | case elfcpp::DW_FORM_block1: | |
75 | die += *die; | |
76 | die += 1; | |
77 | break; | |
78 | case elfcpp::DW_FORM_block2: | |
79 | { | |
80 | uint16_t block_size; | |
81 | block_size = read_from_pointer<16>(&die); | |
82 | die += block_size; | |
83 | break; | |
84 | } | |
85 | case elfcpp::DW_FORM_block4: | |
86 | { | |
87 | uint32_t block_size; | |
88 | block_size = read_from_pointer<32>(&die); | |
89 | die += block_size; | |
90 | break; | |
91 | } | |
92 | case elfcpp::DW_FORM_block: | |
feb5f3b0 | 93 | case elfcpp::DW_FORM_exprloc: |
62b01cb5 ILT |
94 | LEB_decoded = read_unsigned_LEB_128(die, &LEB_size); |
95 | die += (LEB_decoded + LEB_size); | |
96 | break; | |
97 | case elfcpp::DW_FORM_data1: | |
98 | case elfcpp::DW_FORM_ref1: | |
99 | case elfcpp::DW_FORM_flag: | |
100 | die += 1; | |
101 | break; | |
102 | case elfcpp::DW_FORM_data2: | |
103 | case elfcpp::DW_FORM_ref2: | |
104 | die += 2; | |
105 | break; | |
106 | case elfcpp::DW_FORM_data4: | |
107 | case elfcpp::DW_FORM_ref4: | |
108 | die += 4; | |
109 | break; | |
110 | case elfcpp::DW_FORM_data8: | |
111 | case elfcpp::DW_FORM_ref8: | |
feb5f3b0 | 112 | case elfcpp::DW_FORM_ref_sig8: |
62b01cb5 ILT |
113 | die += 8; |
114 | break; | |
115 | case elfcpp::DW_FORM_ref_udata: | |
116 | case elfcpp::DW_FORM_udata: | |
117 | read_unsigned_LEB_128(die, &LEB_size); | |
118 | die += LEB_size; | |
119 | break; | |
feb5f3b0 CC |
120 | case elfcpp::DW_FORM_sdata: |
121 | read_signed_LEB_128(die, &LEB_size); | |
122 | die += LEB_size; | |
123 | break; | |
62b01cb5 ILT |
124 | case elfcpp::DW_FORM_string: |
125 | { | |
126 | size_t length = strlen(reinterpret_cast<char*>(die)); | |
127 | die += length + 1; | |
128 | break; | |
129 | } | |
62b01cb5 | 130 | case elfcpp::DW_FORM_indirect: |
feb5f3b0 CC |
131 | case elfcpp::DW_FORM_GNU_ref_index: |
132 | case elfcpp::DW_FORM_GNU_addr_index: | |
133 | case elfcpp::DW_FORM_GNU_str_index: | |
62b01cb5 ILT |
134 | return false; |
135 | } | |
136 | } | |
137 | *die_end = die; | |
138 | return true; | |
139 | } | |
140 | ||
141 | void | |
142 | Output_reduced_debug_abbrev_section::set_final_data_size() | |
143 | { | |
144 | if (this->sized_ || this->failed_) | |
145 | return; | |
146 | ||
147 | uint64_t abbrev_number; | |
148 | size_t LEB_size; | |
149 | unsigned char* abbrev_data = this->postprocessing_buffer(); | |
150 | unsigned char* abbrev_end = this->postprocessing_buffer() + | |
151 | this->postprocessing_buffer_size(); | |
152 | this->write_to_postprocessing_buffer(); | |
153 | while(abbrev_data < abbrev_end) | |
154 | { | |
155 | uint64_t abbrev_offset = abbrev_data - this->postprocessing_buffer(); | |
156 | while((abbrev_number = read_unsigned_LEB_128(abbrev_data, &LEB_size))) | |
157 | { | |
158 | if (abbrev_data >= abbrev_end) | |
159 | { | |
160 | failed("Debug abbreviations extend beyond .debug_abbrev " | |
161 | "section; failed to reduce debug abbreviations"); | |
162 | return; | |
163 | } | |
164 | abbrev_data += LEB_size; | |
165 | ||
166 | // Together with the abbreviation number these fields make up | |
9b547ce6 | 167 | // the header for each abbreviation. |
62b01cb5 ILT |
168 | uint64_t abbrev_type = read_unsigned_LEB_128(abbrev_data, &LEB_size); |
169 | abbrev_data += LEB_size; | |
170 | ||
171 | // This would ordinarily be the has_children field of the | |
9b547ce6 RW |
172 | // abbreviation. But it's going to be false after reducing the |
173 | // information, so there's no point in storing it. | |
62b01cb5 ILT |
174 | abbrev_data++; |
175 | ||
9b547ce6 | 176 | // Read to the end of the current abbreviation. |
62b01cb5 ILT |
177 | // This is indicated by two zero unsigned LEBs in a row. We don't |
178 | // need to parse the data yet, so we just scan through the data | |
179 | // looking for two consecutive 0 bytes indicating the end of the | |
9b547ce6 | 180 | // abbreviation. |
62b01cb5 ILT |
181 | unsigned char* current_abbrev; |
182 | for (current_abbrev = abbrev_data; | |
183 | current_abbrev[0] || current_abbrev[1]; | |
184 | current_abbrev++) | |
185 | { | |
186 | if (current_abbrev >= abbrev_end) | |
187 | { | |
188 | this->failed(_("Debug abbreviations extend beyond " | |
189 | ".debug_abbrev section; failed to reduce " | |
190 | "debug abbreviations")); | |
191 | return; | |
192 | } | |
193 | } | |
194 | // Account for the two nulls and advance to the start of the | |
195 | // next abbreviation. | |
196 | current_abbrev += 2; | |
197 | ||
198 | // We're eliminating every entry except for compile units, so we | |
199 | // only need to store abbreviations that describe them | |
200 | if (abbrev_type == elfcpp::DW_TAG_compile_unit) | |
201 | { | |
202 | write_unsigned_LEB_128(&this->data_, ++this->abbrev_count_); | |
203 | write_unsigned_LEB_128(&this->data_, abbrev_type); | |
204 | // has_children is false for all entries | |
205 | this->data_.push_back(0); | |
206 | this->abbrev_mapping_[std::make_pair(abbrev_offset, | |
207 | abbrev_number)] = | |
208 | std::make_pair(abbrev_count_, this->data_.size()); | |
209 | this->data_.insert(this->data_.end(), abbrev_data, | |
210 | current_abbrev); | |
211 | } | |
212 | abbrev_data = current_abbrev; | |
213 | } | |
214 | gold_assert(LEB_size == 1); | |
215 | abbrev_data += LEB_size; | |
216 | } | |
217 | // Null terminate the list of abbreviations | |
218 | this->data_.push_back(0); | |
219 | this->set_data_size(data_.size()); | |
220 | this->sized_ = true; | |
221 | } | |
222 | ||
223 | void | |
224 | Output_reduced_debug_abbrev_section::do_write(Output_file* of) | |
225 | { | |
2ea97941 ILT |
226 | off_t offset = this->offset(); |
227 | off_t data_size = this->data_size(); | |
228 | unsigned char* view = of->get_output_view(offset, data_size); | |
62b01cb5 ILT |
229 | if (this->failed_) |
230 | memcpy(view, this->postprocessing_buffer(), | |
231 | this->postprocessing_buffer_size()); | |
232 | else | |
2ea97941 ILT |
233 | memcpy(view, &this->data_.front(), data_size); |
234 | of->write_output_view(offset, data_size, view); | |
62b01cb5 ILT |
235 | } |
236 | ||
237 | // Locates the abbreviation with abbreviation_number abbrev_number in the | |
238 | // abbreviation table at offset abbrev_offset. abbrev_number is updated with | |
239 | // its new abbreviation number and a pointer to the beginning of the | |
240 | // abbreviation is returned. | |
241 | unsigned char* | |
242 | Output_reduced_debug_abbrev_section::get_new_abbrev( | |
243 | uint64_t* abbrev_number, uint64_t abbrev_offset) | |
244 | { | |
245 | set_final_data_size(); | |
246 | std::pair<uint64_t, uint64_t> abbrev_info = | |
247 | this->abbrev_mapping_[std::make_pair(abbrev_offset, *abbrev_number)]; | |
248 | *abbrev_number = abbrev_info.first; | |
249 | return &this->data_[abbrev_info.second]; | |
250 | } | |
251 | ||
252 | void Output_reduced_debug_info_section::set_final_data_size() | |
253 | { | |
254 | if (this->failed_) | |
255 | return; | |
256 | unsigned char* debug_info = this->postprocessing_buffer(); | |
257 | unsigned char* debug_info_end = (this->postprocessing_buffer() | |
258 | + this->postprocessing_buffer_size()); | |
259 | unsigned char* next_compile_unit; | |
260 | this->write_to_postprocessing_buffer(); | |
261 | ||
262 | while (debug_info < debug_info_end) | |
263 | { | |
264 | uint32_t compile_unit_start = read_from_pointer<32>(&debug_info); | |
265 | // The first 4 bytes of each compile unit determine whether or | |
266 | // not we're using dwarf32 or dwarf64. This is not necessarily | |
267 | // related to whether the binary is 32 or 64 bits. | |
268 | if (compile_unit_start == 0xFFFFFFFF) | |
269 | { | |
270 | // Technically the size can be up to 96 bits. Rather than handle | |
271 | // 96/128 bit integers we just truncate the size at 64 bits. | |
272 | if (0 != read_from_pointer<32>(&debug_info)) | |
273 | { | |
274 | this->failed(_("Extremely large compile unit in debug info; " | |
275 | "failed to reduce debug info")); | |
276 | return; | |
277 | } | |
278 | const int dwarf64_header_size = sizeof(uint64_t) + sizeof(uint16_t) + | |
279 | sizeof(uint64_t) + sizeof(uint8_t); | |
280 | if (debug_info + dwarf64_header_size >= debug_info_end) | |
281 | { | |
282 | this->failed(_("Debug info extends beyond .debug_info section;" | |
283 | "failed to reduce debug info")); | |
284 | return; | |
285 | } | |
286 | ||
287 | uint64_t compile_unit_size = read_from_pointer<64>(&debug_info); | |
288 | next_compile_unit = debug_info + compile_unit_size; | |
289 | uint16_t version = read_from_pointer<16>(&debug_info); | |
290 | uint64_t abbrev_offset = read_from_pointer<64>(&debug_info); | |
291 | uint8_t address_size = read_from_pointer<8>(&debug_info); | |
292 | size_t LEB_size; | |
293 | uint64_t abbreviation_number = read_unsigned_LEB_128(debug_info, | |
294 | &LEB_size); | |
295 | debug_info += LEB_size; | |
296 | unsigned char* die_abbrev = this->associated_abbrev_->get_new_abbrev( | |
297 | &abbreviation_number, abbrev_offset); | |
298 | unsigned char* die_end; | |
299 | if (!this->get_die_end(debug_info, die_abbrev, &die_end, | |
300 | debug_info_end, address_size, true)) | |
301 | { | |
302 | this->failed(_("Invalid DIE in debug info; " | |
303 | "failed to reduce debug info")); | |
304 | return; | |
305 | } | |
306 | ||
e0ebcf42 ILT |
307 | insert_into_vector<32>(&this->data_, 0xFFFFFFFF); |
308 | insert_into_vector<32>(&this->data_, 0); | |
309 | insert_into_vector<64>( | |
62b01cb5 ILT |
310 | &this->data_, |
311 | (11 + get_length_as_unsigned_LEB_128(abbreviation_number) | |
312 | + die_end - debug_info)); | |
e0ebcf42 ILT |
313 | insert_into_vector<16>(&this->data_, version); |
314 | insert_into_vector<64>(&this->data_, 0); | |
315 | insert_into_vector<8>(&this->data_, address_size); | |
62b01cb5 ILT |
316 | write_unsigned_LEB_128(&this->data_, abbreviation_number); |
317 | this->data_.insert(this->data_.end(), debug_info, die_end); | |
318 | } | |
319 | else | |
320 | { | |
321 | const int dwarf32_header_size = | |
322 | sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint8_t); | |
323 | if (debug_info + dwarf32_header_size >= debug_info_end) | |
324 | { | |
325 | this->failed(_("Debug info extends beyond .debug_info section; " | |
326 | "failed to reduce debug info")); | |
327 | return; | |
328 | } | |
329 | uint32_t compile_unit_size = compile_unit_start; | |
330 | next_compile_unit = debug_info + compile_unit_size; | |
331 | uint16_t version = read_from_pointer<16>(&debug_info); | |
332 | uint32_t abbrev_offset = read_from_pointer<32>(&debug_info); | |
333 | uint8_t address_size = read_from_pointer<8>(&debug_info); | |
334 | size_t LEB_size; | |
335 | uint64_t abbreviation_number = read_unsigned_LEB_128(debug_info, | |
336 | &LEB_size); | |
337 | debug_info += LEB_size; | |
338 | unsigned char* die_abbrev = this->associated_abbrev_->get_new_abbrev( | |
339 | &abbreviation_number, abbrev_offset); | |
340 | unsigned char* die_end; | |
341 | if (!this->get_die_end(debug_info, die_abbrev, &die_end, | |
342 | debug_info_end, address_size, false)) | |
343 | { | |
344 | this->failed(_("Invalid DIE in debug info; " | |
345 | "failed to reduce debug info")); | |
346 | return; | |
347 | } | |
348 | ||
e0ebcf42 | 349 | insert_into_vector<32>( |
62b01cb5 ILT |
350 | &this->data_, |
351 | (7 + get_length_as_unsigned_LEB_128(abbreviation_number) | |
352 | + die_end - debug_info)); | |
e0ebcf42 ILT |
353 | insert_into_vector<16>(&this->data_, version); |
354 | insert_into_vector<32>(&this->data_, 0); | |
355 | insert_into_vector<8>(&this->data_, address_size); | |
62b01cb5 ILT |
356 | write_unsigned_LEB_128(&this->data_, abbreviation_number); |
357 | this->data_.insert(this->data_.end(), debug_info, die_end); | |
358 | } | |
359 | debug_info = next_compile_unit; | |
360 | } | |
361 | this->set_data_size(data_.size()); | |
362 | } | |
363 | ||
364 | void Output_reduced_debug_info_section::do_write(Output_file* of) | |
365 | { | |
2ea97941 ILT |
366 | off_t offset = this->offset(); |
367 | off_t data_size = this->data_size(); | |
368 | unsigned char* view = of->get_output_view(offset, data_size); | |
62b01cb5 ILT |
369 | if (this->failed_) |
370 | memcpy(view, this->postprocessing_buffer(), | |
371 | this->postprocessing_buffer_size()); | |
372 | else | |
2ea97941 ILT |
373 | memcpy(view, &this->data_.front(), data_size); |
374 | of->write_output_view(offset, data_size, view); | |
62b01cb5 ILT |
375 | } |
376 | ||
377 | } // End namespace gold. |