LLVM  8.0.1
FaultMaps.cpp
Go to the documentation of this file.
1 //===- FaultMaps.cpp ------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/CodeGen/FaultMaps.h"
11 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/Format.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "faultmaps"
25 
26 static const int FaultMapVersion = 1;
27 const char *FaultMaps::WFMP = "Fault Maps: ";
28 
30 
32  const MCSymbol *HandlerLabel) {
33  MCContext &OutContext = AP.OutStreamer->getContext();
34  MCSymbol *FaultingLabel = OutContext.createTempSymbol();
35 
36  AP.OutStreamer->EmitLabel(FaultingLabel);
37 
38  const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
39  MCSymbolRefExpr::create(FaultingLabel, OutContext),
40  MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
41 
42  const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
43  MCSymbolRefExpr::create(HandlerLabel, OutContext),
44  MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
45 
46  FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
47  HandlerOffset);
48 }
49 
51  if (FunctionInfos.empty())
52  return;
53 
54  MCContext &OutContext = AP.OutStreamer->getContext();
55  MCStreamer &OS = *AP.OutStreamer;
56 
57  // Create the section.
58  MCSection *FaultMapSection =
59  OutContext.getObjectFileInfo()->getFaultMapSection();
60  OS.SwitchSection(FaultMapSection);
61 
62  // Emit a dummy symbol to force section inclusion.
63  OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
64 
65  LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
66 
67  // Header
68  OS.EmitIntValue(FaultMapVersion, 1); // Version.
69  OS.EmitIntValue(0, 1); // Reserved.
70  OS.EmitIntValue(0, 2); // Reserved.
71 
72  LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
73  OS.EmitIntValue(FunctionInfos.size(), 4);
74 
75  LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
76 
77  for (const auto &FFI : FunctionInfos)
78  emitFunctionInfo(FFI.first, FFI.second);
79 }
80 
81 void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
82  const FunctionFaultInfos &FFI) {
83  MCStreamer &OS = *AP.OutStreamer;
84 
85  LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
86  OS.EmitSymbolValue(FnLabel, 8);
87 
88  LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
89  OS.EmitIntValue(FFI.size(), 4);
90 
91  OS.EmitIntValue(0, 4); // Reserved
92 
93  for (auto &Fault : FFI) {
94  LLVM_DEBUG(dbgs() << WFMP << " fault type: "
95  << faultTypeToString(Fault.Kind) << "\n");
96  OS.EmitIntValue(Fault.Kind, 4);
97 
98  LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
99  << *Fault.FaultingOffsetExpr << "\n");
100  OS.EmitValue(Fault.FaultingOffsetExpr, 4);
101 
102  LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
103  << *Fault.HandlerOffsetExpr << "\n");
104  OS.EmitValue(Fault.HandlerOffsetExpr, 4);
105  }
106 }
107 
109  switch (FT) {
110  default:
111  llvm_unreachable("unhandled fault type!");
113  return "FaultingLoad";
115  return "FaultingLoadStore";
117  return "FaultingStore";
118  }
119 }
120 
124  OS << "Fault kind: "
126  << ", faulting PC offset: " << FFI.getFaultingPCOffset()
127  << ", handling PC offset: " << FFI.getHandlerPCOffset();
128  return OS;
129 }
130 
133  OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
134  << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
135  for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
136  OS << FI.getFunctionFaultInfoAt(i) << "\n";
137  return OS;
138 }
139 
141  OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
142  OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
143 
144  if (FMP.getNumFunctions() == 0)
145  return OS;
146 
148 
149  for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
150  FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
151  OS << FI;
152  }
153 
154  return OS;
155 }
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:94
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:323
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:159
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
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:81
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
FunctionInfoAccessor getFirstFunctionInfo() const
Definition: FaultMaps.h:202
Context object for machine code objects.
Definition: MCContext.h:63
void recordFaultingOp(FaultKind FaultTy, const MCSymbol *HandlerLabel)
Definition: FaultMaps.cpp:31
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:546
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:124
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:155
static const int FaultMapVersion
Definition: FaultMaps.cpp:26
NumFunctionsType getNumFunctions() const
Definition: FaultMaps.h:198
FaultMaps(AsmPrinter &AP)
Definition: FaultMaps.cpp:29
Streaming machine code generation interface.
Definition: MCStreamer.h:189
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:217
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:113
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:79
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:297
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:50
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
static const char * faultTypeToString(FaultKind)
Definition: FaultMaps.cpp:108
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:123
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2039
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:347
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:118
NumFaultingPCsType getNumFaultingPCs() const
Definition: FaultMaps.h:168
FunctionInfoAccessor getNextFunctionInfo() const
Definition: FaultMaps.h:179
#define LLVM_DEBUG(X)
Definition: Debug.h:123