84 lines · cpp
1//===-- EHContGuardTargets.cpp - EH continuation target symbols -*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file contains a machine function pass to insert a symbol before each11/// valid target where the unwinder in Windows may continue exectution after an12/// exception is thrown and store this in the MachineFunction's EHContTargets13/// vector. This will be used to emit the table of valid targets used by Windows14/// EH Continuation Guard.15///16//===----------------------------------------------------------------------===//17 18#include "llvm/ADT/Statistic.h"19#include "llvm/CodeGen/MachineBasicBlock.h"20#include "llvm/CodeGen/MachineFunctionPass.h"21#include "llvm/CodeGen/MachineModuleInfo.h"22#include "llvm/CodeGen/Passes.h"23#include "llvm/IR/Module.h"24#include "llvm/InitializePasses.h"25 26using namespace llvm;27 28#define DEBUG_TYPE "ehcontguard-catchret"29 30STATISTIC(EHContGuardTargetsFound, "Number of EHCont Guard targets");31 32namespace {33 34/// MachineFunction pass to insert a symbol before each valid catchret target35/// and store these in the MachineFunction's CatchRetTargets vector.36class EHContGuardTargets : public MachineFunctionPass {37public:38 static char ID;39 40 EHContGuardTargets() : MachineFunctionPass(ID) {41 initializeEHContGuardTargetsPass(*PassRegistry::getPassRegistry());42 }43 44 StringRef getPassName() const override {45 return "EH Cont Guard catchret targets";46 }47 48 bool runOnMachineFunction(MachineFunction &MF) override;49};50 51} // end anonymous namespace52 53char EHContGuardTargets::ID = 0;54 55INITIALIZE_PASS(EHContGuardTargets, "EHContGuardTargets",56 "Insert symbols at valid targets for /guard:ehcont", false,57 false)58FunctionPass *llvm::createEHContGuardTargetsPass() {59 return new EHContGuardTargets();60}61 62bool EHContGuardTargets::runOnMachineFunction(MachineFunction &MF) {63 64 // Skip modules for which the ehcontguard flag is not set.65 if (!MF.getFunction().getParent()->getModuleFlag("ehcontguard"))66 return false;67 68 // Skip functions that do not have targets69 if (!MF.hasEHContTarget())70 return false;71 72 bool Result = false;73 74 for (MachineBasicBlock &MBB : MF) {75 if (MBB.isEHContTarget()) {76 MF.addEHContTarget(MBB.getEHContSymbol());77 EHContGuardTargetsFound++;78 Result = true;79 }80 }81 82 return Result;83}84