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"
15 #include "llvm/DebugInfo/CodeView/CodeViewError.h"
16 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
17 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
18 #include "llvm/Support/BinaryStreamReader.h"
19 #include "llvm/Support/BinaryStreamRef.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:
CVRecord()30 CVRecord() : Type(static_cast<Kind>(0)) {}
31
CVRecord(Kind K,ArrayRef<uint8_t> Data)32 CVRecord(Kind K, ArrayRef<uint8_t> Data) : Type(K), RecordData(Data) {}
33
valid()34 bool valid() const { return Type != static_cast<Kind>(0); }
35
length()36 uint32_t length() const { return RecordData.size(); }
kind()37 Kind kind() const { return Type; }
data()38 ArrayRef<uint8_t> data() const { return RecordData; }
str_data()39 StringRef str_data() const {
40 return StringRef(reinterpret_cast<const char *>(RecordData.data()),
41 RecordData.size());
42 }
43
content()44 ArrayRef<uint8_t> content() const {
45 return RecordData.drop_front(sizeof(RecordPrefix));
46 }
47
hash()48 Optional<uint32_t> hash() const { return Hash; }
49
setHash(uint32_t Value)50 void setHash(uint32_t Value) { Hash = Value; }
51
52 Kind Type;
53 ArrayRef<uint8_t> RecordData;
54 Optional<uint32_t> Hash;
55 };
56
57 template <typename Kind> struct RemappedRecord {
RemappedRecordRemappedRecord58 explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {}
59
60 CVRecord<Kind> OriginalRecord;
61 SmallVector<std::pair<uint32_t, TypeIndex>, 8> Mappings;
62 };
63
64 template <typename Record, typename Func>
forEachCodeViewRecord(ArrayRef<uint8_t> StreamBuffer,Func F)65 Error forEachCodeViewRecord(ArrayRef<uint8_t> StreamBuffer, Func F) {
66 while (!StreamBuffer.empty()) {
67 if (StreamBuffer.size() < sizeof(RecordPrefix))
68 return make_error<CodeViewError>(cv_error_code::corrupt_record);
69
70 const RecordPrefix *Prefix =
71 reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
72
73 size_t RealLen = Prefix->RecordLen + 2;
74 if (StreamBuffer.size() < RealLen)
75 return make_error<CodeViewError>(cv_error_code::corrupt_record);
76
77 ArrayRef<uint8_t> Data = StreamBuffer.take_front(RealLen);
78 StreamBuffer = StreamBuffer.drop_front(RealLen);
79
80 Record R(static_cast<decltype(Record::Type)>((uint16_t)Prefix->RecordKind),
81 Data);
82 if (auto EC = F(R))
83 return EC;
84 }
85 return Error::success();
86 }
87
88 /// Read a complete record from a stream at a random offset.
89 template <typename Kind>
readCVRecordFromStream(BinaryStreamRef Stream,uint32_t Offset)90 inline Expected<CVRecord<Kind>> readCVRecordFromStream(BinaryStreamRef Stream,
91 uint32_t Offset) {
92 const RecordPrefix *Prefix = nullptr;
93 BinaryStreamReader Reader(Stream);
94 Reader.setOffset(Offset);
95
96 if (auto EC = Reader.readObject(Prefix))
97 return std::move(EC);
98 if (Prefix->RecordLen < 2)
99 return make_error<CodeViewError>(cv_error_code::corrupt_record);
100 Kind K = static_cast<Kind>(uint16_t(Prefix->RecordKind));
101
102 Reader.setOffset(Offset);
103 ArrayRef<uint8_t> RawData;
104 if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
105 return std::move(EC);
106 return codeview::CVRecord<Kind>(K, RawData);
107 }
108
109 } // end namespace codeview
110
111 template <typename Kind>
112 struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
113 Error operator()(BinaryStreamRef Stream, uint32_t &Len,
114 codeview::CVRecord<Kind> &Item) {
115 auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
116 if (!ExpectedRec)
117 return ExpectedRec.takeError();
118 Item = *ExpectedRec;
119 Len = ExpectedRec->length();
120 return Error::success();
121 }
122 };
123
124 } // end namespace llvm
125
126 #endif // LLVM_DEBUGINFO_CODEVIEW_RECORDITERATOR_H
127