forked from raeburn/emacs-objdump-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjdump.el
564 lines (485 loc) · 19.5 KB
/
objdump.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
;;; objdump.el --- Disassemble and browse code -*- lexical-binding: t -*-
;; Author: raeburn
;; Keywords: tools
;;; Code:
(require 'cl-lib)
(require 'hexl)
;;; Faces
(defgroup objdump nil
"Major mode for viewing object file disassembly."
:group 'tools)
(defface objdump-address-face
'((t :inherit font-lock-constant-face))
"Face for memory addresses."
:group 'objdump-faces)
(defface objdump-symbol-face
'((t :inherit font-lock-function-name-face))
"Face for symbol names."
:group 'objdump-faces)
(defface objdump-instruction-face
'((t :inherit font-lock-keyword-face :weight bold))
"Face for assembly instructions."
:group 'objdump-faces)
(defface objdump-register-face
'((t :inherit font-lock-variable-name-face))
"Face for CPU registers."
:group 'objdump-faces)
(defface objdump-immediate-face
'((t :inherit font-lock-constant-face))
"Face for immediate values."
:group 'objdump-faces)
(defface objdump-comment-face
'((t :inherit font-lock-comment-face))
"Face for comments and file info."
:group 'objdump-faces)
;;; Variables
(defvar objdump-file-name nil
"Name of object file currently being examined with objdump-mode, if any.")
(make-variable-buffer-local 'objdump-file-name)
(defvar objdump-binary-buffer nil
"Buffer containing the binary file in hexl-mode.")
(make-variable-buffer-local 'objdump-binary-buffer)
(defvar objdump-symbol-table nil
"Symbol table for current buffer.")
(make-variable-buffer-local 'objdump-symbol-table)
(defcustom objdump-command "objdump"
"Command to run to disassemble object file"
:type 'string
:group 'objdump)
(defvar objdump-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "s" 'imenu)
(define-key map "i" 'imenu)
(define-key map "g" 'objdump-revert)
(define-key map "p" 'objdump-previous-function)
(define-key map "n" 'objdump-next-function)
(define-key map "q" 'kill-this-buffer)
(define-key map (kbd "C-n") 'objdump-next-line)
(define-key map (kbd "C-p") 'objdump-previous-line)
(define-key map (kbd "C-f") 'objdump-forward-byte)
(define-key map (kbd "C-b") 'objdump-backward-byte)
(define-key map (kbd "C-a") 'objdump-move-beginning-of-line)
(define-key map (kbd "C-e") 'objdump-move-end-of-line)
(define-key map (kbd "RET") 'objdump-visit-address-in-hexl)
map)
"Keymap for `objdump-mode'.")
(defun objdump-get-address-at-point ()
"Extract the hexadecimal address from the current line in objdump output.
Returns nil if no address is found."
(save-excursion
(beginning-of-line)
(when (looking-at "^ *\\([0-9a-f]+\\):")
(string-to-number (match-string 1) 16))))
(defun objdump-ensure-hexl-buffer ()
"Ensure we have a hexl-mode buffer for the binary file.
Returns the buffer or nil if the binary file cannot be found."
(unless (and objdump-binary-buffer
(buffer-live-p objdump-binary-buffer))
(when (and objdump-file-name
(file-exists-p objdump-file-name)) ; Add check for file existence
(let ((buf (find-file-noselect objdump-file-name)))
(with-current-buffer buf
(unless (eq major-mode 'hexl-mode)
(hexl-mode))
(setq objdump-binary-buffer buf)))))
objdump-binary-buffer)
(defun objdump-get-byte-offset-in-line ()
"Get the byte offset from the start of the line based on point position.
Returns nil if not on a hex byte."
(when-let* ((range (objdump--get-hex-range))
(start (car range))
(end (cdr range))
(point-in-range (and (>= (point) start) (< (point) end))))
(let ((byte-count 0))
(save-excursion
(goto-char start)
(while (< (point) (min (point) end))
(when (looking-at "[0-9a-f]")
(forward-char)
(when (looking-at "[0-9a-f]")
(setq byte-count (1+ byte-count)))
(forward-char))
(skip-chars-forward " ")))
byte-count)))
(defvar-local objdump-hexl-window-shrunk nil
"Flag indicating whether the hexl window has been shrunk.")
(defun objdump-visit-address-in-hexl ()
"Visit the address from current objdump line in a hexl-mode buffer."
(interactive)
(let ((addr (objdump-get-address-at-point))
(hexl-buf-name (file-name-nondirectory objdump-file-name)))
(unless addr
(user-error "No valid address found on current line"))
(unless objdump-file-name
(user-error "No binary file path stored"))
(unless (file-exists-p objdump-file-name)
(user-error "Binary file %s not found" objdump-file-name))
;; Find or create the buffer
(find-file-other-window objdump-file-name)
(unless (eq major-mode 'hexl-mode)
(hexl-mode))
(unless objdump-hexl-window-shrunk
(shrink-window-horizontally 27)
(setq objdump-hexl-window-shrunk t))
(hexl-goto-address addr)))
;; (defun objdump-visit-address-in-hexl ()
;; "Visit the address from current objdump line in a hexl-mode buffer."
;; (interactive)
;; (let ((addr (objdump-get-address-at-point))
;; (hexl-buf-name (file-name-nondirectory objdump-file-name)))
;; (unless addr
;; (user-error "No valid address found on current line"))
;; (unless objdump-file-name
;; (user-error "No binary file path stored"))
;; (unless (file-exists-p objdump-file-name)
;; (user-error "Binary file %s not found" objdump-file-name))
;; ;; Find or create the buffer
;; (find-file-other-window objdump-file-name)
;; (unless (eq major-mode 'hexl-mode)
;; (hexl-mode))
;; (hexl-goto-address addr)))
(defvar objdump-extensions
'(".o" ; compiled object file
".so" ; shared library
".a" ; archive library
".ko" ; Linux kernel objects
;".dylib" ; Mac OS X libraries
;".dll" ; etc
;".obj" ;
)
"Extensions typically indicating object files we should disassemble.")
(defvar objdump-font-lock-keywords
`(
;; Addresses at start of line
("^ *\\([0-9a-f]+\\):" 1 'objdump-address-face)
;; Symbol definitions and references
("^[0-9a-f]+ <\\([^>]+\\)>:$" 1 'objdump-symbol-face)
("<\\([^>]+\\)>" 1 'objdump-symbol-face)
;; Instructions - note the pattern now requires a tab and whitespace
("\t[0-9a-f ]+\t\\([a-z][a-z0-9.]*\\)" 1 'objdump-instruction-face)
;; x86/x86_64 registers
(,(concat "\\b\\(%[a-z][a-z0-9]*\\|[re][abcd]x\\|[re]sp\\|[re]bp\\|"
"[re]si\\|[re]di\\|[re]ip\\|r[0-9]+\\|[xyz]mm[0-9]+\\)\\b")
. 'objdump-register-face)
;; Immediate values
("\\b\\(\\$?-?0x[0-9a-fA-F]+\\|\\$[0-9]+\\)\\b" . 'objdump-immediate-face)
;; Comments and file info
("\\(#.*\\|File .*\\|\\.?\\.?L[A-Za-z0-9]*:\\)" . 'objdump-comment-face))
"Syntax highlighting rules for objdump mode.")
;;; Helpers for address conversion
(defun convert-64bit-to-number (string)
"Convert 64-bit hex STRING to number, handling sign extension."
(if (and (eq (length string) 16)
(string-match "^ff" string))
(- (string-to-number (substring string 2) 16)
(expt 2 56))
(string-to-number string 16)))
(defun convert-number-to-64bit (number)
"Convert NUMBER to 64-bit hex string, handling sign extension."
(if (< number 0)
(format "ff%14x" (+ number (expt 2 56)))
(format "%x" number)))
;;; Symbol table management
(defun objdump--get-symbols ()
"Get or build symbol table for current buffer."
(or objdump-symbol-table
(progn
(setq objdump-symbol-table (make-vector 300 nil))
(save-excursion
(save-match-data
(goto-char (point-min))
(while (re-search-forward "^[0-9a-f]+ <\\([a-zA-Z_0-9:.]+\\)>:$"
nil t)
(intern (match-string 1) objdump-symbol-table))))
objdump-symbol-table)))
(defun objdump--read-address (prompt)
"Read an address with completion using PROMPT."
(completing-read prompt (objdump--get-symbols) nil))
;;; Interactive commands
(defun objdump-revert ()
"Rerun objdump on the (presumably changed) object file."
(interactive)
(if (not objdump-file-name)
(error "No defined object file name for this buffer"))
(let ((pos (point)))
(objdump objdump-file-name)
(goto-char pos)))
(defun get-completion-ignored-extensions-for-objects ()
"Get completion ignored extensions, excluding object file extensions."
(let ((extensions (apply 'list completion-ignored-extensions)))
(mapc (lambda (e)
(setq extensions (delete e extensions)))
objdump-extensions)
(append (list ".c" ".s" ".h" ".cc") extensions)))
;; Movement Functions
(defun objdump-next-function ()
"Move to the start of the next function in objdump output."
(interactive)
(let ((old-point (point)))
(end-of-line)
(if (re-search-forward "^[0-9a-f]+ <[^>]+>:$" nil t)
(progn
(goto-char (line-beginning-position))
(recenter))
(goto-char old-point)
(message "No more functions"))))
(defun objdump-previous-function ()
"Move to the start of the previous function in objdump output."
(interactive)
(let ((old-point (point)))
(beginning-of-line)
(if (re-search-backward "^[0-9a-f]+ <[^>]+>:$" nil t)
(progn
(goto-char (line-beginning-position))
(recenter))
(goto-char old-point)
(message "No previous functions"))))
;; Imenu support
(defgroup objdump-completion nil
"Completion settings for objdump mode."
:group 'objdump)
(defface objdump-completion-address
'((t :inherit marginalia-documentation))
"Face for objdump addresses in completion annotations."
:group 'objdump-completion)
(defface objdump-completion-size
'((t :inherit marginalia-size :weight bold))
"Face for function size annotations."
:group 'objdump-completion)
(defvar-local objdump--longest-symbol-length 0
"Length of longest symbol name in current buffer.")
(defvar-local objdump--longest-addr-length 0
"Length of longest address in current buffer.")
(defun objdump--compute-function-size (start-addr next-addr)
"Compute function size from START-ADDR to NEXT-ADDR."
(when (and start-addr next-addr)
(- (string-to-number next-addr 16)
(string-to-number start-addr 16))))
(defun objdump-imenu-create-index ()
"Create imenu index for objdump buffer."
(let ((index-alist '())
(max-len 0)
(max-addr-len 0)
(prev-addr nil))
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^\\([0-9a-f]+\\) <\\([^>]+\\)>:$" nil t)
(let* ((addr (match-string 1))
(name (match-string 2))
(name-len (length name))
(addr-len (+ 2 (length addr))) ; +2 for "0x" prefix
(size (when prev-addr
(objdump--compute-function-size prev-addr addr)))
(location (point-marker)))
(setq max-len (max max-len name-len)
max-addr-len (max max-addr-len addr-len))
(let ((completion-item name))
(put-text-property 0 (length completion-item)
'objdump-address addr completion-item)
(when size
(put-text-property 0 (length completion-item)
'objdump-size size completion-item))
(push (cons completion-item location) index-alist))
(setq prev-addr addr))))
(setq objdump--longest-symbol-length (+ max-len 2)
objdump--longest-addr-length (+ max-addr-len 2))
(nreverse index-alist)))
(defun objdump-completion-annotator (cand)
"Annotate imenu CAND with address and size info for marginalia."
(when-let ((addr (get-text-property 0 'objdump-address cand)))
(let* ((size (get-text-property 0 'objdump-size cand))
(addr-str (format "0x%s" addr))
(addr-padding (make-string
(max 0 (- objdump--longest-addr-length (length addr-str)))
?\s)))
(concat
(make-string (max 0 (- objdump--longest-symbol-length (length cand))) ?\s)
(propertize addr-str 'face 'objdump-completion-address)
addr-padding
" " ; Two spaces after address
(when size
(propertize (format "%d" size)
'face 'objdump-completion-size))))))
(with-eval-after-load 'marginalia
(add-to-list 'marginalia-annotator-registry
'(imenu objdump-completion-annotator marginalia-annotate-binding)))
(defun objdump--line-has-hex-p ()
"Return t if current line has hex instruction bytes."
(save-excursion
(beginning-of-line)
(looking-at "^\\s-*[0-9a-f]+:\\s-+[0-9a-f]")))
(defun objdump--get-hex-range ()
"Get the start and end positions of hex bytes on current line.
Returns (start . end) positions, or nil if not on a hex line."
(save-excursion
(beginning-of-line)
(when (looking-at "^\\s-*[0-9a-f]+:\\s-+\\([0-9a-f ]\\{2,\\}\\)\\s-+\\S-")
(cons (match-beginning 1) (match-end 1)))))
(defun objdump--find-nearest-hex-position (target-column)
"Find nearest hex position to TARGET-COLUMN in current line.
Returns point position of nearest hex digit, or nil if none found."
(when-let* ((range (objdump--get-hex-range))
(start (car range))
(end (cdr range)))
(save-excursion
;; Get column positions of all hex digits
(let ((positions '())
(min-diff nil)
(best-pos nil))
(goto-char start)
(while (< (point) end)
(when (looking-at "[0-9a-f]")
(let* ((cur-col (current-column))
(diff (abs (- cur-col target-column))))
(when (or (null min-diff) (<= diff min-diff))
(setq min-diff diff)
(setq best-pos (point)))))
(forward-char))
best-pos))))
(defun objdump-forward-byte ()
"Smart forward movement through hex bytes."
(interactive)
(when-let ((range (objdump--get-hex-range)))
(let ((hex-end (cdr range)))
(cond
;; On first digit of a pair, move to second digit
((and (< (point) hex-end)
(looking-at "[0-9a-f]")
(not (looking-back "[0-9a-f]" (1- (point)))))
(forward-char))
;; On second digit or space, move to next pair or wrap
((< (point) hex-end)
(forward-char)
(skip-chars-forward " ")
(when (>= (point) hex-end)
;; At end of line, try to wrap
(when (objdump--next-hex-line)
(beginning-of-line)
(when (looking-at "^\\s-*[0-9a-f]+:\\s-+")
(goto-char (match-end 0))))))))))
(defun objdump-backward-byte ()
"Move backward through hex bytes one character at a time, with wrapping."
(interactive)
(when-let ((range (objdump--get-hex-range)))
(let ((hex-start (car range)))
(cond
;; Case 1: We're after hex-start, handle normal backward movement
((> (point) hex-start)
(backward-char)
;; If we landed on whitespace, skip back to previous hex digit
(when (looking-at "\\s-")
(skip-chars-backward " ")
(when (looking-back "[0-9a-f]" (1- (point)))
(backward-char))))
;; Case 2: We're at the start of hex range, need to wrap to previous line
(t
(when (save-excursion
(forward-line -1)
(objdump--line-has-hex-p))
(forward-line -1)
(when-let* ((prev-range (objdump--get-hex-range))
(prev-end (cdr prev-range)))
;; Go to last hex digit of previous line
(goto-char prev-end)
(backward-char) ; Move off potential whitespace
(while (and (> (point) (car prev-range))
(not (looking-at "[0-9a-f]")))
(backward-char)))))))))
(defun objdump--next-hex-line ()
"Move to next line with hex bytes, preserving column position if possible."
(let ((target-column (current-column)))
(forward-line)
(while (and (not (eobp))
(not (objdump--line-has-hex-p)))
(forward-line))
(when (objdump--line-has-hex-p)
(when-let ((pos (objdump--find-nearest-hex-position target-column)))
(goto-char pos)
t))))
(defun objdump--prev-hex-line ()
"Move to previous line with hex bytes, preserving column position if possible."
(let ((target-column (current-column)))
(forward-line -1)
(while (and (not (bobp))
(not (objdump--line-has-hex-p)))
(forward-line -1))
(when (objdump--line-has-hex-p)
(when-let ((pos (objdump--find-nearest-hex-position target-column)))
(goto-char pos)
t))))
(defun objdump-next-line ()
"Move to next line preserving column when possible."
(interactive)
(let ((target-column (current-column)))
(if (objdump--next-hex-line)
t ; Column already preserved by next-hex-line
;; Try to find next function
(when (re-search-forward "^[0-9a-f]+ <.*>:$" nil t)
(forward-line)
(when (objdump--line-has-hex-p)
(beginning-of-line)
(when (looking-at "^\\s-*[0-9a-f]+:\\s-+")
(goto-char (match-end 0))))))))
(defun objdump-previous-line ()
"Move to previous line preserving column when possible."
(interactive)
(let ((target-column (current-column)))
(if (objdump--prev-hex-line)
t ; Column already preserved by prev-hex-line
;; Try to find previous function
(when (re-search-backward "^[0-9a-f]+ <.*>:$" nil t)
(forward-line)
(when (objdump--line-has-hex-p)
(beginning-of-line)
(when (looking-at "^\\s-*[0-9a-f]+:\\s-+")
(goto-char (match-end 0))))))))
(defun objdump-move-beginning-of-line ()
"Move to first hex character of the line."
(interactive)
(when-let ((range (objdump--get-hex-range)))
(goto-char (car range))))
(defun objdump-move-end-of-line ()
"Move to last hex character of the line."
(interactive)
(when-let ((range (objdump--get-hex-range)))
(goto-char (cdr range))
(backward-char)
(while (and (> (point) (car range))
(not (looking-at "[0-9a-f]")))
(backward-char))))
;; Update keymap
;;;###autoload
(define-derived-mode objdump-mode text-mode "Objdump"
"Major mode for viewing object file disassembly.
\\{objdump-mode-map}"
(setq buffer-read-only t)
(setq font-lock-defaults '(objdump-font-lock-keywords))
(setq truncate-lines t)
(setq imenu-create-index-function #'objdump-imenu-create-index)
;; Advice imenu to recenter after jumping
(advice-add 'imenu :after
(lambda (&rest _)
(beginning-of-line)
(recenter 0))))
;;;###autoload
(defun objdump (filename)
"Run objdump to disassemble an object file, and invoke objdump-mode."
(interactive
(let ((completion-ignored-extensions
(get-completion-ignored-extensions-for-objects)))
(list (read-file-name "Object file to disassemble: "
nil nil t))))
(let ((output-buffer (get-buffer-create (concat "*Objdump " filename "*")))
(command (concat objdump-command " -dCSlr " filename)))
(with-current-buffer output-buffer
(setq buffer-read-only nil)
(erase-buffer))
(message "Running %s ..." command)
(shell-command command output-buffer)
(with-current-buffer output-buffer
(objdump-mode)
(setq objdump-file-name filename))
(message "%s... %s" command
(propertize "DONE" 'face '(:inherit success :weight bold)))))
(provide 'objdump)
;;; objdump.el ends here