167c56014SZachary Turner //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
267c56014SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
667c56014SZachary Turner //
767c56014SZachary Turner //===----------------------------------------------------------------------===//
867c56014SZachary Turner
967c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
1067c56014SZachary Turner #include "llvm/ADT/iterator_range.h"
114fcfc199SEugene Zelenko #include "llvm/DebugInfo/CodeView/CodeView.h"
124fcfc199SEugene Zelenko #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
13bb3d7e56SZachary Turner #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
14*ed98c1b3Sserge-sans-paille #include "llvm/DebugInfo/MSF/MSFCommon.h"
15*ed98c1b3Sserge-sans-paille #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
1667c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
174aeea4ccSAlexandre Ganea #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
1867c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/RawError.h"
19*ed98c1b3Sserge-sans-paille #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
20*ed98c1b3Sserge-sans-paille #include "llvm/Support/BinaryStreamArray.h"
2167c56014SZachary Turner #include "llvm/Support/BinaryStreamReader.h"
2267c56014SZachary Turner #include "llvm/Support/BinaryStreamRef.h"
2367c56014SZachary Turner #include "llvm/Support/Error.h"
2467c56014SZachary Turner #include <cstdint>
2567c56014SZachary Turner
2667c56014SZachary Turner using namespace llvm;
27c37cb0c6SZachary Turner using namespace llvm::codeview;
2867c56014SZachary Turner using namespace llvm::msf;
2967c56014SZachary Turner using namespace llvm::pdb;
3067c56014SZachary Turner
ModuleDebugStreamRef(const DbiModuleDescriptor & Module,std::unique_ptr<MappedBlockStream> Stream)317cc13e55SZachary Turner ModuleDebugStreamRef::ModuleDebugStreamRef(
327cc13e55SZachary Turner const DbiModuleDescriptor &Module,
3367c56014SZachary Turner std::unique_ptr<MappedBlockStream> Stream)
3467c56014SZachary Turner : Mod(Module), Stream(std::move(Stream)) {}
3567c56014SZachary Turner
367cc13e55SZachary Turner ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
3767c56014SZachary Turner
reload()387cc13e55SZachary Turner Error ModuleDebugStreamRef::reload() {
3967c56014SZachary Turner BinaryStreamReader Reader(*Stream);
4067c56014SZachary Turner
414aeea4ccSAlexandre Ganea if (Mod.getModuleStreamIndex() != llvm::pdb::kInvalidStreamIndex) {
424aeea4ccSAlexandre Ganea if (Error E = reloadSerialize(Reader))
434aeea4ccSAlexandre Ganea return E;
444aeea4ccSAlexandre Ganea }
454aeea4ccSAlexandre Ganea if (Reader.bytesRemaining() > 0)
464aeea4ccSAlexandre Ganea return make_error<RawError>(raw_error_code::corrupt_file,
474aeea4ccSAlexandre Ganea "Unexpected bytes in module stream.");
484aeea4ccSAlexandre Ganea return Error::success();
494aeea4ccSAlexandre Ganea }
504aeea4ccSAlexandre Ganea
reloadSerialize(BinaryStreamReader & Reader)514aeea4ccSAlexandre Ganea Error ModuleDebugStreamRef::reloadSerialize(BinaryStreamReader &Reader) {
5267c56014SZachary Turner uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
535b6e4e0aSZachary Turner uint32_t C11Size = Mod.getC11LineInfoByteSize();
5467c56014SZachary Turner uint32_t C13Size = Mod.getC13LineInfoByteSize();
5567c56014SZachary Turner
5667c56014SZachary Turner if (C11Size > 0 && C13Size > 0)
5767c56014SZachary Turner return make_error<RawError>(raw_error_code::corrupt_file,
5867c56014SZachary Turner "Module has both C11 and C13 line info");
5967c56014SZachary Turner
6067c56014SZachary Turner BinaryStreamRef S;
6167c56014SZachary Turner
6267c56014SZachary Turner if (auto EC = Reader.readInteger(Signature))
6367c56014SZachary Turner return EC;
64579264bdSZachary Turner Reader.setOffset(0);
65579264bdSZachary Turner if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
66fa332827SZachary Turner return EC;
67fa332827SZachary Turner if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
68fa332827SZachary Turner return EC;
69fa332827SZachary Turner if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
7067c56014SZachary Turner return EC;
7167c56014SZachary Turner
72fa332827SZachary Turner BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
73579264bdSZachary Turner if (auto EC = SymbolReader.readArray(
74579264bdSZachary Turner SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
7567c56014SZachary Turner return EC;
7667c56014SZachary Turner
77fa332827SZachary Turner BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
7892dcdda6SZachary Turner if (auto EC = SubsectionsReader.readArray(Subsections,
7992dcdda6SZachary Turner SubsectionsReader.bytesRemaining()))
8067c56014SZachary Turner return EC;
8167c56014SZachary Turner
8267c56014SZachary Turner uint32_t GlobalRefsSize;
8367c56014SZachary Turner if (auto EC = Reader.readInteger(GlobalRefsSize))
8467c56014SZachary Turner return EC;
85fa332827SZachary Turner if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
8667c56014SZachary Turner return EC;
8767c56014SZachary Turner return Error::success();
8867c56014SZachary Turner }
8967c56014SZachary Turner
90bb3d7e56SZachary Turner const codeview::CVSymbolArray
getSymbolArrayForScope(uint32_t ScopeBegin) const91bb3d7e56SZachary Turner ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {
92bb3d7e56SZachary Turner return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
93bb3d7e56SZachary Turner }
94bb3d7e56SZachary Turner
getSymbolsSubstream() const95fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
96fa332827SZachary Turner return SymbolsSubstream;
97fa332827SZachary Turner }
98fa332827SZachary Turner
getC11LinesSubstream() const99fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
100fa332827SZachary Turner return C11LinesSubstream;
101fa332827SZachary Turner }
102fa332827SZachary Turner
getC13LinesSubstream() const103fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
104fa332827SZachary Turner return C13LinesSubstream;
105fa332827SZachary Turner }
106fa332827SZachary Turner
getGlobalRefsSubstream() const107fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
108fa332827SZachary Turner return GlobalRefsSubstream;
109fa332827SZachary Turner }
110fa332827SZachary Turner
11167c56014SZachary Turner iterator_range<codeview::CVSymbolArray::Iterator>
symbols(bool * HadError) const1127cc13e55SZachary Turner ModuleDebugStreamRef::symbols(bool *HadError) const {
113fa332827SZachary Turner return make_range(SymbolArray.begin(HadError), SymbolArray.end());
11467c56014SZachary Turner }
11567c56014SZachary Turner
readSymbolAtOffset(uint32_t Offset) const11694926a6dSZachary Turner CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
117579264bdSZachary Turner auto Iter = SymbolArray.at(Offset);
11894926a6dSZachary Turner assert(Iter != SymbolArray.end());
11994926a6dSZachary Turner return *Iter;
12094926a6dSZachary Turner }
12194926a6dSZachary Turner
1224fcfc199SEugene Zelenko iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
subsections() const12392dcdda6SZachary Turner ModuleDebugStreamRef::subsections() const {
12492dcdda6SZachary Turner return make_range(Subsections.begin(), Subsections.end());
12567c56014SZachary Turner }
12667c56014SZachary Turner
hasDebugSubsections() const12792dcdda6SZachary Turner bool ModuleDebugStreamRef::hasDebugSubsections() const {
128fa332827SZachary Turner return !C13LinesSubstream.empty();
12967c56014SZachary Turner }
13067c56014SZachary Turner
commit()1317cc13e55SZachary Turner Error ModuleDebugStreamRef::commit() { return Error::success(); }
13292dcdda6SZachary Turner
13392dcdda6SZachary Turner Expected<codeview::DebugChecksumsSubsectionRef>
findChecksumsSubsection() const13492dcdda6SZachary Turner ModuleDebugStreamRef::findChecksumsSubsection() const {
135349c18f8SZachary Turner codeview::DebugChecksumsSubsectionRef Result;
13692dcdda6SZachary Turner for (const auto &SS : subsections()) {
13792dcdda6SZachary Turner if (SS.kind() != DebugSubsectionKind::FileChecksums)
13892dcdda6SZachary Turner continue;
13992dcdda6SZachary Turner
14092dcdda6SZachary Turner if (auto EC = Result.initialize(SS.getRecordData()))
141c55cf4afSBill Wendling return std::move(EC);
14292dcdda6SZachary Turner return Result;
14392dcdda6SZachary Turner }
144349c18f8SZachary Turner return Result;
14592dcdda6SZachary Turner }
146