blob: 62960b67db23c33aa2172ca3fc09ebb0c42f9ef4 [file] [log] [blame]
Zachary Turnerf52a8992016-07-15 20:43:38 +00001//
2// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8
Zachary Turnera3225b02016-07-29 20:56:36 +00009#include "llvm/DebugInfo/MSF/MSFBuilder.h"
10#include "llvm/DebugInfo/MSF/MSFError.h"
Zachary Turnerf52a8992016-07-15 20:43:38 +000011
12using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000013using namespace llvm::msf;
Zachary Turnerf52a8992016-07-15 20:43:38 +000014using namespace llvm::support;
15
16namespace {
17const uint32_t kSuperBlockBlock = 0;
Zachary Turnerb927e022016-07-15 22:17:19 +000018const uint32_t kFreePageMap0Block = 1;
19const uint32_t kFreePageMap1Block = 2;
20const uint32_t kNumReservedPages = 3;
21
22const uint32_t kDefaultBlockMapAddr = kNumReservedPages;
Zachary Turnerf52a8992016-07-15 20:43:38 +000023}
24
Zachary Turnera3225b02016-07-29 20:56:36 +000025MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow,
Zachary Turnerf52a8992016-07-15 20:43:38 +000026 BumpPtrAllocator &Allocator)
Saleem Abdulrasoolea6a4fe2016-07-15 21:10:31 +000027 : Allocator(Allocator), IsGrowable(CanGrow), BlockSize(BlockSize),
28 MininumBlocks(MinBlockCount), BlockMapAddr(kDefaultBlockMapAddr),
Zachary Turnerb927e022016-07-15 22:17:19 +000029 FreeBlocks(MinBlockCount, true) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000030 FreeBlocks[kSuperBlockBlock] = false;
Zachary Turnerb927e022016-07-15 22:17:19 +000031 FreeBlocks[kFreePageMap0Block] = false;
32 FreeBlocks[kFreePageMap1Block] = false;
Zachary Turnerf52a8992016-07-15 20:43:38 +000033 FreeBlocks[BlockMapAddr] = false;
34}
35
Zachary Turnera3225b02016-07-29 20:56:36 +000036Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator,
Zachary Turnerf52a8992016-07-15 20:43:38 +000037 uint32_t BlockSize,
38 uint32_t MinBlockCount, bool CanGrow) {
Zachary Turnerbac69d32016-07-22 19:56:05 +000039 if (!isValidBlockSize(BlockSize))
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 "The requested block size is unsupported");
42
Zachary Turnera3225b02016-07-29 20:56:36 +000043 return MSFBuilder(BlockSize,
Zachary Turnerb927e022016-07-15 22:17:19 +000044 std::max(MinBlockCount, msf::getMinimumBlockCount()),
45 CanGrow, Allocator);
Zachary Turnerf52a8992016-07-15 20:43:38 +000046}
47
Zachary Turnera3225b02016-07-29 20:56:36 +000048Error MSFBuilder::setBlockMapAddr(uint32_t Addr) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000049 if (Addr == BlockMapAddr)
50 return Error::success();
51
52 if (Addr >= FreeBlocks.size()) {
53 if (!IsGrowable)
Zachary Turnera3225b02016-07-29 20:56:36 +000054 return make_error<MSFError>(msf_error_code::insufficient_buffer,
Zachary Turnerf52a8992016-07-15 20:43:38 +000055 "Cannot grow the number of blocks");
56 FreeBlocks.resize(Addr + 1);
57 }
58
59 if (!isBlockFree(Addr))
Zachary Turnera3225b02016-07-29 20:56:36 +000060 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +000061 msf_error_code::block_in_use,
62 "Requested block map address is already in use");
Zachary Turnerf52a8992016-07-15 20:43:38 +000063 FreeBlocks[BlockMapAddr] = true;
64 FreeBlocks[Addr] = false;
65 BlockMapAddr = Addr;
66 return Error::success();
67}
68
Zachary Turnera3225b02016-07-29 20:56:36 +000069void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; }
Zachary Turnerfaa554b2016-07-15 22:16:56 +000070
Zachary Turnera3225b02016-07-29 20:56:36 +000071void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; }
Zachary Turnerfaa554b2016-07-15 22:16:56 +000072
Zachary Turnera3225b02016-07-29 20:56:36 +000073Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000074 for (auto B : DirectoryBlocks)
75 FreeBlocks[B] = true;
76 for (auto B : DirBlocks) {
77 if (!isBlockFree(B)) {
Zachary Turnera3225b02016-07-29 20:56:36 +000078 return make_error<MSFError>(msf_error_code::unspecified,
Zachary Turnerfaa554b2016-07-15 22:16:56 +000079 "Attempt to reuse an allocated block");
80 }
81 FreeBlocks[B] = false;
82 }
83
84 DirectoryBlocks = DirBlocks;
85 return Error::success();
86}
87
Zachary Turnera3225b02016-07-29 20:56:36 +000088Error MSFBuilder::allocateBlocks(uint32_t NumBlocks,
Zachary Turnerf52a8992016-07-15 20:43:38 +000089 MutableArrayRef<uint32_t> Blocks) {
90 if (NumBlocks == 0)
91 return Error::success();
92
93 uint32_t NumFreeBlocks = FreeBlocks.count();
94 if (NumFreeBlocks < NumBlocks) {
95 if (!IsGrowable)
Zachary Turnera3225b02016-07-29 20:56:36 +000096 return make_error<MSFError>(msf_error_code::insufficient_buffer,
Zachary Turnerf52a8992016-07-15 20:43:38 +000097 "There are no free Blocks in the file");
98 uint32_t AllocBlocks = NumBlocks - NumFreeBlocks;
99 FreeBlocks.resize(AllocBlocks + FreeBlocks.size(), true);
100 }
101
102 int I = 0;
103 int Block = FreeBlocks.find_first();
104 do {
105 assert(Block != -1 && "We ran out of Blocks!");
106
107 uint32_t NextBlock = static_cast<uint32_t>(Block);
108 Blocks[I++] = NextBlock;
109 FreeBlocks.reset(NextBlock);
110 Block = FreeBlocks.find_next(Block);
111 } while (--NumBlocks > 0);
112 return Error::success();
113}
114
Zachary Turnera3225b02016-07-29 20:56:36 +0000115uint32_t MSFBuilder::getNumUsedBlocks() const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000116 return getTotalBlockCount() - getNumFreeBlocks();
117}
118
Zachary Turnera3225b02016-07-29 20:56:36 +0000119uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000120
Zachary Turnera3225b02016-07-29 20:56:36 +0000121uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000122
Zachary Turnera3225b02016-07-29 20:56:36 +0000123bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000124
Zachary Turnera3225b02016-07-29 20:56:36 +0000125Error MSFBuilder::addStream(uint32_t Size, ArrayRef<uint32_t> Blocks) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000126 // Add a new stream mapped to the specified blocks. Verify that the specified
127 // blocks are both necessary and sufficient for holding the requested number
128 // of bytes, and verify that all requested blocks are free.
129 uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
130 if (ReqBlocks != Blocks.size())
Zachary Turnera3225b02016-07-29 20:56:36 +0000131 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +0000132 msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +0000133 "Incorrect number of blocks for requested stream size");
134 for (auto Block : Blocks) {
135 if (Block >= FreeBlocks.size())
136 FreeBlocks.resize(Block + 1, true);
137
138 if (!FreeBlocks.test(Block))
Zachary Turnera3225b02016-07-29 20:56:36 +0000139 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +0000140 msf_error_code::unspecified,
Zachary Turnerf52a8992016-07-15 20:43:38 +0000141 "Attempt to re-use an already allocated block");
142 }
143 // Mark all the blocks occupied by the new stream as not free.
144 for (auto Block : Blocks) {
145 FreeBlocks.reset(Block);
146 }
147 StreamData.push_back(std::make_pair(Size, Blocks));
148 return Error::success();
149}
150
Zachary Turnera3225b02016-07-29 20:56:36 +0000151Error MSFBuilder::addStream(uint32_t Size) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000152 uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
153 std::vector<uint32_t> NewBlocks;
154 NewBlocks.resize(ReqBlocks);
155 if (auto EC = allocateBlocks(ReqBlocks, NewBlocks))
156 return EC;
157 StreamData.push_back(std::make_pair(Size, NewBlocks));
158 return Error::success();
159}
160
Zachary Turnera3225b02016-07-29 20:56:36 +0000161Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000162 uint32_t OldSize = getStreamSize(Idx);
163 if (OldSize == Size)
164 return Error::success();
165
166 uint32_t NewBlocks = bytesToBlocks(Size, BlockSize);
167 uint32_t OldBlocks = bytesToBlocks(OldSize, BlockSize);
168
169 if (NewBlocks > OldBlocks) {
170 uint32_t AddedBlocks = NewBlocks - OldBlocks;
171 // If we're growing, we have to allocate new Blocks.
172 std::vector<uint32_t> AddedBlockList;
173 AddedBlockList.resize(AddedBlocks);
174 if (auto EC = allocateBlocks(AddedBlocks, AddedBlockList))
175 return EC;
176 auto &CurrentBlocks = StreamData[Idx].second;
177 CurrentBlocks.insert(CurrentBlocks.end(), AddedBlockList.begin(),
178 AddedBlockList.end());
179 } else if (OldBlocks > NewBlocks) {
180 // For shrinking, free all the Blocks in the Block map, update the stream
181 // data, then shrink the directory.
182 uint32_t RemovedBlocks = OldBlocks - NewBlocks;
183 auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second);
184 auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks);
185 for (auto P : RemovedBlockList)
186 FreeBlocks[P] = true;
187 StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks);
188 }
189
190 StreamData[Idx].first = Size;
191 return Error::success();
192}
193
Zachary Turnera3225b02016-07-29 20:56:36 +0000194uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000195
Zachary Turnera3225b02016-07-29 20:56:36 +0000196uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000197 return StreamData[StreamIdx].first;
198}
199
Zachary Turnera3225b02016-07-29 20:56:36 +0000200ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000201 return StreamData[StreamIdx].second;
202}
203
Zachary Turnera3225b02016-07-29 20:56:36 +0000204uint32_t MSFBuilder::computeDirectoryByteSize() const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000205 // The directory has the following layout, where each item is a ulittle32_t:
206 // NumStreams
207 // StreamSizes[NumStreams]
208 // StreamBlocks[NumStreams][]
209 uint32_t Size = sizeof(ulittle32_t); // NumStreams
210 Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes
211 for (const auto &D : StreamData) {
212 uint32_t ExpectedNumBlocks = bytesToBlocks(D.first, BlockSize);
213 assert(ExpectedNumBlocks == D.second.size() &&
214 "Unexpected number of blocks");
215 Size += ExpectedNumBlocks * sizeof(ulittle32_t);
216 }
217 return Size;
218}
219
Zachary Turnera3225b02016-07-29 20:56:36 +0000220Expected<MSFLayout> MSFBuilder::build() {
Zachary Turnere4a4f332016-07-22 19:56:33 +0000221 SuperBlock *SB = Allocator.Allocate<SuperBlock>();
Zachary Turnera3225b02016-07-29 20:56:36 +0000222 MSFLayout L;
Zachary Turnere4a4f332016-07-22 19:56:33 +0000223 L.SB = SB;
Zachary Turnerf52a8992016-07-15 20:43:38 +0000224
Zachary Turnere4a4f332016-07-22 19:56:33 +0000225 std::memcpy(SB->MagicBytes, Magic, sizeof(Magic));
226 SB->BlockMapAddr = BlockMapAddr;
227 SB->BlockSize = BlockSize;
228 SB->NumDirectoryBytes = computeDirectoryByteSize();
229 SB->FreeBlockMapBlock = FreePageMap;
230 SB->Unknown1 = Unknown1;
231
232 uint32_t NumDirectoryBlocks = bytesToBlocks(SB->NumDirectoryBytes, BlockSize);
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000233 if (NumDirectoryBlocks > DirectoryBlocks.size()) {
234 // Our hint wasn't enough to satisfy the entire directory. Allocate
235 // remaining pages.
236 std::vector<uint32_t> ExtraBlocks;
237 uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size();
238 ExtraBlocks.resize(NumExtraBlocks);
239 if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks))
240 return std::move(EC);
241 DirectoryBlocks.insert(DirectoryBlocks.end(), ExtraBlocks.begin(),
242 ExtraBlocks.end());
243 } else if (NumDirectoryBlocks < DirectoryBlocks.size()) {
244 uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks;
245 for (auto B :
246 ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks))
247 FreeBlocks[B] = true;
248 DirectoryBlocks.resize(NumDirectoryBlocks);
249 }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000250
251 // Don't set the number of blocks in the file until after allocating Blocks
Zachary Turnerbac69d32016-07-22 19:56:05 +0000252 // for the directory, since the allocation might cause the file to need to
253 // grow.
Zachary Turnere4a4f332016-07-22 19:56:33 +0000254 SB->NumBlocks = FreeBlocks.size();
Zachary Turnerf52a8992016-07-15 20:43:38 +0000255
256 ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks);
257 std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks,
258 DirBlocks);
259 L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks);
260
261 // The stream sizes should be re-allocated as a stable pointer and the stream
262 // map should have each of its entries allocated as a separate stable pointer.
263 if (StreamData.size() > 0) {
264 ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size());
265 L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size());
266 L.StreamMap.resize(StreamData.size());
267 for (uint32_t I = 0; I < StreamData.size(); ++I) {
268 Sizes[I] = StreamData[I].first;
269 ulittle32_t *BlockList =
270 Allocator.Allocate<ulittle32_t>(StreamData[I].second.size());
271 std::uninitialized_copy_n(StreamData[I].second.begin(),
272 StreamData[I].second.size(), BlockList);
273 L.StreamMap[I] =
274 ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size());
275 }
276 }
277
278 return L;
279}