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 
18 using namespace llvm;
19 using namespace dwarf;
20 
21 DWARFDebugPubTable::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     std::tie(SetData.Length, SetData.Format) =
32         PubNames.getInitialLength(&Offset);
33     const unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(SetData.Format);
34 
35     SetData.Version = PubNames.getU16(&Offset);
36     SetData.Offset = PubNames.getRelocatedValue(OffsetSize, &Offset);
37     SetData.Size = PubNames.getUnsigned(&Offset, OffsetSize);
38 
39     while (Offset < Sec.Data.size()) {
40       uint64_t DieRef = PubNames.getUnsigned(&Offset, OffsetSize);
41       if (DieRef == 0)
42         break;
43       uint8_t IndexEntryValue = GnuStyle ? PubNames.getU8(&Offset) : 0;
44       StringRef Name = PubNames.getCStrRef(&Offset);
45       SetData.Entries.push_back(
46           {DieRef, PubIndexEntryDescriptor(IndexEntryValue), Name});
47     }
48   }
49 }
50 
51 void DWARFDebugPubTable::dump(raw_ostream &OS) const {
52   for (const Set &S : Sets) {
53     int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(S.Format);
54     OS << "length = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Length);
55     OS << ", format = " << dwarf::FormatString(S.Format);
56     OS << ", version = " << format("0x%04x", S.Version);
57     OS << ", unit_offset = "
58        << format("0x%0*" PRIx64, OffsetDumpWidth, S.Offset);
59     OS << ", unit_size = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Size)
60        << '\n';
61     OS << (GnuStyle ? "Offset     Linkage  Kind     Name\n"
62                     : "Offset     Name\n");
63 
64     for (const Entry &E : S.Entries) {
65       OS << format("0x%0*" PRIx64 " ", OffsetDumpWidth, E.SecOffset);
66       if (GnuStyle) {
67         StringRef EntryLinkage =
68             GDBIndexEntryLinkageString(E.Descriptor.Linkage);
69         StringRef EntryKind = dwarf::GDBIndexEntryKindString(E.Descriptor.Kind);
70         OS << format("%-8s", EntryLinkage.data()) << ' '
71            << format("%-8s", EntryKind.data()) << ' ';
72       }
73       OS << '\"' << E.Name << "\"\n";
74     }
75   }
76 }
77