blob: 8caa4b85772c52c6cadd1527e958c255d710a49d [file] [log] [blame]
Zachary Turner67c56012017-04-27 16:11:19 +00001//===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
Zachary Turner06c2b4b2016-05-09 17:45:21 +00002//
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
Zachary Turner67c56012017-04-27 16:11:19 +000010#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000011#include "llvm/ADT/iterator_range.h"
12#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Zachary Turner67c56012017-04-27 16:11:19 +000013#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000014#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
15#include "llvm/DebugInfo/PDB/Native/RawError.h"
16#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000017#include "llvm/Support/BinaryStreamReader.h"
18#include "llvm/Support/BinaryStreamRef.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 Turner67c56012017-04-27 16:11:19 +000027ModuleDebugStream::ModuleDebugStream(const DbiModuleDescriptor &Module,
28 std::unique_ptr<MappedBlockStream> Stream)
Zachary Turnera1657a92016-06-08 17:26:39 +000029 : Mod(Module), Stream(std::move(Stream)) {}
Zachary Turner06c2b4b2016-05-09 17:45:21 +000030
Zachary Turner67c56012017-04-27 16:11:19 +000031ModuleDebugStream::~ModuleDebugStream() = default;
Zachary Turner06c2b4b2016-05-09 17:45:21 +000032
Zachary Turner67c56012017-04-27 16:11:19 +000033Error ModuleDebugStream::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +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 Turner120faca2017-02-27 22:11:43 +000044 BinaryStreamRef S;
Zachary Turner1de49c92016-05-27 18:47:20 +000045
Zachary Turner695ed562017-02-28 00:04:07 +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 Turner120faca2017-02-27 22:11:43 +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 Turner695ed562017-02-28 00:04:07 +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>
Zachary Turner67c56012017-04-27 16:11:19 +000073ModuleDebugStream::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 Turner67c56012017-04-27 16:11:19 +000080iterator_range<codeview::ModuleDebugFragmentArray::Iterator>
81ModuleDebugStream::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
Zachary Turner67c56012017-04-27 16:11:19 +000085bool ModuleDebugStream::hasLineInfo() const {
Zachary Turneree3b9c22017-04-25 20:22:02 +000086 return C13LinesSubstream.getLength() > 0 || LinesSubstream.getLength() > 0;
87}
88
Zachary Turner67c56012017-04-27 16:11:19 +000089Error ModuleDebugStream::commit() { return Error::success(); }