LLVM  14.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 
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "faultmaps"
22 
23 static const int FaultMapVersion = 1;
24 const 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 =
54  OutContext.getObjectFileInfo()->getFaultMapSection();
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 
76 void 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 (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 }
AsmPrinter.h
llvm::MCContext::getObjectFileInfo
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:427
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::AsmPrinter::CurrentFnSymForSize
MCSymbol * CurrentFnSymForSize
The symbol used to represent the start of the current function for the purpose of calculating its siz...
Definition: AsmPrinter.h:127
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
ErrorHandling.h
llvm::MCStreamer::emitValue
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:177
MCObjectFileInfo.h
llvm::MCStreamer::emitInt32
void emitInt32(uint64_t Value)
Definition: MCStreamer.h:707
llvm::FaultMaps::FaultingLoadStore
@ FaultingLoadStore
Definition: FaultMaps.h:27
llvm::MCObjectFileInfo::getFaultMapSection
MCSection * getFaultMapSection() const
Definition: MCObjectFileInfo.h:346
llvm::FaultMaps::serializeToFaultMapSection
void serializeToFaultMapSection()
Definition: FaultMaps.cpp:45
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
FaultMapVersion
static const int FaultMapVersion
Definition: FaultMaps.cpp:23
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:199
llvm::MCContext::getOrCreateSymbol
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:191
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:96
Twine.h
MCContext.h
llvm::MCStreamer::emitLabel
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:415
llvm::MCStreamer::emitSymbolValue
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:181
llvm::AsmPrinter::CurrentFnSym
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:118
llvm::ClrHandlerType::Fault
@ Fault
llvm::MCStreamer::emitInt16
void emitInt16(uint64_t Value)
Definition: MCStreamer.h:706
llvm::FaultMaps::FaultingStore
@ FaultingStore
Definition: FaultMaps.h:28
llvm::MCStreamer::emitIntValue
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:133
llvm::MCBinaryExpr::createSub
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:609
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::FaultMaps::FaultingLoad
@ FaultingLoad
Definition: FaultMaps.h:26
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:81
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:385
llvm::FaultMaps::FaultKind
FaultKind
Definition: FaultMaps.h:25
MCStreamer.h
FaultMaps.h
llvm::FaultMaps::faultTypeToString
static const char * faultTypeToString(FaultKind)
Definition: FaultMaps.cpp:103
llvm::MCStreamer::SwitchSection
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:1212
MCExpr.h
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Debug.h
llvm::FaultMaps::FaultMaps
FaultMaps(AsmPrinter &AP)
Definition: FaultMaps.cpp:26
llvm::FaultMaps::recordFaultingOp
void recordFaultingOp(FaultKind FaultTy, const MCSymbol *FaultingLabel, const MCSymbol *HandlerLabel)
Definition: FaultMaps.cpp:28