Home | History | Annotate | Download | only in MSF
      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 
     16 #include "llvm/Support/Endian.h"
     17 #include "llvm/Support/Error.h"
     18 #include "llvm/Support/MathExtras.h"
     19 
     20 #include <vector>
     21 
     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 
     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() : SB(nullptr) {}
     54   const SuperBlock *SB;
     55   BitVector FreePageMap;
     56   ArrayRef<support::ulittle32_t> DirectoryBlocks;
     57   ArrayRef<support::ulittle32_t> StreamSizes;
     58   std::vector<ArrayRef<support::ulittle32_t>> StreamMap;
     59 };
     60 
     61 inline bool isValidBlockSize(uint32_t Size) {
     62   switch (Size) {
     63   case 512:
     64   case 1024:
     65   case 2048:
     66   case 4096:
     67     return true;
     68   }
     69   return false;
     70 }
     71 
     72 // Super Block, Fpm0, Fpm1, and Block Map
     73 inline uint32_t getMinimumBlockCount() { return 4; }
     74 
     75 // Super Block, Fpm0, and Fpm1 are reserved.  The Block Map, although required
     76 // need not be at block 3.
     77 inline uint32_t getFirstUnreservedBlock() { return 3; }
     78 
     79 inline uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize) {
     80   return alignTo(NumBytes, BlockSize) / BlockSize;
     81 }
     82 
     83 inline uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize) {
     84   return BlockNumber * BlockSize;
     85 }
     86 
     87 inline uint32_t getFpmIntervalLength(const MSFLayout &L) {
     88   return L.SB->BlockSize;
     89 }
     90 
     91 inline uint32_t getNumFpmIntervals(const MSFLayout &L) {
     92   uint32_t Length = getFpmIntervalLength(L);
     93   return llvm::alignTo(L.SB->NumBlocks, Length) / Length;
     94 }
     95 
     96 inline uint32_t getFullFpmByteSize(const MSFLayout &L) {
     97   return llvm::alignTo(L.SB->NumBlocks, 8) / 8;
     98 }
     99 
    100 Error validateSuperBlock(const SuperBlock &SB);
    101 } // namespace msf
    102 } // namespace llvm
    103 
    104 #endif // LLVM_DEBUGINFO_MSF_MSFCOMMON_H
    105