1//===- DbiStreamBuilder.h - PDB Dbi Stream Creation -------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_DEBUGINFO_PDB_RAW_PDBDBISTREAMBUILDER_H
11#define LLVM_DEBUGINFO_PDB_RAW_PDBDBISTREAMBUILDER_H
12
13#include "llvm/ADT/Optional.h"
14#include "llvm/ADT/StringSet.h"
15#include "llvm/Support/Error.h"
16
17#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
18#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
19#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
20#include "llvm/DebugInfo/PDB/PDBTypes.h"
21#include "llvm/Support/BinaryByteStream.h"
22#include "llvm/Support/BinaryStreamReader.h"
23#include "llvm/Support/Endian.h"
24
25namespace llvm {
26namespace msf {
27class MSFBuilder;
28}
29namespace object {
30struct coff_section;
31}
32namespace pdb {
33class DbiStream;
34struct DbiStreamHeader;
35class DbiModuleDescriptorBuilder;
36class PDBFile;
37
38class DbiStreamBuilder {
39public:
40  DbiStreamBuilder(msf::MSFBuilder &Msf);
41  ~DbiStreamBuilder();
42
43  DbiStreamBuilder(const DbiStreamBuilder &) = delete;
44  DbiStreamBuilder &operator=(const DbiStreamBuilder &) = delete;
45
46  void setVersionHeader(PdbRaw_DbiVer V);
47  void setAge(uint32_t A);
48  void setBuildNumber(uint16_t B);
49  void setPdbDllVersion(uint16_t V);
50  void setPdbDllRbld(uint16_t R);
51  void setFlags(uint16_t F);
52  void setMachineType(PDB_Machine M);
53  void setSectionMap(ArrayRef<SecMapEntry> SecMap);
54
55  // Add given bytes as a new stream.
56  Error addDbgStream(pdb::DbgHeaderType Type, ArrayRef<uint8_t> Data);
57
58  uint32_t addECName(StringRef Name);
59
60  uint32_t calculateSerializedLength() const;
61
62  void setGlobalsStreamIndex(uint32_t Index);
63  void setPublicsStreamIndex(uint32_t Index);
64  void setSymbolRecordStreamIndex(uint32_t Index);
65
66  Expected<DbiModuleDescriptorBuilder &> addModuleInfo(StringRef ModuleName);
67  Error addModuleSourceFile(DbiModuleDescriptorBuilder &Module, StringRef File);
68  Expected<uint32_t> getSourceFileNameIndex(StringRef FileName);
69
70  Error finalizeMsfLayout();
71
72  Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef MsfBuffer);
73
74  void addSectionContrib(const SectionContrib &SC) {
75    SectionContribs.emplace_back(SC);
76  }
77
78  // A helper function to create a Section Map from a COFF section header.
79  static std::vector<SecMapEntry>
80  createSectionMap(ArrayRef<llvm::object::coff_section> SecHdrs);
81
82private:
83  struct DebugStream {
84    ArrayRef<uint8_t> Data;
85    uint16_t StreamNumber = kInvalidStreamIndex;
86  };
87
88  Error finalize();
89  uint32_t calculateModiSubstreamSize() const;
90  uint32_t calculateNamesOffset() const;
91  uint32_t calculateSectionContribsStreamSize() const;
92  uint32_t calculateSectionMapStreamSize() const;
93  uint32_t calculateFileInfoSubstreamSize() const;
94  uint32_t calculateNamesBufferSize() const;
95  uint32_t calculateDbgStreamsSize() const;
96
97  Error generateFileInfoSubstream();
98
99  msf::MSFBuilder &Msf;
100  BumpPtrAllocator &Allocator;
101
102  Optional<PdbRaw_DbiVer> VerHeader;
103  uint32_t Age;
104  uint16_t BuildNumber;
105  uint16_t PdbDllVersion;
106  uint16_t PdbDllRbld;
107  uint16_t Flags;
108  PDB_Machine MachineType;
109  uint32_t GlobalsStreamIndex = kInvalidStreamIndex;
110  uint32_t PublicsStreamIndex = kInvalidStreamIndex;
111  uint32_t SymRecordStreamIndex = kInvalidStreamIndex;
112
113  const DbiStreamHeader *Header;
114
115  std::vector<std::unique_ptr<DbiModuleDescriptorBuilder>> ModiList;
116
117  StringMap<uint32_t> SourceFileNames;
118
119  PDBStringTableBuilder ECNamesBuilder;
120  WritableBinaryStreamRef NamesBuffer;
121  MutableBinaryByteStream FileInfoBuffer;
122  std::vector<SectionContrib> SectionContribs;
123  ArrayRef<SecMapEntry> SectionMap;
124  llvm::SmallVector<DebugStream, (int)DbgHeaderType::Max> DbgStreams;
125};
126}
127}
128
129#endif
130