1 //===------------------- FaultMaps.h - The "FaultMaps" section --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CODEGEN_FAULTMAPS_H
11 #define LLVM_CODEGEN_FAULTMAPS_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/Format.h"
30 enum FaultKind { FaultingLoad = 1, FaultKindMax };
32 static const char *faultTypeToString(FaultKind);
34 explicit FaultMaps(AsmPrinter &AP);
36 void recordFaultingOp(FaultKind FaultTy, const MCSymbol *HandlerLabel);
37 void serializeToFaultMapSection();
40 static const char *WFMP;
44 const MCExpr *FaultingOffsetExpr;
45 const MCExpr *HandlerOffsetExpr;
48 : Kind(FaultKindMax), FaultingOffsetExpr(nullptr),
49 HandlerOffsetExpr(nullptr) {}
51 explicit FaultInfo(FaultMaps::FaultKind Kind, const MCExpr *FaultingOffset,
52 const MCExpr *HandlerOffset)
53 : Kind(Kind), FaultingOffsetExpr(FaultingOffset),
54 HandlerOffsetExpr(HandlerOffset) {}
57 typedef std::vector<FaultInfo> FunctionFaultInfos;
59 // We'd like to keep a stable iteration order for FunctionInfos to help
60 // FileCheck based testing.
61 struct MCSymbolComparator {
62 bool operator()(const MCSymbol *LHS, const MCSymbol *RHS) const {
63 return LHS->getName() < RHS->getName();
67 std::map<const MCSymbol *, FunctionFaultInfos, MCSymbolComparator>
71 void emitFunctionInfo(const MCSymbol *FnLabel, const FunctionFaultInfos &FFI);
74 /// A parser for the __llvm_faultmaps section generated by the FaultMaps class
75 /// above. This parser is version locked with with the __llvm_faultmaps section
76 /// generated by the version of LLVM that includes it. No guarantees are made
77 /// with respect to forward or backward compatibility.
78 class FaultMapParser {
79 typedef uint8_t FaultMapVersionType;
80 static const size_t FaultMapVersionOffset = 0;
82 typedef uint8_t Reserved0Type;
83 static const size_t Reserved0Offset =
84 FaultMapVersionOffset + sizeof(FaultMapVersionType);
86 typedef uint16_t Reserved1Type;
87 static const size_t Reserved1Offset = Reserved0Offset + sizeof(Reserved0Type);
89 typedef uint32_t NumFunctionsType;
90 static const size_t NumFunctionsOffset =
91 Reserved1Offset + sizeof(Reserved1Type);
93 static const size_t FunctionInfosOffset =
94 NumFunctionsOffset + sizeof(NumFunctionsType);
99 template <typename T> static T read(const uint8_t *P, const uint8_t *E) {
100 assert(P + sizeof(T) <= E && "out of bounds read!");
101 return support::endian::read<T, support::little, 1>(P);
105 class FunctionFaultInfoAccessor {
106 typedef uint32_t FaultKindType;
107 static const size_t FaultKindOffset = 0;
109 typedef uint32_t FaultingPCOffsetType;
110 static const size_t FaultingPCOffsetOffset =
111 FaultKindOffset + sizeof(FaultKindType);
113 typedef uint32_t HandlerPCOffsetType;
114 static const size_t HandlerPCOffsetOffset =
115 FaultingPCOffsetOffset + sizeof(FaultingPCOffsetType);
121 static const size_t Size =
122 HandlerPCOffsetOffset + sizeof(HandlerPCOffsetType);
124 explicit FunctionFaultInfoAccessor(const uint8_t *P, const uint8_t *E)
127 FaultKindType getFaultKind() const {
128 return read<FaultKindType>(P + FaultKindOffset, E);
131 FaultingPCOffsetType getFaultingPCOffset() const {
132 return read<FaultingPCOffsetType>(P + FaultingPCOffsetOffset, E);
135 HandlerPCOffsetType getHandlerPCOffset() const {
136 return read<HandlerPCOffsetType>(P + HandlerPCOffsetOffset, E);
140 class FunctionInfoAccessor {
141 typedef uint64_t FunctionAddrType;
142 static const size_t FunctionAddrOffset = 0;
144 typedef uint32_t NumFaultingPCsType;
145 static const size_t NumFaultingPCsOffset =
146 FunctionAddrOffset + sizeof(FunctionAddrType);
148 typedef uint32_t ReservedType;
149 static const size_t ReservedOffset =
150 NumFaultingPCsOffset + sizeof(NumFaultingPCsType);
152 static const size_t FunctionFaultInfosOffset =
153 ReservedOffset + sizeof(ReservedType);
155 static const size_t FunctionInfoHeaderSize = FunctionFaultInfosOffset;
161 FunctionInfoAccessor() : P(nullptr), E(nullptr) {}
163 explicit FunctionInfoAccessor(const uint8_t *P, const uint8_t *E)
166 FunctionAddrType getFunctionAddr() const {
167 return read<FunctionAddrType>(P + FunctionAddrOffset, E);
170 NumFaultingPCsType getNumFaultingPCs() const {
171 return read<NumFaultingPCsType>(P + NumFaultingPCsOffset, E);
174 FunctionFaultInfoAccessor getFunctionFaultInfoAt(uint32_t Index) const {
175 assert(Index < getNumFaultingPCs() && "index out of bounds!");
176 const uint8_t *Begin = P + FunctionFaultInfosOffset +
177 FunctionFaultInfoAccessor::Size * Index;
178 return FunctionFaultInfoAccessor(Begin, E);
181 FunctionInfoAccessor getNextFunctionInfo() const {
182 size_t MySize = FunctionInfoHeaderSize +
183 getNumFaultingPCs() * FunctionFaultInfoAccessor::Size;
185 const uint8_t *Begin = P + MySize;
186 assert(Begin < E && "out of bounds!");
187 return FunctionInfoAccessor(Begin, E);
191 explicit FaultMapParser(const uint8_t *Begin, const uint8_t *End)
192 : P(Begin), E(End) {}
194 FaultMapVersionType getFaultMapVersion() const {
195 auto Version = read<FaultMapVersionType>(P + FaultMapVersionOffset, E);
196 assert(Version == 1 && "only version 1 supported!");
200 NumFunctionsType getNumFunctions() const {
201 return read<NumFunctionsType>(P + NumFunctionsOffset, E);
204 FunctionInfoAccessor getFirstFunctionInfo() const {
205 const uint8_t *Begin = P + FunctionInfosOffset;
206 return FunctionInfoAccessor(Begin, E);
211 operator<<(raw_ostream &OS, const FaultMapParser::FunctionFaultInfoAccessor &);
213 raw_ostream &operator<<(raw_ostream &OS,
214 const FaultMapParser::FunctionInfoAccessor &);
216 raw_ostream &operator<<(raw_ostream &OS, const FaultMapParser &);