]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/FaultMaps.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / CodeGen / FaultMaps.cpp
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
9 #include "llvm/CodeGen/FaultMaps.h"
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/CodeGen/AsmPrinter.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCObjectFileInfo.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/raw_ostream.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
28 FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
29
30 void FaultMaps::recordFaultingOp(FaultKind FaultTy,
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
49 void FaultMaps::serializeToFaultMapSection() {
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
107 const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
108   switch (FT) {
109   default:
110     llvm_unreachable("unhandled fault type!");
111   case FaultMaps::FaultingLoad:
112     return "FaultingLoad";
113   case FaultMaps::FaultingLoadStore:
114     return "FaultingLoadStore";
115   case FaultMaps::FaultingStore:
116     return "FaultingStore";
117   }
118 }
119
120 raw_ostream &llvm::
121 operator<<(raw_ostream &OS,
122            const FaultMapParser::FunctionFaultInfoAccessor &FFI) {
123   OS << "Fault kind: "
124      << FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
125      << ", faulting PC offset: " << FFI.getFaultingPCOffset()
126      << ", handling PC offset: " << FFI.getHandlerPCOffset();
127   return OS;
128 }
129
130 raw_ostream &llvm::
131 operator<<(raw_ostream &OS, const FaultMapParser::FunctionInfoAccessor &FI) {
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
139 raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
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
146   FaultMapParser::FunctionInfoAccessor FI;
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 }