* section.c (bfd_get_section_contents): Detect and handle the case
[binutils.git] / gold / reduced_debug_output.cc
blob48346943dd511a9d68f959a87d5730095b9d3a88
1 // reduced_debug_output.cc -- output reduced debugging information to save space
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Caleb Howe <cshowe@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.
23 #include "gold.h"
25 #include "parameters.h"
26 #include "options.h"
27 #include "dwarf.h"
28 #include "dwarf_reader.h"
29 #include "reduced_debug_output.h"
31 #include <vector>
33 namespace gold
36 void
37 write_unsigned_LEB_128(std::vector<unsigned char>* buffer, uint64_t value)
41 unsigned char current_byte = value & 0x7f;
42 value >>= 7;
43 if (value != 0)
45 current_byte |= 0x80;
47 buffer->push_back(current_byte);
49 while (value != 0);
52 size_t
53 get_length_as_unsigned_LEB_128(uint64_t value)
55 size_t length = 0;
58 unsigned char current_byte = value & 0x7f;
59 value >>= 7;
60 if (value != 0)
62 current_byte |= 0x80;
64 length++;
66 while (value != 0);
67 return length;
70 template <int valsize>
71 void insert_into_vector(std::vector<unsigned char>* destination,
72 typename elfcpp::Valtype_base<valsize>::Valtype value)
74 unsigned char buffer[valsize / 8];
75 if (parameters->target().is_big_endian())
76 elfcpp::Swap_unaligned<valsize, true>::writeval(buffer, value);
77 else
78 elfcpp::Swap_unaligned<valsize, false>::writeval(buffer, value);
79 destination->insert(destination->end(), buffer, buffer + valsize / 8);
82 template <int valsize>
83 typename elfcpp::Valtype_base<valsize>::Valtype
84 read_from_pointer(unsigned char** source)
86 typename elfcpp::Valtype_base<valsize>::Valtype return_value;
87 if (parameters->target().is_big_endian())
88 return_value = elfcpp::Swap_unaligned<valsize, true>::readval(*source);
89 else
90 return_value = elfcpp::Swap_unaligned<valsize, false>::readval(*source);
91 *source += valsize / 8;
92 return return_value;
95 // Given a pointer to the beginning of a die and the beginning of the associated
96 // abbreviation fills in die_end with the end of the information entry. If
97 // successful returns true. Get_die_end also takes a pointer to the end of the
98 // buffer containing the die. If die_end would be beyond the end of the
99 // buffer, or if an unsupported dwarf form is encountered returns false.
100 bool
101 Output_reduced_debug_info_section::get_die_end(
102 unsigned char* die, unsigned char* abbrev, unsigned char** die_end,
103 unsigned char* buffer_end, int address_size, bool is64)
105 size_t LEB_size;
106 uint64_t LEB_decoded;
107 for(;;)
109 uint64_t attribute = read_unsigned_LEB_128(abbrev, &LEB_size);
110 abbrev += LEB_size;
111 elfcpp::DW_FORM form =
112 static_cast<elfcpp::DW_FORM>(read_unsigned_LEB_128(abbrev,
113 &LEB_size));
114 abbrev += LEB_size;
115 if (!(attribute || form))
116 break;
117 if (die >= buffer_end)
118 return false;
119 switch(form)
121 case elfcpp::DW_FORM_null:
122 break;
123 case elfcpp::DW_FORM_strp:
124 die += is64 ? 8 : 4;
125 break;
126 case elfcpp::DW_FORM_addr:
127 case elfcpp::DW_FORM_ref_addr:
128 die += address_size;
129 break;
130 case elfcpp::DW_FORM_block1:
131 die += *die;
132 die += 1;
133 break;
134 case elfcpp::DW_FORM_block2:
136 uint16_t block_size;
137 block_size = read_from_pointer<16>(&die);
138 die += block_size;
139 break;
141 case elfcpp::DW_FORM_block4:
143 uint32_t block_size;
144 block_size = read_from_pointer<32>(&die);
145 die += block_size;
146 break;
148 case elfcpp::DW_FORM_block:
149 LEB_decoded = read_unsigned_LEB_128(die, &LEB_size);
150 die += (LEB_decoded + LEB_size);
151 break;
152 case elfcpp::DW_FORM_data1:
153 case elfcpp::DW_FORM_ref1:
154 case elfcpp::DW_FORM_flag:
155 die += 1;
156 break;
157 case elfcpp::DW_FORM_data2:
158 case elfcpp::DW_FORM_ref2:
159 die += 2;
160 break;
161 case elfcpp::DW_FORM_data4:
162 case elfcpp::DW_FORM_ref4:
163 die += 4;
164 break;
165 case elfcpp::DW_FORM_data8:
166 case elfcpp::DW_FORM_ref8:
167 die += 8;
168 break;
169 case elfcpp::DW_FORM_ref_udata:
170 case elfcpp::DW_FORM_udata:
171 read_unsigned_LEB_128(die, &LEB_size);
172 die += LEB_size;
173 break;
174 case elfcpp::DW_FORM_string:
176 size_t length = strlen(reinterpret_cast<char*>(die));
177 die += length + 1;
178 break;
180 case elfcpp::DW_FORM_sdata:
181 case elfcpp::DW_FORM_indirect:
182 return false;
185 *die_end = die;
186 return true;
189 void
190 Output_reduced_debug_abbrev_section::set_final_data_size()
192 if (this->sized_ || this->failed_)
193 return;
195 uint64_t abbrev_number;
196 size_t LEB_size;
197 unsigned char* abbrev_data = this->postprocessing_buffer();
198 unsigned char* abbrev_end = this->postprocessing_buffer() +
199 this->postprocessing_buffer_size();
200 this->write_to_postprocessing_buffer();
201 while(abbrev_data < abbrev_end)
203 uint64_t abbrev_offset = abbrev_data - this->postprocessing_buffer();
204 while((abbrev_number = read_unsigned_LEB_128(abbrev_data, &LEB_size)))
206 if (abbrev_data >= abbrev_end)
208 failed("Debug abbreviations extend beyond .debug_abbrev "
209 "section; failed to reduce debug abbreviations");
210 return;
212 abbrev_data += LEB_size;
214 // Together with the abbreviation number these fields make up
215 // the header for each abbreviation
216 uint64_t abbrev_type = read_unsigned_LEB_128(abbrev_data, &LEB_size);
217 abbrev_data += LEB_size;
219 // This would ordinarily be the has_children field of the
220 // abbreviation. But it's going to be false after reducting the
221 // information, so there's no point in storing it
222 abbrev_data++;
224 // Read to the end of the current abbreviation
225 // This is indicated by two zero unsigned LEBs in a row. We don't
226 // need to parse the data yet, so we just scan through the data
227 // looking for two consecutive 0 bytes indicating the end of the
228 // abbreviation
229 unsigned char* current_abbrev;
230 for (current_abbrev = abbrev_data;
231 current_abbrev[0] || current_abbrev[1];
232 current_abbrev++)
234 if (current_abbrev >= abbrev_end)
236 this->failed(_("Debug abbreviations extend beyond "
237 ".debug_abbrev section; failed to reduce "
238 "debug abbreviations"));
239 return;
242 // Account for the two nulls and advance to the start of the
243 // next abbreviation.
244 current_abbrev += 2;
246 // We're eliminating every entry except for compile units, so we
247 // only need to store abbreviations that describe them
248 if (abbrev_type == elfcpp::DW_TAG_compile_unit)
250 write_unsigned_LEB_128(&this->data_, ++this->abbrev_count_);
251 write_unsigned_LEB_128(&this->data_, abbrev_type);
252 // has_children is false for all entries
253 this->data_.push_back(0);
254 this->abbrev_mapping_[std::make_pair(abbrev_offset,
255 abbrev_number)] =
256 std::make_pair(abbrev_count_, this->data_.size());
257 this->data_.insert(this->data_.end(), abbrev_data,
258 current_abbrev);
260 abbrev_data = current_abbrev;
262 gold_assert(LEB_size == 1);
263 abbrev_data += LEB_size;
265 // Null terminate the list of abbreviations
266 this->data_.push_back(0);
267 this->set_data_size(data_.size());
268 this->sized_ = true;
271 void
272 Output_reduced_debug_abbrev_section::do_write(Output_file* of)
274 off_t offset = this->offset();
275 off_t data_size = this->data_size();
276 unsigned char* view = of->get_output_view(offset, data_size);
277 if (this->failed_)
278 memcpy(view, this->postprocessing_buffer(),
279 this->postprocessing_buffer_size());
280 else
281 memcpy(view, &this->data_.front(), data_size);
282 of->write_output_view(offset, data_size, view);
285 // Locates the abbreviation with abbreviation_number abbrev_number in the
286 // abbreviation table at offset abbrev_offset. abbrev_number is updated with
287 // its new abbreviation number and a pointer to the beginning of the
288 // abbreviation is returned.
289 unsigned char*
290 Output_reduced_debug_abbrev_section::get_new_abbrev(
291 uint64_t* abbrev_number, uint64_t abbrev_offset)
293 set_final_data_size();
294 std::pair<uint64_t, uint64_t> abbrev_info =
295 this->abbrev_mapping_[std::make_pair(abbrev_offset, *abbrev_number)];
296 *abbrev_number = abbrev_info.first;
297 return &this->data_[abbrev_info.second];
300 void Output_reduced_debug_info_section::set_final_data_size()
302 if (this->failed_)
303 return;
304 unsigned char* debug_info = this->postprocessing_buffer();
305 unsigned char* debug_info_end = (this->postprocessing_buffer()
306 + this->postprocessing_buffer_size());
307 unsigned char* next_compile_unit;
308 this->write_to_postprocessing_buffer();
310 while (debug_info < debug_info_end)
312 uint32_t compile_unit_start = read_from_pointer<32>(&debug_info);
313 // The first 4 bytes of each compile unit determine whether or
314 // not we're using dwarf32 or dwarf64. This is not necessarily
315 // related to whether the binary is 32 or 64 bits.
316 if (compile_unit_start == 0xFFFFFFFF)
318 // Technically the size can be up to 96 bits. Rather than handle
319 // 96/128 bit integers we just truncate the size at 64 bits.
320 if (0 != read_from_pointer<32>(&debug_info))
322 this->failed(_("Extremely large compile unit in debug info; "
323 "failed to reduce debug info"));
324 return;
326 const int dwarf64_header_size = sizeof(uint64_t) + sizeof(uint16_t) +
327 sizeof(uint64_t) + sizeof(uint8_t);
328 if (debug_info + dwarf64_header_size >= debug_info_end)
330 this->failed(_("Debug info extends beyond .debug_info section;"
331 "failed to reduce debug info"));
332 return;
335 uint64_t compile_unit_size = read_from_pointer<64>(&debug_info);
336 next_compile_unit = debug_info + compile_unit_size;
337 uint16_t version = read_from_pointer<16>(&debug_info);
338 uint64_t abbrev_offset = read_from_pointer<64>(&debug_info);
339 uint8_t address_size = read_from_pointer<8>(&debug_info);
340 size_t LEB_size;
341 uint64_t abbreviation_number = read_unsigned_LEB_128(debug_info,
342 &LEB_size);
343 debug_info += LEB_size;
344 unsigned char* die_abbrev = this->associated_abbrev_->get_new_abbrev(
345 &abbreviation_number, abbrev_offset);
346 unsigned char* die_end;
347 if (!this->get_die_end(debug_info, die_abbrev, &die_end,
348 debug_info_end, address_size, true))
350 this->failed(_("Invalid DIE in debug info; "
351 "failed to reduce debug info"));
352 return;
355 insert_into_vector<32>(&this->data_, 0xFFFFFFFF);
356 insert_into_vector<32>(&this->data_, 0);
357 insert_into_vector<64>(
358 &this->data_,
359 (11 + get_length_as_unsigned_LEB_128(abbreviation_number)
360 + die_end - debug_info));
361 insert_into_vector<16>(&this->data_, version);
362 insert_into_vector<64>(&this->data_, 0);
363 insert_into_vector<8>(&this->data_, address_size);
364 write_unsigned_LEB_128(&this->data_, abbreviation_number);
365 this->data_.insert(this->data_.end(), debug_info, die_end);
367 else
369 const int dwarf32_header_size =
370 sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint8_t);
371 if (debug_info + dwarf32_header_size >= debug_info_end)
373 this->failed(_("Debug info extends beyond .debug_info section; "
374 "failed to reduce debug info"));
375 return;
377 uint32_t compile_unit_size = compile_unit_start;
378 next_compile_unit = debug_info + compile_unit_size;
379 uint16_t version = read_from_pointer<16>(&debug_info);
380 uint32_t abbrev_offset = read_from_pointer<32>(&debug_info);
381 uint8_t address_size = read_from_pointer<8>(&debug_info);
382 size_t LEB_size;
383 uint64_t abbreviation_number = read_unsigned_LEB_128(debug_info,
384 &LEB_size);
385 debug_info += LEB_size;
386 unsigned char* die_abbrev = this->associated_abbrev_->get_new_abbrev(
387 &abbreviation_number, abbrev_offset);
388 unsigned char* die_end;
389 if (!this->get_die_end(debug_info, die_abbrev, &die_end,
390 debug_info_end, address_size, false))
392 this->failed(_("Invalid DIE in debug info; "
393 "failed to reduce debug info"));
394 return;
397 insert_into_vector<32>(
398 &this->data_,
399 (7 + get_length_as_unsigned_LEB_128(abbreviation_number)
400 + die_end - debug_info));
401 insert_into_vector<16>(&this->data_, version);
402 insert_into_vector<32>(&this->data_, 0);
403 insert_into_vector<8>(&this->data_, address_size);
404 write_unsigned_LEB_128(&this->data_, abbreviation_number);
405 this->data_.insert(this->data_.end(), debug_info, die_end);
407 debug_info = next_compile_unit;
409 this->set_data_size(data_.size());
412 void Output_reduced_debug_info_section::do_write(Output_file* of)
414 off_t offset = this->offset();
415 off_t data_size = this->data_size();
416 unsigned char* view = of->get_output_view(offset, data_size);
417 if (this->failed_)
418 memcpy(view, this->postprocessing_buffer(),
419 this->postprocessing_buffer_size());
420 else
421 memcpy(view, &this->data_.front(), data_size);
422 of->write_output_view(offset, data_size, view);
425 } // End namespace gold.