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 10 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" 11 12 #include "llvm/ADT/BitVector.h" 13 14 #include "llvm/DebugInfo/MSF/MSFBuilder.h" 15 #include "llvm/DebugInfo/PDB/Native/DbiStream.h" 16 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" 17 #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h" 18 #include "llvm/DebugInfo/PDB/Native/InfoStream.h" 19 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" 20 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" 21 #include "llvm/DebugInfo/PDB/Native/RawError.h" 22 #include "llvm/DebugInfo/PDB/Native/TpiStream.h" 23 #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" 24 #include "llvm/Support/BinaryStream.h" 25 #include "llvm/Support/BinaryStreamWriter.h" 26 #include "llvm/Support/JamCRC.h" 27 #include "llvm/Support/Path.h" 28 29 using namespace llvm; 30 using namespace llvm::codeview; 31 using namespace llvm::msf; 32 using namespace llvm::pdb; 33 using namespace llvm::support; 34 35 PDBFileBuilder::PDBFileBuilder(BumpPtrAllocator &Allocator) 36 : Allocator(Allocator), InjectedSourceHashTraits(Strings), 37 InjectedSourceTable(2, InjectedSourceHashTraits) {} 38 39 PDBFileBuilder::~PDBFileBuilder() {} 40 41 Error PDBFileBuilder::initialize(uint32_t BlockSize) { 42 auto ExpectedMsf = MSFBuilder::create(Allocator, BlockSize); 43 if (!ExpectedMsf) 44 return ExpectedMsf.takeError(); 45 Msf = llvm::make_unique<MSFBuilder>(std::move(*ExpectedMsf)); 46 return Error::success(); 47 } 48 49 MSFBuilder &PDBFileBuilder::getMsfBuilder() { return *Msf; } 50 51 InfoStreamBuilder &PDBFileBuilder::getInfoBuilder() { 52 if (!Info) 53 Info = llvm::make_unique<InfoStreamBuilder>(*Msf, NamedStreams); 54 return *Info; 55 } 56 57 DbiStreamBuilder &PDBFileBuilder::getDbiBuilder() { 58 if (!Dbi) 59 Dbi = llvm::make_unique<DbiStreamBuilder>(*Msf); 60 return *Dbi; 61 } 62 63 TpiStreamBuilder &PDBFileBuilder::getTpiBuilder() { 64 if (!Tpi) 65 Tpi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamTPI); 66 return *Tpi; 67 } 68 69 TpiStreamBuilder &PDBFileBuilder::getIpiBuilder() { 70 if (!Ipi) 71 Ipi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamIPI); 72 return *Ipi; 73 } 74 75 PDBStringTableBuilder &PDBFileBuilder::getStringTableBuilder() { 76 return Strings; 77 } 78 79 GSIStreamBuilder &PDBFileBuilder::getGsiBuilder() { 80 if (!Gsi) 81 Gsi = llvm::make_unique<GSIStreamBuilder>(*Msf); 82 return *Gsi; 83 } 84 85 Expected<uint32_t> PDBFileBuilder::allocateNamedStream(StringRef Name, 86 uint32_t Size) { 87 auto ExpectedStream = Msf->addStream(Size); 88 if (ExpectedStream) 89 NamedStreams.set(Name, *ExpectedStream); 90 return ExpectedStream; 91 } 92 93 Error PDBFileBuilder::addNamedStream(StringRef Name, StringRef Data) { 94 Expected<uint32_t> ExpectedIndex = allocateNamedStream(Name, Data.size()); 95 if (!ExpectedIndex) 96 return ExpectedIndex.takeError(); 97 assert(NamedStreamData.count(*ExpectedIndex) == 0); 98 NamedStreamData[*ExpectedIndex] = Data; 99 return Error::success(); 100 } 101 102 void PDBFileBuilder::addInjectedSource(StringRef Name, 103 std::unique_ptr<MemoryBuffer> Buffer) { 104 // Stream names must be exact matches, since they get looked up in a hash 105 // table and the hash value is dependent on the exact contents of the string. 106 // link.exe lowercases a path and converts / to \, so we must do the same. 107 SmallString<64> VName; 108 sys::path::native(Name.lower(), VName); 109 110 uint32_t NI = getStringTableBuilder().insert(Name); 111 uint32_t VNI = getStringTableBuilder().insert(VName); 112 113 InjectedSourceDescriptor Desc; 114 Desc.Content = std::move(Buffer); 115 Desc.NameIndex = NI; 116 Desc.VNameIndex = VNI; 117 Desc.StreamName = "/src/files/"; 118 119 Desc.StreamName += VName; 120 121 InjectedSources.push_back(std::move(Desc)); 122 } 123 124 Error PDBFileBuilder::finalizeMsfLayout() { 125 126 if (Ipi && Ipi->getRecordCount() > 0) { 127 // In theory newer PDBs always have an ID stream, but by saying that we're 128 // only going to *really* have an ID stream if there is at least one ID 129 // record, we leave open the opportunity to test older PDBs such as those 130 // that don't have an ID stream. 131 auto &Info = getInfoBuilder(); 132 Info.addFeature(PdbRaw_FeatureSig::VC140); 133 } 134 135 uint32_t StringsLen = Strings.calculateSerializedSize(); 136 137 Expected<uint32_t> SN = allocateNamedStream("/LinkInfo", 0); 138 if (!SN) 139 return SN.takeError(); 140 141 if (Gsi) { 142 if (auto EC = Gsi->finalizeMsfLayout()) 143 return EC; 144 if (Dbi) { 145 Dbi->setPublicsStreamIndex(Gsi->getPublicsStreamIndex()); 146 Dbi->setGlobalsStreamIndex(Gsi->getGlobalsStreamIndex()); 147 Dbi->setSymbolRecordStreamIndex(Gsi->getRecordStreamIdx()); 148 } 149 } 150 if (Tpi) { 151 if (auto EC = Tpi->finalizeMsfLayout()) 152 return EC; 153 } 154 if (Dbi) { 155 if (auto EC = Dbi->finalizeMsfLayout()) 156 return EC; 157 } 158 SN = allocateNamedStream("/names", StringsLen); 159 if (!SN) 160 return SN.takeError(); 161 162 if (Ipi) { 163 if (auto EC = Ipi->finalizeMsfLayout()) 164 return EC; 165 } 166 167 // Do this last, since it relies on the named stream map being complete, and 168 // that can be updated by previous steps in the finalization. 169 if (Info) { 170 if (auto EC = Info->finalizeMsfLayout()) 171 return EC; 172 } 173 174 if (!InjectedSources.empty()) { 175 for (const auto &IS : InjectedSources) { 176 JamCRC CRC(0); 177 CRC.update(makeArrayRef(IS.Content->getBufferStart(), 178 IS.Content->getBufferSize())); 179 180 SrcHeaderBlockEntry Entry; 181 ::memset(&Entry, 0, sizeof(SrcHeaderBlockEntry)); 182 Entry.Size = sizeof(SrcHeaderBlockEntry); 183 Entry.FileSize = IS.Content->getBufferSize(); 184 Entry.FileNI = IS.NameIndex; 185 Entry.VFileNI = IS.VNameIndex; 186 Entry.ObjNI = 1; 187 Entry.IsVirtual = 0; 188 Entry.Version = 189 static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne); 190 Entry.CRC = CRC.getCRC(); 191 StringRef VName = getStringTableBuilder().getStringForId(IS.VNameIndex); 192 InjectedSourceTable.set_as(VName, std::move(Entry)); 193 } 194 195 uint32_t SrcHeaderBlockSize = 196 sizeof(SrcHeaderBlockHeader) + 197 InjectedSourceTable.calculateSerializedLength(); 198 SN = allocateNamedStream("/src/headerblock", SrcHeaderBlockSize); 199 if (!SN) 200 return SN.takeError(); 201 for (const auto &IS : InjectedSources) { 202 SN = allocateNamedStream(IS.StreamName, IS.Content->getBufferSize()); 203 if (!SN) 204 return SN.takeError(); 205 } 206 } 207 208 // Do this last, since it relies on the named stream map being complete, and 209 // that can be updated by previous steps in the finalization. 210 if (Info) { 211 if (auto EC = Info->finalizeMsfLayout()) 212 return EC; 213 } 214 215 return Error::success(); 216 } 217 218 Expected<uint32_t> PDBFileBuilder::getNamedStreamIndex(StringRef Name) const { 219 uint32_t SN = 0; 220 if (!NamedStreams.get(Name, SN)) 221 return llvm::make_error<pdb::RawError>(raw_error_code::no_stream); 222 return SN; 223 } 224 225 void PDBFileBuilder::commitSrcHeaderBlock(WritableBinaryStream &MsfBuffer, 226 const msf::MSFLayout &Layout) { 227 assert(!InjectedSourceTable.empty()); 228 229 uint32_t SN = cantFail(getNamedStreamIndex("/src/headerblock")); 230 auto Stream = WritableMappedBlockStream::createIndexedStream( 231 Layout, MsfBuffer, SN, Allocator); 232 BinaryStreamWriter Writer(*Stream); 233 234 SrcHeaderBlockHeader Header; 235 ::memset(&Header, 0, sizeof(Header)); 236 Header.Version = static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne); 237 Header.Size = Writer.bytesRemaining(); 238 239 cantFail(Writer.writeObject(Header)); 240 cantFail(InjectedSourceTable.commit(Writer)); 241 242 assert(Writer.bytesRemaining() == 0); 243 } 244 245 void PDBFileBuilder::commitInjectedSources(WritableBinaryStream &MsfBuffer, 246 const msf::MSFLayout &Layout) { 247 if (InjectedSourceTable.empty()) 248 return; 249 250 commitSrcHeaderBlock(MsfBuffer, Layout); 251 252 for (const auto &IS : InjectedSources) { 253 uint32_t SN = cantFail(getNamedStreamIndex(IS.StreamName)); 254 255 auto SourceStream = WritableMappedBlockStream::createIndexedStream( 256 Layout, MsfBuffer, SN, Allocator); 257 BinaryStreamWriter SourceWriter(*SourceStream); 258 assert(SourceWriter.bytesRemaining() == IS.Content->getBufferSize()); 259 cantFail(SourceWriter.writeBytes( 260 arrayRefFromStringRef(IS.Content->getBuffer()))); 261 } 262 } 263 264 Error PDBFileBuilder::commit(StringRef Filename) { 265 assert(!Filename.empty()); 266 if (auto EC = finalizeMsfLayout()) 267 return EC; 268 269 MSFLayout Layout; 270 auto ExpectedMsfBuffer = Msf->commit(Filename, Layout); 271 if (!ExpectedMsfBuffer) 272 return ExpectedMsfBuffer.takeError(); 273 FileBufferByteStream Buffer = std::move(*ExpectedMsfBuffer); 274 275 auto ExpectedSN = getNamedStreamIndex("/names"); 276 if (!ExpectedSN) 277 return ExpectedSN.takeError(); 278 279 auto NS = WritableMappedBlockStream::createIndexedStream( 280 Layout, Buffer, *ExpectedSN, Allocator); 281 BinaryStreamWriter NSWriter(*NS); 282 if (auto EC = Strings.commit(NSWriter)) 283 return EC; 284 285 for (const auto &NSE : NamedStreamData) { 286 if (NSE.second.empty()) 287 continue; 288 289 auto NS = WritableMappedBlockStream::createIndexedStream( 290 Layout, Buffer, NSE.first, Allocator); 291 BinaryStreamWriter NSW(*NS); 292 if (auto EC = NSW.writeBytes(arrayRefFromStringRef(NSE.second))) 293 return EC; 294 } 295 296 if (Info) { 297 if (auto EC = Info->commit(Layout, Buffer)) 298 return EC; 299 } 300 301 if (Dbi) { 302 if (auto EC = Dbi->commit(Layout, Buffer)) 303 return EC; 304 } 305 306 if (Tpi) { 307 if (auto EC = Tpi->commit(Layout, Buffer)) 308 return EC; 309 } 310 311 if (Ipi) { 312 if (auto EC = Ipi->commit(Layout, Buffer)) 313 return EC; 314 } 315 316 if (Gsi) { 317 if (auto EC = Gsi->commit(Layout, Buffer)) 318 return EC; 319 } 320 321 auto InfoStreamBlocks = Layout.StreamMap[StreamPDB]; 322 assert(!InfoStreamBlocks.empty()); 323 uint64_t InfoStreamFileOffset = 324 blockToOffset(InfoStreamBlocks.front(), Layout.SB->BlockSize); 325 InfoStreamHeader *H = reinterpret_cast<InfoStreamHeader *>( 326 Buffer.getBufferStart() + InfoStreamFileOffset); 327 328 commitInjectedSources(Buffer, Layout); 329 330 // Set the build id at the very end, after every other byte of the PDB 331 // has been written. 332 // FIXME: Use a hash of the PDB rather than time(nullptr) for the signature. 333 H->Age = Info->getAge(); 334 H->Guid = Info->getGuid(); 335 Optional<uint32_t> Sig = Info->getSignature(); 336 H->Signature = Sig.hasValue() ? *Sig : time(nullptr); 337 338 return Buffer.commit(); 339 } 340