LLVM 17.0.0git
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
19using namespace llvm;
20
21#define DEBUG_TYPE "faultmaps"
22
23static const int FaultMapVersion = 1;
24const char *FaultMaps::WFMP = "Fault Maps: ";
25
27
29 const MCSymbol *FaultingLabel,
30 const MCSymbol *HandlerLabel) {
31 MCContext &OutContext = AP.OutStreamer->getContext();
32
33 const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
34 MCSymbolRefExpr::create(FaultingLabel, OutContext),
35 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
36
37 const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
38 MCSymbolRefExpr::create(HandlerLabel, OutContext),
39 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
40
41 FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
42 HandlerOffset);
43}
44
46 if (FunctionInfos.empty())
47 return;
48
49 MCContext &OutContext = AP.OutStreamer->getContext();
50 MCStreamer &OS = *AP.OutStreamer;
51
52 // Create the section.
53 MCSection *FaultMapSection =
55 OS.switchSection(FaultMapSection);
56
57 // Emit a dummy symbol to force section inclusion.
58 OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
59
60 LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
61
62 // Header
63 OS.emitIntValue(FaultMapVersion, 1); // Version.
64 OS.emitIntValue(0, 1); // Reserved.
65 OS.emitInt16(0); // Reserved.
66
67 LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
68 OS.emitInt32(FunctionInfos.size());
69
70 LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
71
72 for (const auto &FFI : FunctionInfos)
73 emitFunctionInfo(FFI.first, FFI.second);
74}
75
76void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
77 const FunctionFaultInfos &FFI) {
78 MCStreamer &OS = *AP.OutStreamer;
79
80 LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
81 OS.emitSymbolValue(FnLabel, 8);
82
83 LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
84 OS.emitInt32(FFI.size());
85
86 OS.emitInt32(0); // Reserved
87
88 for (const auto &Fault : FFI) {
89 LLVM_DEBUG(dbgs() << WFMP << " fault type: "
90 << faultTypeToString(Fault.Kind) << "\n");
91 OS.emitInt32(Fault.Kind);
92
93 LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
94 << *Fault.FaultingOffsetExpr << "\n");
95 OS.emitValue(Fault.FaultingOffsetExpr, 4);
96
97 LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
98 << *Fault.HandlerOffsetExpr << "\n");
99 OS.emitValue(Fault.HandlerOffsetExpr, 4);
100 }
101}
102
104 switch (FT) {
105 default:
106 llvm_unreachable("unhandled fault type!");
108 return "FaultingLoad";
110 return "FaultingLoadStore";
112 return "FaultingStore";
113 }
114}
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static const int FaultMapVersion
Definition: FaultMaps.cpp:23
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:121
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:99
MCSymbol * CurrentFnSymForSize
The symbol used to represent the start of the current function for the purpose of calculating its siz...
Definition: AsmPrinter.h:130
void recordFaultingOp(FaultKind FaultTy, const MCSymbol *FaultingLabel, const MCSymbol *HandlerLabel)
Definition: FaultMaps.cpp:28
void serializeToFaultMapSection()
Definition: FaultMaps.cpp:45
FaultMaps(AsmPrinter &AP)
Definition: FaultMaps.cpp:26
static const char * faultTypeToString(FaultKind)
Definition: FaultMaps.cpp:103
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:610
Context object for machine code objects.
Definition: MCContext.h:76
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:450
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:201
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
MCSection * getFaultMapSection() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
Streaming machine code generation interface.
Definition: MCStreamer.h:212
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:180
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:184
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:423
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:134
void emitInt16(uint64_t Value)
Definition: MCStreamer.h:747
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
void emitInt32(uint64_t Value)
Definition: MCStreamer.h:748
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163