180814287SRaphael Isemann //===-- DWARFDebugArangeSet.cpp -------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
930fdc8d8SChris Lattner #include "DWARFDebugArangeSet.h"
1011e05491SPavel Labath #include "DWARFDataExtractor.h"
11eee30906SGreg Clayton #include "LogChannelDWARF.h"
121cbbab92SZachary Turner #include "llvm/Object/Error.h"
1311e05491SPavel Labath #include <cassert>
1430fdc8d8SChris Lattner 
1530fdc8d8SChris Lattner using namespace lldb_private;
1630fdc8d8SChris Lattner 
DWARFDebugArangeSet()17b9c1b51eSKate Stone DWARFDebugArangeSet::DWARFDebugArangeSet()
18eee30906SGreg Clayton     : m_offset(DW_INVALID_OFFSET), m_next_offset(DW_INVALID_OFFSET) {}
1930fdc8d8SChris Lattner 
Clear()20b9c1b51eSKate Stone void DWARFDebugArangeSet::Clear() {
2130fdc8d8SChris Lattner   m_offset = DW_INVALID_OFFSET;
22eee30906SGreg Clayton   m_next_offset = DW_INVALID_OFFSET;
2330fdc8d8SChris Lattner   m_header.length = 0;
2430fdc8d8SChris Lattner   m_header.version = 0;
2530fdc8d8SChris Lattner   m_header.cu_offset = 0;
2630fdc8d8SChris Lattner   m_header.addr_size = 0;
2730fdc8d8SChris Lattner   m_header.seg_size = 0;
2830fdc8d8SChris Lattner   m_arange_descriptors.clear();
2930fdc8d8SChris Lattner }
3030fdc8d8SChris Lattner 
extract(const DWARFDataExtractor & data,lldb::offset_t * offset_ptr)311cbbab92SZachary Turner llvm::Error DWARFDebugArangeSet::extract(const DWARFDataExtractor &data,
32b9c1b51eSKate Stone                                          lldb::offset_t *offset_ptr) {
331cbbab92SZachary Turner   assert(data.ValidOffset(*offset_ptr));
341cbbab92SZachary Turner 
3530fdc8d8SChris Lattner   m_arange_descriptors.clear();
3630fdc8d8SChris Lattner   m_offset = *offset_ptr;
3730fdc8d8SChris Lattner 
3830fdc8d8SChris Lattner   // 7.20 Address Range Table
3930fdc8d8SChris Lattner   //
4005097246SAdrian Prantl   // Each set of entries in the table of address ranges contained in the
4105097246SAdrian Prantl   // .debug_aranges section begins with a header consisting of: a 4-byte
4205097246SAdrian Prantl   // length containing the length of the set of entries for this compilation
4305097246SAdrian Prantl   // unit, not including the length field itself; a 2-byte version identifier
4405097246SAdrian Prantl   // containing the value 2 for DWARF Version 2; a 4-byte offset into
4505097246SAdrian Prantl   // the.debug_infosection; a 1-byte unsigned integer containing the size in
4605097246SAdrian Prantl   // bytes of an address (or the offset portion of an address for segmented
4705097246SAdrian Prantl   // addressing) on the target system; and a 1-byte unsigned integer
4805097246SAdrian Prantl   // containing the size in bytes of a segment descriptor on the target
4905097246SAdrian Prantl   // system. This header is followed by a series of tuples. Each tuple
5005097246SAdrian Prantl   // consists of an address and a length, each in the size appropriate for an
5105097246SAdrian Prantl   // address on the target architecture.
52eeae7218SEd Maste   m_header.length = data.GetDWARFInitialLength(offset_ptr);
53eee30906SGreg Clayton   // The length could be 4 bytes or 12 bytes, so use the current offset to
54eee30906SGreg Clayton   // determine the next offset correctly.
55eee30906SGreg Clayton   if (m_header.length > 0)
56eee30906SGreg Clayton     m_next_offset = *offset_ptr + m_header.length;
57eee30906SGreg Clayton   else
58eee30906SGreg Clayton     m_next_offset = DW_INVALID_OFFSET;
5930fdc8d8SChris Lattner   m_header.version = data.GetU16(offset_ptr);
60eeae7218SEd Maste   m_header.cu_offset = data.GetDWARFOffset(offset_ptr);
6130fdc8d8SChris Lattner   m_header.addr_size = data.GetU8(offset_ptr);
6230fdc8d8SChris Lattner   m_header.seg_size = data.GetU8(offset_ptr);
6330fdc8d8SChris Lattner 
647ec287cdSGreg Clayton   // Try to avoid reading invalid arange sets by making sure:
657ec287cdSGreg Clayton   // 1 - the version looks good
667ec287cdSGreg Clayton   // 2 - the address byte size looks plausible
677ec287cdSGreg Clayton   // 3 - the length seems to make sense
680fa33209SLuke Drummond   // 4 - size looks plausible
690fa33209SLuke Drummond   // 5 - the arange tuples do not contain a segment field
701cbbab92SZachary Turner   if (m_header.version < 2 || m_header.version > 5)
711cbbab92SZachary Turner     return llvm::make_error<llvm::object::GenericBinaryError>(
721cbbab92SZachary Turner         "Invalid arange header version");
731cbbab92SZachary Turner 
741cbbab92SZachary Turner   if (m_header.addr_size != 4 && m_header.addr_size != 8)
751cbbab92SZachary Turner     return llvm::make_error<llvm::object::GenericBinaryError>(
761cbbab92SZachary Turner         "Invalid arange header address size");
771cbbab92SZachary Turner 
781cbbab92SZachary Turner   if (m_header.length == 0)
791cbbab92SZachary Turner     return llvm::make_error<llvm::object::GenericBinaryError>(
801cbbab92SZachary Turner         "Invalid arange header length");
811cbbab92SZachary Turner 
821cbbab92SZachary Turner   if (!data.ValidOffset(m_offset + sizeof(m_header.length) + m_header.length -
831cbbab92SZachary Turner                         1))
841cbbab92SZachary Turner     return llvm::make_error<llvm::object::GenericBinaryError>(
851cbbab92SZachary Turner         "Invalid arange header length");
861cbbab92SZachary Turner 
870fa33209SLuke Drummond   if (m_header.seg_size)
880fa33209SLuke Drummond     return llvm::make_error<llvm::object::GenericBinaryError>(
890fa33209SLuke Drummond         "segmented arange entries are not supported");
900fa33209SLuke Drummond 
9130fdc8d8SChris Lattner   // The first tuple following the header in each set begins at an offset
9230fdc8d8SChris Lattner   // that is a multiple of the size of a single tuple (that is, twice the
9330fdc8d8SChris Lattner   // size of an address). The header is padded, if necessary, to the
9430fdc8d8SChris Lattner   // appropriate boundary.
9530fdc8d8SChris Lattner   const uint32_t header_size = *offset_ptr - m_offset;
9630fdc8d8SChris Lattner   const uint32_t tuple_size = m_header.addr_size << 1;
9730fdc8d8SChris Lattner   uint32_t first_tuple_offset = 0;
9830fdc8d8SChris Lattner   while (first_tuple_offset < header_size)
9930fdc8d8SChris Lattner     first_tuple_offset += tuple_size;
10030fdc8d8SChris Lattner 
10130fdc8d8SChris Lattner   *offset_ptr = m_offset + first_tuple_offset;
10230fdc8d8SChris Lattner 
10330fdc8d8SChris Lattner   Descriptor arangeDescriptor;
10430fdc8d8SChris Lattner 
1051cbbab92SZachary Turner   static_assert(sizeof(arangeDescriptor.address) ==
1061cbbab92SZachary Turner                     sizeof(arangeDescriptor.length),
107b9c1b51eSKate Stone                 "DWARFDebugArangeSet::Descriptor.address and "
108b9c1b51eSKate Stone                 "DWARFDebugArangeSet::Descriptor.length must have same size");
10930fdc8d8SChris Lattner 
110eee30906SGreg Clayton   const lldb::offset_t next_offset = GetNextOffset();
111eee30906SGreg Clayton   assert(next_offset != DW_INVALID_OFFSET);
112eee30906SGreg Clayton   uint32_t num_terminators = 0;
113eee30906SGreg Clayton   bool last_was_terminator = false;
114eee30906SGreg Clayton   while (*offset_ptr < next_offset) {
1151cbbab92SZachary Turner     arangeDescriptor.address = data.GetMaxU64(offset_ptr, m_header.addr_size);
1161cbbab92SZachary Turner     arangeDescriptor.length = data.GetMaxU64(offset_ptr, m_header.addr_size);
11730fdc8d8SChris Lattner 
11805097246SAdrian Prantl     // Each set of tuples is terminated by a 0 for the address and 0 for
119eee30906SGreg Clayton     // the length. Some linkers can emit .debug_aranges with multiple
120eee30906SGreg Clayton     // terminator pair entries that are still withing the length of the
121eee30906SGreg Clayton     // DWARFDebugArangeSet. We want to be sure to parse all entries for
122eee30906SGreg Clayton     // this DWARFDebugArangeSet so that we don't stop parsing early and end up
123eee30906SGreg Clayton     // treating addresses as a header of the next DWARFDebugArangeSet. We also
124eee30906SGreg Clayton     // need to make sure we parse all valid address pairs so we don't omit them
125eee30906SGreg Clayton     // from the aranges result, so we can't stop at the first terminator entry
126eee30906SGreg Clayton     // we find.
127eee30906SGreg Clayton     if (arangeDescriptor.address == 0 && arangeDescriptor.length == 0) {
128eee30906SGreg Clayton       ++num_terminators;
129eee30906SGreg Clayton       last_was_terminator = true;
130eee30906SGreg Clayton     } else {
131eee30906SGreg Clayton       last_was_terminator = false;
132eee30906SGreg Clayton       // Only add .debug_aranges address entries that have a non zero size.
133eee30906SGreg Clayton       // Some linkers will zero out the length field for some .debug_aranges
134eee30906SGreg Clayton       // entries if they were stripped. We also could watch out for multiple
135eee30906SGreg Clayton       // entries at address zero and remove those as well.
136eee30906SGreg Clayton       if (arangeDescriptor.length > 0)
1371cbbab92SZachary Turner         m_arange_descriptors.push_back(arangeDescriptor);
13830fdc8d8SChris Lattner     }
139eee30906SGreg Clayton   }
140eee30906SGreg Clayton   if (num_terminators > 1) {
141*2d75f627SPavel Labath     Log *log = GetLog(DWARFLog::DebugInfo);
142eee30906SGreg Clayton     LLDB_LOG(log,
143eee30906SGreg Clayton              "warning: DWARFDebugArangeSet at %#" PRIx64 " contains %u "
144eee30906SGreg Clayton              "terminator entries",
145eee30906SGreg Clayton              m_offset, num_terminators);
146eee30906SGreg Clayton   }
147eee30906SGreg Clayton   if (last_was_terminator)
148eee30906SGreg Clayton     return llvm::ErrorSuccess();
1491cbbab92SZachary Turner 
1501cbbab92SZachary Turner   return llvm::make_error<llvm::object::GenericBinaryError>(
1511cbbab92SZachary Turner       "arange descriptors not terminated by null entry");
15230fdc8d8SChris Lattner }
15330fdc8d8SChris Lattner 
154b9c1b51eSKate Stone class DescriptorContainsAddress {
15530fdc8d8SChris Lattner public:
DescriptorContainsAddress(dw_addr_t address)15630fdc8d8SChris Lattner   DescriptorContainsAddress(dw_addr_t address) : m_address(address) {}
operator ()(const DWARFDebugArangeSet::Descriptor & desc) const157b9c1b51eSKate Stone   bool operator()(const DWARFDebugArangeSet::Descriptor &desc) const {
158b9c1b51eSKate Stone     return (m_address >= desc.address) &&
159b9c1b51eSKate Stone            (m_address < (desc.address + desc.length));
16030fdc8d8SChris Lattner   }
161b9c1b51eSKate Stone 
16230fdc8d8SChris Lattner private:
16330fdc8d8SChris Lattner   const dw_addr_t m_address;
16430fdc8d8SChris Lattner };
16530fdc8d8SChris Lattner 
FindAddress(dw_addr_t address) const166b9c1b51eSKate Stone dw_offset_t DWARFDebugArangeSet::FindAddress(dw_addr_t address) const {
16730fdc8d8SChris Lattner   DescriptorConstIter end = m_arange_descriptors.end();
168b9c1b51eSKate Stone   DescriptorConstIter pos =
169b9c1b51eSKate Stone       std::find_if(m_arange_descriptors.begin(), end,   // Range
17030fdc8d8SChris Lattner                    DescriptorContainsAddress(address)); // Predicate
17130fdc8d8SChris Lattner   if (pos != end)
17230fdc8d8SChris Lattner     return m_header.cu_offset;
17330fdc8d8SChris Lattner 
17430fdc8d8SChris Lattner   return DW_INVALID_OFFSET;
17530fdc8d8SChris Lattner }
176