Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp
35269 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 "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"9#include "llvm/BinaryFormat/Dwarf.h"10#include "llvm/DebugInfo/DWARF/DWARFContext.h"11#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"12#include "llvm/Support/Errc.h"13#include "llvm/Support/Format.h"14#include "llvm/Support/raw_ostream.h"15#include <cassert>16#include <cinttypes>17#include <cstdint>18#include <cstring>1920using namespace llvm;2122void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS,23uint32_t AddressSize) const {24OS << '[';25DWARFFormValue::dumpAddress(OS, AddressSize, Address);26OS << ", ";27DWARFFormValue::dumpAddress(OS, AddressSize, getEndAddress());28OS << ')';29}3031void DWARFDebugArangeSet::clear() {32Offset = -1ULL;33std::memset(&HeaderData, 0, sizeof(Header));34ArangeDescriptors.clear();35}3637Error DWARFDebugArangeSet::extract(DWARFDataExtractor data,38uint64_t *offset_ptr,39function_ref<void(Error)> WarningHandler) {40assert(data.isValidOffset(*offset_ptr));41ArangeDescriptors.clear();42Offset = *offset_ptr;4344// 7.21 Address Range Table (extract)45// Each set of entries in the table of address ranges contained in46// the .debug_aranges section begins with a header containing:47// 1. unit_length (initial length)48// A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing49// the length of the set of entries for this compilation unit,50// not including the length field itself.51// 2. version (uhalf)52// The value in this field is 2.53// 3. debug_info_offset (section offset)54// A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the55// .debug_info section of the compilation unit header.56// 4. address_size (ubyte)57// 5. segment_selector_size (ubyte)58// This header is followed by a series of tuples. Each tuple consists of59// a segment, an address and a length. The segment selector size is given by60// the segment_selector_size field of the header; the address and length61// size are each given by the address_size field of the header. Each set of62// tuples is terminated by a 0 for the segment, a 0 for the address and 063// for the length. If the segment_selector_size field in the header is zero,64// the segment selectors are omitted from all tuples, including65// the terminating tuple.6667Error Err = Error::success();68std::tie(HeaderData.Length, HeaderData.Format) =69data.getInitialLength(offset_ptr, &Err);70HeaderData.Version = data.getU16(offset_ptr, &Err);71HeaderData.CuOffset = data.getUnsigned(72offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);73HeaderData.AddrSize = data.getU8(offset_ptr, &Err);74HeaderData.SegSize = data.getU8(offset_ptr, &Err);75if (Err) {76return createStringError(errc::invalid_argument,77"parsing address ranges table at offset 0x%" PRIx6478": %s",79Offset, toString(std::move(Err)).c_str());80}8182// Perform basic validation of the header fields.83uint64_t full_length =84dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;85if (!data.isValidOffsetForDataOfSize(Offset, full_length))86return createStringError(errc::invalid_argument,87"the length of address range table at offset "88"0x%" PRIx64 " exceeds section size",89Offset);90if (Error SizeErr = DWARFContext::checkAddressSizeSupported(91HeaderData.AddrSize, errc::invalid_argument,92"address range table at offset 0x%" PRIx64, Offset))93return SizeErr;94if (HeaderData.SegSize != 0)95return createStringError(errc::not_supported,96"non-zero segment selector size in address range "97"table at offset 0x%" PRIx64 " is not supported",98Offset);99100// The first tuple following the header in each set begins at an offset that101// is a multiple of the size of a single tuple (that is, twice the size of102// an address because we do not support non-zero segment selector sizes).103// Therefore, the full length should also be a multiple of the tuple size.104const uint32_t tuple_size = HeaderData.AddrSize * 2;105if (full_length % tuple_size != 0)106return createStringError(107errc::invalid_argument,108"address range table at offset 0x%" PRIx64109" has length that is not a multiple of the tuple size",110Offset);111112// The header is padded, if necessary, to the appropriate boundary.113const uint32_t header_size = *offset_ptr - Offset;114uint32_t first_tuple_offset = 0;115while (first_tuple_offset < header_size)116first_tuple_offset += tuple_size;117118// There should be space for at least one tuple.119if (full_length <= first_tuple_offset)120return createStringError(121errc::invalid_argument,122"address range table at offset 0x%" PRIx64123" has an insufficient length to contain any entries",124Offset);125126*offset_ptr = Offset + first_tuple_offset;127128Descriptor arangeDescriptor;129130static_assert(sizeof(arangeDescriptor.Address) ==131sizeof(arangeDescriptor.Length),132"Different datatypes for addresses and sizes!");133assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);134135uint64_t end_offset = Offset + full_length;136while (*offset_ptr < end_offset) {137uint64_t EntryOffset = *offset_ptr;138arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);139arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);140141// Each set of tuples is terminated by a 0 for the address and 0142// for the length.143if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) {144if (*offset_ptr == end_offset)145return ErrorSuccess();146WarningHandler(createStringError(147errc::invalid_argument,148"address range table at offset 0x%" PRIx64149" has a premature terminator entry at offset 0x%" PRIx64,150Offset, EntryOffset));151}152153ArangeDescriptors.push_back(arangeDescriptor);154}155156return createStringError(errc::invalid_argument,157"address range table at offset 0x%" PRIx64158" is not terminated by null entry",159Offset);160}161162void DWARFDebugArangeSet::dump(raw_ostream &OS) const {163int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);164OS << "Address Range Header: "165<< format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length)166<< "format = " << dwarf::FormatString(HeaderData.Format) << ", "167<< format("version = 0x%4.4x, ", HeaderData.Version)168<< format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth,169HeaderData.CuOffset)170<< format("addr_size = 0x%2.2x, ", HeaderData.AddrSize)171<< format("seg_size = 0x%2.2x\n", HeaderData.SegSize);172173for (const auto &Desc : ArangeDescriptors) {174Desc.dump(OS, HeaderData.AddrSize);175OS << '\n';176}177}178179180