diff options
-rw-r--r-- | subx/011run.cc | 6 | ||||
-rw-r--r-- | subx/022check_instruction.cc | 1 | ||||
-rw-r--r-- | subx/023check_operand_bounds.cc (renamed from subx/023check_operand_sizes.cc) | 1 | ||||
-rw-r--r-- | subx/024pack_operands.cc (renamed from subx/024pack_instructions.cc) | 41 | ||||
-rw-r--r-- | subx/025non_code_segment.cc | 5 | ||||
-rw-r--r-- | subx/026labels.cc | 15 |
6 files changed, 40 insertions, 29 deletions
diff --git a/subx/011run.cc b/subx/011run.cc index 4a754a09..218f4e16 100644 --- a/subx/011run.cc +++ b/subx/011run.cc @@ -121,6 +121,7 @@ struct word { :(code) void parse(istream& fin, program& out) { vector<line> l; + trace(99, "parse") << "begin" << end(); while (has_data(fin)) { string line_data; getline(fin, line_data); @@ -172,6 +173,7 @@ void parse(istream& fin, program& out) { trace(99, "parse") << "flushing to segment" << end(); out.segments.back().lines.swap(l); } + trace(99, "parse") << "done" << end(); } //:: transform @@ -182,13 +184,16 @@ typedef void (*transform_fn)(program&); vector<transform_fn> Transform; void transform(program& p) { + trace(99, "transform") << "begin" << end(); for (int t = 0; t < SIZE(Transform); ++t) (*Transform.at(t))(p); + trace(99, "transform") << "done" << end(); } //:: load void load(const program& p) { + trace(99, "load") << "begin" << end(); if (p.segments.empty()) { raise << "no code to run\n" << end(); return; @@ -211,6 +216,7 @@ void load(const program& p) { if (i == 0) End_of_program = addr; } EIP = p.segments.at(0).start; + trace(99, "load") << "done" << end(); } uint8_t hex_byte(const string& s) { diff --git a/subx/022check_instruction.cc b/subx/022check_instruction.cc index 5eb21136..31834b38 100644 --- a/subx/022check_instruction.cc +++ b/subx/022check_instruction.cc @@ -66,6 +66,7 @@ Transform.push_back(check_operands); :(code) void check_operands(/*const*/ program& p) { + trace(99, "transform") << "-- check operands" << end(); if (p.segments.empty()) return; const segment& code = p.segments.at(0); for (int i = 0; i < SIZE(code.lines); ++i) { diff --git a/subx/023check_operand_sizes.cc b/subx/023check_operand_bounds.cc index 048cc28a..c868603a 100644 --- a/subx/023check_operand_sizes.cc +++ b/subx/023check_operand_bounds.cc @@ -26,6 +26,7 @@ put(Operand_bound, "imm8", 1<<8); Transform.push_back(check_operand_bounds); :(code) void check_operand_bounds(/*const*/ program& p) { + trace(99, "transform") << "-- check operand bounds" << end(); if (p.segments.empty()) return; const segment& code = p.segments.at(0); for (int i = 0; i < SIZE(code.lines); ++i) { diff --git a/subx/024pack_instructions.cc b/subx/024pack_operands.cc index 62d47d22..aa4fec7d 100644 --- a/subx/024pack_instructions.cc +++ b/subx/024pack_operands.cc @@ -7,23 +7,23 @@ # 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 bb 0x2a/imm32 # copy 42 to EBX -+translate: packing instruction 'bb 0x2a/imm32' -+translate: instruction after packing: 'bb 2a 00 00 00' ++transform: packing instruction 'bb 0x2a/imm32' ++transform: instruction after packing: 'bb 2a 00 00 00' +run: copy imm32 0x0000002a to EBX :(scenario pack_disp8) == 0x1 74 2/disp8 # jump 2 bytes away if ZF is set -+translate: packing instruction '74 2/disp8' -+translate: instruction after packing: '74 02' ++transform: packing instruction '74 2/disp8' ++transform: instruction after packing: '74 02' :(scenarios transform) :(scenario pack_disp8_negative) == 0x1 # running this will cause an infinite loop 74 -1/disp8 # jump 1 byte before if ZF is set -+translate: packing instruction '74 -1/disp8' -+translate: instruction after packing: '74 ff' ++transform: packing instruction '74 -1/disp8' ++transform: instruction after packing: '74 ff' :(scenarios run) :(scenario pack_modrm_imm32) @@ -32,32 +32,33 @@ # 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 81 0/add/subop 3/mod/direct 3/ebx/rm32 1/imm32 # add 1 to EBX -+translate: packing instruction '81 0/add/subop 3/mod/direct 3/ebx/rm32 1/imm32' -+translate: instruction after packing: '81 c3 01 00 00 00' ++transform: packing instruction '81 0/add/subop 3/mod/direct 3/ebx/rm32 1/imm32' ++transform: instruction after packing: '81 c3 01 00 00 00' :(scenario pack_imm32_large) == 0x1 b9 0x080490a7/imm32 # copy to ECX -+translate: packing instruction 'b9 0x080490a7/imm32' -+translate: instruction after packing: 'b9 a7 90 04 08' ++transform: packing instruction 'b9 0x080490a7/imm32' ++transform: instruction after packing: 'b9 a7 90 04 08' :(before "End One-time Setup") -Transform.push_back(pack_instructions); +Transform.push_back(pack_operands); :(code) -void pack_instructions(program& p) { +void pack_operands(program& p) { + trace(99, "transform") << "-- pack operands" << end(); if (p.segments.empty()) return; segment& code = p.segments.at(0); for (int i = 0; i < SIZE(code.lines); ++i) { line& inst = code.lines.at(i); if (all_hex_bytes(inst)) continue; - trace(99, "translate") << "packing instruction '" << to_string(/*with metadata*/inst) << "'" << end(); - pack_instruction(inst); - trace(99, "translate") << "instruction after packing: '" << to_string(/*without metadata*/inst.words) << "'" << end(); + trace(99, "transform") << "packing instruction '" << to_string(/*with metadata*/inst) << "'" << end(); + pack_operands(inst); + trace(99, "transform") << "instruction after packing: '" << to_string(/*without metadata*/inst.words) << "'" << end(); } } -void pack_instruction(line& inst) { +void pack_operands(line& inst) { line new_inst; add_opcodes(inst, new_inst); add_modrm_byte(inst, new_inst); @@ -196,8 +197,8 @@ void transform(const string& text_bytes) { # 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 bb 0x2a/imm32 # copy 42 to EBX -+translate: packing instruction 'bb 0x2a/imm32' -+translate: instruction after packing: 'bb 2a 00 00 00' ++transform: packing instruction 'bb 0x2a/imm32' ++transform: instruction after packing: 'bb 2a 00 00 00' +run: copy imm32 0x0000002a to EBX :(scenarios transform) @@ -207,8 +208,8 @@ void transform(const string& text_bytes) { # 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 bb foo/imm32 # copy foo to EBX -+translate: packing instruction 'bb foo/imm32' ++transform: packing instruction 'bb foo/imm32' # no change (we're just not printing metadata to the trace) -+translate: instruction after packing: 'bb foo' ++transform: instruction after packing: 'bb foo' $error: 0 :(scenarios run) diff --git a/subx/025non_code_segment.cc b/subx/025non_code_segment.cc index ba713cca..5006e9a1 100644 --- a/subx/025non_code_segment.cc +++ b/subx/025non_code_segment.cc @@ -9,9 +9,10 @@ cd 12/imm8 +error: 12/imm8: metadata imm8 is only allowed in the (first) code segment :(before "End One-time Setup") -Transform.push_back(check_operands_in_non_code_segments); +Transform.push_back(ensure_operands_only_in_code_segments); :(code) -void check_operands_in_non_code_segments(/*const*/ program& p) { +void ensure_operands_only_in_code_segments(/*const*/ program& p) { + trace(99, "transform") << "-- ensure operands only in code segments" << end(); if (p.segments.empty()) return; for (int i = /*skip code segment*/1; i < SIZE(p.segments); ++i) { const segment& seg = p.segments.at(i); diff --git a/subx/026labels.cc b/subx/026labels.cc index fc52fe74..cc11eb0b 100644 --- a/subx/026labels.cc +++ b/subx/026labels.cc @@ -9,13 +9,14 @@ # 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 loop: 05 0x0d0c0b0a/imm32 # add to EAX -+translate: label 'loop' is at address 1 ++transform: label 'loop' is at address 1 :(before "End One-time Setup") -Transform.push_back(replace_labels); +Transform.push_back(rewrite_labels); :(code) -void replace_labels(program& p) { +void rewrite_labels(program& p) { + trace(99, "transform") << "-- rewrite labels" << end(); if (p.segments.empty()) return; segment& code = p.segments.at(0); map<string, uint32_t> address; @@ -53,7 +54,7 @@ void compute_addresses_for_labels(const segment& code, map<string, uint32_t> add raise << "'" << to_string(inst) << "': labels can only be the first word in a line.\n" << end(); string label = curr.data.substr(0, SIZE(curr.data)-1); put(address, label, current_byte); - trace(99, "translate") << "label '" << label << "' is at address " << (current_byte+code.start) << end(); + trace(99, "transform") << "label '" << label << "' is at address " << (current_byte+code.start) << end(); // no modifying current_byte; label definitions won't be in the final binary } } @@ -90,6 +91,6 @@ loop2: 05 0x0d0c0b0a/imm32 # add to EAX loop3: f -+translate: label 'loop' is at address 1 -+translate: label 'loop2' is at address 1 -+translate: label 'loop3' is at address 6 ++transform: label 'loop' is at address 1 ++transform: label 'loop2' is at address 1 ++transform: label 'loop3' is at address 6 |