Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 1 | //===- PDB.cpp ------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 10 | #include "PDB.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 11 | #include "Error.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 12 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 14 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
| 15 | #include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h" |
| 16 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
| 17 | #include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h" |
| 18 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
| 19 | #include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h" |
| 20 | #include "llvm/DebugInfo/PDB/Raw/TpiStream.h" |
| 21 | #include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h" |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Endian.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileOutputBuffer.h" |
| 24 | #include <memory> |
| 25 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 26 | using namespace lld; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 28 | using namespace llvm::support; |
| 29 | using namespace llvm::support::endian; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 30 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 31 | static ExitOnError ExitOnErr; |
| 32 | |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 33 | const int BlockSize = 4096; |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 34 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 35 | void coff::createPDB(StringRef Path) { |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 36 | // Create a file. |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 37 | size_t FileSize = BlockSize * 10; |
| 38 | auto BufferOrErr = FileOutputBuffer::create(Path, FileSize); |
Rui Ueyama | 0d09a86 | 2016-07-15 00:40:46 +0000 | [diff] [blame] | 39 | if (auto EC = BufferOrErr.getError()) |
| 40 | fatal(EC, "failed to open " + Path); |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 41 | auto FileByteStream = |
| 42 | llvm::make_unique<msf::FileBufferByteStream>(std::move(*BufferOrErr)); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 43 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 44 | // Create the superblock. |
| 45 | msf::SuperBlock SB; |
| 46 | memcpy(SB.MagicBytes, msf::Magic, sizeof(msf::Magic)); |
| 47 | SB.BlockSize = 4096; |
| 48 | SB.FreeBlockMapBlock = 2; |
| 49 | SB.NumBlocks = 10; |
| 50 | SB.NumDirectoryBytes = 0; |
| 51 | SB.Unknown1 = 0; |
| 52 | SB.BlockMapAddr = 9; |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 53 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 54 | BumpPtrAllocator Alloc; |
| 55 | pdb::PDBFileBuilder Builder(Alloc); |
| 56 | ExitOnErr(Builder.initialize(SB)); |
| 57 | ExitOnErr(Builder.getMsfBuilder().setDirectoryBlocksHint({8})); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 58 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 59 | ExitOnErr(Builder.getMsfBuilder().addStream(1, {4})); |
| 60 | ExitOnErr(Builder.getMsfBuilder().addStream(1, {5})); |
| 61 | ExitOnErr(Builder.getMsfBuilder().addStream(1, {6})); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 62 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 63 | // Add an empty IPI stream. |
| 64 | Builder.getInfoBuilder(); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 65 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 66 | // Add an empty TPI stream. |
| 67 | auto &TpiBuilder = Builder.getTpiBuilder(); |
| 68 | TpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 69 | |
| 70 | // Write the root directory. Root stream is on page 2. |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame^] | 71 | ExitOnErr(Builder.commit(*FileByteStream)); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 72 | } |