blob: 11a01fe51ea618b52005dd25e8b56212b7143512 [file] [log] [blame]
Bill Wendlingeb907212009-05-15 01:12:28 +00001//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendling28275fd2009-09-10 06:50:01 +000010// This file contains support for writing DWARF exception info into asm files.
Bill Wendlingeb907212009-05-15 01:12:28 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfException.h"
15#include "llvm/Module.h"
16#include "llvm/CodeGen/MachineModuleInfo.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefc4da0c2009-08-19 21:55:33 +000018#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000019#include "llvm/CodeGen/MachineLocation.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
Bill Wendling43e484f2009-09-10 01:12:47 +000023#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000024#include "llvm/MC/MCStreamer.h"
Chris Lattner7a2ba942010-01-16 18:37:32 +000025#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000026#include "llvm/Target/Mangler.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000027#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000030#include "llvm/Target/TargetOptions.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000031#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000032#include "llvm/Support/Dwarf.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000033#include "llvm/Support/FormattedStream.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000034#include "llvm/Support/Timer.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000035#include "llvm/ADT/SmallString.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000036#include "llvm/ADT/StringExtras.h"
Bill Wendling1f8075d2010-02-09 22:49:16 +000037#include "llvm/ADT/Twine.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000038using namespace llvm;
39
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000040DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
Chris Lattneraf76e592009-08-22 20:48:53 +000041 const MCAsmInfo *T)
Chris Lattnerc3421bb2010-03-09 00:00:15 +000042 : DwarfPrinter(OS, A, T), shouldEmitTable(false),shouldEmitMoves(false),
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000043 shouldEmitTableModule(false), shouldEmitMovesModule(false),
44 ExceptionTimer(0) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +000045 if (TimePassesIsEnabled)
Chris Lattner0b86a6f2009-12-28 07:41:18 +000046 ExceptionTimer = new Timer("DWARF Exception Writer");
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000047}
48
49DwarfException::~DwarfException() {
50 delete ExceptionTimer;
51}
52
Bill 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;
Chris Lattner98cdab52010-03-10 02:25:11 +000063 MCSymbol *DotSym = Asm->OutContext.GetOrCreateTemporarySymbol(Name.str());
Bill Wendling7378b1b2009-11-17 01:23:53 +000064 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 Lattner974c0fb2010-03-10 02:48:06 +000087 MCSymbol *EHFrameSym;
Chris Lattner09d53fe2010-03-10 07:20:42 +000088 if (TLOF.isFunctionEHFrameSymbolPrivate())
Chris Lattner974c0fb2010-03-10 02:48:06 +000089 EHFrameSym = getDWLabel("EH_frame", Index);
90 else
91 EHFrameSym = Asm->OutContext.GetOrCreateSymbol(Twine("EH_frame") +
92 Twine(Index));
93 Asm->OutStreamer.EmitLabel(EHFrameSym);
Chris Lattner8c6ed052009-09-16 01:46:41 +000094
Chris Lattnerf829eef2010-03-08 22:44:40 +000095 Asm->OutStreamer.EmitLabel(getDWLabel("section_eh_frame", Index));
Bill Wendlingeb907212009-05-15 01:12:28 +000096
97 // Define base labels.
Chris Lattnerf829eef2010-03-08 22:44:40 +000098 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_common", Index));
Bill Wendlingeb907212009-05-15 01:12:28 +000099
100 // Define the eh frame length.
Chris Lattner233f52b2010-03-09 23:52:58 +0000101 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnereffa8682010-03-08 23:02:59 +0000102 EmitDifference(getDWLabel("eh_frame_common_end", Index),
103 getDWLabel("eh_frame_common_begin", Index), true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000104
105 // EH frame header.
Chris Lattnerf829eef2010-03-08 22:44:40 +0000106 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_common_begin", Index));
Chris Lattner233f52b2010-03-09 23:52:58 +0000107 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000108 Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Chris Lattner233f52b2010-03-09 23:52:58 +0000109 Asm->OutStreamer.AddComment("DW_CIE_VERSION");
Chris Lattner066c9ac2010-01-22 22:23:57 +0000110 Asm->OutStreamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1/*size*/, 0/*addr*/);
Bill Wendlingeb907212009-05-15 01:12:28 +0000111
112 // The personality presence indicates that language specific information will
Chris Lattner8c6ed052009-09-16 01:46:41 +0000113 // show up in the eh frame. Find out how we are supposed to lower the
114 // personality function reference:
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000115
116 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
117 unsigned FDEEncoding = TLOF.getFDEEncoding();
118 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Bill Wendling52783c62009-09-09 23:56:55 +0000119
Chris Lattner4cf202b2010-01-23 03:11:46 +0000120 char Augmentation[6] = { 0 };
Bill Wendling52783c62009-09-09 23:56:55 +0000121 unsigned AugmentationSize = 0;
122 char *APtr = Augmentation + 1;
123
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000124 if (PersonalityFn) {
Bill Wendling52783c62009-09-09 23:56:55 +0000125 // There is a personality function.
126 *APtr++ = 'P';
127 AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
128 }
129
130 if (UsesLSDA[Index]) {
131 // An LSDA pointer is in the FDE augmentation.
132 *APtr++ = 'L';
133 ++AugmentationSize;
134 }
135
136 if (FDEEncoding != dwarf::DW_EH_PE_absptr) {
137 // A non-default pointer encoding for the FDE.
138 *APtr++ = 'R';
139 ++AugmentationSize;
140 }
141
142 if (APtr != Augmentation + 1)
143 Augmentation[0] = 'z';
144
Chris Lattner233f52b2010-03-09 23:52:58 +0000145 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattner4cf202b2010-01-23 03:11:46 +0000146 Asm->OutStreamer.EmitBytes(StringRef(Augmentation, strlen(Augmentation)+1),0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000147
148 // Round out reader.
Chris Lattner894d75a2010-01-22 23:18:42 +0000149 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000150 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner233f52b2010-03-09 23:52:58 +0000151 Asm->OutStreamer.AddComment("CIE Return Address Column");
Bill Wendlingeb907212009-05-15 01:12:28 +0000152 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
Bill Wendlingeb907212009-05-15 01:12:28 +0000153
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000154 if (Augmentation[0]) {
155 EmitULEB128(AugmentationSize, "Augmentation Size");
Bill Wendling52783c62009-09-09 23:56:55 +0000156
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000157 // If there is a personality, we need to indicate the function's location.
158 if (PersonalityFn) {
159 EmitEncodingByte(PerEncoding, "Personality");
Chris Lattner233f52b2010-03-09 23:52:58 +0000160 Asm->OutStreamer.AddComment("Personality");
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000161 EmitReference(PersonalityFn, PerEncoding);
Anton Korobeynikovac8a3d02010-02-15 22:36:41 +0000162 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000163 if (UsesLSDA[Index])
164 EmitEncodingByte(LSDAEncoding, "LSDA");
165 if (FDEEncoding != dwarf::DW_EH_PE_absptr)
166 EmitEncodingByte(FDEEncoding, "FDE");
Bill Wendlingeb907212009-05-15 01:12:28 +0000167 }
168
169 // Indicate locations of general callee saved registers in frame.
170 std::vector<MachineMove> Moves;
171 RI->getInitialFrameState(Moves);
172 EmitFrameMoves(NULL, 0, Moves, true);
173
174 // On Darwin the linker honors the alignment of eh_frame, which means it must
175 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
176 // holes which confuse readers of eh_frame.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000177 Asm->EmitAlignment(TD->getPointerSize() == 4 ? 2 : 3, 0, 0, false);
Chris Lattnerf829eef2010-03-08 22:44:40 +0000178 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_common_end", Index));
Bill Wendlingeb907212009-05-15 01:12:28 +0000179}
180
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000181/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
182void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000183 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000184 "Should not emit 'available externally' functions at all");
185
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000186 const Function *TheFunc = EHFrameInfo.function;
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000187 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000188
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000189 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
190 unsigned FDEEncoding = TLOF.getFDEEncoding();
191
192 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000193
Bill Wendlingeb907212009-05-15 01:12:28 +0000194 // Externally visible entry into the functions eh frame info. If the
195 // corresponding function is static, this should not be externally visible.
Chris Lattner09d53fe2010-03-10 07:20:42 +0000196 if (!TheFunc->hasLocalLinkage() && TLOF.isFunctionEHSymbolGlobal())
197 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,MCSA_Global);
Bill Wendlingeb907212009-05-15 01:12:28 +0000198
199 // If corresponding function is weak definition, this should be too.
Chris Lattner10b318b2010-01-17 21:43:43 +0000200 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
Chris Lattner974c0fb2010-03-10 02:48:06 +0000201 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
202 MCSA_WeakDefinition);
Bill Wendlingee161a62009-11-11 01:24:59 +0000203
204 // If corresponding function is hidden, this should be too.
205 if (TheFunc->hasHiddenVisibility())
Chris Lattner152a29b2010-01-23 06:53:23 +0000206 if (MCSymbolAttr HiddenAttr = MAI->getHiddenVisibilityAttr())
207 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
208 HiddenAttr);
Bill Wendlingeb907212009-05-15 01:12:28 +0000209
210 // If there are no calls then you can't unwind. This may mean we can omit the
211 // EH Frame, but some environments do not handle weak absolute symbols. If
212 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
213 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000214 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
215 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000216 !MAI->getWeakDefDirective() ||
Chris Lattner09d53fe2010-03-10 07:20:42 +0000217 TLOF.getSupportsWeakOmittedEHFrame())) {
Chris Lattner974c0fb2010-03-10 02:48:06 +0000218 Asm->OutStreamer.EmitAssignment(EHFrameInfo.FunctionEHSym,
219 MCConstantExpr::Create(0, Asm->OutContext));
Bill Wendlingeb907212009-05-15 01:12:28 +0000220 // This name has no connection to the function, so it might get
221 // dead-stripped when the function is not, erroneously. Prohibit
222 // dead-stripping unconditionally.
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000223 if (MAI->hasNoDeadStrip())
224 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000225 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000226 } else {
Chris Lattner974c0fb2010-03-10 02:48:06 +0000227 Asm->OutStreamer.EmitLabel(EHFrameInfo.FunctionEHSym);
Bill Wendlingeb907212009-05-15 01:12:28 +0000228
229 // EH frame header.
Chris Lattner233f52b2010-03-09 23:52:58 +0000230 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattnereffa8682010-03-08 23:02:59 +0000231 EmitDifference(getDWLabel("eh_frame_end", EHFrameInfo.Number),
232 getDWLabel("eh_frame_begin", EHFrameInfo.Number),
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000233 true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000234
Chris Lattnerf829eef2010-03-08 22:44:40 +0000235 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_begin",EHFrameInfo.Number));
Bill Wendlingeb907212009-05-15 01:12:28 +0000236
Chris Lattner233f52b2010-03-09 23:52:58 +0000237 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000238 EmitSectionOffset(getDWLabel("eh_frame_begin", EHFrameInfo.Number),
239 getDWLabel("eh_frame_common",
240 EHFrameInfo.PersonalityIndex),
Chris Lattner57578762010-03-08 23:23:25 +0000241 true, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000242
Bill Wendlingeb907212009-05-15 01:12:28 +0000243
Chris Lattner233f52b2010-03-09 23:52:58 +0000244 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattner326861c2010-03-08 22:50:36 +0000245 EmitReference(getDWLabel("eh_func_begin", EHFrameInfo.Number), FDEEncoding);
Chris Lattner233f52b2010-03-09 23:52:58 +0000246 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnereffa8682010-03-08 23:02:59 +0000247 EmitDifference(getDWLabel("eh_func_end", EHFrameInfo.Number),
248 getDWLabel("eh_func_begin", EHFrameInfo.Number),
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000249 SizeOfEncodedValue(FDEEncoding) == 4);
Bill Wendlingeb907212009-05-15 01:12:28 +0000250
251 // If there is a personality and landing pads then point to the language
252 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000253 if (MMI->getPersonalities()[0] != NULL) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000254 unsigned Size = SizeOfEncodedValue(LSDAEncoding);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000255
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000256 EmitULEB128(Size, "Augmentation size");
Chris Lattner233f52b2010-03-09 23:52:58 +0000257 Asm->OutStreamer.AddComment("Language Specific Data Area");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000258 if (EHFrameInfo.hasLandingPads)
Chris Lattner326861c2010-03-08 22:50:36 +0000259 EmitReference(getDWLabel("exception", EHFrameInfo.Number),LSDAEncoding);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000260 else
261 Asm->OutStreamer.EmitIntValue(0, Size/*size*/, 0/*addrspace*/);
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000262
Bill Wendlingeb907212009-05-15 01:12:28 +0000263 } else {
Chris Lattner894d75a2010-01-22 23:18:42 +0000264 EmitULEB128(0, "Augmentation size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000265 }
266
267 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000268 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000269 true);
270
271 // On Darwin the linker honors the alignment of eh_frame, which means it
272 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
273 // get holes which confuse readers of eh_frame.
274 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
275 0, 0, false);
Chris Lattnerf829eef2010-03-08 22:44:40 +0000276 Asm->OutStreamer.EmitLabel(getDWLabel("eh_frame_end", EHFrameInfo.Number));
Bill Wendlingeb907212009-05-15 01:12:28 +0000277
278 // If the function is marked used, this table should be also. We cannot
279 // make the mark unconditional in this case, since retaining the table also
280 // retains the function in this case, and there is code around that depends
281 // on unused functions (calling undefined externals) being dead-stripped to
282 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000283 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000284 if (MAI->hasNoDeadStrip())
285 Asm->OutStreamer.EmitSymbolAttribute(EHFrameInfo.FunctionEHSym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000286 MCSA_NoDeadStrip);
Bill Wendlingeb907212009-05-15 01:12:28 +0000287 }
Chris Lattner188a87d2010-03-10 01:04:13 +0000288 Asm->OutStreamer.AddBlankLine();
Bill Wendlingeb907212009-05-15 01:12:28 +0000289}
290
Bill Wendlingeb907212009-05-15 01:12:28 +0000291/// SharedTypeIds - How many leading type ids two landing pads have in common.
292unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
293 const LandingPadInfo *R) {
294 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
295 unsigned LSize = LIds.size(), RSize = RIds.size();
296 unsigned MinSize = LSize < RSize ? LSize : RSize;
297 unsigned Count = 0;
298
299 for (; Count != MinSize; ++Count)
300 if (LIds[Count] != RIds[Count])
301 return Count;
302
303 return Count;
304}
305
306/// PadLT - Order landing pads lexicographically by type id.
307bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
308 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
309 unsigned LSize = LIds.size(), RSize = RIds.size();
310 unsigned MinSize = LSize < RSize ? LSize : RSize;
311
312 for (unsigned i = 0; i != MinSize; ++i)
313 if (LIds[i] != RIds[i])
314 return LIds[i] < RIds[i];
315
316 return LSize < RSize;
317}
318
Bill Wendlingd4609622009-07-28 23:23:00 +0000319/// ComputeActionsTable - Compute the actions table and gather the first action
320/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000321unsigned DwarfException::
322ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
323 SmallVectorImpl<ActionEntry> &Actions,
324 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000325
326 // The action table follows the call-site table in the LSDA. The individual
327 // records are of two types:
328 //
329 // * Catch clause
330 // * Exception specification
331 //
332 // The two record kinds have the same format, with only small differences.
333 // They are distinguished by the "switch value" field: Catch clauses
334 // (TypeInfos) have strictly positive switch values, and exception
335 // specifications (FilterIds) have strictly negative switch values. Value 0
336 // indicates a catch-all clause.
337 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000338 // Negative type IDs index into FilterIds. Positive type IDs index into
339 // TypeInfos. The value written for a positive type ID is just the type ID
340 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000341 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000342 // offset is usually equal to the type ID (because the FilterIds entries are
343 // written using a variable width encoding, which outputs one byte per entry
344 // as long as the value written is not too large) but can differ. This kind
345 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000346 // output using a fixed width encoding. FilterOffsets[i] holds the byte
347 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000348
349 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000350 SmallVector<int, 16> FilterOffsets;
351 FilterOffsets.reserve(FilterIds.size());
352 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000353
354 for (std::vector<unsigned>::const_iterator
355 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000356 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000357 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000358 }
359
Bill Wendlingeb907212009-05-15 01:12:28 +0000360 FirstActions.reserve(LandingPads.size());
361
362 int FirstAction = 0;
363 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000364 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000365
Bill Wendling5cff4872009-07-28 23:44:43 +0000366 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000367 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
368 const LandingPadInfo *LPI = *I;
369 const std::vector<int> &TypeIds = LPI->TypeIds;
370 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000371 unsigned SizeSiteActions = 0;
372
373 if (NumShared < TypeIds.size()) {
374 unsigned SizeAction = 0;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000375 unsigned PrevAction = (unsigned)-1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000376
377 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000378 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000379 assert(Actions.size());
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000380 PrevAction = Actions.size() - 1;
381 SizeAction =
382 MCAsmInfo::getSLEB128Size(Actions[PrevAction].NextAction) +
383 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000384
385 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000386 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000387 SizeAction -=
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000388 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID);
389 SizeAction += -Actions[PrevAction].NextAction;
390 PrevAction = Actions[PrevAction].Previous;
Bill Wendlingeb907212009-05-15 01:12:28 +0000391 }
392 }
393
394 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000395 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
396 int TypeID = TypeIds[J];
397 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000398 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000399 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000400
401 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000402 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000403 SizeSiteActions += SizeAction;
404
Bill Wendlinga583c552009-08-20 22:02:24 +0000405 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000406 Actions.push_back(Action);
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000407 PrevAction = Actions.size() - 1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000408 }
409
410 // Record the first action of the landing pad site.
411 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
412 } // else identical - re-use previous FirstAction
413
Bill Wendlinga583c552009-08-20 22:02:24 +0000414 // Information used when created the call-site table. The action record
415 // field of the call site record is the offset of the first associated
416 // action record, relative to the start of the actions table. This value is
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000417 // biased by 1 (1 indicating the start of the actions table), and 0
Bill Wendlinga583c552009-08-20 22:02:24 +0000418 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000419 FirstActions.push_back(FirstAction);
420
421 // Compute this sites contribution to size.
422 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000423
424 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000425 }
426
Bill Wendling5e953dd2009-07-28 23:22:13 +0000427 return SizeActions;
428}
429
Bill Wendlinged060dc2009-11-12 21:59:20 +0000430/// CallToNoUnwindFunction - Return `true' if this is a call to a function
431/// marked `nounwind'. Return `false' otherwise.
432bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) {
433 assert(MI->getDesc().isCall() && "This should be a call instruction!");
434
435 bool MarkedNoUnwind = false;
436 bool SawFunc = false;
437
438 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
439 const MachineOperand &MO = MI->getOperand(I);
440
441 if (MO.isGlobal()) {
442 if (Function *F = dyn_cast<Function>(MO.getGlobal())) {
443 if (SawFunc) {
444 // Be conservative. If we have more than one function operand for this
445 // call, then we can't make the assumption that it's the callee and
446 // not a parameter to the call.
447 //
448 // FIXME: Determine if there's a way to say that `F' is the callee or
449 // parameter.
450 MarkedNoUnwind = false;
451 break;
452 }
Bill Wendlingecc260e2009-11-12 23:13:08 +0000453
454 MarkedNoUnwind = F->doesNotThrow();
455 SawFunc = true;
Bill Wendlinged060dc2009-11-12 21:59:20 +0000456 }
457 }
458 }
459
460 return MarkedNoUnwind;
461}
462
Bill Wendlingade025c2009-07-29 00:31:35 +0000463/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000464/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000465/// appropriate action. The entry for an ordinary call has a try-range
466/// containing the call and zero for the landing pad and the action. Calls
467/// marked 'nounwind' have no entry and must not be contained in the try-range
468/// of any entry - they form gaps in the table. Entries must be ordered by
469/// try-range address.
470void DwarfException::
471ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
472 const RangeMapType &PadMap,
473 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
474 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000475 // The end label of the previous invoke or nounwind try-range.
476 unsigned LastLabel = 0;
477
478 // Whether there is a potentially throwing instruction (currently this means
479 // an ordinary call) between the end of the previous try-range and now.
480 bool SawPotentiallyThrowing = false;
481
Bill Wendling5cff4872009-07-28 23:44:43 +0000482 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000483 bool PreviousIsInvoke = false;
484
485 // Visit all instructions in order of address.
486 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
487 I != E; ++I) {
488 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
489 MI != E; ++MI) {
490 if (!MI->isLabel()) {
Bill Wendlinged060dc2009-11-12 21:59:20 +0000491 if (MI->getDesc().isCall())
492 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI);
Bill Wendling73b55512009-11-11 23:17:02 +0000493
Bill Wendlingeb907212009-05-15 01:12:28 +0000494 continue;
495 }
496
497 unsigned BeginLabel = MI->getOperand(0).getImm();
498 assert(BeginLabel && "Invalid label!");
499
500 // End of the previous try-range?
501 if (BeginLabel == LastLabel)
502 SawPotentiallyThrowing = false;
503
504 // Beginning of a new try-range?
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000505 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
Bill Wendlingeb907212009-05-15 01:12:28 +0000506 if (L == PadMap.end())
507 // Nope, it was just some random label.
508 continue;
509
Bill Wendlinga583c552009-08-20 22:02:24 +0000510 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000511 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000512 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
513 "Inconsistent landing pad map!");
514
Bill Wendlinga583c552009-08-20 22:02:24 +0000515 // For Dwarf exception handling (SjLj handling doesn't use this). If some
516 // instruction between the previous try-range and this one may throw,
517 // create a call-site entry with no landing pad for the region between the
518 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000519 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000520 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000521 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000522 CallSites.push_back(Site);
523 PreviousIsInvoke = false;
524 }
525
526 LastLabel = LandingPad->EndLabels[P.RangeIndex];
527 assert(BeginLabel && LastLabel && "Invalid landing pad!");
528
529 if (LandingPad->LandingPadLabel) {
530 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000531 CallSiteEntry Site = {
532 BeginLabel,
533 LastLabel,
534 LandingPad->LandingPadLabel,
535 FirstActions[P.PadIndex]
536 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000537
Jim Grosbach33668c02009-09-01 17:19:13 +0000538 // Try to merge with the previous call-site. SJLJ doesn't do this
539 if (PreviousIsInvoke &&
540 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000541 CallSiteEntry &Prev = CallSites.back();
542 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
543 // Extend the range of the previous entry.
544 Prev.EndLabel = Site.EndLabel;
545 continue;
546 }
547 }
548
549 // Otherwise, create a new call-site.
Jim Grosbachca752c92010-01-28 01:45:32 +0000550 if (MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf)
551 CallSites.push_back(Site);
552 else {
553 // SjLj EH must maintain the call sites in the order assigned
554 // to them by the SjLjPrepare pass.
555 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
556 if (CallSites.size() < SiteNo)
557 CallSites.resize(SiteNo);
558 CallSites[SiteNo - 1] = Site;
559 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000560 PreviousIsInvoke = true;
561 } else {
562 // Create a gap.
563 PreviousIsInvoke = false;
564 }
565 }
566 }
567
568 // If some instruction between the previous try-range and the end of the
569 // function may throw, create a call-site entry with no landing pad for the
570 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000571 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000572 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000573 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000574 CallSites.push_back(Site);
575 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000576}
577
Bill Wendling0dafca92009-07-29 00:50:05 +0000578/// EmitExceptionTable - Emit landing pads and actions.
579///
580/// The general organization of the table is complex, but the basic concepts are
581/// easy. First there is a header which describes the location and organization
582/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000583///
Bill Wendling0dafca92009-07-29 00:50:05 +0000584/// 1. The landing pad site information describes the range of code covered by
585/// the try. In our case it's an accumulation of the ranges covered by the
586/// invokes in the try. There is also a reference to the landing pad that
587/// handles the exception once processed. Finally an index into the actions
588/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000589/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000590/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000591/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000592/// it matches then the exception and type id are passed on to the landing
593/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling28275fd2009-09-10 06:50:01 +0000594/// with a next action of zero. If no type id is found then the frame is
Bill Wendling0dafca92009-07-29 00:50:05 +0000595/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000596/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling28275fd2009-09-10 06:50:01 +0000597/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000598void DwarfException::EmitExceptionTable() {
599 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
600 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
601 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
602 if (PadInfos.empty()) return;
603
604 // Sort the landing pads in order of their type ids. This is used to fold
605 // duplicate actions.
606 SmallVector<const LandingPadInfo *, 64> LandingPads;
607 LandingPads.reserve(PadInfos.size());
608
609 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
610 LandingPads.push_back(&PadInfos[i]);
611
612 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
613
614 // Compute the actions table and gather the first action index for each
615 // landing pad site.
616 SmallVector<ActionEntry, 32> Actions;
617 SmallVector<unsigned, 64> FirstActions;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000618 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000619
620 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
621 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling28275fd2009-09-10 06:50:01 +0000622 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000623 RangeMapType PadMap;
624 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
625 const LandingPadInfo *LandingPad = LandingPads[i];
626 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
627 unsigned BeginLabel = LandingPad->BeginLabels[j];
628 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
629 PadRange P = { i, j };
630 PadMap[BeginLabel] = P;
631 }
632 }
633
634 // Compute the call-site table.
635 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000636 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000637
638 // Final tallies.
639
640 // Call sites.
Bill Wendling40121bc2009-09-10 00:13:16 +0000641 const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
642 const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
643 const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000644 bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000645 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Bill Wendling3dc9b482010-02-24 23:34:35 +0000646 unsigned CallSiteTableLength;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000647
648 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000649 CallSiteTableLength = 0;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000650 else
Bill Wendling3dc9b482010-02-24 23:34:35 +0000651 CallSiteTableLength = CallSites.size() *
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000652 (SiteStartSize + SiteLengthSize + LandingPadSize);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000653
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000654 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Bill Wendling3dc9b482010-02-24 23:34:35 +0000655 CallSiteTableLength += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000656 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000657 CallSiteTableLength += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000658 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000659
Bill Wendlingeb907212009-05-15 01:12:28 +0000660 // Type infos.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000661 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000662 unsigned TTypeEncoding;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000663 unsigned TypeFormatSize;
Bill Wendlingeb907212009-05-15 01:12:28 +0000664
Bill Wendling43e484f2009-09-10 01:12:47 +0000665 if (!HaveTTData) {
Bill Wendling28275fd2009-09-10 06:50:01 +0000666 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
667 // that we're omitting that bit.
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000668 TTypeEncoding = dwarf::DW_EH_PE_omit;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000669 TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr);
Chris Lattner81c9a062009-07-31 22:03:47 +0000670 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000671 // Okay, we have actual filters or typeinfos to emit. As such, we need to
672 // pick a type encoding for them. We're about to emit a list of pointers to
673 // typeinfo objects at the end of the LSDA. However, unless we're in static
674 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000675 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000676 // Because of this, we have a couple of options:
Bill Wendling28275fd2009-09-10 06:50:01 +0000677 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000678 // 1) If we are in -static mode, we can always use an absolute reference
679 // from the LSDA, because the static linker will resolve it.
Bill Wendling28275fd2009-09-10 06:50:01 +0000680 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000681 // 2) Otherwise, if the LSDA section is writable, we can output the direct
682 // reference to the typeinfo and allow the dynamic linker to relocate
683 // it. Since it is in a writable section, the dynamic linker won't
684 // have a problem.
Bill Wendling28275fd2009-09-10 06:50:01 +0000685 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000686 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
687 // we need to use some form of indirection. For example, on Darwin,
688 // we can output a statically-relocatable reference to a dyld stub. The
689 // offset to the stub is constant, but the contents are in a section
690 // that is updated by the dynamic linker. This is easy enough, but we
691 // need to tell the personality function of the unwinder to indirect
692 // through the dyld stub.
693 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000694 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattnerad88bc42009-08-02 03:59:56 +0000695 // somewhere. This predicate should be moved to a shared location that is
696 // in target-independent code.
697 //
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000698 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
699 TypeFormatSize = SizeOfEncodedValue(TTypeEncoding);
Bill Wendlingfe220282009-09-10 02:07:37 +0000700 }
Bill Wendling43e484f2009-09-10 01:12:47 +0000701
Bill Wendlingfe220282009-09-10 02:07:37 +0000702 // Begin the exception table.
703 Asm->OutStreamer.SwitchSection(LSDASection);
704 Asm->EmitAlignment(2, 0, 0, false);
Bill Wendling43e484f2009-09-10 01:12:47 +0000705
Bill Wendling3dc9b482010-02-24 23:34:35 +0000706 // Emit the LSDA.
Chris Lattner974c0fb2010-03-10 02:48:06 +0000707 MCSymbol *GCCETSym =
708 Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+
709 Twine(SubprogramCount));
710 Asm->OutStreamer.EmitLabel(GCCETSym);
Chris Lattnerf829eef2010-03-08 22:44:40 +0000711 Asm->OutStreamer.EmitLabel(getDWLabel("exception", SubprogramCount));
Bill Wendlingfe220282009-09-10 02:07:37 +0000712
Chris Lattner974c0fb2010-03-10 02:48:06 +0000713 if (IsSJLJ)
714 Asm->OutStreamer.EmitLabel(getDWLabel("_LSDA_", Asm->getFunctionNumber()));
Bill Wendlingfe220282009-09-10 02:07:37 +0000715
Bill Wendling3dc9b482010-02-24 23:34:35 +0000716 // Emit the LSDA header.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000717 EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000718 EmitEncodingByte(TTypeEncoding, "@TType");
Bill Wendlingfe220282009-09-10 02:07:37 +0000719
Bill Wendling3dc9b482010-02-24 23:34:35 +0000720 // The type infos need to be aligned. GCC does this by inserting padding just
721 // before the type infos. However, this changes the size of the exception
722 // table, so you need to take this into account when you output the exception
723 // table size. However, the size is output using a variable length encoding.
724 // So by increasing the size by inserting padding, you may increase the number
725 // of bytes used for writing the size. If it increases, say by one byte, then
726 // you now need to output one less byte of padding to get the type infos
727 // aligned. However this decreases the size of the exception table. This
728 // changes the value you have to output for the exception table size. Due to
729 // the variable length encoding, the number of bytes used for writing the
730 // length may decrease. If so, you then have to increase the amount of
731 // padding. And so on. If you look carefully at the GCC code you will see that
732 // it indeed does this in a loop, going on and on until the values stabilize.
733 // We chose another solution: don't output padding inside the table like GCC
734 // does, instead output it before the table.
735 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
736 unsigned CallSiteTableLengthSize =
737 MCAsmInfo::getULEB128Size(CallSiteTableLength);
738 unsigned TTypeBaseOffset =
739 sizeof(int8_t) + // Call site format
740 CallSiteTableLengthSize + // Call site table length size
741 CallSiteTableLength + // Call site table length
742 SizeActions + // Actions size
743 SizeTypes;
744 unsigned TTypeBaseOffsetSize = MCAsmInfo::getULEB128Size(TTypeBaseOffset);
745 unsigned TotalSize =
746 sizeof(int8_t) + // LPStart format
747 sizeof(int8_t) + // TType format
748 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size
749 TTypeBaseOffset; // TType base offset
750 unsigned SizeAlign = (4 - TotalSize) & 3;
751
Bill Wendling86f0d332010-02-25 23:52:44 +0000752 if (HaveTTData) {
Bill Wendling6507eca2010-02-26 21:31:01 +0000753 // Account for any extra padding that will be added to the call site table
Bill Wendlingf7e90ae2010-02-25 21:19:47 +0000754 // length.
Bill Wendling1869ac82010-02-26 22:17:52 +0000755 EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign);
756 SizeAlign = 0;
Bill Wendling86f0d332010-02-25 23:52:44 +0000757 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000758
Bill Wendling28275fd2009-09-10 06:50:01 +0000759 // SjLj Exception handling
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000760 if (IsSJLJ) {
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000761 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling1869ac82010-02-26 22:17:52 +0000762
763 // Add extra padding if it wasn't added to the TType base offset.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000764 EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000765
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000766 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000767 unsigned idx = 0;
768 for (SmallVectorImpl<CallSiteEntry>::const_iterator
769 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
770 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000771
772 // Offset of the landing pad, counted in 16-byte bundles relative to the
773 // @LPStart address.
Chris Lattner894d75a2010-01-22 23:18:42 +0000774 EmitULEB128(idx, "Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000775
776 // Offset of the first associated action record, relative to the start of
777 // the action table. This value is biased by 1 (1 indicates the start of
778 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000779 EmitULEB128(S.Action, "Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000780 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000781 } else {
782 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000783 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000784
Bill Wendlinga583c552009-08-20 22:02:24 +0000785 // The call-site table is a list of all call sites that may throw an
786 // exception (including C++ 'throw' statements) in the procedure
787 // fragment. It immediately follows the LSDA header. Each entry indicates,
788 // for a given call, the first corresponding action record and corresponding
789 // landing pad.
790 //
791 // The table begins with the number of bytes, stored as an LEB128
792 // compressed, unsigned integer. The records immediately follow the record
793 // count. They are sorted in increasing call-site address. Each record
794 // indicates:
795 //
796 // * The position of the call-site.
797 // * The position of the landing pad.
798 // * The first action record for that call site.
799 //
800 // A missing entry in the call-site table indicates that a call is not
Bill Wendling28275fd2009-09-10 06:50:01 +0000801 // supposed to throw.
Bill Wendlinga583c552009-08-20 22:02:24 +0000802
803 // Emit the landing pad call site table.
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000804 EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling1869ac82010-02-26 22:17:52 +0000805
806 // Add extra padding if it wasn't added to the TType base offset.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000807 EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000808
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000809 for (SmallVectorImpl<CallSiteEntry>::const_iterator
810 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
811 const CallSiteEntry &S = *I;
812 const char *BeginTag;
813 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000814
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000815 if (!S.BeginLabel) {
816 BeginTag = "eh_func_begin";
817 BeginNumber = SubprogramCount;
818 } else {
819 BeginTag = "label";
820 BeginNumber = S.BeginLabel;
821 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000822
Bill Wendlinga583c552009-08-20 22:02:24 +0000823 // Offset of the call site relative to the previous call site, counted in
824 // number of 16-byte bundles. The first call site is counted relative to
825 // the start of the procedure fragment.
Chris Lattner233f52b2010-03-09 23:52:58 +0000826 Asm->OutStreamer.AddComment("Region start");
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000827 EmitSectionOffset(getDWLabel(BeginTag, BeginNumber),
828 getDWLabel("eh_func_begin", SubprogramCount),
Bill Wendlingeb907212009-05-15 01:12:28 +0000829 true, true);
830
Chris Lattner233f52b2010-03-09 23:52:58 +0000831 Asm->OutStreamer.AddComment("Region length");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000832 if (!S.EndLabel)
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000833 EmitDifference(getDWLabel("eh_func_end", SubprogramCount),
834 getDWLabel(BeginTag, BeginNumber),
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000835 true);
836 else
Chris Lattnereffa8682010-03-08 23:02:59 +0000837 EmitDifference(getDWLabel("label", S.EndLabel),
838 getDWLabel(BeginTag, BeginNumber), true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000839
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000840
Bill Wendlinga583c552009-08-20 22:02:24 +0000841 // Offset of the landing pad, counted in 16-byte bundles relative to the
842 // @LPStart address.
Chris Lattner233f52b2010-03-09 23:52:58 +0000843 Asm->OutStreamer.AddComment("Landing pad");
Bill Wendling1f8075d2010-02-09 22:49:16 +0000844 if (!S.PadLabel) {
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000845 Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Bill Wendling1f8075d2010-02-09 22:49:16 +0000846 } else {
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000847 EmitSectionOffset(getDWLabel("label", S.PadLabel),
848 getDWLabel("eh_func_begin", SubprogramCount),
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000849 true, true);
Bill Wendling1f8075d2010-02-09 22:49:16 +0000850 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000851
Bill Wendlinga583c552009-08-20 22:02:24 +0000852 // Offset of the first associated action record, relative to the start of
853 // the action table. This value is biased by 1 (1 indicates the start of
854 // the action table), and 0 indicates that there are no actions.
Chris Lattner894d75a2010-01-22 23:18:42 +0000855 EmitULEB128(S.Action, "Action");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000856 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000857 }
858
Bill Wendlinga583c552009-08-20 22:02:24 +0000859 // Emit the Action Table.
Chris Lattner233f52b2010-03-09 23:52:58 +0000860 if (Actions.size() != 0) {
861 Asm->OutStreamer.AddComment("-- Action Record Table --");
862 Asm->OutStreamer.AddBlankLine();
863 }
864
Bill Wendling5cff4872009-07-28 23:44:43 +0000865 for (SmallVectorImpl<ActionEntry>::const_iterator
866 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
867 const ActionEntry &Action = *I;
Chris Lattner233f52b2010-03-09 23:52:58 +0000868 Asm->OutStreamer.AddComment("Action Record");
869 Asm->OutStreamer.AddBlankLine();
Bill Wendlinga583c552009-08-20 22:02:24 +0000870
871 // Type Filter
872 //
873 // Used by the runtime to match the type of the thrown exception to the
874 // type of the catch clauses or the types in the exception specification.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000875 EmitSLEB128(Action.ValueForTypeID, " TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000876
877 // Action Record
878 //
879 // Self-relative signed displacement in bytes of the next action record,
880 // or 0 if there is no next action record.
Bill Wendling1f8075d2010-02-09 22:49:16 +0000881 EmitSLEB128(Action.NextAction, " Next action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000882 }
883
Bill Wendling48dc29e2009-10-22 20:48:59 +0000884 // Emit the Catch TypeInfos.
Chris Lattner233f52b2010-03-09 23:52:58 +0000885 if (!TypeInfos.empty()) {
886 Asm->OutStreamer.AddComment("-- Catch TypeInfos --");
887 Asm->OutStreamer.AddBlankLine();
888 }
Bill Wendlingec044582009-11-18 23:18:46 +0000889 for (std::vector<GlobalVariable *>::const_reverse_iterator
Bill Wendling01c69372009-11-19 00:09:14 +0000890 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000891 const GlobalVariable *GV = *I;
Bill Wendlingec044582009-11-18 23:18:46 +0000892
Chris Lattner90e4af72010-03-10 00:09:21 +0000893 Asm->OutStreamer.AddComment("TypeInfo");
894 if (GV)
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000895 EmitReference(GV, TTypeEncoding);
Chris Lattner90e4af72010-03-10 00:09:21 +0000896 else
897 Asm->OutStreamer.EmitIntValue(0, SizeOfEncodedValue(TTypeEncoding), 0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000898 }
899
Bill Wendling48dc29e2009-10-22 20:48:59 +0000900 // Emit the Exception Specifications.
Chris Lattner233f52b2010-03-09 23:52:58 +0000901 if (!FilterIds.empty()) {
902 Asm->OutStreamer.AddComment("-- Filter IDs --");
903 Asm->OutStreamer.AddBlankLine();
904 }
Bill Wendling5cff4872009-07-28 23:44:43 +0000905 for (std::vector<unsigned>::const_iterator
906 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
907 unsigned TypeID = *I;
Chris Lattner894d75a2010-01-22 23:18:42 +0000908 EmitULEB128(TypeID, TypeID != 0 ? "Exception specification" : 0);
Bill Wendlingeb907212009-05-15 01:12:28 +0000909 }
910
911 Asm->EmitAlignment(2, 0, 0, false);
912}
913
Bill Wendlingeb907212009-05-15 01:12:28 +0000914/// EndModule - Emit all exception information that should come after the
915/// content.
916void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000917 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000918 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000919
Bill Wendling52783c62009-09-09 23:56:55 +0000920 if (!shouldEmitMovesModule && !shouldEmitTableModule)
921 return;
922
Bill Wendlingeb907212009-05-15 01:12:28 +0000923 if (TimePassesIsEnabled)
924 ExceptionTimer->startTimer();
925
Bill Wendling52783c62009-09-09 23:56:55 +0000926 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000927
Bill Wendling28275fd2009-09-10 06:50:01 +0000928 for (unsigned I = 0, E = Personalities.size(); I < E; ++I)
929 EmitCIE(Personalities[I], I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000930
Bill Wendling52783c62009-09-09 23:56:55 +0000931 for (std::vector<FunctionEHFrameInfo>::iterator
932 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
933 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000934
935 if (TimePassesIsEnabled)
936 ExceptionTimer->stopTimer();
937}
938
Bill Wendling28275fd2009-09-10 06:50:01 +0000939/// BeginFunction - Gather pre-function exception information. Assumes it's
940/// being emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +0000941void DwarfException::BeginFunction(const MachineFunction *MF) {
Bill Wendling73c5a612009-09-10 18:28:06 +0000942 if (!MMI || !MAI->doesSupportExceptionHandling()) return;
943
Bill Wendlingeb907212009-05-15 01:12:28 +0000944 if (TimePassesIsEnabled)
945 ExceptionTimer->startTimer();
946
947 this->MF = MF;
948 shouldEmitTable = shouldEmitMoves = false;
949
Bill Wendling73c5a612009-09-10 18:28:06 +0000950 // Map all labels and get rid of any dead landing pads.
951 MMI->TidyLandingPads();
Bill Wendlingeb907212009-05-15 01:12:28 +0000952
Bill Wendling73c5a612009-09-10 18:28:06 +0000953 // If any landing pads survive, we need an EH table.
954 if (!MMI->getLandingPads().empty())
955 shouldEmitTable = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000956
Bill Wendling73c5a612009-09-10 18:28:06 +0000957 // See if we need frame move info.
958 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
959 shouldEmitMoves = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000960
Bill Wendling73c5a612009-09-10 18:28:06 +0000961 if (shouldEmitMoves || shouldEmitTable)
962 // Assumes in correct section after the entry point.
Chris Lattnerf829eef2010-03-08 22:44:40 +0000963 Asm->OutStreamer.EmitLabel(getDWLabel("eh_func_begin", ++SubprogramCount));
Bill Wendlingeb907212009-05-15 01:12:28 +0000964
965 shouldEmitTableModule |= shouldEmitTable;
966 shouldEmitMovesModule |= shouldEmitMoves;
967
968 if (TimePassesIsEnabled)
969 ExceptionTimer->stopTimer();
970}
971
972/// EndFunction - Gather and emit post-function exception information.
973///
974void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000975 if (!shouldEmitMoves && !shouldEmitTable) return;
976
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000977 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000978 ExceptionTimer->startTimer();
979
Chris Lattnerf829eef2010-03-08 22:44:40 +0000980 Asm->OutStreamer.EmitLabel(getDWLabel("eh_func_end", SubprogramCount));
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000981 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000982
Chris Lattner09d53fe2010-03-10 07:20:42 +0000983 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000984 MCSymbol *FunctionEHSym =
Chris Lattner7a2ba942010-01-16 18:37:32 +0000985 Asm->GetSymbolWithGlobalValueBase(MF->getFunction(), ".eh",
Chris Lattner09d53fe2010-03-10 07:20:42 +0000986 TLOF.isFunctionEHFrameSymbolPrivate());
Chris Lattner25d812b2009-09-16 00:35:39 +0000987
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000988 // Save EH frame information
Chris Lattner7a2ba942010-01-16 18:37:32 +0000989 EHFrames.push_back(FunctionEHFrameInfo(FunctionEHSym, SubprogramCount,
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000990 MMI->getPersonalityIndex(),
991 MF->getFrameInfo()->hasCalls(),
992 !MMI->getLandingPads().empty(),
993 MMI->getFrameMoves(),
994 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +0000995
Bill Wendling52783c62009-09-09 23:56:55 +0000996 // Record if this personality index uses a landing pad.
997 UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
998
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000999 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +00001000 ExceptionTimer->stopTimer();
1001}