1 //===-- DWARFDebugArangeSet.cpp ---------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DWARFDebugArangeSet.h"
10 
11 #include "SymbolFileDWARF.h"
12 #include "lldb/Utility/Stream.h"
13 #include "llvm/Object/Error.h"
14 #include <assert.h>
15 
16 using namespace lldb_private;
17 
18 DWARFDebugArangeSet::DWARFDebugArangeSet()
19     : m_offset(DW_INVALID_OFFSET), m_header(), m_arange_descriptors() {
20   m_header.length = 0;
21   m_header.version = 0;
22   m_header.cu_offset = 0;
23   m_header.addr_size = 0;
24   m_header.seg_size = 0;
25 }
26 
27 void DWARFDebugArangeSet::Clear() {
28   m_offset = DW_INVALID_OFFSET;
29   m_header.length = 0;
30   m_header.version = 0;
31   m_header.cu_offset = 0;
32   m_header.addr_size = 0;
33   m_header.seg_size = 0;
34   m_arange_descriptors.clear();
35 }
36 
37 llvm::Error DWARFDebugArangeSet::extract(const DWARFDataExtractor &data,
38                                          lldb::offset_t *offset_ptr) {
39   assert(data.ValidOffset(*offset_ptr));
40 
41   m_arange_descriptors.clear();
42   m_offset = *offset_ptr;
43 
44   // 7.20 Address Range Table
45   //
46   // Each set of entries in the table of address ranges contained in the
47   // .debug_aranges section begins with a header consisting of: a 4-byte
48   // length containing the length of the set of entries for this compilation
49   // unit, not including the length field itself; a 2-byte version identifier
50   // containing the value 2 for DWARF Version 2; a 4-byte offset into
51   // the.debug_infosection; a 1-byte unsigned integer containing the size in
52   // bytes of an address (or the offset portion of an address for segmented
53   // addressing) on the target system; and a 1-byte unsigned integer
54   // containing the size in bytes of a segment descriptor on the target
55   // system. This header is followed by a series of tuples. Each tuple
56   // consists of an address and a length, each in the size appropriate for an
57   // address on the target architecture.
58   m_header.length = data.GetDWARFInitialLength(offset_ptr);
59   m_header.version = data.GetU16(offset_ptr);
60   m_header.cu_offset = data.GetDWARFOffset(offset_ptr);
61   m_header.addr_size = data.GetU8(offset_ptr);
62   m_header.seg_size = data.GetU8(offset_ptr);
63 
64   // Try to avoid reading invalid arange sets by making sure:
65   // 1 - the version looks good
66   // 2 - the address byte size looks plausible
67   // 3 - the length seems to make sense
68   // size looks plausible
69   if (m_header.version < 2 || m_header.version > 5)
70     return llvm::make_error<llvm::object::GenericBinaryError>(
71         "Invalid arange header version");
72 
73   if (m_header.addr_size != 4 && m_header.addr_size != 8)
74     return llvm::make_error<llvm::object::GenericBinaryError>(
75         "Invalid arange header address size");
76 
77   if (m_header.length == 0)
78     return llvm::make_error<llvm::object::GenericBinaryError>(
79         "Invalid arange header length");
80 
81   if (!data.ValidOffset(m_offset + sizeof(m_header.length) + m_header.length -
82                         1))
83     return llvm::make_error<llvm::object::GenericBinaryError>(
84         "Invalid arange header length");
85 
86   // The first tuple following the header in each set begins at an offset
87   // that is a multiple of the size of a single tuple (that is, twice the
88   // size of an address). The header is padded, if necessary, to the
89   // appropriate boundary.
90   const uint32_t header_size = *offset_ptr - m_offset;
91   const uint32_t tuple_size = m_header.addr_size << 1;
92   uint32_t first_tuple_offset = 0;
93   while (first_tuple_offset < header_size)
94     first_tuple_offset += tuple_size;
95 
96   *offset_ptr = m_offset + first_tuple_offset;
97 
98   Descriptor arangeDescriptor;
99 
100   static_assert(sizeof(arangeDescriptor.address) ==
101                     sizeof(arangeDescriptor.length),
102                 "DWARFDebugArangeSet::Descriptor.address and "
103                 "DWARFDebugArangeSet::Descriptor.length must have same size");
104 
105   while (data.ValidOffset(*offset_ptr)) {
106     arangeDescriptor.address = data.GetMaxU64(offset_ptr, m_header.addr_size);
107     arangeDescriptor.length = data.GetMaxU64(offset_ptr, m_header.addr_size);
108 
109     // Each set of tuples is terminated by a 0 for the address and 0 for
110     // the length.
111     if (!arangeDescriptor.address && !arangeDescriptor.length)
112       return llvm::ErrorSuccess();
113 
114     m_arange_descriptors.push_back(arangeDescriptor);
115   }
116 
117   return llvm::make_error<llvm::object::GenericBinaryError>(
118       "arange descriptors not terminated by null entry");
119 }
120 
121 class DescriptorContainsAddress {
122 public:
123   DescriptorContainsAddress(dw_addr_t address) : m_address(address) {}
124   bool operator()(const DWARFDebugArangeSet::Descriptor &desc) const {
125     return (m_address >= desc.address) &&
126            (m_address < (desc.address + desc.length));
127   }
128 
129 private:
130   const dw_addr_t m_address;
131 };
132 
133 dw_offset_t DWARFDebugArangeSet::FindAddress(dw_addr_t address) const {
134   DescriptorConstIter end = m_arange_descriptors.end();
135   DescriptorConstIter pos =
136       std::find_if(m_arange_descriptors.begin(), end,   // Range
137                    DescriptorContainsAddress(address)); // Predicate
138   if (pos != end)
139     return m_header.cu_offset;
140 
141   return DW_INVALID_OFFSET;
142 }
143