Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFUnitIndex.cpp
35269 views
//===- DWARFUnitIndex.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/DWARFUnitIndex.h"9#include "llvm/ADT/STLExtras.h"10#include "llvm/ADT/StringRef.h"11#include "llvm/Support/DataExtractor.h"12#include "llvm/Support/ErrorHandling.h"13#include "llvm/Support/Format.h"14#include "llvm/Support/raw_ostream.h"15#include <cinttypes>16#include <cstdint>1718using namespace llvm;1920namespace {2122enum class DWARFSectionKindV2 {23DW_SECT_INFO = 1,24DW_SECT_TYPES = 2,25DW_SECT_ABBREV = 3,26DW_SECT_LINE = 4,27DW_SECT_LOC = 5,28DW_SECT_STR_OFFSETS = 6,29DW_SECT_MACINFO = 7,30DW_SECT_MACRO = 8,31};3233} // namespace3435// Return true if the section identifier is defined in the DWARFv5 standard.36constexpr bool isKnownV5SectionID(uint32_t ID) {37return ID >= DW_SECT_INFO && ID <= DW_SECT_RNGLISTS &&38ID != DW_SECT_EXT_TYPES;39}4041uint32_t llvm::serializeSectionKind(DWARFSectionKind Kind,42unsigned IndexVersion) {43if (IndexVersion == 5) {44assert(isKnownV5SectionID(Kind));45return static_cast<uint32_t>(Kind);46}47assert(IndexVersion == 2);48switch (Kind) {49#define CASE(S,T) \50case DW_SECT_##S: \51return static_cast<uint32_t>(DWARFSectionKindV2::DW_SECT_##T)52CASE(INFO, INFO);53CASE(EXT_TYPES, TYPES);54CASE(ABBREV, ABBREV);55CASE(LINE, LINE);56CASE(EXT_LOC, LOC);57CASE(STR_OFFSETS, STR_OFFSETS);58CASE(EXT_MACINFO, MACINFO);59CASE(MACRO, MACRO);60#undef CASE61default:62// All other section kinds have no corresponding values in v2 indexes.63llvm_unreachable("Invalid DWARFSectionKind");64}65}6667DWARFSectionKind llvm::deserializeSectionKind(uint32_t Value,68unsigned IndexVersion) {69if (IndexVersion == 5)70return isKnownV5SectionID(Value)71? static_cast<DWARFSectionKind>(Value)72: DW_SECT_EXT_unknown;73assert(IndexVersion == 2);74switch (static_cast<DWARFSectionKindV2>(Value)) {75#define CASE(S,T) \76case DWARFSectionKindV2::DW_SECT_##S: \77return DW_SECT_##T78CASE(INFO, INFO);79CASE(TYPES, EXT_TYPES);80CASE(ABBREV, ABBREV);81CASE(LINE, LINE);82CASE(LOC, EXT_LOC);83CASE(STR_OFFSETS, STR_OFFSETS);84CASE(MACINFO, EXT_MACINFO);85CASE(MACRO, MACRO);86#undef CASE87}88return DW_SECT_EXT_unknown;89}9091bool DWARFUnitIndex::Header::parse(DataExtractor IndexData,92uint64_t *OffsetPtr) {93const uint64_t BeginOffset = *OffsetPtr;94if (!IndexData.isValidOffsetForDataOfSize(*OffsetPtr, 16))95return false;96// GCC Debug Fission defines the version as an unsigned 32-bit field97// with value of 2, https://gcc.gnu.org/wiki/DebugFissionDWP.98// DWARFv5 defines the same space as an uhalf version field with value of 599// and a 2 bytes long padding, see Section 7.3.5.3.100Version = IndexData.getU32(OffsetPtr);101if (Version != 2) {102*OffsetPtr = BeginOffset;103Version = IndexData.getU16(OffsetPtr);104if (Version != 5)105return false;106*OffsetPtr += 2; // Skip padding.107}108NumColumns = IndexData.getU32(OffsetPtr);109NumUnits = IndexData.getU32(OffsetPtr);110NumBuckets = IndexData.getU32(OffsetPtr);111return true;112}113114void DWARFUnitIndex::Header::dump(raw_ostream &OS) const {115OS << format("version = %u, units = %u, slots = %u\n\n", Version, NumUnits, NumBuckets);116}117118bool DWARFUnitIndex::parse(DataExtractor IndexData) {119bool b = parseImpl(IndexData);120if (!b) {121// Make sure we don't try to dump anything122Header.NumBuckets = 0;123// Release any partially initialized data.124ColumnKinds.reset();125Rows.reset();126}127return b;128}129130bool DWARFUnitIndex::parseImpl(DataExtractor IndexData) {131uint64_t Offset = 0;132if (!Header.parse(IndexData, &Offset))133return false;134135// Fix InfoColumnKind: in DWARFv5, type units are in .debug_info.dwo.136if (Header.Version == 5)137InfoColumnKind = DW_SECT_INFO;138139if (!IndexData.isValidOffsetForDataOfSize(140Offset, Header.NumBuckets * (8 + 4) +141(2 * Header.NumUnits + 1) * 4 * Header.NumColumns))142return false;143144Rows = std::make_unique<Entry[]>(Header.NumBuckets);145auto Contribs =146std::make_unique<Entry::SectionContribution *[]>(Header.NumUnits);147ColumnKinds = std::make_unique<DWARFSectionKind[]>(Header.NumColumns);148RawSectionIds = std::make_unique<uint32_t[]>(Header.NumColumns);149150// Read Hash Table of Signatures151for (unsigned i = 0; i != Header.NumBuckets; ++i)152Rows[i].Signature = IndexData.getU64(&Offset);153154// Read Parallel Table of Indexes155for (unsigned i = 0; i != Header.NumBuckets; ++i) {156auto Index = IndexData.getU32(&Offset);157if (!Index)158continue;159Rows[i].Index = this;160Rows[i].Contributions =161std::make_unique<Entry::SectionContribution[]>(Header.NumColumns);162Contribs[Index - 1] = Rows[i].Contributions.get();163}164165// Read the Column Headers166for (unsigned i = 0; i != Header.NumColumns; ++i) {167RawSectionIds[i] = IndexData.getU32(&Offset);168ColumnKinds[i] = deserializeSectionKind(RawSectionIds[i], Header.Version);169if (ColumnKinds[i] == InfoColumnKind) {170if (InfoColumn != -1)171return false;172InfoColumn = i;173}174}175176if (InfoColumn == -1)177return false;178179// Read Table of Section Offsets180for (unsigned i = 0; i != Header.NumUnits; ++i) {181auto *Contrib = Contribs[i];182for (unsigned i = 0; i != Header.NumColumns; ++i)183Contrib[i].setOffset(IndexData.getU32(&Offset));184}185186// Read Table of Section Sizes187for (unsigned i = 0; i != Header.NumUnits; ++i) {188auto *Contrib = Contribs[i];189for (unsigned i = 0; i != Header.NumColumns; ++i)190Contrib[i].setLength(IndexData.getU32(&Offset));191}192193return true;194}195196StringRef DWARFUnitIndex::getColumnHeader(DWARFSectionKind DS) {197switch (DS) {198#define HANDLE_DW_SECT(ID, NAME) \199case DW_SECT_##NAME: \200return #NAME;201#include "llvm/BinaryFormat/Dwarf.def"202case DW_SECT_EXT_TYPES:203return "TYPES";204case DW_SECT_EXT_LOC:205return "LOC";206case DW_SECT_EXT_MACINFO:207return "MACINFO";208case DW_SECT_EXT_unknown:209return StringRef();210}211llvm_unreachable("Unknown DWARFSectionKind");212}213214void DWARFUnitIndex::dump(raw_ostream &OS) const {215if (!*this)216return;217218Header.dump(OS);219OS << "Index Signature ";220for (unsigned i = 0; i != Header.NumColumns; ++i) {221DWARFSectionKind Kind = ColumnKinds[i];222StringRef Name = getColumnHeader(Kind);223if (!Name.empty())224OS << ' '225<< left_justify(Name,226Kind == DWARFSectionKind::DW_SECT_INFO ? 40 : 24);227else228OS << format(" Unknown: %-15" PRIu32, RawSectionIds[i]);229}230OS << "\n----- ------------------";231for (unsigned i = 0; i != Header.NumColumns; ++i) {232DWARFSectionKind Kind = ColumnKinds[i];233if (Kind == DWARFSectionKind::DW_SECT_INFO ||234Kind == DWARFSectionKind::DW_SECT_EXT_TYPES)235OS << " ----------------------------------------";236else237OS << " ------------------------";238}239OS << '\n';240for (unsigned i = 0; i != Header.NumBuckets; ++i) {241auto &Row = Rows[i];242if (auto *Contribs = Row.Contributions.get()) {243OS << format("%5u 0x%016" PRIx64 " ", i + 1, Row.Signature);244for (unsigned i = 0; i != Header.NumColumns; ++i) {245auto &Contrib = Contribs[i];246DWARFSectionKind Kind = ColumnKinds[i];247if (Kind == DWARFSectionKind::DW_SECT_INFO ||248Kind == DWARFSectionKind::DW_SECT_EXT_TYPES)249OS << format("[0x%016" PRIx64 ", 0x%016" PRIx64 ") ",250Contrib.getOffset(),251Contrib.getOffset() + Contrib.getLength());252else253OS << format("[0x%08" PRIx32 ", 0x%08" PRIx32 ") ",254Contrib.getOffset32(),255Contrib.getOffset32() + Contrib.getLength32());256}257OS << '\n';258}259}260}261262const DWARFUnitIndex::Entry::SectionContribution *263DWARFUnitIndex::Entry::getContribution(DWARFSectionKind Sec) const {264uint32_t i = 0;265for (; i != Index->Header.NumColumns; ++i)266if (Index->ColumnKinds[i] == Sec)267return &Contributions[i];268return nullptr;269}270271DWARFUnitIndex::Entry::SectionContribution &272DWARFUnitIndex::Entry::getContribution() {273return Contributions[Index->InfoColumn];274}275276const DWARFUnitIndex::Entry::SectionContribution *277DWARFUnitIndex::Entry::getContribution() const {278return &Contributions[Index->InfoColumn];279}280281const DWARFUnitIndex::Entry *282DWARFUnitIndex::getFromOffset(uint64_t Offset) const {283if (OffsetLookup.empty()) {284for (uint32_t i = 0; i != Header.NumBuckets; ++i)285if (Rows[i].Contributions)286OffsetLookup.push_back(&Rows[i]);287llvm::sort(OffsetLookup, [&](Entry *E1, Entry *E2) {288return E1->Contributions[InfoColumn].getOffset() <289E2->Contributions[InfoColumn].getOffset();290});291}292auto I = partition_point(OffsetLookup, [&](Entry *E2) {293return E2->Contributions[InfoColumn].getOffset() <= Offset;294});295if (I == OffsetLookup.begin())296return nullptr;297--I;298const auto *E = *I;299const auto &InfoContrib = E->Contributions[InfoColumn];300if ((InfoContrib.getOffset() + InfoContrib.getLength()) <= Offset)301return nullptr;302return E;303}304305const DWARFUnitIndex::Entry *DWARFUnitIndex::getFromHash(uint64_t S) const {306uint64_t Mask = Header.NumBuckets - 1;307308auto H = S & Mask;309auto HP = ((S >> 32) & Mask) | 1;310// The spec says "while 0 is a valid hash value, the row index in a used slot311// will always be non-zero". Loop until we find a match or an empty slot.312while (Rows[H].getSignature() != S && Rows[H].Index != nullptr)313H = (H + HP) & Mask;314315// If the slot is empty, we don't care whether the signature matches (it could316// be zero and still match the zeros in the empty slot).317if (Rows[H].Index == nullptr)318return nullptr;319320return &Rows[H];321}322323324