LLVM 20.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();
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) {
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(...)
Definition: Debug.h:106
static const int FaultMapVersion
Definition: FaultMaps.cpp:23
raw_pwrite_stream & OS
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:123
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:101
MCSymbol * CurrentFnSymForSize
The symbol used to represent the start of the current function for the purpose of calculating its siz...
Definition: AsmPrinter.h:132
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:622
Context object for machine code objects.
Definition: MCContext.h:83
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:416
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:212
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
MCSection * getFaultMapSection() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
Streaming machine code generation interface.
Definition: MCStreamer.h:213
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
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