zydis/tools/ZydisDisasm.c

148 lines
5.2 KiB
C
Raw Normal View History

2017-01-18 03:53:34 +08:00
/***************************************************************************************************
2016-05-26 03:25:48 +08:00
Zyan Disassembler Engine (Zydis)
Original Author : Florian Bernd, Joel Höner
* 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 <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <Zydis/Zydis.h>
//#include "Zydis/Encoder.h"
2016-05-26 03:25:48 +08:00
/* ============================================================================================== */
/* Entry point */
/* ============================================================================================== */
int main(int argc, char** argv)
{
2017-09-20 21:46:51 +08:00
if (ZydisGetVersion() != ZYDIS_VERSION)
{
fputs("Invalid zydis version\n", stderr);
return EXIT_FAILURE;
}
2017-01-11 19:18:26 +08:00
if (argc < 1 || argc > 2)
2016-05-26 03:25:48 +08:00
{
2017-01-11 19:18:26 +08:00
fprintf(stderr, "Usage: %s [input file]\n", (argc > 0 ? argv[0] : "ZydisDisasm"));
2016-05-26 03:25:48 +08:00
return EXIT_FAILURE;
}
2017-01-11 19:18:26 +08:00
FILE* file = argc >= 2 ? fopen(argv[1], "rb") : stdin;
2016-05-26 03:25:48 +08:00
if (!file)
{
fprintf(stderr, "Can not open file: %s\n", strerror(errno));
return EXIT_FAILURE;
}
2017-07-03 23:36:03 +08:00
ZydisDecoder decoder;
if (!ZYDIS_SUCCESS(
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64)))
2017-06-26 05:28:15 +08:00
{
fputs("Failed to initialize decoder\n", stderr);
return EXIT_FAILURE;
}
2017-07-03 23:36:03 +08:00
ZydisFormatter formatter;
if (!ZYDIS_SUCCESS(ZydisFormatterInitEx(&formatter, ZYDIS_FORMATTER_STYLE_INTEL,
ZYDIS_FMTFLAG_FORCE_SEGMENTS | ZYDIS_FMTFLAG_FORCE_OPERANDSIZE,
ZYDIS_FORMATTER_ADDR_ABSOLUTE, ZYDIS_FORMATTER_DISP_DEFAULT, ZYDIS_FORMATTER_IMM_DEFAULT)))
2016-05-26 03:25:48 +08:00
{
fputs("Failed to initialized instruction-formatter\n", stderr);
return EXIT_FAILURE;
}
2017-01-12 17:29:42 +08:00
uint8_t readBuf[ZYDIS_MAX_INSTRUCTION_LENGTH * 1024];
2017-01-11 19:18:26 +08:00
size_t numBytesRead;
do
2016-05-26 03:25:48 +08:00
{
2017-01-11 19:18:26 +08:00
numBytesRead = fread(readBuf, 1, sizeof(readBuf), file);
2017-07-03 09:58:25 +08:00
ZydisDecodedInstruction instruction;
2017-01-11 19:18:26 +08:00
ZydisStatus status;
size_t readOffs = 0;
2017-07-03 09:58:25 +08:00
while ((status = ZydisDecoderDecodeBuffer(&decoder, readBuf + readOffs,
numBytesRead - readOffs, readOffs, &instruction)) != ZYDIS_STATUS_NO_MORE_DATA)
2016-05-26 03:25:48 +08:00
{
2017-01-11 19:18:26 +08:00
if (!ZYDIS_SUCCESS(status))
{
++readOffs;
2017-07-03 09:58:25 +08:00
printf("db %02X\n", instruction.data[0]);
2017-01-11 19:18:26 +08:00
continue;
}
char printBuffer[256];
ZydisFormatterFormatInstruction(
2017-07-03 09:58:25 +08:00
&formatter, &instruction, printBuffer, sizeof(printBuffer));
puts(printBuffer);
2017-01-18 03:53:34 +08:00
// TODO: Remove
// DEBUG CODE START
#if 0
for (size_t i = 0; i < instruction.length; ++i)
{
printf("%02X ", *(readBuf + readOffs + i));
}
putchar('\n');
ZydisEncoderRequest req;
ZydisStatus transStatus = ZydisEncoderDecodedInstructionToRequest(
&instruction, &req
);
(void)transStatus;
ZYDIS_ASSERT(ZYDIS_SUCCESS(transStatus));
uint8_t encBuffer[15];
size_t encBufferSize = sizeof(encBuffer);
ZydisStatus encStatus = ZydisEncoderEncodeInstruction(
encBuffer, &encBufferSize, &req
);
2017-01-23 00:38:14 +08:00
(void)encStatus;
ZYDIS_ASSERT(ZYDIS_SUCCESS(encStatus));
for (size_t i = 0; i < encBufferSize; ++i)
{
printf("%02X ", encBuffer[i]);
}
putchar('\n');
ZYDIS_ASSERT(encBufferSize == instruction.length);
ZYDIS_ASSERT(!memcmp(encBuffer, readBuf + readOffs, encBufferSize));
#endif
2017-01-18 03:53:34 +08:00
// DEBUG CODE END
2017-07-03 09:58:25 +08:00
readOffs += instruction.length;
2017-01-11 19:18:26 +08:00
}
if (readOffs < sizeof(readBuf))
{
memmove(readBuf, readBuf + readOffs, sizeof(readBuf) - readOffs);
2016-05-26 03:25:48 +08:00
}
2017-01-11 19:18:26 +08:00
} while (numBytesRead == sizeof(readBuf));
2016-05-26 03:25:48 +08:00
2017-01-11 19:18:26 +08:00
return 0;
2016-05-26 03:25:48 +08:00
}
/* ============================================================================================== */