]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/DebugInfo/CodeView/CVRecord.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / DebugInfo / CodeView / CVRecord.h
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:
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
44   ArrayRef<uint8_t> content() const {
45     return RecordData.drop_front(sizeof(RecordPrefix));
46   }
47
48   Kind Type;
49   ArrayRef<uint8_t> RecordData;
50 };
51
52 template <typename Kind> struct RemappedRecord {
53   explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {}
54
55   CVRecord<Kind> OriginalRecord;
56   SmallVector<std::pair<uint32_t, TypeIndex>, 8> Mappings;
57 };
58
59 template <typename Record, typename Func>
60 Error forEachCodeViewRecord(ArrayRef<uint8_t> StreamBuffer, Func F) {
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>
85 inline Expected<CVRecord<Kind>> readCVRecordFromStream(BinaryStreamRef Stream,
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>> {
108   Error operator()(BinaryStreamRef Stream, uint32_t &Len,
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