1//===- DWARFDebugPubTable.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
10#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/BinaryFormat/Dwarf.h"
13#include "llvm/Support/DataExtractor.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16#include <cstdint>
17
18using namespace llvm;
19using namespace dwarf;
20
21DWARFDebugPubTable::DWARFDebugPubTable(const DWARFObject &Obj,
22                                       const DWARFSection &Sec,
23                                       bool LittleEndian, bool GnuStyle)
24    : GnuStyle(GnuStyle) {
25  DWARFDataExtractor PubNames(Obj, Sec, LittleEndian, 0);
26  uint64_t Offset = 0;
27  while (PubNames.isValidOffset(Offset)) {
28    Sets.push_back({});
29    Set &SetData = Sets.back();
30
31    SetData.Length = PubNames.getU32(&Offset);
32    SetData.Version = PubNames.getU16(&Offset);
33    SetData.Offset = PubNames.getRelocatedValue(4, &Offset);
34    SetData.Size = PubNames.getU32(&Offset);
35
36    while (Offset < Sec.Data.size()) {
37      uint32_t DieRef = PubNames.getU32(&Offset);
38      if (DieRef == 0)
39        break;
40      uint8_t IndexEntryValue = GnuStyle ? PubNames.getU8(&Offset) : 0;
41      StringRef Name = PubNames.getCStrRef(&Offset);
42      SetData.Entries.push_back(
43          {DieRef, PubIndexEntryDescriptor(IndexEntryValue), Name});
44    }
45  }
46}
47
48void DWARFDebugPubTable::dump(raw_ostream &OS) const {
49  for (const Set &S : Sets) {
50    OS << "length = " << format("0x%08x", S.Length);
51    OS << " version = " << format("0x%04x", S.Version);
52    OS << " unit_offset = " << format("0x%08" PRIx64, S.Offset);
53    OS << " unit_size = " << format("0x%08x", S.Size) << '\n';
54    OS << (GnuStyle ? "Offset     Linkage  Kind     Name\n"
55                    : "Offset     Name\n");
56
57    for (const Entry &E : S.Entries) {
58      OS << format("0x%8.8" PRIx64 " ", E.SecOffset);
59      if (GnuStyle) {
60        StringRef EntryLinkage =
61            GDBIndexEntryLinkageString(E.Descriptor.Linkage);
62        StringRef EntryKind = dwarf::GDBIndexEntryKindString(E.Descriptor.Kind);
63        OS << format("%-8s", EntryLinkage.data()) << ' '
64           << format("%-8s", EntryKind.data()) << ' ';
65      }
66      OS << '\"' << E.Name << "\"\n";
67    }
68  }
69}
70