10b57cec5SDimitry Andric //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
100b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
130b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
14*81ad6265SDimitry Andric #include "llvm/DebugInfo/MSF/MSFCommon.h"
15*81ad6265SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
160b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
170b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
180b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawError.h"
19*81ad6265SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
20*81ad6265SDimitry Andric #include "llvm/Support/BinaryStreamArray.h"
210b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
220b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamRef.h"
230b57cec5SDimitry Andric #include "llvm/Support/Error.h"
240b57cec5SDimitry Andric #include <cstdint>
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric using namespace llvm::codeview;
280b57cec5SDimitry Andric using namespace llvm::msf;
290b57cec5SDimitry Andric using namespace llvm::pdb;
300b57cec5SDimitry Andric
ModuleDebugStreamRef(const DbiModuleDescriptor & Module,std::unique_ptr<MappedBlockStream> Stream)310b57cec5SDimitry Andric ModuleDebugStreamRef::ModuleDebugStreamRef(
320b57cec5SDimitry Andric const DbiModuleDescriptor &Module,
330b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream> Stream)
340b57cec5SDimitry Andric : Mod(Module), Stream(std::move(Stream)) {}
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
370b57cec5SDimitry Andric
reload()380b57cec5SDimitry Andric Error ModuleDebugStreamRef::reload() {
390b57cec5SDimitry Andric BinaryStreamReader Reader(*Stream);
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric if (Mod.getModuleStreamIndex() != llvm::pdb::kInvalidStreamIndex) {
420b57cec5SDimitry Andric if (Error E = reloadSerialize(Reader))
430b57cec5SDimitry Andric return E;
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric if (Reader.bytesRemaining() > 0)
460b57cec5SDimitry Andric return make_error<RawError>(raw_error_code::corrupt_file,
470b57cec5SDimitry Andric "Unexpected bytes in module stream.");
480b57cec5SDimitry Andric return Error::success();
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric
reloadSerialize(BinaryStreamReader & Reader)510b57cec5SDimitry Andric Error ModuleDebugStreamRef::reloadSerialize(BinaryStreamReader &Reader) {
520b57cec5SDimitry Andric uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
530b57cec5SDimitry Andric uint32_t C11Size = Mod.getC11LineInfoByteSize();
540b57cec5SDimitry Andric uint32_t C13Size = Mod.getC13LineInfoByteSize();
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric if (C11Size > 0 && C13Size > 0)
570b57cec5SDimitry Andric return make_error<RawError>(raw_error_code::corrupt_file,
580b57cec5SDimitry Andric "Module has both C11 and C13 line info");
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric BinaryStreamRef S;
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric if (auto EC = Reader.readInteger(Signature))
630b57cec5SDimitry Andric return EC;
640b57cec5SDimitry Andric Reader.setOffset(0);
650b57cec5SDimitry Andric if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
660b57cec5SDimitry Andric return EC;
670b57cec5SDimitry Andric if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
680b57cec5SDimitry Andric return EC;
690b57cec5SDimitry Andric if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
700b57cec5SDimitry Andric return EC;
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
730b57cec5SDimitry Andric if (auto EC = SymbolReader.readArray(
740b57cec5SDimitry Andric SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
750b57cec5SDimitry Andric return EC;
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
780b57cec5SDimitry Andric if (auto EC = SubsectionsReader.readArray(Subsections,
790b57cec5SDimitry Andric SubsectionsReader.bytesRemaining()))
800b57cec5SDimitry Andric return EC;
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric uint32_t GlobalRefsSize;
830b57cec5SDimitry Andric if (auto EC = Reader.readInteger(GlobalRefsSize))
840b57cec5SDimitry Andric return EC;
850b57cec5SDimitry Andric if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
860b57cec5SDimitry Andric return EC;
870b57cec5SDimitry Andric return Error::success();
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric const codeview::CVSymbolArray
getSymbolArrayForScope(uint32_t ScopeBegin) const910b57cec5SDimitry Andric ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {
920b57cec5SDimitry Andric return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
getSymbolsSubstream() const950b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
960b57cec5SDimitry Andric return SymbolsSubstream;
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
getC11LinesSubstream() const990b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
1000b57cec5SDimitry Andric return C11LinesSubstream;
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric
getC13LinesSubstream() const1030b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
1040b57cec5SDimitry Andric return C13LinesSubstream;
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric
getGlobalRefsSubstream() const1070b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
1080b57cec5SDimitry Andric return GlobalRefsSubstream;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric iterator_range<codeview::CVSymbolArray::Iterator>
symbols(bool * HadError) const1120b57cec5SDimitry Andric ModuleDebugStreamRef::symbols(bool *HadError) const {
1130b57cec5SDimitry Andric return make_range(SymbolArray.begin(HadError), SymbolArray.end());
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric
readSymbolAtOffset(uint32_t Offset) const1160b57cec5SDimitry Andric CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
1170b57cec5SDimitry Andric auto Iter = SymbolArray.at(Offset);
1180b57cec5SDimitry Andric assert(Iter != SymbolArray.end());
1190b57cec5SDimitry Andric return *Iter;
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
subsections() const1230b57cec5SDimitry Andric ModuleDebugStreamRef::subsections() const {
1240b57cec5SDimitry Andric return make_range(Subsections.begin(), Subsections.end());
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric
hasDebugSubsections() const1270b57cec5SDimitry Andric bool ModuleDebugStreamRef::hasDebugSubsections() const {
1280b57cec5SDimitry Andric return !C13LinesSubstream.empty();
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric
commit()1310b57cec5SDimitry Andric Error ModuleDebugStreamRef::commit() { return Error::success(); }
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric Expected<codeview::DebugChecksumsSubsectionRef>
findChecksumsSubsection() const1340b57cec5SDimitry Andric ModuleDebugStreamRef::findChecksumsSubsection() const {
1350b57cec5SDimitry Andric codeview::DebugChecksumsSubsectionRef Result;
1360b57cec5SDimitry Andric for (const auto &SS : subsections()) {
1370b57cec5SDimitry Andric if (SS.kind() != DebugSubsectionKind::FileChecksums)
1380b57cec5SDimitry Andric continue;
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric if (auto EC = Result.initialize(SS.getRecordData()))
1410b57cec5SDimitry Andric return std::move(EC);
1420b57cec5SDimitry Andric return Result;
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric return Result;
1450b57cec5SDimitry Andric }
146