blob: 73df002d04d5ccf3d14d9f15c25f78a674ebca26 [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/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000018#include "llvm/CodeGen/AsmPrinter.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000023#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MachineLocation.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/Dwarf.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/FormattedStream.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000035#include "llvm/Target/TargetFrameLowering.h"
36#include "llvm/Target/TargetLoweringObjectFile.h"
37#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetOptions.h"
39#include "llvm/Target/TargetRegisterInfo.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000040using namespace llvm;
41
Rafael Espindolaa6001792015-03-09 18:29:12 +000042DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
43 : EHStreamer(A), shouldEmitCFI(false) {}
44
45void DwarfCFIExceptionBase::markFunctionEnd() {
Amaury Sechet7067ad32016-02-26 20:30:37 +000046 endFragment();
Rafael Espindolaa6001792015-03-09 18:29:12 +000047
48 if (MMI->getLandingPads().empty())
49 return;
50
51 // Map all labels and get rid of any dead landing pads.
52 MMI->TidyLandingPads();
53}
54
Amaury Sechet7067ad32016-02-26 20:30:37 +000055void DwarfCFIExceptionBase::endFragment() {
56 if (shouldEmitCFI)
57 Asm->OutStreamer->EmitCFIEndProc();
58}
59
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000060DwarfCFIException::DwarfCFIException(AsmPrinter *A)
Rafael Espindolaa6001792015-03-09 18:29:12 +000061 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
Amaury Sechet7067ad32016-02-26 20:30:37 +000062 shouldEmitLSDA(false), forceEmitPersonality(false),
63 shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {}
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000064
65DwarfCFIException::~DwarfCFIException() {}
66
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000067/// endModule - Emit all exception information that should come after the
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000068/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000069void DwarfCFIException::endModule() {
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000070 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
Lang Hames9ff69c82015-04-24 19:11:51 +000071 Asm->OutStreamer->EmitCFISections(false, true);
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000072
Reid Kleckner5cc15692015-01-23 18:49:01 +000073 // SjLj uses this pass and it doesn't need this info.
74 if (!Asm->MAI->usesCFIForEH())
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000075 return;
76
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000077 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindola1fc5bf92011-04-29 15:09:53 +000078
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000079 unsigned PerEncoding = TLOF.getPersonalityEncoding();
80
Logan Chienc0029812014-05-30 16:48:56 +000081 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000082 return;
83
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000084 // Emit references to all used personality functions
Reid Klecknere00faf82015-08-31 20:02:16 +000085 for (const Function *Personality : MMI->getPersonalities()) {
86 if (!Personality)
Rafael Espindola697edc82011-04-29 14:48:51 +000087 continue;
Reid Klecknere00faf82015-08-31 20:02:16 +000088 MCSymbol *Sym = Asm->getSymbol(Personality);
Mehdi Amini5c0fa582015-07-16 06:04:17 +000089 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000090 }
91}
92
Amaury Sechet7067ad32016-02-26 20:30:37 +000093static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
94 return Asm->getCurExceptionSym();
95}
96
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000097void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000098 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Keno Fischeraff703a2015-07-14 19:22:51 +000099 const Function *F = MF->getFunction();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000100
101 // If any landing pads survive, we need an EH table.
Rafael Espindola697edc82011-04-29 14:48:51 +0000102 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000103
104 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +0000105 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
106 if (MoveType == AsmPrinter::CFI_M_EH ||
107 (MoveType == AsmPrinter::CFI_M_Debug &&
108 moveTypeModule == AsmPrinter::CFI_M_None))
109 moveTypeModule = MoveType;
110
111 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000112
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000113 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000114 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Keno Fischeraff703a2015-07-14 19:22:51 +0000115 const Function *Per = nullptr;
116 if (F->hasPersonalityFn())
117 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Rafael Espindola697edc82011-04-29 14:48:51 +0000118
Keno Fischeraff703a2015-07-14 19:22:51 +0000119 // Emit a personality function even when there are no landing pads
Amaury Sechet7067ad32016-02-26 20:30:37 +0000120 forceEmitPersonality =
Keno Fischeraff703a2015-07-14 19:22:51 +0000121 // ...if a personality function is explicitly specified
122 F->hasPersonalityFn() &&
123 // ... and it's not known to be a noop in the absence of invokes
124 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
125 // ... and we're not explicitly asked not to emit it
126 F->needsUnwindTableEntry();
127
128 shouldEmitPersonality =
129 (forceEmitPersonality ||
130 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
131 Per;
Rafael Espindola697edc82011-04-29 14:48:51 +0000132
133 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
134 shouldEmitLSDA = shouldEmitPersonality &&
135 LSDAEncoding != dwarf::DW_EH_PE_omit;
136
Rafael Espindolaa6001792015-03-09 18:29:12 +0000137 shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
Amaury Sechet7067ad32016-02-26 20:30:37 +0000138 beginFragment(&*MF->begin(), getExceptionSym);
139}
140
141void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
142 ExceptionSymbolProvider ESP) {
Rafael Espindolaa6001792015-03-09 18:29:12 +0000143 if (!shouldEmitCFI)
Rafael Espindola697edc82011-04-29 14:48:51 +0000144 return;
145
Lang Hames9ff69c82015-04-24 19:11:51 +0000146 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
Rafael Espindola697edc82011-04-29 14:48:51 +0000147
148 // Indicate personality routine, if any.
149 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000150 return;
151
Amaury Sechet7067ad32016-02-26 20:30:37 +0000152 auto *F = MBB->getParent()->getFunction();
153 auto *P = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
154 assert(P && "Expected personality function");
155
Keno Fischeraff703a2015-07-14 19:22:51 +0000156 // If we are forced to emit this personality, make sure to record
157 // it because it might not appear in any landingpad
158 if (forceEmitPersonality)
Amaury Sechet7067ad32016-02-26 20:30:37 +0000159 MMI->addPersonality(P);
Keno Fischeraff703a2015-07-14 19:22:51 +0000160
Amaury Sechet7067ad32016-02-26 20:30:37 +0000161 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
162 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000163 const MCSymbol *Sym =
Amaury Sechet7067ad32016-02-26 20:30:37 +0000164 TLOF.getCFIPersonalitySymbol(P, *Asm->Mang, Asm->TM, MMI);
Lang Hames9ff69c82015-04-24 19:11:51 +0000165 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000166
Rafael Espindola697edc82011-04-29 14:48:51 +0000167 // Provide LSDA information.
Amaury Sechet7067ad32016-02-26 20:30:37 +0000168 if (shouldEmitLSDA)
169 Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000170}
171
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000172/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000173///
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000174void DwarfCFIException::endFunction(const MachineFunction *) {
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000175 if (!shouldEmitPersonality)
176 return;
177
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000178 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000179}