LLVM  13.0.0git
RecordSerialization.cpp
Go to the documentation of this file.
1 //===-- RecordSerialization.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utilities for serializing and deserializing CodeView records.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/APSInt.h"
20 
21 using namespace llvm;
22 using namespace llvm::codeview;
23 using namespace llvm::support;
24 
25 /// Reinterpret a byte array as an array of characters. Does not interpret as
26 /// a C string, as StringRef has several helpers (split) that make that easy.
28  return StringRef(reinterpret_cast<const char *>(LeafData.data()),
29  LeafData.size());
30 }
31 
33  return getBytesAsCharacters(LeafData).split('\0').first;
34 }
35 
37  // Used to avoid overload ambiguity on APInt constructor.
38  bool FalseVal = false;
39  uint16_t Short;
40  if (auto EC = Reader.readInteger(Short))
41  return EC;
42 
43  if (Short < LF_NUMERIC) {
44  Num = APSInt(APInt(/*numBits=*/16, Short, /*isSigned=*/false),
45  /*isUnsigned=*/true);
46  return Error::success();
47  }
48 
49  switch (Short) {
50  case LF_CHAR: {
51  int8_t N;
52  if (auto EC = Reader.readInteger(N))
53  return EC;
54  Num = APSInt(APInt(8, N, true), false);
55  return Error::success();
56  }
57  case LF_SHORT: {
58  int16_t N;
59  if (auto EC = Reader.readInteger(N))
60  return EC;
61  Num = APSInt(APInt(16, N, true), false);
62  return Error::success();
63  }
64  case LF_USHORT: {
65  uint16_t N;
66  if (auto EC = Reader.readInteger(N))
67  return EC;
68  Num = APSInt(APInt(16, N, false), true);
69  return Error::success();
70  }
71  case LF_LONG: {
72  int32_t N;
73  if (auto EC = Reader.readInteger(N))
74  return EC;
75  Num = APSInt(APInt(32, N, true), false);
76  return Error::success();
77  }
78  case LF_ULONG: {
79  uint32_t N;
80  if (auto EC = Reader.readInteger(N))
81  return EC;
82  Num = APSInt(APInt(32, N, FalseVal), true);
83  return Error::success();
84  }
85  case LF_QUADWORD: {
86  int64_t N;
87  if (auto EC = Reader.readInteger(N))
88  return EC;
89  Num = APSInt(APInt(64, N, true), false);
90  return Error::success();
91  }
92  case LF_UQUADWORD: {
93  uint64_t N;
94  if (auto EC = Reader.readInteger(N))
95  return EC;
96  Num = APSInt(APInt(64, N, false), true);
97  return Error::success();
98  }
99  }
100  return make_error<CodeViewError>(cv_error_code::corrupt_record,
101  "Buffer contains invalid APSInt type");
102 }
103 
105  ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
107  BinaryStreamReader SR(S);
108  auto EC = consume(SR, Num);
109  Data = Data.take_back(SR.bytesRemaining());
110  return EC;
111 }
112 
113 /// Decode a numeric leaf value that is known to be a uint64_t.
115  uint64_t &Num) {
116  APSInt N;
117  if (auto EC = consume(Reader, N))
118  return EC;
119  if (N.isSigned() || !N.isIntN(64))
120  return make_error<CodeViewError>(cv_error_code::corrupt_record,
121  "Data is not a numeric value!");
122  Num = N.getLimitedValue();
123  return Error::success();
124 }
125 
127  return Reader.readInteger(Item);
128 }
129 
131  ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
133  BinaryStreamReader SR(S);
134  auto EC = consume(SR, Item);
135  Data = Data.take_back(SR.bytesRemaining());
136  return EC;
137 }
138 
140  return Reader.readInteger(Item);
141 }
142 
144  if (Reader.empty())
145  return make_error<CodeViewError>(cv_error_code::corrupt_record,
146  "Null terminated string buffer is empty!");
147 
148  return Reader.readCString(Item);
149 }
150 
152  uint32_t Offset) {
153  return readCVRecordFromStream<SymbolKind>(Stream, Offset);
154 }
llvm::BinaryStreamReader::bytesRemaining
uint32_t bytesRemaining() const
Definition: BinaryStreamReader.h:257
llvm::BinaryStreamReader::empty
bool empty() const
Definition: BinaryStreamReader.h:253
llvm
Definition: AllocatorList.h:23
RecordSerialization.h
BinaryByteStream.h
SymbolRecord.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
llvm::lltok::APSInt
@ APSInt
Definition: LLToken.h:491
APInt.h
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::tgtok::FalseVal
@ FalseVal
Definition: TGLexer.h:61
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::ArrayRef::data
const T * data() const
Definition: ArrayRef.h:162
llvm::support::little
@ little
Definition: Endian.h:27
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
llvm::BinaryByteStream
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
Definition: BinaryByteStream.h:31
llvm::BinaryStreamReader::readInteger
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
Definition: BinaryStreamReader.h:75
llvm::APSInt
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:22
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
llvm::codeview::readSymbolFromStream
Expected< CVSymbol > readSymbolFromStream(BinaryStreamRef Stream, uint32_t Offset)
Definition: RecordSerialization.cpp:151
TypeRecord.h
llvm::BinaryStreamReader::readCString
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
Definition: BinaryStreamReader.cpp:74
llvm::codeview::getBytesAsCString
StringRef getBytesAsCString(ArrayRef< uint8_t > LeafData)
Definition: RecordSerialization.cpp:32
llvm::codeview::CompileSym2Flags::EC
@ EC
APSInt.h
llvm::codeview::consume
Error consume(BinaryStreamReader &Reader)
Definition: RecordSerialization.h:46
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
uint16_t
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::support
Definition: Endian.h:25
llvm::codeview::cv_error_code::corrupt_record
@ corrupt_record
llvm::codeview
Definition: AppendingTypeTableBuilder.h:22
llvm::codeview::getBytesAsCharacters
StringRef getBytesAsCharacters(ArrayRef< uint8_t > LeafData)
Reinterpret a byte array as an array of characters.
Definition: RecordSerialization.cpp:27
N
#define N
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
CodeViewError.h
llvm::codeview::consume_numeric
Error consume_numeric(BinaryStreamReader &Reader, uint64_t &Value)
Decodes a numeric leaf value that is known to be a particular type.
Definition: RecordSerialization.cpp:114
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition: BinaryStreamRef.h:156