Skip to content

Commit 0b6b6fb

Browse files
committed
fixed some ide warnings..
1 parent d1da513 commit 0b6b6fb

File tree

9 files changed

+36
-22
lines changed

9 files changed

+36
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*.dSYM
2121
*.dylib
2222
.zed
23+
.idea
2324
.vscode
2425
example
2526
infer-out

include/tinyhook.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#define tinyhook_h
33

44
#include <objc/runtime.h>
5-
#include <stdbool.h>
6-
#include <stddef.h>
75
#include <stdint.h>
86

97
#ifdef __cplusplus
@@ -27,7 +25,7 @@ int tiny_hook(void *function, void *destination, void **origin);
2725

2826
int tiny_hook_ex(th_bak_t *bak, void *function, void *destination, void **origin);
2927

30-
int tiny_unhook_ex(th_bak_t *bak);
28+
int tiny_unhook_ex(const th_bak_t *bak);
3129

3230
int tiny_insert(void *address, void *destination);
3331

src/exhook.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "../include/tinyhook.h"
22
#include "private.h"
33

4-
static int get_jump_size(void *src, void *dst) {
4+
static int get_jump_size(const void *src, const void *dst) {
55
bool is_far = need_far_jump(src, dst);
66
#ifdef __arm64__
77
return is_far ? 12 : 4;
@@ -17,6 +17,6 @@ int tiny_hook_ex(th_bak_t *bak, void *function, void *destination, void **origin
1717
return tiny_hook(function, destination, origin);
1818
}
1919

20-
int tiny_unhook_ex(th_bak_t *bak) {
20+
int tiny_unhook_ex(const th_bak_t *bak) {
2121
return write_mem(bak->address, bak->head_bak, bak->jump_size);
2222
}

src/interpose.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ int tiny_interpose(uint32_t image_index, const char *symbol_name, void *replacem
2121
if (strcmp(segment->segname, "__DATA_CONST") == 0) {
2222
if (sym_sects[0]) continue;
2323
struct section_64 *data_const_sect = (void *)(segment + 1);
24-
for (int i = 0; i < segment->nsects; i++) {
25-
if ((data_const_sect[i].flags & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS) {
26-
sym_sects[0] = data_const_sect + i; // __nl_symbol_ptr
24+
for (int j = 0; j < segment->nsects; j++) {
25+
if ((data_const_sect[j].flags & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS) {
26+
sym_sects[0] = data_const_sect + j; // __nl_symbol_ptr
2727
}
2828
}
2929
}
3030
else if (strcmp(segment->segname, "__DATA") == 0) {
3131
if (sym_sects[1]) continue;
3232
struct section_64 *data_sect = (void *)(segment + 1);
33-
for (int i = 0; i < segment->nsects; i++) {
34-
if ((data_sect[i].flags & SECTION_TYPE) == S_LAZY_SYMBOL_POINTERS) {
35-
sym_sects[1] = data_sect + i; // __la_symbol_ptr
33+
for (int j = 0; j < segment->nsects; j++) {
34+
if ((data_sect[j].flags & SECTION_TYPE) == S_LAZY_SYMBOL_POINTERS) {
35+
sym_sects[1] = data_sect + j; // __la_symbol_ptr
3636
}
3737
}
3838
}
@@ -48,7 +48,11 @@ int tiny_interpose(uint32_t image_index, const char *symbol_name, void *replacem
4848
}
4949
ld_command = (void *)ld_command + ld_command->cmdsize;
5050
}
51-
intptr_t linkedit_base = image_slide + linkedit_cmd->vmaddr - linkedit_cmd->fileoff;
51+
if (linkedit_cmd == NULL || symtab_cmd == NULL || dysymtab_cmd == NULL) {
52+
LOG_ERROR("tiny_interpose: bad mach-o structure!");
53+
return 1;
54+
}
55+
uint64_t linkedit_base = image_slide + linkedit_cmd->vmaddr - linkedit_cmd->fileoff;
5256
char *str_tbl = (void *)linkedit_base + symtab_cmd->stroff;
5357
struct nlist_64 *nl_tbl = (void *)linkedit_base + symtab_cmd->symoff;
5458
uint32_t *indirect_sym_tbl = (void *)linkedit_base + dysymtab_cmd->indirectsymoff;
@@ -60,7 +64,7 @@ int tiny_interpose(uint32_t image_index, const char *symbol_name, void *replacem
6064
if (!sym_sec) continue;
6165
void **sym_ptrs = (void *)sym_sec->addr + image_slide;
6266
uint32_t *indirect_sym_entry = indirect_sym_tbl + sym_sec->reserved1; // reserved1 is an index!
63-
int nptrs = sym_sec->size / sizeof(void *);
67+
size_t nptrs = sym_sec->size / sizeof(void *);
6468
for (int j = 0; j < nptrs; j++) {
6569
uint32_t symtab_index = indirect_sym_entry[j];
6670
if (symtab_index == INDIRECT_SYMBOL_LOCAL ||

src/memory.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "private.h"
33

44
#include <string.h> // memcpy()
5+
#ifndef COMPACT
6+
#include <mach/mach_error.h> // mach_error_string()
7+
#endif
58

69
int read_mem(void *destination, const void *source, size_t len) {
710
int kr = 0;

src/private.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#ifdef COMPACT
2020
#define LOG_ERROR(fmt, ...) ((void)0)
2121
#else
22-
#include <mach/mach_error.h> // mach_error_string()
2322
#include <printf.h> // fprintf()
2423
#define LOG_ERROR(fmt, ...) fprintf(stderr, "ERROR [%s:%d]: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
2524
#endif
@@ -44,6 +43,6 @@
4443
#define X86_64_MOV_RM64 0x8b48 // mov r64, [r64]
4544
#endif
4645

47-
bool need_far_jump(void *src, void *dst);
46+
bool need_far_jump(const void *src, const void *dst);
4847

4948
#endif

src/symsolve/symexport.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ void *symexp_solve(uint32_t image_index, const char *symbol_name) {
8787
LOG_ERROR("symexp_solve: LC_DYLD_INFO_ONLY segment not found!");
8888
return NULL;
8989
}
90+
if (linkedit_cmd == NULL) {
91+
LOG_ERROR("symexp_solve: __LINKEDIT segment not found!");
92+
return NULL;
93+
}
9094
// stroff and strtbl are in the __LINKEDIT segment
9195
// Its offset will change when loaded into the memory, so we need to add this slide
92-
intptr_t linkedit_base = image_slide + linkedit_cmd->vmaddr - linkedit_cmd->fileoff;
96+
uint64_t linkedit_base = image_slide + linkedit_cmd->vmaddr - linkedit_cmd->fileoff;
9397
uint8_t *export_offset = (uint8_t *)linkedit_base + dyldinfo_cmd->export_off;
9498
symbol_address = trie_query(export_offset, symbol_name);
9599

src/symsolve/symtable.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ void *symtbl_solve(uint32_t image_index, const char *symbol_name) {
3131
}
3232
ld_command = (void *)ld_command + ld_command->cmdsize;
3333
}
34+
if (linkedit_cmd == NULL || symtab_cmd == NULL) {
35+
LOG_ERROR("symtbl_solve: bad mach-o structure!");
36+
return NULL;
37+
}
3438
// stroff and strtbl are in the __LINKEDIT segment
3539
// Its offset will change when loaded into the memory, so we need to add this slide
36-
intptr_t linkedit_base = image_slide + linkedit_cmd->vmaddr - linkedit_cmd->fileoff;
40+
uint64_t linkedit_base = image_slide + linkedit_cmd->vmaddr - linkedit_cmd->fileoff;
3741
struct nlist_64 *nl_tbl = (void *)linkedit_base + symtab_cmd->symoff;
3842
char *str_tbl = (void *)linkedit_base + symtab_cmd->stroff;
3943
for (int j = 0; j < symtab_cmd->nsyms; j++) {

src/tinyhook.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#include <stdint.h>
55
#include <string.h> // memcpy()
6+
#ifndef COMPACT
7+
#include <mach/mach_error.h> // mach_error_string()
8+
#endif
69

710
#ifdef __x86_64__
811
#include "fde64/fde64.h"
@@ -13,8 +16,7 @@ static int calc_near_jump(uint8_t *output, void *src, void *dst, bool link) {
1316
#ifdef __aarch64__
1417
// b/bl imm ; go to dst
1518
jump_size = 4;
16-
uint32_t insn;
17-
insn = (dst - src) >> 2 & 0x3ffffff;
19+
uint32_t insn = (dst - src) >> 2 & 0x3ffffff;
1820
insn |= link ? AARCH64_BL : AARCH64_B;
1921
*(uint32_t *)output = insn;
2022
#elif __x86_64__
@@ -33,8 +35,7 @@ static int calc_far_jump(uint8_t *output, void *src, void *dst, bool link) {
3335
// add x17, x17, imm ; x17 -> dst
3436
// br/blr x17
3537
jump_size = 12;
36-
uint32_t insn;
37-
insn = (((int64_t)dst >> 12) - ((int64_t)src >> 12)) & 0x1fffff;
38+
uint32_t insn = (((int64_t)dst >> 12) - ((int64_t)src >> 12)) & 0x1fffff;
3839
insn = ((insn & 0x3) << 29) | (insn >> 2 << 5) | AARCH64_ADRP;
3940
*(uint32_t *)output = insn;
4041
insn = ((int64_t)dst & 0xfff) << 10 | AARCH64_ADD;
@@ -50,7 +51,7 @@ static int calc_far_jump(uint8_t *output, void *src, void *dst, bool link) {
5051
return jump_size;
5152
}
5253

53-
bool need_far_jump(void *src, void *dst) {
54+
bool need_far_jump(const void *src, const void *dst) {
5455
long long distance = dst > src ? dst - src : src - dst;
5556
#ifdef __aarch64__
5657
return distance >= 128 * MB;

0 commit comments

Comments
 (0)