-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.cpp
88 lines (77 loc) · 3.13 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "binutils/elf/elf++.hh"
#include "disasm/ElfDisassembler.h"
#include "disasm/analysis/SectionDisassemblyAnalyzerARM.h"
#include <fcntl.h>
#include <util/cmdline.h>
struct ConfigConsts {
const std::string kFile;
const std::string kNoSymbols;
const std::string kSpeculative;
const std::string kText;
ConfigConsts() : kFile{"file"},
kNoSymbols{"no-symbols"},
kSpeculative{"speculative"},
kText{"text"} { }
};
int main(int argc, char **argv) {
ConfigConsts config;
cmdline::parser cmd_parser;
cmd_parser.add<std::string>(config.kFile,
'f',
"Path to an ARM ELF file to be disassembled",
true,
"");
cmd_parser.add(config.kSpeculative, 's',
"Show all 'valid' disassembly");
cmd_parser.add(config.kText, 't',
"Disassemble .text section only");
cmd_parser.parse_check(argc, argv);
auto file_path = cmd_parser.get<std::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 disassmble ARM/Thumb executables only
if ((elf_file.get_hdr().machine) != EM_ARM) {
fprintf(stderr, "%s : Elf file architecture is not ARM!\n", argv[1]);
return 3;
}
disasm::ElfDisassembler disassembler{elf_file};
if (cmd_parser.exist(config.kSpeculative)) {
std::cout << "Speculative disassembly of file: "
<< file_path << "\n";
if (cmd_parser.exist(config.kText)) {
auto result =
disassembler.disassembleSectionbyNameSpeculative(".text");
disasm::SectionDisassemblyAnalyzerARM analyzer{&elf_file, &result};
analyzer.buildCFG();
analyzer.refineCFG();
disassembler.prettyPrintSectionCFG
(&analyzer.getCFG(),
disasm::PrettyPrintConfig::kHideDataNodes);
// disassembler.prettyPrintSwitchTables(&analyzer.getCFG());
// analyzer.buildCallGraph();
} else {
disassembler.disassembleCodeSpeculative();
}
} else if (disassembler.isSymbolTableAvailable()) {
std::cout << "Disassembly using symbol table of file: "
<< file_path << "\n";
if (cmd_parser.exist(config.kText)) {
auto result = disassembler.disassembleSectionbyName(".text");
disasm::SectionDisassemblyAnalyzerARM analyzer{&elf_file, &result};
analyzer.buildCFG();
analyzer.refineCFG();
disassembler.prettyPrintSectionCFG
(&analyzer.getCFG(),
disasm::PrettyPrintConfig::kDisplayDataNodes);
// disassembler.prettyPrintSwitchTables(&analyzer.getCFG());
// analyzer.buildCallGraph();
} else
disassembler.disassembleCodeUsingSymbols();
} else
std::cout << "Symbol table was not found!!" << "\n";
return 0;
}