使用Theos %hookf Hook iOS OC应用C函数报错,求解答
关于Objective-C中使用%hookf Hook C函数的问题
原目标C函数实现
NSNumber *addition(NSNumber *num1, NSNumber *num2) { double result = [num1 doubleValue] + [num2 doubleValue]; return @(result); }
我编写的Tweak代码
#import <UIKit/UIKit.h> NSInteger addition(NSInteger num1, NSInteger num2); %hookf(NSInteger, addition, NSInteger num1, NSInteger num2) { return 99; }
编译时出现的错误
==> Linking tweak objchooker (armv7)… ld: warning: -multiply_defined is obsolete Undefined symbols for architecture armv7: "_addition", referenced from: __logosLocalInit in Tweak.x.f54e192c.o ld: symbol(s) not found for architecture armv7 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: *** [/Users/hnin.sin/Desktop/objchooker/.theos/obj/debug/armv7/objchooker.dylib] Error 1 make[2]: *** [/Users/hnin.sin/Desktop/objchooker/.theos/obj/debug/armv7/objchooker.dylib] Error 2 make[1]: *** [internal-library-all_] Error 2 make: *** [objchooker.all.tweak.variables] Error 2
我的疑问
在Swift测试应用中使用%hook和%hookf均正常,但在Objective-C应用中仅%hook可用。请问是我的%hookf用法有误,还是该语法不适用于Objective-C应用?
问题原因及解决方法
- 函数签名完全不匹配:原函数的返回值是
NSNumber*、参数是两个NSNumber*类型,但你在Tweak里声明的是NSInteger类型的返回值和参数,Logos无法匹配到正确的目标函数符号,直接导致链接错误。 - %hookf语法本身完全适用于Objective-C应用,核心问题是你没有严格对齐目标函数的签名(返回值、参数类型都要和原函数一致)。
- 链接错误里的
undefined symbols本质是Logos生成的代码找不到对应签名的_addition符号,因为你声明的函数和原函数根本不是同一个。
修正后的Tweak代码
#import <UIKit/UIKit.h> #import <Foundation/Foundation.h> // 严格匹配原函数的签名 NSNumber* addition(NSNumber* num1, NSNumber* num2); %hookf(NSNumber*, addition, NSNumber* num1, NSNumber* num2) { // 自定义逻辑,比如返回固定值99 return @(99); }
额外注意事项
- 确保目标应用中的
addition是全局导出的C函数(不能用static修饰),static函数属于文件私有,无法被外部Tweak Hook。 - 确认Tweak的编译架构和目标应用一致(比如同时支持armv7和arm64),避免架构不匹配导致的符号问题。
内容的提问来源于stack exchange,提问作者pilokenorse
相关产品推荐
相关产品推荐

