2010年9月17日金曜日

ELispでobjdumpを呼んでバイト列を逆アセンブルする

Linuxなどでプログラムを逆アセンブルする際には、objdumpを使うと便利です、たぶん。

Twitterなどでバイナリアンな方々が16進数で会話をしているのについていけなくて困るときのために、 Emacsからobjdumpを呼んでバイト列を逆アセンブルするようなELispを書いてみました。

バイト列といってもリストしか対応していませんが。

(defvar *arch-type* "i386")

(defun make-disasm-command (target file)
(format "objdump -b binary -m %s -D %s" target file))

(defun make-perl-command (lst tmp-file)
(concat
"perl -e 'print \""
(apply
'concat
(mapcar
(lambda (n)
(format "\\x%x" n))
lst))
"\"' >"
tmp-file))


(defun disasm-byte-list (lst)
(let ((tmp-file (make-temp-file "disasm_")))
(let ((file-buf (find-file-noselect tmp-file))
(cmd (make-disasm-command *arch-type* tmp-file)))
(shell-command (make-perl-command lst tmp-file))
(let ((buf (get-buffer-create "*disasm*")))
(shell-command cmd buf)
(delete-file tmp-file)
(pop-to-buffer buf)))))

;; example
(disasm-byte-list '(#x90))

0 件のコメント:

コメントを投稿