1 ! Copyright (C) 2008 Slava Pestov, Jorge Acereda Macia.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: tools.disassembler namespaces combinators
4 alien alien.syntax alien.c-types lexer parser kernel
5 sequences layouts math math.parser system make fry arrays ;
6 IN: tools.disassembler.udis
10 { [ os macosx? ] [ "libudis86.0.dylib" ] }
11 { [ os unix? ] [ "libudis86.so.0" ] }
12 { [ os winnt? ] [ "libudis86.dll" ] }
13 } cond "cdecl" add-library
20 FUNCTION: void ud_translate_intel ( ud* u ) ;
21 FUNCTION: void ud_translate_att ( ud* u ) ;
23 : UD_SYN_INTEL ( -- addr ) &: ud_translate_intel ; inline
24 : UD_SYN_ATT ( -- addr ) &: ud_translate_att ; inline
27 : UD_INP_CACHE_SZ 32 ; inline
28 : UD_VENDOR_AMD 0 ; inline
29 : UD_VENDOR_INTEL 1 ; inline
31 FUNCTION: void ud_init ( ud* u ) ;
32 FUNCTION: void ud_set_mode ( ud* u, uint8_t mode ) ;
33 FUNCTION: void ud_set_pc ( ud* u, ulonglong pc ) ;
34 FUNCTION: void ud_set_input_buffer ( ud* u, uint8_t* offset, size_t size ) ;
35 FUNCTION: void ud_set_vendor ( ud* u, uint vendor ) ;
36 FUNCTION: void ud_set_syntax ( ud* u, void* syntax ) ;
37 FUNCTION: void ud_input_skip ( ud* u, size_t size ) ;
38 FUNCTION: int ud_input_end ( ud* u ) ;
39 FUNCTION: uint ud_decode ( ud* u ) ;
40 FUNCTION: uint ud_disassemble ( ud* u ) ;
41 FUNCTION: char* ud_insn_asm ( ud* u ) ;
42 FUNCTION: void* ud_insn_ptr ( ud* u ) ;
43 FUNCTION: ulonglong ud_insn_off ( ud* u ) ;
44 FUNCTION: char* ud_insn_hex ( ud* u ) ;
45 FUNCTION: uint ud_insn_len ( ud* u ) ;
46 FUNCTION: char* ud_lookup_mnemonic ( int c ) ;
51 dup cell-bits ud_set_mode
52 dup UD_SYN_INTEL ud_set_syntax ;
54 SINGLETON: udis-disassembler
56 : buf/len ( from to -- buf len ) [ drop <alien> ] [ swap - ] 2bi ;
58 : format-disassembly ( lines -- lines' )
59 dup [ second length ] map supremum
62 [ first >hex cell 2 * CHAR: 0 pad-head % ": " % ]
63 [ second _ CHAR: \s pad-tail % " " % ]
69 : (disassemble) ( ud -- lines )
83 M: udis-disassembler disassemble* ( from to -- buffer )
86 [ buf/len ud_set_input_buffer ]
87 [ 2drop (disassemble) format-disassembly ]
90 udis-disassembler disassembler-backend set-global