Skip to content

Commit

Permalink
cleaned up unix includes from libmem.h
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbo committed Dec 22, 2022
1 parent cc86bc6 commit 21e411f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 250 deletions.
140 changes: 3 additions & 137 deletions libmem/include/libmem.h
Expand Up @@ -289,44 +289,11 @@
# include <Windows.h>
# include <TlHelp32.h>
# include <Psapi.h>
#elif LM_OS == LM_OS_LINUX || LM_OS == LM_OS_ANDROID
# include <dirent.h>
# include <errno.h>
# include <sys/types.h>
# include <unistd.h>
# include <sys/stat.h>
# include <sys/ptrace.h>
# include <sys/wait.h>
# include <sys/mman.h>
# include <sys/user.h>
# include <sys/syscall.h>
# include <sys/utsname.h>
# if LM_OS != LM_OS_ANDROID
# include <sys/io.h>
# endif
# include <sys/uio.h>
# include <dlfcn.h>
# include <fcntl.h>
#elif LM_OS == LM_OS_BSD
# include <dirent.h>
# include <errno.h>
#else
# include <sys/types.h>
# include <unistd.h>
# include <sys/stat.h>
# include <sys/param.h>
# include <sys/ptrace.h>
# include <sys/wait.h>
# include <sys/mman.h>
# include <sys/user.h>
# include <sys/sysctl.h>
# include <sys/syscall.h>
# include <sys/utsname.h>
# include <machine/reg.h>
# include <dlfcn.h>
# include <fcntl.h>
# include <kvm.h>
# include <libprocstat.h>
# include <paths.h>
# include <limits.h>
#endif /* LM_OS */

#if LM_LANG == LM_LANG_CPP
Expand Down Expand Up @@ -423,114 +390,13 @@ typedef struct {
lm_flags_t flags;
} lm_page_t;

enum {
LM_DETOUR_ANY,
# if LM_ARCH == LM_ARCH_X86
LM_DETOUR_JMP32,
LM_DETOUR_JMP64,
LM_DETOUR_CALL32,
LM_DETOUR_CALL64,
LM_DETOUR_RET32,
LM_DETOUR_RET64,
# endif
LM_DETOUR_INVAL
};

typedef lm_int_t lm_detour_t;

/* LM_GetModule(Ex) Flags */
enum {
LM_MOD_BY_STR = 0,
LM_MOD_BY_ADDR
};

enum {
LM_DATLOC_INVAL = 0,
# if LM_ARCH == LM_ARCH_X86
/* x86_32 */
LM_DATLOC_EAX,
LM_DATLOC_EBX,
LM_DATLOC_ECX,
LM_DATLOC_EDX,
LM_DATLOC_ESI,
LM_DATLOC_EDI,
LM_DATLOC_ESP,
LM_DATLOC_EBP,
LM_DATLOC_EIP,
/*
LM_DATLOC_XMM0,
LM_DATLOC_XMM1,
LM_DATLOC_XMM2,
LM_DATLOC_XMM3,
LM_DATLOC_XMM4,
LM_DATLOC_XMM5,
LM_DATLOC_XMM6,
LM_DATLOC_XMM7,
*/
/* x86_64 */
# if LM_BITS == 64
LM_DATLOC_RAX,
LM_DATLOC_RBX,
LM_DATLOC_RCX,
LM_DATLOC_RDX,
LM_DATLOC_RSI,
LM_DATLOC_RDI,
LM_DATLOC_RSP,
LM_DATLOC_RBP,
LM_DATLOC_RIP,
LM_DATLOC_R8,
LM_DATLOC_R9,
LM_DATLOC_R10,
LM_DATLOC_R11,
LM_DATLOC_R12,
LM_DATLOC_R13,
LM_DATLOC_R14,
LM_DATLOC_R15,
/*
LM_DATLOC_XMM8,
LM_DATLOC_XMM9,
LM_DATLOC_XMM10,
LM_DATLOC_XMM11,
LM_DATLOC_XMM12,
LM_DATLOC_XMM13,
LM_DATLOC_XMM14,
LM_DATLOC_XMM15,
*/
# endif
# elif LM_ARCH == LM_ARCH_ARM
# endif
LM_DATLOC_STACK
};

typedef lm_int_t lm_datloc_t;

typedef struct {
lm_datloc_t datloc;
lm_size_t size;
lm_byte_t *data;
} lm_datio_t;

typedef struct {
# if LM_OS == LM_OS_WIN
CONTEXT regs;
# if LM_BITS == 64
WOW64_CONTEXT regs32;
# endif
# elif LM_OS == LM_OS_LINUX || LM_OS == LM_OS_ANDROID
# if LM_ARCH == LM_ARCH_X86
struct user_regs_struct regs;
struct user_fpregs_struct fpregs;
# elif LM_ARCH == LM_ARCH_ARM
struct user regs;
# endif
# elif LM_OS == LM_OS_BSD
# if LM_ARCH == LM_ARCH_X86
struct reg regs;
struct fpreg fpregs;
# elif LM_ARCH == LM_ARCH_ARM
# endif
# endif
} lm_regs_t;
typedef lm_int_t lm_detour_t;

/* Based from instruction struct from capstone.h */
typedef struct {
Expand Down
2 changes: 2 additions & 0 deletions libmem/src/helpers.c
@@ -1,6 +1,8 @@
#include "internal.h"

#if LM_OS != LM_OS_WIN
#include <fcntl.h>

LM_PRIVATE lm_size_t
_LM_OpenFileBuf(lm_tstring_t path,
lm_tchar_t **pfilebuf)
Expand Down
115 changes: 2 additions & 113 deletions libmem/src/hook.c
Expand Up @@ -9,117 +9,6 @@ _LM_DetourPayload(lm_address_t src,
{
lm_size_t size = 0;

if (!buf)
return size;

# if LM_ARCH == LM_ARCH_X86
switch (detour) {
case LM_DETOUR_JMP32:
{
lm_byte_t payload[] = {
0xE9, 0x0, 0x0, 0x0, 0x0 /* jmp 0x0 */
};

size = sizeof(payload);

*(lm_uint32_t *)&payload[1] = (lm_uint32_t)(
(lm_uintptr_t)dst - (lm_uintptr_t)src - size
);

*buf = (lm_byte_t *)LM_MALLOC(size);
LM_MEMCPY(*buf, payload, size);
break;
}
case LM_DETOUR_JMP64:
case LM_DETOUR_ANY:
{
if (bits == 64) {
lm_byte_t payload[] = {
0xFF, 0x25, 0x0, 0x0, 0x0, 0x0, /* jmp [rip] */
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* <dst> */
};

size = sizeof(payload);

*(lm_uintptr_t *)&payload[6] = (lm_uintptr_t)dst;

*buf = (lm_byte_t *)LM_MALLOC(size);
LM_MEMCPY(*buf, payload, size);
} else {
lm_byte_t payload[] = {
0xFF, 0x25, 0x0, 0x0, 0x0, 0x0, /* jmp [eip] */
0x0, 0x0, 0x0, 0x0 /* <dst> */
};

size = sizeof(payload);

*(lm_uint32_t *)&payload[6] = (lm_uint32_t)(
(lm_uintptr_t)dst
);

*buf = (lm_byte_t *)LM_MALLOC(size);
LM_MEMCPY(*buf, payload, size);
}
break;
}
case LM_DETOUR_CALL32:
{
lm_byte_t payload[] = {
0xE8, 0x0, 0x0, 0x0, 0x0 /* call 0x0 */
};

size = sizeof(payload);

*(lm_uint32_t *)&payload[1] = (lm_uint32_t)(
(lm_uintptr_t)dst - (lm_uintptr_t)src - size
);

*buf = (lm_byte_t *)LM_MALLOC(size);
LM_MEMCPY(*buf, payload, size);
break;
}
case LM_DETOUR_CALL64:
{
if (bits == 64) {
lm_byte_t payload[] = {
0xFF, 0x15, 0x0, 0x0, 0x0, 0x0, /* call [rip] */
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 /* <dst> */
};

size = sizeof(payload);

*(lm_uintptr_t *)&payload[6] = (lm_uintptr_t)dst;

*buf = (lm_byte_t *)LM_MALLOC(size);
LM_MEMCPY(*buf, payload, size);
} else {
lm_byte_t payload[] = {
0xFF, 0x15, 0x0, 0x0, 0x0, 0x0, /* call [eip] */
0x0, 0x0, 0x0, 0x0 /* <dst> */
};

size = sizeof(payload);

*(lm_uint32_t *)&payload[6] = (lm_uint32_t)(
(lm_uintptr_t)dst
);

*buf = (lm_byte_t *)LM_MALLOC(size);
LM_MEMCPY(*buf, payload, size);
}
break;
}
case LM_DETOUR_RET32:
{
break;
}
case LM_DETOUR_RET64:
{
break;
}
}
# elif LM_ARCH == LM_ARCH_ARM
# endif

return size;
}
Expand Down Expand Up @@ -193,7 +82,7 @@ LM_MakeTrampoline(lm_address_t src,

payload_size = _LM_DetourPayload(LM_NULLPTR,
&((lm_byte_t *)src)[size],
LM_DETOUR_JMP64,
0,
LM_GetProcessBits(),
&payload);

Expand Down Expand Up @@ -237,7 +126,7 @@ LM_MakeTrampolineEx(lm_process_t proc,

payload_size = _LM_DetourPayload(LM_NULLPTR,
&((lm_byte_t *)src)[size],
LM_DETOUR_JMP64,
0,
LM_GetProcessBits(),
&payload);

Expand Down
3 changes: 3 additions & 0 deletions libmem/src/memory.c
@@ -1,4 +1,7 @@
#include "internal.h"
#if LM_OS != LM_OS_WIN
# include <sys/uio.h>
#endif

LM_API lm_size_t
LM_ReadMemory(lm_address_t src,
Expand Down
3 changes: 3 additions & 0 deletions libmem/src/module.c
@@ -1,4 +1,7 @@
#include "internal.h"
#if LM_OS != LM_OS_WIN
# include <dlfcn.h>
#endif

LM_API lm_bool_t
LM_EnumModules(lm_bool_t(*callback)(lm_module_t mod,
Expand Down
4 changes: 4 additions & 0 deletions libmem/src/process.c
@@ -1,4 +1,8 @@
#include "internal.h"
#if LM_OS != LM_OS_WIN
# include <dirent.h>
# include <sys/utsname.h>
#endif

#if LM_OS == LM_OS_WIN
LM_PRIVATE lm_bool_t
Expand Down
1 change: 1 addition & 0 deletions libmem/src/symbol.cpp
Expand Up @@ -29,6 +29,7 @@ _LM_EnumPeSyms(lm_size_t bits,
}
#else
#include <LIEF/ELF.hpp>
#include <fcntl.h>

using namespace LIEF::ELF;

Expand Down
3 changes: 3 additions & 0 deletions libmem/src/thread.c
@@ -1,4 +1,7 @@
#include "internal.h"
#if LM_OS != LM_OS_WIN
# include <dirent.h>
#endif

LM_API lm_bool_t
LM_EnumThreads(lm_bool_t(*callback)(lm_tid_t tid,
Expand Down

0 comments on commit 21e411f

Please sign in to comment.