blob: fb4f0700059c4b8f45d7bb66844b5a0461c7dce1 [file] [log] [blame]
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +00001//===- MSFCommon.cpp - Common types and functions for MSF files -----------===//
Zachary Turnerf52a8992016-07-15 20:43:38 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turnerf52a8992016-07-15 20:43:38 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turnera3225b02016-07-29 20:56:36 +00009#include "llvm/DebugInfo/MSF/MSFCommon.h"
10#include "llvm/DebugInfo/MSF/MSFError.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000011#include "llvm/Support/Endian.h"
12#include "llvm/Support/Error.h"
13#include <cstdint>
14#include <cstring>
Zachary Turnerf52a8992016-07-15 20:43:38 +000015
16using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000017using namespace llvm::msf;
Zachary Turnerf52a8992016-07-15 20:43:38 +000018
Zachary Turnerbac69d32016-07-22 19:56:05 +000019Error llvm::msf::validateSuperBlock(const SuperBlock &SB) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000020 // Check the magic bytes.
21 if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0)
Zachary Turnera3225b02016-07-29 20:56:36 +000022 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000023 "MSF magic header doesn't match");
24
25 if (!isValidBlockSize(SB.BlockSize))
Zachary Turnera3225b02016-07-29 20:56:36 +000026 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000027 "Unsupported block size.");
28
29 // We don't support directories whose sizes aren't a multiple of four bytes.
30 if (SB.NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
Zachary Turnera3225b02016-07-29 20:56:36 +000031 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000032 "Directory size is not multiple of 4.");
33
34 // The number of blocks which comprise the directory is a simple function of
35 // the number of bytes it contains.
36 uint64_t NumDirectoryBlocks =
37 bytesToBlocks(SB.NumDirectoryBytes, SB.BlockSize);
38
39 // The directory, as we understand it, is a block which consists of a list of
40 // block numbers. It is unclear what would happen if the number of blocks
41 // couldn't fit on a single block.
42 if (NumDirectoryBlocks > SB.BlockSize / sizeof(support::ulittle32_t))
Zachary Turnera3225b02016-07-29 20:56:36 +000043 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000044 "Too many directory blocks.");
45
46 if (SB.BlockMapAddr == 0)
Zachary Turnera3225b02016-07-29 20:56:36 +000047 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000048 "Block 0 is reserved");
49
David Majnemer9bca03b2016-12-18 00:41:10 +000050 if (SB.BlockMapAddr >= SB.NumBlocks)
51 return make_error<MSFError>(msf_error_code::invalid_format,
52 "Block map address is invalid.");
53
54 if (SB.FreeBlockMapBlock != 1 && SB.FreeBlockMapBlock != 2)
55 return make_error<MSFError>(
56 msf_error_code::invalid_format,
57 "The free block map isn't at block 1 or block 2.");
58
Zachary Turnerf52a8992016-07-15 20:43:38 +000059 return Error::success();
60}
Zachary Turnerc3d8eec2017-08-02 22:25:52 +000061
Zachary Turner9fb9d712017-08-02 22:31:39 +000062MSFStreamLayout llvm::msf::getFpmStreamLayout(const MSFLayout &Msf,
63 bool IncludeUnusedFpmData,
64 bool AltFpm) {
Zachary Turnerc3d8eec2017-08-02 22:25:52 +000065 MSFStreamLayout FL;
Zachary Turnerde6a4872018-01-05 18:12:14 +000066 uint32_t NumFpmIntervals =
67 getNumFpmIntervals(Msf, IncludeUnusedFpmData, AltFpm);
68
69 uint32_t FpmBlock = AltFpm ? Msf.alternateFpmBlock() : Msf.mainFpmBlock();
70
Zachary Turner9fb9d712017-08-02 22:31:39 +000071 for (uint32_t I = 0; I < NumFpmIntervals; ++I) {
Zachary Turnerde6a4872018-01-05 18:12:14 +000072 FL.Blocks.push_back(support::ulittle32_t(FpmBlock));
Zachary Turnerc3d8eec2017-08-02 22:25:52 +000073 FpmBlock += msf::getFpmIntervalLength(Msf);
Zachary Turnerc3d8eec2017-08-02 22:25:52 +000074 }
Zachary Turner9fb9d712017-08-02 22:31:39 +000075
76 if (IncludeUnusedFpmData)
77 FL.Length = NumFpmIntervals * Msf.SB->BlockSize;
78 else
79 FL.Length = divideCeil(Msf.SB->NumBlocks, 8);
Zachary Turnerc3d8eec2017-08-02 22:25:52 +000080
81 return FL;
82}