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#include "DWARFDataExtractor.h" 11#include "llvm/Object/Error.h" 12#include <cassert> 13 14using namespace lldb_private; 15 16DWARFDebugArangeSet::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 25void 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 35llvm::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 // 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"); 70 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"); 74 75 if (m_header.length == 0) 76 return llvm::make_error<llvm::object::GenericBinaryError>( 77 "Invalid arange header length"); 78 79 if (!data.ValidOffset(m_offset + sizeof(m_header.length) + m_header.length - 80 1)) 81 return llvm::make_error<llvm::object::GenericBinaryError>( 82 "Invalid arange header length"); 83 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; 93 94 *offset_ptr = m_offset + first_tuple_offset; 95 96 Descriptor arangeDescriptor; 97 98 static_assert(sizeof(arangeDescriptor.address) == 99 sizeof(arangeDescriptor.length), 100 "DWARFDebugArangeSet::Descriptor.address and " 101 "DWARFDebugArangeSet::Descriptor.length must have same size"); 102 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); 106 107 // Each set of tuples is terminated by a 0 for the address and 0 for 108 // the length. 109 if (!arangeDescriptor.address && !arangeDescriptor.length) 110 return llvm::ErrorSuccess(); 111 112 m_arange_descriptors.push_back(arangeDescriptor); 113 } 114 115 return llvm::make_error<llvm::object::GenericBinaryError>( 116 "arange descriptors not terminated by null entry"); 117} 118 119class DescriptorContainsAddress { 120public: 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)); 125 } 126 127private: 128 const dw_addr_t m_address; 129}; 130 131dw_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 136 if (pos != end) 137 return m_header.cu_offset; 138 139 return DW_INVALID_OFFSET; 140} 141