DIE.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/BufferStream.h>
  30. #include <AK/ByteBuffer.h>
  31. namespace Dwarf {
  32. DIE::DIE(const CompilationUnit& unit, u32 offset)
  33. : m_compilation_unit(unit)
  34. , m_offset(offset)
  35. {
  36. BufferStream stream(const_cast<ByteBuffer&>(m_compilation_unit.dwarf_info().debug_info_data()));
  37. stream.advance(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. BufferStream& debug_info_stream) const
  57. {
  58. AttributeValue value;
  59. switch (form) {
  60. case AttributeDataForm::StringPointer: {
  61. u32 offset = 0;
  62. debug_info_stream >> offset;
  63. value.type = AttributeValue::Type::String;
  64. auto strings_data = m_compilation_unit.dwarf_info().debug_strings_data();
  65. value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  66. break;
  67. }
  68. case AttributeDataForm::Data1: {
  69. u8 data = 0;
  70. debug_info_stream >> data;
  71. value.type = AttributeValue::Type::UnsignedNumber;
  72. value.data.as_u32 = data;
  73. break;
  74. }
  75. case AttributeDataForm::Data2: {
  76. u16 data = 0;
  77. debug_info_stream >> data;
  78. value.type = AttributeValue::Type::UnsignedNumber;
  79. value.data.as_u32 = data;
  80. break;
  81. }
  82. case AttributeDataForm::Addr: {
  83. u32 address = 0;
  84. debug_info_stream >> address;
  85. value.type = AttributeValue::Type::UnsignedNumber;
  86. value.data.as_u32 = address;
  87. break;
  88. }
  89. case AttributeDataForm::SecOffset: {
  90. u32 data = 0;
  91. debug_info_stream >> data;
  92. value.type = AttributeValue::Type::SecOffset;
  93. value.data.as_u32 = data;
  94. break;
  95. }
  96. case AttributeDataForm::Data4: {
  97. u32 data = 0;
  98. debug_info_stream >> data;
  99. value.type = AttributeValue::Type::UnsignedNumber;
  100. value.data.as_u32 = data;
  101. break;
  102. }
  103. case AttributeDataForm::Ref4: {
  104. u32 data = 0;
  105. debug_info_stream >> data;
  106. value.type = AttributeValue::Type::DieReference;
  107. value.data.as_u32 = data + m_compilation_unit.offset();
  108. break;
  109. }
  110. case AttributeDataForm::FlagPresent: {
  111. value.type = AttributeValue::Type::Boolean;
  112. value.data.as_bool = true;
  113. break;
  114. }
  115. case AttributeDataForm::ExprLoc: {
  116. size_t length = 0;
  117. debug_info_stream.read_LEB128_unsigned(length);
  118. value.type = AttributeValue::Type::DwarfExpression;
  119. value.data.as_dwarf_expression.length = length;
  120. value.data.as_dwarf_expression.bytes = reinterpret_cast<const u8*>(m_compilation_unit.dwarf_info().debug_info_data().data() + debug_info_stream.offset());
  121. debug_info_stream.advance(length);
  122. break;
  123. }
  124. case AttributeDataForm::String: {
  125. String str;
  126. u32 str_offset = debug_info_stream.offset();
  127. debug_info_stream >> str;
  128. value.type = AttributeValue::Type::String;
  129. value.data.as_string = reinterpret_cast<const char*>(str_offset + m_compilation_unit.dwarf_info().debug_info_data().data());
  130. break;
  131. }
  132. default:
  133. dbg() << "Unimplemented AttributeDataForm: " << (u32)form;
  134. ASSERT_NOT_REACHED();
  135. }
  136. return value;
  137. }
  138. Optional<DIE::AttributeValue> DIE::get_attribute(const Attribute& attribute) const
  139. {
  140. BufferStream stream(const_cast<ByteBuffer&>(m_compilation_unit.dwarf_info().debug_info_data()));
  141. stream.advance(m_data_offset);
  142. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  143. ASSERT(abbreviation_info.has_value());
  144. for (const auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
  145. auto value = get_attribute_value(attribute_spec.form, stream);
  146. if (attribute_spec.attribute == attribute) {
  147. return value;
  148. }
  149. }
  150. return {};
  151. }
  152. void DIE::for_each_child(Function<void(const DIE& child)> callback) const
  153. {
  154. if (!m_has_children)
  155. return;
  156. NonnullOwnPtr<DIE> current_child = make<DIE>(m_compilation_unit, m_offset + m_size);
  157. while (true) {
  158. callback(*current_child);
  159. if (current_child->is_null())
  160. break;
  161. if (!current_child->has_children()) {
  162. current_child = make<DIE>(m_compilation_unit, current_child->offset() + current_child->size());
  163. continue;
  164. }
  165. auto sibling = current_child->get_attribute(Attribute::Sibling);
  166. u32 sibling_offset = 0;
  167. if (sibling.has_value()) {
  168. sibling_offset = sibling.value().data.as_u32;
  169. }
  170. if (!sibling.has_value()) {
  171. // NOTE: According to the spec, the compiler does't have to supply the sibling information.
  172. // When it doesn't, we have to recursively iterate the current child's children to find where they end
  173. current_child->for_each_child([&](const DIE& sub_child) {
  174. sibling_offset = sub_child.offset() + sub_child.size();
  175. });
  176. }
  177. current_child = make<DIE>(m_compilation_unit, sibling_offset);
  178. }
  179. }
  180. DIE DIE::get_die_at_offset(u32 offset) const
  181. {
  182. ASSERT(offset >= m_compilation_unit.offset() && offset < m_compilation_unit.offset() + m_compilation_unit.size());
  183. return DIE(m_compilation_unit, offset);
  184. }
  185. }