1 //===- MSFCommon.cpp - Common types and functions for MSF files -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/MSF/MSFCommon.h"
11 #include "llvm/DebugInfo/MSF/MSFError.h"
12 #include "llvm/Support/Endian.h"
13 #include "llvm/Support/Error.h"
18 using namespace llvm::msf
;
20 Error
llvm::msf::validateSuperBlock(const SuperBlock
&SB
) {
21 // Check the magic bytes.
22 if (std::memcmp(SB
.MagicBytes
, Magic
, sizeof(Magic
)) != 0)
23 return make_error
<MSFError
>(msf_error_code::invalid_format
,
24 "MSF magic header doesn't match");
26 if (!isValidBlockSize(SB
.BlockSize
))
27 return make_error
<MSFError
>(msf_error_code::invalid_format
,
28 "Unsupported block size.");
30 // We don't support directories whose sizes aren't a multiple of four bytes.
31 if (SB
.NumDirectoryBytes
% sizeof(support::ulittle32_t
) != 0)
32 return make_error
<MSFError
>(msf_error_code::invalid_format
,
33 "Directory size is not multiple of 4.");
35 // The number of blocks which comprise the directory is a simple function of
36 // the number of bytes it contains.
37 uint64_t NumDirectoryBlocks
=
38 bytesToBlocks(SB
.NumDirectoryBytes
, SB
.BlockSize
);
40 // The directory, as we understand it, is a block which consists of a list of
41 // block numbers. It is unclear what would happen if the number of blocks
42 // couldn't fit on a single block.
43 if (NumDirectoryBlocks
> SB
.BlockSize
/ sizeof(support::ulittle32_t
))
44 return make_error
<MSFError
>(msf_error_code::invalid_format
,
45 "Too many directory blocks.");
47 if (SB
.BlockMapAddr
== 0)
48 return make_error
<MSFError
>(msf_error_code::invalid_format
,
49 "Block 0 is reserved");
51 if (SB
.BlockMapAddr
>= SB
.NumBlocks
)
52 return make_error
<MSFError
>(msf_error_code::invalid_format
,
53 "Block map address is invalid.");
55 if (SB
.FreeBlockMapBlock
!= 1 && SB
.FreeBlockMapBlock
!= 2)
56 return make_error
<MSFError
>(
57 msf_error_code::invalid_format
,
58 "The free block map isn't at block 1 or block 2.");
60 return Error::success();
63 MSFStreamLayout
llvm::msf::getFpmStreamLayout(const MSFLayout
&Msf
,
64 bool IncludeUnusedFpmData
,
67 uint32_t NumFpmIntervals
=
68 getNumFpmIntervals(Msf
, IncludeUnusedFpmData
, AltFpm
);
70 uint32_t FpmBlock
= AltFpm
? Msf
.alternateFpmBlock() : Msf
.mainFpmBlock();
72 for (uint32_t I
= 0; I
< NumFpmIntervals
; ++I
) {
73 FL
.Blocks
.push_back(support::ulittle32_t(FpmBlock
));
74 FpmBlock
+= msf::getFpmIntervalLength(Msf
);
77 if (IncludeUnusedFpmData
)
78 FL
.Length
= NumFpmIntervals
* Msf
.SB
->BlockSize
;
80 FL
.Length
= divideCeil(Msf
.SB
->NumBlocks
, 8);