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
37using namespace llvm;
38using namespace bolt;
39
40namespace opts {
41
42cl::OptionCategory BatDumpCategory("BAT dump options");
43
44static cl::OptionCategory *BatDumpCategories[] = {&BatDumpCategory};
45
46static cl::opt<std::string> InputFilename(cl::Positional,
47 cl::desc("<executable>"),
48 cl::Required,
49 cl::cat(BatDumpCategory));
50
51static cl::list<uint64_t> Translate("translate",
52 cl::desc("translate addresses using BAT"),
53 cl::value_desc("addr"),
54 cl::cat(BatDumpCategory));
55
56static cl::opt<bool> DumpAll("dump-all", cl::desc("dump all BAT tables"),
57 cl::cat(BatDumpCategory));
58
59} // namespace opts
60
61static StringRef ToolName;
62
63static void report_error(StringRef Message, std::error_code EC) {
64 assert(EC);
65 errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n";
66 exit(status: 1);
67}
68
69static void report_error(StringRef Message, Error E) {
70 assert(E);
71 errs() << ToolName << ": '" << Message << "': " << toString(E: std::move(E))
72 << ".\n";
73 exit(status: 1);
74}
75
76static 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(Path: ExecutablePath))
80 if (llvm::ErrorOr<std::string> P =
81 llvm::sys::findProgramByName(Name: ExecutablePath))
82 ExecutablePath = *P;
83 return std::string(ExecutablePath);
84}
85
86void dumpBATFor(llvm::object::ELFObjectFileBase *InputFile) {
87 BoltAddressTranslation BAT;
88 if (!BAT.enabledFor(InputFile)) {
89 errs() << "error: no BAT table found.\n";
90 exit(status: 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(status: 1);
115 }
116
117 if (std::error_code EC = BAT.parse(OS&: outs(), Buf: SectionContents)) {
118 errs() << "BOLT-ERROR: failed to parse BOLT address translation "
119 "table. Malformed BAT section\n";
120 exit(status: 1);
121 }
122
123 if (opts::DumpAll)
124 BAT.dump(OS&: 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(ValOrErr: Symbol.getType()) != llvm::object::SymbolRef::ST_Function)
134 continue;
135 const StringRef Name = *NameOrError;
136 const uint64_t Address = cantFail(ValOrErr: 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(x: Address);
143 if (FI == FunctionsMap.begin()) {
144 outs() << "No function symbol found for 0x" << Twine::utohexstr(Val: Address)
145 << "\n";
146 continue;
147 }
148 --FI;
149 outs() << "0x" << Twine::utohexstr(Val: Address) << " -> " << FI->second
150 << " + 0x"
151 << Twine::utohexstr(
152 Val: BAT.translate(FuncAddress: FI->first, Offset: Address - FI->first, IsBranchSrc: false))
153 << "\n";
154 }
155 }
156}
157
158int main(int argc, char **argv) {
159 cl::HideUnrelatedOptions(Categories: ArrayRef(opts::BatDumpCategories));
160 cl::ParseCommandLineOptions(argc, argv, Overview: "");
161
162 if (!sys::fs::exists(Path: opts::InputFilename))
163 report_error(Message: opts::InputFilename, EC: errc::no_such_file_or_directory);
164
165 ToolName = argv[0];
166 std::string ToolPath = GetExecutablePath(Argv0: argv[0]);
167 Expected<llvm::object::OwningBinary<llvm::object::Binary>> BinaryOrErr =
168 llvm::object::createBinary(Path: opts::InputFilename);
169 if (Error E = BinaryOrErr.takeError())
170 report_error(Message: opts::InputFilename, E: std::move(E));
171 llvm::object::Binary &Binary = *BinaryOrErr.get().getBinary();
172
173 if (auto *InputFile = dyn_cast<llvm::object::ELFObjectFileBase>(Val: &Binary))
174 dumpBATFor(InputFile);
175 else
176 report_error(Message: opts::InputFilename,
177 EC: llvm::object::object_error::invalid_file_type);
178
179 return EXIT_SUCCESS;
180}
181

source code of bolt/tools/bat-dump/bat-dump.cpp