-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
72 lines (59 loc) · 2.16 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "binutils/elf/elf++.hh"
#include "disasm/ElfDisassembler.h"
#include "disasm/ElfData.h"
#include <fcntl.h>
#include <util/cmdline.h>
using namespace std;
struct ConfigConsts {
const std::string kFile;
const std::string kLinearSweep;
const std::string kTextSectionOnly;
ConfigConsts() : kFile{"file"},
kLinearSweep{"linear-sweep"},
kTextSectionOnly{"text-section"} { }
};
int main(int argc, char **argv) {
ConfigConsts config;
cmdline::parser cmd_parser;
cmd_parser.add<string>(config.kFile,
'f',
"Path to a TriCore ELF file to be disassembled",
true,
"");
cmd_parser.add(config.kLinearSweep, 'l', "Disassembly using linear sweep");
cmd_parser.add(config.kTextSectionOnly,
't',
"Disassembly text section only");
cmd_parser.parse_check(argc, argv);
auto file_path = cmd_parser.get<string>(config.kFile);
int fd = open(file_path.c_str(), O_RDONLY);
if (fd < 0) {
fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
return 1;
}
elf::elf elf_file(elf::create_mmap_loader(fd));
// We only disassemble TriCore executables.
if (static_cast<elf::ElfISA>(elf_file.get_hdr().machine)
!= elf::ElfISA::kTriCore) {
fprintf(stderr, "%s : Elf file architecture is not TriCore!\n", argv[1]);
return 3;
}
disasm::ElfDisassembler disassembler{elf_file};
if (!disassembler.isSymbolTableAvailable()
|| cmd_parser.exist(config.kLinearSweep)) {
if (cmd_parser.exist(config.kTextSectionOnly)) {
disassembler.disassembleSectionUsingLinearSweep
(disassembler.findSectionbyName(".text"));
} else {
disassembler.disassembleCodeUsingLinearSweep();
}
} else {
if (cmd_parser.exist(config.kTextSectionOnly)) {
disassembler.disassembleSectionUsingSymbols
(disassembler.findSectionbyName(".text"));
} else {
disassembler.disassembleCodeUsingSymbols();
}
}
return 0;
}