LLVM API Documentation
00001 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains support for writing dwarf exception info into asm files. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "DwarfException.h" 00015 #include "llvm/Module.h" 00016 #include "llvm/CodeGen/MachineModuleInfo.h" 00017 #include "llvm/CodeGen/MachineFrameInfo.h" 00018 #include "llvm/CodeGen/MachineFunction.h" 00019 #include "llvm/CodeGen/MachineLocation.h" 00020 #include "llvm/MC/MCStreamer.h" 00021 #include "llvm/Target/TargetAsmInfo.h" 00022 #include "llvm/Target/TargetData.h" 00023 #include "llvm/Target/TargetFrameInfo.h" 00024 #include "llvm/Target/TargetLoweringObjectFile.h" 00025 #include "llvm/Target/TargetOptions.h" 00026 #include "llvm/Target/TargetRegisterInfo.h" 00027 #include "llvm/Support/Dwarf.h" 00028 #include "llvm/Support/Timer.h" 00029 #include "llvm/Support/raw_ostream.h" 00030 #include "llvm/ADT/StringExtras.h" 00031 using namespace llvm; 00032 00033 static TimerGroup &getDwarfTimerGroup() { 00034 static TimerGroup DwarfTimerGroup("Dwarf Exception"); 00035 return DwarfTimerGroup; 00036 } 00037 00038 DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A, 00039 const TargetAsmInfo *T) 00040 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false), 00041 shouldEmitTableModule(false), shouldEmitMovesModule(false), 00042 ExceptionTimer(0) { 00043 if (TimePassesIsEnabled) 00044 ExceptionTimer = new Timer("Dwarf Exception Writer", 00045 getDwarfTimerGroup()); 00046 } 00047 00048 DwarfException::~DwarfException() { 00049 delete ExceptionTimer; 00050 } 00051 00052 void DwarfException::EmitCommonEHFrame(const Function *Personality, 00053 unsigned Index) { 00054 // Size and sign of stack growth. 00055 int stackGrowth = 00056 Asm->TM.getFrameInfo()->getStackGrowthDirection() == 00057 TargetFrameInfo::StackGrowsUp ? 00058 TD->getPointerSize() : -TD->getPointerSize(); 00059 00060 // Begin eh frame section. 00061 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection()); 00062 00063 if (TAI->is_EHSymbolPrivate()) 00064 O << TAI->getPrivateGlobalPrefix(); 00065 00066 O << "EH_frame" << Index << ":\n"; 00067 EmitLabel("section_eh_frame", Index); 00068 00069 // Define base labels. 00070 EmitLabel("eh_frame_common", Index); 00071 00072 // Define the eh frame length. 00073 EmitDifference("eh_frame_common_end", Index, 00074 "eh_frame_common_begin", Index, true); 00075 Asm->EOL("Length of Common Information Entry"); 00076 00077 // EH frame header. 00078 EmitLabel("eh_frame_common_begin", Index); 00079 Asm->EmitInt32((int)0); 00080 Asm->EOL("CIE Identifier Tag"); 00081 Asm->EmitInt8(dwarf::DW_CIE_VERSION); 00082 Asm->EOL("CIE Version"); 00083 00084 // The personality presence indicates that language specific information will 00085 // show up in the eh frame. 00086 Asm->EmitString(Personality ? "zPLR" : "zR"); 00087 Asm->EOL("CIE Augmentation"); 00088 00089 // Round out reader. 00090 Asm->EmitULEB128Bytes(1); 00091 Asm->EOL("CIE Code Alignment Factor"); 00092 Asm->EmitSLEB128Bytes(stackGrowth); 00093 Asm->EOL("CIE Data Alignment Factor"); 00094 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true)); 00095 Asm->EOL("CIE Return Address Column"); 00096 00097 // If there is a personality, we need to indicate the functions location. 00098 if (Personality) { 00099 Asm->EmitULEB128Bytes(7); 00100 Asm->EOL("Augmentation Size"); 00101 00102 if (TAI->getNeedsIndirectEncoding()) { 00103 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 | 00104 dwarf::DW_EH_PE_indirect); 00105 Asm->EOL("Personality (pcrel sdata4 indirect)"); 00106 } else { 00107 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); 00108 Asm->EOL("Personality (pcrel sdata4)"); 00109 } 00110 00111 PrintRelDirective(true); 00112 O << TAI->getPersonalityPrefix(); 00113 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality)); 00114 O << TAI->getPersonalitySuffix(); 00115 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL")) 00116 O << "-" << TAI->getPCSymbol(); 00117 Asm->EOL("Personality"); 00118 00119 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); 00120 Asm->EOL("LSDA Encoding (pcrel sdata4)"); 00121 00122 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); 00123 Asm->EOL("FDE Encoding (pcrel sdata4)"); 00124 } else { 00125 Asm->EmitULEB128Bytes(1); 00126 Asm->EOL("Augmentation Size"); 00127 00128 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); 00129 Asm->EOL("FDE Encoding (pcrel sdata4)"); 00130 } 00131 00132 // Indicate locations of general callee saved registers in frame. 00133 std::vector<MachineMove> Moves; 00134 RI->getInitialFrameState(Moves); 00135 EmitFrameMoves(NULL, 0, Moves, true); 00136 00137 // On Darwin the linker honors the alignment of eh_frame, which means it must 00138 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get 00139 // holes which confuse readers of eh_frame. 00140 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, 00141 0, 0, false); 00142 EmitLabel("eh_frame_common_end", Index); 00143 00144 Asm->EOL(); 00145 } 00146 00147 /// EmitEHFrame - Emit function exception frame information. 00148 /// 00149 void DwarfException::EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) { 00150 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() && 00151 "Should not emit 'available externally' functions at all"); 00152 00153 const Function *TheFunc = EHFrameInfo.function; 00154 00155 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection()); 00156 00157 // Externally visible entry into the functions eh frame info. If the 00158 // corresponding function is static, this should not be externally visible. 00159 if (!TheFunc->hasLocalLinkage()) 00160 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective()) 00161 O << GlobalEHDirective << EHFrameInfo.FnName << "\n"; 00162 00163 // If corresponding function is weak definition, this should be too. 00164 if (TheFunc->isWeakForLinker() && TAI->getWeakDefDirective()) 00165 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n"; 00166 00167 // If there are no calls then you can't unwind. This may mean we can omit the 00168 // EH Frame, but some environments do not handle weak absolute symbols. If 00169 // UnwindTablesMandatory is set we cannot do this optimization; the unwind 00170 // info is to be available for non-EH uses. 00171 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory && 00172 (!TheFunc->isWeakForLinker() || 00173 !TAI->getWeakDefDirective() || 00174 TAI->getSupportsWeakOmittedEHFrame())) { 00175 O << EHFrameInfo.FnName << " = 0\n"; 00176 // This name has no connection to the function, so it might get 00177 // dead-stripped when the function is not, erroneously. Prohibit 00178 // dead-stripping unconditionally. 00179 if (const char *UsedDirective = TAI->getUsedDirective()) 00180 O << UsedDirective << EHFrameInfo.FnName << "\n\n"; 00181 } else { 00182 O << EHFrameInfo.FnName << ":\n"; 00183 00184 // EH frame header. 00185 EmitDifference("eh_frame_end", EHFrameInfo.Number, 00186 "eh_frame_begin", EHFrameInfo.Number, true); 00187 Asm->EOL("Length of Frame Information Entry"); 00188 00189 EmitLabel("eh_frame_begin", EHFrameInfo.Number); 00190 00191 EmitSectionOffset("eh_frame_begin", "eh_frame_common", 00192 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex, 00193 true, true, false); 00194 00195 Asm->EOL("FDE CIE offset"); 00196 00197 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true); 00198 Asm->EOL("FDE initial location"); 00199 EmitDifference("eh_func_end", EHFrameInfo.Number, 00200 "eh_func_begin", EHFrameInfo.Number, true); 00201 Asm->EOL("FDE address range"); 00202 00203 // If there is a personality and landing pads then point to the language 00204 // specific data area in the exception table. 00205 if (MMI->getPersonalities()[0] != NULL) { 00206 bool is4Byte = TD->getPointerSize() == sizeof(int32_t); 00207 00208 Asm->EmitULEB128Bytes(is4Byte ? 4 : 8); 00209 Asm->EOL("Augmentation size"); 00210 00211 if (EHFrameInfo.hasLandingPads) 00212 EmitReference("exception", EHFrameInfo.Number, true, false); 00213 else { 00214 if (is4Byte) 00215 Asm->EmitInt32((int)0); 00216 else 00217 Asm->EmitInt64((int)0); 00218 } 00219 Asm->EOL("Language Specific Data Area"); 00220 } else { 00221 Asm->EmitULEB128Bytes(0); 00222 Asm->EOL("Augmentation size"); 00223 } 00224 00225 // Indicate locations of function specific callee saved registers in frame. 00226 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, 00227 true); 00228 00229 // On Darwin the linker honors the alignment of eh_frame, which means it 00230 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you 00231 // get holes which confuse readers of eh_frame. 00232 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, 00233 0, 0, false); 00234 EmitLabel("eh_frame_end", EHFrameInfo.Number); 00235 00236 // If the function is marked used, this table should be also. We cannot 00237 // make the mark unconditional in this case, since retaining the table also 00238 // retains the function in this case, and there is code around that depends 00239 // on unused functions (calling undefined externals) being dead-stripped to 00240 // link correctly. Yes, there really is. 00241 if (MMI->isUsedFunction(EHFrameInfo.function)) 00242 if (const char *UsedDirective = TAI->getUsedDirective()) 00243 O << UsedDirective << EHFrameInfo.FnName << "\n\n"; 00244 } 00245 } 00246 00247 /// SharedTypeIds - How many leading type ids two landing pads have in common. 00248 unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L, 00249 const LandingPadInfo *R) { 00250 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 00251 unsigned LSize = LIds.size(), RSize = RIds.size(); 00252 unsigned MinSize = LSize < RSize ? LSize : RSize; 00253 unsigned Count = 0; 00254 00255 for (; Count != MinSize; ++Count) 00256 if (LIds[Count] != RIds[Count]) 00257 return Count; 00258 00259 return Count; 00260 } 00261 00262 /// PadLT - Order landing pads lexicographically by type id. 00263 bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { 00264 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 00265 unsigned LSize = LIds.size(), RSize = RIds.size(); 00266 unsigned MinSize = LSize < RSize ? LSize : RSize; 00267 00268 for (unsigned i = 0; i != MinSize; ++i) 00269 if (LIds[i] != RIds[i]) 00270 return LIds[i] < RIds[i]; 00271 00272 return LSize < RSize; 00273 } 00274 00275 /// ComputeActionsTable - Compute the actions table and gather the first action 00276 /// index for each landing pad site. 00277 unsigned DwarfException:: 00278 ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads, 00279 SmallVectorImpl<ActionEntry> &Actions, 00280 SmallVectorImpl<unsigned> &FirstActions) { 00281 00282 // The action table follows the call-site table in the LSDA. The individual 00283 // records are of two types: 00284 // 00285 // * Catch clause 00286 // * Exception specification 00287 // 00288 // The two record kinds have the same format, with only small differences. 00289 // They are distinguished by the "switch value" field: Catch clauses 00290 // (TypeInfos) have strictly positive switch values, and exception 00291 // specifications (FilterIds) have strictly negative switch values. Value 0 00292 // indicates a catch-all clause. 00293 // 00294 // Negative type IDs index into FilterIds. Positive type IDs index into 00295 // TypeInfos. The value written for a positive type ID is just the type ID 00296 // itself. For a negative type ID, however, the value written is the 00297 // (negative) byte offset of the corresponding FilterIds entry. The byte 00298 // offset is usually equal to the type ID (because the FilterIds entries are 00299 // written using a variable width encoding, which outputs one byte per entry 00300 // as long as the value written is not too large) but can differ. This kind 00301 // of complication does not occur for positive type IDs because type infos are 00302 // output using a fixed width encoding. FilterOffsets[i] holds the byte 00303 // offset corresponding to FilterIds[i]. 00304 00305 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 00306 SmallVector<int, 16> FilterOffsets; 00307 FilterOffsets.reserve(FilterIds.size()); 00308 int Offset = -1; 00309 00310 for (std::vector<unsigned>::const_iterator 00311 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { 00312 FilterOffsets.push_back(Offset); 00313 Offset -= TargetAsmInfo::getULEB128Size(*I); 00314 } 00315 00316 FirstActions.reserve(LandingPads.size()); 00317 00318 int FirstAction = 0; 00319 unsigned SizeActions = 0; 00320 const LandingPadInfo *PrevLPI = 0; 00321 00322 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator 00323 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { 00324 const LandingPadInfo *LPI = *I; 00325 const std::vector<int> &TypeIds = LPI->TypeIds; 00326 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0; 00327 unsigned SizeSiteActions = 0; 00328 00329 if (NumShared < TypeIds.size()) { 00330 unsigned SizeAction = 0; 00331 ActionEntry *PrevAction = 0; 00332 00333 if (NumShared) { 00334 const unsigned SizePrevIds = PrevLPI->TypeIds.size(); 00335 assert(Actions.size()); 00336 PrevAction = &Actions.back(); 00337 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) + 00338 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); 00339 00340 for (unsigned j = NumShared; j != SizePrevIds; ++j) { 00341 SizeAction -= 00342 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); 00343 SizeAction += -PrevAction->NextAction; 00344 PrevAction = PrevAction->Previous; 00345 } 00346 } 00347 00348 // Compute the actions. 00349 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { 00350 int TypeID = TypeIds[J]; 00351 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 00352 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; 00353 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID); 00354 00355 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; 00356 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction); 00357 SizeSiteActions += SizeAction; 00358 00359 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; 00360 Actions.push_back(Action); 00361 PrevAction = &Actions.back(); 00362 } 00363 00364 // Record the first action of the landing pad site. 00365 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; 00366 } // else identical - re-use previous FirstAction 00367 00368 // Information used when created the call-site table. The action record 00369 // field of the call site record is the offset of the first associated 00370 // action record, relative to the start of the actions table. This value is 00371 // biased by 1 (1 in dicating the start of the actions table), and 0 00372 // indicates that there are no actions. 00373 FirstActions.push_back(FirstAction); 00374 00375 // Compute this sites contribution to size. 00376 SizeActions += SizeSiteActions; 00377 00378 PrevLPI = LPI; 00379 } 00380 00381 return SizeActions; 00382 } 00383 00384 /// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke 00385 /// has a try-range containing the call, a non-zero landing pad, and an 00386 /// appropriate action. The entry for an ordinary call has a try-range 00387 /// containing the call and zero for the landing pad and the action. Calls 00388 /// marked 'nounwind' have no entry and must not be contained in the try-range 00389 /// of any entry - they form gaps in the table. Entries must be ordered by 00390 /// try-range address. 00391 void DwarfException:: 00392 ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, 00393 const RangeMapType &PadMap, 00394 const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 00395 const SmallVectorImpl<unsigned> &FirstActions) { 00396 // The end label of the previous invoke or nounwind try-range. 00397 unsigned LastLabel = 0; 00398 00399 // Whether there is a potentially throwing instruction (currently this means 00400 // an ordinary call) between the end of the previous try-range and now. 00401 bool SawPotentiallyThrowing = false; 00402 00403 // Whether the last CallSite entry was for an invoke. 00404 bool PreviousIsInvoke = false; 00405 00406 // Visit all instructions in order of address. 00407 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 00408 I != E; ++I) { 00409 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); 00410 MI != E; ++MI) { 00411 if (!MI->isLabel()) { 00412 SawPotentiallyThrowing |= MI->getDesc().isCall(); 00413 continue; 00414 } 00415 00416 unsigned BeginLabel = MI->getOperand(0).getImm(); 00417 assert(BeginLabel && "Invalid label!"); 00418 00419 // End of the previous try-range? 00420 if (BeginLabel == LastLabel) 00421 SawPotentiallyThrowing = false; 00422 00423 // Beginning of a new try-range? 00424 RangeMapType::iterator L = PadMap.find(BeginLabel); 00425 if (L == PadMap.end()) 00426 // Nope, it was just some random label. 00427 continue; 00428 00429 const PadRange &P = L->second; 00430 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 00431 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 00432 "Inconsistent landing pad map!"); 00433 00434 // For Dwarf exception handling (SjLj handling doesn't use this). If some 00435 // instruction between the previous try-range and this one may throw, 00436 // create a call-site entry with no landing pad for the region between the 00437 // try-ranges. 00438 if (SawPotentiallyThrowing && 00439 TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { 00440 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 }; 00441 CallSites.push_back(Site); 00442 PreviousIsInvoke = false; 00443 } 00444 00445 LastLabel = LandingPad->EndLabels[P.RangeIndex]; 00446 assert(BeginLabel && LastLabel && "Invalid landing pad!"); 00447 00448 if (LandingPad->LandingPadLabel) { 00449 // This try-range is for an invoke. 00450 CallSiteEntry Site = { 00451 BeginLabel, 00452 LastLabel, 00453 LandingPad->LandingPadLabel, 00454 FirstActions[P.PadIndex] 00455 }; 00456 00457 // Try to merge with the previous call-site. 00458 if (PreviousIsInvoke) { 00459 CallSiteEntry &Prev = CallSites.back(); 00460 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { 00461 // Extend the range of the previous entry. 00462 Prev.EndLabel = Site.EndLabel; 00463 continue; 00464 } 00465 } 00466 00467 // Otherwise, create a new call-site. 00468 CallSites.push_back(Site); 00469 PreviousIsInvoke = true; 00470 } else { 00471 // Create a gap. 00472 PreviousIsInvoke = false; 00473 } 00474 } 00475 } 00476 00477 // If some instruction between the previous try-range and the end of the 00478 // function may throw, create a call-site entry with no landing pad for the 00479 // region following the try-range. 00480 if (SawPotentiallyThrowing && 00481 TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { 00482 CallSiteEntry Site = { LastLabel, 0, 0, 0 }; 00483 CallSites.push_back(Site); 00484 } 00485 } 00486 00487 /// EmitExceptionTable - Emit landing pads and actions. 00488 /// 00489 /// The general organization of the table is complex, but the basic concepts are 00490 /// easy. First there is a header which describes the location and organization 00491 /// of the three components that follow. 00492 /// 00493 /// 1. The landing pad site information describes the range of code covered by 00494 /// the try. In our case it's an accumulation of the ranges covered by the 00495 /// invokes in the try. There is also a reference to the landing pad that 00496 /// handles the exception once processed. Finally an index into the actions 00497 /// table. 00498 /// 2. The action table, in our case, is composed of pairs of type IDs and next 00499 /// action offset. Starting with the action index from the landing pad 00500 /// site, each type ID is checked for a match to the current exception. If 00501 /// it matches then the exception and type id are passed on to the landing 00502 /// pad. Otherwise the next action is looked up. This chain is terminated 00503 /// with a next action of zero. If no type id is found the the frame is 00504 /// unwound and handling continues. 00505 /// 3. Type ID table contains references to all the C++ typeinfo for all 00506 /// catches in the function. This tables is reversed indexed base 1. 00507 void DwarfException::EmitExceptionTable() { 00508 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); 00509 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 00510 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); 00511 if (PadInfos.empty()) return; 00512 00513 // Sort the landing pads in order of their type ids. This is used to fold 00514 // duplicate actions. 00515 SmallVector<const LandingPadInfo *, 64> LandingPads; 00516 LandingPads.reserve(PadInfos.size()); 00517 00518 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 00519 LandingPads.push_back(&PadInfos[i]); 00520 00521 std::sort(LandingPads.begin(), LandingPads.end(), PadLT); 00522 00523 // Compute the actions table and gather the first action index for each 00524 // landing pad site. 00525 SmallVector<ActionEntry, 32> Actions; 00526 SmallVector<unsigned, 64> FirstActions; 00527 unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions); 00528 00529 // Invokes and nounwind calls have entries in PadMap (due to being bracketed 00530 // by try-range labels when lowered). Ordinary calls do not, so appropriate 00531 // try-ranges for them need be deduced when using Dwarf exception handling. 00532 RangeMapType PadMap; 00533 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 00534 const LandingPadInfo *LandingPad = LandingPads[i]; 00535 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 00536 unsigned BeginLabel = LandingPad->BeginLabels[j]; 00537 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 00538 PadRange P = { i, j }; 00539 PadMap[BeginLabel] = P; 00540 } 00541 } 00542 00543 // Compute the call-site table. 00544 SmallVector<CallSiteEntry, 64> CallSites; 00545 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions); 00546 00547 // Final tallies. 00548 00549 // Call sites. 00550 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4 00551 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4 00552 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4 00553 unsigned SizeSites; 00554 00555 bool HaveTTData = (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) 00556 ? (!TypeInfos.empty() || !FilterIds.empty()) : true; 00557 00558 00559 if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) { 00560 SizeSites = 0; 00561 } else 00562 SizeSites = CallSites.size() * 00563 (SiteStartSize + SiteLengthSize + LandingPadSize); 00564 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { 00565 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action); 00566 if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) 00567 SizeSites += TargetAsmInfo::getULEB128Size(i); 00568 } 00569 // Type infos. 00570 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr 00571 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize; 00572 00573 unsigned TypeOffset = sizeof(int8_t) + // Call site format 00574 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length 00575 SizeSites + SizeActions + SizeTypes; 00576 00577 unsigned TotalSize = sizeof(int8_t) + // LPStart format 00578 sizeof(int8_t) + // TType format 00579 (HaveTTData ? 00580 TargetAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset 00581 TypeOffset; 00582 00583 unsigned SizeAlign = (4 - TotalSize) & 3; 00584 00585 // Begin the exception table. 00586 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); 00587 Asm->OutStreamer.SwitchSection(LSDASection); 00588 Asm->EmitAlignment(2, 0, 0, false); 00589 O << "GCC_except_table" << SubprogramCount << ":\n"; 00590 00591 for (unsigned i = 0; i != SizeAlign; ++i) { 00592 Asm->EmitInt8(0); 00593 Asm->EOL("Padding"); 00594 } 00595 00596 EmitLabel("exception", SubprogramCount); 00597 if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) { 00598 std::string SjLjName = "_lsda_"; 00599 SjLjName += MF->getFunction()->getName().str(); 00600 EmitLabel(SjLjName.c_str(), 0); 00601 } 00602 00603 // Emit the header. 00604 Asm->EmitInt8(dwarf::DW_EH_PE_omit); 00605 Asm->EOL("@LPStart format (DW_EH_PE_omit)"); 00606 00607 #if 0 00608 if (TypeInfos.empty() && FilterIds.empty()) { 00609 // If there are no typeinfos or filters, there is nothing to emit, optimize 00610 // by specifying the "omit" encoding. 00611 Asm->EmitInt8(dwarf::DW_EH_PE_omit); 00612 Asm->EOL("@TType format (DW_EH_PE_omit)"); 00613 } else { 00614 // Okay, we have actual filters or typeinfos to emit. As such, we need to 00615 // pick a type encoding for them. We're about to emit a list of pointers to 00616 // typeinfo objects at the end of the LSDA. However, unless we're in static 00617 // mode, this reference will require a relocation by the dynamic linker. 00618 // 00619 // Because of this, we have a couple of options: 00620 // 1) If we are in -static mode, we can always use an absolute reference 00621 // from the LSDA, because the static linker will resolve it. 00622 // 2) Otherwise, if the LSDA section is writable, we can output the direct 00623 // reference to the typeinfo and allow the dynamic linker to relocate 00624 // it. Since it is in a writable section, the dynamic linker won't 00625 // have a problem. 00626 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 00627 // we need to use some form of indirection. For example, on Darwin, 00628 // we can output a statically-relocatable reference to a dyld stub. The 00629 // offset to the stub is constant, but the contents are in a section 00630 // that is updated by the dynamic linker. This is easy enough, but we 00631 // need to tell the personality function of the unwinder to indirect 00632 // through the dyld stub. 00633 // 00634 // FIXME: When this is actually implemented, we'll have to emit the stubs 00635 // somewhere. This predicate should be moved to a shared location that is 00636 // in target-independent code. 00637 // 00638 if (LSDASection->isWritable() || 00639 Asm->TM.getRelocationModel() == Reloc::Static) { 00640 Asm->EmitInt8(DW_EH_PE_absptr); 00641 Asm->EOL("TType format (DW_EH_PE_absptr)"); 00642 } else { 00643 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4); 00644 Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect" 00645 " | DW_EH_PE_sdata4)"); 00646 } 00647 Asm->EmitULEB128Bytes(TypeOffset); 00648 Asm->EOL("TType base offset"); 00649 } 00650 #else 00651 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly 00652 // say that we're omitting that bit. 00653 // FIXME: does this apply to Dwarf also? The above #if 0 implies yes? 00654 if (!HaveTTData) { 00655 Asm->EmitInt8(dwarf::DW_EH_PE_omit); 00656 Asm->EOL("@TType format (DW_EH_PE_omit)"); 00657 } else { 00658 Asm->EmitInt8(dwarf::DW_EH_PE_absptr); 00659 Asm->EOL("@TType format (DW_EH_PE_absptr)"); 00660 Asm->EmitULEB128Bytes(TypeOffset); 00661 Asm->EOL("@TType base offset"); 00662 } 00663 #endif 00664 00665 // SjLj Exception handilng 00666 if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) { 00667 Asm->EmitInt8(dwarf::DW_EH_PE_udata4); 00668 Asm->EOL("Call site format (DW_EH_PE_udata4)"); 00669 Asm->EmitULEB128Bytes(SizeSites); 00670 Asm->EOL("Call site table length"); 00671 00672 // Emit the landing pad site information. 00673 unsigned idx = 0; 00674 for (SmallVectorImpl<CallSiteEntry>::const_iterator 00675 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 00676 const CallSiteEntry &S = *I; 00677 00678 // Offset of the landing pad, counted in 16-byte bundles relative to the 00679 // @LPStart address. 00680 Asm->EmitULEB128Bytes(idx); 00681 Asm->EOL("Landing pad"); 00682 00683 // Offset of the first associated action record, relative to the start of 00684 // the action table. This value is biased by 1 (1 indicates the start of 00685 // the action table), and 0 indicates that there are no actions. 00686 Asm->EmitULEB128Bytes(S.Action); 00687 Asm->EOL("Action"); 00688 } 00689 } else { 00690 // DWARF Exception handling 00691 assert(TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf); 00692 00693 // The call-site table is a list of all call sites that may throw an 00694 // exception (including C++ 'throw' statements) in the procedure 00695 // fragment. It immediately follows the LSDA header. Each entry indicates, 00696 // for a given call, the first corresponding action record and corresponding 00697 // landing pad. 00698 // 00699 // The table begins with the number of bytes, stored as an LEB128 00700 // compressed, unsigned integer. The records immediately follow the record 00701 // count. They are sorted in increasing call-site address. Each record 00702 // indicates: 00703 // 00704 // * The position of the call-site. 00705 // * The position of the landing pad. 00706 // * The first action record for that call site. 00707 // 00708 // A missing entry in the call-site table indicates that a call is not 00709 // supposed to throw. Such calls include: 00710 // 00711 // * Calls to destructors within cleanup code. C++ semantics forbids these 00712 // calls to throw. 00713 // * Calls to intrinsic routines in the standard library which are known 00714 // not to throw (sin, memcpy, et al). 00715 // 00716 // If the runtime does not find the call-site entry for a given call, it 00717 // will call `terminate()'. 00718 00719 // Emit the landing pad call site table. 00720 Asm->EmitInt8(dwarf::DW_EH_PE_udata4); 00721 Asm->EOL("Call site format (DW_EH_PE_udata4)"); 00722 Asm->EmitULEB128Bytes(SizeSites); 00723 Asm->EOL("Call site table size"); 00724 00725 for (SmallVectorImpl<CallSiteEntry>::const_iterator 00726 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { 00727 const CallSiteEntry &S = *I; 00728 const char *BeginTag; 00729 unsigned BeginNumber; 00730 00731 if (!S.BeginLabel) { 00732 BeginTag = "eh_func_begin"; 00733 BeginNumber = SubprogramCount; 00734 } else { 00735 BeginTag = "label"; 00736 BeginNumber = S.BeginLabel; 00737 } 00738 00739 // Offset of the call site relative to the previous call site, counted in 00740 // number of 16-byte bundles. The first call site is counted relative to 00741 // the start of the procedure fragment. 00742 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount, 00743 true, true); 00744 Asm->EOL("Region start"); 00745 00746 if (!S.EndLabel) 00747 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber, 00748 true); 00749 else 00750 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true); 00751 00752 Asm->EOL("Region length"); 00753 00754 // Offset of the landing pad, counted in 16-byte bundles relative to the 00755 // @LPStart address. 00756 if (!S.PadLabel) 00757 Asm->EmitInt32(0); 00758 else 00759 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount, 00760 true, true); 00761 00762 Asm->EOL("Landing pad"); 00763 00764 // Offset of the first associated action record, relative to the start of 00765 // the action table. This value is biased by 1 (1 indicates the start of 00766 // the action table), and 0 indicates that there are no actions. 00767 Asm->EmitULEB128Bytes(S.Action); 00768 Asm->EOL("Action"); 00769 } 00770 } 00771 00772 // Emit the Action Table. 00773 for (SmallVectorImpl<ActionEntry>::const_iterator 00774 I = Actions.begin(), E = Actions.end(); I != E; ++I) { 00775 const ActionEntry &Action = *I; 00776 00777 // Type Filter 00778 // 00779 // Used by the runtime to match the type of the thrown exception to the 00780 // type of the catch clauses or the types in the exception specification. 00781 00782 Asm->EmitSLEB128Bytes(Action.ValueForTypeID); 00783 Asm->EOL("TypeInfo index"); 00784 00785 // Action Record 00786 // 00787 // Self-relative signed displacement in bytes of the next action record, 00788 // or 0 if there is no next action record. 00789 00790 Asm->EmitSLEB128Bytes(Action.NextAction); 00791 Asm->EOL("Next action"); 00792 } 00793 00794 // Emit the Catch Clauses. The code for the catch clauses following the same 00795 // try is similar to a switch statement. The catch clause action record 00796 // informs the runtime about the type of a catch clause and about the 00797 // associated switch value. 00798 // 00799 // Action Record Fields: 00800 // 00801 // * Filter Value 00802 // Positive value, starting at 1. Index in the types table of the 00803 // __typeinfo for the catch-clause type. 1 is the first word preceding 00804 // TTBase, 2 is the second word, and so on. Used by the runtime to check 00805 // if the thrown exception type matches the catch-clause type. Back-end 00806 // generated switch statements check against this value. 00807 // 00808 // * Next 00809 // Signed offset, in bytes from the start of this field, to the next 00810 // chained action record, or zero if none. 00811 // 00812 // The order of the action records determined by the next field is the order 00813 // of the catch clauses as they appear in the source code, and must be kept in 00814 // the same order. As a result, changing the order of the catch clause would 00815 // change the semantics of the program. 00816 for (std::vector<GlobalVariable *>::const_reverse_iterator 00817 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { 00818 const GlobalVariable *GV = *I; 00819 PrintRelDirective(); 00820 00821 if (GV) { 00822 std::string GLN; 00823 O << Asm->getGlobalLinkName(GV, GLN); 00824 } else { 00825 O << "0"; 00826 } 00827 00828 Asm->EOL("TypeInfo"); 00829 } 00830 00831 // Emit the Type Table. 00832 for (std::vector<unsigned>::const_iterator 00833 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 00834 unsigned TypeID = *I; 00835 Asm->EmitULEB128Bytes(TypeID); 00836 Asm->EOL("Filter TypeInfo index"); 00837 } 00838 00839 Asm->EmitAlignment(2, 0, 0, false); 00840 } 00841 00842 /// EndModule - Emit all exception information that should come after the 00843 /// content. 00844 void DwarfException::EndModule() { 00845 if (TAI->getExceptionHandlingType() != ExceptionHandling::Dwarf) 00846 return; 00847 if (TimePassesIsEnabled) 00848 ExceptionTimer->startTimer(); 00849 00850 if (shouldEmitMovesModule || shouldEmitTableModule) { 00851 const std::vector<Function *> Personalities = MMI->getPersonalities(); 00852 for (unsigned i = 0; i < Personalities.size(); ++i) 00853 EmitCommonEHFrame(Personalities[i], i); 00854 00855 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(), 00856 E = EHFrames.end(); I != E; ++I) 00857 EmitEHFrame(*I); 00858 } 00859 00860 if (TimePassesIsEnabled) 00861 ExceptionTimer->stopTimer(); 00862 } 00863 00864 /// BeginFunction - Gather pre-function exception information. Assumes being 00865 /// emitted immediately after the function entry point. 00866 void DwarfException::BeginFunction(MachineFunction *MF) { 00867 if (TimePassesIsEnabled) 00868 ExceptionTimer->startTimer(); 00869 00870 this->MF = MF; 00871 shouldEmitTable = shouldEmitMoves = false; 00872 00873 if (MMI && TAI->doesSupportExceptionHandling()) { 00874 // Map all labels and get rid of any dead landing pads. 00875 MMI->TidyLandingPads(); 00876 00877 // If any landing pads survive, we need an EH table. 00878 if (MMI->getLandingPads().size()) 00879 shouldEmitTable = true; 00880 00881 // See if we need frame move info. 00882 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory) 00883 shouldEmitMoves = true; 00884 00885 if (shouldEmitMoves || shouldEmitTable) 00886 // Assumes in correct section after the entry point. 00887 EmitLabel("eh_func_begin", ++SubprogramCount); 00888 } 00889 00890 shouldEmitTableModule |= shouldEmitTable; 00891 shouldEmitMovesModule |= shouldEmitMoves; 00892 00893 if (TimePassesIsEnabled) 00894 ExceptionTimer->stopTimer(); 00895 } 00896 00897 /// EndFunction - Gather and emit post-function exception information. 00898 /// 00899 void DwarfException::EndFunction() { 00900 if (TimePassesIsEnabled) 00901 ExceptionTimer->startTimer(); 00902 00903 if (shouldEmitMoves || shouldEmitTable) { 00904 EmitLabel("eh_func_end", SubprogramCount); 00905 EmitExceptionTable(); 00906 00907 // Save EH frame information 00908 EHFrames.push_back( 00909 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF), 00910 SubprogramCount, 00911 MMI->getPersonalityIndex(), 00912 MF->getFrameInfo()->hasCalls(), 00913 !MMI->getLandingPads().empty(), 00914 MMI->getFrameMoves(), 00915 MF->getFunction())); 00916 } 00917 00918 if (TimePassesIsEnabled) 00919 ExceptionTimer->stopTimer(); 00920 }