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