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 << " version = " << format("0x%04x", S.Version);
56     OS << " unit_offset = "
57        << format("0x%0*" PRIx64, OffsetDumpWidth, S.Offset);
58     OS << " unit_size = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Size)
59        << '\n';
60     OS << (GnuStyle ? "Offset     Linkage  Kind     Name\n"
61                     : "Offset     Name\n");
62 
63     for (const Entry &E : S.Entries) {
64       OS << format("0x%0*" PRIx64 " ", OffsetDumpWidth, E.SecOffset);
65       if (GnuStyle) {
66         StringRef EntryLinkage =
67             GDBIndexEntryLinkageString(E.Descriptor.Linkage);
68         StringRef EntryKind = dwarf::GDBIndexEntryKindString(E.Descriptor.Kind);
69         OS << format("%-8s", EntryLinkage.data()) << ' '
70            << format("%-8s", EntryKind.data()) << ' ';
71       }
72       OS << '\"' << E.Name << "\"\n";
73     }
74   }
75 }
76