DwarfInfo.cpp 15 KB

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