167c56014SZachary Turner //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
267c56014SZachary Turner //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
667c56014SZachary Turner //
767c56014SZachary Turner //===----------------------------------------------------------------------===//
867c56014SZachary Turner 
967c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
1067c56014SZachary Turner #include "llvm/ADT/iterator_range.h"
114fcfc199SEugene Zelenko #include "llvm/DebugInfo/CodeView/CodeView.h"
124fcfc199SEugene Zelenko #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
13bb3d7e56SZachary Turner #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
1467c56014SZachary Turner #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
15bb3d7e56SZachary Turner #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
1667c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
1767c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/RawError.h"
1867c56014SZachary Turner #include "llvm/Support/BinaryStreamReader.h"
1967c56014SZachary Turner #include "llvm/Support/BinaryStreamRef.h"
2067c56014SZachary Turner #include "llvm/Support/Error.h"
2167c56014SZachary Turner #include <algorithm>
2267c56014SZachary Turner #include <cstdint>
2367c56014SZachary Turner 
2467c56014SZachary Turner using namespace llvm;
25c37cb0c6SZachary Turner using namespace llvm::codeview;
2667c56014SZachary Turner using namespace llvm::msf;
2767c56014SZachary Turner using namespace llvm::pdb;
2867c56014SZachary Turner 
297cc13e55SZachary Turner ModuleDebugStreamRef::ModuleDebugStreamRef(
307cc13e55SZachary Turner     const DbiModuleDescriptor &Module,
3167c56014SZachary Turner     std::unique_ptr<MappedBlockStream> Stream)
3267c56014SZachary Turner     : Mod(Module), Stream(std::move(Stream)) {}
3367c56014SZachary Turner 
347cc13e55SZachary Turner ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
3567c56014SZachary Turner 
367cc13e55SZachary Turner Error ModuleDebugStreamRef::reload() {
3767c56014SZachary Turner   BinaryStreamReader Reader(*Stream);
3867c56014SZachary Turner 
3967c56014SZachary Turner   uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
405b6e4e0aSZachary Turner   uint32_t C11Size = Mod.getC11LineInfoByteSize();
4167c56014SZachary Turner   uint32_t C13Size = Mod.getC13LineInfoByteSize();
4267c56014SZachary Turner 
4367c56014SZachary Turner   if (C11Size > 0 && C13Size > 0)
4467c56014SZachary Turner     return make_error<RawError>(raw_error_code::corrupt_file,
4567c56014SZachary Turner                                 "Module has both C11 and C13 line info");
4667c56014SZachary Turner 
4767c56014SZachary Turner   BinaryStreamRef S;
4867c56014SZachary Turner 
4967c56014SZachary Turner   if (auto EC = Reader.readInteger(Signature))
5067c56014SZachary Turner     return EC;
51579264bdSZachary Turner   Reader.setOffset(0);
52579264bdSZachary Turner   if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
53fa332827SZachary Turner     return EC;
54fa332827SZachary Turner   if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
55fa332827SZachary Turner     return EC;
56fa332827SZachary Turner   if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
5767c56014SZachary Turner     return EC;
5867c56014SZachary Turner 
59fa332827SZachary Turner   BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
60579264bdSZachary Turner   if (auto EC = SymbolReader.readArray(
61579264bdSZachary Turner           SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
6267c56014SZachary Turner     return EC;
6367c56014SZachary Turner 
64fa332827SZachary Turner   BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
6592dcdda6SZachary Turner   if (auto EC = SubsectionsReader.readArray(Subsections,
6692dcdda6SZachary Turner                                             SubsectionsReader.bytesRemaining()))
6767c56014SZachary Turner     return EC;
6867c56014SZachary Turner 
6967c56014SZachary Turner   uint32_t GlobalRefsSize;
7067c56014SZachary Turner   if (auto EC = Reader.readInteger(GlobalRefsSize))
7167c56014SZachary Turner     return EC;
72fa332827SZachary Turner   if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
7367c56014SZachary Turner     return EC;
7467c56014SZachary Turner   if (Reader.bytesRemaining() > 0)
7567c56014SZachary Turner     return make_error<RawError>(raw_error_code::corrupt_file,
7667c56014SZachary Turner                                 "Unexpected bytes in module stream.");
7767c56014SZachary Turner 
7867c56014SZachary Turner   return Error::success();
7967c56014SZachary Turner }
8067c56014SZachary Turner 
81bb3d7e56SZachary Turner const codeview::CVSymbolArray
82bb3d7e56SZachary Turner ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {
83bb3d7e56SZachary Turner   return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
84bb3d7e56SZachary Turner }
85bb3d7e56SZachary Turner 
86fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
87fa332827SZachary Turner   return SymbolsSubstream;
88fa332827SZachary Turner }
89fa332827SZachary Turner 
90fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
91fa332827SZachary Turner   return C11LinesSubstream;
92fa332827SZachary Turner }
93fa332827SZachary Turner 
94fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
95fa332827SZachary Turner   return C13LinesSubstream;
96fa332827SZachary Turner }
97fa332827SZachary Turner 
98fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
99fa332827SZachary Turner   return GlobalRefsSubstream;
100fa332827SZachary Turner }
101fa332827SZachary Turner 
10267c56014SZachary Turner iterator_range<codeview::CVSymbolArray::Iterator>
1037cc13e55SZachary Turner ModuleDebugStreamRef::symbols(bool *HadError) const {
104fa332827SZachary Turner   return make_range(SymbolArray.begin(HadError), SymbolArray.end());
10567c56014SZachary Turner }
10667c56014SZachary Turner 
10794926a6dSZachary Turner CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
108579264bdSZachary Turner   auto Iter = SymbolArray.at(Offset);
10994926a6dSZachary Turner   assert(Iter != SymbolArray.end());
11094926a6dSZachary Turner   return *Iter;
11194926a6dSZachary Turner }
11294926a6dSZachary Turner 
1134fcfc199SEugene Zelenko iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
11492dcdda6SZachary Turner ModuleDebugStreamRef::subsections() const {
11592dcdda6SZachary Turner   return make_range(Subsections.begin(), Subsections.end());
11667c56014SZachary Turner }
11767c56014SZachary Turner 
11892dcdda6SZachary Turner bool ModuleDebugStreamRef::hasDebugSubsections() const {
119fa332827SZachary Turner   return !C13LinesSubstream.empty();
12067c56014SZachary Turner }
12167c56014SZachary Turner 
1227cc13e55SZachary Turner Error ModuleDebugStreamRef::commit() { return Error::success(); }
12392dcdda6SZachary Turner 
12492dcdda6SZachary Turner Expected<codeview::DebugChecksumsSubsectionRef>
12592dcdda6SZachary Turner ModuleDebugStreamRef::findChecksumsSubsection() const {
126349c18f8SZachary Turner   codeview::DebugChecksumsSubsectionRef Result;
12792dcdda6SZachary Turner   for (const auto &SS : subsections()) {
12892dcdda6SZachary Turner     if (SS.kind() != DebugSubsectionKind::FileChecksums)
12992dcdda6SZachary Turner       continue;
13092dcdda6SZachary Turner 
13192dcdda6SZachary Turner     if (auto EC = Result.initialize(SS.getRecordData()))
13292dcdda6SZachary Turner       return std::move(EC);
13392dcdda6SZachary Turner     return Result;
13492dcdda6SZachary Turner   }
135349c18f8SZachary Turner   return Result;
13692dcdda6SZachary Turner }
137