DwarfInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2020-2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DwarfInfo.h"
  7. #include "AddressRanges.h"
  8. #include "AttributeValue.h"
  9. #include "CompilationUnit.h"
  10. #include <AK/MemoryStream.h>
  11. #include <LibDebug/DebugInfo.h>
  12. namespace Debug::Dwarf {
  13. DwarfInfo::DwarfInfo(ELF::Image const& elf)
  14. : m_elf(elf)
  15. {
  16. m_debug_info_data = section_data(".debug_info"sv);
  17. m_abbreviation_data = section_data(".debug_abbrev"sv);
  18. m_debug_strings_data = section_data(".debug_str"sv);
  19. m_debug_line_data = section_data(".debug_line"sv);
  20. m_debug_line_strings_data = section_data(".debug_line_str"sv);
  21. m_debug_range_lists_data = section_data(".debug_rnglists"sv);
  22. populate_compilation_units();
  23. }
  24. ReadonlyBytes DwarfInfo::section_data(StringView const& section_name) const
  25. {
  26. auto section = m_elf.lookup_section(section_name);
  27. if (!section.has_value())
  28. return {};
  29. return section->bytes();
  30. }
  31. void DwarfInfo::populate_compilation_units()
  32. {
  33. if (!m_debug_info_data.data())
  34. return;
  35. InputMemoryStream debug_info_stream { m_debug_info_data };
  36. InputMemoryStream line_info_stream { m_debug_line_data };
  37. while (!debug_info_stream.eof()) {
  38. auto unit_offset = debug_info_stream.offset();
  39. CompilationUnitHeader compilation_unit_header {};
  40. debug_info_stream >> compilation_unit_header;
  41. VERIFY(compilation_unit_header.common.version <= 5);
  42. VERIFY(compilation_unit_header.address_size() == sizeof(FlatPtr));
  43. u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));
  44. auto line_program = make<LineProgram>(*this, line_info_stream);
  45. m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header, move(line_program)));
  46. debug_info_stream.discard_or_error(length_after_header);
  47. }
  48. VERIFY(line_info_stream.eof());
  49. }
  50. AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t implicit_const_value,
  51. InputMemoryStream& debug_info_stream, const CompilationUnit* unit) const
  52. {
  53. AttributeValue value;
  54. value.form = form;
  55. auto assign_raw_bytes_value = [&](size_t length) {
  56. value.data.as_raw_bytes.length = length;
  57. value.data.as_raw_bytes.bytes = reinterpret_cast<const u8*>(debug_info_data().data()
  58. + debug_info_stream.offset());
  59. debug_info_stream.discard_or_error(length);
  60. };
  61. switch (form) {
  62. case AttributeDataForm::StringPointer: {
  63. u32 offset;
  64. debug_info_stream >> offset;
  65. VERIFY(!debug_info_stream.has_any_error());
  66. value.type = AttributeValue::Type::String;
  67. auto strings_data = debug_strings_data();
  68. value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  69. break;
  70. }
  71. case AttributeDataForm::Data1: {
  72. u8 data;
  73. debug_info_stream >> data;
  74. VERIFY(!debug_info_stream.has_any_error());
  75. value.type = AttributeValue::Type::UnsignedNumber;
  76. value.data.as_unsigned = data;
  77. break;
  78. }
  79. case AttributeDataForm::Data2: {
  80. u16 data;
  81. debug_info_stream >> data;
  82. VERIFY(!debug_info_stream.has_any_error());
  83. value.type = AttributeValue::Type::UnsignedNumber;
  84. value.data.as_signed = data;
  85. break;
  86. }
  87. case AttributeDataForm::Addr: {
  88. FlatPtr address;
  89. debug_info_stream >> address;
  90. VERIFY(!debug_info_stream.has_any_error());
  91. value.type = AttributeValue::Type::UnsignedNumber;
  92. value.data.as_addr = address;
  93. break;
  94. }
  95. case AttributeDataForm::SData: {
  96. i64 data;
  97. debug_info_stream.read_LEB128_signed(data);
  98. VERIFY(!debug_info_stream.has_any_error());
  99. value.type = AttributeValue::Type::SignedNumber;
  100. value.data.as_signed = data;
  101. break;
  102. }
  103. case AttributeDataForm::UData: {
  104. u64 data;
  105. debug_info_stream.read_LEB128_unsigned(data);
  106. VERIFY(!debug_info_stream.has_any_error());
  107. value.type = AttributeValue::Type::UnsignedNumber;
  108. value.data.as_unsigned = data;
  109. break;
  110. }
  111. case AttributeDataForm::SecOffset: {
  112. u32 data;
  113. debug_info_stream >> data;
  114. VERIFY(!debug_info_stream.has_any_error());
  115. value.type = AttributeValue::Type::SecOffset;
  116. value.data.as_unsigned = data;
  117. break;
  118. }
  119. case AttributeDataForm::Data4: {
  120. u32 data;
  121. debug_info_stream >> data;
  122. VERIFY(!debug_info_stream.has_any_error());
  123. value.type = AttributeValue::Type::UnsignedNumber;
  124. value.data.as_unsigned = data;
  125. break;
  126. }
  127. case AttributeDataForm::Data8: {
  128. u64 data;
  129. debug_info_stream >> data;
  130. VERIFY(!debug_info_stream.has_any_error());
  131. value.type = AttributeValue::Type::UnsignedNumber;
  132. value.data.as_unsigned = data;
  133. break;
  134. }
  135. case AttributeDataForm::Ref4: {
  136. u32 data;
  137. debug_info_stream >> data;
  138. VERIFY(!debug_info_stream.has_any_error());
  139. value.type = AttributeValue::Type::DieReference;
  140. VERIFY(unit);
  141. value.data.as_unsigned = data + unit->offset();
  142. break;
  143. }
  144. case AttributeDataForm::FlagPresent: {
  145. value.type = AttributeValue::Type::Boolean;
  146. value.data.as_bool = true;
  147. break;
  148. }
  149. case AttributeDataForm::ExprLoc: {
  150. size_t length;
  151. debug_info_stream.read_LEB128_unsigned(length);
  152. VERIFY(!debug_info_stream.has_any_error());
  153. value.type = AttributeValue::Type::DwarfExpression;
  154. assign_raw_bytes_value(length);
  155. break;
  156. }
  157. case AttributeDataForm::String: {
  158. String str;
  159. u32 str_offset = debug_info_stream.offset();
  160. debug_info_stream >> str;
  161. VERIFY(!debug_info_stream.has_any_error());
  162. value.type = AttributeValue::Type::String;
  163. value.data.as_string = reinterpret_cast<const char*>(str_offset + debug_info_data().data());
  164. break;
  165. }
  166. case AttributeDataForm::Block1: {
  167. value.type = AttributeValue::Type::RawBytes;
  168. u8 length;
  169. debug_info_stream >> length;
  170. VERIFY(!debug_info_stream.has_any_error());
  171. assign_raw_bytes_value(length);
  172. break;
  173. }
  174. case AttributeDataForm::Block2: {
  175. value.type = AttributeValue::Type::RawBytes;
  176. u16 length;
  177. debug_info_stream >> length;
  178. VERIFY(!debug_info_stream.has_any_error());
  179. assign_raw_bytes_value(length);
  180. break;
  181. }
  182. case AttributeDataForm::Block4: {
  183. value.type = AttributeValue::Type::RawBytes;
  184. u32 length;
  185. debug_info_stream >> length;
  186. VERIFY(!debug_info_stream.has_any_error());
  187. assign_raw_bytes_value(length);
  188. break;
  189. }
  190. case AttributeDataForm::Block: {
  191. value.type = AttributeValue::Type::RawBytes;
  192. size_t length;
  193. debug_info_stream.read_LEB128_unsigned(length);
  194. VERIFY(!debug_info_stream.has_any_error());
  195. assign_raw_bytes_value(length);
  196. break;
  197. }
  198. case AttributeDataForm::LineStrP: {
  199. u32 offset;
  200. debug_info_stream >> offset;
  201. VERIFY(!debug_info_stream.has_any_error());
  202. value.type = AttributeValue::Type::String;
  203. auto strings_data = debug_line_strings_data();
  204. value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  205. break;
  206. }
  207. case AttributeDataForm::ImplicitConst: {
  208. /* Value is part of the abbreviation record. */
  209. value.type = AttributeValue::Type::SignedNumber;
  210. value.data.as_signed = implicit_const_value;
  211. break;
  212. }
  213. default:
  214. dbgln("Unimplemented AttributeDataForm: {}", (u32)form);
  215. VERIFY_NOT_REACHED();
  216. }
  217. return value;
  218. }
  219. void DwarfInfo::build_cached_dies() const
  220. {
  221. auto insert_to_cache = [this](DIE const& die, DIERange const& range) {
  222. m_cached_dies_by_range.insert(range.start_address, DIEAndRange { die, range });
  223. m_cached_dies_by_offset.insert(die.offset(), die);
  224. };
  225. auto get_ranges_of_die = [this](DIE const& die) -> Vector<DIERange> {
  226. auto ranges = die.get_attribute(Attribute::Ranges);
  227. if (ranges.has_value()) {
  228. // TODO Support DW_FORM_rnglistx
  229. if (ranges->form != AttributeDataForm::SecOffset)
  230. return {};
  231. auto offset = ranges->data.as_addr;
  232. AddressRanges address_ranges(debug_range_lists_data(), offset, die.compilation_unit());
  233. Vector<DIERange> entries;
  234. address_ranges.for_each_range([&entries](auto range) {
  235. entries.empend(range.start, range.end);
  236. });
  237. return entries;
  238. }
  239. auto start = die.get_attribute(Attribute::LowPc);
  240. auto end = die.get_attribute(Attribute::HighPc);
  241. if (!start.has_value() || !end.has_value())
  242. return {};
  243. VERIFY(start->form == Dwarf::AttributeDataForm::Addr);
  244. // DW_AT_high_pc attribute can have different meanings depending on the attribute form.
  245. // (Dwarf version 5, section 2.17.2).
  246. uint32_t range_end = 0;
  247. if (end->form == Dwarf::AttributeDataForm::Addr)
  248. range_end = end->data.as_addr;
  249. else
  250. range_end = start->data.as_unsigned + end->data.as_unsigned;
  251. return { DIERange { (FlatPtr)start.value().data.as_addr, range_end } };
  252. };
  253. // If we simply use a lambda, type deduction fails because it's used recursively.
  254. Function<void(DIE const& die)> insert_to_cache_recursively;
  255. insert_to_cache_recursively = [&](DIE const& die) {
  256. if (die.offset() == 0 || die.parent_offset().has_value()) {
  257. auto ranges = get_ranges_of_die(die);
  258. for (auto& range : ranges) {
  259. insert_to_cache(die, range);
  260. }
  261. }
  262. die.for_each_child([&](DIE const& child) {
  263. if (!child.is_null()) {
  264. insert_to_cache_recursively(child);
  265. }
  266. });
  267. };
  268. for_each_compilation_unit([&](CompilationUnit const& compilation_unit) {
  269. insert_to_cache_recursively(compilation_unit.root_die());
  270. });
  271. m_built_cached_dies = true;
  272. }
  273. Optional<DIE> DwarfInfo::get_die_at_address(FlatPtr address) const
  274. {
  275. if (!m_built_cached_dies)
  276. build_cached_dies();
  277. auto iter = m_cached_dies_by_range.find_largest_not_above_iterator(address);
  278. while (!iter.is_end() && !iter.is_begin() && iter->range.end_address < address) {
  279. --iter;
  280. }
  281. if (iter.is_end())
  282. return {};
  283. if (iter->range.start_address > address || iter->range.end_address < address) {
  284. return {};
  285. }
  286. return iter->die;
  287. }
  288. Optional<DIE> DwarfInfo::get_cached_die_at_offset(FlatPtr offset) const
  289. {
  290. if (!m_built_cached_dies)
  291. build_cached_dies();
  292. auto* die = m_cached_dies_by_offset.find(offset);
  293. if (!die)
  294. return {};
  295. return *die;
  296. }
  297. }