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