Tengeneza dylib na sehemu ya __interpose (__DATA___interpose) (au sehemu iliyofungwa na S_INTERPOSING) inayojumuisha jozi za pointa za kazi zinazorejelea kazi za awali na kazi mbadala.
Chaguo la mazingira la DYLD_PRINT_INTERPOSTING linaweza kutumika kwa kusudi la kudekta interposing na litachapisha mchakato wa interpose.
Pia elewa kwamba interposing hutokea kati ya mchakato na maktaba zilizopakiwa, haitafanyi kazi na hifadhi ya maktaba iliyoshirikiwa.
Interposing ya Kisasa
Sasa pia ni iwezekanavyo kuingiza kazi kwa njia ya kisasa kwa kutumia kazi ya dyld_dynamic_interpose. Hii inaruhusu kuingiza kazi kwa njia ya programu wakati wa muda wa uendeshaji badala ya kufanya hivyo tu kutoka mwanzoni.
Inahitajika tu kuonyesha tuples ya kazi ya kuchukua nafasi na kazi mbadala.
Katika ObjectiveC hivi ndivyo njia inavyoitwa: [myClassInstance nameOfTheMethodFirstParam:param1 secondParam:param2]
Inahitajika kitu, njia na vipimo. Na wakati njia inaitwa msg inatumwa kutumia kazi objc_msgSend: int i = ((int (*)(id, SEL, NSString *, NSString *))objc_msgSend)(someObject, @selector(method1p1:p2:), value1, value2);
Kitu ni someObject, njia ni @selector(method1p1:p2:) na hoja ni value1, value2.
Kufuatia miundo ya vitu, ni rahisi kufikia orodha ya njia ambapo majina na pointi kwa msimbo wa njia zinapatikana.
Tafadhali kumbuka kwamba kwa sababu njia na madarasa vinapata kulingana na majina yao, habari hii imehifadhiwa kwenye binary, hivyo ni rahisi kuipata kwa kutumia otool -ov </path/bin> au class-dump </path/bin>
Kufikia njia za asili
Inawezekana kupata habari za njia kama jina, idadi ya vipimo au anwani kama ilivyo katika mfano ufuatao:
// gcc -framework Foundation test.m -o test
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
int main() {
// Get class of the variable
NSString* str = @"This is an example";
Class strClass = [str class];
NSLog(@"str's Class name: %s", class_getName(strClass));
// Get parent class of a class
Class strSuper = class_getSuperclass(strClass);
NSLog(@"Superclass name: %@",NSStringFromClass(strSuper));
// Get information about a method
SEL sel = @selector(length);
NSLog(@"Selector name: %@", NSStringFromSelector(sel));
Method m = class_getInstanceMethod(strClass,sel);
NSLog(@"Number of arguments: %d", method_getNumberOfArguments(m));
NSLog(@"Implementation address: 0x%lx", (unsigned long)method_getImplementation(m));
// Iterate through the class hierarchy
NSLog(@"Listing methods:");
Class currentClass = strClass;
while (currentClass != NULL) {
unsigned int inheritedMethodCount = 0;
Method* inheritedMethods = class_copyMethodList(currentClass, &inheritedMethodCount);
NSLog(@"Number of inherited methods in %s: %u", class_getName(currentClass), inheritedMethodCount);
for (unsigned int i = 0; i < inheritedMethodCount; i++) {
Method method = inheritedMethods[i];
SEL selector = method_getName(method);
const char* methodName = sel_getName(selector);
unsigned long address = (unsigned long)method_getImplementation(m);
NSLog(@"Inherited method name: %s (0x%lx)", methodName, address);
}
// Free the memory allocated by class_copyMethodList
free(inheritedMethods);
currentClass = class_getSuperclass(currentClass);
}
// Other ways to call uppercaseString method
if([str respondsToSelector:@selector(uppercaseString)]) {
NSString *uppercaseString = [str performSelector:@selector(uppercaseString)];
NSLog(@"Uppercase string: %@", uppercaseString);
}
// Using objc_msgSend directly
NSString *uppercaseString2 = ((NSString *(*)(id, SEL))objc_msgSend)(str, @selector(uppercaseString));
NSLog(@"Uppercase string: %@", uppercaseString2);
// Calling the address directly
IMP imp = method_getImplementation(class_getInstanceMethod(strClass, @selector(uppercaseString))); // Get the function address
NSString *(*callImp)(id,SEL) = (typeof(callImp))imp; // Generates a function capable to method from imp
NSString *uppercaseString3 = callImp(str,@selector(uppercaseString)); // Call the method
NSLog(@"Uppercase string: %@", uppercaseString3);
return 0;
}