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"
27 #include <system_error>
28 #include <type_traits>
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>"),
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
));
56 static StringRef ToolName
;
58 static void report_error(StringRef Message
, std::error_code EC
) {
60 errs() << ToolName
<< ": '" << Message
<< "': " << EC
.message() << ".\n";
64 static void report_error(StringRef Message
, Error E
) {
66 errs() << ToolName
<< ": '" << Message
<< "': " << toString(std::move(E
))
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
))
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";
88 // Look for BAT section
90 StringRef SectionContents
;
91 for (const llvm::object::SectionRef
&Section
: InputFile
->sections()) {
92 Expected
<StringRef
> SectionNameOrErr
= Section
.getName();
93 if (Error E
= SectionNameOrErr
.takeError())
96 if (SectionNameOrErr
.get() != BoltAddressTranslation::SECTION_NAME
)
100 Expected
<StringRef
> ContentsOrErr
= Section
.getContents();
101 if (Error E
= ContentsOrErr
.takeError())
103 SectionContents
= ContentsOrErr
.get();
107 errs() << "BOLT-ERROR: failed to parse BOLT address translation "
108 "table. No BAT section found\n";
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";
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())
128 if (cantFail(Symbol
.getType()) != llvm::object::SymbolRef::ST_Function
)
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
)
144 outs() << "0x" << Twine::utohexstr(Address
) << " -> " << FI
->second
147 BAT
.translate(FI
->first
, Address
- FI
->first
, false))
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
);
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
);
171 report_error(opts::InputFilename
,
172 llvm::object::object_error::invalid_file_type
);