| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 1 | //===-- 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 Wendling | 639217c | 2009-08-27 03:32:50 +0000 | [diff] [blame] | 10 | // This file contains support for writing dwarf exception info into asm files. | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
|  | 14 | #include "DwarfException.h" | 
|  | 15 | #include "llvm/Module.h" | 
|  | 16 | #include "llvm/CodeGen/MachineModuleInfo.h" | 
|  | 17 | #include "llvm/CodeGen/MachineFrameInfo.h" | 
| David Greene | fc4da0c | 2009-08-19 21:55:33 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineLocation.h" | 
| Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCSection.h" | 
| Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCStreamer.h" | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCAsmInfo.h" | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" | 
|  | 24 | #include "llvm/Target/TargetFrameInfo.h" | 
| Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetLoweringObjectFile.h" | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" | 
| Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetRegisterInfo.h" | 
| Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Dwarf.h" | 
| Jim Grosbach | 3fb2b1e | 2009-09-01 01:57:56 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Mangler.h" | 
| Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Timer.h" | 
|  | 31 | #include "llvm/Support/raw_ostream.h" | 
| Jim Grosbach | c40d9f9 | 2009-09-01 18:49:12 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallString.h" | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" | 
|  | 34 | using namespace llvm; | 
|  | 35 |  | 
|  | 36 | static TimerGroup &getDwarfTimerGroup() { | 
|  | 37 | static TimerGroup DwarfTimerGroup("Dwarf Exception"); | 
|  | 38 | return DwarfTimerGroup; | 
|  | 39 | } | 
|  | 40 |  | 
| Bill Wendling | bc0d23a | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 41 | DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A, | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 42 | const MCAsmInfo *T) | 
| Bill Wendling | bc0d23a | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 43 | : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false), | 
|  | 44 | shouldEmitTableModule(false), shouldEmitMovesModule(false), | 
|  | 45 | ExceptionTimer(0) { | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 46 | if (TimePassesIsEnabled) | 
| Bill Wendling | bc0d23a | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 47 | ExceptionTimer = new Timer("Dwarf Exception Writer", | 
|  | 48 | getDwarfTimerGroup()); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | DwarfException::~DwarfException() { | 
|  | 52 | delete ExceptionTimer; | 
|  | 53 | } | 
|  | 54 |  | 
| Bill Wendling | bb3e299 | 2009-09-10 00:04:48 +0000 | [diff] [blame] | 55 | /// SizeOfEncodedValue - Return the size of the encoding in bytes. | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 56 | unsigned DwarfException::SizeOfEncodedValue(unsigned Encoding) { | 
|  | 57 | if (Encoding == dwarf::DW_EH_PE_omit) | 
|  | 58 | return 0; | 
|  | 59 |  | 
|  | 60 | switch (Encoding & 0x07) { | 
|  | 61 | case dwarf::DW_EH_PE_absptr: | 
|  | 62 | return TD->getPointerSize(); | 
|  | 63 | case dwarf::DW_EH_PE_udata2: | 
|  | 64 | return 2; | 
|  | 65 | case dwarf::DW_EH_PE_udata4: | 
|  | 66 | return 4; | 
|  | 67 | case dwarf::DW_EH_PE_udata8: | 
|  | 68 | return 8; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | llvm_unreachable("Invalid encoded value."); | 
|  | 72 | return 0; | 
|  | 73 | } | 
|  | 74 |  | 
| Bill Wendling | 7ccda0f | 2009-08-25 08:08:33 +0000 | [diff] [blame] | 75 | /// EmitCIE - Emit a Common Information Entry (CIE). This holds information that | 
|  | 76 | /// is shared among many Frame Description Entries.  There is at least one CIE | 
|  | 77 | /// in every non-empty .debug_frame section. | 
|  | 78 | void DwarfException::EmitCIE(const Function *Personality, unsigned Index) { | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 79 | // Size and sign of stack growth. | 
|  | 80 | int stackGrowth = | 
|  | 81 | Asm->TM.getFrameInfo()->getStackGrowthDirection() == | 
|  | 82 | TargetFrameInfo::StackGrowsUp ? | 
|  | 83 | TD->getPointerSize() : -TD->getPointerSize(); | 
|  | 84 |  | 
|  | 85 | // Begin eh frame section. | 
| Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 86 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection()); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 87 |  | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 88 | if (MAI->is_EHSymbolPrivate()) | 
|  | 89 | O << MAI->getPrivateGlobalPrefix(); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 90 |  | 
|  | 91 | O << "EH_frame" << Index << ":\n"; | 
|  | 92 | EmitLabel("section_eh_frame", Index); | 
|  | 93 |  | 
|  | 94 | // Define base labels. | 
|  | 95 | EmitLabel("eh_frame_common", Index); | 
|  | 96 |  | 
|  | 97 | // Define the eh frame length. | 
|  | 98 | EmitDifference("eh_frame_common_end", Index, | 
|  | 99 | "eh_frame_common_begin", Index, true); | 
|  | 100 | Asm->EOL("Length of Common Information Entry"); | 
|  | 101 |  | 
|  | 102 | // EH frame header. | 
|  | 103 | EmitLabel("eh_frame_common_begin", Index); | 
|  | 104 | Asm->EmitInt32((int)0); | 
|  | 105 | Asm->EOL("CIE Identifier Tag"); | 
|  | 106 | Asm->EmitInt8(dwarf::DW_CIE_VERSION); | 
|  | 107 | Asm->EOL("CIE Version"); | 
|  | 108 |  | 
|  | 109 | // The personality presence indicates that language specific information will | 
|  | 110 | // show up in the eh frame. | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 111 |  | 
|  | 112 | // FIXME: Don't hardcode these encodings. | 
|  | 113 | unsigned PerEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; | 
|  | 114 | if (Personality && MAI->getNeedsIndirectEncoding()) | 
|  | 115 | PerEncoding |= dwarf::DW_EH_PE_indirect; | 
|  | 116 | unsigned LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; | 
|  | 117 | unsigned FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; | 
|  | 118 |  | 
|  | 119 | char Augmentation[5] = { 0 }; | 
|  | 120 | unsigned AugmentationSize = 0; | 
|  | 121 | char *APtr = Augmentation + 1; | 
|  | 122 |  | 
|  | 123 | if (Personality) { | 
|  | 124 | // There is a personality function. | 
|  | 125 | *APtr++ = 'P'; | 
|  | 126 | AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | if (UsesLSDA[Index]) { | 
|  | 130 | // An LSDA pointer is in the FDE augmentation. | 
|  | 131 | *APtr++ = 'L'; | 
|  | 132 | ++AugmentationSize; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | if (FDEEncoding != dwarf::DW_EH_PE_absptr) { | 
|  | 136 | // A non-default pointer encoding for the FDE. | 
|  | 137 | *APtr++ = 'R'; | 
|  | 138 | ++AugmentationSize; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | if (APtr != Augmentation + 1) | 
|  | 142 | Augmentation[0] = 'z'; | 
|  | 143 |  | 
|  | 144 | Asm->EmitString(Augmentation); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 145 | Asm->EOL("CIE Augmentation"); | 
|  | 146 |  | 
|  | 147 | // Round out reader. | 
|  | 148 | Asm->EmitULEB128Bytes(1); | 
|  | 149 | Asm->EOL("CIE Code Alignment Factor"); | 
|  | 150 | Asm->EmitSLEB128Bytes(stackGrowth); | 
|  | 151 | Asm->EOL("CIE Data Alignment Factor"); | 
|  | 152 | Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true)); | 
|  | 153 | Asm->EOL("CIE Return Address Column"); | 
|  | 154 |  | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 155 | Asm->EmitULEB128Bytes(AugmentationSize); | 
|  | 156 | Asm->EOL("Augmentation Size"); | 
|  | 157 |  | 
|  | 158 | Asm->EmitInt8(PerEncoding); | 
|  | 159 | Asm->EOL("Personality", PerEncoding); | 
|  | 160 |  | 
| Bill Wendling | 4bda11f | 2009-08-25 02:32:05 +0000 | [diff] [blame] | 161 | // If there is a personality, we need to indicate the function's location. | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 162 | if (Personality) { | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 163 | PrintRelDirective(true); | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 164 | O << MAI->getPersonalityPrefix(); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 165 | Asm->EmitExternalGlobal((const GlobalVariable *)(Personality)); | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 166 | O << MAI->getPersonalitySuffix(); | 
|  | 167 | if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL")) | 
|  | 168 | O << "-" << MAI->getPCSymbol(); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 169 | Asm->EOL("Personality"); | 
|  | 170 |  | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 171 | Asm->EmitInt8(LSDAEncoding); | 
|  | 172 | Asm->EOL("LSDA Encoding", LSDAEncoding); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 173 |  | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 174 | Asm->EmitInt8(FDEEncoding); | 
|  | 175 | Asm->EOL("FDE Encoding", FDEEncoding); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
|  | 178 | // Indicate locations of general callee saved registers in frame. | 
|  | 179 | std::vector<MachineMove> Moves; | 
|  | 180 | RI->getInitialFrameState(Moves); | 
|  | 181 | EmitFrameMoves(NULL, 0, Moves, true); | 
|  | 182 |  | 
|  | 183 | // On Darwin the linker honors the alignment of eh_frame, which means it must | 
|  | 184 | // be 8-byte on 64-bit targets to match what gcc does.  Otherwise you get | 
|  | 185 | // holes which confuse readers of eh_frame. | 
|  | 186 | Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, | 
|  | 187 | 0, 0, false); | 
|  | 188 | EmitLabel("eh_frame_common_end", Index); | 
|  | 189 |  | 
|  | 190 | Asm->EOL(); | 
|  | 191 | } | 
|  | 192 |  | 
| Bill Wendling | 7ccda0f | 2009-08-25 08:08:33 +0000 | [diff] [blame] | 193 | /// EmitFDE - Emit the Frame Description Entry (FDE) for the function. | 
|  | 194 | void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) { | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 195 | assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() && | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 196 | "Should not emit 'available externally' functions at all"); | 
|  | 197 |  | 
| Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 198 | const Function *TheFunc = EHFrameInfo.function; | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 199 |  | 
| Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 200 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection()); | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 201 |  | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 202 | // Externally visible entry into the functions eh frame info. If the | 
|  | 203 | // corresponding function is static, this should not be externally visible. | 
| Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 204 | if (!TheFunc->hasLocalLinkage()) | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 205 | if (const char *GlobalEHDirective = MAI->getGlobalEHDirective()) | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 206 | O << GlobalEHDirective << EHFrameInfo.FnName << "\n"; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 207 |  | 
|  | 208 | // If corresponding function is weak definition, this should be too. | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 209 | if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective()) | 
|  | 210 | O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n"; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 211 |  | 
|  | 212 | // If there are no calls then you can't unwind.  This may mean we can omit the | 
|  | 213 | // EH Frame, but some environments do not handle weak absolute symbols. If | 
|  | 214 | // UnwindTablesMandatory is set we cannot do this optimization; the unwind | 
|  | 215 | // info is to be available for non-EH uses. | 
| Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 216 | if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory && | 
|  | 217 | (!TheFunc->isWeakForLinker() || | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 218 | !MAI->getWeakDefDirective() || | 
|  | 219 | MAI->getSupportsWeakOmittedEHFrame())) { | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 220 | O << EHFrameInfo.FnName << " = 0\n"; | 
|  | 221 | // This name has no connection to the function, so it might get | 
|  | 222 | // dead-stripped when the function is not, erroneously.  Prohibit | 
|  | 223 | // dead-stripping unconditionally. | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 224 | if (const char *UsedDirective = MAI->getUsedDirective()) | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 225 | O << UsedDirective << EHFrameInfo.FnName << "\n\n"; | 
|  | 226 | } else { | 
|  | 227 | O << EHFrameInfo.FnName << ":\n"; | 
|  | 228 |  | 
|  | 229 | // EH frame header. | 
|  | 230 | EmitDifference("eh_frame_end", EHFrameInfo.Number, | 
|  | 231 | "eh_frame_begin", EHFrameInfo.Number, true); | 
|  | 232 | Asm->EOL("Length of Frame Information Entry"); | 
|  | 233 |  | 
|  | 234 | EmitLabel("eh_frame_begin", EHFrameInfo.Number); | 
|  | 235 |  | 
| Chris Lattner | a4ff5e4 | 2009-07-17 20:53:51 +0000 | [diff] [blame] | 236 | EmitSectionOffset("eh_frame_begin", "eh_frame_common", | 
|  | 237 | EHFrameInfo.Number, EHFrameInfo.PersonalityIndex, | 
|  | 238 | true, true, false); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 239 |  | 
|  | 240 | Asm->EOL("FDE CIE offset"); | 
|  | 241 |  | 
| Duncan Sands | c69d74a | 2009-08-31 16:45:16 +0000 | [diff] [blame] | 242 | EmitReference("eh_func_begin", EHFrameInfo.Number, true, true); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 243 | Asm->EOL("FDE initial location"); | 
|  | 244 | EmitDifference("eh_func_end", EHFrameInfo.Number, | 
| Duncan Sands | c69d74a | 2009-08-31 16:45:16 +0000 | [diff] [blame] | 245 | "eh_func_begin", EHFrameInfo.Number, true); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 246 | Asm->EOL("FDE address range"); | 
|  | 247 |  | 
|  | 248 | // If there is a personality and landing pads then point to the language | 
|  | 249 | // specific data area in the exception table. | 
| Eric Christopher | d44fff7 | 2009-08-26 21:30:49 +0000 | [diff] [blame] | 250 | if (MMI->getPersonalities()[0] != NULL) { | 
| Duncan Sands | c69d74a | 2009-08-31 16:45:16 +0000 | [diff] [blame] | 251 | bool is4Byte = TD->getPointerSize() == sizeof(int32_t); | 
|  | 252 |  | 
| Eric Christopher | 6fefceb | 2009-08-29 01:12:46 +0000 | [diff] [blame] | 253 | Asm->EmitULEB128Bytes(is4Byte ? 4 : 8); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 254 | Asm->EOL("Augmentation size"); | 
|  | 255 |  | 
| Duncan Sands | c69d74a | 2009-08-31 16:45:16 +0000 | [diff] [blame] | 256 | if (EHFrameInfo.hasLandingPads) | 
| Eric Christopher | 6fefceb | 2009-08-29 01:12:46 +0000 | [diff] [blame] | 257 | EmitReference("exception", EHFrameInfo.Number, true, false); | 
| Duncan Sands | c69d74a | 2009-08-31 16:45:16 +0000 | [diff] [blame] | 258 | else { | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 259 | if (is4Byte) | 
|  | 260 | Asm->EmitInt32((int)0); | 
|  | 261 | else | 
|  | 262 | Asm->EmitInt64((int)0); | 
| Eric Christopher | 6fefceb | 2009-08-29 01:12:46 +0000 | [diff] [blame] | 263 | } | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 264 | Asm->EOL("Language Specific Data Area"); | 
|  | 265 | } else { | 
|  | 266 | Asm->EmitULEB128Bytes(0); | 
|  | 267 | Asm->EOL("Augmentation size"); | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | // Indicate locations of function specific callee saved registers in frame. | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 271 | EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 272 | true); | 
|  | 273 |  | 
|  | 274 | // On Darwin the linker honors the alignment of eh_frame, which means it | 
|  | 275 | // must be 8-byte on 64-bit targets to match what gcc does.  Otherwise you | 
|  | 276 | // get holes which confuse readers of eh_frame. | 
|  | 277 | Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, | 
|  | 278 | 0, 0, false); | 
|  | 279 | EmitLabel("eh_frame_end", EHFrameInfo.Number); | 
|  | 280 |  | 
|  | 281 | // If the function is marked used, this table should be also.  We cannot | 
|  | 282 | // make the mark unconditional in this case, since retaining the table also | 
|  | 283 | // retains the function in this case, and there is code around that depends | 
|  | 284 | // on unused functions (calling undefined externals) being dead-stripped to | 
|  | 285 | // link correctly.  Yes, there really is. | 
| Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 286 | if (MMI->isUsedFunction(EHFrameInfo.function)) | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 287 | if (const char *UsedDirective = MAI->getUsedDirective()) | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 288 | O << UsedDirective << EHFrameInfo.FnName << "\n\n"; | 
|  | 289 | } | 
| Bill Wendling | 4bda11f | 2009-08-25 02:32:05 +0000 | [diff] [blame] | 290 |  | 
|  | 291 | Asm->EOL(); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 292 | } | 
|  | 293 |  | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 294 | /// SharedTypeIds - How many leading type ids two landing pads have in common. | 
|  | 295 | unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L, | 
|  | 296 | const LandingPadInfo *R) { | 
|  | 297 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; | 
|  | 298 | unsigned LSize = LIds.size(), RSize = RIds.size(); | 
|  | 299 | unsigned MinSize = LSize < RSize ? LSize : RSize; | 
|  | 300 | unsigned Count = 0; | 
|  | 301 |  | 
|  | 302 | for (; Count != MinSize; ++Count) | 
|  | 303 | if (LIds[Count] != RIds[Count]) | 
|  | 304 | return Count; | 
|  | 305 |  | 
|  | 306 | return Count; | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | /// PadLT - Order landing pads lexicographically by type id. | 
|  | 310 | bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { | 
|  | 311 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; | 
|  | 312 | unsigned LSize = LIds.size(), RSize = RIds.size(); | 
|  | 313 | unsigned MinSize = LSize < RSize ? LSize : RSize; | 
|  | 314 |  | 
|  | 315 | for (unsigned i = 0; i != MinSize; ++i) | 
|  | 316 | if (LIds[i] != RIds[i]) | 
|  | 317 | return LIds[i] < RIds[i]; | 
|  | 318 |  | 
|  | 319 | return LSize < RSize; | 
|  | 320 | } | 
|  | 321 |  | 
| Bill Wendling | d460962 | 2009-07-28 23:23:00 +0000 | [diff] [blame] | 322 | /// ComputeActionsTable - Compute the actions table and gather the first action | 
|  | 323 | /// index for each landing pad site. | 
| Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 324 | unsigned DwarfException:: | 
|  | 325 | ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads, | 
|  | 326 | SmallVectorImpl<ActionEntry> &Actions, | 
|  | 327 | SmallVectorImpl<unsigned> &FirstActions) { | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 328 |  | 
|  | 329 | // The action table follows the call-site table in the LSDA. The individual | 
|  | 330 | // records are of two types: | 
|  | 331 | // | 
|  | 332 | //   * Catch clause | 
|  | 333 | //   * Exception specification | 
|  | 334 | // | 
|  | 335 | // The two record kinds have the same format, with only small differences. | 
|  | 336 | // They are distinguished by the "switch value" field: Catch clauses | 
|  | 337 | // (TypeInfos) have strictly positive switch values, and exception | 
|  | 338 | // specifications (FilterIds) have strictly negative switch values. Value 0 | 
|  | 339 | // indicates a catch-all clause. | 
|  | 340 | // | 
| Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 341 | // Negative type IDs index into FilterIds. Positive type IDs index into | 
|  | 342 | // TypeInfos.  The value written for a positive type ID is just the type ID | 
|  | 343 | // itself.  For a negative type ID, however, the value written is the | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 344 | // (negative) byte offset of the corresponding FilterIds entry.  The byte | 
| Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 345 | // offset is usually equal to the type ID (because the FilterIds entries are | 
|  | 346 | // written using a variable width encoding, which outputs one byte per entry | 
|  | 347 | // as long as the value written is not too large) but can differ.  This kind | 
|  | 348 | // of complication does not occur for positive type IDs because type infos are | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 349 | // output using a fixed width encoding.  FilterOffsets[i] holds the byte | 
|  | 350 | // offset corresponding to FilterIds[i]. | 
| Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 351 |  | 
|  | 352 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 353 | SmallVector<int, 16> FilterOffsets; | 
|  | 354 | FilterOffsets.reserve(FilterIds.size()); | 
|  | 355 | int Offset = -1; | 
| Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 356 |  | 
|  | 357 | for (std::vector<unsigned>::const_iterator | 
|  | 358 | I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 359 | FilterOffsets.push_back(Offset); | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 360 | Offset -= MCAsmInfo::getULEB128Size(*I); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 363 | FirstActions.reserve(LandingPads.size()); | 
|  | 364 |  | 
|  | 365 | int FirstAction = 0; | 
|  | 366 | unsigned SizeActions = 0; | 
| Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 367 | const LandingPadInfo *PrevLPI = 0; | 
| Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 368 |  | 
| Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 369 | for (SmallVectorImpl<const LandingPadInfo *>::const_iterator | 
| Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 370 | I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { | 
|  | 371 | const LandingPadInfo *LPI = *I; | 
|  | 372 | const std::vector<int> &TypeIds = LPI->TypeIds; | 
|  | 373 | const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 374 | unsigned SizeSiteActions = 0; | 
|  | 375 |  | 
|  | 376 | if (NumShared < TypeIds.size()) { | 
|  | 377 | unsigned SizeAction = 0; | 
|  | 378 | ActionEntry *PrevAction = 0; | 
|  | 379 |  | 
|  | 380 | if (NumShared) { | 
| Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 381 | const unsigned SizePrevIds = PrevLPI->TypeIds.size(); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 382 | assert(Actions.size()); | 
|  | 383 | PrevAction = &Actions.back(); | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 384 | SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) + | 
|  | 385 | MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 386 |  | 
|  | 387 | for (unsigned j = NumShared; j != SizePrevIds; ++j) { | 
|  | 388 | SizeAction -= | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 389 | MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 390 | SizeAction += -PrevAction->NextAction; | 
|  | 391 | PrevAction = PrevAction->Previous; | 
|  | 392 | } | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | // Compute the actions. | 
| Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 396 | for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { | 
|  | 397 | int TypeID = TypeIds[J]; | 
|  | 398 | assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 399 | int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 400 | unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 401 |  | 
|  | 402 | int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 403 | SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 404 | SizeSiteActions += SizeAction; | 
|  | 405 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 406 | ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 407 | Actions.push_back(Action); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 408 | PrevAction = &Actions.back(); | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | // Record the first action of the landing pad site. | 
|  | 412 | FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; | 
|  | 413 | } // else identical - re-use previous FirstAction | 
|  | 414 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 415 | // Information used when created the call-site table. The action record | 
|  | 416 | // field of the call site record is the offset of the first associated | 
|  | 417 | // action record, relative to the start of the actions table. This value is | 
|  | 418 | // biased by 1 (1 in dicating the start of the actions table), and 0 | 
|  | 419 | // indicates that there are no actions. | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 420 | FirstActions.push_back(FirstAction); | 
|  | 421 |  | 
|  | 422 | // Compute this sites contribution to size. | 
|  | 423 | SizeActions += SizeSiteActions; | 
| Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 424 |  | 
|  | 425 | PrevLPI = LPI; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 426 | } | 
|  | 427 |  | 
| Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 428 | return SizeActions; | 
|  | 429 | } | 
|  | 430 |  | 
| Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 431 | /// ComputeCallSiteTable - Compute the call-site table.  The entry for an invoke | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 432 | /// has a try-range containing the call, a non-zero landing pad, and an | 
| Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 433 | /// appropriate action.  The entry for an ordinary call has a try-range | 
|  | 434 | /// containing the call and zero for the landing pad and the action.  Calls | 
|  | 435 | /// marked 'nounwind' have no entry and must not be contained in the try-range | 
|  | 436 | /// of any entry - they form gaps in the table.  Entries must be ordered by | 
|  | 437 | /// try-range address. | 
|  | 438 | void DwarfException:: | 
|  | 439 | ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, | 
|  | 440 | const RangeMapType &PadMap, | 
|  | 441 | const SmallVectorImpl<const LandingPadInfo *> &LandingPads, | 
|  | 442 | const SmallVectorImpl<unsigned> &FirstActions) { | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 443 | // The end label of the previous invoke or nounwind try-range. | 
|  | 444 | unsigned LastLabel = 0; | 
|  | 445 |  | 
|  | 446 | // Whether there is a potentially throwing instruction (currently this means | 
|  | 447 | // an ordinary call) between the end of the previous try-range and now. | 
|  | 448 | bool SawPotentiallyThrowing = false; | 
|  | 449 |  | 
| Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 450 | // Whether the last CallSite entry was for an invoke. | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 451 | bool PreviousIsInvoke = false; | 
|  | 452 |  | 
|  | 453 | // Visit all instructions in order of address. | 
|  | 454 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); | 
|  | 455 | I != E; ++I) { | 
|  | 456 | for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); | 
|  | 457 | MI != E; ++MI) { | 
|  | 458 | if (!MI->isLabel()) { | 
|  | 459 | SawPotentiallyThrowing |= MI->getDesc().isCall(); | 
|  | 460 | continue; | 
|  | 461 | } | 
|  | 462 |  | 
|  | 463 | unsigned BeginLabel = MI->getOperand(0).getImm(); | 
|  | 464 | assert(BeginLabel && "Invalid label!"); | 
|  | 465 |  | 
|  | 466 | // End of the previous try-range? | 
|  | 467 | if (BeginLabel == LastLabel) | 
|  | 468 | SawPotentiallyThrowing = false; | 
|  | 469 |  | 
|  | 470 | // Beginning of a new try-range? | 
|  | 471 | RangeMapType::iterator L = PadMap.find(BeginLabel); | 
|  | 472 | if (L == PadMap.end()) | 
|  | 473 | // Nope, it was just some random label. | 
|  | 474 | continue; | 
|  | 475 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 476 | const PadRange &P = L->second; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 477 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 478 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && | 
|  | 479 | "Inconsistent landing pad map!"); | 
|  | 480 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 481 | // For Dwarf exception handling (SjLj handling doesn't use this). If some | 
|  | 482 | // instruction between the previous try-range and this one may throw, | 
|  | 483 | // create a call-site entry with no landing pad for the region between the | 
|  | 484 | // try-ranges. | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 485 | if (SawPotentiallyThrowing && | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 486 | MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 487 | CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 }; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 488 | CallSites.push_back(Site); | 
|  | 489 | PreviousIsInvoke = false; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; | 
|  | 493 | assert(BeginLabel && LastLabel && "Invalid landing pad!"); | 
|  | 494 |  | 
|  | 495 | if (LandingPad->LandingPadLabel) { | 
|  | 496 | // This try-range is for an invoke. | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 497 | CallSiteEntry Site = { | 
|  | 498 | BeginLabel, | 
|  | 499 | LastLabel, | 
|  | 500 | LandingPad->LandingPadLabel, | 
|  | 501 | FirstActions[P.PadIndex] | 
|  | 502 | }; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 503 |  | 
| Jim Grosbach | 33668c0 | 2009-09-01 17:19:13 +0000 | [diff] [blame] | 504 | // Try to merge with the previous call-site. SJLJ doesn't do this | 
|  | 505 | if (PreviousIsInvoke && | 
|  | 506 | MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 507 | CallSiteEntry &Prev = CallSites.back(); | 
|  | 508 | if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { | 
|  | 509 | // Extend the range of the previous entry. | 
|  | 510 | Prev.EndLabel = Site.EndLabel; | 
|  | 511 | continue; | 
|  | 512 | } | 
|  | 513 | } | 
|  | 514 |  | 
|  | 515 | // Otherwise, create a new call-site. | 
|  | 516 | CallSites.push_back(Site); | 
|  | 517 | PreviousIsInvoke = true; | 
|  | 518 | } else { | 
|  | 519 | // Create a gap. | 
|  | 520 | PreviousIsInvoke = false; | 
|  | 521 | } | 
|  | 522 | } | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | // If some instruction between the previous try-range and the end of the | 
|  | 526 | // function may throw, create a call-site entry with no landing pad for the | 
|  | 527 | // region following the try-range. | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 528 | if (SawPotentiallyThrowing && | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 529 | MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 530 | CallSiteEntry Site = { LastLabel, 0, 0, 0 }; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 531 | CallSites.push_back(Site); | 
|  | 532 | } | 
| Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 533 | } | 
|  | 534 |  | 
| Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 535 | /// EmitExceptionTable - Emit landing pads and actions. | 
|  | 536 | /// | 
|  | 537 | /// The general organization of the table is complex, but the basic concepts are | 
|  | 538 | /// easy.  First there is a header which describes the location and organization | 
|  | 539 | /// of the three components that follow. | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 540 | /// | 
| Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 541 | ///  1. The landing pad site information describes the range of code covered by | 
|  | 542 | ///     the try.  In our case it's an accumulation of the ranges covered by the | 
|  | 543 | ///     invokes in the try.  There is also a reference to the landing pad that | 
|  | 544 | ///     handles the exception once processed.  Finally an index into the actions | 
|  | 545 | ///     table. | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 546 | ///  2. The action table, in our case, is composed of pairs of type IDs and next | 
| Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 547 | ///     action offset.  Starting with the action index from the landing pad | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 548 | ///     site, each type ID is checked for a match to the current exception.  If | 
| Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 549 | ///     it matches then the exception and type id are passed on to the landing | 
|  | 550 | ///     pad.  Otherwise the next action is looked up.  This chain is terminated | 
|  | 551 | ///     with a next action of zero.  If no type id is found the the frame is | 
|  | 552 | ///     unwound and handling continues. | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 553 | ///  3. Type ID table contains references to all the C++ typeinfo for all | 
| Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 554 | ///     catches in the function.  This tables is reversed indexed base 1. | 
| Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 555 | void DwarfException::EmitExceptionTable() { | 
|  | 556 | const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); | 
|  | 557 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); | 
|  | 558 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); | 
|  | 559 | if (PadInfos.empty()) return; | 
|  | 560 |  | 
|  | 561 | // Sort the landing pads in order of their type ids.  This is used to fold | 
|  | 562 | // duplicate actions. | 
|  | 563 | SmallVector<const LandingPadInfo *, 64> LandingPads; | 
|  | 564 | LandingPads.reserve(PadInfos.size()); | 
|  | 565 |  | 
|  | 566 | for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) | 
|  | 567 | LandingPads.push_back(&PadInfos[i]); | 
|  | 568 |  | 
|  | 569 | std::sort(LandingPads.begin(), LandingPads.end(), PadLT); | 
|  | 570 |  | 
|  | 571 | // Compute the actions table and gather the first action index for each | 
|  | 572 | // landing pad site. | 
|  | 573 | SmallVector<ActionEntry, 32> Actions; | 
|  | 574 | SmallVector<unsigned, 64> FirstActions; | 
|  | 575 | unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions); | 
|  | 576 |  | 
|  | 577 | // Invokes and nounwind calls have entries in PadMap (due to being bracketed | 
|  | 578 | // by try-range labels when lowered).  Ordinary calls do not, so appropriate | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 579 | // try-ranges for them need be deduced when using Dwarf exception handling. | 
| Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 580 | RangeMapType PadMap; | 
|  | 581 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { | 
|  | 582 | const LandingPadInfo *LandingPad = LandingPads[i]; | 
|  | 583 | for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { | 
|  | 584 | unsigned BeginLabel = LandingPad->BeginLabels[j]; | 
|  | 585 | assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); | 
|  | 586 | PadRange P = { i, j }; | 
|  | 587 | PadMap[BeginLabel] = P; | 
|  | 588 | } | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | // Compute the call-site table. | 
|  | 592 | SmallVector<CallSiteEntry, 64> CallSites; | 
| Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 593 | ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 594 |  | 
|  | 595 | // Final tallies. | 
|  | 596 |  | 
|  | 597 | // Call sites. | 
| Bill Wendling | 40121bc | 2009-09-10 00:13:16 +0000 | [diff] [blame] | 598 | const unsigned SiteStartSize  = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); | 
|  | 599 | const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); | 
|  | 600 | const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 601 | unsigned SizeSites; | 
| Jim Grosbach | bff3923 | 2009-08-12 17:38:44 +0000 | [diff] [blame] | 602 |  | 
| Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 603 | bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; | 
| Jim Grosbach | bff3923 | 2009-08-12 17:38:44 +0000 | [diff] [blame] | 604 |  | 
| Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 605 | bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true; | 
|  | 606 |  | 
|  | 607 | if (IsSJLJ) | 
| Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 608 | SizeSites = 0; | 
| Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 609 | else | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 610 | SizeSites = CallSites.size() * | 
|  | 611 | (SiteStartSize + SiteLengthSize + LandingPadSize); | 
| Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 612 |  | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 613 | for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 614 | SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action); | 
| Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 615 | if (IsSJLJ) | 
| Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 616 | SizeSites += MCAsmInfo::getULEB128Size(i); | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 617 | } | 
| Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 618 |  | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 619 | // Type infos. | 
| Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 620 | const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); | 
| Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 621 | unsigned TTypeFormat; | 
| Bill Wendling | a2f6449 | 2009-09-10 06:27:16 +0000 | [diff] [blame^] | 622 | unsigned TypeFormatSize; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 623 |  | 
| Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 624 | if (!HaveTTData) { | 
| Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 625 | TTypeFormat = dwarf::DW_EH_PE_omit; | 
| Bill Wendling | a2f6449 | 2009-09-10 06:27:16 +0000 | [diff] [blame^] | 626 | TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr); | 
| Chris Lattner | 81c9a06 | 2009-07-31 22:03:47 +0000 | [diff] [blame] | 627 | } else { | 
| Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 628 | // Okay, we have actual filters or typeinfos to emit.  As such, we need to | 
|  | 629 | // pick a type encoding for them.  We're about to emit a list of pointers to | 
|  | 630 | // typeinfo objects at the end of the LSDA.  However, unless we're in static | 
|  | 631 | // mode, this reference will require a relocation by the dynamic linker. | 
| Chris Lattner | 46b754c | 2009-07-31 22:18:14 +0000 | [diff] [blame] | 632 | // | 
| Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 633 | // Because of this, we have a couple of options: | 
|  | 634 | //   1) If we are in -static mode, we can always use an absolute reference | 
|  | 635 | //      from the LSDA, because the static linker will resolve it. | 
|  | 636 | //   2) Otherwise, if the LSDA section is writable, we can output the direct | 
|  | 637 | //      reference to the typeinfo and allow the dynamic linker to relocate | 
|  | 638 | //      it.  Since it is in a writable section, the dynamic linker won't | 
|  | 639 | //      have a problem. | 
|  | 640 | //   3) Finally, if we're in PIC mode and the LDSA section isn't writable, | 
|  | 641 | //      we need to use some form of indirection.  For example, on Darwin, | 
|  | 642 | //      we can output a statically-relocatable reference to a dyld stub. The | 
|  | 643 | //      offset to the stub is constant, but the contents are in a section | 
|  | 644 | //      that is updated by the dynamic linker.  This is easy enough, but we | 
|  | 645 | //      need to tell the personality function of the unwinder to indirect | 
|  | 646 | //      through the dyld stub. | 
|  | 647 | // | 
| Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 648 | // FIXME: When (3) is actually implemented, we'll have to emit the stubs | 
| Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 649 | // somewhere.  This predicate should be moved to a shared location that is | 
|  | 650 | // in target-independent code. | 
|  | 651 | // | 
| Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 652 | if (LSDASection->getKind().isWriteable() || | 
|  | 653 | Asm->TM.getRelocationModel() == Reloc::Static) | 
|  | 654 | TTypeFormat = dwarf::DW_EH_PE_absptr; | 
|  | 655 | else | 
|  | 656 | TTypeFormat = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | | 
|  | 657 | dwarf::DW_EH_PE_sdata4; | 
| Bill Wendling | a2f6449 | 2009-09-10 06:27:16 +0000 | [diff] [blame^] | 658 |  | 
|  | 659 | TypeFormatSize = SizeOfEncodedValue(TTypeFormat); | 
| Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 660 | } | 
| Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 661 |  | 
| Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 662 | // Begin the exception table. | 
|  | 663 | Asm->OutStreamer.SwitchSection(LSDASection); | 
|  | 664 | Asm->EmitAlignment(2, 0, 0, false); | 
| Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 665 |  | 
| Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 666 | O << "GCC_except_table" << SubprogramCount << ":\n"; | 
| Bill Wendling | a2f6449 | 2009-09-10 06:27:16 +0000 | [diff] [blame^] | 667 |  | 
|  | 668 | // The type infos need to be aligned. GCC does this by inserting padding just | 
|  | 669 | // before the type infos. However, this changes the size of the exception | 
|  | 670 | // table, so you need to take this into account when you output the exception | 
|  | 671 | // table size. However, the size is output using a variable length encoding. | 
|  | 672 | // So by increasing the size by inserting padding, you may increase the number | 
|  | 673 | // of bytes used for writing the size. If it increases, say by one byte, then | 
|  | 674 | // you now need to output one less byte of padding to get the type infos | 
|  | 675 | // aligned.  However this decreases the size of the exception table. This | 
|  | 676 | // changes the value you have to output for the exception table size. Due to | 
|  | 677 | // the variable length encoding, the number of bytes used for writing the | 
|  | 678 | // length may decrease. If so, you then have to increase the amount of | 
|  | 679 | // padding. And so on. If you look carefully at the GCC code you will see that | 
|  | 680 | // it indeed does this in a loop, going on and on until the values stabilize. | 
|  | 681 | // We chose another solution: don't output padding inside the table like GCC | 
|  | 682 | // does, instead output it before the table. | 
|  | 683 | unsigned SizeTypes = TypeInfos.size() * TypeFormatSize; | 
|  | 684 | unsigned TyOffset = sizeof(int8_t) +          // Call site format | 
|  | 685 | MCAsmInfo::getULEB128Size(SizeSites) +      // Call-site table length | 
|  | 686 | SizeSites + SizeActions + SizeTypes; | 
|  | 687 | unsigned TotalSize = sizeof(int8_t) +         // LPStart format | 
|  | 688 | sizeof(int8_t) +         // TType format | 
|  | 689 | (HaveTTData ? | 
|  | 690 | MCAsmInfo::getULEB128Size(TyOffset) : 0) + // TType base offset | 
|  | 691 | TyOffset; | 
|  | 692 | unsigned SizeAlign = (4 - TotalSize) & 3; | 
|  | 693 |  | 
|  | 694 | for (unsigned i = 0; i != SizeAlign; ++i) { | 
|  | 695 | Asm->EmitInt8(0); | 
|  | 696 | Asm->EOL("Padding"); | 
|  | 697 | } | 
|  | 698 |  | 
| Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 699 | EmitLabel("exception", SubprogramCount); | 
|  | 700 |  | 
|  | 701 | if (IsSJLJ) { | 
|  | 702 | SmallString<16> LSDAName; | 
|  | 703 | raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() << | 
|  | 704 | "_LSDA_" << Asm->getFunctionNumber(); | 
|  | 705 | O << LSDAName.str() << ":\n"; | 
|  | 706 | } | 
|  | 707 |  | 
|  | 708 | // Emit the header. | 
|  | 709 | Asm->EmitInt8(dwarf::DW_EH_PE_omit); | 
|  | 710 | Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit); | 
|  | 711 |  | 
|  | 712 | // For SjLj exceptions, if there is no TypeInfo, then we just explicitly | 
|  | 713 | // say that we're omitting that bit. | 
|  | 714 | Asm->EmitInt8(TTypeFormat); | 
|  | 715 | Asm->EOL("@TType format", TTypeFormat); | 
|  | 716 |  | 
|  | 717 | if (HaveTTData) { | 
| Bill Wendling | a2f6449 | 2009-09-10 06:27:16 +0000 | [diff] [blame^] | 718 | Asm->EmitULEB128Bytes(TyOffset); | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 719 | Asm->EOL("@TType base offset"); | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 720 | } | 
| Bill Wendling | b0d9c3e | 2009-07-28 22:23:45 +0000 | [diff] [blame] | 721 |  | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 722 | // SjLj Exception handilng | 
| Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 723 | if (IsSJLJ) { | 
| Bill Wendling | 639217c | 2009-08-27 03:32:50 +0000 | [diff] [blame] | 724 | Asm->EmitInt8(dwarf::DW_EH_PE_udata4); | 
| Bill Wendling | 0734d35 | 2009-09-09 21:26:19 +0000 | [diff] [blame] | 725 | Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4); | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 726 | Asm->EmitULEB128Bytes(SizeSites); | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 727 | Asm->EOL("Call site table length"); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 728 |  | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 729 | // Emit the landing pad site information. | 
| Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 730 | unsigned idx = 0; | 
|  | 731 | for (SmallVectorImpl<CallSiteEntry>::const_iterator | 
|  | 732 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { | 
|  | 733 | const CallSiteEntry &S = *I; | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 734 |  | 
|  | 735 | // Offset of the landing pad, counted in 16-byte bundles relative to the | 
|  | 736 | // @LPStart address. | 
| Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 737 | Asm->EmitULEB128Bytes(idx); | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 738 | Asm->EOL("Landing pad"); | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 739 |  | 
|  | 740 | // Offset of the first associated action record, relative to the start of | 
|  | 741 | // the action table. This value is biased by 1 (1 indicates the start of | 
|  | 742 | // the action table), and 0 indicates that there are no actions. | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 743 | Asm->EmitULEB128Bytes(S.Action); | 
|  | 744 | Asm->EOL("Action"); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 745 | } | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 746 | } else { | 
|  | 747 | // DWARF Exception handling | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 748 | assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 749 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 750 | // The call-site table is a list of all call sites that may throw an | 
|  | 751 | // exception (including C++ 'throw' statements) in the procedure | 
|  | 752 | // fragment. It immediately follows the LSDA header. Each entry indicates, | 
|  | 753 | // for a given call, the first corresponding action record and corresponding | 
|  | 754 | // landing pad. | 
|  | 755 | // | 
|  | 756 | // The table begins with the number of bytes, stored as an LEB128 | 
|  | 757 | // compressed, unsigned integer. The records immediately follow the record | 
|  | 758 | // count. They are sorted in increasing call-site address. Each record | 
|  | 759 | // indicates: | 
|  | 760 | // | 
|  | 761 | //   * The position of the call-site. | 
|  | 762 | //   * The position of the landing pad. | 
|  | 763 | //   * The first action record for that call site. | 
|  | 764 | // | 
|  | 765 | // A missing entry in the call-site table indicates that a call is not | 
|  | 766 | // supposed to throw. Such calls include: | 
|  | 767 | // | 
|  | 768 | //   * Calls to destructors within cleanup code. C++ semantics forbids these | 
|  | 769 | //     calls to throw. | 
|  | 770 | //   * Calls to intrinsic routines in the standard library which are known | 
|  | 771 | //     not to throw (sin, memcpy, et al). | 
|  | 772 | // | 
|  | 773 | // If the runtime does not find the call-site entry for a given call, it | 
|  | 774 | // will call `terminate()'. | 
|  | 775 |  | 
|  | 776 | // Emit the landing pad call site table. | 
| Bill Wendling | 639217c | 2009-08-27 03:32:50 +0000 | [diff] [blame] | 777 | Asm->EmitInt8(dwarf::DW_EH_PE_udata4); | 
| Bill Wendling | 0734d35 | 2009-09-09 21:26:19 +0000 | [diff] [blame] | 778 | Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4); | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 779 | Asm->EmitULEB128Bytes(SizeSites); | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 780 | Asm->EOL("Call site table size"); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 781 |  | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 782 | for (SmallVectorImpl<CallSiteEntry>::const_iterator | 
|  | 783 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { | 
|  | 784 | const CallSiteEntry &S = *I; | 
|  | 785 | const char *BeginTag; | 
|  | 786 | unsigned BeginNumber; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 787 |  | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 788 | if (!S.BeginLabel) { | 
|  | 789 | BeginTag = "eh_func_begin"; | 
|  | 790 | BeginNumber = SubprogramCount; | 
|  | 791 | } else { | 
|  | 792 | BeginTag = "label"; | 
|  | 793 | BeginNumber = S.BeginLabel; | 
|  | 794 | } | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 795 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 796 | // Offset of the call site relative to the previous call site, counted in | 
|  | 797 | // number of 16-byte bundles. The first call site is counted relative to | 
|  | 798 | // the start of the procedure fragment. | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 799 | EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount, | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 800 | true, true); | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 801 | Asm->EOL("Region start"); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 802 |  | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 803 | if (!S.EndLabel) | 
|  | 804 | EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber, | 
|  | 805 | true); | 
|  | 806 | else | 
|  | 807 | EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 808 |  | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 809 | Asm->EOL("Region length"); | 
|  | 810 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 811 | // Offset of the landing pad, counted in 16-byte bundles relative to the | 
|  | 812 | // @LPStart address. | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 813 | if (!S.PadLabel) | 
|  | 814 | Asm->EmitInt32(0); | 
|  | 815 | else | 
|  | 816 | EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount, | 
|  | 817 | true, true); | 
|  | 818 |  | 
|  | 819 | Asm->EOL("Landing pad"); | 
|  | 820 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 821 | // Offset of the first associated action record, relative to the start of | 
|  | 822 | // the action table. This value is biased by 1 (1 indicates the start of | 
|  | 823 | // the action table), and 0 indicates that there are no actions. | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 824 | Asm->EmitULEB128Bytes(S.Action); | 
|  | 825 | Asm->EOL("Action"); | 
|  | 826 | } | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 827 | } | 
|  | 828 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 829 | // Emit the Action Table. | 
| Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 830 | for (SmallVectorImpl<ActionEntry>::const_iterator | 
|  | 831 | I = Actions.begin(), E = Actions.end(); I != E; ++I) { | 
|  | 832 | const ActionEntry &Action = *I; | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 833 |  | 
|  | 834 | // Type Filter | 
|  | 835 | // | 
|  | 836 | //   Used by the runtime to match the type of the thrown exception to the | 
|  | 837 | //   type of the catch clauses or the types in the exception specification. | 
|  | 838 |  | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 839 | Asm->EmitSLEB128Bytes(Action.ValueForTypeID); | 
|  | 840 | Asm->EOL("TypeInfo index"); | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 841 |  | 
|  | 842 | // Action Record | 
|  | 843 | // | 
|  | 844 | //   Self-relative signed displacement in bytes of the next action record, | 
|  | 845 | //   or 0 if there is no next action record. | 
|  | 846 |  | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 847 | Asm->EmitSLEB128Bytes(Action.NextAction); | 
|  | 848 | Asm->EOL("Next action"); | 
|  | 849 | } | 
|  | 850 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 851 | // Emit the Catch Clauses. The code for the catch clauses following the same | 
|  | 852 | // try is similar to a switch statement. The catch clause action record | 
|  | 853 | // informs the runtime about the type of a catch clause and about the | 
|  | 854 | // associated switch value. | 
|  | 855 | // | 
|  | 856 | //  Action Record Fields: | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 857 | // | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 858 | //   * Filter Value | 
|  | 859 | //     Positive value, starting at 1. Index in the types table of the | 
|  | 860 | //     __typeinfo for the catch-clause type. 1 is the first word preceding | 
|  | 861 | //     TTBase, 2 is the second word, and so on. Used by the runtime to check | 
|  | 862 | //     if the thrown exception type matches the catch-clause type. Back-end | 
|  | 863 | //     generated switch statements check against this value. | 
|  | 864 | // | 
|  | 865 | //   * Next | 
|  | 866 | //     Signed offset, in bytes from the start of this field, to the next | 
|  | 867 | //     chained action record, or zero if none. | 
|  | 868 | // | 
|  | 869 | // The order of the action records determined by the next field is the order | 
|  | 870 | // of the catch clauses as they appear in the source code, and must be kept in | 
|  | 871 | // the same order. As a result, changing the order of the catch clause would | 
|  | 872 | // change the semantics of the program. | 
| Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 873 | for (std::vector<GlobalVariable *>::const_reverse_iterator | 
|  | 874 | I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 875 | const GlobalVariable *GV = *I; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 876 | PrintRelDirective(); | 
|  | 877 |  | 
|  | 878 | if (GV) { | 
|  | 879 | std::string GLN; | 
|  | 880 | O << Asm->getGlobalLinkName(GV, GLN); | 
|  | 881 | } else { | 
| Bill Wendling | 049e98d | 2009-08-31 18:26:48 +0000 | [diff] [blame] | 882 | O << "0x0"; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 883 | } | 
|  | 884 |  | 
|  | 885 | Asm->EOL("TypeInfo"); | 
|  | 886 | } | 
|  | 887 |  | 
| Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 888 | // Emit the Type Table. | 
| Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 889 | for (std::vector<unsigned>::const_iterator | 
|  | 890 | I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { | 
|  | 891 | unsigned TypeID = *I; | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 892 | Asm->EmitULEB128Bytes(TypeID); | 
|  | 893 | Asm->EOL("Filter TypeInfo index"); | 
|  | 894 | } | 
|  | 895 |  | 
|  | 896 | Asm->EmitAlignment(2, 0, 0, false); | 
|  | 897 | } | 
|  | 898 |  | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 899 | /// EndModule - Emit all exception information that should come after the | 
|  | 900 | /// content. | 
|  | 901 | void DwarfException::EndModule() { | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 902 | if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf) | 
| Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 903 | return; | 
| Bill Wendling | b4049fe | 2009-09-09 21:06:24 +0000 | [diff] [blame] | 904 |  | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 905 | if (!shouldEmitMovesModule && !shouldEmitTableModule) | 
|  | 906 | return; | 
|  | 907 |  | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 908 | if (TimePassesIsEnabled) | 
|  | 909 | ExceptionTimer->startTimer(); | 
|  | 910 |  | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 911 | const std::vector<Function *> Personalities = MMI->getPersonalities(); | 
| Bill Wendling | b4049fe | 2009-09-09 21:06:24 +0000 | [diff] [blame] | 912 |  | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 913 | for (unsigned i = 0, e = Personalities.size(); i < e; ++i) | 
|  | 914 | EmitCIE(Personalities[i], i); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 915 |  | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 916 | for (std::vector<FunctionEHFrameInfo>::iterator | 
|  | 917 | I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I) | 
|  | 918 | EmitFDE(*I); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 919 |  | 
|  | 920 | if (TimePassesIsEnabled) | 
|  | 921 | ExceptionTimer->stopTimer(); | 
|  | 922 | } | 
|  | 923 |  | 
|  | 924 | /// BeginFunction - Gather pre-function exception information.  Assumes being | 
|  | 925 | /// emitted immediately after the function entry point. | 
|  | 926 | void DwarfException::BeginFunction(MachineFunction *MF) { | 
|  | 927 | if (TimePassesIsEnabled) | 
|  | 928 | ExceptionTimer->startTimer(); | 
|  | 929 |  | 
|  | 930 | this->MF = MF; | 
|  | 931 | shouldEmitTable = shouldEmitMoves = false; | 
|  | 932 |  | 
| Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 933 | if (MMI && MAI->doesSupportExceptionHandling()) { | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 934 | // Map all labels and get rid of any dead landing pads. | 
|  | 935 | MMI->TidyLandingPads(); | 
|  | 936 |  | 
|  | 937 | // If any landing pads survive, we need an EH table. | 
| Bill Wendling | b4049fe | 2009-09-09 21:06:24 +0000 | [diff] [blame] | 938 | if (!MMI->getLandingPads().empty()) | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 939 | shouldEmitTable = true; | 
|  | 940 |  | 
|  | 941 | // See if we need frame move info. | 
|  | 942 | if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory) | 
|  | 943 | shouldEmitMoves = true; | 
|  | 944 |  | 
|  | 945 | if (shouldEmitMoves || shouldEmitTable) | 
|  | 946 | // Assumes in correct section after the entry point. | 
|  | 947 | EmitLabel("eh_func_begin", ++SubprogramCount); | 
|  | 948 | } | 
|  | 949 |  | 
|  | 950 | shouldEmitTableModule |= shouldEmitTable; | 
|  | 951 | shouldEmitMovesModule |= shouldEmitMoves; | 
|  | 952 |  | 
|  | 953 | if (TimePassesIsEnabled) | 
|  | 954 | ExceptionTimer->stopTimer(); | 
|  | 955 | } | 
|  | 956 |  | 
|  | 957 | /// EndFunction - Gather and emit post-function exception information. | 
|  | 958 | /// | 
|  | 959 | void DwarfException::EndFunction() { | 
| Bill Wendling | 7b09a6c | 2009-09-09 21:08:12 +0000 | [diff] [blame] | 960 | if (!shouldEmitMoves && !shouldEmitTable) return; | 
|  | 961 |  | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 962 | if (TimePassesIsEnabled) | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 963 | ExceptionTimer->startTimer(); | 
|  | 964 |  | 
| Bill Wendling | 7b09a6c | 2009-09-09 21:08:12 +0000 | [diff] [blame] | 965 | EmitLabel("eh_func_end", SubprogramCount); | 
|  | 966 | EmitExceptionTable(); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 967 |  | 
| Bill Wendling | 7b09a6c | 2009-09-09 21:08:12 +0000 | [diff] [blame] | 968 | // Save EH frame information | 
|  | 969 | EHFrames.push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF), | 
|  | 970 | SubprogramCount, | 
|  | 971 | MMI->getPersonalityIndex(), | 
|  | 972 | MF->getFrameInfo()->hasCalls(), | 
|  | 973 | !MMI->getLandingPads().empty(), | 
|  | 974 | MMI->getFrameMoves(), | 
|  | 975 | MF->getFunction())); | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 976 |  | 
| Bill Wendling | 52783c6 | 2009-09-09 23:56:55 +0000 | [diff] [blame] | 977 | // Record if this personality index uses a landing pad. | 
|  | 978 | UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty(); | 
|  | 979 |  | 
| Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 980 | if (TimePassesIsEnabled) | 
| Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 981 | ExceptionTimer->stopTimer(); | 
|  | 982 | } |