DwarfInfo.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DwarfInfo.h"
  7. #include <AK/MemoryStream.h>
  8. namespace Debug::Dwarf {
  9. DwarfInfo::DwarfInfo(const ELF::Image& elf)
  10. : m_elf(elf)
  11. {
  12. m_debug_info_data = section_data(".debug_info"sv);
  13. m_abbreviation_data = section_data(".debug_abbrev"sv);
  14. m_debug_strings_data = section_data(".debug_str"sv);
  15. m_debug_line_strings_data = section_data(".debug_line_str"sv);
  16. populate_compilation_units();
  17. }
  18. ReadonlyBytes DwarfInfo::section_data(const StringView& section_name) const
  19. {
  20. auto section = m_elf.lookup_section(section_name);
  21. if (!section.has_value())
  22. return {};
  23. return section->bytes();
  24. }
  25. void DwarfInfo::populate_compilation_units()
  26. {
  27. if (!m_debug_info_data.data())
  28. return;
  29. InputMemoryStream stream { m_debug_info_data };
  30. while (!stream.eof()) {
  31. auto unit_offset = stream.offset();
  32. CompilationUnitHeader compilation_unit_header {};
  33. stream >> compilation_unit_header;
  34. VERIFY(compilation_unit_header.common.version <= 5);
  35. VERIFY(compilation_unit_header.address_size() == sizeof(u32));
  36. u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));
  37. m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header));
  38. stream.discard_or_error(length_after_header);
  39. }
  40. }
  41. AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t implicit_const_value,
  42. InputMemoryStream& debug_info_stream, const CompilationUnit* unit) const
  43. {
  44. AttributeValue value;
  45. auto assign_raw_bytes_value = [&](size_t length) {
  46. value.data.as_raw_bytes.length = length;
  47. value.data.as_raw_bytes.bytes = reinterpret_cast<const u8*>(debug_info_data().data()
  48. + debug_info_stream.offset());
  49. debug_info_stream.discard_or_error(length);
  50. };
  51. switch (form) {
  52. case AttributeDataForm::StringPointer: {
  53. u32 offset;
  54. debug_info_stream >> offset;
  55. VERIFY(!debug_info_stream.has_any_error());
  56. value.type = AttributeValue::Type::String;
  57. auto strings_data = debug_strings_data();
  58. value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  59. break;
  60. }
  61. case AttributeDataForm::Data1: {
  62. u8 data;
  63. debug_info_stream >> data;
  64. VERIFY(!debug_info_stream.has_any_error());
  65. value.type = AttributeValue::Type::UnsignedNumber;
  66. value.data.as_u32 = data;
  67. break;
  68. }
  69. case AttributeDataForm::Data2: {
  70. u16 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::Addr: {
  78. u32 address;
  79. debug_info_stream >> address;
  80. VERIFY(!debug_info_stream.has_any_error());
  81. value.type = AttributeValue::Type::UnsignedNumber;
  82. value.data.as_u32 = address;
  83. break;
  84. }
  85. case AttributeDataForm::SData: {
  86. ssize_t data;
  87. debug_info_stream.read_LEB128_signed(data);
  88. VERIFY(!debug_info_stream.has_any_error());
  89. value.type = AttributeValue::Type::SignedNumber;
  90. value.data.as_i32 = data;
  91. break;
  92. }
  93. case AttributeDataForm::UData: {
  94. size_t data;
  95. debug_info_stream.read_LEB128_unsigned(data);
  96. VERIFY(!debug_info_stream.has_any_error());
  97. value.type = AttributeValue::Type::UnsignedNumber;
  98. value.data.as_u32 = data;
  99. break;
  100. }
  101. case AttributeDataForm::SecOffset: {
  102. u32 data;
  103. debug_info_stream >> data;
  104. VERIFY(!debug_info_stream.has_any_error());
  105. value.type = AttributeValue::Type::SecOffset;
  106. value.data.as_u32 = data;
  107. break;
  108. }
  109. case AttributeDataForm::Data4: {
  110. u32 data;
  111. debug_info_stream >> data;
  112. VERIFY(!debug_info_stream.has_any_error());
  113. value.type = AttributeValue::Type::UnsignedNumber;
  114. value.data.as_u32 = data;
  115. break;
  116. }
  117. case AttributeDataForm::Data8: {
  118. u64 data;
  119. debug_info_stream >> data;
  120. VERIFY(!debug_info_stream.has_any_error());
  121. value.type = AttributeValue::Type::LongUnsignedNumber;
  122. value.data.as_u64 = data;
  123. break;
  124. }
  125. case AttributeDataForm::Ref4: {
  126. u32 data;
  127. debug_info_stream >> data;
  128. VERIFY(!debug_info_stream.has_any_error());
  129. value.type = AttributeValue::Type::DieReference;
  130. VERIFY(unit);
  131. value.data.as_u32 = data + unit->offset();
  132. break;
  133. }
  134. case AttributeDataForm::FlagPresent: {
  135. value.type = AttributeValue::Type::Boolean;
  136. value.data.as_bool = true;
  137. break;
  138. }
  139. case AttributeDataForm::ExprLoc: {
  140. size_t length;
  141. debug_info_stream.read_LEB128_unsigned(length);
  142. VERIFY(!debug_info_stream.has_any_error());
  143. value.type = AttributeValue::Type::DwarfExpression;
  144. assign_raw_bytes_value(length);
  145. break;
  146. }
  147. case AttributeDataForm::String: {
  148. String str;
  149. u32 str_offset = debug_info_stream.offset();
  150. debug_info_stream >> str;
  151. VERIFY(!debug_info_stream.has_any_error());
  152. value.type = AttributeValue::Type::String;
  153. value.data.as_string = reinterpret_cast<const char*>(str_offset + debug_info_data().data());
  154. break;
  155. }
  156. case AttributeDataForm::Block1: {
  157. value.type = AttributeValue::Type::RawBytes;
  158. u8 length;
  159. debug_info_stream >> length;
  160. VERIFY(!debug_info_stream.has_any_error());
  161. assign_raw_bytes_value(length);
  162. break;
  163. }
  164. case AttributeDataForm::Block2: {
  165. value.type = AttributeValue::Type::RawBytes;
  166. u16 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::Block4: {
  173. value.type = AttributeValue::Type::RawBytes;
  174. u32 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::Block: {
  181. value.type = AttributeValue::Type::RawBytes;
  182. size_t length;
  183. debug_info_stream.read_LEB128_unsigned(length);
  184. VERIFY(!debug_info_stream.has_any_error());
  185. assign_raw_bytes_value(length);
  186. break;
  187. }
  188. case AttributeDataForm::LineStrP: {
  189. u32 offset;
  190. debug_info_stream >> offset;
  191. VERIFY(!debug_info_stream.has_any_error());
  192. value.type = AttributeValue::Type::String;
  193. auto strings_data = debug_line_strings_data();
  194. value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  195. break;
  196. }
  197. case AttributeDataForm::ImplicitConst: {
  198. /* Value is part of the abbreviation record. */
  199. value.type = AttributeValue::Type::SignedNumber;
  200. value.data.as_i32 = implicit_const_value;
  201. break;
  202. }
  203. default:
  204. dbgln("Unimplemented AttributeDataForm: {}", (u32)form);
  205. VERIFY_NOT_REACHED();
  206. }
  207. return value;
  208. }
  209. }