blob: f00e8e19038681fa8b3b3c9715e20489e84fd7a7 [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 Lattnerb98b1bf2010-03-08 22:23:36 +0000233 EmitSectionOffset(getDWLabel("eh_frame_begin", EHFrameInfo.Number),
234 getDWLabel("eh_frame_common",
235 EHFrameInfo.PersonalityIndex),
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000236 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000237
Chris Lattnerfaca5492010-01-22 23:47:11 +0000238 EOL("FDE CIE offset");
Bill Wendlingeb907212009-05-15 01:12:28 +0000239
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000240 EmitReference("eh_func_begin", EHFrameInfo.Number, FDEEncoding);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000241 EOL("FDE initial location");
Bill Wendlingeb907212009-05-15 01:12:28 +0000242 EmitDifference("eh_func_end", EHFrameInfo.Number,
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000243 "eh_func_begin", EHFrameInfo.Number,
244 SizeOfEncodedValue(FDEEncoding) == 4);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000245 EOL("FDE address range");
Bill Wendlingeb907212009-05-15 01:12:28 +0000246
247 // If there is a personality and landing pads then point to the language
248 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000249 if (MMI->getPersonalities()[0] != NULL) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000250 unsigned Size = SizeOfEncodedValue(LSDAEncoding);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000251
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000252 EmitULEB128(Size, "Augmentation size");
253 if (EHFrameInfo.hasLandingPads)
254 EmitReference("exception", EHFrameInfo.Number, LSDAEncoding);
255 else
256 Asm->OutStreamer.EmitIntValue(0, Size/*size*/, 0/*addrspace*/);
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000257
Chris Lattnerfaca5492010-01-22 23:47:11 +0000258 EOL("Language Specific Data Area");
Bill Wendlingeb907212009-05-15 01:12:28 +0000259 } else {
Chris Lattner894d75a2010-01-22 23:18:42 +0000260 EmitULEB128(0, "Augmentation size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000261 }
262
263 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000264 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000265 true);
266
267 // On Darwin the linker honors the alignment of eh_frame, which means it
268 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
269 // get holes which confuse readers of eh_frame.
270 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
271 0, 0, false);
272 EmitLabel("eh_frame_end", EHFrameInfo.Number);
273
274 // If the function is marked used, this table should be also. We cannot
275 // make the mark unconditional in this case, since retaining the table also
276 // retains the function in this case, and there is code around that depends
277 // on unused functions (calling undefined externals) being dead-stripped to
278 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000279 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000280 if (MAI->hasNoDeadStrip())
281 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000282 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000283 }
Chris Lattner0ad9c912010-01-22 22:09:00 +0000284 Asm->O << '\n';
Bill Wendlingeb907212009-05-15 01:12:28 +0000285}
286
Bill Wendlingeb907212009-05-15 01:12:28 +0000287/// SharedTypeIds - How many leading type ids two landing pads have in common.
288unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
289 const LandingPadInfo *R) {
290 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
291 unsigned LSize = LIds.size(), RSize = RIds.size();
292 unsigned MinSize = LSize < RSize ? LSize : RSize;
293 unsigned Count = 0;
294
295 for (; Count != MinSize; ++Count)
296 if (LIds[Count] != RIds[Count])
297 return Count;
298
299 return Count;
300}
301
302/// PadLT - Order landing pads lexicographically by type id.
303bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
304 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
305 unsigned LSize = LIds.size(), RSize = RIds.size();
306 unsigned MinSize = LSize < RSize ? LSize : RSize;
307
308 for (unsigned i = 0; i != MinSize; ++i)
309 if (LIds[i] != RIds[i])
310 return LIds[i] < RIds[i];
311
312 return LSize < RSize;
313}
314
Bill Wendlingd4609622009-07-28 23:23:00 +0000315/// ComputeActionsTable - Compute the actions table and gather the first action
316/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000317unsigned DwarfException::
318ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
319 SmallVectorImpl<ActionEntry> &Actions,
320 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000321
322 // The action table follows the call-site table in the LSDA. The individual
323 // records are of two types:
324 //
325 // * Catch clause
326 // * Exception specification
327 //
328 // The two record kinds have the same format, with only small differences.
329 // They are distinguished by the "switch value" field: Catch clauses
330 // (TypeInfos) have strictly positive switch values, and exception
331 // specifications (FilterIds) have strictly negative switch values. Value 0
332 // indicates a catch-all clause.
333 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000334 // Negative type IDs index into FilterIds. Positive type IDs index into
335 // TypeInfos. The value written for a positive type ID is just the type ID
336 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000337 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000338 // offset is usually equal to the type ID (because the FilterIds entries are
339 // written using a variable width encoding, which outputs one byte per entry
340 // as long as the value written is not too large) but can differ. This kind
341 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000342 // output using a fixed width encoding. FilterOffsets[i] holds the byte
343 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000344
345 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000346 SmallVector<int, 16> FilterOffsets;
347 FilterOffsets.reserve(FilterIds.size());
348 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000349
350 for (std::vector<unsigned>::const_iterator
351 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000352 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000353 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000354 }
355
Bill Wendlingeb907212009-05-15 01:12:28 +0000356 FirstActions.reserve(LandingPads.size());
357
358 int FirstAction = 0;
359 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000360 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000361
Bill Wendling5cff4872009-07-28 23:44:43 +0000362 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000363 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
364 const LandingPadInfo *LPI = *I;
365 const std::vector<int> &TypeIds = LPI->TypeIds;
366 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000367 unsigned SizeSiteActions = 0;
368
369 if (NumShared < TypeIds.size()) {
370 unsigned SizeAction = 0;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000371 unsigned PrevAction = (unsigned)-1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000372
373 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000374 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000375 assert(Actions.size());
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000376 PrevAction = Actions.size() - 1;
377 SizeAction =
378 MCAsmInfo::getSLEB128Size(Actions[PrevAction].NextAction) +
379 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000380
381 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000382 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000383 SizeAction -=
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000384 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
385 SizeAction += -Actions[PrevAction].NextAction;
386 PrevAction = Actions[PrevAction].Previous;
Bill Wendlingeb907212009-05-15 01:12:28 +0000387 }
388 }
389
390 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000391 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
392 int TypeID = TypeIds[J];
393 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000394 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000395 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000396
397 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000398 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000399 SizeSiteActions += SizeAction;
400
Bill Wendlinga583c552009-08-20 22:02:24 +0000401 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000402 Actions.push_back(Action);
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000403 PrevAction = Actions.size() - 1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000404 }
405
406 // Record the first action of the landing pad site.
407 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
408 } // else identical - re-use previous FirstAction
409
Bill Wendlinga583c552009-08-20 22:02:24 +0000410 // Information used when created the call-site table. The action record
411 // field of the call site record is the offset of the first associated
412 // action record, relative to the start of the actions table. This value is
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000413 // biased by 1 (1 indicating the start of the actions table), and 0
Bill Wendlinga583c552009-08-20 22:02:24 +0000414 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000415 FirstActions.push_back(FirstAction);
416
417 // Compute this sites contribution to size.
418 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000419
420 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000421 }
422
Bill Wendling5e953dd2009-07-28 23:22:13 +0000423 return SizeActions;
424}
425
Bill Wendlinged060dc2009-11-12 21:59:20 +0000426/// CallToNoUnwindFunction - Return `true' if this is a call to a function
427/// marked `nounwind'. Return `false' otherwise.
428bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) {
429 assert(MI->getDesc().isCall() && "This should be a call instruction!");
430
431 bool MarkedNoUnwind = false;
432 bool SawFunc = false;
433
434 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
435 const MachineOperand &MO = MI->getOperand(I);
436
437 if (MO.isGlobal()) {
438 if (Function *F = dyn_cast<Function>(MO.getGlobal())) {
439 if (SawFunc) {
440 // Be conservative. If we have more than one function operand for this
441 // call, then we can't make the assumption that it's the callee and
442 // not a parameter to the call.
443 //
444 // FIXME: Determine if there's a way to say that `F' is the callee or
445 // parameter.
446 MarkedNoUnwind = false;
447 break;
448 }
Bill Wendlingecc260e2009-11-12 23:13:08 +0000449
450 MarkedNoUnwind = F->doesNotThrow();
451 SawFunc = true;
Bill Wendlinged060dc2009-11-12 21:59:20 +0000452 }
453 }
454 }
455
456 return MarkedNoUnwind;
457}
458
Bill Wendlingade025c2009-07-29 00:31:35 +0000459/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000460/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000461/// appropriate action. The entry for an ordinary call has a try-range
462/// containing the call and zero for the landing pad and the action. Calls
463/// marked 'nounwind' have no entry and must not be contained in the try-range
464/// of any entry - they form gaps in the table. Entries must be ordered by
465/// try-range address.
466void DwarfException::
467ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
468 const RangeMapType &PadMap,
469 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
470 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000471 // The end label of the previous invoke or nounwind try-range.
472 unsigned LastLabel = 0;
473
474 // Whether there is a potentially throwing instruction (currently this means
475 // an ordinary call) between the end of the previous try-range and now.
476 bool SawPotentiallyThrowing = false;
477
Bill Wendling5cff4872009-07-28 23:44:43 +0000478 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000479 bool PreviousIsInvoke = false;
480
481 // Visit all instructions in order of address.
482 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
483 I != E; ++I) {
484 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
485 MI != E; ++MI) {
486 if (!MI->isLabel()) {
Bill Wendlinged060dc2009-11-12 21:59:20 +0000487 if (MI->getDesc().isCall())
488 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI);
Bill Wendling73b55512009-11-11 23:17:02 +0000489
Bill Wendlingeb907212009-05-15 01:12:28 +0000490 continue;
491 }
492
493 unsigned BeginLabel = MI->getOperand(0).getImm();
494 assert(BeginLabel && "Invalid label!");
495
496 // End of the previous try-range?
497 if (BeginLabel == LastLabel)
498 SawPotentiallyThrowing = false;
499
500 // Beginning of a new try-range?
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000501 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
Bill Wendlingeb907212009-05-15 01:12:28 +0000502 if (L == PadMap.end())
503 // Nope, it was just some random label.
504 continue;
505
Bill Wendlinga583c552009-08-20 22:02:24 +0000506 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000507 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000508 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
509 "Inconsistent landing pad map!");
510
Bill Wendlinga583c552009-08-20 22:02:24 +0000511 // For Dwarf exception handling (SjLj handling doesn't use this). If some
512 // instruction between the previous try-range and this one may throw,
513 // create a call-site entry with no landing pad for the region between the
514 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000515 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000516 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000517 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000518 CallSites.push_back(Site);
519 PreviousIsInvoke = false;
520 }
521
522 LastLabel = LandingPad->EndLabels[P.RangeIndex];
523 assert(BeginLabel && LastLabel && "Invalid landing pad!");
524
525 if (LandingPad->LandingPadLabel) {
526 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000527 CallSiteEntry Site = {
528 BeginLabel,
529 LastLabel,
530 LandingPad->LandingPadLabel,
531 FirstActions[P.PadIndex]
532 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000533
Jim Grosbach33668c02009-09-01 17:19:13 +0000534 // Try to merge with the previous call-site. SJLJ doesn't do this
535 if (PreviousIsInvoke &&
536 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000537 CallSiteEntry &Prev = CallSites.back();
538 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
539 // Extend the range of the previous entry.
540 Prev.EndLabel = Site.EndLabel;
541 continue;
542 }
543 }
544
545 // Otherwise, create a new call-site.
Jim Grosbachca752c92010-01-28 01:45:32 +0000546 if (MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf)
547 CallSites.push_back(Site);
548 else {
549 // SjLj EH must maintain the call sites in the order assigned
550 // to them by the SjLjPrepare pass.
551 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
552 if (CallSites.size() < SiteNo)
553 CallSites.resize(SiteNo);
554 CallSites[SiteNo - 1] = Site;
555 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000556 PreviousIsInvoke = true;
557 } else {
558 // Create a gap.
559 PreviousIsInvoke = false;
560 }
561 }
562 }
563
564 // If some instruction between the previous try-range and the end of the
565 // function may throw, create a call-site entry with no landing pad for the
566 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000567 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000568 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000569 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000570 CallSites.push_back(Site);
571 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000572}
573
Bill Wendling0dafca92009-07-29 00:50:05 +0000574/// EmitExceptionTable - Emit landing pads and actions.
575///
576/// The general organization of the table is complex, but the basic concepts are
577/// easy. First there is a header which describes the location and organization
578/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000579///
Bill Wendling0dafca92009-07-29 00:50:05 +0000580/// 1. The landing pad site information describes the range of code covered by
581/// the try. In our case it's an accumulation of the ranges covered by the
582/// invokes in the try. There is also a reference to the landing pad that
583/// handles the exception once processed. Finally an index into the actions
584/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000585/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000586/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000587/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000588/// it matches then the exception and type id are passed on to the landing
589/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling28275fd2009-09-10 06:50:01 +0000590/// with a next action of zero. If no type id is found then the frame is
Bill Wendling0dafca92009-07-29 00:50:05 +0000591/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000592/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling28275fd2009-09-10 06:50:01 +0000593/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000594void DwarfException::EmitExceptionTable() {
595 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
596 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
597 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
598 if (PadInfos.empty()) return;
599
600 // Sort the landing pads in order of their type ids. This is used to fold
601 // duplicate actions.
602 SmallVector<const LandingPadInfo *, 64> LandingPads;
603 LandingPads.reserve(PadInfos.size());
604
605 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
606 LandingPads.push_back(&PadInfos[i]);
607
608 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
609
610 // Compute the actions table and gather the first action index for each
611 // landing pad site.
612 SmallVector<ActionEntry, 32> Actions;
613 SmallVector<unsigned, 64> FirstActions;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000614 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000615
616 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
617 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling28275fd2009-09-10 06:50:01 +0000618 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000619 RangeMapType PadMap;
620 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
621 const LandingPadInfo *LandingPad = LandingPads[i];
622 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
623 unsigned BeginLabel = LandingPad->BeginLabels[j];
624 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
625 PadRange P = { i, j };
626 PadMap[BeginLabel] = P;
627 }
628 }
629
630 // Compute the call-site table.
631 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000632 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000633
634 // Final tallies.
635
636 // Call sites.
Bill Wendling40121bc2009-09-10 00:13:16 +0000637 const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
638 const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
639 const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000640 bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000641 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Bill Wendling3dc9b482010-02-24 23:34:35 +0000642 unsigned CallSiteTableLength;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000643
644 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000645 CallSiteTableLength = 0;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000646 else
Bill Wendling3dc9b482010-02-24 23:34:35 +0000647 CallSiteTableLength = CallSites.size() *
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000648 (SiteStartSize + SiteLengthSize + LandingPadSize);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000649
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000650 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Bill Wendling3dc9b482010-02-24 23:34:35 +0000651 CallSiteTableLength += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000652 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000653 CallSiteTableLength += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000654 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000655
Bill Wendlingeb907212009-05-15 01:12:28 +0000656 // Type infos.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000657 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000658 unsigned TTypeEncoding;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000659 unsigned TypeFormatSize;
Bill Wendlingeb907212009-05-15 01:12:28 +0000660
Bill Wendling43e484f2009-09-10 01:12:47 +0000661 if (!HaveTTData) {
Bill Wendling28275fd2009-09-10 06:50:01 +0000662 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
663 // that we're omitting that bit.
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000664 TTypeEncoding = dwarf::DW_EH_PE_omit;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000665 TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr);
Chris Lattner81c9a062009-07-31 22:03:47 +0000666 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000667 // Okay, we have actual filters or typeinfos to emit. As such, we need to
668 // pick a type encoding for them. We're about to emit a list of pointers to
669 // typeinfo objects at the end of the LSDA. However, unless we're in static
670 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000671 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000672 // Because of this, we have a couple of options:
Bill Wendling28275fd2009-09-10 06:50:01 +0000673 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000674 // 1) If we are in -static mode, we can always use an absolute reference
675 // from the LSDA, because the static linker will resolve it.
Bill Wendling28275fd2009-09-10 06:50:01 +0000676 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000677 // 2) Otherwise, if the LSDA section is writable, we can output the direct
678 // reference to the typeinfo and allow the dynamic linker to relocate
679 // it. Since it is in a writable section, the dynamic linker won't
680 // have a problem.
Bill Wendling28275fd2009-09-10 06:50:01 +0000681 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000682 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
683 // we need to use some form of indirection. For example, on Darwin,
684 // we can output a statically-relocatable reference to a dyld stub. The
685 // offset to the stub is constant, but the contents are in a section
686 // that is updated by the dynamic linker. This is easy enough, but we
687 // need to tell the personality function of the unwinder to indirect
688 // through the dyld stub.
689 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000690 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattnerad88bc42009-08-02 03:59:56 +0000691 // somewhere. This predicate should be moved to a shared location that is
692 // in target-independent code.
693 //
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000694 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
695 TypeFormatSize = SizeOfEncodedValue(TTypeEncoding);
Bill Wendlingfe220282009-09-10 02:07:37 +0000696 }
Bill Wendling43e484f2009-09-10 01:12:47 +0000697
Bill Wendlingfe220282009-09-10 02:07:37 +0000698 // Begin the exception table.
699 Asm->OutStreamer.SwitchSection(LSDASection);
700 Asm->EmitAlignment(2, 0, 0, false);
Bill Wendling43e484f2009-09-10 01:12:47 +0000701
Bill Wendling3dc9b482010-02-24 23:34:35 +0000702 // Emit the LSDA.
Bill Wendlingfe220282009-09-10 02:07:37 +0000703 O << "GCC_except_table" << SubprogramCount << ":\n";
704 EmitLabel("exception", SubprogramCount);
705
706 if (IsSJLJ) {
707 SmallString<16> LSDAName;
708 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
709 "_LSDA_" << Asm->getFunctionNumber();
710 O << LSDAName.str() << ":\n";
711 }
712
Bill Wendling3dc9b482010-02-24 23:34:35 +0000713 // Emit the LSDA header.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000714 EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000715 EmitEncodingByte(TTypeEncoding, "@TType");
Bill Wendlingfe220282009-09-10 02:07:37 +0000716
Bill Wendling3dc9b482010-02-24 23:34:35 +0000717 // The type infos need to be aligned. GCC does this by inserting padding just
718 // before the type infos. However, this changes the size of the exception
719 // table, so you need to take this into account when you output the exception
720 // table size. However, the size is output using a variable length encoding.
721 // So by increasing the size by inserting padding, you may increase the number
722 // of bytes used for writing the size. If it increases, say by one byte, then
723 // you now need to output one less byte of padding to get the type infos
724 // aligned. However this decreases the size of the exception table. This
725 // changes the value you have to output for the exception table size. Due to
726 // the variable length encoding, the number of bytes used for writing the
727 // length may decrease. If so, you then have to increase the amount of
728 // padding. And so on. If you look carefully at the GCC code you will see that
729 // it indeed does this in a loop, going on and on until the values stabilize.
730 // We chose another solution: don't output padding inside the table like GCC
731 // does, instead output it before the table.
732 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
733 unsigned CallSiteTableLengthSize =
734 MCAsmInfo::getULEB128Size(CallSiteTableLength);
735 unsigned TTypeBaseOffset =
736 sizeof(int8_t) + // Call site format
737 CallSiteTableLengthSize + // Call site table length size
738 CallSiteTableLength + // Call site table length
739 SizeActions + // Actions size
740 SizeTypes;
741 unsigned TTypeBaseOffsetSize = MCAsmInfo::getULEB128Size(TTypeBaseOffset);
742 unsigned TotalSize =
743 sizeof(int8_t) + // LPStart format
744 sizeof(int8_t) + // TType format
745 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size
746 TTypeBaseOffset; // TType base offset
747 unsigned SizeAlign = (4 - TotalSize) & 3;
748
Bill Wendling86f0d332010-02-25 23:52:44 +0000749 if (HaveTTData) {
Bill Wendling6507eca2010-02-26 21:31:01 +0000750 // Account for any extra padding that will be added to the call site table
Bill Wendlingf7e90ae2010-02-25 21:19:47 +0000751 // length.
Bill Wendling1869ac82010-02-26 22:17:52 +0000752 EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign);
753 SizeAlign = 0;
Bill Wendling86f0d332010-02-25 23:52:44 +0000754 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000755
Bill Wendling28275fd2009-09-10 06:50:01 +0000756 // SjLj Exception handling
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000757 if (IsSJLJ) {
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000758 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling1869ac82010-02-26 22:17:52 +0000759
760 // Add extra padding if it wasn't added to the TType base offset.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000761 EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000762
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000763 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000764 unsigned idx = 0;
765 for (SmallVectorImpl<CallSiteEntry>::const_iterator
766 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
767 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000768
769 // Offset of the landing pad, counted in 16-byte bundles relative to the
770 // @LPStart address.
Chris Lattner894d75a2010-01-22 23:18:42 +0000771 EmitULEB128(idx, "Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000772
773 // Offset of the first associated action record, relative to the start of
774 // the action table. This value is biased by 1 (1 indicates the start of
775 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000776 EmitULEB128(S.Action, "Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000777 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000778 } else {
779 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000780 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000781
Bill Wendlinga583c552009-08-20 22:02:24 +0000782 // The call-site table is a list of all call sites that may throw an
783 // exception (including C++ 'throw' statements) in the procedure
784 // fragment. It immediately follows the LSDA header. Each entry indicates,
785 // for a given call, the first corresponding action record and corresponding
786 // landing pad.
787 //
788 // The table begins with the number of bytes, stored as an LEB128
789 // compressed, unsigned integer. The records immediately follow the record
790 // count. They are sorted in increasing call-site address. Each record
791 // indicates:
792 //
793 // * The position of the call-site.
794 // * The position of the landing pad.
795 // * The first action record for that call site.
796 //
797 // A missing entry in the call-site table indicates that a call is not
Bill Wendling28275fd2009-09-10 06:50:01 +0000798 // supposed to throw.
Bill Wendlinga583c552009-08-20 22:02:24 +0000799
800 // Emit the landing pad call site table.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000801 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling1869ac82010-02-26 22:17:52 +0000802
803 // Add extra padding if it wasn't added to the TType base offset.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000804 EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000805
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000806 for (SmallVectorImpl<CallSiteEntry>::const_iterator
807 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
808 const CallSiteEntry &S = *I;
809 const char *BeginTag;
810 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000811
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000812 if (!S.BeginLabel) {
813 BeginTag = "eh_func_begin";
814 BeginNumber = SubprogramCount;
815 } else {
816 BeginTag = "label";
817 BeginNumber = S.BeginLabel;
818 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000819
Bill Wendlinga583c552009-08-20 22:02:24 +0000820 // Offset of the call site relative to the previous call site, counted in
821 // number of 16-byte bundles. The first call site is counted relative to
822 // the start of the procedure fragment.
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000823 EmitSectionOffset(getDWLabel(BeginTag, BeginNumber),
824 getDWLabel("eh_func_begin", SubprogramCount),
Bill Wendlingeb907212009-05-15 01:12:28 +0000825 true, true);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000826 EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000827
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000828 if (!S.EndLabel)
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000829 EmitDifference(getDWLabel("eh_func_end", SubprogramCount),
830 getDWLabel(BeginTag, BeginNumber),
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000831 true);
832 else
833 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000834
Chris Lattnerfaca5492010-01-22 23:47:11 +0000835 EOL("Region length");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000836
Bill Wendlinga583c552009-08-20 22:02:24 +0000837 // Offset of the landing pad, counted in 16-byte bundles relative to the
838 // @LPStart address.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000839 if (!S.PadLabel) {
840 Asm->OutStreamer.AddComment("Landing pad");
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000841 Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Bill Wendling1f8075d2010-02-09 22:49:16 +0000842 } else {
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000843 EmitSectionOffset(getDWLabel("label", S.PadLabel),
844 getDWLabel("eh_func_begin", SubprogramCount),
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000845 true, true);
Bill Wendling1f8075d2010-02-09 22:49:16 +0000846 EOL("Landing pad");
847 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000848
Bill Wendlinga583c552009-08-20 22:02:24 +0000849 // Offset of the first associated action record, relative to the start of
850 // the action table. This value is biased by 1 (1 indicates the start of
851 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000852 EmitULEB128(S.Action, "Action");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000853 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000854 }
855
Bill Wendlinga583c552009-08-20 22:02:24 +0000856 // Emit the Action Table.
Bill Wendling35c187b2010-02-10 00:45:28 +0000857 if (Actions.size() != 0) EOL("-- Action Record Table --");
Bill Wendling5cff4872009-07-28 23:44:43 +0000858 for (SmallVectorImpl<ActionEntry>::const_iterator
859 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
860 const ActionEntry &Action = *I;
Bill Wendling35c187b2010-02-10 00:45:28 +0000861 EOL("Action Record:");
Bill Wendlinga583c552009-08-20 22:02:24 +0000862
863 // Type Filter
864 //
865 // Used by the runtime to match the type of the thrown exception to the
866 // type of the catch clauses or the types in the exception specification.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000867 EmitSLEB128(Action.ValueForTypeID, " TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000868
869 // Action Record
870 //
871 // Self-relative signed displacement in bytes of the next action record,
872 // or 0 if there is no next action record.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000873 EmitSLEB128(Action.NextAction, " Next action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000874 }
875
Bill Wendling48dc29e2009-10-22 20:48:59 +0000876 // Emit the Catch TypeInfos.
Bill Wendlinge9d10a62010-02-11 10:37:57 +0000877 if (!TypeInfos.empty()) EOL("-- Catch TypeInfos --");
Bill Wendlingec044582009-11-18 23:18:46 +0000878 for (std::vector<GlobalVariable *>::const_reverse_iterator
Bill Wendling01c69372009-11-19 00:09:14 +0000879 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000880 const GlobalVariable *GV = *I;
Bill Wendlingec044582009-11-18 23:18:46 +0000881
Bill Wendling1f8075d2010-02-09 22:49:16 +0000882 if (GV) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000883 EmitReference(GV, TTypeEncoding);
Bill Wendlingd3a47a32010-02-10 00:59:47 +0000884 EOL("TypeInfo");
Bill Wendling1f8075d2010-02-09 22:49:16 +0000885 } else {
Anton Korobeynikov0b217302010-02-15 22:38:25 +0000886 PrintRelDirective(TTypeEncoding);
Bill Wendling049e98d2009-08-31 18:26:48 +0000887 O << "0x0";
Bill Wendling1f8075d2010-02-09 22:49:16 +0000888 EOL("");
889 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000890 }
891
Bill Wendling48dc29e2009-10-22 20:48:59 +0000892 // Emit the Exception Specifications.
Bill Wendlinge9d10a62010-02-11 10:37:57 +0000893 if (!FilterIds.empty()) EOL("-- Filter IDs --");
Bill Wendling5cff4872009-07-28 23:44:43 +0000894 for (std::vector<unsigned>::const_iterator
895 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
896 unsigned TypeID = *I;
Chris Lattner894d75a2010-01-22 23:18:42 +0000897 EmitULEB128(TypeID, TypeID != 0 ? "Exception specification" : 0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000898 }
899
900 Asm->EmitAlignment(2, 0, 0, false);
901}
902
Bill Wendlingeb907212009-05-15 01:12:28 +0000903/// EndModule - Emit all exception information that should come after the
904/// content.
905void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000906 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000907 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000908
Bill Wendling52783c62009-09-09 23:56:55 +0000909 if (!shouldEmitMovesModule && !shouldEmitTableModule)
910 return;
911
Bill Wendlingeb907212009-05-15 01:12:28 +0000912 if (TimePassesIsEnabled)
913 ExceptionTimer->startTimer();
914
Bill Wendling52783c62009-09-09 23:56:55 +0000915 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000916
Bill Wendling28275fd2009-09-10 06:50:01 +0000917 for (unsigned I = 0, E = Personalities.size(); I < E; ++I)
918 EmitCIE(Personalities[I], I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000919
Bill Wendling52783c62009-09-09 23:56:55 +0000920 for (std::vector<FunctionEHFrameInfo>::iterator
921 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
922 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000923
924 if (TimePassesIsEnabled)
925 ExceptionTimer->stopTimer();
926}
927
Bill Wendling28275fd2009-09-10 06:50:01 +0000928/// BeginFunction - Gather pre-function exception information. Assumes it's
929/// being emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +0000930void DwarfException::BeginFunction(const MachineFunction *MF) {
Bill Wendling73c5a612009-09-10 18:28:06 +0000931 if (!MMI || !MAI->doesSupportExceptionHandling()) return;
932
Bill Wendlingeb907212009-05-15 01:12:28 +0000933 if (TimePassesIsEnabled)
934 ExceptionTimer->startTimer();
935
936 this->MF = MF;
937 shouldEmitTable = shouldEmitMoves = false;
938
Bill Wendling73c5a612009-09-10 18:28:06 +0000939 // Map all labels and get rid of any dead landing pads.
940 MMI->TidyLandingPads();
Bill Wendlingeb907212009-05-15 01:12:28 +0000941
Bill Wendling73c5a612009-09-10 18:28:06 +0000942 // If any landing pads survive, we need an EH table.
943 if (!MMI->getLandingPads().empty())
944 shouldEmitTable = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000945
Bill Wendling73c5a612009-09-10 18:28:06 +0000946 // See if we need frame move info.
947 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
948 shouldEmitMoves = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000949
Bill Wendling73c5a612009-09-10 18:28:06 +0000950 if (shouldEmitMoves || shouldEmitTable)
951 // Assumes in correct section after the entry point.
952 EmitLabel("eh_func_begin", ++SubprogramCount);
Bill Wendlingeb907212009-05-15 01:12:28 +0000953
954 shouldEmitTableModule |= shouldEmitTable;
955 shouldEmitMovesModule |= shouldEmitMoves;
956
957 if (TimePassesIsEnabled)
958 ExceptionTimer->stopTimer();
959}
960
961/// EndFunction - Gather and emit post-function exception information.
962///
963void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000964 if (!shouldEmitMoves && !shouldEmitTable) return;
965
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000966 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000967 ExceptionTimer->startTimer();
968
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000969 EmitLabel("eh_func_end", SubprogramCount);
970 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000971
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000972 MCSymbol *FunctionEHSym =
Chris Lattner7a2ba942010-01-16 18:37:32 +0000973 Asm->GetSymbolWithGlobalValueBase(MF->getFunction(), ".eh",
974 Asm->MAI->is_EHSymbolPrivate());
Chris Lattner25d812b2009-09-16 00:35:39 +0000975
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000976 // Save EH frame information
Chris Lattner7a2ba942010-01-16 18:37:32 +0000977 EHFrames.push_back(FunctionEHFrameInfo(FunctionEHSym, SubprogramCount,
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000978 MMI->getPersonalityIndex(),
979 MF->getFrameInfo()->hasCalls(),
980 !MMI->getLandingPads().empty(),
981 MMI->getFrameMoves(),
982 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +0000983
Bill Wendling52783c62009-09-09 23:56:55 +0000984 // Record if this personality index uses a landing pad.
985 UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
986
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000987 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000988 ExceptionTimer->stopTimer();
989}