]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | //===-- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer --===// |
223e47cc LB |
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 | // | |
1a4d82fc | 10 | // This file contains support for writing exception info into assembly files. |
223e47cc LB |
11 | // |
12 | //===----------------------------------------------------------------------===// | |
13 | ||
1a4d82fc | 14 | #include "EHStreamer.h" |
223e47cc | 15 | #include "llvm/CodeGen/AsmPrinter.h" |
223e47cc | 16 | #include "llvm/CodeGen/MachineFunction.h" |
1a4d82fc | 17 | #include "llvm/CodeGen/MachineInstr.h" |
970d7e83 | 18 | #include "llvm/CodeGen/MachineModuleInfo.h" |
1a4d82fc | 19 | #include "llvm/IR/Function.h" |
223e47cc | 20 | #include "llvm/MC/MCAsmInfo.h" |
223e47cc LB |
21 | #include "llvm/MC/MCStreamer.h" |
22 | #include "llvm/MC/MCSymbol.h" | |
1a4d82fc | 23 | #include "llvm/Support/LEB128.h" |
223e47cc | 24 | #include "llvm/Target/TargetLoweringObjectFile.h" |
1a4d82fc | 25 | |
223e47cc LB |
26 | using namespace llvm; |
27 | ||
1a4d82fc | 28 | EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} |
223e47cc | 29 | |
1a4d82fc | 30 | EHStreamer::~EHStreamer() {} |
223e47cc | 31 | |
1a4d82fc JJ |
32 | /// How many leading type ids two landing pads have in common. |
33 | unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L, | |
34 | const LandingPadInfo *R) { | |
223e47cc LB |
35 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
36 | unsigned LSize = LIds.size(), RSize = RIds.size(); | |
37 | unsigned MinSize = LSize < RSize ? LSize : RSize; | |
38 | unsigned Count = 0; | |
39 | ||
40 | for (; Count != MinSize; ++Count) | |
41 | if (LIds[Count] != RIds[Count]) | |
42 | return Count; | |
43 | ||
44 | return Count; | |
45 | } | |
46 | ||
1a4d82fc JJ |
47 | /// Compute the actions table and gather the first action index for each landing |
48 | /// pad site. | |
49 | unsigned EHStreamer:: | |
50 | computeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads, | |
223e47cc LB |
51 | SmallVectorImpl<ActionEntry> &Actions, |
52 | SmallVectorImpl<unsigned> &FirstActions) { | |
53 | ||
54 | // The action table follows the call-site table in the LSDA. The individual | |
55 | // records are of two types: | |
56 | // | |
57 | // * Catch clause | |
58 | // * Exception specification | |
59 | // | |
60 | // The two record kinds have the same format, with only small differences. | |
61 | // They are distinguished by the "switch value" field: Catch clauses | |
62 | // (TypeInfos) have strictly positive switch values, and exception | |
63 | // specifications (FilterIds) have strictly negative switch values. Value 0 | |
64 | // indicates a catch-all clause. | |
65 | // | |
66 | // Negative type IDs index into FilterIds. Positive type IDs index into | |
67 | // TypeInfos. The value written for a positive type ID is just the type ID | |
68 | // itself. For a negative type ID, however, the value written is the | |
69 | // (negative) byte offset of the corresponding FilterIds entry. The byte | |
70 | // offset is usually equal to the type ID (because the FilterIds entries are | |
71 | // written using a variable width encoding, which outputs one byte per entry | |
72 | // as long as the value written is not too large) but can differ. This kind | |
73 | // of complication does not occur for positive type IDs because type infos are | |
74 | // output using a fixed width encoding. FilterOffsets[i] holds the byte | |
75 | // offset corresponding to FilterIds[i]. | |
76 | ||
77 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); | |
78 | SmallVector<int, 16> FilterOffsets; | |
79 | FilterOffsets.reserve(FilterIds.size()); | |
80 | int Offset = -1; | |
81 | ||
82 | for (std::vector<unsigned>::const_iterator | |
83 | I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { | |
84 | FilterOffsets.push_back(Offset); | |
1a4d82fc | 85 | Offset -= getULEB128Size(*I); |
223e47cc LB |
86 | } |
87 | ||
88 | FirstActions.reserve(LandingPads.size()); | |
89 | ||
90 | int FirstAction = 0; | |
91 | unsigned SizeActions = 0; | |
1a4d82fc | 92 | const LandingPadInfo *PrevLPI = nullptr; |
223e47cc LB |
93 | |
94 | for (SmallVectorImpl<const LandingPadInfo *>::const_iterator | |
95 | I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { | |
96 | const LandingPadInfo *LPI = *I; | |
97 | const std::vector<int> &TypeIds = LPI->TypeIds; | |
1a4d82fc | 98 | unsigned NumShared = PrevLPI ? sharedTypeIDs(LPI, PrevLPI) : 0; |
223e47cc LB |
99 | unsigned SizeSiteActions = 0; |
100 | ||
101 | if (NumShared < TypeIds.size()) { | |
102 | unsigned SizeAction = 0; | |
103 | unsigned PrevAction = (unsigned)-1; | |
104 | ||
105 | if (NumShared) { | |
106 | unsigned SizePrevIds = PrevLPI->TypeIds.size(); | |
107 | assert(Actions.size()); | |
108 | PrevAction = Actions.size() - 1; | |
1a4d82fc JJ |
109 | SizeAction = getSLEB128Size(Actions[PrevAction].NextAction) + |
110 | getSLEB128Size(Actions[PrevAction].ValueForTypeID); | |
223e47cc LB |
111 | |
112 | for (unsigned j = NumShared; j != SizePrevIds; ++j) { | |
113 | assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!"); | |
1a4d82fc | 114 | SizeAction -= getSLEB128Size(Actions[PrevAction].ValueForTypeID); |
223e47cc LB |
115 | SizeAction += -Actions[PrevAction].NextAction; |
116 | PrevAction = Actions[PrevAction].Previous; | |
117 | } | |
118 | } | |
119 | ||
120 | // Compute the actions. | |
121 | for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { | |
122 | int TypeID = TypeIds[J]; | |
123 | assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); | |
c34b1796 | 124 | int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; |
1a4d82fc | 125 | unsigned SizeTypeID = getSLEB128Size(ValueForTypeID); |
223e47cc LB |
126 | |
127 | int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; | |
1a4d82fc | 128 | SizeAction = SizeTypeID + getSLEB128Size(NextAction); |
223e47cc LB |
129 | SizeSiteActions += SizeAction; |
130 | ||
131 | ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; | |
132 | Actions.push_back(Action); | |
133 | PrevAction = Actions.size() - 1; | |
134 | } | |
135 | ||
136 | // Record the first action of the landing pad site. | |
137 | FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; | |
138 | } // else identical - re-use previous FirstAction | |
139 | ||
140 | // Information used when created the call-site table. The action record | |
141 | // field of the call site record is the offset of the first associated | |
142 | // action record, relative to the start of the actions table. This value is | |
143 | // biased by 1 (1 indicating the start of the actions table), and 0 | |
144 | // indicates that there are no actions. | |
145 | FirstActions.push_back(FirstAction); | |
146 | ||
147 | // Compute this sites contribution to size. | |
148 | SizeActions += SizeSiteActions; | |
149 | ||
150 | PrevLPI = LPI; | |
151 | } | |
152 | ||
153 | return SizeActions; | |
154 | } | |
155 | ||
1a4d82fc JJ |
156 | /// Return `true' if this is a call to a function marked `nounwind'. Return |
157 | /// `false' otherwise. | |
158 | bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) { | |
223e47cc LB |
159 | assert(MI->isCall() && "This should be a call instruction!"); |
160 | ||
161 | bool MarkedNoUnwind = false; | |
162 | bool SawFunc = false; | |
163 | ||
164 | for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { | |
165 | const MachineOperand &MO = MI->getOperand(I); | |
166 | ||
167 | if (!MO.isGlobal()) continue; | |
168 | ||
169 | const Function *F = dyn_cast<Function>(MO.getGlobal()); | |
1a4d82fc | 170 | if (!F) continue; |
223e47cc LB |
171 | |
172 | if (SawFunc) { | |
173 | // Be conservative. If we have more than one function operand for this | |
174 | // call, then we can't make the assumption that it's the callee and | |
175 | // not a parameter to the call. | |
176 | // | |
177 | // FIXME: Determine if there's a way to say that `F' is the callee or | |
178 | // parameter. | |
179 | MarkedNoUnwind = false; | |
180 | break; | |
181 | } | |
182 | ||
183 | MarkedNoUnwind = F->doesNotThrow(); | |
184 | SawFunc = true; | |
185 | } | |
186 | ||
187 | return MarkedNoUnwind; | |
188 | } | |
189 | ||
1a4d82fc JJ |
190 | /// Compute the call-site table. The entry for an invoke has a try-range |
191 | /// containing the call, a non-zero landing pad, and an appropriate action. The | |
192 | /// entry for an ordinary call has a try-range containing the call and zero for | |
193 | /// the landing pad and the action. Calls marked 'nounwind' have no entry and | |
194 | /// must not be contained in the try-range of any entry - they form gaps in the | |
195 | /// table. Entries must be ordered by try-range address. | |
196 | void EHStreamer:: | |
197 | computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, | |
223e47cc LB |
198 | const SmallVectorImpl<const LandingPadInfo *> &LandingPads, |
199 | const SmallVectorImpl<unsigned> &FirstActions) { | |
85aaf69f SL |
200 | // Invokes and nounwind calls have entries in PadMap (due to being bracketed |
201 | // by try-range labels when lowered). Ordinary calls do not, so appropriate | |
202 | // try-ranges for them need be deduced so we can put them in the LSDA. | |
203 | RangeMapType PadMap; | |
204 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { | |
205 | const LandingPadInfo *LandingPad = LandingPads[i]; | |
206 | for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { | |
207 | MCSymbol *BeginLabel = LandingPad->BeginLabels[j]; | |
208 | assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); | |
209 | PadRange P = { i, j }; | |
210 | PadMap[BeginLabel] = P; | |
211 | } | |
212 | } | |
213 | ||
223e47cc | 214 | // The end label of the previous invoke or nounwind try-range. |
1a4d82fc | 215 | MCSymbol *LastLabel = nullptr; |
223e47cc LB |
216 | |
217 | // Whether there is a potentially throwing instruction (currently this means | |
218 | // an ordinary call) between the end of the previous try-range and now. | |
219 | bool SawPotentiallyThrowing = false; | |
220 | ||
221 | // Whether the last CallSite entry was for an invoke. | |
222 | bool PreviousIsInvoke = false; | |
223 | ||
85aaf69f SL |
224 | bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; |
225 | ||
223e47cc | 226 | // Visit all instructions in order of address. |
1a4d82fc JJ |
227 | for (const auto &MBB : *Asm->MF) { |
228 | for (const auto &MI : MBB) { | |
229 | if (!MI.isEHLabel()) { | |
230 | if (MI.isCall()) | |
231 | SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI); | |
223e47cc LB |
232 | continue; |
233 | } | |
234 | ||
235 | // End of the previous try-range? | |
1a4d82fc | 236 | MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol(); |
223e47cc LB |
237 | if (BeginLabel == LastLabel) |
238 | SawPotentiallyThrowing = false; | |
239 | ||
240 | // Beginning of a new try-range? | |
241 | RangeMapType::const_iterator L = PadMap.find(BeginLabel); | |
242 | if (L == PadMap.end()) | |
243 | // Nope, it was just some random label. | |
244 | continue; | |
245 | ||
246 | const PadRange &P = L->second; | |
247 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; | |
248 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && | |
249 | "Inconsistent landing pad map!"); | |
250 | ||
251 | // For Dwarf exception handling (SjLj handling doesn't use this). If some | |
252 | // instruction between the previous try-range and this one may throw, | |
253 | // create a call-site entry with no landing pad for the region between the | |
254 | // try-ranges. | |
85aaf69f | 255 | if (SawPotentiallyThrowing && !IsSJLJ) { |
1a4d82fc | 256 | CallSiteEntry Site = { LastLabel, BeginLabel, nullptr, 0 }; |
223e47cc LB |
257 | CallSites.push_back(Site); |
258 | PreviousIsInvoke = false; | |
259 | } | |
260 | ||
261 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; | |
262 | assert(BeginLabel && LastLabel && "Invalid landing pad!"); | |
263 | ||
264 | if (!LandingPad->LandingPadLabel) { | |
265 | // Create a gap. | |
266 | PreviousIsInvoke = false; | |
267 | } else { | |
268 | // This try-range is for an invoke. | |
269 | CallSiteEntry Site = { | |
270 | BeginLabel, | |
271 | LastLabel, | |
c34b1796 | 272 | LandingPad->LandingPadLabel, |
223e47cc LB |
273 | FirstActions[P.PadIndex] |
274 | }; | |
275 | ||
276 | // Try to merge with the previous call-site. SJLJ doesn't do this | |
85aaf69f | 277 | if (PreviousIsInvoke && !IsSJLJ) { |
223e47cc | 278 | CallSiteEntry &Prev = CallSites.back(); |
c34b1796 | 279 | if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { |
223e47cc LB |
280 | // Extend the range of the previous entry. |
281 | Prev.EndLabel = Site.EndLabel; | |
282 | continue; | |
283 | } | |
284 | } | |
285 | ||
286 | // Otherwise, create a new call-site. | |
85aaf69f | 287 | if (!IsSJLJ) |
223e47cc LB |
288 | CallSites.push_back(Site); |
289 | else { | |
290 | // SjLj EH must maintain the call sites in the order assigned | |
291 | // to them by the SjLjPrepare pass. | |
292 | unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel); | |
293 | if (CallSites.size() < SiteNo) | |
294 | CallSites.resize(SiteNo); | |
295 | CallSites[SiteNo - 1] = Site; | |
296 | } | |
297 | PreviousIsInvoke = true; | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
302 | // If some instruction between the previous try-range and the end of the | |
303 | // function may throw, create a call-site entry with no landing pad for the | |
304 | // region following the try-range. | |
85aaf69f | 305 | if (SawPotentiallyThrowing && !IsSJLJ) { |
1a4d82fc | 306 | CallSiteEntry Site = { LastLabel, nullptr, nullptr, 0 }; |
223e47cc LB |
307 | CallSites.push_back(Site); |
308 | } | |
309 | } | |
310 | ||
1a4d82fc | 311 | /// Emit landing pads and actions. |
223e47cc LB |
312 | /// |
313 | /// The general organization of the table is complex, but the basic concepts are | |
314 | /// easy. First there is a header which describes the location and organization | |
315 | /// of the three components that follow. | |
316 | /// | |
317 | /// 1. The landing pad site information describes the range of code covered by | |
318 | /// the try. In our case it's an accumulation of the ranges covered by the | |
319 | /// invokes in the try. There is also a reference to the landing pad that | |
320 | /// handles the exception once processed. Finally an index into the actions | |
321 | /// table. | |
322 | /// 2. The action table, in our case, is composed of pairs of type IDs and next | |
323 | /// action offset. Starting with the action index from the landing pad | |
324 | /// site, each type ID is checked for a match to the current exception. If | |
325 | /// it matches then the exception and type id are passed on to the landing | |
326 | /// pad. Otherwise the next action is looked up. This chain is terminated | |
327 | /// with a next action of zero. If no type id is found then the frame is | |
328 | /// unwound and handling continues. | |
329 | /// 3. Type ID table contains references to all the C++ typeinfo for all | |
330 | /// catches in the function. This tables is reverse indexed base 1. | |
1a4d82fc | 331 | void EHStreamer::emitExceptionTable() { |
85aaf69f | 332 | const std::vector<const GlobalValue *> &TypeInfos = MMI->getTypeInfos(); |
223e47cc LB |
333 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
334 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); | |
335 | ||
336 | // Sort the landing pads in order of their type ids. This is used to fold | |
337 | // duplicate actions. | |
338 | SmallVector<const LandingPadInfo *, 64> LandingPads; | |
339 | LandingPads.reserve(PadInfos.size()); | |
340 | ||
341 | for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) | |
342 | LandingPads.push_back(&PadInfos[i]); | |
343 | ||
1a4d82fc JJ |
344 | // Order landing pads lexicographically by type id. |
345 | std::sort(LandingPads.begin(), LandingPads.end(), | |
346 | [](const LandingPadInfo *L, | |
347 | const LandingPadInfo *R) { return L->TypeIds < R->TypeIds; }); | |
223e47cc LB |
348 | |
349 | // Compute the actions table and gather the first action index for each | |
350 | // landing pad site. | |
351 | SmallVector<ActionEntry, 32> Actions; | |
352 | SmallVector<unsigned, 64> FirstActions; | |
1a4d82fc JJ |
353 | unsigned SizeActions = |
354 | computeActionsTable(LandingPads, Actions, FirstActions); | |
223e47cc | 355 | |
223e47cc LB |
356 | // Compute the call-site table. |
357 | SmallVector<CallSiteEntry, 64> CallSites; | |
85aaf69f | 358 | computeCallSiteTable(CallSites, LandingPads, FirstActions); |
223e47cc LB |
359 | |
360 | // Final tallies. | |
361 | ||
362 | // Call sites. | |
363 | bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; | |
364 | bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true; | |
365 | ||
366 | unsigned CallSiteTableLength; | |
367 | if (IsSJLJ) | |
368 | CallSiteTableLength = 0; | |
369 | else { | |
370 | unsigned SiteStartSize = 4; // dwarf::DW_EH_PE_udata4 | |
371 | unsigned SiteLengthSize = 4; // dwarf::DW_EH_PE_udata4 | |
372 | unsigned LandingPadSize = 4; // dwarf::DW_EH_PE_udata4 | |
373 | CallSiteTableLength = | |
374 | CallSites.size() * (SiteStartSize + SiteLengthSize + LandingPadSize); | |
375 | } | |
376 | ||
377 | for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { | |
1a4d82fc | 378 | CallSiteTableLength += getULEB128Size(CallSites[i].Action); |
223e47cc | 379 | if (IsSJLJ) |
1a4d82fc | 380 | CallSiteTableLength += getULEB128Size(i); |
223e47cc LB |
381 | } |
382 | ||
383 | // Type infos. | |
384 | const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); | |
385 | unsigned TTypeEncoding; | |
386 | unsigned TypeFormatSize; | |
387 | ||
388 | if (!HaveTTData) { | |
389 | // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say | |
390 | // that we're omitting that bit. | |
391 | TTypeEncoding = dwarf::DW_EH_PE_omit; | |
392 | // dwarf::DW_EH_PE_absptr | |
970d7e83 | 393 | TypeFormatSize = Asm->getDataLayout().getPointerSize(); |
223e47cc LB |
394 | } else { |
395 | // Okay, we have actual filters or typeinfos to emit. As such, we need to | |
396 | // pick a type encoding for them. We're about to emit a list of pointers to | |
397 | // typeinfo objects at the end of the LSDA. However, unless we're in static | |
398 | // mode, this reference will require a relocation by the dynamic linker. | |
399 | // | |
400 | // Because of this, we have a couple of options: | |
401 | // | |
402 | // 1) If we are in -static mode, we can always use an absolute reference | |
403 | // from the LSDA, because the static linker will resolve it. | |
404 | // | |
405 | // 2) Otherwise, if the LSDA section is writable, we can output the direct | |
406 | // reference to the typeinfo and allow the dynamic linker to relocate | |
407 | // it. Since it is in a writable section, the dynamic linker won't | |
408 | // have a problem. | |
409 | // | |
410 | // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, | |
411 | // we need to use some form of indirection. For example, on Darwin, | |
412 | // we can output a statically-relocatable reference to a dyld stub. The | |
413 | // offset to the stub is constant, but the contents are in a section | |
414 | // that is updated by the dynamic linker. This is easy enough, but we | |
415 | // need to tell the personality function of the unwinder to indirect | |
416 | // through the dyld stub. | |
417 | // | |
418 | // FIXME: When (3) is actually implemented, we'll have to emit the stubs | |
419 | // somewhere. This predicate should be moved to a shared location that is | |
420 | // in target-independent code. | |
421 | // | |
422 | TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); | |
423 | TypeFormatSize = Asm->GetSizeOfEncodedValue(TTypeEncoding); | |
424 | } | |
425 | ||
426 | // Begin the exception table. | |
427 | // Sometimes we want not to emit the data into separate section (e.g. ARM | |
428 | // EHABI). In this case LSDASection will be NULL. | |
429 | if (LSDASection) | |
430 | Asm->OutStreamer.SwitchSection(LSDASection); | |
431 | Asm->EmitAlignment(2); | |
432 | ||
433 | // Emit the LSDA. | |
434 | MCSymbol *GCCETSym = | |
435 | Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+ | |
436 | Twine(Asm->getFunctionNumber())); | |
437 | Asm->OutStreamer.EmitLabel(GCCETSym); | |
438 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("exception", | |
439 | Asm->getFunctionNumber())); | |
440 | ||
441 | if (IsSJLJ) | |
442 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("_LSDA_", | |
443 | Asm->getFunctionNumber())); | |
444 | ||
445 | // Emit the LSDA header. | |
446 | Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); | |
447 | Asm->EmitEncodingByte(TTypeEncoding, "@TType"); | |
448 | ||
449 | // The type infos need to be aligned. GCC does this by inserting padding just | |
450 | // before the type infos. However, this changes the size of the exception | |
451 | // table, so you need to take this into account when you output the exception | |
452 | // table size. However, the size is output using a variable length encoding. | |
453 | // So by increasing the size by inserting padding, you may increase the number | |
454 | // of bytes used for writing the size. If it increases, say by one byte, then | |
455 | // you now need to output one less byte of padding to get the type infos | |
456 | // aligned. However this decreases the size of the exception table. This | |
457 | // changes the value you have to output for the exception table size. Due to | |
458 | // the variable length encoding, the number of bytes used for writing the | |
459 | // length may decrease. If so, you then have to increase the amount of | |
460 | // padding. And so on. If you look carefully at the GCC code you will see that | |
461 | // it indeed does this in a loop, going on and on until the values stabilize. | |
462 | // We chose another solution: don't output padding inside the table like GCC | |
463 | // does, instead output it before the table. | |
464 | unsigned SizeTypes = TypeInfos.size() * TypeFormatSize; | |
1a4d82fc | 465 | unsigned CallSiteTableLengthSize = getULEB128Size(CallSiteTableLength); |
223e47cc LB |
466 | unsigned TTypeBaseOffset = |
467 | sizeof(int8_t) + // Call site format | |
468 | CallSiteTableLengthSize + // Call site table length size | |
469 | CallSiteTableLength + // Call site table length | |
470 | SizeActions + // Actions size | |
471 | SizeTypes; | |
1a4d82fc | 472 | unsigned TTypeBaseOffsetSize = getULEB128Size(TTypeBaseOffset); |
223e47cc LB |
473 | unsigned TotalSize = |
474 | sizeof(int8_t) + // LPStart format | |
475 | sizeof(int8_t) + // TType format | |
476 | (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size | |
477 | TTypeBaseOffset; // TType base offset | |
478 | unsigned SizeAlign = (4 - TotalSize) & 3; | |
479 | ||
480 | if (HaveTTData) { | |
481 | // Account for any extra padding that will be added to the call site table | |
482 | // length. | |
483 | Asm->EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign); | |
484 | SizeAlign = 0; | |
485 | } | |
486 | ||
487 | bool VerboseAsm = Asm->OutStreamer.isVerboseAsm(); | |
488 | ||
489 | // SjLj Exception handling | |
490 | if (IsSJLJ) { | |
491 | Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site"); | |
492 | ||
493 | // Add extra padding if it wasn't added to the TType base offset. | |
494 | Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign); | |
495 | ||
496 | // Emit the landing pad site information. | |
497 | unsigned idx = 0; | |
498 | for (SmallVectorImpl<CallSiteEntry>::const_iterator | |
499 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { | |
500 | const CallSiteEntry &S = *I; | |
501 | ||
502 | // Offset of the landing pad, counted in 16-byte bundles relative to the | |
503 | // @LPStart address. | |
504 | if (VerboseAsm) { | |
505 | Asm->OutStreamer.AddComment(">> Call Site " + Twine(idx) + " <<"); | |
506 | Asm->OutStreamer.AddComment(" On exception at call site "+Twine(idx)); | |
507 | } | |
508 | Asm->EmitULEB128(idx); | |
509 | ||
510 | // Offset of the first associated action record, relative to the start of | |
511 | // the action table. This value is biased by 1 (1 indicates the start of | |
512 | // the action table), and 0 indicates that there are no actions. | |
513 | if (VerboseAsm) { | |
514 | if (S.Action == 0) | |
515 | Asm->OutStreamer.AddComment(" Action: cleanup"); | |
516 | else | |
517 | Asm->OutStreamer.AddComment(" Action: " + | |
518 | Twine((S.Action - 1) / 2 + 1)); | |
519 | } | |
520 | Asm->EmitULEB128(S.Action); | |
521 | } | |
522 | } else { | |
85aaf69f | 523 | // Itanium LSDA exception handling |
223e47cc LB |
524 | |
525 | // The call-site table is a list of all call sites that may throw an | |
526 | // exception (including C++ 'throw' statements) in the procedure | |
527 | // fragment. It immediately follows the LSDA header. Each entry indicates, | |
528 | // for a given call, the first corresponding action record and corresponding | |
529 | // landing pad. | |
530 | // | |
531 | // The table begins with the number of bytes, stored as an LEB128 | |
532 | // compressed, unsigned integer. The records immediately follow the record | |
533 | // count. They are sorted in increasing call-site address. Each record | |
534 | // indicates: | |
535 | // | |
536 | // * The position of the call-site. | |
537 | // * The position of the landing pad. | |
538 | // * The first action record for that call site. | |
539 | // | |
540 | // A missing entry in the call-site table indicates that a call is not | |
541 | // supposed to throw. | |
542 | ||
543 | // Emit the landing pad call site table. | |
544 | Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site"); | |
545 | ||
546 | // Add extra padding if it wasn't added to the TType base offset. | |
547 | Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign); | |
548 | ||
549 | unsigned Entry = 0; | |
550 | for (SmallVectorImpl<CallSiteEntry>::const_iterator | |
551 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { | |
552 | const CallSiteEntry &S = *I; | |
553 | ||
554 | MCSymbol *EHFuncBeginSym = | |
555 | Asm->GetTempSymbol("eh_func_begin", Asm->getFunctionNumber()); | |
556 | ||
557 | MCSymbol *BeginLabel = S.BeginLabel; | |
1a4d82fc | 558 | if (!BeginLabel) |
223e47cc LB |
559 | BeginLabel = EHFuncBeginSym; |
560 | MCSymbol *EndLabel = S.EndLabel; | |
1a4d82fc | 561 | if (!EndLabel) |
223e47cc LB |
562 | EndLabel = Asm->GetTempSymbol("eh_func_end", Asm->getFunctionNumber()); |
563 | ||
564 | ||
565 | // Offset of the call site relative to the previous call site, counted in | |
566 | // number of 16-byte bundles. The first call site is counted relative to | |
567 | // the start of the procedure fragment. | |
568 | if (VerboseAsm) | |
569 | Asm->OutStreamer.AddComment(">> Call Site " + Twine(++Entry) + " <<"); | |
570 | Asm->EmitLabelDifference(BeginLabel, EHFuncBeginSym, 4); | |
571 | if (VerboseAsm) | |
572 | Asm->OutStreamer.AddComment(Twine(" Call between ") + | |
573 | BeginLabel->getName() + " and " + | |
574 | EndLabel->getName()); | |
575 | Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); | |
576 | ||
577 | // Offset of the landing pad, counted in 16-byte bundles relative to the | |
578 | // @LPStart address. | |
c34b1796 | 579 | if (!S.PadLabel) { |
223e47cc LB |
580 | if (VerboseAsm) |
581 | Asm->OutStreamer.AddComment(" has no landing pad"); | |
970d7e83 | 582 | Asm->OutStreamer.EmitIntValue(0, 4/*size*/); |
223e47cc LB |
583 | } else { |
584 | if (VerboseAsm) | |
585 | Asm->OutStreamer.AddComment(Twine(" jumps to ") + | |
c34b1796 AL |
586 | S.PadLabel->getName()); |
587 | Asm->EmitLabelDifference(S.PadLabel, EHFuncBeginSym, 4); | |
223e47cc LB |
588 | } |
589 | ||
590 | // Offset of the first associated action record, relative to the start of | |
591 | // the action table. This value is biased by 1 (1 indicates the start of | |
592 | // the action table), and 0 indicates that there are no actions. | |
593 | if (VerboseAsm) { | |
594 | if (S.Action == 0) | |
595 | Asm->OutStreamer.AddComment(" On action: cleanup"); | |
596 | else | |
597 | Asm->OutStreamer.AddComment(" On action: " + | |
598 | Twine((S.Action - 1) / 2 + 1)); | |
599 | } | |
600 | Asm->EmitULEB128(S.Action); | |
601 | } | |
602 | } | |
603 | ||
604 | // Emit the Action Table. | |
605 | int Entry = 0; | |
606 | for (SmallVectorImpl<ActionEntry>::const_iterator | |
607 | I = Actions.begin(), E = Actions.end(); I != E; ++I) { | |
608 | const ActionEntry &Action = *I; | |
609 | ||
610 | if (VerboseAsm) { | |
611 | // Emit comments that decode the action table. | |
612 | Asm->OutStreamer.AddComment(">> Action Record " + Twine(++Entry) + " <<"); | |
613 | } | |
614 | ||
615 | // Type Filter | |
616 | // | |
617 | // Used by the runtime to match the type of the thrown exception to the | |
618 | // type of the catch clauses or the types in the exception specification. | |
619 | if (VerboseAsm) { | |
620 | if (Action.ValueForTypeID > 0) | |
621 | Asm->OutStreamer.AddComment(" Catch TypeInfo " + | |
622 | Twine(Action.ValueForTypeID)); | |
623 | else if (Action.ValueForTypeID < 0) | |
624 | Asm->OutStreamer.AddComment(" Filter TypeInfo " + | |
625 | Twine(Action.ValueForTypeID)); | |
626 | else | |
627 | Asm->OutStreamer.AddComment(" Cleanup"); | |
628 | } | |
629 | Asm->EmitSLEB128(Action.ValueForTypeID); | |
630 | ||
631 | // Action Record | |
632 | // | |
633 | // Self-relative signed displacement in bytes of the next action record, | |
634 | // or 0 if there is no next action record. | |
635 | if (VerboseAsm) { | |
636 | if (Action.NextAction == 0) { | |
637 | Asm->OutStreamer.AddComment(" No further actions"); | |
638 | } else { | |
639 | unsigned NextAction = Entry + (Action.NextAction + 1) / 2; | |
640 | Asm->OutStreamer.AddComment(" Continue to action "+Twine(NextAction)); | |
641 | } | |
642 | } | |
643 | Asm->EmitSLEB128(Action.NextAction); | |
644 | } | |
645 | ||
1a4d82fc | 646 | emitTypeInfos(TTypeEncoding); |
970d7e83 LB |
647 | |
648 | Asm->EmitAlignment(2); | |
649 | } | |
650 | ||
1a4d82fc | 651 | void EHStreamer::emitTypeInfos(unsigned TTypeEncoding) { |
85aaf69f | 652 | const std::vector<const GlobalValue *> &TypeInfos = MMI->getTypeInfos(); |
970d7e83 LB |
653 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
654 | ||
655 | bool VerboseAsm = Asm->OutStreamer.isVerboseAsm(); | |
656 | ||
657 | int Entry = 0; | |
223e47cc LB |
658 | // Emit the Catch TypeInfos. |
659 | if (VerboseAsm && !TypeInfos.empty()) { | |
660 | Asm->OutStreamer.AddComment(">> Catch TypeInfos <<"); | |
661 | Asm->OutStreamer.AddBlankLine(); | |
662 | Entry = TypeInfos.size(); | |
663 | } | |
664 | ||
85aaf69f | 665 | for (std::vector<const GlobalValue *>::const_reverse_iterator |
223e47cc | 666 | I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { |
85aaf69f | 667 | const GlobalValue *GV = *I; |
223e47cc LB |
668 | if (VerboseAsm) |
669 | Asm->OutStreamer.AddComment("TypeInfo " + Twine(Entry--)); | |
970d7e83 | 670 | Asm->EmitTTypeReference(GV, TTypeEncoding); |
223e47cc LB |
671 | } |
672 | ||
673 | // Emit the Exception Specifications. | |
674 | if (VerboseAsm && !FilterIds.empty()) { | |
675 | Asm->OutStreamer.AddComment(">> Filter TypeInfos <<"); | |
676 | Asm->OutStreamer.AddBlankLine(); | |
677 | Entry = 0; | |
678 | } | |
679 | for (std::vector<unsigned>::const_iterator | |
680 | I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { | |
681 | unsigned TypeID = *I; | |
682 | if (VerboseAsm) { | |
683 | --Entry; | |
c34b1796 | 684 | if (TypeID != 0) |
223e47cc LB |
685 | Asm->OutStreamer.AddComment("FilterInfo " + Twine(Entry)); |
686 | } | |
687 | ||
688 | Asm->EmitULEB128(TypeID); | |
689 | } | |
223e47cc LB |
690 | } |
691 | ||
1a4d82fc JJ |
692 | /// Emit all exception information that should come after the content. |
693 | void EHStreamer::endModule() { | |
223e47cc LB |
694 | llvm_unreachable("Should be implemented"); |
695 | } | |
696 | ||
1a4d82fc JJ |
697 | /// Gather pre-function exception information. Assumes it's being emitted |
698 | /// immediately after the function entry point. | |
699 | void EHStreamer::beginFunction(const MachineFunction *MF) { | |
223e47cc LB |
700 | llvm_unreachable("Should be implemented"); |
701 | } | |
702 | ||
1a4d82fc JJ |
703 | /// Gather and emit post-function exception information. |
704 | void EHStreamer::endFunction(const MachineFunction *) { | |
223e47cc LB |
705 | llvm_unreachable("Should be implemented"); |
706 | } |