blob: 6665c16159a0ee482f44772456813d8433c0877a [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() {
46 if (shouldEmitCFI)
Lang Hames9ff69c82015-04-24 19:11:51 +000047 Asm->OutStreamer->EmitCFIEndProc();
Rafael Espindolaa6001792015-03-09 18:29:12 +000048
49 if (MMI->getLandingPads().empty())
50 return;
51
52 // Map all labels and get rid of any dead landing pads.
53 MMI->TidyLandingPads();
54}
55
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000056DwarfCFIException::DwarfCFIException(AsmPrinter *A)
Rafael Espindolaa6001792015-03-09 18:29:12 +000057 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
58 shouldEmitLSDA(false), shouldEmitMoves(false),
59 moveTypeModule(AsmPrinter::CFI_M_None) {}
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000060
61DwarfCFIException::~DwarfCFIException() {}
62
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000063/// endModule - Emit all exception information that should come after the
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000064/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000065void DwarfCFIException::endModule() {
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000066 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
Lang Hames9ff69c82015-04-24 19:11:51 +000067 Asm->OutStreamer->EmitCFISections(false, true);
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000068
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
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000089void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000090 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Keno Fischeraff703a2015-07-14 19:22:51 +000091 const Function *F = MF->getFunction();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000092
93 // If any landing pads survive, we need an EH table.
Rafael Espindola697edc82011-04-29 14:48:51 +000094 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000095
96 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000097 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
98 if (MoveType == AsmPrinter::CFI_M_EH ||
99 (MoveType == AsmPrinter::CFI_M_Debug &&
100 moveTypeModule == AsmPrinter::CFI_M_None))
101 moveTypeModule = MoveType;
102
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
112 bool forceEmitPersonality =
113 // ...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
Rafael Espindolaa6001792015-03-09 18:29:12 +0000129 shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
130 if (!shouldEmitCFI)
Rafael Espindola697edc82011-04-29 14:48:51 +0000131 return;
132
Lang Hames9ff69c82015-04-24 19:11:51 +0000133 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
Rafael Espindola697edc82011-04-29 14:48:51 +0000134
135 // Indicate personality routine, if any.
136 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000137 return;
138
Keno Fischeraff703a2015-07-14 19:22:51 +0000139 // If we are forced to emit this personality, make sure to record
140 // it because it might not appear in any landingpad
141 if (forceEmitPersonality)
142 MMI->addPersonality(Per);
143
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000144 const MCSymbol *Sym =
145 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
Lang Hames9ff69c82015-04-24 19:11:51 +0000146 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000147
Rafael Espindola697edc82011-04-29 14:48:51 +0000148 // Provide LSDA information.
149 if (!shouldEmitLSDA)
150 return;
151
Lang Hames9ff69c82015-04-24 19:11:51 +0000152 Asm->OutStreamer->EmitCFILsda(Asm->getCurExceptionSym(), LSDAEncoding);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000153}
154
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000155/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000156///
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000157void DwarfCFIException::endFunction(const MachineFunction *) {
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000158 if (!shouldEmitPersonality)
159 return;
160
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000161 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000162}