2011-01-14 22:57:53 +01:00
|
|
|
//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for writing DWARF exception info into asm files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DwarfException.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2011-01-14 22:57:53 +01:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-07 22:19:40 +01:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-01-14 22:57:53 +01:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/MC/MachineLocation.h"
|
|
|
|
#include "llvm/Support/Dwarf.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2011-01-14 22:57:53 +01:00
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-03-09 19:29:12 +01:00
|
|
|
DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
|
|
|
|
: EHStreamer(A), shouldEmitCFI(false) {}
|
|
|
|
|
|
|
|
void DwarfCFIExceptionBase::markFunctionEnd() {
|
|
|
|
if (shouldEmitCFI)
|
|
|
|
Asm->OutStreamer.EmitCFIEndProc();
|
|
|
|
|
|
|
|
if (MMI->getLandingPads().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Map all labels and get rid of any dead landing pads.
|
|
|
|
MMI->TidyLandingPads();
|
|
|
|
}
|
|
|
|
|
2011-01-14 22:57:53 +01:00
|
|
|
DwarfCFIException::DwarfCFIException(AsmPrinter *A)
|
2015-03-09 19:29:12 +01:00
|
|
|
: DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
|
|
|
|
shouldEmitLSDA(false), shouldEmitMoves(false),
|
|
|
|
moveTypeModule(AsmPrinter::CFI_M_None) {}
|
2011-01-14 22:57:53 +01:00
|
|
|
|
|
|
|
DwarfCFIException::~DwarfCFIException() {}
|
|
|
|
|
2013-11-26 14:34:55 +01:00
|
|
|
/// endModule - Emit all exception information that should come after the
|
2011-01-14 22:57:53 +01:00
|
|
|
/// content.
|
2013-11-26 14:34:55 +01:00
|
|
|
void DwarfCFIException::endModule() {
|
2011-05-10 20:39:09 +02:00
|
|
|
if (moveTypeModule == AsmPrinter::CFI_M_Debug)
|
|
|
|
Asm->OutStreamer.EmitCFISections(false, true);
|
|
|
|
|
2015-01-23 19:49:01 +01:00
|
|
|
// SjLj uses this pass and it doesn't need this info.
|
|
|
|
if (!Asm->MAI->usesCFIForEH())
|
2011-01-14 22:57:53 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
|
2011-04-29 17:09:53 +02:00
|
|
|
|
2011-01-14 22:57:53 +01:00
|
|
|
unsigned PerEncoding = TLOF.getPersonalityEncoding();
|
|
|
|
|
2014-05-30 18:48:56 +02:00
|
|
|
if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
|
2011-04-15 17:11:06 +02:00
|
|
|
return;
|
|
|
|
|
2011-01-14 22:57:53 +01:00
|
|
|
// Emit references to all used personality functions
|
|
|
|
const std::vector<const Function*> &Personalities = MMI->getPersonalities();
|
|
|
|
for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
|
2011-04-29 16:48:51 +02:00
|
|
|
if (!Personalities[i])
|
|
|
|
continue;
|
2013-10-29 18:07:16 +01:00
|
|
|
MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
|
2011-04-16 05:51:21 +02:00
|
|
|
TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
|
2011-01-14 22:57:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-26 14:34:55 +01:00
|
|
|
/// beginFunction - Gather pre-function exception information. Assumes it's
|
2011-01-14 22:57:53 +01:00
|
|
|
/// being emitted immediately after the function entry point.
|
2013-11-26 14:34:55 +01:00
|
|
|
void DwarfCFIException::beginFunction(const MachineFunction *MF) {
|
2011-04-29 16:48:51 +02:00
|
|
|
shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
|
2011-01-14 22:57:53 +01:00
|
|
|
|
|
|
|
// If any landing pads survive, we need an EH table.
|
2011-04-29 16:48:51 +02:00
|
|
|
bool hasLandingPads = !MMI->getLandingPads().empty();
|
2011-01-14 22:57:53 +01:00
|
|
|
|
|
|
|
// See if we need frame move info.
|
2011-05-10 20:39:09 +02:00
|
|
|
AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
|
|
|
|
if (MoveType == AsmPrinter::CFI_M_EH ||
|
|
|
|
(MoveType == AsmPrinter::CFI_M_Debug &&
|
|
|
|
moveTypeModule == AsmPrinter::CFI_M_None))
|
|
|
|
moveTypeModule = MoveType;
|
|
|
|
|
|
|
|
shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
|
2011-01-14 22:57:53 +01:00
|
|
|
|
2011-04-29 16:48:51 +02:00
|
|
|
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
|
|
|
|
unsigned PerEncoding = TLOF.getPersonalityEncoding();
|
2015-01-14 23:47:54 +01:00
|
|
|
const Function *Per = MMI->getPersonality();
|
2011-01-14 22:57:53 +01:00
|
|
|
|
2011-04-29 16:48:51 +02:00
|
|
|
shouldEmitPersonality = hasLandingPads &&
|
|
|
|
PerEncoding != dwarf::DW_EH_PE_omit && Per;
|
2011-01-14 22:57:53 +01:00
|
|
|
|
2011-04-29 16:48:51 +02:00
|
|
|
unsigned LSDAEncoding = TLOF.getLSDAEncoding();
|
|
|
|
shouldEmitLSDA = shouldEmitPersonality &&
|
|
|
|
LSDAEncoding != dwarf::DW_EH_PE_omit;
|
2011-01-14 22:57:53 +01:00
|
|
|
|
2015-03-09 19:29:12 +01:00
|
|
|
shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
|
|
|
|
if (!shouldEmitCFI)
|
2011-01-14 22:57:53 +01:00
|
|
|
return;
|
|
|
|
|
2014-11-03 13:19:03 +01:00
|
|
|
Asm->OutStreamer.EmitCFIStartProc(/*IsSimple=*/false);
|
2011-01-14 22:57:53 +01:00
|
|
|
|
|
|
|
// Indicate personality routine, if any.
|
2011-04-29 16:48:51 +02:00
|
|
|
if (!shouldEmitPersonality)
|
2011-04-15 17:11:06 +02:00
|
|
|
return;
|
|
|
|
|
2014-02-19 18:23:20 +01:00
|
|
|
const MCSymbol *Sym =
|
|
|
|
TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
|
2011-04-15 17:11:06 +02:00
|
|
|
Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
|
2011-04-29 16:48:51 +02:00
|
|
|
|
|
|
|
// Provide LSDA information.
|
|
|
|
if (!shouldEmitLSDA)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
|
|
|
|
Asm->getFunctionNumber()),
|
|
|
|
LSDAEncoding);
|
2011-01-14 22:57:53 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 14:34:55 +01:00
|
|
|
/// endFunction - Gather and emit post-function exception information.
|
2011-01-14 22:57:53 +01:00
|
|
|
///
|
2013-12-03 16:10:23 +01:00
|
|
|
void DwarfCFIException::endFunction(const MachineFunction *) {
|
2012-01-17 05:19:20 +01:00
|
|
|
if (!shouldEmitPersonality)
|
|
|
|
return;
|
|
|
|
|
2014-06-11 03:19:03 +02:00
|
|
|
emitExceptionTable();
|
2011-01-14 22:57:53 +01:00
|
|
|
}
|