xref: /llvm-project/bolt/tools/bat-dump/bat-dump.cpp (revision fd38366e4525c5507bbb2a2fc1f7d113a964224e)
1 //===- bolt/tools/bat-dump/bat-dump.cpp - BAT dumper utility --------------===//
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 #include "bolt/Profile/BoltAddressTranslation.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Object/Binary.h"
15 #include "llvm/Object/ELFObjectFile.h"
16 #include "llvm/Object/Error.h"
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/Object/SymbolicFile.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Errc.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ErrorOr.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Program.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <assert.h>
29 #include <cstdint>
30 #include <map>
31 #include <stdlib.h>
32 #include <string>
33 #include <system_error>
34 #include <type_traits>
35 #include <utility>
36 
37 using namespace llvm;
38 using namespace bolt;
39 
40 namespace opts {
41 
42 cl::OptionCategory BatDumpCategory("BAT dump options");
43 
44 static cl::OptionCategory *BatDumpCategories[] = {&BatDumpCategory};
45 
46 static cl::opt<std::string> InputFilename(cl::Positional,
47                                           cl::desc("<executable>"),
48                                           cl::Required,
49                                           cl::cat(BatDumpCategory));
50 
51 static cl::list<uint64_t> Translate("translate",
52                                     cl::desc("translate addresses using BAT"),
53                                     cl::value_desc("addr"),
54                                     cl::cat(BatDumpCategory));
55 
56 static cl::opt<bool> DumpAll("dump-all", cl::desc("dump all BAT tables"),
57                              cl::cat(BatDumpCategory));
58 
59 } // namespace opts
60 
61 static StringRef ToolName;
62 
report_error(StringRef Message,std::error_code EC)63 static void report_error(StringRef Message, std::error_code EC) {
64   assert(EC);
65   errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n";
66   exit(1);
67 }
68 
report_error(StringRef Message,Error E)69 static void report_error(StringRef Message, Error E) {
70   assert(E);
71   errs() << ToolName << ": '" << Message << "': " << toString(std::move(E))
72          << ".\n";
73   exit(1);
74 }
75 
GetExecutablePath(const char * Argv0)76 static std::string GetExecutablePath(const char *Argv0) {
77   SmallString<256> ExecutablePath(Argv0);
78   // Do a PATH lookup if Argv0 isn't a valid path.
79   if (!llvm::sys::fs::exists(ExecutablePath))
80     if (llvm::ErrorOr<std::string> P =
81             llvm::sys::findProgramByName(ExecutablePath))
82       ExecutablePath = *P;
83   return std::string(ExecutablePath);
84 }
85 
dumpBATFor(llvm::object::ELFObjectFileBase * InputFile)86 void dumpBATFor(llvm::object::ELFObjectFileBase *InputFile) {
87   BoltAddressTranslation BAT;
88   if (!BAT.enabledFor(InputFile)) {
89     errs() << "error: no BAT table found.\n";
90     exit(1);
91   }
92 
93   // Look for BAT section
94   bool Found = false;
95   StringRef SectionContents;
96   for (const llvm::object::SectionRef &Section : InputFile->sections()) {
97     Expected<StringRef> SectionNameOrErr = Section.getName();
98     if (Error E = SectionNameOrErr.takeError())
99       continue;
100 
101     if (SectionNameOrErr.get() != BoltAddressTranslation::SECTION_NAME)
102       continue;
103 
104     Found = true;
105     Expected<StringRef> ContentsOrErr = Section.getContents();
106     if (Error E = ContentsOrErr.takeError())
107       continue;
108     SectionContents = ContentsOrErr.get();
109   }
110 
111   if (!Found) {
112     errs() << "BOLT-ERROR: failed to parse BOLT address translation "
113               "table. No BAT section found\n";
114     exit(1);
115   }
116 
117   if (std::error_code EC = BAT.parse(outs(), SectionContents)) {
118     errs() << "BOLT-ERROR: failed to parse BOLT address translation "
119               "table. Malformed BAT section\n";
120     exit(1);
121   }
122 
123   if (opts::DumpAll)
124     BAT.dump(outs());
125 
126   if (!opts::Translate.empty()) {
127     // Build map of <Address, SymbolName> for InputFile
128     std::map<uint64_t, StringRef> FunctionsMap;
129     for (const llvm::object::ELFSymbolRef &Symbol : InputFile->symbols()) {
130       Expected<StringRef> NameOrError = Symbol.getName();
131       if (NameOrError.takeError())
132         continue;
133       if (cantFail(Symbol.getType()) != llvm::object::SymbolRef::ST_Function)
134         continue;
135       const StringRef Name = *NameOrError;
136       const uint64_t Address = cantFail(Symbol.getAddress());
137       FunctionsMap[Address] = Name;
138     }
139 
140     outs() << "Translating addresses according to parsed BAT tables:\n";
141     for (uint64_t Address : opts::Translate) {
142       auto FI = FunctionsMap.upper_bound(Address);
143       if (FI == FunctionsMap.begin()) {
144         outs() << "No function symbol found for 0x" << Twine::utohexstr(Address)
145                << "\n";
146         continue;
147       }
148       --FI;
149       outs() << "0x" << Twine::utohexstr(Address) << " -> " << FI->second
150              << " + 0x"
151              << Twine::utohexstr(
152                     BAT.translate(FI->first, Address - FI->first, false))
153              << "\n";
154     }
155   }
156 }
157 
main(int argc,char ** argv)158 int main(int argc, char **argv) {
159   cl::HideUnrelatedOptions(ArrayRef(opts::BatDumpCategories));
160   cl::ParseCommandLineOptions(argc, argv, "");
161 
162   if (!sys::fs::exists(opts::InputFilename))
163     report_error(opts::InputFilename, errc::no_such_file_or_directory);
164 
165   ToolName = argv[0];
166   std::string ToolPath = GetExecutablePath(argv[0]);
167   Expected<llvm::object::OwningBinary<llvm::object::Binary>> BinaryOrErr =
168       llvm::object::createBinary(opts::InputFilename);
169   if (Error E = BinaryOrErr.takeError())
170     report_error(opts::InputFilename, std::move(E));
171   llvm::object::Binary &Binary = *BinaryOrErr.get().getBinary();
172 
173   if (auto *InputFile = dyn_cast<llvm::object::ELFObjectFileBase>(&Binary))
174     dumpBATFor(InputFile);
175   else
176     report_error(opts::InputFilename,
177                  llvm::object::object_error::invalid_file_type);
178 
179   return EXIT_SUCCESS;
180 }
181