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