2011-01-15 05:57:53 +08: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-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
2011-01-15 05:57:53 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetLoweringObjectFile.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-08 05:19:40 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-01-15 05:57:53 +08: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-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MachineLocation.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2011-01-15 05:57:53 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-03-10 02:29:12 +08:00
|
|
|
DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
|
2017-01-03 02:05:27 +08:00
|
|
|
: EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
|
2015-03-10 02:29:12 +08:00
|
|
|
|
|
|
|
void DwarfCFIExceptionBase::markFunctionEnd() {
|
2016-02-27 04:30:37 +08:00
|
|
|
endFragment();
|
2015-03-10 02:29:12 +08:00
|
|
|
|
|
|
|
// Map all labels and get rid of any dead landing pads.
|
2016-12-02 03:32:15 +08:00
|
|
|
if (!Asm->MF->getLandingPads().empty()) {
|
|
|
|
MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
|
|
|
|
NonConstMF->tidyLandingPads();
|
|
|
|
}
|
2015-03-10 02:29:12 +08:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:30:37 +08:00
|
|
|
void DwarfCFIExceptionBase::endFragment() {
|
|
|
|
if (shouldEmitCFI)
|
|
|
|
Asm->OutStreamer->EmitCFIEndProc();
|
|
|
|
}
|
|
|
|
|
2011-01-15 05:57:53 +08:00
|
|
|
DwarfCFIException::DwarfCFIException(AsmPrinter *A)
|
2015-03-10 02:29:12 +08:00
|
|
|
: DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
|
2016-02-27 04:49:07 +08:00
|
|
|
forceEmitPersonality(false), shouldEmitLSDA(false),
|
2017-01-03 02:05:27 +08:00
|
|
|
shouldEmitMoves(false) {}
|
2011-01-15 05:57:53 +08:00
|
|
|
|
|
|
|
DwarfCFIException::~DwarfCFIException() {}
|
|
|
|
|
2013-11-26 21:34:55 +08:00
|
|
|
/// endModule - Emit all exception information that should come after the
|
2011-01-15 05:57:53 +08:00
|
|
|
/// content.
|
2013-11-26 21:34:55 +08:00
|
|
|
void DwarfCFIException::endModule() {
|
2015-01-24 02:49:01 +08:00
|
|
|
// SjLj uses this pass and it doesn't need this info.
|
|
|
|
if (!Asm->MAI->usesCFIForEH())
|
2011-01-15 05:57:53 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
|
2011-04-29 23:09:53 +08:00
|
|
|
|
2011-01-15 05:57:53 +08:00
|
|
|
unsigned PerEncoding = TLOF.getPersonalityEncoding();
|
|
|
|
|
2014-05-31 00:48:56 +08:00
|
|
|
if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
|
2011-04-15 23:11:06 +08:00
|
|
|
return;
|
|
|
|
|
2011-01-15 05:57:53 +08:00
|
|
|
// Emit references to all used personality functions
|
2015-09-01 04:02:16 +08:00
|
|
|
for (const Function *Personality : MMI->getPersonalities()) {
|
|
|
|
if (!Personality)
|
2011-04-29 22:48:51 +08:00
|
|
|
continue;
|
2015-09-01 04:02:16 +08:00
|
|
|
MCSymbol *Sym = Asm->getSymbol(Personality);
|
2015-07-16 14:04:17 +08:00
|
|
|
TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
|
2011-01-15 05:57:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:30:37 +08:00
|
|
|
static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
|
|
|
|
return Asm->getCurExceptionSym();
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:34:55 +08:00
|
|
|
void DwarfCFIException::beginFunction(const MachineFunction *MF) {
|
2011-04-29 22:48:51 +08:00
|
|
|
shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
|
2017-12-16 06:22:58 +08:00
|
|
|
const Function &F = MF->getFunction();
|
2011-01-15 05:57:53 +08:00
|
|
|
|
|
|
|
// If any landing pads survive, we need an EH table.
|
2016-12-02 03:32:15 +08:00
|
|
|
bool hasLandingPads = !MF->getLandingPads().empty();
|
2011-01-15 05:57:53 +08:00
|
|
|
|
|
|
|
// See if we need frame move info.
|
2011-05-11 02:39:09 +08:00
|
|
|
AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
|
|
|
|
|
|
|
|
shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
|
2011-01-15 05:57:53 +08:00
|
|
|
|
2011-04-29 22:48:51 +08:00
|
|
|
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
|
|
|
|
unsigned PerEncoding = TLOF.getPersonalityEncoding();
|
2015-07-15 03:22:51 +08:00
|
|
|
const Function *Per = nullptr;
|
2017-12-16 06:22:58 +08:00
|
|
|
if (F.hasPersonalityFn())
|
|
|
|
Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
|
2015-07-15 03:22:51 +08:00
|
|
|
|
|
|
|
// Emit a personality function even when there are no landing pads
|
2016-02-27 04:30:37 +08:00
|
|
|
forceEmitPersonality =
|
2015-07-15 03:22:51 +08:00
|
|
|
// ...if a personality function is explicitly specified
|
2017-12-16 06:22:58 +08:00
|
|
|
F.hasPersonalityFn() &&
|
2015-07-15 03:22:51 +08:00
|
|
|
// ... and it's not known to be a noop in the absence of invokes
|
|
|
|
!isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
|
|
|
|
// ... and we're not explicitly asked not to emit it
|
2017-12-16 06:22:58 +08:00
|
|
|
F.needsUnwindTableEntry();
|
2015-07-15 03:22:51 +08:00
|
|
|
|
|
|
|
shouldEmitPersonality =
|
|
|
|
(forceEmitPersonality ||
|
|
|
|
(hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
|
|
|
|
Per;
|
2011-01-15 05:57:53 +08:00
|
|
|
|
2011-04-29 22:48:51 +08:00
|
|
|
unsigned LSDAEncoding = TLOF.getLSDAEncoding();
|
|
|
|
shouldEmitLSDA = shouldEmitPersonality &&
|
|
|
|
LSDAEncoding != dwarf::DW_EH_PE_omit;
|
2011-01-15 05:57:53 +08:00
|
|
|
|
2016-07-16 05:10:29 +08:00
|
|
|
shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() &&
|
|
|
|
(shouldEmitPersonality || shouldEmitMoves);
|
2016-02-27 04:30:37 +08:00
|
|
|
beginFragment(&*MF->begin(), getExceptionSym);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
|
|
|
|
ExceptionSymbolProvider ESP) {
|
2015-03-10 02:29:12 +08:00
|
|
|
if (!shouldEmitCFI)
|
2011-01-15 05:57:53 +08:00
|
|
|
return;
|
|
|
|
|
2017-01-03 02:05:27 +08:00
|
|
|
if (!hasEmittedCFISections) {
|
2017-01-06 04:55:28 +08:00
|
|
|
if (Asm->needsOnlyDebugCFIMoves())
|
2017-01-03 02:05:27 +08:00
|
|
|
Asm->OutStreamer->EmitCFISections(false, true);
|
|
|
|
hasEmittedCFISections = true;
|
|
|
|
}
|
|
|
|
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
|
2011-01-15 05:57:53 +08:00
|
|
|
|
|
|
|
// Indicate personality routine, if any.
|
2011-04-29 22:48:51 +08:00
|
|
|
if (!shouldEmitPersonality)
|
2011-04-15 23:11:06 +08:00
|
|
|
return;
|
|
|
|
|
2017-12-16 06:22:58 +08:00
|
|
|
auto &F = MBB->getParent()->getFunction();
|
|
|
|
auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
|
2016-02-27 04:30:37 +08:00
|
|
|
assert(P && "Expected personality function");
|
|
|
|
|
2015-07-15 03:22:51 +08:00
|
|
|
// If we are forced to emit this personality, make sure to record
|
|
|
|
// it because it might not appear in any landingpad
|
|
|
|
if (forceEmitPersonality)
|
2016-02-27 04:30:37 +08:00
|
|
|
MMI->addPersonality(P);
|
2015-07-15 03:22:51 +08:00
|
|
|
|
2016-02-27 04:30:37 +08:00
|
|
|
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
|
|
|
|
unsigned PerEncoding = TLOF.getPersonalityEncoding();
|
2016-09-16 15:33:15 +08:00
|
|
|
const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
|
2011-04-29 22:48:51 +08:00
|
|
|
|
|
|
|
// Provide LSDA information.
|
2016-02-27 04:30:37 +08:00
|
|
|
if (shouldEmitLSDA)
|
|
|
|
Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
|
2011-01-15 05:57:53 +08:00
|
|
|
}
|
|
|
|
|
2013-11-26 21:34:55 +08:00
|
|
|
/// endFunction - Gather and emit post-function exception information.
|
2011-01-15 05:57:53 +08:00
|
|
|
///
|
2016-12-02 03:32:15 +08:00
|
|
|
void DwarfCFIException::endFunction(const MachineFunction *MF) {
|
2012-01-17 12:19:20 +08:00
|
|
|
if (!shouldEmitPersonality)
|
|
|
|
return;
|
|
|
|
|
2014-06-11 09:19:03 +08:00
|
|
|
emitExceptionTable();
|
2011-01-15 05:57:53 +08:00
|
|
|
}
|