OSDN Git Service

Revert "[docs] As of binutils 2.21.51.0.2, ld.bfd supports plugins too, represent...
[android-x86/external-llvm.git] / docs / PDB / MsfFile.rst
1 =====================================\r
2 The MSF File Format\r
3 =====================================\r
4 \r
5 .. contents::\r
6    :local:\r
7 \r
8 .. _msf_layout:\r
9 \r
10 File Layout\r
11 ===========\r
12 \r
13 The MSF file format consists of the following components:\r
14 \r
15 1. :ref:`msf_superblock`\r
16 2. :ref:`msf_freeblockmap` (also know as Free Page Map, or FPM)\r
17 3. Data\r
18 \r
19 Each component is stored as an indexed block, the length of which is specified\r
20 in ``SuperBlock::BlockSize``. The file consists of 1 or more iterations of the\r
21 following pattern (sometimes referred to as an "interval"):\r
22 \r
23 1. 1 block of data\r
24 2. Free Block Map 1 (corresponds to ``SuperBlock::FreeBlockMapBlock`` 1)\r
25 3. Free Block Map 2 (corresponds to ``SuperBlock::FreeBlockMapBlock`` 2)\r
26 4. ``SuperBlock::BlockSize - 3`` blocks of data\r
27 \r
28 In the first interval, the first data block is used to store\r
29 :ref:`msf_superblock`.\r
30 \r
31 The following diagram demonstrates the general layout of the file (\| denotes\r
32 the end of an interval, and is for visualization purposes only):\r
33 \r
34 +-------------+-----------------------+------------------+------------------+----------+----+------+------+------+-------------+----+-----+\r
35 | Block Index | 0                     | 1                | 2                | 3 - 4095 | \| | 4096 | 4097 | 4098 | 4099 - 8191 | \| | ... |\r
36 +=============+=======================+==================+==================+==========+====+======+======+======+=============+====+=====+\r
37 | Meaning     | :ref:`msf_superblock` | Free Block Map 1 | Free Block Map 2 | Data     | \| | Data | FPM1 | FPM2 | Data        | \| | ... |\r
38 +-------------+-----------------------+------------------+------------------+----------+----+------+------+------+-------------+----+-----+\r
39 \r
40 The file may end after any block, including immediately after a FPM1.\r
41 \r
42 .. note::\r
43   LLVM only supports 4096 byte blocks (sometimes referred to as the "BigMsf"\r
44   variant), so the rest of this document will assume a block size of 4096.\r
45 \r
46 .. _msf_superblock:\r
47 \r
48 The Superblock\r
49 ==============\r
50 At file offset 0 in an MSF file is the MSF *SuperBlock*, which is laid out as\r
51 follows:\r
52 \r
53 .. code-block:: c++\r
54 \r
55   struct SuperBlock {\r
56     char FileMagic[sizeof(Magic)];\r
57     ulittle32_t BlockSize;\r
58     ulittle32_t FreeBlockMapBlock;\r
59     ulittle32_t NumBlocks;\r
60     ulittle32_t NumDirectoryBytes;\r
61     ulittle32_t Unknown;\r
62     ulittle32_t BlockMapAddr;\r
63   };\r
64 \r
65 - **FileMagic** - Must be equal to ``"Microsoft C / C++ MSF 7.00\\r\\n"``\r
66   followed by the bytes ``1A 44 53 00 00 00``.\r
67 - **BlockSize** - The block size of the internal file system.  Valid values are\r
68   512, 1024, 2048, and 4096 bytes.  Certain aspects of the MSF file layout vary\r
69   depending on the block sizes.  For the purposes of LLVM, we handle only block\r
70   sizes of 4KiB, and all further discussion assumes a block size of 4KiB.\r
71 - **FreeBlockMapBlock** - The index of a block within the file, at which begins\r
72   a bitfield representing the set of all blocks within the file which are "free"\r
73   (i.e. the data within that block is not used).  See :ref:`msf_freeblockmap` for\r
74   more information.\r
75   **Important**: ``FreeBlockMapBlock`` can only be ``1`` or ``2``!\r
76 - **NumBlocks** - The total number of blocks in the file.  ``NumBlocks * BlockSize``\r
77   should equal the size of the file on disk.\r
78 - **NumDirectoryBytes** - The size of the stream directory, in bytes.  The stream\r
79   directory contains information about each stream's size and the set of blocks\r
80   that it occupies.  It will be described in more detail later.\r
81 - **BlockMapAddr** - The index of a block within the MSF file.  At this block is\r
82   an array of ``ulittle32_t``'s listing the blocks that the stream directory\r
83   resides on.  For large MSF files, the stream directory (which describes the\r
84   block layout of each stream) may not fit entirely on a single block.  As a\r
85   result, this extra layer of indirection is introduced, whereby this block\r
86   contains the list of blocks that the stream directory occupies, and the stream\r
87   directory itself can be stitched together accordingly.  The number of\r
88   ``ulittle32_t``'s in this array is given by ``ceil(NumDirectoryBytes / BlockSize)``.\r
89 \r
90 .. _msf_freeblockmap:\r
91 \r
92 The Free Block Map\r
93 ==================\r
94 \r
95 The Free Block Map (sometimes referred to as the Free Page Map, or FPM) is a\r
96 series of blocks which contains a bit flag for every block in the file. The\r
97 flag will be set to 0 if the block is in use, and 1 if the block is unused.\r
98 \r
99 Each file contains two FPMs, one of which is active at any given time. This\r
100 feature is designed to support incremental and atomic updates of the underlying\r
101 MSF file. While writing to an MSF file, if the active FPM is FPM1, you can\r
102 write your new modified bitfield to FPM2, and vice versa. Only when you commit\r
103 the file to disk do you need to swap the value in the SuperBlock to point to\r
104 the new ``FreeBlockMapBlock``.\r
105 \r
106 The Free Block Maps are stored as a series of single blocks thoughout the file\r
107 at intervals of BlockSize. Because each FPM block is of size ``BlockSize``\r
108 bytes, it contains 8 times as many bits as an interval has blocks. This means\r
109 that the first block of each FPM refers to the first 8 intervals of the file\r
110 (the first 32768 blocks), the second block of each FPM refers to the next 8\r
111 blocks, and so on. This results in far more FPM blocks being present than are\r
112 required, but in order to maintain backwards compatibility the format must stay\r
113 this way.\r
114 \r
115 The Stream Directory\r
116 ====================\r
117 The Stream Directory is the root of all access to the other streams in an MSF\r
118 file.  Beginning at byte 0 of the stream directory is the following structure:\r
119 \r
120 .. code-block:: c++\r
121 \r
122   struct StreamDirectory {\r
123     ulittle32_t NumStreams;\r
124     ulittle32_t StreamSizes[NumStreams];\r
125     ulittle32_t StreamBlocks[NumStreams][];\r
126   };\r
127 \r
128 And this structure occupies exactly ``SuperBlock->NumDirectoryBytes`` bytes.\r
129 Note that each of the last two arrays is of variable length, and in particular\r
130 that the second array is jagged.\r
131 \r
132 **Example:** Suppose a hypothetical PDB file with a 4KiB block size, and 4\r
133 streams of lengths {1000 bytes, 8000 bytes, 16000 bytes, 9000 bytes}.\r
134 \r
135 Stream 0: ceil(1000 / 4096) = 1 block\r
136 \r
137 Stream 1: ceil(8000 / 4096) = 2 blocks\r
138 \r
139 Stream 2: ceil(16000 / 4096) = 4 blocks\r
140 \r
141 Stream 3: ceil(9000 / 4096) = 3 blocks\r
142 \r
143 In total, 10 blocks are used.  Let's see what the stream directory might look\r
144 like:\r
145 \r
146 .. code-block:: c++\r
147 \r
148   struct StreamDirectory {\r
149     ulittle32_t NumStreams = 4;\r
150     ulittle32_t StreamSizes[] = {1000, 8000, 16000, 9000};\r
151     ulittle32_t StreamBlocks[][] = {\r
152       {4},\r
153       {5, 6},\r
154       {11, 9, 7, 8},\r
155       {10, 15, 12}\r
156     };\r
157   };\r
158 \r
159 In total, this occupies ``15 * 4 = 60`` bytes, so ``SuperBlock->NumDirectoryBytes``\r
160 would equal ``60``, and ``SuperBlock->BlockMapAddr`` would be an array of one\r
161 ``ulittle32_t``, since ``60 <= SuperBlock->BlockSize``.\r
162 \r
163 Note also that the streams are discontiguous, and that part of stream 3 is in the\r
164 middle of part of stream 2.  You cannot assume anything about the layout of the\r
165 blocks!\r
166 \r
167 Alignment and Block Boundaries\r
168 ==============================\r
169 As may be clear by now, it is possible for a single field (whether it be a high\r
170 level record, a long string field, or even a single ``uint16``) to begin and\r
171 end in separate blocks.  For example, if the block size is 4096 bytes, and a\r
172 ``uint16`` field begins at the last byte of the current block, then it would\r
173 need to end on the first byte of the next block.  Since blocks are not\r
174 necessarily contiguously laid out in the file, this means that both the consumer\r
175 and the producer of an MSF file must be prepared to split data apart\r
176 accordingly.  In the aforementioned example, the high byte of the ``uint16``\r
177 would be written to the last byte of block N, and the low byte would be written\r
178 to the first byte of block N+1, which could be tens of thousands of bytes later\r
179 (or even earlier!) in the file, depending on what the stream directory says.\r