Path: blob/main/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugArangeSet.cpp
39644 views
//===-- DWARFDebugArangeSet.cpp -------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "DWARFDebugArangeSet.h"9#include "DWARFDataExtractor.h"10#include "LogChannelDWARF.h"11#include "llvm/Object/Error.h"12#include <cassert>1314using namespace lldb_private;15using namespace lldb_private::plugin::dwarf;1617DWARFDebugArangeSet::DWARFDebugArangeSet()18: m_offset(DW_INVALID_OFFSET), m_next_offset(DW_INVALID_OFFSET) {}1920void DWARFDebugArangeSet::Clear() {21m_offset = DW_INVALID_OFFSET;22m_next_offset = DW_INVALID_OFFSET;23m_header.length = 0;24m_header.version = 0;25m_header.cu_offset = 0;26m_header.addr_size = 0;27m_header.seg_size = 0;28m_arange_descriptors.clear();29}3031llvm::Error DWARFDebugArangeSet::extract(const DWARFDataExtractor &data,32lldb::offset_t *offset_ptr) {33assert(data.ValidOffset(*offset_ptr));3435m_arange_descriptors.clear();36m_offset = *offset_ptr;3738// 7.20 Address Range Table39//40// Each set of entries in the table of address ranges contained in the41// .debug_aranges section begins with a header consisting of: a 4-byte42// length containing the length of the set of entries for this compilation43// unit, not including the length field itself; a 2-byte version identifier44// containing the value 2 for DWARF Version 2; a 4-byte offset into45// the.debug_infosection; a 1-byte unsigned integer containing the size in46// bytes of an address (or the offset portion of an address for segmented47// addressing) on the target system; and a 1-byte unsigned integer48// containing the size in bytes of a segment descriptor on the target49// system. This header is followed by a series of tuples. Each tuple50// consists of an address and a length, each in the size appropriate for an51// address on the target architecture.52m_header.length = data.GetDWARFInitialLength(offset_ptr);53// The length could be 4 bytes or 12 bytes, so use the current offset to54// determine the next offset correctly.55if (m_header.length > 0)56m_next_offset = *offset_ptr + m_header.length;57else58m_next_offset = DW_INVALID_OFFSET;59m_header.version = data.GetU16(offset_ptr);60m_header.cu_offset = data.GetDWARFOffset(offset_ptr);61m_header.addr_size = data.GetU8(offset_ptr);62m_header.seg_size = data.GetU8(offset_ptr);6364// Try to avoid reading invalid arange sets by making sure:65// 1 - the version looks good66// 2 - the address byte size looks plausible67// 3 - the length seems to make sense68// 4 - size looks plausible69// 5 - the arange tuples do not contain a segment field70if (m_header.version < 2 || m_header.version > 5)71return llvm::make_error<llvm::object::GenericBinaryError>(72"Invalid arange header version");7374if (m_header.addr_size != 4 && m_header.addr_size != 8)75return llvm::make_error<llvm::object::GenericBinaryError>(76"Invalid arange header address size");7778if (m_header.length == 0)79return llvm::make_error<llvm::object::GenericBinaryError>(80"Invalid arange header length");8182if (!data.ValidOffset(m_offset + sizeof(m_header.length) + m_header.length -831))84return llvm::make_error<llvm::object::GenericBinaryError>(85"Invalid arange header length");8687if (m_header.seg_size)88return llvm::make_error<llvm::object::GenericBinaryError>(89"segmented arange entries are not supported");9091// The first tuple following the header in each set begins at an offset92// that is a multiple of the size of a single tuple (that is, twice the93// size of an address). The header is padded, if necessary, to the94// appropriate boundary.95const uint32_t header_size = *offset_ptr - m_offset;96const uint32_t tuple_size = m_header.addr_size << 1;97uint32_t first_tuple_offset = 0;98while (first_tuple_offset < header_size)99first_tuple_offset += tuple_size;100101*offset_ptr = m_offset + first_tuple_offset;102103Descriptor arangeDescriptor;104105static_assert(sizeof(arangeDescriptor.address) ==106sizeof(arangeDescriptor.length),107"DWARFDebugArangeSet::Descriptor.address and "108"DWARFDebugArangeSet::Descriptor.length must have same size");109110const lldb::offset_t next_offset = GetNextOffset();111assert(next_offset != DW_INVALID_OFFSET);112uint32_t num_terminators = 0;113bool last_was_terminator = false;114while (*offset_ptr < next_offset) {115arangeDescriptor.address = data.GetMaxU64(offset_ptr, m_header.addr_size);116arangeDescriptor.length = data.GetMaxU64(offset_ptr, m_header.addr_size);117118// Each set of tuples is terminated by a 0 for the address and 0 for119// the length. Some linkers can emit .debug_aranges with multiple120// terminator pair entries that are still withing the length of the121// DWARFDebugArangeSet. We want to be sure to parse all entries for122// this DWARFDebugArangeSet so that we don't stop parsing early and end up123// treating addresses as a header of the next DWARFDebugArangeSet. We also124// need to make sure we parse all valid address pairs so we don't omit them125// from the aranges result, so we can't stop at the first terminator entry126// we find.127if (arangeDescriptor.address == 0 && arangeDescriptor.length == 0) {128++num_terminators;129last_was_terminator = true;130} else {131last_was_terminator = false;132// Only add .debug_aranges address entries that have a non zero size.133// Some linkers will zero out the length field for some .debug_aranges134// entries if they were stripped. We also could watch out for multiple135// entries at address zero and remove those as well.136if (arangeDescriptor.length > 0)137m_arange_descriptors.push_back(arangeDescriptor);138}139}140if (num_terminators > 1) {141Log *log = GetLog(DWARFLog::DebugInfo);142LLDB_LOG(log,143"warning: DWARFDebugArangeSet at %#" PRIx64 " contains %u "144"terminator entries",145m_offset, num_terminators);146}147if (last_was_terminator)148return llvm::ErrorSuccess();149150return llvm::make_error<llvm::object::GenericBinaryError>(151"arange descriptors not terminated by null entry");152}153154class DescriptorContainsAddress {155public:156DescriptorContainsAddress(dw_addr_t address) : m_address(address) {}157bool operator()(const DWARFDebugArangeSet::Descriptor &desc) const {158return (m_address >= desc.address) &&159(m_address < (desc.address + desc.length));160}161162private:163const dw_addr_t m_address;164};165166dw_offset_t DWARFDebugArangeSet::FindAddress(dw_addr_t address) const {167DescriptorConstIter end = m_arange_descriptors.end();168DescriptorConstIter pos =169std::find_if(m_arange_descriptors.begin(), end, // Range170DescriptorContainsAddress(address)); // Predicate171if (pos != end)172return m_header.cu_offset;173174return DW_INVALID_OFFSET;175}176177178