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