10b57cec5SDimitry Andric //===- MSFBuilder.cpp -----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFBuilder.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFError.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
130b57cec5SDimitry Andric #include "llvm/Support/BinaryByteStream.h"
140b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
150b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
160b57cec5SDimitry Andric #include "llvm/Support/Error.h"
170b57cec5SDimitry Andric #include "llvm/Support/FileOutputBuffer.h"
18fe6060f1SDimitry Andric #include "llvm/Support/FormatVariadic.h"
19*5f757f3fSDimitry Andric #include "llvm/Support/TimeProfiler.h"
200b57cec5SDimitry Andric #include <algorithm>
210b57cec5SDimitry Andric #include <cassert>
220b57cec5SDimitry Andric #include <cstdint>
230b57cec5SDimitry Andric #include <cstring>
240b57cec5SDimitry Andric #include <memory>
250b57cec5SDimitry Andric #include <utility>
260b57cec5SDimitry Andric #include <vector>
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric using namespace llvm::msf;
300b57cec5SDimitry Andric using namespace llvm::support;
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric static const uint32_t kSuperBlockBlock = 0;
330b57cec5SDimitry Andric static const uint32_t kFreePageMap0Block = 1;
340b57cec5SDimitry Andric static const uint32_t kFreePageMap1Block = 2;
350b57cec5SDimitry Andric static const uint32_t kNumReservedPages = 3;
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric static const uint32_t kDefaultFreePageMap = kFreePageMap1Block;
380b57cec5SDimitry Andric static const uint32_t kDefaultBlockMapAddr = kNumReservedPages;
390b57cec5SDimitry Andric
MSFBuilder(uint32_t BlockSize,uint32_t MinBlockCount,bool CanGrow,BumpPtrAllocator & Allocator)400b57cec5SDimitry Andric MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow,
410b57cec5SDimitry Andric BumpPtrAllocator &Allocator)
420b57cec5SDimitry Andric : Allocator(Allocator), IsGrowable(CanGrow),
430b57cec5SDimitry Andric FreePageMap(kDefaultFreePageMap), BlockSize(BlockSize),
440b57cec5SDimitry Andric BlockMapAddr(kDefaultBlockMapAddr), FreeBlocks(MinBlockCount, true) {
450b57cec5SDimitry Andric FreeBlocks[kSuperBlockBlock] = false;
460b57cec5SDimitry Andric FreeBlocks[kFreePageMap0Block] = false;
470b57cec5SDimitry Andric FreeBlocks[kFreePageMap1Block] = false;
480b57cec5SDimitry Andric FreeBlocks[BlockMapAddr] = false;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric
create(BumpPtrAllocator & Allocator,uint32_t BlockSize,uint32_t MinBlockCount,bool CanGrow)510b57cec5SDimitry Andric Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator,
520b57cec5SDimitry Andric uint32_t BlockSize,
530b57cec5SDimitry Andric uint32_t MinBlockCount, bool CanGrow) {
540b57cec5SDimitry Andric if (!isValidBlockSize(BlockSize))
550b57cec5SDimitry Andric return make_error<MSFError>(msf_error_code::invalid_format,
560b57cec5SDimitry Andric "The requested block size is unsupported");
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric return MSFBuilder(BlockSize,
590b57cec5SDimitry Andric std::max(MinBlockCount, msf::getMinimumBlockCount()),
600b57cec5SDimitry Andric CanGrow, Allocator);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
setBlockMapAddr(uint32_t Addr)630b57cec5SDimitry Andric Error MSFBuilder::setBlockMapAddr(uint32_t Addr) {
640b57cec5SDimitry Andric if (Addr == BlockMapAddr)
650b57cec5SDimitry Andric return Error::success();
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric if (Addr >= FreeBlocks.size()) {
680b57cec5SDimitry Andric if (!IsGrowable)
690b57cec5SDimitry Andric return make_error<MSFError>(msf_error_code::insufficient_buffer,
700b57cec5SDimitry Andric "Cannot grow the number of blocks");
710b57cec5SDimitry Andric FreeBlocks.resize(Addr + 1, true);
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric if (!isBlockFree(Addr))
750b57cec5SDimitry Andric return make_error<MSFError>(
760b57cec5SDimitry Andric msf_error_code::block_in_use,
770b57cec5SDimitry Andric "Requested block map address is already in use");
780b57cec5SDimitry Andric FreeBlocks[BlockMapAddr] = true;
790b57cec5SDimitry Andric FreeBlocks[Addr] = false;
800b57cec5SDimitry Andric BlockMapAddr = Addr;
810b57cec5SDimitry Andric return Error::success();
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric
setFreePageMap(uint32_t Fpm)840b57cec5SDimitry Andric void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; }
850b57cec5SDimitry Andric
setUnknown1(uint32_t Unk1)860b57cec5SDimitry Andric void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; }
870b57cec5SDimitry Andric
setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks)880b57cec5SDimitry Andric Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) {
890b57cec5SDimitry Andric for (auto B : DirectoryBlocks)
900b57cec5SDimitry Andric FreeBlocks[B] = true;
910b57cec5SDimitry Andric for (auto B : DirBlocks) {
920b57cec5SDimitry Andric if (!isBlockFree(B)) {
930b57cec5SDimitry Andric return make_error<MSFError>(msf_error_code::unspecified,
940b57cec5SDimitry Andric "Attempt to reuse an allocated block");
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric FreeBlocks[B] = false;
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric DirectoryBlocks = DirBlocks;
1000b57cec5SDimitry Andric return Error::success();
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric
allocateBlocks(uint32_t NumBlocks,MutableArrayRef<uint32_t> Blocks)1030b57cec5SDimitry Andric Error MSFBuilder::allocateBlocks(uint32_t NumBlocks,
1040b57cec5SDimitry Andric MutableArrayRef<uint32_t> Blocks) {
1050b57cec5SDimitry Andric if (NumBlocks == 0)
1060b57cec5SDimitry Andric return Error::success();
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric uint32_t NumFreeBlocks = FreeBlocks.count();
1090b57cec5SDimitry Andric if (NumFreeBlocks < NumBlocks) {
1100b57cec5SDimitry Andric if (!IsGrowable)
1110b57cec5SDimitry Andric return make_error<MSFError>(msf_error_code::insufficient_buffer,
1120b57cec5SDimitry Andric "There are no free Blocks in the file");
1130b57cec5SDimitry Andric uint32_t AllocBlocks = NumBlocks - NumFreeBlocks;
1140b57cec5SDimitry Andric uint32_t OldBlockCount = FreeBlocks.size();
1150b57cec5SDimitry Andric uint32_t NewBlockCount = AllocBlocks + OldBlockCount;
1160b57cec5SDimitry Andric uint32_t NextFpmBlock = alignTo(OldBlockCount, BlockSize) + 1;
1170b57cec5SDimitry Andric FreeBlocks.resize(NewBlockCount, true);
1180b57cec5SDimitry Andric // If we crossed over an fpm page, we actually need to allocate 2 extra
1190b57cec5SDimitry Andric // blocks for each FPM group crossed and mark both blocks from the group as
1200b57cec5SDimitry Andric // used. FPM blocks are marked as allocated regardless of whether or not
1210b57cec5SDimitry Andric // they ultimately describe the status of blocks in the file. This means
1220b57cec5SDimitry Andric // that not only are extraneous blocks at the end of the main FPM marked as
1230b57cec5SDimitry Andric // allocated, but also blocks from the alternate FPM are always marked as
1240b57cec5SDimitry Andric // allocated.
1250b57cec5SDimitry Andric while (NextFpmBlock < NewBlockCount) {
1260b57cec5SDimitry Andric NewBlockCount += 2;
1270b57cec5SDimitry Andric FreeBlocks.resize(NewBlockCount, true);
1280b57cec5SDimitry Andric FreeBlocks.reset(NextFpmBlock, NextFpmBlock + 2);
1290b57cec5SDimitry Andric NextFpmBlock += BlockSize;
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric int I = 0;
1340b57cec5SDimitry Andric int Block = FreeBlocks.find_first();
1350b57cec5SDimitry Andric do {
1360b57cec5SDimitry Andric assert(Block != -1 && "We ran out of Blocks!");
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric uint32_t NextBlock = static_cast<uint32_t>(Block);
1390b57cec5SDimitry Andric Blocks[I++] = NextBlock;
1400b57cec5SDimitry Andric FreeBlocks.reset(NextBlock);
1410b57cec5SDimitry Andric Block = FreeBlocks.find_next(Block);
1420b57cec5SDimitry Andric } while (--NumBlocks > 0);
1430b57cec5SDimitry Andric return Error::success();
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric
getNumUsedBlocks() const1460b57cec5SDimitry Andric uint32_t MSFBuilder::getNumUsedBlocks() const {
1470b57cec5SDimitry Andric return getTotalBlockCount() - getNumFreeBlocks();
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric
getNumFreeBlocks() const1500b57cec5SDimitry Andric uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); }
1510b57cec5SDimitry Andric
getTotalBlockCount() const1520b57cec5SDimitry Andric uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); }
1530b57cec5SDimitry Andric
isBlockFree(uint32_t Idx) const1540b57cec5SDimitry Andric bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; }
1550b57cec5SDimitry Andric
addStream(uint32_t Size,ArrayRef<uint32_t> Blocks)1560b57cec5SDimitry Andric Expected<uint32_t> MSFBuilder::addStream(uint32_t Size,
1570b57cec5SDimitry Andric ArrayRef<uint32_t> Blocks) {
1580b57cec5SDimitry Andric // Add a new stream mapped to the specified blocks. Verify that the specified
1590b57cec5SDimitry Andric // blocks are both necessary and sufficient for holding the requested number
1600b57cec5SDimitry Andric // of bytes, and verify that all requested blocks are free.
1610b57cec5SDimitry Andric uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
1620b57cec5SDimitry Andric if (ReqBlocks != Blocks.size())
1630b57cec5SDimitry Andric return make_error<MSFError>(
1640b57cec5SDimitry Andric msf_error_code::invalid_format,
1650b57cec5SDimitry Andric "Incorrect number of blocks for requested stream size");
1660b57cec5SDimitry Andric for (auto Block : Blocks) {
1670b57cec5SDimitry Andric if (Block >= FreeBlocks.size())
1680b57cec5SDimitry Andric FreeBlocks.resize(Block + 1, true);
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric if (!FreeBlocks.test(Block))
1710b57cec5SDimitry Andric return make_error<MSFError>(
1720b57cec5SDimitry Andric msf_error_code::unspecified,
1730b57cec5SDimitry Andric "Attempt to re-use an already allocated block");
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric // Mark all the blocks occupied by the new stream as not free.
1760b57cec5SDimitry Andric for (auto Block : Blocks) {
1770b57cec5SDimitry Andric FreeBlocks.reset(Block);
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric StreamData.push_back(std::make_pair(Size, Blocks));
1800b57cec5SDimitry Andric return StreamData.size() - 1;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric
addStream(uint32_t Size)1830b57cec5SDimitry Andric Expected<uint32_t> MSFBuilder::addStream(uint32_t Size) {
1840b57cec5SDimitry Andric uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
1850b57cec5SDimitry Andric std::vector<uint32_t> NewBlocks;
1860b57cec5SDimitry Andric NewBlocks.resize(ReqBlocks);
1870b57cec5SDimitry Andric if (auto EC = allocateBlocks(ReqBlocks, NewBlocks))
1880b57cec5SDimitry Andric return std::move(EC);
1890b57cec5SDimitry Andric StreamData.push_back(std::make_pair(Size, NewBlocks));
1900b57cec5SDimitry Andric return StreamData.size() - 1;
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric
setStreamSize(uint32_t Idx,uint32_t Size)1930b57cec5SDimitry Andric Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) {
1940b57cec5SDimitry Andric uint32_t OldSize = getStreamSize(Idx);
1950b57cec5SDimitry Andric if (OldSize == Size)
1960b57cec5SDimitry Andric return Error::success();
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric uint32_t NewBlocks = bytesToBlocks(Size, BlockSize);
1990b57cec5SDimitry Andric uint32_t OldBlocks = bytesToBlocks(OldSize, BlockSize);
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andric if (NewBlocks > OldBlocks) {
2020b57cec5SDimitry Andric uint32_t AddedBlocks = NewBlocks - OldBlocks;
2030b57cec5SDimitry Andric // If we're growing, we have to allocate new Blocks.
2040b57cec5SDimitry Andric std::vector<uint32_t> AddedBlockList;
2050b57cec5SDimitry Andric AddedBlockList.resize(AddedBlocks);
2060b57cec5SDimitry Andric if (auto EC = allocateBlocks(AddedBlocks, AddedBlockList))
2070b57cec5SDimitry Andric return EC;
2080b57cec5SDimitry Andric auto &CurrentBlocks = StreamData[Idx].second;
209e8d8bef9SDimitry Andric llvm::append_range(CurrentBlocks, AddedBlockList);
2100b57cec5SDimitry Andric } else if (OldBlocks > NewBlocks) {
2110b57cec5SDimitry Andric // For shrinking, free all the Blocks in the Block map, update the stream
2120b57cec5SDimitry Andric // data, then shrink the directory.
2130b57cec5SDimitry Andric uint32_t RemovedBlocks = OldBlocks - NewBlocks;
2140b57cec5SDimitry Andric auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second);
2150b57cec5SDimitry Andric auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks);
2160b57cec5SDimitry Andric for (auto P : RemovedBlockList)
2170b57cec5SDimitry Andric FreeBlocks[P] = true;
2180b57cec5SDimitry Andric StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks);
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric
2210b57cec5SDimitry Andric StreamData[Idx].first = Size;
2220b57cec5SDimitry Andric return Error::success();
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric
getNumStreams() const2250b57cec5SDimitry Andric uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); }
2260b57cec5SDimitry Andric
getStreamSize(uint32_t StreamIdx) const2270b57cec5SDimitry Andric uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const {
2280b57cec5SDimitry Andric return StreamData[StreamIdx].first;
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric
getStreamBlocks(uint32_t StreamIdx) const2310b57cec5SDimitry Andric ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const {
2320b57cec5SDimitry Andric return StreamData[StreamIdx].second;
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric
computeDirectoryByteSize() const2350b57cec5SDimitry Andric uint32_t MSFBuilder::computeDirectoryByteSize() const {
2360b57cec5SDimitry Andric // The directory has the following layout, where each item is a ulittle32_t:
2370b57cec5SDimitry Andric // NumStreams
2380b57cec5SDimitry Andric // StreamSizes[NumStreams]
2390b57cec5SDimitry Andric // StreamBlocks[NumStreams][]
2400b57cec5SDimitry Andric uint32_t Size = sizeof(ulittle32_t); // NumStreams
2410b57cec5SDimitry Andric Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes
2420b57cec5SDimitry Andric for (const auto &D : StreamData) {
2430b57cec5SDimitry Andric uint32_t ExpectedNumBlocks = bytesToBlocks(D.first, BlockSize);
2440b57cec5SDimitry Andric assert(ExpectedNumBlocks == D.second.size() &&
2450b57cec5SDimitry Andric "Unexpected number of blocks");
2460b57cec5SDimitry Andric Size += ExpectedNumBlocks * sizeof(ulittle32_t);
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric return Size;
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric
generateLayout()2510b57cec5SDimitry Andric Expected<MSFLayout> MSFBuilder::generateLayout() {
252*5f757f3fSDimitry Andric llvm::TimeTraceScope timeScope("MSF: Generate layout");
253*5f757f3fSDimitry Andric
2540b57cec5SDimitry Andric SuperBlock *SB = Allocator.Allocate<SuperBlock>();
2550b57cec5SDimitry Andric MSFLayout L;
2560b57cec5SDimitry Andric L.SB = SB;
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric std::memcpy(SB->MagicBytes, Magic, sizeof(Magic));
2590b57cec5SDimitry Andric SB->BlockMapAddr = BlockMapAddr;
2600b57cec5SDimitry Andric SB->BlockSize = BlockSize;
2610b57cec5SDimitry Andric SB->NumDirectoryBytes = computeDirectoryByteSize();
2620b57cec5SDimitry Andric SB->FreeBlockMapBlock = FreePageMap;
2630b57cec5SDimitry Andric SB->Unknown1 = Unknown1;
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric uint32_t NumDirectoryBlocks = bytesToBlocks(SB->NumDirectoryBytes, BlockSize);
2660b57cec5SDimitry Andric if (NumDirectoryBlocks > DirectoryBlocks.size()) {
2670b57cec5SDimitry Andric // Our hint wasn't enough to satisfy the entire directory. Allocate
2680b57cec5SDimitry Andric // remaining pages.
2690b57cec5SDimitry Andric std::vector<uint32_t> ExtraBlocks;
2700b57cec5SDimitry Andric uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size();
2710b57cec5SDimitry Andric ExtraBlocks.resize(NumExtraBlocks);
2720b57cec5SDimitry Andric if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks))
2730b57cec5SDimitry Andric return std::move(EC);
274e8d8bef9SDimitry Andric llvm::append_range(DirectoryBlocks, ExtraBlocks);
2750b57cec5SDimitry Andric } else if (NumDirectoryBlocks < DirectoryBlocks.size()) {
2760b57cec5SDimitry Andric uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks;
2770b57cec5SDimitry Andric for (auto B :
2780b57cec5SDimitry Andric ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks))
2790b57cec5SDimitry Andric FreeBlocks[B] = true;
2800b57cec5SDimitry Andric DirectoryBlocks.resize(NumDirectoryBlocks);
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andric // Don't set the number of blocks in the file until after allocating Blocks
2840b57cec5SDimitry Andric // for the directory, since the allocation might cause the file to need to
2850b57cec5SDimitry Andric // grow.
2860b57cec5SDimitry Andric SB->NumBlocks = FreeBlocks.size();
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks);
2890b57cec5SDimitry Andric std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks,
2900b57cec5SDimitry Andric DirBlocks);
2910b57cec5SDimitry Andric L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks);
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric // The stream sizes should be re-allocated as a stable pointer and the stream
2940b57cec5SDimitry Andric // map should have each of its entries allocated as a separate stable pointer.
2950b57cec5SDimitry Andric if (!StreamData.empty()) {
2960b57cec5SDimitry Andric ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size());
2970b57cec5SDimitry Andric L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size());
2980b57cec5SDimitry Andric L.StreamMap.resize(StreamData.size());
2990b57cec5SDimitry Andric for (uint32_t I = 0; I < StreamData.size(); ++I) {
3000b57cec5SDimitry Andric Sizes[I] = StreamData[I].first;
3010b57cec5SDimitry Andric ulittle32_t *BlockList =
3020b57cec5SDimitry Andric Allocator.Allocate<ulittle32_t>(StreamData[I].second.size());
3030b57cec5SDimitry Andric std::uninitialized_copy_n(StreamData[I].second.begin(),
3040b57cec5SDimitry Andric StreamData[I].second.size(), BlockList);
3050b57cec5SDimitry Andric L.StreamMap[I] =
3060b57cec5SDimitry Andric ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size());
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric }
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andric L.FreePageMap = FreeBlocks;
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric return L;
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric
commitFpm(WritableBinaryStream & MsfBuffer,const MSFLayout & Layout,BumpPtrAllocator & Allocator)3150b57cec5SDimitry Andric static void commitFpm(WritableBinaryStream &MsfBuffer, const MSFLayout &Layout,
3160b57cec5SDimitry Andric BumpPtrAllocator &Allocator) {
3170b57cec5SDimitry Andric auto FpmStream =
3180b57cec5SDimitry Andric WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator);
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric // We only need to create the alt fpm stream so that it gets initialized.
3210b57cec5SDimitry Andric WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator,
3220b57cec5SDimitry Andric true);
3230b57cec5SDimitry Andric
3240b57cec5SDimitry Andric uint32_t BI = 0;
3250b57cec5SDimitry Andric BinaryStreamWriter FpmWriter(*FpmStream);
3260b57cec5SDimitry Andric while (BI < Layout.SB->NumBlocks) {
3270b57cec5SDimitry Andric uint8_t ThisByte = 0;
3280b57cec5SDimitry Andric for (uint32_t I = 0; I < 8; ++I) {
3290b57cec5SDimitry Andric bool IsFree =
3300b57cec5SDimitry Andric (BI < Layout.SB->NumBlocks) ? Layout.FreePageMap.test(BI) : true;
3310b57cec5SDimitry Andric uint8_t Mask = uint8_t(IsFree) << I;
3320b57cec5SDimitry Andric ThisByte |= Mask;
3330b57cec5SDimitry Andric ++BI;
3340b57cec5SDimitry Andric }
3350b57cec5SDimitry Andric cantFail(FpmWriter.writeObject(ThisByte));
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric assert(FpmWriter.bytesRemaining() == 0);
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric
commit(StringRef Path,MSFLayout & Layout)3400b57cec5SDimitry Andric Expected<FileBufferByteStream> MSFBuilder::commit(StringRef Path,
3410b57cec5SDimitry Andric MSFLayout &Layout) {
342*5f757f3fSDimitry Andric llvm::TimeTraceScope timeScope("Commit MSF");
343*5f757f3fSDimitry Andric
3440b57cec5SDimitry Andric Expected<MSFLayout> L = generateLayout();
3450b57cec5SDimitry Andric if (!L)
3460b57cec5SDimitry Andric return L.takeError();
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andric Layout = std::move(*L);
3490b57cec5SDimitry Andric
350fe6060f1SDimitry Andric uint64_t FileSize = uint64_t(Layout.SB->BlockSize) * Layout.SB->NumBlocks;
3510eae32dcSDimitry Andric // Ensure that the file size is under the limit for the specified block size.
3520eae32dcSDimitry Andric if (FileSize > getMaxFileSizeFromBlockSize(Layout.SB->BlockSize)) {
3530eae32dcSDimitry Andric msf_error_code error_code = [](uint32_t BlockSize) {
3540eae32dcSDimitry Andric switch (BlockSize) {
3550eae32dcSDimitry Andric case 8192:
3560eae32dcSDimitry Andric return msf_error_code::size_overflow_8192;
3570eae32dcSDimitry Andric case 16384:
3580eae32dcSDimitry Andric return msf_error_code::size_overflow_16384;
3590eae32dcSDimitry Andric case 32768:
3600eae32dcSDimitry Andric return msf_error_code::size_overflow_32768;
3610eae32dcSDimitry Andric default:
3620eae32dcSDimitry Andric return msf_error_code::size_overflow_4096;
3630eae32dcSDimitry Andric }
3640eae32dcSDimitry Andric }(Layout.SB->BlockSize);
3650eae32dcSDimitry Andric
366fe6060f1SDimitry Andric return make_error<MSFError>(
3670eae32dcSDimitry Andric error_code,
3680eae32dcSDimitry Andric formatv("File size {0,1:N} too large for current PDB page size {1}",
3690eae32dcSDimitry Andric FileSize, Layout.SB->BlockSize));
370fe6060f1SDimitry Andric }
371fe6060f1SDimitry Andric
37206c3fb27SDimitry Andric uint64_t NumDirectoryBlocks =
37306c3fb27SDimitry Andric bytesToBlocks(Layout.SB->NumDirectoryBytes, Layout.SB->BlockSize);
37406c3fb27SDimitry Andric uint64_t DirectoryBlockMapSize =
37506c3fb27SDimitry Andric NumDirectoryBlocks * sizeof(support::ulittle32_t);
37606c3fb27SDimitry Andric if (DirectoryBlockMapSize > Layout.SB->BlockSize) {
37706c3fb27SDimitry Andric return make_error<MSFError>(msf_error_code::stream_directory_overflow,
37806c3fb27SDimitry Andric formatv("The directory block map ({0} bytes) "
37906c3fb27SDimitry Andric "doesn't fit in a block ({1} bytes)",
38006c3fb27SDimitry Andric DirectoryBlockMapSize,
38106c3fb27SDimitry Andric Layout.SB->BlockSize));
38206c3fb27SDimitry Andric }
38306c3fb27SDimitry Andric
3840b57cec5SDimitry Andric auto OutFileOrError = FileOutputBuffer::create(Path, FileSize);
3850b57cec5SDimitry Andric if (auto EC = OutFileOrError.takeError())
3860b57cec5SDimitry Andric return std::move(EC);
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andric FileBufferByteStream Buffer(std::move(*OutFileOrError),
389*5f757f3fSDimitry Andric llvm::endianness::little);
3900b57cec5SDimitry Andric BinaryStreamWriter Writer(Buffer);
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andric if (auto EC = Writer.writeObject(*Layout.SB))
3930b57cec5SDimitry Andric return std::move(EC);
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andric commitFpm(Buffer, Layout, Allocator);
3960b57cec5SDimitry Andric
3970b57cec5SDimitry Andric uint32_t BlockMapOffset =
3980b57cec5SDimitry Andric msf::blockToOffset(Layout.SB->BlockMapAddr, Layout.SB->BlockSize);
3990b57cec5SDimitry Andric Writer.setOffset(BlockMapOffset);
4000b57cec5SDimitry Andric if (auto EC = Writer.writeArray(Layout.DirectoryBlocks))
4010b57cec5SDimitry Andric return std::move(EC);
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric auto DirStream = WritableMappedBlockStream::createDirectoryStream(
4040b57cec5SDimitry Andric Layout, Buffer, Allocator);
4050b57cec5SDimitry Andric BinaryStreamWriter DW(*DirStream);
4060b57cec5SDimitry Andric if (auto EC = DW.writeInteger<uint32_t>(Layout.StreamSizes.size()))
4070b57cec5SDimitry Andric return std::move(EC);
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andric if (auto EC = DW.writeArray(Layout.StreamSizes))
4100b57cec5SDimitry Andric return std::move(EC);
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andric for (const auto &Blocks : Layout.StreamMap) {
4130b57cec5SDimitry Andric if (auto EC = DW.writeArray(Blocks))
4140b57cec5SDimitry Andric return std::move(EC);
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
4170b57cec5SDimitry Andric return std::move(Buffer);
4180b57cec5SDimitry Andric }
419