DwarfInfo.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. m_debug_str_offsets_data = section_data(".debug_str_offsets"sv);
  23. m_debug_addr_data = section_data(".debug_addr"sv);
  24. m_debug_ranges_data = section_data(".debug_ranges"sv);
  25. populate_compilation_units();
  26. }
  27. ReadonlyBytes DwarfInfo::section_data(StringView section_name) const
  28. {
  29. auto section = m_elf.lookup_section(section_name);
  30. if (!section.has_value())
  31. return {};
  32. return section->bytes();
  33. }
  34. void DwarfInfo::populate_compilation_units()
  35. {
  36. if (!m_debug_info_data.data())
  37. return;
  38. InputMemoryStream debug_info_stream { m_debug_info_data };
  39. InputMemoryStream line_info_stream { m_debug_line_data };
  40. while (!debug_info_stream.eof()) {
  41. auto unit_offset = debug_info_stream.offset();
  42. CompilationUnitHeader compilation_unit_header {};
  43. debug_info_stream >> compilation_unit_header;
  44. VERIFY(compilation_unit_header.common.version <= 5);
  45. VERIFY(compilation_unit_header.address_size() == sizeof(FlatPtr));
  46. u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));
  47. auto line_program = make<LineProgram>(*this, line_info_stream);
  48. // HACK: Clang generates line programs for embedded resource assembly files, but not compile units.
  49. // Meaning that for graphical applications, some line info data would be unread, triggering the assertion below.
  50. // As a fix, we don't create compilation units for line programs that come from resource files.
  51. #ifdef __clang__
  52. if (line_program->source_files().size() == 1 && line_program->source_files()[0].name.view().contains("serenity_icon_"sv)) {
  53. debug_info_stream.seek(unit_offset);
  54. } else
  55. #endif
  56. {
  57. m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header, move(line_program)));
  58. debug_info_stream.discard_or_error(length_after_header);
  59. }
  60. }
  61. VERIFY(line_info_stream.eof());
  62. }
  63. AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t implicit_const_value,
  64. InputMemoryStream& debug_info_stream, const CompilationUnit* unit) const
  65. {
  66. AttributeValue value;
  67. value.m_form = form;
  68. value.m_compilation_unit = unit;
  69. auto assign_raw_bytes_value = [&](size_t length) {
  70. value.m_data.as_raw_bytes = { reinterpret_cast<const u8*>(debug_info_data().data() + debug_info_stream.offset()), length };
  71. debug_info_stream.discard_or_error(length);
  72. VERIFY(!debug_info_stream.has_any_error());
  73. };
  74. switch (form) {
  75. case AttributeDataForm::StringPointer: {
  76. u32 offset;
  77. debug_info_stream >> offset;
  78. VERIFY(!debug_info_stream.has_any_error());
  79. value.m_type = AttributeValue::Type::String;
  80. auto strings_data = debug_strings_data();
  81. value.m_data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  82. break;
  83. }
  84. case AttributeDataForm::Data1: {
  85. u8 data;
  86. debug_info_stream >> data;
  87. VERIFY(!debug_info_stream.has_any_error());
  88. value.m_type = AttributeValue::Type::UnsignedNumber;
  89. value.m_data.as_unsigned = data;
  90. break;
  91. }
  92. case AttributeDataForm::Data2: {
  93. u16 data;
  94. debug_info_stream >> data;
  95. VERIFY(!debug_info_stream.has_any_error());
  96. value.m_type = AttributeValue::Type::UnsignedNumber;
  97. value.m_data.as_signed = data;
  98. break;
  99. }
  100. case AttributeDataForm::Addr: {
  101. FlatPtr address;
  102. debug_info_stream >> address;
  103. VERIFY(!debug_info_stream.has_any_error());
  104. value.m_type = AttributeValue::Type::Address;
  105. value.m_data.as_addr = address;
  106. break;
  107. }
  108. case AttributeDataForm::SData: {
  109. i64 data;
  110. debug_info_stream.read_LEB128_signed(data);
  111. VERIFY(!debug_info_stream.has_any_error());
  112. value.m_type = AttributeValue::Type::SignedNumber;
  113. value.m_data.as_signed = data;
  114. break;
  115. }
  116. case AttributeDataForm::UData: {
  117. u64 data;
  118. debug_info_stream.read_LEB128_unsigned(data);
  119. VERIFY(!debug_info_stream.has_any_error());
  120. value.m_type = AttributeValue::Type::UnsignedNumber;
  121. value.m_data.as_unsigned = data;
  122. break;
  123. }
  124. case AttributeDataForm::SecOffset: {
  125. u32 data;
  126. debug_info_stream >> data;
  127. VERIFY(!debug_info_stream.has_any_error());
  128. value.m_type = AttributeValue::Type::SecOffset;
  129. value.m_data.as_unsigned = data;
  130. break;
  131. }
  132. case AttributeDataForm::Data4: {
  133. u32 data;
  134. debug_info_stream >> data;
  135. VERIFY(!debug_info_stream.has_any_error());
  136. value.m_type = AttributeValue::Type::UnsignedNumber;
  137. value.m_data.as_unsigned = data;
  138. break;
  139. }
  140. case AttributeDataForm::Data8: {
  141. u64 data;
  142. debug_info_stream >> data;
  143. VERIFY(!debug_info_stream.has_any_error());
  144. value.m_type = AttributeValue::Type::UnsignedNumber;
  145. value.m_data.as_unsigned = data;
  146. break;
  147. }
  148. case AttributeDataForm::Data16: {
  149. value.m_type = AttributeValue::Type::RawBytes;
  150. assign_raw_bytes_value(16);
  151. VERIFY(!debug_info_stream.has_any_error());
  152. break;
  153. }
  154. case AttributeDataForm::Ref4: {
  155. u32 data;
  156. debug_info_stream >> data;
  157. VERIFY(!debug_info_stream.has_any_error());
  158. value.m_type = AttributeValue::Type::DieReference;
  159. VERIFY(unit);
  160. value.m_data.as_unsigned = data + unit->offset();
  161. break;
  162. }
  163. case AttributeDataForm::FlagPresent: {
  164. value.m_type = AttributeValue::Type::Boolean;
  165. value.m_data.as_bool = true;
  166. break;
  167. }
  168. case AttributeDataForm::ExprLoc: {
  169. size_t length;
  170. debug_info_stream.read_LEB128_unsigned(length);
  171. VERIFY(!debug_info_stream.has_any_error());
  172. value.m_type = AttributeValue::Type::DwarfExpression;
  173. assign_raw_bytes_value(length);
  174. break;
  175. }
  176. case AttributeDataForm::String: {
  177. String str;
  178. u32 str_offset = debug_info_stream.offset();
  179. debug_info_stream >> str;
  180. VERIFY(!debug_info_stream.has_any_error());
  181. value.m_type = AttributeValue::Type::String;
  182. value.m_data.as_string = reinterpret_cast<const char*>(str_offset + debug_info_data().data());
  183. break;
  184. }
  185. case AttributeDataForm::Block1: {
  186. value.m_type = AttributeValue::Type::RawBytes;
  187. u8 length;
  188. debug_info_stream >> length;
  189. VERIFY(!debug_info_stream.has_any_error());
  190. assign_raw_bytes_value(length);
  191. break;
  192. }
  193. case AttributeDataForm::Block2: {
  194. value.m_type = AttributeValue::Type::RawBytes;
  195. u16 length;
  196. debug_info_stream >> length;
  197. VERIFY(!debug_info_stream.has_any_error());
  198. assign_raw_bytes_value(length);
  199. break;
  200. }
  201. case AttributeDataForm::Block4: {
  202. value.m_type = AttributeValue::Type::RawBytes;
  203. u32 length;
  204. debug_info_stream >> length;
  205. VERIFY(!debug_info_stream.has_any_error());
  206. assign_raw_bytes_value(length);
  207. break;
  208. }
  209. case AttributeDataForm::Block: {
  210. value.m_type = AttributeValue::Type::RawBytes;
  211. size_t length;
  212. debug_info_stream.read_LEB128_unsigned(length);
  213. VERIFY(!debug_info_stream.has_any_error());
  214. assign_raw_bytes_value(length);
  215. break;
  216. }
  217. case AttributeDataForm::LineStrP: {
  218. u32 offset;
  219. debug_info_stream >> offset;
  220. VERIFY(!debug_info_stream.has_any_error());
  221. value.m_type = AttributeValue::Type::String;
  222. auto strings_data = debug_line_strings_data();
  223. value.m_data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  224. break;
  225. }
  226. case AttributeDataForm::ImplicitConst: {
  227. /* Value is part of the abbreviation record. */
  228. value.m_type = AttributeValue::Type::SignedNumber;
  229. value.m_data.as_signed = implicit_const_value;
  230. break;
  231. }
  232. case AttributeDataForm::StrX1: {
  233. u8 index;
  234. debug_info_stream >> index;
  235. VERIFY(!debug_info_stream.has_any_error());
  236. value.m_type = AttributeValue::Type::String;
  237. value.m_data.as_unsigned = index;
  238. break;
  239. }
  240. case AttributeDataForm::StrX2: {
  241. u16 index;
  242. debug_info_stream >> index;
  243. VERIFY(!debug_info_stream.has_any_error());
  244. value.m_type = AttributeValue::Type::String;
  245. value.m_data.as_unsigned = index;
  246. break;
  247. }
  248. case AttributeDataForm::StrX4: {
  249. u32 index;
  250. debug_info_stream >> index;
  251. VERIFY(!debug_info_stream.has_any_error());
  252. value.m_type = AttributeValue::Type::String;
  253. value.m_data.as_unsigned = index;
  254. break;
  255. }
  256. case AttributeDataForm::StrX: {
  257. size_t index;
  258. debug_info_stream.read_LEB128_unsigned(index);
  259. VERIFY(!debug_info_stream.has_any_error());
  260. value.m_type = AttributeValue::Type::String;
  261. value.m_data.as_unsigned = index;
  262. break;
  263. }
  264. case AttributeDataForm::AddrX1: {
  265. u8 index;
  266. debug_info_stream >> index;
  267. VERIFY(!debug_info_stream.has_any_error());
  268. value.m_type = AttributeValue::Type::Address;
  269. value.m_data.as_unsigned = index;
  270. break;
  271. }
  272. case AttributeDataForm::AddrX2: {
  273. u16 index;
  274. debug_info_stream >> index;
  275. VERIFY(!debug_info_stream.has_any_error());
  276. value.m_type = AttributeValue::Type::Address;
  277. value.m_data.as_unsigned = index;
  278. break;
  279. }
  280. case AttributeDataForm::AddrX4: {
  281. u32 index;
  282. debug_info_stream >> index;
  283. VERIFY(!debug_info_stream.has_any_error());
  284. value.m_type = AttributeValue::Type::Address;
  285. value.m_data.as_unsigned = index;
  286. break;
  287. }
  288. case AttributeDataForm::AddrX: {
  289. size_t index;
  290. debug_info_stream.read_LEB128_unsigned(index);
  291. VERIFY(!debug_info_stream.has_any_error());
  292. value.m_type = AttributeValue::Type::Address;
  293. value.m_data.as_unsigned = index;
  294. break;
  295. }
  296. case AttributeDataForm::RngListX: {
  297. size_t index;
  298. debug_info_stream.read_LEB128_unsigned(index);
  299. VERIFY(!debug_info_stream.has_any_error());
  300. value.m_type = AttributeValue::Type::UnsignedNumber;
  301. value.m_data.as_unsigned = index;
  302. break;
  303. }
  304. default:
  305. dbgln("Unimplemented AttributeDataForm: {}", (u32)form);
  306. VERIFY_NOT_REACHED();
  307. }
  308. return value;
  309. }
  310. void DwarfInfo::build_cached_dies() const
  311. {
  312. auto insert_to_cache = [this](DIE const& die, DIERange const& range) {
  313. m_cached_dies_by_range.insert(range.start_address, DIEAndRange { die, range });
  314. m_cached_dies_by_offset.insert(die.offset(), die);
  315. };
  316. auto get_ranges_of_die = [this](DIE const& die) -> Vector<DIERange> {
  317. auto ranges = die.get_attribute(Attribute::Ranges);
  318. if (ranges.has_value()) {
  319. size_t offset;
  320. if (ranges->form() == AttributeDataForm::SecOffset) {
  321. offset = ranges->as_unsigned();
  322. } else {
  323. auto index = ranges->as_unsigned();
  324. auto base = die.compilation_unit().range_lists_base();
  325. // FIXME: This assumes that the format is DWARf32
  326. auto offsets = reinterpret_cast<u32 const*>(debug_range_lists_data().offset(base));
  327. offset = offsets[index] + base;
  328. }
  329. Vector<DIERange> entries;
  330. if (die.compilation_unit().dwarf_version() == 5) {
  331. AddressRangesV5 address_ranges(debug_range_lists_data(), offset, die.compilation_unit());
  332. address_ranges.for_each_range([&entries](auto range) {
  333. entries.empend(range.start, range.end);
  334. });
  335. } else {
  336. AddressRangesV4 address_ranges(debug_ranges_data(), offset, die.compilation_unit());
  337. address_ranges.for_each_range([&entries](auto range) {
  338. entries.empend(range.start, range.end);
  339. });
  340. }
  341. return entries;
  342. }
  343. auto start = die.get_attribute(Attribute::LowPc);
  344. auto end = die.get_attribute(Attribute::HighPc);
  345. if (!start.has_value() || !end.has_value())
  346. return {};
  347. VERIFY(start->type() == Dwarf::AttributeValue::Type::Address);
  348. // DW_AT_high_pc attribute can have different meanings depending on the attribute form.
  349. // (Dwarf version 5, section 2.17.2).
  350. uint32_t range_end = 0;
  351. if (end->form() == Dwarf::AttributeDataForm::Addr)
  352. range_end = end->as_addr();
  353. else
  354. range_end = start->as_addr() + end->as_unsigned();
  355. return { DIERange { start.value().as_addr(), range_end } };
  356. };
  357. // If we simply use a lambda, type deduction fails because it's used recursively.
  358. Function<void(DIE const& die)> insert_to_cache_recursively;
  359. insert_to_cache_recursively = [&](DIE const& die) {
  360. if (die.offset() == 0 || die.parent_offset().has_value()) {
  361. auto ranges = get_ranges_of_die(die);
  362. for (auto& range : ranges) {
  363. insert_to_cache(die, range);
  364. }
  365. }
  366. die.for_each_child([&](DIE const& child) {
  367. if (!child.is_null()) {
  368. insert_to_cache_recursively(child);
  369. }
  370. });
  371. };
  372. for_each_compilation_unit([&](CompilationUnit const& compilation_unit) {
  373. insert_to_cache_recursively(compilation_unit.root_die());
  374. });
  375. m_built_cached_dies = true;
  376. }
  377. Optional<DIE> DwarfInfo::get_die_at_address(FlatPtr address) const
  378. {
  379. if (!m_built_cached_dies)
  380. build_cached_dies();
  381. auto iter = m_cached_dies_by_range.find_largest_not_above_iterator(address);
  382. while (!iter.is_end() && !iter.is_begin() && iter->range.end_address < address) {
  383. --iter;
  384. }
  385. if (iter.is_end())
  386. return {};
  387. if (iter->range.start_address > address || iter->range.end_address < address) {
  388. return {};
  389. }
  390. return iter->die;
  391. }
  392. Optional<DIE> DwarfInfo::get_cached_die_at_offset(FlatPtr offset) const
  393. {
  394. if (!m_built_cached_dies)
  395. build_cached_dies();
  396. auto* die = m_cached_dies_by_offset.find(offset);
  397. if (!die)
  398. return {};
  399. return *die;
  400. }
  401. }