LLVM  8.0.1
MSFCommon.h
Go to the documentation of this file.
1 //===- MSFCommon.h - Common types and functions for MSF files ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_DEBUGINFO_MSF_MSFCOMMON_H
11 #define LLVM_DEBUGINFO_MSF_MSFCOMMON_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/BitVector.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/Error.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.
38  // The index of the free block map.
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.
44  // This contains the number of bytes which make up the directory.
46  // This field's purpose is not yet known.
48  // This contains the block # of the block map.
50 };
51 
52 struct MSFLayout {
53  MSFLayout() = default;
54 
56  assert(SB->FreeBlockMapBlock == 1 || SB->FreeBlockMapBlock == 2);
57  return SB->FreeBlockMapBlock;
58  }
59 
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;
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.
79 public:
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.
88  bool IncludeUnusedFpmData = false,
89  bool AltFpm = false);
90 
92  switch (Size) {
93  case 512:
94  case 1024:
95  case 2048:
96  case 4096:
97  return true;
98  }
99  return false;
100 }
101 
102 // Super Block, Fpm0, Fpm1, and Block Map
103 inline uint32_t getMinimumBlockCount() { return 4; }
104 
105 // Super Block, Fpm0, and Fpm1 are reserved. The Block Map, although required
106 // need not be at block 3.
107 inline uint32_t getFirstUnreservedBlock() { return 3; }
108 
109 inline uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize) {
110  return divideCeil(NumBytes, BlockSize);
111 }
112 
113 inline uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize) {
114  return BlockNumber * BlockSize;
115 }
116 
118  return L.SB->BlockSize;
119 }
120 
121 /// Given an MSF with the specified block size and number of blocks, determine
122 /// how many pieces the specified Fpm is split into.
123 /// \p BlockSize - the block size of the MSF
124 /// \p NumBlocks - the total number of blocks in the MSF
125 /// \p IncludeUnusedFpmData - When true, this will count every block that is
126 /// both in the file and matches the form of an FPM block, even if some of
127 /// those FPM blocks are unused (a single FPM block can describe the
128 /// allocation status of up to 32,767 blocks, although one appears only
129 /// every 4,096 blocks). So there are 8x as many blocks that match the
130 /// form as there are blocks that are necessary to describe the allocation
131 /// status of the file. When this parameter is false, these extraneous
132 /// trailing blocks are not counted.
134  bool IncludeUnusedFpmData, int FpmNumber) {
135  assert(FpmNumber == 1 || FpmNumber == 2);
136  if (IncludeUnusedFpmData) {
137  // This calculation determines how many times a number of the form
138  // BlockSize * k + N appears in the range [0, NumBlocks). We only need to
139  // do this when unused data is included, since the number of blocks dwarfs
140  // the number of fpm blocks.
141  return divideCeil(NumBlocks - FpmNumber, BlockSize);
142  }
143 
144  // We want the minimum number of intervals required, where each interval can
145  // represent BlockSize * 8 blocks.
146  return divideCeil(NumBlocks, 8 * BlockSize);
147 }
148 
150  bool IncludeUnusedFpmData = false,
151  bool AltFpm = false) {
153  IncludeUnusedFpmData,
154  AltFpm ? L.alternateFpmBlock() : L.mainFpmBlock());
155 }
156 
158 
159 } // end namespace msf
160 } // end namespace llvm
161 
162 #endif // LLVM_DEBUGINFO_MSF_MSFCOMMON_H
char MagicBytes[sizeof(Magic)]
Definition: MSFCommon.h:33
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Error validateSuperBlock(const SuperBlock &SB)
Definition: MSFCommon.cpp:20
uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize)
Definition: MSFCommon.h:113
uint32_t getMinimumBlockCount()
Definition: MSFCommon.h:103
Describes the layout of a stream in an MSF layout.
Definition: MSFCommon.h:78
MSFStreamLayout getFpmStreamLayout(const MSFLayout &Msf, bool IncludeUnusedFpmData=false, bool AltFpm=false)
Determine the layout of the FPM stream, given the MSF layout.
Definition: MSFCommon.cpp:63
uint32_t getFpmIntervalLength(const MSFLayout &L)
Definition: MSFCommon.h:117
std::vector< support::ulittle32_t > Blocks
Definition: MSFCommon.h:81
uint32_t getNumFpmIntervals(uint32_t BlockSize, uint32_t NumBlocks, bool IncludeUnusedFpmData, int FpmNumber)
Given an MSF with the specified block size and number of blocks, determine how many pieces the specif...
Definition: MSFCommon.h:133
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
support::ulittle32_t BlockSize
Definition: MSFCommon.h:37
support::ulittle32_t BlockMapAddr
Definition: MSFCommon.h:49
static const char Magic[]
Definition: MSFCommon.h:24
support::ulittle32_t Unknown1
Definition: MSFCommon.h:47
uint32_t alternateFpmBlock() const
Definition: MSFCommon.h:60
ArrayRef< support::ulittle32_t > DirectoryBlocks
Definition: MSFCommon.h:67
BitVector FreePageMap
Definition: MSFCommon.h:66
uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize)
Definition: MSFCommon.h:109
uint32_t mainFpmBlock() const
Definition: MSFCommon.h:55
uint32_t Size
Definition: Profile.cpp:47
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition: MathExtras.h:699
std::vector< ArrayRef< support::ulittle32_t > > StreamMap
Definition: MSFCommon.h:69
ArrayRef< support::ulittle32_t > StreamSizes
Definition: MSFCommon.h:68
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
support::ulittle32_t FreeBlockMapBlock
Definition: MSFCommon.h:39
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
bool isValidBlockSize(uint32_t Size)
Definition: MSFCommon.h:91
uint32_t getFirstUnreservedBlock()
Definition: MSFCommon.h:107
const SuperBlock * SB
Definition: MSFCommon.h:65
support::ulittle32_t NumDirectoryBytes
Definition: MSFCommon.h:45
support::ulittle32_t NumBlocks
Definition: MSFCommon.h:43