Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:43

0001 //===- MSFCommon.h - Common types and functions for MSF files ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DEBUGINFO_MSF_MSFCOMMON_H
0010 #define LLVM_DEBUGINFO_MSF_MSFCOMMON_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/BitVector.h"
0014 #include "llvm/Support/Endian.h"
0015 #include "llvm/Support/Error.h"
0016 #include "llvm/Support/MathExtras.h"
0017 #include <cstdint>
0018 #include <vector>
0019 
0020 namespace llvm {
0021 namespace msf {
0022 
0023 static const char Magic[] = {'M',  'i',  'c',    'r', 'o', 's',  'o',  'f',
0024                              't',  ' ',  'C',    '/', 'C', '+',  '+',  ' ',
0025                              'M',  'S',  'F',    ' ', '7', '.',  '0',  '0',
0026                              '\r', '\n', '\x1a', 'D', 'S', '\0', '\0', '\0'};
0027 
0028 // The superblock is overlaid at the beginning of the file (offset 0).
0029 // It starts with a magic header and is followed by information which
0030 // describes the layout of the file system.
0031 struct SuperBlock {
0032   char MagicBytes[sizeof(Magic)];
0033   // The file system is split into a variable number of fixed size elements.
0034   // These elements are referred to as blocks.  The size of a block may vary
0035   // from system to system.
0036   support::ulittle32_t BlockSize;
0037   // The index of the free block map.
0038   support::ulittle32_t FreeBlockMapBlock;
0039   // This contains the number of blocks resident in the file system.  In
0040   // practice, NumBlocks * BlockSize is equivalent to the size of the MSF
0041   // file.
0042   support::ulittle32_t NumBlocks;
0043   // This contains the number of bytes which make up the directory.
0044   support::ulittle32_t NumDirectoryBytes;
0045   // This field's purpose is not yet known.
0046   support::ulittle32_t Unknown1;
0047   // This contains the block # of the block map.
0048   support::ulittle32_t BlockMapAddr;
0049 };
0050 
0051 struct MSFLayout {
0052   MSFLayout() = default;
0053 
0054   uint32_t mainFpmBlock() const {
0055     assert(SB->FreeBlockMapBlock == 1 || SB->FreeBlockMapBlock == 2);
0056     return SB->FreeBlockMapBlock;
0057   }
0058 
0059   uint32_t alternateFpmBlock() const {
0060     // If mainFpmBlock is 1, this is 2.  If mainFpmBlock is 2, this is 1.
0061     return 3U - mainFpmBlock();
0062   }
0063 
0064   const SuperBlock *SB = nullptr;
0065   BitVector FreePageMap;
0066   ArrayRef<support::ulittle32_t> DirectoryBlocks;
0067   ArrayRef<support::ulittle32_t> StreamSizes;
0068   std::vector<ArrayRef<support::ulittle32_t>> StreamMap;
0069 };
0070 
0071 /// Describes the layout of a stream in an MSF layout.  A "stream" here
0072 /// is defined as any logical unit of data which may be arranged inside the MSF
0073 /// file as a sequence of (possibly discontiguous) blocks.  When we want to read
0074 /// from a particular MSF Stream, we fill out a stream layout structure and the
0075 /// reader uses it to determine which blocks in the underlying MSF file contain
0076 /// the data, so that it can be pieced together in the right order.
0077 class MSFStreamLayout {
0078 public:
0079   uint32_t Length;
0080   std::vector<support::ulittle32_t> Blocks;
0081 };
0082 
0083 /// Determine the layout of the FPM stream, given the MSF layout.  An FPM
0084 /// stream spans 1 or more blocks, each at equally spaced intervals throughout
0085 /// the file.
0086 MSFStreamLayout getFpmStreamLayout(const MSFLayout &Msf,
0087                                    bool IncludeUnusedFpmData = false,
0088                                    bool AltFpm = false);
0089 
0090 inline bool isValidBlockSize(uint32_t Size) {
0091   switch (Size) {
0092   case 512:
0093   case 1024:
0094   case 2048:
0095   case 4096:
0096   case 8192:
0097   case 16384:
0098   case 32768:
0099     return true;
0100   }
0101   return false;
0102 }
0103 
0104 /// Given the specified block size, returns the maximum possible file size.
0105 /// Block Size  |  Max File Size
0106 /// <= 4096     |      4GB
0107 ///    8192     |      8GB
0108 ///   16384     |      16GB
0109 ///   32768     |      32GB
0110 /// \p Size - the block size of the MSF
0111 inline uint64_t getMaxFileSizeFromBlockSize(uint32_t Size) {
0112   switch (Size) {
0113   case 8192:
0114     return (uint64_t)UINT32_MAX * 2ULL;
0115   case 16384:
0116     return (uint64_t)UINT32_MAX * 3ULL;
0117   case 32768:
0118     return (uint64_t)UINT32_MAX * 4ULL;
0119   default:
0120     return (uint64_t)UINT32_MAX;
0121   }
0122 }
0123 
0124 // Super Block, Fpm0, Fpm1, and Block Map
0125 inline uint32_t getMinimumBlockCount() { return 4; }
0126 
0127 // Super Block, Fpm0, and Fpm1 are reserved.  The Block Map, although required
0128 // need not be at block 3.
0129 inline uint32_t getFirstUnreservedBlock() { return 3; }
0130 
0131 inline uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize) {
0132   return divideCeil(NumBytes, BlockSize);
0133 }
0134 
0135 inline uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize) {
0136   return BlockNumber * BlockSize;
0137 }
0138 
0139 inline uint32_t getFpmIntervalLength(const MSFLayout &L) {
0140   return L.SB->BlockSize;
0141 }
0142 
0143 /// Given an MSF with the specified block size and number of blocks, determine
0144 /// how many pieces the specified Fpm is split into.
0145 /// \p BlockSize - the block size of the MSF
0146 /// \p NumBlocks - the total number of blocks in the MSF
0147 /// \p IncludeUnusedFpmData - When true, this will count every block that is
0148 ///    both in the file and matches the form of an FPM block, even if some of
0149 ///    those FPM blocks are unused (a single FPM block can describe the
0150 ///    allocation status of up to 32,767 blocks, although one appears only
0151 ///    every 4,096 blocks).  So there are 8x as many blocks that match the
0152 ///    form as there are blocks that are necessary to describe the allocation
0153 ///    status of the file.  When this parameter is false, these extraneous
0154 ///    trailing blocks are not counted.
0155 inline uint32_t getNumFpmIntervals(uint32_t BlockSize, uint32_t NumBlocks,
0156                                    bool IncludeUnusedFpmData, int FpmNumber) {
0157   assert(FpmNumber == 1 || FpmNumber == 2);
0158   if (IncludeUnusedFpmData) {
0159     // This calculation determines how many times a number of the form
0160     // BlockSize * k + N appears in the range [0, NumBlocks).  We only need to
0161     // do this when unused data is included, since the number of blocks dwarfs
0162     // the number of fpm blocks.
0163     return divideCeil(NumBlocks - FpmNumber, BlockSize);
0164   }
0165 
0166   // We want the minimum number of intervals required, where each interval can
0167   // represent BlockSize * 8 blocks.
0168   return divideCeil(NumBlocks, 8 * BlockSize);
0169 }
0170 
0171 inline uint32_t getNumFpmIntervals(const MSFLayout &L,
0172                                    bool IncludeUnusedFpmData = false,
0173                                    bool AltFpm = false) {
0174   return getNumFpmIntervals(L.SB->BlockSize, L.SB->NumBlocks,
0175                             IncludeUnusedFpmData,
0176                             AltFpm ? L.alternateFpmBlock() : L.mainFpmBlock());
0177 }
0178 
0179 Error validateSuperBlock(const SuperBlock &SB);
0180 
0181 } // end namespace msf
0182 } // end namespace llvm
0183 
0184 #endif // LLVM_DEBUGINFO_MSF_MSFCOMMON_H