blob: 79200198c5ac9ab26ab5bb92561867e395983565 [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 Lattner066c9ac2010-01-22 22:23:57 +000042 : DwarfPrinter(OS, A, T, "eh"), 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 Wendling7378b1b2009-11-17 01:23:53 +000053/// CreateLabelDiff - Emit a label and subtract it from the expression we
54/// already have. This is equivalent to emitting "foo - .", but we have to emit
55/// the label for "." directly.
56const MCExpr *DwarfException::CreateLabelDiff(const MCExpr *ExprRef,
57 const char *LabelName,
58 unsigned Index) {
59 SmallString<64> Name;
60 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
61 << LabelName << Asm->getFunctionNumber()
62 << "_" << Index;
63 MCSymbol *DotSym = Asm->OutContext.GetOrCreateSymbol(Name.str());
64 Asm->OutStreamer.EmitLabel(DotSym);
65
66 return MCBinaryExpr::CreateSub(ExprRef,
67 MCSymbolRefExpr::Create(DotSym,
68 Asm->OutContext),
69 Asm->OutContext);
70}
71
Bill Wendling7ccda0f2009-08-25 08:08:33 +000072/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
73/// is shared among many Frame Description Entries. There is at least one CIE
74/// in every non-empty .debug_frame section.
Chris Lattner8c6ed052009-09-16 01:46:41 +000075void DwarfException::EmitCIE(const Function *PersonalityFn, unsigned Index) {
Bill Wendlingeb907212009-05-15 01:12:28 +000076 // Size and sign of stack growth.
77 int stackGrowth =
78 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
79 TargetFrameInfo::StackGrowsUp ?
80 TD->getPointerSize() : -TD->getPointerSize();
81
Chris Lattner8c6ed052009-09-16 01:46:41 +000082 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikov9184b252010-02-15 22:35:59 +000083
Bill Wendlingeb907212009-05-15 01:12:28 +000084 // Begin eh frame section.
Chris Lattner8c6ed052009-09-16 01:46:41 +000085 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
Bill Wendlingeb907212009-05-15 01:12:28 +000086
Chris Lattner33adcfb2009-08-22 21:43:10 +000087 if (MAI->is_EHSymbolPrivate())
88 O << MAI->getPrivateGlobalPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +000089 O << "EH_frame" << Index << ":\n";
Chris Lattner8c6ed052009-09-16 01:46:41 +000090
Bill Wendlingeb907212009-05-15 01:12:28 +000091 EmitLabel("section_eh_frame", Index);
92
93 // Define base labels.
94 EmitLabel("eh_frame_common", Index);
95
96 // Define the eh frame length.
97 EmitDifference("eh_frame_common_end", Index,
98 "eh_frame_common_begin", Index, true);
Chris Lattnerfaca5492010-01-22 23:47:11 +000099 EOL("Length of Common Information Entry");
Bill Wendlingeb907212009-05-15 01:12:28 +0000100
101 // EH frame header.
102 EmitLabel("eh_frame_common_begin", Index);
Chris Lattner066c9ac2010-01-22 22:23:57 +0000103 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("CIE Identifier Tag");
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000104 Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Chris Lattner066c9ac2010-01-22 22:23:57 +0000105 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DW_CIE_VERSION");
106 Asm->OutStreamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1/*size*/, 0/*addr*/);
Bill Wendlingeb907212009-05-15 01:12:28 +0000107
108 // The personality presence indicates that language specific information will
Chris Lattner8c6ed052009-09-16 01:46:41 +0000109 // show up in the eh frame. Find out how we are supposed to lower the
110 // personality function reference:
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000111
112 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
113 unsigned FDEEncoding = TLOF.getFDEEncoding();
114 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Bill Wendling52783c62009-09-09 23:56:55 +0000115
Chris Lattner4cf202b2010-01-23 03:11:46 +0000116 char Augmentation[6] = { 0 };
Bill Wendling52783c62009-09-09 23:56:55 +0000117 unsigned AugmentationSize = 0;
118 char *APtr = Augmentation + 1;
119
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000120 if (PersonalityFn) {
Bill Wendling52783c62009-09-09 23:56:55 +0000121 // There is a personality function.
122 *APtr++ = 'P';
123 AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
124 }
125
126 if (UsesLSDA[Index]) {
127 // An LSDA pointer is in the FDE augmentation.
128 *APtr++ = 'L';
129 ++AugmentationSize;
130 }
131
132 if (FDEEncoding != dwarf::DW_EH_PE_absptr) {
133 // A non-default pointer encoding for the FDE.
134 *APtr++ = 'R';
135 ++AugmentationSize;
136 }
137
138 if (APtr != Augmentation + 1)
139 Augmentation[0] = 'z';
140
Chris Lattner4cf202b2010-01-23 03:11:46 +0000141 Asm->OutStreamer.EmitBytes(StringRef(Augmentation, strlen(Augmentation)+1),0);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000142 EOL("CIE Augmentation");
Bill Wendlingeb907212009-05-15 01:12:28 +0000143
144 // Round out reader.
Chris Lattner894d75a2010-01-22 23:18:42 +0000145 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000146 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Bill Wendlingeb907212009-05-15 01:12:28 +0000147 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Chris Lattnerfaca5492010-01-22 23:47:11 +0000148 EOL("CIE Return Address Column");
Bill Wendlingeb907212009-05-15 01:12:28 +0000149
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000150 if (Augmentation[0]) {
151 EmitULEB128(AugmentationSize, "Augmentation Size");
Bill Wendling52783c62009-09-09 23:56:55 +0000152
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000153 // If there is a personality, we need to indicate the function's location.
154 if (PersonalityFn) {
155 EmitEncodingByte(PerEncoding, "Personality");
156 EmitReference(PersonalityFn, PerEncoding);
157 EOL("Personality");
158 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000159 if (UsesLSDA[Index])
160 EmitEncodingByte(LSDAEncoding, "LSDA");
161 if (FDEEncoding != dwarf::DW_EH_PE_absptr)
162 EmitEncodingByte(FDEEncoding, "FDE");
Bill Wendlingeb907212009-05-15 01:12:28 +0000163 }
164
165 // Indicate locations of general callee saved registers in frame.
166 std::vector<MachineMove> Moves;
167 RI->getInitialFrameState(Moves);
168 EmitFrameMoves(NULL, 0, Moves, true);
169
170 // On Darwin the linker honors the alignment of eh_frame, which means it must
171 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
172 // holes which confuse readers of eh_frame.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000173 Asm->EmitAlignment(TD->getPointerSize() == 4 ? 2 : 3, 0, 0, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000174 EmitLabel("eh_frame_common_end", Index);
Chris Lattner0ad9c912010-01-22 22:09:00 +0000175 Asm->O << '\n';
Bill Wendlingeb907212009-05-15 01:12:28 +0000176}
177
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000178/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
179void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000180 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000181 "Should not emit 'available externally' functions at all");
182
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000183 const Function *TheFunc = EHFrameInfo.function;
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000184 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000185
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000186 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
187 unsigned FDEEncoding = TLOF.getFDEEncoding();
188
189 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000190
Bill Wendlingeb907212009-05-15 01:12:28 +0000191 // Externally visible entry into the functions eh frame info. If the
192 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000193 if (!TheFunc->hasLocalLinkage())
Chris Lattner10b318b2010-01-17 21:43:43 +0000194 if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
195 O << GlobalEHDirective << *EHFrameInfo.FunctionEHSym << '\n';
Bill Wendlingeb907212009-05-15 01:12:28 +0000196
197 // If corresponding function is weak definition, this should be too.
Chris Lattner10b318b2010-01-17 21:43:43 +0000198 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
199 O << MAI->getWeakDefDirective() << *EHFrameInfo.FunctionEHSym << '\n';
Bill Wendlingee161a62009-11-11 01:24:59 +0000200
201 // If corresponding function is hidden, this should be too.
202 if (TheFunc->hasHiddenVisibility())
Chris Lattner152a29b2010-01-23 06:53:23 +0000203 if (MCSymbolAttr HiddenAttr = MAI->getHiddenVisibilityAttr())
204 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
205 HiddenAttr);
Bill Wendlingeb907212009-05-15 01:12:28 +0000206
207 // If there are no calls then you can't unwind. This may mean we can omit the
208 // EH Frame, but some environments do not handle weak absolute symbols. If
209 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
210 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000211 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
212 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000213 !MAI->getWeakDefDirective() ||
214 MAI->getSupportsWeakOmittedEHFrame())) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000215 O << *EHFrameInfo.FunctionEHSym << " = 0\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000216 // This name has no connection to the function, so it might get
217 // dead-stripped when the function is not, erroneously. Prohibit
218 // dead-stripping unconditionally.
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000219 if (MAI->hasNoDeadStrip())
220 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000221 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000222 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000223 O << *EHFrameInfo.FunctionEHSym << ":\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000224
225 // EH frame header.
226 EmitDifference("eh_frame_end", EHFrameInfo.Number,
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000227 "eh_frame_begin", EHFrameInfo.Number,
228 true);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000229 EOL("Length of Frame Information Entry");
Bill Wendlingeb907212009-05-15 01:12:28 +0000230
231 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
232
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000233 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
234 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
235 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000236
Chris Lattnerfaca5492010-01-22 23:47:11 +0000237 EOL("FDE CIE offset");
Bill Wendlingeb907212009-05-15 01:12:28 +0000238
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000239 EmitReference("eh_func_begin", EHFrameInfo.Number, FDEEncoding);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000240 EOL("FDE initial location");
Bill Wendlingeb907212009-05-15 01:12:28 +0000241 EmitDifference("eh_func_end", EHFrameInfo.Number,
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000242 "eh_func_begin", EHFrameInfo.Number,
243 SizeOfEncodedValue(FDEEncoding) == 4);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000244 EOL("FDE address range");
Bill Wendlingeb907212009-05-15 01:12:28 +0000245
246 // If there is a personality and landing pads then point to the language
247 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000248 if (MMI->getPersonalities()[0] != NULL) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000249 unsigned Size = SizeOfEncodedValue(LSDAEncoding);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000250
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000251 EmitULEB128(Size, "Augmentation size");
252 if (EHFrameInfo.hasLandingPads)
253 EmitReference("exception", EHFrameInfo.Number, LSDAEncoding);
254 else
255 Asm->OutStreamer.EmitIntValue(0, Size/*size*/, 0/*addrspace*/);
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000256
Chris Lattnerfaca5492010-01-22 23:47:11 +0000257 EOL("Language Specific Data Area");
Bill Wendlingeb907212009-05-15 01:12:28 +0000258 } else {
Chris Lattner894d75a2010-01-22 23:18:42 +0000259 EmitULEB128(0, "Augmentation size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000260 }
261
262 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000263 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000264 true);
265
266 // On Darwin the linker honors the alignment of eh_frame, which means it
267 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
268 // get holes which confuse readers of eh_frame.
269 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
270 0, 0, false);
271 EmitLabel("eh_frame_end", EHFrameInfo.Number);
272
273 // If the function is marked used, this table should be also. We cannot
274 // make the mark unconditional in this case, since retaining the table also
275 // retains the function in this case, and there is code around that depends
276 // on unused functions (calling undefined externals) being dead-stripped to
277 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000278 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000279 if (MAI->hasNoDeadStrip())
280 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000281 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000282 }
Chris Lattner0ad9c912010-01-22 22:09:00 +0000283 Asm->O << '\n';
Bill Wendlingeb907212009-05-15 01:12:28 +0000284}
285
Bill Wendlingeb907212009-05-15 01:12:28 +0000286/// SharedTypeIds - How many leading type ids two landing pads have in common.
287unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
288 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 unsigned Count = 0;
293
294 for (; Count != MinSize; ++Count)
295 if (LIds[Count] != RIds[Count])
296 return Count;
297
298 return Count;
299}
300
301/// PadLT - Order landing pads lexicographically by type id.
302bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
303 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
304 unsigned LSize = LIds.size(), RSize = RIds.size();
305 unsigned MinSize = LSize < RSize ? LSize : RSize;
306
307 for (unsigned i = 0; i != MinSize; ++i)
308 if (LIds[i] != RIds[i])
309 return LIds[i] < RIds[i];
310
311 return LSize < RSize;
312}
313
Bill Wendlingd4609622009-07-28 23:23:00 +0000314/// ComputeActionsTable - Compute the actions table and gather the first action
315/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000316unsigned DwarfException::
317ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
318 SmallVectorImpl<ActionEntry> &Actions,
319 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000320
321 // The action table follows the call-site table in the LSDA. The individual
322 // records are of two types:
323 //
324 // * Catch clause
325 // * Exception specification
326 //
327 // The two record kinds have the same format, with only small differences.
328 // They are distinguished by the "switch value" field: Catch clauses
329 // (TypeInfos) have strictly positive switch values, and exception
330 // specifications (FilterIds) have strictly negative switch values. Value 0
331 // indicates a catch-all clause.
332 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000333 // Negative type IDs index into FilterIds. Positive type IDs index into
334 // TypeInfos. The value written for a positive type ID is just the type ID
335 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000336 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000337 // offset is usually equal to the type ID (because the FilterIds entries are
338 // written using a variable width encoding, which outputs one byte per entry
339 // as long as the value written is not too large) but can differ. This kind
340 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000341 // output using a fixed width encoding. FilterOffsets[i] holds the byte
342 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000343
344 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000345 SmallVector<int, 16> FilterOffsets;
346 FilterOffsets.reserve(FilterIds.size());
347 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000348
349 for (std::vector<unsigned>::const_iterator
350 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000351 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000352 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000353 }
354
Bill Wendlingeb907212009-05-15 01:12:28 +0000355 FirstActions.reserve(LandingPads.size());
356
357 int FirstAction = 0;
358 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000359 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000360
Bill Wendling5cff4872009-07-28 23:44:43 +0000361 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000362 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
363 const LandingPadInfo *LPI = *I;
364 const std::vector<int> &TypeIds = LPI->TypeIds;
365 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000366 unsigned SizeSiteActions = 0;
367
368 if (NumShared < TypeIds.size()) {
369 unsigned SizeAction = 0;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000370 unsigned PrevAction = (unsigned)-1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000371
372 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000373 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000374 assert(Actions.size());
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000375 PrevAction = Actions.size() - 1;
376 SizeAction =
377 MCAsmInfo::getSLEB128Size(Actions[PrevAction].NextAction) +
378 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000379
380 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000381 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000382 SizeAction -=
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000383 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
384 SizeAction += -Actions[PrevAction].NextAction;
385 PrevAction = Actions[PrevAction].Previous;
Bill Wendlingeb907212009-05-15 01:12:28 +0000386 }
387 }
388
389 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000390 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
391 int TypeID = TypeIds[J];
392 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000393 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000394 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000395
396 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000397 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000398 SizeSiteActions += SizeAction;
399
Bill Wendlinga583c552009-08-20 22:02:24 +0000400 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000401 Actions.push_back(Action);
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000402 PrevAction = Actions.size() - 1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000403 }
404
405 // Record the first action of the landing pad site.
406 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
407 } // else identical - re-use previous FirstAction
408
Bill Wendlinga583c552009-08-20 22:02:24 +0000409 // Information used when created the call-site table. The action record
410 // field of the call site record is the offset of the first associated
411 // action record, relative to the start of the actions table. This value is
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000412 // biased by 1 (1 indicating the start of the actions table), and 0
Bill Wendlinga583c552009-08-20 22:02:24 +0000413 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000414 FirstActions.push_back(FirstAction);
415
416 // Compute this sites contribution to size.
417 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000418
419 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000420 }
421
Bill Wendling5e953dd2009-07-28 23:22:13 +0000422 return SizeActions;
423}
424
Bill Wendlinged060dc2009-11-12 21:59:20 +0000425/// CallToNoUnwindFunction - Return `true' if this is a call to a function
426/// marked `nounwind'. Return `false' otherwise.
427bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) {
428 assert(MI->getDesc().isCall() && "This should be a call instruction!");
429
430 bool MarkedNoUnwind = false;
431 bool SawFunc = false;
432
433 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
434 const MachineOperand &MO = MI->getOperand(I);
435
436 if (MO.isGlobal()) {
437 if (Function *F = dyn_cast<Function>(MO.getGlobal())) {
438 if (SawFunc) {
439 // Be conservative. If we have more than one function operand for this
440 // call, then we can't make the assumption that it's the callee and
441 // not a parameter to the call.
442 //
443 // FIXME: Determine if there's a way to say that `F' is the callee or
444 // parameter.
445 MarkedNoUnwind = false;
446 break;
447 }
Bill Wendlingecc260e2009-11-12 23:13:08 +0000448
449 MarkedNoUnwind = F->doesNotThrow();
450 SawFunc = true;
Bill Wendlinged060dc2009-11-12 21:59:20 +0000451 }
452 }
453 }
454
455 return MarkedNoUnwind;
456}
457
Bill Wendlingade025c2009-07-29 00:31:35 +0000458/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000459/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000460/// appropriate action. The entry for an ordinary call has a try-range
461/// containing the call and zero for the landing pad and the action. Calls
462/// marked 'nounwind' have no entry and must not be contained in the try-range
463/// of any entry - they form gaps in the table. Entries must be ordered by
464/// try-range address.
465void DwarfException::
466ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
467 const RangeMapType &PadMap,
468 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
469 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000470 // The end label of the previous invoke or nounwind try-range.
471 unsigned LastLabel = 0;
472
473 // Whether there is a potentially throwing instruction (currently this means
474 // an ordinary call) between the end of the previous try-range and now.
475 bool SawPotentiallyThrowing = false;
476
Bill Wendling5cff4872009-07-28 23:44:43 +0000477 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000478 bool PreviousIsInvoke = false;
479
480 // Visit all instructions in order of address.
481 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
482 I != E; ++I) {
483 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
484 MI != E; ++MI) {
485 if (!MI->isLabel()) {
Bill Wendlinged060dc2009-11-12 21:59:20 +0000486 if (MI->getDesc().isCall())
487 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI);
Bill Wendling73b55512009-11-11 23:17:02 +0000488
Bill Wendlingeb907212009-05-15 01:12:28 +0000489 continue;
490 }
491
492 unsigned BeginLabel = MI->getOperand(0).getImm();
493 assert(BeginLabel && "Invalid label!");
494
495 // End of the previous try-range?
496 if (BeginLabel == LastLabel)
497 SawPotentiallyThrowing = false;
498
499 // Beginning of a new try-range?
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000500 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
Bill Wendlingeb907212009-05-15 01:12:28 +0000501 if (L == PadMap.end())
502 // Nope, it was just some random label.
503 continue;
504
Bill Wendlinga583c552009-08-20 22:02:24 +0000505 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000506 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000507 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
508 "Inconsistent landing pad map!");
509
Bill Wendlinga583c552009-08-20 22:02:24 +0000510 // For Dwarf exception handling (SjLj handling doesn't use this). If some
511 // instruction between the previous try-range and this one may throw,
512 // create a call-site entry with no landing pad for the region between the
513 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000514 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000515 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000516 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000517 CallSites.push_back(Site);
518 PreviousIsInvoke = false;
519 }
520
521 LastLabel = LandingPad->EndLabels[P.RangeIndex];
522 assert(BeginLabel && LastLabel && "Invalid landing pad!");
523
524 if (LandingPad->LandingPadLabel) {
525 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000526 CallSiteEntry Site = {
527 BeginLabel,
528 LastLabel,
529 LandingPad->LandingPadLabel,
530 FirstActions[P.PadIndex]
531 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000532
Jim Grosbach33668c02009-09-01 17:19:13 +0000533 // Try to merge with the previous call-site. SJLJ doesn't do this
534 if (PreviousIsInvoke &&
535 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000536 CallSiteEntry &Prev = CallSites.back();
537 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
538 // Extend the range of the previous entry.
539 Prev.EndLabel = Site.EndLabel;
540 continue;
541 }
542 }
543
544 // Otherwise, create a new call-site.
Jim Grosbachca752c92010-01-28 01:45:32 +0000545 if (MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf)
546 CallSites.push_back(Site);
547 else {
548 // SjLj EH must maintain the call sites in the order assigned
549 // to them by the SjLjPrepare pass.
550 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
551 if (CallSites.size() < SiteNo)
552 CallSites.resize(SiteNo);
553 CallSites[SiteNo - 1] = Site;
554 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000555 PreviousIsInvoke = true;
556 } else {
557 // Create a gap.
558 PreviousIsInvoke = false;
559 }
560 }
561 }
562
563 // If some instruction between the previous try-range and the end of the
564 // function may throw, create a call-site entry with no landing pad for the
565 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000566 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000567 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000568 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000569 CallSites.push_back(Site);
570 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000571}
572
Bill Wendling0dafca92009-07-29 00:50:05 +0000573/// EmitExceptionTable - Emit landing pads and actions.
574///
575/// The general organization of the table is complex, but the basic concepts are
576/// easy. First there is a header which describes the location and organization
577/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000578///
Bill Wendling0dafca92009-07-29 00:50:05 +0000579/// 1. The landing pad site information describes the range of code covered by
580/// the try. In our case it's an accumulation of the ranges covered by the
581/// invokes in the try. There is also a reference to the landing pad that
582/// handles the exception once processed. Finally an index into the actions
583/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000584/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000585/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000586/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000587/// it matches then the exception and type id are passed on to the landing
588/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling28275fd2009-09-10 06:50:01 +0000589/// with a next action of zero. If no type id is found then the frame is
Bill Wendling0dafca92009-07-29 00:50:05 +0000590/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000591/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling28275fd2009-09-10 06:50:01 +0000592/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000593void DwarfException::EmitExceptionTable() {
594 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
595 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
596 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
597 if (PadInfos.empty()) return;
598
599 // Sort the landing pads in order of their type ids. This is used to fold
600 // duplicate actions.
601 SmallVector<const LandingPadInfo *, 64> LandingPads;
602 LandingPads.reserve(PadInfos.size());
603
604 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
605 LandingPads.push_back(&PadInfos[i]);
606
607 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
608
609 // Compute the actions table and gather the first action index for each
610 // landing pad site.
611 SmallVector<ActionEntry, 32> Actions;
612 SmallVector<unsigned, 64> FirstActions;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000613 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000614
615 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
616 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling28275fd2009-09-10 06:50:01 +0000617 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000618 RangeMapType PadMap;
619 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
620 const LandingPadInfo *LandingPad = LandingPads[i];
621 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
622 unsigned BeginLabel = LandingPad->BeginLabels[j];
623 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
624 PadRange P = { i, j };
625 PadMap[BeginLabel] = P;
626 }
627 }
628
629 // Compute the call-site table.
630 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000631 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000632
633 // Final tallies.
634
635 // Call sites.
Bill Wendling40121bc2009-09-10 00:13:16 +0000636 const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
637 const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
638 const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000639 bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000640 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Bill Wendling3dc9b482010-02-24 23:34:35 +0000641 unsigned CallSiteTableLength;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000642
643 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000644 CallSiteTableLength = 0;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000645 else
Bill Wendling3dc9b482010-02-24 23:34:35 +0000646 CallSiteTableLength = CallSites.size() *
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000647 (SiteStartSize + SiteLengthSize + LandingPadSize);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000648
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000649 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Bill Wendling3dc9b482010-02-24 23:34:35 +0000650 CallSiteTableLength += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000651 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000652 CallSiteTableLength += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000653 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000654
Bill Wendlingeb907212009-05-15 01:12:28 +0000655 // Type infos.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000656 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000657 unsigned TTypeEncoding;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000658 unsigned TypeFormatSize;
Bill Wendlingeb907212009-05-15 01:12:28 +0000659
Bill Wendling43e484f2009-09-10 01:12:47 +0000660 if (!HaveTTData) {
Bill Wendling28275fd2009-09-10 06:50:01 +0000661 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
662 // that we're omitting that bit.
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000663 TTypeEncoding = dwarf::DW_EH_PE_omit;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000664 TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr);
Chris Lattner81c9a062009-07-31 22:03:47 +0000665 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000666 // Okay, we have actual filters or typeinfos to emit. As such, we need to
667 // pick a type encoding for them. We're about to emit a list of pointers to
668 // typeinfo objects at the end of the LSDA. However, unless we're in static
669 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000670 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000671 // Because of this, we have a couple of options:
Bill Wendling28275fd2009-09-10 06:50:01 +0000672 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000673 // 1) If we are in -static mode, we can always use an absolute reference
674 // from the LSDA, because the static linker will resolve it.
Bill Wendling28275fd2009-09-10 06:50:01 +0000675 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000676 // 2) Otherwise, if the LSDA section is writable, we can output the direct
677 // reference to the typeinfo and allow the dynamic linker to relocate
678 // it. Since it is in a writable section, the dynamic linker won't
679 // have a problem.
Bill Wendling28275fd2009-09-10 06:50:01 +0000680 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000681 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
682 // we need to use some form of indirection. For example, on Darwin,
683 // we can output a statically-relocatable reference to a dyld stub. The
684 // offset to the stub is constant, but the contents are in a section
685 // that is updated by the dynamic linker. This is easy enough, but we
686 // need to tell the personality function of the unwinder to indirect
687 // through the dyld stub.
688 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000689 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattnerad88bc42009-08-02 03:59:56 +0000690 // somewhere. This predicate should be moved to a shared location that is
691 // in target-independent code.
692 //
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000693 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
694 TypeFormatSize = SizeOfEncodedValue(TTypeEncoding);
Bill Wendlingfe220282009-09-10 02:07:37 +0000695 }
Bill Wendling43e484f2009-09-10 01:12:47 +0000696
Bill Wendlingfe220282009-09-10 02:07:37 +0000697 // Begin the exception table.
698 Asm->OutStreamer.SwitchSection(LSDASection);
699 Asm->EmitAlignment(2, 0, 0, false);
Bill Wendling43e484f2009-09-10 01:12:47 +0000700
Bill Wendling3dc9b482010-02-24 23:34:35 +0000701 // Emit the LSDA.
Bill Wendlingfe220282009-09-10 02:07:37 +0000702 O << "GCC_except_table" << SubprogramCount << ":\n";
703 EmitLabel("exception", SubprogramCount);
704
705 if (IsSJLJ) {
706 SmallString<16> LSDAName;
707 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
708 "_LSDA_" << Asm->getFunctionNumber();
709 O << LSDAName.str() << ":\n";
710 }
711
Bill Wendling3dc9b482010-02-24 23:34:35 +0000712 // Emit the LSDA header.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000713 EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000714 EmitEncodingByte(TTypeEncoding, "@TType");
Bill Wendlingfe220282009-09-10 02:07:37 +0000715
Bill Wendling3dc9b482010-02-24 23:34:35 +0000716 // The type infos need to be aligned. GCC does this by inserting padding just
717 // before the type infos. However, this changes the size of the exception
718 // table, so you need to take this into account when you output the exception
719 // table size. However, the size is output using a variable length encoding.
720 // So by increasing the size by inserting padding, you may increase the number
721 // of bytes used for writing the size. If it increases, say by one byte, then
722 // you now need to output one less byte of padding to get the type infos
723 // aligned. However this decreases the size of the exception table. This
724 // changes the value you have to output for the exception table size. Due to
725 // the variable length encoding, the number of bytes used for writing the
726 // length may decrease. If so, you then have to increase the amount of
727 // padding. And so on. If you look carefully at the GCC code you will see that
728 // it indeed does this in a loop, going on and on until the values stabilize.
729 // We chose another solution: don't output padding inside the table like GCC
730 // does, instead output it before the table.
731 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
732 unsigned CallSiteTableLengthSize =
733 MCAsmInfo::getULEB128Size(CallSiteTableLength);
734 unsigned TTypeBaseOffset =
735 sizeof(int8_t) + // Call site format
736 CallSiteTableLengthSize + // Call site table length size
737 CallSiteTableLength + // Call site table length
738 SizeActions + // Actions size
739 SizeTypes;
740 unsigned TTypeBaseOffsetSize = MCAsmInfo::getULEB128Size(TTypeBaseOffset);
741 unsigned TotalSize =
742 sizeof(int8_t) + // LPStart format
743 sizeof(int8_t) + // TType format
744 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size
745 TTypeBaseOffset; // TType base offset
746 unsigned SizeAlign = (4 - TotalSize) & 3;
747
Chris Lattner894d75a2010-01-22 23:18:42 +0000748 if (HaveTTData)
Bill Wendlingf7e90ae2010-02-25 21:19:47 +0000749 // Account for any extra padded that will be added to the call site table
750 // length.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000751 EmitULEB128(TTypeBaseOffset + SizeAlign, "@TType base offset");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000752
Bill Wendling28275fd2009-09-10 06:50:01 +0000753 // SjLj Exception handling
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000754 if (IsSJLJ) {
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000755 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling3dc9b482010-02-24 23:34:35 +0000756 EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000757
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000758 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000759 unsigned idx = 0;
760 for (SmallVectorImpl<CallSiteEntry>::const_iterator
761 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
762 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000763
764 // Offset of the landing pad, counted in 16-byte bundles relative to the
765 // @LPStart address.
Chris Lattner894d75a2010-01-22 23:18:42 +0000766 EmitULEB128(idx, "Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000767
768 // Offset of the first associated action record, relative to the start of
769 // the action table. This value is biased by 1 (1 indicates the start of
770 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000771 EmitULEB128(S.Action, "Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000772 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000773 } else {
774 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000775 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000776
Bill Wendlinga583c552009-08-20 22:02:24 +0000777 // The call-site table is a list of all call sites that may throw an
778 // exception (including C++ 'throw' statements) in the procedure
779 // fragment. It immediately follows the LSDA header. Each entry indicates,
780 // for a given call, the first corresponding action record and corresponding
781 // landing pad.
782 //
783 // The table begins with the number of bytes, stored as an LEB128
784 // compressed, unsigned integer. The records immediately follow the record
785 // count. They are sorted in increasing call-site address. Each record
786 // indicates:
787 //
788 // * The position of the call-site.
789 // * The position of the landing pad.
790 // * The first action record for that call site.
791 //
792 // A missing entry in the call-site table indicates that a call is not
Bill Wendling28275fd2009-09-10 06:50:01 +0000793 // supposed to throw.
Bill Wendlinga583c552009-08-20 22:02:24 +0000794
795 // Emit the landing pad call site table.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000796 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling3dc9b482010-02-24 23:34:35 +0000797 EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000798
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000799 for (SmallVectorImpl<CallSiteEntry>::const_iterator
800 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
801 const CallSiteEntry &S = *I;
802 const char *BeginTag;
803 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000804
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000805 if (!S.BeginLabel) {
806 BeginTag = "eh_func_begin";
807 BeginNumber = SubprogramCount;
808 } else {
809 BeginTag = "label";
810 BeginNumber = S.BeginLabel;
811 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000812
Bill Wendlinga583c552009-08-20 22:02:24 +0000813 // Offset of the call site relative to the previous call site, counted in
814 // number of 16-byte bundles. The first call site is counted relative to
815 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000816 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000817 true, true);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000818 EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000819
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000820 if (!S.EndLabel)
821 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
822 true);
823 else
824 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000825
Chris Lattnerfaca5492010-01-22 23:47:11 +0000826 EOL("Region length");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000827
Bill Wendlinga583c552009-08-20 22:02:24 +0000828 // Offset of the landing pad, counted in 16-byte bundles relative to the
829 // @LPStart address.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000830 if (!S.PadLabel) {
831 Asm->OutStreamer.AddComment("Landing pad");
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000832 Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Bill Wendling1f8075d2010-02-09 22:49:16 +0000833 } else {
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000834 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
835 true, true);
Bill Wendling1f8075d2010-02-09 22:49:16 +0000836 EOL("Landing pad");
837 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000838
Bill Wendlinga583c552009-08-20 22:02:24 +0000839 // Offset of the first associated action record, relative to the start of
840 // the action table. This value is biased by 1 (1 indicates the start of
841 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000842 EmitULEB128(S.Action, "Action");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000843 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000844 }
845
Bill Wendlinga583c552009-08-20 22:02:24 +0000846 // Emit the Action Table.
Bill Wendling35c187b2010-02-10 00:45:28 +0000847 if (Actions.size() != 0) EOL("-- Action Record Table --");
Bill Wendling5cff4872009-07-28 23:44:43 +0000848 for (SmallVectorImpl<ActionEntry>::const_iterator
849 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
850 const ActionEntry &Action = *I;
Bill Wendling35c187b2010-02-10 00:45:28 +0000851 EOL("Action Record:");
Bill Wendlinga583c552009-08-20 22:02:24 +0000852
853 // Type Filter
854 //
855 // Used by the runtime to match the type of the thrown exception to the
856 // type of the catch clauses or the types in the exception specification.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000857 EmitSLEB128(Action.ValueForTypeID, " TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000858
859 // Action Record
860 //
861 // Self-relative signed displacement in bytes of the next action record,
862 // or 0 if there is no next action record.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000863 EmitSLEB128(Action.NextAction, " Next action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000864 }
865
Bill Wendling48dc29e2009-10-22 20:48:59 +0000866 // Emit the Catch TypeInfos.
Bill Wendlinge9d10a62010-02-11 10:37:57 +0000867 if (!TypeInfos.empty()) EOL("-- Catch TypeInfos --");
Bill Wendlingec044582009-11-18 23:18:46 +0000868 for (std::vector<GlobalVariable *>::const_reverse_iterator
Bill Wendling01c69372009-11-19 00:09:14 +0000869 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000870 const GlobalVariable *GV = *I;
Bill Wendlingec044582009-11-18 23:18:46 +0000871
Bill Wendling1f8075d2010-02-09 22:49:16 +0000872 if (GV) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000873 EmitReference(GV, TTypeEncoding);
Bill Wendlingd3a47a32010-02-10 00:59:47 +0000874 EOL("TypeInfo");
Bill Wendling1f8075d2010-02-09 22:49:16 +0000875 } else {
Anton Korobeynikov0b217302010-02-15 22:38:25 +0000876 PrintRelDirective(TTypeEncoding);
Bill Wendling049e98d2009-08-31 18:26:48 +0000877 O << "0x0";
Bill Wendling1f8075d2010-02-09 22:49:16 +0000878 EOL("");
879 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000880 }
881
Bill Wendling48dc29e2009-10-22 20:48:59 +0000882 // Emit the Exception Specifications.
Bill Wendlinge9d10a62010-02-11 10:37:57 +0000883 if (!FilterIds.empty()) EOL("-- Filter IDs --");
Bill Wendling5cff4872009-07-28 23:44:43 +0000884 for (std::vector<unsigned>::const_iterator
885 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
886 unsigned TypeID = *I;
Chris Lattner894d75a2010-01-22 23:18:42 +0000887 EmitULEB128(TypeID, TypeID != 0 ? "Exception specification" : 0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000888 }
889
890 Asm->EmitAlignment(2, 0, 0, false);
891}
892
Bill Wendlingeb907212009-05-15 01:12:28 +0000893/// EndModule - Emit all exception information that should come after the
894/// content.
895void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000896 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000897 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000898
Bill Wendling52783c62009-09-09 23:56:55 +0000899 if (!shouldEmitMovesModule && !shouldEmitTableModule)
900 return;
901
Bill Wendlingeb907212009-05-15 01:12:28 +0000902 if (TimePassesIsEnabled)
903 ExceptionTimer->startTimer();
904
Bill Wendling52783c62009-09-09 23:56:55 +0000905 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000906
Bill Wendling28275fd2009-09-10 06:50:01 +0000907 for (unsigned I = 0, E = Personalities.size(); I < E; ++I)
908 EmitCIE(Personalities[I], I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000909
Bill Wendling52783c62009-09-09 23:56:55 +0000910 for (std::vector<FunctionEHFrameInfo>::iterator
911 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
912 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000913
914 if (TimePassesIsEnabled)
915 ExceptionTimer->stopTimer();
916}
917
Bill Wendling28275fd2009-09-10 06:50:01 +0000918/// BeginFunction - Gather pre-function exception information. Assumes it's
919/// being emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +0000920void DwarfException::BeginFunction(const MachineFunction *MF) {
Bill Wendling73c5a612009-09-10 18:28:06 +0000921 if (!MMI || !MAI->doesSupportExceptionHandling()) return;
922
Bill Wendlingeb907212009-05-15 01:12:28 +0000923 if (TimePassesIsEnabled)
924 ExceptionTimer->startTimer();
925
926 this->MF = MF;
927 shouldEmitTable = shouldEmitMoves = false;
928
Bill Wendling73c5a612009-09-10 18:28:06 +0000929 // Map all labels and get rid of any dead landing pads.
930 MMI->TidyLandingPads();
Bill Wendlingeb907212009-05-15 01:12:28 +0000931
Bill Wendling73c5a612009-09-10 18:28:06 +0000932 // If any landing pads survive, we need an EH table.
933 if (!MMI->getLandingPads().empty())
934 shouldEmitTable = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000935
Bill Wendling73c5a612009-09-10 18:28:06 +0000936 // See if we need frame move info.
937 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
938 shouldEmitMoves = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000939
Bill Wendling73c5a612009-09-10 18:28:06 +0000940 if (shouldEmitMoves || shouldEmitTable)
941 // Assumes in correct section after the entry point.
942 EmitLabel("eh_func_begin", ++SubprogramCount);
Bill Wendlingeb907212009-05-15 01:12:28 +0000943
944 shouldEmitTableModule |= shouldEmitTable;
945 shouldEmitMovesModule |= shouldEmitMoves;
946
947 if (TimePassesIsEnabled)
948 ExceptionTimer->stopTimer();
949}
950
951/// EndFunction - Gather and emit post-function exception information.
952///
953void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000954 if (!shouldEmitMoves && !shouldEmitTable) return;
955
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000956 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000957 ExceptionTimer->startTimer();
958
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000959 EmitLabel("eh_func_end", SubprogramCount);
960 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000961
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000962 MCSymbol *FunctionEHSym =
Chris Lattner7a2ba942010-01-16 18:37:32 +0000963 Asm->GetSymbolWithGlobalValueBase(MF->getFunction(), ".eh",
964 Asm->MAI->is_EHSymbolPrivate());
Chris Lattner25d812b2009-09-16 00:35:39 +0000965
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000966 // Save EH frame information
Chris Lattner7a2ba942010-01-16 18:37:32 +0000967 EHFrames.push_back(FunctionEHFrameInfo(FunctionEHSym, SubprogramCount,
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000968 MMI->getPersonalityIndex(),
969 MF->getFrameInfo()->hasCalls(),
970 !MMI->getLandingPads().empty(),
971 MMI->getFrameMoves(),
972 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +0000973
Bill Wendling52783c62009-09-09 23:56:55 +0000974 // Record if this personality index uses a landing pad.
975 UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
976
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000977 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000978 ExceptionTimer->stopTimer();
979}