LLVM  6.0.0svn
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"
24 
25 using namespace llvm;
26 using namespace llvm::codeview;
27 using namespace llvm::msf;
28 using namespace llvm::pdb;
29 
31  uint32_t C13Size) {
32  uint32_t Size = sizeof(uint32_t); // Signature
33  Size += alignTo(SymbolByteSize, 4); // Symbol Data
34  Size += 0; // TODO: Layout.C11Bytes
35  Size += C13Size; // C13 Debug Info Size
36  Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
37  Size += 0; // GlobalRefs substream bytes
38  return Size;
39 }
40 
41 DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
42  uint32_t ModIndex,
43  msf::MSFBuilder &Msf)
44  : MSF(Msf), ModuleName(ModuleName) {
45  ::memset(&Layout, 0, sizeof(Layout));
46  Layout.Mod = ModIndex;
47 }
48 
50 
52  return Layout.ModDiStream;
53 }
54 
56  ObjFileName = Name;
57 }
58 
60  PdbFilePathNI = NI;
61 }
62 
64  Symbols.push_back(Symbol);
65  // Symbols written to a PDB file are required to be 4 byte aligned. The same
66  // is not true of object files.
67  assert(Symbol.length() % alignOf(CodeViewContainer::Pdb) == 0 &&
68  "Invalid Symbol alignment!");
69  SymbolByteSize += Symbol.length();
70 }
71 
72 void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
73  SourceFiles.push_back(Path);
74 }
75 
76 uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
77  uint32_t Result = 0;
78  for (const auto &Builder : C13Builders) {
79  assert(Builder && "Empty C13 Fragment Builder!");
80  Result += Builder->calculateSerializedLength();
81  }
82  return Result;
83 }
84 
86  uint32_t L = sizeof(Layout);
87  uint32_t M = ModuleName.size() + 1;
88  uint32_t O = ObjFileName.size() + 1;
89  return alignTo(L + M + O, sizeof(uint32_t));
90 }
91 
93  Layout.SC.ModuleIndex = Layout.Mod;
94  Layout.FileNameOffs = 0; // TODO: Fix this
95  Layout.Flags = 0; // TODO: Fix this
96  Layout.C11Bytes = 0;
97  Layout.C13Bytes = calculateC13DebugInfoSize();
98  (void)Layout.Mod; // Set in constructor
99  (void)Layout.ModDiStream; // Set in finalizeMsfLayout
100  Layout.NumFiles = SourceFiles.size();
101  Layout.PdbFilePathNI = PdbFilePathNI;
102  Layout.SrcFileNameNI = 0;
103 
104  // This value includes both the signature field as well as the record bytes
105  // from the symbol stream.
106  Layout.SymBytes = SymbolByteSize + sizeof(uint32_t);
107 }
108 
110  this->Layout.ModDiStream = kInvalidStreamIndex;
111  uint32_t C13Size = calculateC13DebugInfoSize();
112  auto ExpectedSN =
113  MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
114  if (!ExpectedSN)
115  return ExpectedSN.takeError();
116  Layout.ModDiStream = *ExpectedSN;
117  return Error::success();
118 }
119 
121  const msf::MSFLayout &MsfLayout,
122  WritableBinaryStreamRef MsfBuffer) {
123  // We write the Modi record to the `ModiWriter`, but we additionally write its
124  // symbol stream to a brand new stream.
125  if (auto EC = ModiWriter.writeObject(Layout))
126  return EC;
127  if (auto EC = ModiWriter.writeCString(ModuleName))
128  return EC;
129  if (auto EC = ModiWriter.writeCString(ObjFileName))
130  return EC;
131  if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
132  return EC;
133 
134  if (Layout.ModDiStream != kInvalidStreamIndex) {
135  auto NS = WritableMappedBlockStream::createIndexedStream(
136  MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
137  WritableBinaryStreamRef Ref(*NS);
138  BinaryStreamWriter SymbolWriter(Ref);
139  // Write the symbols.
140  if (auto EC =
142  return EC;
144  Records.setItems(Symbols);
145  BinaryStreamRef RecordsRef(Records);
146  if (auto EC = SymbolWriter.writeStreamRef(RecordsRef))
147  return EC;
148  if (auto EC = SymbolWriter.padToAlignment(4))
149  return EC;
150  // TODO: Write C11 Line data
151  assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
152  "Invalid debug section alignment!");
153  for (const auto &Builder : C13Builders) {
154  assert(Builder && "Empty C13 Fragment Builder!");
155  if (auto EC = Builder->commit(SymbolWriter))
156  return EC;
157  }
158 
159  // TODO: Figure out what GlobalRefs substream actually is and populate it.
160  if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
161  return EC;
162  if (SymbolWriter.bytesRemaining() > 0)
163  return make_error<RawError>(raw_error_code::stream_too_long);
164  }
165  return Error::success();
166 }
167 
169  std::shared_ptr<DebugSubsection> Subsection) {
170  assert(Subsection);
171  C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
172  std::move(Subsection), CodeViewContainer::Pdb));
173 }
174 
176  const DebugSubsectionRecord &SubsectionContents) {
177  C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
178  SubsectionContents, CodeViewContainer::Pdb));
179 }
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
support::ulittle16_t NumFiles
Number of files contributing to this module.
Definition: RawTypes.h:249
support::ulittle32_t SrcFileNameNI
Name Index for src file name.
Definition: RawTypes.h:263
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:240
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
uint32_t alignOf(CodeViewContainer Container)
Definition: CodeView.h:572
SectionContribEntry SC
First section contribution of this module.
Definition: RawTypes.h:231
support::ulittle32_t PdbFilePathNI
Name Index for path to compiler PDB.
Definition: RawTypes.h:266
support::ulittle16_t ModuleIndex
Definition: RawTypes.h:184
support::ulittle16_t Flags
See ModInfoFlags definition.
Definition: RawTypes.h:234
uint32_t length() const
Definition: CVRecord.h:36
support::ulittle32_t C13Bytes
Size of C13 line number info in above stream.
Definition: RawTypes.h:246
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:260
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. ...
uint32_t bytesRemaining() const
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:313
support::ulittle32_t Mod
Currently opened module.
Definition: RawTypes.h:228
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:151
support::ulittle16_t ModDiStream
Stream Number of module debug info.
Definition: RawTypes.h:237
static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize, uint32_t C13Size)
support::ulittle32_t C11Bytes
Size of C11 line number info in above stream.
Definition: RawTypes.h:243
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...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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)