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
18using namespace llvm;
19
20#define DEBUG_TYPE "faultmaps"
21
22static const int FaultMapVersion = 1;
23const char *FaultMaps::WFMP = "Fault Maps: ";
24
25FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
26
27void FaultMaps::recordFaultingOp(FaultKind FaultTy,
28 const MCSymbol *FaultingLabel,
29 const MCSymbol *HandlerLabel) {
30 MCContext &OutContext = AP.OutStreamer->getContext();
31
32 const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
33 LHS: MCSymbolRefExpr::create(Symbol: FaultingLabel, Ctx&: OutContext),
34 RHS: MCSymbolRefExpr::create(Symbol: AP.CurrentFnSymForSize, Ctx&: OutContext), Ctx&: OutContext);
35
36 const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
37 LHS: MCSymbolRefExpr::create(Symbol: HandlerLabel, Ctx&: OutContext),
38 RHS: MCSymbolRefExpr::create(Symbol: AP.CurrentFnSymForSize, Ctx&: OutContext), Ctx&: OutContext);
39
40 FunctionInfos[AP.CurrentFnSym].emplace_back(args&: FaultTy, args&: FaultingOffset,
41 args&: HandlerOffset);
42}
43
44void FaultMaps::serializeToFaultMapSection() {
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 =
53 OutContext.getObjectFileInfo()->getFaultMapSection();
54 OS.switchSection(Section: FaultMapSection);
55
56 // Emit a dummy symbol to force section inclusion.
57 OS.emitLabel(Symbol: OutContext.getOrCreateSymbol(Name: Twine("__LLVM_FaultMaps")));
58
59 LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
60
61 // Header
62 OS.emitIntValue(Value: FaultMapVersion, Size: 1); // Version.
63 OS.emitIntValue(Value: 0, Size: 1); // Reserved.
64 OS.emitInt16(Value: 0); // Reserved.
65
66 LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
67 OS.emitInt32(Value: FunctionInfos.size());
68
69 LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
70
71 for (const auto &FFI : FunctionInfos)
72 emitFunctionInfo(FnLabel: FFI.first, FFI: 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(Sym: FnLabel, Size: 8);
81
82 LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
83 OS.emitInt32(Value: FFI.size());
84
85 OS.emitInt32(Value: 0); // Reserved
86
87 for (const auto &Fault : FFI) {
88 OS.emitInt32(Value: Fault.Kind);
89 OS.emitValue(Value: Fault.FaultingOffsetExpr, Size: 4);
90 OS.emitValue(Value: Fault.HandlerOffsetExpr, Size: 4);
91 }
92}
93