2015-06-15 18:44:08 +00:00
|
|
|
//===---------------------------- FaultMaps.cpp ---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/FaultMaps.h"
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "faultmaps"
|
|
|
|
|
|
|
|
static const int FaultMapVersion = 1;
|
|
|
|
const char *FaultMaps::WFMP = "Fault Maps: ";
|
|
|
|
|
|
|
|
FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
|
|
|
|
|
2015-06-15 19:29:44 +00:00
|
|
|
void FaultMaps::recordFaultingOp(FaultKind FaultTy,
|
2015-06-15 18:44:08 +00:00
|
|
|
const MCSymbol *HandlerLabel) {
|
|
|
|
MCContext &OutContext = AP.OutStreamer->getContext();
|
|
|
|
MCSymbol *FaultingLabel = OutContext.createTempSymbol();
|
|
|
|
|
|
|
|
AP.OutStreamer->EmitLabel(FaultingLabel);
|
|
|
|
|
|
|
|
const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
|
|
|
|
MCSymbolRefExpr::create(FaultingLabel, OutContext),
|
|
|
|
MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
|
|
|
|
|
|
|
|
const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
|
|
|
|
MCSymbolRefExpr::create(HandlerLabel, OutContext),
|
|
|
|
MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
|
|
|
|
|
|
|
|
FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
|
|
|
|
HandlerOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultMaps::serializeToFaultMapSection() {
|
|
|
|
if (FunctionInfos.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MCContext &OutContext = AP.OutStreamer->getContext();
|
|
|
|
MCStreamer &OS = *AP.OutStreamer;
|
|
|
|
|
|
|
|
// Create the section.
|
|
|
|
MCSection *FaultMapSection =
|
|
|
|
OutContext.getObjectFileInfo()->getFaultMapSection();
|
|
|
|
OS.SwitchSection(FaultMapSection);
|
|
|
|
|
|
|
|
// Emit a dummy symbol to force section inclusion.
|
|
|
|
OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "********** Fault Map Output **********\n");
|
|
|
|
|
|
|
|
// Header
|
|
|
|
OS.EmitIntValue(FaultMapVersion, 1); // Version.
|
|
|
|
OS.EmitIntValue(0, 1); // Reserved.
|
|
|
|
OS.EmitIntValue(0, 2); // Reserved.
|
|
|
|
|
|
|
|
DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
|
|
|
|
OS.EmitIntValue(FunctionInfos.size(), 4);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << WFMP << "functions:\n");
|
|
|
|
|
|
|
|
for (const auto &FFI : FunctionInfos)
|
|
|
|
emitFunctionInfo(FFI.first, FFI.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
|
|
|
|
const FunctionFaultInfos &FFI) {
|
|
|
|
MCStreamer &OS = *AP.OutStreamer;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
|
|
|
|
OS.EmitSymbolValue(FnLabel, 8);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
|
|
|
|
OS.EmitIntValue(FFI.size(), 4);
|
|
|
|
|
|
|
|
OS.EmitIntValue(0, 4); // Reserved
|
|
|
|
|
|
|
|
for (auto &Fault : FFI) {
|
|
|
|
DEBUG(dbgs() << WFMP << " fault type: "
|
2015-06-15 19:29:44 +00:00
|
|
|
<< faultTypeToString(Fault.Kind) << "\n");
|
|
|
|
OS.EmitIntValue(Fault.Kind, 4);
|
2015-06-15 18:44:08 +00:00
|
|
|
|
|
|
|
DEBUG(dbgs() << WFMP << " faulting PC offset: "
|
|
|
|
<< *Fault.FaultingOffsetExpr << "\n");
|
|
|
|
OS.EmitValue(Fault.FaultingOffsetExpr, 4);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << WFMP << " fault handler PC offset: "
|
|
|
|
<< *Fault.HandlerOffsetExpr << "\n");
|
|
|
|
OS.EmitValue(Fault.HandlerOffsetExpr, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-15 19:29:44 +00:00
|
|
|
const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
|
2015-06-15 18:44:08 +00:00
|
|
|
switch (FT) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unhandled fault type!");
|
|
|
|
|
|
|
|
case FaultMaps::FaultingLoad:
|
|
|
|
return "FaultingLoad";
|
|
|
|
}
|
|
|
|
}
|