LLVM 24.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"
17
18using namespace llvm;
19
20#define DEBUG_TYPE "faultmaps"
21
22static const int FaultMapVersion = 1;
23const char *FaultMaps::WFMP = "Fault Maps: ";
24
26
28 const MCSymbol *FaultingLabel,
29 const MCSymbol *HandlerLabel) {
30 MCContext &OutContext = AP.OutStreamer->getContext();
31
32 const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
33 MCSymbolRefExpr::create(FaultingLabel, OutContext),
34 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
35
36 const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
37 MCSymbolRefExpr::create(HandlerLabel, OutContext),
38 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
39
40 FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
41 HandlerOffset);
42}
43
45 if (FunctionInfos.empty())
46 return;
47
48 MCContext &OutContext = AP.OutStreamer->getContext();
49 MCStreamer &OS = *AP.OutStreamer;
50
51 // Create the section.
52 MCSection *FaultMapSection =
54 OS.switchSection(FaultMapSection);
55
56 // Emit a dummy symbol to force section inclusion.
57 OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
58
59 LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
60
61 // Header
62 OS.emitIntValue(FaultMapVersion, 1); // Version.
63 OS.emitIntValue(0, 1); // Reserved.
64 OS.emitInt16(0); // Reserved.
65
66 LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
67 OS.emitInt32(FunctionInfos.size());
68
69 LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
70
71 for (const auto &FFI : FunctionInfos)
72 emitFunctionInfo(FFI.first, FFI.second);
73}
74
75void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
76 const FunctionFaultInfos &FFI) {
77 MCStreamer &OS = *AP.OutStreamer;
78
79 LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
80 OS.emitSymbolValue(FnLabel, 8);
81
82 LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
83 OS.emitInt32(FFI.size());
84
85 OS.emitInt32(0); // Reserved
86
87 for (const auto &Fault : FFI) {
88 OS.emitInt32(Fault.Kind);
89 OS.emitValue(Fault.FaultingOffsetExpr, 4);
90 OS.emitValue(Fault.HandlerOffsetExpr, 4);
91 }
92}
static const int FaultMapVersion
Definition FaultMaps.cpp:22
#define LLVM_DEBUG(...)
Definition Debug.h:119
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:94
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition AsmPrinter.h:109
LLVM_ABI void recordFaultingOp(FaultKind FaultTy, const MCSymbol *FaultingLabel, const MCSymbol *HandlerLabel)
Definition FaultMaps.cpp:27
LLVM_ABI void serializeToFaultMapSection()
Definition FaultMaps.cpp:44
LLVM_ABI FaultMaps(AsmPrinter &AP)
Definition FaultMaps.cpp:25
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:427
Context object for machine code objects.
Definition MCContext.h:84
const MCObjectFileInfo * getObjectFileInfo() const
Definition MCContext.h:413
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
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:580
Streaming machine code generation interface.
Definition MCStreamer.h:222
void emitSymbolValue(const MCSymbol *Sym, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
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.
void emitInt16(uint64_t Value)
Definition MCStreamer.h:768
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
void emitInt32(uint64_t Value)
Definition MCStreamer.h:769
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209