xref: /freebsd-src/contrib/llvm-project/llvm/lib/DebugInfo/MSF/MSFCommon.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- MSFCommon.cpp - Common types and functions for MSF files -----------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFCommon.h"
10*0b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFError.h"
11*0b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
12*0b57cec5SDimitry Andric #include "llvm/Support/Error.h"
13*0b57cec5SDimitry Andric #include <cstdint>
14*0b57cec5SDimitry Andric #include <cstring>
15*0b57cec5SDimitry Andric 
16*0b57cec5SDimitry Andric using namespace llvm;
17*0b57cec5SDimitry Andric using namespace llvm::msf;
18*0b57cec5SDimitry Andric 
validateSuperBlock(const SuperBlock & SB)19*0b57cec5SDimitry Andric Error llvm::msf::validateSuperBlock(const SuperBlock &SB) {
20*0b57cec5SDimitry Andric   // Check the magic bytes.
21*0b57cec5SDimitry Andric   if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0)
22*0b57cec5SDimitry Andric     return make_error<MSFError>(msf_error_code::invalid_format,
23*0b57cec5SDimitry Andric                                 "MSF magic header doesn't match");
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric   if (!isValidBlockSize(SB.BlockSize))
26*0b57cec5SDimitry Andric     return make_error<MSFError>(msf_error_code::invalid_format,
27*0b57cec5SDimitry Andric                                 "Unsupported block size.");
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric   // We don't support directories whose sizes aren't a multiple of four bytes.
30*0b57cec5SDimitry Andric   if (SB.NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
31*0b57cec5SDimitry Andric     return make_error<MSFError>(msf_error_code::invalid_format,
32*0b57cec5SDimitry Andric                                 "Directory size is not multiple of 4.");
33*0b57cec5SDimitry Andric 
34*0b57cec5SDimitry Andric   // The number of blocks which comprise the directory is a simple function of
35*0b57cec5SDimitry Andric   // the number of bytes it contains.
36*0b57cec5SDimitry Andric   uint64_t NumDirectoryBlocks =
37*0b57cec5SDimitry Andric       bytesToBlocks(SB.NumDirectoryBytes, SB.BlockSize);
38*0b57cec5SDimitry Andric 
39*0b57cec5SDimitry Andric   // The directory, as we understand it, is a block which consists of a list of
40*0b57cec5SDimitry Andric   // block numbers.  It is unclear what would happen if the number of blocks
41*0b57cec5SDimitry Andric   // couldn't fit on a single block.
42*0b57cec5SDimitry Andric   if (NumDirectoryBlocks > SB.BlockSize / sizeof(support::ulittle32_t))
43*0b57cec5SDimitry Andric     return make_error<MSFError>(msf_error_code::invalid_format,
44*0b57cec5SDimitry Andric                                 "Too many directory blocks.");
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric   if (SB.BlockMapAddr == 0)
47*0b57cec5SDimitry Andric     return make_error<MSFError>(msf_error_code::invalid_format,
48*0b57cec5SDimitry Andric                                 "Block 0 is reserved");
49*0b57cec5SDimitry Andric 
50*0b57cec5SDimitry Andric   if (SB.BlockMapAddr >= SB.NumBlocks)
51*0b57cec5SDimitry Andric     return make_error<MSFError>(msf_error_code::invalid_format,
52*0b57cec5SDimitry Andric                                 "Block map address is invalid.");
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric   if (SB.FreeBlockMapBlock != 1 && SB.FreeBlockMapBlock != 2)
55*0b57cec5SDimitry Andric     return make_error<MSFError>(
56*0b57cec5SDimitry Andric         msf_error_code::invalid_format,
57*0b57cec5SDimitry Andric         "The free block map isn't at block 1 or block 2.");
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric   return Error::success();
60*0b57cec5SDimitry Andric }
61*0b57cec5SDimitry Andric 
getFpmStreamLayout(const MSFLayout & Msf,bool IncludeUnusedFpmData,bool AltFpm)62*0b57cec5SDimitry Andric MSFStreamLayout llvm::msf::getFpmStreamLayout(const MSFLayout &Msf,
63*0b57cec5SDimitry Andric                                               bool IncludeUnusedFpmData,
64*0b57cec5SDimitry Andric                                               bool AltFpm) {
65*0b57cec5SDimitry Andric   MSFStreamLayout FL;
66*0b57cec5SDimitry Andric   uint32_t NumFpmIntervals =
67*0b57cec5SDimitry Andric       getNumFpmIntervals(Msf, IncludeUnusedFpmData, AltFpm);
68*0b57cec5SDimitry Andric 
69*0b57cec5SDimitry Andric   uint32_t FpmBlock = AltFpm ? Msf.alternateFpmBlock() : Msf.mainFpmBlock();
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric   for (uint32_t I = 0; I < NumFpmIntervals; ++I) {
72*0b57cec5SDimitry Andric     FL.Blocks.push_back(support::ulittle32_t(FpmBlock));
73*0b57cec5SDimitry Andric     FpmBlock += msf::getFpmIntervalLength(Msf);
74*0b57cec5SDimitry Andric   }
75*0b57cec5SDimitry Andric 
76*0b57cec5SDimitry Andric   if (IncludeUnusedFpmData)
77*0b57cec5SDimitry Andric     FL.Length = NumFpmIntervals * Msf.SB->BlockSize;
78*0b57cec5SDimitry Andric   else
79*0b57cec5SDimitry Andric     FL.Length = divideCeil(Msf.SB->NumBlocks, 8);
80*0b57cec5SDimitry Andric 
81*0b57cec5SDimitry Andric   return FL;
82*0b57cec5SDimitry Andric }
83