Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meson build system support #56

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
*~
/build*

# subproject directories
/subprojects/*
!/subprojects/packagefiles/
!/subprojects/*.wrap
/subprojects/zycore.wrap
196 changes: 196 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
project(
'funchook',
'c',
version: '2.0.0',
# Nearly Classpath-exception-2.0, but not exactly. Additional clause:
# "If you modify this library, you must extend this exception to
# your version of the library."
license: 'GPL-2.0-or-later WITH Classpath-exception-2.0',
default_options: [
'c_std=c11',
'warning_level=2',
],
meson_version: '>=1.2',
)

cc = meson.get_compiler('c')

host_cpu_family = host_machine.cpu_family()

if host_cpu_family in ['x86', 'x86_64']
funchook_cpu = 'x86'
default_disasm = 'Zydis'
elif host_cpu_family == 'aarch64'
funchook_cpu = 'arm64'
default_disasm = 'capstone'
endif

host_system = host_machine.system()

funchook_deps = []
cdata = configuration_data()

if host_system in ['windows', 'cygwin']
funchook_os = 'windows'
funchook_deps += cc.find_library('psapi')
else
funchook_os = 'unix'
funchook_deps += dependency('dl')
endif

if host_machine.system() == 'linux'
add_project_arguments(
'-D_GNU_SOURCE',
language: 'c',
)
# musl libc doesn't provide the GNU-specific version of strerror_r
# even when _GNU_SOURCE is defined.
gnu_strerror_r = cc.compiles(
'''
#define _GNU_SOURCE
#include <string.h>
int main()
{
char dummy[128];
return *strerror_r(0, dummy, sizeof(dummy));
}
''',
)
cdata.set10('GNU_SPECIFIC_STRERROR_R', gnu_strerror_r)
endif

disasm = get_option('disasm')
if disasm == 'auto'
funchook_disasm = default_disasm
else
funchook_disasm = disasm
endif

if funchook_disasm == 'Zydis'
zydis_dep = dependency(
'zydis',
version: '>=4.0.0',
default_options: {
'minimal': 'enabled',
'decoder': 'enabled',
},
)
cdata.set10('DISASM_ZYDIS', true)

cdata.set10(
'DISASM_ZYDIS_V5',
cc.has_member(
'ZydisDecodedOperandMem',
'disp.size',
dependencies: zydis_dep.partial_dependency(compile_args: true, includes: true),
prefix: '''
#include <Zydis/Zydis.h>
''',
),
)

funchook_deps += zydis_dep
elif disasm == 'capstone'
funchook_deps += dependency(
'capstone',
version: '>=5.0.0',
default_options: {
'archs': [funchook_cpu],
'x86_reduce': true,
'x86_att_disable': true,
},
)
cdata.set10('DISASM_CAPSTONE', true)
endif

sizeof_void_p = cc.sizeof('void*')
cdata.set('SIZEOF_VOID_P', sizeof_void_p)

configure_file(
output: 'config.h',
configuration: cdata,
)

hdrs = files(
'include/funchook.h',
)

src = files(
'src/funchook.c',
f'src/arch_@[email protected]',
f'src/os_@[email protected]',
f'src/disasm_@[email protected]',
)

extra_masmflags = []

if funchook_cpu == 'x86' and sizeof_void_p == 8
if cc.get_id() == 'msvc' and add_languages('masm', native: false)
prehook = 'prehook-x86_64-ms'
src += configure_file(
input: 'src' / f'@[email protected]',
output: f'@[email protected]',
copy: true,
)
elif funchook_os == 'windows'
src += files('src/prehook-x86_64-ms.S')
else
src += files('src/prehook-x86_64-sysv.S')
endif
endif

if funchook_cpu == 'x86' and sizeof_void_p == 4
if cc.get_id() == 'msvc' and add_languages('masm', native: false)
prehook = 'prehook-i686-ms'
src += configure_file(
input: 'src' / f'@[email protected]',
output: f'@[email protected]',
copy: true,
)
extra_masmflags += '/safeseh'
else
src += files('src/prehook-i686-gas.S')
endif
endif

if funchook_cpu == 'arm64'
if cc.get_id() == 'msvc' and add_languages('masm', native: false)
prehook = 'prehook-arm64-ms'
src += configure_file(
input: 'src' / f'@[email protected]',
output: f'@[email protected]',
copy: true,
)
else
src += files('src/prehook-arm64-gas.S')
endif
endif

inc = include_directories('include')

funchook_lib = library(
'funchook',
src,
include_directories: inc,
dependencies: funchook_deps,
masm_args: extra_masmflags,
version: meson.project_version(),
install: true,
)

install_headers(hdrs)

funchook_dep = declare_dependency(
link_with: funchook_lib,
include_directories: inc,
)

pkg = import('pkgconfig')
pkg.generate(
funchook_lib,
name: 'funchook',
description: 'Cross platform inline hooking library',
url: 'https://github.com/kubo/funchook',
)

meson.override_dependency('funchook', funchook_dep)
6 changes: 6 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
option(
'disasm',
type: 'combo',
choices: ['auto', 'Zydis', 'capstone'],
value: 'auto',
)
2 changes: 1 addition & 1 deletion src/disasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef _DInst funchook_insn_t;
#endif

#ifdef DISASM_CAPSTONE
#include <capstone/capstone.h>
#include <capstone.h>

typedef struct funchook_disasm {
funchook_t *funchook;
Expand Down
4 changes: 4 additions & 0 deletions src/disasm_Zydis.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ void funchook_disasm_x86_rip_relative(funchook_disasm_t *disasm, const funchook_
int i;
for (i = 0; i < insn->insn.operand_count; i++) {
const ZydisDecodedOperand *op = &insn->operands[i];
#if DISASM_ZYDIS_V5
if (op->mem.disp.size != 0 && op->mem.base == ZYDIS_REGISTER_RIP) {
#else
if (op->mem.disp.has_displacement && op->mem.base == ZYDIS_REGISTER_RIP) {
#endif
// Fix IP-relative addressing such as:
// mov eax, dword ptr [rip + 0x236eda]
// jmp qword ptr [rip + 0x239468]
Expand Down
2 changes: 1 addition & 1 deletion src/os_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ void *funchook_resolve_func(funchook_t *funchook, void *func)
if ((void*)lm->l_addr <= func) {
if (lmap == NULL) {
lmap = lm;
} else if (lmap->l_addr > lm->l_addr) {
} else if (lmap->l_addr < lm->l_addr) {
lmap = lm;
}
}
Expand Down
7 changes: 7 additions & 0 deletions subprojects/capstone.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[wrap-git]
url = https://github.com/frida/capstone.git
revision = e98746112da0a40b2ccd0340db0d20cca5f97950
depth = 1

[provide]
dependency_names = capstone
7 changes: 7 additions & 0 deletions subprojects/zydis.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[wrap-git]
url = https://github.com/zyantific/zydis.git
revision = bffbb610cfea643b98e87658b9058382f7522807
depth = 1

[provide]
dependency_names = zydis