Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/MSF/MSFCommon.cpp
35266 views
//===- MSFCommon.cpp - Common types and functions for MSF files -----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/MSF/MSFCommon.h"9#include "llvm/DebugInfo/MSF/MSFError.h"10#include "llvm/Support/Endian.h"11#include "llvm/Support/Error.h"12#include <cstdint>13#include <cstring>1415using namespace llvm;16using namespace llvm::msf;1718Error llvm::msf::validateSuperBlock(const SuperBlock &SB) {19// Check the magic bytes.20if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0)21return make_error<MSFError>(msf_error_code::invalid_format,22"MSF magic header doesn't match");2324if (!isValidBlockSize(SB.BlockSize))25return make_error<MSFError>(msf_error_code::invalid_format,26"Unsupported block size.");2728// We don't support directories whose sizes aren't a multiple of four bytes.29if (SB.NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)30return make_error<MSFError>(msf_error_code::invalid_format,31"Directory size is not multiple of 4.");3233// The number of blocks which comprise the directory is a simple function of34// the number of bytes it contains.35uint64_t NumDirectoryBlocks =36bytesToBlocks(SB.NumDirectoryBytes, SB.BlockSize);3738// The directory, as we understand it, is a block which consists of a list of39// block numbers. It is unclear what would happen if the number of blocks40// couldn't fit on a single block.41if (NumDirectoryBlocks > SB.BlockSize / sizeof(support::ulittle32_t))42return make_error<MSFError>(msf_error_code::invalid_format,43"Too many directory blocks.");4445if (SB.BlockMapAddr == 0)46return make_error<MSFError>(msf_error_code::invalid_format,47"Block 0 is reserved");4849if (SB.BlockMapAddr >= SB.NumBlocks)50return make_error<MSFError>(msf_error_code::invalid_format,51"Block map address is invalid.");5253if (SB.FreeBlockMapBlock != 1 && SB.FreeBlockMapBlock != 2)54return make_error<MSFError>(55msf_error_code::invalid_format,56"The free block map isn't at block 1 or block 2.");5758return Error::success();59}6061MSFStreamLayout llvm::msf::getFpmStreamLayout(const MSFLayout &Msf,62bool IncludeUnusedFpmData,63bool AltFpm) {64MSFStreamLayout FL;65uint32_t NumFpmIntervals =66getNumFpmIntervals(Msf, IncludeUnusedFpmData, AltFpm);6768uint32_t FpmBlock = AltFpm ? Msf.alternateFpmBlock() : Msf.mainFpmBlock();6970for (uint32_t I = 0; I < NumFpmIntervals; ++I) {71FL.Blocks.push_back(support::ulittle32_t(FpmBlock));72FpmBlock += msf::getFpmIntervalLength(Msf);73}7475if (IncludeUnusedFpmData)76FL.Length = NumFpmIntervals * Msf.SB->BlockSize;77else78FL.Length = divideCeil(Msf.SB->NumBlocks, 8);7980return FL;81}828384