LLVM  8.0.1
CVRecord.h
Go to the documentation of this file.
1 //===- RecordIterator.h -----------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
11 #define LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/Optional.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include <cstdint>
23 
24 namespace llvm {
25 
26 namespace codeview {
27 
28 template <typename Kind> class CVRecord {
29 public:
30  CVRecord() : Type(static_cast<Kind>(0)) {}
31 
32  CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
33 
34  bool valid() const { return Type != static_cast<Kind>(0); }
35 
36  uint32_t length() const { return RecordData.size(); }
37  Kind kind() const { return Type; }
38  ArrayRef<uint8_t> data() const { return RecordData; }
39  StringRef str_data() const {
40  return StringRef(reinterpret_cast<const char *>(RecordData.data()),
41  RecordData.size());
42  }
43 
45  return RecordData.drop_front(sizeof(RecordPrefix));
46  }
47 
50 };
51 
52 template <typename Kind> struct RemappedRecord {
53  explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {}
54 
57 };
58 
59 template <typename Record, typename Func>
61  while (!StreamBuffer.empty()) {
62  if (StreamBuffer.size() < sizeof(RecordPrefix))
63  return make_error<CodeViewError>(cv_error_code::corrupt_record);
64 
65  const RecordPrefix *Prefix =
66  reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
67 
68  size_t RealLen = Prefix->RecordLen + 2;
69  if (StreamBuffer.size() < RealLen)
70  return make_error<CodeViewError>(cv_error_code::corrupt_record);
71 
72  ArrayRef<uint8_t> Data = StreamBuffer.take_front(RealLen);
73  StreamBuffer = StreamBuffer.drop_front(RealLen);
74 
75  Record R(static_cast<decltype(Record::Type)>((uint16_t)Prefix->RecordKind),
76  Data);
77  if (auto EC = F(R))
78  return EC;
79  }
80  return Error::success();
81 }
82 
83 /// Read a complete record from a stream at a random offset.
84 template <typename Kind>
86  uint32_t Offset) {
87  const RecordPrefix *Prefix = nullptr;
88  BinaryStreamReader Reader(Stream);
89  Reader.setOffset(Offset);
90 
91  if (auto EC = Reader.readObject(Prefix))
92  return std::move(EC);
93  if (Prefix->RecordLen < 2)
94  return make_error<CodeViewError>(cv_error_code::corrupt_record);
95  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
96 
97  Reader.setOffset(Offset);
98  ArrayRef<uint8_t> RawData;
99  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
100  return std::move(EC);
101  return codeview::CVRecord<Kind>(K, RawData);
102 }
103 
104 } // end namespace codeview
105 
106 template <typename Kind>
107 struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
109  codeview::CVRecord<Kind> &Item) {
110  auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
111  if (!ExpectedRec)
112  return ExpectedRec.takeError();
113  Item = *ExpectedRec;
114  Len = ExpectedRec->length();
115  return Error::success();
116  }
117 };
118 
119 } // end namespace llvm
120 
121 #endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:49
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:212
Kind kind() const
Definition: CVRecord.h:37
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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...
SmallVector< std::pair< uint32_t, TypeIndex >, 8 > Mappings
Definition: CVRecord.h:56
CVRecord< Kind > OriginalRecord
Definition: CVRecord.h:55
ArrayRef< uint8_t > content() const
Definition: CVRecord.h:44
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Error forEachCodeViewRecord(ArrayRef< uint8_t > StreamBuffer, Func F)
Definition: CVRecord.h:60
Error operator()(BinaryStreamRef Stream, uint32_t &Len, codeview::CVRecord< Kind > &Item)
Definition: CVRecord.h:108
uint32_t length() const
Definition: CVRecord.h:36
ArrayRef< uint8_t > data() const
Definition: CVRecord.h:38
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
StringRef str_data() const
Definition: CVRecord.h:39
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Expected< CVRecord< Kind > > readCVRecordFromStream(BinaryStreamRef Stream, uint32_t Offset)
Read a complete record from a stream at a random offset.
Definition: CVRecord.h:85
const T * data() const
Definition: ArrayRef.h:146
void setOffset(uint32_t Off)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
CVRecord(Kind K, ArrayRef< uint8_t > Data)
Definition: CVRecord.h:32
ArrayRef< uint8_t > RecordData
Definition: CVRecord.h:49
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:188
const unsigned Kind
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
RemappedRecord(const CVRecord< Kind > &R)
Definition: CVRecord.h:53
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool valid() const
Definition: CVRecord.h:34
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144