1 /* udis86 - udcli/udcli.c
3 * Copyright (c) 2002-2013 Vivek Thampi
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "..\udis86.h"
32 #define PACKAGE_STRING "udis86 pre-1.8"
38 #if defined(__APPLE__)
40 #elif defined(__amd64__) || defined(__x86_64__)
46 #if defined(__DJGPP__) || defined(_WIN32)
52 # include <unistd.h> /* for isatty() */
53 # define _setmode setmode
54 # define _fileno fileno
55 # define _O_BINARY O_BINARY
61 "Usage: %s [-option[s]] file\n"
63 " -16 : Set the disassembly mode to 16 bits. \n"
64 " -32 : Set the disassembly mode to 32 bits. (default)\n"
65 " -64 : Set the disassembly mode to 64 bits.\n"
66 " -intel : Set the output to INTEL (NASM like) syntax. (default)\n"
67 " -att : Set the output to AT&T (GAS like) syntax.\n"
68 " -v <v> : Set vendor. <v> = {intel, amd}.\n"
69 " -o <pc> : Set the value of program counter to <pc>. (default = 0)\n"
70 " -s <n> : Set the number of bytes to skip before disassembly to <n>.\n"
71 " -c <n> : Set the number of bytes to disassemble to <n>.\n"
72 " -x : Set the input mode to whitespace separated 8-bit numbers in\n"
73 " hexadecimal representation. Example: 0f 01 ae 00\n"
74 " -noff : Do not display the offset of instructions.\n"
75 " -nohex : Do not display the hexadecimal code of instructions.\n"
76 " -h : Display this help message.\n"
77 " --version: Show version.\n"
79 "Udcli is a front-end to the Udis86 Disassembler Library.\n"
80 "http://udis86.sourceforge.net/\n"
86 unsigned char o_do_count= 0;
87 unsigned char o_do_off = 1;
88 unsigned char o_do_hex = 1;
89 unsigned char o_do_x = 0;
90 unsigned o_vendor = UD_VENDOR_AMD;
92 int input_hook_x(ud_t* u);
93 int input_hook_file(ud_t* u);
95 int main(int argc, char **argv)
97 char *prog_path = *argv;
103 ud_set_mode(&ud_obj, 32);
104 ud_set_syntax(&ud_obj, UD_SYN_INTEL);
107 if ( !isatty( fileno( stdin ) ) )
109 #if defined(__DJGPP) || defined(_WIN32)
110 _setmode(_fileno(stdin), _O_BINARY);
117 /* loop through the args */
119 if (strcmp(*argv, "-h") == 0) {
120 printf(help, prog_path);
122 } else if (strcmp(*argv,"-16") == 0) {
123 ud_set_mode(&ud_obj, 16);
124 } else if (strcmp(*argv,"-32") == 0) {
125 ud_set_mode(&ud_obj, 32);
126 } else if (strcmp(*argv,"-64") == 0) {
127 ud_set_mode(&ud_obj, 64);
128 } else if (strcmp(*argv,"-intel") == 0)
129 ud_set_syntax(&ud_obj, UD_SYN_INTEL);
130 else if (strcmp(*argv,"-att") == 0)
131 ud_set_syntax(&ud_obj, UD_SYN_ATT);
132 else if (strcmp(*argv,"-noff") == 0)
134 else if (strcmp(*argv,"-nohex") == 0)
136 else if (strcmp(*argv,"-x") == 0)
138 else if (strcmp(*argv,"-s") == 0)
141 if (sscanf(s, "%" FMT64 "u", &o_skip) == 0)
142 fprintf(stderr, "Invalid value given for -s.\n");
144 fprintf(stderr, "No value given for -s.\n");
145 printf(help, prog_path);
148 else if (strcmp(*argv,"-c") == 0)
152 if (sscanf(s, "%" FMT64 "u", &o_count) == 0)
153 fprintf(stderr, "Invalid value given for -c.\n");
155 fprintf(stderr, "No value given for -c.\n");
156 printf(help, prog_path);
159 else if (strcmp(*argv,"-v") == 0)
163 ud_set_vendor(&ud_obj, UD_VENDOR_INTEL);
165 fprintf(stderr, "No value given for -v.\n");
166 printf(help, prog_path);
169 else if (strcmp(*argv,"-o") == 0) {
173 if (sscanf(s, "%" FMT64 "x", &pc) == 0)
174 fprintf(stderr, "Invalid value given for -o.\n");
175 ud_set_pc(&ud_obj, pc);
177 fprintf(stderr, "No value given for -o.\n");
178 printf(help, prog_path);
181 } else if ( strcmp( *argv, "--version" ) == 0 ) {
182 fprintf(stderr, "%s\n", PACKAGE_STRING );
184 } else if((*argv)[0] == '-') {
185 fprintf(stderr, "Invalid option %s.\n", *argv);
186 printf(help, prog_path);
192 fprintf(stderr, "Multiple files specified.\n");
195 if ((fptr = fopen(s, "rb")) == NULL) {
196 fprintf(stderr, "Failed to open file: %s.\n", s);
204 ud_set_input_hook(&ud_obj, input_hook_x);
205 else ud_set_input_hook(&ud_obj, input_hook_file);
209 ud_input_skip(&ud_obj, o_skip);
212 /* disassembly loop */
213 while (ud_disassemble(&ud_obj)) {
215 printf("%016" FMT64 "x ", ud_insn_off(&ud_obj));
217 const char* hex1, *hex2;
218 hex1 = ud_insn_hex(&ud_obj);
220 printf("%-16.16s %-24s", hex1, ud_insn_asm(&ud_obj));
221 if (strlen(hex1) > 16) {
224 printf("%15s -", "");
225 printf("%-16s", hex2);
228 else printf(" %-24s", ud_insn_asm(&ud_obj));
237 int input_hook_x(ud_t* u)
247 i = fscanf(fptr, "%x", &c);
252 fprintf(stderr, "Error: Invalid input, should be in hexadecimal form (8-bit).\n");
256 fprintf(stderr, "Warning: Casting non-8-bit input (%x), to %x.\n", c, c & 0xFF);
257 return (int) (c & 0xFF);
260 int input_hook_file(ud_t* u)
270 if ((c = fgetc(fptr)) == EOF)