LLVM  5.0.0
DbiModuleDescriptorBuilder.cpp
Go to the documentation of this file.
1 //===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info 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 
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/BinaryFormat/COFF.h"
23 
24 using namespace llvm;
25 using namespace llvm::codeview;
26 using namespace llvm::msf;
27 using namespace llvm::pdb;
28 
29 namespace llvm {
30 template <> struct BinaryItemTraits<CVSymbol> {
31  static size_t length(const CVSymbol &Item) { return Item.RecordData.size(); }
32 
33  static ArrayRef<uint8_t> bytes(const CVSymbol &Item) {
34  return Item.RecordData;
35  }
36 };
37 }
38 
40  uint32_t C13Size) {
41  uint32_t Size = sizeof(uint32_t); // Signature
42  Size += alignTo(SymbolByteSize, 4); // Symbol Data
43  Size += 0; // TODO: Layout.C11Bytes
44  Size += C13Size; // C13 Debug Info Size
45  Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
46  Size += 0; // GlobalRefs substream bytes
47  return Size;
48 }
49 
50 DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
51  uint32_t ModIndex,
52  msf::MSFBuilder &Msf)
53  : MSF(Msf), ModuleName(ModuleName) {
54  ::memset(&Layout, 0, sizeof(Layout));
55  Layout.Mod = ModIndex;
56 }
57 
59 
61  return Layout.ModDiStream;
62 }
63 
65  ObjFileName = Name;
66 }
67 
69  PdbFilePathNI = NI;
70 }
71 
73  Symbols.push_back(Symbol);
74  // Symbols written to a PDB file are required to be 4 byte aligned. The same
75  // is not true of object files.
76  assert(Symbol.length() % alignOf(CodeViewContainer::Pdb) == 0 &&
77  "Invalid Symbol alignment!");
78  SymbolByteSize += Symbol.length();
79 }
80 
81 void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
82  SourceFiles.push_back(Path);
83 }
84 
85 uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
86  uint32_t Result = 0;
87  for (const auto &Builder : C13Builders) {
88  assert(Builder && "Empty C13 Fragment Builder!");
89  Result += Builder->calculateSerializedLength();
90  }
91  return Result;
92 }
93 
95  uint32_t L = sizeof(Layout);
96  uint32_t M = ModuleName.size() + 1;
97  uint32_t O = ObjFileName.size() + 1;
98  return alignTo(L + M + O, sizeof(uint32_t));
99 }
100 
101 template <typename T> struct Foo {
102  explicit Foo(T &&Answer) : Answer(Answer) {}
103 
105 };
106 
107 template <typename T> Foo<T> makeFoo(T &&t) { return Foo<T>(std::move(t)); }
108 
110  Layout.SC.ModuleIndex = Layout.Mod;
111  Layout.FileNameOffs = 0; // TODO: Fix this
112  Layout.Flags = 0; // TODO: Fix this
113  Layout.C11Bytes = 0;
114  Layout.C13Bytes = calculateC13DebugInfoSize();
115  (void)Layout.Mod; // Set in constructor
116  (void)Layout.ModDiStream; // Set in finalizeMsfLayout
117  Layout.NumFiles = SourceFiles.size();
118  Layout.PdbFilePathNI = PdbFilePathNI;
119  Layout.SrcFileNameNI = 0;
120 
121  // This value includes both the signature field as well as the record bytes
122  // from the symbol stream.
123  Layout.SymBytes = SymbolByteSize + sizeof(uint32_t);
124 }
125 
127  this->Layout.ModDiStream = kInvalidStreamIndex;
128  uint32_t C13Size = calculateC13DebugInfoSize();
129  auto ExpectedSN =
130  MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
131  if (!ExpectedSN)
132  return ExpectedSN.takeError();
133  Layout.ModDiStream = *ExpectedSN;
134  return Error::success();
135 }
136 
138  const msf::MSFLayout &MsfLayout,
139  WritableBinaryStreamRef MsfBuffer) {
140  // We write the Modi record to the `ModiWriter`, but we additionally write its
141  // symbol stream to a brand new stream.
142  if (auto EC = ModiWriter.writeObject(Layout))
143  return EC;
144  if (auto EC = ModiWriter.writeCString(ModuleName))
145  return EC;
146  if (auto EC = ModiWriter.writeCString(ObjFileName))
147  return EC;
148  if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
149  return EC;
150 
151  if (Layout.ModDiStream != kInvalidStreamIndex) {
152  auto NS = WritableMappedBlockStream::createIndexedStream(
153  MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
154  WritableBinaryStreamRef Ref(*NS);
155  BinaryStreamWriter SymbolWriter(Ref);
156  // Write the symbols.
157  if (auto EC =
159  return EC;
161  Records.setItems(Symbols);
162  BinaryStreamRef RecordsRef(Records);
163  if (auto EC = SymbolWriter.writeStreamRef(RecordsRef))
164  return EC;
165  if (auto EC = SymbolWriter.padToAlignment(4))
166  return EC;
167  // TODO: Write C11 Line data
168  assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
169  "Invalid debug section alignment!");
170  for (const auto &Builder : C13Builders) {
171  assert(Builder && "Empty C13 Fragment Builder!");
172  if (auto EC = Builder->commit(SymbolWriter))
173  return EC;
174  }
175 
176  // TODO: Figure out what GlobalRefs substream actually is and populate it.
177  if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
178  return EC;
179  if (SymbolWriter.bytesRemaining() > 0)
180  return make_error<RawError>(raw_error_code::stream_too_long);
181  }
182  return Error::success();
183 }
184 
186  std::shared_ptr<DebugSubsection> Subsection) {
187  assert(Subsection);
188  C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
189  std::move(Subsection), CodeViewContainer::Pdb));
190 }
191 
193  const DebugSubsectionRecord &SubsectionContents) {
194  C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
195  SubsectionContents, CodeViewContainer::Pdb));
196 }
MachineLoop * L
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
support::ulittle16_t NumFiles
Number of files contributing to this module.
Definition: RawTypes.h:235
support::ulittle32_t SrcFileNameNI
Name Index for src file name.
Definition: RawTypes.h:249
Error commit(BinaryStreamWriter &ModiWriter, const msf::MSFLayout &MsfLayout, WritableBinaryStreamRef MsfBuffer)
support::ulittle32_t SymBytes
Size of local symbol debug info in above stream.
Definition: RawTypes.h:226
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
Foo< T > makeFoo(T &&t)
uint32_t alignOf(CodeViewContainer Container)
Definition: CodeView.h:613
static F t[256]
SectionContribEntry SC
First section contribution of this module.
Definition: RawTypes.h:217
support::ulittle32_t PdbFilePathNI
Name Index for path to compiler PDB.
Definition: RawTypes.h:252
support::ulittle16_t ModuleIndex
Definition: RawTypes.h:170
support::ulittle16_t Flags
See ModInfoFlags definition.
Definition: RawTypes.h:220
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
const char * Name
support::ulittle32_t C13Bytes
Size of C13 line number info in above stream.
Definition: RawTypes.h:232
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:20
Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
support::ulittle32_t FileNameOffs
Array of [0..NumFiles) DBI name buffer offsets.
Definition: RawTypes.h:246
Provides write only access to a subclass of WritableBinaryStream.
Error writeInteger(T Value)
Write the the integer Value to the underlying stream in the specified endianness. ...
static size_t length(const CVSymbol &Item)
Error writeCString(StringRef Str)
Write the the string Str to the underlying stream followed by a null terminator.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
support::ulittle32_t Mod
Currently opened module.
Definition: RawTypes.h:214
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Expected< uint32_t > addStream(uint32_t Size, ArrayRef< uint32_t > Blocks)
Add a stream to the MSF file with the given size, occupying the given list of blocks.
Definition: MSFBuilder.cpp:136
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
support::ulittle16_t ModDiStream
Stream Number of module debug info.
Definition: RawTypes.h:223
uint32_t length() const
Definition: CVRecord.h:36
static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize, uint32_t C13Size)
ArrayRef< uint8_t > RecordData
Definition: CVRecord.h:53
uint32_t bytesRemaining() const
support::ulittle32_t C11Bytes
Size of C11 line number info in above stream.
Definition: RawTypes.h:229
Error padToAlignment(uint32_t Align)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
BinaryItemStream represents a sequence of objects stored in some kind of external container but for w...
static ArrayRef< uint8_t > bytes(const CVSymbol &Item)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
void setItems(ArrayRef< T > ItemArray)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
BumpPtrAllocator & getAllocator()
Definition: MSFBuilder.h:114
void addDebugSubsection(std::shared_ptr< codeview::DebugSubsection > Subsection)