| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 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 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 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. |
| 14 |
* |
| 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 |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "hints_t.h" |
| 21 |
|
| 22 |
#include <fcml_renderer.h> |
| 23 |
|
| 24 |
#include "instructions_base_t.h" |
| 25 |
|
| 26 |
fcml_bool fcml_tf_hints_suite_init(void) { |
| 27 |
return FCML_TRUE; |
| 28 |
} |
| 29 |
|
| 30 |
fcml_bool fcml_tf_hints_suite_cleanup(void) { |
| 31 |
return FCML_TRUE; |
| 32 |
} |
| 33 |
|
| 34 |
void fcml_fn_hints_sib(void) { |
| 35 |
FCML_I32( "add dword ptr [eax+00000001h],eax", 0x01, 0x40, 0x01 ); |
| 36 |
FCML_A32( "add %eax,0x00000001(%eax)", 0x01, 0x40, 0x01 ); |
| 37 |
FCML_I32_A( "add dword ptr [sib eax+00000001h],eax", 0x01, 0x44, 0x20, 0x01 ); |
| 38 |
FCML_A32_D( "add %eax,0x00000001(%eax)", 0x01, 0x44, 0x20, 0x01 ); |
| 39 |
FCML_I32( "add dword ptr [eax],eax", 0x01, 0x00 ); |
| 40 |
FCML_I32_A( "add dword ptr [sib eax],eax", 0x01, 0x04, 0x20 ); |
| 41 |
} |
| 42 |
|
| 43 |
void fcml_fn_hints_rip(void) { |
| 44 |
/* RIP enabled by default.*/ |
| 45 |
FCML_I64_RIP_A( "rcl byte ptr [0000800000401007h],03h", 0xC0, 0x15, 0x00, 0x00, 0x00, 0x00, 0x03 ); |
| 46 |
/* RIP by default, absolute forced, take into account that SIB encoding was used to encode it.*/ |
| 47 |
FCML_I64_RIP_A( "rcl byte ptr [abs 0000000000401007h],03h", 0xc0, 0x14, 0x25, 0x07, 0x10, 0x40, 0x00, 0x03 ); |
| 48 |
/* Offset is too big to be encoded as absolute offset.*/ |
| 49 |
FCML_I64_A_FAILED( "rcl byte ptr [0000800000401007h],03h", 0xC0, 0x15, 0x00, 0x00, 0x00, 0x00, 0x03 ); |
| 50 |
/* Absolute offset enabled by default.*/ |
| 51 |
FCML_I64( "rcl byte ptr [0000000000401007h],03h", 0xc0, 0x14, 0x25, 0x07, 0x10, 0x40, 0x00, 0x03 ); |
| 52 |
/* In 32 bit mode, SIB is not needed, so it is not used by default, but...*/ |
| 53 |
FCML_I32( "rcl byte ptr [00401007h],03h", 0xc0, 0x15, 0x07, 0x10, 0x40, 0x00, 0x03 ); |
| 54 |
/* we can force it.*/ |
| 55 |
FCML_I32_A( "rcl byte ptr [sib 00401007h],03h", 0xc0, 0x14, 0x25, 0x07, 0x10, 0x40, 0x00, 0x03 ); |
| 56 |
/* It's interesting combination. RIP should be used by default, but SIB hint was used. Hints have higher precedence*/ |
| 57 |
/* so it forced absolute address to be used (Because absolute offset is encoded using SIB). In this case SIB hint works like the ABS one.*/ |
| 58 |
FCML_I64_RIP_A( "rcl byte ptr [sib 00401007h],03h", 0xc0, 0x14, 0x25, 0x07, 0x10, 0x40, 0x00, 0x03 ); |
| 59 |
/* Absolute by default but RIP forced.*/ |
| 60 |
FCML_I64_A( "rcl byte ptr [rel 0000800000401007h],03h", 0xc0, 0x15, 0x00, 0x00, 0x00, 0x00, 0x03 ); |
| 61 |
/* Hints rendering.*/ |
| 62 |
FCML_I64_RF( "rcl byte ptr [rel 0000800000401007h],03h", FCML_REND_FLAG_RENDER_REL_HINT, 0xc0, 0x15, 0x00, 0x00, 0x00, 0x00, 0x03 ); |
| 63 |
FCML_I64_RF( "rcl byte ptr [abs 0000000000401007h],03h", FCML_REND_FLAG_RENDER_ABS_HINT, 0xc0, 0x14, 0x25, 0x07, 0x10, 0x40, 0x00, 0x03 ); |
| 64 |
} |
| 65 |
|
| 66 |
void fcml_fn_hints_indirect_addressing(void) { |
| 67 |
|
| 68 |
/* 1: 0xFF, 0x25, 0x01, 0x10, 0x40, 0x00 (jmp dword [00401001h]) |
| 69 |
* By default indirect addressing is chosen. |
| 70 |
*/ |
| 71 |
|
| 72 |
fcml_st_assembler_context context = {0}; |
| 73 |
context.assembler = assembler_intel; |
| 74 |
context.entry_point.op_mode = FCML_OM_32_BIT; |
| 75 |
context.entry_point.ip = 0x00401000; |
| 76 |
|
| 77 |
fcml_st_instruction instruction = {0}; |
| 78 |
instruction.mnemonic = "jmp"; |
| 79 |
instruction.operands[0].type = FCML_OT_ADDRESS; |
| 80 |
instruction.operands[0].address.address_form = FCML_AF_OFFSET; |
| 81 |
instruction.operands[0].address.size_operator = FCML_DS_32; |
| 82 |
instruction.operands[0].address.offset.off32 = 0x00401001; |
| 83 |
instruction.operands[0].address.offset.size = FCML_DS_32; |
| 84 |
instruction.operands_count = 1; |
| 85 |
|
| 86 |
fcml_st_assembler_result result; |
| 87 |
|
| 88 |
fcml_fn_assembler_result_prepare( &result ); |
| 89 |
|
| 90 |
fcml_ceh_error error; |
| 91 |
if( !( error = fcml_fn_assemble( &context, &instruction, &result ) ) ) { |
| 92 |
STF_ASSERT_PTR_NOT_NULL( result.chosen_instruction ); |
| 93 |
if( result.chosen_instruction ) { |
| 94 |
STF_ASSERT_EQUAL( 6, result.chosen_instruction->code_length ); |
| 95 |
if( result.chosen_instruction->code_length == 6 ) { |
| 96 |
STF_ASSERT_EQUAL( 0xFF, result.chosen_instruction->code[0] ); |
| 97 |
STF_ASSERT_EQUAL( 0x25, result.chosen_instruction->code[1] ); |
| 98 |
STF_ASSERT_EQUAL( 0x01, result.chosen_instruction->code[2] ); |
| 99 |
STF_ASSERT_EQUAL( 0x10, result.chosen_instruction->code[3] ); |
| 100 |
STF_ASSERT_EQUAL( 0x40, result.chosen_instruction->code[4] ); |
| 101 |
STF_ASSERT_EQUAL( 0x00, result.chosen_instruction->code[5] ); |
| 102 |
} |
| 103 |
} |
| 104 |
fcml_fn_assembler_result_free( &result ); |
| 105 |
} else { |
| 106 |
STF_FAIL("Can not assemble instruction."); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
void fcml_fn_hints_direct_addressing(void) { |
| 112 |
|
| 113 |
/* 1: 0xeb, 0xff (jmp 00401001h) |
| 114 |
* Forcing indirect addressing. |
| 115 |
*/ |
| 116 |
|
| 117 |
fcml_st_assembler_context context = {0}; |
| 118 |
context.assembler = assembler_intel; |
| 119 |
context.entry_point.op_mode = FCML_OM_32_BIT; |
| 120 |
context.entry_point.ip = 0x00401000; |
| 121 |
|
| 122 |
fcml_st_instruction instruction = {0}; |
| 123 |
instruction.mnemonic = "jmp"; |
| 124 |
instruction.operands[0].type = FCML_OT_ADDRESS; |
| 125 |
instruction.operands[0].address.address_form = FCML_AF_OFFSET; |
| 126 |
instruction.operands[0].address.offset.off32 = 0x00401001; |
| 127 |
instruction.operands[0].address.offset.size = FCML_DS_32; |
| 128 |
instruction.hints |= FCML_HINT_DIRECT_POINTER; |
| 129 |
instruction.operands_count = 1; |
| 130 |
|
| 131 |
fcml_st_assembler_result result; |
| 132 |
|
| 133 |
fcml_fn_assembler_result_prepare( &result ); |
| 134 |
|
| 135 |
fcml_ceh_error error; |
| 136 |
if( !( error = fcml_fn_assemble( &context, &instruction, &result ) ) ) { |
| 137 |
STF_ASSERT_PTR_NOT_NULL( result.chosen_instruction ); |
| 138 |
if( result.chosen_instruction ) { |
| 139 |
STF_ASSERT_EQUAL( 2, result.chosen_instruction->code_length ); |
| 140 |
if( result.chosen_instruction->code_length == 2 ) { |
| 141 |
STF_ASSERT_EQUAL( 0xEB, result.chosen_instruction->code[0] ); |
| 142 |
STF_ASSERT_EQUAL( 0xFF, result.chosen_instruction->code[1] ); |
| 143 |
} |
| 144 |
} |
| 145 |
fcml_fn_assembler_result_free( &result ); |
| 146 |
} else { |
| 147 |
STF_FAIL("Can not assemble instruction."); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
fcml_stf_test_case fcml_ti_hints[] = { |
| 154 |
{ "fcml_fn_hints_sib", fcml_fn_hints_sib }, |
| 155 |
{ "fcml_fn_hints_rip", fcml_fn_hints_rip }, |
| 156 |
{ "fcml_fn_hints_indirect_addressing", fcml_fn_hints_indirect_addressing }, |
| 157 |
{ "fcml_fn_hints_direct_addressing", fcml_fn_hints_direct_addressing }, |
| 158 |
FCML_STF_NULL_TEST |
| 159 |
}; |
| 160 |
|
| 161 |
fcml_stf_test_suite fcml_si_hints = { |
| 162 |
"suite-fcml-hints", fcml_tf_hints_suite_init, fcml_tf_hints_suite_cleanup, fcml_ti_hints |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
|