LLVM 22.0.0git
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_CVRECORD_H
10#define LLVM_DEBUGINFO_CODEVIEW_CVRECORD_H
11
12#include "llvm/ADT/ArrayRef.h"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/Error.h"
20#include <cstdint>
21
22namespace llvm {
23
24namespace codeview {
25
26/// CVRecord is a fat pointer (base + size pair) to a symbol or type record.
27/// Carrying the size separately instead of trusting the size stored in the
28/// record prefix provides some extra safety and flexibility.
29template <typename Kind> class CVRecord {
30public:
31 CVRecord() = default;
32
34
35 CVRecord(const RecordPrefix *P, size_t Size)
36 : RecordData(reinterpret_cast<const uint8_t *>(P), Size) {}
37
38 bool valid() const { return kind() != Kind(0); }
39
40 uint32_t length() const { return RecordData.size(); }
41
42 Kind kind() const {
43 if (RecordData.size() < sizeof(RecordPrefix))
44 return Kind(0);
45 return static_cast<Kind>(static_cast<uint16_t>(
46 reinterpret_cast<const RecordPrefix *>(RecordData.data())->RecordKind));
47 }
48
49 ArrayRef<uint8_t> data() const { return RecordData; }
50
52 return StringRef(reinterpret_cast<const char *>(RecordData.data()),
53 RecordData.size());
54 }
55
57 return RecordData.drop_front(sizeof(RecordPrefix));
58 }
59
61};
62
63// There are two kinds of codeview records: type and symbol records.
66
67template <typename Record, typename Func>
69 while (!StreamBuffer.empty()) {
70 if (StreamBuffer.size() < sizeof(RecordPrefix))
72
73 const RecordPrefix *Prefix =
74 reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
75
76 size_t RealLen = Prefix->RecordLen + 2;
77 if (StreamBuffer.size() < RealLen)
79
80 ArrayRef<uint8_t> Data = StreamBuffer.take_front(RealLen);
81 StreamBuffer = StreamBuffer.drop_front(RealLen);
82
83 Record R(Data);
84 if (auto EC = F(R))
85 return EC;
86 }
87 return Error::success();
88}
89
90/// Read a complete record from a stream at a random offset.
91template <typename Kind>
94 const RecordPrefix *Prefix = nullptr;
95 BinaryStreamReader Reader(Stream);
96 Reader.setOffset(Offset);
97
98 if (auto EC = Reader.readObject(Prefix))
99 return std::move(EC);
100 if (Prefix->RecordLen < 2)
102
103 Reader.setOffset(Offset);
104 ArrayRef<uint8_t> RawData;
105 if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
106 return std::move(EC);
107 return codeview::CVRecord<Kind>(RawData);
108}
109
110} // end namespace codeview
111
112template <typename Kind>
113struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
116 auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
117 if (!ExpectedRec)
118 return ExpectedRec.takeError();
119 Item = *ExpectedRec;
120 Len = ExpectedRec->length();
121 return Error::success();
122 }
123};
124
125namespace codeview {
129} // namespace codeview
130
131} // end namespace llvm
132
133#endif // LLVM_DEBUGINFO_CODEVIEW_CVRECORD_H
aarch64 promote const
#define F(x, y, z)
Definition MD5.cpp:55
#define P(N)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition ArrayRef.h:224
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:200
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
const T * data() const
Definition ArrayRef.h:144
Provides read only access to a subclass of BinaryStream.
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...
LLVM_ABI 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 ...
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
CVRecord is a fat pointer (base + size pair) to a symbol or type record.
Definition CVRecord.h:29
CVRecord(ArrayRef< uint8_t > Data)
Definition CVRecord.h:33
StringRef str_data() const
Definition CVRecord.h:51
CVRecord(const RecordPrefix *P, size_t Size)
Definition CVRecord.h:35
ArrayRef< uint8_t > data() const
Definition CVRecord.h:49
ArrayRef< uint8_t > content() const
Definition CVRecord.h:56
uint32_t length() const
Definition CVRecord.h:40
A range adaptor for a pair of iterators.
iterator_range< CVTypeArray::Iterator > CVTypeRange
Definition CVRecord.h:128
CVRecord< TypeLeafKind > CVType
Definition CVRecord.h:64
VarStreamArray< CVSymbol > CVSymbolArray
Definition CVRecord.h:126
Error forEachCodeViewRecord(ArrayRef< uint8_t > StreamBuffer, Func F)
Definition CVRecord.h:68
VarStreamArray< CVType > CVTypeArray
Definition CVRecord.h:127
CVRecord< SymbolKind > CVSymbol
Definition CVRecord.h:65
Expected< CVRecord< Kind > > readCVRecordFromStream(BinaryStreamRef Stream, uint32_t Offset)
Read a complete record from a stream at a random offset.
Definition CVRecord.h:92
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
Error operator()(BinaryStreamRef Stream, uint32_t &Len, codeview::CVRecord< Kind > &Item)
Definition CVRecord.h:114
VarStreamArrayExtractor is intended to be specialized to provide customized extraction logic.