LLVM  10.0.0
FaultMaps.cpp
Go to the documentation of this file.
1 //===- FaultMaps.cpp ------------------------------------------------------===//
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 
10 #include "llvm/ADT/Twine.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/Format.h"
20 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "faultmaps"
24 
25 static const int FaultMapVersion = 1;
26 const char *FaultMaps::WFMP = "Fault Maps: ";
27 
29 
31  const MCSymbol *FaultingLabel,
32  const MCSymbol *HandlerLabel) {
33  MCContext &OutContext = AP.OutStreamer->getContext();
34 
35  const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
36  MCSymbolRefExpr::create(FaultingLabel, OutContext),
37  MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
38 
39  const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
40  MCSymbolRefExpr::create(HandlerLabel, OutContext),
41  MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
42 
43  FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
44  HandlerOffset);
45 }
46 
48  if (FunctionInfos.empty())
49  return;
50 
51  MCContext &OutContext = AP.OutStreamer->getContext();
52  MCStreamer &OS = *AP.OutStreamer;
53 
54  // Create the section.
55  MCSection *FaultMapSection =
56  OutContext.getObjectFileInfo()->getFaultMapSection();
57  OS.SwitchSection(FaultMapSection);
58 
59  // Emit a dummy symbol to force section inclusion.
60  OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
61 
62  LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
63 
64  // Header
65  OS.EmitIntValue(FaultMapVersion, 1); // Version.
66  OS.EmitIntValue(0, 1); // Reserved.
67  OS.EmitIntValue(0, 2); // Reserved.
68 
69  LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
70  OS.EmitIntValue(FunctionInfos.size(), 4);
71 
72  LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
73 
74  for (const auto &FFI : FunctionInfos)
75  emitFunctionInfo(FFI.first, FFI.second);
76 }
77 
78 void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
79  const FunctionFaultInfos &FFI) {
80  MCStreamer &OS = *AP.OutStreamer;
81 
82  LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
83  OS.EmitSymbolValue(FnLabel, 8);
84 
85  LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
86  OS.EmitIntValue(FFI.size(), 4);
87 
88  OS.EmitIntValue(0, 4); // Reserved
89 
90  for (auto &Fault : FFI) {
91  LLVM_DEBUG(dbgs() << WFMP << " fault type: "
92  << faultTypeToString(Fault.Kind) << "\n");
93  OS.EmitIntValue(Fault.Kind, 4);
94 
95  LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
96  << *Fault.FaultingOffsetExpr << "\n");
97  OS.EmitValue(Fault.FaultingOffsetExpr, 4);
98 
99  LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
100  << *Fault.HandlerOffsetExpr << "\n");
101  OS.EmitValue(Fault.HandlerOffsetExpr, 4);
102  }
103 }
104 
106  switch (FT) {
107  default:
108  llvm_unreachable("unhandled fault type!");
110  return "FaultingLoad";
112  return "FaultingLoadStore";
114  return "FaultingStore";
115  }
116 }
117 
121  OS << "Fault kind: "
123  << ", faulting PC offset: " << FFI.getFaultingPCOffset()
124  << ", handling PC offset: " << FFI.getHandlerPCOffset();
125  return OS;
126 }
127 
130  OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
131  << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
132  for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
133  OS << FI.getFunctionFaultInfoAt(i) << "\n";
134  return OS;
135 }
136 
138  OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
139  OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
140 
141  if (FMP.getNumFunctions() == 0)
142  return OS;
143 
145 
146  for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
147  FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
148  OS << FI;
149  }
150 
151  return OS;
152 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
FaultingPCOffsetType getFaultingPCOffset() const
Definition: FaultMaps.h:132
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:95
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:329
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
Definition: MCStreamer.cpp:166
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
HandlerPCOffsetType getHandlerPCOffset() const
Definition: FaultMaps.h:136
FunctionAddrType getFunctionAddr() const
Definition: FaultMaps.h:164
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
FunctionInfoAccessor getFirstFunctionInfo() const
Definition: FaultMaps.h:202
Context object for machine code objects.
Definition: MCContext.h:65
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:552
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:131
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:162
static const int FaultMapVersion
Definition: FaultMaps.cpp:25
NumFunctionsType getNumFunctions() const
Definition: FaultMaps.h:198
FaultMaps(AsmPrinter &AP)
Definition: FaultMaps.cpp:28
Streaming machine code generation interface.
Definition: MCStreamer.h:197
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:121
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:80
constexpr double e
Definition: MathExtras.h:57
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:322
FunctionFaultInfoAccessor getFunctionFaultInfoAt(uint32_t Index) const
Definition: FaultMaps.h:172
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A parser for the __llvm_faultmaps section generated by the FaultMaps class above. ...
Definition: FaultMaps.h:83
void serializeToFaultMapSection()
Definition: FaultMaps.cpp:47
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static const char * faultTypeToString(FaultKind)
Definition: FaultMaps.cpp:105
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:130
void recordFaultingOp(FaultKind FaultTy, const MCSymbol *FaultingLabel, const MCSymbol *HandlerLabel)
Definition: FaultMaps.cpp:30
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2096
MCSection * getFaultMapSection() const
FaultMapVersionType getFaultMapVersion() const
Definition: FaultMaps.h:192
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:400
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
MCSymbol * CurrentFnSymForSize
The symbol used to represent the start of the current function for the purpose of calculating its siz...
Definition: AsmPrinter.h:130
NumFaultingPCsType getNumFaultingPCs() const
Definition: FaultMaps.h:168
FunctionInfoAccessor getNextFunctionInfo() const
Definition: FaultMaps.h:179
#define LLVM_DEBUG(X)
Definition: Debug.h:122