File indexing completed on 2024-05-12 05:43:27

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include "dwarfranges.h"
0019 #include "dwarfdie.h"
0020 #include "dwarfinfo.h"
0021 
0022 #include <cassert>
0023 
0024 DwarfRanges::DwarfRanges() : m_rangeSize(0)
0025 {
0026 }
0027 
0028 DwarfRanges::DwarfRanges(const DwarfDie* die, uint64_t offset)
0029 {
0030     Dwarf_Ranges* ranges = nullptr;
0031     const auto res = dwarf_get_ranges_a(die->dwarfInfo()->dwarfHandle(), offset,
0032                                         die->dieHandle(), &ranges, &m_rangeSize,
0033                                         nullptr, nullptr);
0034     if (res != DW_DLV_OK)
0035         return;
0036 
0037     m_ranges.reset(ranges, [die, this](Dwarf_Ranges* ranges) {
0038         dwarf_ranges_dealloc(die->dwarfInfo()->dwarfHandle(), ranges, m_rangeSize);
0039     });
0040 }
0041 
0042 DwarfRanges::DwarfRanges(const DwarfRanges&) = default;
0043 DwarfRanges::DwarfRanges(DwarfRanges&&) = default;
0044 DwarfRanges::~DwarfRanges() = default;
0045 
0046 DwarfRanges& DwarfRanges::operator=(const DwarfRanges&) = default;
0047 DwarfRanges& DwarfRanges::operator=(DwarfRanges&&) = default;
0048 
0049 bool DwarfRanges::isValid() const
0050 {
0051     return m_ranges.get() != nullptr;
0052 }
0053 
0054 int DwarfRanges::size() const
0055 {
0056     // ignore null terminator
0057     if (m_rangeSize > 0 && entry(m_rangeSize - 1)->dwr_type == DW_RANGES_END)
0058         return m_rangeSize - 1;
0059     return m_rangeSize;
0060 }
0061 
0062 Dwarf_Ranges* DwarfRanges::entry(int index) const
0063 {
0064     return &m_ranges.get()[index];
0065 }