Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 1 | //===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info Creation ---*- C++ -*-===// |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 11 | |
| 12 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 13 | #include "llvm/BinaryFormat/COFF.h" |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
| 16 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
| 17 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h" |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Native/RawConstants.h" |
| 20 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
| 21 | #include "llvm/Support/BinaryItemStream.h" |
| 22 | #include "llvm/Support/BinaryStreamWriter.h" |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 39 | static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize, |
| 40 | uint32_t C13Size) { |
Zachary Turner | 1bfb9f4 | 2017-06-06 23:54:23 +0000 | [diff] [blame] | 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 |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 47 | return Size; |
| 48 | } |
| 49 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 50 | DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName, |
| 51 | uint32_t ModIndex, |
| 52 | msf::MSFBuilder &Msf) |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 53 | : MSF(Msf), ModuleName(ModuleName) { |
Zachary Turner | 297b6eb | 2017-06-20 18:50:55 +0000 | [diff] [blame^] | 54 | ::memset(&Layout, 0, sizeof(Layout)); |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 55 | Layout.Mod = ModIndex; |
| 56 | } |
| 57 | |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 58 | DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() {} |
| 59 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 60 | uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const { |
| 61 | return Layout.ModDiStream; |
| 62 | } |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 63 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 64 | void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) { |
| 65 | ObjFileName = Name; |
| 66 | } |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 67 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 68 | void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) { |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 69 | Symbols.push_back(Symbol); |
Zachary Turner | ebd3ae8 | 2017-06-01 21:52:41 +0000 | [diff] [blame] | 70 | // Symbols written to a PDB file are required to be 4 byte aligned. The same |
| 71 | // is not true of object files. |
| 72 | assert(Symbol.length() % alignOf(CodeViewContainer::Pdb) == 0 && |
| 73 | "Invalid Symbol alignment!"); |
| 74 | SymbolByteSize += Symbol.length(); |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 77 | void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) { |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 78 | SourceFiles.push_back(Path); |
| 79 | } |
| 80 | |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 81 | uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const { |
| 82 | uint32_t Result = 0; |
| 83 | for (const auto &Builder : C13Builders) { |
| 84 | assert(Builder && "Empty C13 Fragment Builder!"); |
| 85 | Result += Builder->calculateSerializedLength(); |
| 86 | } |
| 87 | return Result; |
| 88 | } |
| 89 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 90 | uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const { |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 91 | uint32_t L = sizeof(Layout); |
| 92 | uint32_t M = ModuleName.size() + 1; |
| 93 | uint32_t O = ObjFileName.size() + 1; |
| 94 | return alignTo(L + M + O, sizeof(uint32_t)); |
| 95 | } |
| 96 | |
Zachary Turner | 59e8389 | 2017-05-03 05:34:00 +0000 | [diff] [blame] | 97 | template <typename T> struct Foo { |
| 98 | explicit Foo(T &&Answer) : Answer(Answer) {} |
| 99 | |
| 100 | T Answer; |
| 101 | }; |
| 102 | |
| 103 | template <typename T> Foo<T> makeFoo(T &&t) { return Foo<T>(std::move(t)); } |
| 104 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 105 | void DbiModuleDescriptorBuilder::finalize() { |
Zachary Turner | 297b6eb | 2017-06-20 18:50:55 +0000 | [diff] [blame^] | 106 | Layout.SC.ModuleIndex = Layout.Mod; |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 107 | Layout.FileNameOffs = 0; // TODO: Fix this |
| 108 | Layout.Flags = 0; // TODO: Fix this |
Zachary Turner | 5b6e4e0 | 2017-04-29 01:13:21 +0000 | [diff] [blame] | 109 | Layout.C11Bytes = 0; |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 110 | Layout.C13Bytes = calculateC13DebugInfoSize(); |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 111 | (void)Layout.Mod; // Set in constructor |
| 112 | (void)Layout.ModDiStream; // Set in finalizeMsfLayout |
| 113 | Layout.NumFiles = SourceFiles.size(); |
| 114 | Layout.PdbFilePathNI = 0; |
| 115 | Layout.SrcFileNameNI = 0; |
| 116 | |
| 117 | // This value includes both the signature field as well as the record bytes |
| 118 | // from the symbol stream. |
| 119 | Layout.SymBytes = SymbolByteSize + sizeof(uint32_t); |
| 120 | } |
| 121 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 122 | Error DbiModuleDescriptorBuilder::finalizeMsfLayout() { |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 123 | this->Layout.ModDiStream = kInvalidStreamIndex; |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 124 | uint32_t C13Size = calculateC13DebugInfoSize(); |
| 125 | auto ExpectedSN = |
| 126 | MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size)); |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 127 | if (!ExpectedSN) |
| 128 | return ExpectedSN.takeError(); |
| 129 | Layout.ModDiStream = *ExpectedSN; |
| 130 | return Error::success(); |
| 131 | } |
| 132 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 133 | Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter, |
| 134 | const msf::MSFLayout &MsfLayout, |
| 135 | WritableBinaryStreamRef MsfBuffer) { |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 136 | // We write the Modi record to the `ModiWriter`, but we additionally write its |
| 137 | // symbol stream to a brand new stream. |
| 138 | if (auto EC = ModiWriter.writeObject(Layout)) |
| 139 | return EC; |
| 140 | if (auto EC = ModiWriter.writeCString(ModuleName)) |
| 141 | return EC; |
| 142 | if (auto EC = ModiWriter.writeCString(ObjFileName)) |
| 143 | return EC; |
| 144 | if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t))) |
| 145 | return EC; |
| 146 | |
| 147 | if (Layout.ModDiStream != kInvalidStreamIndex) { |
| 148 | auto NS = WritableMappedBlockStream::createIndexedStream( |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 149 | MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator()); |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 150 | WritableBinaryStreamRef Ref(*NS); |
| 151 | BinaryStreamWriter SymbolWriter(Ref); |
| 152 | // Write the symbols. |
| 153 | if (auto EC = |
| 154 | SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC)) |
| 155 | return EC; |
| 156 | BinaryItemStream<CVSymbol> Records(llvm::support::endianness::little); |
| 157 | Records.setItems(Symbols); |
| 158 | BinaryStreamRef RecordsRef(Records); |
| 159 | if (auto EC = SymbolWriter.writeStreamRef(RecordsRef)) |
| 160 | return EC; |
Zachary Turner | 1bfb9f4 | 2017-06-06 23:54:23 +0000 | [diff] [blame] | 161 | if (auto EC = SymbolWriter.padToAlignment(4)) |
| 162 | return EC; |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 163 | // TODO: Write C11 Line data |
Zachary Turner | ebd3ae8 | 2017-06-01 21:52:41 +0000 | [diff] [blame] | 164 | assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 && |
| 165 | "Invalid debug section alignment!"); |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 166 | for (const auto &Builder : C13Builders) { |
| 167 | assert(Builder && "Empty C13 Fragment Builder!"); |
| 168 | if (auto EC = Builder->commit(SymbolWriter)) |
| 169 | return EC; |
| 170 | } |
| 171 | |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 172 | // TODO: Figure out what GlobalRefs substream actually is and populate it. |
| 173 | if (auto EC = SymbolWriter.writeInteger<uint32_t>(0)) |
| 174 | return EC; |
| 175 | if (SymbolWriter.bytesRemaining() > 0) |
| 176 | return make_error<RawError>(raw_error_code::stream_too_long); |
| 177 | } |
| 178 | return Error::success(); |
| 179 | } |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 180 | |
Zachary Turner | 92dcdda | 2017-06-02 19:49:14 +0000 | [diff] [blame] | 181 | void DbiModuleDescriptorBuilder::addDebugSubsection( |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 182 | std::shared_ptr<DebugSubsection> Subsection) { |
Zachary Turner | 92dcdda | 2017-06-02 19:49:14 +0000 | [diff] [blame] | 183 | assert(Subsection); |
Zachary Turner | ebd3ae8 | 2017-06-01 21:52:41 +0000 | [diff] [blame] | 184 | C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>( |
Zachary Turner | 92dcdda | 2017-06-02 19:49:14 +0000 | [diff] [blame] | 185 | std::move(Subsection), CodeViewContainer::Pdb)); |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 186 | } |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 187 | |
| 188 | void DbiModuleDescriptorBuilder::addDebugSubsection( |
| 189 | const DebugSubsectionRecord &SubsectionContents) { |
| 190 | C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>( |
| 191 | SubsectionContents, CodeViewContainer::Pdb)); |
| 192 | } |