1 //===- MSFCommon.h - Common types and functions for MSF files ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_DEBUGINFO_MSF_MSFCOMMON_H
10 #define LLVM_DEBUGINFO_MSF_MSFCOMMON_H
11
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/BitVector.h"
14 #include "llvm/Support/Compiler.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/MathExtras.h"
18 #include <cstdint>
19 #include <vector>
20
21 namespace llvm {
22 namespace msf {
23
24 static const char Magic[] = {'M', 'i', 'c', 'r', 'o', 's', 'o', 'f',
25 't', ' ', 'C', '/', 'C', '+', '+', ' ',
26 'M', 'S', 'F', ' ', '7', '.', '0', '0',
27 '\r', '\n', '\x1a', 'D', 'S', '\0', '\0', '\0'};
28
29 // The superblock is overlaid at the beginning of the file (offset 0).
30 // It starts with a magic header and is followed by information which
31 // describes the layout of the file system.
32 struct SuperBlock {
33 char MagicBytes[sizeof(Magic)];
34 // The file system is split into a variable number of fixed size elements.
35 // These elements are referred to as blocks. The size of a block may vary
36 // from system to system.
37 support::ulittle32_t BlockSize;
38 // The index of the free block map.
39 support::ulittle32_t FreeBlockMapBlock;
40 // This contains the number of blocks resident in the file system. In
41 // practice, NumBlocks * BlockSize is equivalent to the size of the MSF
42 // file.
43 support::ulittle32_t NumBlocks;
44 // This contains the number of bytes which make up the directory.
45 support::ulittle32_t NumDirectoryBytes;
46 // This field's purpose is not yet known.
47 support::ulittle32_t Unknown1;
48 // This contains the block # of the block map.
49 support::ulittle32_t BlockMapAddr;
50 };
51
52 struct MSFLayout {
53 MSFLayout() = default;
54
mainFpmBlockMSFLayout55 uint32_t mainFpmBlock() const {
56 assert(SB->FreeBlockMapBlock == 1 || SB->FreeBlockMapBlock == 2);
57 return SB->FreeBlockMapBlock;
58 }
59
alternateFpmBlockMSFLayout60 uint32_t alternateFpmBlock() const {
61 // If mainFpmBlock is 1, this is 2. If mainFpmBlock is 2, this is 1.
62 return 3U - mainFpmBlock();
63 }
64
65 const SuperBlock *SB = nullptr;
66 BitVector FreePageMap;
67 ArrayRef<support::ulittle32_t> DirectoryBlocks;
68 ArrayRef<support::ulittle32_t> StreamSizes;
69 std::vector<ArrayRef<support::ulittle32_t>> StreamMap;
70 };
71
72 /// Describes the layout of a stream in an MSF layout. A "stream" here
73 /// is defined as any logical unit of data which may be arranged inside the MSF
74 /// file as a sequence of (possibly discontiguous) blocks. When we want to read
75 /// from a particular MSF Stream, we fill out a stream layout structure and the
76 /// reader uses it to determine which blocks in the underlying MSF file contain
77 /// the data, so that it can be pieced together in the right order.
78 class MSFStreamLayout {
79 public:
80 uint32_t Length;
81 std::vector<support::ulittle32_t> Blocks;
82 };
83
84 /// Determine the layout of the FPM stream, given the MSF layout. An FPM
85 /// stream spans 1 or more blocks, each at equally spaced intervals throughout
86 /// the file.
87 LLVM_ABI MSFStreamLayout getFpmStreamLayout(const MSFLayout &Msf,
88 bool IncludeUnusedFpmData = false,
89 bool AltFpm = false);
90
isValidBlockSize(uint32_t Size)91 inline bool isValidBlockSize(uint32_t Size) {
92 switch (Size) {
93 case 512:
94 case 1024:
95 case 2048:
96 case 4096:
97 case 8192:
98 case 16384:
99 case 32768:
100 return true;
101 }
102 return false;
103 }
104
105 /// Given the specified block size, returns the maximum possible file size.
106 /// Block Size | Max File Size
107 /// <= 4096 | 4GB
108 /// 8192 | 8GB
109 /// 16384 | 16GB
110 /// 32768 | 32GB
111 /// \p Size - the block size of the MSF
getMaxFileSizeFromBlockSize(uint32_t Size)112 inline uint64_t getMaxFileSizeFromBlockSize(uint32_t Size) {
113 switch (Size) {
114 case 8192:
115 return (uint64_t)UINT32_MAX * 2ULL;
116 case 16384:
117 return (uint64_t)UINT32_MAX * 3ULL;
118 case 32768:
119 return (uint64_t)UINT32_MAX * 4ULL;
120 default:
121 return (uint64_t)UINT32_MAX;
122 }
123 }
124
125 // Super Block, Fpm0, Fpm1, and Block Map
getMinimumBlockCount()126 inline uint32_t getMinimumBlockCount() { return 4; }
127
128 // Super Block, Fpm0, and Fpm1 are reserved. The Block Map, although required
129 // need not be at block 3.
getFirstUnreservedBlock()130 inline uint32_t getFirstUnreservedBlock() { return 3; }
131
bytesToBlocks(uint64_t NumBytes,uint64_t BlockSize)132 inline uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize) {
133 return divideCeil(NumBytes, BlockSize);
134 }
135
blockToOffset(uint64_t BlockNumber,uint64_t BlockSize)136 inline uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize) {
137 return BlockNumber * BlockSize;
138 }
139
getFpmIntervalLength(const MSFLayout & L)140 inline uint32_t getFpmIntervalLength(const MSFLayout &L) {
141 return L.SB->BlockSize;
142 }
143
144 /// Given an MSF with the specified block size and number of blocks, determine
145 /// how many pieces the specified Fpm is split into.
146 /// \p BlockSize - the block size of the MSF
147 /// \p NumBlocks - the total number of blocks in the MSF
148 /// \p IncludeUnusedFpmData - When true, this will count every block that is
149 /// both in the file and matches the form of an FPM block, even if some of
150 /// those FPM blocks are unused (a single FPM block can describe the
151 /// allocation status of up to 32,767 blocks, although one appears only
152 /// every 4,096 blocks). So there are 8x as many blocks that match the
153 /// form as there are blocks that are necessary to describe the allocation
154 /// status of the file. When this parameter is false, these extraneous
155 /// trailing blocks are not counted.
getNumFpmIntervals(uint32_t BlockSize,uint32_t NumBlocks,bool IncludeUnusedFpmData,int FpmNumber)156 inline uint32_t getNumFpmIntervals(uint32_t BlockSize, uint32_t NumBlocks,
157 bool IncludeUnusedFpmData, int FpmNumber) {
158 assert(FpmNumber == 1 || FpmNumber == 2);
159 if (IncludeUnusedFpmData) {
160 // This calculation determines how many times a number of the form
161 // BlockSize * k + N appears in the range [0, NumBlocks). We only need to
162 // do this when unused data is included, since the number of blocks dwarfs
163 // the number of fpm blocks.
164 return divideCeil(NumBlocks - FpmNumber, BlockSize);
165 }
166
167 // We want the minimum number of intervals required, where each interval can
168 // represent BlockSize * 8 blocks.
169 return divideCeil(NumBlocks, 8 * BlockSize);
170 }
171
172 inline uint32_t getNumFpmIntervals(const MSFLayout &L,
173 bool IncludeUnusedFpmData = false,
174 bool AltFpm = false) {
175 return getNumFpmIntervals(L.SB->BlockSize, L.SB->NumBlocks,
176 IncludeUnusedFpmData,
177 AltFpm ? L.alternateFpmBlock() : L.mainFpmBlock());
178 }
179
180 LLVM_ABI Error validateSuperBlock(const SuperBlock &SB);
181
182 } // end namespace msf
183 } // end namespace llvm
184
185 #endif // LLVM_DEBUGINFO_MSF_MSFCOMMON_H
186