| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov, Jorge Acereda Macia. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: tools.disassembler namespaces combinators | 
					
						
							|  |  |  | alien alien.syntax alien.c-types lexer parser kernel | 
					
						
							| 
									
										
										
										
											2009-03-26 00:00:19 -04:00
										 |  |  | sequences layouts math math.order alien.libraries | 
					
						
							| 
									
										
										
										
											2009-06-22 17:06:07 -04:00
										 |  |  | math.parser system make fry arrays libc destructors | 
					
						
							| 
									
										
										
										
											2009-09-18 18:57:50 -04:00
										 |  |  | tools.disassembler.utils splitting alien.data | 
					
						
							|  |  |  | classes.struct ;
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | IN: tools.disassembler.udis | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | << | 
					
						
							|  |  |  | "libudis86" { | 
					
						
							|  |  |  |     { [ os macosx? ] [ "libudis86.0.dylib" ] } | 
					
						
							|  |  |  |     { [ os unix? ] [ "libudis86.so.0" ] } | 
					
						
							|  |  |  |     { [ os winnt? ] [ "libudis86.dll" ] } | 
					
						
							|  |  |  | } cond "cdecl" add-library | 
					
						
							|  |  |  | >> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LIBRARY: libudis86 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-18 18:57:50 -04:00
										 |  |  | STRUCT: ud_operand | 
					
						
							|  |  |  |     { type int } | 
					
						
							|  |  |  |     { size uchar } | 
					
						
							|  |  |  |     { lval ulonglong } | 
					
						
							|  |  |  |     { base int } | 
					
						
							|  |  |  |     { index int } | 
					
						
							|  |  |  |     { offset uchar } | 
					
						
							|  |  |  |     { scale uchar } ;
 | 
					
						
							| 
									
										
										
										
											2009-05-05 13:36:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-18 18:57:50 -04:00
										 |  |  | STRUCT: ud | 
					
						
							|  |  |  |     { inp_hook void* } | 
					
						
							|  |  |  |     { inp_curr uchar } | 
					
						
							|  |  |  |     { inp_fill uchar } | 
					
						
							| 
									
										
										
										
											2009-09-27 00:14:57 -04:00
										 |  |  |     { inp_file void* } | 
					
						
							| 
									
										
										
										
											2009-09-18 18:57:50 -04:00
										 |  |  |     { inp_ctr uchar } | 
					
						
							|  |  |  |     { inp_buff uchar* } | 
					
						
							|  |  |  |     { inp_buff_end uchar* } | 
					
						
							|  |  |  |     { inp_end uchar } | 
					
						
							|  |  |  |     { translator void* } | 
					
						
							|  |  |  |     { insn_offset ulonglong } | 
					
						
							|  |  |  |     { insn_hexcode char[32] } | 
					
						
							|  |  |  |     { insn_buffer char[64] } | 
					
						
							|  |  |  |     { insn_fill uint } | 
					
						
							|  |  |  |     { dis_mode uchar } | 
					
						
							|  |  |  |     { pc ulonglong } | 
					
						
							|  |  |  |     { vendor uchar } | 
					
						
							|  |  |  |     { mapen void* } | 
					
						
							|  |  |  |     { mnemonic int } | 
					
						
							|  |  |  |     { operand ud_operand[3] } | 
					
						
							|  |  |  |     { error uchar } | 
					
						
							|  |  |  |     { pfx_rex uchar } | 
					
						
							|  |  |  |     { pfx_seg uchar } | 
					
						
							|  |  |  |     { pfx_opr uchar } | 
					
						
							|  |  |  |     { pfx_adr uchar } | 
					
						
							|  |  |  |     { pfx_lock uchar } | 
					
						
							|  |  |  |     { pfx_rep uchar } | 
					
						
							|  |  |  |     { pfx_repe uchar } | 
					
						
							|  |  |  |     { pfx_repne uchar } | 
					
						
							|  |  |  |     { pfx_insn uchar } | 
					
						
							|  |  |  |     { default64 uchar } | 
					
						
							|  |  |  |     { opr_mode uchar } | 
					
						
							|  |  |  |     { adr_mode uchar } | 
					
						
							|  |  |  |     { br_far uchar } | 
					
						
							|  |  |  |     { br_near uchar } | 
					
						
							|  |  |  |     { implicit_addr uchar } | 
					
						
							|  |  |  |     { c1 uchar } | 
					
						
							|  |  |  |     { c2 uchar } | 
					
						
							|  |  |  |     { c3 uchar } | 
					
						
							|  |  |  |     { inp_cache uchar[256] } | 
					
						
							|  |  |  |     { inp_sess uchar[64] } | 
					
						
							| 
									
										
										
										
											2009-09-27 00:14:57 -04:00
										 |  |  |     { itab_entry void* } ;
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | FUNCTION: void ud_translate_intel ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: void ud_translate_att ( ud* u ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-15 20:44:56 -05:00
										 |  |  | : UD_SYN_INTEL ( -- addr ) &: ud_translate_intel ; inline
 | 
					
						
							|  |  |  | : UD_SYN_ATT ( -- addr ) &: ud_translate_att ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-22 20:13:08 -05:00
										 |  |  | CONSTANT: UD_EOI          -1
 | 
					
						
							|  |  |  | CONSTANT: UD_INP_CACHE_SZ 32
 | 
					
						
							|  |  |  | CONSTANT: UD_VENDOR_AMD   0
 | 
					
						
							|  |  |  | CONSTANT: UD_VENDOR_INTEL 1
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | FUNCTION: void ud_init ( ud* u ) ;
 | 
					
						
							| 
									
										
										
										
											2009-03-22 23:19:40 -04:00
										 |  |  | FUNCTION: void ud_set_mode ( ud* u, uchar mode ) ;
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | FUNCTION: void ud_set_pc ( ud* u, ulonglong pc ) ;
 | 
					
						
							| 
									
										
										
										
											2009-03-22 23:19:40 -04:00
										 |  |  | FUNCTION: void ud_set_input_buffer ( ud* u, uchar* offset, size_t size ) ;
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | FUNCTION: void ud_set_vendor ( ud* u, uint vendor ) ;
 | 
					
						
							|  |  |  | FUNCTION: void ud_set_syntax ( ud* u, void* syntax ) ;
 | 
					
						
							|  |  |  | FUNCTION: void ud_input_skip ( ud* u, size_t size ) ;
 | 
					
						
							|  |  |  | FUNCTION: int ud_input_end ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: uint ud_decode ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: uint ud_disassemble ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: char* ud_insn_asm ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: void* ud_insn_ptr ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: ulonglong ud_insn_off ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: char* ud_insn_hex ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: uint ud_insn_len ( ud* u ) ;
 | 
					
						
							|  |  |  | FUNCTION: char* ud_lookup_mnemonic ( int c ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <ud> ( -- ud )
 | 
					
						
							| 
									
										
										
										
											2009-09-18 18:57:50 -04:00
										 |  |  |     ud malloc-struct &free | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  |     dup ud_init | 
					
						
							|  |  |  |     dup cell-bits ud_set_mode | 
					
						
							|  |  |  |     dup UD_SYN_INTEL ud_set_syntax ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 03:44:29 -04:00
										 |  |  | : with-ud ( quot: ( ud -- ) -- )
 | 
					
						
							| 
									
										
										
										
											2009-06-22 17:06:07 -04:00
										 |  |  |     [ [ [ <ud> ] dip call ] with-destructors ] with-words-xt ; inline
 | 
					
						
							| 
									
										
										
										
											2009-05-01 03:44:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | SINGLETON: udis-disassembler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : buf/len ( from to -- buf len ) [ drop <alien> ] [ swap - ] 2bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-22 17:06:07 -04:00
										 |  |  | : resolve-call ( str -- str' ) "0x" split1-last [ resolve-xt append ] when* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | : format-disassembly ( lines -- lines' )
 | 
					
						
							| 
									
										
										
										
											2009-02-03 01:27:34 -05:00
										 |  |  |     dup [ second length ] [ max ] map-reduce
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  |     '[ | 
					
						
							|  |  |  |         [ | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |             [ first >hex cell 2 * CHAR: 0 pad-head % ": " % ] | 
					
						
							|  |  |  |             [ second _ CHAR: \s pad-tail % "  " % ] | 
					
						
							| 
									
										
										
										
											2009-06-22 17:06:07 -04:00
										 |  |  |             [ third resolve-call % ] | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  |             tri
 | 
					
						
							|  |  |  |         ] "" make | 
					
						
							|  |  |  |     ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (disassemble) ( ud -- lines )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         dup '[ | 
					
						
							|  |  |  |             _ ud_disassemble 0 =
 | 
					
						
							|  |  |  |             [ f ] [ | 
					
						
							|  |  |  |                 _ | 
					
						
							|  |  |  |                 [ ud_insn_off ] | 
					
						
							|  |  |  |                 [ ud_insn_hex ] | 
					
						
							|  |  |  |                 [ ud_insn_asm ] | 
					
						
							|  |  |  |                 tri 3array , t
 | 
					
						
							|  |  |  |             ] if
 | 
					
						
							|  |  |  |         ] loop
 | 
					
						
							|  |  |  |     ] { } make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: udis-disassembler disassemble* ( from to -- buffer )
 | 
					
						
							| 
									
										
										
										
											2009-05-01 03:44:29 -04:00
										 |  |  |     '[ | 
					
						
							|  |  |  |         _ _ | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  |         [ drop ud_set_pc ] | 
					
						
							|  |  |  |         [ buf/len ud_set_input_buffer ] | 
					
						
							|  |  |  |         [ 2drop (disassemble) format-disassembly ] | 
					
						
							| 
									
										
										
										
											2009-05-01 03:44:29 -04:00
										 |  |  |         3tri
 | 
					
						
							|  |  |  |     ] with-ud ;
 | 
					
						
							| 
									
										
										
										
											2008-12-10 20:35:18 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | udis-disassembler disassembler-backend set-global
 |