2016-08-12 12:17:55 +00:00
|
|
|
;;; -*-scheme-*-
|
|
|
|
|
2016-08-13 13:27:02 +00:00
|
|
|
(define (int->bv32 value)
|
|
|
|
(let ((bv (make-bytevector 4)))
|
|
|
|
(bytevector-u32-native-set! bv 0 value)
|
|
|
|
bv))
|
|
|
|
|
|
|
|
(define (int->bv16 value)
|
|
|
|
(let ((bv (make-bytevector 2)))
|
|
|
|
(bytevector-u16-native-set! bv 0 value)
|
|
|
|
bv))
|
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(define vaddress #x08048000)
|
|
|
|
|
|
|
|
(define ei-magic `(#x7f ,@(string->list "ELF")))
|
2016-08-12 13:10:05 +00:00
|
|
|
(define ei-class '(#x01)) ;; 32 bit
|
|
|
|
(define ei-data '(#x01)) ;; little endian
|
|
|
|
(define ei-version '(#x01))
|
|
|
|
(define ei-osabi '(#x00))
|
|
|
|
(define ei-pad '(#x0 #x0 #x0 #x0 #x0 #x0 #x0 #x0))
|
|
|
|
(define e-ident
|
|
|
|
(append
|
|
|
|
ei-magic
|
|
|
|
ei-class
|
|
|
|
ei-data
|
|
|
|
ei-version
|
|
|
|
ei-osabi
|
|
|
|
ei-pad))
|
|
|
|
|
|
|
|
(define e-type '(#x02 #x00)) ;; ET_EXEC
|
|
|
|
(define e-machine '(#x03 #x00))
|
|
|
|
(define e-version '(#x01 #x00 #x00 #x00))
|
2016-08-13 16:42:38 +00:00
|
|
|
(define e-entry '(0 0 0 0))
|
|
|
|
(define e-phoff '(0 0 0 0))
|
|
|
|
(define e-shoff '(0 0 0 0))
|
2016-08-12 13:10:05 +00:00
|
|
|
(define e-flags '(#x00 #x00 #x00 #x00))
|
2016-08-13 16:42:38 +00:00
|
|
|
(define e-ehsize '(0 0))
|
|
|
|
(define e-phentsize '(0 0))
|
2016-08-12 13:54:15 +00:00
|
|
|
(define e-phnum '(#x01 #x00))
|
2016-08-13 16:42:38 +00:00
|
|
|
(define e-shentsize '(0 0))
|
2016-08-13 16:46:22 +00:00
|
|
|
(define e-shnum '(#x05 #x00))
|
|
|
|
(define e-shstrndx '(#x04 #x00))
|
2016-08-12 13:10:05 +00:00
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(define (elf-header)
|
2016-08-12 13:10:05 +00:00
|
|
|
(append
|
|
|
|
e-ident
|
|
|
|
e-type
|
|
|
|
e-machine
|
|
|
|
e-version
|
|
|
|
e-entry
|
|
|
|
e-phoff
|
|
|
|
e-shoff
|
|
|
|
e-flags
|
|
|
|
e-ehsize
|
|
|
|
e-phentsize
|
|
|
|
e-phnum
|
|
|
|
e-shentsize
|
|
|
|
e-shnum
|
|
|
|
e-shstrndx
|
|
|
|
))
|
|
|
|
|
2016-08-12 14:38:59 +00:00
|
|
|
(define p-type '(#x01 #x00 #x00 #x00))
|
2016-08-13 16:42:38 +00:00
|
|
|
(define p-offset '(0 0 0 0))
|
|
|
|
(define p-vaddr '(0 0 0 0))
|
|
|
|
(define p-paddr '(0 0 0 0))
|
|
|
|
(define p-filesz '(0 0 0 0))
|
|
|
|
(define p-memsz '(0 0 0 0))
|
2016-08-12 14:38:59 +00:00
|
|
|
(define p-flags '(#x07 #x00 #x00 #x00))
|
|
|
|
(define p-align '(#x01 #x00 #x00 #x00))
|
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(define (program-header-0)
|
2016-08-12 13:10:05 +00:00
|
|
|
(append
|
2016-08-12 14:38:59 +00:00
|
|
|
p-type
|
|
|
|
p-offset
|
|
|
|
p-vaddr
|
|
|
|
p-paddr
|
|
|
|
p-filesz
|
|
|
|
p-memsz
|
|
|
|
p-flags
|
|
|
|
p-align
|
2016-08-12 13:10:05 +00:00
|
|
|
))
|
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(set! e-phentsize (int->bv16 (length (program-header-0))))
|
|
|
|
|
|
|
|
(define (program-headers)
|
2016-08-13 08:32:07 +00:00
|
|
|
(append
|
2016-08-13 16:42:38 +00:00
|
|
|
(program-header-0)
|
2016-08-13 08:32:07 +00:00
|
|
|
))
|
2016-08-12 14:38:59 +00:00
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(define (headers)
|
2016-08-13 13:27:02 +00:00
|
|
|
(append
|
2016-08-13 16:42:38 +00:00
|
|
|
(elf-header)
|
|
|
|
(program-headers)
|
2016-08-13 13:27:02 +00:00
|
|
|
))
|
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(define (elf-header-size)
|
|
|
|
(length
|
|
|
|
(elf-header)))
|
|
|
|
|
|
|
|
(set! e-phoff (int->bv32 (elf-header-size)))
|
|
|
|
(set! e-ehsize (int->bv16 (elf-header-size)))
|
|
|
|
|
|
|
|
(define (text-offset)
|
|
|
|
(length (headers)))
|
2016-08-13 13:27:02 +00:00
|
|
|
|
|
|
|
(define data-offset #x1076)
|
2016-08-13 16:42:38 +00:00
|
|
|
(define data-address (+ data-offset vaddress))
|
2016-08-13 13:27:02 +00:00
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(define (i386:puts data)
|
|
|
|
`(
|
2016-08-13 13:27:02 +00:00
|
|
|
#xba #x0e #x00 #x00 #x00 ;; mov $0xe,%edx
|
2016-08-13 16:42:38 +00:00
|
|
|
#xb9 ,@(int->bv32 data) ;; mov $data,%ecx
|
2016-08-13 13:27:02 +00:00
|
|
|
#xbb #x01 #x00 #x00 #x00 ;; mov $0x1,%ebx
|
|
|
|
#xb8 #x04 #x00 #x00 #x00 ;; mov $0x4,%eax
|
|
|
|
#xcd #x80 ;; int $0x80
|
2016-08-13 16:42:38 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
(define (i386:exit code)
|
|
|
|
`(
|
|
|
|
#xbb ,@(int->bv32 code) ;; mov $code,%ebx
|
2016-08-13 13:27:02 +00:00
|
|
|
#xb8 #x01 #x00 #x00 #x00 ;; mov $0x1,%eax
|
|
|
|
#xcd #x80 ;; int $0x80
|
|
|
|
))
|
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(define text
|
|
|
|
(append
|
|
|
|
(i386:puts data-address)
|
|
|
|
(i386:exit 0)
|
|
|
|
))
|
|
|
|
|
2016-08-13 13:27:02 +00:00
|
|
|
(define text-free
|
2016-08-13 16:42:38 +00:00
|
|
|
(vector->list (make-vector (- data-offset (length text) (text-offset)) #x00)))
|
|
|
|
|
|
|
|
(define program-length
|
|
|
|
(+ (length text) (length text-free)))
|
|
|
|
(set! p-filesz (int->bv32 program-length))
|
|
|
|
(set! p-memsz (int->bv32 program-length))
|
2016-08-13 13:27:02 +00:00
|
|
|
|
|
|
|
;; 1076
|
|
|
|
(define data
|
2016-08-13 16:42:38 +00:00
|
|
|
(string->list "Hello, world!\n"))
|
2016-08-13 13:27:02 +00:00
|
|
|
|
2016-08-13 16:46:22 +00:00
|
|
|
(define note
|
2016-08-13 16:57:49 +00:00
|
|
|
(string->list
|
|
|
|
(string-append
|
|
|
|
"Mes -- Maxwell Equations of Software\n"
|
|
|
|
"https://gitlab.com/janneke/mes")
|
|
|
|
;; #x05 #x00 #x00 #x00 #x00 #x00 #x00 #x00 #x02 #x00 #x00 #x00
|
|
|
|
;; #\i #\3 #\8 #\6 #x00 #x00 #x00 #x00
|
2016-08-13 16:46:22 +00:00
|
|
|
))
|
|
|
|
|
2016-08-13 13:27:02 +00:00
|
|
|
;; 1098 sh str table
|
|
|
|
(define tab
|
2016-08-13 16:42:38 +00:00
|
|
|
`(
|
|
|
|
#x00 ,@(string->list ".shstrtab")
|
|
|
|
#x00 ,@(string->list ".text")
|
|
|
|
#x00 ,@(string->list ".data")
|
2016-08-13 16:46:22 +00:00
|
|
|
#x00 ,@(string->list ".note")
|
2016-08-13 16:42:38 +00:00
|
|
|
#x00 #x00 #x00 #x00
|
2016-08-13 13:27:02 +00:00
|
|
|
))
|
|
|
|
|
2016-08-13 08:32:07 +00:00
|
|
|
(define sh-0-name '(#x99 #x99 #x00 #x00))
|
|
|
|
(define sh-0-type '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-0-flags '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-0-addr '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-0-offset '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-0-size '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-0-link '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-0-info '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-0-addralign '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-0-entsize '(#x00 #x00 #x00 #x00))
|
|
|
|
|
2016-08-13 16:57:49 +00:00
|
|
|
(define (sh-0)
|
2016-08-13 08:32:07 +00:00
|
|
|
(append
|
|
|
|
sh-0-name
|
|
|
|
sh-0-type
|
|
|
|
sh-0-flags
|
|
|
|
sh-0-addr
|
|
|
|
sh-0-offset
|
|
|
|
sh-0-size
|
|
|
|
sh-0-link
|
|
|
|
sh-0-info
|
|
|
|
sh-0-addralign
|
|
|
|
sh-0-entsize
|
|
|
|
))
|
2016-08-12 14:38:59 +00:00
|
|
|
|
2016-08-13 16:57:49 +00:00
|
|
|
(set! e-shentsize (int->bv16 (length (sh-0))))
|
2016-08-13 16:42:38 +00:00
|
|
|
|
2016-08-13 08:32:07 +00:00
|
|
|
;; 10e0 sh1: .text
|
|
|
|
(define sh-text-name '(#x0b #x00 #x00 #x00))
|
|
|
|
(define sh-text-type '(#x01 #x00 #x00 #x00))
|
|
|
|
(define sh-text-flags '(#x06 #x00 #x00 #x00))
|
2016-08-13 16:42:38 +00:00
|
|
|
;;(define sh-text-addr '(#x54 #x80 #x04 #x08))
|
|
|
|
(define sh-text-addr (int->bv32 (+ vaddress (text-offset))))
|
|
|
|
(set! e-entry sh-text-addr)
|
|
|
|
(set! p-vaddr sh-text-addr)
|
|
|
|
(set! p-paddr sh-text-addr)
|
2016-08-13 13:27:02 +00:00
|
|
|
;;(define sh-text-offset '(#x54 #x00 #x00 #x00))
|
2016-08-13 16:42:38 +00:00
|
|
|
(define sh-text-offset (int->bv32 (text-offset)))
|
|
|
|
(set! p-offset sh-text-offset)
|
2016-08-13 08:32:07 +00:00
|
|
|
(define sh-text-size '(#x22 #x00 #x00 #x00))
|
|
|
|
(define sh-text-link '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-text-info '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-text-addralign '(#x01 #x00 #x00 #x00))
|
|
|
|
(define sh-text-entsize '(#x00 #x00 #x00 #x00))
|
|
|
|
|
2016-08-13 16:57:49 +00:00
|
|
|
(define (sh-text)
|
2016-08-13 08:32:07 +00:00
|
|
|
(append
|
|
|
|
sh-text-name
|
|
|
|
sh-text-type
|
|
|
|
sh-text-flags
|
|
|
|
sh-text-addr
|
2016-08-13 16:42:38 +00:00
|
|
|
sh-text-offset
|
2016-08-13 08:32:07 +00:00
|
|
|
sh-text-size
|
|
|
|
sh-text-link
|
|
|
|
sh-text-info
|
|
|
|
sh-text-addralign
|
|
|
|
sh-text-entsize
|
|
|
|
))
|
2016-08-12 14:38:59 +00:00
|
|
|
|
2016-08-13 08:32:07 +00:00
|
|
|
;; 1108 sh2: .data
|
|
|
|
(define sh-data-name '(#x11 #x00 #x00 #x00))
|
|
|
|
(define sh-data-type '(#x01 #x00 #x00 #x00))
|
|
|
|
(define sh-data-flags '(#x03 #x00 #x00 #x00))
|
2016-08-13 16:42:38 +00:00
|
|
|
;;(define sh-data-addr '(#x76 #x90 #x04 #x08))
|
|
|
|
(define sh-data-addr (int->bv32 data-address))
|
2016-08-13 13:27:02 +00:00
|
|
|
;;(define sh-data-offset '(#x76 #x10 #x00 #x00))
|
2016-08-13 16:42:38 +00:00
|
|
|
(define sh-data-offset (int->bv32 data-offset))
|
|
|
|
;;(define sh-data-size '(#x0e #x00 #x00 #x00))
|
|
|
|
(define sh-data-size (int->bv32 (length data)))
|
2016-08-13 08:32:07 +00:00
|
|
|
(define sh-data-link '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-data-info '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-data-addralign '(#x01 #x00 #x00 #x00))
|
|
|
|
(define sh-data-entsize '(#x00 #x00 #x00 #x00))
|
|
|
|
|
2016-08-13 16:57:49 +00:00
|
|
|
(define (sh-data)
|
2016-08-13 08:32:07 +00:00
|
|
|
(append
|
|
|
|
sh-data-name
|
|
|
|
sh-data-type
|
|
|
|
sh-data-flags
|
|
|
|
sh-data-addr
|
2016-08-13 16:42:38 +00:00
|
|
|
sh-data-offset
|
|
|
|
sh-data-size
|
2016-08-13 08:32:07 +00:00
|
|
|
sh-data-link
|
|
|
|
sh-data-info
|
|
|
|
sh-data-addralign
|
|
|
|
sh-data-entsize
|
|
|
|
))
|
2016-08-12 14:38:59 +00:00
|
|
|
|
2016-08-13 16:46:22 +00:00
|
|
|
;; 1130 sh3: .note
|
|
|
|
(define sh-note-name '(#x17 #x00 #x00 #x00))
|
|
|
|
(define sh-note-type '(#x07 #x00 #x00 #x00))
|
|
|
|
(define sh-note-flags '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-note-addr '(#x00 #x00 #x00 #x00))
|
2016-08-13 16:57:49 +00:00
|
|
|
;;(define sh-note-offset '(#x84 #x10 #x00 #x00))
|
|
|
|
(define sh-note-offset '(0 0 0 0))
|
|
|
|
(define (note-offset) 0)
|
|
|
|
;;(define sh-note-offset (int->bv32 (note-offset)))
|
2016-08-13 16:46:22 +00:00
|
|
|
(define sh-note-size '(#x14 #x00 #x00 #x00))
|
|
|
|
(define sh-note-link '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-note-info '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-note-addralign '(#x01 #x00 #x00 #x00))
|
|
|
|
(define sh-note-entsize '(#x00 #x00 #x00 #x00))
|
|
|
|
|
2016-08-13 16:57:49 +00:00
|
|
|
(define (sh-note)
|
2016-08-13 16:46:22 +00:00
|
|
|
(append
|
|
|
|
sh-note-name
|
|
|
|
sh-note-type
|
|
|
|
sh-note-flags
|
|
|
|
sh-note-addr
|
|
|
|
sh-note-offset
|
|
|
|
sh-note-size
|
|
|
|
sh-note-link
|
|
|
|
sh-note-info
|
|
|
|
sh-note-addralign
|
|
|
|
sh-note-entsize
|
|
|
|
))
|
|
|
|
|
2016-08-13 08:32:07 +00:00
|
|
|
;; 1158 sh4: .shstrtab
|
|
|
|
(define sh-tab-name '(#x01 #x00 #x00 #x00))
|
|
|
|
(define sh-tab-type '(#x03 #x00 #x00 #x00))
|
|
|
|
(define sh-tab-flags '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-tab-addr '(#x00 #x00 #x00 #x00))
|
2016-08-13 16:57:49 +00:00
|
|
|
;;(define sh-tab-offset '(#x98 #x10 #x00 #x00))
|
|
|
|
(define sh-tab-offset '(0 0 0 0))
|
|
|
|
;;(define sh-tab-offset (int->bv32 (tab-offset)))
|
2016-08-13 08:32:07 +00:00
|
|
|
(define sh-tab-size '(#x1d #x00 #x00 #x00))
|
|
|
|
(define sh-tab-link '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-tab-info '(#x00 #x00 #x00 #x00))
|
|
|
|
(define sh-tab-addralign '(#x01 #x00 #x00 #x00))
|
|
|
|
(define sh-tab-entsize '(#x00 #x00 #x00 #x00))
|
|
|
|
|
2016-08-13 16:57:49 +00:00
|
|
|
(define (sh-tab)
|
2016-08-13 08:32:07 +00:00
|
|
|
(append
|
|
|
|
sh-tab-name
|
|
|
|
sh-tab-type
|
|
|
|
sh-tab-flags
|
|
|
|
sh-tab-addr
|
|
|
|
sh-tab-offset
|
|
|
|
sh-tab-size
|
|
|
|
sh-tab-link
|
|
|
|
sh-tab-info
|
|
|
|
sh-tab-addralign
|
|
|
|
sh-tab-entsize
|
|
|
|
))
|
2016-08-12 14:38:59 +00:00
|
|
|
|
2016-08-13 16:57:49 +00:00
|
|
|
(define (section-headers)
|
2016-08-13 08:32:07 +00:00
|
|
|
(append
|
2016-08-13 16:57:49 +00:00
|
|
|
(sh-0)
|
|
|
|
(sh-text)
|
|
|
|
(sh-data)
|
|
|
|
(sh-note)
|
|
|
|
(sh-tab)
|
2016-08-13 08:32:07 +00:00
|
|
|
))
|
2016-08-12 14:38:59 +00:00
|
|
|
|
2016-08-13 16:57:49 +00:00
|
|
|
(define (note-offset)
|
|
|
|
(length
|
|
|
|
(append
|
|
|
|
(headers)
|
|
|
|
text
|
|
|
|
text-free
|
|
|
|
data
|
|
|
|
)))
|
|
|
|
|
|
|
|
(set! sh-note-offset (int->bv32 (note-offset)))
|
|
|
|
|
|
|
|
(define (tab-offset)
|
|
|
|
(length
|
|
|
|
(append
|
|
|
|
(headers)
|
|
|
|
text
|
|
|
|
text-free
|
|
|
|
data
|
|
|
|
note
|
|
|
|
)))
|
|
|
|
|
|
|
|
(set! sh-tab-offset (int->bv32 (tab-offset)))
|
|
|
|
|
2016-08-13 16:42:38 +00:00
|
|
|
(define (section-headers-offset)
|
|
|
|
(length
|
|
|
|
(append
|
|
|
|
(headers)
|
|
|
|
text
|
|
|
|
text-free
|
|
|
|
data
|
2016-08-13 16:46:22 +00:00
|
|
|
note
|
2016-08-13 16:42:38 +00:00
|
|
|
tab)))
|
|
|
|
|
|
|
|
(set! e-shoff (int->bv32 (section-headers-offset)))
|
|
|
|
|
2016-08-12 13:54:15 +00:00
|
|
|
(define exe
|
|
|
|
(append
|
2016-08-13 16:42:38 +00:00
|
|
|
(headers)
|
2016-08-13 08:32:07 +00:00
|
|
|
text
|
|
|
|
text-free
|
|
|
|
;;1076 (+ 0804 8000): 0804 9076
|
|
|
|
data
|
2016-08-13 16:46:22 +00:00
|
|
|
;;1084
|
|
|
|
note
|
|
|
|
;;1098
|
2016-08-13 08:32:07 +00:00
|
|
|
tab
|
2016-08-13 16:46:22 +00:00
|
|
|
;;10b8
|
2016-08-13 16:57:49 +00:00
|
|
|
(section-headers)
|
2016-08-13 08:32:07 +00:00
|
|
|
))
|
2016-08-12 13:54:15 +00:00
|
|
|
(map write-char exe)
|