Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 1 | //===- PDBFileBuilder.cpp - PDB File 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 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 11 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/BitVector.h" |
| 13 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/GenericError.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 17 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 20 | #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 24 | #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/BinaryStream.h" |
| 26 | #include "llvm/Support/BinaryStreamWriter.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace llvm::codeview; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 30 | using namespace llvm::msf; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 31 | using namespace llvm::pdb; |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 32 | using namespace llvm::support; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 33 | |
Zachary Turner | e109dc6 | 2016-07-22 19:56:26 +0000 | [diff] [blame] | 34 | PDBFileBuilder::PDBFileBuilder(BumpPtrAllocator &Allocator) |
| 35 | : Allocator(Allocator) {} |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 36 | |
Zachary Turner | 7eaf1d9 | 2017-07-10 22:40:20 +0000 | [diff] [blame] | 37 | PDBFileBuilder::~PDBFileBuilder() {} |
| 38 | |
Rui Ueyama | 5d6714e | 2016-09-30 20:52:12 +0000 | [diff] [blame] | 39 | Error PDBFileBuilder::initialize(uint32_t BlockSize) { |
| 40 | auto ExpectedMsf = MSFBuilder::create(Allocator, BlockSize); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 41 | if (!ExpectedMsf) |
| 42 | return ExpectedMsf.takeError(); |
Rui Ueyama | 5d6714e | 2016-09-30 20:52:12 +0000 | [diff] [blame] | 43 | Msf = llvm::make_unique<MSFBuilder>(std::move(*ExpectedMsf)); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 44 | return Error::success(); |
| 45 | } |
| 46 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 47 | MSFBuilder &PDBFileBuilder::getMsfBuilder() { return *Msf; } |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 48 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 49 | InfoStreamBuilder &PDBFileBuilder::getInfoBuilder() { |
| 50 | if (!Info) |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 51 | Info = llvm::make_unique<InfoStreamBuilder>(*Msf, NamedStreams); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 52 | return *Info; |
| 53 | } |
| 54 | |
| 55 | DbiStreamBuilder &PDBFileBuilder::getDbiBuilder() { |
| 56 | if (!Dbi) |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 57 | Dbi = llvm::make_unique<DbiStreamBuilder>(*Msf); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 58 | return *Dbi; |
| 59 | } |
| 60 | |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 61 | TpiStreamBuilder &PDBFileBuilder::getTpiBuilder() { |
| 62 | if (!Tpi) |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 63 | Tpi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamTPI); |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 64 | return *Tpi; |
| 65 | } |
| 66 | |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 67 | TpiStreamBuilder &PDBFileBuilder::getIpiBuilder() { |
| 68 | if (!Ipi) |
| 69 | Ipi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamIPI); |
| 70 | return *Ipi; |
| 71 | } |
| 72 | |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 73 | PDBStringTableBuilder &PDBFileBuilder::getStringTableBuilder() { |
| 74 | return Strings; |
| 75 | } |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 76 | |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 77 | GSIStreamBuilder &PDBFileBuilder::getGsiBuilder() { |
| 78 | if (!Gsi) |
| 79 | Gsi = llvm::make_unique<GSIStreamBuilder>(*Msf); |
| 80 | return *Gsi; |
Zachary Turner | 8d927b6 | 2017-07-31 19:36:08 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 83 | Error PDBFileBuilder::addNamedStream(StringRef Name, uint32_t Size) { |
| 84 | auto ExpectedStream = Msf->addStream(Size); |
| 85 | if (!ExpectedStream) |
| 86 | return ExpectedStream.takeError(); |
| 87 | NamedStreams.set(Name, *ExpectedStream); |
| 88 | return Error::success(); |
| 89 | } |
| 90 | |
| 91 | Expected<msf::MSFLayout> PDBFileBuilder::finalizeMsfLayout() { |
Zachary Turner | 68ea80d | 2017-06-12 21:46:51 +0000 | [diff] [blame] | 92 | |
| 93 | if (Ipi && Ipi->getRecordCount() > 0) { |
| 94 | // In theory newer PDBs always have an ID stream, but by saying that we're |
| 95 | // only going to *really* have an ID stream if there is at least one ID |
| 96 | // record, we leave open the opportunity to test older PDBs such as those |
| 97 | // that don't have an ID stream. |
| 98 | auto &Info = getInfoBuilder(); |
| 99 | Info.addFeature(PdbRaw_FeatureSig::VC140); |
| 100 | } |
| 101 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 102 | uint32_t StringsLen = Strings.calculateSerializedSize(); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 103 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 104 | if (auto EC = addNamedStream("/names", StringsLen)) |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 105 | return std::move(EC); |
| 106 | if (auto EC = addNamedStream("/LinkInfo", 0)) |
| 107 | return std::move(EC); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 108 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 109 | if (Info) { |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 110 | if (auto EC = Info->finalizeMsfLayout()) |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 111 | return std::move(EC); |
| 112 | } |
| 113 | if (Dbi) { |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 114 | if (auto EC = Dbi->finalizeMsfLayout()) |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 115 | return std::move(EC); |
| 116 | } |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 117 | if (Tpi) { |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 118 | if (auto EC = Tpi->finalizeMsfLayout()) |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 119 | return std::move(EC); |
| 120 | } |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 121 | if (Ipi) { |
| 122 | if (auto EC = Ipi->finalizeMsfLayout()) |
| 123 | return std::move(EC); |
| 124 | } |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 125 | if (Gsi) { |
| 126 | if (auto EC = Gsi->finalizeMsfLayout()) |
Zachary Turner | 7eaf1d9 | 2017-07-10 22:40:20 +0000 | [diff] [blame] | 127 | return std::move(EC); |
| 128 | if (Dbi) { |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 129 | Dbi->setPublicsStreamIndex(Gsi->getPublicsStreamIndex()); |
| 130 | Dbi->setGlobalsStreamIndex(Gsi->getGlobalsStreamIndex()); |
| 131 | Dbi->setSymbolRecordStreamIndex(Gsi->getRecordStreamIdx()); |
Zachary Turner | 7eaf1d9 | 2017-07-10 22:40:20 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 134 | |
Zachary Turner | 199f48a | 2016-07-28 19:11:09 +0000 | [diff] [blame] | 135 | return Msf->build(); |
| 136 | } |
| 137 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 138 | Expected<uint32_t> PDBFileBuilder::getNamedStreamIndex(StringRef Name) const { |
| 139 | uint32_t SN = 0; |
| 140 | if (!NamedStreams.get(Name, SN)) |
| 141 | return llvm::make_error<pdb::RawError>(raw_error_code::no_stream); |
| 142 | return SN; |
| 143 | } |
| 144 | |
Zachary Turner | 9fb9d71 | 2017-08-02 22:31:39 +0000 | [diff] [blame] | 145 | void PDBFileBuilder::commitFpm(WritableBinaryStream &MsfBuffer, |
| 146 | const MSFLayout &Layout) { |
| 147 | auto FpmStream = |
| 148 | WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator); |
| 149 | |
| 150 | // We only need to create the alt fpm stream so that it gets initialized. |
| 151 | WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator, |
| 152 | true); |
| 153 | |
| 154 | uint32_t BI = 0; |
| 155 | BinaryStreamWriter FpmWriter(*FpmStream); |
| 156 | while (BI < Layout.SB->NumBlocks) { |
| 157 | uint8_t ThisByte = 0; |
| 158 | for (uint32_t I = 0; I < 8; ++I) { |
| 159 | bool IsFree = |
| 160 | (BI < Layout.SB->NumBlocks) ? Layout.FreePageMap.test(BI) : true; |
| 161 | uint8_t Mask = uint8_t(IsFree) << I; |
| 162 | ThisByte |= Mask; |
| 163 | ++BI; |
| 164 | } |
| 165 | cantFail(FpmWriter.writeObject(ThisByte)); |
| 166 | } |
| 167 | assert(FpmWriter.bytesRemaining() == 0); |
| 168 | } |
| 169 | |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 170 | Error PDBFileBuilder::commit(StringRef Filename) { |
Bob Haarman | de33a637 | 2017-05-17 20:46:48 +0000 | [diff] [blame] | 171 | assert(!Filename.empty()); |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 172 | auto ExpectedLayout = finalizeMsfLayout(); |
| 173 | if (!ExpectedLayout) |
| 174 | return ExpectedLayout.takeError(); |
| 175 | auto &Layout = *ExpectedLayout; |
| 176 | |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 177 | uint64_t Filesize = Layout.SB->BlockSize * Layout.SB->NumBlocks; |
| 178 | auto OutFileOrError = FileOutputBuffer::create(Filename, Filesize); |
| 179 | if (OutFileOrError.getError()) |
| 180 | return llvm::make_error<pdb::GenericError>(generic_error_code::invalid_path, |
| 181 | Filename); |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 182 | FileBufferByteStream Buffer(std::move(*OutFileOrError), |
| 183 | llvm::support::little); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 184 | BinaryStreamWriter Writer(Buffer); |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 185 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 186 | if (auto EC = Writer.writeObject(*Layout.SB)) |
| 187 | return EC; |
Zachary Turner | 9fb9d71 | 2017-08-02 22:31:39 +0000 | [diff] [blame] | 188 | |
| 189 | commitFpm(Buffer, Layout); |
| 190 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 191 | uint32_t BlockMapOffset = |
| 192 | msf::blockToOffset(Layout.SB->BlockMapAddr, Layout.SB->BlockSize); |
| 193 | Writer.setOffset(BlockMapOffset); |
| 194 | if (auto EC = Writer.writeArray(Layout.DirectoryBlocks)) |
| 195 | return EC; |
| 196 | |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 197 | auto DirStream = WritableMappedBlockStream::createDirectoryStream( |
| 198 | Layout, Buffer, Allocator); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 199 | BinaryStreamWriter DW(*DirStream); |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 200 | if (auto EC = DW.writeInteger<uint32_t>(Layout.StreamSizes.size())) |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 201 | return EC; |
| 202 | |
| 203 | if (auto EC = DW.writeArray(Layout.StreamSizes)) |
| 204 | return EC; |
| 205 | |
| 206 | for (const auto &Blocks : Layout.StreamMap) { |
| 207 | if (auto EC = DW.writeArray(Blocks)) |
| 208 | return EC; |
| 209 | } |
| 210 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 211 | auto ExpectedSN = getNamedStreamIndex("/names"); |
| 212 | if (!ExpectedSN) |
| 213 | return ExpectedSN.takeError(); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 214 | |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 215 | auto NS = WritableMappedBlockStream::createIndexedStream( |
| 216 | Layout, Buffer, *ExpectedSN, Allocator); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 217 | BinaryStreamWriter NSWriter(*NS); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 218 | if (auto EC = Strings.commit(NSWriter)) |
| 219 | return EC; |
| 220 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 221 | if (Info) { |
| 222 | if (auto EC = Info->commit(Layout, Buffer)) |
| 223 | return EC; |
| 224 | } |
| 225 | |
| 226 | if (Dbi) { |
| 227 | if (auto EC = Dbi->commit(Layout, Buffer)) |
| 228 | return EC; |
| 229 | } |
| 230 | |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 231 | if (Tpi) { |
| 232 | if (auto EC = Tpi->commit(Layout, Buffer)) |
| 233 | return EC; |
| 234 | } |
| 235 | |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 236 | if (Ipi) { |
| 237 | if (auto EC = Ipi->commit(Layout, Buffer)) |
| 238 | return EC; |
| 239 | } |
| 240 | |
Zachary Turner | 946204c | 2017-08-09 04:23:25 +0000 | [diff] [blame] | 241 | if (Gsi) { |
| 242 | if (auto EC = Gsi->commit(Layout, Buffer)) |
Zachary Turner | 8d927b6 | 2017-07-31 19:36:08 +0000 | [diff] [blame] | 243 | return EC; |
| 244 | } |
| 245 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 246 | return Buffer.commit(); |
Rui Ueyama | fc22cef | 2016-09-30 20:34:44 +0000 | [diff] [blame] | 247 | } |