blob: e2d95272c2c533751f582123640772e9b00bd3c9 [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
42DwarfCFIException::DwarfCFIException(AsmPrinter *A)
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000043 : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false),
44 shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {}
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000045
46DwarfCFIException::~DwarfCFIException() {}
47
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000048/// endModule - Emit all exception information that should come after the
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000049/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000050void DwarfCFIException::endModule() {
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000051 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
52 Asm->OutStreamer.EmitCFISections(false, true);
53
Anton Korobeynikov9be547c2011-01-14 21:58:08 +000054 if (!Asm->MAI->isExceptionHandlingDwarf())
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000055 return;
56
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000057 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindola1fc5bf92011-04-29 15:09:53 +000058
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000059 unsigned PerEncoding = TLOF.getPersonalityEncoding();
60
Logan Chienc0029812014-05-30 16:48:56 +000061 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000062 return;
63
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000064 // Emit references to all used personality functions
Rafael Espindolae4066702011-05-02 15:49:52 +000065 bool AtLeastOne = false;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000066 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
67 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
Rafael Espindola697edc82011-04-29 14:48:51 +000068 if (!Personalities[i])
69 continue;
Rafael Espindola79858aa2013-10-29 17:07:16 +000070 MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000071 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
Rafael Espindolae4066702011-05-02 15:49:52 +000072 AtLeastOne = true;
73 }
74
75 if (AtLeastOne && !TLOF.isFunctionEHFrameSymbolPrivate()) {
76 // This is a temporary hack to keep sections in the same order they
77 // were before. This lets us produce bit identical outputs while
78 // transitioning to CFI.
Evan Cheng76792992011-07-20 05:58:47 +000079 Asm->OutStreamer.SwitchSection(
80 const_cast<TargetLoweringObjectFile&>(TLOF).getEHFrameSection());
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000081 }
82}
83
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000084/// beginFunction - Gather pre-function exception information. Assumes it's
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000085/// being emitted immediately after the function entry point.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000086void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000087 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000088
89 // If any landing pads survive, we need an EH table.
Rafael Espindola697edc82011-04-29 14:48:51 +000090 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000091
92 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000093 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
94 if (MoveType == AsmPrinter::CFI_M_EH ||
95 (MoveType == AsmPrinter::CFI_M_Debug &&
96 moveTypeModule == AsmPrinter::CFI_M_None))
97 moveTypeModule = MoveType;
98
99 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000100
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000101 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000102 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000103 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
Rafael Espindola697edc82011-04-29 14:48:51 +0000104
105 shouldEmitPersonality = hasLandingPads &&
106 PerEncoding != dwarf::DW_EH_PE_omit && Per;
107
108 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
109 shouldEmitLSDA = shouldEmitPersonality &&
110 LSDAEncoding != dwarf::DW_EH_PE_omit;
111
112 if (!shouldEmitPersonality && !shouldEmitMoves)
113 return;
114
David Majnemere035cf92014-01-27 17:20:25 +0000115 Asm->OutStreamer.EmitCFIStartProc(/*IsSimple=*/false);
Rafael Espindola697edc82011-04-29 14:48:51 +0000116
117 // Indicate personality routine, if any.
118 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000119 return;
120
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000121 const MCSymbol *Sym =
122 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000123 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000124
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000125 Asm->OutStreamer.EmitDebugLabel
126 (Asm->GetTempSymbol("eh_func_begin",
127 Asm->getFunctionNumber()));
Rafael Espindola697edc82011-04-29 14:48:51 +0000128
129 // Provide LSDA information.
130 if (!shouldEmitLSDA)
131 return;
132
133 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
134 Asm->getFunctionNumber()),
135 LSDAEncoding);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000136}
137
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000138/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000139///
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000140void DwarfCFIException::endFunction(const MachineFunction *) {
Rafael Espindola697edc82011-04-29 14:48:51 +0000141 if (!shouldEmitPersonality && !shouldEmitMoves)
142 return;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000143
Rafael Espindolaa0761992011-04-24 19:55:34 +0000144 Asm->OutStreamer.EmitCFIEndProc();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000145
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000146 if (!shouldEmitPersonality)
147 return;
148
Rafael Espindolaa6931282012-01-13 23:28:50 +0000149 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
150 Asm->getFunctionNumber()));
151
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000152 // Map all labels and get rid of any dead landing pads.
153 MMI->TidyLandingPads();
154
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000155 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000156}