blob: 2e1f61c7a25dc35cbf3161503fb19446849a9dd0 [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"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000012#include "llvm/DebugInfo/CodeView/CodeView.h"
13#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000014#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Zachary Turner67c56012017-04-27 16:11:19 +000015#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000016#include "llvm/DebugInfo/PDB/Native/RawError.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 Turnerfa332822017-06-23 23:08:57 +000050 if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize - 4))
51 return EC;
52 if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
53 return EC;
54 if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
Zachary Turner1de49c92016-05-27 18:47:20 +000055 return EC;
56
Zachary Turnerfa332822017-06-23 23:08:57 +000057 BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
58 if (auto EC =
59 SymbolReader.readArray(SymbolArray, SymbolReader.bytesRemaining()))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000060 return EC;
Zachary Turner7eb6d352016-06-02 20:11:22 +000061
Zachary Turnerfa332822017-06-23 23:08:57 +000062 BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
Zachary Turner92dcdda2017-06-02 19:49:14 +000063 if (auto EC = SubsectionsReader.readArray(Subsections,
64 SubsectionsReader.bytesRemaining()))
Zachary Turner93839cb2016-06-02 05:07:49 +000065 return EC;
Zachary Turner06c2b4b2016-05-09 17:45:21 +000066
67 uint32_t GlobalRefsSize;
Zachary Turner695ed562017-02-28 00:04:07 +000068 if (auto EC = Reader.readInteger(GlobalRefsSize))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000069 return EC;
Zachary Turnerfa332822017-06-23 23:08:57 +000070 if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
Zachary Turner06c2b4b2016-05-09 17:45:21 +000071 return EC;
72 if (Reader.bytesRemaining() > 0)
Eugene Zelenko570e39a2016-11-23 23:16:32 +000073 return make_error<RawError>(raw_error_code::corrupt_file,
74 "Unexpected bytes in module stream.");
Zachary Turner06c2b4b2016-05-09 17:45:21 +000075
76 return Error::success();
77}
78
Zachary Turnerfa332822017-06-23 23:08:57 +000079BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
80 return SymbolsSubstream;
81}
82
83BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
84 return C11LinesSubstream;
85}
86
87BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
88 return C13LinesSubstream;
89}
90
91BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
92 return GlobalRefsSubstream;
93}
94
Zachary Turner0d43c1c2016-05-28 05:21:57 +000095iterator_range<codeview::CVSymbolArray::Iterator>
Zachary Turner7cc13e52017-05-01 16:46:39 +000096ModuleDebugStreamRef::symbols(bool *HadError) const {
Zachary Turnerfa332822017-06-23 23:08:57 +000097 return make_range(SymbolArray.begin(HadError), SymbolArray.end());
Zachary Turner06c2b4b2016-05-09 17:45:21 +000098}
Zachary Turner7eb6d352016-06-02 20:11:22 +000099
Eugene Zelenko4fcfc192017-06-30 23:06:03 +0000100iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
Zachary Turner92dcdda2017-06-02 19:49:14 +0000101ModuleDebugStreamRef::subsections() const {
102 return make_range(Subsections.begin(), Subsections.end());
Zachary Turner7eb6d352016-06-02 20:11:22 +0000103}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000104
Zachary Turner92dcdda2017-06-02 19:49:14 +0000105bool ModuleDebugStreamRef::hasDebugSubsections() const {
Zachary Turnerfa332822017-06-23 23:08:57 +0000106 return !C13LinesSubstream.empty();
Zachary Turneree3b9c22017-04-25 20:22:02 +0000107}
108
Zachary Turner7cc13e52017-05-01 16:46:39 +0000109Error ModuleDebugStreamRef::commit() { return Error::success(); }
Zachary Turner92dcdda2017-06-02 19:49:14 +0000110
111Expected<codeview::DebugChecksumsSubsectionRef>
112ModuleDebugStreamRef::findChecksumsSubsection() const {
Zachary Turner349c18f2017-06-05 21:40:33 +0000113 codeview::DebugChecksumsSubsectionRef Result;
Zachary Turner92dcdda2017-06-02 19:49:14 +0000114 for (const auto &SS : subsections()) {
115 if (SS.kind() != DebugSubsectionKind::FileChecksums)
116 continue;
117
Zachary Turner92dcdda2017-06-02 19:49:14 +0000118 if (auto EC = Result.initialize(SS.getRecordData()))
119 return std::move(EC);
120 return Result;
121 }
Zachary Turner349c18f2017-06-05 21:40:33 +0000122 return Result;
Zachary Turner92dcdda2017-06-02 19:49:14 +0000123}