2017-09-21 04:38:14 +08:00
|
|
|
//===-- llvm-cfi-verify.cpp - CFI Verification tool for LLVM --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This tool verifies Control Flow Integrity (CFI) instrumentation by static
|
|
|
|
// binary anaylsis. See the design document in /docs/CFIVerify.rst for more
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
// This tool is currently incomplete. It currently only does disassembly for
|
|
|
|
// object files, and searches through the code for indirect control flow
|
|
|
|
// instructions, printing them once found.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-10-11 04:59:08 +08:00
|
|
|
#include "FileAnalysis.h"
|
|
|
|
|
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2017-09-21 04:38:14 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-10-11 04:59:08 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-09-21 04:38:14 +08:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
2017-10-11 04:59:08 +08:00
|
|
|
using namespace llvm::cfi_verify;
|
2017-09-21 04:38:14 +08:00
|
|
|
|
|
|
|
cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
|
|
|
|
cl::Required);
|
|
|
|
|
2017-10-11 04:59:08 +08:00
|
|
|
ExitOnError ExitOnErr;
|
2017-09-21 04:38:14 +08:00
|
|
|
|
2017-10-11 04:59:08 +08:00
|
|
|
void printIndirectCFInstructions(const FileAnalysis &Verifier) {
|
|
|
|
for (uint64_t Address : Verifier.getIndirectInstructions()) {
|
|
|
|
const auto &InstrMeta = Verifier.getInstructionOrDie(Address);
|
|
|
|
outs() << format_hex(Address, 2) << " |"
|
|
|
|
<< Verifier.getMCInstrInfo()->getName(
|
|
|
|
InstrMeta.Instruction.getOpcode())
|
|
|
|
<< " ";
|
|
|
|
InstrMeta.Instruction.print(outs());
|
2017-09-21 04:38:14 +08:00
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv);
|
|
|
|
|
|
|
|
InitializeAllTargetInfos();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
InitializeAllDisassemblers();
|
|
|
|
|
2017-10-11 04:59:08 +08:00
|
|
|
FileAnalysis Verifier = ExitOnErr(FileAnalysis::Create(InputFilename));
|
|
|
|
printIndirectCFInstructions(Verifier);
|
2017-09-21 04:38:14 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|