Skip to content

Commit d5b550c

Browse files
committed
added support for ios
1 parent 89881ae commit d5b550c

File tree

12 files changed

+124
-74
lines changed

12 files changed

+124
-74
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ AllowShortIfStatementsOnASingleLine: true
1212
BreakBeforeBraces: Custom
1313
BraceWrapping:
1414
BeforeElse: true
15+
IndentPPDirectives: BeforeHash

.github/workflows/c-cpp.yml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ on:
88

99
jobs:
1010
build:
11+
runs-on: macos-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: build macos libraries
17+
run: ./build.sh macos
18+
19+
- name: upload build artifact
20+
uses: actions/upload-artifact@v4
21+
with:
22+
name: tinyhook-osx-universal
23+
path: libtinyhook.a libtinyhook.dylib include/tinyhook.h
24+
compression-level: 9
25+
26+
- name: build ios libraries
27+
run: ./build.sh ios
28+
29+
- name: upload build artifact
30+
uses: actions/upload-artifact@v4
31+
with:
32+
name: tinyhook-ios
33+
path: libtinyhook.a libtinyhook.dylib include/tinyhook.h
34+
compression-level: 9
35+
36+
test:
1137
strategy:
1238
matrix:
1339
include:
@@ -20,16 +46,6 @@ jobs:
2046

2147
steps:
2248
- uses: actions/checkout@v4
23-
- name: build libraries
24-
run: |
25-
make
26-
zip -j -9 tinyhook-${{ matrix.arch }}.zip libtinyhook.a libtinyhook.dylib include/tinyhook.h
2749

2850
- name: run test
2951
run: make test
30-
31-
- name: upload build artifact
32-
uses: actions/upload-artifact@v4
33-
with:
34-
name: tinyhook-${{ matrix.arch }}
35-
path: tinyhook-${{ matrix.arch }}.zip

Makefile

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
ARCH ?= $(shell uname -m)
2+
TARGET ?= macosx
23
OSX_VER ?= 10.15
3-
export MACOSX_DEPLOYMENT_TARGET=$(OSX_VER)
4+
IOS_VER ?= 12.0
45

56
CFLAGS := -arch $(ARCH) -O3 -Wall # -fsanitize=address
67
LDFLAGS := -flto -lobjc # -fsanitize=address
78
ASFLAGS := -arch $(ARCH)
89

10+
ifeq ($(TARGET), macosx)
11+
export MACOSX_DEPLOYMENT_TARGET=$(OSX_VER)
12+
endif
13+
14+
ifeq ($(TARGET), iphoneos)
15+
export IPHONEOS_DEPLOYMENT_TARGET=$(IOS_VER)
16+
CFLAGS += -isysroot $(shell xcrun --sdk $(TARGET) --show-sdk-path)
17+
endif
18+
919
SRC := $(shell find src -name "*.c")
1020
OBJ := $(patsubst %.c,%.o,$(wildcard $(SRC)))
1121
LIB_STATIC := libtinyhook.a
@@ -17,7 +27,11 @@ ifeq ($(ARCH), x86_64)
1727
OBJ += src/fde64/fde64.o
1828
endif
1929

20-
build: $(LIB_STATIC) $(LIB_SHARED)
30+
all: static shared
31+
32+
static: $(LIB_STATIC)
33+
34+
shared: $(LIB_SHARED)
2135

2236
$(LIB_STATIC): $(OBJ)
2337
ar -rcs $@ $^
@@ -28,10 +42,8 @@ $(LIB_SHARED): $(OBJ)
2842
test: $(LIB_STATIC)
2943
cd test && $(MAKE) run ARCH=$(ARCH)
3044

31-
all: build test
32-
3345
clean:
3446
cd test && $(MAKE) clean
3547
rm -f $(LIB_STATIC) $(LIB_SHARED) $(OBJ) $(FDE)
3648

37-
.PHONY: build test all clean
49+
.PHONY: all static shared test clean

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ Thanks to these projects for their inspiring idea and code!
7979

8080
- <https://github.com/rodionovd/rd_route>
8181
- <https://github.com/GiveMeZeny/fde64>
82+
- <https://github.com/facebook/fishhook>

build.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/zsh
2+
3+
set -e
4+
set -o pipefail
5+
6+
cd "$(dirname "$0")"
7+
8+
MIN_OSX="10.15"
9+
MIN_IOS="12.0"
10+
11+
function build_macos() {
12+
mkdir arm64 x86_64
13+
make clean
14+
make ARCH=x86_64 OSX_VER=$MIN_OSX # COMPACT=1
15+
mv libtinyhook.a libtinyhook.dylib x86_64
16+
make clean
17+
make ARCH=arm64 OSX_VER=$MIN_OSX # COMPACT=1
18+
mv libtinyhook.a libtinyhook.dylib arm64
19+
make clean
20+
21+
lipo -create x86_64/libtinyhook.a arm64/libtinyhook.a -o libtinyhook.a
22+
lipo -create x86_64/libtinyhook.dylib arm64/libtinyhook.dylib -o libtinyhook.dylib
23+
rm -r arm64 x86_64
24+
}
25+
26+
function build_ios() {
27+
make clean
28+
make TARGET=iphoneos IOS_VER=$MIN_IOS # COMPACT=1
29+
}
30+
31+
target=$1
32+
33+
if [[ $target == "macos" || $target == "" ]] {
34+
build_macos
35+
} elif [[ $target == "ios" ]] {
36+
build_ios
37+
} else {
38+
echo "unknown target!"
39+
exit 1
40+
}

include/tinyhook.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef tinyhook_h
22
#define tinyhook_h
33

4-
#include <objc/objc-runtime.h>
4+
#include <objc/runtime.h>
55
#include <stdbool.h>
66
#include <stddef.h>
77
#include <stdint.h>

src/interpose.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <mach-o/dyld.h>
55
#include <mach-o/loader.h>
66
#include <mach-o/nlist.h>
7-
#include <mach/mach_init.h> // mach_task_self()
8-
#include <mach/mach_vm.h> // mach_vm_*
97
#include <stdint.h>
108
#include <string.h>
119

src/memory.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#include <mach/mach_init.h> // mach_task_self()
2-
#include <mach/mach_vm.h> // mach_vm_*
3-
#include <string.h> // memcpy()
4-
51
#include "../include/tinyhook.h"
62
#include "private.h"
73

4+
#include <string.h> // memcpy()
5+
86
int read_mem(void *destination, const void *source, size_t len) {
97
int kr = 0;
108
vm_offset_t data;

src/objcrt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#include <objc/runtime.h> // objc_*, ...
2-
31
#include "../include/tinyhook.h"
42
#include "private.h"
53

4+
#include <objc/runtime.h> // objc_*, ...
5+
66
Method ocrt_method(char type, const char *cls, const char *sel) {
77
Method oc_method = NULL;
88
Class oc_class = objc_getClass(cls);

src/private.h

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
#ifndef TINYHOOK_PRIVATE_H
22
#define TINYHOOK_PRIVATE_H
33

4-
#ifndef COMPACT
5-
#include <mach/mach_error.h> // mach_error_string()
6-
#include <printf.h> // fprintf()
7-
#define LOG_ERROR(fmt, ...) fprintf(stderr, "ERROR [%s:%d]: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
4+
#include <TargetConditionals.h>
5+
6+
#include <mach/mach_init.h> // mach_task_self()
7+
#if TARGET_OS_IPHONE
8+
#include <mach/vm_map.h> // vm_*
9+
#define mach_vm_address_t vm_address_t
10+
#define mach_vm_allocate vm_allocate
11+
#define mach_vm_deallocate vm_deallocate
12+
#define mach_vm_read vm_read
13+
#define mach_vm_write vm_write
14+
#define mach_vm_protect vm_protect
15+
#elif TARGET_OS_MAC
16+
#include <mach/mach_vm.h> // mach_vm_*
17+
#endif
18+
19+
#ifdef COMPACT
20+
#define LOG_ERROR(fmt, ...) ((void)0)
821
#else
9-
#define LOG_ERROR(fmt, ...) ((void)0)
22+
#include <mach/mach_error.h> // mach_error_string()
23+
#include <printf.h> // fprintf()
24+
#define LOG_ERROR(fmt, ...) fprintf(stderr, "ERROR [%s:%d]: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
1025
#endif
1126

1227
#define MB (1ll << 20)
1328
#define GB (1ll << 30)
1429

1530
#ifdef __aarch64__
16-
#define AARCH64_B 0x14000000 // b +0
17-
#define AARCH64_BL 0x94000000 // bl +0
18-
#define AARCH64_ADRP 0x90000011 // adrp x17, 0
19-
#define AARCH64_BR 0xd61f0220 // br x17
20-
#define AARCH64_BLR 0xd63f0220 // blr x17
21-
#define AARCH64_ADD 0x91000231 // add x17, x17, 0
22-
#define AARCH64_SUB 0xd1000231 // sub x17, x17, 0
23-
31+
#define AARCH64_B 0x14000000 // b +0
32+
#define AARCH64_BL 0x94000000 // bl +0
33+
#define AARCH64_ADRP 0x90000011 // adrp x17, 0
34+
#define AARCH64_BR 0xd61f0220 // br x17
35+
#define AARCH64_BLR 0xd63f0220 // blr x17
36+
#define AARCH64_ADD 0x91000231 // add x17, x17, 0
37+
#define AARCH64_SUB 0xd1000231 // sub x17, x17, 0
2438
#elif __x86_64__
25-
#define X86_64_CALL 0xe8 // call
26-
#define X86_64_JMP 0xe9 // jmp
27-
#define X86_64_JMP_RIP 0x000025ff // jmp [rip]
28-
#define X86_64_CALL_RIP 0x000015ff // call [rip]
29-
#define X86_64_MOV_RI64 0xb848 // mov r64, m64
30-
#define X86_64_MOV_RM64 0x8b48 // mov r64, [r64]
39+
#define X86_64_CALL 0xe8 // call
40+
#define X86_64_JMP 0xe9 // jmp
41+
#define X86_64_JMP_RIP 0x000025ff // jmp [rip]
42+
#define X86_64_CALL_RIP 0x000015ff // call [rip]
43+
#define X86_64_MOV_RI64 0xb848 // mov r64, m64
44+
#define X86_64_MOV_RM64 0x8b48 // mov r64, [r64]
3145
#endif
3246

3347
bool need_far_jump(void *src, void *dst);

0 commit comments

Comments
 (0)