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" 14 #include "llvm/DebugInfo/CodeView/CodeViewError.h" 15 #include "llvm/DebugInfo/CodeView/RecordSerialization.h" 16 #include "llvm/DebugInfo/CodeView/TypeIndex.h" 17 #include "llvm/Support/BinaryStreamReader.h" 18 #include "llvm/Support/BinaryStreamRef.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 34 CVRecord(ArrayRef<uint8_t> Data) : RecordData(Data) {} 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 57 ArrayRef<uint8_t> content() const { 58 return RecordData.drop_front(sizeof(RecordPrefix)); 59 } 60 61 ArrayRef<uint8_t> RecordData; 62 }; 63 64 template <typename Kind> struct RemappedRecord { 65 explicit RemappedRecord(const CVRecord<Kind> &R) : OriginalRecord(R) {} 66 67 CVRecord<Kind> OriginalRecord; 68 SmallVector<std::pair<uint32_t, TypeIndex>, 8> Mappings; 69 }; 70 71 template <typename Record, typename Func> 72 Error forEachCodeViewRecord(ArrayRef<uint8_t> StreamBuffer, Func F) { 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> 96 inline Expected<CVRecord<Kind>> readCVRecordFromStream(BinaryStreamRef Stream, 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>> { 118 Error operator()(BinaryStreamRef Stream, uint32_t &Len, 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 132