LLVM  14.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"
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 /// 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 
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 
58  return RecordData.drop_front(sizeof(RecordPrefix));
59  }
60 
62 };
63 
64 // There are two kinds of codeview records: type and symbol records.
67 
68 template <typename Record, typename Func>
70  while (!StreamBuffer.empty()) {
71  if (StreamBuffer.size() < sizeof(RecordPrefix))
72  return make_error<CodeViewError>(cv_error_code::corrupt_record);
73 
74  const RecordPrefix *Prefix =
75  reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());
76 
77  size_t RealLen = Prefix->RecordLen + 2;
78  if (StreamBuffer.size() < RealLen)
79  return make_error<CodeViewError>(cv_error_code::corrupt_record);
80 
81  ArrayRef<uint8_t> Data = StreamBuffer.take_front(RealLen);
82  StreamBuffer = StreamBuffer.drop_front(RealLen);
83 
84  Record R(Data);
85  if (auto EC = F(R))
86  return EC;
87  }
88  return Error::success();
89 }
90 
91 /// Read a complete record from a stream at a random offset.
92 template <typename Kind>
94  uint32_t Offset) {
95  const RecordPrefix *Prefix = nullptr;
96  BinaryStreamReader Reader(Stream);
97  Reader.setOffset(Offset);
98 
99  if (auto EC = Reader.readObject(Prefix))
100  return std::move(EC);
101  if (Prefix->RecordLen < 2)
102  return make_error<CodeViewError>(cv_error_code::corrupt_record);
103 
104  Reader.setOffset(Offset);
105  ArrayRef<uint8_t> RawData;
106  if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t)))
107  return std::move(EC);
108  return codeview::CVRecord<Kind>(RawData);
109 }
110 
111 } // end namespace codeview
112 
113 template <typename Kind>
114 struct VarStreamArrayExtractor<codeview::CVRecord<Kind>> {
116  codeview::CVRecord<Kind> &Item) {
117  auto ExpectedRec = codeview::readCVRecordFromStream<Kind>(Stream, 0);
118  if (!ExpectedRec)
119  return ExpectedRec.takeError();
120  Item = *ExpectedRec;
121  Len = ExpectedRec->length();
122  return Error::success();
123  }
124 };
125 
126 namespace codeview {
130 } // namespace codeview
131 
132 } // end namespace llvm
133 
134 #endif // LLVM_DEBUGINFO_CODEVIEW_CVRECORD_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
BinaryStreamReader.h
llvm::BinaryStreamReader::setOffset
void setOffset(uint64_t Off)
Definition: BinaryStreamReader.h:254
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Optional.h
RecordSerialization.h
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::BinaryStreamReader::readBytes
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 ...
Definition: BinaryStreamReader.cpp:37
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
Error.h
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::VarStreamArrayExtractor
VarStreamArrayExtractor is intended to be specialized to provide customized extraction logic.
Definition: BinaryStreamArray.h:47
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::codeview::CVRecord::CVRecord
CVRecord(const RecordPrefix *P, size_t Size)
Definition: CVRecord.h:36
llvm::codeview::RecordPrefix
Definition: RecordSerialization.h:33
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::codeview::CVRecord::RecordData
ArrayRef< uint8_t > RecordData
Definition: CVRecord.h:61
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
llvm::ArrayRef::data
const T * data() const
Definition: ArrayRef.h:162
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::codeview::CVRecord::valid
bool valid() const
Definition: CVRecord.h:39
llvm::VarStreamArray< CVSymbol >
llvm::codeview::forEachCodeViewRecord
Error forEachCodeViewRecord(ArrayRef< uint8_t > StreamBuffer, Func F)
Definition: CVRecord.h:69
llvm::VarStreamArrayExtractor< codeview::CVRecord< Kind > >::operator()
Error operator()(BinaryStreamRef Stream, uint32_t &Len, codeview::CVRecord< Kind > &Item)
Definition: CVRecord.h:115
CodeView.h
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
llvm::ArrayRef::drop_front
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:204
llvm::codeview::CVRecord::CVRecord
CVRecord(ArrayRef< uint8_t > Data)
Definition: CVRecord.h:34
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
llvm::codeview::CVRecord::kind
Kind kind() const
Definition: CVRecord.h:43
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
ArrayRef.h
llvm::codeview::CompileSym2Flags::EC
@ EC
llvm::codeview::CVRecord::CVRecord
CVRecord()=default
llvm::BinaryStreamReader::readObject
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...
Definition: BinaryStreamReader.h:169
llvm::codeview::readCVRecordFromStream
Expected< CVRecord< Kind > > readCVRecordFromStream(BinaryStreamRef Stream, uint32_t Offset)
Read a complete record from a stream at a random offset.
Definition: CVRecord.h:93
llvm::Record
Definition: Record.h:1472
llvm::codeview::CVRecord::content
ArrayRef< uint8_t > content() const
Definition: CVRecord.h:57
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
BinaryStreamRef.h
llvm::codeview::CVRecord
CVRecord is a fat pointer (base + size pair) to a symbol or type record.
Definition: CVRecord.h:30
uint32_t
uint16_t
llvm::ArrayRef::take_front
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:228
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::codeview::CVRecord::data
ArrayRef< uint8_t > data() const
Definition: CVRecord.h:50
llvm::codeview::cv_error_code::corrupt_record
@ corrupt_record
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
CodeViewError.h
Endian.h
llvm::codeview::CVRecord::str_data
StringRef str_data() const
Definition: CVRecord.h:52
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition: BinaryStreamRef.h:156
llvm::codeview::CVRecord::length
uint32_t length() const
Definition: CVRecord.h:41