DwarfCFIException.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/TargetMachine.h"
  32. #include "llvm/Target/TargetOptions.h"
  33. using namespace llvm;
  34. DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A) : EHStreamer(A) {}
  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 && !Asm->MF->hasBBSections())
  45. Asm->OutStreamer->emitCFIEndProc();
  46. }
  47. DwarfCFIException::DwarfCFIException(AsmPrinter *A)
  48. : DwarfCFIExceptionBase(A) {}
  49. DwarfCFIException::~DwarfCFIException() {}
  50. /// endModule - Emit all exception information that should come after the
  51. /// content.
  52. void DwarfCFIException::endModule() {
  53. // SjLj uses this pass and it doesn't need this info.
  54. if (!Asm->MAI->usesCFIForEH())
  55. return;
  56. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  57. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  58. if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
  59. return;
  60. // Emit references to all used personality functions
  61. for (const Function *Personality : MMI->getPersonalities()) {
  62. if (!Personality)
  63. continue;
  64. MCSymbol *Sym = Asm->getSymbol(Personality);
  65. TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
  66. }
  67. }
  68. static MCSymbol *getExceptionSym(AsmPrinter *Asm,
  69. const MachineBasicBlock *MBB) {
  70. return Asm->getMBBExceptionSym(*MBB);
  71. }
  72. void DwarfCFIException::beginFunction(const MachineFunction *MF) {
  73. shouldEmitPersonality = shouldEmitLSDA = false;
  74. const Function &F = MF->getFunction();
  75. // If any landing pads survive, we need an EH table.
  76. bool hasLandingPads = !MF->getLandingPads().empty();
  77. // See if we need frame move info.
  78. bool shouldEmitMoves =
  79. Asm->getFunctionCFISectionType(*MF) != AsmPrinter::CFISection::None;
  80. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  81. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  82. const Function *Per = nullptr;
  83. if (F.hasPersonalityFn())
  84. Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
  85. // Emit a personality function even when there are no landing pads
  86. forceEmitPersonality =
  87. // ...if a personality function is explicitly specified
  88. F.hasPersonalityFn() &&
  89. // ... and it's not known to be a noop in the absence of invokes
  90. !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
  91. // ... and we're not explicitly asked not to emit it
  92. F.needsUnwindTableEntry();
  93. shouldEmitPersonality =
  94. (forceEmitPersonality ||
  95. (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
  96. Per;
  97. unsigned LSDAEncoding = TLOF.getLSDAEncoding();
  98. shouldEmitLSDA = shouldEmitPersonality &&
  99. LSDAEncoding != dwarf::DW_EH_PE_omit;
  100. const MCAsmInfo &MAI = *MF->getMMI().getContext().getAsmInfo();
  101. if (MAI.getExceptionHandlingType() != ExceptionHandling::None)
  102. shouldEmitCFI =
  103. MAI.usesCFIForEH() && (shouldEmitPersonality || shouldEmitMoves);
  104. else
  105. shouldEmitCFI = Asm->needsCFIForDebug() && shouldEmitMoves;
  106. beginFragment(&*MF->begin(), getExceptionSym);
  107. }
  108. void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
  109. ExceptionSymbolProvider ESP) {
  110. if (!shouldEmitCFI)
  111. return;
  112. if (!hasEmittedCFISections) {
  113. AsmPrinter::CFISection CFISecType = Asm->getModuleCFISectionType();
  114. // If we don't say anything it implies `.cfi_sections .eh_frame`, so we
  115. // chose not to be verbose in that case. And with `ForceDwarfFrameSection`,
  116. // we should always emit .debug_frame.
  117. if (CFISecType == AsmPrinter::CFISection::Debug ||
  118. Asm->TM.Options.ForceDwarfFrameSection)
  119. Asm->OutStreamer->emitCFISections(
  120. CFISecType == AsmPrinter::CFISection::EH, true);
  121. hasEmittedCFISections = true;
  122. }
  123. Asm->OutStreamer->emitCFIStartProc(/*IsSimple=*/false);
  124. // Indicate personality routine, if any.
  125. if (!shouldEmitPersonality)
  126. return;
  127. auto &F = MBB->getParent()->getFunction();
  128. auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
  129. assert(P && "Expected personality function");
  130. // If we are forced to emit this personality, make sure to record
  131. // it because it might not appear in any landingpad
  132. if (forceEmitPersonality)
  133. MMI->addPersonality(P);
  134. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  135. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  136. const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
  137. Asm->OutStreamer->emitCFIPersonality(Sym, PerEncoding);
  138. // Provide LSDA information.
  139. if (shouldEmitLSDA)
  140. Asm->OutStreamer->emitCFILsda(ESP(Asm, MBB), TLOF.getLSDAEncoding());
  141. }
  142. /// endFunction - Gather and emit post-function exception information.
  143. ///
  144. void DwarfCFIException::endFunction(const MachineFunction *MF) {
  145. if (!shouldEmitPersonality)
  146. return;
  147. emitExceptionTable();
  148. }
  149. void DwarfCFIException::beginBasicBlock(const MachineBasicBlock &MBB) {
  150. beginFragment(&MBB, getExceptionSym);
  151. }
  152. void DwarfCFIException::endBasicBlock(const MachineBasicBlock &MBB) {
  153. if (shouldEmitCFI)
  154. Asm->OutStreamer->emitCFIEndProc();
  155. }