167c56014SZachary Turner //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
267c56014SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler 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"
174aeea4ccSAlexandre Ganea #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
1867c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/RawError.h"
1967c56014SZachary Turner #include "llvm/Support/BinaryStreamReader.h"
2067c56014SZachary Turner #include "llvm/Support/BinaryStreamRef.h"
2167c56014SZachary Turner #include "llvm/Support/Error.h"
2267c56014SZachary Turner #include <algorithm>
2367c56014SZachary Turner #include <cstdint>
2467c56014SZachary Turner 
2567c56014SZachary Turner using namespace llvm;
26c37cb0c6SZachary Turner using namespace llvm::codeview;
2767c56014SZachary Turner using namespace llvm::msf;
2867c56014SZachary Turner using namespace llvm::pdb;
2967c56014SZachary Turner 
307cc13e55SZachary Turner ModuleDebugStreamRef::ModuleDebugStreamRef(
317cc13e55SZachary Turner     const DbiModuleDescriptor &Module,
3267c56014SZachary Turner     std::unique_ptr<MappedBlockStream> Stream)
3367c56014SZachary Turner     : Mod(Module), Stream(std::move(Stream)) {}
3467c56014SZachary Turner 
357cc13e55SZachary Turner ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
3667c56014SZachary Turner 
377cc13e55SZachary Turner Error ModuleDebugStreamRef::reload() {
3867c56014SZachary Turner   BinaryStreamReader Reader(*Stream);
3967c56014SZachary Turner 
404aeea4ccSAlexandre Ganea   if (Mod.getModuleStreamIndex() != llvm::pdb::kInvalidStreamIndex) {
414aeea4ccSAlexandre Ganea     if (Error E = reloadSerialize(Reader))
424aeea4ccSAlexandre Ganea       return E;
434aeea4ccSAlexandre Ganea   }
444aeea4ccSAlexandre Ganea   if (Reader.bytesRemaining() > 0)
454aeea4ccSAlexandre Ganea     return make_error<RawError>(raw_error_code::corrupt_file,
464aeea4ccSAlexandre Ganea                                 "Unexpected bytes in module stream.");
474aeea4ccSAlexandre Ganea   return Error::success();
484aeea4ccSAlexandre Ganea }
494aeea4ccSAlexandre Ganea 
504aeea4ccSAlexandre Ganea Error ModuleDebugStreamRef::reloadSerialize(BinaryStreamReader &Reader) {
5167c56014SZachary Turner   uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
525b6e4e0aSZachary Turner   uint32_t C11Size = Mod.getC11LineInfoByteSize();
5367c56014SZachary Turner   uint32_t C13Size = Mod.getC13LineInfoByteSize();
5467c56014SZachary Turner 
5567c56014SZachary Turner   if (C11Size > 0 && C13Size > 0)
5667c56014SZachary Turner     return make_error<RawError>(raw_error_code::corrupt_file,
5767c56014SZachary Turner                                 "Module has both C11 and C13 line info");
5867c56014SZachary Turner 
5967c56014SZachary Turner   BinaryStreamRef S;
6067c56014SZachary Turner 
6167c56014SZachary Turner   if (auto EC = Reader.readInteger(Signature))
6267c56014SZachary Turner     return EC;
63579264bdSZachary Turner   Reader.setOffset(0);
64579264bdSZachary Turner   if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
65fa332827SZachary Turner     return EC;
66fa332827SZachary Turner   if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
67fa332827SZachary Turner     return EC;
68fa332827SZachary Turner   if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
6967c56014SZachary Turner     return EC;
7067c56014SZachary Turner 
71fa332827SZachary Turner   BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
72579264bdSZachary Turner   if (auto EC = SymbolReader.readArray(
73579264bdSZachary Turner           SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
7467c56014SZachary Turner     return EC;
7567c56014SZachary Turner 
76fa332827SZachary Turner   BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
7792dcdda6SZachary Turner   if (auto EC = SubsectionsReader.readArray(Subsections,
7892dcdda6SZachary Turner                                             SubsectionsReader.bytesRemaining()))
7967c56014SZachary Turner     return EC;
8067c56014SZachary Turner 
8167c56014SZachary Turner   uint32_t GlobalRefsSize;
8267c56014SZachary Turner   if (auto EC = Reader.readInteger(GlobalRefsSize))
8367c56014SZachary Turner     return EC;
84fa332827SZachary Turner   if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
8567c56014SZachary Turner     return EC;
8667c56014SZachary Turner   return Error::success();
8767c56014SZachary Turner }
8867c56014SZachary Turner 
89bb3d7e56SZachary Turner const codeview::CVSymbolArray
90bb3d7e56SZachary Turner ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {
91bb3d7e56SZachary Turner   return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
92bb3d7e56SZachary Turner }
93bb3d7e56SZachary Turner 
94fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
95fa332827SZachary Turner   return SymbolsSubstream;
96fa332827SZachary Turner }
97fa332827SZachary Turner 
98fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
99fa332827SZachary Turner   return C11LinesSubstream;
100fa332827SZachary Turner }
101fa332827SZachary Turner 
102fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
103fa332827SZachary Turner   return C13LinesSubstream;
104fa332827SZachary Turner }
105fa332827SZachary Turner 
106fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
107fa332827SZachary Turner   return GlobalRefsSubstream;
108fa332827SZachary Turner }
109fa332827SZachary Turner 
11067c56014SZachary Turner iterator_range<codeview::CVSymbolArray::Iterator>
1117cc13e55SZachary Turner ModuleDebugStreamRef::symbols(bool *HadError) const {
112fa332827SZachary Turner   return make_range(SymbolArray.begin(HadError), SymbolArray.end());
11367c56014SZachary Turner }
11467c56014SZachary Turner 
11594926a6dSZachary Turner CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
116579264bdSZachary Turner   auto Iter = SymbolArray.at(Offset);
11794926a6dSZachary Turner   assert(Iter != SymbolArray.end());
11894926a6dSZachary Turner   return *Iter;
11994926a6dSZachary Turner }
12094926a6dSZachary Turner 
1214fcfc199SEugene Zelenko iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
12292dcdda6SZachary Turner ModuleDebugStreamRef::subsections() const {
12392dcdda6SZachary Turner   return make_range(Subsections.begin(), Subsections.end());
12467c56014SZachary Turner }
12567c56014SZachary Turner 
12692dcdda6SZachary Turner bool ModuleDebugStreamRef::hasDebugSubsections() const {
127fa332827SZachary Turner   return !C13LinesSubstream.empty();
12867c56014SZachary Turner }
12967c56014SZachary Turner 
1307cc13e55SZachary Turner Error ModuleDebugStreamRef::commit() { return Error::success(); }
13192dcdda6SZachary Turner 
13292dcdda6SZachary Turner Expected<codeview::DebugChecksumsSubsectionRef>
13392dcdda6SZachary Turner ModuleDebugStreamRef::findChecksumsSubsection() const {
134349c18f8SZachary Turner   codeview::DebugChecksumsSubsectionRef Result;
13592dcdda6SZachary Turner   for (const auto &SS : subsections()) {
13692dcdda6SZachary Turner     if (SS.kind() != DebugSubsectionKind::FileChecksums)
13792dcdda6SZachary Turner       continue;
13892dcdda6SZachary Turner 
13992dcdda6SZachary Turner     if (auto EC = Result.initialize(SS.getRecordData()))
140*c55cf4afSBill Wendling       return std::move(EC);
14192dcdda6SZachary Turner     return Result;
14292dcdda6SZachary Turner   }
143349c18f8SZachary Turner   return Result;
14492dcdda6SZachary Turner }
145