zydis/src/Utils.c

95 lines
3.7 KiB
C
Raw Normal View History

2016-05-26 03:25:48 +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 <stdint.h>
#include <Zydis/Utils.h>
/* ============================================================================================== */
/* Operand utils */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Exported functions */
/* ---------------------------------------------------------------------------------------------- */
ZydisStatus ZydisUtilsCalcAbsoluteTargetAddress(const ZydisInstructionInfo* info,
const ZydisOperandInfo* operand, uint64_t* address)
{
if (!info || !operand || !address)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
switch (operand->type)
{
case ZYDIS_OPERAND_TYPE_MEMORY:
if (operand->mem.disp.dataSize == 0)
2016-05-26 03:25:48 +08:00
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
if (operand->mem.base == ZYDIS_REGISTER_EIP)
{
2017-04-10 05:11:16 +08:00
*address = (uint64_t)((uint32_t)info->instrPointer + operand->mem.disp.value.sdword);
2016-05-26 03:25:48 +08:00
return ZYDIS_STATUS_SUCCESS;
}
if (operand->mem.base == ZYDIS_REGISTER_RIP)
{
2017-04-10 05:11:16 +08:00
*address =
(uint64_t)(info->instrPointer + operand->mem.disp.value.sqword);
2016-05-26 03:25:48 +08:00
return ZYDIS_STATUS_SUCCESS;
}
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-04-10 05:11:16 +08:00
*address =
(uint64_t)((int64_t)info->instrPointer + info->length + operand->imm.value.sqword);
switch (info->machineMode)
2016-05-26 03:25:48 +08:00
{
case 16:
case 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;
case 64:
break;
default:
return ZYDIS_STATUS_INVALID_PARAMETER;
2016-05-26 03:25:48 +08:00
}
return ZYDIS_STATUS_SUCCESS;
}
break;
default:
break;
2016-05-26 03:25:48 +08:00
}
return ZYDIS_STATUS_INVALID_PARAMETER;
}
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */