Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugPubTable.cpp
35266 views
//===- DWARFDebugPubTable.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/DWARFDebugPubTable.h"9#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"10#include "llvm/ADT/StringRef.h"11#include "llvm/BinaryFormat/Dwarf.h"12#include "llvm/Support/DataExtractor.h"13#include "llvm/Support/Errc.h"14#include "llvm/Support/Format.h"15#include "llvm/Support/raw_ostream.h"16#include <cstdint>1718using namespace llvm;19using namespace dwarf;2021void DWARFDebugPubTable::extract(22DWARFDataExtractor Data, bool GnuStyle,23function_ref<void(Error)> RecoverableErrorHandler) {24this->GnuStyle = GnuStyle;25Sets.clear();26uint64_t Offset = 0;27while (Data.isValidOffset(Offset)) {28uint64_t SetOffset = Offset;29Sets.push_back({});30Set &NewSet = Sets.back();3132DataExtractor::Cursor C(Offset);33std::tie(NewSet.Length, NewSet.Format) = Data.getInitialLength(C);34if (!C) {35// Drop the newly added set because it does not contain anything useful36// to dump.37Sets.pop_back();38RecoverableErrorHandler(createStringError(39errc::invalid_argument,40"name lookup table at offset 0x%" PRIx64 " parsing failed: %s",41SetOffset, toString(C.takeError()).c_str()));42return;43}4445Offset = C.tell() + NewSet.Length;46DWARFDataExtractor SetData(Data, Offset);47const unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(NewSet.Format);4849NewSet.Version = SetData.getU16(C);50NewSet.Offset = SetData.getRelocatedValue(C, OffsetSize);51NewSet.Size = SetData.getUnsigned(C, OffsetSize);5253if (!C) {54// Preserve the newly added set because at least some fields of the header55// are read and can be dumped.56RecoverableErrorHandler(57createStringError(errc::invalid_argument,58"name lookup table at offset 0x%" PRIx6459" does not have a complete header: %s",60SetOffset, toString(C.takeError()).c_str()));61continue;62}6364while (C) {65uint64_t DieRef = SetData.getUnsigned(C, OffsetSize);66if (DieRef == 0)67break;68uint8_t IndexEntryValue = GnuStyle ? SetData.getU8(C) : 0;69StringRef Name = SetData.getCStrRef(C);70if (C)71NewSet.Entries.push_back(72{DieRef, PubIndexEntryDescriptor(IndexEntryValue), Name});73}7475if (!C) {76RecoverableErrorHandler(createStringError(77errc::invalid_argument,78"name lookup table at offset 0x%" PRIx64 " parsing failed: %s",79SetOffset, toString(C.takeError()).c_str()));80continue;81}82if (C.tell() != Offset)83RecoverableErrorHandler(createStringError(84errc::invalid_argument,85"name lookup table at offset 0x%" PRIx6486" has a terminator at offset 0x%" PRIx6487" before the expected end at 0x%" PRIx64,88SetOffset, C.tell() - OffsetSize, Offset - OffsetSize));89}90}9192void DWARFDebugPubTable::dump(raw_ostream &OS) const {93for (const Set &S : Sets) {94int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(S.Format);95OS << "length = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Length);96OS << ", format = " << dwarf::FormatString(S.Format);97OS << ", version = " << format("0x%04x", S.Version);98OS << ", unit_offset = "99<< format("0x%0*" PRIx64, OffsetDumpWidth, S.Offset);100OS << ", unit_size = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Size)101<< '\n';102OS << (GnuStyle ? "Offset Linkage Kind Name\n"103: "Offset Name\n");104105for (const Entry &E : S.Entries) {106OS << format("0x%0*" PRIx64 " ", OffsetDumpWidth, E.SecOffset);107if (GnuStyle) {108StringRef EntryLinkage =109GDBIndexEntryLinkageString(E.Descriptor.Linkage);110StringRef EntryKind = dwarf::GDBIndexEntryKindString(E.Descriptor.Kind);111OS << format("%-8s", EntryLinkage.data()) << ' '112<< format("%-8s", EntryKind.data()) << ' ';113}114OS << '\"' << E.Name << "\"\n";115}116}117}118119120