2016-05-12 19:45:44 +02:00
|
|
|
//===-- RecordSerialization.cpp -------------------------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-05-12 19:45:44 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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-04 21:39:55 +02:00
|
|
|
#include "llvm/DebugInfo/CodeView/CodeViewError.h"
|
2017-07-26 02:40:36 +02:00
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
|
2016-05-12 19:45:44 +02:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeRecord.h"
|
2017-03-02 21:52:51 +01:00
|
|
|
#include "llvm/Support/BinaryByteStream.h"
|
2016-05-12 19:45:44 +02: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-27 23:11:43 +01:00
|
|
|
Error llvm::codeview::consume(BinaryStreamReader &Reader, APSInt &Num) {
|
2016-05-12 19:45:44 +02:00
|
|
|
// Used to avoid overload ambiguity on APInt construtor.
|
|
|
|
bool FalseVal = false;
|
2016-10-20 20:31:19 +02:00
|
|
|
uint16_t Short;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = Reader.readInteger(Short))
|
2016-10-20 20:31:19 +02:00
|
|
|
return EC;
|
|
|
|
|
2016-05-12 19:45:44 +02:00
|
|
|
if (Short < LF_NUMERIC) {
|
|
|
|
Num = APSInt(APInt(/*numBits=*/16, Short, /*isSigned=*/false),
|
|
|
|
/*isUnsigned=*/true);
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-05-12 19:45:44 +02:00
|
|
|
}
|
2016-10-20 20:31:19 +02:00
|
|
|
|
2016-05-12 19:45:44 +02:00
|
|
|
switch (Short) {
|
2016-10-20 20:31:19 +02:00
|
|
|
case LF_CHAR: {
|
|
|
|
int8_t N;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-20 20:31:19 +02:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(8, N, true), false);
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-10-20 20:31:19 +02:00
|
|
|
}
|
|
|
|
case LF_SHORT: {
|
|
|
|
int16_t N;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-20 20:31:19 +02:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(16, N, true), false);
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-10-20 20:31:19 +02:00
|
|
|
}
|
|
|
|
case LF_USHORT: {
|
|
|
|
uint16_t N;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-20 20:31:19 +02:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(16, N, false), true);
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-10-20 20:31:19 +02:00
|
|
|
}
|
|
|
|
case LF_LONG: {
|
|
|
|
int32_t N;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-20 20:31:19 +02:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(32, N, true), false);
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-10-20 20:31:19 +02:00
|
|
|
}
|
|
|
|
case LF_ULONG: {
|
|
|
|
uint32_t N;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-20 20:31:19 +02:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(32, N, FalseVal), true);
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-10-20 20:31:19 +02:00
|
|
|
}
|
|
|
|
case LF_QUADWORD: {
|
|
|
|
int64_t N;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-20 20:31:19 +02:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(64, N, true), false);
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-10-20 20:31:19 +02:00
|
|
|
}
|
|
|
|
case LF_UQUADWORD: {
|
|
|
|
uint64_t N;
|
2017-02-28 01:04:07 +01:00
|
|
|
if (auto EC = Reader.readInteger(N))
|
2016-10-20 20:31:19 +02:00
|
|
|
return EC;
|
|
|
|
Num = APSInt(APInt(64, N, false), true);
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-05-12 19:45:44 +02:00
|
|
|
}
|
2016-10-20 20:31:19 +02:00
|
|
|
}
|
2016-08-04 21:39:55 +02:00
|
|
|
return make_error<CodeViewError>(cv_error_code::corrupt_record,
|
|
|
|
"Buffer contains invalid APSInt type");
|
2016-05-12 19:45:44 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 21:39:55 +02:00
|
|
|
Error llvm::codeview::consume(StringRef &Data, APSInt &Num) {
|
2016-05-12 19:45:44 +02:00
|
|
|
ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
|
2017-02-28 01:04:07 +01:00
|
|
|
BinaryByteStream S(Bytes, llvm::support::little);
|
2017-02-27 23:11:43 +01:00
|
|
|
BinaryStreamReader SR(S);
|
2016-10-20 20:31:19 +02:00
|
|
|
auto EC = consume(SR, Num);
|
|
|
|
Data = Data.take_back(SR.bytesRemaining());
|
2016-05-12 19:45:44 +02:00
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decode a numeric leaf value that is known to be a uint64_t.
|
2017-02-27 23:11:43 +01:00
|
|
|
Error llvm::codeview::consume_numeric(BinaryStreamReader &Reader,
|
2016-10-20 20:31:19 +02:00
|
|
|
uint64_t &Num) {
|
2016-05-12 19:45:44 +02:00
|
|
|
APSInt N;
|
2016-10-20 20:31:19 +02:00
|
|
|
if (auto EC = consume(Reader, N))
|
2016-05-12 19:45:44 +02:00
|
|
|
return EC;
|
|
|
|
if (N.isSigned() || !N.isIntN(64))
|
2016-08-04 21:39:55 +02:00
|
|
|
return make_error<CodeViewError>(cv_error_code::corrupt_record,
|
|
|
|
"Data is not a numeric value!");
|
2016-05-12 19:45:44 +02:00
|
|
|
Num = N.getLimitedValue();
|
2016-08-04 21:39:55 +02:00
|
|
|
return Error::success();
|
2016-05-12 19:45:44 +02:00
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
Error llvm::codeview::consume(BinaryStreamReader &Reader, uint32_t &Item) {
|
2017-02-28 01:04:07 +01:00
|
|
|
return Reader.readInteger(Item);
|
2016-05-12 19:45:44 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 21:39:55 +02:00
|
|
|
Error llvm::codeview::consume(StringRef &Data, uint32_t &Item) {
|
2016-05-12 19:45:44 +02:00
|
|
|
ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
|
2017-02-28 01:04:07 +01:00
|
|
|
BinaryByteStream S(Bytes, llvm::support::little);
|
2017-02-27 23:11:43 +01:00
|
|
|
BinaryStreamReader SR(S);
|
2016-10-20 20:31:19 +02:00
|
|
|
auto EC = consume(SR, Item);
|
|
|
|
Data = Data.take_back(SR.bytesRemaining());
|
2016-05-12 19:45:44 +02:00
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
Error llvm::codeview::consume(BinaryStreamReader &Reader, int32_t &Item) {
|
2017-02-28 01:04:07 +01:00
|
|
|
return Reader.readInteger(Item);
|
2016-05-12 19:45:44 +02:00
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
Error llvm::codeview::consume(BinaryStreamReader &Reader, StringRef &Item) {
|
2016-10-20 20:31:19 +02:00
|
|
|
if (Reader.empty())
|
2016-08-04 21:39:55 +02:00
|
|
|
return make_error<CodeViewError>(cv_error_code::corrupt_record,
|
|
|
|
"Null terminated string buffer is empty!");
|
2016-05-12 19:45:44 +02:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
return Reader.readCString(Item);
|
2016-05-12 19:45:44 +02:00
|
|
|
}
|
2017-07-26 02:40:36 +02:00
|
|
|
|
|
|
|
Expected<CVSymbol> llvm::codeview::readSymbolFromStream(BinaryStreamRef Stream,
|
|
|
|
uint32_t Offset) {
|
|
|
|
return readCVRecordFromStream<SymbolKind>(Stream, Offset);
|
|
|
|
}
|