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