xref: /src/contrib/llvm-project/llvm/lib/DebugInfo/PDB/Native/InjectedSourceStream.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1e6d15924SDimitry Andric //===- InjectedSourceStream.cpp - PDB Headerblock Stream Access -----------===//
2e6d15924SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e6d15924SDimitry Andric //
7e6d15924SDimitry Andric //===----------------------------------------------------------------------===//
8e6d15924SDimitry Andric 
9e6d15924SDimitry Andric #include "llvm/DebugInfo/PDB/Native/InjectedSourceStream.h"
10e6d15924SDimitry Andric 
11e6d15924SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
12145449b1SDimitry Andric #include "llvm/DebugInfo/PDB/Native/HashTable.h"
13e6d15924SDimitry Andric #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
14e6d15924SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
15e6d15924SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
16e6d15924SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
17e6d15924SDimitry Andric 
18e6d15924SDimitry Andric using namespace llvm;
19e6d15924SDimitry Andric using namespace llvm::msf;
20e6d15924SDimitry Andric using namespace llvm::support;
21e6d15924SDimitry Andric using namespace llvm::pdb;
22e6d15924SDimitry Andric 
InjectedSourceStream(std::unique_ptr<MappedBlockStream> Stream)23e6d15924SDimitry Andric InjectedSourceStream::InjectedSourceStream(
24e6d15924SDimitry Andric     std::unique_ptr<MappedBlockStream> Stream)
25e6d15924SDimitry Andric     : Stream(std::move(Stream)) {}
26e6d15924SDimitry Andric 
reload(const PDBStringTable & Strings)27e6d15924SDimitry Andric Error InjectedSourceStream::reload(const PDBStringTable &Strings) {
28e6d15924SDimitry Andric   BinaryStreamReader Reader(*Stream);
29e6d15924SDimitry Andric 
30e6d15924SDimitry Andric   if (auto EC = Reader.readObject(Header))
31e6d15924SDimitry Andric     return EC;
32e6d15924SDimitry Andric 
33e6d15924SDimitry Andric   if (Header->Version !=
34e6d15924SDimitry Andric       static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne))
35e6d15924SDimitry Andric     return make_error<RawError>(raw_error_code::corrupt_file,
36e6d15924SDimitry Andric                                 "Invalid headerblock header version");
37e6d15924SDimitry Andric 
38e6d15924SDimitry Andric   if (auto EC = InjectedSourceTable.load(Reader))
39e6d15924SDimitry Andric     return EC;
40e6d15924SDimitry Andric 
41e6d15924SDimitry Andric   for (const auto& Entry : *this) {
42e6d15924SDimitry Andric     if (Entry.second.Size != sizeof(SrcHeaderBlockEntry))
43e6d15924SDimitry Andric       return make_error<RawError>(raw_error_code::corrupt_file,
44e6d15924SDimitry Andric                                   "Invalid headerbock entry size");
45e6d15924SDimitry Andric     if (Entry.second.Version !=
46e6d15924SDimitry Andric         static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne))
47e6d15924SDimitry Andric       return make_error<RawError>(raw_error_code::corrupt_file,
48e6d15924SDimitry Andric                                   "Invalid headerbock entry version");
49e6d15924SDimitry Andric 
50e6d15924SDimitry Andric     // Check that all name references are valid.
51e6d15924SDimitry Andric     auto Name = Strings.getStringForID(Entry.second.FileNI);
52e6d15924SDimitry Andric     if (!Name)
53e6d15924SDimitry Andric       return Name.takeError();
54e6d15924SDimitry Andric     auto ObjName = Strings.getStringForID(Entry.second.ObjNI);
55e6d15924SDimitry Andric     if (!ObjName)
56e6d15924SDimitry Andric       return ObjName.takeError();
57e6d15924SDimitry Andric     auto VName = Strings.getStringForID(Entry.second.VFileNI);
58e6d15924SDimitry Andric     if (!VName)
59e6d15924SDimitry Andric       return VName.takeError();
60e6d15924SDimitry Andric   }
61e6d15924SDimitry Andric 
62e6d15924SDimitry Andric   assert(Reader.bytesRemaining() == 0);
63e6d15924SDimitry Andric   return Error::success();
64e6d15924SDimitry Andric }
65