forked from xezon/unassemblize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
185 lines (164 loc) · 5.76 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @file
*
* @brief main function and option handling.
*
* @copyright Assemblize is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version
* 3 of the License, or (at your option) any later version.
* A full copy of the GNU General Public License can be found in
* LICENSE
*/
#include "function.h"
#include "gitinfo.h"
#include <LIEF/LIEF.hpp>
#include <getopt.h>
#include <inttypes.h>
#include <stdio.h>
#include <strings.h>
void print_help()
{
char revision[12] = {0};
const char *version = GitTag[0] == 'v' ? GitTag : GitShortSHA1;
if (GitTag[0] != 'v') {
snprintf(revision, sizeof(revision), "r%d ", GitRevision);
}
printf(
"\nunassemblize %s%s%s\n"
" x86 Unassembly tool\n\n"
"Usage:\n"
" unassemblize [OPTIONS] [INPUT]\n"
"Options:\n"
" -o --output Filename for single file output. Default is program.S\n"
" -f --format Assembly output format.\n"
" -c --config Configuration file describing how to dissassemble the input\n"
" file and containing extra symbol info. Default: config.json\n"
" -s --start Starting address of a single function to dissassemble in\n"
" hexidecimal notation.\n"
" -e --end Ending address of a single function to dissassemble in\n"
" hexidecimal notation.\n"
" -v --verbose Verbose output on current state of the program.\n"
" --section Section to target for dissassembly, defaults to '.text'.\n"
" --listsections Prints a list of sections in the exe then exits.\n"
" -d --dumpsyms Dumps symbols stored in the executable to the config file.\n"
" then exits.\n"
" -h --help Displays this help.\n\n",
revision,
GitUncommittedChanges ? "~" : "",
version);
}
void print_sections(unassemblize::Executable &exe)
{
for (auto it = exe.sections().begin(); it != exe.sections().end(); ++it) {
printf(
"Name: %s, Address: 0x%" PRIx64 " Size: %" PRIu64 "\n", it->first.c_str(), it->second.address, it->second.size);
}
}
int main(int argc, char **argv)
{
if (argc <= 1) {
print_help();
return -1;
}
const char *section_name = ".text";
const char *output = "program.S";
const char *config_file = "config.json";
const char *format_string = nullptr;
uint64_t start_addr = 0;
uint64_t end_addr = 0;
bool print_secs = false;
bool dump_syms = false;
bool verbose = false;
while (true) {
static struct option long_options[] = {
{"output", required_argument, nullptr, 'o'},
{"format", required_argument, nullptr, 'f'},
{"start", required_argument, nullptr, 's'},
{"end", required_argument, nullptr, 'e'},
{"config", required_argument, nullptr, 'c'},
{"section", required_argument, nullptr, 1},
{"listsections", no_argument, nullptr, 2},
{"dumpsyms", no_argument, nullptr, 'd'},
{"verbose", no_argument, nullptr, 'v'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, 0},
};
int option_index = 0;
int c = getopt_long(argc, argv, "+dhv?o:f:s:e:c:", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 1:
section_name = optarg;
break;
case 2:
print_secs = true;
break;
case 'd':
dump_syms = true;
break;
case 'o':
output = optarg;
break;
case 'f':
format_string = optarg;
break;
case 's':
start_addr = strtoull(optarg, nullptr, 16);
break;
case 'e':
end_addr = strtoull(optarg, nullptr, 16);
break;
case 'c':
config_file = optarg;
break;
case 'v':
verbose = true;
break;
case '?':
printf("\nOption %d not recognised.\n", optopt);
print_help();
return 0;
case ':':
printf("\nAn option is missing arguments.\n");
print_help();
return 0;
case 'h':
print_help();
break;
default:
break;
}
}
if (verbose) {
printf("Parsing executable file '%s'...\n", argv[optind]);
}
// TODO implement default value where exe object decides internally what to do.
unassemblize::Executable::OutputFormats format = unassemblize::Executable::OUTPUT_IGAS;
if (format_string != nullptr) {
if (strcasecmp(format_string, "igas") == 0) {
format = unassemblize::Executable::OUTPUT_IGAS;
} else if (strcasecmp(format_string, "masm") == 0) {
format = unassemblize::Executable::OUTPUT_MASM;
}
}
unassemblize::Executable exe(argv[optind], format, verbose);
if (print_secs) {
print_sections(exe);
return 0;
}
if (dump_syms) {
exe.save_config(config_file);
return 0;
}
exe.load_config(config_file);
FILE *fp = nullptr;
if (output != nullptr) {
fp = fopen(output, "w+");
}
fprintf(fp, ".intel_syntax noprefix\n\n");
exe.dissassemble_function(fp, section_name, start_addr, end_addr);
return 0;
}