Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / bolt / tools / bat-dump / bat-dump.cpp
blobfa53c5b122f8880cbec10bb47b1ea3de167d205e
1 #include "bolt/Profile/BoltAddressTranslation.h"
2 #include "llvm/ADT/ArrayRef.h"
3 #include "llvm/ADT/SmallString.h"
4 #include "llvm/ADT/StringRef.h"
5 #include "llvm/ADT/Twine.h"
6 #include "llvm/ADT/iterator_range.h"
7 #include "llvm/Object/Binary.h"
8 #include "llvm/Object/ELFObjectFile.h"
9 #include "llvm/Object/Error.h"
10 #include "llvm/Object/ObjectFile.h"
11 #include "llvm/Object/SymbolicFile.h"
12 #include "llvm/Support/Casting.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Program.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <algorithm>
22 #include <assert.h>
23 #include <cstdint>
24 #include <map>
25 #include <stdlib.h>
26 #include <string>
27 #include <system_error>
28 #include <type_traits>
29 #include <utility>
30 #include <vector>
32 using namespace llvm;
33 using namespace bolt;
35 namespace opts {
37 cl::OptionCategory BatDumpCategory("BAT dump options");
39 static cl::OptionCategory *BatDumpCategories[] = {&BatDumpCategory};
41 static cl::opt<std::string> InputFilename(cl::Positional,
42 cl::desc("<executable>"),
43 cl::Required,
44 cl::cat(BatDumpCategory));
46 static cl::list<uint64_t> Translate("translate",
47 cl::desc("translate addresses using BAT"),
48 cl::value_desc("addr"),
49 cl::cat(BatDumpCategory));
51 static cl::opt<bool> DumpAll("dump-all", cl::desc("dump all BAT tables"),
52 cl::cat(BatDumpCategory));
54 } // namespace opts
56 static StringRef ToolName;
58 static void report_error(StringRef Message, std::error_code EC) {
59 assert(EC);
60 errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n";
61 exit(1);
64 static void report_error(StringRef Message, Error E) {
65 assert(E);
66 errs() << ToolName << ": '" << Message << "': " << toString(std::move(E))
67 << ".\n";
68 exit(1);
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());
81 void dumpBATFor(llvm::object::ELFObjectFileBase *InputFile) {
82 BoltAddressTranslation BAT;
83 if (!BAT.enabledFor(InputFile)) {
84 errs() << "error: no BAT table found.\n";
85 exit(1);
88 // Look for BAT section
89 bool Found = false;
90 StringRef SectionContents;
91 for (const llvm::object::SectionRef &Section : InputFile->sections()) {
92 Expected<StringRef> SectionNameOrErr = Section.getName();
93 if (Error E = SectionNameOrErr.takeError())
94 continue;
96 if (SectionNameOrErr.get() != BoltAddressTranslation::SECTION_NAME)
97 continue;
99 Found = true;
100 Expected<StringRef> ContentsOrErr = Section.getContents();
101 if (Error E = ContentsOrErr.takeError())
102 continue;
103 SectionContents = ContentsOrErr.get();
106 if (!Found) {
107 errs() << "BOLT-ERROR: failed to parse BOLT address translation "
108 "table. No BAT section found\n";
109 exit(1);
112 if (std::error_code EC = BAT.parse(SectionContents)) {
113 errs() << "BOLT-ERROR: failed to parse BOLT address translation "
114 "table. Malformed BAT section\n";
115 exit(1);
118 if (opts::DumpAll)
119 BAT.dump(outs());
121 if (!opts::Translate.empty()) {
122 // Build map of <Address, SymbolName> for InputFile
123 std::map<uint64_t, StringRef> FunctionsMap;
124 for (const llvm::object::ELFSymbolRef &Symbol : InputFile->symbols()) {
125 Expected<StringRef> NameOrError = Symbol.getName();
126 if (NameOrError.takeError())
127 continue;
128 if (cantFail(Symbol.getType()) != llvm::object::SymbolRef::ST_Function)
129 continue;
130 const StringRef Name = *NameOrError;
131 const uint64_t Address = cantFail(Symbol.getAddress());
132 FunctionsMap[Address] = Name;
135 outs() << "Translating addresses according to parsed BAT tables:\n";
136 for (uint64_t Address : opts::Translate) {
137 auto FI = FunctionsMap.upper_bound(Address);
138 if (FI == FunctionsMap.begin()) {
139 outs() << "No function symbol found for 0x" << Twine::utohexstr(Address)
140 << "\n";
141 continue;
143 --FI;
144 outs() << "0x" << Twine::utohexstr(Address) << " -> " << FI->second
145 << " + 0x"
146 << Twine::utohexstr(
147 BAT.translate(FI->first, Address - FI->first, false))
148 << "\n";
153 int main(int argc, char **argv) {
154 cl::HideUnrelatedOptions(ArrayRef(opts::BatDumpCategories));
155 cl::ParseCommandLineOptions(argc, argv, "");
157 if (!sys::fs::exists(opts::InputFilename))
158 report_error(opts::InputFilename, errc::no_such_file_or_directory);
160 ToolName = argv[0];
161 std::string ToolPath = GetExecutablePath(argv[0]);
162 Expected<llvm::object::OwningBinary<llvm::object::Binary>> BinaryOrErr =
163 llvm::object::createBinary(opts::InputFilename);
164 if (Error E = BinaryOrErr.takeError())
165 report_error(opts::InputFilename, std::move(E));
166 llvm::object::Binary &Binary = *BinaryOrErr.get().getBinary();
168 if (auto *InputFile = dyn_cast<llvm::object::ELFObjectFileBase>(&Binary))
169 dumpBATFor(InputFile);
170 else
171 report_error(opts::InputFilename,
172 llvm::object::object_error::invalid_file_type);
174 return EXIT_SUCCESS;