2016-05-13 01:45:44 +08:00
|
|
|
//===-- RecordSerialization.cpp -------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Utilities for serializing and deserializing CodeView records.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
|
|
|
|
#include "llvm/ADT/APInt.h"
|
|
|
|
#include "llvm/ADT/APSInt.h"
|
2016-08-05 03:39:55 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/CodeViewError.h"
|
2016-05-13 01:45:44 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeRecord.h"
|
2017-03-03 04:52:51 +08:00
|
|
|
#include "llvm/Support/BinaryByteStream.h"
|
2016-05-13 01:45:44 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::codeview;
|
|
|
|
using namespace llvm::support;
|
|
|
|
|
|
|
|
/// Reinterpret a byte array as an array of characters. Does not interpret as
|
|
|
|
/// a C string, as StringRef has several helpers (split) that make that easy.
|
|
|
|
StringRef llvm::codeview::getBytesAsCharacters(ArrayRef<uint8_t> LeafData) {
|
|
|
|
return StringRef(reinterpret_cast<const char *>(LeafData.data()),
|
|
|
|
LeafData.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef llvm::codeview::getBytesAsCString(ArrayRef<uint8_t> LeafData) {
|
|
|
|
return getBytesAsCharacters(LeafData).split('\0').first;
|
|
|
|
}
|
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
Error llvm::codeview::consume(BinaryStreamReader &Reader, APSInt &Num) {
|
2016-05-13 01:45:44 +08:00
|
|
|
// Used to avoid overload ambiguity on APInt construtor.
|
|
|
|
bool FalseVal = false;
|
2016-10-21 02:31:19 +08:00
|
|
|
uint16_t Short;
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(Short))
|
2016-10-21 02:31:19 +08:00
|
|
|
return EC;
|
|
|
|
|
2016-05-13 01:45:44 +08:00
|
|
|
if (Short < LF_NUMERIC) {
|
|
|
|
Num = APSInt(APInt(/*numBits=*/16, Short, /*isSigned=*/false),
|
|
|
|
/*isUnsigned=*/true);
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-05-13 01:45:44 +08:00
|
|
|
}
|
2016-10-21 02:31:19 +08:00
|
|
|
|
2016-05-13 01:45:44 +08:00
|
|
|
switch (Short) {
|
2016-10-21 02:31:19 +08:00
|
|
|
case LF_CHAR: {
|
|
|
|
int8_t N;
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-21 02:31:19 +08:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(8, N, true), false);
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-10-21 02:31:19 +08:00
|
|
|
}
|
|
|
|
case LF_SHORT: {
|
|
|
|
int16_t N;
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-21 02:31:19 +08:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(16, N, true), false);
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-10-21 02:31:19 +08:00
|
|
|
}
|
|
|
|
case LF_USHORT: {
|
|
|
|
uint16_t N;
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-21 02:31:19 +08:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(16, N, false), true);
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-10-21 02:31:19 +08:00
|
|
|
}
|
|
|
|
case LF_LONG: {
|
|
|
|
int32_t N;
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-21 02:31:19 +08:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(32, N, true), false);
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-10-21 02:31:19 +08:00
|
|
|
}
|
|
|
|
case LF_ULONG: {
|
|
|
|
uint32_t N;
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-21 02:31:19 +08:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(32, N, FalseVal), true);
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-10-21 02:31:19 +08:00
|
|
|
}
|
|
|
|
case LF_QUADWORD: {
|
|
|
|
int64_t N;
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-21 02:31:19 +08:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(64, N, true), false);
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-10-21 02:31:19 +08:00
|
|
|
}
|
|
|
|
case LF_UQUADWORD: {
|
|
|
|
uint64_t N;
|
2017-02-28 08:04:07 +08:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-21 02:31:19 +08:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(64, N, false), true);
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-05-13 01:45:44 +08:00
|
|
|
}
|
2016-10-21 02:31:19 +08:00
|
|
|
}
|
2016-08-05 03:39:55 +08:00
|
|
|
return make_error<CodeViewError>(cv_error_code::corrupt_record,
|
|
|
|
"Buffer contains invalid APSInt type");
|
2016-05-13 01:45:44 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 03:39:55 +08:00
|
|
|
Error llvm::codeview::consume(StringRef &Data, APSInt &Num) {
|
2016-05-13 01:45:44 +08:00
|
|
|
ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
|
2017-02-28 08:04:07 +08:00
|
|
|
BinaryByteStream S(Bytes, llvm::support::little);
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamReader SR(S);
|
2016-10-21 02:31:19 +08:00
|
|
|
auto EC = consume(SR, Num);
|
|
|
|
Data = Data.take_back(SR.bytesRemaining());
|
2016-05-13 01:45:44 +08:00
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decode a numeric leaf value that is known to be a uint64_t.
|
2017-02-28 06:11:43 +08:00
|
|
|
Error llvm::codeview::consume_numeric(BinaryStreamReader &Reader,
|
2016-10-21 02:31:19 +08:00
|
|
|
uint64_t &Num) {
|
2016-05-13 01:45:44 +08:00
|
|
|
APSInt N;
|
2016-10-21 02:31:19 +08:00
|
|
|
if (auto EC = consume(Reader, N))
|
2016-05-13 01:45:44 +08:00
|
|
|
return EC;
|
|
|
|
if (N.isSigned() || !N.isIntN(64))
|
2016-08-05 03:39:55 +08:00
|
|
|
return make_error<CodeViewError>(cv_error_code::corrupt_record,
|
|
|
|
"Data is not a numeric value!");
|
2016-05-13 01:45:44 +08:00
|
|
|
Num = N.getLimitedValue();
|
2016-08-05 03:39:55 +08:00
|
|
|
return Error::success();
|
2016-05-13 01:45:44 +08:00
|
|
|
}
|
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
Error llvm::codeview::consume(BinaryStreamReader &Reader, uint32_t &Item) {
|
2017-02-28 08:04:07 +08:00
|
|
|
return Reader.readInteger(Item);
|
2016-05-13 01:45:44 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 03:39:55 +08:00
|
|
|
Error llvm::codeview::consume(StringRef &Data, uint32_t &Item) {
|
2016-05-13 01:45:44 +08:00
|
|
|
ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
|
2017-02-28 08:04:07 +08:00
|
|
|
BinaryByteStream S(Bytes, llvm::support::little);
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamReader SR(S);
|
2016-10-21 02:31:19 +08:00
|
|
|
auto EC = consume(SR, Item);
|
|
|
|
Data = Data.take_back(SR.bytesRemaining());
|
2016-05-13 01:45:44 +08:00
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
Error llvm::codeview::consume(BinaryStreamReader &Reader, int32_t &Item) {
|
2017-02-28 08:04:07 +08:00
|
|
|
return Reader.readInteger(Item);
|
2016-05-13 01:45:44 +08:00
|
|
|
}
|
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
Error llvm::codeview::consume(BinaryStreamReader &Reader, StringRef &Item) {
|
2016-10-21 02:31:19 +08:00
|
|
|
if (Reader.empty())
|
2016-08-05 03:39:55 +08:00
|
|
|
return make_error<CodeViewError>(cv_error_code::corrupt_record,
|
|
|
|
"Null terminated string buffer is empty!");
|
2016-05-13 01:45:44 +08:00
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
return Reader.readCString(Item);
|
2016-05-13 01:45:44 +08:00
|
|
|
}
|