Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugRnglists.cpp
35269 views
//===- DWARFDebugRnglists.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/DWARFDebugRnglists.h"9#include "llvm/BinaryFormat/Dwarf.h"10#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"11#include "llvm/DebugInfo/DWARF/DWARFUnit.h"12#include "llvm/Support/Errc.h"13#include "llvm/Support/Error.h"14#include "llvm/Support/Format.h"15#include "llvm/Support/raw_ostream.h"1617using namespace llvm;1819Error RangeListEntry::extract(DWARFDataExtractor Data, uint64_t *OffsetPtr) {20Offset = *OffsetPtr;21SectionIndex = -1ULL;22// The caller should guarantee that we have at least 1 byte available, so23// we just assert instead of revalidate.24assert(*OffsetPtr < Data.size() &&25"not enough space to extract a rangelist encoding");26uint8_t Encoding = Data.getU8(OffsetPtr);2728DataExtractor::Cursor C(*OffsetPtr);29switch (Encoding) {30case dwarf::DW_RLE_end_of_list:31Value0 = Value1 = 0;32break;33// TODO: Support other encodings.34case dwarf::DW_RLE_base_addressx: {35Value0 = Data.getULEB128(C);36break;37}38case dwarf::DW_RLE_startx_endx:39Value0 = Data.getULEB128(C);40Value1 = Data.getULEB128(C);41break;42case dwarf::DW_RLE_startx_length: {43Value0 = Data.getULEB128(C);44Value1 = Data.getULEB128(C);45break;46}47case dwarf::DW_RLE_offset_pair: {48Value0 = Data.getULEB128(C);49Value1 = Data.getULEB128(C);50break;51}52case dwarf::DW_RLE_base_address: {53Value0 = Data.getRelocatedAddress(C, &SectionIndex);54break;55}56case dwarf::DW_RLE_start_end: {57Value0 = Data.getRelocatedAddress(C, &SectionIndex);58Value1 = Data.getRelocatedAddress(C);59break;60}61case dwarf::DW_RLE_start_length: {62Value0 = Data.getRelocatedAddress(C, &SectionIndex);63Value1 = Data.getULEB128(C);64break;65}66default:67consumeError(C.takeError());68return createStringError(errc::not_supported,69"unknown rnglists encoding 0x%" PRIx3270" at offset 0x%" PRIx64,71uint32_t(Encoding), Offset);72}7374if (!C) {75consumeError(C.takeError());76return createStringError(77errc::invalid_argument,78"read past end of table when reading %s encoding at offset 0x%" PRIx64,79dwarf::RLEString(Encoding).data(), Offset);80}8182*OffsetPtr = C.tell();83EntryKind = Encoding;84return Error::success();85}8687DWARFAddressRangesVector DWARFDebugRnglist::getAbsoluteRanges(88std::optional<object::SectionedAddress> BaseAddr, DWARFUnit &U) const {89return getAbsoluteRanges(90BaseAddr, U.getAddressByteSize(),91[&](uint32_t Index) { return U.getAddrOffsetSectionItem(Index); });92}9394DWARFAddressRangesVector DWARFDebugRnglist::getAbsoluteRanges(95std::optional<object::SectionedAddress> BaseAddr, uint8_t AddressByteSize,96function_ref<std::optional<object::SectionedAddress>(uint32_t)>97LookupPooledAddress) const {98DWARFAddressRangesVector Res;99uint64_t Tombstone = dwarf::computeTombstoneAddress(AddressByteSize);100for (const RangeListEntry &RLE : Entries) {101if (RLE.EntryKind == dwarf::DW_RLE_end_of_list)102break;103if (RLE.EntryKind == dwarf::DW_RLE_base_addressx) {104BaseAddr = LookupPooledAddress(RLE.Value0);105if (!BaseAddr)106BaseAddr = {RLE.Value0, -1ULL};107continue;108}109if (RLE.EntryKind == dwarf::DW_RLE_base_address) {110BaseAddr = {RLE.Value0, RLE.SectionIndex};111continue;112}113114DWARFAddressRange E;115E.SectionIndex = RLE.SectionIndex;116if (BaseAddr && E.SectionIndex == -1ULL)117E.SectionIndex = BaseAddr->SectionIndex;118119switch (RLE.EntryKind) {120case dwarf::DW_RLE_offset_pair:121E.LowPC = RLE.Value0;122if (E.LowPC == Tombstone)123continue;124E.HighPC = RLE.Value1;125if (BaseAddr) {126if (BaseAddr->Address == Tombstone)127continue;128E.LowPC += BaseAddr->Address;129E.HighPC += BaseAddr->Address;130}131break;132case dwarf::DW_RLE_start_end:133E.LowPC = RLE.Value0;134E.HighPC = RLE.Value1;135break;136case dwarf::DW_RLE_start_length:137E.LowPC = RLE.Value0;138E.HighPC = E.LowPC + RLE.Value1;139break;140case dwarf::DW_RLE_startx_length: {141auto Start = LookupPooledAddress(RLE.Value0);142if (!Start)143Start = {0, -1ULL};144E.SectionIndex = Start->SectionIndex;145E.LowPC = Start->Address;146E.HighPC = E.LowPC + RLE.Value1;147break;148}149case dwarf::DW_RLE_startx_endx: {150auto Start = LookupPooledAddress(RLE.Value0);151if (!Start)152Start = {0, -1ULL};153auto End = LookupPooledAddress(RLE.Value1);154if (!End)155End = {0, -1ULL};156// FIXME: Some error handling if Start.SectionIndex != End.SectionIndex157E.SectionIndex = Start->SectionIndex;158E.LowPC = Start->Address;159E.HighPC = End->Address;160break;161}162default:163// Unsupported encodings should have been reported during extraction,164// so we should not run into any here.165llvm_unreachable("Unsupported range list encoding");166}167if (E.LowPC == Tombstone)168continue;169Res.push_back(E);170}171return Res;172}173174void RangeListEntry::dump(175raw_ostream &OS, uint8_t AddrSize, uint8_t MaxEncodingStringLength,176uint64_t &CurrentBase, DIDumpOptions DumpOpts,177llvm::function_ref<std::optional<object::SectionedAddress>(uint32_t)>178LookupPooledAddress) const {179auto PrintRawEntry = [](raw_ostream &OS, const RangeListEntry &Entry,180uint8_t AddrSize, DIDumpOptions DumpOpts) {181if (DumpOpts.Verbose) {182DumpOpts.DisplayRawContents = true;183DWARFAddressRange(Entry.Value0, Entry.Value1)184.dump(OS, AddrSize, DumpOpts);185OS << " => ";186}187};188189if (DumpOpts.Verbose) {190// Print the section offset in verbose mode.191OS << format("0x%8.8" PRIx64 ":", Offset);192auto EncodingString = dwarf::RangeListEncodingString(EntryKind);193// Unsupported encodings should have been reported during parsing.194assert(!EncodingString.empty() && "Unknown range entry encoding");195OS << format(" [%s%*c", EncodingString.data(),196MaxEncodingStringLength - EncodingString.size() + 1, ']');197if (EntryKind != dwarf::DW_RLE_end_of_list)198OS << ": ";199}200201uint64_t Tombstone = dwarf::computeTombstoneAddress(AddrSize);202203switch (EntryKind) {204case dwarf::DW_RLE_end_of_list:205OS << (DumpOpts.Verbose ? "" : "<End of list>");206break;207case dwarf::DW_RLE_base_addressx: {208if (auto SA = LookupPooledAddress(Value0))209CurrentBase = SA->Address;210else211CurrentBase = Value0;212if (!DumpOpts.Verbose)213return;214DWARFFormValue::dumpAddress(OS << ' ', AddrSize, Value0);215break;216}217case dwarf::DW_RLE_base_address:218// In non-verbose mode we do not print anything for this entry.219CurrentBase = Value0;220if (!DumpOpts.Verbose)221return;222DWARFFormValue::dumpAddress(OS << ' ', AddrSize, Value0);223break;224case dwarf::DW_RLE_start_length:225PrintRawEntry(OS, *this, AddrSize, DumpOpts);226DWARFAddressRange(Value0, Value0 + Value1).dump(OS, AddrSize, DumpOpts);227break;228case dwarf::DW_RLE_offset_pair:229PrintRawEntry(OS, *this, AddrSize, DumpOpts);230if (CurrentBase != Tombstone)231DWARFAddressRange(Value0 + CurrentBase, Value1 + CurrentBase)232.dump(OS, AddrSize, DumpOpts);233else234OS << "dead code";235break;236case dwarf::DW_RLE_start_end:237DWARFAddressRange(Value0, Value1).dump(OS, AddrSize, DumpOpts);238break;239case dwarf::DW_RLE_startx_length: {240PrintRawEntry(OS, *this, AddrSize, DumpOpts);241uint64_t Start = 0;242if (auto SA = LookupPooledAddress(Value0))243Start = SA->Address;244DWARFAddressRange(Start, Start + Value1).dump(OS, AddrSize, DumpOpts);245break;246}247case dwarf::DW_RLE_startx_endx: {248PrintRawEntry(OS, *this, AddrSize, DumpOpts);249uint64_t Start = 0;250if (auto SA = LookupPooledAddress(Value0))251Start = SA->Address;252uint64_t End = 0;253if (auto SA = LookupPooledAddress(Value1))254End = SA->Address;255DWARFAddressRange(Start, End).dump(OS, AddrSize, DumpOpts);256break;257}258default:259llvm_unreachable("Unsupported range list encoding");260}261OS << "\n";262}263264265