FaultMaps.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #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"
  17. using namespace llvm;
  18. #define DEBUG_TYPE "faultmaps"
  19. static const int FaultMapVersion = 1;
  20. const char *FaultMaps::WFMP = "Fault Maps: ";
  21. FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
  22. void FaultMaps::recordFaultingOp(FaultKind FaultTy,
  23. const MCSymbol *FaultingLabel,
  24. const MCSymbol *HandlerLabel) {
  25. MCContext &OutContext = AP.OutStreamer->getContext();
  26. const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
  27. MCSymbolRefExpr::create(FaultingLabel, OutContext),
  28. MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
  29. const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
  30. MCSymbolRefExpr::create(HandlerLabel, OutContext),
  31. MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
  32. FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
  33. HandlerOffset);
  34. }
  35. void FaultMaps::serializeToFaultMapSection() {
  36. if (FunctionInfos.empty())
  37. return;
  38. MCContext &OutContext = AP.OutStreamer->getContext();
  39. MCStreamer &OS = *AP.OutStreamer;
  40. // Create the section.
  41. MCSection *FaultMapSection =
  42. OutContext.getObjectFileInfo()->getFaultMapSection();
  43. OS.switchSection(FaultMapSection);
  44. // Emit a dummy symbol to force section inclusion.
  45. OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
  46. LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
  47. // Header
  48. OS.emitIntValue(FaultMapVersion, 1); // Version.
  49. OS.emitIntValue(0, 1); // Reserved.
  50. OS.emitInt16(0); // Reserved.
  51. LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
  52. OS.emitInt32(FunctionInfos.size());
  53. LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
  54. for (const auto &FFI : FunctionInfos)
  55. emitFunctionInfo(FFI.first, FFI.second);
  56. }
  57. void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
  58. const FunctionFaultInfos &FFI) {
  59. MCStreamer &OS = *AP.OutStreamer;
  60. LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
  61. OS.emitSymbolValue(FnLabel, 8);
  62. LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
  63. OS.emitInt32(FFI.size());
  64. OS.emitInt32(0); // Reserved
  65. for (const auto &Fault : FFI) {
  66. LLVM_DEBUG(dbgs() << WFMP << " fault type: "
  67. << faultTypeToString(Fault.Kind) << "\n");
  68. OS.emitInt32(Fault.Kind);
  69. LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
  70. << *Fault.FaultingOffsetExpr << "\n");
  71. OS.emitValue(Fault.FaultingOffsetExpr, 4);
  72. LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
  73. << *Fault.HandlerOffsetExpr << "\n");
  74. OS.emitValue(Fault.HandlerOffsetExpr, 4);
  75. }
  76. }
  77. const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
  78. switch (FT) {
  79. default:
  80. llvm_unreachable("unhandled fault type!");
  81. case FaultMaps::FaultingLoad:
  82. return "FaultingLoad";
  83. case FaultMaps::FaultingLoadStore:
  84. return "FaultingLoadStore";
  85. case FaultMaps::FaultingStore:
  86. return "FaultingStore";
  87. }
  88. }