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