reduced_debug_output.cc 14 KB

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