1 //===- bolt/tools/bat-dump/bat-dump.cpp - BAT dumper utility --------------===//
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
7 //===----------------------------------------------------------------------===//
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"
33 #include <system_error>
34 #include <type_traits>
42 cl::OptionCategory
BatDumpCategory("BAT dump options");
44 static cl::OptionCategory
*BatDumpCategories
[] = {&BatDumpCategory
};
46 static cl::opt
<std::string
> InputFilename(cl::Positional
,
47 cl::desc("<executable>"),
49 cl::cat(BatDumpCategory
));
51 static cl::list
<uint64_t> Translate("translate",
52 cl::desc("translate addresses using BAT"),
53 cl::value_desc("addr"),
54 cl::cat(BatDumpCategory
));
56 static cl::opt
<bool> DumpAll("dump-all", cl::desc("dump all BAT tables"),
57 cl::cat(BatDumpCategory
));
61 static StringRef ToolName
;
63 static void report_error(StringRef Message
, std::error_code EC
) {
65 errs() << ToolName
<< ": '" << Message
<< "': " << EC
.message() << ".\n";
69 static void report_error(StringRef Message
, Error E
) {
71 errs() << ToolName
<< ": '" << Message
<< "': " << toString(std::move(E
))
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
))
83 return std::string(ExecutablePath
);
86 void dumpBATFor(llvm::object::ELFObjectFileBase
*InputFile
) {
87 BoltAddressTranslation BAT
;
88 if (!BAT
.enabledFor(InputFile
)) {
89 errs() << "error: no BAT table found.\n";
93 // Look for BAT section
95 StringRef SectionContents
;
96 for (const llvm::object::SectionRef
&Section
: InputFile
->sections()) {
97 Expected
<StringRef
> SectionNameOrErr
= Section
.getName();
98 if (Error E
= SectionNameOrErr
.takeError())
101 if (SectionNameOrErr
.get() != BoltAddressTranslation::SECTION_NAME
)
105 Expected
<StringRef
> ContentsOrErr
= Section
.getContents();
106 if (Error E
= ContentsOrErr
.takeError())
108 SectionContents
= ContentsOrErr
.get();
112 errs() << "BOLT-ERROR: failed to parse BOLT address translation "
113 "table. No BAT section found\n";
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";
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())
133 if (cantFail(Symbol
.getType()) != llvm::object::SymbolRef::ST_Function
)
135 const StringRef Name
= *NameOrError
;
136 const uint64_t Address
= cantFail(Symbol
.getAddress());
137 FunctionsMap
[Address
] = Name
;
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
)
149 outs() << "0x" << Twine::utohexstr(Address
) << " -> " << FI
->second
152 BAT
.translate(FI
->first
, Address
- FI
->first
, false))
158 int main(int argc
, char **argv
) {
159 cl::HideUnrelatedOptions(ArrayRef(opts::BatDumpCategories
));
160 cl::ParseCommandLineOptions(argc
, argv
, "");
162 if (!sys::fs::exists(opts::InputFilename
))
163 report_error(opts::InputFilename
, errc::no_such_file_or_directory
);
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();
173 if (auto *InputFile
= dyn_cast
<llvm::object::ELFObjectFileBase
>(&Binary
))
174 dumpBATFor(InputFile
);
176 report_error(opts::InputFilename
,
177 llvm::object::object_error::invalid_file_type
);