167c56014SZachary Turner //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
267c56014SZachary Turner //
367c56014SZachary Turner //                     The LLVM Compiler Infrastructure
467c56014SZachary Turner //
567c56014SZachary Turner // This file is distributed under the University of Illinois Open Source
667c56014SZachary Turner // License. See LICENSE.TXT for details.
767c56014SZachary Turner //
867c56014SZachary Turner //===----------------------------------------------------------------------===//
967c56014SZachary Turner 
1067c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
1167c56014SZachary Turner #include "llvm/ADT/iterator_range.h"
12*4fcfc199SEugene Zelenko #include "llvm/DebugInfo/CodeView/CodeView.h"
13*4fcfc199SEugene Zelenko #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
1467c56014SZachary Turner #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
1567c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
1667c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/RawError.h"
1767c56014SZachary Turner #include "llvm/Support/BinaryStreamReader.h"
1867c56014SZachary Turner #include "llvm/Support/BinaryStreamRef.h"
1967c56014SZachary Turner #include "llvm/Support/Error.h"
2067c56014SZachary Turner #include <algorithm>
2167c56014SZachary Turner #include <cstdint>
2267c56014SZachary Turner 
2367c56014SZachary Turner using namespace llvm;
24c37cb0c6SZachary Turner using namespace llvm::codeview;
2567c56014SZachary Turner using namespace llvm::msf;
2667c56014SZachary Turner using namespace llvm::pdb;
2767c56014SZachary Turner 
287cc13e55SZachary Turner ModuleDebugStreamRef::ModuleDebugStreamRef(
297cc13e55SZachary Turner     const DbiModuleDescriptor &Module,
3067c56014SZachary Turner     std::unique_ptr<MappedBlockStream> Stream)
3167c56014SZachary Turner     : Mod(Module), Stream(std::move(Stream)) {}
3267c56014SZachary Turner 
337cc13e55SZachary Turner ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
3467c56014SZachary Turner 
357cc13e55SZachary Turner Error ModuleDebugStreamRef::reload() {
3667c56014SZachary Turner   BinaryStreamReader Reader(*Stream);
3767c56014SZachary Turner 
3867c56014SZachary Turner   uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
395b6e4e0aSZachary Turner   uint32_t C11Size = Mod.getC11LineInfoByteSize();
4067c56014SZachary Turner   uint32_t C13Size = Mod.getC13LineInfoByteSize();
4167c56014SZachary Turner 
4267c56014SZachary Turner   if (C11Size > 0 && C13Size > 0)
4367c56014SZachary Turner     return make_error<RawError>(raw_error_code::corrupt_file,
4467c56014SZachary Turner                                 "Module has both C11 and C13 line info");
4567c56014SZachary Turner 
4667c56014SZachary Turner   BinaryStreamRef S;
4767c56014SZachary Turner 
4867c56014SZachary Turner   if (auto EC = Reader.readInteger(Signature))
4967c56014SZachary Turner     return EC;
50fa332827SZachary Turner   if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize - 4))
51fa332827SZachary Turner     return EC;
52fa332827SZachary Turner   if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
53fa332827SZachary Turner     return EC;
54fa332827SZachary Turner   if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
5567c56014SZachary Turner     return EC;
5667c56014SZachary Turner 
57fa332827SZachary Turner   BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
58fa332827SZachary Turner   if (auto EC =
59fa332827SZachary Turner           SymbolReader.readArray(SymbolArray, SymbolReader.bytesRemaining()))
6067c56014SZachary Turner     return EC;
6167c56014SZachary Turner 
62fa332827SZachary Turner   BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
6392dcdda6SZachary Turner   if (auto EC = SubsectionsReader.readArray(Subsections,
6492dcdda6SZachary Turner                                             SubsectionsReader.bytesRemaining()))
6567c56014SZachary Turner     return EC;
6667c56014SZachary Turner 
6767c56014SZachary Turner   uint32_t GlobalRefsSize;
6867c56014SZachary Turner   if (auto EC = Reader.readInteger(GlobalRefsSize))
6967c56014SZachary Turner     return EC;
70fa332827SZachary Turner   if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
7167c56014SZachary Turner     return EC;
7267c56014SZachary Turner   if (Reader.bytesRemaining() > 0)
7367c56014SZachary Turner     return make_error<RawError>(raw_error_code::corrupt_file,
7467c56014SZachary Turner                                 "Unexpected bytes in module stream.");
7567c56014SZachary Turner 
7667c56014SZachary Turner   return Error::success();
7767c56014SZachary Turner }
7867c56014SZachary Turner 
79fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
80fa332827SZachary Turner   return SymbolsSubstream;
81fa332827SZachary Turner }
82fa332827SZachary Turner 
83fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
84fa332827SZachary Turner   return C11LinesSubstream;
85fa332827SZachary Turner }
86fa332827SZachary Turner 
87fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
88fa332827SZachary Turner   return C13LinesSubstream;
89fa332827SZachary Turner }
90fa332827SZachary Turner 
91fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
92fa332827SZachary Turner   return GlobalRefsSubstream;
93fa332827SZachary Turner }
94fa332827SZachary Turner 
9567c56014SZachary Turner iterator_range<codeview::CVSymbolArray::Iterator>
967cc13e55SZachary Turner ModuleDebugStreamRef::symbols(bool *HadError) const {
97fa332827SZachary Turner   return make_range(SymbolArray.begin(HadError), SymbolArray.end());
9867c56014SZachary Turner }
9967c56014SZachary Turner 
100*4fcfc199SEugene Zelenko iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
10192dcdda6SZachary Turner ModuleDebugStreamRef::subsections() const {
10292dcdda6SZachary Turner   return make_range(Subsections.begin(), Subsections.end());
10367c56014SZachary Turner }
10467c56014SZachary Turner 
10592dcdda6SZachary Turner bool ModuleDebugStreamRef::hasDebugSubsections() const {
106fa332827SZachary Turner   return !C13LinesSubstream.empty();
10767c56014SZachary Turner }
10867c56014SZachary Turner 
1097cc13e55SZachary Turner Error ModuleDebugStreamRef::commit() { return Error::success(); }
11092dcdda6SZachary Turner 
11192dcdda6SZachary Turner Expected<codeview::DebugChecksumsSubsectionRef>
11292dcdda6SZachary Turner ModuleDebugStreamRef::findChecksumsSubsection() const {
113349c18f8SZachary Turner   codeview::DebugChecksumsSubsectionRef Result;
11492dcdda6SZachary Turner   for (const auto &SS : subsections()) {
11592dcdda6SZachary Turner     if (SS.kind() != DebugSubsectionKind::FileChecksums)
11692dcdda6SZachary Turner       continue;
11792dcdda6SZachary Turner 
11892dcdda6SZachary Turner     if (auto EC = Result.initialize(SS.getRecordData()))
11992dcdda6SZachary Turner       return std::move(EC);
12092dcdda6SZachary Turner     return Result;
12192dcdda6SZachary Turner   }
122349c18f8SZachary Turner   return Result;
12392dcdda6SZachary Turner }
124