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 | |
| 10 | #include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h" |
| 11 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/BitVector.h" |
| 13 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/CodeView/StreamInterface.h" |
| 15 | #include "llvm/DebugInfo/CodeView/StreamWriter.h" |
| 16 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
| 17 | #include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h" |
| 18 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
| 19 | #include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h" |
| 20 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace llvm::codeview; |
| 24 | using namespace llvm::pdb; |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 25 | using namespace llvm::support; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 26 | |
| 27 | PDBFileBuilder::PDBFileBuilder( |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 28 | std::unique_ptr<codeview::StreamInterface> FileBuffer) |
| 29 | : File(llvm::make_unique<PDBFile>(std::move(FileBuffer))) {} |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 30 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 31 | Error PDBFileBuilder::initialize(const msf::SuperBlock &Super) { |
| 32 | auto ExpectedMsf = |
| 33 | MsfBuilder::create(File->Allocator, Super.BlockSize, Super.NumBlocks); |
| 34 | if (!ExpectedMsf) |
| 35 | return ExpectedMsf.takeError(); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 36 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 37 | auto &MsfResult = *ExpectedMsf; |
| 38 | if (auto EC = MsfResult.setBlockMapAddr(Super.BlockMapAddr)) |
| 39 | return EC; |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 40 | Msf = llvm::make_unique<MsfBuilder>(std::move(MsfResult)); |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame^] | 41 | Msf->setFreePageMap(Super.FreeBlockMapBlock); |
| 42 | Msf->setUnknown1(Super.Unknown1); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 43 | return Error::success(); |
| 44 | } |
| 45 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 46 | MsfBuilder &PDBFileBuilder::getMsfBuilder() { return *Msf; } |
| 47 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 48 | InfoStreamBuilder &PDBFileBuilder::getInfoBuilder() { |
| 49 | if (!Info) |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 50 | Info = llvm::make_unique<InfoStreamBuilder>(); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 51 | return *Info; |
| 52 | } |
| 53 | |
| 54 | DbiStreamBuilder &PDBFileBuilder::getDbiBuilder() { |
| 55 | if (!Dbi) |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 56 | Dbi = llvm::make_unique<DbiStreamBuilder>(); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 57 | return *Dbi; |
| 58 | } |
| 59 | |
| 60 | Expected<std::unique_ptr<PDBFile>> PDBFileBuilder::build() { |
| 61 | if (Info) { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 62 | uint32_t Length = Info->calculateSerializedLength(); |
| 63 | if (auto EC = Msf->setStreamSize(StreamPDB, Length)) |
| 64 | return std::move(EC); |
| 65 | } |
| 66 | if (Dbi) { |
| 67 | uint32_t Length = Dbi->calculateSerializedLength(); |
| 68 | if (auto EC = Msf->setStreamSize(StreamDBI, Length)) |
| 69 | return std::move(EC); |
| 70 | } |
| 71 | |
| 72 | auto ExpectedLayout = Msf->build(); |
| 73 | if (!ExpectedLayout) |
| 74 | return ExpectedLayout.takeError(); |
| 75 | |
| 76 | const msf::Layout &L = *ExpectedLayout; |
| 77 | File->StreamMap = L.StreamMap; |
| 78 | File->StreamSizes = L.StreamSizes; |
| 79 | File->DirectoryBlocks = L.DirectoryBlocks; |
| 80 | File->SB = L.SB; |
| 81 | |
| 82 | if (Info) { |
| 83 | auto ExpectedInfo = Info->build(*File); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 84 | if (!ExpectedInfo) |
| 85 | return ExpectedInfo.takeError(); |
| 86 | File->Info = std::move(*ExpectedInfo); |
| 87 | } |
| 88 | |
| 89 | if (Dbi) { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 90 | auto ExpectedDbi = Dbi->build(*File); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 91 | if (!ExpectedDbi) |
| 92 | return ExpectedDbi.takeError(); |
| 93 | File->Dbi = std::move(*ExpectedDbi); |
| 94 | } |
| 95 | |
| 96 | if (File->Info && File->Dbi && File->Info->getAge() != File->Dbi->getAge()) |
| 97 | return llvm::make_error<RawError>( |
| 98 | raw_error_code::corrupt_file, |
| 99 | "PDB Stream Age doesn't match Dbi Stream Age!"); |
| 100 | |
| 101 | return std::move(File); |
| 102 | } |