blob: df9ec818a1d968c65973d691c485c9a09235111c [file] [log] [blame]
Zachary Turner06c2b4b2016-05-09 17:45:21 +00001//===- ModStream.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
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/ModStream.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000011#include "llvm/ADT/iterator_range.h"
12#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Zachary Turnerd2684b72017-02-25 00:33:34 +000013#include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
14#include "llvm/DebugInfo/MSF/BinaryStreamRef.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000015#include "llvm/DebugInfo/PDB/Native/ModInfo.h"
16#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
17#include "llvm/DebugInfo/PDB/Native/RawError.h"
18#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000019#include "llvm/Support/Error.h"
20#include <algorithm>
21#include <cstdint>
Zachary Turner06c2b4b2016-05-09 17:45:21 +000022
23using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000024using namespace llvm::msf;
Zachary Turner06c2b4b2016-05-09 17:45:21 +000025using namespace llvm::pdb;
26
Zachary Turnera1657a92016-06-08 17:26:39 +000027ModStream::ModStream(const ModInfo &Module,
28 std::unique_ptr<MappedBlockStream> Stream)
29 : Mod(Module), Stream(std::move(Stream)) {}
Zachary Turner06c2b4b2016-05-09 17:45:21 +000030
Eugene Zelenko570e39a2016-11-23 23:16:32 +000031ModStream::~ModStream() = default;
Zachary Turner06c2b4b2016-05-09 17:45:21 +000032
33Error ModStream::reload() {
Zachary Turneraf299ea2017-02-25 00:44:30 +000034 BinaryStreamReader Reader(*Stream);
Zachary Turner06c2b4b2016-05-09 17:45:21 +000035
36 uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
37 uint32_t C11Size = Mod.getLineInfoByteSize();
38 uint32_t C13Size = Mod.getC13LineInfoByteSize();
39
40 if (C11Size > 0 && C13Size > 0)
Eugene Zelenko570e39a2016-11-23 23:16:32 +000041 return make_error<RawError>(raw_error_code::corrupt_file,
42 "Module has both C11 and C13 line info");
Zachary Turner06c2b4b2016-05-09 17:45:21 +000043
Zachary Turneraf299ea2017-02-25 00:44:30 +000044 BinaryStreamRef S;
Zachary Turner1de49c92016-05-27 18:47:20 +000045
Zachary Turneraf299ea2017-02-25 00:44:30 +000046 if (auto EC = Reader.readInteger(Signature))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000047 return EC;
Zachary Turner1de49c92016-05-27 18:47:20 +000048 if (auto EC = Reader.readArray(SymbolsSubstream, SymbolSize - 4))
49 return EC;
50
Zachary Turner8dbe3622016-05-27 01:54:44 +000051 if (auto EC = Reader.readStreamRef(LinesSubstream, C11Size))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000052 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000053 if (auto EC = Reader.readStreamRef(C13LinesSubstream, C13Size))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000054 return EC;
Zachary Turner7eb6d352016-06-02 20:11:22 +000055
Zachary Turneraf299ea2017-02-25 00:44:30 +000056 BinaryStreamReader LineReader(C13LinesSubstream);
Zachary Turner7eb6d352016-06-02 20:11:22 +000057 if (auto EC = LineReader.readArray(LineInfo, LineReader.bytesRemaining()))
Zachary Turner93839cb2016-06-02 05:07:49 +000058 return EC;
Zachary Turner06c2b4b2016-05-09 17:45:21 +000059
60 uint32_t GlobalRefsSize;
Zachary Turneraf299ea2017-02-25 00:44:30 +000061 if (auto EC = Reader.readInteger(GlobalRefsSize))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000062 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000063 if (auto EC = Reader.readStreamRef(GlobalRefsSubstream, GlobalRefsSize))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000064 return EC;
65 if (Reader.bytesRemaining() > 0)
Eugene Zelenko570e39a2016-11-23 23:16:32 +000066 return make_error<RawError>(raw_error_code::corrupt_file,
67 "Unexpected bytes in module stream.");
Zachary Turner06c2b4b2016-05-09 17:45:21 +000068
69 return Error::success();
70}
71
Zachary Turner0d43c1c2016-05-28 05:21:57 +000072iterator_range<codeview::CVSymbolArray::Iterator>
73ModStream::symbols(bool *HadError) const {
Reid Kleckner64b16172016-07-01 00:37:49 +000074 // It's OK if the stream is empty.
75 if (SymbolsSubstream.getUnderlyingStream().getLength() == 0)
Eugene Zelenko570e39a2016-11-23 23:16:32 +000076 return make_range(SymbolsSubstream.end(), SymbolsSubstream.end());
77 return make_range(SymbolsSubstream.begin(HadError), SymbolsSubstream.end());
Zachary Turner06c2b4b2016-05-09 17:45:21 +000078}
Zachary Turner7eb6d352016-06-02 20:11:22 +000079
Zachary Turnera96cce62016-06-03 03:25:59 +000080iterator_range<codeview::ModuleSubstreamArray::Iterator>
Zachary Turner7eb6d352016-06-02 20:11:22 +000081ModStream::lines(bool *HadError) const {
Eugene Zelenko570e39a2016-11-23 23:16:32 +000082 return make_range(LineInfo.begin(HadError), LineInfo.end());
Zachary Turner7eb6d352016-06-02 20:11:22 +000083}
Zachary Turner8848a7a2016-07-06 18:05:57 +000084
85Error ModStream::commit() { return Error::success(); }