Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/FaultMaps.cpp
35233 views
//===- FaultMaps.cpp ------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/CodeGen/FaultMaps.h"9#include "llvm/ADT/Twine.h"10#include "llvm/CodeGen/AsmPrinter.h"11#include "llvm/MC/MCContext.h"12#include "llvm/MC/MCExpr.h"13#include "llvm/MC/MCObjectFileInfo.h"14#include "llvm/MC/MCStreamer.h"15#include "llvm/Support/Debug.h"16#include "llvm/Support/ErrorHandling.h"1718using namespace llvm;1920#define DEBUG_TYPE "faultmaps"2122static const int FaultMapVersion = 1;23const char *FaultMaps::WFMP = "Fault Maps: ";2425FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}2627void FaultMaps::recordFaultingOp(FaultKind FaultTy,28const MCSymbol *FaultingLabel,29const MCSymbol *HandlerLabel) {30MCContext &OutContext = AP.OutStreamer->getContext();3132const MCExpr *FaultingOffset = MCBinaryExpr::createSub(33MCSymbolRefExpr::create(FaultingLabel, OutContext),34MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);3536const MCExpr *HandlerOffset = MCBinaryExpr::createSub(37MCSymbolRefExpr::create(HandlerLabel, OutContext),38MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);3940FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,41HandlerOffset);42}4344void FaultMaps::serializeToFaultMapSection() {45if (FunctionInfos.empty())46return;4748MCContext &OutContext = AP.OutStreamer->getContext();49MCStreamer &OS = *AP.OutStreamer;5051// Create the section.52MCSection *FaultMapSection =53OutContext.getObjectFileInfo()->getFaultMapSection();54OS.switchSection(FaultMapSection);5556// Emit a dummy symbol to force section inclusion.57OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));5859LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");6061// Header62OS.emitIntValue(FaultMapVersion, 1); // Version.63OS.emitIntValue(0, 1); // Reserved.64OS.emitInt16(0); // Reserved.6566LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");67OS.emitInt32(FunctionInfos.size());6869LLVM_DEBUG(dbgs() << WFMP << "functions:\n");7071for (const auto &FFI : FunctionInfos)72emitFunctionInfo(FFI.first, FFI.second);73}7475void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,76const FunctionFaultInfos &FFI) {77MCStreamer &OS = *AP.OutStreamer;7879LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");80OS.emitSymbolValue(FnLabel, 8);8182LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");83OS.emitInt32(FFI.size());8485OS.emitInt32(0); // Reserved8687for (const auto &Fault : FFI) {88LLVM_DEBUG(dbgs() << WFMP << " fault type: "89<< faultTypeToString(Fault.Kind) << "\n");90OS.emitInt32(Fault.Kind);9192LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "93<< *Fault.FaultingOffsetExpr << "\n");94OS.emitValue(Fault.FaultingOffsetExpr, 4);9596LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "97<< *Fault.HandlerOffsetExpr << "\n");98OS.emitValue(Fault.HandlerOffsetExpr, 4);99}100}101102const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {103switch (FT) {104default:105llvm_unreachable("unhandled fault type!");106case FaultMaps::FaultingLoad:107return "FaultingLoad";108case FaultMaps::FaultingLoadStore:109return "FaultingLoadStore";110case FaultMaps::FaultingStore:111return "FaultingStore";112}113}114115116