label creation

main
James 3 years ago
parent 68fc208612
commit 783875b587

@ -1,6 +1,7 @@
#ifndef __CODE_H #ifndef __CODE_H
#define __CODE_H #define __CODE_H
#define CODE_FLAG_IS_LABEL (1<<0) #define CODE_FLAG_IS_LABEL (1<<0)
#define CODE_FLAG_IS_REL_JMP (1<<1)
#endif #endif

@ -94,7 +94,7 @@ BOOL NcCreateLabels(PNATIVE_CODE_BLOCK Block)
XED_OPERAND_TYPE_ENUM OperandType = XedOperandType(Operand); XED_OPERAND_TYPE_ENUM OperandType = XedOperandType(Operand);
if (OperandType != XED_OPERAND_TYPE_IMM && OperandType != XED_OPERAND_TYPE_IMM_CONST) if (OperandType != XED_OPERAND_TYPE_IMM && OperandType != XED_OPERAND_TYPE_IMM_CONST)
{ {
printf("Found jump to non immediate value. Cat: %s\n", XedCategoryEnumToString(Category)); printf("Found jump to non immediate value. Category: %s\n", XedCategoryEnumToString(Category));
continue; continue;
} }
@ -112,21 +112,53 @@ BOOL NcCreateLabels(PNATIVE_CODE_BLOCK Block)
} }
else else
{ {
NcInsertLinkBefore(JmpPos, new NATIVE_CODE_LINK(CurrentLabelId++)); NcInsertLinkBefore(JmpPos, new NATIVE_CODE_LINK(CurrentLabelId));
T->Label = CurrentLabelId;
++CurrentLabelId;
} }
T->Flags |= CODE_FLAG_IS_REL_JMP;
} }
} }
PNATIVE_CODE_LINK NcValidateJmp(PNATIVE_CODE_LINK Jmp, INT32 Delta) PNATIVE_CODE_LINK NcValidateJmp(PNATIVE_CODE_LINK Jmp, INT32 Delta)
{ {
if (Delta < 0) PNATIVE_CODE_LINK T;
if (Delta > 0)
{ {
T = Jmp->Next;
while (Delta > 0 && T)
{
if (T->Flags & CODE_FLAG_IS_LABEL)
continue;
Delta -= XedDecodedInstGetLength(&T->XedInst);
T = T->Next;
}
if (Delta != 0 || !T)
return NULL;
while (T && (T->Flags & CODE_FLAG_IS_LABEL))
T = T->Next;
return T;
} }
else if (Delta > 0) else if (Delta < 0)
{ {
T = Jmp;
while (T)
{
if (T->Flags & CODE_FLAG_IS_LABEL)
continue;
Delta += XedDecodedInstGetLength(&T->XedInst);
if (Delta >= 0)
break;
T = T->Next;
}
if (Delta != 0 || !T)
return NULL;
while (T && (T->Flags & CODE_FLAG_IS_LABEL))
T = T->Next;
return T;
} }
//return the jmp if that delta is zero
return Jmp;
} }
BOOL NcFromBuffer(PNATIVE_CODE_BLOCK Block, PVOID Buffer, ULONG BufferSize) BOOL NcFromBuffer(PNATIVE_CODE_BLOCK Block, PVOID Buffer, ULONG BufferSize)
@ -160,6 +192,8 @@ BOOL NcFromBuffer(PNATIVE_CODE_BLOCK Block, PVOID Buffer, ULONG BufferSize)
Block->Start = Block->Start->Next; Block->Start = Block->Start->Next;
delete StartLink; delete StartLink;
NcCreateLabels(Block);
return TRUE; return TRUE;
} }

@ -23,7 +23,7 @@ typedef struct _NATIVE_CODE_BLOCK
{ {
PNATIVE_CODE_LINK Start; PNATIVE_CODE_LINK Start;
PNATIVE_CODE_LINK End; PNATIVE_CODE_LINK End;
}NATIVE_CODE_BLOCK, * PNATIVE_CODE_BLOCK; }NATIVE_CODE_BLOCK, *PNATIVE_CODE_BLOCK;
VOID NcInsertLinkAfter(PNATIVE_CODE_LINK Link1, PNATIVE_CODE_LINK Link2); VOID NcInsertLinkAfter(PNATIVE_CODE_LINK Link1, PNATIVE_CODE_LINK Link2);

@ -1 +1,17 @@
#include "VmCode.h" #include "VmCode.h"
_VM_CODE_LINK::_VM_CODE_LINK()
{
Flags = 0;
Next = Prev = NULL;
Label = 0;
RawData = NULL;
RawDataSize = 0UL;
}
_VM_CODE_LINK::_VM_CODE_LINK(ULONG LabelId)
: _VM_CODE_LINK()
{
Label = LabelId;
Flags = CODE_FLAG_IS_LABEL;
}

@ -2,7 +2,26 @@
#define __VM_CODE_H #define __VM_CODE_H
#include "Windas.h" #include "Windas.h"
#include "Code.h"
typedef struct _VM_CODE_LINK
{
_VM_CODE_LINK* Next;
_VM_CODE_LINK* Prev;
ULONG Flags;
ULONG Label;
PUCHAR RawData;
ULONG RawDataSize;
_VM_CODE_LINK();
_VM_CODE_LINK(ULONG LabelId);
}VM_CODE_LINK, *PVM_CODE_LINK;
typedef struct _VM_CODE_BLOCK
{
PVM_CODE_LINK Start;
PVM_CODE_LINK End;
}VM_CODE_BLOCK, *PVM_CODE_BLOCK;
#endif #endif

@ -19,9 +19,10 @@ VOID InitXed();
#define XED_ERROR_ENUM xed_error_enum_t #define XED_ERROR_ENUM xed_error_enum_t
#define XED_CATEGORY_ENUM xed_category_enum_t #define XED_CATEGORY_ENUM xed_category_enum_t
#define XedDecode xed_decode
#define XedDecodedInstZero xed_decoded_inst_zero #define XedDecodedInstZero xed_decoded_inst_zero
#define XedDecodedInstSetMode xed_decoded_inst_set_mode #define XedDecodedInstSetMode xed_decoded_inst_set_mode
#define XedDecode xed_decode
#define XedDecodedInstGetLength xed_decoded_inst_get_length #define XedDecodedInstGetLength xed_decoded_inst_get_length
#define XedDecodedInstGetCategory xed_decoded_inst_get_category #define XedDecodedInstGetCategory xed_decoded_inst_get_category
#define XedDecodedInstGetBranchDisplacementWidth xed_decoded_inst_get_branch_displacement_width #define XedDecodedInstGetBranchDisplacementWidth xed_decoded_inst_get_branch_displacement_width

Loading…
Cancel
Save