DwarfException.cpp revision 73b55510585573cd37fca85864c8d7d73e6b4093
1eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===// 2eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// 3eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// The LLVM Compiler Infrastructure 4eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// 5eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// This file is distributed under the University of Illinois Open Source 6eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// License. See LICENSE.TXT for details. 7eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// 8eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===----------------------------------------------------------------------===// 9eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// 1028275fdf02885587156da5cb20388b22d5b96557Bill Wendling// This file contains support for writing DWARF exception info into asm files. 11eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// 12eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===----------------------------------------------------------------------===// 13eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 14eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "DwarfException.h" 15eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Module.h" 16eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineModuleInfo.h" 17eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineFrameInfo.h" 18fc4da0cea8a0f6c9c1483b92c2dffda217c0ba24David Greene#include "llvm/CodeGen/MachineFunction.h" 19eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineLocation.h" 208c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner#include "llvm/MC/MCAsmInfo.h" 218c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner#include "llvm/MC/MCContext.h" 228c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner#include "llvm/MC/MCExpr.h" 2343e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling#include "llvm/MC/MCSection.h" 246c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/MC/MCStreamer.h" 25eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetData.h" 26eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetFrameInfo.h" 27d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner#include "llvm/Target/TargetLoweringObjectFile.h" 28eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetOptions.h" 29d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner#include "llvm/Target/TargetRegisterInfo.h" 306c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Dwarf.h" 313fb2b1ede30193b59a651328a946174196b20610Jim Grosbach#include "llvm/Support/Mangler.h" 326c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Timer.h" 336c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/raw_ostream.h" 34c40d9f9bae70c83947bf8fa5f9ee97adbf1bb0c0Jim Grosbach#include "llvm/ADT/SmallString.h" 35eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/ADT/StringExtras.h" 36eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingusing namespace llvm; 37eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 38eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingstatic TimerGroup &getDwarfTimerGroup() { 3928275fdf02885587156da5cb20388b22d5b96557Bill Wendling static TimerGroup DwarfTimerGroup("DWARF Exception"); 40eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling return DwarfTimerGroup; 41eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 42eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 43bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A, 44af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner const MCAsmInfo *T) 45bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false), 46bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling shouldEmitTableModule(false), shouldEmitMovesModule(false), 47bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling ExceptionTimer(0) { 48dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher if (TimePassesIsEnabled) 4928275fdf02885587156da5cb20388b22d5b96557Bill Wendling ExceptionTimer = new Timer("DWARF Exception Writer", 50bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling getDwarfTimerGroup()); 51bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling} 52bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling 53bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::~DwarfException() { 54bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling delete ExceptionTimer; 55bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling} 56bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling 57bb3e29932d14cc012149a1ab452b88e6f6f5bb49Bill Wendling/// SizeOfEncodedValue - Return the size of the encoding in bytes. 5852783c63a13d384582997684384553e6545852d3Bill Wendlingunsigned DwarfException::SizeOfEncodedValue(unsigned Encoding) { 5952783c63a13d384582997684384553e6545852d3Bill Wendling if (Encoding == dwarf::DW_EH_PE_omit) 6052783c63a13d384582997684384553e6545852d3Bill Wendling return 0; 6152783c63a13d384582997684384553e6545852d3Bill Wendling 6252783c63a13d384582997684384553e6545852d3Bill Wendling switch (Encoding & 0x07) { 6352783c63a13d384582997684384553e6545852d3Bill Wendling case dwarf::DW_EH_PE_absptr: 6452783c63a13d384582997684384553e6545852d3Bill Wendling return TD->getPointerSize(); 6552783c63a13d384582997684384553e6545852d3Bill Wendling case dwarf::DW_EH_PE_udata2: 6652783c63a13d384582997684384553e6545852d3Bill Wendling return 2; 6752783c63a13d384582997684384553e6545852d3Bill Wendling case dwarf::DW_EH_PE_udata4: 6852783c63a13d384582997684384553e6545852d3Bill Wendling return 4; 6952783c63a13d384582997684384553e6545852d3Bill Wendling case dwarf::DW_EH_PE_udata8: 7052783c63a13d384582997684384553e6545852d3Bill Wendling return 8; 7152783c63a13d384582997684384553e6545852d3Bill Wendling } 7252783c63a13d384582997684384553e6545852d3Bill Wendling 7328275fdf02885587156da5cb20388b22d5b96557Bill Wendling assert(0 && "Invalid encoded value."); 7452783c63a13d384582997684384553e6545852d3Bill Wendling return 0; 7552783c63a13d384582997684384553e6545852d3Bill Wendling} 7652783c63a13d384582997684384553e6545852d3Bill Wendling 777ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that 787ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// is shared among many Frame Description Entries. There is at least one CIE 797ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// in every non-empty .debug_frame section. 808c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattnervoid DwarfException::EmitCIE(const Function *PersonalityFn, unsigned Index) { 81eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Size and sign of stack growth. 82eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling int stackGrowth = 83eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->TM.getFrameInfo()->getStackGrowthDirection() == 84eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling TargetFrameInfo::StackGrowsUp ? 85eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling TD->getPointerSize() : -TD->getPointerSize(); 86eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 878c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 888c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner 89eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Begin eh frame section. 908c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection()); 91eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 9233adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner if (MAI->is_EHSymbolPrivate()) 9333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner O << MAI->getPrivateGlobalPrefix(); 94eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling O << "EH_frame" << Index << ":\n"; 958c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner 96eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitLabel("section_eh_frame", Index); 97eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 98eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Define base labels. 99eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitLabel("eh_frame_common", Index); 100eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 101eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Define the eh frame length. 102eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitDifference("eh_frame_common_end", Index, 103eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling "eh_frame_common_begin", Index, true); 104eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("Length of Common Information Entry"); 105eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 106eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // EH frame header. 107eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitLabel("eh_frame_common_begin", Index); 108eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitInt32((int)0); 109eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("CIE Identifier Tag"); 110eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitInt8(dwarf::DW_CIE_VERSION); 111eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("CIE Version"); 112eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 113eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // The personality presence indicates that language specific information will 1148c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner // show up in the eh frame. Find out how we are supposed to lower the 1158c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner // personality function reference: 1168c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner const MCExpr *PersonalityRef = 0; 1178c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner bool IsPersonalityIndirect = false, IsPersonalityPCRel = false; 1188c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner if (PersonalityFn) { 1198c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner // FIXME: HANDLE STATIC CODEGEN MODEL HERE. 1208c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner 1218c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner // In non-static mode, ask the object file how to represent this reference. 1228c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner PersonalityRef = 1238c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner TLOF.getSymbolForDwarfGlobalReference(PersonalityFn, Asm->Mang, 1248609c7c931c0213d6d29f665df2110cf3c709e4cChris Lattner Asm->MMI, 1258c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner IsPersonalityIndirect, 1268c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner IsPersonalityPCRel); 1278c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner } 1288c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner 12952783c63a13d384582997684384553e6545852d3Bill Wendling unsigned PerEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 1308c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner if (IsPersonalityIndirect) 13152783c63a13d384582997684384553e6545852d3Bill Wendling PerEncoding |= dwarf::DW_EH_PE_indirect; 13252783c63a13d384582997684384553e6545852d3Bill Wendling unsigned LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 13352783c63a13d384582997684384553e6545852d3Bill Wendling unsigned FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 13452783c63a13d384582997684384553e6545852d3Bill Wendling 13552783c63a13d384582997684384553e6545852d3Bill Wendling char Augmentation[5] = { 0 }; 13652783c63a13d384582997684384553e6545852d3Bill Wendling unsigned AugmentationSize = 0; 13752783c63a13d384582997684384553e6545852d3Bill Wendling char *APtr = Augmentation + 1; 13852783c63a13d384582997684384553e6545852d3Bill Wendling 1398c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner if (PersonalityRef) { 14052783c63a13d384582997684384553e6545852d3Bill Wendling // There is a personality function. 14152783c63a13d384582997684384553e6545852d3Bill Wendling *APtr++ = 'P'; 14252783c63a13d384582997684384553e6545852d3Bill Wendling AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding); 14352783c63a13d384582997684384553e6545852d3Bill Wendling } 14452783c63a13d384582997684384553e6545852d3Bill Wendling 14552783c63a13d384582997684384553e6545852d3Bill Wendling if (UsesLSDA[Index]) { 14652783c63a13d384582997684384553e6545852d3Bill Wendling // An LSDA pointer is in the FDE augmentation. 14752783c63a13d384582997684384553e6545852d3Bill Wendling *APtr++ = 'L'; 14852783c63a13d384582997684384553e6545852d3Bill Wendling ++AugmentationSize; 14952783c63a13d384582997684384553e6545852d3Bill Wendling } 15052783c63a13d384582997684384553e6545852d3Bill Wendling 15152783c63a13d384582997684384553e6545852d3Bill Wendling if (FDEEncoding != dwarf::DW_EH_PE_absptr) { 15252783c63a13d384582997684384553e6545852d3Bill Wendling // A non-default pointer encoding for the FDE. 15352783c63a13d384582997684384553e6545852d3Bill Wendling *APtr++ = 'R'; 15452783c63a13d384582997684384553e6545852d3Bill Wendling ++AugmentationSize; 15552783c63a13d384582997684384553e6545852d3Bill Wendling } 15652783c63a13d384582997684384553e6545852d3Bill Wendling 15752783c63a13d384582997684384553e6545852d3Bill Wendling if (APtr != Augmentation + 1) 15852783c63a13d384582997684384553e6545852d3Bill Wendling Augmentation[0] = 'z'; 15952783c63a13d384582997684384553e6545852d3Bill Wendling 16052783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EmitString(Augmentation); 161eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("CIE Augmentation"); 162eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 163eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Round out reader. 164eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitULEB128Bytes(1); 165eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("CIE Code Alignment Factor"); 166eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitSLEB128Bytes(stackGrowth); 167eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("CIE Data Alignment Factor"); 168eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true)); 169eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("CIE Return Address Column"); 170eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 17152783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EmitULEB128Bytes(AugmentationSize); 17252783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EOL("Augmentation Size"); 17352783c63a13d384582997684384553e6545852d3Bill Wendling 17452783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EmitInt8(PerEncoding); 17552783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EOL("Personality", PerEncoding); 17652783c63a13d384582997684384553e6545852d3Bill Wendling 1774bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling // If there is a personality, we need to indicate the function's location. 1788c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner if (PersonalityRef) { 179711bb91574db242ba34a86caea719819b0ecef43Bill Wendling // If the reference to the personality function symbol is not already 180711bb91574db242ba34a86caea719819b0ecef43Bill Wendling // pc-relative, then we need to subtract our current address from it. Do 181711bb91574db242ba34a86caea719819b0ecef43Bill Wendling // this by emitting a label and subtracting it from the expression we 182711bb91574db242ba34a86caea719819b0ecef43Bill Wendling // already have. This is equivalent to emitting "foo - .", but we have to 183711bb91574db242ba34a86caea719819b0ecef43Bill Wendling // emit the label for "." directly. 184711bb91574db242ba34a86caea719819b0ecef43Bill Wendling if (!IsPersonalityPCRel) { 185711bb91574db242ba34a86caea719819b0ecef43Bill Wendling SmallString<64> Name; 186711bb91574db242ba34a86caea719819b0ecef43Bill Wendling raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() 187711bb91574db242ba34a86caea719819b0ecef43Bill Wendling << "personalityref_addr" << Asm->getFunctionNumber() << "_" << Index; 188711bb91574db242ba34a86caea719819b0ecef43Bill Wendling MCSymbol *DotSym = Asm->OutContext.GetOrCreateSymbol(Name.str()); 189711bb91574db242ba34a86caea719819b0ecef43Bill Wendling Asm->OutStreamer.EmitLabel(DotSym); 190711bb91574db242ba34a86caea719819b0ecef43Bill Wendling 191711bb91574db242ba34a86caea719819b0ecef43Bill Wendling PersonalityRef = 192711bb91574db242ba34a86caea719819b0ecef43Bill Wendling MCBinaryExpr::CreateSub(PersonalityRef, 193711bb91574db242ba34a86caea719819b0ecef43Bill Wendling MCSymbolRefExpr::Create(DotSym,Asm->OutContext), 194711bb91574db242ba34a86caea719819b0ecef43Bill Wendling Asm->OutContext); 195711bb91574db242ba34a86caea719819b0ecef43Bill Wendling } 196f60d3eb186c2821f65d85b40665d602eb7d051f4Chris Lattner 1978c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner O << MAI->getData32bitsDirective(); 1988c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner PersonalityRef->print(O, MAI); 199eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("Personality"); 200eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 20152783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EmitInt8(LSDAEncoding); 20252783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EOL("LSDA Encoding", LSDAEncoding); 203eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 20452783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EmitInt8(FDEEncoding); 20552783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EOL("FDE Encoding", FDEEncoding); 206eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 207eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 208eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Indicate locations of general callee saved registers in frame. 209eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling std::vector<MachineMove> Moves; 210eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling RI->getInitialFrameState(Moves); 211eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitFrameMoves(NULL, 0, Moves, true); 212eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 213eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // On Darwin the linker honors the alignment of eh_frame, which means it must 214eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get 215eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // holes which confuse readers of eh_frame. 2168c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner Asm->EmitAlignment(TD->getPointerSize() == 4 ? 2 : 3, 0, 0, false); 217eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitLabel("eh_frame_common_end", Index); 218eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 219eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL(); 220eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 221eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 2227ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// EmitFDE - Emit the Frame Description Entry (FDE) for the function. 2237ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendlingvoid DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) { 224dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() && 225eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling "Should not emit 'available externally' functions at all"); 226eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 2273e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner const Function *TheFunc = EHFrameInfo.function; 228dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher 2296c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection()); 230dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher 231eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Externally visible entry into the functions eh frame info. If the 232eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // corresponding function is static, this should not be externally visible. 2333e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner if (!TheFunc->hasLocalLinkage()) 23433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner if (const char *GlobalEHDirective = MAI->getGlobalEHDirective()) 235ee161a68240dfe7e28fdc45d5595380a3ec3b9c8Bill Wendling O << GlobalEHDirective << EHFrameInfo.FnName << '\n'; 236eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 237eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // If corresponding function is weak definition, this should be too. 23833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective()) 239ee161a68240dfe7e28fdc45d5595380a3ec3b9c8Bill Wendling O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << '\n'; 240ee161a68240dfe7e28fdc45d5595380a3ec3b9c8Bill Wendling 241ee161a68240dfe7e28fdc45d5595380a3ec3b9c8Bill Wendling // If corresponding function is hidden, this should be too. 242ee161a68240dfe7e28fdc45d5595380a3ec3b9c8Bill Wendling if (TheFunc->hasHiddenVisibility()) 243ee161a68240dfe7e28fdc45d5595380a3ec3b9c8Bill Wendling if (const char *HiddenDirective = MAI->getHiddenDirective()) 244ee161a68240dfe7e28fdc45d5595380a3ec3b9c8Bill Wendling O << HiddenDirective << EHFrameInfo.FnName << '\n' ; 245eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 246eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // If there are no calls then you can't unwind. This may mean we can omit the 247eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // EH Frame, but some environments do not handle weak absolute symbols. If 248eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // UnwindTablesMandatory is set we cannot do this optimization; the unwind 249eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // info is to be available for non-EH uses. 2503e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory && 2513e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner (!TheFunc->isWeakForLinker() || 25233adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner !MAI->getWeakDefDirective() || 25333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner MAI->getSupportsWeakOmittedEHFrame())) { 254eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling O << EHFrameInfo.FnName << " = 0\n"; 255eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // This name has no connection to the function, so it might get 256eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // dead-stripped when the function is not, erroneously. Prohibit 257eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // dead-stripping unconditionally. 25833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner if (const char *UsedDirective = MAI->getUsedDirective()) 259eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling O << UsedDirective << EHFrameInfo.FnName << "\n\n"; 260eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } else { 261eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling O << EHFrameInfo.FnName << ":\n"; 262eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 263eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // EH frame header. 264eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitDifference("eh_frame_end", EHFrameInfo.Number, 265eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling "eh_frame_begin", EHFrameInfo.Number, true); 266eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("Length of Frame Information Entry"); 267eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 268eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitLabel("eh_frame_begin", EHFrameInfo.Number); 269eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 270a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner EmitSectionOffset("eh_frame_begin", "eh_frame_common", 271a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner EHFrameInfo.Number, EHFrameInfo.PersonalityIndex, 272a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner true, true, false); 273eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 274eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("FDE CIE offset"); 275eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 276c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands EmitReference("eh_func_begin", EHFrameInfo.Number, true, true); 277eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("FDE initial location"); 278eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitDifference("eh_func_end", EHFrameInfo.Number, 279c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands "eh_func_begin", EHFrameInfo.Number, true); 280eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("FDE address range"); 281eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 282eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // If there is a personality and landing pads then point to the language 283eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // specific data area in the exception table. 284d44fff7849034fc17d6a0e9f9926542865bb7881Eric Christopher if (MMI->getPersonalities()[0] != NULL) { 285c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands bool is4Byte = TD->getPointerSize() == sizeof(int32_t); 286c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands 2876fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher Asm->EmitULEB128Bytes(is4Byte ? 4 : 8); 288eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("Augmentation size"); 289eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 290c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands if (EHFrameInfo.hasLandingPads) 2916fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher EmitReference("exception", EHFrameInfo.Number, true, false); 292c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands else { 29352783c63a13d384582997684384553e6545852d3Bill Wendling if (is4Byte) 29452783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EmitInt32((int)0); 29552783c63a13d384582997684384553e6545852d3Bill Wendling else 29652783c63a13d384582997684384553e6545852d3Bill Wendling Asm->EmitInt64((int)0); 2976fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher } 298eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("Language Specific Data Area"); 299eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } else { 300eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitULEB128Bytes(0); 301eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("Augmentation size"); 302eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 303eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 304eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Indicate locations of function specific callee saved registers in frame. 305dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, 306eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling true); 307eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 308eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // On Darwin the linker honors the alignment of eh_frame, which means it 309eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you 310eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // get holes which confuse readers of eh_frame. 311eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, 312eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 0, 0, false); 313eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling EmitLabel("eh_frame_end", EHFrameInfo.Number); 314eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 315eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // If the function is marked used, this table should be also. We cannot 316eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // make the mark unconditional in this case, since retaining the table also 317eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // retains the function in this case, and there is code around that depends 318eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // on unused functions (calling undefined externals) being dead-stripped to 319eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // link correctly. Yes, there really is. 320401e10c4fbfcdcfade5065093e2ca97f69a1d144Chris Lattner if (MMI->isUsedFunction(EHFrameInfo.function)) 32133adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner if (const char *UsedDirective = MAI->getUsedDirective()) 322eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling O << UsedDirective << EHFrameInfo.FnName << "\n\n"; 323eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 3244bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling 3254bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling Asm->EOL(); 326eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 327eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 328eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// SharedTypeIds - How many leading type ids two landing pads have in common. 329eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingunsigned DwarfException::SharedTypeIds(const LandingPadInfo *L, 330eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling const LandingPadInfo *R) { 331eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 332eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned LSize = LIds.size(), RSize = RIds.size(); 333eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned MinSize = LSize < RSize ? LSize : RSize; 334eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned Count = 0; 335eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 336eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling for (; Count != MinSize; ++Count) 337eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (LIds[Count] != RIds[Count]) 338eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling return Count; 339eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 340eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling return Count; 341eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 342eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 343eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// PadLT - Order landing pads lexicographically by type id. 344eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingbool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { 345eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 346eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned LSize = LIds.size(), RSize = RIds.size(); 347eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned MinSize = LSize < RSize ? LSize : RSize; 348eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 349eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling for (unsigned i = 0; i != MinSize; ++i) 350eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (LIds[i] != RIds[i]) 351eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling return LIds[i] < RIds[i]; 352eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 353eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling return LSize < RSize; 354eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 355eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 356d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// ComputeActionsTable - Compute the actions table and gather the first action 357d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// index for each landing pad site. 358ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingunsigned DwarfException:: 359ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads, 360ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVectorImpl<ActionEntry> &Actions, 361ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVectorImpl<unsigned> &FirstActions) { 362a583c55864e83e470333b7be878280b10e175a6eBill Wendling 363a583c55864e83e470333b7be878280b10e175a6eBill Wendling // The action table follows the call-site table in the LSDA. The individual 364a583c55864e83e470333b7be878280b10e175a6eBill Wendling // records are of two types: 365a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 366a583c55864e83e470333b7be878280b10e175a6eBill Wendling // * Catch clause 367a583c55864e83e470333b7be878280b10e175a6eBill Wendling // * Exception specification 368a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 369a583c55864e83e470333b7be878280b10e175a6eBill Wendling // The two record kinds have the same format, with only small differences. 370a583c55864e83e470333b7be878280b10e175a6eBill Wendling // They are distinguished by the "switch value" field: Catch clauses 371a583c55864e83e470333b7be878280b10e175a6eBill Wendling // (TypeInfos) have strictly positive switch values, and exception 372a583c55864e83e470333b7be878280b10e175a6eBill Wendling // specifications (FilterIds) have strictly negative switch values. Value 0 373a583c55864e83e470333b7be878280b10e175a6eBill Wendling // indicates a catch-all clause. 374a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 3755e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling // Negative type IDs index into FilterIds. Positive type IDs index into 3765e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling // TypeInfos. The value written for a positive type ID is just the type ID 3775e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling // itself. For a negative type ID, however, the value written is the 378eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // (negative) byte offset of the corresponding FilterIds entry. The byte 3795e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling // offset is usually equal to the type ID (because the FilterIds entries are 3805e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling // written using a variable width encoding, which outputs one byte per entry 3815e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling // as long as the value written is not too large) but can differ. This kind 3825e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling // of complication does not occur for positive type IDs because type infos are 383eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // output using a fixed width encoding. FilterOffsets[i] holds the byte 384eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // offset corresponding to FilterIds[i]. 385409914b773ffd53d53d214394d5636a76f673186Bill Wendling 386409914b773ffd53d53d214394d5636a76f673186Bill Wendling const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 387eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling SmallVector<int, 16> FilterOffsets; 388eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling FilterOffsets.reserve(FilterIds.size()); 389eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling int Offset = -1; 390409914b773ffd53d53d214394d5636a76f673186Bill Wendling 391409914b773ffd53d53d214394d5636a76f673186Bill Wendling for (std::vector<unsigned>::const_iterator 392409914b773ffd53d53d214394d5636a76f673186Bill Wendling I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { 393eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling FilterOffsets.push_back(Offset); 394af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner Offset -= MCAsmInfo::getULEB128Size(*I); 395eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 396eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 397eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling FirstActions.reserve(LandingPads.size()); 398eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 399eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling int FirstAction = 0; 400eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned SizeActions = 0; 4015e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling const LandingPadInfo *PrevLPI = 0; 402409914b773ffd53d53d214394d5636a76f673186Bill Wendling 4035cff487665097d971067cbede1598e249f673182Bill Wendling for (SmallVectorImpl<const LandingPadInfo *>::const_iterator 4045e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { 4055e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling const LandingPadInfo *LPI = *I; 4065e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling const std::vector<int> &TypeIds = LPI->TypeIds; 4075e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0; 408eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned SizeSiteActions = 0; 409eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 410eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (NumShared < TypeIds.size()) { 411eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned SizeAction = 0; 412eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ActionEntry *PrevAction = 0; 413eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 414eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (NumShared) { 4155e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling const unsigned SizePrevIds = PrevLPI->TypeIds.size(); 416eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling assert(Actions.size()); 417eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PrevAction = &Actions.back(); 418af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) + 419af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); 420eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 421eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling for (unsigned j = NumShared; j != SizePrevIds; ++j) { 422eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling SizeAction -= 423af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); 424eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling SizeAction += -PrevAction->NextAction; 425eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PrevAction = PrevAction->Previous; 426eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 427eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 428eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 429eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Compute the actions. 4305e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { 4315e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling int TypeID = TypeIds[J]; 4325e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 433eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; 434af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID); 435eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 436eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; 437af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction); 438eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling SizeSiteActions += SizeAction; 439eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 440a583c55864e83e470333b7be878280b10e175a6eBill Wendling ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; 441eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Actions.push_back(Action); 442eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PrevAction = &Actions.back(); 443eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 444eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 445eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Record the first action of the landing pad site. 446eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; 447eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } // else identical - re-use previous FirstAction 448eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 449a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Information used when created the call-site table. The action record 450a583c55864e83e470333b7be878280b10e175a6eBill Wendling // field of the call site record is the offset of the first associated 451a583c55864e83e470333b7be878280b10e175a6eBill Wendling // action record, relative to the start of the actions table. This value is 452a583c55864e83e470333b7be878280b10e175a6eBill Wendling // biased by 1 (1 in dicating the start of the actions table), and 0 453a583c55864e83e470333b7be878280b10e175a6eBill Wendling // indicates that there are no actions. 454eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling FirstActions.push_back(FirstAction); 455eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 456eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Compute this sites contribution to size. 457eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling SizeActions += SizeSiteActions; 4585e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling 4595e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling PrevLPI = LPI; 460eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 461eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 4625e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling return SizeActions; 4635e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling} 4645e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling 465ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke 466a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// has a try-range containing the call, a non-zero landing pad, and an 467ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// appropriate action. The entry for an ordinary call has a try-range 468ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// containing the call and zero for the landing pad and the action. Calls 469ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// marked 'nounwind' have no entry and must not be contained in the try-range 470ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// of any entry - they form gaps in the table. Entries must be ordered by 471ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// try-range address. 472ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException:: 473ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, 474ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const RangeMapType &PadMap, 475ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 476ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const SmallVectorImpl<unsigned> &FirstActions) { 477eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // The end label of the previous invoke or nounwind try-range. 478eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned LastLabel = 0; 479eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 480eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Whether there is a potentially throwing instruction (currently this means 481eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // an ordinary call) between the end of the previous try-range and now. 482eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling bool SawPotentiallyThrowing = false; 483eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 4845cff487665097d971067cbede1598e249f673182Bill Wendling // Whether the last CallSite entry was for an invoke. 485eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling bool PreviousIsInvoke = false; 486eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 487eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Visit all instructions in order of address. 488eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 489eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling I != E; ++I) { 490eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); 491eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling MI != E; ++MI) { 492eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (!MI->isLabel()) { 49373b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling if (MI->getDesc().isCall()) { 49473b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling // Don't mark a call as potentially throwing if the function it's 49573b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling // calling is marked "nounwind". 49673b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling bool DoesNotThrow = false; 49773b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling for (unsigned OI = 0, OE = MI->getNumOperands(); OI != OE; ++OI) { 49873b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling const MachineOperand &MO = MI->getOperand(OI); 49973b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling 50073b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling if (MO.isGlobal()) { 50173b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling if (Function *F = dyn_cast<Function>(MO.getGlobal())) { 50273b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling if (F->doesNotThrow()) { 50373b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling DoesNotThrow = true; 50473b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling break; 50573b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling } 50673b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling } 50773b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling } 50873b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling } 50973b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling 51073b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling if (!DoesNotThrow) 51173b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling SawPotentiallyThrowing = true; 51273b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling } 51373b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling 514eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling continue; 515eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 516eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 517eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned BeginLabel = MI->getOperand(0).getImm(); 518eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling assert(BeginLabel && "Invalid label!"); 519eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 520eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // End of the previous try-range? 521eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (BeginLabel == LastLabel) 522eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling SawPotentiallyThrowing = false; 523eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 524eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Beginning of a new try-range? 52581cf4325698b48b02eddab921ac333c7f25005c3Jeffrey Yasskin RangeMapType::const_iterator L = PadMap.find(BeginLabel); 526eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (L == PadMap.end()) 527eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Nope, it was just some random label. 528eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling continue; 529eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 530a583c55864e83e470333b7be878280b10e175a6eBill Wendling const PadRange &P = L->second; 531eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 532eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 533eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling "Inconsistent landing pad map!"); 534eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 535a583c55864e83e470333b7be878280b10e175a6eBill Wendling // For Dwarf exception handling (SjLj handling doesn't use this). If some 536a583c55864e83e470333b7be878280b10e175a6eBill Wendling // instruction between the previous try-range and this one may throw, 537a583c55864e83e470333b7be878280b10e175a6eBill Wendling // create a call-site entry with no landing pad for the region between the 538a583c55864e83e470333b7be878280b10e175a6eBill Wendling // try-ranges. 5391b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (SawPotentiallyThrowing && 54033adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { 541a583c55864e83e470333b7be878280b10e175a6eBill Wendling CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 }; 542eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling CallSites.push_back(Site); 543eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PreviousIsInvoke = false; 544eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 545eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 546eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling LastLabel = LandingPad->EndLabels[P.RangeIndex]; 547eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling assert(BeginLabel && LastLabel && "Invalid landing pad!"); 548eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 549eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (LandingPad->LandingPadLabel) { 550eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // This try-range is for an invoke. 551a583c55864e83e470333b7be878280b10e175a6eBill Wendling CallSiteEntry Site = { 552a583c55864e83e470333b7be878280b10e175a6eBill Wendling BeginLabel, 553a583c55864e83e470333b7be878280b10e175a6eBill Wendling LastLabel, 554a583c55864e83e470333b7be878280b10e175a6eBill Wendling LandingPad->LandingPadLabel, 555a583c55864e83e470333b7be878280b10e175a6eBill Wendling FirstActions[P.PadIndex] 556a583c55864e83e470333b7be878280b10e175a6eBill Wendling }; 557eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 55833668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach // Try to merge with the previous call-site. SJLJ doesn't do this 55933668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach if (PreviousIsInvoke && 56033668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { 561eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling CallSiteEntry &Prev = CallSites.back(); 562eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { 563eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Extend the range of the previous entry. 564eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Prev.EndLabel = Site.EndLabel; 565eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling continue; 566eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 567eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 568eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 569eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Otherwise, create a new call-site. 570eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling CallSites.push_back(Site); 571eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PreviousIsInvoke = true; 572eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } else { 573eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Create a gap. 574eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PreviousIsInvoke = false; 575eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 576eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 577eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 578eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 579eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // If some instruction between the previous try-range and the end of the 580eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // function may throw, create a call-site entry with no landing pad for the 581eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // region following the try-range. 5821b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (SawPotentiallyThrowing && 58333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { 584a583c55864e83e470333b7be878280b10e175a6eBill Wendling CallSiteEntry Site = { LastLabel, 0, 0, 0 }; 585eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling CallSites.push_back(Site); 586eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 587ade025c65c12503aba161a4fa399fd97414abaffBill Wendling} 588ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 5890dafca90761097230f02e655fdd541f59b888315Bill Wendling/// EmitExceptionTable - Emit landing pads and actions. 5900dafca90761097230f02e655fdd541f59b888315Bill Wendling/// 5910dafca90761097230f02e655fdd541f59b888315Bill Wendling/// The general organization of the table is complex, but the basic concepts are 5920dafca90761097230f02e655fdd541f59b888315Bill Wendling/// easy. First there is a header which describes the location and organization 5930dafca90761097230f02e655fdd541f59b888315Bill Wendling/// of the three components that follow. 594dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher/// 5950dafca90761097230f02e655fdd541f59b888315Bill Wendling/// 1. The landing pad site information describes the range of code covered by 5960dafca90761097230f02e655fdd541f59b888315Bill Wendling/// the try. In our case it's an accumulation of the ranges covered by the 5970dafca90761097230f02e655fdd541f59b888315Bill Wendling/// invokes in the try. There is also a reference to the landing pad that 5980dafca90761097230f02e655fdd541f59b888315Bill Wendling/// handles the exception once processed. Finally an index into the actions 5990dafca90761097230f02e655fdd541f59b888315Bill Wendling/// table. 600a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// 2. The action table, in our case, is composed of pairs of type IDs and next 6010dafca90761097230f02e655fdd541f59b888315Bill Wendling/// action offset. Starting with the action index from the landing pad 602a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// site, each type ID is checked for a match to the current exception. If 6030dafca90761097230f02e655fdd541f59b888315Bill Wendling/// it matches then the exception and type id are passed on to the landing 6040dafca90761097230f02e655fdd541f59b888315Bill Wendling/// pad. Otherwise the next action is looked up. This chain is terminated 60528275fdf02885587156da5cb20388b22d5b96557Bill Wendling/// with a next action of zero. If no type id is found then the frame is 6060dafca90761097230f02e655fdd541f59b888315Bill Wendling/// unwound and handling continues. 607a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// 3. Type ID table contains references to all the C++ typeinfo for all 60828275fdf02885587156da5cb20388b22d5b96557Bill Wendling/// catches in the function. This tables is reverse indexed base 1. 609ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::EmitExceptionTable() { 610ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); 611ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 612ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); 613ade025c65c12503aba161a4fa399fd97414abaffBill Wendling if (PadInfos.empty()) return; 614ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 615ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // Sort the landing pads in order of their type ids. This is used to fold 616ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // duplicate actions. 617ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVector<const LandingPadInfo *, 64> LandingPads; 618ade025c65c12503aba161a4fa399fd97414abaffBill Wendling LandingPads.reserve(PadInfos.size()); 619ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 620ade025c65c12503aba161a4fa399fd97414abaffBill Wendling for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 621ade025c65c12503aba161a4fa399fd97414abaffBill Wendling LandingPads.push_back(&PadInfos[i]); 622ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 623ade025c65c12503aba161a4fa399fd97414abaffBill Wendling std::sort(LandingPads.begin(), LandingPads.end(), PadLT); 624ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 625ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // Compute the actions table and gather the first action index for each 626ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // landing pad site. 627ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVector<ActionEntry, 32> Actions; 628ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVector<unsigned, 64> FirstActions; 62928275fdf02885587156da5cb20388b22d5b96557Bill Wendling unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, 63028275fdf02885587156da5cb20388b22d5b96557Bill Wendling FirstActions); 631ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 632ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // Invokes and nounwind calls have entries in PadMap (due to being bracketed 633ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // by try-range labels when lowered). Ordinary calls do not, so appropriate 63428275fdf02885587156da5cb20388b22d5b96557Bill Wendling // try-ranges for them need be deduced when using DWARF exception handling. 635ade025c65c12503aba161a4fa399fd97414abaffBill Wendling RangeMapType PadMap; 636ade025c65c12503aba161a4fa399fd97414abaffBill Wendling for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 637ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const LandingPadInfo *LandingPad = LandingPads[i]; 638ade025c65c12503aba161a4fa399fd97414abaffBill Wendling for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 639ade025c65c12503aba161a4fa399fd97414abaffBill Wendling unsigned BeginLabel = LandingPad->BeginLabels[j]; 640ade025c65c12503aba161a4fa399fd97414abaffBill Wendling assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 641ade025c65c12503aba161a4fa399fd97414abaffBill Wendling PadRange P = { i, j }; 642ade025c65c12503aba161a4fa399fd97414abaffBill Wendling PadMap[BeginLabel] = P; 643ade025c65c12503aba161a4fa399fd97414abaffBill Wendling } 644ade025c65c12503aba161a4fa399fd97414abaffBill Wendling } 645ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 646ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // Compute the call-site table. 647ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVector<CallSiteEntry, 64> CallSites; 6488b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions); 649eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 650eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Final tallies. 651eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 652eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Call sites. 65340121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); 65440121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); 65540121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); 656d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 657d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true; 65828275fdf02885587156da5cb20388b22d5b96557Bill Wendling unsigned SizeSites; 659d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling 660d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling if (IsSJLJ) 6618b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach SizeSites = 0; 662d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling else 6631b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach SizeSites = CallSites.size() * 6641b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach (SiteStartSize + SiteLengthSize + LandingPadSize); 665d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling 6661b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { 667af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action); 668d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling if (IsSJLJ) 669af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner SizeSites += MCAsmInfo::getULEB128Size(i); 6701b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 671d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling 672eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Type infos. 673d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); 674fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling unsigned TTypeFormat; 675a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned TypeFormatSize; 676eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 67743e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling if (!HaveTTData) { 67828275fdf02885587156da5cb20388b22d5b96557Bill Wendling // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say 67928275fdf02885587156da5cb20388b22d5b96557Bill Wendling // that we're omitting that bit. 680fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling TTypeFormat = dwarf::DW_EH_PE_omit; 681a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr); 68281c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner } else { 683ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // Okay, we have actual filters or typeinfos to emit. As such, we need to 684ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // pick a type encoding for them. We're about to emit a list of pointers to 685ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // typeinfo objects at the end of the LSDA. However, unless we're in static 686ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // mode, this reference will require a relocation by the dynamic linker. 68746b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner // 688ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // Because of this, we have a couple of options: 68928275fdf02885587156da5cb20388b22d5b96557Bill Wendling // 690ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 1) If we are in -static mode, we can always use an absolute reference 691ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // from the LSDA, because the static linker will resolve it. 69228275fdf02885587156da5cb20388b22d5b96557Bill Wendling // 693ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 2) Otherwise, if the LSDA section is writable, we can output the direct 694ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // reference to the typeinfo and allow the dynamic linker to relocate 695ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // it. Since it is in a writable section, the dynamic linker won't 696ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // have a problem. 69728275fdf02885587156da5cb20388b22d5b96557Bill Wendling // 698ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 699ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // we need to use some form of indirection. For example, on Darwin, 700ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // we can output a statically-relocatable reference to a dyld stub. The 701ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // offset to the stub is constant, but the contents are in a section 702ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // that is updated by the dynamic linker. This is easy enough, but we 703ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // need to tell the personality function of the unwinder to indirect 704ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // through the dyld stub. 705ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 70643e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling // FIXME: When (3) is actually implemented, we'll have to emit the stubs 707ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // somewhere. This predicate should be moved to a shared location that is 708ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // in target-independent code. 709ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 71043e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling if (LSDASection->getKind().isWriteable() || 71143e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling Asm->TM.getRelocationModel() == Reloc::Static) 71243e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling TTypeFormat = dwarf::DW_EH_PE_absptr; 71343e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling else 71443e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling TTypeFormat = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 71543e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling dwarf::DW_EH_PE_sdata4; 716a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling 717a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling TypeFormatSize = SizeOfEncodedValue(TTypeFormat); 718fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling } 719fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 720fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling // Begin the exception table. 721fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->OutStreamer.SwitchSection(LSDASection); 722fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EmitAlignment(2, 0, 0, false); 72343e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling 724fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling O << "GCC_except_table" << SubprogramCount << ":\n"; 725a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling 726a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // The type infos need to be aligned. GCC does this by inserting padding just 727a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // before the type infos. However, this changes the size of the exception 728a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // table, so you need to take this into account when you output the exception 729a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // table size. However, the size is output using a variable length encoding. 730a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // So by increasing the size by inserting padding, you may increase the number 731a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // of bytes used for writing the size. If it increases, say by one byte, then 732a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // you now need to output one less byte of padding to get the type infos 733a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // aligned. However this decreases the size of the exception table. This 734a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // changes the value you have to output for the exception table size. Due to 735a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // the variable length encoding, the number of bytes used for writing the 736a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // length may decrease. If so, you then have to increase the amount of 737a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // padding. And so on. If you look carefully at the GCC code you will see that 738a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // it indeed does this in a loop, going on and on until the values stabilize. 739a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // We chose another solution: don't output padding inside the table like GCC 740a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // does, instead output it before the table. 741a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned SizeTypes = TypeInfos.size() * TypeFormatSize; 742a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned TyOffset = sizeof(int8_t) + // Call site format 743a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length 744a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling SizeSites + SizeActions + SizeTypes; 745a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned TotalSize = sizeof(int8_t) + // LPStart format 746a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling sizeof(int8_t) + // TType format 747a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling (HaveTTData ? 748a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling MCAsmInfo::getULEB128Size(TyOffset) : 0) + // TType base offset 749a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling TyOffset; 750a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned SizeAlign = (4 - TotalSize) & 3; 751a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling 752a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling for (unsigned i = 0; i != SizeAlign; ++i) { 753a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling Asm->EmitInt8(0); 754a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling Asm->EOL("Padding"); 755a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling } 756a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling 757fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling EmitLabel("exception", SubprogramCount); 758fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 759fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling if (IsSJLJ) { 760fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling SmallString<16> LSDAName; 761fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() << 762fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling "_LSDA_" << Asm->getFunctionNumber(); 763fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling O << LSDAName.str() << ":\n"; 764fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling } 765fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 766fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling // Emit the header. 767fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EmitInt8(dwarf::DW_EH_PE_omit); 768fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit); 769fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 770fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EmitInt8(TTypeFormat); 771fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EOL("@TType format", TTypeFormat); 772fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 773fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling if (HaveTTData) { 774a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling Asm->EmitULEB128Bytes(TyOffset); 775a583c55864e83e470333b7be878280b10e175a6eBill Wendling Asm->EOL("@TType base offset"); 7761b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 777b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling 77828275fdf02885587156da5cb20388b22d5b96557Bill Wendling // SjLj Exception handling 779d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling if (IsSJLJ) { 780639217cb6a3948800c71c39503f8def4873a535eBill Wendling Asm->EmitInt8(dwarf::DW_EH_PE_udata4); 7810734d35044c304dd072f20e49840bcbea427db9cBill Wendling Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4); 7821b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitULEB128Bytes(SizeSites); 783a583c55864e83e470333b7be878280b10e175a6eBill Wendling Asm->EOL("Call site table length"); 7841b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach 7851b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach // Emit the landing pad site information. 7868b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach unsigned idx = 0; 7878b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach for (SmallVectorImpl<CallSiteEntry>::const_iterator 7888b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 7898b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach const CallSiteEntry &S = *I; 790a583c55864e83e470333b7be878280b10e175a6eBill Wendling 791a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the landing pad, counted in 16-byte bundles relative to the 792a583c55864e83e470333b7be878280b10e175a6eBill Wendling // @LPStart address. 7938b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach Asm->EmitULEB128Bytes(idx); 7941b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Landing pad"); 795a583c55864e83e470333b7be878280b10e175a6eBill Wendling 796a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the first associated action record, relative to the start of 797a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the action table. This value is biased by 1 (1 indicates the start of 798a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the action table), and 0 indicates that there are no actions. 7991b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitULEB128Bytes(S.Action); 8001b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Action"); 8011b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 8021b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } else { 8031b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach // DWARF Exception handling 80433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf); 8051b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach 806a583c55864e83e470333b7be878280b10e175a6eBill Wendling // The call-site table is a list of all call sites that may throw an 807a583c55864e83e470333b7be878280b10e175a6eBill Wendling // exception (including C++ 'throw' statements) in the procedure 808a583c55864e83e470333b7be878280b10e175a6eBill Wendling // fragment. It immediately follows the LSDA header. Each entry indicates, 809a583c55864e83e470333b7be878280b10e175a6eBill Wendling // for a given call, the first corresponding action record and corresponding 810a583c55864e83e470333b7be878280b10e175a6eBill Wendling // landing pad. 811a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 812a583c55864e83e470333b7be878280b10e175a6eBill Wendling // The table begins with the number of bytes, stored as an LEB128 813a583c55864e83e470333b7be878280b10e175a6eBill Wendling // compressed, unsigned integer. The records immediately follow the record 814a583c55864e83e470333b7be878280b10e175a6eBill Wendling // count. They are sorted in increasing call-site address. Each record 815a583c55864e83e470333b7be878280b10e175a6eBill Wendling // indicates: 816a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 817a583c55864e83e470333b7be878280b10e175a6eBill Wendling // * The position of the call-site. 818a583c55864e83e470333b7be878280b10e175a6eBill Wendling // * The position of the landing pad. 819a583c55864e83e470333b7be878280b10e175a6eBill Wendling // * The first action record for that call site. 820a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 821a583c55864e83e470333b7be878280b10e175a6eBill Wendling // A missing entry in the call-site table indicates that a call is not 82228275fdf02885587156da5cb20388b22d5b96557Bill Wendling // supposed to throw. 823a583c55864e83e470333b7be878280b10e175a6eBill Wendling 824a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Emit the landing pad call site table. 825639217cb6a3948800c71c39503f8def4873a535eBill Wendling Asm->EmitInt8(dwarf::DW_EH_PE_udata4); 8260734d35044c304dd072f20e49840bcbea427db9cBill Wendling Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4); 8271b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitULEB128Bytes(SizeSites); 828a583c55864e83e470333b7be878280b10e175a6eBill Wendling Asm->EOL("Call site table size"); 829eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8301b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach for (SmallVectorImpl<CallSiteEntry>::const_iterator 8315cff487665097d971067cbede1598e249f673182Bill Wendling I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { 8321b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach const CallSiteEntry &S = *I; 8331b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach const char *BeginTag; 8341b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach unsigned BeginNumber; 835eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8361b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (!S.BeginLabel) { 8371b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach BeginTag = "eh_func_begin"; 8381b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach BeginNumber = SubprogramCount; 8391b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } else { 8401b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach BeginTag = "label"; 8411b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach BeginNumber = S.BeginLabel; 8421b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 843eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 844a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the call site relative to the previous call site, counted in 845a583c55864e83e470333b7be878280b10e175a6eBill Wendling // number of 16-byte bundles. The first call site is counted relative to 846a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the start of the procedure fragment. 8471b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount, 8481b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach true, true); 8491b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Region start"); 850eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8511b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (!S.EndLabel) 8521b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber, 8531b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach true); 8541b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach else 8551b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true); 856eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8571b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Region length"); 858eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 859a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the landing pad, counted in 16-byte bundles relative to the 860a583c55864e83e470333b7be878280b10e175a6eBill Wendling // @LPStart address. 8611b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (!S.PadLabel) 8621b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitInt32(0); 8631b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach else 8641b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount, 8651b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach true, true); 866eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8671b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Landing pad"); 868eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 869a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the first associated action record, relative to the start of 870a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the action table. This value is biased by 1 (1 indicates the start of 871a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the action table), and 0 indicates that there are no actions. 8721b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitULEB128Bytes(S.Action); 8731b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Action"); 8741b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 875eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 876eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 877a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Emit the Action Table. 8785cff487665097d971067cbede1598e249f673182Bill Wendling for (SmallVectorImpl<ActionEntry>::const_iterator 8795cff487665097d971067cbede1598e249f673182Bill Wendling I = Actions.begin(), E = Actions.end(); I != E; ++I) { 8805cff487665097d971067cbede1598e249f673182Bill Wendling const ActionEntry &Action = *I; 881a583c55864e83e470333b7be878280b10e175a6eBill Wendling 882a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Type Filter 883a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 884a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Used by the runtime to match the type of the thrown exception to the 885a583c55864e83e470333b7be878280b10e175a6eBill Wendling // type of the catch clauses or the types in the exception specification. 886a583c55864e83e470333b7be878280b10e175a6eBill Wendling 887eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitSLEB128Bytes(Action.ValueForTypeID); 888eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("TypeInfo index"); 889a583c55864e83e470333b7be878280b10e175a6eBill Wendling 890a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Action Record 891a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 892a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Self-relative signed displacement in bytes of the next action record, 893a583c55864e83e470333b7be878280b10e175a6eBill Wendling // or 0 if there is no next action record. 894a583c55864e83e470333b7be878280b10e175a6eBill Wendling 895eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitSLEB128Bytes(Action.NextAction); 896eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("Next action"); 897eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 898eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 89948dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling // Emit the Catch TypeInfos. 9005cff487665097d971067cbede1598e249f673182Bill Wendling for (std::vector<GlobalVariable *>::const_reverse_iterator 901711bb91574db242ba34a86caea719819b0ecef43Bill Wendling I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { 902711bb91574db242ba34a86caea719819b0ecef43Bill Wendling const GlobalVariable *GV = *I; 903711bb91574db242ba34a86caea719819b0ecef43Bill Wendling PrintRelDirective(); 904296ab7e5445f0402d0aae3814c38f964d6f547fbBill Wendling 905711bb91574db242ba34a86caea719819b0ecef43Bill Wendling if (GV) { 906711bb91574db242ba34a86caea719819b0ecef43Bill Wendling O << Asm->Mang->getMangledName(GV); 907eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } else { 908049e98d641f90e90e8312d76cbf4c68908fb9d1dBill Wendling O << "0x0"; 909eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 910eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 911eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("TypeInfo"); 912eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 913eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 91448dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling // Emit the Exception Specifications. 9155cff487665097d971067cbede1598e249f673182Bill Wendling for (std::vector<unsigned>::const_iterator 9165cff487665097d971067cbede1598e249f673182Bill Wendling I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 9175cff487665097d971067cbede1598e249f673182Bill Wendling unsigned TypeID = *I; 918eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitULEB128Bytes(TypeID); 91948dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling if (TypeID != 0) 92048dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling Asm->EOL("Exception specification"); 92148dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling else 92248dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling Asm->EOL(); 923eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 924eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 925eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitAlignment(2, 0, 0, false); 926eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 927eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 928eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndModule - Emit all exception information that should come after the 929eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// content. 930eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndModule() { 93133adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf) 9321b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach return; 933b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling 93452783c63a13d384582997684384553e6545852d3Bill Wendling if (!shouldEmitMovesModule && !shouldEmitTableModule) 93552783c63a13d384582997684384553e6545852d3Bill Wendling return; 93652783c63a13d384582997684384553e6545852d3Bill Wendling 937eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (TimePassesIsEnabled) 938eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->startTimer(); 939eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 94052783c63a13d384582997684384553e6545852d3Bill Wendling const std::vector<Function *> Personalities = MMI->getPersonalities(); 941b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling 94228275fdf02885587156da5cb20388b22d5b96557Bill Wendling for (unsigned I = 0, E = Personalities.size(); I < E; ++I) 94328275fdf02885587156da5cb20388b22d5b96557Bill Wendling EmitCIE(Personalities[I], I); 944eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 94552783c63a13d384582997684384553e6545852d3Bill Wendling for (std::vector<FunctionEHFrameInfo>::iterator 94652783c63a13d384582997684384553e6545852d3Bill Wendling I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I) 94752783c63a13d384582997684384553e6545852d3Bill Wendling EmitFDE(*I); 948eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 949eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (TimePassesIsEnabled) 950eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->stopTimer(); 951eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 952eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 95328275fdf02885587156da5cb20388b22d5b96557Bill Wendling/// BeginFunction - Gather pre-function exception information. Assumes it's 95428275fdf02885587156da5cb20388b22d5b96557Bill Wendling/// being emitted immediately after the function entry point. 955eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::BeginFunction(MachineFunction *MF) { 95673c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling if (!MMI || !MAI->doesSupportExceptionHandling()) return; 95773c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling 958eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (TimePassesIsEnabled) 959eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->startTimer(); 960eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 961eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling this->MF = MF; 962eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling shouldEmitTable = shouldEmitMoves = false; 963eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 96473c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling // Map all labels and get rid of any dead landing pads. 96573c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling MMI->TidyLandingPads(); 966eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 96773c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling // If any landing pads survive, we need an EH table. 96873c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling if (!MMI->getLandingPads().empty()) 96973c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling shouldEmitTable = true; 970eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 97173c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling // See if we need frame move info. 97273c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory) 97373c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling shouldEmitMoves = true; 974eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 97573c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling if (shouldEmitMoves || shouldEmitTable) 97673c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling // Assumes in correct section after the entry point. 97773c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling EmitLabel("eh_func_begin", ++SubprogramCount); 978eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 979eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling shouldEmitTableModule |= shouldEmitTable; 980eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling shouldEmitMovesModule |= shouldEmitMoves; 981eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 982eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (TimePassesIsEnabled) 983eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->stopTimer(); 984eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 985eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 986eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndFunction - Gather and emit post-function exception information. 987eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// 988eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndFunction() { 9897b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling if (!shouldEmitMoves && !shouldEmitTable) return; 9907b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling 991dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher if (TimePassesIsEnabled) 992eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->startTimer(); 993eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 9947b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling EmitLabel("eh_func_end", SubprogramCount); 9957b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling EmitExceptionTable(); 9967b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling 99725d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner std::string FunctionEHName = 99825d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner Asm->Mang->getMangledName(MF->getFunction(), ".eh", 99925d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner Asm->MAI->is_EHSymbolPrivate()); 100025d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner 10017b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling // Save EH frame information 100225d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner EHFrames.push_back(FunctionEHFrameInfo(FunctionEHName, SubprogramCount, 10037b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling MMI->getPersonalityIndex(), 10047b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling MF->getFrameInfo()->hasCalls(), 10057b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling !MMI->getLandingPads().empty(), 10067b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling MMI->getFrameMoves(), 10077b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling MF->getFunction())); 1008eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 100952783c63a13d384582997684384553e6545852d3Bill Wendling // Record if this personality index uses a landing pad. 101052783c63a13d384582997684384553e6545852d3Bill Wendling UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty(); 101152783c63a13d384582997684384553e6545852d3Bill Wendling 1012dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher if (TimePassesIsEnabled) 1013eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->stopTimer(); 1014eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 1015