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 = dwarf::DWARF32;
32     SetData.Length = PubNames.getU32(&Offset);
33     if (SetData.Length == dwarf::DW_LENGTH_DWARF64) {
34       Format = dwarf::DWARF64;
35       SetData.Length = PubNames.getU64(&Offset);
36     }
37     const unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(Format);
38 
39     SetData.Version = PubNames.getU16(&Offset);
40     SetData.Offset = PubNames.getRelocatedValue(OffsetSize, &Offset);
41     SetData.Size = PubNames.getUnsigned(&Offset, OffsetSize);
42 
43     while (Offset < Sec.Data.size()) {
44       uint64_t DieRef = PubNames.getUnsigned(&Offset, OffsetSize);
45       if (DieRef == 0)
46         break;
47       uint8_t IndexEntryValue = GnuStyle ? PubNames.getU8(&Offset) : 0;
48       StringRef Name = PubNames.getCStrRef(&Offset);
49       SetData.Entries.push_back(
50           {DieRef, PubIndexEntryDescriptor(IndexEntryValue), Name});
51     }
52   }
53 }
54 
55 void DWARFDebugPubTable::dump(raw_ostream &OS) const {
56   for (const Set &S : Sets) {
57     OS << "length = " << format("0x%08" PRIx64, S.Length);
58     OS << " version = " << format("0x%04x", S.Version);
59     OS << " unit_offset = " << format("0x%08" PRIx64, S.Offset);
60     OS << " unit_size = " << format("0x%08" PRIx64, S.Size) << '\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%8.8" PRIx64 " ", 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