DwarfCFIException.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
  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. // This file contains support for writing DWARF exception info into asm files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "DwarfException.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/BinaryFormat/Dwarf.h"
  15. #include "llvm/CodeGen/AsmPrinter.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineModuleInfo.h"
  18. #include "llvm/IR/DataLayout.h"
  19. #include "llvm/IR/Mangler.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/MC/MCAsmInfo.h"
  22. #include "llvm/MC/MCContext.h"
  23. #include "llvm/MC/MCExpr.h"
  24. #include "llvm/MC/MCSection.h"
  25. #include "llvm/MC/MCStreamer.h"
  26. #include "llvm/MC/MCSymbol.h"
  27. #include "llvm/MC/MachineLocation.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/FormattedStream.h"
  30. #include "llvm/Target/TargetLoweringObjectFile.h"
  31. #include "llvm/Target/TargetOptions.h"
  32. using namespace llvm;
  33. DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
  34. : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
  35. void DwarfCFIExceptionBase::markFunctionEnd() {
  36. endFragment();
  37. // Map all labels and get rid of any dead landing pads.
  38. if (!Asm->MF->getLandingPads().empty()) {
  39. MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
  40. NonConstMF->tidyLandingPads();
  41. }
  42. }
  43. void DwarfCFIExceptionBase::endFragment() {
  44. if (shouldEmitCFI)
  45. Asm->OutStreamer->EmitCFIEndProc();
  46. }
  47. DwarfCFIException::DwarfCFIException(AsmPrinter *A)
  48. : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
  49. forceEmitPersonality(false), shouldEmitLSDA(false),
  50. shouldEmitMoves(false) {}
  51. DwarfCFIException::~DwarfCFIException() {}
  52. /// endModule - Emit all exception information that should come after the
  53. /// content.
  54. void DwarfCFIException::endModule() {
  55. // SjLj uses this pass and it doesn't need this info.
  56. if (!Asm->MAI->usesCFIForEH())
  57. return;
  58. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  59. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  60. if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
  61. return;
  62. // Emit references to all used personality functions
  63. for (const Function *Personality : MMI->getPersonalities()) {
  64. if (!Personality)
  65. continue;
  66. MCSymbol *Sym = Asm->getSymbol(Personality);
  67. TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
  68. }
  69. }
  70. static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
  71. return Asm->getCurExceptionSym();
  72. }
  73. void DwarfCFIException::beginFunction(const MachineFunction *MF) {
  74. shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
  75. const Function &F = MF->getFunction();
  76. // If any landing pads survive, we need an EH table.
  77. bool hasLandingPads = !MF->getLandingPads().empty();
  78. // See if we need frame move info.
  79. AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
  80. shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
  81. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  82. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  83. const Function *Per = nullptr;
  84. if (F.hasPersonalityFn())
  85. Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
  86. // Emit a personality function even when there are no landing pads
  87. forceEmitPersonality =
  88. // ...if a personality function is explicitly specified
  89. F.hasPersonalityFn() &&
  90. // ... and it's not known to be a noop in the absence of invokes
  91. !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
  92. // ... and we're not explicitly asked not to emit it
  93. F.needsUnwindTableEntry();
  94. shouldEmitPersonality =
  95. (forceEmitPersonality ||
  96. (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
  97. Per;
  98. unsigned LSDAEncoding = TLOF.getLSDAEncoding();
  99. shouldEmitLSDA = shouldEmitPersonality &&
  100. LSDAEncoding != dwarf::DW_EH_PE_omit;
  101. shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() &&
  102. (shouldEmitPersonality || shouldEmitMoves);
  103. beginFragment(&*MF->begin(), getExceptionSym);
  104. }
  105. void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
  106. ExceptionSymbolProvider ESP) {
  107. if (!shouldEmitCFI)
  108. return;
  109. if (!hasEmittedCFISections) {
  110. if (Asm->needsOnlyDebugCFIMoves())
  111. Asm->OutStreamer->EmitCFISections(false, true);
  112. hasEmittedCFISections = true;
  113. }
  114. Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
  115. // Indicate personality routine, if any.
  116. if (!shouldEmitPersonality)
  117. return;
  118. auto &F = MBB->getParent()->getFunction();
  119. auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
  120. assert(P && "Expected personality function");
  121. // If we are forced to emit this personality, make sure to record
  122. // it because it might not appear in any landingpad
  123. if (forceEmitPersonality)
  124. MMI->addPersonality(P);
  125. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  126. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  127. const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
  128. Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
  129. // Provide LSDA information.
  130. if (shouldEmitLSDA)
  131. Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
  132. }
  133. /// endFunction - Gather and emit post-function exception information.
  134. ///
  135. void DwarfCFIException::endFunction(const MachineFunction *MF) {
  136. if (!shouldEmitPersonality)
  137. return;
  138. emitExceptionTable();
  139. }