167c56014SZachary Turner //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===// 267c56014SZachary Turner // 367c56014SZachary Turner // The LLVM Compiler Infrastructure 467c56014SZachary Turner // 567c56014SZachary Turner // This file is distributed under the University of Illinois Open Source 667c56014SZachary Turner // License. See LICENSE.TXT for details. 767c56014SZachary Turner // 867c56014SZachary Turner //===----------------------------------------------------------------------===// 967c56014SZachary Turner 1067c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h" 1167c56014SZachary Turner #include "llvm/ADT/iterator_range.h" 124fcfc199SEugene Zelenko #include "llvm/DebugInfo/CodeView/CodeView.h" 134fcfc199SEugene Zelenko #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" 1467c56014SZachary Turner #include "llvm/DebugInfo/CodeView/SymbolRecord.h" 1567c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h" 1667c56014SZachary Turner #include "llvm/DebugInfo/PDB/Native/RawError.h" 1767c56014SZachary Turner #include "llvm/Support/BinaryStreamReader.h" 1867c56014SZachary Turner #include "llvm/Support/BinaryStreamRef.h" 1967c56014SZachary Turner #include "llvm/Support/Error.h" 2067c56014SZachary Turner #include <algorithm> 2167c56014SZachary Turner #include <cstdint> 2267c56014SZachary Turner 2367c56014SZachary Turner using namespace llvm; 24c37cb0c6SZachary Turner using namespace llvm::codeview; 2567c56014SZachary Turner using namespace llvm::msf; 2667c56014SZachary Turner using namespace llvm::pdb; 2767c56014SZachary Turner 287cc13e55SZachary Turner ModuleDebugStreamRef::ModuleDebugStreamRef( 297cc13e55SZachary Turner const DbiModuleDescriptor &Module, 3067c56014SZachary Turner std::unique_ptr<MappedBlockStream> Stream) 3167c56014SZachary Turner : Mod(Module), Stream(std::move(Stream)) {} 3267c56014SZachary Turner 337cc13e55SZachary Turner ModuleDebugStreamRef::~ModuleDebugStreamRef() = default; 3467c56014SZachary Turner 357cc13e55SZachary Turner Error ModuleDebugStreamRef::reload() { 3667c56014SZachary Turner BinaryStreamReader Reader(*Stream); 3767c56014SZachary Turner 3867c56014SZachary Turner uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize(); 395b6e4e0aSZachary Turner uint32_t C11Size = Mod.getC11LineInfoByteSize(); 4067c56014SZachary Turner uint32_t C13Size = Mod.getC13LineInfoByteSize(); 4167c56014SZachary Turner 4267c56014SZachary Turner if (C11Size > 0 && C13Size > 0) 4367c56014SZachary Turner return make_error<RawError>(raw_error_code::corrupt_file, 4467c56014SZachary Turner "Module has both C11 and C13 line info"); 4567c56014SZachary Turner 4667c56014SZachary Turner BinaryStreamRef S; 4767c56014SZachary Turner 4867c56014SZachary Turner if (auto EC = Reader.readInteger(Signature)) 4967c56014SZachary Turner return EC; 50*579264bdSZachary Turner Reader.setOffset(0); 51*579264bdSZachary Turner if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize)) 52fa332827SZachary Turner return EC; 53fa332827SZachary Turner if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size)) 54fa332827SZachary Turner return EC; 55fa332827SZachary Turner if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size)) 5667c56014SZachary Turner return EC; 5767c56014SZachary Turner 58fa332827SZachary Turner BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData); 59*579264bdSZachary Turner if (auto EC = SymbolReader.readArray( 60*579264bdSZachary Turner SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t))) 6167c56014SZachary Turner return EC; 6267c56014SZachary Turner 63fa332827SZachary Turner BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData); 6492dcdda6SZachary Turner if (auto EC = SubsectionsReader.readArray(Subsections, 6592dcdda6SZachary Turner SubsectionsReader.bytesRemaining())) 6667c56014SZachary Turner return EC; 6767c56014SZachary Turner 6867c56014SZachary Turner uint32_t GlobalRefsSize; 6967c56014SZachary Turner if (auto EC = Reader.readInteger(GlobalRefsSize)) 7067c56014SZachary Turner return EC; 71fa332827SZachary Turner if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize)) 7267c56014SZachary Turner return EC; 7367c56014SZachary Turner if (Reader.bytesRemaining() > 0) 7467c56014SZachary Turner return make_error<RawError>(raw_error_code::corrupt_file, 7567c56014SZachary Turner "Unexpected bytes in module stream."); 7667c56014SZachary Turner 7767c56014SZachary Turner return Error::success(); 7867c56014SZachary Turner } 7967c56014SZachary Turner 80fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const { 81fa332827SZachary Turner return SymbolsSubstream; 82fa332827SZachary Turner } 83fa332827SZachary Turner 84fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const { 85fa332827SZachary Turner return C11LinesSubstream; 86fa332827SZachary Turner } 87fa332827SZachary Turner 88fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const { 89fa332827SZachary Turner return C13LinesSubstream; 90fa332827SZachary Turner } 91fa332827SZachary Turner 92fa332827SZachary Turner BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const { 93fa332827SZachary Turner return GlobalRefsSubstream; 94fa332827SZachary Turner } 95fa332827SZachary Turner 9667c56014SZachary Turner iterator_range<codeview::CVSymbolArray::Iterator> 977cc13e55SZachary Turner ModuleDebugStreamRef::symbols(bool *HadError) const { 98fa332827SZachary Turner return make_range(SymbolArray.begin(HadError), SymbolArray.end()); 9967c56014SZachary Turner } 10067c56014SZachary Turner 10194926a6dSZachary Turner CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const { 102*579264bdSZachary Turner auto Iter = SymbolArray.at(Offset); 10394926a6dSZachary Turner assert(Iter != SymbolArray.end()); 10494926a6dSZachary Turner return *Iter; 10594926a6dSZachary Turner } 10694926a6dSZachary Turner 1074fcfc199SEugene Zelenko iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator> 10892dcdda6SZachary Turner ModuleDebugStreamRef::subsections() const { 10992dcdda6SZachary Turner return make_range(Subsections.begin(), Subsections.end()); 11067c56014SZachary Turner } 11167c56014SZachary Turner 11292dcdda6SZachary Turner bool ModuleDebugStreamRef::hasDebugSubsections() const { 113fa332827SZachary Turner return !C13LinesSubstream.empty(); 11467c56014SZachary Turner } 11567c56014SZachary Turner 1167cc13e55SZachary Turner Error ModuleDebugStreamRef::commit() { return Error::success(); } 11792dcdda6SZachary Turner 11892dcdda6SZachary Turner Expected<codeview::DebugChecksumsSubsectionRef> 11992dcdda6SZachary Turner ModuleDebugStreamRef::findChecksumsSubsection() const { 120349c18f8SZachary Turner codeview::DebugChecksumsSubsectionRef Result; 12192dcdda6SZachary Turner for (const auto &SS : subsections()) { 12292dcdda6SZachary Turner if (SS.kind() != DebugSubsectionKind::FileChecksums) 12392dcdda6SZachary Turner continue; 12492dcdda6SZachary Turner 12592dcdda6SZachary Turner if (auto EC = Result.initialize(SS.getRecordData())) 12692dcdda6SZachary Turner return std::move(EC); 12792dcdda6SZachary Turner return Result; 12892dcdda6SZachary Turner } 129349c18f8SZachary Turner return Result; 13092dcdda6SZachary Turner } 131