LLVM  9.0.0svn
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 *HandlerLabel) {
32  MCContext &OutContext = AP.OutStreamer->getContext();
33  MCSymbol *FaultingLabel = OutContext.createTempSymbol();
34 
35  AP.OutStreamer->EmitLabel(FaultingLabel);
36 
37  const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
38  MCSymbolRefExpr::create(FaultingLabel, OutContext),
39  MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
40 
41  const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
42  MCSymbolRefExpr::create(HandlerLabel, OutContext),
43  MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
44 
45  FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
46  HandlerOffset);
47 }
48 
50  if (FunctionInfos.empty())
51  return;
52 
53  MCContext &OutContext = AP.OutStreamer->getContext();
54  MCStreamer &OS = *AP.OutStreamer;
55 
56  // Create the section.
57  MCSection *FaultMapSection =
58  OutContext.getObjectFileInfo()->getFaultMapSection();
59  OS.SwitchSection(FaultMapSection);
60 
61  // Emit a dummy symbol to force section inclusion.
62  OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
63 
64  LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
65 
66  // Header
67  OS.EmitIntValue(FaultMapVersion, 1); // Version.
68  OS.EmitIntValue(0, 1); // Reserved.
69  OS.EmitIntValue(0, 2); // Reserved.
70 
71  LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
72  OS.EmitIntValue(FunctionInfos.size(), 4);
73 
74  LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
75 
76  for (const auto &FFI : FunctionInfos)
77  emitFunctionInfo(FFI.first, FFI.second);
78 }
79 
80 void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
81  const FunctionFaultInfos &FFI) {
82  MCStreamer &OS = *AP.OutStreamer;
83 
84  LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
85  OS.EmitSymbolValue(FnLabel, 8);
86 
87  LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
88  OS.EmitIntValue(FFI.size(), 4);
89 
90  OS.EmitIntValue(0, 4); // Reserved
91 
92  for (auto &Fault : FFI) {
93  LLVM_DEBUG(dbgs() << WFMP << " fault type: "
94  << faultTypeToString(Fault.Kind) << "\n");
95  OS.EmitIntValue(Fault.Kind, 4);
96 
97  LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
98  << *Fault.FaultingOffsetExpr << "\n");
99  OS.EmitValue(Fault.FaultingOffsetExpr, 4);
100 
101  LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
102  << *Fault.HandlerOffsetExpr << "\n");
103  OS.EmitValue(Fault.HandlerOffsetExpr, 4);
104  }
105 }
106 
108  switch (FT) {
109  default:
110  llvm_unreachable("unhandled fault type!");
112  return "FaultingLoad";
114  return "FaultingLoadStore";
116  return "FaultingStore";
117  }
118 }
119 
123  OS << "Fault kind: "
125  << ", faulting PC offset: " << FFI.getFaultingPCOffset()
126  << ", handling PC offset: " << FFI.getHandlerPCOffset();
127  return OS;
128 }
129 
132  OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
133  << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
134  for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
135  OS << FI.getFunctionFaultInfoAt(i) << "\n";
136  return OS;
137 }
138 
140  OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
141  OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
142 
143  if (FMP.getNumFunctions() == 0)
144  return OS;
145 
147 
148  for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
149  FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
150  OS << FI;
151  }
152 
153  return OS;
154 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
FaultingPCOffsetType getFaultingPCOffset() const
Definition: FaultMaps.h:131
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:322
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:158
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:185
HandlerPCOffsetType getHandlerPCOffset() const
Definition: FaultMaps.h:135
FunctionAddrType getFunctionAddr() const
Definition: FaultMaps.h:163
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:201
Context object for machine code objects.
Definition: MCContext.h:62
void recordFaultingOp(FaultKind FaultTy, const MCSymbol *HandlerLabel)
Definition: FaultMaps.cpp:30
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:545
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:123
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:154
static const int FaultMapVersion
Definition: FaultMaps.cpp:25
NumFunctionsType getNumFunctions() const
Definition: FaultMaps.h:197
FaultMaps(AsmPrinter &AP)
Definition: FaultMaps.cpp:28
Streaming machine code generation interface.
Definition: MCStreamer.h:188
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:216
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:112
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:78
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:296
FunctionFaultInfoAccessor getFunctionFaultInfoAt(uint32_t Index) const
Definition: FaultMaps.h:171
#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:82
void serializeToFaultMapSection()
Definition: FaultMaps.cpp:49
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:107
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:122
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2038
MCSection * getFaultMapSection() const
FaultMapVersionType getFaultMapVersion() const
Definition: FaultMaps.h:191
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:346
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
MCSymbol * CurrentFnSymForSize
The symbol used to represent the start of the current function for the purpose of calculating its siz...
Definition: AsmPrinter.h:117
NumFaultingPCsType getNumFaultingPCs() const
Definition: FaultMaps.h:167
FunctionInfoAccessor getNextFunctionInfo() const
Definition: FaultMaps.h:178
#define LLVM_DEBUG(X)
Definition: Debug.h:122