DwarfException.cpp revision ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7
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 465ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling/// CallToNoUnwindFunction - Return `true' if this is a call to a function 466ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling/// marked `nounwind'. Return `false' otherwise. 467ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendlingbool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) { 468ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling assert(MI->getDesc().isCall() && "This should be a call instruction!"); 469ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling 470ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling bool MarkedNoUnwind = false; 471ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling bool SawFunc = false; 472ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling 473ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { 474ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling const MachineOperand &MO = MI->getOperand(I); 475ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling 476ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling if (MO.isGlobal()) { 477ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling if (Function *F = dyn_cast<Function>(MO.getGlobal())) { 478ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling if (SawFunc) { 479ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling // Be conservative. If we have more than one function operand for this 480ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling // call, then we can't make the assumption that it's the callee and 481ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling // not a parameter to the call. 482ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling // 483ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling // FIXME: Determine if there's a way to say that `F' is the callee or 484ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling // parameter. 485ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling MarkedNoUnwind = false; 486ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling break; 487ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling } 488ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling if (F->doesNotThrow()) { 489ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling SawFunc = true; 490ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling MarkedNoUnwind = true; 491ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling } 492ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling } 493ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling } 494ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling } 495ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling 496ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling return MarkedNoUnwind; 497ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling} 498ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling 499ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke 500a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// has a try-range containing the call, a non-zero landing pad, and an 501ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// appropriate action. The entry for an ordinary call has a try-range 502ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// containing the call and zero for the landing pad and the action. Calls 503ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// marked 'nounwind' have no entry and must not be contained in the try-range 504ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// of any entry - they form gaps in the table. Entries must be ordered by 505ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// try-range address. 506ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException:: 507ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, 508ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const RangeMapType &PadMap, 509ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 510ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const SmallVectorImpl<unsigned> &FirstActions) { 511eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // The end label of the previous invoke or nounwind try-range. 512eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned LastLabel = 0; 513eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 514eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Whether there is a potentially throwing instruction (currently this means 515eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // an ordinary call) between the end of the previous try-range and now. 516eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling bool SawPotentiallyThrowing = false; 517eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 5185cff487665097d971067cbede1598e249f673182Bill Wendling // Whether the last CallSite entry was for an invoke. 519eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling bool PreviousIsInvoke = false; 520eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 521eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Visit all instructions in order of address. 522eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 523eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling I != E; ++I) { 524eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); 525eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling MI != E; ++MI) { 526eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (!MI->isLabel()) { 527ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling if (MI->getDesc().isCall()) 528ed060dcb5c7e9cb2fd11ff3b0b98203fb53063e7Bill Wendling SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI); 52973b55510585573cd37fca85864c8d7d73e6b4093Bill Wendling 530eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling continue; 531eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 532eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 533eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling unsigned BeginLabel = MI->getOperand(0).getImm(); 534eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling assert(BeginLabel && "Invalid label!"); 535eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 536eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // End of the previous try-range? 537eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (BeginLabel == LastLabel) 538eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling SawPotentiallyThrowing = false; 539eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 540eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Beginning of a new try-range? 54181cf4325698b48b02eddab921ac333c7f25005c3Jeffrey Yasskin RangeMapType::const_iterator L = PadMap.find(BeginLabel); 542eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (L == PadMap.end()) 543eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Nope, it was just some random label. 544eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling continue; 545eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 546a583c55864e83e470333b7be878280b10e175a6eBill Wendling const PadRange &P = L->second; 547eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 548eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 549eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling "Inconsistent landing pad map!"); 550eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 551a583c55864e83e470333b7be878280b10e175a6eBill Wendling // For Dwarf exception handling (SjLj handling doesn't use this). If some 552a583c55864e83e470333b7be878280b10e175a6eBill Wendling // instruction between the previous try-range and this one may throw, 553a583c55864e83e470333b7be878280b10e175a6eBill Wendling // create a call-site entry with no landing pad for the region between the 554a583c55864e83e470333b7be878280b10e175a6eBill Wendling // try-ranges. 5551b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (SawPotentiallyThrowing && 55633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { 557a583c55864e83e470333b7be878280b10e175a6eBill Wendling CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 }; 558eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling CallSites.push_back(Site); 559eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PreviousIsInvoke = false; 560eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 561eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 562eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling LastLabel = LandingPad->EndLabels[P.RangeIndex]; 563eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling assert(BeginLabel && LastLabel && "Invalid landing pad!"); 564eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 565eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (LandingPad->LandingPadLabel) { 566eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // This try-range is for an invoke. 567a583c55864e83e470333b7be878280b10e175a6eBill Wendling CallSiteEntry Site = { 568a583c55864e83e470333b7be878280b10e175a6eBill Wendling BeginLabel, 569a583c55864e83e470333b7be878280b10e175a6eBill Wendling LastLabel, 570a583c55864e83e470333b7be878280b10e175a6eBill Wendling LandingPad->LandingPadLabel, 571a583c55864e83e470333b7be878280b10e175a6eBill Wendling FirstActions[P.PadIndex] 572a583c55864e83e470333b7be878280b10e175a6eBill Wendling }; 573eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 57433668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach // Try to merge with the previous call-site. SJLJ doesn't do this 57533668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach if (PreviousIsInvoke && 57633668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { 577eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling CallSiteEntry &Prev = CallSites.back(); 578eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { 579eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Extend the range of the previous entry. 580eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Prev.EndLabel = Site.EndLabel; 581eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling continue; 582eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 583eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 584eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 585eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Otherwise, create a new call-site. 586eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling CallSites.push_back(Site); 587eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PreviousIsInvoke = true; 588eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } else { 589eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Create a gap. 590eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling PreviousIsInvoke = false; 591eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 592eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 593eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 594eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 595eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // If some instruction between the previous try-range and the end of the 596eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // function may throw, create a call-site entry with no landing pad for the 597eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // region following the try-range. 5981b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (SawPotentiallyThrowing && 59933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { 600a583c55864e83e470333b7be878280b10e175a6eBill Wendling CallSiteEntry Site = { LastLabel, 0, 0, 0 }; 601eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling CallSites.push_back(Site); 602eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 603ade025c65c12503aba161a4fa399fd97414abaffBill Wendling} 604ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 6050dafca90761097230f02e655fdd541f59b888315Bill Wendling/// EmitExceptionTable - Emit landing pads and actions. 6060dafca90761097230f02e655fdd541f59b888315Bill Wendling/// 6070dafca90761097230f02e655fdd541f59b888315Bill Wendling/// The general organization of the table is complex, but the basic concepts are 6080dafca90761097230f02e655fdd541f59b888315Bill Wendling/// easy. First there is a header which describes the location and organization 6090dafca90761097230f02e655fdd541f59b888315Bill Wendling/// of the three components that follow. 610dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher/// 6110dafca90761097230f02e655fdd541f59b888315Bill Wendling/// 1. The landing pad site information describes the range of code covered by 6120dafca90761097230f02e655fdd541f59b888315Bill Wendling/// the try. In our case it's an accumulation of the ranges covered by the 6130dafca90761097230f02e655fdd541f59b888315Bill Wendling/// invokes in the try. There is also a reference to the landing pad that 6140dafca90761097230f02e655fdd541f59b888315Bill Wendling/// handles the exception once processed. Finally an index into the actions 6150dafca90761097230f02e655fdd541f59b888315Bill Wendling/// table. 616a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// 2. The action table, in our case, is composed of pairs of type IDs and next 6170dafca90761097230f02e655fdd541f59b888315Bill Wendling/// action offset. Starting with the action index from the landing pad 618a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// site, each type ID is checked for a match to the current exception. If 6190dafca90761097230f02e655fdd541f59b888315Bill Wendling/// it matches then the exception and type id are passed on to the landing 6200dafca90761097230f02e655fdd541f59b888315Bill Wendling/// pad. Otherwise the next action is looked up. This chain is terminated 62128275fdf02885587156da5cb20388b22d5b96557Bill Wendling/// with a next action of zero. If no type id is found then the frame is 6220dafca90761097230f02e655fdd541f59b888315Bill Wendling/// unwound and handling continues. 623a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// 3. Type ID table contains references to all the C++ typeinfo for all 62428275fdf02885587156da5cb20388b22d5b96557Bill Wendling/// catches in the function. This tables is reverse indexed base 1. 625ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::EmitExceptionTable() { 626ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); 627ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 628ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); 629ade025c65c12503aba161a4fa399fd97414abaffBill Wendling if (PadInfos.empty()) return; 630ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 631ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // Sort the landing pads in order of their type ids. This is used to fold 632ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // duplicate actions. 633ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVector<const LandingPadInfo *, 64> LandingPads; 634ade025c65c12503aba161a4fa399fd97414abaffBill Wendling LandingPads.reserve(PadInfos.size()); 635ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 636ade025c65c12503aba161a4fa399fd97414abaffBill Wendling for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 637ade025c65c12503aba161a4fa399fd97414abaffBill Wendling LandingPads.push_back(&PadInfos[i]); 638ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 639ade025c65c12503aba161a4fa399fd97414abaffBill Wendling std::sort(LandingPads.begin(), LandingPads.end(), PadLT); 640ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 641ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // Compute the actions table and gather the first action index for each 642ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // landing pad site. 643ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVector<ActionEntry, 32> Actions; 644ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVector<unsigned, 64> FirstActions; 64528275fdf02885587156da5cb20388b22d5b96557Bill Wendling unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, 64628275fdf02885587156da5cb20388b22d5b96557Bill Wendling FirstActions); 647ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 648ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // Invokes and nounwind calls have entries in PadMap (due to being bracketed 649ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // by try-range labels when lowered). Ordinary calls do not, so appropriate 65028275fdf02885587156da5cb20388b22d5b96557Bill Wendling // try-ranges for them need be deduced when using DWARF exception handling. 651ade025c65c12503aba161a4fa399fd97414abaffBill Wendling RangeMapType PadMap; 652ade025c65c12503aba161a4fa399fd97414abaffBill Wendling for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 653ade025c65c12503aba161a4fa399fd97414abaffBill Wendling const LandingPadInfo *LandingPad = LandingPads[i]; 654ade025c65c12503aba161a4fa399fd97414abaffBill Wendling for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 655ade025c65c12503aba161a4fa399fd97414abaffBill Wendling unsigned BeginLabel = LandingPad->BeginLabels[j]; 656ade025c65c12503aba161a4fa399fd97414abaffBill Wendling assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 657ade025c65c12503aba161a4fa399fd97414abaffBill Wendling PadRange P = { i, j }; 658ade025c65c12503aba161a4fa399fd97414abaffBill Wendling PadMap[BeginLabel] = P; 659ade025c65c12503aba161a4fa399fd97414abaffBill Wendling } 660ade025c65c12503aba161a4fa399fd97414abaffBill Wendling } 661ade025c65c12503aba161a4fa399fd97414abaffBill Wendling 662ade025c65c12503aba161a4fa399fd97414abaffBill Wendling // Compute the call-site table. 663ade025c65c12503aba161a4fa399fd97414abaffBill Wendling SmallVector<CallSiteEntry, 64> CallSites; 6648b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions); 665eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 666eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Final tallies. 667eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 668eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Call sites. 66940121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); 67040121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); 67140121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4); 672d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 673d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true; 67428275fdf02885587156da5cb20388b22d5b96557Bill Wendling unsigned SizeSites; 675d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling 676d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling if (IsSJLJ) 6778b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach SizeSites = 0; 678d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling else 6791b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach SizeSites = CallSites.size() * 6801b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach (SiteStartSize + SiteLengthSize + LandingPadSize); 681d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling 6821b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { 683af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action); 684d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling if (IsSJLJ) 685af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner SizeSites += MCAsmInfo::getULEB128Size(i); 6861b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 687d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling 688eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling // Type infos. 689d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); 690fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling unsigned TTypeFormat; 691a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned TypeFormatSize; 692eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 69343e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling if (!HaveTTData) { 69428275fdf02885587156da5cb20388b22d5b96557Bill Wendling // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say 69528275fdf02885587156da5cb20388b22d5b96557Bill Wendling // that we're omitting that bit. 696fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling TTypeFormat = dwarf::DW_EH_PE_omit; 697a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr); 69881c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner } else { 699ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // Okay, we have actual filters or typeinfos to emit. As such, we need to 700ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // pick a type encoding for them. We're about to emit a list of pointers to 701ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // typeinfo objects at the end of the LSDA. However, unless we're in static 702ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // mode, this reference will require a relocation by the dynamic linker. 70346b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner // 704ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // Because of this, we have a couple of options: 70528275fdf02885587156da5cb20388b22d5b96557Bill Wendling // 706ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 1) If we are in -static mode, we can always use an absolute reference 707ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // from the LSDA, because the static linker will resolve it. 70828275fdf02885587156da5cb20388b22d5b96557Bill Wendling // 709ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 2) Otherwise, if the LSDA section is writable, we can output the direct 710ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // reference to the typeinfo and allow the dynamic linker to relocate 711ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // it. Since it is in a writable section, the dynamic linker won't 712ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // have a problem. 71328275fdf02885587156da5cb20388b22d5b96557Bill Wendling // 714ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 715ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // we need to use some form of indirection. For example, on Darwin, 716ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // we can output a statically-relocatable reference to a dyld stub. The 717ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // offset to the stub is constant, but the contents are in a section 718ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // that is updated by the dynamic linker. This is easy enough, but we 719ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // need to tell the personality function of the unwinder to indirect 720ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // through the dyld stub. 721ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 72243e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling // FIXME: When (3) is actually implemented, we'll have to emit the stubs 723ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // somewhere. This predicate should be moved to a shared location that is 724ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // in target-independent code. 725ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner // 72643e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling if (LSDASection->getKind().isWriteable() || 72743e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling Asm->TM.getRelocationModel() == Reloc::Static) 72843e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling TTypeFormat = dwarf::DW_EH_PE_absptr; 72943e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling else 73043e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling TTypeFormat = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 73143e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling dwarf::DW_EH_PE_sdata4; 732a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling 733a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling TypeFormatSize = SizeOfEncodedValue(TTypeFormat); 734fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling } 735fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 736fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling // Begin the exception table. 737fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->OutStreamer.SwitchSection(LSDASection); 738fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EmitAlignment(2, 0, 0, false); 73943e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling 740fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling O << "GCC_except_table" << SubprogramCount << ":\n"; 741a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling 742a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // The type infos need to be aligned. GCC does this by inserting padding just 743a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // before the type infos. However, this changes the size of the exception 744a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // table, so you need to take this into account when you output the exception 745a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // table size. However, the size is output using a variable length encoding. 746a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // So by increasing the size by inserting padding, you may increase the number 747a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // of bytes used for writing the size. If it increases, say by one byte, then 748a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // you now need to output one less byte of padding to get the type infos 749a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // aligned. However this decreases the size of the exception table. This 750a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // changes the value you have to output for the exception table size. Due to 751a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // the variable length encoding, the number of bytes used for writing the 752a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // length may decrease. If so, you then have to increase the amount of 753a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // padding. And so on. If you look carefully at the GCC code you will see that 754a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // it indeed does this in a loop, going on and on until the values stabilize. 755a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // We chose another solution: don't output padding inside the table like GCC 756a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling // does, instead output it before the table. 757a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned SizeTypes = TypeInfos.size() * TypeFormatSize; 758a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned TyOffset = sizeof(int8_t) + // Call site format 759a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length 760a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling SizeSites + SizeActions + SizeTypes; 761a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned TotalSize = sizeof(int8_t) + // LPStart format 762a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling sizeof(int8_t) + // TType format 763a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling (HaveTTData ? 764a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling MCAsmInfo::getULEB128Size(TyOffset) : 0) + // TType base offset 765a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling TyOffset; 766a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling unsigned SizeAlign = (4 - TotalSize) & 3; 767a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling 768a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling for (unsigned i = 0; i != SizeAlign; ++i) { 769a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling Asm->EmitInt8(0); 770a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling Asm->EOL("Padding"); 771a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling } 772a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling 773fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling EmitLabel("exception", SubprogramCount); 774fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 775fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling if (IsSJLJ) { 776fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling SmallString<16> LSDAName; 777fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() << 778fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling "_LSDA_" << Asm->getFunctionNumber(); 779fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling O << LSDAName.str() << ":\n"; 780fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling } 781fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 782fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling // Emit the header. 783fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EmitInt8(dwarf::DW_EH_PE_omit); 784fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit); 785fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 786fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EmitInt8(TTypeFormat); 787fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling Asm->EOL("@TType format", TTypeFormat); 788fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling 789fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling if (HaveTTData) { 790a2f6449a57fcce73beacc30158aafe39b5349947Bill Wendling Asm->EmitULEB128Bytes(TyOffset); 791a583c55864e83e470333b7be878280b10e175a6eBill Wendling Asm->EOL("@TType base offset"); 7921b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 793b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling 79428275fdf02885587156da5cb20388b22d5b96557Bill Wendling // SjLj Exception handling 795d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling if (IsSJLJ) { 796639217cb6a3948800c71c39503f8def4873a535eBill Wendling Asm->EmitInt8(dwarf::DW_EH_PE_udata4); 7970734d35044c304dd072f20e49840bcbea427db9cBill Wendling Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4); 7981b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitULEB128Bytes(SizeSites); 799a583c55864e83e470333b7be878280b10e175a6eBill Wendling Asm->EOL("Call site table length"); 8001b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach 8011b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach // Emit the landing pad site information. 8028b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach unsigned idx = 0; 8038b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach for (SmallVectorImpl<CallSiteEntry>::const_iterator 8048b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 8058b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach const CallSiteEntry &S = *I; 806a583c55864e83e470333b7be878280b10e175a6eBill Wendling 807a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the landing pad, counted in 16-byte bundles relative to the 808a583c55864e83e470333b7be878280b10e175a6eBill Wendling // @LPStart address. 8098b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach Asm->EmitULEB128Bytes(idx); 8101b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Landing pad"); 811a583c55864e83e470333b7be878280b10e175a6eBill Wendling 812a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the first associated action record, relative to the start of 813a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the action table. This value is biased by 1 (1 indicates the start of 814a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the action table), and 0 indicates that there are no actions. 8151b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitULEB128Bytes(S.Action); 8161b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Action"); 8171b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 8181b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } else { 8191b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach // DWARF Exception handling 82033adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf); 8211b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach 822a583c55864e83e470333b7be878280b10e175a6eBill Wendling // The call-site table is a list of all call sites that may throw an 823a583c55864e83e470333b7be878280b10e175a6eBill Wendling // exception (including C++ 'throw' statements) in the procedure 824a583c55864e83e470333b7be878280b10e175a6eBill Wendling // fragment. It immediately follows the LSDA header. Each entry indicates, 825a583c55864e83e470333b7be878280b10e175a6eBill Wendling // for a given call, the first corresponding action record and corresponding 826a583c55864e83e470333b7be878280b10e175a6eBill Wendling // landing pad. 827a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 828a583c55864e83e470333b7be878280b10e175a6eBill Wendling // The table begins with the number of bytes, stored as an LEB128 829a583c55864e83e470333b7be878280b10e175a6eBill Wendling // compressed, unsigned integer. The records immediately follow the record 830a583c55864e83e470333b7be878280b10e175a6eBill Wendling // count. They are sorted in increasing call-site address. Each record 831a583c55864e83e470333b7be878280b10e175a6eBill Wendling // indicates: 832a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 833a583c55864e83e470333b7be878280b10e175a6eBill Wendling // * The position of the call-site. 834a583c55864e83e470333b7be878280b10e175a6eBill Wendling // * The position of the landing pad. 835a583c55864e83e470333b7be878280b10e175a6eBill Wendling // * The first action record for that call site. 836a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 837a583c55864e83e470333b7be878280b10e175a6eBill Wendling // A missing entry in the call-site table indicates that a call is not 83828275fdf02885587156da5cb20388b22d5b96557Bill Wendling // supposed to throw. 839a583c55864e83e470333b7be878280b10e175a6eBill Wendling 840a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Emit the landing pad call site table. 841639217cb6a3948800c71c39503f8def4873a535eBill Wendling Asm->EmitInt8(dwarf::DW_EH_PE_udata4); 8420734d35044c304dd072f20e49840bcbea427db9cBill Wendling Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4); 8431b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitULEB128Bytes(SizeSites); 844a583c55864e83e470333b7be878280b10e175a6eBill Wendling Asm->EOL("Call site table size"); 845eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8461b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach for (SmallVectorImpl<CallSiteEntry>::const_iterator 8475cff487665097d971067cbede1598e249f673182Bill Wendling I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { 8481b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach const CallSiteEntry &S = *I; 8491b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach const char *BeginTag; 8501b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach unsigned BeginNumber; 851eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8521b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (!S.BeginLabel) { 8531b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach BeginTag = "eh_func_begin"; 8541b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach BeginNumber = SubprogramCount; 8551b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } else { 8561b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach BeginTag = "label"; 8571b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach BeginNumber = S.BeginLabel; 8581b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 859eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 860a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the call site relative to the previous call site, counted in 861a583c55864e83e470333b7be878280b10e175a6eBill Wendling // number of 16-byte bundles. The first call site is counted relative to 862a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the start of the procedure fragment. 8631b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount, 8641b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach true, true); 8651b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Region start"); 866eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8671b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (!S.EndLabel) 8681b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber, 8691b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach true); 8701b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach else 8711b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true); 872eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8731b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Region length"); 874eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 875a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the landing pad, counted in 16-byte bundles relative to the 876a583c55864e83e470333b7be878280b10e175a6eBill Wendling // @LPStart address. 8771b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach if (!S.PadLabel) 8781b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitInt32(0); 8791b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach else 8801b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount, 8811b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach true, true); 882eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 8831b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Landing pad"); 884eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 885a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Offset of the first associated action record, relative to the start of 886a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the action table. This value is biased by 1 (1 indicates the start of 887a583c55864e83e470333b7be878280b10e175a6eBill Wendling // the action table), and 0 indicates that there are no actions. 8881b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EmitULEB128Bytes(S.Action); 8891b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach Asm->EOL("Action"); 8901b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach } 891eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 892eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 893a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Emit the Action Table. 8945cff487665097d971067cbede1598e249f673182Bill Wendling for (SmallVectorImpl<ActionEntry>::const_iterator 8955cff487665097d971067cbede1598e249f673182Bill Wendling I = Actions.begin(), E = Actions.end(); I != E; ++I) { 8965cff487665097d971067cbede1598e249f673182Bill Wendling const ActionEntry &Action = *I; 897a583c55864e83e470333b7be878280b10e175a6eBill Wendling 898a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Type Filter 899a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 900a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Used by the runtime to match the type of the thrown exception to the 901a583c55864e83e470333b7be878280b10e175a6eBill Wendling // type of the catch clauses or the types in the exception specification. 902a583c55864e83e470333b7be878280b10e175a6eBill Wendling 903eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitSLEB128Bytes(Action.ValueForTypeID); 904eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("TypeInfo index"); 905a583c55864e83e470333b7be878280b10e175a6eBill Wendling 906a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Action Record 907a583c55864e83e470333b7be878280b10e175a6eBill Wendling // 908a583c55864e83e470333b7be878280b10e175a6eBill Wendling // Self-relative signed displacement in bytes of the next action record, 909a583c55864e83e470333b7be878280b10e175a6eBill Wendling // or 0 if there is no next action record. 910a583c55864e83e470333b7be878280b10e175a6eBill Wendling 911eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitSLEB128Bytes(Action.NextAction); 912eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("Next action"); 913eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 914eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 91548dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling // Emit the Catch TypeInfos. 9165cff487665097d971067cbede1598e249f673182Bill Wendling for (std::vector<GlobalVariable *>::const_reverse_iterator 917711bb91574db242ba34a86caea719819b0ecef43Bill Wendling I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { 918711bb91574db242ba34a86caea719819b0ecef43Bill Wendling const GlobalVariable *GV = *I; 919711bb91574db242ba34a86caea719819b0ecef43Bill Wendling PrintRelDirective(); 920296ab7e5445f0402d0aae3814c38f964d6f547fbBill Wendling 921711bb91574db242ba34a86caea719819b0ecef43Bill Wendling if (GV) { 922711bb91574db242ba34a86caea719819b0ecef43Bill Wendling O << Asm->Mang->getMangledName(GV); 923eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } else { 924049e98d641f90e90e8312d76cbf4c68908fb9d1dBill Wendling O << "0x0"; 925eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 926eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 927eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EOL("TypeInfo"); 928eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 929eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 93048dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling // Emit the Exception Specifications. 9315cff487665097d971067cbede1598e249f673182Bill Wendling for (std::vector<unsigned>::const_iterator 9325cff487665097d971067cbede1598e249f673182Bill Wendling I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 9335cff487665097d971067cbede1598e249f673182Bill Wendling unsigned TypeID = *I; 934eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitULEB128Bytes(TypeID); 93548dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling if (TypeID != 0) 93648dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling Asm->EOL("Exception specification"); 93748dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling else 93848dc29ef911a223a52b099604d0ccb499ecbf703Bill Wendling Asm->EOL(); 939eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling } 940eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 941eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling Asm->EmitAlignment(2, 0, 0, false); 942eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 943eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 944eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndModule - Emit all exception information that should come after the 945eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// content. 946eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndModule() { 94733adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf) 9481b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach return; 949b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling 95052783c63a13d384582997684384553e6545852d3Bill Wendling if (!shouldEmitMovesModule && !shouldEmitTableModule) 95152783c63a13d384582997684384553e6545852d3Bill Wendling return; 95252783c63a13d384582997684384553e6545852d3Bill Wendling 953eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (TimePassesIsEnabled) 954eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->startTimer(); 955eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 95652783c63a13d384582997684384553e6545852d3Bill Wendling const std::vector<Function *> Personalities = MMI->getPersonalities(); 957b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling 95828275fdf02885587156da5cb20388b22d5b96557Bill Wendling for (unsigned I = 0, E = Personalities.size(); I < E; ++I) 95928275fdf02885587156da5cb20388b22d5b96557Bill Wendling EmitCIE(Personalities[I], I); 960eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 96152783c63a13d384582997684384553e6545852d3Bill Wendling for (std::vector<FunctionEHFrameInfo>::iterator 96252783c63a13d384582997684384553e6545852d3Bill Wendling I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I) 96352783c63a13d384582997684384553e6545852d3Bill Wendling EmitFDE(*I); 964eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 965eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (TimePassesIsEnabled) 966eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->stopTimer(); 967eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 968eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 96928275fdf02885587156da5cb20388b22d5b96557Bill Wendling/// BeginFunction - Gather pre-function exception information. Assumes it's 97028275fdf02885587156da5cb20388b22d5b96557Bill Wendling/// being emitted immediately after the function entry point. 971eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::BeginFunction(MachineFunction *MF) { 97273c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling if (!MMI || !MAI->doesSupportExceptionHandling()) return; 97373c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling 974eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (TimePassesIsEnabled) 975eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->startTimer(); 976eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 977eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling this->MF = MF; 978eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling shouldEmitTable = shouldEmitMoves = false; 979eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 98073c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling // Map all labels and get rid of any dead landing pads. 98173c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling MMI->TidyLandingPads(); 982eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 98373c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling // If any landing pads survive, we need an EH table. 98473c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling if (!MMI->getLandingPads().empty()) 98573c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling shouldEmitTable = true; 986eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 98773c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling // See if we need frame move info. 98873c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory) 98973c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling shouldEmitMoves = true; 990eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 99173c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling if (shouldEmitMoves || shouldEmitTable) 99273c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling // Assumes in correct section after the entry point. 99373c5a6172508e957d0b1a46170cb070dce6cac4fBill Wendling EmitLabel("eh_func_begin", ++SubprogramCount); 994eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 995eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling shouldEmitTableModule |= shouldEmitTable; 996eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling shouldEmitMovesModule |= shouldEmitMoves; 997eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 998eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling if (TimePassesIsEnabled) 999eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->stopTimer(); 1000eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 1001eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 1002eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndFunction - Gather and emit post-function exception information. 1003eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// 1004eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndFunction() { 10057b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling if (!shouldEmitMoves && !shouldEmitTable) return; 10067b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling 1007dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher if (TimePassesIsEnabled) 1008eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->startTimer(); 1009eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 10107b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling EmitLabel("eh_func_end", SubprogramCount); 10117b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling EmitExceptionTable(); 10127b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling 101325d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner std::string FunctionEHName = 101425d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner Asm->Mang->getMangledName(MF->getFunction(), ".eh", 101525d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner Asm->MAI->is_EHSymbolPrivate()); 101625d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner 10177b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling // Save EH frame information 101825d812bd7d1f58f2ba1b598b1425a2e146e27381Chris Lattner EHFrames.push_back(FunctionEHFrameInfo(FunctionEHName, SubprogramCount, 10197b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling MMI->getPersonalityIndex(), 10207b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling MF->getFrameInfo()->hasCalls(), 10217b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling !MMI->getLandingPads().empty(), 10227b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling MMI->getFrameMoves(), 10237b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling MF->getFunction())); 1024eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling 102552783c63a13d384582997684384553e6545852d3Bill Wendling // Record if this personality index uses a landing pad. 102652783c63a13d384582997684384553e6545852d3Bill Wendling UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty(); 102752783c63a13d384582997684384553e6545852d3Bill Wendling 1028dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher if (TimePassesIsEnabled) 1029eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling ExceptionTimer->stopTimer(); 1030eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling} 1031