LLVM  14.0.0git
RecordSerialization.h
Go to the documentation of this file.
1 //===- RecordSerialization.h ------------------------------------*- C++ -*-===//
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 #ifndef LLVM_DEBUGINFO_CODEVIEW_RECORDSERIALIZATION_H
10 #define LLVM_DEBUGINFO_CODEVIEW_RECORDSERIALIZATION_H
11 
12 #include "llvm/ADT/APSInt.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
20 #include <cinttypes>
21 #include <tuple>
22 
23 namespace llvm {
24 namespace codeview {
28 
29 /// Limit on the size of all codeview symbol and type records, including the
30 /// RecordPrefix. MSVC does not emit any records larger than this.
31 enum : unsigned { MaxRecordLength = 0xFF00 };
32 
33 struct RecordPrefix {
34  RecordPrefix() = default;
36 
37  ulittle16_t RecordLen; // Record length, starting from &RecordKind.
38  ulittle16_t RecordKind; // Record kind enum (SymRecordKind or TypeRecordKind)
39 };
40 
41 /// Reinterpret a byte array as an array of characters. Does not interpret as
42 /// a C string, as StringRef has several helpers (split) that make that easy.
45 
46 inline Error consume(BinaryStreamReader &Reader) { return Error::success(); }
47 
48 /// Decodes a numeric "leaf" value. These are integer literals encountered in
49 /// the type stream. If the value is positive and less than LF_NUMERIC (1 <<
50 /// 15), it is emitted directly in Data. Otherwise, it has a tag like LF_CHAR
51 /// that indicates the bitwidth and sign of the numeric data.
52 Error consume(BinaryStreamReader &Reader, APSInt &Num);
53 
54 /// Decodes a numeric leaf value that is known to be a particular type.
56 
57 /// Decodes signed and unsigned fixed-length integers.
59 Error consume(BinaryStreamReader &Reader, int32_t &Item);
60 
61 /// Decodes a null terminated string.
63 
64 Error consume(StringRef &Data, APSInt &Num);
65 Error consume(StringRef &Data, uint32_t &Item);
66 
67 /// Decodes an arbitrary object whose layout matches that of the underlying
68 /// byte sequence, and returns a pointer to the object.
69 template <typename T> Error consume(BinaryStreamReader &Reader, T *&Item) {
70  return Reader.readObject(Item);
71 }
72 
73 template <typename T, typename U> struct serialize_conditional_impl {
75 
77  if (!Func())
78  return Error::success();
79  return consume(Reader, Item);
80  }
81 
82  T &Item;
83  U Func;
84 };
85 
86 template <typename T, typename U>
88  return serialize_conditional_impl<T, U>(Item, Func);
89 }
90 
91 template <typename T, typename U> struct serialize_array_impl {
93 
95  return Reader.readArray(Item, Func());
96  }
97 
99  U Func;
100 };
101 
102 template <typename T> struct serialize_vector_tail_impl {
103  serialize_vector_tail_impl(std::vector<T> &Item) : Item(Item) {}
104 
106  T Field;
107  // Stop when we run out of bytes or we hit record padding bytes.
108  while (!Reader.empty() && Reader.peek() < LF_PAD0) {
109  if (auto EC = consume(Reader, Field))
110  return EC;
111  Item.push_back(Field);
112  }
113  return Error::success();
114  }
115 
116  std::vector<T> &Item;
117 };
118 
121  : Item(Item) {}
122 
124  if (Reader.empty())
125  return make_error<CodeViewError>(cv_error_code::insufficient_buffer,
126  "Null terminated string is empty!");
127 
128  while (Reader.peek() != 0) {
130  if (auto EC = Reader.readCString(Field))
131  return EC;
132  Item.push_back(Field);
133  }
134  return Reader.skip(1);
135  }
136 
137  std::vector<StringRef> &Item;
138 };
139 
140 template <typename T> struct serialize_arrayref_tail_impl {
142 
144  uint32_t Count = Reader.bytesRemaining() / sizeof(T);
145  return Reader.readArray(Item, Count);
146  }
147 
149 };
150 
151 template <typename T> struct serialize_numeric_impl {
153 
155  return consume_numeric(Reader, Item);
156  }
157 
158  T &Item;
159 };
160 
161 template <typename T, typename U>
163  return serialize_array_impl<T, U>(Item, Func);
164 }
165 
166 inline serialize_null_term_string_array_impl
167 serialize_null_term_string_array(std::vector<StringRef> &Item) {
169 }
170 
171 template <typename T>
173  return serialize_vector_tail_impl<T>(Item);
174 }
175 
176 template <typename T>
178  return serialize_arrayref_tail_impl<T>(Item);
179 }
180 
181 template <typename T> serialize_numeric_impl<T> serialize_numeric(T &Item) {
182  return serialize_numeric_impl<T>(Item);
183 }
184 
185 template <typename T, typename U>
187  const serialize_conditional_impl<T, U> &Item) {
188  return Item.deserialize(Reader);
189 }
190 
191 template <typename T, typename U>
193  const serialize_array_impl<T, U> &Item) {
194  return Item.deserialize(Reader);
195 }
196 
199  return Item.deserialize(Reader);
200 }
201 
202 template <typename T>
204  const serialize_vector_tail_impl<T> &Item) {
205  return Item.deserialize(Reader);
206 }
207 
208 template <typename T>
210  const serialize_arrayref_tail_impl<T> &Item) {
211  return Item.deserialize(Reader);
212 }
213 
214 template <typename T>
216  const serialize_numeric_impl<T> &Item) {
217  return Item.deserialize(Reader);
218 }
219 
220 template <typename T, typename U, typename... Args>
221 Error consume(BinaryStreamReader &Reader, T &&X, U &&Y, Args &&... Rest) {
222  if (auto EC = consume(Reader, X))
223  return EC;
224  return consume(Reader, Y, std::forward<Args>(Rest)...);
225 }
226 
227 }
228 }
229 
230 #endif
BinaryStreamReader.h
llvm::BinaryStreamReader::empty
bool empty() const
Definition: BinaryStreamReader.h:253
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::codeview::serialize_null_term_string_array_impl::Item
std::vector< StringRef > & Item
Definition: RecordSerialization.h:137
llvm::codeview::serialize_array_impl
Definition: RecordSerialization.h:91
StringRef.h
Field
OptimizedStructLayoutField Field
Definition: OptimizedStructLayout.cpp:17
llvm::BinaryStreamReader::skip
Error skip(uint64_t Amount)
Advance the stream's offset by Amount bytes.
Definition: BinaryStreamReader.cpp:148
llvm::codeview::serialize_numeric_impl
Definition: RecordSerialization.h:151
llvm::codeview::serialize_null_term_string_array_impl::deserialize
Error deserialize(BinaryStreamReader &Reader) const
Definition: RecordSerialization.h:123
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::codeview::serialize_array_impl::Item
ArrayRef< T > & Item
Definition: RecordSerialization.h:98
llvm::codeview::serialize_numeric_impl::Item
T & Item
Definition: RecordSerialization.h:158
Error.h
llvm::codeview::serialize_conditional_impl::Item
T & Item
Definition: RecordSerialization.h:82
llvm::codeview::serialize_arrayref_tail_impl::serialize_arrayref_tail_impl
serialize_arrayref_tail_impl(ArrayRef< T > &Item)
Definition: RecordSerialization.h:141
llvm::codeview::serialize_array_tail
serialize_vector_tail_impl< T > serialize_array_tail(std::vector< T > &Item)
Definition: RecordSerialization.h:172
llvm::BinaryStreamReader::bytesRemaining
uint64_t bytesRemaining() const
Definition: BinaryStreamReader.h:257
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::codeview::serialize_conditional
serialize_conditional_impl< T, U > serialize_conditional(T &Item, U Func)
Definition: RecordSerialization.h:87
llvm::codeview::cv_error_code::insufficient_buffer
@ insufficient_buffer
llvm::codeview::RecordPrefix
Definition: RecordSerialization.h:33
llvm::codeview::serialize_vector_tail_impl::Item
std::vector< T > & Item
Definition: RecordSerialization.h:116
llvm::codeview::serialize_array_impl::Func
U Func
Definition: RecordSerialization.h:99
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
llvm::codeview::serialize_null_term_string_array_impl
Definition: RecordSerialization.h:119
llvm::APSInt
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:22
CodeView.h
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::codeview::serialize_numeric_impl::serialize_numeric_impl
serialize_numeric_impl(T &Item)
Definition: RecordSerialization.h:152
llvm::codeview::serialize_numeric_impl::deserialize
Error deserialize(BinaryStreamReader &Reader) const
Definition: RecordSerialization.h:154
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::codeview::RecordPrefix::RecordPrefix
RecordPrefix()=default
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
llvm::support::ulittle32_t
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:272
llvm::codeview::serialize_array
serialize_array_impl< T, U > serialize_array(ArrayRef< T > &Item, U Func)
Definition: RecordSerialization.h:162
llvm::support::ulittle16_t
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
Definition: Endian.h:270
llvm::codeview::serialize_null_term_string_array_impl::serialize_null_term_string_array_impl
serialize_null_term_string_array_impl(std::vector< StringRef > &Item)
Definition: RecordSerialization.h:120
llvm::codeview::MaxRecordLength
@ MaxRecordLength
Definition: RecordSerialization.h:31
uint64_t
llvm::codeview::serialize_arrayref_tail_impl::deserialize
Error deserialize(BinaryStreamReader &Reader) const
Definition: RecordSerialization.h:143
llvm::BinaryStreamReader::readCString
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
Definition: BinaryStreamReader.cpp:74
llvm::codeview::serialize_arrayref_tail_impl
Definition: RecordSerialization.h:140
ArrayRef.h
llvm::codeview::getBytesAsCString
StringRef getBytesAsCString(ArrayRef< uint8_t > LeafData)
Definition: RecordSerialization.cpp:32
llvm::codeview::serialize_conditional_impl::serialize_conditional_impl
serialize_conditional_impl(T &Item, U Func)
Definition: RecordSerialization.h:74
llvm::codeview::serialize_vector_tail_impl
Definition: RecordSerialization.h:102
llvm::codeview::CompileSym2Flags::EC
@ EC
APSInt.h
llvm::BinaryStreamReader::readObject
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
Definition: BinaryStreamReader.h:169
llvm::codeview::consume
Error consume(BinaryStreamReader &Reader)
Definition: RecordSerialization.h:46
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::codeview::serialize_null_term_string_array
serialize_null_term_string_array_impl serialize_null_term_string_array(std::vector< StringRef > &Item)
Definition: RecordSerialization.h:167
llvm::codeview::serialize_conditional_impl::Func
U Func
Definition: RecordSerialization.h:83
llvm::codeview::serialize_vector_tail_impl::serialize_vector_tail_impl
serialize_vector_tail_impl(std::vector< T > &Item)
Definition: RecordSerialization.h:103
llvm::codeview::serialize_array_impl::serialize_array_impl
serialize_array_impl(ArrayRef< T > &Item, U Func)
Definition: RecordSerialization.h:92
llvm::codeview::serialize_numeric
serialize_numeric_impl< T > serialize_numeric(T &Item)
Definition: RecordSerialization.h:181
llvm::codeview::serialize_vector_tail_impl::deserialize
Error deserialize(BinaryStreamReader &Reader) const
Definition: RecordSerialization.h:105
uint16_t
llvm::codeview::RecordPrefix::RecordKind
ulittle16_t RecordKind
Definition: RecordSerialization.h:38
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::support::little32_t
detail::packed_endian_specific_integral< int32_t, little, unaligned > little32_t
Definition: Endian.h:279
llvm::codeview::RecordPrefix::RecordPrefix
RecordPrefix(uint16_t Kind)
Definition: RecordSerialization.h:35
llvm::codeview::serialize_conditional_impl::deserialize
Error deserialize(BinaryStreamReader &Reader) const
Definition: RecordSerialization.h:76
llvm::codeview::serialize_arrayref_tail_impl::Item
ArrayRef< T > & Item
Definition: RecordSerialization.h:148
llvm::codeview::getBytesAsCharacters
StringRef getBytesAsCharacters(ArrayRef< uint8_t > LeafData)
Reinterpret a byte array as an array of characters.
Definition: RecordSerialization.cpp:27
llvm::codeview::serialize_array_impl::deserialize
Error deserialize(BinaryStreamReader &Reader) const
Definition: RecordSerialization.h:94
llvm::BinaryStreamReader::peek
uint8_t peek() const
Examine the next byte of the underlying stream without advancing the stream's offset.
Definition: BinaryStreamReader.cpp:160
llvm::codeview::RecordPrefix::RecordLen
ulittle16_t RecordLen
Definition: RecordSerialization.h:37
llvm::codeview::serialize_conditional_impl
Definition: RecordSerialization.h:73
llvm::OptimizedStructLayoutField
A field in a structure.
Definition: OptimizedStructLayout.h:45
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
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
Endian.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::BinaryStreamReader::readArray
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
Definition: BinaryStreamReader.h:187