xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugArangeSet.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
15ffd83dbSDimitry Andric //===-- DWARFDebugArangeSet.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "DWARFDebugArangeSet.h"
100b57cec5SDimitry Andric #include "DWARFDataExtractor.h"
11fe6060f1SDimitry Andric #include "LogChannelDWARF.h"
120b57cec5SDimitry Andric #include "llvm/Object/Error.h"
130b57cec5SDimitry Andric #include <cassert>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric using namespace lldb_private;
16*5f757f3fSDimitry Andric using namespace lldb_private::plugin::dwarf;
170b57cec5SDimitry Andric 
DWARFDebugArangeSet()180b57cec5SDimitry Andric DWARFDebugArangeSet::DWARFDebugArangeSet()
19fe6060f1SDimitry Andric     : m_offset(DW_INVALID_OFFSET), m_next_offset(DW_INVALID_OFFSET) {}
200b57cec5SDimitry Andric 
Clear()210b57cec5SDimitry Andric void DWARFDebugArangeSet::Clear() {
220b57cec5SDimitry Andric   m_offset = DW_INVALID_OFFSET;
23fe6060f1SDimitry Andric   m_next_offset = DW_INVALID_OFFSET;
240b57cec5SDimitry Andric   m_header.length = 0;
250b57cec5SDimitry Andric   m_header.version = 0;
260b57cec5SDimitry Andric   m_header.cu_offset = 0;
270b57cec5SDimitry Andric   m_header.addr_size = 0;
280b57cec5SDimitry Andric   m_header.seg_size = 0;
290b57cec5SDimitry Andric   m_arange_descriptors.clear();
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric 
extract(const DWARFDataExtractor & data,lldb::offset_t * offset_ptr)320b57cec5SDimitry Andric llvm::Error DWARFDebugArangeSet::extract(const DWARFDataExtractor &data,
330b57cec5SDimitry Andric                                          lldb::offset_t *offset_ptr) {
340b57cec5SDimitry Andric   assert(data.ValidOffset(*offset_ptr));
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   m_arange_descriptors.clear();
370b57cec5SDimitry Andric   m_offset = *offset_ptr;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   // 7.20 Address Range Table
400b57cec5SDimitry Andric   //
410b57cec5SDimitry Andric   // Each set of entries in the table of address ranges contained in the
420b57cec5SDimitry Andric   // .debug_aranges section begins with a header consisting of: a 4-byte
430b57cec5SDimitry Andric   // length containing the length of the set of entries for this compilation
440b57cec5SDimitry Andric   // unit, not including the length field itself; a 2-byte version identifier
450b57cec5SDimitry Andric   // containing the value 2 for DWARF Version 2; a 4-byte offset into
460b57cec5SDimitry Andric   // the.debug_infosection; a 1-byte unsigned integer containing the size in
470b57cec5SDimitry Andric   // bytes of an address (or the offset portion of an address for segmented
480b57cec5SDimitry Andric   // addressing) on the target system; and a 1-byte unsigned integer
490b57cec5SDimitry Andric   // containing the size in bytes of a segment descriptor on the target
500b57cec5SDimitry Andric   // system. This header is followed by a series of tuples. Each tuple
510b57cec5SDimitry Andric   // consists of an address and a length, each in the size appropriate for an
520b57cec5SDimitry Andric   // address on the target architecture.
530b57cec5SDimitry Andric   m_header.length = data.GetDWARFInitialLength(offset_ptr);
54fe6060f1SDimitry Andric   // The length could be 4 bytes or 12 bytes, so use the current offset to
55fe6060f1SDimitry Andric   // determine the next offset correctly.
56fe6060f1SDimitry Andric   if (m_header.length > 0)
57fe6060f1SDimitry Andric     m_next_offset = *offset_ptr + m_header.length;
58fe6060f1SDimitry Andric   else
59fe6060f1SDimitry Andric     m_next_offset = DW_INVALID_OFFSET;
600b57cec5SDimitry Andric   m_header.version = data.GetU16(offset_ptr);
610b57cec5SDimitry Andric   m_header.cu_offset = data.GetDWARFOffset(offset_ptr);
620b57cec5SDimitry Andric   m_header.addr_size = data.GetU8(offset_ptr);
630b57cec5SDimitry Andric   m_header.seg_size = data.GetU8(offset_ptr);
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   // Try to avoid reading invalid arange sets by making sure:
660b57cec5SDimitry Andric   // 1 - the version looks good
670b57cec5SDimitry Andric   // 2 - the address byte size looks plausible
680b57cec5SDimitry Andric   // 3 - the length seems to make sense
695ffd83dbSDimitry Andric   // 4 - size looks plausible
705ffd83dbSDimitry Andric   // 5 - the arange tuples do not contain a segment field
710b57cec5SDimitry Andric   if (m_header.version < 2 || m_header.version > 5)
720b57cec5SDimitry Andric     return llvm::make_error<llvm::object::GenericBinaryError>(
730b57cec5SDimitry Andric         "Invalid arange header version");
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   if (m_header.addr_size != 4 && m_header.addr_size != 8)
760b57cec5SDimitry Andric     return llvm::make_error<llvm::object::GenericBinaryError>(
770b57cec5SDimitry Andric         "Invalid arange header address size");
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   if (m_header.length == 0)
800b57cec5SDimitry Andric     return llvm::make_error<llvm::object::GenericBinaryError>(
810b57cec5SDimitry Andric         "Invalid arange header length");
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   if (!data.ValidOffset(m_offset + sizeof(m_header.length) + m_header.length -
840b57cec5SDimitry Andric                         1))
850b57cec5SDimitry Andric     return llvm::make_error<llvm::object::GenericBinaryError>(
860b57cec5SDimitry Andric         "Invalid arange header length");
870b57cec5SDimitry Andric 
885ffd83dbSDimitry Andric   if (m_header.seg_size)
895ffd83dbSDimitry Andric     return llvm::make_error<llvm::object::GenericBinaryError>(
905ffd83dbSDimitry Andric         "segmented arange entries are not supported");
915ffd83dbSDimitry Andric 
920b57cec5SDimitry Andric   // The first tuple following the header in each set begins at an offset
930b57cec5SDimitry Andric   // that is a multiple of the size of a single tuple (that is, twice the
940b57cec5SDimitry Andric   // size of an address). The header is padded, if necessary, to the
950b57cec5SDimitry Andric   // appropriate boundary.
960b57cec5SDimitry Andric   const uint32_t header_size = *offset_ptr - m_offset;
970b57cec5SDimitry Andric   const uint32_t tuple_size = m_header.addr_size << 1;
980b57cec5SDimitry Andric   uint32_t first_tuple_offset = 0;
990b57cec5SDimitry Andric   while (first_tuple_offset < header_size)
1000b57cec5SDimitry Andric     first_tuple_offset += tuple_size;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   *offset_ptr = m_offset + first_tuple_offset;
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   Descriptor arangeDescriptor;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   static_assert(sizeof(arangeDescriptor.address) ==
1070b57cec5SDimitry Andric                     sizeof(arangeDescriptor.length),
1080b57cec5SDimitry Andric                 "DWARFDebugArangeSet::Descriptor.address and "
1090b57cec5SDimitry Andric                 "DWARFDebugArangeSet::Descriptor.length must have same size");
1100b57cec5SDimitry Andric 
111fe6060f1SDimitry Andric   const lldb::offset_t next_offset = GetNextOffset();
112fe6060f1SDimitry Andric   assert(next_offset != DW_INVALID_OFFSET);
113fe6060f1SDimitry Andric   uint32_t num_terminators = 0;
114fe6060f1SDimitry Andric   bool last_was_terminator = false;
115fe6060f1SDimitry Andric   while (*offset_ptr < next_offset) {
1160b57cec5SDimitry Andric     arangeDescriptor.address = data.GetMaxU64(offset_ptr, m_header.addr_size);
1170b57cec5SDimitry Andric     arangeDescriptor.length = data.GetMaxU64(offset_ptr, m_header.addr_size);
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric     // Each set of tuples is terminated by a 0 for the address and 0 for
120fe6060f1SDimitry Andric     // the length. Some linkers can emit .debug_aranges with multiple
121fe6060f1SDimitry Andric     // terminator pair entries that are still withing the length of the
122fe6060f1SDimitry Andric     // DWARFDebugArangeSet. We want to be sure to parse all entries for
123fe6060f1SDimitry Andric     // this DWARFDebugArangeSet so that we don't stop parsing early and end up
124fe6060f1SDimitry Andric     // treating addresses as a header of the next DWARFDebugArangeSet. We also
125fe6060f1SDimitry Andric     // need to make sure we parse all valid address pairs so we don't omit them
126fe6060f1SDimitry Andric     // from the aranges result, so we can't stop at the first terminator entry
127fe6060f1SDimitry Andric     // we find.
128fe6060f1SDimitry Andric     if (arangeDescriptor.address == 0 && arangeDescriptor.length == 0) {
129fe6060f1SDimitry Andric       ++num_terminators;
130fe6060f1SDimitry Andric       last_was_terminator = true;
131fe6060f1SDimitry Andric     } else {
132fe6060f1SDimitry Andric       last_was_terminator = false;
133fe6060f1SDimitry Andric       // Only add .debug_aranges address entries that have a non zero size.
134fe6060f1SDimitry Andric       // Some linkers will zero out the length field for some .debug_aranges
135fe6060f1SDimitry Andric       // entries if they were stripped. We also could watch out for multiple
136fe6060f1SDimitry Andric       // entries at address zero and remove those as well.
137fe6060f1SDimitry Andric       if (arangeDescriptor.length > 0)
1380b57cec5SDimitry Andric         m_arange_descriptors.push_back(arangeDescriptor);
1390b57cec5SDimitry Andric     }
140fe6060f1SDimitry Andric   }
141fe6060f1SDimitry Andric   if (num_terminators > 1) {
1421fd87a68SDimitry Andric     Log *log = GetLog(DWARFLog::DebugInfo);
143fe6060f1SDimitry Andric     LLDB_LOG(log,
144fe6060f1SDimitry Andric              "warning: DWARFDebugArangeSet at %#" PRIx64 " contains %u "
145fe6060f1SDimitry Andric              "terminator entries",
146fe6060f1SDimitry Andric              m_offset, num_terminators);
147fe6060f1SDimitry Andric   }
148fe6060f1SDimitry Andric   if (last_was_terminator)
149fe6060f1SDimitry Andric     return llvm::ErrorSuccess();
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   return llvm::make_error<llvm::object::GenericBinaryError>(
1520b57cec5SDimitry Andric       "arange descriptors not terminated by null entry");
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric class DescriptorContainsAddress {
1560b57cec5SDimitry Andric public:
DescriptorContainsAddress(dw_addr_t address)1570b57cec5SDimitry Andric   DescriptorContainsAddress(dw_addr_t address) : m_address(address) {}
operator ()(const DWARFDebugArangeSet::Descriptor & desc) const1580b57cec5SDimitry Andric   bool operator()(const DWARFDebugArangeSet::Descriptor &desc) const {
1590b57cec5SDimitry Andric     return (m_address >= desc.address) &&
1600b57cec5SDimitry Andric            (m_address < (desc.address + desc.length));
1610b57cec5SDimitry Andric   }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric private:
1640b57cec5SDimitry Andric   const dw_addr_t m_address;
1650b57cec5SDimitry Andric };
1660b57cec5SDimitry Andric 
FindAddress(dw_addr_t address) const1670b57cec5SDimitry Andric dw_offset_t DWARFDebugArangeSet::FindAddress(dw_addr_t address) const {
1680b57cec5SDimitry Andric   DescriptorConstIter end = m_arange_descriptors.end();
1690b57cec5SDimitry Andric   DescriptorConstIter pos =
1700b57cec5SDimitry Andric       std::find_if(m_arange_descriptors.begin(), end,   // Range
1710b57cec5SDimitry Andric                    DescriptorContainsAddress(address)); // Predicate
1720b57cec5SDimitry Andric   if (pos != end)
1730b57cec5SDimitry Andric     return m_header.cu_offset;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   return DW_INVALID_OFFSET;
1760b57cec5SDimitry Andric }
177