CompilationUnit.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CompilationUnit.h"
  7. #include "DIE.h"
  8. #include <AK/ByteReader.h>
  9. namespace Debug::Dwarf {
  10. CompilationUnit::CompilationUnit(DwarfInfo const& dwarf_info, u32 offset, CompilationUnitHeader const& header, NonnullOwnPtr<LineProgram>&& line_program)
  11. : m_dwarf_info(dwarf_info)
  12. , m_offset(offset)
  13. , m_header(header)
  14. , m_abbreviations(dwarf_info, header.abbrev_offset())
  15. , m_line_program(move(line_program))
  16. {
  17. VERIFY(header.version() < 5 || header.unit_type() == CompilationUnitType::Full);
  18. }
  19. DIE CompilationUnit::root_die() const
  20. {
  21. return DIE(*this, m_offset + m_header.header_size());
  22. }
  23. DIE CompilationUnit::get_die_at_offset(u32 die_offset) const
  24. {
  25. VERIFY(die_offset >= offset() && die_offset < offset() + size());
  26. return DIE(*this, die_offset);
  27. }
  28. Optional<FlatPtr> CompilationUnit::base_address() const
  29. {
  30. if (m_has_cached_base_address)
  31. return m_cached_base_address;
  32. auto die = root_die();
  33. auto res = die.get_attribute(Attribute::LowPc);
  34. if (res.has_value()) {
  35. m_cached_base_address = res->as_addr();
  36. }
  37. m_has_cached_base_address = true;
  38. return m_cached_base_address;
  39. }
  40. u64 CompilationUnit::address_table_base() const
  41. {
  42. if (m_has_cached_address_table_base)
  43. return m_cached_address_table_base;
  44. auto die = root_die();
  45. auto res = die.get_attribute(Attribute::AddrBase);
  46. if (res.has_value()) {
  47. VERIFY(res->form() == AttributeDataForm::SecOffset);
  48. m_cached_address_table_base = res->as_unsigned();
  49. }
  50. m_has_cached_address_table_base = true;
  51. return m_cached_address_table_base;
  52. }
  53. u64 CompilationUnit::string_offsets_base() const
  54. {
  55. if (m_has_cached_string_offsets_base)
  56. return m_cached_string_offsets_base;
  57. auto die = root_die();
  58. auto res = die.get_attribute(Attribute::StrOffsetsBase);
  59. if (res.has_value()) {
  60. VERIFY(res->form() == AttributeDataForm::SecOffset);
  61. m_cached_string_offsets_base = res->as_unsigned();
  62. }
  63. m_has_cached_string_offsets_base = true;
  64. return m_cached_string_offsets_base;
  65. }
  66. u64 CompilationUnit::range_lists_base() const
  67. {
  68. if (m_has_cached_range_lists_base)
  69. return m_cached_range_lists_base;
  70. auto die = root_die();
  71. auto res = die.get_attribute(Attribute::RngListsBase);
  72. if (res.has_value()) {
  73. VERIFY(res->form() == AttributeDataForm::SecOffset);
  74. m_cached_range_lists_base = res->as_unsigned();
  75. }
  76. m_has_cached_range_lists_base = true;
  77. return m_cached_range_lists_base;
  78. }
  79. FlatPtr CompilationUnit::get_address(size_t index) const
  80. {
  81. auto base = address_table_base();
  82. auto debug_addr_data = dwarf_info().debug_addr_data();
  83. VERIFY(base < debug_addr_data.size());
  84. auto addresses = debug_addr_data.slice(base);
  85. VERIFY(index * sizeof(FlatPtr) < addresses.size());
  86. FlatPtr value { 0 };
  87. ByteReader::load<FlatPtr>(addresses.offset_pointer(index * sizeof(FlatPtr)), value);
  88. return value;
  89. }
  90. char const* CompilationUnit::get_string(size_t index) const
  91. {
  92. auto base = string_offsets_base();
  93. auto debug_str_offsets_data = dwarf_info().debug_str_offsets_data();
  94. VERIFY(base < debug_str_offsets_data.size());
  95. // FIXME: This assumes DWARF32
  96. auto offsets = debug_str_offsets_data.slice(base);
  97. VERIFY(index * sizeof(u32) < offsets.size());
  98. auto offset = ByteReader::load32(offsets.offset_pointer(index * sizeof(u32)));
  99. return bit_cast<char const*>(dwarf_info().debug_strings_data().offset(offset));
  100. }
  101. }