【说明】:近来发现EMACS LISP的插件比较有趣,可以很好地提升工作效率,但是已有的插件不一定能满足编程者的个性化要求,需要自己定制,正好看到这篇文档,提供了一个简单的插件写作实例,可以对照着学习。
Step@88
2007-12-28
写了个emacs的插件 objdump的封装,dump elf - [Emacs]
没找到相关的封装插件,索性就自己个了,比较简单,不过可以学学简单的写emacs 插件的中的一些要素,比如 defface ,defcustom, defvar,以及定义自己的major mode,key map 等等,还有学习一下函数,interactive等等,还是不错.
现在来说说elf-mode 是如何用的吧,把elf.el 放入你的load path 或者 (load "/your/path/to/elf.el") 放入.emacs中,再在.eamcs中添加
(require 'elf)
(setq elf-cmd-tool "objdump")
(global-set-key (kbd "<f5> o") 'elf-dump) ; 你也可以直接 M-x elf-dump
在elf-mode 中的键绑定(基本上对应objdump的参数):
比如你要 objdump -h hello.o 那么你只要在elf-mode buffer 里面 按 h 键就可以了,其它也是如此 如 -r ,-R, -t,-T,-x,-p 等等。
还有定义了两个鼠标按键, mouse-1 显示 section 以及十六进制, mouse-2 直接显示 section
还有比如你按j ,那么他会提示你输入 section name,如果有相应的section 那么会反汇编 dump 出来。
需要注意的是 elf-default-arch 我默认的是 i386 elf-bdf-target 默认 elf32-i386 ,这些你可以通过 customize 或则 setq 定制.
文件的下载: http://fxl.blogbus.com/files/11988303050.el
收藏到: Del.icio.us
from : http://fxl.blogbus.com/logs/12845439.html
附上该作者写的 elf.el
;;; elf.el --- show elf file with objdump
;; Copyright (C) 2007 step
;; Author: step <fxlzju@gmail.com>
;; Version:0.1
;; Keywords: elf,file
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Install:
;; put the 'elf.el' to your load path
;; add follow sentences to your .emacs
;; (require 'elf)
;; (setq elf-cmd-tool "objdump")
;;
;;; Code:
(provide 'elf)
(defface elf-header
'((t (:bold t :foreground "blue")))
"elf title header"
:group 'elf)
(defcustom elf-cmd-tool nil
"the shell program to display elf file"
:type 'string
:group 'elf)
(defcustom elf-default-flag "-x"
"the default flags to object"
:type 'string
:group 'elf)
(defcustom elf-bfd-target "elf32-i386"
"the default bfd target"
:type 'string
:group 'elf)
(defcustom elf-default-arch "i386"
"the default arch"
:type 'string
:group 'elf)
(defvar elf-mode-map nil
"keymap for elf mode")
(if elf-mode-map
nil
(setq elf-mode-map (make-sparse-keymap))
(suppress-keymap elf-mode-map)
(let ((map elf-mode-map))
(define-key map "h" 'elf-dump-sections-table)
(define-key map "t" 'elf-dump-symbol-table)
(define-key map "T" 'elf-dump-dsymbol-table)
(define-key map "d" 'elf-dump-assemble)
(define-key map "D" 'elf-dump-assemble-all)
(define-key map "f" 'elf-dump-file-header)
(define-key map "p" 'elf-dump-private-header)
(define-key map "r" 'elf-dump-reloc)
(define-key map "R" 'elf-dump-dyrelocate)
(define-key map "s" 'elf-dump-full-content)
(define-key map "j" 'elf-dump-section)
(define-key map "i" 'elf-dump-info)
(define-key map "g" 'elf-dump-debug)
(define-key map "x" 'elf-dump-all-header)
(define-key map "b" 'elf-dump-addr)
(define-key map "q" 'bury-buffer)
(define-key map "k" 'elf-kill-buffer)
(define-key map [mouse-2] 'elf-dump-mouse-2)
(define-key map [mouse-1] 'elf-dump-mouse-1)
)
)
;;;; elf-mode
(defun elf-mode()
"ELF mode for object file"
(setq major-mode 'elf-mode)
(setq mode-name "ELF")
(setq buffer-read-only t)
(use-local-map elf-mode-map)
(run-hooks 'elf-mode-hook)
)
;;;; global variable
(setq elf-file-map nil)
;;;; dump the elf with default flags -x
(defun elf-dump (object)
"show elf file with elf-mode"
(interactive "fObject file:")
(let (cmdstr bname default)
(setq bname (concat "ELF-" (file-name-nondirectory object)))
(switch-to-buffer bname)
(if (< (buffer-size) 10)
(progn
(setq default (concat "-b " elf-bfd-target " -m " elf-default-arch))
(setq cmdstr (concat elf-cmd-tool " " default " " elf-default-flag " " object))
(shell-command cmdstr (current-buffer))
(elf-font-modify "\\([.].*?[[:blank:]]\\|^.*?[::]\\)")
))
(setq elf-file-map (append elf-file-map (list (cons bname object))))
(elf-mode)
(set-buffer-modified-p nil))
)
;;;; change the font
(defun elf-font-modify (regex)
"modify the font display according"
(save-excursion
(goto-char (point-min))
(while (re-search-forward regex nil t)
(put-text-property (match-beginning 0) (match-end 0) 'face 'elf-header)
(put-text-property (match-beginning 0) (match-end 0) 'mouse-face 'highlight))
)
(setq buffer-read-only t)
(set-buffer-modified-p nil))
;;;; elf dump do
(defun elf-dump-do (flags)
"dump the elf file with the flag"
(let (cmdstr object default)
(setq default (concat "-b " elf-bfd-target " -m " elf-default-arch))
(setq object (cdr (assoc (buffer-name) elf-file-map)))
(setq cmdstr (concat elf-cmd-tool " " default " " flags " " object))
(shell-command cmdstr (current-buffer))
))
;;;; show elf sections table
(defun elf-dump-sections-table ()
"dump the elf file section table"
(interactive)
(elf-dump-do "-h")
(elf-font-modify "\\([.].*?[[:blank:]]\\|^.*?[::]\\)")
)
;;;; elf dump symbol tables
(defun elf-dump-symbol-table ()
"dump the elf file symbol table"
(interactive)
(elf-dump-do "-t")
(elf-font-modify "\\([.].*?[[:blank:]]\\|^.*?[::]\\)")
)
;;;; elf dump dynamic symbol tables
(defun elf-dump-dsymbol-table ()
"dump the elf file dynamic symbol table"
(interactive)
(elf-dump-do "-T")
(elf-font-modify "\\([.].*?[[:blank:]]\\|^.*?[::]\\)"))
;;;; elf dump disassemble
(defun elf-dump-assemble ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-d")
(elf-font-modify "^.*?[::]"))
;;;; elf dump disassemble all
(defun elf-dump-assemble-all ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-D")
(elf-font-modify "^.*?[::]"))
;;;; elf dump file header
(defun elf-dump-file-header ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-f")
(elf-font-modify "^.*?[::]"))
;;;; elf dump private header
(defun elf-dump-private-header ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-p")
(elf-font-modify "^.*?[::]"))
;;;; elf dump relocate section
(defun elf-dump-reloc ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-r")
(elf-font-modify "^.*?[::]"))
;;;; elf dump dynamic reloc
(defun elf-dump-dyrelocate ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-R")
(elf-font-modify "^.*?[::]"))
;;;; elf dump full hexl content
(defun elf-dump-full-content ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-s")
(elf-font-modify "^.*?[::]"))
;;;; elf dump info
(defun elf-dump-info ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-i")
(elf-font-modify "^.*?[::]"))
;;;; elf dump debug info
(defun elf-dump-debug ()
"dump elf file disassemble"
(interactive)
(elf-dump-do "-g")
(elf-font-modify "^.*?[::]"))
;;;; elf dump section
(defun elf-dump-section (name)
"elf dump setcion"
(interactive "sSection:")
(elf-dump-section-do "-d -j" name)
)
;;;; elf dump section do
(defun elf-dump-section-do (flag name)
"elf dump section do"
(elf-dump-do (concat flag name))
(elf-font-modify "^.*?[::]")
)
;;; elf dump all header
(defun elf-dump-all-header ()
"dump all header"
(interactive)
(elf-dump-do elf-default-flag)
(elf-font-modify "\\([.].*?[[:blank:]]\\|^.*?[::]\\)")
)
;;;; elf dump with mouse 2
(defun elf-dump-mouse-2 (click)
"mouse 2 event"
(interactive "e")
(if (featurep 'xemacs)
nil
(goto-char (car (cdr (event-start click))))) ;;;; get the point
(save-excursion
(beginning-of-line)
(and (re-search-forward "[.].*?[[:blank:]]" (min (point-max) (+ (point) 40)) t)
(elf-dump-section-do "-d -j" (buffer-substring (match-beginning 0) (match-end 0)))
))
)
;;;; elf dump with mouse 1
(defun elf-dump-mouse-1 (click)
"mouse 1 event"
(interactive "e")
(if (featurep 'xemacs)
nil
(goto-char (car (cdr (event-start click))))) ;;;; get the point
(save-excursion
(beginning-of-line)
(and (re-search-forward "[.].*?[[:blank:]]" (min (point-max) (+ (point) 40)) t)
(elf-dump-section-do "-d -s -j" (buffer-substring (match-beginning 0) (match-end 0)))
)))
;;;; elf dump with gived address
(defun elf-dump-addr (beg end)
"dump with gived address"
(interactive "sBegin Addr:\nsEnd Addr:")
(let (flag)
(setq flag (concat "--start-address=" beg " --stop-address=" end " -d"))
(elf-dump-do flag)
(elf-font-modify "^.*?[::]")
))
;;;; elf-kill-buffer
(defun elf-kill-buffer ()
"kill the current elf buffer"
(interactive)
(kill-buffer (current-buffer)))
;;; elf.el ends here