DIE.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DIE.h"
  27. #include "CompilationUnit.h"
  28. #include "DwarfInfo.h"
  29. #include <AK/ByteBuffer.h>
  30. #include <AK/Stream.h>
  31. namespace Dwarf {
  32. DIE::DIE(const CompilationUnit& unit, u32 offset)
  33. : m_compilation_unit(unit)
  34. , m_offset(offset)
  35. {
  36. InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data().span());
  37. stream.discard_or_error(m_offset);
  38. stream.read_LEB128_unsigned(m_abbreviation_code);
  39. m_data_offset = stream.offset();
  40. if (m_abbreviation_code == 0) {
  41. // An abbrevation code of 0 ( = null DIE entry) means the end of a chain of sibilings
  42. m_tag = EntryTag::None;
  43. } else {
  44. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  45. ASSERT(abbreviation_info.has_value());
  46. m_tag = abbreviation_info.value().tag;
  47. m_has_children = abbreviation_info.value().has_children;
  48. // We iterate the attributes data only to calculate this DIE's size
  49. for (auto attribute_spec : abbreviation_info.value().attribute_specifications) {
  50. get_attribute_value(attribute_spec.form, stream);
  51. }
  52. }
  53. m_size = stream.offset() - m_offset;
  54. }
  55. DIE::AttributeValue DIE::get_attribute_value(AttributeDataForm form,
  56. InputMemoryStream& debug_info_stream) const
  57. {
  58. AttributeValue value;
  59. auto assign_raw_bytes_value = [&](size_t length) {
  60. value.data.as_raw_bytes.length = length;
  61. value.data.as_raw_bytes.bytes = reinterpret_cast<const u8*>(m_compilation_unit.dwarf_info().debug_info_data().data()
  62. + debug_info_stream.offset());
  63. debug_info_stream.discard_or_error(length);
  64. };
  65. switch (form) {
  66. case AttributeDataForm::StringPointer: {
  67. u32 offset;
  68. debug_info_stream >> offset;
  69. value.type = AttributeValue::Type::String;
  70. auto strings_data = m_compilation_unit.dwarf_info().debug_strings_data();
  71. value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  72. break;
  73. }
  74. case AttributeDataForm::Data1: {
  75. u8 data;
  76. debug_info_stream >> data;
  77. value.type = AttributeValue::Type::UnsignedNumber;
  78. value.data.as_u32 = data;
  79. break;
  80. }
  81. case AttributeDataForm::Data2: {
  82. u16 data;
  83. debug_info_stream >> data;
  84. value.type = AttributeValue::Type::UnsignedNumber;
  85. value.data.as_u32 = data;
  86. break;
  87. }
  88. case AttributeDataForm::Addr: {
  89. u32 address;
  90. debug_info_stream >> address;
  91. value.type = AttributeValue::Type::UnsignedNumber;
  92. value.data.as_u32 = address;
  93. break;
  94. }
  95. case AttributeDataForm::SecOffset: {
  96. u32 data;
  97. debug_info_stream >> data;
  98. value.type = AttributeValue::Type::SecOffset;
  99. value.data.as_u32 = data;
  100. break;
  101. }
  102. case AttributeDataForm::Data4: {
  103. u32 data;
  104. debug_info_stream >> data;
  105. value.type = AttributeValue::Type::UnsignedNumber;
  106. value.data.as_u32 = data;
  107. break;
  108. }
  109. case AttributeDataForm::Ref4: {
  110. u32 data;
  111. debug_info_stream >> data;
  112. value.type = AttributeValue::Type::DieReference;
  113. value.data.as_u32 = data + m_compilation_unit.offset();
  114. break;
  115. }
  116. case AttributeDataForm::FlagPresent: {
  117. value.type = AttributeValue::Type::Boolean;
  118. value.data.as_bool = true;
  119. break;
  120. }
  121. case AttributeDataForm::ExprLoc: {
  122. size_t length;
  123. debug_info_stream.read_LEB128_unsigned(length);
  124. value.type = AttributeValue::Type::DwarfExpression;
  125. assign_raw_bytes_value(length);
  126. break;
  127. }
  128. case AttributeDataForm::String: {
  129. String str;
  130. u32 str_offset = debug_info_stream.offset();
  131. debug_info_stream >> str;
  132. value.type = AttributeValue::Type::String;
  133. value.data.as_string = reinterpret_cast<const char*>(str_offset + m_compilation_unit.dwarf_info().debug_info_data().data());
  134. break;
  135. }
  136. case AttributeDataForm::Block1: {
  137. value.type = AttributeValue::Type::RawBytes;
  138. u8 length;
  139. debug_info_stream >> length;
  140. assign_raw_bytes_value(length);
  141. break;
  142. }
  143. case AttributeDataForm::Block2: {
  144. value.type = AttributeValue::Type::RawBytes;
  145. u16 length;
  146. debug_info_stream >> length;
  147. assign_raw_bytes_value(length);
  148. break;
  149. }
  150. case AttributeDataForm::Block4: {
  151. value.type = AttributeValue::Type::RawBytes;
  152. u32 length;
  153. debug_info_stream >> length;
  154. assign_raw_bytes_value(length);
  155. break;
  156. }
  157. case AttributeDataForm::Block: {
  158. value.type = AttributeValue::Type::RawBytes;
  159. size_t length;
  160. debug_info_stream.read_LEB128_unsigned(length);
  161. assign_raw_bytes_value(length);
  162. break;
  163. }
  164. default:
  165. dbg() << "Unimplemented AttributeDataForm: " << (u32)form;
  166. ASSERT_NOT_REACHED();
  167. }
  168. return value;
  169. }
  170. Optional<DIE::AttributeValue> DIE::get_attribute(const Attribute& attribute) const
  171. {
  172. InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data().span() };
  173. stream.discard_or_error(m_data_offset);
  174. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  175. ASSERT(abbreviation_info.has_value());
  176. for (const auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
  177. auto value = get_attribute_value(attribute_spec.form, stream);
  178. if (attribute_spec.attribute == attribute) {
  179. return value;
  180. }
  181. }
  182. return {};
  183. }
  184. void DIE::for_each_child(Function<void(const DIE& child)> callback) const
  185. {
  186. if (!m_has_children)
  187. return;
  188. NonnullOwnPtr<DIE> current_child = make<DIE>(m_compilation_unit, m_offset + m_size);
  189. while (true) {
  190. callback(*current_child);
  191. if (current_child->is_null())
  192. break;
  193. if (!current_child->has_children()) {
  194. current_child = make<DIE>(m_compilation_unit, current_child->offset() + current_child->size());
  195. continue;
  196. }
  197. auto sibling = current_child->get_attribute(Attribute::Sibling);
  198. u32 sibling_offset = 0;
  199. if (sibling.has_value()) {
  200. sibling_offset = sibling.value().data.as_u32;
  201. }
  202. if (!sibling.has_value()) {
  203. // NOTE: According to the spec, the compiler does't have to supply the sibling information.
  204. // When it doesn't, we have to recursively iterate the current child's children to find where they end
  205. current_child->for_each_child([&](const DIE& sub_child) {
  206. sibling_offset = sub_child.offset() + sub_child.size();
  207. });
  208. }
  209. current_child = make<DIE>(m_compilation_unit, sibling_offset);
  210. }
  211. }
  212. DIE DIE::get_die_at_offset(u32 offset) const
  213. {
  214. ASSERT(offset >= m_compilation_unit.offset() && offset < m_compilation_unit.offset() + m_compilation_unit.size());
  215. return DIE(m_compilation_unit, offset);
  216. }
  217. }