DwarfInfo.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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");
  13. m_abbreviation_data = section_data(".debug_abbrev");
  14. m_debug_strings_data = section_data(".debug_str");
  15. populate_compilation_units();
  16. }
  17. ReadonlyBytes DwarfInfo::section_data(const String& section_name) const
  18. {
  19. auto section = m_elf.lookup_section(section_name);
  20. if (section.is_undefined())
  21. return {};
  22. return section.bytes();
  23. }
  24. void DwarfInfo::populate_compilation_units()
  25. {
  26. if (!m_debug_info_data.data())
  27. return;
  28. InputMemoryStream stream { m_debug_info_data };
  29. while (!stream.eof()) {
  30. auto unit_offset = stream.offset();
  31. CompilationUnitHeader compilation_unit_header {};
  32. stream >> Bytes { &compilation_unit_header, sizeof(compilation_unit_header) };
  33. VERIFY(compilation_unit_header.address_size == sizeof(u32));
  34. VERIFY(compilation_unit_header.version <= 4);
  35. u32 length_after_header = compilation_unit_header.length - (sizeof(CompilationUnitHeader) - offsetof(CompilationUnitHeader, version));
  36. m_compilation_units.empend(*this, unit_offset, compilation_unit_header);
  37. stream.discard_or_error(length_after_header);
  38. }
  39. }
  40. AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form,
  41. InputMemoryStream& debug_info_stream, const CompilationUnit* unit) const
  42. {
  43. AttributeValue value;
  44. auto assign_raw_bytes_value = [&](size_t length) {
  45. value.data.as_raw_bytes.length = length;
  46. value.data.as_raw_bytes.bytes = reinterpret_cast<const u8*>(debug_info_data().data()
  47. + debug_info_stream.offset());
  48. debug_info_stream.discard_or_error(length);
  49. };
  50. switch (form) {
  51. case AttributeDataForm::StringPointer: {
  52. u32 offset;
  53. debug_info_stream >> offset;
  54. VERIFY(!debug_info_stream.has_any_error());
  55. value.type = AttributeValue::Type::String;
  56. auto strings_data = debug_strings_data();
  57. value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  58. break;
  59. }
  60. case AttributeDataForm::Data1: {
  61. u8 data;
  62. debug_info_stream >> data;
  63. VERIFY(!debug_info_stream.has_any_error());
  64. value.type = AttributeValue::Type::UnsignedNumber;
  65. value.data.as_u32 = data;
  66. break;
  67. }
  68. case AttributeDataForm::Data2: {
  69. u16 data;
  70. debug_info_stream >> data;
  71. VERIFY(!debug_info_stream.has_any_error());
  72. value.type = AttributeValue::Type::UnsignedNumber;
  73. value.data.as_u32 = data;
  74. break;
  75. }
  76. case AttributeDataForm::Addr: {
  77. u32 address;
  78. debug_info_stream >> address;
  79. VERIFY(!debug_info_stream.has_any_error());
  80. value.type = AttributeValue::Type::UnsignedNumber;
  81. value.data.as_u32 = address;
  82. break;
  83. }
  84. case AttributeDataForm::SData: {
  85. ssize_t data;
  86. debug_info_stream.read_LEB128_signed(data);
  87. VERIFY(!debug_info_stream.has_any_error());
  88. value.type = AttributeValue::Type::SignedNumber;
  89. value.data.as_i32 = data;
  90. break;
  91. }
  92. case AttributeDataForm::SecOffset: {
  93. u32 data;
  94. debug_info_stream >> data;
  95. VERIFY(!debug_info_stream.has_any_error());
  96. value.type = AttributeValue::Type::SecOffset;
  97. value.data.as_u32 = data;
  98. break;
  99. }
  100. case AttributeDataForm::Data4: {
  101. u32 data;
  102. debug_info_stream >> data;
  103. VERIFY(!debug_info_stream.has_any_error());
  104. value.type = AttributeValue::Type::UnsignedNumber;
  105. value.data.as_u32 = data;
  106. break;
  107. }
  108. case AttributeDataForm::Data8: {
  109. u64 data;
  110. debug_info_stream >> data;
  111. VERIFY(!debug_info_stream.has_any_error());
  112. value.type = AttributeValue::Type::LongUnsignedNumber;
  113. value.data.as_u64 = data;
  114. break;
  115. }
  116. case AttributeDataForm::Ref4: {
  117. u32 data;
  118. debug_info_stream >> data;
  119. VERIFY(!debug_info_stream.has_any_error());
  120. value.type = AttributeValue::Type::DieReference;
  121. VERIFY(unit);
  122. value.data.as_u32 = data + unit->offset();
  123. break;
  124. }
  125. case AttributeDataForm::FlagPresent: {
  126. value.type = AttributeValue::Type::Boolean;
  127. value.data.as_bool = true;
  128. break;
  129. }
  130. case AttributeDataForm::ExprLoc: {
  131. size_t length;
  132. debug_info_stream.read_LEB128_unsigned(length);
  133. VERIFY(!debug_info_stream.has_any_error());
  134. value.type = AttributeValue::Type::DwarfExpression;
  135. assign_raw_bytes_value(length);
  136. break;
  137. }
  138. case AttributeDataForm::String: {
  139. String str;
  140. u32 str_offset = debug_info_stream.offset();
  141. debug_info_stream >> str;
  142. VERIFY(!debug_info_stream.has_any_error());
  143. value.type = AttributeValue::Type::String;
  144. value.data.as_string = reinterpret_cast<const char*>(str_offset + debug_info_data().data());
  145. break;
  146. }
  147. case AttributeDataForm::Block1: {
  148. value.type = AttributeValue::Type::RawBytes;
  149. u8 length;
  150. debug_info_stream >> length;
  151. VERIFY(!debug_info_stream.has_any_error());
  152. assign_raw_bytes_value(length);
  153. break;
  154. }
  155. case AttributeDataForm::Block2: {
  156. value.type = AttributeValue::Type::RawBytes;
  157. u16 length;
  158. debug_info_stream >> length;
  159. VERIFY(!debug_info_stream.has_any_error());
  160. assign_raw_bytes_value(length);
  161. break;
  162. }
  163. case AttributeDataForm::Block4: {
  164. value.type = AttributeValue::Type::RawBytes;
  165. u32 length;
  166. debug_info_stream >> length;
  167. VERIFY(!debug_info_stream.has_any_error());
  168. assign_raw_bytes_value(length);
  169. break;
  170. }
  171. case AttributeDataForm::Block: {
  172. value.type = AttributeValue::Type::RawBytes;
  173. size_t length;
  174. debug_info_stream.read_LEB128_unsigned(length);
  175. VERIFY(!debug_info_stream.has_any_error());
  176. assign_raw_bytes_value(length);
  177. break;
  178. }
  179. default:
  180. dbgln("Unimplemented AttributeDataForm: {}", (u32)form);
  181. VERIFY_NOT_REACHED();
  182. }
  183. return value;
  184. }
  185. }