LLVM  3.7.0
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 
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/Support/Debug.h"
18 
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "faultmaps"
22 
23 static const int FaultMapVersion = 1;
24 const char *FaultMaps::WFMP = "Fault Maps: ";
25 
27 
29  const MCSymbol *HandlerLabel) {
30  MCContext &OutContext = AP.OutStreamer->getContext();
31  MCSymbol *FaultingLabel = OutContext.createTempSymbol();
32 
33  AP.OutStreamer->EmitLabel(FaultingLabel);
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  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  DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
70  OS.EmitIntValue(FunctionInfos.size(), 4);
71 
72  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  DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
83  OS.EmitSymbolValue(FnLabel, 8);
84 
85  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  DEBUG(dbgs() << WFMP << " fault type: "
92  << faultTypeToString(Fault.Kind) << "\n");
93  OS.EmitIntValue(Fault.Kind, 4);
94 
95  DEBUG(dbgs() << WFMP << " faulting PC offset: "
96  << *Fault.FaultingOffsetExpr << "\n");
97  OS.EmitValue(Fault.FaultingOffsetExpr, 4);
98 
99  DEBUG(dbgs() << WFMP << " fault handler PC offset: "
100  << *Fault.HandlerOffsetExpr << "\n");
101  OS.EmitValue(Fault.HandlerOffsetExpr, 4);
102  }
103 }
104 
105 
107  switch (FT) {
108  default:
109  llvm_unreachable("unhandled fault type!");
110 
112  return "FaultingLoad";
113  }
114 }
115 
119  OS << "Fault kind: "
121  << ", faulting PC offset: " << FFI.getFaultingPCOffset()
122  << ", handling PC offset: " << FFI.getHandlerPCOffset();
123  return OS;
124 }
125 
128  OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
129  << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
130  for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
131  OS << FI.getFunctionFaultInfoAt(i) << "\n";
132  return OS;
133 }
134 
136  OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
137  OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
138 
139  if (FMP.getNumFunctions() == 0)
140  return OS;
141 
143 
144  for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
145  FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
146  OS << FI;
147  }
148 
149  return OS;
150 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:83
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:315
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:115
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:162
FunctionInfoAccessor getFirstFunctionInfo() const
Definition: FaultMaps.h:204
MCSection * getFaultMapSection() const
FaultingPCOffsetType getFaultingPCOffset() const
Definition: FaultMaps.h:131
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Context object for machine code objects.
Definition: MCContext.h:48
void recordFaultingOp(FaultKind FaultTy, const MCSymbol *HandlerLabel)
Definition: FaultMaps.cpp:28
NumFaultingPCsType getNumFaultingPCs() const
Definition: FaultMaps.h:170
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:514
HandlerPCOffsetType getHandlerPCOffset() const
Definition: FaultMaps.h:135
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:79
static const int FaultMapVersion
Definition: FaultMaps.cpp:23
FaultMaps(AsmPrinter &AP)
Definition: FaultMaps.cpp:26
Streaming machine code generation interface.
Definition: MCStreamer.h:157
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:222
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:98
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:701
FunctionFaultInfoAccessor getFunctionFaultInfoAt(uint32_t Index) const
Definition: FaultMaps.h:174
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
A parser for the __llvm_faultmaps section generated by the FaultMaps class above. ...
Definition: FaultMaps.h:78
virtual void EmitLabel(MCSymbol *Symbol)
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:203
void serializeToFaultMapSection()
Definition: FaultMaps.cpp:47
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
NumFunctionsType getNumFunctions() const
Definition: FaultMaps.h:200
FaultMapVersionType getFaultMapVersion() const
Definition: FaultMaps.h:194
static const char * faultTypeToString(FaultKind)
Definition: FaultMaps.cpp:106
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:111
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1738
void EmitValue(const MCExpr *Value, unsigned Size, const SMLoc &Loc=SMLoc())
Definition: MCStreamer.cpp:110
FunctionInfoAccessor getNextFunctionInfo() const
Definition: FaultMaps.h:181
FunctionAddrType getFunctionAddr() const
Definition: FaultMaps.h:166
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:229
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
#define DEBUG(X)
Definition: Debug.h:92
MCSymbol * CurrentFnSymForSize
The symbol used to represent the start of the current function for the purpose of calculating its siz...
Definition: AsmPrinter.h:103