Skip to content

Commit

Permalink
Add meson build system support
Browse files Browse the repository at this point in the history
  • Loading branch information
mochaaP committed Sep 16, 2024
1 parent 2aca28e commit 92d197d
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 6 deletions.
82 changes: 77 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,80 @@
*.disasm
*.dll
*.lib
### C ###
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*~
/build*
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

### Meson ###
# subproject directories
/subprojects/*
!/subprojects/packagefiles/
!/subprojects/*.wrap

# Meson Directories
meson-logs
meson-private

# Meson Files
meson_benchmark_setup.dat
meson_test_setup.dat
sanitycheckcpp.cc # C++ specific
sanitycheckcpp.exe # C++ specific

# Ninja
build.ninja
.ninja_deps
.ninja_logs

# Misc
compile_commands.json

/build*
/build
182 changes: 182 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
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'
funchook_deps += dependency(
'zydis',
version: '>=4.0.0',
default_options: {
'minimal': 'enabled',
'decoder': 'enabled',
},
)
cdata.set10('DISASM_ZYDIS', true)
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
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 = cb487f1cb477b2c03345aa72baf7eda725b77507
depth = 1

[provide]
dependency_names = zydis

0 comments on commit 92d197d

Please sign in to comment.