blob: e3f06a3347b90df11c7a39446ada8d7e5d5c52e4 [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
Chris Lattner894d75a2010-01-22 23:18:42 +0000150 EmitULEB128(AugmentationSize, "Augmentation Size");
Bill Wendling52783c62009-09-09 23:56:55 +0000151
Bill Wendling4bda11f2009-08-25 02:32:05 +0000152 // If there is a personality, we need to indicate the function's location.
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000153 if (PersonalityFn) {
154 EmitEncodingByte(PerEncoding, "Personality");
155 EmitReference(PersonalityFn, PerEncoding);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000156 EOL("Personality");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000157 if (UsesLSDA[Index])
158 EmitEncodingByte(LSDAEncoding, "LSDA");
159 if (FDEEncoding != dwarf::DW_EH_PE_absptr)
160 EmitEncodingByte(FDEEncoding, "FDE");
Bill Wendlingeb907212009-05-15 01:12:28 +0000161 }
162
163 // Indicate locations of general callee saved registers in frame.
164 std::vector<MachineMove> Moves;
165 RI->getInitialFrameState(Moves);
166 EmitFrameMoves(NULL, 0, Moves, true);
167
168 // On Darwin the linker honors the alignment of eh_frame, which means it must
169 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
170 // holes which confuse readers of eh_frame.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000171 Asm->EmitAlignment(TD->getPointerSize() == 4 ? 2 : 3, 0, 0, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000172 EmitLabel("eh_frame_common_end", Index);
Chris Lattner0ad9c912010-01-22 22:09:00 +0000173 Asm->O << '\n';
Bill Wendlingeb907212009-05-15 01:12:28 +0000174}
175
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000176/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
177void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000178 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000179 "Should not emit 'available externally' functions at all");
180
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000181 const Function *TheFunc = EHFrameInfo.function;
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000182 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000183
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000184 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
185 unsigned FDEEncoding = TLOF.getFDEEncoding();
186
187 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000188
Bill Wendlingeb907212009-05-15 01:12:28 +0000189 // Externally visible entry into the functions eh frame info. If the
190 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000191 if (!TheFunc->hasLocalLinkage())
Chris Lattner10b318b2010-01-17 21:43:43 +0000192 if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
193 O << GlobalEHDirective << *EHFrameInfo.FunctionEHSym << '\n';
Bill Wendlingeb907212009-05-15 01:12:28 +0000194
195 // If corresponding function is weak definition, this should be too.
Chris Lattner10b318b2010-01-17 21:43:43 +0000196 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
197 O << MAI->getWeakDefDirective() << *EHFrameInfo.FunctionEHSym << '\n';
Bill Wendlingee161a62009-11-11 01:24:59 +0000198
199 // If corresponding function is hidden, this should be too.
200 if (TheFunc->hasHiddenVisibility())
Chris Lattner152a29b2010-01-23 06:53:23 +0000201 if (MCSymbolAttr HiddenAttr = MAI->getHiddenVisibilityAttr())
202 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
203 HiddenAttr);
Bill Wendlingeb907212009-05-15 01:12:28 +0000204
205 // If there are no calls then you can't unwind. This may mean we can omit the
206 // EH Frame, but some environments do not handle weak absolute symbols. If
207 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
208 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000209 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
210 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000211 !MAI->getWeakDefDirective() ||
212 MAI->getSupportsWeakOmittedEHFrame())) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000213 O << *EHFrameInfo.FunctionEHSym << " = 0\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000214 // This name has no connection to the function, so it might get
215 // dead-stripped when the function is not, erroneously. Prohibit
216 // dead-stripping unconditionally.
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000217 if (MAI->hasNoDeadStrip())
218 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000219 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000220 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000221 O << *EHFrameInfo.FunctionEHSym << ":\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000222
223 // EH frame header.
224 EmitDifference("eh_frame_end", EHFrameInfo.Number,
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000225 "eh_frame_begin", EHFrameInfo.Number,
226 true);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000227 EOL("Length of Frame Information Entry");
Bill Wendlingeb907212009-05-15 01:12:28 +0000228
229 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
230
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000231 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
232 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
233 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000234
Chris Lattnerfaca5492010-01-22 23:47:11 +0000235 EOL("FDE CIE offset");
Bill Wendlingeb907212009-05-15 01:12:28 +0000236
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000237 EmitReference("eh_func_begin", EHFrameInfo.Number, FDEEncoding);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000238 EOL("FDE initial location");
Bill Wendlingeb907212009-05-15 01:12:28 +0000239 EmitDifference("eh_func_end", EHFrameInfo.Number,
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000240 "eh_func_begin", EHFrameInfo.Number,
241 SizeOfEncodedValue(FDEEncoding) == 4);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000242 EOL("FDE address range");
Bill Wendlingeb907212009-05-15 01:12:28 +0000243
244 // If there is a personality and landing pads then point to the language
245 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000246 if (MMI->getPersonalities()[0] != NULL) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000247 unsigned Size = SizeOfEncodedValue(LSDAEncoding);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000248
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000249 EmitULEB128(Size, "Augmentation size");
250 if (EHFrameInfo.hasLandingPads)
251 EmitReference("exception", EHFrameInfo.Number, LSDAEncoding);
252 else
253 Asm->OutStreamer.EmitIntValue(0, Size/*size*/, 0/*addrspace*/);
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000254
Chris Lattnerfaca5492010-01-22 23:47:11 +0000255 EOL("Language Specific Data Area");
Bill Wendlingeb907212009-05-15 01:12:28 +0000256 } else {
Chris Lattner894d75a2010-01-22 23:18:42 +0000257 EmitULEB128(0, "Augmentation size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000258 }
259
260 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000261 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000262 true);
263
264 // On Darwin the linker honors the alignment of eh_frame, which means it
265 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
266 // get holes which confuse readers of eh_frame.
267 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
268 0, 0, false);
269 EmitLabel("eh_frame_end", EHFrameInfo.Number);
270
271 // If the function is marked used, this table should be also. We cannot
272 // make the mark unconditional in this case, since retaining the table also
273 // retains the function in this case, and there is code around that depends
274 // on unused functions (calling undefined externals) being dead-stripped to
275 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000276 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000277 if (MAI->hasNoDeadStrip())
278 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000279 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000280 }
Chris Lattner0ad9c912010-01-22 22:09:00 +0000281 Asm->O << '\n';
Bill Wendlingeb907212009-05-15 01:12:28 +0000282}
283
Bill Wendlingeb907212009-05-15 01:12:28 +0000284/// SharedTypeIds - How many leading type ids two landing pads have in common.
285unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
286 const LandingPadInfo *R) {
287 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
288 unsigned LSize = LIds.size(), RSize = RIds.size();
289 unsigned MinSize = LSize < RSize ? LSize : RSize;
290 unsigned Count = 0;
291
292 for (; Count != MinSize; ++Count)
293 if (LIds[Count] != RIds[Count])
294 return Count;
295
296 return Count;
297}
298
299/// PadLT - Order landing pads lexicographically by type id.
300bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
301 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
302 unsigned LSize = LIds.size(), RSize = RIds.size();
303 unsigned MinSize = LSize < RSize ? LSize : RSize;
304
305 for (unsigned i = 0; i != MinSize; ++i)
306 if (LIds[i] != RIds[i])
307 return LIds[i] < RIds[i];
308
309 return LSize < RSize;
310}
311
Bill Wendlingd4609622009-07-28 23:23:00 +0000312/// ComputeActionsTable - Compute the actions table and gather the first action
313/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000314unsigned DwarfException::
315ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
316 SmallVectorImpl<ActionEntry> &Actions,
317 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000318
319 // The action table follows the call-site table in the LSDA. The individual
320 // records are of two types:
321 //
322 // * Catch clause
323 // * Exception specification
324 //
325 // The two record kinds have the same format, with only small differences.
326 // They are distinguished by the "switch value" field: Catch clauses
327 // (TypeInfos) have strictly positive switch values, and exception
328 // specifications (FilterIds) have strictly negative switch values. Value 0
329 // indicates a catch-all clause.
330 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000331 // Negative type IDs index into FilterIds. Positive type IDs index into
332 // TypeInfos. The value written for a positive type ID is just the type ID
333 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000334 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000335 // offset is usually equal to the type ID (because the FilterIds entries are
336 // written using a variable width encoding, which outputs one byte per entry
337 // as long as the value written is not too large) but can differ. This kind
338 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000339 // output using a fixed width encoding. FilterOffsets[i] holds the byte
340 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000341
342 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000343 SmallVector<int, 16> FilterOffsets;
344 FilterOffsets.reserve(FilterIds.size());
345 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000346
347 for (std::vector<unsigned>::const_iterator
348 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000349 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000350 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000351 }
352
Bill Wendlingeb907212009-05-15 01:12:28 +0000353 FirstActions.reserve(LandingPads.size());
354
355 int FirstAction = 0;
356 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000357 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000358
Bill Wendling5cff4872009-07-28 23:44:43 +0000359 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000360 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
361 const LandingPadInfo *LPI = *I;
362 const std::vector<int> &TypeIds = LPI->TypeIds;
363 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000364 unsigned SizeSiteActions = 0;
365
366 if (NumShared < TypeIds.size()) {
367 unsigned SizeAction = 0;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000368 unsigned PrevAction = (unsigned)-1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000369
370 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000371 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000372 assert(Actions.size());
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000373 PrevAction = Actions.size() - 1;
374 SizeAction =
375 MCAsmInfo::getSLEB128Size(Actions[PrevAction].NextAction) +
376 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000377
378 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000379 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000380 SizeAction -=
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000381 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
382 SizeAction += -Actions[PrevAction].NextAction;
383 PrevAction = Actions[PrevAction].Previous;
Bill Wendlingeb907212009-05-15 01:12:28 +0000384 }
385 }
386
387 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000388 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
389 int TypeID = TypeIds[J];
390 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000391 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000392 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000393
394 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000395 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000396 SizeSiteActions += SizeAction;
397
Bill Wendlinga583c552009-08-20 22:02:24 +0000398 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000399 Actions.push_back(Action);
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000400 PrevAction = Actions.size() - 1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000401 }
402
403 // Record the first action of the landing pad site.
404 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
405 } // else identical - re-use previous FirstAction
406
Bill Wendlinga583c552009-08-20 22:02:24 +0000407 // Information used when created the call-site table. The action record
408 // field of the call site record is the offset of the first associated
409 // action record, relative to the start of the actions table. This value is
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000410 // biased by 1 (1 indicating the start of the actions table), and 0
Bill Wendlinga583c552009-08-20 22:02:24 +0000411 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000412 FirstActions.push_back(FirstAction);
413
414 // Compute this sites contribution to size.
415 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000416
417 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000418 }
419
Bill Wendling5e953dd2009-07-28 23:22:13 +0000420 return SizeActions;
421}
422
Bill Wendlinged060dc2009-11-12 21:59:20 +0000423/// CallToNoUnwindFunction - Return `true' if this is a call to a function
424/// marked `nounwind'. Return `false' otherwise.
425bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) {
426 assert(MI->getDesc().isCall() && "This should be a call instruction!");
427
428 bool MarkedNoUnwind = false;
429 bool SawFunc = false;
430
431 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
432 const MachineOperand &MO = MI->getOperand(I);
433
434 if (MO.isGlobal()) {
435 if (Function *F = dyn_cast<Function>(MO.getGlobal())) {
436 if (SawFunc) {
437 // Be conservative. If we have more than one function operand for this
438 // call, then we can't make the assumption that it's the callee and
439 // not a parameter to the call.
440 //
441 // FIXME: Determine if there's a way to say that `F' is the callee or
442 // parameter.
443 MarkedNoUnwind = false;
444 break;
445 }
Bill Wendlingecc260e2009-11-12 23:13:08 +0000446
447 MarkedNoUnwind = F->doesNotThrow();
448 SawFunc = true;
Bill Wendlinged060dc2009-11-12 21:59:20 +0000449 }
450 }
451 }
452
453 return MarkedNoUnwind;
454}
455
Bill Wendlingade025c2009-07-29 00:31:35 +0000456/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000457/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000458/// appropriate action. The entry for an ordinary call has a try-range
459/// containing the call and zero for the landing pad and the action. Calls
460/// marked 'nounwind' have no entry and must not be contained in the try-range
461/// of any entry - they form gaps in the table. Entries must be ordered by
462/// try-range address.
463void DwarfException::
464ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
465 const RangeMapType &PadMap,
466 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
467 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000468 // The end label of the previous invoke or nounwind try-range.
469 unsigned LastLabel = 0;
470
471 // Whether there is a potentially throwing instruction (currently this means
472 // an ordinary call) between the end of the previous try-range and now.
473 bool SawPotentiallyThrowing = false;
474
Bill Wendling5cff4872009-07-28 23:44:43 +0000475 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000476 bool PreviousIsInvoke = false;
477
478 // Visit all instructions in order of address.
479 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
480 I != E; ++I) {
481 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
482 MI != E; ++MI) {
483 if (!MI->isLabel()) {
Bill Wendlinged060dc2009-11-12 21:59:20 +0000484 if (MI->getDesc().isCall())
485 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI);
Bill Wendling73b55512009-11-11 23:17:02 +0000486
Bill Wendlingeb907212009-05-15 01:12:28 +0000487 continue;
488 }
489
490 unsigned BeginLabel = MI->getOperand(0).getImm();
491 assert(BeginLabel && "Invalid label!");
492
493 // End of the previous try-range?
494 if (BeginLabel == LastLabel)
495 SawPotentiallyThrowing = false;
496
497 // Beginning of a new try-range?
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000498 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
Bill Wendlingeb907212009-05-15 01:12:28 +0000499 if (L == PadMap.end())
500 // Nope, it was just some random label.
501 continue;
502
Bill Wendlinga583c552009-08-20 22:02:24 +0000503 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000504 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000505 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
506 "Inconsistent landing pad map!");
507
Bill Wendlinga583c552009-08-20 22:02:24 +0000508 // For Dwarf exception handling (SjLj handling doesn't use this). If some
509 // instruction between the previous try-range and this one may throw,
510 // create a call-site entry with no landing pad for the region between the
511 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000512 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000513 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000514 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000515 CallSites.push_back(Site);
516 PreviousIsInvoke = false;
517 }
518
519 LastLabel = LandingPad->EndLabels[P.RangeIndex];
520 assert(BeginLabel && LastLabel && "Invalid landing pad!");
521
522 if (LandingPad->LandingPadLabel) {
523 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000524 CallSiteEntry Site = {
525 BeginLabel,
526 LastLabel,
527 LandingPad->LandingPadLabel,
528 FirstActions[P.PadIndex]
529 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000530
Jim Grosbach33668c02009-09-01 17:19:13 +0000531 // Try to merge with the previous call-site. SJLJ doesn't do this
532 if (PreviousIsInvoke &&
533 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000534 CallSiteEntry &Prev = CallSites.back();
535 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
536 // Extend the range of the previous entry.
537 Prev.EndLabel = Site.EndLabel;
538 continue;
539 }
540 }
541
542 // Otherwise, create a new call-site.
Jim Grosbachca752c92010-01-28 01:45:32 +0000543 if (MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf)
544 CallSites.push_back(Site);
545 else {
546 // SjLj EH must maintain the call sites in the order assigned
547 // to them by the SjLjPrepare pass.
548 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
549 if (CallSites.size() < SiteNo)
550 CallSites.resize(SiteNo);
551 CallSites[SiteNo - 1] = Site;
552 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000553 PreviousIsInvoke = true;
554 } else {
555 // Create a gap.
556 PreviousIsInvoke = false;
557 }
558 }
559 }
560
561 // If some instruction between the previous try-range and the end of the
562 // function may throw, create a call-site entry with no landing pad for the
563 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000564 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000565 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000566 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000567 CallSites.push_back(Site);
568 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000569}
570
Bill Wendling0dafca92009-07-29 00:50:05 +0000571/// EmitExceptionTable - Emit landing pads and actions.
572///
573/// The general organization of the table is complex, but the basic concepts are
574/// easy. First there is a header which describes the location and organization
575/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000576///
Bill Wendling0dafca92009-07-29 00:50:05 +0000577/// 1. The landing pad site information describes the range of code covered by
578/// the try. In our case it's an accumulation of the ranges covered by the
579/// invokes in the try. There is also a reference to the landing pad that
580/// handles the exception once processed. Finally an index into the actions
581/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000582/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000583/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000584/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000585/// it matches then the exception and type id are passed on to the landing
586/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling28275fd2009-09-10 06:50:01 +0000587/// with a next action of zero. If no type id is found then the frame is
Bill Wendling0dafca92009-07-29 00:50:05 +0000588/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000589/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling28275fd2009-09-10 06:50:01 +0000590/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000591void DwarfException::EmitExceptionTable() {
592 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
593 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
594 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
595 if (PadInfos.empty()) return;
596
597 // Sort the landing pads in order of their type ids. This is used to fold
598 // duplicate actions.
599 SmallVector<const LandingPadInfo *, 64> LandingPads;
600 LandingPads.reserve(PadInfos.size());
601
602 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
603 LandingPads.push_back(&PadInfos[i]);
604
605 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
606
607 // Compute the actions table and gather the first action index for each
608 // landing pad site.
609 SmallVector<ActionEntry, 32> Actions;
610 SmallVector<unsigned, 64> FirstActions;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000611 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000612
613 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
614 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling28275fd2009-09-10 06:50:01 +0000615 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000616 RangeMapType PadMap;
617 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
618 const LandingPadInfo *LandingPad = LandingPads[i];
619 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
620 unsigned BeginLabel = LandingPad->BeginLabels[j];
621 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
622 PadRange P = { i, j };
623 PadMap[BeginLabel] = P;
624 }
625 }
626
627 // Compute the call-site table.
628 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000629 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000630
631 // Final tallies.
632
633 // Call sites.
Bill Wendling40121bc2009-09-10 00:13:16 +0000634 const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
635 const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
636 const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000637 bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000638 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Bill Wendling28275fd2009-09-10 06:50:01 +0000639 unsigned SizeSites;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000640
641 if (IsSJLJ)
Jim Grosbach8b818d72009-08-17 16:41:22 +0000642 SizeSites = 0;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000643 else
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000644 SizeSites = CallSites.size() *
645 (SiteStartSize + SiteLengthSize + LandingPadSize);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000646
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000647 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000648 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000649 if (IsSJLJ)
Chris Lattneraf76e592009-08-22 20:48:53 +0000650 SizeSites += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000651 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000652
Bill Wendlingeb907212009-05-15 01:12:28 +0000653 // Type infos.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000654 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000655 unsigned TTypeEncoding;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000656 unsigned TypeFormatSize;
Bill Wendlingeb907212009-05-15 01:12:28 +0000657
Bill Wendling43e484f2009-09-10 01:12:47 +0000658 if (!HaveTTData) {
Bill Wendling28275fd2009-09-10 06:50:01 +0000659 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
660 // that we're omitting that bit.
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000661 TTypeEncoding = dwarf::DW_EH_PE_omit;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000662 TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr);
Chris Lattner81c9a062009-07-31 22:03:47 +0000663 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000664 // Okay, we have actual filters or typeinfos to emit. As such, we need to
665 // pick a type encoding for them. We're about to emit a list of pointers to
666 // typeinfo objects at the end of the LSDA. However, unless we're in static
667 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000668 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000669 // Because of this, we have a couple of options:
Bill Wendling28275fd2009-09-10 06:50:01 +0000670 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000671 // 1) If we are in -static mode, we can always use an absolute reference
672 // from the LSDA, because the static linker will resolve it.
Bill Wendling28275fd2009-09-10 06:50:01 +0000673 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000674 // 2) Otherwise, if the LSDA section is writable, we can output the direct
675 // reference to the typeinfo and allow the dynamic linker to relocate
676 // it. Since it is in a writable section, the dynamic linker won't
677 // have a problem.
Bill Wendling28275fd2009-09-10 06:50:01 +0000678 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000679 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
680 // we need to use some form of indirection. For example, on Darwin,
681 // we can output a statically-relocatable reference to a dyld stub. The
682 // offset to the stub is constant, but the contents are in a section
683 // that is updated by the dynamic linker. This is easy enough, but we
684 // need to tell the personality function of the unwinder to indirect
685 // through the dyld stub.
686 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000687 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattnerad88bc42009-08-02 03:59:56 +0000688 // somewhere. This predicate should be moved to a shared location that is
689 // in target-independent code.
690 //
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000691 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
692 TypeFormatSize = SizeOfEncodedValue(TTypeEncoding);
Bill Wendlingfe220282009-09-10 02:07:37 +0000693 }
Bill Wendling43e484f2009-09-10 01:12:47 +0000694
Bill Wendlingfe220282009-09-10 02:07:37 +0000695 // Begin the exception table.
696 Asm->OutStreamer.SwitchSection(LSDASection);
697 Asm->EmitAlignment(2, 0, 0, false);
Bill Wendling43e484f2009-09-10 01:12:47 +0000698
Bill Wendlingfe220282009-09-10 02:07:37 +0000699 O << "GCC_except_table" << SubprogramCount << ":\n";
Bill Wendlinga2f64492009-09-10 06:27:16 +0000700
701 // The type infos need to be aligned. GCC does this by inserting padding just
702 // before the type infos. However, this changes the size of the exception
703 // table, so you need to take this into account when you output the exception
704 // table size. However, the size is output using a variable length encoding.
705 // So by increasing the size by inserting padding, you may increase the number
706 // of bytes used for writing the size. If it increases, say by one byte, then
707 // you now need to output one less byte of padding to get the type infos
708 // aligned. However this decreases the size of the exception table. This
709 // changes the value you have to output for the exception table size. Due to
710 // the variable length encoding, the number of bytes used for writing the
711 // length may decrease. If so, you then have to increase the amount of
712 // padding. And so on. If you look carefully at the GCC code you will see that
713 // it indeed does this in a loop, going on and on until the values stabilize.
714 // We chose another solution: don't output padding inside the table like GCC
715 // does, instead output it before the table.
716 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
717 unsigned TyOffset = sizeof(int8_t) + // Call site format
Bill Wendling35c187b2010-02-10 00:45:28 +0000718 MCAsmInfo::getULEB128Size(SizeSites) + // Call site table length
Bill Wendlinga2f64492009-09-10 06:27:16 +0000719 SizeSites + SizeActions + SizeTypes;
720 unsigned TotalSize = sizeof(int8_t) + // LPStart format
721 sizeof(int8_t) + // TType format
722 (HaveTTData ?
723 MCAsmInfo::getULEB128Size(TyOffset) : 0) + // TType base offset
724 TyOffset;
725 unsigned SizeAlign = (4 - TotalSize) & 3;
726
727 for (unsigned i = 0; i != SizeAlign; ++i) {
728 Asm->EmitInt8(0);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000729 EOL("Padding");
Bill Wendlinga2f64492009-09-10 06:27:16 +0000730 }
731
Bill Wendlingfe220282009-09-10 02:07:37 +0000732 EmitLabel("exception", SubprogramCount);
733
734 if (IsSJLJ) {
735 SmallString<16> LSDAName;
736 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
737 "_LSDA_" << Asm->getFunctionNumber();
738 O << LSDAName.str() << ":\n";
739 }
740
741 // Emit the header.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000742 EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000743 EmitEncodingByte(TTypeEncoding, "@TType");
Bill Wendlingfe220282009-09-10 02:07:37 +0000744
Chris Lattner894d75a2010-01-22 23:18:42 +0000745 if (HaveTTData)
746 EmitULEB128(TyOffset, "@TType base offset");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000747
Bill Wendling28275fd2009-09-10 06:50:01 +0000748 // SjLj Exception handling
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000749 if (IsSJLJ) {
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000750 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Chris Lattner894d75a2010-01-22 23:18:42 +0000751 EmitULEB128(SizeSites, "Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000752
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000753 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000754 unsigned idx = 0;
755 for (SmallVectorImpl<CallSiteEntry>::const_iterator
756 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
757 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000758
759 // Offset of the landing pad, counted in 16-byte bundles relative to the
760 // @LPStart address.
Chris Lattner894d75a2010-01-22 23:18:42 +0000761 EmitULEB128(idx, "Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000762
763 // Offset of the first associated action record, relative to the start of
764 // the action table. This value is biased by 1 (1 indicates the start of
765 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000766 EmitULEB128(S.Action, "Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000767 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000768 } else {
769 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000770 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000771
Bill Wendlinga583c552009-08-20 22:02:24 +0000772 // The call-site table is a list of all call sites that may throw an
773 // exception (including C++ 'throw' statements) in the procedure
774 // fragment. It immediately follows the LSDA header. Each entry indicates,
775 // for a given call, the first corresponding action record and corresponding
776 // landing pad.
777 //
778 // The table begins with the number of bytes, stored as an LEB128
779 // compressed, unsigned integer. The records immediately follow the record
780 // count. They are sorted in increasing call-site address. Each record
781 // indicates:
782 //
783 // * The position of the call-site.
784 // * The position of the landing pad.
785 // * The first action record for that call site.
786 //
787 // A missing entry in the call-site table indicates that a call is not
Bill Wendling28275fd2009-09-10 06:50:01 +0000788 // supposed to throw.
Bill Wendlinga583c552009-08-20 22:02:24 +0000789
790 // Emit the landing pad call site table.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000791 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling35c187b2010-02-10 00:45:28 +0000792 EmitULEB128(SizeSites, "Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000793
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000794 for (SmallVectorImpl<CallSiteEntry>::const_iterator
795 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
796 const CallSiteEntry &S = *I;
797 const char *BeginTag;
798 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000799
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000800 if (!S.BeginLabel) {
801 BeginTag = "eh_func_begin";
802 BeginNumber = SubprogramCount;
803 } else {
804 BeginTag = "label";
805 BeginNumber = S.BeginLabel;
806 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000807
Bill Wendlinga583c552009-08-20 22:02:24 +0000808 // Offset of the call site relative to the previous call site, counted in
809 // number of 16-byte bundles. The first call site is counted relative to
810 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000811 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000812 true, true);
Chris Lattnerfaca5492010-01-22 23:47:11 +0000813 EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000814
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000815 if (!S.EndLabel)
816 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
817 true);
818 else
819 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000820
Chris Lattnerfaca5492010-01-22 23:47:11 +0000821 EOL("Region length");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000822
Bill Wendlinga583c552009-08-20 22:02:24 +0000823 // Offset of the landing pad, counted in 16-byte bundles relative to the
824 // @LPStart address.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000825 if (!S.PadLabel) {
826 Asm->OutStreamer.AddComment("Landing pad");
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000827 Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Bill Wendling1f8075d2010-02-09 22:49:16 +0000828 } else {
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000829 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
830 true, true);
Bill Wendling1f8075d2010-02-09 22:49:16 +0000831 EOL("Landing pad");
832 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000833
Bill Wendlinga583c552009-08-20 22:02:24 +0000834 // Offset of the first associated action record, relative to the start of
835 // the action table. This value is biased by 1 (1 indicates the start of
836 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000837 EmitULEB128(S.Action, "Action");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000838 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000839 }
840
Bill Wendlinga583c552009-08-20 22:02:24 +0000841 // Emit the Action Table.
Bill Wendling35c187b2010-02-10 00:45:28 +0000842 if (Actions.size() != 0) EOL("-- Action Record Table --");
Bill Wendling5cff4872009-07-28 23:44:43 +0000843 for (SmallVectorImpl<ActionEntry>::const_iterator
844 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
845 const ActionEntry &Action = *I;
Bill Wendling35c187b2010-02-10 00:45:28 +0000846 EOL("Action Record:");
Bill Wendlinga583c552009-08-20 22:02:24 +0000847
848 // Type Filter
849 //
850 // Used by the runtime to match the type of the thrown exception to the
851 // type of the catch clauses or the types in the exception specification.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000852 EmitSLEB128(Action.ValueForTypeID, " TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000853
854 // Action Record
855 //
856 // Self-relative signed displacement in bytes of the next action record,
857 // or 0 if there is no next action record.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000858 EmitSLEB128(Action.NextAction, " Next action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000859 }
860
Bill Wendling48dc29e2009-10-22 20:48:59 +0000861 // Emit the Catch TypeInfos.
Bill Wendlinge9d10a62010-02-11 10:37:57 +0000862 if (!TypeInfos.empty()) EOL("-- Catch TypeInfos --");
Bill Wendlingec044582009-11-18 23:18:46 +0000863 for (std::vector<GlobalVariable *>::const_reverse_iterator
Bill Wendling01c69372009-11-19 00:09:14 +0000864 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000865 const GlobalVariable *GV = *I;
Bill Wendlingec044582009-11-18 23:18:46 +0000866
Bill Wendling1f8075d2010-02-09 22:49:16 +0000867 if (GV) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000868 EmitReference(GV, TTypeEncoding);
Bill Wendlingd3a47a32010-02-10 00:59:47 +0000869 EOL("TypeInfo");
Bill Wendling1f8075d2010-02-09 22:49:16 +0000870 } else {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000871 PrintRelDirective();
Bill Wendling049e98d2009-08-31 18:26:48 +0000872 O << "0x0";
Bill Wendling1f8075d2010-02-09 22:49:16 +0000873 EOL("");
874 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000875 }
876
Bill Wendling48dc29e2009-10-22 20:48:59 +0000877 // Emit the Exception Specifications.
Bill Wendlinge9d10a62010-02-11 10:37:57 +0000878 if (!FilterIds.empty()) EOL("-- Filter IDs --");
Bill Wendling5cff4872009-07-28 23:44:43 +0000879 for (std::vector<unsigned>::const_iterator
880 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
881 unsigned TypeID = *I;
Chris Lattner894d75a2010-01-22 23:18:42 +0000882 EmitULEB128(TypeID, TypeID != 0 ? "Exception specification" : 0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000883 }
884
885 Asm->EmitAlignment(2, 0, 0, false);
886}
887
Bill Wendlingeb907212009-05-15 01:12:28 +0000888/// EndModule - Emit all exception information that should come after the
889/// content.
890void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000891 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000892 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000893
Bill Wendling52783c62009-09-09 23:56:55 +0000894 if (!shouldEmitMovesModule && !shouldEmitTableModule)
895 return;
896
Bill Wendlingeb907212009-05-15 01:12:28 +0000897 if (TimePassesIsEnabled)
898 ExceptionTimer->startTimer();
899
Bill Wendling52783c62009-09-09 23:56:55 +0000900 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000901
Bill Wendling28275fd2009-09-10 06:50:01 +0000902 for (unsigned I = 0, E = Personalities.size(); I < E; ++I)
903 EmitCIE(Personalities[I], I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000904
Bill Wendling52783c62009-09-09 23:56:55 +0000905 for (std::vector<FunctionEHFrameInfo>::iterator
906 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
907 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000908
909 if (TimePassesIsEnabled)
910 ExceptionTimer->stopTimer();
911}
912
Bill Wendling28275fd2009-09-10 06:50:01 +0000913/// BeginFunction - Gather pre-function exception information. Assumes it's
914/// being emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +0000915void DwarfException::BeginFunction(const MachineFunction *MF) {
Bill Wendling73c5a612009-09-10 18:28:06 +0000916 if (!MMI || !MAI->doesSupportExceptionHandling()) return;
917
Bill Wendlingeb907212009-05-15 01:12:28 +0000918 if (TimePassesIsEnabled)
919 ExceptionTimer->startTimer();
920
921 this->MF = MF;
922 shouldEmitTable = shouldEmitMoves = false;
923
Bill Wendling73c5a612009-09-10 18:28:06 +0000924 // Map all labels and get rid of any dead landing pads.
925 MMI->TidyLandingPads();
Bill Wendlingeb907212009-05-15 01:12:28 +0000926
Bill Wendling73c5a612009-09-10 18:28:06 +0000927 // If any landing pads survive, we need an EH table.
928 if (!MMI->getLandingPads().empty())
929 shouldEmitTable = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000930
Bill Wendling73c5a612009-09-10 18:28:06 +0000931 // See if we need frame move info.
932 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
933 shouldEmitMoves = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000934
Bill Wendling73c5a612009-09-10 18:28:06 +0000935 if (shouldEmitMoves || shouldEmitTable)
936 // Assumes in correct section after the entry point.
937 EmitLabel("eh_func_begin", ++SubprogramCount);
Bill Wendlingeb907212009-05-15 01:12:28 +0000938
939 shouldEmitTableModule |= shouldEmitTable;
940 shouldEmitMovesModule |= shouldEmitMoves;
941
942 if (TimePassesIsEnabled)
943 ExceptionTimer->stopTimer();
944}
945
946/// EndFunction - Gather and emit post-function exception information.
947///
948void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000949 if (!shouldEmitMoves && !shouldEmitTable) return;
950
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000951 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000952 ExceptionTimer->startTimer();
953
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000954 EmitLabel("eh_func_end", SubprogramCount);
955 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000956
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000957 MCSymbol *FunctionEHSym =
Chris Lattner7a2ba942010-01-16 18:37:32 +0000958 Asm->GetSymbolWithGlobalValueBase(MF->getFunction(), ".eh",
959 Asm->MAI->is_EHSymbolPrivate());
Chris Lattner25d812b2009-09-16 00:35:39 +0000960
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000961 // Save EH frame information
Chris Lattner7a2ba942010-01-16 18:37:32 +0000962 EHFrames.push_back(FunctionEHFrameInfo(FunctionEHSym, SubprogramCount,
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000963 MMI->getPersonalityIndex(),
964 MF->getFrameInfo()->hasCalls(),
965 !MMI->getLandingPads().empty(),
966 MMI->getFrameMoves(),
967 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +0000968
Bill Wendling52783c62009-09-09 23:56:55 +0000969 // Record if this personality index uses a landing pad.
970 UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
971
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000972 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000973 ExceptionTimer->stopTimer();
974}