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     dwarf::DwarfFormat Format;
32     std::tie(SetData.Length, Format) = PubNames.getInitialLength(&Offset);
33     const unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(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     OS << "length = " << format("0x%08" PRIx64, S.Length);
54     OS << " version = " << format("0x%04x", S.Version);
55     OS << " unit_offset = " << format("0x%08" PRIx64, S.Offset);
56     OS << " unit_size = " << format("0x%08" PRIx64, S.Size) << '\n';
57     OS << (GnuStyle ? "Offset     Linkage  Kind     Name\n"
58                     : "Offset     Name\n");
59 
60     for (const Entry &E : S.Entries) {
61       OS << format("0x%8.8" PRIx64 " ", E.SecOffset);
62       if (GnuStyle) {
63         StringRef EntryLinkage =
64             GDBIndexEntryLinkageString(E.Descriptor.Linkage);
65         StringRef EntryKind = dwarf::GDBIndexEntryKindString(E.Descriptor.Kind);
66         OS << format("%-8s", EntryLinkage.data()) << ' '
67            << format("%-8s", EntryKind.data()) << ' ';
68       }
69       OS << '\"' << E.Name << "\"\n";
70     }
71   }
72 }
73