blob: 290be81c6baabef6155607701111e0406aed5b72 [file] [log] [blame]
Adrian McCarthydb2736d2018-01-09 23:49:30 +00001//===-- CodeGen/AsmPrinter/WinCFGuard.cpp - Control Flow Guard Impl ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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-exception
Adrian McCarthydb2736d2018-01-09 23:49:30 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing Win64 exception info into asm files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "WinCFGuard.h"
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineModuleInfo.h"
17#include "llvm/CodeGen/MachineOperand.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/Metadata.h"
20#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCObjectFileInfo.h"
22#include "llvm/MC/MCStreamer.h"
23
24#include <vector>
25
26using namespace llvm;
27
28WinCFGuard::WinCFGuard(AsmPrinter *A) : AsmPrinterHandler(), Asm(A) {}
29
30WinCFGuard::~WinCFGuard() {}
31
32void WinCFGuard::endModule() {
33 const Module *M = Asm->MMI->getModule();
34 std::vector<const Function *> Functions;
35 for (const Function &F : *M)
36 if (F.hasAddressTaken())
37 Functions.push_back(&F);
38 if (Functions.empty())
39 return;
40 auto &OS = *Asm->OutStreamer;
41 OS.SwitchSection(Asm->OutContext.getObjectFileInfo()->getGFIDsSection());
42 for (const Function *F : Functions)
43 OS.EmitCOFFSymbolIndex(Asm->getSymbol(F));
44}