Adrian McCarthy | db2736d | 2018-01-09 23:49:30 +0000 | [diff] [blame] | 1 | //===-- CodeGen/AsmPrinter/WinCFGuard.cpp - Control Flow Guard Impl ------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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-exception |
Adrian McCarthy | db2736d | 2018-01-09 23:49:30 +0000 | [diff] [blame] | 6 | // |
| 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 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | WinCFGuard::WinCFGuard(AsmPrinter *A) : AsmPrinterHandler(), Asm(A) {} |
| 29 | |
| 30 | WinCFGuard::~WinCFGuard() {} |
| 31 | |
| 32 | void 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 | } |