blob: e08306b001fbfe88059f19820eca09eac7cf337d [file] [log] [blame]
Anton Korobeynikovb46ef572011-01-14 21:57:53 +00001//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing DWARF exception info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfException.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/StringExtras.h"
16#include "llvm/ADT/Twine.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000017#include "llvm/CodeGen/AsmPrinter.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000022#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000024#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/MC/MachineLocation.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/Dwarf.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/FormattedStream.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000034#include "llvm/Target/TargetFrameLowering.h"
35#include "llvm/Target/TargetLoweringObjectFile.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
38#include "llvm/Target/TargetRegisterInfo.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000039using namespace llvm;
40
Rafael Espindolaa6001792015-03-09 18:29:12 +000041DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
Joerg Sonnenberger7b837322017-01-02 18:05:27 +000042 : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
Rafael Espindolaa6001792015-03-09 18:29:12 +000043
44void DwarfCFIExceptionBase::markFunctionEnd() {
Amaury Sechet7067ad32016-02-26 20:30:37 +000045 endFragment();
Rafael Espindolaa6001792015-03-09 18:29:12 +000046
Rafael Espindolaa6001792015-03-09 18:29:12 +000047 // Map all labels and get rid of any dead landing pads.
Matthias Braund0ee66c2016-12-01 19:32:15 +000048 if (!Asm->MF->getLandingPads().empty()) {
49 MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
50 NonConstMF->tidyLandingPads();
51 }
Rafael Espindolaa6001792015-03-09 18:29:12 +000052}
53
Amaury Sechet7067ad32016-02-26 20:30:37 +000054void DwarfCFIExceptionBase::endFragment() {
55 if (shouldEmitCFI)
56 Asm->OutStreamer->EmitCFIEndProc();
57}
58
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000059DwarfCFIException::DwarfCFIException(AsmPrinter *A)
Rafael Espindolaa6001792015-03-09 18:29:12 +000060 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
Amaury Sechetb2055c52016-02-26 20:49:07 +000061 forceEmitPersonality(false), shouldEmitLSDA(false),
Joerg Sonnenberger7b837322017-01-02 18:05:27 +000062 shouldEmitMoves(false) {}
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000063
64DwarfCFIException::~DwarfCFIException() {}
65
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000066/// endModule - Emit all exception information that should come after the
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000067/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000068void DwarfCFIException::endModule() {
Reid Kleckner5cc15692015-01-23 18:49:01 +000069 // SjLj uses this pass and it doesn't need this info.
70 if (!Asm->MAI->usesCFIForEH())
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000071 return;
72
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000073 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindola1fc5bf92011-04-29 15:09:53 +000074
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000075 unsigned PerEncoding = TLOF.getPersonalityEncoding();
76
Logan Chienc0029812014-05-30 16:48:56 +000077 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000078 return;
79
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000080 // Emit references to all used personality functions
Reid Klecknere00faf82015-08-31 20:02:16 +000081 for (const Function *Personality : MMI->getPersonalities()) {
82 if (!Personality)
Rafael Espindola697edc82011-04-29 14:48:51 +000083 continue;
Reid Klecknere00faf82015-08-31 20:02:16 +000084 MCSymbol *Sym = Asm->getSymbol(Personality);
Mehdi Amini5c0fa582015-07-16 06:04:17 +000085 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000086 }
87}
88
Amaury Sechet7067ad32016-02-26 20:30:37 +000089static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
90 return Asm->getCurExceptionSym();
91}
92
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000093void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000094 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Keno Fischeraff703a2015-07-14 19:22:51 +000095 const Function *F = MF->getFunction();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000096
97 // If any landing pads survive, we need an EH table.
Matthias Braund0ee66c2016-12-01 19:32:15 +000098 bool hasLandingPads = !MF->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000099
100 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +0000101 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +0000102
103 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000104
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000105 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000106 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Keno Fischeraff703a2015-07-14 19:22:51 +0000107 const Function *Per = nullptr;
108 if (F->hasPersonalityFn())
109 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Rafael Espindola697edc82011-04-29 14:48:51 +0000110
Keno Fischeraff703a2015-07-14 19:22:51 +0000111 // Emit a personality function even when there are no landing pads
Amaury Sechet7067ad32016-02-26 20:30:37 +0000112 forceEmitPersonality =
Keno Fischeraff703a2015-07-14 19:22:51 +0000113 // ...if a personality function is explicitly specified
114 F->hasPersonalityFn() &&
115 // ... and it's not known to be a noop in the absence of invokes
116 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
117 // ... and we're not explicitly asked not to emit it
118 F->needsUnwindTableEntry();
119
120 shouldEmitPersonality =
121 (forceEmitPersonality ||
122 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
123 Per;
Rafael Espindola697edc82011-04-29 14:48:51 +0000124
125 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
126 shouldEmitLSDA = shouldEmitPersonality &&
127 LSDAEncoding != dwarf::DW_EH_PE_omit;
128
Saleem Abdulrasool467269a2016-07-15 21:10:29 +0000129 shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() &&
130 (shouldEmitPersonality || shouldEmitMoves);
Amaury Sechet7067ad32016-02-26 20:30:37 +0000131 beginFragment(&*MF->begin(), getExceptionSym);
132}
133
134void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
135 ExceptionSymbolProvider ESP) {
Rafael Espindolaa6001792015-03-09 18:29:12 +0000136 if (!shouldEmitCFI)
Rafael Espindola697edc82011-04-29 14:48:51 +0000137 return;
138
Joerg Sonnenberger7b837322017-01-02 18:05:27 +0000139 if (!hasEmittedCFISections) {
Joerg Sonnenberger83963992017-01-05 20:55:28 +0000140 if (Asm->needsOnlyDebugCFIMoves())
Joerg Sonnenberger7b837322017-01-02 18:05:27 +0000141 Asm->OutStreamer->EmitCFISections(false, true);
142 hasEmittedCFISections = true;
143 }
144
Lang Hames9ff69c82015-04-24 19:11:51 +0000145 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
Rafael Espindola697edc82011-04-29 14:48:51 +0000146
147 // Indicate personality routine, if any.
148 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000149 return;
150
Amaury Sechet7067ad32016-02-26 20:30:37 +0000151 auto *F = MBB->getParent()->getFunction();
152 auto *P = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
153 assert(P && "Expected personality function");
154
Keno Fischeraff703a2015-07-14 19:22:51 +0000155 // If we are forced to emit this personality, make sure to record
156 // it because it might not appear in any landingpad
157 if (forceEmitPersonality)
Amaury Sechet7067ad32016-02-26 20:30:37 +0000158 MMI->addPersonality(P);
Keno Fischeraff703a2015-07-14 19:22:51 +0000159
Amaury Sechet7067ad32016-02-26 20:30:37 +0000160 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
161 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Eric Christopher4367c7f2016-09-16 07:33:15 +0000162 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
Lang Hames9ff69c82015-04-24 19:11:51 +0000163 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000164
Rafael Espindola697edc82011-04-29 14:48:51 +0000165 // Provide LSDA information.
Amaury Sechet7067ad32016-02-26 20:30:37 +0000166 if (shouldEmitLSDA)
167 Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000168}
169
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000170/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000171///
Matthias Braund0ee66c2016-12-01 19:32:15 +0000172void DwarfCFIException::endFunction(const MachineFunction *MF) {
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000173 if (!shouldEmitPersonality)
174 return;
175
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000176 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000177}