aboutsummaryrefslogtreecommitdiff
path: root/decomp/decomp.c
blob: 07935267cfe868d0c09f82cb9589f6830eb126a5 (plain) (blame)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* decomp.c
 * Decompile an executable file.
 *
 * Copyright (C) 2001 Jonathan duSaint <dusaint@earthlink.net>
 *
 * Started around 15 November 2001.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <stdint.h>

/*#include <signal.h>*/

#include "decomp.h"


char pname[] = PACKAGE_NAME;
char version[] = PACKAGE_VERSION;
char author[] = AUTHOR;
char author_email[] = AUTHOR_EMAIL;


int debug = 0;
int print_address = 0;
int print_opcode = 0;
int print_prefix = 0;
enum syntax syntax = INTEL;
int host_endian = ELITTLE, target_endian = ELITTLE; /* handy default */

void
write_file_header (FILE *fp, const char *source_file, const char *file_name)
{
  fprintf (fp,
	   "\t; %s\n"
	   "\t; Decompiled by Decomp from %s.\n"
	   "\t; In case of an incorrect decompilation, let the author know.\n"
	   "\n",
	   file_name, source_file);
}


/* get_output_name
 * Called when an output file has not been specified with -o.  Appends
 * DEFAULT_OUTPUT_EXTENSION on to the file name of the object file.  The
 * returned memory must be freed.
 */
char *
get_output_name (const char *file_name)
{
  char *output_name;
  char output_extension[] = DEFAULT_OUTPUT_EXTENSION;

  output_name = xmalloc (strlen (file_name) + strlen (output_extension) + 1);

  sprintf (output_name, "%s%s", file_name, output_extension);

  return (output_name);
}


/* byte_reverse
 * Convert from big to little endian and vice-versa.
 */
void
byte_reverse (void *ptr, size_t size, size_t nmemb)
{
  uint8_t *buf = (uint8_t *)ptr;

  if (size == 1 || nmemb == 0) return;

  do
    {
      register size_t low = 0, high = size - 1;

      while (high > low)
	{
	  register uint8_t tmp;

	  tmp = buf[low];
	  buf[low] = buf[high];
	  buf[high] = tmp;

	  high--; low++;
	}

      buf += size;
    }
  while (--nmemb);
}


/* d_read
 * Similar in functionality to fread, except ensures that > 0 bytes are
 * read and accounts for byte order.
 */
void
d_read (void *ptr, size_t size, size_t nmemb, FILE *stream, int as_is)
{
  if (fread (ptr, size, nmemb, stream) <= 0) error_out (ERR_FILE_READ);

  if (as_is) return;

  /* if (host_endian .xor. target_endian) */
  if ((host_endian || target_endian) - (host_endian && target_endian))
    byte_reverse (ptr, size, nmemb);
}


int
get_host_endian (void)
{
  union {
    char c[4];
    uint32_t i;
  } x;

  memset (&x, 0, 4); /* just to be sure */

  x.i = 1;

  if (x.c[0] != 0) return ELITTLE;
  else return EBIG;
}


void
print_version (void)
{
  printf ("%s %s\n"
	  "Copyright (C) 2001 %s %s\n"
	  "%s comes with NO WARRANTY, to the extent\n"
	  "permitted by law.  You may redistribute copies\n"
	  "of %s under the terms of the GNU General\n"
	  "Public License.  For more information about\n"
	  "these matters, see the file named COPYING.\n",
	  pname, version, author, author_email, pname, pname);

  exit (EXIT_SUCCESS);
}

void
print_help (void)
{
  printf ("Usage: decomp [OPTION]... <FILE>\n"
	  "Decompile FILE into a complete assembly language file\n"
	  "which can in turn be re-assembled.\n"
	  "\n"
	  "  -o, --output <file>      Place output in file.  The default is\n"
	  "                           to append .S to the input file name.\n"
	  "\n"
	  "  -h, --help               Print this message.\n"
	  "  -v, --version            Print version information.\n"
	  "\n"
	  "  -a, --address            Print the address of each instruction.\n"
	  "  -c, --opcode             Print the opcode of each instruction.\n"
	  "  -p, --prefix             Print any instruction prefixes.\n"
	  "\n"
	  "  -d, --debug              Print debugging information.\n"
	  "  -t, --trace              Turn on memory tracing.\n"
	  "\n"
	  "  -q, --queue-size <size>  Adjust the size of the output\n"
	  "                           instruction queue.  Only use this if\n"
	  "                           you know what you are doing.\n"
	  "IA-32 specific options:\n"
	  "  -s, --style <nasm|gas>   Select assembly language style.\n"
	  "                           Not yet implemented.\n"
	  "\n"
	  "Report bugs to %s %s\n",
	  author, author_email);
}


int
main (int argc, char *argv[])
{
  int k;
  char opt, *output_file_name = NULL;
  struct file_info fi;
  hash_t symtab;
  FILE *ofp;
  struct option options[] = {
    { "address",    no_argument,       NULL, 'a' },
    { "debug",      no_argument,       NULL, 'd' },
    { "help",       no_argument,       NULL, 'h' },
    { "opcode",     no_argument,       NULL, 'c' },
    { "output",     required_argument, NULL, 'o' },
    { "prefix",     no_argument,       NULL, 'p' },
    { "queue-size", required_argument, NULL, 'q' },
    { "style",      required_argument, NULL, 's' },
    { "trace",      no_argument,       NULL, 't' },
    { "version",    no_argument,       NULL, 'v' },
    { 0, 0, 0, 0 }
  };


  /* parse through argv, looking for options */
  while (1)
    {
      opt = getopt_long (argc, argv, "acdho:pq:s:tv", options, NULL);

      if (opt == EOF) break;

      switch (opt)
	{
	case 'a':
	  print_address = 1;
	  break;
	case 'c':
	  print_opcode = 1;
	  break;
	case 'd':
	  debug = 1;
	  break;
	case 'h':
	  print_help ();
	  exit (EXIT_SUCCESS);
	case 'o':
	  output_file_name = optarg;
	  break;
	case 'p':
	  print_prefix = 1;
	  break;
	case 'q':
	  max_queue_size = strtol (optarg, NULL, 0);
	  if (max_queue_size < 1)
	    {
	      fprintf (stderr, "DECOMP: invalid queue size - using default\n");
	      max_queue_size = MAX_OUTPUT_QUEUE_SIZE;
	    }
	  break;
	case 's':
	  if (!strcmp (optarg, "att"))
	    syntax = ATT;
	  else if (!strcmp (optarg, "intel"))
	    syntax = INTEL;
	  else
	    {
	      fprintf (stderr, "DECOMP: unknown disassembly syntax `%s'\n",
		       optarg);
	      exit (1);
	    }
	  break;
	case 't':
	  memory_trace = 1;
	  break;
	case 'v':
	  print_version ();
	  exit (EXIT_SUCCESS);
	case '?':
	  fprintf (stderr, "DECOMP: unknown option character `%c'\n", optopt);
	  exit (EXIT_FAILURE);
	default:
	  fprintf (stderr,
		   "DECOMP: ?? getopt returned character code 0x%x (%c) ??\n",
		   opt, opt);
	  exit (EXIT_FAILURE);
	}
    }

  /* all that's left in argv should be a file name to disassemble */
  if (argv[optind] == NULL) error_out (ERR_NEED_FILE_NAME);


  /* get host endian-ness and set target as the same for now */
  target_endian = host_endian = get_host_endian ();


  /* open_files (); */
  /* get_file_info (); */
  if (get_file_info (&fi, argv[optind])) error_out (ERR_FILE_INFO);

  /* write_asm_header (); */
  if (output_file_name == NULL)
    output_file_name = get_output_name (argv[optind]);

  ofp = fopen (output_file_name, "wt");
  if (ofp == NULL) error_out (ERR_FILE_OPEN);

  if (debug)
    setvbuf (ofp, NULL, _IONBF, 0);

  write_file_header (ofp, argv[optind], output_file_name);

  /* read the symtab */
  symtab = read_symtab (&fi);

  /* decode and write out the code sections */
  k = -1;
  while (fi.code_sections[++k])
    decode_code_section (&fi, fi.code_sections[k], symtab, ofp);

  /* decode and write out the data sections */
  k = -1;
  while (fi.data_sections[++k])
    decode_data_section (&fi, fi.data_sections[k], symtab, ofp);


  /* write_asm_footer (); */


  /* clean up */
  clean_symtab (symtab);
  fclose (ofp);
  if (clean_up_file (&fi)) error_out (ERR_CLEAN_UP);
  free_all ();

  return (0);
}