LLVM  10.0.0svn
CVRecord.h
Go to the documentation of this file.
1 //===- CVRecord.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_RECORDITERATOR_H
10 #define LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/Optional.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/Error.h"
21 #include <cstdint>
22 
23 namespace llvm {
24 
25 namespace codeview {
26 
27 /// CVRecord is a fat pointer (base + size pair) to a symbol or type record.
28 /// Carrying the size separately instead of trusting the size stored in the
29 /// record prefix provides some extra safety and flexibility.
30 template <typename Kind> class CVRecord {
31 public:
32  CVRecord() = default;
33 
35 
36  CVRecord(const RecordPrefix *P, size_t Size)
37  : RecordData(reinterpret_cast<const uint8_t *>(P), Size) {}
38 
39  bool valid() const { return kind() != Kind(0); }
40 
41  uint32_t length() const { return RecordData.size(); }
42 
43  Kind kind() const {
44  if (RecordData.size() < sizeof(RecordPrefix))
45  return Kind(0);
46  return static_cast<Kind>(static_cast<uint16_t>(
47  reinterpret_cast<const RecordPrefix *>(RecordData.data())->RecordKind));
48  }
49 
50  ArrayRef<uint8_t> data() const { return RecordData; }
51 
52  StringRef str_data() const {
53  return StringRef(reinterpret_cast<const char *>(RecordData.data()),
54  RecordData.size());
55  }
56 
58  return RecordData.drop_front(sizeof(RecordPrefix));
59  }
60 
62 };
63 
64 template <typename Kind> struct RemappedRecord {
65  explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {}
66 
69 };
70 
71 template <typename Record, typename Func>
73  while (!StreamBuffer.empty()) {
74  if (StreamBuffer.size() < sizeof(RecordPrefix))
75  return make_error<CodeViewError>(cv_error_code::corrupt_record);
76 
77  const RecordPrefix *Prefix =
78  reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
79 
80  size_t RealLen = Prefix->RecordLen + 2;
81  if (StreamBuffer.size() < RealLen)
82  return make_error<CodeViewError>(cv_error_code::corrupt_record);
83 
84  ArrayRef<uint8_t> Data = StreamBuffer.take_front(RealLen);
85  StreamBuffer = StreamBuffer.drop_front(RealLen);
86 
87  Record R(Data);
88  if (auto EC = F(R))
89  return EC;
90  }
91  return Error::success();
92 }
93 
94 /// Read a complete record from a stream at a random offset.
95 template <typename Kind>
97  uint32_t Offset) {
98  const RecordPrefix *Prefix = nullptr;
99  BinaryStreamReader Reader(Stream);
100  Reader.setOffset(Offset);
101 
102  if (auto EC = Reader.readObject(Prefix))
103  return std::move(EC);
104  if (Prefix->RecordLen < 2)
105  return make_error<CodeViewError>(cv_error_code::corrupt_record);
106 
107  Reader.setOffset(Offset);
108  ArrayRef<uint8_t> RawData;
109  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
110  return std::move(EC);
111  return codeview::CVRecord<Kind>(RawData);
112 }
113 
114 } // end namespace codeview
115 
116 template <typename Kind>
117 struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
119  codeview::CVRecord<Kind> &Item) {
120  auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
121  if (!ExpectedRec)
122  return ExpectedRec.takeError();
123  Item = *ExpectedRec;
124  Len = ExpectedRec->length();
125  return Error::success();
126  }
127 };
128 
129 } // end namespace llvm
130 
131 #endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:211
Kind kind() const
Definition: CVRecord.h:43
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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...
F(f)
VarStreamArrayExtractor is intended to be specialized to provide customized extraction logic...
CVRecord(ArrayRef< uint8_t > Data)
Definition: CVRecord.h:34
SmallVector< std::pair< uint32_t, TypeIndex >, 8 > Mappings
Definition: CVRecord.h:68
CVRecord< Kind > OriginalRecord
Definition: CVRecord.h:67
ArrayRef< uint8_t > content() const
Definition: CVRecord.h:57
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
Error forEachCodeViewRecord(ArrayRef< uint8_t > StreamBuffer, Func F)
Definition: CVRecord.h:72
Error operator()(BinaryStreamRef Stream, uint32_t &Len, codeview::CVRecord< Kind > &Item)
Definition: CVRecord.h:118
uint32_t length() const
Definition: CVRecord.h:41
ArrayRef< uint8_t > data() const
Definition: CVRecord.h:50
#define P(N)
StringRef str_data() const
Definition: CVRecord.h:52
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
Expected< CVRecord< Kind > > readCVRecordFromStream(BinaryStreamRef Stream, uint32_t Offset)
Read a complete record from a stream at a random offset.
Definition: CVRecord.h:96
const T * data() const
Definition: ArrayRef.h:145
void setOffset(uint32_t Off)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
CVRecord(const RecordPrefix *P, size_t Size)
Definition: CVRecord.h:36
ArrayRef< uint8_t > RecordData
Definition: CVRecord.h:61
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Read Size bytes from the underlying stream at the current offset and and set Buffer to the resulting ...
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:187
uint32_t Size
Definition: Profile.cpp:46
aarch64 promote const
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
RemappedRecord(const CVRecord< Kind > &R)
Definition: CVRecord.h:65
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
CVRecord is a fat pointer (base + size pair) to a symbol or type record.
Definition: CVRecord.h:30
bool valid() const
Definition: CVRecord.h:39
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143