llvm.org GIT mirror llvm / ab792ca
[PDB] Begin adding documentation for the PDB file format. Differential Revision: https://reviews.llvm.org/D26374 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286491 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 2 years ago
10 changed file(s) with 306 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 =====================================
1 The PDB DBI (Debug Info) Stream
2 =====================================
0 =====================================
1 The PDB Global Symbol Stream
2 =====================================
0 =====================================
1 The TPI & IPI Hash Streams
2 =====================================
0 =====================================
1 The Module Information Stream
2 =====================================
0 =====================================
1 The MSF File Format
2 =====================================
3
4 .. contents::
5 :local:
6
7 .. _msf_superblock:
8
9 The Superblock
10 ==============
11 At file offset 0 in an MSF file is the MSF *SuperBlock*, which is laid out as
12 follows:
13
14 .. code-block:: c++
15
16 struct SuperBlock {
17 char FileMagic[sizeof(Magic)];
18 ulittle32_t BlockSize;
19 ulittle32_t FreeBlockMapBlock;
20 ulittle32_t NumBlocks;
21 ulittle32_t NumDirectoryBytes;
22 ulittle32_t Unknown;
23 ulittle32_t BlockMapAddr;
24 };
25
26 - **FileMagic** - Must be equal to ``"Microsoft C / C++ MSF 7.00\\r\\n"``
27 followed by the bytes ``1A 44 53 00 00 00``.
28 - **BlockSize** - The block size of the internal file system. Valid values are
29 512, 1024, 2048, and 4096 bytes. Certain aspects of the MSF file layout vary
30 depending on the block sizes. For the purposes of LLVM, we handle only block
31 sizes of 4KiB, and all further discussion assumes a block size of 4KiB.
32 - **FreeBlockMapBlock** - The index of a block within the file, at which begins
33 a bitfield representing the set of all blocks within the file which are "free"
34 (i.e. the data within that block is not used). This bitfield is spread across
35 the MSF file at ``BlockSize`` intervals.
36 **Important**: ``FreeBlockMapBlock`` can only be ``1`` or ``2``! This field
37 is designed to support incremental and atomic updates of the underlying MSF
38 file. While writing to an MSF file, if the value of this field is `1`, you
39 can write your new modified bitfield to page 2, and vice versa. Only when
40 you commit the file to disk do you need to swap the value in the SuperBlock
41 to point to the new ``FreeBlockMapBlock``.
42 - **NumBlocks** - The total number of blocks in the file. ``NumBlocks * BlockSize``
43 should equal the size of the file on disk.
44 - **NumDirectoryBytes** - The size of the stream directory, in bytes. The stream
45 directory contains information about each stream's size and the set of blocks
46 that it occupies. It will be described in more detail later.
47 - **BlockMapAddr** - The index of a block within the MSF file. At this block is
48 an array of ``ulittle32_t``'s listing the blocks that the stream directory
49 resides on. For large MSF files, the stream directory (which describes the
50 block layout of each stream) may not fit entirely on a single block. As a
51 result, this extra layer of indirection is introduced, whereby this block
52 contains the list of blocks that the stream directory occupies, and the stream
53 directory itself can be stitched together accordingly. The number of
54 ``ulittle32_t``'s in this array is given by ``ceil(NumDirectoryBytes / BlockSize)``.
55
56 The Stream Directory
57 ====================
58 The Stream Directory is the root of all access to the other streams in an MSF
59 file. Beginning at byte 0 of the stream directory is the following structure:
60
61 .. code-block:: c++
62
63 struct StreamDirectory {
64 ulittle32_t NumStreams;
65 ulittle32_t StreamSizes[NumStreams];
66 ulittle32_t StreamBlocks[NumStreams][];
67 };
68
69 And this structure occupies exactly ``SuperBlock->NumDirectoryBytes`` bytes.
70 Note that each of the last two arrays is of variable length, and in particular
71 that the second array is jagged.
72
73 **Example:** Suppose a hypothetical PDB file with a 4KiB block size, and 4
74 streams of lengths {1000 bytes, 8000 bytes, 16000 bytes, 9000 bytes}.
75
76 Stream 0: ceil(1000 / 4096) = 1 block
77
78 Stream 1: ceil(8000 / 4096) = 2 blocks
79
80 Stream 2: ceil(16000 / 4096) = 4 blocks
81
82 Stream 3: ceil(9000 / 4096) = 3 blocks
83
84 In total, 10 blocks are used. Let's see what the stream directory might look
85 like:
86
87 .. code-block:: c++
88
89 struct StreamDirectory {
90 ulittle32_t NumStreams = 4;
91 ulittle32_t StreamSizes[] = {1000, 8000, 16000, 9000};
92 ulittle32_t StreamBlocks[][] = {
93 {4},
94 {5, 6},
95 {11, 9, 7, 8},
96 {10, 15, 12}
97 };
98 };
99
100 In total, this occupies ``15 * 4 = 60`` bytes, so ``SuperBlock->NumDirectoryBytes``
101 would equal ``60``, and ``SuperBlock->BlockMapAddr`` would be an array of one
102 ``ulittle32_t``, since ``60 <= SuperBlock->BlockSize``.
103
104 Note also that the streams are discontiguous, and that part of stream 3 is in the
105 middle of part of stream 2. You cannot assume anything about the layout of the
106 blocks!
107
108 Alignment and Block Boundaries
109 ==============================
110 As may be clear by now, it is possible for a single field (whether it be a high
111 level record, a long string field, or even a single ``uint16``) to begin and
112 end in separate blocks. For example, if the block size is 4096 bytes, and a
113 ``uint16`` field begins at the last byte of the current block, then it would
114 need to end on the first byte of the next block. Since blocks are not
115 necessarily contiguously laid out in the file, this means that both the consumer
116 and the producer of an MSF file must be prepared to split data apart
117 accordingly. In the aforementioned example, the high byte of the ``uint16``
118 would be written to the last byte of block N, and the low byte would be written
119 to the first byte of block N+1, which could be tens of thousands of bytes later
120 (or even earlier!) in the file, depending on what the stream directory says.
0 ========================================
1 The PDB Info Stream (aka the PDB Stream)
2 ========================================
0 =====================================
1 The PDB Public Symbol Stream
2 =====================================
0 =====================================
1 The PDB TPI Stream
2 =====================================
0 =====================================
1 The PDB File Format
2 =====================================
3
4 .. contents::
5 :local:
6
7 .. _pdb_intro:
8
9 Introduction
10 ============
11
12 PDB (Program Database) is a file format invented by Microsoft and which contains
13 debug information that can be consumed by debuggers and other tools. Since
14 officially supported APIs exist on Windows for querying debug information from
15 PDBs even without the user understanding the internals of the file format, a
16 large ecosystem of tools has been built for Windows to consume this format. In
17 order for Clang to be able to generate programs that can interoperate with these
18 tools, it is necessary for us to generate PDB files ourselves.
19
20 At the same time, LLVM has a long history of being able to cross-compile from
21 any platform to any platform, and we wish for the same to be true here. So it
22 is necessary for us to understand the PDB file format at the byte-level so that
23 we can generate PDB files entirely on our own.
24
25 This manual describes what we know about the PDB file format today. The layout
26 of the file, the various streams contained within, the format of individual
27 records within, and more.
28
29 We would like to extend our heartfelt gratitude to Microsoft, without whom we
30 would not be where we are today. Much of the knowledge contained within this
31 manual was learned through reading code published by Microsoft on their `GitHub
32 repo `__.
33
34 .. _pdb_layout:
35
36 File Layout
37 ===========
38
39 .. toctree::
40 :hidden:
41
42 MsfFile
43 PdbStream
44 TpiStream
45 DbiStream
46 ModiStream
47 PublicStream
48 GlobalStream
49 HashStream
50
51 .. _msf:
52
53 The MSF Container
54 -----------------
55 A PDB file is really just a special case of an MSF (Multi-Stream Format) file.
56 An MSF file is actually a miniature "file system within a file". It contains
57 multiple streams (aka files) which can represent arbitrary data, and these
58 streams are divided into blocks which may not necessarily be contiguously
59 laid out within the file (aka fragmented). Additionally, the MSF contains a
60 stream directory (aka MFT) which describes how the streams (files) are laid
61 out within the MSF.
62
63 For more information about the MSF container format, stream directory, and
64 block layout, see :doc:`MsfFile`.
65
66 .. _streams:
67
68 Streams
69 -------
70 The PDB format contains a number of streams which describe various information
71 such as the types, symbols, source files, and compilands (e.g. object files)
72 of a program, as well as some additional streams containing hash tables that are
73 used by debuggers and other tools to provide fast lookup of records and types
74 by name, and various other information about how the program was compiled such
75 as the specific toolchain used, and more. A summary of streams contained in a
76 PDB file is as follows:
77
78 +--------------------+------------------------------+-------------------------------------------+
79 | Name | Stream Index | Contents |
80 +====================+==============================+===========================================+
81 | Old Directory | - Fixed Stream Index 0 | - Previous MSF Stream Directory |
82 +--------------------+------------------------------+-------------------------------------------+
83 | PDB Stream | - Fixed Stream Index 1 | - Basic File Information |
84 | | | - Fields to match EXE to this PDB |
85 | | | - Map of named streams to stream indices |
86 +--------------------+------------------------------+-------------------------------------------+
87 | TPI Stream | - Fixed Stream Index 2 | - CodeView Type Records |
88 | | | - Index of TPI Hash Stream |
89 +--------------------+------------------------------+-------------------------------------------+
90 | DBI Stream | - Fixed Stream Index 3 | - Module/Compiland Information |
91 | | | - Indices of individual module streams |
92 | | | - Indices of public / global streams |
93 | | | - Section Contribution Information |
94 | | | - Source File Information |
95 | | | - FPO / PGO Data |
96 +--------------------+------------------------------+-------------------------------------------+
97 | IPI Stream | - Fixed Stream Index 4 | - CodeView Type Records |
98 | | | - Index of IPI Hash Stream |
99 +--------------------+------------------------------+-------------------------------------------+
100 | /LinkInfo | - Contained in PDB Stream | - Unknown |
101 | | Named Stream map | |
102 +--------------------+------------------------------+-------------------------------------------+
103 | /src/headerblock | - Contained in PDB Stream | - Unknown |
104 | | Named Stream map | |
105 +--------------------+------------------------------+-------------------------------------------+
106 | /names | - Contained in PDB Stream | - PDB-wide global string table used for |
107 | | Named Stream map | string de-duplication |
108 +--------------------+------------------------------+-------------------------------------------+
109 | Module Info Stream | - Contained in DBI Stream | - CodeView Symbol Records for this module |
110 | | - One for each compiland | - Line Number Information |
111 +--------------------+------------------------------+-------------------------------------------+
112 | Public Stream | - Contained in DBI Stream | - Public (Exported) Symbol Records |
113 | | | - Index of Public Hash Stream |
114 +--------------------+------------------------------+-------------------------------------------+
115 | Global Stream | - Contained in DBI Stream | - Global Symbol Records |
116 | | | - Index of Global Hash Stream |
117 +--------------------+------------------------------+-------------------------------------------+
118 | TPI Hash Stream | - Contained in TPI Stream | - Hash table for looking up TPI records |
119 | | | by name |
120 +--------------------+------------------------------+-------------------------------------------+
121 | IPI Hash Stream | - Contained in IPI Stream | - Hash table for looking up IPI records |
122 | | | by name |
123 +--------------------+------------------------------+-------------------------------------------+
124
125 More information about the structure of each of these can be found on the
126 following pages:
127
128 :doc:`PdbStream`
129 Information about the PDB Info Stream and how it is used to match PDBs to EXEs.
130
131 :doc:`TpiStream`
132 Information about the TPI stream and the CodeView records contained within.
133
134 :doc:`DbiStream`
135 Information about the DBI stream and relevant substreams including the Module Substreams,
136 source file information, and CodeView symbol records contained within.
137
138 :doc:`ModiStream`
139 Information about the Module Information Stream, of which there is one for each compilation
140 unit and the format of symbols contained within.
141
142 :doc:`PublicStream`
143 Information about the Public Symbol Stream.
144
145 :doc:`GlobalStream`
146 Information about the Global Symbol Stream.
147
148 :doc:`HashStream`
149 Information about the Hash Table stream, and how it can be used to quickly look up records
150 by name.
151
152 CodeView
153 ========
154 CodeView is another format which comes into the picture. While MSF defines
155 the structure of the overall file, and PDB defines the set of streams that
156 appear within the MSF file and the format of those streams, CodeView defines
157 the format of **symbol and type records** that appear within specific streams.
158 Refer to the pages on `CodeView Symbol Records` and `CodeView Type Records` for
159 more information about the CodeView format.
273273 Coroutines
274274 GlobalISel
275275 XRay
276 PDB/index
276277
277278 :doc:`WritingAnLLVMPass`
278279 Information on how to write LLVM transformations and analyses.
396397
397398 :doc:`XRay`
398399 High-level documentation of how to use XRay in LLVM.
400
401 :doc:`The Microsoft PDB File Format `
402 A detailed description of the Microsoft PDB (Program Database) file format.
399403
400404 Development Process Documentation
401405 =================================