1 //===- FaultMaps.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
23 #define DEBUG_TYPE "faultmaps"
25 static const int FaultMapVersion = 1;
26 const char *FaultMaps::WFMP = "Fault Maps: ";
28 FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
30 void FaultMaps::recordFaultingOp(FaultKind FaultTy,
31 const MCSymbol *FaultingLabel,
32 const MCSymbol *HandlerLabel) {
33 MCContext &OutContext = AP.OutStreamer->getContext();
35 const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
36 MCSymbolRefExpr::create(FaultingLabel, OutContext),
37 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
39 const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
40 MCSymbolRefExpr::create(HandlerLabel, OutContext),
41 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
43 FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
47 void FaultMaps::serializeToFaultMapSection() {
48 if (FunctionInfos.empty())
51 MCContext &OutContext = AP.OutStreamer->getContext();
52 MCStreamer &OS = *AP.OutStreamer;
54 // Create the section.
55 MCSection *FaultMapSection =
56 OutContext.getObjectFileInfo()->getFaultMapSection();
57 OS.SwitchSection(FaultMapSection);
59 // Emit a dummy symbol to force section inclusion.
60 OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
62 LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
65 OS.emitIntValue(FaultMapVersion, 1); // Version.
66 OS.emitIntValue(0, 1); // Reserved.
67 OS.emitInt16(0); // Reserved.
69 LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
70 OS.emitInt32(FunctionInfos.size());
72 LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
74 for (const auto &FFI : FunctionInfos)
75 emitFunctionInfo(FFI.first, FFI.second);
78 void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
79 const FunctionFaultInfos &FFI) {
80 MCStreamer &OS = *AP.OutStreamer;
82 LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
83 OS.emitSymbolValue(FnLabel, 8);
85 LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
86 OS.emitInt32(FFI.size());
88 OS.emitInt32(0); // Reserved
90 for (auto &Fault : FFI) {
91 LLVM_DEBUG(dbgs() << WFMP << " fault type: "
92 << faultTypeToString(Fault.Kind) << "\n");
93 OS.emitInt32(Fault.Kind);
95 LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
96 << *Fault.FaultingOffsetExpr << "\n");
97 OS.emitValue(Fault.FaultingOffsetExpr, 4);
99 LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
100 << *Fault.HandlerOffsetExpr << "\n");
101 OS.emitValue(Fault.HandlerOffsetExpr, 4);
105 const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
108 llvm_unreachable("unhandled fault type!");
109 case FaultMaps::FaultingLoad:
110 return "FaultingLoad";
111 case FaultMaps::FaultingLoadStore:
112 return "FaultingLoadStore";
113 case FaultMaps::FaultingStore:
114 return "FaultingStore";
119 operator<<(raw_ostream &OS,
120 const FaultMapParser::FunctionFaultInfoAccessor &FFI) {
122 << FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
123 << ", faulting PC offset: " << FFI.getFaultingPCOffset()
124 << ", handling PC offset: " << FFI.getHandlerPCOffset();
129 operator<<(raw_ostream &OS, const FaultMapParser::FunctionInfoAccessor &FI) {
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";
137 raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
138 OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
139 OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
141 if (FMP.getNumFunctions() == 0)
144 FaultMapParser::FunctionInfoAccessor FI;
146 for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
147 FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();