blob: 5cc063e6c9551a5904e180449aabb563c2527b56 [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)
42 : EHStreamer(A), shouldEmitCFI(false) {}
43
44void DwarfCFIExceptionBase::markFunctionEnd() {
Amaury Sechet7067ad32016-02-26 20:30:37 +000045 endFragment();
Rafael Espindolaa6001792015-03-09 18:29:12 +000046
47 if (MMI->getLandingPads().empty())
48 return;
49
50 // Map all labels and get rid of any dead landing pads.
51 MMI->TidyLandingPads();
52}
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),
Amaury Sechet7067ad32016-02-26 20:30:37 +000062 shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {}
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() {
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000069 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
Lang Hames9ff69c82015-04-24 19:11:51 +000070 Asm->OutStreamer->EmitCFISections(false, true);
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000071
Reid Kleckner5cc15692015-01-23 18:49:01 +000072 // SjLj uses this pass and it doesn't need this info.
73 if (!Asm->MAI->usesCFIForEH())
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000074 return;
75
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000076 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindola1fc5bf92011-04-29 15:09:53 +000077
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000078 unsigned PerEncoding = TLOF.getPersonalityEncoding();
79
Logan Chienc0029812014-05-30 16:48:56 +000080 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000081 return;
82
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000083 // Emit references to all used personality functions
Reid Klecknere00faf82015-08-31 20:02:16 +000084 for (const Function *Personality : MMI->getPersonalities()) {
85 if (!Personality)
Rafael Espindola697edc82011-04-29 14:48:51 +000086 continue;
Reid Klecknere00faf82015-08-31 20:02:16 +000087 MCSymbol *Sym = Asm->getSymbol(Personality);
Mehdi Amini5c0fa582015-07-16 06:04:17 +000088 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000089 }
90}
91
Amaury Sechet7067ad32016-02-26 20:30:37 +000092static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
93 return Asm->getCurExceptionSym();
94}
95
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000096void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000097 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Keno Fischeraff703a2015-07-14 19:22:51 +000098 const Function *F = MF->getFunction();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000099
100 // If any landing pads survive, we need an EH table.
Rafael Espindola697edc82011-04-29 14:48:51 +0000101 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000102
103 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +0000104 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
105 if (MoveType == AsmPrinter::CFI_M_EH ||
106 (MoveType == AsmPrinter::CFI_M_Debug &&
107 moveTypeModule == AsmPrinter::CFI_M_None))
108 moveTypeModule = MoveType;
109
110 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000111
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000112 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000113 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Keno Fischeraff703a2015-07-14 19:22:51 +0000114 const Function *Per = nullptr;
115 if (F->hasPersonalityFn())
116 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Rafael Espindola697edc82011-04-29 14:48:51 +0000117
Keno Fischeraff703a2015-07-14 19:22:51 +0000118 // Emit a personality function even when there are no landing pads
Amaury Sechet7067ad32016-02-26 20:30:37 +0000119 forceEmitPersonality =
Keno Fischeraff703a2015-07-14 19:22:51 +0000120 // ...if a personality function is explicitly specified
121 F->hasPersonalityFn() &&
122 // ... and it's not known to be a noop in the absence of invokes
123 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
124 // ... and we're not explicitly asked not to emit it
125 F->needsUnwindTableEntry();
126
127 shouldEmitPersonality =
128 (forceEmitPersonality ||
129 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
130 Per;
Rafael Espindola697edc82011-04-29 14:48:51 +0000131
132 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
133 shouldEmitLSDA = shouldEmitPersonality &&
134 LSDAEncoding != dwarf::DW_EH_PE_omit;
135
Rafael Espindolaa6001792015-03-09 18:29:12 +0000136 shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
Amaury Sechet7067ad32016-02-26 20:30:37 +0000137 beginFragment(&*MF->begin(), getExceptionSym);
138}
139
140void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
141 ExceptionSymbolProvider ESP) {
Rafael Espindolaa6001792015-03-09 18:29:12 +0000142 if (!shouldEmitCFI)
Rafael Espindola697edc82011-04-29 14:48:51 +0000143 return;
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();
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000162 const MCSymbol *Sym =
Amaury Sechet7067ad32016-02-26 20:30:37 +0000163 TLOF.getCFIPersonalitySymbol(P, *Asm->Mang, Asm->TM, MMI);
Lang Hames9ff69c82015-04-24 19:11:51 +0000164 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000165
Rafael Espindola697edc82011-04-29 14:48:51 +0000166 // Provide LSDA information.
Amaury Sechet7067ad32016-02-26 20:30:37 +0000167 if (shouldEmitLSDA)
168 Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000169}
170
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000171/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000172///
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000173void DwarfCFIException::endFunction(const MachineFunction *) {
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000174 if (!shouldEmitPersonality)
175 return;
176
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000177 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000178}