DwarfInfo.cpp 10 KB

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