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 | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Endian.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 14 | #include "llvm/Support/FileOutputBuffer.h" |
| 15 | #include <memory> |
| 16 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame^] | 17 | using namespace lld; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 19 | using namespace llvm::support; |
| 20 | using namespace llvm::support::endian; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 21 | |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 22 | const int BlockSize = 4096; |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 23 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame^] | 24 | void coff::createPDB(StringRef Path) { |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 25 | // Create a file. |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 26 | size_t FileSize = BlockSize * 3; |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 27 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 28 | FileOutputBuffer::create(Path, FileSize); |
Rui Ueyama | 0d09a86 | 2016-07-15 00:40:46 +0000 | [diff] [blame] | 29 | if (auto EC = BufferOrErr.getError()) |
| 30 | fatal(EC, "failed to open " + Path); |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 31 | std::unique_ptr<FileOutputBuffer> Buffer = std::move(*BufferOrErr); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 32 | |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 33 | // Write the file header. |
| 34 | uint8_t *Buf = Buffer->getBufferStart(); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 35 | auto *SB = reinterpret_cast<msf::SuperBlock *>(Buf); |
| 36 | memcpy(SB->MagicBytes, msf::Magic, sizeof(msf::Magic)); |
| 37 | SB->BlockSize = BlockSize; |
| 38 | |
| 39 | // FreeBlockMap is a page number containing free page map bitmap. |
| 40 | // Set a dummy value for now. |
| 41 | SB->FreeBlockMapBlock = 1; |
| 42 | |
| 43 | SB->NumBlocks = FileSize / BlockSize; |
| 44 | |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 45 | // Root directory is empty, containing only the length field. |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 46 | SB->NumDirectoryBytes = 4; |
| 47 | |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 48 | // Root directory is on page 1. |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 49 | SB->BlockMapAddr = 1; |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 50 | |
| 51 | // Write the root directory. Root stream is on page 2. |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 52 | write32le(Buf + BlockSize, 2); |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 53 | Buffer->commit(); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 54 | } |