LLVM  9.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 template <typename Kind> class CVRecord {
28 public:
29  CVRecord() : Type(static_cast<Kind>(0)) {}
30 
31  CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
32 
33  bool valid() const { return Type != static_cast<Kind>(0); }
34 
35  uint32_t length() const { return RecordData.size(); }
36  Kind kind() const { return Type; }
37  ArrayRef<uint8_t> data() const { return RecordData; }
38  StringRef str_data() const {
39  return StringRef(reinterpret_cast<const char *>(RecordData.data()),
40  RecordData.size());
41  }
42 
44  return RecordData.drop_front(sizeof(RecordPrefix));
45  }
46 
49 };
50 
51 template <typename Kind> struct RemappedRecord {
52  explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {}
53 
56 };
57 
58 template <typename Record, typename Func>
60  while (!StreamBuffer.empty()) {
61  if (StreamBuffer.size() < sizeof(RecordPrefix))
62  return make_error<CodeViewError>(cv_error_code::corrupt_record);
63 
64  const RecordPrefix *Prefix =
65  reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
66 
67  size_t RealLen = Prefix->RecordLen + 2;
68  if (StreamBuffer.size() < RealLen)
69  return make_error<CodeViewError>(cv_error_code::corrupt_record);
70 
71  ArrayRef<uint8_t> Data = StreamBuffer.take_front(RealLen);
72  StreamBuffer = StreamBuffer.drop_front(RealLen);
73 
74  Record R(static_cast<decltype(Record::Type)>((uint16_t)Prefix->RecordKind),
75  Data);
76  if (auto EC = F(R))
77  return EC;
78  }
79  return Error::success();
80 }
81 
82 /// Read a complete record from a stream at a random offset.
83 template <typename Kind>
85  uint32_t Offset) {
86  const RecordPrefix *Prefix = nullptr;
87  BinaryStreamReader Reader(Stream);
88  Reader.setOffset(Offset);
89 
90  if (auto EC = Reader.readObject(Prefix))
91  return std::move(EC);
92  if (Prefix->RecordLen < 2)
93  return make_error<CodeViewError>(cv_error_code::corrupt_record);
94  Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
95 
96  Reader.setOffset(Offset);
97  ArrayRef<uint8_t> RawData;
98  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
99  return std::move(EC);
100  return codeview::CVRecord<Kind>(K, RawData);
101 }
102 
103 } // end namespace codeview
104 
105 template <typename Kind>
106 struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
108  codeview::CVRecord<Kind> &Item) {
109  auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
110  if (!ExpectedRec)
111  return ExpectedRec.takeError();
112  Item = *ExpectedRec;
113  Len = ExpectedRec->length();
114  return Error::success();
115  }
116 };
117 
118 } // end namespace llvm
119 
120 #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:48
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:36
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...
SmallVector< std::pair< uint32_t, TypeIndex >, 8 > Mappings
Definition: CVRecord.h:55
CVRecord< Kind > OriginalRecord
Definition: CVRecord.h:54
ArrayRef< uint8_t > content() const
Definition: CVRecord.h:43
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
Error forEachCodeViewRecord(ArrayRef< uint8_t > StreamBuffer, Func F)
Definition: CVRecord.h:59
Error operator()(BinaryStreamRef Stream, uint32_t &Len, codeview::CVRecord< Kind > &Item)
Definition: CVRecord.h:107
uint32_t length() const
Definition: CVRecord.h:35
ArrayRef< uint8_t > data() const
Definition: CVRecord.h:37
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
StringRef str_data() const
Definition: CVRecord.h:38
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:84
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:839
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
CVRecord(Kind K, ArrayRef< uint8_t > Data)
Definition: CVRecord.h:31
ArrayRef< uint8_t > RecordData
Definition: CVRecord.h:48
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
const unsigned Kind
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
RemappedRecord(const CVRecord< Kind > &R)
Definition: CVRecord.h:52
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool valid() const
Definition: CVRecord.h:33
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143