blob: 4186f2eb6ba0167d2281ab2b3458ebaa577206de [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 Turnerc37cb0c2017-04-27 16:12:16 +000024using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000025using namespace llvm::msf;
Zachary Turner06c2b4b2016-05-09 17:45:21 +000026using namespace llvm::pdb;
27
Zachary Turner7cc13e52017-05-01 16:46:39 +000028ModuleDebugStreamRef::ModuleDebugStreamRef(
29 const DbiModuleDescriptor &Module,
30 std::unique_ptr<MappedBlockStream> Stream)
Zachary Turnera1657a92016-06-08 17:26:39 +000031 : Mod(Module), Stream(std::move(Stream)) {}
Zachary Turner06c2b4b2016-05-09 17:45:21 +000032
Zachary Turner7cc13e52017-05-01 16:46:39 +000033ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
Zachary Turner06c2b4b2016-05-09 17:45:21 +000034
Zachary Turner7cc13e52017-05-01 16:46:39 +000035Error ModuleDebugStreamRef::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +000036 BinaryStreamReader Reader(*Stream);
Zachary Turner06c2b4b2016-05-09 17:45:21 +000037
38 uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
Zachary Turner5b6e4e02017-04-29 01:13:21 +000039 uint32_t C11Size = Mod.getC11LineInfoByteSize();
Zachary Turner06c2b4b2016-05-09 17:45:21 +000040 uint32_t C13Size = Mod.getC13LineInfoByteSize();
41
42 if (C11Size > 0 && C13Size > 0)
Eugene Zelenko570e39a2016-11-23 23:16:32 +000043 return make_error<RawError>(raw_error_code::corrupt_file,
44 "Module has both C11 and C13 line info");
Zachary Turner06c2b4b2016-05-09 17:45:21 +000045
Zachary Turner120faca2017-02-27 22:11:43 +000046 BinaryStreamRef S;
Zachary Turner1de49c92016-05-27 18:47:20 +000047
Zachary Turner695ed562017-02-28 00:04:07 +000048 if (auto EC = Reader.readInteger(Signature))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000049 return EC;
Zachary Turner1de49c92016-05-27 18:47:20 +000050 if (auto EC = Reader.readArray(SymbolsSubstream, SymbolSize - 4))
51 return EC;
52
Zachary Turner7cc13e52017-05-01 16:46:39 +000053 if (auto EC = Reader.readStreamRef(C11LinesSubstream, C11Size))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000054 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000055 if (auto EC = Reader.readStreamRef(C13LinesSubstream, C13Size))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000056 return EC;
Zachary Turner7eb6d352016-06-02 20:11:22 +000057
Zachary Turner92dcdda2017-06-02 19:49:14 +000058 BinaryStreamReader SubsectionsReader(C13LinesSubstream);
59 if (auto EC = SubsectionsReader.readArray(Subsections,
60 SubsectionsReader.bytesRemaining()))
Zachary Turner93839cb2016-06-02 05:07:49 +000061 return EC;
Zachary Turner06c2b4b2016-05-09 17:45:21 +000062
63 uint32_t GlobalRefsSize;
Zachary Turner695ed562017-02-28 00:04:07 +000064 if (auto EC = Reader.readInteger(GlobalRefsSize))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000065 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000066 if (auto EC = Reader.readStreamRef(GlobalRefsSubstream, GlobalRefsSize))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000067 return EC;
68 if (Reader.bytesRemaining() > 0)
Eugene Zelenko570e39a2016-11-23 23:16:32 +000069 return make_error<RawError>(raw_error_code::corrupt_file,
70 "Unexpected bytes in module stream.");
Zachary Turner06c2b4b2016-05-09 17:45:21 +000071
72 return Error::success();
73}
74
Zachary Turner0d43c1c2016-05-28 05:21:57 +000075iterator_range<codeview::CVSymbolArray::Iterator>
Zachary Turner7cc13e52017-05-01 16:46:39 +000076ModuleDebugStreamRef::symbols(bool *HadError) const {
Eugene Zelenko570e39a2016-11-23 23:16:32 +000077 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 Turner92dcdda2017-06-02 19:49:14 +000080llvm::iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
81ModuleDebugStreamRef::subsections() const {
82 return make_range(Subsections.begin(), Subsections.end());
Zachary Turner7eb6d352016-06-02 20:11:22 +000083}
Zachary Turner8848a7a2016-07-06 18:05:57 +000084
Zachary Turner92dcdda2017-06-02 19:49:14 +000085bool ModuleDebugStreamRef::hasDebugSubsections() const {
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000086 return C13LinesSubstream.getLength() > 0;
Zachary Turneree3b9c22017-04-25 20:22:02 +000087}
88
Zachary Turner7cc13e52017-05-01 16:46:39 +000089Error ModuleDebugStreamRef::commit() { return Error::success(); }
Zachary Turner92dcdda2017-06-02 19:49:14 +000090
91Expected<codeview::DebugChecksumsSubsectionRef>
92ModuleDebugStreamRef::findChecksumsSubsection() const {
Zachary Turner349c18f2017-06-05 21:40:33 +000093 codeview::DebugChecksumsSubsectionRef Result;
Zachary Turner92dcdda2017-06-02 19:49:14 +000094 for (const auto &SS : subsections()) {
95 if (SS.kind() != DebugSubsectionKind::FileChecksums)
96 continue;
97
Zachary Turner92dcdda2017-06-02 19:49:14 +000098 if (auto EC = Result.initialize(SS.getRecordData()))
99 return std::move(EC);
100 return Result;
101 }
Zachary Turner349c18f2017-06-05 21:40:33 +0000102 return Result;
Zachary Turner92dcdda2017-06-02 19:49:14 +0000103}