2019-05-18 21:18:52 +00:00
|
|
|
# Assign addresses (co-ordinates) to instructions (landmarks) in a program
|
|
|
|
# (landscape).
|
|
|
|
# Use the addresses assigned to:
|
|
|
|
# a) replace labels
|
2019-06-08 19:38:53 +00:00
|
|
|
# b) add segment headers with addresses and offsets correctly filled in
|
2019-05-18 21:18:52 +00:00
|
|
|
#
|
|
|
|
# To build (from the subx/ directory):
|
|
|
|
# $ ./subx translate *.subx apps/survey.subx -o apps/survey
|
|
|
|
#
|
|
|
|
# The expected input is a stream of bytes with segment headers, comments and
|
|
|
|
# some interspersed labels.
|
|
|
|
# $ cat x
|
|
|
|
# == code 0x1
|
|
|
|
# l1:
|
|
|
|
# aa bb l1/imm8
|
|
|
|
# cc dd l2/disp32
|
|
|
|
# l2:
|
|
|
|
# ee foo/imm32
|
|
|
|
# == data 0x10
|
|
|
|
# foo:
|
|
|
|
# 00
|
|
|
|
#
|
|
|
|
# The output is the stream of bytes without segment headers or label definitions,
|
|
|
|
# and with label references replaced with numeric values/displacements.
|
|
|
|
#
|
|
|
|
# $ cat x |./subx run apps/assort
|
|
|
|
# ...ELF header bytes...
|
|
|
|
# # ELF header above will specify that code segment begins at this offset
|
|
|
|
# aa bb nn # some computed address
|
|
|
|
# cc dd nn nn nn nn # some computed displacement
|
|
|
|
# ee nn nn nn nn # some computed address
|
|
|
|
# # ELF header above will specify that data segment begins at this offset
|
|
|
|
# 00
|
|
|
|
|
|
|
|
== code
|
|
|
|
# instruction effective address register displacement immediate
|
|
|
|
# . op subop mod rm32 base index scale r32
|
|
|
|
# . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes
|
|
|
|
|
|
|
|
Entry:
|
2019-06-08 19:38:53 +00:00
|
|
|
# Heap = new-segment(64KB)
|
2019-05-18 21:18:52 +00:00
|
|
|
# . . push args
|
|
|
|
68/push Heap/imm32
|
|
|
|
68/push 0x10000/imm32/64KB
|
|
|
|
# . . call
|
|
|
|
e8/call new-segment/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2019-06-08 19:38:53 +00:00
|
|
|
# initialize-trace-stream(256KB)
|
|
|
|
# . . push args
|
|
|
|
68/push 0x40000/imm32/256KB
|
|
|
|
# . . call
|
|
|
|
e8/call initialize-trace-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2019-05-18 21:18:52 +00:00
|
|
|
|
|
|
|
# run tests if necessary, convert stdin if not
|
|
|
|
# . prolog
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# initialize heap
|
|
|
|
# - if argc > 1 and argv[1] == "test", then return run_tests()
|
|
|
|
# . argc > 1
|
|
|
|
81 7/subop/compare 1/mod/*+disp8 5/rm32/EBP . . . . 0/disp8 1/imm32 # compare *EBP
|
|
|
|
7e/jump-if-lesser-or-equal $run-main/disp8
|
|
|
|
# . argv[1] == "test"
|
|
|
|
# . . push args
|
|
|
|
68/push "test"/imm32
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
|
|
|
|
# . . call
|
|
|
|
e8/call kernel-string-equal?/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check result
|
|
|
|
3d/compare-EAX-and 1/imm32
|
|
|
|
75/jump-if-not-equal $run-main/disp8
|
|
|
|
# . run-tests()
|
|
|
|
e8/call run-tests/disp32
|
|
|
|
8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX
|
|
|
|
eb/jump $main:end/disp8
|
|
|
|
$run-main:
|
|
|
|
# - otherwise convert stdin
|
|
|
|
# var ed/EAX : exit-descriptor
|
|
|
|
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # subtract from ESP
|
|
|
|
89/copy 3/mod/direct 0/rm32/EAX . . . 4/r32/ESP . . # copy ESP to EAX
|
|
|
|
# configure ed to really exit()
|
|
|
|
# . ed->target = 0
|
|
|
|
c7 0/subop/copy 0/mod/direct 0/rm32/EAX . . . . . 0/imm32 # copy to *EAX
|
|
|
|
# return convert(Stdin, 1/stdout, 2/stderr, ed)
|
|
|
|
# . . push args
|
|
|
|
50/push-EAX/ed
|
|
|
|
68/push Stderr/imm32
|
|
|
|
68/push Stdout/imm32
|
|
|
|
68/push Stdin/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call convert/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP
|
|
|
|
# . syscall(exit, 0)
|
|
|
|
bb/copy-to-EBX 0/imm32
|
|
|
|
$main:end:
|
|
|
|
b8/copy-to-EAX 1/imm32/exit
|
|
|
|
cd/syscall 0x80/imm8
|
|
|
|
|
2019-06-08 19:38:53 +00:00
|
|
|
# data structures:
|
|
|
|
# segment-info: {address, file-offset, size} (12 bytes)
|
|
|
|
# segments: (address stream {string, segment-info}) (16 bytes per row)
|
|
|
|
# label-info: {segment-name, segment-offset, address} (12 bytes)
|
|
|
|
# labels: (address stream {string, label-info}) (16 bytes per row)
|
2019-06-29 01:24:44 +00:00
|
|
|
# these are all inefficient; use sequential scans for lookups
|
2019-06-08 19:38:53 +00:00
|
|
|
|
2019-05-18 21:18:52 +00:00
|
|
|
convert: # in : (address buffered-file), out : (address buffered-file) -> <void>
|
2019-06-08 19:38:53 +00:00
|
|
|
# pseudocode
|
|
|
|
# var segments = new-stream(10 rows, 16 bytes each)
|
2019-06-12 17:18:51 +00:00
|
|
|
# var labels = new-stream(512 rows, 16 bytes each)
|
2019-06-08 19:38:53 +00:00
|
|
|
# compute-offsets(in, segments, labels)
|
|
|
|
# compute-addresses(segments, labels)
|
|
|
|
# rewind-stream(in)
|
|
|
|
# emit-output(in, out, segments, labels)
|
|
|
|
#
|
2019-05-18 21:18:52 +00:00
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
2019-06-08 19:38:53 +00:00
|
|
|
51/push-ECX
|
|
|
|
52/push-EDX
|
|
|
|
# var segments/ECX = stream(10 * 16)
|
|
|
|
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0xa0/imm32 # subtract from ESP
|
|
|
|
68/push 0xa0/imm32/length
|
|
|
|
68/push 0/imm32/read
|
|
|
|
68/push 0/imm32/write
|
|
|
|
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
|
2019-06-12 17:18:51 +00:00
|
|
|
# var labels/EDX = stream(512 * 16)
|
|
|
|
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x2000/imm32 # subtract from ESP
|
|
|
|
68/push 0x2000/imm32/length
|
2019-06-08 19:38:53 +00:00
|
|
|
68/push 0/imm32/read
|
|
|
|
68/push 0/imm32/write
|
|
|
|
89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX
|
|
|
|
# compute-offsets(in, segments, labels)
|
|
|
|
# . . push args
|
|
|
|
52/push-EDX
|
|
|
|
51/push-ECX
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
|
|
|
|
# . . call
|
|
|
|
e8/call compute-offsets/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
# compute-addresses(segments, labels)
|
|
|
|
# . . push args
|
|
|
|
52/push-EDX
|
|
|
|
51/push-ECX
|
|
|
|
# . . call
|
|
|
|
e8/call compute-addresses/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP
|
|
|
|
# rewind-stream(in)
|
|
|
|
# . . push args
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
|
|
|
|
# . . call
|
|
|
|
e8/call rewind-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# emit-output(in, out, segments, labels)
|
|
|
|
# . . push args
|
|
|
|
52/push-EDX
|
|
|
|
51/push-ECX
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12)
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
|
|
|
|
# . . call
|
|
|
|
e8/call emit-output/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP
|
2019-05-18 21:18:52 +00:00
|
|
|
$convert:end:
|
|
|
|
# . reclaim locals
|
2019-06-08 19:38:53 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x214/imm32 # add to ESP
|
2019-05-18 21:18:52 +00:00
|
|
|
# . restore registers
|
2019-06-08 19:38:53 +00:00
|
|
|
5a/pop-to-EDX
|
|
|
|
59/pop-to-ECX
|
2019-05-18 21:18:52 +00:00
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
2019-06-08 19:38:53 +00:00
|
|
|
test-convert-computes-addresses:
|
|
|
|
# input:
|
|
|
|
# == code 0x1
|
|
|
|
# ab x/imm32
|
|
|
|
# == data 0x1000
|
|
|
|
# x:
|
|
|
|
# 01
|
|
|
|
#
|
|
|
|
# trace contains (in any order):
|
|
|
|
# label x is at address 0x1079
|
|
|
|
# segment code starts at address 0x74
|
|
|
|
# segment code has size 5
|
|
|
|
# segment data starts at address 0x1079
|
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# setup
|
|
|
|
# . clear-stream(_test-input-stream)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(_test-input-buffered-file+4)
|
|
|
|
# . . push args
|
|
|
|
b8/copy-to-EAX _test-input-buffered-file/imm32
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(_test-output-stream)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-output-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(_test-output-buffered-file+4)
|
|
|
|
# . . push args
|
|
|
|
b8/copy-to-EAX _test-output-buffered-file/imm32
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# initialize input
|
|
|
|
# . write(_test-input-stream, "== code 0x1\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "== code 0x1\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . write(_test-input-stream, "ab x/imm32\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "ab x/imm32\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . write(_test-input-stream, "== data 0x1000\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "== data 0x1000\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . write(_test-input-stream, "x:\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "x:\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . write(_test-input-stream, "01\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "01\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# convert(_test-input-buffered-file, _test-output-buffered-file)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-output-buffered-file/imm32
|
|
|
|
68/push _test-input-buffered-file/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call convert/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# check trace
|
2019-06-12 17:18:51 +00:00
|
|
|
# . check-trace-contains("label 'x' is at address 0x1079", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-convert-computes-addresses/0"/imm32
|
|
|
|
68/push "label 'x' is at address 0x1079"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("segment 'code' starts at address 0x74", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-convert-computes-addresses/1"/imm32
|
|
|
|
68/push "segment 'code' starts at address 0x74"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("segment 'code' has size 0x5", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-convert-computes-addresses/2"/imm32
|
|
|
|
68/push "segment 'code' has size 0x5"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("segment 'data' starts at address 0x1079", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-convert-computes-addresses/3"/imm32
|
|
|
|
68/push "segment 'data' starts at address 0x1079"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2019-06-08 19:38:53 +00:00
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
2019-06-29 21:58:32 +00:00
|
|
|
is-label?: # word : (address slice) -> EAX : boolean
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
51/push-ECX
|
|
|
|
# ECX = word
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX
|
|
|
|
# ECX = word->end
|
|
|
|
8b/copy 1/mod/*+disp8 1/rm32/ECX . . . 1/r32/ECX 4/disp8 . # copy *(ECX+4) to ECX
|
|
|
|
# *(word->end - 1) == ':'
|
|
|
|
# . EAX = 0
|
|
|
|
31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX
|
|
|
|
# . EAX = *((char *) word->end - 1)
|
|
|
|
8a/copy-byte 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/AL -1/disp8 . # copy byte at *(ECX-1) to AL
|
|
|
|
# . (word->end - 1)/EAX == ':'
|
|
|
|
3d/compare-EAX-and 3A/imm32/colon
|
|
|
|
b8/copy-to-EAX 1/imm32/true
|
|
|
|
74/jump-if-equal $is-label?:end/disp8
|
|
|
|
b8/copy-to-EAX 0/imm32/false
|
|
|
|
$is-label?:end:
|
|
|
|
# . restore registers
|
|
|
|
59/pop-to-ECX
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
test-is-label?:
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
$test-is-label?:true:
|
|
|
|
# var slice/ECX = slice("AAA:")
|
|
|
|
68/push _test-label-slice-end1/imm32
|
|
|
|
68/push _test-label-slice-start/imm32
|
|
|
|
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
|
|
|
|
# is-label?(slice/ECX)
|
|
|
|
51/push-ECX
|
|
|
|
e8/call is-label?/disp32
|
|
|
|
# . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# check-ints-equal(EAX, 1, msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-is-label?:true"/imm32
|
|
|
|
68/push 1/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call check-ints-equal/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
$test-is-label?:false:
|
|
|
|
# var slice/ECX = slice("AAA")
|
|
|
|
68/push _test-label-slice-end2/imm32
|
|
|
|
68/push _test-label-slice-start/imm32
|
|
|
|
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
|
|
|
|
# is-label?(slice/ECX)
|
|
|
|
51/push-ECX
|
|
|
|
e8/call is-label?/disp32
|
|
|
|
# . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# check-ints-equal(EAX, 0, msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-is-label?:false"/imm32
|
|
|
|
68/push 0/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call check-ints-equal/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
2019-06-08 19:38:53 +00:00
|
|
|
compute-offsets: # in : (address buffered-file), segments : (address stream {string, segment-info}), labels : (address stream {string, label-info})
|
|
|
|
# pseudocode:
|
2019-06-13 08:08:02 +00:00
|
|
|
# var curr-segment-name = 0
|
|
|
|
# var file-offset = 0, segment-offset = 0
|
|
|
|
# var line = new-stream(512, 1)
|
|
|
|
# while true
|
|
|
|
# clear-stream(line)
|
|
|
|
# read-line-buffered(in, line)
|
|
|
|
# if (line->write == 0) break # end of file
|
|
|
|
# while true
|
|
|
|
# var word-slice = next-word(line)
|
|
|
|
# if slice-empty?(word-slice) # end of line
|
|
|
|
# break
|
|
|
|
# if slice-starts-with?(word-slice, "#") # comment
|
|
|
|
# continue
|
|
|
|
# if is-label?(word-slice)
|
|
|
|
# x : (address number) = insert(labels, name)
|
|
|
|
# *x = segment-offset
|
|
|
|
# trace("label '", word-slice, "' is in segment '", curr-segment-name, "'")
|
|
|
|
# trace("label '", word-slice, "' is at offset 0x", file-offset)
|
2019-06-13 21:20:35 +00:00
|
|
|
# # labels occupy no space, so no need to increment offsets
|
2019-06-13 08:08:02 +00:00
|
|
|
# continue
|
|
|
|
# if slice-equal?(word-slice, "==")
|
2019-06-13 21:20:35 +00:00
|
|
|
# curr-segment-name = next-word(line)
|
|
|
|
# if slice-empty?(curr-segment-name)
|
2019-06-13 08:08:02 +00:00
|
|
|
# abort
|
2019-06-13 21:20:35 +00:00
|
|
|
# segment-start = next-word(line)
|
2019-06-13 08:08:02 +00:00
|
|
|
# if slice-empty?(segment-start)
|
|
|
|
# abort
|
2019-06-13 21:20:35 +00:00
|
|
|
# seg = insert(segments, curr-segment-name)
|
2019-06-13 08:08:02 +00:00
|
|
|
# seg->starting-address = parse-hex-int(segment-start)
|
|
|
|
# seg->starting-offset = file-offset
|
2019-06-13 21:20:35 +00:00
|
|
|
# trace("segment '", curr-segment-name, "' is at file offset 0x", seg->starting-offset)
|
|
|
|
# trace("segment '", curr-segment-name, "' has size 0x", seg->starting-offset)
|
2019-06-13 08:08:02 +00:00
|
|
|
# segment-offset = 0
|
|
|
|
# else
|
|
|
|
# width = compute-width(word-slice)
|
|
|
|
# segment-offset += width
|
|
|
|
# file-offset += width
|
2019-06-08 19:38:53 +00:00
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
$compute-offsets:end:
|
|
|
|
# . reclaim locals
|
|
|
|
# . restore registers
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
test-compute-offsets:
|
|
|
|
# input:
|
|
|
|
# == code 0x1
|
|
|
|
# ab x/imm32
|
|
|
|
# == data 0x1000
|
|
|
|
# x:
|
2019-06-12 17:18:51 +00:00
|
|
|
# 34
|
2019-06-08 19:38:53 +00:00
|
|
|
#
|
|
|
|
# trace contains (in any order):
|
2019-06-12 17:18:51 +00:00
|
|
|
# segment 'code' is at file offset 0x0
|
|
|
|
# segment 'code' has size 0x5
|
|
|
|
# segment 'data' is at file offset 0x5
|
|
|
|
# label 'x' is in segment 'data'
|
|
|
|
# label 'x' is at offset 0x0
|
|
|
|
# segment 'data' has size 0x1
|
2019-06-08 19:38:53 +00:00
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# setup
|
2019-06-12 17:18:51 +00:00
|
|
|
# . clear-stream(_test-input-stream)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(_test-input-buffered-file+4)
|
|
|
|
# . . push args
|
|
|
|
b8/copy-to-EAX _test-input-buffered-file/imm32
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(_test-output-stream)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-output-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(_test-output-buffered-file+4)
|
|
|
|
# . . push args
|
|
|
|
b8/copy-to-EAX _test-output-buffered-file/imm32
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# var segments/ECX = stream(2 * 16)
|
|
|
|
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x20/imm32 # subtract from ESP
|
|
|
|
68/push 0x20/imm32/length
|
|
|
|
68/push 0/imm32/read
|
|
|
|
68/push 0/imm32/write
|
|
|
|
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
|
|
|
|
# var labels/EDX = stream(2 * 16)
|
|
|
|
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x20/imm32 # subtract from ESP
|
|
|
|
68/push 0x20/imm32/length
|
|
|
|
68/push 0/imm32/read
|
|
|
|
68/push 0/imm32/write
|
|
|
|
89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX
|
|
|
|
# initialize input
|
|
|
|
# . write(_test-input-stream, "== code 0x1\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "== code 0x1\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . write(_test-input-stream, "ab x/imm32\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "ab x/imm32\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . write(_test-input-stream, "== data 0x1000\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "== data 0x1000\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . write(_test-input-stream, "x:\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "x:\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . write(_test-input-stream, "34\n")
|
|
|
|
# . . push args
|
|
|
|
68/push "34\n"/imm32
|
|
|
|
68/push _test-input-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# compute-offsets(_test-input-buffered-file, _test-output-buffered-file, segments, labels)
|
|
|
|
# . . push args
|
|
|
|
52/push-EDX
|
|
|
|
51/push-ECX
|
|
|
|
68/push _test-output-buffered-file/imm32
|
|
|
|
68/push _test-input-buffered-file/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call compute-offsets/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP
|
|
|
|
# check trace
|
|
|
|
# . check-trace-contains("segment 'code' is at file offset 0x0", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-offsets/0"/imm32
|
|
|
|
68/push "segment 'code' is at file offset 0x0"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("segment 'code' has size 0x5", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-offsets/1"/imm32
|
|
|
|
68/push "segment 'code' has size 0x5"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("segment 'data' is at file offset 0x5", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-offsets/2"/imm32
|
|
|
|
68/push "segment 'data' is at file offset 0x5"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("segment 'data' has size 0x1", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-offsets/3"/imm32
|
|
|
|
68/push "segment 'data' has size 0x1"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("label 'x' is in segment 'data'", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-offsets/4"/imm32
|
|
|
|
68/push "label 'x' is in segment 'data'"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("label 'x' is at offset 0x0", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-offsets/5"/imm32
|
|
|
|
68/push "label 'x' is at offset 0x0"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2019-06-08 19:38:53 +00:00
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
compute-addresses: # segments : (address stream {string, segment-info}), labels : (address stream {string, label-info})
|
|
|
|
# pseudocode:
|
2019-06-13 21:20:35 +00:00
|
|
|
# s : (address segment-info) = segments->data + 4 # skip key
|
|
|
|
# max = segments->data + segments->write
|
|
|
|
# while true
|
|
|
|
# if (s >= max) break
|
|
|
|
# s->address &= 0xfffff000 # clear last 12 bits for p_align
|
|
|
|
# s->address += (s->file-offset & 0x00000fff)
|
2019-06-29 01:20:21 +00:00
|
|
|
# s += 16 # size of row
|
2019-06-13 21:20:35 +00:00
|
|
|
# l : (address label-info) = labels->data + 4 # skip key
|
|
|
|
# max = labels->data + labels->write
|
|
|
|
# while true
|
|
|
|
# if (l >= max) break
|
|
|
|
# seg-name : (address string) = l->segment-name
|
2019-06-29 01:20:21 +00:00
|
|
|
# label-seg : (address segment-info) = get-or-insert(segments, seg-name)
|
2019-06-13 21:20:35 +00:00
|
|
|
# l->address = label-seg->address + l->segment-offset
|
2019-06-29 01:20:21 +00:00
|
|
|
# l += 16 # size of row
|
2019-06-08 19:38:53 +00:00
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
2019-06-29 01:20:21 +00:00
|
|
|
50/push-EAX
|
|
|
|
51/push-ECX
|
|
|
|
52/push-EDX
|
|
|
|
53/push-EBX
|
|
|
|
56/push-ESI
|
|
|
|
# ESI = segments
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI
|
|
|
|
# s/EAX = segments->data + 4
|
|
|
|
8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 . # copy ESI+16 to EAX
|
|
|
|
# max/ECX = segments->data + segments->write
|
|
|
|
8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX
|
|
|
|
01/add 3/mod/direct 1/rm32/ECX . . . 6/r32/ESI . . # add ESI to ECX
|
|
|
|
$compute-addresses:segment-loop:
|
|
|
|
# if (s >= max) break
|
|
|
|
39/compare 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # compare EAX with ECX
|
|
|
|
73/jump-if-greater-or-equal-unsigned $compute-addresses:segment-break/disp8
|
|
|
|
# clear last 12 bits of s->address for p_align=0x1000
|
|
|
|
# . EDX = s->address
|
|
|
|
8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX
|
|
|
|
# . EDX &= 0xfffff000
|
|
|
|
81 4/subop/and 3/mod/direct 2/rm32/EDX . . . . . 0xfffff000/imm32 # bitwise and of EDX
|
|
|
|
# update last 12 bits from s->file-offset
|
|
|
|
# . EBX = s->file-offset
|
|
|
|
8b/copy 1/mod/*+disp8 0/rm32/EAX . . . 3/r32/EBX 4/disp8 . # copy *(EAX+4) to EBX
|
|
|
|
# . EBX &= 0xfff
|
|
|
|
81 4/subop/and 3/mod/direct 3/rm32/EBX . . . . . 0x00000fff/imm32 # bitwise and of EBX
|
|
|
|
# . s->address = EDX | EBX
|
|
|
|
09/or 3/mod/direct 2/rm32/EDX . . . 3/r32/EBX . . # EDX = bitwise OR with EBX
|
|
|
|
89/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to *EAX
|
|
|
|
# s += 16 # size of row
|
|
|
|
05/add-to-EAX 0x10/imm32
|
|
|
|
eb/jump $compute-addresses:segment-loop/disp8
|
|
|
|
$compute-addresses:segment-break:
|
|
|
|
# ESI = labels
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI
|
|
|
|
# l/EAX = labels->data + 4
|
|
|
|
8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 . # copy ESI+16 to EAX
|
|
|
|
# max/ECX = labels->data + labels->write
|
|
|
|
8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX
|
|
|
|
01/add 3/mod/direct 1/rm32/ECX . . . 6/r32/ESI . . # add ESI to ECX
|
|
|
|
$compute-addresses:label-loop:
|
|
|
|
# if (l >= max) break
|
|
|
|
39/compare 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # compare EAX with ECX
|
|
|
|
73/jump-if-greater-or-equal-unsigned $compute-addresses:end/disp8
|
|
|
|
# seg-name/EDX = l->segment-name
|
|
|
|
8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX
|
|
|
|
# label-seg/EDX : (address label-info) = get-or-insert(labels, seg-name, row-size=16)
|
|
|
|
# . . push args
|
|
|
|
68/push 0x10/imm32/row-size
|
|
|
|
52/push-EDX
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12)
|
|
|
|
# . . call
|
|
|
|
e8/call get-or-insert/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
# EBX = label-seg->address
|
|
|
|
8b/copy 0/mod/indirect 2/rm32/EDX . . . 3/r32/EBX . . # copy *EDX to EBX
|
|
|
|
# EBX += l->segment-offset
|
|
|
|
03/add 1/mod/*+disp8 5/rm32/EBP . . . 3/r32/EBX 4/disp8 . # add *(EAX+4) to EBX
|
|
|
|
# l->address = EBX
|
|
|
|
89/copy 0/mod/indirect 0/rm32/EAX . . . 3/r32/EBX . . # copy EBX to *EAX
|
|
|
|
# l += 16 # size of row
|
|
|
|
05/add-to-EAX 0x10/imm32
|
|
|
|
eb/jump $compute-addresses:label-loop/disp8
|
2019-06-08 19:38:53 +00:00
|
|
|
$compute-addresses:end:
|
|
|
|
# . restore registers
|
2019-06-29 01:20:21 +00:00
|
|
|
5e/pop-to-ESI
|
|
|
|
5b/pop-to-EBX
|
|
|
|
5a/pop-to-EDX
|
|
|
|
59/pop-to-ECX
|
|
|
|
58/pop-to-EAX
|
2019-06-08 19:38:53 +00:00
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
test-compute-addresses:
|
2019-06-28 23:54:41 +00:00
|
|
|
# segments:
|
|
|
|
# - 'a': {0x1000, 0, 5}
|
|
|
|
# - 'b': {0x500, 0, 1}
|
|
|
|
# - 'c': {0x5444, 0, 12}
|
|
|
|
# labels:
|
|
|
|
# - 'l1': {'a', 3, 0}
|
|
|
|
# - 'l2': {'b', 0, 0}
|
2019-06-08 19:38:53 +00:00
|
|
|
#
|
|
|
|
# trace contains (in any order):
|
2019-06-28 23:54:41 +00:00
|
|
|
# segment 'a' starts at address 0x1074
|
|
|
|
# segment 'b' starts at address 0x579
|
2019-06-08 19:38:53 +00:00
|
|
|
# label 'l1' is at address 0x1077
|
|
|
|
# label 'l2' is at address 0x579
|
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# setup
|
2019-06-28 23:54:41 +00:00
|
|
|
# . var segments/ECX = stream(10 * 16)
|
|
|
|
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0xa0/imm32 # subtract from ESP
|
|
|
|
68/push 0xa0/imm32/length
|
|
|
|
68/push 0/imm32/read
|
|
|
|
68/push 0/imm32/write
|
|
|
|
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
|
|
|
|
# . var labels/EDX = stream(512 * 16)
|
|
|
|
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x2000/imm32 # subtract from ESP
|
|
|
|
68/push 0x2000/imm32/length
|
|
|
|
68/push 0/imm32/read
|
|
|
|
68/push 0/imm32/write
|
|
|
|
89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX
|
|
|
|
# . stream-add4(segments, "a", 0x1000, 0, 5)
|
|
|
|
68/push 5/imm32/segment-size
|
|
|
|
68/push 0/imm32/file-offset
|
|
|
|
68/push 0x1000/imm32/start-address
|
|
|
|
68/push "a"/imm32/segment-name
|
|
|
|
51/push-ECX
|
|
|
|
# . . call
|
|
|
|
e8/call stream-add4/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP
|
|
|
|
# . stream-add4(segments, "b", 0x500, 0, 1)
|
|
|
|
68/push 1/imm32/segment-size
|
|
|
|
68/push 0/imm32/file-offset
|
|
|
|
68/push 0x500/imm32/start-address
|
|
|
|
68/push "b"/imm32/segment-name
|
|
|
|
51/push-ECX
|
|
|
|
# . . call
|
|
|
|
e8/call stream-add4/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP
|
|
|
|
# . stream-add4(segments, "c", 0x5444, 0, 12)
|
|
|
|
68/push 0xc/imm32/segment-size
|
|
|
|
68/push 0/imm32/file-offset
|
|
|
|
68/push 0x5444/imm32/start-address
|
|
|
|
68/push "c"/imm32/segment-name
|
|
|
|
51/push-ECX
|
|
|
|
# . . call
|
|
|
|
e8/call stream-add4/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP
|
|
|
|
# . stream-add4(labels, "l1", "a", 3, 0)
|
|
|
|
68/push 0/imm32/label-address
|
|
|
|
68/push 3/imm32/segment-offset
|
|
|
|
68/push "a"/imm32/segment-name
|
|
|
|
68/push "l1"/imm32/label-name
|
|
|
|
52/push-EDX
|
|
|
|
# . . call
|
|
|
|
e8/call stream-add4/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP
|
|
|
|
# . stream-add4(labels, "l2", "b", 0, 0)
|
|
|
|
68/push 0/imm32/label-address
|
|
|
|
68/push 0/imm32/segment-offset
|
|
|
|
68/push "b"/imm32/segment-name
|
|
|
|
68/push "l2"/imm32/label-name
|
|
|
|
52/push-EDX
|
|
|
|
# . . call
|
|
|
|
e8/call stream-add4/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP
|
|
|
|
# checks
|
|
|
|
# . check-trace-contains("segment 'a' starts at address 0x1074", msg)
|
2019-06-12 17:18:51 +00:00
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-addresses/0"/imm32
|
2019-06-28 23:54:41 +00:00
|
|
|
68/push "segment 'a' starts at address 0x1074"/imm32
|
2019-06-12 17:18:51 +00:00
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2019-06-28 23:54:41 +00:00
|
|
|
# . check-trace-contains("segment 'b' starts at address 0x579", msg)
|
2019-06-12 17:18:51 +00:00
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-addresses/1"/imm32
|
|
|
|
68/push "segment 'data' starts at address 0x579"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("label 'l1' is at address 0x1077", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-addresses/2"/imm32
|
|
|
|
68/push "label 'l1' is at address 0x1077"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . check-trace-contains("label 'l2' is at address 0x579", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-compute-addresses/3"/imm32
|
|
|
|
68/push "label 'l2' is at address 0x579"/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-trace-contains/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2019-06-08 19:38:53 +00:00
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
2019-06-28 23:54:41 +00:00
|
|
|
# helper for tests
|
|
|
|
stream-add4: # in : (address stream byte), key : address, val1 : address, val2 : address, val3 : address
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
50/push-EAX
|
|
|
|
51/push-ECX
|
|
|
|
52/push-EDX
|
|
|
|
56/push-ESI
|
|
|
|
# ESI = in
|
2019-06-29 01:20:21 +00:00
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI
|
2019-06-28 23:54:41 +00:00
|
|
|
# curr/EAX = in->data + in->write
|
|
|
|
# . EAX = in->write
|
|
|
|
8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX
|
|
|
|
# . EAX = ESI+EAX+12
|
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 0/index/EAX . 0/r32/EAX 0xc/disp8 . # copy ESI+EAX+12 to EAX
|
|
|
|
# max/EDX = in->data + in->length
|
|
|
|
# . EDX = in->length
|
|
|
|
8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 2/r32/EDX 8/disp8 . # copy *(ESI+8) to EDX
|
|
|
|
# . EDX = ESI+EDX+12
|
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 2/index/EDX . 2/r32/EDX 0xc/disp8 . # copy ESI+EDX+12 to EDX
|
|
|
|
# if (curr >= max) abort
|
|
|
|
39/compare 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # compare EAX with EDX
|
|
|
|
73/jump-if-greater-or-equal-unsigned $stream-add4:abort/disp8
|
|
|
|
# *curr = key
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . 1/r32/ECX 0xc/disp8 . # copy *(EBP+12) to ECX
|
|
|
|
89/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy ECX to *EAX
|
|
|
|
# curr += 4
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
# if (curr >= max) abort
|
|
|
|
39/compare 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # compare EAX with EDX
|
|
|
|
73/jump-if-greater-or-equal-unsigned $stream-add4:abort/disp8
|
|
|
|
# *curr = val1
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . 1/r32/ECX 0x10/disp8 . # copy *(EBP+16) to ECX
|
|
|
|
89/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy ECX to *EAX
|
|
|
|
# curr += 4
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
# if (curr >= max) abort
|
|
|
|
39/compare 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # compare EAX with EDX
|
|
|
|
73/jump-if-greater-or-equal-unsigned $stream-add4:abort/disp8
|
|
|
|
# *curr = val2
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . 1/r32/ECX 0x14/disp8 . # copy *(EBP+20) to ECX
|
|
|
|
89/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy ECX to *EAX
|
|
|
|
# curr += 4
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
# if (curr >= max) abort
|
|
|
|
39/compare 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # compare EAX with EDX
|
|
|
|
73/jump-if-greater-or-equal-unsigned $stream-add4:abort/disp8
|
|
|
|
# *curr = val3
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . 1/r32/ECX 0x18/disp8 . # copy *(EBP+24) to ECX
|
|
|
|
89/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy ECX to *EAX
|
|
|
|
# in->write += 16
|
|
|
|
81 0/subop/add 0/mod/indirect 6/rm32/ESI . . . . . 0x10/imm32 # add to *ESI
|
|
|
|
$stream-add4:end:
|
|
|
|
# . restore registers
|
|
|
|
5e/pop-to-ESI
|
|
|
|
5a/pop-to-EDX
|
|
|
|
59/pop-to-ECX
|
|
|
|
58/pop-to-EAX
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
$stream-add4:abort:
|
|
|
|
# . _write(2/stderr, error)
|
|
|
|
# . . push args
|
|
|
|
68/push "overflow in stream-add4\n"/imm32
|
|
|
|
68/push 2/imm32/stderr
|
|
|
|
# . . call
|
|
|
|
e8/call _write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . syscall(exit, 1)
|
|
|
|
bb/copy-to-EBX 1/imm32
|
|
|
|
b8/copy-to-EAX 1/imm32/exit
|
|
|
|
cd/syscall 0x80/imm8
|
|
|
|
# never gets here
|
|
|
|
|
2019-06-08 19:38:53 +00:00
|
|
|
emit-output: # in : (address buffered-file), out : (address buffered-file), segments : (address stream {string, segment-info}), labels : (address stream {string, label-info})
|
|
|
|
# pseudocode:
|
2019-06-13 21:20:35 +00:00
|
|
|
# emit-headers(out, segments, labels)
|
|
|
|
# var offset-of-next-instruction = 0
|
|
|
|
# var line = new-stream(512, 1)
|
|
|
|
# while true
|
|
|
|
# clear-stream(line)
|
|
|
|
# read-line-buffered(in, line)
|
|
|
|
# if (line->write == 0) break # end of file
|
|
|
|
# offset-of-next-instruction += num-bytes(line)
|
|
|
|
# while true
|
|
|
|
# var word-slice = next-word(line)
|
|
|
|
# if slice-empty?(word-slice) # end of line
|
|
|
|
# break
|
|
|
|
# if slice-starts-with?(word-slice, "#") # comment
|
|
|
|
# break
|
|
|
|
# if is-label?(word-slice) # no need for label declarations anymore
|
|
|
|
# break
|
|
|
|
# if slice-equal?(word-slice, "==")
|
|
|
|
# break # no need for segment header lines
|
|
|
|
# if length(word-slice) == 2
|
|
|
|
# write-slice-buffered(out, word-slice)
|
|
|
|
# write-buffered(out, " ")
|
|
|
|
# continue
|
|
|
|
# datum = next-token(word-slice, "/")
|
|
|
|
# info = get(labels, datum)
|
|
|
|
# if has-metadata?(word-slice, "imm8")
|
|
|
|
# abort # label should never go to imm8
|
|
|
|
# else if has-metadata?(word-slice, "imm32")
|
|
|
|
# emit(out, info->address, 4)
|
|
|
|
# else if has-metadata?(word-slice, "disp8")
|
|
|
|
# value = info->offset - offset-of-next-instruction
|
|
|
|
# emit(out, value, 1)
|
|
|
|
# else if has-metadata?(word-slice, "disp32")
|
|
|
|
# value = info->offset - offset-of-next-instruction
|
|
|
|
# emit(out, value, 4)
|
|
|
|
# else
|
|
|
|
# abort
|
|
|
|
# write-buffered(out, "\n")
|
2019-06-08 19:38:53 +00:00
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
$emit-output:end:
|
|
|
|
# . reclaim locals
|
|
|
|
# . restore registers
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
2019-05-18 21:18:52 +00:00
|
|
|
|
2019-06-13 21:20:35 +00:00
|
|
|
emit-headers: # out : (address buffered-file), segments : (address stream {string, segment-info}), labels : (address stream {string, label-info})
|
|
|
|
# pseudocode:
|
|
|
|
# emit-elf-header(out, segments, labels)
|
|
|
|
# curr-segment = segments->data
|
|
|
|
# max = segments->data + segments->write
|
|
|
|
# while true
|
|
|
|
# if (curr-segment >= max) break
|
|
|
|
# emit-elf-program-header-entry(curr-segment)
|
|
|
|
# curr-segment += 20 # size of a row
|
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
$emit-headers:end:
|
|
|
|
# . reclaim locals
|
|
|
|
# . restore registers
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
emit-elf-header: # out : (address buffered-file), segments : (address stream {string, segment-info}), labels : (address stream {string, label-info})
|
|
|
|
# pseudocode
|
|
|
|
# *Elf_e_entry = get(labels, "Entry")->address
|
|
|
|
# *Elf_e_phnum = segments->write / 20 # size of a row
|
|
|
|
# write(out, Elf_header)
|
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
$emit-elf-header:end:
|
|
|
|
# . reclaim locals
|
|
|
|
# . restore registers
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
emit-elf-program-header-entry: # curr-segment : {string, segment-info}
|
|
|
|
# pseudocode:
|
|
|
|
# *Elf_p_offset = curr-segment->file-offset
|
|
|
|
# *Elf_p_vaddr = curr-segment->address
|
|
|
|
# *Elf_p_paddr = curr-segment->address
|
|
|
|
# *Elf_p_filesz = curr-segment->size
|
|
|
|
# *Elf_p_memsz = curr-segment->size
|
|
|
|
# if curr-segment->name == "code"
|
|
|
|
# *Elf_p_flags = 5 # r-x
|
|
|
|
# else
|
|
|
|
# *Elf_p_flags = 6 # rw-
|
|
|
|
# write(out, Elf_program_header_entry)
|
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
$emit-elf-program-header-entry:end:
|
|
|
|
# . reclaim locals
|
|
|
|
# . restore registers
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
num-bytes: # line : (address stream) -> EAX : int
|
|
|
|
# pseudocode:
|
|
|
|
# result = 0
|
|
|
|
# while true
|
|
|
|
# var word-slice = next-word(line)
|
|
|
|
# if slice-empty?(word-slice) # end of line
|
|
|
|
# break
|
|
|
|
# if slice-starts-with?(word-slice, "#") # comment
|
|
|
|
# break
|
|
|
|
# if is-label?(word-slice) # no need for label declarations anymore
|
|
|
|
# break
|
|
|
|
# if slice-equal?(word-slice, "==")
|
|
|
|
# break # no need for segment header lines
|
|
|
|
# result += compute-width(word-slice)
|
|
|
|
# return result
|
|
|
|
#
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
$num-bytes:end:
|
|
|
|
# . reclaim locals
|
|
|
|
# . restore registers
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
2019-05-18 21:18:52 +00:00
|
|
|
== data
|
|
|
|
|
|
|
|
Segment-size:
|
|
|
|
0x1000/imm32/4KB
|
|
|
|
|
|
|
|
# This block of bytes gets copied to the start of the output ELF file, with
|
|
|
|
# some fields filled in.
|
|
|
|
# http://www.sco.com/developers/gabi/latest/ch4.eheader.html
|
|
|
|
Elf_header:
|
2019-05-19 19:47:21 +00:00
|
|
|
# - length
|
|
|
|
0x34/imm32
|
|
|
|
# - data
|
2019-05-18 21:18:52 +00:00
|
|
|
$e_ident:
|
|
|
|
7f 45/E 4c/L 46/F
|
|
|
|
01/32-bit 01/little-endian 01/file-version 00/no-os-extensions
|
|
|
|
00 00 00 00 00 00 00 00 # 8 bytes of padding
|
|
|
|
$e_type:
|
|
|
|
02 00
|
|
|
|
$e_machine:
|
|
|
|
03 00
|
|
|
|
$e_version:
|
|
|
|
1/imm32
|
|
|
|
Elf_e_entry:
|
|
|
|
0x09000000/imm32 # approximate default; must be updated
|
|
|
|
$e_phoff:
|
|
|
|
0x34/imm32 # offset for the 'program header table' containing segment headers
|
|
|
|
$e_shoff:
|
|
|
|
0/imm32 # no sections
|
|
|
|
$e_flags:
|
|
|
|
0/imm32 # unused
|
|
|
|
$e_ehsize:
|
|
|
|
0x34 00
|
|
|
|
$e_phentsize:
|
|
|
|
0x20 00
|
|
|
|
Elf_e_phnum:
|
|
|
|
00 00 # number of segments; must be updated
|
|
|
|
$e_shentsize:
|
|
|
|
00 00 # no sections
|
|
|
|
$e_shnum:
|
|
|
|
00 00
|
|
|
|
$e_shstrndx:
|
|
|
|
00 00
|
|
|
|
|
|
|
|
# This block of bytes gets copied after the Elf_header once for each segment.
|
|
|
|
# Some fields need filling in each time.
|
|
|
|
# https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-83432/index.html
|
|
|
|
Elf_program_header_entry:
|
2019-05-19 19:47:21 +00:00
|
|
|
# - length
|
|
|
|
0x20/imm32
|
|
|
|
# - data
|
2019-05-18 21:18:52 +00:00
|
|
|
$p_type:
|
|
|
|
1/imm32/PT_LOAD
|
|
|
|
Elf_p_offset:
|
|
|
|
0/imm32 # byte offset in the file at which a segment begins; must be updated
|
|
|
|
Elf_p_vaddr:
|
|
|
|
0/imm32 # starting address to store the segment at before running the program
|
|
|
|
Elf_p_paddr:
|
|
|
|
0/imm32 # should have same value as Elf_p_vaddr
|
|
|
|
Elf_p_filesz:
|
|
|
|
0/imm32
|
|
|
|
Elf_p_memsz:
|
|
|
|
0/imm32 # should have same value as Elf_p_filesz
|
|
|
|
Elf_p_flags:
|
|
|
|
6/imm32/rw- # read/write/execute permissions for the segment; must be updated for the code segment
|
2019-06-13 21:20:35 +00:00
|
|
|
$p_align:
|
2019-05-18 21:18:52 +00:00
|
|
|
# we hold this constant; changing it will require adjusting the way we
|
|
|
|
# compute the starting address for each segment
|
|
|
|
0x1000/imm32
|
|
|
|
|
|
|
|
# . . vim:nowrap:textwidth=0
|