WinCFGuard.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //===-- CodeGen/AsmPrinter/WinCFGuard.cpp - Control Flow Guard Impl ------===//
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains support for writing Win64 exception info into asm files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "WinCFGuard.h"
  13. #include "llvm/CodeGen/AsmPrinter.h"
  14. #include "llvm/CodeGen/MachineFunction.h"
  15. #include "llvm/CodeGen/MachineModuleInfo.h"
  16. #include "llvm/CodeGen/MachineOperand.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/Metadata.h"
  19. #include "llvm/MC/MCAsmInfo.h"
  20. #include "llvm/MC/MCObjectFileInfo.h"
  21. #include "llvm/MC/MCStreamer.h"
  22. #include <vector>
  23. using namespace llvm;
  24. WinCFGuard::WinCFGuard(AsmPrinter *A) : AsmPrinterHandler(), Asm(A) {}
  25. WinCFGuard::~WinCFGuard() {}
  26. void WinCFGuard::endModule() {
  27. const Module *M = Asm->MMI->getModule();
  28. std::vector<const Function *> Functions;
  29. for (const Function &F : *M)
  30. if (F.hasAddressTaken())
  31. Functions.push_back(&F);
  32. if (Functions.empty())
  33. return;
  34. auto &OS = *Asm->OutStreamer;
  35. OS.SwitchSection(Asm->OutContext.getObjectFileInfo()->getGFIDsSection());
  36. for (const Function *F : Functions)
  37. OS.EmitCOFFSymbolIndex(Asm->getSymbol(F));
  38. }