blob: fdab7884646ec6c592dba46b4f9c3b2f990df58b [file] [log] [blame]
Zachary Turnera3225b02016-07-29 20:56:36 +00001//===- MSFCommon.cpp - Common types and functions for MSF files -*- C++ -*-===//
Zachary Turnerf52a8992016-07-15 20:43:38 +00002//
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
Zachary Turnera3225b02016-07-29 20:56:36 +000010#include "llvm/DebugInfo/MSF/MSFCommon.h"
11#include "llvm/DebugInfo/MSF/MSFError.h"
Zachary Turnerf52a8992016-07-15 20:43:38 +000012
13using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000014using namespace llvm::msf;
Zachary Turnerf52a8992016-07-15 20:43:38 +000015
Zachary Turnerbac69d32016-07-22 19:56:05 +000016Error llvm::msf::validateSuperBlock(const SuperBlock &SB) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000017 // Check the magic bytes.
18 if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0)
Zachary Turnera3225b02016-07-29 20:56:36 +000019 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000020 "MSF magic header doesn't match");
21
22 if (!isValidBlockSize(SB.BlockSize))
Zachary Turnera3225b02016-07-29 20:56:36 +000023 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000024 "Unsupported block size.");
25
26 // We don't support directories whose sizes aren't a multiple of four bytes.
27 if (SB.NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
Zachary Turnera3225b02016-07-29 20:56:36 +000028 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000029 "Directory size is not multiple of 4.");
30
31 // The number of blocks which comprise the directory is a simple function of
32 // the number of bytes it contains.
33 uint64_t NumDirectoryBlocks =
34 bytesToBlocks(SB.NumDirectoryBytes, SB.BlockSize);
35
36 // The directory, as we understand it, is a block which consists of a list of
37 // block numbers. It is unclear what would happen if the number of blocks
38 // couldn't fit on a single block.
39 if (NumDirectoryBlocks > SB.BlockSize / sizeof(support::ulittle32_t))
Zachary Turnera3225b02016-07-29 20:56:36 +000040 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000041 "Too many directory blocks.");
42
43 if (SB.BlockMapAddr == 0)
Zachary Turnera3225b02016-07-29 20:56:36 +000044 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000045 "Block 0 is reserved");
46
David Majnemer9bca03b2016-12-18 00:41:10 +000047 if (SB.BlockMapAddr >= SB.NumBlocks)
48 return make_error<MSFError>(msf_error_code::invalid_format,
49 "Block map address is invalid.");
50
51 if (SB.FreeBlockMapBlock != 1 && SB.FreeBlockMapBlock != 2)
52 return make_error<MSFError>(
53 msf_error_code::invalid_format,
54 "The free block map isn't at block 1 or block 2.");
55
Zachary Turnerf52a8992016-07-15 20:43:38 +000056 return Error::success();
57}