1 //===-- DWARFDebugArangeSet.cpp ---------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "DWARFDebugArangeSet.h"
10 #include "DWARFDataExtractor.h"
11 #include "llvm/Object/Error.h"
14 using namespace lldb_private;
16 DWARFDebugArangeSet::DWARFDebugArangeSet()
17 : m_offset(DW_INVALID_OFFSET), m_header(), m_arange_descriptors() {
20 m_header.cu_offset = 0;
21 m_header.addr_size = 0;
22 m_header.seg_size = 0;
25 void DWARFDebugArangeSet::Clear() {
26 m_offset = DW_INVALID_OFFSET;
29 m_header.cu_offset = 0;
30 m_header.addr_size = 0;
31 m_header.seg_size = 0;
32 m_arange_descriptors.clear();
35 llvm::Error DWARFDebugArangeSet::extract(const DWARFDataExtractor &data,
36 lldb::offset_t *offset_ptr) {
37 assert(data.ValidOffset(*offset_ptr));
39 m_arange_descriptors.clear();
40 m_offset = *offset_ptr;
42 // 7.20 Address Range Table
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);
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 // size looks plausible
67 if (m_header.version < 2 || m_header.version > 5)
68 return llvm::make_error<llvm::object::GenericBinaryError>(
69 "Invalid arange header version");
71 if (m_header.addr_size != 4 && m_header.addr_size != 8)
72 return llvm::make_error<llvm::object::GenericBinaryError>(
73 "Invalid arange header address size");
75 if (m_header.length == 0)
76 return llvm::make_error<llvm::object::GenericBinaryError>(
77 "Invalid arange header length");
79 if (!data.ValidOffset(m_offset + sizeof(m_header.length) + m_header.length -
81 return llvm::make_error<llvm::object::GenericBinaryError>(
82 "Invalid arange header length");
84 // The first tuple following the header in each set begins at an offset
85 // that is a multiple of the size of a single tuple (that is, twice the
86 // size of an address). The header is padded, if necessary, to the
87 // appropriate boundary.
88 const uint32_t header_size = *offset_ptr - m_offset;
89 const uint32_t tuple_size = m_header.addr_size << 1;
90 uint32_t first_tuple_offset = 0;
91 while (first_tuple_offset < header_size)
92 first_tuple_offset += tuple_size;
94 *offset_ptr = m_offset + first_tuple_offset;
96 Descriptor arangeDescriptor;
98 static_assert(sizeof(arangeDescriptor.address) ==
99 sizeof(arangeDescriptor.length),
100 "DWARFDebugArangeSet::Descriptor.address and "
101 "DWARFDebugArangeSet::Descriptor.length must have same size");
103 while (data.ValidOffset(*offset_ptr)) {
104 arangeDescriptor.address = data.GetMaxU64(offset_ptr, m_header.addr_size);
105 arangeDescriptor.length = data.GetMaxU64(offset_ptr, m_header.addr_size);
107 // Each set of tuples is terminated by a 0 for the address and 0 for
109 if (!arangeDescriptor.address && !arangeDescriptor.length)
110 return llvm::ErrorSuccess();
112 m_arange_descriptors.push_back(arangeDescriptor);
115 return llvm::make_error<llvm::object::GenericBinaryError>(
116 "arange descriptors not terminated by null entry");
119 class DescriptorContainsAddress {
121 DescriptorContainsAddress(dw_addr_t address) : m_address(address) {}
122 bool operator()(const DWARFDebugArangeSet::Descriptor &desc) const {
123 return (m_address >= desc.address) &&
124 (m_address < (desc.address + desc.length));
128 const dw_addr_t m_address;
131 dw_offset_t DWARFDebugArangeSet::FindAddress(dw_addr_t address) const {
132 DescriptorConstIter end = m_arange_descriptors.end();
133 DescriptorConstIter pos =
134 std::find_if(m_arange_descriptors.begin(), end, // Range
135 DescriptorContainsAddress(address)); // Predicate
137 return m_header.cu_offset;
139 return DW_INVALID_OFFSET;