aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/lib/DebugInfo/DWARF/DWARFDebugPubTable.cpp
blob: 5031acdb54efcc19ccedf41eec737b036ebfd343 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//===- DWARFDebugPubTable.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdint>

using namespace llvm;
using namespace dwarf;

void DWARFDebugPubTable::extract(
    DWARFDataExtractor Data, bool GnuStyle,
    function_ref<void(Error)> RecoverableErrorHandler) {
  this->GnuStyle = GnuStyle;
  Sets.clear();
  uint64_t Offset = 0;
  while (Data.isValidOffset(Offset)) {
    uint64_t SetOffset = Offset;
    Sets.push_back({});
    Set &NewSet = Sets.back();

    DataExtractor::Cursor C(Offset);
    std::tie(NewSet.Length, NewSet.Format) = Data.getInitialLength(C);
    if (!C) {
      // Drop the newly added set because it does not contain anything useful
      // to dump.
      Sets.pop_back();
      RecoverableErrorHandler(createStringError(
          errc::invalid_argument,
          "name lookup table at offset 0x%" PRIx64 " parsing failed: %s",
          SetOffset, toString(C.takeError()).c_str()));
      return;
    }

    Offset = C.tell() + NewSet.Length;
    DWARFDataExtractor SetData(Data, Offset);
    const unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(NewSet.Format);

    NewSet.Version = SetData.getU16(C);
    NewSet.Offset = SetData.getRelocatedValue(C, OffsetSize);
    NewSet.Size = SetData.getUnsigned(C, OffsetSize);

    if (!C) {
      // Preserve the newly added set because at least some fields of the header
      // are read and can be dumped.
      RecoverableErrorHandler(
          createStringError(errc::invalid_argument,
                            "name lookup table at offset 0x%" PRIx64
                            " does not have a complete header: %s",
                            SetOffset, toString(C.takeError()).c_str()));
      continue;
    }

    while (C) {
      uint64_t DieRef = SetData.getUnsigned(C, OffsetSize);
      if (DieRef == 0)
        break;
      uint8_t IndexEntryValue = GnuStyle ? SetData.getU8(C) : 0;
      StringRef Name = SetData.getCStrRef(C);
      if (C)
        NewSet.Entries.push_back(
            {DieRef, PubIndexEntryDescriptor(IndexEntryValue), Name});
    }

    if (!C) {
      RecoverableErrorHandler(createStringError(
          errc::invalid_argument,
          "name lookup table at offset 0x%" PRIx64 " parsing failed: %s",
          SetOffset, toString(C.takeError()).c_str()));
      continue;
    }
    if (C.tell() != Offset)
      RecoverableErrorHandler(createStringError(
          errc::invalid_argument,
          "name lookup table at offset 0x%" PRIx64
          " has a terminator at offset 0x%" PRIx64
          " before the expected end at 0x%" PRIx64,
          SetOffset, C.tell() - OffsetSize, Offset - OffsetSize));
  }
}

void DWARFDebugPubTable::dump(raw_ostream &OS) const {
  for (const Set &S : Sets) {
    int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(S.Format);
    OS << "length = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Length);
    OS << ", format = " << dwarf::FormatString(S.Format);
    OS << ", version = " << format("0x%04x", S.Version);
    OS << ", unit_offset = "
       << format("0x%0*" PRIx64, OffsetDumpWidth, S.Offset);
    OS << ", unit_size = " << format("0x%0*" PRIx64, OffsetDumpWidth, S.Size)
       << '\n';
    OS << (GnuStyle ? "Offset     Linkage  Kind     Name\n"
                    : "Offset     Name\n");

    for (const Entry &E : S.Entries) {
      OS << format("0x%0*" PRIx64 " ", OffsetDumpWidth, E.SecOffset);
      if (GnuStyle) {
        StringRef EntryLinkage =
            GDBIndexEntryLinkageString(E.Descriptor.Linkage);
        StringRef EntryKind = dwarf::GDBIndexEntryKindString(E.Descriptor.Kind);
        OS << format("%-8s", EntryLinkage.data()) << ' '
           << format("%-8s", EntryKind.data()) << ' ';
      }
      OS << '\"' << E.Name << "\"\n";
    }
  }
}