1 //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
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 "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
13 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
14 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
15 #include "llvm/DebugInfo/PDB/Native/RawError.h"
16 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
17 #include "llvm/Support/BinaryStreamReader.h"
18 #include "llvm/Support/BinaryStreamRef.h"
19 #include "llvm/Support/Error.h"
20 #include <algorithm>
21 #include <cstdint>
22 
23 using namespace llvm;
24 using namespace llvm::codeview;
25 using namespace llvm::msf;
26 using namespace llvm::pdb;
27 
28 ModuleDebugStream::ModuleDebugStream(const DbiModuleDescriptor &Module,
29                                      std::unique_ptr<MappedBlockStream> Stream)
30     : Mod(Module), Stream(std::move(Stream)) {}
31 
32 ModuleDebugStream::~ModuleDebugStream() = default;
33 
34 Error ModuleDebugStream::reload() {
35   BinaryStreamReader Reader(*Stream);
36 
37   uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
38   uint32_t C11Size = Mod.getLineInfoByteSize();
39   uint32_t C13Size = Mod.getC13LineInfoByteSize();
40 
41   if (C11Size > 0 && C13Size > 0)
42     return make_error<RawError>(raw_error_code::corrupt_file,
43                                 "Module has both C11 and C13 line info");
44 
45   BinaryStreamRef S;
46 
47   if (auto EC = Reader.readInteger(Signature))
48     return EC;
49   if (auto EC = Reader.readArray(SymbolsSubstream, SymbolSize - 4))
50     return EC;
51 
52   if (auto EC = Reader.readStreamRef(LinesSubstream, C11Size))
53     return EC;
54   if (auto EC = Reader.readStreamRef(C13LinesSubstream, C13Size))
55     return EC;
56 
57   BinaryStreamReader LineReader(C13LinesSubstream);
58   if (auto EC =
59           LineReader.readArray(LinesAndChecksums, LineReader.bytesRemaining()))
60     return EC;
61 
62   uint32_t GlobalRefsSize;
63   if (auto EC = Reader.readInteger(GlobalRefsSize))
64     return EC;
65   if (auto EC = Reader.readStreamRef(GlobalRefsSubstream, GlobalRefsSize))
66     return EC;
67   if (Reader.bytesRemaining() > 0)
68     return make_error<RawError>(raw_error_code::corrupt_file,
69                                 "Unexpected bytes in module stream.");
70 
71   return Error::success();
72 }
73 
74 iterator_range<codeview::CVSymbolArray::Iterator>
75 ModuleDebugStream::symbols(bool *HadError) const {
76   return make_range(SymbolsSubstream.begin(HadError), SymbolsSubstream.end());
77 }
78 
79 llvm::iterator_range<ModuleDebugStream::LinesAndChecksumsIterator>
80 ModuleDebugStream::linesAndChecksums() const {
81   return make_range(LinesAndChecksums.begin(), LinesAndChecksums.end());
82 }
83 
84 bool ModuleDebugStream::hasLineInfo() const {
85   return C13LinesSubstream.getLength() > 0;
86 }
87 
88 Error ModuleDebugStream::commit() { return Error::success(); }
89