1 //===- ModStream.cpp - PDB Module Info Stream Access ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/PDB/Native/ModStream.h"
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
13 #include "llvm/DebugInfo/PDB/Native/ModInfo.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"
24 using namespace llvm::msf;
25 using namespace llvm::pdb;
27 ModStream::ModStream(const ModInfo &Module,
28 std::unique_ptr<MappedBlockStream> Stream)
29 : Mod(Module), Stream(std::move(Stream)) {}
31 ModStream::~ModStream() = default;
33 Error ModStream::reload() {
34 BinaryStreamReader Reader(*Stream);
36 uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
37 uint32_t C11Size = Mod.getLineInfoByteSize();
38 uint32_t C13Size = Mod.getC13LineInfoByteSize();
40 if (C11Size > 0 && C13Size > 0)
41 return make_error<RawError>(raw_error_code::corrupt_file,
42 "Module has both C11 and C13 line info");
46 if (auto EC = Reader.readInteger(Signature))
48 if (auto EC = Reader.readArray(SymbolsSubstream, SymbolSize - 4))
51 if (auto EC = Reader.readStreamRef(LinesSubstream, C11Size))
53 if (auto EC = Reader.readStreamRef(C13LinesSubstream, C13Size))
56 BinaryStreamReader LineReader(C13LinesSubstream);
57 if (auto EC = LineReader.readArray(LineInfo, LineReader.bytesRemaining()))
60 uint32_t GlobalRefsSize;
61 if (auto EC = Reader.readInteger(GlobalRefsSize))
63 if (auto EC = Reader.readStreamRef(GlobalRefsSubstream, GlobalRefsSize))
65 if (Reader.bytesRemaining() > 0)
66 return make_error<RawError>(raw_error_code::corrupt_file,
67 "Unexpected bytes in module stream.");
69 return Error::success();
72 iterator_range<codeview::CVSymbolArray::Iterator>
73 ModStream::symbols(bool *HadError) const {
74 // It's OK if the stream is empty.
75 if (SymbolsSubstream.getUnderlyingStream().getLength() == 0)
76 return make_range(SymbolsSubstream.end(), SymbolsSubstream.end());
77 return make_range(SymbolsSubstream.begin(HadError), SymbolsSubstream.end());
80 iterator_range<codeview::ModuleSubstreamArray::Iterator>
81 ModStream::lines(bool *HadError) const {
82 return make_range(LineInfo.begin(HadError), LineInfo.end());
85 bool ModStream::hasLineInfo() const {
86 return C13LinesSubstream.getLength() > 0 || LinesSubstream.getLength() > 0;
89 Error ModStream::commit() { return Error::success(); }