]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbutil/StreamUtil.cpp
Merge ACPICA 20180105.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-pdbutil / StreamUtil.cpp
1 //===- StreamUtil.cpp - PDB stream utilities --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "StreamUtil.h"
11 #include "FormatUtil.h"
12
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseMapInfo.h"
15 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
16 #include "llvm/DebugInfo/PDB/Native/DbiModuleList.h"
17 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
18 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
19 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
21
22 using namespace llvm;
23 using namespace llvm::pdb;
24
25 void llvm::pdb::discoverStreamPurposes(
26     PDBFile &File,
27     SmallVectorImpl<std::pair<StreamPurpose, std::string>> &Purposes) {
28   // It's OK if we fail to load some of these streams, we still attempt to print
29   // what we can.
30   auto Dbi = File.getPDBDbiStream();
31   auto Tpi = File.getPDBTpiStream();
32   auto Ipi = File.getPDBIpiStream();
33   auto Info = File.getPDBInfoStream();
34
35   uint32_t StreamCount = File.getNumStreams();
36   DenseMap<uint16_t, DbiModuleDescriptor> ModStreams;
37   DenseMap<uint16_t, std::string> NamedStreams;
38
39   if (Dbi) {
40     const DbiModuleList &Modules = Dbi->modules();
41     for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
42       DbiModuleDescriptor Descriptor = Modules.getModuleDescriptor(I);
43       uint16_t SN = Descriptor.getModuleStreamIndex();
44       if (SN != kInvalidStreamIndex)
45         ModStreams[SN] = Descriptor;
46     }
47   }
48   if (Info) {
49     for (auto &NSE : Info->named_streams()) {
50       if (NSE.second != kInvalidStreamIndex)
51         NamedStreams[NSE.second] = NSE.first();
52     }
53   }
54
55   Purposes.resize(StreamCount);
56   for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
57     std::pair<StreamPurpose, std::string> Value;
58     if (StreamIdx == OldMSFDirectory)
59       Value = std::make_pair(StreamPurpose::Other, "Old MSF Directory");
60     else if (StreamIdx == StreamPDB)
61       Value = std::make_pair(StreamPurpose::Other, "PDB Stream");
62     else if (StreamIdx == StreamDBI)
63       Value = std::make_pair(StreamPurpose::Other, "DBI Stream");
64     else if (StreamIdx == StreamTPI)
65       Value = std::make_pair(StreamPurpose::Other, "TPI Stream");
66     else if (StreamIdx == StreamIPI)
67       Value = std::make_pair(StreamPurpose::Other, "IPI Stream");
68     else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex())
69       Value = std::make_pair(StreamPurpose::Other, "Global Symbol Hash");
70     else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex())
71       Value = std::make_pair(StreamPurpose::Other, "Public Symbol Hash");
72     else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex())
73       Value = std::make_pair(StreamPurpose::Other, "Public Symbol Records");
74     else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex())
75       Value = std::make_pair(StreamPurpose::Other, "TPI Hash");
76     else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex())
77       Value = std::make_pair(StreamPurpose::Other, "TPI Aux Hash");
78     else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex())
79       Value = std::make_pair(StreamPurpose::Other, "IPI Hash");
80     else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex())
81       Value = std::make_pair(StreamPurpose::Other, "IPI Aux Hash");
82     else if (Dbi &&
83              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception))
84       Value = std::make_pair(StreamPurpose::Other, "Exception Data");
85     else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup))
86       Value = std::make_pair(StreamPurpose::Other, "Fixup Data");
87     else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO))
88       Value = std::make_pair(StreamPurpose::Other, "FPO Data");
89     else if (Dbi &&
90              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO))
91       Value = std::make_pair(StreamPurpose::Other, "New FPO Data");
92     else if (Dbi &&
93              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
94       Value = std::make_pair(StreamPurpose::Other, "Omap From Source Data");
95     else if (Dbi &&
96              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc))
97       Value = std::make_pair(StreamPurpose::Other, "Omap To Source Data");
98     else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata))
99       Value = std::make_pair(StreamPurpose::Other, "Pdata");
100     else if (Dbi &&
101              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr))
102       Value = std::make_pair(StreamPurpose::Other, "Section Header Data");
103     else if (Dbi &&
104              StreamIdx ==
105                  Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
106       Value =
107           std::make_pair(StreamPurpose::Other, "Section Header Original Data");
108     else if (Dbi &&
109              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap))
110       Value = std::make_pair(StreamPurpose::Other, "Token Rid Data");
111     else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata))
112       Value = std::make_pair(StreamPurpose::Other, "Xdata");
113     else {
114       auto ModIter = ModStreams.find(StreamIdx);
115       auto NSIter = NamedStreams.find(StreamIdx);
116       if (ModIter != ModStreams.end()) {
117         Value = std::make_pair(StreamPurpose::ModuleStream,
118                                ModIter->second.getModuleName());
119       } else if (NSIter != NamedStreams.end()) {
120         Value = std::make_pair(StreamPurpose::NamedStream, NSIter->second);
121       } else {
122         Value = std::make_pair(StreamPurpose::Other, "???");
123       }
124     }
125     Purposes[StreamIdx] = Value;
126   }
127
128   // Consume errors from missing streams.
129   if (!Dbi)
130     consumeError(Dbi.takeError());
131   if (!Tpi)
132     consumeError(Tpi.takeError());
133   if (!Ipi)
134     consumeError(Ipi.takeError());
135   if (!Info)
136     consumeError(Info.takeError());
137 }
138
139 void llvm::pdb::discoverStreamPurposes(PDBFile &File,
140                                        SmallVectorImpl<std::string> &Purposes) {
141   SmallVector<std::pair<StreamPurpose, std::string>, 24> SP;
142   discoverStreamPurposes(File, SP);
143   Purposes.reserve(SP.size());
144   for (const auto &P : SP) {
145     if (P.first == StreamPurpose::NamedStream)
146       Purposes.push_back(formatv("Named Stream \"{0}\"", P.second));
147     else if (P.first == StreamPurpose::ModuleStream)
148       Purposes.push_back(formatv("Module \"{0}\"", P.second));
149     else
150       Purposes.push_back(P.second);
151   }
152 }