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