2016-04-26 01:38:08 +08:00
|
|
|
//===- PDBFile.cpp - Low level interface to a PDB file ----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-01-26 06:38:55 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
|
2016-04-26 01:38:08 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2016-11-24 07:16:32 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/DebugInfo/MSF/MSFCommon.h"
|
2017-01-21 06:41:15 +08:00
|
|
|
#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
|
2017-01-26 06:38:55 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
|
2017-05-03 02:00:13 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
|
2017-01-26 06:38:55 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/RawError.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
|
2017-03-03 04:52:51 +08:00
|
|
|
#include "llvm/Support/BinaryStream.h"
|
|
|
|
#include "llvm/Support/BinaryStreamArray.h"
|
|
|
|
#include "llvm/Support/BinaryStreamReader.h"
|
2016-04-26 01:38:08 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2016-11-24 07:16:32 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-02-17 07:35:45 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-11-24 07:16:32 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2016-04-26 01:38:08 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-06-10 13:10:19 +08:00
|
|
|
using namespace llvm::codeview;
|
2016-07-23 03:56:05 +08:00
|
|
|
using namespace llvm::msf;
|
2016-04-30 01:28:47 +08:00
|
|
|
using namespace llvm::pdb;
|
2016-04-26 01:38:08 +08:00
|
|
|
|
|
|
|
namespace {
|
2016-06-10 13:10:19 +08:00
|
|
|
typedef FixedStreamArray<support::ulittle32_t> ulittle_array;
|
2016-11-24 07:16:32 +08:00
|
|
|
} // end anonymous namespace
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
PDBFile::PDBFile(StringRef Path, std::unique_ptr<BinaryStream> PdbFileBuffer,
|
2016-07-23 03:56:26 +08:00
|
|
|
BumpPtrAllocator &Allocator)
|
2017-02-17 07:35:45 +08:00
|
|
|
: FilePath(Path), Allocator(Allocator), Buffer(std::move(PdbFileBuffer)) {}
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-11-24 07:16:32 +08:00
|
|
|
PDBFile::~PDBFile() = default;
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2017-02-17 07:35:45 +08:00
|
|
|
StringRef PDBFile::getFilePath() const { return FilePath; }
|
|
|
|
|
|
|
|
StringRef PDBFile::getFileDirectory() const {
|
|
|
|
return sys::path::parent_path(FilePath);
|
|
|
|
}
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t PDBFile::getBlockSize() const { return ContainerLayout.SB->BlockSize; }
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-23 03:56:33 +08:00
|
|
|
uint32_t PDBFile::getFreeBlockMapBlock() const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ContainerLayout.SB->FreeBlockMapBlock;
|
2016-07-23 03:56:33 +08:00
|
|
|
}
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t PDBFile::getBlockCount() const {
|
|
|
|
return ContainerLayout.SB->NumBlocks;
|
|
|
|
}
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-23 03:56:33 +08:00
|
|
|
uint32_t PDBFile::getNumDirectoryBytes() const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ContainerLayout.SB->NumDirectoryBytes;
|
2016-07-23 03:56:33 +08:00
|
|
|
}
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-23 03:56:33 +08:00
|
|
|
uint32_t PDBFile::getBlockMapIndex() const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ContainerLayout.SB->BlockMapAddr;
|
2016-07-23 03:56:33 +08:00
|
|
|
}
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t PDBFile::getUnknown1() const { return ContainerLayout.SB->Unknown1; }
|
2016-04-26 01:38:08 +08:00
|
|
|
|
|
|
|
uint32_t PDBFile::getNumDirectoryBlocks() const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return msf::bytesToBlocks(ContainerLayout.SB->NumDirectoryBytes,
|
|
|
|
ContainerLayout.SB->BlockSize);
|
2016-04-26 01:38:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t PDBFile::getBlockMapOffset() const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return (uint64_t)ContainerLayout.SB->BlockMapAddr *
|
|
|
|
ContainerLayout.SB->BlockSize;
|
2016-04-26 01:38:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t PDBFile::getNumStreams() const {
|
|
|
|
return ContainerLayout.StreamSizes.size();
|
|
|
|
}
|
2016-04-26 01:38:08 +08:00
|
|
|
|
|
|
|
uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ContainerLayout.StreamSizes[StreamIndex];
|
2016-04-26 01:38:08 +08:00
|
|
|
}
|
|
|
|
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 13:28:55 +08:00
|
|
|
ArrayRef<support::ulittle32_t>
|
2016-04-26 01:38:08 +08:00
|
|
|
PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ContainerLayout.StreamMap[StreamIndex];
|
2016-04-26 01:38:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-10 13:32:05 +08:00
|
|
|
uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); }
|
2016-06-15 04:48:36 +08:00
|
|
|
|
2016-07-10 11:34:47 +08:00
|
|
|
Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
|
|
|
|
uint32_t NumBytes) const {
|
2016-07-16 06:16:56 +08:00
|
|
|
uint64_t StreamBlockOffset = msf::blockToOffset(BlockIndex, getBlockSize());
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-06-10 13:10:19 +08:00
|
|
|
ArrayRef<uint8_t> Result;
|
|
|
|
if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
|
2016-07-10 11:34:47 +08:00
|
|
|
return std::move(EC);
|
2016-06-10 13:10:19 +08:00
|
|
|
return Result;
|
2016-04-26 01:38:08 +08:00
|
|
|
}
|
|
|
|
|
2016-06-10 13:09:12 +08:00
|
|
|
Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
|
|
|
|
ArrayRef<uint8_t> Data) const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return make_error<RawError>(raw_error_code::not_writable,
|
|
|
|
"PDBFile is immutable");
|
2016-06-10 13:09:12 +08:00
|
|
|
}
|
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
Error PDBFile::parseFileHeaders() {
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamReader Reader(*Buffer);
|
2016-05-25 11:53:16 +08:00
|
|
|
|
2016-07-30 05:38:00 +08:00
|
|
|
// Initialize SB.
|
2016-07-23 03:56:33 +08:00
|
|
|
const msf::SuperBlock *SB = nullptr;
|
2016-06-10 13:10:19 +08:00
|
|
|
if (auto EC = Reader.readObject(SB)) {
|
|
|
|
consumeError(std::move(EC));
|
2016-05-07 04:51:57 +08:00
|
|
|
return make_error<RawError>(raw_error_code::corrupt_file,
|
|
|
|
"Does not contain superblock");
|
2016-06-10 13:10:19 +08:00
|
|
|
}
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-23 03:56:33 +08:00
|
|
|
if (auto EC = msf::validateSuperBlock(*SB))
|
2016-07-01 01:43:00 +08:00
|
|
|
return EC;
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-23 03:56:33 +08:00
|
|
|
if (Buffer->getLength() % SB->BlockSize != 0)
|
|
|
|
return make_error<RawError>(raw_error_code::corrupt_file,
|
|
|
|
"File size is not a multiple of block size");
|
2016-07-29 03:12:28 +08:00
|
|
|
ContainerLayout.SB = SB;
|
2016-07-23 03:56:33 +08:00
|
|
|
|
2016-07-30 05:38:00 +08:00
|
|
|
// Initialize Free Page Map.
|
2016-08-02 05:19:45 +08:00
|
|
|
ContainerLayout.FreePageMap.resize(SB->NumBlocks);
|
|
|
|
// The Fpm exists either at block 1 or block 2 of the MSF. However, this
|
|
|
|
// allows for a maximum of getBlockSize() * 8 blocks bits in the Fpm, and
|
|
|
|
// thusly an equal number of total blocks in the file. For a block size
|
|
|
|
// of 4KiB (very common), this would yield 32KiB total blocks in file, for a
|
|
|
|
// maximum file size of 32KiB * 4KiB = 128MiB. Obviously this won't do, so
|
|
|
|
// the Fpm is split across the file at `getBlockSize()` intervals. As a
|
|
|
|
// result, every block whose index is of the form |{1,2} + getBlockSize() * k|
|
|
|
|
// for any non-negative integer k is an Fpm block. In theory, we only really
|
|
|
|
// need to reserve blocks of the form |{1,2} + getBlockSize() * 8 * k|, but
|
|
|
|
// current versions of the MSF format already expect the Fpm to be arranged
|
|
|
|
// at getBlockSize() intervals, so we have to be compatible.
|
|
|
|
// See the function fpmPn() for more information:
|
|
|
|
// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L489
|
2016-08-04 00:53:21 +08:00
|
|
|
auto FpmStream = MappedBlockStream::createFpmStream(ContainerLayout, *Buffer);
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamReader FpmReader(*FpmStream);
|
2016-08-04 00:53:21 +08:00
|
|
|
ArrayRef<uint8_t> FpmBytes;
|
|
|
|
if (auto EC = FpmReader.readBytes(FpmBytes,
|
|
|
|
msf::getFullFpmByteSize(ContainerLayout)))
|
|
|
|
return EC;
|
2016-08-02 05:19:45 +08:00
|
|
|
uint32_t BlocksRemaining = getBlockCount();
|
2016-08-04 00:53:21 +08:00
|
|
|
uint32_t BI = 0;
|
|
|
|
for (auto Byte : FpmBytes) {
|
|
|
|
uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U);
|
|
|
|
for (uint32_t I = 0; I < BlocksThisByte; ++I) {
|
|
|
|
if (Byte & (1 << I))
|
2016-08-02 05:19:45 +08:00
|
|
|
ContainerLayout.FreePageMap[BI] = true;
|
2016-08-04 00:53:21 +08:00
|
|
|
--BlocksRemaining;
|
|
|
|
++BI;
|
2016-08-02 05:19:45 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-30 05:38:00 +08:00
|
|
|
|
2016-07-01 01:43:00 +08:00
|
|
|
Reader.setOffset(getBlockMapOffset());
|
2016-07-29 03:12:28 +08:00
|
|
|
if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks,
|
|
|
|
getNumDirectoryBlocks()))
|
2016-07-01 01:43:00 +08:00
|
|
|
return EC;
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
return Error::success();
|
2016-04-26 01:38:08 +08:00
|
|
|
}
|
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
Error PDBFile::parseStreamData() {
|
2016-07-29 03:12:28 +08:00
|
|
|
assert(ContainerLayout.SB);
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 13:28:55 +08:00
|
|
|
if (DirectoryStream)
|
|
|
|
return Error::success();
|
2016-04-26 01:38:08 +08:00
|
|
|
|
|
|
|
uint32_t NumStreams = 0;
|
|
|
|
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 13:28:55 +08:00
|
|
|
// Normally you can't use a MappedBlockStream without having fully parsed the
|
|
|
|
// PDB file, because it accesses the directory and various other things, which
|
|
|
|
// is exactly what we are attempting to parse. By specifying a custom
|
|
|
|
// subclass of IPDBStreamData which only accesses the fields that have already
|
|
|
|
// been parsed, we can avoid this and reuse MappedBlockStream.
|
2016-07-29 03:12:28 +08:00
|
|
|
auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer);
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamReader Reader(*DS);
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(NumStreams))
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 13:28:55 +08:00
|
|
|
return EC;
|
2016-05-28 13:59:19 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 13:28:55 +08:00
|
|
|
return EC;
|
|
|
|
for (uint32_t I = 0; I < NumStreams; ++I) {
|
2016-06-23 06:42:24 +08:00
|
|
|
uint32_t StreamSize = getStreamByteSize(I);
|
|
|
|
// FIXME: What does StreamSize ~0U mean?
|
2016-05-28 00:16:48 +08:00
|
|
|
uint64_t NumExpectedStreamBlocks =
|
2016-07-23 03:56:33 +08:00
|
|
|
StreamSize == UINT32_MAX
|
|
|
|
? 0
|
2016-07-29 03:12:28 +08:00
|
|
|
: msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
|
2016-06-10 13:10:19 +08:00
|
|
|
|
|
|
|
// For convenience, we store the block array contiguously. This is because
|
|
|
|
// if someone calls setStreamMap(), it is more convenient to be able to call
|
|
|
|
// it with an ArrayRef instead of setting up a StreamRef. Since the
|
|
|
|
// DirectoryStream is cached in the class and thus lives for the life of the
|
|
|
|
// class, we can be guaranteed that readArray() will return a stable
|
|
|
|
// reference, even if it has to allocate from its internal pool.
|
|
|
|
ArrayRef<support::ulittle32_t> Blocks;
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 13:28:55 +08:00
|
|
|
if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
|
|
|
|
return EC;
|
2016-07-10 13:32:05 +08:00
|
|
|
for (uint32_t Block : Blocks) {
|
2016-07-29 03:12:28 +08:00
|
|
|
uint64_t BlockEndOffset =
|
|
|
|
(uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
|
2016-07-10 13:32:05 +08:00
|
|
|
if (BlockEndOffset > getFileSize())
|
|
|
|
return make_error<RawError>(raw_error_code::corrupt_file,
|
|
|
|
"Stream block map is corrupt.");
|
|
|
|
}
|
2016-07-29 03:12:28 +08:00
|
|
|
ContainerLayout.StreamMap.push_back(Blocks);
|
2016-05-28 00:16:48 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 01:38:08 +08:00
|
|
|
// We should have read exactly SB->NumDirectoryBytes bytes.
|
[pdb] Use MappedBlockStream to parse the PDB directory.
In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.
Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.
To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).
This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.
Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046
llvm-svn: 271982
2016-06-07 13:28:55 +08:00
|
|
|
assert(Reader.bytesRemaining() == 0);
|
2016-07-29 03:12:28 +08:00
|
|
|
DirectoryStream = std::move(DS);
|
2016-05-07 04:51:57 +08:00
|
|
|
return Error::success();
|
2016-04-26 01:38:08 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 07:16:32 +08:00
|
|
|
ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ContainerLayout.DirectoryBlocks;
|
2016-04-26 01:38:08 +08:00
|
|
|
}
|
2016-04-27 02:42:34 +08:00
|
|
|
|
2016-10-22 03:43:19 +08:00
|
|
|
Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
|
|
|
|
if (!Globals) {
|
|
|
|
auto DbiS = getPDBDbiStream();
|
|
|
|
if (!DbiS)
|
|
|
|
return DbiS.takeError();
|
|
|
|
|
2016-12-06 06:44:00 +08:00
|
|
|
auto GlobalS = safelyCreateIndexedStream(
|
2016-10-22 03:43:19 +08:00
|
|
|
ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!GlobalS)
|
|
|
|
return GlobalS.takeError();
|
2016-12-06 06:44:00 +08:00
|
|
|
auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
|
2016-10-22 03:43:19 +08:00
|
|
|
if (auto EC = TempGlobals->reload())
|
|
|
|
return std::move(EC);
|
|
|
|
Globals = std::move(TempGlobals);
|
|
|
|
}
|
|
|
|
return *Globals;
|
|
|
|
}
|
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
Expected<InfoStream &> PDBFile::getPDBInfoStream() {
|
2016-04-30 01:28:47 +08:00
|
|
|
if (!Info) {
|
2016-12-06 06:44:00 +08:00
|
|
|
auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!InfoS)
|
|
|
|
return InfoS.takeError();
|
2016-12-06 06:44:00 +08:00
|
|
|
auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
|
2016-06-09 01:26:39 +08:00
|
|
|
if (auto EC = TempInfo->reload())
|
2016-05-07 04:51:57 +08:00
|
|
|
return std::move(EC);
|
2016-06-09 01:26:39 +08:00
|
|
|
Info = std::move(TempInfo);
|
2016-04-27 02:42:34 +08:00
|
|
|
}
|
2016-04-30 01:28:47 +08:00
|
|
|
return *Info;
|
2016-04-27 02:42:34 +08:00
|
|
|
}
|
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
Expected<DbiStream &> PDBFile::getPDBDbiStream() {
|
2016-04-30 01:28:47 +08:00
|
|
|
if (!Dbi) {
|
2016-12-06 06:44:00 +08:00
|
|
|
auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!DbiS)
|
|
|
|
return DbiS.takeError();
|
2016-12-06 06:44:00 +08:00
|
|
|
auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
|
2016-06-09 01:26:39 +08:00
|
|
|
if (auto EC = TempDbi->reload())
|
2016-05-07 04:51:57 +08:00
|
|
|
return std::move(EC);
|
2016-06-09 01:26:39 +08:00
|
|
|
Dbi = std::move(TempDbi);
|
2016-04-27 02:42:34 +08:00
|
|
|
}
|
2016-04-30 01:28:47 +08:00
|
|
|
return *Dbi;
|
2016-04-27 02:42:34 +08:00
|
|
|
}
|
2016-05-03 08:28:21 +08:00
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
Expected<TpiStream &> PDBFile::getPDBTpiStream() {
|
2016-05-03 08:28:21 +08:00
|
|
|
if (!Tpi) {
|
2016-12-06 06:44:00 +08:00
|
|
|
auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!TpiS)
|
|
|
|
return TpiS.takeError();
|
2016-12-06 06:44:00 +08:00
|
|
|
auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
|
2016-06-09 01:26:39 +08:00
|
|
|
if (auto EC = TempTpi->reload())
|
2016-05-07 04:51:57 +08:00
|
|
|
return std::move(EC);
|
2016-06-09 01:26:39 +08:00
|
|
|
Tpi = std::move(TempTpi);
|
2016-05-03 08:28:21 +08:00
|
|
|
}
|
|
|
|
return *Tpi;
|
|
|
|
}
|
2016-05-14 05:21:53 +08:00
|
|
|
|
2016-05-25 12:35:22 +08:00
|
|
|
Expected<TpiStream &> PDBFile::getPDBIpiStream() {
|
|
|
|
if (!Ipi) {
|
2016-12-06 06:44:00 +08:00
|
|
|
auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!IpiS)
|
|
|
|
return IpiS.takeError();
|
2016-12-06 06:44:00 +08:00
|
|
|
auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
|
2016-06-09 01:26:39 +08:00
|
|
|
if (auto EC = TempIpi->reload())
|
2016-05-25 12:35:22 +08:00
|
|
|
return std::move(EC);
|
2016-06-09 01:26:39 +08:00
|
|
|
Ipi = std::move(TempIpi);
|
2016-05-25 12:35:22 +08:00
|
|
|
}
|
|
|
|
return *Ipi;
|
|
|
|
}
|
|
|
|
|
2016-05-14 05:21:53 +08:00
|
|
|
Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
|
|
|
|
if (!Publics) {
|
|
|
|
auto DbiS = getPDBDbiStream();
|
2016-06-09 01:26:39 +08:00
|
|
|
if (!DbiS)
|
|
|
|
return DbiS.takeError();
|
|
|
|
|
2016-12-06 06:44:00 +08:00
|
|
|
auto PublicS = safelyCreateIndexedStream(
|
|
|
|
ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!PublicS)
|
|
|
|
return PublicS.takeError();
|
2016-06-09 01:26:39 +08:00
|
|
|
auto TempPublics =
|
2016-12-06 06:44:00 +08:00
|
|
|
llvm::make_unique<PublicsStream>(*this, std::move(*PublicS));
|
2016-06-09 01:26:39 +08:00
|
|
|
if (auto EC = TempPublics->reload())
|
2016-05-14 05:21:53 +08:00
|
|
|
return std::move(EC);
|
2016-06-09 01:26:39 +08:00
|
|
|
Publics = std::move(TempPublics);
|
2016-05-14 05:21:53 +08:00
|
|
|
}
|
|
|
|
return *Publics;
|
|
|
|
}
|
2016-05-21 03:55:17 +08:00
|
|
|
|
|
|
|
Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
|
|
|
|
if (!Symbols) {
|
|
|
|
auto DbiS = getPDBDbiStream();
|
2016-06-09 01:26:39 +08:00
|
|
|
if (!DbiS)
|
|
|
|
return DbiS.takeError();
|
|
|
|
|
2016-05-21 03:55:17 +08:00
|
|
|
uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
|
2016-12-06 06:44:00 +08:00
|
|
|
auto SymbolS =
|
|
|
|
safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!SymbolS)
|
|
|
|
return SymbolS.takeError();
|
2016-05-21 03:55:17 +08:00
|
|
|
|
2016-12-06 06:44:00 +08:00
|
|
|
auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
|
2016-06-09 01:26:39 +08:00
|
|
|
if (auto EC = TempSymbols->reload())
|
2016-05-21 03:55:17 +08:00
|
|
|
return std::move(EC);
|
2016-06-09 01:26:39 +08:00
|
|
|
Symbols = std::move(TempSymbols);
|
2016-05-21 03:55:17 +08:00
|
|
|
}
|
|
|
|
return *Symbols;
|
|
|
|
}
|
2016-06-03 13:52:57 +08:00
|
|
|
|
2017-05-03 02:00:13 +08:00
|
|
|
Expected<PDBStringTable &> PDBFile::getStringTable() {
|
|
|
|
if (!Strings || !PDBStringTableStream) {
|
2016-06-09 01:26:39 +08:00
|
|
|
auto IS = getPDBInfoStream();
|
|
|
|
if (!IS)
|
|
|
|
return IS.takeError();
|
|
|
|
|
|
|
|
uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
|
2016-06-03 13:52:57 +08:00
|
|
|
|
2016-12-06 06:44:00 +08:00
|
|
|
auto NS =
|
|
|
|
safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!NS)
|
|
|
|
return NS.takeError();
|
2016-06-08 08:25:08 +08:00
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamReader Reader(**NS);
|
2017-05-03 02:00:13 +08:00
|
|
|
auto N = llvm::make_unique<PDBStringTable>();
|
2016-06-03 13:52:57 +08:00
|
|
|
if (auto EC = N->load(Reader))
|
|
|
|
return std::move(EC);
|
2017-01-21 06:41:15 +08:00
|
|
|
Strings = std::move(N);
|
2017-05-03 02:00:13 +08:00
|
|
|
PDBStringTableStream = std::move(*NS);
|
2016-06-03 13:52:57 +08:00
|
|
|
}
|
2017-01-21 06:41:15 +08:00
|
|
|
return *Strings;
|
2016-06-03 13:52:57 +08:00
|
|
|
}
|
2016-12-06 06:44:00 +08:00
|
|
|
|
|
|
|
bool PDBFile::hasPDBDbiStream() const { return StreamDBI < getNumStreams(); }
|
|
|
|
|
|
|
|
bool PDBFile::hasPDBGlobalsStream() {
|
|
|
|
auto DbiS = getPDBDbiStream();
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!DbiS)
|
|
|
|
return false;
|
2016-12-06 06:44:00 +08:00
|
|
|
return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PDBFile::hasPDBInfoStream() { return StreamPDB < getNumStreams(); }
|
|
|
|
|
|
|
|
bool PDBFile::hasPDBIpiStream() const { return StreamIPI < getNumStreams(); }
|
|
|
|
|
|
|
|
bool PDBFile::hasPDBPublicsStream() {
|
|
|
|
auto DbiS = getPDBDbiStream();
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!DbiS)
|
|
|
|
return false;
|
2016-12-06 06:44:00 +08:00
|
|
|
return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PDBFile::hasPDBSymbolStream() {
|
|
|
|
auto DbiS = getPDBDbiStream();
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!DbiS)
|
|
|
|
return false;
|
2016-12-06 06:44:00 +08:00
|
|
|
return DbiS->getSymRecordStreamIndex() < getNumStreams();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
|
|
|
|
|
2017-05-03 02:00:13 +08:00
|
|
|
bool PDBFile::hasPDBStringTable() {
|
2016-12-06 06:44:00 +08:00
|
|
|
auto IS = getPDBInfoStream();
|
2017-01-26 06:38:55 +08:00
|
|
|
if (!IS)
|
|
|
|
return false;
|
2016-12-06 06:44:00 +08:00
|
|
|
return IS->getNamedStreamIndex("/names") < getNumStreams();
|
|
|
|
}
|
|
|
|
|
2017-03-01 01:49:34 +08:00
|
|
|
/// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
|
|
|
|
/// stream with that index actually exists. If it does not, the return value
|
|
|
|
/// will have an MSFError with code msf_error_code::no_stream. Else, the return
|
|
|
|
/// value will contain the stream returned by createIndexedStream().
|
2017-01-26 06:38:55 +08:00
|
|
|
Expected<std::unique_ptr<MappedBlockStream>>
|
|
|
|
PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamRef MsfData,
|
2017-01-26 06:38:55 +08:00
|
|
|
uint32_t StreamIndex) const {
|
2016-12-06 06:44:00 +08:00
|
|
|
if (StreamIndex >= getNumStreams())
|
|
|
|
return make_error<RawError>(raw_error_code::no_stream);
|
|
|
|
return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex);
|
|
|
|
}
|