Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 1 | //===- MSFCommon.cpp - Common types and functions for MSF files -*- C++ -*-===// |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 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 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
| 11 | #include "llvm/DebugInfo/MSF/MSFError.h" |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace llvm; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 14 | using namespace llvm::msf; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 15 | |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 16 | Error llvm::msf::validateSuperBlock(const SuperBlock &SB) { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 17 | // Check the magic bytes. |
| 18 | if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 19 | return make_error<MSFError>(msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 20 | "MSF magic header doesn't match"); |
| 21 | |
| 22 | if (!isValidBlockSize(SB.BlockSize)) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 23 | return make_error<MSFError>(msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 24 | "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 Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 28 | return make_error<MSFError>(msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 29 | "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 Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 40 | return make_error<MSFError>(msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 41 | "Too many directory blocks."); |
| 42 | |
| 43 | if (SB.BlockMapAddr == 0) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 44 | return make_error<MSFError>(msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 45 | "Block 0 is reserved"); |
| 46 | |
David Majnemer | 9bca03b | 2016-12-18 00:41:10 +0000 | [diff] [blame^] | 47 | 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 Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 56 | return Error::success(); |
| 57 | } |