2016-05-26 03:25:48 +08:00
|
|
|
/***************************************************************************************************
|
|
|
|
|
2016-12-05 09:24:01 +08:00
|
|
|
Zyan Disassembler Library (Zydis)
|
2016-05-26 03:25:48 +08:00
|
|
|
|
|
|
|
Original Author : Florian Bernd
|
|
|
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
|
|
|
|
***************************************************************************************************/
|
|
|
|
|
|
|
|
#include <Zydis/Utils.h>
|
|
|
|
|
|
|
|
/* ============================================================================================== */
|
2017-07-12 00:51:54 +08:00
|
|
|
/* Address calculation */
|
2016-05-26 03:25:48 +08:00
|
|
|
/* ============================================================================================== */
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
/* Exported functions */
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
2017-09-25 23:59:14 +08:00
|
|
|
ZydisStatus ZydisCalcAbsoluteAddress(const ZydisDecodedInstruction* instruction,
|
2017-07-03 09:14:01 +08:00
|
|
|
const ZydisDecodedOperand* operand, uint64_t* address)
|
2016-05-26 03:25:48 +08:00
|
|
|
{
|
2017-07-03 09:14:01 +08:00
|
|
|
if (!instruction || !operand || !address)
|
2016-05-26 03:25:48 +08:00
|
|
|
{
|
|
|
|
return ZYDIS_STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
switch (operand->type)
|
|
|
|
{
|
|
|
|
case ZYDIS_OPERAND_TYPE_MEMORY:
|
2017-06-14 02:17:20 +08:00
|
|
|
if (!operand->mem.disp.hasDisplacement)
|
2016-05-26 03:25:48 +08:00
|
|
|
{
|
|
|
|
return ZYDIS_STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
if (operand->mem.base == ZYDIS_REGISTER_EIP)
|
|
|
|
{
|
2017-07-03 09:14:01 +08:00
|
|
|
*address =
|
|
|
|
(uint64_t)((uint32_t)instruction->instrPointer + (uint32_t)operand->mem.disp.value);
|
2016-05-26 03:25:48 +08:00
|
|
|
return ZYDIS_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
if (operand->mem.base == ZYDIS_REGISTER_RIP)
|
|
|
|
{
|
2017-07-03 09:14:01 +08:00
|
|
|
*address = (uint64_t)(instruction->instrPointer + operand->mem.disp.value);
|
2016-05-26 03:25:48 +08:00
|
|
|
return ZYDIS_STATUS_SUCCESS;
|
|
|
|
}
|
2017-09-25 23:59:14 +08:00
|
|
|
if ((operand->mem.base == ZYDIS_REGISTER_NONE) &&
|
|
|
|
(operand->mem.index == ZYDIS_REGISTER_NONE))
|
|
|
|
{
|
|
|
|
switch (instruction->addressWidth)
|
|
|
|
{
|
|
|
|
case 16:
|
|
|
|
*address = (uint64_t)operand->mem.disp.value & 0x000000000000FFFF;
|
|
|
|
return ZYDIS_STATUS_SUCCESS;
|
|
|
|
case 32:
|
|
|
|
*address = (uint64_t)operand->mem.disp.value & 0x00000000FFFFFFFF;
|
|
|
|
return ZYDIS_STATUS_SUCCESS;
|
|
|
|
case 64:
|
|
|
|
*address = (uint64_t)operand->mem.disp.value;
|
|
|
|
return ZYDIS_STATUS_SUCCESS;
|
|
|
|
default:
|
|
|
|
return ZYDIS_STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 03:25:48 +08:00
|
|
|
break;
|
|
|
|
case ZYDIS_OPERAND_TYPE_IMMEDIATE:
|
2016-11-21 21:55:17 +08:00
|
|
|
if (operand->imm.isSigned && operand->imm.isRelative)
|
2016-05-26 03:25:48 +08:00
|
|
|
{
|
2017-07-03 09:14:01 +08:00
|
|
|
*address = (uint64_t)((int64_t)instruction->instrPointer + operand->imm.value.s);
|
|
|
|
switch (instruction->machineMode)
|
2016-05-26 03:25:48 +08:00
|
|
|
{
|
2017-11-14 14:33:15 +08:00
|
|
|
case ZYDIS_MACHINE_MODE_LONG_COMPAT_16:
|
|
|
|
case ZYDIS_MACHINE_MODE_LEGACY_16:
|
|
|
|
case ZYDIS_MACHINE_MODE_REAL_16:
|
|
|
|
case ZYDIS_MACHINE_MODE_LONG_COMPAT_32:
|
|
|
|
case ZYDIS_MACHINE_MODE_LEGACY_32:
|
2016-11-21 21:55:17 +08:00
|
|
|
if (operand->size == 16)
|
2016-05-26 03:25:48 +08:00
|
|
|
{
|
2016-11-21 21:55:17 +08:00
|
|
|
*address &= 0xFFFF;
|
2016-05-26 03:25:48 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-11-14 14:33:15 +08:00
|
|
|
case ZYDIS_MACHINE_MODE_LONG_64:
|
2016-11-12 05:03:26 +08:00
|
|
|
break;
|
2016-11-14 09:10:59 +08:00
|
|
|
default:
|
|
|
|
return ZYDIS_STATUS_INVALID_PARAMETER;
|
2016-05-26 03:25:48 +08:00
|
|
|
}
|
|
|
|
return ZYDIS_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-12 05:03:26 +08:00
|
|
|
default:
|
|
|
|
break;
|
2016-05-26 03:25:48 +08:00
|
|
|
}
|
|
|
|
return ZYDIS_STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* ============================================================================================== */
|
2017-07-12 00:51:54 +08:00
|
|
|
/* Flags */
|
|
|
|
/* ============================================================================================== */
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
/* Exported functions */
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
2017-09-11 03:43:52 +08:00
|
|
|
ZydisStatus ZydisGetAccessedFlagsByAction(const ZydisDecodedInstruction* instruction,
|
2017-07-12 00:51:54 +08:00
|
|
|
ZydisCPUFlagAction action, ZydisCPUFlagMask* flags)
|
|
|
|
{
|
|
|
|
if (!instruction)
|
|
|
|
{
|
|
|
|
return ZYDIS_STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
*flags = 0;
|
2017-09-11 03:43:52 +08:00
|
|
|
for (uint8_t i = 0; i < ZYDIS_ARRAY_SIZE(instruction->accessedFlags); ++i)
|
2017-07-12 00:51:54 +08:00
|
|
|
{
|
2017-09-11 03:43:52 +08:00
|
|
|
if (instruction->accessedFlags[i].action == action)
|
2017-07-12 00:51:54 +08:00
|
|
|
{
|
|
|
|
*flags |= (1 << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ZYDIS_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* ============================================================================================== */
|