blob: 1990456cc555c50497585d47e994a5b33c3d0479 [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/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/Dwarf.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/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000021#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000023#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/MC/MachineLocation.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/FormattedStream.h"
David Blaikie6054e652018-03-23 23:58:19 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000033#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000034using namespace llvm;
35
Rafael Espindolaa6001792015-03-09 18:29:12 +000036DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
Joerg Sonnenberger7b837322017-01-02 18:05:27 +000037 : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
Rafael Espindolaa6001792015-03-09 18:29:12 +000038
39void DwarfCFIExceptionBase::markFunctionEnd() {
Amaury Sechet7067ad32016-02-26 20:30:37 +000040 endFragment();
Rafael Espindolaa6001792015-03-09 18:29:12 +000041
Rafael Espindolaa6001792015-03-09 18:29:12 +000042 // Map all labels and get rid of any dead landing pads.
Matthias Braund0ee66c2016-12-01 19:32:15 +000043 if (!Asm->MF->getLandingPads().empty()) {
44 MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
45 NonConstMF->tidyLandingPads();
46 }
Rafael Espindolaa6001792015-03-09 18:29:12 +000047}
48
Amaury Sechet7067ad32016-02-26 20:30:37 +000049void DwarfCFIExceptionBase::endFragment() {
50 if (shouldEmitCFI)
51 Asm->OutStreamer->EmitCFIEndProc();
52}
53
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000054DwarfCFIException::DwarfCFIException(AsmPrinter *A)
Rafael Espindolaa6001792015-03-09 18:29:12 +000055 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
Amaury Sechetb2055c52016-02-26 20:49:07 +000056 forceEmitPersonality(false), shouldEmitLSDA(false),
Joerg Sonnenberger7b837322017-01-02 18:05:27 +000057 shouldEmitMoves(false) {}
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000058
59DwarfCFIException::~DwarfCFIException() {}
60
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000061/// endModule - Emit all exception information that should come after the
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000062/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000063void DwarfCFIException::endModule() {
Reid Kleckner5cc15692015-01-23 18:49:01 +000064 // SjLj uses this pass and it doesn't need this info.
65 if (!Asm->MAI->usesCFIForEH())
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000066 return;
67
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000068 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindola1fc5bf92011-04-29 15:09:53 +000069
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000070 unsigned PerEncoding = TLOF.getPersonalityEncoding();
71
Logan Chienc0029812014-05-30 16:48:56 +000072 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000073 return;
74
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000075 // Emit references to all used personality functions
Reid Klecknere00faf82015-08-31 20:02:16 +000076 for (const Function *Personality : MMI->getPersonalities()) {
77 if (!Personality)
Rafael Espindola697edc82011-04-29 14:48:51 +000078 continue;
Reid Klecknere00faf82015-08-31 20:02:16 +000079 MCSymbol *Sym = Asm->getSymbol(Personality);
Mehdi Amini5c0fa582015-07-16 06:04:17 +000080 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000081 }
82}
83
Amaury Sechet7067ad32016-02-26 20:30:37 +000084static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
85 return Asm->getCurExceptionSym();
86}
87
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000088void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000089 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Matthias Braunf1caa282017-12-15 22:22:58 +000090 const Function &F = MF->getFunction();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000091
92 // If any landing pads survive, we need an EH table.
Matthias Braund0ee66c2016-12-01 19:32:15 +000093 bool hasLandingPads = !MF->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000094
95 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000096 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000097
98 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000099
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000100 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000101 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Keno Fischeraff703a2015-07-14 19:22:51 +0000102 const Function *Per = nullptr;
Matthias Braunf1caa282017-12-15 22:22:58 +0000103 if (F.hasPersonalityFn())
104 Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
Rafael Espindola697edc82011-04-29 14:48:51 +0000105
Keno Fischeraff703a2015-07-14 19:22:51 +0000106 // Emit a personality function even when there are no landing pads
Amaury Sechet7067ad32016-02-26 20:30:37 +0000107 forceEmitPersonality =
Keno Fischeraff703a2015-07-14 19:22:51 +0000108 // ...if a personality function is explicitly specified
Matthias Braunf1caa282017-12-15 22:22:58 +0000109 F.hasPersonalityFn() &&
Keno Fischeraff703a2015-07-14 19:22:51 +0000110 // ... and it's not known to be a noop in the absence of invokes
111 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
112 // ... and we're not explicitly asked not to emit it
Matthias Braunf1caa282017-12-15 22:22:58 +0000113 F.needsUnwindTableEntry();
Keno Fischeraff703a2015-07-14 19:22:51 +0000114
115 shouldEmitPersonality =
116 (forceEmitPersonality ||
117 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
118 Per;
Rafael Espindola697edc82011-04-29 14:48:51 +0000119
120 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
121 shouldEmitLSDA = shouldEmitPersonality &&
122 LSDAEncoding != dwarf::DW_EH_PE_omit;
123
Saleem Abdulrasool467269a2016-07-15 21:10:29 +0000124 shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() &&
125 (shouldEmitPersonality || shouldEmitMoves);
Amaury Sechet7067ad32016-02-26 20:30:37 +0000126 beginFragment(&*MF->begin(), getExceptionSym);
127}
128
129void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
130 ExceptionSymbolProvider ESP) {
Rafael Espindolaa6001792015-03-09 18:29:12 +0000131 if (!shouldEmitCFI)
Rafael Espindola697edc82011-04-29 14:48:51 +0000132 return;
133
Joerg Sonnenberger7b837322017-01-02 18:05:27 +0000134 if (!hasEmittedCFISections) {
Joerg Sonnenberger83963992017-01-05 20:55:28 +0000135 if (Asm->needsOnlyDebugCFIMoves())
Joerg Sonnenberger7b837322017-01-02 18:05:27 +0000136 Asm->OutStreamer->EmitCFISections(false, true);
137 hasEmittedCFISections = true;
138 }
139
Lang Hames9ff69c82015-04-24 19:11:51 +0000140 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
Rafael Espindola697edc82011-04-29 14:48:51 +0000141
142 // Indicate personality routine, if any.
143 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000144 return;
145
Matthias Braunf1caa282017-12-15 22:22:58 +0000146 auto &F = MBB->getParent()->getFunction();
147 auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
Amaury Sechet7067ad32016-02-26 20:30:37 +0000148 assert(P && "Expected personality function");
149
Keno Fischeraff703a2015-07-14 19:22:51 +0000150 // If we are forced to emit this personality, make sure to record
151 // it because it might not appear in any landingpad
152 if (forceEmitPersonality)
Amaury Sechet7067ad32016-02-26 20:30:37 +0000153 MMI->addPersonality(P);
Keno Fischeraff703a2015-07-14 19:22:51 +0000154
Amaury Sechet7067ad32016-02-26 20:30:37 +0000155 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
156 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Eric Christopher4367c7f2016-09-16 07:33:15 +0000157 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
Lang Hames9ff69c82015-04-24 19:11:51 +0000158 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000159
Rafael Espindola697edc82011-04-29 14:48:51 +0000160 // Provide LSDA information.
Amaury Sechet7067ad32016-02-26 20:30:37 +0000161 if (shouldEmitLSDA)
162 Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000163}
164
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000165/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000166///
Matthias Braund0ee66c2016-12-01 19:32:15 +0000167void DwarfCFIException::endFunction(const MachineFunction *MF) {
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000168 if (!shouldEmitPersonality)
169 return;
170
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000171 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000172}