1 ;;; Guile RTL disassembler
3 ;;; Copyright (C) 2001, 2009, 2010, 2012, 2013 Free Software Foundation, Inc.
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (system vm disassembler)
22 #:use-module (system vm instruction)
23 #:use-module (system vm elf)
24 #:use-module (system vm debug)
25 #:use-module (system vm program)
26 #:use-module (system vm objcode)
27 #:use-module (system foreign)
28 #:use-module (rnrs bytevectors)
29 #:use-module (ice-9 format)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 vlist)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-4)
34 #:export (disassemble-program
38 (define-syntax-rule (u32-ref buf n)
39 (bytevector-u32-native-ref buf (* n 4)))
41 (define-syntax-rule (s32-ref buf n)
42 (bytevector-s32-native-ref buf (* n 4)))
44 (define-syntax visit-opcodes
47 ((visit-opcodes macro arg ...)
48 (with-syntax (((inst ...)
49 (map (lambda (x) (datum->syntax #'macro x))
50 (rtl-instruction-list))))
52 (macro arg ... . inst)
55 (eval-when (expand compile load eval)
56 (define (id-append ctx a b)
57 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b)))))
59 (define (unpack-scm n)
60 (pointer->scm (make-pointer n)))
62 (define (unpack-s24 s)
63 (if (zero? (logand s (ash 1 23)))
67 (define (unpack-s32 s)
68 (if (zero? (logand s (ash 1 31)))
72 (define-syntax disassembler
74 (define (parse-first-word word type)
75 (with-syntax ((word word))
82 #'((unpack-s24 (ash word -8))))
84 #'((logand (ash word -8) #xff)
87 #'((logand (ash word -8) #xfff)
90 #'((logand (ash word -8) #xff)
91 (logand (ash word -16) #xff)
94 (error "bad kind" type)))))
96 (define (parse-tail-word word type)
97 (with-syntax ((word word))
100 #'((logand word #ff)))
102 #'((logand word #xff)
105 #'((logand word #xff)
106 (unpack-s24 (ash word -8))))
108 #'((logand word #xff)
109 (logand (ash word -8) #xff)
112 #'((logand word #xff)
113 (logand (ash word -8) #xfff)
116 #'((logand word #xff)
117 (logand (ash word -8) #xff)
118 (logand (ash word -16) #xff)
129 #'((unpack-s32 word)))
131 #'((unpack-s32 word)))
133 #'((unpack-s32 word)))
135 #'((unpack-s32 word)))
139 #'((logand (ash word -8) #xfff)
142 #'((unpack-s24 (ash word -8))))
144 #'((not (zero? (logand word #x1)))
145 (unpack-s24 (ash word -8))))
147 #'((not (zero? (logand word #x1)))
148 (logand (ash word -1) #x7f)
149 (unpack-s24 (ash word -8))))
151 #'((not (zero? (logand word #x1)))))
153 #'((not (zero? (logand word #x1)))
156 (error "bad kind" type)))))
159 ((_ name opcode word0 word* ...)
160 (let ((vars (generate-temporaries #'(word* ...))))
161 (with-syntax (((word* ...) vars)
162 ((n ...) (map 1+ (iota (length #'(word* ...)))))
164 (parse-first-word #'first (syntax->datum #'word0)))
166 (map (lambda (word type)
167 (parse-tail-word word type))
169 (syntax->datum #'(word* ...)))))
170 #'(lambda (buf offset first)
171 (let ((word* (u32-ref buf (+ offset n)))
173 (values (+ 1 (length '(word* ...)))
174 (list 'name asm ... asm* ... ...))))))))))
176 (define (disasm-invalid buf offset first)
177 (error "bad instruction" (logand first #xff) first buf offset))
179 (define disassemblers (make-vector 256 disasm-invalid))
181 (define-syntax define-disassembler
184 ((_ name opcode kind arg ...)
185 (with-syntax ((parse (id-append #'name #'parse- #'name)))
186 #'(let ((parse (disassembler name opcode arg ...)))
187 (vector-set! disassemblers opcode parse)))))))
189 (visit-opcodes define-disassembler)
192 (define (disassemble-one buf offset)
193 (let ((first (u32-ref buf offset)))
194 ((vector-ref disassemblers (logand first #xff)) buf offset first)))
196 (define (u32-offset->addr offset context)
197 "Given an offset into an image in 32-bit units, return the absolute
198 address of that offset."
199 (+ (debug-context-base context) (* offset 4)))
201 (define (code-annotation code len offset start labels context)
202 ;; FIXME: Print names for register loads and stores that correspond to
203 ;; access to named locals.
204 (define (reference-scm target)
205 (unpack-scm (u32-offset->addr (+ offset target) context)))
207 (define (dereference-scm target)
208 (let ((addr (u32-offset->addr (+ offset target)
211 (dereference-pointer (make-pointer addr)))))
215 'br-if-nargs-ne 'br-if-nargs-lt 'br-if-nargs-gt
216 'br-if-true 'br-if-null 'br-if-nil 'br-if-pair 'br-if-struct
217 'br-if-char 'br-if-tc7 'br-if-eq 'br-if-eqv 'br-if-equal
218 'br-if-= 'br-if-< 'br-if-<= 'br-if-> 'br-if->=) _ ... target)
219 (list "-> ~A" (vector-ref labels (- (+ offset target) start))))
220 (('prompt tag flags handler)
221 ;; The H is for handler.
222 (list "H -> ~A" (vector-ref labels (- (+ offset handler) start))))
223 (((or 'make-short-immediate 'make-long-immediate) _ imm)
224 (list "~S" (unpack-scm imm)))
225 (('make-long-long-immediate _ high low)
226 (list "~S" (unpack-scm (logior (ash high 32) low))))
227 (('assert-nargs-ee/locals nargs locals)
228 ;; The nargs includes the procedure.
229 (list "~a arg~:p, ~a local~:p" (1- nargs) locals))
230 (('tail-call nargs proc)
231 (list "~a arg~:p" nargs))
232 (('make-closure dst target nfree)
233 (let* ((addr (u32-offset->addr (+ offset target) context))
234 (pdi (find-program-debug-info addr context)))
235 ;; FIXME: Disassemble embedded closures as well.
236 (list "~A at 0x~X (~A free var~:p)"
237 (or (and pdi (program-debug-info-name pdi))
238 "(anonymous procedure)")
241 (('make-non-immediate dst target)
242 (list "~@Y" (reference-scm target)))
243 (((or 'static-ref 'static-set!) _ target)
244 (list "~@Y" (dereference-scm target)))
245 (('link-procedure! src target)
246 (let* ((addr (u32-offset->addr (+ offset target) context))
247 (pdi (find-program-debug-info addr context)))
249 (or (and pdi (program-debug-info-name pdi))
250 "(anonymous procedure)")
252 (('resolve-module dst name public)
253 (list "~a" (if (zero? public) "private" "public")))
254 (('toplevel-box _ var-offset mod-offset sym-offset bound?)
255 (list "`~A'~A" (dereference-scm sym-offset)
256 (if bound? "" " (maybe unbound)")))
257 (('module-box _ var-offset mod-name-offset sym-offset bound?)
258 (let ((mod-name (reference-scm mod-name-offset)))
259 (list "`(~A ~A ~A)'~A" (if (car mod-name) '@ '@@) (cdr mod-name)
260 (dereference-scm sym-offset)
261 (if bound? "" " (maybe unbound)"))))
262 (('load-typed-array dst type shape target len)
263 (let ((addr (u32-offset->addr (+ offset target) context)))
264 (list "~a bytes from #x~X" len addr)))
267 (define (compute-labels bv start end)
268 (let ((labels (make-vector (- end start) #f)))
269 (define (add-label! pos header)
270 (unless (vector-ref labels (- pos start))
271 (vector-set! labels (- pos start) header)))
273 (let lp ((offset start))
275 (call-with-values (lambda () (disassemble-one bv offset))
281 br-if-nargs-ne br-if-nargs-lt br-if-nargs-gt
282 br-if-true br-if-null br-if-nil br-if-pair br-if-struct
283 br-if-char br-if-tc7 br-if-eq br-if-eqv br-if-equal
284 br-if-= br-if-< br-if-<= br-if-> br-if->=)
287 (add-label! (+ offset target) "L"))))
291 (add-label! (+ offset target) "H")))))))
292 (lp (+ offset len))))))
293 (let lp ((offset start) (n 1))
295 (let* ((pos (- offset start))
296 (label (vector-ref labels pos)))
302 (string-append label (number->string n))))
303 (lp (1+ offset) (1+ n)))
304 (lp (1+ offset) n)))))
307 (define (print-info port addr label info extra src)
309 (format port "~A:\n" label))
310 (format port "~4@S ~32S~@[;; ~1{~@?~}~]~@[~61t at ~a~]\n"
311 addr info extra src))
313 (define (disassemble-buffer port bv start end context)
314 (let ((labels (compute-labels bv start end))
315 (sources (find-program-sources (u32-offset->addr start context)
317 (define (lookup-source addr)
318 (let lp ((sources sources))
322 (let ((pc (source-pre-pc source)))
324 ((< pc addr) (lp sources))
326 (format #f "~a:~a:~a"
328 (source-line-for-user source)
329 (source-column source)))
331 (let lp ((offset start))
333 (call-with-values (lambda () (disassemble-one bv offset))
335 (let ((pos (- offset start))
336 (addr (u32-offset->addr offset context))
337 (annotation (code-annotation elt len offset start labels
339 (print-info port pos (vector-ref labels pos) elt annotation
340 (lookup-source addr))
341 (lp (+ offset len)))))))))
343 (define* (disassemble-program program #:optional (port (current-output-port)))
345 ((find-program-debug-info (rtl-program-code program))
347 (format port "Disassembly of ~S at #x~X:\n\n" program
348 (program-debug-info-addr pdi))
349 (disassemble-buffer port
350 (program-debug-info-image pdi)
351 (program-debug-info-u32-offset pdi)
352 (program-debug-info-u32-offset-end pdi)
353 (program-debug-info-context pdi))))
355 (format port "Debugging information unavailable.~%")))
358 (define* (disassemble-image bv #:optional (port (current-output-port)))
359 (let* ((ctx (debug-context-from-image bv))
360 (base (debug-context-text-base ctx)))
364 (let ((name (elf-symbol-name sym))
365 (value (elf-symbol-value sym))
366 (size (elf-symbol-size sym)))
367 (format port "Disassembly of ~A at #x~X:\n\n"
368 (if (and (string? name) (not (string-null? name)))
370 "<unnamed function>")
372 (disassemble-buffer port
375 (/ (+ base value size) 4)
377 (display "\n\n" port)))))
380 (define (disassemble-file file)
381 (let* ((thunk (load-thunk-from-file file))
382 (elf (find-mapped-elf-image (rtl-program-code thunk))))
383 (disassemble-image elf)))