1 //===- bolt/tools/binary-analysis/binary-analysis.cpp ---------------------===// 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 is a generic binary analysis tool, where multiple different specific 10 // binary analyses can be plugged in to. The binary analyses are mostly built 11 // on top of BOLT components. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "bolt/Rewrite/RewriteInstance.h" 16 #include "bolt/Utils/CommandLineOpts.h" 17 #include "llvm/MC/TargetRegistry.h" 18 #include "llvm/Object/Binary.h" 19 #include "llvm/Object/ELFObjectFile.h" 20 #include "llvm/Support/CommandLine.h" 21 #include "llvm/Support/Errc.h" 22 #include "llvm/Support/ManagedStatic.h" 23 #include "llvm/Support/PrettyStackTrace.h" 24 #include "llvm/Support/Program.h" 25 #include "llvm/Support/Signals.h" 26 #include "llvm/Support/TargetSelect.h" 27 #include "llvm/Support/VirtualFileSystem.h" 28 29 #define DEBUG_TYPE "bolt" 30 31 using namespace llvm; 32 using namespace object; 33 using namespace bolt; 34 35 namespace opts { 36 37 static cl::OptionCategory *BinaryAnalysisCategories[] = { 38 &BinaryAnalysisCategory}; 39 40 static cl::opt<std::string> InputFilename(cl::Positional, 41 cl::desc("<executable>"), 42 cl::Required, 43 cl::cat(BinaryAnalysisCategory), 44 cl::sub(cl::SubCommand::getAll())); 45 46 } // namespace opts 47 48 static StringRef ToolName = "llvm-bolt-binary-analysis"; 49 50 static void report_error(StringRef Message, std::error_code EC) { 51 assert(EC); 52 errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n"; 53 exit(1); 54 } 55 56 static void report_error(StringRef Message, Error E) { 57 assert(E); 58 errs() << ToolName << ": '" << Message << "': " << toString(std::move(E)) 59 << ".\n"; 60 exit(1); 61 } 62 63 void ParseCommandLine(int argc, char **argv) { 64 cl::HideUnrelatedOptions(ArrayRef(opts::BinaryAnalysisCategories)); 65 // Register the target printer for --version. 66 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 67 68 cl::ParseCommandLineOptions(argc, argv, "BinaryAnalysis\n"); 69 } 70 71 static std::string GetExecutablePath(const char *Argv0) { 72 SmallString<256> ExecutablePath(Argv0); 73 // Do a PATH lookup if Argv0 isn't a valid path. 74 if (!llvm::sys::fs::exists(ExecutablePath)) 75 if (llvm::ErrorOr<std::string> P = 76 llvm::sys::findProgramByName(ExecutablePath)) 77 ExecutablePath = *P; 78 return std::string(ExecutablePath.str()); 79 } 80 81 int main(int argc, char **argv) { 82 // Print a stack trace if we signal out. 83 sys::PrintStackTraceOnErrorSignal(argv[0]); 84 PrettyStackTraceProgram X(argc, argv); 85 86 std::string ToolPath = GetExecutablePath(argv[0]); 87 88 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 89 90 // Initialize targets and assembly printers/parsers. 91 llvm::InitializeAllTargetInfos(); 92 llvm::InitializeAllTargetMCs(); 93 llvm::InitializeAllAsmParsers(); 94 llvm::InitializeAllDisassemblers(); 95 96 llvm::InitializeAllTargets(); 97 llvm::InitializeAllAsmPrinters(); 98 99 ParseCommandLine(argc, argv); 100 101 opts::BinaryAnalysisMode = true; 102 103 if (!sys::fs::exists(opts::InputFilename)) 104 report_error(opts::InputFilename, errc::no_such_file_or_directory); 105 106 Expected<OwningBinary<Binary>> BinaryOrErr = 107 createBinary(opts::InputFilename); 108 if (Error E = BinaryOrErr.takeError()) 109 report_error(opts::InputFilename, std::move(E)); 110 Binary &Binary = *BinaryOrErr.get().getBinary(); 111 112 if (auto *e = dyn_cast<ELFObjectFileBase>(&Binary)) { 113 auto RIOrErr = RewriteInstance::create(e, argc, argv, ToolPath); 114 if (Error E = RIOrErr.takeError()) 115 report_error(opts::InputFilename, std::move(E)); 116 RewriteInstance &RI = *RIOrErr.get(); 117 if (Error E = RI.run()) 118 report_error(opts::InputFilename, std::move(E)); 119 } 120 121 return EXIT_SUCCESS; 122 } 123