elf-map.scm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env guile
  2. !#
  3. (use-modules (ice-9 binary-ports)
  4. (rnrs bytevectors)
  5. (ice-9 match)
  6. (system vm elf)
  7. (charting)
  8. (srfi srfi-1))
  9. (define (read-elf file)
  10. (parse-elf (call-with-input-file file
  11. (lambda (port) (get-bytevector-all port)))))
  12. (define (make-elf-map elf-file png-file)
  13. (let ((elf (read-elf elf-file)))
  14. (make-page-map
  15. elf-file
  16. (stable-sort
  17. (filter-map
  18. (match-lambda
  19. ((name . section)
  20. (and (not (eqv? (elf-section-type section) SHT_NULL))
  21. (cons (if (string-null? name)
  22. "(unnamed)"
  23. name)
  24. (cons (elf-section-offset section)
  25. (elf-section-size section))))))
  26. (elf-sections-by-name elf))
  27. (match-lambda*
  28. (((_ . (x-start . _)) (_ . (y-start . _)))
  29. (<= x-start y-start))))
  30. png-file)))
  31. (when (batch-mode?)
  32. (match (program-arguments)
  33. ((_ elf-file png-file)
  34. (make-elf-map elf-file png-file))
  35. ((arg0 . _)
  36. (format (current-error-port) "Usage: ~a ELF-FILE PNG-FILE\n" arg0)
  37. (exit 1))))