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