Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugRangeList.cpp
35269 views
//===- DWARFDebugRangesList.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/DWARFDebugRangeList.h"9#include "llvm/DebugInfo/DWARF/DWARFContext.h"10#include "llvm/Support/Errc.h"11#include "llvm/Support/Format.h"12#include "llvm/Support/raw_ostream.h"13#include <cinttypes>14#include <cstdint>1516using namespace llvm;1718bool DWARFDebugRangeList::RangeListEntry::isBaseAddressSelectionEntry(19uint8_t AddressSize) const {20assert(DWARFContext::isAddressSizeSupported(AddressSize));21return StartAddress == dwarf::computeTombstoneAddress(AddressSize);22}2324void DWARFDebugRangeList::clear() {25Offset = -1ULL;26AddressSize = 0;27Entries.clear();28}2930Error DWARFDebugRangeList::extract(const DWARFDataExtractor &data,31uint64_t *offset_ptr) {32clear();33if (!data.isValidOffset(*offset_ptr))34return createStringError(errc::invalid_argument,35"invalid range list offset 0x%" PRIx64, *offset_ptr);3637AddressSize = data.getAddressSize();38if (Error SizeErr = DWARFContext::checkAddressSizeSupported(39AddressSize, errc::invalid_argument,40"range list at offset 0x%" PRIx64, *offset_ptr))41return SizeErr;42Offset = *offset_ptr;43while (true) {44RangeListEntry Entry;45Entry.SectionIndex = -1ULL;4647uint64_t prev_offset = *offset_ptr;48Entry.StartAddress = data.getRelocatedAddress(offset_ptr);49Entry.EndAddress =50data.getRelocatedAddress(offset_ptr, &Entry.SectionIndex);5152// Check that both values were extracted correctly.53if (*offset_ptr != prev_offset + 2 * AddressSize) {54clear();55return createStringError(errc::invalid_argument,56"invalid range list entry at offset 0x%" PRIx64,57prev_offset);58}59if (Entry.isEndOfListEntry())60break;61Entries.push_back(Entry);62}63return Error::success();64}6566void DWARFDebugRangeList::dump(raw_ostream &OS) const {67const char *AddrFmt;68switch (AddressSize) {69case 2:70AddrFmt = "%08" PRIx64 " %04" PRIx64 " %04" PRIx64 "\n";71break;72case 4:73AddrFmt = "%08" PRIx64 " %08" PRIx64 " %08" PRIx64 "\n";74break;75case 8:76AddrFmt = "%08" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n";77break;78default:79llvm_unreachable("unsupported address size");80}81for (const RangeListEntry &RLE : Entries)82OS << format(AddrFmt, Offset, RLE.StartAddress, RLE.EndAddress);83OS << format("%08" PRIx64 " <End of list>\n", Offset);84}8586DWARFAddressRangesVector DWARFDebugRangeList::getAbsoluteRanges(87std::optional<object::SectionedAddress> BaseAddr) const {88DWARFAddressRangesVector Res;89// debug_addr can't use the max integer tombstone because that's used for the90// base address specifier entry - so use max-1.91uint64_t Tombstone = dwarf::computeTombstoneAddress(AddressSize) - 1;92for (const RangeListEntry &RLE : Entries) {93if (RLE.isBaseAddressSelectionEntry(AddressSize)) {94BaseAddr = {RLE.EndAddress, RLE.SectionIndex};95continue;96}9798DWARFAddressRange E;99E.LowPC = RLE.StartAddress;100if (E.LowPC == Tombstone)101continue;102E.HighPC = RLE.EndAddress;103E.SectionIndex = RLE.SectionIndex;104// Base address of a range list entry is determined by the closest preceding105// base address selection entry in the same range list. It defaults to the106// base address of the compilation unit if there is no such entry.107if (BaseAddr) {108if (BaseAddr->Address == Tombstone)109continue;110E.LowPC += BaseAddr->Address;111E.HighPC += BaseAddr->Address;112if (E.SectionIndex == -1ULL)113E.SectionIndex = BaseAddr->SectionIndex;114}115Res.push_back(E);116}117return Res;118}119120121