2
* dacli.c - libdisarm command line interface
4
* Copyright (C) 2007 Jon Lund Steffensen <jonlst@gmail.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31
#include <libdisarm/disarm.h>
35
"Usage: %s [-EB|-EL] [-h] [-m OFFSET] [-s SKIP] [FILE]\n"
38
" Disassemble ARM machine code from FILE or standard input.\n" \
39
" -EB\t\tRead input as big endian data\n" \
40
" -EL\t\tRead input as little endian data\n" \
41
" -h\t\tDisplay this help message\n" \
42
" -m OFFSET\tUse OFFSET as memory address of input\n" \
43
" -s SKIP\tNumber of bytes to skip before disassembly\n" \
44
"Report bugs to <" PACKAGE_BUGREPORT ">.\n"
46
/* Return -1 on error, 0 on EOF, 1 on succesful read. */
48
read_hex_input(void *dest, size_t size, FILE *f)
51
if (c == EOF) return 0;
54
for (i = 0; i < size; i++) {
59
if (c == EOF) return 0;
63
for (j = 0; j < 2; j++) {
65
if (c >= '0' && c <= '9') {
67
} else if (c >= 'a' && c <= 'f') {
69
} else if (c >= 'A' && c <= 'F') {
71
} else if (j > 0 && isspace(c)) {
77
byte = (byte << 4) | input;
83
((unsigned char *)dest)[i] = byte;
90
main(int argc, char *argv[])
95
da_addr_t mem_offset = 0;
96
off_t file_offset = 0;
97
ssize_t disasm_size = -1;
101
while ((opt = getopt(argc, argv, "c:E:hm:s:x")) != -1) {
104
disasm_size = atoi(optarg);
107
if (optarg != NULL &&
108
(optarg[0] == 'B' || optarg[0] == 'L')) {
109
big_endian = (optarg[0] == 'B');
111
fprintf(stderr, USAGE, argv[0]);
116
printf(HELP, argv[0]);
120
mem_offset = atoi(optarg);
123
file_offset = atoi(optarg);
129
fprintf(stderr, USAGE, argv[0]);
136
if (optind < argc && strcmp(argv[optind], "-")) {
137
f = fopen(argv[optind], "rb");
143
if (file_offset > 0) {
144
r = fseek(f, file_offset, SEEK_SET);
152
da_addr_t addr = mem_offset;
154
while (disasm_size < 0 || addr < mem_offset + disasm_size) {
158
size_t read = fread(&data, sizeof(da_word_t), 1, f);
167
r = read_hex_input(&data, sizeof(da_word_t), f);
169
fprintf(stderr, "Unable to parse input.\n");
177
da_instr_args_t args;
178
da_instr_parse(&instr, data, big_endian);
179
da_instr_parse_args(&args, &instr);
181
printf("%08x\t", addr);
182
printf("%08x\t", instr.data);
183
da_instr_fprint(stdout, &instr, &args, addr);
186
addr += sizeof(da_word_t);