Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/PDB/Native/ModuleDebugStream.cpp
35293 views
//===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"9#include "llvm/ADT/iterator_range.h"10#include "llvm/DebugInfo/CodeView/CodeView.h"11#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"12#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"13#include "llvm/DebugInfo/MSF/MSFCommon.h"14#include "llvm/DebugInfo/MSF/MappedBlockStream.h"15#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"16#include "llvm/DebugInfo/PDB/Native/RawConstants.h"17#include "llvm/DebugInfo/PDB/Native/RawError.h"18#include "llvm/DebugInfo/PDB/Native/RawTypes.h"19#include "llvm/Support/BinaryStreamArray.h"20#include "llvm/Support/BinaryStreamReader.h"21#include "llvm/Support/BinaryStreamRef.h"22#include "llvm/Support/Error.h"23#include <cstdint>2425using namespace llvm;26using namespace llvm::codeview;27using namespace llvm::msf;28using namespace llvm::pdb;2930ModuleDebugStreamRef::ModuleDebugStreamRef(31const DbiModuleDescriptor &Module,32std::unique_ptr<MappedBlockStream> Stream)33: Mod(Module), Stream(std::move(Stream)) {}3435ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;3637Error ModuleDebugStreamRef::reload() {38BinaryStreamReader Reader(*Stream);3940if (Mod.getModuleStreamIndex() != llvm::pdb::kInvalidStreamIndex) {41if (Error E = reloadSerialize(Reader))42return E;43}44if (Reader.bytesRemaining() > 0)45return make_error<RawError>(raw_error_code::corrupt_file,46"Unexpected bytes in module stream.");47return Error::success();48}4950Error ModuleDebugStreamRef::reloadSerialize(BinaryStreamReader &Reader) {51uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();52uint32_t C11Size = Mod.getC11LineInfoByteSize();53uint32_t C13Size = Mod.getC13LineInfoByteSize();5455if (C11Size > 0 && C13Size > 0)56return make_error<RawError>(raw_error_code::corrupt_file,57"Module has both C11 and C13 line info");5859BinaryStreamRef S;6061if (auto EC = Reader.readInteger(Signature))62return EC;63Reader.setOffset(0);64if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))65return EC;66if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))67return EC;68if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))69return EC;7071BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);72if (auto EC = SymbolReader.readArray(73SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))74return EC;7576BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);77if (auto EC = SubsectionsReader.readArray(Subsections,78SubsectionsReader.bytesRemaining()))79return EC;8081uint32_t GlobalRefsSize;82if (auto EC = Reader.readInteger(GlobalRefsSize))83return EC;84if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))85return EC;86return Error::success();87}8889const codeview::CVSymbolArray90ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {91return limitSymbolArrayToScope(SymbolArray, ScopeBegin);92}9394BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {95return SymbolsSubstream;96}9798BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {99return C11LinesSubstream;100}101102BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {103return C13LinesSubstream;104}105106BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {107return GlobalRefsSubstream;108}109110iterator_range<codeview::CVSymbolArray::Iterator>111ModuleDebugStreamRef::symbols(bool *HadError) const {112return make_range(SymbolArray.begin(HadError), SymbolArray.end());113}114115CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {116auto Iter = SymbolArray.at(Offset);117assert(Iter != SymbolArray.end());118return *Iter;119}120121iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>122ModuleDebugStreamRef::subsections() const {123return make_range(Subsections.begin(), Subsections.end());124}125126bool ModuleDebugStreamRef::hasDebugSubsections() const {127return !C13LinesSubstream.empty();128}129130Error ModuleDebugStreamRef::commit() { return Error::success(); }131132Expected<codeview::DebugChecksumsSubsectionRef>133ModuleDebugStreamRef::findChecksumsSubsection() const {134codeview::DebugChecksumsSubsectionRef Result;135for (const auto &SS : subsections()) {136if (SS.kind() != DebugSubsectionKind::FileChecksums)137continue;138139if (auto EC = Result.initialize(SS.getRecordData()))140return std::move(EC);141return Result;142}143return Result;144}145146147