decompiler won't compile!

Reply

Join Date: Oct 2006
Posts: 222
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

decompiler won't compile!

 
0
  #1
Nov 24th, 2006
I have some old programs compiled in the aout format which I would like to recompile with ELF for Linux.
I found this decompiler program (REC) which claims to be able to do the trick.
Unfortunatly, the way he has the the download setup is not very good for a direct Linux install, so I decided to use the sorce code. It comes up with a ton of warnings and an undefined reference error for the mneu variable used as a function arg.

I don't know anything about C. So I ask-Is this just bad code or it is salvagable with a few tweeks?

  1.  
  2. /* dismips.c
  3.  *
  4.  * Created by Giampiero Caprino
  5.  * Backer Street Software
  6.  *
  7.  * This software is part of REC, the reverse engineering compiler.
  8.  * You are free to use, copy, modify and distribute this software.
  9.  * If you fix bugs and or extend this software, I
  10.  * will be happy to include your changes in the
  11.  * most current version of the source.
  12.  * just send mail to caprino@netcom.com
  13.  */
  14.  
  15. struct instr {
  16. char *name;
  17. unsigned long opcode;
  18. unsigned long opcode2;
  19. int (*optype)();
  20. int (*op1)();
  21. int (*op2)();
  22. int (*op3)();
  23. };
  24.  
  25. static char opcode[32]; /* opcode mnemonic of current instruction */
  26. static char ops[8][32]; /* operands of current instruction */
  27. static char *op[8]; /* pointers to operand */
  28. static char **opp; /* pointer to current operand */
  29. static long cur_pc; /* address of current instruction */
  30. extern char mneu[]; /* the output string from the disassembler */
  31.  
  32. static int op26(struct instr *ip, unsigned long val)
  33. {
  34. if((val >> 26) == ip->opcode)
  35. return(1);
  36. return(0);
  37. }
  38.  
  39. static int op32(struct instr *ip, unsigned long val)
  40. {
  41. if(val == ip->opcode)
  42. return(1);
  43. return(0);
  44. }
  45.  
  46. static int op26_16(struct instr *ip, unsigned long val)
  47. {
  48. if((val >> 26) == ip->opcode && ((val >> 16) & 0x1F) == ip->opcode2)
  49. return(1);
  50. return(0);
  51. }
  52.  
  53. static int op26__16(struct instr *ip, unsigned long val)
  54. {
  55. if((val >> 26) == ip->opcode && (val & 0xFFFF) == ip->opcode2)
  56. return(1);
  57. return(0);
  58. }
  59.  
  60. static int op21_6(struct instr *ip, unsigned long val)
  61. {
  62. if((val >> 21) == ip->opcode && (val & 0x3F) == ip->opcode2)
  63. return(1);
  64. return(0);
  65. }
  66.  
  67. static int op5___11(struct instr *ip, unsigned long val)
  68. {
  69. if((val >> 26) == ip->opcode && (val & 0x7FF) == ip->opcode2)
  70. return(1);
  71. return(0);
  72. }
  73.  
  74. static int op26_5(struct instr *ip, unsigned long val)
  75. {
  76. if((val >> 26) == ip->opcode && (val & 0x3F) == ip->opcode2)
  77. return(1);
  78. return(0);
  79. }
  80.  
  81. static int op26_16_11(struct instr *ip, unsigned long val)
  82. {
  83. if((val >> 26) == ip->opcode && (val & 0x001F07FF) == ip->opcode2)
  84. return(1);
  85. return(0);
  86. }
  87.  
  88. static int op26_21(struct instr *ip, unsigned long val)
  89. {
  90. if((val >> 26) == ip->opcode && (val & 0x001FFFFF) == ip->opcode2)
  91. return(1);
  92. return(0);
  93. }
  94.  
  95. static int op_r21(struct instr *ip, unsigned long val)
  96. {
  97. sprintf(*opp, "r%d", (val >> 21) & 0x1F);
  98. return(1);
  99. }
  100.  
  101. static int op_r16(struct instr *ip, unsigned long val)
  102. {
  103. sprintf(*opp, "r%d", (val >> 16) & 0x1F);
  104. return(1);
  105. }
  106.  
  107. static int op_r11(struct instr *ip, unsigned long val)
  108. {
  109. sprintf(*opp, "r%d", (val >> 11) & 0x1F);
  110. return(1);
  111. }
  112.  
  113. static int op_i16(struct instr *ip, unsigned long val)
  114. {
  115. long lv;
  116.  
  117. lv = (long)val;
  118. lv <<= 16;
  119. lv >>= 16;
  120. sprintf(*opp, "#0x%lx", lv);
  121. return(1);
  122. }
  123.  
  124. static int op_u16(struct instr *ip, unsigned long val)
  125. {
  126. sprintf(*opp, "#0x%lx", val & 0xffff);
  127. return(1);
  128. }
  129.  
  130. static int op_i6(struct instr *ip, unsigned long val)
  131. {
  132. sprintf(*opp, "#%ld", (val >> 6) & 0x1f);
  133. return(1);
  134. }
  135.  
  136. static int op_h16(struct instr *ip, unsigned long val)
  137. {
  138. sprintf(*opp, "#0x%lx", val << 16);
  139. return(1);
  140. }
  141.  
  142. static int op_b21(struct instr *ip, unsigned long val)
  143. {
  144. long lv;
  145.  
  146. lv = (long)val;
  147. lv <<= 16;
  148. lv >>= 16;
  149. sprintf(*opp, "%ld(r%d)", lv, (val >> 21) & 0x1F);
  150. return(1);
  151. }
  152.  
  153. static int op_p16(struct instr *ip, unsigned long val)
  154. {
  155. long lv;
  156.  
  157. lv = (long)val;
  158. lv <<= 16;
  159. lv >>= 14;
  160. lv += cur_pc + 4;
  161. sprintf(*opp, "0x%lx", lv);
  162. return(1);
  163. }
  164.  
  165. static int op_p26(struct instr *ip, unsigned long val)
  166. {
  167. long lv;
  168.  
  169. lv = (long)val & 0x03FFFFFF;
  170. lv <<= 2;
  171. lv |= ((cur_pc + 4) & 0xF0000000);
  172. sprintf(*opp, "0x%lx", lv);
  173. return(1);
  174. }
  175.  
  176. static int op_null(struct instr *ip, unsigned long val)
  177. {
  178. return(0);
  179. }
  180.  
  181. struct instr itab[] = {
  182. { "add", 0, 0x20, op5___11, op_r11, op_r21, op_r16 },
  183. { "addi", 8, 0x00, op26, op_r16, op_r21, op_i16 },
  184. { "addiu", 9, 0x00, op26, op_r16, op_r21, op_i16 },
  185. { "addu", 0, 0x21, op5___11, op_r11, op_r21, op_r16 },
  186. { "and", 0, 0x24, op5___11, op_r11, op_r21, op_r16 },
  187. { "andi", 0xc, 0, op26, op_r16, op_r21, op_u16 },
  188. { "beq", 0x4, 0, op26, op_r21, op_r16, op_p16 },
  189. { "beql", 0x14, 0, op26, op_r21, op_r16, op_p16 },
  190. { "bgez", 0x1, 0x1, op26_16, op_r21, op_p16, op_null },
  191. { "bgezal", 0x1, 0x11, op26_16, op_r21, op_p16, op_null },
  192. { "bgezall", 0x1, 0x13, op26_16, op_r21, op_p16, op_null },
  193. { "bgezl", 0x1, 0x03, op26_16, op_r21, op_p16, op_null },
  194. { "bgtz", 0x7, 0x0, op26_16, op_r21, op_p16, op_null },
  195. { "bgtzl", 0x17, 0x0, op26_16, op_r21, op_p16, op_null },
  196. { "blez", 0x6, 0x0, op26_16, op_r21, op_p16, op_null },
  197. { "blezl", 0x16, 0x0, op26_16, op_r21, op_p16, op_null },
  198. { "bltz", 0x1, 0x0, op26_16, op_r21, op_p16, op_null },
  199. { "bltzal", 0x1, 0x10, op26_16, op_r21, op_p16, op_null },
  200. { "bltzall", 0x1, 0x12, op26_16, op_r21, op_p16, op_null },
  201. { "bne", 0x5, 0x0, op26, op_r21, op_r16, op_p16 },
  202. { "bnel", 0x15, 0x0, op26, op_r21, op_r16, op_p16 },
  203. /* { "break", 0x15, 0x0, op26_16, op_r21, op_r16, op_p16 }, */
  204. /* { "cache", 0x15, 0x0, op26_16, op_r21, op_r16, op_p16 }, */
  205. { "div", 0, 0x1a, op26__16, op_r21, op_r16, op_null },
  206. { "divu", 0, 0x1b, op26__16, op_r21, op_r16, op_null },
  207. { "j", 2, 0, op26, op_p26, op_null, op_null },
  208. { "jal", 3, 0, op26, op_p26, op_null, op_null },
  209. { "jalr", 0, 0x9, op26_16_11, op_r11, op_r21, op_null },
  210. { "jr", 0, 0x8, op26_21, op_r21, op_null, op_null },
  211. { "lb", 0x20, 0, op26, op_r16, op_b21, op_null },
  212. { "lbu", 0x24, 0, op26, op_r16, op_b21, op_null },
  213. { "lh", 0x21, 0, op26, op_r16, op_b21, op_null },
  214. { "lhu", 0x25, 0, op26, op_r16, op_b21, op_null },
  215. { "ll", 0x30, 0, op26, op_r16, op_b21, op_null },
  216. { "lui", 0x0F, 0, op26, op_r16, op_h16, op_null },
  217. { "lw", 0x23, 0, op26, op_r16, op_b21, op_null },
  218. { "lwu", 0x2F, 0, op26, op_r16, op_b21, op_null },
  219. { "lwl", 0x22, 0, op26, op_r16, op_b21, op_null },
  220. { "lwr", 0x26, 0, op26, op_r16, op_b21, op_null },
  221. { "mult", 0, 0x18, op26__16, op_r21, op_r16, op_null },
  222. { "multu", 0, 0x19, op26__16, op_r21, op_r16, op_null },
  223. { "nor", 0, 0x27, op5___11, op_r11, op_r21, op_r16 },
  224. { "or", 0, 0x25, op5___11, op_r11, op_r21, op_r16 },
  225. { "ori", 0xd, 0, op26, op_r16, op_r21, op_u16 },
  226. { "sb", 0x28, 0, op26, op_r16, op_b21, op_null },
  227. { "sc", 0x38, 0, op26, op_r16, op_b21, op_null },
  228. { "sh", 0x29, 0, op26, op_r16, op_b21, op_null },
  229. { "sll", 0, 0, op26_5, op_r11, op_r16, op_i6 },
  230. { "sllv", 0, 4, op5___11, op_r11, op_r16, op_r21 },
  231. { "slt", 0, 0x2a, op5___11, op_r11, op_r21, op_r16 },
  232. { "slti", 0xa, 0, op26, op_r16, op_r21, op_i16 },
  233. { "sltiu", 0xb, 0, op26, op_r16, op_r21, op_i16 },
  234. { "sltu", 0, 0x2b, op5___11, op_r11, op_r21, op_r16 },
  235. { "sra", 0, 3, op21_6, op_r11, op_r16, op_i6 },
  236. { "srav", 0, 7, op5___11, op_r11, op_r16, op_r21 },
  237. { "srl", 0, 2, op21_6, op_r11, op_r16, op_i6 },
  238. { "srlv", 0, 6, op5___11, op_r11, op_r16, op_r21 },
  239. { "sub", 0, 0x22, op5___11, op_r11, op_r21, op_r16 },
  240. { "subu", 0, 0x23, op5___11, op_r11, op_r21, op_r16 },
  241. { "sw", 0x2b, 0, op26, op_r16, op_b21, op_null },
  242. { "swl", 0x2a, 0, op26, op_r16, op_b21, op_null },
  243. { "swr", 0x2e, 0, op26, op_r16, op_b21, op_null },
  244. { "sync", 0xf, 0, op32, op_null, op_null, op_null },
  245. { "syscall", 0xc, 0, op32, op_null, op_null, op_null },
  246. { "xor", 0, 0x26, op5___11, op_r11, op_r21, op_r16 },
  247. { "xori", 0x0e, 0, op26, op_r16, op_r21, op_u16 },
  248. { 0 }
  249. };
  250.  
  251. char *mips_disass(unsigned long addr, unsigned long val)
  252. {
  253. struct instr *ip;
  254. int i;
  255. char *sep;
  256.  
  257. for(i = 0; i < 8; ++i) {
  258. ops[i][0] = 0;
  259. op[i] = ops[i];
  260. }
  261. opp = op;
  262. cur_pc = addr;
  263. for(ip = itab; ip->name; ++ip) {
  264. if(ip->optype && ip->optype(ip, val)) {
  265. strcpy(opcode, ip->name);
  266. if(ip->op1(ip, val))
  267. ++opp;
  268. if(ip->op2(ip, val))
  269. ++opp;
  270. if(ip->op3(ip, val))
  271. ++opp;
  272. break;
  273. }
  274. }
  275. *op = 0; /* end of arguments */
  276. if(!ip->name) {
  277. sprintf(opcode, "Unknown opcode");
  278. sprintf(ops[0], "0x%08lx", val);
  279. ops[1][0] = 0;
  280. }
  281. strcpy(mneu, opcode);
  282. sep = " ";
  283. for(i = 0; ops[i][0]; ++i) {
  284. strcat(mneu, sep);
  285. strcat(mneu, ops[i]);
  286. sep = ",";
  287. }
  288. return(mneu);
  289. }

Also, this looks like it can only generate the machine code, rather than C code?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: decompiler won't compile!

 
0
  #2
Nov 24th, 2006
its nearly impossible to take a compiled executable and reconstruct the c program. how is a decompiler even supposed to know the original source was written in c, or c++, or basic, or fortran, or ... ? Also it is not possible for it to reconstruct the structures that may have been used. So I think you are wasting your time if you expect the decompiler to generate c code for you. Just not going to happen.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 100
Reputation: manutd is an unknown quantity at this point 
Solved Threads: 1
manutd's Avatar
manutd manutd is offline Offline
Junior Poster

Re: decompiler won't compile!

 
0
  #3
Nov 24th, 2006
It would be nearly imposible to reconstruct C code as all compilers will compile it slightly different. Also, the same compiler might do it differently twice.
Silence is better than unmeaning words.
- Pythagoras
My blog
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: decompiler won't compile!

 
0
  #4
Nov 24th, 2006
> Also, this looks like it can only generate the machine code, rather than C code?
True, and it's also specific to the MIPS processor as well, which means that at best, it will only turn a MIPS a.out file back into MIPS assembly, which might give you a chance to reassemble it to MIPS elf format.

> It comes up with a ton of warnings and an undefined reference error for the mneu variable
This file is most likely just one of MANY files in the whole project. It also lacks a main() for example.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC