Added Zydis lib support for Trace enabled build

This commit is contained in:
ptitSeb 2021-03-02 14:49:36 +01:00
parent a6143f33ce
commit 1b9d1dc609
5 changed files with 4482 additions and 2 deletions

View File

@ -106,6 +106,7 @@ set(ELFLOADER_SRC
"${BOX64_ROOT}/src/emu/x64emu.c"
"${BOX64_ROOT}/src/emu/x64int3.c"
"${BOX64_ROOT}/src/emu/x87emu_private.c"
"${BOX64_ROOT}/src/emu/x64trace.c"
"${BOX64_ROOT}/src/libtools/auxval.c"
"${BOX64_ROOT}/src/tools/box64stack.c"
"${BOX64_ROOT}/src/tools/pathcoll.c"

111
src/emu/x64trace.c Executable file
View File

@ -0,0 +1,111 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "debug.h"
#include "x64trace.h"
#include "box64context.h"
#include "x86zydis.h"
#include "x64emu_private.h"
typedef ZyanStatus (*PFNZydisDecoderInit)(ZydisDecoder* decoder, ZydisMachineMode machine_mode,
ZydisAddressWidth address_width);
typedef ZyanStatus (*PFNZydisFormatterInit)(ZydisFormatter* formatter, ZydisFormatterStyle style);
typedef ZyanStatus (*PFNZydisDecoderDecodeBuffer)(const ZydisDecoder* decoder,
const void* buffer, ZyanUSize length, ZydisDecodedInstruction* instruction);
typedef ZyanStatus (*PFNZydisFormatterFormatInstruction)(const ZydisFormatter* formatter,
const ZydisDecodedInstruction* instruction, char* buffer, ZyanUSize length,
ZyanU64 runtime_address);
typedef struct zydis_s {
void* lib;
PFNZydisDecoderInit ZydisDecoderInit;
PFNZydisFormatterInit ZydisFormatterInit;
PFNZydisDecoderDecodeBuffer ZydisDecoderDecodeBuffer;
PFNZydisFormatterFormatInstruction ZydisFormatterFormatInstruction;
} zydis_t;
typedef struct zydis_dec_s {
ZydisDecoder decoder;
ZydisFormatter formatter;
ZydisDecodedInstruction instruction;
PFNZydisDecoderDecodeBuffer ZydisDecoderDecodeBuffer;
PFNZydisFormatterFormatInstruction ZydisFormatterFormatInstruction;
} zydis_dec_t;
int InitX64Trace(box64context_t *context)
{
if(context->zydis)
return 0;
context->zydis = (zydis_t*)calloc(1, sizeof(zydis_t));
if(!context->zydis)
return 1;
context->zydis->lib = dlopen("libZydis.so", RTLD_LAZY);
if(!context->zydis->lib) {
printf_log(LOG_INFO, "Failed to open libZydis: %s\n", dlerror());
return 1;
}
#define GO(f) context->zydis->f = (PFN##f)dlsym(context->zydis->lib, #f);\
if(!context->zydis->f) {printf_log(LOG_INFO, "Fail to load Zydis function %s\n", #f); dlclose(context->zydis->lib); context->zydis->lib=NULL; return 1;}
GO(ZydisDecoderInit);
GO(ZydisFormatterInit);
GO(ZydisDecoderDecodeBuffer);
GO(ZydisFormatterFormatInstruction);
#undef GO
context->dec = InitX64TraceDecoder(context);
return 0;
}
void DeleteX64Trace(box64context_t *context)
{
if(!context->zydis)
return;
if(context->zydis->lib)
dlclose(context->zydis->lib);
free(context->zydis);
context->zydis = NULL;
}
zydis_dec_t* InitX64TraceDecoder(box64context_t *context)
{
if(!context->zydis)
return NULL;
zydis_dec_t *dec = (zydis_dec_t*)calloc(1, sizeof(zydis_dec_t));
dec->ZydisDecoderDecodeBuffer = context->zydis->ZydisDecoderDecodeBuffer;
dec->ZydisFormatterFormatInstruction = context->zydis->ZydisFormatterFormatInstruction;
context->zydis->ZydisDecoderInit(&dec->decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64);
context->zydis->ZydisFormatterInit(&dec->formatter, ZYDIS_FORMATTER_STYLE_INTEL);
return dec;
}
void DeleteX64TraceDecoder(zydis_dec_t **dec)
{
free(*dec);
*dec = NULL;
}
const char* DecodeX64Trace(zydis_dec_t *dec, uintptr_t p)
{
static char buff[256];
if(ZYAN_SUCCESS(dec->ZydisDecoderDecodeBuffer(&dec->decoder, (char*)p, 15,
&dec->instruction))) {
char tmp[255];
buff[0]='\0';
for (int i=0; i<dec->instruction.length; ++i) {
sprintf(tmp, "%02X ", *((unsigned char*)p+i));
strcat(buff, tmp);
}
dec->ZydisFormatterFormatInstruction(&dec->formatter, &dec->instruction, tmp, sizeof(tmp),p);
strcat(buff, tmp);
} else {
sprintf(buff, "Decoder failed @%p", (void*)p);
}
return buff;
}

4349
src/emu/x86zydis.h Executable file

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,8 @@
typedef struct elfheader_s elfheader_t;
typedef struct cleanup_s cleanup_t;
typedef struct x64emu_s x64emu_t;
typedef struct zydis_s zydis_t;
typedef struct zydis_dec_s zydis_dec_t;
typedef void* (*procaddess_t)(const char* name);
typedef void* (*vkprocaddess_t)(void* instance, const char* name);
@ -31,7 +33,7 @@ typedef struct box64context_s {
uint32_t sel_serial; // will be increment each time selectors changes
//zydis_t *zydis; // dlopen the zydis dissasembler
zydis_t *zydis; // dlopen the zydis dissasembler
void* box64lib; // dlopen on box86 itself
int argc;
@ -65,7 +67,7 @@ typedef struct box64context_s {
int clean_sz;
int clean_cap;
//zydis_dec_t *dec; // trace
zydis_dec_t *dec; // trace
uint8_t canary[4];

17
src/include/x64trace.h Executable file
View File

@ -0,0 +1,17 @@
#ifndef __X64TRACE_H_
#define __X64TRACE_H_
#include <stdint.h>
typedef struct box64context_s box64context_t;
typedef struct zydis_dec_s zydis_dec_t;
int InitX64Trace(box64context_t *context);
void DeleteX64Trace(box64context_t *context);
zydis_dec_t* InitX64TraceDecoder(box64context_t *context);
void DeleteX64TraceDecoder(zydis_dec_t **dec);
const char* DecodeX64Trace(zydis_dec_t *dec, uintptr_t p);
#define ZYDIS_RUNTIME_ADDRESS_NONE (uint64_t)(-1)
#endif //__X64TRACE_H_