DwarfCFIException.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/BinaryFormat/Dwarf.h"
  14. #include "llvm/CodeGen/AsmPrinter.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineModuleInfo.h"
  17. #include "llvm/MC/MCAsmInfo.h"
  18. #include "llvm/MC/MCContext.h"
  19. #include "llvm/MC/MCStreamer.h"
  20. #include "llvm/Target/TargetLoweringObjectFile.h"
  21. #include "llvm/Target/TargetMachine.h"
  22. #include "llvm/Target/TargetOptions.h"
  23. using namespace llvm;
  24. DwarfCFIException::DwarfCFIException(AsmPrinter *A) : EHStreamer(A) {}
  25. DwarfCFIException::~DwarfCFIException() = default;
  26. void DwarfCFIException::addPersonality(const GlobalValue *Personality) {
  27. if (!llvm::is_contained(Personalities, Personality))
  28. Personalities.push_back(Personality);
  29. }
  30. /// endModule - Emit all exception information that should come after the
  31. /// content.
  32. void DwarfCFIException::endModule() {
  33. // SjLj uses this pass and it doesn't need this info.
  34. if (!Asm->MAI->usesCFIForEH())
  35. return;
  36. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  37. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  38. if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
  39. return;
  40. // Emit indirect reference table for all used personality functions
  41. for (const GlobalValue *Personality : Personalities) {
  42. MCSymbol *Sym = Asm->getSymbol(Personality);
  43. TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
  44. }
  45. Personalities.clear();
  46. }
  47. void DwarfCFIException::beginFunction(const MachineFunction *MF) {
  48. shouldEmitPersonality = shouldEmitLSDA = false;
  49. const Function &F = MF->getFunction();
  50. // If any landing pads survive, we need an EH table.
  51. bool hasLandingPads = !MF->getLandingPads().empty();
  52. // See if we need frame move info.
  53. bool shouldEmitMoves =
  54. Asm->getFunctionCFISectionType(*MF) != AsmPrinter::CFISection::None;
  55. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  56. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  57. const GlobalValue *Per = nullptr;
  58. if (F.hasPersonalityFn())
  59. Per = dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
  60. // Emit a personality function even when there are no landing pads
  61. forceEmitPersonality =
  62. // ...if a personality function is explicitly specified
  63. F.hasPersonalityFn() &&
  64. // ... and it's not known to be a noop in the absence of invokes
  65. !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
  66. // ... and we're not explicitly asked not to emit it
  67. F.needsUnwindTableEntry();
  68. shouldEmitPersonality =
  69. (forceEmitPersonality ||
  70. (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
  71. Per;
  72. unsigned LSDAEncoding = TLOF.getLSDAEncoding();
  73. shouldEmitLSDA = shouldEmitPersonality &&
  74. LSDAEncoding != dwarf::DW_EH_PE_omit;
  75. const MCAsmInfo &MAI = *MF->getMMI().getContext().getAsmInfo();
  76. if (MAI.getExceptionHandlingType() != ExceptionHandling::None)
  77. shouldEmitCFI =
  78. MAI.usesCFIForEH() && (shouldEmitPersonality || shouldEmitMoves);
  79. else
  80. shouldEmitCFI = Asm->needsCFIForDebug() && shouldEmitMoves;
  81. }
  82. void DwarfCFIException::beginBasicBlockSection(const MachineBasicBlock &MBB) {
  83. if (!shouldEmitCFI)
  84. return;
  85. if (!hasEmittedCFISections) {
  86. AsmPrinter::CFISection CFISecType = Asm->getModuleCFISectionType();
  87. // If we don't say anything it implies `.cfi_sections .eh_frame`, so we
  88. // chose not to be verbose in that case. And with `ForceDwarfFrameSection`,
  89. // we should always emit .debug_frame.
  90. if (CFISecType == AsmPrinter::CFISection::Debug ||
  91. Asm->TM.Options.ForceDwarfFrameSection)
  92. Asm->OutStreamer->emitCFISections(
  93. CFISecType == AsmPrinter::CFISection::EH, true);
  94. hasEmittedCFISections = true;
  95. }
  96. Asm->OutStreamer->emitCFIStartProc(/*IsSimple=*/false);
  97. // Indicate personality routine, if any.
  98. if (!shouldEmitPersonality)
  99. return;
  100. auto &F = MBB.getParent()->getFunction();
  101. auto *P = dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
  102. assert(P && "Expected personality function");
  103. // Record the personality function.
  104. addPersonality(P);
  105. const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
  106. unsigned PerEncoding = TLOF.getPersonalityEncoding();
  107. const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
  108. Asm->OutStreamer->emitCFIPersonality(Sym, PerEncoding);
  109. // Provide LSDA information.
  110. if (shouldEmitLSDA)
  111. Asm->OutStreamer->emitCFILsda(Asm->getMBBExceptionSym(MBB),
  112. TLOF.getLSDAEncoding());
  113. }
  114. void DwarfCFIException::endBasicBlockSection(const MachineBasicBlock &MBB) {
  115. if (shouldEmitCFI)
  116. Asm->OutStreamer->emitCFIEndProc();
  117. }
  118. /// endFunction - Gather and emit post-function exception information.
  119. ///
  120. void DwarfCFIException::endFunction(const MachineFunction *MF) {
  121. if (!shouldEmitPersonality)
  122. return;
  123. emitExceptionTable();
  124. }