blob: ff0aa80b1d79865410a70bd363b018ad6aa07617 [file] [log] [blame]
Bill Wendlingeb907212009-05-15 01:12:28 +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//
Bill Wendling28275fd2009-09-10 06:50:01 +000010// This file contains support for writing DWARF exception info into asm files.
Bill Wendlingeb907212009-05-15 01:12:28 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfException.h"
15#include "llvm/Module.h"
16#include "llvm/CodeGen/MachineModuleInfo.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefc4da0c2009-08-19 21:55:33 +000018#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000019#include "llvm/CodeGen/MachineLocation.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
Bill Wendling43e484f2009-09-10 01:12:47 +000023#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000024#include "llvm/MC/MCStreamer.h"
Chris Lattner7a2ba942010-01-16 18:37:32 +000025#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000026#include "llvm/Target/Mangler.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000027#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000030#include "llvm/Target/TargetOptions.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000031#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000032#include "llvm/Support/Dwarf.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000033#include "llvm/Support/FormattedStream.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000034#include "llvm/Support/Timer.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000035#include "llvm/ADT/SmallString.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000036#include "llvm/ADT/StringExtras.h"
Bill Wendling1f8075d2010-02-09 22:49:16 +000037#include "llvm/ADT/Twine.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000038using namespace llvm;
39
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000040DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
Chris Lattneraf76e592009-08-22 20:48:53 +000041 const MCAsmInfo *T)
Chris Lattnerc3421bb2010-03-09 00:00:15 +000042 : DwarfPrinter(OS, A, T), shouldEmitTable(false),shouldEmitMoves(false),
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000043 shouldEmitTableModule(false), shouldEmitMovesModule(false),
44 ExceptionTimer(0) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +000045 if (TimePassesIsEnabled)
Chris Lattner0b86a6f2009-12-28 07:41:18 +000046 ExceptionTimer = new Timer("DWARF Exception Writer");
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000047}
48
49DwarfException::~DwarfException() {
50 delete ExceptionTimer;
51}
52
Bill Wendling7ccda0f2009-08-25 08:08:33 +000053/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
54/// is shared among many Frame Description Entries. There is at least one CIE
55/// in every non-empty .debug_frame section.
Chris Lattner8c6ed052009-09-16 01:46:41 +000056void DwarfException::EmitCIE(const Function *PersonalityFn, unsigned Index) {
Bill Wendlingeb907212009-05-15 01:12:28 +000057 // Size and sign of stack growth.
58 int stackGrowth =
59 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
60 TargetFrameInfo::StackGrowsUp ?
61 TD->getPointerSize() : -TD->getPointerSize();
62
Chris Lattner8c6ed052009-09-16 01:46:41 +000063 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikov9184b252010-02-15 22:35:59 +000064
Bill Wendlingeb907212009-05-15 01:12:28 +000065 // Begin eh frame section.
Chris Lattner8c6ed052009-09-16 01:46:41 +000066 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
Bill Wendlingeb907212009-05-15 01:12:28 +000067
Chris Lattner974c0fb2010-03-10 02:48:06 +000068 MCSymbol *EHFrameSym;
Chris Lattner09d53fe2010-03-10 07:20:42 +000069 if (TLOF.isFunctionEHFrameSymbolPrivate())
Chris Lattner974c0fb2010-03-10 02:48:06 +000070 EHFrameSym = getDWLabel("EH_frame", Index);
71 else
72 EHFrameSym = Asm->OutContext.GetOrCreateSymbol(Twine("EH_frame") +
73 Twine(Index));
74 Asm->OutStreamer.EmitLabel(EHFrameSym);
Chris Lattner8c6ed052009-09-16 01:46:41 +000075
Chris Lattnerf829eef2010-03-08 22:44:40 +000076 Asm->OutStreamer.EmitLabel(getDWLabel("section_eh_frame", Index));
Bill Wendlingeb907212009-05-15 01:12:28 +000077
78 // Define base labels.
Chris Lattnerf829eef2010-03-08 22:44:40 +000079 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_common", Index));
Bill Wendlingeb907212009-05-15 01:12:28 +000080
81 // Define the eh frame length.
Chris Lattner233f52b2010-03-09 23:52:58 +000082 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnereffa8682010-03-08 23:02:59 +000083 EmitDifference(getDWLabel("eh_frame_common_end", Index),
84 getDWLabel("eh_frame_common_begin", Index), true);
Bill Wendlingeb907212009-05-15 01:12:28 +000085
86 // EH frame header.
Chris Lattnerf829eef2010-03-08 22:44:40 +000087 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_common_begin", Index));
Chris Lattner233f52b2010-03-09 23:52:58 +000088 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Chris Lattnerbcb83e52010-01-20 07:41:15 +000089 Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Chris Lattner233f52b2010-03-09 23:52:58 +000090 Asm->OutStreamer.AddComment("DW_CIE_VERSION");
Chris Lattner066c9ac2010-01-22 22:23:57 +000091 Asm->OutStreamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1/*size*/, 0/*addr*/);
Bill Wendlingeb907212009-05-15 01:12:28 +000092
93 // The personality presence indicates that language specific information will
Chris Lattner8c6ed052009-09-16 01:46:41 +000094 // show up in the eh frame. Find out how we are supposed to lower the
95 // personality function reference:
Anton Korobeynikov9184b252010-02-15 22:35:59 +000096
97 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
98 unsigned FDEEncoding = TLOF.getFDEEncoding();
99 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Bill Wendling52783c62009-09-09 23:56:55 +0000100
Chris Lattner4cf202b2010-01-23 03:11:46 +0000101 char Augmentation[6] = { 0 };
Bill Wendling52783c62009-09-09 23:56:55 +0000102 unsigned AugmentationSize = 0;
103 char *APtr = Augmentation + 1;
104
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000105 if (PersonalityFn) {
Bill Wendling52783c62009-09-09 23:56:55 +0000106 // There is a personality function.
107 *APtr++ = 'P';
108 AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
109 }
110
111 if (UsesLSDA[Index]) {
112 // An LSDA pointer is in the FDE augmentation.
113 *APtr++ = 'L';
114 ++AugmentationSize;
115 }
116
117 if (FDEEncoding != dwarf::DW_EH_PE_absptr) {
118 // A non-default pointer encoding for the FDE.
119 *APtr++ = 'R';
120 ++AugmentationSize;
121 }
122
123 if (APtr != Augmentation + 1)
124 Augmentation[0] = 'z';
125
Chris Lattner233f52b2010-03-09 23:52:58 +0000126 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +0000127 Asm->OutStreamer.EmitBytes(StringRef(Augmentation, strlen(Augmentation)+1),0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000128
129 // Round out reader.
Chris Lattner894d75a2010-01-22 23:18:42 +0000130 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000131 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +0000132 Asm->OutStreamer.AddComment("CIE Return Address Column");
Bill Wendlingeb907212009-05-15 01:12:28 +0000133 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Bill Wendlingeb907212009-05-15 01:12:28 +0000134
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000135 if (Augmentation[0]) {
136 EmitULEB128(AugmentationSize, "Augmentation Size");
Bill Wendling52783c62009-09-09 23:56:55 +0000137
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000138 // If there is a personality, we need to indicate the function's location.
139 if (PersonalityFn) {
140 EmitEncodingByte(PerEncoding, "Personality");
Chris Lattner233f52b2010-03-09 23:52:58 +0000141 Asm->OutStreamer.AddComment("Personality");
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000142 EmitReference(PersonalityFn, PerEncoding);
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000143 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000144 if (UsesLSDA[Index])
145 EmitEncodingByte(LSDAEncoding, "LSDA");
146 if (FDEEncoding != dwarf::DW_EH_PE_absptr)
147 EmitEncodingByte(FDEEncoding, "FDE");
Bill Wendlingeb907212009-05-15 01:12:28 +0000148 }
149
150 // Indicate locations of general callee saved registers in frame.
151 std::vector<MachineMove> Moves;
152 RI->getInitialFrameState(Moves);
Chris Lattner5e6cbe02010-03-13 08:05:25 +0000153 EmitFrameMoves(0, Moves, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000154
155 // On Darwin the linker honors the alignment of eh_frame, which means it must
156 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
157 // holes which confuse readers of eh_frame.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000158 Asm->EmitAlignment(TD->getPointerSize() == 4 ? 2 : 3, 0, 0, false);
Chris Lattnerf829eef2010-03-08 22:44:40 +0000159 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_common_end", Index));
Bill Wendlingeb907212009-05-15 01:12:28 +0000160}
161
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000162/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
163void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000164 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000165 "Should not emit 'available externally' functions at all");
166
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000167 const Function *TheFunc = EHFrameInfo.function;
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000168 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000169
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000170 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
171 unsigned FDEEncoding = TLOF.getFDEEncoding();
172
173 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000174
Bill Wendlingeb907212009-05-15 01:12:28 +0000175 // Externally visible entry into the functions eh frame info. If the
176 // corresponding function is static, this should not be externally visible.
Chris Lattner09d53fe2010-03-10 07:20:42 +0000177 if (!TheFunc->hasLocalLinkage() && TLOF.isFunctionEHSymbolGlobal())
178 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,MCSA_Global);
Bill Wendlingeb907212009-05-15 01:12:28 +0000179
180 // If corresponding function is weak definition, this should be too.
Chris Lattner10b318b2010-01-17 21:43:43 +0000181 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
Chris Lattner974c0fb2010-03-10 02:48:06 +0000182 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
183 MCSA_WeakDefinition);
Bill Wendlingee161a62009-11-11 01:24:59 +0000184
185 // If corresponding function is hidden, this should be too.
186 if (TheFunc->hasHiddenVisibility())
Chris Lattner152a29b2010-01-23 06:53:23 +0000187 if (MCSymbolAttr HiddenAttr = MAI->getHiddenVisibilityAttr())
188 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
189 HiddenAttr);
Bill Wendlingeb907212009-05-15 01:12:28 +0000190
191 // If there are no calls then you can't unwind. This may mean we can omit the
192 // EH Frame, but some environments do not handle weak absolute symbols. If
193 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
194 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000195 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
196 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000197 !MAI->getWeakDefDirective() ||
Chris Lattner09d53fe2010-03-10 07:20:42 +0000198 TLOF.getSupportsWeakOmittedEHFrame())) {
Chris Lattner974c0fb2010-03-10 02:48:06 +0000199 Asm->OutStreamer.EmitAssignment(EHFrameInfo.FunctionEHSym,
200 MCConstantExpr::Create(0, Asm->OutContext));
Bill Wendlingeb907212009-05-15 01:12:28 +0000201 // This name has no connection to the function, so it might get
202 // dead-stripped when the function is not, erroneously. Prohibit
203 // dead-stripping unconditionally.
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000204 if (MAI->hasNoDeadStrip())
205 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000206 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000207 } else {
Chris Lattner974c0fb2010-03-10 02:48:06 +0000208 Asm->OutStreamer.EmitLabel(EHFrameInfo.FunctionEHSym);
Bill Wendlingeb907212009-05-15 01:12:28 +0000209
210 // EH frame header.
Chris Lattner233f52b2010-03-09 23:52:58 +0000211 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattnereffa8682010-03-08 23:02:59 +0000212 EmitDifference(getDWLabel("eh_frame_end", EHFrameInfo.Number),
213 getDWLabel("eh_frame_begin", EHFrameInfo.Number),
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000214 true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000215
Chris Lattnerf829eef2010-03-08 22:44:40 +0000216 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_begin",EHFrameInfo.Number));
Bill Wendlingeb907212009-05-15 01:12:28 +0000217
Chris Lattner233f52b2010-03-09 23:52:58 +0000218 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000219 EmitSectionOffset(getDWLabel("eh_frame_begin", EHFrameInfo.Number),
220 getDWLabel("eh_frame_common",
221 EHFrameInfo.PersonalityIndex),
Chris Lattner57578762010-03-08 23:23:25 +0000222 true, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000223
Chris Lattnerfb658072010-03-13 07:40:56 +0000224 MCSymbol *EHFuncBeginSym = getDWLabel("eh_func_begin", EHFrameInfo.Number);
Bill Wendlingeb907212009-05-15 01:12:28 +0000225
Chris Lattner233f52b2010-03-09 23:52:58 +0000226 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerfb658072010-03-13 07:40:56 +0000227 EmitReference(EHFuncBeginSym, FDEEncoding);
228
Chris Lattner233f52b2010-03-09 23:52:58 +0000229 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerfb658072010-03-13 07:40:56 +0000230 EmitDifference(getDWLabel("eh_func_end", EHFrameInfo.Number),EHFuncBeginSym,
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000231 SizeOfEncodedValue(FDEEncoding) == 4);
Bill Wendlingeb907212009-05-15 01:12:28 +0000232
233 // If there is a personality and landing pads then point to the language
234 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000235 if (MMI->getPersonalities()[0] != NULL) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000236 unsigned Size = SizeOfEncodedValue(LSDAEncoding);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000237
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000238 EmitULEB128(Size, "Augmentation size");
Chris Lattner233f52b2010-03-09 23:52:58 +0000239 Asm->OutStreamer.AddComment("Language Specific Data Area");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000240 if (EHFrameInfo.hasLandingPads)
Chris Lattner326861c2010-03-08 22:50:36 +0000241 EmitReference(getDWLabel("exception", EHFrameInfo.Number),LSDAEncoding);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000242 else
243 Asm->OutStreamer.EmitIntValue(0, Size/*size*/, 0/*addrspace*/);
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000244
Bill Wendlingeb907212009-05-15 01:12:28 +0000245 } else {
Chris Lattner894d75a2010-01-22 23:18:42 +0000246 EmitULEB128(0, "Augmentation size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000247 }
248
249 // Indicate locations of function specific callee saved registers in frame.
Chris Lattner5e6cbe02010-03-13 08:05:25 +0000250 EmitFrameMoves(EHFuncBeginSym, EHFrameInfo.Moves, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000251
252 // On Darwin the linker honors the alignment of eh_frame, which means it
253 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
254 // get holes which confuse readers of eh_frame.
255 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
256 0, 0, false);
Chris Lattnerf829eef2010-03-08 22:44:40 +0000257 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_end", EHFrameInfo.Number));
Bill Wendlingeb907212009-05-15 01:12:28 +0000258
259 // If the function is marked used, this table should be also. We cannot
260 // make the mark unconditional in this case, since retaining the table also
261 // retains the function in this case, and there is code around that depends
262 // on unused functions (calling undefined externals) being dead-stripped to
263 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000264 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000265 if (MAI->hasNoDeadStrip())
266 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000267 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000268 }
Chris Lattner188a87d2010-03-10 01:04:13 +0000269 Asm->OutStreamer.AddBlankLine();
Bill Wendlingeb907212009-05-15 01:12:28 +0000270}
271
Bill Wendlingeb907212009-05-15 01:12:28 +0000272/// SharedTypeIds - How many leading type ids two landing pads have in common.
273unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
274 const LandingPadInfo *R) {
275 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
276 unsigned LSize = LIds.size(), RSize = RIds.size();
277 unsigned MinSize = LSize < RSize ? LSize : RSize;
278 unsigned Count = 0;
279
280 for (; Count != MinSize; ++Count)
281 if (LIds[Count] != RIds[Count])
282 return Count;
283
284 return Count;
285}
286
287/// PadLT - Order landing pads lexicographically by type id.
288bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
289 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
290 unsigned LSize = LIds.size(), RSize = RIds.size();
291 unsigned MinSize = LSize < RSize ? LSize : RSize;
292
293 for (unsigned i = 0; i != MinSize; ++i)
294 if (LIds[i] != RIds[i])
295 return LIds[i] < RIds[i];
296
297 return LSize < RSize;
298}
299
Bill Wendlingd4609622009-07-28 23:23:00 +0000300/// ComputeActionsTable - Compute the actions table and gather the first action
301/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000302unsigned DwarfException::
303ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
304 SmallVectorImpl<ActionEntry> &Actions,
305 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000306
307 // The action table follows the call-site table in the LSDA. The individual
308 // records are of two types:
309 //
310 // * Catch clause
311 // * Exception specification
312 //
313 // The two record kinds have the same format, with only small differences.
314 // They are distinguished by the "switch value" field: Catch clauses
315 // (TypeInfos) have strictly positive switch values, and exception
316 // specifications (FilterIds) have strictly negative switch values. Value 0
317 // indicates a catch-all clause.
318 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000319 // Negative type IDs index into FilterIds. Positive type IDs index into
320 // TypeInfos. The value written for a positive type ID is just the type ID
321 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000322 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000323 // offset is usually equal to the type ID (because the FilterIds entries are
324 // written using a variable width encoding, which outputs one byte per entry
325 // as long as the value written is not too large) but can differ. This kind
326 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000327 // output using a fixed width encoding. FilterOffsets[i] holds the byte
328 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000329
330 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000331 SmallVector<int, 16> FilterOffsets;
332 FilterOffsets.reserve(FilterIds.size());
333 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000334
335 for (std::vector<unsigned>::const_iterator
336 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000337 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000338 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000339 }
340
Bill Wendlingeb907212009-05-15 01:12:28 +0000341 FirstActions.reserve(LandingPads.size());
342
343 int FirstAction = 0;
344 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000345 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000346
Bill Wendling5cff4872009-07-28 23:44:43 +0000347 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000348 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
349 const LandingPadInfo *LPI = *I;
350 const std::vector<int> &TypeIds = LPI->TypeIds;
351 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000352 unsigned SizeSiteActions = 0;
353
354 if (NumShared < TypeIds.size()) {
355 unsigned SizeAction = 0;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000356 unsigned PrevAction = (unsigned)-1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000357
358 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000359 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000360 assert(Actions.size());
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000361 PrevAction = Actions.size() - 1;
362 SizeAction =
363 MCAsmInfo::getSLEB128Size(Actions[PrevAction].NextAction) +
364 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000365
366 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000367 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000368 SizeAction -=
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000369 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
370 SizeAction += -Actions[PrevAction].NextAction;
371 PrevAction = Actions[PrevAction].Previous;
Bill Wendlingeb907212009-05-15 01:12:28 +0000372 }
373 }
374
375 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000376 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
377 int TypeID = TypeIds[J];
378 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000379 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000380 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000381
382 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000383 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000384 SizeSiteActions += SizeAction;
385
Bill Wendlinga583c552009-08-20 22:02:24 +0000386 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000387 Actions.push_back(Action);
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000388 PrevAction = Actions.size() - 1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000389 }
390
391 // Record the first action of the landing pad site.
392 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
393 } // else identical - re-use previous FirstAction
394
Bill Wendlinga583c552009-08-20 22:02:24 +0000395 // Information used when created the call-site table. The action record
396 // field of the call site record is the offset of the first associated
397 // action record, relative to the start of the actions table. This value is
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000398 // biased by 1 (1 indicating the start of the actions table), and 0
Bill Wendlinga583c552009-08-20 22:02:24 +0000399 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000400 FirstActions.push_back(FirstAction);
401
402 // Compute this sites contribution to size.
403 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000404
405 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000406 }
407
Bill Wendling5e953dd2009-07-28 23:22:13 +0000408 return SizeActions;
409}
410
Bill Wendlinged060dc2009-11-12 21:59:20 +0000411/// CallToNoUnwindFunction - Return `true' if this is a call to a function
412/// marked `nounwind'. Return `false' otherwise.
413bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) {
414 assert(MI->getDesc().isCall() && "This should be a call instruction!");
415
416 bool MarkedNoUnwind = false;
417 bool SawFunc = false;
418
419 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
420 const MachineOperand &MO = MI->getOperand(I);
421
422 if (MO.isGlobal()) {
423 if (Function *F = dyn_cast<Function>(MO.getGlobal())) {
424 if (SawFunc) {
425 // Be conservative. If we have more than one function operand for this
426 // call, then we can't make the assumption that it's the callee and
427 // not a parameter to the call.
428 //
429 // FIXME: Determine if there's a way to say that `F' is the callee or
430 // parameter.
431 MarkedNoUnwind = false;
432 break;
433 }
Bill Wendlingecc260e2009-11-12 23:13:08 +0000434
435 MarkedNoUnwind = F->doesNotThrow();
436 SawFunc = true;
Bill Wendlinged060dc2009-11-12 21:59:20 +0000437 }
438 }
439 }
440
441 return MarkedNoUnwind;
442}
443
Bill Wendlingade025c2009-07-29 00:31:35 +0000444/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000445/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000446/// appropriate action. The entry for an ordinary call has a try-range
447/// containing the call and zero for the landing pad and the action. Calls
448/// marked 'nounwind' have no entry and must not be contained in the try-range
449/// of any entry - they form gaps in the table. Entries must be ordered by
450/// try-range address.
451void DwarfException::
452ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
453 const RangeMapType &PadMap,
454 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
455 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000456 // The end label of the previous invoke or nounwind try-range.
Chris Lattner16112732010-03-14 01:41:15 +0000457 MCSymbol *LastLabel = 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000458
459 // Whether there is a potentially throwing instruction (currently this means
460 // an ordinary call) between the end of the previous try-range and now.
461 bool SawPotentiallyThrowing = false;
462
Bill Wendling5cff4872009-07-28 23:44:43 +0000463 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000464 bool PreviousIsInvoke = false;
465
466 // Visit all instructions in order of address.
467 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
468 I != E; ++I) {
469 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
470 MI != E; ++MI) {
471 if (!MI->isLabel()) {
Bill Wendlinged060dc2009-11-12 21:59:20 +0000472 if (MI->getDesc().isCall())
473 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI);
Bill Wendling73b55512009-11-11 23:17:02 +0000474
Bill Wendlingeb907212009-05-15 01:12:28 +0000475 continue;
476 }
477
Chris Lattner16112732010-03-14 01:41:15 +0000478 unsigned BeginLabelNo = MI->getOperand(0).getImm();
479 assert(BeginLabelNo && "Invalid label!");
480 MCSymbol *BeginLabel = getDWLabel("label", BeginLabelNo);
Bill Wendlingeb907212009-05-15 01:12:28 +0000481
482 // End of the previous try-range?
483 if (BeginLabel == LastLabel)
484 SawPotentiallyThrowing = false;
485
486 // Beginning of a new try-range?
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000487 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
Bill Wendlingeb907212009-05-15 01:12:28 +0000488 if (L == PadMap.end())
489 // Nope, it was just some random label.
490 continue;
491
Bill Wendlinga583c552009-08-20 22:02:24 +0000492 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000493 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000494 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
495 "Inconsistent landing pad map!");
496
Bill Wendlinga583c552009-08-20 22:02:24 +0000497 // For Dwarf exception handling (SjLj handling doesn't use this). If some
498 // instruction between the previous try-range and this one may throw,
499 // create a call-site entry with no landing pad for the region between the
500 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000501 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000502 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000503 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000504 CallSites.push_back(Site);
505 PreviousIsInvoke = false;
506 }
507
508 LastLabel = LandingPad->EndLabels[P.RangeIndex];
509 assert(BeginLabel && LastLabel && "Invalid landing pad!");
510
511 if (LandingPad->LandingPadLabel) {
512 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000513 CallSiteEntry Site = {
514 BeginLabel,
515 LastLabel,
516 LandingPad->LandingPadLabel,
517 FirstActions[P.PadIndex]
518 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000519
Jim Grosbach33668c02009-09-01 17:19:13 +0000520 // Try to merge with the previous call-site. SJLJ doesn't do this
521 if (PreviousIsInvoke &&
522 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000523 CallSiteEntry &Prev = CallSites.back();
524 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
525 // Extend the range of the previous entry.
526 Prev.EndLabel = Site.EndLabel;
527 continue;
528 }
529 }
530
531 // Otherwise, create a new call-site.
Jim Grosbachca752c92010-01-28 01:45:32 +0000532 if (MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf)
533 CallSites.push_back(Site);
534 else {
535 // SjLj EH must maintain the call sites in the order assigned
536 // to them by the SjLjPrepare pass.
537 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
538 if (CallSites.size() < SiteNo)
539 CallSites.resize(SiteNo);
540 CallSites[SiteNo - 1] = Site;
541 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000542 PreviousIsInvoke = true;
543 } else {
544 // Create a gap.
545 PreviousIsInvoke = false;
546 }
547 }
548 }
549
550 // If some instruction between the previous try-range and the end of the
551 // function may throw, create a call-site entry with no landing pad for the
552 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000553 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000554 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000555 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000556 CallSites.push_back(Site);
557 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000558}
559
Bill Wendling0dafca92009-07-29 00:50:05 +0000560/// EmitExceptionTable - Emit landing pads and actions.
561///
562/// The general organization of the table is complex, but the basic concepts are
563/// easy. First there is a header which describes the location and organization
564/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000565///
Bill Wendling0dafca92009-07-29 00:50:05 +0000566/// 1. The landing pad site information describes the range of code covered by
567/// the try. In our case it's an accumulation of the ranges covered by the
568/// invokes in the try. There is also a reference to the landing pad that
569/// handles the exception once processed. Finally an index into the actions
570/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000571/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000572/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000573/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000574/// it matches then the exception and type id are passed on to the landing
575/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling28275fd2009-09-10 06:50:01 +0000576/// with a next action of zero. If no type id is found then the frame is
Bill Wendling0dafca92009-07-29 00:50:05 +0000577/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000578/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling28275fd2009-09-10 06:50:01 +0000579/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000580void DwarfException::EmitExceptionTable() {
581 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
582 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
583 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
Bill Wendlingade025c2009-07-29 00:31:35 +0000584
585 // Sort the landing pads in order of their type ids. This is used to fold
586 // duplicate actions.
587 SmallVector<const LandingPadInfo *, 64> LandingPads;
588 LandingPads.reserve(PadInfos.size());
589
590 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
591 LandingPads.push_back(&PadInfos[i]);
592
593 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
594
595 // Compute the actions table and gather the first action index for each
596 // landing pad site.
597 SmallVector<ActionEntry, 32> Actions;
598 SmallVector<unsigned, 64> FirstActions;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000599 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000600
601 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
602 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling28275fd2009-09-10 06:50:01 +0000603 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000604 RangeMapType PadMap;
605 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
606 const LandingPadInfo *LandingPad = LandingPads[i];
607 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Chris Lattner16112732010-03-14 01:41:15 +0000608 MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
Bill Wendlingade025c2009-07-29 00:31:35 +0000609 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
610 PadRange P = { i, j };
611 PadMap[BeginLabel] = P;
612 }
613 }
614
615 // Compute the call-site table.
616 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000617 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000618
619 // Final tallies.
620
621 // Call sites.
Bill Wendling40121bc2009-09-10 00:13:16 +0000622 const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
623 const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
624 const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000625 bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000626 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Bill Wendling3dc9b482010-02-24 23:34:35 +0000627 unsigned CallSiteTableLength;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000628
629 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000630 CallSiteTableLength = 0;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000631 else
Bill Wendling3dc9b482010-02-24 23:34:35 +0000632 CallSiteTableLength = CallSites.size() *
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000633 (SiteStartSize + SiteLengthSize + LandingPadSize);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000634
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000635 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Bill Wendling3dc9b482010-02-24 23:34:35 +0000636 CallSiteTableLength += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000637 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000638 CallSiteTableLength += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000639 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000640
Bill Wendlingeb907212009-05-15 01:12:28 +0000641 // Type infos.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000642 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000643 unsigned TTypeEncoding;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000644 unsigned TypeFormatSize;
Bill Wendlingeb907212009-05-15 01:12:28 +0000645
Bill Wendling43e484f2009-09-10 01:12:47 +0000646 if (!HaveTTData) {
Bill Wendling28275fd2009-09-10 06:50:01 +0000647 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
648 // that we're omitting that bit.
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000649 TTypeEncoding = dwarf::DW_EH_PE_omit;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000650 TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr);
Chris Lattner81c9a062009-07-31 22:03:47 +0000651 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000652 // Okay, we have actual filters or typeinfos to emit. As such, we need to
653 // pick a type encoding for them. We're about to emit a list of pointers to
654 // typeinfo objects at the end of the LSDA. However, unless we're in static
655 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000656 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000657 // Because of this, we have a couple of options:
Bill Wendling28275fd2009-09-10 06:50:01 +0000658 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000659 // 1) If we are in -static mode, we can always use an absolute reference
660 // from the LSDA, because the static linker will resolve it.
Bill Wendling28275fd2009-09-10 06:50:01 +0000661 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000662 // 2) Otherwise, if the LSDA section is writable, we can output the direct
663 // reference to the typeinfo and allow the dynamic linker to relocate
664 // it. Since it is in a writable section, the dynamic linker won't
665 // have a problem.
Bill Wendling28275fd2009-09-10 06:50:01 +0000666 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000667 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
668 // we need to use some form of indirection. For example, on Darwin,
669 // we can output a statically-relocatable reference to a dyld stub. The
670 // offset to the stub is constant, but the contents are in a section
671 // that is updated by the dynamic linker. This is easy enough, but we
672 // need to tell the personality function of the unwinder to indirect
673 // through the dyld stub.
674 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000675 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattnerad88bc42009-08-02 03:59:56 +0000676 // somewhere. This predicate should be moved to a shared location that is
677 // in target-independent code.
678 //
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000679 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
680 TypeFormatSize = SizeOfEncodedValue(TTypeEncoding);
Bill Wendlingfe220282009-09-10 02:07:37 +0000681 }
Bill Wendling43e484f2009-09-10 01:12:47 +0000682
Bill Wendlingfe220282009-09-10 02:07:37 +0000683 // Begin the exception table.
684 Asm->OutStreamer.SwitchSection(LSDASection);
685 Asm->EmitAlignment(2, 0, 0, false);
Bill Wendling43e484f2009-09-10 01:12:47 +0000686
Bill Wendling3dc9b482010-02-24 23:34:35 +0000687 // Emit the LSDA.
Chris Lattner974c0fb2010-03-10 02:48:06 +0000688 MCSymbol *GCCETSym =
689 Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+
690 Twine(SubprogramCount));
691 Asm->OutStreamer.EmitLabel(GCCETSym);
Chris Lattnerf829eef2010-03-08 22:44:40 +0000692 Asm->OutStreamer.EmitLabel(getDWLabel("exception", SubprogramCount));
Bill Wendlingfe220282009-09-10 02:07:37 +0000693
Chris Lattner974c0fb2010-03-10 02:48:06 +0000694 if (IsSJLJ)
695 Asm->OutStreamer.EmitLabel(getDWLabel("_LSDA_", Asm->getFunctionNumber()));
Bill Wendlingfe220282009-09-10 02:07:37 +0000696
Bill Wendling3dc9b482010-02-24 23:34:35 +0000697 // Emit the LSDA header.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000698 EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000699 EmitEncodingByte(TTypeEncoding, "@TType");
Bill Wendlingfe220282009-09-10 02:07:37 +0000700
Bill Wendling3dc9b482010-02-24 23:34:35 +0000701 // The type infos need to be aligned. GCC does this by inserting padding just
702 // before the type infos. However, this changes the size of the exception
703 // table, so you need to take this into account when you output the exception
704 // table size. However, the size is output using a variable length encoding.
705 // So by increasing the size by inserting padding, you may increase the number
706 // of bytes used for writing the size. If it increases, say by one byte, then
707 // you now need to output one less byte of padding to get the type infos
708 // aligned. However this decreases the size of the exception table. This
709 // changes the value you have to output for the exception table size. Due to
710 // the variable length encoding, the number of bytes used for writing the
711 // length may decrease. If so, you then have to increase the amount of
712 // padding. And so on. If you look carefully at the GCC code you will see that
713 // it indeed does this in a loop, going on and on until the values stabilize.
714 // We chose another solution: don't output padding inside the table like GCC
715 // does, instead output it before the table.
716 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
717 unsigned CallSiteTableLengthSize =
718 MCAsmInfo::getULEB128Size(CallSiteTableLength);
719 unsigned TTypeBaseOffset =
720 sizeof(int8_t) + // Call site format
721 CallSiteTableLengthSize + // Call site table length size
722 CallSiteTableLength + // Call site table length
723 SizeActions + // Actions size
724 SizeTypes;
725 unsigned TTypeBaseOffsetSize = MCAsmInfo::getULEB128Size(TTypeBaseOffset);
726 unsigned TotalSize =
727 sizeof(int8_t) + // LPStart format
728 sizeof(int8_t) + // TType format
729 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size
730 TTypeBaseOffset; // TType base offset
731 unsigned SizeAlign = (4 - TotalSize) & 3;
732
Bill Wendling86f0d332010-02-25 23:52:44 +0000733 if (HaveTTData) {
Bill Wendling6507eca2010-02-26 21:31:01 +0000734 // Account for any extra padding that will be added to the call site table
Bill Wendlingf7e90ae2010-02-25 21:19:47 +0000735 // length.
Bill Wendling1869ac82010-02-26 22:17:52 +0000736 EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign);
737 SizeAlign = 0;
Bill Wendling86f0d332010-02-25 23:52:44 +0000738 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000739
Bill Wendling28275fd2009-09-10 06:50:01 +0000740 // SjLj Exception handling
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000741 if (IsSJLJ) {
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000742 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling1869ac82010-02-26 22:17:52 +0000743
744 // Add extra padding if it wasn't added to the TType base offset.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000745 EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000746
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000747 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000748 unsigned idx = 0;
749 for (SmallVectorImpl<CallSiteEntry>::const_iterator
750 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
751 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000752
753 // Offset of the landing pad, counted in 16-byte bundles relative to the
754 // @LPStart address.
Chris Lattner894d75a2010-01-22 23:18:42 +0000755 EmitULEB128(idx, "Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000756
757 // Offset of the first associated action record, relative to the start of
758 // the action table. This value is biased by 1 (1 indicates the start of
759 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000760 EmitULEB128(S.Action, "Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000761 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000762 } else {
763 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000764 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000765
Bill Wendlinga583c552009-08-20 22:02:24 +0000766 // The call-site table is a list of all call sites that may throw an
767 // exception (including C++ 'throw' statements) in the procedure
768 // fragment. It immediately follows the LSDA header. Each entry indicates,
769 // for a given call, the first corresponding action record and corresponding
770 // landing pad.
771 //
772 // The table begins with the number of bytes, stored as an LEB128
773 // compressed, unsigned integer. The records immediately follow the record
774 // count. They are sorted in increasing call-site address. Each record
775 // indicates:
776 //
777 // * The position of the call-site.
778 // * The position of the landing pad.
779 // * The first action record for that call site.
780 //
781 // A missing entry in the call-site table indicates that a call is not
Bill Wendling28275fd2009-09-10 06:50:01 +0000782 // supposed to throw.
Bill Wendlinga583c552009-08-20 22:02:24 +0000783
784 // Emit the landing pad call site table.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000785 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling1869ac82010-02-26 22:17:52 +0000786
787 // Add extra padding if it wasn't added to the TType base offset.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000788 EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000789
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000790 for (SmallVectorImpl<CallSiteEntry>::const_iterator
791 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
792 const CallSiteEntry &S = *I;
Chris Lattner16112732010-03-14 01:41:15 +0000793
794 MCSymbol *EHFuncBeginSym = getDWLabel("eh_func_begin", SubprogramCount);
795
796 MCSymbol *BeginLabel = S.BeginLabel;
797 if (BeginLabel == 0)
798 BeginLabel = EHFuncBeginSym;
799 MCSymbol *EndLabel = S.EndLabel;
800 if (EndLabel == 0)
801 EndLabel = getDWLabel("eh_func_end", SubprogramCount);
802
Bill Wendlinga583c552009-08-20 22:02:24 +0000803 // Offset of the call site relative to the previous call site, counted in
804 // number of 16-byte bundles. The first call site is counted relative to
805 // the start of the procedure fragment.
Chris Lattner233f52b2010-03-09 23:52:58 +0000806 Asm->OutStreamer.AddComment("Region start");
Chris Lattner16112732010-03-14 01:41:15 +0000807 EmitSectionOffset(EHFuncBeginSym, EndLabel, true);
808
Chris Lattner233f52b2010-03-09 23:52:58 +0000809 Asm->OutStreamer.AddComment("Region length");
Chris Lattner16112732010-03-14 01:41:15 +0000810 EmitDifference(EndLabel, BeginLabel, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000811
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000812
Bill Wendlinga583c552009-08-20 22:02:24 +0000813 // Offset of the landing pad, counted in 16-byte bundles relative to the
814 // @LPStart address.
Chris Lattner233f52b2010-03-09 23:52:58 +0000815 Asm->OutStreamer.AddComment("Landing pad");
Chris Lattner16112732010-03-14 01:41:15 +0000816 if (!S.PadLabel)
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000817 Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Chris Lattner16112732010-03-14 01:41:15 +0000818 else
819 EmitSectionOffset(S.PadLabel, EHFuncBeginSym, true, true);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000820
Bill Wendlinga583c552009-08-20 22:02:24 +0000821 // Offset of the first associated action record, relative to the start of
822 // the action table. This value is biased by 1 (1 indicates the start of
823 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000824 EmitULEB128(S.Action, "Action");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000825 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000826 }
827
Bill Wendlinga583c552009-08-20 22:02:24 +0000828 // Emit the Action Table.
Chris Lattner233f52b2010-03-09 23:52:58 +0000829 if (Actions.size() != 0) {
830 Asm->OutStreamer.AddComment("-- Action Record Table --");
831 Asm->OutStreamer.AddBlankLine();
832 }
833
Bill Wendling5cff4872009-07-28 23:44:43 +0000834 for (SmallVectorImpl<ActionEntry>::const_iterator
835 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
836 const ActionEntry &Action = *I;
Chris Lattner233f52b2010-03-09 23:52:58 +0000837 Asm->OutStreamer.AddComment("Action Record");
838 Asm->OutStreamer.AddBlankLine();
Bill Wendlinga583c552009-08-20 22:02:24 +0000839
840 // Type Filter
841 //
842 // Used by the runtime to match the type of the thrown exception to the
843 // type of the catch clauses or the types in the exception specification.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000844 EmitSLEB128(Action.ValueForTypeID, " TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000845
846 // Action Record
847 //
848 // Self-relative signed displacement in bytes of the next action record,
849 // or 0 if there is no next action record.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000850 EmitSLEB128(Action.NextAction, " Next action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000851 }
852
Bill Wendling48dc29e2009-10-22 20:48:59 +0000853 // Emit the Catch TypeInfos.
Chris Lattner233f52b2010-03-09 23:52:58 +0000854 if (!TypeInfos.empty()) {
855 Asm->OutStreamer.AddComment("-- Catch TypeInfos --");
856 Asm->OutStreamer.AddBlankLine();
857 }
Bill Wendlingec044582009-11-18 23:18:46 +0000858 for (std::vector<GlobalVariable *>::const_reverse_iterator
Bill Wendling01c69372009-11-19 00:09:14 +0000859 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000860 const GlobalVariable *GV = *I;
Bill Wendlingec044582009-11-18 23:18:46 +0000861
Chris Lattner90e4af72010-03-10 00:09:21 +0000862 Asm->OutStreamer.AddComment("TypeInfo");
863 if (GV)
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000864 EmitReference(GV, TTypeEncoding);
Chris Lattner90e4af72010-03-10 00:09:21 +0000865 else
866 Asm->OutStreamer.EmitIntValue(0, SizeOfEncodedValue(TTypeEncoding), 0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000867 }
868
Bill Wendling48dc29e2009-10-22 20:48:59 +0000869 // Emit the Exception Specifications.
Chris Lattner233f52b2010-03-09 23:52:58 +0000870 if (!FilterIds.empty()) {
871 Asm->OutStreamer.AddComment("-- Filter IDs --");
872 Asm->OutStreamer.AddBlankLine();
873 }
Bill Wendling5cff4872009-07-28 23:44:43 +0000874 for (std::vector<unsigned>::const_iterator
875 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
876 unsigned TypeID = *I;
Chris Lattner894d75a2010-01-22 23:18:42 +0000877 EmitULEB128(TypeID, TypeID != 0 ? "Exception specification" : 0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000878 }
879
880 Asm->EmitAlignment(2, 0, 0, false);
881}
882
Bill Wendlingeb907212009-05-15 01:12:28 +0000883/// EndModule - Emit all exception information that should come after the
884/// content.
885void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000886 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000887 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000888
Bill Wendling52783c62009-09-09 23:56:55 +0000889 if (!shouldEmitMovesModule && !shouldEmitTableModule)
890 return;
891
Bill Wendlingeb907212009-05-15 01:12:28 +0000892 if (TimePassesIsEnabled)
893 ExceptionTimer->startTimer();
894
Bill Wendling52783c62009-09-09 23:56:55 +0000895 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000896
Bill Wendling28275fd2009-09-10 06:50:01 +0000897 for (unsigned I = 0, E = Personalities.size(); I < E; ++I)
898 EmitCIE(Personalities[I], I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000899
Bill Wendling52783c62009-09-09 23:56:55 +0000900 for (std::vector<FunctionEHFrameInfo>::iterator
901 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
902 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000903
904 if (TimePassesIsEnabled)
905 ExceptionTimer->stopTimer();
906}
907
Bill Wendling28275fd2009-09-10 06:50:01 +0000908/// BeginFunction - Gather pre-function exception information. Assumes it's
909/// being emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +0000910void DwarfException::BeginFunction(const MachineFunction *MF) {
Bill Wendling73c5a612009-09-10 18:28:06 +0000911 if (!MMI || !MAI->doesSupportExceptionHandling()) return;
912
Bill Wendlingeb907212009-05-15 01:12:28 +0000913 if (TimePassesIsEnabled)
914 ExceptionTimer->startTimer();
915
916 this->MF = MF;
917 shouldEmitTable = shouldEmitMoves = false;
918
Bill Wendling73c5a612009-09-10 18:28:06 +0000919 // If any landing pads survive, we need an EH table.
Chris Lattner16112732010-03-14 01:41:15 +0000920 shouldEmitTable = !MMI->getLandingPads().empty();
Bill Wendlingeb907212009-05-15 01:12:28 +0000921
Bill Wendling73c5a612009-09-10 18:28:06 +0000922 // See if we need frame move info.
Chris Lattner16112732010-03-14 01:41:15 +0000923 shouldEmitMoves = !MF->getFunction()->doesNotThrow() || UnwindTablesMandatory;
Bill Wendlingeb907212009-05-15 01:12:28 +0000924
Bill Wendling73c5a612009-09-10 18:28:06 +0000925 if (shouldEmitMoves || shouldEmitTable)
926 // Assumes in correct section after the entry point.
Chris Lattnerf829eef2010-03-08 22:44:40 +0000927 Asm->OutStreamer.EmitLabel(getDWLabel("eh_func_begin", ++SubprogramCount));
Bill Wendlingeb907212009-05-15 01:12:28 +0000928
929 shouldEmitTableModule |= shouldEmitTable;
930 shouldEmitMovesModule |= shouldEmitMoves;
931
932 if (TimePassesIsEnabled)
933 ExceptionTimer->stopTimer();
934}
935
936/// EndFunction - Gather and emit post-function exception information.
937///
938void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000939 if (!shouldEmitMoves && !shouldEmitTable) return;
940
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000941 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000942 ExceptionTimer->startTimer();
943
Chris Lattnerf829eef2010-03-08 22:44:40 +0000944 Asm->OutStreamer.EmitLabel(getDWLabel("eh_func_end", SubprogramCount));
Chris Lattner16112732010-03-14 01:41:15 +0000945
946 // Record if this personality index uses a landing pad.
947 bool HasLandingPad = !MMI->getLandingPads().empty();
948 UsesLSDA[MMI->getPersonalityIndex()] |= HasLandingPad;
949
950 // Map all labels and get rid of any dead landing pads.
951 MMI->TidyLandingPads();
952
953 if (HasLandingPad)
954 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000955
Chris Lattner09d53fe2010-03-10 07:20:42 +0000956 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000957 MCSymbol *FunctionEHSym =
Chris Lattner7a2ba942010-01-16 18:37:32 +0000958 Asm->GetSymbolWithGlobalValueBase(MF->getFunction(), ".eh",
Chris Lattner09d53fe2010-03-10 07:20:42 +0000959 TLOF.isFunctionEHFrameSymbolPrivate());
Chris Lattner25d812b2009-09-16 00:35:39 +0000960
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000961 // Save EH frame information
Chris Lattner7a2ba942010-01-16 18:37:32 +0000962 EHFrames.push_back(FunctionEHFrameInfo(FunctionEHSym, SubprogramCount,
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000963 MMI->getPersonalityIndex(),
964 MF->getFrameInfo()->hasCalls(),
965 !MMI->getLandingPads().empty(),
966 MMI->getFrameMoves(),
967 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +0000968
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000969 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000970 ExceptionTimer->stopTimer();
971}