MSFCommon.h 6.2 KB

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